Pass proper format strings to gnus-message.
[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 ;; Avoid the "Recursive load suspected" error in Emacs 21.1.
38 (eval-and-compile
39   (let ((recursive-load-depth-limit 100))
40     (require 'gnus-sum)))
41 (require 'gnus-spec)
42 (require 'gnus-int)
43 (require 'gnus-win)
44 (require 'mm-bodies)
45 (require 'mail-parse)
46 (require 'mm-decode)
47 (require 'mm-view)
48 (require 'wid-edit)
49 (require 'mm-uu)
50 (require 'message)
51
52 (autoload 'gnus-msg-mail "gnus-msg" nil t)
53 (autoload 'gnus-button-mailto "gnus-msg")
54 (autoload 'gnus-button-reply "gnus-msg" nil t)
55 (autoload 'parse-time-string "parse-time" nil nil)
56 (autoload 'ansi-color-apply-on-region "ansi-color")
57 (autoload 'mm-url-insert-file-contents-external "mm-url")
58 (autoload 'mm-extern-cache-contents "mm-extern")
59
60 (defgroup gnus-article nil
61   "Article display."
62   :link '(custom-manual "(gnus)Article Buffer")
63   :group 'gnus)
64
65 (defgroup gnus-article-treat nil
66   "Treating article parts."
67   :link '(custom-manual "(gnus)Article Hiding")
68   :group 'gnus-article)
69
70 (defgroup gnus-article-hiding nil
71   "Hiding article parts."
72   :link '(custom-manual "(gnus)Article Hiding")
73   :group 'gnus-article)
74
75 (defgroup gnus-article-highlight nil
76   "Article highlighting."
77   :link '(custom-manual "(gnus)Article Highlighting")
78   :group 'gnus-article
79   :group 'gnus-visual)
80
81 (defgroup gnus-article-signature nil
82   "Article signatures."
83   :link '(custom-manual "(gnus)Article Signature")
84   :group 'gnus-article)
85
86 (defgroup gnus-article-headers nil
87   "Article headers."
88   :link '(custom-manual "(gnus)Hiding Headers")
89   :group 'gnus-article)
90
91 (defgroup gnus-article-washing nil
92   "Special commands on articles."
93   :link '(custom-manual "(gnus)Article Washing")
94   :group 'gnus-article)
95
96 (defgroup gnus-article-emphasis nil
97   "Fontisizing articles."
98   :link '(custom-manual "(gnus)Article Fontisizing")
99   :group 'gnus-article)
100
101 (defgroup gnus-article-saving nil
102   "Saving articles."
103   :link '(custom-manual "(gnus)Saving Articles")
104   :group 'gnus-article)
105
106 (defgroup gnus-article-mime nil
107   "Worshiping the MIME wonder."
108   :link '(custom-manual "(gnus)Using MIME")
109   :group 'gnus-article)
110
111 (defgroup gnus-article-buttons nil
112   "Pushable buttons in the article buffer."
113   :link '(custom-manual "(gnus)Article Buttons")
114   :group 'gnus-article)
115
116 (defgroup gnus-article-various nil
117   "Other article options."
118   :link '(custom-manual "(gnus)Misc Article")
119   :group 'gnus-article)
120
121 (defcustom gnus-ignored-headers
122   (mapcar
123    (lambda (header)
124      (concat "^" header ":"))
125    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
126      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
127      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
128      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
129      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
130      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
131      "X-Attribution" "X-Originating-IP" "Delivered-To"
132      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
133      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
134      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
135      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
136      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
137      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
138      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
139      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
140      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
141      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
142      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
143      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
144      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
145      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
146      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
147      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
148      "List-[A-Za-z]+" "X-Listprocessor-Version"
149      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
150      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
151      "X-Received" "Content-length" "X-precedence"
152      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
153      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
154      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
155      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
156      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
157      "X-Content-length" "X-Posting-Agent" "Original-Received"
158      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
159      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
160      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
161      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
162      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
163      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
164      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
165      "Envelope-Sender" "Envelope-Recipients"))
166   "*All headers that start with this regexp will be hidden.
167 This variable can also be a list of regexps of headers to be ignored.
168 If `gnus-visible-headers' is non-nil, this variable will be ignored."
169   :type '(choice :custom-show nil
170                  regexp
171                  (repeat regexp))
172   :group 'gnus-article-hiding)
173
174 (defcustom gnus-visible-headers
175   "^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:"
176   "*All headers that do not match this regexp will be hidden.
177 This variable can also be a list of regexp of headers to remain visible.
178 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
179   :type '(choice
180           (repeat :value-to-internal (lambda (widget value)
181                                        (custom-split-regexp-maybe value))
182                   :match (lambda (widget value)
183                            (or (stringp value)
184                                (widget-editable-list-match widget value)))
185                   regexp)
186           (const :tag "Use gnus-ignored-headers" nil)
187           regexp)
188   :group 'gnus-article-hiding)
189
190 (defcustom gnus-sorted-header-list
191   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
192     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
193   "*This variable is a list of regular expressions.
194 If it is non-nil, headers that match the regular expressions will
195 be placed first in the article buffer in the sequence specified by
196 this list."
197   :type '(repeat regexp)
198   :group 'gnus-article-hiding)
199
200 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
201   "Headers that are only to be displayed if they have interesting data.
202 Possible values in this list are:
203
204   'empty       Headers with no content.
205   'newsgroups  Newsgroup identical to Gnus group.
206   'to-address  To identical to To-address.
207   'to-list     To identical to To-list.
208   'cc-list     CC identical to To-list.
209   'followup-to Followup-to identical to Newsgroups.
210   'reply-to    Reply-to identical to From.
211   'date        Date less than four days old.
212   'long-to     To and/or Cc longer than 1024 characters.
213   'many-to     Multiple To and/or Cc."
214   :type '(set (const :tag "Headers with no content." empty)
215               (const :tag "Newsgroups identical to Gnus group." newsgroups)
216               (const :tag "To identical to To-address." to-address)
217               (const :tag "To identical to To-list." to-list)
218               (const :tag "CC identical to To-list." cc-list)
219               (const :tag "Followup-to identical to Newsgroups." followup-to)
220               (const :tag "Reply-to identical to From." reply-to)
221               (const :tag "Date less than four days old." date)
222               (const :tag "To and/or Cc longer than 1024 characters." long-to)
223               (const :tag "Multiple To and/or Cc headers." many-to))
224   :group 'gnus-article-hiding)
225
226 (defcustom gnus-article-skip-boring nil
227   "Skip over text that is not worth reading.
228 By default, if you set this t, then Gnus will display citations and
229 signatures, but will never scroll down to show you a page consisting
230 only of boring text.  Boring text is controlled by
231 `gnus-article-boring-faces'."
232   :version "22.1"
233   :type 'boolean
234   :group 'gnus-article-hiding)
235
236 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
237   "Regexp matching signature separator.
238 This can also be a list of regexps.  In that case, it will be checked
239 from head to tail looking for a separator.  Searches will be done from
240 the end of the buffer."
241   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
242                  (regexp)
243                  (repeat :tag "List of regexp" regexp))
244   :group 'gnus-article-signature)
245
246 (defcustom gnus-signature-limit nil
247   "Provide a limit to what is considered a signature.
248 If it is a number, no signature may not be longer (in characters) than
249 that number.  If it is a floating point number, no signature may be
250 longer (in lines) than that number.  If it is a function, the function
251 will be called without any parameters, and if it returns nil, there is
252 no signature in the buffer.  If it is a string, it will be used as a
253 regexp.  If it matches, the text in question is not a signature.
254
255 This can also be a list of the above values."
256   :type '(choice (const nil)
257                  (integer :value 200)
258                  (number :value 4.0)
259                  function
260                  (regexp :value ".*"))
261   :group 'gnus-article-signature)
262
263 (defcustom gnus-hidden-properties '(invisible t intangible t)
264   "Property list to use for hiding text."
265   :type 'sexp
266   :group 'gnus-article-hiding)
267
268 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
269 ;; frames in a session.
270 (defcustom gnus-article-x-face-command
271   (if (featurep 'xemacs)
272       (if (or (gnus-image-type-available-p 'xface)
273               (gnus-image-type-available-p 'pbm))
274           'gnus-display-x-face-in-from
275         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
276     (if (gnus-image-type-available-p 'pbm)
277         'gnus-display-x-face-in-from
278       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
279 display -"))
280   "*String or function to be executed to display an X-Face header.
281 If it is a string, the command will be executed in a sub-shell
282 asynchronously.  The compressed face will be piped to this command."
283   :type `(choice string
284                  (function-item gnus-display-x-face-in-from)
285                  function)
286   :version "21.1"
287   :group 'gnus-picon
288   :group 'gnus-article-washing)
289
290 (defcustom gnus-article-x-face-too-ugly nil
291   "Regexp matching posters whose face shouldn't be shown automatically."
292   :type '(choice regexp (const nil))
293   :group 'gnus-article-washing)
294
295 (defcustom gnus-article-banner-alist nil
296   "Banner alist for stripping.
297 For example,
298      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
299   :version "21.1"
300   :type '(repeat (cons symbol regexp))
301   :group 'gnus-article-washing)
302
303 (gnus-define-group-parameter
304  banner
305  :variable-document
306  "Alist of regexps (to match group names) and banner."
307  :variable-group gnus-article-washing
308  :parameter-type
309  '(choice :tag "Banner"
310           :value nil
311           (const :tag "Remove signature" signature)
312           (symbol :tag "Item in `gnus-article-banner-alist'" none)
313           regexp
314           (const :tag "None" nil))
315  :parameter-document
316  "If non-nil, specify how to remove `banners' from articles.
317
318 Symbol `signature' means to remove signatures delimited by
319 `gnus-signature-separator'.  Any other symbol is used to look up a
320 regular expression to match the banner in `gnus-article-banner-alist'.
321 A string is used as a regular expression to match the banner
322 directly.")
323
324 (defcustom gnus-article-address-banner-alist nil
325   "Alist of mail addresses and banners.
326 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
327 to match a mail address in the From: header, BANNER is one of a symbol
328 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
329 If ADDRESS matches author's mail address, it will remove things like
330 advertisements.  For example:
331
332 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
333 "
334   :type '(repeat
335           (cons
336            (regexp :tag "Address")
337            (choice :tag "Banner" :value nil
338                    (const :tag "Remove signature" signature)
339                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
340                    regexp
341                    (const :tag "None" nil))))
342   :version "22.1"
343   :group 'gnus-article-washing)
344
345 (defmacro gnus-emphasis-custom-with-format (&rest body)
346   `(let ((format "\
347 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
348 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
349      ,@body))
350
351 (defun gnus-emphasis-custom-value-to-external (value)
352   (gnus-emphasis-custom-with-format
353    (if (consp (car value))
354        (list (format format (car (car value)) (cdr (car value)))
355              2
356              (if (nth 1 value) 2 3)
357              (nth 2 value))
358      value)))
359
360 (defun gnus-emphasis-custom-value-to-internal (value)
361   (gnus-emphasis-custom-with-format
362    (let ((regexp (concat "\\`"
363                          (format (regexp-quote format)
364                                  "\\([^()]+\\)" "\\([^()]+\\)")
365                          "\\'"))
366          pattern)
367      (if (string-match regexp (setq pattern (car value)))
368          (list (cons (match-string 1 pattern) (match-string 2 pattern))
369                (= (nth 2 value) 2)
370                (nth 3 value))
371        value))))
372
373 (defcustom gnus-emphasis-alist
374   (let ((types
375          '(("\\*" "\\*" bold nil 2)
376            ("_" "_" underline)
377            ("/" "/" italic)
378            ("_/" "/_" underline-italic)
379            ("_\\*" "\\*_" underline-bold)
380            ("\\*/" "/\\*" bold-italic)
381            ("_\\*/" "/\\*_" underline-bold-italic))))
382     (nconc
383      (gnus-emphasis-custom-with-format
384       (mapcar (lambda (spec)
385                 (list (format format (car spec) (cadr spec))
386                       (or (nth 3 spec) 2)
387                       (or (nth 4 spec) 3)
388                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
389               types))
390      '(;; I've never seen anyone use this strikethru convention whereas I've
391        ;; several times seen it triggered by normal text.  --Stef
392        ;; Miles suggests that this form is sometimes used but for italics,
393        ;; so maybe we should map it to `italic'.
394        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
395        ;; 2 3 gnus-emphasis-strikethru)
396        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
397         2 3 gnus-emphasis-underline))))
398   "*Alist that says how to fontify certain phrases.
399 Each item looks like this:
400
401   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
402
403 The first element is a regular expression to be matched.  The second
404 is a number that says what regular expression grouping used to find
405 the entire emphasized word.  The third is a number that says what
406 regexp grouping should be displayed and highlighted.  The fourth
407 is the face used for highlighting."
408   :type
409   '(repeat
410     (menu-choice
411      :format "%[Customizing Style%]\n%v"
412      :indent 2
413      (group :tag "Default"
414             :value ("" 0 0 default)
415             :value-create
416             (lambda (widget)
417               (let ((value (widget-get
418                             (cadr (widget-get (widget-get widget :parent)
419                                               :args))
420                             :value)))
421                 (if (not (eq (nth 2 value) 'default))
422                     (widget-put
423                      widget
424                      :value
425                      (gnus-emphasis-custom-value-to-external value))))
426               (widget-group-value-create widget))
427             regexp
428             (integer :format "Match group: %v")
429             (integer :format "Emphasize group: %v")
430             face)
431      (group :tag "Simple"
432             :value (("_" . "_") nil default)
433             (cons :format "%v"
434                   (regexp :format "Start regexp: %v")
435                   (regexp :format "End regexp: %v"))
436             (boolean :format "Show start and end patterns: %[%v%]\n"
437                      :on " On " :off " Off ")
438             face)))
439   :get (lambda (symbol)
440          (mapcar 'gnus-emphasis-custom-value-to-internal
441                  (default-value symbol)))
442   :set (lambda (symbol value)
443          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
444                                      value)))
445   :group 'gnus-article-emphasis)
446
447 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
448   "A regexp to describe whitespace which should not be emphasized.
449 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
450 The former avoids underlining of leading and trailing whitespace,
451 and the latter avoids underlining any whitespace at all."
452   :version "21.1"
453   :group 'gnus-article-emphasis
454   :type 'regexp)
455
456 (defface gnus-emphasis-bold '((t (:bold t)))
457   "Face used for displaying strong emphasized text (*word*)."
458   :group 'gnus-article-emphasis)
459
460 (defface gnus-emphasis-italic '((t (:italic t)))
461   "Face used for displaying italic emphasized text (/word/)."
462   :group 'gnus-article-emphasis)
463
464 (defface gnus-emphasis-underline '((t (:underline t)))
465   "Face used for displaying underlined emphasized text (_word_)."
466   :group 'gnus-article-emphasis)
467
468 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
469   "Face used for displaying underlined bold emphasized text (_*word*_)."
470   :group 'gnus-article-emphasis)
471
472 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
473   "Face used for displaying underlined italic emphasized text (_/word/_)."
474   :group 'gnus-article-emphasis)
475
476 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
477   "Face used for displaying bold italic emphasized text (/*word*/)."
478   :group 'gnus-article-emphasis)
479
480 (defface gnus-emphasis-underline-bold-italic
481   '((t (:bold t :italic t :underline t)))
482   "Face used for displaying underlined bold italic emphasized text.
483 Example: (_/*word*/_)."
484   :group 'gnus-article-emphasis)
485
486 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
487                                       '((t (:strikethru t)))
488                                     '((t (:strike-through t))))
489   "Face used for displaying strike-through text (-word-)."
490   :group 'gnus-article-emphasis)
491
492 (defface gnus-emphasis-highlight-words
493   '((t (:background "black" :foreground "yellow")))
494   "Face used for displaying highlighted words."
495   :group 'gnus-article-emphasis)
496
497 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
498   "Format for display of Date headers in article bodies.
499 See `format-time-string' for the possible values.
500
501 The variable can also be function, which should return a complete Date
502 header.  The function is called with one argument, the time, which can
503 be fed to `format-time-string'."
504   :type '(choice string function)
505   :link '(custom-manual "(gnus)Article Date")
506   :group 'gnus-article-washing)
507
508 (defcustom gnus-save-all-headers t
509   "*If non-nil, don't remove any headers before saving.
510 This will be overridden by the `:headers' property that the symbol of
511 the saver function, which is specified by `gnus-default-article-saver',
512 might have."
513   :group 'gnus-article-saving
514   :type 'boolean)
515
516 (defcustom gnus-prompt-before-saving 'always
517   "*This variable says how much prompting is to be done when saving articles.
518 If it is nil, no prompting will be done, and the articles will be
519 saved to the default files.  If this variable is `always', each and
520 every article that is saved will be preceded by a prompt, even when
521 saving large batches of articles.  If this variable is neither nil not
522 `always', there the user will be prompted once for a file name for
523 each invocation of the saving commands."
524   :group 'gnus-article-saving
525   :type '(choice (item always)
526                  (item :tag "never" nil)
527                  (sexp :tag "once" :format "%t\n" :value t)))
528
529 (defcustom gnus-saved-headers gnus-visible-headers
530   "Headers to keep if `gnus-save-all-headers' is nil.
531 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
532 If that variable is nil, however, all headers that match this regexp
533 will be kept while the rest will be deleted before saving.  This and
534 `gnus-save-all-headers' will be overridden by the `:headers' property
535 that the symbol of the saver function, which is specified by
536 `gnus-default-article-saver', might have."
537   :group 'gnus-article-saving
538   :type 'regexp)
539
540 ;; Note that "Rmail format" is mbox since Emacs 23, but Babyl before.
541 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
542   "A function to save articles in your favourite format.
543 The function will be called by way of the `gnus-summary-save-article'
544 command, and friends such as `gnus-summary-save-article-rmail'.
545
546 Gnus provides the following functions:
547
548 * gnus-summary-save-in-rmail (Rmail format)
549 * gnus-summary-save-in-mail (Unix mail format)
550 * gnus-summary-save-in-folder (MH folder)
551 * gnus-summary-save-in-file (article format)
552 * gnus-summary-save-body-in-file (article body)
553 * gnus-summary-save-in-vm (use VM's folder format)
554 * gnus-summary-write-to-file (article format -- overwrite)
555 * gnus-summary-write-body-to-file (article body -- overwrite)
556 * gnus-summary-save-in-pipe (article format)
557
558 The symbol of each function may have the following properties:
559
560 * :decode
561 The value non-nil means save decoded articles.  This is meaningful
562 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
563 `gnus-summary-write-to-file', `gnus-summary-write-body-to-file', and
564 `gnus-summary-save-in-pipe'.
565
566 * :function
567 The value specifies an alternative function which appends, not
568 overwrites, articles to a file.  This implies that when saving many
569 articles at a time, `gnus-prompt-before-saving' is bound to t and all
570 articles are saved in a single file.  This is meaningful only with
571 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
572
573 * :headers
574 The value specifies the symbol of a variable of which the value
575 specifies headers to be saved.  If it is omitted,
576 `gnus-save-all-headers' and `gnus-saved-headers' control what
577 headers should be saved."
578   :group 'gnus-article-saving
579   :type '(radio (function-item gnus-summary-save-in-rmail)
580                 (function-item gnus-summary-save-in-mail)
581                 (function-item gnus-summary-save-in-folder)
582                 (function-item gnus-summary-save-in-file)
583                 (function-item gnus-summary-save-body-in-file)
584                 (function-item gnus-summary-save-in-vm)
585                 (function-item gnus-summary-write-to-file)
586                 (function-item gnus-summary-write-body-to-file)
587                 (function-item gnus-summary-save-in-pipe)
588                 (function)))
589
590 (defcustom gnus-article-save-coding-system
591   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
592       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
593       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
594       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
595   "Coding system used to save decoded articles to a file.
596
597 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
598 which can safely encode any characters in text.  This is used by the
599 commands including:
600
601 * gnus-summary-save-article-file
602 * gnus-summary-save-article-body-file
603 * gnus-summary-write-article-file
604 * gnus-summary-write-article-body-file
605
606 and the functions to which you may set `gnus-default-article-saver':
607
608 * gnus-summary-save-in-file
609 * gnus-summary-save-body-in-file
610 * gnus-summary-write-to-file
611 * gnus-summary-write-body-to-file
612
613 Those commands and functions save just text displayed in the article
614 buffer to a file if the value of this variable is non-nil.  Note that
615 buttonized MIME parts will be lost in a saved file in that case.
616 Otherwise, raw articles will be saved."
617   :group 'gnus-article-saving
618   :type `(choice
619           :format "%{%t%}:\n %[Value Menu%] %v"
620           (const :tag "Save raw articles" nil)
621           ,@(delq nil
622                   (mapcar
623                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
624                    '((const :tag "UTF-8" utf-8)
625                      (const :tag "iso-2022-7bit" iso-2022-7bit)
626                      (const :tag "Emacs internal" emacs-mule)
627                      (const :tag "escape-quoted" escape-quoted))))
628           (symbol :tag "Coding system")))
629
630 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
631   "A function generating a file name to save articles in Rmail format.
632 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
633   :group 'gnus-article-saving
634   :type 'function)
635
636 (defcustom gnus-mail-save-name 'gnus-plain-save-name
637   "A function generating a file name to save articles in Unix mail format.
638 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
639   :group 'gnus-article-saving
640   :type 'function)
641
642 (defcustom gnus-folder-save-name 'gnus-folder-save-name
643   "A function generating a file name to save articles in MH folder.
644 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
645   :group 'gnus-article-saving
646   :type 'function)
647
648 (defcustom gnus-file-save-name 'gnus-numeric-save-name
649   "A function generating a file name to save articles in article format.
650 The function is called with NEWSGROUP, HEADERS, and optional
651 LAST-FILE."
652   :group 'gnus-article-saving
653   :type 'function)
654
655 (defcustom gnus-split-methods
656   '((gnus-article-archive-name)
657     (gnus-article-nndoc-name))
658   "*Variable used to suggest where articles are to be saved.
659 For instance, if you would like to save articles related to Gnus in
660 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
661 you could set this variable to something like:
662
663  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
664    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
665
666 This variable is an alist where the key is the match and the
667 value is a list of possible files to save in if the match is
668 non-nil.
669
670 If the match is a string, it is used as a regexp match on the
671 article.  If the match is a symbol, that symbol will be funcalled
672 from the buffer of the article to be saved with the newsgroup as the
673 parameter.  If it is a list, it will be evaled in the same buffer.
674
675 If this form or function returns a string, this string will be used as a
676 possible file name; and if it returns a non-nil list, that list will be
677 used as possible file names."
678   :group 'gnus-article-saving
679   :type '(repeat (choice (list :value (fun) function)
680                          (cons :value ("" "") regexp (repeat string))
681                          (sexp :value nil))))
682
683 (defcustom gnus-page-delimiter "^\^L"
684   "*Regexp describing what to use as article page delimiters.
685 The default value is \"^\^L\", which is a form linefeed at the
686 beginning of a line."
687   :type 'regexp
688   :group 'gnus-article-various)
689
690 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
691   "*The format specification for the article mode line.
692 See `gnus-summary-mode-line-format' for a closer description.
693
694 The following additional specs are available:
695
696 %w  The article washing status.
697 %m  The number of MIME parts in the article."
698   :type 'string
699   :group 'gnus-article-various)
700
701 (defcustom gnus-article-mode-hook nil
702   "*A hook for Gnus article mode."
703   :type 'hook
704   :group 'gnus-article-various)
705
706 (when (featurep 'xemacs)
707   ;; Extracted from gnus-xmas-define in order to preserve user settings
708   (when (fboundp 'turn-off-scroll-in-place)
709     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
710   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
711   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
712
713 (defcustom gnus-article-menu-hook nil
714   "*Hook run after the creation of the article mode menu."
715   :type 'hook
716   :group 'gnus-article-various)
717
718 (defcustom gnus-article-prepare-hook nil
719   "*A hook called after an article has been prepared in the article buffer."
720   :type 'hook
721   :group 'gnus-article-various)
722
723 (defcustom gnus-copy-article-ignored-headers nil
724   "List of headers to be removed when copying an article.
725 Each element is a regular expression."
726   :version "23.1" ;; No Gnus
727   :type '(repeat regexp)
728   :group 'gnus-article-various)
729
730 (make-obsolete-variable 'gnus-article-hide-pgp-hook nil
731                         "Gnus 5.10 (Emacs-22.1)")
732
733 (defface gnus-button
734   '((t (:weight bold)))
735   "Face used for highlighting a button in the article buffer."
736   :group 'gnus-article-buttons)
737
738 (defcustom gnus-article-button-face 'gnus-button
739   "Face used for highlighting buttons in the article buffer.
740
741 An article button is a piece of text that you can activate by pressing
742 `RET' or `mouse-2' above it."
743   :type 'face
744   :group 'gnus-article-buttons)
745
746 (defcustom gnus-article-mouse-face 'highlight
747   "Face used for mouse highlighting in the article buffer.
748
749 Article buttons will be displayed in this face when the cursor is
750 above them."
751   :type 'face
752   :group 'gnus-article-buttons)
753
754 (defcustom gnus-signature-face 'gnus-signature
755   "Face used for highlighting a signature in the article buffer.
756 Obsolete; use the face `gnus-signature' for customizations instead."
757   :type 'face
758   :group 'gnus-article-highlight
759   :group 'gnus-article-signature)
760
761 (defface gnus-signature
762   '((t
763      (:italic t)))
764   "Face used for highlighting a signature in the article buffer."
765   :group 'gnus-article-highlight
766   :group 'gnus-article-signature)
767 ;; backward-compatibility alias
768 (put 'gnus-signature-face 'face-alias 'gnus-signature)
769 (put 'gnus-signature-face 'obsolete-face "22.1")
770
771 (defface gnus-header-from
772   '((((class color)
773       (background dark))
774      (:foreground "PaleGreen1"))
775     (((class color)
776       (background light))
777      (:foreground "red3"))
778     (t
779      (:italic t)))
780   "Face used for displaying from headers."
781   :group 'gnus-article-headers
782   :group 'gnus-article-highlight)
783 ;; backward-compatibility alias
784 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
785 (put 'gnus-header-from-face 'obsolete-face "22.1")
786
787 (defface gnus-header-subject
788   '((((class color)
789       (background dark))
790      (:foreground "SeaGreen1"))
791     (((class color)
792       (background light))
793      (:foreground "red4"))
794     (t
795      (:bold t :italic t)))
796   "Face used for displaying subject headers."
797   :group 'gnus-article-headers
798   :group 'gnus-article-highlight)
799 ;; backward-compatibility alias
800 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
801 (put 'gnus-header-subject-face 'obsolete-face "22.1")
802
803 (defface gnus-header-newsgroups
804   '((((class color)
805       (background dark))
806      (:foreground "yellow" :italic t))
807     (((class color)
808       (background light))
809      (:foreground "MidnightBlue" :italic t))
810     (t
811      (:italic t)))
812   "Face used for displaying newsgroups headers.
813 In the default setup this face is only used for crossposted
814 articles."
815   :group 'gnus-article-headers
816   :group 'gnus-article-highlight)
817 ;; backward-compatibility alias
818 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
819 (put 'gnus-header-newsgroups-face 'obsolete-face "22.1")
820
821 (defface gnus-header-name
822   '((((class color)
823       (background dark))
824      (:foreground "SpringGreen2"))
825     (((class color)
826       (background light))
827      (:foreground "maroon"))
828     (t
829      (:bold t)))
830   "Face used for displaying header names."
831   :group 'gnus-article-headers
832   :group 'gnus-article-highlight)
833 ;; backward-compatibility alias
834 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
835 (put 'gnus-header-name-face 'obsolete-face "22.1")
836
837 (defface gnus-header-content
838   '((((class color)
839       (background dark))
840      (:foreground "SpringGreen1" :italic t))
841     (((class color)
842       (background light))
843      (:foreground "indianred4" :italic t))
844     (t
845      (:italic t)))  "Face used for displaying header content."
846   :group 'gnus-article-headers
847   :group 'gnus-article-highlight)
848 ;; backward-compatibility alias
849 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
850 (put 'gnus-header-content-face 'obsolete-face "22.1")
851
852 (defcustom gnus-header-face-alist
853   '(("From" nil gnus-header-from)
854     ("Subject" nil gnus-header-subject)
855     ("Newsgroups:.*," nil gnus-header-newsgroups)
856     ("" gnus-header-name gnus-header-content))
857   "*Controls highlighting of article headers.
858
859 An alist of the form (HEADER NAME CONTENT).
860
861 HEADER is a regular expression which should match the name of a
862 header and NAME and CONTENT are either face names or nil.
863
864 The name of each header field will be displayed using the face
865 specified by the first element in the list where HEADER matches
866 the header name and NAME is non-nil.  Similarly, the content will
867 be displayed by the first non-nil matching CONTENT face."
868   :group 'gnus-article-headers
869   :group 'gnus-article-highlight
870   :type '(repeat (list (regexp :tag "Header")
871                        (choice :tag "Name"
872                                (item :tag "skip" nil)
873                                (face :value default))
874                        (choice :tag "Content"
875                                (item :tag "skip" nil)
876                                (face :value default)))))
877
878 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
879                                           '((xface . (:face gnus-x-face)))
880                                         '((pbm . (:face gnus-x-face))
881                                           (png . nil)))
882   "Alist of image types and properties applied to Face and X-Face images.
883 Here are examples:
884
885 ;; Specify the altitude of Face images in the From header.
886 \(setq gnus-face-properties-alist
887       '((pbm . (:face gnus-x-face :ascent 80))
888         (png . (:ascent 80))))
889
890 ;; Show Face images as pressed buttons.
891 \(setq gnus-face-properties-alist
892       '((pbm . (:face gnus-x-face :relief -2))
893         (png . (:relief -2))))
894
895 See the manual for the valid properties for various image types.
896 Currently, `pbm' is used for X-Face images and `png' is used for Face
897 images in Emacs.  Only the `:face' property is effective on the `xface'
898 image type in XEmacs if it is built with the libcompface library."
899   :version "23.1" ;; No Gnus
900   :group 'gnus-article-headers
901   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
902
903 (defcustom gnus-article-decode-hook
904   '(article-decode-charset article-decode-encoded-words
905                            article-decode-group-name article-decode-idna-rhs)
906   "*Hook run to decode charsets in articles."
907   :group 'gnus-article-headers
908   :type 'hook)
909
910 (defcustom gnus-display-mime-function 'gnus-display-mime
911   "Function to display MIME articles."
912   :group 'gnus-article-mime
913   :type 'function)
914
915 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
916   "Function used to decode headers.")
917
918 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
919   "Function used to decode addresses.")
920
921 (defvar gnus-article-dumbquotes-map
922   '(("\200" "EUR")
923     ("\202" ",")
924     ("\203" "f")
925     ("\204" ",,")
926     ("\205" "...")
927     ("\213" "<")
928     ("\214" "OE")
929     ("\221" "`")
930     ("\222" "'")
931     ("\223" "``")
932     ("\224" "\"")
933     ("\225" "*")
934     ("\226" "-")
935     ("\227" "--")
936     ("\230" "~")
937     ("\231" "(TM)")
938     ("\233" ">")
939     ("\234" "oe")
940     ("\264" "'"))
941   "Table for MS-to-Latin1 translation.")
942
943 (defcustom gnus-ignored-mime-types nil
944   "List of MIME types that should be ignored by Gnus."
945   :version "21.1"
946   :group 'gnus-article-mime
947   :type '(repeat regexp))
948
949 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
950   "List of MIME types that should not be given buttons when rendered inline.
951 See also `gnus-buttonized-mime-types' which may override this variable.
952 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
953   :version "21.1"
954   :group 'gnus-article-mime
955   :type '(repeat regexp))
956
957 (defcustom gnus-buttonized-mime-types nil
958   "List of MIME types that should be given buttons when rendered inline.
959 If set, this variable overrides `gnus-unbuttonized-mime-types'.
960 To see e.g. security buttons you could set this to
961 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
962 this list to display radio buttons that allow you to choose one of two
963 media types those mails include.  See also `mm-discouraged-alternatives'.
964 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
965   :version "22.1"
966   :group 'gnus-article-mime
967   :type '(repeat regexp))
968
969 (defcustom gnus-inhibit-mime-unbuttonizing nil
970   "If non-nil, all MIME parts get buttons.
971 When nil (the default value), then some MIME parts do not get buttons,
972 as described by the variables `gnus-buttonized-mime-types' and
973 `gnus-unbuttonized-mime-types'."
974   :version "22.1"
975   :group 'gnus-article-mime
976   :type 'boolean)
977
978 (defcustom gnus-body-boundary-delimiter "_"
979   "String used to delimit header and body.
980 This variable is used by `gnus-article-treat-body-boundary' which can
981 be controlled by `gnus-treat-body-boundary'."
982   :version "22.1"
983   :group 'gnus-article-various
984   :type '(choice (item :tag "None" :value nil)
985                  string))
986
987 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
988                                   "/usr/share/picons")
989   "Defines the location of the faces database.
990 For information on obtaining this database of pretty pictures, please
991 see http://www.cs.indiana.edu/picons/ftp/index.html"
992   :version "22.1"
993   :type '(repeat directory)
994   :link '(url-link :tag "download"
995                    "http://www.cs.indiana.edu/picons/ftp/index.html")
996   :link '(custom-manual "(gnus)Picons")
997   :group 'gnus-picon)
998
999 (defun gnus-picons-installed-p ()
1000   "Say whether picons are installed on your machine."
1001   (let ((installed nil))
1002     (dolist (database gnus-picon-databases)
1003       (when (file-exists-p database)
1004         (setq installed t)))
1005     installed))
1006
1007 (defcustom gnus-article-mime-part-function nil
1008   "Function called with a MIME handle as the argument.
1009 This is meant for people who want to do something automatic based
1010 on parts -- for instance, adding Vcard info to a database."
1011   :group 'gnus-article-mime
1012   :type '(choice (const nil)
1013                  function))
1014
1015 (defcustom gnus-mime-multipart-functions nil
1016   "An alist of MIME types to functions to display them."
1017   :version "21.1"
1018   :group 'gnus-article-mime
1019   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1020
1021 (defcustom gnus-article-date-lapsed-new-header nil
1022   "Whether the X-Sent and Date headers can coexist.
1023 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
1024 either replace the old \"Date:\" header (if this variable is nil), or
1025 be added below it (otherwise)."
1026   :version "21.1"
1027   :group 'gnus-article-headers
1028   :type 'boolean)
1029
1030 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1031   "Function called with a MIME handle as the argument.
1032 This is meant for people who want to view first matched part.
1033 For `undisplayed-alternative' (default), the first undisplayed
1034 part or alternative part is used.  For `undisplayed', the first
1035 undisplayed part is used.  For a function, the first part which
1036 the function return t is used.  For nil, the first part is
1037 used."
1038   :version "21.1"
1039   :group 'gnus-article-mime
1040   :type '(choice
1041           (item :tag "first" :value nil)
1042           (item :tag "undisplayed" :value undisplayed)
1043           (item :tag "undisplayed or alternative"
1044                 :value undisplayed-alternative)
1045           (function)))
1046
1047 (defcustom gnus-mime-action-alist
1048   '(("save to file" . gnus-mime-save-part)
1049     ("save and strip" . gnus-mime-save-part-and-strip)
1050     ("replace with file" . gnus-mime-replace-part)
1051     ("delete part" . gnus-mime-delete-part)
1052     ("display as text" . gnus-mime-inline-part)
1053     ("view the part" . gnus-mime-view-part)
1054     ("pipe to command" . gnus-mime-pipe-part)
1055     ("toggle display" . gnus-article-press-button)
1056     ("toggle display" . gnus-article-view-part-as-charset)
1057     ("view as type" . gnus-mime-view-part-as-type)
1058     ("view internally" . gnus-mime-view-part-internally)
1059     ("view externally" . gnus-mime-view-part-externally))
1060   "An alist of actions that run on the MIME attachment."
1061   :group 'gnus-article-mime
1062   :type '(repeat (cons (string :tag "name")
1063                        (function))))
1064
1065 (defcustom gnus-auto-select-part 1
1066   "Advance to next MIME part when deleting or stripping parts.
1067
1068 When 0, point will be placed on the same part as before.  When
1069 positive (negative), move point forward (backwards) this many
1070 parts.  When nil, redisplay article."
1071   :version "23.1" ;; No Gnus
1072   :group 'gnus-article-mime
1073   :type '(choice (const nil :tag "Redisplay article.")
1074                  (const 1 :tag "Next part.")
1075                  (const 0 :tag "Current part.")
1076                  integer))
1077
1078 ;;;
1079 ;;; The treatment variables
1080 ;;;
1081
1082 (defvar gnus-part-display-hook nil
1083   "Hook called on parts that are to receive treatment.")
1084
1085 (defvar gnus-article-treat-custom
1086   '(choice (const :tag "Off" nil)
1087            (const :tag "On" t)
1088            (const :tag "Header" head)
1089            (const :tag "First" first)
1090            (const :tag "Last" last)
1091            (integer :tag "Less")
1092            (repeat :tag "Groups" regexp)
1093            (sexp :tag "Predicate")))
1094
1095 (defvar gnus-article-treat-head-custom
1096   '(choice (const :tag "Off" nil)
1097            (const :tag "Header" head)))
1098
1099 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1100                                    "text/x-patch")
1101   "Parts to treat.")
1102
1103 (defvar gnus-inhibit-treatment nil
1104   "Whether to inhibit treatment.")
1105
1106 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1107   "Highlight the signature.
1108 Valid values are nil, t, `head', `first', `last', an integer or a
1109 predicate.  See Info node `(gnus)Customizing Articles'."
1110   :group 'gnus-article-treat
1111   :link '(custom-manual "(gnus)Customizing Articles")
1112   :type gnus-article-treat-custom)
1113 (put 'gnus-treat-highlight-signature 'highlight t)
1114
1115 (defcustom gnus-treat-buttonize 100000
1116   "Add buttons.
1117 Valid values are nil, t, `head', `first', `last', an integer or a
1118 predicate.  See Info node `(gnus)Customizing Articles'."
1119   :group 'gnus-article-treat
1120   :link '(custom-manual "(gnus)Customizing Articles")
1121   :type gnus-article-treat-custom)
1122 (put 'gnus-treat-buttonize 'highlight t)
1123
1124 (defcustom gnus-treat-buttonize-head 'head
1125   "Add buttons to the head.
1126 Valid values are nil, t, `head', `first', `last', an integer or a
1127 predicate.  See Info node `(gnus)Customizing Articles'."
1128   :group 'gnus-article-treat
1129   :link '(custom-manual "(gnus)Customizing Articles")
1130   :type gnus-article-treat-head-custom)
1131 (put 'gnus-treat-buttonize-head 'highlight t)
1132
1133 (defcustom gnus-treat-emphasize 50000
1134   "Emphasize text.
1135 Valid values are nil, t, `head', `first', `last', an integer or a
1136 predicate.  See Info node `(gnus)Customizing Articles'."
1137   :group 'gnus-article-treat
1138   :link '(custom-manual "(gnus)Customizing Articles")
1139   :type gnus-article-treat-custom)
1140 (put 'gnus-treat-emphasize 'highlight t)
1141
1142 (defcustom gnus-treat-strip-cr nil
1143   "Remove carriage returns.
1144 Valid values are nil, t, `head', `first', `last', an integer or a
1145 predicate.  See Info node `(gnus)Customizing Articles'."
1146   :version "22.1"
1147   :group 'gnus-article-treat
1148   :link '(custom-manual "(gnus)Customizing Articles")
1149   :type gnus-article-treat-custom)
1150
1151 (defcustom gnus-treat-unsplit-urls nil
1152   "Remove newlines from within URLs.
1153 Valid values are nil, t, `head', `first', `last', an integer or a
1154 predicate.  See Info node `(gnus)Customizing Articles'."
1155   :version "22.1"
1156   :group 'gnus-article-treat
1157   :link '(custom-manual "(gnus)Customizing Articles")
1158   :type gnus-article-treat-custom)
1159
1160 (defcustom gnus-treat-leading-whitespace nil
1161   "Remove leading whitespace in headers.
1162 Valid values are nil, t, `head', `first', `last', an integer or a
1163 predicate.  See Info node `(gnus)Customizing Articles'."
1164   :version "22.1"
1165   :group 'gnus-article-treat
1166   :link '(custom-manual "(gnus)Customizing Articles")
1167   :type gnus-article-treat-custom)
1168
1169 (defcustom gnus-treat-hide-headers 'head
1170   "Hide headers.
1171 Valid values are nil, t, `head', `first', `last', an integer or a
1172 predicate.  See Info node `(gnus)Customizing Articles'."
1173   :group 'gnus-article-treat
1174   :link '(custom-manual "(gnus)Customizing Articles")
1175   :type gnus-article-treat-head-custom)
1176
1177 (defcustom gnus-treat-hide-boring-headers nil
1178   "Hide boring headers.
1179 Valid values are nil, t, `head', `first', `last', an integer or a
1180 predicate.  See Info node `(gnus)Customizing Articles'."
1181   :group 'gnus-article-treat
1182   :link '(custom-manual "(gnus)Customizing Articles")
1183   :type gnus-article-treat-head-custom)
1184
1185 (defcustom gnus-treat-hide-signature nil
1186   "Hide the signature.
1187 Valid values are nil, t, `head', `first', `last', an integer or a
1188 predicate.  See Info node `(gnus)Customizing Articles'."
1189   :group 'gnus-article-treat
1190   :link '(custom-manual "(gnus)Customizing Articles")
1191   :type gnus-article-treat-custom)
1192
1193 (defcustom gnus-treat-fill-article nil
1194   "Fill the article.
1195 Valid values are nil, t, `head', `first', `last', an integer or a
1196 predicate.  See Info node `(gnus)Customizing Articles'."
1197   :group 'gnus-article-treat
1198   :link '(custom-manual "(gnus)Customizing Articles")
1199   :type gnus-article-treat-custom)
1200
1201 (defcustom gnus-treat-hide-citation nil
1202   "Hide cited text.
1203 Valid values are nil, t, `head', `first', `last', an integer or a
1204 predicate.  See Info node `(gnus)Customizing Articles'."
1205   :group 'gnus-article-treat
1206   :link '(custom-manual "(gnus)Customizing Articles")
1207   :type gnus-article-treat-custom)
1208
1209 (defcustom gnus-treat-hide-citation-maybe nil
1210   "Hide cited text.
1211 Valid values are nil, t, `head', `first', `last', an integer or a
1212 predicate.  See Info node `(gnus)Customizing Articles'."
1213   :group 'gnus-article-treat
1214   :link '(custom-manual "(gnus)Customizing Articles")
1215   :type gnus-article-treat-custom)
1216
1217 (defcustom gnus-treat-strip-list-identifiers 'head
1218   "Strip list identifiers from `gnus-list-identifiers`.
1219 Valid values are nil, t, `head', `first', `last', an integer or a
1220 predicate.  See Info node `(gnus)Customizing Articles'."
1221   :version "21.1"
1222   :group 'gnus-article-treat
1223   :link '(custom-manual "(gnus)Customizing Articles")
1224   :type gnus-article-treat-custom)
1225
1226 (make-obsolete-variable 'gnus-treat-strip-pgp nil
1227                         "Gnus 5.10 (Emacs 22.1)")
1228
1229 (defcustom gnus-treat-strip-pem nil
1230   "Strip PEM signatures.
1231 Valid values are nil, t, `head', `first', `last', an integer or a
1232 predicate.  See Info node `(gnus)Customizing Articles'."
1233   :group 'gnus-article-treat
1234   :link '(custom-manual "(gnus)Customizing Articles")
1235   :type gnus-article-treat-custom)
1236
1237 (defcustom gnus-treat-strip-banner t
1238   "Strip banners from articles.
1239 The banner to be stripped is specified in the `banner' group parameter.
1240 Valid values are nil, t, `head', `first', `last', an integer or a
1241 predicate.  See Info node `(gnus)Customizing Articles'."
1242   :group 'gnus-article-treat
1243   :link '(custom-manual "(gnus)Customizing Articles")
1244   :type gnus-article-treat-custom)
1245
1246 (defcustom gnus-treat-highlight-headers 'head
1247   "Highlight the headers.
1248 Valid values are nil, t, `head', `first', `last', an integer or a
1249 predicate.  See Info node `(gnus)Customizing Articles'."
1250   :group 'gnus-article-treat
1251   :link '(custom-manual "(gnus)Customizing Articles")
1252   :type gnus-article-treat-head-custom)
1253 (put 'gnus-treat-highlight-headers 'highlight t)
1254
1255 (defcustom gnus-treat-highlight-citation t
1256   "Highlight cited text.
1257 Valid values are nil, t, `head', `first', `last', an integer or a
1258 predicate.  See Info node `(gnus)Customizing Articles'."
1259   :group 'gnus-article-treat
1260   :link '(custom-manual "(gnus)Customizing Articles")
1261   :type gnus-article-treat-custom)
1262 (put 'gnus-treat-highlight-citation 'highlight t)
1263
1264 (defcustom gnus-treat-date-ut nil
1265   "Display the Date in UT (GMT).
1266 Valid values are nil, t, `head', `first', `last', an integer or a
1267 predicate.  See Info node `(gnus)Customizing Articles'."
1268   :group 'gnus-article-treat
1269   :link '(custom-manual "(gnus)Customizing Articles")
1270   :type gnus-article-treat-head-custom)
1271
1272 (defcustom gnus-treat-date-local nil
1273   "Display the Date in the local timezone.
1274 Valid values are nil, t, `head', `first', `last', an integer or a
1275 predicate.  See Info node `(gnus)Customizing Articles'."
1276   :group 'gnus-article-treat
1277   :link '(custom-manual "(gnus)Customizing Articles")
1278   :type gnus-article-treat-head-custom)
1279
1280 (defcustom gnus-treat-date-english nil
1281   "Display the Date in a format that can be read aloud in English.
1282 Valid values are nil, t, `head', `first', `last', an integer or a
1283 predicate.  See Info node `(gnus)Customizing Articles'."
1284   :version "22.1"
1285   :group 'gnus-article-treat
1286   :link '(custom-manual "(gnus)Customizing Articles")
1287   :type gnus-article-treat-head-custom)
1288
1289 (defcustom gnus-treat-date-lapsed nil
1290   "Display the Date header in a way that says how much time has elapsed.
1291 Valid values are nil, t, `head', `first', `last', an integer or a
1292 predicate.  See Info node `(gnus)Customizing Articles'."
1293   :group 'gnus-article-treat
1294   :link '(custom-manual "(gnus)Customizing Articles")
1295   :type gnus-article-treat-head-custom)
1296
1297 (defcustom gnus-treat-date-original nil
1298   "Display the date in the original timezone.
1299 Valid values are nil, t, `head', `first', `last', an integer or a
1300 predicate.  See Info node `(gnus)Customizing Articles'."
1301   :group 'gnus-article-treat
1302   :link '(custom-manual "(gnus)Customizing Articles")
1303   :type gnus-article-treat-head-custom)
1304
1305 (defcustom gnus-treat-date-iso8601 nil
1306   "Display the date in the ISO8601 format.
1307 Valid values are nil, t, `head', `first', `last', an integer or a
1308 predicate.  See Info node `(gnus)Customizing Articles'."
1309   :version "21.1"
1310   :group 'gnus-article-treat
1311   :link '(custom-manual "(gnus)Customizing Articles")
1312   :type gnus-article-treat-head-custom)
1313
1314 (defcustom gnus-treat-date-user-defined nil
1315   "Display the date in a user-defined format.
1316 The format is defined by the `gnus-article-time-format' variable.
1317 Valid values are nil, t, `head', `first', `last', an integer or a
1318 predicate.  See Info node `(gnus)Customizing Articles'."
1319   :group 'gnus-article-treat
1320   :link '(custom-manual "(gnus)Customizing Articles")
1321   :type gnus-article-treat-head-custom)
1322
1323 (defcustom gnus-treat-strip-headers-in-body t
1324   "Strip the X-No-Archive header line from the beginning of the body.
1325 Valid values are nil, t, `head', `first', `last', an integer or a
1326 predicate.  See Info node `(gnus)Customizing Articles'."
1327   :version "21.1"
1328   :group 'gnus-article-treat
1329   :link '(custom-manual "(gnus)Customizing Articles")
1330   :type gnus-article-treat-custom)
1331
1332 (defcustom gnus-treat-strip-trailing-blank-lines nil
1333   "Strip trailing blank lines.
1334 Valid values are nil, t, `head', `first', `last', an integer or a
1335 predicate.  See Info node `(gnus)Customizing Articles'.
1336
1337 When set to t, it also strips trailing blanks in all MIME parts.
1338 Consider to use `last' instead."
1339   :group 'gnus-article-treat
1340   :link '(custom-manual "(gnus)Customizing Articles")
1341   :type gnus-article-treat-custom)
1342
1343 (defcustom gnus-treat-strip-leading-blank-lines nil
1344   "Strip leading blank lines.
1345 Valid values are nil, t, `head', `first', `last', an integer or a
1346 predicate.  See Info node `(gnus)Customizing Articles'.
1347
1348 When set to t, it also strips trailing blanks in all MIME parts."
1349   :group 'gnus-article-treat
1350   :link '(custom-manual "(gnus)Customizing Articles")
1351   :type gnus-article-treat-custom)
1352
1353 (defcustom gnus-treat-strip-multiple-blank-lines nil
1354   "Strip multiple blank lines.
1355 Valid values are nil, t, `head', `first', `last', an integer or a
1356 predicate.  See Info node `(gnus)Customizing Articles'."
1357   :group 'gnus-article-treat
1358   :link '(custom-manual "(gnus)Customizing Articles")
1359   :type gnus-article-treat-custom)
1360
1361 (defcustom gnus-treat-unfold-headers 'head
1362   "Unfold folded header lines.
1363 Valid values are nil, t, `head', `first', `last', an integer or a
1364 predicate.  See Info node `(gnus)Customizing Articles'."
1365   :version "22.1"
1366   :group 'gnus-article-treat
1367   :link '(custom-manual "(gnus)Customizing Articles")
1368   :type gnus-article-treat-custom)
1369
1370 (defcustom gnus-article-unfold-long-headers nil
1371   "If non-nil, allow unfolding headers even if the header is long.
1372 If it is a regexp, only long headers matching this regexp are unfolded.
1373 If it is t, all long headers are unfolded.
1374
1375 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1376   :version "23.1" ;; No Gnus
1377   :group 'gnus-article-treat
1378   :type '(choice (const nil)
1379                  (const :tag "all" t)
1380                  (regexp)))
1381
1382 (defcustom gnus-treat-fold-headers nil
1383   "Fold headers.
1384 Valid values are nil, t, `head', `first', `last', an integer or a
1385 predicate.  See Info node `(gnus)Customizing Articles'."
1386   :version "22.1"
1387   :group 'gnus-article-treat
1388   :link '(custom-manual "(gnus)Customizing Articles")
1389   :type gnus-article-treat-custom)
1390
1391 (defcustom gnus-treat-fold-newsgroups 'head
1392   "Fold the Newsgroups and Followup-To headers.
1393 Valid values are nil, t, `head', `first', `last', an integer or a
1394 predicate.  See Info node `(gnus)Customizing Articles'."
1395   :version "22.1"
1396   :group 'gnus-article-treat
1397   :link '(custom-manual "(gnus)Customizing Articles")
1398   :type gnus-article-treat-custom)
1399
1400 (defcustom gnus-treat-overstrike t
1401   "Treat overstrike highlighting.
1402 Valid values are nil, t, `head', `first', `last', an integer or a
1403 predicate.  See Info node `(gnus)Customizing Articles'."
1404   :group 'gnus-article-treat
1405   :link '(custom-manual "(gnus)Customizing Articles")
1406   :type gnus-article-treat-custom)
1407 (put 'gnus-treat-overstrike 'highlight t)
1408
1409 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1410   "Treat ANSI SGR control sequences.
1411 Valid values are nil, t, `head', `first', `last', an integer or a
1412 predicate.  See Info node `(gnus)Customizing Articles'."
1413   :group 'gnus-article-treat
1414   :link '(custom-manual "(gnus)Customizing Articles")
1415   :type gnus-article-treat-custom)
1416
1417 (make-obsolete-variable 'gnus-treat-display-xface
1418                         'gnus-treat-display-x-face "22.1")
1419
1420 (defcustom gnus-treat-display-x-face
1421   (and (not noninteractive)
1422        (gnus-image-type-available-p 'xbm)
1423        (if (featurep 'xemacs)
1424            (featurep 'xface)
1425          (condition-case nil
1426              (and (string-match "^0x" (shell-command-to-string "uncompface"))
1427                   (executable-find "icontopbm"))
1428            ;; shell-command-to-string may signal an error, e.g. if
1429            ;; shell-file-name is not found.
1430            (error nil)))
1431        'head)
1432   "Display X-Face headers.
1433 Valid values are nil and `head'.
1434 See Info node `(gnus)Customizing Articles' and Info node
1435 `(gnus)X-Face' for details."
1436   :group 'gnus-article-treat
1437   :version "21.1"
1438   :link '(custom-manual "(gnus)Customizing Articles")
1439   :link '(custom-manual "(gnus)X-Face")
1440   :type gnus-article-treat-head-custom
1441   :set (lambda (symbol value)
1442          (set-default
1443           symbol
1444           (cond ((or (boundp symbol) (get symbol 'saved-value))
1445                  value)
1446                 ((boundp 'gnus-treat-display-xface)
1447                  (message "\
1448 ** gnus-treat-display-xface is an obsolete variable;\
1449  use gnus-treat-display-x-face instead")
1450                  (default-value 'gnus-treat-display-xface))
1451                 ((get 'gnus-treat-display-xface 'saved-value)
1452                  (message "\
1453 ** gnus-treat-display-xface is an obsolete variable;\
1454  use gnus-treat-display-x-face instead")
1455                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1456                 (t
1457                  value)))))
1458 (put 'gnus-treat-display-x-face 'highlight t)
1459
1460 (defcustom gnus-treat-display-face
1461   (and (not noninteractive)
1462        (gnus-image-type-available-p 'png)
1463        'head)
1464   "Display Face headers.
1465 Valid values are nil, t, `head', `first', `last', an integer or a
1466 predicate.  See Info node `(gnus)Customizing Articles' and Info
1467 node `(gnus)Face' for details."
1468   :group 'gnus-article-treat
1469   :version "22.1"
1470   :link '(custom-manual "(gnus)Customizing Articles")
1471   :link '(custom-manual "(gnus)X-Face")
1472   :type gnus-article-treat-head-custom)
1473 (put 'gnus-treat-display-face 'highlight t)
1474
1475 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1476   "Display smileys.
1477 Valid values are nil, t, `head', `first', `last', an integer or a
1478 predicate.  See Info node `(gnus)Customizing Articles' and Info
1479 node `(gnus)Smileys' for details."
1480   :group 'gnus-article-treat
1481   :version "21.1"
1482   :link '(custom-manual "(gnus)Customizing Articles")
1483   :link '(custom-manual "(gnus)Smileys")
1484   :type gnus-article-treat-custom)
1485 (put 'gnus-treat-display-smileys 'highlight t)
1486
1487 (defcustom gnus-treat-from-picon
1488   (if (and (gnus-image-type-available-p 'xpm)
1489            (gnus-picons-installed-p))
1490       'head nil)
1491   "Display picons in the From header.
1492 Valid values are nil, t, `head', `first', `last', an integer or a
1493 predicate.  See Info node `(gnus)Customizing Articles' and Info
1494 node `(gnus)Picons' for details."
1495   :version "22.1"
1496   :group 'gnus-article-treat
1497   :group 'gnus-picon
1498   :link '(custom-manual "(gnus)Customizing Articles")
1499   :link '(custom-manual "(gnus)Picons")
1500   :type gnus-article-treat-head-custom)
1501 (put 'gnus-treat-from-picon 'highlight t)
1502
1503 (defcustom gnus-treat-mail-picon
1504   (if (and (gnus-image-type-available-p 'xpm)
1505            (gnus-picons-installed-p))
1506       'head nil)
1507   "Display picons in To and Cc headers.
1508 Valid values are nil, t, `head', `first', `last', an integer or a
1509 predicate.  See Info node `(gnus)Customizing Articles' and Info
1510 node `(gnus)Picons' for details."
1511   :version "22.1"
1512   :group 'gnus-article-treat
1513   :group 'gnus-picon
1514   :link '(custom-manual "(gnus)Customizing Articles")
1515   :link '(custom-manual "(gnus)Picons")
1516   :type gnus-article-treat-head-custom)
1517 (put 'gnus-treat-mail-picon 'highlight t)
1518
1519 (defcustom gnus-treat-newsgroups-picon
1520   (if (and (gnus-image-type-available-p 'xpm)
1521            (gnus-picons-installed-p))
1522       'head nil)
1523   "Display picons in the Newsgroups and Followup-To headers.
1524 Valid values are nil, t, `head', `first', `last', an integer or a
1525 predicate.  See Info node `(gnus)Customizing Articles' and Info
1526 node `(gnus)Picons' for details."
1527   :version "22.1"
1528   :group 'gnus-article-treat
1529   :group 'gnus-picon
1530   :link '(custom-manual "(gnus)Customizing Articles")
1531   :link '(custom-manual "(gnus)Picons")
1532   :type gnus-article-treat-head-custom)
1533 (put 'gnus-treat-newsgroups-picon 'highlight t)
1534
1535 (defcustom gnus-treat-body-boundary
1536   (if (or gnus-treat-newsgroups-picon
1537           gnus-treat-mail-picon
1538           gnus-treat-from-picon)
1539       ;; If there's much decoration, the user might prefer a boundery.
1540       'head
1541     nil)
1542   "Draw a boundary at the end of the headers.
1543 Valid values are nil and `head'.
1544 See Info node `(gnus)Customizing Articles' for details."
1545   :version "22.1"
1546   :group 'gnus-article-treat
1547   :link '(custom-manual "(gnus)Customizing Articles")
1548   :type gnus-article-treat-head-custom)
1549
1550 (defcustom gnus-treat-capitalize-sentences nil
1551   "Capitalize sentence-starting words.
1552 Valid values are nil, t, `head', `first', `last', an integer or a
1553 predicate.  See Info node `(gnus)Customizing Articles'."
1554   :version "21.1"
1555   :group 'gnus-article-treat
1556   :link '(custom-manual "(gnus)Customizing Articles")
1557   :type gnus-article-treat-custom)
1558
1559 (defcustom gnus-treat-wash-html nil
1560   "Format as HTML.
1561 Valid values are nil, t, `head', `first', `last', an integer or a
1562 predicate.  See Info node `(gnus)Customizing Articles'."
1563   :version "22.1"
1564   :group 'gnus-article-treat
1565   :link '(custom-manual "(gnus)Customizing Articles")
1566   :type gnus-article-treat-custom)
1567
1568 (defcustom gnus-treat-fill-long-lines nil
1569   "Fill long lines.
1570 Valid values are nil, t, `head', `first', `last', an integer or a
1571 predicate.  See Info node `(gnus)Customizing Articles'."
1572   :group 'gnus-article-treat
1573   :link '(custom-manual "(gnus)Customizing Articles")
1574   :type gnus-article-treat-custom)
1575
1576 (defcustom gnus-treat-play-sounds nil
1577   "Play sounds.
1578 Valid values are nil, t, `head', `first', `last', an integer or a
1579 predicate.  See Info node `(gnus)Customizing Articles'."
1580   :version "21.1"
1581   :group 'gnus-article-treat
1582   :link '(custom-manual "(gnus)Customizing Articles")
1583   :type gnus-article-treat-custom)
1584
1585 (defcustom gnus-treat-translate nil
1586   "Translate articles from one language to another.
1587 Valid values are nil, t, `head', `first', `last', an integer or a
1588 predicate.  See Info node `(gnus)Customizing Articles'."
1589   :version "21.1"
1590   :group 'gnus-article-treat
1591   :link '(custom-manual "(gnus)Customizing Articles")
1592   :type gnus-article-treat-custom)
1593
1594 (defcustom gnus-treat-x-pgp-sig nil
1595   "Verify X-PGP-Sig.
1596 To automatically treat X-PGP-Sig, set it to head.
1597 Valid values are nil, t, `head', `first', `last', an integer or a
1598 predicate.  See Info node `(gnus)Customizing Articles'."
1599   :version "22.1"
1600   :group 'gnus-article-treat
1601   :group 'mime-security
1602   :link '(custom-manual "(gnus)Customizing Articles")
1603   :type gnus-article-treat-custom)
1604
1605 (defvar gnus-article-encrypt-protocol-alist
1606   '(("PGP" . mml2015-self-encrypt)))
1607
1608 ;; Set to nil if more than one protocol added to
1609 ;; gnus-article-encrypt-protocol-alist.
1610 (defcustom gnus-article-encrypt-protocol "PGP"
1611   "The protocol used for encrypt articles.
1612 It is a string, such as \"PGP\". If nil, ask user."
1613   :version "22.1"
1614   :type 'string
1615   :group 'mime-security)
1616
1617 (defvar gnus-article-wash-function nil
1618   "Function used for converting HTML into text.")
1619
1620 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1621                               (mm-coding-system-p 'utf-8)
1622                               (executable-find idna-program))
1623   "Whether IDNA decoding of headers is used when viewing messages.
1624 This requires GNU Libidn, and by default only enabled if it is found."
1625   :version "22.1"
1626   :group 'gnus-article-headers
1627   :type 'boolean)
1628
1629 (defcustom gnus-article-over-scroll nil
1630   "If non-nil, allow scrolling the article buffer even when there no more text."
1631   :version "22.1"
1632   :group 'gnus-article
1633   :type 'boolean)
1634
1635 ;;; Internal variables
1636
1637 (defvar gnus-english-month-names
1638   '("January" "February" "March" "April" "May" "June" "July" "August"
1639     "September" "October" "November" "December"))
1640
1641 (defvar article-goto-body-goes-to-point-min-p nil)
1642 (defvar gnus-article-wash-types nil)
1643 (defvar gnus-article-emphasis-alist nil)
1644 (defvar gnus-article-image-alist nil)
1645
1646 (defvar gnus-article-mime-handle-alist-1 nil)
1647 (defvar gnus-treatment-function-alist
1648   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1649     (gnus-treat-strip-banner gnus-article-strip-banner)
1650     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1651     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1652     (gnus-treat-buttonize gnus-article-add-buttons)
1653     (gnus-treat-fill-article gnus-article-fill-cited-article)
1654     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1655     (gnus-treat-strip-cr gnus-article-remove-cr)
1656     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1657     (gnus-treat-date-ut gnus-article-date-ut)
1658     (gnus-treat-date-local gnus-article-date-local)
1659     (gnus-treat-date-english gnus-article-date-english)
1660     (gnus-treat-date-original gnus-article-date-original)
1661     (gnus-treat-date-user-defined gnus-article-date-user)
1662     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1663     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1664     (gnus-treat-display-x-face gnus-article-display-x-face)
1665     (gnus-treat-display-face gnus-article-display-face)
1666     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1667     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1668     (gnus-treat-hide-signature gnus-article-hide-signature)
1669     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1670     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1671     (gnus-treat-strip-pem gnus-article-hide-pem)
1672     (gnus-treat-from-picon gnus-treat-from-picon)
1673     (gnus-treat-mail-picon gnus-treat-mail-picon)
1674     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1675     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1676     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1677     (gnus-treat-strip-trailing-blank-lines
1678      gnus-article-remove-trailing-blank-lines)
1679     (gnus-treat-strip-leading-blank-lines
1680      gnus-article-strip-leading-blank-lines)
1681     (gnus-treat-strip-multiple-blank-lines
1682      gnus-article-strip-multiple-blank-lines)
1683     (gnus-treat-overstrike gnus-article-treat-overstrike)
1684     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1685     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1686     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1687     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1688     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1689     (gnus-treat-display-smileys gnus-treat-smiley)
1690     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1691     (gnus-treat-wash-html gnus-article-wash-html)
1692     (gnus-treat-emphasize gnus-article-emphasize)
1693     (gnus-treat-hide-citation gnus-article-hide-citation)
1694     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1695     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1696     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1697     (gnus-treat-play-sounds gnus-earcon-display)))
1698
1699 (defvar gnus-article-mime-handle-alist nil)
1700 (defvar article-lapsed-timer nil)
1701 (defvar gnus-article-current-summary nil)
1702
1703 (defvar gnus-article-mode-syntax-table
1704   (let ((table (copy-syntax-table text-mode-syntax-table)))
1705     ;; This causes the citation match run O(2^n).
1706     ;; (modify-syntax-entry ?- "w" table)
1707     (modify-syntax-entry ?> ")<" table)
1708     (modify-syntax-entry ?< "(>" table)
1709     ;; make M-. in article buffers work for `foo' strings
1710     (modify-syntax-entry ?' " " table)
1711     (modify-syntax-entry ?` " " table)
1712     table)
1713   "Syntax table used in article mode buffers.
1714 Initialized from `text-mode-syntax-table.")
1715
1716 (defvar gnus-save-article-buffer nil)
1717
1718 (defvar gnus-number-of-articles-to-be-saved nil)
1719
1720 (defvar gnus-inhibit-hiding nil)
1721
1722 (defvar gnus-article-edit-mode nil)
1723
1724 ;;; Macros for dealing with the article buffer.
1725
1726 (defmacro gnus-with-article-headers (&rest forms)
1727   `(with-current-buffer gnus-article-buffer
1728      (save-restriction
1729        (let ((inhibit-read-only t)
1730              (inhibit-point-motion-hooks t)
1731              (case-fold-search t))
1732          (article-narrow-to-head)
1733          ,@forms))))
1734
1735 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1736 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1737
1738 (defmacro gnus-with-article-buffer (&rest forms)
1739   `(with-current-buffer gnus-article-buffer
1740      (let ((inhibit-read-only t))
1741        ,@forms)))
1742
1743 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1744 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1745
1746 (defun gnus-article-goto-header (header)
1747   "Go to HEADER, which is a regular expression."
1748   (re-search-forward (concat "^\\(" header "\\):") nil t))
1749
1750 (defsubst gnus-article-hide-text (b e props)
1751   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1752   (gnus-add-text-properties-when 'article-type nil b e props)
1753   (when (memq 'intangible props)
1754     (put-text-property
1755      (max (1- b) (point-min))
1756      b 'intangible (cddr (memq 'intangible props)))))
1757
1758 (defsubst gnus-article-unhide-text (b e)
1759   "Remove hidden text properties from region between B and E."
1760   (remove-text-properties b e gnus-hidden-properties)
1761   (when (memq 'intangible gnus-hidden-properties)
1762     (put-text-property (max (1- b) (point-min))
1763                        b 'intangible nil)))
1764
1765 (defun gnus-article-hide-text-type (b e type)
1766   "Hide text of TYPE between B and E."
1767   (gnus-add-wash-type type)
1768   (gnus-article-hide-text
1769    b e (cons 'article-type (cons type gnus-hidden-properties))))
1770
1771 (defun gnus-article-unhide-text-type (b e type)
1772   "Unhide text of TYPE between B and E."
1773   (gnus-delete-wash-type type)
1774   (remove-text-properties
1775    b e (cons 'article-type (cons type gnus-hidden-properties)))
1776   (when (memq 'intangible gnus-hidden-properties)
1777     (put-text-property (max (1- b) (point-min))
1778                        b 'intangible nil)))
1779
1780 (defun gnus-article-hide-text-of-type (type)
1781   "Hide text of TYPE in the current buffer."
1782   (save-excursion
1783     (let ((b (point-min))
1784           (e (point-max)))
1785       (while (setq b (text-property-any b e 'article-type type))
1786         (add-text-properties b (incf b) gnus-hidden-properties)))))
1787
1788 (defun gnus-article-delete-text-of-type (type)
1789   "Delete text of TYPE in the current buffer."
1790   (save-excursion
1791     (let ((b (point-min)))
1792       (if (eq type 'multipart)
1793           ;; Remove MIME buttons associated with multipart/alternative parts.
1794           (progn
1795             (goto-char b)
1796             (while (if (get-text-property (point) 'gnus-part)
1797                        (setq b (point))
1798                      (when (setq b (next-single-property-change (point)
1799                                                                 'gnus-part))
1800                        (goto-char b)
1801                        t))
1802               (end-of-line)
1803               (skip-chars-forward "\n")
1804               (when (eq (get-text-property b 'article-type) 'multipart)
1805                 (delete-region b (point)))))
1806         (while (setq b (text-property-any b (point-max) 'article-type type))
1807           (delete-region
1808            b (or (text-property-not-all b (point-max) 'article-type type)
1809                  (point-max))))))))
1810
1811 (defun gnus-article-delete-invisible-text ()
1812   "Delete all invisible text in the current buffer."
1813   (save-excursion
1814     (let ((b (point-min)))
1815       (while (setq b (text-property-any b (point-max) 'invisible t))
1816         (delete-region
1817          b (or (text-property-not-all b (point-max) 'invisible t)
1818                (point-max)))))))
1819
1820 (defun gnus-article-text-type-exists-p (type)
1821   "Say whether any text of type TYPE exists in the buffer."
1822   (text-property-any (point-min) (point-max) 'article-type type))
1823
1824 (defsubst gnus-article-header-rank ()
1825   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1826   (let ((list gnus-sorted-header-list)
1827         (i 1))
1828     (while list
1829       (if (looking-at (car list))
1830           (setq list nil)
1831         (setq list (cdr list))
1832         (incf i)))
1833       i))
1834
1835 (defun article-hide-headers (&optional arg delete)
1836   "Hide unwanted headers and possibly sort them as well."
1837   (interactive)
1838   ;; This function might be inhibited.
1839   (unless gnus-inhibit-hiding
1840     (let ((inhibit-read-only t)
1841           (case-fold-search t)
1842           (max (1+ (length gnus-sorted-header-list)))
1843           (inhibit-point-motion-hooks t)
1844           (cur (current-buffer))
1845           ignored visible beg)
1846       (save-excursion
1847         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1848         ;; group parameters, so we should go to the summary buffer.
1849         (when (prog1
1850                   (condition-case nil
1851                       (progn (set-buffer gnus-summary-buffer) t)
1852                     (error nil))
1853                 (setq ignored (when (not gnus-visible-headers)
1854                                 (cond ((stringp gnus-ignored-headers)
1855                                        gnus-ignored-headers)
1856                                       ((listp gnus-ignored-headers)
1857                                        (mapconcat 'identity
1858                                                   gnus-ignored-headers
1859                                                   "\\|"))))
1860                       visible (cond ((stringp gnus-visible-headers)
1861                                      gnus-visible-headers)
1862                                     ((and gnus-visible-headers
1863                                           (listp gnus-visible-headers))
1864                                      (mapconcat 'identity
1865                                                 gnus-visible-headers
1866                                                 "\\|")))))
1867           (set-buffer cur))
1868         (save-restriction
1869           ;; First we narrow to just the headers.
1870           (article-narrow-to-head)
1871           ;; Hide any "From " lines at the beginning of (mail) articles.
1872           (while (looking-at "From ")
1873             (forward-line 1))
1874           (unless (bobp)
1875             (delete-region (point-min) (point)))
1876           ;; Then treat the rest of the header lines.
1877           ;; Then we use the two regular expressions
1878           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1879           ;; select which header lines is to remain visible in the
1880           ;; article buffer.
1881           (while (re-search-forward "^[^ \t:]*:" nil t)
1882             (beginning-of-line)
1883             ;; Mark the rank of the header.
1884             (put-text-property
1885              (point) (1+ (point)) 'message-rank
1886              (if (or (and visible (looking-at visible))
1887                      (and ignored
1888                           (not (looking-at ignored))))
1889                  (gnus-article-header-rank)
1890                (+ 2 max)))
1891             (forward-line 1))
1892           (message-sort-headers-1)
1893           (when (setq beg (text-property-any
1894                            (point-min) (point-max) 'message-rank (+ 2 max)))
1895             ;; We delete the unwanted headers.
1896             (gnus-add-wash-type 'headers)
1897             (add-text-properties (point-min) (+ 5 (point-min))
1898                                  '(article-type headers dummy-invisible t))
1899             (delete-region beg (point-max))))))))
1900
1901 (defun article-hide-boring-headers (&optional arg)
1902   "Toggle hiding of headers that aren't very interesting.
1903 If given a negative prefix, always show; if given a positive prefix,
1904 always hide."
1905   (interactive (gnus-article-hidden-arg))
1906   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1907              (not gnus-show-all-headers))
1908     (save-excursion
1909       (save-restriction
1910         (let ((inhibit-read-only t)
1911               (inhibit-point-motion-hooks t))
1912           (article-narrow-to-head)
1913           (dolist (elem gnus-boring-article-headers)
1914             (goto-char (point-min))
1915             (cond
1916              ;; Hide empty headers.
1917              ((eq elem 'empty)
1918               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1919                 (forward-line -1)
1920                 (gnus-article-hide-text-type
1921                  (point-at-bol)
1922                  (progn
1923                    (end-of-line)
1924                    (if (re-search-forward "^[^ \t]" nil t)
1925                        (match-beginning 0)
1926                      (point-max)))
1927                  'boring-headers)))
1928              ;; Hide boring Newsgroups header.
1929              ((eq elem 'newsgroups)
1930               (when (gnus-string-equal
1931                      (gnus-fetch-field "newsgroups")
1932                      (gnus-group-real-name
1933                       (if (boundp 'gnus-newsgroup-name)
1934                           gnus-newsgroup-name
1935                         "")))
1936                 (gnus-article-hide-header "newsgroups")))
1937              ((eq elem 'to-address)
1938               (let ((to (message-fetch-field "to"))
1939                     (to-address
1940                      (gnus-parameter-to-address
1941                       (if (boundp 'gnus-newsgroup-name)
1942                           gnus-newsgroup-name ""))))
1943                 (when (and to to-address
1944                            (ignore-errors
1945                              (gnus-string-equal
1946                               ;; only one address in To
1947                               (nth 1 (mail-extract-address-components to))
1948                               to-address)))
1949                   (gnus-article-hide-header "to"))))
1950              ((eq elem 'to-list)
1951               (let ((to (message-fetch-field "to"))
1952                     (to-list
1953                      (gnus-parameter-to-list
1954                       (if (boundp 'gnus-newsgroup-name)
1955                           gnus-newsgroup-name ""))))
1956                 (when (and to to-list
1957                            (ignore-errors
1958                              (gnus-string-equal
1959                               ;; only one address in To
1960                               (nth 1 (mail-extract-address-components to))
1961                               to-list)))
1962                   (gnus-article-hide-header "to"))))
1963              ((eq elem 'cc-list)
1964               (let ((cc (message-fetch-field "cc"))
1965                     (to-list
1966                      (gnus-parameter-to-list
1967                       (if (boundp 'gnus-newsgroup-name)
1968                           gnus-newsgroup-name ""))))
1969                 (when (and cc to-list
1970                            (ignore-errors
1971                              (gnus-string-equal
1972                               ;; only one address in CC
1973                               (nth 1 (mail-extract-address-components cc))
1974                               to-list)))
1975                   (gnus-article-hide-header "cc"))))
1976              ((eq elem 'followup-to)
1977               (when (gnus-string-equal
1978                      (message-fetch-field "followup-to")
1979                      (message-fetch-field "newsgroups"))
1980                 (gnus-article-hide-header "followup-to")))
1981              ((eq elem 'reply-to)
1982               (if (gnus-group-find-parameter
1983                    gnus-newsgroup-name 'broken-reply-to)
1984                   (gnus-article-hide-header "reply-to")
1985                 (let ((from (message-fetch-field "from"))
1986                       (reply-to (message-fetch-field "reply-to")))
1987                   (when
1988                       (and
1989                        from reply-to
1990                        (ignore-errors
1991                          (equal
1992                           (sort (mapcar
1993                                  (lambda (x) (downcase (cadr x)))
1994                                  (mail-extract-address-components from t))
1995                                 'string<)
1996                           (sort (mapcar
1997                                  (lambda (x) (downcase (cadr x)))
1998                                  (mail-extract-address-components reply-to t))
1999                                 'string<))))
2000                     (gnus-article-hide-header "reply-to")))))
2001              ((eq elem 'date)
2002               (let ((date (with-current-buffer gnus-original-article-buffer
2003                             ;; If date in `gnus-article-buffer' is localized
2004                             ;; (`gnus-treat-date-user-defined'),
2005                             ;; `days-between' might fail.
2006                             (message-fetch-field "date"))))
2007                 (when (and date
2008                            (< (days-between (current-time-string) date)
2009                               4))
2010                   (gnus-article-hide-header "date"))))
2011              ((eq elem 'long-to)
2012               (let ((to (message-fetch-field "to"))
2013                     (cc (message-fetch-field "cc")))
2014                 (when (> (length to) 1024)
2015                   (gnus-article-hide-header "to"))
2016                 (when (> (length cc) 1024)
2017                   (gnus-article-hide-header "cc"))))
2018              ((eq elem 'many-to)
2019               (let ((to-count 0)
2020                     (cc-count 0))
2021                 (goto-char (point-min))
2022                 (while (re-search-forward "^to:" nil t)
2023                   (setq to-count (1+ to-count)))
2024                 (when (> to-count 1)
2025                   (while (> to-count 0)
2026                     (goto-char (point-min))
2027                     (save-restriction
2028                       (re-search-forward "^to:" nil nil to-count)
2029                       (forward-line -1)
2030                       (narrow-to-region (point) (point-max))
2031                       (gnus-article-hide-header "to"))
2032                     (setq to-count (1- to-count))))
2033                 (goto-char (point-min))
2034                 (while (re-search-forward "^cc:" nil t)
2035                   (setq cc-count (1+ cc-count)))
2036                 (when (> cc-count 1)
2037                   (while (> cc-count 0)
2038                     (goto-char (point-min))
2039                     (save-restriction
2040                       (re-search-forward "^cc:" nil nil cc-count)
2041                       (forward-line -1)
2042                       (narrow-to-region (point) (point-max))
2043                       (gnus-article-hide-header "cc"))
2044                     (setq cc-count (1- cc-count)))))))))))))
2045
2046 (defun gnus-article-hide-header (header)
2047   (save-excursion
2048     (goto-char (point-min))
2049     (when (re-search-forward (concat "^" header ":") nil t)
2050       (gnus-article-hide-text-type
2051        (point-at-bol)
2052        (progn
2053          (end-of-line)
2054          (if (re-search-forward "^[^ \t]" nil t)
2055              (match-beginning 0)
2056            (point-max)))
2057        'boring-headers))))
2058
2059 (defvar gnus-article-normalized-header-length 40
2060   "Length of normalized headers.")
2061
2062 (defun article-normalize-headers ()
2063   "Make all header lines 40 characters long."
2064   (interactive)
2065   (let ((inhibit-read-only t)
2066         column)
2067     (save-excursion
2068       (save-restriction
2069         (article-narrow-to-head)
2070         (while (not (eobp))
2071           (cond
2072            ((< (setq column (- (point-at-eol) (point)))
2073                gnus-article-normalized-header-length)
2074             (end-of-line)
2075             (insert (make-string
2076                      (- gnus-article-normalized-header-length column)
2077                      ? )))
2078            ((> column gnus-article-normalized-header-length)
2079             (gnus-put-text-property
2080              (progn
2081                (forward-char gnus-article-normalized-header-length)
2082                (point))
2083              (point-at-eol)
2084              'invisible t))
2085            (t
2086             ;; Do nothing.
2087             ))
2088           (forward-line 1))))))
2089
2090 (defun article-treat-dumbquotes ()
2091   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2092 Note that this function guesses whether a character is a sm*rtq**t* or
2093 not, so it should only be used interactively.
2094
2095 Sm*rtq**t*s are M****s***'s unilateral extension to the
2096 iso-8859-1 character map in an attempt to provide more quoting
2097 characters.  If you see something like \\222 or \\264 where
2098 you're expecting some kind of apostrophe or quotation mark, then
2099 try this wash."
2100   (interactive)
2101   (article-translate-strings gnus-article-dumbquotes-map))
2102
2103 (defun article-translate-characters (from to)
2104   "Translate all characters in the body of the article according to FROM and TO.
2105 FROM is a string of characters to translate from; to is a string of
2106 characters to translate to."
2107   (save-excursion
2108     (when (article-goto-body)
2109       (let ((inhibit-read-only t)
2110             (x (make-string 225 ?x))
2111             (i -1))
2112         (while (< (incf i) (length x))
2113           (aset x i i))
2114         (setq i 0)
2115         (while (< i (length from))
2116           (aset x (aref from i) (aref to i))
2117           (incf i))
2118         (translate-region (point) (point-max) x)))))
2119
2120 (defun article-translate-strings (map)
2121   "Translate all string in the body of the article according to MAP.
2122 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2123   (save-excursion
2124     (when (article-goto-body)
2125       (let ((inhibit-read-only t))
2126         (dolist (elem map)
2127           (save-excursion
2128             (while (search-forward (car elem) nil t)
2129               (replace-match (cadr elem)))))))))
2130
2131 (defun article-treat-overstrike ()
2132   "Translate overstrikes into bold text."
2133   (interactive)
2134   (save-excursion
2135     (when (article-goto-body)
2136       (let ((inhibit-read-only t))
2137         (while (search-forward "\b" nil t)
2138           (let ((next (char-after))
2139                 (previous (char-after (- (point) 2))))
2140             ;; We do the boldification/underlining by hiding the
2141             ;; overstrikes and putting the proper text property
2142             ;; on the letters.
2143             (cond
2144              ((eq next previous)
2145               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2146               (put-text-property (point) (1+ (point)) 'face 'bold))
2147              ((eq next ?_)
2148               (gnus-article-hide-text-type
2149                (1- (point)) (1+ (point)) 'overstrike)
2150               (put-text-property
2151                (- (point) 2) (1- (point)) 'face 'underline))
2152              ((eq previous ?_)
2153               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2154               (put-text-property
2155                (point) (1+ (point)) 'face 'underline)))))))))
2156
2157 (defun article-treat-ansi-sequences ()
2158   "Translate ANSI SGR control sequences into overlays or extents."
2159   (interactive)
2160   (save-excursion
2161     (when (article-goto-body)
2162       (let ((inhibit-read-only t))
2163         (ansi-color-apply-on-region (point) (point-max))))))
2164
2165 (defun gnus-article-treat-unfold-headers ()
2166   "Unfold folded message headers.
2167 Only the headers that fit into the current window width will be
2168 unfolded."
2169   (interactive)
2170   (gnus-with-article-headers
2171     (let (length)
2172       (while (not (eobp))
2173         (save-restriction
2174           (mail-header-narrow-to-field)
2175           (let* ((header (buffer-string))
2176                  (unfoldable
2177                   (or (equal gnus-article-unfold-long-headers t)
2178                       (and (stringp gnus-article-unfold-long-headers)
2179                            (string-match gnus-article-unfold-long-headers header)))))
2180             (with-temp-buffer
2181               (insert header)
2182               (goto-char (point-min))
2183               (while (re-search-forward "\n[\t ]" nil t)
2184                 (replace-match " " t t)))
2185             (setq length (- (point-max) (point-min) 1))
2186             (when (or unfoldable
2187                       (< length (window-width)))
2188               (while (re-search-forward "\n[\t ]" nil t)
2189                 (replace-match " " t t))))
2190           (goto-char (point-max)))))))
2191
2192 (defun gnus-article-treat-fold-headers ()
2193   "Fold message headers."
2194   (interactive)
2195   (gnus-with-article-headers
2196     (while (not (eobp))
2197       (save-restriction
2198         (mail-header-narrow-to-field)
2199         (mail-header-fold-field)
2200         (goto-char (point-max))))))
2201
2202 (defun gnus-treat-smiley ()
2203   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2204   (interactive)
2205   (gnus-with-article-buffer
2206     (if (memq 'smiley gnus-article-wash-types)
2207         (gnus-delete-images 'smiley)
2208       (article-goto-body)
2209       (let ((images (smiley-region (point) (point-max))))
2210         (when images
2211           (gnus-add-wash-type 'smiley)
2212           (dolist (image images)
2213             (gnus-add-image 'smiley image)))))))
2214
2215 (defun gnus-article-remove-images ()
2216   "Remove all images from the article buffer."
2217   (interactive)
2218   (gnus-with-article-buffer
2219     (dolist (elem gnus-article-image-alist)
2220       (gnus-delete-images (car elem)))))
2221
2222 (defun gnus-article-treat-fold-newsgroups ()
2223   "Unfold folded message headers.
2224 Only the headers that fit into the current window width will be
2225 unfolded."
2226   (interactive)
2227   (gnus-with-article-headers
2228     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2229       (save-restriction
2230         (mail-header-narrow-to-field)
2231         (while (re-search-forward ", *" nil t)
2232           (replace-match ", " t t))
2233         (mail-header-fold-field)
2234         (goto-char (point-max))))))
2235
2236 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2237   "Value of `truncate-lines' in Gnus Article buffer.
2238 Valid values are nil, t, `head', `first', `last', an integer or a
2239 predicate.  See Info node `(gnus)Customizing Articles'."
2240   :version "23.1" ;; No Gnus
2241   :group 'gnus-article
2242   ;; :link '(custom-manual "(gnus)Customizing Articles")
2243   :type 'boolean)
2244
2245 (defun gnus-article-toggle-truncate-lines (&optional arg)
2246   "Toggle whether to fold or truncate long lines in article the buffer.
2247 If ARG is non-nil and not a number, toggle
2248 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2249 long lines if and only if arg is positive."
2250   (interactive "P")
2251   (cond
2252    ((and (numberp arg) (> arg 0))
2253     (setq gnus-article-truncate-lines t))
2254    ((numberp arg)
2255     (setq gnus-article-truncate-lines nil))
2256    (arg
2257     (setq gnus-article-truncate-lines
2258           (not gnus-article-truncate-lines))))
2259   (gnus-with-article-buffer
2260     (cond
2261      ((and (numberp arg) (> arg 0))
2262       (setq truncate-lines nil))
2263      ((numberp arg)
2264       (setq truncate-lines t)))
2265     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2266     ;; `toggle-truncate-lines' needs an argument.
2267     (toggle-truncate-lines)))
2268
2269 (defun gnus-article-treat-body-boundary ()
2270   "Place a boundary line at the end of the headers."
2271   (interactive)
2272   (when (and gnus-body-boundary-delimiter
2273              (> (length gnus-body-boundary-delimiter) 0))
2274     (gnus-with-article-headers
2275       (goto-char (point-max))
2276       (let ((start (point)))
2277         (insert "X-Boundary: ")
2278         (gnus-add-text-properties start (point) '(invisible t intangible t))
2279         (insert (let (str)
2280                   (while (>= (1- (window-width)) (length str))
2281                     (setq str (concat str gnus-body-boundary-delimiter)))
2282                   (substring str 0 (1- (window-width))))
2283                 "\n")
2284         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2285
2286 (defun article-fill-long-lines ()
2287   "Fill lines that are wider than the window width."
2288   (interactive)
2289   (save-excursion
2290     (let ((inhibit-read-only t)
2291           (width (window-width (get-buffer-window (current-buffer)))))
2292       (save-restriction
2293         (article-goto-body)
2294         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2295           (while (not (eobp))
2296             (end-of-line)
2297             (when (>= (current-column) (min fill-column width))
2298               (narrow-to-region (min (1+ (point)) (point-max))
2299                                 (point-at-bol))
2300               (let ((goback (point-marker)))
2301                 (fill-paragraph nil)
2302                 (goto-char (marker-position goback)))
2303               (widen))
2304             (forward-line 1)))))))
2305
2306 (defun article-capitalize-sentences ()
2307   "Capitalize the first word in each sentence."
2308   (interactive)
2309   (save-excursion
2310     (let ((inhibit-read-only t)
2311           (paragraph-start "^[\n\^L]"))
2312       (article-goto-body)
2313       (while (not (eobp))
2314         (capitalize-word 1)
2315         (forward-sentence)))))
2316
2317 (defun article-remove-cr ()
2318   "Remove trailing CRs and then translate remaining CRs into LFs."
2319   (interactive)
2320   (save-excursion
2321     (let ((inhibit-read-only t))
2322       (goto-char (point-min))
2323       (while (re-search-forward "\r+$" nil t)
2324         (replace-match "" t t))
2325       (goto-char (point-min))
2326       (while (search-forward "\r" nil t)
2327         (replace-match "\n" t t)))))
2328
2329 (defun article-remove-trailing-blank-lines ()
2330   "Remove all trailing blank lines from the article."
2331   (interactive)
2332   (save-excursion
2333     (let ((inhibit-read-only t))
2334       (goto-char (point-max))
2335       (delete-region
2336        (point)
2337        (progn
2338          (while (and (not (bobp))
2339                      (looking-at "^[ \t]*$")
2340                      (not (gnus-annotation-in-region-p
2341                            (point) (point-at-eol))))
2342            (forward-line -1))
2343          (forward-line 1)
2344          (point))))))
2345
2346 (defvar gnus-face-properties-alist)
2347
2348 (defun article-display-face (&optional force)
2349   "Display any Face headers in the header."
2350   (interactive (list 'force))
2351   (let ((wash-face-p buffer-read-only))
2352     (gnus-with-article-headers
2353       ;; When displaying parts, this function can be called several times on
2354       ;; the same article, without any intended toggle semantic (as typing `W
2355       ;; D d' would have). So face deletion must occur only when we come from
2356       ;; an interactive command, that is when the *Article* buffer is
2357       ;; read-only.
2358       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2359           (gnus-delete-images 'face)
2360         (let ((from (message-fetch-field "from"))
2361               face faces)
2362           (save-current-buffer
2363             (when (and wash-face-p
2364                        (gnus-buffer-live-p gnus-original-article-buffer)
2365                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2366               (set-buffer gnus-original-article-buffer))
2367             (save-restriction
2368               (mail-narrow-to-head)
2369               (when (or force
2370                         ;; Check whether this face is censored.
2371                         (not (and gnus-article-x-face-too-ugly
2372                                   (or from
2373                                       (setq from (message-fetch-field "from")))
2374                                   (string-match gnus-article-x-face-too-ugly
2375                                                 from))))
2376                 (while (gnus-article-goto-header "Face")
2377                   (push (mail-header-field-value) faces)))))
2378           (when faces
2379             (goto-char (point-min))
2380             (let (png image)
2381               (unless (setq from (gnus-article-goto-header "from"))
2382                 (insert "From:")
2383                 (setq from (point))
2384                 (insert " [no `from' set]\n"))
2385               (while faces
2386                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2387                   (setq image
2388                         (apply 'gnus-create-image png 'png t
2389                                (cdr (assq 'png gnus-face-properties-alist))))
2390                   (goto-char from)
2391                   (gnus-add-wash-type 'face)
2392                   (gnus-add-image 'face image)
2393                   (gnus-put-image image nil 'face))))))))))
2394
2395 (defun article-display-x-face (&optional force)
2396   "Look for an X-Face header and display it if present."
2397   (interactive (list 'force))
2398   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2399     (gnus-with-article-headers
2400       ;; Delete the old process, if any.
2401       (when (process-status "article-x-face")
2402         (delete-process "article-x-face"))
2403       ;; See the comment in `article-display-face'.
2404       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2405           ;; We have already displayed X-Faces, so we remove them
2406           ;; instead.
2407           (gnus-delete-images 'xface)
2408         ;; Display X-Faces.
2409         (let ((from (message-fetch-field "from"))
2410               x-faces face)
2411           (save-current-buffer
2412             (when (and wash-face-p
2413                        (gnus-buffer-live-p gnus-original-article-buffer)
2414                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2415               ;; If type `W f', use gnus-original-article-buffer,
2416               ;; otherwise use the current buffer because displaying
2417               ;; RFC822 parts calls this function too.
2418               (set-buffer gnus-original-article-buffer))
2419             (save-restriction
2420               (mail-narrow-to-head)
2421               (and gnus-article-x-face-command
2422                    (or force
2423                        ;; Check whether this face is censored.
2424                        (not (and gnus-article-x-face-too-ugly
2425                                  (or from
2426                                      (setq from (message-fetch-field "from")))
2427                                  (string-match gnus-article-x-face-too-ugly
2428                                                from))))
2429                    (while (gnus-article-goto-header "X-Face")
2430                      (push (mail-header-field-value) x-faces)))))
2431           (when x-faces
2432             ;; We display the face.
2433             (cond ((functionp gnus-article-x-face-command)
2434                    ;; The command is a lisp function, so we call it.
2435                    (mapc gnus-article-x-face-command x-faces))
2436                   ((stringp gnus-article-x-face-command)
2437                    ;; The command is a string, so we interpret the command
2438                    ;; as a, well, command, and fork it off.
2439                    (let ((process-connection-type nil))
2440                      (gnus-set-process-query-on-exit-flag
2441                       (start-process
2442                        "article-x-face" nil shell-file-name
2443                        shell-command-switch gnus-article-x-face-command)
2444                       nil)
2445                      ;; Sending multiple EOFs to xv doesn't work,
2446                      ;; so we only do a single external face.
2447                      (with-temp-buffer
2448                        (insert (car x-faces))
2449                        (process-send-region "article-x-face"
2450                                             (point-min) (point-max)))
2451                      (process-send-eof "article-x-face")))
2452                   (t
2453                    (error "`%s' set to `%s' is not a function"
2454                           gnus-article-x-face-command
2455                           'gnus-article-x-face-command)))))))))
2456
2457 (defun article-decode-mime-words ()
2458   "Decode all MIME-encoded words in the article."
2459   (interactive)
2460   (gnus-with-article-buffer
2461     (let ((inhibit-point-motion-hooks t)
2462           (mail-parse-charset gnus-newsgroup-charset)
2463           (mail-parse-ignored-charsets
2464            (with-current-buffer gnus-summary-buffer
2465              gnus-newsgroup-ignored-charsets)))
2466       (mail-decode-encoded-word-region (point-min) (point-max)))))
2467
2468 (defun article-decode-charset (&optional prompt)
2469   "Decode charset-encoded text in the article.
2470 If PROMPT (the prefix), prompt for a coding system to use."
2471   (interactive "P")
2472   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2473         (inhibit-read-only t)
2474         (mail-parse-charset gnus-newsgroup-charset)
2475         (mail-parse-ignored-charsets
2476          (save-excursion (condition-case nil
2477                              (set-buffer gnus-summary-buffer)
2478                            (error))
2479                          gnus-newsgroup-ignored-charsets))
2480         ct cte ctl charset format)
2481     (save-excursion
2482       (save-restriction
2483         (article-narrow-to-head)
2484         (setq ct (message-fetch-field "Content-Type" t)
2485               cte (message-fetch-field "Content-Transfer-Encoding" t)
2486               ctl (and ct (mail-header-parse-content-type ct))
2487               charset (cond
2488                        (prompt
2489                         (mm-read-coding-system "Charset to decode: "))
2490                        (ctl
2491                         (mail-content-type-get ctl 'charset)))
2492               format (and ctl (mail-content-type-get ctl 'format)))
2493         (when cte
2494           (setq cte (mail-header-strip cte)))
2495         (if (and ctl (not (string-match "/" (car ctl))))
2496             (setq ctl nil))
2497         (goto-char (point-max)))
2498       (forward-line 1)
2499       (save-restriction
2500         (narrow-to-region (point) (point-max))
2501         (when (and (eq mail-parse-charset 'gnus-decoded)
2502                    (eq (mm-body-7-or-8) '8bit))
2503           ;; The text code could have been decoded.
2504           (setq charset mail-parse-charset))
2505         (when (and (or (not ctl)
2506                        (equal (car ctl) "text/plain"))
2507                    (not format)) ;; article with format will decode later.
2508           (mm-decode-body
2509            charset (and cte (intern (downcase
2510                                      (gnus-strip-whitespace cte))))
2511            (car ctl)))))))
2512
2513 (defun article-decode-encoded-words ()
2514   "Remove encoded-word encoding from headers."
2515   (let ((inhibit-point-motion-hooks t)
2516         (mail-parse-charset gnus-newsgroup-charset)
2517         (mail-parse-ignored-charsets
2518          (save-excursion (condition-case nil
2519                              (set-buffer gnus-summary-buffer)
2520                            (error))
2521                          gnus-newsgroup-ignored-charsets))
2522         (inhibit-read-only t)
2523         end start)
2524     (goto-char (point-min))
2525     (when (search-forward "\n\n" nil 'move)
2526       (forward-line -1))
2527     (setq end (point))
2528     (while (not (bobp))
2529       (while (progn
2530                (forward-line -1)
2531                (and (not (bobp))
2532                     (memq (char-after) '(?\t ? )))))
2533       (setq start (point))
2534       (if (looking-at "\
2535 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2536 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2537           (funcall gnus-decode-address-function start end)
2538         (funcall gnus-decode-header-function start end))
2539       (goto-char (setq end start)))))
2540
2541 (defun article-decode-group-name ()
2542   "Decode group names in Newsgroups, Followup-To and Xref headers."
2543   (let ((inhibit-point-motion-hooks t)
2544         (inhibit-read-only t)
2545         (method (gnus-find-method-for-group gnus-newsgroup-name))
2546         regexp)
2547     (when (and (or gnus-group-name-charset-method-alist
2548                    gnus-group-name-charset-group-alist)
2549                (gnus-buffer-live-p gnus-original-article-buffer))
2550       (save-restriction
2551         (article-narrow-to-head)
2552         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2553           (with-current-buffer gnus-original-article-buffer
2554             (goto-char (point-min)))
2555           (setq regexp (concat "^" header
2556                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2557           (while (re-search-forward regexp nil t)
2558             (replace-match (save-match-data
2559                              (gnus-decode-newsgroups
2560                               ;; XXX how to use data in article buffer?
2561                               (with-current-buffer gnus-original-article-buffer
2562                                 (re-search-forward regexp nil t)
2563                                 (match-string 1))
2564                               gnus-newsgroup-name method))
2565                            t t nil 1))
2566           (goto-char (point-min)))))))
2567
2568 (autoload 'idna-to-unicode "idna")
2569
2570 (defun article-decode-idna-rhs ()
2571   "Decode IDNA strings in RHS in various headers in current buffer.
2572 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2573 Mail-Reply-To: and Mail-Followup-To:."
2574   (when gnus-use-idna
2575     (save-restriction
2576       (let ((inhibit-point-motion-hooks t)
2577             (inhibit-read-only t))
2578         (article-narrow-to-head)
2579         (goto-char (point-min))
2580         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2581           (let (ace unicode)
2582             (when (save-match-data
2583                     (and (setq ace (match-string 1))
2584                          (save-excursion
2585                            (and (re-search-backward "^[^ \t]" nil t)
2586                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2587                          (setq unicode (idna-to-unicode ace))))
2588               (unless (string= ace unicode)
2589                 (replace-match unicode nil nil nil 1)))))))))
2590
2591 (defun article-de-quoted-unreadable (&optional force read-charset)
2592   "Translate a quoted-printable-encoded article.
2593 If FORCE, decode the article whether it is marked as quoted-printable
2594 or not.
2595 If READ-CHARSET, ask for a coding system."
2596   (interactive (list 'force current-prefix-arg))
2597   (save-excursion
2598     (let ((inhibit-read-only t) type charset)
2599       (if (gnus-buffer-live-p gnus-original-article-buffer)
2600           (with-current-buffer gnus-original-article-buffer
2601             (setq type
2602                   (gnus-fetch-field "content-transfer-encoding"))
2603             (let* ((ct (gnus-fetch-field "content-type"))
2604                    (ctl (and ct (mail-header-parse-content-type ct))))
2605               (setq charset (and ctl
2606                                  (mail-content-type-get ctl 'charset)))
2607               (if (stringp charset)
2608                   (setq charset (intern (downcase charset)))))))
2609       (if read-charset
2610           (setq charset (mm-read-coding-system "Charset: " charset)))
2611       (unless charset
2612         (setq charset gnus-newsgroup-charset))
2613       (when (or force
2614                 (and type (let ((case-fold-search t))
2615                             (string-match "quoted-printable" type))))
2616         (article-goto-body)
2617         (quoted-printable-decode-region
2618          (point) (point-max) (mm-charset-to-coding-system charset))))))
2619
2620 (defun article-de-base64-unreadable (&optional force read-charset)
2621   "Translate a base64 article.
2622 If FORCE, decode the article whether it is marked as base64 not.
2623 If READ-CHARSET, ask for a coding system."
2624   (interactive (list 'force current-prefix-arg))
2625   (save-excursion
2626     (let ((inhibit-read-only t) type charset)
2627       (if (gnus-buffer-live-p gnus-original-article-buffer)
2628           (with-current-buffer gnus-original-article-buffer
2629             (setq type
2630                   (gnus-fetch-field "content-transfer-encoding"))
2631             (let* ((ct (gnus-fetch-field "content-type"))
2632                    (ctl (and ct (mail-header-parse-content-type ct))))
2633               (setq charset (and ctl
2634                                  (mail-content-type-get ctl 'charset)))
2635               (if (stringp charset)
2636                   (setq charset (intern (downcase charset)))))))
2637       (if read-charset
2638           (setq charset (mm-read-coding-system "Charset: " charset)))
2639       (unless charset
2640         (setq charset gnus-newsgroup-charset))
2641       (when (or force
2642                 (and type (let ((case-fold-search t))
2643                             (string-match "base64" type))))
2644         (article-goto-body)
2645         (save-restriction
2646           (narrow-to-region (point) (point-max))
2647           (base64-decode-region (point-min) (point-max))
2648           (mm-decode-coding-region
2649            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2650
2651 (eval-when-compile
2652   (require 'rfc1843))
2653
2654 (defun article-decode-HZ ()
2655   "Translate a HZ-encoded article."
2656   (interactive)
2657   (require 'rfc1843)
2658   (save-excursion
2659     (let ((inhibit-read-only t))
2660       (rfc1843-decode-region (point-min) (point-max)))))
2661
2662 (defun article-unsplit-urls ()
2663   "Remove the newlines that some other mailers insert into URLs."
2664   (interactive)
2665   (save-excursion
2666     (let ((inhibit-read-only t))
2667       (goto-char (point-min))
2668       (while (re-search-forward
2669               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2670         (replace-match "\\1\\3" t)))
2671     (when (interactive-p)
2672       (gnus-treat-article nil))))
2673
2674
2675 (defun article-wash-html (&optional read-charset)
2676   "Format an HTML article.
2677 If READ-CHARSET, ask for a coding system.  If it is a number, the
2678 charset defined in `gnus-summary-show-article-charset-alist' is used."
2679   (interactive "P")
2680   (save-excursion
2681     (let ((inhibit-read-only t)
2682           charset)
2683       (if read-charset
2684           (if (or (and (numberp read-charset)
2685                        (setq charset
2686                              (cdr
2687                               (assq read-charset
2688                                     gnus-summary-show-article-charset-alist))))
2689                   (setq charset (mm-read-coding-system "Charset: ")))
2690               (let ((gnus-summary-show-article-charset-alist
2691                      (list (cons 1 charset))))
2692                 (with-current-buffer gnus-summary-buffer
2693                   (gnus-summary-show-article 1)))
2694             (error "No charset is given"))
2695         (when (gnus-buffer-live-p gnus-original-article-buffer)
2696           (with-current-buffer gnus-original-article-buffer
2697             (let* ((ct (gnus-fetch-field "content-type"))
2698                    (ctl (and ct (mail-header-parse-content-type ct))))
2699               (setq charset (and ctl
2700                                  (mail-content-type-get ctl 'charset)))
2701               (when (stringp charset)
2702                 (setq charset (intern (downcase charset)))))))
2703         (unless charset
2704           (setq charset gnus-newsgroup-charset)))
2705       (article-goto-body)
2706       (save-window-excursion
2707         (save-restriction
2708           (narrow-to-region (point) (point-max))
2709           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2710                  (entry (assq func mm-text-html-washer-alist)))
2711             (when entry
2712               (setq func (cdr entry)))
2713             (cond
2714              ((functionp func)
2715               (funcall func))
2716              (t
2717               (apply (car func) (cdr func))))))))))
2718
2719 ;; External.
2720 (declare-function w3-region "ext:w3-display" (st nd))
2721
2722 (defun gnus-article-wash-html-with-w3 ()
2723   "Wash the current buffer with w3."
2724   (mm-setup-w3)
2725   (let ((w3-strict-width (window-width))
2726         (url-standalone-mode t)
2727         (url-gateway-unplugged t)
2728         (w3-honor-stylesheets nil))
2729     (condition-case ()
2730         (w3-region (point-min) (point-max))
2731       (error))))
2732
2733 ;; External.
2734 (declare-function w3m-region "ext:w3m" (start end &optional url charset))
2735
2736 (defun gnus-article-wash-html-with-w3m ()
2737   "Wash the current buffer with emacs-w3m."
2738   (mm-setup-w3m)
2739   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2740         w3m-force-redisplay)
2741     (w3m-region (point-min) (point-max)))
2742   ;; Put the mark meaning this part was rendered by emacs-w3m.
2743   (put-text-property (point-min) (point-max) 'mm-inline-text-html-with-w3m t)
2744   (when (and mm-inline-text-html-with-w3m-keymap
2745              (boundp 'w3m-minor-mode-map)
2746              w3m-minor-mode-map)
2747     (if (and (boundp 'w3m-link-map)
2748              w3m-link-map)
2749         (let* ((start (point-min))
2750                (end (point-max))
2751                (on (get-text-property start 'w3m-href-anchor))
2752                (map (copy-keymap w3m-link-map))
2753                next)
2754           (set-keymap-parent map w3m-minor-mode-map)
2755           (while (< start end)
2756             (if on
2757                 (progn
2758                   (setq next (or (text-property-any start end
2759                                                     'w3m-href-anchor nil)
2760                                  end))
2761                   (put-text-property start next 'keymap map))
2762               (setq next (or (text-property-not-all start end
2763                                                     'w3m-href-anchor nil)
2764                              end))
2765               (put-text-property start next 'keymap w3m-minor-mode-map))
2766             (setq start next
2767                   on (not on))))
2768       (put-text-property (point-min) (point-max) 'keymap w3m-minor-mode-map))))
2769
2770 (defvar charset) ;; Bound by `article-wash-html'.
2771
2772 (defun gnus-article-wash-html-with-w3m-standalone ()
2773   "Wash the current buffer with w3m."
2774   (if (mm-w3m-standalone-supports-m17n-p)
2775       (progn
2776         (unless (mm-coding-system-p charset) ;; Bound by `article-wash-html'.
2777           ;; The default.
2778           (setq charset 'iso-8859-1))
2779         (let ((coding-system-for-write charset)
2780               (coding-system-for-read charset))
2781           (call-process-region
2782            (point-min) (point-max)
2783            "w3m" t t nil "-dump" "-T" "text/html"
2784            "-I" (symbol-name charset) "-O" (symbol-name charset))))
2785     (mm-inline-wash-with-stdin nil "w3m" "-dump" "-T" "text/html")))
2786
2787 (defvar gnus-article-browse-html-temp-list nil
2788   "List of temporary files created by `gnus-article-browse-html-parts'.
2789 Internal variable.")
2790
2791 (defcustom gnus-article-browse-delete-temp 'ask
2792   "What to do with temporary files from `gnus-article-browse-html-parts'.
2793 If nil, don't delete temporary files.  If it is t, delete them on
2794 exit from the summary buffer.  If it is the symbol `file', query
2795 on each file, if it is `ask' ask once when exiting from the
2796 summary buffer."
2797   :group 'gnus-article
2798   :version "23.1" ;; No Gnus
2799   :type '(choice (const :tag "Don't delete" nil)
2800                  (const :tag "Don't ask" t)
2801                  (const :tag "Ask" ask)
2802                  (const :tag "Ask for each file" file)))
2803
2804 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2805
2806 (defun gnus-article-browse-delete-temp-files (&optional how)
2807   "Delete temp-files created by `gnus-article-browse-html-parts'."
2808   (when (and gnus-article-browse-html-temp-list
2809              (progn
2810                (or how (setq how gnus-article-browse-delete-temp))
2811                (if (eq how 'ask)
2812                    (let ((files (length gnus-article-browse-html-temp-list)))
2813                      (gnus-y-or-n-p (format
2814                                      "Delete all %s temporary HTML file%s? "
2815                                      files
2816                                      (if (> files 1) "s" ""))))
2817                  how)))
2818     (dolist (file gnus-article-browse-html-temp-list)
2819       (cond ((file-directory-p file)
2820              (when (or (not (eq how 'file))
2821                        (gnus-y-or-n-p
2822                         (format
2823                          "Delete temporary HTML file(s) in directory `%s'? "
2824                          (file-name-as-directory file))))
2825                (gnus-delete-directory file)))
2826             ((file-exists-p file)
2827              (when (or (not (eq how 'file))
2828                        (gnus-y-or-n-p
2829                         (format "Delete temporary HTML file `%s'? " file)))
2830                (delete-file file)))))
2831     ;; Also remove file from the list when not deleted or if file doesn't
2832     ;; exist anymore.
2833     (setq gnus-article-browse-html-temp-list nil))
2834   gnus-article-browse-html-temp-list)
2835
2836 (defun gnus-article-browse-html-save-cid-content (cid handles directory)
2837   "Find CID content in HANDLES and save it in a file in DIRECTORY.
2838 Return file name."
2839   (save-match-data
2840     (let (file type)
2841       (catch 'found
2842         (dolist (handle handles)
2843           (cond
2844            ((not (listp handle)))
2845            ((equal (mm-handle-media-supertype handle) "multipart")
2846             (when (setq file (gnus-article-browse-html-save-cid-content
2847                               cid handle directory))
2848               (throw 'found file)))
2849            ((equal (concat "<" cid ">") (mm-handle-id handle))
2850             (setq file
2851                   (expand-file-name
2852                    (or (mail-content-type-get
2853                         (mm-handle-disposition handle) 'filename)
2854                        (mail-content-type-get
2855                         (setq type (mm-handle-type handle)) 'name)
2856                        (concat
2857                         (make-temp-name "cid")
2858                         (car (rassoc (car type) mailcap-mime-extensions))))
2859                    directory))
2860             (mm-save-part-to-file handle file)
2861             (throw 'found file))))))))
2862
2863 (defun gnus-article-browse-html-parts (list &optional header)
2864   "View all \"text/html\" parts from LIST.
2865 Recurse into multiparts.  The optional HEADER that should be a decoded
2866 message header will be added to the bodies of the \"text/html\" parts."
2867   ;; Internal function used by `gnus-article-browse-html-article'.
2868   (let (type file charset content cid-dir tmp-file showed)
2869     ;; Find and show the html-parts.
2870     (dolist (handle list)
2871       ;; If HTML, show it:
2872       (cond ((not (listp handle)))
2873             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2874                  (and (equal (car type) "message/external-body")
2875                       (or header
2876                           (setq file (or (mail-content-type-get type 'name)
2877                                          (mail-content-type-get
2878                                           (mm-handle-disposition handle)
2879                                           'filename))))
2880                       (or (mm-handle-cache handle)
2881                           (condition-case code
2882                               (progn (mm-extern-cache-contents handle) t)
2883                             (error
2884                              (gnus-message 3 "%s" (error-message-string code))
2885                              (when (>= gnus-verbose 3) (sit-for 2))
2886                              nil)))
2887                       (progn
2888                         (setq handle (mm-handle-cache handle)
2889                               type (mm-handle-type handle))
2890                         (equal (car type) "text/html"))))
2891              (setq charset (mail-content-type-get type 'charset)
2892                    content (mm-get-part handle))
2893              (with-temp-buffer
2894                (if (eq charset 'gnus-decoded)
2895                    (mm-enable-multibyte)
2896                  (mm-disable-multibyte))
2897                (insert content)
2898                ;; resolve cid contents
2899                (let ((case-fold-search t)
2900                      cid-file)
2901                  (goto-char (point-min))
2902                  (while (re-search-forward "\
2903 <img[\t\n ]+\\(?:[^\t\n >]+[\t\n ]+\\)*src=\"\\(cid:\\([^\"]+\\)\\)\""
2904                                            nil t)
2905                    (unless cid-dir
2906                      (setq cid-dir (mm-make-temp-file "cid" t))
2907                      (add-to-list 'gnus-article-browse-html-temp-list cid-dir))
2908                    (setq file nil
2909                          content nil)
2910                    (when (setq cid-file
2911                                (gnus-article-browse-html-save-cid-content
2912                                 (match-string 2)
2913                                 (with-current-buffer gnus-article-buffer
2914                                   gnus-article-mime-handles)
2915                                 cid-dir))
2916                      (replace-match (concat "file://" cid-file)
2917                                     nil nil nil 1))))
2918                (unless content (setq content (buffer-string))))
2919              (when (or charset header (not file))
2920                (setq tmp-file (mm-make-temp-file
2921                                ;; Do we need to care for 8.3 filenames?
2922                                "mm-" nil ".html")))
2923              ;; Add a meta html tag to specify charset and a header.
2924              (cond
2925               (header
2926                (let (title eheader body hcharset coding force-charset)
2927                  (with-temp-buffer
2928                    (mm-enable-multibyte)
2929                    (setq case-fold-search t)
2930                    (insert header "\n")
2931                    (setq title (message-fetch-field "subject"))
2932                    (goto-char (point-min))
2933                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2934                      (replace-match (cond ((match-beginning 1) "&lt;")
2935                                           ((match-beginning 2) "&gt;")
2936                                           (t "&amp;"))))
2937                    (goto-char (point-min))
2938                    (insert "<pre>\n")
2939                    (goto-char (point-max))
2940                    (insert "</pre>\n<hr>\n")
2941                    ;; We have to examine charset one by one since
2942                    ;; charset specified in parts might be different.
2943                    (if (eq charset 'gnus-decoded)
2944                        (setq charset 'utf-8
2945                              eheader (mm-encode-coding-string (buffer-string)
2946                                                               charset)
2947                              title (when title
2948                                      (mm-encode-coding-string title charset))
2949                              body (mm-encode-coding-string content charset)
2950                              force-charset t)
2951                      (setq hcharset (mm-find-mime-charset-region (point-min)
2952                                                                  (point-max)))
2953                      (cond ((= (length hcharset) 1)
2954                             (setq hcharset (car hcharset)
2955                                   coding (mm-charset-to-coding-system
2956                                           hcharset)))
2957                            ((> (length hcharset) 1)
2958                             (setq hcharset 'utf-8
2959                                   coding hcharset)))
2960                      (if coding
2961                          (if charset
2962                              (progn
2963                                (setq body
2964                                      (mm-charset-to-coding-system charset))
2965                                (if (eq coding body)
2966                                    (setq eheader (mm-encode-coding-string
2967                                                   (buffer-string) coding)
2968                                          title (when title
2969                                                  (mm-encode-coding-string
2970                                                   title coding))
2971                                          body content)
2972                                  (setq charset 'utf-8
2973                                        eheader (mm-encode-coding-string
2974                                                 (buffer-string) charset)
2975                                        title (when title
2976                                                (mm-encode-coding-string
2977                                                 title charset))
2978                                        body (mm-encode-coding-string
2979                                              (mm-decode-coding-string
2980                                               content body)
2981                                              charset)
2982                                        force-charset t)))
2983                            (setq charset hcharset
2984                                  eheader (mm-encode-coding-string
2985                                           (buffer-string) coding)
2986                                  title (when title
2987                                          (mm-encode-coding-string
2988                                           title coding))
2989                                  body content))
2990                        (setq eheader (mm-string-as-unibyte (buffer-string))
2991                              body content)))
2992                    (erase-buffer)
2993                    (mm-disable-multibyte)
2994                    (insert body)
2995                    (when charset
2996                      (mm-add-meta-html-tag handle charset force-charset))
2997                    (when title
2998                      (goto-char (point-min))
2999                      (unless (search-forward "<title>" nil t)
3000                        (re-search-forward "<head>\\s-*" nil t)
3001                        (insert "<title>" title "</title>\n")))
3002                    (goto-char (point-min))
3003                    (or (re-search-forward
3004                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
3005                        (re-search-forward
3006                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
3007                    (insert eheader)
3008                    (mm-write-region (point-min) (point-max)
3009                                     tmp-file nil nil nil 'binary t))))
3010               (charset
3011                (mm-with-unibyte-buffer
3012                  (insert (if (eq charset 'gnus-decoded)
3013                              (mm-encode-coding-string content
3014                                                       (setq charset 'utf-8))
3015                            content))
3016                  (if (or (mm-add-meta-html-tag handle charset)
3017                          (not file))
3018                      (mm-write-region (point-min) (point-max)
3019                                       tmp-file nil nil nil 'binary t)
3020                    (setq tmp-file nil))))
3021               (tmp-file
3022                (mm-save-part-to-file handle tmp-file)))
3023              (when tmp-file
3024                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
3025              (add-hook 'gnus-summary-prepare-exit-hook
3026                        'gnus-article-browse-delete-temp-files)
3027              (add-hook 'gnus-exit-gnus-hook
3028                        (lambda  ()
3029                          (gnus-article-browse-delete-temp-files t)))
3030              ;; FIXME: Warn if there's an <img> tag?
3031              (browse-url-of-file (or tmp-file (expand-file-name file)))
3032              (setq showed t))
3033             ;; If multipart, recurse
3034             ((equal (mm-handle-media-supertype handle) "multipart")
3035              (when (gnus-article-browse-html-parts handle header)
3036                (setq showed t)))
3037             ((equal (mm-handle-media-type handle) "message/rfc822")
3038              (mm-with-multibyte-buffer
3039                (mm-insert-part handle)
3040                (setq handle (mm-dissect-buffer t t))
3041                (when (and (bufferp (car handle))
3042                           (stringp (car (mm-handle-type handle))))
3043                  (setq handle (list handle)))
3044                (when header
3045                  (article-decode-encoded-words)
3046                  (let ((gnus-visible-headers
3047                         (or (get 'gnus-visible-headers 'standard-value)
3048                             gnus-visible-headers)))
3049                    (article-hide-headers))
3050                  (goto-char (point-min))
3051                  (search-forward "\n\n" nil 'move)
3052                  (skip-chars-backward "\t\n ")
3053                  (setq header (buffer-substring (point-min) (point)))))
3054              (when (prog1
3055                        (gnus-article-browse-html-parts handle header)
3056                      (mm-destroy-parts handle))
3057                (setq showed t)))))
3058     showed))
3059
3060 (defun gnus-article-browse-html-article (&optional arg)
3061   "View \"text/html\" parts of the current article with a WWW browser.
3062 Inline images embedded in a message using the cid scheme, as they are
3063 generally considered to be safe, will be processed properly.
3064 The message header is added to the beginning of every html part unless
3065 the prefix argument ARG is given.
3066
3067 Warning: Spammers use links to images (using the http scheme) in HTML
3068 articles to verify whether you have read the message.  As
3069 `gnus-article-browse-html-article' passes the HTML content to the
3070 browser without eliminating these \"web bugs\" you should only
3071 use it for mails from trusted senders.
3072
3073 If you always want to display HTML parts in the browser, set
3074 `mm-text-html-renderer' to nil.
3075
3076 This command creates temporary files to pass HTML contents including
3077 images if any to the browser, and deletes them when exiting the group
3078 \(if you want)."
3079   ;; Cf. `mm-w3m-safe-url-regexp'
3080   (interactive "P")
3081   (if arg
3082       (gnus-summary-show-article)
3083     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
3084                                     gnus-visible-headers))
3085           ;; As we insert a <hr>, there's no need for the body boundary.
3086           (gnus-treat-body-boundary nil))
3087       (gnus-summary-show-article)))
3088   (with-current-buffer gnus-article-buffer
3089     (let ((header (unless arg
3090                     (save-restriction
3091                       (widen)
3092                       (buffer-substring-no-properties
3093                        (goto-char (point-min))
3094                        (if (search-forward "\n\n" nil t)
3095                            (match-beginning 0)
3096                          (goto-char (point-max))
3097                          (skip-chars-backward "\t\n ")
3098                          (point))))))
3099           parts)
3100       (set-buffer gnus-original-article-buffer)
3101       (setq parts (mm-dissect-buffer t t))
3102       ;; If singlepart, enforce a list.
3103       (when (and (bufferp (car parts))
3104                  (stringp (car (mm-handle-type parts))))
3105         (setq parts (list parts)))
3106       ;; Process the list
3107       (unless (gnus-article-browse-html-parts parts header)
3108         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3109       (mm-destroy-parts parts)
3110       (unless arg
3111         (gnus-summary-show-article)))))
3112
3113 (defun article-hide-list-identifiers ()
3114   "Remove list identifies from the Subject header.
3115 The `gnus-list-identifiers' variable specifies what to do."
3116   (interactive)
3117   (let ((inhibit-point-motion-hooks t)
3118         (regexp (if (consp gnus-list-identifiers)
3119                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3120                   gnus-list-identifiers))
3121         (inhibit-read-only t))
3122     (when regexp
3123       (save-excursion
3124         (save-restriction
3125           (article-narrow-to-head)
3126           (goto-char (point-min))
3127           (while (re-search-forward
3128                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3129                   nil t)
3130             (delete-region (match-beginning 2) (match-end 0))
3131             (beginning-of-line))
3132           (when (re-search-forward
3133                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3134             (delete-region (match-beginning 1) (match-end 1))))))))
3135
3136 (defun article-hide-pem (&optional arg)
3137   "Toggle hiding of any PEM headers and signatures in the current article.
3138 If given a negative prefix, always show; if given a positive prefix,
3139 always hide."
3140   (interactive (gnus-article-hidden-arg))
3141   (unless (gnus-article-check-hidden-text 'pem arg)
3142     (save-excursion
3143       (let ((inhibit-read-only t) end)
3144         (goto-char (point-min))
3145         ;; Hide the horrendously ugly "header".
3146         (when (and (search-forward
3147                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3148                     nil t)
3149                    (setq end (1+ (match-beginning 0))))
3150           (gnus-add-wash-type 'pem)
3151           (gnus-article-hide-text-type
3152            end
3153            (if (search-forward "\n\n" nil t)
3154                (match-end 0)
3155              (point-max))
3156            'pem)
3157           ;; Hide the trailer as well
3158           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3159                                 nil t)
3160             (gnus-article-hide-text-type
3161              (match-beginning 0) (match-end 0) 'pem)))))))
3162
3163 (defun article-strip-banner ()
3164   "Strip the banners specified by the `banner' group parameter and by
3165 `gnus-article-address-banner-alist'."
3166   (interactive)
3167   (save-excursion
3168     (save-restriction
3169       (let ((inhibit-point-motion-hooks t))
3170         (when (gnus-parameter-banner gnus-newsgroup-name)
3171           (article-really-strip-banner
3172            (gnus-parameter-banner gnus-newsgroup-name)))
3173         (when gnus-article-address-banner-alist
3174           ;; Note that the From header is decoded here, so it is
3175           ;; required that the *-extract-address-components function
3176           ;; supports non-ASCII text.
3177           (let ((from (save-restriction
3178                         (widen)
3179                         (article-narrow-to-head)
3180                         (mail-fetch-field "from"))))
3181             (when (and from
3182                        (setq from
3183                              (cadr (funcall gnus-extract-address-components
3184                                             from))))
3185               (catch 'found
3186                 (dolist (pair gnus-article-address-banner-alist)
3187                   (when (string-match (car pair) from)
3188                     (throw 'found
3189                            (article-really-strip-banner (cdr pair)))))))))))))
3190
3191 (defun article-really-strip-banner (banner)
3192   "Strip the banner specified by the argument."
3193   (save-excursion
3194     (save-restriction
3195       (let ((inhibit-point-motion-hooks t)
3196             (gnus-signature-limit nil)
3197             (inhibit-read-only t))
3198         (article-goto-body)
3199         (cond
3200          ((eq banner 'signature)
3201           (when (gnus-article-narrow-to-signature)
3202             (widen)
3203             (forward-line -1)
3204             (delete-region (point) (point-max))))
3205          ((symbolp banner)
3206           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3207               (while (re-search-forward banner nil t)
3208                 (delete-region (match-beginning 0) (match-end 0)))))
3209          ((stringp banner)
3210           (while (re-search-forward banner nil t)
3211             (delete-region (match-beginning 0) (match-end 0)))))))))
3212
3213 (defun article-babel ()
3214   "Translate article using an online translation service."
3215   (interactive)
3216   (require 'babel)
3217   (gnus-with-article-buffer
3218     (when (article-goto-body)
3219       (let* ((start (point))
3220              (end (point-max))
3221              (orig (buffer-substring start end))
3222              (trans (babel-as-string orig)))
3223         (save-restriction
3224           (narrow-to-region start end)
3225           (delete-region start end)
3226           (insert trans))))))
3227
3228 (defun article-hide-signature (&optional arg)
3229   "Hide the signature in the current article.
3230 If given a negative prefix, always show; if given a positive prefix,
3231 always hide."
3232   (interactive (gnus-article-hidden-arg))
3233   (unless (gnus-article-check-hidden-text 'signature arg)
3234     (save-excursion
3235       (save-restriction
3236         (let ((inhibit-read-only t))
3237           (when (gnus-article-narrow-to-signature)
3238             (gnus-article-hide-text-type
3239              (point-min) (point-max) 'signature))))))
3240   (gnus-set-mode-line 'article))
3241
3242 (defun article-strip-headers-in-body ()
3243   "Strip offensive headers from bodies."
3244   (interactive)
3245   (save-excursion
3246     (article-goto-body)
3247     (let ((case-fold-search t))
3248       (when (looking-at "x-no-archive:")
3249         (gnus-delete-line)))))
3250
3251 (defun article-strip-leading-blank-lines ()
3252   "Remove all blank lines from the beginning of the article."
3253   (interactive)
3254   (save-excursion
3255     (let ((inhibit-point-motion-hooks t)
3256           (inhibit-read-only t))
3257       (when (article-goto-body)
3258         (while (and (not (eobp))
3259                     (looking-at "[ \t]*$"))
3260           (gnus-delete-line))))))
3261
3262 (defun article-narrow-to-head ()
3263   "Narrow the buffer to the head of the message.
3264 Point is left at the beginning of the narrowed-to region."
3265   (narrow-to-region
3266    (goto-char (point-min))
3267    (if (search-forward "\n\n" nil 1)
3268        (1- (point))
3269      (point-max)))
3270   (goto-char (point-min)))
3271
3272 (defun article-goto-body ()
3273   "Place point at the start of the body."
3274   (goto-char (point-min))
3275   (cond
3276    ;; This variable is only bound when dealing with separate
3277    ;; MIME body parts.
3278    (article-goto-body-goes-to-point-min-p
3279     t)
3280    ((search-forward "\n\n" nil t)
3281     t)
3282    (t
3283     (goto-char (point-max))
3284     nil)))
3285
3286 (defun article-strip-multiple-blank-lines ()
3287   "Replace consecutive blank lines with one empty line."
3288   (interactive)
3289   (save-excursion
3290     (let ((inhibit-point-motion-hooks t)
3291           (inhibit-read-only t))
3292       ;; First make all blank lines empty.
3293       (article-goto-body)
3294       (while (re-search-forward "^[ \t]+$" nil t)
3295         (unless (gnus-annotation-in-region-p
3296                  (match-beginning 0) (match-end 0))
3297           (replace-match "" nil t)))
3298       ;; Then replace multiple empty lines with a single empty line.
3299       (article-goto-body)
3300       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3301         (unless (gnus-annotation-in-region-p
3302                  (match-beginning 0) (match-end 0))
3303           (delete-region (match-beginning 1) (match-end 1)))))))
3304
3305 (defun article-strip-leading-space ()
3306   "Remove all white space from the beginning of the lines in the article."
3307   (interactive)
3308   (save-excursion
3309     (let ((inhibit-point-motion-hooks t)
3310           (inhibit-read-only t))
3311       (article-goto-body)
3312       (while (re-search-forward "^[ \t]+" nil t)
3313         (replace-match "" t t)))))
3314
3315 (defun article-strip-trailing-space ()
3316   "Remove all white space from the end of the lines in the article."
3317   (interactive)
3318   (save-excursion
3319     (let ((inhibit-point-motion-hooks t)
3320           (inhibit-read-only t))
3321       (article-goto-body)
3322       (while (re-search-forward "[ \t]+$" nil t)
3323         (replace-match "" t t)))))
3324
3325 (defun article-strip-blank-lines ()
3326   "Strip leading, trailing and multiple blank lines."
3327   (interactive)
3328   (article-strip-leading-blank-lines)
3329   (article-remove-trailing-blank-lines)
3330   (article-strip-multiple-blank-lines))
3331
3332 (defun article-strip-all-blank-lines ()
3333   "Strip all blank lines."
3334   (interactive)
3335   (save-excursion
3336     (let ((inhibit-point-motion-hooks t)
3337           (inhibit-read-only t))
3338       (article-goto-body)
3339       (while (re-search-forward "^[ \t]*\n" nil t)
3340         (replace-match "" t t)))))
3341
3342 (defun gnus-article-narrow-to-signature ()
3343   "Narrow to the signature; return t if a signature is found, else nil."
3344   (let ((inhibit-point-motion-hooks t))
3345     (when (gnus-article-search-signature)
3346       (forward-line 1)
3347       ;; Check whether we have some limits to what we consider
3348       ;; to be a signature.
3349       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3350                       (list gnus-signature-limit)))
3351             limit limited)
3352         (while (setq limit (pop limits))
3353           (if (or (and (integerp limit)
3354                        (< (- (point-max) (point)) limit))
3355                   (and (floatp limit)
3356                        (< (count-lines (point) (point-max)) limit))
3357                   (and (functionp limit)
3358                        (funcall limit))
3359                   (and (stringp limit)
3360                        (not (re-search-forward limit nil t))))
3361               ()                        ; This limit did not succeed.
3362             (setq limited t
3363                   limits nil)))
3364         (unless limited
3365           (narrow-to-region (point) (point-max))
3366           t)))))
3367
3368 (defun gnus-article-search-signature ()
3369   "Search the current buffer for the signature separator.
3370 Put point at the beginning of the signature separator."
3371   (let ((cur (point)))
3372     (goto-char (point-max))
3373     (if (if (stringp gnus-signature-separator)
3374             (re-search-backward gnus-signature-separator nil t)
3375           (let ((seps gnus-signature-separator))
3376             (while (and seps
3377                         (not (re-search-backward (car seps) nil t)))
3378               (pop seps))
3379             seps))
3380         t
3381       (goto-char cur)
3382       nil)))
3383
3384 (defun gnus-article-hidden-arg ()
3385   "Return the current prefix arg as a number, or 0 if no prefix."
3386   (list (if current-prefix-arg
3387             (prefix-numeric-value current-prefix-arg)
3388           0)))
3389
3390 (defun gnus-article-check-hidden-text (type arg)
3391   "Return nil if hiding is necessary.
3392 Arg can be nil or a number.  nil and positive means hide, negative
3393 means show, 0 means toggle."
3394   (save-excursion
3395     (save-restriction
3396       (let ((hide (gnus-article-hidden-text-p type)))
3397         (cond
3398          ((or (null arg)
3399               (> arg 0))
3400           nil)
3401          ((< arg 0)
3402           (gnus-article-show-hidden-text type)
3403           t)
3404          (t
3405           (if (eq hide 'hidden)
3406               (progn
3407                 (gnus-article-show-hidden-text type)
3408                 t)
3409             nil)))))))
3410
3411 (defun gnus-article-hidden-text-p (type)
3412   "Say whether the current buffer contains hidden text of type TYPE."
3413   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3414     (while (and pos
3415                 (not (get-text-property pos 'invisible))
3416                 (not (get-text-property pos 'dummy-invisible)))
3417       (setq pos
3418             (text-property-any (1+ pos) (point-max) 'article-type type)))
3419     (if pos
3420         'hidden
3421       nil)))
3422
3423 (defun gnus-article-show-hidden-text (type &optional dummy)
3424   "Show all hidden text of type TYPE.
3425 Originally it is hide instead of DUMMY."
3426   (let ((inhibit-read-only t)
3427         (inhibit-point-motion-hooks t))
3428     (gnus-remove-text-properties-when
3429      'article-type type
3430      (point-min) (point-max)
3431      (cons 'article-type (cons type
3432                                gnus-hidden-properties)))
3433     (gnus-delete-wash-type type)))
3434
3435 (defconst article-time-units
3436   `((year . ,(* 365.25 24 60 60))
3437     (week . ,(* 7 24 60 60))
3438     (day . ,(* 24 60 60))
3439     (hour . ,(* 60 60))
3440     (minute . 60)
3441     (second . 1))
3442   "Mapping from time units to seconds.")
3443
3444 (defun gnus-article-forward-header ()
3445   "Move point to the start of the next header.
3446 If the current header is a continuation header, this can be several
3447 lines forward."
3448   (let ((ended nil))
3449     (while (not ended)
3450       (forward-line 1)
3451       (if (looking-at "[ \t]+[^ \t]")
3452           (forward-line 1)
3453         (setq ended t)))))
3454
3455 (defun article-date-ut (&optional type highlight)
3456   "Convert DATE date to universal time in the current article.
3457 If TYPE is `local', convert to local time; if it is `lapsed', output
3458 how much time has lapsed since DATE.  For `lapsed', the value of
3459 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3460 should replace the \"Date:\" one, or should be added below it."
3461   (interactive (list 'ut t))
3462   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3463          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3464                              tdate-regexp)
3465                             ((eq type 'lapsed)
3466                              "^X-Sent:[ \t]")
3467                             (article-lapsed-timer
3468                              "^Date:[ \t]")
3469                             (t
3470                              tdate-regexp)))
3471          (case-fold-search t)
3472          (inhibit-read-only t)
3473          (inhibit-point-motion-hooks t)
3474          pos date bface eface)
3475     (save-excursion
3476       (save-restriction
3477         (widen)
3478         (goto-char (point-min))
3479         (while (or (setq date (get-text-property (setq pos (point))
3480                                                  'original-date))
3481                    (when (setq pos (next-single-property-change
3482                                     (point) 'original-date))
3483                      (setq date (get-text-property pos 'original-date))
3484                      t))
3485           (narrow-to-region
3486            pos (if (setq pos (text-property-any pos (point-max)
3487                                                 'original-date nil))
3488                    (progn
3489                      (goto-char pos)
3490                      (if (or (bolp) (eobp))
3491                          (point)
3492                        (1+ (point))))
3493                  (point-max)))
3494           (goto-char (point-min))
3495           (when (re-search-forward tdate-regexp nil t)
3496             (setq bface (get-text-property (point-at-bol) 'face)
3497                   eface (get-text-property (1- (point-at-eol)) 'face)))
3498           (goto-char (point-min))
3499           (setq pos nil)
3500           ;; Delete any old Date headers.
3501           (while (re-search-forward date-regexp nil t)
3502             (if pos
3503                 (delete-region (point-at-bol) (progn
3504                                                 (gnus-article-forward-header)
3505                                                 (point)))
3506               (delete-region (point-at-bol) (progn
3507                                               (gnus-article-forward-header)
3508                                               (forward-char -1)
3509                                               (point)))
3510               (setq pos (point))))
3511           (when (and (not pos)
3512                      (re-search-forward tdate-regexp nil t))
3513             (forward-line 1))
3514           (gnus-goto-char pos)
3515           (insert (article-make-date-line date (or type 'ut)))
3516           (unless pos
3517             (insert "\n")
3518             (forward-line -1))
3519           ;; Do highlighting.
3520           (beginning-of-line)
3521           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3522             (put-text-property (match-beginning 1) (1+ (match-end 1))
3523                                'face bface)
3524             (put-text-property (match-beginning 2) (match-end 2)
3525                                'face eface))
3526           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3527           (goto-char (point-max))
3528           (widen))))))
3529
3530 (defun article-make-date-line (date type)
3531   "Return a DATE line of TYPE."
3532   (unless (memq type '(local ut original user iso8601 lapsed english))
3533     (error "Unknown conversion type: %s" type))
3534   (condition-case ()
3535       (let ((time (date-to-time date)))
3536         (cond
3537          ;; Convert to the local timezone.
3538          ((eq type 'local)
3539           (concat "Date: " (message-make-date time)))
3540          ;; Convert to Universal Time.
3541          ((eq type 'ut)
3542           (concat "Date: "
3543                   (substring
3544                    (message-make-date
3545                     (let* ((e (parse-time-string date))
3546                            (tm (apply 'encode-time e))
3547                            (ms (car tm))
3548                            (ls (- (cadr tm) (car (current-time-zone time)))))
3549                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3550                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3551                             (t (list ms ls)))))
3552                    0 -5)
3553                   "UT"))
3554          ;; Get the original date from the article.
3555          ((eq type 'original)
3556           (concat "Date: " (if (string-match "\n+$" date)
3557                                (substring date 0 (match-beginning 0))
3558                              date)))
3559          ;; Let the user define the format.
3560          ((eq type 'user)
3561           (let ((format (or (condition-case nil
3562                                 (with-current-buffer gnus-summary-buffer
3563                                   gnus-article-time-format)
3564                               (error nil))
3565                             gnus-article-time-format)))
3566             (if (functionp format)
3567                 (funcall format time)
3568               (concat "Date: " (format-time-string format time)))))
3569          ;; ISO 8601.
3570          ((eq type 'iso8601)
3571           (let ((tz (car (current-time-zone time))))
3572             (concat
3573              "Date: "
3574              (format-time-string "%Y%m%dT%H%M%S" time)
3575              (format "%s%02d%02d"
3576                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3577                      (/ (% (abs tz) 3600) 60)))))
3578          ;; Do an X-Sent lapsed format.
3579          ((eq type 'lapsed)
3580           ;; If the date is seriously mangled, the timezone functions are
3581           ;; liable to bug out, so we ignore all errors.
3582           (let* ((now (current-time))
3583                  (real-time (subtract-time now time))
3584                  (real-sec (and real-time
3585                                 (+ (* (float (car real-time)) 65536)
3586                                    (cadr real-time))))
3587                  (sec (and real-time (abs real-sec)))
3588                  num prev)
3589             (cond
3590              ((null real-time)
3591               "X-Sent: Unknown")
3592              ((zerop sec)
3593               "X-Sent: Now")
3594              (t
3595               (concat
3596                "X-Sent: "
3597                ;; This is a bit convoluted, but basically we go
3598                ;; through the time units for years, weeks, etc,
3599                ;; and divide things to see whether that results
3600                ;; in positive answers.
3601                (mapconcat
3602                 (lambda (unit)
3603                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3604                       ;; The (remaining) seconds are too few to
3605                       ;; be divided into this time unit.
3606                       ""
3607                     ;; It's big enough, so we output it.
3608                     (setq sec (- sec (* num (cdr unit))))
3609                     (prog1
3610                         (concat (if prev ", " "") (int-to-string
3611                                                    (floor num))
3612                                 " " (symbol-name (car unit))
3613                                 (if (> num 1) "s" ""))
3614                       (setq prev t))))
3615                 article-time-units "")
3616                ;; If dates are odd, then it might appear like the
3617                ;; article was sent in the future.
3618                (if (> real-sec 0)
3619                    " ago"
3620                  " in the future"))))))
3621          ;; Display the date in proper English
3622          ((eq type 'english)
3623           (let ((dtime (decode-time time)))
3624             (concat
3625              "Date: the "
3626              (number-to-string (nth 3 dtime))
3627              (let ((digit (% (nth 3 dtime) 10)))
3628                (cond
3629                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3630                 ((= digit 1) "st")
3631                 ((= digit 2) "nd")
3632                 ((= digit 3) "rd")
3633                 (t "th")))
3634              " of "
3635              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3636              " "
3637              (number-to-string (nth 5 dtime))
3638              " at "
3639              (format "%02d" (nth 2 dtime))
3640              ":"
3641              (format "%02d" (nth 1 dtime)))))))
3642     (error
3643      (format "Date: %s (from Gnus)" date))))
3644
3645 (defun article-date-local (&optional highlight)
3646   "Convert the current article date to the local timezone."
3647   (interactive (list t))
3648   (article-date-ut 'local highlight))
3649
3650 (defun article-date-english (&optional highlight)
3651   "Convert the current article date to something that is proper English."
3652   (interactive (list t))
3653   (article-date-ut 'english highlight))
3654
3655 (defun article-date-original (&optional highlight)
3656   "Convert the current article date to what it was originally.
3657 This is only useful if you have used some other date conversion
3658 function and want to see what the date was before converting."
3659   (interactive (list t))
3660   (article-date-ut 'original highlight))
3661
3662 (defun article-date-lapsed (&optional highlight)
3663   "Convert the current article date to time lapsed since it was sent."
3664   (interactive (list t))
3665   (article-date-ut 'lapsed highlight))
3666
3667 (defun article-update-date-lapsed ()
3668   "Function to be run from a timer to update the lapsed time line."
3669   (save-match-data
3670     (let (deactivate-mark)
3671       (save-excursion
3672         (ignore-errors
3673          (walk-windows
3674           (lambda (w)
3675             (set-buffer (window-buffer w))
3676             (when (eq major-mode 'gnus-article-mode)
3677               (let ((mark (point-marker)))
3678                 (goto-char (point-min))
3679                 (when (re-search-forward "^X-Sent:" nil t)
3680                   (article-date-lapsed t))
3681                 (goto-char (marker-position mark))
3682                 (move-marker mark nil))))
3683           nil 'visible))))))
3684
3685 (defun gnus-start-date-timer (&optional n)
3686   "Start a timer to update the X-Sent header in the article buffers.
3687 The numerical prefix says how frequently (in seconds) the function
3688 is to run."
3689   (interactive "p")
3690   (unless n
3691     (setq n 1))
3692   (gnus-stop-date-timer)
3693   (setq article-lapsed-timer
3694         (run-at-time 1 n 'article-update-date-lapsed)))
3695
3696 (defun gnus-stop-date-timer ()
3697   "Stop the X-Sent timer."
3698   (interactive)
3699   (when article-lapsed-timer
3700     (nnheader-cancel-timer article-lapsed-timer)
3701     (setq article-lapsed-timer nil)))
3702
3703 (defun article-date-user (&optional highlight)
3704   "Convert the current article date to the user-defined format.
3705 This format is defined by the `gnus-article-time-format' variable."
3706   (interactive (list t))
3707   (article-date-ut 'user highlight))
3708
3709 (defun article-date-iso8601 (&optional highlight)
3710   "Convert the current article date to ISO8601."
3711   (interactive (list t))
3712   (article-date-ut 'iso8601 highlight))
3713
3714 (defmacro gnus-article-save-original-date (&rest forms)
3715   "Save the original date as a text property and evaluate FORMS."
3716   `(let* ((case-fold-search t)
3717           (start (progn
3718                    (goto-char (point-min))
3719                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3720                               (not (bolp)))
3721                      (match-end 0))))
3722           (date (when (and start
3723                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3724                                               nil t))
3725                   (buffer-substring-no-properties start
3726                                                   (match-beginning 0)))))
3727      (goto-char (point-max))
3728      (skip-chars-backward "\n")
3729      (put-text-property (point-min) (point) 'original-date date)
3730      ,@forms
3731      (goto-char (point-max))
3732      (skip-chars-backward "\n")
3733      (put-text-property (point-min) (point) 'original-date date)))
3734
3735 ;; (defun article-show-all ()
3736 ;;   "Show all hidden text in the article buffer."
3737 ;;   (interactive)
3738 ;;   (save-excursion
3739 ;;     (let ((inhibit-read-only t))
3740 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3741
3742 (defun article-remove-leading-whitespace ()
3743   "Remove excessive whitespace from all headers."
3744   (interactive)
3745   (save-excursion
3746     (save-restriction
3747       (let ((inhibit-read-only t))
3748         (article-narrow-to-head)
3749         (goto-char (point-min))
3750         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3751           (delete-region (match-beginning 1) (match-end 1)))))))
3752
3753 (defun article-emphasize (&optional arg)
3754   "Emphasize text according to `gnus-emphasis-alist'."
3755   (interactive (gnus-article-hidden-arg))
3756   (unless (gnus-article-check-hidden-text 'emphasis arg)
3757     (save-excursion
3758       (let ((alist (or
3759                     (condition-case nil
3760                         (with-current-buffer gnus-summary-buffer
3761                           gnus-article-emphasis-alist)
3762                       (error))
3763                     gnus-emphasis-alist))
3764             (inhibit-read-only t)
3765             (props (append '(article-type emphasis)
3766                            gnus-hidden-properties))
3767             regexp elem beg invisible visible face)
3768         (article-goto-body)
3769         (setq beg (point))
3770         (while (setq elem (pop alist))
3771           (goto-char beg)
3772           (setq regexp (car elem)
3773                 invisible (nth 1 elem)
3774                 visible (nth 2 elem)
3775                 face (nth 3 elem))
3776           (while (re-search-forward regexp nil t)
3777             (when (and (match-beginning visible) (match-beginning invisible))
3778               (gnus-article-hide-text
3779                (match-beginning invisible) (match-end invisible) props)
3780               (gnus-article-unhide-text-type
3781                (match-beginning visible) (match-end visible) 'emphasis)
3782               (gnus-put-overlay-excluding-newlines
3783                (match-beginning visible) (match-end visible) 'face face)
3784               (gnus-add-wash-type 'emphasis)
3785               (goto-char (match-end invisible)))))))))
3786
3787 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3788   "Setup newsgroup emphasis alist."
3789   (unless gnus-article-emphasis-alist
3790     (let ((name (and gnus-newsgroup-name
3791                      (gnus-group-real-name gnus-newsgroup-name))))
3792       (make-local-variable 'gnus-article-emphasis-alist)
3793       (setq gnus-article-emphasis-alist
3794             (nconc
3795              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3796                (while (setq elem (pop alist))
3797                  (when (and name (string-match (car elem) name))
3798                    (setq alist nil
3799                          highlight (copy-sequence (cdr elem)))))
3800                highlight)
3801              (copy-sequence highlight-words)
3802              (if gnus-newsgroup-name
3803                  (copy-sequence (gnus-group-find-parameter
3804                                  gnus-newsgroup-name 'highlight-words t)))
3805              gnus-emphasis-alist)))))
3806
3807 (defvar gnus-summary-article-menu)
3808 (defvar gnus-summary-post-menu)
3809
3810 ;;; Saving functions.
3811
3812 (defun gnus-article-save (save-buffer file &optional num)
3813   "Save the currently selected article."
3814   (when (or (get gnus-default-article-saver :headers)
3815             (not gnus-save-all-headers))
3816     ;; Remove headers according to `gnus-saved-headers' or the value
3817     ;; of the `:headers' property that the saver function might have.
3818     (let ((gnus-visible-headers
3819            (or (symbol-value (get gnus-default-article-saver :headers))
3820                gnus-saved-headers gnus-visible-headers))
3821           ;; Ignore group parameter.  See `article-hide-headers'.
3822           (gnus-summary-buffer nil))
3823       (with-current-buffer save-buffer
3824         (article-hide-headers 1 t))))
3825   (save-window-excursion
3826     (if (not gnus-default-article-saver)
3827         (error "No default saver is defined")
3828       ;; !!! Magic!  The saving functions all save
3829       ;; `gnus-save-article-buffer' (or so they think), but we
3830       ;; bind that variable to our save-buffer.
3831       (set-buffer gnus-article-buffer)
3832       (let* ((gnus-save-article-buffer save-buffer)
3833              (filename
3834               (cond
3835                ((not gnus-prompt-before-saving) 'default)
3836                ((eq gnus-prompt-before-saving 'always) nil)
3837                (t file)))
3838              (gnus-number-of-articles-to-be-saved
3839               (when (eq gnus-prompt-before-saving t)
3840                 num)))                  ; Magic
3841         (set-buffer gnus-article-current-summary)
3842         (funcall gnus-default-article-saver filename)))))
3843
3844 (defun gnus-read-save-file-name (prompt &optional filename
3845                                         function group headers variable
3846                                         dir-var)
3847   (let ((default-name
3848           (funcall function group headers (symbol-value variable)))
3849         result)
3850     (setq result
3851           (expand-file-name
3852            (cond
3853             ((eq filename 'default)
3854              default-name)
3855             ((eq filename t)
3856              default-name)
3857             (filename filename)
3858             (t
3859              (when (symbol-value dir-var)
3860                (setq default-name (expand-file-name
3861                                    (file-name-nondirectory default-name)
3862                                    (symbol-value dir-var))))
3863              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3864                     (prompt
3865                      (format prompt
3866                              (if (and gnus-number-of-articles-to-be-saved
3867                                       (> gnus-number-of-articles-to-be-saved 1))
3868                                  (format "these %d articles"
3869                                          gnus-number-of-articles-to-be-saved)
3870                                "this article")))
3871                     (file
3872                      ;; Let the split methods have their say.
3873                      (cond
3874                       ;; No split name was found.
3875                       ((null split-name)
3876                        (read-file-name
3877                         (concat prompt " (default "
3878                                 (file-name-nondirectory default-name) "): ")
3879                         (file-name-directory default-name)
3880                         default-name))
3881                       ;; A single group name is returned.
3882                       ((stringp split-name)
3883                        (setq default-name
3884                              (funcall function split-name headers
3885                                       (symbol-value variable)))
3886                        (read-file-name
3887                         (concat prompt " (default "
3888                                 (file-name-nondirectory default-name) "): ")
3889                         (file-name-directory default-name)
3890                         default-name))
3891                       ;; A single split name was found
3892                       ((= 1 (length split-name))
3893                        (let* ((name (expand-file-name
3894                                      (car split-name)
3895                                      gnus-article-save-directory))
3896                               (dir (cond ((file-directory-p name)
3897                                           (file-name-as-directory name))
3898                                          ((file-exists-p name) name)
3899                                          (t gnus-article-save-directory))))
3900                          (read-file-name
3901                           (concat prompt " (default " name "): ")
3902                           dir name)))
3903                       ;; A list of splits was found.
3904                       (t
3905                        (setq split-name (nreverse split-name))
3906                        (let (result)
3907                          (let ((file-name-history
3908                                 (nconc split-name file-name-history)))
3909                            (setq result
3910                                  (expand-file-name
3911                                   (read-file-name
3912                                    (concat prompt " (`M-p' for defaults): ")
3913                                    gnus-article-save-directory
3914                                    (car split-name))
3915                                   gnus-article-save-directory)))
3916                          (car (push result file-name-history)))))))
3917                ;; Create the directory.
3918                (gnus-make-directory (file-name-directory file))
3919                ;; If we have read a directory, we append the default file name.
3920                (when (file-directory-p file)
3921                  (setq file (expand-file-name (file-name-nondirectory
3922                                                default-name)
3923                                               (file-name-as-directory file))))
3924                ;; Possibly translate some characters.
3925                (nnheader-translate-file-chars file))))))
3926     (gnus-make-directory (file-name-directory result))
3927     (when variable
3928       (set variable result))
3929     (when dir-var
3930       (set dir-var (file-name-directory result)))
3931     result))
3932
3933 (defun gnus-article-archive-name (group)
3934   "Return the first instance of an \"Archive-name\" in the current buffer."
3935   (let ((case-fold-search t))
3936     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3937       (nnheader-concat gnus-article-save-directory
3938                        (match-string 1)))))
3939
3940 (defun gnus-article-nndoc-name (group)
3941   "If GROUP is an nndoc group, return the name of the parent group."
3942   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3943     (gnus-group-get-parameter group 'save-article-group)))
3944
3945 (defun gnus-summary-save-in-rmail (&optional filename)
3946   "Append this article to Rmail file.
3947 Optional argument FILENAME specifies file name.
3948 Directory to save to is default to `gnus-article-save-directory'."
3949   (setq filename (gnus-read-save-file-name
3950                   "Save %s in rmail file" filename
3951                   gnus-rmail-save-name gnus-newsgroup-name
3952                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3953   (gnus-eval-in-buffer-window gnus-save-article-buffer
3954     (save-excursion
3955       (save-restriction
3956         (widen)
3957         ;; Note that unlike gnus-summary-save-in-mail, there is no
3958         ;; check to see if filename is Babyl.  Rmail in Emacs 23 does
3959         ;; not use Babyl.
3960         (gnus-output-to-rmail filename))))
3961   filename)
3962
3963 (defun gnus-summary-save-in-mail (&optional filename)
3964   "Append this article to Unix mail file.
3965 Optional argument FILENAME specifies file name.
3966 Directory to save to is default to `gnus-article-save-directory'."
3967   (setq filename (gnus-read-save-file-name
3968                   "Save %s in Unix mail file" filename
3969                   gnus-mail-save-name gnus-newsgroup-name
3970                   gnus-current-headers 'gnus-newsgroup-last-mail))
3971   (gnus-eval-in-buffer-window gnus-save-article-buffer
3972     (save-excursion
3973       (save-restriction
3974         (widen)
3975         (if (and (file-readable-p filename)
3976                  (file-regular-p filename)
3977                  (mail-file-babyl-p filename))
3978             (gnus-output-to-rmail filename)
3979           (gnus-output-to-mail filename)))))
3980   filename)
3981
3982 (put 'gnus-summary-save-in-file :decode t)
3983 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
3984 (defun gnus-summary-save-in-file (&optional filename overwrite)
3985   "Append this article to file.
3986 Optional argument FILENAME specifies file name.
3987 Directory to save to is default to `gnus-article-save-directory'."
3988   (setq filename (gnus-read-save-file-name
3989                   "Save %s in file" filename
3990                   gnus-file-save-name gnus-newsgroup-name
3991                   gnus-current-headers 'gnus-newsgroup-last-file))
3992   (gnus-eval-in-buffer-window gnus-save-article-buffer
3993     (save-excursion
3994       (save-restriction
3995         (widen)
3996         (when (and overwrite
3997                    (file-exists-p filename))
3998           (delete-file filename))
3999         (gnus-output-to-file filename))))
4000   filename)
4001
4002 (put 'gnus-summary-write-to-file :decode t)
4003 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
4004 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
4005 (defun gnus-summary-write-to-file (&optional filename)
4006   "Write this article to a file, overwriting it if the file exists.
4007 Optional argument FILENAME specifies file name.
4008 The directory to save in defaults to `gnus-article-save-directory'."
4009   (setq filename (gnus-read-save-file-name
4010                   "Save %s in file" filename
4011                   gnus-file-save-name gnus-newsgroup-name
4012                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4013   (gnus-summary-save-in-file filename t))
4014
4015 (put 'gnus-summary-save-body-in-file :decode t)
4016 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
4017   "Append this article body to a file.
4018 Optional argument FILENAME specifies file name.
4019 The directory to save in defaults to `gnus-article-save-directory'."
4020   (setq filename (gnus-read-save-file-name
4021                   "Save %s body in file" filename
4022                   gnus-file-save-name gnus-newsgroup-name
4023                   gnus-current-headers 'gnus-newsgroup-last-file))
4024   (gnus-eval-in-buffer-window gnus-save-article-buffer
4025     (save-excursion
4026       (save-restriction
4027         (widen)
4028         (when (article-goto-body)
4029           (narrow-to-region (point) (point-max)))
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-body-to-file :decode t)
4037 (put 'gnus-summary-write-body-to-file
4038      :function 'gnus-summary-save-body-in-file)
4039 (defun gnus-summary-write-body-to-file (&optional filename)
4040   "Write this article body 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 body in file" filename
4045                   gnus-file-save-name gnus-newsgroup-name
4046                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
4047   (gnus-summary-save-body-in-file filename t))
4048
4049 (put 'gnus-summary-save-in-pipe :decode t)
4050 (put 'gnus-summary-save-in-pipe :headers 'gnus-saved-headers)
4051 (defun gnus-summary-save-in-pipe (&optional command raw)
4052   "Pipe this article to subprocess COMMAND.
4053 Valid values for COMMAND include:
4054   a string
4055     The executable command name and possibly arguments.
4056   nil
4057     You will be prompted for the command in the minibuffer.
4058   the symbol `default'
4059     It will be replaced with the command which the variable
4060     `gnus-summary-pipe-output-default-command' holds or the command
4061     last used for saving.
4062 Non-nil value for RAW overrides `:decode' and `:headers' properties
4063 and the raw article including all headers will be piped."
4064   (let ((article (gnus-summary-article-number))
4065         (decode (unless raw
4066                   (get 'gnus-summary-save-in-pipe :decode)))
4067         save-buffer default)
4068     (if article
4069         (if (vectorp (gnus-summary-article-header article))
4070             (save-current-buffer
4071               (gnus-summary-select-article decode decode nil article)
4072               (insert-buffer-substring
4073                (prog1
4074                    (if decode
4075                        gnus-article-buffer
4076                      gnus-original-article-buffer)
4077                  (setq save-buffer
4078                        (nnheader-set-temp-buffer " *Gnus Save*"))))
4079               ;; Remove unwanted headers.
4080               (when (and (not raw)
4081                          (or (get 'gnus-summary-save-in-pipe :headers)
4082                              (not gnus-save-all-headers)))
4083                 (let ((gnus-visible-headers
4084                        (or (symbol-value (get 'gnus-summary-save-in-pipe
4085                                               :headers))
4086                            gnus-saved-headers gnus-visible-headers))
4087                       (gnus-summary-buffer nil))
4088                   (article-hide-headers 1 t))))
4089           (error "%d is not a real article" article))
4090       (error "No article to pipe"))
4091     (setq default (or gnus-summary-pipe-output-default-command
4092                       gnus-last-shell-command))
4093     (unless (stringp command)
4094       (setq command
4095             (if (and (eq command 'default) default)
4096                 default
4097               (gnus-read-shell-command "Shell command on this article: "
4098                                        default))))
4099     (when (string-equal command "")
4100       (if default
4101           (setq command default)
4102         (error "A command is required")))
4103     (gnus-eval-in-buffer-window save-buffer
4104       (save-restriction
4105         (widen)
4106         (shell-command-on-region (point-min) (point-max) command nil)))
4107     (gnus-kill-buffer save-buffer))
4108   (setq gnus-summary-pipe-output-default-command command))
4109
4110 (defun gnus-summary-pipe-to-muttprint (&optional command)
4111   "Pipe this article to muttprint."
4112   (unless (stringp command)
4113     (setq command (read-string
4114                    "Print using command: " gnus-summary-muttprint-program
4115                    nil gnus-summary-muttprint-program)))
4116   (let ((gnus-summary-pipe-output-default-command
4117          gnus-summary-pipe-output-default-command))
4118     (gnus-summary-save-in-pipe command))
4119   (setq gnus-summary-muttprint-program command))
4120
4121 ;;; Article file names when saving.
4122
4123 (defun gnus-capitalize-newsgroup (newsgroup)
4124   "Capitalize NEWSGROUP name."
4125   (when (not (zerop (length newsgroup)))
4126     (concat (char-to-string (upcase (aref newsgroup 0)))
4127             (substring newsgroup 1))))
4128
4129 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
4130   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4131 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
4132 Otherwise, it is like ~/News/news/group/num."
4133   (let ((default
4134           (expand-file-name
4135            (concat (if (gnus-use-long-file-name 'not-save)
4136                        (gnus-capitalize-newsgroup newsgroup)
4137                      (gnus-newsgroup-directory-form newsgroup))
4138                    "/" (int-to-string (mail-header-number headers)))
4139            gnus-article-save-directory)))
4140     (if (and last-file
4141              (string-equal (file-name-directory default)
4142                            (file-name-directory last-file))
4143              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4144         default
4145       (or last-file default))))
4146
4147 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4148   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4149 If variable `gnus-use-long-file-name' is non-nil, it is
4150 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4151   (let ((default
4152           (expand-file-name
4153            (concat (if (gnus-use-long-file-name 'not-save)
4154                        newsgroup
4155                      (gnus-newsgroup-directory-form newsgroup))
4156                    "/" (int-to-string (mail-header-number headers)))
4157            gnus-article-save-directory)))
4158     (if (and last-file
4159              (string-equal (file-name-directory default)
4160                            (file-name-directory last-file))
4161              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4162         default
4163       (or last-file default))))
4164
4165 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4166   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4167 If variable `gnus-use-long-file-name' is non-nil, it is
4168 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4169   (or last-file
4170       (expand-file-name
4171        (if (gnus-use-long-file-name 'not-save)
4172            newsgroup
4173          (file-relative-name
4174           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4175           default-directory))
4176        gnus-article-save-directory)))
4177
4178 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4179   "Generate file name from sender."
4180   (let ((from (mail-header-from headers)))
4181     (expand-file-name
4182      (if (and from (string-match "\\([^ <]+\\)@" from))
4183          (match-string 1 from)
4184        "nobody")
4185      gnus-article-save-directory)))
4186
4187 (defun article-verify-x-pgp-sig ()
4188   "Verify X-PGP-Sig."
4189   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4190   (interactive)
4191   (if (gnus-buffer-live-p gnus-original-article-buffer)
4192       (let ((sig (with-current-buffer gnus-original-article-buffer
4193                    (gnus-fetch-field "X-PGP-Sig")))
4194             items info headers)
4195         (when (and sig
4196                    mml2015-use
4197                    (mml2015-clear-verify-function))
4198           (with-temp-buffer
4199             (insert-buffer-substring gnus-original-article-buffer)
4200             (setq items (split-string sig))
4201             (message-narrow-to-head)
4202             (let ((inhibit-point-motion-hooks t)
4203                   (case-fold-search t))
4204               ;; Don't verify multiple headers.
4205               (setq headers (mapconcat (lambda (header)
4206                                          (concat header ": "
4207                                                  (mail-fetch-field header)
4208                                                  "\n"))
4209                                        (split-string (nth 1 items) ",") "")))
4210             (delete-region (point-min) (point-max))
4211             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4212             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4213             (insert headers)
4214             (widen)
4215             (forward-line)
4216             (while (not (eobp))
4217               (if (looking-at "^-")
4218                   (insert "- "))
4219               (forward-line))
4220             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4221             (insert "Version: " (car items) "\n\n")
4222             (insert (mapconcat 'identity (cddr items) "\n"))
4223             (insert "\n-----END PGP SIGNATURE-----\n")
4224             (let ((mm-security-handle (list (format "multipart/signed"))))
4225               (mml2015-clean-buffer)
4226               (let ((coding-system-for-write (or gnus-newsgroup-charset
4227                                                  'iso-8859-1)))
4228                 (funcall (mml2015-clear-verify-function)))
4229               (setq info
4230                     (or (mm-handle-multipart-ctl-parameter
4231                          mm-security-handle 'gnus-details)
4232                         (mm-handle-multipart-ctl-parameter
4233                          mm-security-handle 'gnus-info)))))
4234           (when info
4235             (let ((inhibit-read-only t) bface eface)
4236               (save-restriction
4237                 (message-narrow-to-head)
4238                 (goto-char (point-max))
4239                 (forward-line -1)
4240                 (setq bface (get-text-property (point-at-bol) 'face)
4241                       eface (get-text-property (1- (point-at-eol)) 'face))
4242                 (message-remove-header "X-Gnus-PGP-Verify")
4243                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4244                     (forward-line)
4245                   (goto-char (point-max)))
4246                 (narrow-to-region (point) (point))
4247                 (insert "X-Gnus-PGP-Verify: " info "\n")
4248                 (goto-char (point-min))
4249                 (forward-line)
4250                 (while (not (eobp))
4251                   (if (not (looking-at "^[ \t]"))
4252                       (insert " "))
4253                   (forward-line))
4254                 ;; Do highlighting.
4255                 (goto-char (point-min))
4256                 (when (looking-at "\\([^:]+\\): *")
4257                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4258                                      'face bface)
4259                   (put-text-property (match-end 0) (point-max)
4260                                      'face eface)))))))))
4261
4262 (autoload 'canlock-verify "canlock" nil t) ;; for Emacs 21.
4263
4264 (defun article-verify-cancel-lock ()
4265   "Verify Cancel-Lock header."
4266   (interactive)
4267   (if (gnus-buffer-live-p gnus-original-article-buffer)
4268       (canlock-verify gnus-original-article-buffer)))
4269
4270 (eval-and-compile
4271   (mapc
4272    (lambda (func)
4273      (let (afunc gfunc)
4274        (if (consp func)
4275            (setq afunc (car func)
4276                  gfunc (cdr func))
4277          (setq afunc func
4278                gfunc (intern (format "gnus-%s" func))))
4279        (defalias gfunc
4280          (when (fboundp afunc)
4281            `(lambda (&optional interactive &rest args)
4282               ,(documentation afunc t)
4283               (interactive (list t))
4284               (with-current-buffer gnus-article-buffer
4285                 (if interactive
4286                     (call-interactively ',afunc)
4287                   (apply ',afunc args))))))))
4288    '(article-hide-headers
4289      article-verify-x-pgp-sig
4290      article-verify-cancel-lock
4291      article-hide-boring-headers
4292      article-treat-overstrike
4293      article-treat-ansi-sequences
4294      article-fill-long-lines
4295      article-capitalize-sentences
4296      article-remove-cr
4297      article-remove-leading-whitespace
4298      article-display-x-face
4299      article-display-face
4300      article-de-quoted-unreadable
4301      article-de-base64-unreadable
4302      article-decode-HZ
4303      article-wash-html
4304      article-unsplit-urls
4305      article-hide-list-identifiers
4306      article-strip-banner
4307      article-babel
4308      article-hide-pem
4309      article-hide-signature
4310      article-strip-headers-in-body
4311      article-remove-trailing-blank-lines
4312      article-strip-leading-blank-lines
4313      article-strip-multiple-blank-lines
4314      article-strip-leading-space
4315      article-strip-trailing-space
4316      article-strip-blank-lines
4317      article-strip-all-blank-lines
4318      article-date-local
4319      article-date-english
4320      article-date-iso8601
4321      article-date-original
4322      article-date-ut
4323      article-decode-mime-words
4324      article-decode-charset
4325      article-decode-encoded-words
4326      article-date-user
4327      article-date-lapsed
4328      article-emphasize
4329      article-treat-dumbquotes
4330      article-normalize-headers
4331      ;;(article-show-all . gnus-article-show-all-headers)
4332      )))
4333 \f
4334 ;;;
4335 ;;; Gnus article mode
4336 ;;;
4337
4338 (put 'gnus-article-mode 'mode-class 'special)
4339
4340 (set-keymap-parent gnus-article-mode-map widget-keymap)
4341
4342 (gnus-define-keys gnus-article-mode-map
4343   " " gnus-article-goto-next-page
4344   "\177" gnus-article-goto-prev-page
4345   [delete] gnus-article-goto-prev-page
4346   [backspace] gnus-article-goto-prev-page
4347   "\C-c^" gnus-article-refer-article
4348   "h" gnus-article-show-summary
4349   "s" gnus-article-show-summary
4350   "\C-c\C-m" gnus-article-mail
4351   "?" gnus-article-describe-briefly
4352   "<" beginning-of-buffer
4353   ">" end-of-buffer
4354   "\C-c\C-i" gnus-info-find-node
4355   "\C-c\C-b" gnus-bug
4356   "R" gnus-article-reply-with-original
4357   "F" gnus-article-followup-with-original
4358   "\C-hk" gnus-article-describe-key
4359   "\C-hc" gnus-article-describe-key-briefly
4360   "\C-hb" gnus-article-describe-bindings
4361
4362   "e" gnus-article-read-summary-keys
4363   "\C-d" gnus-article-read-summary-keys
4364   "\M-*" gnus-article-read-summary-keys
4365   "\M-#" gnus-article-read-summary-keys
4366   "\M-^" gnus-article-read-summary-keys
4367   "\M-g" gnus-article-read-summary-keys)
4368
4369 (substitute-key-definition
4370  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4371
4372 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4373   "W" gnus-article-wide-reply-with-original)
4374 (if (featurep 'xemacs)
4375     (set-keymap-default-binding gnus-article-send-map
4376                                 'gnus-article-read-summary-send-keys)
4377   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4378
4379 (defun gnus-article-make-menu-bar ()
4380   (unless (boundp 'gnus-article-commands-menu)
4381     (gnus-summary-make-menu-bar))
4382   (gnus-turn-off-edit-menu 'article)
4383   (unless (boundp 'gnus-article-article-menu)
4384     (easy-menu-define
4385      gnus-article-article-menu gnus-article-mode-map ""
4386      '("Article"
4387        ["Scroll forwards" gnus-article-goto-next-page t]
4388        ["Scroll backwards" gnus-article-goto-prev-page t]
4389        ["Show summary" gnus-article-show-summary t]
4390        ["Fetch Message-ID at point" gnus-article-refer-article t]
4391        ["Mail to address at point" gnus-article-mail t]
4392        ["Send a bug report" gnus-bug t]))
4393
4394     (easy-menu-define
4395      gnus-article-treatment-menu gnus-article-mode-map ""
4396      ;; Fixme: this should use :active (and maybe :visible).
4397      '("Treatment"
4398        ["Hide headers" gnus-article-hide-headers t]
4399        ["Hide signature" gnus-article-hide-signature t]
4400        ["Hide citation" gnus-article-hide-citation t]
4401        ["Treat overstrike" gnus-article-treat-overstrike t]
4402        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4403        ["Remove carriage return" gnus-article-remove-cr t]
4404        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4405        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4406        ["Remove base64" gnus-article-de-base64-unreadable t]
4407        ["Treat html" gnus-article-wash-html t]
4408        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4409        ["Decode HZ" gnus-article-decode-HZ t]))
4410
4411     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4412
4413     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4414
4415     (gnus-run-hooks 'gnus-article-menu-hook)))
4416
4417 (defvar bookmark-make-record-function)
4418
4419 (defun gnus-article-mode ()
4420   "Major mode for displaying an article.
4421
4422 All normal editing commands are switched off.
4423
4424 The following commands are available in addition to all summary mode
4425 commands:
4426 \\<gnus-article-mode-map>
4427 \\[gnus-article-next-page]\t Scroll the article one page forwards
4428 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4429 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4430 \\[gnus-article-show-summary]\t Display the summary buffer
4431 \\[gnus-article-mail]\t Send a reply to the address near point
4432 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4433 \\[gnus-info-find-node]\t Go to the Gnus info node"
4434   (interactive)
4435   (kill-all-local-variables)
4436   (gnus-simplify-mode-line)
4437   (setq mode-name "Article")
4438   (setq major-mode 'gnus-article-mode)
4439   (make-local-variable 'minor-mode-alist)
4440   (use-local-map gnus-article-mode-map)
4441   (when (gnus-visual-p 'article-menu 'menu)
4442     (gnus-article-make-menu-bar)
4443     (when gnus-summary-tool-bar-map
4444       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4445   (gnus-update-format-specifications nil 'article-mode)
4446   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4447   (set (make-local-variable 'gnus-page-broken) nil)
4448   (make-local-variable 'gnus-button-marker-list)
4449   (make-local-variable 'gnus-article-current-summary)
4450   (make-local-variable 'gnus-article-mime-handles)
4451   (make-local-variable 'gnus-article-decoded-p)
4452   (make-local-variable 'gnus-article-mime-handle-alist)
4453   (make-local-variable 'gnus-article-wash-types)
4454   (make-local-variable 'gnus-article-image-alist)
4455   (make-local-variable 'gnus-article-charset)
4456   (make-local-variable 'gnus-article-ignored-charsets)
4457   (set (make-local-variable 'bookmark-make-record-function)
4458        'gnus-summary-bookmark-make-record)
4459   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4460   ;; face.
4461   (set (make-local-variable 'nobreak-char-display) nil)
4462   (setq cursor-in-non-selected-windows nil)
4463   (setq truncate-lines gnus-article-truncate-lines)
4464   (gnus-set-default-directory)
4465   (buffer-disable-undo)
4466   (setq buffer-read-only t
4467         show-trailing-whitespace nil)
4468   (set-syntax-table gnus-article-mode-syntax-table)
4469   (mm-enable-multibyte)
4470   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4471
4472 (defvar gnus-button-marker-list nil
4473   "Regexp matching any of the regexps from `gnus-button-alist'.
4474 Internal variable.")
4475
4476 (defun gnus-article-setup-buffer ()
4477   "Initialize the article buffer."
4478   (let* ((name (if gnus-single-article-buffer "*Article*"
4479                  (concat "*Article " gnus-newsgroup-name "*")))
4480          (original
4481           (progn (string-match "\\*Article" name)
4482                  (concat " *Original Article"
4483                          (substring name (match-end 0))))))
4484     (setq gnus-article-buffer name)
4485     (setq gnus-original-article-buffer original)
4486     (setq gnus-article-mime-handle-alist nil)
4487     (with-current-buffer gnus-summary-buffer
4488       ;; This might be a variable local to the summary buffer.
4489       (unless gnus-single-article-buffer
4490         (setq gnus-article-buffer name)
4491         (setq gnus-original-article-buffer original)
4492         (gnus-set-global-variables)))
4493     (gnus-article-setup-highlight-words)
4494     ;; Init original article buffer.
4495     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4496       (mm-enable-multibyte)
4497       (setq major-mode 'gnus-original-article-mode)
4498       (make-local-variable 'gnus-original-article))
4499     (if (and (get-buffer name)
4500              (with-current-buffer name
4501                (if gnus-article-edit-mode
4502                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4503                        (progn
4504                          (set-buffer-modified-p nil)
4505                          (gnus-kill-buffer name)
4506                          (message "")
4507                          nil)
4508                      (error "Action aborted"))
4509                  t)))
4510         (with-current-buffer name
4511           (set (make-local-variable 'gnus-article-edit-mode) nil)
4512           (when gnus-article-mime-handles
4513             (mm-destroy-parts gnus-article-mime-handles)
4514             (setq gnus-article-mime-handles nil))
4515           ;; Set it to nil in article-buffer!
4516           (setq gnus-article-mime-handle-alist nil)
4517           (buffer-disable-undo)
4518           (setq buffer-read-only t)
4519           ;; This list just keeps growing if we don't reset it.
4520           (setq gnus-button-marker-list nil)
4521           (unless (eq major-mode 'gnus-article-mode)
4522             (gnus-article-mode))
4523           (current-buffer))
4524       (with-current-buffer (gnus-get-buffer-create name)
4525         (gnus-article-mode)
4526         (make-local-variable 'gnus-summary-buffer)
4527         (setq gnus-summary-buffer
4528               (gnus-summary-buffer-name gnus-newsgroup-name))
4529         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4530         (current-buffer)))))
4531
4532 ;; Set article window start at LINE, where LINE is the number of lines
4533 ;; from the head of the article.
4534 (defun gnus-article-set-window-start (&optional line)
4535   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4536     (when article-window
4537       (set-window-start
4538        article-window
4539        (with-current-buffer gnus-article-buffer
4540          (goto-char (point-min))
4541          (if (not line)
4542              (point-min)
4543            (gnus-message 6 "Moved to bookmark")
4544            (search-forward "\n\n" nil t)
4545            (forward-line line)
4546            (point)))))))
4547
4548 (defun gnus-article-prepare (article &optional all-headers header)
4549   "Prepare ARTICLE in article mode buffer.
4550 ARTICLE should either be an article number or a Message-ID.
4551 If ARTICLE is an id, HEADER should be the article headers.
4552 If ALL-HEADERS is non-nil, no headers are hidden."
4553   (save-excursion
4554     ;; Make sure we start in a summary buffer.
4555     (unless (eq major-mode 'gnus-summary-mode)
4556       (set-buffer gnus-summary-buffer))
4557     (setq gnus-summary-buffer (current-buffer))
4558     (let* ((gnus-article (if header (mail-header-number header) article))
4559            (summary-buffer (current-buffer))
4560            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4561            (group gnus-newsgroup-name)
4562            result)
4563       (save-excursion
4564         (gnus-article-setup-buffer)
4565         (set-buffer gnus-article-buffer)
4566         ;; Deactivate active regions.
4567         (when (and (boundp 'transient-mark-mode)
4568                    transient-mark-mode)
4569           (setq mark-active nil))
4570         (if (not (setq result (let ((inhibit-read-only t))
4571                                 (gnus-request-article-this-buffer
4572                                  article group))))
4573             ;; There is no such article.
4574             (save-excursion
4575               (when (and (numberp article)
4576                          (not (memq article gnus-newsgroup-sparse)))
4577                 (setq gnus-article-current
4578                       (cons gnus-newsgroup-name article))
4579                 (set-buffer gnus-summary-buffer)
4580                 (setq gnus-current-article article)
4581                 (if (and (memq article gnus-newsgroup-undownloaded)
4582                          (not (gnus-online (gnus-find-method-for-group
4583                                             gnus-newsgroup-name))))
4584                     (progn
4585                       (gnus-summary-set-agent-mark article)
4586                       (message "Message marked for downloading"))
4587                   (gnus-summary-mark-article article gnus-canceled-mark)
4588                   (unless (memq article gnus-newsgroup-sparse)
4589                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4590           (if (or (eq result 'pseudo)
4591                   (eq result 'nneething))
4592               (progn
4593                 (with-current-buffer summary-buffer
4594                   (push article gnus-newsgroup-history)
4595                   (setq gnus-last-article gnus-current-article
4596                         gnus-current-article 0
4597                         gnus-current-headers nil
4598                         gnus-article-current nil)
4599                   (if (eq result 'nneething)
4600                       (gnus-configure-windows 'summary)
4601                     (gnus-configure-windows 'article))
4602                   (gnus-set-global-variables))
4603                 (let ((gnus-article-mime-handle-alist-1
4604                        gnus-article-mime-handle-alist))
4605                   (gnus-set-mode-line 'article)))
4606             ;; The result from the `request' was an actual article -
4607             ;; or at least some text that is now displayed in the
4608             ;; article buffer.
4609             (when (and (numberp article)
4610                        (not (eq article gnus-current-article)))
4611               ;; Seems like a new article has been selected.
4612               ;; `gnus-current-article' must be an article number.
4613               (with-current-buffer summary-buffer
4614                 (push article gnus-newsgroup-history)
4615                 (setq gnus-last-article gnus-current-article
4616                       gnus-current-article article
4617                       gnus-current-headers
4618                       (gnus-summary-article-header gnus-current-article)
4619                       gnus-article-current
4620                       (cons gnus-newsgroup-name gnus-current-article))
4621                 (unless (vectorp gnus-current-headers)
4622                   (setq gnus-current-headers nil))
4623                 (gnus-summary-goto-subject gnus-current-article)
4624                 (when (gnus-summary-show-thread)
4625                   ;; If the summary buffer really was folded, the
4626                   ;; previous goto may not actually have gone to
4627                   ;; the right article, but the thread root instead.
4628                   ;; So we go again.
4629                   (gnus-summary-goto-subject gnus-current-article))
4630                 (gnus-run-hooks 'gnus-mark-article-hook)
4631                 (gnus-set-mode-line 'summary)
4632                 (when (gnus-visual-p 'article-highlight 'highlight)
4633                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4634                 ;; Set the global newsgroup variables here.
4635                 (gnus-set-global-variables)
4636                 (setq gnus-have-all-headers
4637                       (or all-headers gnus-show-all-headers))))
4638             (save-excursion
4639               (gnus-configure-windows 'article))
4640             (when (or (numberp article)
4641                       (stringp article))
4642               (gnus-article-prepare-display)
4643               ;; Do page break.
4644               (goto-char (point-min))
4645               (when gnus-break-pages
4646                 (gnus-narrow-to-page)))
4647             (let ((gnus-article-mime-handle-alist-1
4648                    gnus-article-mime-handle-alist))
4649               (gnus-set-mode-line 'article))
4650             (article-goto-body)
4651             (unless (bobp)
4652               (forward-line -1))
4653             (set-window-point (get-buffer-window (current-buffer)) (point))
4654             (gnus-configure-windows 'article)
4655             t))))))
4656
4657 ;;;###autoload
4658 (defun gnus-article-prepare-display ()
4659   "Make the current buffer look like a nice article."
4660   ;; Hooks for getting information from the article.
4661   ;; This hook must be called before being narrowed.
4662   (let ((gnus-article-buffer (current-buffer))
4663         buffer-read-only
4664         (inhibit-read-only t))
4665     (unless (eq major-mode 'gnus-article-mode)
4666       (gnus-article-mode))
4667     (setq buffer-read-only nil
4668           gnus-article-wash-types nil
4669           gnus-article-image-alist nil)
4670     (gnus-run-hooks 'gnus-tmp-internal-hook)
4671     (when gnus-display-mime-function
4672       (funcall gnus-display-mime-function))
4673     (gnus-run-hooks 'gnus-article-prepare-hook)))
4674
4675 ;;;
4676 ;;; Gnus Sticky Article Mode
4677 ;;;
4678
4679 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4680   "Mode for sticky articles."
4681   ;; Release bindings that won't work.
4682   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4683                              gnus-sticky-article-mode-map)
4684   (substitute-key-definition 'gnus-article-refer-article 'undefined
4685                              gnus-sticky-article-mode-map)
4686   (dolist (k '("e" "h" "s" "F" "R"))
4687     (define-key gnus-sticky-article-mode-map k nil))
4688   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4689   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4690   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4691   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4692
4693 (defun gnus-sticky-article (arg)
4694   "Make the current article sticky.
4695 If a prefix ARG is given, ask for a name for this sticky article buffer."
4696   (interactive "P")
4697   (gnus-summary-show-thread)
4698   (gnus-summary-select-article nil nil 'pseudo)
4699   (let (new-art-buf-name)
4700     (gnus-eval-in-buffer-window gnus-article-buffer
4701       (setq new-art-buf-name
4702             (concat
4703              "*Sticky Article: "
4704              (if arg
4705                  (read-from-minibuffer "Sticky article buffer name: ")
4706                (gnus-with-article-headers
4707                  (gnus-article-goto-header "subject")
4708                  (setq new-art-buf-name
4709                        (buffer-substring-no-properties
4710                         (line-beginning-position) (line-end-position)))
4711                  (goto-char (point-min))
4712                  (gnus-article-goto-header "from")
4713                  (setq new-art-buf-name
4714                        (concat
4715                         new-art-buf-name ", "
4716                         (buffer-substring-no-properties
4717                          (line-beginning-position) (line-end-position))))
4718                  (goto-char (point-min))
4719                  (gnus-article-goto-header "date")
4720                  (setq new-art-buf-name
4721                        (concat
4722                         new-art-buf-name ", "
4723                         (buffer-substring-no-properties
4724                          (line-beginning-position) (line-end-position))))))
4725              "*"))
4726       (if (and (gnus-buffer-live-p new-art-buf-name)
4727                (with-current-buffer new-art-buf-name
4728                  (eq major-mode 'gnus-sticky-article-mode)))
4729           (switch-to-buffer new-art-buf-name)
4730         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4731       (gnus-sticky-article-mode))
4732     (setq gnus-article-buffer new-art-buf-name))
4733   (gnus-summary-recenter)
4734   (gnus-summary-position-point))
4735
4736 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4737   "Kill the given sticky article BUFFER.
4738 If none is given, assume the current buffer and kill it if it has
4739 `gnus-sticky-article-mode'."
4740   (interactive)
4741   (unless buffer
4742     (setq buffer (current-buffer)))
4743   (with-current-buffer buffer
4744     (when (eq major-mode 'gnus-sticky-article-mode)
4745       (gnus-kill-buffer buffer))))
4746
4747 (defun gnus-kill-sticky-article-buffers (arg)
4748   "Kill all sticky article buffers.
4749 If a prefix ARG is given, ask for confirmation."
4750   (interactive "P")
4751   (dolist (buf (gnus-buffers))
4752     (with-current-buffer buf
4753       (when (eq major-mode 'gnus-sticky-article-mode)
4754         (if (not arg)
4755             (gnus-kill-buffer buf)
4756           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4757             (gnus-kill-buffer buf)))))))
4758
4759 ;;;
4760 ;;; Gnus MIME viewing functions
4761 ;;;
4762
4763 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4764   "Format of the MIME buttons.
4765
4766 Valid specifiers include:
4767 %t  The MIME type
4768 %T  MIME type, along with additional info
4769 %n  The `name' parameter
4770 %d  The description, if any
4771 %l  The length of the encoded part
4772 %p  The part identifier number
4773 %e  Dots if the part isn't displayed
4774
4775 General format specifiers can also be used.  See Info node
4776 `(gnus)Formatting Variables'.")
4777
4778 (defvar gnus-mime-button-line-format-alist
4779   '((?t gnus-tmp-type ?s)
4780     (?T gnus-tmp-type-long ?s)
4781     (?n gnus-tmp-name ?s)
4782     (?d gnus-tmp-description ?s)
4783     (?p gnus-tmp-id ?s)
4784     (?l gnus-tmp-length ?d)
4785     (?e gnus-tmp-dots ?s)))
4786
4787 (defvar gnus-mime-button-commands
4788   '((gnus-article-press-button "\r" "Toggle Display")
4789     (gnus-mime-view-part "v" "View Interactively...")
4790     (gnus-mime-view-part-as-type "t" "View As Type...")
4791     (gnus-mime-view-part-as-charset "C" "View As charset...")
4792     (gnus-mime-save-part "o" "Save...")
4793     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4794     (gnus-mime-replace-part "r" "Replace part")
4795     (gnus-mime-delete-part "d" "Delete part")
4796     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4797     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4798     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4799     (gnus-mime-view-part-externally "e" "View Externally")
4800     (gnus-mime-print-part "p" "Print")
4801     (gnus-mime-pipe-part "|" "Pipe To Command...")
4802     (gnus-mime-action-on-part "." "Take action on the part...")))
4803
4804 (defun gnus-article-mime-part-status ()
4805   (if gnus-article-mime-handle-alist-1
4806       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4807           " (1 part)"
4808         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4809     ""))
4810
4811 (defvar gnus-mime-button-map
4812   (let ((map (make-sparse-keymap)))
4813     (define-key map gnus-mouse-2 'gnus-article-push-button)
4814     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4815     (dolist (c gnus-mime-button-commands)
4816       (define-key map (cadr c) (car c)))
4817     map))
4818
4819 (easy-menu-define
4820   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4821   `("MIME Part"
4822     ,@(mapcar (lambda (c)
4823                 (vector (caddr c) (car c) :active t))
4824               gnus-mime-button-commands)))
4825
4826 (defvar gnus-url-button-commands
4827   '((gnus-article-copy-string "u" "Copy URL to kill ring")))
4828
4829 (defvar gnus-url-button-map
4830   (let ((map (make-sparse-keymap)))
4831     (dolist (c gnus-url-button-commands)
4832       (define-key map (cadr c) (car c)))
4833     map))
4834
4835 (easy-menu-define
4836   gnus-url-button-menu gnus-url-button-map "URL button menu."
4837   `("Url Button"
4838     ,@(mapcar (lambda (c)
4839                 (vector (caddr c) (car c) :active t))
4840               gnus-url-button-commands)))
4841
4842 (defmacro gnus-bind-safe-url-regexp (&rest body)
4843   "Bind `mm-w3m-safe-url-regexp' according to `gnus-safe-html-newsgroups'."
4844   `(let ((mm-w3m-safe-url-regexp
4845           (let ((group (if (and (eq major-mode 'gnus-article-mode)
4846                                 (gnus-buffer-live-p
4847                                  gnus-article-current-summary))
4848                            (with-current-buffer gnus-article-current-summary
4849                              gnus-newsgroup-name)
4850                          gnus-newsgroup-name)))
4851             (if (cond ((not group)
4852                        ;; Maybe we're in a mml-preview buffer
4853                        ;; and no group is selected.
4854                        t)
4855                       ((stringp gnus-safe-html-newsgroups)
4856                        (string-match gnus-safe-html-newsgroups group))
4857                       ((consp gnus-safe-html-newsgroups)
4858                        (member group gnus-safe-html-newsgroups)))
4859                 nil
4860               mm-w3m-safe-url-regexp))))
4861      ,@body))
4862
4863 (defun gnus-mime-button-menu (event prefix)
4864  "Construct a context-sensitive menu of MIME commands."
4865  (interactive "e\nP")
4866  (save-window-excursion
4867    (let ((pos (event-start event)))
4868      (select-window (posn-window pos))
4869      (goto-char (posn-point pos))
4870      (gnus-article-check-buffer)
4871      (popup-menu gnus-mime-button-menu nil prefix))))
4872
4873 (defun gnus-mime-view-all-parts (&optional handles)
4874   "View all the MIME parts."
4875   (interactive)
4876   (with-current-buffer gnus-article-buffer
4877     (let ((handles (or handles gnus-article-mime-handles))
4878           (mail-parse-charset gnus-newsgroup-charset)
4879           (mail-parse-ignored-charsets
4880            (with-current-buffer gnus-summary-buffer
4881              gnus-newsgroup-ignored-charsets)))
4882       (when handles
4883         (mm-remove-parts handles)
4884         (goto-char (point-min))
4885         (or (search-forward "\n\n") (goto-char (point-max)))
4886         (let ((inhibit-read-only t))
4887           (delete-region (point) (point-max))
4888           (gnus-bind-safe-url-regexp (mm-display-parts handles)))))))
4889
4890 (defun gnus-article-jump-to-part (n)
4891   "Jump to MIME part N."
4892   (interactive "P")
4893   (pop-to-buffer gnus-article-buffer)
4894   ;; FIXME: why is it necessary?
4895   (sit-for 0)
4896   (let ((parts (length gnus-article-mime-handle-alist)))
4897     (or n (setq n
4898                 (string-to-number
4899                  (read-string ;; Emacs 21 doesn't have `read-number'.
4900                   (format "Jump to part (2..%s): " parts)))))
4901     (unless (and (integerp n) (<= n parts) (>= n 1))
4902       (setq n
4903             (progn
4904               (gnus-message 7 "Invalid part `%s', using %s instead."
4905                             n parts)
4906               parts)))
4907     (gnus-message 9 "Jumping to part %s." n)
4908     (cond ((>= gnus-auto-select-part 1)
4909            (while (and (<= n parts)
4910                        (not (gnus-article-goto-part n)))
4911              (setq n (1+ n))))
4912           ((< gnus-auto-select-part 0)
4913            (while (and (>= n 1)
4914                        (not (gnus-article-goto-part n)))
4915              (setq n (1- n))))
4916           (t
4917            (gnus-article-goto-part n)))))
4918
4919 (defvar gnus-mime-buttonized-part-id nil
4920   "ID of a mime part that should be buttonized.
4921 `gnus-mime-save-part-and-strip' and `gnus-mime-delete-part' bind it.")
4922
4923 (eval-when-compile
4924   (defsubst gnus-article-edit-part (handles &optional current-id)
4925     "Edit an article in order to delete a mime part.
4926 This function is exclusively used by `gnus-mime-save-part-and-strip'
4927 and `gnus-mime-delete-part', and not provided at run-time normally."
4928     (gnus-article-edit-article
4929      `(lambda ()
4930         (buffer-disable-undo)
4931         (erase-buffer)
4932         (let ((mail-parse-charset (or gnus-article-charset
4933                                       ',gnus-newsgroup-charset))
4934               (mail-parse-ignored-charsets
4935                (or gnus-article-ignored-charsets
4936                    ',gnus-newsgroup-ignored-charsets))
4937               (mbl mml-buffer-list))
4938           (setq mml-buffer-list nil)
4939           (insert-buffer-substring gnus-original-article-buffer)
4940           (mime-to-mml ',handles)
4941           (setq gnus-article-mime-handles nil)
4942           (let ((mbl1 mml-buffer-list))
4943             (setq mml-buffer-list mbl)
4944             (set (make-local-variable 'mml-buffer-list) mbl1))
4945           (gnus-make-local-hook 'kill-buffer-hook)
4946           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4947      `(lambda (no-highlight)
4948         (let ((mail-parse-charset (or gnus-article-charset
4949                                       ',gnus-newsgroup-charset))
4950               (message-options message-options)
4951               (message-options-set-recipient)
4952               (mail-parse-ignored-charsets
4953                (or gnus-article-ignored-charsets
4954                    ',gnus-newsgroup-ignored-charsets)))
4955           (mml-to-mime)
4956           (mml-destroy-buffers)
4957           (remove-hook 'kill-buffer-hook
4958                        'mml-destroy-buffers t)
4959           (kill-local-variable 'mml-buffer-list))
4960         (gnus-summary-edit-article-done
4961          ,(or (mail-header-references gnus-current-headers) "")
4962          ,(gnus-group-read-only-p)
4963          ,gnus-summary-buffer no-highlight))
4964      t)
4965     ;; Force buttonizing this part.
4966     (let ((gnus-mime-buttonized-part-id current-id))
4967       (gnus-article-edit-done))
4968     (gnus-configure-windows 'article)
4969     (when (and current-id (integerp gnus-auto-select-part))
4970       (gnus-article-jump-to-part
4971        (min (max (+ current-id gnus-auto-select-part) 1)
4972             (with-current-buffer gnus-article-buffer
4973               (length gnus-article-mime-handle-alist)))))))
4974
4975 (defun gnus-mime-replace-part (file)
4976   "Replace MIME part under point with an external body."
4977   ;; Useful if file has already been saved to disk
4978   (interactive
4979    (list
4980     (read-file-name "Replace MIME part with file: "
4981                     (or mm-default-directory default-directory)
4982                     nil nil)))
4983   (gnus-mime-save-part-and-strip file))
4984
4985 (defun gnus-mime-save-part-and-strip (&optional file)
4986   "Save the MIME part under point then replace it with an external body.
4987 If FILE is given, use it for the external part."
4988   (interactive)
4989   (gnus-article-check-buffer)
4990   (when (gnus-group-read-only-p)
4991     (error "The current group does not support deleting of parts"))
4992   (when (mm-complicated-handles gnus-article-mime-handles)
4993     (error "\
4994 The current article has a complicated MIME structure, giving up..."))
4995   (let* ((data (get-text-property (point) 'gnus-data))
4996          (id (get-text-property (point) 'gnus-part))
4997          param
4998          (handles gnus-article-mime-handles))
4999     (unless file
5000       (setq file
5001             (and data (mm-save-part data "Delete MIME part and save to: "))))
5002     (when file
5003       (with-current-buffer (mm-handle-buffer data)
5004         (erase-buffer)
5005         (insert "Content-Type: " (mm-handle-media-type data))
5006         (mml-insert-parameter-string (cdr (mm-handle-type data))
5007                                      '(charset))
5008         ;; Add a filename for the sake of saving the part again.
5009         (mml-insert-parameter
5010          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
5011         (insert "\n")
5012         (insert "Content-ID: " (message-make-message-id) "\n")
5013         (insert "Content-Transfer-Encoding: binary\n")
5014         (insert "\n"))
5015       (setcdr data
5016               (cdr (mm-make-handle nil
5017                                    `("message/external-body"
5018                                      (access-type . "LOCAL-FILE")
5019                                      (name . ,file)))))
5020       ;; (set-buffer gnus-summary-buffer)
5021       (gnus-article-edit-part handles id))))
5022
5023 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
5024 ;; parts...>') but with stripping would be nice.
5025
5026 (defun gnus-mime-delete-part ()
5027   "Delete the MIME part under point.
5028 Replace it with some information about the removed part."
5029   (interactive)
5030   (gnus-article-check-buffer)
5031   (when (gnus-group-read-only-p)
5032     (error "The current group does not support deleting of parts"))
5033   (when (mm-complicated-handles gnus-article-mime-handles)
5034     (error "\
5035 The current article has a complicated MIME structure, giving up..."))
5036   (when (or gnus-expert-user
5037             (gnus-yes-or-no-p "\
5038 Deleting parts may malfunction or destroy the article; continue? "))
5039     (let* ((data (get-text-property (point) 'gnus-data))
5040            (id (get-text-property (point) 'gnus-part))
5041            (handles gnus-article-mime-handles)
5042            (none "(none)")
5043            (description
5044             (let ((desc (mm-handle-description data)))
5045               (when desc
5046                 (mail-decode-encoded-word-string desc))))
5047            (filename
5048             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
5049                 none))
5050            (type (mm-handle-media-type data)))
5051       (unless data
5052         (error "No MIME part under point"))
5053       (with-current-buffer (mm-handle-buffer data)
5054         (let ((bsize (format "%s" (buffer-size))))
5055           (erase-buffer)
5056           (insert
5057            (concat
5058             ",----\n"
5059             "| The following attachment has been deleted:\n"
5060             "|\n"
5061             "| Type:           " type "\n"
5062             "| Filename:       " filename "\n"
5063             "| Size (encoded): " bsize " Byte\n"
5064             (when description
5065               (concat    "| Description:    " description "\n"))
5066             "`----\n"))
5067           (setcdr data
5068                   (cdr (mm-make-handle
5069                         nil `("text/plain" (charset . gnus-decoded)) nil nil
5070                         (list "attachment")
5071                         (format "Deleted attachment (%s bytes)" bsize))))))
5072       ;; (set-buffer gnus-summary-buffer)
5073       (gnus-article-edit-part handles id))))
5074
5075 (defun gnus-mime-save-part ()
5076   "Save the MIME part under point."
5077   (interactive)
5078   (gnus-article-check-buffer)
5079   (let ((data (get-text-property (point) 'gnus-data)))
5080     (when data
5081       (mm-save-part data))))
5082
5083 (defun gnus-mime-pipe-part (&optional cmd)
5084   "Pipe the MIME part under point to a process.
5085 Use CMD as the process."
5086   (interactive)
5087   (gnus-article-check-buffer)
5088   (let ((data (get-text-property (point) 'gnus-data)))
5089     (when data
5090       (mm-pipe-part data cmd))))
5091
5092 (defun gnus-mime-view-part ()
5093   "Interactively choose a viewing method for the MIME part under point."
5094   (interactive)
5095   (gnus-article-check-buffer)
5096   (let ((data (get-text-property (point) 'gnus-data)))
5097     (when data
5098       (setq gnus-article-mime-handles
5099             (mm-merge-handles
5100              gnus-article-mime-handles (setq data (copy-sequence data))))
5101       (mm-interactively-view-part data))))
5102
5103 (defun gnus-mime-view-part-as-type-internal ()
5104   (gnus-article-check-buffer)
5105   (let* ((handle (get-text-property (point) 'gnus-data))
5106          (name (or
5107                 ;; Content-Type: foo/bar; name=...
5108                 (mail-content-type-get (mm-handle-type handle) 'name)
5109                 ;; Content-Disposition: attachment; filename=...
5110                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
5111          (def-type (and name (mm-default-file-encoding name))))
5112     (or (and def-type (cons def-type 0))
5113         (and handle
5114              (equal (mm-handle-media-supertype handle) "text")
5115              '("text/plain" . 0))
5116         '("application/octet-stream" . 0))))
5117
5118 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
5119   "Choose a MIME media type, and view the part as such.
5120 If non-nil, PRED is a predicate to use during completion to limit the
5121 available media-types."
5122   (interactive)
5123   (unless mime-type
5124     (setq mime-type
5125           (let ((default (gnus-mime-view-part-as-type-internal)))
5126             (completing-read
5127              (format "View as MIME type (default %s): "
5128                      (car default))
5129              (mapcar #'list (mailcap-mime-types))
5130              pred nil nil nil
5131              (car default)))))
5132   (gnus-article-check-buffer)
5133   (let ((handle (get-text-property (point) 'gnus-data)))
5134     (when handle
5135       (when (equal (mm-handle-media-type handle) "message/external-body")
5136         (unless (mm-handle-cache handle)
5137           (mm-extern-cache-contents handle))
5138         (setq handle (mm-handle-cache handle)))
5139       (setq handle
5140             (mm-make-handle (mm-handle-buffer handle)
5141                             (cons mime-type (cdr (mm-handle-type handle)))
5142                             (mm-handle-encoding handle)
5143                             (mm-handle-undisplayer handle)
5144                             (mm-handle-disposition handle)
5145                             (mm-handle-description handle)
5146                             nil
5147                             (mm-handle-id handle)))
5148       (setq gnus-article-mime-handles
5149             (mm-merge-handles gnus-article-mime-handles handle))
5150       (when (mm-handle-displayed-p handle)
5151         (mm-remove-part handle))
5152       (gnus-mm-display-part handle))))
5153
5154 (defun gnus-mime-copy-part (&optional handle arg)
5155   "Put the MIME part under point into a new buffer.
5156 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
5157 are decompressed."
5158   (interactive (list nil current-prefix-arg))
5159   (gnus-article-check-buffer)
5160   (unless handle
5161     (setq handle (get-text-property (point) 'gnus-data)))
5162   (when handle
5163     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
5164                                                'name)
5165                         (mail-content-type-get (mm-handle-disposition handle)
5166                                                'filename)))
5167           contents dont-decode charset coding-system)
5168       (mm-with-unibyte-buffer
5169         (mm-insert-part handle)
5170         (setq contents (or (condition-case nil
5171                                (mm-decompress-buffer filename nil 'sig)
5172                              (error
5173                               (setq dont-decode t)
5174                               nil))
5175                            (buffer-string))))
5176       (setq filename (cond (filename (file-name-nondirectory filename))
5177                            (dont-decode "*raw data*")
5178                            (t "*decoded*")))
5179       (cond
5180        (dont-decode)
5181        ((not arg)
5182         (unless (setq charset (mail-content-type-get
5183                                (mm-handle-type handle) 'charset))
5184           (unless (setq coding-system (mm-with-unibyte-buffer
5185                                         (insert contents)
5186                                         (mm-find-buffer-file-coding-system)))
5187             (setq charset gnus-newsgroup-charset))))
5188        ((numberp arg)
5189         (setq charset (or (cdr (assq arg
5190                                      gnus-summary-show-article-charset-alist))
5191                           (mm-read-coding-system "Charset: ")))))
5192       (switch-to-buffer (generate-new-buffer filename))
5193       (if (or coding-system
5194               (and charset
5195                    (setq coding-system (mm-charset-to-coding-system charset))
5196                    (not (eq charset 'ascii))))
5197           (progn
5198             (mm-enable-multibyte)
5199             (insert (mm-decode-coding-string contents coding-system))
5200             (setq buffer-file-coding-system
5201                   (if (boundp 'last-coding-system-used)
5202                       (symbol-value 'last-coding-system-used)
5203                     coding-system)))
5204         (mm-disable-multibyte)
5205         (insert contents)
5206         (setq buffer-file-coding-system mm-binary-coding-system))
5207       ;; We do it this way to make `normal-mode' set the appropriate mode.
5208       (unwind-protect
5209           (progn
5210             (setq buffer-file-name (expand-file-name filename))
5211             (normal-mode))
5212         (setq buffer-file-name nil))
5213       (goto-char (point-min)))))
5214
5215 (defun gnus-mime-print-part (&optional handle filename)
5216   "Print the MIME part under point."
5217   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5218   (gnus-article-check-buffer)
5219   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5220          (contents (and handle (mm-get-part handle)))
5221          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5222          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5223     (when contents
5224         (if printer
5225             (unwind-protect
5226                 (progn
5227                   (mm-save-part-to-file handle file)
5228                   (call-process shell-file-name nil
5229                                 (generate-new-buffer " *mm*")
5230                                 nil
5231                                 shell-command-switch
5232                                 (mm-mailcap-command
5233                                  printer file (mm-handle-type handle))))
5234               (delete-file file))
5235           (with-temp-buffer
5236             (insert contents)
5237             (gnus-print-buffer))
5238           (ps-despool filename)))))
5239
5240 (defun gnus-mime-inline-part (&optional handle arg)
5241   "Insert the MIME part under point into the current buffer.
5242 Compressed files like .gz and .bz2 are decompressed."
5243   (interactive (list nil current-prefix-arg))
5244   (gnus-article-check-buffer)
5245   (unless handle
5246     (setq handle (get-text-property (point) 'gnus-data)))
5247   (when handle
5248     (let ((b (point))
5249           (inhibit-read-only t)
5250           contents charset coding-system)
5251       (if (and (not arg) (mm-handle-undisplayer handle))
5252           (mm-remove-part handle)
5253         (mm-with-unibyte-buffer
5254           (mm-insert-part handle)
5255           (setq contents
5256                 (or (mm-decompress-buffer
5257                      (or (mail-content-type-get (mm-handle-type handle)
5258                                                 'name)
5259                          (mail-content-type-get (mm-handle-disposition handle)
5260                                                 'filename))
5261                      nil t)
5262                     (buffer-string))))
5263         (cond
5264          ((not arg)
5265           (unless (setq charset (mail-content-type-get
5266                                  (mm-handle-type handle) 'charset))
5267             (unless (setq coding-system
5268                           (mm-with-unibyte-buffer
5269                             (insert contents)
5270                             (mm-find-buffer-file-coding-system)))
5271               (setq charset gnus-newsgroup-charset))))
5272          ((numberp arg)
5273           (if (mm-handle-undisplayer handle)
5274               (mm-remove-part handle))
5275           (setq charset
5276                 (or (cdr (assq arg
5277                                gnus-summary-show-article-charset-alist))
5278                     (mm-read-coding-system "Charset: "))))
5279          (t
5280           (if (mm-handle-undisplayer handle)
5281               (mm-remove-part handle))))
5282         (forward-line 2)
5283         (mm-insert-inline
5284          handle
5285          (if (or coding-system
5286                  (and charset
5287                       (setq coding-system
5288                             (mm-charset-to-coding-system charset))
5289                       (not (eq coding-system 'ascii))))
5290              (mm-decode-coding-string contents coding-system)
5291            (mm-string-to-multibyte contents)))
5292         (goto-char b)))))
5293
5294 (defun gnus-mime-set-charset-parameters (handle charset)
5295   "Set CHARSET to parameters in HANDLE.
5296 CHARSET may either be a string or a symbol."
5297   (unless (stringp charset)
5298     (setq charset (symbol-name charset)))
5299   (if (stringp (car handle))
5300       (dolist (h (cdr handle))
5301         (gnus-mime-set-charset-parameters h charset))
5302     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5303                                             "message/external-body")
5304                                      (progn
5305                                        (unless (mm-handle-cache handle)
5306                                          (mm-extern-cache-contents handle))
5307                                        (mm-handle-cache handle))
5308                                    handle)))
5309            (param (assq 'charset (cdr type))))
5310       (if param
5311           (setcdr param charset)
5312         (setcdr type (cons (cons 'charset charset) (cdr type)))))))
5313
5314 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5315   "Insert the MIME part under point into the current buffer using the
5316 specified charset."
5317   (interactive (list nil current-prefix-arg))
5318   (gnus-article-check-buffer)
5319   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5320         (fun (get-text-property (point) 'gnus-callback))
5321         (gnus-newsgroup-ignored-charsets 'gnus-all)
5322         charset form preferred parts)
5323     (when handle
5324       (when (prog1
5325                 (and fun
5326                      (setq charset
5327                            (or (cdr (assq
5328                                      arg
5329                                      gnus-summary-show-article-charset-alist))
5330                                (mm-read-coding-system "Charset: "))))
5331               (if (mm-handle-undisplayer handle)
5332                   (mm-remove-part handle)))
5333         (gnus-mime-set-charset-parameters handle charset)
5334         (when (and (consp (setq form (cdr-safe fun)))
5335                    (setq form (ignore-errors
5336                                 (assq 'gnus-mime-display-alternative form)))
5337                    (setq preferred (caddr form))
5338                    (progn
5339                      (when (eq (car preferred) 'quote)
5340                        (setq preferred (cadr preferred)))
5341                      (not (equal preferred
5342                                  (get-text-property (point) 'gnus-data))))
5343                    (setq parts (get-text-property (point) 'gnus-part))
5344                    (setq parts (cdr (assq parts
5345                                           gnus-article-mime-handle-alist)))
5346                    (equal (mm-handle-media-type parts) "multipart/alternative")
5347                    (setq parts (reverse (cdr parts))))
5348           (setcar (cddr form)
5349                   (list 'quote (or (cadr (member preferred parts))
5350                                    (car parts)))))
5351         (funcall fun handle)))))
5352
5353 (defun gnus-mime-view-part-externally (&optional handle)
5354   "View the MIME part under point with an external viewer."
5355   (interactive)
5356   (gnus-article-check-buffer)
5357   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5358          (mm-user-display-methods nil)
5359          (mm-inlined-types nil)
5360          (mail-parse-charset gnus-newsgroup-charset)
5361          (mail-parse-ignored-charsets
5362           (with-current-buffer gnus-summary-buffer
5363             gnus-newsgroup-ignored-charsets))
5364          (type (mm-handle-media-type handle))
5365          (method (mailcap-mime-info type))
5366          (mm-enable-external t))
5367     (if (not (stringp method))
5368         (gnus-mime-view-part-as-type
5369          nil (lambda (types) (stringp (mailcap-mime-info (car types)))))
5370       (when handle
5371         (if (mm-handle-undisplayer handle)
5372             (mm-remove-part handle)
5373           (mm-display-part handle))))))
5374
5375 (defun gnus-mime-view-part-internally (&optional handle)
5376   "View the MIME part under point with an internal viewer.
5377 If no internal viewer is available, use an external viewer."
5378   (interactive)
5379   (gnus-article-check-buffer)
5380   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5381          (mm-inlined-types '(".*"))
5382          (mm-inline-large-images t)
5383          (mail-parse-charset gnus-newsgroup-charset)
5384          (mail-parse-ignored-charsets
5385           (with-current-buffer gnus-summary-buffer
5386             gnus-newsgroup-ignored-charsets))
5387          (inhibit-read-only t))
5388     (if (not (mm-inlinable-p handle))
5389         (gnus-mime-view-part-as-type
5390          nil (lambda (types) (mm-inlinable-p handle (car types))))
5391       (when handle
5392         (if (mm-handle-undisplayer handle)
5393             (mm-remove-part handle)
5394           (gnus-bind-safe-url-regexp (mm-display-part handle)))))))
5395
5396 (defun gnus-mime-action-on-part (&optional action)
5397   "Do something with the MIME attachment at \(point\)."
5398   (interactive
5399    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
5400   (gnus-article-check-buffer)
5401   (let ((action-pair (assoc action gnus-mime-action-alist)))
5402     (if action-pair
5403         (funcall (cdr action-pair)))))
5404
5405 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5406   "Call FUNCTION on MIME part N.
5407 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5408 If INTERACTIVE, call FUNCTION interactivly."
5409   (let (window frame)
5410     ;; Check whether the article is displayed.
5411     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5412                  (setq window (get-buffer-window gnus-article-buffer t))
5413                  (frame-visible-p (setq frame (window-frame window))))
5414       (error "No article is displayed"))
5415     (with-current-buffer gnus-article-buffer
5416       ;; Check whether the article displays the right contents.
5417       (unless (with-current-buffer gnus-summary-buffer
5418                 (eq gnus-current-article (gnus-summary-article-number)))
5419         (error "You should select the right article first"))
5420       (if n
5421           (setq n (prefix-numeric-value n))
5422         (let ((pt (point)))
5423           (setq n (or (get-text-property pt 'gnus-part)
5424                       (and (not (bobp))
5425                            (get-text-property (1- pt) 'gnus-part))
5426                       (get-text-property (prog2
5427                                              (forward-line 1)
5428                                              (point)
5429                                            (goto-char pt))
5430                                          'gnus-part)
5431                       (get-text-property
5432                        (or (and (setq pt (previous-single-property-change
5433                                           pt 'gnus-part))
5434                                 (1- pt))
5435                            (next-single-property-change (point) 'gnus-part)
5436                            (point))
5437                        'gnus-part)
5438                       1))))
5439       ;; Check whether the specified part exists.
5440       (when (> n (length gnus-article-mime-handle-alist))
5441         (error "No such part")))
5442     (unless
5443         (progn
5444           ;; To select the window is needed so that the cursor
5445           ;; might be visible on the MIME button.
5446           (select-window (prog1
5447                              window
5448                            (setq window (selected-window))
5449                            ;; Article may be displayed in the other frame.
5450                            (gnus-select-frame-set-input-focus
5451                             (prog1
5452                                 frame
5453                               (setq frame (selected-frame))))))
5454           (when (gnus-article-goto-part n)
5455             ;; We point the cursor and the arrow at the MIME button
5456             ;; when the `function' prompt the user for something.
5457             (let ((cursor-in-non-selected-windows t)
5458                   (overlay-arrow-string "=>")
5459                   (overlay-arrow-position (point-marker)))
5460               (unwind-protect
5461                   (cond
5462                    ((and no-handle interactive)
5463                     (call-interactively function))
5464                    (no-handle
5465                     (funcall function))
5466                    (interactive
5467                     (call-interactively
5468                      function
5469                      (cdr (assq n gnus-article-mime-handle-alist))))
5470                    (t
5471                     (funcall function
5472                              (cdr (assq n gnus-article-mime-handle-alist)))))
5473                 (set-marker overlay-arrow-position nil)
5474                 (unless gnus-auto-select-part
5475                   (gnus-select-frame-set-input-focus frame)
5476                   (select-window window))))
5477             t))
5478       (if gnus-inhibit-mime-unbuttonizing
5479           ;; This is the default though the program shouldn't reach here.
5480           (error "No such part")
5481         ;; The part which doesn't have the MIME button is selected.
5482         ;; So, we display all the buttons and redo it.
5483         (let ((gnus-inhibit-mime-unbuttonizing t))
5484           (gnus-summary-show-article)
5485           (gnus-article-part-wrapper n function no-handle))))))
5486
5487 (defun gnus-article-pipe-part (n)
5488   "Pipe MIME part N, which is the numerical prefix."
5489   (interactive "P")
5490   (gnus-article-part-wrapper n 'mm-pipe-part))
5491
5492 (defun gnus-article-save-part (n)
5493   "Save MIME part N, which is the numerical prefix."
5494   (interactive "P")
5495   (gnus-article-part-wrapper n 'mm-save-part))
5496
5497 (defun gnus-article-interactively-view-part (n)
5498   "View MIME part N interactively, which is the numerical prefix."
5499   (interactive "P")
5500   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5501
5502 (defun gnus-article-copy-part (n)
5503   "Copy MIME part N, which is the numerical prefix."
5504   (interactive "P")
5505   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5506
5507 (defun gnus-article-view-part-as-charset (n)
5508   "View MIME part N using a specified charset.
5509 N is the numerical prefix."
5510   (interactive "P")
5511   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5512
5513 (defun gnus-article-view-part-externally (n)
5514   "View MIME part N externally, which is the numerical prefix."
5515   (interactive "P")
5516   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5517
5518 (defun gnus-article-inline-part (n)
5519   "Inline MIME part N, which is the numerical prefix."
5520   (interactive "P")
5521   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5522
5523 (defun gnus-article-save-part-and-strip (n)
5524   "Save MIME part N and replace it with an external body.
5525 N is the numerical prefix."
5526   (interactive "P")
5527   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5528
5529 (defun gnus-article-replace-part (n)
5530   "Replace MIME part N with an external body.
5531 N is the numerical prefix."
5532   (interactive "P")
5533   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5534
5535 (defun gnus-article-delete-part (n)
5536   "Delete MIME part N and add some information about the removed part.
5537 N is the numerical prefix."
5538   (interactive "P")
5539   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5540
5541 (defun gnus-article-view-part-as-type (n)
5542   "Choose a MIME media type, and view part N as such.
5543 N is the numerical prefix."
5544   (interactive "P")
5545   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5546
5547 (defun gnus-article-mime-match-handle-first (condition)
5548   (if condition
5549       (let (n)
5550         (dolist (ihandle gnus-article-mime-handle-alist)
5551           (if (and (cond
5552                     ((functionp condition)
5553                      (funcall condition (cdr ihandle)))
5554                     ((eq condition 'undisplayed)
5555                      (not (or (mm-handle-undisplayer (cdr ihandle))
5556                               (equal (mm-handle-media-type (cdr ihandle))
5557                                      "multipart/alternative"))))
5558                     ((eq condition 'undisplayed-alternative)
5559                      (not (mm-handle-undisplayer (cdr ihandle))))
5560                     (t t))
5561                    (gnus-article-goto-part (car ihandle))
5562                    (or (not n) (< (car ihandle) n)))
5563               (setq n (car ihandle))))
5564         (or n 1))
5565     1))
5566
5567 (defun gnus-article-view-part (&optional n)
5568   "View MIME part N, which is the numerical prefix.
5569 If the part is already shown, hide the part.  If N is nil, view
5570 all parts."
5571   (interactive "P")
5572   (with-current-buffer gnus-article-buffer
5573     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5574                              gnus-article-mime-match-handle-function)))
5575     (when (> n (length gnus-article-mime-handle-alist))
5576       (error "No such part"))
5577     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5578       (when (gnus-article-goto-part n)
5579         (if (equal (car handle) "multipart/alternative")
5580             (gnus-article-press-button)
5581           (when (eq (gnus-mm-display-part handle) 'internal)
5582             (gnus-set-window-start)))))))
5583
5584 (defsubst gnus-article-mime-total-parts ()
5585   (if (bufferp (car gnus-article-mime-handles))
5586       1 ;; single part
5587     (1- (length gnus-article-mime-handles))))
5588
5589 (defun gnus-mm-display-part (handle)
5590   "Display HANDLE and fix MIME button."
5591   (let ((id (get-text-property (point) 'gnus-part))
5592         (point (point))
5593         (inhibit-read-only t))
5594     (forward-line 1)
5595     (prog1
5596         (let ((window (selected-window))
5597               (mail-parse-charset gnus-newsgroup-charset)
5598               (mail-parse-ignored-charsets
5599                (if (gnus-buffer-live-p gnus-summary-buffer)
5600                    (with-current-buffer gnus-summary-buffer
5601                      gnus-newsgroup-ignored-charsets)
5602                  nil)))
5603           (save-excursion
5604             (unwind-protect
5605                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5606                       (beg (point)))
5607                   (when win
5608                     (select-window win))
5609                   (goto-char point)
5610                   (forward-line)
5611                   (if (mm-handle-displayed-p handle)
5612                       ;; This will remove the part.
5613                       (mm-display-part handle)
5614                     (save-restriction
5615                       (narrow-to-region (point)
5616                                         (if (eobp) (point) (1+ (point))))
5617                       (gnus-bind-safe-url-regexp (mm-display-part handle))
5618                       ;; We narrow to the part itself and
5619                       ;; then call the treatment functions.
5620                       (goto-char (point-min))
5621                       (forward-line 1)
5622                       (narrow-to-region (point) (point-max))
5623                       (gnus-treat-article
5624                        nil id
5625                        (gnus-article-mime-total-parts)
5626                        (mm-handle-media-type handle)))))
5627               (if (window-live-p window)
5628                   (select-window window)))))
5629       (goto-char point)
5630       (gnus-delete-line)
5631       (gnus-insert-mime-button
5632        handle id (list (mm-handle-displayed-p handle)))
5633       (goto-char point))))
5634
5635 (defun gnus-article-goto-part (n)
5636   "Go to MIME part N."
5637   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
5638
5639 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5640   (let ((gnus-tmp-name
5641          (or (mail-content-type-get (mm-handle-type handle) 'name)
5642              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5643              (mail-content-type-get (mm-handle-type handle) 'url)
5644              ""))
5645         (gnus-tmp-type (mm-handle-media-type handle))
5646         (gnus-tmp-description (or (mm-handle-description handle) ""))
5647         (gnus-tmp-dots
5648          (if (if displayed (car displayed)
5649                (mm-handle-displayed-p handle))
5650              "" "..."))
5651         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5652                            (buffer-size)))
5653         gnus-tmp-type-long b e)
5654     (when (string-match ".*/" gnus-tmp-name)
5655       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5656     (setq gnus-tmp-type-long (concat gnus-tmp-type
5657                                      (and (not (equal gnus-tmp-name ""))
5658                                           (concat "; " gnus-tmp-name))))
5659     (unless (equal gnus-tmp-description "")
5660       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5661     (unless (bolp)
5662       (insert "\n"))
5663     (setq b (point))
5664     (gnus-eval-format
5665      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5666      `(keymap ,gnus-mime-button-map
5667               gnus-callback gnus-mm-display-part
5668               gnus-part ,gnus-tmp-id
5669               article-type annotation
5670               gnus-data ,handle))
5671     (setq e (if (bolp)
5672                 ;; Exclude a newline.
5673                 (1- (point))
5674               (point)))
5675     (when gnus-article-button-face
5676       (gnus-overlay-put (gnus-make-overlay b e nil t)
5677                         'face gnus-article-button-face))
5678     (widget-convert-button
5679      'link b e
5680      :mime-handle handle
5681      :action 'gnus-widget-press-button
5682      :button-keymap gnus-mime-button-map
5683      :help-echo
5684      (lambda (widget/window &optional overlay pos)
5685        ;; Needed to properly clear the message due to a bug in
5686        ;; wid-edit (XEmacs only).
5687        (if (boundp 'help-echo-owns-message)
5688            (setq help-echo-owns-message t))
5689        (format
5690         "%S: %s the MIME part; %S: more options"
5691         (aref gnus-mouse-2 0)
5692         ;; XEmacs will get a single widget arg; Emacs 21 will get
5693         ;; window, overlay, position.
5694         (if (mm-handle-displayed-p
5695              (if overlay
5696                  (with-current-buffer (gnus-overlay-buffer overlay)
5697                    (widget-get (widget-at (gnus-overlay-start overlay))
5698                                :mime-handle))
5699                (widget-get widget/window :mime-handle)))
5700             "hide" "show")
5701         (aref gnus-down-mouse-3 0))))))
5702
5703 (defun gnus-widget-press-button (elems el)
5704   (goto-char (widget-get elems :from))
5705   (gnus-article-press-button))
5706
5707 (defvar gnus-displaying-mime nil)
5708
5709 (defun gnus-display-mime (&optional ihandles)
5710   "Display the MIME parts."
5711   (save-excursion
5712     (save-selected-window
5713       (let ((window (get-buffer-window gnus-article-buffer))
5714             (point (point)))
5715         (when window
5716           (select-window window)
5717           ;; We have to do this since selecting the window
5718           ;; may change the point.  So we set the window point.
5719           (set-window-point window point)))
5720       (let ((handles ihandles)
5721             (inhibit-read-only t)
5722             handle)
5723         (cond (handles)
5724               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5725                (when gnus-article-emulate-mime
5726                  (mm-uu-dissect-text-parts handles)))
5727               (gnus-article-emulate-mime
5728                (setq handles (mm-uu-dissect))))
5729         (when (and (not ihandles)
5730                    (not gnus-displaying-mime))
5731           ;; Top-level call; we clean up.
5732           (when gnus-article-mime-handles
5733             (mm-destroy-parts gnus-article-mime-handles)
5734             (setq gnus-article-mime-handle-alist nil));; A trick.
5735           (setq gnus-article-mime-handles handles)
5736           ;; We allow users to glean info from the handles.
5737           (when gnus-article-mime-part-function
5738             (gnus-mime-part-function handles)))
5739         (if (and handles
5740                  (or (not (stringp (car handles)))
5741                      (cdr handles)))
5742             (progn
5743               (when (and (not ihandles)
5744                          (not gnus-displaying-mime))
5745                 ;; Clean up for mime parts.
5746                 (article-goto-body)
5747                 (delete-region (point) (point-max)))
5748               (let ((gnus-displaying-mime t))
5749                 (gnus-mime-display-part handles)))
5750           (save-restriction
5751             (article-goto-body)
5752             (narrow-to-region (point) (point-max))
5753             (gnus-treat-article nil 1 1)
5754             (widen)))
5755         (unless ihandles
5756           ;; Highlight the headers.
5757           (save-excursion
5758             (save-restriction
5759               (article-goto-body)
5760               (narrow-to-region (point-min) (point))
5761               (gnus-article-save-original-date
5762                (gnus-treat-article 'head)))))))
5763     ;; Cope with broken MIME messages.
5764     (goto-char (point-max))
5765     (unless (bolp)
5766       (insert "\n"))))
5767
5768 (defcustom gnus-mime-display-multipart-as-mixed nil
5769   "Display \"multipart\" parts as  \"multipart/mixed\".
5770
5771 If t, it overrides nil values of
5772 `gnus-mime-display-multipart-alternative-as-mixed' and
5773 `gnus-mime-display-multipart-related-as-mixed'."
5774   :group 'gnus-article-mime
5775   :type 'boolean)
5776
5777 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5778   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5779   :version "22.1"
5780   :group 'gnus-article-mime
5781   :type 'boolean)
5782
5783 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5784   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5785
5786 If displaying \"text/html\" is discouraged \(see
5787 `mm-discouraged-alternatives'\) images or other material inside a
5788 \"multipart/related\" part might be overlooked when this variable is nil."
5789   :version "22.1"
5790   :group 'gnus-article-mime
5791   :type 'boolean)
5792
5793 (defun gnus-mime-display-part (handle)
5794   (cond
5795    ;; Maybe a broken MIME message.
5796    ((null handle))
5797    ;; Single part.
5798    ((not (stringp (car handle)))
5799     (gnus-mime-display-single handle))
5800    ;; User-defined multipart
5801    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5802     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5803              handle))
5804    ;; multipart/alternative
5805    ((and (equal (car handle) "multipart/alternative")
5806          (not (or gnus-mime-display-multipart-as-mixed
5807                   gnus-mime-display-multipart-alternative-as-mixed)))
5808     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5809       (push (cons id handle) gnus-article-mime-handle-alist)
5810       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5811    ;; multipart/related
5812    ((and (equal (car handle) "multipart/related")
5813          (not (or gnus-mime-display-multipart-as-mixed
5814                   gnus-mime-display-multipart-related-as-mixed)))
5815     ;;;!!!We should find the start part, but we just default
5816     ;;;!!!to the first part.
5817     ;;(gnus-mime-display-part (cadr handle))
5818     ;;;!!! Most multipart/related is an HTML message plus images.
5819     ;;;!!! Unfortunately we are unable to let W3 display those
5820     ;;;!!! included images, so we just display it as a mixed multipart.
5821     ;;(gnus-mime-display-mixed (cdr handle))
5822     ;;;!!! No, w3 can display everything just fine.
5823     (gnus-mime-display-part (cadr handle)))
5824    ((equal (car handle) "multipart/signed")
5825     (gnus-add-wash-type 'signed)
5826     (gnus-mime-display-security handle))
5827    ((equal (car handle) "multipart/encrypted")
5828     (gnus-add-wash-type 'encrypted)
5829     (gnus-mime-display-security handle))
5830    ;; Other multiparts are handled like multipart/mixed.
5831    (t
5832     (gnus-mime-display-mixed (cdr handle)))))
5833
5834 (defun gnus-mime-part-function (handles)
5835   (if (stringp (car handles))
5836       (mapcar 'gnus-mime-part-function (cdr handles))
5837     (funcall gnus-article-mime-part-function handles)))
5838
5839 (defun gnus-mime-display-mixed (handles)
5840   (mapcar 'gnus-mime-display-part handles))
5841
5842 (defun gnus-mime-display-single (handle)
5843   (let ((type (mm-handle-media-type handle))
5844         (ignored gnus-ignored-mime-types)
5845         (not-attachment t)
5846         (move nil)
5847         display text)
5848     (catch 'ignored
5849       (progn
5850         (while ignored
5851           (when (string-match (pop ignored) type)
5852             (throw 'ignored nil)))
5853         (if (and (setq not-attachment
5854                        (and (not (mm-inline-override-p handle))
5855                             (or (not (mm-handle-disposition handle))
5856                                 (equal (car (mm-handle-disposition handle))
5857                                        "inline")
5858                                 (mm-attachment-override-p handle))))
5859                  (mm-automatic-display-p handle)
5860                  (or (and
5861                       (mm-inlinable-p handle)
5862                       (mm-inlined-p handle))
5863                      (mm-automatic-external-display-p type)))
5864             (setq display t)
5865           (when (equal (mm-handle-media-supertype handle) "text")
5866             (setq text t)))
5867         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5868               beg)
5869           (push (cons id handle) gnus-article-mime-handle-alist)
5870           (when (and display
5871                      (equal (mm-handle-media-supertype handle) "message"))
5872             (insert-char
5873              ?\n
5874              (cond ((not (bolp)) 2)
5875                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5876                    (t 1))))
5877           (when (or (not display)
5878                     (not (gnus-unbuttonized-mime-type-p type))
5879                     (eq id gnus-mime-buttonized-part-id))
5880             (gnus-insert-mime-button
5881              handle id (list (or display (and not-attachment text))))
5882             (gnus-article-insert-newline)
5883             ;; Remember modify the number of forward lines.
5884             (setq move t))
5885           (setq beg (point))
5886           (cond
5887            (display
5888             (when move
5889               (forward-line -1)
5890               (setq beg (point)))
5891             (let ((mail-parse-charset gnus-newsgroup-charset)
5892                   (mail-parse-ignored-charsets
5893                    (save-excursion (condition-case ()
5894                                        (set-buffer gnus-summary-buffer)
5895                                      (error))
5896                                    gnus-newsgroup-ignored-charsets)))
5897               (gnus-bind-safe-url-regexp (mm-display-part handle t)))
5898             (goto-char (point-max)))
5899            ((and text not-attachment)
5900             (when move
5901               (forward-line -1)
5902               (setq beg (point)))
5903             (gnus-article-insert-newline)
5904             (mm-insert-inline
5905              handle
5906              (let ((charset (or (mail-content-type-get (mm-handle-type handle)
5907                                                        'charset)
5908                                 (and (equal type "text/calendar") 'utf-8))))
5909                (cond ((not charset)
5910                       (mm-string-as-multibyte (mm-get-part handle)))
5911                      ((eq charset 'gnus-decoded)
5912                       (with-current-buffer (mm-handle-buffer handle)
5913                         (buffer-string)))
5914                      (t
5915                       (mm-decode-string (mm-get-part handle) charset)))))
5916             (goto-char (point-max))))
5917           ;; Do highlighting.
5918           (save-excursion
5919             (save-restriction
5920               (narrow-to-region beg (point))
5921               (if (eq handle gnus-article-mime-handles)
5922                   ;; The format=flowed case.
5923                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5924                 ;; Don't count signature parts that are never displayed.
5925                 ;; The part number should be re-calculated supposing this
5926                 ;; might be a message/rfc822 part.
5927                 (let (handles)
5928                   (dolist (part gnus-article-mime-handles)
5929                     (unless (or (stringp part)
5930                                 (equal (car (mm-handle-type part))
5931                                        "application/pgp-signature"))
5932                       (push part handles)))
5933                   (gnus-treat-article
5934                    nil (length (memq handle handles)) (length handles)
5935                    (mm-handle-media-type handle)))))))))))
5936
5937 (defun gnus-unbuttonized-mime-type-p (type)
5938   "Say whether TYPE is to be unbuttonized."
5939   (unless gnus-inhibit-mime-unbuttonizing
5940     (when (catch 'found
5941             (let ((types gnus-unbuttonized-mime-types))
5942               (while types
5943                 (when (string-match (pop types) type)
5944                   (throw 'found t)))))
5945       (not (catch 'found
5946              (let ((types gnus-buttonized-mime-types))
5947                (while types
5948                  (when (string-match (pop types) type)
5949                    (throw 'found t)))))))))
5950
5951 (defun gnus-article-insert-newline ()
5952   "Insert a newline, but mark it as undeletable."
5953   (gnus-put-text-property
5954    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5955
5956 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5957   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5958          (ihandles handles)
5959          (point (point))
5960          handle (inhibit-read-only t) from props begend not-pref)
5961     (save-window-excursion
5962       (save-restriction
5963         (when ibegend
5964           (narrow-to-region (car ibegend)
5965                             (or (cdr ibegend)
5966                                 (progn
5967                                   (goto-char (car ibegend))
5968                                   (forward-line 2)
5969                                   (point))))
5970           (delete-region (point-min) (point-max))
5971           (mm-remove-parts handles))
5972         (setq begend (list (point-marker)))
5973         ;; Do the toggle.
5974         (unless (setq not-pref (cadr (member preferred ihandles)))
5975           (setq not-pref (car ihandles)))
5976         (when (or ibegend
5977                   (not preferred)
5978                   (not (gnus-unbuttonized-mime-type-p
5979                         "multipart/alternative")))
5980           (gnus-add-text-properties
5981            (setq from (point))
5982            (progn
5983              (insert (format "%d.  " id))
5984              (point))
5985            `(gnus-callback
5986              (lambda (handles)
5987                (unless ,(not ibegend)
5988                  (setq gnus-article-mime-handle-alist
5989                        ',gnus-article-mime-handle-alist))
5990                (gnus-mime-display-alternative
5991                 ',ihandles ',not-pref ',begend ,id))
5992              keymap ,gnus-mime-button-map
5993              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5994              face ,gnus-article-button-face
5995              gnus-part ,id
5996              article-type multipart))
5997           (widget-convert-button 'link from (point)
5998                                  :action 'gnus-widget-press-button
5999                                  :button-keymap gnus-widget-button-keymap)
6000           ;; Do the handles
6001           (while (setq handle (pop handles))
6002             (gnus-add-text-properties
6003              (setq from (point))
6004              (progn
6005                (insert (format "(%c) %-18s"
6006                                (if (equal handle preferred) ?* ? )
6007                                (mm-handle-media-type handle)))
6008                (point))
6009              `(gnus-callback
6010                (lambda (handles)
6011                  (unless ,(not ibegend)
6012                    (setq gnus-article-mime-handle-alist
6013                          ',gnus-article-mime-handle-alist))
6014                  (gnus-mime-display-alternative
6015                   ',ihandles ',handle ',begend ,id))
6016                keymap ,gnus-mime-button-map
6017                ,gnus-mouse-face-prop ,gnus-article-mouse-face
6018                face ,gnus-article-button-face
6019                gnus-part ,id
6020                gnus-data ,handle))
6021             (widget-convert-button 'link from (point)
6022                                    :action 'gnus-widget-press-button
6023                                    :button-keymap gnus-widget-button-keymap)
6024             (insert "  "))
6025           (insert "\n\n"))
6026         (when preferred
6027           (if (stringp (car preferred))
6028               (gnus-display-mime preferred)
6029             (let ((mail-parse-charset gnus-newsgroup-charset)
6030                   (mail-parse-ignored-charsets
6031                    (with-current-buffer gnus-summary-buffer
6032                      gnus-newsgroup-ignored-charsets)))
6033               (gnus-bind-safe-url-regexp (mm-display-part preferred))
6034               ;; Do highlighting.
6035               (save-excursion
6036                 (save-restriction
6037                   (narrow-to-region (car begend) (point-max))
6038                   (gnus-treat-article
6039                    nil (length gnus-article-mime-handle-alist)
6040                    (gnus-article-mime-total-parts)
6041                    (mm-handle-media-type handle))))))
6042           (goto-char (point-max))
6043           (setcdr begend (point-marker)))))
6044     (when ibegend
6045       (goto-char point))))
6046
6047 (defconst gnus-article-wash-status-strings
6048   (let ((alist '((cite "c" "Possible hidden citation text"
6049                        " " "All citation text visible")
6050                  (headers "h" "Hidden headers"
6051                           " " "All headers visible.")
6052                  (pgp "p" "Encrypted or signed message status hidden"
6053                       " " "No hidden encryption nor digital signature status")
6054                  (signature "s" "Signature has been hidden"
6055                             " " "Signature is visible")
6056                  (overstrike "o" "Overstrike (^H) characters applied"
6057                              " " "No overstrike characters applied")
6058                  (emphasis "e" "/*_Emphasis_*/ characters applied"
6059                            " " "No /*_emphasis_*/ characters applied")))
6060         result)
6061     (dolist (entry alist result)
6062       (let ((key (nth 0 entry))
6063             (on (copy-sequence (nth 1 entry)))
6064             (on-help (nth 2 entry))
6065             (off (copy-sequence (nth 3 entry)))
6066             (off-help (nth 4 entry)))
6067         (put-text-property 0 1 'help-echo on-help on)
6068         (put-text-property 0 1 'help-echo off-help off)
6069         (push (list key on off) result))))
6070   "Alist of strings describing wash status in the mode line.
6071 Each entry has the form (KEY ON OF), where the KEY is a symbol
6072 representing the particular washing function, ON is the string to use
6073 in the article mode line when the washing function is active, and OFF
6074 is the string to use when it is inactive.")
6075
6076 (defun gnus-article-wash-status-entry (key value)
6077   (let ((entry (assoc key gnus-article-wash-status-strings)))
6078     (if value (nth 1 entry) (nth 2 entry))))
6079
6080 (defun gnus-article-wash-status ()
6081   "Return a string which display status of article washing."
6082   (with-current-buffer gnus-article-buffer
6083     (let ((cite (memq 'cite gnus-article-wash-types))
6084           (headers (memq 'headers gnus-article-wash-types))
6085           (boring (memq 'boring-headers gnus-article-wash-types))
6086           (pgp (memq 'pgp gnus-article-wash-types))
6087           (pem (memq 'pem gnus-article-wash-types))
6088           (signed (memq 'signed gnus-article-wash-types))
6089           (encrypted (memq 'encrypted gnus-article-wash-types))
6090           (signature (memq 'signature gnus-article-wash-types))
6091           (overstrike (memq 'overstrike gnus-article-wash-types))
6092           (emphasis (memq 'emphasis gnus-article-wash-types)))
6093       (concat
6094        (gnus-article-wash-status-entry 'cite cite)
6095        (gnus-article-wash-status-entry 'headers (or headers boring))
6096        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
6097        (gnus-article-wash-status-entry 'signature signature)
6098        (gnus-article-wash-status-entry 'overstrike overstrike)
6099        (gnus-article-wash-status-entry 'emphasis emphasis)))))
6100
6101 (defun gnus-add-wash-type (type)
6102   "Add a washing of TYPE to the current status."
6103   (add-to-list 'gnus-article-wash-types type))
6104
6105 (defun gnus-delete-wash-type (type)
6106   "Add a washing of TYPE to the current status."
6107   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
6108
6109 (defun gnus-add-image (category image)
6110   "Add IMAGE of CATEGORY to the list of displayed images."
6111   (let ((entry (assq category gnus-article-image-alist)))
6112     (unless entry
6113       (setq entry (list category))
6114       (push entry gnus-article-image-alist))
6115     (nconc entry (list image))))
6116
6117 (defun gnus-delete-images (category)
6118   "Delete all images in CATEGORY."
6119   (let ((entry (assq category gnus-article-image-alist)))
6120     (dolist (image (cdr entry))
6121       (gnus-remove-image image category))
6122     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
6123     (gnus-delete-wash-type category)))
6124
6125 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
6126
6127 (defun gnus-article-maybe-hide-headers ()
6128   "Hide unwanted headers if `gnus-have-all-headers' is nil.
6129 Provided for backwards compatibility."
6130   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
6131                  (not (with-current-buffer gnus-summary-buffer
6132                         gnus-have-all-headers)))
6133              (not gnus-inhibit-hiding))
6134     (gnus-article-hide-headers)))
6135
6136 ;;; Article savers.
6137
6138 (defun gnus-output-to-file (file-name)
6139   "Append the current article to a file named FILE-NAME.
6140 If `gnus-article-save-coding-system' is non-nil, it is used to encode
6141 text and used as the value of the coding cookie which is added to the
6142 top of a file.  Otherwise, this function saves a raw article without
6143 the coding cookie."
6144   (let* ((artbuf (current-buffer))
6145          (file-name-coding-system nnmail-pathname-coding-system)
6146          (coding gnus-article-save-coding-system)
6147          (coding-system-for-read (if coding
6148                                      nil ;; Rely on the coding cookie.
6149                                    mm-text-coding-system))
6150          (coding-system-for-write (or coding
6151                                       mm-text-coding-system-for-write
6152                                       mm-text-coding-system))
6153          (exists (file-exists-p file-name)))
6154     (with-temp-buffer
6155       (when exists
6156         (insert-file-contents file-name)
6157         (goto-char (point-min))
6158         ;; Remove the existing coding cookie.
6159         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
6160           (delete-region (match-beginning 0) (match-end 0))))
6161       (goto-char (point-max))
6162       (insert-buffer-substring artbuf)
6163       ;; Append newline at end of the buffer as separator, and then
6164       ;; save it to file.
6165       (goto-char (point-max))
6166       (insert "\n")
6167       (when coding
6168         ;; If the coding system is not suitable to encode the text,
6169         ;; ask a user for a proper one.
6170         (when (fboundp 'select-safe-coding-system)
6171           (setq coding (coding-system-base
6172                         (save-window-excursion
6173                           (select-safe-coding-system (point-min) (point-max)
6174                                                      coding))))
6175           (setq coding-system-for-write
6176                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
6177                     coding)))
6178         (goto-char (point-min))
6179         ;; Add the coding cookie.
6180         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
6181                         coding-system-for-write)))
6182       (if exists
6183           (progn
6184             (write-region (point-min) (point-max) file-name nil 'no-message)
6185             (message "Appended to %s" file-name))
6186         (write-region (point-min) (point-max) file-name))))
6187   t)
6188
6189 (defun gnus-narrow-to-page (&optional arg)
6190   "Narrow the article buffer to a page.
6191 If given a numerical ARG, move forward ARG pages."
6192   (interactive "P")
6193   (setq arg (if arg (prefix-numeric-value arg) 0))
6194   (with-current-buffer gnus-article-buffer
6195     (widen)
6196     ;; Remove any old next/prev buttons.
6197     (when (gnus-visual-p 'page-marker)
6198       (let ((inhibit-read-only t))
6199         (gnus-remove-text-with-property 'gnus-prev)
6200         (gnus-remove-text-with-property 'gnus-next)))
6201     (let (st nd pt)
6202       (when (save-excursion
6203               (cond ((< arg 0)
6204                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6205                          (prog1
6206                              (setq nd (match-beginning 0)
6207                                    pt nd)
6208                            (when (re-search-backward page-delimiter nil t)
6209                              (setq st (match-end 0))))
6210                        (when (re-search-forward page-delimiter nil t)
6211                          (setq nd (match-beginning 0)
6212                                pt (point-min)))))
6213                     ((> arg 0)
6214                      (if (re-search-forward page-delimiter nil 'move arg)
6215                          (prog1
6216                              (setq st (match-end 0)
6217                                    pt st)
6218                            (when (re-search-forward page-delimiter nil t)
6219                              (setq nd (match-beginning 0))))
6220                        (when (re-search-backward page-delimiter nil t)
6221                          (setq st (match-end 0)
6222                                pt (point-max)))))
6223                     (t
6224                      (when (re-search-backward page-delimiter nil t)
6225                        (goto-char (setq st (match-end 0))))
6226                      (when (re-search-forward page-delimiter nil t)
6227                        (setq nd (match-beginning 0)))
6228                      (or st nd))))
6229         (setq gnus-page-broken t)
6230         (when pt (goto-char pt))
6231         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6232         (when (gnus-visual-p 'page-marker)
6233           (save-excursion
6234             (when nd
6235               (goto-char nd)
6236               (gnus-insert-next-page-button))
6237             (when st
6238               (goto-char st)
6239               (gnus-insert-prev-page-button))))))))
6240
6241 ;; Article mode commands
6242
6243 (defun gnus-article-goto-next-page ()
6244   "Show the next page of the article."
6245   (interactive)
6246   (when (gnus-article-next-page)
6247     (goto-char (point-min))
6248     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6249
6250
6251 (defun gnus-article-goto-prev-page ()
6252   "Show the previous page of the article."
6253   (interactive)
6254   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6255       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6256     (gnus-article-prev-page nil)))
6257
6258 ;; This is cleaner but currently breaks `gnus-pick-mode':
6259 ;;
6260 ;; (defun gnus-article-goto-next-page ()
6261 ;;   "Show the next page of the article."
6262 ;;   (interactive)
6263 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6264 ;;     (gnus-summary-next-page)))
6265 ;;
6266 ;; (defun gnus-article-goto-prev-page ()
6267 ;;   "Show the next page of the article."
6268 ;;   (interactive)
6269 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6270 ;;     (gnus-summary-prev-page)))
6271
6272 (defun gnus-article-next-page (&optional lines)
6273   "Show the next page of the current article.
6274 If end of article, return non-nil.  Otherwise return nil.
6275 Argument LINES specifies lines to be scrolled up."
6276   (interactive "p")
6277   (move-to-window-line (if (featurep 'xemacs) -1 (- -1 scroll-margin)))
6278   (if (and (not (and gnus-article-over-scroll
6279                      (> (count-lines (window-start) (point-max))
6280                         (if (featurep 'xemacs)
6281                             (or lines (1- (window-height)))
6282                           (+ (or lines (1- (window-height))) scroll-margin)))))
6283            (save-excursion
6284              (end-of-line)
6285              (and (pos-visible-in-window-p)     ;Not continuation line.
6286                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6287       ;; Nothing in this page.
6288       (if (or (not gnus-page-broken)
6289               (save-excursion
6290                 (save-restriction
6291                   (widen)
6292                   (forward-line)
6293                   (eobp)))) ;Real end-of-buffer?
6294           (progn
6295             (when gnus-article-over-scroll
6296               (gnus-article-next-page-1 lines))
6297             t)                  ;Nothing more.
6298         (gnus-narrow-to-page 1)         ;Go to next page.
6299         nil)
6300     ;; More in this page.
6301     (gnus-article-next-page-1 lines)
6302     nil))
6303
6304 (defun gnus-article-beginning-of-window ()
6305   "Move point to the beginning of the window.
6306 In Emacs, the point is placed at the line number which `scroll-margin'
6307 specifies."
6308   (if (featurep 'xemacs)
6309       (move-to-window-line 0)
6310     ;; There is an obscure bug in Emacs that makes it impossible to
6311     ;; scroll past big pictures in the article buffer.  Try to fix
6312     ;; this by adding a sanity check by counting the lines visible.
6313     (when (> (count-lines (window-start) (window-end)) 30)
6314       (move-to-window-line
6315        (min (max 0 scroll-margin)
6316             (max 1 (- (window-height)
6317                       (if mode-line-format 1 0)
6318                       (if header-line-format 1 0)
6319                       2)))))))
6320
6321 (defun gnus-article-next-page-1 (lines)
6322   (unless (featurep 'xemacs)
6323     ;; Protect against the bug that Emacs 21.x hangs up when scrolling up for
6324     ;; too many number of lines if `scroll-margin' is set as two or greater.
6325     (when (and (numberp lines)
6326                (> lines 0)
6327                (> scroll-margin 0))
6328       (setq lines (min lines
6329                        (max 0 (- (count-lines (window-start) (point-max))
6330                                  scroll-margin))))))
6331   (condition-case ()
6332       (let ((scroll-in-place nil))
6333         (scroll-up lines))
6334     (end-of-buffer
6335      ;; Long lines may cause an end-of-buffer error.
6336      (goto-char (point-max))))
6337   (gnus-article-beginning-of-window))
6338
6339 (defun gnus-article-prev-page (&optional lines)
6340   "Show previous page of current article.
6341 Argument LINES specifies lines to be scrolled down."
6342   (interactive "p")
6343   (move-to-window-line 0)
6344   (if (and gnus-page-broken
6345            (bobp)
6346            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6347       (progn
6348         (gnus-narrow-to-page -1)        ;Go to previous page.
6349         (goto-char (point-max))
6350         (recenter (if gnus-article-over-scroll
6351                       (if lines
6352                           (max (if (featurep 'xemacs)
6353                                    lines
6354                                  (+ lines scroll-margin))
6355                                3)
6356                         (- (window-height) 2))
6357                     -1)))
6358     (prog1
6359         (condition-case ()
6360             (let ((scroll-in-place nil))
6361               (scroll-down lines))
6362           (beginning-of-buffer
6363            (goto-char (point-min))))
6364       (gnus-article-beginning-of-window))))
6365
6366 (defun gnus-article-only-boring-p ()
6367   "Decide whether there is only boring text remaining in the article.
6368 Something \"interesting\" is a word of at least two letters that does
6369 not have a face in `gnus-article-boring-faces'."
6370   (when (and gnus-article-skip-boring
6371              (boundp 'gnus-article-boring-faces)
6372              (symbol-value 'gnus-article-boring-faces))
6373     (save-excursion
6374       (let ((inhibit-point-motion-hooks t))
6375         (catch 'only-boring
6376           (while (re-search-forward "\\b\\w\\w" nil t)
6377             (forward-char -1)
6378             (when (not (gnus-intersection
6379                         (gnus-faces-at (point))
6380                         (symbol-value 'gnus-article-boring-faces)))
6381               (throw 'only-boring nil)))
6382           (throw 'only-boring t))))))
6383
6384 (defun gnus-article-refer-article ()
6385   "Read article specified by message-id around point."
6386   (interactive)
6387   (save-excursion
6388     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6389     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6390     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6391         (let ((msg-id (concat "<" (match-string 0) ">")))
6392           (set-buffer gnus-summary-buffer)
6393           (gnus-summary-refer-article msg-id))
6394       (error "No references around point"))))
6395
6396 (defun gnus-article-show-summary ()
6397   "Reconfigure windows to show summary buffer."
6398   (interactive)
6399   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6400       (error "There is no summary buffer for this article buffer")
6401     (gnus-article-set-globals)
6402     (gnus-configure-windows 'article)
6403     (gnus-summary-goto-subject gnus-current-article)
6404     (gnus-summary-position-point)))
6405
6406 (defun gnus-article-describe-briefly ()
6407   "Describe article mode commands briefly."
6408   (interactive)
6409   (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")))
6410
6411 (defun gnus-article-check-buffer ()
6412   "Beep if not in an article buffer."
6413   (unless (equal major-mode 'gnus-article-mode)
6414     (error "Command invoked outside of a Gnus article buffer")))
6415
6416 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6417   "Read a summary buffer key sequence and execute it from the article buffer."
6418   (interactive "P")
6419   (gnus-article-check-buffer)
6420   (let ((nosaves
6421          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6422            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6423            "=" "^" "\M-^" "|"))
6424         (nosave-but-article
6425          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6426            "An" "Ap" [?A (meta return)] [?A delete]))
6427         (nosave-in-article
6428          '("AS" "\C-d"))
6429         (up-to-top
6430          '("n" "Gn" "p" "Gp"))
6431         keys new-sum-point)
6432     (with-current-buffer gnus-article-current-summary
6433       (let (gnus-pick-mode)
6434         (setq unread-command-events (nconc unread-command-events
6435                                            (list (or key last-command-event)))
6436               keys (if (featurep 'xemacs)
6437                        (events-to-keys (read-key-sequence nil t))
6438                      (read-key-sequence nil t)))))
6439
6440     (message "")
6441
6442     (cond
6443      ((eq (aref keys (1- (length keys))) ?\C-h)
6444       (gnus-article-describe-bindings (substring keys 0 -1)))
6445      ((or (member keys nosaves)
6446           (member keys nosave-but-article)
6447           (member keys nosave-in-article))
6448       (let (func)
6449         (save-window-excursion
6450           (pop-to-buffer gnus-article-current-summary)
6451           ;; We disable the pick minor mode commands.
6452           (let (gnus-pick-mode)
6453             (setq func (key-binding keys t))))
6454         (if (or (not func)
6455                 (numberp func))
6456             (ding)
6457           (unless (member keys nosave-in-article)
6458             (set-buffer gnus-article-current-summary))
6459           (call-interactively func)
6460           (setq new-sum-point (point)))
6461         (when (member keys nosave-but-article)
6462           (pop-to-buffer gnus-article-buffer))))
6463      (t
6464       ;; These commands should restore window configuration.
6465       (let ((obuf (current-buffer))
6466             (owin (current-window-configuration))
6467             win func in-buffer selected new-sum-start new-sum-hscroll err)
6468         (cond (not-restore-window
6469                (pop-to-buffer gnus-article-current-summary)
6470                (setq win (selected-window)))
6471               ((setq win (get-buffer-window gnus-article-current-summary))
6472                (select-window win))
6473               (t
6474                (let ((summary-buffer gnus-article-current-summary))
6475                  (gnus-configure-windows 'article)
6476                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6477                    (let ((gnus-buffer-configuration
6478                           '((article ((vertical 1.0
6479                                                 (summary 0.25 point)
6480                                                 (article 1.0)))))))
6481                      (gnus-configure-windows 'article))
6482                    (setq win (get-buffer-window summary-buffer 'visible)))
6483                  (gnus-select-frame-set-input-focus (window-frame win))
6484                  (select-window win))))
6485         (setq in-buffer (current-buffer))
6486         ;; We disable the pick minor mode commands.
6487         (if (and (setq func (let (gnus-pick-mode)
6488                               (key-binding keys t)))
6489                  (functionp func)
6490                  (condition-case code
6491                      (progn
6492                        (call-interactively func)
6493                        t)
6494                    (error
6495                     (setq err code)
6496                     nil)))
6497             (progn
6498               (when (eq win (selected-window))
6499                 (setq new-sum-point (point)
6500                       new-sum-start (window-start win)
6501                       new-sum-hscroll (window-hscroll win)))
6502               (when (or (eq in-buffer (current-buffer))
6503                         (when (eq obuf (current-buffer))
6504                           (set-buffer in-buffer)
6505                           t))
6506                 (setq selected (gnus-summary-select-article))
6507                 (set-buffer obuf)
6508                 (unless not-restore-window
6509                   (set-window-configuration owin))
6510                 (when (and (eq selected 'old)
6511                            new-sum-point)
6512                   (set-window-start (get-buffer-window (current-buffer))
6513                                     1)
6514                   (set-window-point (get-buffer-window (current-buffer))
6515                                     (if (article-goto-body)
6516                                         (1- (point))
6517                                       (point))))
6518                 (when (and (not not-restore-window)
6519                            new-sum-point
6520                            (window-live-p win)
6521                            (with-current-buffer (window-buffer win)
6522                              (eq major-mode 'gnus-summary-mode)))
6523                   (set-window-point win new-sum-point)
6524                   (set-window-start win new-sum-start)
6525                   (set-window-hscroll win new-sum-hscroll))))
6526           (set-window-configuration owin)
6527           (if err
6528               (signal (car err) (cdr err))
6529             (ding))))))))
6530
6531 (defun gnus-article-read-summary-send-keys ()
6532   (interactive)
6533   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6534     (gnus-article-read-summary-keys)))
6535
6536 (defun gnus-article-describe-key (key)
6537   "Display documentation of the function invoked by KEY.
6538 KEY is a string or a vector."
6539   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6540                        (read-key-sequence "Describe key: "))))
6541   (gnus-article-check-buffer)
6542   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6543                                   gnus-article-read-summary-send-keys))
6544       (with-current-buffer gnus-article-current-summary
6545         (setq unread-command-events
6546               (if (featurep 'xemacs)
6547                   (append key nil)
6548                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6549                                         (list 'meta (- x 128))
6550                                       x))
6551                         key)))
6552         (let ((cursor-in-echo-area t)
6553               gnus-pick-mode)
6554           (describe-key (read-key-sequence nil t))))
6555     (describe-key key)))
6556
6557 (defun gnus-article-describe-key-briefly (key &optional insert)
6558   "Display documentation of the function invoked by KEY.
6559 KEY is a string or a vector."
6560   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6561                        (read-key-sequence "Describe key: "))
6562                      current-prefix-arg))
6563   (gnus-article-check-buffer)
6564   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6565                                   gnus-article-read-summary-send-keys))
6566       (with-current-buffer gnus-article-current-summary
6567         (setq unread-command-events
6568               (if (featurep 'xemacs)
6569                   (append key nil)
6570                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6571                                         (list 'meta (- x 128))
6572                                       x))
6573                         key)))
6574         (let ((cursor-in-echo-area t)
6575               gnus-pick-mode)
6576           (describe-key-briefly (read-key-sequence nil t) insert)))
6577     (describe-key-briefly key insert)))
6578
6579 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6580 (defvar gnus-agent-summary-mode)
6581 (defvar gnus-draft-mode)
6582 ;; Calling help-buffer will autoload help-mode.
6583 (defvar help-xref-stack-item)
6584
6585 (defun gnus-article-describe-bindings (&optional prefix)
6586   "Show a list of all defined keys, and their definitions.
6587 The optional argument PREFIX, if non-nil, should be a key sequence;
6588 then we display only bindings that start with that prefix."
6589   (interactive)
6590   (gnus-article-check-buffer)
6591   (let ((keymap (copy-keymap gnus-article-mode-map))
6592         (map (copy-keymap gnus-article-send-map))
6593         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6594         parent agent draft)
6595     (define-key keymap "S" map)
6596     (define-key map [t] nil)
6597     (with-current-buffer gnus-article-current-summary
6598       (set-keymap-parent
6599        keymap
6600        (if (setq parent (keymap-parent gnus-article-mode-map))
6601            (prog1
6602                (setq parent (copy-keymap parent))
6603              (set-keymap-parent parent (current-local-map)))
6604          (current-local-map)))
6605       (set-keymap-parent map (key-binding "S"))
6606       (let (key def gnus-pick-mode)
6607         (while sumkeys
6608           (setq key (pop sumkeys))
6609           (cond ((and (vectorp key) (= (length key) 1)
6610                       (consp (setq def (aref key 0)))
6611                       (numberp (car def)) (numberp (cdr def)))
6612                  (when (< (max (car def) (cdr def)) 128)
6613                    (setq sumkeys
6614                          (append (mapcar
6615                                   #'vector
6616                                   (nreverse (gnus-uncompress-range def)))
6617                                  sumkeys))))
6618                 ((setq def (key-binding key))
6619                  (unless (eq def 'undefined)
6620                    (define-key keymap key def))))))
6621       (when (boundp 'gnus-agent-summary-mode)
6622         (setq agent gnus-agent-summary-mode))
6623       (when (boundp 'gnus-draft-mode)
6624         (setq draft gnus-draft-mode)))
6625     (with-temp-buffer
6626       (use-local-map keymap)
6627       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6628       (set (make-local-variable 'gnus-draft-mode) draft)
6629       (describe-bindings prefix))
6630     (let ((item `((lambda (prefix)
6631                     (with-current-buffer ,(current-buffer)
6632                       (gnus-article-describe-bindings prefix)))
6633                   ,prefix)))
6634       (with-current-buffer (if (fboundp 'help-buffer)
6635                                (let (help-xref-following) (help-buffer))
6636                              "*Help*") ;; Emacs 21
6637         (setq help-xref-stack-item item)))))
6638
6639 (defun gnus-article-reply-with-original (&optional wide)
6640   "Start composing a reply mail to the current message.
6641 The text in the region will be yanked.  If the region isn't active,
6642 the entire article will be yanked."
6643   (interactive)
6644   (let ((article (cdr gnus-article-current))
6645         contents)
6646     (if (not (gnus-region-active-p))
6647         (with-current-buffer gnus-summary-buffer
6648           (gnus-summary-reply (list (list article)) wide))
6649       (setq contents (buffer-substring (point) (mark t)))
6650       ;; Deactivate active regions.
6651       (when (and (boundp 'transient-mark-mode)
6652                  transient-mark-mode)
6653         (setq mark-active nil))
6654       (with-current-buffer gnus-summary-buffer
6655         (gnus-summary-reply
6656          (list (list article contents)) wide)))))
6657
6658 (defun gnus-article-wide-reply-with-original ()
6659   "Start composing a wide 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   (gnus-article-reply-with-original t))
6664
6665 (defun gnus-article-followup-with-original ()
6666   "Compose a followup to the current article.
6667 The text in the region will be yanked.  If the region isn't active,
6668 the entire article will be yanked."
6669   (interactive)
6670   (let ((article (cdr gnus-article-current))
6671         contents)
6672       (if (not (gnus-region-active-p))
6673           (with-current-buffer gnus-summary-buffer
6674             (gnus-summary-followup (list (list article))))
6675         (setq contents (buffer-substring (point) (mark t)))
6676         ;; Deactivate active regions.
6677         (when (and (boundp 'transient-mark-mode)
6678                    transient-mark-mode)
6679           (setq mark-active nil))
6680         (with-current-buffer gnus-summary-buffer
6681           (gnus-summary-followup
6682            (list (list article contents)))))))
6683
6684 (defun gnus-article-hide (&optional arg force)
6685   "Hide all the gruft in the current article.
6686 This means that signatures, cited text and (some) headers will be
6687 hidden.
6688 If given a prefix, show the hidden text instead."
6689   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6690   (gnus-article-hide-headers arg)
6691   (gnus-article-hide-list-identifiers arg)
6692   (gnus-article-hide-citation-maybe arg force)
6693   (gnus-article-hide-signature arg))
6694
6695 (defun gnus-article-maybe-highlight ()
6696   "Do some article highlighting if article highlighting is requested."
6697   (when (gnus-visual-p 'article-highlight 'highlight)
6698     (gnus-article-highlight-some)))
6699
6700 (defun gnus-check-group-server ()
6701   ;; Make sure the connection to the server is alive.
6702   (unless (gnus-server-opened
6703            (gnus-find-method-for-group gnus-newsgroup-name))
6704     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6705     (gnus-request-group gnus-newsgroup-name t)))
6706
6707 (eval-when-compile
6708   (autoload 'nneething-get-file-name "nneething"))
6709
6710 (defun gnus-request-article-this-buffer (article group)
6711   "Get an article and insert it into this buffer."
6712   (let (do-update-line sparse-header)
6713     (prog1
6714         (save-excursion
6715           (erase-buffer)
6716           (gnus-kill-all-overlays)
6717           (setq group (or group gnus-newsgroup-name))
6718
6719           ;; Using `gnus-request-article' directly will insert the article into
6720           ;; `nntp-server-buffer' - so we'll save some time by not having to
6721           ;; copy it from the server buffer into the article buffer.
6722
6723           ;; We only request an article by message-id when we do not have the
6724           ;; headers for it, so we'll have to get those.
6725           (when (stringp article)
6726             (gnus-read-header article))
6727
6728           ;; If the article number is negative, that means that this article
6729           ;; doesn't belong in this newsgroup (possibly), so we find its
6730           ;; message-id and request it by id instead of number.
6731           (when (and (numberp article)
6732                      gnus-summary-buffer
6733                      (get-buffer gnus-summary-buffer)
6734                      (gnus-buffer-exists-p gnus-summary-buffer))
6735             (with-current-buffer gnus-summary-buffer
6736               (let ((header (gnus-summary-article-header article)))
6737                 (when (< article 0)
6738                   (cond
6739                    ((memq article gnus-newsgroup-sparse)
6740                     ;; This is a sparse gap article.
6741                     (setq do-update-line article)
6742                     (setq article (mail-header-id header))
6743                     (setq sparse-header (gnus-read-header article))
6744                     (setq gnus-newsgroup-sparse
6745                           (delq article gnus-newsgroup-sparse)))
6746                    ((vectorp header)
6747                     ;; It's a real article.
6748                     (setq article (mail-header-id header)))
6749                    (t
6750                     ;; It is an extracted pseudo-article.
6751                     (setq article 'pseudo)
6752                     (gnus-request-pseudo-article header))))
6753
6754                 (let ((method (gnus-find-method-for-group
6755                                gnus-newsgroup-name)))
6756                   (when (and (eq (car method) 'nneething)
6757                              (vectorp header))
6758                     (let ((dir (nneething-get-file-name
6759                                 (mail-header-id header))))
6760                       (when (and (stringp dir)
6761                                  (file-directory-p dir))
6762                         (setq article 'nneething)
6763                         (gnus-group-enter-directory dir))))))))
6764
6765           (cond
6766            ;; Refuse to select canceled articles.
6767            ((and (numberp article)
6768                  gnus-summary-buffer
6769                  (get-buffer gnus-summary-buffer)
6770                  (gnus-buffer-exists-p gnus-summary-buffer)
6771                  (eq (cdr (with-current-buffer gnus-summary-buffer
6772                             (assq article gnus-newsgroup-reads)))
6773                      gnus-canceled-mark))
6774             nil)
6775            ;; We first check `gnus-original-article-buffer'.
6776            ((and (get-buffer gnus-original-article-buffer)
6777                  (numberp article)
6778                  (with-current-buffer gnus-original-article-buffer
6779                    (and (equal (car gnus-original-article) group)
6780                         (eq (cdr gnus-original-article) article))))
6781             ;; `insert-buffer-substring' would incorrectly use the
6782             ;; equivalent of string-make-multibyte which amount to decoding
6783             ;; with locale-coding-system, causing failure of
6784             ;; subsequent decoding.
6785             (insert (mm-string-to-multibyte
6786                      (with-current-buffer gnus-original-article-buffer
6787                        (buffer-substring (point-min) (point-max)))))
6788             'article)
6789            ;; Check the backlog.
6790            ((and gnus-keep-backlog
6791                  (gnus-backlog-request-article group article (current-buffer)))
6792             'article)
6793            ;; Check asynchronous pre-fetch.
6794            ((gnus-async-request-fetched-article group article (current-buffer))
6795             (gnus-async-prefetch-next group article gnus-summary-buffer)
6796             (when (and (numberp article) gnus-keep-backlog)
6797               (gnus-backlog-enter-article group article (current-buffer)))
6798             'article)
6799            ;; Check the cache.
6800            ((and gnus-use-cache
6801                  (numberp article)
6802                  (gnus-cache-request-article article group))
6803             'article)
6804            ;; Check the agent cache.
6805            ((gnus-agent-request-article article group)
6806             'article)
6807            ;; Get the article and put into the article buffer.
6808            ((or (stringp article)
6809                 (numberp article))
6810             (let ((gnus-override-method gnus-override-method)
6811                   (methods (and (stringp article)
6812                                 gnus-refer-article-method))
6813                   (backend (car (gnus-find-method-for-group
6814                                  gnus-newsgroup-name)))
6815                   result
6816                   (inhibit-read-only t))
6817               (if (or (not (listp methods))
6818                       (and (symbolp (car methods))
6819                            (assq (car methods) nnoo-definition-alist)))
6820                   (setq methods (list methods)))
6821               (when (and (null gnus-override-method)
6822                          methods)
6823                 (setq gnus-override-method (pop methods)))
6824               (while (not result)
6825                 (when (eq gnus-override-method 'current)
6826                   (setq gnus-override-method
6827                         (with-current-buffer gnus-summary-buffer
6828                           gnus-current-select-method)))
6829                 (erase-buffer)
6830                 (gnus-kill-all-overlays)
6831                 (let ((gnus-newsgroup-name group))
6832                   (gnus-check-group-server))
6833                 (cond
6834                  ((gnus-request-article article group (current-buffer))
6835                   (when (numberp article)
6836                     (gnus-async-prefetch-next group article
6837                                               gnus-summary-buffer)
6838                     (when gnus-keep-backlog
6839                       (gnus-backlog-enter-article
6840                        group article (current-buffer))))
6841                   (setq result 'article))
6842                  (methods
6843                   (setq gnus-override-method (pop methods)))
6844                  ((not (string-match "^400 "
6845                                      (nnheader-get-report backend)))
6846                   ;; If we get 400 server disconnect, reconnect and
6847                   ;; retry; otherwise, assume the article has expired.
6848                   (setq result 'done))))
6849               (and (eq result 'article) 'article)))
6850            ;; It was a pseudo.
6851            (t article)))
6852
6853       ;; Associate this article with the current summary buffer.
6854       (setq gnus-article-current-summary gnus-summary-buffer)
6855
6856       ;; Take the article from the original article buffer
6857       ;; and place it in the buffer it's supposed to be in.
6858       (when (and (get-buffer gnus-article-buffer)
6859                  (equal (buffer-name (current-buffer))
6860                         (buffer-name (get-buffer gnus-article-buffer))))
6861         (save-excursion
6862           (if (get-buffer gnus-original-article-buffer)
6863               (set-buffer gnus-original-article-buffer)
6864             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6865             (buffer-disable-undo)
6866             (setq major-mode 'gnus-original-article-mode)
6867             (setq buffer-read-only t))
6868           (let ((inhibit-read-only t))
6869             (erase-buffer)
6870             (insert-buffer-substring gnus-article-buffer))
6871           (setq gnus-original-article (cons group article)))
6872
6873         ;; Decode charsets.
6874         (run-hooks 'gnus-article-decode-hook)
6875         ;; Mark article as decoded or not.
6876         (setq gnus-article-decoded-p gnus-article-decode-hook))
6877
6878       ;; Update sparse articles.
6879       (when (and do-update-line
6880                  (or (numberp article)
6881                      (stringp article)))
6882         (let ((buf (current-buffer)))
6883           (set-buffer gnus-summary-buffer)
6884           (gnus-summary-update-article do-update-line sparse-header)
6885           (gnus-summary-goto-subject do-update-line nil t)
6886           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6887                             (point))
6888           (set-buffer buf))))))
6889
6890 ;;;
6891 ;;; Article editing
6892 ;;;
6893
6894 (defcustom gnus-article-edit-mode-hook nil
6895   "Hook run in article edit mode buffers."
6896   :group 'gnus-article-various
6897   :type 'hook)
6898
6899 (defvar gnus-article-edit-done-function nil)
6900
6901 (defvar gnus-article-edit-mode-map nil)
6902
6903 ;; Should we be using derived.el for this?
6904 (unless gnus-article-edit-mode-map
6905   (setq gnus-article-edit-mode-map (make-keymap))
6906   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6907
6908   (gnus-define-keys gnus-article-edit-mode-map
6909     "\C-c?"    describe-mode
6910     "\C-c\C-c" gnus-article-edit-done
6911     "\C-c\C-k" gnus-article-edit-exit
6912     "\C-c\C-f\C-t" message-goto-to
6913     "\C-c\C-f\C-o" message-goto-from
6914     "\C-c\C-f\C-b" message-goto-bcc
6915     ;;"\C-c\C-f\C-w" message-goto-fcc
6916     "\C-c\C-f\C-c" message-goto-cc
6917     "\C-c\C-f\C-s" message-goto-subject
6918     "\C-c\C-f\C-r" message-goto-reply-to
6919     "\C-c\C-f\C-n" message-goto-newsgroups
6920     "\C-c\C-f\C-d" message-goto-distribution
6921     "\C-c\C-f\C-f" message-goto-followup-to
6922     "\C-c\C-f\C-m" message-goto-mail-followup-to
6923     "\C-c\C-f\C-k" message-goto-keywords
6924     "\C-c\C-f\C-u" message-goto-summary
6925     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6926     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6927     "\C-c\C-b" message-goto-body
6928     "\C-c\C-i" message-goto-signature
6929
6930     "\C-c\C-t" message-insert-to
6931     "\C-c\C-n" message-insert-newsgroups
6932     "\C-c\C-o" message-sort-headers
6933     "\C-c\C-e" message-elide-region
6934     "\C-c\C-v" message-delete-not-region
6935     "\C-c\C-z" message-kill-to-signature
6936     "\M-\r" message-newline-and-reformat
6937     "\C-c\C-a" mml-attach-file
6938     "\C-a" message-beginning-of-line
6939     "\t" message-tab
6940     "\M-;" comment-region)
6941
6942   (gnus-define-keys (gnus-article-edit-wash-map
6943                      "\C-c\C-w" gnus-article-edit-mode-map)
6944     "f" gnus-article-edit-full-stops))
6945
6946 (easy-menu-define
6947   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6948   '("Field"
6949     ["Fetch To" message-insert-to t]
6950     ["Fetch Newsgroups" message-insert-newsgroups t]
6951     "----"
6952     ["To" message-goto-to t]
6953     ["From" message-goto-from t]
6954     ["Subject" message-goto-subject t]
6955     ["Cc" message-goto-cc t]
6956     ["Reply-To" message-goto-reply-to t]
6957     ["Summary" message-goto-summary t]
6958     ["Keywords" message-goto-keywords t]
6959     ["Newsgroups" message-goto-newsgroups t]
6960     ["Followup-To" message-goto-followup-to t]
6961     ["Mail-Followup-To" message-goto-mail-followup-to t]
6962     ["Distribution" message-goto-distribution t]
6963     ["Body" message-goto-body t]
6964     ["Signature" message-goto-signature t]))
6965
6966 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6967   "Major mode for editing articles.
6968 This is an extended text-mode.
6969
6970 \\{gnus-article-edit-mode-map}"
6971   (make-local-variable 'gnus-article-edit-done-function)
6972   (make-local-variable 'gnus-prev-winconf)
6973   (set (make-local-variable 'font-lock-defaults)
6974        '(message-font-lock-keywords t))
6975   (set (make-local-variable 'mail-header-separator) "")
6976   (set (make-local-variable 'gnus-article-edit-mode) t)
6977   (easy-menu-add message-mode-field-menu message-mode-map)
6978   (mml-mode)
6979   (setq buffer-read-only nil)
6980   (buffer-enable-undo)
6981   (widen))
6982
6983 (defun gnus-article-edit (&optional force)
6984   "Edit the current article.
6985 This will have permanent effect only in mail groups.
6986 If FORCE is non-nil, allow editing of articles even in read-only
6987 groups."
6988   (interactive "P")
6989   (when (and (not force)
6990              (gnus-group-read-only-p))
6991     (error "The current newsgroup does not support article editing"))
6992   (gnus-article-date-original)
6993   (gnus-article-edit-article
6994    'ignore
6995    `(lambda (no-highlight)
6996       'ignore
6997       (gnus-summary-edit-article-done
6998        ,(or (mail-header-references gnus-current-headers) "")
6999        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
7000
7001 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
7002   "Start editing the contents of the current article buffer."
7003   (let ((winconf (current-window-configuration)))
7004     (set-buffer gnus-article-buffer)
7005     (let ((message-auto-save-directory
7006            ;; Don't associate the article buffer with a draft file.
7007            nil))
7008       (gnus-article-edit-mode))
7009     (funcall start-func)
7010     (set-buffer-modified-p nil)
7011     (gnus-configure-windows 'edit-article)
7012     (setq gnus-article-edit-done-function exit-func)
7013     (setq gnus-prev-winconf winconf)
7014     (unless quiet
7015       (gnus-message 6 "C-c C-c to end edits"))))
7016
7017 (defun gnus-article-edit-done (&optional arg)
7018   "Update the article edits and exit."
7019   (interactive "P")
7020   (let ((func gnus-article-edit-done-function)
7021         (buf (current-buffer))
7022         (start (window-start))
7023         (p (point))
7024         (winconf gnus-prev-winconf))
7025     (widen) ;; Widen it in case that users narrowed the buffer.
7026     (funcall func arg)
7027     (set-buffer buf)
7028     ;; The cache and backlog have to be flushed somewhat.
7029     (when gnus-keep-backlog
7030       (gnus-backlog-remove-article
7031        (car gnus-article-current) (cdr gnus-article-current)))
7032     ;; Flush original article as well.
7033     (when (get-buffer gnus-original-article-buffer)
7034       (with-current-buffer gnus-original-article-buffer
7035         (setq gnus-original-article nil)))
7036     (when gnus-use-cache
7037       (gnus-cache-update-article
7038        (car gnus-article-current) (cdr gnus-article-current)))
7039     ;; We remove all text props from the article buffer.
7040     (kill-all-local-variables)
7041     (set-text-properties (point-min) (point-max) nil)
7042     (gnus-article-mode)
7043     (set-window-configuration winconf)
7044     (set-buffer buf)
7045     (set-window-start (get-buffer-window buf) start)
7046     (set-window-point (get-buffer-window buf) (point)))
7047   (gnus-summary-show-article))
7048
7049 (defun gnus-article-edit-exit ()
7050   "Exit the article editing without updating."
7051   (interactive)
7052   (when (or (not (buffer-modified-p))
7053             (yes-or-no-p "Article modified; kill anyway? "))
7054     (let ((curbuf (current-buffer))
7055           (p (point))
7056           (window-start (window-start)))
7057       (erase-buffer)
7058       (if (gnus-buffer-live-p gnus-original-article-buffer)
7059           (insert-buffer-substring gnus-original-article-buffer))
7060       (let ((winconf gnus-prev-winconf))
7061         (kill-all-local-variables)
7062         (gnus-article-mode)
7063         (set-window-configuration winconf)
7064         ;; Tippy-toe some to make sure that point remains where it was.
7065         (save-current-buffer
7066           (set-buffer curbuf)
7067           (set-window-start (get-buffer-window (current-buffer)) window-start)
7068           (goto-char p))))
7069     (gnus-summary-show-article)))
7070
7071 (defun gnus-article-edit-full-stops ()
7072   "Interactively repair spacing at end of sentences."
7073   (interactive)
7074   (save-excursion
7075     (goto-char (point-min))
7076     (search-forward-regexp "^$" nil t)
7077     (let ((case-fold-search nil))
7078       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
7079
7080 ;;;
7081 ;;; Article highlights
7082 ;;;
7083
7084 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
7085
7086 ;;; Internal Variables:
7087
7088 (defcustom gnus-button-url-regexp
7089   (concat
7090    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
7091    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
7092    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
7093    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
7094        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
7095              (punct "!?:;.,"))
7096          (concat
7097           "\\(?:"
7098           ;; Match paired parentheses, e.g. in Wikipedia URLs:
7099           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
7100           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
7101           "\\|"
7102           "[" chars punct     "]+" "[" chars "]"
7103           "\\)"))
7104      (concat ;; XEmacs 21.4 doesn't support POSIX.
7105       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
7106       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
7107    "\\)")
7108   "Regular expression that matches URLs."
7109   :group 'gnus-article-buttons
7110   :type 'regexp)
7111
7112 (defcustom gnus-button-valid-fqdn-regexp
7113   message-valid-fqdn-regexp
7114   "Regular expression that matches a valid FQDN."
7115   :version "22.1"
7116   :group 'gnus-article-buttons
7117   :type 'regexp)
7118
7119 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
7120 (defcustom gnus-button-valid-localpart-regexp
7121   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
7122   "Regular expression that matches a localpart of mail addresses or MIDs."
7123   :version "22.1"
7124   :group 'gnus-article-buttons
7125   :type 'regexp)
7126
7127 (defcustom gnus-button-man-handler 'manual-entry
7128   "Function to use for displaying man pages.
7129 The function must take at least one argument with a string naming the
7130 man page."
7131   :version "22.1"
7132   :type '(choice (function-item :tag "Man" manual-entry)
7133                  (function-item :tag "Woman" woman)
7134                  (function :tag "Other"))
7135   :group 'gnus-article-buttons)
7136
7137 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
7138   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
7139 If the default site is too slow, try to find a CTAN mirror, see
7140 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
7141 the variable `gnus-button-handle-ctan'."
7142   :version "22.1"
7143   :group 'gnus-article-buttons
7144   :link '(custom-manual "(gnus)Group Parameters")
7145   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
7146                  (const "http://tug.ctan.org/tex-archive/")
7147                  (const "http://www.dante.de/CTAN/")
7148                  (string :tag "Other")))
7149
7150 (defcustom gnus-button-ctan-handler 'browse-url
7151   "Function to use for displaying CTAN links.
7152 The function must take one argument, the string naming the URL."
7153   :version "22.1"
7154   :type '(choice (function-item :tag "Browse Url" browse-url)
7155                  (function :tag "Other"))
7156   :group 'gnus-article-buttons)
7157
7158 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
7159   "Bogus strings removed from CTAN URLs."
7160   :version "22.1"
7161   :group 'gnus-article-buttons
7162   :type '(choice (const "^/?tex-archive/\\|/")
7163                  (regexp :tag "Other")))
7164
7165 (defcustom gnus-button-ctan-directory-regexp
7166   (regexp-opt
7167    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
7168          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
7169          "languages" "macros" "nonfree" "obsolete" "support" "systems"
7170          "tds" "tools" "usergrps" "web") t)
7171   "Regular expression for ctan directories.
7172 It should match all directories in the top level of `gnus-ctan-url'."
7173   :version "22.1"
7174   :group 'gnus-article-buttons
7175   :type 'regexp)
7176
7177 (defcustom gnus-button-mid-or-mail-regexp
7178   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
7179           gnus-button-valid-fqdn-regexp
7180           ">?\\)\\b")
7181   "Regular expression that matches a message ID or a mail address."
7182   :version "22.1"
7183   :group 'gnus-article-buttons
7184   :type 'regexp)
7185
7186 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
7187   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
7188 Strings like this can be either a message ID or a mail address.  If it is one
7189 of the symbols `mid' or `mail', Gnus will always assume that the string is a
7190 message ID or a mail address, respectively.  If this variable is set to the
7191 symbol `ask', always query the user what do do.  If it is a function, this
7192 function will be called with the string as its only argument.  The function
7193 must return `mid', `mail', `invalid' or `ask'."
7194   :version "22.1"
7195   :group 'gnus-article-buttons
7196   :type '(choice (function-item :tag "Heuristic function"
7197                                 gnus-button-mid-or-mail-heuristic)
7198                  (const ask)
7199                  (const mid)
7200                  (const mail)))
7201
7202 (defcustom gnus-button-mid-or-mail-heuristic-alist
7203   '((-10.0 . ".+\\$.+@")
7204     (-10.0 . "#")
7205     (-10.0 . "\\*")
7206     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7207     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7208     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7209     (-1.0  . "^[^a-z]+@")
7210     ;;
7211     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7212     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7213     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7214     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7215     ;;
7216     (-2.0  . "^[0-9]")
7217     (-1.0  . "^[0-9][0-9]")
7218     ;;
7219     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7220     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7221     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7222     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7223     ;;
7224     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7225     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7226     ;;       "[0-9]{8,}.*\@"
7227     (-3.0
7228      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7229     ;; "[0-9]{12,}.*\@"
7230     ;; compensation for TDMA dated mail addresses:
7231     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7232     ;;
7233     (-20.0 . "\\.fsf@") ;; Gnus
7234     (-20.0 . "^slrn")
7235     (-20.0 . "^Pine")
7236     (-20.0 . "^alpine\\.")
7237     (-20.0 . "_-_") ;; Subject change in thread
7238     ;;
7239     (-20.0 . "\\.ln@") ;; leafnode
7240     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7241     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7242     ;;
7243     ;; (5.0 . "") ;; $local_part_len <= 7
7244     (10.0  . "^[^0-9]+@")
7245     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7246     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7247     (3.0   . "\@stud")
7248     ;;
7249     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7250     ;;
7251     (0.5   . "^[A-Z][a-z]")
7252     (0.5   . "^[A-Z][a-z][a-z]")
7253     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7254     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7255   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7256
7257 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7258 address.  The REGEXP is processed with `case-fold-search' set to nil."
7259   :version "22.1"
7260   :group 'gnus-article-buttons
7261   :type '(repeat (cons (number :tag "Rate")
7262                        (regexp :tag "Regexp"))))
7263
7264 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7265   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7266 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7267 address, `ask' if unsure and `invalid' if the string is invalid."
7268   (let ((case-fold-search nil)
7269         (list gnus-button-mid-or-mail-heuristic-alist)
7270         (result 0) rate regexp lpartlen elem)
7271     (setq lpartlen
7272           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7273     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7274     ;; Certain special cases...
7275     (when (string-match
7276            (concat
7277             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7278             "^[0-9]+\\.[0-9]+@compuserve\\|"
7279             "@public\\.gmane\\.org")
7280            mid-or-mail)
7281       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7282       (setq result 'mail))
7283     (when (string-match "@.*@\\| " mid-or-mail)
7284       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7285       (setq result 'invalid))
7286     ;; Nothing more to do, if result is not a number here...
7287     (when (numberp result)
7288       (while list
7289         (setq elem (car list)
7290               rate (car elem)
7291               regexp (cdr elem)
7292               list (cdr list))
7293         (when (string-match regexp mid-or-mail)
7294           (setq result (+ result rate))
7295           (gnus-message
7296            9 "`%s' matched `%s', rate `%s', result `%s'."
7297            mid-or-mail regexp rate result)))
7298       (when (<= lpartlen 7)
7299         (setq result (+ result 5.0))
7300         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7301                       mid-or-mail result))
7302       (when (>= lpartlen 12)
7303         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7304         (cond
7305          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7306           ;; Long local part should contain realname if e-mail address,
7307           ;; too many digits: message-id.
7308           ;; $score -= 5.0 + 0.1 * $local_part_len;
7309           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7310           (setq result (+ result rate))
7311           (gnus-message
7312            9 "Many digits in `%s', rate `%s', result `%s'."
7313            mid-or-mail rate result))
7314          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7315                         mid-or-mail)
7316           ;; Too few vowels [^aeiouy]{4,}.*\@
7317           (setq result (+ result -5.0))
7318           (gnus-message
7319            9 "Few vowels in `%s', rate `%s', result `%s'."
7320            mid-or-mail -5.0 result))
7321          (t
7322           (setq result (+ result 5.0))
7323           (gnus-message
7324            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7325     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7326     ;; Maybe we should make this a customizable alist: (condition . 'result)
7327     (cond
7328      ((symbolp result) result)
7329      ;; Now convert number into proper results:
7330      ((< result -10.0) 'mid)
7331      ((> result  10.0) 'mail)
7332      (t 'ask))))
7333
7334 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7335   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7336          (url-mid (concat "news" ":" mid-or-mail))
7337          (url-mailto (concat "mailto" ":" mid-or-mail)))
7338     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7339     (when (fboundp pref)
7340       (setq guessed
7341             ;; get rid of surrounding angles...
7342             (funcall pref
7343                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7344       (if (or (eq 'mid guessed) (eq 'mail guessed))
7345           (setq pref guessed)
7346         (setq pref 'ask)))
7347     (if (eq pref 'ask)
7348         (save-window-excursion
7349           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7350               (setq pref 'mail)
7351             (setq pref 'mid))))
7352     (cond ((eq pref 'mid)
7353            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7354            (gnus-button-handle-news url-mid))
7355           ((eq pref 'mail)
7356            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7357            (gnus-url-mailto url-mailto))
7358           (t (gnus-message 3 "Invalid string.")))))
7359
7360 (defun gnus-button-handle-custom (fun arg)
7361   "Call function FUN on argument ARG.
7362 Both FUN and ARG are supposed to be strings.  ARG will be passed
7363 as a symbol to FUN."
7364   (funcall (intern fun)
7365            (if (string-match "^customize-apropos" fun)
7366                arg
7367              (intern arg))))
7368
7369 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7370
7371 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7372 ;; stuff?
7373
7374 (defun gnus-button-handle-describe-function (url)
7375   "Call `describe-function' when pushing the corresponding URL button."
7376   (describe-function
7377    (intern
7378     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7379
7380 (defun gnus-button-handle-describe-variable (url)
7381   "Call `describe-variable' when pushing the corresponding URL button."
7382   (describe-variable
7383    (intern
7384     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7385
7386 (defun gnus-button-handle-symbol (url)
7387 "Display help on variable or function.
7388 Calls `describe-variable' or `describe-function'."
7389   (let ((sym (intern url)))
7390     (cond
7391      ((fboundp sym) (describe-function sym))
7392      ((boundp sym) (describe-variable sym))
7393      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7394
7395 (defun gnus-button-handle-describe-key (url)
7396   "Call `describe-key' when pushing the corresponding URL button."
7397   (let* ((key-string
7398           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7399          (keys (ignore-errors (eval `(kbd ,key-string)))))
7400     (if keys
7401         (describe-key keys)
7402       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7403
7404 (defun gnus-button-handle-apropos (url)
7405   "Call `apropos' when pushing the corresponding URL button."
7406   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7407
7408 (defun gnus-button-handle-apropos-command (url)
7409   "Call `apropos' when pushing the corresponding URL button."
7410   (apropos-command
7411    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7412
7413 (defun gnus-button-handle-apropos-variable (url)
7414   "Call `apropos' when pushing the corresponding URL button."
7415   (funcall
7416    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7417    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7418
7419 (defun gnus-button-handle-apropos-documentation (url)
7420   "Call `apropos' when pushing the corresponding URL button."
7421   (funcall
7422    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7423    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7424
7425 (defun gnus-button-handle-library (url)
7426   "Call `locate-library' when pushing the corresponding URL button."
7427   (gnus-message 9 "url=`%s'" url)
7428   (let* ((lib (locate-library url))
7429          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7430     (if (not lib)
7431         (gnus-message 1 "Cannot locale library `%s'." url)
7432       (find-file-read-only file))))
7433
7434 (defun gnus-button-handle-ctan (url)
7435   "Call `browse-url' when pushing a CTAN URL button."
7436   (funcall
7437    gnus-button-ctan-handler
7438    (concat
7439     gnus-ctan-url
7440     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
7441
7442 (defcustom gnus-button-tex-level 5
7443   "*Integer that says how many TeX-related buttons Gnus will show.
7444 The higher the number, the more buttons will appear and the more false
7445 positives are possible.  Note that you can set this variable local to
7446 specific groups.  Setting it higher in TeX groups is probably a good idea.
7447 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7448 how to set variables in specific groups."
7449   :version "22.1"
7450   :group 'gnus-article-buttons
7451   :link '(custom-manual "(gnus)Group Parameters")
7452   :type 'integer)
7453
7454 (defcustom gnus-button-man-level 5
7455   "*Integer that says how many man-related buttons Gnus will show.
7456 The higher the number, the more buttons will appear and the more false
7457 positives are possible.  Note that you can set this variable local to
7458 specific groups.  Setting it higher in Unix groups is probably a good idea.
7459 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7460 how to set variables in specific groups."
7461   :version "22.1"
7462   :group 'gnus-article-buttons
7463   :link '(custom-manual "(gnus)Group Parameters")
7464   :type 'integer)
7465
7466 (defcustom gnus-button-emacs-level 5
7467   "*Integer that says how many emacs-related buttons Gnus will show.
7468 The higher the number, the more buttons will appear and the more false
7469 positives are possible.  Note that you can set this variable local to
7470 specific groups.  Setting it higher in Emacs or Gnus related groups is
7471 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7472 `gnus-parameters' on how to set variables in specific groups."
7473   :version "22.1"
7474   :group 'gnus-article-buttons
7475   :link '(custom-manual "(gnus)Group Parameters")
7476   :type 'integer)
7477
7478 (defcustom gnus-button-message-level 5
7479   "*Integer that says how many buttons for news or mail messages will appear.
7480 The higher the number, the more buttons will appear and the more false
7481 positives are possible."
7482   ;; mail addresses, MIDs, URLs for news, ...
7483   :version "22.1"
7484   :group 'gnus-article-buttons
7485   :type 'integer)
7486
7487 (defcustom gnus-button-browse-level 5
7488   "*Integer that says how many buttons for browsing will appear.
7489 The higher the number, the more buttons will appear and the more false
7490 positives are possible."
7491   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7492   :version "22.1"
7493   :group 'gnus-article-buttons
7494   :type 'integer)
7495
7496 (defcustom gnus-button-alist
7497   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7498      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7499     ((concat "\\b\\(nntp\\|news\\):\\("
7500              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7501      0 t gnus-button-handle-news 2)
7502     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7503      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7504     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7505      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7506     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7507     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7508      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7509     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7510      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7511     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7512      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7513     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7514      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7515     ;; RFC 2368 (The mailto URL scheme)
7516     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7517      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7518     ("\\bmailto:\\([^ \n\t]+\\)"
7519      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7520     ;; CTAN
7521     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
7522              gnus-button-ctan-directory-regexp
7523              "[^][>)!;:,'\n\t ]+\\)")
7524      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
7525     ((concat "\\btex-archive/\\("
7526              gnus-button-ctan-directory-regexp
7527              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
7528      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
7529     ((concat
7530       "\\b\\("
7531       gnus-button-ctan-directory-regexp
7532       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
7533      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
7534     ;; Info Konqueror style <info:/foo/bar baz>.
7535     ;; Must come before " Gnus home-grown style".
7536     ("\\binfo://?\\([^'\">\n\t]+\\)"
7537      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7538    ;; Info, Gnus home-grown style (deprecated) <info://foo/bar+baz>
7539     ("\\binfo://\\([^'\">\n\t ]+\\)"
7540      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7541     ;; Info GNOME style <info:foo#bar_baz>
7542     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7543      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7544     ;; Info KDE style <info:(foo)bar baz>
7545     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7546      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7547     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7548      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7549     ("\\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,]*\\)\\)?"
7550      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7551      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7552     ;; This is custom
7553     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7554      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7555     ;; Emacs help commands
7556     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7557      ;; regexp doesn't match arguments containing ` '.
7558      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7559     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7560      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7561     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7562      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7563     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7564      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7565     ;; The following entries may lead to many false positives so don't enable
7566     ;; them by default (use a high button level).
7567     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7568      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7569      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7570     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7571      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7572     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7573      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7574     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7575      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7576     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7577      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7578     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7579      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7580     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7581      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7582     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7583      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7584     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7585      ;; Unlike the other regexps we really have to require quoting
7586      ;; here to determine where it ends.
7587      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7588     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7589     ("<URL: *\\([^\n<>]*\\)>"
7590      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7591     ;; RFC 2396 (2.4.3., delims) ...
7592     ("\"URL: *\\([^\n\"]*\\)\""
7593      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7594     ;; Raw URLs.
7595     (gnus-button-url-regexp
7596      0 (>= gnus-button-browse-level 0) browse-url 0)
7597     ;; man pages
7598     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7599      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7600      gnus-button-handle-man 1)
7601     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7602     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7603      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7604      gnus-button-handle-man 1)
7605     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7606     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7607     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7608      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7609     ;; Recognizing patches to .el files.  This is somewhat obscure,
7610     ;; but considering the percentage of Gnus users who hack Emacs
7611     ;; Lisp files...
7612     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7613      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7614     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7615      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7616     ;; MID or mail: To avoid too many false positives we don't try to catch
7617     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7618     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7619     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7620     ;; so that non-ambiguous entries (see above) match first.
7621     (gnus-button-mid-or-mail-regexp
7622      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7623   "*Alist of regexps matching buttons in article bodies.
7624
7625 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7626 REGEXP: is the string (case insensitive) matching text around the button (can
7627 also be Lisp expression evaluating to a string),
7628 BUTTON: is the number of the regexp grouping actually matching the button,
7629 FORM: is a Lisp expression which must eval to true for the button to
7630 be added,
7631 CALLBACK: is the function to call when the user push this button, and each
7632 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7633
7634 CALLBACK can also be a variable, in that case the value of that
7635 variable it the real callback function."
7636   :group 'gnus-article-buttons
7637   :type '(repeat (list (choice regexp variable sexp)
7638                        (integer :tag "Button")
7639                        (sexp :tag "Form")
7640                        (function :tag "Callback")
7641                        (repeat :tag "Par"
7642                                :inline t
7643                                (integer :tag "Regexp group")))))
7644 (put 'gnus-button-alist 'risky-local-variable t)
7645
7646 (defcustom gnus-header-button-alist
7647   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7648      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7649     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7650      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7651     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7652      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7653     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7654      0 (>= gnus-button-browse-level 0) browse-url 0)
7655     ("^Subject:" gnus-button-url-regexp
7656      0 (>= gnus-button-browse-level 0) browse-url 0)
7657     ("^[^:]+:" gnus-button-url-regexp
7658      0 (>= gnus-button-browse-level 0) browse-url 0)
7659     ("^OpenPGP:.*url=" gnus-button-url-regexp
7660      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7661     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7662      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7663     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7664      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7665   "*Alist of headers and regexps to match buttons in article heads.
7666
7667 This alist is very similar to `gnus-button-alist', except that each
7668 alist has an additional HEADER element first in each entry:
7669
7670 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7671
7672 HEADER is a regexp to match a header.  For a fuller explanation, see
7673 `gnus-button-alist'."
7674   :group 'gnus-article-buttons
7675   :group 'gnus-article-headers
7676   :type '(repeat (list (regexp :tag "Header")
7677                        (choice regexp variable)
7678                        (integer :tag "Button")
7679                        (sexp :tag "Form")
7680                        (function :tag "Callback")
7681                        (repeat :tag "Par"
7682                                :inline t
7683                                (integer :tag "Regexp group")))))
7684 (put 'gnus-header-button-alist 'risky-local-variable t)
7685
7686 ;;; Commands:
7687
7688 (defun gnus-article-push-button (event)
7689   "Check text under the mouse pointer for a callback function.
7690 If the text under the mouse pointer has a `gnus-callback' property,
7691 call it with the value of the `gnus-data' text property."
7692   (interactive "e")
7693   (set-buffer (window-buffer (posn-window (event-start event))))
7694   (let* ((pos (posn-point (event-start event)))
7695          (data (get-text-property pos 'gnus-data))
7696          (fun (get-text-property pos 'gnus-callback)))
7697     (goto-char pos)
7698     (when fun
7699       (funcall fun data))))
7700
7701 (defun gnus-article-press-button ()
7702   "Check text at point for a callback function.
7703 If the text at point has a `gnus-callback' property,
7704 call it with the value of the `gnus-data' text property."
7705   (interactive)
7706   (let ((data (get-text-property (point) 'gnus-data))
7707         (fun (get-text-property (point) 'gnus-callback)))
7708     (when fun
7709       (funcall fun data))))
7710
7711 (defun gnus-article-highlight (&optional force)
7712   "Highlight current article.
7713 This function calls `gnus-article-highlight-headers',
7714 `gnus-article-highlight-citation',
7715 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7716 do the highlighting.  See the documentation for those functions."
7717   (interactive (list 'force))
7718   (gnus-article-highlight-headers)
7719   (gnus-article-highlight-citation force)
7720   (gnus-article-highlight-signature)
7721   (gnus-article-add-buttons force)
7722   (gnus-article-add-buttons-to-head))
7723
7724 (defun gnus-article-highlight-some (&optional force)
7725   "Highlight current article.
7726 This function calls `gnus-article-highlight-headers',
7727 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7728 do the highlighting.  See the documentation for those functions."
7729   (interactive (list 'force))
7730   (gnus-article-highlight-headers)
7731   (gnus-article-highlight-signature)
7732   (gnus-article-add-buttons))
7733
7734 (defun gnus-article-highlight-headers ()
7735   "Highlight article headers as specified by `gnus-header-face-alist'."
7736   (interactive)
7737   (gnus-with-article-headers
7738     (let (regexp header-face field-face from hpoints fpoints)
7739       (dolist (entry gnus-header-face-alist)
7740         (goto-char (point-min))
7741         (setq regexp (concat "^\\("
7742                              (if (string-equal "" (nth 0 entry))
7743                                  "[^\t ]"
7744                                (nth 0 entry))
7745                              "\\)")
7746               header-face (nth 1 entry)
7747               field-face (nth 2 entry))
7748         (while (and (re-search-forward regexp nil t)
7749                     (not (eobp)))
7750           (beginning-of-line)
7751           (setq from (point))
7752           (unless (search-forward ":" nil t)
7753             (forward-char 1))
7754           (when (and header-face
7755                      (not (memq (point) hpoints)))
7756             (push (point) hpoints)
7757             (gnus-put-text-property from (point) 'face header-face))
7758           (when (and field-face
7759                      (not (memq (setq from (point)) fpoints)))
7760             (push from fpoints)
7761             (if (re-search-forward "^[^ \t]" nil t)
7762                 (forward-char -2)
7763               (goto-char (point-max)))
7764             (gnus-put-text-property from (point) 'face field-face)))))))
7765
7766 (defun gnus-article-highlight-signature ()
7767   "Highlight the signature in an article.
7768 It does this by highlighting everything after
7769 `gnus-signature-separator' using the face `gnus-signature'."
7770   (interactive)
7771   (gnus-with-article-buffer
7772     (let ((inhibit-point-motion-hooks t))
7773       (save-restriction
7774         (when (and gnus-signature-face
7775                    (gnus-article-narrow-to-signature))
7776           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7777                             'face gnus-signature-face)
7778           (widen)
7779           (gnus-article-search-signature)
7780           (let ((start (match-beginning 0))
7781                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7782             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7783                                      end)))))))
7784
7785 (defun gnus-button-in-region-p (b e prop)
7786   "Say whether PROP exists in the region."
7787   (text-property-not-all b e prop nil))
7788
7789 (defun gnus-article-add-buttons (&optional force)
7790   "Find external references in the article and make buttons of them.
7791 \"External references\" are things like Message-IDs and URLs, as
7792 specified by `gnus-button-alist'."
7793   (interactive (list 'force))
7794   (gnus-with-article-buffer
7795     (let ((inhibit-point-motion-hooks t)
7796           (case-fold-search t)
7797           (alist gnus-button-alist)
7798           beg entry regexp)
7799       ;; Remove all old markers.
7800       (let (marker entry new-list)
7801         (while (setq marker (pop gnus-button-marker-list))
7802           (if (or (< marker (point-min)) (>= marker (point-max)))
7803               (push marker new-list)
7804             (goto-char marker)
7805             (when (setq entry (gnus-button-entry))
7806               (put-text-property (match-beginning (nth 1 entry))
7807                                  (match-end (nth 1 entry))
7808                                  'gnus-callback nil))
7809             (set-marker marker nil)))
7810         (setq gnus-button-marker-list new-list))
7811       ;; We skip the headers.
7812       (article-goto-body)
7813       (setq beg (point))
7814       (while (setq entry (pop alist))
7815         (setq regexp (eval (car entry)))
7816         (goto-char beg)
7817         (while (re-search-forward regexp nil t)
7818           (let ((start (match-beginning (nth 1 entry)))
7819                 (end (match-end (nth 1 entry)))
7820                 (from (match-beginning 0)))
7821             (when (and (or (eq t (nth 2 entry))
7822                            (eval (nth 2 entry)))
7823                        (not (gnus-button-in-region-p
7824                              start end 'gnus-callback)))
7825               ;; That optional form returned non-nil, so we add the
7826               ;; button.
7827               (setq from (set-marker (make-marker) from))
7828               (push from gnus-button-marker-list)
7829               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7830                            (gnus-article-extend-url-button from start end))
7831                 (gnus-article-add-button start end
7832                                          'gnus-button-push from)
7833                 (gnus-put-text-property
7834                  start end
7835                  'gnus-string (buffer-substring-no-properties
7836                                start end))))))))))
7837
7838 (defun gnus-article-extend-url-button (beg start end)
7839   "Extend url button if url is folded into two or more lines.
7840 Return non-nil if button is extended.  BEG is a marker that points to
7841 the beginning position of a text containing url.  START and END are
7842 the endpoints of a url button before it is extended.  The concatenated
7843 url is put as the `gnus-button-url' overlay property on the button."
7844   (let ((opoint (point))
7845         (points (list start end))
7846         url delim regexp)
7847     (prog1
7848         (when (and (progn
7849                      (goto-char end)
7850                      (not (looking-at "[\t ]*[\">]")))
7851                    (progn
7852                      (goto-char start)
7853                      (string-match
7854                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7855                       (buffer-substring (point-at-bol) start)))
7856                    (progn
7857                      (setq url (list (buffer-substring start end))
7858                            delim (if (match-beginning 1) ">" "\""))
7859                      (beginning-of-line)
7860                      (setq regexp (concat
7861                                    (when (and (looking-at
7862                                                message-cite-prefix-regexp)
7863                                               (< (match-end 0) start))
7864                                      (regexp-quote (match-string 0)))
7865                                    "\
7866 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7867                                    delim "\\)"))
7868                      (while (progn
7869                               (forward-line 1)
7870                               (and (looking-at regexp)
7871                                    (prog1
7872                                        (match-beginning 1)
7873                                      (push (or (match-string 2)
7874                                                (match-string 1))
7875                                            url)
7876                                      (push (setq end (or (match-end 2)
7877                                                          (match-end 1)))
7878                                            points)
7879                                      (push (or (match-beginning 2)
7880                                                (match-beginning 1))
7881                                            points)))))
7882                      (match-beginning 2)))
7883           (let (gnus-article-mouse-face widget-mouse-face)
7884             (while points
7885               (gnus-article-add-button (pop points) (pop points)
7886                                        'gnus-button-push beg)))
7887           (let ((overlay (gnus-make-overlay start end)))
7888             (gnus-overlay-put overlay 'evaporate t)
7889             (gnus-overlay-put overlay 'gnus-button-url
7890                               (list (mapconcat 'identity (nreverse url) "")))
7891             (when gnus-article-mouse-face
7892               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7893           t)
7894       (goto-char opoint))))
7895
7896 ;; Add buttons to the head of an article.
7897 (defun gnus-article-add-buttons-to-head ()
7898   "Add buttons to the head of the article."
7899   (interactive)
7900   (gnus-with-article-headers
7901     (let (beg end)
7902       (dolist (entry gnus-header-button-alist)
7903         ;; Each alist entry.
7904         (goto-char (point-min))
7905         (while (re-search-forward (car entry) nil t)
7906           ;; Each header matching the entry.
7907           (setq beg (match-beginning 0))
7908           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7909                              (match-beginning 0))
7910                         (point-max)))
7911           (goto-char beg)
7912           (while (re-search-forward (eval (nth 1 entry)) end t)
7913             ;; Each match within a header.
7914             (let* ((entry (cdr entry))
7915                    (start (match-beginning (nth 1 entry)))
7916                    (end (match-end (nth 1 entry)))
7917                    (form (nth 2 entry)))
7918               (goto-char (match-end 0))
7919               (when (eval form)
7920                 (gnus-article-add-button
7921                  start end (nth 3 entry)
7922                  (buffer-substring (match-beginning (nth 4 entry))
7923                                    (match-end (nth 4 entry)))))))
7924           (goto-char end))))))
7925
7926 ;;; External functions:
7927
7928 (defun gnus-article-add-button (from to fun &optional data text)
7929   "Create a button between FROM and TO with callback FUN and data DATA."
7930   (when gnus-article-button-face
7931     (gnus-overlay-put (gnus-make-overlay from to nil t)
7932                       'face gnus-article-button-face))
7933   (gnus-add-text-properties
7934    from to
7935    (nconc (and gnus-article-mouse-face
7936                (list gnus-mouse-face-prop gnus-article-mouse-face))
7937           (list 'gnus-callback fun)
7938           (and data (list 'gnus-data data))))
7939   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7940                          :help-echo (or text "Follow the link")
7941                          :keymap gnus-url-button-map
7942                          :button-keymap gnus-widget-button-keymap))
7943
7944 (defun gnus-article-copy-string ()
7945   "Copy the string in the button to the kill ring."
7946   (interactive)
7947   (gnus-article-check-buffer)
7948   (let ((data (get-text-property (point) 'gnus-string)))
7949     (when data
7950       (with-temp-buffer
7951         (insert data)
7952         (copy-region-as-kill (point-min) (point-max))
7953         (message "Copied %s" data)))))
7954
7955 ;;; Internal functions:
7956
7957 (defun gnus-article-set-globals ()
7958   (with-current-buffer gnus-summary-buffer
7959     (gnus-set-global-variables)))
7960
7961 (defun gnus-signature-toggle (end)
7962   (gnus-with-article-buffer
7963     (let ((inhibit-point-motion-hooks t))
7964       (if (text-property-any end (point-max) 'article-type 'signature)
7965           (progn
7966             (gnus-delete-wash-type 'signature)
7967             (gnus-remove-text-properties-when
7968              'article-type 'signature end (point-max)
7969              (cons 'article-type (cons 'signature
7970                                        gnus-hidden-properties))))
7971         (gnus-add-wash-type 'signature)
7972         (gnus-add-text-properties-when
7973          'article-type nil end (point-max)
7974          (cons 'article-type (cons 'signature
7975                                    gnus-hidden-properties)))))
7976     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7977       (gnus-set-mode-line 'article))))
7978
7979 (defun gnus-button-entry ()
7980   ;; Return the first entry in `gnus-button-alist' matching this place.
7981   (let ((alist gnus-button-alist)
7982         (entry nil))
7983     (while alist
7984       (setq entry (pop alist))
7985       (if (looking-at (eval (car entry)))
7986           (setq alist nil)
7987         (setq entry nil)))
7988     entry))
7989
7990 (defun gnus-button-push (marker)
7991   ;; Push button starting at MARKER.
7992   (save-excursion
7993     (goto-char marker)
7994     (let* ((entry (gnus-button-entry))
7995            (inhibit-point-motion-hooks t)
7996            (fun (nth 3 entry))
7997            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7998                           (get-char-property marker 'gnus-button-url))
7999                      (mapcar (lambda (group)
8000                                (let ((string (match-string group)))
8001                                  (set-text-properties
8002                                   0 (length string) nil string)
8003                                  string))
8004                              (nthcdr 4 entry)))))
8005       (cond
8006        ((fboundp fun)
8007         (apply fun args))
8008        ((and (boundp fun)
8009              (fboundp (symbol-value fun)))
8010         (apply (symbol-value fun) args))
8011        (t
8012         (gnus-message 1 "You must define `%S' to use this button"
8013                       (cons fun args)))))))
8014
8015 (defun gnus-parse-news-url (url)
8016   (let (scheme server port group message-id articles)
8017     (with-temp-buffer
8018       (insert url)
8019       (goto-char (point-min))
8020       (when (looking-at "\\([A-Za-z]+\\):")
8021         (setq scheme (match-string 1))
8022         (goto-char (match-end 0)))
8023       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
8024         (setq server (match-string 1))
8025         (setq port (if (stringp (match-string 3))
8026                        (string-to-number (match-string 3))
8027                      (match-string 3)))
8028         (goto-char (match-end 0)))
8029
8030       (cond
8031        ((looking-at "\\(.*@.*\\)")
8032         (setq message-id (match-string 1)))
8033        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
8034         (setq group (match-string 1)
8035               articles (split-string (match-string 2) "-")))
8036        ((looking-at "\\([^/]+\\)/?")
8037         (setq group (match-string 1)))
8038        (t
8039         (error "Unknown news URL syntax"))))
8040     (list scheme server port group message-id articles)))
8041
8042 (defun gnus-button-handle-news (url)
8043   "Fetch a news URL."
8044   (destructuring-bind (scheme server port group message-id articles)
8045       (gnus-parse-news-url url)
8046     (cond
8047      (message-id
8048       (with-current-buffer gnus-summary-buffer
8049         (if server
8050             (let ((gnus-refer-article-method
8051                    (nconc (list (list 'nntp server))
8052                           gnus-refer-article-method))
8053                   (nntp-port-number (or port "nntp")))
8054               (gnus-message 7 "Fetching %s with %s"
8055                             message-id gnus-refer-article-method)
8056               (gnus-summary-refer-article message-id))
8057           (gnus-summary-refer-article message-id))))
8058      (group
8059       (gnus-button-fetch-group url)))))
8060
8061 (defun gnus-button-patch (library line)
8062   "Visit an Emacs Lisp library LIBRARY on line LINE."
8063   (interactive)
8064   (let ((file (locate-library (file-name-nondirectory library))))
8065     (unless file
8066       (error "Couldn't find library %s" library))
8067     (find-file file)
8068     (goto-char (point-min))
8069     (forward-line (1- (string-to-number line)))))
8070
8071 (defun gnus-button-handle-man (url)
8072   "Fetch a man page."
8073   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8074   (when (eq gnus-button-man-handler 'woman)
8075     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
8076   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
8077   (funcall gnus-button-man-handler url))
8078
8079 (defun gnus-button-handle-info-url (url)
8080   "Fetch an info URL."
8081   (setq url (mm-subst-char-in-string ?+ ?\  url))
8082   (cond
8083    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
8084     (gnus-info-find-node
8085      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
8086                      "Gnus")
8087              ")" (gnus-url-unhex-string (match-string 2 url)))))
8088    ((string-match "([^)\"]+)[^\"]+" url)
8089     (setq url
8090           (gnus-replace-in-string
8091            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
8092     (gnus-info-find-node url))
8093    (t (error "Can't parse %s" url))))
8094
8095 (defun gnus-button-handle-info-url-gnome (url)
8096   "Fetch GNOME style info URL."
8097   (setq url (mm-subst-char-in-string ?_ ?\  url))
8098   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
8099       (gnus-info-find-node
8100        (concat "("
8101                (gnus-url-unhex-string
8102                  (match-string 1 url))
8103                ")"
8104                (or (gnus-url-unhex-string
8105                     (match-string 2 url))
8106                    "Top")))
8107     (error "Can't parse %s" url)))
8108
8109 (defun gnus-button-handle-info-url-kde (url)
8110   "Fetch KDE style info URL."
8111   (gnus-info-find-node (gnus-url-unhex-string url)))
8112
8113 ;; (info) will autoload info.el
8114 (declare-function Info-menu "info" (menu-item &optional fork))
8115 (declare-function Info-index-next "info" (num))
8116
8117 (defun gnus-button-handle-info-keystrokes (url)
8118   "Call `info' when pushing the corresponding URL button."
8119   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
8120   (let (node indx comma)
8121     (if (string-match
8122          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
8123                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8124                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET"
8125                  "\\(?:[ \t\n,]*\\)\\)?")
8126          url)
8127         (setq node (match-string 2 url)
8128               indx (match-string 3 url))
8129       (error "Can't parse %s" url))
8130     (info)
8131     (Info-directory)
8132     (Info-menu node)
8133     (when (> (length indx) 0)
8134       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
8135                             "\\([ \t\n,]*\\)")
8136                     indx)
8137       (setq comma (match-string 2 indx))
8138       (setq indx  (match-string 1 indx))
8139       (Info-index indx)
8140       (when comma
8141         (dotimes (i (with-temp-buffer
8142                       (insert comma)
8143                       ;; Note: the XEmacs version of `how-many' takes
8144                       ;; no optional argument.
8145                       (goto-char (point-min))
8146                       (how-many ",")))
8147           (Info-index-next 1)))
8148       nil)))
8149
8150 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
8151 (declare-function pgg-display-output-buffer "pgg" (start end status))
8152
8153 (defun gnus-button-openpgp (url)
8154   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
8155   (with-temp-buffer
8156     (mm-url-insert-file-contents-external url)
8157     (pgg-snarf-keys-region (point-min) (point-max))
8158     (pgg-display-output-buffer nil nil nil)))
8159
8160 (defun gnus-button-message-id (message-id)
8161   "Fetch MESSAGE-ID."
8162   (with-current-buffer gnus-summary-buffer
8163     (gnus-summary-refer-article message-id)))
8164
8165 (defun gnus-button-fetch-group (address &rest ignore)
8166   "Fetch GROUP specified by ADDRESS."
8167   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
8168                       address)
8169     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
8170     ;; for nntp:// and news://
8171     (setq address (match-string 3 address)))
8172   (if (not (string-match "[:/]" address))
8173       ;; This is just a simple group url.
8174       (gnus-group-read-ephemeral-group address gnus-select-method)
8175     (if (not
8176          (string-match
8177           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
8178           address))
8179         (error "Can't parse %s" address)
8180       (gnus-group-read-ephemeral-group
8181        (match-string 4 address)
8182        `(nntp ,(match-string 1 address)
8183               (nntp-address ,(match-string 1 address))
8184               (nntp-port-number ,(if (match-end 3)
8185                                      (match-string 3 address)
8186                                    "nntp")))
8187        nil nil nil
8188        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
8189
8190 (defun gnus-url-parse-query-string (query &optional downcase)
8191   (let (retval pairs cur key val)
8192     (setq pairs (split-string query "&"))
8193     (while pairs
8194       (setq cur (car pairs)
8195             pairs (cdr pairs))
8196       (if (not (string-match "=" cur))
8197           nil                           ; Grace
8198         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
8199               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
8200         (if downcase
8201             (setq key (downcase key)))
8202         (setq cur (assoc key retval))
8203         (if cur
8204             (setcdr cur (cons val (cdr cur)))
8205           (setq retval (cons (list key val) retval)))))
8206     retval))
8207
8208 (defun gnus-url-mailto (url)
8209   ;; Send mail to someone
8210   (when (string-match "mailto:/*\\(.*\\)" url)
8211     (setq url (substring url (match-beginning 1) nil)))
8212   (let (to args subject func)
8213     (setq args (gnus-url-parse-query-string
8214                 (if (string-match "^\\?" url)
8215                     (substring url 1)
8216                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8217                       (concat "to=" (match-string 1 url) "&"
8218                               (match-string 2 url))
8219                     (concat "to=" url)))
8220                 t)
8221           subject (cdr-safe (assoc "subject" args)))
8222     (gnus-msg-mail)
8223     (while args
8224       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8225       (if (fboundp func)
8226           (funcall func)
8227         (message-position-on-field (caar args)))
8228       (insert (gnus-replace-in-string
8229                (mapconcat 'identity (reverse (cdar args)) ", ")
8230                "\r\n" "\n" t))
8231       (setq args (cdr args)))
8232     (if subject
8233         (message-goto-body)
8234       (message-goto-subject))))
8235
8236 (defun gnus-button-embedded-url (address)
8237   "Activate ADDRESS with `browse-url'."
8238   (browse-url (gnus-strip-whitespace address)))
8239
8240 ;;; Next/prev buttons in the article buffer.
8241
8242 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8243 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8244
8245 (defvar gnus-prev-page-map
8246   (let ((map (make-sparse-keymap)))
8247     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8248     (define-key map "\r" 'gnus-button-prev-page)
8249     map))
8250
8251 (defvar gnus-next-page-map
8252   (let ((map (make-sparse-keymap)))
8253     (unless (>= emacs-major-version 21)
8254       ;; XEmacs doesn't care.
8255       (set-keymap-parent map gnus-article-mode-map))
8256     (define-key map gnus-mouse-2 'gnus-button-next-page)
8257     (define-key map "\r" 'gnus-button-next-page)
8258     map))
8259
8260 (defun gnus-insert-prev-page-button ()
8261   (let ((b (point)) e
8262         (inhibit-read-only t))
8263     (gnus-eval-format
8264      gnus-prev-page-line-format nil
8265      `(keymap ,gnus-prev-page-map
8266               gnus-prev t
8267               gnus-callback gnus-article-button-prev-page
8268               article-type annotation))
8269     (setq e (if (bolp)
8270                 ;; Exclude a newline.
8271                 (1- (point))
8272               (point)))
8273     (when gnus-article-button-face
8274       (gnus-overlay-put (gnus-make-overlay b e nil t)
8275                         'face gnus-article-button-face))
8276     (widget-convert-button
8277      'link b e
8278      :action 'gnus-button-prev-page
8279      :button-keymap gnus-prev-page-map)))
8280
8281 (defun gnus-button-next-page (&optional args more-args)
8282   "Go to the next page."
8283   (interactive)
8284   (let ((win (selected-window)))
8285     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8286     (gnus-article-next-page)
8287     (select-window win)))
8288
8289 (defun gnus-button-prev-page (&optional args more-args)
8290   "Go to the prev page."
8291   (interactive)
8292   (let ((win (selected-window)))
8293     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8294     (gnus-article-prev-page)
8295     (select-window win)))
8296
8297 (defun gnus-insert-next-page-button ()
8298   (let ((b (point)) e
8299         (inhibit-read-only t))
8300     (gnus-eval-format gnus-next-page-line-format nil
8301                       `(keymap ,gnus-next-page-map
8302                                gnus-next t
8303                                gnus-callback gnus-article-button-next-page
8304                                article-type annotation))
8305     (setq e (if (bolp)
8306                 ;; Exclude a newline.
8307                 (1- (point))
8308               (point)))
8309     (when gnus-article-button-face
8310       (gnus-overlay-put (gnus-make-overlay b e nil t)
8311                         'face gnus-article-button-face))
8312     (widget-convert-button
8313      'link b e
8314      :action 'gnus-button-next-page
8315      :button-keymap gnus-next-page-map)))
8316
8317 (defun gnus-article-button-next-page (arg)
8318   "Go to the next page."
8319   (interactive "P")
8320   (let ((win (selected-window)))
8321     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8322     (gnus-article-next-page)
8323     (select-window win)))
8324
8325 (defun gnus-article-button-prev-page (arg)
8326   "Go to the prev page."
8327   (interactive "P")
8328   (let ((win (selected-window)))
8329     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8330     (gnus-article-prev-page)
8331     (select-window win)))
8332
8333 (defvar gnus-decode-header-methods
8334   '(mail-decode-encoded-word-region)
8335   "List of methods used to decode headers.
8336
8337 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8338 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8339 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8340 whose names match REGEXP.
8341
8342 For example:
8343 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8344  mail-decode-encoded-word-region
8345  (\"chinese\" . rfc1843-decode-region))
8346 ")
8347
8348 (defvar gnus-decode-header-methods-cache nil)
8349
8350 (defun gnus-multi-decode-header (start end)
8351   "Apply the functions from `gnus-encoded-word-methods' that match."
8352   (unless (and gnus-decode-header-methods-cache
8353                (eq gnus-newsgroup-name
8354                    (car gnus-decode-header-methods-cache)))
8355     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8356     (dolist (x gnus-decode-header-methods)
8357       (if (symbolp x)
8358           (nconc gnus-decode-header-methods-cache (list x))
8359         (if (and gnus-newsgroup-name
8360                  (string-match (car x) gnus-newsgroup-name))
8361             (nconc gnus-decode-header-methods-cache
8362                    (list (cdr x)))))))
8363   (let ((xlist gnus-decode-header-methods-cache))
8364     (pop xlist)
8365     (save-restriction
8366       (narrow-to-region start end)
8367       (while xlist
8368         (funcall (pop xlist) (point-min) (point-max))))))
8369
8370 ;;;
8371 ;;; Treatment top-level handling.
8372 ;;;
8373
8374 (defun gnus-treat-article (condition &optional part-number total-parts type)
8375   (let ((length (- (point-max) (point-min)))
8376         (alist gnus-treatment-function-alist)
8377         (article-goto-body-goes-to-point-min-p t)
8378         (treated-type
8379          (or (not type)
8380              (catch 'found
8381                (let ((list gnus-article-treat-types))
8382                  (while list
8383                    (when (string-match (pop list) type)
8384                      (throw 'found t)))))))
8385         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8386         val elem)
8387     (gnus-run-hooks 'gnus-part-display-hook)
8388     (dolist (elem alist)
8389       (setq val
8390             (save-excursion
8391               (when (gnus-buffer-live-p gnus-summary-buffer)
8392                 (set-buffer gnus-summary-buffer))
8393               (symbol-value (car elem))))
8394       (when (and (or (consp val)
8395                      treated-type)
8396                  (gnus-treat-predicate val)
8397                  (or (not (get (car elem) 'highlight))
8398                      highlightp))
8399         (save-restriction
8400           (funcall (cadr elem)))))))
8401
8402 ;; Dynamic variables.
8403 (defvar part-number)
8404 (defvar total-parts)
8405 (defvar type)
8406 (defvar condition)
8407 (defvar length)
8408
8409 (defun gnus-treat-predicate (val)
8410   (cond
8411    ((null val)
8412     nil)
8413    (condition
8414     (eq condition val))
8415    ((and (listp val)
8416          (stringp (car val)))
8417     (apply 'gnus-or (mapcar `(lambda (s)
8418                                (string-match s ,(or gnus-newsgroup-name "")))
8419                             val)))
8420    ((listp val)
8421     (let ((pred (pop val)))
8422       (cond
8423        ((eq pred 'or)
8424         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8425        ((eq pred 'and)
8426         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8427        ((eq pred 'not)
8428         (not (gnus-treat-predicate (car val))))
8429        ((eq pred 'typep)
8430         (equal (car val) type))
8431        (t
8432         (error "%S is not a valid predicate" pred)))))
8433    ((eq val t)
8434     t)
8435    ((eq val 'head)
8436     nil)
8437    ((eq val 'first)
8438     (eq part-number 1))
8439    ((eq val 'last)
8440     (eq part-number total-parts))
8441    ((numberp val)
8442     (< length val))
8443    (t
8444     (error "%S is not a valid value" val))))
8445
8446 (defun gnus-article-encrypt-body (protocol &optional n)
8447   "Encrypt the article body."
8448   (interactive
8449    (list
8450     (or gnus-article-encrypt-protocol
8451         (completing-read "Encrypt protocol: "
8452                          gnus-article-encrypt-protocol-alist
8453                          nil t))
8454     current-prefix-arg))
8455   ;; User might hit `K E' instead of `K e', so prompt once.
8456   (when (and gnus-article-encrypt-protocol
8457              gnus-novice-user)
8458     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8459       (error "Encrypt aborted")))
8460   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8461     (unless func
8462       (error "Can't find the encrypt protocol %s" protocol))
8463     (if (member gnus-newsgroup-name '("nndraft:delayed"
8464                                       "nndraft:drafts"
8465                                       "nndraft:queue"))
8466         (error "Can't encrypt the article in group %s"
8467                gnus-newsgroup-name))
8468     (gnus-summary-iterate n
8469       (with-current-buffer gnus-summary-buffer
8470         (let ((mail-parse-charset gnus-newsgroup-charset)
8471               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8472               (summary-buffer gnus-summary-buffer)
8473               references point)
8474           (gnus-set-global-variables)
8475           (when (gnus-group-read-only-p)
8476             (error "The current newsgroup does not support article encrypt"))
8477           (gnus-summary-show-article t)
8478           (setq references
8479               (or (mail-header-references gnus-current-headers) ""))
8480           (set-buffer gnus-article-buffer)
8481           (let* ((inhibit-read-only t)
8482                  (headers
8483                   (mapcar (lambda (field)
8484                             (and (save-restriction
8485                                    (message-narrow-to-head)
8486                                    (goto-char (point-min))
8487                                    (search-forward field nil t))
8488                                  (prog2
8489                                      (message-narrow-to-field)
8490                                      (buffer-string)
8491                                    (delete-region (point-min) (point-max))
8492                                    (widen))))
8493                           '("Content-Type:" "Content-Transfer-Encoding:"
8494                             "Content-Disposition:"))))
8495             (message-narrow-to-head)
8496             (message-remove-header "MIME-Version")
8497             (goto-char (point-max))
8498             (setq point (point))
8499             (insert (apply 'concat headers))
8500             (widen)
8501             (narrow-to-region point (point-max))
8502             (let ((message-options message-options))
8503               (message-options-set 'message-sender user-mail-address)
8504               (message-options-set 'message-recipients user-mail-address)
8505               (message-options-set 'message-sign-encrypt 'not)
8506               (funcall func))
8507             (goto-char (point-min))
8508             (insert "MIME-Version: 1.0\n")
8509             (widen)
8510             (gnus-summary-edit-article-done
8511              references nil summary-buffer t))
8512           (when gnus-keep-backlog
8513             (gnus-backlog-remove-article
8514              (car gnus-article-current) (cdr gnus-article-current)))
8515           (when (get-buffer gnus-original-article-buffer)
8516             (with-current-buffer gnus-original-article-buffer
8517               (setq gnus-original-article nil)))
8518           (when gnus-use-cache
8519             (gnus-cache-update-article
8520              (car gnus-article-current) (cdr gnus-article-current))))))))
8521
8522 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8523   "The following specs can be used:
8524 %t  The security MIME type
8525 %i  Additional info
8526 %d  Details
8527 %D  Details if button is pressed")
8528
8529 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8530   "The following specs can be used:
8531 %t  The security MIME type
8532 %i  Additional info
8533 %d  Details
8534 %D  Details if button is pressed")
8535
8536 (defvar gnus-mime-security-button-line-format-alist
8537   '((?t gnus-tmp-type ?s)
8538     (?i gnus-tmp-info ?s)
8539     (?d gnus-tmp-details ?s)
8540     (?D gnus-tmp-pressed-details ?s)))
8541
8542 (defvar gnus-mime-security-button-commands
8543   '((gnus-article-press-button "\r" "Show Detail")
8544     (undefined "v")
8545     (undefined "t")
8546     (undefined "C")
8547     (gnus-mime-security-save-part "o" "Save...")
8548     (undefined "\C-o")
8549     (undefined "r")
8550     (undefined "d")
8551     (undefined "c")
8552     (undefined "i")
8553     (undefined "E")
8554     (undefined "e")
8555     (undefined "p")
8556     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8557     (undefined ".")))
8558
8559 (defvar gnus-mime-security-button-map
8560   (let ((map (make-sparse-keymap)))
8561     (define-key map gnus-mouse-2 'gnus-article-push-button)
8562     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8563     (dolist (c gnus-mime-security-button-commands)
8564       (define-key map (cadr c) (car c)))
8565     map))
8566
8567 (easy-menu-define
8568   gnus-mime-security-button-menu gnus-mime-security-button-map
8569   "Security button menu."
8570   `("Security Part"
8571     ,@(delq nil
8572             (mapcar (lambda (c)
8573                       (unless (eq (car c) 'undefined)
8574                         (vector (caddr c) (car c) :active t)))
8575                     gnus-mime-security-button-commands))))
8576
8577 (defun gnus-mime-security-button-menu (event prefix)
8578   "Construct a context-sensitive menu of security commands."
8579   (interactive "e\nP")
8580   (save-window-excursion
8581     (let ((pos (event-start event)))
8582       (select-window (posn-window pos))
8583       (goto-char (posn-point pos))
8584       (gnus-article-check-buffer)
8585       (popup-menu gnus-mime-security-button-menu nil prefix))))
8586
8587 (defvar gnus-mime-security-details-buffer nil)
8588
8589 (defvar gnus-mime-security-button-pressed nil)
8590
8591 (defvar gnus-mime-security-show-details-inline t
8592   "If non-nil, show details in the article buffer.")
8593
8594 (defun gnus-mime-security-verify-or-decrypt (handle)
8595   (mm-remove-parts (cdr handle))
8596   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8597         point (inhibit-read-only t))
8598     (if region
8599         (goto-char (car region)))
8600     (setq point (point))
8601     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8602       (let* ((mm-verify-option 'known)
8603              (mm-decrypt-option 'known)
8604              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8605         (unless (eq nparts (cdr handle))
8606           (mm-destroy-parts (cdr handle))
8607           (setcdr handle nparts))))
8608     (gnus-mime-display-security handle)
8609     (when region
8610       (delete-region (point) (cdr region))
8611       (set-marker (car region) nil)
8612       (set-marker (cdr region) nil))
8613     (goto-char point)))
8614
8615 (defun gnus-mime-security-show-details (handle)
8616   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8617     (if (not details)
8618         (gnus-message 5 "No details.")
8619       (if gnus-mime-security-show-details-inline
8620           (let ((gnus-mime-security-button-pressed
8621                  (not (get-text-property (point) 'gnus-mime-details)))
8622                 (gnus-mime-security-button-line-format
8623                  (get-text-property (point) 'gnus-line-format))
8624                 (inhibit-read-only t))
8625             (forward-char -1)
8626             (while (eq (get-text-property (point) 'gnus-line-format)
8627                        gnus-mime-security-button-line-format)
8628               (forward-char -1))
8629             (forward-char)
8630             (save-restriction
8631               (narrow-to-region (point) (point))
8632               (gnus-insert-mime-security-button handle))
8633             (delete-region (point)
8634                            (or (text-property-not-all
8635                                 (point) (point-max)
8636                                 'gnus-line-format
8637                                 gnus-mime-security-button-line-format)
8638                                (point-max))))
8639         ;; Not inlined.
8640         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8641             (with-current-buffer gnus-mime-security-details-buffer
8642               (erase-buffer)
8643               t)
8644           (setq gnus-mime-security-details-buffer
8645                 (gnus-get-buffer-create "*MIME Security Details*")))
8646         (with-current-buffer gnus-mime-security-details-buffer
8647           (insert details)
8648           (goto-char (point-min)))
8649         (pop-to-buffer gnus-mime-security-details-buffer)))))
8650
8651 (defun gnus-mime-security-press-button (handle)
8652   (save-excursion
8653     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8654         (gnus-mime-security-show-details handle)
8655       (gnus-mime-security-verify-or-decrypt handle))))
8656
8657 (defun gnus-insert-mime-security-button (handle &optional displayed)
8658   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8659          (gnus-tmp-type
8660           (concat
8661            (or (nth 2 (assoc protocol mm-verify-function-alist))
8662                (nth 2 (assoc protocol mm-decrypt-function-alist))
8663                "Unknown")
8664            (if (equal (car handle) "multipart/signed")
8665                " Signed" " Encrypted")
8666            " Part"))
8667          (gnus-tmp-info
8668           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8669               "Undecided"))
8670          (gnus-tmp-details
8671           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8672          gnus-tmp-pressed-details
8673          b e)
8674     (setq gnus-tmp-details
8675           (if gnus-tmp-details
8676               (concat "\n" gnus-tmp-details)
8677             ""))
8678     (setq gnus-tmp-pressed-details
8679           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8680     (unless (bolp)
8681       (insert "\n"))
8682     (setq b (point))
8683     (gnus-eval-format
8684      gnus-mime-security-button-line-format
8685      gnus-mime-security-button-line-format-alist
8686      `(keymap ,gnus-mime-security-button-map
8687          gnus-callback gnus-mime-security-press-button
8688          gnus-line-format ,gnus-mime-security-button-line-format
8689          gnus-mime-details ,gnus-mime-security-button-pressed
8690          article-type annotation
8691          gnus-data ,handle))
8692     (setq e (if (bolp)
8693                 ;; Exclude a newline.
8694                 (1- (point))
8695               (point)))
8696     (when gnus-article-button-face
8697       (gnus-overlay-put (gnus-make-overlay b e nil t)
8698                         'face gnus-article-button-face))
8699     (widget-convert-button
8700      'link b e
8701      :mime-handle handle
8702      :action 'gnus-widget-press-button
8703      :button-keymap gnus-mime-security-button-map
8704      :help-echo
8705      (lambda (widget/window &optional overlay pos)
8706        ;; Needed to properly clear the message due to a bug in
8707        ;; wid-edit (XEmacs only).
8708        (when (boundp 'help-echo-owns-message)
8709          (setq help-echo-owns-message t))
8710        (format
8711         "%S: show detail; %S: more options"
8712         (aref gnus-mouse-2 0)
8713         (aref gnus-down-mouse-3 0))))))
8714
8715 (defun gnus-mime-display-security (handle)
8716   (save-restriction
8717     (narrow-to-region (point) (point))
8718     (unless (gnus-unbuttonized-mime-type-p (car handle))
8719       (gnus-insert-mime-security-button handle))
8720     (gnus-mime-display-part (cadr handle))
8721     (unless (bolp)
8722       (insert "\n"))
8723     (unless (gnus-unbuttonized-mime-type-p (car handle))
8724       (let ((gnus-mime-security-button-line-format
8725              gnus-mime-security-button-end-line-format))
8726         (gnus-insert-mime-security-button handle)))
8727     (mm-set-handle-multipart-parameter
8728      handle 'gnus-region
8729      (cons (set-marker (make-marker) (point-min))
8730            (set-marker (make-marker) (point-max))))
8731     (goto-char (point-max))))
8732
8733 (defun gnus-mime-security-run-function (function)
8734   "Run FUNCTION with the security part under point."
8735   (gnus-article-check-buffer)
8736   (let ((data (get-text-property (point) 'gnus-data))
8737         buffer handle)
8738     (when (and (stringp (car-safe data))
8739                (setq buffer (mm-handle-multipart-original-buffer data))
8740                (setq handle (cadr data)))
8741       (if (bufferp (mm-handle-buffer handle))
8742           (progn
8743             (setq handle (cons buffer (copy-sequence (cdr handle))))
8744             (mm-handle-set-undisplayer handle nil))
8745         (setq handle (mm-make-handle
8746                       buffer
8747                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8748                       nil nil nil nil nil nil)))
8749       (funcall function handle))))
8750
8751 (defun gnus-mime-security-save-part ()
8752   "Save the security part under point."
8753   (interactive)
8754   (gnus-mime-security-run-function 'mm-save-part))
8755
8756 (defun gnus-mime-security-pipe-part ()
8757   "Pipe the security part under point to a process."
8758   (interactive)
8759   (gnus-mime-security-run-function 'mm-pipe-part))
8760
8761 (gnus-ems-redefine)
8762
8763 (provide 'gnus-art)
8764
8765 (run-hooks 'gnus-art-load-hook)
8766
8767 ;;; gnus-art.el ends here