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