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