Remove useless gnus-treat-translate
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33 (defvar tool-bar-map)
34 (defvar w3m-minor-mode-map)
35
36 (require 'gnus)
37 (require 'gnus-sum)
38 (require 'gnus-spec)
39 (require 'gnus-int)
40 (require 'gnus-win)
41 (require 'mm-bodies)
42 (require 'mail-parse)
43 (require 'mm-decode)
44 (require 'mm-view)
45 (require 'wid-edit)
46 (require 'mm-uu)
47 (require 'message)
48
49 (autoload 'gnus-msg-mail "gnus-msg" nil t)
50 (autoload 'gnus-button-mailto "gnus-msg")
51 (autoload 'gnus-button-reply "gnus-msg" nil t)
52 (autoload 'parse-time-string "parse-time" nil nil)
53 (autoload 'ansi-color-apply-on-region "ansi-color")
54 (autoload 'mm-url-insert-file-contents-external "mm-url")
55 (autoload 'mm-extern-cache-contents "mm-extern")
56
57 (defgroup gnus-article nil
58   "Article display."
59   :link '(custom-manual "(gnus)Article Buffer")
60   :group 'gnus)
61
62 (defgroup gnus-article-treat nil
63   "Treating article parts."
64   :link '(custom-manual "(gnus)Article Hiding")
65   :group 'gnus-article)
66
67 (defgroup gnus-article-hiding nil
68   "Hiding article parts."
69   :link '(custom-manual "(gnus)Article Hiding")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-highlight nil
73   "Article highlighting."
74   :link '(custom-manual "(gnus)Article Highlighting")
75   :group 'gnus-article
76   :group 'gnus-visual)
77
78 (defgroup gnus-article-signature nil
79   "Article signatures."
80   :link '(custom-manual "(gnus)Article Signature")
81   :group 'gnus-article)
82
83 (defgroup gnus-article-headers nil
84   "Article headers."
85   :link '(custom-manual "(gnus)Hiding Headers")
86   :group 'gnus-article)
87
88 (defgroup gnus-article-washing nil
89   "Special commands on articles."
90   :link '(custom-manual "(gnus)Article Washing")
91   :group 'gnus-article)
92
93 (defgroup gnus-article-emphasis nil
94   "Fontisizing articles."
95   :link '(custom-manual "(gnus)Article Fontisizing")
96   :group 'gnus-article)
97
98 (defgroup gnus-article-saving nil
99   "Saving articles."
100   :link '(custom-manual "(gnus)Saving Articles")
101   :group 'gnus-article)
102
103 (defgroup gnus-article-mime nil
104   "Worshiping the MIME wonder."
105   :link '(custom-manual "(gnus)Using MIME")
106   :group 'gnus-article)
107
108 (defgroup gnus-article-buttons nil
109   "Pushable buttons in the article buffer."
110   :link '(custom-manual "(gnus)Article Buttons")
111   :group 'gnus-article)
112
113 (defgroup gnus-article-various nil
114   "Other article options."
115   :link '(custom-manual "(gnus)Misc Article")
116   :group 'gnus-article)
117
118 (defcustom gnus-ignored-headers
119   (mapcar
120    (lambda (header)
121      (concat "^" header ":"))
122    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
123      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
124      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
125      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
126      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
127      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
128      "X-Attribution" "X-Originating-IP" "Delivered-To"
129      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
130      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
131      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
132      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
133      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
134      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
135      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
136      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
137      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
138      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
139      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
140      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
141      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
142      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
143      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
144      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
145      "List-[A-Za-z]+" "X-Listprocessor-Version"
146      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
147      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
148      "X-Received" "Content-length" "X-precedence"
149      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
150      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
151      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
152      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
153      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
154      "X-Content-length" "X-Posting-Agent" "Original-Received"
155      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
156      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
157      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
158      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
159      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
160      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
161      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
162      "Envelope-Sender" "Envelope-Recipients"))
163   "*All headers that start with this regexp will be hidden.
164 This variable can also be a list of regexps of headers to be ignored.
165 If `gnus-visible-headers' is non-nil, this variable will be ignored."
166   :type '(choice :custom-show nil
167                  regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(choice
177           (repeat :value-to-internal (lambda (widget value)
178                                        (custom-split-regexp-maybe value))
179                   :match (lambda (widget value)
180                            (or (stringp value)
181                                (widget-editable-list-match widget value)))
182                   regexp)
183           (const :tag "Use gnus-ignored-headers" nil)
184           regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-sorted-header-list
188   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
189     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
190   "*This variable is a list of regular expressions.
191 If it is non-nil, headers that match the regular expressions will
192 be placed first in the article buffer in the sequence specified by
193 this list."
194   :type '(repeat regexp)
195   :group 'gnus-article-hiding)
196
197 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
198   "Headers that are only to be displayed if they have interesting data.
199 Possible values in this list are:
200
201   'empty       Headers with no content.
202   'newsgroups  Newsgroup identical to Gnus group.
203   'to-address  To identical to To-address.
204   'to-list     To identical to To-list.
205   'cc-list     CC identical to To-list.
206   'followup-to Followup-to identical to Newsgroups.
207   'reply-to    Reply-to identical to From.
208   'date        Date less than four days old.
209   'long-to     To and/or Cc longer than 1024 characters.
210   'many-to     Multiple To and/or Cc."
211   :type '(set (const :tag "Headers with no content." empty)
212               (const :tag "Newsgroups identical to Gnus group." newsgroups)
213               (const :tag "To identical to To-address." to-address)
214               (const :tag "To identical to To-list." to-list)
215               (const :tag "CC identical to To-list." cc-list)
216               (const :tag "Followup-to identical to Newsgroups." followup-to)
217               (const :tag "Reply-to identical to From." reply-to)
218               (const :tag "Date less than four days old." date)
219               (const :tag "To and/or Cc longer than 1024 characters." long-to)
220               (const :tag "Multiple To and/or Cc headers." many-to))
221   :group 'gnus-article-hiding)
222
223 (defcustom gnus-article-skip-boring nil
224   "Skip over text that is not worth reading.
225 By default, if you set this t, then Gnus will display citations and
226 signatures, but will never scroll down to show you a page consisting
227 only of boring text.  Boring text is controlled by
228 `gnus-article-boring-faces'."
229   :version "22.1"
230   :type 'boolean
231   :group 'gnus-article-hiding)
232
233 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
234   "Regexp matching signature separator.
235 This can also be a list of regexps.  In that case, it will be checked
236 from head to tail looking for a separator.  Searches will be done from
237 the end of the buffer."
238   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
239                  (regexp)
240                  (repeat :tag "List of regexp" regexp))
241   :group 'gnus-article-signature)
242
243 (defcustom gnus-signature-limit nil
244   "Provide a limit to what is considered a signature.
245 If it is a number, no signature may not be longer (in characters) than
246 that number.  If it is a floating point number, no signature may be
247 longer (in lines) than that number.  If it is a function, the function
248 will be called without any parameters, and if it returns nil, there is
249 no signature in the buffer.  If it is a string, it will be used as a
250 regexp.  If it matches, the text in question is not a signature.
251
252 This can also be a list of the above values."
253   :type '(choice (const nil)
254                  (integer :value 200)
255                  (number :value 4.0)
256                  function
257                  (regexp :value ".*"))
258   :group 'gnus-article-signature)
259
260 (defcustom gnus-hidden-properties '(invisible t intangible t)
261   "Property list to use for hiding text."
262   :type 'sexp
263   :group 'gnus-article-hiding)
264
265 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
266 ;; frames in a session.
267 (defcustom gnus-article-x-face-command
268   (if (featurep 'xemacs)
269       (if (or (gnus-image-type-available-p 'xface)
270               (gnus-image-type-available-p 'pbm))
271           'gnus-display-x-face-in-from
272         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
273     (if (gnus-image-type-available-p 'pbm)
274         'gnus-display-x-face-in-from
275       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
276 display -"))
277   "*String or function to be executed to display an X-Face header.
278 If it is a string, the command will be executed in a sub-shell
279 asynchronously.  The compressed face will be piped to this command."
280   :type `(choice string
281                  (function-item gnus-display-x-face-in-from)
282                  function)
283   :version "21.1"
284   :group 'gnus-picon
285   :group 'gnus-article-washing)
286
287 (defcustom gnus-article-x-face-too-ugly nil
288   "Regexp matching posters whose face shouldn't be shown automatically."
289   :type '(choice regexp (const nil))
290   :group 'gnus-article-washing)
291
292 (defcustom gnus-article-banner-alist nil
293   "Banner alist for stripping.
294 For example,
295      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
296   :version "21.1"
297   :type '(repeat (cons symbol regexp))
298   :group 'gnus-article-washing)
299
300 (gnus-define-group-parameter
301  banner
302  :variable-document
303  "Alist of regexps (to match group names) and banner."
304  :variable-group gnus-article-washing
305  :parameter-type
306  '(choice :tag "Banner"
307           :value nil
308           (const :tag "Remove signature" signature)
309           (symbol :tag "Item in `gnus-article-banner-alist'" none)
310           regexp
311           (const :tag "None" nil))
312  :parameter-document
313  "If non-nil, specify how to remove `banners' from articles.
314
315 Symbol `signature' means to remove signatures delimited by
316 `gnus-signature-separator'.  Any other symbol is used to look up a
317 regular expression to match the banner in `gnus-article-banner-alist'.
318 A string is used as a regular expression to match the banner
319 directly.")
320
321 (defcustom gnus-article-address-banner-alist nil
322   "Alist of mail addresses and banners.
323 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
324 to match a mail address in the From: header, BANNER is one of a symbol
325 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
326 If ADDRESS matches author's mail address, it will remove things like
327 advertisements.  For example:
328
329 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
330 "
331   :type '(repeat
332           (cons
333            (regexp :tag "Address")
334            (choice :tag "Banner" :value nil
335                    (const :tag "Remove signature" signature)
336                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
337                    regexp
338                    (const :tag "None" nil))))
339   :version "22.1"
340   :group 'gnus-article-washing)
341
342 (defmacro gnus-emphasis-custom-with-format (&rest body)
343   `(let ((format "\
344 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
345 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
346      ,@body))
347
348 (defun gnus-emphasis-custom-value-to-external (value)
349   (gnus-emphasis-custom-with-format
350    (if (consp (car value))
351        (list (format format (car (car value)) (cdr (car value)))
352              2
353              (if (nth 1 value) 2 3)
354              (nth 2 value))
355      value)))
356
357 (defun gnus-emphasis-custom-value-to-internal (value)
358   (gnus-emphasis-custom-with-format
359    (let ((regexp (concat "\\`"
360                          (format (regexp-quote format)
361                                  "\\([^()]+\\)" "\\([^()]+\\)")
362                          "\\'"))
363          pattern)
364      (if (string-match regexp (setq pattern (car value)))
365          (list (cons (match-string 1 pattern) (match-string 2 pattern))
366                (= (nth 2 value) 2)
367                (nth 3 value))
368        value))))
369
370 (defcustom gnus-emphasis-alist
371   (let ((types
372          '(("\\*" "\\*" bold nil 2)
373            ("_" "_" underline)
374            ("/" "/" italic)
375            ("_/" "/_" underline-italic)
376            ("_\\*" "\\*_" underline-bold)
377            ("\\*/" "/\\*" bold-italic)
378            ("_\\*/" "/\\*_" underline-bold-italic))))
379     (nconc
380      (gnus-emphasis-custom-with-format
381       (mapcar (lambda (spec)
382                 (list (format format (car spec) (cadr spec))
383                       (or (nth 3 spec) 2)
384                       (or (nth 4 spec) 3)
385                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
386               types))
387      '(;; I've never seen anyone use this strikethru convention whereas I've
388        ;; several times seen it triggered by normal text.  --Stef
389        ;; Miles suggests that this form is sometimes used but for italics,
390        ;; so maybe we should map it to `italic'.
391        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
392        ;; 2 3 gnus-emphasis-strikethru)
393        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
394         2 3 gnus-emphasis-underline))))
395   "*Alist that says how to fontify certain phrases.
396 Each item looks like this:
397
398   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
399
400 The first element is a regular expression to be matched.  The second
401 is a number that says what regular expression grouping used to find
402 the entire emphasized word.  The third is a number that says what
403 regexp grouping should be displayed and highlighted.  The fourth
404 is the face used for highlighting."
405   :type
406   '(repeat
407     (menu-choice
408      :format "%[Customizing Style%]\n%v"
409      :indent 2
410      (group :tag "Default"
411             :value ("" 0 0 default)
412             :value-create
413             (lambda (widget)
414               (let ((value (widget-get
415                             (cadr (widget-get (widget-get widget :parent)
416                                               :args))
417                             :value)))
418                 (if (not (eq (nth 2 value) 'default))
419                     (widget-put
420                      widget
421                      :value
422                      (gnus-emphasis-custom-value-to-external value))))
423               (widget-group-value-create widget))
424             regexp
425             (integer :format "Match group: %v")
426             (integer :format "Emphasize group: %v")
427             face)
428      (group :tag "Simple"
429             :value (("_" . "_") nil default)
430             (cons :format "%v"
431                   (regexp :format "Start regexp: %v")
432                   (regexp :format "End regexp: %v"))
433             (boolean :format "Show start and end patterns: %[%v%]\n"
434                      :on " On " :off " Off ")
435             face)))
436   :get (lambda (symbol)
437          (mapcar 'gnus-emphasis-custom-value-to-internal
438                  (default-value symbol)))
439   :set (lambda (symbol value)
440          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
441                                      value)))
442   :group 'gnus-article-emphasis)
443
444 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
445   "A regexp to describe whitespace which should not be emphasized.
446 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
447 The former avoids underlining of leading and trailing whitespace,
448 and the latter avoids underlining any whitespace at all."
449   :version "21.1"
450   :group 'gnus-article-emphasis
451   :type 'regexp)
452
453 (defface gnus-emphasis-bold '((t (:bold t)))
454   "Face used for displaying strong emphasized text (*word*)."
455   :group 'gnus-article-emphasis)
456
457 (defface gnus-emphasis-italic '((t (:italic t)))
458   "Face used for displaying italic emphasized text (/word/)."
459   :group 'gnus-article-emphasis)
460
461 (defface gnus-emphasis-underline '((t (:underline t)))
462   "Face used for displaying underlined emphasized text (_word_)."
463   :group 'gnus-article-emphasis)
464
465 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
466   "Face used for displaying underlined bold emphasized text (_*word*_)."
467   :group 'gnus-article-emphasis)
468
469 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
470   "Face used for displaying underlined italic emphasized text (_/word/_)."
471   :group 'gnus-article-emphasis)
472
473 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
474   "Face used for displaying bold italic emphasized text (/*word*/)."
475   :group 'gnus-article-emphasis)
476
477 (defface gnus-emphasis-underline-bold-italic
478   '((t (:bold t :italic t :underline t)))
479   "Face used for displaying underlined bold italic emphasized text.
480 Example: (_/*word*/_)."
481   :group 'gnus-article-emphasis)
482
483 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
484                                       '((t (:strikethru t)))
485                                     '((t (:strike-through t))))
486   "Face used for displaying strike-through text (-word-)."
487   :group 'gnus-article-emphasis)
488
489 (defface gnus-emphasis-highlight-words
490   '((t (:background "black" :foreground "yellow")))
491   "Face used for displaying highlighted words."
492   :group 'gnus-article-emphasis)
493
494 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
495   "Format for display of Date headers in article bodies.
496 See `format-time-string' for the possible values.
497
498 The variable can also be function, which should return a complete Date
499 header.  The function is called with one argument, the time, which can
500 be fed to `format-time-string'."
501   :type '(choice string function)
502   :link '(custom-manual "(gnus)Article Date")
503   :group 'gnus-article-washing)
504
505 (defcustom gnus-save-all-headers t
506   "*If non-nil, don't remove any headers before saving.
507 This will be overridden by the `:headers' property that the symbol of
508 the saver function, which is specified by `gnus-default-article-saver',
509 might have."
510   :group 'gnus-article-saving
511   :type 'boolean)
512
513 (defcustom gnus-prompt-before-saving 'always
514   "*This variable says how much prompting is to be done when saving articles.
515 If it is nil, no prompting will be done, and the articles will be
516 saved to the default files.  If this variable is `always', each and
517 every article that is saved will be preceded by a prompt, even when
518 saving large batches of articles.  If this variable is neither nil not
519 `always', there the user will be prompted once for a file name for
520 each invocation of the saving commands."
521   :group 'gnus-article-saving
522   :type '(choice (item always)
523                  (item :tag "never" nil)
524                  (sexp :tag "once" :format "%t\n" :value t)))
525
526 (defcustom gnus-saved-headers gnus-visible-headers
527   "Headers to keep if `gnus-save-all-headers' is nil.
528 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
529 If that variable is nil, however, all headers that match this regexp
530 will be kept while the rest will be deleted before saving.  This and
531 `gnus-save-all-headers' will be overridden by the `:headers' property
532 that the symbol of the saver function, which is specified by
533 `gnus-default-article-saver', might have."
534   :group 'gnus-article-saving
535   :type 'regexp)
536
537 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
538 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
539   "A function to save articles in your favourite format.
540 The function will be called by way of the `gnus-summary-save-article'
541 command, and friends such as `gnus-summary-save-article-rmail'.
542
543 Gnus provides the following functions:
544
545 * gnus-summary-save-in-rmail (Rmail format)
546 * gnus-summary-save-in-mail (Unix mail format)
547 * gnus-summary-save-in-folder (MH folder)
548 * gnus-summary-save-in-file (article format)
549 * gnus-summary-save-body-in-file (article body)
550 * gnus-summary-save-in-vm (use VM's folder format)
551 * gnus-summary-write-to-file (article format -- overwrite)
552 * gnus-summary-write-body-to-file (article body -- overwrite)
553 * gnus-summary-save-in-pipe (article format)
554
555 The symbol of each function may have the following properties:
556
557 * :decode
558 The value non-nil means save decoded articles.  This is meaningful
559 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
560 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
561 `gnus-summary-save-in-pipe'.
562
563 * :function
564 The value specifies an alternative function which appends, not
565 overwrites, articles to a file.  This implies that when saving many
566 articles at a time, `gnus-prompt-before-saving' is bound to t and all
567 articles are saved in a single file.  This is meaningful only with
568 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
569
570 * :headers
571 The value specifies the symbol of a variable of which the value
572 specifies headers to be saved.  If it is omitted,
573 `gnus-save-all-headers' and `gnus-saved-headers' control what
574 headers should be saved."
575   :group 'gnus-article-saving
576   :type '(radio (function-item gnus-summary-save-in-rmail)
577                 (function-item gnus-summary-save-in-mail)
578                 (function-item gnus-summary-save-in-folder)
579                 (function-item gnus-summary-save-in-file)
580                 (function-item gnus-summary-save-body-in-file)
581                 (function-item gnus-summary-save-in-vm)
582                 (function-item gnus-summary-write-to-file)
583                 (function-item gnus-summary-write-body-to-file)
584                 (function-item gnus-summary-save-in-pipe)
585                 (function)))
586
587 (defcustom gnus-article-save-coding-system
588   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
589       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
590       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
591       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
592   "Coding system used to save decoded articles to a file.
593
594 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
595 which can safely encode any characters in text.  This is used by the
596 commands including:
597
598 * gnus-summary-save-article-file
599 * gnus-summary-save-article-body-file
600 * gnus-summary-write-article-file
601 * gnus-summary-write-article-body-file
602
603 and the functions to which you may set `gnus-default-article-saver':
604
605 * gnus-summary-save-in-file
606 * gnus-summary-save-body-in-file
607 * gnus-summary-write-to-file
608 * gnus-summary-write-body-to-file
609
610 Those commands and functions save just text displayed in the article
611 buffer to a file if the value of this variable is non-nil.  Note that
612 buttonized MIME parts will be lost in a saved file in that case.
613 Otherwise, raw articles will be saved."
614   :group 'gnus-article-saving
615   :type `(choice
616           :format "%{%t%}:\n %[Value Menu%] %v"
617           (const :tag "Save raw articles" nil)
618           ,@(delq nil
619                   (mapcar
620                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
621                    '((const :tag "UTF-8" utf-8)
622                      (const :tag "iso-2022-7bit" iso-2022-7bit)
623                      (const :tag "Emacs internal" emacs-mule)
624                      (const :tag "escape-quoted" escape-quoted))))
625           (symbol :tag "Coding system")))
626
627 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
628   "A function generating a file name to save articles in Rmail format.
629 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
630   :group 'gnus-article-saving
631   :type 'function)
632
633 (defcustom gnus-mail-save-name 'gnus-plain-save-name
634   "A function generating a file name to save articles in Unix mail format.
635 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
636   :group 'gnus-article-saving
637   :type 'function)
638
639 (defcustom gnus-folder-save-name 'gnus-folder-save-name
640   "A function generating a file name to save articles in MH folder.
641 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
642   :group 'gnus-article-saving
643   :type 'function)
644
645 (defcustom gnus-file-save-name 'gnus-numeric-save-name
646   "A function generating a file name to save articles in article format.
647 The function is called with NEWSGROUP, HEADERS, and optional
648 LAST-FILE."
649   :group 'gnus-article-saving
650   :type 'function)
651
652 (defcustom gnus-split-methods
653   '((gnus-article-archive-name)
654     (gnus-article-nndoc-name))
655   "*Variable used to suggest where articles are to be saved.
656 For instance, if you would like to save articles related to Gnus in
657 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
658 you could set this variable to something like:
659
660  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
661    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
662
663 This variable is an alist where the key is the match and the
664 value is a list of possible files to save in if the match is
665 non-nil.
666
667 If the match is a string, it is used as a regexp match on the
668 article.  If the match is a symbol, that symbol will be funcalled
669 from the buffer of the article to be saved with the newsgroup as the
670 parameter.  If it is a list, it will be evaled in the same buffer.
671
672 If this form or function returns a string, this string will be used as a
673 possible file name; and if it returns a non-nil list, that list will be
674 used as possible file names."
675   :group 'gnus-article-saving
676   :type '(repeat (choice (list :value (fun) function)
677                          (cons :value ("" "") regexp (repeat string))
678                          (sexp :value nil))))
679
680 (defcustom gnus-page-delimiter "^\^L"
681   "*Regexp describing what to use as article page delimiters.
682 The default value is \"^\^L\", which is a form linefeed at the
683 beginning of a line."
684   :type 'regexp
685   :group 'gnus-article-various)
686
687 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
688   "*The format specification for the article mode line.
689 See `gnus-summary-mode-line-format' for a closer description.
690
691 The following additional specs are available:
692
693 %w  The article washing status.
694 %m  The number of MIME parts in the article."
695   :type 'string
696   :group 'gnus-article-various)
697
698 (defcustom gnus-article-mode-hook nil
699   "*A hook for Gnus article mode."
700   :type 'hook
701   :group 'gnus-article-various)
702
703 (when (featurep 'xemacs)
704   ;; Extracted from gnus-xmas-define in order to preserve user settings
705   (when (fboundp 'turn-off-scroll-in-place)
706     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
707   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
708   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
709
710 (defcustom gnus-article-menu-hook nil
711   "*Hook run after the creation of the article mode menu."
712   :type 'hook
713   :group 'gnus-article-various)
714
715 (defcustom gnus-article-prepare-hook nil
716   "*A hook called after an article has been prepared in the article buffer."
717   :type 'hook
718   :group 'gnus-article-various)
719
720 (defcustom gnus-copy-article-ignored-headers nil
721   "List of headers to be removed when copying an article.
722 Each element is a regular expression."
723   :version "23.1" ;; No Gnus
724   :type '(repeat regexp)
725   :group 'gnus-article-various)
726
727 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
728                         "Gnus 5.10 (Emacs-22.1)")
729
730 (defface gnus-button
731   '((t (:weight bold)))
732   "Face used for highlighting a button in the article buffer."
733   :group 'gnus-article-buttons)
734
735 (defcustom gnus-article-button-face 'gnus-button
736   "Face used for highlighting buttons in the article buffer.
737
738 An article button is a piece of text that you can activate by pressing
739 `RET' or `mouse-2' above it."
740   :type 'face
741   :group 'gnus-article-buttons)
742
743 (defcustom gnus-article-mouse-face 'highlight
744   "Face used for mouse highlighting in the article buffer.
745
746 Article buttons will be displayed in this face when the cursor is
747 above them."
748   :type 'face
749   :group 'gnus-article-buttons)
750
751 (defcustom gnus-signature-face 'gnus-signature
752   "Face used for highlighting a signature in the article buffer.
753 Obsolete; use the face `gnus-signature' for customizations instead."
754   :type 'face
755   :group 'gnus-article-highlight
756   :group 'gnus-article-signature)
757
758 (defface gnus-signature
759   '((t
760      (:italic t)))
761   "Face used for highlighting a signature in the article buffer."
762   :group 'gnus-article-highlight
763   :group 'gnus-article-signature)
764 ;; backward-compatibility alias
765 (put 'gnus-signature-face 'face-alias 'gnus-signature)
766 (put 'gnus-signature-face 'obsolete-face "22.1")
767
768 (defface gnus-header-from
769   '((((class color)
770       (background dark))
771      (:foreground "PaleGreen1"))
772     (((class color)
773       (background light))
774      (:foreground "red3"))
775     (t
776      (:italic t)))
777   "Face used for displaying from headers."
778   :group 'gnus-article-headers
779   :group 'gnus-article-highlight)
780 ;; backward-compatibility alias
781 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
782 (put 'gnus-header-from-face 'obsolete-face "22.1")
783
784 (defface gnus-header-subject
785   '((((class color)
786       (background dark))
787      (:foreground "SeaGreen1"))
788     (((class color)
789       (background light))
790      (:foreground "red4"))
791     (t
792      (:bold t :italic t)))
793   "Face used for displaying subject headers."
794   :group 'gnus-article-headers
795   :group 'gnus-article-highlight)
796 ;; backward-compatibility alias
797 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
798 (put 'gnus-header-subject-face 'obsolete-face "22.1")
799
800 (defface gnus-header-newsgroups
801   '((((class color)
802       (background dark))
803      (:foreground "yellow" :italic t))
804     (((class color)
805       (background light))
806      (:foreground "MidnightBlue" :italic t))
807     (t
808      (:italic t)))
809   "Face used for displaying newsgroups headers.
810 In the default setup this face is only used for crossposted
811 articles."
812   :group 'gnus-article-headers
813   :group 'gnus-article-highlight)
814 ;; backward-compatibility alias
815 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
816 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
817
818 (defface gnus-header-name
819   '((((class color)
820       (background dark))
821      (:foreground "SpringGreen2"))
822     (((class color)
823       (background light))
824      (:foreground "maroon"))
825     (t
826      (:bold t)))
827   "Face used for displaying header names."
828   :group 'gnus-article-headers
829   :group 'gnus-article-highlight)
830 ;; backward-compatibility alias
831 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
832 (put 'gnus-header-name-face 'obsolete-face "22.1")
833
834 (defface gnus-header-content
835   '((((class color)
836       (background dark))
837      (:foreground "SpringGreen1" :italic t))
838     (((class color)
839       (background light))
840      (:foreground "indianred4" :italic t))
841     (t
842      (:italic t)))  "Face used for displaying header content."
843   :group 'gnus-article-headers
844   :group 'gnus-article-highlight)
845 ;; backward-compatibility alias
846 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
847 (put 'gnus-header-content-face 'obsolete-face "22.1")
848
849 (defcustom gnus-header-face-alist
850   '(("From" nil gnus-header-from)
851     ("Subject" nil gnus-header-subject)
852     ("Newsgroups:.*," nil gnus-header-newsgroups)
853     ("" gnus-header-name gnus-header-content))
854   "*Controls highlighting of article headers.
855
856 An alist of the form (HEADER NAME CONTENT).
857
858 HEADER is a regular expression which should match the name of a
859 header and NAME and CONTENT are either face names or nil.
860
861 The name of each header field will be displayed using the face
862 specified by the first element in the list where HEADER matches
863 the header name and NAME is non-nil.  Similarly, the content will
864 be displayed by the first non-nil matching CONTENT face."
865   :group 'gnus-article-headers
866   :group 'gnus-article-highlight
867   :type '(repeat (list (regexp :tag "Header")
868                        (choice :tag "Name"
869                                (item :tag "skip" nil)
870                                (face :value default))
871                        (choice :tag "Content"
872                                (item :tag "skip" nil)
873                                (face :value default)))))
874
875 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
876                                           '((xface . (:face gnus-x-face)))
877                                         '((pbm . (:face gnus-x-face))
878                                           (png . nil)))
879   "Alist of image types and properties applied to Face and X-Face images.
880 Here are examples:
881
882 ;; Specify the altitude of Face images in the From header.
883 \(setq gnus-face-properties-alist
884       '((pbm . (:face gnus-x-face :ascent 80))
885         (png . (:ascent 80))))
886
887 ;; Show Face images as pressed buttons.
888 \(setq gnus-face-properties-alist
889       '((pbm . (:face gnus-x-face :relief -2))
890         (png . (:relief -2))))
891
892 See the manual for the valid properties for various image types.
893 Currently, `pbm' is used for X-Face images and `png' is used for Face
894 images in Emacs.  Only the `:face' property is effective on the `xface'
895 image type in XEmacs if it is built with the libcompface library."
896   :version "23.1" ;; No Gnus
897   :group 'gnus-article-headers
898   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
899
900 (defcustom gnus-article-decode-hook
901   '(article-decode-charset article-decode-encoded-words
902                            article-decode-group-name article-decode-idna-rhs)
903   "*Hook run to decode charsets in articles."
904   :group 'gnus-article-headers
905   :type 'hook)
906
907 (defcustom gnus-display-mime-function 'gnus-display-mime
908   "Function to display MIME articles."
909   :group 'gnus-article-mime
910   :type 'function)
911
912 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
913   "Function used to decode headers.")
914
915 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
916   "Function used to decode addresses.")
917
918 (defvar gnus-article-dumbquotes-map
919   '(("\200" "EUR")
920     ("\202" ",")
921     ("\203" "f")
922     ("\204" ",,")
923     ("\205" "...")
924     ("\213" "<")
925     ("\214" "OE")
926     ("\221" "`")
927     ("\222" "'")
928     ("\223" "``")
929     ("\224" "\"")
930     ("\225" "*")
931     ("\226" "-")
932     ("\227" "--")
933     ("\230" "~")
934     ("\231" "(TM)")
935     ("\233" ">")
936     ("\234" "oe")
937     ("\264" "'"))
938   "Table for MS-to-Latin1 translation.")
939
940 (defcustom gnus-ignored-mime-types nil
941   "List of MIME types that should be ignored by Gnus."
942   :version "21.1"
943   :group 'gnus-article-mime
944   :type '(repeat regexp))
945
946 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
947   "List of MIME types that should not be given buttons when rendered inline.
948 See also `gnus-buttonized-mime-types' which may override this variable.
949 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
950   :version "21.1"
951   :group 'gnus-article-mime
952   :type '(repeat regexp))
953
954 (defcustom gnus-buttonized-mime-types nil
955   "List of MIME types that should be given buttons when rendered inline.
956 If set, this variable overrides `gnus-unbuttonized-mime-types'.
957 To see e.g. security buttons you could set this to
958 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
959 this list to display radio buttons that allow you to choose one of two
960 media types those mails include.  See also `mm-discouraged-alternatives'.
961 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
962   :version "22.1"
963   :group 'gnus-article-mime
964   :type '(repeat regexp))
965
966 (defcustom gnus-inhibit-mime-unbuttonizing nil
967   "If non-nil, all MIME parts get buttons.
968 When nil (the default value), then some MIME parts do not get buttons,
969 as described by the variables `gnus-buttonized-mime-types' and
970 `gnus-unbuttonized-mime-types'."
971   :version "22.1"
972   :group 'gnus-article-mime
973   :type 'boolean)
974
975 (defcustom gnus-body-boundary-delimiter "_"
976   "String used to delimit header and body.
977 This variable is used by `gnus-article-treat-body-boundary' which can
978 be controlled by `gnus-treat-body-boundary'."
979   :version "22.1"
980   :group 'gnus-article-various
981   :type '(choice (item :tag "None" :value nil)
982                  string))
983
984 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
985                                   "/usr/share/picons")
986   "Defines the location of the faces database.
987 For information on obtaining this database of pretty pictures, please
988 see http://www.cs.indiana.edu/picons/ftp/index.html"
989   :version "22.1"
990   :type '(repeat directory)
991   :link '(url-link :tag "download"
992                    "http://www.cs.indiana.edu/picons/ftp/index.html")
993   :link '(custom-manual "(gnus)Picons")
994   :group 'gnus-picon)
995
996 (defun gnus-picons-installed-p ()
997   "Say whether picons are installed on your machine."
998   (let ((installed nil))
999     (dolist (database gnus-picon-databases)
1000       (when (file-exists-p database)
1001         (setq installed t)))
1002     installed))
1003
1004 (defcustom gnus-article-mime-part-function nil
1005   "Function called with a MIME handle as the argument.
1006 This is meant for people who want to do something automatic based
1007 on parts -- for instance, adding Vcard info to a database."
1008   :group 'gnus-article-mime
1009   :type '(choice (const nil)
1010                  function))
1011
1012 (defcustom gnus-mime-multipart-functions nil
1013   "An alist of MIME types to functions to display them."
1014   :version "21.1"
1015   :group 'gnus-article-mime
1016   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1017
1018 (defcustom gnus-article-date-lapsed-new-header nil
1019   "Whether the X-Sent and Date headers can coexist.
1020 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
1021 either replace the old \"Date:\" header (if this variable is nil), or
1022 be added below it (otherwise)."
1023   :version "21.1"
1024   :group 'gnus-article-headers
1025   :type 'boolean)
1026
1027 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1028   "Function called with a MIME handle as the argument.
1029 This is meant for people who want to view first matched part.
1030 For `undisplayed-alternative' (default), the first undisplayed
1031 part or alternative part is used.  For `undisplayed', the first
1032 undisplayed part is used.  For a function, the first part which
1033 the function return t is used.  For nil, the first part is
1034 used."
1035   :version "21.1"
1036   :group 'gnus-article-mime
1037   :type '(choice
1038           (item :tag "first" :value nil)
1039           (item :tag "undisplayed" :value undisplayed)
1040           (item :tag "undisplayed or alternative"
1041                 :value undisplayed-alternative)
1042           (function)))
1043
1044 (defcustom gnus-mime-action-alist
1045   '(("save to file" . gnus-mime-save-part)
1046     ("save and strip" . gnus-mime-save-part-and-strip)
1047     ("replace with file" . gnus-mime-replace-part)
1048     ("delete part" . gnus-mime-delete-part)
1049     ("display as text" . gnus-mime-inline-part)
1050     ("view the part" . gnus-mime-view-part)
1051     ("pipe to command" . gnus-mime-pipe-part)
1052     ("toggle display" . gnus-article-press-button)
1053     ("toggle display" . gnus-article-view-part-as-charset)
1054     ("view as type" . gnus-mime-view-part-as-type)
1055     ("view internally" . gnus-mime-view-part-internally)
1056     ("view externally" . gnus-mime-view-part-externally))
1057   "An alist of actions that run on the MIME attachment."
1058   :group 'gnus-article-mime
1059   :type '(repeat (cons (string :tag "name")
1060                        (function))))
1061
1062 (defcustom gnus-auto-select-part 1
1063   "Advance to next MIME part when deleting or stripping parts.
1064
1065 When 0, point will be placed on the same part as before.  When
1066 positive (negative), move point forward (backwards) this many
1067 parts.  When nil, redisplay article."
1068   :version "23.1" ;; No Gnus
1069   :group 'gnus-article-mime
1070   :type '(choice (const nil :tag "Redisplay article.")
1071                  (const 1 :tag "Next part.")
1072                  (const 0 :tag "Current part.")
1073                  integer))
1074
1075 ;;;
1076 ;;; The treatment variables
1077 ;;;
1078
1079 (defvar gnus-part-display-hook nil
1080   "Hook called on parts that are to receive treatment.")
1081
1082 (defvar gnus-article-treat-custom
1083   '(choice (const :tag "Off" nil)
1084            (const :tag "On" t)
1085            (const :tag "Header" head)
1086            (const :tag "First" first)
1087            (const :tag "Last" last)
1088            (integer :tag "Less")
1089            (repeat :tag "Groups" regexp)
1090            (sexp :tag "Predicate")))
1091
1092 (defvar gnus-article-treat-head-custom
1093   '(choice (const :tag "Off" nil)
1094            (const :tag "Header" head)))
1095
1096 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1097                                    "text/x-patch")
1098   "Parts to treat.")
1099
1100 (defvar gnus-inhibit-treatment nil
1101   "Whether to inhibit treatment.")
1102
1103 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1104   "Highlight the signature.
1105 Valid values are nil, t, `head', `first', `last', an integer or a
1106 predicate.  See Info node `(gnus)Customizing Articles'."
1107   :group 'gnus-article-treat
1108   :link '(custom-manual "(gnus)Customizing Articles")
1109   :type gnus-article-treat-custom)
1110 (put 'gnus-treat-highlight-signature 'highlight t)
1111
1112 (defcustom gnus-treat-buttonize 100000
1113   "Add buttons.
1114 Valid values are nil, t, `head', `first', `last', an integer or a
1115 predicate.  See Info node `(gnus)Customizing Articles'."
1116   :group 'gnus-article-treat
1117   :link '(custom-manual "(gnus)Customizing Articles")
1118   :type gnus-article-treat-custom)
1119 (put 'gnus-treat-buttonize 'highlight t)
1120
1121 (defcustom gnus-treat-buttonize-head 'head
1122   "Add buttons to the head.
1123 Valid values are nil, t, `head', `first', `last', an integer or a
1124 predicate.  See Info node `(gnus)Customizing Articles'."
1125   :group 'gnus-article-treat
1126   :link '(custom-manual "(gnus)Customizing Articles")
1127   :type gnus-article-treat-head-custom)
1128 (put 'gnus-treat-buttonize-head 'highlight t)
1129
1130 (defcustom gnus-treat-emphasize 50000
1131   "Emphasize text.
1132 Valid values are nil, t, `head', `first', `last', an integer or a
1133 predicate.  See Info node `(gnus)Customizing Articles'."
1134   :group 'gnus-article-treat
1135   :link '(custom-manual "(gnus)Customizing Articles")
1136   :type gnus-article-treat-custom)
1137 (put 'gnus-treat-emphasize 'highlight t)
1138
1139 (defcustom gnus-treat-strip-cr nil
1140   "Remove carriage returns.
1141 Valid values are nil, t, `head', `first', `last', an integer or a
1142 predicate.  See Info node `(gnus)Customizing Articles'."
1143   :version "22.1"
1144   :group 'gnus-article-treat
1145   :link '(custom-manual "(gnus)Customizing Articles")
1146   :type gnus-article-treat-custom)
1147
1148 (defcustom gnus-treat-unsplit-urls nil
1149   "Remove newlines from within URLs.
1150 Valid values are nil, t, `head', `first', `last', an integer or a
1151 predicate.  See Info node `(gnus)Customizing Articles'."
1152   :version "22.1"
1153   :group 'gnus-article-treat
1154   :link '(custom-manual "(gnus)Customizing Articles")
1155   :type gnus-article-treat-custom)
1156
1157 (defcustom gnus-treat-leading-whitespace nil
1158   "Remove leading whitespace in headers.
1159 Valid values are nil, t, `head', `first', `last', an integer or a
1160 predicate.  See Info node `(gnus)Customizing Articles'."
1161   :version "22.1"
1162   :group 'gnus-article-treat
1163   :link '(custom-manual "(gnus)Customizing Articles")
1164   :type gnus-article-treat-custom)
1165
1166 (defcustom gnus-treat-hide-headers 'head
1167   "Hide headers.
1168 Valid values are nil, t, `head', `first', `last', an integer or a
1169 predicate.  See Info node `(gnus)Customizing Articles'."
1170   :group 'gnus-article-treat
1171   :link '(custom-manual "(gnus)Customizing Articles")
1172   :type gnus-article-treat-head-custom)
1173
1174 (defcustom gnus-treat-hide-boring-headers nil
1175   "Hide boring headers.
1176 Valid values are nil, t, `head', `first', `last', an integer or a
1177 predicate.  See Info node `(gnus)Customizing Articles'."
1178   :group 'gnus-article-treat
1179   :link '(custom-manual "(gnus)Customizing Articles")
1180   :type gnus-article-treat-head-custom)
1181
1182 (defcustom gnus-treat-hide-signature nil
1183   "Hide the signature.
1184 Valid values are nil, t, `head', `first', `last', an integer or a
1185 predicate.  See Info node `(gnus)Customizing Articles'."
1186   :group 'gnus-article-treat
1187   :link '(custom-manual "(gnus)Customizing Articles")
1188   :type gnus-article-treat-custom)
1189
1190 (defcustom gnus-treat-fill-article nil
1191   "Fill the article.
1192 Valid values are nil, t, `head', `first', `last', an integer or a
1193 predicate.  See Info node `(gnus)Customizing Articles'."
1194   :group 'gnus-article-treat
1195   :link '(custom-manual "(gnus)Customizing Articles")
1196   :type gnus-article-treat-custom)
1197
1198 (defcustom gnus-treat-hide-citation nil
1199   "Hide cited text.
1200 Valid values are nil, t, `head', `first', `last', an integer or a
1201 predicate.  See Info node `(gnus)Customizing Articles'."
1202   :group 'gnus-article-treat
1203   :link '(custom-manual "(gnus)Customizing Articles")
1204   :type gnus-article-treat-custom)
1205
1206 (defcustom gnus-treat-hide-citation-maybe nil
1207   "Hide cited text.
1208 Valid values are nil, t, `head', `first', `last', an integer or a
1209 predicate.  See Info node `(gnus)Customizing Articles'."
1210   :group 'gnus-article-treat
1211   :link '(custom-manual "(gnus)Customizing Articles")
1212   :type gnus-article-treat-custom)
1213
1214 (defcustom gnus-treat-strip-list-identifiers 'head
1215   "Strip list identifiers from `gnus-list-identifiers`.
1216 Valid values are nil, t, `head', `first', `last', an integer or a
1217 predicate.  See Info node `(gnus)Customizing Articles'."
1218   :version "21.1"
1219   :group 'gnus-article-treat
1220   :link '(custom-manual "(gnus)Customizing Articles")
1221   :type gnus-article-treat-custom)
1222
1223 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1224                         "Gnus 5.10 (Emacs 22.1)")
1225
1226 (defcustom gnus-treat-strip-pem nil
1227   "Strip PEM signatures.
1228 Valid values are nil, t, `head', `first', `last', an integer or a
1229 predicate.  See Info node `(gnus)Customizing Articles'."
1230   :group 'gnus-article-treat
1231   :link '(custom-manual "(gnus)Customizing Articles")
1232   :type gnus-article-treat-custom)
1233
1234 (defcustom gnus-treat-strip-banner t
1235   "Strip banners from articles.
1236 The banner to be stripped is specified in the `banner' group parameter.
1237 Valid values are nil, t, `head', `first', `last', an integer or a
1238 predicate.  See Info node `(gnus)Customizing Articles'."
1239   :group 'gnus-article-treat
1240   :link '(custom-manual "(gnus)Customizing Articles")
1241   :type gnus-article-treat-custom)
1242
1243 (defcustom gnus-treat-highlight-headers 'head
1244   "Highlight the headers.
1245 Valid values are nil, t, `head', `first', `last', an integer or a
1246 predicate.  See Info node `(gnus)Customizing Articles'."
1247   :group 'gnus-article-treat
1248   :link '(custom-manual "(gnus)Customizing Articles")
1249   :type gnus-article-treat-head-custom)
1250 (put 'gnus-treat-highlight-headers 'highlight t)
1251
1252 (defcustom gnus-treat-highlight-citation t
1253   "Highlight 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 (put 'gnus-treat-highlight-citation 'highlight t)
1260
1261 (defcustom gnus-treat-date-ut nil
1262   "Display the Date in UT (GMT).
1263 Valid values are nil, t, `head', `first', `last', an integer or a
1264 predicate.  See Info node `(gnus)Customizing Articles'."
1265   :group 'gnus-article-treat
1266   :link '(custom-manual "(gnus)Customizing Articles")
1267   :type gnus-article-treat-head-custom)
1268
1269 (defcustom gnus-treat-date-local nil
1270   "Display the Date in the local timezone.
1271 Valid values are nil, t, `head', `first', `last', an integer or a
1272 predicate.  See Info node `(gnus)Customizing Articles'."
1273   :group 'gnus-article-treat
1274   :link '(custom-manual "(gnus)Customizing Articles")
1275   :type gnus-article-treat-head-custom)
1276
1277 (defcustom gnus-treat-date-english nil
1278   "Display the Date in a format that can be read aloud in English.
1279 Valid values are nil, t, `head', `first', `last', an integer or a
1280 predicate.  See Info node `(gnus)Customizing Articles'."
1281   :version "22.1"
1282   :group 'gnus-article-treat
1283   :link '(custom-manual "(gnus)Customizing Articles")
1284   :type gnus-article-treat-head-custom)
1285
1286 (defcustom gnus-treat-date-lapsed nil
1287   "Display the Date header in a way that says how much time has elapsed.
1288 Valid values are nil, t, `head', `first', `last', an integer or a
1289 predicate.  See Info node `(gnus)Customizing Articles'."
1290   :group 'gnus-article-treat
1291   :link '(custom-manual "(gnus)Customizing Articles")
1292   :type gnus-article-treat-head-custom)
1293
1294 (defcustom gnus-treat-date-original nil
1295   "Display the date in the original timezone.
1296 Valid values are nil, t, `head', `first', `last', an integer or a
1297 predicate.  See Info node `(gnus)Customizing Articles'."
1298   :group 'gnus-article-treat
1299   :link '(custom-manual "(gnus)Customizing Articles")
1300   :type gnus-article-treat-head-custom)
1301
1302 (defcustom gnus-treat-date-iso8601 nil
1303   "Display the date in the ISO8601 format.
1304 Valid values are nil, t, `head', `first', `last', an integer or a
1305 predicate.  See Info node `(gnus)Customizing Articles'."
1306   :version "21.1"
1307   :group 'gnus-article-treat
1308   :link '(custom-manual "(gnus)Customizing Articles")
1309   :type gnus-article-treat-head-custom)
1310
1311 (defcustom gnus-treat-date-user-defined nil
1312   "Display the date in a user-defined format.
1313 The format is defined by the `gnus-article-time-format' variable.
1314 Valid values are nil, t, `head', `first', `last', an integer or a
1315 predicate.  See Info node `(gnus)Customizing Articles'."
1316   :group 'gnus-article-treat
1317   :link '(custom-manual "(gnus)Customizing Articles")
1318   :type gnus-article-treat-head-custom)
1319
1320 (defcustom gnus-treat-strip-headers-in-body t
1321   "Strip the X-No-Archive header line from the beginning of the body.
1322 Valid values are nil, t, `head', `first', `last', an integer or a
1323 predicate.  See Info node `(gnus)Customizing Articles'."
1324   :version "21.1"
1325   :group 'gnus-article-treat
1326   :link '(custom-manual "(gnus)Customizing Articles")
1327   :type gnus-article-treat-custom)
1328
1329 (defcustom gnus-treat-strip-trailing-blank-lines nil
1330   "Strip trailing blank lines.
1331 Valid values are nil, t, `head', `first', `last', an integer or a
1332 predicate.  See Info node `(gnus)Customizing Articles'.
1333
1334 When set to t, it also strips trailing blanks in all MIME parts.
1335 Consider to use `last' instead."
1336   :group 'gnus-article-treat
1337   :link '(custom-manual "(gnus)Customizing Articles")
1338   :type gnus-article-treat-custom)
1339
1340 (defcustom gnus-treat-strip-leading-blank-lines nil
1341   "Strip leading blank lines.
1342 Valid values are nil, t, `head', `first', `last', an integer or a
1343 predicate.  See Info node `(gnus)Customizing Articles'.
1344
1345 When set to t, it also strips trailing blanks in all MIME parts."
1346   :group 'gnus-article-treat
1347   :link '(custom-manual "(gnus)Customizing Articles")
1348   :type gnus-article-treat-custom)
1349
1350 (defcustom gnus-treat-strip-multiple-blank-lines nil
1351   "Strip multiple blank lines.
1352 Valid values are nil, t, `head', `first', `last', an integer or a
1353 predicate.  See Info node `(gnus)Customizing Articles'."
1354   :group 'gnus-article-treat
1355   :link '(custom-manual "(gnus)Customizing Articles")
1356   :type gnus-article-treat-custom)
1357
1358 (defcustom gnus-treat-unfold-headers 'head
1359   "Unfold folded header lines.
1360 Valid values are nil, t, `head', `first', `last', an integer or a
1361 predicate.  See Info node `(gnus)Customizing Articles'."
1362   :version "22.1"
1363   :group 'gnus-article-treat
1364   :link '(custom-manual "(gnus)Customizing Articles")
1365   :type gnus-article-treat-custom)
1366
1367 (defcustom gnus-article-unfold-long-headers nil
1368   "If non-nil, allow unfolding headers even if the header is long.
1369 If it is a regexp, only long headers matching this regexp are unfolded.
1370 If it is t, all long headers are unfolded.
1371
1372 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1373   :version "23.1" ;; No Gnus
1374   :group 'gnus-article-treat
1375   :type '(choice (const nil)
1376                  (const :tag "all" t)
1377                  (regexp)))
1378
1379 (defcustom gnus-treat-fold-headers nil
1380   "Fold headers.
1381 Valid values are nil, t, `head', `first', `last', an integer or a
1382 predicate.  See Info node `(gnus)Customizing Articles'."
1383   :version "22.1"
1384   :group 'gnus-article-treat
1385   :link '(custom-manual "(gnus)Customizing Articles")
1386   :type gnus-article-treat-custom)
1387
1388 (defcustom gnus-treat-fold-newsgroups 'head
1389   "Fold the Newsgroups and Followup-To headers.
1390 Valid values are nil, t, `head', `first', `last', an integer or a
1391 predicate.  See Info node `(gnus)Customizing Articles'."
1392   :version "22.1"
1393   :group 'gnus-article-treat
1394   :link '(custom-manual "(gnus)Customizing Articles")
1395   :type gnus-article-treat-custom)
1396
1397 (defcustom gnus-treat-overstrike t
1398   "Treat overstrike highlighting.
1399 Valid values are nil, t, `head', `first', `last', an integer or a
1400 predicate.  See Info node `(gnus)Customizing Articles'."
1401   :group 'gnus-article-treat
1402   :link '(custom-manual "(gnus)Customizing Articles")
1403   :type gnus-article-treat-custom)
1404 (put 'gnus-treat-overstrike 'highlight t)
1405
1406 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1407   "Treat ANSI SGR control sequences.
1408 Valid values are nil, t, `head', `first', `last', an integer or a
1409 predicate.  See Info node `(gnus)Customizing Articles'."
1410   :group 'gnus-article-treat
1411   :link '(custom-manual "(gnus)Customizing Articles")
1412   :type gnus-article-treat-custom)
1413
1414 (make-obsolete-variable 'gnus-treat-display-xface
1415                         'gnus-treat-display-x-face "22.1")
1416
1417 (defcustom gnus-treat-display-x-face
1418   (and (not noninteractive)
1419        (gnus-image-type-available-p 'xbm)
1420        (if (featurep 'xemacs)
1421            (featurep 'xface)
1422          (condition-case nil
1423              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1424                   (executable-find "icontopbm"))
1425            ;; shell-command-to-string may signal an error, e.g. if
1426            ;; shell-file-name is not found.
1427            (error nil)))
1428        'head)
1429   "Display X-Face headers.
1430 Valid values are nil and `head'.
1431 See Info node `(gnus)Customizing Articles' and Info node
1432 `(gnus)X-Face' for details."
1433   :group 'gnus-article-treat
1434   :version "21.1"
1435   :link '(custom-manual "(gnus)Customizing Articles")
1436   :link '(custom-manual "(gnus)X-Face")
1437   :type gnus-article-treat-head-custom
1438   :set (lambda (symbol value)
1439          (set-default
1440           symbol
1441           (cond ((or (boundp symbol) (get symbol 'saved-value))
1442                  value)
1443                 ((boundp 'gnus-treat-display-xface)
1444                  (message "\
1445 ** gnus-treat-display-xface is an obsolete variable;\
1446  use gnus-treat-display-x-face instead")
1447                  (default-value 'gnus-treat-display-xface))
1448                 ((get 'gnus-treat-display-xface 'saved-value)
1449                  (message "\
1450 ** gnus-treat-display-xface is an obsolete variable;\
1451  use gnus-treat-display-x-face instead")
1452                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1453                 (t
1454                  value)))))
1455 (put 'gnus-treat-display-x-face 'highlight t)
1456
1457 (defcustom gnus-treat-display-face
1458   (and (not noninteractive)
1459        (gnus-image-type-available-p 'png)
1460        'head)
1461   "Display Face headers.
1462 Valid values are nil, t, `head', `first', `last', an integer or a
1463 predicate.  See Info node `(gnus)Customizing Articles' and Info
1464 node `(gnus)Face' for details."
1465   :group 'gnus-article-treat
1466   :version "22.1"
1467   :link '(custom-manual "(gnus)Customizing Articles")
1468   :link '(custom-manual "(gnus)X-Face")
1469   :type gnus-article-treat-head-custom)
1470 (put 'gnus-treat-display-face 'highlight t)
1471
1472 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1473   "Display smileys.
1474 Valid values are nil, t, `head', `first', `last', an integer or a
1475 predicate.  See Info node `(gnus)Customizing Articles' and Info
1476 node `(gnus)Smileys' for details."
1477   :group 'gnus-article-treat
1478   :version "21.1"
1479   :link '(custom-manual "(gnus)Customizing Articles")
1480   :link '(custom-manual "(gnus)Smileys")
1481   :type gnus-article-treat-custom)
1482 (put 'gnus-treat-display-smileys 'highlight t)
1483
1484 (defcustom gnus-treat-from-picon
1485   (if (and (gnus-image-type-available-p 'xpm)
1486            (gnus-picons-installed-p))
1487       'head nil)
1488   "Display picons in the From header.
1489 Valid values are nil, t, `head', `first', `last', an integer or a
1490 predicate.  See Info node `(gnus)Customizing Articles' and Info
1491 node `(gnus)Picons' for details."
1492   :version "22.1"
1493   :group 'gnus-article-treat
1494   :group 'gnus-picon
1495   :link '(custom-manual "(gnus)Customizing Articles")
1496   :link '(custom-manual "(gnus)Picons")
1497   :type gnus-article-treat-head-custom)
1498 (put 'gnus-treat-from-picon 'highlight t)
1499
1500 (defcustom gnus-treat-mail-picon
1501   (if (and (gnus-image-type-available-p 'xpm)
1502            (gnus-picons-installed-p))
1503       'head nil)
1504   "Display picons in To and Cc headers.
1505 Valid values are nil, t, `head', `first', `last', an integer or a
1506 predicate.  See Info node `(gnus)Customizing Articles' and Info
1507 node `(gnus)Picons' for details."
1508   :version "22.1"
1509   :group 'gnus-article-treat
1510   :group 'gnus-picon
1511   :link '(custom-manual "(gnus)Customizing Articles")
1512   :link '(custom-manual "(gnus)Picons")
1513   :type gnus-article-treat-head-custom)
1514 (put 'gnus-treat-mail-picon 'highlight t)
1515
1516 (defcustom gnus-treat-newsgroups-picon
1517   (if (and (gnus-image-type-available-p 'xpm)
1518            (gnus-picons-installed-p))
1519       'head nil)
1520   "Display picons in the Newsgroups and Followup-To headers.
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)Picons' for details."
1524   :version "22.1"
1525   :group 'gnus-article-treat
1526   :group 'gnus-picon
1527   :link '(custom-manual "(gnus)Customizing Articles")
1528   :link '(custom-manual "(gnus)Picons")
1529   :type gnus-article-treat-head-custom)
1530 (put 'gnus-treat-newsgroups-picon 'highlight t)
1531
1532 (defcustom gnus-treat-from-gravatar
1533   (when (display-images-p) 'head)
1534   "Display gravatars in the From header.
1535 Valid values are nil, t, `head', `first', `last', an integer or a
1536 predicate.  See Info node `(gnus)Customizing Articles' and Info
1537 node `(gnus)Gravatars' for details."
1538   :version "24.1"
1539   :group 'gnus-article-treat
1540   :group 'gnus-gravatar
1541   :link '(custom-manual "(gnus)Customizing Articles")
1542   :link '(custom-manual "(gnus)Gravatars")
1543   :type gnus-article-treat-head-custom)
1544 (put 'gnus-treat-from-gravatar 'highlight t)
1545
1546 (defcustom gnus-treat-mail-gravatar
1547   (when (display-images-p) 'head)
1548   "Display gravatars in To and Cc headers.
1549 Valid values are nil, t, `head', `first', `last', an integer or a
1550 predicate.  See Info node `(gnus)Customizing Articles' and Info
1551 node `(gnus)Gravatars' for details."
1552   :version "24.1"
1553   :group 'gnus-article-treat
1554   :group 'gnus-gravatar
1555   :link '(custom-manual "(gnus)Customizing Articles")
1556   :link '(custom-manual "(gnus)Gravatars")
1557   :type gnus-article-treat-head-custom)
1558 (put 'gnus-treat-mail-gravatar 'highlight t)
1559
1560 (defcustom gnus-treat-body-boundary
1561   (if (or gnus-treat-newsgroups-picon
1562           gnus-treat-mail-picon
1563           gnus-treat-from-picon
1564           gnus-treat-from-gravatar
1565           gnus-treat-mail-gravatar)
1566       ;; If there's much decoration, the user might prefer a boundery.
1567       'head
1568     nil)
1569   "Draw a boundary at the end of the headers.
1570 Valid values are nil and `head'.
1571 See Info node `(gnus)Customizing Articles' for details."
1572   :version "22.1"
1573   :group 'gnus-article-treat
1574   :link '(custom-manual "(gnus)Customizing Articles")
1575   :type gnus-article-treat-head-custom)
1576
1577 (defcustom gnus-treat-capitalize-sentences nil
1578   "Capitalize sentence-starting words.
1579 Valid values are nil, t, `head', `first', `last', an integer or a
1580 predicate.  See Info node `(gnus)Customizing Articles'."
1581   :version "21.1"
1582   :group 'gnus-article-treat
1583   :link '(custom-manual "(gnus)Customizing Articles")
1584   :type gnus-article-treat-custom)
1585
1586 (defcustom gnus-treat-wash-html nil
1587   "Format as HTML.
1588 Valid values are nil, t, `head', `first', `last', an integer or a
1589 predicate.  See Info node `(gnus)Customizing Articles'."
1590   :version "22.1"
1591   :group 'gnus-article-treat
1592   :link '(custom-manual "(gnus)Customizing Articles")
1593   :type gnus-article-treat-custom)
1594
1595 (defcustom gnus-treat-fill-long-lines nil
1596   "Fill long lines.
1597 Valid values are nil, t, `head', `first', `last', an integer or a
1598 predicate.  See Info node `(gnus)Customizing Articles'."
1599   :group 'gnus-article-treat
1600   :link '(custom-manual "(gnus)Customizing Articles")
1601   :type gnus-article-treat-custom)
1602
1603 (defcustom gnus-treat-play-sounds nil
1604   "Play sounds.
1605 Valid values are nil, t, `head', `first', `last', an integer or a
1606 predicate.  See Info node `(gnus)Customizing Articles'."
1607   :version "21.1"
1608   :group 'gnus-article-treat
1609   :link '(custom-manual "(gnus)Customizing Articles")
1610   :type gnus-article-treat-custom)
1611
1612 (defcustom gnus-treat-x-pgp-sig nil
1613   "Verify X-PGP-Sig.
1614 To automatically treat X-PGP-Sig, set it to head.
1615 Valid values are nil, t, `head', `first', `last', an integer or a
1616 predicate.  See Info node `(gnus)Customizing Articles'."
1617   :version "22.1"
1618   :group 'gnus-article-treat
1619   :group 'mime-security
1620   :link '(custom-manual "(gnus)Customizing Articles")
1621   :type gnus-article-treat-custom)
1622
1623 (defvar gnus-article-encrypt-protocol-alist
1624   '(("PGP" . mml2015-self-encrypt)))
1625
1626 ;; Set to nil if more than one protocol added to
1627 ;; gnus-article-encrypt-protocol-alist.
1628 (defcustom gnus-article-encrypt-protocol "PGP"
1629   "The protocol used for encrypt articles.
1630 It is a string, such as \"PGP\". If nil, ask user."
1631   :version "22.1"
1632   :type 'string
1633   :group 'mime-security)
1634
1635 (defvar gnus-article-wash-function nil
1636   "Function used for converting HTML into text.")
1637
1638 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1639                               (mm-coding-system-p 'utf-8)
1640                               (executable-find idna-program))
1641   "Whether IDNA decoding of headers is used when viewing messages.
1642 This requires GNU Libidn, and by default only enabled if it is found."
1643   :version "22.1"
1644   :group 'gnus-article-headers
1645   :type 'boolean)
1646
1647 (defcustom gnus-article-over-scroll nil
1648   "If non-nil, allow scrolling the article buffer even when there no more text."
1649   :version "22.1"
1650   :group 'gnus-article
1651   :type 'boolean)
1652
1653 ;;; Internal variables
1654
1655 (defvar gnus-english-month-names
1656   '("January" "February" "March" "April" "May" "June" "July" "August"
1657     "September" "October" "November" "December"))
1658
1659 (defvar article-goto-body-goes-to-point-min-p nil)
1660 (defvar gnus-article-wash-types nil)
1661 (defvar gnus-article-emphasis-alist nil)
1662 (defvar gnus-article-image-alist nil)
1663
1664 (defvar gnus-article-mime-handle-alist-1 nil)
1665 (defvar gnus-treatment-function-alist
1666   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1667     (gnus-treat-strip-banner gnus-article-strip-banner)
1668     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1669     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1670     (gnus-treat-buttonize gnus-article-add-buttons)
1671     (gnus-treat-fill-article gnus-article-fill-cited-article)
1672     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1673     (gnus-treat-strip-cr gnus-article-remove-cr)
1674     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1675     (gnus-treat-date-ut gnus-article-date-ut)
1676     (gnus-treat-date-local gnus-article-date-local)
1677     (gnus-treat-date-english gnus-article-date-english)
1678     (gnus-treat-date-original gnus-article-date-original)
1679     (gnus-treat-date-user-defined gnus-article-date-user)
1680     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1681     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1682     (gnus-treat-display-x-face gnus-article-display-x-face)
1683     (gnus-treat-display-face gnus-article-display-face)
1684     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1685     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1686     (gnus-treat-hide-signature gnus-article-hide-signature)
1687     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1688     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1689     (gnus-treat-strip-pem gnus-article-hide-pem)
1690     (gnus-treat-from-picon gnus-treat-from-picon)
1691     (gnus-treat-mail-picon gnus-treat-mail-picon)
1692     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1693     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1694     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1695     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1696     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1697     (gnus-treat-strip-trailing-blank-lines
1698      gnus-article-remove-trailing-blank-lines)
1699     (gnus-treat-strip-leading-blank-lines
1700      gnus-article-strip-leading-blank-lines)
1701     (gnus-treat-strip-multiple-blank-lines
1702      gnus-article-strip-multiple-blank-lines)
1703     (gnus-treat-overstrike gnus-article-treat-overstrike)
1704     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1705     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1706     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1707     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1708     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1709     (gnus-treat-display-smileys gnus-treat-smiley)
1710     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1711     (gnus-treat-wash-html gnus-article-wash-html)
1712     (gnus-treat-emphasize gnus-article-emphasize)
1713     (gnus-treat-hide-citation gnus-article-hide-citation)
1714     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1715     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1716     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1717     (gnus-treat-play-sounds gnus-earcon-display)))
1718
1719 (defvar gnus-article-mime-handle-alist nil)
1720 (defvar article-lapsed-timer nil)
1721 (defvar gnus-article-current-summary nil)
1722
1723 (defvar gnus-article-mode-syntax-table
1724   (let ((table (copy-syntax-table text-mode-syntax-table)))
1725     ;; This causes the citation match run O(2^n).
1726     ;; (modify-syntax-entry ?- "w" table)
1727     (modify-syntax-entry ?> ")<" table)
1728     (modify-syntax-entry ?< "(>" table)
1729     ;; make M-. in article buffers work for `foo' strings
1730     (modify-syntax-entry ?' " " table)
1731     (modify-syntax-entry ?` " " table)
1732     table)
1733   "Syntax table used in article mode buffers.
1734 Initialized from `text-mode-syntax-table.")
1735
1736 (defvar gnus-save-article-buffer nil)
1737
1738 (defvar gnus-number-of-articles-to-be-saved nil)
1739
1740 (defvar gnus-inhibit-hiding nil)
1741
1742 (defvar gnus-article-edit-mode nil)
1743
1744 ;;; Macros for dealing with the article buffer.
1745
1746 (defmacro gnus-with-article-headers (&rest forms)
1747   `(with-current-buffer gnus-article-buffer
1748      (save-restriction
1749        (let ((inhibit-read-only t)
1750              (inhibit-point-motion-hooks t)
1751              (case-fold-search t))
1752          (article-narrow-to-head)
1753          ,@forms))))
1754
1755 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1756 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1757
1758 (defmacro gnus-with-article-buffer (&rest forms)
1759   `(with-current-buffer gnus-article-buffer
1760      (let ((inhibit-read-only t))
1761        ,@forms)))
1762
1763 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1764 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1765
1766 (defun gnus-article-goto-header (header)
1767   "Go to HEADER, which is a regular expression."
1768   (re-search-forward (concat "^\\(" header "\\):") nil t))
1769
1770 (defsubst gnus-article-hide-text (b e props)
1771   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1772   (gnus-add-text-properties-when 'article-type nil b e props)
1773   (when (memq 'intangible props)
1774     (put-text-property
1775      (max (1- b) (point-min))
1776      b 'intangible (cddr (memq 'intangible props)))))
1777
1778 (defsubst gnus-article-unhide-text (b e)
1779   "Remove hidden text properties from region between B and E."
1780   (remove-text-properties b e gnus-hidden-properties)
1781   (when (memq 'intangible gnus-hidden-properties)
1782     (put-text-property (max (1- b) (point-min))
1783                        b 'intangible nil)))
1784
1785 (defun gnus-article-hide-text-type (b e type)
1786   "Hide text of TYPE between B and E."
1787   (gnus-add-wash-type type)
1788   (gnus-article-hide-text
1789    b e (cons 'article-type (cons type gnus-hidden-properties))))
1790
1791 (defun gnus-article-unhide-text-type (b e type)
1792   "Unhide text of TYPE between B and E."
1793   (gnus-delete-wash-type type)
1794   (remove-text-properties
1795    b e (cons 'article-type (cons type gnus-hidden-properties)))
1796   (when (memq 'intangible gnus-hidden-properties)
1797     (put-text-property (max (1- b) (point-min))
1798                        b 'intangible nil)))
1799
1800 (defun gnus-article-hide-text-of-type (type)
1801   "Hide text of TYPE in the current buffer."
1802   (save-excursion
1803     (let ((b (point-min))
1804           (e (point-max)))
1805       (while (setq b (text-property-any b e 'article-type type))
1806         (add-text-properties b (incf b) gnus-hidden-properties)))))
1807
1808 (defun gnus-article-delete-text-of-type (type)
1809   "Delete text of TYPE in the current buffer."
1810   (save-excursion
1811     (let ((b (point-min)))
1812       (if (eq type 'multipart)
1813           ;; Remove MIME buttons associated with multipart/alternative parts.
1814           (progn
1815             (goto-char b)
1816             (while (if (get-text-property (point) 'gnus-part)
1817                        (setq b (point))
1818                      (when (setq b (next-single-property-change (point)
1819                                                                 'gnus-part))
1820                        (goto-char b)
1821                        t))
1822               (end-of-line)
1823               (skip-chars-forward "\n")
1824               (when (eq (get-text-property b 'article-type) 'multipart)
1825                 (delete-region b (point)))))
1826         (while (setq b (text-property-any b (point-max) 'article-type type))
1827           (delete-region
1828            b (or (text-property-not-all b (point-max) 'article-type type)
1829                  (point-max))))))))
1830
1831 (defun gnus-article-delete-invisible-text ()
1832   "Delete all invisible text in the current buffer."
1833   (save-excursion
1834     (let ((b (point-min)))
1835       (while (setq b (text-property-any b (point-max) 'invisible t))
1836         (delete-region
1837          b (or (text-property-not-all b (point-max) 'invisible t)
1838                (point-max)))))))
1839
1840 (defun gnus-article-text-type-exists-p (type)
1841   "Say whether any text of type TYPE exists in the buffer."
1842   (text-property-any (point-min) (point-max) 'article-type type))
1843
1844 (defsubst gnus-article-header-rank ()
1845   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1846   (let ((list gnus-sorted-header-list)
1847         (i 1))
1848     (while list
1849       (if (looking-at (car list))
1850           (setq list nil)
1851         (setq list (cdr list))
1852         (incf i)))
1853       i))
1854
1855 (defun article-hide-headers (&optional arg delete)
1856   "Hide unwanted headers and possibly sort them as well."
1857   (interactive)
1858   ;; This function might be inhibited.
1859   (unless gnus-inhibit-hiding
1860     (let ((inhibit-read-only t)
1861           (case-fold-search t)
1862           (max (1+ (length gnus-sorted-header-list)))
1863           (inhibit-point-motion-hooks t)
1864           (cur (current-buffer))
1865           ignored visible beg)
1866       (save-excursion
1867         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1868         ;; group parameters, so we should go to the summary buffer.
1869         (when (prog1
1870                   (condition-case nil
1871                       (progn (set-buffer gnus-summary-buffer) t)
1872                     (error nil))
1873                 (setq ignored (when (not gnus-visible-headers)
1874                                 (cond ((stringp gnus-ignored-headers)
1875                                        gnus-ignored-headers)
1876                                       ((listp gnus-ignored-headers)
1877                                        (mapconcat 'identity
1878                                                   gnus-ignored-headers
1879                                                   "\\|"))))
1880                       visible (cond ((stringp gnus-visible-headers)
1881                                      gnus-visible-headers)
1882                                     ((and gnus-visible-headers
1883                                           (listp gnus-visible-headers))
1884                                      (mapconcat 'identity
1885                                                 gnus-visible-headers
1886                                                 "\\|")))))
1887           (set-buffer cur))
1888         (save-restriction
1889           ;; First we narrow to just the headers.
1890           (article-narrow-to-head)
1891           ;; Hide any "From " lines at the beginning of (mail) articles.
1892           (while (looking-at "From ")
1893             (forward-line 1))
1894           (unless (bobp)
1895             (delete-region (point-min) (point)))
1896           ;; Then treat the rest of the header lines.
1897           ;; Then we use the two regular expressions
1898           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1899           ;; select which header lines is to remain visible in the
1900           ;; article buffer.
1901           (while (re-search-forward "^[^ \t:]*:" nil t)
1902             (beginning-of-line)
1903             ;; Mark the rank of the header.
1904             (put-text-property
1905              (point) (1+ (point)) 'message-rank
1906              (if (or (and visible (looking-at visible))
1907                      (and ignored
1908                           (not (looking-at ignored))))
1909                  (gnus-article-header-rank)
1910                (+ 2 max)))
1911             (forward-line 1))
1912           (message-sort-headers-1)
1913           (when (setq beg (text-property-any
1914                            (point-min) (point-max) 'message-rank (+ 2 max)))
1915             ;; We delete the unwanted headers.
1916             (gnus-add-wash-type 'headers)
1917             (add-text-properties (point-min) (+ 5 (point-min))
1918                                  '(article-type headers dummy-invisible t))
1919             (delete-region beg (point-max))))))))
1920
1921 (defun article-hide-boring-headers (&optional arg)
1922   "Toggle hiding of headers that aren't very interesting.
1923 If given a negative prefix, always show; if given a positive prefix,
1924 always hide."
1925   (interactive (gnus-article-hidden-arg))
1926   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1927              (not gnus-show-all-headers))
1928     (save-excursion
1929       (save-restriction
1930         (let ((inhibit-read-only t)
1931               (inhibit-point-motion-hooks t))
1932           (article-narrow-to-head)
1933           (dolist (elem gnus-boring-article-headers)
1934             (goto-char (point-min))
1935             (cond
1936              ;; Hide empty headers.
1937              ((eq elem 'empty)
1938               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1939                 (forward-line -1)
1940                 (gnus-article-hide-text-type
1941                  (point-at-bol)
1942                  (progn
1943                    (end-of-line)
1944                    (if (re-search-forward "^[^ \t]" nil t)
1945                        (match-beginning 0)
1946                      (point-max)))
1947                  'boring-headers)))
1948              ;; Hide boring Newsgroups header.
1949              ((eq elem 'newsgroups)
1950               (when (gnus-string-equal
1951                      (gnus-fetch-field "newsgroups")
1952                      (gnus-group-real-name
1953                       (if (boundp 'gnus-newsgroup-name)
1954                           gnus-newsgroup-name
1955                         "")))
1956                 (gnus-article-hide-header "newsgroups")))
1957              ((eq elem 'to-address)
1958               (let ((to (message-fetch-field "to"))
1959                     (to-address
1960                      (gnus-parameter-to-address
1961                       (if (boundp 'gnus-newsgroup-name)
1962                           gnus-newsgroup-name ""))))
1963                 (when (and to to-address
1964                            (ignore-errors
1965                              (gnus-string-equal
1966                               ;; only one address in To
1967                               (nth 1 (mail-extract-address-components to))
1968                               to-address)))
1969                   (gnus-article-hide-header "to"))))
1970              ((eq elem 'to-list)
1971               (let ((to (message-fetch-field "to"))
1972                     (to-list
1973                      (gnus-parameter-to-list
1974                       (if (boundp 'gnus-newsgroup-name)
1975                           gnus-newsgroup-name ""))))
1976                 (when (and to to-list
1977                            (ignore-errors
1978                              (gnus-string-equal
1979                               ;; only one address in To
1980                               (nth 1 (mail-extract-address-components to))
1981                               to-list)))
1982                   (gnus-article-hide-header "to"))))
1983              ((eq elem 'cc-list)
1984               (let ((cc (message-fetch-field "cc"))
1985                     (to-list
1986                      (gnus-parameter-to-list
1987                       (if (boundp 'gnus-newsgroup-name)
1988                           gnus-newsgroup-name ""))))
1989                 (when (and cc to-list
1990                            (ignore-errors
1991                              (gnus-string-equal
1992                               ;; only one address in CC
1993                               (nth 1 (mail-extract-address-components cc))
1994                               to-list)))
1995                   (gnus-article-hide-header "cc"))))
1996              ((eq elem 'followup-to)
1997               (when (gnus-string-equal
1998                      (message-fetch-field "followup-to")
1999                      (message-fetch-field "newsgroups"))
2000                 (gnus-article-hide-header "followup-to")))
2001              ((eq elem 'reply-to)
2002               (if (gnus-group-find-parameter
2003                    gnus-newsgroup-name 'broken-reply-to)
2004                   (gnus-article-hide-header "reply-to")
2005                 (let ((from (message-fetch-field "from"))
2006                       (reply-to (message-fetch-field "reply-to")))
2007                   (when
2008                       (and
2009                        from reply-to
2010                        (ignore-errors
2011                          (equal
2012                           (sort (mapcar
2013                                  (lambda (x) (downcase (cadr x)))
2014                                  (mail-extract-address-components from t))
2015                                 'string<)
2016                           (sort (mapcar
2017                                  (lambda (x) (downcase (cadr x)))
2018                                  (mail-extract-address-components reply-to t))
2019                                 'string<))))
2020                     (gnus-article-hide-header "reply-to")))))
2021              ((eq elem 'date)
2022               (let ((date (with-current-buffer gnus-original-article-buffer
2023                             ;; If date in `gnus-article-buffer' is localized
2024                             ;; (`gnus-treat-date-user-defined'),
2025                             ;; `days-between' might fail.
2026                             (message-fetch-field "date"))))
2027                 (when (and date
2028                            (< (days-between (current-time-string) date)
2029                               4))
2030                   (gnus-article-hide-header "date"))))
2031              ((eq elem 'long-to)
2032               (let ((to (message-fetch-field "to"))
2033                     (cc (message-fetch-field "cc")))
2034                 (when (> (length to) 1024)
2035                   (gnus-article-hide-header "to"))
2036                 (when (> (length cc) 1024)
2037                   (gnus-article-hide-header "cc"))))
2038              ((eq elem 'many-to)
2039               (let ((to-count 0)
2040                     (cc-count 0))
2041                 (goto-char (point-min))
2042                 (while (re-search-forward "^to:" nil t)
2043                   (setq to-count (1+ to-count)))
2044                 (when (> to-count 1)
2045                   (while (> to-count 0)
2046                     (goto-char (point-min))
2047                     (save-restriction
2048                       (re-search-forward "^to:" nil nil to-count)
2049                       (forward-line -1)
2050                       (narrow-to-region (point) (point-max))
2051                       (gnus-article-hide-header "to"))
2052                     (setq to-count (1- to-count))))
2053                 (goto-char (point-min))
2054                 (while (re-search-forward "^cc:" nil t)
2055                   (setq cc-count (1+ cc-count)))
2056                 (when (> cc-count 1)
2057                   (while (> cc-count 0)
2058                     (goto-char (point-min))
2059                     (save-restriction
2060                       (re-search-forward "^cc:" nil nil cc-count)
2061                       (forward-line -1)
2062                       (narrow-to-region (point) (point-max))
2063                       (gnus-article-hide-header "cc"))
2064                     (setq cc-count (1- cc-count)))))))))))))
2065
2066 (defun gnus-article-hide-header (header)
2067   (save-excursion
2068     (goto-char (point-min))
2069     (when (re-search-forward (concat "^" header ":") nil t)
2070       (gnus-article-hide-text-type
2071        (point-at-bol)
2072        (progn
2073          (end-of-line)
2074          (if (re-search-forward "^[^ \t]" nil t)
2075              (match-beginning 0)
2076            (point-max)))
2077        'boring-headers))))
2078
2079 (defvar gnus-article-normalized-header-length 40
2080   "Length of normalized headers.")
2081
2082 (defun article-normalize-headers ()
2083   "Make all header lines 40 characters long."
2084   (interactive)
2085   (let ((inhibit-read-only t)
2086         column)
2087     (save-excursion
2088       (save-restriction
2089         (article-narrow-to-head)
2090         (while (not (eobp))
2091           (cond
2092            ((< (setq column (- (point-at-eol) (point)))
2093                gnus-article-normalized-header-length)
2094             (end-of-line)
2095             (insert (make-string
2096                      (- gnus-article-normalized-header-length column)
2097                      ? )))
2098            ((> column gnus-article-normalized-header-length)
2099             (gnus-put-text-property
2100              (progn
2101                (forward-char gnus-article-normalized-header-length)
2102                (point))
2103              (point-at-eol)
2104              'invisible t))
2105            (t
2106             ;; Do nothing.
2107             ))
2108           (forward-line 1))))))
2109
2110 (defun article-treat-dumbquotes ()
2111   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2112 Note that this function guesses whether a character is a sm*rtq**t* or
2113 not, so it should only be used interactively.
2114
2115 Sm*rtq**t*s are M****s***'s unilateral extension to the
2116 iso-8859-1 character map in an attempt to provide more quoting
2117 characters.  If you see something like \\222 or \\264 where
2118 you're expecting some kind of apostrophe or quotation mark, then
2119 try this wash."
2120   (interactive)
2121   (article-translate-strings gnus-article-dumbquotes-map))
2122
2123 (defun article-translate-characters (from to)
2124   "Translate all characters in the body of the article according to FROM and TO.
2125 FROM is a string of characters to translate from; to is a string of
2126 characters to translate to."
2127   (save-excursion
2128     (when (article-goto-body)
2129       (let ((inhibit-read-only t)
2130             (x (make-string 225 ?x))
2131             (i -1))
2132         (while (< (incf i) (length x))
2133           (aset x i i))
2134         (setq i 0)
2135         (while (< i (length from))
2136           (aset x (aref from i) (aref to i))
2137           (incf i))
2138         (translate-region (point) (point-max) x)))))
2139
2140 (defun article-translate-strings (map)
2141   "Translate all string in the body of the article according to MAP.
2142 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2143   (save-excursion
2144     (when (article-goto-body)
2145       (let ((inhibit-read-only t))
2146         (dolist (elem map)
2147           (save-excursion
2148             (while (search-forward (car elem) nil t)
2149               (replace-match (cadr elem)))))))))
2150
2151 (defun article-treat-overstrike ()
2152   "Translate overstrikes into bold text."
2153   (interactive)
2154   (save-excursion
2155     (when (article-goto-body)
2156       (let ((inhibit-read-only t))
2157         (while (search-forward "\b" nil t)
2158           (let ((next (char-after))
2159                 (previous (char-after (- (point) 2))))
2160             ;; We do the boldification/underlining by hiding the
2161             ;; overstrikes and putting the proper text property
2162             ;; on the letters.
2163             (cond
2164              ((eq next previous)
2165               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2166               (put-text-property (point) (1+ (point)) 'face 'bold))
2167              ((eq next ?_)
2168               (gnus-article-hide-text-type
2169                (1- (point)) (1+ (point)) 'overstrike)
2170               (put-text-property
2171                (- (point) 2) (1- (point)) 'face 'underline))
2172              ((eq previous ?_)
2173               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2174               (put-text-property
2175                (point) (1+ (point)) 'face 'underline)))))))))
2176
2177 (defun article-treat-ansi-sequences ()
2178   "Translate ANSI SGR control sequences into overlays or extents."
2179   (interactive)
2180   (save-excursion
2181     (when (article-goto-body)
2182       (let ((inhibit-read-only t))
2183         (ansi-color-apply-on-region (point) (point-max))))))
2184
2185 (defun gnus-article-treat-unfold-headers ()
2186   "Unfold folded message headers.
2187 Only the headers that fit into the current window width will be
2188 unfolded."
2189   (interactive)
2190   (gnus-with-article-headers
2191     (let (length)
2192       (while (not (eobp))
2193         (save-restriction
2194           (mail-header-narrow-to-field)
2195           (let* ((header (buffer-string))
2196                  (unfoldable
2197                   (or (equal gnus-article-unfold-long-headers t)
2198                       (and (stringp gnus-article-unfold-long-headers)
2199                            (string-match gnus-article-unfold-long-headers header)))))
2200             (with-temp-buffer
2201               (insert header)
2202               (goto-char (point-min))
2203               (while (re-search-forward "\n[\t ]" nil t)
2204                 (replace-match " " t t)))
2205             (setq length (- (point-max) (point-min) 1))
2206             (when (or unfoldable
2207                       (< length (window-width)))
2208               (while (re-search-forward "\n[\t ]" nil t)
2209                 (replace-match " " t t))))
2210           (goto-char (point-max)))))))
2211
2212 (defun gnus-article-treat-fold-headers ()
2213   "Fold message headers."
2214   (interactive)
2215   (gnus-with-article-headers
2216     (while (not (eobp))
2217       (save-restriction
2218         (mail-header-narrow-to-field)
2219         (mail-header-fold-field)
2220         (goto-char (point-max))))))
2221
2222 (defun gnus-treat-smiley ()
2223   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2224   (interactive)
2225   (gnus-with-article-buffer
2226     (if (memq 'smiley gnus-article-wash-types)
2227         (gnus-delete-images 'smiley)
2228       (article-goto-body)
2229       (let ((images (smiley-region (point) (point-max))))
2230         (when images
2231           (gnus-add-wash-type 'smiley)
2232           (dolist (image images)
2233             (gnus-add-image 'smiley image)))))))
2234
2235 (defun gnus-article-remove-images ()
2236   "Remove all images from the article buffer."
2237   (interactive)
2238   (gnus-with-article-buffer
2239     (dolist (elem gnus-article-image-alist)
2240       (gnus-delete-images (car elem)))))
2241
2242 (defun gnus-article-treat-fold-newsgroups ()
2243   "Unfold folded message headers.
2244 Only the headers that fit into the current window width will be
2245 unfolded."
2246   (interactive)
2247   (gnus-with-article-headers
2248     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2249       (save-restriction
2250         (mail-header-narrow-to-field)
2251         (while (re-search-forward ", *" nil t)
2252           (replace-match ", " t t))
2253         (mail-header-fold-field)
2254         (goto-char (point-max))))))
2255
2256 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2257   "Value of `truncate-lines' in Gnus Article buffer.
2258 Valid values are nil, t, `head', `first', `last', an integer or a
2259 predicate.  See Info node `(gnus)Customizing Articles'."
2260   :version "23.1" ;; No Gnus
2261   :group 'gnus-article
2262   ;; :link '(custom-manual "(gnus)Customizing Articles")
2263   :type 'boolean)
2264
2265 (defun gnus-article-toggle-truncate-lines (&optional arg)
2266   "Toggle whether to fold or truncate long lines in article the buffer.
2267 If ARG is non-nil and not a number, toggle
2268 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2269 long lines if and only if arg is positive."
2270   (interactive "P")
2271   (cond
2272    ((and (numberp arg) (> arg 0))
2273     (setq gnus-article-truncate-lines t))
2274    ((numberp arg)
2275     (setq gnus-article-truncate-lines nil))
2276    (arg
2277     (setq gnus-article-truncate-lines
2278           (not gnus-article-truncate-lines))))
2279   (gnus-with-article-buffer
2280     (cond
2281      ((and (numberp arg) (> arg 0))
2282       (setq truncate-lines nil))
2283      ((numberp arg)
2284       (setq truncate-lines t)))
2285     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2286     ;; `toggle-truncate-lines' needs an argument.
2287     (toggle-truncate-lines)))
2288
2289 (defun gnus-article-treat-body-boundary ()
2290   "Place a boundary line at the end of the headers."
2291   (interactive)
2292   (when (and gnus-body-boundary-delimiter
2293              (> (length gnus-body-boundary-delimiter) 0))
2294     (gnus-with-article-headers
2295       (goto-char (point-max))
2296       (let ((start (point)))
2297         (insert "X-Boundary: ")
2298         (gnus-add-text-properties start (point) '(invisible t intangible t))
2299         (insert (let (str)
2300                   (while (>= (1- (window-width)) (length str))
2301                     (setq str (concat str gnus-body-boundary-delimiter)))
2302                   (substring str 0 (1- (window-width))))
2303                 "\n")
2304         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2305
2306 (defun article-fill-long-lines ()
2307   "Fill lines that are wider than the window width."
2308   (interactive)
2309   (save-excursion
2310     (let ((inhibit-read-only t)
2311           (width (window-width (get-buffer-window (current-buffer)))))
2312       (save-restriction
2313         (article-goto-body)
2314         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2315           (while (not (eobp))
2316             (end-of-line)
2317             (when (>= (current-column) (min fill-column width))
2318               (narrow-to-region (min (1+ (point)) (point-max))
2319                                 (point-at-bol))
2320               (let ((goback (point-marker)))
2321                 (fill-paragraph nil)
2322                 (goto-char (marker-position goback)))
2323               (widen))
2324             (forward-line 1)))))))
2325
2326 (defun article-capitalize-sentences ()
2327   "Capitalize the first word in each sentence."
2328   (interactive)
2329   (save-excursion
2330     (let ((inhibit-read-only t)
2331           (paragraph-start "^[\n\^L]"))
2332       (article-goto-body)
2333       (while (not (eobp))
2334         (capitalize-word 1)
2335         (forward-sentence)))))
2336
2337 (defun article-remove-cr ()
2338   "Remove trailing CRs and then translate remaining CRs into LFs."
2339   (interactive)
2340   (save-excursion
2341     (let ((inhibit-read-only t))
2342       (goto-char (point-min))
2343       (while (re-search-forward "\r+$" nil t)
2344         (replace-match "" t t))
2345       (goto-char (point-min))
2346       (while (search-forward "\r" nil t)
2347         (replace-match "\n" t t)))))
2348
2349 (defun article-remove-trailing-blank-lines ()
2350   "Remove all trailing blank lines from the article."
2351   (interactive)
2352   (save-excursion
2353     (let ((inhibit-read-only t))
2354       (goto-char (point-max))
2355       (delete-region
2356        (point)
2357        (progn
2358          (while (and (not (bobp))
2359                      (looking-at "^[ \t]*$")
2360                      (not (gnus-annotation-in-region-p
2361                            (point) (point-at-eol))))
2362            (forward-line -1))
2363          (forward-line 1)
2364          (point))))))
2365
2366 (defvar gnus-face-properties-alist)
2367
2368 (defun article-display-face (&optional force)
2369   "Display any Face headers in the header."
2370   (interactive (list 'force))
2371   (let ((wash-face-p buffer-read-only))
2372     (gnus-with-article-headers
2373       ;; When displaying parts, this function can be called several times on
2374       ;; the same article, without any intended toggle semantic (as typing `W
2375       ;; D d' would have). So face deletion must occur only when we come from
2376       ;; an interactive command, that is when the *Article* buffer is
2377       ;; read-only.
2378       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2379           (gnus-delete-images 'face)
2380         (let ((from (message-fetch-field "from"))
2381               face faces)
2382           (save-current-buffer
2383             (when (and wash-face-p
2384                        (gnus-buffer-live-p gnus-original-article-buffer)
2385                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2386               (set-buffer gnus-original-article-buffer))
2387             (save-restriction
2388               (mail-narrow-to-head)
2389               (when (or force
2390                         ;; Check whether this face is censored.
2391                         (not (and gnus-article-x-face-too-ugly
2392                                   (or from
2393                                       (setq from (message-fetch-field "from")))
2394                                   (string-match gnus-article-x-face-too-ugly
2395                                                 from))))
2396                 (while (gnus-article-goto-header "Face")
2397                   (push (mail-header-field-value) faces)))))
2398           (when faces
2399             (goto-char (point-min))
2400             (let (png image)
2401               (unless (setq from (gnus-article-goto-header "from"))
2402                 (insert "From:")
2403                 (setq from (point))
2404                 (insert " [no `from' set]\n"))
2405               (while faces
2406                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2407                   (setq image
2408                         (apply 'gnus-create-image png 'png t
2409                                (cdr (assq 'png gnus-face-properties-alist))))
2410                   (goto-char from)
2411                   (gnus-add-wash-type 'face)
2412                   (gnus-add-image 'face image)
2413                   (gnus-put-image image nil 'face))))))))))
2414
2415 (defun article-display-x-face (&optional force)
2416   "Look for an X-Face header and display it if present."
2417   (interactive (list 'force))
2418   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2419     (gnus-with-article-headers
2420       ;; Delete the old process, if any.
2421       (when (process-status "article-x-face")
2422         (delete-process "article-x-face"))
2423       ;; See the comment in `article-display-face'.
2424       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2425           ;; We have already displayed X-Faces, so we remove them
2426           ;; instead.
2427           (gnus-delete-images 'xface)
2428         ;; Display X-Faces.
2429         (let ((from (message-fetch-field "from"))
2430               x-faces face)
2431           (save-current-buffer
2432             (when (and wash-face-p
2433                        (gnus-buffer-live-p gnus-original-article-buffer)
2434                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2435               ;; If type `W f', use gnus-original-article-buffer,
2436               ;; otherwise use the current buffer because displaying
2437               ;; RFC822 parts calls this function too.
2438               (set-buffer gnus-original-article-buffer))
2439             (save-restriction
2440               (mail-narrow-to-head)
2441               (and gnus-article-x-face-command
2442                    (or force
2443                        ;; Check whether this face is censored.
2444                        (not (and gnus-article-x-face-too-ugly
2445                                  (or from
2446                                      (setq from (message-fetch-field "from")))
2447                                  (string-match gnus-article-x-face-too-ugly
2448                                                from))))
2449                    (while (gnus-article-goto-header "X-Face")
2450                      (push (mail-header-field-value) x-faces)))))
2451           (when x-faces
2452             ;; We display the face.
2453             (cond ((functionp gnus-article-x-face-command)
2454                    ;; The command is a lisp function, so we call it.
2455                    (mapc gnus-article-x-face-command x-faces))
2456                   ((stringp gnus-article-x-face-command)
2457                    ;; The command is a string, so we interpret the command
2458                    ;; as a, well, command, and fork it off.
2459                    (let ((process-connection-type nil))
2460                      (gnus-set-process-query-on-exit-flag
2461                       (start-process
2462                        "article-x-face" nil shell-file-name
2463                        shell-command-switch gnus-article-x-face-command)
2464                       nil)
2465                      ;; Sending multiple EOFs to xv doesn't work,
2466                      ;; so we only do a single external face.
2467                      (with-temp-buffer
2468                        (insert (car x-faces))
2469                        (process-send-region "article-x-face"
2470                                             (point-min) (point-max)))
2471                      (process-send-eof "article-x-face")))
2472                   (t
2473                    (error "`%s' set to `%s' is not a function"
2474                           gnus-article-x-face-command
2475                           'gnus-article-x-face-command)))))))))
2476
2477 (defun article-decode-mime-words ()
2478   "Decode all MIME-encoded words in the article."
2479   (interactive)
2480   (gnus-with-article-buffer
2481     (let ((inhibit-point-motion-hooks t)
2482           (mail-parse-charset gnus-newsgroup-charset)
2483           (mail-parse-ignored-charsets
2484            (with-current-buffer gnus-summary-buffer
2485              gnus-newsgroup-ignored-charsets)))
2486       (mail-decode-encoded-word-region (point-min) (point-max)))))
2487
2488 (defun article-decode-charset (&optional prompt)
2489   "Decode charset-encoded text in the article.
2490 If PROMPT (the prefix), prompt for a coding system to use."
2491   (interactive "P")
2492   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2493         (inhibit-read-only t)
2494         (mail-parse-charset gnus-newsgroup-charset)
2495         (mail-parse-ignored-charsets
2496          (save-excursion (condition-case nil
2497                              (set-buffer gnus-summary-buffer)
2498                            (error))
2499                          gnus-newsgroup-ignored-charsets))
2500         ct cte ctl charset format)
2501     (save-excursion
2502       (save-restriction
2503         (article-narrow-to-head)
2504         (setq ct (message-fetch-field "Content-Type" t)
2505               cte (message-fetch-field "Content-Transfer-Encoding" t)
2506               ctl (and ct (mail-header-parse-content-type ct))
2507               charset (cond
2508                        (prompt
2509                         (mm-read-coding-system "Charset to decode: "))
2510                        (ctl
2511                         (mail-content-type-get ctl 'charset)))
2512               format (and ctl (mail-content-type-get ctl 'format)))
2513         (when cte
2514           (setq cte (mail-header-strip cte)))
2515         (if (and ctl (not (string-match "/" (car ctl))))
2516             (setq ctl nil))
2517         (goto-char (point-max)))
2518       (forward-line 1)
2519       (save-restriction
2520         (narrow-to-region (point) (point-max))
2521         (when (and (eq mail-parse-charset 'gnus-decoded)
2522                    (eq (mm-body-7-or-8) '8bit))
2523           ;; The text code could have been decoded.
2524           (setq charset mail-parse-charset))
2525         (when (and (or (not ctl)
2526                        (equal (car ctl) "text/plain"))
2527                    (not format)) ;; article with format will decode later.
2528           (mm-decode-body
2529            charset (and cte (intern (downcase
2530                                      (gnus-strip-whitespace cte))))
2531            (car ctl)))))))
2532
2533 (defun article-decode-encoded-words ()
2534   "Remove encoded-word encoding from headers."
2535   (let ((inhibit-point-motion-hooks t)
2536         (mail-parse-charset gnus-newsgroup-charset)
2537         (mail-parse-ignored-charsets
2538          (save-excursion (condition-case nil
2539                              (set-buffer gnus-summary-buffer)
2540                            (error))
2541                          gnus-newsgroup-ignored-charsets))
2542         (inhibit-read-only t)
2543         end start)
2544     (goto-char (point-min))
2545     (when (search-forward "\n\n" nil 'move)
2546       (forward-line -1))
2547     (setq end (point))
2548     (while (not (bobp))
2549       (while (progn
2550                (forward-line -1)
2551                (and (not (bobp))
2552                     (memq (char-after) '(?\t ? )))))
2553       (setq start (point))
2554       (if (looking-at "\
2555 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2556 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2557           (funcall gnus-decode-address-function start end)
2558         (funcall gnus-decode-header-function start end))
2559       (goto-char (setq end start)))))
2560
2561 (defun article-decode-group-name ()
2562   "Decode group names in Newsgroups, Followup-To and Xref headers."
2563   (let ((inhibit-point-motion-hooks t)
2564         (inhibit-read-only t)
2565         (method (gnus-find-method-for-group gnus-newsgroup-name))
2566         regexp)
2567     (when (and (or gnus-group-name-charset-method-alist
2568                    gnus-group-name-charset-group-alist)
2569                (gnus-buffer-live-p gnus-original-article-buffer))
2570       (save-restriction
2571         (article-narrow-to-head)
2572         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2573           (with-current-buffer gnus-original-article-buffer
2574             (goto-char (point-min)))
2575           (setq regexp (concat "^" header
2576                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2577           (while (re-search-forward regexp nil t)
2578             (replace-match (save-match-data
2579                              (gnus-decode-newsgroups
2580                               ;; XXX how to use data in article buffer?
2581                               (with-current-buffer gnus-original-article-buffer
2582                                 (re-search-forward regexp nil t)
2583                                 (match-string 1))
2584                               gnus-newsgroup-name method))
2585                            t t nil 1))
2586           (goto-char (point-min)))))))
2587
2588 (autoload 'idna-to-unicode "idna")
2589
2590 (defun article-decode-idna-rhs ()
2591   "Decode IDNA strings in RHS in various headers in current buffer.
2592 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2593 Mail-Reply-To: and Mail-Followup-To:."
2594   (when gnus-use-idna
2595     (save-restriction
2596       (let ((inhibit-point-motion-hooks t)
2597             (inhibit-read-only t))
2598         (article-narrow-to-head)
2599         (goto-char (point-min))
2600         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2601           (let (ace unicode)
2602             (when (save-match-data
2603                     (and (setq ace (match-string 1))
2604                          (save-excursion
2605                            (and (re-search-backward "^[^ \t]" nil t)
2606                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2607                          (setq unicode (idna-to-unicode ace))))
2608               (unless (string= ace unicode)
2609                 (replace-match unicode nil nil nil 1)))))))))
2610
2611 (defun article-de-quoted-unreadable (&optional force read-charset)
2612   "Translate a quoted-printable-encoded article.
2613 If FORCE, decode the article whether it is marked as quoted-printable
2614 or not.
2615 If READ-CHARSET, ask for a coding system."
2616   (interactive (list 'force current-prefix-arg))
2617   (save-excursion
2618     (let ((inhibit-read-only t) type charset)
2619       (if (gnus-buffer-live-p gnus-original-article-buffer)
2620           (with-current-buffer gnus-original-article-buffer
2621             (setq type
2622                   (gnus-fetch-field "content-transfer-encoding"))
2623             (let* ((ct (gnus-fetch-field "content-type"))
2624                    (ctl (and ct (mail-header-parse-content-type ct))))
2625               (setq charset (and ctl
2626                                  (mail-content-type-get ctl 'charset)))
2627               (if (stringp charset)
2628                   (setq charset (intern (downcase charset)))))))
2629       (if read-charset
2630           (setq charset (mm-read-coding-system "Charset: " charset)))
2631       (unless charset
2632         (setq charset gnus-newsgroup-charset))
2633       (when (or force
2634                 (and type (let ((case-fold-search t))
2635                             (string-match "quoted-printable" type))))
2636         (article-goto-body)
2637         (quoted-printable-decode-region
2638          (point) (point-max) (mm-charset-to-coding-system charset))))))
2639
2640 (defun article-de-base64-unreadable (&optional force read-charset)
2641   "Translate a base64 article.
2642 If FORCE, decode the article whether it is marked as base64 not.
2643 If READ-CHARSET, ask for a coding system."
2644   (interactive (list 'force current-prefix-arg))
2645   (save-excursion
2646     (let ((inhibit-read-only t) type charset)
2647       (if (gnus-buffer-live-p gnus-original-article-buffer)
2648           (with-current-buffer gnus-original-article-buffer
2649             (setq type
2650                   (gnus-fetch-field "content-transfer-encoding"))
2651             (let* ((ct (gnus-fetch-field "content-type"))
2652                    (ctl (and ct (mail-header-parse-content-type ct))))
2653               (setq charset (and ctl
2654                                  (mail-content-type-get ctl 'charset)))
2655               (if (stringp charset)
2656                   (setq charset (intern (downcase charset)))))))
2657       (if read-charset
2658           (setq charset (mm-read-coding-system "Charset: " charset)))
2659       (unless charset
2660         (setq charset gnus-newsgroup-charset))
2661       (when (or force
2662                 (and type (let ((case-fold-search t))
2663                             (string-match "base64" type))))
2664         (article-goto-body)
2665         (save-restriction
2666           (narrow-to-region (point) (point-max))
2667           (base64-decode-region (point-min) (point-max))
2668           (mm-decode-coding-region
2669            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2670
2671 (eval-when-compile
2672   (require 'rfc1843))
2673
2674 (defun article-decode-HZ ()
2675   "Translate a HZ-encoded article."
2676   (interactive)
2677   (require 'rfc1843)
2678   (save-excursion
2679     (let ((inhibit-read-only t))
2680       (rfc1843-decode-region (point-min) (point-max)))))
2681
2682 (defun article-unsplit-urls ()
2683   "Remove the newlines that some other mailers insert into URLs."
2684   (interactive)
2685   (save-excursion
2686     (let ((inhibit-read-only t))
2687       (goto-char (point-min))
2688       (while (re-search-forward
2689               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2690         (replace-match "\\1\\3" t)))
2691     (when (interactive-p)
2692       (gnus-treat-article nil))))
2693
2694
2695 (defun article-wash-html (&optional read-charset)
2696   "Format an HTML article.
2697 If READ-CHARSET, ask for a coding system.  If it is a number, the
2698 charset defined in `gnus-summary-show-article-charset-alist' is used."
2699   (interactive "P")
2700   (save-excursion
2701     (let ((inhibit-read-only t)
2702           charset)
2703       (if read-charset
2704           (if (or (and (numberp read-charset)
2705                        (setq charset
2706                              (cdr
2707                               (assq read-charset
2708                                     gnus-summary-show-article-charset-alist))))
2709                   (setq charset (mm-read-coding-system "Charset: ")))
2710               (let ((gnus-summary-show-article-charset-alist
2711                      (list (cons 1 charset))))
2712                 (with-current-buffer gnus-summary-buffer
2713                   (gnus-summary-show-article 1)))
2714             (error "No charset is given"))
2715         (when (gnus-buffer-live-p gnus-original-article-buffer)
2716           (with-current-buffer gnus-original-article-buffer
2717             (let* ((ct (gnus-fetch-field "content-type"))
2718                    (ctl (and ct (mail-header-parse-content-type ct))))
2719               (setq charset (and ctl
2720                                  (mail-content-type-get ctl 'charset)))
2721               (when (stringp charset)
2722                 (setq charset (intern (downcase charset)))))))
2723         (unless charset
2724           (setq charset gnus-newsgroup-charset)))
2725       (article-goto-body)
2726       (save-window-excursion
2727         (save-restriction
2728           (narrow-to-region (point) (point-max))
2729           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2730                  (entry (assq func mm-text-html-washer-alist)))
2731             (when entry
2732               (setq func (cdr entry)))
2733             (cond
2734              ((functionp func)
2735               (funcall func))
2736              (t
2737               (apply (car func) (cdr func))))))))))
2738
2739 ;; External.
2740 (declare-function w3-region "ext:w3-display" (st nd))
2741
2742 (defun gnus-article-wash-html-with-w3 ()
2743   "Wash the current buffer with w3."
2744   (mm-setup-w3)
2745   (let ((w3-strict-width (window-width))
2746         (url-standalone-mode t)
2747         (url-gateway-unplugged t)
2748         (w3-honor-stylesheets nil))
2749     (condition-case ()
2750         (w3-region (point-min) (point-max))
2751       (error))))
2752
2753 ;; External.
2754 (declare-function w3m-region "ext:w3m" (start end &optional url charset))
2755
2756 (defun gnus-article-wash-html-with-w3m ()
2757   "Wash the current buffer with emacs-w3m."
2758   (mm-setup-w3m)
2759   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2760         w3m-force-redisplay)
2761     (w3m-region (point-min) (point-max)))
2762   ;; Put the mark meaning this part was rendered by emacs-w3m.
2763   (put-text-property (point-min) (point-max) 'mm-inline-text-html-with-w3m t)
2764   (when (and mm-inline-text-html-with-w3m-keymap
2765              (boundp 'w3m-minor-mode-map)
2766              w3m-minor-mode-map)
2767     (if (and (boundp 'w3m-link-map)
2768              w3m-link-map)
2769         (let* ((start (point-min))
2770                (end (point-max))
2771                (on (get-text-property start 'w3m-href-anchor))
2772                (map (copy-keymap w3m-link-map))
2773                next)
2774           (set-keymap-parent map w3m-minor-mode-map)
2775           (while (< start end)
2776             (if on
2777                 (progn
2778                   (setq next (or (text-property-any start end
2779                                                     'w3m-href-anchor nil)
2780                                  end))
2781                   (put-text-property start next 'keymap map))
2782               (setq next (or (text-property-not-all start end
2783                                                     'w3m-href-anchor nil)
2784                              end))
2785               (put-text-property start next 'keymap w3m-minor-mode-map))
2786             (setq start next
2787                   on (not on))))
2788       (put-text-property (point-min) (point-max) 'keymap w3m-minor-mode-map))))
2789
2790 (defvar charset) ;; Bound by `article-wash-html'.
2791
2792 (defun gnus-article-wash-html-with-w3m-standalone ()
2793   "Wash the current buffer with w3m."
2794   (if (mm-w3m-standalone-supports-m17n-p)
2795       (progn
2796         (unless (mm-coding-system-p charset) ;; Bound by `article-wash-html'.
2797           ;; The default.
2798           (setq charset 'iso-8859-1))
2799         (let ((coding-system-for-write charset)
2800               (coding-system-for-read charset))
2801           (call-process-region
2802            (point-min) (point-max)
2803            "w3m" t t nil "-dump" "-T" "text/html"
2804            "-I" (symbol-name charset) "-O" (symbol-name charset))))
2805     (mm-inline-wash-with-stdin nil "w3m" "-dump" "-T" "text/html")))
2806
2807 (defvar gnus-article-browse-html-temp-list nil
2808   "List of temporary files created by `gnus-article-browse-html-parts'.
2809 Internal variable.")
2810
2811 (defcustom gnus-article-browse-delete-temp 'ask
2812   "What to do with temporary files from `gnus-article-browse-html-parts'.
2813 If nil, don't delete temporary files.  If it is t, delete them on
2814 exit from the summary buffer.  If it is the symbol `file', query
2815 on each file, if it is `ask' ask once when exiting from the
2816 summary buffer."
2817   :group 'gnus-article
2818   :version "23.1" ;; No Gnus
2819   :type '(choice (const :tag "Don't delete" nil)
2820                  (const :tag "Don't ask" t)
2821                  (const :tag "Ask" ask)
2822                  (const :tag "Ask for each file" file)))
2823
2824 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2825
2826 (defun gnus-article-browse-delete-temp-files (&optional how)
2827   "Delete temp-files created by `gnus-article-browse-html-parts'."
2828   (when (and gnus-article-browse-html-temp-list
2829              (progn
2830                (or how (setq how gnus-article-browse-delete-temp))
2831                (if (eq how 'ask)
2832                    (let ((files (length gnus-article-browse-html-temp-list)))
2833                      (gnus-y-or-n-p (format
2834                                      "Delete all %s temporary HTML file%s? "
2835                                      files
2836                                      (if (> files 1) "s" ""))))
2837                  how)))
2838     (dolist (file gnus-article-browse-html-temp-list)
2839       (cond ((file-directory-p file)
2840              (when (or (not (eq how 'file))
2841                        (gnus-y-or-n-p
2842                         (format
2843                          "Delete temporary HTML file(s) in directory `%s'? "
2844                          (file-name-as-directory file))))
2845                (gnus-delete-directory file)))
2846             ((file-exists-p file)
2847              (when (or (not (eq how 'file))
2848                        (gnus-y-or-n-p
2849                         (format "Delete temporary HTML file `%s'? " file)))
2850                (delete-file file)))))
2851     ;; Also remove file from the list when not deleted or if file doesn't
2852     ;; exist anymore.
2853     (setq gnus-article-browse-html-temp-list nil))
2854   gnus-article-browse-html-temp-list)
2855
2856 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2857   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2858 Return file name."
2859   (save-match-data
2860     (let (file type)
2861       (catch 'found
2862         (dolist (handle handles)
2863           (cond
2864            ((not (listp handle)))
2865            ((equal (mm-handle-media-supertype handle) "multipart")
2866             (when (setq file (gnus-article-browse-html-save-cid-content
2867                               cid handle directory))
2868               (throw 'found file)))
2869            ((equal (concat "<" cid ">") (mm-handle-id handle))
2870             (setq file
2871                   (expand-file-name
2872                    (or (mail-content-type-get
2873                         (mm-handle-disposition handle) 'filename)
2874                        (mail-content-type-get
2875                         (setq type (mm-handle-type handle)) 'name)
2876                        (concat
2877                         (make-temp-name "cid")
2878                         (car (rassoc (car type) mailcap-mime-extensions))))
2879                    directory))
2880             (mm-save-part-to-file handle file)
2881             (throw 'found file))))))))
2882
2883 (defun gnus-article-browse-html-parts (list &optional header)
2884   "View all \"text/html\" parts from LIST.
2885 Recurse into multiparts.  The optional HEADER that should be a decoded
2886 message header will be added to the bodies of the \"text/html\" parts."
2887   ;; Internal function used by `gnus-article-browse-html-article'.
2888   (let (type file charset content cid-dir tmp-file showed)
2889     ;; Find and show the html-parts.
2890     (dolist (handle list)
2891       ;; If HTML, show it:
2892       (cond ((not (listp handle)))
2893             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2894                  (and (equal (car type) "message/external-body")
2895                       (or header
2896                           (setq file (or (mail-content-type-get type 'name)
2897                                          (mail-content-type-get
2898                                           (mm-handle-disposition handle)
2899                                           'filename))))
2900                       (or (mm-handle-cache handle)
2901                           (condition-case code
2902                               (progn (mm-extern-cache-contents handle) t)
2903                             (error
2904                              (gnus-message 3 "%s" (error-message-string code))
2905                              (when (>= gnus-verbose 3) (sit-for 2))
2906                              nil)))
2907                       (progn
2908                         (setq handle (mm-handle-cache handle)
2909                               type (mm-handle-type handle))
2910                         (equal (car type) "text/html"))))
2911              (setq charset (mail-content-type-get type 'charset)
2912                    content (mm-get-part handle))
2913              (with-temp-buffer
2914                (if (eq charset 'gnus-decoded)
2915                    (mm-enable-multibyte)
2916                  (mm-disable-multibyte))
2917                (insert content)
2918                ;; resolve cid contents
2919                (let ((case-fold-search t)
2920                      cid-file)
2921                  (goto-char (point-min))
2922                  (while (re-search-forward "\
2923 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2924                                            nil t)
2925                    (unless cid-dir
2926                      (setq cid-dir (mm-make-temp-file "cid" t))
2927                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2928                    (setq file nil
2929                          content nil)
2930                    (when (setq cid-file
2931                                (gnus-article-browse-html-save-cid-content
2932                                 (match-string 2)
2933                                 (with-current-buffer gnus-article-buffer
2934                                   gnus-article-mime-handles)
2935                                 cid-dir))
2936                      (replace-match (concat "file://" cid-file)
2937                                     nil nil nil 1))))
2938                (unless content (setq content (buffer-string))))
2939              (when (or charset header (not file))
2940                (setq tmp-file (mm-make-temp-file
2941                                ;; Do we need to care for 8.3 filenames?
2942                                "mm-" nil ".html")))
2943              ;; Add a meta html tag to specify charset and a header.
2944              (cond
2945               (header
2946                (let (title eheader body hcharset coding force-charset)
2947                  (with-temp-buffer
2948                    (mm-enable-multibyte)
2949                    (setq case-fold-search t)
2950                    (insert header "\n")
2951                    (setq title (message-fetch-field "subject"))
2952                    (goto-char (point-min))
2953                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2954                      (replace-match (cond ((match-beginning 1) "&lt;")
2955                                           ((match-beginning 2) "&gt;")
2956                                           (t "&amp;"))))
2957                    (goto-char (point-min))
2958                    (insert "<pre>\n")
2959                    (goto-char (point-max))
2960                    (insert "</pre>\n<hr>\n")
2961                    ;; We have to examine charset one by one since
2962                    ;; charset specified in parts might be different.
2963                    (if (eq charset 'gnus-decoded)
2964                        (setq charset 'utf-8
2965                              eheader (mm-encode-coding-string (buffer-string)
2966                                                               charset)
2967                              title (when title
2968                                      (mm-encode-coding-string title charset))
2969                              body (mm-encode-coding-string content charset)
2970                              force-charset t)
2971                      (setq hcharset (mm-find-mime-charset-region (point-min)
2972                                                                  (point-max)))
2973                      (cond ((= (length hcharset) 1)
2974                             (setq hcharset (car hcharset)
2975                                   coding (mm-charset-to-coding-system
2976                                           hcharset)))
2977                            ((> (length hcharset) 1)
2978                             (setq hcharset 'utf-8
2979                                   coding hcharset)))
2980                      (if coding
2981                          (if charset
2982                              (progn
2983                                (setq body
2984                                      (mm-charset-to-coding-system charset))
2985                                (if (eq coding body)
2986                                    (setq eheader (mm-encode-coding-string
2987                                                   (buffer-string) coding)
2988                                          title (when title
2989                                                  (mm-encode-coding-string
2990                                                   title coding))
2991                                          body content)
2992                                  (setq charset 'utf-8
2993                                        eheader (mm-encode-coding-string
2994                                                 (buffer-string) charset)
2995                                        title (when title
2996                                                (mm-encode-coding-string
2997                                                 title charset))
2998                                        body (mm-encode-coding-string
2999                                              (mm-decode-coding-string
3000                                               content body)
3001                                              charset)
3002                                        force-charset t)))
3003                            (setq charset hcharset
3004                                  eheader (mm-encode-coding-string
3005                                           (buffer-string) coding)
3006                                  title (when title
3007                                          (mm-encode-coding-string
3008                                           title coding))
3009                                  body content))
3010                        (setq eheader (mm-string-as-unibyte (buffer-string))
3011                              body content)))
3012                    (erase-buffer)
3013                    (mm-disable-multibyte)
3014                    (insert body)
3015                    (when charset
3016                      (mm-add-meta-html-tag handle charset force-charset))
3017                    (when title
3018                      (goto-char (point-min))
3019                      (unless (search-forward "<title>" nil t)
3020                        (re-search-forward "<head>\\s-*" nil t)
3021                        (insert "<title>" title "</title>\n")))
3022                    (goto-char (point-min))
3023                    (or (re-search-forward
3024                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
3025                        (re-search-forward
3026                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
3027                    (insert eheader)
3028                    (mm-write-region (point-min) (point-max)
3029                                     tmp-file nil nil nil 'binary t))))
3030               (charset
3031                (mm-with-unibyte-buffer
3032                  (insert (if (eq charset 'gnus-decoded)
3033                              (mm-encode-coding-string content
3034                                                       (setq charset 'utf-8))
3035                            content))
3036                  (if (or (mm-add-meta-html-tag handle charset)
3037                          (not file))
3038                      (mm-write-region (point-min) (point-max)
3039                                       tmp-file nil nil nil 'binary t)
3040                    (setq tmp-file nil))))
3041               (tmp-file
3042                (mm-save-part-to-file handle tmp-file)))
3043              (when tmp-file
3044                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
3045              (add-hook 'gnus-summary-prepare-exit-hook
3046                        'gnus-article-browse-delete-temp-files)
3047              (add-hook 'gnus-exit-gnus-hook
3048                        (lambda  ()
3049                          (gnus-article-browse-delete-temp-files t)))
3050              ;; FIXME: Warn if there's an <img> tag?
3051              (browse-url-of-file (or tmp-file (expand-file-name file)))
3052              (setq showed t))
3053             ;; If multipart, recurse
3054             ((equal (mm-handle-media-supertype handle) "multipart")
3055              (when (gnus-article-browse-html-parts handle header)
3056                (setq showed t)))
3057             ((equal (mm-handle-media-type handle) "message/rfc822")
3058              (mm-with-multibyte-buffer
3059                (mm-insert-part handle)
3060                (setq handle (mm-dissect-buffer t t))
3061                (when (and (bufferp (car handle))
3062                           (stringp (car (mm-handle-type handle))))
3063                  (setq handle (list handle)))
3064                (when header
3065                  (article-decode-encoded-words)
3066                  (let ((gnus-visible-headers
3067                         (or (get 'gnus-visible-headers 'standard-value)
3068                             gnus-visible-headers)))
3069                    (article-hide-headers))
3070                  (goto-char (point-min))
3071                  (search-forward "\n\n" nil 'move)
3072                  (skip-chars-backward "\t\n ")
3073                  (setq header (buffer-substring (point-min) (point)))))
3074              (when (prog1
3075                        (gnus-article-browse-html-parts handle header)
3076                      (mm-destroy-parts handle))
3077                (setq showed t)))))
3078     showed))
3079
3080 (defun gnus-article-browse-html-article (&optional arg)
3081   "View \"text/html\" parts of the current article with a WWW browser.
3082 Inline images embedded in a message using the cid scheme, as they are
3083 generally considered to be safe, will be processed properly.
3084 The message header is added to the beginning of every html part unless
3085 the prefix argument ARG is given.
3086
3087 Warning: Spammers use links to images (using the http scheme) in HTML
3088 articles to verify whether you have read the message.  As
3089 `gnus-article-browse-html-article' passes the HTML content to the
3090 browser without eliminating these \"web bugs\" you should only
3091 use it for mails from trusted senders.
3092
3093 If you always want to display HTML parts in the browser, set
3094 `mm-text-html-renderer' to nil.
3095
3096 This command creates temporary files to pass HTML contents including
3097 images if any to the browser, and deletes them when exiting the group
3098 \(if you want)."
3099   ;; Cf. `mm-w3m-safe-url-regexp'
3100   (interactive "P")
3101   (if arg
3102       (gnus-summary-show-article)
3103     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3104                                     gnus-visible-headers))
3105           ;; As we insert a <hr>, there's no need for the body boundary.
3106           (gnus-treat-body-boundary nil))
3107       (gnus-summary-show-article)))
3108   (with-current-buffer gnus-article-buffer
3109     (let ((header (unless arg
3110                     (save-restriction
3111                       (widen)
3112                       (buffer-substring-no-properties
3113                        (goto-char (point-min))
3114                        (if (search-forward "\n\n" nil t)
3115                            (match-beginning 0)
3116                          (goto-char (point-max))
3117                          (skip-chars-backward "\t\n ")
3118                          (point))))))
3119           parts)
3120       (set-buffer gnus-original-article-buffer)
3121       (setq parts (mm-dissect-buffer t t))
3122       ;; If singlepart, enforce a list.
3123       (when (and (bufferp (car parts))
3124                  (stringp (car (mm-handle-type parts))))
3125         (setq parts (list parts)))
3126       ;; Process the list
3127       (unless (gnus-article-browse-html-parts parts header)
3128         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3129       (mm-destroy-parts parts)
3130       (unless arg
3131         (gnus-summary-show-article)))))
3132
3133 (defun article-hide-list-identifiers ()
3134   "Remove list identifies from the Subject header.
3135 The `gnus-list-identifiers' variable specifies what to do."
3136   (interactive)
3137   (let ((inhibit-point-motion-hooks t)
3138         (regexp (if (consp gnus-list-identifiers)
3139                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3140                   gnus-list-identifiers))
3141         (inhibit-read-only t))
3142     (when regexp
3143       (save-excursion
3144         (save-restriction
3145           (article-narrow-to-head)
3146           (goto-char (point-min))
3147           (while (re-search-forward
3148                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3149                   nil t)
3150             (delete-region (match-beginning 2) (match-end 0))
3151             (beginning-of-line))
3152           (when (re-search-forward
3153                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3154             (delete-region (match-beginning 1) (match-end 1))))))))
3155
3156 (defun article-hide-pem (&optional arg)
3157   "Toggle hiding of any PEM headers and signatures in the current article.
3158 If given a negative prefix, always show; if given a positive prefix,
3159 always hide."
3160   (interactive (gnus-article-hidden-arg))
3161   (unless (gnus-article-check-hidden-text 'pem arg)
3162     (save-excursion
3163       (let ((inhibit-read-only t) end)
3164         (goto-char (point-min))
3165         ;; Hide the horrendously ugly "header".
3166         (when (and (search-forward
3167                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3168                     nil t)
3169                    (setq end (1+ (match-beginning 0))))
3170           (gnus-add-wash-type 'pem)
3171           (gnus-article-hide-text-type
3172            end
3173            (if (search-forward "\n\n" nil t)
3174                (match-end 0)
3175              (point-max))
3176            'pem)
3177           ;; Hide the trailer as well
3178           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3179                                 nil t)
3180             (gnus-article-hide-text-type
3181              (match-beginning 0) (match-end 0) 'pem)))))))
3182
3183 (defun article-strip-banner ()
3184   "Strip the banners specified by the `banner' group parameter and by
3185 `gnus-article-address-banner-alist'."
3186   (interactive)
3187   (save-excursion
3188     (save-restriction
3189       (let ((inhibit-point-motion-hooks t))
3190         (when (gnus-parameter-banner gnus-newsgroup-name)
3191           (article-really-strip-banner
3192            (gnus-parameter-banner gnus-newsgroup-name)))
3193         (when gnus-article-address-banner-alist
3194           ;; Note that the From header is decoded here, so it is
3195           ;; required that the *-extract-address-components function
3196           ;; supports non-ASCII text.
3197           (let ((from (save-restriction
3198                         (widen)
3199                         (article-narrow-to-head)
3200                         (mail-fetch-field "from"))))
3201             (when (and from
3202                        (setq from
3203                              (cadr (funcall gnus-extract-address-components
3204                                             from))))
3205               (catch 'found
3206                 (dolist (pair gnus-article-address-banner-alist)
3207                   (when (string-match (car pair) from)
3208                     (throw 'found
3209                            (article-really-strip-banner (cdr pair)))))))))))))
3210
3211 (defun article-really-strip-banner (banner)
3212   "Strip the banner specified by the argument."
3213   (save-excursion
3214     (save-restriction
3215       (let ((inhibit-point-motion-hooks t)
3216             (gnus-signature-limit nil)
3217             (inhibit-read-only t))
3218         (article-goto-body)
3219         (cond
3220          ((eq banner 'signature)
3221           (when (gnus-article-narrow-to-signature)
3222             (widen)
3223             (forward-line -1)
3224             (delete-region (point) (point-max))))
3225          ((symbolp banner)
3226           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3227               (while (re-search-forward banner nil t)
3228                 (delete-region (match-beginning 0) (match-end 0)))))
3229          ((stringp banner)
3230           (while (re-search-forward banner nil t)
3231             (delete-region (match-beginning 0) (match-end 0)))))))))
3232
3233 (defun article-babel ()
3234   "Translate article using an online translation service."
3235   (interactive)
3236   (require 'babel)
3237   (gnus-with-article-buffer
3238     (when (article-goto-body)
3239       (let* ((start (point))
3240              (end (point-max))
3241              (orig (buffer-substring start end))
3242              (trans (babel-as-string orig)))
3243         (save-restriction
3244           (narrow-to-region start end)
3245           (delete-region start end)
3246           (insert trans))))))
3247
3248 (defun article-hide-signature (&optional arg)
3249   "Hide the signature in the current article.
3250 If given a negative prefix, always show; if given a positive prefix,
3251 always hide."
3252   (interactive (gnus-article-hidden-arg))
3253   (unless (gnus-article-check-hidden-text 'signature arg)
3254     (save-excursion
3255       (save-restriction
3256         (let ((inhibit-read-only t))
3257           (when (gnus-article-narrow-to-signature)
3258             (gnus-article-hide-text-type
3259              (point-min) (point-max) 'signature))))))
3260   (gnus-set-mode-line 'article))
3261
3262 (defun article-strip-headers-in-body ()
3263   "Strip offensive headers from bodies."
3264   (interactive)
3265   (save-excursion
3266     (article-goto-body)
3267     (let ((case-fold-search t))
3268       (when (looking-at "x-no-archive:")
3269         (gnus-delete-line)))))
3270
3271 (defun article-strip-leading-blank-lines ()
3272   "Remove all blank lines from the beginning of the article."
3273   (interactive)
3274   (save-excursion
3275     (let ((inhibit-point-motion-hooks t)
3276           (inhibit-read-only t))
3277       (when (article-goto-body)
3278         (while (and (not (eobp))
3279                     (looking-at "[ \t]*$"))
3280           (gnus-delete-line))))))
3281
3282 (defun article-narrow-to-head ()
3283   "Narrow the buffer to the head of the message.
3284 Point is left at the beginning of the narrowed-to region."
3285   (narrow-to-region
3286    (goto-char (point-min))
3287    (if (search-forward "\n\n" nil 1)
3288        (1- (point))
3289      (point-max)))
3290   (goto-char (point-min)))
3291
3292 (defun article-goto-body ()
3293   "Place point at the start of the body."
3294   (goto-char (point-min))
3295   (cond
3296    ;; This variable is only bound when dealing with separate
3297    ;; MIME body parts.
3298    (article-goto-body-goes-to-point-min-p
3299     t)
3300    ((search-forward "\n\n" nil t)
3301     t)
3302    (t
3303     (goto-char (point-max))
3304     nil)))
3305
3306 (defun article-strip-multiple-blank-lines ()
3307   "Replace consecutive blank lines with one empty line."
3308   (interactive)
3309   (save-excursion
3310     (let ((inhibit-point-motion-hooks t)
3311           (inhibit-read-only t))
3312       ;; First make all blank lines empty.
3313       (article-goto-body)
3314       (while (re-search-forward "^[ \t]+$" nil t)
3315         (unless (gnus-annotation-in-region-p
3316                  (match-beginning 0) (match-end 0))
3317           (replace-match "" nil t)))
3318       ;; Then replace multiple empty lines with a single empty line.
3319       (article-goto-body)
3320       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3321         (unless (gnus-annotation-in-region-p
3322                  (match-beginning 0) (match-end 0))
3323           (delete-region (match-beginning 1) (match-end 1)))))))
3324
3325 (defun article-strip-leading-space ()
3326   "Remove all white space from the beginning of the lines in the article."
3327   (interactive)
3328   (save-excursion
3329     (let ((inhibit-point-motion-hooks t)
3330           (inhibit-read-only t))
3331       (article-goto-body)
3332       (while (re-search-forward "^[ \t]+" nil t)
3333         (replace-match "" t t)))))
3334
3335 (defun article-strip-trailing-space ()
3336   "Remove all white space from the end of the lines in the article."
3337   (interactive)
3338   (save-excursion
3339     (let ((inhibit-point-motion-hooks t)
3340           (inhibit-read-only t))
3341       (article-goto-body)
3342       (while (re-search-forward "[ \t]+$" nil t)
3343         (replace-match "" t t)))))
3344
3345 (defun article-strip-blank-lines ()
3346   "Strip leading, trailing and multiple blank lines."
3347   (interactive)
3348   (article-strip-leading-blank-lines)
3349   (article-remove-trailing-blank-lines)
3350   (article-strip-multiple-blank-lines))
3351
3352 (defun article-strip-all-blank-lines ()
3353   "Strip all blank lines."
3354   (interactive)
3355   (save-excursion
3356     (let ((inhibit-point-motion-hooks t)
3357           (inhibit-read-only t))
3358       (article-goto-body)
3359       (while (re-search-forward "^[ \t]*\n" nil t)
3360         (replace-match "" t t)))))
3361
3362 (defun gnus-article-narrow-to-signature ()
3363   "Narrow to the signature; return t if a signature is found, else nil."
3364   (let ((inhibit-point-motion-hooks t))
3365     (when (gnus-article-search-signature)
3366       (forward-line 1)
3367       ;; Check whether we have some limits to what we consider
3368       ;; to be a signature.
3369       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3370                       (list gnus-signature-limit)))
3371             limit limited)
3372         (while (setq limit (pop limits))
3373           (if (or (and (integerp limit)
3374                        (< (- (point-max) (point)) limit))
3375                   (and (floatp limit)
3376                        (< (count-lines (point) (point-max)) limit))
3377                   (and (functionp limit)
3378                        (funcall limit))
3379                   (and (stringp limit)
3380                        (not (re-search-forward limit nil t))))
3381               ()                        ; This limit did not succeed.
3382             (setq limited t
3383                   limits nil)))
3384         (unless limited
3385           (narrow-to-region (point) (point-max))
3386           t)))))
3387
3388 (defun gnus-article-search-signature ()
3389   "Search the current buffer for the signature separator.
3390 Put point at the beginning of the signature separator."
3391   (let ((cur (point)))
3392     (goto-char (point-max))
3393     (if (if (stringp gnus-signature-separator)
3394             (re-search-backward gnus-signature-separator nil t)
3395           (let ((seps gnus-signature-separator))
3396             (while (and seps
3397                         (not (re-search-backward (car seps) nil t)))
3398               (pop seps))
3399             seps))
3400         t
3401       (goto-char cur)
3402       nil)))
3403
3404 (defun gnus-article-hidden-arg ()
3405   "Return the current prefix arg as a number, or 0 if no prefix."
3406   (list (if current-prefix-arg
3407             (prefix-numeric-value current-prefix-arg)
3408           0)))
3409
3410 (defun gnus-article-check-hidden-text (type arg)
3411   "Return nil if hiding is necessary.
3412 Arg can be nil or a number.  nil and positive means hide, negative
3413 means show, 0 means toggle."
3414   (save-excursion
3415     (save-restriction
3416       (let ((hide (gnus-article-hidden-text-p type)))
3417         (cond
3418          ((or (null arg)
3419               (> arg 0))
3420           nil)
3421          ((< arg 0)
3422           (gnus-article-show-hidden-text type)
3423           t)
3424          (t
3425           (if (eq hide 'hidden)
3426               (progn
3427                 (gnus-article-show-hidden-text type)
3428                 t)
3429             nil)))))))
3430
3431 (defun gnus-article-hidden-text-p (type)
3432   "Say whether the current buffer contains hidden text of type TYPE."
3433   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3434     (while (and pos
3435                 (not (get-text-property pos 'invisible))
3436                 (not (get-text-property pos 'dummy-invisible)))
3437       (setq pos
3438             (text-property-any (1+ pos) (point-max) 'article-type type)))
3439     (if pos
3440         'hidden
3441       nil)))
3442
3443 (defun gnus-article-show-hidden-text (type &optional dummy)
3444   "Show all hidden text of type TYPE.
3445 Originally it is hide instead of DUMMY."
3446   (let ((inhibit-read-only t)
3447         (inhibit-point-motion-hooks t))
3448     (gnus-remove-text-properties-when
3449      'article-type type
3450      (point-min) (point-max)
3451      (cons 'article-type (cons type
3452                                gnus-hidden-properties)))
3453     (gnus-delete-wash-type type)))
3454
3455 (defconst article-time-units
3456   `((year . ,(* 365.25 24 60 60))
3457     (week . ,(* 7 24 60 60))
3458     (day . ,(* 24 60 60))
3459     (hour . ,(* 60 60))
3460     (minute . 60)
3461     (second . 1))
3462   "Mapping from time units to seconds.")
3463
3464 (defun gnus-article-forward-header ()
3465   "Move point to the start of the next header.
3466 If the current header is a continuation header, this can be several
3467 lines forward."
3468   (let ((ended nil))
3469     (while (not ended)
3470       (forward-line 1)
3471       (if (looking-at "[ \t]+[^ \t]")
3472           (forward-line 1)
3473         (setq ended t)))))
3474
3475 (defun article-date-ut (&optional type highlight)
3476   "Convert DATE date to universal time in the current article.
3477 If TYPE is `local', convert to local time; if it is `lapsed', output
3478 how much time has lapsed since DATE.  For `lapsed', the value of
3479 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3480 should replace the \"Date:\" one, or should be added below it."
3481   (interactive (list 'ut t))
3482   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3483          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3484                              tdate-regexp)
3485                             ((eq type 'lapsed)
3486                              "^X-Sent:[ \t]")
3487                             (article-lapsed-timer
3488                              "^Date:[ \t]")
3489                             (t
3490                              tdate-regexp)))
3491          (case-fold-search t)
3492          (inhibit-read-only t)
3493          (inhibit-point-motion-hooks t)
3494          pos date bface eface)
3495     (save-excursion
3496       (save-restriction
3497         (widen)
3498         (goto-char (point-min))
3499         (while (or (setq date (get-text-property (setq pos (point))
3500                                                  'original-date))
3501                    (when (setq pos (next-single-property-change
3502                                     (point) 'original-date))
3503                      (setq date (get-text-property pos 'original-date))
3504                      t))
3505           (narrow-to-region
3506            pos (if (setq pos (text-property-any pos (point-max)
3507                                                 'original-date nil))
3508                    (progn
3509                      (goto-char pos)
3510                      (if (or (bolp) (eobp))
3511                          (point)
3512                        (1+ (point))))
3513                  (point-max)))
3514           (goto-char (point-min))
3515           (when (re-search-forward tdate-regexp nil t)
3516             (setq bface (get-text-property (point-at-bol) 'face)
3517                   eface (get-text-property (1- (point-at-eol)) 'face)))
3518           (goto-char (point-min))
3519           (setq pos nil)
3520           ;; Delete any old Date headers.
3521           (while (re-search-forward date-regexp nil t)
3522             (if pos
3523                 (delete-region (point-at-bol) (progn
3524                                                 (gnus-article-forward-header)
3525                                                 (point)))
3526               (delete-region (point-at-bol) (progn
3527                                               (gnus-article-forward-header)
3528                                               (forward-char -1)
3529                                               (point)))
3530               (setq pos (point))))
3531           (when (and (not pos)
3532                      (re-search-forward tdate-regexp nil t))
3533             (forward-line 1))
3534           (gnus-goto-char pos)
3535           (insert (article-make-date-line date (or type 'ut)))
3536           (unless pos
3537             (insert "\n")
3538             (forward-line -1))
3539           ;; Do highlighting.
3540           (beginning-of-line)
3541           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3542             (put-text-property (match-beginning 1) (1+ (match-end 1))
3543                                'face bface)
3544             (put-text-property (match-beginning 2) (match-end 2)
3545                                'face eface))
3546           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3547           (goto-char (point-max))
3548           (widen))))))
3549
3550 (defun article-make-date-line (date type)
3551   "Return a DATE line of TYPE."
3552   (unless (memq type '(local ut original user iso8601 lapsed english))
3553     (error "Unknown conversion type: %s" type))
3554   (condition-case ()
3555       (let ((time (date-to-time date)))
3556         (cond
3557          ;; Convert to the local timezone.
3558          ((eq type 'local)
3559           (concat "Date: " (message-make-date time)))
3560          ;; Convert to Universal Time.
3561          ((eq type 'ut)
3562           (concat "Date: "
3563                   (substring
3564                    (message-make-date
3565                     (let* ((e (parse-time-string date))
3566                            (tm (apply 'encode-time e))
3567                            (ms (car tm))
3568                            (ls (- (cadr tm) (car (current-time-zone time)))))
3569                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3570                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3571                             (t (list ms ls)))))
3572                    0 -5)
3573                   "UT"))
3574          ;; Get the original date from the article.
3575          ((eq type 'original)
3576           (concat "Date: " (if (string-match "\n+$" date)
3577                                (substring date 0 (match-beginning 0))
3578                              date)))
3579          ;; Let the user define the format.
3580          ((eq type 'user)
3581           (let ((format (or (condition-case nil
3582                                 (with-current-buffer gnus-summary-buffer
3583                                   gnus-article-time-format)
3584                               (error nil))
3585                             gnus-article-time-format)))
3586             (if (functionp format)
3587                 (funcall format time)
3588               (concat "Date: " (format-time-string format time)))))
3589          ;; ISO 8601.
3590          ((eq type 'iso8601)
3591           (let ((tz (car (current-time-zone time))))
3592             (concat
3593              "Date: "
3594              (format-time-string "%Y%m%dT%H%M%S" time)
3595              (format "%s%02d%02d"
3596                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3597                      (/ (% (abs tz) 3600) 60)))))
3598          ;; Do an X-Sent lapsed format.
3599          ((eq type 'lapsed)
3600           ;; If the date is seriously mangled, the timezone functions are
3601           ;; liable to bug out, so we ignore all errors.
3602           (let* ((now (current-time))
3603                  (real-time (subtract-time now time))
3604                  (real-sec (and real-time
3605                                 (+ (* (float (car real-time)) 65536)
3606                                    (cadr real-time))))
3607                  (sec (and real-time (abs real-sec)))
3608                  num prev)
3609             (cond
3610              ((null real-time)
3611               "X-Sent: Unknown")
3612              ((zerop sec)
3613               "X-Sent: Now")
3614              (t
3615               (concat
3616                "X-Sent: "
3617                ;; This is a bit convoluted, but basically we go
3618                ;; through the time units for years, weeks, etc,
3619                ;; and divide things to see whether that results
3620                ;; in positive answers.
3621                (mapconcat
3622                 (lambda (unit)
3623                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3624                       ;; The (remaining) seconds are too few to
3625                       ;; be divided into this time unit.
3626                       ""
3627                     ;; It's big enough, so we output it.
3628                     (setq sec (- sec (* num (cdr unit))))
3629                     (prog1
3630                         (concat (if prev ", " "") (int-to-string
3631                                                    (floor num))
3632                                 " " (symbol-name (car unit))
3633                                 (if (> num 1) "s" ""))
3634                       (setq prev t))))
3635                 article-time-units "")
3636                ;; If dates are odd, then it might appear like the
3637                ;; article was sent in the future.
3638                (if (> real-sec 0)
3639                    " ago"
3640                  " in the future"))))))
3641          ;; Display the date in proper English
3642          ((eq type 'english)
3643           (let ((dtime (decode-time time)))
3644             (concat
3645              "Date: the "
3646              (number-to-string (nth 3 dtime))
3647              (let ((digit (% (nth 3 dtime) 10)))
3648                (cond
3649                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3650                 ((= digit 1) "st")
3651                 ((= digit 2) "nd")
3652                 ((= digit 3) "rd")
3653                 (t "th")))
3654              " of "
3655              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3656              " "
3657              (number-to-string (nth 5 dtime))
3658              " at "
3659              (format "%02d" (nth 2 dtime))
3660              ":"
3661              (format "%02d" (nth 1 dtime)))))))
3662     (error
3663      (format "Date: %s (from Gnus)" date))))
3664
3665 (defun article-date-local (&optional highlight)
3666   "Convert the current article date to the local timezone."
3667   (interactive (list t))
3668   (article-date-ut 'local highlight))
3669
3670 (defun article-date-english (&optional highlight)
3671   "Convert the current article date to something that is proper English."
3672   (interactive (list t))
3673   (article-date-ut 'english highlight))
3674
3675 (defun article-date-original (&optional highlight)
3676   "Convert the current article date to what it was originally.
3677 This is only useful if you have used some other date conversion
3678 function and want to see what the date was before converting."
3679   (interactive (list t))
3680   (article-date-ut 'original highlight))
3681
3682 (defun article-date-lapsed (&optional highlight)
3683   "Convert the current article date to time lapsed since it was sent."
3684   (interactive (list t))
3685   (article-date-ut 'lapsed highlight))
3686
3687 (defun article-update-date-lapsed ()
3688   "Function to be run from a timer to update the lapsed time line."
3689   (save-match-data
3690     (let (deactivate-mark)
3691       (save-excursion
3692         (ignore-errors
3693          (walk-windows
3694           (lambda (w)
3695             (set-buffer (window-buffer w))
3696             (when (eq major-mode 'gnus-article-mode)
3697               (let ((mark (point-marker)))
3698                 (goto-char (point-min))
3699                 (when (re-search-forward "^X-Sent:" nil t)
3700                   (article-date-lapsed t))
3701                 (goto-char (marker-position mark))
3702                 (move-marker mark nil))))
3703           nil 'visible))))))
3704
3705 (defun gnus-start-date-timer (&optional n)
3706   "Start a timer to update the X-Sent header in the article buffers.
3707 The numerical prefix says how frequently (in seconds) the function
3708 is to run."
3709   (interactive "p")
3710   (unless n
3711     (setq n 1))
3712   (gnus-stop-date-timer)
3713   (setq article-lapsed-timer
3714         (run-at-time 1 n 'article-update-date-lapsed)))
3715
3716 (defun gnus-stop-date-timer ()
3717   "Stop the X-Sent timer."
3718   (interactive)
3719   (when article-lapsed-timer
3720     (nnheader-cancel-timer article-lapsed-timer)
3721     (setq article-lapsed-timer nil)))
3722
3723 (defun article-date-user (&optional highlight)
3724   "Convert the current article date to the user-defined format.
3725 This format is defined by the `gnus-article-time-format' variable."
3726   (interactive (list t))
3727   (article-date-ut 'user highlight))
3728
3729 (defun article-date-iso8601 (&optional highlight)
3730   "Convert the current article date to ISO8601."
3731   (interactive (list t))
3732   (article-date-ut 'iso8601 highlight))
3733
3734 (defmacro gnus-article-save-original-date (&rest forms)
3735   "Save the original date as a text property and evaluate FORMS."
3736   `(let* ((case-fold-search t)
3737           (start (progn
3738                    (goto-char (point-min))
3739                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3740                               (not (bolp)))
3741                      (match-end 0))))
3742           (date (when (and start
3743                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3744                                               nil t))
3745                   (buffer-substring-no-properties start
3746                                                   (match-beginning 0)))))
3747      (goto-char (point-max))
3748      (skip-chars-backward "\n")
3749      (put-text-property (point-min) (point) 'original-date date)
3750      ,@forms
3751      (goto-char (point-max))
3752      (skip-chars-backward "\n")
3753      (put-text-property (point-min) (point) 'original-date date)))
3754
3755 ;; (defun article-show-all ()
3756 ;;   "Show all hidden text in the article buffer."
3757 ;;   (interactive)
3758 ;;   (save-excursion
3759 ;;     (let ((inhibit-read-only t))
3760 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3761
3762 (defun article-remove-leading-whitespace ()
3763   "Remove excessive whitespace from all headers."
3764   (interactive)
3765   (save-excursion
3766     (save-restriction
3767       (let ((inhibit-read-only t))
3768         (article-narrow-to-head)
3769         (goto-char (point-min))
3770         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3771           (delete-region (match-beginning 1) (match-end 1)))))))
3772
3773 (defun article-emphasize (&optional arg)
3774   "Emphasize text according to `gnus-emphasis-alist'."
3775   (interactive (gnus-article-hidden-arg))
3776   (unless (gnus-article-check-hidden-text 'emphasis arg)
3777     (save-excursion
3778       (let ((alist (or
3779                     (condition-case nil
3780                         (with-current-buffer gnus-summary-buffer
3781                           gnus-article-emphasis-alist)
3782                       (error))
3783                     gnus-emphasis-alist))
3784             (inhibit-read-only t)
3785             (props (append '(article-type emphasis)
3786                            gnus-hidden-properties))
3787             regexp elem beg invisible visible face)
3788         (article-goto-body)
3789         (setq beg (point))
3790         (while (setq elem (pop alist))
3791           (goto-char beg)
3792           (setq regexp (car elem)
3793                 invisible (nth 1 elem)
3794                 visible (nth 2 elem)
3795                 face (nth 3 elem))
3796           (while (re-search-forward regexp nil t)
3797             (when (and (match-beginning visible) (match-beginning invisible))
3798               (gnus-article-hide-text
3799                (match-beginning invisible) (match-end invisible) props)
3800               (gnus-article-unhide-text-type
3801                (match-beginning visible) (match-end visible) 'emphasis)
3802               (gnus-put-overlay-excluding-newlines
3803                (match-beginning visible) (match-end visible) 'face face)
3804               (gnus-add-wash-type 'emphasis)
3805               (goto-char (match-end invisible)))))))))
3806
3807 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3808   "Setup newsgroup emphasis alist."
3809   (unless gnus-article-emphasis-alist
3810     (let ((name (and gnus-newsgroup-name
3811                      (gnus-group-real-name gnus-newsgroup-name))))
3812       (make-local-variable 'gnus-article-emphasis-alist)
3813       (setq gnus-article-emphasis-alist
3814             (nconc
3815              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3816                (while (setq elem (pop alist))
3817                  (when (and name (string-match (car elem) name))
3818                    (setq alist nil
3819                          highlight (copy-sequence (cdr elem)))))
3820                highlight)
3821              (copy-sequence highlight-words)
3822              (if gnus-newsgroup-name
3823                  (copy-sequence (gnus-group-find-parameter
3824                                  gnus-newsgroup-name 'highlight-words t)))
3825              gnus-emphasis-alist)))))
3826
3827 (defvar gnus-summary-article-menu)
3828 (defvar gnus-summary-post-menu)
3829
3830 ;;; Saving functions.
3831
3832 (defun gnus-article-save (save-buffer file &optional num)
3833   "Save the currently selected article."
3834   (when (or (get gnus-default-article-saver :headers)
3835             (not gnus-save-all-headers))
3836     ;; Remove headers according to `gnus-saved-headers' or the value
3837     ;; of the `:headers' property that the saver function might have.
3838     (let ((gnus-visible-headers
3839            (or (symbol-value (get gnus-default-article-saver :headers))
3840                gnus-saved-headers gnus-visible-headers))
3841           ;; Ignore group parameter.  See `article-hide-headers'.
3842           (gnus-summary-buffer nil))
3843       (with-current-buffer save-buffer
3844         (article-hide-headers 1 t))))
3845   (save-window-excursion
3846     (if (not gnus-default-article-saver)
3847         (error "No default saver is defined")
3848       ;; !!! Magic!  The saving functions all save
3849       ;; `gnus-save-article-buffer' (or so they think), but we
3850       ;; bind that variable to our save-buffer.
3851       (set-buffer gnus-article-buffer)
3852       (let* ((gnus-save-article-buffer save-buffer)
3853              (filename
3854               (cond
3855                ((not gnus-prompt-before-saving) 'default)
3856                ((eq gnus-prompt-before-saving 'always) nil)
3857                (t file)))
3858              (gnus-number-of-articles-to-be-saved
3859               (when (eq gnus-prompt-before-saving t)
3860                 num)))                  ; Magic
3861         (set-buffer gnus-article-current-summary)
3862         (funcall gnus-default-article-saver filename)))))
3863
3864 (defun gnus-read-save-file-name (prompt &optional filename
3865                                         function group headers variable
3866                                         dir-var)
3867   (let ((default-name
3868           (funcall function group headers (symbol-value variable)))
3869         result)
3870     (setq result
3871           (expand-file-name
3872            (cond
3873             ((eq filename 'default)
3874              default-name)
3875             ((eq filename t)
3876              default-name)
3877             (filename filename)
3878             (t
3879              (when (symbol-value dir-var)
3880                (setq default-name (expand-file-name
3881                                    (file-name-nondirectory default-name)
3882                                    (symbol-value dir-var))))
3883              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3884                     (prompt
3885                      (format prompt
3886                              (if (and gnus-number-of-articles-to-be-saved
3887                                       (> gnus-number-of-articles-to-be-saved 1))
3888                                  (format "these %d articles"
3889                                          gnus-number-of-articles-to-be-saved)
3890                                "this article")))
3891                     (file
3892                      ;; Let the split methods have their say.
3893                      (cond
3894                       ;; No split name was found.
3895                       ((null split-name)
3896                        (read-file-name
3897                         (concat prompt " (default "
3898                                 (file-name-nondirectory default-name) "): ")
3899                         (file-name-directory default-name)
3900                         default-name))
3901                       ;; A single group name is returned.
3902                       ((stringp split-name)
3903                        (setq default-name
3904                              (funcall function split-name headers
3905                                       (symbol-value variable)))
3906                        (read-file-name
3907                         (concat prompt " (default "
3908                                 (file-name-nondirectory default-name) "): ")
3909                         (file-name-directory default-name)
3910                         default-name))
3911                       ;; A single split name was found
3912                       ((= 1 (length split-name))
3913                        (let* ((name (expand-file-name
3914                                      (car split-name)
3915                                      gnus-article-save-directory))
3916                               (dir (cond ((file-directory-p name)
3917                                           (file-name-as-directory name))
3918                                          ((file-exists-p name) name)
3919                                          (t gnus-article-save-directory))))
3920                          (read-file-name
3921                           (concat prompt " (default " name "): ")
3922                           dir name)))
3923                       ;; A list of splits was found.
3924                       (t
3925                        (setq split-name (nreverse split-name))
3926                        (let (result)
3927                          (let ((file-name-history
3928                                 (nconc split-name file-name-history)))
3929                            (setq result
3930                                  (expand-file-name
3931                                   (read-file-name
3932                                    (concat prompt " (`M-p' for defaults): ")
3933                                    gnus-article-save-directory
3934                                    (car split-name))
3935                                   gnus-article-save-directory)))
3936                          (car (push result file-name-history)))))))
3937                ;; Create the directory.
3938                (gnus-make-directory (file-name-directory file))
3939                ;; If we have read a directory, we append the default file name.
3940                (when (file-directory-p file)
3941                  (setq file (expand-file-name (file-name-nondirectory
3942                                                default-name)
3943                                               (file-name-as-directory file))))
3944                ;; Possibly translate some characters.
3945                (nnheader-translate-file-chars file))))))
3946     (gnus-make-directory (file-name-directory result))
3947     (when variable
3948       (set variable result))
3949     (when dir-var
3950       (set dir-var (file-name-directory result)))
3951     result))
3952
3953 (defun gnus-article-archive-name (group)
3954   "Return the first instance of an \"Archive-name\" in the current buffer."
3955   (let ((case-fold-search t))
3956     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3957       (nnheader-concat gnus-article-save-directory
3958                        (match-string 1)))))
3959
3960 (defun gnus-article-nndoc-name (group)
3961   "If GROUP is an nndoc group, return the name of the parent group."
3962   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3963     (gnus-group-get-parameter group 'save-article-group)))
3964
3965 (defun gnus-summary-save-in-rmail (&optional filename)
3966   "Append this article to Rmail file.
3967 Optional argument FILENAME specifies file name.
3968 Directory to save to is default to `gnus-article-save-directory'."
3969   (setq filename (gnus-read-save-file-name
3970                   "Save %s in rmail file" filename
3971                   gnus-rmail-save-name gnus-newsgroup-name
3972                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3973   (gnus-eval-in-buffer-window gnus-save-article-buffer
3974     (save-excursion
3975       (save-restriction
3976         (widen)
3977         ;; Note that unlike gnus-summary-save-in-mail, there is no
3978         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3979         ;; not use Babyl.
3980         (gnus-output-to-rmail filename))))
3981   filename)
3982
3983 (defun gnus-summary-save-in-mail (&optional filename)
3984   "Append this article to Unix mail file.
3985 Optional argument FILENAME specifies file name.
3986 Directory to save to is default to `gnus-article-save-directory'."
3987   (setq filename (gnus-read-save-file-name
3988                   "Save %s in Unix mail file" filename
3989                   gnus-mail-save-name gnus-newsgroup-name
3990                   gnus-current-headers 'gnus-newsgroup-last-mail))
3991   (gnus-eval-in-buffer-window gnus-save-article-buffer
3992     (save-excursion
3993       (save-restriction
3994         (widen)
3995         (if (and (file-readable-p filename)
3996                  (file-regular-p filename)
3997                  (mail-file-babyl-p filename))
3998             (gnus-output-to-rmail filename)
3999           (gnus-output-to-mail filename)))))
4000   filename)
4001
4002 (put 'gnus-summary-save-in-file :decode t)
4003 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4004 (defun gnus-summary-save-in-file (&optional filename overwrite)
4005   "Append this article to file.
4006 Optional argument FILENAME specifies file name.
4007 Directory to save to is default to `gnus-article-save-directory'."
4008   (setq filename (gnus-read-save-file-name
4009                   "Save %s in file" filename
4010                   gnus-file-save-name gnus-newsgroup-name
4011                   gnus-current-headers 'gnus-newsgroup-last-file))
4012   (gnus-eval-in-buffer-window gnus-save-article-buffer
4013     (save-excursion
4014       (save-restriction
4015         (widen)
4016         (when (and overwrite
4017                    (file-exists-p filename))
4018           (delete-file filename))
4019         (gnus-output-to-file filename))))
4020   filename)
4021
4022 (put 'gnus-summary-write-to-file :decode t)
4023 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4024 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4025 (defun gnus-summary-write-to-file (&optional filename)
4026   "Write this article to a file, overwriting it if the file exists.
4027 Optional argument FILENAME specifies file name.
4028 The directory to save in defaults to `gnus-article-save-directory'."
4029   (setq filename (gnus-read-save-file-name
4030                   "Save %s in file" filename
4031                   gnus-file-save-name gnus-newsgroup-name
4032                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4033   (gnus-summary-save-in-file filename t))
4034
4035 (put 'gnus-summary-save-body-in-file :decode t)
4036 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4037   "Append this article body to a file.
4038 Optional argument FILENAME specifies file name.
4039 The directory to save in defaults to `gnus-article-save-directory'."
4040   (setq filename (gnus-read-save-file-name
4041                   "Save %s body in file" filename
4042                   gnus-file-save-name gnus-newsgroup-name
4043                   gnus-current-headers 'gnus-newsgroup-last-file))
4044   (gnus-eval-in-buffer-window gnus-save-article-buffer
4045     (save-excursion
4046       (save-restriction
4047         (widen)
4048         (when (article-goto-body)
4049           (narrow-to-region (point) (point-max)))
4050         (when (and overwrite
4051                    (file-exists-p filename))
4052           (delete-file filename))
4053         (gnus-output-to-file filename))))
4054   filename)
4055
4056 (put 'gnus-summary-write-body-to-file :decode t)
4057 (put 'gnus-summary-write-body-to-file
4058      :function 'gnus-summary-save-body-in-file)
4059 (defun gnus-summary-write-body-to-file (&optional filename)
4060   "Write this article body to a file, overwriting it if the file exists.
4061 Optional argument FILENAME specifies file name.
4062 The directory to save in defaults to `gnus-article-save-directory'."
4063   (setq filename (gnus-read-save-file-name
4064                   "Save %s body in file" filename
4065                   gnus-file-save-name gnus-newsgroup-name
4066                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4067   (gnus-summary-save-body-in-file filename t))
4068
4069 (put 'gnus-summary-save-in-pipe :decode t)
4070 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4071 (defun gnus-summary-save-in-pipe (&optional command raw)
4072   "Pipe this article to subprocess COMMAND.
4073 Valid values for COMMAND include:
4074   a string
4075     The executable command name and possibly arguments.
4076   nil
4077     You will be prompted for the command in the minibuffer.
4078   the symbol `default'
4079     It will be replaced with the command which the variable
4080     `gnus-summary-pipe-output-default-command' holds or the command
4081     last used for saving.
4082 Non-nil value for RAW overrides `:decode' and `:headers' properties
4083 and the raw article including all headers will be piped."
4084   (let ((article (gnus-summary-article-number))
4085         (decode (unless raw
4086                   (get 'gnus-summary-save-in-pipe :decode)))
4087         save-buffer default)
4088     (if article
4089         (if (vectorp (gnus-summary-article-header article))
4090             (save-current-buffer
4091               (gnus-summary-select-article decode decode nil article)
4092               (insert-buffer-substring
4093                (prog1
4094                    (if decode
4095                        gnus-article-buffer
4096                      gnus-original-article-buffer)
4097                  (setq save-buffer
4098                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4099               ;; Remove unwanted headers.
4100               (when (and (not raw)
4101                          (or (get 'gnus-summary-save-in-pipe :headers)
4102                              (not gnus-save-all-headers)))
4103                 (let ((gnus-visible-headers
4104                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4105                                               :headers))
4106                            gnus-saved-headers gnus-visible-headers))
4107                       (gnus-summary-buffer nil))
4108                   (article-hide-headers 1 t))))
4109           (error "%d is not a real article" article))
4110       (error "No article to pipe"))
4111     (setq default (or gnus-summary-pipe-output-default-command
4112                       gnus-last-shell-command))
4113     (unless (stringp command)
4114       (setq command
4115             (if (and (eq command 'default) default)
4116                 default
4117               (gnus-read-shell-command "Shell command on this article: "
4118                                        default))))
4119     (when (string-equal command "")
4120       (if default
4121           (setq command default)
4122         (error "A command is required")))
4123     (gnus-eval-in-buffer-window save-buffer
4124       (save-restriction
4125         (widen)
4126         (shell-command-on-region (point-min) (point-max) command nil)))
4127     (gnus-kill-buffer save-buffer))
4128   (setq gnus-summary-pipe-output-default-command command))
4129
4130 (defun gnus-summary-pipe-to-muttprint (&optional command)
4131   "Pipe this article to muttprint."
4132   (unless (stringp command)
4133     (setq command (read-string
4134                    "Print using command: " gnus-summary-muttprint-program
4135                    nil gnus-summary-muttprint-program)))
4136   (let ((gnus-summary-pipe-output-default-command
4137          gnus-summary-pipe-output-default-command))
4138     (gnus-summary-save-in-pipe command))
4139   (setq gnus-summary-muttprint-program command))
4140
4141 ;;; Article file names when saving.
4142
4143 (defun gnus-capitalize-newsgroup (newsgroup)
4144   "Capitalize NEWSGROUP name."
4145   (when (not (zerop (length newsgroup)))
4146     (concat (char-to-string (upcase (aref newsgroup 0)))
4147             (substring newsgroup 1))))
4148
4149 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4150   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4151 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4152 Otherwise, it is like ~/News/news/group/num."
4153   (let ((default
4154           (expand-file-name
4155            (concat (if (gnus-use-long-file-name 'not-save)
4156                        (gnus-capitalize-newsgroup newsgroup)
4157                      (gnus-newsgroup-directory-form newsgroup))
4158                    "/" (int-to-string (mail-header-number headers)))
4159            gnus-article-save-directory)))
4160     (if (and last-file
4161              (string-equal (file-name-directory default)
4162                            (file-name-directory last-file))
4163              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4164         default
4165       (or last-file default))))
4166
4167 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4168   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4169 If variable `gnus-use-long-file-name' is non-nil, it is
4170 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4171   (let ((default
4172           (expand-file-name
4173            (concat (if (gnus-use-long-file-name 'not-save)
4174                        newsgroup
4175                      (gnus-newsgroup-directory-form newsgroup))
4176                    "/" (int-to-string (mail-header-number headers)))
4177            gnus-article-save-directory)))
4178     (if (and last-file
4179              (string-equal (file-name-directory default)
4180                            (file-name-directory last-file))
4181              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4182         default
4183       (or last-file default))))
4184
4185 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4186   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4187 If variable `gnus-use-long-file-name' is non-nil, it is
4188 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4189   (or last-file
4190       (expand-file-name
4191        (if (gnus-use-long-file-name 'not-save)
4192            newsgroup
4193          (file-relative-name
4194           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4195           default-directory))
4196        gnus-article-save-directory)))
4197
4198 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4199   "Generate file name from sender."
4200   (let ((from (mail-header-from headers)))
4201     (expand-file-name
4202      (if (and from (string-match "\\([^ <]+\\)@" from))
4203          (match-string 1 from)
4204        "nobody")
4205      gnus-article-save-directory)))
4206
4207 (defun article-verify-x-pgp-sig ()
4208   "Verify X-PGP-Sig."
4209   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4210   (interactive)
4211   (if (gnus-buffer-live-p gnus-original-article-buffer)
4212       (let ((sig (with-current-buffer gnus-original-article-buffer
4213                    (gnus-fetch-field "X-PGP-Sig")))
4214             items info headers)
4215         (when (and sig
4216                    mml2015-use
4217                    (mml2015-clear-verify-function))
4218           (with-temp-buffer
4219             (insert-buffer-substring gnus-original-article-buffer)
4220             (setq items (split-string sig))
4221             (message-narrow-to-head)
4222             (let ((inhibit-point-motion-hooks t)
4223                   (case-fold-search t))
4224               ;; Don't verify multiple headers.
4225               (setq headers (mapconcat (lambda (header)
4226                                          (concat header ": "
4227                                                  (mail-fetch-field header)
4228                                                  "\n"))
4229                                        (split-string (nth 1 items) ",") "")))
4230             (delete-region (point-min) (point-max))
4231             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4232             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4233             (insert headers)
4234             (widen)
4235             (forward-line)
4236             (while (not (eobp))
4237               (if (looking-at "^-")
4238                   (insert "- "))
4239               (forward-line))
4240             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4241             (insert "Version: " (car items) "\n\n")
4242             (insert (mapconcat 'identity (cddr items) "\n"))
4243             (insert "\n-----END PGP SIGNATURE-----\n")
4244             (let ((mm-security-handle (list (format "multipart/signed"))))
4245               (mml2015-clean-buffer)
4246               (let ((coding-system-for-write (or gnus-newsgroup-charset
4247                                                  'iso-8859-1)))
4248                 (funcall (mml2015-clear-verify-function)))
4249               (setq info
4250                     (or (mm-handle-multipart-ctl-parameter
4251                          mm-security-handle 'gnus-details)
4252                         (mm-handle-multipart-ctl-parameter
4253                          mm-security-handle 'gnus-info)))))
4254           (when info
4255             (let ((inhibit-read-only t) bface eface)
4256               (save-restriction
4257                 (message-narrow-to-head)
4258                 (goto-char (point-max))
4259                 (forward-line -1)
4260                 (setq bface (get-text-property (point-at-bol) 'face)
4261                       eface (get-text-property (1- (point-at-eol)) 'face))
4262                 (message-remove-header "X-Gnus-PGP-Verify")
4263                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4264                     (forward-line)
4265                   (goto-char (point-max)))
4266                 (narrow-to-region (point) (point))
4267                 (insert "X-Gnus-PGP-Verify: " info "\n")
4268                 (goto-char (point-min))
4269                 (forward-line)
4270                 (while (not (eobp))
4271                   (if (not (looking-at "^[ \t]"))
4272                       (insert " "))
4273                   (forward-line))
4274                 ;; Do highlighting.
4275                 (goto-char (point-min))
4276                 (when (looking-at "\\([^:]+\\): *")
4277                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4278                                      'face bface)
4279                   (put-text-property (match-end 0) (point-max)
4280                                      'face eface)))))))))
4281
4282 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4283
4284 (defun article-verify-cancel-lock ()
4285   "Verify Cancel-Lock header."
4286   (interactive)
4287   (if (gnus-buffer-live-p gnus-original-article-buffer)
4288       (canlock-verify gnus-original-article-buffer)))
4289
4290 (eval-and-compile
4291   (mapc
4292    (lambda (func)
4293      (let (afunc gfunc)
4294        (if (consp func)
4295            (setq afunc (car func)
4296                  gfunc (cdr func))
4297          (setq afunc func
4298                gfunc (intern (format "gnus-%s" func))))
4299        (defalias gfunc
4300          (when (fboundp afunc)
4301            `(lambda (&optional interactive &rest args)
4302               ,(documentation afunc t)
4303               (interactive (list t))
4304               (with-current-buffer gnus-article-buffer
4305                 (if interactive
4306                     (call-interactively ',afunc)
4307                   (apply ',afunc args))))))))
4308    '(article-hide-headers
4309      article-verify-x-pgp-sig
4310      article-verify-cancel-lock
4311      article-hide-boring-headers
4312      article-treat-overstrike
4313      article-treat-ansi-sequences
4314      article-fill-long-lines
4315      article-capitalize-sentences
4316      article-remove-cr
4317      article-remove-leading-whitespace
4318      article-display-x-face
4319      article-display-face
4320      article-de-quoted-unreadable
4321      article-de-base64-unreadable
4322      article-decode-HZ
4323      article-wash-html
4324      article-unsplit-urls
4325      article-hide-list-identifiers
4326      article-strip-banner
4327      article-babel
4328      article-hide-pem
4329      article-hide-signature
4330      article-strip-headers-in-body
4331      article-remove-trailing-blank-lines
4332      article-strip-leading-blank-lines
4333      article-strip-multiple-blank-lines
4334      article-strip-leading-space
4335      article-strip-trailing-space
4336      article-strip-blank-lines
4337      article-strip-all-blank-lines
4338      article-date-local
4339      article-date-english
4340      article-date-iso8601
4341      article-date-original
4342      article-date-ut
4343      article-decode-mime-words
4344      article-decode-charset
4345      article-decode-encoded-words
4346      article-date-user
4347      article-date-lapsed
4348      article-emphasize
4349      article-treat-dumbquotes
4350      article-normalize-headers
4351      ;;(article-show-all . gnus-article-show-all-headers)
4352      )))
4353 \f
4354 ;;;
4355 ;;; Gnus article mode
4356 ;;;
4357
4358 (put 'gnus-article-mode 'mode-class 'special)
4359
4360 (set-keymap-parent gnus-article-mode-map widget-keymap)
4361
4362 (gnus-define-keys gnus-article-mode-map
4363   " " gnus-article-goto-next-page
4364   "\177" gnus-article-goto-prev-page
4365   [delete] gnus-article-goto-prev-page
4366   [backspace] gnus-article-goto-prev-page
4367   "\C-c^" gnus-article-refer-article
4368   "h" gnus-article-show-summary
4369   "s" gnus-article-show-summary
4370   "\C-c\C-m" gnus-article-mail
4371   "?" gnus-article-describe-briefly
4372   "<" beginning-of-buffer
4373   ">" end-of-buffer
4374   "\C-c\C-i" gnus-info-find-node
4375   "\C-c\C-b" gnus-bug
4376   "R" gnus-article-reply-with-original
4377   "F" gnus-article-followup-with-original
4378   "\C-hk" gnus-article-describe-key
4379   "\C-hc" gnus-article-describe-key-briefly
4380   "\C-hb" gnus-article-describe-bindings
4381
4382   "e" gnus-article-read-summary-keys
4383   "\C-d" gnus-article-read-summary-keys
4384   "\M-*" gnus-article-read-summary-keys
4385   "\M-#" gnus-article-read-summary-keys
4386   "\M-^" gnus-article-read-summary-keys
4387   "\M-g" gnus-article-read-summary-keys)
4388
4389 (substitute-key-definition
4390  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4391
4392 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4393   "W" gnus-article-wide-reply-with-original)
4394 (if (featurep 'xemacs)
4395     (set-keymap-default-binding gnus-article-send-map
4396                                 'gnus-article-read-summary-send-keys)
4397   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4398
4399 (defun gnus-article-make-menu-bar ()
4400   (unless (boundp 'gnus-article-commands-menu)
4401     (gnus-summary-make-menu-bar))
4402   (gnus-turn-off-edit-menu 'article)
4403   (unless (boundp 'gnus-article-article-menu)
4404     (easy-menu-define
4405      gnus-article-article-menu gnus-article-mode-map ""
4406      '("Article"
4407        ["Scroll forwards" gnus-article-goto-next-page t]
4408        ["Scroll backwards" gnus-article-goto-prev-page t]
4409        ["Show summary" gnus-article-show-summary t]
4410        ["Fetch Message-ID at point" gnus-article-refer-article t]
4411        ["Mail to address at point" gnus-article-mail t]
4412        ["Send a bug report" gnus-bug t]))
4413
4414     (easy-menu-define
4415      gnus-article-treatment-menu gnus-article-mode-map ""
4416      ;; Fixme: this should use :active (and maybe :visible).
4417      '("Treatment"
4418        ["Hide headers" gnus-article-hide-headers t]
4419        ["Hide signature" gnus-article-hide-signature t]
4420        ["Hide citation" gnus-article-hide-citation t]
4421        ["Treat overstrike" gnus-article-treat-overstrike t]
4422        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4423        ["Remove carriage return" gnus-article-remove-cr t]
4424        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4425        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4426        ["Remove base64" gnus-article-de-base64-unreadable t]
4427        ["Treat html" gnus-article-wash-html t]
4428        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4429        ["Decode HZ" gnus-article-decode-HZ t]))
4430
4431     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4432
4433     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4434
4435     (gnus-run-hooks 'gnus-article-menu-hook)))
4436
4437 (defvar bookmark-make-record-function)
4438
4439 (defun gnus-article-mode ()
4440   "Major mode for displaying an article.
4441
4442 All normal editing commands are switched off.
4443
4444 The following commands are available in addition to all summary mode
4445 commands:
4446 \\<gnus-article-mode-map>
4447 \\[gnus-article-next-page]\t Scroll the article one page forwards
4448 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4449 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4450 \\[gnus-article-show-summary]\t Display the summary buffer
4451 \\[gnus-article-mail]\t Send a reply to the address near point
4452 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4453 \\[gnus-info-find-node]\t Go to the Gnus info node"
4454   (interactive)
4455   (kill-all-local-variables)
4456   (gnus-simplify-mode-line)
4457   (setq mode-name "Article")
4458   (setq major-mode 'gnus-article-mode)
4459   (make-local-variable 'minor-mode-alist)
4460   (use-local-map gnus-article-mode-map)
4461   (when (gnus-visual-p 'article-menu 'menu)
4462     (gnus-article-make-menu-bar)
4463     (when gnus-summary-tool-bar-map
4464       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4465   (gnus-update-format-specifications nil 'article-mode)
4466   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4467   (set (make-local-variable 'gnus-page-broken) nil)
4468   (make-local-variable 'gnus-button-marker-list)
4469   (make-local-variable 'gnus-article-current-summary)
4470   (make-local-variable 'gnus-article-mime-handles)
4471   (make-local-variable 'gnus-article-decoded-p)
4472   (make-local-variable 'gnus-article-mime-handle-alist)
4473   (make-local-variable 'gnus-article-wash-types)
4474   (make-local-variable 'gnus-article-image-alist)
4475   (make-local-variable 'gnus-article-charset)
4476   (make-local-variable 'gnus-article-ignored-charsets)
4477   (set (make-local-variable 'bookmark-make-record-function)
4478        'gnus-summary-bookmark-make-record)
4479   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4480   ;; face.
4481   (set (make-local-variable 'nobreak-char-display) nil)
4482   (setq cursor-in-non-selected-windows nil)
4483   (setq truncate-lines gnus-article-truncate-lines)
4484   (gnus-set-default-directory)
4485   (buffer-disable-undo)
4486   (setq buffer-read-only t
4487         show-trailing-whitespace nil)
4488   (set-syntax-table gnus-article-mode-syntax-table)
4489   (mm-enable-multibyte)
4490   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4491
4492 (defvar gnus-button-marker-list nil
4493   "Regexp matching any of the regexps from `gnus-button-alist'.
4494 Internal variable.")
4495
4496 (defun gnus-article-setup-buffer ()
4497   "Initialize the article buffer."
4498   (let* ((name (if gnus-single-article-buffer "*Article*"
4499                  (concat "*Article " gnus-newsgroup-name "*")))
4500          (original
4501           (progn (string-match "\\*Article" name)
4502                  (concat " *Original Article"
4503                          (substring name (match-end 0))))))
4504     (setq gnus-article-buffer name)
4505     (setq gnus-original-article-buffer original)
4506     (setq gnus-article-mime-handle-alist nil)
4507     (with-current-buffer gnus-summary-buffer
4508       ;; This might be a variable local to the summary buffer.
4509       (unless gnus-single-article-buffer
4510         (setq gnus-article-buffer name)
4511         (setq gnus-original-article-buffer original)
4512         (gnus-set-global-variables)))
4513     (gnus-article-setup-highlight-words)
4514     ;; Init original article buffer.
4515     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4516       (mm-enable-multibyte)
4517       (setq major-mode 'gnus-original-article-mode)
4518       (make-local-variable 'gnus-original-article))
4519     (if (and (get-buffer name)
4520              (with-current-buffer name
4521                (if gnus-article-edit-mode
4522                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4523                        (progn
4524                          (set-buffer-modified-p nil)
4525                          (gnus-kill-buffer name)
4526                          (message "")
4527                          nil)
4528                      (error "Action aborted"))
4529                  t)))
4530         (with-current-buffer name
4531           (set (make-local-variable 'gnus-article-edit-mode) nil)
4532           (when gnus-article-mime-handles
4533             (mm-destroy-parts gnus-article-mime-handles)
4534             (setq gnus-article-mime-handles nil))
4535           ;; Set it to nil in article-buffer!
4536           (setq gnus-article-mime-handle-alist nil)
4537           (buffer-disable-undo)
4538           (setq buffer-read-only t)
4539           ;; This list just keeps growing if we don't reset it.
4540           (setq gnus-button-marker-list nil)
4541           (unless (eq major-mode 'gnus-article-mode)
4542             (gnus-article-mode))
4543           (current-buffer))
4544       (with-current-buffer (gnus-get-buffer-create name)
4545         (gnus-article-mode)
4546         (make-local-variable 'gnus-summary-buffer)
4547         (setq gnus-summary-buffer
4548               (gnus-summary-buffer-name gnus-newsgroup-name))
4549         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4550         (current-buffer)))))
4551
4552 ;; Set article window start at LINE, where LINE is the number of lines
4553 ;; from the head of the article.
4554 (defun gnus-article-set-window-start (&optional line)
4555   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4556     (when article-window
4557       (set-window-start
4558        article-window
4559        (with-current-buffer gnus-article-buffer
4560          (goto-char (point-min))
4561          (if (not line)
4562              (point-min)
4563            (gnus-message 6 "Moved to bookmark")
4564            (search-forward "\n\n" nil t)
4565            (forward-line line)
4566            (point)))))))
4567
4568 (defun gnus-article-prepare (article &optional all-headers header)
4569   "Prepare ARTICLE in article mode buffer.
4570 ARTICLE should either be an article number or a Message-ID.
4571 If ARTICLE is an id, HEADER should be the article headers.
4572 If ALL-HEADERS is non-nil, no headers are hidden."
4573   (save-excursion
4574     ;; Make sure we start in a summary buffer.
4575     (unless (eq major-mode 'gnus-summary-mode)
4576       (set-buffer gnus-summary-buffer))
4577     (setq gnus-summary-buffer (current-buffer))
4578     (let* ((gnus-article (if header (mail-header-number header) article))
4579            (summary-buffer (current-buffer))
4580            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4581            (group gnus-newsgroup-name)
4582            result)
4583       (save-excursion
4584         (gnus-article-setup-buffer)
4585         (set-buffer gnus-article-buffer)
4586         ;; Deactivate active regions.
4587         (when (and (boundp 'transient-mark-mode)
4588                    transient-mark-mode)
4589           (setq mark-active nil))
4590         (if (not (setq result (let ((inhibit-read-only t))
4591                                 (gnus-request-article-this-buffer
4592                                  article group))))
4593             ;; There is no such article.
4594             (save-excursion
4595               (when (and (numberp article)
4596                          (not (memq article gnus-newsgroup-sparse)))
4597                 (setq gnus-article-current
4598                       (cons gnus-newsgroup-name article))
4599                 (set-buffer gnus-summary-buffer)
4600                 (setq gnus-current-article article)
4601                 (if (and (memq article gnus-newsgroup-undownloaded)
4602                          (not (gnus-online (gnus-find-method-for-group
4603                                             gnus-newsgroup-name))))
4604                     (progn
4605                       (gnus-summary-set-agent-mark article)
4606                       (message "Message marked for downloading"))
4607                   (gnus-summary-mark-article article gnus-canceled-mark)
4608                   (unless (memq article gnus-newsgroup-sparse)
4609                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4610           (if (or (eq result 'pseudo)
4611                   (eq result 'nneething))
4612               (progn
4613                 (with-current-buffer summary-buffer
4614                   (push article gnus-newsgroup-history)
4615                   (setq gnus-last-article gnus-current-article
4616                         gnus-current-article 0
4617                         gnus-current-headers nil
4618                         gnus-article-current nil)
4619                   (if (eq result 'nneething)
4620                       (gnus-configure-windows 'summary)
4621                     (gnus-configure-windows 'article))
4622                   (gnus-set-global-variables))
4623                 (let ((gnus-article-mime-handle-alist-1
4624                        gnus-article-mime-handle-alist))
4625                   (gnus-set-mode-line 'article)))
4626             ;; The result from the `request' was an actual article -
4627             ;; or at least some text that is now displayed in the
4628             ;; article buffer.
4629             (when (and (numberp article)
4630                        (not (eq article gnus-current-article)))
4631               ;; Seems like a new article has been selected.
4632               ;; `gnus-current-article' must be an article number.
4633               (with-current-buffer summary-buffer
4634                 (push article gnus-newsgroup-history)
4635                 (setq gnus-last-article gnus-current-article
4636                       gnus-current-article article
4637                       gnus-current-headers
4638                       (gnus-summary-article-header gnus-current-article)
4639                       gnus-article-current
4640                       (cons gnus-newsgroup-name gnus-current-article))
4641                 (unless (vectorp gnus-current-headers)
4642                   (setq gnus-current-headers nil))
4643                 (gnus-summary-goto-subject gnus-current-article)
4644                 (when (gnus-summary-show-thread)
4645                   ;; If the summary buffer really was folded, the
4646                   ;; previous goto may not actually have gone to
4647                   ;; the right article, but the thread root instead.
4648                   ;; So we go again.
4649                   (gnus-summary-goto-subject gnus-current-article))
4650                 (gnus-run-hooks 'gnus-mark-article-hook)
4651                 (gnus-set-mode-line 'summary)
4652                 (when (gnus-visual-p 'article-highlight 'highlight)
4653                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4654                 ;; Set the global newsgroup variables here.
4655                 (gnus-set-global-variables)
4656                 (setq gnus-have-all-headers
4657                       (or all-headers gnus-show-all-headers))))
4658             (save-excursion
4659               (gnus-configure-windows 'article))
4660             (when (or (numberp article)
4661                       (stringp article))
4662               (gnus-article-prepare-display)
4663               ;; Do page break.
4664               (goto-char (point-min))
4665               (when gnus-break-pages
4666                 (gnus-narrow-to-page)))
4667             (let ((gnus-article-mime-handle-alist-1
4668                    gnus-article-mime-handle-alist))
4669               (gnus-set-mode-line 'article))
4670             (article-goto-body)
4671             (unless (bobp)
4672               (forward-line -1))
4673             (set-window-point (get-buffer-window (current-buffer)) (point))
4674             (gnus-configure-windows 'article)
4675             t))))))
4676
4677 ;;;###autoload
4678 (defun gnus-article-prepare-display ()
4679   "Make the current buffer look like a nice article."
4680   ;; Hooks for getting information from the article.
4681   ;; This hook must be called before being narrowed.
4682   (let ((gnus-article-buffer (current-buffer))
4683         buffer-read-only
4684         (inhibit-read-only t))
4685     (unless (eq major-mode 'gnus-article-mode)
4686       (gnus-article-mode))
4687     (setq buffer-read-only nil
4688           gnus-article-wash-types nil
4689           gnus-article-image-alist nil)
4690     (gnus-run-hooks 'gnus-tmp-internal-hook)
4691     (when gnus-display-mime-function
4692       (funcall gnus-display-mime-function))
4693     (gnus-run-hooks 'gnus-article-prepare-hook)))
4694
4695 ;;;
4696 ;;; Gnus Sticky Article Mode
4697 ;;;
4698
4699 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4700   "Mode for sticky articles."
4701   ;; Release bindings that won't work.
4702   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4703                              gnus-sticky-article-mode-map)
4704   (substitute-key-definition 'gnus-article-refer-article 'undefined
4705                              gnus-sticky-article-mode-map)
4706   (dolist (k '("e" "h" "s" "F" "R"))
4707     (define-key gnus-sticky-article-mode-map k nil))
4708   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4709   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4710   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4711   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4712
4713 (defun gnus-sticky-article (arg)
4714   "Make the current article sticky.
4715 If a prefix ARG is given, ask for a name for this sticky article buffer."
4716   (interactive "P")
4717   (gnus-summary-show-thread)
4718   (gnus-summary-select-article nil nil 'pseudo)
4719   (let (new-art-buf-name)
4720     (gnus-eval-in-buffer-window gnus-article-buffer
4721       (setq new-art-buf-name
4722             (concat
4723              "*Sticky Article: "
4724              (if arg
4725                  (read-from-minibuffer "Sticky article buffer name: ")
4726                (gnus-with-article-headers
4727                  (gnus-article-goto-header "subject")
4728                  (setq new-art-buf-name
4729                        (buffer-substring-no-properties
4730                         (line-beginning-position) (line-end-position)))
4731                  (goto-char (point-min))
4732                  (gnus-article-goto-header "from")
4733                  (setq new-art-buf-name
4734                        (concat
4735                         new-art-buf-name ", "
4736                         (buffer-substring-no-properties
4737                          (line-beginning-position) (line-end-position))))
4738                  (goto-char (point-min))
4739                  (gnus-article-goto-header "date")
4740                  (setq new-art-buf-name
4741                        (concat
4742                         new-art-buf-name ", "
4743                         (buffer-substring-no-properties
4744                          (line-beginning-position) (line-end-position))))))
4745              "*"))
4746       (if (and (gnus-buffer-live-p new-art-buf-name)
4747                (with-current-buffer new-art-buf-name
4748                  (eq major-mode 'gnus-sticky-article-mode)))
4749           (switch-to-buffer new-art-buf-name)
4750         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4751       (gnus-sticky-article-mode))
4752     (setq gnus-article-buffer new-art-buf-name))
4753   (gnus-summary-recenter)
4754   (gnus-summary-position-point))
4755
4756 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4757   "Kill the given sticky article BUFFER.
4758 If none is given, assume the current buffer and kill it if it has
4759 `gnus-sticky-article-mode'."
4760   (interactive)
4761   (unless buffer
4762     (setq buffer (current-buffer)))
4763   (with-current-buffer buffer
4764     (when (eq major-mode 'gnus-sticky-article-mode)
4765       (gnus-kill-buffer buffer))))
4766
4767 (defun gnus-kill-sticky-article-buffers (arg)
4768   "Kill all sticky article buffers.
4769 If a prefix ARG is given, ask for confirmation."
4770   (interactive "P")
4771   (dolist (buf (gnus-buffers))
4772     (with-current-buffer buf
4773       (when (eq major-mode 'gnus-sticky-article-mode)
4774         (if (not arg)
4775             (gnus-kill-buffer buf)
4776           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4777             (gnus-kill-buffer buf)))))))
4778
4779 ;;;
4780 ;;; Gnus MIME viewing functions
4781 ;;;
4782
4783 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4784   "Format of the MIME buttons.
4785
4786 Valid specifiers include:
4787 %t  The MIME type
4788 %T  MIME type, along with additional info
4789 %n  The `name' parameter
4790 %d  The description, if any
4791 %l  The length of the encoded part
4792 %p  The part identifier number
4793 %e  Dots if the part isn't displayed
4794
4795 General format specifiers can also be used.  See Info node
4796 `(gnus)Formatting Variables'.")
4797
4798 (defvar gnus-mime-button-line-format-alist
4799   '((?t gnus-tmp-type ?s)
4800     (?T gnus-tmp-type-long ?s)
4801     (?n gnus-tmp-name ?s)
4802     (?d gnus-tmp-description ?s)
4803     (?p gnus-tmp-id ?s)
4804     (?l gnus-tmp-length ?d)
4805     (?e gnus-tmp-dots ?s)))
4806
4807 (defvar gnus-mime-button-commands
4808   '((gnus-article-press-button "\r" "Toggle Display")
4809     (gnus-mime-view-part "v" "View Interactively...")
4810     (gnus-mime-view-part-as-type "t" "View As Type...")
4811     (gnus-mime-view-part-as-charset "C" "View As charset...")
4812     (gnus-mime-save-part "o" "Save...")
4813     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4814     (gnus-mime-replace-part "r" "Replace part")
4815     (gnus-mime-delete-part "d" "Delete part")
4816     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4817     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4818     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4819     (gnus-mime-view-part-externally "e" "View Externally")
4820     (gnus-mime-print-part "p" "Print")
4821     (gnus-mime-pipe-part "|" "Pipe To Command...")
4822     (gnus-mime-action-on-part "." "Take action on the part...")))
4823
4824 (defun gnus-article-mime-part-status ()
4825   (if gnus-article-mime-handle-alist-1
4826       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4827           " (1 part)"
4828         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4829     ""))
4830
4831 (defvar gnus-mime-button-map
4832   (let ((map (make-sparse-keymap)))
4833     (define-key map gnus-mouse-2 'gnus-article-push-button)
4834     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4835     (dolist (c gnus-mime-button-commands)
4836       (define-key map (cadr c) (car c)))
4837     map))
4838
4839 (easy-menu-define
4840   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4841   `("MIME Part"
4842     ,@(mapcar (lambda (c)
4843                 (vector (caddr c) (car c) :active t))
4844               gnus-mime-button-commands)))
4845
4846 (defvar gnus-url-button-commands
4847   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4848
4849 (defvar gnus-url-button-map
4850   (let ((map (make-sparse-keymap)))
4851     (dolist (c gnus-url-button-commands)
4852       (define-key map (cadr c) (car c)))
4853     map))
4854
4855 (easy-menu-define
4856   gnus-url-button-menu gnus-url-button-map "URL button menu."
4857   `("Url Button"
4858     ,@(mapcar (lambda (c)
4859                 (vector (caddr c) (car c) :active t))
4860               gnus-url-button-commands)))
4861
4862 (defmacro gnus-bind-safe-url-regexp (&rest body)
4863   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4864   `(let ((mm-w3m-safe-url-regexp
4865           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4866                                 (gnus-buffer-live-p
4867                                  gnus-article-current-summary))
4868                            (with-current-buffer gnus-article-current-summary
4869                              gnus-newsgroup-name)
4870                          gnus-newsgroup-name)))
4871             (if (cond ((not group)
4872                        ;; Maybe we're in a mml-preview buffer
4873                        ;; and no group is selected.
4874                        t)
4875                       ((stringp gnus-safe-html-newsgroups)
4876                        (string-match gnus-safe-html-newsgroups group))
4877                       ((consp gnus-safe-html-newsgroups)
4878                        (member group gnus-safe-html-newsgroups)))
4879                 nil
4880               mm-w3m-safe-url-regexp))))
4881      ,@body))
4882
4883 (defun gnus-mime-button-menu (event prefix)
4884  "Construct a context-sensitive menu of MIME commands."
4885  (interactive "e\nP")
4886  (save-window-excursion
4887    (let ((pos (event-start event)))
4888      (select-window (posn-window pos))
4889      (goto-char (posn-point pos))
4890      (gnus-article-check-buffer)
4891      (popup-menu gnus-mime-button-menu nil prefix))))
4892
4893 (defun gnus-mime-view-all-parts (&optional handles)
4894   "View all the MIME parts."
4895   (interactive)
4896   (with-current-buffer gnus-article-buffer
4897     (let ((handles (or handles gnus-article-mime-handles))
4898           (mail-parse-charset gnus-newsgroup-charset)
4899           (mail-parse-ignored-charsets
4900            (with-current-buffer gnus-summary-buffer
4901              gnus-newsgroup-ignored-charsets)))
4902       (when handles
4903         (mm-remove-parts handles)
4904         (goto-char (point-min))
4905         (or (search-forward "\n\n") (goto-char (point-max)))
4906         (let ((inhibit-read-only t))
4907           (delete-region (point) (point-max))
4908           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4909
4910 (defun gnus-article-jump-to-part (n)
4911   "Jump to MIME part N."
4912   (interactive "P")
4913   (pop-to-buffer gnus-article-buffer)
4914   ;; FIXME: why is it necessary?
4915   (sit-for 0)
4916   (let ((parts (length gnus-article-mime-handle-alist)))
4917     (or n (setq n (read-number (format "Jump to part (2..%s): " parts))))
4918     (unless (and (integerp n) (<= n parts) (>= n 1))
4919       (setq n
4920             (progn
4921               (gnus-message 7 "Invalid part `%s', using %s instead."
4922                             n parts)
4923               parts)))
4924     (gnus-message 9 "Jumping to part %s." n)
4925     (cond ((>= gnus-auto-select-part 1)
4926            (while (and (<= n parts)
4927                        (not (gnus-article-goto-part n)))
4928              (setq n (1+ n))))
4929           ((< gnus-auto-select-part 0)
4930            (while (and (>= n 1)
4931                        (not (gnus-article-goto-part n)))
4932              (setq n (1- n))))
4933           (t
4934            (gnus-article-goto-part n)))))
4935
4936 (defvar gnus-mime-buttonized-part-id nil
4937   "ID of a mime part that should be buttonized.
4938 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4939
4940 (eval-when-compile
4941   (defsubst gnus-article-edit-part (handles &optional current-id)
4942     "Edit an article in order to delete a mime part.
4943 This function is exclusively used by `gnus-mime-save-part-and-strip'
4944 and `gnus-mime-delete-part', and not provided at run-time normally."
4945     (gnus-article-edit-article
4946      `(lambda ()
4947         (buffer-disable-undo)
4948         (erase-buffer)
4949         (let ((mail-parse-charset (or gnus-article-charset
4950                                       ',gnus-newsgroup-charset))
4951               (mail-parse-ignored-charsets
4952                (or gnus-article-ignored-charsets
4953                    ',gnus-newsgroup-ignored-charsets))
4954               (mbl mml-buffer-list))
4955           (setq mml-buffer-list nil)
4956           (insert-buffer-substring gnus-original-article-buffer)
4957           (mime-to-mml ',handles)
4958           (setq gnus-article-mime-handles nil)
4959           (let ((mbl1 mml-buffer-list))
4960             (setq mml-buffer-list mbl)
4961             (set (make-local-variable 'mml-buffer-list) mbl1))
4962           (gnus-make-local-hook 'kill-buffer-hook)
4963           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4964      `(lambda (no-highlight)
4965         (let ((mail-parse-charset (or gnus-article-charset
4966                                       ',gnus-newsgroup-charset))
4967               (message-options message-options)
4968               (message-options-set-recipient)
4969               (mail-parse-ignored-charsets
4970                (or gnus-article-ignored-charsets
4971                    ',gnus-newsgroup-ignored-charsets)))
4972           (mml-to-mime)
4973           (mml-destroy-buffers)
4974           (remove-hook 'kill-buffer-hook
4975                        'mml-destroy-buffers t)
4976           (kill-local-variable 'mml-buffer-list))
4977         (gnus-summary-edit-article-done
4978          ,(or (mail-header-references gnus-current-headers) "")
4979          ,(gnus-group-read-only-p)
4980          ,gnus-summary-buffer no-highlight))
4981      t)
4982     ;; Force buttonizing this part.
4983     (let ((gnus-mime-buttonized-part-id current-id))
4984       (gnus-article-edit-done))
4985     (gnus-configure-windows 'article)
4986     (when (and current-id (integerp gnus-auto-select-part))
4987       (gnus-article-jump-to-part
4988        (min (max (+ current-id gnus-auto-select-part) 1)
4989             (with-current-buffer gnus-article-buffer
4990               (length gnus-article-mime-handle-alist)))))))
4991
4992 (defun gnus-mime-replace-part (file)
4993   "Replace MIME part under point with an external body."
4994   ;; Useful if file has already been saved to disk
4995   (interactive
4996    (list
4997     (read-file-name "Replace MIME part with file: "
4998                     (or mm-default-directory default-directory)
4999                     nil nil)))
5000   (gnus-mime-save-part-and-strip file))
5001
5002 (defun gnus-mime-save-part-and-strip (&optional file)
5003   "Save the MIME part under point then replace it with an external body.
5004 If FILE is given, use it for the external part."
5005   (interactive)
5006   (gnus-article-check-buffer)
5007   (when (gnus-group-read-only-p)
5008     (error "The current group does not support deleting of parts"))
5009   (when (mm-complicated-handles gnus-article-mime-handles)
5010     (error "\
5011 The current article has a complicated MIME structure, giving up..."))
5012   (let* ((data (get-text-property (point) 'gnus-data))
5013          (id (get-text-property (point) 'gnus-part))
5014          param
5015          (handles gnus-article-mime-handles))
5016     (unless file
5017       (setq file
5018             (and data (mm-save-part data "Delete MIME part and save to: "))))
5019     (when file
5020       (with-current-buffer (mm-handle-buffer data)
5021         (erase-buffer)
5022         (insert "Content-Type: " (mm-handle-media-type data))
5023         (mml-insert-parameter-string (cdr (mm-handle-type data))
5024                                      '(charset))
5025         ;; Add a filename for the sake of saving the part again.
5026         (mml-insert-parameter
5027          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5028         (insert "\n")
5029         (insert "Content-ID: " (message-make-message-id) "\n")
5030         (insert "Content-Transfer-Encoding: binary\n")
5031         (insert "\n"))
5032       (setcdr data
5033               (cdr (mm-make-handle nil
5034                                    `("message/external-body"
5035                                      (access-type . "LOCAL-FILE")
5036                                      (name . ,file)))))
5037       ;; (set-buffer gnus-summary-buffer)
5038       (gnus-article-edit-part handles id))))
5039
5040 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5041 ;; parts...>') but with stripping would be nice.
5042
5043 (defun gnus-mime-delete-part ()
5044   "Delete the MIME part under point.
5045 Replace it with some information about the removed part."
5046   (interactive)
5047   (gnus-article-check-buffer)
5048   (when (gnus-group-read-only-p)
5049     (error "The current group does not support deleting of parts"))
5050   (when (mm-complicated-handles gnus-article-mime-handles)
5051     (error "\
5052 The current article has a complicated MIME structure, giving up..."))
5053   (when (or gnus-expert-user
5054             (gnus-yes-or-no-p "\
5055 Deleting parts may malfunction or destroy the article; continue? "))
5056     (let* ((data (get-text-property (point) 'gnus-data))
5057            (id (get-text-property (point) 'gnus-part))
5058            (handles gnus-article-mime-handles)
5059            (none "(none)")
5060            (description
5061             (let ((desc (mm-handle-description data)))
5062               (when desc
5063                 (mail-decode-encoded-word-string desc))))
5064            (filename
5065             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
5066                 none))
5067            (type (mm-handle-media-type data)))
5068       (unless data
5069         (error "No MIME part under point"))
5070       (with-current-buffer (mm-handle-buffer data)
5071         (let ((bsize (format "%s" (buffer-size))))
5072           (erase-buffer)
5073           (insert
5074            (concat
5075             ",----\n"
5076             "| The following attachment has been deleted:\n"
5077             "|\n"
5078             "| Type:           " type "\n"
5079             "| Filename:       " filename "\n"
5080             "| Size (encoded): " bsize " Byte\n"
5081             (when description
5082               (concat    "| Description:    " description "\n"))
5083             "`----\n"))
5084           (setcdr data
5085                   (cdr (mm-make-handle
5086                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5087                         (list "attachment")
5088                         (format "Deleted attachment (%s bytes)" bsize))))))
5089       ;; (set-buffer gnus-summary-buffer)
5090       (gnus-article-edit-part handles id))))
5091
5092 (defun gnus-mime-save-part ()
5093   "Save the MIME part under point."
5094   (interactive)
5095   (gnus-article-check-buffer)
5096   (let ((data (get-text-property (point) 'gnus-data)))
5097     (when data
5098       (mm-save-part data))))
5099
5100 (defun gnus-mime-pipe-part (&optional cmd)
5101   "Pipe the MIME part under point to a process.
5102 Use CMD as the process."
5103   (interactive)
5104   (gnus-article-check-buffer)
5105   (let ((data (get-text-property (point) 'gnus-data)))
5106     (when data
5107       (mm-pipe-part data cmd))))
5108
5109 (defun gnus-mime-view-part ()
5110   "Interactively choose a viewing method for the MIME part under point."
5111   (interactive)
5112   (gnus-article-check-buffer)
5113   (let ((data (get-text-property (point) 'gnus-data)))
5114     (when data
5115       (setq gnus-article-mime-handles
5116             (mm-merge-handles
5117              gnus-article-mime-handles (setq data (copy-sequence data))))
5118       (mm-interactively-view-part data))))
5119
5120 (defun gnus-mime-view-part-as-type-internal ()
5121   (gnus-article-check-buffer)
5122   (let* ((handle (get-text-property (point) 'gnus-data))
5123          (name (or
5124                 ;; Content-Type: foo/bar; name=...
5125                 (mail-content-type-get (mm-handle-type handle) 'name)
5126                 ;; Content-Disposition: attachment; filename=...
5127                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5128          (def-type (and name (mm-default-file-encoding name))))
5129     (or (and def-type (cons def-type 0))
5130         (and handle
5131              (equal (mm-handle-media-supertype handle) "text")
5132              '("text/plain" . 0))
5133         '("application/octet-stream" . 0))))
5134
5135 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5136   "Choose a MIME media type, and view the part as such.
5137 If non-nil, PRED is a predicate to use during completion to limit the
5138 available media-types."
5139   (interactive)
5140   (unless mime-type
5141     (setq mime-type
5142           (let ((default (gnus-mime-view-part-as-type-internal)))
5143             (completing-read
5144              (format "View as MIME type (default %s): "
5145                      (car default))
5146              (mapcar #'list (mailcap-mime-types))
5147              pred nil nil nil
5148              (car default)))))
5149   (gnus-article-check-buffer)
5150   (let ((handle (get-text-property (point) 'gnus-data)))
5151     (when handle
5152       (when (equal (mm-handle-media-type handle) "message/external-body")
5153         (unless (mm-handle-cache handle)
5154           (mm-extern-cache-contents handle))
5155         (setq handle (mm-handle-cache handle)))
5156       (setq handle
5157             (mm-make-handle (mm-handle-buffer handle)
5158                             (cons mime-type (cdr (mm-handle-type handle)))
5159                             (mm-handle-encoding handle)
5160                             (mm-handle-undisplayer handle)
5161                             (mm-handle-disposition handle)
5162                             (mm-handle-description handle)
5163                             nil
5164                             (mm-handle-id handle)))
5165       (setq gnus-article-mime-handles
5166             (mm-merge-handles gnus-article-mime-handles handle))
5167       (when (mm-handle-displayed-p handle)
5168         (mm-remove-part handle))
5169       (gnus-mm-display-part handle))))
5170
5171 (defun gnus-mime-copy-part (&optional handle arg)
5172   "Put the MIME part under point into a new buffer.
5173 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5174 are decompressed."
5175   (interactive (list nil current-prefix-arg))
5176   (gnus-article-check-buffer)
5177   (unless handle
5178     (setq handle (get-text-property (point) 'gnus-data)))
5179   (when handle
5180     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5181                                                'name)
5182                         (mail-content-type-get (mm-handle-disposition handle)
5183                                                'filename)))
5184           contents dont-decode charset coding-system)
5185       (mm-with-unibyte-buffer
5186         (mm-insert-part handle)
5187         (setq contents (or (condition-case nil
5188                                (mm-decompress-buffer filename nil 'sig)
5189                              (error
5190                               (setq dont-decode t)
5191                               nil))
5192                            (buffer-string))))
5193       (setq filename (cond (filename (file-name-nondirectory filename))
5194                            (dont-decode "*raw data*")
5195                            (t "*decoded*")))
5196       (cond
5197        (dont-decode)
5198        ((not arg)
5199         (unless (setq charset (mail-content-type-get
5200                                (mm-handle-type handle) 'charset))
5201           (unless (setq coding-system (mm-with-unibyte-buffer
5202                                         (insert contents)
5203                                         (mm-find-buffer-file-coding-system)))
5204             (setq charset gnus-newsgroup-charset))))
5205        ((numberp arg)
5206         (setq charset (or (cdr (assq arg
5207                                      gnus-summary-show-article-charset-alist))
5208                           (mm-read-coding-system "Charset: ")))))
5209       (switch-to-buffer (generate-new-buffer filename))
5210       (if (or coding-system
5211               (and charset
5212                    (setq coding-system (mm-charset-to-coding-system charset))
5213                    (not (eq charset 'ascii))))
5214           (progn
5215             (mm-enable-multibyte)
5216             (insert (mm-decode-coding-string contents coding-system))
5217             (setq buffer-file-coding-system
5218                   (if (boundp 'last-coding-system-used)
5219                       (symbol-value 'last-coding-system-used)
5220                     coding-system)))
5221         (mm-disable-multibyte)
5222         (insert contents)
5223         (setq buffer-file-coding-system mm-binary-coding-system))
5224       ;; We do it this way to make `normal-mode' set the appropriate mode.
5225       (unwind-protect
5226           (progn
5227             (setq buffer-file-name (expand-file-name filename))
5228             (normal-mode))
5229         (setq buffer-file-name nil))
5230       (goto-char (point-min)))))
5231
5232 (defun gnus-mime-print-part (&optional handle filename)
5233   "Print the MIME part under point."
5234   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5235   (gnus-article-check-buffer)
5236   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5237          (contents (and handle (mm-get-part handle)))
5238          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5239          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5240     (when contents
5241         (if printer
5242             (unwind-protect
5243                 (progn
5244                   (mm-save-part-to-file handle file)
5245                   (call-process shell-file-name nil
5246                                 (generate-new-buffer " *mm*")
5247                                 nil
5248                                 shell-command-switch
5249                                 (mm-mailcap-command
5250                                  printer file (mm-handle-type handle))))
5251               (delete-file file))
5252           (with-temp-buffer
5253             (insert contents)
5254             (gnus-print-buffer))
5255           (ps-despool filename)))))
5256
5257 (defun gnus-mime-inline-part (&optional handle arg)
5258   "Insert the MIME part under point into the current buffer.
5259 Compressed files like .gz and .bz2 are decompressed."
5260   (interactive (list nil current-prefix-arg))
5261   (gnus-article-check-buffer)
5262   (unless handle
5263     (setq handle (get-text-property (point) 'gnus-data)))
5264   (when handle
5265     (let ((b (point))
5266           (inhibit-read-only t)
5267           contents charset coding-system)
5268       (if (and (not arg) (mm-handle-undisplayer handle))
5269           (mm-remove-part handle)
5270         (mm-with-unibyte-buffer
5271           (mm-insert-part handle)
5272           (setq contents
5273                 (or (mm-decompress-buffer
5274                      (or (mail-content-type-get (mm-handle-type handle)
5275                                                 'name)
5276                          (mail-content-type-get (mm-handle-disposition handle)
5277                                                 'filename))
5278                      nil t)
5279                     (buffer-string))))
5280         (cond
5281          ((not arg)
5282           (unless (setq charset (mail-content-type-get
5283                                  (mm-handle-type handle) 'charset))
5284             (unless (setq coding-system
5285                           (mm-with-unibyte-buffer
5286                             (insert contents)
5287                             (mm-find-buffer-file-coding-system)))
5288               (setq charset gnus-newsgroup-charset))))
5289          ((numberp arg)
5290           (if (mm-handle-undisplayer handle)
5291               (mm-remove-part handle))
5292           (setq charset
5293                 (or (cdr (assq arg
5294                                gnus-summary-show-article-charset-alist))
5295                     (mm-read-coding-system "Charset: "))))
5296          (t
5297           (if (mm-handle-undisplayer handle)
5298               (mm-remove-part handle))))
5299         (forward-line 2)
5300         (mm-insert-inline
5301          handle
5302          (if (or coding-system
5303                  (and charset
5304                       (setq coding-system
5305                             (mm-charset-to-coding-system charset))
5306                       (not (eq coding-system 'ascii))))
5307              (mm-decode-coding-string contents coding-system)
5308            (mm-string-to-multibyte contents)))
5309         (goto-char b)))))
5310
5311 (defun gnus-mime-set-charset-parameters (handle charset)
5312   "Set CHARSET to parameters in HANDLE.
5313 CHARSET may either be a string or a symbol."
5314   (unless (stringp charset)
5315     (setq charset (symbol-name charset)))
5316   (if (stringp (car handle))
5317       (dolist (h (cdr handle))
5318         (gnus-mime-set-charset-parameters h charset))
5319     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5320                                             "message/external-body")
5321                                      (progn
5322                                        (unless (mm-handle-cache handle)
5323                                          (mm-extern-cache-contents handle))
5324                                        (mm-handle-cache handle))
5325                                    handle)))
5326            (param (assq 'charset (cdr type))))
5327       (if param
5328           (setcdr param charset)
5329         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5330
5331 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5332   "Insert the MIME part under point into the current buffer using the
5333 specified charset."
5334   (interactive (list nil current-prefix-arg))
5335   (gnus-article-check-buffer)
5336   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5337         (fun (get-text-property (point) 'gnus-callback))
5338         (gnus-newsgroup-ignored-charsets 'gnus-all)
5339         charset form preferred parts)
5340     (when handle
5341       (when (prog1
5342                 (and fun
5343                      (setq charset
5344                            (or (cdr (assq
5345                                      arg
5346                                      gnus-summary-show-article-charset-alist))
5347                                (mm-read-coding-system "Charset: "))))
5348               (if (mm-handle-undisplayer handle)
5349                   (mm-remove-part handle)))
5350         (gnus-mime-set-charset-parameters handle charset)
5351         (when (and (consp (setq form (cdr-safe fun)))
5352                    (setq form (ignore-errors
5353                                 (assq 'gnus-mime-display-alternative form)))
5354                    (setq preferred (caddr form))
5355                    (progn
5356                      (when (eq (car preferred) 'quote)
5357                        (setq preferred (cadr preferred)))
5358                      (not (equal preferred
5359                                  (get-text-property (point) 'gnus-data))))
5360                    (setq parts (get-text-property (point) 'gnus-part))
5361                    (setq parts (cdr (assq parts
5362                                           gnus-article-mime-handle-alist)))
5363                    (equal (mm-handle-media-type parts) "multipart/alternative")
5364                    (setq parts (reverse (cdr parts))))
5365           (setcar (cddr form)
5366                   (list 'quote (or (cadr (member preferred parts))
5367                                    (car parts)))))
5368         (funcall fun handle)))))
5369
5370 (defun gnus-mime-view-part-externally (&optional handle)
5371   "View the MIME part under point with an external viewer."
5372   (interactive)
5373   (gnus-article-check-buffer)
5374   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5375          (mm-user-display-methods nil)
5376          (mm-inlined-types nil)
5377          (mail-parse-charset gnus-newsgroup-charset)
5378          (mail-parse-ignored-charsets
5379           (with-current-buffer gnus-summary-buffer
5380             gnus-newsgroup-ignored-charsets))
5381          (type (mm-handle-media-type handle))
5382          (method (mailcap-mime-info type))
5383          (mm-enable-external t))
5384     (if (not (stringp method))
5385         (gnus-mime-view-part-as-type
5386          nil (lambda (types) (stringp (mailcap-mime-info (car types)))))
5387       (when handle
5388         (if (mm-handle-undisplayer handle)
5389             (mm-remove-part handle)
5390           (mm-display-part handle))))))
5391
5392 (defun gnus-mime-view-part-internally (&optional handle)
5393   "View the MIME part under point with an internal viewer.
5394 If no internal viewer is available, use an external viewer."
5395   (interactive)
5396   (gnus-article-check-buffer)
5397   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5398          (mm-inlined-types '(".*"))
5399          (mm-inline-large-images t)
5400          (mail-parse-charset gnus-newsgroup-charset)
5401          (mail-parse-ignored-charsets
5402           (with-current-buffer gnus-summary-buffer
5403             gnus-newsgroup-ignored-charsets))
5404          (inhibit-read-only t))
5405     (if (not (mm-inlinable-p handle))
5406         (gnus-mime-view-part-as-type
5407          nil (lambda (types) (mm-inlinable-p handle (car types))))
5408       (when handle
5409         (if (mm-handle-undisplayer handle)
5410             (mm-remove-part handle)
5411           (gnus-bind-safe-url-regexp (mm-display-part handle)))))))
5412
5413 (defun gnus-mime-action-on-part (&optional action)
5414   "Do something with the MIME attachment at \(point\)."
5415   (interactive
5416    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
5417   (gnus-article-check-buffer)
5418   (let ((action-pair (assoc action gnus-mime-action-alist)))
5419     (if action-pair
5420         (funcall (cdr action-pair)))))
5421
5422 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5423   "Call FUNCTION on MIME part N.
5424 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5425 If INTERACTIVE, call FUNCTION interactivly."
5426   (let (window frame)
5427     ;; Check whether the article is displayed.
5428     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5429                  (setq window (get-buffer-window gnus-article-buffer t))
5430                  (frame-visible-p (setq frame (window-frame window))))
5431       (error "No article is displayed"))
5432     (with-current-buffer gnus-article-buffer
5433       ;; Check whether the article displays the right contents.
5434       (unless (with-current-buffer gnus-summary-buffer
5435                 (eq gnus-current-article (gnus-summary-article-number)))
5436         (error "You should select the right article first"))
5437       (if n
5438           (setq n (prefix-numeric-value n))
5439         (let ((pt (point)))
5440           (setq n (or (get-text-property pt 'gnus-part)
5441                       (and (not (bobp))
5442                            (get-text-property (1- pt) 'gnus-part))
5443                       (get-text-property (prog2
5444                                              (forward-line 1)
5445                                              (point)
5446                                            (goto-char pt))
5447                                          'gnus-part)
5448                       (get-text-property
5449                        (or (and (setq pt (previous-single-property-change
5450                                           pt 'gnus-part))
5451                                 (1- pt))
5452                            (next-single-property-change (point) 'gnus-part)
5453                            (point))
5454                        'gnus-part)
5455                       1))))
5456       ;; Check whether the specified part exists.
5457       (when (> n (length gnus-article-mime-handle-alist))
5458         (error "No such part")))
5459     (unless
5460         (progn
5461           ;; To select the window is needed so that the cursor
5462           ;; might be visible on the MIME button.
5463           (select-window (prog1
5464                              window
5465                            (setq window (selected-window))
5466                            ;; Article may be displayed in the other frame.
5467                            (gnus-select-frame-set-input-focus
5468                             (prog1
5469                                 frame
5470                               (setq frame (selected-frame))))))
5471           (when (gnus-article-goto-part n)
5472             ;; We point the cursor and the arrow at the MIME button
5473             ;; when the `function' prompt the user for something.
5474             (let ((cursor-in-non-selected-windows t)
5475                   (overlay-arrow-string "=>")
5476                   (overlay-arrow-position (point-marker)))
5477               (unwind-protect
5478                   (cond
5479                    ((and no-handle interactive)
5480                     (call-interactively function))
5481                    (no-handle
5482                     (funcall function))
5483                    (interactive
5484                     (call-interactively
5485                      function
5486                      (cdr (assq n gnus-article-mime-handle-alist))))
5487                    (t
5488                     (funcall function
5489                              (cdr (assq n gnus-article-mime-handle-alist)))))
5490                 (set-marker overlay-arrow-position nil)
5491                 (unless gnus-auto-select-part
5492                   (gnus-select-frame-set-input-focus frame)
5493                   (select-window window))))
5494             t))
5495       (if gnus-inhibit-mime-unbuttonizing
5496           ;; This is the default though the program shouldn't reach here.
5497           (error "No such part")
5498         ;; The part which doesn't have the MIME button is selected.
5499         ;; So, we display all the buttons and redo it.
5500         (let ((gnus-inhibit-mime-unbuttonizing t))
5501           (gnus-summary-show-article)
5502           (gnus-article-part-wrapper n function no-handle))))))
5503
5504 (defun gnus-article-pipe-part (n)
5505   "Pipe MIME part N, which is the numerical prefix."
5506   (interactive "P")
5507   (gnus-article-part-wrapper n 'mm-pipe-part))
5508
5509 (defun gnus-article-save-part (n)
5510   "Save MIME part N, which is the numerical prefix."
5511   (interactive "P")
5512   (gnus-article-part-wrapper n 'mm-save-part))
5513
5514 (defun gnus-article-interactively-view-part (n)
5515   "View MIME part N interactively, which is the numerical prefix."
5516   (interactive "P")
5517   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5518
5519 (defun gnus-article-copy-part (n)
5520   "Copy MIME part N, which is the numerical prefix."
5521   (interactive "P")
5522   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5523
5524 (defun gnus-article-view-part-as-charset (n)
5525   "View MIME part N using a specified charset.
5526 N is the numerical prefix."
5527   (interactive "P")
5528   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5529
5530 (defun gnus-article-view-part-externally (n)
5531   "View MIME part N externally, which is the numerical prefix."
5532   (interactive "P")
5533   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5534
5535 (defun gnus-article-inline-part (n)
5536   "Inline MIME part N, which is the numerical prefix."
5537   (interactive "P")
5538   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5539
5540 (defun gnus-article-save-part-and-strip (n)
5541   "Save MIME part N and replace it with an external body.
5542 N is the numerical prefix."
5543   (interactive "P")
5544   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5545
5546 (defun gnus-article-replace-part (n)
5547   "Replace MIME part N with an external body.
5548 N is the numerical prefix."
5549   (interactive "P")
5550   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5551
5552 (defun gnus-article-delete-part (n)
5553   "Delete MIME part N and add some information about the removed part.
5554 N is the numerical prefix."
5555   (interactive "P")
5556   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5557
5558 (defun gnus-article-view-part-as-type (n)
5559   "Choose a MIME media type, and view part N as such.
5560 N is the numerical prefix."
5561   (interactive "P")
5562   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5563
5564 (defun gnus-article-mime-match-handle-first (condition)
5565   (if condition
5566       (let (n)
5567         (dolist (ihandle gnus-article-mime-handle-alist)
5568           (if (and (cond
5569                     ((functionp condition)
5570                      (funcall condition (cdr ihandle)))
5571                     ((eq condition 'undisplayed)
5572                      (not (or (mm-handle-undisplayer (cdr ihandle))
5573                               (equal (mm-handle-media-type (cdr ihandle))
5574                                      "multipart/alternative"))))
5575                     ((eq condition 'undisplayed-alternative)
5576                      (not (mm-handle-undisplayer (cdr ihandle))))
5577                     (t t))
5578                    (gnus-article-goto-part (car ihandle))
5579                    (or (not n) (< (car ihandle) n)))
5580               (setq n (car ihandle))))
5581         (or n 1))
5582     1))
5583
5584 (defun gnus-article-view-part (&optional n)
5585   "View MIME part N, which is the numerical prefix.
5586 If the part is already shown, hide the part.  If N is nil, view
5587 all parts."
5588   (interactive "P")
5589   (with-current-buffer gnus-article-buffer
5590     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5591                              gnus-article-mime-match-handle-function)))
5592     (when (> n (length gnus-article-mime-handle-alist))
5593       (error "No such part"))
5594     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5595       (when (gnus-article-goto-part n)
5596         (if (equal (car handle) "multipart/alternative")
5597             (gnus-article-press-button)
5598           (when (eq (gnus-mm-display-part handle) 'internal)
5599             (gnus-set-window-start)))))))
5600
5601 (defsubst gnus-article-mime-total-parts ()
5602   (if (bufferp (car gnus-article-mime-handles))
5603       1 ;; single part
5604     (1- (length gnus-article-mime-handles))))
5605
5606 (defun gnus-mm-display-part (handle)
5607   "Display HANDLE and fix MIME button."
5608   (let ((id (get-text-property (point) 'gnus-part))
5609         (point (point))
5610         (inhibit-read-only t))
5611     (forward-line 1)
5612     (prog1
5613         (let ((window (selected-window))
5614               (mail-parse-charset gnus-newsgroup-charset)
5615               (mail-parse-ignored-charsets
5616                (if (gnus-buffer-live-p gnus-summary-buffer)
5617                    (with-current-buffer gnus-summary-buffer
5618                      gnus-newsgroup-ignored-charsets)
5619                  nil)))
5620           (save-excursion
5621             (unwind-protect
5622                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5623                       (beg (point)))
5624                   (when win
5625                     (select-window win))
5626                   (goto-char point)
5627                   (forward-line)
5628                   (if (mm-handle-displayed-p handle)
5629                       ;; This will remove the part.
5630                       (mm-display-part handle)
5631                     (save-restriction
5632                       (narrow-to-region (point)
5633                                         (if (eobp) (point) (1+ (point))))
5634                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5635                       ;; We narrow to the part itself and
5636                       ;; then call the treatment functions.
5637                       (goto-char (point-min))
5638                       (forward-line 1)
5639                       (narrow-to-region (point) (point-max))
5640                       (gnus-treat-article
5641                        nil id
5642                        (gnus-article-mime-total-parts)
5643                        (mm-handle-media-type handle)))))
5644               (if (window-live-p window)
5645                   (select-window window)))))
5646       (goto-char point)
5647       (gnus-delete-line)
5648       (gnus-insert-mime-button
5649        handle id (list (mm-handle-displayed-p handle)))
5650       (goto-char point))))
5651
5652 (defun gnus-article-goto-part (n)
5653   "Go to MIME part N."
5654   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
5655
5656 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5657   (let ((gnus-tmp-name
5658          (or (mail-content-type-get (mm-handle-type handle) 'name)
5659              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5660              (mail-content-type-get (mm-handle-type handle) 'url)
5661              ""))
5662         (gnus-tmp-type (mm-handle-media-type handle))
5663         (gnus-tmp-description (or (mm-handle-description handle) ""))
5664         (gnus-tmp-dots
5665          (if (if displayed (car displayed)
5666                (mm-handle-displayed-p handle))
5667              "" "..."))
5668         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5669                            (buffer-size)))
5670         gnus-tmp-type-long b e)
5671     (when (string-match ".*/" gnus-tmp-name)
5672       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5673     (setq gnus-tmp-type-long (concat gnus-tmp-type
5674                                      (and (not (equal gnus-tmp-name ""))
5675                                           (concat "; " gnus-tmp-name))))
5676     (unless (equal gnus-tmp-description "")
5677       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5678     (unless (bolp)
5679       (insert "\n"))
5680     (setq b (point))
5681     (gnus-eval-format
5682      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5683      `(keymap ,gnus-mime-button-map
5684               gnus-callback gnus-mm-display-part
5685               gnus-part ,gnus-tmp-id
5686               article-type annotation
5687               gnus-data ,handle))
5688     (setq e (if (bolp)
5689                 ;; Exclude a newline.
5690                 (1- (point))
5691               (point)))
5692     (when gnus-article-button-face
5693       (gnus-overlay-put (gnus-make-overlay b e nil t)
5694                         'face gnus-article-button-face))
5695     (widget-convert-button
5696      'link b e
5697      :mime-handle handle
5698      :action 'gnus-widget-press-button
5699      :button-keymap gnus-mime-button-map
5700      :help-echo
5701      (lambda (widget)
5702        ;; Needed to properly clear the message due to a bug in
5703        ;; wid-edit (XEmacs only).
5704        (if (boundp 'help-echo-owns-message)
5705            (setq help-echo-owns-message t))
5706        (format
5707         "%S: %s the MIME part; %S: more options"
5708         (aref gnus-mouse-2 0)
5709         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5710             "hide" "show")
5711         (aref gnus-down-mouse-3 0))))))
5712
5713 (defun gnus-widget-press-button (elems el)
5714   (goto-char (widget-get elems :from))
5715   (gnus-article-press-button))
5716
5717 (defvar gnus-displaying-mime nil)
5718
5719 (defun gnus-display-mime (&optional ihandles)
5720   "Display the MIME parts."
5721   (save-excursion
5722     (save-selected-window
5723       (let ((window (get-buffer-window gnus-article-buffer))
5724             (point (point)))
5725         (when window
5726           (select-window window)
5727           ;; We have to do this since selecting the window
5728           ;; may change the point.  So we set the window point.
5729           (set-window-point window point)))
5730       (let ((handles ihandles)
5731             (inhibit-read-only t)
5732             handle)
5733         (cond (handles)
5734               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5735                (when gnus-article-emulate-mime
5736                  (mm-uu-dissect-text-parts handles)))
5737               (gnus-article-emulate-mime
5738                (setq handles (mm-uu-dissect))))
5739         (when (and (not ihandles)
5740                    (not gnus-displaying-mime))
5741           ;; Top-level call; we clean up.
5742           (when gnus-article-mime-handles
5743             (mm-destroy-parts gnus-article-mime-handles)
5744             (setq gnus-article-mime-handle-alist nil));; A trick.
5745           (setq gnus-article-mime-handles handles)
5746           ;; We allow users to glean info from the handles.
5747           (when gnus-article-mime-part-function
5748             (gnus-mime-part-function handles)))
5749         (if (and handles
5750                  (or (not (stringp (car handles)))
5751                      (cdr handles)))
5752             (progn
5753               (when (and (not ihandles)
5754                          (not gnus-displaying-mime))
5755                 ;; Clean up for mime parts.
5756                 (article-goto-body)
5757                 (delete-region (point) (point-max)))
5758               (let ((gnus-displaying-mime t))
5759                 (gnus-mime-display-part handles)))
5760           (save-restriction
5761             (article-goto-body)
5762             (narrow-to-region (point) (point-max))
5763             (gnus-treat-article nil 1 1)
5764             (widen)))
5765         (unless ihandles
5766           ;; Highlight the headers.
5767           (save-excursion
5768             (save-restriction
5769               (article-goto-body)
5770               (narrow-to-region (point-min) (point))
5771               (gnus-article-save-original-date
5772                (gnus-treat-article 'head)))))))
5773     ;; Cope with broken MIME messages.
5774     (goto-char (point-max))
5775     (unless (bolp)
5776       (insert "\n"))))
5777
5778 (defcustom gnus-mime-display-multipart-as-mixed nil
5779   "Display \"multipart\" parts as  \"multipart/mixed\".
5780
5781 If t, it overrides nil values of
5782 `gnus-mime-display-multipart-alternative-as-mixed' and
5783 `gnus-mime-display-multipart-related-as-mixed'."
5784   :group 'gnus-article-mime
5785   :type 'boolean)
5786
5787 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5788   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5789   :version "22.1"
5790   :group 'gnus-article-mime
5791   :type 'boolean)
5792
5793 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5794   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5795
5796 If displaying \"text/html\" is discouraged \(see
5797 `mm-discouraged-alternatives'\) images or other material inside a
5798 \"multipart/related\" part might be overlooked when this variable is nil."
5799   :version "22.1"
5800   :group 'gnus-article-mime
5801   :type 'boolean)
5802
5803 (defun gnus-mime-display-part (handle)
5804   (cond
5805    ;; Maybe a broken MIME message.
5806    ((null handle))
5807    ;; Single part.
5808    ((not (stringp (car handle)))
5809     (gnus-mime-display-single handle))
5810    ;; User-defined multipart
5811    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5812     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5813              handle))
5814    ;; multipart/alternative
5815    ((and (equal (car handle) "multipart/alternative")
5816          (not (or gnus-mime-display-multipart-as-mixed
5817                   gnus-mime-display-multipart-alternative-as-mixed)))
5818     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5819       (push (cons id handle) gnus-article-mime-handle-alist)
5820       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5821    ;; multipart/related
5822    ((and (equal (car handle) "multipart/related")
5823          (not (or gnus-mime-display-multipart-as-mixed
5824                   gnus-mime-display-multipart-related-as-mixed)))
5825     ;;;!!!We should find the start part, but we just default
5826     ;;;!!!to the first part.
5827     ;;(gnus-mime-display-part (cadr handle))
5828     ;;;!!! Most multipart/related is an HTML message plus images.
5829     ;;;!!! Unfortunately we are unable to let W3 display those
5830     ;;;!!! included images, so we just display it as a mixed multipart.
5831     ;;(gnus-mime-display-mixed (cdr handle))
5832     ;;;!!! No, w3 can display everything just fine.
5833     (gnus-mime-display-part (cadr handle)))
5834    ((equal (car handle) "multipart/signed")
5835     (gnus-add-wash-type 'signed)
5836     (gnus-mime-display-security handle))
5837    ((equal (car handle) "multipart/encrypted")
5838     (gnus-add-wash-type 'encrypted)
5839     (gnus-mime-display-security handle))
5840    ;; Other multiparts are handled like multipart/mixed.
5841    (t
5842     (gnus-mime-display-mixed (cdr handle)))))
5843
5844 (defun gnus-mime-part-function (handles)
5845   (if (stringp (car handles))
5846       (mapcar 'gnus-mime-part-function (cdr handles))
5847     (funcall gnus-article-mime-part-function handles)))
5848
5849 (defun gnus-mime-display-mixed (handles)
5850   (mapcar 'gnus-mime-display-part handles))
5851
5852 (defun gnus-mime-display-single (handle)
5853   (let ((type (mm-handle-media-type handle))
5854         (ignored gnus-ignored-mime-types)
5855         (not-attachment t)
5856         (move nil)
5857         display text)
5858     (catch 'ignored
5859       (progn
5860         (while ignored
5861           (when (string-match (pop ignored) type)
5862             (throw 'ignored nil)))
5863         (if (and (setq not-attachment
5864                        (and (not (mm-inline-override-p handle))
5865                             (or (not (mm-handle-disposition handle))
5866                                 (equal (car (mm-handle-disposition handle))
5867                                        "inline")
5868                                 (mm-attachment-override-p handle))))
5869                  (mm-automatic-display-p handle)
5870                  (or (and
5871                       (mm-inlinable-p handle)
5872                       (mm-inlined-p handle))
5873                      (mm-automatic-external-display-p type)))
5874             (setq display t)
5875           (when (equal (mm-handle-media-supertype handle) "text")
5876             (setq text t)))
5877         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5878               beg)
5879           (push (cons id handle) gnus-article-mime-handle-alist)
5880           (when (and display
5881                      (equal (mm-handle-media-supertype handle) "message"))
5882             (insert-char
5883              ?\n
5884              (cond ((not (bolp)) 2)
5885                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5886                    (t 1))))
5887           (when (or (not display)
5888                     (not (gnus-unbuttonized-mime-type-p type))
5889                     (eq id gnus-mime-buttonized-part-id))
5890             (gnus-insert-mime-button
5891              handle id (list (or display (and not-attachment text))))
5892             (gnus-article-insert-newline)
5893             ;; Remember modify the number of forward lines.
5894             (setq move t))
5895           (setq beg (point))
5896           (cond
5897            (display
5898             (when move
5899               (forward-line -1)
5900               (setq beg (point)))
5901             (let ((mail-parse-charset gnus-newsgroup-charset)
5902                   (mail-parse-ignored-charsets
5903                    (save-excursion (condition-case ()
5904                                        (set-buffer gnus-summary-buffer)
5905                                      (error))
5906                                    gnus-newsgroup-ignored-charsets)))
5907               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5908             (goto-char (point-max)))
5909            ((and text not-attachment)
5910             (when move
5911               (forward-line -1)
5912               (setq beg (point)))
5913             (gnus-article-insert-newline)
5914             (mm-insert-inline
5915              handle
5916              (let ((charset (or (mail-content-type-get (mm-handle-type handle)
5917                                                        'charset)
5918                                 (and (equal type "text/calendar") 'utf-8))))
5919                (cond ((not charset)
5920                       (mm-string-as-multibyte (mm-get-part handle)))
5921                      ((eq charset 'gnus-decoded)
5922                       (with-current-buffer (mm-handle-buffer handle)
5923                         (buffer-string)))
5924                      (t
5925                       (mm-decode-string (mm-get-part handle) charset)))))
5926             (goto-char (point-max))))
5927           ;; Do highlighting.
5928           (save-excursion
5929             (save-restriction
5930               (narrow-to-region beg (point))
5931               (if (eq handle gnus-article-mime-handles)
5932                   ;; The format=flowed case.
5933                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5934                 ;; Don't count signature parts that are never displayed.
5935                 ;; The part number should be re-calculated supposing this
5936                 ;; might be a message/rfc822 part.
5937                 (let (handles)
5938                   (dolist (part gnus-article-mime-handles)
5939                     (unless (or (stringp part)
5940                                 (equal (car (mm-handle-type part))
5941                                        "application/pgp-signature"))
5942                       (push part handles)))
5943                   (gnus-treat-article
5944                    nil (length (memq handle handles)) (length handles)
5945                    (mm-handle-media-type handle)))))))))))
5946
5947 (defun gnus-unbuttonized-mime-type-p (type)
5948   "Say whether TYPE is to be unbuttonized."
5949   (unless gnus-inhibit-mime-unbuttonizing
5950     (when (catch 'found
5951             (let ((types gnus-unbuttonized-mime-types))
5952               (while types
5953                 (when (string-match (pop types) type)
5954                   (throw 'found t)))))
5955       (not (catch 'found
5956              (let ((types gnus-buttonized-mime-types))
5957                (while types
5958                  (when (string-match (pop types) type)
5959                    (throw 'found t)))))))))
5960
5961 (defun gnus-article-insert-newline ()
5962   "Insert a newline, but mark it as undeletable."
5963   (gnus-put-text-property
5964    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5965
5966 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5967   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5968          (ihandles handles)
5969          (point (point))
5970          handle (inhibit-read-only t) from props begend not-pref)
5971     (save-window-excursion
5972       (save-restriction
5973         (when ibegend
5974           (narrow-to-region (car ibegend)
5975                             (or (cdr ibegend)
5976                                 (progn
5977                                   (goto-char (car ibegend))
5978                                   (forward-line 2)
5979                                   (point))))
5980           (delete-region (point-min) (point-max))
5981           (mm-remove-parts handles))
5982         (setq begend (list (point-marker)))
5983         ;; Do the toggle.
5984         (unless (setq not-pref (cadr (member preferred ihandles)))
5985           (setq not-pref (car ihandles)))
5986         (when (or ibegend
5987                   (not preferred)
5988                   (not (gnus-unbuttonized-mime-type-p
5989                         "multipart/alternative")))
5990           (gnus-add-text-properties
5991            (setq from (point))
5992            (progn
5993              (insert (format "%d.  " id))
5994              (point))
5995            `(gnus-callback
5996              (lambda (handles)
5997                (unless ,(not ibegend)
5998                  (setq gnus-article-mime-handle-alist
5999                        ',gnus-article-mime-handle-alist))
6000                (gnus-mime-display-alternative
6001                 ',ihandles ',not-pref ',begend ,id))
6002              keymap ,gnus-mime-button-map
6003              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6004              face ,gnus-article-button-face
6005              gnus-part ,id
6006              article-type multipart))
6007           (widget-convert-button 'link from (point)
6008                                  :action 'gnus-widget-press-button
6009                                  :button-keymap gnus-widget-button-keymap)
6010           ;; Do the handles
6011           (while (setq handle (pop handles))
6012             (gnus-add-text-properties
6013              (setq from (point))
6014              (progn
6015                (insert (format "(%c) %-18s"
6016                                (if (equal handle preferred) ?* ? )
6017                                (mm-handle-media-type handle)))
6018                (point))
6019              `(gnus-callback
6020                (lambda (handles)
6021                  (unless ,(not ibegend)
6022                    (setq gnus-article-mime-handle-alist
6023                          ',gnus-article-mime-handle-alist))
6024                  (gnus-mime-display-alternative
6025                   ',ihandles ',handle ',begend ,id))
6026                keymap ,gnus-mime-button-map
6027                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6028                face ,gnus-article-button-face
6029                gnus-part ,id
6030                gnus-data ,handle))
6031             (widget-convert-button 'link from (point)
6032                                    :action 'gnus-widget-press-button
6033                                    :button-keymap gnus-widget-button-keymap)
6034             (insert "  "))
6035           (insert "\n\n"))
6036         (when preferred
6037           (if (stringp (car preferred))
6038               (gnus-display-mime preferred)
6039             (let ((mail-parse-charset gnus-newsgroup-charset)
6040                   (mail-parse-ignored-charsets
6041                    (with-current-buffer gnus-summary-buffer
6042                      gnus-newsgroup-ignored-charsets)))
6043               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6044               ;; Do highlighting.
6045               (save-excursion
6046                 (save-restriction
6047                   (narrow-to-region (car begend) (point-max))
6048                   (gnus-treat-article
6049                    nil (length gnus-article-mime-handle-alist)
6050                    (gnus-article-mime-total-parts)
6051                    (mm-handle-media-type handle))))))
6052           (goto-char (point-max))
6053           (setcdr begend (point-marker)))))
6054     (when ibegend
6055       (goto-char point))))
6056
6057 (defconst gnus-article-wash-status-strings
6058   (let ((alist '((cite "c" "Possible hidden citation text"
6059                        " " "All citation text visible")
6060                  (headers "h" "Hidden headers"
6061                           " " "All headers visible.")
6062                  (pgp "p" "Encrypted or signed message status hidden"
6063                       " " "No hidden encryption nor digital signature status")
6064                  (signature "s" "Signature has been hidden"
6065                             " " "Signature is visible")
6066                  (overstrike "o" "Overstrike (^H) characters applied"
6067                              " " "No overstrike characters applied")
6068                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6069                            " " "No /*_emphasis_*/ characters applied")))
6070         result)
6071     (dolist (entry alist result)
6072       (let ((key (nth 0 entry))
6073             (on (copy-sequence (nth 1 entry)))
6074             (on-help (nth 2 entry))
6075             (off (copy-sequence (nth 3 entry)))
6076             (off-help (nth 4 entry)))
6077         (put-text-property 0 1 'help-echo on-help on)
6078         (put-text-property 0 1 'help-echo off-help off)
6079         (push (list key on off) result))))
6080   "Alist of strings describing wash status in the mode line.
6081 Each entry has the form (KEY ON OF), where the KEY is a symbol
6082 representing the particular washing function, ON is the string to use
6083 in the article mode line when the washing function is active, and OFF
6084 is the string to use when it is inactive.")
6085
6086 (defun gnus-article-wash-status-entry (key value)
6087   (let ((entry (assoc key gnus-article-wash-status-strings)))
6088     (if value (nth 1 entry) (nth 2 entry))))
6089
6090 (defun gnus-article-wash-status ()
6091   "Return a string which display status of article washing."
6092   (with-current-buffer gnus-article-buffer
6093     (let ((cite (memq 'cite gnus-article-wash-types))
6094           (headers (memq 'headers gnus-article-wash-types))
6095           (boring (memq 'boring-headers gnus-article-wash-types))
6096           (pgp (memq 'pgp gnus-article-wash-types))
6097           (pem (memq 'pem gnus-article-wash-types))
6098           (signed (memq 'signed gnus-article-wash-types))
6099           (encrypted (memq 'encrypted gnus-article-wash-types))
6100           (signature (memq 'signature gnus-article-wash-types))
6101           (overstrike (memq 'overstrike gnus-article-wash-types))
6102           (emphasis (memq 'emphasis gnus-article-wash-types)))
6103       (concat
6104        (gnus-article-wash-status-entry 'cite cite)
6105        (gnus-article-wash-status-entry 'headers (or headers boring))
6106        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6107        (gnus-article-wash-status-entry 'signature signature)
6108        (gnus-article-wash-status-entry 'overstrike overstrike)
6109        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6110
6111 (defun gnus-add-wash-type (type)
6112   "Add a washing of TYPE to the current status."
6113   (add-to-list 'gnus-article-wash-types type))
6114
6115 (defun gnus-delete-wash-type (type)
6116   "Add a washing of TYPE to the current status."
6117   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6118
6119 (defun gnus-add-image (category image)
6120   "Add IMAGE of CATEGORY to the list of displayed images."
6121   (let ((entry (assq category gnus-article-image-alist)))
6122     (unless entry
6123       (setq entry (list category))
6124       (push entry gnus-article-image-alist))
6125     (nconc entry (list image))))
6126
6127 (defun gnus-delete-images (category)
6128   "Delete all images in CATEGORY."
6129   (let ((entry (assq category gnus-article-image-alist)))
6130     (dolist (image (cdr entry))
6131       (gnus-remove-image image category))
6132     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6133     (gnus-delete-wash-type category)))
6134
6135 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6136
6137 (defun gnus-article-maybe-hide-headers ()
6138   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6139 Provided for backwards compatibility."
6140   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6141                  (not (with-current-buffer gnus-summary-buffer
6142                         gnus-have-all-headers)))
6143              (not gnus-inhibit-hiding))
6144     (gnus-article-hide-headers)))
6145
6146 ;;; Article savers.
6147
6148 (defun gnus-output-to-file (file-name)
6149   "Append the current article to a file named FILE-NAME.
6150 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6151 text and used as the value of the coding cookie which is added to the
6152 top of a file.  Otherwise, this function saves a raw article without
6153 the coding cookie."
6154   (let* ((artbuf (current-buffer))
6155          (file-name-coding-system nnmail-pathname-coding-system)
6156          (coding gnus-article-save-coding-system)
6157          (coding-system-for-read (if coding
6158                                      nil ;; Rely on the coding cookie.
6159                                    mm-text-coding-system))
6160          (coding-system-for-write (or coding
6161                                       mm-text-coding-system-for-write
6162                                       mm-text-coding-system))
6163          (exists (file-exists-p file-name)))
6164     (with-temp-buffer
6165       (when exists
6166         (insert-file-contents file-name)
6167         (goto-char (point-min))
6168         ;; Remove the existing coding cookie.
6169         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6170           (delete-region (match-beginning 0) (match-end 0))))
6171       (goto-char (point-max))
6172       (insert-buffer-substring artbuf)
6173       ;; Append newline at end of the buffer as separator, and then
6174       ;; save it to file.
6175       (goto-char (point-max))
6176       (insert "\n")
6177       (when coding
6178         ;; If the coding system is not suitable to encode the text,
6179         ;; ask a user for a proper one.
6180         (when (fboundp 'select-safe-coding-system)
6181           (setq coding (coding-system-base
6182                         (save-window-excursion
6183                           (select-safe-coding-system (point-min) (point-max)
6184                                                      coding))))
6185           (setq coding-system-for-write
6186                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6187                     coding)))
6188         (goto-char (point-min))
6189         ;; Add the coding cookie.
6190         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6191                         coding-system-for-write)))
6192       (if exists
6193           (progn
6194             (write-region (point-min) (point-max) file-name nil 'no-message)
6195             (message "Appended to %s" file-name))
6196         (write-region (point-min) (point-max) file-name))))
6197   t)
6198
6199 (defun gnus-narrow-to-page (&optional arg)
6200   "Narrow the article buffer to a page.
6201 If given a numerical ARG, move forward ARG pages."
6202   (interactive "P")
6203   (setq arg (if arg (prefix-numeric-value arg) 0))
6204   (with-current-buffer gnus-article-buffer
6205     (widen)
6206     ;; Remove any old next/prev buttons.
6207     (when (gnus-visual-p 'page-marker)
6208       (let ((inhibit-read-only t))
6209         (gnus-remove-text-with-property 'gnus-prev)
6210         (gnus-remove-text-with-property 'gnus-next)))
6211     (let (st nd pt)
6212       (when (save-excursion
6213               (cond ((< arg 0)
6214                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6215                          (prog1
6216                              (setq nd (match-beginning 0)
6217                                    pt nd)
6218                            (when (re-search-backward page-delimiter nil t)
6219                              (setq st (match-end 0))))
6220                        (when (re-search-forward page-delimiter nil t)
6221                          (setq nd (match-beginning 0)
6222                                pt (point-min)))))
6223                     ((> arg 0)
6224                      (if (re-search-forward page-delimiter nil 'move arg)
6225                          (prog1
6226                              (setq st (match-end 0)
6227                                    pt st)
6228                            (when (re-search-forward page-delimiter nil t)
6229                              (setq nd (match-beginning 0))))
6230                        (when (re-search-backward page-delimiter nil t)
6231                          (setq st (match-end 0)
6232                                pt (point-max)))))
6233                     (t
6234                      (when (re-search-backward page-delimiter nil t)
6235                        (goto-char (setq st (match-end 0))))
6236                      (when (re-search-forward page-delimiter nil t)
6237                        (setq nd (match-beginning 0)))
6238                      (or st nd))))
6239         (setq gnus-page-broken t)
6240         (when pt (goto-char pt))
6241         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6242         (when (gnus-visual-p 'page-marker)
6243           (save-excursion
6244             (when nd
6245               (goto-char nd)
6246               (gnus-insert-next-page-button))
6247             (when st
6248               (goto-char st)
6249               (gnus-insert-prev-page-button))))))))
6250
6251 ;; Article mode commands
6252
6253 (defun gnus-article-goto-next-page ()
6254   "Show the next page of the article."
6255   (interactive)
6256   (when (gnus-article-next-page)
6257     (goto-char (point-min))
6258     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6259
6260
6261 (defun gnus-article-goto-prev-page ()
6262   "Show the previous page of the article."
6263   (interactive)
6264   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6265       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6266     (gnus-article-prev-page nil)))
6267
6268 ;; This is cleaner but currently breaks `gnus-pick-mode':
6269 ;;
6270 ;; (defun gnus-article-goto-next-page ()
6271 ;;   "Show the next page of the article."
6272 ;;   (interactive)
6273 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6274 ;;     (gnus-summary-next-page)))
6275 ;;
6276 ;; (defun gnus-article-goto-prev-page ()
6277 ;;   "Show the next page of the article."
6278 ;;   (interactive)
6279 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6280 ;;     (gnus-summary-prev-page)))
6281
6282 (defun gnus-article-next-page (&optional lines)
6283   "Show the next page of the current article.
6284 If end of article, return non-nil.  Otherwise return nil.
6285 Argument LINES specifies lines to be scrolled up."
6286   (interactive "p")
6287   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6288   (if (and (not (and gnus-article-over-scroll
6289                      (> (count-lines (window-start) (point-max))
6290                         (if (featurep 'xemacs)
6291                             (or lines (1- (window-height)))
6292                           (+ (or lines (1- (window-height))) scroll-margin)))))
6293            (save-excursion
6294              (end-of-line)
6295              (and (pos-visible-in-window-p)     ;Not continuation line.
6296                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6297       ;; Nothing in this page.
6298       (if (or (not gnus-page-broken)
6299               (save-excursion
6300                 (save-restriction
6301                   (widen)
6302                   (forward-line)
6303                   (eobp)))) ;Real end-of-buffer?
6304           (progn
6305             (when gnus-article-over-scroll
6306               (gnus-article-next-page-1 lines))
6307             t)                  ;Nothing more.
6308         (gnus-narrow-to-page 1)         ;Go to next page.
6309         nil)
6310     ;; More in this page.
6311     (gnus-article-next-page-1 lines)
6312     nil))
6313
6314 (defun gnus-article-beginning-of-window ()
6315   "Move point to the beginning of the window.
6316 In Emacs, the point is placed at the line number which `scroll-margin'
6317 specifies."
6318   (if (featurep 'xemacs)
6319       (move-to-window-line 0)
6320     ;; There is an obscure bug in Emacs that makes it impossible to
6321     ;; scroll past big pictures in the article buffer.  Try to fix
6322     ;; this by adding a sanity check by counting the lines visible.
6323     (when (> (count-lines (window-start) (window-end)) 30)
6324       (move-to-window-line
6325        (min (max 0 scroll-margin)
6326             (max 1 (- (window-height)
6327                       (if mode-line-format 1 0)
6328                       (if header-line-format 1 0)
6329                       2)))))))
6330
6331 (defun gnus-article-next-page-1 (lines)
6332   (condition-case ()
6333       (let ((scroll-in-place nil))
6334         (scroll-up lines))
6335     (end-of-buffer
6336      ;; Long lines may cause an end-of-buffer error.
6337      (goto-char (point-max))))
6338   (gnus-article-beginning-of-window))
6339
6340 (defun gnus-article-prev-page (&optional lines)
6341   "Show previous page of current article.
6342 Argument LINES specifies lines to be scrolled down."
6343   (interactive "p")
6344   (move-to-window-line 0)
6345   (if (and gnus-page-broken
6346            (bobp)
6347            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6348       (progn
6349         (gnus-narrow-to-page -1)        ;Go to previous page.
6350         (goto-char (point-max))
6351         (recenter (if gnus-article-over-scroll
6352                       (if lines
6353                           (max (if (featurep 'xemacs)
6354                                    lines
6355                                  (+ lines scroll-margin))
6356                                3)
6357                         (- (window-height) 2))
6358                     -1)))
6359     (prog1
6360         (condition-case ()
6361             (let ((scroll-in-place nil))
6362               (scroll-down lines))
6363           (beginning-of-buffer
6364            (goto-char (point-min))))
6365       (gnus-article-beginning-of-window))))
6366
6367 (defun gnus-article-only-boring-p ()
6368   "Decide whether there is only boring text remaining in the article.
6369 Something \"interesting\" is a word of at least two letters that does
6370 not have a face in `gnus-article-boring-faces'."
6371   (when (and gnus-article-skip-boring
6372              (boundp 'gnus-article-boring-faces)
6373              (symbol-value 'gnus-article-boring-faces))
6374     (save-excursion
6375       (let ((inhibit-point-motion-hooks t))
6376         (catch 'only-boring
6377           (while (re-search-forward "\\b\\w\\w" nil t)
6378             (forward-char -1)
6379             (when (not (gnus-intersection
6380                         (gnus-faces-at (point))
6381                         (symbol-value 'gnus-article-boring-faces)))
6382               (throw 'only-boring nil)))
6383           (throw 'only-boring t))))))
6384
6385 (defun gnus-article-refer-article ()
6386   "Read article specified by message-id around point."
6387   (interactive)
6388   (save-excursion
6389     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6390     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6391     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6392         (let ((msg-id (concat "<" (match-string 0) ">")))
6393           (set-buffer gnus-summary-buffer)
6394           (gnus-summary-refer-article msg-id))
6395       (error "No references around point"))))
6396
6397 (defun gnus-article-show-summary ()
6398   "Reconfigure windows to show summary buffer."
6399   (interactive)
6400   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6401       (error "There is no summary buffer for this article buffer")
6402     (gnus-article-set-globals)
6403     (gnus-configure-windows 'article)
6404     (gnus-summary-goto-subject gnus-current-article)
6405     (gnus-summary-position-point)))
6406
6407 (defun gnus-article-describe-briefly ()
6408   "Describe article mode commands briefly."
6409   (interactive)
6410   (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")))
6411
6412 (defun gnus-article-check-buffer ()
6413   "Beep if not in an article buffer."
6414   (unless (equal major-mode 'gnus-article-mode)
6415     (error "Command invoked outside of a Gnus article buffer")))
6416
6417 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6418   "Read a summary buffer key sequence and execute it from the article buffer."
6419   (interactive "P")
6420   (gnus-article-check-buffer)
6421   (let ((nosaves
6422          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6423            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6424            "=" "^" "\M-^" "|"))
6425         (nosave-but-article
6426          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6427            "An" "Ap" [?A (meta return)] [?A delete]))
6428         (nosave-in-article
6429          '("AS" "\C-d"))
6430         (up-to-top
6431          '("n" "Gn" "p" "Gp"))
6432         keys new-sum-point)
6433     (with-current-buffer gnus-article-current-summary
6434       (let (gnus-pick-mode)
6435         (setq unread-command-events (nconc unread-command-events
6436                                            (list (or key last-command-event)))
6437               keys (if (featurep 'xemacs)
6438                        (events-to-keys (read-key-sequence nil t))
6439                      (read-key-sequence nil t)))))
6440
6441     (message "")
6442
6443     (cond
6444      ((eq (aref keys (1- (length keys))) ?\C-h)
6445       (gnus-article-describe-bindings (substring keys 0 -1)))
6446      ((or (member keys nosaves)
6447           (member keys nosave-but-article)
6448           (member keys nosave-in-article))
6449       (let (func)
6450         (save-window-excursion
6451           (pop-to-buffer gnus-article-current-summary)
6452           ;; We disable the pick minor mode commands.
6453           (let (gnus-pick-mode)
6454             (setq func (key-binding keys t))))
6455         (if (or (not func)
6456                 (numberp func))
6457             (ding)
6458           (unless (member keys nosave-in-article)
6459             (set-buffer gnus-article-current-summary))
6460           (call-interactively func)
6461           (setq new-sum-point (point)))
6462         (when (member keys nosave-but-article)
6463           (pop-to-buffer gnus-article-buffer))))
6464      (t
6465       ;; These commands should restore window configuration.
6466       (let ((obuf (current-buffer))
6467             (owin (current-window-configuration))
6468             win func in-buffer selected new-sum-start new-sum-hscroll err)
6469         (cond (not-restore-window
6470                (pop-to-buffer gnus-article-current-summary)
6471                (setq win (selected-window)))
6472               ((setq win (get-buffer-window gnus-article-current-summary))
6473                (select-window win))
6474               (t
6475                (let ((summary-buffer gnus-article-current-summary))
6476                  (gnus-configure-windows 'article)
6477                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6478                    (let ((gnus-buffer-configuration
6479                           '((article ((vertical 1.0
6480                                                 (summary 0.25 point)
6481                                                 (article 1.0)))))))
6482                      (gnus-configure-windows 'article))
6483                    (setq win (get-buffer-window summary-buffer 'visible)))
6484                  (gnus-select-frame-set-input-focus (window-frame win))
6485                  (select-window win))))
6486         (setq in-buffer (current-buffer))
6487         ;; We disable the pick minor mode commands.
6488         (if (and (setq func (let (gnus-pick-mode)
6489                               (key-binding keys t)))
6490                  (functionp func)
6491                  (condition-case code
6492                      (progn
6493                        (call-interactively func)
6494                        t)
6495                    (error
6496                     (setq err code)
6497                     nil)))
6498             (progn
6499               (when (eq win (selected-window))
6500                 (setq new-sum-point (point)
6501                       new-sum-start (window-start win)
6502                       new-sum-hscroll (window-hscroll win)))
6503               (when (or (eq in-buffer (current-buffer))
6504                         (when (eq obuf (current-buffer))
6505                           (set-buffer in-buffer)
6506                           t))
6507                 (setq selected (gnus-summary-select-article))
6508                 (set-buffer obuf)
6509                 (unless not-restore-window
6510                   (set-window-configuration owin))
6511                 (when (and (eq selected 'old)
6512                            new-sum-point)
6513                   (set-window-start (get-buffer-window (current-buffer))
6514                                     1)
6515                   (set-window-point (get-buffer-window (current-buffer))
6516                                     (if (article-goto-body)
6517                                         (1- (point))
6518                                       (point))))
6519                 (when (and (not not-restore-window)
6520                            new-sum-point
6521                            (window-live-p win)
6522                            (with-current-buffer (window-buffer win)
6523                              (eq major-mode 'gnus-summary-mode)))
6524                   (set-window-point win new-sum-point)
6525                   (set-window-start win new-sum-start)
6526                   (set-window-hscroll win new-sum-hscroll))))
6527           (set-window-configuration owin)
6528           (if err
6529               (signal (car err) (cdr err))
6530             (ding))))))))
6531
6532 (defun gnus-article-read-summary-send-keys ()
6533   (interactive)
6534   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6535     (gnus-article-read-summary-keys)))
6536
6537 (defun gnus-article-describe-key (key)
6538   "Display documentation of the function invoked by KEY.
6539 KEY is a string or a vector."
6540   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6541                        (read-key-sequence "Describe key: "))))
6542   (gnus-article-check-buffer)
6543   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6544                                   gnus-article-read-summary-send-keys))
6545       (with-current-buffer gnus-article-current-summary
6546         (setq unread-command-events
6547               (if (featurep 'xemacs)
6548                   (append key nil)
6549                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6550                                         (list 'meta (- x 128))
6551                                       x))
6552                         key)))
6553         (let ((cursor-in-echo-area t)
6554               gnus-pick-mode)
6555           (describe-key (read-key-sequence nil t))))
6556     (describe-key key)))
6557
6558 (defun gnus-article-describe-key-briefly (key &optional insert)
6559   "Display documentation of the function invoked by KEY.
6560 KEY is a string or a vector."
6561   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6562                        (read-key-sequence "Describe key: "))
6563                      current-prefix-arg))
6564   (gnus-article-check-buffer)
6565   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6566                                   gnus-article-read-summary-send-keys))
6567       (with-current-buffer gnus-article-current-summary
6568         (setq unread-command-events
6569               (if (featurep 'xemacs)
6570                   (append key nil)
6571                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6572                                         (list 'meta (- x 128))
6573                                       x))
6574                         key)))
6575         (let ((cursor-in-echo-area t)
6576               gnus-pick-mode)
6577           (describe-key-briefly (read-key-sequence nil t) insert)))
6578     (describe-key-briefly key insert)))
6579
6580 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6581 (defvar gnus-agent-summary-mode)
6582 (defvar gnus-draft-mode)
6583 ;; Calling help-buffer will autoload help-mode.
6584 (defvar help-xref-stack-item)
6585 ;; Emacs 22 doesn't load it in the batch mode.
6586 (eval-when-compile
6587   (autoload 'help-buffer "help-mode"))
6588
6589 (defun gnus-article-describe-bindings (&optional prefix)
6590   "Show a list of all defined keys, and their definitions.
6591 The optional argument PREFIX, if non-nil, should be a key sequence;
6592 then we display only bindings that start with that prefix."
6593   (interactive)
6594   (gnus-article-check-buffer)
6595   (let ((keymap (copy-keymap gnus-article-mode-map))
6596         (map (copy-keymap gnus-article-send-map))
6597         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6598         parent agent draft)
6599     (define-key keymap "S" map)
6600     (define-key map [t] nil)
6601     (with-current-buffer gnus-article-current-summary
6602       (set-keymap-parent
6603        keymap
6604        (if (setq parent (keymap-parent gnus-article-mode-map))
6605            (prog1
6606                (setq parent (copy-keymap parent))
6607              (set-keymap-parent parent (current-local-map)))
6608          (current-local-map)))
6609       (set-keymap-parent map (key-binding "S"))
6610       (let (key def gnus-pick-mode)
6611         (while sumkeys
6612           (setq key (pop sumkeys))
6613           (cond ((and (vectorp key) (= (length key) 1)
6614                       (consp (setq def (aref key 0)))
6615                       (numberp (car def)) (numberp (cdr def)))
6616                  (when (< (max (car def) (cdr def)) 128)
6617                    (setq sumkeys
6618                          (append (mapcar
6619                                   #'vector
6620                                   (nreverse (gnus-uncompress-range def)))
6621                                  sumkeys))))
6622                 ((setq def (key-binding key))
6623                  (unless (eq def 'undefined)
6624                    (define-key keymap key def))))))
6625       (when (boundp 'gnus-agent-summary-mode)
6626         (setq agent gnus-agent-summary-mode))
6627       (when (boundp 'gnus-draft-mode)
6628         (setq draft gnus-draft-mode)))
6629     (with-temp-buffer
6630       (use-local-map keymap)
6631       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6632       (set (make-local-variable 'gnus-draft-mode) draft)
6633       (describe-bindings prefix))
6634     (let ((item `((lambda (prefix)
6635                     (with-current-buffer ,(current-buffer)
6636                       (gnus-article-describe-bindings prefix)))
6637                   ,prefix)))
6638       (with-current-buffer (let (help-xref-following) (help-buffer))
6639         (setq help-xref-stack-item item)))))
6640
6641 (defun gnus-article-reply-with-original (&optional wide)
6642   "Start composing a reply mail to the current message.
6643 The text in the region will be yanked.  If the region isn't active,
6644 the entire article will be yanked."
6645   (interactive)
6646   (let ((article (cdr gnus-article-current))
6647         contents)
6648     (if (not (gnus-region-active-p))
6649         (with-current-buffer gnus-summary-buffer
6650           (gnus-summary-reply (list (list article)) wide))
6651       (setq contents (buffer-substring (point) (mark t)))
6652       ;; Deactivate active regions.
6653       (when (and (boundp 'transient-mark-mode)
6654                  transient-mark-mode)
6655         (setq mark-active nil))
6656       (with-current-buffer gnus-summary-buffer
6657         (gnus-summary-reply
6658          (list (list article contents)) wide)))))
6659
6660 (defun gnus-article-wide-reply-with-original ()
6661   "Start composing a wide reply mail to the current message.
6662 The text in the region will be yanked.  If the region isn't active,
6663 the entire article will be yanked."
6664   (interactive)
6665   (gnus-article-reply-with-original t))
6666
6667 (defun gnus-article-followup-with-original ()
6668   "Compose a followup to the current article.
6669 The text in the region will be yanked.  If the region isn't active,
6670 the entire article will be yanked."
6671   (interactive)
6672   (let ((article (cdr gnus-article-current))
6673         contents)
6674       (if (not (gnus-region-active-p))
6675           (with-current-buffer gnus-summary-buffer
6676             (gnus-summary-followup (list (list article))))
6677         (setq contents (buffer-substring (point) (mark t)))
6678         ;; Deactivate active regions.
6679         (when (and (boundp 'transient-mark-mode)
6680                    transient-mark-mode)
6681           (setq mark-active nil))
6682         (with-current-buffer gnus-summary-buffer
6683           (gnus-summary-followup
6684            (list (list article contents)))))))
6685
6686 (defun gnus-article-hide (&optional arg force)
6687   "Hide all the gruft in the current article.
6688 This means that signatures, cited text and (some) headers will be
6689 hidden.
6690 If given a prefix, show the hidden text instead."
6691   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6692   (gnus-article-hide-headers arg)
6693   (gnus-article-hide-list-identifiers arg)
6694   (gnus-article-hide-citation-maybe arg force)
6695   (gnus-article-hide-signature arg))
6696
6697 (defun gnus-article-maybe-highlight ()
6698   "Do some article highlighting if article highlighting is requested."
6699   (when (gnus-visual-p 'article-highlight 'highlight)
6700     (gnus-article-highlight-some)))
6701
6702 (defun gnus-check-group-server ()
6703   ;; Make sure the connection to the server is alive.
6704   (unless (gnus-server-opened
6705            (gnus-find-method-for-group gnus-newsgroup-name))
6706     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6707     (gnus-request-group gnus-newsgroup-name t)))
6708
6709 (eval-when-compile
6710   (autoload 'nneething-get-file-name "nneething"))
6711
6712 (defun gnus-request-article-this-buffer (article group)
6713   "Get an article and insert it into this buffer."
6714   (let (do-update-line sparse-header)
6715     (prog1
6716         (save-excursion
6717           (erase-buffer)
6718           (gnus-kill-all-overlays)
6719           (setq group (or group gnus-newsgroup-name))
6720
6721           ;; Using `gnus-request-article' directly will insert the article into
6722           ;; `nntp-server-buffer' - so we'll save some time by not having to
6723           ;; copy it from the server buffer into the article buffer.
6724
6725           ;; We only request an article by message-id when we do not have the
6726           ;; headers for it, so we'll have to get those.
6727           (when (stringp article)
6728             (gnus-read-header article))
6729
6730           ;; If the article number is negative, that means that this article
6731           ;; doesn't belong in this newsgroup (possibly), so we find its
6732           ;; message-id and request it by id instead of number.
6733           (when (and (numberp article)
6734                      gnus-summary-buffer
6735                      (get-buffer gnus-summary-buffer)
6736                      (gnus-buffer-exists-p gnus-summary-buffer))
6737             (with-current-buffer gnus-summary-buffer
6738               (let ((header (gnus-summary-article-header article)))
6739                 (when (< article 0)
6740                   (cond
6741                    ((memq article gnus-newsgroup-sparse)
6742                     ;; This is a sparse gap article.
6743                     (setq do-update-line article)
6744                     (setq article (mail-header-id header))
6745                     (setq sparse-header (gnus-read-header article))
6746                     (setq gnus-newsgroup-sparse
6747                           (delq article gnus-newsgroup-sparse)))
6748                    ((vectorp header)
6749                     ;; It's a real article.
6750                     (setq article (mail-header-id header)))
6751                    (t
6752                     ;; It is an extracted pseudo-article.
6753                     (setq article 'pseudo)
6754                     (gnus-request-pseudo-article header))))
6755
6756                 (let ((method (gnus-find-method-for-group
6757                                gnus-newsgroup-name)))
6758                   (when (and (eq (car method) 'nneething)
6759                              (vectorp header))
6760                     (let ((dir (nneething-get-file-name
6761                                 (mail-header-id header))))
6762                       (when (and (stringp dir)
6763                                  (file-directory-p dir))
6764                         (setq article 'nneething)
6765                         (gnus-group-enter-directory dir))))))))
6766
6767           (cond
6768            ;; Refuse to select canceled articles.
6769            ((and (numberp article)
6770                  gnus-summary-buffer
6771                  (get-buffer gnus-summary-buffer)
6772                  (gnus-buffer-exists-p gnus-summary-buffer)
6773                  (eq (cdr (with-current-buffer gnus-summary-buffer
6774                             (assq article gnus-newsgroup-reads)))
6775                      gnus-canceled-mark))
6776             nil)
6777            ;; We first check `gnus-original-article-buffer'.
6778            ((and (get-buffer gnus-original-article-buffer)
6779                  (numberp article)
6780                  (with-current-buffer gnus-original-article-buffer
6781                    (and (equal (car gnus-original-article) group)
6782                         (eq (cdr gnus-original-article) article))))
6783             ;; `insert-buffer-substring' would incorrectly use the
6784             ;; equivalent of string-make-multibyte which amount to decoding
6785             ;; with locale-coding-system, causing failure of
6786             ;; subsequent decoding.
6787             (insert (mm-string-to-multibyte
6788                      (with-current-buffer gnus-original-article-buffer
6789                        (buffer-substring (point-min) (point-max)))))
6790             'article)
6791            ;; Check the backlog.
6792            ((and gnus-keep-backlog
6793                  (gnus-backlog-request-article group article (current-buffer)))
6794             'article)
6795            ;; Check asynchronous pre-fetch.
6796            ((gnus-async-request-fetched-article group article (current-buffer))
6797             (gnus-async-prefetch-next group article gnus-summary-buffer)
6798             (when (and (numberp article) gnus-keep-backlog)
6799               (gnus-backlog-enter-article group article (current-buffer)))
6800             'article)
6801            ;; Check the cache.
6802            ((and gnus-use-cache
6803                  (numberp article)
6804                  (gnus-cache-request-article article group))
6805             'article)
6806            ;; Check the agent cache.
6807            ((gnus-agent-request-article article group)
6808             'article)
6809            ;; Get the article and put into the article buffer.
6810            ((or (stringp article)
6811                 (numberp article))
6812             (let ((gnus-override-method gnus-override-method)
6813                   (methods (and (stringp article)
6814                                 gnus-refer-article-method))
6815                   (backend (car (gnus-find-method-for-group
6816                                  gnus-newsgroup-name)))
6817                   result
6818                   (inhibit-read-only t))
6819               (if (or (not (listp methods))
6820                       (and (symbolp (car methods))
6821                            (assq (car methods) nnoo-definition-alist)))
6822                   (setq methods (list methods)))
6823               (when (and (null gnus-override-method)
6824                          methods)
6825                 (setq gnus-override-method (pop methods)))
6826               (while (not result)
6827                 (when (eq gnus-override-method 'current)
6828                   (setq gnus-override-method
6829                         (with-current-buffer gnus-summary-buffer
6830                           gnus-current-select-method)))
6831                 (erase-buffer)
6832                 (gnus-kill-all-overlays)
6833                 (let ((gnus-newsgroup-name group))
6834                   (gnus-check-group-server))
6835                 (cond
6836                  ((gnus-request-article article group (current-buffer))
6837                   (when (numberp article)
6838                     (gnus-async-prefetch-next group article
6839                                               gnus-summary-buffer)
6840                     (when gnus-keep-backlog
6841                       (gnus-backlog-enter-article
6842                        group article (current-buffer))))
6843                   (setq result 'article))
6844                  (methods
6845                   (setq gnus-override-method (pop methods)))
6846                  ((not (string-match "^400 "
6847                                      (nnheader-get-report backend)))
6848                   ;; If we get 400 server disconnect, reconnect and
6849                   ;; retry; otherwise, assume the article has expired.
6850                   (setq result 'done))))
6851               (and (eq result 'article) 'article)))
6852            ;; It was a pseudo.
6853            (t article)))
6854
6855       ;; Associate this article with the current summary buffer.
6856       (setq gnus-article-current-summary gnus-summary-buffer)
6857
6858       ;; Take the article from the original article buffer
6859       ;; and place it in the buffer it's supposed to be in.
6860       (when (and (get-buffer gnus-article-buffer)
6861                  (equal (buffer-name (current-buffer))
6862                         (buffer-name (get-buffer gnus-article-buffer))))
6863         (save-excursion
6864           (if (get-buffer gnus-original-article-buffer)
6865               (set-buffer gnus-original-article-buffer)
6866             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6867             (buffer-disable-undo)
6868             (setq major-mode 'gnus-original-article-mode)
6869             (setq buffer-read-only t))
6870           (let ((inhibit-read-only t))
6871             (erase-buffer)
6872             (insert-buffer-substring gnus-article-buffer))
6873           (setq gnus-original-article (cons group article)))
6874
6875         ;; Decode charsets.
6876         (run-hooks 'gnus-article-decode-hook)
6877         ;; Mark article as decoded or not.
6878         (setq gnus-article-decoded-p gnus-article-decode-hook))
6879
6880       ;; Update sparse articles.
6881       (when (and do-update-line
6882                  (or (numberp article)
6883                      (stringp article)))
6884         (let ((buf (current-buffer)))
6885           (set-buffer gnus-summary-buffer)
6886           (gnus-summary-update-article do-update-line sparse-header)
6887           (gnus-summary-goto-subject do-update-line nil t)
6888           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6889                             (point))
6890           (set-buffer buf))))))
6891
6892 ;;;
6893 ;;; Article editing
6894 ;;;
6895
6896 (defcustom gnus-article-edit-mode-hook nil
6897   "Hook run in article edit mode buffers."
6898   :group 'gnus-article-various
6899   :type 'hook)
6900
6901 (defvar gnus-article-edit-done-function nil)
6902
6903 (defvar gnus-article-edit-mode-map nil)
6904
6905 ;; Should we be using derived.el for this?
6906 (unless gnus-article-edit-mode-map
6907   (setq gnus-article-edit-mode-map (make-keymap))
6908   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6909
6910   (gnus-define-keys gnus-article-edit-mode-map
6911     "\C-c?"    describe-mode
6912     "\C-c\C-c" gnus-article-edit-done
6913     "\C-c\C-k" gnus-article-edit-exit
6914     "\C-c\C-f\C-t" message-goto-to
6915     "\C-c\C-f\C-o" message-goto-from
6916     "\C-c\C-f\C-b" message-goto-bcc
6917     ;;"\C-c\C-f\C-w" message-goto-fcc
6918     "\C-c\C-f\C-c" message-goto-cc
6919     "\C-c\C-f\C-s" message-goto-subject
6920     "\C-c\C-f\C-r" message-goto-reply-to
6921     "\C-c\C-f\C-n" message-goto-newsgroups
6922     "\C-c\C-f\C-d" message-goto-distribution
6923     "\C-c\C-f\C-f" message-goto-followup-to
6924     "\C-c\C-f\C-m" message-goto-mail-followup-to
6925     "\C-c\C-f\C-k" message-goto-keywords
6926     "\C-c\C-f\C-u" message-goto-summary
6927     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6928     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6929     "\C-c\C-b" message-goto-body
6930     "\C-c\C-i" message-goto-signature
6931
6932     "\C-c\C-t" message-insert-to
6933     "\C-c\C-n" message-insert-newsgroups
6934     "\C-c\C-o" message-sort-headers
6935     "\C-c\C-e" message-elide-region
6936     "\C-c\C-v" message-delete-not-region
6937     "\C-c\C-z" message-kill-to-signature
6938     "\M-\r" message-newline-and-reformat
6939     "\C-c\C-a" mml-attach-file
6940     "\C-a" message-beginning-of-line
6941     "\t" message-tab
6942     "\M-;" comment-region)
6943
6944   (gnus-define-keys (gnus-article-edit-wash-map
6945                      "\C-c\C-w" gnus-article-edit-mode-map)
6946     "f" gnus-article-edit-full-stops))
6947
6948 (easy-menu-define
6949   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6950   '("Field"
6951     ["Fetch To" message-insert-to t]
6952     ["Fetch Newsgroups" message-insert-newsgroups t]
6953     "----"
6954     ["To" message-goto-to t]
6955     ["From" message-goto-from t]
6956     ["Subject" message-goto-subject t]
6957     ["Cc" message-goto-cc t]
6958     ["Reply-To" message-goto-reply-to t]
6959     ["Summary" message-goto-summary t]
6960     ["Keywords" message-goto-keywords t]
6961     ["Newsgroups" message-goto-newsgroups t]
6962     ["Followup-To" message-goto-followup-to t]
6963     ["Mail-Followup-To" message-goto-mail-followup-to t]
6964     ["Distribution" message-goto-distribution t]
6965     ["Body" message-goto-body t]
6966     ["Signature" message-goto-signature t]))
6967
6968 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6969   "Major mode for editing articles.
6970 This is an extended text-mode.
6971
6972 \\{gnus-article-edit-mode-map}"
6973   (make-local-variable 'gnus-article-edit-done-function)
6974   (make-local-variable 'gnus-prev-winconf)
6975   (set (make-local-variable 'font-lock-defaults)
6976        '(message-font-lock-keywords t))
6977   (set (make-local-variable 'mail-header-separator) "")
6978   (set (make-local-variable 'gnus-article-edit-mode) t)
6979   (easy-menu-add message-mode-field-menu message-mode-map)
6980   (mml-mode)
6981   (setq buffer-read-only nil)
6982   (buffer-enable-undo)
6983   (widen))
6984
6985 (defun gnus-article-edit (&optional force)
6986   "Edit the current article.
6987 This will have permanent effect only in mail groups.
6988 If FORCE is non-nil, allow editing of articles even in read-only
6989 groups."
6990   (interactive "P")
6991   (when (and (not force)
6992              (gnus-group-read-only-p))
6993     (error "The current newsgroup does not support article editing"))
6994   (gnus-article-date-original)
6995   (gnus-article-edit-article
6996    'ignore
6997    `(lambda (no-highlight)
6998       'ignore
6999       (gnus-summary-edit-article-done
7000        ,(or (mail-header-references gnus-current-headers) "")
7001        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7002
7003 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7004   "Start editing the contents of the current article buffer."
7005   (let ((winconf (current-window-configuration)))
7006     (set-buffer gnus-article-buffer)
7007     (let ((message-auto-save-directory
7008            ;; Don't associate the article buffer with a draft file.
7009            nil))
7010       (gnus-article-edit-mode))
7011     (funcall start-func)
7012     (set-buffer-modified-p nil)
7013     (gnus-configure-windows 'edit-article)
7014     (setq gnus-article-edit-done-function exit-func)
7015     (setq gnus-prev-winconf winconf)
7016     (unless quiet
7017       (gnus-message 6 "C-c C-c to end edits"))))
7018
7019 (defun gnus-article-edit-done (&optional arg)
7020   "Update the article edits and exit."
7021   (interactive "P")
7022   (let ((func gnus-article-edit-done-function)
7023         (buf (current-buffer))
7024         (start (window-start))
7025         (p (point))
7026         (winconf gnus-prev-winconf))
7027     (widen) ;; Widen it in case that users narrowed the buffer.
7028     (funcall func arg)
7029     (set-buffer buf)
7030     ;; The cache and backlog have to be flushed somewhat.
7031     (when gnus-keep-backlog
7032       (gnus-backlog-remove-article
7033        (car gnus-article-current) (cdr gnus-article-current)))
7034     ;; Flush original article as well.
7035     (when (get-buffer gnus-original-article-buffer)
7036       (with-current-buffer gnus-original-article-buffer
7037         (setq gnus-original-article nil)))
7038     (when gnus-use-cache
7039       (gnus-cache-update-article
7040        (car gnus-article-current) (cdr gnus-article-current)))
7041     ;; We remove all text props from the article buffer.
7042     (kill-all-local-variables)
7043     (set-text-properties (point-min) (point-max) nil)
7044     (gnus-article-mode)
7045     (set-window-configuration winconf)
7046     (set-buffer buf)
7047     (set-window-start (get-buffer-window buf) start)
7048     (set-window-point (get-buffer-window buf) (point)))
7049   (gnus-summary-show-article))
7050
7051 (defun gnus-article-edit-exit ()
7052   "Exit the article editing without updating."
7053   (interactive)
7054   (when (or (not (buffer-modified-p))
7055             (yes-or-no-p "Article modified; kill anyway? "))
7056     (let ((curbuf (current-buffer))
7057           (p (point))
7058           (window-start (window-start)))
7059       (erase-buffer)
7060       (if (gnus-buffer-live-p gnus-original-article-buffer)
7061           (insert-buffer-substring gnus-original-article-buffer))
7062       (let ((winconf gnus-prev-winconf))
7063         (kill-all-local-variables)
7064         (gnus-article-mode)
7065         (set-window-configuration winconf)
7066         ;; Tippy-toe some to make sure that point remains where it was.
7067         (save-current-buffer
7068           (set-buffer curbuf)
7069           (set-window-start (get-buffer-window (current-buffer)) window-start)
7070           (goto-char p))))
7071     (gnus-summary-show-article)))
7072
7073 (defun gnus-article-edit-full-stops ()
7074   "Interactively repair spacing at end of sentences."
7075   (interactive)
7076   (save-excursion
7077     (goto-char (point-min))
7078     (search-forward-regexp "^$" nil t)
7079     (let ((case-fold-search nil))
7080       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7081
7082 ;;;
7083 ;;; Article highlights
7084 ;;;
7085
7086 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7087
7088 ;;; Internal Variables:
7089
7090 (defcustom gnus-button-url-regexp
7091   (concat
7092    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7093    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7094    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7095    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7096        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7097              (punct "!?:;.,"))
7098          (concat
7099           "\\(?:"
7100           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7101           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7102           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7103           "\\|"
7104           "[" chars punct     "]+" "[" chars "]"
7105           "\\)"))
7106      (concat ;; XEmacs 21.4 doesn't support POSIX.
7107       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7108       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7109    "\\)")
7110   "Regular expression that matches URLs."
7111   :group 'gnus-article-buttons
7112   :type 'regexp)
7113
7114 (defcustom gnus-button-valid-fqdn-regexp
7115   message-valid-fqdn-regexp
7116   "Regular expression that matches a valid FQDN."
7117   :version "22.1"
7118   :group 'gnus-article-buttons
7119   :type 'regexp)
7120
7121 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7122 (defcustom gnus-button-valid-localpart-regexp
7123   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7124   "Regular expression that matches a localpart of mail addresses or MIDs."
7125   :version "22.1"
7126   :group 'gnus-article-buttons
7127   :type 'regexp)
7128
7129 (defcustom gnus-button-man-handler 'manual-entry
7130   "Function to use for displaying man pages.
7131 The function must take at least one argument with a string naming the
7132 man page."
7133   :version "22.1"
7134   :type '(choice (function-item :tag "Man" manual-entry)
7135                  (function-item :tag "Woman" woman)
7136                  (function :tag "Other"))
7137   :group 'gnus-article-buttons)
7138
7139 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
7140   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
7141 If the default site is too slow, try to find a CTAN mirror, see
7142 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
7143 the variable `gnus-button-handle-ctan'."
7144   :version "22.1"
7145   :group 'gnus-article-buttons
7146   :link '(custom-manual "(gnus)Group Parameters")
7147   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
7148                  (const "http://tug.ctan.org/tex-archive/")
7149                  (const "http://www.dante.de/CTAN/")
7150                  (string :tag "Other")))
7151
7152 (defcustom gnus-button-ctan-handler 'browse-url
7153   "Function to use for displaying CTAN links.
7154 The function must take one argument, the string naming the URL."
7155   :version "22.1"
7156   :type '(choice (function-item :tag "Browse Url" browse-url)
7157                  (function :tag "Other"))
7158   :group 'gnus-article-buttons)
7159
7160 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
7161   "Bogus strings removed from CTAN URLs."
7162   :version "22.1"
7163   :group 'gnus-article-buttons
7164   :type '(choice (const "^/?tex-archive/\\|/")
7165                  (regexp :tag "Other")))
7166
7167 (defcustom gnus-button-ctan-directory-regexp
7168   (regexp-opt
7169    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
7170          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
7171          "languages" "macros" "nonfree" "obsolete" "support" "systems"
7172          "tds" "tools" "usergrps" "web") t)
7173   "Regular expression for ctan directories.
7174 It should match all directories in the top level of `gnus-ctan-url'."
7175   :version "22.1"
7176   :group 'gnus-article-buttons
7177   :type 'regexp)
7178
7179 (defcustom gnus-button-mid-or-mail-regexp
7180   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7181           gnus-button-valid-fqdn-regexp
7182           ">?\\)\\b")
7183   "Regular expression that matches a message ID or a mail address."
7184   :version "22.1"
7185   :group 'gnus-article-buttons
7186   :type 'regexp)
7187
7188 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7189   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7190 Strings like this can be either a message ID or a mail address.  If it is one
7191 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7192 message ID or a mail address, respectively.  If this variable is set to the
7193 symbol `ask', always query the user what do do.  If it is a function, this
7194 function will be called with the string as its only argument.  The function
7195 must return `mid', `mail', `invalid' or `ask'."
7196   :version "22.1"
7197   :group 'gnus-article-buttons
7198   :type '(choice (function-item :tag "Heuristic function"
7199                                 gnus-button-mid-or-mail-heuristic)
7200                  (const ask)
7201                  (const mid)
7202                  (const mail)))
7203
7204 (defcustom gnus-button-mid-or-mail-heuristic-alist
7205   '((-10.0 . ".+\\$.+@")
7206     (-10.0 . "#")
7207     (-10.0 . "\\*")
7208     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7209     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7210     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7211     (-1.0  . "^[^a-z]+@")
7212     ;;
7213     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7214     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7215     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7216     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7217     ;;
7218     (-2.0  . "^[0-9]")
7219     (-1.0  . "^[0-9][0-9]")
7220     ;;
7221     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7222     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7223     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7224     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7225     ;;
7226     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7227     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7228     ;;       "[0-9]{8,}.*\@"
7229     (-3.0
7230      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7231     ;; "[0-9]{12,}.*\@"
7232     ;; compensation for TDMA dated mail addresses:
7233     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7234     ;;
7235     (-20.0 . "\\.fsf@") ;; Gnus
7236     (-20.0 . "^slrn")
7237     (-20.0 . "^Pine")
7238     (-20.0 . "^alpine\\.")
7239     (-20.0 . "_-_") ;; Subject change in thread
7240     ;;
7241     (-20.0 . "\\.ln@") ;; leafnode
7242     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7243     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7244     ;;
7245     ;; (5.0 . "") ;; $local_part_len <= 7
7246     (10.0  . "^[^0-9]+@")
7247     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7248     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7249     (3.0   . "\@stud")
7250     ;;
7251     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7252     ;;
7253     (0.5   . "^[A-Z][a-z]")
7254     (0.5   . "^[A-Z][a-z][a-z]")
7255     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7256     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7257   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7258
7259 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7260 address.  The REGEXP is processed with `case-fold-search' set to nil."
7261   :version "22.1"
7262   :group 'gnus-article-buttons
7263   :type '(repeat (cons (number :tag "Rate")
7264                        (regexp :tag "Regexp"))))
7265
7266 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7267   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7268 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7269 address, `ask' if unsure and `invalid' if the string is invalid."
7270   (let ((case-fold-search nil)
7271         (list gnus-button-mid-or-mail-heuristic-alist)
7272         (result 0) rate regexp lpartlen elem)
7273     (setq lpartlen
7274           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7275     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7276     ;; Certain special cases...
7277     (when (string-match
7278            (concat
7279             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7280             "^[0-9]+\\.[0-9]+@compuserve\\|"
7281             "@public\\.gmane\\.org")
7282            mid-or-mail)
7283       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7284       (setq result 'mail))
7285     (when (string-match "@.*@\\| " mid-or-mail)
7286       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7287       (setq result 'invalid))
7288     ;; Nothing more to do, if result is not a number here...
7289     (when (numberp result)
7290       (while list
7291         (setq elem (car list)
7292               rate (car elem)
7293               regexp (cdr elem)
7294               list (cdr list))
7295         (when (string-match regexp mid-or-mail)
7296           (setq result (+ result rate))
7297           (gnus-message
7298            9 "`%s' matched `%s', rate `%s', result `%s'."
7299            mid-or-mail regexp rate result)))
7300       (when (<= lpartlen 7)
7301         (setq result (+ result 5.0))
7302         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7303                       mid-or-mail result))
7304       (when (>= lpartlen 12)
7305         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7306         (cond
7307          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7308           ;; Long local part should contain realname if e-mail address,
7309           ;; too many digits: message-id.
7310           ;; $score -= 5.0 + 0.1 * $local_part_len;
7311           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7312           (setq result (+ result rate))
7313           (gnus-message
7314            9 "Many digits in `%s', rate `%s', result `%s'."
7315            mid-or-mail rate result))
7316          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7317                         mid-or-mail)
7318           ;; Too few vowels [^aeiouy]{4,}.*\@
7319           (setq result (+ result -5.0))
7320           (gnus-message
7321            9 "Few vowels in `%s', rate `%s', result `%s'."
7322            mid-or-mail -5.0 result))
7323          (t
7324           (setq result (+ result 5.0))
7325           (gnus-message
7326            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7327     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7328     ;; Maybe we should make this a customizable alist: (condition . 'result)
7329     (cond
7330      ((symbolp result) result)
7331      ;; Now convert number into proper results:
7332      ((< result -10.0) 'mid)
7333      ((> result  10.0) 'mail)
7334      (t 'ask))))
7335
7336 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7337   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7338          (url-mid (concat "news" ":" mid-or-mail))
7339          (url-mailto (concat "mailto" ":" mid-or-mail)))
7340     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7341     (when (fboundp pref)
7342       (setq guessed
7343             ;; get rid of surrounding angles...
7344             (funcall pref
7345                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7346       (if (or (eq 'mid guessed) (eq 'mail guessed))
7347           (setq pref guessed)
7348         (setq pref 'ask)))
7349     (if (eq pref 'ask)
7350         (save-window-excursion
7351           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7352               (setq pref 'mail)
7353             (setq pref 'mid))))
7354     (cond ((eq pref 'mid)
7355            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7356            (gnus-button-handle-news url-mid))
7357           ((eq pref 'mail)
7358            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7359            (gnus-url-mailto url-mailto))
7360           (t (gnus-message 3 "Invalid string.")))))
7361
7362 (defun gnus-button-handle-custom (fun arg)
7363   "Call function FUN on argument ARG.
7364 Both FUN and ARG are supposed to be strings.  ARG will be passed
7365 as a symbol to FUN."
7366   (funcall (intern fun)
7367            (if (string-match "^customize-apropos" fun)
7368                arg
7369              (intern arg))))
7370
7371 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7372
7373 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7374 ;; stuff?
7375
7376 (defun gnus-button-handle-describe-function (url)
7377   "Call `describe-function' when pushing the corresponding URL button."
7378   (describe-function
7379    (intern
7380     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7381
7382 (defun gnus-button-handle-describe-variable (url)
7383   "Call `describe-variable' when pushing the corresponding URL button."
7384   (describe-variable
7385    (intern
7386     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7387
7388 (defun gnus-button-handle-symbol (url)
7389 "Display help on variable or function.
7390 Calls `describe-variable' or `describe-function'."
7391   (let ((sym (intern url)))
7392     (cond
7393      ((fboundp sym) (describe-function sym))
7394      ((boundp sym) (describe-variable sym))
7395      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7396
7397 (defun gnus-button-handle-describe-key (url)
7398   "Call `describe-key' when pushing the corresponding URL button."
7399   (let* ((key-string
7400           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7401          (keys (ignore-errors (eval `(kbd ,key-string)))))
7402     (if keys
7403         (describe-key keys)
7404       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7405
7406 (defun gnus-button-handle-apropos (url)
7407   "Call `apropos' when pushing the corresponding URL button."
7408   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7409
7410 (defun gnus-button-handle-apropos-command (url)
7411   "Call `apropos' when pushing the corresponding URL button."
7412   (apropos-command
7413    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7414
7415 (defun gnus-button-handle-apropos-variable (url)
7416   "Call `apropos' when pushing the corresponding URL button."
7417   (funcall
7418    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7419    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7420
7421 (defun gnus-button-handle-apropos-documentation (url)
7422   "Call `apropos' when pushing the corresponding URL button."
7423   (funcall
7424    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7425    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7426
7427 (defun gnus-button-handle-library (url)
7428   "Call `locate-library' when pushing the corresponding URL button."
7429   (gnus-message 9 "url=`%s'" url)
7430   (let* ((lib (locate-library url))
7431          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7432     (if (not lib)
7433         (gnus-message 1 "Cannot locale library `%s'." url)
7434       (find-file-read-only file))))
7435
7436 (defun gnus-button-handle-ctan (url)
7437   "Call `browse-url' when pushing a CTAN URL button."
7438   (funcall
7439    gnus-button-ctan-handler
7440    (concat
7441     gnus-ctan-url
7442     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
7443
7444 (defcustom gnus-button-tex-level 5
7445   "*Integer that says how many TeX-related buttons Gnus will show.
7446 The higher the number, the more buttons will appear and the more false
7447 positives are possible.  Note that you can set this variable local to
7448 specific groups.  Setting it higher in TeX groups is probably a good idea.
7449 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7450 how to set variables in specific groups."
7451   :version "22.1"
7452   :group 'gnus-article-buttons
7453   :link '(custom-manual "(gnus)Group Parameters")
7454   :type 'integer)
7455
7456 (defcustom gnus-button-man-level 5
7457   "*Integer that says how many man-related buttons Gnus will show.
7458 The higher the number, the more buttons will appear and the more false
7459 positives are possible.  Note that you can set this variable local to
7460 specific groups.  Setting it higher in Unix groups is probably a good idea.
7461 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7462 how to set variables in specific groups."
7463   :version "22.1"
7464   :group 'gnus-article-buttons
7465   :link '(custom-manual "(gnus)Group Parameters")
7466   :type 'integer)
7467
7468 (defcustom gnus-button-emacs-level 5
7469   "*Integer that says how many emacs-related buttons Gnus will show.
7470 The higher the number, the more buttons will appear and the more false
7471 positives are possible.  Note that you can set this variable local to
7472 specific groups.  Setting it higher in Emacs or Gnus related groups is
7473 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7474 `gnus-parameters' on how to set variables in specific groups."
7475   :version "22.1"
7476   :group 'gnus-article-buttons
7477   :link '(custom-manual "(gnus)Group Parameters")
7478   :type 'integer)
7479
7480 (defcustom gnus-button-message-level 5
7481   "*Integer that says how many buttons for news or mail messages will appear.
7482 The higher the number, the more buttons will appear and the more false
7483 positives are possible."
7484   ;; mail addresses, MIDs, URLs for news, ...
7485   :version "22.1"
7486   :group 'gnus-article-buttons
7487   :type 'integer)
7488
7489 (defcustom gnus-button-browse-level 5
7490   "*Integer that says how many buttons for browsing will appear.
7491 The higher the number, the more buttons will appear and the more false
7492 positives are possible."
7493   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7494   :version "22.1"
7495   :group 'gnus-article-buttons
7496   :type 'integer)
7497
7498 (defcustom gnus-button-alist
7499   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7500      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7501     ((concat "\\b\\(nntp\\|news\\):\\("
7502              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7503      0 t gnus-button-handle-news 2)
7504     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7505      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7506     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7507      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7508     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7509     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7510      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7511     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7512      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7513     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7514      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7515     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7516      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7517     ;; RFC 2368 (The mailto URL scheme)
7518     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7519      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7520     ("\\bmailto:\\([^ \n\t]+\\)"
7521      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7522     ;; CTAN
7523     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
7524              gnus-button-ctan-directory-regexp
7525              "[^][>)!;:,'\n\t ]+\\)")
7526      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
7527     ((concat "\\btex-archive/\\("
7528              gnus-button-ctan-directory-regexp
7529              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
7530      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
7531     ((concat
7532       "\\b\\("
7533       gnus-button-ctan-directory-regexp
7534       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
7535      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
7536     ;; Info Konqueror style <info:/foo/bar baz>.
7537     ;; Must come before " Gnus home-grown style".
7538     ("\\binfo://?\\([^'\">\n\t]+\\)"
7539      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7540    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7541     ("\\binfo://\\([^'\">\n\t ]+\\)"
7542      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7543     ;; Info GNOME style <info:foo#bar_baz>
7544     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7545      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7546     ;; Info KDE style <info:(foo)bar baz>
7547     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7548      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7549     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7550      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7551     ("\\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,]*\\)\\)?"
7552      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7553      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7554     ;; This is custom
7555     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7556      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7557     ;; Emacs help commands
7558     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7559      ;; regexp doesn't match arguments containing ` '.
7560      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7561     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7562      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7563     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7564      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7565     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7566      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7567     ;; The following entries may lead to many false positives so don't enable
7568     ;; them by default (use a high button level).
7569     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7570      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7571      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7572     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7573      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7574     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7575      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7576     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7577      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7578     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7579      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7580     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7581      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7582     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7583      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7584     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7585      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7586     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7587      ;; Unlike the other regexps we really have to require quoting
7588      ;; here to determine where it ends.
7589      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7590     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7591     ("<URL: *\\([^\n<>]*\\)>"
7592      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7593     ;; RFC 2396 (2.4.3., delims) ...
7594     ("\"URL: *\\([^\n\"]*\\)\""
7595      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7596     ;; Raw URLs.
7597     (gnus-button-url-regexp
7598      0 (>= gnus-button-browse-level 0) browse-url 0)
7599     ;; man pages
7600     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7601      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7602      gnus-button-handle-man 1)
7603     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7604     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7605      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7606      gnus-button-handle-man 1)
7607     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7608     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7609     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7610      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7611     ;; Recognizing patches to .el files.  This is somewhat obscure,
7612     ;; but considering the percentage of Gnus users who hack Emacs
7613     ;; Lisp files...
7614     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7615      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7616     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7617      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7618     ;; MID or mail: To avoid too many false positives we don't try to catch
7619     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7620     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7621     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7622     ;; so that non-ambiguous entries (see above) match first.
7623     (gnus-button-mid-or-mail-regexp
7624      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7625   "*Alist of regexps matching buttons in article bodies.
7626
7627 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7628 REGEXP: is the string (case insensitive) matching text around the button (can
7629 also be Lisp expression evaluating to a string),
7630 BUTTON: is the number of the regexp grouping actually matching the button,
7631 FORM: is a Lisp expression which must eval to true for the button to
7632 be added,
7633 CALLBACK: is the function to call when the user push this button, and each
7634 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7635
7636 CALLBACK can also be a variable, in that case the value of that
7637 variable it the real callback function."
7638   :group 'gnus-article-buttons
7639   :type '(repeat (list (choice regexp variable sexp)
7640                        (integer :tag "Button")
7641                        (sexp :tag "Form")
7642                        (function :tag "Callback")
7643                        (repeat :tag "Par"
7644                                :inline t
7645                                (integer :tag "Regexp group")))))
7646 (put 'gnus-button-alist 'risky-local-variable t)
7647
7648 (defcustom gnus-header-button-alist
7649   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7650      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7651     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7652      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7653     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7654      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7655     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7656      0 (>= gnus-button-browse-level 0) browse-url 0)
7657     ("^Subject:" gnus-button-url-regexp
7658      0 (>= gnus-button-browse-level 0) browse-url 0)
7659     ("^[^:]+:" gnus-button-url-regexp
7660      0 (>= gnus-button-browse-level 0) browse-url 0)
7661     ("^OpenPGP:.*url=" gnus-button-url-regexp
7662      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7663     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7664      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7665     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7666      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7667   "*Alist of headers and regexps to match buttons in article heads.
7668
7669 This alist is very similar to `gnus-button-alist', except that each
7670 alist has an additional HEADER element first in each entry:
7671
7672 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7673
7674 HEADER is a regexp to match a header.  For a fuller explanation, see
7675 `gnus-button-alist'."
7676   :group 'gnus-article-buttons
7677   :group 'gnus-article-headers
7678   :type '(repeat (list (regexp :tag "Header")
7679                        (choice regexp variable)
7680                        (integer :tag "Button")
7681                        (sexp :tag "Form")
7682                        (function :tag "Callback")
7683                        (repeat :tag "Par"
7684                                :inline t
7685                                (integer :tag "Regexp group")))))
7686 (put 'gnus-header-button-alist 'risky-local-variable t)
7687
7688 ;;; Commands:
7689
7690 (defun gnus-article-push-button (event)
7691   "Check text under the mouse pointer for a callback function.
7692 If the text under the mouse pointer has a `gnus-callback' property,
7693 call it with the value of the `gnus-data' text property."
7694   (interactive "e")
7695   (set-buffer (window-buffer (posn-window (event-start event))))
7696   (let* ((pos (posn-point (event-start event)))
7697          (data (get-text-property pos 'gnus-data))
7698          (fun (get-text-property pos 'gnus-callback)))
7699     (goto-char pos)
7700     (when fun
7701       (funcall fun data))))
7702
7703 (defun gnus-article-press-button ()
7704   "Check text at point for a callback function.
7705 If the text at point has a `gnus-callback' property,
7706 call it with the value of the `gnus-data' text property."
7707   (interactive)
7708   (let ((data (get-text-property (point) 'gnus-data))
7709         (fun (get-text-property (point) 'gnus-callback)))
7710     (when fun
7711       (funcall fun data))))
7712
7713 (defun gnus-article-highlight (&optional force)
7714   "Highlight current article.
7715 This function calls `gnus-article-highlight-headers',
7716 `gnus-article-highlight-citation',
7717 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7718 do the highlighting.  See the documentation for those functions."
7719   (interactive (list 'force))
7720   (gnus-article-highlight-headers)
7721   (gnus-article-highlight-citation force)
7722   (gnus-article-highlight-signature)
7723   (gnus-article-add-buttons force)
7724   (gnus-article-add-buttons-to-head))
7725
7726 (defun gnus-article-highlight-some (&optional force)
7727   "Highlight current article.
7728 This function calls `gnus-article-highlight-headers',
7729 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7730 do the highlighting.  See the documentation for those functions."
7731   (interactive (list 'force))
7732   (gnus-article-highlight-headers)
7733   (gnus-article-highlight-signature)
7734   (gnus-article-add-buttons))
7735
7736 (defun gnus-article-highlight-headers ()
7737   "Highlight article headers as specified by `gnus-header-face-alist'."
7738   (interactive)
7739   (gnus-with-article-headers
7740     (let (regexp header-face field-face from hpoints fpoints)
7741       (dolist (entry gnus-header-face-alist)
7742         (goto-char (point-min))
7743         (setq regexp (concat "^\\("
7744                              (if (string-equal "" (nth 0 entry))
7745                                  "[^\t ]"
7746                                (nth 0 entry))
7747                              "\\)")
7748               header-face (nth 1 entry)
7749               field-face (nth 2 entry))
7750         (while (and (re-search-forward regexp nil t)
7751                     (not (eobp)))
7752           (beginning-of-line)
7753           (setq from (point))
7754           (unless (search-forward ":" nil t)
7755             (forward-char 1))
7756           (when (and header-face
7757                      (not (memq (point) hpoints)))
7758             (push (point) hpoints)
7759             (gnus-put-text-property from (point) 'face header-face))
7760           (when (and field-face
7761                      (not (memq (setq from (point)) fpoints)))
7762             (push from fpoints)
7763             (if (re-search-forward "^[^ \t]" nil t)
7764                 (forward-char -2)
7765               (goto-char (point-max)))
7766             (gnus-put-text-property from (point) 'face field-face)))))))
7767
7768 (defun gnus-article-highlight-signature ()
7769   "Highlight the signature in an article.
7770 It does this by highlighting everything after
7771 `gnus-signature-separator' using the face `gnus-signature'."
7772   (interactive)
7773   (gnus-with-article-buffer
7774     (let ((inhibit-point-motion-hooks t))
7775       (save-restriction
7776         (when (and gnus-signature-face
7777                    (gnus-article-narrow-to-signature))
7778           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7779                             'face gnus-signature-face)
7780           (widen)
7781           (gnus-article-search-signature)
7782           (let ((start (match-beginning 0))
7783                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7784             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7785                                      end)))))))
7786
7787 (defun gnus-button-in-region-p (b e prop)
7788   "Say whether PROP exists in the region."
7789   (text-property-not-all b e prop nil))
7790
7791 (defun gnus-article-add-buttons (&optional force)
7792   "Find external references in the article and make buttons of them.
7793 \"External references\" are things like Message-IDs and URLs, as
7794 specified by `gnus-button-alist'."
7795   (interactive (list 'force))
7796   (gnus-with-article-buffer
7797     (let ((inhibit-point-motion-hooks t)
7798           (case-fold-search t)
7799           (alist gnus-button-alist)
7800           beg entry regexp)
7801       ;; Remove all old markers.
7802       (let (marker entry new-list)
7803         (while (setq marker (pop gnus-button-marker-list))
7804           (if (or (< marker (point-min)) (>= marker (point-max)))
7805               (push marker new-list)
7806             (goto-char marker)
7807             (when (setq entry (gnus-button-entry))
7808               (put-text-property (match-beginning (nth 1 entry))
7809                                  (match-end (nth 1 entry))
7810                                  'gnus-callback nil))
7811             (set-marker marker nil)))
7812         (setq gnus-button-marker-list new-list))
7813       ;; We skip the headers.
7814       (article-goto-body)
7815       (setq beg (point))
7816       (while (setq entry (pop alist))
7817         (setq regexp (eval (car entry)))
7818         (goto-char beg)
7819         (while (re-search-forward regexp nil t)
7820           (let ((start (match-beginning (nth 1 entry)))
7821                 (end (match-end (nth 1 entry)))
7822                 (from (match-beginning 0)))
7823             (when (and (or (eq t (nth 2 entry))
7824                            (eval (nth 2 entry)))
7825                        (not (gnus-button-in-region-p
7826                              start end 'gnus-callback)))
7827               ;; That optional form returned non-nil, so we add the
7828               ;; button.
7829               (setq from (set-marker (make-marker) from))
7830               (push from gnus-button-marker-list)
7831               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7832                            (gnus-article-extend-url-button from start end))
7833                 (gnus-article-add-button start end
7834                                          'gnus-button-push from)
7835                 (gnus-put-text-property
7836                  start end
7837                  'gnus-string (buffer-substring-no-properties
7838                                start end))))))))))
7839
7840 (defun gnus-article-extend-url-button (beg start end)
7841   "Extend url button if url is folded into two or more lines.
7842 Return non-nil if button is extended.  BEG is a marker that points to
7843 the beginning position of a text containing url.  START and END are
7844 the endpoints of a url button before it is extended.  The concatenated
7845 url is put as the `gnus-button-url' overlay property on the button."
7846   (let ((opoint (point))
7847         (points (list start end))
7848         url delim regexp)
7849     (prog1
7850         (when (and (progn
7851                      (goto-char end)
7852                      (not (looking-at "[\t ]*[\">]")))
7853                    (progn
7854                      (goto-char start)
7855                      (string-match
7856                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7857                       (buffer-substring (point-at-bol) start)))
7858                    (progn
7859                      (setq url (list (buffer-substring start end))
7860                            delim (if (match-beginning 1) ">" "\""))
7861                      (beginning-of-line)
7862                      (setq regexp (concat
7863                                    (when (and (looking-at
7864                                                message-cite-prefix-regexp)
7865                                               (< (match-end 0) start))
7866                                      (regexp-quote (match-string 0)))
7867                                    "\
7868 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7869                                    delim "\\)"))
7870                      (while (progn
7871                               (forward-line 1)
7872                               (and (looking-at regexp)
7873                                    (prog1
7874                                        (match-beginning 1)
7875                                      (push (or (match-string 2)
7876                                                (match-string 1))
7877                                            url)
7878                                      (push (setq end (or (match-end 2)
7879                                                          (match-end 1)))
7880                                            points)
7881                                      (push (or (match-beginning 2)
7882                                                (match-beginning 1))
7883                                            points)))))
7884                      (match-beginning 2)))
7885           (let (gnus-article-mouse-face widget-mouse-face)
7886             (while points
7887               (gnus-article-add-button (pop points) (pop points)
7888                                        'gnus-button-push beg)))
7889           (let ((overlay (gnus-make-overlay start end)))
7890             (gnus-overlay-put overlay 'evaporate t)
7891             (gnus-overlay-put overlay 'gnus-button-url
7892                               (list (mapconcat 'identity (nreverse url) "")))
7893             (when gnus-article-mouse-face
7894               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7895           t)
7896       (goto-char opoint))))
7897
7898 ;; Add buttons to the head of an article.
7899 (defun gnus-article-add-buttons-to-head ()
7900   "Add buttons to the head of the article."
7901   (interactive)
7902   (gnus-with-article-headers
7903     (let (beg end)
7904       (dolist (entry gnus-header-button-alist)
7905         ;; Each alist entry.
7906         (goto-char (point-min))
7907         (while (re-search-forward (car entry) nil t)
7908           ;; Each header matching the entry.
7909           (setq beg (match-beginning 0))
7910           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7911                              (match-beginning 0))
7912                         (point-max)))
7913           (goto-char beg)
7914           (while (re-search-forward (eval (nth 1 entry)) end t)
7915             ;; Each match within a header.
7916             (let* ((entry (cdr entry))
7917                    (start (match-beginning (nth 1 entry)))
7918                    (end (match-end (nth 1 entry)))
7919                    (form (nth 2 entry)))
7920               (goto-char (match-end 0))
7921               (when (eval form)
7922                 (gnus-article-add-button
7923                  start end (nth 3 entry)
7924                  (buffer-substring (match-beginning (nth 4 entry))
7925                                    (match-end (nth 4 entry)))))))
7926           (goto-char end))))))
7927
7928 ;;; External functions:
7929
7930 (defun gnus-article-add-button (from to fun &optional data text)
7931   "Create a button between FROM and TO with callback FUN and data DATA."
7932   (when gnus-article-button-face
7933     (gnus-overlay-put (gnus-make-overlay from to nil t)
7934                       'face gnus-article-button-face))
7935   (gnus-add-text-properties
7936    from to
7937    (nconc (and gnus-article-mouse-face
7938                (list gnus-mouse-face-prop gnus-article-mouse-face))
7939           (list 'gnus-callback fun)
7940           (and data (list 'gnus-data data))))
7941   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7942                          :help-echo (or text "Follow the link")
7943                          :keymap gnus-url-button-map
7944                          :button-keymap gnus-widget-button-keymap))
7945
7946 (defun gnus-article-copy-string ()
7947   "Copy the string in the button to the kill ring."
7948   (interactive)
7949   (gnus-article-check-buffer)
7950   (let ((data (get-text-property (point) 'gnus-string)))
7951     (when data
7952       (with-temp-buffer
7953         (insert data)
7954         (copy-region-as-kill (point-min) (point-max))
7955         (message "Copied %s" data)))))
7956
7957 ;;; Internal functions:
7958
7959 (defun gnus-article-set-globals ()
7960   (with-current-buffer gnus-summary-buffer
7961     (gnus-set-global-variables)))
7962
7963 (defun gnus-signature-toggle (end)
7964   (gnus-with-article-buffer
7965     (let ((inhibit-point-motion-hooks t))
7966       (if (text-property-any end (point-max) 'article-type 'signature)
7967           (progn
7968             (gnus-delete-wash-type 'signature)
7969             (gnus-remove-text-properties-when
7970              'article-type 'signature end (point-max)
7971              (cons 'article-type (cons 'signature
7972                                        gnus-hidden-properties))))
7973         (gnus-add-wash-type 'signature)
7974         (gnus-add-text-properties-when
7975          'article-type nil end (point-max)
7976          (cons 'article-type (cons 'signature
7977                                    gnus-hidden-properties)))))
7978     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7979       (gnus-set-mode-line 'article))))
7980
7981 (defun gnus-button-entry ()
7982   ;; Return the first entry in `gnus-button-alist' matching this place.
7983   (let ((alist gnus-button-alist)
7984         (entry nil))
7985     (while alist
7986       (setq entry (pop alist))
7987       (if (looking-at (eval (car entry)))
7988           (setq alist nil)
7989         (setq entry nil)))
7990     entry))
7991
7992 (defun gnus-button-push (marker)
7993   ;; Push button starting at MARKER.
7994   (save-excursion
7995     (goto-char marker)
7996     (let* ((entry (gnus-button-entry))
7997            (inhibit-point-motion-hooks t)
7998            (fun (nth 3 entry))
7999            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
8000                           (get-char-property marker 'gnus-button-url))
8001                      (mapcar (lambda (group)
8002                                (let ((string (match-string group)))
8003                                  (set-text-properties
8004                                   0 (length string) nil string)
8005                                  string))
8006                              (nthcdr 4 entry)))))
8007       (cond
8008        ((fboundp fun)
8009         (apply fun args))
8010        ((and (boundp fun)
8011              (fboundp (symbol-value fun)))
8012         (apply (symbol-value fun) args))
8013        (t
8014         (gnus-message 1 "You must define `%S' to use this button"
8015                       (cons fun args)))))))
8016
8017 (defun gnus-parse-news-url (url)
8018   (let (scheme server port group message-id articles)
8019     (with-temp-buffer
8020       (insert url)
8021       (goto-char (point-min))
8022       (when (looking-at "\\([A-Za-z]+\\):")
8023         (setq scheme (match-string 1))
8024         (goto-char (match-end 0)))
8025       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8026         (setq server (match-string 1))
8027         (setq port (if (stringp (match-string 3))
8028                        (string-to-number (match-string 3))
8029                      (match-string 3)))
8030         (goto-char (match-end 0)))
8031
8032       (cond
8033        ((looking-at "\\(.*@.*\\)")
8034         (setq message-id (match-string 1)))
8035        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8036         (setq group (match-string 1)
8037               articles (split-string (match-string 2) "-")))
8038        ((looking-at "\\([^/]+\\)/?")
8039         (setq group (match-string 1)))
8040        (t
8041         (error "Unknown news URL syntax"))))
8042     (list scheme server port group message-id articles)))
8043
8044 (defun gnus-button-handle-news (url)
8045   "Fetch a news URL."
8046   (destructuring-bind (scheme server port group message-id articles)
8047       (gnus-parse-news-url url)
8048     (cond
8049      (message-id
8050       (with-current-buffer gnus-summary-buffer
8051         (if server
8052             (let ((gnus-refer-article-method
8053                    (nconc (list (list 'nntp server))
8054                           gnus-refer-article-method))
8055                   (nntp-port-number (or port "nntp")))
8056               (gnus-message 7 "Fetching %s with %s"
8057                             message-id gnus-refer-article-method)
8058               (gnus-summary-refer-article message-id))
8059           (gnus-summary-refer-article message-id))))
8060      (group
8061       (gnus-button-fetch-group url)))))
8062
8063 (defun gnus-button-patch (library line)
8064   "Visit an Emacs Lisp library LIBRARY on line LINE."
8065   (interactive)
8066   (let ((file (locate-library (file-name-nondirectory library))))
8067     (unless file
8068       (error "Couldn't find library %s" library))
8069     (find-file file)
8070     (goto-char (point-min))
8071     (forward-line (1- (string-to-number line)))))
8072
8073 (defun gnus-button-handle-man (url)
8074   "Fetch a man page."
8075   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8076   (when (eq gnus-button-man-handler 'woman)
8077     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8078   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8079   (funcall gnus-button-man-handler url))
8080
8081 (defun gnus-button-handle-info-url (url)
8082   "Fetch an info URL."
8083   (setq url (mm-subst-char-in-string ?+ ?\  url))
8084   (cond
8085    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8086     (gnus-info-find-node
8087      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8088                      "Gnus")
8089              ")" (gnus-url-unhex-string (match-string 2 url)))))
8090    ((string-match "([^)\"]+)[^\"]+" url)
8091     (setq url
8092           (gnus-replace-in-string
8093            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8094     (gnus-info-find-node url))
8095    (t (error "Can't parse %s" url))))
8096
8097 (defun gnus-button-handle-info-url-gnome (url)
8098   "Fetch GNOME style info URL."
8099   (setq url (mm-subst-char-in-string ?_ ?\  url))
8100   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8101       (gnus-info-find-node
8102        (concat "("
8103                (gnus-url-unhex-string
8104                  (match-string 1 url))
8105                ")"
8106                (or (gnus-url-unhex-string
8107                     (match-string 2 url))
8108                    "Top")))
8109     (error "Can't parse %s" url)))
8110
8111 (defun gnus-button-handle-info-url-kde (url)
8112   "Fetch KDE style info URL."
8113   (gnus-info-find-node (gnus-url-unhex-string url)))
8114
8115 ;; (info) will autoload info.el
8116 (declare-function Info-menu "info" (menu-item &optional fork))
8117 (declare-function Info-index-next "info" (num))
8118
8119 (defun gnus-button-handle-info-keystrokes (url)
8120   "Call `info' when pushing the corresponding URL button."
8121   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8122   (let (node indx comma)
8123     (if (string-match
8124          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8125                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8126                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET"
8127                  "\\(?:[ \t\n,]*\\)\\)?")
8128          url)
8129         (setq node (match-string 2 url)
8130               indx (match-string 3 url))
8131       (error "Can't parse %s" url))
8132     (info)
8133     (Info-directory)
8134     (Info-menu node)
8135     (when (> (length indx) 0)
8136       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8137                             "\\([ \t\n,]*\\)")
8138                     indx)
8139       (setq comma (match-string 2 indx))
8140       (setq indx  (match-string 1 indx))
8141       (Info-index indx)
8142       (when comma
8143         (dotimes (i (with-temp-buffer
8144                       (insert comma)
8145                       ;; Note: the XEmacs version of `how-many' takes
8146                       ;; no optional argument.
8147                       (goto-char (point-min))
8148                       (how-many ",")))
8149           (Info-index-next 1)))
8150       nil)))
8151
8152 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8153 (declare-function pgg-display-output-buffer "pgg" (start end status))
8154
8155 (defun gnus-button-openpgp (url)
8156   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8157   (with-temp-buffer
8158     (mm-url-insert-file-contents-external url)
8159     (pgg-snarf-keys-region (point-min) (point-max))
8160     (pgg-display-output-buffer nil nil nil)))
8161
8162 (defun gnus-button-message-id (message-id)
8163   "Fetch MESSAGE-ID."
8164   (with-current-buffer gnus-summary-buffer
8165     (gnus-summary-refer-article message-id)))
8166
8167 (defun gnus-button-fetch-group (address &rest ignore)
8168   "Fetch GROUP specified by ADDRESS."
8169   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8170                       address)
8171     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8172     ;; for nntp:// and news://
8173     (setq address (match-string 3 address)))
8174   (if (not (string-match "[:/]" address))
8175       ;; This is just a simple group url.
8176       (gnus-group-read-ephemeral-group address gnus-select-method)
8177     (if (not
8178          (string-match
8179           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8180           address))
8181         (error "Can't parse %s" address)
8182       (gnus-group-read-ephemeral-group
8183        (match-string 4 address)
8184        `(nntp ,(match-string 1 address)
8185               (nntp-address ,(match-string 1 address))
8186               (nntp-port-number ,(if (match-end 3)
8187                                      (match-string 3 address)
8188                                    "nntp")))
8189        nil nil nil
8190        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8191
8192 (defun gnus-url-parse-query-string (query &optional downcase)
8193   (let (retval pairs cur key val)
8194     (setq pairs (split-string query "&"))
8195     (while pairs
8196       (setq cur (car pairs)
8197             pairs (cdr pairs))
8198       (if (not (string-match "=" cur))
8199           nil                           ; Grace
8200         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8201               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8202         (if downcase
8203             (setq key (downcase key)))
8204         (setq cur (assoc key retval))
8205         (if cur
8206             (setcdr cur (cons val (cdr cur)))
8207           (setq retval (cons (list key val) retval)))))
8208     retval))
8209
8210 (defun gnus-url-mailto (url)
8211   ;; Send mail to someone
8212   (when (string-match "mailto:/*\\(.*\\)" url)
8213     (setq url (substring url (match-beginning 1) nil)))
8214   (let (to args subject func)
8215     (setq args (gnus-url-parse-query-string
8216                 (if (string-match "^\\?" url)
8217                     (substring url 1)
8218                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8219                       (concat "to=" (match-string 1 url) "&"
8220                               (match-string 2 url))
8221                     (concat "to=" url)))
8222                 t)
8223           subject (cdr-safe (assoc "subject" args)))
8224     (gnus-msg-mail)
8225     (while args
8226       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8227       (if (fboundp func)
8228           (funcall func)
8229         (message-position-on-field (caar args)))
8230       (insert (gnus-replace-in-string
8231                (mapconcat 'identity (reverse (cdar args)) ", ")
8232                "\r\n" "\n" t))
8233       (setq args (cdr args)))
8234     (if subject
8235         (message-goto-body)
8236       (message-goto-subject))))
8237
8238 (defun gnus-button-embedded-url (address)
8239   "Activate ADDRESS with `browse-url'."
8240   (browse-url (gnus-strip-whitespace address)))
8241
8242 ;;; Next/prev buttons in the article buffer.
8243
8244 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8245 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8246
8247 (defvar gnus-prev-page-map
8248   (let ((map (make-sparse-keymap)))
8249     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8250     (define-key map "\r" 'gnus-button-prev-page)
8251     map))
8252
8253 (defvar gnus-next-page-map
8254   (let ((map (make-sparse-keymap)))
8255     (unless (>= emacs-major-version 21)
8256       ;; XEmacs doesn't care.
8257       (set-keymap-parent map gnus-article-mode-map))
8258     (define-key map gnus-mouse-2 'gnus-button-next-page)
8259     (define-key map "\r" 'gnus-button-next-page)
8260     map))
8261
8262 (defun gnus-insert-prev-page-button ()
8263   (let ((b (point)) e
8264         (inhibit-read-only t))
8265     (gnus-eval-format
8266      gnus-prev-page-line-format nil
8267      `(keymap ,gnus-prev-page-map
8268               gnus-prev t
8269               gnus-callback gnus-article-button-prev-page
8270               article-type annotation))
8271     (setq e (if (bolp)
8272                 ;; Exclude a newline.
8273                 (1- (point))
8274               (point)))
8275     (when gnus-article-button-face
8276       (gnus-overlay-put (gnus-make-overlay b e nil t)
8277                         'face gnus-article-button-face))
8278     (widget-convert-button
8279      'link b e
8280      :action 'gnus-button-prev-page
8281      :button-keymap gnus-prev-page-map)))
8282
8283 (defun gnus-button-next-page (&optional args more-args)
8284   "Go to the next page."
8285   (interactive)
8286   (let ((win (selected-window)))
8287     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8288     (gnus-article-next-page)
8289     (select-window win)))
8290
8291 (defun gnus-button-prev-page (&optional args more-args)
8292   "Go to the prev page."
8293   (interactive)
8294   (let ((win (selected-window)))
8295     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8296     (gnus-article-prev-page)
8297     (select-window win)))
8298
8299 (defun gnus-insert-next-page-button ()
8300   (let ((b (point)) e
8301         (inhibit-read-only t))
8302     (gnus-eval-format gnus-next-page-line-format nil
8303                       `(keymap ,gnus-next-page-map
8304                                gnus-next t
8305                                gnus-callback gnus-article-button-next-page
8306                                article-type annotation))
8307     (setq e (if (bolp)
8308                 ;; Exclude a newline.
8309                 (1- (point))
8310               (point)))
8311     (when gnus-article-button-face
8312       (gnus-overlay-put (gnus-make-overlay b e nil t)
8313                         'face gnus-article-button-face))
8314     (widget-convert-button
8315      'link b e
8316      :action 'gnus-button-next-page
8317      :button-keymap gnus-next-page-map)))
8318
8319 (defun gnus-article-button-next-page (arg)
8320   "Go to the next page."
8321   (interactive "P")
8322   (let ((win (selected-window)))
8323     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8324     (gnus-article-next-page)
8325     (select-window win)))
8326
8327 (defun gnus-article-button-prev-page (arg)
8328   "Go to the prev page."
8329   (interactive "P")
8330   (let ((win (selected-window)))
8331     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8332     (gnus-article-prev-page)
8333     (select-window win)))
8334
8335 (defvar gnus-decode-header-methods
8336   '(mail-decode-encoded-word-region)
8337   "List of methods used to decode headers.
8338
8339 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8340 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8341 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8342 whose names match REGEXP.
8343
8344 For example:
8345 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8346  mail-decode-encoded-word-region
8347  (\"chinese\" . rfc1843-decode-region))
8348 ")
8349
8350 (defvar gnus-decode-header-methods-cache nil)
8351
8352 (defun gnus-multi-decode-header (start end)
8353   "Apply the functions from `gnus-encoded-word-methods' that match."
8354   (unless (and gnus-decode-header-methods-cache
8355                (eq gnus-newsgroup-name
8356                    (car gnus-decode-header-methods-cache)))
8357     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8358     (dolist (x gnus-decode-header-methods)
8359       (if (symbolp x)
8360           (nconc gnus-decode-header-methods-cache (list x))
8361         (if (and gnus-newsgroup-name
8362                  (string-match (car x) gnus-newsgroup-name))
8363             (nconc gnus-decode-header-methods-cache
8364                    (list (cdr x)))))))
8365   (let ((xlist gnus-decode-header-methods-cache))
8366     (pop xlist)
8367     (save-restriction
8368       (narrow-to-region start end)
8369       (while xlist
8370         (funcall (pop xlist) (point-min) (point-max))))))
8371
8372 ;;;
8373 ;;; Treatment top-level handling.
8374 ;;;
8375
8376 (defun gnus-treat-article (condition &optional part-number total-parts type)
8377   (let ((length (- (point-max) (point-min)))
8378         (alist gnus-treatment-function-alist)
8379         (article-goto-body-goes-to-point-min-p t)
8380         (treated-type
8381          (or (not type)
8382              (catch 'found
8383                (let ((list gnus-article-treat-types))
8384                  (while list
8385                    (when (string-match (pop list) type)
8386                      (throw 'found t)))))))
8387         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8388         val elem)
8389     (gnus-run-hooks 'gnus-part-display-hook)
8390     (dolist (elem alist)
8391       (setq val
8392             (save-excursion
8393               (when (gnus-buffer-live-p gnus-summary-buffer)
8394                 (set-buffer gnus-summary-buffer))
8395               (symbol-value (car elem))))
8396       (when (and (or (consp val)
8397                      treated-type)
8398                  (gnus-treat-predicate val)
8399                  (or (not (get (car elem) 'highlight))
8400                      highlightp))
8401         (save-restriction
8402           (funcall (cadr elem)))))))
8403
8404 ;; Dynamic variables.
8405 (defvar part-number)
8406 (defvar total-parts)
8407 (defvar type)
8408 (defvar condition)
8409 (defvar length)
8410
8411 (defun gnus-treat-predicate (val)
8412   (cond
8413    ((null val)
8414     nil)
8415    (condition
8416     (eq condition val))
8417    ((and (listp val)
8418          (stringp (car val)))
8419     (apply 'gnus-or (mapcar `(lambda (s)
8420                                (string-match s ,(or gnus-newsgroup-name "")))
8421                             val)))
8422    ((listp val)
8423     (let ((pred (pop val)))
8424       (cond
8425        ((eq pred 'or)
8426         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8427        ((eq pred 'and)
8428         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8429        ((eq pred 'not)
8430         (not (gnus-treat-predicate (car val))))
8431        ((eq pred 'typep)
8432         (equal (car val) type))
8433        (t
8434         (error "%S is not a valid predicate" pred)))))
8435    ((eq val t)
8436     t)
8437    ((eq val 'head)
8438     nil)
8439    ((eq val 'first)
8440     (eq part-number 1))
8441    ((eq val 'last)
8442     (eq part-number total-parts))
8443    ((numberp val)
8444     (< length val))
8445    (t
8446     (error "%S is not a valid value" val))))
8447
8448 (defun gnus-article-encrypt-body (protocol &optional n)
8449   "Encrypt the article body."
8450   (interactive
8451    (list
8452     (or gnus-article-encrypt-protocol
8453         (completing-read "Encrypt protocol: "
8454                          gnus-article-encrypt-protocol-alist
8455                          nil t))
8456     current-prefix-arg))
8457   ;; User might hit `K E' instead of `K e', so prompt once.
8458   (when (and gnus-article-encrypt-protocol
8459              gnus-novice-user)
8460     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8461       (error "Encrypt aborted")))
8462   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8463     (unless func
8464       (error "Can't find the encrypt protocol %s" protocol))
8465     (if (member gnus-newsgroup-name '("nndraft:delayed"
8466                                       "nndraft:drafts"
8467                                       "nndraft:queue"))
8468         (error "Can't encrypt the article in group %s"
8469                gnus-newsgroup-name))
8470     (gnus-summary-iterate n
8471       (with-current-buffer gnus-summary-buffer
8472         (let ((mail-parse-charset gnus-newsgroup-charset)
8473               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8474               (summary-buffer gnus-summary-buffer)
8475               references point)
8476           (gnus-set-global-variables)
8477           (when (gnus-group-read-only-p)
8478             (error "The current newsgroup does not support article encrypt"))
8479           (gnus-summary-show-article t)
8480           (setq references
8481               (or (mail-header-references gnus-current-headers) ""))
8482           (set-buffer gnus-article-buffer)
8483           (let* ((inhibit-read-only t)
8484                  (headers
8485                   (mapcar (lambda (field)
8486                             (and (save-restriction
8487                                    (message-narrow-to-head)
8488                                    (goto-char (point-min))
8489                                    (search-forward field nil t))
8490                                  (prog2
8491                                      (message-narrow-to-field)
8492                                      (buffer-string)
8493                                    (delete-region (point-min) (point-max))
8494                                    (widen))))
8495                           '("Content-Type:" "Content-Transfer-Encoding:"
8496                             "Content-Disposition:"))))
8497             (message-narrow-to-head)
8498             (message-remove-header "MIME-Version")
8499             (goto-char (point-max))
8500             (setq point (point))
8501             (insert (apply 'concat headers))
8502             (widen)
8503             (narrow-to-region point (point-max))
8504             (let ((message-options message-options))
8505               (message-options-set 'message-sender user-mail-address)
8506               (message-options-set 'message-recipients user-mail-address)
8507               (message-options-set 'message-sign-encrypt 'not)
8508               (funcall func))
8509             (goto-char (point-min))
8510             (insert "MIME-Version: 1.0\n")
8511             (widen)
8512             (gnus-summary-edit-article-done
8513              references nil summary-buffer t))
8514           (when gnus-keep-backlog
8515             (gnus-backlog-remove-article
8516              (car gnus-article-current) (cdr gnus-article-current)))
8517           (when (get-buffer gnus-original-article-buffer)
8518             (with-current-buffer gnus-original-article-buffer
8519               (setq gnus-original-article nil)))
8520           (when gnus-use-cache
8521             (gnus-cache-update-article
8522              (car gnus-article-current) (cdr gnus-article-current))))))))
8523
8524 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8525   "The following specs can be used:
8526 %t  The security MIME type
8527 %i  Additional info
8528 %d  Details
8529 %D  Details if button is pressed")
8530
8531 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8532   "The following specs can be used:
8533 %t  The security MIME type
8534 %i  Additional info
8535 %d  Details
8536 %D  Details if button is pressed")
8537
8538 (defvar gnus-mime-security-button-line-format-alist
8539   '((?t gnus-tmp-type ?s)
8540     (?i gnus-tmp-info ?s)
8541     (?d gnus-tmp-details ?s)
8542     (?D gnus-tmp-pressed-details ?s)))
8543
8544 (defvar gnus-mime-security-button-commands
8545   '((gnus-article-press-button "\r" "Show Detail")
8546     (undefined "v")
8547     (undefined "t")
8548     (undefined "C")
8549     (gnus-mime-security-save-part "o" "Save...")
8550     (undefined "\C-o")
8551     (undefined "r")
8552     (undefined "d")
8553     (undefined "c")
8554     (undefined "i")
8555     (undefined "E")
8556     (undefined "e")
8557     (undefined "p")
8558     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8559     (undefined ".")))
8560
8561 (defvar gnus-mime-security-button-map
8562   (let ((map (make-sparse-keymap)))
8563     (define-key map gnus-mouse-2 'gnus-article-push-button)
8564     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8565     (dolist (c gnus-mime-security-button-commands)
8566       (define-key map (cadr c) (car c)))
8567     map))
8568
8569 (easy-menu-define
8570   gnus-mime-security-button-menu gnus-mime-security-button-map
8571   "Security button menu."
8572   `("Security Part"
8573     ,@(delq nil
8574             (mapcar (lambda (c)
8575                       (unless (eq (car c) 'undefined)
8576                         (vector (caddr c) (car c) :active t)))
8577                     gnus-mime-security-button-commands))))
8578
8579 (defun gnus-mime-security-button-menu (event prefix)
8580   "Construct a context-sensitive menu of security commands."
8581   (interactive "e\nP")
8582   (save-window-excursion
8583     (let ((pos (event-start event)))
8584       (select-window (posn-window pos))
8585       (goto-char (posn-point pos))
8586       (gnus-article-check-buffer)
8587       (popup-menu gnus-mime-security-button-menu nil prefix))))
8588
8589 (defvar gnus-mime-security-details-buffer nil)
8590
8591 (defvar gnus-mime-security-button-pressed nil)
8592
8593 (defvar gnus-mime-security-show-details-inline t
8594   "If non-nil, show details in the article buffer.")
8595
8596 (defun gnus-mime-security-verify-or-decrypt (handle)
8597   (mm-remove-parts (cdr handle))
8598   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8599         point (inhibit-read-only t))
8600     (if region
8601         (goto-char (car region)))
8602     (setq point (point))
8603     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8604       (let* ((mm-verify-option 'known)
8605              (mm-decrypt-option 'known)
8606              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8607         (unless (eq nparts (cdr handle))
8608           (mm-destroy-parts (cdr handle))
8609           (setcdr handle nparts))))
8610     (gnus-mime-display-security handle)
8611     (when region
8612       (delete-region (point) (cdr region))
8613       (set-marker (car region) nil)
8614       (set-marker (cdr region) nil))
8615     (goto-char point)))
8616
8617 (defun gnus-mime-security-show-details (handle)
8618   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8619     (if (not details)
8620         (gnus-message 5 "No details.")
8621       (if gnus-mime-security-show-details-inline
8622           (let ((gnus-mime-security-button-pressed
8623                  (not (get-text-property (point) 'gnus-mime-details)))
8624                 (gnus-mime-security-button-line-format
8625                  (get-text-property (point) 'gnus-line-format))
8626                 (inhibit-read-only t))
8627             (forward-char -1)
8628             (while (eq (get-text-property (point) 'gnus-line-format)
8629                        gnus-mime-security-button-line-format)
8630               (forward-char -1))
8631             (forward-char)
8632             (save-restriction
8633               (narrow-to-region (point) (point))
8634               (gnus-insert-mime-security-button handle))
8635             (delete-region (point)
8636                            (or (text-property-not-all
8637                                 (point) (point-max)
8638                                 'gnus-line-format
8639                                 gnus-mime-security-button-line-format)
8640                                (point-max))))
8641         ;; Not inlined.
8642         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8643             (with-current-buffer gnus-mime-security-details-buffer
8644               (erase-buffer)
8645               t)
8646           (setq gnus-mime-security-details-buffer
8647                 (gnus-get-buffer-create "*MIME Security Details*")))
8648         (with-current-buffer gnus-mime-security-details-buffer
8649           (insert details)
8650           (goto-char (point-min)))
8651         (pop-to-buffer gnus-mime-security-details-buffer)))))
8652
8653 (defun gnus-mime-security-press-button (handle)
8654   (save-excursion
8655     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8656         (gnus-mime-security-show-details handle)
8657       (gnus-mime-security-verify-or-decrypt handle))))
8658
8659 (defun gnus-insert-mime-security-button (handle &optional displayed)
8660   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8661          (gnus-tmp-type
8662           (concat
8663            (or (nth 2 (assoc protocol mm-verify-function-alist))
8664                (nth 2 (assoc protocol mm-decrypt-function-alist))
8665                "Unknown")
8666            (if (equal (car handle) "multipart/signed")
8667                " Signed" " Encrypted")
8668            " Part"))
8669          (gnus-tmp-info
8670           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8671               "Undecided"))
8672          (gnus-tmp-details
8673           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8674          gnus-tmp-pressed-details
8675          b e)
8676     (setq gnus-tmp-details
8677           (if gnus-tmp-details
8678               (concat "\n" gnus-tmp-details)
8679             ""))
8680     (setq gnus-tmp-pressed-details
8681           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8682     (unless (bolp)
8683       (insert "\n"))
8684     (setq b (point))
8685     (gnus-eval-format
8686      gnus-mime-security-button-line-format
8687      gnus-mime-security-button-line-format-alist
8688      `(keymap ,gnus-mime-security-button-map
8689          gnus-callback gnus-mime-security-press-button
8690          gnus-line-format ,gnus-mime-security-button-line-format
8691          gnus-mime-details ,gnus-mime-security-button-pressed
8692          article-type annotation
8693          gnus-data ,handle))
8694     (setq e (if (bolp)
8695                 ;; Exclude a newline.
8696                 (1- (point))
8697               (point)))
8698     (when gnus-article-button-face
8699       (gnus-overlay-put (gnus-make-overlay b e nil t)
8700                         'face gnus-article-button-face))
8701     (widget-convert-button
8702      'link b e
8703      :mime-handle handle
8704      :action 'gnus-widget-press-button
8705      :button-keymap gnus-mime-security-button-map
8706      :help-echo
8707      (lambda (widget)
8708        ;; Needed to properly clear the message due to a bug in
8709        ;; wid-edit (XEmacs only).
8710        (when (boundp 'help-echo-owns-message)
8711          (setq help-echo-owns-message t))
8712        (format
8713         "%S: show detail; %S: more options"
8714         (aref gnus-mouse-2 0)
8715         (aref gnus-down-mouse-3 0))))))
8716
8717 (defun gnus-mime-display-security (handle)
8718   (save-restriction
8719     (narrow-to-region (point) (point))
8720     (unless (gnus-unbuttonized-mime-type-p (car handle))
8721       (gnus-insert-mime-security-button handle))
8722     (gnus-mime-display-part (cadr handle))
8723     (unless (bolp)
8724       (insert "\n"))
8725     (unless (gnus-unbuttonized-mime-type-p (car handle))
8726       (let ((gnus-mime-security-button-line-format
8727              gnus-mime-security-button-end-line-format))
8728         (gnus-insert-mime-security-button handle)))
8729     (mm-set-handle-multipart-parameter
8730      handle 'gnus-region
8731      (cons (set-marker (make-marker) (point-min))
8732            (set-marker (make-marker) (point-max))))
8733     (goto-char (point-max))))
8734
8735 (defun gnus-mime-security-run-function (function)
8736   "Run FUNCTION with the security part under point."
8737   (gnus-article-check-buffer)
8738   (let ((data (get-text-property (point) 'gnus-data))
8739         buffer handle)
8740     (when (and (stringp (car-safe data))
8741                (setq buffer (mm-handle-multipart-original-buffer data))
8742                (setq handle (cadr data)))
8743       (if (bufferp (mm-handle-buffer handle))
8744           (progn
8745             (setq handle (cons buffer (copy-sequence (cdr handle))))
8746             (mm-handle-set-undisplayer handle nil))
8747         (setq handle (mm-make-handle
8748                       buffer
8749                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8750                       nil nil nil nil nil nil)))
8751       (funcall function handle))))
8752
8753 (defun gnus-mime-security-save-part ()
8754   "Save the security part under point."
8755   (interactive)
8756   (gnus-mime-security-run-function 'mm-save-part))
8757
8758 (defun gnus-mime-security-pipe-part ()
8759   "Pipe the security part under point to a process."
8760   (interactive)
8761   (gnus-mime-security-run-function 'mm-pipe-part))
8762
8763 (gnus-ems-redefine)
8764
8765 (provide 'gnus-art)
8766
8767 (run-hooks 'gnus-art-load-hook)
8768
8769 ;;; gnus-art.el ends here