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