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