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