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