e9a02619e6f93b70174167ea21f772a3d7f1290a
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30 (eval-when-compile
31   (require 'cl))
32 (defvar tool-bar-map)
33 (defvar w3m-minor-mode-map)
34
35 (require 'gnus)
36 (require 'gnus-sum)
37 (require 'gnus-spec)
38 (require 'gnus-int)
39 (require 'gnus-win)
40 (require 'mm-bodies)
41 (require 'mail-parse)
42 (require 'mm-decode)
43 (require 'mm-view)
44 (require 'wid-edit)
45 (require 'mm-uu)
46 (require 'message)
47
48 (autoload 'gnus-msg-mail "gnus-msg" nil t)
49 (autoload 'gnus-button-mailto "gnus-msg")
50 (autoload 'gnus-button-reply "gnus-msg" nil t)
51 (autoload 'parse-time-string "parse-time" nil nil)
52 (autoload 'ansi-color-apply-on-region "ansi-color")
53 (autoload 'mm-url-insert-file-contents-external "mm-url")
54 (autoload 'mm-extern-cache-contents "mm-extern")
55
56 (defgroup gnus-article nil
57   "Article display."
58   :link '(custom-manual "(gnus)Article Buffer")
59   :group 'gnus)
60
61 (defgroup gnus-article-treat nil
62   "Treating article parts."
63   :link '(custom-manual "(gnus)Article Hiding")
64   :group 'gnus-article)
65
66 (defgroup gnus-article-hiding nil
67   "Hiding article parts."
68   :link '(custom-manual "(gnus)Article Hiding")
69   :group 'gnus-article)
70
71 (defgroup gnus-article-highlight nil
72   "Article highlighting."
73   :link '(custom-manual "(gnus)Article Highlighting")
74   :group 'gnus-article
75   :group 'gnus-visual)
76
77 (defgroup gnus-article-signature nil
78   "Article signatures."
79   :link '(custom-manual "(gnus)Article Signature")
80   :group 'gnus-article)
81
82 (defgroup gnus-article-headers nil
83   "Article headers."
84   :link '(custom-manual "(gnus)Hiding Headers")
85   :group 'gnus-article)
86
87 (defgroup gnus-article-washing nil
88   "Special commands on articles."
89   :link '(custom-manual "(gnus)Article Washing")
90   :group 'gnus-article)
91
92 (defgroup gnus-article-emphasis nil
93   "Fontisizing articles."
94   :link '(custom-manual "(gnus)Article Fontisizing")
95   :group 'gnus-article)
96
97 (defgroup gnus-article-saving nil
98   "Saving articles."
99   :link '(custom-manual "(gnus)Saving Articles")
100   :group 'gnus-article)
101
102 (defgroup gnus-article-mime nil
103   "Worshiping the MIME wonder."
104   :link '(custom-manual "(gnus)Using MIME")
105   :group 'gnus-article)
106
107 (defgroup gnus-article-buttons nil
108   "Pushable buttons in the article buffer."
109   :link '(custom-manual "(gnus)Article Buttons")
110   :group 'gnus-article)
111
112 (defgroup gnus-article-various nil
113   "Other article options."
114   :link '(custom-manual "(gnus)Misc Article")
115   :group 'gnus-article)
116
117 (defcustom gnus-ignored-headers
118   (mapcar
119    (lambda (header)
120      (concat "^" header ":"))
121    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
122      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
123      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
124      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
125      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
126      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
127      "X-Attribution" "X-Originating-IP" "Delivered-To"
128      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
129      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
130      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
131      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
132      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
133      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
134      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
135      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
136      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
137      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
138      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
139      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
140      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
141      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
142      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
143      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
144      "List-[A-Za-z]+" "X-Listprocessor-Version"
145      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
146      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
147      "X-Received" "Content-length" "X-precedence"
148      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
149      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
150      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
151      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
152      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
153      "X-Content-length" "X-Posting-Agent" "Original-Received"
154      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
155      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
156      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
157      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
158      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
159      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
160      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
161      "Envelope-Sender" "Envelope-Recipients"))
162   "*All headers that start with this regexp will be hidden.
163 This variable can also be a list of regexps of headers to be ignored.
164 If `gnus-visible-headers' is non-nil, this variable will be ignored."
165   :type '(choice :custom-show nil
166                  regexp
167                  (repeat regexp))
168   :group 'gnus-article-hiding)
169
170 (defcustom gnus-visible-headers
171   "^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:"
172   "*All headers that do not match this regexp will be hidden.
173 This variable can also be a list of regexp of headers to remain visible.
174 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
175   :type '(choice
176           (repeat :value-to-internal (lambda (widget value)
177                                        (custom-split-regexp-maybe value))
178                   :match (lambda (widget value)
179                            (or (stringp value)
180                                (widget-editable-list-match widget value)))
181                   regexp)
182           (const :tag "Use gnus-ignored-headers" nil)
183           regexp)
184   :group 'gnus-article-hiding)
185
186 (defcustom gnus-sorted-header-list
187   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
188     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
189   "*This variable is a list of regular expressions.
190 If it is non-nil, headers that match the regular expressions will
191 be placed first in the article buffer in the sequence specified by
192 this list."
193   :type '(repeat regexp)
194   :group 'gnus-article-hiding)
195
196 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
197   "Headers that are only to be displayed if they have interesting data.
198 Possible values in this list are:
199
200   'empty       Headers with no content.
201   'newsgroups  Newsgroup identical to Gnus group.
202   'to-address  To identical to To-address.
203   'to-list     To identical to To-list.
204   'cc-list     CC identical to To-list.
205   'followup-to Followup-to identical to Newsgroups.
206   'reply-to    Reply-to identical to From.
207   'date        Date less than four days old.
208   'long-to     To and/or Cc longer than 1024 characters.
209   'many-to     Multiple To and/or Cc."
210   :type '(set (const :tag "Headers with no content." empty)
211               (const :tag "Newsgroups identical to Gnus group." newsgroups)
212               (const :tag "To identical to To-address." to-address)
213               (const :tag "To identical to To-list." to-list)
214               (const :tag "CC identical to To-list." cc-list)
215               (const :tag "Followup-to identical to Newsgroups." followup-to)
216               (const :tag "Reply-to identical to From." reply-to)
217               (const :tag "Date less than four days old." date)
218               (const :tag "To and/or Cc longer than 1024 characters." long-to)
219               (const :tag "Multiple To and/or Cc headers." many-to))
220   :group 'gnus-article-hiding)
221
222 (defcustom gnus-article-skip-boring nil
223   "Skip over text that is not worth reading.
224 By default, if you set this t, then Gnus will display citations and
225 signatures, but will never scroll down to show you a page consisting
226 only of boring text.  Boring text is controlled by
227 `gnus-article-boring-faces'."
228   :version "22.1"
229   :type 'boolean
230   :group 'gnus-article-hiding)
231
232 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
233   "Regexp matching signature separator.
234 This can also be a list of regexps.  In that case, it will be checked
235 from head to tail looking for a separator.  Searches will be done from
236 the end of the buffer."
237   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
238                  (regexp)
239                  (repeat :tag "List of regexp" regexp))
240   :group 'gnus-article-signature)
241
242 (defcustom gnus-signature-limit nil
243   "Provide a limit to what is considered a signature.
244 If it is a number, no signature may not be longer (in characters) than
245 that number.  If it is a floating point number, no signature may be
246 longer (in lines) than that number.  If it is a function, the function
247 will be called without any parameters, and if it returns nil, there is
248 no signature in the buffer.  If it is a string, it will be used as a
249 regexp.  If it matches, the text in question is not a signature.
250
251 This can also be a list of the above values."
252   :type '(choice (const nil)
253                  (integer :value 200)
254                  (number :value 4.0)
255                  function
256                  (regexp :value ".*"))
257   :group 'gnus-article-signature)
258
259 (defcustom gnus-hidden-properties '(invisible t intangible t)
260   "Property list to use for hiding text."
261   :type 'sexp
262   :group 'gnus-article-hiding)
263
264 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
265 ;; frames in a session.
266 (defcustom gnus-article-x-face-command
267   (if (featurep 'xemacs)
268       (if (or (gnus-image-type-available-p 'xface)
269               (gnus-image-type-available-p 'pbm))
270           'gnus-display-x-face-in-from
271         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
272     (if (gnus-image-type-available-p 'pbm)
273         'gnus-display-x-face-in-from
274       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
275 display -"))
276   "*String or function to be executed to display an X-Face header.
277 If it is a string, the command will be executed in a sub-shell
278 asynchronously.  The compressed face will be piped to this command."
279   :type `(choice string
280                  (function-item gnus-display-x-face-in-from)
281                  function)
282   :version "21.1"
283   :group 'gnus-picon
284   :group 'gnus-article-washing)
285
286 (defcustom gnus-article-x-face-too-ugly nil
287   "Regexp matching posters whose face shouldn't be shown automatically."
288   :type '(choice regexp (const nil))
289   :group 'gnus-article-washing)
290
291 (defcustom gnus-article-banner-alist nil
292   "Banner alist for stripping.
293 For example,
294      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
295   :version "21.1"
296   :type '(repeat (cons symbol regexp))
297   :group 'gnus-article-washing)
298
299 (gnus-define-group-parameter
300  banner
301  :variable-document
302  "Alist of regexps (to match group names) and banner."
303  :variable-group gnus-article-washing
304  :parameter-type
305  '(choice :tag "Banner"
306           :value nil
307           (const :tag "Remove signature" signature)
308           (symbol :tag "Item in `gnus-article-banner-alist'" none)
309           regexp
310           (const :tag "None" nil))
311  :parameter-document
312  "If non-nil, specify how to remove `banners' from articles.
313
314 Symbol `signature' means to remove signatures delimited by
315 `gnus-signature-separator'.  Any other symbol is used to look up a
316 regular expression to match the banner in `gnus-article-banner-alist'.
317 A string is used as a regular expression to match the banner
318 directly.")
319
320 (defcustom gnus-article-address-banner-alist nil
321   "Alist of mail addresses and banners.
322 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
323 to match a mail address in the From: header, BANNER is one of a symbol
324 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
325 If ADDRESS matches author's mail address, it will remove things like
326 advertisements.  For example:
327
328 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
329 "
330   :type '(repeat
331           (cons
332            (regexp :tag "Address")
333            (choice :tag "Banner" :value nil
334                    (const :tag "Remove signature" signature)
335                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
336                    regexp
337                    (const :tag "None" nil))))
338   :version "22.1"
339   :group 'gnus-article-washing)
340
341 (defmacro gnus-emphasis-custom-with-format (&rest body)
342   `(let ((format "\
343 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
344 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
345      ,@body))
346
347 (defun gnus-emphasis-custom-value-to-external (value)
348   (gnus-emphasis-custom-with-format
349    (if (consp (car value))
350        (list (format format (car (car value)) (cdr (car value)))
351              2
352              (if (nth 1 value) 2 3)
353              (nth 2 value))
354      value)))
355
356 (defun gnus-emphasis-custom-value-to-internal (value)
357   (gnus-emphasis-custom-with-format
358    (let ((regexp (concat "\\`"
359                          (format (regexp-quote format)
360                                  "\\([^()]+\\)" "\\([^()]+\\)")
361                          "\\'"))
362          pattern)
363      (if (string-match regexp (setq pattern (car value)))
364          (list (cons (match-string 1 pattern) (match-string 2 pattern))
365                (= (nth 2 value) 2)
366                (nth 3 value))
367        value))))
368
369 (defcustom gnus-emphasis-alist
370   (let ((types
371          '(("\\*" "\\*" bold nil 2)
372            ("_" "_" underline)
373            ("/" "/" italic)
374            ("_/" "/_" underline-italic)
375            ("_\\*" "\\*_" underline-bold)
376            ("\\*/" "/\\*" bold-italic)
377            ("_\\*/" "/\\*_" underline-bold-italic))))
378     (nconc
379      (gnus-emphasis-custom-with-format
380       (mapcar (lambda (spec)
381                 (list (format format (car spec) (cadr spec))
382                       (or (nth 3 spec) 2)
383                       (or (nth 4 spec) 3)
384                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
385               types))
386      '(;; I've never seen anyone use this strikethru convention whereas I've
387        ;; several times seen it triggered by normal text.  --Stef
388        ;; Miles suggests that this form is sometimes used but for italics,
389        ;; so maybe we should map it to `italic'.
390        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
391        ;; 2 3 gnus-emphasis-strikethru)
392        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
393         2 3 gnus-emphasis-underline))))
394   "*Alist that says how to fontify certain phrases.
395 Each item looks like this:
396
397   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
398
399 The first element is a regular expression to be matched.  The second
400 is a number that says what regular expression grouping used to find
401 the entire emphasized word.  The third is a number that says what
402 regexp grouping should be displayed and highlighted.  The fourth
403 is the face used for highlighting."
404   :type
405   '(repeat
406     (menu-choice
407      :format "%[Customizing Style%]\n%v"
408      :indent 2
409      (group :tag "Default"
410             :value ("" 0 0 default)
411             :value-create
412             (lambda (widget)
413               (let ((value (widget-get
414                             (cadr (widget-get (widget-get widget :parent)
415                                               :args))
416                             :value)))
417                 (if (not (eq (nth 2 value) 'default))
418                     (widget-put
419                      widget
420                      :value
421                      (gnus-emphasis-custom-value-to-external value))))
422               (widget-group-value-create widget))
423             regexp
424             (integer :format "Match group: %v")
425             (integer :format "Emphasize group: %v")
426             face)
427      (group :tag "Simple"
428             :value (("_" . "_") nil default)
429             (cons :format "%v"
430                   (regexp :format "Start regexp: %v")
431                   (regexp :format "End regexp: %v"))
432             (boolean :format "Show start and end patterns: %[%v%]\n"
433                      :on " On " :off " Off ")
434             face)))
435   :get (lambda (symbol)
436          (mapcar 'gnus-emphasis-custom-value-to-internal
437                  (default-value symbol)))
438   :set (lambda (symbol value)
439          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
440                                      value)))
441   :group 'gnus-article-emphasis)
442
443 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
444   "A regexp to describe whitespace which should not be emphasized.
445 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
446 The former avoids underlining of leading and trailing whitespace,
447 and the latter avoids underlining any whitespace at all."
448   :version "21.1"
449   :group 'gnus-article-emphasis
450   :type 'regexp)
451
452 (defface gnus-emphasis-bold '((t (:bold t)))
453   "Face used for displaying strong emphasized text (*word*)."
454   :group 'gnus-article-emphasis)
455
456 (defface gnus-emphasis-italic '((t (:italic t)))
457   "Face used for displaying italic emphasized text (/word/)."
458   :group 'gnus-article-emphasis)
459
460 (defface gnus-emphasis-underline '((t (:underline t)))
461   "Face used for displaying underlined emphasized text (_word_)."
462   :group 'gnus-article-emphasis)
463
464 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
465   "Face used for displaying underlined bold emphasized text (_*word*_)."
466   :group 'gnus-article-emphasis)
467
468 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
469   "Face used for displaying underlined italic emphasized text (_/word/_)."
470   :group 'gnus-article-emphasis)
471
472 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
473   "Face used for displaying bold italic emphasized text (/*word*/)."
474   :group 'gnus-article-emphasis)
475
476 (defface gnus-emphasis-underline-bold-italic
477   '((t (:bold t :italic t :underline t)))
478   "Face used for displaying underlined bold italic emphasized text.
479 Example: (_/*word*/_)."
480   :group 'gnus-article-emphasis)
481
482 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
483                                       '((t (:strikethru t)))
484                                     '((t (:strike-through t))))
485   "Face used for displaying strike-through text (-word-)."
486   :group 'gnus-article-emphasis)
487
488 (defface gnus-emphasis-highlight-words
489   '((t (:background "black" :foreground "yellow")))
490   "Face used for displaying highlighted words."
491   :group 'gnus-article-emphasis)
492
493 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
494   "Format for display of Date headers in article bodies.
495 See `format-time-string' for the possible values.
496
497 The variable can also be function, which should return a complete Date
498 header.  The function is called with one argument, the time, which can
499 be fed to `format-time-string'."
500   :type '(choice string function)
501   :link '(custom-manual "(gnus)Article Date")
502   :group 'gnus-article-washing)
503
504 (defcustom gnus-save-all-headers t
505   "*If non-nil, don't remove any headers before saving.
506 This will be overridden by the `:headers' property that the symbol of
507 the saver function, which is specified by `gnus-default-article-saver',
508 might have."
509   :group 'gnus-article-saving
510   :type 'boolean)
511
512 (defcustom gnus-prompt-before-saving 'always
513   "*This variable says how much prompting is to be done when saving articles.
514 If it is nil, no prompting will be done, and the articles will be
515 saved to the default files.  If this variable is `always', each and
516 every article that is saved will be preceded by a prompt, even when
517 saving large batches of articles.  If this variable is neither nil not
518 `always', there the user will be prompted once for a file name for
519 each invocation of the saving commands."
520   :group 'gnus-article-saving
521   :type '(choice (item always)
522                  (item :tag "never" nil)
523                  (sexp :tag "once" :format "%t\n" :value t)))
524
525 (defcustom gnus-saved-headers gnus-visible-headers
526   "Headers to keep if `gnus-save-all-headers' is nil.
527 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
528 If that variable is nil, however, all headers that match this regexp
529 will be kept while the rest will be deleted before saving.  This and
530 `gnus-save-all-headers' will be overridden by the `:headers' property
531 that the symbol of the saver function, which is specified by
532 `gnus-default-article-saver', might have."
533   :group 'gnus-article-saving
534   :type 'regexp)
535
536 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
537 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
538   "A function to save articles in your favourite format.
539 The function will be called by way of the `gnus-summary-save-article'
540 command, and friends such as `gnus-summary-save-article-rmail'.
541
542 Gnus provides the following functions:
543
544 * gnus-summary-save-in-rmail (Rmail format)
545 * gnus-summary-save-in-mail (Unix mail format)
546 * gnus-summary-save-in-folder (MH folder)
547 * gnus-summary-save-in-file (article format)
548 * gnus-summary-save-body-in-file (article body)
549 * gnus-summary-save-in-vm (use VM's folder format)
550 * gnus-summary-write-to-file (article format -- overwrite)
551 * gnus-summary-write-body-to-file (article body -- overwrite)
552 * gnus-summary-save-in-pipe (article format)
553
554 The symbol of each function may have the following properties:
555
556 * :decode
557 The value non-nil means save decoded articles.  This is meaningful
558 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
559 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
560 `gnus-summary-save-in-pipe'.
561
562 * :function
563 The value specifies an alternative function which appends, not
564 overwrites, articles to a file.  This implies that when saving many
565 articles at a time, `gnus-prompt-before-saving' is bound to t and all
566 articles are saved in a single file.  This is meaningful only with
567 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
568
569 * :headers
570 The value specifies the symbol of a variable of which the value
571 specifies headers to be saved.  If it is omitted,
572 `gnus-save-all-headers' and `gnus-saved-headers' control what
573 headers should be saved."
574   :group 'gnus-article-saving
575   :type '(radio (function-item gnus-summary-save-in-rmail)
576                 (function-item gnus-summary-save-in-mail)
577                 (function-item gnus-summary-save-in-folder)
578                 (function-item gnus-summary-save-in-file)
579                 (function-item gnus-summary-save-body-in-file)
580                 (function-item gnus-summary-save-in-vm)
581                 (function-item gnus-summary-write-to-file)
582                 (function-item gnus-summary-write-body-to-file)
583                 (function-item gnus-summary-save-in-pipe)
584                 (function)))
585
586 (defcustom gnus-article-save-coding-system
587   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
588       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
589       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
590       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
591   "Coding system used to save decoded articles to a file.
592
593 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
594 which can safely encode any characters in text.  This is used by the
595 commands including:
596
597 * gnus-summary-save-article-file
598 * gnus-summary-save-article-body-file
599 * gnus-summary-write-article-file
600 * gnus-summary-write-article-body-file
601
602 and the functions to which you may set `gnus-default-article-saver':
603
604 * gnus-summary-save-in-file
605 * gnus-summary-save-body-in-file
606 * gnus-summary-write-to-file
607 * gnus-summary-write-body-to-file
608
609 Those commands and functions save just text displayed in the article
610 buffer to a file if the value of this variable is non-nil.  Note that
611 buttonized MIME parts will be lost in a saved file in that case.
612 Otherwise, raw articles will be saved."
613   :group 'gnus-article-saving
614   :type `(choice
615           :format "%{%t%}:\n %[Value Menu%] %v"
616           (const :tag "Save raw articles" nil)
617           ,@(delq nil
618                   (mapcar
619                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
620                    '((const :tag "UTF-8" utf-8)
621                      (const :tag "iso-2022-7bit" iso-2022-7bit)
622                      (const :tag "Emacs internal" emacs-mule)
623                      (const :tag "escape-quoted" escape-quoted))))
624           (symbol :tag "Coding system")))
625
626 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
627   "A function generating a file name to save articles in Rmail format.
628 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
629   :group 'gnus-article-saving
630   :type 'function)
631
632 (defcustom gnus-mail-save-name 'gnus-plain-save-name
633   "A function generating a file name to save articles in Unix mail format.
634 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
635   :group 'gnus-article-saving
636   :type 'function)
637
638 (defcustom gnus-folder-save-name 'gnus-folder-save-name
639   "A function generating a file name to save articles in MH folder.
640 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
641   :group 'gnus-article-saving
642   :type 'function)
643
644 (defcustom gnus-file-save-name 'gnus-numeric-save-name
645   "A function generating a file name to save articles in article format.
646 The function is called with NEWSGROUP, HEADERS, and optional
647 LAST-FILE."
648   :group 'gnus-article-saving
649   :type 'function)
650
651 (defcustom gnus-split-methods
652   '((gnus-article-archive-name)
653     (gnus-article-nndoc-name))
654   "*Variable used to suggest where articles are to be saved.
655 For instance, if you would like to save articles related to Gnus in
656 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
657 you could set this variable to something like:
658
659  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
660    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
661
662 This variable is an alist where the key is the match and the
663 value is a list of possible files to save in if the match is
664 non-nil.
665
666 If the match is a string, it is used as a regexp match on the
667 article.  If the match is a symbol, that symbol will be funcalled
668 from the buffer of the article to be saved with the newsgroup as the
669 parameter.  If it is a list, it will be evaled in the same buffer.
670
671 If this form or function returns a string, this string will be used as a
672 possible file name; and if it returns a non-nil list, that list will be
673 used as possible file names."
674   :group 'gnus-article-saving
675   :type '(repeat (choice (list :value (fun) function)
676                          (cons :value ("" "") regexp (repeat string))
677                          (sexp :value nil))))
678
679 (defcustom gnus-page-delimiter "^\^L"
680   "*Regexp describing what to use as article page delimiters.
681 The default value is \"^\^L\", which is a form linefeed at the
682 beginning of a line."
683   :type 'regexp
684   :group 'gnus-article-various)
685
686 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
687   "*The format specification for the article mode line.
688 See `gnus-summary-mode-line-format' for a closer description.
689
690 The following additional specs are available:
691
692 %w  The article washing status.
693 %m  The number of MIME parts in the article."
694   :type 'string
695   :group 'gnus-article-various)
696
697 (defcustom gnus-article-mode-hook nil
698   "*A hook for Gnus article mode."
699   :type 'hook
700   :group 'gnus-article-various)
701
702 (when (featurep 'xemacs)
703   ;; Extracted from gnus-xmas-define in order to preserve user settings
704   (when (fboundp 'turn-off-scroll-in-place)
705     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
706   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
707   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
708
709 (defcustom gnus-article-menu-hook nil
710   "*Hook run after the creation of the article mode menu."
711   :type 'hook
712   :group 'gnus-article-various)
713
714 (defcustom gnus-article-prepare-hook nil
715   "*A hook called after an article has been prepared in the article buffer."
716   :type 'hook
717   :group 'gnus-article-various)
718
719 (defcustom gnus-copy-article-ignored-headers nil
720   "List of headers to be removed when copying an article.
721 Each element is a regular expression."
722   :version "23.1" ;; No Gnus
723   :type '(repeat regexp)
724   :group 'gnus-article-various)
725
726 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
727                         "Gnus 5.10 (Emacs 22.1)")
728
729 (defface gnus-button
730   '((t (:weight bold)))
731   "Face used for highlighting a button in the article buffer."
732   :group 'gnus-article-buttons)
733
734 (defcustom gnus-article-button-face 'gnus-button
735   "Face used for highlighting buttons in the article buffer.
736
737 An article button is a piece of text that you can activate by pressing
738 `RET' or `mouse-2' above it."
739   :type 'face
740   :group 'gnus-article-buttons)
741
742 (defcustom gnus-article-mouse-face 'highlight
743   "Face used for mouse highlighting in the article buffer.
744
745 Article buttons will be displayed in this face when the cursor is
746 above them."
747   :type 'face
748   :group 'gnus-article-buttons)
749
750 (defcustom gnus-signature-face 'gnus-signature
751   "Face used for highlighting a signature in the article buffer.
752 Obsolete; use the face `gnus-signature' for customizations instead."
753   :type 'face
754   :group 'gnus-article-highlight
755   :group 'gnus-article-signature)
756
757 (defface gnus-signature
758   '((t
759      (:italic t)))
760   "Face used for highlighting a signature in the article buffer."
761   :group 'gnus-article-highlight
762   :group 'gnus-article-signature)
763 ;; backward-compatibility alias
764 (put 'gnus-signature-face 'face-alias 'gnus-signature)
765 (put 'gnus-signature-face 'obsolete-face "22.1")
766
767 (defface gnus-header-from
768   '((((class color)
769       (background dark))
770      (:foreground "PaleGreen1"))
771     (((class color)
772       (background light))
773      (:foreground "red3"))
774     (t
775      (:italic t)))
776   "Face used for displaying from headers."
777   :group 'gnus-article-headers
778   :group 'gnus-article-highlight)
779 ;; backward-compatibility alias
780 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
781 (put 'gnus-header-from-face 'obsolete-face "22.1")
782
783 (defface gnus-header-subject
784   '((((class color)
785       (background dark))
786      (:foreground "SeaGreen1"))
787     (((class color)
788       (background light))
789      (:foreground "red4"))
790     (t
791      (:bold t :italic t)))
792   "Face used for displaying subject headers."
793   :group 'gnus-article-headers
794   :group 'gnus-article-highlight)
795 ;; backward-compatibility alias
796 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
797 (put 'gnus-header-subject-face 'obsolete-face "22.1")
798
799 (defface gnus-header-newsgroups
800   '((((class color)
801       (background dark))
802      (:foreground "yellow" :italic t))
803     (((class color)
804       (background light))
805      (:foreground "MidnightBlue" :italic t))
806     (t
807      (:italic t)))
808   "Face used for displaying newsgroups headers.
809 In the default setup this face is only used for crossposted
810 articles."
811   :group 'gnus-article-headers
812   :group 'gnus-article-highlight)
813 ;; backward-compatibility alias
814 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
815 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
816
817 (defface gnus-header-name
818   '((((class color)
819       (background dark))
820      (:foreground "SpringGreen2"))
821     (((class color)
822       (background light))
823      (:foreground "maroon"))
824     (t
825      (:bold t)))
826   "Face used for displaying header names."
827   :group 'gnus-article-headers
828   :group 'gnus-article-highlight)
829 ;; backward-compatibility alias
830 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
831 (put 'gnus-header-name-face 'obsolete-face "22.1")
832
833 (defface gnus-header-content
834   '((((class color)
835       (background dark))
836      (:foreground "SpringGreen1" :italic t))
837     (((class color)
838       (background light))
839      (:foreground "indianred4" :italic t))
840     (t
841      (:italic t)))  "Face used for displaying header content."
842   :group 'gnus-article-headers
843   :group 'gnus-article-highlight)
844 ;; backward-compatibility alias
845 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
846 (put 'gnus-header-content-face 'obsolete-face "22.1")
847
848 (defcustom gnus-header-face-alist
849   '(("From" nil gnus-header-from)
850     ("Subject" nil gnus-header-subject)
851     ("Newsgroups:.*," nil gnus-header-newsgroups)
852     ("" gnus-header-name gnus-header-content))
853   "*Controls highlighting of article headers.
854
855 An alist of the form (HEADER NAME CONTENT).
856
857 HEADER is a regular expression which should match the name of a
858 header and NAME and CONTENT are either face names or nil.
859
860 The name of each header field will be displayed using the face
861 specified by the first element in the list where HEADER matches
862 the header name and NAME is non-nil.  Similarly, the content will
863 be displayed by the first non-nil matching CONTENT face."
864   :group 'gnus-article-headers
865   :group 'gnus-article-highlight
866   :type '(repeat (list (regexp :tag "Header")
867                        (choice :tag "Name"
868                                (item :tag "skip" nil)
869                                (face :value default))
870                        (choice :tag "Content"
871                                (item :tag "skip" nil)
872                                (face :value default)))))
873
874 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
875                                           '((xface . (:face gnus-x-face)))
876                                         '((pbm . (:face gnus-x-face))
877                                           (png . nil)))
878   "Alist of image types and properties applied to Face and X-Face images.
879 Here are examples:
880
881 ;; Specify the altitude of Face images in the From header.
882 \(setq gnus-face-properties-alist
883       '((pbm . (:face gnus-x-face :ascent 80))
884         (png . (:ascent 80))))
885
886 ;; Show Face images as pressed buttons.
887 \(setq gnus-face-properties-alist
888       '((pbm . (:face gnus-x-face :relief -2))
889         (png . (:relief -2))))
890
891 See the manual for the valid properties for various image types.
892 Currently, `pbm' is used for X-Face images and `png' is used for Face
893 images in Emacs.  Only the `:face' property is effective on the `xface'
894 image type in XEmacs if it is built with the libcompface library."
895   :version "23.1" ;; No Gnus
896   :group 'gnus-article-headers
897   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
898
899 (defcustom gnus-article-decode-hook
900   '(article-decode-charset article-decode-encoded-words
901                            article-decode-group-name article-decode-idna-rhs)
902   "*Hook run to decode charsets in articles."
903   :group 'gnus-article-headers
904   :type 'hook)
905
906 (defcustom gnus-display-mime-function 'gnus-display-mime
907   "Function to display MIME articles."
908   :group 'gnus-article-mime
909   :type 'function)
910
911 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
912   "Function used to decode headers.")
913
914 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
915   "Function used to decode addresses.")
916
917 (defvar gnus-article-dumbquotes-map
918   '((?\200 "EUR")
919     (?\202 ",")
920     (?\203 "f")
921     (?\204 ",,")
922     (?\205 "...")
923     (?\213 "<")
924     (?\214 "OE")
925     (?\221 "`")
926     (?\222 "'")
927     (?\223 "``")
928     (?\224 "\"")
929     (?\225 "*")
930     (?\226 "-")
931     (?\227 "--")
932     (?\230 "~")
933     (?\231 "(TM)")
934     (?\233 ">")
935     (?\234 "oe")
936     (?\264 "'"))
937   "Table for MS-to-Latin1 translation.")
938
939 (defcustom gnus-ignored-mime-types nil
940   "List of MIME types that should be ignored by Gnus."
941   :version "21.1"
942   :group 'gnus-article-mime
943   :type '(repeat regexp))
944
945 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
946   "List of MIME types that should not be given buttons when rendered inline.
947 See also `gnus-buttonized-mime-types' which may override this variable.
948 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
949   :version "21.1"
950   :group 'gnus-article-mime
951   :type '(repeat regexp))
952
953 (defcustom gnus-buttonized-mime-types nil
954   "List of MIME types that should be given buttons when rendered inline.
955 If set, this variable overrides `gnus-unbuttonized-mime-types'.
956 To see e.g. security buttons you could set this to
957 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
958 this list to display radio buttons that allow you to choose one of two
959 media types those mails include.  See also `mm-discouraged-alternatives'.
960 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
961   :version "22.1"
962   :group 'gnus-article-mime
963   :type '(repeat regexp))
964
965 (defcustom gnus-inhibit-mime-unbuttonizing nil
966   "If non-nil, all MIME parts get buttons.
967 When nil (the default value), then some MIME parts do not get buttons,
968 as described by the variables `gnus-buttonized-mime-types' and
969 `gnus-unbuttonized-mime-types'."
970   :version "22.1"
971   :group 'gnus-article-mime
972   :type 'boolean)
973
974 (defcustom gnus-body-boundary-delimiter "_"
975   "String used to delimit header and body.
976 This variable is used by `gnus-article-treat-body-boundary' which can
977 be controlled by `gnus-treat-body-boundary'."
978   :version "22.1"
979   :group 'gnus-article-various
980   :type '(choice (item :tag "None" :value nil)
981                  string))
982
983 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
984                                   "/usr/share/picons")
985   "Defines the location of the faces database.
986 For information on obtaining this database of pretty pictures, please
987 see http://www.cs.indiana.edu/picons/ftp/index.html"
988   :version "22.1"
989   :type '(repeat directory)
990   :link '(url-link :tag "download"
991                    "http://www.cs.indiana.edu/picons/ftp/index.html")
992   :link '(custom-manual "(gnus)Picons")
993   :group 'gnus-picon)
994
995 (defun gnus-picons-installed-p ()
996   "Say whether picons are installed on your machine."
997   (let ((installed nil))
998     (dolist (database gnus-picon-databases)
999       (when (file-exists-p database)
1000         (setq installed t)))
1001     installed))
1002
1003 (defcustom gnus-article-mime-part-function nil
1004   "Function called with a MIME handle as the argument.
1005 This is meant for people who want to do something automatic based
1006 on parts -- for instance, adding Vcard info to a database."
1007   :group 'gnus-article-mime
1008   :type '(choice (const nil)
1009                  function))
1010
1011 (defcustom gnus-mime-multipart-functions nil
1012   "An alist of MIME types to functions to display them."
1013   :version "21.1"
1014   :group 'gnus-article-mime
1015   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1016
1017 (defcustom gnus-article-date-headers '(combined-lapsed)
1018   "A list of Date header formats to display.
1019 Valid formats are `ut' (universal time), `local' (local time
1020 zone), `english' (readable English), `lapsed' (elapsed time),
1021 `combined-lapsed' (both the original date and the elapsed time),
1022 `original' (the original date header), `iso8601' (ISO8601
1023 format), and `user-defined' (a user-defined format defined by the
1024 `gnus-article-time-format' variable).
1025
1026 You have as many date headers as you want in the article buffer.
1027 Some of these headers are updated automatically.  See
1028 `gnus-article-update-date-headers' for details."
1029   :version "24.1"
1030   :group 'gnus-article-headers
1031   :type '(repeat
1032           (item :tag "Universal time (UT)" :value 'ut)
1033           (item :tag "Local time zone" :value 'local)
1034           (item :tag "Readable English" :value 'english)
1035           (item :tag "Elapsed time" :value 'lapsed)
1036           (item :tag "Original and elapsed time" :value 'combined-lapsed)
1037           (item :tag "Original date header" :value 'original)
1038           (item :tag "ISO8601 format" :value 'iso8601)
1039           (item :tag "User-defined" :value 'user-defined)))
1040
1041 (defcustom gnus-article-update-date-headers 1
1042   "How often to update the date header.
1043 If nil, don't update it at all."
1044   :version "24.1"
1045   :group 'gnus-article-headers
1046   :type '(choice
1047           (item :tag "Don't update" :value nil)
1048           integer))
1049
1050 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1051   "Function called with a MIME handle as the argument.
1052 This is meant for people who want to view first matched part.
1053 For `undisplayed-alternative' (default), the first undisplayed
1054 part or alternative part is used.  For `undisplayed', the first
1055 undisplayed part is used.  For a function, the first part which
1056 the function return t is used.  For nil, the first part is
1057 used."
1058   :version "21.1"
1059   :group 'gnus-article-mime
1060   :type '(choice
1061           (item :tag "first" :value nil)
1062           (item :tag "undisplayed" :value undisplayed)
1063           (item :tag "undisplayed or alternative"
1064                 :value undisplayed-alternative)
1065           (function)))
1066
1067 (defcustom gnus-mime-action-alist
1068   '(("save to file" . gnus-mime-save-part)
1069     ("save and strip" . gnus-mime-save-part-and-strip)
1070     ("replace with file" . gnus-mime-replace-part)
1071     ("delete part" . gnus-mime-delete-part)
1072     ("display as text" . gnus-mime-inline-part)
1073     ("view the part" . gnus-mime-view-part)
1074     ("pipe to command" . gnus-mime-pipe-part)
1075     ("toggle display" . gnus-article-press-button)
1076     ("toggle display" . gnus-article-view-part-as-charset)
1077     ("view as type" . gnus-mime-view-part-as-type)
1078     ("view internally" . gnus-mime-view-part-internally)
1079     ("view externally" . gnus-mime-view-part-externally))
1080   "An alist of actions that run on the MIME attachment."
1081   :group 'gnus-article-mime
1082   :type '(repeat (cons (string :tag "name")
1083                        (function))))
1084
1085 (defcustom gnus-auto-select-part 1
1086   "Advance to next MIME part when deleting or stripping parts.
1087
1088 When 0, point will be placed on the same part as before.  When
1089 positive (negative), move point forward (backwards) this many
1090 parts.  When nil, redisplay article."
1091   :version "23.1" ;; No Gnus
1092   :group 'gnus-article-mime
1093   :type '(choice (const nil :tag "Redisplay article.")
1094                  (const 1 :tag "Next part.")
1095                  (const 0 :tag "Current part.")
1096                  integer))
1097
1098 ;;;
1099 ;;; The treatment variables
1100 ;;;
1101
1102 (defvar gnus-part-display-hook nil
1103   "Hook called on parts that are to receive treatment.")
1104
1105 (defvar gnus-article-treat-custom
1106   '(choice (const :tag "Off" nil)
1107            (const :tag "On" t)
1108            (const :tag "Header" head)
1109            (const :tag "First" first)
1110            (const :tag "Last" last)
1111            (integer :tag "Less")
1112            (repeat :tag "Groups" regexp)
1113            (sexp :tag "Predicate")))
1114
1115 (defvar gnus-article-treat-head-custom
1116   '(choice (const :tag "Off" nil)
1117            (const :tag "Header" head)))
1118
1119 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1120                                    "text/x-patch")
1121   "Parts to treat.")
1122
1123 (defvar gnus-inhibit-treatment nil
1124   "Whether to inhibit treatment.")
1125
1126 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1127   "Highlight the signature.
1128 Valid values are nil, t, `head', `first', `last', an integer or a
1129 predicate.  See Info node `(gnus)Customizing Articles'."
1130   :group 'gnus-article-treat
1131   :link '(custom-manual "(gnus)Customizing Articles")
1132   :type gnus-article-treat-custom)
1133 (put 'gnus-treat-highlight-signature 'highlight t)
1134
1135 (defcustom gnus-treat-buttonize 100000
1136   "Add buttons.
1137 Valid values are nil, t, `head', `first', `last', an integer or a
1138 predicate.  See Info node `(gnus)Customizing Articles'."
1139   :group 'gnus-article-treat
1140   :link '(custom-manual "(gnus)Customizing Articles")
1141   :type gnus-article-treat-custom)
1142 (put 'gnus-treat-buttonize 'highlight t)
1143
1144 (defcustom gnus-treat-buttonize-head 'head
1145   "Add buttons to the head.
1146 Valid values are nil, t, `head', `first', `last', an integer or a
1147 predicate.  See Info node `(gnus)Customizing Articles'."
1148   :group 'gnus-article-treat
1149   :link '(custom-manual "(gnus)Customizing Articles")
1150   :type gnus-article-treat-head-custom)
1151 (put 'gnus-treat-buttonize-head 'highlight t)
1152
1153 (defcustom gnus-treat-date 'head
1154   "Display dates according to the `gnus-article-date-headers' variable.
1155 Valid values are nil, t, `head', `first', `last', an integer or a
1156 predicate.  See Info node `(gnus)Customizing Articles'."
1157   :version "24.1"
1158   :group 'gnus-article-treat
1159   :link '(custom-manual "(gnus)Customizing Articles")
1160   :type gnus-article-treat-head-custom)
1161
1162 (defcustom gnus-treat-emphasize 50000
1163   "Emphasize text.
1164 Valid values are nil, t, `head', `first', `last', an integer or a
1165 predicate.  See Info node `(gnus)Customizing Articles'."
1166   :group 'gnus-article-treat
1167   :link '(custom-manual "(gnus)Customizing Articles")
1168   :type gnus-article-treat-custom)
1169 (put 'gnus-treat-emphasize 'highlight t)
1170
1171 (defcustom gnus-treat-strip-cr nil
1172   "Remove carriage returns.
1173 Valid values are nil, t, `head', `first', `last', an integer or a
1174 predicate.  See Info node `(gnus)Customizing Articles'."
1175   :version "22.1"
1176   :group 'gnus-article-treat
1177   :link '(custom-manual "(gnus)Customizing Articles")
1178   :type gnus-article-treat-custom)
1179
1180 (defcustom gnus-treat-unsplit-urls nil
1181   "Remove newlines from within URLs.
1182 Valid values are nil, t, `head', `first', `last', an integer or a
1183 predicate.  See Info node `(gnus)Customizing Articles'."
1184   :version "22.1"
1185   :group 'gnus-article-treat
1186   :link '(custom-manual "(gnus)Customizing Articles")
1187   :type gnus-article-treat-custom)
1188
1189 (defcustom gnus-treat-leading-whitespace nil
1190   "Remove leading whitespace in headers.
1191 Valid values are nil, t, `head', `first', `last', an integer or a
1192 predicate.  See Info node `(gnus)Customizing Articles'."
1193   :version "22.1"
1194   :group 'gnus-article-treat
1195   :link '(custom-manual "(gnus)Customizing Articles")
1196   :type gnus-article-treat-custom)
1197
1198 (defcustom gnus-treat-hide-headers 'head
1199   "Hide headers.
1200 Valid values are nil, t, `head', `first', `last', an integer or a
1201 predicate.  See Info node `(gnus)Customizing Articles'."
1202   :group 'gnus-article-treat
1203   :link '(custom-manual "(gnus)Customizing Articles")
1204   :type gnus-article-treat-head-custom)
1205
1206 (defcustom gnus-treat-hide-boring-headers nil
1207   "Hide boring headers.
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-head-custom)
1213
1214 (defcustom gnus-treat-hide-signature nil
1215   "Hide the signature.
1216 Valid values are nil, t, `head', `first', `last', an integer or a
1217 predicate.  See Info node `(gnus)Customizing Articles'."
1218   :group 'gnus-article-treat
1219   :link '(custom-manual "(gnus)Customizing Articles")
1220   :type gnus-article-treat-custom)
1221
1222 (defcustom gnus-treat-fill-article nil
1223   "Fill the article.
1224 Valid values are nil, t, `head', `first', `last', an integer or a
1225 predicate.  See Info node `(gnus)Customizing Articles'."
1226   :group 'gnus-article-treat
1227   :link '(custom-manual "(gnus)Customizing Articles")
1228   :type gnus-article-treat-custom)
1229
1230 (defcustom gnus-treat-hide-citation nil
1231   "Hide cited text.
1232 Valid values are nil, t, `head', `first', `last', an integer or a
1233 predicate.  See Info node `(gnus)Customizing Articles'."
1234   :group 'gnus-article-treat
1235   :link '(custom-manual "(gnus)Customizing Articles")
1236   :type gnus-article-treat-custom)
1237
1238 (defcustom gnus-treat-hide-citation-maybe nil
1239   "Hide cited text.
1240 Valid values are nil, t, `head', `first', `last', an integer or a
1241 predicate.  See Info node `(gnus)Customizing Articles'."
1242   :group 'gnus-article-treat
1243   :link '(custom-manual "(gnus)Customizing Articles")
1244   :type gnus-article-treat-custom)
1245
1246 (defcustom gnus-treat-strip-list-identifiers 'head
1247   "Strip list identifiers from `gnus-list-identifiers`.
1248 Valid values are nil, t, `head', `first', `last', an integer or a
1249 predicate.  See Info node `(gnus)Customizing Articles'."
1250   :version "21.1"
1251   :group 'gnus-article-treat
1252   :link '(custom-manual "(gnus)Customizing Articles")
1253   :type gnus-article-treat-custom)
1254
1255 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1256                         "Gnus 5.10 (Emacs 22.1)")
1257
1258 (defcustom gnus-treat-strip-pem nil
1259   "Strip PEM signatures.
1260 Valid values are nil, t, `head', `first', `last', an integer or a
1261 predicate.  See Info node `(gnus)Customizing Articles'."
1262   :group 'gnus-article-treat
1263   :link '(custom-manual "(gnus)Customizing Articles")
1264   :type gnus-article-treat-custom)
1265
1266 (defcustom gnus-treat-strip-banner t
1267   "Strip banners from articles.
1268 The banner to be stripped is specified in the `banner' group parameter.
1269 Valid values are nil, t, `head', `first', `last', an integer or a
1270 predicate.  See Info node `(gnus)Customizing Articles'."
1271   :group 'gnus-article-treat
1272   :link '(custom-manual "(gnus)Customizing Articles")
1273   :type gnus-article-treat-custom)
1274
1275 (defcustom gnus-treat-highlight-headers 'head
1276   "Highlight the headers.
1277 Valid values are nil, t, `head', `first', `last', an integer or a
1278 predicate.  See Info node `(gnus)Customizing Articles'."
1279   :group 'gnus-article-treat
1280   :link '(custom-manual "(gnus)Customizing Articles")
1281   :type gnus-article-treat-head-custom)
1282 (put 'gnus-treat-highlight-headers 'highlight t)
1283
1284 (defcustom gnus-treat-highlight-citation t
1285   "Highlight cited text.
1286 Valid values are nil, t, `head', `first', `last', an integer or a
1287 predicate.  See Info node `(gnus)Customizing Articles'."
1288   :group 'gnus-article-treat
1289   :link '(custom-manual "(gnus)Customizing Articles")
1290   :type gnus-article-treat-custom)
1291 (put 'gnus-treat-highlight-citation 'highlight t)
1292
1293 (defcustom gnus-treat-strip-headers-in-body t
1294   "Strip the X-No-Archive header line from the beginning of the body.
1295 Valid values are nil, t, `head', `first', `last', an integer or a
1296 predicate.  See Info node `(gnus)Customizing Articles'."
1297   :version "21.1"
1298   :group 'gnus-article-treat
1299   :link '(custom-manual "(gnus)Customizing Articles")
1300   :type gnus-article-treat-custom)
1301
1302 (defcustom gnus-treat-strip-trailing-blank-lines nil
1303   "Strip trailing blank lines.
1304 Valid values are nil, t, `head', `first', `last', an integer or a
1305 predicate.  See Info node `(gnus)Customizing Articles'.
1306
1307 When set to t, it also strips trailing blanks in all MIME parts.
1308 Consider to use `last' instead."
1309   :group 'gnus-article-treat
1310   :link '(custom-manual "(gnus)Customizing Articles")
1311   :type gnus-article-treat-custom)
1312
1313 (defcustom gnus-treat-strip-leading-blank-lines nil
1314   "Strip leading blank lines.
1315 Valid values are nil, t, `head', `first', `last', an integer or a
1316 predicate.  See Info node `(gnus)Customizing Articles'.
1317
1318 When set to t, it also strips trailing blanks in all MIME parts."
1319   :group 'gnus-article-treat
1320   :link '(custom-manual "(gnus)Customizing Articles")
1321   :type gnus-article-treat-custom)
1322
1323 (defcustom gnus-treat-strip-multiple-blank-lines nil
1324   "Strip multiple blank lines.
1325 Valid values are nil, t, `head', `first', `last', an integer or a
1326 predicate.  See Info node `(gnus)Customizing Articles'."
1327   :group 'gnus-article-treat
1328   :link '(custom-manual "(gnus)Customizing Articles")
1329   :type gnus-article-treat-custom)
1330
1331 (defcustom gnus-treat-unfold-headers 'head
1332   "Unfold folded header lines.
1333 Valid values are nil, t, `head', `first', `last', an integer or a
1334 predicate.  See Info node `(gnus)Customizing Articles'."
1335   :version "22.1"
1336   :group 'gnus-article-treat
1337   :link '(custom-manual "(gnus)Customizing Articles")
1338   :type gnus-article-treat-custom)
1339
1340 (defcustom gnus-article-unfold-long-headers nil
1341   "If non-nil, allow unfolding headers even if the header is long.
1342 If it is a regexp, only long headers matching this regexp are unfolded.
1343 If it is t, all long headers are unfolded.
1344
1345 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1346   :version "23.1" ;; No Gnus
1347   :group 'gnus-article-treat
1348   :type '(choice (const nil)
1349                  (const :tag "all" t)
1350                  (regexp)))
1351
1352 (defcustom gnus-treat-fold-headers nil
1353   "Fold headers.
1354 Valid values are nil, t, `head', `first', `last', an integer or a
1355 predicate.  See Info node `(gnus)Customizing Articles'."
1356   :version "22.1"
1357   :group 'gnus-article-treat
1358   :link '(custom-manual "(gnus)Customizing Articles")
1359   :type gnus-article-treat-custom)
1360
1361 (defcustom gnus-treat-fold-newsgroups 'head
1362   "Fold the Newsgroups and Followup-To headers.
1363 Valid values are nil, t, `head', `first', `last', an integer or a
1364 predicate.  See Info node `(gnus)Customizing Articles'."
1365   :version "22.1"
1366   :group 'gnus-article-treat
1367   :link '(custom-manual "(gnus)Customizing Articles")
1368   :type gnus-article-treat-custom)
1369
1370 (defcustom gnus-treat-overstrike t
1371   "Treat overstrike highlighting.
1372 Valid values are nil, t, `head', `first', `last', an integer or a
1373 predicate.  See Info node `(gnus)Customizing Articles'."
1374   :group 'gnus-article-treat
1375   :link '(custom-manual "(gnus)Customizing Articles")
1376   :type gnus-article-treat-custom)
1377 (put 'gnus-treat-overstrike 'highlight t)
1378
1379 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1380   "Treat ANSI SGR control sequences.
1381 Valid values are nil, t, `head', `first', `last', an integer or a
1382 predicate.  See Info node `(gnus)Customizing Articles'."
1383   :group 'gnus-article-treat
1384   :link '(custom-manual "(gnus)Customizing Articles")
1385   :type gnus-article-treat-custom)
1386
1387 (make-obsolete-variable 'gnus-treat-display-xface
1388                         'gnus-treat-display-x-face "Emacs 22.1")
1389
1390 (defcustom gnus-treat-display-x-face
1391   (and (not noninteractive)
1392        (gnus-image-type-available-p 'xbm)
1393        (if (featurep 'xemacs)
1394            (featurep 'xface)
1395          (condition-case nil
1396              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1397                   (executable-find "icontopbm"))
1398            ;; shell-command-to-string may signal an error, e.g. if
1399            ;; shell-file-name is not found.
1400            (error nil)))
1401        'head)
1402   "Display X-Face headers.
1403 Valid values are nil and `head'.
1404 See Info node `(gnus)Customizing Articles' and Info node
1405 `(gnus)X-Face' for details."
1406   :group 'gnus-article-treat
1407   :version "21.1"
1408   :link '(custom-manual "(gnus)Customizing Articles")
1409   :link '(custom-manual "(gnus)X-Face")
1410   :type gnus-article-treat-head-custom
1411   :set (lambda (symbol value)
1412          (set-default
1413           symbol
1414           (cond ((or (boundp symbol) (get symbol 'saved-value))
1415                  value)
1416                 ((boundp 'gnus-treat-display-xface)
1417                  (message "\
1418 ** gnus-treat-display-xface is an obsolete variable;\
1419  use gnus-treat-display-x-face instead")
1420                  (default-value 'gnus-treat-display-xface))
1421                 ((get 'gnus-treat-display-xface 'saved-value)
1422                  (message "\
1423 ** gnus-treat-display-xface is an obsolete variable;\
1424  use gnus-treat-display-x-face instead")
1425                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1426                 (t
1427                  value)))))
1428 (put 'gnus-treat-display-x-face 'highlight t)
1429
1430 (defcustom gnus-treat-display-face
1431   (and (not noninteractive)
1432        (gnus-image-type-available-p 'png)
1433        'head)
1434   "Display Face headers.
1435 Valid values are nil, t, `head', `first', `last', an integer or a
1436 predicate.  See Info node `(gnus)Customizing Articles' and Info
1437 node `(gnus)Face' for details."
1438   :group 'gnus-article-treat
1439   :version "22.1"
1440   :link '(custom-manual "(gnus)Customizing Articles")
1441   :link '(custom-manual "(gnus)X-Face")
1442   :type gnus-article-treat-head-custom)
1443 (put 'gnus-treat-display-face 'highlight t)
1444
1445 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1446   "Display smileys.
1447 Valid values are nil, t, `head', `first', `last', an integer or a
1448 predicate.  See Info node `(gnus)Customizing Articles' and Info
1449 node `(gnus)Smileys' for details."
1450   :group 'gnus-article-treat
1451   :version "21.1"
1452   :link '(custom-manual "(gnus)Customizing Articles")
1453   :link '(custom-manual "(gnus)Smileys")
1454   :type gnus-article-treat-custom)
1455 (put 'gnus-treat-display-smileys 'highlight t)
1456
1457 (defcustom gnus-treat-from-picon
1458   (if (and (gnus-image-type-available-p 'xpm)
1459            (gnus-picons-installed-p))
1460       'head nil)
1461   "Display picons in the From header.
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)Picons' for details."
1465   :version "22.1"
1466   :group 'gnus-article-treat
1467   :group 'gnus-picon
1468   :link '(custom-manual "(gnus)Customizing Articles")
1469   :link '(custom-manual "(gnus)Picons")
1470   :type gnus-article-treat-head-custom)
1471 (put 'gnus-treat-from-picon 'highlight t)
1472
1473 (defcustom gnus-treat-mail-picon
1474   (if (and (gnus-image-type-available-p 'xpm)
1475            (gnus-picons-installed-p))
1476       'head nil)
1477   "Display picons in To and Cc headers.
1478 Valid values are nil, t, `head', `first', `last', an integer or a
1479 predicate.  See Info node `(gnus)Customizing Articles' and Info
1480 node `(gnus)Picons' for details."
1481   :version "22.1"
1482   :group 'gnus-article-treat
1483   :group 'gnus-picon
1484   :link '(custom-manual "(gnus)Customizing Articles")
1485   :link '(custom-manual "(gnus)Picons")
1486   :type gnus-article-treat-head-custom)
1487 (put 'gnus-treat-mail-picon 'highlight t)
1488
1489 (defcustom gnus-treat-newsgroups-picon
1490   (if (and (gnus-image-type-available-p 'xpm)
1491            (gnus-picons-installed-p))
1492       'head nil)
1493   "Display picons in the Newsgroups and Followup-To headers.
1494 Valid values are nil, t, `head', `first', `last', an integer or a
1495 predicate.  See Info node `(gnus)Customizing Articles' and Info
1496 node `(gnus)Picons' for details."
1497   :version "22.1"
1498   :group 'gnus-article-treat
1499   :group 'gnus-picon
1500   :link '(custom-manual "(gnus)Customizing Articles")
1501   :link '(custom-manual "(gnus)Picons")
1502   :type gnus-article-treat-head-custom)
1503 (put 'gnus-treat-newsgroups-picon 'highlight t)
1504
1505 (defcustom gnus-treat-from-gravatar nil
1506   "Display gravatars in the From header.
1507 Valid values are nil, t, `head', `first', `last', an integer or a
1508 predicate.  See Info node `(gnus)Customizing Articles' and Info
1509 node `(gnus)Gravatars' for details."
1510   :version "24.1"
1511   :group 'gnus-article-treat
1512   :group 'gnus-gravatar
1513   :link '(custom-manual "(gnus)Customizing Articles")
1514   :link '(custom-manual "(gnus)Gravatars")
1515   :type gnus-article-treat-head-custom)
1516 (put 'gnus-treat-from-gravatar 'highlight t)
1517
1518 (defcustom gnus-treat-mail-gravatar nil
1519   "Display gravatars in To and Cc headers.
1520 Valid values are nil, t, `head', `first', `last', an integer or a
1521 predicate.  See Info node `(gnus)Customizing Articles' and Info
1522 node `(gnus)Gravatars' for details."
1523   :version "24.1"
1524   :group 'gnus-article-treat
1525   :group 'gnus-gravatar
1526   :link '(custom-manual "(gnus)Customizing Articles")
1527   :link '(custom-manual "(gnus)Gravatars")
1528   :type gnus-article-treat-head-custom)
1529 (put 'gnus-treat-mail-gravatar 'highlight t)
1530
1531 (defcustom gnus-treat-body-boundary
1532   (if (or gnus-treat-newsgroups-picon
1533           gnus-treat-mail-picon
1534           gnus-treat-from-picon
1535           gnus-treat-from-gravatar
1536           gnus-treat-mail-gravatar)
1537       ;; If there's much decoration, the user might prefer a boundery.
1538       'head
1539     nil)
1540   "Draw a boundary at the end of the headers.
1541 Valid values are nil and `head'.
1542 See Info node `(gnus)Customizing Articles' for details."
1543   :version "22.1"
1544   :group 'gnus-article-treat
1545   :link '(custom-manual "(gnus)Customizing Articles")
1546   :type gnus-article-treat-head-custom)
1547
1548 (defcustom gnus-treat-capitalize-sentences nil
1549   "Capitalize sentence-starting words.
1550 Valid values are nil, t, `head', `first', `last', an integer or a
1551 predicate.  See Info node `(gnus)Customizing Articles'."
1552   :version "21.1"
1553   :group 'gnus-article-treat
1554   :link '(custom-manual "(gnus)Customizing Articles")
1555   :type gnus-article-treat-custom)
1556
1557 (defcustom gnus-treat-wash-html nil
1558   "Format as HTML.
1559 Valid values are nil, t, `head', `first', `last', an integer or a
1560 predicate.  See Info node `(gnus)Customizing Articles'."
1561   :version "22.1"
1562   :group 'gnus-article-treat
1563   :link '(custom-manual "(gnus)Customizing Articles")
1564   :type gnus-article-treat-custom)
1565
1566 (defcustom gnus-treat-fill-long-lines '(typep "text/plain")
1567   "Fill long lines.
1568 Valid values are nil, t, `head', `first', `last', an integer or a
1569 predicate.  See Info node `(gnus)Customizing Articles'."
1570   :version "24.1"
1571   :group 'gnus-article-treat
1572   :link '(custom-manual "(gnus)Customizing Articles")
1573   :type gnus-article-treat-custom)
1574
1575 (defcustom gnus-treat-x-pgp-sig nil
1576   "Verify X-PGP-Sig.
1577 To automatically treat X-PGP-Sig, set it to head.
1578 Valid values are nil, t, `head', `first', `last', an integer or a
1579 predicate.  See Info node `(gnus)Customizing Articles'."
1580   :version "22.1"
1581   :group 'gnus-article-treat
1582   :group 'mime-security
1583   :link '(custom-manual "(gnus)Customizing Articles")
1584   :type gnus-article-treat-custom)
1585
1586 (defvar gnus-article-encrypt-protocol-alist
1587   '(("PGP" . mml2015-self-encrypt)))
1588
1589 ;; Set to nil if more than one protocol added to
1590 ;; gnus-article-encrypt-protocol-alist.
1591 (defcustom gnus-article-encrypt-protocol "PGP"
1592   "The protocol used for encrypt articles.
1593 It is a string, such as \"PGP\". If nil, ask user."
1594   :version "22.1"
1595   :type 'string
1596   :group 'mime-security)
1597
1598 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1599                               (mm-coding-system-p 'utf-8)
1600                               (executable-find idna-program))
1601   "Whether IDNA decoding of headers is used when viewing messages.
1602 This requires GNU Libidn, and by default only enabled if it is found."
1603   :version "22.1"
1604   :group 'gnus-article-headers
1605   :type 'boolean)
1606
1607 (defcustom gnus-article-over-scroll nil
1608   "If non-nil, allow scrolling the article buffer even when there no more text."
1609   :version "22.1"
1610   :group 'gnus-article
1611   :type 'boolean)
1612
1613 (defcustom gnus-inhibit-images nil
1614   "Non-nil means inhibit displaying of images inline in the article body."
1615   :version "24.1"
1616   :group 'gnus-article
1617   :type 'boolean)
1618
1619 (defcustom gnus-blocked-images 'gnus-block-private-groups
1620   "Images that have URLs matching this regexp will be blocked.
1621 This can also be a function to be evaluated.  If so, it will be
1622 called with the group name as the parameter, and should return a
1623 regexp."
1624   :version "24.1"
1625   :group 'gnus-art
1626   :type 'regexp)
1627
1628 ;;; Internal variables
1629
1630 (defvar gnus-english-month-names
1631   '("January" "February" "March" "April" "May" "June" "July" "August"
1632     "September" "October" "November" "December"))
1633
1634 (defvar article-goto-body-goes-to-point-min-p nil)
1635 (defvar gnus-article-wash-types nil)
1636 (defvar gnus-article-emphasis-alist nil)
1637 (defvar gnus-article-image-alist nil)
1638
1639 (defvar gnus-article-mime-handle-alist-1 nil)
1640 (defvar gnus-treatment-function-alist
1641   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1642     (gnus-treat-strip-banner gnus-article-strip-banner)
1643     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1644     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1645     (gnus-treat-buttonize gnus-article-add-buttons)
1646     (gnus-treat-fill-article gnus-article-fill-cited-article)
1647     (gnus-treat-fill-long-lines gnus-article-fill-cited-long-lines)
1648     (gnus-treat-strip-cr gnus-article-remove-cr)
1649     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1650     (gnus-treat-display-x-face gnus-article-display-x-face)
1651     (gnus-treat-display-face gnus-article-display-face)
1652     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1653     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1654     (gnus-treat-hide-signature gnus-article-hide-signature)
1655     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1656     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1657     (gnus-treat-from-picon gnus-treat-from-picon)
1658     (gnus-treat-mail-picon gnus-treat-mail-picon)
1659     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1660     (gnus-treat-strip-pem gnus-article-hide-pem)
1661     (gnus-treat-date gnus-article-treat-date)
1662     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1663     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1664     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1665     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1666     (gnus-treat-strip-trailing-blank-lines
1667      gnus-article-remove-trailing-blank-lines)
1668     (gnus-treat-strip-leading-blank-lines
1669      gnus-article-strip-leading-blank-lines)
1670     (gnus-treat-strip-multiple-blank-lines
1671      gnus-article-strip-multiple-blank-lines)
1672     (gnus-treat-overstrike gnus-article-treat-overstrike)
1673     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1674     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1675     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1676     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1677     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1678     (gnus-treat-display-smileys gnus-treat-smiley)
1679     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1680     (gnus-treat-wash-html gnus-article-wash-html)
1681     (gnus-treat-emphasize gnus-article-emphasize)
1682     (gnus-treat-hide-citation gnus-article-hide-citation)
1683     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1684     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1685     (gnus-treat-body-boundary gnus-article-treat-body-boundary)))
1686
1687 (defvar gnus-article-mime-handle-alist nil)
1688 (defvar article-lapsed-timer nil)
1689 (defvar gnus-article-current-summary nil)
1690
1691 (defvar gnus-article-mode-syntax-table
1692   (let ((table (copy-syntax-table text-mode-syntax-table)))
1693     ;; This causes the citation match run O(2^n).
1694     ;; (modify-syntax-entry ?- "w" table)
1695     (modify-syntax-entry ?> ")<" table)
1696     (modify-syntax-entry ?< "(>" table)
1697     ;; make M-. in article buffers work for `foo' strings
1698     (modify-syntax-entry ?' " " table)
1699     (modify-syntax-entry ?` " " table)
1700     table)
1701   "Syntax table used in article mode buffers.
1702 Initialized from `text-mode-syntax-table.")
1703
1704 (defvar gnus-save-article-buffer nil)
1705
1706 (defvar gnus-number-of-articles-to-be-saved nil)
1707
1708 (defvar gnus-inhibit-hiding nil)
1709
1710 (defvar gnus-article-edit-mode nil)
1711
1712 ;;; Macros for dealing with the article buffer.
1713
1714 (defmacro gnus-with-article-headers (&rest forms)
1715   `(with-current-buffer gnus-article-buffer
1716      (save-restriction
1717        (let ((inhibit-read-only t)
1718              (inhibit-point-motion-hooks t)
1719              (case-fold-search t))
1720          (article-narrow-to-head)
1721          ,@forms))))
1722
1723 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1724 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1725
1726 (defmacro gnus-with-article-buffer (&rest forms)
1727   `(with-current-buffer gnus-article-buffer
1728      (let ((inhibit-read-only t))
1729        ,@forms)))
1730
1731 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1732 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1733
1734 (defun gnus-article-goto-header (header)
1735   "Go to HEADER, which is a regular expression."
1736   (re-search-forward (concat "^\\(" header "\\):") nil t))
1737
1738 (defsubst gnus-article-hide-text (b e props)
1739   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1740   (gnus-add-text-properties-when 'article-type nil b e props)
1741   (when (memq 'intangible props)
1742     (put-text-property
1743      (max (1- b) (point-min))
1744      b 'intangible (cddr (memq 'intangible props)))))
1745
1746 (defsubst gnus-article-unhide-text (b e)
1747   "Remove hidden text properties from region between B and E."
1748   (remove-text-properties b e gnus-hidden-properties)
1749   (when (memq 'intangible gnus-hidden-properties)
1750     (put-text-property (max (1- b) (point-min))
1751                        b 'intangible nil)))
1752
1753 (defun gnus-article-hide-text-type (b e type)
1754   "Hide text of TYPE between B and E."
1755   (gnus-add-wash-type type)
1756   (gnus-article-hide-text
1757    b e (cons 'article-type (cons type gnus-hidden-properties))))
1758
1759 (defun gnus-article-unhide-text-type (b e type)
1760   "Unhide text of TYPE between B and E."
1761   (gnus-delete-wash-type type)
1762   (remove-text-properties
1763    b e (cons 'article-type (cons type gnus-hidden-properties)))
1764   (when (memq 'intangible gnus-hidden-properties)
1765     (put-text-property (max (1- b) (point-min))
1766                        b 'intangible nil)))
1767
1768 (defun gnus-article-hide-text-of-type (type)
1769   "Hide text of TYPE in the current buffer."
1770   (save-excursion
1771     (let ((b (point-min))
1772           (e (point-max)))
1773       (while (setq b (text-property-any b e 'article-type type))
1774         (add-text-properties b (incf b) gnus-hidden-properties)))))
1775
1776 (defun gnus-article-delete-text-of-type (type)
1777   "Delete text of TYPE in the current buffer."
1778   (save-excursion
1779     (let ((b (point-min)))
1780       (if (eq type 'multipart)
1781           ;; Remove MIME buttons associated with multipart/alternative parts.
1782           (progn
1783             (goto-char b)
1784             (while (if (get-text-property (point) 'gnus-part)
1785                        (setq b (point))
1786                      (when (setq b (next-single-property-change (point)
1787                                                                 'gnus-part))
1788                        (goto-char b)
1789                        t))
1790               (end-of-line)
1791               (skip-chars-forward "\n")
1792               (when (eq (get-text-property b 'article-type) 'multipart)
1793                 (delete-region b (point)))))
1794         (while (setq b (text-property-any b (point-max) 'article-type type))
1795           (delete-region
1796            b (or (text-property-not-all b (point-max) 'article-type type)
1797                  (point-max))))))))
1798
1799 (defun gnus-article-delete-invisible-text ()
1800   "Delete all invisible text in the current buffer."
1801   (save-excursion
1802     (let ((b (point-min)))
1803       (while (setq b (text-property-any b (point-max) 'invisible t))
1804         (delete-region
1805          b (or (text-property-not-all b (point-max) 'invisible t)
1806                (point-max)))))))
1807
1808 (defun gnus-article-text-type-exists-p (type)
1809   "Say whether any text of type TYPE exists in the buffer."
1810   (text-property-any (point-min) (point-max) 'article-type type))
1811
1812 (defsubst gnus-article-header-rank ()
1813   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1814   (let ((list gnus-sorted-header-list)
1815         (i 1))
1816     (while list
1817       (if (looking-at (car list))
1818           (setq list nil)
1819         (setq list (cdr list))
1820         (incf i)))
1821       i))
1822
1823 (defun article-hide-headers (&optional arg delete)
1824   "Hide unwanted headers and possibly sort them as well."
1825   (interactive)
1826   ;; This function might be inhibited.
1827   (unless gnus-inhibit-hiding
1828     (let ((inhibit-read-only t)
1829           (case-fold-search t)
1830           (max (1+ (length gnus-sorted-header-list)))
1831           (inhibit-point-motion-hooks t)
1832           (cur (current-buffer))
1833           ignored visible beg)
1834       (save-excursion
1835         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1836         ;; group parameters, so we should go to the summary buffer.
1837         (when (prog1
1838                   (condition-case nil
1839                       (progn (set-buffer gnus-summary-buffer) t)
1840                     (error nil))
1841                 (setq ignored (when (not gnus-visible-headers)
1842                                 (cond ((stringp gnus-ignored-headers)
1843                                        gnus-ignored-headers)
1844                                       ((listp gnus-ignored-headers)
1845                                        (mapconcat 'identity
1846                                                   gnus-ignored-headers
1847                                                   "\\|"))))
1848                       visible (cond ((stringp gnus-visible-headers)
1849                                      gnus-visible-headers)
1850                                     ((and gnus-visible-headers
1851                                           (listp gnus-visible-headers))
1852                                      (mapconcat 'identity
1853                                                 gnus-visible-headers
1854                                                 "\\|")))))
1855           (set-buffer cur))
1856         (save-restriction
1857           ;; First we narrow to just the headers.
1858           (article-narrow-to-head)
1859           ;; Hide any "From " lines at the beginning of (mail) articles.
1860           (while (looking-at "From ")
1861             (forward-line 1))
1862           (unless (bobp)
1863             (delete-region (point-min) (point)))
1864           ;; Then treat the rest of the header lines.
1865           ;; Then we use the two regular expressions
1866           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1867           ;; select which header lines is to remain visible in the
1868           ;; article buffer.
1869           (while (re-search-forward "^[^ \t:]*:" nil t)
1870             (beginning-of-line)
1871             ;; Mark the rank of the header.
1872             (put-text-property
1873              (point) (1+ (point)) 'message-rank
1874              (if (or (and visible (looking-at visible))
1875                      (and ignored
1876                           (not (looking-at ignored))))
1877                  (gnus-article-header-rank)
1878                (+ 2 max)))
1879             (forward-line 1))
1880           (message-sort-headers-1)
1881           (when (setq beg (text-property-any
1882                            (point-min) (point-max) 'message-rank (+ 2 max)))
1883             ;; We delete the unwanted headers.
1884             (gnus-add-wash-type 'headers)
1885             (add-text-properties (point-min) (+ 5 (point-min))
1886                                  '(article-type headers dummy-invisible t))
1887             (delete-region beg (point-max))))))))
1888
1889 (defun article-hide-boring-headers (&optional arg)
1890   "Toggle hiding of headers that aren't very interesting.
1891 If given a negative prefix, always show; if given a positive prefix,
1892 always hide."
1893   (interactive (gnus-article-hidden-arg))
1894   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1895              (not gnus-show-all-headers))
1896     (save-excursion
1897       (save-restriction
1898         (let ((inhibit-read-only t)
1899               (inhibit-point-motion-hooks t))
1900           (article-narrow-to-head)
1901           (dolist (elem gnus-boring-article-headers)
1902             (goto-char (point-min))
1903             (cond
1904              ;; Hide empty headers.
1905              ((eq elem 'empty)
1906               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1907                 (forward-line -1)
1908                 (gnus-article-hide-text-type
1909                  (point-at-bol)
1910                  (progn
1911                    (end-of-line)
1912                    (if (re-search-forward "^[^ \t]" nil t)
1913                        (match-beginning 0)
1914                      (point-max)))
1915                  'boring-headers)))
1916              ;; Hide boring Newsgroups header.
1917              ((eq elem 'newsgroups)
1918               (when (gnus-string-equal
1919                      (gnus-fetch-field "newsgroups")
1920                      (gnus-group-real-name
1921                       (if (boundp 'gnus-newsgroup-name)
1922                           gnus-newsgroup-name
1923                         "")))
1924                 (gnus-article-hide-header "newsgroups")))
1925              ((eq elem 'to-address)
1926               (let ((to (message-fetch-field "to"))
1927                     (to-address
1928                      (gnus-parameter-to-address
1929                       (if (boundp 'gnus-newsgroup-name)
1930                           gnus-newsgroup-name ""))))
1931                 (when (and to to-address
1932                            (ignore-errors
1933                              (gnus-string-equal
1934                               ;; only one address in To
1935                               (nth 1 (mail-extract-address-components to))
1936                               to-address)))
1937                   (gnus-article-hide-header "to"))))
1938              ((eq elem 'to-list)
1939               (let ((to (message-fetch-field "to"))
1940                     (to-list
1941                      (gnus-parameter-to-list
1942                       (if (boundp 'gnus-newsgroup-name)
1943                           gnus-newsgroup-name ""))))
1944                 (when (and to to-list
1945                            (ignore-errors
1946                              (gnus-string-equal
1947                               ;; only one address in To
1948                               (nth 1 (mail-extract-address-components to))
1949                               to-list)))
1950                   (gnus-article-hide-header "to"))))
1951              ((eq elem 'cc-list)
1952               (let ((cc (message-fetch-field "cc"))
1953                     (to-list
1954                      (gnus-parameter-to-list
1955                       (if (boundp 'gnus-newsgroup-name)
1956                           gnus-newsgroup-name ""))))
1957                 (when (and cc to-list
1958                            (ignore-errors
1959                              (gnus-string-equal
1960                               ;; only one address in CC
1961                               (nth 1 (mail-extract-address-components cc))
1962                               to-list)))
1963                   (gnus-article-hide-header "cc"))))
1964              ((eq elem 'followup-to)
1965               (when (gnus-string-equal
1966                      (message-fetch-field "followup-to")
1967                      (message-fetch-field "newsgroups"))
1968                 (gnus-article-hide-header "followup-to")))
1969              ((eq elem 'reply-to)
1970               (if (gnus-group-find-parameter
1971                    gnus-newsgroup-name 'broken-reply-to)
1972                   (gnus-article-hide-header "reply-to")
1973                 (let ((from (message-fetch-field "from"))
1974                       (reply-to (message-fetch-field "reply-to")))
1975                   (when
1976                       (and
1977                        from reply-to
1978                        (ignore-errors
1979                          (equal
1980                           (sort (mapcar
1981                                  (lambda (x) (downcase (cadr x)))
1982                                  (mail-extract-address-components from t))
1983                                 'string<)
1984                           (sort (mapcar
1985                                  (lambda (x) (downcase (cadr x)))
1986                                  (mail-extract-address-components reply-to t))
1987                                 'string<))))
1988                     (gnus-article-hide-header "reply-to")))))
1989              ((eq elem 'date)
1990               (let ((date (with-current-buffer gnus-original-article-buffer
1991                             ;; If date in `gnus-article-buffer' is localized
1992                             ;; (`gnus-treat-date-user-defined'),
1993                             ;; `days-between' might fail.
1994                             (message-fetch-field "date"))))
1995                 (when (and date
1996                            (< (days-between (current-time-string) date)
1997                               4))
1998                   (gnus-article-hide-header "date"))))
1999              ((eq elem 'long-to)
2000               (let ((to (message-fetch-field "to"))
2001                     (cc (message-fetch-field "cc")))
2002                 (when (> (length to) 1024)
2003                   (gnus-article-hide-header "to"))
2004                 (when (> (length cc) 1024)
2005                   (gnus-article-hide-header "cc"))))
2006              ((eq elem 'many-to)
2007               (let ((to-count 0)
2008                     (cc-count 0))
2009                 (goto-char (point-min))
2010                 (while (re-search-forward "^to:" nil t)
2011                   (setq to-count (1+ to-count)))
2012                 (when (> to-count 1)
2013                   (while (> to-count 0)
2014                     (goto-char (point-min))
2015                     (save-restriction
2016                       (re-search-forward "^to:" nil nil to-count)
2017                       (forward-line -1)
2018                       (narrow-to-region (point) (point-max))
2019                       (gnus-article-hide-header "to"))
2020                     (setq to-count (1- to-count))))
2021                 (goto-char (point-min))
2022                 (while (re-search-forward "^cc:" nil t)
2023                   (setq cc-count (1+ cc-count)))
2024                 (when (> cc-count 1)
2025                   (while (> cc-count 0)
2026                     (goto-char (point-min))
2027                     (save-restriction
2028                       (re-search-forward "^cc:" nil nil cc-count)
2029                       (forward-line -1)
2030                       (narrow-to-region (point) (point-max))
2031                       (gnus-article-hide-header "cc"))
2032                     (setq cc-count (1- cc-count)))))))))))))
2033
2034 (defun gnus-article-hide-header (header)
2035   (save-excursion
2036     (goto-char (point-min))
2037     (when (re-search-forward (concat "^" header ":") nil t)
2038       (gnus-article-hide-text-type
2039        (point-at-bol)
2040        (progn
2041          (end-of-line)
2042          (if (re-search-forward "^[^ \t]" nil t)
2043              (match-beginning 0)
2044            (point-max)))
2045        'boring-headers))))
2046
2047 (defvar gnus-article-normalized-header-length 40
2048   "Length of normalized headers.")
2049
2050 (defun article-normalize-headers ()
2051   "Make all header lines 40 characters long."
2052   (interactive)
2053   (let ((inhibit-read-only t)
2054         column)
2055     (save-excursion
2056       (save-restriction
2057         (article-narrow-to-head)
2058         (while (not (eobp))
2059           (cond
2060            ((< (setq column (- (point-at-eol) (point)))
2061                gnus-article-normalized-header-length)
2062             (end-of-line)
2063             (insert (make-string
2064                      (- gnus-article-normalized-header-length column)
2065                      ? )))
2066            ((> column gnus-article-normalized-header-length)
2067             (gnus-put-text-property
2068              (progn
2069                (forward-char gnus-article-normalized-header-length)
2070                (point))
2071              (point-at-eol)
2072              'invisible t))
2073            (t
2074             ;; Do nothing.
2075             ))
2076           (forward-line 1))))))
2077
2078 (defun article-treat-dumbquotes ()
2079   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2080 Note that this function guesses whether a character is a sm*rtq**t* or
2081 not, so it should only be used interactively.
2082
2083 Sm*rtq**t*s are M****s***'s unilateral extension to the
2084 iso-8859-1 character map in an attempt to provide more quoting
2085 characters.  If you see something like \\222 or \\264 where
2086 you're expecting some kind of apostrophe or quotation mark, then
2087 try this wash."
2088   (interactive)
2089   (article-translate-strings gnus-article-dumbquotes-map))
2090
2091 (defvar org-entities)
2092
2093 (defun article-treat-non-ascii ()
2094   "Translate many Unicode characters into their ASCII equivalents."
2095   (interactive)
2096   (require 'org-entities)
2097   (let ((table (make-char-table (if (featurep 'xemacs) 'generic))))
2098     (dolist (elem org-entities)
2099       (when (and (listp elem)
2100                  (= (length (nth 6 elem)) 1))
2101         (if (featurep 'xemacs)
2102             (put-char-table (aref (nth 6 elem) 0) (nth 4 elem) table)
2103           (set-char-table-range table (aref (nth 6 elem) 0) (nth 4 elem)))))
2104     (save-excursion
2105       (when (article-goto-body)
2106         (let ((inhibit-read-only t)
2107               replace props)
2108           (while (not (eobp))
2109             (if (not (setq replace (if (featurep 'xemacs)
2110                                        (get-char-table (following-char) table)
2111                                      (aref table (following-char)))))
2112                 (forward-char 1)
2113               (if (prog1
2114                       (setq props (text-properties-at (point)))
2115                     (delete-char 1))
2116                   (add-text-properties (point) (progn (insert replace) (point))
2117                                        props)
2118                 (insert replace)))))))))
2119
2120 (defun article-translate-characters (from to)
2121   "Translate all characters in the body of the article according to FROM and TO.
2122 FROM is a string of characters to translate from; to is a string of
2123 characters to translate to."
2124   (save-excursion
2125     (when (article-goto-body)
2126       (let ((inhibit-read-only t)
2127             (x (make-string 225 ?x))
2128             (i -1))
2129         (while (< (incf i) (length x))
2130           (aset x i i))
2131         (setq i 0)
2132         (while (< i (length from))
2133           (aset x (aref from i) (aref to i))
2134           (incf i))
2135         (translate-region (point) (point-max) x)))))
2136
2137 (defun article-translate-strings (map)
2138   "Translate all string in the body of the article according to MAP.
2139 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2140   (save-excursion
2141     (when (article-goto-body)
2142       (let ((inhibit-read-only t))
2143         (dolist (elem map)
2144           (let ((from (car elem))
2145                 (to (cadr elem)))
2146             (save-excursion
2147               (if (stringp from)
2148                   (while (search-forward from nil t)
2149                     (replace-match to))
2150                 (while (not (eobp))
2151                   (if (eq (following-char) from)
2152                       (progn
2153                         (delete-char 1)
2154                         (insert to))
2155                     (forward-char 1)))))))))))
2156
2157 (defun article-treat-overstrike ()
2158   "Translate overstrikes into bold text."
2159   (interactive)
2160   (save-excursion
2161     (when (article-goto-body)
2162       (let ((inhibit-read-only t))
2163         (while (search-forward "\b" nil t)
2164           (let ((next (char-after))
2165                 (previous (char-after (- (point) 2))))
2166             ;; We do the boldification/underlining by hiding the
2167             ;; overstrikes and putting the proper text property
2168             ;; on the letters.
2169             (cond
2170              ((eq next previous)
2171               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2172               (put-text-property (point) (1+ (point)) 'face 'bold))
2173              ((eq next ?_)
2174               (gnus-article-hide-text-type
2175                (1- (point)) (1+ (point)) 'overstrike)
2176               (put-text-property
2177                (- (point) 2) (1- (point)) 'face 'underline))
2178              ((eq previous ?_)
2179               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2180               (put-text-property
2181                (point) (1+ (point)) 'face 'underline)))))))))
2182
2183 (defun article-treat-ansi-sequences ()
2184   "Translate ANSI SGR control sequences into overlays or extents."
2185   (interactive)
2186   (save-excursion
2187     (when (article-goto-body)
2188       (let ((inhibit-read-only t))
2189         (ansi-color-apply-on-region (point) (point-max))))))
2190
2191 (defun gnus-article-treat-unfold-headers ()
2192   "Unfold folded message headers.
2193 Only the headers that fit into the current window width will be
2194 unfolded."
2195   (interactive)
2196   (gnus-with-article-headers
2197     (let (length)
2198       (while (not (eobp))
2199         (save-restriction
2200           (mail-header-narrow-to-field)
2201           (let* ((header (buffer-string))
2202                  (unfoldable
2203                   (or (equal gnus-article-unfold-long-headers t)
2204                       (and (stringp gnus-article-unfold-long-headers)
2205                            (string-match gnus-article-unfold-long-headers header)))))
2206             (with-temp-buffer
2207               (insert header)
2208               (goto-char (point-min))
2209               (while (re-search-forward "\n[\t ]" nil t)
2210                 (replace-match " " t t)))
2211             (setq length (- (point-max) (point-min) 1))
2212             (when (or unfoldable
2213                       (< length (window-width)))
2214               (while (re-search-forward "\n[\t ]" nil t)
2215                 (replace-match " " t t))))
2216           (goto-char (point-max)))))))
2217
2218 (defun gnus-article-treat-fold-headers ()
2219   "Fold message headers."
2220   (interactive)
2221   (gnus-with-article-headers
2222     (while (not (eobp))
2223       (save-restriction
2224         (mail-header-narrow-to-field)
2225         (mail-header-fold-field)
2226         (goto-char (point-max))))))
2227
2228 (defun gnus-treat-smiley ()
2229   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2230   (interactive)
2231   (gnus-with-article-buffer
2232     (if (memq 'smiley gnus-article-wash-types)
2233         (gnus-delete-images 'smiley)
2234       (article-goto-body)
2235       (let ((images (smiley-region (point) (point-max))))
2236         (when images
2237           (gnus-add-wash-type 'smiley)
2238           (dolist (image images)
2239             (gnus-add-image 'smiley image)))))))
2240
2241 (defun gnus-article-remove-images ()
2242   "Remove all images from the article buffer."
2243   (interactive)
2244   (gnus-with-article-buffer
2245     (save-restriction
2246       (widen)
2247       (dolist (elem gnus-article-image-alist)
2248         (gnus-delete-images (car elem))))))
2249
2250 (defun gnus-article-show-images ()
2251   "Show any images that are in the HTML-rendered article buffer.
2252 This only works if the article in question is HTML."
2253   (interactive)
2254   (gnus-with-article-buffer
2255     (save-restriction
2256       (widen)
2257       (dolist (region (gnus-find-text-property-region (point-min) (point-max)
2258                                                       'image-displayer))
2259         (destructuring-bind (start end function) region
2260           (funcall function (get-text-property start 'image-url)
2261                    start end))))))
2262
2263 (defun gnus-article-treat-fold-newsgroups ()
2264   "Unfold folded message headers.
2265 Only the headers that fit into the current window width will be
2266 unfolded."
2267   (interactive)
2268   (gnus-with-article-headers
2269     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2270       (save-restriction
2271         (mail-header-narrow-to-field)
2272         (while (re-search-forward ", *" nil t)
2273           (replace-match ", " t t))
2274         (mail-header-fold-field)
2275         (goto-char (point-max))))))
2276
2277 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2278   "Value of `truncate-lines' in Gnus Article buffer.
2279 Valid values are nil, t, `head', `first', `last', an integer or a
2280 predicate.  See Info node `(gnus)Customizing Articles'."
2281   :version "23.1" ;; No Gnus
2282   :group 'gnus-article
2283   ;; :link '(custom-manual "(gnus)Customizing Articles")
2284   :type 'boolean)
2285
2286 (defun gnus-article-toggle-truncate-lines (&optional arg)
2287   "Toggle whether to fold or truncate long lines in article the buffer.
2288 If ARG is non-nil and not a number, toggle
2289 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2290 long lines if and only if arg is positive."
2291   (interactive "P")
2292   (cond
2293    ((and (numberp arg) (> arg 0))
2294     (setq gnus-article-truncate-lines t))
2295    ((numberp arg)
2296     (setq gnus-article-truncate-lines nil))
2297    (arg
2298     (setq gnus-article-truncate-lines
2299           (not gnus-article-truncate-lines))))
2300   (gnus-with-article-buffer
2301     (cond
2302      ((and (numberp arg) (> arg 0))
2303       (setq truncate-lines nil))
2304      ((numberp arg)
2305       (setq truncate-lines t)))
2306     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2307     ;; `toggle-truncate-lines' needs an argument.
2308     (toggle-truncate-lines)))
2309
2310 (defun gnus-article-treat-body-boundary ()
2311   "Place a boundary line at the end of the headers."
2312   (interactive)
2313   (when (and gnus-body-boundary-delimiter
2314              (> (length gnus-body-boundary-delimiter) 0))
2315     (gnus-with-article-headers
2316       (goto-char (point-max))
2317       (let ((start (point)))
2318         (insert "X-Boundary: ")
2319         (gnus-add-text-properties start (point) '(invisible t intangible t))
2320         (insert (let (str)
2321                   (while (>= (window-width) (length str))
2322                     (setq str (concat str gnus-body-boundary-delimiter)))
2323                   (substring str 0 (window-width)))
2324                 "\n")
2325         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2326
2327 (defun article-fill-long-lines ()
2328   "Fill lines that are wider than the window width."
2329   (interactive)
2330   (save-excursion
2331     (let ((inhibit-read-only t)
2332           (width (window-width (get-buffer-window (current-buffer)))))
2333       (save-restriction
2334         (article-goto-body)
2335         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2336           (while (not (eobp))
2337             (end-of-line)
2338             (when (>= (current-column) (min fill-column width))
2339               (narrow-to-region (min (1+ (point)) (point-max))
2340                                 (point-at-bol))
2341               (let ((goback (point-marker)))
2342                 (fill-paragraph nil)
2343                 (goto-char (marker-position goback)))
2344               (widen))
2345             (forward-line 1)))))))
2346
2347 (defun article-capitalize-sentences ()
2348   "Capitalize the first word in each sentence."
2349   (interactive)
2350   (save-excursion
2351     (let ((inhibit-read-only t)
2352           (paragraph-start "^[\n\^L]"))
2353       (article-goto-body)
2354       (while (not (eobp))
2355         (capitalize-word 1)
2356         (forward-sentence)))))
2357
2358 (defun article-remove-cr ()
2359   "Remove trailing CRs and then translate remaining CRs into LFs."
2360   (interactive)
2361   (save-excursion
2362     (let ((inhibit-read-only t))
2363       (goto-char (point-min))
2364       (while (re-search-forward "\r+$" nil t)
2365         (replace-match "" t t))
2366       (goto-char (point-min))
2367       (while (search-forward "\r" nil t)
2368         (replace-match "\n" t t)))))
2369
2370 (defun article-remove-trailing-blank-lines ()
2371   "Remove all trailing blank lines from the article."
2372   (interactive)
2373   (save-excursion
2374     (let ((inhibit-read-only t))
2375       (goto-char (point-max))
2376       (delete-region
2377        (point)
2378        (progn
2379          (while (and (not (bobp))
2380                      (looking-at "^[ \t]*$")
2381                      (not (gnus-annotation-in-region-p
2382                            (point) (point-at-eol))))
2383            (forward-line -1))
2384          (forward-line 1)
2385          (point))))))
2386
2387 (defvar gnus-face-properties-alist)
2388
2389 (defun article-display-face (&optional force)
2390   "Display any Face headers in the header."
2391   (interactive (list 'force))
2392   (let ((wash-face-p buffer-read-only))
2393     (gnus-with-article-headers
2394       ;; When displaying parts, this function can be called several times on
2395       ;; the same article, without any intended toggle semantic (as typing `W
2396       ;; D d' would have). So face deletion must occur only when we come from
2397       ;; an interactive command, that is when the *Article* buffer is
2398       ;; read-only.
2399       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2400           (gnus-delete-images 'face)
2401         (let ((from (message-fetch-field "from"))
2402               face faces)
2403           (save-current-buffer
2404             (when (and wash-face-p
2405                        (gnus-buffer-live-p gnus-original-article-buffer)
2406                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2407               (set-buffer gnus-original-article-buffer))
2408             (save-restriction
2409               (mail-narrow-to-head)
2410               (when (or force
2411                         ;; Check whether this face is censored.
2412                         (not (and gnus-article-x-face-too-ugly
2413                                   (or from
2414                                       (setq from (message-fetch-field "from")))
2415                                   (string-match gnus-article-x-face-too-ugly
2416                                                 from))))
2417                 (while (gnus-article-goto-header "Face")
2418                   (push (mail-header-field-value) faces)))))
2419           (when faces
2420             (goto-char (point-min))
2421             (let (png image)
2422               (unless (setq from (gnus-article-goto-header "from"))
2423                 (insert "From:")
2424                 (setq from (point))
2425                 (insert " [no `from' set]\n"))
2426               (while faces
2427                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2428                   (setq image
2429                         (apply 'gnus-create-image png 'png t
2430                                (cdr (assq 'png gnus-face-properties-alist))))
2431                   (goto-char from)
2432                   (gnus-add-wash-type 'face)
2433                   (gnus-add-image 'face image)
2434                   (gnus-put-image image nil 'face))))))))))
2435
2436 (defun article-display-x-face (&optional force)
2437   "Look for an X-Face header and display it if present."
2438   (interactive (list 'force))
2439   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2440     (gnus-with-article-headers
2441       ;; Delete the old process, if any.
2442       (when (process-status "article-x-face")
2443         (delete-process "article-x-face"))
2444       ;; See the comment in `article-display-face'.
2445       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2446           ;; We have already displayed X-Faces, so we remove them
2447           ;; instead.
2448           (gnus-delete-images 'xface)
2449         ;; Display X-Faces.
2450         (let ((from (message-fetch-field "from"))
2451               x-faces face)
2452           (save-current-buffer
2453             (when (and wash-face-p
2454                        (gnus-buffer-live-p gnus-original-article-buffer)
2455                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2456               ;; If type `W f', use gnus-original-article-buffer,
2457               ;; otherwise use the current buffer because displaying
2458               ;; RFC822 parts calls this function too.
2459               (set-buffer gnus-original-article-buffer))
2460             (save-restriction
2461               (mail-narrow-to-head)
2462               (and gnus-article-x-face-command
2463                    (or force
2464                        ;; Check whether this face is censored.
2465                        (not (and gnus-article-x-face-too-ugly
2466                                  (or from
2467                                      (setq from (message-fetch-field "from")))
2468                                  (string-match gnus-article-x-face-too-ugly
2469                                                from))))
2470                    (while (gnus-article-goto-header "X-Face")
2471                      (push (mail-header-field-value) x-faces)))))
2472           (when x-faces
2473             ;; We display the face.
2474             (cond ((functionp gnus-article-x-face-command)
2475                    ;; The command is a lisp function, so we call it.
2476                    (mapc gnus-article-x-face-command x-faces))
2477                   ((stringp gnus-article-x-face-command)
2478                    ;; The command is a string, so we interpret the command
2479                    ;; as a, well, command, and fork it off.
2480                    (let ((process-connection-type nil))
2481                      (gnus-set-process-query-on-exit-flag
2482                       (start-process
2483                        "article-x-face" nil shell-file-name
2484                        shell-command-switch gnus-article-x-face-command)
2485                       nil)
2486                      ;; Sending multiple EOFs to xv doesn't work,
2487                      ;; so we only do a single external face.
2488                      (with-temp-buffer
2489                        (insert (car x-faces))
2490                        (process-send-region "article-x-face"
2491                                             (point-min) (point-max)))
2492                      (process-send-eof "article-x-face")))
2493                   (t
2494                    (error "`%s' set to `%s' is not a function"
2495                           gnus-article-x-face-command
2496                           'gnus-article-x-face-command)))))))))
2497
2498 (defun article-decode-mime-words ()
2499   "Decode all MIME-encoded words in the article."
2500   (interactive)
2501   (gnus-with-article-buffer
2502     (let ((inhibit-point-motion-hooks t)
2503           (mail-parse-charset gnus-newsgroup-charset)
2504           (mail-parse-ignored-charsets
2505            (with-current-buffer gnus-summary-buffer
2506              gnus-newsgroup-ignored-charsets)))
2507       (mail-decode-encoded-word-region (point-min) (point-max)))))
2508
2509 (defun article-decode-charset (&optional prompt)
2510   "Decode charset-encoded text in the article.
2511 If PROMPT (the prefix), prompt for a coding system to use."
2512   (interactive "P")
2513   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2514         (inhibit-read-only t)
2515         (mail-parse-charset gnus-newsgroup-charset)
2516         (mail-parse-ignored-charsets
2517          (save-excursion (condition-case nil
2518                              (set-buffer gnus-summary-buffer)
2519                            (error))
2520                          gnus-newsgroup-ignored-charsets))
2521         ct cte ctl charset format)
2522     (save-excursion
2523       (save-restriction
2524         (article-narrow-to-head)
2525         (setq ct (message-fetch-field "Content-Type" t)
2526               cte (message-fetch-field "Content-Transfer-Encoding" t)
2527               ctl (and ct (mail-header-parse-content-type ct))
2528               charset (cond
2529                        (prompt
2530                         (mm-read-coding-system "Charset to decode: "))
2531                        (ctl
2532                         (mail-content-type-get ctl 'charset)))
2533               format (and ctl (mail-content-type-get ctl 'format)))
2534         (when cte
2535           (setq cte (mail-header-strip cte)))
2536         (if (and ctl (not (string-match "/" (car ctl))))
2537             (setq ctl nil))
2538         (goto-char (point-max)))
2539       (forward-line 1)
2540       (save-restriction
2541         (narrow-to-region (point) (point-max))
2542         (when (and (eq mail-parse-charset 'gnus-decoded)
2543                    (eq (mm-body-7-or-8) '8bit))
2544           ;; The text code could have been decoded.
2545           (setq charset mail-parse-charset))
2546         (when (and (or (not ctl)
2547                        (equal (car ctl) "text/plain"))
2548                    (not format)) ;; article with format will decode later.
2549           (mm-decode-body
2550            charset (and cte (intern (downcase
2551                                      (gnus-strip-whitespace cte))))
2552            (car ctl)))))))
2553
2554 (defun article-decode-encoded-words ()
2555   "Remove encoded-word encoding from headers."
2556   (let ((inhibit-point-motion-hooks t)
2557         (mail-parse-charset gnus-newsgroup-charset)
2558         (mail-parse-ignored-charsets
2559          (save-excursion (condition-case nil
2560                              (set-buffer gnus-summary-buffer)
2561                            (error))
2562                          gnus-newsgroup-ignored-charsets))
2563         (inhibit-read-only t)
2564         end start)
2565     (goto-char (point-min))
2566     (when (search-forward "\n\n" nil 'move)
2567       (forward-line -1))
2568     (setq end (point))
2569     (while (not (bobp))
2570       (while (progn
2571                (forward-line -1)
2572                (and (not (bobp))
2573                     (memq (char-after) '(?\t ? )))))
2574       (setq start (point))
2575       (if (looking-at "\
2576 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2577 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2578           (funcall gnus-decode-address-function start end)
2579         (funcall gnus-decode-header-function start end))
2580       (goto-char (setq end start)))))
2581
2582 (defun article-decode-group-name ()
2583   "Decode group names in Newsgroups, Followup-To and Xref headers."
2584   (let ((inhibit-point-motion-hooks t)
2585         (inhibit-read-only t)
2586         (method (gnus-find-method-for-group gnus-newsgroup-name))
2587         regexp)
2588     (when (and (or gnus-group-name-charset-method-alist
2589                    gnus-group-name-charset-group-alist)
2590                (gnus-buffer-live-p gnus-original-article-buffer))
2591       (save-restriction
2592         (article-narrow-to-head)
2593         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2594           (with-current-buffer gnus-original-article-buffer
2595             (goto-char (point-min)))
2596           (setq regexp (concat "^" header
2597                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2598           (while (re-search-forward regexp nil t)
2599             (replace-match (save-match-data
2600                              (gnus-decode-newsgroups
2601                               ;; XXX how to use data in article buffer?
2602                               (with-current-buffer gnus-original-article-buffer
2603                                 (re-search-forward regexp nil t)
2604                                 (match-string 1))
2605                               gnus-newsgroup-name method))
2606                            t t nil 1))
2607           (goto-char (point-min)))))))
2608
2609 (autoload 'idna-to-unicode "idna")
2610
2611 (defun article-decode-idna-rhs ()
2612   "Decode IDNA strings in RHS in various headers in current buffer.
2613 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2614 Mail-Reply-To: and Mail-Followup-To:."
2615   (when gnus-use-idna
2616     (save-restriction
2617       (let ((inhibit-point-motion-hooks t)
2618             (inhibit-read-only t))
2619         (article-narrow-to-head)
2620         (goto-char (point-min))
2621         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2622           (let (ace unicode)
2623             (when (save-match-data
2624                     (and (setq ace (match-string 1))
2625                          (save-excursion
2626                            (and (re-search-backward "^[^ \t]" nil t)
2627                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2628                          (setq unicode (idna-to-unicode ace))))
2629               (unless (string= ace unicode)
2630                 (replace-match unicode nil nil nil 1)))))))))
2631
2632 (defun article-de-quoted-unreadable (&optional force read-charset)
2633   "Translate a quoted-printable-encoded article.
2634 If FORCE, decode the article whether it is marked as quoted-printable
2635 or not.
2636 If READ-CHARSET, ask for a coding system."
2637   (interactive (list 'force current-prefix-arg))
2638   (save-excursion
2639     (let ((inhibit-read-only t) type charset)
2640       (if (gnus-buffer-live-p gnus-original-article-buffer)
2641           (with-current-buffer gnus-original-article-buffer
2642             (setq type
2643                   (gnus-fetch-field "content-transfer-encoding"))
2644             (let* ((ct (gnus-fetch-field "content-type"))
2645                    (ctl (and ct (mail-header-parse-content-type ct))))
2646               (setq charset (and ctl
2647                                  (mail-content-type-get ctl 'charset)))
2648               (if (stringp charset)
2649                   (setq charset (intern (downcase charset)))))))
2650       (if read-charset
2651           (setq charset (mm-read-coding-system "Charset: " charset)))
2652       (unless charset
2653         (setq charset gnus-newsgroup-charset))
2654       (when (or force
2655                 (and type (let ((case-fold-search t))
2656                             (string-match "quoted-printable" type))))
2657         (article-goto-body)
2658         (quoted-printable-decode-region
2659          (point) (point-max) (mm-charset-to-coding-system charset))))))
2660
2661 (defun article-de-base64-unreadable (&optional force read-charset)
2662   "Translate a base64 article.
2663 If FORCE, decode the article whether it is marked as base64 not.
2664 If READ-CHARSET, ask for a coding system."
2665   (interactive (list 'force current-prefix-arg))
2666   (save-excursion
2667     (let ((inhibit-read-only t) type charset)
2668       (if (gnus-buffer-live-p gnus-original-article-buffer)
2669           (with-current-buffer gnus-original-article-buffer
2670             (setq type
2671                   (gnus-fetch-field "content-transfer-encoding"))
2672             (let* ((ct (gnus-fetch-field "content-type"))
2673                    (ctl (and ct (mail-header-parse-content-type ct))))
2674               (setq charset (and ctl
2675                                  (mail-content-type-get ctl 'charset)))
2676               (if (stringp charset)
2677                   (setq charset (intern (downcase charset)))))))
2678       (if read-charset
2679           (setq charset (mm-read-coding-system "Charset: " charset)))
2680       (unless charset
2681         (setq charset gnus-newsgroup-charset))
2682       (when (or force
2683                 (and type (let ((case-fold-search t))
2684                             (string-match "base64" type))))
2685         (article-goto-body)
2686         (save-restriction
2687           (narrow-to-region (point) (point-max))
2688           (base64-decode-region (point-min) (point-max))
2689           (mm-decode-coding-region
2690            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2691
2692 (eval-when-compile
2693   (require 'rfc1843))
2694
2695 (defun article-decode-HZ ()
2696   "Translate a HZ-encoded article."
2697   (interactive)
2698   (require 'rfc1843)
2699   (save-excursion
2700     (let ((inhibit-read-only t))
2701       (rfc1843-decode-region (point-min) (point-max)))))
2702
2703 (defun article-unsplit-urls ()
2704   "Remove the newlines that some other mailers insert into URLs."
2705   (interactive)
2706   (save-excursion
2707     (let ((inhibit-read-only t))
2708       (goto-char (point-min))
2709       (while (re-search-forward
2710               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2711         (replace-match "\\1\\3" t)))
2712     (when (interactive-p)
2713       (gnus-treat-article nil))))
2714
2715 (defun article-wash-html ()
2716   "Format an HTML article."
2717   (interactive)
2718   (let ((handles nil)
2719         (buffer-read-only nil))
2720     (when (gnus-buffer-live-p gnus-original-article-buffer)
2721       (setq handles (mm-dissect-buffer t t)))
2722     (article-goto-body)
2723     (delete-region (point) (point-max))
2724     (mm-inline-text-html handles)))
2725
2726 (defvar gnus-article-browse-html-temp-list nil
2727   "List of temporary files created by `gnus-article-browse-html-parts'.
2728 Internal variable.")
2729
2730 (defcustom gnus-article-browse-delete-temp 'ask
2731   "What to do with temporary files from `gnus-article-browse-html-parts'.
2732 If nil, don't delete temporary files.  If it is t, delete them on
2733 exit from the summary buffer.  If it is the symbol `file', query
2734 on each file, if it is `ask' ask once when exiting from the
2735 summary buffer."
2736   :group 'gnus-article
2737   :version "23.1" ;; No Gnus
2738   :type '(choice (const :tag "Don't delete" nil)
2739                  (const :tag "Don't ask" t)
2740                  (const :tag "Ask" ask)
2741                  (const :tag "Ask for each file" file)))
2742
2743 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2744
2745 (defun gnus-article-browse-delete-temp-files (&optional how)
2746   "Delete temp-files created by `gnus-article-browse-html-parts'."
2747   (when (and gnus-article-browse-html-temp-list
2748              (progn
2749                (or how (setq how gnus-article-browse-delete-temp))
2750                (if (eq how 'ask)
2751                    (let ((files (length gnus-article-browse-html-temp-list)))
2752                      (gnus-y-or-n-p (format
2753                                      "Delete all %s temporary HTML file%s? "
2754                                      files
2755                                      (if (> files 1) "s" ""))))
2756                  how)))
2757     (dolist (file gnus-article-browse-html-temp-list)
2758       (cond ((file-directory-p file)
2759              (when (or (not (eq how 'file))
2760                        (gnus-y-or-n-p
2761                         (format
2762                          "Delete temporary HTML file(s) in directory `%s'? "
2763                          (file-name-as-directory file))))
2764                (gnus-delete-directory file)))
2765             ((file-exists-p file)
2766              (when (or (not (eq how 'file))
2767                        (gnus-y-or-n-p
2768                         (format "Delete temporary HTML file `%s'? " file)))
2769                (delete-file file)))))
2770     ;; Also remove file from the list when not deleted or if file doesn't
2771     ;; exist anymore.
2772     (setq gnus-article-browse-html-temp-list nil))
2773   gnus-article-browse-html-temp-list)
2774
2775 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2776   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2777 Return file name."
2778   (save-match-data
2779     (let (file type)
2780       (catch 'found
2781         (dolist (handle handles)
2782           (cond
2783            ((not (listp handle)))
2784            ((equal (mm-handle-media-supertype handle) "multipart")
2785             (when (setq file (gnus-article-browse-html-save-cid-content
2786                               cid handle directory))
2787               (throw 'found file)))
2788            ((equal (concat "<" cid ">") (mm-handle-id handle))
2789             (setq file
2790                   (expand-file-name
2791                    (or (mail-content-type-get
2792                         (mm-handle-disposition handle) 'filename)
2793                        (mail-content-type-get
2794                         (setq type (mm-handle-type handle)) 'name)
2795                        (concat
2796                         (make-temp-name "cid")
2797                         (car (rassoc (car type) mailcap-mime-extensions))))
2798                    directory))
2799             (mm-save-part-to-file handle file)
2800             (throw 'found file))))))))
2801
2802 (defun gnus-article-browse-html-parts (list &optional header)
2803   "View all \"text/html\" parts from LIST.
2804 Recurse into multiparts.  The optional HEADER that should be a decoded
2805 message header will be added to the bodies of the \"text/html\" parts."
2806   ;; Internal function used by `gnus-article-browse-html-article'.
2807   (let (type file charset content cid-dir tmp-file showed)
2808     ;; Find and show the html-parts.
2809     (dolist (handle list)
2810       ;; If HTML, show it:
2811       (cond ((not (listp handle)))
2812             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2813                  (and (equal (car type) "message/external-body")
2814                       (or header
2815                           (setq file (or (mail-content-type-get type 'name)
2816                                          (mail-content-type-get
2817                                           (mm-handle-disposition handle)
2818                                           'filename))))
2819                       (or (mm-handle-cache handle)
2820                           (condition-case code
2821                               (progn (mm-extern-cache-contents handle) t)
2822                             (error
2823                              (gnus-message 3 "%s" (error-message-string code))
2824                              (when (>= gnus-verbose 3) (sit-for 2))
2825                              nil)))
2826                       (progn
2827                         (setq handle (mm-handle-cache handle)
2828                               type (mm-handle-type handle))
2829                         (equal (car type) "text/html"))))
2830              (setq charset (mail-content-type-get type 'charset)
2831                    content (mm-get-part handle))
2832              (with-temp-buffer
2833                (if (eq charset 'gnus-decoded)
2834                    (mm-enable-multibyte)
2835                  (mm-disable-multibyte))
2836                (insert content)
2837                ;; resolve cid contents
2838                (let ((case-fold-search t)
2839                      cid-file)
2840                  (goto-char (point-min))
2841                  (while (re-search-forward "\
2842 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2843                                            nil t)
2844                    (unless cid-dir
2845                      (setq cid-dir (mm-make-temp-file "cid" t))
2846                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2847                    (setq file nil
2848                          content nil)
2849                    (when (setq cid-file
2850                                (gnus-article-browse-html-save-cid-content
2851                                 (match-string 2)
2852                                 (with-current-buffer gnus-article-buffer
2853                                   gnus-article-mime-handles)
2854                                 cid-dir))
2855                      (replace-match (concat "file://" cid-file)
2856                                     nil nil nil 1))))
2857                (unless content (setq content (buffer-string))))
2858              (when (or charset header (not file))
2859                (setq tmp-file (mm-make-temp-file
2860                                ;; Do we need to care for 8.3 filenames?
2861                                "mm-" nil ".html")))
2862              ;; Add a meta html tag to specify charset and a header.
2863              (cond
2864               (header
2865                (let (title eheader body hcharset coding force-charset)
2866                  (with-temp-buffer
2867                    (mm-enable-multibyte)
2868                    (setq case-fold-search t)
2869                    (insert header "\n")
2870                    (setq title (message-fetch-field "subject"))
2871                    (goto-char (point-min))
2872                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2873                      (replace-match (cond ((match-beginning 1) "&lt;")
2874                                           ((match-beginning 2) "&gt;")
2875                                           (t "&amp;"))))
2876                    (goto-char (point-min))
2877                    (insert "<pre>\n")
2878                    (goto-char (point-max))
2879                    (insert "</pre>\n<hr>\n")
2880                    ;; We have to examine charset one by one since
2881                    ;; charset specified in parts might be different.
2882                    (if (eq charset 'gnus-decoded)
2883                        (setq charset 'utf-8
2884                              eheader (mm-encode-coding-string (buffer-string)
2885                                                               charset)
2886                              title (when title
2887                                      (mm-encode-coding-string title charset))
2888                              body (mm-encode-coding-string content charset)
2889                              force-charset t)
2890                      (setq hcharset (mm-find-mime-charset-region (point-min)
2891                                                                  (point-max)))
2892                      (cond ((= (length hcharset) 1)
2893                             (setq hcharset (car hcharset)
2894                                   coding (mm-charset-to-coding-system
2895                                           hcharset)))
2896                            ((> (length hcharset) 1)
2897                             (setq hcharset 'utf-8
2898                                   coding hcharset)))
2899                      (if coding
2900                          (if charset
2901                              (progn
2902                                (setq body
2903                                      (mm-charset-to-coding-system charset))
2904                                (if (eq coding body)
2905                                    (setq eheader (mm-encode-coding-string
2906                                                   (buffer-string) coding)
2907                                          title (when title
2908                                                  (mm-encode-coding-string
2909                                                   title coding))
2910                                          body content)
2911                                  (setq charset 'utf-8
2912                                        eheader (mm-encode-coding-string
2913                                                 (buffer-string) charset)
2914                                        title (when title
2915                                                (mm-encode-coding-string
2916                                                 title charset))
2917                                        body (mm-encode-coding-string
2918                                              (mm-decode-coding-string
2919                                               content body)
2920                                              charset)
2921                                        force-charset t)))
2922                            (setq charset hcharset
2923                                  eheader (mm-encode-coding-string
2924                                           (buffer-string) coding)
2925                                  title (when title
2926                                          (mm-encode-coding-string
2927                                           title coding))
2928                                  body content))
2929                        (setq eheader (mm-string-as-unibyte (buffer-string))
2930                              body content)))
2931                    (erase-buffer)
2932                    (mm-disable-multibyte)
2933                    (insert body)
2934                    (when charset
2935                      (mm-add-meta-html-tag handle charset force-charset))
2936                    (when title
2937                      (goto-char (point-min))
2938                      (unless (search-forward "<title>" nil t)
2939                        (re-search-forward "<head>\\s-*" nil t)
2940                        (insert "<title>" title "</title>\n")))
2941                    (goto-char (point-min))
2942                    (or (re-search-forward
2943                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2944                        (re-search-forward
2945                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2946                    (insert eheader)
2947                    (mm-write-region (point-min) (point-max)
2948                                     tmp-file nil nil nil 'binary t))))
2949               (charset
2950                (mm-with-unibyte-buffer
2951                  (insert (if (eq charset 'gnus-decoded)
2952                              (mm-encode-coding-string content
2953                                                       (setq charset 'utf-8))
2954                            content))
2955                  (if (or (mm-add-meta-html-tag handle charset)
2956                          (not file))
2957                      (mm-write-region (point-min) (point-max)
2958                                       tmp-file nil nil nil 'binary t)
2959                    (setq tmp-file nil))))
2960               (tmp-file
2961                (mm-save-part-to-file handle tmp-file)))
2962              (when tmp-file
2963                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2964              (add-hook 'gnus-summary-prepare-exit-hook
2965                        'gnus-article-browse-delete-temp-files)
2966              (add-hook 'gnus-exit-gnus-hook
2967                        (lambda  ()
2968                          (gnus-article-browse-delete-temp-files t)))
2969              ;; FIXME: Warn if there's an <img> tag?
2970              (browse-url-of-file (or tmp-file (expand-file-name file)))
2971              (setq showed t))
2972             ;; If multipart, recurse
2973             ((equal (mm-handle-media-supertype handle) "multipart")
2974              (when (gnus-article-browse-html-parts handle header)
2975                (setq showed t)))
2976             ((equal (mm-handle-media-type handle) "message/rfc822")
2977              (mm-with-multibyte-buffer
2978                (mm-insert-part handle)
2979                (setq handle (mm-dissect-buffer t t))
2980                (when (and (bufferp (car handle))
2981                           (stringp (car (mm-handle-type handle))))
2982                  (setq handle (list handle)))
2983                (when header
2984                  (article-decode-encoded-words)
2985                  (let ((gnus-visible-headers
2986                         (or (get 'gnus-visible-headers 'standard-value)
2987                             gnus-visible-headers)))
2988                    (article-hide-headers))
2989                  (goto-char (point-min))
2990                  (search-forward "\n\n" nil 'move)
2991                  (skip-chars-backward "\t\n ")
2992                  (setq header (buffer-substring (point-min) (point)))))
2993              (when (prog1
2994                        (gnus-article-browse-html-parts handle header)
2995                      (mm-destroy-parts handle))
2996                (setq showed t)))))
2997     showed))
2998
2999 (defun gnus-article-browse-html-article (&optional arg)
3000   "View \"text/html\" parts of the current article with a WWW browser.
3001 Inline images embedded in a message using the cid scheme, as they are
3002 generally considered to be safe, will be processed properly.
3003 The message header is added to the beginning of every html part unless
3004 the prefix argument ARG is given.
3005
3006 Warning: Spammers use links to images (using the http scheme) in HTML
3007 articles to verify whether you have read the message.  As
3008 `gnus-article-browse-html-article' passes the HTML content to the
3009 browser without eliminating these \"web bugs\" you should only
3010 use it for mails from trusted senders.
3011
3012 If you always want to display HTML parts in the browser, set
3013 `mm-text-html-renderer' to nil.
3014
3015 This command creates temporary files to pass HTML contents including
3016 images if any to the browser, and deletes them when exiting the group
3017 \(if you want)."
3018   ;; Cf. `mm-w3m-safe-url-regexp'
3019   (interactive "P")
3020   (if arg
3021       (gnus-summary-show-article)
3022     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3023                                     gnus-visible-headers))
3024           ;; As we insert a <hr>, there's no need for the body boundary.
3025           (gnus-treat-body-boundary nil))
3026       (gnus-summary-show-article)))
3027   (with-current-buffer gnus-article-buffer
3028     (let ((header (unless arg
3029                     (save-restriction
3030                       (widen)
3031                       (buffer-substring-no-properties
3032                        (goto-char (point-min))
3033                        (if (search-forward "\n\n" nil t)
3034                            (match-beginning 0)
3035                          (goto-char (point-max))
3036                          (skip-chars-backward "\t\n ")
3037                          (point))))))
3038           parts)
3039       (set-buffer gnus-original-article-buffer)
3040       (setq parts (mm-dissect-buffer t t))
3041       ;; If singlepart, enforce a list.
3042       (when (and (bufferp (car parts))
3043                  (stringp (car (mm-handle-type parts))))
3044         (setq parts (list parts)))
3045       ;; Process the list
3046       (unless (gnus-article-browse-html-parts parts header)
3047         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3048       (mm-destroy-parts parts)
3049       (unless arg
3050         (gnus-summary-show-article)))))
3051
3052 (defun article-hide-list-identifiers ()
3053   "Remove list identifies from the Subject header.
3054 The `gnus-list-identifiers' variable specifies what to do."
3055   (interactive)
3056   (let ((inhibit-point-motion-hooks t)
3057         (regexp (if (consp gnus-list-identifiers)
3058                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3059                   gnus-list-identifiers))
3060         (inhibit-read-only t))
3061     (when regexp
3062       (save-excursion
3063         (save-restriction
3064           (article-narrow-to-head)
3065           (goto-char (point-min))
3066           (while (re-search-forward
3067                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3068                   nil t)
3069             (delete-region (match-beginning 2) (match-end 0))
3070             (beginning-of-line))
3071           (when (re-search-forward
3072                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3073             (delete-region (match-beginning 1) (match-end 1))))))))
3074
3075 (defun article-hide-pem (&optional arg)
3076   "Toggle hiding of any PEM headers and signatures in the current article.
3077 If given a negative prefix, always show; if given a positive prefix,
3078 always hide."
3079   (interactive (gnus-article-hidden-arg))
3080   (unless (gnus-article-check-hidden-text 'pem arg)
3081     (save-excursion
3082       (let ((inhibit-read-only t) end)
3083         (goto-char (point-min))
3084         ;; Hide the horrendously ugly "header".
3085         (when (and (search-forward
3086                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3087                     nil t)
3088                    (setq end (1+ (match-beginning 0))))
3089           (gnus-add-wash-type 'pem)
3090           (gnus-article-hide-text-type
3091            end
3092            (if (search-forward "\n\n" nil t)
3093                (match-end 0)
3094              (point-max))
3095            'pem)
3096           ;; Hide the trailer as well
3097           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3098                                 nil t)
3099             (gnus-article-hide-text-type
3100              (match-beginning 0) (match-end 0) 'pem)))))))
3101
3102 (defun article-strip-banner ()
3103   "Strip the banners specified by the `banner' group parameter and by
3104 `gnus-article-address-banner-alist'."
3105   (interactive)
3106   (save-excursion
3107     (save-restriction
3108       (let ((inhibit-point-motion-hooks t))
3109         (when (gnus-parameter-banner gnus-newsgroup-name)
3110           (article-really-strip-banner
3111            (gnus-parameter-banner gnus-newsgroup-name)))
3112         (when gnus-article-address-banner-alist
3113           ;; Note that the From header is decoded here, so it is
3114           ;; required that the *-extract-address-components function
3115           ;; supports non-ASCII text.
3116           (let ((from (save-restriction
3117                         (widen)
3118                         (article-narrow-to-head)
3119                         (mail-fetch-field "from"))))
3120             (when (and from
3121                        (setq from
3122                              (cadr (funcall gnus-extract-address-components
3123                                             from))))
3124               (catch 'found
3125                 (dolist (pair gnus-article-address-banner-alist)
3126                   (when (string-match (car pair) from)
3127                     (throw 'found
3128                            (article-really-strip-banner (cdr pair)))))))))))))
3129
3130 (defun article-really-strip-banner (banner)
3131   "Strip the banner specified by the argument."
3132   (save-excursion
3133     (save-restriction
3134       (let ((inhibit-point-motion-hooks t)
3135             (gnus-signature-limit nil)
3136             (inhibit-read-only t))
3137         (article-goto-body)
3138         (cond
3139          ((eq banner 'signature)
3140           (when (gnus-article-narrow-to-signature)
3141             (widen)
3142             (forward-line -1)
3143             (delete-region (point) (point-max))))
3144          ((symbolp banner)
3145           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3146               (while (re-search-forward banner nil t)
3147                 (delete-region (match-beginning 0) (match-end 0)))))
3148          ((stringp banner)
3149           (while (re-search-forward banner nil t)
3150             (delete-region (match-beginning 0) (match-end 0)))))))))
3151
3152 (defun article-babel ()
3153   "Translate article using an online translation service."
3154   (interactive)
3155   (require 'babel)
3156   (gnus-with-article-buffer
3157     (when (article-goto-body)
3158       (let* ((start (point))
3159              (end (point-max))
3160              (orig (buffer-substring start end))
3161              (trans (babel-as-string orig)))
3162         (save-restriction
3163           (narrow-to-region start end)
3164           (delete-region start end)
3165           (insert trans))))))
3166
3167 (defun article-hide-signature (&optional arg)
3168   "Hide the signature in the current article.
3169 If given a negative prefix, always show; if given a positive prefix,
3170 always hide."
3171   (interactive (gnus-article-hidden-arg))
3172   (unless (gnus-article-check-hidden-text 'signature arg)
3173     (save-excursion
3174       (save-restriction
3175         (let ((inhibit-read-only t))
3176           (when (gnus-article-narrow-to-signature)
3177             (gnus-article-hide-text-type
3178              (point-min) (point-max) 'signature))))))
3179   (gnus-set-mode-line 'article))
3180
3181 (defun article-strip-headers-in-body ()
3182   "Strip offensive headers from bodies."
3183   (interactive)
3184   (save-excursion
3185     (article-goto-body)
3186     (let ((case-fold-search t))
3187       (when (looking-at "x-no-archive:")
3188         (gnus-delete-line)))))
3189
3190 (defun article-strip-leading-blank-lines ()
3191   "Remove all blank lines from the beginning of the article."
3192   (interactive)
3193   (save-excursion
3194     (let ((inhibit-point-motion-hooks t)
3195           (inhibit-read-only t))
3196       (when (article-goto-body)
3197         (while (and (not (eobp))
3198                     (looking-at "[ \t]*$"))
3199           (gnus-delete-line))))))
3200
3201 (defun article-narrow-to-head ()
3202   "Narrow the buffer to the head of the message.
3203 Point is left at the beginning of the narrowed-to region."
3204   (narrow-to-region
3205    (goto-char (point-min))
3206    (if (search-forward "\n\n" nil 1)
3207        (1- (point))
3208      (point-max)))
3209   (goto-char (point-min)))
3210
3211 (defun article-goto-body ()
3212   "Place point at the start of the body."
3213   (goto-char (point-min))
3214   (cond
3215    ;; This variable is only bound when dealing with separate
3216    ;; MIME body parts.
3217    (article-goto-body-goes-to-point-min-p
3218     t)
3219    ((search-forward "\n\n" nil t)
3220     t)
3221    (t
3222     (goto-char (point-max))
3223     nil)))
3224
3225 (defun article-strip-multiple-blank-lines ()
3226   "Replace consecutive blank lines with one empty line."
3227   (interactive)
3228   (save-excursion
3229     (let ((inhibit-point-motion-hooks t)
3230           (inhibit-read-only t))
3231       ;; First make all blank lines empty.
3232       (article-goto-body)
3233       (while (re-search-forward "^[ \t]+$" nil t)
3234         (unless (gnus-annotation-in-region-p
3235                  (match-beginning 0) (match-end 0))
3236           (replace-match "" nil t)))
3237       ;; Then replace multiple empty lines with a single empty line.
3238       (article-goto-body)
3239       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3240         (unless (gnus-annotation-in-region-p
3241                  (match-beginning 0) (match-end 0))
3242           (delete-region (match-beginning 1) (match-end 1)))))))
3243
3244 (defun article-strip-leading-space ()
3245   "Remove all white space from the beginning of the lines in the article."
3246   (interactive)
3247   (save-excursion
3248     (let ((inhibit-point-motion-hooks t)
3249           (inhibit-read-only t))
3250       (article-goto-body)
3251       (while (re-search-forward "^[ \t]+" nil t)
3252         (replace-match "" t t)))))
3253
3254 (defun article-strip-trailing-space ()
3255   "Remove all white space from the end of the lines in the article."
3256   (interactive)
3257   (save-excursion
3258     (let ((inhibit-point-motion-hooks t)
3259           (inhibit-read-only t))
3260       (article-goto-body)
3261       (while (re-search-forward "[ \t]+$" nil t)
3262         (replace-match "" t t)))))
3263
3264 (defun article-strip-blank-lines ()
3265   "Strip leading, trailing and multiple blank lines."
3266   (interactive)
3267   (article-strip-leading-blank-lines)
3268   (article-remove-trailing-blank-lines)
3269   (article-strip-multiple-blank-lines))
3270
3271 (defun article-strip-all-blank-lines ()
3272   "Strip all blank lines."
3273   (interactive)
3274   (save-excursion
3275     (let ((inhibit-point-motion-hooks t)
3276           (inhibit-read-only t))
3277       (article-goto-body)
3278       (while (re-search-forward "^[ \t]*\n" nil t)
3279         (replace-match "" t t)))))
3280
3281 (defun gnus-article-narrow-to-signature ()
3282   "Narrow to the signature; return t if a signature is found, else nil."
3283   (let ((inhibit-point-motion-hooks t))
3284     (when (gnus-article-search-signature)
3285       (forward-line 1)
3286       ;; Check whether we have some limits to what we consider
3287       ;; to be a signature.
3288       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3289                       (list gnus-signature-limit)))
3290             limit limited)
3291         (while (setq limit (pop limits))
3292           (if (or (and (integerp limit)
3293                        (< (- (point-max) (point)) limit))
3294                   (and (floatp limit)
3295                        (< (count-lines (point) (point-max)) limit))
3296                   (and (functionp limit)
3297                        (funcall limit))
3298                   (and (stringp limit)
3299                        (not (re-search-forward limit nil t))))
3300               ()                        ; This limit did not succeed.
3301             (setq limited t
3302                   limits nil)))
3303         (unless limited
3304           (narrow-to-region (point) (point-max))
3305           t)))))
3306
3307 (defun gnus-article-search-signature ()
3308   "Search the current buffer for the signature separator.
3309 Put point at the beginning of the signature separator."
3310   (let ((cur (point)))
3311     (goto-char (point-max))
3312     (if (if (stringp gnus-signature-separator)
3313             (re-search-backward gnus-signature-separator nil t)
3314           (let ((seps gnus-signature-separator))
3315             (while (and seps
3316                         (not (re-search-backward (car seps) nil t)))
3317               (pop seps))
3318             seps))
3319         t
3320       (goto-char cur)
3321       nil)))
3322
3323 (defun gnus-article-hidden-arg ()
3324   "Return the current prefix arg as a number, or 0 if no prefix."
3325   (list (if current-prefix-arg
3326             (prefix-numeric-value current-prefix-arg)
3327           0)))
3328
3329 (defun gnus-article-check-hidden-text (type arg)
3330   "Return nil if hiding is necessary.
3331 Arg can be nil or a number.  nil and positive means hide, negative
3332 means show, 0 means toggle."
3333   (save-excursion
3334     (save-restriction
3335       (let ((hide (gnus-article-hidden-text-p type)))
3336         (cond
3337          ((or (null arg)
3338               (> arg 0))
3339           nil)
3340          ((< arg 0)
3341           (gnus-article-show-hidden-text type)
3342           t)
3343          (t
3344           (if (eq hide 'hidden)
3345               (progn
3346                 (gnus-article-show-hidden-text type)
3347                 t)
3348             nil)))))))
3349
3350 (defun gnus-article-hidden-text-p (type)
3351   "Say whether the current buffer contains hidden text of type TYPE."
3352   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3353     (while (and pos
3354                 (not (get-text-property pos 'invisible))
3355                 (not (get-text-property pos 'dummy-invisible)))
3356       (setq pos
3357             (text-property-any (1+ pos) (point-max) 'article-type type)))
3358     (if pos
3359         'hidden
3360       nil)))
3361
3362 (defun gnus-article-show-hidden-text (type &optional dummy)
3363   "Show all hidden text of type TYPE.
3364 Originally it is hide instead of DUMMY."
3365   (let ((inhibit-read-only t)
3366         (inhibit-point-motion-hooks t))
3367     (gnus-remove-text-properties-when
3368      'article-type type
3369      (point-min) (point-max)
3370      (cons 'article-type (cons type
3371                                gnus-hidden-properties)))
3372     (gnus-delete-wash-type type)))
3373
3374 (defconst article-time-units
3375   `((year . ,(* 365.25 24 60 60))
3376     (week . ,(* 7 24 60 60))
3377     (day . ,(* 24 60 60))
3378     (hour . ,(* 60 60))
3379     (minute . 60)
3380     (second . 1))
3381   "Mapping from time units to seconds.")
3382
3383 (defun gnus-article-forward-header ()
3384   "Move point to the start of the next header.
3385 If the current header is a continuation header, this can be several
3386 lines forward."
3387   (let ((ended nil))
3388     (while (not ended)
3389       (forward-line 1)
3390       (if (looking-at "[ \t]+[^ \t]")
3391           (forward-line 1)
3392         (setq ended t)))))
3393
3394 (defun article-treat-date ()
3395   (article-date-ut gnus-article-date-headers t))
3396
3397 (defun article-date-ut (&optional type highlight date-position)
3398   "Convert DATE date to TYPE in the current article.
3399 The default type is `ut'.  See `gnus-article-date-headers' for
3400 possible values."
3401   (interactive (list 'ut t))
3402   (let* ((case-fold-search t)
3403          (inhibit-read-only t)
3404          (inhibit-point-motion-hooks t)
3405          (first t)
3406          pos date bface eface)
3407     (save-excursion
3408       (save-restriction
3409         (goto-char (point-min))
3410         (when (re-search-forward "^Date:" nil t)
3411           (setq bface (get-text-property (point-at-bol) 'face)
3412                 eface (get-text-property (1- (point-at-eol)) 'face)))
3413         (goto-char (point-min))
3414         ;; Delete any old Date headers.
3415         (if date-position
3416             (progn
3417               (goto-char date-position)
3418               (setq date (get-text-property (point) 'original-date))
3419               (delete-region (point)
3420                              (progn
3421                                (gnus-article-forward-header)
3422                                (point)))
3423               (article-transform-date date type bface eface))
3424           (while (re-search-forward "^Date:" nil t)
3425             (setq date (get-text-property (match-beginning 0) 'original-date))
3426             (delete-region (point-at-bol) (progn
3427                                             (gnus-article-forward-header)
3428                                             (point))))
3429           (when date
3430             (article-transform-date date type bface eface)))))))
3431
3432 (defun article-transform-date (date type bface eface)
3433   (dolist (this-type (cond
3434                       ((null type)
3435                        (list 'ut))
3436                       ((atom type)
3437                        (list type))
3438                       (t
3439                        type)))
3440     (insert (article-make-date-line date (or this-type 'ut)) "\n")
3441     (forward-line -1)
3442     (beginning-of-line)
3443     (put-text-property (point) (1+ (point))
3444                        'original-date date)
3445     (put-text-property (point) (1+ (point))
3446                        'gnus-date-type this-type)
3447     ;; Do highlighting.
3448     (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3449       (put-text-property (match-beginning 1) (1+ (match-end 1))
3450                          'face bface)
3451       (put-text-property (match-beginning 2) (match-end 2)
3452                          'face eface))
3453     (forward-line 1)))
3454
3455 (defun article-make-date-line (date type)
3456   "Return a DATE line of TYPE."
3457   (unless (memq type '(local ut original user-defined iso8601 lapsed english
3458                              combined-lapsed))
3459     (error "Unknown conversion type: %s" type))
3460   (condition-case ()
3461       (let ((time (date-to-time date)))
3462         (cond
3463          ;; Convert to the local timezone.
3464          ((eq type 'local)
3465           (concat "Date: " (message-make-date time)))
3466          ;; Convert to Universal Time.
3467          ((eq type 'ut)
3468           (concat "Date: "
3469                   (substring
3470                    (message-make-date
3471                     (let* ((e (parse-time-string date))
3472                            (tm (apply 'encode-time e))
3473                            (ms (car tm))
3474                            (ls (- (cadr tm) (car (current-time-zone time)))))
3475                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3476                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3477                             (t (list ms ls)))))
3478                    0 -5)
3479                   "UT"))
3480          ;; Get the original date from the article.
3481          ((eq type 'original)
3482           (concat "Date: " (if (string-match "\n+$" date)
3483                                (substring date 0 (match-beginning 0))
3484                              date)))
3485          ;; Let the user define the format.
3486          ((eq type 'user-defined)
3487           (let ((format (or (condition-case nil
3488                                 (with-current-buffer gnus-summary-buffer
3489                                   gnus-article-time-format)
3490                               (error nil))
3491                             gnus-article-time-format)))
3492             (if (functionp format)
3493                 (funcall format time)
3494               (concat "Date: " (format-time-string format time)))))
3495          ;; ISO 8601.
3496          ((eq type 'iso8601)
3497           (let ((tz (car (current-time-zone time))))
3498             (concat
3499              "Date: "
3500              (format-time-string "%Y%m%dT%H%M%S" time)
3501              (format "%s%02d%02d"
3502                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3503                      (/ (% (abs tz) 3600) 60)))))
3504          ;; Do a lapsed format.
3505          ((eq type 'lapsed)
3506           (concat "Date: " (article-lapsed-string time)))
3507          ;; A combined date/lapsed format.
3508          ((eq type 'combined-lapsed)
3509           (let ((date-string (article-make-date-line date 'original))
3510                 (segments 3)
3511                 lapsed-string)
3512             (while (and
3513                     (setq lapsed-string
3514                           (concat " (" (article-lapsed-string time segments) ")"))
3515                     (> (+ (length date-string)
3516                           (length lapsed-string))
3517                        (+ fill-column 6))
3518                     (> segments 0))
3519               (setq segments (1- segments)))
3520             (if (> segments 0)
3521                 (concat date-string lapsed-string)
3522               date-string)))
3523          ;; Display the date in proper English
3524          ((eq type 'english)
3525           (let ((dtime (decode-time time)))
3526             (concat
3527              "Date: the "
3528              (number-to-string (nth 3 dtime))
3529              (let ((digit (% (nth 3 dtime) 10)))
3530                (cond
3531                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3532                 ((= digit 1) "st")
3533                 ((= digit 2) "nd")
3534                 ((= digit 3) "rd")
3535                 (t "th")))
3536              " of "
3537              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3538              " "
3539              (number-to-string (nth 5 dtime))
3540              " at "
3541              (format "%02d" (nth 2 dtime))
3542              ":"
3543              (format "%02d" (nth 1 dtime)))))))
3544     (foo
3545      (format "Date: %s (from Gnus)" date))))
3546
3547 (defun article-lapsed-string (time &optional max-segments)
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          (segments 0)
3557          num prev)
3558     (unless max-segments
3559       (setq max-segments (length article-time-units)))
3560     (cond
3561      ((null real-time)
3562       "Unknown")
3563      ((zerop sec)
3564       "Now")
3565      (t
3566       (concat
3567        ;; This is a bit convoluted, but basically we go
3568        ;; through the time units for years, weeks, etc,
3569        ;; and divide things to see whether that results
3570        ;; in positive answers.
3571        (mapconcat
3572         (lambda (unit)
3573           (if (or (zerop (setq num (ffloor (/ sec (cdr unit)))))
3574                   (>= segments max-segments))
3575               ;; The (remaining) seconds are too few to
3576               ;; be divided into this time unit.
3577               ""
3578             ;; It's big enough, so we output it.
3579             (setq sec (- sec (* num (cdr unit))))
3580             (prog1
3581                 (concat (if prev ", " "") (int-to-string
3582                                            (floor num))
3583                         " " (symbol-name (car unit))
3584                         (if (> num 1) "s" ""))
3585               (setq prev t
3586                     segments (1+ segments)))))
3587         article-time-units "")
3588        ;; If dates are odd, then it might appear like the
3589        ;; article was sent in the future.
3590        (if (> real-sec 0)
3591            " ago"
3592          " in the future"))))))
3593
3594 (defun article-date-local (&optional highlight)
3595   "Convert the current article date to the local timezone."
3596   (interactive (list t))
3597   (article-date-ut 'local highlight))
3598
3599 (defun article-date-english (&optional highlight)
3600   "Convert the current article date to something that is proper English."
3601   (interactive (list t))
3602   (article-date-ut 'english highlight))
3603
3604 (defun article-date-original (&optional highlight)
3605   "Convert the current article date to what it was originally.
3606 This is only useful if you have used some other date conversion
3607 function and want to see what the date was before converting."
3608   (interactive (list t))
3609   (article-date-ut 'original highlight))
3610
3611 (defun article-date-lapsed (&optional highlight)
3612   "Convert the current article date to time lapsed since it was sent."
3613   (interactive (list t))
3614   (article-date-ut 'lapsed highlight))
3615
3616 (defun article-date-combined-lapsed (&optional highlight)
3617   "Convert the current article date to time lapsed since it was sent."
3618   (interactive (list t))
3619   (article-date-ut 'combined-lapsed highlight))
3620
3621 (defun article-update-date-lapsed ()
3622   "Function to be run from a timer to update the lapsed time line."
3623   (save-match-data
3624     (let ((buffer (current-buffer)))
3625       (ignore-errors
3626         (walk-windows
3627          (lambda (w)
3628            (set-buffer (window-buffer w))
3629            (when (eq major-mode 'gnus-article-mode)
3630              (let ((old-line (count-lines (point-min) (point)))
3631                    (old-column (- (point) (line-beginning-position)))
3632                    (window-start
3633                     (window-start (get-buffer-window (current-buffer)))))
3634                (goto-char (point-min))
3635                (while (re-search-forward "^Date:" nil t)
3636                  (let ((type (get-text-property (match-beginning 0)
3637                                                 'gnus-date-type)))
3638                    (when (memq type '(lapsed combined-lapsed user-format))
3639                      (unless (= window-start
3640                                 (save-excursion
3641                                   (forward-line 1)
3642                                   (point)))
3643                        (setq window-start nil))
3644                      (save-excursion
3645                        (article-date-ut type t (match-beginning 0)))
3646                      (forward-line 1)
3647                      (when window-start
3648                        (set-window-start (get-buffer-window (current-buffer))
3649                                          (point))))))
3650                (goto-char (point-min))
3651                (when (> old-column 0)
3652                  (setq old-line (1- old-line)))
3653                (forward-line old-line)
3654                (end-of-line)
3655                (when (> (current-column) old-column)
3656                  (beginning-of-line)
3657                  (forward-char old-column)))))
3658          nil 'visible))
3659       (set-buffer buffer))))
3660
3661 (defun gnus-start-date-timer (&optional n)
3662   "Start a timer to update the Date headers in the article buffers.
3663 The numerical prefix says how frequently (in seconds) the function
3664 is to run."
3665   (interactive "p")
3666   (unless n
3667     (setq n 1))
3668   (gnus-stop-date-timer)
3669   (setq article-lapsed-timer
3670         (run-at-time 1 n 'article-update-date-lapsed)))
3671
3672 (defun gnus-stop-date-timer ()
3673   "Stop the Date timer."
3674   (interactive)
3675   (when article-lapsed-timer
3676     (nnheader-cancel-timer article-lapsed-timer)
3677     (setq article-lapsed-timer nil)))
3678
3679 (defun article-date-user (&optional highlight)
3680   "Convert the current article date to the user-defined format.
3681 This format is defined by the `gnus-article-time-format' variable."
3682   (interactive (list t))
3683   (article-date-ut 'user highlight))
3684
3685 (defun article-date-iso8601 (&optional highlight)
3686   "Convert the current article date to ISO8601."
3687   (interactive (list t))
3688   (article-date-ut 'iso8601 highlight))
3689
3690 (defmacro gnus-article-save-original-date (&rest forms)
3691   "Save the original date as a text property and evaluate FORMS."
3692   `(let* ((case-fold-search t)
3693           (start (progn
3694                    (goto-char (point-min))
3695                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3696                               (not (bolp)))
3697                      (match-end 0))))
3698           (date (when (and start
3699                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3700                                               nil t))
3701                   (buffer-substring-no-properties start
3702                                                   (match-beginning 0)))))
3703      (goto-char (point-max))
3704      (skip-chars-backward "\n")
3705      (put-text-property (point-min) (point) 'original-date date)
3706      ,@forms
3707      (goto-char (point-max))
3708      (skip-chars-backward "\n")
3709      (put-text-property (point-min) (point) 'original-date date)))
3710
3711 ;; (defun article-show-all ()
3712 ;;   "Show all hidden text in the article buffer."
3713 ;;   (interactive)
3714 ;;   (save-excursion
3715 ;;     (let ((inhibit-read-only t))
3716 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3717
3718 (defun article-remove-leading-whitespace ()
3719   "Remove excessive whitespace from all headers."
3720   (interactive)
3721   (save-excursion
3722     (save-restriction
3723       (let ((inhibit-read-only t))
3724         (article-narrow-to-head)
3725         (goto-char (point-min))
3726         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3727           (delete-region (match-beginning 1) (match-end 1)))))))
3728
3729 (defun article-emphasize (&optional arg)
3730   "Emphasize text according to `gnus-emphasis-alist'."
3731   (interactive (gnus-article-hidden-arg))
3732   (unless (gnus-article-check-hidden-text 'emphasis arg)
3733     (save-excursion
3734       (let ((alist (or
3735                     (condition-case nil
3736                         (with-current-buffer gnus-summary-buffer
3737                           gnus-article-emphasis-alist)
3738                       (error))
3739                     gnus-emphasis-alist))
3740             (inhibit-read-only t)
3741             (props (append '(article-type emphasis)
3742                            gnus-hidden-properties))
3743             regexp elem beg invisible visible face)
3744         (article-goto-body)
3745         (setq beg (point))
3746         (while (setq elem (pop alist))
3747           (goto-char beg)
3748           (setq regexp (car elem)
3749                 invisible (nth 1 elem)
3750                 visible (nth 2 elem)
3751                 face (nth 3 elem))
3752           (while (re-search-forward regexp nil t)
3753             (when (and (match-beginning visible) (match-beginning invisible))
3754               (gnus-article-hide-text
3755                (match-beginning invisible) (match-end invisible) props)
3756               (gnus-article-unhide-text-type
3757                (match-beginning visible) (match-end visible) 'emphasis)
3758               (gnus-put-overlay-excluding-newlines
3759                (match-beginning visible) (match-end visible) 'face face)
3760               (gnus-add-wash-type 'emphasis)
3761               (goto-char (match-end invisible)))))))))
3762
3763 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3764   "Setup newsgroup emphasis alist."
3765   (unless gnus-article-emphasis-alist
3766     (let ((name (and gnus-newsgroup-name
3767                      (gnus-group-real-name gnus-newsgroup-name))))
3768       (make-local-variable 'gnus-article-emphasis-alist)
3769       (setq gnus-article-emphasis-alist
3770             (nconc
3771              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3772                (while (setq elem (pop alist))
3773                  (when (and name (string-match (car elem) name))
3774                    (setq alist nil
3775                          highlight (copy-sequence (cdr elem)))))
3776                highlight)
3777              (copy-sequence highlight-words)
3778              (if gnus-newsgroup-name
3779                  (copy-sequence (gnus-group-find-parameter
3780                                  gnus-newsgroup-name 'highlight-words t)))
3781              gnus-emphasis-alist)))))
3782
3783 (defvar gnus-summary-article-menu)
3784 (defvar gnus-summary-post-menu)
3785
3786 ;;; Saving functions.
3787
3788 (defun gnus-article-save (save-buffer file &optional num)
3789   "Save the currently selected article."
3790   (when (or (get gnus-default-article-saver :headers)
3791             (not gnus-save-all-headers))
3792     ;; Remove headers according to `gnus-saved-headers' or the value
3793     ;; of the `:headers' property that the saver function might have.
3794     (let ((gnus-visible-headers
3795            (or (symbol-value (get gnus-default-article-saver :headers))
3796                gnus-saved-headers gnus-visible-headers))
3797           ;; Ignore group parameter.  See `article-hide-headers'.
3798           (gnus-summary-buffer nil))
3799       (with-current-buffer save-buffer
3800         (article-hide-headers 1 t))))
3801   (save-window-excursion
3802     (if (not gnus-default-article-saver)
3803         (error "No default saver is defined")
3804       ;; !!! Magic!  The saving functions all save
3805       ;; `gnus-save-article-buffer' (or so they think), but we
3806       ;; bind that variable to our save-buffer.
3807       (set-buffer gnus-article-buffer)
3808       (let* ((gnus-save-article-buffer save-buffer)
3809              (filename
3810               (cond
3811                ((not gnus-prompt-before-saving) 'default)
3812                ((eq gnus-prompt-before-saving 'always) nil)
3813                (t file)))
3814              (gnus-number-of-articles-to-be-saved
3815               (when (eq gnus-prompt-before-saving t)
3816                 num)))                  ; Magic
3817         (set-buffer gnus-article-current-summary)
3818         (funcall gnus-default-article-saver filename)))))
3819
3820 (defun gnus-read-save-file-name (prompt &optional filename
3821                                         function group headers variable
3822                                         dir-var)
3823   (let ((default-name
3824           (funcall function group headers (symbol-value variable)))
3825         result)
3826     (setq result
3827           (expand-file-name
3828            (cond
3829             ((eq filename 'default)
3830              default-name)
3831             ((eq filename t)
3832              default-name)
3833             (filename filename)
3834             (t
3835              (when (symbol-value dir-var)
3836                (setq default-name (expand-file-name
3837                                    (file-name-nondirectory default-name)
3838                                    (symbol-value dir-var))))
3839              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3840                     (prompt
3841                      (format prompt
3842                              (if (and gnus-number-of-articles-to-be-saved
3843                                       (> gnus-number-of-articles-to-be-saved 1))
3844                                  (format "these %d articles"
3845                                          gnus-number-of-articles-to-be-saved)
3846                                "this article")))
3847                     (file
3848                      ;; Let the split methods have their say.
3849                      (cond
3850                       ;; No split name was found.
3851                       ((null split-name)
3852                        (read-file-name
3853                         (concat prompt " (default "
3854                                 (file-name-nondirectory default-name) "): ")
3855                         (file-name-directory default-name)
3856                         default-name))
3857                       ;; A single group name is returned.
3858                       ((stringp split-name)
3859                        (setq default-name
3860                              (funcall function split-name headers
3861                                       (symbol-value variable)))
3862                        (read-file-name
3863                         (concat prompt " (default "
3864                                 (file-name-nondirectory default-name) "): ")
3865                         (file-name-directory default-name)
3866                         default-name))
3867                       ;; A single split name was found
3868                       ((= 1 (length split-name))
3869                        (let* ((name (expand-file-name
3870                                      (car split-name)
3871                                      gnus-article-save-directory))
3872                               (dir (cond ((file-directory-p name)
3873                                           (file-name-as-directory name))
3874                                          ((file-exists-p name) name)
3875                                          (t gnus-article-save-directory))))
3876                          (read-file-name
3877                           (concat prompt " (default " name "): ")
3878                           dir name)))
3879                       ;; A list of splits was found.
3880                       (t
3881                        (setq split-name (nreverse split-name))
3882                        (let (result)
3883                          (let ((file-name-history
3884                                 (nconc split-name file-name-history)))
3885                            (setq result
3886                                  (expand-file-name
3887                                   (read-file-name
3888                                    (concat prompt " (`M-p' for defaults): ")
3889                                    gnus-article-save-directory
3890                                    (car split-name))
3891                                   gnus-article-save-directory)))
3892                          (car (push result file-name-history)))))))
3893                ;; Create the directory.
3894                (gnus-make-directory (file-name-directory file))
3895                ;; If we have read a directory, we append the default file name.
3896                (when (file-directory-p file)
3897                  (setq file (expand-file-name (file-name-nondirectory
3898                                                default-name)
3899                                               (file-name-as-directory file))))
3900                ;; Possibly translate some characters.
3901                (nnheader-translate-file-chars file))))))
3902     (gnus-make-directory (file-name-directory result))
3903     (when variable
3904       (set variable result))
3905     (when dir-var
3906       (set dir-var (file-name-directory result)))
3907     result))
3908
3909 (defun gnus-article-archive-name (group)
3910   "Return the first instance of an \"Archive-name\" in the current buffer."
3911   (let ((case-fold-search t))
3912     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3913       (nnheader-concat gnus-article-save-directory
3914                        (match-string 1)))))
3915
3916 (defun gnus-article-nndoc-name (group)
3917   "If GROUP is an nndoc group, return the name of the parent group."
3918   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3919     (gnus-group-get-parameter group 'save-article-group)))
3920
3921 (defun gnus-summary-save-in-rmail (&optional filename)
3922   "Append this article to Rmail file.
3923 Optional argument FILENAME specifies file name.
3924 Directory to save to is default to `gnus-article-save-directory'."
3925   (setq filename (gnus-read-save-file-name
3926                   "Save %s in rmail file" filename
3927                   gnus-rmail-save-name gnus-newsgroup-name
3928                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3929   (with-current-buffer gnus-save-article-buffer
3930     (save-excursion
3931       (save-restriction
3932         (widen)
3933         ;; Note that unlike gnus-summary-save-in-mail, there is no
3934         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3935         ;; not use Babyl.
3936         (gnus-output-to-rmail filename))))
3937   filename)
3938
3939 (defun gnus-summary-save-in-mail (&optional filename)
3940   "Append this article to Unix mail file.
3941 Optional argument FILENAME specifies file name.
3942 Directory to save to is default to `gnus-article-save-directory'."
3943   (setq filename (gnus-read-save-file-name
3944                   "Save %s in Unix mail file" filename
3945                   gnus-mail-save-name gnus-newsgroup-name
3946                   gnus-current-headers 'gnus-newsgroup-last-mail))
3947   (with-current-buffer gnus-save-article-buffer
3948     (save-excursion
3949       (save-restriction
3950         (widen)
3951         (if (and (file-readable-p filename)
3952                  (file-regular-p filename)
3953                  (mail-file-babyl-p filename))
3954             (gnus-output-to-rmail filename)
3955           (gnus-output-to-mail filename)))))
3956   filename)
3957
3958 (put 'gnus-summary-save-in-file :decode t)
3959 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
3960 (defun gnus-summary-save-in-file (&optional filename overwrite)
3961   "Append this article to file.
3962 Optional argument FILENAME specifies file name.
3963 Directory to save to is default to `gnus-article-save-directory'."
3964   (setq filename (gnus-read-save-file-name
3965                   "Save %s in file" filename
3966                   gnus-file-save-name gnus-newsgroup-name
3967                   gnus-current-headers 'gnus-newsgroup-last-file))
3968   (with-current-buffer gnus-save-article-buffer
3969     (save-excursion
3970       (save-restriction
3971         (widen)
3972         (when (and overwrite
3973                    (file-exists-p filename))
3974           (delete-file filename))
3975         (gnus-output-to-file filename))))
3976   filename)
3977
3978 (put 'gnus-summary-write-to-file :decode t)
3979 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
3980 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
3981 (defun gnus-summary-write-to-file (&optional filename)
3982   "Write this article to a file, overwriting it if the file exists.
3983 Optional argument FILENAME specifies file name.
3984 The directory to save in defaults to `gnus-article-save-directory'."
3985   (setq filename (gnus-read-save-file-name
3986                   "Save %s in file" filename
3987                   gnus-file-save-name gnus-newsgroup-name
3988                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
3989   (gnus-summary-save-in-file filename t))
3990
3991 (put 'gnus-summary-save-body-in-file :decode t)
3992 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
3993   "Append this article body to a file.
3994 Optional argument FILENAME specifies file name.
3995 The directory to save in defaults to `gnus-article-save-directory'."
3996   (setq filename (gnus-read-save-file-name
3997                   "Save %s body in file" filename
3998                   gnus-file-save-name gnus-newsgroup-name
3999                   gnus-current-headers 'gnus-newsgroup-last-file))
4000   (with-current-buffer gnus-save-article-buffer
4001     (save-excursion
4002       (save-restriction
4003         (widen)
4004         (when (article-goto-body)
4005           (narrow-to-region (point) (point-max)))
4006         (when (and overwrite
4007                    (file-exists-p filename))
4008           (delete-file filename))
4009         (gnus-output-to-file filename))))
4010   filename)
4011
4012 (put 'gnus-summary-write-body-to-file :decode t)
4013 (put 'gnus-summary-write-body-to-file
4014      :function 'gnus-summary-save-body-in-file)
4015 (defun gnus-summary-write-body-to-file (&optional filename)
4016   "Write this article body to a file, overwriting it if the file exists.
4017 Optional argument FILENAME specifies file name.
4018 The directory to save in defaults to `gnus-article-save-directory'."
4019   (setq filename (gnus-read-save-file-name
4020                   "Save %s body in file" filename
4021                   gnus-file-save-name gnus-newsgroup-name
4022                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4023   (gnus-summary-save-body-in-file filename t))
4024
4025 (put 'gnus-summary-save-in-pipe :decode t)
4026 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4027 (defun gnus-summary-save-in-pipe (&optional command raw)
4028   "Pipe this article to subprocess COMMAND.
4029 Valid values for COMMAND include:
4030   a string
4031     The executable command name and possibly arguments.
4032   nil
4033     You will be prompted for the command in the minibuffer.
4034   the symbol `default'
4035     It will be replaced with the command which the variable
4036     `gnus-summary-pipe-output-default-command' holds or the command
4037     last used for saving.
4038 Non-nil value for RAW overrides `:decode' and `:headers' properties
4039 and the raw article including all headers will be piped."
4040   (let ((article (gnus-summary-article-number))
4041         (decode (unless raw
4042                   (get 'gnus-summary-save-in-pipe :decode)))
4043         save-buffer default)
4044     (if article
4045         (if (vectorp (gnus-summary-article-header article))
4046             (save-current-buffer
4047               (gnus-summary-select-article decode decode nil article)
4048               (insert-buffer-substring
4049                (prog1
4050                    (if decode
4051                        gnus-article-buffer
4052                      gnus-original-article-buffer)
4053                  (setq save-buffer
4054                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4055               ;; Remove unwanted headers.
4056               (when (and (not raw)
4057                          (or (get 'gnus-summary-save-in-pipe :headers)
4058                              (not gnus-save-all-headers)))
4059                 (let ((gnus-visible-headers
4060                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4061                                               :headers))
4062                            gnus-saved-headers gnus-visible-headers))
4063                       (gnus-summary-buffer nil))
4064                   (article-hide-headers 1 t))))
4065           (error "%d is not a real article" article))
4066       (error "No article to pipe"))
4067     (setq default (or gnus-summary-pipe-output-default-command
4068                       gnus-last-shell-command))
4069     (unless (stringp command)
4070       (setq command
4071             (if (and (eq command 'default) default)
4072                 default
4073               (gnus-read-shell-command "Shell command on this article: "
4074                                        default))))
4075     (when (string-equal command "")
4076       (if default
4077           (setq command default)
4078         (error "A command is required")))
4079     (with-current-buffer save-buffer
4080       (save-restriction
4081         (widen)
4082         (shell-command-on-region (point-min) (point-max) command nil)))
4083     (gnus-kill-buffer save-buffer))
4084   (setq gnus-summary-pipe-output-default-command command))
4085
4086 (defun gnus-summary-pipe-to-muttprint (&optional command)
4087   "Pipe this article to muttprint."
4088   (unless (stringp command)
4089     (setq command (read-string
4090                    "Print using command: " gnus-summary-muttprint-program
4091                    nil gnus-summary-muttprint-program)))
4092   (let ((gnus-summary-pipe-output-default-command
4093          gnus-summary-pipe-output-default-command))
4094     (gnus-summary-save-in-pipe command))
4095   (setq gnus-summary-muttprint-program command))
4096
4097 ;;; Article file names when saving.
4098
4099 (defun gnus-capitalize-newsgroup (newsgroup)
4100   "Capitalize NEWSGROUP name."
4101   (when (not (zerop (length newsgroup)))
4102     (concat (char-to-string (upcase (aref newsgroup 0)))
4103             (substring newsgroup 1))))
4104
4105 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4106   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4107 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4108 Otherwise, it is like ~/News/news/group/num."
4109   (let ((default
4110           (expand-file-name
4111            (concat (if (gnus-use-long-file-name 'not-save)
4112                        (gnus-capitalize-newsgroup newsgroup)
4113                      (gnus-newsgroup-directory-form newsgroup))
4114                    "/" (int-to-string (mail-header-number headers)))
4115            gnus-article-save-directory)))
4116     (if (and last-file
4117              (string-equal (file-name-directory default)
4118                            (file-name-directory last-file))
4119              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4120         default
4121       (or last-file default))))
4122
4123 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4124   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4125 If variable `gnus-use-long-file-name' is non-nil, it is
4126 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4127   (let ((default
4128           (expand-file-name
4129            (concat (if (gnus-use-long-file-name 'not-save)
4130                        newsgroup
4131                      (gnus-newsgroup-directory-form newsgroup))
4132                    "/" (int-to-string (mail-header-number headers)))
4133            gnus-article-save-directory)))
4134     (if (and last-file
4135              (string-equal (file-name-directory default)
4136                            (file-name-directory last-file))
4137              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4138         default
4139       (or last-file default))))
4140
4141 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4142   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4143 If variable `gnus-use-long-file-name' is non-nil, it is
4144 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4145   (or last-file
4146       (expand-file-name
4147        (if (gnus-use-long-file-name 'not-save)
4148            newsgroup
4149          (file-relative-name
4150           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4151           default-directory))
4152        gnus-article-save-directory)))
4153
4154 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4155   "Generate file name from sender."
4156   (let ((from (mail-header-from headers)))
4157     (expand-file-name
4158      (if (and from (string-match "\\([^ <]+\\)@" from))
4159          (match-string 1 from)
4160        "nobody")
4161      gnus-article-save-directory)))
4162
4163 (defun article-verify-x-pgp-sig ()
4164   "Verify X-PGP-Sig."
4165   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4166   (interactive)
4167   (if (gnus-buffer-live-p gnus-original-article-buffer)
4168       (let ((sig (with-current-buffer gnus-original-article-buffer
4169                    (gnus-fetch-field "X-PGP-Sig")))
4170             items info headers)
4171         (when (and sig
4172                    mml2015-use
4173                    (mml2015-clear-verify-function))
4174           (with-temp-buffer
4175             (insert-buffer-substring gnus-original-article-buffer)
4176             (setq items (split-string sig))
4177             (message-narrow-to-head)
4178             (let ((inhibit-point-motion-hooks t)
4179                   (case-fold-search t))
4180               ;; Don't verify multiple headers.
4181               (setq headers (mapconcat (lambda (header)
4182                                          (concat header ": "
4183                                                  (mail-fetch-field header)
4184                                                  "\n"))
4185                                        (split-string (nth 1 items) ",") "")))
4186             (delete-region (point-min) (point-max))
4187             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4188             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4189             (insert headers)
4190             (widen)
4191             (forward-line)
4192             (while (not (eobp))
4193               (if (looking-at "^-")
4194                   (insert "- "))
4195               (forward-line))
4196             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4197             (insert "Version: " (car items) "\n\n")
4198             (insert (mapconcat 'identity (cddr items) "\n"))
4199             (insert "\n-----END PGP SIGNATURE-----\n")
4200             (let ((mm-security-handle (list (format "multipart/signed"))))
4201               (mml2015-clean-buffer)
4202               (let ((coding-system-for-write (or gnus-newsgroup-charset
4203                                                  'iso-8859-1)))
4204                 (funcall (mml2015-clear-verify-function)))
4205               (setq info
4206                     (or (mm-handle-multipart-ctl-parameter
4207                          mm-security-handle 'gnus-details)
4208                         (mm-handle-multipart-ctl-parameter
4209                          mm-security-handle 'gnus-info)))))
4210           (when info
4211             (let ((inhibit-read-only t) bface eface)
4212               (save-restriction
4213                 (message-narrow-to-head)
4214                 (goto-char (point-max))
4215                 (forward-line -1)
4216                 (setq bface (get-text-property (point-at-bol) 'face)
4217                       eface (get-text-property (1- (point-at-eol)) 'face))
4218                 (message-remove-header "X-Gnus-PGP-Verify")
4219                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4220                     (forward-line)
4221                   (goto-char (point-max)))
4222                 (narrow-to-region (point) (point))
4223                 (insert "X-Gnus-PGP-Verify: " info "\n")
4224                 (goto-char (point-min))
4225                 (forward-line)
4226                 (while (not (eobp))
4227                   (if (not (looking-at "^[ \t]"))
4228                       (insert " "))
4229                   (forward-line))
4230                 ;; Do highlighting.
4231                 (goto-char (point-min))
4232                 (when (looking-at "\\([^:]+\\): *")
4233                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4234                                      'face bface)
4235                   (put-text-property (match-end 0) (point-max)
4236                                      'face eface)))))))))
4237
4238 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4239
4240 (defun article-verify-cancel-lock ()
4241   "Verify Cancel-Lock header."
4242   (interactive)
4243   (if (gnus-buffer-live-p gnus-original-article-buffer)
4244       (canlock-verify gnus-original-article-buffer)))
4245
4246 (eval-and-compile
4247   (mapc
4248    (lambda (func)
4249      (let (afunc gfunc)
4250        (if (consp func)
4251            (setq afunc (car func)
4252                  gfunc (cdr func))
4253          (setq afunc func
4254                gfunc (intern (format "gnus-%s" func))))
4255        (defalias gfunc
4256          (when (fboundp afunc)
4257            `(lambda (&optional interactive &rest args)
4258               ,(documentation afunc t)
4259               (interactive (list t))
4260               (with-current-buffer gnus-article-buffer
4261                 (if interactive
4262                     (call-interactively ',afunc)
4263                   (apply ',afunc args))))))))
4264    '(article-hide-headers
4265      article-verify-x-pgp-sig
4266      article-verify-cancel-lock
4267      article-hide-boring-headers
4268      article-treat-overstrike
4269      article-treat-ansi-sequences
4270      article-fill-long-lines
4271      article-capitalize-sentences
4272      article-remove-cr
4273      article-remove-leading-whitespace
4274      article-display-x-face
4275      article-display-face
4276      article-de-quoted-unreadable
4277      article-de-base64-unreadable
4278      article-decode-HZ
4279      article-wash-html
4280      article-unsplit-urls
4281      article-hide-list-identifiers
4282      article-strip-banner
4283      article-babel
4284      article-hide-pem
4285      article-hide-signature
4286      article-strip-headers-in-body
4287      article-remove-trailing-blank-lines
4288      article-strip-leading-blank-lines
4289      article-strip-multiple-blank-lines
4290      article-strip-leading-space
4291      article-strip-trailing-space
4292      article-strip-blank-lines
4293      article-strip-all-blank-lines
4294      article-date-local
4295      article-date-english
4296      article-date-iso8601
4297      article-date-original
4298      article-treat-date
4299      article-date-ut
4300      article-decode-mime-words
4301      article-decode-charset
4302      article-decode-encoded-words
4303      article-date-user
4304      article-date-lapsed
4305      article-date-combined-lapsed
4306      article-emphasize
4307      article-treat-dumbquotes
4308      article-treat-non-ascii
4309      article-normalize-headers
4310      ;;(article-show-all . gnus-article-show-all-headers)
4311      )))
4312 \f
4313 ;;;
4314 ;;; Gnus article mode
4315 ;;;
4316
4317 (put 'gnus-article-mode 'mode-class 'special)
4318
4319 (set-keymap-parent gnus-article-mode-map widget-keymap)
4320
4321 (gnus-define-keys gnus-article-mode-map
4322   " " gnus-article-goto-next-page
4323   "\177" gnus-article-goto-prev-page
4324   [delete] gnus-article-goto-prev-page
4325   [backspace] gnus-article-goto-prev-page
4326   "\C-c^" gnus-article-refer-article
4327   "h" gnus-article-show-summary
4328   "s" gnus-article-show-summary
4329   "\C-c\C-m" gnus-article-mail
4330   "?" gnus-article-describe-briefly
4331   "<" beginning-of-buffer
4332   ">" end-of-buffer
4333   "\C-c\C-i" gnus-info-find-node
4334   "\C-c\C-b" gnus-bug
4335   "R" gnus-article-reply-with-original
4336   "F" gnus-article-followup-with-original
4337   "\C-hk" gnus-article-describe-key
4338   "\C-hc" gnus-article-describe-key-briefly
4339   "\C-hb" gnus-article-describe-bindings
4340
4341   "e" gnus-article-read-summary-keys
4342   "\C-d" gnus-article-read-summary-keys
4343   "\M-*" gnus-article-read-summary-keys
4344   "\M-#" gnus-article-read-summary-keys
4345   "\M-^" gnus-article-read-summary-keys
4346   "\M-g" gnus-article-read-summary-keys)
4347
4348 (substitute-key-definition
4349  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4350
4351 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4352   "W" gnus-article-wide-reply-with-original)
4353 (if (featurep 'xemacs)
4354     (set-keymap-default-binding gnus-article-send-map
4355                                 'gnus-article-read-summary-send-keys)
4356   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4357
4358 (defun gnus-article-make-menu-bar ()
4359   (unless (boundp 'gnus-article-commands-menu)
4360     (gnus-summary-make-menu-bar))
4361   (unless (boundp 'gnus-article-article-menu)
4362     (easy-menu-define
4363      gnus-article-article-menu gnus-article-mode-map ""
4364      '("Article"
4365        ["Scroll forwards" gnus-article-goto-next-page t]
4366        ["Scroll backwards" gnus-article-goto-prev-page t]
4367        ["Show summary" gnus-article-show-summary t]
4368        ["Fetch Message-ID at point" gnus-article-refer-article t]
4369        ["Mail to address at point" gnus-article-mail t]
4370        ["Send a bug report" gnus-bug t]))
4371
4372     (easy-menu-define
4373      gnus-article-treatment-menu gnus-article-mode-map ""
4374      ;; Fixme: this should use :active (and maybe :visible).
4375      '("Treatment"
4376        ["Hide headers" gnus-article-hide-headers t]
4377        ["Hide signature" gnus-article-hide-signature t]
4378        ["Hide citation" gnus-article-hide-citation t]
4379        ["Treat overstrike" gnus-article-treat-overstrike t]
4380        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4381        ["Remove carriage return" gnus-article-remove-cr t]
4382        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4383        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4384        ["Remove base64" gnus-article-de-base64-unreadable t]
4385        ["Treat html" gnus-article-wash-html t]
4386        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4387        ["Decode HZ" gnus-article-decode-HZ t]))
4388
4389     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4390
4391     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4392
4393     (gnus-run-hooks 'gnus-article-menu-hook)))
4394
4395 (defvar bookmark-make-record-function)
4396
4397 (defun gnus-article-mode ()
4398   "Major mode for displaying an article.
4399
4400 All normal editing commands are switched off.
4401
4402 The following commands are available in addition to all summary mode
4403 commands:
4404 \\<gnus-article-mode-map>
4405 \\[gnus-article-next-page]\t Scroll the article one page forwards
4406 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4407 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4408 \\[gnus-article-show-summary]\t Display the summary buffer
4409 \\[gnus-article-mail]\t Send a reply to the address near point
4410 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4411 \\[gnus-info-find-node]\t Go to the Gnus info node"
4412   (interactive)
4413   (kill-all-local-variables)
4414   (gnus-simplify-mode-line)
4415   (setq mode-name "Article")
4416   (setq major-mode 'gnus-article-mode)
4417   (make-local-variable 'minor-mode-alist)
4418   (use-local-map gnus-article-mode-map)
4419   (when (gnus-visual-p 'article-menu 'menu)
4420     (gnus-article-make-menu-bar)
4421     (when gnus-summary-tool-bar-map
4422       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4423   (gnus-update-format-specifications nil 'article-mode)
4424   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4425   (set (make-local-variable 'gnus-page-broken) nil)
4426   (make-local-variable 'gnus-article-current-summary)
4427   (make-local-variable 'gnus-article-mime-handles)
4428   (make-local-variable 'gnus-article-decoded-p)
4429   (make-local-variable 'gnus-article-mime-handle-alist)
4430   (make-local-variable 'gnus-article-wash-types)
4431   (make-local-variable 'gnus-article-image-alist)
4432   (make-local-variable 'gnus-article-charset)
4433   (make-local-variable 'gnus-article-ignored-charsets)
4434   (set (make-local-variable 'bookmark-make-record-function)
4435        'gnus-summary-bookmark-make-record)
4436   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4437   ;; face.
4438   (set (make-local-variable 'nobreak-char-display) nil)
4439   (setq cursor-in-non-selected-windows nil)
4440   (gnus-set-default-directory)
4441   (buffer-disable-undo)
4442   (setq buffer-read-only t
4443         show-trailing-whitespace nil)
4444   (set-syntax-table gnus-article-mode-syntax-table)
4445   (mm-enable-multibyte)
4446   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4447
4448 (defun gnus-article-setup-buffer ()
4449   "Initialize the article buffer."
4450   (let* ((name (if gnus-single-article-buffer "*Article*"
4451                  (concat "*Article " gnus-newsgroup-name "*")))
4452          (original
4453           (progn (string-match "\\*Article" name)
4454                  (concat " *Original Article"
4455                          (substring name (match-end 0))))))
4456     (setq gnus-article-buffer name)
4457     (setq gnus-original-article-buffer original)
4458     (setq gnus-article-mime-handle-alist nil)
4459     (with-current-buffer gnus-summary-buffer
4460       ;; This might be a variable local to the summary buffer.
4461       (unless gnus-single-article-buffer
4462         (setq gnus-article-buffer name)
4463         (setq gnus-original-article-buffer original)
4464         (gnus-set-global-variables)))
4465     (gnus-article-setup-highlight-words)
4466     ;; Init original article buffer.
4467     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4468       (mm-enable-multibyte)
4469       (setq major-mode 'gnus-original-article-mode)
4470       (make-local-variable 'gnus-original-article))
4471     (if (and (get-buffer name)
4472              (with-current-buffer name
4473                (if gnus-article-edit-mode
4474                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4475                        (progn
4476                          (set-buffer-modified-p nil)
4477                          (gnus-kill-buffer name)
4478                          (message "")
4479                          nil)
4480                      (error "Action aborted"))
4481                  t)))
4482         (with-current-buffer name
4483           (set (make-local-variable 'gnus-article-edit-mode) nil)
4484           (when gnus-article-mime-handles
4485             (mm-destroy-parts gnus-article-mime-handles)
4486             (setq gnus-article-mime-handles nil))
4487           ;; Set it to nil in article-buffer!
4488           (setq gnus-article-mime-handle-alist nil)
4489           (buffer-disable-undo)
4490           (setq buffer-read-only t)
4491           (unless (eq major-mode 'gnus-article-mode)
4492             (gnus-article-mode))
4493           (setq truncate-lines gnus-article-truncate-lines)
4494           (current-buffer))
4495       (with-current-buffer (gnus-get-buffer-create name)
4496         (gnus-article-mode)
4497         (setq truncate-lines gnus-article-truncate-lines)
4498         (make-local-variable 'gnus-summary-buffer)
4499         (setq gnus-summary-buffer
4500               (gnus-summary-buffer-name gnus-newsgroup-name))
4501         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4502         (cond
4503          ((and gnus-article-update-date-headers
4504                (not article-lapsed-timer))
4505           (gnus-start-date-timer gnus-article-update-date-headers))
4506          ((and (not gnus-article-update-date-headers)
4507                article-lapsed-timer)
4508           (gnus-stop-date-timer)))
4509         (current-buffer)))))
4510
4511 ;; Set article window start at LINE, where LINE is the number of lines
4512 ;; from the head of the article.
4513 (defun gnus-article-set-window-start (&optional line)
4514   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4515     (when article-window
4516       (set-window-start
4517        article-window
4518        (with-current-buffer gnus-article-buffer
4519          (goto-char (point-min))
4520          (if (not line)
4521              (point-min)
4522            (gnus-message 6 "Moved to bookmark")
4523            (search-forward "\n\n" nil t)
4524            (forward-line line)
4525            (point)))))))
4526
4527 (defun gnus-article-prepare (article &optional all-headers header)
4528   "Prepare ARTICLE in article mode buffer.
4529 ARTICLE should either be an article number or a Message-ID.
4530 If ARTICLE is an id, HEADER should be the article headers.
4531 If ALL-HEADERS is non-nil, no headers are hidden."
4532   (save-excursion
4533     ;; Make sure we start in a summary buffer.
4534     (unless (eq major-mode 'gnus-summary-mode)
4535       (set-buffer gnus-summary-buffer))
4536     (setq gnus-summary-buffer (current-buffer))
4537     (let* ((gnus-article (if header (mail-header-number header) article))
4538            (summary-buffer (current-buffer))
4539            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4540            (group gnus-newsgroup-name)
4541            result)
4542       (save-excursion
4543         (gnus-article-setup-buffer)
4544         (set-buffer gnus-article-buffer)
4545         ;; Deactivate active regions.
4546         (when (and (boundp 'transient-mark-mode)
4547                    transient-mark-mode)
4548           (setq mark-active nil))
4549         (if (not (setq result (let ((inhibit-read-only t))
4550                                 (gnus-request-article-this-buffer
4551                                  article group))))
4552             ;; There is no such article.
4553             (save-excursion
4554               (when (and (numberp article)
4555                          (not (memq article gnus-newsgroup-sparse)))
4556                 (setq gnus-article-current
4557                       (cons gnus-newsgroup-name article))
4558                 (set-buffer gnus-summary-buffer)
4559                 (setq gnus-current-article article)
4560                 (if (and (memq article gnus-newsgroup-undownloaded)
4561                          (not (gnus-online (gnus-find-method-for-group
4562                                             gnus-newsgroup-name))))
4563                     (progn
4564                       (gnus-summary-set-agent-mark article)
4565                       (message "Message marked for downloading"))
4566                   (gnus-summary-mark-article article gnus-canceled-mark)
4567                   (unless (memq article gnus-newsgroup-sparse)
4568                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4569           (if (or (eq result 'pseudo)
4570                   (eq result 'nneething))
4571               (progn
4572                 (with-current-buffer summary-buffer
4573                   (push article gnus-newsgroup-history)
4574                   (setq gnus-last-article gnus-current-article
4575                         gnus-current-article 0
4576                         gnus-current-headers nil
4577                         gnus-article-current nil)
4578                   (if (eq result 'nneething)
4579                       (gnus-configure-windows 'summary)
4580                     (gnus-configure-windows 'article))
4581                   (gnus-set-global-variables))
4582                 (let ((gnus-article-mime-handle-alist-1
4583                        gnus-article-mime-handle-alist))
4584                   (gnus-set-mode-line 'article)))
4585             ;; The result from the `request' was an actual article -
4586             ;; or at least some text that is now displayed in the
4587             ;; article buffer.
4588             (when (and (numberp article)
4589                        (not (eq article gnus-current-article)))
4590               ;; Seems like a new article has been selected.
4591               ;; `gnus-current-article' must be an article number.
4592               (with-current-buffer summary-buffer
4593                 (push article gnus-newsgroup-history)
4594                 (setq gnus-last-article gnus-current-article
4595                       gnus-current-article article
4596                       gnus-current-headers
4597                       (gnus-summary-article-header gnus-current-article)
4598                       gnus-article-current
4599                       (cons gnus-newsgroup-name gnus-current-article))
4600                 (unless (vectorp gnus-current-headers)
4601                   (setq gnus-current-headers nil))
4602                 (gnus-summary-goto-subject gnus-current-article)
4603                 (when (gnus-summary-show-thread)
4604                   ;; If the summary buffer really was folded, the
4605                   ;; previous goto may not actually have gone to
4606                   ;; the right article, but the thread root instead.
4607                   ;; So we go again.
4608                   (gnus-summary-goto-subject gnus-current-article))
4609                 (gnus-run-hooks 'gnus-mark-article-hook)
4610                 (gnus-set-mode-line 'summary)
4611                 (when (gnus-visual-p 'article-highlight 'highlight)
4612                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4613                 ;; Set the global newsgroup variables here.
4614                 (gnus-set-global-variables)
4615                 (setq gnus-have-all-headers
4616                       (or all-headers gnus-show-all-headers))))
4617             (save-excursion
4618               (gnus-configure-windows 'article))
4619             (when (or (numberp article)
4620                       (stringp article))
4621               (gnus-article-prepare-display)
4622               ;; Do page break.
4623               (goto-char (point-min))
4624               (when gnus-break-pages
4625                 (gnus-narrow-to-page)))
4626             (let ((gnus-article-mime-handle-alist-1
4627                    gnus-article-mime-handle-alist))
4628               (gnus-set-mode-line 'article))
4629             (article-goto-body)
4630             (unless (bobp)
4631               (forward-line -1))
4632             (set-window-point (get-buffer-window (current-buffer)) (point))
4633             (gnus-configure-windows 'article)
4634             t))))))
4635
4636 ;;;###autoload
4637 (defun gnus-article-prepare-display ()
4638   "Make the current buffer look like a nice article."
4639   ;; Hooks for getting information from the article.
4640   ;; This hook must be called before being narrowed.
4641   (let ((gnus-article-buffer (current-buffer))
4642         buffer-read-only
4643         (inhibit-read-only t))
4644     (unless (eq major-mode 'gnus-article-mode)
4645       (gnus-article-mode))
4646     (setq buffer-read-only nil
4647           gnus-article-wash-types nil
4648           gnus-article-image-alist nil)
4649     (gnus-run-hooks 'gnus-tmp-internal-hook)
4650     (when gnus-display-mime-function
4651       (funcall gnus-display-mime-function))
4652     (gnus-run-hooks 'gnus-article-prepare-hook)))
4653
4654 ;;;
4655 ;;; Gnus Sticky Article Mode
4656 ;;;
4657
4658 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4659   "Mode for sticky articles."
4660   ;; Release bindings that won't work.
4661   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4662                              gnus-sticky-article-mode-map)
4663   (substitute-key-definition 'gnus-article-refer-article 'undefined
4664                              gnus-sticky-article-mode-map)
4665   (dolist (k '("e" "h" "s" "F" "R"))
4666     (define-key gnus-sticky-article-mode-map k nil))
4667   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4668   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4669   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4670   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4671
4672 (defun gnus-sticky-article (arg)
4673   "Make the current article sticky.
4674 If a prefix ARG is given, ask for a name for this sticky article buffer."
4675   (interactive "P")
4676   (gnus-summary-show-thread)
4677   (gnus-summary-select-article nil nil 'pseudo)
4678   (let (new-art-buf-name)
4679     (gnus-eval-in-buffer-window gnus-article-buffer
4680       (setq new-art-buf-name
4681             (concat
4682              "*Sticky Article: "
4683              (if arg
4684                  (read-from-minibuffer "Sticky article buffer name: ")
4685                (gnus-with-article-headers
4686                  (gnus-article-goto-header "subject")
4687                  (setq new-art-buf-name
4688                        (buffer-substring-no-properties
4689                         (line-beginning-position) (line-end-position)))
4690                  (goto-char (point-min))
4691                  (gnus-article-goto-header "from")
4692                  (setq new-art-buf-name
4693                        (concat
4694                         new-art-buf-name ", "
4695                         (buffer-substring-no-properties
4696                          (line-beginning-position) (line-end-position))))
4697                  (goto-char (point-min))
4698                  (gnus-article-goto-header "date")
4699                  (setq new-art-buf-name
4700                        (concat
4701                         new-art-buf-name ", "
4702                         (buffer-substring-no-properties
4703                          (line-beginning-position) (line-end-position))))))
4704              "*"))
4705       (if (and (gnus-buffer-live-p new-art-buf-name)
4706                (with-current-buffer new-art-buf-name
4707                  (eq major-mode 'gnus-sticky-article-mode)))
4708           (switch-to-buffer new-art-buf-name)
4709         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4710       (gnus-sticky-article-mode))
4711     (setq gnus-article-buffer new-art-buf-name))
4712   (gnus-summary-recenter)
4713   (gnus-summary-position-point))
4714
4715 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4716   "Kill the given sticky article BUFFER.
4717 If none is given, assume the current buffer and kill it if it has
4718 `gnus-sticky-article-mode'."
4719   (interactive)
4720   (unless buffer
4721     (setq buffer (current-buffer)))
4722   (with-current-buffer buffer
4723     (when (eq major-mode 'gnus-sticky-article-mode)
4724       (gnus-kill-buffer buffer))))
4725
4726 (defun gnus-kill-sticky-article-buffers (arg)
4727   "Kill all sticky article buffers.
4728 If a prefix ARG is given, ask for confirmation."
4729   (interactive "P")
4730   (dolist (buf (gnus-buffers))
4731     (with-current-buffer buf
4732       (when (eq major-mode 'gnus-sticky-article-mode)
4733         (if (not arg)
4734             (gnus-kill-buffer buf)
4735           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4736             (gnus-kill-buffer buf)))))))
4737
4738 ;;;
4739 ;;; Gnus MIME viewing functions
4740 ;;;
4741
4742 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4743   "Format of the MIME buttons.
4744
4745 Valid specifiers include:
4746 %t  The MIME type
4747 %T  MIME type, along with additional info
4748 %n  The `name' parameter
4749 %d  The description, if any
4750 %l  The length of the encoded part
4751 %p  The part identifier number
4752 %e  Dots if the part isn't displayed
4753
4754 General format specifiers can also be used.  See Info node
4755 `(gnus)Formatting Variables'.")
4756
4757 (defvar gnus-mime-button-line-format-alist
4758   '((?t gnus-tmp-type ?s)
4759     (?T gnus-tmp-type-long ?s)
4760     (?n gnus-tmp-name ?s)
4761     (?d gnus-tmp-description ?s)
4762     (?p gnus-tmp-id ?s)
4763     (?l gnus-tmp-length ?d)
4764     (?e gnus-tmp-dots ?s)))
4765
4766 (defvar gnus-mime-button-commands
4767   '((gnus-article-press-button "\r" "Toggle Display")
4768     (gnus-mime-view-part "v" "View Interactively...")
4769     (gnus-mime-view-part-as-type "t" "View As Type...")
4770     (gnus-mime-view-part-as-charset "C" "View As charset...")
4771     (gnus-mime-save-part "o" "Save...")
4772     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4773     (gnus-mime-replace-part "r" "Replace part")
4774     (gnus-mime-delete-part "d" "Delete part")
4775     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4776     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4777     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4778     (gnus-mime-view-part-externally "e" "View Externally")
4779     (gnus-mime-print-part "p" "Print")
4780     (gnus-mime-pipe-part "|" "Pipe To Command...")
4781     (gnus-mime-action-on-part "." "Take action on the part...")))
4782
4783 (defun gnus-article-mime-part-status ()
4784   (if gnus-article-mime-handle-alist-1
4785       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4786           " (1 part)"
4787         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4788     ""))
4789
4790 (defvar gnus-mime-button-map
4791   (let ((map (make-sparse-keymap)))
4792     (define-key map gnus-mouse-2 'gnus-article-push-button)
4793     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4794     (dolist (c gnus-mime-button-commands)
4795       (define-key map (cadr c) (car c)))
4796     map))
4797
4798 (easy-menu-define
4799   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4800   `("MIME Part"
4801     ,@(mapcar (lambda (c)
4802                 (vector (caddr c) (car c) :active t))
4803               gnus-mime-button-commands)))
4804
4805 (defvar gnus-url-button-commands
4806   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4807
4808 (defvar gnus-url-button-map
4809   (let ((map (make-sparse-keymap)))
4810     (dolist (c gnus-url-button-commands)
4811       (define-key map (cadr c) (car c)))
4812     map))
4813
4814 (easy-menu-define
4815   gnus-url-button-menu gnus-url-button-map "URL button menu."
4816   `("Url Button"
4817     ,@(mapcar (lambda (c)
4818                 (vector (caddr c) (car c) :active t))
4819               gnus-url-button-commands)))
4820
4821 (defmacro gnus-bind-safe-url-regexp (&rest body)
4822   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4823   `(let ((mm-w3m-safe-url-regexp
4824           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4825                                 (gnus-buffer-live-p
4826                                  gnus-article-current-summary))
4827                            (with-current-buffer gnus-article-current-summary
4828                              gnus-newsgroup-name)
4829                          gnus-newsgroup-name)))
4830             (if (cond ((not group)
4831                        ;; Maybe we're in a mml-preview buffer
4832                        ;; and no group is selected.
4833                        t)
4834                       ((stringp gnus-safe-html-newsgroups)
4835                        (string-match gnus-safe-html-newsgroups group))
4836                       ((consp gnus-safe-html-newsgroups)
4837                        (member group gnus-safe-html-newsgroups)))
4838                 nil
4839               mm-w3m-safe-url-regexp))))
4840      ,@body))
4841
4842 (defun gnus-mime-button-menu (event prefix)
4843  "Construct a context-sensitive menu of MIME commands."
4844  (interactive "e\nP")
4845  (save-window-excursion
4846    (let ((pos (event-start event)))
4847      (select-window (posn-window pos))
4848      (goto-char (posn-point pos))
4849      (gnus-article-check-buffer)
4850      (popup-menu gnus-mime-button-menu nil prefix))))
4851
4852 (defun gnus-mime-view-all-parts (&optional handles)
4853   "View all the MIME parts."
4854   (interactive)
4855   (with-current-buffer gnus-article-buffer
4856     (let ((handles (or handles gnus-article-mime-handles))
4857           (mail-parse-charset gnus-newsgroup-charset)
4858           (mail-parse-ignored-charsets
4859            (with-current-buffer gnus-summary-buffer
4860              gnus-newsgroup-ignored-charsets)))
4861       (when handles
4862         (mm-remove-parts handles)
4863         (goto-char (point-min))
4864         (or (search-forward "\n\n") (goto-char (point-max)))
4865         (let ((inhibit-read-only t))
4866           (delete-region (point) (point-max))
4867           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4868
4869 (defun gnus-article-jump-to-part (n)
4870   "Jump to MIME part N."
4871   (interactive "P")
4872   (let ((parts (with-current-buffer gnus-article-buffer
4873                  (length gnus-article-mime-handle-alist))))
4874     (when (zerop parts)
4875       (error "No such part"))
4876     (pop-to-buffer gnus-article-buffer)
4877     ;; FIXME: why is it necessary?
4878     (sit-for 0)
4879     (or n
4880         (setq n (if (= parts 1)
4881                     1
4882                   (read-number (format "Jump to part (1..%s): " parts)))))
4883     (unless (and (integerp n) (<= n parts) (>= n 1))
4884       (setq n
4885             (progn
4886               (gnus-message 7 "Invalid part `%s', using %s instead."
4887                             n parts)
4888               parts)))
4889     (gnus-message 9 "Jumping to part %s." n)
4890     (cond ((>= gnus-auto-select-part 1)
4891            (while (and (<= n parts)
4892                        (not (gnus-article-goto-part n)))
4893              (setq n (1+ n))))
4894           ((< gnus-auto-select-part 0)
4895            (while (and (>= n 1)
4896                        (not (gnus-article-goto-part n)))
4897              (setq n (1- n))))
4898           (t
4899            (gnus-article-goto-part n)))))
4900
4901 (defvar gnus-mime-buttonized-part-id nil
4902   "ID of a mime part that should be buttonized.
4903 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4904
4905 (eval-when-compile
4906   (defsubst gnus-article-edit-part (handles &optional current-id)
4907     "Edit an article in order to delete a mime part.
4908 This function is exclusively used by `gnus-mime-save-part-and-strip'
4909 and `gnus-mime-delete-part', and not provided at run-time normally."
4910     (gnus-article-edit-article
4911      `(lambda ()
4912         (buffer-disable-undo)
4913         (erase-buffer)
4914         (let ((mail-parse-charset (or gnus-article-charset
4915                                       ',gnus-newsgroup-charset))
4916               (mail-parse-ignored-charsets
4917                (or gnus-article-ignored-charsets
4918                    ',gnus-newsgroup-ignored-charsets))
4919               (mbl mml-buffer-list))
4920           (setq mml-buffer-list nil)
4921           (insert-buffer-substring gnus-original-article-buffer)
4922           (mime-to-mml ',handles)
4923           (setq gnus-article-mime-handles nil)
4924           (let ((mbl1 mml-buffer-list))
4925             (setq mml-buffer-list mbl)
4926             (set (make-local-variable 'mml-buffer-list) mbl1))
4927           (gnus-make-local-hook 'kill-buffer-hook)
4928           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4929      `(lambda (no-highlight)
4930         (let ((mail-parse-charset (or gnus-article-charset
4931                                       ',gnus-newsgroup-charset))
4932               (message-options message-options)
4933               (message-options-set-recipient)
4934               (mail-parse-ignored-charsets
4935                (or gnus-article-ignored-charsets
4936                    ',gnus-newsgroup-ignored-charsets)))
4937           (mml-to-mime)
4938           (mml-destroy-buffers)
4939           (remove-hook 'kill-buffer-hook
4940                        'mml-destroy-buffers t)
4941           (kill-local-variable 'mml-buffer-list))
4942         (gnus-summary-edit-article-done
4943          ,(or (mail-header-references gnus-current-headers) "")
4944          ,(gnus-group-read-only-p)
4945          ,gnus-summary-buffer no-highlight))
4946      t)
4947     ;; Force buttonizing this part.
4948     (let ((gnus-mime-buttonized-part-id current-id))
4949       (gnus-article-edit-done))
4950     (gnus-configure-windows 'article)
4951     (when (and current-id (integerp gnus-auto-select-part))
4952       (gnus-article-jump-to-part
4953        (min (max (+ current-id gnus-auto-select-part) 1)
4954             (with-current-buffer gnus-article-buffer
4955               (length gnus-article-mime-handle-alist)))))))
4956
4957 (defun gnus-mime-replace-part (file)
4958   "Replace MIME part under point with an external body."
4959   ;; Useful if file has already been saved to disk
4960   (interactive
4961    (list
4962     (read-file-name "Replace MIME part with file: "
4963                     (or mm-default-directory default-directory)
4964                     nil nil)))
4965   (gnus-mime-save-part-and-strip file))
4966
4967 (defun gnus-mime-save-part-and-strip (&optional file)
4968   "Save the MIME part under point then replace it with an external body.
4969 If FILE is given, use it for the external part."
4970   (interactive)
4971   (gnus-article-check-buffer)
4972   (when (gnus-group-read-only-p)
4973     (error "The current group does not support deleting of parts"))
4974   (when (mm-complicated-handles gnus-article-mime-handles)
4975     (error "\
4976 The current article has a complicated MIME structure, giving up..."))
4977   (let* ((data (get-text-property (point) 'gnus-data))
4978          (id (get-text-property (point) 'gnus-part))
4979          param
4980          (handles gnus-article-mime-handles))
4981     (unless file
4982       (setq file
4983             (and data (mm-save-part data "Delete MIME part and save to: "))))
4984     (when file
4985       (with-current-buffer (mm-handle-buffer data)
4986         (erase-buffer)
4987         (insert "Content-Type: " (mm-handle-media-type data))
4988         (mml-insert-parameter-string (cdr (mm-handle-type data))
4989                                      '(charset))
4990         ;; Add a filename for the sake of saving the part again.
4991         (mml-insert-parameter
4992          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
4993         (insert "\n")
4994         (insert "Content-ID: " (message-make-message-id) "\n")
4995         (insert "Content-Transfer-Encoding: binary\n")
4996         (insert "\n"))
4997       (setcdr data
4998               (cdr (mm-make-handle nil
4999                                    `("message/external-body"
5000                                      (access-type . "LOCAL-FILE")
5001                                      (name . ,file)))))
5002       ;; (set-buffer gnus-summary-buffer)
5003       (gnus-article-edit-part handles id))))
5004
5005 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5006 ;; parts...>') but with stripping would be nice.
5007
5008 (defun gnus-mime-delete-part ()
5009   "Delete the MIME part under point.
5010 Replace it with some information about the removed part."
5011   (interactive)
5012   (gnus-article-check-buffer)
5013   (when (gnus-group-read-only-p)
5014     (error "The current group does not support deleting of parts"))
5015   (when (mm-complicated-handles gnus-article-mime-handles)
5016     (error "\
5017 The current article has a complicated MIME structure, giving up..."))
5018   (when (or gnus-expert-user
5019             (gnus-yes-or-no-p "\
5020 Deleting parts may malfunction or destroy the article; continue? "))
5021     (let* ((data (get-text-property (point) 'gnus-data))
5022            (id (get-text-property (point) 'gnus-part))
5023            (handles gnus-article-mime-handles)
5024            (none "(none)")
5025            (description
5026             (let ((desc (mm-handle-description data)))
5027               (when desc
5028                 (mail-decode-encoded-word-string desc))))
5029            (filename
5030             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
5031                 none))
5032            (type (mm-handle-media-type data)))
5033       (unless data
5034         (error "No MIME part under point"))
5035       (with-current-buffer (mm-handle-buffer data)
5036         (let ((bsize (buffer-size)))
5037           (erase-buffer)
5038           (insert
5039            (concat
5040             ",----\n"
5041             "| The following attachment has been deleted:\n"
5042             "|\n"
5043             "| Type:           " type "\n"
5044             "| Filename:       " filename "\n"
5045             "| Size (encoded): " (format "%s byte%s\n"
5046                                          bsize (if (= bsize 1)
5047                                                    ""
5048                                                  "s"))
5049             (when description
5050               (concat    "| Description:    " description "\n"))
5051             "`----\n"))
5052           (setcdr data
5053                   (cdr (mm-make-handle
5054                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5055                         (list "attachment")
5056                         (format "Deleted attachment (%s bytes)" bsize))))))
5057       ;; (set-buffer gnus-summary-buffer)
5058       (gnus-article-edit-part handles id))))
5059
5060 (defun gnus-mime-save-part ()
5061   "Save 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       (mm-save-part data))))
5067
5068 (defun gnus-mime-pipe-part (&optional cmd)
5069   "Pipe the MIME part under point to a process.
5070 Use CMD as the process."
5071   (interactive)
5072   (gnus-article-check-buffer)
5073   (let ((data (get-text-property (point) 'gnus-data)))
5074     (when data
5075       (mm-pipe-part data cmd))))
5076
5077 (defun gnus-mime-view-part ()
5078   "Interactively choose a viewing method for the MIME part under point."
5079   (interactive)
5080   (gnus-article-check-buffer)
5081   (let ((data (get-text-property (point) 'gnus-data)))
5082     (when data
5083       (setq gnus-article-mime-handles
5084             (mm-merge-handles
5085              gnus-article-mime-handles (setq data (copy-sequence data))))
5086       (mm-interactively-view-part data))))
5087
5088 (defun gnus-mime-view-part-as-type-internal ()
5089   (gnus-article-check-buffer)
5090   (let* ((handle (get-text-property (point) 'gnus-data))
5091          (name (or
5092                 ;; Content-Type: foo/bar; name=...
5093                 (mail-content-type-get (mm-handle-type handle) 'name)
5094                 ;; Content-Disposition: attachment; filename=...
5095                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5096          (def-type (and name (mm-default-file-encoding name))))
5097     (or (and def-type (cons def-type 0))
5098         (and handle
5099              (equal (mm-handle-media-supertype handle) "text")
5100              '("text/plain" . 0))
5101         '("application/octet-stream" . 0))))
5102
5103 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5104   "Choose a MIME media type, and view the part as such.
5105 If non-nil, PRED is a predicate to use during completion to limit the
5106 available media-types."
5107   (interactive)
5108   (unless mime-type
5109     (setq mime-type
5110           (let ((default (gnus-mime-view-part-as-type-internal)))
5111             (gnus-completing-read
5112              "View as MIME type"
5113              (if pred
5114                  (gnus-remove-if-not pred (mailcap-mime-types))
5115                (mailcap-mime-types))
5116              nil nil nil
5117              (car default)))))
5118   (gnus-article-check-buffer)
5119   (let ((handle (get-text-property (point) 'gnus-data)))
5120     (when handle
5121       (when (equal (mm-handle-media-type handle) "message/external-body")
5122         (unless (mm-handle-cache handle)
5123           (mm-extern-cache-contents handle))
5124         (setq handle (mm-handle-cache handle)))
5125       (setq handle
5126             (mm-make-handle (mm-handle-buffer handle)
5127                             (cons mime-type (cdr (mm-handle-type handle)))
5128                             (mm-handle-encoding handle)
5129                             (mm-handle-undisplayer handle)
5130                             (mm-handle-disposition handle)
5131                             (mm-handle-description handle)
5132                             nil
5133                             (mm-handle-id handle)))
5134       (setq gnus-article-mime-handles
5135             (mm-merge-handles gnus-article-mime-handles handle))
5136       (when (mm-handle-displayed-p handle)
5137         (mm-remove-part handle))
5138       (gnus-mm-display-part handle))))
5139
5140 (defun gnus-mime-copy-part (&optional handle arg)
5141   "Put the MIME part under point into a new buffer.
5142 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5143 are decompressed."
5144   (interactive (list nil current-prefix-arg))
5145   (gnus-article-check-buffer)
5146   (unless handle
5147     (setq handle (get-text-property (point) 'gnus-data)))
5148   (when handle
5149     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5150                                                'name)
5151                         (mail-content-type-get (mm-handle-disposition handle)
5152                                                'filename)))
5153           contents dont-decode charset coding-system)
5154       (mm-with-unibyte-buffer
5155         (mm-insert-part handle)
5156         (setq contents (or (condition-case nil
5157                                (mm-decompress-buffer filename nil 'sig)
5158                              (error
5159                               (setq dont-decode t)
5160                               nil))
5161                            (buffer-string))))
5162       (setq filename (cond (filename (file-name-nondirectory filename))
5163                            (dont-decode "*raw data*")
5164                            (t "*decoded*")))
5165       (cond
5166        (dont-decode)
5167        ((not arg)
5168         (unless (setq charset (mail-content-type-get
5169                                (mm-handle-type handle) 'charset))
5170           (unless (setq coding-system (mm-with-unibyte-buffer
5171                                         (insert contents)
5172                                         (mm-find-buffer-file-coding-system)))
5173             (setq charset gnus-newsgroup-charset))))
5174        ((numberp arg)
5175         (setq charset (or (cdr (assq arg
5176                                      gnus-summary-show-article-charset-alist))
5177                           (mm-read-coding-system "Charset: ")))))
5178       (switch-to-buffer (generate-new-buffer filename))
5179       (if (or coding-system
5180               (and charset
5181                    (setq coding-system (mm-charset-to-coding-system charset))
5182                    (not (eq coding-system 'ascii))))
5183           (progn
5184             (mm-enable-multibyte)
5185             (insert (mm-decode-coding-string contents coding-system))
5186             (setq buffer-file-coding-system
5187                   (if (boundp 'last-coding-system-used)
5188                       (symbol-value 'last-coding-system-used)
5189                     coding-system)))
5190         (mm-disable-multibyte)
5191         (insert contents)
5192         (setq buffer-file-coding-system mm-binary-coding-system))
5193       ;; We do it this way to make `normal-mode' set the appropriate mode.
5194       (unwind-protect
5195           (progn
5196             (setq buffer-file-name (expand-file-name filename))
5197             (normal-mode))
5198         (setq buffer-file-name nil))
5199       (goto-char (point-min)))))
5200
5201 (defun gnus-mime-print-part (&optional handle filename)
5202   "Print the MIME part under point."
5203   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5204   (gnus-article-check-buffer)
5205   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5206          (contents (and handle (mm-get-part handle)))
5207          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5208          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5209     (when contents
5210         (if printer
5211             (unwind-protect
5212                 (progn
5213                   (mm-save-part-to-file handle file)
5214                   (call-process shell-file-name nil
5215                                 (generate-new-buffer " *mm*")
5216                                 nil
5217                                 shell-command-switch
5218                                 (mm-mailcap-command
5219                                  printer file (mm-handle-type handle))))
5220               (delete-file file))
5221           (with-temp-buffer
5222             (insert contents)
5223             (gnus-print-buffer))
5224           (ps-despool filename)))))
5225
5226 (defun gnus-mime-inline-part (&optional handle arg)
5227   "Insert the MIME part under point into the current buffer.
5228 Compressed files like .gz and .bz2 are decompressed."
5229   (interactive (list nil current-prefix-arg))
5230   (gnus-article-check-buffer)
5231   (unless handle
5232     (setq handle (get-text-property (point) 'gnus-data)))
5233   (when handle
5234     (let ((b (point))
5235           (inhibit-read-only t)
5236           contents charset coding-system)
5237       (if (and (not arg) (mm-handle-undisplayer handle))
5238           (mm-remove-part handle)
5239         (mm-with-unibyte-buffer
5240           (mm-insert-part handle)
5241           (setq contents
5242                 (or (mm-decompress-buffer
5243                      (or (mail-content-type-get (mm-handle-type handle)
5244                                                 'name)
5245                          (mail-content-type-get (mm-handle-disposition handle)
5246                                                 'filename))
5247                      nil t)
5248                     (buffer-string))))
5249         (cond
5250          ((not arg)
5251           (unless (setq charset (mail-content-type-get
5252                                  (mm-handle-type handle) 'charset))
5253             (unless (setq coding-system
5254                           (mm-with-unibyte-buffer
5255                             (insert contents)
5256                             (mm-find-buffer-file-coding-system)))
5257               (setq charset gnus-newsgroup-charset))))
5258          ((numberp arg)
5259           (if (mm-handle-undisplayer handle)
5260               (mm-remove-part handle))
5261           (setq charset
5262                 (or (cdr (assq arg
5263                                gnus-summary-show-article-charset-alist))
5264                     (mm-read-coding-system "Charset: "))))
5265          (t
5266           (if (mm-handle-undisplayer handle)
5267               (mm-remove-part handle))))
5268         (forward-line 2)
5269         (mm-display-inline handle)
5270         (goto-char b)))))
5271
5272 (defun gnus-mime-set-charset-parameters (handle charset)
5273   "Set CHARSET to parameters in HANDLE.
5274 CHARSET may either be a string or a symbol."
5275   (unless (stringp charset)
5276     (setq charset (symbol-name charset)))
5277   (if (stringp (car handle))
5278       (dolist (h (cdr handle))
5279         (gnus-mime-set-charset-parameters h charset))
5280     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5281                                             "message/external-body")
5282                                      (progn
5283                                        (unless (mm-handle-cache handle)
5284                                          (mm-extern-cache-contents handle))
5285                                        (mm-handle-cache handle))
5286                                    handle)))
5287            (param (assq 'charset (cdr type))))
5288       (if param
5289           (setcdr param charset)
5290         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5291
5292 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5293   "Insert the MIME part under point into the current buffer using the
5294 specified charset."
5295   (interactive (list nil current-prefix-arg))
5296   (gnus-article-check-buffer)
5297   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5298         (fun (get-text-property (point) 'gnus-callback))
5299         (gnus-newsgroup-ignored-charsets 'gnus-all)
5300         charset form preferred parts)
5301     (when handle
5302       (when (prog1
5303                 (and fun
5304                      (setq charset
5305                            (or (cdr (assq
5306                                      arg
5307                                      gnus-summary-show-article-charset-alist))
5308                                (mm-read-coding-system "Charset: "))))
5309               (if (mm-handle-undisplayer handle)
5310                   (mm-remove-part handle)))
5311         (gnus-mime-set-charset-parameters handle charset)
5312         (when (and (consp (setq form (cdr-safe fun)))
5313                    (setq form (ignore-errors
5314                                 (assq 'gnus-mime-display-alternative form)))
5315                    (setq preferred (caddr form))
5316                    (progn
5317                      (when (eq (car preferred) 'quote)
5318                        (setq preferred (cadr preferred)))
5319                      (not (equal preferred
5320                                  (get-text-property (point) 'gnus-data))))
5321                    (setq parts (get-text-property (point) 'gnus-part))
5322                    (setq parts (cdr (assq parts
5323                                           gnus-article-mime-handle-alist)))
5324                    (equal (mm-handle-media-type parts) "multipart/alternative")
5325                    (setq parts (reverse (cdr parts))))
5326           (setcar (cddr form)
5327                   (list 'quote (or (cadr (member preferred parts))
5328                                    (car parts)))))
5329         (funcall fun handle)))))
5330
5331 (defun gnus-mime-view-part-externally (&optional handle)
5332   "View the MIME part under point with an external viewer."
5333   (interactive)
5334   (gnus-article-check-buffer)
5335   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5336          (mm-user-display-methods nil)
5337          (mm-inlined-types nil)
5338          (mail-parse-charset gnus-newsgroup-charset)
5339          (mail-parse-ignored-charsets
5340           (with-current-buffer gnus-summary-buffer
5341             gnus-newsgroup-ignored-charsets))
5342          (type (mm-handle-media-type handle))
5343          (method (mailcap-mime-info type))
5344          (mm-enable-external t))
5345     (if (not (stringp method))
5346         (gnus-mime-view-part-as-type
5347          nil (lambda (type) (stringp (mailcap-mime-info type))))
5348       (when handle
5349         (mm-display-part handle nil t)))))
5350
5351 (defun gnus-mime-view-part-internally (&optional handle)
5352   "View the MIME part under point with an internal viewer.
5353 If no internal viewer is available, use an external viewer."
5354   (interactive)
5355   (gnus-article-check-buffer)
5356   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5357          (mm-inlined-types '(".*"))
5358          (mm-inline-large-images t)
5359          (mail-parse-charset gnus-newsgroup-charset)
5360          (mail-parse-ignored-charsets
5361           (with-current-buffer gnus-summary-buffer
5362             gnus-newsgroup-ignored-charsets))
5363          (inhibit-read-only t))
5364     (if (not (mm-inlinable-p handle))
5365         (gnus-mime-view-part-as-type
5366          nil (lambda (type) (mm-inlinable-p handle type)))
5367       (when handle
5368         (gnus-bind-safe-url-regexp (mm-display-part handle))))))
5369
5370 (defun gnus-mime-action-on-part (&optional action)
5371   "Do something with the MIME attachment at \(point\)."
5372   (interactive
5373    (list (gnus-completing-read "Action" (mapcar 'car gnus-mime-action-alist) t)))
5374   (gnus-article-check-buffer)
5375   (let ((action-pair (assoc action gnus-mime-action-alist)))
5376     (if action-pair
5377         (funcall (cdr action-pair)))))
5378
5379 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5380   "Call FUNCTION on MIME part N.
5381 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5382 If INTERACTIVE, call FUNCTION interactivly."
5383   (let (window frame)
5384     ;; Check whether the article is displayed.
5385     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5386                  (setq window (get-buffer-window gnus-article-buffer t))
5387                  (frame-visible-p (setq frame (window-frame window))))
5388       (error "No article is displayed"))
5389     (with-current-buffer gnus-article-buffer
5390       ;; Check whether the article displays the right contents.
5391       (unless (with-current-buffer gnus-summary-buffer
5392                 (eq gnus-current-article (gnus-summary-article-number)))
5393         (error "You should select the right article first"))
5394       (if n
5395           (setq n (prefix-numeric-value n))
5396         (let ((pt (point)))
5397           (setq n (or (get-text-property pt 'gnus-part)
5398                       (and (not (bobp))
5399                            (get-text-property (1- pt) 'gnus-part))
5400                       (get-text-property (prog2
5401                                              (forward-line 1)
5402                                              (point)
5403                                            (goto-char pt))
5404                                          'gnus-part)
5405                       (get-text-property
5406                        (or (and (setq pt (previous-single-property-change
5407                                           pt 'gnus-part))
5408                                 (1- pt))
5409                            (next-single-property-change (point) 'gnus-part)
5410                            (point))
5411                        'gnus-part)
5412                       1))))
5413       ;; Check whether the specified part exists.
5414       (when (> n (length gnus-article-mime-handle-alist))
5415         (error "No such part")))
5416     (unless
5417         (progn
5418           ;; To select the window is needed so that the cursor
5419           ;; might be visible on the MIME button.
5420           (select-window (prog1
5421                              window
5422                            (setq window (selected-window))
5423                            ;; Article may be displayed in the other frame.
5424                            (gnus-select-frame-set-input-focus
5425                             (prog1
5426                                 frame
5427                               (setq frame (selected-frame))))))
5428           (when (gnus-article-goto-part n)
5429             ;; We point the cursor and the arrow at the MIME button
5430             ;; when the `function' prompt the user for something.
5431             (unless (and (pos-visible-in-window-p)
5432                          (> (count-lines (point) (window-end))
5433                             (/ (1- (window-height)) 3)))
5434               (recenter (/ (1- (window-height)) 3)))
5435             (let ((cursor-in-non-selected-windows t)
5436                   (overlay-arrow-string "=>")
5437                   (overlay-arrow-position (point-marker)))
5438               (unwind-protect
5439                   (cond
5440                    ((and no-handle interactive)
5441                     (call-interactively function))
5442                    (no-handle
5443                     (funcall function))
5444                    (interactive
5445                     (call-interactively
5446                      function (get-text-property (point) 'gnus-data)))
5447                    (t
5448                     (funcall function
5449                              (get-text-property (point) 'gnus-data))))
5450                 (set-marker overlay-arrow-position nil)
5451                 (unless gnus-auto-select-part
5452                   (gnus-select-frame-set-input-focus frame)
5453                   (select-window window))))
5454             t))
5455       (if gnus-inhibit-mime-unbuttonizing
5456           ;; This is the default though the program shouldn't reach here.
5457           (error "No such part")
5458         ;; The part which doesn't have the MIME button is selected.
5459         ;; So, we display all the buttons and redo it.
5460         (let ((gnus-inhibit-mime-unbuttonizing t))
5461           (gnus-summary-show-article)
5462           (gnus-article-part-wrapper n function no-handle))))))
5463
5464 (defun gnus-article-pipe-part (n)
5465   "Pipe MIME part N, which is the numerical prefix."
5466   (interactive "P")
5467   (gnus-article-part-wrapper n 'mm-pipe-part))
5468
5469 (defun gnus-article-save-part (n)
5470   "Save MIME part N, which is the numerical prefix."
5471   (interactive "P")
5472   (gnus-article-part-wrapper n 'mm-save-part))
5473
5474 (defun gnus-article-interactively-view-part (n)
5475   "View MIME part N interactively, which is the numerical prefix."
5476   (interactive "P")
5477   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5478
5479 (defun gnus-article-copy-part (n)
5480   "Copy MIME part N, which is the numerical prefix."
5481   (interactive "P")
5482   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5483
5484 (defun gnus-article-view-part-as-charset (n)
5485   "View MIME part N using a specified charset.
5486 N is the numerical prefix."
5487   (interactive "P")
5488   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5489
5490 (defun gnus-article-view-part-externally (n)
5491   "View MIME part N externally, which is the numerical prefix."
5492   (interactive "P")
5493   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5494
5495 (defun gnus-article-inline-part (n)
5496   "Inline MIME part N, which is the numerical prefix."
5497   (interactive "P")
5498   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5499
5500 (defun gnus-article-save-part-and-strip (n)
5501   "Save MIME part N and replace it with an external body.
5502 N is the numerical prefix."
5503   (interactive "P")
5504   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5505
5506 (defun gnus-article-replace-part (n)
5507   "Replace MIME part N with an external body.
5508 N is the numerical prefix."
5509   (interactive "P")
5510   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5511
5512 (defun gnus-article-delete-part (n)
5513   "Delete MIME part N and add some information about the removed part.
5514 N is the numerical prefix."
5515   (interactive "P")
5516   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5517
5518 (defun gnus-article-view-part-as-type (n)
5519   "Choose a MIME media type, and view part N as such.
5520 N is the numerical prefix."
5521   (interactive "P")
5522   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5523
5524 (defun gnus-article-mime-match-handle-first (condition)
5525   (if condition
5526       (let (n)
5527         (dolist (ihandle gnus-article-mime-handle-alist)
5528           (if (and (cond
5529                     ((functionp condition)
5530                      (funcall condition (cdr ihandle)))
5531                     ((eq condition 'undisplayed)
5532                      (not (or (mm-handle-undisplayer (cdr ihandle))
5533                               (equal (mm-handle-media-type (cdr ihandle))
5534                                      "multipart/alternative"))))
5535                     ((eq condition 'undisplayed-alternative)
5536                      (not (mm-handle-undisplayer (cdr ihandle))))
5537                     (t t))
5538                    (gnus-article-goto-part (car ihandle))
5539                    (or (not n) (< (car ihandle) n)))
5540               (setq n (car ihandle))))
5541         (or n 1))
5542     1))
5543
5544 (defun gnus-article-view-part (&optional n)
5545   "View MIME part N, which is the numerical prefix.
5546 If the part is already shown, hide the part.  If N is nil, view
5547 all parts."
5548   (interactive "P")
5549   (with-current-buffer gnus-article-buffer
5550     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5551                              gnus-article-mime-match-handle-function)))
5552     (when (> n (length gnus-article-mime-handle-alist))
5553       (error "No such part"))
5554     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5555       (when (gnus-article-goto-part n)
5556         (if (equal (car handle) "multipart/alternative")
5557             (gnus-article-press-button)
5558           (when (eq (gnus-mm-display-part handle) 'internal)
5559             (gnus-set-window-start)))))))
5560
5561 (defsubst gnus-article-mime-total-parts ()
5562   (if (bufferp (car gnus-article-mime-handles))
5563       1 ;; single part
5564     (1- (length gnus-article-mime-handles))))
5565
5566 (defun gnus-mm-display-part (handle)
5567   "Display HANDLE and fix MIME button."
5568   (let ((id (get-text-property (point) 'gnus-part))
5569         (point (point))
5570         (inhibit-read-only t))
5571     (forward-line 1)
5572     (prog1
5573         (let ((window (selected-window))
5574               (mail-parse-charset gnus-newsgroup-charset)
5575               (mail-parse-ignored-charsets
5576                (if (gnus-buffer-live-p gnus-summary-buffer)
5577                    (with-current-buffer gnus-summary-buffer
5578                      gnus-newsgroup-ignored-charsets)
5579                  nil)))
5580           (save-excursion
5581             (unwind-protect
5582                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5583                       (beg (point)))
5584                   (when win
5585                     (select-window win))
5586                   (goto-char point)
5587                   (forward-line)
5588                   (if (mm-handle-displayed-p handle)
5589                       ;; This will remove the part.
5590                       (mm-display-part handle)
5591                     (save-restriction
5592                       (narrow-to-region (point)
5593                                         (if (eobp) (point) (1+ (point))))
5594                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5595                       ;; We narrow to the part itself and
5596                       ;; then call the treatment functions.
5597                       (goto-char (point-min))
5598                       (forward-line 1)
5599                       (narrow-to-region (point) (point-max))
5600                       (gnus-treat-article
5601                        nil id
5602                        (gnus-article-mime-total-parts)
5603                        (mm-handle-media-type handle)))))
5604               (if (window-live-p window)
5605                   (select-window window)))))
5606       (goto-char point)
5607       (gnus-delete-line)
5608       (gnus-insert-mime-button
5609        handle id (list (mm-handle-displayed-p handle)))
5610       (goto-char point))))
5611
5612 (defun gnus-article-goto-part (n)
5613   "Go to MIME part N."
5614   (when gnus-break-pages
5615     (widen))
5616   (prog1
5617       (let ((start (text-property-any (point-min) (point-max) 'gnus-part n))
5618             part handle end next handles)
5619         (when start
5620           (goto-char start)
5621           (if (setq handle (get-text-property start 'gnus-data))
5622               start
5623             ;; Go to the displayed subpart, assuming this is
5624             ;; multipart/alternative.
5625             (setq part start
5626                   end (point-at-eol))
5627             (while (and (not handle)
5628                         part
5629                         (< part end)
5630                         (setq next (text-property-not-all part end
5631                                                           'gnus-data nil)))
5632               (setq part next
5633                     handle (get-text-property part 'gnus-data))
5634               (push (cons handle part) handles)
5635               (unless (mm-handle-displayed-p handle)
5636                 (setq handle nil
5637                       part (text-property-any part end 'gnus-data nil))))
5638             (unless handle
5639               ;; No subpart is displayed, so we find preferred one.
5640               (setq part
5641                     (cdr (assq (mm-preferred-alternative
5642                                 (nreverse (mapcar 'car handles)))
5643                                handles))))
5644             (if part
5645                 (goto-char (1+ part))
5646               start))))
5647     (when gnus-break-pages
5648       (gnus-narrow-to-page))))
5649
5650 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5651   (let ((gnus-tmp-name
5652          (or (mail-content-type-get (mm-handle-type handle) 'name)
5653              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5654              (mail-content-type-get (mm-handle-type handle) 'url)
5655              ""))
5656         (gnus-tmp-type (mm-handle-media-type handle))
5657         (gnus-tmp-description (or (mm-handle-description handle) ""))
5658         (gnus-tmp-dots
5659          (if (if displayed (car displayed)
5660                (mm-handle-displayed-p handle))
5661              "" "..."))
5662         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5663                            (buffer-size)))
5664         gnus-tmp-type-long b e)
5665     (when (string-match ".*/" gnus-tmp-name)
5666       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5667     (setq gnus-tmp-type-long (concat gnus-tmp-type
5668                                      (and (not (equal gnus-tmp-name ""))
5669                                           (concat "; " gnus-tmp-name))))
5670     (unless (equal gnus-tmp-description "")
5671       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5672     (unless (bolp)
5673       (insert "\n"))
5674     (setq b (point))
5675     (gnus-eval-format
5676      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5677      `(keymap ,gnus-mime-button-map
5678               gnus-callback gnus-mm-display-part
5679               gnus-part ,gnus-tmp-id
5680               article-type annotation
5681               gnus-data ,handle))
5682     (setq e (if (bolp)
5683                 ;; Exclude a newline.
5684                 (1- (point))
5685               (point)))
5686     (when gnus-article-button-face
5687       (gnus-overlay-put (gnus-make-overlay b e nil t)
5688                         'face gnus-article-button-face))
5689     (widget-convert-button
5690      'link b e
5691      :mime-handle handle
5692      :action 'gnus-widget-press-button
5693      :button-keymap gnus-mime-button-map
5694      :help-echo
5695      (lambda (widget)
5696        ;; Needed to properly clear the message due to a bug in
5697        ;; wid-edit (XEmacs only).
5698        (if (boundp 'help-echo-owns-message)
5699            (setq help-echo-owns-message t))
5700        (format
5701         "%S: %s the MIME part; %S: more options"
5702         (aref gnus-mouse-2 0)
5703         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5704             "hide" "show")
5705         (aref gnus-down-mouse-3 0))))))
5706
5707 (defun gnus-widget-press-button (elems el)
5708   (goto-char (widget-get elems :from))
5709   (gnus-article-press-button))
5710
5711 (defvar gnus-displaying-mime nil)
5712
5713 (defun gnus-display-mime (&optional ihandles)
5714   "Display the MIME parts."
5715   (save-excursion
5716     (save-selected-window
5717       (let ((window (get-buffer-window gnus-article-buffer))
5718             (point (point)))
5719         (when window
5720           (select-window window)
5721           ;; We have to do this since selecting the window
5722           ;; may change the point.  So we set the window point.
5723           (set-window-point window point)))
5724       (let ((handles ihandles)
5725             (inhibit-read-only t)
5726             handle)
5727         (cond (handles)
5728               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5729                (when gnus-article-emulate-mime
5730                  (mm-uu-dissect-text-parts handles)))
5731               (gnus-article-emulate-mime
5732                (setq handles (mm-uu-dissect))))
5733         (when (and (not ihandles)
5734                    (not gnus-displaying-mime))
5735           ;; Top-level call; we clean up.
5736           (when gnus-article-mime-handles
5737             (mm-destroy-parts gnus-article-mime-handles)
5738             (setq gnus-article-mime-handle-alist nil));; A trick.
5739           (setq gnus-article-mime-handles handles)
5740           ;; We allow users to glean info from the handles.
5741           (when gnus-article-mime-part-function
5742             (gnus-mime-part-function handles)))
5743         (if (and handles
5744                  (or (not (stringp (car handles)))
5745                      (cdr handles)))
5746             (progn
5747               (when (and (not ihandles)
5748                          (not gnus-displaying-mime))
5749                 ;; Clean up for mime parts.
5750                 (article-goto-body)
5751                 (delete-region (point) (point-max)))
5752               (let ((gnus-displaying-mime t))
5753                 (gnus-mime-display-part handles)))
5754           (save-restriction
5755             (article-goto-body)
5756             (narrow-to-region (point) (point-max))
5757             (gnus-treat-article nil 1 1 "text/plain")
5758             (widen)))
5759         (unless ihandles
5760           ;; Highlight the headers.
5761           (save-excursion
5762             (save-restriction
5763               (article-goto-body)
5764               (narrow-to-region (point-min) (point))
5765               (gnus-article-save-original-date
5766                (gnus-treat-article 'head)))))))
5767     ;; Cope with broken MIME messages.
5768     (goto-char (point-max))
5769     (unless (bolp)
5770       (insert "\n"))))
5771
5772 (defcustom gnus-mime-display-multipart-as-mixed nil
5773   "Display \"multipart\" parts as  \"multipart/mixed\".
5774
5775 If t, it overrides nil values of
5776 `gnus-mime-display-multipart-alternative-as-mixed' and
5777 `gnus-mime-display-multipart-related-as-mixed'."
5778   :group 'gnus-article-mime
5779   :type 'boolean)
5780
5781 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5782   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5783   :version "22.1"
5784   :group 'gnus-article-mime
5785   :type 'boolean)
5786
5787 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5788   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5789
5790 If displaying \"text/html\" is discouraged \(see
5791 `mm-discouraged-alternatives'\) images or other material inside a
5792 \"multipart/related\" part might be overlooked when this variable is nil."
5793   :version "22.1"
5794   :group 'gnus-article-mime
5795   :type 'boolean)
5796
5797 (defun gnus-mime-display-part (handle)
5798   (cond
5799    ;; Maybe a broken MIME message.
5800    ((null handle))
5801    ;; Single part.
5802    ((not (stringp (car handle)))
5803     (gnus-mime-display-single handle))
5804    ;; User-defined multipart
5805    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5806     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5807              handle))
5808    ;; multipart/alternative
5809    ((and (equal (car handle) "multipart/alternative")
5810          (not (or gnus-mime-display-multipart-as-mixed
5811                   gnus-mime-display-multipart-alternative-as-mixed)))
5812     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5813       (push (cons id handle) gnus-article-mime-handle-alist)
5814       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5815    ;; multipart/related
5816    ((and (equal (car handle) "multipart/related")
5817          (not (or gnus-mime-display-multipart-as-mixed
5818                   gnus-mime-display-multipart-related-as-mixed)))
5819     ;;;!!!We should find the start part, but we just default
5820     ;;;!!!to the first part.
5821     ;;(gnus-mime-display-part (cadr handle))
5822     ;;;!!! Most multipart/related is an HTML message plus images.
5823     ;;;!!! Unfortunately we are unable to let W3 display those
5824     ;;;!!! included images, so we just display it as a mixed multipart.
5825     ;;(gnus-mime-display-mixed (cdr handle))
5826     ;;;!!! No, w3 can display everything just fine.
5827     (gnus-mime-display-part (cadr handle)))
5828    ((equal (car handle) "multipart/signed")
5829     (gnus-add-wash-type 'signed)
5830     (gnus-mime-display-security handle))
5831    ((equal (car handle) "multipart/encrypted")
5832     (gnus-add-wash-type 'encrypted)
5833     (gnus-mime-display-security handle))
5834    ;; Other multiparts are handled like multipart/mixed.
5835    (t
5836     (gnus-mime-display-mixed (cdr handle)))))
5837
5838 (defun gnus-mime-part-function (handles)
5839   (if (stringp (car handles))
5840       (mapcar 'gnus-mime-part-function (cdr handles))
5841     (funcall gnus-article-mime-part-function handles)))
5842
5843 (defun gnus-mime-display-mixed (handles)
5844   (mapcar 'gnus-mime-display-part handles))
5845
5846 (defun gnus-mime-display-single (handle)
5847   (let ((type (mm-handle-media-type handle))
5848         (ignored gnus-ignored-mime-types)
5849         (not-attachment t)
5850         (move nil)
5851         display text)
5852     (catch 'ignored
5853       (progn
5854         (while ignored
5855           (when (string-match (pop ignored) type)
5856             (throw 'ignored nil)))
5857         (if (and (not (and (if (gnus-buffer-live-p gnus-summary-buffer)
5858                                (with-current-buffer gnus-summary-buffer
5859                                  gnus-inhibit-images)
5860                              gnus-inhibit-images)
5861                            (string-match "\\`image/" type)))
5862                  (setq not-attachment
5863                        (and (not (mm-inline-override-p handle))
5864                             (or (not (mm-handle-disposition handle))
5865                                 (equal (car (mm-handle-disposition handle))
5866                                        "inline")
5867                                 (mm-attachment-override-p handle))))
5868                  (mm-automatic-display-p handle)
5869                  (or (and
5870                       (mm-inlinable-p handle)
5871                       (mm-inlined-p handle))
5872                      (mm-automatic-external-display-p type)))
5873             (setq display t)
5874           (when (equal (mm-handle-media-supertype handle) "text")
5875             (setq text t)))
5876         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5877               beg)
5878           (push (cons id handle) gnus-article-mime-handle-alist)
5879           (when (and display
5880                      (equal (mm-handle-media-supertype handle) "message"))
5881             (insert-char
5882              ?\n
5883              (cond ((not (bolp)) 2)
5884                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5885                    (t 1))))
5886           (when (or (not display)
5887                     (not (gnus-unbuttonized-mime-type-p type))
5888                     (eq id gnus-mime-buttonized-part-id))
5889             (gnus-insert-mime-button
5890              handle id (list (or display (and not-attachment text))))
5891             (gnus-article-insert-newline)
5892             ;; Remember modify the number of forward lines.
5893             (setq move t))
5894           (setq beg (point))
5895           (cond
5896            (display
5897             (when move
5898               (forward-line -1)
5899               (setq beg (point)))
5900             (let ((mail-parse-charset gnus-newsgroup-charset)
5901                   (mail-parse-ignored-charsets
5902                    (save-excursion (condition-case ()
5903                                        (set-buffer gnus-summary-buffer)
5904                                      (error))
5905                                    gnus-newsgroup-ignored-charsets)))
5906               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5907             (goto-char (point-max)))
5908            ((and text not-attachment)
5909             (when move
5910               (forward-line -1)
5911               (setq beg (point)))
5912             (gnus-article-insert-newline)
5913             (mm-display-inline handle)
5914             (goto-char (point-max))))
5915           ;; Do highlighting.
5916           (save-excursion
5917             (save-restriction
5918               (narrow-to-region beg (point))
5919               (if (eq handle gnus-article-mime-handles)
5920                   ;; The format=flowed case.
5921                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5922                 ;; Don't count signature parts that are never displayed.
5923                 ;; The part number should be re-calculated supposing this
5924                 ;; might be a message/rfc822 part.
5925                 (let (handles)
5926                   (dolist (part gnus-article-mime-handles)
5927                     (unless (or (stringp part)
5928                                 (equal (car (mm-handle-type part))
5929                                        "application/pgp-signature"))
5930                       (push part handles)))
5931                   (gnus-treat-article
5932                    nil (length (memq handle handles)) (length handles)
5933                    (mm-handle-media-type handle)))))))))))
5934
5935 (defun gnus-unbuttonized-mime-type-p (type)
5936   "Say whether TYPE is to be unbuttonized."
5937   (unless gnus-inhibit-mime-unbuttonizing
5938     (when (catch 'found
5939             (let ((types gnus-unbuttonized-mime-types))
5940               (while types
5941                 (when (string-match (pop types) type)
5942                   (throw 'found t)))))
5943       (not (catch 'found
5944              (let ((types gnus-buttonized-mime-types))
5945                (while types
5946                  (when (string-match (pop types) type)
5947                    (throw 'found t)))))))))
5948
5949 (defun gnus-article-insert-newline ()
5950   "Insert a newline, but mark it as undeletable."
5951   (gnus-put-text-property
5952    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5953
5954 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5955   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5956          (ihandles handles)
5957          (point (point))
5958          handle (inhibit-read-only t) from props begend not-pref)
5959     (save-window-excursion
5960       (save-restriction
5961         (when ibegend
5962           (narrow-to-region (car ibegend)
5963                             (or (cdr ibegend)
5964                                 (progn
5965                                   (goto-char (car ibegend))
5966                                   (forward-line 2)
5967                                   (point))))
5968           (delete-region (point-min) (point-max))
5969           (mm-remove-parts handles))
5970         (setq begend (list (point-marker)))
5971         ;; Do the toggle.
5972         (unless (setq not-pref (cadr (member preferred ihandles)))
5973           (setq not-pref (car ihandles)))
5974         (when (or ibegend
5975                   (not preferred)
5976                   (not (gnus-unbuttonized-mime-type-p
5977                         "multipart/alternative")))
5978           (gnus-add-text-properties
5979            (setq from (point))
5980            (progn
5981              (insert (format "%d.  " id))
5982              (point))
5983            `(gnus-callback
5984              (lambda (handles)
5985                (unless ,(not ibegend)
5986                  (setq gnus-article-mime-handle-alist
5987                        ',gnus-article-mime-handle-alist))
5988                (gnus-mime-display-alternative
5989                 ',ihandles ',not-pref ',begend ,id))
5990              keymap ,gnus-mime-button-map
5991              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5992              face ,gnus-article-button-face
5993              gnus-part ,id
5994              article-type multipart))
5995           (widget-convert-button 'link from (point)
5996                                  :action 'gnus-widget-press-button
5997                                  :button-keymap gnus-widget-button-keymap)
5998           ;; Do the handles
5999           (while (setq handle (pop handles))
6000             (gnus-add-text-properties
6001              (setq from (point))
6002              (progn
6003                (insert (format "(%c) %-18s"
6004                                (if (equal handle preferred) ?* ? )
6005                                (mm-handle-media-type handle)))
6006                (point))
6007              `(gnus-callback
6008                (lambda (handles)
6009                  (unless ,(not ibegend)
6010                    (setq gnus-article-mime-handle-alist
6011                          ',gnus-article-mime-handle-alist))
6012                  (gnus-mime-display-alternative
6013                   ',ihandles ',handle ',begend ,id))
6014                keymap ,gnus-mime-button-map
6015                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6016                face ,gnus-article-button-face
6017                gnus-part ,id
6018                gnus-data ,handle))
6019             (widget-convert-button 'link from (point)
6020                                    :action 'gnus-widget-press-button
6021                                    :button-keymap gnus-widget-button-keymap)
6022             (insert "  "))
6023           (insert "\n\n"))
6024         (when preferred
6025           (if (stringp (car preferred))
6026               (gnus-display-mime preferred)
6027             (let ((mail-parse-charset gnus-newsgroup-charset)
6028                   (mail-parse-ignored-charsets
6029                    (with-current-buffer gnus-summary-buffer
6030                      gnus-newsgroup-ignored-charsets)))
6031               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6032               ;; Do highlighting.
6033               (save-excursion
6034                 (save-restriction
6035                   (narrow-to-region (car begend) (point-max))
6036                   (gnus-treat-article
6037                    nil (length gnus-article-mime-handle-alist)
6038                    (gnus-article-mime-total-parts)
6039                    (mm-handle-media-type preferred))))))
6040           (goto-char (point-max))
6041           (setcdr begend (point-marker)))))
6042     (when ibegend
6043       (goto-char point))))
6044
6045 (defconst gnus-article-wash-status-strings
6046   (let ((alist '((cite "c" "Possible hidden citation text"
6047                        " " "All citation text visible")
6048                  (headers "h" "Hidden headers"
6049                           " " "All headers visible.")
6050                  (pgp "p" "Encrypted or signed message status hidden"
6051                       " " "No hidden encryption nor digital signature status")
6052                  (signature "s" "Signature has been hidden"
6053                             " " "Signature is visible")
6054                  (overstrike "o" "Overstrike (^H) characters applied"
6055                              " " "No overstrike characters applied")
6056                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6057                            " " "No /*_emphasis_*/ characters applied")))
6058         result)
6059     (dolist (entry alist result)
6060       (let ((key (nth 0 entry))
6061             (on (copy-sequence (nth 1 entry)))
6062             (on-help (nth 2 entry))
6063             (off (copy-sequence (nth 3 entry)))
6064             (off-help (nth 4 entry)))
6065         (put-text-property 0 1 'help-echo on-help on)
6066         (put-text-property 0 1 'help-echo off-help off)
6067         (push (list key on off) result))))
6068   "Alist of strings describing wash status in the mode line.
6069 Each entry has the form (KEY ON OF), where the KEY is a symbol
6070 representing the particular washing function, ON is the string to use
6071 in the article mode line when the washing function is active, and OFF
6072 is the string to use when it is inactive.")
6073
6074 (defun gnus-article-wash-status-entry (key value)
6075   (let ((entry (assoc key gnus-article-wash-status-strings)))
6076     (if value (nth 1 entry) (nth 2 entry))))
6077
6078 (defun gnus-article-wash-status ()
6079   "Return a string which display status of article washing."
6080   (with-current-buffer gnus-article-buffer
6081     (let ((cite (memq 'cite gnus-article-wash-types))
6082           (headers (memq 'headers gnus-article-wash-types))
6083           (boring (memq 'boring-headers gnus-article-wash-types))
6084           (pgp (memq 'pgp gnus-article-wash-types))
6085           (pem (memq 'pem gnus-article-wash-types))
6086           (signed (memq 'signed gnus-article-wash-types))
6087           (encrypted (memq 'encrypted gnus-article-wash-types))
6088           (signature (memq 'signature gnus-article-wash-types))
6089           (overstrike (memq 'overstrike gnus-article-wash-types))
6090           (emphasis (memq 'emphasis gnus-article-wash-types)))
6091       (concat
6092        (gnus-article-wash-status-entry 'cite cite)
6093        (gnus-article-wash-status-entry 'headers (or headers boring))
6094        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6095        (gnus-article-wash-status-entry 'signature signature)
6096        (gnus-article-wash-status-entry 'overstrike overstrike)
6097        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6098
6099 (defun gnus-add-wash-type (type)
6100   "Add a washing of TYPE to the current status."
6101   (add-to-list 'gnus-article-wash-types type))
6102
6103 (defun gnus-delete-wash-type (type)
6104   "Add a washing of TYPE to the current status."
6105   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6106
6107 (defun gnus-add-image (category image)
6108   "Add IMAGE of CATEGORY to the list of displayed images."
6109   (let ((entry (assq category gnus-article-image-alist)))
6110     (unless entry
6111       (setq entry (list category))
6112       (push entry gnus-article-image-alist))
6113     (nconc entry (list image))))
6114
6115 (defun gnus-delete-images (category)
6116   "Delete all images in CATEGORY."
6117   (let ((entry (assq category gnus-article-image-alist)))
6118     (dolist (image (cdr entry))
6119       (gnus-remove-image image category))
6120     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6121     (gnus-delete-wash-type category)))
6122
6123 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6124
6125 (defun gnus-article-maybe-hide-headers ()
6126   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6127 Provided for backwards compatibility."
6128   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6129                  (not (with-current-buffer gnus-summary-buffer
6130                         gnus-have-all-headers)))
6131              (not gnus-inhibit-hiding))
6132     (gnus-article-hide-headers)))
6133
6134 ;;; Article savers.
6135
6136 (defun gnus-output-to-file (file-name)
6137   "Append the current article to a file named FILE-NAME.
6138 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6139 text and used as the value of the coding cookie which is added to the
6140 top of a file.  Otherwise, this function saves a raw article without
6141 the coding cookie."
6142   (let* ((artbuf (current-buffer))
6143          (file-name-coding-system nnmail-pathname-coding-system)
6144          (coding gnus-article-save-coding-system)
6145          (coding-system-for-read (if coding
6146                                      nil ;; Rely on the coding cookie.
6147                                    mm-text-coding-system))
6148          (coding-system-for-write (or coding
6149                                       mm-text-coding-system-for-write
6150                                       mm-text-coding-system))
6151          (exists (file-exists-p file-name)))
6152     (with-temp-buffer
6153       (when exists
6154         (insert-file-contents file-name)
6155         (goto-char (point-min))
6156         ;; Remove the existing coding cookie.
6157         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6158           (delete-region (match-beginning 0) (match-end 0))))
6159       (goto-char (point-max))
6160       (insert-buffer-substring artbuf)
6161       ;; Append newline at end of the buffer as separator, and then
6162       ;; save it to file.
6163       (goto-char (point-max))
6164       (insert "\n")
6165       (when coding
6166         ;; If the coding system is not suitable to encode the text,
6167         ;; ask a user for a proper one.
6168         (when (fboundp 'select-safe-coding-system)
6169           (setq coding (coding-system-base
6170                         (save-window-excursion
6171                           (select-safe-coding-system (point-min) (point-max)
6172                                                      coding))))
6173           (setq coding-system-for-write
6174                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6175                     coding)))
6176         (goto-char (point-min))
6177         ;; Add the coding cookie.
6178         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6179                         coding-system-for-write)))
6180       (if exists
6181           (progn
6182             (write-region (point-min) (point-max) file-name nil 'no-message)
6183             (message "Appended to %s" file-name))
6184         (write-region (point-min) (point-max) file-name))))
6185   t)
6186
6187 (defun gnus-narrow-to-page (&optional arg)
6188   "Narrow the article buffer to a page.
6189 If given a numerical ARG, move forward ARG pages."
6190   (interactive "P")
6191   (setq arg (if arg (prefix-numeric-value arg) 0))
6192   (with-current-buffer gnus-article-buffer
6193     (widen)
6194     ;; Remove any old next/prev buttons.
6195     (when (gnus-visual-p 'page-marker)
6196       (let ((inhibit-read-only t))
6197         (gnus-remove-text-with-property 'gnus-prev)
6198         (gnus-remove-text-with-property 'gnus-next)))
6199     (let (st nd pt)
6200       (when (save-excursion
6201               (cond ((< arg 0)
6202                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6203                          (prog1
6204                              (setq nd (match-beginning 0)
6205                                    pt nd)
6206                            (when (re-search-backward page-delimiter nil t)
6207                              (setq st (match-end 0))))
6208                        (when (re-search-forward page-delimiter nil t)
6209                          (setq nd (match-beginning 0)
6210                                pt (point-min)))))
6211                     ((> arg 0)
6212                      (if (re-search-forward page-delimiter nil 'move arg)
6213                          (prog1
6214                              (setq st (match-end 0)
6215                                    pt st)
6216                            (when (re-search-forward page-delimiter nil t)
6217                              (setq nd (match-beginning 0))))
6218                        (when (re-search-backward page-delimiter nil t)
6219                          (setq st (match-end 0)
6220                                pt (point-max)))))
6221                     (t
6222                      (when (re-search-backward page-delimiter nil t)
6223                        (goto-char (setq st (match-end 0))))
6224                      (when (re-search-forward page-delimiter nil t)
6225                        (setq nd (match-beginning 0)))
6226                      (or st nd))))
6227         (setq gnus-page-broken t)
6228         (when pt (goto-char pt))
6229         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6230         (when (gnus-visual-p 'page-marker)
6231           (save-excursion
6232             (when nd
6233               (goto-char nd)
6234               (gnus-insert-next-page-button))
6235             (when st
6236               (goto-char st)
6237               (gnus-insert-prev-page-button))))))))
6238
6239 ;; Article mode commands
6240
6241 (defun gnus-article-goto-next-page ()
6242   "Show the next page of the article."
6243   (interactive)
6244   (when (gnus-article-next-page)
6245     (goto-char (point-min))
6246     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6247
6248
6249 (defun gnus-article-goto-prev-page ()
6250   "Show the previous page of the article."
6251   (interactive)
6252   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6253       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6254     (gnus-article-prev-page nil)))
6255
6256 ;; This is cleaner but currently breaks `gnus-pick-mode':
6257 ;;
6258 ;; (defun gnus-article-goto-next-page ()
6259 ;;   "Show the next page of the article."
6260 ;;   (interactive)
6261 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6262 ;;     (gnus-summary-next-page)))
6263 ;;
6264 ;; (defun gnus-article-goto-prev-page ()
6265 ;;   "Show the next page of the article."
6266 ;;   (interactive)
6267 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6268 ;;     (gnus-summary-prev-page)))
6269
6270 (defun gnus-article-next-page (&optional lines)
6271   "Show the next page of the current article.
6272 If end of article, return non-nil.  Otherwise return nil.
6273 Argument LINES specifies lines to be scrolled up."
6274   (interactive "p")
6275   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6276   (if (and (not (and gnus-article-over-scroll
6277                      (> (count-lines (window-start) (point-max))
6278                         (if (featurep 'xemacs)
6279                             (or lines (1- (window-height)))
6280                           (+ (or lines (1- (window-height))) scroll-margin)))))
6281            (save-excursion
6282              (end-of-line)
6283              (and (pos-visible-in-window-p)     ;Not continuation line.
6284                   (>= (point) (point-max)))))
6285       ;; Nothing in this page.
6286       (if (or (not gnus-page-broken)
6287               (save-excursion
6288                 (save-restriction
6289                   (widen)
6290                   (forward-line)
6291                   (eobp)))) ;Real end-of-buffer?
6292           (progn
6293             (when gnus-article-over-scroll
6294               (gnus-article-next-page-1 lines))
6295             t)                  ;Nothing more.
6296         (gnus-narrow-to-page 1)         ;Go to next page.
6297         nil)
6298     ;; More in this page.
6299     (gnus-article-next-page-1 lines)
6300     nil))
6301
6302 (defun gnus-article-beginning-of-window ()
6303   "Move point to the beginning of the window.
6304 In Emacs, the point is placed at the line number which `scroll-margin'
6305 specifies."
6306   (if (featurep 'xemacs)
6307       (move-to-window-line 0)
6308     ;; There is an obscure bug in Emacs that makes it impossible to
6309     ;; scroll past big pictures in the article buffer.  Try to fix
6310     ;; this by adding a sanity check by counting the lines visible.
6311     (when (> (count-lines (window-start) (window-end)) 30)
6312       (move-to-window-line
6313        (min (max 0 scroll-margin)
6314             (max 1 (- (window-height)
6315                       (if mode-line-format 1 0)
6316                       (if header-line-format 1 0)
6317                       2)))))))
6318
6319 (defun gnus-article-next-page-1 (lines)
6320   (condition-case ()
6321       (let ((scroll-in-place nil))
6322         (scroll-up lines))
6323     (end-of-buffer
6324      ;; Long lines may cause an end-of-buffer error.
6325      (goto-char (point-max))))
6326   (gnus-article-beginning-of-window))
6327
6328 (defun gnus-article-prev-page (&optional lines)
6329   "Show previous page of current article.
6330 Argument LINES specifies lines to be scrolled down."
6331   (interactive "p")
6332   (move-to-window-line 0)
6333   (if (and gnus-page-broken
6334            (bobp)
6335            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6336       (progn
6337         (gnus-narrow-to-page -1)        ;Go to previous page.
6338         (goto-char (point-max))
6339         (recenter (if gnus-article-over-scroll
6340                       (if lines
6341                           (max (if (featurep 'xemacs)
6342                                    lines
6343                                  (+ lines scroll-margin))
6344                                3)
6345                         (- (window-height) 2))
6346                     -1)))
6347     (prog1
6348         (condition-case ()
6349             (let ((scroll-in-place nil))
6350               (scroll-down lines))
6351           (beginning-of-buffer
6352            (goto-char (point-min))))
6353       (gnus-article-beginning-of-window))))
6354
6355 (defun gnus-article-only-boring-p ()
6356   "Decide whether there is only boring text remaining in the article.
6357 Something \"interesting\" is a word of at least two letters that does
6358 not have a face in `gnus-article-boring-faces'."
6359   (when (and gnus-article-skip-boring
6360              (boundp 'gnus-article-boring-faces)
6361              (symbol-value 'gnus-article-boring-faces))
6362     (save-excursion
6363       (let ((inhibit-point-motion-hooks t))
6364         (catch 'only-boring
6365           (while (re-search-forward "\\b\\w\\w" nil t)
6366             (forward-char -1)
6367             (when (not (gnus-intersection
6368                         (gnus-faces-at (point))
6369                         (symbol-value 'gnus-article-boring-faces)))
6370               (throw 'only-boring nil)))
6371           (throw 'only-boring t))))))
6372
6373 (defun gnus-article-refer-article ()
6374   "Read article specified by message-id around point."
6375   (interactive)
6376   (save-excursion
6377     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6378     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6379     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6380         (let ((msg-id (concat "<" (match-string 0) ">")))
6381           (set-buffer gnus-summary-buffer)
6382           (gnus-summary-refer-article msg-id))
6383       (error "No references around point"))))
6384
6385 (defun gnus-article-show-summary ()
6386   "Reconfigure windows to show summary buffer."
6387   (interactive)
6388   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6389       (error "There is no summary buffer for this article buffer")
6390     (gnus-article-set-globals)
6391     (gnus-configure-windows 'article)
6392     (gnus-summary-goto-subject gnus-current-article)
6393     (gnus-summary-position-point)))
6394
6395 (defun gnus-article-describe-briefly ()
6396   "Describe article mode commands briefly."
6397   (interactive)
6398   (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")))
6399
6400 (defun gnus-article-check-buffer ()
6401   "Beep if not in an article buffer."
6402   (unless (equal major-mode 'gnus-article-mode)
6403     (error "Command invoked outside of a Gnus article buffer")))
6404
6405 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6406   "Read a summary buffer key sequence and execute it from the article buffer."
6407   (interactive "P")
6408   (gnus-article-check-buffer)
6409   (let ((nosaves
6410          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6411            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6412            "=" "^" "\M-^" "|"))
6413         (nosave-but-article
6414          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6415            "An" "Ap" [?A (meta return)] [?A delete]))
6416         (nosave-in-article
6417          '("AS" "\C-d"))
6418         (up-to-top
6419          '("n" "Gn" "p" "Gp"))
6420         keys new-sum-point)
6421     (with-current-buffer gnus-article-current-summary
6422       (let (gnus-pick-mode)
6423         (setq unread-command-events (nconc unread-command-events
6424                                            (list (or key last-command-event)))
6425               keys (if (featurep 'xemacs)
6426                        (events-to-keys (read-key-sequence nil t))
6427                      (read-key-sequence nil t)))))
6428
6429     (message "")
6430
6431     (cond
6432      ((eq (aref keys (1- (length keys))) ?\C-h)
6433       (gnus-article-describe-bindings (substring keys 0 -1)))
6434      ((or (member keys nosaves)
6435           (member keys nosave-but-article)
6436           (member keys nosave-in-article))
6437       (let (func)
6438         (save-window-excursion
6439           (pop-to-buffer gnus-article-current-summary)
6440           ;; We disable the pick minor mode commands.
6441           (let (gnus-pick-mode)
6442             (setq func (key-binding keys t))))
6443         (if (or (not func)
6444                 (numberp func))
6445             (ding)
6446           (unless (member keys nosave-in-article)
6447             (set-buffer gnus-article-current-summary))
6448           (when (get func 'disabled)
6449             (error "Function %s disabled" func))
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         (setq func (let (gnus-pick-mode)
6479                      (key-binding keys t)))
6480         (when (get func 'disabled)
6481           (error "Function %s disabled" func))
6482         (if (and func
6483                  (functionp func)
6484                  (condition-case code
6485                      (progn
6486                        (call-interactively func)
6487                        t)
6488                    (error
6489                     (setq err code)
6490                     nil)))
6491             (progn
6492               (when (eq win (selected-window))
6493                 (setq new-sum-point (point)
6494                       new-sum-start (window-start win)
6495                       new-sum-hscroll (window-hscroll win)))
6496               (when (or (eq in-buffer (current-buffer))
6497                         (when (eq obuf (current-buffer))
6498                           (set-buffer in-buffer)
6499                           t))
6500                 (setq selected (gnus-summary-select-article))
6501                 (set-buffer obuf)
6502                 (unless not-restore-window
6503                   (set-window-configuration owin))
6504                 (when (and (eq selected 'old)
6505                            new-sum-point)
6506                   (set-window-start (get-buffer-window (current-buffer))
6507                                     1)
6508                   (set-window-point (get-buffer-window (current-buffer))
6509                                     (if (article-goto-body)
6510                                         (1- (point))
6511                                       (point))))
6512                 (when (and (not not-restore-window)
6513                            new-sum-point
6514                            (window-live-p win)
6515                            (with-current-buffer (window-buffer win)
6516                              (eq major-mode 'gnus-summary-mode)))
6517                   (set-window-point win new-sum-point)
6518                   (set-window-start win new-sum-start)
6519                   (set-window-hscroll win new-sum-hscroll))))
6520           (set-window-configuration owin)
6521           (if err
6522               (signal (car err) (cdr err))
6523             (ding))))))))
6524
6525 (defun gnus-article-read-summary-send-keys ()
6526   (interactive)
6527   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6528     (gnus-article-read-summary-keys)))
6529
6530 (defun gnus-article-describe-key (key)
6531   "Display documentation of the function invoked by KEY.
6532 KEY is a string or a vector."
6533   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6534                        (read-key-sequence "Describe key: "))))
6535   (gnus-article-check-buffer)
6536   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6537                                   gnus-article-read-summary-send-keys))
6538       (with-current-buffer gnus-article-current-summary
6539         (setq unread-command-events
6540               (if (featurep 'xemacs)
6541                   (append key nil)
6542                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6543                                         (list 'meta (- x 128))
6544                                       x))
6545                         key)))
6546         (let ((cursor-in-echo-area t)
6547               gnus-pick-mode)
6548           (describe-key (read-key-sequence nil t))))
6549     (describe-key key)))
6550
6551 (defun gnus-article-describe-key-briefly (key &optional insert)
6552   "Display documentation of the function invoked by KEY.
6553 KEY is a string or a vector."
6554   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6555                        (read-key-sequence "Describe key: "))
6556                      current-prefix-arg))
6557   (gnus-article-check-buffer)
6558   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6559                                   gnus-article-read-summary-send-keys))
6560       (with-current-buffer gnus-article-current-summary
6561         (setq unread-command-events
6562               (if (featurep 'xemacs)
6563                   (append key nil)
6564                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6565                                         (list 'meta (- x 128))
6566                                       x))
6567                         key)))
6568         (let ((cursor-in-echo-area t)
6569               gnus-pick-mode)
6570           (describe-key-briefly (read-key-sequence nil t) insert)))
6571     (describe-key-briefly key insert)))
6572
6573 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6574 (defvar gnus-agent-summary-mode)
6575 (defvar gnus-draft-mode)
6576 ;; Calling help-buffer will autoload help-mode.
6577 (defvar help-xref-stack-item)
6578 ;; Emacs 22 doesn't load it in the batch mode.
6579 (eval-when-compile
6580   (autoload 'help-buffer "help-mode"))
6581
6582 (defun gnus-article-describe-bindings (&optional prefix)
6583   "Show a list of all defined keys, and their definitions.
6584 The optional argument PREFIX, if non-nil, should be a key sequence;
6585 then we display only bindings that start with that prefix."
6586   (interactive)
6587   (gnus-article-check-buffer)
6588   (let ((keymap (copy-keymap gnus-article-mode-map))
6589         (map (copy-keymap gnus-article-send-map))
6590         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6591         parent agent draft)
6592     (define-key keymap "S" map)
6593     (define-key map [t] nil)
6594     (with-current-buffer gnus-article-current-summary
6595       (set-keymap-parent
6596        keymap
6597        (if (setq parent (keymap-parent gnus-article-mode-map))
6598            (prog1
6599                (setq parent (copy-keymap parent))
6600              (set-keymap-parent parent (current-local-map)))
6601          (current-local-map)))
6602       (set-keymap-parent map (key-binding "S"))
6603       (let (key def gnus-pick-mode)
6604         (while sumkeys
6605           (setq key (pop sumkeys))
6606           (cond ((and (vectorp key) (= (length key) 1)
6607                       (consp (setq def (aref key 0)))
6608                       (numberp (car def)) (numberp (cdr def)))
6609                  (when (< (max (car def) (cdr def)) 128)
6610                    (setq sumkeys
6611                          (append (mapcar
6612                                   #'vector
6613                                   (nreverse (gnus-uncompress-range def)))
6614                                  sumkeys))))
6615                 ((setq def (key-binding key))
6616                  (unless (eq def 'undefined)
6617                    (define-key keymap key def))))))
6618       (when (boundp 'gnus-agent-summary-mode)
6619         (setq agent gnus-agent-summary-mode))
6620       (when (boundp 'gnus-draft-mode)
6621         (setq draft gnus-draft-mode)))
6622     (with-temp-buffer
6623       (use-local-map keymap)
6624       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6625       (set (make-local-variable 'gnus-draft-mode) draft)
6626       (describe-bindings prefix))
6627     (let ((item `((lambda (prefix)
6628                     (with-current-buffer ,(current-buffer)
6629                       (gnus-article-describe-bindings prefix)))
6630                   ,prefix)))
6631       (with-current-buffer (let (help-xref-following) (help-buffer))
6632         (setq help-xref-stack-item item)))))
6633
6634 (defun gnus-article-reply-with-original (&optional wide)
6635   "Start composing a reply mail to the current message.
6636 The text in the region will be yanked.  If the region isn't active,
6637 the entire article will be yanked."
6638   (interactive)
6639   (let ((article (cdr gnus-article-current))
6640         contents)
6641     (if (not (gnus-region-active-p))
6642         (with-current-buffer gnus-summary-buffer
6643           (gnus-summary-reply (list (list article)) wide))
6644       (setq contents (buffer-substring (point) (mark t)))
6645       ;; Deactivate active regions.
6646       (when (and (boundp 'transient-mark-mode)
6647                  transient-mark-mode)
6648         (setq mark-active nil))
6649       (with-current-buffer gnus-summary-buffer
6650         (gnus-summary-reply
6651          (list (list article contents)) wide)))))
6652
6653 (defun gnus-article-wide-reply-with-original ()
6654   "Start composing a wide reply mail to the current message.
6655 The text in the region will be yanked.  If the region isn't active,
6656 the entire article will be yanked."
6657   (interactive)
6658   (gnus-article-reply-with-original t))
6659
6660 (defun gnus-article-followup-with-original ()
6661   "Compose a followup to the current article.
6662 The text in the region will be yanked.  If the region isn't active,
6663 the entire article will be yanked."
6664   (interactive)
6665   (let ((article (cdr gnus-article-current))
6666         contents)
6667       (if (not (gnus-region-active-p))
6668           (with-current-buffer gnus-summary-buffer
6669             (gnus-summary-followup (list (list article))))
6670         (setq contents (buffer-substring (point) (mark t)))
6671         ;; Deactivate active regions.
6672         (when (and (boundp 'transient-mark-mode)
6673                    transient-mark-mode)
6674           (setq mark-active nil))
6675         (with-current-buffer gnus-summary-buffer
6676           (gnus-summary-followup
6677            (list (list article contents)))))))
6678
6679 (defun gnus-article-hide (&optional arg force)
6680   "Hide all the gruft in the current article.
6681 This means that signatures, cited text and (some) headers will be
6682 hidden.
6683 If given a prefix, show the hidden text instead."
6684   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6685   (gnus-article-hide-headers arg)
6686   (gnus-article-hide-list-identifiers arg)
6687   (gnus-article-hide-citation-maybe arg force)
6688   (gnus-article-hide-signature arg))
6689
6690 (defun gnus-article-maybe-highlight ()
6691   "Do some article highlighting if article highlighting is requested."
6692   (when (gnus-visual-p 'article-highlight 'highlight)
6693     (gnus-article-highlight-some)))
6694
6695 (defun gnus-check-group-server ()
6696   ;; Make sure the connection to the server is alive.
6697   (unless (gnus-server-opened
6698            (gnus-find-method-for-group gnus-newsgroup-name))
6699     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6700     (gnus-request-group gnus-newsgroup-name t)))
6701
6702 (eval-when-compile
6703   (autoload 'nneething-get-file-name "nneething"))
6704
6705 (defun gnus-request-article-this-buffer (article group)
6706   "Get an article and insert it into this buffer."
6707   (let (do-update-line sparse-header)
6708     (prog1
6709         (save-excursion
6710           (erase-buffer)
6711           (gnus-kill-all-overlays)
6712           (setq group (or group gnus-newsgroup-name))
6713
6714           ;; Using `gnus-request-article' directly will insert the article into
6715           ;; `nntp-server-buffer' - so we'll save some time by not having to
6716           ;; copy it from the server buffer into the article buffer.
6717
6718           ;; We only request an article by message-id when we do not have the
6719           ;; headers for it, so we'll have to get those.
6720           (when (stringp article)
6721             (gnus-read-header article))
6722
6723           ;; If the article number is negative, that means that this article
6724           ;; doesn't belong in this newsgroup (possibly), so we find its
6725           ;; message-id and request it by id instead of number.
6726           (when (and (numberp article)
6727                      gnus-summary-buffer
6728                      (get-buffer gnus-summary-buffer)
6729                      (gnus-buffer-exists-p gnus-summary-buffer))
6730             (with-current-buffer gnus-summary-buffer
6731               (let ((header (gnus-summary-article-header article)))
6732                 (when (< article 0)
6733                   (cond
6734                    ((memq article gnus-newsgroup-sparse)
6735                     ;; This is a sparse gap article.
6736                     (setq do-update-line article)
6737                     (setq article (mail-header-id header))
6738                     (setq sparse-header (gnus-read-header article))
6739                     (setq gnus-newsgroup-sparse
6740                           (delq article gnus-newsgroup-sparse)))
6741                    ((vectorp header)
6742                     ;; It's a real article.
6743                     (setq article (mail-header-id header)))
6744                    (t
6745                     ;; It is an extracted pseudo-article.
6746                     (setq article 'pseudo)
6747                     (gnus-request-pseudo-article header))))
6748
6749                 (let ((method (gnus-find-method-for-group
6750                                gnus-newsgroup-name)))
6751                   (when (and (eq (car method) 'nneething)
6752                              (vectorp header))
6753                     (let ((dir (nneething-get-file-name
6754                                 (mail-header-id header))))
6755                       (when (and (stringp dir)
6756                                  (file-directory-p dir))
6757                         (setq article 'nneething)
6758                         (gnus-group-enter-directory dir))))))))
6759
6760           (cond
6761            ;; Refuse to select canceled articles.
6762            ((and (numberp article)
6763                  gnus-summary-buffer
6764                  (get-buffer gnus-summary-buffer)
6765                  (gnus-buffer-exists-p gnus-summary-buffer)
6766                  (eq (cdr (with-current-buffer gnus-summary-buffer
6767                             (assq article gnus-newsgroup-reads)))
6768                      gnus-canceled-mark))
6769             nil)
6770            ;; We first check `gnus-original-article-buffer'.
6771            ((and (get-buffer gnus-original-article-buffer)
6772                  (numberp article)
6773                  (with-current-buffer gnus-original-article-buffer
6774                    (and (equal (car gnus-original-article) group)
6775                         (eq (cdr gnus-original-article) article))))
6776             ;; `insert-buffer-substring' would incorrectly use the
6777             ;; equivalent of string-make-multibyte which amount to decoding
6778             ;; with locale-coding-system, causing failure of
6779             ;; subsequent decoding.
6780             (insert (mm-string-to-multibyte
6781                      (with-current-buffer gnus-original-article-buffer
6782                        (buffer-substring (point-min) (point-max)))))
6783             'article)
6784            ;; Check the backlog.
6785            ((and gnus-keep-backlog
6786                  (gnus-backlog-request-article group article (current-buffer)))
6787             'article)
6788            ;; Check asynchronous pre-fetch.
6789            ((gnus-async-request-fetched-article group article (current-buffer))
6790             (gnus-async-prefetch-next group article gnus-summary-buffer)
6791             (when (and (numberp article) gnus-keep-backlog)
6792               (gnus-backlog-enter-article group article (current-buffer)))
6793             'article)
6794            ;; Check the cache.
6795            ((and gnus-use-cache
6796                  (numberp article)
6797                  (gnus-cache-request-article article group))
6798             'article)
6799            ;; Check the agent cache.
6800            ((gnus-agent-request-article article group)
6801             'article)
6802            ;; Get the article and put into the article buffer.
6803            ((or (stringp article)
6804                 (numberp article))
6805             (let ((gnus-override-method gnus-override-method)
6806                   (methods (and (stringp article)
6807                                 gnus-refer-article-method))
6808                   (backend (car (gnus-find-method-for-group
6809                                  gnus-newsgroup-name)))
6810                   result
6811                   (inhibit-read-only t))
6812               (if (or (not (listp methods))
6813                       (and (symbolp (car methods))
6814                            (assq (car methods) nnoo-definition-alist)))
6815                   (setq methods (list methods)))
6816               (when (and (null gnus-override-method)
6817                          methods)
6818                 (setq gnus-override-method (pop methods)))
6819               (while (not result)
6820                 (when (eq gnus-override-method 'current)
6821                   (setq gnus-override-method
6822                         (with-current-buffer gnus-summary-buffer
6823                           gnus-current-select-method)))
6824                 (erase-buffer)
6825                 (gnus-kill-all-overlays)
6826                 (let ((gnus-newsgroup-name group))
6827                   (gnus-check-group-server))
6828                 (cond
6829                  ((gnus-request-article article group (current-buffer))
6830                   (when (numberp article)
6831                     (gnus-async-prefetch-next group article
6832                                               gnus-summary-buffer)
6833                     (when gnus-keep-backlog
6834                       (gnus-backlog-enter-article
6835                        group article (current-buffer))))
6836                   (setq result 'article))
6837                  (methods
6838                   (setq gnus-override-method (pop methods)))
6839                  ((not (string-match "^400 "
6840                                      (nnheader-get-report backend)))
6841                   ;; If we get 400 server disconnect, reconnect and
6842                   ;; retry; otherwise, assume the article has expired.
6843                   (setq result 'done))))
6844               (and (eq result 'article) 'article)))
6845            ;; It was a pseudo.
6846            (t article)))
6847
6848       ;; Associate this article with the current summary buffer.
6849       (setq gnus-article-current-summary gnus-summary-buffer)
6850
6851       ;; Take the article from the original article buffer
6852       ;; and place it in the buffer it's supposed to be in.
6853       (when (and (get-buffer gnus-article-buffer)
6854                  (equal (buffer-name (current-buffer))
6855                         (buffer-name (get-buffer gnus-article-buffer))))
6856         (save-excursion
6857           (if (get-buffer gnus-original-article-buffer)
6858               (set-buffer gnus-original-article-buffer)
6859             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6860             (buffer-disable-undo)
6861             (setq major-mode 'gnus-original-article-mode)
6862             (setq buffer-read-only t))
6863           (let ((inhibit-read-only t))
6864             (erase-buffer)
6865             (insert-buffer-substring gnus-article-buffer))
6866           (setq gnus-original-article (cons group article)))
6867
6868         ;; Decode charsets.
6869         (run-hooks 'gnus-article-decode-hook)
6870         ;; Mark article as decoded or not.
6871         (setq gnus-article-decoded-p gnus-article-decode-hook))
6872
6873       ;; Update sparse articles.
6874       (when (and do-update-line
6875                  (or (numberp article)
6876                      (stringp article)))
6877         (let ((buf (current-buffer)))
6878           (set-buffer gnus-summary-buffer)
6879           (gnus-summary-update-article do-update-line sparse-header)
6880           (gnus-summary-goto-subject do-update-line nil t)
6881           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6882                             (point))
6883           (set-buffer buf))))))
6884
6885 (defun gnus-block-private-groups (group)
6886   (if (gnus-news-group-p group)
6887       ;; Block nothing in news groups.
6888       nil
6889     ;; Block everything anywhere else.
6890     "."))
6891
6892 (defun gnus-blocked-images ()
6893   (if (functionp gnus-blocked-images)
6894       (funcall gnus-blocked-images gnus-newsgroup-name)
6895     gnus-blocked-images))
6896
6897 ;;;
6898 ;;; Article editing
6899 ;;;
6900
6901 (defcustom gnus-article-edit-mode-hook nil
6902   "Hook run in article edit mode buffers."
6903   :group 'gnus-article-various
6904   :type 'hook)
6905
6906 (defvar gnus-article-edit-done-function nil)
6907
6908 (defvar gnus-article-edit-mode-map nil)
6909
6910 ;; Should we be using derived.el for this?
6911 (unless gnus-article-edit-mode-map
6912   (setq gnus-article-edit-mode-map (make-keymap))
6913   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6914
6915   (gnus-define-keys gnus-article-edit-mode-map
6916     "\C-c?"    describe-mode
6917     "\C-c\C-c" gnus-article-edit-done
6918     "\C-c\C-k" gnus-article-edit-exit
6919     "\C-c\C-f\C-t" message-goto-to
6920     "\C-c\C-f\C-o" message-goto-from
6921     "\C-c\C-f\C-b" message-goto-bcc
6922     ;;"\C-c\C-f\C-w" message-goto-fcc
6923     "\C-c\C-f\C-c" message-goto-cc
6924     "\C-c\C-f\C-s" message-goto-subject
6925     "\C-c\C-f\C-r" message-goto-reply-to
6926     "\C-c\C-f\C-n" message-goto-newsgroups
6927     "\C-c\C-f\C-d" message-goto-distribution
6928     "\C-c\C-f\C-f" message-goto-followup-to
6929     "\C-c\C-f\C-m" message-goto-mail-followup-to
6930     "\C-c\C-f\C-k" message-goto-keywords
6931     "\C-c\C-f\C-u" message-goto-summary
6932     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6933     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6934     "\C-c\C-b" message-goto-body
6935     "\C-c\C-i" message-goto-signature
6936
6937     "\C-c\C-t" message-insert-to
6938     "\C-c\C-n" message-insert-newsgroups
6939     "\C-c\C-o" message-sort-headers
6940     "\C-c\C-e" message-elide-region
6941     "\C-c\C-v" message-delete-not-region
6942     "\C-c\C-z" message-kill-to-signature
6943     "\M-\r" message-newline-and-reformat
6944     "\C-c\C-a" mml-attach-file
6945     "\C-a" message-beginning-of-line
6946     "\t" message-tab
6947     "\M-;" comment-region)
6948
6949   (gnus-define-keys (gnus-article-edit-wash-map
6950                      "\C-c\C-w" gnus-article-edit-mode-map)
6951     "f" gnus-article-edit-full-stops))
6952
6953 (easy-menu-define
6954   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6955   '("Field"
6956     ["Fetch To" message-insert-to t]
6957     ["Fetch Newsgroups" message-insert-newsgroups t]
6958     "----"
6959     ["To" message-goto-to t]
6960     ["From" message-goto-from t]
6961     ["Subject" message-goto-subject t]
6962     ["Cc" message-goto-cc t]
6963     ["Reply-To" message-goto-reply-to t]
6964     ["Summary" message-goto-summary t]
6965     ["Keywords" message-goto-keywords t]
6966     ["Newsgroups" message-goto-newsgroups t]
6967     ["Followup-To" message-goto-followup-to t]
6968     ["Mail-Followup-To" message-goto-mail-followup-to t]
6969     ["Distribution" message-goto-distribution t]
6970     ["Body" message-goto-body t]
6971     ["Signature" message-goto-signature t]))
6972
6973 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6974   "Major mode for editing articles.
6975 This is an extended text-mode.
6976
6977 \\{gnus-article-edit-mode-map}"
6978   (make-local-variable 'gnus-article-edit-done-function)
6979   (make-local-variable 'gnus-prev-winconf)
6980   (set (make-local-variable 'font-lock-defaults)
6981        '(message-font-lock-keywords t))
6982   (set (make-local-variable 'mail-header-separator) "")
6983   (set (make-local-variable 'gnus-article-edit-mode) t)
6984   (easy-menu-add message-mode-field-menu message-mode-map)
6985   (mml-mode)
6986   (setq buffer-read-only nil)
6987   (buffer-enable-undo)
6988   (widen))
6989
6990 (defun gnus-article-edit (&optional force)
6991   "Edit the current article.
6992 This will have permanent effect only in mail groups.
6993 If FORCE is non-nil, allow editing of articles even in read-only
6994 groups."
6995   (interactive "P")
6996   (when (and (not force)
6997              (gnus-group-read-only-p))
6998     (error "The current newsgroup does not support article editing"))
6999   (gnus-article-date-original)
7000   (gnus-article-edit-article
7001    'ignore
7002    `(lambda (no-highlight)
7003       'ignore
7004       (gnus-summary-edit-article-done
7005        ,(or (mail-header-references gnus-current-headers) "")
7006        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7007
7008 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7009   "Start editing the contents of the current article buffer."
7010   (let ((winconf (current-window-configuration)))
7011     (set-buffer gnus-article-buffer)
7012     (let ((message-auto-save-directory
7013            ;; Don't associate the article buffer with a draft file.
7014            nil))
7015       (gnus-article-edit-mode))
7016     (funcall start-func)
7017     (set-buffer-modified-p nil)
7018     (gnus-configure-windows 'edit-article)
7019     (setq gnus-article-edit-done-function exit-func)
7020     (setq gnus-prev-winconf winconf)
7021     (unless quiet
7022       (gnus-message 6 "C-c C-c to end edits"))))
7023
7024 (defun gnus-article-edit-done (&optional arg)
7025   "Update the article edits and exit."
7026   (interactive "P")
7027   (let ((func gnus-article-edit-done-function)
7028         (buf (current-buffer))
7029         (start (window-start))
7030         (p (point))
7031         (winconf gnus-prev-winconf))
7032     (widen) ;; Widen it in case that users narrowed the buffer.
7033     (funcall func arg)
7034     (set-buffer buf)
7035     ;; The cache and backlog have to be flushed somewhat.
7036     (when gnus-keep-backlog
7037       (gnus-backlog-remove-article
7038        (car gnus-article-current) (cdr gnus-article-current)))
7039     ;; Flush original article as well.
7040     (gnus-flush-original-article-buffer)
7041     (when gnus-use-cache
7042       (gnus-cache-update-article
7043        (car gnus-article-current) (cdr gnus-article-current)))
7044     ;; We remove all text props from the article buffer.
7045     (kill-all-local-variables)
7046     (set-text-properties (point-min) (point-max) nil)
7047     (gnus-article-mode)
7048     (set-window-configuration winconf)
7049     (set-buffer buf)
7050     (set-window-start (get-buffer-window buf) start)
7051     (set-window-point (get-buffer-window buf) (point)))
7052   (gnus-summary-show-article))
7053
7054 (defun gnus-flush-original-article-buffer ()
7055   (when (get-buffer gnus-original-article-buffer)
7056     (with-current-buffer gnus-original-article-buffer
7057       (setq gnus-original-article nil))))
7058
7059 (defun gnus-article-edit-exit ()
7060   "Exit the article editing without updating."
7061   (interactive)
7062   (when (or (not (buffer-modified-p))
7063             (yes-or-no-p "Article modified; kill anyway? "))
7064     (let ((curbuf (current-buffer))
7065           (p (point))
7066           (window-start (window-start)))
7067       (erase-buffer)
7068       (if (gnus-buffer-live-p gnus-original-article-buffer)
7069           (insert-buffer-substring gnus-original-article-buffer))
7070       (let ((winconf gnus-prev-winconf))
7071         (kill-all-local-variables)
7072         (gnus-article-mode)
7073         (set-window-configuration winconf)
7074         ;; Tippy-toe some to make sure that point remains where it was.
7075         (save-current-buffer
7076           (set-buffer curbuf)
7077           (set-window-start (get-buffer-window (current-buffer)) window-start)
7078           (goto-char p))))
7079     (gnus-summary-show-article)))
7080
7081 (defun gnus-article-edit-full-stops ()
7082   "Interactively repair spacing at end of sentences."
7083   (interactive)
7084   (save-excursion
7085     (goto-char (point-min))
7086     (search-forward-regexp "^$" nil t)
7087     (let ((case-fold-search nil))
7088       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7089
7090 ;;;
7091 ;;; Article highlights
7092 ;;;
7093
7094 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7095
7096 ;;; Internal Variables:
7097
7098 (defcustom gnus-button-url-regexp
7099   (concat
7100    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7101    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7102    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7103    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7104        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7105              (punct "!?:;.,"))
7106          (concat
7107           "\\(?:"
7108           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7109           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7110           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7111           "\\|"
7112           "[" chars punct     "]+" "[" chars "]"
7113           "\\)"))
7114      (concat ;; XEmacs 21.4 doesn't support POSIX.
7115       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7116       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7117    "\\)")
7118   "Regular expression that matches URLs."
7119   :group 'gnus-article-buttons
7120   :type 'regexp)
7121
7122 (defcustom gnus-button-valid-fqdn-regexp
7123   message-valid-fqdn-regexp
7124   "Regular expression that matches a valid FQDN."
7125   :version "22.1"
7126   :group 'gnus-article-buttons
7127   :type 'regexp)
7128
7129 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7130 (defcustom gnus-button-valid-localpart-regexp
7131   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7132   "Regular expression that matches a localpart of mail addresses or MIDs."
7133   :version "22.1"
7134   :group 'gnus-article-buttons
7135   :type 'regexp)
7136
7137 (defcustom gnus-button-man-handler 'manual-entry
7138   "Function to use for displaying man pages.
7139 The function must take at least one argument with a string naming the
7140 man page."
7141   :version "22.1"
7142   :type '(choice (function-item :tag "Man" manual-entry)
7143                  (function-item :tag "Woman" woman)
7144                  (function :tag "Other"))
7145   :group 'gnus-article-buttons)
7146
7147 (defcustom gnus-button-mid-or-mail-regexp
7148   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7149           gnus-button-valid-fqdn-regexp
7150           ">?\\)\\b")
7151   "Regular expression that matches a message ID or a mail address."
7152   :version "22.1"
7153   :group 'gnus-article-buttons
7154   :type 'regexp)
7155
7156 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7157   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7158 Strings like this can be either a message ID or a mail address.  If it is one
7159 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7160 message ID or a mail address, respectively.  If this variable is set to the
7161 symbol `ask', always query the user what do do.  If it is a function, this
7162 function will be called with the string as its only argument.  The function
7163 must return `mid', `mail', `invalid' or `ask'."
7164   :version "22.1"
7165   :group 'gnus-article-buttons
7166   :type '(choice (function-item :tag "Heuristic function"
7167                                 gnus-button-mid-or-mail-heuristic)
7168                  (const ask)
7169                  (const mid)
7170                  (const mail)))
7171
7172 (defcustom gnus-button-mid-or-mail-heuristic-alist
7173   '((-10.0 . ".+\\$.+@")
7174     (-10.0 . "#")
7175     (-10.0 . "\\*")
7176     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7177     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7178     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7179     (-1.0  . "^[^a-z]+@")
7180     ;;
7181     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7182     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7183     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7184     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7185     ;;
7186     (-2.0  . "^[0-9]")
7187     (-1.0  . "^[0-9][0-9]")
7188     ;;
7189     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7190     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7191     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7192     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7193     ;;
7194     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7195     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7196     ;;       "[0-9]{8,}.*\@"
7197     (-3.0
7198      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7199     ;; "[0-9]{12,}.*\@"
7200     ;; compensation for TDMA dated mail addresses:
7201     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7202     ;;
7203     (-20.0 . "\\.fsf@") ;; Gnus
7204     (-20.0 . "^slrn")
7205     (-20.0 . "^Pine")
7206     (-20.0 . "^alpine\\.")
7207     (-20.0 . "_-_") ;; Subject change in thread
7208     ;;
7209     (-20.0 . "\\.ln@") ;; leafnode
7210     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7211     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7212     ;;
7213     ;; (5.0 . "") ;; $local_part_len <= 7
7214     (10.0  . "^[^0-9]+@")
7215     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7216     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7217     (3.0   . "\@stud")
7218     ;;
7219     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7220     ;;
7221     (0.5   . "^[A-Z][a-z]")
7222     (0.5   . "^[A-Z][a-z][a-z]")
7223     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7224     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7225   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7226
7227 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7228 address.  The REGEXP is processed with `case-fold-search' set to nil."
7229   :version "22.1"
7230   :group 'gnus-article-buttons
7231   :type '(repeat (cons (number :tag "Rate")
7232                        (regexp :tag "Regexp"))))
7233
7234 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7235   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7236 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7237 address, `ask' if unsure and `invalid' if the string is invalid."
7238   (let ((case-fold-search nil)
7239         (list gnus-button-mid-or-mail-heuristic-alist)
7240         (result 0) rate regexp lpartlen elem)
7241     (setq lpartlen
7242           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7243     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7244     ;; Certain special cases...
7245     (when (string-match
7246            (concat
7247             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7248             "^[0-9]+\\.[0-9]+@compuserve\\|"
7249             "@public\\.gmane\\.org")
7250            mid-or-mail)
7251       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7252       (setq result 'mail))
7253     (when (string-match "@.*@\\| " mid-or-mail)
7254       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7255       (setq result 'invalid))
7256     ;; Nothing more to do, if result is not a number here...
7257     (when (numberp result)
7258       (while list
7259         (setq elem (car list)
7260               rate (car elem)
7261               regexp (cdr elem)
7262               list (cdr list))
7263         (when (string-match regexp mid-or-mail)
7264           (setq result (+ result rate))
7265           (gnus-message
7266            9 "`%s' matched `%s', rate `%s', result `%s'."
7267            mid-or-mail regexp rate result)))
7268       (when (<= lpartlen 7)
7269         (setq result (+ result 5.0))
7270         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7271                       mid-or-mail result))
7272       (when (>= lpartlen 12)
7273         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7274         (cond
7275          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7276           ;; Long local part should contain realname if e-mail address,
7277           ;; too many digits: message-id.
7278           ;; $score -= 5.0 + 0.1 * $local_part_len;
7279           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7280           (setq result (+ result rate))
7281           (gnus-message
7282            9 "Many digits in `%s', rate `%s', result `%s'."
7283            mid-or-mail rate result))
7284          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7285                         mid-or-mail)
7286           ;; Too few vowels [^aeiouy]{4,}.*\@
7287           (setq result (+ result -5.0))
7288           (gnus-message
7289            9 "Few vowels in `%s', rate `%s', result `%s'."
7290            mid-or-mail -5.0 result))
7291          (t
7292           (setq result (+ result 5.0))
7293           (gnus-message
7294            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7295     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7296     ;; Maybe we should make this a customizable alist: (condition . 'result)
7297     (cond
7298      ((symbolp result) result)
7299      ;; Now convert number into proper results:
7300      ((< result -10.0) 'mid)
7301      ((> result  10.0) 'mail)
7302      (t 'ask))))
7303
7304 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7305   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7306          (url-mid (concat "news" ":" mid-or-mail))
7307          (url-mailto (concat "mailto" ":" mid-or-mail)))
7308     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7309     (when (fboundp pref)
7310       (setq guessed
7311             ;; get rid of surrounding angles...
7312             (funcall pref
7313                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7314       (if (or (eq 'mid guessed) (eq 'mail guessed))
7315           (setq pref guessed)
7316         (setq pref 'ask)))
7317     (if (eq pref 'ask)
7318         (save-window-excursion
7319           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7320               (setq pref 'mail)
7321             (setq pref 'mid))))
7322     (cond ((eq pref 'mid)
7323            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7324            (gnus-button-handle-news url-mid))
7325           ((eq pref 'mail)
7326            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7327            (gnus-url-mailto url-mailto))
7328           (t (gnus-message 3 "Invalid string.")))))
7329
7330 (defun gnus-button-handle-custom (fun arg)
7331   "Call function FUN on argument ARG.
7332 Both FUN and ARG are supposed to be strings.  ARG will be passed
7333 as a symbol to FUN."
7334   (funcall (intern fun)
7335            (if (string-match "^customize-apropos" fun)
7336                arg
7337              (intern arg))))
7338
7339 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7340
7341 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7342 ;; stuff?
7343
7344 (defun gnus-button-handle-describe-function (url)
7345   "Call `describe-function' when pushing the corresponding URL button."
7346   (describe-function
7347    (intern
7348     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7349
7350 (defun gnus-button-handle-describe-variable (url)
7351   "Call `describe-variable' when pushing the corresponding URL button."
7352   (describe-variable
7353    (intern
7354     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7355
7356 (defun gnus-button-handle-symbol (url)
7357 "Display help on variable or function.
7358 Calls `describe-variable' or `describe-function'."
7359   (let ((sym (intern url)))
7360     (cond
7361      ((fboundp sym) (describe-function sym))
7362      ((boundp sym) (describe-variable sym))
7363      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7364
7365 (defun gnus-button-handle-describe-key (url)
7366   "Call `describe-key' when pushing the corresponding URL button."
7367   (let* ((key-string
7368           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7369          (keys (ignore-errors (eval `(kbd ,key-string)))))
7370     (if keys
7371         (describe-key keys)
7372       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7373
7374 (defun gnus-button-handle-apropos (url)
7375   "Call `apropos' when pushing the corresponding URL button."
7376   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7377
7378 (defun gnus-button-handle-apropos-command (url)
7379   "Call `apropos' when pushing the corresponding URL button."
7380   (apropos-command
7381    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7382
7383 (defun gnus-button-handle-apropos-variable (url)
7384   "Call `apropos' when pushing the corresponding URL button."
7385   (funcall
7386    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7387    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7388
7389 (defun gnus-button-handle-apropos-documentation (url)
7390   "Call `apropos' when pushing the corresponding URL button."
7391   (funcall
7392    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7393    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7394
7395 (defun gnus-button-handle-library (url)
7396   "Call `locate-library' when pushing the corresponding URL button."
7397   (gnus-message 9 "url=`%s'" url)
7398   (let* ((lib (locate-library url))
7399          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7400     (if (not lib)
7401         (gnus-message 1 "Cannot locale library `%s'." url)
7402       (find-file-read-only file))))
7403
7404 (defcustom gnus-button-man-level 5
7405   "*Integer that says how many man-related buttons Gnus will show.
7406 The higher the number, the more buttons will appear and the more false
7407 positives are possible.  Note that you can set this variable local to
7408 specific groups.  Setting it higher in Unix groups is probably a good idea.
7409 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7410 how to set variables in specific groups."
7411   :version "22.1"
7412   :group 'gnus-article-buttons
7413   :link '(custom-manual "(gnus)Group Parameters")
7414   :type 'integer)
7415
7416 (defcustom gnus-button-emacs-level 5
7417   "*Integer that says how many emacs-related buttons Gnus will show.
7418 The higher the number, the more buttons will appear and the more false
7419 positives are possible.  Note that you can set this variable local to
7420 specific groups.  Setting it higher in Emacs or Gnus related groups is
7421 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7422 `gnus-parameters' on how to set variables in specific groups."
7423   :version "22.1"
7424   :group 'gnus-article-buttons
7425   :link '(custom-manual "(gnus)Group Parameters")
7426   :type 'integer)
7427
7428 (defcustom gnus-button-message-level 5
7429   "*Integer that says how many buttons for news or mail messages will appear.
7430 The higher the number, the more buttons will appear and the more false
7431 positives are possible."
7432   ;; mail addresses, MIDs, URLs for news, ...
7433   :version "22.1"
7434   :group 'gnus-article-buttons
7435   :type 'integer)
7436
7437 (defcustom gnus-button-browse-level 5
7438   "*Integer that says how many buttons for browsing will appear.
7439 The higher the number, the more buttons will appear and the more false
7440 positives are possible."
7441   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7442   :version "22.1"
7443   :group 'gnus-article-buttons
7444   :type 'integer)
7445
7446 (defcustom gnus-button-alist
7447   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7448      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7449     ((concat "\\b\\(nntp\\|news\\):\\("
7450              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7451      0 t gnus-button-handle-news 2)
7452     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7453      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7454     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7455      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7456     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7457     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7458      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7459     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7460      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7461     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7462      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7463     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7464      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7465     ;; RFC 2368 (The mailto URL scheme)
7466     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7467      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7468     ("\\bmailto:\\([^ \n\t]+\\)"
7469      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7470     ;; Info Konqueror style <info:/foo/bar baz>.
7471     ;; Must come before " Gnus home-grown style".
7472     ("\\binfo://?\\([^'\">\n\t]+\\)"
7473      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7474    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7475     ("\\binfo://\\([^'\">\n\t ]+\\)"
7476      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7477     ;; Info GNOME style <info:foo#bar_baz>
7478     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7479      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7480     ;; Info KDE style <info:(foo)bar baz>
7481     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7482      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7483     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7484      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7485     ("\\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,]*\\)\\)?"
7486      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7487      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7488     ;; This is custom
7489     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET\\>" 0
7490      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7491     ;; Emacs help commands
7492     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7493      ;; regexp doesn't match arguments containing ` '.
7494      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7495     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7496      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7497     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7498      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7499     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7500      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7501     ;; The following entries may lead to many false positives so don't enable
7502     ;; them by default (use a high button level).
7503     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7504      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7505      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7506     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7507      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7508     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7509      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7510     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7511      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7512     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7513      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7514     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7515      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7516     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7517      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7518     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET\\>"
7519      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7520     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7521      ;; Unlike the other regexps we really have to require quoting
7522      ;; here to determine where it ends.
7523      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7524     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7525     ("<URL: *\\([^\n<>]*\\)>"
7526      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7527     ;; RFC 2396 (2.4.3., delims) ...
7528     ("\"URL: *\\([^\n\"]*\\)\""
7529      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7530     ;; Raw URLs.
7531     (gnus-button-url-regexp
7532      0 (>= gnus-button-browse-level 0) browse-url 0)
7533     ;; man pages
7534     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7535      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7536      gnus-button-handle-man 1)
7537     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7538     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7539      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7540      gnus-button-handle-man 1)
7541     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7542     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7543     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7544      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7545     ;; Recognizing patches to .el files.  This is somewhat obscure,
7546     ;; but considering the percentage of Gnus users who hack Emacs
7547     ;; Lisp files...
7548     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7549      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7550     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7551      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7552     ;; MID or mail: To avoid too many false positives we don't try to catch
7553     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7554     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7555     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7556     ;; so that non-ambiguous entries (see above) match first.
7557     (gnus-button-mid-or-mail-regexp
7558      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7559   "*Alist of regexps matching buttons in article bodies.
7560
7561 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7562 REGEXP: is the string (case insensitive) matching text around the button (can
7563 also be Lisp expression evaluating to a string),
7564 BUTTON: is the number of the regexp grouping actually matching the button,
7565 FORM: is a Lisp expression which must eval to true for the button to
7566 be added,
7567 CALLBACK: is the function to call when the user push this button, and each
7568 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7569
7570 CALLBACK can also be a variable, in that case the value of that
7571 variable it the real callback function."
7572   :group 'gnus-article-buttons
7573   :type '(repeat (list (choice regexp variable sexp)
7574                        (integer :tag "Button")
7575                        (sexp :tag "Form")
7576                        (function :tag "Callback")
7577                        (repeat :tag "Par"
7578                                :inline t
7579                                (integer :tag "Regexp group")))))
7580 (put 'gnus-button-alist 'risky-local-variable t)
7581
7582 (defcustom gnus-header-button-alist
7583   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7584      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7585     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7586      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7587     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7588      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7589     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7590      0 (>= gnus-button-browse-level 0) browse-url 0)
7591     ("^Subject:" gnus-button-url-regexp
7592      0 (>= gnus-button-browse-level 0) browse-url 0)
7593     ("^[^:]+:" gnus-button-url-regexp
7594      0 (>= gnus-button-browse-level 0) browse-url 0)
7595     ("^OpenPGP:.*url=" gnus-button-url-regexp
7596      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7597     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7598      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7599     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7600      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7601   "*Alist of headers and regexps to match buttons in article heads.
7602
7603 This alist is very similar to `gnus-button-alist', except that each
7604 alist has an additional HEADER element first in each entry:
7605
7606 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7607
7608 HEADER is a regexp to match a header.  For a fuller explanation, see
7609 `gnus-button-alist'."
7610   :group 'gnus-article-buttons
7611   :group 'gnus-article-headers
7612   :type '(repeat (list (regexp :tag "Header")
7613                        (choice regexp variable)
7614                        (integer :tag "Button")
7615                        (sexp :tag "Form")
7616                        (function :tag "Callback")
7617                        (repeat :tag "Par"
7618                                :inline t
7619                                (integer :tag "Regexp group")))))
7620 (put 'gnus-header-button-alist 'risky-local-variable t)
7621
7622 ;;; Commands:
7623
7624 (defun gnus-article-push-button (event)
7625   "Check text under the mouse pointer for a callback function.
7626 If the text under the mouse pointer has a `gnus-callback' property,
7627 call it with the value of the `gnus-data' text property."
7628   (interactive "e")
7629   (set-buffer (window-buffer (posn-window (event-start event))))
7630   (let* ((pos (posn-point (event-start event)))
7631          (data (get-text-property pos 'gnus-data))
7632          (fun (get-text-property pos 'gnus-callback)))
7633     (goto-char pos)
7634     (when fun
7635       (funcall fun data))))
7636
7637 (defun gnus-article-press-button ()
7638   "Check text at point for a callback function.
7639 If the text at point has a `gnus-callback' property,
7640 call it with the value of the `gnus-data' text property."
7641   (interactive)
7642   (let ((data (get-text-property (point) 'gnus-data))
7643         (fun (get-text-property (point) 'gnus-callback)))
7644     (when fun
7645       (funcall fun data))))
7646
7647 (defun gnus-article-highlight (&optional force)
7648   "Highlight current article.
7649 This function calls `gnus-article-highlight-headers',
7650 `gnus-article-highlight-citation',
7651 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7652 do the highlighting.  See the documentation for those functions."
7653   (interactive (list 'force))
7654   (gnus-article-highlight-headers)
7655   (gnus-article-highlight-citation force)
7656   (gnus-article-highlight-signature)
7657   (gnus-article-add-buttons)
7658   (gnus-article-add-buttons-to-head))
7659
7660 (defun gnus-article-highlight-some (&optional force)
7661   "Highlight current article.
7662 This function calls `gnus-article-highlight-headers',
7663 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7664 do the highlighting.  See the documentation for those functions."
7665   (interactive (list 'force))
7666   (gnus-article-highlight-headers)
7667   (gnus-article-highlight-signature)
7668   (gnus-article-add-buttons))
7669
7670 (defun gnus-article-highlight-headers ()
7671   "Highlight article headers as specified by `gnus-header-face-alist'."
7672   (interactive)
7673   (gnus-with-article-headers
7674     (let (regexp header-face field-face from hpoints fpoints)
7675       (dolist (entry gnus-header-face-alist)
7676         (goto-char (point-min))
7677         (setq regexp (concat "^\\("
7678                              (if (string-equal "" (nth 0 entry))
7679                                  "[^\t ]"
7680                                (nth 0 entry))
7681                              "\\)")
7682               header-face (nth 1 entry)
7683               field-face (nth 2 entry))
7684         (while (and (re-search-forward regexp nil t)
7685                     (not (eobp)))
7686           (beginning-of-line)
7687           (setq from (point))
7688           (unless (search-forward ":" nil t)
7689             (forward-char 1))
7690           (when (and header-face
7691                      (not (memq (point) hpoints)))
7692             (push (point) hpoints)
7693             (gnus-put-text-property from (point) 'face header-face))
7694           (when (and field-face
7695                      (not (memq (setq from (point)) fpoints)))
7696             (push from fpoints)
7697             (if (re-search-forward "^[^ \t]" nil t)
7698                 (forward-char -2)
7699               (goto-char (point-max)))
7700             (gnus-put-text-property from (point) 'face field-face)))))))
7701
7702 (defun gnus-article-highlight-signature ()
7703   "Highlight the signature in an article.
7704 It does this by highlighting everything after
7705 `gnus-signature-separator' using the face `gnus-signature'."
7706   (interactive)
7707   (gnus-with-article-buffer
7708     (let ((inhibit-point-motion-hooks t))
7709       (save-restriction
7710         (when (and gnus-signature-face
7711                    (gnus-article-narrow-to-signature))
7712           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7713                             'face gnus-signature-face)
7714           (widen)
7715           (gnus-article-search-signature)
7716           (let ((start (match-beginning 0))
7717                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7718             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7719                                      end)))))))
7720
7721 (defun gnus-button-in-region-p (b e prop)
7722   "Say whether PROP exists in the region."
7723   (text-property-not-all b e prop nil))
7724
7725 (defun gnus-article-add-buttons ()
7726   "Find external references in the article and make buttons of them.
7727 \"External references\" are things like Message-IDs and URLs, as
7728 specified by `gnus-button-alist'."
7729   (interactive)
7730   (gnus-with-article-buffer
7731     (let ((inhibit-point-motion-hooks t)
7732           (case-fold-search t)
7733           (alist gnus-button-alist)
7734           beg entry regexp)
7735       ;; We skip the headers.
7736       (article-goto-body)
7737       (setq beg (point))
7738       (while (setq entry (pop alist))
7739         (setq regexp (eval (car entry)))
7740         (goto-char beg)
7741         (while (re-search-forward regexp nil t)
7742           (let ((start (match-beginning (nth 1 entry)))
7743                 (end (match-end (nth 1 entry)))
7744                 (from (match-beginning 0)))
7745             (when (and (eval (nth 2 entry))
7746                        (not (gnus-button-in-region-p
7747                              start end 'gnus-callback)))
7748               ;; That optional form returned non-nil, so we add the
7749               ;; button.
7750               (setq from (set-marker (make-marker) from))
7751               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7752                            (gnus-article-extend-url-button from start end))
7753                 (gnus-article-add-button start end
7754                                          'gnus-button-push (list from entry))
7755                 (gnus-put-text-property
7756                  start end
7757                  'gnus-string (buffer-substring-no-properties
7758                                start end))))))))))
7759
7760 (defun gnus-article-extend-url-button (beg start end)
7761   "Extend url button if url is folded into two or more lines.
7762 Return non-nil if button is extended.  BEG is a marker that points to
7763 the beginning position of a text containing url.  START and END are
7764 the endpoints of a url button before it is extended.  The concatenated
7765 url is put as the `gnus-button-url' overlay property on the button."
7766   (let ((opoint (point))
7767         (points (list start end))
7768         url delim regexp)
7769     (prog1
7770         (when (and (progn
7771                      (goto-char end)
7772                      (not (looking-at "[\t ]*[\">]")))
7773                    (progn
7774                      (goto-char start)
7775                      (string-match
7776                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7777                       (buffer-substring (point-at-bol) start)))
7778                    (progn
7779                      (setq url (list (buffer-substring start end))
7780                            delim (if (match-beginning 1) ">" "\""))
7781                      (beginning-of-line)
7782                      (setq regexp (concat
7783                                    (when (and (looking-at
7784                                                message-cite-prefix-regexp)
7785                                               (< (match-end 0) start))
7786                                      (regexp-quote (match-string 0)))
7787                                    "\
7788 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7789                                    delim "\\)"))
7790                      (while (progn
7791                               (forward-line 1)
7792                               (and (looking-at regexp)
7793                                    (prog1
7794                                        (match-beginning 1)
7795                                      (push (or (match-string 2)
7796                                                (match-string 1))
7797                                            url)
7798                                      (push (setq end (or (match-end 2)
7799                                                          (match-end 1)))
7800                                            points)
7801                                      (push (or (match-beginning 2)
7802                                                (match-beginning 1))
7803                                            points)))))
7804                      (match-beginning 2)))
7805           (let (gnus-article-mouse-face widget-mouse-face)
7806             (while points
7807               (gnus-article-add-button (pop points) (pop points)
7808                                        'gnus-button-push beg)))
7809           (let ((overlay (gnus-make-overlay start end)))
7810             (gnus-overlay-put overlay 'evaporate t)
7811             (gnus-overlay-put overlay 'gnus-button-url
7812                               (list (mapconcat 'identity (nreverse url) "")))
7813             (when gnus-article-mouse-face
7814               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7815           t)
7816       (goto-char opoint))))
7817
7818 ;; Add buttons to the head of an article.
7819 (defun gnus-article-add-buttons-to-head ()
7820   "Add buttons to the head of the article."
7821   (interactive)
7822   (gnus-with-article-headers
7823     (let (beg end)
7824       (dolist (entry gnus-header-button-alist)
7825         ;; Each alist entry.
7826         (goto-char (point-min))
7827         (while (re-search-forward (car entry) nil t)
7828           ;; Each header matching the entry.
7829           (setq beg (match-beginning 0))
7830           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7831                              (match-beginning 0))
7832                         (point-max)))
7833           (goto-char beg)
7834           (while (re-search-forward (eval (nth 1 entry)) end t)
7835             ;; Each match within a header.
7836             (let* ((entry (cdr entry))
7837                    (start (match-beginning (nth 1 entry)))
7838                    (end (match-end (nth 1 entry)))
7839                    (form (nth 2 entry)))
7840               (goto-char (match-end 0))
7841               (when (eval form)
7842                 (gnus-article-add-button
7843                  start end (nth 3 entry)
7844                  (buffer-substring (match-beginning (nth 4 entry))
7845                                    (match-end (nth 4 entry)))))))
7846           (goto-char end))))))
7847
7848 ;;; External functions:
7849
7850 (defun gnus-article-add-button (from to fun &optional data text)
7851   "Create a button between FROM and TO with callback FUN and data DATA."
7852   (when gnus-article-button-face
7853     (gnus-overlay-put (gnus-make-overlay from to nil t)
7854                       'face gnus-article-button-face))
7855   (gnus-add-text-properties
7856    from to
7857    (nconc (and gnus-article-mouse-face
7858                (list gnus-mouse-face-prop gnus-article-mouse-face))
7859           (list 'gnus-callback fun)
7860           (and data (list 'gnus-data data))))
7861   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7862                          :help-echo (or text "Follow the link")
7863                          :keymap gnus-url-button-map
7864                          :button-keymap gnus-widget-button-keymap))
7865
7866 (defun gnus-article-copy-string ()
7867   "Copy the string in the button to the kill ring."
7868   (interactive)
7869   (gnus-article-check-buffer)
7870   (let ((data (get-text-property (point) 'gnus-string)))
7871     (when data
7872       (with-temp-buffer
7873         (insert data)
7874         (copy-region-as-kill (point-min) (point-max))
7875         (message "Copied %s" data)))))
7876
7877 ;;; Internal functions:
7878
7879 (defun gnus-article-set-globals ()
7880   (with-current-buffer gnus-summary-buffer
7881     (gnus-set-global-variables)))
7882
7883 (defun gnus-signature-toggle (end)
7884   (gnus-with-article-buffer
7885     (let ((inhibit-point-motion-hooks t))
7886       (if (text-property-any end (point-max) 'article-type 'signature)
7887           (progn
7888             (gnus-delete-wash-type 'signature)
7889             (gnus-remove-text-properties-when
7890              'article-type 'signature end (point-max)
7891              (cons 'article-type (cons 'signature
7892                                        gnus-hidden-properties))))
7893         (gnus-add-wash-type 'signature)
7894         (gnus-add-text-properties-when
7895          'article-type nil end (point-max)
7896          (cons 'article-type (cons 'signature
7897                                    gnus-hidden-properties)))))
7898     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7899       (gnus-set-mode-line 'article))))
7900
7901 (defun gnus-button-push (marker-and-entry)
7902   ;; Push button starting at MARKER.
7903   (save-excursion
7904     (let* ((marker (car marker-and-entry))
7905            (entry (cadr marker-and-entry))
7906            (regexp (car entry))
7907            (inhibit-point-motion-hooks t))
7908       (goto-char marker)
7909       ;; This is obviously true, or something bad is happening :)
7910       ;; But we need it to have the match-data
7911       (when (looking-at (or (if (symbolp regexp)
7912                                 (symbol-value regexp)
7913                               regexp)))
7914         (let ((fun (nth 3 entry))
7915               (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7916                              (get-char-property marker 'gnus-button-url))
7917                         (mapcar (lambda (group)
7918                                   (let ((string (match-string group)))
7919                                     (set-text-properties
7920                                      0 (length string) nil string)
7921                                     string))
7922                                 (nthcdr 4 entry)))))
7923
7924           (cond
7925            ((fboundp fun)
7926             (apply fun args))
7927            ((and (boundp fun)
7928                  (fboundp (symbol-value fun)))
7929             (apply (symbol-value fun) args))
7930            (t
7931             (gnus-message 1 "You must define `%S' to use this button"
7932                           (cons fun args)))))))))
7933
7934 (defun gnus-parse-news-url (url)
7935   (let (scheme server port group message-id articles)
7936     (with-temp-buffer
7937       (insert url)
7938       (goto-char (point-min))
7939       (when (looking-at "\\([A-Za-z]+\\):")
7940         (setq scheme (match-string 1))
7941         (goto-char (match-end 0)))
7942       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7943         (setq server (match-string 1))
7944         (setq port (if (stringp (match-string 3))
7945                        (string-to-number (match-string 3))
7946                      (match-string 3)))
7947         (goto-char (match-end 0)))
7948
7949       (cond
7950        ((looking-at "\\(.*@.*\\)")
7951         (setq message-id (match-string 1)))
7952        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7953         (setq group (match-string 1)
7954               articles (split-string (match-string 2) "-")))
7955        ((looking-at "\\([^/]+\\)/?")
7956         (setq group (match-string 1)))
7957        (t
7958         (error "Unknown news URL syntax"))))
7959     (list scheme server port group message-id articles)))
7960
7961 (defun gnus-button-handle-news (url)
7962   "Fetch a news URL."
7963   (destructuring-bind (scheme server port group message-id articles)
7964       (gnus-parse-news-url url)
7965     (cond
7966      (message-id
7967       (with-current-buffer gnus-summary-buffer
7968         (if server
7969             (let ((gnus-refer-article-method
7970                    (nconc (list (list 'nntp server))
7971                           gnus-refer-article-method))
7972                   (nntp-port-number (or port "nntp")))
7973               (gnus-message 7 "Fetching %s with %s"
7974                             message-id gnus-refer-article-method)
7975               (gnus-summary-refer-article message-id))
7976           (gnus-summary-refer-article message-id))))
7977      (group
7978       (gnus-button-fetch-group url)))))
7979
7980 (defun gnus-button-patch (library line)
7981   "Visit an Emacs Lisp library LIBRARY on line LINE."
7982   (interactive)
7983   (let ((file (locate-library (file-name-nondirectory library))))
7984     (unless file
7985       (error "Couldn't find library %s" library))
7986     (find-file file)
7987     (goto-char (point-min))
7988     (forward-line (1- (string-to-number line)))))
7989
7990 (defun gnus-button-handle-man (url)
7991   "Fetch a man page."
7992   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7993   (when (eq gnus-button-man-handler 'woman)
7994     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
7995   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7996   (funcall gnus-button-man-handler url))
7997
7998 (defun gnus-button-handle-info-url (url)
7999   "Fetch an info URL."
8000   (setq url (mm-subst-char-in-string ?+ ?\  url))
8001   (cond
8002    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8003     (gnus-info-find-node
8004      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8005                      "Gnus")
8006              ")" (gnus-url-unhex-string (match-string 2 url)))))
8007    ((string-match "([^)\"]+)[^\"]+" url)
8008     (setq url
8009           (gnus-replace-in-string
8010            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8011     (gnus-info-find-node url))
8012    (t (error "Can't parse %s" url))))
8013
8014 (defun gnus-button-handle-info-url-gnome (url)
8015   "Fetch GNOME style info URL."
8016   (setq url (mm-subst-char-in-string ?_ ?\  url))
8017   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8018       (gnus-info-find-node
8019        (concat "("
8020                (gnus-url-unhex-string
8021                  (match-string 1 url))
8022                ")"
8023                (or (gnus-url-unhex-string
8024                     (match-string 2 url))
8025                    "Top")))
8026     (error "Can't parse %s" url)))
8027
8028 (defun gnus-button-handle-info-url-kde (url)
8029   "Fetch KDE style info URL."
8030   (gnus-info-find-node (gnus-url-unhex-string url)))
8031
8032 ;; (info) will autoload info.el
8033 (declare-function Info-menu "info" (menu-item &optional fork))
8034 (declare-function Info-index-next "info" (num))
8035
8036 (defun gnus-button-handle-info-keystrokes (url)
8037   "Call `info' when pushing the corresponding URL button."
8038   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8039   (let (node indx comma)
8040     (if (string-match
8041          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8042                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8043                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\>"
8044                  "\\(?:[ \t\n,]*\\)\\)?")
8045          url)
8046         (setq node (match-string 2 url)
8047               indx (match-string 3 url))
8048       (error "Can't parse %s" url))
8049     (info)
8050     (Info-directory)
8051     (Info-menu node)
8052     (when (> (length indx) 0)
8053       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET\\>"
8054                             "\\([ \t\n,]*\\)")
8055                     indx)
8056       (setq comma (match-string 2 indx))
8057       (setq indx  (match-string 1 indx))
8058       (Info-index indx)
8059       (when comma
8060         (dotimes (i (with-temp-buffer
8061                       (insert comma)
8062                       ;; Note: the XEmacs version of `how-many' takes
8063                       ;; no optional argument.
8064                       (goto-char (point-min))
8065                       (how-many ",")))
8066           (Info-index-next 1)))
8067       nil)))
8068
8069 (autoload 'pgg-snarf-keys-region "pgg")
8070 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8071 (declare-function pgg-display-output-buffer "pgg" (start end status))
8072
8073 (defun gnus-button-openpgp (url)
8074   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8075   (with-temp-buffer
8076     (mm-url-insert-file-contents-external url)
8077     (pgg-snarf-keys-region (point-min) (point-max))
8078     (pgg-display-output-buffer nil nil nil)))
8079
8080 (defun gnus-button-message-id (message-id)
8081   "Fetch MESSAGE-ID."
8082   (with-current-buffer gnus-summary-buffer
8083     (gnus-summary-refer-article message-id)))
8084
8085 (defun gnus-button-fetch-group (address &rest ignore)
8086   "Fetch GROUP specified by ADDRESS."
8087   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8088                       address)
8089     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8090     ;; for nntp:// and news://
8091     (setq address (match-string 3 address)))
8092   (if (not (string-match "[:/]" address))
8093       ;; This is just a simple group url.
8094       (gnus-group-read-ephemeral-group address gnus-select-method)
8095     (if (not
8096          (string-match
8097           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8098           address))
8099         (error "Can't parse %s" address)
8100       (gnus-group-read-ephemeral-group
8101        (match-string 4 address)
8102        `(nntp ,(match-string 1 address)
8103               (nntp-address ,(match-string 1 address))
8104               (nntp-port-number ,(if (match-end 3)
8105                                      (match-string 3 address)
8106                                    "nntp")))
8107        nil nil nil
8108        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8109
8110 (defun gnus-url-parse-query-string (query &optional downcase)
8111   (let (retval pairs cur key val)
8112     (setq pairs (split-string query "&"))
8113     (while pairs
8114       (setq cur (car pairs)
8115             pairs (cdr pairs))
8116       (if (not (string-match "=" cur))
8117           nil                           ; Grace
8118         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8119               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8120         (if downcase
8121             (setq key (downcase key)))
8122         (setq cur (assoc key retval))
8123         (if cur
8124             (setcdr cur (cons val (cdr cur)))
8125           (setq retval (cons (list key val) retval)))))
8126     retval))
8127
8128 (defun gnus-url-mailto (url)
8129   ;; Send mail to someone
8130   (setq url (replace-regexp-in-string "\n" " " url))
8131   (when (string-match "mailto:/*\\(.*\\)" url)
8132     (setq url (substring url (match-beginning 1) nil)))
8133   (let (to args subject func)
8134     (setq args (gnus-url-parse-query-string
8135                 (if (string-match "^\\?" url)
8136                     (substring url 1)
8137                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8138                       (concat "to=" (match-string 1 url) "&"
8139                               (match-string 2 url))
8140                     (concat "to=" url))))
8141           subject (cdr-safe (assoc "subject" args)))
8142     (gnus-msg-mail)
8143     (while args
8144       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8145       (if (fboundp func)
8146           (funcall func)
8147         (message-position-on-field (caar args)))
8148       (insert (gnus-replace-in-string
8149                (mapconcat 'identity (reverse (cdar args)) ", ")
8150                "\r\n" "\n" t))
8151       (setq args (cdr args)))
8152     (if subject
8153         (message-goto-body)
8154       (message-goto-subject))))
8155
8156 (defun gnus-button-embedded-url (address)
8157   "Activate ADDRESS with `browse-url'."
8158   (browse-url (gnus-strip-whitespace address)))
8159
8160 ;;; Next/prev buttons in the article buffer.
8161
8162 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8163 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8164
8165 (defvar gnus-prev-page-map
8166   (let ((map (make-sparse-keymap)))
8167     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8168     (define-key map "\r" 'gnus-button-prev-page)
8169     map))
8170
8171 (defvar gnus-next-page-map
8172   (let ((map (make-sparse-keymap)))
8173     (define-key map gnus-mouse-2 'gnus-button-next-page)
8174     (define-key map "\r" 'gnus-button-next-page)
8175     map))
8176
8177 (defun gnus-insert-prev-page-button ()
8178   (let ((b (point)) e
8179         (inhibit-read-only t))
8180     (gnus-eval-format
8181      gnus-prev-page-line-format nil
8182      `(keymap ,gnus-prev-page-map
8183               gnus-prev t
8184               gnus-callback gnus-article-button-prev-page
8185               article-type annotation))
8186     (setq e (if (bolp)
8187                 ;; Exclude a newline.
8188                 (1- (point))
8189               (point)))
8190     (when gnus-article-button-face
8191       (gnus-overlay-put (gnus-make-overlay b e nil t)
8192                         'face gnus-article-button-face))
8193     (widget-convert-button
8194      'link b e
8195      :action 'gnus-button-prev-page
8196      :button-keymap gnus-prev-page-map)))
8197
8198 (defun gnus-button-next-page (&optional args more-args)
8199   "Go to the next page."
8200   (interactive)
8201   (let ((win (selected-window)))
8202     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8203     (gnus-article-next-page)
8204     (select-window win)))
8205
8206 (defun gnus-button-prev-page (&optional args more-args)
8207   "Go to the prev page."
8208   (interactive)
8209   (let ((win (selected-window)))
8210     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8211     (gnus-article-prev-page)
8212     (select-window win)))
8213
8214 (defun gnus-insert-next-page-button ()
8215   (let ((b (point)) e
8216         (inhibit-read-only t))
8217     (gnus-eval-format gnus-next-page-line-format nil
8218                       `(keymap ,gnus-next-page-map
8219                                gnus-next t
8220                                gnus-callback gnus-article-button-next-page
8221                                article-type annotation))
8222     (setq e (if (bolp)
8223                 ;; Exclude a newline.
8224                 (1- (point))
8225               (point)))
8226     (when gnus-article-button-face
8227       (gnus-overlay-put (gnus-make-overlay b e nil t)
8228                         'face gnus-article-button-face))
8229     (widget-convert-button
8230      'link b e
8231      :action 'gnus-button-next-page
8232      :button-keymap gnus-next-page-map)))
8233
8234 (defun gnus-article-button-next-page (arg)
8235   "Go to the next page."
8236   (interactive "P")
8237   (let ((win (selected-window)))
8238     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8239     (gnus-article-next-page)
8240     (select-window win)))
8241
8242 (defun gnus-article-button-prev-page (arg)
8243   "Go to the prev page."
8244   (interactive "P")
8245   (let ((win (selected-window)))
8246     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8247     (gnus-article-prev-page)
8248     (select-window win)))
8249
8250 (defvar gnus-decode-header-methods
8251   '(mail-decode-encoded-word-region)
8252   "List of methods used to decode headers.
8253
8254 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8255 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8256 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8257 whose names match REGEXP.
8258
8259 For example:
8260 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8261  mail-decode-encoded-word-region
8262  (\"chinese\" . rfc1843-decode-region))
8263 ")
8264
8265 (defvar gnus-decode-header-methods-cache nil)
8266
8267 (defun gnus-multi-decode-header (start end)
8268   "Apply the functions from `gnus-encoded-word-methods' that match."
8269   (unless (and gnus-decode-header-methods-cache
8270                (eq gnus-newsgroup-name
8271                    (car gnus-decode-header-methods-cache)))
8272     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8273     (dolist (x gnus-decode-header-methods)
8274       (if (symbolp x)
8275           (nconc gnus-decode-header-methods-cache (list x))
8276         (if (and gnus-newsgroup-name
8277                  (string-match (car x) gnus-newsgroup-name))
8278             (nconc gnus-decode-header-methods-cache
8279                    (list (cdr x)))))))
8280   (let ((xlist gnus-decode-header-methods-cache))
8281     (pop xlist)
8282     (save-restriction
8283       (narrow-to-region start end)
8284       (while xlist
8285         (funcall (pop xlist) (point-min) (point-max))))))
8286
8287 ;;;
8288 ;;; Treatment top-level handling.
8289 ;;;
8290
8291 (defvar gnus-inhibit-article-treatments nil)
8292
8293 (defun gnus-treat-article (gnus-treat-condition
8294                            &optional part-number total-parts gnus-treat-type)
8295   (let ((gnus-treat-length (- (point-max) (point-min)))
8296         (alist gnus-treatment-function-alist)
8297         (article-goto-body-goes-to-point-min-p t)
8298         (treated-type
8299          (or (not gnus-treat-type)
8300              (catch 'found
8301                (let ((list gnus-article-treat-types))
8302                  (while list
8303                    (when (string-match (pop list) gnus-treat-type)
8304                      (throw 'found t)))))))
8305         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8306         val elem)
8307     (gnus-run-hooks 'gnus-part-display-hook)
8308     (dolist (elem alist)
8309       (setq val
8310             (save-excursion
8311               (when (gnus-buffer-live-p gnus-summary-buffer)
8312                 (set-buffer gnus-summary-buffer))
8313               (symbol-value (car elem))))
8314       (when (and (or (consp val)
8315                      treated-type)
8316                  (or (not gnus-inhibit-article-treatments)
8317                      (eq gnus-treat-condition 'head))
8318                  (gnus-treat-predicate val)
8319                  (or (not (get (car elem) 'highlight))
8320                      highlightp))
8321         (save-restriction
8322           (funcall (cadr elem)))))))
8323
8324 ;; Dynamic variables.
8325 (defvar part-number)
8326 (defvar total-parts)
8327 (defvar gnus-treat-type)
8328 (defvar gnus-treat-condition)
8329 (defvar gnus-treat-length)
8330
8331 (defun gnus-treat-predicate (val)
8332   (cond
8333    ((null val)
8334     nil)
8335    (gnus-treat-condition
8336     (eq gnus-treat-condition val))
8337    ((and (listp val)
8338          (stringp (car val)))
8339     (apply 'gnus-or (mapcar `(lambda (s)
8340                                (string-match s ,(or gnus-newsgroup-name "")))
8341                             val)))
8342    ((listp val)
8343     (let ((pred (pop val)))
8344       (cond
8345        ((eq pred 'or)
8346         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8347        ((eq pred 'and)
8348         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8349        ((eq pred 'not)
8350         (not (gnus-treat-predicate (car val))))
8351        ((eq pred 'typep)
8352         (equal (car val) gnus-treat-type))
8353        (t
8354         (error "%S is not a valid predicate" pred)))))
8355    ((eq val t)
8356     t)
8357    ((eq val 'head)
8358     nil)
8359    ((eq val 'first)
8360     (eq part-number 1))
8361    ((eq val 'last)
8362     (eq part-number total-parts))
8363    ((numberp val)
8364     (< gnus-treat-length val))
8365    (t
8366     (error "%S is not a valid value" val))))
8367
8368 (defun gnus-article-encrypt-body (protocol &optional n)
8369   "Encrypt the article body."
8370   (interactive
8371    (list
8372     (or gnus-article-encrypt-protocol
8373         (gnus-completing-read "Encrypt protocol"
8374                               (mapcar 'car gnus-article-encrypt-protocol-alist)
8375                               t))
8376     current-prefix-arg))
8377   ;; User might hit `K E' instead of `K e', so prompt once.
8378   (when (and gnus-article-encrypt-protocol
8379              gnus-novice-user)
8380     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8381       (error "Encrypt aborted")))
8382   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8383     (unless func
8384       (error "Can't find the encrypt protocol %s" protocol))
8385     (if (member gnus-newsgroup-name '("nndraft:delayed"
8386                                       "nndraft:drafts"
8387                                       "nndraft:queue"))
8388         (error "Can't encrypt the article in group %s"
8389                gnus-newsgroup-name))
8390     (gnus-summary-iterate n
8391       (with-current-buffer gnus-summary-buffer
8392         (let ((mail-parse-charset gnus-newsgroup-charset)
8393               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8394               (summary-buffer gnus-summary-buffer)
8395               references point)
8396           (gnus-set-global-variables)
8397           (when (gnus-group-read-only-p)
8398             (error "The current newsgroup does not support article encrypt"))
8399           (gnus-summary-show-article t)
8400           (setq references
8401               (or (mail-header-references gnus-current-headers) ""))
8402           (set-buffer gnus-article-buffer)
8403           (let* ((inhibit-read-only t)
8404                  (headers
8405                   (mapcar (lambda (field)
8406                             (and (save-restriction
8407                                    (message-narrow-to-head)
8408                                    (goto-char (point-min))
8409                                    (search-forward field nil t))
8410                                  (prog2
8411                                      (message-narrow-to-field)
8412                                      (buffer-string)
8413                                    (delete-region (point-min) (point-max))
8414                                    (widen))))
8415                           '("Content-Type:" "Content-Transfer-Encoding:"
8416                             "Content-Disposition:"))))
8417             (message-narrow-to-head)
8418             (message-remove-header "MIME-Version")
8419             (goto-char (point-max))
8420             (setq point (point))
8421             (insert (apply 'concat headers))
8422             (widen)
8423             (narrow-to-region point (point-max))
8424             (let ((message-options message-options))
8425               (message-options-set 'message-sender user-mail-address)
8426               (message-options-set 'message-recipients user-mail-address)
8427               (message-options-set 'message-sign-encrypt 'not)
8428               (funcall func))
8429             (goto-char (point-min))
8430             (insert "MIME-Version: 1.0\n")
8431             (widen)
8432             (gnus-summary-edit-article-done
8433              references nil summary-buffer t))
8434           (when gnus-keep-backlog
8435             (gnus-backlog-remove-article
8436              (car gnus-article-current) (cdr gnus-article-current)))
8437           (gnus-flush-original-article-buffer)
8438           (when gnus-use-cache
8439             (gnus-cache-update-article
8440              (car gnus-article-current) (cdr gnus-article-current))))))))
8441
8442 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8443   "The following specs can be used:
8444 %t  The security MIME type
8445 %i  Additional info
8446 %d  Details
8447 %D  Details if button is pressed")
8448
8449 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8450   "The following specs can be used:
8451 %t  The security MIME type
8452 %i  Additional info
8453 %d  Details
8454 %D  Details if button is pressed")
8455
8456 (defvar gnus-mime-security-button-line-format-alist
8457   '((?t gnus-tmp-type ?s)
8458     (?i gnus-tmp-info ?s)
8459     (?d gnus-tmp-details ?s)
8460     (?D gnus-tmp-pressed-details ?s)))
8461
8462 (defvar gnus-mime-security-button-commands
8463   '((gnus-article-press-button "\r" "Show Detail")
8464     (undefined "v")
8465     (undefined "t")
8466     (undefined "C")
8467     (gnus-mime-security-save-part "o" "Save...")
8468     (undefined "\C-o")
8469     (undefined "r")
8470     (undefined "d")
8471     (undefined "c")
8472     (undefined "i")
8473     (undefined "E")
8474     (undefined "e")
8475     (undefined "p")
8476     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8477     (undefined ".")))
8478
8479 (defvar gnus-mime-security-button-map
8480   (let ((map (make-sparse-keymap)))
8481     (define-key map gnus-mouse-2 'gnus-article-push-button)
8482     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8483     (dolist (c gnus-mime-security-button-commands)
8484       (define-key map (cadr c) (car c)))
8485     map))
8486
8487 (easy-menu-define
8488   gnus-mime-security-button-menu gnus-mime-security-button-map
8489   "Security button menu."
8490   `("Security Part"
8491     ,@(delq nil
8492             (mapcar (lambda (c)
8493                       (unless (eq (car c) 'undefined)
8494                         (vector (caddr c) (car c) :active t)))
8495                     gnus-mime-security-button-commands))))
8496
8497 (defun gnus-mime-security-button-menu (event prefix)
8498   "Construct a context-sensitive menu of security commands."
8499   (interactive "e\nP")
8500   (save-window-excursion
8501     (let ((pos (event-start event)))
8502       (select-window (posn-window pos))
8503       (goto-char (posn-point pos))
8504       (gnus-article-check-buffer)
8505       (popup-menu gnus-mime-security-button-menu nil prefix))))
8506
8507 (defvar gnus-mime-security-details-buffer nil)
8508
8509 (defvar gnus-mime-security-button-pressed nil)
8510
8511 (defvar gnus-mime-security-show-details-inline t
8512   "If non-nil, show details in the article buffer.")
8513
8514 (defun gnus-mime-security-verify-or-decrypt (handle)
8515   (mm-remove-parts (cdr handle))
8516   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8517         point (inhibit-read-only t))
8518     (if region
8519         (goto-char (car region)))
8520     (setq point (point))
8521     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8522       (let* ((mm-verify-option 'known)
8523              (mm-decrypt-option 'known)
8524              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8525         (unless (eq nparts (cdr handle))
8526           (mm-destroy-parts (cdr handle))
8527           (setcdr handle nparts))))
8528     (gnus-mime-display-security handle)
8529     (when region
8530       (delete-region (point) (cdr region))
8531       (set-marker (car region) nil)
8532       (set-marker (cdr region) nil))
8533     (goto-char point)))
8534
8535 (defun gnus-mime-security-show-details (handle)
8536   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8537     (if (not details)
8538         (gnus-message 5 "No details.")
8539       (if gnus-mime-security-show-details-inline
8540           (let ((gnus-mime-security-button-pressed
8541                  (not (get-text-property (point) 'gnus-mime-details)))
8542                 (gnus-mime-security-button-line-format
8543                  (get-text-property (point) 'gnus-line-format))
8544                 (inhibit-read-only t))
8545             (forward-char -1)
8546             (while (eq (get-text-property (point) 'gnus-line-format)
8547                        gnus-mime-security-button-line-format)
8548               (forward-char -1))
8549             (forward-char)
8550             (save-restriction
8551               (narrow-to-region (point) (point))
8552               (gnus-insert-mime-security-button handle))
8553             (delete-region (point)
8554                            (or (text-property-not-all
8555                                 (point) (point-max)
8556                                 'gnus-line-format
8557                                 gnus-mime-security-button-line-format)
8558                                (point-max))))
8559         ;; Not inlined.
8560         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8561             (with-current-buffer gnus-mime-security-details-buffer
8562               (erase-buffer)
8563               t)
8564           (setq gnus-mime-security-details-buffer
8565                 (gnus-get-buffer-create "*MIME Security Details*")))
8566         (with-current-buffer gnus-mime-security-details-buffer
8567           (insert details)
8568           (goto-char (point-min)))
8569         (pop-to-buffer gnus-mime-security-details-buffer)))))
8570
8571 (defun gnus-mime-security-press-button (handle)
8572   (save-excursion
8573     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8574         (gnus-mime-security-show-details handle)
8575       (gnus-mime-security-verify-or-decrypt handle))))
8576
8577 (defun gnus-insert-mime-security-button (handle &optional displayed)
8578   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8579          (gnus-tmp-type
8580           (concat
8581            (or (nth 2 (assoc protocol mm-verify-function-alist))
8582                (nth 2 (assoc protocol mm-decrypt-function-alist))
8583                "Unknown")
8584            (if (equal (car handle) "multipart/signed")
8585                " Signed" " Encrypted")
8586            " Part"))
8587          (gnus-tmp-info
8588           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8589               "Undecided"))
8590          (gnus-tmp-details
8591           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8592          gnus-tmp-pressed-details
8593          b e)
8594     (setq gnus-tmp-details
8595           (if gnus-tmp-details
8596               (concat "\n" gnus-tmp-details)
8597             ""))
8598     (setq gnus-tmp-pressed-details
8599           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8600     (unless (bolp)
8601       (insert "\n"))
8602     (setq b (point))
8603     (gnus-eval-format
8604      gnus-mime-security-button-line-format
8605      gnus-mime-security-button-line-format-alist
8606      `(keymap ,gnus-mime-security-button-map
8607          gnus-callback gnus-mime-security-press-button
8608          gnus-line-format ,gnus-mime-security-button-line-format
8609          gnus-mime-details ,gnus-mime-security-button-pressed
8610          article-type annotation
8611          gnus-data ,handle))
8612     (setq e (if (bolp)
8613                 ;; Exclude a newline.
8614                 (1- (point))
8615               (point)))
8616     (when gnus-article-button-face
8617       (gnus-overlay-put (gnus-make-overlay b e nil t)
8618                         'face gnus-article-button-face))
8619     (widget-convert-button
8620      'link b e
8621      :mime-handle handle
8622      :action 'gnus-widget-press-button
8623      :button-keymap gnus-mime-security-button-map
8624      :help-echo
8625      (lambda (widget)
8626        ;; Needed to properly clear the message due to a bug in
8627        ;; wid-edit (XEmacs only).
8628        (when (boundp 'help-echo-owns-message)
8629          (setq help-echo-owns-message t))
8630        (format
8631         "%S: show detail; %S: more options"
8632         (aref gnus-mouse-2 0)
8633         (aref gnus-down-mouse-3 0))))))
8634
8635 (defun gnus-mime-display-security (handle)
8636   (save-restriction
8637     (narrow-to-region (point) (point))
8638     (unless (gnus-unbuttonized-mime-type-p (car handle))
8639       (gnus-insert-mime-security-button handle))
8640     (gnus-mime-display-part (cadr handle))
8641     (unless (bolp)
8642       (insert "\n"))
8643     (unless (gnus-unbuttonized-mime-type-p (car handle))
8644       (let ((gnus-mime-security-button-line-format
8645              gnus-mime-security-button-end-line-format))
8646         (gnus-insert-mime-security-button handle)))
8647     (mm-set-handle-multipart-parameter
8648      handle 'gnus-region
8649      (cons (set-marker (make-marker) (point-min))
8650            (set-marker (make-marker) (point-max))))
8651     (goto-char (point-max))))
8652
8653 (defun gnus-mime-security-run-function (function)
8654   "Run FUNCTION with the security part under point."
8655   (gnus-article-check-buffer)
8656   (let ((data (get-text-property (point) 'gnus-data))
8657         buffer handle)
8658     (when (and (stringp (car-safe data))
8659                (setq buffer (mm-handle-multipart-original-buffer data))
8660                (setq handle (cadr data)))
8661       (if (bufferp (mm-handle-buffer handle))
8662           (progn
8663             (setq handle (cons buffer (copy-sequence (cdr handle))))
8664             (mm-handle-set-undisplayer handle nil))
8665         (setq handle (mm-make-handle
8666                       buffer
8667                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8668                       nil nil nil nil nil nil)))
8669       (funcall function handle))))
8670
8671 (defun gnus-mime-security-save-part ()
8672   "Save the security part under point."
8673   (interactive)
8674   (gnus-mime-security-run-function 'mm-save-part))
8675
8676 (defun gnus-mime-security-pipe-part ()
8677   "Pipe the security part under point to a process."
8678   (interactive)
8679   (gnus-mime-security-run-function 'mm-pipe-part))
8680
8681 (gnus-ems-redefine)
8682
8683 (provide 'gnus-art)
8684
8685 (run-hooks 'gnus-art-load-hook)
8686
8687 ;;; gnus-art.el ends here