t-gnus-6_15-quimby: New branch for developing and synchronizing with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Keywords: mail, news, MIME
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 2, or (at your option)
14 ;; 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; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31 (eval-when-compile (require 'static))
32
33 (require 'path-util)
34 (require 'gnus)
35 (require 'gnus-sum)
36 (require 'gnus-spec)
37 (require 'gnus-int)
38 (require 'alist)
39 (require 'mime-view)
40 (require 'wid-edit)
41
42 ;; Avoid byte-compile warnings.
43 (eval-when-compile
44   (require 'mm-bodies)
45   (require 'mail-parse)
46   (require 'mm-decode)
47   (require 'mm-view)
48   (require 'mm-uu)
49   )
50
51 (defgroup gnus-article nil
52   "Article display."
53   :link '(custom-manual "(gnus)The Article Buffer")
54   :group 'gnus)
55
56 (defgroup gnus-article-treat nil
57   "Treating article parts."
58   :link '(custom-manual "(gnus)Article Hiding")
59   :group 'gnus-article)
60
61 (defgroup gnus-article-hiding nil
62   "Hiding article parts."
63   :link '(custom-manual "(gnus)Article Hiding")
64   :group 'gnus-article)
65
66 (defgroup gnus-article-highlight nil
67   "Article highlighting."
68   :link '(custom-manual "(gnus)Article Highlighting")
69   :group 'gnus-article
70   :group 'gnus-visual)
71
72 (defgroup gnus-article-signature nil
73   "Article signatures."
74   :link '(custom-manual "(gnus)Article Signature")
75   :group 'gnus-article)
76
77 (defgroup gnus-article-headers nil
78   "Article headers."
79   :link '(custom-manual "(gnus)Hiding Headers")
80   :group 'gnus-article)
81
82 (defgroup gnus-article-washing nil
83   "Special commands on articles."
84   :link '(custom-manual "(gnus)Article Washing")
85   :group 'gnus-article)
86
87 (defgroup gnus-article-emphasis nil
88   "Fontisizing articles."
89   :link '(custom-manual "(gnus)Article Fontisizing")
90   :group 'gnus-article)
91
92 (defgroup gnus-article-saving nil
93   "Saving articles."
94   :link '(custom-manual "(gnus)Saving Articles")
95   :group 'gnus-article)
96
97 (defgroup gnus-article-mime nil
98   "Worshiping the MIME wonder."
99   :link '(custom-manual "(gnus)Using MIME")
100   :group 'gnus-article)
101
102 (defgroup gnus-article-buttons nil
103   "Pushable buttons in the article buffer."
104   :link '(custom-manual "(gnus)Article Buttons")
105   :group 'gnus-article)
106
107 (defgroup gnus-article-various nil
108   "Other article options."
109   :link '(custom-manual "(gnus)Misc Article")
110   :group 'gnus-article)
111
112 (defcustom gnus-ignored-headers
113   '("^Path:" "^Expires:" "^Date-Received:" "^References:" "^Xref:" "^Lines:"
114     "^Relay-Version:" "^Message-ID:" "^Approved:" "^Sender:" "^Received:"
115     "^X-UIDL:" "^MIME-Version:" "^Return-Path:" "^In-Reply-To:"
116     "^Content-Type:" "^Content-Transfer-Encoding:" "^X-WebTV-Signature:"
117     "^X-MimeOLE:" "^X-MSMail-Priority:" "^X-Priority:" "^X-Loop:"
118     "^X-Authentication-Warning:" "^X-MIME-Autoconverted:" "^X-Face:"
119     "^X-Attribution:" "^X-Originating-IP:" "^Delivered-To:"
120     "^NNTP-[-A-Za-z]+:" "^Distribution:" "^X-no-archive:" "^X-Trace:"
121     "^X-Complaints-To:" "^X-NNTP-Posting-Host:" "^X-Orig.*:"
122     "^Abuse-Reports-To:" "^Cache-Post-Path:" "^X-Article-Creation-Date:"
123     "^X-Poster:" "^X-Mail2News-Path:" "^X-Server-Date:" "^X-Cache:"
124     "^Originator:" "^X-Problems-To:" "^X-Auth-User:" "^X-Post-Time:"
125     "^X-Admin:" "^X-UID:" "^Resent-[-A-Za-z]+:" "^X-Mailing-List:"
126     "^Precedence:" "^Original-[-A-Za-z]+:" "^X-filename:" "^X-Orcpt:"
127     "^Old-Received:" "^X-Pgp" "^X-Auth:" "^X-From-Line:"
128     "^X-Gnus-Article-Number:" "^X-Majordomo:" "^X-Url:" "^X-Sender:"
129     "^MBOX-Line" "^Priority:" "^X400-[-A-Za-z]+:"
130     "^Status:" "^X-Gnus-Mail-Source:" "^Cancel-Lock:"
131     "^X-FTN" "^X-EXP32-SerialNo:" "^Encoding:" "^Importance:"
132     "^Autoforwarded:" "^Original-Encoded-Information-Types:" "^X-Ya-Pop3:"
133     "^X-Face-Version:" "^X-Vms-To:" "^X-ML-NAME:" "^X-ML-COUNT:"
134     "^Mailing-List:" "^X-finfo:" "^X-md5sum:" "^X-md5sum-Origin:"
135     "^X-Sun-Charset:" "^X-Accept-Language:" "^X-Envelope-Sender:"
136     "^List-[A-Za-z]+:" "^X-Listprocessor-Version:"
137     "^X-Received:" "^X-Distribute:" "^X-Sequence:" "^X-Juno-Line-Breaks:"
138     "^X-Notes-Item:" "^X-MS-TNEF-Correlator:" "^x-uunet-gateway:"
139     "^X-Received:" "^Content-length:" "X-precedence:")
140   "*All headers that start with this regexp will be hidden.
141 This variable can also be a list of regexps of headers to be ignored.
142 If `gnus-visible-headers' is non-nil, this variable will be ignored."
143   :type '(choice :custom-show nil
144                  regexp
145                  (repeat regexp))
146   :group 'gnus-article-hiding)
147
148 (defcustom gnus-visible-headers
149   "^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:"
150   "*All headers that do not match this regexp will be hidden.
151 This variable can also be a list of regexp of headers to remain visible.
152 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
153   :type '(repeat :value-to-internal (lambda (widget value)
154                                       (custom-split-regexp-maybe value))
155                  :match (lambda (widget value)
156                           (or (stringp value)
157                               (widget-editable-list-match widget value)))
158                  regexp)
159   :group 'gnus-article-hiding)
160
161 (defcustom gnus-sorted-header-list
162   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
163     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
164   "*This variable is a list of regular expressions.
165 If it is non-nil, headers that match the regular expressions will
166 be placed first in the article buffer in the sequence specified by
167 this list."
168   :type '(repeat regexp)
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
172   "Headers that are only to be displayed if they have interesting data.
173 Possible values in this list are `empty', `newsgroups', `followup-to',
174 `reply-to', `date', `long-to', and `many-to'."
175   :type '(set (const :tag "Headers with no content." empty)
176               (const :tag "Newsgroups with only one group." newsgroups)
177               (const :tag "Followup-to identical to newsgroups." followup-to)
178               (const :tag "Reply-to identical to from." reply-to)
179               (const :tag "Date less than four days old." date)
180               (const :tag "Very long To and/or Cc header." long-to)
181               (const :tag "Multiple To and/or Cc headers." many-to))
182   :group 'gnus-article-hiding)
183
184 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
185   "Regexp matching signature separator.
186 This can also be a list of regexps.  In that case, it will be checked
187 from head to tail looking for a separator.  Searches will be done from
188 the end of the buffer."
189   :type '(repeat string)
190   :group 'gnus-article-signature)
191
192 (defcustom gnus-signature-limit nil
193   "Provide a limit to what is considered a signature.
194 If it is a number, no signature may not be longer (in characters) than
195 that number.  If it is a floating point number, no signature may be
196 longer (in lines) than that number.  If it is a function, the function
197 will be called without any parameters, and if it returns nil, there is
198 no signature in the buffer.  If it is a string, it will be used as a
199 regexp.  If it matches, the text in question is not a signature."
200   :type '(choice (integer :value 200)
201                  (number :value 4.0)
202                  (function :value fun)
203                  (regexp :value ".*"))
204   :group 'gnus-article-signature)
205
206 (defcustom gnus-hidden-properties '(invisible t intangible t)
207   "Property list to use for hiding text."
208   :type 'sexp
209   :group 'gnus-article-hiding)
210
211 (defcustom gnus-article-x-face-command
212   (cond
213    ;; Fixme: This isn't the right thing for mixed graphical and and
214    ;; non-graphical frames in a session.
215    ;; gnus-xmas.el overrides this for XEmacs.
216    ((and (fboundp 'image-type-available-p)
217          (image-type-available-p 'xbm))
218     (if (module-installed-p 'x-face-e21)
219         'x-face-decode-message-header
220       'gnus-article-display-xface))
221    ((and (not (featurep 'xemacs))
222          window-system
223          (module-installed-p 'x-face-mule))
224     'x-face-mule-gnus-article-display-x-face)
225    (gnus-article-compface-xbm
226     "{ echo '/* Width=48, Height=48 */'; uncompface; } | display -")
227    (t
228     "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
229 display -"))
230   "*String or function to be executed to display an X-Face header.
231 If it is a string, the command will be executed in a sub-shell
232 asynchronously.  The compressed face will be piped to this command."
233   :type '(choice string
234                  (function-item
235                   :tag "x-face-decode-message-header (x-face-e21)"
236                   x-face-decode-message-header)
237                  (function-item gnus-article-display-xface)
238                  (function-item x-face-mule-gnus-article-display-x-face)
239                  function)
240   :group 'gnus-article-washing)
241
242 (defcustom gnus-article-x-face-too-ugly nil
243   "Regexp matching posters whose face shouldn't be shown automatically."
244   :type '(choice regexp (const nil))
245   :group 'gnus-article-washing)
246
247 (defcustom gnus-article-banner-alist nil
248   "Banner alist for stripping.
249 For example, 
250      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( eGroups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
251   :version "21.1"
252   :type '(repeat (cons symbol regexp))
253   :group 'gnus-article-washing)
254
255 (defcustom gnus-article-banner-alist nil
256   "Banner alist for stripping.
257 For example, 
258      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( eGroups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
259   :version "21.1"
260   :type '(repeat (cons symbol regexp))
261   :group 'gnus-article-washing)
262
263 (defcustom gnus-emphasis-alist
264   (let ((format
265          "\\(\\s-\\|^\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\s-\\|[-,;:\"]\\s-\\|[?!.]+\\s-\\|\\s)\\)")
266         (types
267          '(("_" "_" underline)
268            ("/" "/" italic)
269            ("\\*" "\\*" bold)
270            ("_/" "/_" underline-italic)
271            ("_\\*" "\\*_" underline-bold)
272            ("\\*/" "/\\*" bold-italic)
273            ("_\\*/" "/\\*_" underline-bold-italic))))
274     `(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
275        2 3 gnus-emphasis-underline)
276       ,@(mapcar
277          (lambda (spec)
278            (list
279             (format format (car spec) (car (cdr spec)))
280             2 3 (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
281          types)))
282   "*Alist that says how to fontify certain phrases.
283 Each item looks like this:
284
285   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
286
287 The first element is a regular expression to be matched.  The second
288 is a number that says what regular expression grouping used to find
289 the entire emphasized word.  The third is a number that says what
290 regexp grouping should be displayed and highlighted.  The fourth
291 is the face used for highlighting."
292   :type '(repeat (list :value ("" 0 0 default)
293                        regexp
294                        (integer :tag "Match group")
295                        (integer :tag "Emphasize group")
296                        face))
297   :group 'gnus-article-emphasis)
298
299 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
300   "A regexp to describe whitespace which should not be emphasized.
301 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
302 The former avoids underlining of leading and trailing whitespace,
303 and the latter avoids underlining any whitespace at all."
304   :version "21.1"
305   :group 'gnus-article-emphasis
306   :type 'regexp)
307
308 (defface gnus-emphasis-bold '((t (:bold t)))
309   "Face used for displaying strong emphasized text (*word*)."
310   :group 'gnus-article-emphasis)
311
312 (defface gnus-emphasis-italic '((t (:italic t)))
313   "Face used for displaying italic emphasized text (/word/)."
314   :group 'gnus-article-emphasis)
315
316 (defface gnus-emphasis-underline '((t (:underline t)))
317   "Face used for displaying underlined emphasized text (_word_)."
318   :group 'gnus-article-emphasis)
319
320 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
321   "Face used for displaying underlined bold emphasized text (_*word*_)."
322   :group 'gnus-article-emphasis)
323
324 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
325   "Face used for displaying underlined italic emphasized text (_/word/_)."
326   :group 'gnus-article-emphasis)
327
328 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
329   "Face used for displaying bold italic emphasized text (/*word*/)."
330   :group 'gnus-article-emphasis)
331
332 (defface gnus-emphasis-underline-bold-italic
333   '((t (:bold t :italic t :underline t)))
334   "Face used for displaying underlined bold italic emphasized text.
335 Esample: (_/*word*/_)."
336   :group 'gnus-article-emphasis)
337
338 (defface gnus-emphasis-highlight-words
339   '((t (:background "black" :foreground "yellow")))
340   "Face used for displaying highlighted words."
341   :group 'gnus-article-emphasis)
342
343 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
344   "Format for display of Date headers in article bodies.
345 See `format-time-string' for the possible values.
346
347 The variable can also be function, which should return a complete Date
348 header.  The function is called with one argument, the time, which can
349 be fed to `format-time-string'."
350   :type '(choice string symbol)
351   :link '(custom-manual "(gnus)Article Date")
352   :group 'gnus-article-washing)
353
354 (eval-and-compile
355   (autoload 'mail-extract-address-components "mail-extr"))
356
357 (defcustom gnus-save-all-headers t
358   "*If non-nil, don't remove any headers before saving."
359   :group 'gnus-article-saving
360   :type 'boolean)
361
362 (defcustom gnus-prompt-before-saving 'always
363   "*This variable says how much prompting is to be done when saving articles.
364 If it is nil, no prompting will be done, and the articles will be
365 saved to the default files.  If this variable is `always', each and
366 every article that is saved will be preceded by a prompt, even when
367 saving large batches of articles.  If this variable is neither nil not
368 `always', there the user will be prompted once for a file name for
369 each invocation of the saving commands."
370   :group 'gnus-article-saving
371   :type '(choice (item always)
372                  (item :tag "never" nil)
373                  (sexp :tag "once" :format "%t\n" :value t)))
374
375 (defcustom gnus-saved-headers gnus-visible-headers
376   "Headers to keep if `gnus-save-all-headers' is nil.
377 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
378 If that variable is nil, however, all headers that match this regexp
379 will be kept while the rest will be deleted before saving."
380   :group 'gnus-article-saving
381   :type 'regexp)
382
383 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
384   "A function to save articles in your favourite format.
385 The function must be interactively callable (in other words, it must
386 be an Emacs command).
387
388 Gnus provides the following functions:
389
390 * gnus-summary-save-in-rmail (Rmail format)
391 * gnus-summary-save-in-mail (Unix mail format)
392 * gnus-summary-save-in-folder (MH folder)
393 * gnus-summary-save-in-file (article format)
394 * gnus-summary-save-in-vm (use VM's folder format)
395 * gnus-summary-write-to-file (article format -- overwrite)."
396   :group 'gnus-article-saving
397   :type '(radio (function-item gnus-summary-save-in-rmail)
398                 (function-item gnus-summary-save-in-mail)
399                 (function-item gnus-summary-save-in-folder)
400                 (function-item gnus-summary-save-in-file)
401                 (function-item gnus-summary-save-in-vm)
402                 (function-item gnus-summary-write-to-file)))
403
404 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
405   "A function generating a file name to save articles in Rmail format.
406 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
407   :group 'gnus-article-saving
408   :type 'function)
409
410 (defcustom gnus-mail-save-name 'gnus-plain-save-name
411   "A function generating a file name to save articles in Unix mail format.
412 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
413   :group 'gnus-article-saving
414   :type 'function)
415
416 (defcustom gnus-folder-save-name 'gnus-folder-save-name
417   "A function generating a file name to save articles in MH folder.
418 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
419   :group 'gnus-article-saving
420   :type 'function)
421
422 (defcustom gnus-file-save-name 'gnus-numeric-save-name
423   "A function generating a file name to save articles in article format.
424 The function is called with NEWSGROUP, HEADERS, and optional
425 LAST-FILE."
426   :group 'gnus-article-saving
427   :type 'function)
428
429 (defcustom gnus-split-methods
430   '((gnus-article-archive-name)
431     (gnus-article-nndoc-name))
432   "*Variable used to suggest where articles are to be saved.
433 For instance, if you would like to save articles related to Gnus in
434 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
435 you could set this variable to something like:
436
437  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
438    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
439
440 This variable is an alist where the where the key is the match and the
441 value is a list of possible files to save in if the match is non-nil.
442
443 If the match is a string, it is used as a regexp match on the
444 article.  If the match is a symbol, that symbol will be funcalled
445 from the buffer of the article to be saved with the newsgroup as the
446 parameter.  If it is a list, it will be evaled in the same buffer.
447
448 If this form or function returns a string, this string will be used as
449 a possible file name; and if it returns a non-nil list, that list will
450 be used as possible file names."
451   :group 'gnus-article-saving
452   :type '(repeat (choice (list :value (fun) function)
453                          (cons :value ("" "") regexp (repeat string))
454                          (sexp :value nil))))
455
456 (defcustom gnus-article-display-method-for-mime
457   'gnus-article-display-mime-message
458   "Function to display a MIME message.
459 The function is called from the article buffer."
460   :group 'gnus-article-mime
461   :type 'function)
462
463 (defcustom gnus-article-display-method-for-traditional
464   'gnus-article-display-traditional-message
465   "*Function to display a traditional message.
466 The function is called from the article buffer."
467   :group 'gnus-article-mime
468   :type 'function)
469
470 (defcustom gnus-page-delimiter "^\^L"
471   "*Regexp describing what to use as article page delimiters.
472 The default value is \"^\^L\", which is a form linefeed at the
473 beginning of a line."
474   :type 'regexp
475   :group 'gnus-article-various)
476
477 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
478   "*The format specification for the article mode line.
479 See `gnus-summary-mode-line-format' for a closer description.
480
481 The following additional specs are available:
482
483 %w  The article washing status.
484 %m  The number of MIME parts in the article."
485   :type 'string
486   :group 'gnus-article-various)
487
488 (defcustom gnus-article-mode-hook nil
489   "*A hook for Gnus article mode."
490   :type 'hook
491   :group 'gnus-article-various)
492
493 (defcustom gnus-article-menu-hook nil
494   "*Hook run after the creation of the article mode menu."
495   :type 'hook
496   :group 'gnus-article-various)
497
498 (defcustom gnus-article-prepare-hook nil
499   "*A hook called after an article has been prepared in the article buffer."
500   :type 'hook
501   :group 'gnus-article-various)
502
503 (defcustom gnus-article-hide-pgp-hook nil
504   "*A hook called after successfully hiding a PGP signature."
505   :type 'hook
506   :group 'gnus-article-various)
507
508 (defcustom gnus-article-button-face 'bold
509   "Face used for highlighting buttons in the article buffer.
510
511 An article button is a piece of text that you can activate by pressing
512 `RET' or `mouse-2' above it."
513   :type 'face
514   :group 'gnus-article-buttons)
515
516 (defcustom gnus-article-mouse-face 'highlight
517   "Face used for mouse highlighting in the article buffer.
518
519 Article buttons will be displayed in this face when the cursor is
520 above them."
521   :type 'face
522   :group 'gnus-article-buttons)
523
524 (defcustom gnus-signature-face 'gnus-signature-face
525   "Face used for highlighting a signature in the article buffer.
526 Obsolete; use the face `gnus-signature-face' for customizations instead."
527   :type 'face
528   :group 'gnus-article-highlight
529   :group 'gnus-article-signature)
530
531 (defface gnus-signature-face
532   '((t
533      (:italic t)))
534   "Face used for highlighting a signature in the article buffer."
535   :group 'gnus-article-highlight
536   :group 'gnus-article-signature)
537
538 (defface gnus-header-from-face
539   '((((class color)
540       (background dark))
541      (:foreground "spring green"))
542     (((class color)
543       (background light))
544      (:foreground "red3"))
545     (t
546      (:italic t)))
547   "Face used for displaying from headers."
548   :group 'gnus-article-headers
549   :group 'gnus-article-highlight)
550
551 (defface gnus-header-subject-face
552   '((((class color)
553       (background dark))
554      (:foreground "SeaGreen3"))
555     (((class color)
556       (background light))
557      (:foreground "red4"))
558     (t
559      (:bold t :italic t)))
560   "Face used for displaying subject headers."
561   :group 'gnus-article-headers
562   :group 'gnus-article-highlight)
563
564 (defface gnus-header-newsgroups-face
565   '((((class color)
566       (background dark))
567      (:foreground "yellow" :italic t))
568     (((class color)
569       (background light))
570      (:foreground "MidnightBlue" :italic t))
571     (t
572      (:italic t)))
573   "Face used for displaying newsgroups headers."
574   :group 'gnus-article-headers
575   :group 'gnus-article-highlight)
576
577 (defface gnus-header-name-face
578   '((((class color)
579       (background dark))
580      (:foreground "SeaGreen"))
581     (((class color)
582       (background light))
583      (:foreground "maroon"))
584     (t
585      (:bold t)))
586   "Face used for displaying header names."
587   :group 'gnus-article-headers
588   :group 'gnus-article-highlight)
589
590 (defface gnus-header-content-face
591   '((((class color)
592       (background dark))
593      (:foreground "forest green" :italic t))
594     (((class color)
595       (background light))
596      (:foreground "indianred4" :italic t))
597     (t
598      (:italic t)))  "Face used for displaying header content."
599   :group 'gnus-article-headers
600   :group 'gnus-article-highlight)
601
602 (defcustom gnus-header-face-alist
603   '(("From" nil gnus-header-from-face)
604     ("Subject" nil gnus-header-subject-face)
605     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
606     ("" gnus-header-name-face gnus-header-content-face))
607   "*Controls highlighting of article header.
608
609 An alist of the form (HEADER NAME CONTENT).
610
611 HEADER is a regular expression which should match the name of an
612 header header and NAME and CONTENT are either face names or nil.
613
614 The name of each header field will be displayed using the face
615 specified by the first element in the list where HEADER match the
616 header name and NAME is non-nil.  Similarly, the content will be
617 displayed by the first non-nil matching CONTENT face."
618   :group 'gnus-article-headers
619   :group 'gnus-article-highlight
620   :type '(repeat (list (regexp :tag "Header")
621                        (choice :tag "Name"
622                                (item :tag "skip" nil)
623                                (face :value default))
624                        (choice :tag "Content"
625                                (item :tag "skip" nil)
626                                (face :value default)))))
627
628 (defcustom gnus-article-decode-hook nil
629   "*Hook run to decode charsets in articles."
630   :group 'gnus-article-headers
631   :type 'hook)
632
633 (defcustom gnus-display-mime-function 'gnus-display-mime
634   "Function to display MIME articles."
635   :group 'gnus-article-mime
636   :type 'function)
637
638 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
639   "Function used to decode headers.")
640
641 (defvar gnus-article-dumbquotes-map
642   '(("\200" "EUR")
643     ("\202" ",")
644     ("\203" "f")
645     ("\204" ",,")
646     ("\205" "...")
647     ("\213" "<")
648     ("\214" "OE")
649     ("\221" "`")
650     ("\222" "'")
651     ("\223" "``")
652     ("\224" "\"")
653     ("\225" "*")
654     ("\226" "-")
655     ("\227" "--")
656     ("\231" "(TM)")
657     ("\233" ">")
658     ("\234" "oe")
659     ("\264" "'"))
660   "Table for MS-to-Latin1 translation.")
661
662 (defcustom gnus-ignored-mime-types nil
663   "List of MIME types that should be ignored by Gnus."
664   :version "21.1"
665   :group 'gnus-article-mime
666   :type '(repeat regexp))
667
668 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
669   "List of MIME types that should not be given buttons when rendered inline."
670   :version "21.1"
671   :group 'gnus-article-mime
672   :type '(repeat regexp))
673
674 (defcustom gnus-article-mime-part-function nil
675   "Function called with a MIME handle as the argument.
676 This is meant for people who want to do something automatic based
677 on parts -- for instance, adding Vcard info to a database."
678   :group 'gnus-article-mime
679   :type 'function)
680
681 (defcustom gnus-mime-multipart-functions nil
682   "An alist of MIME types to functions to display them."
683   :version "21.1"
684   :group 'gnus-article-mime
685   :type 'alist)
686
687 (defcustom gnus-article-date-lapsed-new-header nil
688   "Whether the X-Sent and Date headers can coexist.
689 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
690 either replace the old \"Date:\" header (if this variable is nil), or
691 be added below it (otherwise)."
692   :version "21.1"
693   :group 'gnus-article-headers
694   :type 'boolean)
695
696 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
697   "Function called with a MIME handle as the argument.
698 This is meant for people who want to view first matched part.
699 For `undisplayed-alternative' (default), the first undisplayed 
700 part or alternative part is used.  For `undisplayed', the first 
701 undisplayed part is used.  For a function, the first part which 
702 the function return `t' is used.  For `nil', the first part is
703 used."
704   :version "21.1"
705   :group 'gnus-article-mime
706   :type '(choice 
707           (item :tag "first" :value nil)
708           (item :tag "undisplayed" :value undisplayed)
709           (item :tag "undisplayed or alternative" 
710                 :value undisplayed-alternative)
711           (function)))
712
713 (defcustom gnus-mime-action-alist
714   '(("save to file" . gnus-mime-save-part)
715     ("save and strip" . gnus-mime-save-part-and-strip)
716     ("display as text" . gnus-mime-inline-part)
717     ("view the part" . gnus-mime-view-part)
718     ("pipe to command" . gnus-mime-pipe-part)
719     ("toggle display" . gnus-article-press-button)
720     ("toggle display" . gnus-article-view-part-as-charset)
721     ("view as type" . gnus-mime-view-part-as-type)
722     ("internalize type" . gnus-mime-internalize-part)
723     ("externalize type" . gnus-mime-externalize-part))
724   "An alist of actions that run on the MIME attachment."
725   :group 'gnus-article-mime
726   :type '(repeat (cons (string :tag "name")
727                        (function))))
728
729 (defcustom gnus-mime-action-alist
730   '(("save to file" . gnus-mime-save-part)
731     ("display as text" . gnus-mime-inline-part)
732     ("view the part" . gnus-mime-view-part)
733     ("pipe to command" . gnus-mime-pipe-part)
734     ("toggle display" . gnus-article-press-button)
735     ("view as type" . gnus-mime-view-part-as-type)
736     ("internalize type" . gnus-mime-internalize-part)
737     ("externalize type" . gnus-mime-externalize-part))
738   "An alist of actions that run on the MIME attachment."
739   :version "21.1"
740   :group 'gnus-article-mime
741   :type '(repeat (cons (string :tag "name")
742                        (function))))
743
744 ;;;
745 ;;; The treatment variables
746 ;;;
747
748 (defvar gnus-part-display-hook nil
749   "Hook called on parts that are to receive treatment.")
750
751 (defvar gnus-article-treat-custom
752   '(choice (const :tag "Off" nil)
753            (const :tag "On" t)
754            (const :tag "Header" head)
755            (const :tag "Last" last)
756            (const :tag "Mime" mime)
757            (integer :tag "Less")
758            (repeat :tag "Groups" regexp)
759            (sexp :tag "Predicate")))
760
761 (defvar gnus-article-treat-head-custom
762   '(choice (const :tag "Off" nil)
763            (const :tag "Header" head)))
764
765 (defvar gnus-article-treat-types '("text/plain")
766   "Parts to treat.")
767
768 (defvar gnus-inhibit-treatment nil
769   "Whether to inhibit treatment.")
770
771 (defcustom gnus-treat-highlight-signature '(or last (typep "text/x-vcard"))
772   "Highlight the signature.
773 Valid values are nil, t, `head', `last', an integer or a predicate.
774 See the manual for details."
775   :group 'gnus-article-treat
776   :type gnus-article-treat-custom)
777 (put 'gnus-treat-highlight-signature 'highlight t)
778
779 (defcustom gnus-treat-buttonize 100000
780   "Add buttons.
781 Valid values are nil, t, `head', `last', an integer or a predicate.
782 See the manual for details."
783   :group 'gnus-article-treat
784   :type gnus-article-treat-custom)
785 (put 'gnus-treat-buttonize 'highlight t)
786
787 (defcustom gnus-treat-buttonize-head 'head
788   "Add buttons to the head.
789 Valid values are nil, t, `head', `last', an integer or a predicate.
790 See the manual for details."
791   :group 'gnus-article-treat
792   :type gnus-article-treat-head-custom)
793 (put 'gnus-treat-buttonize-head 'highlight t)
794
795 (defcustom gnus-treat-emphasize nil
796   "Emphasize text.
797 Valid values are nil, t, `head', `last', an integer or a predicate.
798 See the manual for details."
799   :group 'gnus-article-treat
800   :type gnus-article-treat-custom)
801 (put 'gnus-treat-emphasize 'highlight t)
802
803 (defcustom gnus-treat-strip-cr nil
804   "Remove carriage returns.
805 Valid values are nil, t, `head', `last', an integer or a predicate.
806 See the manual for details."
807   :group 'gnus-article-treat
808   :type gnus-article-treat-custom)
809
810 (defcustom gnus-treat-hide-headers 'head
811   "Hide headers.
812 Valid values are nil, t, `head', `last', an integer or a predicate.
813 See the manual for details."
814   :group 'gnus-article-treat
815   :type gnus-article-treat-head-custom)
816
817 (defcustom gnus-treat-hide-boring-headers nil
818   "Hide boring headers.
819 Valid values are nil, t, `head', `last', an integer or a predicate.
820 See the manual for details."
821   :group 'gnus-article-treat
822   :type gnus-article-treat-head-custom)
823
824 (defcustom gnus-treat-hide-signature nil
825   "Hide the signature.
826 Valid values are nil, t, `head', `last', an integer or a predicate.
827 See the manual for details."
828   :group 'gnus-article-treat
829   :type gnus-article-treat-custom)
830
831 (defcustom gnus-treat-fill-article nil
832   "Fill the article.
833 Valid values are nil, t, `head', `last', an integer or a predicate.
834 See the manual for details."
835   :group 'gnus-article-treat
836   :type gnus-article-treat-custom)
837
838 (defcustom gnus-treat-hide-citation nil
839   "Hide cited text.
840 Valid values are nil, t, `head', `last', an integer or a predicate.
841 See the manual for details."
842   :group 'gnus-article-treat
843   :type gnus-article-treat-custom)
844
845 (defcustom gnus-treat-hide-citation-maybe nil
846   "Hide cited text.
847 Valid values are nil, t, `head', `last', an integer or a predicate.
848 See the manual for details."
849   :group 'gnus-article-treat
850   :type gnus-article-treat-custom)
851
852 (defcustom gnus-treat-hide-citation-maybe nil
853   "Hide cited text.
854 Valid values are nil, t, `head', `last', an integer or a predicate.
855 See the manual for details."
856   :group 'gnus-article-treat
857   :type gnus-article-treat-custom)
858
859 (defcustom gnus-treat-strip-list-identifiers 'head
860   "Strip list identifiers from `gnus-list-identifiers`.
861 Valid values are nil, t, `head', `last', an integer or a predicate.
862 See the manual for details."
863   :version "21.1"
864   :group 'gnus-article-treat
865   :type gnus-article-treat-custom)
866
867 (defcustom gnus-treat-strip-pgp t
868   "Strip PGP signatures.
869 Valid values are nil, t, `head', `last', an integer or a predicate.
870 See the manual for details."
871   :group 'gnus-article-treat
872   :type gnus-article-treat-custom)
873
874 (defcustom gnus-treat-strip-pem nil
875   "Strip PEM signatures.
876 Valid values are nil, t, `head', `last', an integer or a predicate.
877 See the manual for details."
878   :group 'gnus-article-treat
879   :type gnus-article-treat-custom)
880
881 (defcustom gnus-treat-strip-banner t
882   "Strip banners from articles.
883 The banner to be stripped is specified in the `banner' group parameter.
884 Valid values are nil, t, `head', `last', an integer or a predicate.
885 See the manual for details."
886   :group 'gnus-article-treat
887   :type gnus-article-treat-custom)
888
889 (defcustom gnus-treat-highlight-headers 'head
890   "Highlight the headers.
891 Valid values are nil, t, `head', `last', an integer or a predicate.
892 See the manual for details."
893   :group 'gnus-article-treat
894   :type gnus-article-treat-head-custom)
895 (put 'gnus-treat-highlight-headers 'highlight t)
896
897 (defcustom gnus-treat-highlight-citation t
898   "Highlight cited text.
899 Valid values are nil, t, `head', `last', an integer or a predicate.
900 See the manual for details."
901   :group 'gnus-article-treat
902   :type gnus-article-treat-custom)
903 (put 'gnus-treat-highlight-citation 'highlight t)
904
905 (defcustom gnus-treat-date-ut nil
906   "Display the Date in UT (GMT).
907 Valid values are nil, t, `head', `last', an integer or a predicate.
908 See the manual for details."
909   :group 'gnus-article-treat
910   :type gnus-article-treat-head-custom)
911
912 (defcustom gnus-treat-date-local nil
913   "Display the Date in the local timezone.
914 Valid values are nil, t, `head', `last', an integer or a predicate.
915 See the manual for details."
916   :group 'gnus-article-treat
917   :type gnus-article-treat-head-custom)
918
919 (defcustom gnus-treat-date-lapsed nil
920   "Display the Date header in a way that says how much time has elapsed.
921 Valid values are nil, t, `head', `last', an integer or a predicate.
922 See the manual for details."
923   :group 'gnus-article-treat
924   :type gnus-article-treat-head-custom)
925
926 (defcustom gnus-treat-date-original nil
927   "Display the date in the original timezone.
928 Valid values are nil, t, `head', `last', an integer or a predicate.
929 See the manual for details."
930   :group 'gnus-article-treat
931   :type gnus-article-treat-head-custom)
932
933 (defcustom gnus-treat-date-iso8601 nil
934   "Display the date in the ISO8601 format.
935 Valid values are nil, t, `head', `last', an integer or a predicate.
936 See the manual for details."
937   :version "21.1"
938   :group 'gnus-article-treat
939   :type gnus-article-treat-head-custom)
940
941 (defcustom gnus-treat-date-user-defined nil
942   "Display the date in a user-defined format.
943 The format is defined by the `gnus-article-time-format' variable.
944 Valid values are nil, t, `head', `last', an integer or a predicate.
945 See the manual for details."
946   :group 'gnus-article-treat
947   :type gnus-article-treat-head-custom)
948
949 (defcustom gnus-treat-strip-headers-in-body t
950   "Strip the X-No-Archive header line from the beginning of the body.
951 Valid values are nil, t, `head', `last', an integer or a predicate.
952 See the manual for details."
953   :version "21.1"
954   :group 'gnus-article-treat
955   :type gnus-article-treat-custom)
956
957 (defcustom gnus-treat-strip-trailing-blank-lines nil
958   "Strip trailing blank lines.
959 Valid values are nil, t, `head', `last', an integer or a predicate.
960 See the manual for details."
961   :group 'gnus-article-treat
962   :type gnus-article-treat-custom)
963
964 (defcustom gnus-treat-strip-leading-blank-lines nil
965   "Strip leading blank lines.
966 Valid values are nil, t, `head', `last', an integer or a predicate.
967 See the manual for details."
968   :group 'gnus-article-treat
969   :type gnus-article-treat-custom)
970
971 (defcustom gnus-treat-strip-multiple-blank-lines nil
972   "Strip multiple blank lines.
973 Valid values are nil, t, `head', `last', an integer or a predicate.
974 See the manual for details."
975   :group 'gnus-article-treat
976   :type gnus-article-treat-custom)
977
978 (defcustom gnus-treat-overstrike t
979   "Treat overstrike highlighting.
980 Valid values are nil, t, `head', `last', an integer or a predicate.
981 See the manual for details."
982   :group 'gnus-article-treat
983   :type gnus-article-treat-custom)
984 (put 'gnus-treat-overstrike 'highlight t)
985
986 (defcustom gnus-treat-display-xface
987   (and (or (and (fboundp 'image-type-available-p)
988                 (image-type-available-p 'xbm)
989                 (string-match "^0x" (shell-command-to-string "uncompface")))
990            (and (featurep 'xemacs) (featurep 'xface))
991            (eq 'x-face-mule-gnus-article-display-x-face
992                gnus-article-x-face-command))
993        'head)
994   "Display X-Face headers.
995 Valid values are nil, t, `head', `last', an integer or a predicate.
996 See the manual for details."
997   :group 'gnus-article-treat
998   :type gnus-article-treat-head-custom)
999 (put 'gnus-treat-display-xface 'highlight t)
1000
1001 (defcustom gnus-treat-display-smileys
1002   (if (or (and (featurep 'xemacs)
1003                (featurep 'xpm))
1004           (and (fboundp 'image-type-available-p)
1005                (image-type-available-p 'pbm))
1006           (and (not (featurep 'xemacs))
1007                window-system
1008                (module-installed-p 'gnus-bitmap)))
1009       t
1010     nil)
1011   "Display smileys.
1012 Valid values are nil, t, `head', `last', an integer or a predicate.
1013 See the manual for details."
1014   :group 'gnus-article-treat
1015   :type gnus-article-treat-custom)
1016 (put 'gnus-treat-display-smileys 'highlight t)
1017
1018 (defcustom gnus-treat-display-picons (if (featurep 'xemacs) 'head nil)
1019   "Display picons.
1020 Valid values are nil, t, `head', `last', an integer or a predicate.
1021 See the manual for details."
1022   :group 'gnus-article-treat
1023   :type gnus-article-treat-head-custom)
1024 (put 'gnus-treat-display-picons 'highlight t)
1025
1026 (defcustom gnus-treat-capitalize-sentences nil
1027   "Capitalize sentence-starting words.
1028 Valid values are nil, t, `head', `last', an integer or a predicate.
1029 See the manual for details."
1030   :version "21.1"
1031   :group 'gnus-article-treat
1032   :type gnus-article-treat-custom)
1033
1034 (defcustom gnus-treat-fill-long-lines nil
1035   "Fill long lines.
1036 Valid values are nil, t, `head', `last', an integer or a predicate.
1037 See the manual for details."
1038   :group 'gnus-article-treat
1039   :type gnus-article-treat-custom)
1040
1041 (defcustom gnus-treat-play-sounds nil
1042   "Play sounds.
1043 Valid values are nil, t, `head', `last', an integer or a predicate.
1044 See the manual for details."
1045   :version "21.1"
1046   :group 'gnus-article-treat
1047   :type gnus-article-treat-custom)
1048
1049 (defcustom gnus-treat-decode-article-as-default-mime-charset nil
1050   "Decode an article as `default-mime-charset'.  For instance, if you want to
1051 attempt to decode an article even if the value of `gnus-show-mime' is nil,
1052 you could set this variable to something like: nil for don't decode, t for
1053 decode the body, '(or header t) for the whole article, etc."
1054   :group 'gnus-article-treat
1055   :type '(radio (const :tag "Off" nil)
1056                 (const :tag "Decode body" t)
1057                 (const :tag "Decode all" (or head t))))
1058
1059 (defcustom gnus-treat-translate nil
1060   "Translate articles from one language to another.
1061 Valid values are nil, t, `head', `last', an integer or a predicate.
1062 See the manual for details."
1063   :version "21.1"
1064   :group 'gnus-article-treat
1065   :type gnus-article-treat-custom)
1066
1067 (defcustom gnus-treat-x-pgp-sig nil
1068   "Verify X-PGP-Sig.
1069 To automatically treat X-PGP-Sig, set it to head.
1070 Valid values are nil, t, `head', `last', an integer or a predicate.
1071 See the manual for details."
1072   :group 'gnus-article-treat
1073   :group 'mime-security
1074   :type gnus-article-treat-custom)
1075
1076 (defvar gnus-article-encrypt-protocol-alist
1077   '(("PGP" . mml2015-self-encrypt)))
1078
1079 ;; Set to nil if more than one protocol added to
1080 ;; gnus-article-encrypt-protocol-alist.
1081 (defcustom gnus-article-encrypt-protocol "PGP"
1082   "The protocol used for encrypt articles.
1083 It is a string, such as \"PGP\". If nil, ask user."
1084   :type 'string
1085   :group 'mime-security)
1086
1087 ;;; Internal variables
1088
1089 (defvar article-goto-body-goes-to-point-min-p nil)
1090 (defvar gnus-article-wash-types nil)
1091 (defvar gnus-article-emphasis-alist nil)
1092
1093 (defvar gnus-article-mime-handle-alist-1 nil)
1094 (defvar gnus-treatment-function-alist
1095   '((gnus-treat-decode-article-as-default-mime-charset
1096      gnus-article-decode-article-as-default-mime-charset)
1097     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1098     (gnus-treat-strip-banner gnus-article-strip-banner)
1099     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1100     (gnus-treat-buttonize gnus-article-add-buttons)
1101     (gnus-treat-fill-article gnus-article-fill-cited-article)
1102     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1103     (gnus-treat-strip-cr gnus-article-remove-cr)
1104     (gnus-treat-display-xface gnus-article-display-x-face)
1105     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1106     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1107     (gnus-treat-hide-signature gnus-article-hide-signature)
1108     (gnus-treat-hide-citation gnus-article-hide-citation)
1109     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1110     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1111     (gnus-treat-strip-pgp gnus-article-hide-pgp)
1112     (gnus-treat-strip-pem gnus-article-hide-pem)
1113     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1114     (gnus-treat-emphasize gnus-article-emphasize)
1115     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1116     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1117     (gnus-treat-date-ut gnus-article-date-ut)
1118     (gnus-treat-date-local gnus-article-date-local)
1119     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1120     (gnus-treat-date-original gnus-article-date-original)
1121     (gnus-treat-date-user-defined gnus-article-date-user)
1122     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1123     (gnus-treat-strip-trailing-blank-lines
1124      gnus-article-remove-trailing-blank-lines)
1125     (gnus-treat-strip-leading-blank-lines
1126      gnus-article-strip-leading-blank-lines)
1127     (gnus-treat-strip-multiple-blank-lines
1128      gnus-article-strip-multiple-blank-lines)
1129     (gnus-treat-overstrike gnus-article-treat-overstrike)
1130     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1131     (gnus-treat-display-smileys gnus-article-smiley-display)
1132     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1133     (gnus-treat-display-picons gnus-article-display-picons)
1134     (gnus-treat-play-sounds gnus-earcon-display)))
1135
1136 (defvar gnus-article-mime-handle-alist nil)
1137 (defvar article-lapsed-timer nil)
1138 (defvar gnus-article-current-summary nil)
1139
1140 (defvar gnus-article-mode-syntax-table
1141   (let ((table (copy-syntax-table text-mode-syntax-table)))
1142     ;; This causes the citation match run O(2^n).
1143     ;; (modify-syntax-entry ?- "w" table) 
1144     (modify-syntax-entry ?> ")" table)
1145     (modify-syntax-entry ?< "(" table)
1146     table)
1147   "Syntax table used in article mode buffers.
1148 Initialized from `text-mode-syntax-table.")
1149
1150 (defvar gnus-save-article-buffer nil)
1151
1152 (defvar gnus-article-mode-line-format-alist
1153   (nconc '((?w (gnus-article-wash-status) ?s)
1154            (?m (gnus-article-mime-part-status) ?s))
1155          gnus-summary-mode-line-format-alist))
1156
1157 (defvar gnus-number-of-articles-to-be-saved nil)
1158
1159 (defvar gnus-inhibit-hiding nil)
1160
1161 (defsubst gnus-article-hide-text (b e props)
1162   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1163   (gnus-add-text-properties-when 'article-type nil b e props)
1164   (when (memq 'intangible props)
1165     (put-text-property
1166      (max (1- b) (point-min))
1167      b 'intangible (cddr (memq 'intangible props)))))
1168
1169 (defsubst gnus-article-unhide-text (b e)
1170   "Remove hidden text properties from region between B and E."
1171   (remove-text-properties b e gnus-hidden-properties)
1172   (when (memq 'intangible gnus-hidden-properties)
1173     (put-text-property (max (1- b) (point-min))
1174                        b 'intangible nil)))
1175
1176 (defun gnus-article-hide-text-type (b e type)
1177   "Hide text of TYPE between B and E."
1178   (push type gnus-article-wash-types)
1179   (gnus-article-hide-text
1180    b e (cons 'article-type (cons type gnus-hidden-properties))))
1181
1182 (defun gnus-article-unhide-text-type (b e type)
1183   "Unhide text of TYPE between B and E."
1184   (setq gnus-article-wash-types
1185         (delq type gnus-article-wash-types))
1186   (remove-text-properties
1187    b e (cons 'article-type (cons type gnus-hidden-properties)))
1188   (when (memq 'intangible gnus-hidden-properties)
1189     (put-text-property (max (1- b) (point-min))
1190                        b 'intangible nil)))
1191
1192 (defun gnus-article-hide-text-of-type (type)
1193   "Hide text of TYPE in the current buffer."
1194   (save-excursion
1195     (let ((b (point-min))
1196           (e (point-max)))
1197       (while (setq b (text-property-any b e 'article-type type))
1198         (add-text-properties b (incf b) gnus-hidden-properties)))))
1199
1200 (defun gnus-article-delete-text-of-type (type)
1201   "Delete text of TYPE in the current buffer."
1202   (save-excursion
1203     (let ((b (point-min)))
1204       (while (setq b (text-property-any b (point-max) 'article-type type))
1205         (delete-region
1206          b (or (text-property-not-all b (point-max) 'article-type type)
1207                (point-max)))))))
1208
1209 (defun gnus-article-delete-invisible-text ()
1210   "Delete all invisible text in the current buffer."
1211   (save-excursion
1212     (let ((b (point-min)))
1213       (while (setq b (text-property-any b (point-max) 'invisible t))
1214         (delete-region
1215          b (or (text-property-not-all b (point-max) 'invisible t)
1216                (point-max)))))))
1217
1218 (defun gnus-article-text-type-exists-p (type)
1219   "Say whether any text of type TYPE exists in the buffer."
1220   (text-property-any (point-min) (point-max) 'article-type type))
1221
1222 (defsubst gnus-article-header-rank ()
1223   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1224   (let ((list gnus-sorted-header-list)
1225         (i 0))
1226     (while list
1227       (when (looking-at (car list))
1228         (setq list nil))
1229       (setq list (cdr list))
1230       (incf i))
1231     i))
1232
1233 (defun article-hide-headers (&optional arg delete)
1234   "Hide unwanted headers and possibly sort them as well."
1235   (interactive (gnus-article-hidden-arg))
1236   ;; Lars said that this function might be inhibited.
1237   (if (gnus-article-check-hidden-text 'headers arg)
1238       (progn
1239         ;; Show boring headers as well.
1240         (gnus-article-show-hidden-text 'boring-headers)
1241         (when (eq 1 (point-min))
1242           (set-window-start (get-buffer-window (current-buffer)) 1)))
1243   (unless gnus-inhibit-hiding
1244     (save-excursion
1245       (save-restriction
1246         (let ((buffer-read-only nil)
1247               (inhibit-read-only t)
1248               (case-fold-search t)
1249               (max (1+ (length gnus-sorted-header-list)))
1250               (ignored (when (not gnus-visible-headers)
1251                          (cond ((stringp gnus-ignored-headers)
1252                                 gnus-ignored-headers)
1253                                ((listp gnus-ignored-headers)
1254                                 (mapconcat 'identity gnus-ignored-headers
1255                                            "\\|")))))
1256               (visible
1257                (cond ((stringp gnus-visible-headers)
1258                       gnus-visible-headers)
1259                      ((and gnus-visible-headers
1260                            (listp gnus-visible-headers))
1261                       (mapconcat 'identity gnus-visible-headers "\\|"))))
1262               (inhibit-point-motion-hooks t)
1263               beg)
1264           ;; First we narrow to just the headers.
1265           (article-narrow-to-head)
1266           ;; Hide any "From " lines at the beginning of (mail) articles.
1267           (while (looking-at "From ")
1268             (forward-line 1))
1269           (unless (bobp)
1270             (if delete
1271                 (delete-region (point-min) (point))
1272               (gnus-article-hide-text (point-min) (point)
1273                                       (nconc (list 'article-type 'headers)
1274                                              gnus-hidden-properties))))
1275           ;; Then treat the rest of the header lines.
1276           ;; Then we use the two regular expressions
1277           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1278           ;; select which header lines is to remain visible in the
1279           ;; article buffer.
1280           (while (re-search-forward "^[^ \t]*:" nil t)
1281             (beginning-of-line)
1282             ;; Mark the rank of the header.
1283             (put-text-property
1284              (point) (1+ (point)) 'message-rank
1285              (if (or (and visible (looking-at visible))
1286                      (and ignored
1287                           (not (looking-at ignored))))
1288                  (gnus-article-header-rank)
1289                (+ 2 max)))
1290             (forward-line 1))
1291           (message-sort-headers-1)
1292           (when (setq beg (text-property-any
1293                            (point-min) (point-max) 'message-rank (+ 2 max)))
1294             ;; We delete or make invisible the unwanted headers.
1295             (push 'headers gnus-article-wash-types)
1296             (if delete
1297                 (progn
1298                   (add-text-properties
1299                    (point-min) (+ 5 (point-min))
1300                    '(article-type headers dummy-invisible t))
1301                   (delete-region beg (point-max)))
1302               (gnus-article-hide-text-type beg (point-max) 'headers))))))))
1303   )
1304
1305 (defun article-hide-boring-headers (&optional arg)
1306   "Toggle hiding of headers that aren't very interesting.
1307 If given a negative prefix, always show; if given a positive prefix,
1308 always hide."
1309   (interactive (gnus-article-hidden-arg))
1310   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1311              (not gnus-show-all-headers))
1312     (save-excursion
1313       (save-restriction
1314         (let ((buffer-read-only nil)
1315               (list gnus-boring-article-headers)
1316               (inhibit-point-motion-hooks t)
1317               elem)
1318           (article-narrow-to-head)
1319           (while list
1320             (setq elem (pop list))
1321             (goto-char (point-min))
1322             (cond
1323              ;; Hide empty headers.
1324              ((eq elem 'empty)
1325               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1326                 (forward-line -1)
1327                 (gnus-article-hide-text-type
1328                  (progn (beginning-of-line) (point))
1329                  (progn
1330                    (end-of-line)
1331                    (if (re-search-forward "^[^ \t]" nil t)
1332                        (match-beginning 0)
1333                      (point-max)))
1334                  'boring-headers)))
1335              ;; Hide boring Newsgroups header.
1336              ((eq elem 'newsgroups)
1337               (when (equal (gnus-fetch-field "newsgroups")
1338                            (gnus-group-real-name
1339                             (if (boundp 'gnus-newsgroup-name)
1340                                 gnus-newsgroup-name
1341                               "")))
1342                 (gnus-article-hide-header "newsgroups")))
1343              ((eq elem 'followup-to)
1344               (when (equal (message-fetch-field "followup-to")
1345                            (message-fetch-field "newsgroups"))
1346                 (gnus-article-hide-header "followup-to")))
1347              ((eq elem 'reply-to)
1348               (let ((from (message-fetch-field "from"))
1349                     (reply-to (message-fetch-field "reply-to")))
1350                 (when (and
1351                        from reply-to
1352                        (ignore-errors
1353                          (equal
1354                           (nth 1 (funcall gnus-extract-address-components from))
1355                           (nth 1 (funcall gnus-extract-address-components reply-to)))))
1356                   (gnus-article-hide-header "reply-to"))))
1357              ((eq elem 'date)
1358               (let ((date (message-fetch-field "date")))
1359                 (when (and date
1360                            (< (days-between (current-time-string) date)
1361                               4))
1362                   (gnus-article-hide-header "date"))))
1363              ((eq elem 'long-to)
1364               (let ((to (message-fetch-field "to"))
1365                     (cc (message-fetch-field "cc")))
1366                 (when (> (length to) 1024)
1367                   (gnus-article-hide-header "to"))
1368                 (when (> (length cc) 1024)
1369                   (gnus-article-hide-header "cc"))))
1370              ((eq elem 'many-to)
1371               (let ((to-count 0)
1372                     (cc-count 0))
1373                 (goto-char (point-min))
1374                 (while (re-search-forward "^to:" nil t)
1375                   (setq to-count (1+ to-count)))
1376                 (when (> to-count 1)
1377                   (while (> to-count 0)
1378                     (goto-char (point-min))
1379                     (save-restriction
1380                       (re-search-forward "^to:" nil nil to-count)
1381                       (forward-line -1)
1382                       (narrow-to-region (point) (point-max))
1383                       (gnus-article-hide-header "to"))
1384                     (setq to-count (1- to-count))))
1385                 (goto-char (point-min))
1386                 (while (re-search-forward "^cc:" nil t)
1387                   (setq cc-count (1+ cc-count)))
1388                 (when (> cc-count 1)
1389                   (while (> cc-count 0)
1390                     (goto-char (point-min))
1391                     (save-restriction
1392                       (re-search-forward "^cc:" nil nil cc-count)
1393                       (forward-line -1)
1394                       (narrow-to-region (point) (point-max))
1395                       (gnus-article-hide-header "cc"))
1396                     (setq cc-count (1- cc-count)))))))))))))
1397
1398 (defun gnus-article-hide-header (header)
1399   (save-excursion
1400     (goto-char (point-min))
1401     (when (re-search-forward (concat "^" header ":") nil t)
1402       (gnus-article-hide-text-type
1403        (progn (beginning-of-line) (point))
1404        (progn
1405          (end-of-line)
1406          (if (re-search-forward "^[^ \t]" nil t)
1407              (match-beginning 0)
1408            (point-max)))
1409        'boring-headers))))
1410
1411 (defun article-toggle-headers (&optional arg)
1412   "Toggle hiding of headers.  If given a negative prefix, always show;
1413 if given a positive prefix, always hide."
1414   (interactive (gnus-article-hidden-arg))
1415   (let ((force (when (numberp arg)
1416                  (cond ((> arg 0) 'always-hide)
1417                        ((< arg 0) 'always-show))))
1418         (window (get-buffer-window gnus-article-buffer))
1419         (header-end (point-min))
1420         header-start field-end field-start
1421         (inhibit-point-motion-hooks t)
1422         (inhibit-read-only t)
1423         buffer-read-only)
1424     (save-restriction
1425       (widen)
1426       (while (and (setq header-start
1427                         (text-property-any header-end (point-max)
1428                                            'article-treated-header t))
1429                   (setq header-end
1430                         (text-property-not-all header-start (point-max)
1431                                                'article-treated-header t)))
1432         (setq field-end header-start)
1433         (cond
1434          (;; Hide exposed invisible fields.
1435           (and (not (eq 'always-show force))
1436                (setq field-start
1437                      (text-property-any field-end header-end
1438                                         'exposed-invisible-field t)))
1439           (while (and field-start
1440                       (setq field-end (text-property-not-all
1441                                        field-start header-end
1442                                        'exposed-invisible-field t)))
1443             (add-text-properties field-start field-end gnus-hidden-properties)
1444             (setq field-start (text-property-any field-end header-end
1445                                                  'exposed-invisible-field t)))
1446           (put-text-property header-start header-end
1447                              'exposed-invisible-field nil))
1448          (;; Expose invisible fields.
1449           (and (not (eq 'always-hide force))
1450                (setq field-start
1451                      (text-property-any field-end header-end 'invisible t)))
1452           (while (and field-start
1453                       (setq field-end (text-property-not-all
1454                                        field-start header-end
1455                                        'invisible t)))
1456             ;; If the invisible text is not terminated with newline, we
1457             ;; won't expose it.  Because it may be created by x-face-mule.
1458             ;; BTW, XEmacs sometimes fail in putting a invisible text
1459             ;; property with `gnus-article-hide-text' (really?).  In that
1460             ;; case, the invisible text might be started from the middle of
1461             ;; a line so we will expose the sort of thing.
1462             (when (or (not (or (eq header-start field-start)
1463                                (eq ?\n (char-before field-start))))
1464                       (eq ?\n (char-before field-end)))
1465               (remove-text-properties field-start field-end
1466                                       gnus-hidden-properties)
1467               (put-text-property field-start field-end
1468                                  'exposed-invisible-field t))
1469             (setq field-start (text-property-any field-end header-end
1470                                                  'invisible t))))
1471          (;; Hide fields.
1472           (not (eq 'always-show force))
1473           (narrow-to-region header-start header-end)
1474           (article-hide-headers)
1475           ;; Re-display X-Face image under XEmacs.
1476           (when (and (featurep 'xemacs)
1477                      (gnus-functionp gnus-article-x-face-command))
1478             (let ((func (cadr (assq 'gnus-treat-display-xface
1479                                     gnus-treatment-function-alist)))
1480                   (condition 'head))
1481               (when (and (not gnus-inhibit-treatment)
1482                          func
1483                          (gnus-treat-predicate gnus-treat-display-xface))
1484                 (funcall func)
1485                 (put-text-property header-start header-end 'read-only nil))))
1486           (widen))
1487          ))
1488       (goto-char (point-min))
1489       (when window
1490         (set-window-start window (point-min))))))
1491
1492 (defvar gnus-article-normalized-header-length 40
1493   "Length of normalized headers.")
1494
1495 (defun article-normalize-headers ()
1496   "Make all header lines 40 characters long."
1497   (interactive)
1498   (let ((buffer-read-only nil)
1499         column)
1500     (save-excursion
1501       (save-restriction
1502         (article-narrow-to-head)
1503         (while (not (eobp))
1504           (cond
1505            ((< (setq column (- (gnus-point-at-eol) (point)))
1506                gnus-article-normalized-header-length)
1507             (end-of-line)
1508             (insert (make-string
1509                      (- gnus-article-normalized-header-length column)
1510                      ? )))
1511            ((> column gnus-article-normalized-header-length)
1512             (gnus-put-text-property
1513              (progn
1514                (forward-char gnus-article-normalized-header-length)
1515                (point))
1516              (gnus-point-at-eol)
1517              'invisible t))
1518            (t
1519             ;; Do nothing.
1520             ))
1521           (forward-line 1))))))
1522
1523 (defun article-treat-dumbquotes ()
1524   "Translate M******** sm*rtq**t*s into proper text.
1525 Note that this function guesses whether a character is a sm*rtq**t* or
1526 not, so it should only be used interactively."
1527   (interactive)
1528   (article-translate-strings gnus-article-dumbquotes-map))
1529
1530 (defun article-translate-characters (from to)
1531   "Translate all characters in the body of the article according to FROM and TO.
1532 FROM is a string of characters to translate from; to is a string of
1533 characters to translate to."
1534   (save-excursion
1535     (when (article-goto-body)
1536       (let ((buffer-read-only nil)
1537             (x (make-string 225 ?x))
1538             (i -1))
1539         (while (< (incf i) (length x))
1540           (aset x i i))
1541         (setq i 0)
1542         (while (< i (length from))
1543           (aset x (aref from i) (aref to i))
1544           (incf i))
1545         (translate-region (point) (point-max) x)))))
1546
1547 (defun article-translate-strings (map)
1548   "Translate all string in the body of the article according to MAP.
1549 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1550   (save-excursion
1551     (when (article-goto-body)
1552       (let ((buffer-read-only nil)
1553             elem)
1554         (while (setq elem (pop map))
1555           (save-excursion
1556             (while (search-forward (car elem) nil t)
1557               (replace-match (cadr elem)))))))))
1558
1559 (defun article-treat-overstrike ()
1560   "Translate overstrikes into bold text."
1561   (interactive)
1562   (save-excursion
1563     (when (article-goto-body)
1564       (let ((buffer-read-only nil))
1565         (while (search-forward "\b" nil t)
1566           (let ((next (char-after))
1567                 start end previous)
1568             (backward-char 2)
1569             (setq start (point)
1570                   previous (char-after))
1571             (forward-char 3)
1572             (setq end (point))
1573             (backward-char)
1574             ;; We do the boldification/underlining by hiding the
1575             ;; overstrikes and putting the proper text property
1576             ;; on the letters.
1577             (cond
1578              ((eq next previous)
1579               (gnus-article-hide-text-type start (point) 'overstrike)
1580               (put-text-property (point) end 'face 'bold))
1581              ((eq next ?_)
1582               (gnus-article-hide-text-type
1583                (1- (point)) (1+ (point)) 'overstrike)
1584               (put-text-property
1585                start (1- (point)) 'face 'underline))
1586              ((eq previous ?_)
1587               (gnus-article-hide-text-type start (point) 'overstrike)
1588               (put-text-property
1589                (point) end 'face 'underline)))))))))
1590
1591 (defun article-fill-long-lines ()
1592   "Fill lines that are wider than the window width."
1593   (interactive)
1594   (save-excursion
1595     (let ((buffer-read-only nil)
1596           (width (window-width (get-buffer-window (current-buffer)))))
1597       (save-restriction
1598         (article-goto-body)
1599         (let ((adaptive-fill-mode nil))
1600           (while (not (eobp))
1601             (end-of-line)
1602             (when (>= (current-column) (min fill-column width))
1603               (narrow-to-region (point) (gnus-point-at-bol))
1604               (fill-paragraph nil)
1605               (goto-char (point-max))
1606               (widen))
1607             (forward-line 1)))))))
1608
1609 (defun article-capitalize-sentences ()
1610   "Capitalize the first word in each sentence."
1611   (interactive)
1612   (save-excursion
1613     (let ((buffer-read-only nil)
1614           (paragraph-start "^[\n\^L]"))
1615       (article-goto-body)
1616       (while (not (eobp))
1617         (capitalize-word 1)
1618         (forward-sentence)))))
1619
1620 (defun article-remove-cr ()
1621   "Remove trailing CRs and then translate remaining CRs into LFs."
1622   (interactive)
1623   (save-excursion
1624     (let ((buffer-read-only nil))
1625       (goto-char (point-min))
1626       (while (re-search-forward "\r+$" nil t)
1627         (replace-match "" t t))
1628       (goto-char (point-min))
1629       (while (search-forward "\r" nil t)
1630         (replace-match "\n" t t)))))
1631
1632 (defun article-remove-trailing-blank-lines ()
1633   "Remove all trailing blank lines from the article."
1634   (interactive)
1635   (save-excursion
1636     (let ((buffer-read-only nil))
1637       (goto-char (point-max))
1638       (delete-region
1639        (point)
1640        (progn
1641          (while (and (not (bobp))
1642                      (looking-at "^[ \t]*$")
1643                      (not (gnus-annotation-in-region-p
1644                            (point) (gnus-point-at-eol))))
1645            (forward-line -1))
1646          (forward-line 1)
1647          (point))))))
1648
1649 (defun article-display-x-face (&optional force)
1650   "Look for an X-Face header and display it if present."
1651   (interactive (list 'force))
1652   (save-excursion
1653     ;; Delete the old process, if any.
1654     (when (process-status "article-x-face")
1655       (delete-process "article-x-face"))
1656     (let ((inhibit-point-motion-hooks t)
1657           (case-fold-search t)
1658           from last)
1659       (save-restriction
1660         (article-narrow-to-head)
1661         (goto-char (point-min))
1662         (setq from (message-fetch-field "from"))
1663         (goto-char (point-min))
1664         (while (and gnus-article-x-face-command
1665                     (not last)
1666                     (or force
1667                         ;; Check whether this face is censored.
1668                         (not gnus-article-x-face-too-ugly)
1669                         (and gnus-article-x-face-too-ugly from
1670                              (not (string-match gnus-article-x-face-too-ugly
1671                                                 from))))
1672                     ;; Has to be present.
1673                     (re-search-forward "^X-Face:[ \t]*" nil t))
1674           ;; This used to try to do multiple faces (`while' instead of
1675           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
1676           ;; work (b) it can crash some versions of Emacs (c) are
1677           ;; multiple faces really something to encourage?
1678           (when (stringp gnus-article-x-face-command)
1679             (setq last t))
1680           ;; We now have the area of the buffer where the X-Face is stored.
1681           (save-excursion
1682             (let ((beg (point))
1683                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
1684               ;; We display the face.
1685               (if (symbolp gnus-article-x-face-command)
1686                   ;; The command is a lisp function, so we call it.
1687                   (if (gnus-functionp gnus-article-x-face-command)
1688                       (funcall gnus-article-x-face-command beg end)
1689                     (error "%s is not a function" gnus-article-x-face-command))
1690                 ;; The command is a string, so we interpret the command
1691                 ;; as a, well, command, and fork it off.
1692                 (let ((process-connection-type nil))
1693                   (process-kill-without-query
1694                    (start-process
1695                     "article-x-face" nil shell-file-name shell-command-switch
1696                     gnus-article-x-face-command))
1697                   (process-send-region "article-x-face" beg end)
1698                   (process-send-eof "article-x-face"))))))))))
1699
1700 (defun article-decode-mime-words ()
1701   "Decode all MIME-encoded words in the article."
1702   (interactive)
1703   (save-excursion
1704     (set-buffer gnus-article-buffer)
1705     (let ((inhibit-point-motion-hooks t)
1706           buffer-read-only
1707           (mail-parse-charset gnus-newsgroup-charset)
1708           (mail-parse-ignored-charsets 
1709            (save-excursion (set-buffer gnus-summary-buffer)
1710                            gnus-newsgroup-ignored-charsets)))
1711       (mail-decode-encoded-word-region (point-min) (point-max)))))
1712
1713 (defun article-decode-charset (&optional prompt)
1714   "Decode charset-encoded text in the article.
1715 If PROMPT (the prefix), prompt for a coding system to use."
1716   (interactive "P")
1717   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
1718         buffer-read-only
1719         (mail-parse-charset gnus-newsgroup-charset)
1720         (mail-parse-ignored-charsets 
1721          (save-excursion (condition-case nil
1722                              (set-buffer gnus-summary-buffer)
1723                            (error))
1724                          gnus-newsgroup-ignored-charsets))
1725         ct cte ctl charset format)
1726   (save-excursion
1727     (save-restriction
1728       (article-narrow-to-head)
1729       (setq ct (message-fetch-field "Content-Type" t)
1730             cte (message-fetch-field "Content-Transfer-Encoding" t)
1731             ctl (and ct (ignore-errors
1732                           (mail-header-parse-content-type ct)))
1733             charset (cond
1734                      (prompt
1735                       (mm-read-coding-system "Charset to decode: "))
1736                      (ctl
1737                       (mail-content-type-get ctl 'charset)))
1738             format (and ctl (mail-content-type-get ctl 'format)))
1739       (when cte
1740         (setq cte (mail-header-strip cte)))
1741       (if (and ctl (not (string-match "/" (car ctl)))) 
1742           (setq ctl nil))
1743       (goto-char (point-max)))
1744     (forward-line 1)
1745     (save-restriction
1746       (narrow-to-region (point) (point-max))
1747       (when (and (eq mail-parse-charset 'gnus-decoded)
1748                  (eq (mm-body-7-or-8) '8bit))
1749         ;; The text code could have been decoded.
1750         (setq charset mail-parse-charset))
1751       (when (and (or (not ctl)
1752                      (equal (car ctl) "text/plain"))
1753                  (not format)) ;; article with format will decode later.
1754         (mm-decode-body
1755          charset (and cte (intern (downcase
1756                                    (gnus-strip-whitespace cte))))
1757          (car ctl)))))))
1758
1759 (defun article-decode-encoded-words ()
1760   "Remove encoded-word encoding from headers."
1761   (let (buffer-read-only)
1762     (let ((charset (save-excursion
1763                      (set-buffer gnus-summary-buffer)
1764                      default-mime-charset)))
1765       (mime-decode-header-in-buffer charset)
1766       )))
1767
1768 (defun article-de-quoted-unreadable (&optional force)
1769   "Translate a quoted-printable-encoded article.
1770 If FORCE, decode the article whether it is marked as quoted-printable
1771 or not."
1772   (interactive (list 'force))
1773   (save-excursion
1774     (let ((buffer-read-only nil) type charset)
1775       (if (gnus-buffer-live-p gnus-original-article-buffer)
1776           (with-current-buffer gnus-original-article-buffer
1777             (setq type
1778                   (gnus-fetch-field "content-transfer-encoding"))
1779             (let* ((ct (gnus-fetch-field "content-type"))
1780                    (ctl (and ct 
1781                              (ignore-errors
1782                                (mail-header-parse-content-type ct)))))
1783               (setq charset (and ctl
1784                                  (mail-content-type-get ctl 'charset)))
1785               (if (stringp charset)
1786                   (setq charset (intern (downcase charset)))))))
1787       (unless charset 
1788         (setq charset gnus-newsgroup-charset))
1789       (when (or force
1790                 (and type (let ((case-fold-search t))
1791                             (string-match "quoted-printable" type))))
1792         (article-goto-body)
1793         (quoted-printable-decode-region
1794          (point) (point-max) (mm-charset-to-coding-system charset))))))
1795
1796 (defun article-de-base64-unreadable (&optional force)
1797   "Translate a base64 article.
1798 If FORCE, decode the article whether it is marked as base64 not."
1799   (interactive (list 'force))
1800   (save-excursion
1801     (let ((buffer-read-only nil) type charset)
1802       (if (gnus-buffer-live-p gnus-original-article-buffer)
1803           (with-current-buffer gnus-original-article-buffer
1804             (setq type
1805                   (gnus-fetch-field "content-transfer-encoding"))
1806             (let* ((ct (gnus-fetch-field "content-type"))
1807                    (ctl (and ct 
1808                              (ignore-errors
1809                                (mail-header-parse-content-type ct)))))
1810               (setq charset (and ctl
1811                                  (mail-content-type-get ctl 'charset)))
1812               (if (stringp charset)
1813                   (setq charset (intern (downcase charset)))))))
1814       (unless charset 
1815         (setq charset gnus-newsgroup-charset))
1816       (when (or force
1817                 (and type (let ((case-fold-search t))
1818                             (string-match "base64" type))))
1819         (article-goto-body)
1820         (save-restriction
1821           (narrow-to-region (point) (point-max))
1822           (base64-decode-region (point-min) (point-max))
1823           (mm-decode-coding-region
1824            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
1825
1826 (eval-when-compile
1827   (require 'rfc1843))
1828
1829 (defun article-decode-HZ ()
1830   "Translate a HZ-encoded article."
1831   (interactive)
1832   (require 'rfc1843)
1833   (save-excursion
1834     (let ((buffer-read-only nil))
1835       (rfc1843-decode-region (point-min) (point-max)))))
1836
1837 (defun article-wash-html ()
1838   "Format an html article."
1839   (interactive)
1840   (save-excursion
1841     (let ((buffer-read-only nil)
1842           charset)
1843       (if (gnus-buffer-live-p gnus-original-article-buffer)
1844           (with-current-buffer gnus-original-article-buffer
1845             (let* ((ct (gnus-fetch-field "content-type"))
1846                    (ctl (and ct 
1847                              (ignore-errors
1848                                (mail-header-parse-content-type ct)))))
1849               (setq charset (and ctl
1850                                  (mail-content-type-get ctl 'charset)))
1851               (if (stringp charset)
1852                   (setq charset (intern (downcase charset)))))))
1853       (unless charset 
1854         (setq charset gnus-newsgroup-charset))
1855       (article-goto-body)
1856       (save-window-excursion
1857         (save-restriction
1858           (narrow-to-region (point) (point-max))
1859           (mm-setup-w3)
1860           (let ((w3-strict-width (window-width))
1861                 (url-standalone-mode t))
1862             (condition-case var
1863                 (w3-region (point-min) (point-max))
1864               (error))))))))
1865
1866 (defun article-hide-list-identifiers ()
1867   "Remove list identifies from the Subject header.
1868 The `gnus-list-identifiers' variable specifies what to do."
1869   (interactive)
1870   (save-excursion
1871     (save-restriction
1872       (let ((inhibit-point-motion-hooks t)
1873             buffer-read-only)
1874         (article-narrow-to-head)
1875         (let ((regexp (if (stringp gnus-list-identifiers) gnus-list-identifiers
1876                         (mapconcat 'identity gnus-list-identifiers " *\\|"))))
1877           (when regexp
1878             (goto-char (point-min))
1879             (when (re-search-forward
1880                    (concat "^Subject: +\\(\\(\\(Re: +\\)?\\(" regexp 
1881                            " *\\)\\)+\\(Re: +\\)?\\)")
1882                    nil t)
1883               (let ((s (or (match-string 3) (match-string 5))))
1884                 (delete-region (match-beginning 1) (match-end 1))
1885                 (when s
1886                   (goto-char (match-beginning 1))
1887                   (insert s))))))))))
1888
1889 (defun article-hide-pgp ()
1890   "Remove any PGP headers and signatures in the current article."
1891   (interactive)
1892   (save-excursion
1893     (save-restriction
1894       (let ((inhibit-point-motion-hooks t)
1895             buffer-read-only beg end)
1896         (article-goto-body)
1897         ;; Hide the "header".
1898         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
1899           (push 'pgp gnus-article-wash-types)
1900           (delete-region (match-beginning 0) (match-end 0))
1901           ;; Remove armor headers (rfc2440 6.2)
1902           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
1903                                      (point)))
1904           (setq beg (point))
1905           ;; Hide the actual signature.
1906           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
1907                (setq end (1+ (match-beginning 0)))
1908                (delete-region
1909                 end
1910                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
1911                     (match-end 0)
1912                   ;; Perhaps we shouldn't hide to the end of the buffer
1913                   ;; if there is no end to the signature?
1914                   (point-max))))
1915           ;; Hide "- " PGP quotation markers.
1916           (when (and beg end)
1917             (narrow-to-region beg end)
1918             (goto-char (point-min))
1919             (while (re-search-forward "^- " nil t)
1920               (delete-region
1921                (match-beginning 0) (match-end 0)))
1922             (widen))
1923           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1924
1925 (defun article-hide-pem (&optional arg)
1926   "Toggle hiding of any PEM headers and signatures in the current article.
1927 If given a negative prefix, always show; if given a positive prefix,
1928 always hide."
1929   (interactive (gnus-article-hidden-arg))
1930   (unless (gnus-article-check-hidden-text 'pem arg)
1931     (save-excursion
1932       (let (buffer-read-only end)
1933         (goto-char (point-min))
1934         ;; Hide the horrendously ugly "header".
1935         (when (and (search-forward
1936                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1937                     nil t)
1938                    (setq end (1+ (match-beginning 0))))
1939           (push 'pem gnus-article-wash-types)
1940           (gnus-article-hide-text-type
1941            end
1942            (if (search-forward "\n\n" nil t)
1943                (match-end 0)
1944              (point-max))
1945            'pem)
1946           ;; Hide the trailer as well
1947           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1948                                 nil t)
1949             (gnus-article-hide-text-type
1950              (match-beginning 0) (match-end 0) 'pem)))))))
1951
1952 (defun article-strip-banner ()
1953   "Strip the banner specified by the `banner' group parameter."
1954   (interactive)
1955   (save-excursion
1956     (save-restriction
1957       (let ((inhibit-point-motion-hooks t)
1958             (banner (gnus-group-find-parameter gnus-newsgroup-name 'banner))
1959             (gnus-signature-limit nil)
1960             buffer-read-only beg end)
1961         (when banner
1962           (article-goto-body)
1963           (cond
1964            ((eq banner 'signature)
1965             (when (gnus-article-narrow-to-signature)
1966               (widen)
1967               (forward-line -1)
1968               (delete-region (point) (point-max))))
1969            ((symbolp banner)
1970             (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
1971                 (while (re-search-forward banner nil t)
1972                   (delete-region (match-beginning 0) (match-end 0)))))
1973            ((stringp banner)
1974             (while (re-search-forward banner nil t)
1975               (delete-region (match-beginning 0) (match-end 0))))))))))
1976
1977 (defun article-babel ()
1978   "Translate article using an online translation service."
1979   (interactive)
1980   (require 'babel)
1981   (save-excursion
1982     (set-buffer gnus-article-buffer)
1983     (when (article-goto-body)
1984       (let* ((buffer-read-only nil)
1985              (start (point))
1986              (end (point-max))
1987              (orig (buffer-substring start end))
1988              (trans (babel-as-string orig)))
1989         (save-restriction
1990           (narrow-to-region start end)
1991           (delete-region start end)
1992           (insert trans))))))
1993
1994 (defun article-hide-signature (&optional arg)
1995   "Hide the signature in the current article.
1996 If given a negative prefix, always show; if given a positive prefix,
1997 always hide."
1998   (interactive (gnus-article-hidden-arg))
1999   (save-excursion
2000     (save-restriction
2001       (if (interactive-p)
2002           (progn
2003             (widen)
2004             (article-goto-body))
2005         (goto-char (point-min)))
2006       (unless (gnus-article-check-hidden-text 'signature arg)
2007         (let ((buffer-read-only nil)
2008               (button (point)))
2009           (while (setq button (text-property-any button (point-max)
2010                                                  'gnus-callback
2011                                                  'gnus-signature-toggle))
2012             (setq button (text-property-not-all button (point-max)
2013                                                 'gnus-callback
2014                                                 'gnus-signature-toggle))
2015             (when (and button (not (eobp)))
2016               (gnus-article-hide-text-type
2017                (1+ button)
2018                (next-single-property-change (1+ button) 'mime-view-entity
2019                                             nil (point-max))
2020                'signature))))))))
2021
2022 (defun article-strip-headers-in-body ()
2023   "Strip offensive headers from bodies."
2024   (interactive)
2025   (save-excursion
2026     (article-goto-body)
2027     (let ((case-fold-search t))
2028       (when (looking-at "x-no-archive:")
2029         (gnus-delete-line)))))
2030
2031 (defun article-strip-leading-blank-lines ()
2032   "Remove all blank lines from the beginning of the article."
2033   (interactive)
2034   (save-excursion
2035     (let ((inhibit-point-motion-hooks t)
2036           buffer-read-only)
2037       (when (article-goto-body)
2038         (while (and (not (eobp))
2039                     (looking-at "[ \t]*$"))
2040           (gnus-delete-line))))))
2041
2042 (defun article-narrow-to-head ()
2043   "Narrow the buffer to the head of the message.
2044 Point is left at the beginning of the narrowed-to region."
2045   (narrow-to-region
2046    (goto-char (point-min))
2047    (if (search-forward "\n\n" nil 1)
2048        (1- (point))
2049      (point-max)))
2050   (goto-char (point-min)))
2051
2052 (defun article-goto-body ()
2053   "Place point at the start of the body."
2054   (goto-char (point-min))
2055   (cond
2056    ;; This variable is only bound when dealing with separate
2057    ;; MIME body parts.
2058    (article-goto-body-goes-to-point-min-p
2059     t)
2060    ((search-forward "\n\n" nil t)
2061     t)
2062    (t
2063     (goto-char (point-max))
2064     nil)))
2065
2066 (defun article-strip-multiple-blank-lines ()
2067   "Replace consecutive blank lines with one empty line."
2068   (interactive)
2069   (save-excursion
2070     (let ((inhibit-point-motion-hooks t)
2071           buffer-read-only)
2072       ;; First make all blank lines empty.
2073       (article-goto-body)
2074       (while (re-search-forward "^[ \t]+$" nil t)
2075         (unless (gnus-annotation-in-region-p
2076                  (match-beginning 0) (match-end 0))
2077           (replace-match "" nil t)))
2078       ;; Then replace multiple empty lines with a single empty line.
2079       (article-goto-body)
2080       (while (re-search-forward "\n\n\n+" nil t)
2081         (unless (gnus-annotation-in-region-p
2082                  (match-beginning 0) (match-end 0))
2083           (replace-match "\n\n" t t))))))
2084
2085 (defun article-strip-leading-space ()
2086   "Remove all white space from the beginning of the lines in the article."
2087   (interactive)
2088   (save-excursion
2089     (let ((inhibit-point-motion-hooks t)
2090           buffer-read-only)
2091       (article-goto-body)
2092       (while (re-search-forward "^[ \t]+" nil t)
2093         (replace-match "" t t)))))
2094
2095 (defun article-strip-trailing-space ()
2096   "Remove all white space from the end of the lines in the article."
2097   (interactive)
2098   (save-excursion
2099     (let ((inhibit-point-motion-hooks t)
2100           buffer-read-only)
2101       (article-goto-body)
2102       (while (re-search-forward "[ \t]+$" nil t)
2103         (replace-match "" t t)))))
2104
2105 (defun article-strip-blank-lines ()
2106   "Strip leading, trailing and multiple blank lines."
2107   (interactive)
2108   (article-strip-leading-blank-lines)
2109   (article-remove-trailing-blank-lines)
2110   (article-strip-multiple-blank-lines))
2111
2112 (defun article-strip-all-blank-lines ()
2113   "Strip all blank lines."
2114   (interactive)
2115   (save-excursion
2116     (let ((inhibit-point-motion-hooks t)
2117           buffer-read-only)
2118       (article-goto-body)
2119       (while (re-search-forward "^[ \t]*\n" nil t)
2120         (replace-match "" t t)))))
2121
2122 (defun gnus-article-narrow-to-signature ()
2123   "Narrow to the signature; return t if a signature is found, else nil."
2124   (let ((inhibit-point-motion-hooks t))
2125     (when (gnus-article-search-signature)
2126       (forward-line 1)
2127       ;; Check whether we have some limits to what we consider
2128       ;; to be a signature.
2129       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2130                       (list gnus-signature-limit)))
2131             limit limited)
2132         (while (setq limit (pop limits))
2133           (if (or (and (integerp limit)
2134                        (< (- (point-max) (point)) limit))
2135                   (and (floatp limit)
2136                        (< (count-lines (point) (point-max)) limit))
2137                   (and (gnus-functionp limit)
2138                        (funcall limit))
2139                   (and (stringp limit)
2140                        (not (re-search-forward limit nil t))))
2141               ()                        ; This limit did not succeed.
2142             (setq limited t
2143                   limits nil)))
2144         (unless limited
2145           (narrow-to-region (point) (point-max))
2146           t)))))
2147
2148 (defun gnus-article-search-signature ()
2149   "Search the current buffer for the signature separator.
2150 Put point at the beginning of the signature separator."
2151   (let ((cur (point)))
2152     (goto-char (point-max))
2153     (if (if (stringp gnus-signature-separator)
2154             (re-search-backward gnus-signature-separator nil t)
2155           (let ((seps gnus-signature-separator))
2156             (while (and seps
2157                         (not (re-search-backward (car seps) nil t)))
2158               (pop seps))
2159             seps))
2160         t
2161       (goto-char cur)
2162       nil)))
2163
2164 (defun gnus-article-hidden-arg ()
2165   "Return the current prefix arg as a number, or 0 if no prefix."
2166   (list (if current-prefix-arg
2167             (prefix-numeric-value current-prefix-arg)
2168           0)))
2169
2170 (defun gnus-article-check-hidden-text (type arg)
2171   "Return nil if hiding is necessary.
2172 Arg can be nil or a number.  Nil and positive means hide, negative
2173 means show, 0 means toggle."
2174   (save-excursion
2175     (save-restriction
2176       (let ((hide (gnus-article-hidden-text-p type)))
2177         (cond
2178          ((or (null arg)
2179               (> arg 0))
2180           nil)
2181          ((< arg 0)
2182           (gnus-article-show-hidden-text type))
2183          (t
2184           (if (eq hide 'hidden)
2185               (gnus-article-show-hidden-text type)
2186             nil)))))))
2187
2188 (defun gnus-article-hidden-text-p (type)
2189   "Say whether the current buffer contains hidden text of type TYPE."
2190   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2191     (while (and pos
2192                 (not (get-text-property pos 'invisible))
2193                 (not (get-text-property pos 'dummy-invisible)))
2194       (setq pos
2195             (text-property-any (1+ pos) (point-max) 'article-type type)))
2196     (if pos
2197         'hidden
2198       nil)))
2199
2200 (defun gnus-article-show-hidden-text (type &optional dummy)
2201   "Show all hidden text of type TYPE.
2202 Originally it is hide instead of DUMMY."
2203   (let ((buffer-read-only nil)
2204         (inhibit-point-motion-hooks t))
2205     (gnus-remove-text-properties-when 
2206      'article-type type
2207      (point-min) (point-max) 
2208      (cons 'article-type (cons type
2209                                gnus-hidden-properties)))))
2210
2211 (defconst article-time-units
2212   `((year . ,(* 365.25 24 60 60))
2213     (week . ,(* 7 24 60 60))
2214     (day . ,(* 24 60 60))
2215     (hour . ,(* 60 60))
2216     (minute . 60)
2217     (second . 1))
2218   "Mapping from time units to seconds.")
2219
2220 (defun article-date-ut (&optional type highlight header)
2221   "Convert DATE date to universal time in the current article.
2222 If TYPE is `local', convert to local time; if it is `lapsed', output
2223 how much time has lapsed since DATE.  For `lapsed', the value of
2224 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2225 should replace the \"Date:\" one, or should be added below it."
2226   (interactive (list 'ut t))
2227   (let* ((header (or header
2228                      (and (eq 1 (point-min))
2229                           (mail-header-date (save-excursion
2230                                               (set-buffer gnus-summary-buffer)
2231                                               gnus-current-headers)))
2232                      (message-fetch-field "date")
2233                      ""))
2234          (date (if (vectorp header) (mail-header-date header)
2235                  header))
2236          (inhibit-point-motion-hooks t)
2237          bface eface date-pos)
2238     (when (and date (not (string= date "")))
2239       (save-excursion
2240         (save-restriction
2241           (article-narrow-to-head)
2242           (when (or (and (eq type 'lapsed)
2243                          gnus-article-date-lapsed-new-header
2244                          ;; Attempt to get the face of X-Sent first.
2245                          (re-search-forward "^X-Sent:[ \t]" nil t))
2246                     (re-search-forward "^Date:[ \t]" nil t)
2247                     ;; If Date is missing, try again for X-Sent.
2248                     (re-search-forward "^X-Sent:[ \t]" nil t))
2249             (setq bface (get-text-property (gnus-point-at-bol) 'face)
2250                   date (or (get-text-property (gnus-point-at-bol)
2251                                                 'original-date)
2252                              date)
2253                   eface (get-text-property (1- (gnus-point-at-eol))
2254                                            'face)))
2255           (let ((buffer-read-only nil))
2256             ;; Delete any old X-Sent headers.
2257             (when (setq date-pos
2258                         (text-property-any (point-min) (point-max)
2259                                            'article-date-lapsed t))
2260               (goto-char (setq date-pos (set-marker (make-marker) date-pos)))
2261               (delete-region (match-beginning 0)
2262                              (progn (forward-line 1) (point))))
2263             (goto-char (point-min))
2264             ;; Delete any old Date headers.
2265             (while (re-search-forward "^Date:[ \t]" nil t)
2266               (unless date-pos
2267                 (setq date-pos (match-beginning 0)))
2268               (unless (and (eq type 'lapsed)
2269                            gnus-article-date-lapsed-new-header)
2270                 (delete-region (match-beginning 0)
2271                                (progn (message-next-header) (point)))))
2272             (if date-pos
2273                 (progn
2274                   (goto-char date-pos)
2275                   (unless (bolp)
2276                     ;; Possibly, Date has been deleted.
2277                     (insert "\n"))
2278                   (when (and (eq type 'lapsed)
2279                              gnus-article-date-lapsed-new-header
2280                              (looking-at "Date:"))
2281                     (forward-line 1)))
2282               (goto-char (point-min)))
2283             (insert (article-make-date-line date type))
2284             (when (eq type 'lapsed)
2285               (put-text-property (gnus-point-at-bol) (point)
2286                                  'article-date-lapsed t))
2287             (insert "\n")
2288             (forward-line -1)
2289             ;; Do highlighting.
2290             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2291               (put-text-property (match-beginning 1) (1+ (match-end 1))
2292                                  'original-date date)
2293               (put-text-property (match-beginning 1) (1+ (match-end 1))
2294                                  'face bface)
2295               (put-text-property (match-beginning 2) (match-end 2)
2296                                  'face eface))))))))
2297
2298 (defun article-make-date-line (date type)
2299   "Return a DATE line of TYPE."
2300   (let ((time (condition-case ()
2301                   (date-to-time date)
2302                 (error '(0 0)))))
2303     (cond
2304      ;; Convert to the local timezone.  We have to slap a
2305      ;; `condition-case' round the calls to the timezone
2306      ;; functions since they aren't particularly resistant to
2307      ;; buggy dates.
2308      ((eq type 'local)
2309       (let ((tz (car (current-time-zone time))))
2310         (format "Date: %s %s%02d%02d" (current-time-string time)
2311                 (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
2312                 (/ (% (abs tz) 3600) 60))))
2313      ;; Convert to Universal Time.
2314      ((eq type 'ut)
2315       (concat "Date: "
2316               (current-time-string
2317                (let* ((e (parse-time-string date))
2318                       (tm (apply 'encode-time e))
2319                       (ms (car tm))
2320                       (ls (- (cadr tm) (car (current-time-zone time)))))
2321                  (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2322                        ((> ls 65535) (list (1+ ms) (- ls 65536)))
2323                        (t (list ms ls)))))
2324               " UT"))
2325      ;; Get the original date from the article.
2326      ((eq type 'original)
2327       (concat "Date: " (if (string-match "\n+$" date)
2328                            (substring date 0 (match-beginning 0))
2329                          date)))
2330      ;; Let the user define the format.
2331      ((eq type 'user)
2332       (if (gnus-functionp gnus-article-time-format)
2333           (funcall gnus-article-time-format time)
2334         (concat
2335          "Date: "
2336          (format-time-string gnus-article-time-format time))))
2337      ;; ISO 8601.
2338      ((eq type 'iso8601)
2339       (let ((tz (car (current-time-zone time))))
2340         (concat
2341          "Date: "
2342          (format-time-string "%Y%m%dT%H%M%S" time)
2343          (format "%s%02d%02d"
2344                  (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
2345                  (/ (% (abs tz) 3600) 60)))))
2346      ;; Do an X-Sent lapsed format.
2347      ((eq type 'lapsed)
2348       ;; If the date is seriously mangled, the timezone functions are
2349       ;; liable to bug out, so we ignore all errors.
2350       (let* ((now (current-time))
2351              (real-time (subtract-time now time))
2352              (real-sec (and real-time
2353                             (+ (* (float (car real-time)) 65536)
2354                                (cadr real-time))))
2355              (sec (and real-time (abs real-sec)))
2356              num prev)
2357         (cond
2358          ((null real-time)
2359           "X-Sent: Unknown")
2360          ((zerop sec)
2361           "X-Sent: Now")
2362          (t
2363           (concat
2364            "X-Sent: "
2365            ;; This is a bit convoluted, but basically we go
2366            ;; through the time units for years, weeks, etc,
2367            ;; and divide things to see whether that results
2368            ;; in positive answers.
2369            (mapconcat
2370             (lambda (unit)
2371               (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2372                   ;; The (remaining) seconds are too few to
2373                   ;; be divided into this time unit.
2374                   ""
2375                 ;; It's big enough, so we output it.
2376                 (setq sec (- sec (* num (cdr unit))))
2377                 (prog1
2378                     (concat (if prev ", " "") (int-to-string
2379                                                (floor num))
2380                             " " (symbol-name (car unit))
2381                             (if (> num 1) "s" ""))
2382                   (setq prev t))))
2383             article-time-units "")
2384            ;; If dates are odd, then it might appear like the
2385            ;; article was sent in the future.
2386            (if (> real-sec 0)
2387                " ago"
2388              " in the future"))))))
2389      (t
2390       (error "Unknown conversion type: %s" type)))))
2391
2392 (defun article-date-local (&optional highlight)
2393   "Convert the current article date to the local timezone."
2394   (interactive (list t))
2395   (article-date-ut 'local highlight))
2396
2397 (defun article-date-original (&optional highlight)
2398   "Convert the current article date to what it was originally.
2399 This is only useful if you have used some other date conversion
2400 function and want to see what the date was before converting."
2401   (interactive (list t))
2402   (article-date-ut 'original highlight))
2403
2404 (defun article-date-lapsed (&optional highlight)
2405   "Convert the current article date to time lapsed since it was sent."
2406   (interactive (list t))
2407   (article-date-ut 'lapsed highlight))
2408
2409 (defun article-update-date-lapsed ()
2410   "Function to be run from a timer to update the lapsed time line."
2411   (let (deactivate-mark)
2412     (save-excursion
2413       (ignore-errors
2414         (walk-windows
2415          (lambda (w)
2416            (set-buffer (window-buffer w))
2417            (when (eq major-mode 'gnus-article-mode)
2418              (goto-char (point-min))
2419              (when (re-search-forward "^X-Sent:" nil t)
2420                (article-date-lapsed t))))
2421          nil 'visible)))))
2422
2423 (defun gnus-start-date-timer (&optional n)
2424   "Start a timer to update the X-Sent header in the article buffers.
2425 The numerical prefix says how frequently (in seconds) the function
2426 is to run."
2427   (interactive "p")
2428   (unless n
2429     (setq n 1))
2430   (gnus-stop-date-timer)
2431   (setq article-lapsed-timer
2432         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2433
2434 (defun gnus-stop-date-timer ()
2435   "Stop the X-Sent timer."
2436   (interactive)
2437   (when article-lapsed-timer
2438     (nnheader-cancel-timer article-lapsed-timer)
2439     (setq article-lapsed-timer nil)))
2440
2441 (defun article-date-user (&optional highlight)
2442   "Convert the current article date to the user-defined format.
2443 This format is defined by the `gnus-article-time-format' variable."
2444   (interactive (list t))
2445   (article-date-ut 'user highlight))
2446
2447 (defun article-date-iso8601 (&optional highlight)
2448   "Convert the current article date to ISO8601."
2449   (interactive (list t))
2450   (article-date-ut 'iso8601 highlight))
2451
2452 (defun article-show-all ()
2453   "Show all hidden text in the article buffer."
2454   (interactive)
2455   (save-excursion
2456     (widen)
2457     (let ((buffer-read-only nil))
2458       (gnus-article-unhide-text (point-min) (point-max))
2459       (gnus-remove-text-with-property 'gnus-prev)
2460       (gnus-remove-text-with-property 'gnus-next))))
2461
2462 (defun article-show-all-headers ()
2463   "Show all hidden headers in the article buffer."
2464   (interactive)
2465   (save-excursion
2466     (save-restriction
2467       (widen)
2468       (article-narrow-to-head)
2469       (let ((buffer-read-only nil))
2470         (gnus-article-unhide-text (point-min) (point-max))))))
2471
2472 (defun article-emphasize (&optional arg)
2473   "Emphasize text according to `gnus-emphasis-alist'."
2474   (interactive (gnus-article-hidden-arg))
2475   (unless (gnus-article-check-hidden-text 'emphasis arg)
2476     (save-excursion
2477       (let ((alist (or 
2478                     (condition-case nil
2479                         (with-current-buffer gnus-summary-buffer 
2480                           gnus-article-emphasis-alist) 
2481                       (error))
2482                     gnus-emphasis-alist))
2483             (buffer-read-only nil)
2484             (props (append '(article-type emphasis)
2485                            gnus-hidden-properties))
2486             regexp elem beg invisible visible face)
2487         (article-goto-body)
2488         (setq beg (point))
2489         (while (setq elem (pop alist))
2490           (goto-char beg)
2491           (setq regexp (car elem)
2492                 invisible (nth 1 elem)
2493                 visible (nth 2 elem)
2494                 face (nth 3 elem))
2495           (while (re-search-forward regexp nil t)
2496             (when (and (match-beginning visible) (match-beginning invisible))
2497               (push 'emphasis gnus-article-wash-types)
2498               (gnus-article-hide-text
2499                (match-beginning invisible) (match-end invisible) props)
2500               (gnus-article-unhide-text-type
2501                (match-beginning visible) (match-end visible) 'emphasis)
2502               (gnus-put-text-property-excluding-newlines
2503                (match-beginning visible) (match-end visible) 'face face)
2504               (goto-char (match-end invisible)))))))))
2505
2506 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2507   "Setup newsgroup emphasis alist."
2508   (unless gnus-article-emphasis-alist
2509     (let ((name (and gnus-newsgroup-name
2510                      (gnus-group-real-name gnus-newsgroup-name))))
2511       (make-local-variable 'gnus-article-emphasis-alist)
2512       (setq gnus-article-emphasis-alist
2513             (nconc
2514              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2515                (while (setq elem (pop alist))
2516                  (when (and name (string-match (car elem) name))
2517                    (setq alist nil
2518                          highlight (copy-sequence (cdr elem)))))
2519                highlight)
2520              (copy-sequence highlight-words)
2521              (if gnus-newsgroup-name
2522                  (copy-sequence (gnus-group-find-parameter
2523                                  gnus-newsgroup-name 'highlight-words t)))
2524              gnus-emphasis-alist)))))
2525
2526 (defvar gnus-summary-article-menu)
2527 (defvar gnus-summary-post-menu)
2528
2529 ;;; Saving functions.
2530
2531 (defun gnus-article-save (save-buffer file &optional num)
2532   "Save the currently selected article."
2533   (unless gnus-save-all-headers
2534     ;; Remove headers according to `gnus-saved-headers'.
2535     (let ((gnus-visible-headers
2536            (or gnus-saved-headers gnus-visible-headers))
2537           (gnus-article-buffer save-buffer))
2538       (save-excursion
2539         (set-buffer save-buffer)
2540         (article-hide-headers 1 t))))
2541   (save-window-excursion
2542     (if (not gnus-default-article-saver)
2543         (error "No default saver is defined")
2544       ;; !!! Magic!  The saving functions all save
2545       ;; `gnus-save-article-buffer' (or so they think), but we
2546       ;; bind that variable to our save-buffer.
2547       (set-buffer gnus-article-buffer)
2548       (let* ((gnus-save-article-buffer save-buffer)
2549              (filename
2550               (cond
2551                ((not gnus-prompt-before-saving) 'default)
2552                ((eq gnus-prompt-before-saving 'always) nil)
2553                (t file)))
2554              (gnus-number-of-articles-to-be-saved
2555               (when (eq gnus-prompt-before-saving t)
2556                 num)))                  ; Magic
2557         (set-buffer gnus-article-current-summary)
2558         (funcall gnus-default-article-saver filename)))))
2559
2560 (defun gnus-read-save-file-name (prompt &optional filename
2561                                         function group headers variable)
2562   (let ((default-name
2563           (funcall function group headers (symbol-value variable)))
2564         result)
2565     (setq result
2566           (expand-file-name
2567            (cond
2568             ((eq filename 'default)
2569              default-name)
2570             ((eq filename t)
2571              default-name)
2572             (filename filename)
2573             (t
2574              (let* ((split-name (gnus-get-split-value gnus-split-methods))
2575                     (prompt
2576                      (format prompt
2577                              (if (and gnus-number-of-articles-to-be-saved
2578                                       (> gnus-number-of-articles-to-be-saved 1))
2579                                  (format "these %d articles"
2580                                          gnus-number-of-articles-to-be-saved)
2581                                "this article")))
2582                     (file
2583                      ;; Let the split methods have their say.
2584                      (cond
2585                       ;; No split name was found.
2586                       ((null split-name)
2587                        (read-file-name
2588                         (concat prompt " (default "
2589                                 (file-name-nondirectory default-name) ") ")
2590                         (file-name-directory default-name)
2591                         default-name))
2592                       ;; A single group name is returned.
2593                       ((stringp split-name)
2594                        (setq default-name
2595                              (funcall function split-name headers
2596                                       (symbol-value variable)))
2597                        (read-file-name
2598                         (concat prompt " (default "
2599                                 (file-name-nondirectory default-name) ") ")
2600                         (file-name-directory default-name)
2601                         default-name))
2602                       ;; A single split name was found
2603                       ((= 1 (length split-name))
2604                        (let* ((name (expand-file-name
2605                                      (car split-name)
2606                                      gnus-article-save-directory))
2607                               (dir (cond ((file-directory-p name)
2608                                           (file-name-as-directory name))
2609                                          ((file-exists-p name) name)
2610                                          (t gnus-article-save-directory))))
2611                          (read-file-name
2612                           (concat prompt " (default " name ") ")
2613                           dir name)))
2614                       ;; A list of splits was found.
2615                       (t
2616                        (setq split-name (nreverse split-name))
2617                        (let (result)
2618                          (let ((file-name-history
2619                                 (nconc split-name file-name-history)))
2620                            (setq result
2621                                  (expand-file-name
2622                                   (read-file-name
2623                                    (concat prompt " (`M-p' for defaults) ")
2624                                    gnus-article-save-directory
2625                                    (car split-name))
2626                                   gnus-article-save-directory)))
2627                          (car (push result file-name-history)))))))
2628                ;; Create the directory.
2629                (gnus-make-directory (file-name-directory file))
2630       ;; If we have read a directory, we append the default file name.
2631                (when (file-directory-p file)
2632                  (setq file (expand-file-name (file-name-nondirectory
2633                                                default-name)
2634                                               (file-name-as-directory file))))
2635                ;; Possibly translate some characters.
2636                (nnheader-translate-file-chars file))))))
2637     (gnus-make-directory (file-name-directory result))
2638     (set variable result)))
2639
2640 (defun gnus-article-archive-name (group)
2641   "Return the first instance of an \"Archive-name\" in the current buffer."
2642   (let ((case-fold-search t))
2643     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2644       (nnheader-concat gnus-article-save-directory
2645                        (match-string 1)))))
2646
2647 (defun gnus-article-nndoc-name (group)
2648   "If GROUP is an nndoc group, return the name of the parent group."
2649   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2650     (gnus-group-get-parameter group 'save-article-group)))
2651
2652 (defun gnus-summary-save-in-rmail (&optional filename)
2653   "Append this article to Rmail file.
2654 Optional argument FILENAME specifies file name.
2655 Directory to save to is default to `gnus-article-save-directory'."
2656   (setq filename (gnus-read-save-file-name
2657                   "Save %s in rmail file:" filename
2658                   gnus-rmail-save-name gnus-newsgroup-name
2659                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2660   (gnus-eval-in-buffer-window gnus-save-article-buffer
2661     (save-excursion
2662       (save-restriction
2663         (widen)
2664         (gnus-output-to-rmail filename))))
2665   filename)
2666
2667 (defun gnus-summary-save-in-mail (&optional filename)
2668   "Append this article to Unix mail file.
2669 Optional argument FILENAME specifies file name.
2670 Directory to save to is default to `gnus-article-save-directory'."
2671   (setq filename (gnus-read-save-file-name
2672                   "Save %s in Unix mail file:" filename
2673                   gnus-mail-save-name gnus-newsgroup-name
2674                   gnus-current-headers 'gnus-newsgroup-last-mail))
2675   (gnus-eval-in-buffer-window gnus-save-article-buffer
2676     (save-excursion
2677       (save-restriction
2678         (widen)
2679         (if (and (file-readable-p filename)
2680                  (mail-file-babyl-p filename))
2681             (rmail-output-to-rmail-file filename t)
2682           (gnus-output-to-mail filename)))))
2683   filename)
2684
2685 (defun gnus-summary-save-in-file (&optional filename overwrite)
2686   "Append this article to file.
2687 Optional argument FILENAME specifies file name.
2688 Directory to save to is default to `gnus-article-save-directory'."
2689   (setq filename (gnus-read-save-file-name
2690                   "Save %s in file:" filename
2691                   gnus-file-save-name gnus-newsgroup-name
2692                   gnus-current-headers 'gnus-newsgroup-last-file))
2693   (gnus-eval-in-buffer-window gnus-save-article-buffer
2694     (save-excursion
2695       (save-restriction
2696         (widen)
2697         (when (and overwrite
2698                    (file-exists-p filename))
2699           (delete-file filename))
2700         (gnus-output-to-file filename))))
2701   filename)
2702
2703 (defun gnus-summary-write-to-file (&optional filename)
2704   "Write this article to a file.
2705 Optional argument FILENAME specifies file name.
2706 The directory to save in defaults to `gnus-article-save-directory'."
2707   (gnus-summary-save-in-file nil t))
2708
2709 (defun gnus-summary-save-body-in-file (&optional filename)
2710   "Append this article body to a file.
2711 Optional argument FILENAME specifies file name.
2712 The directory to save in defaults to `gnus-article-save-directory'."
2713   (setq filename (gnus-read-save-file-name
2714                   "Save %s body in file:" filename
2715                   gnus-file-save-name gnus-newsgroup-name
2716                   gnus-current-headers 'gnus-newsgroup-last-file))
2717   (gnus-eval-in-buffer-window gnus-save-article-buffer
2718     (save-excursion
2719       (save-restriction
2720         (widen)
2721         (when (article-goto-body)
2722           (narrow-to-region (point) (point-max)))
2723         (gnus-output-to-file filename))))
2724   filename)
2725
2726 (defun gnus-summary-save-in-pipe (&optional command)
2727   "Pipe this article to subprocess."
2728   (setq command
2729         (cond ((and (eq command 'default)
2730                     gnus-last-shell-command)
2731                gnus-last-shell-command)
2732               (command command)
2733               (t (read-string
2734                   (format
2735                    "Shell command on %s: "
2736                    (if (and gnus-number-of-articles-to-be-saved
2737                             (> gnus-number-of-articles-to-be-saved 1))
2738                        (format "these %d articles"
2739                                gnus-number-of-articles-to-be-saved)
2740                      "this article"))
2741                   gnus-last-shell-command))))
2742   (when (string-equal command "")
2743     (setq command gnus-last-shell-command))
2744   (gnus-eval-in-buffer-window gnus-article-buffer
2745     (save-restriction
2746       (widen)
2747       (shell-command-on-region (point-min) (point-max) command nil)))
2748   (setq gnus-last-shell-command command))
2749
2750 ;;; Article file names when saving.
2751
2752 (defun gnus-capitalize-newsgroup (newsgroup)
2753   "Capitalize NEWSGROUP name."
2754   (when (not (zerop (length newsgroup)))
2755     (concat (char-to-string (upcase (aref newsgroup 0)))
2756             (substring newsgroup 1))))
2757
2758 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2759   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2760 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2761 Otherwise, it is like ~/News/news/group/num."
2762   (let ((default
2763           (expand-file-name
2764            (concat (if (gnus-use-long-file-name 'not-save)
2765                        (gnus-capitalize-newsgroup newsgroup)
2766                      (gnus-newsgroup-directory-form newsgroup))
2767                    "/" (int-to-string (mail-header-number headers)))
2768            gnus-article-save-directory)))
2769     (if (and last-file
2770              (string-equal (file-name-directory default)
2771                            (file-name-directory last-file))
2772              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2773         default
2774       (or last-file default))))
2775
2776 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2777   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2778 If variable `gnus-use-long-file-name' is non-nil, it is
2779 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2780   (let ((default
2781           (expand-file-name
2782            (concat (if (gnus-use-long-file-name 'not-save)
2783                        newsgroup
2784                      (gnus-newsgroup-directory-form newsgroup))
2785                    "/" (int-to-string (mail-header-number headers)))
2786            gnus-article-save-directory)))
2787     (if (and last-file
2788              (string-equal (file-name-directory default)
2789                            (file-name-directory last-file))
2790              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2791         default
2792       (or last-file default))))
2793
2794 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2795   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2796 If variable `gnus-use-long-file-name' is non-nil, it is
2797 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2798   (or last-file
2799       (expand-file-name
2800        (if (gnus-use-long-file-name 'not-save)
2801            newsgroup
2802          (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup)))
2803        gnus-article-save-directory)))
2804
2805 (defun article-verify-x-pgp-sig ()
2806   "Verify X-PGP-Sig."
2807   (interactive)
2808   (if (gnus-buffer-live-p gnus-original-article-buffer)
2809       (let ((sig (with-current-buffer gnus-original-article-buffer
2810                    (gnus-fetch-field "X-PGP-Sig")))
2811             items info headers)
2812         (when (and sig 
2813                    mml2015-use
2814                    (mml2015-clear-verify-function))
2815           (with-temp-buffer
2816             (insert-buffer gnus-original-article-buffer)
2817             (setq items (split-string sig))
2818             (message-narrow-to-head)
2819             (let ((inhibit-point-motion-hooks t)
2820                   (case-fold-search t))
2821               ;; Don't verify multiple headers.
2822               (setq headers (mapconcat (lambda (header)
2823                                          (concat header ": " 
2824                                                  (mail-fetch-field header) "\n"))
2825                                        (split-string (nth 1 items) ",") "")))
2826             (delete-region (point-min) (point-max))
2827             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
2828             (insert "X-Signed-Headers: " (nth 1 items) "\n")
2829             (insert headers)
2830             (widen)
2831             (forward-line)
2832             (while (not (eobp))
2833               (if (looking-at "^-")
2834                   (insert "- "))
2835               (forward-line))
2836             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
2837             (insert "Version: " (car items) "\n\n")
2838             (insert (mapconcat 'identity (cddr items) "\n"))
2839             (insert "\n-----END PGP SIGNATURE-----\n")
2840             (let ((mm-security-handle (list (format "multipart/signed"))))
2841               (mml2015-clean-buffer)
2842               (let ((coding-system-for-write (or gnus-newsgroup-charset
2843                                                  'iso-8859-1)))
2844                 (funcall (mml2015-clear-verify-function)))
2845               (setq info 
2846                     (or (mm-handle-multipart-ctl-parameter 
2847                          mm-security-handle 'gnus-details)
2848                         (mm-handle-multipart-ctl-parameter 
2849                          mm-security-handle 'gnus-info)))))
2850           (when info
2851             (let (buffer-read-only bface eface)
2852               (save-restriction
2853                 (message-narrow-to-head)
2854                 (goto-char (point-max))
2855                 (forward-line -1)
2856                 (setq bface (get-text-property (gnus-point-at-bol) 'face)
2857                       eface (get-text-property (1- (gnus-point-at-eol)) 'face))
2858                 (message-remove-header "X-Gnus-PGP-Verify")
2859                 (if (re-search-forward "^X-PGP-Sig:" nil t)
2860                     (forward-line)
2861                   (goto-char (point-max)))
2862                 (narrow-to-region (point) (point))
2863                 (insert "X-Gnus-PGP-Verify: " info "\n")
2864                 (goto-char (point-min))
2865                 (forward-line)
2866                 (while (not (eobp))
2867                   (if (not (looking-at "^[ \t]"))
2868                       (insert " "))
2869                   (forward-line))
2870                 ;; Do highlighting.
2871                 (goto-char (point-min))
2872                 (when (looking-at "\\([^:]+\\): *")
2873                   (put-text-property (match-beginning 1) (1+ (match-end 1))
2874                                      'face bface)
2875                   (put-text-property (match-end 0) (point-max)
2876                                      'face eface)))))))))
2877
2878 (eval-and-compile
2879   (mapcar
2880    (lambda (func)
2881      (let (afunc gfunc)
2882        (if (consp func)
2883            (setq afunc (car func)
2884                  gfunc (cdr func))
2885          (setq afunc func
2886                gfunc (intern (format "gnus-%s" func))))
2887        (defalias gfunc
2888          (if (fboundp afunc)
2889            `(lambda (&optional interactive &rest args)
2890               ,(documentation afunc t)
2891               (interactive (list t))
2892               (save-excursion
2893                 (set-buffer gnus-article-buffer)
2894                 (if interactive
2895                     (call-interactively ',afunc)
2896                   (apply ',afunc args))))))))
2897    '(article-hide-headers
2898      article-verify-x-pgp-sig
2899      article-hide-boring-headers
2900      article-toggle-headers
2901      article-treat-overstrike
2902      article-fill-long-lines
2903      article-capitalize-sentences
2904      article-remove-cr
2905      article-display-x-face
2906      article-de-quoted-unreadable
2907      article-de-base64-unreadable
2908      article-decode-HZ
2909      article-wash-html
2910      article-hide-list-identifiers
2911      article-hide-pgp
2912      article-strip-banner
2913      article-babel
2914      article-hide-pem
2915      article-hide-signature
2916      article-strip-headers-in-body
2917      article-remove-trailing-blank-lines
2918      article-strip-leading-blank-lines
2919      article-strip-multiple-blank-lines
2920      article-strip-leading-space
2921      article-strip-trailing-space
2922      article-strip-blank-lines
2923      article-strip-all-blank-lines
2924      article-date-local
2925      article-date-iso8601
2926      article-date-original
2927      article-date-ut
2928      article-decode-mime-words
2929      article-decode-charset
2930      article-decode-encoded-words
2931      article-date-user
2932      article-date-lapsed
2933      article-emphasize
2934      article-treat-dumbquotes
2935      article-normalize-headers
2936      (article-show-all-headers . gnus-article-show-all-headers)
2937      (article-show-all . gnus-article-show-all))))
2938 \f
2939 ;;;
2940 ;;; Gnus article mode
2941 ;;;
2942
2943 (put 'gnus-article-mode 'mode-class 'special)
2944
2945 (gnus-define-keys gnus-article-mode-map
2946   " " gnus-article-goto-next-page
2947   "\177" gnus-article-goto-prev-page
2948   [delete] gnus-article-goto-prev-page
2949   [backspace] gnus-article-goto-prev-page
2950   "\C-c^" gnus-article-refer-article
2951   "h" gnus-article-show-summary
2952   "s" gnus-article-show-summary
2953   "\C-c\C-m" gnus-article-mail
2954   "?" gnus-article-describe-briefly
2955   "e" gnus-summary-edit-article
2956   "<" beginning-of-buffer
2957   ">" end-of-buffer
2958   "\C-c\C-i" gnus-info-find-node
2959   "\C-c\C-b" gnus-bug
2960   "\C-hk" gnus-article-describe-key
2961   "\C-hc" gnus-article-describe-key-briefly
2962
2963   "\C-d" gnus-article-read-summary-keys
2964   "\M-*" gnus-article-read-summary-keys
2965   "\M-#" gnus-article-read-summary-keys
2966   "\M-^" gnus-article-read-summary-keys
2967   "\M-g" gnus-article-read-summary-keys)
2968
2969 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
2970 (mapcar
2971  (lambda (key)
2972    (unless (lookup-key gnus-article-mode-map key)
2973      (define-key gnus-article-mode-map key
2974        'gnus-article-read-summary-keys)))
2975  (delq nil
2976        (append
2977         (mapcar
2978          (lambda (elt)
2979            (let ((key (car elt)))
2980              (and (> (length key) 0)
2981                   (not (eq 'menu-bar (aref key 0)))
2982                   (symbolp (lookup-key gnus-summary-mode-map key))
2983                   key)))
2984          (accessible-keymaps gnus-summary-mode-map))
2985         (let ((c 127)
2986               keys)
2987           (while (>= c 32)
2988             (push (char-to-string c) keys)
2989             (decf c))
2990           keys))))
2991
2992 (eval-when-compile 
2993   (defvar gnus-article-commands-menu))
2994
2995 (defun gnus-article-make-menu-bar ()
2996   (gnus-turn-off-edit-menu 'article)
2997   (unless (boundp 'gnus-article-article-menu)
2998     (easy-menu-define
2999      gnus-article-article-menu gnus-article-mode-map ""
3000      '("Article"
3001        ["Scroll forwards" gnus-article-goto-next-page t]
3002        ["Scroll backwards" gnus-article-goto-prev-page t]
3003        ["Show summary" gnus-article-show-summary t]
3004        ["Fetch Message-ID at point" gnus-article-refer-article t]
3005        ["Mail to address at point" gnus-article-mail t]
3006        ["Send a bug report" gnus-bug t]))
3007
3008     (easy-menu-define
3009      gnus-article-treatment-menu gnus-article-mode-map ""
3010      ;; Fixme: this should use :active (and maybe :visible).
3011      '("Treatment"
3012        ["Hide headers" gnus-article-toggle-headers t]
3013        ["Hide signature" gnus-article-hide-signature t]
3014        ["Hide citation" gnus-article-hide-citation t]
3015        ["Treat overstrike" gnus-article-treat-overstrike t]
3016        ["Remove carriage return" gnus-article-remove-cr t]
3017        ["Decode HZ" gnus-article-decode-HZ t]))
3018
3019     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3020
3021     (when (boundp 'gnus-summary-post-menu)
3022       (define-key gnus-article-mode-map [menu-bar post]
3023         (cons "Post" gnus-summary-post-menu)))
3024
3025     (gnus-run-hooks 'gnus-article-menu-hook))
3026   ;; Add the menu.
3027   (when (boundp 'gnus-article-commands-menu)
3028     (easy-menu-add gnus-article-commands-menu gnus-article-mode-map))
3029   (when (boundp 'gnus-summary-post-menu)
3030     (easy-menu-add gnus-summary-post-menu gnus-article-mode-map)))
3031
3032 ;; Fixme: do something for the Emacs tool bar in Article mode a la
3033 ;; Summary.
3034
3035 (defun gnus-article-mode ()
3036   "Major mode for displaying an article.
3037
3038 All normal editing commands are switched off.
3039
3040 The following commands are available in addition to all summary mode
3041 commands:
3042 \\<gnus-article-mode-map>
3043 \\[gnus-article-next-page]\t Scroll the article one page forwards
3044 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3045 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3046 \\[gnus-article-show-summary]\t Display the summary buffer
3047 \\[gnus-article-mail]\t Send a reply to the address near point
3048 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3049 \\[gnus-info-find-node]\t Go to the Gnus info node"
3050   (interactive)
3051   (when (gnus-visual-p 'article-menu 'menu)
3052     (gnus-article-make-menu-bar))
3053   (gnus-simplify-mode-line)
3054   (setq mode-name "Article")
3055   (setq major-mode 'gnus-article-mode)
3056   (make-local-variable 'minor-mode-alist)
3057   (unless (assq 'gnus-show-mime minor-mode-alist)
3058     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
3059   (use-local-map gnus-article-mode-map)
3060   (gnus-update-format-specifications nil 'article-mode)
3061   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3062   (make-local-variable 'gnus-page-broken)
3063   (make-local-variable 'gnus-button-marker-list)
3064   (make-local-variable 'gnus-article-current-summary)
3065   (make-local-variable 'gnus-article-mime-handles)
3066   (make-local-variable 'gnus-article-decoded-p)
3067   (make-local-variable 'gnus-article-mime-handle-alist)
3068   (make-local-variable 'gnus-article-wash-types)
3069   (make-local-variable 'gnus-article-charset)
3070   (make-local-variable 'gnus-article-ignored-charsets)
3071   (gnus-set-default-directory)
3072   (buffer-disable-undo)
3073   (setq buffer-read-only t)
3074   (set-syntax-table gnus-article-mode-syntax-table)
3075   (gnus-run-hooks 'gnus-article-mode-hook))
3076
3077 (defun gnus-article-setup-buffer ()
3078   "Initialize the article buffer."
3079   (let* ((name (if gnus-single-article-buffer "*Article*"
3080                  (concat "*Article " gnus-newsgroup-name "*")))
3081          (original
3082           (progn (string-match "\\*Article" name)
3083                  (concat " *Original Article"
3084                          (substring name (match-end 0))))))
3085     (setq gnus-article-buffer name)
3086     (setq gnus-original-article-buffer original)
3087     (setq gnus-article-mime-handle-alist nil)
3088     ;; This might be a variable local to the summary buffer.
3089     (unless gnus-single-article-buffer
3090       (save-excursion
3091         (set-buffer gnus-summary-buffer)
3092         (setq gnus-article-buffer name)
3093         (setq gnus-original-article-buffer original)
3094         (gnus-set-global-variables)))
3095     (gnus-article-setup-highlight-words)
3096     ;; Init original article buffer.
3097     (save-excursion
3098       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3099       (set-buffer-multibyte nil)
3100       (setq major-mode 'gnus-original-article-mode)
3101       (make-local-variable 'gnus-original-article))
3102     (if (get-buffer name)
3103         (save-excursion
3104           (set-buffer name)
3105           (buffer-disable-undo)
3106           (setq buffer-read-only t)
3107           (unless (eq major-mode 'gnus-article-mode)
3108             (gnus-article-mode))
3109           (current-buffer))
3110       (save-excursion
3111         (set-buffer (gnus-get-buffer-create name))
3112         (gnus-article-mode)
3113         (make-local-variable 'gnus-summary-buffer)
3114         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3115         (current-buffer)))))
3116
3117 ;; Set article window start at LINE, where LINE is the number of lines
3118 ;; from the head of the article.
3119 (defun gnus-article-set-window-start (&optional line)
3120   (set-window-start
3121    (get-buffer-window gnus-article-buffer t)
3122    (save-excursion
3123      (set-buffer gnus-article-buffer)
3124      (goto-char (point-min))
3125      (if (not line)
3126          (point-min)
3127        (gnus-message 6 "Moved to bookmark")
3128        (search-forward "\n\n" nil t)
3129        (forward-line line)
3130        (point)))))
3131
3132 ;;; @@ article filters
3133 ;;;
3134
3135 (defun gnus-article-display-mime-message ()
3136   "Article display method for MIME message."
3137   ;; called from `gnus-original-article-buffer'.
3138   (let (charset all-headers)
3139     (with-current-buffer gnus-summary-buffer
3140       (setq charset default-mime-charset
3141             all-headers gnus-have-all-headers))
3142     (make-local-variable 'default-mime-charset)
3143     (setq default-mime-charset charset)
3144     (with-current-buffer (get-buffer-create gnus-article-buffer)
3145       (make-local-variable 'default-mime-charset)
3146       (setq default-mime-charset charset))
3147     (mime-display-message mime-message-structure
3148                           gnus-article-buffer nil gnus-article-mode-map)
3149     (when all-headers
3150       (gnus-article-hide-headers nil -1)))
3151   (run-hooks 'gnus-mime-article-prepare-hook))
3152
3153 (defun gnus-article-display-traditional-message ()
3154   "Article display method for traditional message."
3155   (set-buffer gnus-article-buffer)
3156   (let (buffer-read-only)
3157     (erase-buffer)
3158     (insert-buffer-substring gnus-original-article-buffer)))
3159
3160 (defun gnus-article-make-full-mail-header (&optional number charset)
3161   "Create a new mail header structure in a raw article buffer."
3162   (unless (and number charset)
3163     (save-current-buffer
3164       (set-buffer gnus-summary-buffer)
3165       (unless number
3166         (setq number (or (cdr gnus-article-current) 0)))
3167       (unless charset
3168         (setq charset (or default-mime-charset 'x-ctext)))))
3169   (goto-char (point-min))
3170   (let ((header-end (if (search-forward "\n\n" nil t)
3171                         (1- (point))
3172                       (goto-char (point-max))))
3173         (chars (- (point-max) (point)))
3174         (lines (count-lines (point) (point-max)))
3175         (default-mime-charset charset)
3176         xref)
3177     (narrow-to-region (point-min) header-end)
3178     (setq xref (std11-fetch-field "xref"))
3179     (prog1
3180         (make-full-mail-header
3181          number
3182          (std11-fetch-field "subject")
3183          (std11-fetch-field "from")
3184          (std11-fetch-field "date")
3185          (std11-fetch-field "message-id")
3186          (std11-fetch-field "references")
3187          chars
3188          lines
3189          (when xref (concat "Xref: " xref)))
3190       (widen))))
3191
3192 (defun gnus-article-prepare (article &optional all-headers header)
3193   "Prepare ARTICLE in article mode buffer.
3194 ARTICLE should either be an article number or a Message-ID.
3195 If ARTICLE is an id, HEADER should be the article headers.
3196 If ALL-HEADERS is non-nil, no headers are hidden."
3197   (save-excursion
3198     ;; Make sure we start in a summary buffer.
3199     (unless (eq major-mode 'gnus-summary-mode)
3200       (set-buffer gnus-summary-buffer))
3201     (setq gnus-summary-buffer (current-buffer))
3202     (let* ((gnus-article (if header (mail-header-number header) article))
3203            (summary-buffer (current-buffer))
3204            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3205            (group gnus-newsgroup-name)
3206            result)
3207       (save-excursion
3208         (gnus-article-setup-buffer)
3209         (set-buffer gnus-original-article-buffer)
3210         ;; Deactivate active regions.
3211         (when (and (boundp 'transient-mark-mode)
3212                    transient-mark-mode)
3213           (setq mark-active nil))
3214         (if (not (setq result (let ((buffer-read-only nil))
3215                                 (gnus-request-article-this-buffer
3216                                  article group))))
3217             ;; There is no such article.
3218             (save-excursion
3219               (when (and (numberp article)
3220                          (not (memq article gnus-newsgroup-sparse)))
3221                 (setq gnus-article-current
3222                       (cons gnus-newsgroup-name article))
3223                 (set-buffer gnus-summary-buffer)
3224                 (setq gnus-current-article article)
3225                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
3226                     (progn
3227                       (gnus-summary-set-agent-mark article)
3228                       (message "Message marked for downloading"))
3229                   (gnus-summary-mark-article article gnus-canceled-mark)
3230                   (unless (memq article gnus-newsgroup-sparse)
3231                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3232           (if (or (eq result 'pseudo)
3233                   (eq result 'nneething))
3234               (progn
3235                 (save-excursion
3236                   (set-buffer summary-buffer)
3237                   (push article gnus-newsgroup-history)
3238                   (setq gnus-last-article gnus-current-article
3239                         gnus-current-article 0
3240                         gnus-current-headers nil
3241                         gnus-article-current nil)
3242                   (if (eq result 'nneething)
3243                       (gnus-configure-windows 'summary)
3244                     (gnus-configure-windows 'article))
3245                   (gnus-set-global-variables))
3246                 (let ((gnus-article-mime-handle-alist-1
3247                        gnus-article-mime-handle-alist))
3248                   (gnus-set-mode-line 'article)))
3249             ;; The result from the `request' was an actual article -
3250             ;; or at least some text that is now displayed in the
3251             ;; article buffer.
3252             (when (and (numberp article)
3253                        (not (eq article gnus-current-article)))
3254               ;; Seems like a new article has been selected.
3255               ;; `gnus-current-article' must be an article number.
3256               (save-excursion
3257                 (set-buffer summary-buffer)
3258                 (push article gnus-newsgroup-history)
3259                 (setq gnus-last-article gnus-current-article
3260                       gnus-current-article article
3261                       gnus-current-headers
3262                       (gnus-summary-article-header gnus-current-article)
3263                       gnus-article-current
3264                       (cons gnus-newsgroup-name gnus-current-article))
3265                 (unless (vectorp gnus-current-headers)
3266                   (setq gnus-current-headers nil))
3267                 (gnus-summary-goto-subject gnus-current-article)
3268                 (when (gnus-summary-show-thread)
3269                   ;; If the summary buffer really was folded, the
3270                   ;; previous goto may not actually have gone to
3271                   ;; the right article, but the thread root instead.
3272                   ;; So we go again.
3273                   (gnus-summary-goto-subject gnus-current-article))
3274                 (gnus-run-hooks 'gnus-mark-article-hook)
3275                 (gnus-set-mode-line 'summary)
3276                 (when (gnus-visual-p 'article-highlight 'highlight)
3277                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3278                 ;; Set the global newsgroup variables here.
3279                 (gnus-set-global-variables)
3280                 (setq gnus-have-all-headers
3281                       (or all-headers gnus-show-all-headers))))
3282             (save-excursion
3283               (gnus-configure-windows 'article))
3284             (when (or (numberp article)
3285                       (stringp article))
3286               (gnus-article-prepare-display)
3287               ;; Do page break.
3288               (goto-char (point-min))
3289               (setq gnus-page-broken
3290                     (when gnus-break-pages
3291                       (gnus-narrow-to-page)
3292                       t)))
3293             (let ((gnus-article-mime-handle-alist-1
3294                    gnus-article-mime-handle-alist))
3295               (gnus-set-mode-line 'article))
3296             (article-goto-body)
3297             (set-window-point (get-buffer-window (current-buffer)) (point))
3298             (gnus-configure-windows 'article)
3299             t))))))
3300
3301 (defun gnus-article-prepare-mime-display (&optional number)
3302   (goto-char (point-min))
3303   (when (re-search-forward "^[^\t ]+:" nil t)
3304     (goto-char (match-beginning 0)))
3305   (let ((entity (if (eq 1 (point-min))
3306                     (get-text-property 1 'mime-view-entity)
3307                   (get-text-property (point) 'mime-view-entity)))
3308         last-entity child-entity next type)
3309     (setq child-entity (mime-entity-children entity))
3310     (if child-entity
3311         (setq last-entity (nth (1- (length child-entity))
3312                                child-entity))
3313       (setq last-entity entity))
3314     (save-restriction
3315       (narrow-to-region (point)
3316                         (if (search-forward "\n\n" nil t)
3317                             (point)
3318                           (point-max)))
3319       (gnus-treat-article 'head)
3320       (put-text-property (point-min) (point-max) 'article-treated-header t)
3321       (goto-char (point-max)))
3322     (while (and (not (eobp)) entity)
3323       (setq next (set-marker
3324                   (make-marker)
3325                   (next-single-property-change (point) 'mime-view-entity
3326                                                nil (point-max))))
3327       (let ((types (mime-entity-content-type entity)))
3328         (while (eq 'multipart (mime-content-type-primary-type types))
3329           (setq entity (car (mime-entity-children entity))
3330                 types (mime-entity-content-type entity)))
3331         (when types
3332           (setq type (format "%s/%s"
3333                              (mime-content-type-primary-type types)
3334                              (mime-content-type-subtype types)))))
3335       (if (string-equal type "message/rfc822")
3336           (progn
3337             (setq next (point))
3338             (let ((children (mime-entity-children entity))
3339                   last-children)
3340               (when children
3341                 (setq last-children (nth (1- (length children)) children))
3342                 (while
3343                     (and
3344                      (not (eq last-children
3345                               (get-text-property next 'mime-view-entity)))
3346                      (setq next
3347                            (next-single-property-change next
3348                                                         'mime-view-entity
3349                                                         nil (point-max)))))))
3350             (setq next (next-single-property-change next 'mime-view-entity
3351                                                     nil (point-max)))
3352             (save-restriction
3353               (narrow-to-region (point) next)
3354               (gnus-article-prepare-mime-display)
3355               (goto-char (point-max)))
3356             (setq entity (get-text-property (point) 'mime-view-entity)))
3357         (save-restriction
3358           (narrow-to-region (point) next)
3359           ;; Kludge. We have to count true number, but for now,
3360           ;; part number is here only to achieve `last'.
3361           (gnus-treat-article nil 1
3362                               (if (eq entity last-entity)
3363                                   1 2)
3364                               type)
3365           (goto-char (point-max)))
3366         (setq entity (get-text-property next 'mime-view-entity))))))
3367
3368 ;;;###autoload
3369 (defun gnus-article-prepare-display ()
3370   "Make the current buffer look like a nice article."
3371   (setq gnus-article-wash-types nil)
3372   (gnus-run-hooks 'gnus-tmp-internal-hook)
3373   (gnus-run-hooks 'gnus-article-prepare-hook)
3374   ;; Display message.
3375   (let (mime-display-header-hook mime-display-text/plain-hook)
3376     (funcall (if gnus-show-mime
3377                  (progn
3378                    (setq mime-message-structure gnus-current-headers)
3379                    (mime-buffer-entity-set-buffer-internal
3380                     mime-message-structure
3381                     gnus-original-article-buffer)
3382                    (mime-entity-set-representation-type-internal
3383                     mime-message-structure 'mime-buffer-entity)
3384                    (luna-send mime-message-structure
3385                               'initialize-instance
3386                               mime-message-structure)
3387                    gnus-article-display-method-for-mime)
3388                gnus-article-display-method-for-traditional)))
3389   ;; Associate this article with the current summary buffer.
3390   (setq gnus-article-current-summary gnus-summary-buffer)
3391   ;; Call the treatment functions.
3392   (let ((inhibit-read-only t)
3393         buffer-read-only)
3394     (save-restriction
3395       (widen)
3396       (if gnus-show-mime
3397           (gnus-article-prepare-mime-display)
3398         (narrow-to-region (goto-char (point-min))
3399                           (if (search-forward "\n\n" nil t)
3400                               (point)
3401                             (point-max)))
3402         (gnus-treat-article 'head)
3403         (put-text-property (point-min) (point-max) 'article-treated-header t)
3404         (goto-char (point-max))
3405         (widen)
3406         (narrow-to-region (point) (point-max))
3407         (gnus-treat-article nil))
3408       (put-text-property (point-min) (point-max) 'read-only nil)))
3409   ;; Perform the article display hooks.  Incidentally, this hook is
3410   ;; an obsolete variable by now.
3411   (gnus-run-hooks 'gnus-article-display-hook))
3412
3413 (defun gnus-article-decode-article-as-default-mime-charset ()
3414   "Decode an article as `default-mime-charset'.  It won't work if the
3415 value of the variable `gnus-show-mime' is non-nil."
3416   (unless gnus-show-mime
3417     (decode-mime-charset-region (point-min) (point-max)
3418                                 (with-current-buffer gnus-summary-buffer
3419                                   default-mime-charset))))
3420
3421 ;;;
3422 ;;; Gnus MIME viewing functions
3423 ;;;
3424
3425 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3426   "The following specs can be used:
3427 %t  The MIME type
3428 %T  MIME type, along with additional info
3429 %n  The `name' parameter
3430 %d  The description, if any
3431 %l  The length of the encoded part
3432 %p  The part identifier number
3433 %e  Dots if the part isn't displayed")
3434
3435 (defvar gnus-mime-button-line-format-alist
3436   '((?t gnus-tmp-type ?s)
3437     (?T gnus-tmp-type-long ?s)
3438     (?n gnus-tmp-name ?s)
3439     (?d gnus-tmp-description ?s)
3440     (?p gnus-tmp-id ?s)
3441     (?l gnus-tmp-length ?d)
3442     (?e gnus-tmp-dots ?s)))
3443
3444 (defvar gnus-mime-button-commands
3445   '((gnus-article-press-button "\r" "Toggle Display")
3446     (gnus-mime-view-part "v" "View Interactively...")
3447     (gnus-mime-view-part-as-type "t" "View As Type...")
3448     (gnus-mime-view-part-as-charset "C" "View As charset...")
3449     (gnus-mime-save-part "o" "Save...")
3450     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
3451     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
3452     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
3453     (gnus-mime-internalize-part "E" "View Internally")
3454     (gnus-mime-externalize-part "e" "View Externally")
3455     (gnus-mime-pipe-part "|" "Pipe To Command...")
3456     (gnus-mime-action-on-part "." "Take action on the part")))
3457
3458 (defun gnus-article-mime-part-status ()
3459   (with-current-buffer gnus-article-buffer
3460     (let ((entity (get-text-property (point-min) 'mime-view-entity)))
3461       (if (and entity (mime-entity-children entity))
3462           (format " (%d parts)" (length (mime-entity-children entity)))
3463         ""))))
3464
3465 (defvar gnus-mime-button-map
3466   (let ((map (make-sparse-keymap)))
3467     ;; Not for Emacs 21: fixme better.
3468     ;; (set-keymap-parent map gnus-article-mode-map)
3469     (define-key map gnus-mouse-2 'gnus-article-push-button)
3470     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
3471     (dolist (c gnus-mime-button-commands)
3472       (define-key map (cadr c) (car c)))
3473     map))
3474
3475 (defun gnus-mime-button-menu (event)
3476   "Construct a context-sensitive menu of MIME commands."
3477   (interactive "e")
3478   (save-window-excursion
3479     (let ((pos (event-start event)))
3480       (select-window (posn-window pos))
3481       (goto-char (posn-point pos))
3482       (gnus-article-check-buffer)
3483       (let ((response (x-popup-menu
3484                        t `("MIME Part"
3485                            ("" ,@(mapcar (lambda (c)
3486                                            (cons (caddr c) (car c)))
3487                                          gnus-mime-button-commands))))))
3488         (if response
3489             (call-interactively response))))))
3490
3491 (defun gnus-mime-view-all-parts (&optional handles)
3492   "View all the MIME parts."
3493   (interactive)
3494   (save-current-buffer
3495     (set-buffer gnus-article-buffer)
3496     (let ((handles (or handles gnus-article-mime-handles))
3497           (mail-parse-charset gnus-newsgroup-charset)
3498           (mail-parse-ignored-charsets 
3499            (with-current-buffer gnus-summary-buffer
3500              gnus-newsgroup-ignored-charsets)))
3501       (mm-remove-parts handles)
3502       (goto-char (point-min))
3503       (or (search-forward "\n\n") (goto-char (point-max)))
3504       (let (buffer-read-only)
3505         (delete-region (point) (point-max)))
3506       (mm-display-parts handles))))
3507
3508 (defun gnus-mime-save-part-and-strip ()
3509   "Save the MIME part under point then replace it with an external body."
3510   (interactive)
3511   (gnus-article-check-buffer)
3512   (let* ((data (get-text-property (point) 'gnus-data)) 
3513          (file (mm-save-part data))
3514          param)
3515     (when file
3516       (with-current-buffer (mm-handle-buffer data)
3517         (erase-buffer)
3518         (insert "Content-Type: " (mm-handle-media-type data))
3519         (mml-insert-parameter-string (cdr (mm-handle-type data))
3520                                      '(charset))
3521         (insert "\n")
3522         (insert "Content-ID: " (message-make-message-id) "\n")
3523         (insert "Content-Transfer-Encoding: binary\n")
3524         (insert "\n"))
3525       (setcdr data
3526               (cdr (mm-make-handle nil 
3527                                    `("message/external-body"
3528                                      (access-type . "LOCAL-FILE")
3529                                      (name . ,file)))))
3530       (set-buffer gnus-summary-buffer)
3531       (gnus-article-edit-article
3532        `(lambda () 
3533            (erase-buffer)
3534            (let ((mail-parse-charset (or gnus-article-charset 
3535                                          ',gnus-newsgroup-charset))
3536                  (mail-parse-ignored-charsets 
3537                   (or gnus-article-ignored-charsets
3538                       ',gnus-newsgroup-ignored-charsets))
3539                  (mbl mml-buffer-list))
3540              (setq mml-buffer-list nil)
3541              (insert-buffer gnus-original-article-buffer)
3542              (mime-to-mml gnus-article-mime-handles)
3543              (setq gnus-article-mime-handles nil)
3544              (make-local-hook 'kill-buffer-hook)
3545              (let ((mbl1 mml-buffer-list))
3546                (setq mml-buffer-list mbl)
3547                (set (make-local-variable 'mml-buffer-list) mbl1))
3548              (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
3549        `(lambda (no-highlight)
3550           (let ((mail-parse-charset (or gnus-article-charset
3551                                         ',gnus-newsgroup-charset))
3552                 (message-options message-options)
3553                 (message-options-set-recipient)
3554                 (mail-parse-ignored-charsets  
3555                  (or gnus-article-ignored-charsets
3556                      ',gnus-newsgroup-ignored-charsets)))
3557            (mml-to-mime)
3558            (mml-destroy-buffers)
3559            (remove-hook 'kill-buffer-hook 
3560                         'mml-destroy-buffers t)
3561            (kill-local-variable 'mml-buffer-list))
3562           (gnus-summary-edit-article-done
3563            ,(or (mail-header-references gnus-current-headers) "")
3564            ,(gnus-group-read-only-p) 
3565            ,gnus-summary-buffer no-highlight))))))
3566
3567 (defun gnus-mime-save-part ()
3568   "Save the MIME part under point."
3569   (interactive)
3570   (gnus-article-check-buffer)
3571   (let ((data (get-text-property (point) 'gnus-data)))
3572     (mm-save-part data)))
3573
3574 (defun gnus-mime-pipe-part ()
3575   "Pipe the MIME part under point to a process."
3576   (interactive)
3577   (gnus-article-check-buffer)
3578   (let ((data (get-text-property (point) 'gnus-data)))
3579     (mm-pipe-part data)))
3580
3581 (defun gnus-mime-view-part ()
3582   "Interactively choose a viewing method for the MIME part under point."
3583   (interactive)
3584   (gnus-article-check-buffer)
3585   (let ((data (get-text-property (point) 'gnus-data)))
3586     (mm-interactively-view-part data)))
3587
3588 (defun gnus-mime-view-part-as-type-internal ()
3589   (gnus-article-check-buffer)
3590   (let* ((name (mail-content-type-get
3591                 (mm-handle-type (get-text-property (point) 'gnus-data))
3592                 'name))
3593          (def-type (and name (mm-default-file-encoding name))))
3594     (and def-type (cons def-type 0))))
3595
3596 (defun gnus-mime-view-part-as-type (mime-type)
3597   "Choose a MIME media type, and view the part as such."
3598   (interactive
3599    (list (completing-read
3600           "View as MIME type: "
3601           (mapcar #'list (mailcap-mime-types))
3602           nil nil
3603           (gnus-mime-view-part-as-type-internal))))
3604   (gnus-article-check-buffer)
3605   (let ((handle (get-text-property (point) 'gnus-data)))
3606     (gnus-mm-display-part
3607      (mm-make-handle (mm-handle-buffer handle)
3608                      (cons mime-type (cdr (mm-handle-type handle)))
3609                      (mm-handle-encoding handle)
3610                      (mm-handle-undisplayer handle)
3611                      (mm-handle-disposition handle)
3612                      (mm-handle-description handle)
3613                      (mm-handle-cache handle)
3614                      (mm-handle-id handle)))))
3615
3616 (defun gnus-mime-copy-part (&optional handle)
3617   "Put the the MIME part under point into a new buffer."
3618   (interactive)
3619   (gnus-article-check-buffer)
3620   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3621          (contents (mm-get-part handle))|
3622          (base (file-name-nondirectory
3623                 (or
3624                  (mail-content-type-get (mm-handle-type handle) 'name)
3625                  (mail-content-type-get (mm-handle-type handle)
3626                                         'filename)
3627                  "*decoded*")))
3628          (buffer (generate-new-buffer base)))
3629     (switch-to-buffer buffer)
3630     (insert contents)
3631     ;; We do it this way to make `normal-mode' set the appropriate mode.
3632     (unwind-protect
3633         (progn
3634           (setq buffer-file-name (expand-file-name base))
3635           (normal-mode))
3636       (setq buffer-file-name nil))
3637     (goto-char (point-min))))
3638
3639 (defun gnus-mime-inline-part (&optional handle arg)
3640   "Insert the MIME part under point into the current buffer."
3641   (interactive (list nil current-prefix-arg))
3642   (gnus-article-check-buffer)
3643   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3644          contents charset
3645          (b (point))
3646          buffer-read-only)
3647     (if (and (not arg) (mm-handle-undisplayer handle))
3648         (mm-remove-part handle)
3649       (setq contents (mm-get-part handle))
3650       (cond
3651        ((not arg)
3652         (setq charset (or (mail-content-type-get
3653                            (mm-handle-type handle) 'charset)
3654                           gnus-newsgroup-charset)))
3655        ((numberp arg)
3656         (if (mm-handle-undisplayer handle)
3657             (mm-remove-part handle))
3658         (setq charset
3659               (or (cdr (assq arg 
3660                              gnus-summary-show-article-charset-alist))
3661                   (read-coding-system "Charset: ")))))
3662       (forward-line 2)
3663       (mm-insert-inline handle
3664                         (if (and charset 
3665                                  (setq charset (mm-charset-to-coding-system 
3666                                                 charset))
3667                                  (not (eq charset 'ascii)))
3668                             (mm-decode-coding-string contents charset)
3669                           contents))
3670       (goto-char b))))
3671
3672 (defun gnus-mime-view-part-as-charset (&optional handle arg)
3673   "Insert the MIME part under point into the current buffer."
3674   (interactive (list nil current-prefix-arg))
3675   (gnus-article-check-buffer)
3676   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3677          contents charset
3678          (b (point))
3679          buffer-read-only)
3680     (if (mm-handle-undisplayer handle)
3681         (mm-remove-part handle))
3682     (let ((gnus-newsgroup-charset
3683            (or (cdr (assq arg 
3684                           gnus-summary-show-article-charset-alist))
3685                (read-coding-system "Charset: ")))
3686           (gnus-newsgroup-ignored-charsets 'gnus-all))
3687       (gnus-article-press-button))))
3688
3689 (defun gnus-mime-externalize-part (&optional handle)
3690   "View the MIME part under point with an external viewer."
3691   (interactive)
3692   (gnus-article-check-buffer)
3693   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3694          (mm-user-display-methods nil)
3695          (mm-inlined-types nil)
3696          (mail-parse-charset gnus-newsgroup-charset)
3697          (mail-parse-ignored-charsets 
3698           (save-excursion (set-buffer gnus-summary-buffer)
3699                           gnus-newsgroup-ignored-charsets)))
3700     (if (mm-handle-undisplayer handle)
3701         (mm-remove-part handle)
3702       (mm-display-part handle))))
3703
3704 (defun gnus-mime-internalize-part (&optional handle)
3705   "View the MIME part under point with an internal viewer.
3706 In no internal viewer is available, use an external viewer."
3707   (interactive)
3708   (gnus-article-check-buffer)
3709   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3710          (mm-inlined-types '(".*"))
3711          (mm-inline-large-images t)
3712          (mail-parse-charset gnus-newsgroup-charset)
3713          (mail-parse-ignored-charsets 
3714           (save-excursion (set-buffer gnus-summary-buffer)
3715                           gnus-newsgroup-ignored-charsets)))
3716     (if (mm-handle-undisplayer handle)
3717         (mm-remove-part handle)
3718       (mm-display-part handle))))
3719
3720 (defun gnus-mime-action-on-part (&optional action)
3721   "Do something with the MIME attachment at \(point\)."
3722   (interactive
3723    (list (completing-read "Action: " gnus-mime-action-alist)))
3724   (gnus-article-check-buffer)
3725   (let ((action-pair (assoc action gnus-mime-action-alist)))
3726     (if action-pair
3727         (funcall (cdr action-pair)))))
3728
3729 (defun gnus-article-part-wrapper (n function)
3730   (save-current-buffer
3731     (set-buffer gnus-article-buffer)
3732     (when (> n (length gnus-article-mime-handle-alist))
3733       (error "No such part"))
3734     (gnus-article-goto-part n)
3735     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3736       (funcall function handle))))
3737
3738 (defun gnus-article-pipe-part (n)
3739   "Pipe MIME part N, which is the numerical prefix."
3740   (interactive "p")
3741   (gnus-article-part-wrapper n 'mm-pipe-part))
3742
3743 (defun gnus-article-save-part (n)
3744   "Save MIME part N, which is the numerical prefix."
3745   (interactive "p")
3746   (gnus-article-part-wrapper n 'mm-save-part))
3747
3748 (defun gnus-article-interactively-view-part (n)
3749   "View MIME part N interactively, which is the numerical prefix."
3750   (interactive "p")
3751   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3752
3753 (defun gnus-article-copy-part (n)
3754   "Copy MIME part N, which is the numerical prefix."
3755   (interactive "p")
3756   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3757
3758 (defun gnus-article-view-part-as-charset (n)
3759   "Copy MIME part N, which is the numerical prefix."
3760   (interactive "p")
3761   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
3762
3763 (defun gnus-article-externalize-part (n)
3764   "View MIME part N externally, which is the numerical prefix."
3765   (interactive "p")
3766   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3767
3768 (defun gnus-article-inline-part (n)
3769   "Inline MIME part N, which is the numerical prefix."
3770   (interactive "p")
3771   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3772
3773 (defun gnus-article-mime-match-handle-first (condition)
3774   (if condition
3775       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3776         (while (setq ihandle (pop alist))
3777           (if (and (cond 
3778                     ((functionp condition)
3779                      (funcall condition (cdr ihandle)))
3780                     ((eq condition 'undisplayed) 
3781                      (not (or (mm-handle-undisplayer (cdr ihandle))
3782                               (equal (mm-handle-media-type (cdr ihandle))
3783                                      "multipart/alternative"))))
3784                     ((eq condition 'undisplayed-alternative)
3785                      (not (mm-handle-undisplayer (cdr ihandle))))
3786                     (t t))
3787                    (gnus-article-goto-part (car ihandle))
3788                    (or (not n) (< (car ihandle) n)))
3789               (setq n (car ihandle))))
3790         (or n 1))
3791     1))
3792
3793 (defun gnus-article-view-part (&optional n)
3794   "View MIME part N, which is the numerical prefix."
3795   (interactive "P")
3796   (save-current-buffer
3797     (set-buffer gnus-article-buffer)
3798     (or (numberp n) (setq n (gnus-article-mime-match-handle-first 
3799                              gnus-article-mime-match-handle-function)))
3800     (when (> n (length gnus-article-mime-handle-alist))
3801       (error "No such part"))
3802     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3803       (when (gnus-article-goto-part n)
3804         (if (equal (car handle) "multipart/alternative")
3805             (gnus-article-press-button)
3806           (when (eq (gnus-mm-display-part handle) 'internal)
3807             (gnus-set-window-start)))))))
3808
3809 (defsubst gnus-article-mime-total-parts ()
3810   (if (bufferp (car gnus-article-mime-handles))
3811       1 ;; single part
3812     (1- (length gnus-article-mime-handles))))
3813
3814 (defun gnus-mm-display-part (handle)
3815   "Display HANDLE and fix MIME button."
3816   (let ((id (get-text-property (point) 'gnus-part))
3817         (point (point))
3818         buffer-read-only)
3819     (forward-line 1)
3820     (prog1
3821         (let ((window (selected-window))
3822               (mail-parse-charset gnus-newsgroup-charset)
3823               (mail-parse-ignored-charsets 
3824                (save-excursion (set-buffer gnus-summary-buffer)
3825                                gnus-newsgroup-ignored-charsets)))
3826           (save-excursion
3827             (unwind-protect
3828                 (let ((win (get-buffer-window (current-buffer) t))
3829                       (beg (point)))
3830                   (when win
3831                     (select-window win))
3832                   (goto-char point)
3833                   (forward-line)
3834                   (if (mm-handle-displayed-p handle)
3835                       ;; This will remove the part.
3836                       (mm-display-part handle)
3837                     (save-restriction
3838                       (narrow-to-region (point) (1+ (point)))
3839                       (mm-display-part handle)
3840                       ;; We narrow to the part itself and
3841                       ;; then call the treatment functions.
3842                       (goto-char (point-min))
3843                       (forward-line 1)
3844                       (narrow-to-region (point) (point-max))
3845                       (gnus-treat-article
3846                        nil id
3847                        (gnus-article-mime-total-parts)
3848                        (mm-handle-media-type handle)))))
3849               (select-window window))))
3850       (goto-char point)
3851       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
3852       (gnus-insert-mime-button
3853        handle id (list (mm-handle-displayed-p handle)))
3854       (goto-char point))))
3855
3856 (defun gnus-article-goto-part (n)
3857   "Go to MIME part N."
3858   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
3859     (when point
3860       (goto-char point))))
3861
3862 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
3863   (let ((gnus-tmp-name
3864          (or (mail-content-type-get (mm-handle-type handle)
3865                                     'name)
3866              (mail-content-type-get (mm-handle-disposition handle)
3867                                     'filename)
3868              (mail-content-type-get (mm-handle-type handle)
3869                                     'url)
3870              ""))
3871         (gnus-tmp-type (mm-handle-media-type handle))
3872         (gnus-tmp-description
3873          (mail-decode-encoded-word-string (or (mm-handle-description handle)
3874                                               "")))
3875         (gnus-tmp-dots
3876          (if (if displayed (car displayed)
3877                (mm-handle-displayed-p handle))
3878              "" "..."))
3879         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
3880                            (buffer-size)))
3881         gnus-tmp-type-long b e)
3882     (when (string-match ".*/" gnus-tmp-name)
3883       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
3884     (setq gnus-tmp-type-long (concat gnus-tmp-type
3885                                      (and (not (equal gnus-tmp-name ""))
3886                                           (concat "; " gnus-tmp-name))))
3887     (or (equal gnus-tmp-description "")
3888         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
3889     (unless (bolp)
3890       (insert "\n"))
3891     (setq b (point))
3892     (gnus-eval-format
3893      gnus-mime-button-line-format gnus-mime-button-line-format-alist
3894      `(keymap ,gnus-mime-button-map
3895               ,@(if (>= (string-to-number emacs-version) 21)
3896                     nil 
3897                   (list 'local-map gnus-mime-button-map))
3898               gnus-callback gnus-mm-display-part
3899               gnus-part ,gnus-tmp-id
3900               article-type annotation
3901               gnus-data ,handle))
3902     (setq e (point))
3903     (widget-convert-button
3904      'link b e
3905      :mime-handle handle
3906      :action 'gnus-widget-press-button
3907      :button-keymap gnus-mime-button-map
3908      :help-echo
3909      (lambda (widget/window &optional overlay pos)
3910        ;; Needed to properly clear the message due to a bug in
3911        ;; wid-edit (XEmacs only).
3912        (if (boundp 'help-echo-owns-message)
3913            (setq help-echo-owns-message t))
3914        (format
3915         "%S: %s the MIME part; %S: more options"
3916         (aref gnus-mouse-2 0)
3917         ;; XEmacs will get a single widget arg; Emacs 21 will get
3918         ;; window, overlay, position.
3919         (if (mm-handle-displayed-p
3920              (if overlay
3921                  (with-current-buffer (gnus-overlay-buffer overlay)
3922                    (widget-get (widget-at (gnus-overlay-start overlay))
3923                                :mime-handle))
3924                (widget-get widget/window :mime-handle)))
3925             "hide" "show")
3926         (aref gnus-down-mouse-3 0))))))
3927
3928 (defun gnus-widget-press-button (elems el)
3929   (goto-char (widget-get elems :from))
3930   (gnus-article-press-button))
3931
3932 (defvar gnus-displaying-mime nil)
3933
3934 (defun gnus-display-mime (&optional ihandles)
3935   "Display the MIME parts."
3936   (save-excursion
3937     (save-selected-window
3938       (let ((window (get-buffer-window gnus-article-buffer))
3939             (point (point)))
3940         (when window
3941           (select-window window)
3942           ;; We have to do this since selecting the window
3943           ;; may change the point.  So we set the window point.
3944           (set-window-point window point)))
3945       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3946              buffer-read-only handle name type b e display)
3947         (when (and (not ihandles)
3948                    (not gnus-displaying-mime))
3949           ;; Top-level call; we clean up.
3950           (when gnus-article-mime-handles
3951             (mm-destroy-parts gnus-article-mime-handles)
3952             (setq gnus-article-mime-handle-alist nil));; A trick.
3953           (setq gnus-article-mime-handles handles)
3954           ;; We allow users to glean info from the handles.
3955           (when gnus-article-mime-part-function
3956             (gnus-mime-part-function handles)))
3957         (if (and handles
3958                  (or (not (stringp (car handles)))
3959                      (cdr handles)))
3960             (progn
3961               (when (and (not ihandles)
3962                          (not gnus-displaying-mime))
3963                 ;; Clean up for mime parts.
3964                 (article-goto-body)
3965                 (delete-region (point) (point-max)))
3966               (let ((gnus-displaying-mime t))
3967                 (gnus-mime-display-part handles)))
3968           (save-restriction
3969             (article-goto-body)
3970             (narrow-to-region (point) (point-max))
3971             (gnus-treat-article nil 1 1)
3972             (widen)))
3973         (unless ihandles
3974           ;; Highlight the headers.
3975           (save-excursion
3976             (save-restriction
3977               (article-goto-body)
3978               (narrow-to-region (point-min) (point))
3979               (gnus-treat-article 'head))))))))
3980
3981 (defvar gnus-mime-display-multipart-as-mixed nil)
3982
3983 (defun gnus-mime-display-part (handle)
3984   (cond
3985    ;; Single part.
3986    ((not (stringp (car handle)))
3987     (gnus-mime-display-single handle))
3988    ;; User-defined multipart
3989    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
3990     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
3991              handle))
3992    ;; multipart/alternative
3993    ((and (equal (car handle) "multipart/alternative")
3994          (not gnus-mime-display-multipart-as-mixed))
3995     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3996       (push (cons id handle) gnus-article-mime-handle-alist)
3997       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3998    ;; multipart/related
3999    ((and (equal (car handle) "multipart/related")
4000          (not gnus-mime-display-multipart-as-mixed))
4001     ;;;!!!We should find the start part, but we just default
4002     ;;;!!!to the first part.
4003     ;;(gnus-mime-display-part (cadr handle))
4004     ;;;!!! Most multipart/related is an HTML message plus images.
4005     ;;;!!! Unfortunately we are unable to let W3 display those 
4006     ;;;!!! included images, so we just display it as a mixed multipart.
4007     (gnus-mime-display-mixed (cdr handle)))
4008    ((equal (car handle) "multipart/signed")
4009     (or (memq 'signed gnus-article-wash-types)
4010         (push 'signed gnus-article-wash-types))
4011     (gnus-mime-display-security handle))
4012    ((equal (car handle) "multipart/encrypted")
4013     (or (memq 'encrypted gnus-article-wash-types)
4014         (push 'encrypted gnus-article-wash-types))
4015     (gnus-mime-display-security handle))
4016    ;; Other multiparts are handled like multipart/mixed.
4017    (t
4018     (gnus-mime-display-mixed (cdr handle)))))
4019
4020 (defun gnus-mime-part-function (handles)
4021   (if (stringp (car handles))
4022       (mapcar 'gnus-mime-part-function (cdr handles))
4023     (funcall gnus-article-mime-part-function handles)))
4024
4025 (defun gnus-mime-display-mixed (handles)
4026   (mapcar 'gnus-mime-display-part handles))
4027
4028 (defun gnus-mime-display-single (handle)
4029   (let ((type (mm-handle-media-type handle))
4030         (ignored gnus-ignored-mime-types)
4031         (not-attachment t)
4032         (move nil)
4033         display text)
4034     (catch 'ignored
4035       (progn
4036         (while ignored
4037           (when (string-match (pop ignored) type)
4038             (throw 'ignored nil)))
4039         (if (and (setq not-attachment
4040                        (and (not (mm-inline-override-p handle))
4041                             (or (not (mm-handle-disposition handle))
4042                                 (equal (car (mm-handle-disposition handle))
4043                                        "inline")
4044                                 (mm-attachment-override-p handle))))
4045                  (mm-automatic-display-p handle)
4046                  (or (mm-inlined-p handle)
4047                      (mm-automatic-external-display-p type)))
4048             (setq display t)
4049           (when (equal (mm-handle-media-supertype handle) "text")
4050             (setq text t)))
4051         (let ((id (1+ (length gnus-article-mime-handle-alist)))
4052               beg)
4053           (push (cons id handle) gnus-article-mime-handle-alist)
4054           (when (or (not display)
4055                     (not (gnus-unbuttonized-mime-type-p type)))
4056             ;(gnus-article-insert-newline)
4057             (gnus-insert-mime-button
4058              handle id (list (or display (and not-attachment text))))
4059             (gnus-article-insert-newline) 
4060             ;(gnus-article-insert-newline) 
4061             ;; Remember modify the number of forward lines.
4062             (setq move t))
4063           (setq beg (point))
4064           (cond
4065            (display
4066             (when move
4067               (forward-line -1)
4068               (setq beg (point)))
4069             (let ((mail-parse-charset gnus-newsgroup-charset)
4070                   (mail-parse-ignored-charsets 
4071                    (save-excursion (condition-case ()
4072                                        (set-buffer gnus-summary-buffer)
4073                                      (error))
4074                                    gnus-newsgroup-ignored-charsets)))
4075               (mm-display-part handle t))
4076             (goto-char (point-max)))
4077            ((and text not-attachment)
4078             (when move
4079               (forward-line -1)
4080               (setq beg (point)))
4081             (gnus-article-insert-newline)
4082             (mm-insert-inline handle (mm-get-part handle))
4083             (goto-char (point-max))))
4084           ;; Do highlighting.
4085           (save-excursion
4086             (save-restriction
4087               (narrow-to-region beg (point))
4088               (gnus-treat-article
4089                nil id 
4090                (gnus-article-mime-total-parts)
4091                (mm-handle-media-type handle)))))))))
4092
4093 (defun gnus-unbuttonized-mime-type-p (type)
4094   "Say whether TYPE is to be unbuttonized."
4095   (unless gnus-inhibit-mime-unbuttonizing
4096     (catch 'found
4097       (let ((types gnus-unbuttonized-mime-types))
4098         (while types
4099           (when (string-match (pop types) type)
4100             (throw 'found t)))))))
4101
4102 (defun gnus-article-insert-newline ()
4103   "Insert a newline, but mark it as undeletable."
4104   (gnus-put-text-property
4105    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
4106
4107 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
4108   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
4109          (ihandles handles)
4110          (point (point))
4111          handle buffer-read-only from props begend not-pref)
4112     (save-window-excursion
4113       (save-restriction
4114         (when ibegend
4115           (narrow-to-region (car ibegend)
4116                             (or (cdr ibegend)
4117                                 (progn
4118                                   (goto-char (car ibegend))
4119                                   (forward-line 2)
4120                                   (point))))
4121           (delete-region (point-min) (point-max))
4122           (mm-remove-parts handles))
4123         (setq begend (list (point-marker)))
4124         ;; Do the toggle.
4125         (unless (setq not-pref (cadr (member preferred ihandles)))
4126           (setq not-pref (car ihandles)))
4127         (when (or ibegend
4128                   (not preferred)
4129                   (not (gnus-unbuttonized-mime-type-p
4130                         "multipart/alternative")))
4131           (gnus-add-text-properties
4132            (setq from (point))
4133            (progn
4134              (insert (format "%d.  " id))
4135              (point))
4136            `(gnus-callback
4137              (lambda (handles)
4138                (unless ,(not ibegend)
4139                  (setq gnus-article-mime-handle-alist
4140                        ',gnus-article-mime-handle-alist))
4141                (gnus-mime-display-alternative
4142                 ',ihandles ',not-pref ',begend ,id))
4143              ,@(if (>= (string-to-number emacs-version) 21)
4144                    nil ;; XEmacs doesn't care
4145                  (list 'local-map gnus-mime-button-map))
4146              ,gnus-mouse-face-prop ,gnus-article-mouse-face
4147              face ,gnus-article-button-face
4148              keymap ,gnus-mime-button-map
4149              gnus-part ,id
4150              gnus-data ,handle))
4151           (widget-convert-button 'link from (point)
4152                                  :action 'gnus-widget-press-button
4153                                  :button-keymap gnus-widget-button-keymap)
4154           ;; Do the handles
4155           (while (setq handle (pop handles))
4156             (gnus-add-text-properties
4157              (setq from (point))
4158              (progn
4159                (insert (format "(%c) %-18s"
4160                                (if (equal handle preferred) ?* ? )
4161                                (mm-handle-media-type handle)))
4162                (point))
4163              `(gnus-callback
4164                (lambda (handles)
4165                  (unless ,(not ibegend)
4166                    (setq gnus-article-mime-handle-alist
4167                          ',gnus-article-mime-handle-alist))
4168                  (gnus-mime-display-alternative
4169                   ',ihandles ',handle ',begend ,id))
4170                ,@(if (>= (string-to-number emacs-version) 21)
4171                      nil ;; XEmacs doesn't care
4172                    (list 'local-map gnus-mime-button-map))
4173                ,gnus-mouse-face-prop ,gnus-article-mouse-face
4174                face ,gnus-article-button-face
4175                keymap ,gnus-mime-button-map
4176                gnus-part ,id
4177                gnus-data ,handle))
4178             (widget-convert-button 'link from (point)
4179                                    :action 'gnus-widget-press-button
4180                                    :button-keymap gnus-widget-button-keymap)
4181             (insert "  "))
4182           (insert "\n\n"))
4183         (when preferred
4184           (if (stringp (car preferred))
4185               (gnus-display-mime preferred)
4186             (let ((mail-parse-charset gnus-newsgroup-charset)
4187                   (mail-parse-ignored-charsets 
4188                    (save-excursion (set-buffer gnus-summary-buffer)
4189                                    gnus-newsgroup-ignored-charsets)))
4190               (mm-display-part preferred)
4191               ;; Do highlighting.
4192               (save-excursion
4193                 (save-restriction
4194                   (narrow-to-region (car begend) (point-max))
4195                   (gnus-treat-article
4196                    nil (length gnus-article-mime-handle-alist)
4197                    (gnus-article-mime-total-parts)
4198                    (mm-handle-media-type handle))))))
4199           (goto-char (point-max))
4200           (setcdr begend (point-marker)))))
4201     (when ibegend
4202       (goto-char point))))
4203
4204 (defun gnus-article-wash-status ()
4205   "Return a string which display status of article washing."
4206   (save-excursion
4207     (set-buffer gnus-article-buffer)
4208     (let ((cite (memq 'cite gnus-article-wash-types))
4209           (headers (memq 'headers gnus-article-wash-types))
4210           (boring (memq 'boring-headers gnus-article-wash-types))
4211           (pgp (memq 'pgp gnus-article-wash-types))
4212           (pem (memq 'pem gnus-article-wash-types))
4213           (signed (memq 'signed gnus-article-wash-types))
4214           (encrypted (memq 'encrypted gnus-article-wash-types))
4215           (signature (memq 'signature gnus-article-wash-types))
4216           (overstrike (memq 'overstrike gnus-article-wash-types))
4217           (emphasis (memq 'emphasis gnus-article-wash-types)))
4218       (format "%c%c%c%c%c%c%c"
4219               (if cite ?c ? )
4220               (if (or headers boring) ?h ? )
4221               (if (or pgp pem signed encrypted) ?p ? )
4222               (if signature ?s ? )
4223               (if overstrike ?o ? )
4224               (if gnus-show-mime ?m ? )
4225               (if emphasis ?e ? )))))
4226
4227 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
4228
4229 (defun gnus-article-maybe-hide-headers ()
4230   "Hide unwanted headers if `gnus-have-all-headers' is nil.
4231 Provided for backwards compatibility."
4232   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
4233                  (not (save-excursion (set-buffer gnus-summary-buffer)
4234                                       gnus-have-all-headers)))
4235              (not gnus-inhibit-hiding))
4236     (gnus-article-hide-headers)))
4237
4238 ;;; Article savers.
4239
4240 (defun gnus-output-to-file (file-name)
4241   "Append the current article to a file named FILE-NAME."
4242   (let ((artbuf (current-buffer)))
4243     (with-temp-buffer
4244       (insert-buffer-substring artbuf)
4245       ;; Append newline at end of the buffer as separator, and then
4246       ;; save it to file.
4247       (goto-char (point-max))
4248       (insert "\n")
4249       (write-region-as-binary (point-min) (point-max) file-name 'append)
4250       t)))
4251
4252 (defun gnus-narrow-to-page (&optional arg)
4253   "Narrow the article buffer to a page.
4254 If given a numerical ARG, move forward ARG pages."
4255   (interactive "P")
4256   (setq arg (if arg (prefix-numeric-value arg) 0))
4257   (save-excursion
4258     (set-buffer gnus-article-buffer)
4259     (goto-char (point-min))
4260     (widen)
4261     ;; Remove any old next/prev buttons.
4262     (when (gnus-visual-p 'page-marker)
4263       (let ((buffer-read-only nil))
4264         (gnus-remove-text-with-property 'gnus-prev)
4265         (gnus-remove-text-with-property 'gnus-next)))
4266     (when
4267         (cond ((< arg 0)
4268                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
4269               ((> arg 0)
4270                (re-search-forward page-delimiter nil 'move arg)))
4271       (goto-char (match-end 0)))
4272     (narrow-to-region
4273      (point)
4274      (if (re-search-forward page-delimiter nil 'move)
4275          (match-beginning 0)
4276        (point)))
4277     (when (and (gnus-visual-p 'page-marker)
4278                (not (= (point-min) 1)))
4279       (save-excursion
4280         (goto-char (point-min))
4281         (gnus-insert-prev-page-button)))
4282     (when (and (gnus-visual-p 'page-marker)
4283                (< (+ (point-max) 2) (buffer-size)))
4284       (save-excursion
4285         (goto-char (point-max))
4286         (gnus-insert-next-page-button)))))
4287
4288 ;; Article mode commands
4289
4290 (defun gnus-article-goto-next-page ()
4291   "Show the next page of the article."
4292   (interactive)
4293   (when (gnus-article-next-page)
4294     (goto-char (point-min))
4295     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
4296
4297 (defun gnus-article-goto-prev-page ()
4298   "Show the next page of the article."
4299   (interactive)
4300   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
4301     (gnus-article-prev-page nil)))
4302
4303 (defun gnus-article-next-page (&optional lines)
4304   "Show the next page of the current article.
4305 If end of article, return non-nil.  Otherwise return nil.
4306 Argument LINES specifies lines to be scrolled up."
4307   (interactive "p")
4308   (let ((start (window-start))
4309         end-of-buffer end-of-page)
4310     (save-excursion
4311       (move-to-window-line -1)
4312       (if (<= (point) start)
4313           (progn
4314             (forward-line 2)
4315             (setq start (point)))
4316         (forward-line 1)
4317         (setq start nil))
4318       (unless (or (cond ((eq (1+ (buffer-size)) (point))
4319                          (and (pos-visible-in-window-p)
4320                               (setq end-of-buffer t)))
4321                         ((eobp)
4322                          (setq end-of-page t)))
4323                   (not lines))
4324         (move-to-window-line lines)
4325         (unless (search-backward "\n\n" nil t)
4326           (setq start (point)))))
4327     (cond (end-of-buffer t)
4328           (end-of-page
4329            (gnus-narrow-to-page 1)
4330            nil)
4331           (t
4332            (if start
4333                (set-window-start (selected-window) start)
4334              (let (window-pixel-scroll-increment)
4335                (scroll-up lines)))
4336            nil))))
4337
4338 (defun gnus-article-prev-page (&optional lines)
4339   "Show previous page of current article.
4340 Argument LINES specifies lines to be scrolled down."
4341   (interactive "p")
4342   (let (beginning-of-buffer beginning-of-page)
4343     (save-excursion
4344       (move-to-window-line 0)
4345       (cond ((eq 1 (point))
4346              (setq beginning-of-buffer t))
4347             ((bobp)
4348              (setq beginning-of-page t))))
4349     (cond (beginning-of-buffer)
4350           (beginning-of-page
4351            (gnus-narrow-to-page -1))
4352           (t
4353            (condition-case nil
4354                (let (window-pixel-scroll-increment)
4355                  (scroll-down lines))
4356              (beginning-of-buffer
4357               (goto-char (point-min))))))))
4358
4359 (defun gnus-article-refer-article ()
4360   "Read article specified by message-id around point."
4361   (interactive)
4362   (let ((point (point)))
4363     (search-forward ">" nil t)          ;Move point to end of "<....>".
4364     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
4365         (let ((message-id (match-string 1)))
4366           (goto-char point)
4367           (set-buffer gnus-summary-buffer)
4368           (gnus-summary-refer-article message-id))
4369       (goto-char (point))
4370       (error "No references around point"))))
4371
4372 (defun gnus-article-show-summary ()
4373   "Reconfigure windows to show summary buffer."
4374   (interactive)
4375   (if (not (gnus-buffer-live-p gnus-summary-buffer))
4376       (error "There is no summary buffer for this article buffer")
4377     (gnus-article-set-globals)
4378     (gnus-configure-windows 'article)
4379     (gnus-summary-goto-subject gnus-current-article)
4380     (gnus-summary-position-point)))
4381
4382 (defun gnus-article-describe-briefly ()
4383   "Describe article mode commands briefly."
4384   (interactive)
4385   (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")))
4386
4387 (defun gnus-article-summary-command ()
4388   "Execute the last keystroke in the summary buffer."
4389   (interactive)
4390   (let ((obuf (current-buffer))
4391         (owin (current-window-configuration))
4392         func)
4393     (switch-to-buffer gnus-article-current-summary 'norecord)
4394     (setq func (lookup-key (current-local-map) (this-command-keys)))
4395     (call-interactively func)
4396     (set-buffer obuf)
4397     (set-window-configuration owin)
4398     (set-window-point (get-buffer-window (current-buffer)) (point))))
4399
4400 (defun gnus-article-summary-command-nosave ()
4401   "Execute the last keystroke in the summary buffer."
4402   (interactive)
4403   (let (func)
4404     (pop-to-buffer gnus-article-current-summary 'norecord)
4405     (setq func (lookup-key (current-local-map) (this-command-keys)))
4406     (call-interactively func)))
4407
4408 (defun gnus-article-check-buffer ()
4409   "Beep if not in an article buffer."
4410   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
4411     (error "Command invoked outside of a Gnus article buffer")))
4412
4413 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
4414   "Read a summary buffer key sequence and execute it from the article buffer."
4415   (interactive "P")
4416   (gnus-article-check-buffer)
4417   (let ((nosaves
4418          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
4419            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
4420            "=" "^" "\M-^" "|"))
4421         (nosave-but-article
4422          '("A\r"))
4423         (nosave-in-article
4424          '("\C-d"))
4425         (up-to-top
4426          '("n" "Gn" "p" "Gp"))
4427         keys new-sum-point)
4428     (save-excursion
4429       (set-buffer gnus-article-current-summary)
4430       (let (gnus-pick-mode)
4431         (push (or key last-command-event) unread-command-events)
4432         (setq keys (static-if (featurep 'xemacs)
4433                        (events-to-keys (read-key-sequence nil))
4434                      (read-key-sequence nil)))))
4435     (message "")
4436
4437     (if (or (member keys nosaves)
4438             (member keys nosave-but-article)
4439             (member keys nosave-in-article))
4440         (let (func)
4441           (save-window-excursion
4442             (pop-to-buffer gnus-article-current-summary 'norecord)
4443             ;; We disable the pick minor mode commands.
4444             (let (gnus-pick-mode)
4445               (setq func (lookup-key (current-local-map) keys))))
4446           (if (or (not func)
4447                   (numberp func))
4448               (ding)
4449             (unless (member keys nosave-in-article)
4450               (set-buffer gnus-article-current-summary))
4451             (call-interactively func)
4452             (setq new-sum-point (point)))
4453           (when (member keys nosave-but-article)
4454             (pop-to-buffer gnus-article-buffer 'norecord)))
4455       ;; These commands should restore window configuration.
4456       (let ((obuf (current-buffer))
4457             (owin (current-window-configuration))
4458             (opoint (point))
4459             (summary gnus-article-current-summary)
4460             func in-buffer selected)
4461         (if not-restore-window
4462             (pop-to-buffer summary 'norecord)
4463           (switch-to-buffer summary 'norecord))
4464         (setq in-buffer (current-buffer))
4465         ;; We disable the pick minor mode commands.
4466         (if (and (setq func (let (gnus-pick-mode)
4467                               (lookup-key (current-local-map) keys)))
4468                  (functionp func))
4469             (progn
4470               (call-interactively func)
4471               (setq new-sum-point (point))
4472               (when (eq in-buffer (current-buffer))
4473                 (setq selected (gnus-summary-select-article))
4474                 (set-buffer obuf)
4475                 (unless not-restore-window
4476                   (set-window-configuration owin))
4477                 (when (eq selected 'old)
4478                   (article-goto-body)
4479                   (set-window-start (get-buffer-window (current-buffer))
4480                                     1)
4481                   (set-window-point (get-buffer-window (current-buffer))
4482                                     (point)))
4483                 (let ((win (get-buffer-window gnus-article-current-summary)))
4484                   (when win
4485                     (set-window-point win new-sum-point))))    )
4486           (switch-to-buffer gnus-article-buffer)
4487           (ding))))))
4488
4489 (defun gnus-article-describe-key (key)
4490   "Display documentation of the function invoked by KEY.  KEY is a string."
4491   (interactive "kDescribe key: ")
4492   (gnus-article-check-buffer)
4493   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4494       (save-excursion
4495         (set-buffer gnus-article-current-summary)
4496         (let (gnus-pick-mode)
4497           (push (elt key 0) unread-command-events)
4498           (setq key (if (featurep 'xemacs)
4499                         (events-to-keys (read-key-sequence "Describe key: "))
4500                       (read-key-sequence "Describe key: "))))
4501         (describe-key key))
4502     (describe-key key)))
4503
4504 (defun gnus-article-describe-key-briefly (key &optional insert)
4505   "Display documentation of the function invoked by KEY.  KEY is a string."
4506   (interactive "kDescribe key: \nP")
4507   (gnus-article-check-buffer)
4508   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4509       (save-excursion
4510         (set-buffer gnus-article-current-summary)
4511         (let (gnus-pick-mode)
4512           (push (elt key 0) unread-command-events)
4513           (setq key (if (featurep 'xemacs)
4514                         (events-to-keys (read-key-sequence "Describe key: "))
4515                       (read-key-sequence "Describe key: "))))
4516         (describe-key-briefly key insert))
4517     (describe-key-briefly key insert)))
4518
4519 (defun gnus-article-hide (&optional arg force)
4520   "Hide all the gruft in the current article.
4521 This means that PGP stuff, signatures, cited text and (some)
4522 headers will be hidden.
4523 If given a prefix, show the hidden text instead."
4524   (interactive (append (gnus-article-hidden-arg) (list 'force)))
4525   (gnus-article-hide-headers arg)
4526   (gnus-article-hide-list-identifiers arg)
4527   (gnus-article-hide-pgp arg)
4528   (gnus-article-hide-citation-maybe arg force)
4529   (gnus-article-hide-signature arg))
4530
4531 (defun gnus-article-maybe-highlight ()
4532   "Do some article highlighting if article highlighting is requested."
4533   (when (gnus-visual-p 'article-highlight 'highlight)
4534     (gnus-article-highlight-some)))
4535
4536 (defun gnus-check-group-server ()
4537   ;; Make sure the connection to the server is alive.
4538   (unless (gnus-server-opened
4539            (gnus-find-method-for-group gnus-newsgroup-name))
4540     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
4541     (gnus-request-group gnus-newsgroup-name t)))
4542
4543 (defun gnus-request-article-this-buffer (article group)
4544   "Get an article and insert it into this buffer."
4545   (let (do-update-line sparse-header)
4546     (prog1
4547         (save-excursion
4548           (erase-buffer)
4549           (gnus-kill-all-overlays)
4550           (setq group (or group gnus-newsgroup-name))
4551
4552           ;; Using `gnus-request-article' directly will insert the article into
4553           ;; `nntp-server-buffer' - so we'll save some time by not having to
4554           ;; copy it from the server buffer into the article buffer.
4555
4556           ;; We only request an article by message-id when we do not have the
4557           ;; headers for it, so we'll have to get those.
4558           (when (stringp article)
4559             (gnus-read-header article))
4560
4561           ;; If the article number is negative, that means that this article
4562           ;; doesn't belong in this newsgroup (possibly), so we find its
4563           ;; message-id and request it by id instead of number.
4564           (when (and (numberp article)
4565                      gnus-summary-buffer
4566                      (get-buffer gnus-summary-buffer)
4567                      (gnus-buffer-exists-p gnus-summary-buffer))
4568             (save-excursion
4569               (set-buffer gnus-summary-buffer)
4570               (let ((header (gnus-summary-article-header article)))
4571                 (when (< article 0)
4572                   (cond
4573                    ((memq article gnus-newsgroup-sparse)
4574                     ;; This is a sparse gap article.
4575                     (setq do-update-line article)
4576                     (setq article (mail-header-id header))
4577                     (setq sparse-header (gnus-read-header article))
4578                     (setq gnus-newsgroup-sparse
4579                           (delq article gnus-newsgroup-sparse)))
4580                    ((vectorp header)
4581                     ;; It's a real article.
4582                     (setq article (mail-header-id header)))
4583                    (t
4584                     ;; It is an extracted pseudo-article.
4585                     (setq article 'pseudo)
4586                     (gnus-request-pseudo-article header))))
4587
4588                 (let ((method (gnus-find-method-for-group
4589                                gnus-newsgroup-name)))
4590                   (when (and (eq (car method) 'nneething)
4591                              (vectorp header))
4592                     (let ((dir (expand-file-name
4593                                 (mail-header-subject header)
4594                                 (file-name-as-directory
4595                                  (or (cadr (assq 'nneething-address method))
4596                                      (nth 1 method))))))
4597                       (when (file-directory-p dir)
4598                         (setq article 'nneething)
4599                         (gnus-group-enter-directory dir))))))))
4600
4601           (cond
4602            ;; Refuse to select canceled articles.
4603            ((and (numberp article)
4604                  gnus-summary-buffer
4605                  (get-buffer gnus-summary-buffer)
4606                  (gnus-buffer-exists-p gnus-summary-buffer)
4607                  (eq (cdr (save-excursion
4608                             (set-buffer gnus-summary-buffer)
4609                             (assq article gnus-newsgroup-reads)))
4610                      gnus-canceled-mark))
4611             nil)
4612            ;; Check the backlog.
4613            ((and gnus-keep-backlog
4614                  (gnus-backlog-request-article group article (current-buffer)))
4615             'article)
4616            ;; Check asynchronous pre-fetch.
4617            ((gnus-async-request-fetched-article group article (current-buffer))
4618             (gnus-async-prefetch-next group article gnus-summary-buffer)
4619             (when (and (numberp article) gnus-keep-backlog)
4620               (gnus-backlog-enter-article group article (current-buffer)))
4621             'article)
4622            ;; Check the cache.
4623            ((and gnus-use-cache
4624                  (numberp article)
4625                  (gnus-cache-request-article article group))
4626             'article)
4627            ;; Get the article and put into the article buffer.
4628            ((or (stringp article)
4629                 (numberp article))
4630             (let ((gnus-override-method gnus-override-method)
4631                   (methods (and (stringp article) 
4632                                 gnus-refer-article-method))
4633                   result
4634                   (buffer-read-only nil))
4635               (if (or (not (listp methods))
4636                       (and (symbolp (car methods))
4637                            (assq (car methods) nnoo-definition-alist)))
4638                   (setq methods (list methods)))
4639               (when (and (null gnus-override-method)
4640                          methods)
4641                 (setq gnus-override-method (pop methods)))
4642               (while (not result)
4643                 (when (eq gnus-override-method 'current)
4644                   (setq gnus-override-method gnus-current-select-method))
4645                 (erase-buffer)
4646                 (gnus-kill-all-overlays)
4647                 (let ((gnus-newsgroup-name group))
4648                   (gnus-check-group-server))
4649                 (when (gnus-request-article article group (current-buffer))
4650                   (when (numberp article)
4651                     (gnus-async-prefetch-next group article 
4652                                               gnus-summary-buffer)
4653                     (when gnus-keep-backlog
4654                       (gnus-backlog-enter-article
4655                        group article (current-buffer))))
4656                   (setq result 'article))
4657                 (if (not result)
4658                     (if methods
4659                         (setq gnus-override-method (pop methods))
4660                       (setq result 'done))))
4661               (and (eq result 'article) 'article)))
4662            ;; It was a pseudo.
4663            (t article)))
4664
4665       ;; Associate this article with the current summary buffer.
4666       (setq gnus-article-current-summary gnus-summary-buffer)
4667
4668       ;; Take the article from the original article buffer
4669       ;; and place it in the buffer it's supposed to be in.
4670       (when (and (get-buffer gnus-article-buffer)
4671                  (equal (buffer-name (current-buffer))
4672                         (buffer-name (get-buffer gnus-article-buffer))))
4673         (save-excursion
4674           (if (get-buffer gnus-original-article-buffer)
4675               (set-buffer gnus-original-article-buffer)
4676             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4677             (set-buffer-multibyte nil)
4678             (buffer-disable-undo)
4679             (setq major-mode 'gnus-original-article-mode)
4680             (setq buffer-read-only t))
4681           (let (buffer-read-only)
4682             (erase-buffer)
4683             (insert-buffer-substring gnus-article-buffer))
4684           (setq gnus-original-article (cons group article)))
4685
4686         ;; Decode charsets.
4687         (run-hooks 'gnus-article-decode-hook)
4688         ;; Mark article as decoded or not.
4689         (setq gnus-article-decoded-p gnus-article-decode-hook))
4690
4691       ;; Update sparse articles.
4692       (when (and do-update-line
4693                  (or (numberp article)
4694                      (stringp article)))
4695         (let ((buf (current-buffer)))
4696           (set-buffer gnus-summary-buffer)
4697           (gnus-summary-update-article do-update-line sparse-header)
4698           (gnus-summary-goto-subject do-update-line nil t)
4699           (set-window-point (get-buffer-window (current-buffer) t)
4700                             (point))
4701           (set-buffer buf))))))
4702
4703 ;;;
4704 ;;; Article editing
4705 ;;;
4706
4707 (defcustom gnus-article-edit-mode-hook nil
4708   "Hook run in article edit mode buffers."
4709   :group 'gnus-article-various
4710   :type 'hook)
4711
4712 (defcustom gnus-article-edit-article-setup-function
4713   'gnus-article-mime-edit-article-setup
4714   "Function called to setup an editing article buffer."
4715   :group 'gnus-article-various
4716   :type 'function)
4717
4718 (defvar gnus-article-edit-done-function nil)
4719
4720 (defvar gnus-article-edit-mode-map nil)
4721
4722 ;; Should we be using derived.el for this?
4723 (unless gnus-article-edit-mode-map
4724   (setq gnus-article-edit-mode-map (make-sparse-keymap))
4725   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
4726
4727   (gnus-define-keys gnus-article-edit-mode-map
4728     "\C-c\C-c" gnus-article-edit-done
4729     "\C-c\C-k" gnus-article-edit-exit)
4730
4731   (gnus-define-keys (gnus-article-edit-wash-map
4732                      "\C-c\C-w" gnus-article-edit-mode-map)
4733     "f" gnus-article-edit-full-stops))
4734
4735 (defun gnus-article-edit-mode ()
4736   "Major mode for editing articles.
4737 This is an extended text-mode.
4738
4739 \\{gnus-article-edit-mode-map}"
4740   (interactive)
4741   (setq major-mode 'gnus-article-edit-mode)
4742   (setq mode-name "Article Edit")
4743   (use-local-map gnus-article-edit-mode-map)
4744   (make-local-variable 'gnus-article-edit-done-function)
4745   (make-local-variable 'gnus-prev-winconf)
4746   (setq buffer-read-only nil)
4747   (buffer-enable-undo)
4748   (widen)
4749   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
4750
4751 (defun gnus-article-edit (&optional force)
4752   "Edit the current article.
4753 This will have permanent effect only in mail groups.
4754 If FORCE is non-nil, allow editing of articles even in read-only
4755 groups."
4756   (interactive "P")
4757   (when (and (not force)
4758              (gnus-group-read-only-p))
4759     (error "The current newsgroup does not support article editing"))
4760   (gnus-article-date-original)
4761   (gnus-article-edit-article
4762    'ignore
4763    `(lambda (no-highlight)
4764       'ignore
4765       (gnus-summary-edit-article-done
4766        ,(or (mail-header-references gnus-current-headers) "")
4767        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
4768
4769 (defun gnus-article-edit-article (start-func exit-func)
4770   "Start editing the contents of the current article buffer."
4771   (let ((winconf (current-window-configuration)))
4772     (set-buffer gnus-article-buffer)
4773     (gnus-article-edit-mode)
4774     (funcall start-func)
4775     (gnus-configure-windows 'edit-article)
4776     (setq gnus-article-edit-done-function exit-func)
4777     (setq gnus-prev-winconf winconf)
4778     (when gnus-article-edit-article-setup-function
4779       (funcall gnus-article-edit-article-setup-function))
4780     (gnus-message 6 "C-c C-c to end edits")))
4781
4782 (defun gnus-article-edit-done (&optional arg)
4783   "Update the article edits and exit."
4784   (interactive "P")
4785   (let ((func gnus-article-edit-done-function)
4786         (buf (current-buffer))
4787         (start (window-start)))
4788     (remove-hook 'gnus-article-mode-hook
4789                  'gnus-article-mime-edit-article-unwind)
4790     (gnus-article-edit-exit)
4791     (save-excursion
4792       (set-buffer buf)
4793       (let ((buffer-read-only nil))
4794         (funcall func arg))
4795       ;; The cache and backlog have to be flushed somewhat.
4796       (when gnus-keep-backlog
4797         (gnus-backlog-remove-article
4798          (car gnus-article-current) (cdr gnus-article-current)))
4799       ;; Flush original article as well.
4800       (save-excursion
4801         (when (get-buffer gnus-original-article-buffer)
4802           (set-buffer gnus-original-article-buffer)
4803           (setq gnus-original-article nil)))
4804       (when gnus-use-cache
4805         (gnus-cache-update-article
4806          (car gnus-article-current) (cdr gnus-article-current))))
4807     (set-buffer buf)
4808     (set-window-start (get-buffer-window buf) start)
4809     (set-window-point (get-buffer-window buf) (point))))
4810
4811 (defun gnus-article-edit-exit ()
4812   "Exit the article editing without updating."
4813   (interactive)
4814   ;; We remove all text props from the article buffer.
4815   (let ((buf (buffer-substring-no-properties (point-min) (point-max)))
4816         (curbuf (current-buffer))
4817         (p (point))
4818         (window-start (window-start)))
4819     (erase-buffer)
4820     (insert buf)
4821     (let ((winconf gnus-prev-winconf))
4822       (gnus-article-mode)
4823       (set-window-configuration winconf)
4824       ;; Tippy-toe some to make sure that point remains where it was.
4825       (save-current-buffer
4826         (set-buffer curbuf)
4827         (set-window-start (get-buffer-window (current-buffer)) window-start)
4828         (goto-char p)))))
4829
4830 (defun gnus-article-edit-full-stops ()
4831   "Interactively repair spacing at end of sentences."
4832   (interactive)
4833   (save-excursion
4834     (goto-char (point-min))
4835     (search-forward-regexp "^$" nil t)
4836     (let ((case-fold-search nil))
4837       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
4838
4839 ;;;
4840 ;;; Article editing with MIME-Edit
4841 ;;;
4842
4843 (defcustom gnus-article-mime-edit-article-setup-hook nil
4844   "Hook run after setting up a MIME editing article buffer."
4845   :group 'gnus-article-various
4846   :type 'hook)
4847
4848 (defun gnus-article-mime-edit-article-unwind ()
4849   "Unwind `gnus-article-buffer' if article editing was given up."
4850   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
4851   (when mime-edit-mode-flag
4852     (mime-edit-exit 'nomime 'no-error)
4853     (message ""))
4854   (when (featurep 'font-lock)
4855     (setq font-lock-defaults nil)
4856     (font-lock-mode 0)))
4857
4858 (defun gnus-article-mime-edit-article-setup ()
4859   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
4860 after replacing with the original article."
4861   (setq gnus-show-mime t)
4862   (setq gnus-article-edit-done-function
4863         `(lambda (&rest args)
4864            (when mime-edit-mode-flag
4865              (mime-edit-exit)
4866              (message ""))
4867            (goto-char (point-min))
4868            (let (case-fold-search)
4869              (when (re-search-forward
4870                     (format "^%s$" (regexp-quote mail-header-separator))
4871                     nil t)
4872                (replace-match "")))
4873            (when (featurep 'font-lock)
4874              (setq font-lock-defaults nil)
4875              (font-lock-mode 0))
4876            (apply ,gnus-article-edit-done-function args)
4877            (set-buffer gnus-original-article-buffer)
4878            (erase-buffer)
4879            (insert-buffer gnus-article-buffer)
4880            (setq gnus-current-headers (gnus-article-make-full-mail-header))
4881            (gnus-article-prepare-display)))
4882   (substitute-key-definition
4883    'gnus-article-edit-exit 'gnus-article-mime-edit-exit
4884    gnus-article-edit-mode-map)
4885   (erase-buffer)
4886   (insert-buffer gnus-original-article-buffer)
4887   (mime-edit-again)
4888   (when (featurep 'font-lock)
4889     (set (make-local-variable 'font-lock-defaults)
4890          '(message-font-lock-keywords t))
4891     (font-lock-set-defaults)
4892     (turn-on-font-lock))
4893   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
4894   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
4895
4896 (defun gnus-article-mime-edit-exit ()
4897   "Exit the article MIME editing without updating."
4898   (interactive)
4899   (let ((winconf gnus-prev-winconf)
4900         buf)
4901     (when mime-edit-mode-flag
4902       (mime-edit-exit)
4903       (message ""))
4904     (goto-char (point-min))
4905     (let (case-fold-search)
4906       (when (re-search-forward
4907              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
4908         (replace-match "")))
4909     (when (featurep 'font-lock)
4910       (setq font-lock-defaults nil)
4911       (font-lock-mode 0))
4912     ;; We remove all text props from the article buffer.
4913     (setq buf (format "%s" (buffer-string)))
4914     (set-buffer (get-buffer-create gnus-original-article-buffer))
4915     (erase-buffer)
4916     (insert buf)
4917     (setq gnus-current-headers (gnus-article-make-full-mail-header))
4918     (gnus-article-prepare-display)
4919     (set-window-configuration winconf)))
4920
4921 ;;;
4922 ;;; Article highlights
4923 ;;;
4924
4925 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
4926
4927 ;;; Internal Variables:
4928
4929 (defcustom gnus-button-url-regexp "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\)\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?\\([-a-zA-Z0-9_=!?#$@~`%&*+|\\/:;.,]\\|\\w\\)+\\([-a-zA-Z0-9_=#$@~`%&*+|\\/]\\|\\w\\)\\)"
4930   "Regular expression that matches URLs."
4931   :group 'gnus-article-buttons
4932   :type 'regexp)
4933
4934 (defcustom gnus-button-alist
4935   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
4936      0 t gnus-button-message-id 2)
4937     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
4938     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
4939      1 t
4940      gnus-button-fetch-group 4)
4941     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
4942     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
4943      t gnus-button-message-id 3)
4944     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
4945     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
4946     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
4947     ;; This is how URLs _should_ be embedded in text...
4948     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
4949     ;; Raw URLs.
4950     (,gnus-button-url-regexp 0 t browse-url 0))
4951   "*Alist of regexps matching buttons in article bodies.
4952
4953 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
4954 REGEXP: is the string matching text around the button,
4955 BUTTON: is the number of the regexp grouping actually matching the button,
4956 FORM: is a lisp expression which must eval to true for the button to
4957 be added,
4958 CALLBACK: is the function to call when the user push this button, and each
4959 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
4960
4961 CALLBACK can also be a variable, in that case the value of that
4962 variable it the real callback function."
4963   :group 'gnus-article-buttons
4964   :type '(repeat (list regexp
4965                        (integer :tag "Button")
4966                        (sexp :tag "Form")
4967                        (function :tag "Callback")
4968                        (repeat :tag "Par"
4969                                :inline t
4970                                (integer :tag "Regexp group")))))
4971
4972 (defcustom gnus-header-button-alist
4973   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
4974      0 t gnus-button-message-id 0)
4975     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
4976     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
4977      0 t gnus-button-mailto 0)
4978     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
4979     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
4980     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
4981     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
4982      gnus-button-message-id 3))
4983   "*Alist of headers and regexps to match buttons in article heads.
4984
4985 This alist is very similar to `gnus-button-alist', except that each
4986 alist has an additional HEADER element first in each entry:
4987
4988 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
4989
4990 HEADER is a regexp to match a header.  For a fuller explanation, see
4991 `gnus-button-alist'."
4992   :group 'gnus-article-buttons
4993   :group 'gnus-article-headers
4994   :type '(repeat (list (regexp :tag "Header")
4995                        regexp
4996                        (integer :tag "Button")
4997                        (sexp :tag "Form")
4998                        (function :tag "Callback")
4999                        (repeat :tag "Par"
5000                                :inline t
5001                                (integer :tag "Regexp group")))))
5002
5003 (defvar gnus-button-regexp nil)
5004 (defvar gnus-button-marker-list nil)
5005 ;; Regexp matching any of the regexps from `gnus-button-alist'.
5006
5007 (defvar gnus-button-last nil)
5008 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
5009
5010 ;;; Commands:
5011
5012 (defun gnus-article-push-button (event)
5013   "Check text under the mouse pointer for a callback function.
5014 If the text under the mouse pointer has a `gnus-callback' property,
5015 call it with the value of the `gnus-data' text property."
5016   (interactive "e")
5017   (set-buffer (window-buffer (posn-window (event-start event))))
5018   (let* ((pos (posn-point (event-start event)))
5019          (data (get-text-property pos 'gnus-data))
5020          (fun (get-text-property pos 'gnus-callback)))
5021     (goto-char pos)
5022     (when fun
5023       (funcall fun data))))
5024
5025 (defun gnus-article-press-button ()
5026   "Check text at point for a callback function.
5027 If the text at point has a `gnus-callback' property,
5028 call it with the value of the `gnus-data' text property."
5029   (interactive)
5030   (let* ((data (get-text-property (point) 'gnus-data))
5031          (fun (get-text-property (point) 'gnus-callback)))
5032     (when fun
5033       (funcall fun data))))
5034
5035 (defun gnus-article-prev-button (n)
5036   "Move point to N buttons backward.
5037 If N is negative, move forward instead."
5038   (interactive "p")
5039   (gnus-article-next-button (- n)))
5040
5041 (defun gnus-article-next-button (n)
5042   "Move point to N buttons forward.
5043 If N is negative, move backward instead."
5044   (interactive "p")
5045   (let ((function (if (< n 0) 'previous-single-property-change
5046                     'next-single-property-change))
5047         (inhibit-point-motion-hooks t)
5048         (backward (< n 0))
5049         (limit (if (< n 0) (point-min) (point-max))))
5050     (setq n (abs n))
5051     (while (and (not (= limit (point)))
5052                 (> n 0))
5053       ;; Skip past the current button.
5054       (when (get-text-property (point) 'gnus-callback)
5055         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5056       ;; Go to the next (or previous) button.
5057       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
5058       ;; Put point at the start of the button.
5059       (when (and backward (not (get-text-property (point) 'gnus-callback)))
5060         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5061       ;; Skip past intangible buttons.
5062       (when (get-text-property (point) 'intangible)
5063         (incf n))
5064       (decf n))
5065     (unless (zerop n)
5066       (gnus-message 5 "No more buttons"))
5067     n))
5068
5069 (defun gnus-article-highlight (&optional force)
5070   "Highlight current article.
5071 This function calls `gnus-article-highlight-headers',
5072 `gnus-article-highlight-citation',
5073 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5074 do the highlighting.  See the documentation for those functions."
5075   (interactive (list 'force))
5076   (gnus-article-highlight-headers)
5077   (gnus-article-highlight-citation force)
5078   (gnus-article-highlight-signature)
5079   (gnus-article-add-buttons force)
5080   (gnus-article-add-buttons-to-head))
5081
5082 (defun gnus-article-highlight-some (&optional force)
5083   "Highlight current article.
5084 This function calls `gnus-article-highlight-headers',
5085 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5086 do the highlighting.  See the documentation for those functions."
5087   (interactive (list 'force))
5088   (gnus-article-highlight-headers)
5089   (gnus-article-highlight-signature)
5090   (gnus-article-add-buttons))
5091
5092 (defun gnus-article-highlight-headers ()
5093   "Highlight article headers as specified by `gnus-header-face-alist'."
5094   (interactive)
5095   (save-excursion
5096     (set-buffer gnus-article-buffer)
5097     (save-restriction
5098       (let ((alist gnus-header-face-alist)
5099             (buffer-read-only nil)
5100             (case-fold-search t)
5101             (inhibit-point-motion-hooks t)
5102             entry regexp header-face field-face from hpoints fpoints)
5103         (article-narrow-to-head)
5104         (while (setq entry (pop alist))
5105           (goto-char (point-min))
5106           (setq regexp (concat "^\\("
5107                                (if (string-equal "" (nth 0 entry))
5108                                    "[^\t ]"
5109                                  (nth 0 entry))
5110                                "\\)")
5111                 header-face (nth 1 entry)
5112                 field-face (nth 2 entry))
5113           (while (and (re-search-forward regexp nil t)
5114                       (not (eobp)))
5115             (beginning-of-line)
5116             (setq from (point))
5117             (unless (search-forward ":" nil t)
5118               (forward-char 1))
5119             (when (and header-face
5120                        (not (memq (point) hpoints)))
5121               (push (point) hpoints)
5122               (gnus-put-text-property from (point) 'face header-face))
5123             (when (and field-face
5124                        (not (memq (setq from (point)) fpoints)))
5125               (push from fpoints)
5126               (if (re-search-forward "^[^ \t]" nil t)
5127                   (forward-char -2)
5128                 (goto-char (point-max)))
5129               (gnus-put-text-property from (point) 'face field-face))))))))
5130
5131 (defun gnus-article-highlight-signature ()
5132   "Highlight the signature in an article.
5133 It does this by highlighting everything after
5134 `gnus-signature-separator' using `gnus-signature-face'."
5135   (interactive)
5136   (when gnus-signature-face
5137     (save-excursion
5138       (set-buffer gnus-article-buffer)
5139       (let ((buffer-read-only nil)
5140             (inhibit-point-motion-hooks t))
5141         (save-restriction
5142           (when (gnus-article-narrow-to-signature)
5143             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
5144                               'face gnus-signature-face)))))))
5145
5146 (defun gnus-article-buttonize-signature ()
5147   "Add button to the signature."
5148   (interactive)
5149   (save-excursion
5150     (set-buffer gnus-article-buffer)
5151     (let ((buffer-read-only nil)
5152           (inhibit-point-motion-hooks t))
5153       (when (gnus-article-search-signature)
5154         (gnus-article-add-button (match-beginning 0) (match-end 0)
5155                                  'gnus-signature-toggle
5156                                  (set-marker (make-marker)
5157                                              (1+ (match-end 0))))))))
5158
5159 (defun gnus-button-in-region-p (b e prop)
5160   "Say whether PROP exists in the region."
5161   (text-property-not-all b e prop nil))
5162
5163 (defun gnus-article-add-buttons (&optional force)
5164   "Find external references in the article and make buttons of them.
5165 \"External references\" are things like Message-IDs and URLs, as
5166 specified by `gnus-button-alist'."
5167   (interactive (list 'force))
5168   (save-excursion
5169     (set-buffer gnus-article-buffer)
5170     (let ((buffer-read-only nil)
5171           (inhibit-point-motion-hooks t)
5172           (case-fold-search t)
5173           (alist gnus-button-alist)
5174           beg entry regexp)
5175       ;; Remove all old markers.
5176       (let (marker entry new-list)
5177         (while (setq marker (pop gnus-button-marker-list))
5178           (if (or (< marker (point-min)) (>= marker (point-max)))
5179               (push marker new-list)
5180             (goto-char marker)
5181             (when (setq entry (gnus-button-entry))
5182               (put-text-property (match-beginning (nth 1 entry))
5183                                  (match-end (nth 1 entry))
5184                                  'gnus-callback nil))
5185             (set-marker marker nil)))
5186         (setq gnus-button-marker-list new-list))
5187       ;; We skip the headers.
5188       (article-goto-body)
5189       (setq beg (point))
5190       (while (setq entry (pop alist))
5191         (setq regexp (car entry))
5192         (goto-char beg)
5193         (while (re-search-forward regexp nil t)
5194           (let* ((start (and entry (match-beginning (nth 1 entry))))
5195                  (end (and entry (match-end (nth 1 entry))))
5196                  (from (match-beginning 0)))
5197             (when (and (or (eq t (nth 2 entry))
5198                            (eval (nth 2 entry)))
5199                        (not (gnus-button-in-region-p
5200                              start end 'gnus-callback)))
5201               ;; That optional form returned non-nil, so we add the
5202               ;; button.
5203               (gnus-article-add-button
5204                start end 'gnus-button-push
5205                (car (push (set-marker (make-marker) from)
5206                           gnus-button-marker-list))))))))))
5207
5208 ;; Add buttons to the head of an article.
5209 (defun gnus-article-add-buttons-to-head ()
5210   "Add buttons to the head of the article."
5211   (interactive)
5212   (save-excursion
5213     (set-buffer gnus-article-buffer)
5214     (save-restriction
5215       (let ((buffer-read-only nil)
5216             (inhibit-point-motion-hooks t)
5217             (case-fold-search t)
5218             (alist gnus-header-button-alist)
5219             entry beg end)
5220         (article-narrow-to-head)
5221         (while alist
5222           ;; Each alist entry.
5223           (setq entry (car alist)
5224                 alist (cdr alist))
5225           (goto-char (point-min))
5226           (while (re-search-forward (car entry) nil t)
5227             ;; Each header matching the entry.
5228             (setq beg (match-beginning 0))
5229             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
5230                                (match-beginning 0))
5231                           (point-max)))
5232             (goto-char beg)
5233             (while (re-search-forward (nth 1 entry) end t)
5234               ;; Each match within a header.
5235               (let* ((entry (cdr entry))
5236                      (start (match-beginning (nth 1 entry)))
5237                      (end (match-end (nth 1 entry)))
5238                      (form (nth 2 entry)))
5239                 (goto-char (match-end 0))
5240                 (when (eval form)
5241                   (gnus-article-add-button
5242                    start end (nth 3 entry)
5243                    (buffer-substring (match-beginning (nth 4 entry))
5244                                      (match-end (nth 4 entry)))))))
5245             (goto-char end)))))))
5246
5247 ;;; External functions:
5248
5249 (defun gnus-article-add-button (from to fun &optional data)
5250   "Create a button between FROM and TO with callback FUN and data DATA."
5251   (when gnus-article-button-face
5252     (gnus-overlay-put (gnus-make-overlay from to)
5253                       'face gnus-article-button-face))
5254   (gnus-add-text-properties
5255    from to
5256    (nconc (and gnus-article-mouse-face
5257                (list gnus-mouse-face-prop gnus-article-mouse-face))
5258           (list 'gnus-callback fun)
5259           (and data (list 'gnus-data data))))
5260   (widget-convert-button 'link from to :action 'gnus-widget-press-button
5261                          ;; Quote `:button-keymap' for Mule 2.3
5262                          ;; but it won't work.
5263                          ':button-keymap gnus-widget-button-keymap))
5264
5265 ;;; Internal functions:
5266
5267 (defun gnus-article-set-globals ()
5268   (save-excursion
5269     (set-buffer gnus-summary-buffer)
5270     (gnus-set-global-variables)))
5271
5272 (defun gnus-signature-toggle (end)
5273   (save-excursion
5274     (set-buffer gnus-article-buffer)
5275     (let ((buffer-read-only nil)
5276           (inhibit-point-motion-hooks t)
5277           (limit (next-single-property-change end 'mime-view-entity
5278                                               nil (point-max))))
5279       (if (text-property-any end limit 'article-type 'signature)
5280           (gnus-remove-text-properties-when
5281            'article-type 'signature end limit
5282            (cons 'article-type (cons 'signature
5283                                      gnus-hidden-properties)))
5284         (gnus-add-text-properties-when
5285          'article-type nil end limit
5286          (cons 'article-type (cons 'signature
5287                                    gnus-hidden-properties)))))))
5288
5289 (defun gnus-button-entry ()
5290   ;; Return the first entry in `gnus-button-alist' matching this place.
5291   (let ((alist gnus-button-alist)
5292         (entry nil))
5293     (while alist
5294       (setq entry (pop alist))
5295       (if (looking-at (car entry))
5296           (setq alist nil)
5297         (setq entry nil)))
5298     entry))
5299
5300 (defun gnus-button-push (marker)
5301   ;; Push button starting at MARKER.
5302   (save-excursion
5303     (goto-char marker)
5304     (let* ((entry (gnus-button-entry))
5305            (inhibit-point-motion-hooks t)
5306            (fun (nth 3 entry))
5307            (args (mapcar (lambda (group)
5308                            (let ((string (match-string group)))
5309                              (gnus-set-text-properties
5310                               0 (length string) nil string)
5311                              string))
5312                          (nthcdr 4 entry))))
5313       (cond
5314        ((fboundp fun)
5315         (apply fun args))
5316        ((and (boundp fun)
5317              (fboundp (symbol-value fun)))
5318         (apply (symbol-value fun) args))
5319        (t
5320         (gnus-message 1 "You must define `%S' to use this button"
5321                       (cons fun args)))))))
5322
5323 (defun gnus-button-message-id (message-id)
5324   "Fetch MESSAGE-ID."
5325   (save-excursion
5326     (set-buffer gnus-summary-buffer)
5327     (gnus-summary-refer-article message-id)))
5328
5329 (defun gnus-button-fetch-group (address)
5330   "Fetch GROUP specified by ADDRESS."
5331   (if (not (string-match "[:/]" address))
5332       ;; This is just a simple group url.
5333       (gnus-group-read-ephemeral-group address gnus-select-method)
5334     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
5335                            address))
5336         (error "Can't parse %s" address)
5337       (gnus-group-read-ephemeral-group
5338        (match-string 4 address)
5339        `(nntp ,(match-string 1 address)
5340               (nntp-address ,(match-string 1 address))
5341               (nntp-port-number ,(if (match-end 3)
5342                                      (match-string 3 address)
5343                                    "nntp")))))))
5344
5345 (defun gnus-url-parse-query-string (query &optional downcase)
5346   (let (retval pairs cur key val)
5347     (setq pairs (split-string query "&"))
5348     (while pairs
5349       (setq cur (car pairs)
5350             pairs (cdr pairs))
5351       (if (not (string-match "=" cur))
5352           nil                           ; Grace
5353         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
5354               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
5355         (if downcase
5356             (setq key (downcase key)))
5357         (setq cur (assoc key retval))
5358         (if cur
5359             (setcdr cur (cons val (cdr cur)))
5360           (setq retval (cons (list key val) retval)))))
5361     retval))
5362
5363 (defun gnus-url-unhex (x)
5364   (if (> x ?9)
5365       (if (>= x ?a)
5366           (+ 10 (- x ?a))
5367         (+ 10 (- x ?A)))
5368     (- x ?0)))
5369
5370 (defun gnus-url-unhex-string (str &optional allow-newlines)
5371   "Remove %XXX embedded spaces, etc in a url.
5372 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
5373 decoding of carriage returns and line feeds in the string, which is normally
5374 forbidden in URL encoding."
5375   (setq str (or str ""))
5376   (let ((tmp "")
5377         (case-fold-search t))
5378     (while (string-match "%[0-9a-f][0-9a-f]" str)
5379       (let* ((start (match-beginning 0))
5380              (ch1 (gnus-url-unhex (elt str (+ start 1))))
5381              (code (+ (* 16 ch1)
5382                       (gnus-url-unhex (elt str (+ start 2))))))
5383         (setq tmp (concat
5384                    tmp (substring str 0 start)
5385                    (cond
5386                     (allow-newlines
5387                      (char-to-string code))
5388                     ((or (= code ?\n) (= code ?\r))
5389                      " ")
5390                     (t (char-to-string code))))
5391               str (substring str (match-end 0)))))
5392     (setq tmp (concat tmp str))
5393     tmp))
5394
5395 (defun gnus-url-mailto (url)
5396   ;; Send mail to someone
5397   (when (string-match "mailto:/*\\(.*\\)" url)
5398     (setq url (substring url (match-beginning 1) nil)))
5399   (let (to args subject func)
5400     (if (string-match (regexp-quote "?") url)
5401         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
5402               args (gnus-url-parse-query-string
5403                     (substring url (match-end 0) nil) t))
5404       (setq to (gnus-url-unhex-string url)))
5405     (setq args (cons (list "to" to) args)
5406           subject (cdr-safe (assoc "subject" args)))
5407     (gnus-setup-message 'reply
5408       (message-mail)
5409       (while args
5410         (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
5411         (if (fboundp func)
5412             (funcall func)
5413           (message-position-on-field (caar args)))
5414         (insert (mapconcat 'identity (cdar args) ", "))
5415         (setq args (cdr args)))
5416       (if subject
5417           (message-goto-body)
5418         (message-goto-subject)))))
5419
5420 (defun gnus-button-mailto (address)
5421   "Mail to ADDRESS."
5422   (set-buffer (gnus-copy-article-buffer))
5423   (message-reply address))
5424
5425 (defalias 'gnus-button-reply 'message-reply)
5426
5427 (defun gnus-button-embedded-url (address)
5428   "Activate ADDRESS with `browse-url'."
5429   (browse-url (gnus-strip-whitespace address)))
5430
5431 (defun gnus-article-smiley-display ()
5432   "Display \"smileys\" as small graphical icons."
5433   (smiley-toggle-buffer 1 (current-buffer) (point-min) (point-max)))
5434
5435 ;;; Next/prev buttons in the article buffer.
5436
5437 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
5438 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
5439
5440 (defvar gnus-prev-page-map nil)
5441 (unless gnus-prev-page-map
5442   (setq gnus-prev-page-map (make-sparse-keymap))
5443   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
5444   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
5445
5446 (static-if (featurep 'xemacs)
5447     (defun gnus-insert-prev-page-button ()
5448       (let ((buffer-read-only nil))
5449         (gnus-eval-format
5450          gnus-prev-page-line-format nil
5451          `(gnus-prev t local-map ,gnus-prev-page-map
5452                      gnus-callback gnus-article-button-prev-page
5453                      article-type annotation))))
5454   (defun gnus-insert-prev-page-button ()
5455     (let ((buffer-read-only nil)
5456           (situation (get-text-property (point-min) 'mime-view-situation)))
5457       (set-keymap-parent gnus-prev-page-map (current-local-map))
5458       (gnus-eval-format
5459        gnus-prev-page-line-format nil
5460        `(gnus-prev t local-map ,gnus-prev-page-map
5461                    gnus-callback gnus-article-button-prev-page
5462                    article-type annotation
5463                    mime-view-situation ,situation))))
5464   )
5465
5466 (defvar gnus-next-page-map nil)
5467 (unless gnus-next-page-map
5468   (setq gnus-next-page-map (make-sparse-keymap))
5469   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
5470   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
5471
5472 (defun gnus-button-next-page ()
5473   "Go to the next page."
5474   (interactive)
5475   (let ((win (selected-window)))
5476     (select-window (get-buffer-window gnus-article-buffer t))
5477     (gnus-article-next-page)
5478     (select-window win)))
5479
5480 (defun gnus-button-prev-page ()
5481   "Go to the prev page."
5482   (interactive)
5483   (let ((win (selected-window)))
5484     (select-window (get-buffer-window gnus-article-buffer t))
5485     (gnus-article-prev-page)
5486     (select-window win)))
5487
5488 (static-if (featurep 'xemacs)
5489     (defun gnus-insert-next-page-button ()
5490       (let ((buffer-read-only nil))
5491         (gnus-eval-format gnus-next-page-line-format nil
5492                           `(gnus-next
5493                             t local-map ,gnus-next-page-map
5494                             gnus-callback gnus-article-button-next-page
5495                             article-type annotation))))
5496   (defun gnus-insert-next-page-button ()
5497     (let ((buffer-read-only nil)
5498           (situation (get-text-property (point-min) 'mime-view-situation)))
5499       (set-keymap-parent gnus-next-page-map (current-local-map))
5500       (gnus-eval-format gnus-next-page-line-format nil
5501                         `(gnus-next
5502                           t local-map ,gnus-next-page-map
5503                           gnus-callback gnus-article-button-next-page
5504                           article-type annotation
5505                           mime-view-situation ,situation))))
5506   )
5507
5508 (defun gnus-article-button-next-page (arg)
5509   "Go to the next page."
5510   (interactive "P")
5511   (let ((win (selected-window)))
5512     (select-window (get-buffer-window gnus-article-buffer t))
5513     (gnus-article-next-page)
5514     (select-window win)))
5515
5516 (defun gnus-article-button-prev-page (arg)
5517   "Go to the prev page."
5518   (interactive "P")
5519   (let ((win (selected-window)))
5520     (select-window (get-buffer-window gnus-article-buffer t))
5521     (gnus-article-prev-page)
5522     (select-window win)))
5523
5524 (defvar gnus-decode-header-methods
5525   '(mail-decode-encoded-word-region)
5526   "List of methods used to decode headers.
5527
5528 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
5529 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
5530 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
5531 whose names match REGEXP.
5532
5533 For example:
5534 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
5535  mail-decode-encoded-word-region
5536  (\"chinese\" . rfc1843-decode-region))
5537 ")
5538
5539 (defvar gnus-decode-header-methods-cache nil)
5540
5541 (defun gnus-multi-decode-header (start end)
5542   "Apply the functions from `gnus-encoded-word-methods' that match."
5543   (unless (and gnus-decode-header-methods-cache
5544                (eq gnus-newsgroup-name
5545                    (car gnus-decode-header-methods-cache)))
5546     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
5547     (mapcar (lambda (x)
5548               (if (symbolp x)
5549                   (nconc gnus-decode-header-methods-cache (list x))
5550                 (if (and gnus-newsgroup-name
5551                          (string-match (car x) gnus-newsgroup-name))
5552                     (nconc gnus-decode-header-methods-cache
5553                            (list (cdr x))))))
5554           gnus-decode-header-methods))
5555   (let ((xlist gnus-decode-header-methods-cache))
5556     (pop xlist)
5557     (save-restriction
5558       (narrow-to-region start end)
5559       (while xlist
5560         (funcall (pop xlist) (point-min) (point-max))))))
5561
5562 ;;;
5563 ;;; Treatment top-level handling.
5564 ;;;
5565
5566 (defun gnus-treat-article (condition &optional part-number total-parts type)
5567   (let ((length (- (point-max) (point-min)))
5568         (alist gnus-treatment-function-alist)
5569         (article-goto-body-goes-to-point-min-p t)
5570         (treated-type
5571          (or (not type)
5572              (catch 'found
5573                (let ((list gnus-article-treat-types))
5574                  (while list
5575                    (when (string-match (pop list) type)
5576                      (throw 'found t)))))))
5577         (highlightp (gnus-visual-p 'article-highlight 'highlight))
5578         (entity (static-unless (featurep 'xemacs)
5579                   (when (eq 'head condition)
5580                     (get-text-property (point-min) 'mime-view-entity))))
5581         val elem buttonized)
5582     (gnus-run-hooks 'gnus-part-display-hook)
5583     (unless gnus-inhibit-treatment
5584       (while (setq elem (pop alist))
5585         (setq val
5586               (save-excursion
5587                 (if (gnus-buffer-live-p gnus-summary-buffer)
5588                     (set-buffer gnus-summary-buffer))
5589                 (symbol-value (car elem))))
5590         (when (and (or (consp val)
5591                        treated-type)
5592                    (gnus-treat-predicate val)
5593                    (or (not (get (car elem) 'highlight))
5594                        highlightp))
5595           (when (and (not buttonized)
5596                      (memq (car elem)
5597                            '(gnus-treat-hide-signature
5598                              gnus-treat-highlight-signature)))
5599             (gnus-article-buttonize-signature)
5600             (setq buttonized t))
5601           (save-restriction
5602             (funcall (cadr elem)))))
5603       ;; FSF Emacsen does not inherit the existing text properties
5604       ;; in the new text, so we should do it for `mime-view-entity'.
5605       (static-unless (featurep 'xemacs)
5606         (when entity
5607           (put-text-property (point-min) (point-max)
5608                              'mime-view-entity entity))))))
5609
5610 ;; Dynamic variables.
5611 (eval-when-compile
5612   (defvar part-number)
5613   (defvar total-parts)
5614   (defvar type)
5615   (defvar condition)
5616   (defvar length))
5617
5618 (defun gnus-treat-predicate (val)
5619   (cond
5620    ((null val)
5621     nil)
5622    ((and (listp val)
5623          (stringp (car val)))
5624     (apply 'gnus-or (mapcar `(lambda (s)
5625                                (string-match s ,(or gnus-newsgroup-name "")))
5626                             val)))
5627    ((listp val)
5628     (let ((pred (pop val)))
5629       (cond
5630        ((eq pred 'or)
5631         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
5632        ((eq pred 'and)
5633         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
5634        ((eq pred 'not)
5635         (not (gnus-treat-predicate (car val))))
5636        ((eq pred 'typep)
5637         (equal (car val) type))
5638        (t
5639         (error "%S is not a valid predicate" pred)))))
5640    ((eq val 'mime)
5641     gnus-show-mime)
5642    (condition
5643     (eq condition val))
5644    ((eq val t)
5645     t)
5646    ((eq val 'head)
5647     nil)
5648    ((eq val 'last)
5649     (eq part-number total-parts))
5650    ((numberp val)
5651     (< length val))
5652    (t
5653     (error "%S is not a valid value" val))))
5654
5655 (defun gnus-article-encrypt-body (protocol &optional n)
5656   "Encrypt the article body."
5657   (interactive 
5658    (list
5659     (or gnus-article-encrypt-protocol
5660         (completing-read "Encrypt protocol: "
5661                          gnus-article-encrypt-protocol-alist 
5662                          nil t))
5663     current-prefix-arg))
5664   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
5665     (unless func
5666       (error (format "Can't find the encrypt protocol %s" protocol)))
5667     (if (equal gnus-newsgroup-name "nndraft:drafts")
5668         (error "Can't encrypt the article in group nndraft:drafts."))
5669     (if (equal gnus-newsgroup-name "nndraft:queue")
5670         (error "Don't encrypt the article in group nndraft:queue."))
5671     (gnus-summary-iterate n
5672       (save-excursion
5673         (set-buffer gnus-summary-buffer)
5674         (let ((mail-parse-charset gnus-newsgroup-charset)
5675               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5676               (summary-buffer gnus-summary-buffer)
5677               references point)
5678           (gnus-set-global-variables)
5679           (when (gnus-group-read-only-p)
5680             (error "The current newsgroup does not support article encrypt"))
5681           (gnus-summary-show-article t)
5682           (setq references
5683               (or (mail-header-references gnus-current-headers) ""))
5684           (set-buffer gnus-article-buffer)
5685           (let* ((buffer-read-only nil)
5686                  (headers
5687                   (mapcar (lambda (field)
5688                             (and (save-restriction 
5689                                    (message-narrow-to-head)
5690                                    (goto-char (point-min))
5691                                    (search-forward field nil t))
5692                                  (prog2
5693                                      (message-narrow-to-field)
5694                                      (buffer-substring (point-min) (point-max))
5695                                    (delete-region (point-min) (point-max))
5696                                    (widen))))
5697                           '("Content-Type:" "Content-Transfer-Encoding:"
5698                             "Content-Disposition:"))))
5699             (message-narrow-to-head)
5700             (message-remove-header "MIME-Version")
5701             (goto-char (point-max))
5702             (setq point (point))
5703             (insert (apply 'concat headers))
5704             (widen)
5705             (narrow-to-region point (point-max))
5706             (let ((message-options message-options))
5707               (message-options-set 'message-sender user-mail-address)
5708               (message-options-set 'message-recipients user-mail-address)
5709               (message-options-set 'message-sign-encrypt 'not)
5710               (funcall func))
5711             (goto-char (point-min))
5712             (insert "MIME-Version: 1.0\n")
5713             (widen)
5714             (gnus-summary-edit-article-done
5715              references nil summary-buffer t))
5716           (when gnus-keep-backlog
5717             (gnus-backlog-remove-article
5718              (car gnus-article-current) (cdr gnus-article-current)))
5719           (save-excursion
5720             (when (get-buffer gnus-original-article-buffer)
5721               (set-buffer gnus-original-article-buffer)
5722               (setq gnus-original-article nil)))
5723           (when gnus-use-cache
5724             (gnus-cache-update-article
5725              (car gnus-article-current) (cdr gnus-article-current))))))))
5726
5727 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
5728   "The following specs can be used:
5729 %t  The security MIME type
5730 %i  Additional info
5731 %d  Details
5732 %D  Details if button is pressed")
5733
5734 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
5735   "The following specs can be used:
5736 %t  The security MIME type
5737 %i  Additional info
5738 %d  Details
5739 %D  Details if button is pressed")
5740
5741 (defvar gnus-mime-security-button-line-format-alist
5742   '((?t gnus-tmp-type ?s)
5743     (?i gnus-tmp-info ?s)
5744     (?d gnus-tmp-details ?s)
5745     (?D gnus-tmp-pressed-details ?s)))
5746
5747 (defvar gnus-mime-security-button-map
5748   (let ((map (make-sparse-keymap)))
5749     ;; Not for Emacs 21: fixme better.
5750     ;;(set-keymap-parent map gnus-article-mode-map)
5751     (define-key map gnus-mouse-2 'gnus-article-push-button)
5752     (define-key map "\r" 'gnus-article-press-button)
5753     map))
5754
5755 (defvar gnus-mime-security-details-buffer nil)
5756
5757 (defvar gnus-mime-security-button-pressed nil)
5758
5759 (defvar gnus-mime-security-show-details-inline t
5760   "If non-nil, show details in the article buffer.")
5761
5762 (defun gnus-mime-security-verify-or-decrypt (handle)
5763   (mm-remove-parts (cdr handle))
5764   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
5765         buffer-read-only)
5766     (when region 
5767       (delete-region (car region) (cdr region))
5768       (set-marker (car region) nil)
5769       (set-marker (cdr region) nil)))
5770   (with-current-buffer (mm-handle-multipart-original-buffer handle)
5771     (let* ((mm-verify-option 'known)
5772            (mm-decrypt-option 'known)
5773            (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
5774       (unless (eq nparts (cdr handle))
5775         (mm-destroy-parts (cdr handle))
5776         (setcdr handle nparts))))
5777   (let ((point (point))
5778         buffer-read-only)
5779     (gnus-mime-display-security handle)
5780     (goto-char point)))
5781
5782 (defun gnus-mime-security-show-details (handle)
5783   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
5784     (if details
5785         (if gnus-mime-security-show-details-inline
5786             (let ((gnus-mime-security-button-pressed t)
5787                   (gnus-mime-security-button-line-format 
5788                    (get-text-property (point) 'gnus-line-format))
5789                 buffer-read-only)
5790               (forward-char -1)
5791               (while (eq (get-text-property (point) 'gnus-line-format)
5792                          gnus-mime-security-button-line-format)
5793                 (forward-char -1))
5794               (forward-char)
5795               (delete-region (point)
5796                              (or (text-property-not-all 
5797                                   (point) (point-max)
5798                                 'gnus-line-format   
5799                                 gnus-mime-security-button-line-format)
5800                                  (point-max)))
5801               (gnus-insert-mime-security-button handle))
5802           (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
5803               (with-current-buffer gnus-mime-security-details-buffer
5804                 (erase-buffer)
5805                 t)
5806             (setq gnus-mime-security-details-buffer
5807                   (gnus-get-buffer-create "*MIME Security Details*")))
5808           (with-current-buffer gnus-mime-security-details-buffer
5809             (insert details)
5810             (goto-char (point-min)))
5811           (pop-to-buffer gnus-mime-security-details-buffer))
5812       (gnus-message 5 "No details."))))
5813
5814 (defun gnus-mime-security-press-button (handle)
5815   (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
5816       (gnus-mime-security-show-details handle)
5817     (gnus-mime-security-verify-or-decrypt handle)))
5818
5819 (defun gnus-insert-mime-security-button (handle &optional displayed)
5820   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
5821          (gnus-tmp-type
5822           (concat 
5823            (or (nth 2 (assoc protocol mm-verify-function-alist))
5824                (nth 2 (assoc protocol mm-decrypt-function-alist))
5825                "Unknown")
5826            (if (equal (car handle) "multipart/signed")
5827                " Signed" " Encrypted")
5828            " Part"))
5829          (gnus-tmp-info
5830           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
5831               "Undecided"))
5832          (gnus-tmp-details
5833           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
5834          gnus-tmp-pressed-details
5835          b e)
5836     (setq gnus-tmp-details
5837           (if gnus-tmp-details
5838               (concat "\n" gnus-tmp-details) ""))
5839     (setq gnus-tmp-pressed-details 
5840           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
5841     (unless (bolp)
5842       (insert "\n"))
5843     (setq b (point))
5844     (gnus-eval-format
5845      gnus-mime-security-button-line-format 
5846      gnus-mime-security-button-line-format-alist
5847      `(keymap ,gnus-mime-security-button-map
5848               ,@(if (>= (string-to-number emacs-version) 21)
5849                     nil ;; XEmacs doesn't care
5850                   (list 'local-map gnus-mime-security-button-map))
5851               gnus-callback gnus-mime-security-press-button
5852               gnus-line-format ,gnus-mime-security-button-line-format 
5853               article-type annotation
5854               gnus-data ,handle))
5855     (setq e (point))
5856     (widget-convert-button
5857      'link b e
5858      :mime-handle handle
5859      :action 'gnus-widget-press-button
5860      :button-keymap gnus-mime-security-button-map
5861      :help-echo
5862      (lambda (widget/window &optional overlay pos)
5863        ;; Needed to properly clear the message due to a bug in
5864        ;; wid-edit (XEmacs only).
5865        (if (boundp 'help-echo-owns-message)
5866            (setq help-echo-owns-message t))
5867        (format
5868         "%S: show detail"
5869         (aref gnus-mouse-2 0))))))
5870
5871 (defun gnus-mime-display-security (handle)
5872   (save-restriction
5873     (narrow-to-region (point) (point))
5874     (gnus-insert-mime-security-button handle)
5875     (gnus-mime-display-mixed (cdr handle))
5876     (unless (bolp)
5877       (insert "\n"))
5878     (let ((gnus-mime-security-button-line-format 
5879            gnus-mime-security-button-end-line-format))
5880       (gnus-insert-mime-security-button handle))
5881     (mm-set-handle-multipart-parameter handle 'gnus-region 
5882                                        (cons (set-marker (make-marker)
5883                                                          (point-min))
5884                                              (set-marker (make-marker)
5885                                                          (point-max))))))
5886
5887
5888 ;;; @ for mime-view
5889 ;;;
5890
5891 (defun gnus-article-header-presentation-method (entity situation)
5892   (mime-insert-header entity)
5893   )
5894
5895 (set-alist 'mime-header-presentation-method-alist
5896            'gnus-original-article-mode
5897            #'gnus-article-header-presentation-method)
5898
5899 (defun gnus-mime-preview-quitting-method ()
5900   (mime-preview-kill-buffer)
5901   (delete-other-windows)
5902   (gnus-article-show-summary)
5903   (gnus-summary-select-article gnus-show-all-headers t))
5904
5905 (set-alist 'mime-preview-quitting-method-alist
5906            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
5907
5908 (set-alist 'mime-preview-following-method-alist
5909            'gnus-original-article-mode #'gnus-following-method)
5910
5911 (set-alist 'mime-preview-over-to-previous-method-alist
5912            'gnus-original-article-mode
5913            (lambda ()
5914              (if (> (point-min) 1)
5915                  (gnus-article-prev-page)
5916                (gnus-article-read-summary-keys
5917                 nil (gnus-character-to-event ?P)))))
5918
5919 (set-alist 'mime-preview-over-to-next-method-alist
5920            'gnus-original-article-mode'
5921            (lambda ()
5922              (if (< (point-max) (buffer-size))
5923                  (gnus-article-next-page)
5924                (gnus-article-read-summary-keys
5925                 nil (gnus-character-to-event ?N)))))
5926
5927
5928 ;;; @ end
5929 ;;;
5930
5931 (gnus-ems-redefine)
5932
5933 (provide 'gnus-art)
5934
5935 (run-hooks 'gnus-art-load-hook)
5936
5937 ;;; gnus-art.el ends here