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