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