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