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