Removed CTAN button stuff, which I don't think is very relevant any more.
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; For Emacs < 22.2.
29 (eval-and-compile
30   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile
32   (require 'cl))
33 (defvar tool-bar-map)
34 (defvar w3m-minor-mode-map)
35
36 (require 'gnus)
37 (require 'gnus-sum)
38 (require 'gnus-spec)
39 (require 'gnus-int)
40 (require 'gnus-win)
41 (require 'mm-bodies)
42 (require 'mail-parse)
43 (require 'mm-decode)
44 (require 'mm-view)
45 (require 'wid-edit)
46 (require 'mm-uu)
47 (require 'message)
48
49 (autoload 'gnus-msg-mail "gnus-msg" nil t)
50 (autoload 'gnus-button-mailto "gnus-msg")
51 (autoload 'gnus-button-reply "gnus-msg" nil t)
52 (autoload 'parse-time-string "parse-time" nil nil)
53 (autoload 'ansi-color-apply-on-region "ansi-color")
54 (autoload 'mm-url-insert-file-contents-external "mm-url")
55 (autoload 'mm-extern-cache-contents "mm-extern")
56
57 (defgroup gnus-article nil
58   "Article display."
59   :link '(custom-manual "(gnus)Article Buffer")
60   :group 'gnus)
61
62 (defgroup gnus-article-treat nil
63   "Treating article parts."
64   :link '(custom-manual "(gnus)Article Hiding")
65   :group 'gnus-article)
66
67 (defgroup gnus-article-hiding nil
68   "Hiding article parts."
69   :link '(custom-manual "(gnus)Article Hiding")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-highlight nil
73   "Article highlighting."
74   :link '(custom-manual "(gnus)Article Highlighting")
75   :group 'gnus-article
76   :group 'gnus-visual)
77
78 (defgroup gnus-article-signature nil
79   "Article signatures."
80   :link '(custom-manual "(gnus)Article Signature")
81   :group 'gnus-article)
82
83 (defgroup gnus-article-headers nil
84   "Article headers."
85   :link '(custom-manual "(gnus)Hiding Headers")
86   :group 'gnus-article)
87
88 (defgroup gnus-article-washing nil
89   "Special commands on articles."
90   :link '(custom-manual "(gnus)Article Washing")
91   :group 'gnus-article)
92
93 (defgroup gnus-article-emphasis nil
94   "Fontisizing articles."
95   :link '(custom-manual "(gnus)Article Fontisizing")
96   :group 'gnus-article)
97
98 (defgroup gnus-article-saving nil
99   "Saving articles."
100   :link '(custom-manual "(gnus)Saving Articles")
101   :group 'gnus-article)
102
103 (defgroup gnus-article-mime nil
104   "Worshiping the MIME wonder."
105   :link '(custom-manual "(gnus)Using MIME")
106   :group 'gnus-article)
107
108 (defgroup gnus-article-buttons nil
109   "Pushable buttons in the article buffer."
110   :link '(custom-manual "(gnus)Article Buttons")
111   :group 'gnus-article)
112
113 (defgroup gnus-article-various nil
114   "Other article options."
115   :link '(custom-manual "(gnus)Misc Article")
116   :group 'gnus-article)
117
118 (defcustom gnus-ignored-headers
119   (mapcar
120    (lambda (header)
121      (concat "^" header ":"))
122    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
123      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
124      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
125      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
126      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
127      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
128      "X-Attribution" "X-Originating-IP" "Delivered-To"
129      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
130      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
131      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
132      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
133      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
134      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
135      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
136      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
137      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
138      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
139      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
140      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
141      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
142      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
143      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
144      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
145      "List-[A-Za-z]+" "X-Listprocessor-Version"
146      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
147      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
148      "X-Received" "Content-length" "X-precedence"
149      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
150      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
151      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
152      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
153      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
154      "X-Content-length" "X-Posting-Agent" "Original-Received"
155      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
156      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
157      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
158      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
159      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
160      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
161      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
162      "Envelope-Sender" "Envelope-Recipients"))
163   "*All headers that start with this regexp will be hidden.
164 This variable can also be a list of regexps of headers to be ignored.
165 If `gnus-visible-headers' is non-nil, this variable will be ignored."
166   :type '(choice :custom-show nil
167                  regexp
168                  (repeat regexp))
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-visible-headers
172   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
173   "*All headers that do not match this regexp will be hidden.
174 This variable can also be a list of regexp of headers to remain visible.
175 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
176   :type '(choice
177           (repeat :value-to-internal (lambda (widget value)
178                                        (custom-split-regexp-maybe value))
179                   :match (lambda (widget value)
180                            (or (stringp value)
181                                (widget-editable-list-match widget value)))
182                   regexp)
183           (const :tag "Use gnus-ignored-headers" nil)
184           regexp)
185   :group 'gnus-article-hiding)
186
187 (defcustom gnus-sorted-header-list
188   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
189     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
190   "*This variable is a list of regular expressions.
191 If it is non-nil, headers that match the regular expressions will
192 be placed first in the article buffer in the sequence specified by
193 this list."
194   :type '(repeat regexp)
195   :group 'gnus-article-hiding)
196
197 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
198   "Headers that are only to be displayed if they have interesting data.
199 Possible values in this list are:
200
201   'empty       Headers with no content.
202   'newsgroups  Newsgroup identical to Gnus group.
203   'to-address  To identical to To-address.
204   'to-list     To identical to To-list.
205   'cc-list     CC identical to To-list.
206   'followup-to Followup-to identical to Newsgroups.
207   'reply-to    Reply-to identical to From.
208   'date        Date less than four days old.
209   'long-to     To and/or Cc longer than 1024 characters.
210   'many-to     Multiple To and/or Cc."
211   :type '(set (const :tag "Headers with no content." empty)
212               (const :tag "Newsgroups identical to Gnus group." newsgroups)
213               (const :tag "To identical to To-address." to-address)
214               (const :tag "To identical to To-list." to-list)
215               (const :tag "CC identical to To-list." cc-list)
216               (const :tag "Followup-to identical to Newsgroups." followup-to)
217               (const :tag "Reply-to identical to From." reply-to)
218               (const :tag "Date less than four days old." date)
219               (const :tag "To and/or Cc longer than 1024 characters." long-to)
220               (const :tag "Multiple To and/or Cc headers." many-to))
221   :group 'gnus-article-hiding)
222
223 (defcustom gnus-article-skip-boring nil
224   "Skip over text that is not worth reading.
225 By default, if you set this t, then Gnus will display citations and
226 signatures, but will never scroll down to show you a page consisting
227 only of boring text.  Boring text is controlled by
228 `gnus-article-boring-faces'."
229   :version "22.1"
230   :type 'boolean
231   :group 'gnus-article-hiding)
232
233 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
234   "Regexp matching signature separator.
235 This can also be a list of regexps.  In that case, it will be checked
236 from head to tail looking for a separator.  Searches will be done from
237 the end of the buffer."
238   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
239                  (regexp)
240                  (repeat :tag "List of regexp" regexp))
241   :group 'gnus-article-signature)
242
243 (defcustom gnus-signature-limit nil
244   "Provide a limit to what is considered a signature.
245 If it is a number, no signature may not be longer (in characters) than
246 that number.  If it is a floating point number, no signature may be
247 longer (in lines) than that number.  If it is a function, the function
248 will be called without any parameters, and if it returns nil, there is
249 no signature in the buffer.  If it is a string, it will be used as a
250 regexp.  If it matches, the text in question is not a signature.
251
252 This can also be a list of the above values."
253   :type '(choice (const nil)
254                  (integer :value 200)
255                  (number :value 4.0)
256                  function
257                  (regexp :value ".*"))
258   :group 'gnus-article-signature)
259
260 (defcustom gnus-fetch-partial-articles nil
261   "If non-nil, Gnus will fetch partial articles.
262 If t, nnimap will fetch only the first part.  If a string, it
263 will fetch all parts that have types that match that string.  A
264 likely value would be \"text/\" to automatically fetch all
265 textual parts.
266
267 Currently only the nnimap backend actually supports partial
268 article fetching.  If the backend doesn't support it, it has no
269 effect."
270   :version "24.1"
271   :type '(choice (const nil)
272                  (const t)
273                  (regexp))
274   :group 'gnus-article)
275
276 (defcustom gnus-hidden-properties '(invisible t intangible t)
277   "Property list to use for hiding text."
278   :type 'sexp
279   :group 'gnus-article-hiding)
280
281 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
282 ;; frames in a session.
283 (defcustom gnus-article-x-face-command
284   (if (featurep 'xemacs)
285       (if (or (gnus-image-type-available-p 'xface)
286               (gnus-image-type-available-p 'pbm))
287           'gnus-display-x-face-in-from
288         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
289     (if (gnus-image-type-available-p 'pbm)
290         'gnus-display-x-face-in-from
291       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
292 display -"))
293   "*String or function to be executed to display an X-Face header.
294 If it is a string, the command will be executed in a sub-shell
295 asynchronously.  The compressed face will be piped to this command."
296   :type `(choice string
297                  (function-item gnus-display-x-face-in-from)
298                  function)
299   :version "21.1"
300   :group 'gnus-picon
301   :group 'gnus-article-washing)
302
303 (defcustom gnus-article-x-face-too-ugly nil
304   "Regexp matching posters whose face shouldn't be shown automatically."
305   :type '(choice regexp (const nil))
306   :group 'gnus-article-washing)
307
308 (defcustom gnus-article-banner-alist nil
309   "Banner alist for stripping.
310 For example,
311      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
312   :version "21.1"
313   :type '(repeat (cons symbol regexp))
314   :group 'gnus-article-washing)
315
316 (gnus-define-group-parameter
317  banner
318  :variable-document
319  "Alist of regexps (to match group names) and banner."
320  :variable-group gnus-article-washing
321  :parameter-type
322  '(choice :tag "Banner"
323           :value nil
324           (const :tag "Remove signature" signature)
325           (symbol :tag "Item in `gnus-article-banner-alist'" none)
326           regexp
327           (const :tag "None" nil))
328  :parameter-document
329  "If non-nil, specify how to remove `banners' from articles.
330
331 Symbol `signature' means to remove signatures delimited by
332 `gnus-signature-separator'.  Any other symbol is used to look up a
333 regular expression to match the banner in `gnus-article-banner-alist'.
334 A string is used as a regular expression to match the banner
335 directly.")
336
337 (defcustom gnus-article-address-banner-alist nil
338   "Alist of mail addresses and banners.
339 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
340 to match a mail address in the From: header, BANNER is one of a symbol
341 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
342 If ADDRESS matches author's mail address, it will remove things like
343 advertisements.  For example:
344
345 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
346 "
347   :type '(repeat
348           (cons
349            (regexp :tag "Address")
350            (choice :tag "Banner" :value nil
351                    (const :tag "Remove signature" signature)
352                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
353                    regexp
354                    (const :tag "None" nil))))
355   :version "22.1"
356   :group 'gnus-article-washing)
357
358 (defmacro gnus-emphasis-custom-with-format (&rest body)
359   `(let ((format "\
360 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
361 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
362      ,@body))
363
364 (defun gnus-emphasis-custom-value-to-external (value)
365   (gnus-emphasis-custom-with-format
366    (if (consp (car value))
367        (list (format format (car (car value)) (cdr (car value)))
368              2
369              (if (nth 1 value) 2 3)
370              (nth 2 value))
371      value)))
372
373 (defun gnus-emphasis-custom-value-to-internal (value)
374   (gnus-emphasis-custom-with-format
375    (let ((regexp (concat "\\`"
376                          (format (regexp-quote format)
377                                  "\\([^()]+\\)" "\\([^()]+\\)")
378                          "\\'"))
379          pattern)
380      (if (string-match regexp (setq pattern (car value)))
381          (list (cons (match-string 1 pattern) (match-string 2 pattern))
382                (= (nth 2 value) 2)
383                (nth 3 value))
384        value))))
385
386 (defcustom gnus-emphasis-alist
387   (let ((types
388          '(("\\*" "\\*" bold nil 2)
389            ("_" "_" underline)
390            ("/" "/" italic)
391            ("_/" "/_" underline-italic)
392            ("_\\*" "\\*_" underline-bold)
393            ("\\*/" "/\\*" bold-italic)
394            ("_\\*/" "/\\*_" underline-bold-italic))))
395     (nconc
396      (gnus-emphasis-custom-with-format
397       (mapcar (lambda (spec)
398                 (list (format format (car spec) (cadr spec))
399                       (or (nth 3 spec) 2)
400                       (or (nth 4 spec) 3)
401                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
402               types))
403      '(;; I've never seen anyone use this strikethru convention whereas I've
404        ;; several times seen it triggered by normal text.  --Stef
405        ;; Miles suggests that this form is sometimes used but for italics,
406        ;; so maybe we should map it to `italic'.
407        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
408        ;; 2 3 gnus-emphasis-strikethru)
409        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
410         2 3 gnus-emphasis-underline))))
411   "*Alist that says how to fontify certain phrases.
412 Each item looks like this:
413
414   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
415
416 The first element is a regular expression to be matched.  The second
417 is a number that says what regular expression grouping used to find
418 the entire emphasized word.  The third is a number that says what
419 regexp grouping should be displayed and highlighted.  The fourth
420 is the face used for highlighting."
421   :type
422   '(repeat
423     (menu-choice
424      :format "%[Customizing Style%]\n%v"
425      :indent 2
426      (group :tag "Default"
427             :value ("" 0 0 default)
428             :value-create
429             (lambda (widget)
430               (let ((value (widget-get
431                             (cadr (widget-get (widget-get widget :parent)
432                                               :args))
433                             :value)))
434                 (if (not (eq (nth 2 value) 'default))
435                     (widget-put
436                      widget
437                      :value
438                      (gnus-emphasis-custom-value-to-external value))))
439               (widget-group-value-create widget))
440             regexp
441             (integer :format "Match group: %v")
442             (integer :format "Emphasize group: %v")
443             face)
444      (group :tag "Simple"
445             :value (("_" . "_") nil default)
446             (cons :format "%v"
447                   (regexp :format "Start regexp: %v")
448                   (regexp :format "End regexp: %v"))
449             (boolean :format "Show start and end patterns: %[%v%]\n"
450                      :on " On " :off " Off ")
451             face)))
452   :get (lambda (symbol)
453          (mapcar 'gnus-emphasis-custom-value-to-internal
454                  (default-value symbol)))
455   :set (lambda (symbol value)
456          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
457                                      value)))
458   :group 'gnus-article-emphasis)
459
460 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
461   "A regexp to describe whitespace which should not be emphasized.
462 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
463 The former avoids underlining of leading and trailing whitespace,
464 and the latter avoids underlining any whitespace at all."
465   :version "21.1"
466   :group 'gnus-article-emphasis
467   :type 'regexp)
468
469 (defface gnus-emphasis-bold '((t (:bold t)))
470   "Face used for displaying strong emphasized text (*word*)."
471   :group 'gnus-article-emphasis)
472
473 (defface gnus-emphasis-italic '((t (:italic t)))
474   "Face used for displaying italic emphasized text (/word/)."
475   :group 'gnus-article-emphasis)
476
477 (defface gnus-emphasis-underline '((t (:underline t)))
478   "Face used for displaying underlined emphasized text (_word_)."
479   :group 'gnus-article-emphasis)
480
481 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
482   "Face used for displaying underlined bold emphasized text (_*word*_)."
483   :group 'gnus-article-emphasis)
484
485 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
486   "Face used for displaying underlined italic emphasized text (_/word/_)."
487   :group 'gnus-article-emphasis)
488
489 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
490   "Face used for displaying bold italic emphasized text (/*word*/)."
491   :group 'gnus-article-emphasis)
492
493 (defface gnus-emphasis-underline-bold-italic
494   '((t (:bold t :italic t :underline t)))
495   "Face used for displaying underlined bold italic emphasized text.
496 Example: (_/*word*/_)."
497   :group 'gnus-article-emphasis)
498
499 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
500                                       '((t (:strikethru t)))
501                                     '((t (:strike-through t))))
502   "Face used for displaying strike-through text (-word-)."
503   :group 'gnus-article-emphasis)
504
505 (defface gnus-emphasis-highlight-words
506   '((t (:background "black" :foreground "yellow")))
507   "Face used for displaying highlighted words."
508   :group 'gnus-article-emphasis)
509
510 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
511   "Format for display of Date headers in article bodies.
512 See `format-time-string' for the possible values.
513
514 The variable can also be function, which should return a complete Date
515 header.  The function is called with one argument, the time, which can
516 be fed to `format-time-string'."
517   :type '(choice string function)
518   :link '(custom-manual "(gnus)Article Date")
519   :group 'gnus-article-washing)
520
521 (defcustom gnus-save-all-headers t
522   "*If non-nil, don't remove any headers before saving.
523 This will be overridden by the `:headers' property that the symbol of
524 the saver function, which is specified by `gnus-default-article-saver',
525 might have."
526   :group 'gnus-article-saving
527   :type 'boolean)
528
529 (defcustom gnus-prompt-before-saving 'always
530   "*This variable says how much prompting is to be done when saving articles.
531 If it is nil, no prompting will be done, and the articles will be
532 saved to the default files.  If this variable is `always', each and
533 every article that is saved will be preceded by a prompt, even when
534 saving large batches of articles.  If this variable is neither nil not
535 `always', there the user will be prompted once for a file name for
536 each invocation of the saving commands."
537   :group 'gnus-article-saving
538   :type '(choice (item always)
539                  (item :tag "never" nil)
540                  (sexp :tag "once" :format "%t\n" :value t)))
541
542 (defcustom gnus-saved-headers gnus-visible-headers
543   "Headers to keep if `gnus-save-all-headers' is nil.
544 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
545 If that variable is nil, however, all headers that match this regexp
546 will be kept while the rest will be deleted before saving.  This and
547 `gnus-save-all-headers' will be overridden by the `:headers' property
548 that the symbol of the saver function, which is specified by
549 `gnus-default-article-saver', might have."
550   :group 'gnus-article-saving
551   :type 'regexp)
552
553 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
554 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
555   "A function to save articles in your favourite format.
556 The function will be called by way of the `gnus-summary-save-article'
557 command, and friends such as `gnus-summary-save-article-rmail'.
558
559 Gnus provides the following functions:
560
561 * gnus-summary-save-in-rmail (Rmail format)
562 * gnus-summary-save-in-mail (Unix mail format)
563 * gnus-summary-save-in-folder (MH folder)
564 * gnus-summary-save-in-file (article format)
565 * gnus-summary-save-body-in-file (article body)
566 * gnus-summary-save-in-vm (use VM's folder format)
567 * gnus-summary-write-to-file (article format -- overwrite)
568 * gnus-summary-write-body-to-file (article body -- overwrite)
569 * gnus-summary-save-in-pipe (article format)
570
571 The symbol of each function may have the following properties:
572
573 * :decode
574 The value non-nil means save decoded articles.  This is meaningful
575 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
576 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
577 `gnus-summary-save-in-pipe'.
578
579 * :function
580 The value specifies an alternative function which appends, not
581 overwrites, articles to a file.  This implies that when saving many
582 articles at a time, `gnus-prompt-before-saving' is bound to t and all
583 articles are saved in a single file.  This is meaningful only with
584 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
585
586 * :headers
587 The value specifies the symbol of a variable of which the value
588 specifies headers to be saved.  If it is omitted,
589 `gnus-save-all-headers' and `gnus-saved-headers' control what
590 headers should be saved."
591   :group 'gnus-article-saving
592   :type '(radio (function-item gnus-summary-save-in-rmail)
593                 (function-item gnus-summary-save-in-mail)
594                 (function-item gnus-summary-save-in-folder)
595                 (function-item gnus-summary-save-in-file)
596                 (function-item gnus-summary-save-body-in-file)
597                 (function-item gnus-summary-save-in-vm)
598                 (function-item gnus-summary-write-to-file)
599                 (function-item gnus-summary-write-body-to-file)
600                 (function-item gnus-summary-save-in-pipe)
601                 (function)))
602
603 (defcustom gnus-article-save-coding-system
604   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
605       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
606       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
607       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
608   "Coding system used to save decoded articles to a file.
609
610 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
611 which can safely encode any characters in text.  This is used by the
612 commands including:
613
614 * gnus-summary-save-article-file
615 * gnus-summary-save-article-body-file
616 * gnus-summary-write-article-file
617 * gnus-summary-write-article-body-file
618
619 and the functions to which you may set `gnus-default-article-saver':
620
621 * gnus-summary-save-in-file
622 * gnus-summary-save-body-in-file
623 * gnus-summary-write-to-file
624 * gnus-summary-write-body-to-file
625
626 Those commands and functions save just text displayed in the article
627 buffer to a file if the value of this variable is non-nil.  Note that
628 buttonized MIME parts will be lost in a saved file in that case.
629 Otherwise, raw articles will be saved."
630   :group 'gnus-article-saving
631   :type `(choice
632           :format "%{%t%}:\n %[Value Menu%] %v"
633           (const :tag "Save raw articles" nil)
634           ,@(delq nil
635                   (mapcar
636                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
637                    '((const :tag "UTF-8" utf-8)
638                      (const :tag "iso-2022-7bit" iso-2022-7bit)
639                      (const :tag "Emacs internal" emacs-mule)
640                      (const :tag "escape-quoted" escape-quoted))))
641           (symbol :tag "Coding system")))
642
643 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
644   "A function generating a file name to save articles in Rmail format.
645 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
646   :group 'gnus-article-saving
647   :type 'function)
648
649 (defcustom gnus-mail-save-name 'gnus-plain-save-name
650   "A function generating a file name to save articles in Unix mail format.
651 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
652   :group 'gnus-article-saving
653   :type 'function)
654
655 (defcustom gnus-folder-save-name 'gnus-folder-save-name
656   "A function generating a file name to save articles in MH folder.
657 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
658   :group 'gnus-article-saving
659   :type 'function)
660
661 (defcustom gnus-file-save-name 'gnus-numeric-save-name
662   "A function generating a file name to save articles in article format.
663 The function is called with NEWSGROUP, HEADERS, and optional
664 LAST-FILE."
665   :group 'gnus-article-saving
666   :type 'function)
667
668 (defcustom gnus-split-methods
669   '((gnus-article-archive-name)
670     (gnus-article-nndoc-name))
671   "*Variable used to suggest where articles are to be saved.
672 For instance, if you would like to save articles related to Gnus in
673 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
674 you could set this variable to something like:
675
676  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
677    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
678
679 This variable is an alist where the key is the match and the
680 value is a list of possible files to save in if the match is
681 non-nil.
682
683 If the match is a string, it is used as a regexp match on the
684 article.  If the match is a symbol, that symbol will be funcalled
685 from the buffer of the article to be saved with the newsgroup as the
686 parameter.  If it is a list, it will be evaled in the same buffer.
687
688 If this form or function returns a string, this string will be used as a
689 possible file name; and if it returns a non-nil list, that list will be
690 used as possible file names."
691   :group 'gnus-article-saving
692   :type '(repeat (choice (list :value (fun) function)
693                          (cons :value ("" "") regexp (repeat string))
694                          (sexp :value nil))))
695
696 (defcustom gnus-page-delimiter "^\^L"
697   "*Regexp describing what to use as article page delimiters.
698 The default value is \"^\^L\", which is a form linefeed at the
699 beginning of a line."
700   :type 'regexp
701   :group 'gnus-article-various)
702
703 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
704   "*The format specification for the article mode line.
705 See `gnus-summary-mode-line-format' for a closer description.
706
707 The following additional specs are available:
708
709 %w  The article washing status.
710 %m  The number of MIME parts in the article."
711   :type 'string
712   :group 'gnus-article-various)
713
714 (defcustom gnus-article-mode-hook nil
715   "*A hook for Gnus article mode."
716   :type 'hook
717   :group 'gnus-article-various)
718
719 (when (featurep 'xemacs)
720   ;; Extracted from gnus-xmas-define in order to preserve user settings
721   (when (fboundp 'turn-off-scroll-in-place)
722     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
723   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
724   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
725
726 (defcustom gnus-article-menu-hook nil
727   "*Hook run after the creation of the article mode menu."
728   :type 'hook
729   :group 'gnus-article-various)
730
731 (defcustom gnus-article-prepare-hook nil
732   "*A hook called after an article has been prepared in the article buffer."
733   :type 'hook
734   :group 'gnus-article-various)
735
736 (defcustom gnus-copy-article-ignored-headers nil
737   "List of headers to be removed when copying an article.
738 Each element is a regular expression."
739   :version "23.1" ;; No Gnus
740   :type '(repeat regexp)
741   :group 'gnus-article-various)
742
743 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
744                         "Gnus 5.10 (Emacs-22.1)")
745
746 (defface gnus-button
747   '((t (:weight bold)))
748   "Face used for highlighting a button in the article buffer."
749   :group 'gnus-article-buttons)
750
751 (defcustom gnus-article-button-face 'gnus-button
752   "Face used for highlighting buttons in the article buffer.
753
754 An article button is a piece of text that you can activate by pressing
755 `RET' or `mouse-2' above it."
756   :type 'face
757   :group 'gnus-article-buttons)
758
759 (defcustom gnus-article-mouse-face 'highlight
760   "Face used for mouse highlighting in the article buffer.
761
762 Article buttons will be displayed in this face when the cursor is
763 above them."
764   :type 'face
765   :group 'gnus-article-buttons)
766
767 (defcustom gnus-signature-face 'gnus-signature
768   "Face used for highlighting a signature in the article buffer.
769 Obsolete; use the face `gnus-signature' for customizations instead."
770   :type 'face
771   :group 'gnus-article-highlight
772   :group 'gnus-article-signature)
773
774 (defface gnus-signature
775   '((t
776      (:italic t)))
777   "Face used for highlighting a signature in the article buffer."
778   :group 'gnus-article-highlight
779   :group 'gnus-article-signature)
780 ;; backward-compatibility alias
781 (put 'gnus-signature-face 'face-alias 'gnus-signature)
782 (put 'gnus-signature-face 'obsolete-face "22.1")
783
784 (defface gnus-header-from
785   '((((class color)
786       (background dark))
787      (:foreground "PaleGreen1"))
788     (((class color)
789       (background light))
790      (:foreground "red3"))
791     (t
792      (:italic t)))
793   "Face used for displaying from headers."
794   :group 'gnus-article-headers
795   :group 'gnus-article-highlight)
796 ;; backward-compatibility alias
797 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
798 (put 'gnus-header-from-face 'obsolete-face "22.1")
799
800 (defface gnus-header-subject
801   '((((class color)
802       (background dark))
803      (:foreground "SeaGreen1"))
804     (((class color)
805       (background light))
806      (:foreground "red4"))
807     (t
808      (:bold t :italic t)))
809   "Face used for displaying subject headers."
810   :group 'gnus-article-headers
811   :group 'gnus-article-highlight)
812 ;; backward-compatibility alias
813 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
814 (put 'gnus-header-subject-face 'obsolete-face "22.1")
815
816 (defface gnus-header-newsgroups
817   '((((class color)
818       (background dark))
819      (:foreground "yellow" :italic t))
820     (((class color)
821       (background light))
822      (:foreground "MidnightBlue" :italic t))
823     (t
824      (:italic t)))
825   "Face used for displaying newsgroups headers.
826 In the default setup this face is only used for crossposted
827 articles."
828   :group 'gnus-article-headers
829   :group 'gnus-article-highlight)
830 ;; backward-compatibility alias
831 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
832 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
833
834 (defface gnus-header-name
835   '((((class color)
836       (background dark))
837      (:foreground "SpringGreen2"))
838     (((class color)
839       (background light))
840      (:foreground "maroon"))
841     (t
842      (:bold t)))
843   "Face used for displaying header names."
844   :group 'gnus-article-headers
845   :group 'gnus-article-highlight)
846 ;; backward-compatibility alias
847 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
848 (put 'gnus-header-name-face 'obsolete-face "22.1")
849
850 (defface gnus-header-content
851   '((((class color)
852       (background dark))
853      (:foreground "SpringGreen1" :italic t))
854     (((class color)
855       (background light))
856      (:foreground "indianred4" :italic t))
857     (t
858      (:italic t)))  "Face used for displaying header content."
859   :group 'gnus-article-headers
860   :group 'gnus-article-highlight)
861 ;; backward-compatibility alias
862 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
863 (put 'gnus-header-content-face 'obsolete-face "22.1")
864
865 (defcustom gnus-header-face-alist
866   '(("From" nil gnus-header-from)
867     ("Subject" nil gnus-header-subject)
868     ("Newsgroups:.*," nil gnus-header-newsgroups)
869     ("" gnus-header-name gnus-header-content))
870   "*Controls highlighting of article headers.
871
872 An alist of the form (HEADER NAME CONTENT).
873
874 HEADER is a regular expression which should match the name of a
875 header and NAME and CONTENT are either face names or nil.
876
877 The name of each header field will be displayed using the face
878 specified by the first element in the list where HEADER matches
879 the header name and NAME is non-nil.  Similarly, the content will
880 be displayed by the first non-nil matching CONTENT face."
881   :group 'gnus-article-headers
882   :group 'gnus-article-highlight
883   :type '(repeat (list (regexp :tag "Header")
884                        (choice :tag "Name"
885                                (item :tag "skip" nil)
886                                (face :value default))
887                        (choice :tag "Content"
888                                (item :tag "skip" nil)
889                                (face :value default)))))
890
891 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
892                                           '((xface . (:face gnus-x-face)))
893                                         '((pbm . (:face gnus-x-face))
894                                           (png . nil)))
895   "Alist of image types and properties applied to Face and X-Face images.
896 Here are examples:
897
898 ;; Specify the altitude of Face images in the From header.
899 \(setq gnus-face-properties-alist
900       '((pbm . (:face gnus-x-face :ascent 80))
901         (png . (:ascent 80))))
902
903 ;; Show Face images as pressed buttons.
904 \(setq gnus-face-properties-alist
905       '((pbm . (:face gnus-x-face :relief -2))
906         (png . (:relief -2))))
907
908 See the manual for the valid properties for various image types.
909 Currently, `pbm' is used for X-Face images and `png' is used for Face
910 images in Emacs.  Only the `:face' property is effective on the `xface'
911 image type in XEmacs if it is built with the libcompface library."
912   :version "23.1" ;; No Gnus
913   :group 'gnus-article-headers
914   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
915
916 (defcustom gnus-article-decode-hook
917   '(article-decode-charset article-decode-encoded-words
918                            article-decode-group-name article-decode-idna-rhs)
919   "*Hook run to decode charsets in articles."
920   :group 'gnus-article-headers
921   :type 'hook)
922
923 (defcustom gnus-display-mime-function 'gnus-display-mime
924   "Function to display MIME articles."
925   :group 'gnus-article-mime
926   :type 'function)
927
928 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
929   "Function used to decode headers.")
930
931 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
932   "Function used to decode addresses.")
933
934 (defvar gnus-article-dumbquotes-map
935   '(("\200" "EUR")
936     ("\202" ",")
937     ("\203" "f")
938     ("\204" ",,")
939     ("\205" "...")
940     ("\213" "<")
941     ("\214" "OE")
942     ("\221" "`")
943     ("\222" "'")
944     ("\223" "``")
945     ("\224" "\"")
946     ("\225" "*")
947     ("\226" "-")
948     ("\227" "--")
949     ("\230" "~")
950     ("\231" "(TM)")
951     ("\233" ">")
952     ("\234" "oe")
953     ("\264" "'"))
954   "Table for MS-to-Latin1 translation.")
955
956 (defcustom gnus-ignored-mime-types nil
957   "List of MIME types that should be ignored by Gnus."
958   :version "21.1"
959   :group 'gnus-article-mime
960   :type '(repeat regexp))
961
962 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
963   "List of MIME types that should not be given buttons when rendered inline.
964 See also `gnus-buttonized-mime-types' which may override this variable.
965 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
966   :version "21.1"
967   :group 'gnus-article-mime
968   :type '(repeat regexp))
969
970 (defcustom gnus-buttonized-mime-types nil
971   "List of MIME types that should be given buttons when rendered inline.
972 If set, this variable overrides `gnus-unbuttonized-mime-types'.
973 To see e.g. security buttons you could set this to
974 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
975 this list to display radio buttons that allow you to choose one of two
976 media types those mails include.  See also `mm-discouraged-alternatives'.
977 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
978   :version "22.1"
979   :group 'gnus-article-mime
980   :type '(repeat regexp))
981
982 (defcustom gnus-inhibit-mime-unbuttonizing nil
983   "If non-nil, all MIME parts get buttons.
984 When nil (the default value), then some MIME parts do not get buttons,
985 as described by the variables `gnus-buttonized-mime-types' and
986 `gnus-unbuttonized-mime-types'."
987   :version "22.1"
988   :group 'gnus-article-mime
989   :type 'boolean)
990
991 (defcustom gnus-body-boundary-delimiter "_"
992   "String used to delimit header and body.
993 This variable is used by `gnus-article-treat-body-boundary' which can
994 be controlled by `gnus-treat-body-boundary'."
995   :version "22.1"
996   :group 'gnus-article-various
997   :type '(choice (item :tag "None" :value nil)
998                  string))
999
1000 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
1001                                   "/usr/share/picons")
1002   "Defines the location of the faces database.
1003 For information on obtaining this database of pretty pictures, please
1004 see http://www.cs.indiana.edu/picons/ftp/index.html"
1005   :version "22.1"
1006   :type '(repeat directory)
1007   :link '(url-link :tag "download"
1008                    "http://www.cs.indiana.edu/picons/ftp/index.html")
1009   :link '(custom-manual "(gnus)Picons")
1010   :group 'gnus-picon)
1011
1012 (defun gnus-picons-installed-p ()
1013   "Say whether picons are installed on your machine."
1014   (let ((installed nil))
1015     (dolist (database gnus-picon-databases)
1016       (when (file-exists-p database)
1017         (setq installed t)))
1018     installed))
1019
1020 (defcustom gnus-article-mime-part-function nil
1021   "Function called with a MIME handle as the argument.
1022 This is meant for people who want to do something automatic based
1023 on parts -- for instance, adding Vcard info to a database."
1024   :group 'gnus-article-mime
1025   :type '(choice (const nil)
1026                  function))
1027
1028 (defcustom gnus-mime-multipart-functions nil
1029   "An alist of MIME types to functions to display them."
1030   :version "21.1"
1031   :group 'gnus-article-mime
1032   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1033
1034 (defcustom gnus-article-date-lapsed-new-header nil
1035   "Whether the X-Sent and Date headers can coexist.
1036 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
1037 either replace the old \"Date:\" header (if this variable is nil), or
1038 be added below it (otherwise)."
1039   :version "21.1"
1040   :group 'gnus-article-headers
1041   :type 'boolean)
1042
1043 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1044   "Function called with a MIME handle as the argument.
1045 This is meant for people who want to view first matched part.
1046 For `undisplayed-alternative' (default), the first undisplayed
1047 part or alternative part is used.  For `undisplayed', the first
1048 undisplayed part is used.  For a function, the first part which
1049 the function return t is used.  For nil, the first part is
1050 used."
1051   :version "21.1"
1052   :group 'gnus-article-mime
1053   :type '(choice
1054           (item :tag "first" :value nil)
1055           (item :tag "undisplayed" :value undisplayed)
1056           (item :tag "undisplayed or alternative"
1057                 :value undisplayed-alternative)
1058           (function)))
1059
1060 (defcustom gnus-mime-action-alist
1061   '(("save to file" . gnus-mime-save-part)
1062     ("save and strip" . gnus-mime-save-part-and-strip)
1063     ("replace with file" . gnus-mime-replace-part)
1064     ("delete part" . gnus-mime-delete-part)
1065     ("display as text" . gnus-mime-inline-part)
1066     ("view the part" . gnus-mime-view-part)
1067     ("pipe to command" . gnus-mime-pipe-part)
1068     ("toggle display" . gnus-article-press-button)
1069     ("toggle display" . gnus-article-view-part-as-charset)
1070     ("view as type" . gnus-mime-view-part-as-type)
1071     ("view internally" . gnus-mime-view-part-internally)
1072     ("view externally" . gnus-mime-view-part-externally))
1073   "An alist of actions that run on the MIME attachment."
1074   :group 'gnus-article-mime
1075   :type '(repeat (cons (string :tag "name")
1076                        (function))))
1077
1078 (defcustom gnus-auto-select-part 1
1079   "Advance to next MIME part when deleting or stripping parts.
1080
1081 When 0, point will be placed on the same part as before.  When
1082 positive (negative), move point forward (backwards) this many
1083 parts.  When nil, redisplay article."
1084   :version "23.1" ;; No Gnus
1085   :group 'gnus-article-mime
1086   :type '(choice (const nil :tag "Redisplay article.")
1087                  (const 1 :tag "Next part.")
1088                  (const 0 :tag "Current part.")
1089                  integer))
1090
1091 ;;;
1092 ;;; The treatment variables
1093 ;;;
1094
1095 (defvar gnus-part-display-hook nil
1096   "Hook called on parts that are to receive treatment.")
1097
1098 (defvar gnus-article-treat-custom
1099   '(choice (const :tag "Off" nil)
1100            (const :tag "On" t)
1101            (const :tag "Header" head)
1102            (const :tag "First" first)
1103            (const :tag "Last" last)
1104            (integer :tag "Less")
1105            (repeat :tag "Groups" regexp)
1106            (sexp :tag "Predicate")))
1107
1108 (defvar gnus-article-treat-head-custom
1109   '(choice (const :tag "Off" nil)
1110            (const :tag "Header" head)))
1111
1112 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1113                                    "text/x-patch")
1114   "Parts to treat.")
1115
1116 (defvar gnus-inhibit-treatment nil
1117   "Whether to inhibit treatment.")
1118
1119 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1120   "Highlight the signature.
1121 Valid values are nil, t, `head', `first', `last', an integer or a
1122 predicate.  See Info node `(gnus)Customizing Articles'."
1123   :group 'gnus-article-treat
1124   :link '(custom-manual "(gnus)Customizing Articles")
1125   :type gnus-article-treat-custom)
1126 (put 'gnus-treat-highlight-signature 'highlight t)
1127
1128 (defcustom gnus-treat-buttonize 100000
1129   "Add buttons.
1130 Valid values are nil, t, `head', `first', `last', an integer or a
1131 predicate.  See Info node `(gnus)Customizing Articles'."
1132   :group 'gnus-article-treat
1133   :link '(custom-manual "(gnus)Customizing Articles")
1134   :type gnus-article-treat-custom)
1135 (put 'gnus-treat-buttonize 'highlight t)
1136
1137 (defcustom gnus-treat-buttonize-head 'head
1138   "Add buttons to the head.
1139 Valid values are nil, t, `head', `first', `last', an integer or a
1140 predicate.  See Info node `(gnus)Customizing Articles'."
1141   :group 'gnus-article-treat
1142   :link '(custom-manual "(gnus)Customizing Articles")
1143   :type gnus-article-treat-head-custom)
1144 (put 'gnus-treat-buttonize-head 'highlight t)
1145
1146 (defcustom gnus-treat-emphasize 50000
1147   "Emphasize text.
1148 Valid values are nil, t, `head', `first', `last', an integer or a
1149 predicate.  See Info node `(gnus)Customizing Articles'."
1150   :group 'gnus-article-treat
1151   :link '(custom-manual "(gnus)Customizing Articles")
1152   :type gnus-article-treat-custom)
1153 (put 'gnus-treat-emphasize 'highlight t)
1154
1155 (defcustom gnus-treat-strip-cr nil
1156   "Remove carriage returns.
1157 Valid values are nil, t, `head', `first', `last', an integer or a
1158 predicate.  See Info node `(gnus)Customizing Articles'."
1159   :version "22.1"
1160   :group 'gnus-article-treat
1161   :link '(custom-manual "(gnus)Customizing Articles")
1162   :type gnus-article-treat-custom)
1163
1164 (defcustom gnus-treat-unsplit-urls nil
1165   "Remove newlines from within URLs.
1166 Valid values are nil, t, `head', `first', `last', an integer or a
1167 predicate.  See Info node `(gnus)Customizing Articles'."
1168   :version "22.1"
1169   :group 'gnus-article-treat
1170   :link '(custom-manual "(gnus)Customizing Articles")
1171   :type gnus-article-treat-custom)
1172
1173 (defcustom gnus-treat-leading-whitespace nil
1174   "Remove leading whitespace in headers.
1175 Valid values are nil, t, `head', `first', `last', an integer or a
1176 predicate.  See Info node `(gnus)Customizing Articles'."
1177   :version "22.1"
1178   :group 'gnus-article-treat
1179   :link '(custom-manual "(gnus)Customizing Articles")
1180   :type gnus-article-treat-custom)
1181
1182 (defcustom gnus-treat-hide-headers 'head
1183   "Hide headers.
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-head-custom)
1189
1190 (defcustom gnus-treat-hide-boring-headers nil
1191   "Hide boring headers.
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-head-custom)
1197
1198 (defcustom gnus-treat-hide-signature nil
1199   "Hide the signature.
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-fill-article nil
1207   "Fill the article.
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-hide-citation nil
1215   "Hide cited text.
1216 Valid values are nil, t, `head', `first', `last', an integer or a
1217 predicate.  See Info node `(gnus)Customizing Articles'."
1218   :group 'gnus-article-treat
1219   :link '(custom-manual "(gnus)Customizing Articles")
1220   :type gnus-article-treat-custom)
1221
1222 (defcustom gnus-treat-hide-citation-maybe nil
1223   "Hide cited text.
1224 Valid values are nil, t, `head', `first', `last', an integer or a
1225 predicate.  See Info node `(gnus)Customizing Articles'."
1226   :group 'gnus-article-treat
1227   :link '(custom-manual "(gnus)Customizing Articles")
1228   :type gnus-article-treat-custom)
1229
1230 (defcustom gnus-treat-strip-list-identifiers 'head
1231   "Strip list identifiers from `gnus-list-identifiers`.
1232 Valid values are nil, t, `head', `first', `last', an integer or a
1233 predicate.  See Info node `(gnus)Customizing Articles'."
1234   :version "21.1"
1235   :group 'gnus-article-treat
1236   :link '(custom-manual "(gnus)Customizing Articles")
1237   :type gnus-article-treat-custom)
1238
1239 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1240                         "Gnus 5.10 (Emacs 22.1)")
1241
1242 (defcustom gnus-treat-strip-pem nil
1243   "Strip PEM signatures.
1244 Valid values are nil, t, `head', `first', `last', an integer or a
1245 predicate.  See Info node `(gnus)Customizing Articles'."
1246   :group 'gnus-article-treat
1247   :link '(custom-manual "(gnus)Customizing Articles")
1248   :type gnus-article-treat-custom)
1249
1250 (defcustom gnus-treat-strip-banner t
1251   "Strip banners from articles.
1252 The banner to be stripped is specified in the `banner' group parameter.
1253 Valid values are nil, t, `head', `first', `last', an integer or a
1254 predicate.  See Info node `(gnus)Customizing Articles'."
1255   :group 'gnus-article-treat
1256   :link '(custom-manual "(gnus)Customizing Articles")
1257   :type gnus-article-treat-custom)
1258
1259 (defcustom gnus-treat-highlight-headers 'head
1260   "Highlight the headers.
1261 Valid values are nil, t, `head', `first', `last', an integer or a
1262 predicate.  See Info node `(gnus)Customizing Articles'."
1263   :group 'gnus-article-treat
1264   :link '(custom-manual "(gnus)Customizing Articles")
1265   :type gnus-article-treat-head-custom)
1266 (put 'gnus-treat-highlight-headers 'highlight t)
1267
1268 (defcustom gnus-treat-highlight-citation t
1269   "Highlight cited text.
1270 Valid values are nil, t, `head', `first', `last', an integer or a
1271 predicate.  See Info node `(gnus)Customizing Articles'."
1272   :group 'gnus-article-treat
1273   :link '(custom-manual "(gnus)Customizing Articles")
1274   :type gnus-article-treat-custom)
1275 (put 'gnus-treat-highlight-citation 'highlight t)
1276
1277 (defcustom gnus-treat-date-ut nil
1278   "Display the Date in UT (GMT).
1279 Valid values are nil, t, `head', `first', `last', an integer or a
1280 predicate.  See Info node `(gnus)Customizing Articles'."
1281   :group 'gnus-article-treat
1282   :link '(custom-manual "(gnus)Customizing Articles")
1283   :type gnus-article-treat-head-custom)
1284
1285 (defcustom gnus-treat-date-local nil
1286   "Display the Date in the local timezone.
1287 Valid values are nil, t, `head', `first', `last', an integer or a
1288 predicate.  See Info node `(gnus)Customizing Articles'."
1289   :group 'gnus-article-treat
1290   :link '(custom-manual "(gnus)Customizing Articles")
1291   :type gnus-article-treat-head-custom)
1292
1293 (defcustom gnus-treat-date-english nil
1294   "Display the Date in a format that can be read aloud in English.
1295 Valid values are nil, t, `head', `first', `last', an integer or a
1296 predicate.  See Info node `(gnus)Customizing Articles'."
1297   :version "22.1"
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-lapsed nil
1303   "Display the Date header in a way that says how much time has elapsed.
1304 Valid values are nil, t, `head', `first', `last', an integer or a
1305 predicate.  See Info node `(gnus)Customizing Articles'."
1306   :group 'gnus-article-treat
1307   :link '(custom-manual "(gnus)Customizing Articles")
1308   :type gnus-article-treat-head-custom)
1309
1310 (defcustom gnus-treat-date-original nil
1311   "Display the date in the original timezone.
1312 Valid values are nil, t, `head', `first', `last', an integer or a
1313 predicate.  See Info node `(gnus)Customizing Articles'."
1314   :group 'gnus-article-treat
1315   :link '(custom-manual "(gnus)Customizing Articles")
1316   :type gnus-article-treat-head-custom)
1317
1318 (defcustom gnus-treat-date-iso8601 nil
1319   "Display the date in the ISO8601 format.
1320 Valid values are nil, t, `head', `first', `last', an integer or a
1321 predicate.  See Info node `(gnus)Customizing Articles'."
1322   :version "21.1"
1323   :group 'gnus-article-treat
1324   :link '(custom-manual "(gnus)Customizing Articles")
1325   :type gnus-article-treat-head-custom)
1326
1327 (defcustom gnus-treat-date-user-defined nil
1328   "Display the date in a user-defined format.
1329 The format is defined by the `gnus-article-time-format' variable.
1330 Valid values are nil, t, `head', `first', `last', an integer or a
1331 predicate.  See Info node `(gnus)Customizing Articles'."
1332   :group 'gnus-article-treat
1333   :link '(custom-manual "(gnus)Customizing Articles")
1334   :type gnus-article-treat-head-custom)
1335
1336 (defcustom gnus-treat-strip-headers-in-body t
1337   "Strip the X-No-Archive header line from the beginning of the body.
1338 Valid values are nil, t, `head', `first', `last', an integer or a
1339 predicate.  See Info node `(gnus)Customizing Articles'."
1340   :version "21.1"
1341   :group 'gnus-article-treat
1342   :link '(custom-manual "(gnus)Customizing Articles")
1343   :type gnus-article-treat-custom)
1344
1345 (defcustom gnus-treat-strip-trailing-blank-lines nil
1346   "Strip trailing blank lines.
1347 Valid values are nil, t, `head', `first', `last', an integer or a
1348 predicate.  See Info node `(gnus)Customizing Articles'.
1349
1350 When set to t, it also strips trailing blanks in all MIME parts.
1351 Consider to use `last' instead."
1352   :group 'gnus-article-treat
1353   :link '(custom-manual "(gnus)Customizing Articles")
1354   :type gnus-article-treat-custom)
1355
1356 (defcustom gnus-treat-strip-leading-blank-lines nil
1357   "Strip leading blank lines.
1358 Valid values are nil, t, `head', `first', `last', an integer or a
1359 predicate.  See Info node `(gnus)Customizing Articles'.
1360
1361 When set to t, it also strips trailing blanks in all MIME parts."
1362   :group 'gnus-article-treat
1363   :link '(custom-manual "(gnus)Customizing Articles")
1364   :type gnus-article-treat-custom)
1365
1366 (defcustom gnus-treat-strip-multiple-blank-lines nil
1367   "Strip multiple blank lines.
1368 Valid values are nil, t, `head', `first', `last', an integer or a
1369 predicate.  See Info node `(gnus)Customizing Articles'."
1370   :group 'gnus-article-treat
1371   :link '(custom-manual "(gnus)Customizing Articles")
1372   :type gnus-article-treat-custom)
1373
1374 (defcustom gnus-treat-unfold-headers 'head
1375   "Unfold folded header lines.
1376 Valid values are nil, t, `head', `first', `last', an integer or a
1377 predicate.  See Info node `(gnus)Customizing Articles'."
1378   :version "22.1"
1379   :group 'gnus-article-treat
1380   :link '(custom-manual "(gnus)Customizing Articles")
1381   :type gnus-article-treat-custom)
1382
1383 (defcustom gnus-article-unfold-long-headers nil
1384   "If non-nil, allow unfolding headers even if the header is long.
1385 If it is a regexp, only long headers matching this regexp are unfolded.
1386 If it is t, all long headers are unfolded.
1387
1388 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1389   :version "23.1" ;; No Gnus
1390   :group 'gnus-article-treat
1391   :type '(choice (const nil)
1392                  (const :tag "all" t)
1393                  (regexp)))
1394
1395 (defcustom gnus-treat-fold-headers nil
1396   "Fold headers.
1397 Valid values are nil, t, `head', `first', `last', an integer or a
1398 predicate.  See Info node `(gnus)Customizing Articles'."
1399   :version "22.1"
1400   :group 'gnus-article-treat
1401   :link '(custom-manual "(gnus)Customizing Articles")
1402   :type gnus-article-treat-custom)
1403
1404 (defcustom gnus-treat-fold-newsgroups 'head
1405   "Fold the Newsgroups and Followup-To headers.
1406 Valid values are nil, t, `head', `first', `last', an integer or a
1407 predicate.  See Info node `(gnus)Customizing Articles'."
1408   :version "22.1"
1409   :group 'gnus-article-treat
1410   :link '(custom-manual "(gnus)Customizing Articles")
1411   :type gnus-article-treat-custom)
1412
1413 (defcustom gnus-treat-overstrike t
1414   "Treat overstrike highlighting.
1415 Valid values are nil, t, `head', `first', `last', an integer or a
1416 predicate.  See Info node `(gnus)Customizing Articles'."
1417   :group 'gnus-article-treat
1418   :link '(custom-manual "(gnus)Customizing Articles")
1419   :type gnus-article-treat-custom)
1420 (put 'gnus-treat-overstrike 'highlight t)
1421
1422 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1423   "Treat ANSI SGR control sequences.
1424 Valid values are nil, t, `head', `first', `last', an integer or a
1425 predicate.  See Info node `(gnus)Customizing Articles'."
1426   :group 'gnus-article-treat
1427   :link '(custom-manual "(gnus)Customizing Articles")
1428   :type gnus-article-treat-custom)
1429
1430 (make-obsolete-variable 'gnus-treat-display-xface
1431                         'gnus-treat-display-x-face "22.1")
1432
1433 (defcustom gnus-treat-display-x-face
1434   (and (not noninteractive)
1435        (gnus-image-type-available-p 'xbm)
1436        (if (featurep 'xemacs)
1437            (featurep 'xface)
1438          (condition-case nil
1439              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1440                   (executable-find "icontopbm"))
1441            ;; shell-command-to-string may signal an error, e.g. if
1442            ;; shell-file-name is not found.
1443            (error nil)))
1444        'head)
1445   "Display X-Face headers.
1446 Valid values are nil and `head'.
1447 See Info node `(gnus)Customizing Articles' and Info node
1448 `(gnus)X-Face' for details."
1449   :group 'gnus-article-treat
1450   :version "21.1"
1451   :link '(custom-manual "(gnus)Customizing Articles")
1452   :link '(custom-manual "(gnus)X-Face")
1453   :type gnus-article-treat-head-custom
1454   :set (lambda (symbol value)
1455          (set-default
1456           symbol
1457           (cond ((or (boundp symbol) (get symbol 'saved-value))
1458                  value)
1459                 ((boundp 'gnus-treat-display-xface)
1460                  (message "\
1461 ** gnus-treat-display-xface is an obsolete variable;\
1462  use gnus-treat-display-x-face instead")
1463                  (default-value 'gnus-treat-display-xface))
1464                 ((get 'gnus-treat-display-xface 'saved-value)
1465                  (message "\
1466 ** gnus-treat-display-xface is an obsolete variable;\
1467  use gnus-treat-display-x-face instead")
1468                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1469                 (t
1470                  value)))))
1471 (put 'gnus-treat-display-x-face 'highlight t)
1472
1473 (defcustom gnus-treat-display-face
1474   (and (not noninteractive)
1475        (gnus-image-type-available-p 'png)
1476        'head)
1477   "Display Face headers.
1478 Valid values are nil, t, `head', `first', `last', an integer or a
1479 predicate.  See Info node `(gnus)Customizing Articles' and Info
1480 node `(gnus)Face' for details."
1481   :group 'gnus-article-treat
1482   :version "22.1"
1483   :link '(custom-manual "(gnus)Customizing Articles")
1484   :link '(custom-manual "(gnus)X-Face")
1485   :type gnus-article-treat-head-custom)
1486 (put 'gnus-treat-display-face 'highlight t)
1487
1488 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1489   "Display smileys.
1490 Valid values are nil, t, `head', `first', `last', an integer or a
1491 predicate.  See Info node `(gnus)Customizing Articles' and Info
1492 node `(gnus)Smileys' for details."
1493   :group 'gnus-article-treat
1494   :version "21.1"
1495   :link '(custom-manual "(gnus)Customizing Articles")
1496   :link '(custom-manual "(gnus)Smileys")
1497   :type gnus-article-treat-custom)
1498 (put 'gnus-treat-display-smileys 'highlight t)
1499
1500 (defcustom gnus-treat-from-picon
1501   (if (and (gnus-image-type-available-p 'xpm)
1502            (gnus-picons-installed-p))
1503       'head nil)
1504   "Display picons in the From header.
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-from-picon 'highlight t)
1515
1516 (defcustom gnus-treat-mail-picon
1517   (if (and (gnus-image-type-available-p 'xpm)
1518            (gnus-picons-installed-p))
1519       'head nil)
1520   "Display picons in To and Cc 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-mail-picon 'highlight t)
1531
1532 (defcustom gnus-treat-newsgroups-picon
1533   (if (and (gnus-image-type-available-p 'xpm)
1534            (gnus-picons-installed-p))
1535       'head nil)
1536   "Display picons in the Newsgroups and Followup-To headers.
1537 Valid values are nil, t, `head', `first', `last', an integer or a
1538 predicate.  See Info node `(gnus)Customizing Articles' and Info
1539 node `(gnus)Picons' for details."
1540   :version "22.1"
1541   :group 'gnus-article-treat
1542   :group 'gnus-picon
1543   :link '(custom-manual "(gnus)Customizing Articles")
1544   :link '(custom-manual "(gnus)Picons")
1545   :type gnus-article-treat-head-custom)
1546 (put 'gnus-treat-newsgroups-picon 'highlight t)
1547
1548 (defcustom gnus-treat-from-gravatar nil
1549   "Display gravatars in the From header.
1550 Valid values are nil, t, `head', `first', `last', an integer or a
1551 predicate.  See Info node `(gnus)Customizing Articles' and Info
1552 node `(gnus)Gravatars' for details."
1553   :version "24.1"
1554   :group 'gnus-article-treat
1555   :group 'gnus-gravatar
1556   :link '(custom-manual "(gnus)Customizing Articles")
1557   :link '(custom-manual "(gnus)Gravatars")
1558   :type gnus-article-treat-head-custom)
1559 (put 'gnus-treat-from-gravatar 'highlight t)
1560
1561 (defcustom gnus-treat-mail-gravatar nil
1562   "Display gravatars in To and Cc headers.
1563 Valid values are nil, t, `head', `first', `last', an integer or a
1564 predicate.  See Info node `(gnus)Customizing Articles' and Info
1565 node `(gnus)Gravatars' for details."
1566   :version "24.1"
1567   :group 'gnus-article-treat
1568   :group 'gnus-gravatar
1569   :link '(custom-manual "(gnus)Customizing Articles")
1570   :link '(custom-manual "(gnus)Gravatars")
1571   :type gnus-article-treat-head-custom)
1572 (put 'gnus-treat-mail-gravatar 'highlight t)
1573
1574 (defcustom gnus-treat-body-boundary
1575   (if (or gnus-treat-newsgroups-picon
1576           gnus-treat-mail-picon
1577           gnus-treat-from-picon
1578           gnus-treat-from-gravatar
1579           gnus-treat-mail-gravatar)
1580       ;; If there's much decoration, the user might prefer a boundery.
1581       'head
1582     nil)
1583   "Draw a boundary at the end of the headers.
1584 Valid values are nil and `head'.
1585 See Info node `(gnus)Customizing Articles' for details."
1586   :version "22.1"
1587   :group 'gnus-article-treat
1588   :link '(custom-manual "(gnus)Customizing Articles")
1589   :type gnus-article-treat-head-custom)
1590
1591 (defcustom gnus-treat-capitalize-sentences nil
1592   "Capitalize sentence-starting words.
1593 Valid values are nil, t, `head', `first', `last', an integer or a
1594 predicate.  See Info node `(gnus)Customizing Articles'."
1595   :version "21.1"
1596   :group 'gnus-article-treat
1597   :link '(custom-manual "(gnus)Customizing Articles")
1598   :type gnus-article-treat-custom)
1599
1600 (defcustom gnus-treat-wash-html nil
1601   "Format as HTML.
1602 Valid values are nil, t, `head', `first', `last', an integer or a
1603 predicate.  See Info node `(gnus)Customizing Articles'."
1604   :version "22.1"
1605   :group 'gnus-article-treat
1606   :link '(custom-manual "(gnus)Customizing Articles")
1607   :type gnus-article-treat-custom)
1608
1609 (defcustom gnus-treat-fill-long-lines nil
1610   "Fill long lines.
1611 Valid values are nil, t, `head', `first', `last', an integer or a
1612 predicate.  See Info node `(gnus)Customizing Articles'."
1613   :group 'gnus-article-treat
1614   :link '(custom-manual "(gnus)Customizing Articles")
1615   :type gnus-article-treat-custom)
1616
1617 (defcustom gnus-treat-play-sounds nil
1618   "Play sounds.
1619 Valid values are nil, t, `head', `first', `last', an integer or a
1620 predicate.  See Info node `(gnus)Customizing Articles'."
1621   :version "21.1"
1622   :group 'gnus-article-treat
1623   :link '(custom-manual "(gnus)Customizing Articles")
1624   :type gnus-article-treat-custom)
1625
1626 (defcustom gnus-treat-x-pgp-sig nil
1627   "Verify X-PGP-Sig.
1628 To automatically treat X-PGP-Sig, set it to head.
1629 Valid values are nil, t, `head', `first', `last', an integer or a
1630 predicate.  See Info node `(gnus)Customizing Articles'."
1631   :version "22.1"
1632   :group 'gnus-article-treat
1633   :group 'mime-security
1634   :link '(custom-manual "(gnus)Customizing Articles")
1635   :type gnus-article-treat-custom)
1636
1637 (defvar gnus-article-encrypt-protocol-alist
1638   '(("PGP" . mml2015-self-encrypt)))
1639
1640 ;; Set to nil if more than one protocol added to
1641 ;; gnus-article-encrypt-protocol-alist.
1642 (defcustom gnus-article-encrypt-protocol "PGP"
1643   "The protocol used for encrypt articles.
1644 It is a string, such as \"PGP\". If nil, ask user."
1645   :version "22.1"
1646   :type 'string
1647   :group 'mime-security)
1648
1649 (defvar gnus-article-wash-function nil
1650   "Function used for converting HTML into text.")
1651
1652 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1653                               (mm-coding-system-p 'utf-8)
1654                               (executable-find idna-program))
1655   "Whether IDNA decoding of headers is used when viewing messages.
1656 This requires GNU Libidn, and by default only enabled if it is found."
1657   :version "22.1"
1658   :group 'gnus-article-headers
1659   :type 'boolean)
1660
1661 (defcustom gnus-article-over-scroll nil
1662   "If non-nil, allow scrolling the article buffer even when there no more text."
1663   :version "22.1"
1664   :group 'gnus-article
1665   :type 'boolean)
1666
1667 ;;; Internal variables
1668
1669 (defvar gnus-english-month-names
1670   '("January" "February" "March" "April" "May" "June" "July" "August"
1671     "September" "October" "November" "December"))
1672
1673 (defvar article-goto-body-goes-to-point-min-p nil)
1674 (defvar gnus-article-wash-types nil)
1675 (defvar gnus-article-emphasis-alist nil)
1676 (defvar gnus-article-image-alist nil)
1677
1678 (defvar gnus-article-mime-handle-alist-1 nil)
1679 (defvar gnus-treatment-function-alist
1680   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1681     (gnus-treat-strip-banner gnus-article-strip-banner)
1682     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1683     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1684     (gnus-treat-buttonize gnus-article-add-buttons)
1685     (gnus-treat-fill-article gnus-article-fill-cited-article)
1686     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1687     (gnus-treat-strip-cr gnus-article-remove-cr)
1688     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1689     (gnus-treat-date-ut gnus-article-date-ut)
1690     (gnus-treat-date-local gnus-article-date-local)
1691     (gnus-treat-date-english gnus-article-date-english)
1692     (gnus-treat-date-original gnus-article-date-original)
1693     (gnus-treat-date-user-defined gnus-article-date-user)
1694     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1695     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1696     (gnus-treat-display-x-face gnus-article-display-x-face)
1697     (gnus-treat-display-face gnus-article-display-face)
1698     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1699     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1700     (gnus-treat-hide-signature gnus-article-hide-signature)
1701     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1702     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1703     (gnus-treat-strip-pem gnus-article-hide-pem)
1704     (gnus-treat-from-picon gnus-treat-from-picon)
1705     (gnus-treat-mail-picon gnus-treat-mail-picon)
1706     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1707     (gnus-treat-from-gravatar gnus-treat-from-gravatar)
1708     (gnus-treat-mail-gravatar gnus-treat-mail-gravatar)
1709     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1710     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1711     (gnus-treat-strip-trailing-blank-lines
1712      gnus-article-remove-trailing-blank-lines)
1713     (gnus-treat-strip-leading-blank-lines
1714      gnus-article-strip-leading-blank-lines)
1715     (gnus-treat-strip-multiple-blank-lines
1716      gnus-article-strip-multiple-blank-lines)
1717     (gnus-treat-overstrike gnus-article-treat-overstrike)
1718     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1719     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1720     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1721     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1722     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1723     (gnus-treat-display-smileys gnus-treat-smiley)
1724     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1725     (gnus-treat-wash-html gnus-article-wash-html)
1726     (gnus-treat-emphasize gnus-article-emphasize)
1727     (gnus-treat-hide-citation gnus-article-hide-citation)
1728     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1729     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1730     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1731     (gnus-treat-play-sounds gnus-earcon-display)))
1732
1733 (defvar gnus-article-mime-handle-alist nil)
1734 (defvar article-lapsed-timer nil)
1735 (defvar gnus-article-current-summary nil)
1736
1737 (defvar gnus-article-mode-syntax-table
1738   (let ((table (copy-syntax-table text-mode-syntax-table)))
1739     ;; This causes the citation match run O(2^n).
1740     ;; (modify-syntax-entry ?- "w" table)
1741     (modify-syntax-entry ?> ")<" table)
1742     (modify-syntax-entry ?< "(>" table)
1743     ;; make M-. in article buffers work for `foo' strings
1744     (modify-syntax-entry ?' " " table)
1745     (modify-syntax-entry ?` " " table)
1746     table)
1747   "Syntax table used in article mode buffers.
1748 Initialized from `text-mode-syntax-table.")
1749
1750 (defvar gnus-save-article-buffer nil)
1751
1752 (defvar gnus-number-of-articles-to-be-saved nil)
1753
1754 (defvar gnus-inhibit-hiding nil)
1755
1756 (defvar gnus-article-edit-mode nil)
1757
1758 ;;; Macros for dealing with the article buffer.
1759
1760 (defmacro gnus-with-article-headers (&rest forms)
1761   `(with-current-buffer gnus-article-buffer
1762      (save-restriction
1763        (let ((inhibit-read-only t)
1764              (inhibit-point-motion-hooks t)
1765              (case-fold-search t))
1766          (article-narrow-to-head)
1767          ,@forms))))
1768
1769 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1770 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1771
1772 (defmacro gnus-with-article-buffer (&rest forms)
1773   `(with-current-buffer gnus-article-buffer
1774      (let ((inhibit-read-only t))
1775        ,@forms)))
1776
1777 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1778 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1779
1780 (defun gnus-article-goto-header (header)
1781   "Go to HEADER, which is a regular expression."
1782   (re-search-forward (concat "^\\(" header "\\):") nil t))
1783
1784 (defsubst gnus-article-hide-text (b e props)
1785   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1786   (gnus-add-text-properties-when 'article-type nil b e props)
1787   (when (memq 'intangible props)
1788     (put-text-property
1789      (max (1- b) (point-min))
1790      b 'intangible (cddr (memq 'intangible props)))))
1791
1792 (defsubst gnus-article-unhide-text (b e)
1793   "Remove hidden text properties from region between B and E."
1794   (remove-text-properties b e gnus-hidden-properties)
1795   (when (memq 'intangible gnus-hidden-properties)
1796     (put-text-property (max (1- b) (point-min))
1797                        b 'intangible nil)))
1798
1799 (defun gnus-article-hide-text-type (b e type)
1800   "Hide text of TYPE between B and E."
1801   (gnus-add-wash-type type)
1802   (gnus-article-hide-text
1803    b e (cons 'article-type (cons type gnus-hidden-properties))))
1804
1805 (defun gnus-article-unhide-text-type (b e type)
1806   "Unhide text of TYPE between B and E."
1807   (gnus-delete-wash-type type)
1808   (remove-text-properties
1809    b e (cons 'article-type (cons type gnus-hidden-properties)))
1810   (when (memq 'intangible gnus-hidden-properties)
1811     (put-text-property (max (1- b) (point-min))
1812                        b 'intangible nil)))
1813
1814 (defun gnus-article-hide-text-of-type (type)
1815   "Hide text of TYPE in the current buffer."
1816   (save-excursion
1817     (let ((b (point-min))
1818           (e (point-max)))
1819       (while (setq b (text-property-any b e 'article-type type))
1820         (add-text-properties b (incf b) gnus-hidden-properties)))))
1821
1822 (defun gnus-article-delete-text-of-type (type)
1823   "Delete text of TYPE in the current buffer."
1824   (save-excursion
1825     (let ((b (point-min)))
1826       (if (eq type 'multipart)
1827           ;; Remove MIME buttons associated with multipart/alternative parts.
1828           (progn
1829             (goto-char b)
1830             (while (if (get-text-property (point) 'gnus-part)
1831                        (setq b (point))
1832                      (when (setq b (next-single-property-change (point)
1833                                                                 'gnus-part))
1834                        (goto-char b)
1835                        t))
1836               (end-of-line)
1837               (skip-chars-forward "\n")
1838               (when (eq (get-text-property b 'article-type) 'multipart)
1839                 (delete-region b (point)))))
1840         (while (setq b (text-property-any b (point-max) 'article-type type))
1841           (delete-region
1842            b (or (text-property-not-all b (point-max) 'article-type type)
1843                  (point-max))))))))
1844
1845 (defun gnus-article-delete-invisible-text ()
1846   "Delete all invisible text in the current buffer."
1847   (save-excursion
1848     (let ((b (point-min)))
1849       (while (setq b (text-property-any b (point-max) 'invisible t))
1850         (delete-region
1851          b (or (text-property-not-all b (point-max) 'invisible t)
1852                (point-max)))))))
1853
1854 (defun gnus-article-text-type-exists-p (type)
1855   "Say whether any text of type TYPE exists in the buffer."
1856   (text-property-any (point-min) (point-max) 'article-type type))
1857
1858 (defsubst gnus-article-header-rank ()
1859   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1860   (let ((list gnus-sorted-header-list)
1861         (i 1))
1862     (while list
1863       (if (looking-at (car list))
1864           (setq list nil)
1865         (setq list (cdr list))
1866         (incf i)))
1867       i))
1868
1869 (defun article-hide-headers (&optional arg delete)
1870   "Hide unwanted headers and possibly sort them as well."
1871   (interactive)
1872   ;; This function might be inhibited.
1873   (unless gnus-inhibit-hiding
1874     (let ((inhibit-read-only t)
1875           (case-fold-search t)
1876           (max (1+ (length gnus-sorted-header-list)))
1877           (inhibit-point-motion-hooks t)
1878           (cur (current-buffer))
1879           ignored visible beg)
1880       (save-excursion
1881         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1882         ;; group parameters, so we should go to the summary buffer.
1883         (when (prog1
1884                   (condition-case nil
1885                       (progn (set-buffer gnus-summary-buffer) t)
1886                     (error nil))
1887                 (setq ignored (when (not gnus-visible-headers)
1888                                 (cond ((stringp gnus-ignored-headers)
1889                                        gnus-ignored-headers)
1890                                       ((listp gnus-ignored-headers)
1891                                        (mapconcat 'identity
1892                                                   gnus-ignored-headers
1893                                                   "\\|"))))
1894                       visible (cond ((stringp gnus-visible-headers)
1895                                      gnus-visible-headers)
1896                                     ((and gnus-visible-headers
1897                                           (listp gnus-visible-headers))
1898                                      (mapconcat 'identity
1899                                                 gnus-visible-headers
1900                                                 "\\|")))))
1901           (set-buffer cur))
1902         (save-restriction
1903           ;; First we narrow to just the headers.
1904           (article-narrow-to-head)
1905           ;; Hide any "From " lines at the beginning of (mail) articles.
1906           (while (looking-at "From ")
1907             (forward-line 1))
1908           (unless (bobp)
1909             (delete-region (point-min) (point)))
1910           ;; Then treat the rest of the header lines.
1911           ;; Then we use the two regular expressions
1912           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1913           ;; select which header lines is to remain visible in the
1914           ;; article buffer.
1915           (while (re-search-forward "^[^ \t:]*:" nil t)
1916             (beginning-of-line)
1917             ;; Mark the rank of the header.
1918             (put-text-property
1919              (point) (1+ (point)) 'message-rank
1920              (if (or (and visible (looking-at visible))
1921                      (and ignored
1922                           (not (looking-at ignored))))
1923                  (gnus-article-header-rank)
1924                (+ 2 max)))
1925             (forward-line 1))
1926           (message-sort-headers-1)
1927           (when (setq beg (text-property-any
1928                            (point-min) (point-max) 'message-rank (+ 2 max)))
1929             ;; We delete the unwanted headers.
1930             (gnus-add-wash-type 'headers)
1931             (add-text-properties (point-min) (+ 5 (point-min))
1932                                  '(article-type headers dummy-invisible t))
1933             (delete-region beg (point-max))))))))
1934
1935 (defun article-hide-boring-headers (&optional arg)
1936   "Toggle hiding of headers that aren't very interesting.
1937 If given a negative prefix, always show; if given a positive prefix,
1938 always hide."
1939   (interactive (gnus-article-hidden-arg))
1940   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1941              (not gnus-show-all-headers))
1942     (save-excursion
1943       (save-restriction
1944         (let ((inhibit-read-only t)
1945               (inhibit-point-motion-hooks t))
1946           (article-narrow-to-head)
1947           (dolist (elem gnus-boring-article-headers)
1948             (goto-char (point-min))
1949             (cond
1950              ;; Hide empty headers.
1951              ((eq elem 'empty)
1952               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1953                 (forward-line -1)
1954                 (gnus-article-hide-text-type
1955                  (point-at-bol)
1956                  (progn
1957                    (end-of-line)
1958                    (if (re-search-forward "^[^ \t]" nil t)
1959                        (match-beginning 0)
1960                      (point-max)))
1961                  'boring-headers)))
1962              ;; Hide boring Newsgroups header.
1963              ((eq elem 'newsgroups)
1964               (when (gnus-string-equal
1965                      (gnus-fetch-field "newsgroups")
1966                      (gnus-group-real-name
1967                       (if (boundp 'gnus-newsgroup-name)
1968                           gnus-newsgroup-name
1969                         "")))
1970                 (gnus-article-hide-header "newsgroups")))
1971              ((eq elem 'to-address)
1972               (let ((to (message-fetch-field "to"))
1973                     (to-address
1974                      (gnus-parameter-to-address
1975                       (if (boundp 'gnus-newsgroup-name)
1976                           gnus-newsgroup-name ""))))
1977                 (when (and to to-address
1978                            (ignore-errors
1979                              (gnus-string-equal
1980                               ;; only one address in To
1981                               (nth 1 (mail-extract-address-components to))
1982                               to-address)))
1983                   (gnus-article-hide-header "to"))))
1984              ((eq elem 'to-list)
1985               (let ((to (message-fetch-field "to"))
1986                     (to-list
1987                      (gnus-parameter-to-list
1988                       (if (boundp 'gnus-newsgroup-name)
1989                           gnus-newsgroup-name ""))))
1990                 (when (and to to-list
1991                            (ignore-errors
1992                              (gnus-string-equal
1993                               ;; only one address in To
1994                               (nth 1 (mail-extract-address-components to))
1995                               to-list)))
1996                   (gnus-article-hide-header "to"))))
1997              ((eq elem 'cc-list)
1998               (let ((cc (message-fetch-field "cc"))
1999                     (to-list
2000                      (gnus-parameter-to-list
2001                       (if (boundp 'gnus-newsgroup-name)
2002                           gnus-newsgroup-name ""))))
2003                 (when (and cc to-list
2004                            (ignore-errors
2005                              (gnus-string-equal
2006                               ;; only one address in CC
2007                               (nth 1 (mail-extract-address-components cc))
2008                               to-list)))
2009                   (gnus-article-hide-header "cc"))))
2010              ((eq elem 'followup-to)
2011               (when (gnus-string-equal
2012                      (message-fetch-field "followup-to")
2013                      (message-fetch-field "newsgroups"))
2014                 (gnus-article-hide-header "followup-to")))
2015              ((eq elem 'reply-to)
2016               (if (gnus-group-find-parameter
2017                    gnus-newsgroup-name 'broken-reply-to)
2018                   (gnus-article-hide-header "reply-to")
2019                 (let ((from (message-fetch-field "from"))
2020                       (reply-to (message-fetch-field "reply-to")))
2021                   (when
2022                       (and
2023                        from reply-to
2024                        (ignore-errors
2025                          (equal
2026                           (sort (mapcar
2027                                  (lambda (x) (downcase (cadr x)))
2028                                  (mail-extract-address-components from t))
2029                                 'string<)
2030                           (sort (mapcar
2031                                  (lambda (x) (downcase (cadr x)))
2032                                  (mail-extract-address-components reply-to t))
2033                                 'string<))))
2034                     (gnus-article-hide-header "reply-to")))))
2035              ((eq elem 'date)
2036               (let ((date (with-current-buffer gnus-original-article-buffer
2037                             ;; If date in `gnus-article-buffer' is localized
2038                             ;; (`gnus-treat-date-user-defined'),
2039                             ;; `days-between' might fail.
2040                             (message-fetch-field "date"))))
2041                 (when (and date
2042                            (< (days-between (current-time-string) date)
2043                               4))
2044                   (gnus-article-hide-header "date"))))
2045              ((eq elem 'long-to)
2046               (let ((to (message-fetch-field "to"))
2047                     (cc (message-fetch-field "cc")))
2048                 (when (> (length to) 1024)
2049                   (gnus-article-hide-header "to"))
2050                 (when (> (length cc) 1024)
2051                   (gnus-article-hide-header "cc"))))
2052              ((eq elem 'many-to)
2053               (let ((to-count 0)
2054                     (cc-count 0))
2055                 (goto-char (point-min))
2056                 (while (re-search-forward "^to:" nil t)
2057                   (setq to-count (1+ to-count)))
2058                 (when (> to-count 1)
2059                   (while (> to-count 0)
2060                     (goto-char (point-min))
2061                     (save-restriction
2062                       (re-search-forward "^to:" nil nil to-count)
2063                       (forward-line -1)
2064                       (narrow-to-region (point) (point-max))
2065                       (gnus-article-hide-header "to"))
2066                     (setq to-count (1- to-count))))
2067                 (goto-char (point-min))
2068                 (while (re-search-forward "^cc:" nil t)
2069                   (setq cc-count (1+ cc-count)))
2070                 (when (> cc-count 1)
2071                   (while (> cc-count 0)
2072                     (goto-char (point-min))
2073                     (save-restriction
2074                       (re-search-forward "^cc:" nil nil cc-count)
2075                       (forward-line -1)
2076                       (narrow-to-region (point) (point-max))
2077                       (gnus-article-hide-header "cc"))
2078                     (setq cc-count (1- cc-count)))))))))))))
2079
2080 (defun gnus-article-hide-header (header)
2081   (save-excursion
2082     (goto-char (point-min))
2083     (when (re-search-forward (concat "^" header ":") nil t)
2084       (gnus-article-hide-text-type
2085        (point-at-bol)
2086        (progn
2087          (end-of-line)
2088          (if (re-search-forward "^[^ \t]" nil t)
2089              (match-beginning 0)
2090            (point-max)))
2091        'boring-headers))))
2092
2093 (defvar gnus-article-normalized-header-length 40
2094   "Length of normalized headers.")
2095
2096 (defun article-normalize-headers ()
2097   "Make all header lines 40 characters long."
2098   (interactive)
2099   (let ((inhibit-read-only t)
2100         column)
2101     (save-excursion
2102       (save-restriction
2103         (article-narrow-to-head)
2104         (while (not (eobp))
2105           (cond
2106            ((< (setq column (- (point-at-eol) (point)))
2107                gnus-article-normalized-header-length)
2108             (end-of-line)
2109             (insert (make-string
2110                      (- gnus-article-normalized-header-length column)
2111                      ? )))
2112            ((> column gnus-article-normalized-header-length)
2113             (gnus-put-text-property
2114              (progn
2115                (forward-char gnus-article-normalized-header-length)
2116                (point))
2117              (point-at-eol)
2118              'invisible t))
2119            (t
2120             ;; Do nothing.
2121             ))
2122           (forward-line 1))))))
2123
2124 (defun article-treat-dumbquotes ()
2125   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2126 Note that this function guesses whether a character is a sm*rtq**t* or
2127 not, so it should only be used interactively.
2128
2129 Sm*rtq**t*s are M****s***'s unilateral extension to the
2130 iso-8859-1 character map in an attempt to provide more quoting
2131 characters.  If you see something like \\222 or \\264 where
2132 you're expecting some kind of apostrophe or quotation mark, then
2133 try this wash."
2134   (interactive)
2135   (article-translate-strings gnus-article-dumbquotes-map))
2136
2137 (defun article-translate-characters (from to)
2138   "Translate all characters in the body of the article according to FROM and TO.
2139 FROM is a string of characters to translate from; to is a string of
2140 characters to translate to."
2141   (save-excursion
2142     (when (article-goto-body)
2143       (let ((inhibit-read-only t)
2144             (x (make-string 225 ?x))
2145             (i -1))
2146         (while (< (incf i) (length x))
2147           (aset x i i))
2148         (setq i 0)
2149         (while (< i (length from))
2150           (aset x (aref from i) (aref to i))
2151           (incf i))
2152         (translate-region (point) (point-max) x)))))
2153
2154 (defun article-translate-strings (map)
2155   "Translate all string in the body of the article according to MAP.
2156 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2157   (save-excursion
2158     (when (article-goto-body)
2159       (let ((inhibit-read-only t))
2160         (dolist (elem map)
2161           (save-excursion
2162             (while (search-forward (car elem) nil t)
2163               (replace-match (cadr elem)))))))))
2164
2165 (defun article-treat-overstrike ()
2166   "Translate overstrikes into bold text."
2167   (interactive)
2168   (save-excursion
2169     (when (article-goto-body)
2170       (let ((inhibit-read-only t))
2171         (while (search-forward "\b" nil t)
2172           (let ((next (char-after))
2173                 (previous (char-after (- (point) 2))))
2174             ;; We do the boldification/underlining by hiding the
2175             ;; overstrikes and putting the proper text property
2176             ;; on the letters.
2177             (cond
2178              ((eq next previous)
2179               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2180               (put-text-property (point) (1+ (point)) 'face 'bold))
2181              ((eq next ?_)
2182               (gnus-article-hide-text-type
2183                (1- (point)) (1+ (point)) 'overstrike)
2184               (put-text-property
2185                (- (point) 2) (1- (point)) 'face 'underline))
2186              ((eq previous ?_)
2187               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2188               (put-text-property
2189                (point) (1+ (point)) 'face 'underline)))))))))
2190
2191 (defun article-treat-ansi-sequences ()
2192   "Translate ANSI SGR control sequences into overlays or extents."
2193   (interactive)
2194   (save-excursion
2195     (when (article-goto-body)
2196       (let ((inhibit-read-only t))
2197         (ansi-color-apply-on-region (point) (point-max))))))
2198
2199 (defun gnus-article-treat-unfold-headers ()
2200   "Unfold folded message headers.
2201 Only the headers that fit into the current window width will be
2202 unfolded."
2203   (interactive)
2204   (gnus-with-article-headers
2205     (let (length)
2206       (while (not (eobp))
2207         (save-restriction
2208           (mail-header-narrow-to-field)
2209           (let* ((header (buffer-string))
2210                  (unfoldable
2211                   (or (equal gnus-article-unfold-long-headers t)
2212                       (and (stringp gnus-article-unfold-long-headers)
2213                            (string-match gnus-article-unfold-long-headers header)))))
2214             (with-temp-buffer
2215               (insert header)
2216               (goto-char (point-min))
2217               (while (re-search-forward "\n[\t ]" nil t)
2218                 (replace-match " " t t)))
2219             (setq length (- (point-max) (point-min) 1))
2220             (when (or unfoldable
2221                       (< length (window-width)))
2222               (while (re-search-forward "\n[\t ]" nil t)
2223                 (replace-match " " t t))))
2224           (goto-char (point-max)))))))
2225
2226 (defun gnus-article-treat-fold-headers ()
2227   "Fold message headers."
2228   (interactive)
2229   (gnus-with-article-headers
2230     (while (not (eobp))
2231       (save-restriction
2232         (mail-header-narrow-to-field)
2233         (mail-header-fold-field)
2234         (goto-char (point-max))))))
2235
2236 (defun gnus-treat-smiley ()
2237   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2238   (interactive)
2239   (gnus-with-article-buffer
2240     (if (memq 'smiley gnus-article-wash-types)
2241         (gnus-delete-images 'smiley)
2242       (article-goto-body)
2243       (let ((images (smiley-region (point) (point-max))))
2244         (when images
2245           (gnus-add-wash-type 'smiley)
2246           (dolist (image images)
2247             (gnus-add-image 'smiley image)))))))
2248
2249 (defun gnus-article-remove-images ()
2250   "Remove all images from the article buffer."
2251   (interactive)
2252   (gnus-with-article-buffer
2253     (dolist (elem gnus-article-image-alist)
2254       (gnus-delete-images (car elem)))))
2255
2256 (defun gnus-article-treat-fold-newsgroups ()
2257   "Unfold folded message headers.
2258 Only the headers that fit into the current window width will be
2259 unfolded."
2260   (interactive)
2261   (gnus-with-article-headers
2262     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2263       (save-restriction
2264         (mail-header-narrow-to-field)
2265         (while (re-search-forward ", *" nil t)
2266           (replace-match ", " t t))
2267         (mail-header-fold-field)
2268         (goto-char (point-max))))))
2269
2270 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2271   "Value of `truncate-lines' in Gnus Article buffer.
2272 Valid values are nil, t, `head', `first', `last', an integer or a
2273 predicate.  See Info node `(gnus)Customizing Articles'."
2274   :version "23.1" ;; No Gnus
2275   :group 'gnus-article
2276   ;; :link '(custom-manual "(gnus)Customizing Articles")
2277   :type 'boolean)
2278
2279 (defun gnus-article-toggle-truncate-lines (&optional arg)
2280   "Toggle whether to fold or truncate long lines in article the buffer.
2281 If ARG is non-nil and not a number, toggle
2282 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2283 long lines if and only if arg is positive."
2284   (interactive "P")
2285   (cond
2286    ((and (numberp arg) (> arg 0))
2287     (setq gnus-article-truncate-lines t))
2288    ((numberp arg)
2289     (setq gnus-article-truncate-lines nil))
2290    (arg
2291     (setq gnus-article-truncate-lines
2292           (not gnus-article-truncate-lines))))
2293   (gnus-with-article-buffer
2294     (cond
2295      ((and (numberp arg) (> arg 0))
2296       (setq truncate-lines nil))
2297      ((numberp arg)
2298       (setq truncate-lines t)))
2299     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2300     ;; `toggle-truncate-lines' needs an argument.
2301     (toggle-truncate-lines)))
2302
2303 (defun gnus-article-treat-body-boundary ()
2304   "Place a boundary line at the end of the headers."
2305   (interactive)
2306   (when (and gnus-body-boundary-delimiter
2307              (> (length gnus-body-boundary-delimiter) 0))
2308     (gnus-with-article-headers
2309       (goto-char (point-max))
2310       (let ((start (point)))
2311         (insert "X-Boundary: ")
2312         (gnus-add-text-properties start (point) '(invisible t intangible t))
2313         (insert (let (str)
2314                   (while (>= (1- (window-width)) (length str))
2315                     (setq str (concat str gnus-body-boundary-delimiter)))
2316                   (substring str 0 (1- (window-width))))
2317                 "\n")
2318         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2319
2320 (defun article-fill-long-lines ()
2321   "Fill lines that are wider than the window width."
2322   (interactive)
2323   (save-excursion
2324     (let ((inhibit-read-only t)
2325           (width (window-width (get-buffer-window (current-buffer)))))
2326       (save-restriction
2327         (article-goto-body)
2328         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2329           (while (not (eobp))
2330             (end-of-line)
2331             (when (>= (current-column) (min fill-column width))
2332               (narrow-to-region (min (1+ (point)) (point-max))
2333                                 (point-at-bol))
2334               (let ((goback (point-marker)))
2335                 (fill-paragraph nil)
2336                 (goto-char (marker-position goback)))
2337               (widen))
2338             (forward-line 1)))))))
2339
2340 (defun article-capitalize-sentences ()
2341   "Capitalize the first word in each sentence."
2342   (interactive)
2343   (save-excursion
2344     (let ((inhibit-read-only t)
2345           (paragraph-start "^[\n\^L]"))
2346       (article-goto-body)
2347       (while (not (eobp))
2348         (capitalize-word 1)
2349         (forward-sentence)))))
2350
2351 (defun article-remove-cr ()
2352   "Remove trailing CRs and then translate remaining CRs into LFs."
2353   (interactive)
2354   (save-excursion
2355     (let ((inhibit-read-only t))
2356       (goto-char (point-min))
2357       (while (re-search-forward "\r+$" nil t)
2358         (replace-match "" t t))
2359       (goto-char (point-min))
2360       (while (search-forward "\r" nil t)
2361         (replace-match "\n" t t)))))
2362
2363 (defun article-remove-trailing-blank-lines ()
2364   "Remove all trailing blank lines from the article."
2365   (interactive)
2366   (save-excursion
2367     (let ((inhibit-read-only t))
2368       (goto-char (point-max))
2369       (delete-region
2370        (point)
2371        (progn
2372          (while (and (not (bobp))
2373                      (looking-at "^[ \t]*$")
2374                      (not (gnus-annotation-in-region-p
2375                            (point) (point-at-eol))))
2376            (forward-line -1))
2377          (forward-line 1)
2378          (point))))))
2379
2380 (defvar gnus-face-properties-alist)
2381
2382 (defun article-display-face (&optional force)
2383   "Display any Face headers in the header."
2384   (interactive (list 'force))
2385   (let ((wash-face-p buffer-read-only))
2386     (gnus-with-article-headers
2387       ;; When displaying parts, this function can be called several times on
2388       ;; the same article, without any intended toggle semantic (as typing `W
2389       ;; D d' would have). So face deletion must occur only when we come from
2390       ;; an interactive command, that is when the *Article* buffer is
2391       ;; read-only.
2392       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2393           (gnus-delete-images 'face)
2394         (let ((from (message-fetch-field "from"))
2395               face faces)
2396           (save-current-buffer
2397             (when (and wash-face-p
2398                        (gnus-buffer-live-p gnus-original-article-buffer)
2399                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2400               (set-buffer gnus-original-article-buffer))
2401             (save-restriction
2402               (mail-narrow-to-head)
2403               (when (or force
2404                         ;; Check whether this face is censored.
2405                         (not (and gnus-article-x-face-too-ugly
2406                                   (or from
2407                                       (setq from (message-fetch-field "from")))
2408                                   (string-match gnus-article-x-face-too-ugly
2409                                                 from))))
2410                 (while (gnus-article-goto-header "Face")
2411                   (push (mail-header-field-value) faces)))))
2412           (when faces
2413             (goto-char (point-min))
2414             (let (png image)
2415               (unless (setq from (gnus-article-goto-header "from"))
2416                 (insert "From:")
2417                 (setq from (point))
2418                 (insert " [no `from' set]\n"))
2419               (while faces
2420                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2421                   (setq image
2422                         (apply 'gnus-create-image png 'png t
2423                                (cdr (assq 'png gnus-face-properties-alist))))
2424                   (goto-char from)
2425                   (gnus-add-wash-type 'face)
2426                   (gnus-add-image 'face image)
2427                   (gnus-put-image image nil 'face))))))))))
2428
2429 (defun article-display-x-face (&optional force)
2430   "Look for an X-Face header and display it if present."
2431   (interactive (list 'force))
2432   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2433     (gnus-with-article-headers
2434       ;; Delete the old process, if any.
2435       (when (process-status "article-x-face")
2436         (delete-process "article-x-face"))
2437       ;; See the comment in `article-display-face'.
2438       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2439           ;; We have already displayed X-Faces, so we remove them
2440           ;; instead.
2441           (gnus-delete-images 'xface)
2442         ;; Display X-Faces.
2443         (let ((from (message-fetch-field "from"))
2444               x-faces face)
2445           (save-current-buffer
2446             (when (and wash-face-p
2447                        (gnus-buffer-live-p gnus-original-article-buffer)
2448                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2449               ;; If type `W f', use gnus-original-article-buffer,
2450               ;; otherwise use the current buffer because displaying
2451               ;; RFC822 parts calls this function too.
2452               (set-buffer gnus-original-article-buffer))
2453             (save-restriction
2454               (mail-narrow-to-head)
2455               (and gnus-article-x-face-command
2456                    (or force
2457                        ;; Check whether this face is censored.
2458                        (not (and gnus-article-x-face-too-ugly
2459                                  (or from
2460                                      (setq from (message-fetch-field "from")))
2461                                  (string-match gnus-article-x-face-too-ugly
2462                                                from))))
2463                    (while (gnus-article-goto-header "X-Face")
2464                      (push (mail-header-field-value) x-faces)))))
2465           (when x-faces
2466             ;; We display the face.
2467             (cond ((functionp gnus-article-x-face-command)
2468                    ;; The command is a lisp function, so we call it.
2469                    (mapc gnus-article-x-face-command x-faces))
2470                   ((stringp gnus-article-x-face-command)
2471                    ;; The command is a string, so we interpret the command
2472                    ;; as a, well, command, and fork it off.
2473                    (let ((process-connection-type nil))
2474                      (gnus-set-process-query-on-exit-flag
2475                       (start-process
2476                        "article-x-face" nil shell-file-name
2477                        shell-command-switch gnus-article-x-face-command)
2478                       nil)
2479                      ;; Sending multiple EOFs to xv doesn't work,
2480                      ;; so we only do a single external face.
2481                      (with-temp-buffer
2482                        (insert (car x-faces))
2483                        (process-send-region "article-x-face"
2484                                             (point-min) (point-max)))
2485                      (process-send-eof "article-x-face")))
2486                   (t
2487                    (error "`%s' set to `%s' is not a function"
2488                           gnus-article-x-face-command
2489                           'gnus-article-x-face-command)))))))))
2490
2491 (defun article-decode-mime-words ()
2492   "Decode all MIME-encoded words in the article."
2493   (interactive)
2494   (gnus-with-article-buffer
2495     (let ((inhibit-point-motion-hooks t)
2496           (mail-parse-charset gnus-newsgroup-charset)
2497           (mail-parse-ignored-charsets
2498            (with-current-buffer gnus-summary-buffer
2499              gnus-newsgroup-ignored-charsets)))
2500       (mail-decode-encoded-word-region (point-min) (point-max)))))
2501
2502 (defun article-decode-charset (&optional prompt)
2503   "Decode charset-encoded text in the article.
2504 If PROMPT (the prefix), prompt for a coding system to use."
2505   (interactive "P")
2506   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2507         (inhibit-read-only t)
2508         (mail-parse-charset gnus-newsgroup-charset)
2509         (mail-parse-ignored-charsets
2510          (save-excursion (condition-case nil
2511                              (set-buffer gnus-summary-buffer)
2512                            (error))
2513                          gnus-newsgroup-ignored-charsets))
2514         ct cte ctl charset format)
2515     (save-excursion
2516       (save-restriction
2517         (article-narrow-to-head)
2518         (setq ct (message-fetch-field "Content-Type" t)
2519               cte (message-fetch-field "Content-Transfer-Encoding" t)
2520               ctl (and ct (mail-header-parse-content-type ct))
2521               charset (cond
2522                        (prompt
2523                         (mm-read-coding-system "Charset to decode: "))
2524                        (ctl
2525                         (mail-content-type-get ctl 'charset)))
2526               format (and ctl (mail-content-type-get ctl 'format)))
2527         (when cte
2528           (setq cte (mail-header-strip cte)))
2529         (if (and ctl (not (string-match "/" (car ctl))))
2530             (setq ctl nil))
2531         (goto-char (point-max)))
2532       (forward-line 1)
2533       (save-restriction
2534         (narrow-to-region (point) (point-max))
2535         (when (and (eq mail-parse-charset 'gnus-decoded)
2536                    (eq (mm-body-7-or-8) '8bit))
2537           ;; The text code could have been decoded.
2538           (setq charset mail-parse-charset))
2539         (when (and (or (not ctl)
2540                        (equal (car ctl) "text/plain"))
2541                    (not format)) ;; article with format will decode later.
2542           (mm-decode-body
2543            charset (and cte (intern (downcase
2544                                      (gnus-strip-whitespace cte))))
2545            (car ctl)))))))
2546
2547 (defun article-decode-encoded-words ()
2548   "Remove encoded-word encoding from headers."
2549   (let ((inhibit-point-motion-hooks t)
2550         (mail-parse-charset gnus-newsgroup-charset)
2551         (mail-parse-ignored-charsets
2552          (save-excursion (condition-case nil
2553                              (set-buffer gnus-summary-buffer)
2554                            (error))
2555                          gnus-newsgroup-ignored-charsets))
2556         (inhibit-read-only t)
2557         end start)
2558     (goto-char (point-min))
2559     (when (search-forward "\n\n" nil 'move)
2560       (forward-line -1))
2561     (setq end (point))
2562     (while (not (bobp))
2563       (while (progn
2564                (forward-line -1)
2565                (and (not (bobp))
2566                     (memq (char-after) '(?\t ? )))))
2567       (setq start (point))
2568       (if (looking-at "\
2569 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2570 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2571           (funcall gnus-decode-address-function start end)
2572         (funcall gnus-decode-header-function start end))
2573       (goto-char (setq end start)))))
2574
2575 (defun article-decode-group-name ()
2576   "Decode group names in Newsgroups, Followup-To and Xref headers."
2577   (let ((inhibit-point-motion-hooks t)
2578         (inhibit-read-only t)
2579         (method (gnus-find-method-for-group gnus-newsgroup-name))
2580         regexp)
2581     (when (and (or gnus-group-name-charset-method-alist
2582                    gnus-group-name-charset-group-alist)
2583                (gnus-buffer-live-p gnus-original-article-buffer))
2584       (save-restriction
2585         (article-narrow-to-head)
2586         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2587           (with-current-buffer gnus-original-article-buffer
2588             (goto-char (point-min)))
2589           (setq regexp (concat "^" header
2590                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2591           (while (re-search-forward regexp nil t)
2592             (replace-match (save-match-data
2593                              (gnus-decode-newsgroups
2594                               ;; XXX how to use data in article buffer?
2595                               (with-current-buffer gnus-original-article-buffer
2596                                 (re-search-forward regexp nil t)
2597                                 (match-string 1))
2598                               gnus-newsgroup-name method))
2599                            t t nil 1))
2600           (goto-char (point-min)))))))
2601
2602 (autoload 'idna-to-unicode "idna")
2603
2604 (defun article-decode-idna-rhs ()
2605   "Decode IDNA strings in RHS in various headers in current buffer.
2606 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2607 Mail-Reply-To: and Mail-Followup-To:."
2608   (when gnus-use-idna
2609     (save-restriction
2610       (let ((inhibit-point-motion-hooks t)
2611             (inhibit-read-only t))
2612         (article-narrow-to-head)
2613         (goto-char (point-min))
2614         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2615           (let (ace unicode)
2616             (when (save-match-data
2617                     (and (setq ace (match-string 1))
2618                          (save-excursion
2619                            (and (re-search-backward "^[^ \t]" nil t)
2620                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2621                          (setq unicode (idna-to-unicode ace))))
2622               (unless (string= ace unicode)
2623                 (replace-match unicode nil nil nil 1)))))))))
2624
2625 (defun article-de-quoted-unreadable (&optional force read-charset)
2626   "Translate a quoted-printable-encoded article.
2627 If FORCE, decode the article whether it is marked as quoted-printable
2628 or not.
2629 If READ-CHARSET, ask for a coding system."
2630   (interactive (list 'force current-prefix-arg))
2631   (save-excursion
2632     (let ((inhibit-read-only t) type charset)
2633       (if (gnus-buffer-live-p gnus-original-article-buffer)
2634           (with-current-buffer gnus-original-article-buffer
2635             (setq type
2636                   (gnus-fetch-field "content-transfer-encoding"))
2637             (let* ((ct (gnus-fetch-field "content-type"))
2638                    (ctl (and ct (mail-header-parse-content-type ct))))
2639               (setq charset (and ctl
2640                                  (mail-content-type-get ctl 'charset)))
2641               (if (stringp charset)
2642                   (setq charset (intern (downcase charset)))))))
2643       (if read-charset
2644           (setq charset (mm-read-coding-system "Charset: " charset)))
2645       (unless charset
2646         (setq charset gnus-newsgroup-charset))
2647       (when (or force
2648                 (and type (let ((case-fold-search t))
2649                             (string-match "quoted-printable" type))))
2650         (article-goto-body)
2651         (quoted-printable-decode-region
2652          (point) (point-max) (mm-charset-to-coding-system charset))))))
2653
2654 (defun article-de-base64-unreadable (&optional force read-charset)
2655   "Translate a base64 article.
2656 If FORCE, decode the article whether it is marked as base64 not.
2657 If READ-CHARSET, ask for a coding system."
2658   (interactive (list 'force current-prefix-arg))
2659   (save-excursion
2660     (let ((inhibit-read-only t) type charset)
2661       (if (gnus-buffer-live-p gnus-original-article-buffer)
2662           (with-current-buffer gnus-original-article-buffer
2663             (setq type
2664                   (gnus-fetch-field "content-transfer-encoding"))
2665             (let* ((ct (gnus-fetch-field "content-type"))
2666                    (ctl (and ct (mail-header-parse-content-type ct))))
2667               (setq charset (and ctl
2668                                  (mail-content-type-get ctl 'charset)))
2669               (if (stringp charset)
2670                   (setq charset (intern (downcase charset)))))))
2671       (if read-charset
2672           (setq charset (mm-read-coding-system "Charset: " charset)))
2673       (unless charset
2674         (setq charset gnus-newsgroup-charset))
2675       (when (or force
2676                 (and type (let ((case-fold-search t))
2677                             (string-match "base64" type))))
2678         (article-goto-body)
2679         (save-restriction
2680           (narrow-to-region (point) (point-max))
2681           (base64-decode-region (point-min) (point-max))
2682           (mm-decode-coding-region
2683            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2684
2685 (eval-when-compile
2686   (require 'rfc1843))
2687
2688 (defun article-decode-HZ ()
2689   "Translate a HZ-encoded article."
2690   (interactive)
2691   (require 'rfc1843)
2692   (save-excursion
2693     (let ((inhibit-read-only t))
2694       (rfc1843-decode-region (point-min) (point-max)))))
2695
2696 (defun article-unsplit-urls ()
2697   "Remove the newlines that some other mailers insert into URLs."
2698   (interactive)
2699   (save-excursion
2700     (let ((inhibit-read-only t))
2701       (goto-char (point-min))
2702       (while (re-search-forward
2703               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2704         (replace-match "\\1\\3" t)))
2705     (when (interactive-p)
2706       (gnus-treat-article nil))))
2707
2708
2709 (defun article-wash-html (&optional read-charset)
2710   "Format an HTML article.
2711 If READ-CHARSET, ask for a coding system.  If it is a number, the
2712 charset defined in `gnus-summary-show-article-charset-alist' is used."
2713   (interactive "P")
2714   (save-excursion
2715     (let ((inhibit-read-only t)
2716           charset)
2717       (if read-charset
2718           (if (or (and (numberp read-charset)
2719                        (setq charset
2720                              (cdr
2721                               (assq read-charset
2722                                     gnus-summary-show-article-charset-alist))))
2723                   (setq charset (mm-read-coding-system "Charset: ")))
2724               (let ((gnus-summary-show-article-charset-alist
2725                      (list (cons 1 charset))))
2726                 (with-current-buffer gnus-summary-buffer
2727                   (gnus-summary-show-article 1)))
2728             (error "No charset is given"))
2729         (when (gnus-buffer-live-p gnus-original-article-buffer)
2730           (with-current-buffer gnus-original-article-buffer
2731             (let* ((ct (gnus-fetch-field "content-type"))
2732                    (ctl (and ct (mail-header-parse-content-type ct))))
2733               (setq charset (and ctl
2734                                  (mail-content-type-get ctl 'charset)))
2735               (when (stringp charset)
2736                 (setq charset (intern (downcase charset)))))))
2737         (unless charset
2738           (setq charset gnus-newsgroup-charset)))
2739       (article-goto-body)
2740       (save-window-excursion
2741         (save-restriction
2742           (narrow-to-region (point) (point-max))
2743           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2744                  (entry (assq func mm-text-html-washer-alist)))
2745             (when entry
2746               (setq func (cdr entry)))
2747             (cond
2748              ((functionp func)
2749               (funcall func))
2750              (t
2751               (apply (car func) (cdr func))))))))))
2752
2753 ;; External.
2754 (declare-function w3-region "ext:w3-display" (st nd))
2755
2756 (defun gnus-article-wash-html-with-w3 ()
2757   "Wash the current buffer with w3."
2758   (mm-setup-w3)
2759   (let ((w3-strict-width (window-width))
2760         (url-standalone-mode t)
2761         (url-gateway-unplugged t)
2762         (w3-honor-stylesheets nil))
2763     (condition-case ()
2764         (w3-region (point-min) (point-max))
2765       (error))))
2766
2767 ;; External.
2768 (declare-function w3m-region "ext:w3m" (start end &optional url charset))
2769
2770 (defun gnus-article-wash-html-with-w3m ()
2771   "Wash the current buffer with emacs-w3m."
2772   (mm-setup-w3m)
2773   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2774         w3m-force-redisplay)
2775     (w3m-region (point-min) (point-max)))
2776   ;; Put the mark meaning this part was rendered by emacs-w3m.
2777   (put-text-property (point-min) (point-max) 'mm-inline-text-html-with-w3m t)
2778   (when (and mm-inline-text-html-with-w3m-keymap
2779              (boundp 'w3m-minor-mode-map)
2780              w3m-minor-mode-map)
2781     (if (and (boundp 'w3m-link-map)
2782              w3m-link-map)
2783         (let* ((start (point-min))
2784                (end (point-max))
2785                (on (get-text-property start 'w3m-href-anchor))
2786                (map (copy-keymap w3m-link-map))
2787                next)
2788           (set-keymap-parent map w3m-minor-mode-map)
2789           (while (< start end)
2790             (if on
2791                 (progn
2792                   (setq next (or (text-property-any start end
2793                                                     'w3m-href-anchor nil)
2794                                  end))
2795                   (put-text-property start next 'keymap map))
2796               (setq next (or (text-property-not-all start end
2797                                                     'w3m-href-anchor nil)
2798                              end))
2799               (put-text-property start next 'keymap w3m-minor-mode-map))
2800             (setq start next
2801                   on (not on))))
2802       (put-text-property (point-min) (point-max) 'keymap w3m-minor-mode-map))))
2803
2804 (defvar charset) ;; Bound by `article-wash-html'.
2805
2806 (defun gnus-article-wash-html-with-w3m-standalone ()
2807   "Wash the current buffer with w3m."
2808   (if (mm-w3m-standalone-supports-m17n-p)
2809       (progn
2810         (unless (mm-coding-system-p charset) ;; Bound by `article-wash-html'.
2811           ;; The default.
2812           (setq charset 'iso-8859-1))
2813         (let ((coding-system-for-write charset)
2814               (coding-system-for-read charset))
2815           (call-process-region
2816            (point-min) (point-max)
2817            "w3m" t t nil "-dump" "-T" "text/html"
2818            "-I" (symbol-name charset) "-O" (symbol-name charset))))
2819     (mm-inline-wash-with-stdin nil "w3m" "-dump" "-T" "text/html")))
2820
2821 (defvar gnus-article-browse-html-temp-list nil
2822   "List of temporary files created by `gnus-article-browse-html-parts'.
2823 Internal variable.")
2824
2825 (defcustom gnus-article-browse-delete-temp 'ask
2826   "What to do with temporary files from `gnus-article-browse-html-parts'.
2827 If nil, don't delete temporary files.  If it is t, delete them on
2828 exit from the summary buffer.  If it is the symbol `file', query
2829 on each file, if it is `ask' ask once when exiting from the
2830 summary buffer."
2831   :group 'gnus-article
2832   :version "23.1" ;; No Gnus
2833   :type '(choice (const :tag "Don't delete" nil)
2834                  (const :tag "Don't ask" t)
2835                  (const :tag "Ask" ask)
2836                  (const :tag "Ask for each file" file)))
2837
2838 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2839
2840 (defun gnus-article-browse-delete-temp-files (&optional how)
2841   "Delete temp-files created by `gnus-article-browse-html-parts'."
2842   (when (and gnus-article-browse-html-temp-list
2843              (progn
2844                (or how (setq how gnus-article-browse-delete-temp))
2845                (if (eq how 'ask)
2846                    (let ((files (length gnus-article-browse-html-temp-list)))
2847                      (gnus-y-or-n-p (format
2848                                      "Delete all %s temporary HTML file%s? "
2849                                      files
2850                                      (if (> files 1) "s" ""))))
2851                  how)))
2852     (dolist (file gnus-article-browse-html-temp-list)
2853       (cond ((file-directory-p file)
2854              (when (or (not (eq how 'file))
2855                        (gnus-y-or-n-p
2856                         (format
2857                          "Delete temporary HTML file(s) in directory `%s'? "
2858                          (file-name-as-directory file))))
2859                (gnus-delete-directory file)))
2860             ((file-exists-p file)
2861              (when (or (not (eq how 'file))
2862                        (gnus-y-or-n-p
2863                         (format "Delete temporary HTML file `%s'? " file)))
2864                (delete-file file)))))
2865     ;; Also remove file from the list when not deleted or if file doesn't
2866     ;; exist anymore.
2867     (setq gnus-article-browse-html-temp-list nil))
2868   gnus-article-browse-html-temp-list)
2869
2870 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2871   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2872 Return file name."
2873   (save-match-data
2874     (let (file type)
2875       (catch 'found
2876         (dolist (handle handles)
2877           (cond
2878            ((not (listp handle)))
2879            ((equal (mm-handle-media-supertype handle) "multipart")
2880             (when (setq file (gnus-article-browse-html-save-cid-content
2881                               cid handle directory))
2882               (throw 'found file)))
2883            ((equal (concat "<" cid ">") (mm-handle-id handle))
2884             (setq file
2885                   (expand-file-name
2886                    (or (mail-content-type-get
2887                         (mm-handle-disposition handle) 'filename)
2888                        (mail-content-type-get
2889                         (setq type (mm-handle-type handle)) 'name)
2890                        (concat
2891                         (make-temp-name "cid")
2892                         (car (rassoc (car type) mailcap-mime-extensions))))
2893                    directory))
2894             (mm-save-part-to-file handle file)
2895             (throw 'found file))))))))
2896
2897 (defun gnus-article-browse-html-parts (list &optional header)
2898   "View all \"text/html\" parts from LIST.
2899 Recurse into multiparts.  The optional HEADER that should be a decoded
2900 message header will be added to the bodies of the \"text/html\" parts."
2901   ;; Internal function used by `gnus-article-browse-html-article'.
2902   (let (type file charset content cid-dir tmp-file showed)
2903     ;; Find and show the html-parts.
2904     (dolist (handle list)
2905       ;; If HTML, show it:
2906       (cond ((not (listp handle)))
2907             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2908                  (and (equal (car type) "message/external-body")
2909                       (or header
2910                           (setq file (or (mail-content-type-get type 'name)
2911                                          (mail-content-type-get
2912                                           (mm-handle-disposition handle)
2913                                           'filename))))
2914                       (or (mm-handle-cache handle)
2915                           (condition-case code
2916                               (progn (mm-extern-cache-contents handle) t)
2917                             (error
2918                              (gnus-message 3 "%s" (error-message-string code))
2919                              (when (>= gnus-verbose 3) (sit-for 2))
2920                              nil)))
2921                       (progn
2922                         (setq handle (mm-handle-cache handle)
2923                               type (mm-handle-type handle))
2924                         (equal (car type) "text/html"))))
2925              (setq charset (mail-content-type-get type 'charset)
2926                    content (mm-get-part handle))
2927              (with-temp-buffer
2928                (if (eq charset 'gnus-decoded)
2929                    (mm-enable-multibyte)
2930                  (mm-disable-multibyte))
2931                (insert content)
2932                ;; resolve cid contents
2933                (let ((case-fold-search t)
2934                      cid-file)
2935                  (goto-char (point-min))
2936                  (while (re-search-forward "\
2937 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2938                                            nil t)
2939                    (unless cid-dir
2940                      (setq cid-dir (mm-make-temp-file "cid" t))
2941                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2942                    (setq file nil
2943                          content nil)
2944                    (when (setq cid-file
2945                                (gnus-article-browse-html-save-cid-content
2946                                 (match-string 2)
2947                                 (with-current-buffer gnus-article-buffer
2948                                   gnus-article-mime-handles)
2949                                 cid-dir))
2950                      (replace-match (concat "file://" cid-file)
2951                                     nil nil nil 1))))
2952                (unless content (setq content (buffer-string))))
2953              (when (or charset header (not file))
2954                (setq tmp-file (mm-make-temp-file
2955                                ;; Do we need to care for 8.3 filenames?
2956                                "mm-" nil ".html")))
2957              ;; Add a meta html tag to specify charset and a header.
2958              (cond
2959               (header
2960                (let (title eheader body hcharset coding force-charset)
2961                  (with-temp-buffer
2962                    (mm-enable-multibyte)
2963                    (setq case-fold-search t)
2964                    (insert header "\n")
2965                    (setq title (message-fetch-field "subject"))
2966                    (goto-char (point-min))
2967                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2968                      (replace-match (cond ((match-beginning 1) "&lt;")
2969                                           ((match-beginning 2) "&gt;")
2970                                           (t "&amp;"))))
2971                    (goto-char (point-min))
2972                    (insert "<pre>\n")
2973                    (goto-char (point-max))
2974                    (insert "</pre>\n<hr>\n")
2975                    ;; We have to examine charset one by one since
2976                    ;; charset specified in parts might be different.
2977                    (if (eq charset 'gnus-decoded)
2978                        (setq charset 'utf-8
2979                              eheader (mm-encode-coding-string (buffer-string)
2980                                                               charset)
2981                              title (when title
2982                                      (mm-encode-coding-string title charset))
2983                              body (mm-encode-coding-string content charset)
2984                              force-charset t)
2985                      (setq hcharset (mm-find-mime-charset-region (point-min)
2986                                                                  (point-max)))
2987                      (cond ((= (length hcharset) 1)
2988                             (setq hcharset (car hcharset)
2989                                   coding (mm-charset-to-coding-system
2990                                           hcharset)))
2991                            ((> (length hcharset) 1)
2992                             (setq hcharset 'utf-8
2993                                   coding hcharset)))
2994                      (if coding
2995                          (if charset
2996                              (progn
2997                                (setq body
2998                                      (mm-charset-to-coding-system charset))
2999                                (if (eq coding body)
3000                                    (setq eheader (mm-encode-coding-string
3001                                                   (buffer-string) coding)
3002                                          title (when title
3003                                                  (mm-encode-coding-string
3004                                                   title coding))
3005                                          body content)
3006                                  (setq charset 'utf-8
3007                                        eheader (mm-encode-coding-string
3008                                                 (buffer-string) charset)
3009                                        title (when title
3010                                                (mm-encode-coding-string
3011                                                 title charset))
3012                                        body (mm-encode-coding-string
3013                                              (mm-decode-coding-string
3014                                               content body)
3015                                              charset)
3016                                        force-charset t)))
3017                            (setq charset hcharset
3018                                  eheader (mm-encode-coding-string
3019                                           (buffer-string) coding)
3020                                  title (when title
3021                                          (mm-encode-coding-string
3022                                           title coding))
3023                                  body content))
3024                        (setq eheader (mm-string-as-unibyte (buffer-string))
3025                              body content)))
3026                    (erase-buffer)
3027                    (mm-disable-multibyte)
3028                    (insert body)
3029                    (when charset
3030                      (mm-add-meta-html-tag handle charset force-charset))
3031                    (when title
3032                      (goto-char (point-min))
3033                      (unless (search-forward "<title>" nil t)
3034                        (re-search-forward "<head>\\s-*" nil t)
3035                        (insert "<title>" title "</title>\n")))
3036                    (goto-char (point-min))
3037                    (or (re-search-forward
3038                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
3039                        (re-search-forward
3040                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
3041                    (insert eheader)
3042                    (mm-write-region (point-min) (point-max)
3043                                     tmp-file nil nil nil 'binary t))))
3044               (charset
3045                (mm-with-unibyte-buffer
3046                  (insert (if (eq charset 'gnus-decoded)
3047                              (mm-encode-coding-string content
3048                                                       (setq charset 'utf-8))
3049                            content))
3050                  (if (or (mm-add-meta-html-tag handle charset)
3051                          (not file))
3052                      (mm-write-region (point-min) (point-max)
3053                                       tmp-file nil nil nil 'binary t)
3054                    (setq tmp-file nil))))
3055               (tmp-file
3056                (mm-save-part-to-file handle tmp-file)))
3057              (when tmp-file
3058                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
3059              (add-hook 'gnus-summary-prepare-exit-hook
3060                        'gnus-article-browse-delete-temp-files)
3061              (add-hook 'gnus-exit-gnus-hook
3062                        (lambda  ()
3063                          (gnus-article-browse-delete-temp-files t)))
3064              ;; FIXME: Warn if there's an <img> tag?
3065              (browse-url-of-file (or tmp-file (expand-file-name file)))
3066              (setq showed t))
3067             ;; If multipart, recurse
3068             ((equal (mm-handle-media-supertype handle) "multipart")
3069              (when (gnus-article-browse-html-parts handle header)
3070                (setq showed t)))
3071             ((equal (mm-handle-media-type handle) "message/rfc822")
3072              (mm-with-multibyte-buffer
3073                (mm-insert-part handle)
3074                (setq handle (mm-dissect-buffer t t))
3075                (when (and (bufferp (car handle))
3076                           (stringp (car (mm-handle-type handle))))
3077                  (setq handle (list handle)))
3078                (when header
3079                  (article-decode-encoded-words)
3080                  (let ((gnus-visible-headers
3081                         (or (get 'gnus-visible-headers 'standard-value)
3082                             gnus-visible-headers)))
3083                    (article-hide-headers))
3084                  (goto-char (point-min))
3085                  (search-forward "\n\n" nil 'move)
3086                  (skip-chars-backward "\t\n ")
3087                  (setq header (buffer-substring (point-min) (point)))))
3088              (when (prog1
3089                        (gnus-article-browse-html-parts handle header)
3090                      (mm-destroy-parts handle))
3091                (setq showed t)))))
3092     showed))
3093
3094 (defun gnus-article-browse-html-article (&optional arg)
3095   "View \"text/html\" parts of the current article with a WWW browser.
3096 Inline images embedded in a message using the cid scheme, as they are
3097 generally considered to be safe, will be processed properly.
3098 The message header is added to the beginning of every html part unless
3099 the prefix argument ARG is given.
3100
3101 Warning: Spammers use links to images (using the http scheme) in HTML
3102 articles to verify whether you have read the message.  As
3103 `gnus-article-browse-html-article' passes the HTML content to the
3104 browser without eliminating these \"web bugs\" you should only
3105 use it for mails from trusted senders.
3106
3107 If you always want to display HTML parts in the browser, set
3108 `mm-text-html-renderer' to nil.
3109
3110 This command creates temporary files to pass HTML contents including
3111 images if any to the browser, and deletes them when exiting the group
3112 \(if you want)."
3113   ;; Cf. `mm-w3m-safe-url-regexp'
3114   (interactive "P")
3115   (if arg
3116       (gnus-summary-show-article)
3117     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3118                                     gnus-visible-headers))
3119           ;; As we insert a <hr>, there's no need for the body boundary.
3120           (gnus-treat-body-boundary nil))
3121       (gnus-summary-show-article)))
3122   (with-current-buffer gnus-article-buffer
3123     (let ((header (unless arg
3124                     (save-restriction
3125                       (widen)
3126                       (buffer-substring-no-properties
3127                        (goto-char (point-min))
3128                        (if (search-forward "\n\n" nil t)
3129                            (match-beginning 0)
3130                          (goto-char (point-max))
3131                          (skip-chars-backward "\t\n ")
3132                          (point))))))
3133           parts)
3134       (set-buffer gnus-original-article-buffer)
3135       (setq parts (mm-dissect-buffer t t))
3136       ;; If singlepart, enforce a list.
3137       (when (and (bufferp (car parts))
3138                  (stringp (car (mm-handle-type parts))))
3139         (setq parts (list parts)))
3140       ;; Process the list
3141       (unless (gnus-article-browse-html-parts parts header)
3142         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3143       (mm-destroy-parts parts)
3144       (unless arg
3145         (gnus-summary-show-article)))))
3146
3147 (defun article-hide-list-identifiers ()
3148   "Remove list identifies from the Subject header.
3149 The `gnus-list-identifiers' variable specifies what to do."
3150   (interactive)
3151   (let ((inhibit-point-motion-hooks t)
3152         (regexp (if (consp gnus-list-identifiers)
3153                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3154                   gnus-list-identifiers))
3155         (inhibit-read-only t))
3156     (when regexp
3157       (save-excursion
3158         (save-restriction
3159           (article-narrow-to-head)
3160           (goto-char (point-min))
3161           (while (re-search-forward
3162                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3163                   nil t)
3164             (delete-region (match-beginning 2) (match-end 0))
3165             (beginning-of-line))
3166           (when (re-search-forward
3167                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3168             (delete-region (match-beginning 1) (match-end 1))))))))
3169
3170 (defun article-hide-pem (&optional arg)
3171   "Toggle hiding of any PEM headers and signatures in the current article.
3172 If given a negative prefix, always show; if given a positive prefix,
3173 always hide."
3174   (interactive (gnus-article-hidden-arg))
3175   (unless (gnus-article-check-hidden-text 'pem arg)
3176     (save-excursion
3177       (let ((inhibit-read-only t) end)
3178         (goto-char (point-min))
3179         ;; Hide the horrendously ugly "header".
3180         (when (and (search-forward
3181                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3182                     nil t)
3183                    (setq end (1+ (match-beginning 0))))
3184           (gnus-add-wash-type 'pem)
3185           (gnus-article-hide-text-type
3186            end
3187            (if (search-forward "\n\n" nil t)
3188                (match-end 0)
3189              (point-max))
3190            'pem)
3191           ;; Hide the trailer as well
3192           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3193                                 nil t)
3194             (gnus-article-hide-text-type
3195              (match-beginning 0) (match-end 0) 'pem)))))))
3196
3197 (defun article-strip-banner ()
3198   "Strip the banners specified by the `banner' group parameter and by
3199 `gnus-article-address-banner-alist'."
3200   (interactive)
3201   (save-excursion
3202     (save-restriction
3203       (let ((inhibit-point-motion-hooks t))
3204         (when (gnus-parameter-banner gnus-newsgroup-name)
3205           (article-really-strip-banner
3206            (gnus-parameter-banner gnus-newsgroup-name)))
3207         (when gnus-article-address-banner-alist
3208           ;; Note that the From header is decoded here, so it is
3209           ;; required that the *-extract-address-components function
3210           ;; supports non-ASCII text.
3211           (let ((from (save-restriction
3212                         (widen)
3213                         (article-narrow-to-head)
3214                         (mail-fetch-field "from"))))
3215             (when (and from
3216                        (setq from
3217                              (cadr (funcall gnus-extract-address-components
3218                                             from))))
3219               (catch 'found
3220                 (dolist (pair gnus-article-address-banner-alist)
3221                   (when (string-match (car pair) from)
3222                     (throw 'found
3223                            (article-really-strip-banner (cdr pair)))))))))))))
3224
3225 (defun article-really-strip-banner (banner)
3226   "Strip the banner specified by the argument."
3227   (save-excursion
3228     (save-restriction
3229       (let ((inhibit-point-motion-hooks t)
3230             (gnus-signature-limit nil)
3231             (inhibit-read-only t))
3232         (article-goto-body)
3233         (cond
3234          ((eq banner 'signature)
3235           (when (gnus-article-narrow-to-signature)
3236             (widen)
3237             (forward-line -1)
3238             (delete-region (point) (point-max))))
3239          ((symbolp banner)
3240           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3241               (while (re-search-forward banner nil t)
3242                 (delete-region (match-beginning 0) (match-end 0)))))
3243          ((stringp banner)
3244           (while (re-search-forward banner nil t)
3245             (delete-region (match-beginning 0) (match-end 0)))))))))
3246
3247 (defun article-babel ()
3248   "Translate article using an online translation service."
3249   (interactive)
3250   (require 'babel)
3251   (gnus-with-article-buffer
3252     (when (article-goto-body)
3253       (let* ((start (point))
3254              (end (point-max))
3255              (orig (buffer-substring start end))
3256              (trans (babel-as-string orig)))
3257         (save-restriction
3258           (narrow-to-region start end)
3259           (delete-region start end)
3260           (insert trans))))))
3261
3262 (defun article-hide-signature (&optional arg)
3263   "Hide the signature in the current article.
3264 If given a negative prefix, always show; if given a positive prefix,
3265 always hide."
3266   (interactive (gnus-article-hidden-arg))
3267   (unless (gnus-article-check-hidden-text 'signature arg)
3268     (save-excursion
3269       (save-restriction
3270         (let ((inhibit-read-only t))
3271           (when (gnus-article-narrow-to-signature)
3272             (gnus-article-hide-text-type
3273              (point-min) (point-max) 'signature))))))
3274   (gnus-set-mode-line 'article))
3275
3276 (defun article-strip-headers-in-body ()
3277   "Strip offensive headers from bodies."
3278   (interactive)
3279   (save-excursion
3280     (article-goto-body)
3281     (let ((case-fold-search t))
3282       (when (looking-at "x-no-archive:")
3283         (gnus-delete-line)))))
3284
3285 (defun article-strip-leading-blank-lines ()
3286   "Remove all blank lines from the beginning of the article."
3287   (interactive)
3288   (save-excursion
3289     (let ((inhibit-point-motion-hooks t)
3290           (inhibit-read-only t))
3291       (when (article-goto-body)
3292         (while (and (not (eobp))
3293                     (looking-at "[ \t]*$"))
3294           (gnus-delete-line))))))
3295
3296 (defun article-narrow-to-head ()
3297   "Narrow the buffer to the head of the message.
3298 Point is left at the beginning of the narrowed-to region."
3299   (narrow-to-region
3300    (goto-char (point-min))
3301    (if (search-forward "\n\n" nil 1)
3302        (1- (point))
3303      (point-max)))
3304   (goto-char (point-min)))
3305
3306 (defun article-goto-body ()
3307   "Place point at the start of the body."
3308   (goto-char (point-min))
3309   (cond
3310    ;; This variable is only bound when dealing with separate
3311    ;; MIME body parts.
3312    (article-goto-body-goes-to-point-min-p
3313     t)
3314    ((search-forward "\n\n" nil t)
3315     t)
3316    (t
3317     (goto-char (point-max))
3318     nil)))
3319
3320 (defun article-strip-multiple-blank-lines ()
3321   "Replace consecutive blank lines with one empty line."
3322   (interactive)
3323   (save-excursion
3324     (let ((inhibit-point-motion-hooks t)
3325           (inhibit-read-only t))
3326       ;; First make all blank lines empty.
3327       (article-goto-body)
3328       (while (re-search-forward "^[ \t]+$" nil t)
3329         (unless (gnus-annotation-in-region-p
3330                  (match-beginning 0) (match-end 0))
3331           (replace-match "" nil t)))
3332       ;; Then replace multiple empty lines with a single empty line.
3333       (article-goto-body)
3334       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3335         (unless (gnus-annotation-in-region-p
3336                  (match-beginning 0) (match-end 0))
3337           (delete-region (match-beginning 1) (match-end 1)))))))
3338
3339 (defun article-strip-leading-space ()
3340   "Remove all white space from the beginning of the lines in the article."
3341   (interactive)
3342   (save-excursion
3343     (let ((inhibit-point-motion-hooks t)
3344           (inhibit-read-only t))
3345       (article-goto-body)
3346       (while (re-search-forward "^[ \t]+" nil t)
3347         (replace-match "" t t)))))
3348
3349 (defun article-strip-trailing-space ()
3350   "Remove all white space from the end of the lines in the article."
3351   (interactive)
3352   (save-excursion
3353     (let ((inhibit-point-motion-hooks t)
3354           (inhibit-read-only t))
3355       (article-goto-body)
3356       (while (re-search-forward "[ \t]+$" nil t)
3357         (replace-match "" t t)))))
3358
3359 (defun article-strip-blank-lines ()
3360   "Strip leading, trailing and multiple blank lines."
3361   (interactive)
3362   (article-strip-leading-blank-lines)
3363   (article-remove-trailing-blank-lines)
3364   (article-strip-multiple-blank-lines))
3365
3366 (defun article-strip-all-blank-lines ()
3367   "Strip all blank lines."
3368   (interactive)
3369   (save-excursion
3370     (let ((inhibit-point-motion-hooks t)
3371           (inhibit-read-only t))
3372       (article-goto-body)
3373       (while (re-search-forward "^[ \t]*\n" nil t)
3374         (replace-match "" t t)))))
3375
3376 (defun gnus-article-narrow-to-signature ()
3377   "Narrow to the signature; return t if a signature is found, else nil."
3378   (let ((inhibit-point-motion-hooks t))
3379     (when (gnus-article-search-signature)
3380       (forward-line 1)
3381       ;; Check whether we have some limits to what we consider
3382       ;; to be a signature.
3383       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3384                       (list gnus-signature-limit)))
3385             limit limited)
3386         (while (setq limit (pop limits))
3387           (if (or (and (integerp limit)
3388                        (< (- (point-max) (point)) limit))
3389                   (and (floatp limit)
3390                        (< (count-lines (point) (point-max)) limit))
3391                   (and (functionp limit)
3392                        (funcall limit))
3393                   (and (stringp limit)
3394                        (not (re-search-forward limit nil t))))
3395               ()                        ; This limit did not succeed.
3396             (setq limited t
3397                   limits nil)))
3398         (unless limited
3399           (narrow-to-region (point) (point-max))
3400           t)))))
3401
3402 (defun gnus-article-search-signature ()
3403   "Search the current buffer for the signature separator.
3404 Put point at the beginning of the signature separator."
3405   (let ((cur (point)))
3406     (goto-char (point-max))
3407     (if (if (stringp gnus-signature-separator)
3408             (re-search-backward gnus-signature-separator nil t)
3409           (let ((seps gnus-signature-separator))
3410             (while (and seps
3411                         (not (re-search-backward (car seps) nil t)))
3412               (pop seps))
3413             seps))
3414         t
3415       (goto-char cur)
3416       nil)))
3417
3418 (defun gnus-article-hidden-arg ()
3419   "Return the current prefix arg as a number, or 0 if no prefix."
3420   (list (if current-prefix-arg
3421             (prefix-numeric-value current-prefix-arg)
3422           0)))
3423
3424 (defun gnus-article-check-hidden-text (type arg)
3425   "Return nil if hiding is necessary.
3426 Arg can be nil or a number.  nil and positive means hide, negative
3427 means show, 0 means toggle."
3428   (save-excursion
3429     (save-restriction
3430       (let ((hide (gnus-article-hidden-text-p type)))
3431         (cond
3432          ((or (null arg)
3433               (> arg 0))
3434           nil)
3435          ((< arg 0)
3436           (gnus-article-show-hidden-text type)
3437           t)
3438          (t
3439           (if (eq hide 'hidden)
3440               (progn
3441                 (gnus-article-show-hidden-text type)
3442                 t)
3443             nil)))))))
3444
3445 (defun gnus-article-hidden-text-p (type)
3446   "Say whether the current buffer contains hidden text of type TYPE."
3447   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3448     (while (and pos
3449                 (not (get-text-property pos 'invisible))
3450                 (not (get-text-property pos 'dummy-invisible)))
3451       (setq pos
3452             (text-property-any (1+ pos) (point-max) 'article-type type)))
3453     (if pos
3454         'hidden
3455       nil)))
3456
3457 (defun gnus-article-show-hidden-text (type &optional dummy)
3458   "Show all hidden text of type TYPE.
3459 Originally it is hide instead of DUMMY."
3460   (let ((inhibit-read-only t)
3461         (inhibit-point-motion-hooks t))
3462     (gnus-remove-text-properties-when
3463      'article-type type
3464      (point-min) (point-max)
3465      (cons 'article-type (cons type
3466                                gnus-hidden-properties)))
3467     (gnus-delete-wash-type type)))
3468
3469 (defconst article-time-units
3470   `((year . ,(* 365.25 24 60 60))
3471     (week . ,(* 7 24 60 60))
3472     (day . ,(* 24 60 60))
3473     (hour . ,(* 60 60))
3474     (minute . 60)
3475     (second . 1))
3476   "Mapping from time units to seconds.")
3477
3478 (defun gnus-article-forward-header ()
3479   "Move point to the start of the next header.
3480 If the current header is a continuation header, this can be several
3481 lines forward."
3482   (let ((ended nil))
3483     (while (not ended)
3484       (forward-line 1)
3485       (if (looking-at "[ \t]+[^ \t]")
3486           (forward-line 1)
3487         (setq ended t)))))
3488
3489 (defun article-date-ut (&optional type highlight)
3490   "Convert DATE date to universal time in the current article.
3491 If TYPE is `local', convert to local time; if it is `lapsed', output
3492 how much time has lapsed since DATE.  For `lapsed', the value of
3493 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3494 should replace the \"Date:\" one, or should be added below it."
3495   (interactive (list 'ut t))
3496   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3497          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3498                              tdate-regexp)
3499                             ((eq type 'lapsed)
3500                              "^X-Sent:[ \t]")
3501                             (article-lapsed-timer
3502                              "^Date:[ \t]")
3503                             (t
3504                              tdate-regexp)))
3505          (case-fold-search t)
3506          (inhibit-read-only t)
3507          (inhibit-point-motion-hooks t)
3508          pos date bface eface)
3509     (save-excursion
3510       (save-restriction
3511         (widen)
3512         (goto-char (point-min))
3513         (while (or (setq date (get-text-property (setq pos (point))
3514                                                  'original-date))
3515                    (when (setq pos (next-single-property-change
3516                                     (point) 'original-date))
3517                      (setq date (get-text-property pos 'original-date))
3518                      t))
3519           (narrow-to-region
3520            pos (if (setq pos (text-property-any pos (point-max)
3521                                                 'original-date nil))
3522                    (progn
3523                      (goto-char pos)
3524                      (if (or (bolp) (eobp))
3525                          (point)
3526                        (1+ (point))))
3527                  (point-max)))
3528           (goto-char (point-min))
3529           (when (re-search-forward tdate-regexp nil t)
3530             (setq bface (get-text-property (point-at-bol) 'face)
3531                   eface (get-text-property (1- (point-at-eol)) 'face)))
3532           (goto-char (point-min))
3533           (setq pos nil)
3534           ;; Delete any old Date headers.
3535           (while (re-search-forward date-regexp nil t)
3536             (if pos
3537                 (delete-region (point-at-bol) (progn
3538                                                 (gnus-article-forward-header)
3539                                                 (point)))
3540               (delete-region (point-at-bol) (progn
3541                                               (gnus-article-forward-header)
3542                                               (forward-char -1)
3543                                               (point)))
3544               (setq pos (point))))
3545           (when (and (not pos)
3546                      (re-search-forward tdate-regexp nil t))
3547             (forward-line 1))
3548           (gnus-goto-char pos)
3549           (insert (article-make-date-line date (or type 'ut)))
3550           (unless pos
3551             (insert "\n")
3552             (forward-line -1))
3553           ;; Do highlighting.
3554           (beginning-of-line)
3555           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3556             (put-text-property (match-beginning 1) (1+ (match-end 1))
3557                                'face bface)
3558             (put-text-property (match-beginning 2) (match-end 2)
3559                                'face eface))
3560           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3561           (goto-char (point-max))
3562           (widen))))))
3563
3564 (defun article-make-date-line (date type)
3565   "Return a DATE line of TYPE."
3566   (unless (memq type '(local ut original user iso8601 lapsed english))
3567     (error "Unknown conversion type: %s" type))
3568   (condition-case ()
3569       (let ((time (date-to-time date)))
3570         (cond
3571          ;; Convert to the local timezone.
3572          ((eq type 'local)
3573           (concat "Date: " (message-make-date time)))
3574          ;; Convert to Universal Time.
3575          ((eq type 'ut)
3576           (concat "Date: "
3577                   (substring
3578                    (message-make-date
3579                     (let* ((e (parse-time-string date))
3580                            (tm (apply 'encode-time e))
3581                            (ms (car tm))
3582                            (ls (- (cadr tm) (car (current-time-zone time)))))
3583                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3584                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3585                             (t (list ms ls)))))
3586                    0 -5)
3587                   "UT"))
3588          ;; Get the original date from the article.
3589          ((eq type 'original)
3590           (concat "Date: " (if (string-match "\n+$" date)
3591                                (substring date 0 (match-beginning 0))
3592                              date)))
3593          ;; Let the user define the format.
3594          ((eq type 'user)
3595           (let ((format (or (condition-case nil
3596                                 (with-current-buffer gnus-summary-buffer
3597                                   gnus-article-time-format)
3598                               (error nil))
3599                             gnus-article-time-format)))
3600             (if (functionp format)
3601                 (funcall format time)
3602               (concat "Date: " (format-time-string format time)))))
3603          ;; ISO 8601.
3604          ((eq type 'iso8601)
3605           (let ((tz (car (current-time-zone time))))
3606             (concat
3607              "Date: "
3608              (format-time-string "%Y%m%dT%H%M%S" time)
3609              (format "%s%02d%02d"
3610                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3611                      (/ (% (abs tz) 3600) 60)))))
3612          ;; Do an X-Sent lapsed format.
3613          ((eq type 'lapsed)
3614           ;; If the date is seriously mangled, the timezone functions are
3615           ;; liable to bug out, so we ignore all errors.
3616           (let* ((now (current-time))
3617                  (real-time (subtract-time now time))
3618                  (real-sec (and real-time
3619                                 (+ (* (float (car real-time)) 65536)
3620                                    (cadr real-time))))
3621                  (sec (and real-time (abs real-sec)))
3622                  num prev)
3623             (cond
3624              ((null real-time)
3625               "X-Sent: Unknown")
3626              ((zerop sec)
3627               "X-Sent: Now")
3628              (t
3629               (concat
3630                "X-Sent: "
3631                ;; This is a bit convoluted, but basically we go
3632                ;; through the time units for years, weeks, etc,
3633                ;; and divide things to see whether that results
3634                ;; in positive answers.
3635                (mapconcat
3636                 (lambda (unit)
3637                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3638                       ;; The (remaining) seconds are too few to
3639                       ;; be divided into this time unit.
3640                       ""
3641                     ;; It's big enough, so we output it.
3642                     (setq sec (- sec (* num (cdr unit))))
3643                     (prog1
3644                         (concat (if prev ", " "") (int-to-string
3645                                                    (floor num))
3646                                 " " (symbol-name (car unit))
3647                                 (if (> num 1) "s" ""))
3648                       (setq prev t))))
3649                 article-time-units "")
3650                ;; If dates are odd, then it might appear like the
3651                ;; article was sent in the future.
3652                (if (> real-sec 0)
3653                    " ago"
3654                  " in the future"))))))
3655          ;; Display the date in proper English
3656          ((eq type 'english)
3657           (let ((dtime (decode-time time)))
3658             (concat
3659              "Date: the "
3660              (number-to-string (nth 3 dtime))
3661              (let ((digit (% (nth 3 dtime) 10)))
3662                (cond
3663                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3664                 ((= digit 1) "st")
3665                 ((= digit 2) "nd")
3666                 ((= digit 3) "rd")
3667                 (t "th")))
3668              " of "
3669              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3670              " "
3671              (number-to-string (nth 5 dtime))
3672              " at "
3673              (format "%02d" (nth 2 dtime))
3674              ":"
3675              (format "%02d" (nth 1 dtime)))))))
3676     (error
3677      (format "Date: %s (from Gnus)" date))))
3678
3679 (defun article-date-local (&optional highlight)
3680   "Convert the current article date to the local timezone."
3681   (interactive (list t))
3682   (article-date-ut 'local highlight))
3683
3684 (defun article-date-english (&optional highlight)
3685   "Convert the current article date to something that is proper English."
3686   (interactive (list t))
3687   (article-date-ut 'english highlight))
3688
3689 (defun article-date-original (&optional highlight)
3690   "Convert the current article date to what it was originally.
3691 This is only useful if you have used some other date conversion
3692 function and want to see what the date was before converting."
3693   (interactive (list t))
3694   (article-date-ut 'original highlight))
3695
3696 (defun article-date-lapsed (&optional highlight)
3697   "Convert the current article date to time lapsed since it was sent."
3698   (interactive (list t))
3699   (article-date-ut 'lapsed highlight))
3700
3701 (defun article-update-date-lapsed ()
3702   "Function to be run from a timer to update the lapsed time line."
3703   (save-match-data
3704     (let (deactivate-mark)
3705       (save-excursion
3706         (ignore-errors
3707          (walk-windows
3708           (lambda (w)
3709             (set-buffer (window-buffer w))
3710             (when (eq major-mode 'gnus-article-mode)
3711               (let ((mark (point-marker)))
3712                 (goto-char (point-min))
3713                 (when (re-search-forward "^X-Sent:" nil t)
3714                   (article-date-lapsed t))
3715                 (goto-char (marker-position mark))
3716                 (move-marker mark nil))))
3717           nil 'visible))))))
3718
3719 (defun gnus-start-date-timer (&optional n)
3720   "Start a timer to update the X-Sent header in the article buffers.
3721 The numerical prefix says how frequently (in seconds) the function
3722 is to run."
3723   (interactive "p")
3724   (unless n
3725     (setq n 1))
3726   (gnus-stop-date-timer)
3727   (setq article-lapsed-timer
3728         (run-at-time 1 n 'article-update-date-lapsed)))
3729
3730 (defun gnus-stop-date-timer ()
3731   "Stop the X-Sent timer."
3732   (interactive)
3733   (when article-lapsed-timer
3734     (nnheader-cancel-timer article-lapsed-timer)
3735     (setq article-lapsed-timer nil)))
3736
3737 (defun article-date-user (&optional highlight)
3738   "Convert the current article date to the user-defined format.
3739 This format is defined by the `gnus-article-time-format' variable."
3740   (interactive (list t))
3741   (article-date-ut 'user highlight))
3742
3743 (defun article-date-iso8601 (&optional highlight)
3744   "Convert the current article date to ISO8601."
3745   (interactive (list t))
3746   (article-date-ut 'iso8601 highlight))
3747
3748 (defmacro gnus-article-save-original-date (&rest forms)
3749   "Save the original date as a text property and evaluate FORMS."
3750   `(let* ((case-fold-search t)
3751           (start (progn
3752                    (goto-char (point-min))
3753                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3754                               (not (bolp)))
3755                      (match-end 0))))
3756           (date (when (and start
3757                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3758                                               nil t))
3759                   (buffer-substring-no-properties start
3760                                                   (match-beginning 0)))))
3761      (goto-char (point-max))
3762      (skip-chars-backward "\n")
3763      (put-text-property (point-min) (point) 'original-date date)
3764      ,@forms
3765      (goto-char (point-max))
3766      (skip-chars-backward "\n")
3767      (put-text-property (point-min) (point) 'original-date date)))
3768
3769 ;; (defun article-show-all ()
3770 ;;   "Show all hidden text in the article buffer."
3771 ;;   (interactive)
3772 ;;   (save-excursion
3773 ;;     (let ((inhibit-read-only t))
3774 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3775
3776 (defun article-remove-leading-whitespace ()
3777   "Remove excessive whitespace from all headers."
3778   (interactive)
3779   (save-excursion
3780     (save-restriction
3781       (let ((inhibit-read-only t))
3782         (article-narrow-to-head)
3783         (goto-char (point-min))
3784         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3785           (delete-region (match-beginning 1) (match-end 1)))))))
3786
3787 (defun article-emphasize (&optional arg)
3788   "Emphasize text according to `gnus-emphasis-alist'."
3789   (interactive (gnus-article-hidden-arg))
3790   (unless (gnus-article-check-hidden-text 'emphasis arg)
3791     (save-excursion
3792       (let ((alist (or
3793                     (condition-case nil
3794                         (with-current-buffer gnus-summary-buffer
3795                           gnus-article-emphasis-alist)
3796                       (error))
3797                     gnus-emphasis-alist))
3798             (inhibit-read-only t)
3799             (props (append '(article-type emphasis)
3800                            gnus-hidden-properties))
3801             regexp elem beg invisible visible face)
3802         (article-goto-body)
3803         (setq beg (point))
3804         (while (setq elem (pop alist))
3805           (goto-char beg)
3806           (setq regexp (car elem)
3807                 invisible (nth 1 elem)
3808                 visible (nth 2 elem)
3809                 face (nth 3 elem))
3810           (while (re-search-forward regexp nil t)
3811             (when (and (match-beginning visible) (match-beginning invisible))
3812               (gnus-article-hide-text
3813                (match-beginning invisible) (match-end invisible) props)
3814               (gnus-article-unhide-text-type
3815                (match-beginning visible) (match-end visible) 'emphasis)
3816               (gnus-put-overlay-excluding-newlines
3817                (match-beginning visible) (match-end visible) 'face face)
3818               (gnus-add-wash-type 'emphasis)
3819               (goto-char (match-end invisible)))))))))
3820
3821 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3822   "Setup newsgroup emphasis alist."
3823   (unless gnus-article-emphasis-alist
3824     (let ((name (and gnus-newsgroup-name
3825                      (gnus-group-real-name gnus-newsgroup-name))))
3826       (make-local-variable 'gnus-article-emphasis-alist)
3827       (setq gnus-article-emphasis-alist
3828             (nconc
3829              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3830                (while (setq elem (pop alist))
3831                  (when (and name (string-match (car elem) name))
3832                    (setq alist nil
3833                          highlight (copy-sequence (cdr elem)))))
3834                highlight)
3835              (copy-sequence highlight-words)
3836              (if gnus-newsgroup-name
3837                  (copy-sequence (gnus-group-find-parameter
3838                                  gnus-newsgroup-name 'highlight-words t)))
3839              gnus-emphasis-alist)))))
3840
3841 (defvar gnus-summary-article-menu)
3842 (defvar gnus-summary-post-menu)
3843
3844 ;;; Saving functions.
3845
3846 (defun gnus-article-save (save-buffer file &optional num)
3847   "Save the currently selected article."
3848   (when (or (get gnus-default-article-saver :headers)
3849             (not gnus-save-all-headers))
3850     ;; Remove headers according to `gnus-saved-headers' or the value
3851     ;; of the `:headers' property that the saver function might have.
3852     (let ((gnus-visible-headers
3853            (or (symbol-value (get gnus-default-article-saver :headers))
3854                gnus-saved-headers gnus-visible-headers))
3855           ;; Ignore group parameter.  See `article-hide-headers'.
3856           (gnus-summary-buffer nil))
3857       (with-current-buffer save-buffer
3858         (article-hide-headers 1 t))))
3859   (save-window-excursion
3860     (if (not gnus-default-article-saver)
3861         (error "No default saver is defined")
3862       ;; !!! Magic!  The saving functions all save
3863       ;; `gnus-save-article-buffer' (or so they think), but we
3864       ;; bind that variable to our save-buffer.
3865       (set-buffer gnus-article-buffer)
3866       (let* ((gnus-save-article-buffer save-buffer)
3867              (filename
3868               (cond
3869                ((not gnus-prompt-before-saving) 'default)
3870                ((eq gnus-prompt-before-saving 'always) nil)
3871                (t file)))
3872              (gnus-number-of-articles-to-be-saved
3873               (when (eq gnus-prompt-before-saving t)
3874                 num)))                  ; Magic
3875         (set-buffer gnus-article-current-summary)
3876         (funcall gnus-default-article-saver filename)))))
3877
3878 (defun gnus-read-save-file-name (prompt &optional filename
3879                                         function group headers variable
3880                                         dir-var)
3881   (let ((default-name
3882           (funcall function group headers (symbol-value variable)))
3883         result)
3884     (setq result
3885           (expand-file-name
3886            (cond
3887             ((eq filename 'default)
3888              default-name)
3889             ((eq filename t)
3890              default-name)
3891             (filename filename)
3892             (t
3893              (when (symbol-value dir-var)
3894                (setq default-name (expand-file-name
3895                                    (file-name-nondirectory default-name)
3896                                    (symbol-value dir-var))))
3897              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3898                     (prompt
3899                      (format prompt
3900                              (if (and gnus-number-of-articles-to-be-saved
3901                                       (> gnus-number-of-articles-to-be-saved 1))
3902                                  (format "these %d articles"
3903                                          gnus-number-of-articles-to-be-saved)
3904                                "this article")))
3905                     (file
3906                      ;; Let the split methods have their say.
3907                      (cond
3908                       ;; No split name was found.
3909                       ((null split-name)
3910                        (read-file-name
3911                         (concat prompt " (default "
3912                                 (file-name-nondirectory default-name) "): ")
3913                         (file-name-directory default-name)
3914                         default-name))
3915                       ;; A single group name is returned.
3916                       ((stringp split-name)
3917                        (setq default-name
3918                              (funcall function split-name headers
3919                                       (symbol-value variable)))
3920                        (read-file-name
3921                         (concat prompt " (default "
3922                                 (file-name-nondirectory default-name) "): ")
3923                         (file-name-directory default-name)
3924                         default-name))
3925                       ;; A single split name was found
3926                       ((= 1 (length split-name))
3927                        (let* ((name (expand-file-name
3928                                      (car split-name)
3929                                      gnus-article-save-directory))
3930                               (dir (cond ((file-directory-p name)
3931                                           (file-name-as-directory name))
3932                                          ((file-exists-p name) name)
3933                                          (t gnus-article-save-directory))))
3934                          (read-file-name
3935                           (concat prompt " (default " name "): ")
3936                           dir name)))
3937                       ;; A list of splits was found.
3938                       (t
3939                        (setq split-name (nreverse split-name))
3940                        (let (result)
3941                          (let ((file-name-history
3942                                 (nconc split-name file-name-history)))
3943                            (setq result
3944                                  (expand-file-name
3945                                   (read-file-name
3946                                    (concat prompt " (`M-p' for defaults): ")
3947                                    gnus-article-save-directory
3948                                    (car split-name))
3949                                   gnus-article-save-directory)))
3950                          (car (push result file-name-history)))))))
3951                ;; Create the directory.
3952                (gnus-make-directory (file-name-directory file))
3953                ;; If we have read a directory, we append the default file name.
3954                (when (file-directory-p file)
3955                  (setq file (expand-file-name (file-name-nondirectory
3956                                                default-name)
3957                                               (file-name-as-directory file))))
3958                ;; Possibly translate some characters.
3959                (nnheader-translate-file-chars file))))))
3960     (gnus-make-directory (file-name-directory result))
3961     (when variable
3962       (set variable result))
3963     (when dir-var
3964       (set dir-var (file-name-directory result)))
3965     result))
3966
3967 (defun gnus-article-archive-name (group)
3968   "Return the first instance of an \"Archive-name\" in the current buffer."
3969   (let ((case-fold-search t))
3970     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3971       (nnheader-concat gnus-article-save-directory
3972                        (match-string 1)))))
3973
3974 (defun gnus-article-nndoc-name (group)
3975   "If GROUP is an nndoc group, return the name of the parent group."
3976   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3977     (gnus-group-get-parameter group 'save-article-group)))
3978
3979 (defun gnus-summary-save-in-rmail (&optional filename)
3980   "Append this article to Rmail file.
3981 Optional argument FILENAME specifies file name.
3982 Directory to save to is default to `gnus-article-save-directory'."
3983   (setq filename (gnus-read-save-file-name
3984                   "Save %s in rmail file" filename
3985                   gnus-rmail-save-name gnus-newsgroup-name
3986                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3987   (gnus-eval-in-buffer-window gnus-save-article-buffer
3988     (save-excursion
3989       (save-restriction
3990         (widen)
3991         ;; Note that unlike gnus-summary-save-in-mail, there is no
3992         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3993         ;; not use Babyl.
3994         (gnus-output-to-rmail filename))))
3995   filename)
3996
3997 (defun gnus-summary-save-in-mail (&optional filename)
3998   "Append this article to Unix mail file.
3999 Optional argument FILENAME specifies file name.
4000 Directory to save to is default to `gnus-article-save-directory'."
4001   (setq filename (gnus-read-save-file-name
4002                   "Save %s in Unix mail file" filename
4003                   gnus-mail-save-name gnus-newsgroup-name
4004                   gnus-current-headers 'gnus-newsgroup-last-mail))
4005   (gnus-eval-in-buffer-window gnus-save-article-buffer
4006     (save-excursion
4007       (save-restriction
4008         (widen)
4009         (if (and (file-readable-p filename)
4010                  (file-regular-p filename)
4011                  (mail-file-babyl-p filename))
4012             (gnus-output-to-rmail filename)
4013           (gnus-output-to-mail filename)))))
4014   filename)
4015
4016 (put 'gnus-summary-save-in-file :decode t)
4017 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
4018 (defun gnus-summary-save-in-file (&optional filename overwrite)
4019   "Append this article to file.
4020 Optional argument FILENAME specifies file name.
4021 Directory to save to is default to `gnus-article-save-directory'."
4022   (setq filename (gnus-read-save-file-name
4023                   "Save %s in file" filename
4024                   gnus-file-save-name gnus-newsgroup-name
4025                   gnus-current-headers 'gnus-newsgroup-last-file))
4026   (gnus-eval-in-buffer-window gnus-save-article-buffer
4027     (save-excursion
4028       (save-restriction
4029         (widen)
4030         (when (and overwrite
4031                    (file-exists-p filename))
4032           (delete-file filename))
4033         (gnus-output-to-file filename))))
4034   filename)
4035
4036 (put 'gnus-summary-write-to-file :decode t)
4037 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4038 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4039 (defun gnus-summary-write-to-file (&optional filename)
4040   "Write this article to a file, overwriting it if the file exists.
4041 Optional argument FILENAME specifies file name.
4042 The directory to save in defaults to `gnus-article-save-directory'."
4043   (setq filename (gnus-read-save-file-name
4044                   "Save %s in file" filename
4045                   gnus-file-save-name gnus-newsgroup-name
4046                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4047   (gnus-summary-save-in-file filename t))
4048
4049 (put 'gnus-summary-save-body-in-file :decode t)
4050 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4051   "Append this article body to a file.
4052 Optional argument FILENAME specifies file name.
4053 The directory to save in defaults to `gnus-article-save-directory'."
4054   (setq filename (gnus-read-save-file-name
4055                   "Save %s body in file" filename
4056                   gnus-file-save-name gnus-newsgroup-name
4057                   gnus-current-headers 'gnus-newsgroup-last-file))
4058   (gnus-eval-in-buffer-window gnus-save-article-buffer
4059     (save-excursion
4060       (save-restriction
4061         (widen)
4062         (when (article-goto-body)
4063           (narrow-to-region (point) (point-max)))
4064         (when (and overwrite
4065                    (file-exists-p filename))
4066           (delete-file filename))
4067         (gnus-output-to-file filename))))
4068   filename)
4069
4070 (put 'gnus-summary-write-body-to-file :decode t)
4071 (put 'gnus-summary-write-body-to-file
4072      :function 'gnus-summary-save-body-in-file)
4073 (defun gnus-summary-write-body-to-file (&optional filename)
4074   "Write this article body to a file, overwriting it if the file exists.
4075 Optional argument FILENAME specifies file name.
4076 The directory to save in defaults to `gnus-article-save-directory'."
4077   (setq filename (gnus-read-save-file-name
4078                   "Save %s body in file" filename
4079                   gnus-file-save-name gnus-newsgroup-name
4080                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4081   (gnus-summary-save-body-in-file filename t))
4082
4083 (put 'gnus-summary-save-in-pipe :decode t)
4084 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4085 (defun gnus-summary-save-in-pipe (&optional command raw)
4086   "Pipe this article to subprocess COMMAND.
4087 Valid values for COMMAND include:
4088   a string
4089     The executable command name and possibly arguments.
4090   nil
4091     You will be prompted for the command in the minibuffer.
4092   the symbol `default'
4093     It will be replaced with the command which the variable
4094     `gnus-summary-pipe-output-default-command' holds or the command
4095     last used for saving.
4096 Non-nil value for RAW overrides `:decode' and `:headers' properties
4097 and the raw article including all headers will be piped."
4098   (let ((article (gnus-summary-article-number))
4099         (decode (unless raw
4100                   (get 'gnus-summary-save-in-pipe :decode)))
4101         save-buffer default)
4102     (if article
4103         (if (vectorp (gnus-summary-article-header article))
4104             (save-current-buffer
4105               (gnus-summary-select-article decode decode nil article)
4106               (insert-buffer-substring
4107                (prog1
4108                    (if decode
4109                        gnus-article-buffer
4110                      gnus-original-article-buffer)
4111                  (setq save-buffer
4112                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4113               ;; Remove unwanted headers.
4114               (when (and (not raw)
4115                          (or (get 'gnus-summary-save-in-pipe :headers)
4116                              (not gnus-save-all-headers)))
4117                 (let ((gnus-visible-headers
4118                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4119                                               :headers))
4120                            gnus-saved-headers gnus-visible-headers))
4121                       (gnus-summary-buffer nil))
4122                   (article-hide-headers 1 t))))
4123           (error "%d is not a real article" article))
4124       (error "No article to pipe"))
4125     (setq default (or gnus-summary-pipe-output-default-command
4126                       gnus-last-shell-command))
4127     (unless (stringp command)
4128       (setq command
4129             (if (and (eq command 'default) default)
4130                 default
4131               (gnus-read-shell-command "Shell command on this article: "
4132                                        default))))
4133     (when (string-equal command "")
4134       (if default
4135           (setq command default)
4136         (error "A command is required")))
4137     (gnus-eval-in-buffer-window save-buffer
4138       (save-restriction
4139         (widen)
4140         (shell-command-on-region (point-min) (point-max) command nil)))
4141     (gnus-kill-buffer save-buffer))
4142   (setq gnus-summary-pipe-output-default-command command))
4143
4144 (defun gnus-summary-pipe-to-muttprint (&optional command)
4145   "Pipe this article to muttprint."
4146   (unless (stringp command)
4147     (setq command (read-string
4148                    "Print using command: " gnus-summary-muttprint-program
4149                    nil gnus-summary-muttprint-program)))
4150   (let ((gnus-summary-pipe-output-default-command
4151          gnus-summary-pipe-output-default-command))
4152     (gnus-summary-save-in-pipe command))
4153   (setq gnus-summary-muttprint-program command))
4154
4155 ;;; Article file names when saving.
4156
4157 (defun gnus-capitalize-newsgroup (newsgroup)
4158   "Capitalize NEWSGROUP name."
4159   (when (not (zerop (length newsgroup)))
4160     (concat (char-to-string (upcase (aref newsgroup 0)))
4161             (substring newsgroup 1))))
4162
4163 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4164   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4165 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4166 Otherwise, it is like ~/News/news/group/num."
4167   (let ((default
4168           (expand-file-name
4169            (concat (if (gnus-use-long-file-name 'not-save)
4170                        (gnus-capitalize-newsgroup newsgroup)
4171                      (gnus-newsgroup-directory-form newsgroup))
4172                    "/" (int-to-string (mail-header-number headers)))
4173            gnus-article-save-directory)))
4174     (if (and last-file
4175              (string-equal (file-name-directory default)
4176                            (file-name-directory last-file))
4177              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4178         default
4179       (or last-file default))))
4180
4181 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4182   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4183 If variable `gnus-use-long-file-name' is non-nil, it is
4184 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4185   (let ((default
4186           (expand-file-name
4187            (concat (if (gnus-use-long-file-name 'not-save)
4188                        newsgroup
4189                      (gnus-newsgroup-directory-form newsgroup))
4190                    "/" (int-to-string (mail-header-number headers)))
4191            gnus-article-save-directory)))
4192     (if (and last-file
4193              (string-equal (file-name-directory default)
4194                            (file-name-directory last-file))
4195              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4196         default
4197       (or last-file default))))
4198
4199 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4200   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4201 If variable `gnus-use-long-file-name' is non-nil, it is
4202 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4203   (or last-file
4204       (expand-file-name
4205        (if (gnus-use-long-file-name 'not-save)
4206            newsgroup
4207          (file-relative-name
4208           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4209           default-directory))
4210        gnus-article-save-directory)))
4211
4212 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4213   "Generate file name from sender."
4214   (let ((from (mail-header-from headers)))
4215     (expand-file-name
4216      (if (and from (string-match "\\([^ <]+\\)@" from))
4217          (match-string 1 from)
4218        "nobody")
4219      gnus-article-save-directory)))
4220
4221 (defun article-verify-x-pgp-sig ()
4222   "Verify X-PGP-Sig."
4223   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4224   (interactive)
4225   (if (gnus-buffer-live-p gnus-original-article-buffer)
4226       (let ((sig (with-current-buffer gnus-original-article-buffer
4227                    (gnus-fetch-field "X-PGP-Sig")))
4228             items info headers)
4229         (when (and sig
4230                    mml2015-use
4231                    (mml2015-clear-verify-function))
4232           (with-temp-buffer
4233             (insert-buffer-substring gnus-original-article-buffer)
4234             (setq items (split-string sig))
4235             (message-narrow-to-head)
4236             (let ((inhibit-point-motion-hooks t)
4237                   (case-fold-search t))
4238               ;; Don't verify multiple headers.
4239               (setq headers (mapconcat (lambda (header)
4240                                          (concat header ": "
4241                                                  (mail-fetch-field header)
4242                                                  "\n"))
4243                                        (split-string (nth 1 items) ",") "")))
4244             (delete-region (point-min) (point-max))
4245             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4246             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4247             (insert headers)
4248             (widen)
4249             (forward-line)
4250             (while (not (eobp))
4251               (if (looking-at "^-")
4252                   (insert "- "))
4253               (forward-line))
4254             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4255             (insert "Version: " (car items) "\n\n")
4256             (insert (mapconcat 'identity (cddr items) "\n"))
4257             (insert "\n-----END PGP SIGNATURE-----\n")
4258             (let ((mm-security-handle (list (format "multipart/signed"))))
4259               (mml2015-clean-buffer)
4260               (let ((coding-system-for-write (or gnus-newsgroup-charset
4261                                                  'iso-8859-1)))
4262                 (funcall (mml2015-clear-verify-function)))
4263               (setq info
4264                     (or (mm-handle-multipart-ctl-parameter
4265                          mm-security-handle 'gnus-details)
4266                         (mm-handle-multipart-ctl-parameter
4267                          mm-security-handle 'gnus-info)))))
4268           (when info
4269             (let ((inhibit-read-only t) bface eface)
4270               (save-restriction
4271                 (message-narrow-to-head)
4272                 (goto-char (point-max))
4273                 (forward-line -1)
4274                 (setq bface (get-text-property (point-at-bol) 'face)
4275                       eface (get-text-property (1- (point-at-eol)) 'face))
4276                 (message-remove-header "X-Gnus-PGP-Verify")
4277                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4278                     (forward-line)
4279                   (goto-char (point-max)))
4280                 (narrow-to-region (point) (point))
4281                 (insert "X-Gnus-PGP-Verify: " info "\n")
4282                 (goto-char (point-min))
4283                 (forward-line)
4284                 (while (not (eobp))
4285                   (if (not (looking-at "^[ \t]"))
4286                       (insert " "))
4287                   (forward-line))
4288                 ;; Do highlighting.
4289                 (goto-char (point-min))
4290                 (when (looking-at "\\([^:]+\\): *")
4291                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4292                                      'face bface)
4293                   (put-text-property (match-end 0) (point-max)
4294                                      'face eface)))))))))
4295
4296 (autoload 'canlock-verify "canlock" nil t) ;; for XEmacs.
4297
4298 (defun article-verify-cancel-lock ()
4299   "Verify Cancel-Lock header."
4300   (interactive)
4301   (if (gnus-buffer-live-p gnus-original-article-buffer)
4302       (canlock-verify gnus-original-article-buffer)))
4303
4304 (eval-and-compile
4305   (mapc
4306    (lambda (func)
4307      (let (afunc gfunc)
4308        (if (consp func)
4309            (setq afunc (car func)
4310                  gfunc (cdr func))
4311          (setq afunc func
4312                gfunc (intern (format "gnus-%s" func))))
4313        (defalias gfunc
4314          (when (fboundp afunc)
4315            `(lambda (&optional interactive &rest args)
4316               ,(documentation afunc t)
4317               (interactive (list t))
4318               (with-current-buffer gnus-article-buffer
4319                 (if interactive
4320                     (call-interactively ',afunc)
4321                   (apply ',afunc args))))))))
4322    '(article-hide-headers
4323      article-verify-x-pgp-sig
4324      article-verify-cancel-lock
4325      article-hide-boring-headers
4326      article-treat-overstrike
4327      article-treat-ansi-sequences
4328      article-fill-long-lines
4329      article-capitalize-sentences
4330      article-remove-cr
4331      article-remove-leading-whitespace
4332      article-display-x-face
4333      article-display-face
4334      article-de-quoted-unreadable
4335      article-de-base64-unreadable
4336      article-decode-HZ
4337      article-wash-html
4338      article-unsplit-urls
4339      article-hide-list-identifiers
4340      article-strip-banner
4341      article-babel
4342      article-hide-pem
4343      article-hide-signature
4344      article-strip-headers-in-body
4345      article-remove-trailing-blank-lines
4346      article-strip-leading-blank-lines
4347      article-strip-multiple-blank-lines
4348      article-strip-leading-space
4349      article-strip-trailing-space
4350      article-strip-blank-lines
4351      article-strip-all-blank-lines
4352      article-date-local
4353      article-date-english
4354      article-date-iso8601
4355      article-date-original
4356      article-date-ut
4357      article-decode-mime-words
4358      article-decode-charset
4359      article-decode-encoded-words
4360      article-date-user
4361      article-date-lapsed
4362      article-emphasize
4363      article-treat-dumbquotes
4364      article-normalize-headers
4365      ;;(article-show-all . gnus-article-show-all-headers)
4366      )))
4367 \f
4368 ;;;
4369 ;;; Gnus article mode
4370 ;;;
4371
4372 (put 'gnus-article-mode 'mode-class 'special)
4373
4374 (set-keymap-parent gnus-article-mode-map widget-keymap)
4375
4376 (gnus-define-keys gnus-article-mode-map
4377   " " gnus-article-goto-next-page
4378   "\177" gnus-article-goto-prev-page
4379   [delete] gnus-article-goto-prev-page
4380   [backspace] gnus-article-goto-prev-page
4381   "\C-c^" gnus-article-refer-article
4382   "h" gnus-article-show-summary
4383   "s" gnus-article-show-summary
4384   "\C-c\C-m" gnus-article-mail
4385   "?" gnus-article-describe-briefly
4386   "<" beginning-of-buffer
4387   ">" end-of-buffer
4388   "\C-c\C-i" gnus-info-find-node
4389   "\C-c\C-b" gnus-bug
4390   "R" gnus-article-reply-with-original
4391   "F" gnus-article-followup-with-original
4392   "\C-hk" gnus-article-describe-key
4393   "\C-hc" gnus-article-describe-key-briefly
4394   "\C-hb" gnus-article-describe-bindings
4395
4396   "e" gnus-article-read-summary-keys
4397   "\C-d" gnus-article-read-summary-keys
4398   "\M-*" gnus-article-read-summary-keys
4399   "\M-#" gnus-article-read-summary-keys
4400   "\M-^" gnus-article-read-summary-keys
4401   "\M-g" gnus-article-read-summary-keys)
4402
4403 (substitute-key-definition
4404  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4405
4406 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4407   "W" gnus-article-wide-reply-with-original)
4408 (if (featurep 'xemacs)
4409     (set-keymap-default-binding gnus-article-send-map
4410                                 'gnus-article-read-summary-send-keys)
4411   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4412
4413 (defun gnus-article-make-menu-bar ()
4414   (unless (boundp 'gnus-article-commands-menu)
4415     (gnus-summary-make-menu-bar))
4416   (gnus-turn-off-edit-menu 'article)
4417   (unless (boundp 'gnus-article-article-menu)
4418     (easy-menu-define
4419      gnus-article-article-menu gnus-article-mode-map ""
4420      '("Article"
4421        ["Scroll forwards" gnus-article-goto-next-page t]
4422        ["Scroll backwards" gnus-article-goto-prev-page t]
4423        ["Show summary" gnus-article-show-summary t]
4424        ["Fetch Message-ID at point" gnus-article-refer-article t]
4425        ["Mail to address at point" gnus-article-mail t]
4426        ["Send a bug report" gnus-bug t]))
4427
4428     (easy-menu-define
4429      gnus-article-treatment-menu gnus-article-mode-map ""
4430      ;; Fixme: this should use :active (and maybe :visible).
4431      '("Treatment"
4432        ["Hide headers" gnus-article-hide-headers t]
4433        ["Hide signature" gnus-article-hide-signature t]
4434        ["Hide citation" gnus-article-hide-citation t]
4435        ["Treat overstrike" gnus-article-treat-overstrike t]
4436        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4437        ["Remove carriage return" gnus-article-remove-cr t]
4438        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4439        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4440        ["Remove base64" gnus-article-de-base64-unreadable t]
4441        ["Treat html" gnus-article-wash-html t]
4442        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4443        ["Decode HZ" gnus-article-decode-HZ t]))
4444
4445     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4446
4447     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4448
4449     (gnus-run-hooks 'gnus-article-menu-hook)))
4450
4451 (defvar bookmark-make-record-function)
4452
4453 (defun gnus-article-mode ()
4454   "Major mode for displaying an article.
4455
4456 All normal editing commands are switched off.
4457
4458 The following commands are available in addition to all summary mode
4459 commands:
4460 \\<gnus-article-mode-map>
4461 \\[gnus-article-next-page]\t Scroll the article one page forwards
4462 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4463 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4464 \\[gnus-article-show-summary]\t Display the summary buffer
4465 \\[gnus-article-mail]\t Send a reply to the address near point
4466 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4467 \\[gnus-info-find-node]\t Go to the Gnus info node"
4468   (interactive)
4469   (kill-all-local-variables)
4470   (gnus-simplify-mode-line)
4471   (setq mode-name "Article")
4472   (setq major-mode 'gnus-article-mode)
4473   (make-local-variable 'minor-mode-alist)
4474   (use-local-map gnus-article-mode-map)
4475   (when (gnus-visual-p 'article-menu 'menu)
4476     (gnus-article-make-menu-bar)
4477     (when gnus-summary-tool-bar-map
4478       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4479   (gnus-update-format-specifications nil 'article-mode)
4480   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4481   (set (make-local-variable 'gnus-page-broken) nil)
4482   (make-local-variable 'gnus-button-marker-list)
4483   (make-local-variable 'gnus-article-current-summary)
4484   (make-local-variable 'gnus-article-mime-handles)
4485   (make-local-variable 'gnus-article-decoded-p)
4486   (make-local-variable 'gnus-article-mime-handle-alist)
4487   (make-local-variable 'gnus-article-wash-types)
4488   (make-local-variable 'gnus-article-image-alist)
4489   (make-local-variable 'gnus-article-charset)
4490   (make-local-variable 'gnus-article-ignored-charsets)
4491   (set (make-local-variable 'bookmark-make-record-function)
4492        'gnus-summary-bookmark-make-record)
4493   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4494   ;; face.
4495   (set (make-local-variable 'nobreak-char-display) nil)
4496   (setq cursor-in-non-selected-windows nil)
4497   (setq truncate-lines gnus-article-truncate-lines)
4498   (gnus-set-default-directory)
4499   (buffer-disable-undo)
4500   (setq buffer-read-only t
4501         show-trailing-whitespace nil)
4502   (set-syntax-table gnus-article-mode-syntax-table)
4503   (mm-enable-multibyte)
4504   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4505
4506 (defvar gnus-button-marker-list nil
4507   "Regexp matching any of the regexps from `gnus-button-alist'.
4508 Internal variable.")
4509
4510 (defun gnus-article-setup-buffer ()
4511   "Initialize the article buffer."
4512   (let* ((name (if gnus-single-article-buffer "*Article*"
4513                  (concat "*Article " gnus-newsgroup-name "*")))
4514          (original
4515           (progn (string-match "\\*Article" name)
4516                  (concat " *Original Article"
4517                          (substring name (match-end 0))))))
4518     (setq gnus-article-buffer name)
4519     (setq gnus-original-article-buffer original)
4520     (setq gnus-article-mime-handle-alist nil)
4521     (with-current-buffer gnus-summary-buffer
4522       ;; This might be a variable local to the summary buffer.
4523       (unless gnus-single-article-buffer
4524         (setq gnus-article-buffer name)
4525         (setq gnus-original-article-buffer original)
4526         (gnus-set-global-variables)))
4527     (gnus-article-setup-highlight-words)
4528     ;; Init original article buffer.
4529     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4530       (mm-enable-multibyte)
4531       (setq major-mode 'gnus-original-article-mode)
4532       (make-local-variable 'gnus-original-article))
4533     (if (and (get-buffer name)
4534              (with-current-buffer name
4535                (if gnus-article-edit-mode
4536                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4537                        (progn
4538                          (set-buffer-modified-p nil)
4539                          (gnus-kill-buffer name)
4540                          (message "")
4541                          nil)
4542                      (error "Action aborted"))
4543                  t)))
4544         (with-current-buffer name
4545           (set (make-local-variable 'gnus-article-edit-mode) nil)
4546           (when gnus-article-mime-handles
4547             (mm-destroy-parts gnus-article-mime-handles)
4548             (setq gnus-article-mime-handles nil))
4549           ;; Set it to nil in article-buffer!
4550           (setq gnus-article-mime-handle-alist nil)
4551           (buffer-disable-undo)
4552           (setq buffer-read-only t)
4553           ;; This list just keeps growing if we don't reset it.
4554           (setq gnus-button-marker-list nil)
4555           (unless (eq major-mode 'gnus-article-mode)
4556             (gnus-article-mode))
4557           (current-buffer))
4558       (with-current-buffer (gnus-get-buffer-create name)
4559         (gnus-article-mode)
4560         (make-local-variable 'gnus-summary-buffer)
4561         (setq gnus-summary-buffer
4562               (gnus-summary-buffer-name gnus-newsgroup-name))
4563         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4564         (current-buffer)))))
4565
4566 ;; Set article window start at LINE, where LINE is the number of lines
4567 ;; from the head of the article.
4568 (defun gnus-article-set-window-start (&optional line)
4569   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4570     (when article-window
4571       (set-window-start
4572        article-window
4573        (with-current-buffer gnus-article-buffer
4574          (goto-char (point-min))
4575          (if (not line)
4576              (point-min)
4577            (gnus-message 6 "Moved to bookmark")
4578            (search-forward "\n\n" nil t)
4579            (forward-line line)
4580            (point)))))))
4581
4582 (defun gnus-article-prepare (article &optional all-headers header)
4583   "Prepare ARTICLE in article mode buffer.
4584 ARTICLE should either be an article number or a Message-ID.
4585 If ARTICLE is an id, HEADER should be the article headers.
4586 If ALL-HEADERS is non-nil, no headers are hidden."
4587   (save-excursion
4588     ;; Make sure we start in a summary buffer.
4589     (unless (eq major-mode 'gnus-summary-mode)
4590       (set-buffer gnus-summary-buffer))
4591     (setq gnus-summary-buffer (current-buffer))
4592     (let* ((gnus-article (if header (mail-header-number header) article))
4593            (summary-buffer (current-buffer))
4594            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4595            (group gnus-newsgroup-name)
4596            result)
4597       (save-excursion
4598         (gnus-article-setup-buffer)
4599         (set-buffer gnus-article-buffer)
4600         ;; Deactivate active regions.
4601         (when (and (boundp 'transient-mark-mode)
4602                    transient-mark-mode)
4603           (setq mark-active nil))
4604         (if (not (setq result (let ((inhibit-read-only t))
4605                                 (gnus-request-article-this-buffer
4606                                  article group))))
4607             ;; There is no such article.
4608             (save-excursion
4609               (when (and (numberp article)
4610                          (not (memq article gnus-newsgroup-sparse)))
4611                 (setq gnus-article-current
4612                       (cons gnus-newsgroup-name article))
4613                 (set-buffer gnus-summary-buffer)
4614                 (setq gnus-current-article article)
4615                 (if (and (memq article gnus-newsgroup-undownloaded)
4616                          (not (gnus-online (gnus-find-method-for-group
4617                                             gnus-newsgroup-name))))
4618                     (progn
4619                       (gnus-summary-set-agent-mark article)
4620                       (message "Message marked for downloading"))
4621                   (gnus-summary-mark-article article gnus-canceled-mark)
4622                   (unless (memq article gnus-newsgroup-sparse)
4623                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4624           (if (or (eq result 'pseudo)
4625                   (eq result 'nneething))
4626               (progn
4627                 (with-current-buffer summary-buffer
4628                   (push article gnus-newsgroup-history)
4629                   (setq gnus-last-article gnus-current-article
4630                         gnus-current-article 0
4631                         gnus-current-headers nil
4632                         gnus-article-current nil)
4633                   (if (eq result 'nneething)
4634                       (gnus-configure-windows 'summary)
4635                     (gnus-configure-windows 'article))
4636                   (gnus-set-global-variables))
4637                 (let ((gnus-article-mime-handle-alist-1
4638                        gnus-article-mime-handle-alist))
4639                   (gnus-set-mode-line 'article)))
4640             ;; The result from the `request' was an actual article -
4641             ;; or at least some text that is now displayed in the
4642             ;; article buffer.
4643             (when (and (numberp article)
4644                        (not (eq article gnus-current-article)))
4645               ;; Seems like a new article has been selected.
4646               ;; `gnus-current-article' must be an article number.
4647               (with-current-buffer summary-buffer
4648                 (push article gnus-newsgroup-history)
4649                 (setq gnus-last-article gnus-current-article
4650                       gnus-current-article article
4651                       gnus-current-headers
4652                       (gnus-summary-article-header gnus-current-article)
4653                       gnus-article-current
4654                       (cons gnus-newsgroup-name gnus-current-article))
4655                 (unless (vectorp gnus-current-headers)
4656                   (setq gnus-current-headers nil))
4657                 (gnus-summary-goto-subject gnus-current-article)
4658                 (when (gnus-summary-show-thread)
4659                   ;; If the summary buffer really was folded, the
4660                   ;; previous goto may not actually have gone to
4661                   ;; the right article, but the thread root instead.
4662                   ;; So we go again.
4663                   (gnus-summary-goto-subject gnus-current-article))
4664                 (gnus-run-hooks 'gnus-mark-article-hook)
4665                 (gnus-set-mode-line 'summary)
4666                 (when (gnus-visual-p 'article-highlight 'highlight)
4667                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4668                 ;; Set the global newsgroup variables here.
4669                 (gnus-set-global-variables)
4670                 (setq gnus-have-all-headers
4671                       (or all-headers gnus-show-all-headers))))
4672             (save-excursion
4673               (gnus-configure-windows 'article))
4674             (when (or (numberp article)
4675                       (stringp article))
4676               (gnus-article-prepare-display)
4677               ;; Do page break.
4678               (goto-char (point-min))
4679               (when gnus-break-pages
4680                 (gnus-narrow-to-page)))
4681             (let ((gnus-article-mime-handle-alist-1
4682                    gnus-article-mime-handle-alist))
4683               (gnus-set-mode-line 'article))
4684             (article-goto-body)
4685             (unless (bobp)
4686               (forward-line -1))
4687             (set-window-point (get-buffer-window (current-buffer)) (point))
4688             (gnus-configure-windows 'article)
4689             t))))))
4690
4691 ;;;###autoload
4692 (defun gnus-article-prepare-display ()
4693   "Make the current buffer look like a nice article."
4694   ;; Hooks for getting information from the article.
4695   ;; This hook must be called before being narrowed.
4696   (let ((gnus-article-buffer (current-buffer))
4697         buffer-read-only
4698         (inhibit-read-only t))
4699     (unless (eq major-mode 'gnus-article-mode)
4700       (gnus-article-mode))
4701     (setq buffer-read-only nil
4702           gnus-article-wash-types nil
4703           gnus-article-image-alist nil)
4704     (gnus-run-hooks 'gnus-tmp-internal-hook)
4705     (when gnus-display-mime-function
4706       (funcall gnus-display-mime-function))
4707     (gnus-run-hooks 'gnus-article-prepare-hook)))
4708
4709 ;;;
4710 ;;; Gnus Sticky Article Mode
4711 ;;;
4712
4713 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4714   "Mode for sticky articles."
4715   ;; Release bindings that won't work.
4716   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4717                              gnus-sticky-article-mode-map)
4718   (substitute-key-definition 'gnus-article-refer-article 'undefined
4719                              gnus-sticky-article-mode-map)
4720   (dolist (k '("e" "h" "s" "F" "R"))
4721     (define-key gnus-sticky-article-mode-map k nil))
4722   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4723   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4724   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4725   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4726
4727 (defun gnus-sticky-article (arg)
4728   "Make the current article sticky.
4729 If a prefix ARG is given, ask for a name for this sticky article buffer."
4730   (interactive "P")
4731   (gnus-summary-show-thread)
4732   (gnus-summary-select-article nil nil 'pseudo)
4733   (let (new-art-buf-name)
4734     (gnus-eval-in-buffer-window gnus-article-buffer
4735       (setq new-art-buf-name
4736             (concat
4737              "*Sticky Article: "
4738              (if arg
4739                  (read-from-minibuffer "Sticky article buffer name: ")
4740                (gnus-with-article-headers
4741                  (gnus-article-goto-header "subject")
4742                  (setq new-art-buf-name
4743                        (buffer-substring-no-properties
4744                         (line-beginning-position) (line-end-position)))
4745                  (goto-char (point-min))
4746                  (gnus-article-goto-header "from")
4747                  (setq new-art-buf-name
4748                        (concat
4749                         new-art-buf-name ", "
4750                         (buffer-substring-no-properties
4751                          (line-beginning-position) (line-end-position))))
4752                  (goto-char (point-min))
4753                  (gnus-article-goto-header "date")
4754                  (setq new-art-buf-name
4755                        (concat
4756                         new-art-buf-name ", "
4757                         (buffer-substring-no-properties
4758                          (line-beginning-position) (line-end-position))))))
4759              "*"))
4760       (if (and (gnus-buffer-live-p new-art-buf-name)
4761                (with-current-buffer new-art-buf-name
4762                  (eq major-mode 'gnus-sticky-article-mode)))
4763           (switch-to-buffer new-art-buf-name)
4764         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4765       (gnus-sticky-article-mode))
4766     (setq gnus-article-buffer new-art-buf-name))
4767   (gnus-summary-recenter)
4768   (gnus-summary-position-point))
4769
4770 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4771   "Kill the given sticky article BUFFER.
4772 If none is given, assume the current buffer and kill it if it has
4773 `gnus-sticky-article-mode'."
4774   (interactive)
4775   (unless buffer
4776     (setq buffer (current-buffer)))
4777   (with-current-buffer buffer
4778     (when (eq major-mode 'gnus-sticky-article-mode)
4779       (gnus-kill-buffer buffer))))
4780
4781 (defun gnus-kill-sticky-article-buffers (arg)
4782   "Kill all sticky article buffers.
4783 If a prefix ARG is given, ask for confirmation."
4784   (interactive "P")
4785   (dolist (buf (gnus-buffers))
4786     (with-current-buffer buf
4787       (when (eq major-mode 'gnus-sticky-article-mode)
4788         (if (not arg)
4789             (gnus-kill-buffer buf)
4790           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4791             (gnus-kill-buffer buf)))))))
4792
4793 ;;;
4794 ;;; Gnus MIME viewing functions
4795 ;;;
4796
4797 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4798   "Format of the MIME buttons.
4799
4800 Valid specifiers include:
4801 %t  The MIME type
4802 %T  MIME type, along with additional info
4803 %n  The `name' parameter
4804 %d  The description, if any
4805 %l  The length of the encoded part
4806 %p  The part identifier number
4807 %e  Dots if the part isn't displayed
4808
4809 General format specifiers can also be used.  See Info node
4810 `(gnus)Formatting Variables'.")
4811
4812 (defvar gnus-mime-button-line-format-alist
4813   '((?t gnus-tmp-type ?s)
4814     (?T gnus-tmp-type-long ?s)
4815     (?n gnus-tmp-name ?s)
4816     (?d gnus-tmp-description ?s)
4817     (?p gnus-tmp-id ?s)
4818     (?l gnus-tmp-length ?d)
4819     (?e gnus-tmp-dots ?s)))
4820
4821 (defvar gnus-mime-button-commands
4822   '((gnus-article-press-button "\r" "Toggle Display")
4823     (gnus-mime-view-part "v" "View Interactively...")
4824     (gnus-mime-view-part-as-type "t" "View As Type...")
4825     (gnus-mime-view-part-as-charset "C" "View As charset...")
4826     (gnus-mime-save-part "o" "Save...")
4827     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4828     (gnus-mime-replace-part "r" "Replace part")
4829     (gnus-mime-delete-part "d" "Delete part")
4830     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4831     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4832     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4833     (gnus-mime-view-part-externally "e" "View Externally")
4834     (gnus-mime-print-part "p" "Print")
4835     (gnus-mime-pipe-part "|" "Pipe To Command...")
4836     (gnus-mime-action-on-part "." "Take action on the part...")))
4837
4838 (defun gnus-article-mime-part-status ()
4839   (if gnus-article-mime-handle-alist-1
4840       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4841           " (1 part)"
4842         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4843     ""))
4844
4845 (defvar gnus-mime-button-map
4846   (let ((map (make-sparse-keymap)))
4847     (define-key map gnus-mouse-2 'gnus-article-push-button)
4848     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4849     (dolist (c gnus-mime-button-commands)
4850       (define-key map (cadr c) (car c)))
4851     map))
4852
4853 (easy-menu-define
4854   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4855   `("MIME Part"
4856     ,@(mapcar (lambda (c)
4857                 (vector (caddr c) (car c) :active t))
4858               gnus-mime-button-commands)))
4859
4860 (defvar gnus-url-button-commands
4861   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4862
4863 (defvar gnus-url-button-map
4864   (let ((map (make-sparse-keymap)))
4865     (dolist (c gnus-url-button-commands)
4866       (define-key map (cadr c) (car c)))
4867     map))
4868
4869 (easy-menu-define
4870   gnus-url-button-menu gnus-url-button-map "URL button menu."
4871   `("Url Button"
4872     ,@(mapcar (lambda (c)
4873                 (vector (caddr c) (car c) :active t))
4874               gnus-url-button-commands)))
4875
4876 (defmacro gnus-bind-safe-url-regexp (&rest body)
4877   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4878   `(let ((mm-w3m-safe-url-regexp
4879           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4880                                 (gnus-buffer-live-p
4881                                  gnus-article-current-summary))
4882                            (with-current-buffer gnus-article-current-summary
4883                              gnus-newsgroup-name)
4884                          gnus-newsgroup-name)))
4885             (if (cond ((not group)
4886                        ;; Maybe we're in a mml-preview buffer
4887                        ;; and no group is selected.
4888                        t)
4889                       ((stringp gnus-safe-html-newsgroups)
4890                        (string-match gnus-safe-html-newsgroups group))
4891                       ((consp gnus-safe-html-newsgroups)
4892                        (member group gnus-safe-html-newsgroups)))
4893                 nil
4894               mm-w3m-safe-url-regexp))))
4895      ,@body))
4896
4897 (defun gnus-mime-button-menu (event prefix)
4898  "Construct a context-sensitive menu of MIME commands."
4899  (interactive "e\nP")
4900  (save-window-excursion
4901    (let ((pos (event-start event)))
4902      (select-window (posn-window pos))
4903      (goto-char (posn-point pos))
4904      (gnus-article-check-buffer)
4905      (popup-menu gnus-mime-button-menu nil prefix))))
4906
4907 (defun gnus-mime-view-all-parts (&optional handles)
4908   "View all the MIME parts."
4909   (interactive)
4910   (with-current-buffer gnus-article-buffer
4911     (let ((handles (or handles gnus-article-mime-handles))
4912           (mail-parse-charset gnus-newsgroup-charset)
4913           (mail-parse-ignored-charsets
4914            (with-current-buffer gnus-summary-buffer
4915              gnus-newsgroup-ignored-charsets)))
4916       (when handles
4917         (mm-remove-parts handles)
4918         (goto-char (point-min))
4919         (or (search-forward "\n\n") (goto-char (point-max)))
4920         (let ((inhibit-read-only t))
4921           (delete-region (point) (point-max))
4922           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4923
4924 (defun gnus-article-jump-to-part (n)
4925   "Jump to MIME part N."
4926   (interactive "P")
4927   (pop-to-buffer gnus-article-buffer)
4928   ;; FIXME: why is it necessary?
4929   (sit-for 0)
4930   (let ((parts (length gnus-article-mime-handle-alist)))
4931     (or n (setq n (read-number (format "Jump to part (2..%s): " parts))))
4932     (unless (and (integerp n) (<= n parts) (>= n 1))
4933       (setq n
4934             (progn
4935               (gnus-message 7 "Invalid part `%s', using %s instead."
4936                             n parts)
4937               parts)))
4938     (gnus-message 9 "Jumping to part %s." n)
4939     (cond ((>= gnus-auto-select-part 1)
4940            (while (and (<= n parts)
4941                        (not (gnus-article-goto-part n)))
4942              (setq n (1+ n))))
4943           ((< gnus-auto-select-part 0)
4944            (while (and (>= n 1)
4945                        (not (gnus-article-goto-part n)))
4946              (setq n (1- n))))
4947           (t
4948            (gnus-article-goto-part n)))))
4949
4950 (defvar gnus-mime-buttonized-part-id nil
4951   "ID of a mime part that should be buttonized.
4952 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4953
4954 (eval-when-compile
4955   (defsubst gnus-article-edit-part (handles &optional current-id)
4956     "Edit an article in order to delete a mime part.
4957 This function is exclusively used by `gnus-mime-save-part-and-strip'
4958 and `gnus-mime-delete-part', and not provided at run-time normally."
4959     (gnus-article-edit-article
4960      `(lambda ()
4961         (buffer-disable-undo)
4962         (erase-buffer)
4963         (let ((mail-parse-charset (or gnus-article-charset
4964                                       ',gnus-newsgroup-charset))
4965               (mail-parse-ignored-charsets
4966                (or gnus-article-ignored-charsets
4967                    ',gnus-newsgroup-ignored-charsets))
4968               (mbl mml-buffer-list))
4969           (setq mml-buffer-list nil)
4970           (insert-buffer-substring gnus-original-article-buffer)
4971           (mime-to-mml ',handles)
4972           (setq gnus-article-mime-handles nil)
4973           (let ((mbl1 mml-buffer-list))
4974             (setq mml-buffer-list mbl)
4975             (set (make-local-variable 'mml-buffer-list) mbl1))
4976           (gnus-make-local-hook 'kill-buffer-hook)
4977           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4978      `(lambda (no-highlight)
4979         (let ((mail-parse-charset (or gnus-article-charset
4980                                       ',gnus-newsgroup-charset))
4981               (message-options message-options)
4982               (message-options-set-recipient)
4983               (mail-parse-ignored-charsets
4984                (or gnus-article-ignored-charsets
4985                    ',gnus-newsgroup-ignored-charsets)))
4986           (mml-to-mime)
4987           (mml-destroy-buffers)
4988           (remove-hook 'kill-buffer-hook
4989                        'mml-destroy-buffers t)
4990           (kill-local-variable 'mml-buffer-list))
4991         (gnus-summary-edit-article-done
4992          ,(or (mail-header-references gnus-current-headers) "")
4993          ,(gnus-group-read-only-p)
4994          ,gnus-summary-buffer no-highlight))
4995      t)
4996     ;; Force buttonizing this part.
4997     (let ((gnus-mime-buttonized-part-id current-id))
4998       (gnus-article-edit-done))
4999     (gnus-configure-windows 'article)
5000     (when (and current-id (integerp gnus-auto-select-part))
5001       (gnus-article-jump-to-part
5002        (min (max (+ current-id gnus-auto-select-part) 1)
5003             (with-current-buffer gnus-article-buffer
5004               (length gnus-article-mime-handle-alist)))))))
5005
5006 (defun gnus-mime-replace-part (file)
5007   "Replace MIME part under point with an external body."
5008   ;; Useful if file has already been saved to disk
5009   (interactive
5010    (list
5011     (read-file-name "Replace MIME part with file: "
5012                     (or mm-default-directory default-directory)
5013                     nil nil)))
5014   (gnus-mime-save-part-and-strip file))
5015
5016 (defun gnus-mime-save-part-and-strip (&optional file)
5017   "Save the MIME part under point then replace it with an external body.
5018 If FILE is given, use it for the external part."
5019   (interactive)
5020   (gnus-article-check-buffer)
5021   (when (gnus-group-read-only-p)
5022     (error "The current group does not support deleting of parts"))
5023   (when (mm-complicated-handles gnus-article-mime-handles)
5024     (error "\
5025 The current article has a complicated MIME structure, giving up..."))
5026   (let* ((data (get-text-property (point) 'gnus-data))
5027          (id (get-text-property (point) 'gnus-part))
5028          param
5029          (handles gnus-article-mime-handles))
5030     (unless file
5031       (setq file
5032             (and data (mm-save-part data "Delete MIME part and save to: "))))
5033     (when file
5034       (with-current-buffer (mm-handle-buffer data)
5035         (erase-buffer)
5036         (insert "Content-Type: " (mm-handle-media-type data))
5037         (mml-insert-parameter-string (cdr (mm-handle-type data))
5038                                      '(charset))
5039         ;; Add a filename for the sake of saving the part again.
5040         (mml-insert-parameter
5041          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5042         (insert "\n")
5043         (insert "Content-ID: " (message-make-message-id) "\n")
5044         (insert "Content-Transfer-Encoding: binary\n")
5045         (insert "\n"))
5046       (setcdr data
5047               (cdr (mm-make-handle nil
5048                                    `("message/external-body"
5049                                      (access-type . "LOCAL-FILE")
5050                                      (name . ,file)))))
5051       ;; (set-buffer gnus-summary-buffer)
5052       (gnus-article-edit-part handles id))))
5053
5054 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5055 ;; parts...>') but with stripping would be nice.
5056
5057 (defun gnus-mime-delete-part ()
5058   "Delete the MIME part under point.
5059 Replace it with some information about the removed part."
5060   (interactive)
5061   (gnus-article-check-buffer)
5062   (when (gnus-group-read-only-p)
5063     (error "The current group does not support deleting of parts"))
5064   (when (mm-complicated-handles gnus-article-mime-handles)
5065     (error "\
5066 The current article has a complicated MIME structure, giving up..."))
5067   (when (or gnus-expert-user
5068             (gnus-yes-or-no-p "\
5069 Deleting parts may malfunction or destroy the article; continue? "))
5070     (let* ((data (get-text-property (point) 'gnus-data))
5071            (id (get-text-property (point) 'gnus-part))
5072            (handles gnus-article-mime-handles)
5073            (none "(none)")
5074            (description
5075             (let ((desc (mm-handle-description data)))
5076               (when desc
5077                 (mail-decode-encoded-word-string desc))))
5078            (filename
5079             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
5080                 none))
5081            (type (mm-handle-media-type data)))
5082       (unless data
5083         (error "No MIME part under point"))
5084       (with-current-buffer (mm-handle-buffer data)
5085         (let ((bsize (format "%s" (buffer-size))))
5086           (erase-buffer)
5087           (insert
5088            (concat
5089             ",----\n"
5090             "| The following attachment has been deleted:\n"
5091             "|\n"
5092             "| Type:           " type "\n"
5093             "| Filename:       " filename "\n"
5094             "| Size (encoded): " bsize (format " byte%s\n"
5095                                                (if (= bsize 1)
5096                                                    ""
5097                                                  "s"))
5098             (when description
5099               (concat    "| Description:    " description "\n"))
5100             "`----\n"))
5101           (setcdr data
5102                   (cdr (mm-make-handle
5103                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5104                         (list "attachment")
5105                         (format "Deleted attachment (%s bytes)" bsize))))))
5106       ;; (set-buffer gnus-summary-buffer)
5107       (gnus-article-edit-part handles id))))
5108
5109 (defun gnus-mime-save-part ()
5110   "Save the MIME part under point."
5111   (interactive)
5112   (gnus-article-check-buffer)
5113   (let ((data (get-text-property (point) 'gnus-data)))
5114     (when data
5115       (mm-save-part data))))
5116
5117 (defun gnus-mime-pipe-part (&optional cmd)
5118   "Pipe the MIME part under point to a process.
5119 Use CMD as the process."
5120   (interactive)
5121   (gnus-article-check-buffer)
5122   (let ((data (get-text-property (point) 'gnus-data)))
5123     (when data
5124       (mm-pipe-part data cmd))))
5125
5126 (defun gnus-mime-view-part ()
5127   "Interactively choose a viewing method for the MIME part under point."
5128   (interactive)
5129   (gnus-article-check-buffer)
5130   (let ((data (get-text-property (point) 'gnus-data)))
5131     (when data
5132       (setq gnus-article-mime-handles
5133             (mm-merge-handles
5134              gnus-article-mime-handles (setq data (copy-sequence data))))
5135       (mm-interactively-view-part data))))
5136
5137 (defun gnus-mime-view-part-as-type-internal ()
5138   (gnus-article-check-buffer)
5139   (let* ((handle (get-text-property (point) 'gnus-data))
5140          (name (or
5141                 ;; Content-Type: foo/bar; name=...
5142                 (mail-content-type-get (mm-handle-type handle) 'name)
5143                 ;; Content-Disposition: attachment; filename=...
5144                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5145          (def-type (and name (mm-default-file-encoding name))))
5146     (or (and def-type (cons def-type 0))
5147         (and handle
5148              (equal (mm-handle-media-supertype handle) "text")
5149              '("text/plain" . 0))
5150         '("application/octet-stream" . 0))))
5151
5152 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5153   "Choose a MIME media type, and view the part as such.
5154 If non-nil, PRED is a predicate to use during completion to limit the
5155 available media-types."
5156   (interactive)
5157   (unless mime-type
5158     (setq mime-type
5159           (let ((default (gnus-mime-view-part-as-type-internal)))
5160             (completing-read
5161              (format "View as MIME type (default %s): "
5162                      (car default))
5163              (mapcar #'list (mailcap-mime-types))
5164              pred nil nil nil
5165              (car default)))))
5166   (gnus-article-check-buffer)
5167   (let ((handle (get-text-property (point) 'gnus-data)))
5168     (when handle
5169       (when (equal (mm-handle-media-type handle) "message/external-body")
5170         (unless (mm-handle-cache handle)
5171           (mm-extern-cache-contents handle))
5172         (setq handle (mm-handle-cache handle)))
5173       (setq handle
5174             (mm-make-handle (mm-handle-buffer handle)
5175                             (cons mime-type (cdr (mm-handle-type handle)))
5176                             (mm-handle-encoding handle)
5177                             (mm-handle-undisplayer handle)
5178                             (mm-handle-disposition handle)
5179                             (mm-handle-description handle)
5180                             nil
5181                             (mm-handle-id handle)))
5182       (setq gnus-article-mime-handles
5183             (mm-merge-handles gnus-article-mime-handles handle))
5184       (when (mm-handle-displayed-p handle)
5185         (mm-remove-part handle))
5186       (gnus-mm-display-part handle))))
5187
5188 (defun gnus-mime-copy-part (&optional handle arg)
5189   "Put the MIME part under point into a new buffer.
5190 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5191 are decompressed."
5192   (interactive (list nil current-prefix-arg))
5193   (gnus-article-check-buffer)
5194   (unless handle
5195     (setq handle (get-text-property (point) 'gnus-data)))
5196   (when handle
5197     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5198                                                'name)
5199                         (mail-content-type-get (mm-handle-disposition handle)
5200                                                'filename)))
5201           contents dont-decode charset coding-system)
5202       (mm-with-unibyte-buffer
5203         (mm-insert-part handle)
5204         (setq contents (or (condition-case nil
5205                                (mm-decompress-buffer filename nil 'sig)
5206                              (error
5207                               (setq dont-decode t)
5208                               nil))
5209                            (buffer-string))))
5210       (setq filename (cond (filename (file-name-nondirectory filename))
5211                            (dont-decode "*raw data*")
5212                            (t "*decoded*")))
5213       (cond
5214        (dont-decode)
5215        ((not arg)
5216         (unless (setq charset (mail-content-type-get
5217                                (mm-handle-type handle) 'charset))
5218           (unless (setq coding-system (mm-with-unibyte-buffer
5219                                         (insert contents)
5220                                         (mm-find-buffer-file-coding-system)))
5221             (setq charset gnus-newsgroup-charset))))
5222        ((numberp arg)
5223         (setq charset (or (cdr (assq arg
5224                                      gnus-summary-show-article-charset-alist))
5225                           (mm-read-coding-system "Charset: ")))))
5226       (switch-to-buffer (generate-new-buffer filename))
5227       (if (or coding-system
5228               (and charset
5229                    (setq coding-system (mm-charset-to-coding-system charset))
5230                    (not (eq charset 'ascii))))
5231           (progn
5232             (mm-enable-multibyte)
5233             (insert (mm-decode-coding-string contents coding-system))
5234             (setq buffer-file-coding-system
5235                   (if (boundp 'last-coding-system-used)
5236                       (symbol-value 'last-coding-system-used)
5237                     coding-system)))
5238         (mm-disable-multibyte)
5239         (insert contents)
5240         (setq buffer-file-coding-system mm-binary-coding-system))
5241       ;; We do it this way to make `normal-mode' set the appropriate mode.
5242       (unwind-protect
5243           (progn
5244             (setq buffer-file-name (expand-file-name filename))
5245             (normal-mode))
5246         (setq buffer-file-name nil))
5247       (goto-char (point-min)))))
5248
5249 (defun gnus-mime-print-part (&optional handle filename)
5250   "Print the MIME part under point."
5251   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5252   (gnus-article-check-buffer)
5253   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5254          (contents (and handle (mm-get-part handle)))
5255          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5256          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5257     (when contents
5258         (if printer
5259             (unwind-protect
5260                 (progn
5261                   (mm-save-part-to-file handle file)
5262                   (call-process shell-file-name nil
5263                                 (generate-new-buffer " *mm*")
5264                                 nil
5265                                 shell-command-switch
5266                                 (mm-mailcap-command
5267                                  printer file (mm-handle-type handle))))
5268               (delete-file file))
5269           (with-temp-buffer
5270             (insert contents)
5271             (gnus-print-buffer))
5272           (ps-despool filename)))))
5273
5274 (defun gnus-mime-inline-part (&optional handle arg)
5275   "Insert the MIME part under point into the current buffer.
5276 Compressed files like .gz and .bz2 are decompressed."
5277   (interactive (list nil current-prefix-arg))
5278   (gnus-article-check-buffer)
5279   (unless handle
5280     (setq handle (get-text-property (point) 'gnus-data)))
5281   (when handle
5282     (let ((b (point))
5283           (inhibit-read-only t)
5284           contents charset coding-system)
5285       (if (and (not arg) (mm-handle-undisplayer handle))
5286           (mm-remove-part handle)
5287         (mm-with-unibyte-buffer
5288           (mm-insert-part handle)
5289           (setq contents
5290                 (or (mm-decompress-buffer
5291                      (or (mail-content-type-get (mm-handle-type handle)
5292                                                 'name)
5293                          (mail-content-type-get (mm-handle-disposition handle)
5294                                                 'filename))
5295                      nil t)
5296                     (buffer-string))))
5297         (cond
5298          ((not arg)
5299           (unless (setq charset (mail-content-type-get
5300                                  (mm-handle-type handle) 'charset))
5301             (unless (setq coding-system
5302                           (mm-with-unibyte-buffer
5303                             (insert contents)
5304                             (mm-find-buffer-file-coding-system)))
5305               (setq charset gnus-newsgroup-charset))))
5306          ((numberp arg)
5307           (if (mm-handle-undisplayer handle)
5308               (mm-remove-part handle))
5309           (setq charset
5310                 (or (cdr (assq arg
5311                                gnus-summary-show-article-charset-alist))
5312                     (mm-read-coding-system "Charset: "))))
5313          (t
5314           (if (mm-handle-undisplayer handle)
5315               (mm-remove-part handle))))
5316         (forward-line 2)
5317         (mm-insert-inline
5318          handle
5319          (if (or coding-system
5320                  (and charset
5321                       (setq coding-system
5322                             (mm-charset-to-coding-system charset))
5323                       (not (eq coding-system 'ascii))))
5324              (mm-decode-coding-string contents coding-system)
5325            (mm-string-to-multibyte contents)))
5326         (goto-char b)))))
5327
5328 (defun gnus-mime-set-charset-parameters (handle charset)
5329   "Set CHARSET to parameters in HANDLE.
5330 CHARSET may either be a string or a symbol."
5331   (unless (stringp charset)
5332     (setq charset (symbol-name charset)))
5333   (if (stringp (car handle))
5334       (dolist (h (cdr handle))
5335         (gnus-mime-set-charset-parameters h charset))
5336     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5337                                             "message/external-body")
5338                                      (progn
5339                                        (unless (mm-handle-cache handle)
5340                                          (mm-extern-cache-contents handle))
5341                                        (mm-handle-cache handle))
5342                                    handle)))
5343            (param (assq 'charset (cdr type))))
5344       (if param
5345           (setcdr param charset)
5346         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5347
5348 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5349   "Insert the MIME part under point into the current buffer using the
5350 specified charset."
5351   (interactive (list nil current-prefix-arg))
5352   (gnus-article-check-buffer)
5353   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5354         (fun (get-text-property (point) 'gnus-callback))
5355         (gnus-newsgroup-ignored-charsets 'gnus-all)
5356         charset form preferred parts)
5357     (when handle
5358       (when (prog1
5359                 (and fun
5360                      (setq charset
5361                            (or (cdr (assq
5362                                      arg
5363                                      gnus-summary-show-article-charset-alist))
5364                                (mm-read-coding-system "Charset: "))))
5365               (if (mm-handle-undisplayer handle)
5366                   (mm-remove-part handle)))
5367         (gnus-mime-set-charset-parameters handle charset)
5368         (when (and (consp (setq form (cdr-safe fun)))
5369                    (setq form (ignore-errors
5370                                 (assq 'gnus-mime-display-alternative form)))
5371                    (setq preferred (caddr form))
5372                    (progn
5373                      (when (eq (car preferred) 'quote)
5374                        (setq preferred (cadr preferred)))
5375                      (not (equal preferred
5376                                  (get-text-property (point) 'gnus-data))))
5377                    (setq parts (get-text-property (point) 'gnus-part))
5378                    (setq parts (cdr (assq parts
5379                                           gnus-article-mime-handle-alist)))
5380                    (equal (mm-handle-media-type parts) "multipart/alternative")
5381                    (setq parts (reverse (cdr parts))))
5382           (setcar (cddr form)
5383                   (list 'quote (or (cadr (member preferred parts))
5384                                    (car parts)))))
5385         (funcall fun handle)))))
5386
5387 (defun gnus-mime-view-part-externally (&optional handle)
5388   "View the MIME part under point with an external viewer."
5389   (interactive)
5390   (gnus-article-check-buffer)
5391   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5392          (mm-user-display-methods nil)
5393          (mm-inlined-types nil)
5394          (mail-parse-charset gnus-newsgroup-charset)
5395          (mail-parse-ignored-charsets
5396           (with-current-buffer gnus-summary-buffer
5397             gnus-newsgroup-ignored-charsets))
5398          (type (mm-handle-media-type handle))
5399          (method (mailcap-mime-info type))
5400          (mm-enable-external t))
5401     (if (not (stringp method))
5402         (gnus-mime-view-part-as-type
5403          nil (lambda (types) (stringp (mailcap-mime-info (car types)))))
5404       (when handle
5405         (if (mm-handle-undisplayer handle)
5406             (mm-remove-part handle)
5407           (mm-display-part handle))))))
5408
5409 (defun gnus-mime-view-part-internally (&optional handle)
5410   "View the MIME part under point with an internal viewer.
5411 If no internal viewer is available, use an external viewer."
5412   (interactive)
5413   (gnus-article-check-buffer)
5414   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5415          (mm-inlined-types '(".*"))
5416          (mm-inline-large-images t)
5417          (mail-parse-charset gnus-newsgroup-charset)
5418          (mail-parse-ignored-charsets
5419           (with-current-buffer gnus-summary-buffer
5420             gnus-newsgroup-ignored-charsets))
5421          (inhibit-read-only t))
5422     (if (not (mm-inlinable-p handle))
5423         (gnus-mime-view-part-as-type
5424          nil (lambda (types) (mm-inlinable-p handle (car types))))
5425       (when handle
5426         (if (mm-handle-undisplayer handle)
5427             (mm-remove-part handle)
5428           (gnus-bind-safe-url-regexp (mm-display-part handle)))))))
5429
5430 (defun gnus-mime-action-on-part (&optional action)
5431   "Do something with the MIME attachment at \(point\)."
5432   (interactive
5433    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
5434   (gnus-article-check-buffer)
5435   (let ((action-pair (assoc action gnus-mime-action-alist)))
5436     (if action-pair
5437         (funcall (cdr action-pair)))))
5438
5439 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5440   "Call FUNCTION on MIME part N.
5441 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5442 If INTERACTIVE, call FUNCTION interactivly."
5443   (let (window frame)
5444     ;; Check whether the article is displayed.
5445     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5446                  (setq window (get-buffer-window gnus-article-buffer t))
5447                  (frame-visible-p (setq frame (window-frame window))))
5448       (error "No article is displayed"))
5449     (with-current-buffer gnus-article-buffer
5450       ;; Check whether the article displays the right contents.
5451       (unless (with-current-buffer gnus-summary-buffer
5452                 (eq gnus-current-article (gnus-summary-article-number)))
5453         (error "You should select the right article first"))
5454       (if n
5455           (setq n (prefix-numeric-value n))
5456         (let ((pt (point)))
5457           (setq n (or (get-text-property pt 'gnus-part)
5458                       (and (not (bobp))
5459                            (get-text-property (1- pt) 'gnus-part))
5460                       (get-text-property (prog2
5461                                              (forward-line 1)
5462                                              (point)
5463                                            (goto-char pt))
5464                                          'gnus-part)
5465                       (get-text-property
5466                        (or (and (setq pt (previous-single-property-change
5467                                           pt 'gnus-part))
5468                                 (1- pt))
5469                            (next-single-property-change (point) 'gnus-part)
5470                            (point))
5471                        'gnus-part)
5472                       1))))
5473       ;; Check whether the specified part exists.
5474       (when (> n (length gnus-article-mime-handle-alist))
5475         (error "No such part")))
5476     (unless
5477         (progn
5478           ;; To select the window is needed so that the cursor
5479           ;; might be visible on the MIME button.
5480           (select-window (prog1
5481                              window
5482                            (setq window (selected-window))
5483                            ;; Article may be displayed in the other frame.
5484                            (gnus-select-frame-set-input-focus
5485                             (prog1
5486                                 frame
5487                               (setq frame (selected-frame))))))
5488           (when (gnus-article-goto-part n)
5489             ;; We point the cursor and the arrow at the MIME button
5490             ;; when the `function' prompt the user for something.
5491             (let ((cursor-in-non-selected-windows t)
5492                   (overlay-arrow-string "=>")
5493                   (overlay-arrow-position (point-marker)))
5494               (unwind-protect
5495                   (cond
5496                    ((and no-handle interactive)
5497                     (call-interactively function))
5498                    (no-handle
5499                     (funcall function))
5500                    (interactive
5501                     (call-interactively
5502                      function
5503                      (cdr (assq n gnus-article-mime-handle-alist))))
5504                    (t
5505                     (funcall function
5506                              (cdr (assq n gnus-article-mime-handle-alist)))))
5507                 (set-marker overlay-arrow-position nil)
5508                 (unless gnus-auto-select-part
5509                   (gnus-select-frame-set-input-focus frame)
5510                   (select-window window))))
5511             t))
5512       (if gnus-inhibit-mime-unbuttonizing
5513           ;; This is the default though the program shouldn't reach here.
5514           (error "No such part")
5515         ;; The part which doesn't have the MIME button is selected.
5516         ;; So, we display all the buttons and redo it.
5517         (let ((gnus-inhibit-mime-unbuttonizing t))
5518           (gnus-summary-show-article)
5519           (gnus-article-part-wrapper n function no-handle))))))
5520
5521 (defun gnus-article-pipe-part (n)
5522   "Pipe MIME part N, which is the numerical prefix."
5523   (interactive "P")
5524   (gnus-article-part-wrapper n 'mm-pipe-part))
5525
5526 (defun gnus-article-save-part (n)
5527   "Save MIME part N, which is the numerical prefix."
5528   (interactive "P")
5529   (gnus-article-part-wrapper n 'mm-save-part))
5530
5531 (defun gnus-article-interactively-view-part (n)
5532   "View MIME part N interactively, which is the numerical prefix."
5533   (interactive "P")
5534   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5535
5536 (defun gnus-article-copy-part (n)
5537   "Copy MIME part N, which is the numerical prefix."
5538   (interactive "P")
5539   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5540
5541 (defun gnus-article-view-part-as-charset (n)
5542   "View MIME part N using a specified charset.
5543 N is the numerical prefix."
5544   (interactive "P")
5545   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5546
5547 (defun gnus-article-view-part-externally (n)
5548   "View MIME part N externally, which is the numerical prefix."
5549   (interactive "P")
5550   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5551
5552 (defun gnus-article-inline-part (n)
5553   "Inline MIME part N, which is the numerical prefix."
5554   (interactive "P")
5555   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5556
5557 (defun gnus-article-save-part-and-strip (n)
5558   "Save MIME part N and replace it with an external body.
5559 N is the numerical prefix."
5560   (interactive "P")
5561   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5562
5563 (defun gnus-article-replace-part (n)
5564   "Replace MIME part N with an external body.
5565 N is the numerical prefix."
5566   (interactive "P")
5567   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5568
5569 (defun gnus-article-delete-part (n)
5570   "Delete MIME part N and add some information about the removed part.
5571 N is the numerical prefix."
5572   (interactive "P")
5573   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5574
5575 (defun gnus-article-view-part-as-type (n)
5576   "Choose a MIME media type, and view part N as such.
5577 N is the numerical prefix."
5578   (interactive "P")
5579   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5580
5581 (defun gnus-article-mime-match-handle-first (condition)
5582   (if condition
5583       (let (n)
5584         (dolist (ihandle gnus-article-mime-handle-alist)
5585           (if (and (cond
5586                     ((functionp condition)
5587                      (funcall condition (cdr ihandle)))
5588                     ((eq condition 'undisplayed)
5589                      (not (or (mm-handle-undisplayer (cdr ihandle))
5590                               (equal (mm-handle-media-type (cdr ihandle))
5591                                      "multipart/alternative"))))
5592                     ((eq condition 'undisplayed-alternative)
5593                      (not (mm-handle-undisplayer (cdr ihandle))))
5594                     (t t))
5595                    (gnus-article-goto-part (car ihandle))
5596                    (or (not n) (< (car ihandle) n)))
5597               (setq n (car ihandle))))
5598         (or n 1))
5599     1))
5600
5601 (defun gnus-article-view-part (&optional n)
5602   "View MIME part N, which is the numerical prefix.
5603 If the part is already shown, hide the part.  If N is nil, view
5604 all parts."
5605   (interactive "P")
5606   (with-current-buffer gnus-article-buffer
5607     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5608                              gnus-article-mime-match-handle-function)))
5609     (when (> n (length gnus-article-mime-handle-alist))
5610       (error "No such part"))
5611     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5612       (when (gnus-article-goto-part n)
5613         (if (equal (car handle) "multipart/alternative")
5614             (gnus-article-press-button)
5615           (when (eq (gnus-mm-display-part handle) 'internal)
5616             (gnus-set-window-start)))))))
5617
5618 (defsubst gnus-article-mime-total-parts ()
5619   (if (bufferp (car gnus-article-mime-handles))
5620       1 ;; single part
5621     (1- (length gnus-article-mime-handles))))
5622
5623 (defun gnus-mm-display-part (handle)
5624   "Display HANDLE and fix MIME button."
5625   (let ((id (get-text-property (point) 'gnus-part))
5626         (point (point))
5627         (inhibit-read-only t))
5628     (forward-line 1)
5629     (prog1
5630         (let ((window (selected-window))
5631               (mail-parse-charset gnus-newsgroup-charset)
5632               (mail-parse-ignored-charsets
5633                (if (gnus-buffer-live-p gnus-summary-buffer)
5634                    (with-current-buffer gnus-summary-buffer
5635                      gnus-newsgroup-ignored-charsets)
5636                  nil)))
5637           (save-excursion
5638             (unwind-protect
5639                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5640                       (beg (point)))
5641                   (when win
5642                     (select-window win))
5643                   (goto-char point)
5644                   (forward-line)
5645                   (if (mm-handle-displayed-p handle)
5646                       ;; This will remove the part.
5647                       (mm-display-part handle)
5648                     (save-restriction
5649                       (narrow-to-region (point)
5650                                         (if (eobp) (point) (1+ (point))))
5651                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5652                       ;; We narrow to the part itself and
5653                       ;; then call the treatment functions.
5654                       (goto-char (point-min))
5655                       (forward-line 1)
5656                       (narrow-to-region (point) (point-max))
5657                       (gnus-treat-article
5658                        nil id
5659                        (gnus-article-mime-total-parts)
5660                        (mm-handle-media-type handle)))))
5661               (if (window-live-p window)
5662                   (select-window window)))))
5663       (goto-char point)
5664       (gnus-delete-line)
5665       (gnus-insert-mime-button
5666        handle id (list (mm-handle-displayed-p handle)))
5667       (goto-char point))))
5668
5669 (defun gnus-article-goto-part (n)
5670   "Go to MIME part N."
5671   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
5672
5673 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5674   (let ((gnus-tmp-name
5675          (or (mail-content-type-get (mm-handle-type handle) 'name)
5676              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5677              (mail-content-type-get (mm-handle-type handle) 'url)
5678              ""))
5679         (gnus-tmp-type (mm-handle-media-type handle))
5680         (gnus-tmp-description (or (mm-handle-description handle) ""))
5681         (gnus-tmp-dots
5682          (if (if displayed (car displayed)
5683                (mm-handle-displayed-p handle))
5684              "" "..."))
5685         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5686                            (buffer-size)))
5687         gnus-tmp-type-long b e)
5688     (when (string-match ".*/" gnus-tmp-name)
5689       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5690     (setq gnus-tmp-type-long (concat gnus-tmp-type
5691                                      (and (not (equal gnus-tmp-name ""))
5692                                           (concat "; " gnus-tmp-name))))
5693     (unless (equal gnus-tmp-description "")
5694       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5695     (unless (bolp)
5696       (insert "\n"))
5697     (setq b (point))
5698     (gnus-eval-format
5699      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5700      `(keymap ,gnus-mime-button-map
5701               gnus-callback gnus-mm-display-part
5702               gnus-part ,gnus-tmp-id
5703               article-type annotation
5704               gnus-data ,handle))
5705     (setq e (if (bolp)
5706                 ;; Exclude a newline.
5707                 (1- (point))
5708               (point)))
5709     (when gnus-article-button-face
5710       (gnus-overlay-put (gnus-make-overlay b e nil t)
5711                         'face gnus-article-button-face))
5712     (widget-convert-button
5713      'link b e
5714      :mime-handle handle
5715      :action 'gnus-widget-press-button
5716      :button-keymap gnus-mime-button-map
5717      :help-echo
5718      (lambda (widget)
5719        ;; Needed to properly clear the message due to a bug in
5720        ;; wid-edit (XEmacs only).
5721        (if (boundp 'help-echo-owns-message)
5722            (setq help-echo-owns-message t))
5723        (format
5724         "%S: %s the MIME part; %S: more options"
5725         (aref gnus-mouse-2 0)
5726         (if (mm-handle-displayed-p (widget-get widget :mime-handle))
5727             "hide" "show")
5728         (aref gnus-down-mouse-3 0))))))
5729
5730 (defun gnus-widget-press-button (elems el)
5731   (goto-char (widget-get elems :from))
5732   (gnus-article-press-button))
5733
5734 (defvar gnus-displaying-mime nil)
5735
5736 (defun gnus-display-mime (&optional ihandles)
5737   "Display the MIME parts."
5738   (save-excursion
5739     (save-selected-window
5740       (let ((window (get-buffer-window gnus-article-buffer))
5741             (point (point)))
5742         (when window
5743           (select-window window)
5744           ;; We have to do this since selecting the window
5745           ;; may change the point.  So we set the window point.
5746           (set-window-point window point)))
5747       (let ((handles ihandles)
5748             (inhibit-read-only t)
5749             handle)
5750         (cond (handles)
5751               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5752                (when gnus-article-emulate-mime
5753                  (mm-uu-dissect-text-parts handles)))
5754               (gnus-article-emulate-mime
5755                (setq handles (mm-uu-dissect))))
5756         (when (and (not ihandles)
5757                    (not gnus-displaying-mime))
5758           ;; Top-level call; we clean up.
5759           (when gnus-article-mime-handles
5760             (mm-destroy-parts gnus-article-mime-handles)
5761             (setq gnus-article-mime-handle-alist nil));; A trick.
5762           (setq gnus-article-mime-handles handles)
5763           ;; We allow users to glean info from the handles.
5764           (when gnus-article-mime-part-function
5765             (gnus-mime-part-function handles)))
5766         (if (and handles
5767                  (or (not (stringp (car handles)))
5768                      (cdr handles)))
5769             (progn
5770               (when (and (not ihandles)
5771                          (not gnus-displaying-mime))
5772                 ;; Clean up for mime parts.
5773                 (article-goto-body)
5774                 (delete-region (point) (point-max)))
5775               (let ((gnus-displaying-mime t))
5776                 (gnus-mime-display-part handles)))
5777           (save-restriction
5778             (article-goto-body)
5779             (narrow-to-region (point) (point-max))
5780             (gnus-treat-article nil 1 1)
5781             (widen)))
5782         (unless ihandles
5783           ;; Highlight the headers.
5784           (save-excursion
5785             (save-restriction
5786               (article-goto-body)
5787               (narrow-to-region (point-min) (point))
5788               (gnus-article-save-original-date
5789                (gnus-treat-article 'head)))))))
5790     ;; Cope with broken MIME messages.
5791     (goto-char (point-max))
5792     (unless (bolp)
5793       (insert "\n"))))
5794
5795 (defcustom gnus-mime-display-multipart-as-mixed nil
5796   "Display \"multipart\" parts as  \"multipart/mixed\".
5797
5798 If t, it overrides nil values of
5799 `gnus-mime-display-multipart-alternative-as-mixed' and
5800 `gnus-mime-display-multipart-related-as-mixed'."
5801   :group 'gnus-article-mime
5802   :type 'boolean)
5803
5804 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5805   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5806   :version "22.1"
5807   :group 'gnus-article-mime
5808   :type 'boolean)
5809
5810 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5811   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5812
5813 If displaying \"text/html\" is discouraged \(see
5814 `mm-discouraged-alternatives'\) images or other material inside a
5815 \"multipart/related\" part might be overlooked when this variable is nil."
5816   :version "22.1"
5817   :group 'gnus-article-mime
5818   :type 'boolean)
5819
5820 (defun gnus-mime-display-part (handle)
5821   (cond
5822    ;; Maybe a broken MIME message.
5823    ((null handle))
5824    ;; Single part.
5825    ((not (stringp (car handle)))
5826     (gnus-mime-display-single handle))
5827    ;; User-defined multipart
5828    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5829     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5830              handle))
5831    ;; multipart/alternative
5832    ((and (equal (car handle) "multipart/alternative")
5833          (not (or gnus-mime-display-multipart-as-mixed
5834                   gnus-mime-display-multipart-alternative-as-mixed)))
5835     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5836       (push (cons id handle) gnus-article-mime-handle-alist)
5837       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5838    ;; multipart/related
5839    ((and (equal (car handle) "multipart/related")
5840          (not (or gnus-mime-display-multipart-as-mixed
5841                   gnus-mime-display-multipart-related-as-mixed)))
5842     ;;;!!!We should find the start part, but we just default
5843     ;;;!!!to the first part.
5844     ;;(gnus-mime-display-part (cadr handle))
5845     ;;;!!! Most multipart/related is an HTML message plus images.
5846     ;;;!!! Unfortunately we are unable to let W3 display those
5847     ;;;!!! included images, so we just display it as a mixed multipart.
5848     ;;(gnus-mime-display-mixed (cdr handle))
5849     ;;;!!! No, w3 can display everything just fine.
5850     (gnus-mime-display-part (cadr handle)))
5851    ((equal (car handle) "multipart/signed")
5852     (gnus-add-wash-type 'signed)
5853     (gnus-mime-display-security handle))
5854    ((equal (car handle) "multipart/encrypted")
5855     (gnus-add-wash-type 'encrypted)
5856     (gnus-mime-display-security handle))
5857    ;; Other multiparts are handled like multipart/mixed.
5858    (t
5859     (gnus-mime-display-mixed (cdr handle)))))
5860
5861 (defun gnus-mime-part-function (handles)
5862   (if (stringp (car handles))
5863       (mapcar 'gnus-mime-part-function (cdr handles))
5864     (funcall gnus-article-mime-part-function handles)))
5865
5866 (defun gnus-mime-display-mixed (handles)
5867   (mapcar 'gnus-mime-display-part handles))
5868
5869 (defun gnus-mime-display-single (handle)
5870   (let ((type (mm-handle-media-type handle))
5871         (ignored gnus-ignored-mime-types)
5872         (not-attachment t)
5873         (move nil)
5874         display text)
5875     (catch 'ignored
5876       (progn
5877         (while ignored
5878           (when (string-match (pop ignored) type)
5879             (throw 'ignored nil)))
5880         (if (and (setq not-attachment
5881                        (and (not (mm-inline-override-p handle))
5882                             (or (not (mm-handle-disposition handle))
5883                                 (equal (car (mm-handle-disposition handle))
5884                                        "inline")
5885                                 (mm-attachment-override-p handle))))
5886                  (mm-automatic-display-p handle)
5887                  (or (and
5888                       (mm-inlinable-p handle)
5889                       (mm-inlined-p handle))
5890                      (mm-automatic-external-display-p type)))
5891             (setq display t)
5892           (when (equal (mm-handle-media-supertype handle) "text")
5893             (setq text t)))
5894         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5895               beg)
5896           (push (cons id handle) gnus-article-mime-handle-alist)
5897           (when (and display
5898                      (equal (mm-handle-media-supertype handle) "message"))
5899             (insert-char
5900              ?\n
5901              (cond ((not (bolp)) 2)
5902                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5903                    (t 1))))
5904           (when (or (not display)
5905                     (not (gnus-unbuttonized-mime-type-p type))
5906                     (eq id gnus-mime-buttonized-part-id))
5907             (gnus-insert-mime-button
5908              handle id (list (or display (and not-attachment text))))
5909             (gnus-article-insert-newline)
5910             ;; Remember modify the number of forward lines.
5911             (setq move t))
5912           (setq beg (point))
5913           (cond
5914            (display
5915             (when move
5916               (forward-line -1)
5917               (setq beg (point)))
5918             (let ((mail-parse-charset gnus-newsgroup-charset)
5919                   (mail-parse-ignored-charsets
5920                    (save-excursion (condition-case ()
5921                                        (set-buffer gnus-summary-buffer)
5922                                      (error))
5923                                    gnus-newsgroup-ignored-charsets)))
5924               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5925             (goto-char (point-max)))
5926            ((and text not-attachment)
5927             (when move
5928               (forward-line -1)
5929               (setq beg (point)))
5930             (gnus-article-insert-newline)
5931             (mm-insert-inline
5932              handle
5933              (let ((charset (or (mail-content-type-get (mm-handle-type handle)
5934                                                        'charset)
5935                                 (and (equal type "text/calendar") 'utf-8))))
5936                (cond ((not charset)
5937                       (mm-string-as-multibyte (mm-get-part handle)))
5938                      ((eq charset 'gnus-decoded)
5939                       (with-current-buffer (mm-handle-buffer handle)
5940                         (buffer-string)))
5941                      (t
5942                       (mm-decode-string (mm-get-part handle) charset)))))
5943             (goto-char (point-max))))
5944           ;; Do highlighting.
5945           (save-excursion
5946             (save-restriction
5947               (narrow-to-region beg (point))
5948               (if (eq handle gnus-article-mime-handles)
5949                   ;; The format=flowed case.
5950                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5951                 ;; Don't count signature parts that are never displayed.
5952                 ;; The part number should be re-calculated supposing this
5953                 ;; might be a message/rfc822 part.
5954                 (let (handles)
5955                   (dolist (part gnus-article-mime-handles)
5956                     (unless (or (stringp part)
5957                                 (equal (car (mm-handle-type part))
5958                                        "application/pgp-signature"))
5959                       (push part handles)))
5960                   (gnus-treat-article
5961                    nil (length (memq handle handles)) (length handles)
5962                    (mm-handle-media-type handle)))))))))))
5963
5964 (defun gnus-unbuttonized-mime-type-p (type)
5965   "Say whether TYPE is to be unbuttonized."
5966   (unless gnus-inhibit-mime-unbuttonizing
5967     (when (catch 'found
5968             (let ((types gnus-unbuttonized-mime-types))
5969               (while types
5970                 (when (string-match (pop types) type)
5971                   (throw 'found t)))))
5972       (not (catch 'found
5973              (let ((types gnus-buttonized-mime-types))
5974                (while types
5975                  (when (string-match (pop types) type)
5976                    (throw 'found t)))))))))
5977
5978 (defun gnus-article-insert-newline ()
5979   "Insert a newline, but mark it as undeletable."
5980   (gnus-put-text-property
5981    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5982
5983 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5984   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5985          (ihandles handles)
5986          (point (point))
5987          handle (inhibit-read-only t) from props begend not-pref)
5988     (save-window-excursion
5989       (save-restriction
5990         (when ibegend
5991           (narrow-to-region (car ibegend)
5992                             (or (cdr ibegend)
5993                                 (progn
5994                                   (goto-char (car ibegend))
5995                                   (forward-line 2)
5996                                   (point))))
5997           (delete-region (point-min) (point-max))
5998           (mm-remove-parts handles))
5999         (setq begend (list (point-marker)))
6000         ;; Do the toggle.
6001         (unless (setq not-pref (cadr (member preferred ihandles)))
6002           (setq not-pref (car ihandles)))
6003         (when (or ibegend
6004                   (not preferred)
6005                   (not (gnus-unbuttonized-mime-type-p
6006                         "multipart/alternative")))
6007           (gnus-add-text-properties
6008            (setq from (point))
6009            (progn
6010              (insert (format "%d.  " id))
6011              (point))
6012            `(gnus-callback
6013              (lambda (handles)
6014                (unless ,(not ibegend)
6015                  (setq gnus-article-mime-handle-alist
6016                        ',gnus-article-mime-handle-alist))
6017                (gnus-mime-display-alternative
6018                 ',ihandles ',not-pref ',begend ,id))
6019              keymap ,gnus-mime-button-map
6020              ,gnus-mouse-face-prop ,gnus-article-mouse-face
6021              face ,gnus-article-button-face
6022              gnus-part ,id
6023              article-type multipart))
6024           (widget-convert-button 'link from (point)
6025                                  :action 'gnus-widget-press-button
6026                                  :button-keymap gnus-widget-button-keymap)
6027           ;; Do the handles
6028           (while (setq handle (pop handles))
6029             (gnus-add-text-properties
6030              (setq from (point))
6031              (progn
6032                (insert (format "(%c) %-18s"
6033                                (if (equal handle preferred) ?* ? )
6034                                (mm-handle-media-type handle)))
6035                (point))
6036              `(gnus-callback
6037                (lambda (handles)
6038                  (unless ,(not ibegend)
6039                    (setq gnus-article-mime-handle-alist
6040                          ',gnus-article-mime-handle-alist))
6041                  (gnus-mime-display-alternative
6042                   ',ihandles ',handle ',begend ,id))
6043                keymap ,gnus-mime-button-map
6044                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6045                face ,gnus-article-button-face
6046                gnus-part ,id
6047                gnus-data ,handle))
6048             (widget-convert-button 'link from (point)
6049                                    :action 'gnus-widget-press-button
6050                                    :button-keymap gnus-widget-button-keymap)
6051             (insert "  "))
6052           (insert "\n\n"))
6053         (when preferred
6054           (if (stringp (car preferred))
6055               (gnus-display-mime preferred)
6056             (let ((mail-parse-charset gnus-newsgroup-charset)
6057                   (mail-parse-ignored-charsets
6058                    (with-current-buffer gnus-summary-buffer
6059                      gnus-newsgroup-ignored-charsets)))
6060               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6061               ;; Do highlighting.
6062               (save-excursion
6063                 (save-restriction
6064                   (narrow-to-region (car begend) (point-max))
6065                   (gnus-treat-article
6066                    nil (length gnus-article-mime-handle-alist)
6067                    (gnus-article-mime-total-parts)
6068                    (mm-handle-media-type handle))))))
6069           (goto-char (point-max))
6070           (setcdr begend (point-marker)))))
6071     (when ibegend
6072       (goto-char point))))
6073
6074 (defconst gnus-article-wash-status-strings
6075   (let ((alist '((cite "c" "Possible hidden citation text"
6076                        " " "All citation text visible")
6077                  (headers "h" "Hidden headers"
6078                           " " "All headers visible.")
6079                  (pgp "p" "Encrypted or signed message status hidden"
6080                       " " "No hidden encryption nor digital signature status")
6081                  (signature "s" "Signature has been hidden"
6082                             " " "Signature is visible")
6083                  (overstrike "o" "Overstrike (^H) characters applied"
6084                              " " "No overstrike characters applied")
6085                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6086                            " " "No /*_emphasis_*/ characters applied")))
6087         result)
6088     (dolist (entry alist result)
6089       (let ((key (nth 0 entry))
6090             (on (copy-sequence (nth 1 entry)))
6091             (on-help (nth 2 entry))
6092             (off (copy-sequence (nth 3 entry)))
6093             (off-help (nth 4 entry)))
6094         (put-text-property 0 1 'help-echo on-help on)
6095         (put-text-property 0 1 'help-echo off-help off)
6096         (push (list key on off) result))))
6097   "Alist of strings describing wash status in the mode line.
6098 Each entry has the form (KEY ON OF), where the KEY is a symbol
6099 representing the particular washing function, ON is the string to use
6100 in the article mode line when the washing function is active, and OFF
6101 is the string to use when it is inactive.")
6102
6103 (defun gnus-article-wash-status-entry (key value)
6104   (let ((entry (assoc key gnus-article-wash-status-strings)))
6105     (if value (nth 1 entry) (nth 2 entry))))
6106
6107 (defun gnus-article-wash-status ()
6108   "Return a string which display status of article washing."
6109   (with-current-buffer gnus-article-buffer
6110     (let ((cite (memq 'cite gnus-article-wash-types))
6111           (headers (memq 'headers gnus-article-wash-types))
6112           (boring (memq 'boring-headers gnus-article-wash-types))
6113           (pgp (memq 'pgp gnus-article-wash-types))
6114           (pem (memq 'pem gnus-article-wash-types))
6115           (signed (memq 'signed gnus-article-wash-types))
6116           (encrypted (memq 'encrypted gnus-article-wash-types))
6117           (signature (memq 'signature gnus-article-wash-types))
6118           (overstrike (memq 'overstrike gnus-article-wash-types))
6119           (emphasis (memq 'emphasis gnus-article-wash-types)))
6120       (concat
6121        (gnus-article-wash-status-entry 'cite cite)
6122        (gnus-article-wash-status-entry 'headers (or headers boring))
6123        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6124        (gnus-article-wash-status-entry 'signature signature)
6125        (gnus-article-wash-status-entry 'overstrike overstrike)
6126        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6127
6128 (defun gnus-add-wash-type (type)
6129   "Add a washing of TYPE to the current status."
6130   (add-to-list 'gnus-article-wash-types type))
6131
6132 (defun gnus-delete-wash-type (type)
6133   "Add a washing of TYPE to the current status."
6134   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6135
6136 (defun gnus-add-image (category image)
6137   "Add IMAGE of CATEGORY to the list of displayed images."
6138   (let ((entry (assq category gnus-article-image-alist)))
6139     (unless entry
6140       (setq entry (list category))
6141       (push entry gnus-article-image-alist))
6142     (nconc entry (list image))))
6143
6144 (defun gnus-delete-images (category)
6145   "Delete all images in CATEGORY."
6146   (let ((entry (assq category gnus-article-image-alist)))
6147     (dolist (image (cdr entry))
6148       (gnus-remove-image image category))
6149     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6150     (gnus-delete-wash-type category)))
6151
6152 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6153
6154 (defun gnus-article-maybe-hide-headers ()
6155   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6156 Provided for backwards compatibility."
6157   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6158                  (not (with-current-buffer gnus-summary-buffer
6159                         gnus-have-all-headers)))
6160              (not gnus-inhibit-hiding))
6161     (gnus-article-hide-headers)))
6162
6163 ;;; Article savers.
6164
6165 (defun gnus-output-to-file (file-name)
6166   "Append the current article to a file named FILE-NAME.
6167 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6168 text and used as the value of the coding cookie which is added to the
6169 top of a file.  Otherwise, this function saves a raw article without
6170 the coding cookie."
6171   (let* ((artbuf (current-buffer))
6172          (file-name-coding-system nnmail-pathname-coding-system)
6173          (coding gnus-article-save-coding-system)
6174          (coding-system-for-read (if coding
6175                                      nil ;; Rely on the coding cookie.
6176                                    mm-text-coding-system))
6177          (coding-system-for-write (or coding
6178                                       mm-text-coding-system-for-write
6179                                       mm-text-coding-system))
6180          (exists (file-exists-p file-name)))
6181     (with-temp-buffer
6182       (when exists
6183         (insert-file-contents file-name)
6184         (goto-char (point-min))
6185         ;; Remove the existing coding cookie.
6186         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6187           (delete-region (match-beginning 0) (match-end 0))))
6188       (goto-char (point-max))
6189       (insert-buffer-substring artbuf)
6190       ;; Append newline at end of the buffer as separator, and then
6191       ;; save it to file.
6192       (goto-char (point-max))
6193       (insert "\n")
6194       (when coding
6195         ;; If the coding system is not suitable to encode the text,
6196         ;; ask a user for a proper one.
6197         (when (fboundp 'select-safe-coding-system)
6198           (setq coding (coding-system-base
6199                         (save-window-excursion
6200                           (select-safe-coding-system (point-min) (point-max)
6201                                                      coding))))
6202           (setq coding-system-for-write
6203                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6204                     coding)))
6205         (goto-char (point-min))
6206         ;; Add the coding cookie.
6207         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6208                         coding-system-for-write)))
6209       (if exists
6210           (progn
6211             (write-region (point-min) (point-max) file-name nil 'no-message)
6212             (message "Appended to %s" file-name))
6213         (write-region (point-min) (point-max) file-name))))
6214   t)
6215
6216 (defun gnus-narrow-to-page (&optional arg)
6217   "Narrow the article buffer to a page.
6218 If given a numerical ARG, move forward ARG pages."
6219   (interactive "P")
6220   (setq arg (if arg (prefix-numeric-value arg) 0))
6221   (with-current-buffer gnus-article-buffer
6222     (widen)
6223     ;; Remove any old next/prev buttons.
6224     (when (gnus-visual-p 'page-marker)
6225       (let ((inhibit-read-only t))
6226         (gnus-remove-text-with-property 'gnus-prev)
6227         (gnus-remove-text-with-property 'gnus-next)))
6228     (let (st nd pt)
6229       (when (save-excursion
6230               (cond ((< arg 0)
6231                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6232                          (prog1
6233                              (setq nd (match-beginning 0)
6234                                    pt nd)
6235                            (when (re-search-backward page-delimiter nil t)
6236                              (setq st (match-end 0))))
6237                        (when (re-search-forward page-delimiter nil t)
6238                          (setq nd (match-beginning 0)
6239                                pt (point-min)))))
6240                     ((> arg 0)
6241                      (if (re-search-forward page-delimiter nil 'move arg)
6242                          (prog1
6243                              (setq st (match-end 0)
6244                                    pt st)
6245                            (when (re-search-forward page-delimiter nil t)
6246                              (setq nd (match-beginning 0))))
6247                        (when (re-search-backward page-delimiter nil t)
6248                          (setq st (match-end 0)
6249                                pt (point-max)))))
6250                     (t
6251                      (when (re-search-backward page-delimiter nil t)
6252                        (goto-char (setq st (match-end 0))))
6253                      (when (re-search-forward page-delimiter nil t)
6254                        (setq nd (match-beginning 0)))
6255                      (or st nd))))
6256         (setq gnus-page-broken t)
6257         (when pt (goto-char pt))
6258         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6259         (when (gnus-visual-p 'page-marker)
6260           (save-excursion
6261             (when nd
6262               (goto-char nd)
6263               (gnus-insert-next-page-button))
6264             (when st
6265               (goto-char st)
6266               (gnus-insert-prev-page-button))))))))
6267
6268 ;; Article mode commands
6269
6270 (defun gnus-article-goto-next-page ()
6271   "Show the next page of the article."
6272   (interactive)
6273   (when (gnus-article-next-page)
6274     (goto-char (point-min))
6275     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6276
6277
6278 (defun gnus-article-goto-prev-page ()
6279   "Show the previous page of the article."
6280   (interactive)
6281   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6282       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6283     (gnus-article-prev-page nil)))
6284
6285 ;; This is cleaner but currently breaks `gnus-pick-mode':
6286 ;;
6287 ;; (defun gnus-article-goto-next-page ()
6288 ;;   "Show the next page of the article."
6289 ;;   (interactive)
6290 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6291 ;;     (gnus-summary-next-page)))
6292 ;;
6293 ;; (defun gnus-article-goto-prev-page ()
6294 ;;   "Show the next page of the article."
6295 ;;   (interactive)
6296 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6297 ;;     (gnus-summary-prev-page)))
6298
6299 (defun gnus-article-next-page (&optional lines)
6300   "Show the next page of the current article.
6301 If end of article, return non-nil.  Otherwise return nil.
6302 Argument LINES specifies lines to be scrolled up."
6303   (interactive "p")
6304   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6305   (if (and (not (and gnus-article-over-scroll
6306                      (> (count-lines (window-start) (point-max))
6307                         (if (featurep 'xemacs)
6308                             (or lines (1- (window-height)))
6309                           (+ (or lines (1- (window-height))) scroll-margin)))))
6310            (save-excursion
6311              (end-of-line)
6312              (and (pos-visible-in-window-p)     ;Not continuation line.
6313                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6314       ;; Nothing in this page.
6315       (if (or (not gnus-page-broken)
6316               (save-excursion
6317                 (save-restriction
6318                   (widen)
6319                   (forward-line)
6320                   (eobp)))) ;Real end-of-buffer?
6321           (progn
6322             (when gnus-article-over-scroll
6323               (gnus-article-next-page-1 lines))
6324             t)                  ;Nothing more.
6325         (gnus-narrow-to-page 1)         ;Go to next page.
6326         nil)
6327     ;; More in this page.
6328     (gnus-article-next-page-1 lines)
6329     nil))
6330
6331 (defun gnus-article-beginning-of-window ()
6332   "Move point to the beginning of the window.
6333 In Emacs, the point is placed at the line number which `scroll-margin'
6334 specifies."
6335   (if (featurep 'xemacs)
6336       (move-to-window-line 0)
6337     ;; There is an obscure bug in Emacs that makes it impossible to
6338     ;; scroll past big pictures in the article buffer.  Try to fix
6339     ;; this by adding a sanity check by counting the lines visible.
6340     (when (> (count-lines (window-start) (window-end)) 30)
6341       (move-to-window-line
6342        (min (max 0 scroll-margin)
6343             (max 1 (- (window-height)
6344                       (if mode-line-format 1 0)
6345                       (if header-line-format 1 0)
6346                       2)))))))
6347
6348 (defun gnus-article-next-page-1 (lines)
6349   (condition-case ()
6350       (let ((scroll-in-place nil))
6351         (scroll-up lines))
6352     (end-of-buffer
6353      ;; Long lines may cause an end-of-buffer error.
6354      (goto-char (point-max))))
6355   (gnus-article-beginning-of-window))
6356
6357 (defun gnus-article-prev-page (&optional lines)
6358   "Show previous page of current article.
6359 Argument LINES specifies lines to be scrolled down."
6360   (interactive "p")
6361   (move-to-window-line 0)
6362   (if (and gnus-page-broken
6363            (bobp)
6364            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6365       (progn
6366         (gnus-narrow-to-page -1)        ;Go to previous page.
6367         (goto-char (point-max))
6368         (recenter (if gnus-article-over-scroll
6369                       (if lines
6370                           (max (if (featurep 'xemacs)
6371                                    lines
6372                                  (+ lines scroll-margin))
6373                                3)
6374                         (- (window-height) 2))
6375                     -1)))
6376     (prog1
6377         (condition-case ()
6378             (let ((scroll-in-place nil))
6379               (scroll-down lines))
6380           (beginning-of-buffer
6381            (goto-char (point-min))))
6382       (gnus-article-beginning-of-window))))
6383
6384 (defun gnus-article-only-boring-p ()
6385   "Decide whether there is only boring text remaining in the article.
6386 Something \"interesting\" is a word of at least two letters that does
6387 not have a face in `gnus-article-boring-faces'."
6388   (when (and gnus-article-skip-boring
6389              (boundp 'gnus-article-boring-faces)
6390              (symbol-value 'gnus-article-boring-faces))
6391     (save-excursion
6392       (let ((inhibit-point-motion-hooks t))
6393         (catch 'only-boring
6394           (while (re-search-forward "\\b\\w\\w" nil t)
6395             (forward-char -1)
6396             (when (not (gnus-intersection
6397                         (gnus-faces-at (point))
6398                         (symbol-value 'gnus-article-boring-faces)))
6399               (throw 'only-boring nil)))
6400           (throw 'only-boring t))))))
6401
6402 (defun gnus-article-refer-article ()
6403   "Read article specified by message-id around point."
6404   (interactive)
6405   (save-excursion
6406     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6407     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6408     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6409         (let ((msg-id (concat "<" (match-string 0) ">")))
6410           (set-buffer gnus-summary-buffer)
6411           (gnus-summary-refer-article msg-id))
6412       (error "No references around point"))))
6413
6414 (defun gnus-article-show-summary ()
6415   "Reconfigure windows to show summary buffer."
6416   (interactive)
6417   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6418       (error "There is no summary buffer for this article buffer")
6419     (gnus-article-set-globals)
6420     (gnus-configure-windows 'article)
6421     (gnus-summary-goto-subject gnus-current-article)
6422     (gnus-summary-position-point)))
6423
6424 (defun gnus-article-describe-briefly ()
6425   "Describe article mode commands briefly."
6426   (interactive)
6427   (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")))
6428
6429 (defun gnus-article-check-buffer ()
6430   "Beep if not in an article buffer."
6431   (unless (equal major-mode 'gnus-article-mode)
6432     (error "Command invoked outside of a Gnus article buffer")))
6433
6434 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6435   "Read a summary buffer key sequence and execute it from the article buffer."
6436   (interactive "P")
6437   (gnus-article-check-buffer)
6438   (let ((nosaves
6439          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6440            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6441            "=" "^" "\M-^" "|"))
6442         (nosave-but-article
6443          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6444            "An" "Ap" [?A (meta return)] [?A delete]))
6445         (nosave-in-article
6446          '("AS" "\C-d"))
6447         (up-to-top
6448          '("n" "Gn" "p" "Gp"))
6449         keys new-sum-point)
6450     (with-current-buffer gnus-article-current-summary
6451       (let (gnus-pick-mode)
6452         (setq unread-command-events (nconc unread-command-events
6453                                            (list (or key last-command-event)))
6454               keys (if (featurep 'xemacs)
6455                        (events-to-keys (read-key-sequence nil t))
6456                      (read-key-sequence nil t)))))
6457
6458     (message "")
6459
6460     (cond
6461      ((eq (aref keys (1- (length keys))) ?\C-h)
6462       (gnus-article-describe-bindings (substring keys 0 -1)))
6463      ((or (member keys nosaves)
6464           (member keys nosave-but-article)
6465           (member keys nosave-in-article))
6466       (let (func)
6467         (save-window-excursion
6468           (pop-to-buffer gnus-article-current-summary)
6469           ;; We disable the pick minor mode commands.
6470           (let (gnus-pick-mode)
6471             (setq func (key-binding keys t))))
6472         (if (or (not func)
6473                 (numberp func))
6474             (ding)
6475           (unless (member keys nosave-in-article)
6476             (set-buffer gnus-article-current-summary))
6477           (call-interactively func)
6478           (setq new-sum-point (point)))
6479         (when (member keys nosave-but-article)
6480           (pop-to-buffer gnus-article-buffer))))
6481      (t
6482       ;; These commands should restore window configuration.
6483       (let ((obuf (current-buffer))
6484             (owin (current-window-configuration))
6485             win func in-buffer selected new-sum-start new-sum-hscroll err)
6486         (cond (not-restore-window
6487                (pop-to-buffer gnus-article-current-summary)
6488                (setq win (selected-window)))
6489               ((setq win (get-buffer-window gnus-article-current-summary))
6490                (select-window win))
6491               (t
6492                (let ((summary-buffer gnus-article-current-summary))
6493                  (gnus-configure-windows 'article)
6494                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6495                    (let ((gnus-buffer-configuration
6496                           '((article ((vertical 1.0
6497                                                 (summary 0.25 point)
6498                                                 (article 1.0)))))))
6499                      (gnus-configure-windows 'article))
6500                    (setq win (get-buffer-window summary-buffer 'visible)))
6501                  (gnus-select-frame-set-input-focus (window-frame win))
6502                  (select-window win))))
6503         (setq in-buffer (current-buffer))
6504         ;; We disable the pick minor mode commands.
6505         (if (and (setq func (let (gnus-pick-mode)
6506                               (key-binding keys t)))
6507                  (functionp func)
6508                  (condition-case code
6509                      (progn
6510                        (call-interactively func)
6511                        t)
6512                    (error
6513                     (setq err code)
6514                     nil)))
6515             (progn
6516               (when (eq win (selected-window))
6517                 (setq new-sum-point (point)
6518                       new-sum-start (window-start win)
6519                       new-sum-hscroll (window-hscroll win)))
6520               (when (or (eq in-buffer (current-buffer))
6521                         (when (eq obuf (current-buffer))
6522                           (set-buffer in-buffer)
6523                           t))
6524                 (setq selected (gnus-summary-select-article))
6525                 (set-buffer obuf)
6526                 (unless not-restore-window
6527                   (set-window-configuration owin))
6528                 (when (and (eq selected 'old)
6529                            new-sum-point)
6530                   (set-window-start (get-buffer-window (current-buffer))
6531                                     1)
6532                   (set-window-point (get-buffer-window (current-buffer))
6533                                     (if (article-goto-body)
6534                                         (1- (point))
6535                                       (point))))
6536                 (when (and (not not-restore-window)
6537                            new-sum-point
6538                            (window-live-p win)
6539                            (with-current-buffer (window-buffer win)
6540                              (eq major-mode 'gnus-summary-mode)))
6541                   (set-window-point win new-sum-point)
6542                   (set-window-start win new-sum-start)
6543                   (set-window-hscroll win new-sum-hscroll))))
6544           (set-window-configuration owin)
6545           (if err
6546               (signal (car err) (cdr err))
6547             (ding))))))))
6548
6549 (defun gnus-article-read-summary-send-keys ()
6550   (interactive)
6551   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6552     (gnus-article-read-summary-keys)))
6553
6554 (defun gnus-article-describe-key (key)
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   (gnus-article-check-buffer)
6560   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6561                                   gnus-article-read-summary-send-keys))
6562       (with-current-buffer gnus-article-current-summary
6563         (setq unread-command-events
6564               (if (featurep 'xemacs)
6565                   (append key nil)
6566                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6567                                         (list 'meta (- x 128))
6568                                       x))
6569                         key)))
6570         (let ((cursor-in-echo-area t)
6571               gnus-pick-mode)
6572           (describe-key (read-key-sequence nil t))))
6573     (describe-key key)))
6574
6575 (defun gnus-article-describe-key-briefly (key &optional insert)
6576   "Display documentation of the function invoked by KEY.
6577 KEY is a string or a vector."
6578   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6579                        (read-key-sequence "Describe key: "))
6580                      current-prefix-arg))
6581   (gnus-article-check-buffer)
6582   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6583                                   gnus-article-read-summary-send-keys))
6584       (with-current-buffer gnus-article-current-summary
6585         (setq unread-command-events
6586               (if (featurep 'xemacs)
6587                   (append key nil)
6588                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6589                                         (list 'meta (- x 128))
6590                                       x))
6591                         key)))
6592         (let ((cursor-in-echo-area t)
6593               gnus-pick-mode)
6594           (describe-key-briefly (read-key-sequence nil t) insert)))
6595     (describe-key-briefly key insert)))
6596
6597 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6598 (defvar gnus-agent-summary-mode)
6599 (defvar gnus-draft-mode)
6600 ;; Calling help-buffer will autoload help-mode.
6601 (defvar help-xref-stack-item)
6602 ;; Emacs 22 doesn't load it in the batch mode.
6603 (eval-when-compile
6604   (autoload 'help-buffer "help-mode"))
6605
6606 (defun gnus-article-describe-bindings (&optional prefix)
6607   "Show a list of all defined keys, and their definitions.
6608 The optional argument PREFIX, if non-nil, should be a key sequence;
6609 then we display only bindings that start with that prefix."
6610   (interactive)
6611   (gnus-article-check-buffer)
6612   (let ((keymap (copy-keymap gnus-article-mode-map))
6613         (map (copy-keymap gnus-article-send-map))
6614         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6615         parent agent draft)
6616     (define-key keymap "S" map)
6617     (define-key map [t] nil)
6618     (with-current-buffer gnus-article-current-summary
6619       (set-keymap-parent
6620        keymap
6621        (if (setq parent (keymap-parent gnus-article-mode-map))
6622            (prog1
6623                (setq parent (copy-keymap parent))
6624              (set-keymap-parent parent (current-local-map)))
6625          (current-local-map)))
6626       (set-keymap-parent map (key-binding "S"))
6627       (let (key def gnus-pick-mode)
6628         (while sumkeys
6629           (setq key (pop sumkeys))
6630           (cond ((and (vectorp key) (= (length key) 1)
6631                       (consp (setq def (aref key 0)))
6632                       (numberp (car def)) (numberp (cdr def)))
6633                  (when (< (max (car def) (cdr def)) 128)
6634                    (setq sumkeys
6635                          (append (mapcar
6636                                   #'vector
6637                                   (nreverse (gnus-uncompress-range def)))
6638                                  sumkeys))))
6639                 ((setq def (key-binding key))
6640                  (unless (eq def 'undefined)
6641                    (define-key keymap key def))))))
6642       (when (boundp 'gnus-agent-summary-mode)
6643         (setq agent gnus-agent-summary-mode))
6644       (when (boundp 'gnus-draft-mode)
6645         (setq draft gnus-draft-mode)))
6646     (with-temp-buffer
6647       (use-local-map keymap)
6648       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6649       (set (make-local-variable 'gnus-draft-mode) draft)
6650       (describe-bindings prefix))
6651     (let ((item `((lambda (prefix)
6652                     (with-current-buffer ,(current-buffer)
6653                       (gnus-article-describe-bindings prefix)))
6654                   ,prefix)))
6655       (with-current-buffer (let (help-xref-following) (help-buffer))
6656         (setq help-xref-stack-item item)))))
6657
6658 (defun gnus-article-reply-with-original (&optional wide)
6659   "Start composing a reply mail to the current message.
6660 The text in the region will be yanked.  If the region isn't active,
6661 the entire article will be yanked."
6662   (interactive)
6663   (let ((article (cdr gnus-article-current))
6664         contents)
6665     (if (not (gnus-region-active-p))
6666         (with-current-buffer gnus-summary-buffer
6667           (gnus-summary-reply (list (list article)) wide))
6668       (setq contents (buffer-substring (point) (mark t)))
6669       ;; Deactivate active regions.
6670       (when (and (boundp 'transient-mark-mode)
6671                  transient-mark-mode)
6672         (setq mark-active nil))
6673       (with-current-buffer gnus-summary-buffer
6674         (gnus-summary-reply
6675          (list (list article contents)) wide)))))
6676
6677 (defun gnus-article-wide-reply-with-original ()
6678   "Start composing a wide reply mail to the current message.
6679 The text in the region will be yanked.  If the region isn't active,
6680 the entire article will be yanked."
6681   (interactive)
6682   (gnus-article-reply-with-original t))
6683
6684 (defun gnus-article-followup-with-original ()
6685   "Compose a followup to the current article.
6686 The text in the region will be yanked.  If the region isn't active,
6687 the entire article will be yanked."
6688   (interactive)
6689   (let ((article (cdr gnus-article-current))
6690         contents)
6691       (if (not (gnus-region-active-p))
6692           (with-current-buffer gnus-summary-buffer
6693             (gnus-summary-followup (list (list article))))
6694         (setq contents (buffer-substring (point) (mark t)))
6695         ;; Deactivate active regions.
6696         (when (and (boundp 'transient-mark-mode)
6697                    transient-mark-mode)
6698           (setq mark-active nil))
6699         (with-current-buffer gnus-summary-buffer
6700           (gnus-summary-followup
6701            (list (list article contents)))))))
6702
6703 (defun gnus-article-hide (&optional arg force)
6704   "Hide all the gruft in the current article.
6705 This means that signatures, cited text and (some) headers will be
6706 hidden.
6707 If given a prefix, show the hidden text instead."
6708   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6709   (gnus-article-hide-headers arg)
6710   (gnus-article-hide-list-identifiers arg)
6711   (gnus-article-hide-citation-maybe arg force)
6712   (gnus-article-hide-signature arg))
6713
6714 (defun gnus-article-maybe-highlight ()
6715   "Do some article highlighting if article highlighting is requested."
6716   (when (gnus-visual-p 'article-highlight 'highlight)
6717     (gnus-article-highlight-some)))
6718
6719 (defun gnus-check-group-server ()
6720   ;; Make sure the connection to the server is alive.
6721   (unless (gnus-server-opened
6722            (gnus-find-method-for-group gnus-newsgroup-name))
6723     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6724     (gnus-request-group gnus-newsgroup-name t)))
6725
6726 (eval-when-compile
6727   (autoload 'nneething-get-file-name "nneething"))
6728
6729 (defun gnus-request-article-this-buffer (article group)
6730   "Get an article and insert it into this buffer."
6731   (let (do-update-line sparse-header)
6732     (prog1
6733         (save-excursion
6734           (erase-buffer)
6735           (gnus-kill-all-overlays)
6736           (setq group (or group gnus-newsgroup-name))
6737
6738           ;; Using `gnus-request-article' directly will insert the article into
6739           ;; `nntp-server-buffer' - so we'll save some time by not having to
6740           ;; copy it from the server buffer into the article buffer.
6741
6742           ;; We only request an article by message-id when we do not have the
6743           ;; headers for it, so we'll have to get those.
6744           (when (stringp article)
6745             (gnus-read-header article))
6746
6747           ;; If the article number is negative, that means that this article
6748           ;; doesn't belong in this newsgroup (possibly), so we find its
6749           ;; message-id and request it by id instead of number.
6750           (when (and (numberp article)
6751                      gnus-summary-buffer
6752                      (get-buffer gnus-summary-buffer)
6753                      (gnus-buffer-exists-p gnus-summary-buffer))
6754             (with-current-buffer gnus-summary-buffer
6755               (let ((header (gnus-summary-article-header article)))
6756                 (when (< article 0)
6757                   (cond
6758                    ((memq article gnus-newsgroup-sparse)
6759                     ;; This is a sparse gap article.
6760                     (setq do-update-line article)
6761                     (setq article (mail-header-id header))
6762                     (setq sparse-header (gnus-read-header article))
6763                     (setq gnus-newsgroup-sparse
6764                           (delq article gnus-newsgroup-sparse)))
6765                    ((vectorp header)
6766                     ;; It's a real article.
6767                     (setq article (mail-header-id header)))
6768                    (t
6769                     ;; It is an extracted pseudo-article.
6770                     (setq article 'pseudo)
6771                     (gnus-request-pseudo-article header))))
6772
6773                 (let ((method (gnus-find-method-for-group
6774                                gnus-newsgroup-name)))
6775                   (when (and (eq (car method) 'nneething)
6776                              (vectorp header))
6777                     (let ((dir (nneething-get-file-name
6778                                 (mail-header-id header))))
6779                       (when (and (stringp dir)
6780                                  (file-directory-p dir))
6781                         (setq article 'nneething)
6782                         (gnus-group-enter-directory dir))))))))
6783
6784           (cond
6785            ;; Refuse to select canceled articles.
6786            ((and (numberp article)
6787                  gnus-summary-buffer
6788                  (get-buffer gnus-summary-buffer)
6789                  (gnus-buffer-exists-p gnus-summary-buffer)
6790                  (eq (cdr (with-current-buffer gnus-summary-buffer
6791                             (assq article gnus-newsgroup-reads)))
6792                      gnus-canceled-mark))
6793             nil)
6794            ;; We first check `gnus-original-article-buffer'.
6795            ((and (get-buffer gnus-original-article-buffer)
6796                  (numberp article)
6797                  (with-current-buffer gnus-original-article-buffer
6798                    (and (equal (car gnus-original-article) group)
6799                         (eq (cdr gnus-original-article) article))))
6800             ;; `insert-buffer-substring' would incorrectly use the
6801             ;; equivalent of string-make-multibyte which amount to decoding
6802             ;; with locale-coding-system, causing failure of
6803             ;; subsequent decoding.
6804             (insert (mm-string-to-multibyte
6805                      (with-current-buffer gnus-original-article-buffer
6806                        (buffer-substring (point-min) (point-max)))))
6807             'article)
6808            ;; Check the backlog.
6809            ((and gnus-keep-backlog
6810                  (gnus-backlog-request-article group article (current-buffer)))
6811             'article)
6812            ;; Check asynchronous pre-fetch.
6813            ((gnus-async-request-fetched-article group article (current-buffer))
6814             (gnus-async-prefetch-next group article gnus-summary-buffer)
6815             (when (and (numberp article) gnus-keep-backlog)
6816               (gnus-backlog-enter-article group article (current-buffer)))
6817             'article)
6818            ;; Check the cache.
6819            ((and gnus-use-cache
6820                  (numberp article)
6821                  (gnus-cache-request-article article group))
6822             'article)
6823            ;; Check the agent cache.
6824            ((gnus-agent-request-article article group)
6825             'article)
6826            ;; Get the article and put into the article buffer.
6827            ((or (stringp article)
6828                 (numberp article))
6829             (let ((gnus-override-method gnus-override-method)
6830                   (methods (and (stringp article)
6831                                 gnus-refer-article-method))
6832                   (backend (car (gnus-find-method-for-group
6833                                  gnus-newsgroup-name)))
6834                   result
6835                   (inhibit-read-only t))
6836               (if (or (not (listp methods))
6837                       (and (symbolp (car methods))
6838                            (assq (car methods) nnoo-definition-alist)))
6839                   (setq methods (list methods)))
6840               (when (and (null gnus-override-method)
6841                          methods)
6842                 (setq gnus-override-method (pop methods)))
6843               (while (not result)
6844                 (when (eq gnus-override-method 'current)
6845                   (setq gnus-override-method
6846                         (with-current-buffer gnus-summary-buffer
6847                           gnus-current-select-method)))
6848                 (erase-buffer)
6849                 (gnus-kill-all-overlays)
6850                 (let ((gnus-newsgroup-name group))
6851                   (gnus-check-group-server))
6852                 (cond
6853                  ((gnus-request-article article group (current-buffer))
6854                   (when (numberp article)
6855                     (gnus-async-prefetch-next group article
6856                                               gnus-summary-buffer)
6857                     (when gnus-keep-backlog
6858                       (gnus-backlog-enter-article
6859                        group article (current-buffer))))
6860                   (setq result 'article))
6861                  (methods
6862                   (setq gnus-override-method (pop methods)))
6863                  ((not (string-match "^400 "
6864                                      (nnheader-get-report backend)))
6865                   ;; If we get 400 server disconnect, reconnect and
6866                   ;; retry; otherwise, assume the article has expired.
6867                   (setq result 'done))))
6868               (and (eq result 'article) 'article)))
6869            ;; It was a pseudo.
6870            (t article)))
6871
6872       ;; Associate this article with the current summary buffer.
6873       (setq gnus-article-current-summary gnus-summary-buffer)
6874
6875       ;; Take the article from the original article buffer
6876       ;; and place it in the buffer it's supposed to be in.
6877       (when (and (get-buffer gnus-article-buffer)
6878                  (equal (buffer-name (current-buffer))
6879                         (buffer-name (get-buffer gnus-article-buffer))))
6880         (save-excursion
6881           (if (get-buffer gnus-original-article-buffer)
6882               (set-buffer gnus-original-article-buffer)
6883             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6884             (buffer-disable-undo)
6885             (setq major-mode 'gnus-original-article-mode)
6886             (setq buffer-read-only t))
6887           (let ((inhibit-read-only t))
6888             (erase-buffer)
6889             (insert-buffer-substring gnus-article-buffer))
6890           (setq gnus-original-article (cons group article)))
6891
6892         ;; Decode charsets.
6893         (run-hooks 'gnus-article-decode-hook)
6894         ;; Mark article as decoded or not.
6895         (setq gnus-article-decoded-p gnus-article-decode-hook))
6896
6897       ;; Update sparse articles.
6898       (when (and do-update-line
6899                  (or (numberp article)
6900                      (stringp article)))
6901         (let ((buf (current-buffer)))
6902           (set-buffer gnus-summary-buffer)
6903           (gnus-summary-update-article do-update-line sparse-header)
6904           (gnus-summary-goto-subject do-update-line nil t)
6905           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6906                             (point))
6907           (set-buffer buf))))))
6908
6909 ;;;
6910 ;;; Article editing
6911 ;;;
6912
6913 (defcustom gnus-article-edit-mode-hook nil
6914   "Hook run in article edit mode buffers."
6915   :group 'gnus-article-various
6916   :type 'hook)
6917
6918 (defvar gnus-article-edit-done-function nil)
6919
6920 (defvar gnus-article-edit-mode-map nil)
6921
6922 ;; Should we be using derived.el for this?
6923 (unless gnus-article-edit-mode-map
6924   (setq gnus-article-edit-mode-map (make-keymap))
6925   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6926
6927   (gnus-define-keys gnus-article-edit-mode-map
6928     "\C-c?"    describe-mode
6929     "\C-c\C-c" gnus-article-edit-done
6930     "\C-c\C-k" gnus-article-edit-exit
6931     "\C-c\C-f\C-t" message-goto-to
6932     "\C-c\C-f\C-o" message-goto-from
6933     "\C-c\C-f\C-b" message-goto-bcc
6934     ;;"\C-c\C-f\C-w" message-goto-fcc
6935     "\C-c\C-f\C-c" message-goto-cc
6936     "\C-c\C-f\C-s" message-goto-subject
6937     "\C-c\C-f\C-r" message-goto-reply-to
6938     "\C-c\C-f\C-n" message-goto-newsgroups
6939     "\C-c\C-f\C-d" message-goto-distribution
6940     "\C-c\C-f\C-f" message-goto-followup-to
6941     "\C-c\C-f\C-m" message-goto-mail-followup-to
6942     "\C-c\C-f\C-k" message-goto-keywords
6943     "\C-c\C-f\C-u" message-goto-summary
6944     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6945     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6946     "\C-c\C-b" message-goto-body
6947     "\C-c\C-i" message-goto-signature
6948
6949     "\C-c\C-t" message-insert-to
6950     "\C-c\C-n" message-insert-newsgroups
6951     "\C-c\C-o" message-sort-headers
6952     "\C-c\C-e" message-elide-region
6953     "\C-c\C-v" message-delete-not-region
6954     "\C-c\C-z" message-kill-to-signature
6955     "\M-\r" message-newline-and-reformat
6956     "\C-c\C-a" mml-attach-file
6957     "\C-a" message-beginning-of-line
6958     "\t" message-tab
6959     "\M-;" comment-region)
6960
6961   (gnus-define-keys (gnus-article-edit-wash-map
6962                      "\C-c\C-w" gnus-article-edit-mode-map)
6963     "f" gnus-article-edit-full-stops))
6964
6965 (easy-menu-define
6966   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6967   '("Field"
6968     ["Fetch To" message-insert-to t]
6969     ["Fetch Newsgroups" message-insert-newsgroups t]
6970     "----"
6971     ["To" message-goto-to t]
6972     ["From" message-goto-from t]
6973     ["Subject" message-goto-subject t]
6974     ["Cc" message-goto-cc t]
6975     ["Reply-To" message-goto-reply-to t]
6976     ["Summary" message-goto-summary t]
6977     ["Keywords" message-goto-keywords t]
6978     ["Newsgroups" message-goto-newsgroups t]
6979     ["Followup-To" message-goto-followup-to t]
6980     ["Mail-Followup-To" message-goto-mail-followup-to t]
6981     ["Distribution" message-goto-distribution t]
6982     ["Body" message-goto-body t]
6983     ["Signature" message-goto-signature t]))
6984
6985 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6986   "Major mode for editing articles.
6987 This is an extended text-mode.
6988
6989 \\{gnus-article-edit-mode-map}"
6990   (make-local-variable 'gnus-article-edit-done-function)
6991   (make-local-variable 'gnus-prev-winconf)
6992   (set (make-local-variable 'font-lock-defaults)
6993        '(message-font-lock-keywords t))
6994   (set (make-local-variable 'mail-header-separator) "")
6995   (set (make-local-variable 'gnus-article-edit-mode) t)
6996   (easy-menu-add message-mode-field-menu message-mode-map)
6997   (mml-mode)
6998   (setq buffer-read-only nil)
6999   (buffer-enable-undo)
7000   (widen))
7001
7002 (defun gnus-article-edit (&optional force)
7003   "Edit the current article.
7004 This will have permanent effect only in mail groups.
7005 If FORCE is non-nil, allow editing of articles even in read-only
7006 groups."
7007   (interactive "P")
7008   (when (and (not force)
7009              (gnus-group-read-only-p))
7010     (error "The current newsgroup does not support article editing"))
7011   (gnus-article-date-original)
7012   (gnus-article-edit-article
7013    'ignore
7014    `(lambda (no-highlight)
7015       'ignore
7016       (gnus-summary-edit-article-done
7017        ,(or (mail-header-references gnus-current-headers) "")
7018        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7019
7020 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7021   "Start editing the contents of the current article buffer."
7022   (let ((winconf (current-window-configuration)))
7023     (set-buffer gnus-article-buffer)
7024     (let ((message-auto-save-directory
7025            ;; Don't associate the article buffer with a draft file.
7026            nil))
7027       (gnus-article-edit-mode))
7028     (funcall start-func)
7029     (set-buffer-modified-p nil)
7030     (gnus-configure-windows 'edit-article)
7031     (setq gnus-article-edit-done-function exit-func)
7032     (setq gnus-prev-winconf winconf)
7033     (unless quiet
7034       (gnus-message 6 "C-c C-c to end edits"))))
7035
7036 (defun gnus-article-edit-done (&optional arg)
7037   "Update the article edits and exit."
7038   (interactive "P")
7039   (let ((func gnus-article-edit-done-function)
7040         (buf (current-buffer))
7041         (start (window-start))
7042         (p (point))
7043         (winconf gnus-prev-winconf))
7044     (widen) ;; Widen it in case that users narrowed the buffer.
7045     (funcall func arg)
7046     (set-buffer buf)
7047     ;; The cache and backlog have to be flushed somewhat.
7048     (when gnus-keep-backlog
7049       (gnus-backlog-remove-article
7050        (car gnus-article-current) (cdr gnus-article-current)))
7051     ;; Flush original article as well.
7052     (gnus-flush-original-article-buffer)
7053     (when gnus-use-cache
7054       (gnus-cache-update-article
7055        (car gnus-article-current) (cdr gnus-article-current)))
7056     ;; We remove all text props from the article buffer.
7057     (kill-all-local-variables)
7058     (set-text-properties (point-min) (point-max) nil)
7059     (gnus-article-mode)
7060     (set-window-configuration winconf)
7061     (set-buffer buf)
7062     (set-window-start (get-buffer-window buf) start)
7063     (set-window-point (get-buffer-window buf) (point)))
7064   (gnus-summary-show-article))
7065
7066 (defun gnus-flush-original-article-buffer ()
7067   (when (get-buffer gnus-original-article-buffer)
7068     (with-current-buffer gnus-original-article-buffer
7069       (setq gnus-original-article nil))))
7070
7071 (defun gnus-article-edit-exit ()
7072   "Exit the article editing without updating."
7073   (interactive)
7074   (when (or (not (buffer-modified-p))
7075             (yes-or-no-p "Article modified; kill anyway? "))
7076     (let ((curbuf (current-buffer))
7077           (p (point))
7078           (window-start (window-start)))
7079       (erase-buffer)
7080       (if (gnus-buffer-live-p gnus-original-article-buffer)
7081           (insert-buffer-substring gnus-original-article-buffer))
7082       (let ((winconf gnus-prev-winconf))
7083         (kill-all-local-variables)
7084         (gnus-article-mode)
7085         (set-window-configuration winconf)
7086         ;; Tippy-toe some to make sure that point remains where it was.
7087         (save-current-buffer
7088           (set-buffer curbuf)
7089           (set-window-start (get-buffer-window (current-buffer)) window-start)
7090           (goto-char p))))
7091     (gnus-summary-show-article)))
7092
7093 (defun gnus-article-edit-full-stops ()
7094   "Interactively repair spacing at end of sentences."
7095   (interactive)
7096   (save-excursion
7097     (goto-char (point-min))
7098     (search-forward-regexp "^$" nil t)
7099     (let ((case-fold-search nil))
7100       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7101
7102 ;;;
7103 ;;; Article highlights
7104 ;;;
7105
7106 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7107
7108 ;;; Internal Variables:
7109
7110 (defcustom gnus-button-url-regexp
7111   (concat
7112    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7113    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7114    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7115    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7116        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7117              (punct "!?:;.,"))
7118          (concat
7119           "\\(?:"
7120           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7121           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7122           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7123           "\\|"
7124           "[" chars punct     "]+" "[" chars "]"
7125           "\\)"))
7126      (concat ;; XEmacs 21.4 doesn't support POSIX.
7127       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7128       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7129    "\\)")
7130   "Regular expression that matches URLs."
7131   :group 'gnus-article-buttons
7132   :type 'regexp)
7133
7134 (defcustom gnus-button-valid-fqdn-regexp
7135   message-valid-fqdn-regexp
7136   "Regular expression that matches a valid FQDN."
7137   :version "22.1"
7138   :group 'gnus-article-buttons
7139   :type 'regexp)
7140
7141 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7142 (defcustom gnus-button-valid-localpart-regexp
7143   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7144   "Regular expression that matches a localpart of mail addresses or MIDs."
7145   :version "22.1"
7146   :group 'gnus-article-buttons
7147   :type 'regexp)
7148
7149 (defcustom gnus-button-man-handler 'manual-entry
7150   "Function to use for displaying man pages.
7151 The function must take at least one argument with a string naming the
7152 man page."
7153   :version "22.1"
7154   :type '(choice (function-item :tag "Man" manual-entry)
7155                  (function-item :tag "Woman" woman)
7156                  (function :tag "Other"))
7157   :group 'gnus-article-buttons)=
7158
7159 (defcustom gnus-button-mid-or-mail-regexp
7160   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7161           gnus-button-valid-fqdn-regexp
7162           ">?\\)\\b")
7163   "Regular expression that matches a message ID or a mail address."
7164   :version "22.1"
7165   :group 'gnus-article-buttons
7166   :type 'regexp)
7167
7168 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7169   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7170 Strings like this can be either a message ID or a mail address.  If it is one
7171 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7172 message ID or a mail address, respectively.  If this variable is set to the
7173 symbol `ask', always query the user what do do.  If it is a function, this
7174 function will be called with the string as its only argument.  The function
7175 must return `mid', `mail', `invalid' or `ask'."
7176   :version "22.1"
7177   :group 'gnus-article-buttons
7178   :type '(choice (function-item :tag "Heuristic function"
7179                                 gnus-button-mid-or-mail-heuristic)
7180                  (const ask)
7181                  (const mid)
7182                  (const mail)))
7183
7184 (defcustom gnus-button-mid-or-mail-heuristic-alist
7185   '((-10.0 . ".+\\$.+@")
7186     (-10.0 . "#")
7187     (-10.0 . "\\*")
7188     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7189     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7190     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7191     (-1.0  . "^[^a-z]+@")
7192     ;;
7193     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7194     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7195     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7196     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7197     ;;
7198     (-2.0  . "^[0-9]")
7199     (-1.0  . "^[0-9][0-9]")
7200     ;;
7201     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7202     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7203     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7204     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7205     ;;
7206     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7207     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7208     ;;       "[0-9]{8,}.*\@"
7209     (-3.0
7210      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7211     ;; "[0-9]{12,}.*\@"
7212     ;; compensation for TDMA dated mail addresses:
7213     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7214     ;;
7215     (-20.0 . "\\.fsf@") ;; Gnus
7216     (-20.0 . "^slrn")
7217     (-20.0 . "^Pine")
7218     (-20.0 . "^alpine\\.")
7219     (-20.0 . "_-_") ;; Subject change in thread
7220     ;;
7221     (-20.0 . "\\.ln@") ;; leafnode
7222     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7223     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7224     ;;
7225     ;; (5.0 . "") ;; $local_part_len <= 7
7226     (10.0  . "^[^0-9]+@")
7227     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7228     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7229     (3.0   . "\@stud")
7230     ;;
7231     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7232     ;;
7233     (0.5   . "^[A-Z][a-z]")
7234     (0.5   . "^[A-Z][a-z][a-z]")
7235     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7236     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7237   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7238
7239 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7240 address.  The REGEXP is processed with `case-fold-search' set to nil."
7241   :version "22.1"
7242   :group 'gnus-article-buttons
7243   :type '(repeat (cons (number :tag "Rate")
7244                        (regexp :tag "Regexp"))))
7245
7246 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7247   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7248 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7249 address, `ask' if unsure and `invalid' if the string is invalid."
7250   (let ((case-fold-search nil)
7251         (list gnus-button-mid-or-mail-heuristic-alist)
7252         (result 0) rate regexp lpartlen elem)
7253     (setq lpartlen
7254           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7255     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7256     ;; Certain special cases...
7257     (when (string-match
7258            (concat
7259             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7260             "^[0-9]+\\.[0-9]+@compuserve\\|"
7261             "@public\\.gmane\\.org")
7262            mid-or-mail)
7263       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7264       (setq result 'mail))
7265     (when (string-match "@.*@\\| " mid-or-mail)
7266       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7267       (setq result 'invalid))
7268     ;; Nothing more to do, if result is not a number here...
7269     (when (numberp result)
7270       (while list
7271         (setq elem (car list)
7272               rate (car elem)
7273               regexp (cdr elem)
7274               list (cdr list))
7275         (when (string-match regexp mid-or-mail)
7276           (setq result (+ result rate))
7277           (gnus-message
7278            9 "`%s' matched `%s', rate `%s', result `%s'."
7279            mid-or-mail regexp rate result)))
7280       (when (<= lpartlen 7)
7281         (setq result (+ result 5.0))
7282         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7283                       mid-or-mail result))
7284       (when (>= lpartlen 12)
7285         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7286         (cond
7287          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7288           ;; Long local part should contain realname if e-mail address,
7289           ;; too many digits: message-id.
7290           ;; $score -= 5.0 + 0.1 * $local_part_len;
7291           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7292           (setq result (+ result rate))
7293           (gnus-message
7294            9 "Many digits in `%s', rate `%s', result `%s'."
7295            mid-or-mail rate result))
7296          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7297                         mid-or-mail)
7298           ;; Too few vowels [^aeiouy]{4,}.*\@
7299           (setq result (+ result -5.0))
7300           (gnus-message
7301            9 "Few vowels in `%s', rate `%s', result `%s'."
7302            mid-or-mail -5.0 result))
7303          (t
7304           (setq result (+ result 5.0))
7305           (gnus-message
7306            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7307     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7308     ;; Maybe we should make this a customizable alist: (condition . 'result)
7309     (cond
7310      ((symbolp result) result)
7311      ;; Now convert number into proper results:
7312      ((< result -10.0) 'mid)
7313      ((> result  10.0) 'mail)
7314      (t 'ask))))
7315
7316 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7317   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7318          (url-mid (concat "news" ":" mid-or-mail))
7319          (url-mailto (concat "mailto" ":" mid-or-mail)))
7320     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7321     (when (fboundp pref)
7322       (setq guessed
7323             ;; get rid of surrounding angles...
7324             (funcall pref
7325                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7326       (if (or (eq 'mid guessed) (eq 'mail guessed))
7327           (setq pref guessed)
7328         (setq pref 'ask)))
7329     (if (eq pref 'ask)
7330         (save-window-excursion
7331           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7332               (setq pref 'mail)
7333             (setq pref 'mid))))
7334     (cond ((eq pref 'mid)
7335            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7336            (gnus-button-handle-news url-mid))
7337           ((eq pref 'mail)
7338            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7339            (gnus-url-mailto url-mailto))
7340           (t (gnus-message 3 "Invalid string.")))))
7341
7342 (defun gnus-button-handle-custom (fun arg)
7343   "Call function FUN on argument ARG.
7344 Both FUN and ARG are supposed to be strings.  ARG will be passed
7345 as a symbol to FUN."
7346   (funcall (intern fun)
7347            (if (string-match "^customize-apropos" fun)
7348                arg
7349              (intern arg))))
7350
7351 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7352
7353 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7354 ;; stuff?
7355
7356 (defun gnus-button-handle-describe-function (url)
7357   "Call `describe-function' when pushing the corresponding URL button."
7358   (describe-function
7359    (intern
7360     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7361
7362 (defun gnus-button-handle-describe-variable (url)
7363   "Call `describe-variable' when pushing the corresponding URL button."
7364   (describe-variable
7365    (intern
7366     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7367
7368 (defun gnus-button-handle-symbol (url)
7369 "Display help on variable or function.
7370 Calls `describe-variable' or `describe-function'."
7371   (let ((sym (intern url)))
7372     (cond
7373      ((fboundp sym) (describe-function sym))
7374      ((boundp sym) (describe-variable sym))
7375      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7376
7377 (defun gnus-button-handle-describe-key (url)
7378   "Call `describe-key' when pushing the corresponding URL button."
7379   (let* ((key-string
7380           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7381          (keys (ignore-errors (eval `(kbd ,key-string)))))
7382     (if keys
7383         (describe-key keys)
7384       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7385
7386 (defun gnus-button-handle-apropos (url)
7387   "Call `apropos' when pushing the corresponding URL button."
7388   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7389
7390 (defun gnus-button-handle-apropos-command (url)
7391   "Call `apropos' when pushing the corresponding URL button."
7392   (apropos-command
7393    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7394
7395 (defun gnus-button-handle-apropos-variable (url)
7396   "Call `apropos' when pushing the corresponding URL button."
7397   (funcall
7398    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7399    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7400
7401 (defun gnus-button-handle-apropos-documentation (url)
7402   "Call `apropos' when pushing the corresponding URL button."
7403   (funcall
7404    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7405    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7406
7407 (defun gnus-button-handle-library (url)
7408   "Call `locate-library' when pushing the corresponding URL button."
7409   (gnus-message 9 "url=`%s'" url)
7410   (let* ((lib (locate-library url))
7411          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7412     (if (not lib)
7413         (gnus-message 1 "Cannot locale library `%s'." url)
7414       (find-file-read-only file))))
7415
7416 (defcustom gnus-button-man-level 5
7417   "*Integer that says how many man-related buttons Gnus will show.
7418 The higher the number, the more buttons will appear and the more false
7419 positives are possible.  Note that you can set this variable local to
7420 specific groups.  Setting it higher in Unix groups is probably a good idea.
7421 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7422 how to set variables in specific groups."
7423   :version "22.1"
7424   :group 'gnus-article-buttons
7425   :link '(custom-manual "(gnus)Group Parameters")
7426   :type 'integer)
7427
7428 (defcustom gnus-button-emacs-level 5
7429   "*Integer that says how many emacs-related buttons Gnus will show.
7430 The higher the number, the more buttons will appear and the more false
7431 positives are possible.  Note that you can set this variable local to
7432 specific groups.  Setting it higher in Emacs or Gnus related groups is
7433 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7434 `gnus-parameters' on how to set variables in specific groups."
7435   :version "22.1"
7436   :group 'gnus-article-buttons
7437   :link '(custom-manual "(gnus)Group Parameters")
7438   :type 'integer)
7439
7440 (defcustom gnus-button-message-level 5
7441   "*Integer that says how many buttons for news or mail messages will appear.
7442 The higher the number, the more buttons will appear and the more false
7443 positives are possible."
7444   ;; mail addresses, MIDs, URLs for news, ...
7445   :version "22.1"
7446   :group 'gnus-article-buttons
7447   :type 'integer)
7448
7449 (defcustom gnus-button-browse-level 5
7450   "*Integer that says how many buttons for browsing will appear.
7451 The higher the number, the more buttons will appear and the more false
7452 positives are possible."
7453   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7454   :version "22.1"
7455   :group 'gnus-article-buttons
7456   :type 'integer)
7457
7458 (defcustom gnus-button-alist
7459   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7460      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7461     ((concat "\\b\\(nntp\\|news\\):\\("
7462              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7463      0 t gnus-button-handle-news 2)
7464     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7465      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7466     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7467      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7468     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7469     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7470      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7471     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7472      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7473     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7474      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7475     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7476      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7477     ;; RFC 2368 (The mailto URL scheme)
7478     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7479      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7480     ("\\bmailto:\\([^ \n\t]+\\)"
7481      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7482     ;; Info Konqueror style <info:/foo/bar baz>.
7483     ;; Must come before " Gnus home-grown style".
7484     ("\\binfo://?\\([^'\">\n\t]+\\)"
7485      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7486    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7487     ("\\binfo://\\([^'\">\n\t ]+\\)"
7488      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7489     ;; Info GNOME style <info:foo#bar_baz>
7490     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7491      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7492     ;; Info KDE style <info:(foo)bar baz>
7493     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7494      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7495     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7496      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7497     ("\\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,]*\\)\\)?"
7498      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7499      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7500     ;; This is custom
7501     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7502      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7503     ;; Emacs help commands
7504     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7505      ;; regexp doesn't match arguments containing ` '.
7506      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7507     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7508      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7509     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7510      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7511     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7512      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7513     ;; The following entries may lead to many false positives so don't enable
7514     ;; them by default (use a high button level).
7515     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7516      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7517      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7518     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7519      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7520     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7521      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7522     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7523      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7524     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7525      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7526     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7527      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7528     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7529      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7530     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7531      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7532     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7533      ;; Unlike the other regexps we really have to require quoting
7534      ;; here to determine where it ends.
7535      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7536     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7537     ("<URL: *\\([^\n<>]*\\)>"
7538      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7539     ;; RFC 2396 (2.4.3., delims) ...
7540     ("\"URL: *\\([^\n\"]*\\)\""
7541      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7542     ;; Raw URLs.
7543     (gnus-button-url-regexp
7544      0 (>= gnus-button-browse-level 0) browse-url 0)
7545     ;; man pages
7546     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7547      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7548      gnus-button-handle-man 1)
7549     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7550     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7551      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7552      gnus-button-handle-man 1)
7553     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7554     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7555     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7556      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7557     ;; Recognizing patches to .el files.  This is somewhat obscure,
7558     ;; but considering the percentage of Gnus users who hack Emacs
7559     ;; Lisp files...
7560     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7561      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7562     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7563      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7564     ;; MID or mail: To avoid too many false positives we don't try to catch
7565     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7566     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7567     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7568     ;; so that non-ambiguous entries (see above) match first.
7569     (gnus-button-mid-or-mail-regexp
7570      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7571   "*Alist of regexps matching buttons in article bodies.
7572
7573 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7574 REGEXP: is the string (case insensitive) matching text around the button (can
7575 also be Lisp expression evaluating to a string),
7576 BUTTON: is the number of the regexp grouping actually matching the button,
7577 FORM: is a Lisp expression which must eval to true for the button to
7578 be added,
7579 CALLBACK: is the function to call when the user push this button, and each
7580 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7581
7582 CALLBACK can also be a variable, in that case the value of that
7583 variable it the real callback function."
7584   :group 'gnus-article-buttons
7585   :type '(repeat (list (choice regexp variable sexp)
7586                        (integer :tag "Button")
7587                        (sexp :tag "Form")
7588                        (function :tag "Callback")
7589                        (repeat :tag "Par"
7590                                :inline t
7591                                (integer :tag "Regexp group")))))
7592 (put 'gnus-button-alist 'risky-local-variable t)
7593
7594 (defcustom gnus-header-button-alist
7595   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7596      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7597     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7598      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7599     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7600      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7601     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7602      0 (>= gnus-button-browse-level 0) browse-url 0)
7603     ("^Subject:" gnus-button-url-regexp
7604      0 (>= gnus-button-browse-level 0) browse-url 0)
7605     ("^[^:]+:" gnus-button-url-regexp
7606      0 (>= gnus-button-browse-level 0) browse-url 0)
7607     ("^OpenPGP:.*url=" gnus-button-url-regexp
7608      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7609     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7610      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7611     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7612      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7613   "*Alist of headers and regexps to match buttons in article heads.
7614
7615 This alist is very similar to `gnus-button-alist', except that each
7616 alist has an additional HEADER element first in each entry:
7617
7618 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7619
7620 HEADER is a regexp to match a header.  For a fuller explanation, see
7621 `gnus-button-alist'."
7622   :group 'gnus-article-buttons
7623   :group 'gnus-article-headers
7624   :type '(repeat (list (regexp :tag "Header")
7625                        (choice regexp variable)
7626                        (integer :tag "Button")
7627                        (sexp :tag "Form")
7628                        (function :tag "Callback")
7629                        (repeat :tag "Par"
7630                                :inline t
7631                                (integer :tag "Regexp group")))))
7632 (put 'gnus-header-button-alist 'risky-local-variable t)
7633
7634 ;;; Commands:
7635
7636 (defun gnus-article-push-button (event)
7637   "Check text under the mouse pointer for a callback function.
7638 If the text under the mouse pointer has a `gnus-callback' property,
7639 call it with the value of the `gnus-data' text property."
7640   (interactive "e")
7641   (set-buffer (window-buffer (posn-window (event-start event))))
7642   (let* ((pos (posn-point (event-start event)))
7643          (data (get-text-property pos 'gnus-data))
7644          (fun (get-text-property pos 'gnus-callback)))
7645     (goto-char pos)
7646     (when fun
7647       (funcall fun data))))
7648
7649 (defun gnus-article-press-button ()
7650   "Check text at point for a callback function.
7651 If the text at point has a `gnus-callback' property,
7652 call it with the value of the `gnus-data' text property."
7653   (interactive)
7654   (let ((data (get-text-property (point) 'gnus-data))
7655         (fun (get-text-property (point) 'gnus-callback)))
7656     (when fun
7657       (funcall fun data))))
7658
7659 (defun gnus-article-highlight (&optional force)
7660   "Highlight current article.
7661 This function calls `gnus-article-highlight-headers',
7662 `gnus-article-highlight-citation',
7663 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7664 do the highlighting.  See the documentation for those functions."
7665   (interactive (list 'force))
7666   (gnus-article-highlight-headers)
7667   (gnus-article-highlight-citation force)
7668   (gnus-article-highlight-signature)
7669   (gnus-article-add-buttons force)
7670   (gnus-article-add-buttons-to-head))
7671
7672 (defun gnus-article-highlight-some (&optional force)
7673   "Highlight current article.
7674 This function calls `gnus-article-highlight-headers',
7675 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7676 do the highlighting.  See the documentation for those functions."
7677   (interactive (list 'force))
7678   (gnus-article-highlight-headers)
7679   (gnus-article-highlight-signature)
7680   (gnus-article-add-buttons))
7681
7682 (defun gnus-article-highlight-headers ()
7683   "Highlight article headers as specified by `gnus-header-face-alist'."
7684   (interactive)
7685   (gnus-with-article-headers
7686     (let (regexp header-face field-face from hpoints fpoints)
7687       (dolist (entry gnus-header-face-alist)
7688         (goto-char (point-min))
7689         (setq regexp (concat "^\\("
7690                              (if (string-equal "" (nth 0 entry))
7691                                  "[^\t ]"
7692                                (nth 0 entry))
7693                              "\\)")
7694               header-face (nth 1 entry)
7695               field-face (nth 2 entry))
7696         (while (and (re-search-forward regexp nil t)
7697                     (not (eobp)))
7698           (beginning-of-line)
7699           (setq from (point))
7700           (unless (search-forward ":" nil t)
7701             (forward-char 1))
7702           (when (and header-face
7703                      (not (memq (point) hpoints)))
7704             (push (point) hpoints)
7705             (gnus-put-text-property from (point) 'face header-face))
7706           (when (and field-face
7707                      (not (memq (setq from (point)) fpoints)))
7708             (push from fpoints)
7709             (if (re-search-forward "^[^ \t]" nil t)
7710                 (forward-char -2)
7711               (goto-char (point-max)))
7712             (gnus-put-text-property from (point) 'face field-face)))))))
7713
7714 (defun gnus-article-highlight-signature ()
7715   "Highlight the signature in an article.
7716 It does this by highlighting everything after
7717 `gnus-signature-separator' using the face `gnus-signature'."
7718   (interactive)
7719   (gnus-with-article-buffer
7720     (let ((inhibit-point-motion-hooks t))
7721       (save-restriction
7722         (when (and gnus-signature-face
7723                    (gnus-article-narrow-to-signature))
7724           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7725                             'face gnus-signature-face)
7726           (widen)
7727           (gnus-article-search-signature)
7728           (let ((start (match-beginning 0))
7729                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7730             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7731                                      end)))))))
7732
7733 (defun gnus-button-in-region-p (b e prop)
7734   "Say whether PROP exists in the region."
7735   (text-property-not-all b e prop nil))
7736
7737 (defun gnus-article-add-buttons (&optional force)
7738   "Find external references in the article and make buttons of them.
7739 \"External references\" are things like Message-IDs and URLs, as
7740 specified by `gnus-button-alist'."
7741   (interactive (list 'force))
7742   (gnus-with-article-buffer
7743     (let ((inhibit-point-motion-hooks t)
7744           (case-fold-search t)
7745           (alist gnus-button-alist)
7746           beg entry regexp)
7747       ;; Remove all old markers.
7748       (let (marker entry new-list)
7749         (while (setq marker (pop gnus-button-marker-list))
7750           (if (or (< marker (point-min)) (>= marker (point-max)))
7751               (push marker new-list)
7752             (goto-char marker)
7753             (when (setq entry (gnus-button-entry))
7754               (put-text-property (match-beginning (nth 1 entry))
7755                                  (match-end (nth 1 entry))
7756                                  'gnus-callback nil))
7757             (set-marker marker nil)))
7758         (setq gnus-button-marker-list new-list))
7759       ;; We skip the headers.
7760       (article-goto-body)
7761       (setq beg (point))
7762       (while (setq entry (pop alist))
7763         (setq regexp (eval (car entry)))
7764         (goto-char beg)
7765         (while (re-search-forward regexp nil t)
7766           (let ((start (match-beginning (nth 1 entry)))
7767                 (end (match-end (nth 1 entry)))
7768                 (from (match-beginning 0)))
7769             (when (and (or (eq t (nth 2 entry))
7770                            (eval (nth 2 entry)))
7771                        (not (gnus-button-in-region-p
7772                              start end 'gnus-callback)))
7773               ;; That optional form returned non-nil, so we add the
7774               ;; button.
7775               (setq from (set-marker (make-marker) from))
7776               (push from gnus-button-marker-list)
7777               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7778                            (gnus-article-extend-url-button from start end))
7779                 (gnus-article-add-button start end
7780                                          'gnus-button-push from)
7781                 (gnus-put-text-property
7782                  start end
7783                  'gnus-string (buffer-substring-no-properties
7784                                start end))))))))))
7785
7786 (defun gnus-article-extend-url-button (beg start end)
7787   "Extend url button if url is folded into two or more lines.
7788 Return non-nil if button is extended.  BEG is a marker that points to
7789 the beginning position of a text containing url.  START and END are
7790 the endpoints of a url button before it is extended.  The concatenated
7791 url is put as the `gnus-button-url' overlay property on the button."
7792   (let ((opoint (point))
7793         (points (list start end))
7794         url delim regexp)
7795     (prog1
7796         (when (and (progn
7797                      (goto-char end)
7798                      (not (looking-at "[\t ]*[\">]")))
7799                    (progn
7800                      (goto-char start)
7801                      (string-match
7802                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7803                       (buffer-substring (point-at-bol) start)))
7804                    (progn
7805                      (setq url (list (buffer-substring start end))
7806                            delim (if (match-beginning 1) ">" "\""))
7807                      (beginning-of-line)
7808                      (setq regexp (concat
7809                                    (when (and (looking-at
7810                                                message-cite-prefix-regexp)
7811                                               (< (match-end 0) start))
7812                                      (regexp-quote (match-string 0)))
7813                                    "\
7814 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7815                                    delim "\\)"))
7816                      (while (progn
7817                               (forward-line 1)
7818                               (and (looking-at regexp)
7819                                    (prog1
7820                                        (match-beginning 1)
7821                                      (push (or (match-string 2)
7822                                                (match-string 1))
7823                                            url)
7824                                      (push (setq end (or (match-end 2)
7825                                                          (match-end 1)))
7826                                            points)
7827                                      (push (or (match-beginning 2)
7828                                                (match-beginning 1))
7829                                            points)))))
7830                      (match-beginning 2)))
7831           (let (gnus-article-mouse-face widget-mouse-face)
7832             (while points
7833               (gnus-article-add-button (pop points) (pop points)
7834                                        'gnus-button-push beg)))
7835           (let ((overlay (gnus-make-overlay start end)))
7836             (gnus-overlay-put overlay 'evaporate t)
7837             (gnus-overlay-put overlay 'gnus-button-url
7838                               (list (mapconcat 'identity (nreverse url) "")))
7839             (when gnus-article-mouse-face
7840               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7841           t)
7842       (goto-char opoint))))
7843
7844 ;; Add buttons to the head of an article.
7845 (defun gnus-article-add-buttons-to-head ()
7846   "Add buttons to the head of the article."
7847   (interactive)
7848   (gnus-with-article-headers
7849     (let (beg end)
7850       (dolist (entry gnus-header-button-alist)
7851         ;; Each alist entry.
7852         (goto-char (point-min))
7853         (while (re-search-forward (car entry) nil t)
7854           ;; Each header matching the entry.
7855           (setq beg (match-beginning 0))
7856           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7857                              (match-beginning 0))
7858                         (point-max)))
7859           (goto-char beg)
7860           (while (re-search-forward (eval (nth 1 entry)) end t)
7861             ;; Each match within a header.
7862             (let* ((entry (cdr entry))
7863                    (start (match-beginning (nth 1 entry)))
7864                    (end (match-end (nth 1 entry)))
7865                    (form (nth 2 entry)))
7866               (goto-char (match-end 0))
7867               (when (eval form)
7868                 (gnus-article-add-button
7869                  start end (nth 3 entry)
7870                  (buffer-substring (match-beginning (nth 4 entry))
7871                                    (match-end (nth 4 entry)))))))
7872           (goto-char end))))))
7873
7874 ;;; External functions:
7875
7876 (defun gnus-article-add-button (from to fun &optional data text)
7877   "Create a button between FROM and TO with callback FUN and data DATA."
7878   (when gnus-article-button-face
7879     (gnus-overlay-put (gnus-make-overlay from to nil t)
7880                       'face gnus-article-button-face))
7881   (gnus-add-text-properties
7882    from to
7883    (nconc (and gnus-article-mouse-face
7884                (list gnus-mouse-face-prop gnus-article-mouse-face))
7885           (list 'gnus-callback fun)
7886           (and data (list 'gnus-data data))))
7887   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7888                          :help-echo (or text "Follow the link")
7889                          :keymap gnus-url-button-map
7890                          :button-keymap gnus-widget-button-keymap))
7891
7892 (defun gnus-article-copy-string ()
7893   "Copy the string in the button to the kill ring."
7894   (interactive)
7895   (gnus-article-check-buffer)
7896   (let ((data (get-text-property (point) 'gnus-string)))
7897     (when data
7898       (with-temp-buffer
7899         (insert data)
7900         (copy-region-as-kill (point-min) (point-max))
7901         (message "Copied %s" data)))))
7902
7903 ;;; Internal functions:
7904
7905 (defun gnus-article-set-globals ()
7906   (with-current-buffer gnus-summary-buffer
7907     (gnus-set-global-variables)))
7908
7909 (defun gnus-signature-toggle (end)
7910   (gnus-with-article-buffer
7911     (let ((inhibit-point-motion-hooks t))
7912       (if (text-property-any end (point-max) 'article-type 'signature)
7913           (progn
7914             (gnus-delete-wash-type 'signature)
7915             (gnus-remove-text-properties-when
7916              'article-type 'signature end (point-max)
7917              (cons 'article-type (cons 'signature
7918                                        gnus-hidden-properties))))
7919         (gnus-add-wash-type 'signature)
7920         (gnus-add-text-properties-when
7921          'article-type nil end (point-max)
7922          (cons 'article-type (cons 'signature
7923                                    gnus-hidden-properties)))))
7924     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7925       (gnus-set-mode-line 'article))))
7926
7927 (defun gnus-button-entry ()
7928   ;; Return the first entry in `gnus-button-alist' matching this place.
7929   (let ((alist gnus-button-alist)
7930         (entry nil))
7931     (while alist
7932       (setq entry (pop alist))
7933       (if (looking-at (eval (car entry)))
7934           (setq alist nil)
7935         (setq entry nil)))
7936     entry))
7937
7938 (defun gnus-button-push (marker)
7939   ;; Push button starting at MARKER.
7940   (save-excursion
7941     (goto-char marker)
7942     (let* ((entry (gnus-button-entry))
7943            (inhibit-point-motion-hooks t)
7944            (fun (nth 3 entry))
7945            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7946                           (get-char-property marker 'gnus-button-url))
7947                      (mapcar (lambda (group)
7948                                (let ((string (match-string group)))
7949                                  (set-text-properties
7950                                   0 (length string) nil string)
7951                                  string))
7952                              (nthcdr 4 entry)))))
7953       (cond
7954        ((fboundp fun)
7955         (apply fun args))
7956        ((and (boundp fun)
7957              (fboundp (symbol-value fun)))
7958         (apply (symbol-value fun) args))
7959        (t
7960         (gnus-message 1 "You must define `%S' to use this button"
7961                       (cons fun args)))))))
7962
7963 (defun gnus-parse-news-url (url)
7964   (let (scheme server port group message-id articles)
7965     (with-temp-buffer
7966       (insert url)
7967       (goto-char (point-min))
7968       (when (looking-at "\\([A-Za-z]+\\):")
7969         (setq scheme (match-string 1))
7970         (goto-char (match-end 0)))
7971       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7972         (setq server (match-string 1))
7973         (setq port (if (stringp (match-string 3))
7974                        (string-to-number (match-string 3))
7975                      (match-string 3)))
7976         (goto-char (match-end 0)))
7977
7978       (cond
7979        ((looking-at "\\(.*@.*\\)")
7980         (setq message-id (match-string 1)))
7981        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7982         (setq group (match-string 1)
7983               articles (split-string (match-string 2) "-")))
7984        ((looking-at "\\([^/]+\\)/?")
7985         (setq group (match-string 1)))
7986        (t
7987         (error "Unknown news URL syntax"))))
7988     (list scheme server port group message-id articles)))
7989
7990 (defun gnus-button-handle-news (url)
7991   "Fetch a news URL."
7992   (destructuring-bind (scheme server port group message-id articles)
7993       (gnus-parse-news-url url)
7994     (cond
7995      (message-id
7996       (with-current-buffer gnus-summary-buffer
7997         (if server
7998             (let ((gnus-refer-article-method
7999                    (nconc (list (list 'nntp server))
8000                           gnus-refer-article-method))
8001                   (nntp-port-number (or port "nntp")))
8002               (gnus-message 7 "Fetching %s with %s"
8003                             message-id gnus-refer-article-method)
8004               (gnus-summary-refer-article message-id))
8005           (gnus-summary-refer-article message-id))))
8006      (group
8007       (gnus-button-fetch-group url)))))
8008
8009 (defun gnus-button-patch (library line)
8010   "Visit an Emacs Lisp library LIBRARY on line LINE."
8011   (interactive)
8012   (let ((file (locate-library (file-name-nondirectory library))))
8013     (unless file
8014       (error "Couldn't find library %s" library))
8015     (find-file file)
8016     (goto-char (point-min))
8017     (forward-line (1- (string-to-number line)))))
8018
8019 (defun gnus-button-handle-man (url)
8020   "Fetch a man page."
8021   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8022   (when (eq gnus-button-man-handler 'woman)
8023     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8024   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8025   (funcall gnus-button-man-handler url))
8026
8027 (defun gnus-button-handle-info-url (url)
8028   "Fetch an info URL."
8029   (setq url (mm-subst-char-in-string ?+ ?\  url))
8030   (cond
8031    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8032     (gnus-info-find-node
8033      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8034                      "Gnus")
8035              ")" (gnus-url-unhex-string (match-string 2 url)))))
8036    ((string-match "([^)\"]+)[^\"]+" url)
8037     (setq url
8038           (gnus-replace-in-string
8039            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8040     (gnus-info-find-node url))
8041    (t (error "Can't parse %s" url))))
8042
8043 (defun gnus-button-handle-info-url-gnome (url)
8044   "Fetch GNOME style info URL."
8045   (setq url (mm-subst-char-in-string ?_ ?\  url))
8046   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8047       (gnus-info-find-node
8048        (concat "("
8049                (gnus-url-unhex-string
8050                  (match-string 1 url))
8051                ")"
8052                (or (gnus-url-unhex-string
8053                     (match-string 2 url))
8054                    "Top")))
8055     (error "Can't parse %s" url)))
8056
8057 (defun gnus-button-handle-info-url-kde (url)
8058   "Fetch KDE style info URL."
8059   (gnus-info-find-node (gnus-url-unhex-string url)))
8060
8061 ;; (info) will autoload info.el
8062 (declare-function Info-menu "info" (menu-item &optional fork))
8063 (declare-function Info-index-next "info" (num))
8064
8065 (defun gnus-button-handle-info-keystrokes (url)
8066   "Call `info' when pushing the corresponding URL button."
8067   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8068   (let (node indx comma)
8069     (if (string-match
8070          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8071                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8072                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET"
8073                  "\\(?:[ \t\n,]*\\)\\)?")
8074          url)
8075         (setq node (match-string 2 url)
8076               indx (match-string 3 url))
8077       (error "Can't parse %s" url))
8078     (info)
8079     (Info-directory)
8080     (Info-menu node)
8081     (when (> (length indx) 0)
8082       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8083                             "\\([ \t\n,]*\\)")
8084                     indx)
8085       (setq comma (match-string 2 indx))
8086       (setq indx  (match-string 1 indx))
8087       (Info-index indx)
8088       (when comma
8089         (dotimes (i (with-temp-buffer
8090                       (insert comma)
8091                       ;; Note: the XEmacs version of `how-many' takes
8092                       ;; no optional argument.
8093                       (goto-char (point-min))
8094                       (how-many ",")))
8095           (Info-index-next 1)))
8096       nil)))
8097
8098 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8099 (declare-function pgg-display-output-buffer "pgg" (start end status))
8100
8101 (defun gnus-button-openpgp (url)
8102   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8103   (with-temp-buffer
8104     (mm-url-insert-file-contents-external url)
8105     (pgg-snarf-keys-region (point-min) (point-max))
8106     (pgg-display-output-buffer nil nil nil)))
8107
8108 (defun gnus-button-message-id (message-id)
8109   "Fetch MESSAGE-ID."
8110   (with-current-buffer gnus-summary-buffer
8111     (gnus-summary-refer-article message-id)))
8112
8113 (defun gnus-button-fetch-group (address &rest ignore)
8114   "Fetch GROUP specified by ADDRESS."
8115   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8116                       address)
8117     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8118     ;; for nntp:// and news://
8119     (setq address (match-string 3 address)))
8120   (if (not (string-match "[:/]" address))
8121       ;; This is just a simple group url.
8122       (gnus-group-read-ephemeral-group address gnus-select-method)
8123     (if (not
8124          (string-match
8125           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8126           address))
8127         (error "Can't parse %s" address)
8128       (gnus-group-read-ephemeral-group
8129        (match-string 4 address)
8130        `(nntp ,(match-string 1 address)
8131               (nntp-address ,(match-string 1 address))
8132               (nntp-port-number ,(if (match-end 3)
8133                                      (match-string 3 address)
8134                                    "nntp")))
8135        nil nil nil
8136        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8137
8138 (defun gnus-url-parse-query-string (query &optional downcase)
8139   (let (retval pairs cur key val)
8140     (setq pairs (split-string query "&"))
8141     (while pairs
8142       (setq cur (car pairs)
8143             pairs (cdr pairs))
8144       (if (not (string-match "=" cur))
8145           nil                           ; Grace
8146         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8147               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8148         (if downcase
8149             (setq key (downcase key)))
8150         (setq cur (assoc key retval))
8151         (if cur
8152             (setcdr cur (cons val (cdr cur)))
8153           (setq retval (cons (list key val) retval)))))
8154     retval))
8155
8156 (defun gnus-url-mailto (url)
8157   ;; Send mail to someone
8158   (when (string-match "mailto:/*\\(.*\\)" url)
8159     (setq url (substring url (match-beginning 1) nil)))
8160   (let (to args subject func)
8161     (setq args (gnus-url-parse-query-string
8162                 (if (string-match "^\\?" url)
8163                     (substring url 1)
8164                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8165                       (concat "to=" (match-string 1 url) "&"
8166                               (match-string 2 url))
8167                     (concat "to=" url)))
8168                 t)
8169           subject (cdr-safe (assoc "subject" args)))
8170     (gnus-msg-mail)
8171     (while args
8172       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8173       (if (fboundp func)
8174           (funcall func)
8175         (message-position-on-field (caar args)))
8176       (insert (gnus-replace-in-string
8177                (mapconcat 'identity (reverse (cdar args)) ", ")
8178                "\r\n" "\n" t))
8179       (setq args (cdr args)))
8180     (if subject
8181         (message-goto-body)
8182       (message-goto-subject))))
8183
8184 (defun gnus-button-embedded-url (address)
8185   "Activate ADDRESS with `browse-url'."
8186   (browse-url (gnus-strip-whitespace address)))
8187
8188 ;;; Next/prev buttons in the article buffer.
8189
8190 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8191 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8192
8193 (defvar gnus-prev-page-map
8194   (let ((map (make-sparse-keymap)))
8195     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8196     (define-key map "\r" 'gnus-button-prev-page)
8197     map))
8198
8199 (defvar gnus-next-page-map
8200   (let ((map (make-sparse-keymap)))
8201     (unless (>= emacs-major-version 21)
8202       ;; XEmacs doesn't care.
8203       (set-keymap-parent map gnus-article-mode-map))
8204     (define-key map gnus-mouse-2 'gnus-button-next-page)
8205     (define-key map "\r" 'gnus-button-next-page)
8206     map))
8207
8208 (defun gnus-insert-prev-page-button ()
8209   (let ((b (point)) e
8210         (inhibit-read-only t))
8211     (gnus-eval-format
8212      gnus-prev-page-line-format nil
8213      `(keymap ,gnus-prev-page-map
8214               gnus-prev t
8215               gnus-callback gnus-article-button-prev-page
8216               article-type annotation))
8217     (setq e (if (bolp)
8218                 ;; Exclude a newline.
8219                 (1- (point))
8220               (point)))
8221     (when gnus-article-button-face
8222       (gnus-overlay-put (gnus-make-overlay b e nil t)
8223                         'face gnus-article-button-face))
8224     (widget-convert-button
8225      'link b e
8226      :action 'gnus-button-prev-page
8227      :button-keymap gnus-prev-page-map)))
8228
8229 (defun gnus-button-next-page (&optional args more-args)
8230   "Go to the next page."
8231   (interactive)
8232   (let ((win (selected-window)))
8233     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8234     (gnus-article-next-page)
8235     (select-window win)))
8236
8237 (defun gnus-button-prev-page (&optional args more-args)
8238   "Go to the prev page."
8239   (interactive)
8240   (let ((win (selected-window)))
8241     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8242     (gnus-article-prev-page)
8243     (select-window win)))
8244
8245 (defun gnus-insert-next-page-button ()
8246   (let ((b (point)) e
8247         (inhibit-read-only t))
8248     (gnus-eval-format gnus-next-page-line-format nil
8249                       `(keymap ,gnus-next-page-map
8250                                gnus-next t
8251                                gnus-callback gnus-article-button-next-page
8252                                article-type annotation))
8253     (setq e (if (bolp)
8254                 ;; Exclude a newline.
8255                 (1- (point))
8256               (point)))
8257     (when gnus-article-button-face
8258       (gnus-overlay-put (gnus-make-overlay b e nil t)
8259                         'face gnus-article-button-face))
8260     (widget-convert-button
8261      'link b e
8262      :action 'gnus-button-next-page
8263      :button-keymap gnus-next-page-map)))
8264
8265 (defun gnus-article-button-next-page (arg)
8266   "Go to the next page."
8267   (interactive "P")
8268   (let ((win (selected-window)))
8269     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8270     (gnus-article-next-page)
8271     (select-window win)))
8272
8273 (defun gnus-article-button-prev-page (arg)
8274   "Go to the prev page."
8275   (interactive "P")
8276   (let ((win (selected-window)))
8277     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8278     (gnus-article-prev-page)
8279     (select-window win)))
8280
8281 (defvar gnus-decode-header-methods
8282   '(mail-decode-encoded-word-region)
8283   "List of methods used to decode headers.
8284
8285 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8286 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8287 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8288 whose names match REGEXP.
8289
8290 For example:
8291 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8292  mail-decode-encoded-word-region
8293  (\"chinese\" . rfc1843-decode-region))
8294 ")
8295
8296 (defvar gnus-decode-header-methods-cache nil)
8297
8298 (defun gnus-multi-decode-header (start end)
8299   "Apply the functions from `gnus-encoded-word-methods' that match."
8300   (unless (and gnus-decode-header-methods-cache
8301                (eq gnus-newsgroup-name
8302                    (car gnus-decode-header-methods-cache)))
8303     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8304     (dolist (x gnus-decode-header-methods)
8305       (if (symbolp x)
8306           (nconc gnus-decode-header-methods-cache (list x))
8307         (if (and gnus-newsgroup-name
8308                  (string-match (car x) gnus-newsgroup-name))
8309             (nconc gnus-decode-header-methods-cache
8310                    (list (cdr x)))))))
8311   (let ((xlist gnus-decode-header-methods-cache))
8312     (pop xlist)
8313     (save-restriction
8314       (narrow-to-region start end)
8315       (while xlist
8316         (funcall (pop xlist) (point-min) (point-max))))))
8317
8318 ;;;
8319 ;;; Treatment top-level handling.
8320 ;;;
8321
8322 (defun gnus-treat-article (condition &optional part-number total-parts type)
8323   (let ((length (- (point-max) (point-min)))
8324         (alist gnus-treatment-function-alist)
8325         (article-goto-body-goes-to-point-min-p t)
8326         (treated-type
8327          (or (not type)
8328              (catch 'found
8329                (let ((list gnus-article-treat-types))
8330                  (while list
8331                    (when (string-match (pop list) type)
8332                      (throw 'found t)))))))
8333         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8334         val elem)
8335     (gnus-run-hooks 'gnus-part-display-hook)
8336     (dolist (elem alist)
8337       (setq val
8338             (save-excursion
8339               (when (gnus-buffer-live-p gnus-summary-buffer)
8340                 (set-buffer gnus-summary-buffer))
8341               (symbol-value (car elem))))
8342       (when (and (or (consp val)
8343                      treated-type)
8344                  (gnus-treat-predicate val)
8345                  (or (not (get (car elem) 'highlight))
8346                      highlightp))
8347         (save-restriction
8348           (funcall (cadr elem)))))))
8349
8350 ;; Dynamic variables.
8351 (defvar part-number)
8352 (defvar total-parts)
8353 (defvar type)
8354 (defvar condition)
8355 (defvar length)
8356
8357 (defun gnus-treat-predicate (val)
8358   (cond
8359    ((null val)
8360     nil)
8361    (condition
8362     (eq condition val))
8363    ((and (listp val)
8364          (stringp (car val)))
8365     (apply 'gnus-or (mapcar `(lambda (s)
8366                                (string-match s ,(or gnus-newsgroup-name "")))
8367                             val)))
8368    ((listp val)
8369     (let ((pred (pop val)))
8370       (cond
8371        ((eq pred 'or)
8372         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8373        ((eq pred 'and)
8374         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8375        ((eq pred 'not)
8376         (not (gnus-treat-predicate (car val))))
8377        ((eq pred 'typep)
8378         (equal (car val) type))
8379        (t
8380         (error "%S is not a valid predicate" pred)))))
8381    ((eq val t)
8382     t)
8383    ((eq val 'head)
8384     nil)
8385    ((eq val 'first)
8386     (eq part-number 1))
8387    ((eq val 'last)
8388     (eq part-number total-parts))
8389    ((numberp val)
8390     (< length val))
8391    (t
8392     (error "%S is not a valid value" val))))
8393
8394 (defun gnus-article-encrypt-body (protocol &optional n)
8395   "Encrypt the article body."
8396   (interactive
8397    (list
8398     (or gnus-article-encrypt-protocol
8399         (completing-read "Encrypt protocol: "
8400                          gnus-article-encrypt-protocol-alist
8401                          nil t))
8402     current-prefix-arg))
8403   ;; User might hit `K E' instead of `K e', so prompt once.
8404   (when (and gnus-article-encrypt-protocol
8405              gnus-novice-user)
8406     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8407       (error "Encrypt aborted")))
8408   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8409     (unless func
8410       (error "Can't find the encrypt protocol %s" protocol))
8411     (if (member gnus-newsgroup-name '("nndraft:delayed"
8412                                       "nndraft:drafts"
8413                                       "nndraft:queue"))
8414         (error "Can't encrypt the article in group %s"
8415                gnus-newsgroup-name))
8416     (gnus-summary-iterate n
8417       (with-current-buffer gnus-summary-buffer
8418         (let ((mail-parse-charset gnus-newsgroup-charset)
8419               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8420               (summary-buffer gnus-summary-buffer)
8421               references point)
8422           (gnus-set-global-variables)
8423           (when (gnus-group-read-only-p)
8424             (error "The current newsgroup does not support article encrypt"))
8425           (gnus-summary-show-article t)
8426           (setq references
8427               (or (mail-header-references gnus-current-headers) ""))
8428           (set-buffer gnus-article-buffer)
8429           (let* ((inhibit-read-only t)
8430                  (headers
8431                   (mapcar (lambda (field)
8432                             (and (save-restriction
8433                                    (message-narrow-to-head)
8434                                    (goto-char (point-min))
8435                                    (search-forward field nil t))
8436                                  (prog2
8437                                      (message-narrow-to-field)
8438                                      (buffer-string)
8439                                    (delete-region (point-min) (point-max))
8440                                    (widen))))
8441                           '("Content-Type:" "Content-Transfer-Encoding:"
8442                             "Content-Disposition:"))))
8443             (message-narrow-to-head)
8444             (message-remove-header "MIME-Version")
8445             (goto-char (point-max))
8446             (setq point (point))
8447             (insert (apply 'concat headers))
8448             (widen)
8449             (narrow-to-region point (point-max))
8450             (let ((message-options message-options))
8451               (message-options-set 'message-sender user-mail-address)
8452               (message-options-set 'message-recipients user-mail-address)
8453               (message-options-set 'message-sign-encrypt 'not)
8454               (funcall func))
8455             (goto-char (point-min))
8456             (insert "MIME-Version: 1.0\n")
8457             (widen)
8458             (gnus-summary-edit-article-done
8459              references nil summary-buffer t))
8460           (when gnus-keep-backlog
8461             (gnus-backlog-remove-article
8462              (car gnus-article-current) (cdr gnus-article-current)))
8463           (gnus-flush-original-article-buffer)
8464           (when gnus-use-cache
8465             (gnus-cache-update-article
8466              (car gnus-article-current) (cdr gnus-article-current))))))))
8467
8468 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8469   "The following specs can be used:
8470 %t  The security MIME type
8471 %i  Additional info
8472 %d  Details
8473 %D  Details if button is pressed")
8474
8475 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8476   "The following specs can be used:
8477 %t  The security MIME type
8478 %i  Additional info
8479 %d  Details
8480 %D  Details if button is pressed")
8481
8482 (defvar gnus-mime-security-button-line-format-alist
8483   '((?t gnus-tmp-type ?s)
8484     (?i gnus-tmp-info ?s)
8485     (?d gnus-tmp-details ?s)
8486     (?D gnus-tmp-pressed-details ?s)))
8487
8488 (defvar gnus-mime-security-button-commands
8489   '((gnus-article-press-button "\r" "Show Detail")
8490     (undefined "v")
8491     (undefined "t")
8492     (undefined "C")
8493     (gnus-mime-security-save-part "o" "Save...")
8494     (undefined "\C-o")
8495     (undefined "r")
8496     (undefined "d")
8497     (undefined "c")
8498     (undefined "i")
8499     (undefined "E")
8500     (undefined "e")
8501     (undefined "p")
8502     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8503     (undefined ".")))
8504
8505 (defvar gnus-mime-security-button-map
8506   (let ((map (make-sparse-keymap)))
8507     (define-key map gnus-mouse-2 'gnus-article-push-button)
8508     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8509     (dolist (c gnus-mime-security-button-commands)
8510       (define-key map (cadr c) (car c)))
8511     map))
8512
8513 (easy-menu-define
8514   gnus-mime-security-button-menu gnus-mime-security-button-map
8515   "Security button menu."
8516   `("Security Part"
8517     ,@(delq nil
8518             (mapcar (lambda (c)
8519                       (unless (eq (car c) 'undefined)
8520                         (vector (caddr c) (car c) :active t)))
8521                     gnus-mime-security-button-commands))))
8522
8523 (defun gnus-mime-security-button-menu (event prefix)
8524   "Construct a context-sensitive menu of security commands."
8525   (interactive "e\nP")
8526   (save-window-excursion
8527     (let ((pos (event-start event)))
8528       (select-window (posn-window pos))
8529       (goto-char (posn-point pos))
8530       (gnus-article-check-buffer)
8531       (popup-menu gnus-mime-security-button-menu nil prefix))))
8532
8533 (defvar gnus-mime-security-details-buffer nil)
8534
8535 (defvar gnus-mime-security-button-pressed nil)
8536
8537 (defvar gnus-mime-security-show-details-inline t
8538   "If non-nil, show details in the article buffer.")
8539
8540 (defun gnus-mime-security-verify-or-decrypt (handle)
8541   (mm-remove-parts (cdr handle))
8542   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8543         point (inhibit-read-only t))
8544     (if region
8545         (goto-char (car region)))
8546     (setq point (point))
8547     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8548       (let* ((mm-verify-option 'known)
8549              (mm-decrypt-option 'known)
8550              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8551         (unless (eq nparts (cdr handle))
8552           (mm-destroy-parts (cdr handle))
8553           (setcdr handle nparts))))
8554     (gnus-mime-display-security handle)
8555     (when region
8556       (delete-region (point) (cdr region))
8557       (set-marker (car region) nil)
8558       (set-marker (cdr region) nil))
8559     (goto-char point)))
8560
8561 (defun gnus-mime-security-show-details (handle)
8562   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8563     (if (not details)
8564         (gnus-message 5 "No details.")
8565       (if gnus-mime-security-show-details-inline
8566           (let ((gnus-mime-security-button-pressed
8567                  (not (get-text-property (point) 'gnus-mime-details)))
8568                 (gnus-mime-security-button-line-format
8569                  (get-text-property (point) 'gnus-line-format))
8570                 (inhibit-read-only t))
8571             (forward-char -1)
8572             (while (eq (get-text-property (point) 'gnus-line-format)
8573                        gnus-mime-security-button-line-format)
8574               (forward-char -1))
8575             (forward-char)
8576             (save-restriction
8577               (narrow-to-region (point) (point))
8578               (gnus-insert-mime-security-button handle))
8579             (delete-region (point)
8580                            (or (text-property-not-all
8581                                 (point) (point-max)
8582                                 'gnus-line-format
8583                                 gnus-mime-security-button-line-format)
8584                                (point-max))))
8585         ;; Not inlined.
8586         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8587             (with-current-buffer gnus-mime-security-details-buffer
8588               (erase-buffer)
8589               t)
8590           (setq gnus-mime-security-details-buffer
8591                 (gnus-get-buffer-create "*MIME Security Details*")))
8592         (with-current-buffer gnus-mime-security-details-buffer
8593           (insert details)
8594           (goto-char (point-min)))
8595         (pop-to-buffer gnus-mime-security-details-buffer)))))
8596
8597 (defun gnus-mime-security-press-button (handle)
8598   (save-excursion
8599     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8600         (gnus-mime-security-show-details handle)
8601       (gnus-mime-security-verify-or-decrypt handle))))
8602
8603 (defun gnus-insert-mime-security-button (handle &optional displayed)
8604   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8605          (gnus-tmp-type
8606           (concat
8607            (or (nth 2 (assoc protocol mm-verify-function-alist))
8608                (nth 2 (assoc protocol mm-decrypt-function-alist))
8609                "Unknown")
8610            (if (equal (car handle) "multipart/signed")
8611                " Signed" " Encrypted")
8612            " Part"))
8613          (gnus-tmp-info
8614           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8615               "Undecided"))
8616          (gnus-tmp-details
8617           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8618          gnus-tmp-pressed-details
8619          b e)
8620     (setq gnus-tmp-details
8621           (if gnus-tmp-details
8622               (concat "\n" gnus-tmp-details)
8623             ""))
8624     (setq gnus-tmp-pressed-details
8625           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8626     (unless (bolp)
8627       (insert "\n"))
8628     (setq b (point))
8629     (gnus-eval-format
8630      gnus-mime-security-button-line-format
8631      gnus-mime-security-button-line-format-alist
8632      `(keymap ,gnus-mime-security-button-map
8633          gnus-callback gnus-mime-security-press-button
8634          gnus-line-format ,gnus-mime-security-button-line-format
8635          gnus-mime-details ,gnus-mime-security-button-pressed
8636          article-type annotation
8637          gnus-data ,handle))
8638     (setq e (if (bolp)
8639                 ;; Exclude a newline.
8640                 (1- (point))
8641               (point)))
8642     (when gnus-article-button-face
8643       (gnus-overlay-put (gnus-make-overlay b e nil t)
8644                         'face gnus-article-button-face))
8645     (widget-convert-button
8646      'link b e
8647      :mime-handle handle
8648      :action 'gnus-widget-press-button
8649      :button-keymap gnus-mime-security-button-map
8650      :help-echo
8651      (lambda (widget)
8652        ;; Needed to properly clear the message due to a bug in
8653        ;; wid-edit (XEmacs only).
8654        (when (boundp 'help-echo-owns-message)
8655          (setq help-echo-owns-message t))
8656        (format
8657         "%S: show detail; %S: more options"
8658         (aref gnus-mouse-2 0)
8659         (aref gnus-down-mouse-3 0))))))
8660
8661 (defun gnus-mime-display-security (handle)
8662   (save-restriction
8663     (narrow-to-region (point) (point))
8664     (unless (gnus-unbuttonized-mime-type-p (car handle))
8665       (gnus-insert-mime-security-button handle))
8666     (gnus-mime-display-part (cadr handle))
8667     (unless (bolp)
8668       (insert "\n"))
8669     (unless (gnus-unbuttonized-mime-type-p (car handle))
8670       (let ((gnus-mime-security-button-line-format
8671              gnus-mime-security-button-end-line-format))
8672         (gnus-insert-mime-security-button handle)))
8673     (mm-set-handle-multipart-parameter
8674      handle 'gnus-region
8675      (cons (set-marker (make-marker) (point-min))
8676            (set-marker (make-marker) (point-max))))
8677     (goto-char (point-max))))
8678
8679 (defun gnus-mime-security-run-function (function)
8680   "Run FUNCTION with the security part under point."
8681   (gnus-article-check-buffer)
8682   (let ((data (get-text-property (point) 'gnus-data))
8683         buffer handle)
8684     (when (and (stringp (car-safe data))
8685                (setq buffer (mm-handle-multipart-original-buffer data))
8686                (setq handle (cadr data)))
8687       (if (bufferp (mm-handle-buffer handle))
8688           (progn
8689             (setq handle (cons buffer (copy-sequence (cdr handle))))
8690             (mm-handle-set-undisplayer handle nil))
8691         (setq handle (mm-make-handle
8692                       buffer
8693                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8694                       nil nil nil nil nil nil)))
8695       (funcall function handle))))
8696
8697 (defun gnus-mime-security-save-part ()
8698   "Save the security part under point."
8699   (interactive)
8700   (gnus-mime-security-run-function 'mm-save-part))
8701
8702 (defun gnus-mime-security-pipe-part ()
8703   "Pipe the security part under point to a process."
8704   (interactive)
8705   (gnus-mime-security-run-function 'mm-pipe-part))
8706
8707 (gnus-ems-redefine)
8708
8709 (provide 'gnus-art)
8710
8711 (run-hooks 'gnus-art-load-hook)
8712
8713 ;;; gnus-art.el ends here