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