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