Synch 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****s*** 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
1528 Sm*rtq**t*s are M****s***'s unilateral extension to the character map
1529 in an attempt to provide more quoting characters.  If you see
1530 something like \222 or \264 where you're expecting some kind of
1531 apostrophe or quotation mark, then try this wash."
1532   (interactive)
1533   (article-translate-strings gnus-article-dumbquotes-map))
1534
1535 (defun article-translate-characters (from to)
1536   "Translate all characters in the body of the article according to FROM and TO.
1537 FROM is a string of characters to translate from; to is a string of
1538 characters to translate to."
1539   (save-excursion
1540     (when (article-goto-body)
1541       (let ((buffer-read-only nil)
1542             (x (make-string 225 ?x))
1543             (i -1))
1544         (while (< (incf i) (length x))
1545           (aset x i i))
1546         (setq i 0)
1547         (while (< i (length from))
1548           (aset x (aref from i) (aref to i))
1549           (incf i))
1550         (translate-region (point) (point-max) x)))))
1551
1552 (defun article-translate-strings (map)
1553   "Translate all string in the body of the article according to MAP.
1554 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1555   (save-excursion
1556     (when (article-goto-body)
1557       (let ((buffer-read-only nil)
1558             elem)
1559         (while (setq elem (pop map))
1560           (save-excursion
1561             (while (search-forward (car elem) nil t)
1562               (replace-match (cadr elem)))))))))
1563
1564 (defun article-treat-overstrike ()
1565   "Translate overstrikes into bold text."
1566   (interactive)
1567   (save-excursion
1568     (when (article-goto-body)
1569       (let ((buffer-read-only nil))
1570         (while (search-forward "\b" nil t)
1571           (let ((next (char-after))
1572                 start end previous)
1573             (backward-char 2)
1574             (setq start (point)
1575                   previous (char-after))
1576             (forward-char 3)
1577             (setq end (point))
1578             (backward-char)
1579             ;; We do the boldification/underlining by hiding the
1580             ;; overstrikes and putting the proper text property
1581             ;; on the letters.
1582             (cond
1583              ((eq next previous)
1584               (gnus-article-hide-text-type start (point) 'overstrike)
1585               (put-text-property (point) end 'face 'bold))
1586              ((eq next ?_)
1587               (gnus-article-hide-text-type
1588                (1- (point)) (1+ (point)) 'overstrike)
1589               (put-text-property
1590                start (1- (point)) 'face 'underline))
1591              ((eq previous ?_)
1592               (gnus-article-hide-text-type start (point) 'overstrike)
1593               (put-text-property
1594                (point) end 'face 'underline)))))))))
1595
1596 (defun article-fill-long-lines ()
1597   "Fill lines that are wider than the window width."
1598   (interactive)
1599   (save-excursion
1600     (let ((buffer-read-only nil)
1601           (width (window-width (get-buffer-window (current-buffer)))))
1602       (save-restriction
1603         (article-goto-body)
1604         (let ((adaptive-fill-mode nil))
1605           (while (not (eobp))
1606             (end-of-line)
1607             (when (>= (current-column) (min fill-column width))
1608               (narrow-to-region (point) (gnus-point-at-bol))
1609               (fill-paragraph nil)
1610               (goto-char (point-max))
1611               (widen))
1612             (forward-line 1)))))))
1613
1614 (defun article-capitalize-sentences ()
1615   "Capitalize the first word in each sentence."
1616   (interactive)
1617   (save-excursion
1618     (let ((buffer-read-only nil)
1619           (paragraph-start "^[\n\^L]"))
1620       (article-goto-body)
1621       (while (not (eobp))
1622         (capitalize-word 1)
1623         (forward-sentence)))))
1624
1625 (defun article-remove-cr ()
1626   "Remove trailing CRs and then translate remaining CRs into LFs."
1627   (interactive)
1628   (save-excursion
1629     (let ((buffer-read-only nil))
1630       (goto-char (point-min))
1631       (while (re-search-forward "\r+$" nil t)
1632         (replace-match "" t t))
1633       (goto-char (point-min))
1634       (while (search-forward "\r" nil t)
1635         (replace-match "\n" t t)))))
1636
1637 (defun article-remove-trailing-blank-lines ()
1638   "Remove all trailing blank lines from the article."
1639   (interactive)
1640   (save-excursion
1641     (let ((buffer-read-only nil))
1642       (goto-char (point-max))
1643       (delete-region
1644        (point)
1645        (progn
1646          (while (and (not (bobp))
1647                      (looking-at "^[ \t]*$")
1648                      (not (gnus-annotation-in-region-p
1649                            (point) (gnus-point-at-eol))))
1650            (forward-line -1))
1651          (forward-line 1)
1652          (point))))))
1653
1654 (defun article-display-x-face (&optional force)
1655   "Look for an X-Face header and display it if present."
1656   (interactive (list 'force))
1657   (save-excursion
1658     ;; Delete the old process, if any.
1659     (when (process-status "article-x-face")
1660       (delete-process "article-x-face"))
1661     (let ((inhibit-point-motion-hooks t)
1662           (case-fold-search t)
1663           from last)
1664       (save-restriction
1665         (article-narrow-to-head)
1666         (goto-char (point-min))
1667         (setq from (message-fetch-field "from"))
1668         (goto-char (point-min))
1669         (while (and gnus-article-x-face-command
1670                     (not last)
1671                     (or force
1672                         ;; Check whether this face is censored.
1673                         (not gnus-article-x-face-too-ugly)
1674                         (and gnus-article-x-face-too-ugly from
1675                              (not (string-match gnus-article-x-face-too-ugly
1676                                                 from))))
1677                     ;; Has to be present.
1678                     (re-search-forward "^X-Face:[ \t]*" nil t))
1679           ;; This used to try to do multiple faces (`while' instead of
1680           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
1681           ;; work (b) it can crash some versions of Emacs (c) are
1682           ;; multiple faces really something to encourage?
1683           (when (stringp gnus-article-x-face-command)
1684             (setq last t))
1685           ;; We now have the area of the buffer where the X-Face is stored.
1686           (save-excursion
1687             (let ((beg (point))
1688                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
1689               ;; We display the face.
1690               (if (symbolp gnus-article-x-face-command)
1691                   ;; The command is a lisp function, so we call it.
1692                   (if (gnus-functionp gnus-article-x-face-command)
1693                       (funcall gnus-article-x-face-command beg end)
1694                     (error "%s is not a function" gnus-article-x-face-command))
1695                 ;; The command is a string, so we interpret the command
1696                 ;; as a, well, command, and fork it off.
1697                 (let ((process-connection-type nil))
1698                   (process-kill-without-query
1699                    (start-process
1700                     "article-x-face" nil shell-file-name shell-command-switch
1701                     gnus-article-x-face-command))
1702                   (process-send-region "article-x-face" beg end)
1703                   (process-send-eof "article-x-face"))))))))))
1704
1705 (defun article-decode-mime-words ()
1706   "Decode all MIME-encoded words in the article."
1707   (interactive)
1708   (save-excursion
1709     (set-buffer gnus-article-buffer)
1710     (let ((inhibit-point-motion-hooks t)
1711           buffer-read-only
1712           (mail-parse-charset gnus-newsgroup-charset)
1713           (mail-parse-ignored-charsets 
1714            (save-excursion (set-buffer gnus-summary-buffer)
1715                            gnus-newsgroup-ignored-charsets)))
1716       (mail-decode-encoded-word-region (point-min) (point-max)))))
1717
1718 (defun article-decode-charset (&optional prompt)
1719   "Decode charset-encoded text in the article.
1720 If PROMPT (the prefix), prompt for a coding system to use."
1721   (interactive "P")
1722   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
1723         buffer-read-only
1724         (mail-parse-charset gnus-newsgroup-charset)
1725         (mail-parse-ignored-charsets 
1726          (save-excursion (condition-case nil
1727                              (set-buffer gnus-summary-buffer)
1728                            (error))
1729                          gnus-newsgroup-ignored-charsets))
1730         ct cte ctl charset format)
1731   (save-excursion
1732     (save-restriction
1733       (article-narrow-to-head)
1734       (setq ct (message-fetch-field "Content-Type" t)
1735             cte (message-fetch-field "Content-Transfer-Encoding" t)
1736             ctl (and ct (ignore-errors
1737                           (mail-header-parse-content-type ct)))
1738             charset (cond
1739                      (prompt
1740                       (mm-read-coding-system "Charset to decode: "))
1741                      (ctl
1742                       (mail-content-type-get ctl 'charset)))
1743             format (and ctl (mail-content-type-get ctl 'format)))
1744       (when cte
1745         (setq cte (mail-header-strip cte)))
1746       (if (and ctl (not (string-match "/" (car ctl)))) 
1747           (setq ctl nil))
1748       (goto-char (point-max)))
1749     (forward-line 1)
1750     (save-restriction
1751       (narrow-to-region (point) (point-max))
1752       (when (and (eq mail-parse-charset 'gnus-decoded)
1753                  (eq (mm-body-7-or-8) '8bit))
1754         ;; The text code could have been decoded.
1755         (setq charset mail-parse-charset))
1756       (when (and (or (not ctl)
1757                      (equal (car ctl) "text/plain"))
1758                  (not format)) ;; article with format will decode later.
1759         (mm-decode-body
1760          charset (and cte (intern (downcase
1761                                    (gnus-strip-whitespace cte))))
1762          (car ctl)))))))
1763
1764 (defun article-decode-encoded-words ()
1765   "Remove encoded-word encoding from headers."
1766   (let (buffer-read-only)
1767     (let ((charset (save-excursion
1768                      (set-buffer gnus-summary-buffer)
1769                      default-mime-charset)))
1770       (mime-decode-header-in-buffer charset)
1771       )))
1772
1773 (defun article-de-quoted-unreadable (&optional force)
1774   "Translate a quoted-printable-encoded article.
1775 If FORCE, decode the article whether it is marked as quoted-printable
1776 or not."
1777   (interactive (list 'force))
1778   (save-excursion
1779     (let ((buffer-read-only nil) type charset)
1780       (if (gnus-buffer-live-p gnus-original-article-buffer)
1781           (with-current-buffer gnus-original-article-buffer
1782             (setq type
1783                   (gnus-fetch-field "content-transfer-encoding"))
1784             (let* ((ct (gnus-fetch-field "content-type"))
1785                    (ctl (and ct 
1786                              (ignore-errors
1787                                (mail-header-parse-content-type ct)))))
1788               (setq charset (and ctl
1789                                  (mail-content-type-get ctl 'charset)))
1790               (if (stringp charset)
1791                   (setq charset (intern (downcase charset)))))))
1792       (unless charset 
1793         (setq charset gnus-newsgroup-charset))
1794       (when (or force
1795                 (and type (let ((case-fold-search t))
1796                             (string-match "quoted-printable" type))))
1797         (article-goto-body)
1798         (quoted-printable-decode-region
1799          (point) (point-max) (mm-charset-to-coding-system charset))))))
1800
1801 (defun article-de-base64-unreadable (&optional force)
1802   "Translate a base64 article.
1803 If FORCE, decode the article whether it is marked as base64 not."
1804   (interactive (list 'force))
1805   (save-excursion
1806     (let ((buffer-read-only nil) type charset)
1807       (if (gnus-buffer-live-p gnus-original-article-buffer)
1808           (with-current-buffer gnus-original-article-buffer
1809             (setq type
1810                   (gnus-fetch-field "content-transfer-encoding"))
1811             (let* ((ct (gnus-fetch-field "content-type"))
1812                    (ctl (and ct 
1813                              (ignore-errors
1814                                (mail-header-parse-content-type ct)))))
1815               (setq charset (and ctl
1816                                  (mail-content-type-get ctl 'charset)))
1817               (if (stringp charset)
1818                   (setq charset (intern (downcase charset)))))))
1819       (unless charset 
1820         (setq charset gnus-newsgroup-charset))
1821       (when (or force
1822                 (and type (let ((case-fold-search t))
1823                             (string-match "base64" type))))
1824         (article-goto-body)
1825         (save-restriction
1826           (narrow-to-region (point) (point-max))
1827           (base64-decode-region (point-min) (point-max))
1828           (mm-decode-coding-region
1829            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
1830
1831 (eval-when-compile
1832   (require 'rfc1843))
1833
1834 (defun article-decode-HZ ()
1835   "Translate a HZ-encoded article."
1836   (interactive)
1837   (require 'rfc1843)
1838   (save-excursion
1839     (let ((buffer-read-only nil))
1840       (rfc1843-decode-region (point-min) (point-max)))))
1841
1842 (defun article-wash-html ()
1843   "Format an html article."
1844   (interactive)
1845   (save-excursion
1846     (let ((buffer-read-only nil)
1847           charset)
1848       (if (gnus-buffer-live-p gnus-original-article-buffer)
1849           (with-current-buffer gnus-original-article-buffer
1850             (let* ((ct (gnus-fetch-field "content-type"))
1851                    (ctl (and ct 
1852                              (ignore-errors
1853                                (mail-header-parse-content-type ct)))))
1854               (setq charset (and ctl
1855                                  (mail-content-type-get ctl 'charset)))
1856               (if (stringp charset)
1857                   (setq charset (intern (downcase charset)))))))
1858       (unless charset 
1859         (setq charset gnus-newsgroup-charset))
1860       (article-goto-body)
1861       (save-window-excursion
1862         (save-restriction
1863           (narrow-to-region (point) (point-max))
1864           (mm-setup-w3)
1865           (let ((w3-strict-width (window-width))
1866                 (url-standalone-mode t))
1867             (condition-case var
1868                 (w3-region (point-min) (point-max))
1869               (error))))))))
1870
1871 (defun article-hide-list-identifiers ()
1872   "Remove list identifies from the Subject header.
1873 The `gnus-list-identifiers' variable specifies what to do."
1874   (interactive)
1875   (save-excursion
1876     (save-restriction
1877       (let ((inhibit-point-motion-hooks t)
1878             buffer-read-only)
1879         (article-narrow-to-head)
1880         (let ((regexp (if (stringp gnus-list-identifiers) gnus-list-identifiers
1881                         (mapconcat 'identity gnus-list-identifiers " *\\|"))))
1882           (when regexp
1883             (goto-char (point-min))
1884             (when (re-search-forward
1885                    (concat "^Subject: +\\(\\(\\(Re: +\\)?\\(" regexp 
1886                            " *\\)\\)+\\(Re: +\\)?\\)")
1887                    nil t)
1888               (let ((s (or (match-string 3) (match-string 5))))
1889                 (delete-region (match-beginning 1) (match-end 1))
1890                 (when s
1891                   (goto-char (match-beginning 1))
1892                   (insert s))))))))))
1893
1894 (defun article-hide-pgp ()
1895   "Remove any PGP headers and signatures in the current article."
1896   (interactive)
1897   (save-excursion
1898     (save-restriction
1899       (let ((inhibit-point-motion-hooks t)
1900             buffer-read-only beg end)
1901         (article-goto-body)
1902         ;; Hide the "header".
1903         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
1904           (push 'pgp gnus-article-wash-types)
1905           (delete-region (match-beginning 0) (match-end 0))
1906           ;; Remove armor headers (rfc2440 6.2)
1907           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
1908                                      (point)))
1909           (setq beg (point))
1910           ;; Hide the actual signature.
1911           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
1912                (setq end (1+ (match-beginning 0)))
1913                (delete-region
1914                 end
1915                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
1916                     (match-end 0)
1917                   ;; Perhaps we shouldn't hide to the end of the buffer
1918                   ;; if there is no end to the signature?
1919                   (point-max))))
1920           ;; Hide "- " PGP quotation markers.
1921           (when (and beg end)
1922             (narrow-to-region beg end)
1923             (goto-char (point-min))
1924             (while (re-search-forward "^- " nil t)
1925               (delete-region
1926                (match-beginning 0) (match-end 0)))
1927             (widen))
1928           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1929
1930 (defun article-hide-pem (&optional arg)
1931   "Toggle hiding of any PEM headers and signatures in the current article.
1932 If given a negative prefix, always show; if given a positive prefix,
1933 always hide."
1934   (interactive (gnus-article-hidden-arg))
1935   (unless (gnus-article-check-hidden-text 'pem arg)
1936     (save-excursion
1937       (let (buffer-read-only end)
1938         (goto-char (point-min))
1939         ;; Hide the horrendously ugly "header".
1940         (when (and (search-forward
1941                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1942                     nil t)
1943                    (setq end (1+ (match-beginning 0))))
1944           (push 'pem gnus-article-wash-types)
1945           (gnus-article-hide-text-type
1946            end
1947            (if (search-forward "\n\n" nil t)
1948                (match-end 0)
1949              (point-max))
1950            'pem)
1951           ;; Hide the trailer as well
1952           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1953                                 nil t)
1954             (gnus-article-hide-text-type
1955              (match-beginning 0) (match-end 0) 'pem)))))))
1956
1957 (defun article-strip-banner ()
1958   "Strip the banner specified by the `banner' group parameter."
1959   (interactive)
1960   (save-excursion
1961     (save-restriction
1962       (let ((inhibit-point-motion-hooks t)
1963             (banner (gnus-group-find-parameter gnus-newsgroup-name 'banner))
1964             (gnus-signature-limit nil)
1965             buffer-read-only beg end)
1966         (when banner
1967           (article-goto-body)
1968           (cond
1969            ((eq banner 'signature)
1970             (when (gnus-article-narrow-to-signature)
1971               (widen)
1972               (forward-line -1)
1973               (delete-region (point) (point-max))))
1974            ((symbolp banner)
1975             (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
1976                 (while (re-search-forward banner nil t)
1977                   (delete-region (match-beginning 0) (match-end 0)))))
1978            ((stringp banner)
1979             (while (re-search-forward banner nil t)
1980               (delete-region (match-beginning 0) (match-end 0))))))))))
1981
1982 (defun article-babel ()
1983   "Translate article using an online translation service."
1984   (interactive)
1985   (require 'babel)
1986   (save-excursion
1987     (set-buffer gnus-article-buffer)
1988     (when (article-goto-body)
1989       (let* ((buffer-read-only nil)
1990              (start (point))
1991              (end (point-max))
1992              (orig (buffer-substring start end))
1993              (trans (babel-as-string orig)))
1994         (save-restriction
1995           (narrow-to-region start end)
1996           (delete-region start end)
1997           (insert trans))))))
1998
1999 (defun article-hide-signature (&optional arg)
2000   "Hide the signature in the current article.
2001 If given a negative prefix, always show; if given a positive prefix,
2002 always hide."
2003   (interactive (gnus-article-hidden-arg))
2004   (save-excursion
2005     (save-restriction
2006       (if (interactive-p)
2007           (progn
2008             (widen)
2009             (article-goto-body))
2010         (goto-char (point-min)))
2011       (unless (gnus-article-check-hidden-text 'signature arg)
2012         (let ((buffer-read-only nil)
2013               (button (point)))
2014           (while (setq button (text-property-any button (point-max)
2015                                                  'gnus-callback
2016                                                  'gnus-signature-toggle))
2017             (setq button (text-property-not-all button (point-max)
2018                                                 'gnus-callback
2019                                                 'gnus-signature-toggle))
2020             (when (and button (not (eobp)))
2021               (gnus-article-hide-text-type
2022                (1+ button)
2023                (next-single-property-change (1+ button) 'mime-view-entity
2024                                             nil (point-max))
2025                'signature))))))))
2026
2027 (defun article-strip-headers-in-body ()
2028   "Strip offensive headers from bodies."
2029   (interactive)
2030   (save-excursion
2031     (article-goto-body)
2032     (let ((case-fold-search t))
2033       (when (looking-at "x-no-archive:")
2034         (gnus-delete-line)))))
2035
2036 (defun article-strip-leading-blank-lines ()
2037   "Remove all blank lines from the beginning of the article."
2038   (interactive)
2039   (save-excursion
2040     (let ((inhibit-point-motion-hooks t)
2041           buffer-read-only)
2042       (when (article-goto-body)
2043         (while (and (not (eobp))
2044                     (looking-at "[ \t]*$"))
2045           (gnus-delete-line))))))
2046
2047 (defun article-narrow-to-head ()
2048   "Narrow the buffer to the head of the message.
2049 Point is left at the beginning of the narrowed-to region."
2050   (narrow-to-region
2051    (goto-char (point-min))
2052    (if (search-forward "\n\n" nil 1)
2053        (1- (point))
2054      (point-max)))
2055   (goto-char (point-min)))
2056
2057 (defun article-goto-body ()
2058   "Place point at the start of the body."
2059   (goto-char (point-min))
2060   (cond
2061    ;; This variable is only bound when dealing with separate
2062    ;; MIME body parts.
2063    (article-goto-body-goes-to-point-min-p
2064     t)
2065    ((search-forward "\n\n" nil t)
2066     t)
2067    (t
2068     (goto-char (point-max))
2069     nil)))
2070
2071 (defun article-strip-multiple-blank-lines ()
2072   "Replace consecutive blank lines with one empty line."
2073   (interactive)
2074   (save-excursion
2075     (let ((inhibit-point-motion-hooks t)
2076           buffer-read-only)
2077       ;; First make all blank lines empty.
2078       (article-goto-body)
2079       (while (re-search-forward "^[ \t]+$" nil t)
2080         (unless (gnus-annotation-in-region-p
2081                  (match-beginning 0) (match-end 0))
2082           (replace-match "" nil t)))
2083       ;; Then replace multiple empty lines with a single empty line.
2084       (article-goto-body)
2085       (while (re-search-forward "\n\n\n+" nil t)
2086         (unless (gnus-annotation-in-region-p
2087                  (match-beginning 0) (match-end 0))
2088           (replace-match "\n\n" t t))))))
2089
2090 (defun article-strip-leading-space ()
2091   "Remove all white space from the beginning of the lines in the article."
2092   (interactive)
2093   (save-excursion
2094     (let ((inhibit-point-motion-hooks t)
2095           buffer-read-only)
2096       (article-goto-body)
2097       (while (re-search-forward "^[ \t]+" nil t)
2098         (replace-match "" t t)))))
2099
2100 (defun article-strip-trailing-space ()
2101   "Remove all white space from the end of the lines in the article."
2102   (interactive)
2103   (save-excursion
2104     (let ((inhibit-point-motion-hooks t)
2105           buffer-read-only)
2106       (article-goto-body)
2107       (while (re-search-forward "[ \t]+$" nil t)
2108         (replace-match "" t t)))))
2109
2110 (defun article-strip-blank-lines ()
2111   "Strip leading, trailing and multiple blank lines."
2112   (interactive)
2113   (article-strip-leading-blank-lines)
2114   (article-remove-trailing-blank-lines)
2115   (article-strip-multiple-blank-lines))
2116
2117 (defun article-strip-all-blank-lines ()
2118   "Strip all blank lines."
2119   (interactive)
2120   (save-excursion
2121     (let ((inhibit-point-motion-hooks t)
2122           buffer-read-only)
2123       (article-goto-body)
2124       (while (re-search-forward "^[ \t]*\n" nil t)
2125         (replace-match "" t t)))))
2126
2127 (defun gnus-article-narrow-to-signature ()
2128   "Narrow to the signature; return t if a signature is found, else nil."
2129   (let ((inhibit-point-motion-hooks t))
2130     (when (gnus-article-search-signature)
2131       (forward-line 1)
2132       ;; Check whether we have some limits to what we consider
2133       ;; to be a signature.
2134       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2135                       (list gnus-signature-limit)))
2136             limit limited)
2137         (while (setq limit (pop limits))
2138           (if (or (and (integerp limit)
2139                        (< (- (point-max) (point)) limit))
2140                   (and (floatp limit)
2141                        (< (count-lines (point) (point-max)) limit))
2142                   (and (gnus-functionp limit)
2143                        (funcall limit))
2144                   (and (stringp limit)
2145                        (not (re-search-forward limit nil t))))
2146               ()                        ; This limit did not succeed.
2147             (setq limited t
2148                   limits nil)))
2149         (unless limited
2150           (narrow-to-region (point) (point-max))
2151           t)))))
2152
2153 (defun gnus-article-search-signature ()
2154   "Search the current buffer for the signature separator.
2155 Put point at the beginning of the signature separator."
2156   (let ((cur (point)))
2157     (goto-char (point-max))
2158     (if (if (stringp gnus-signature-separator)
2159             (re-search-backward gnus-signature-separator nil t)
2160           (let ((seps gnus-signature-separator))
2161             (while (and seps
2162                         (not (re-search-backward (car seps) nil t)))
2163               (pop seps))
2164             seps))
2165         t
2166       (goto-char cur)
2167       nil)))
2168
2169 (defun gnus-article-hidden-arg ()
2170   "Return the current prefix arg as a number, or 0 if no prefix."
2171   (list (if current-prefix-arg
2172             (prefix-numeric-value current-prefix-arg)
2173           0)))
2174
2175 (defun gnus-article-check-hidden-text (type arg)
2176   "Return nil if hiding is necessary.
2177 Arg can be nil or a number.  Nil and positive means hide, negative
2178 means show, 0 means toggle."
2179   (save-excursion
2180     (save-restriction
2181       (let ((hide (gnus-article-hidden-text-p type)))
2182         (cond
2183          ((or (null arg)
2184               (> arg 0))
2185           nil)
2186          ((< arg 0)
2187           (gnus-article-show-hidden-text type))
2188          (t
2189           (if (eq hide 'hidden)
2190               (gnus-article-show-hidden-text type)
2191             nil)))))))
2192
2193 (defun gnus-article-hidden-text-p (type)
2194   "Say whether the current buffer contains hidden text of type TYPE."
2195   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2196     (while (and pos
2197                 (not (get-text-property pos 'invisible))
2198                 (not (get-text-property pos 'dummy-invisible)))
2199       (setq pos
2200             (text-property-any (1+ pos) (point-max) 'article-type type)))
2201     (if pos
2202         'hidden
2203       nil)))
2204
2205 (defun gnus-article-show-hidden-text (type &optional dummy)
2206   "Show all hidden text of type TYPE.
2207 Originally it is hide instead of DUMMY."
2208   (let ((buffer-read-only nil)
2209         (inhibit-point-motion-hooks t))
2210     (gnus-remove-text-properties-when 
2211      'article-type type
2212      (point-min) (point-max) 
2213      (cons 'article-type (cons type
2214                                gnus-hidden-properties)))))
2215
2216 (defconst article-time-units
2217   `((year . ,(* 365.25 24 60 60))
2218     (week . ,(* 7 24 60 60))
2219     (day . ,(* 24 60 60))
2220     (hour . ,(* 60 60))
2221     (minute . 60)
2222     (second . 1))
2223   "Mapping from time units to seconds.")
2224
2225 (defun article-date-ut (&optional type highlight header)
2226   "Convert DATE date to universal time in the current article.
2227 If TYPE is `local', convert to local time; if it is `lapsed', output
2228 how much time has lapsed since DATE.  For `lapsed', the value of
2229 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2230 should replace the \"Date:\" one, or should be added below it."
2231   (interactive (list 'ut t))
2232   (let* ((header (or header
2233                      (and (eq 1 (point-min))
2234                           (mail-header-date (save-excursion
2235                                               (set-buffer gnus-summary-buffer)
2236                                               gnus-current-headers)))
2237                      (message-fetch-field "date")
2238                      ""))
2239          (date (if (vectorp header) (mail-header-date header)
2240                  header))
2241          (inhibit-point-motion-hooks t)
2242          bface eface date-pos)
2243     (when (and date (not (string= date "")))
2244       (save-excursion
2245         (save-restriction
2246           (article-narrow-to-head)
2247           (when (or (and (eq type 'lapsed)
2248                          gnus-article-date-lapsed-new-header
2249                          ;; Attempt to get the face of X-Sent first.
2250                          (re-search-forward "^X-Sent:[ \t]" nil t))
2251                     (re-search-forward "^Date:[ \t]" nil t)
2252                     ;; If Date is missing, try again for X-Sent.
2253                     (re-search-forward "^X-Sent:[ \t]" nil t))
2254             (setq bface (get-text-property (gnus-point-at-bol) 'face)
2255                   date (or (get-text-property (gnus-point-at-bol)
2256                                                 'original-date)
2257                              date)
2258                   eface (get-text-property (1- (gnus-point-at-eol))
2259                                            'face)))
2260           (let ((buffer-read-only nil))
2261             ;; Delete any old X-Sent headers.
2262             (when (setq date-pos
2263                         (text-property-any (point-min) (point-max)
2264                                            'article-date-lapsed t))
2265               (goto-char (setq date-pos (set-marker (make-marker) date-pos)))
2266               (delete-region (match-beginning 0)
2267                              (progn (forward-line 1) (point))))
2268             (goto-char (point-min))
2269             ;; Delete any old Date headers.
2270             (while (re-search-forward "^Date:[ \t]" nil t)
2271               (unless date-pos
2272                 (setq date-pos (match-beginning 0)))
2273               (unless (and (eq type 'lapsed)
2274                            gnus-article-date-lapsed-new-header)
2275                 (delete-region (match-beginning 0)
2276                                (progn (message-next-header) (point)))))
2277             (if date-pos
2278                 (progn
2279                   (goto-char date-pos)
2280                   (unless (bolp)
2281                     ;; Possibly, Date has been deleted.
2282                     (insert "\n"))
2283                   (when (and (eq type 'lapsed)
2284                              gnus-article-date-lapsed-new-header
2285                              (looking-at "Date:"))
2286                     (forward-line 1)))
2287               (goto-char (point-min)))
2288             (insert (article-make-date-line date type))
2289             (when (eq type 'lapsed)
2290               (put-text-property (gnus-point-at-bol) (point)
2291                                  'article-date-lapsed t))
2292             (insert "\n")
2293             (forward-line -1)
2294             ;; Do highlighting.
2295             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2296               (put-text-property (match-beginning 1) (1+ (match-end 1))
2297                                  'original-date date)
2298               (put-text-property (match-beginning 1) (1+ (match-end 1))
2299                                  'face bface)
2300               (put-text-property (match-beginning 2) (match-end 2)
2301                                  'face eface))))))))
2302
2303 (defun article-make-date-line (date type)
2304   "Return a DATE line of TYPE."
2305   (let ((time (condition-case ()
2306                   (date-to-time date)
2307                 (error '(0 0)))))
2308     (cond
2309      ;; Convert to the local timezone.  We have to slap a
2310      ;; `condition-case' round the calls to the timezone
2311      ;; functions since they aren't particularly resistant to
2312      ;; buggy dates.
2313      ((eq type 'local)
2314       (let ((tz (car (current-time-zone time))))
2315         (format "Date: %s %s%02d%02d" (current-time-string time)
2316                 (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
2317                 (/ (% (abs tz) 3600) 60))))
2318      ;; Convert to Universal Time.
2319      ((eq type 'ut)
2320       (concat "Date: "
2321               (current-time-string
2322                (let* ((e (parse-time-string date))
2323                       (tm (apply 'encode-time e))
2324                       (ms (car tm))
2325                       (ls (- (cadr tm) (car (current-time-zone time)))))
2326                  (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2327                        ((> ls 65535) (list (1+ ms) (- ls 65536)))
2328                        (t (list ms ls)))))
2329               " UT"))
2330      ;; Get the original date from the article.
2331      ((eq type 'original)
2332       (concat "Date: " (if (string-match "\n+$" date)
2333                            (substring date 0 (match-beginning 0))
2334                          date)))
2335      ;; Let the user define the format.
2336      ((eq type 'user)
2337       (if (gnus-functionp gnus-article-time-format)
2338           (funcall gnus-article-time-format time)
2339         (concat
2340          "Date: "
2341          (format-time-string gnus-article-time-format time))))
2342      ;; ISO 8601.
2343      ((eq type 'iso8601)
2344       (let ((tz (car (current-time-zone time))))
2345         (concat
2346          "Date: "
2347          (format-time-string "%Y%m%dT%H%M%S" time)
2348          (format "%s%02d%02d"
2349                  (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
2350                  (/ (% (abs tz) 3600) 60)))))
2351      ;; Do an X-Sent lapsed format.
2352      ((eq type 'lapsed)
2353       ;; If the date is seriously mangled, the timezone functions are
2354       ;; liable to bug out, so we ignore all errors.
2355       (let* ((now (current-time))
2356              (real-time (subtract-time now time))
2357              (real-sec (and real-time
2358                             (+ (* (float (car real-time)) 65536)
2359                                (cadr real-time))))
2360              (sec (and real-time (abs real-sec)))
2361              num prev)
2362         (cond
2363          ((null real-time)
2364           "X-Sent: Unknown")
2365          ((zerop sec)
2366           "X-Sent: Now")
2367          (t
2368           (concat
2369            "X-Sent: "
2370            ;; This is a bit convoluted, but basically we go
2371            ;; through the time units for years, weeks, etc,
2372            ;; and divide things to see whether that results
2373            ;; in positive answers.
2374            (mapconcat
2375             (lambda (unit)
2376               (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2377                   ;; The (remaining) seconds are too few to
2378                   ;; be divided into this time unit.
2379                   ""
2380                 ;; It's big enough, so we output it.
2381                 (setq sec (- sec (* num (cdr unit))))
2382                 (prog1
2383                     (concat (if prev ", " "") (int-to-string
2384                                                (floor num))
2385                             " " (symbol-name (car unit))
2386                             (if (> num 1) "s" ""))
2387                   (setq prev t))))
2388             article-time-units "")
2389            ;; If dates are odd, then it might appear like the
2390            ;; article was sent in the future.
2391            (if (> real-sec 0)
2392                " ago"
2393              " in the future"))))))
2394      (t
2395       (error "Unknown conversion type: %s" type)))))
2396
2397 (defun article-date-local (&optional highlight)
2398   "Convert the current article date to the local timezone."
2399   (interactive (list t))
2400   (article-date-ut 'local highlight))
2401
2402 (defun article-date-original (&optional highlight)
2403   "Convert the current article date to what it was originally.
2404 This is only useful if you have used some other date conversion
2405 function and want to see what the date was before converting."
2406   (interactive (list t))
2407   (article-date-ut 'original highlight))
2408
2409 (defun article-date-lapsed (&optional highlight)
2410   "Convert the current article date to time lapsed since it was sent."
2411   (interactive (list t))
2412   (article-date-ut 'lapsed highlight))
2413
2414 (defun article-update-date-lapsed ()
2415   "Function to be run from a timer to update the lapsed time line."
2416   (let (deactivate-mark)
2417     (save-excursion
2418       (ignore-errors
2419         (walk-windows
2420          (lambda (w)
2421            (set-buffer (window-buffer w))
2422            (when (eq major-mode 'gnus-article-mode)
2423              (goto-char (point-min))
2424              (when (re-search-forward "^X-Sent:" nil t)
2425                (article-date-lapsed t))))
2426          nil 'visible)))))
2427
2428 (defun gnus-start-date-timer (&optional n)
2429   "Start a timer to update the X-Sent header in the article buffers.
2430 The numerical prefix says how frequently (in seconds) the function
2431 is to run."
2432   (interactive "p")
2433   (unless n
2434     (setq n 1))
2435   (gnus-stop-date-timer)
2436   (setq article-lapsed-timer
2437         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2438
2439 (defun gnus-stop-date-timer ()
2440   "Stop the X-Sent timer."
2441   (interactive)
2442   (when article-lapsed-timer
2443     (nnheader-cancel-timer article-lapsed-timer)
2444     (setq article-lapsed-timer nil)))
2445
2446 (defun article-date-user (&optional highlight)
2447   "Convert the current article date to the user-defined format.
2448 This format is defined by the `gnus-article-time-format' variable."
2449   (interactive (list t))
2450   (article-date-ut 'user highlight))
2451
2452 (defun article-date-iso8601 (&optional highlight)
2453   "Convert the current article date to ISO8601."
2454   (interactive (list t))
2455   (article-date-ut 'iso8601 highlight))
2456
2457 (defun article-show-all ()
2458   "Show all hidden text in the article buffer."
2459   (interactive)
2460   (save-excursion
2461     (widen)
2462     (let ((buffer-read-only nil))
2463       (gnus-article-unhide-text (point-min) (point-max))
2464       (gnus-remove-text-with-property 'gnus-prev)
2465       (gnus-remove-text-with-property 'gnus-next))))
2466
2467 (defun article-show-all-headers ()
2468   "Show all hidden headers in the article buffer."
2469   (interactive)
2470   (save-excursion
2471     (save-restriction
2472       (widen)
2473       (article-narrow-to-head)
2474       (let ((buffer-read-only nil))
2475         (gnus-article-unhide-text (point-min) (point-max))))))
2476
2477 (defun article-emphasize (&optional arg)
2478   "Emphasize text according to `gnus-emphasis-alist'."
2479   (interactive (gnus-article-hidden-arg))
2480   (unless (gnus-article-check-hidden-text 'emphasis arg)
2481     (save-excursion
2482       (let ((alist (or 
2483                     (condition-case nil
2484                         (with-current-buffer gnus-summary-buffer 
2485                           gnus-article-emphasis-alist) 
2486                       (error))
2487                     gnus-emphasis-alist))
2488             (buffer-read-only nil)
2489             (props (append '(article-type emphasis)
2490                            gnus-hidden-properties))
2491             regexp elem beg invisible visible face)
2492         (article-goto-body)
2493         (setq beg (point))
2494         (while (setq elem (pop alist))
2495           (goto-char beg)
2496           (setq regexp (car elem)
2497                 invisible (nth 1 elem)
2498                 visible (nth 2 elem)
2499                 face (nth 3 elem))
2500           (while (re-search-forward regexp nil t)
2501             (when (and (match-beginning visible) (match-beginning invisible))
2502               (push 'emphasis gnus-article-wash-types)
2503               (gnus-article-hide-text
2504                (match-beginning invisible) (match-end invisible) props)
2505               (gnus-article-unhide-text-type
2506                (match-beginning visible) (match-end visible) 'emphasis)
2507               (gnus-put-text-property-excluding-newlines
2508                (match-beginning visible) (match-end visible) 'face face)
2509               (goto-char (match-end invisible)))))))))
2510
2511 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2512   "Setup newsgroup emphasis alist."
2513   (unless gnus-article-emphasis-alist
2514     (let ((name (and gnus-newsgroup-name
2515                      (gnus-group-real-name gnus-newsgroup-name))))
2516       (make-local-variable 'gnus-article-emphasis-alist)
2517       (setq gnus-article-emphasis-alist
2518             (nconc
2519              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2520                (while (setq elem (pop alist))
2521                  (when (and name (string-match (car elem) name))
2522                    (setq alist nil
2523                          highlight (copy-sequence (cdr elem)))))
2524                highlight)
2525              (copy-sequence highlight-words)
2526              (if gnus-newsgroup-name
2527                  (copy-sequence (gnus-group-find-parameter
2528                                  gnus-newsgroup-name 'highlight-words t)))
2529              gnus-emphasis-alist)))))
2530
2531 (defvar gnus-summary-article-menu)
2532 (defvar gnus-summary-post-menu)
2533
2534 ;;; Saving functions.
2535
2536 (defun gnus-article-save (save-buffer file &optional num)
2537   "Save the currently selected article."
2538   (unless gnus-save-all-headers
2539     ;; Remove headers according to `gnus-saved-headers'.
2540     (let ((gnus-visible-headers
2541            (or gnus-saved-headers gnus-visible-headers))
2542           (gnus-article-buffer save-buffer))
2543       (save-excursion
2544         (set-buffer save-buffer)
2545         (article-hide-headers 1 t))))
2546   (save-window-excursion
2547     (if (not gnus-default-article-saver)
2548         (error "No default saver is defined")
2549       ;; !!! Magic!  The saving functions all save
2550       ;; `gnus-save-article-buffer' (or so they think), but we
2551       ;; bind that variable to our save-buffer.
2552       (set-buffer gnus-article-buffer)
2553       (let* ((gnus-save-article-buffer save-buffer)
2554              (filename
2555               (cond
2556                ((not gnus-prompt-before-saving) 'default)
2557                ((eq gnus-prompt-before-saving 'always) nil)
2558                (t file)))
2559              (gnus-number-of-articles-to-be-saved
2560               (when (eq gnus-prompt-before-saving t)
2561                 num)))                  ; Magic
2562         (set-buffer gnus-article-current-summary)
2563         (funcall gnus-default-article-saver filename)))))
2564
2565 (defun gnus-read-save-file-name (prompt &optional filename
2566                                         function group headers variable)
2567   (let ((default-name
2568           (funcall function group headers (symbol-value variable)))
2569         result)
2570     (setq result
2571           (expand-file-name
2572            (cond
2573             ((eq filename 'default)
2574              default-name)
2575             ((eq filename t)
2576              default-name)
2577             (filename filename)
2578             (t
2579              (let* ((split-name (gnus-get-split-value gnus-split-methods))
2580                     (prompt
2581                      (format prompt
2582                              (if (and gnus-number-of-articles-to-be-saved
2583                                       (> gnus-number-of-articles-to-be-saved 1))
2584                                  (format "these %d articles"
2585                                          gnus-number-of-articles-to-be-saved)
2586                                "this article")))
2587                     (file
2588                      ;; Let the split methods have their say.
2589                      (cond
2590                       ;; No split name was found.
2591                       ((null split-name)
2592                        (read-file-name
2593                         (concat prompt " (default "
2594                                 (file-name-nondirectory default-name) ") ")
2595                         (file-name-directory default-name)
2596                         default-name))
2597                       ;; A single group name is returned.
2598                       ((stringp split-name)
2599                        (setq default-name
2600                              (funcall function split-name headers
2601                                       (symbol-value variable)))
2602                        (read-file-name
2603                         (concat prompt " (default "
2604                                 (file-name-nondirectory default-name) ") ")
2605                         (file-name-directory default-name)
2606                         default-name))
2607                       ;; A single split name was found
2608                       ((= 1 (length split-name))
2609                        (let* ((name (expand-file-name
2610                                      (car split-name)
2611                                      gnus-article-save-directory))
2612                               (dir (cond ((file-directory-p name)
2613                                           (file-name-as-directory name))
2614                                          ((file-exists-p name) name)
2615                                          (t gnus-article-save-directory))))
2616                          (read-file-name
2617                           (concat prompt " (default " name ") ")
2618                           dir name)))
2619                       ;; A list of splits was found.
2620                       (t
2621                        (setq split-name (nreverse split-name))
2622                        (let (result)
2623                          (let ((file-name-history
2624                                 (nconc split-name file-name-history)))
2625                            (setq result
2626                                  (expand-file-name
2627                                   (read-file-name
2628                                    (concat prompt " (`M-p' for defaults) ")
2629                                    gnus-article-save-directory
2630                                    (car split-name))
2631                                   gnus-article-save-directory)))
2632                          (car (push result file-name-history)))))))
2633                ;; Create the directory.
2634                (gnus-make-directory (file-name-directory file))
2635       ;; If we have read a directory, we append the default file name.
2636                (when (file-directory-p file)
2637                  (setq file (expand-file-name (file-name-nondirectory
2638                                                default-name)
2639                                               (file-name-as-directory file))))
2640                ;; Possibly translate some characters.
2641                (nnheader-translate-file-chars file))))))
2642     (gnus-make-directory (file-name-directory result))
2643     (set variable result)))
2644
2645 (defun gnus-article-archive-name (group)
2646   "Return the first instance of an \"Archive-name\" in the current buffer."
2647   (let ((case-fold-search t))
2648     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2649       (nnheader-concat gnus-article-save-directory
2650                        (match-string 1)))))
2651
2652 (defun gnus-article-nndoc-name (group)
2653   "If GROUP is an nndoc group, return the name of the parent group."
2654   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2655     (gnus-group-get-parameter group 'save-article-group)))
2656
2657 (defun gnus-summary-save-in-rmail (&optional filename)
2658   "Append this article to Rmail file.
2659 Optional argument FILENAME specifies file name.
2660 Directory to save to is default to `gnus-article-save-directory'."
2661   (setq filename (gnus-read-save-file-name
2662                   "Save %s in rmail file:" filename
2663                   gnus-rmail-save-name gnus-newsgroup-name
2664                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2665   (gnus-eval-in-buffer-window gnus-save-article-buffer
2666     (save-excursion
2667       (save-restriction
2668         (widen)
2669         (gnus-output-to-rmail filename))))
2670   filename)
2671
2672 (defun gnus-summary-save-in-mail (&optional filename)
2673   "Append this article to Unix mail file.
2674 Optional argument FILENAME specifies file name.
2675 Directory to save to is default to `gnus-article-save-directory'."
2676   (setq filename (gnus-read-save-file-name
2677                   "Save %s in Unix mail file:" filename
2678                   gnus-mail-save-name gnus-newsgroup-name
2679                   gnus-current-headers 'gnus-newsgroup-last-mail))
2680   (gnus-eval-in-buffer-window gnus-save-article-buffer
2681     (save-excursion
2682       (save-restriction
2683         (widen)
2684         (if (and (file-readable-p filename)
2685                  (mail-file-babyl-p filename))
2686             (rmail-output-to-rmail-file filename t)
2687           (gnus-output-to-mail filename)))))
2688   filename)
2689
2690 (defun gnus-summary-save-in-file (&optional filename overwrite)
2691   "Append this article to file.
2692 Optional argument FILENAME specifies file name.
2693 Directory to save to is default to `gnus-article-save-directory'."
2694   (setq filename (gnus-read-save-file-name
2695                   "Save %s in file:" filename
2696                   gnus-file-save-name gnus-newsgroup-name
2697                   gnus-current-headers 'gnus-newsgroup-last-file))
2698   (gnus-eval-in-buffer-window gnus-save-article-buffer
2699     (save-excursion
2700       (save-restriction
2701         (widen)
2702         (when (and overwrite
2703                    (file-exists-p filename))
2704           (delete-file filename))
2705         (gnus-output-to-file filename))))
2706   filename)
2707
2708 (defun gnus-summary-write-to-file (&optional filename)
2709   "Write this article to a file.
2710 Optional argument FILENAME specifies file name.
2711 The directory to save in defaults to `gnus-article-save-directory'."
2712   (gnus-summary-save-in-file nil t))
2713
2714 (defun gnus-summary-save-body-in-file (&optional filename)
2715   "Append this article body to a file.
2716 Optional argument FILENAME specifies file name.
2717 The directory to save in defaults to `gnus-article-save-directory'."
2718   (setq filename (gnus-read-save-file-name
2719                   "Save %s body in file:" filename
2720                   gnus-file-save-name gnus-newsgroup-name
2721                   gnus-current-headers 'gnus-newsgroup-last-file))
2722   (gnus-eval-in-buffer-window gnus-save-article-buffer
2723     (save-excursion
2724       (save-restriction
2725         (widen)
2726         (when (article-goto-body)
2727           (narrow-to-region (point) (point-max)))
2728         (gnus-output-to-file filename))))
2729   filename)
2730
2731 (defun gnus-summary-save-in-pipe (&optional command)
2732   "Pipe this article to subprocess."
2733   (setq command
2734         (cond ((and (eq command 'default)
2735                     gnus-last-shell-command)
2736                gnus-last-shell-command)
2737               (command command)
2738               (t (read-string
2739                   (format
2740                    "Shell command on %s: "
2741                    (if (and gnus-number-of-articles-to-be-saved
2742                             (> gnus-number-of-articles-to-be-saved 1))
2743                        (format "these %d articles"
2744                                gnus-number-of-articles-to-be-saved)
2745                      "this article"))
2746                   gnus-last-shell-command))))
2747   (when (string-equal command "")
2748     (setq command gnus-last-shell-command))
2749   (gnus-eval-in-buffer-window gnus-article-buffer
2750     (save-restriction
2751       (widen)
2752       (shell-command-on-region (point-min) (point-max) command nil)))
2753   (setq gnus-last-shell-command command))
2754
2755 ;;; Article file names when saving.
2756
2757 (defun gnus-capitalize-newsgroup (newsgroup)
2758   "Capitalize NEWSGROUP name."
2759   (when (not (zerop (length newsgroup)))
2760     (concat (char-to-string (upcase (aref newsgroup 0)))
2761             (substring newsgroup 1))))
2762
2763 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2764   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2765 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2766 Otherwise, it is like ~/News/news/group/num."
2767   (let ((default
2768           (expand-file-name
2769            (concat (if (gnus-use-long-file-name 'not-save)
2770                        (gnus-capitalize-newsgroup newsgroup)
2771                      (gnus-newsgroup-directory-form newsgroup))
2772                    "/" (int-to-string (mail-header-number headers)))
2773            gnus-article-save-directory)))
2774     (if (and last-file
2775              (string-equal (file-name-directory default)
2776                            (file-name-directory last-file))
2777              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2778         default
2779       (or last-file default))))
2780
2781 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2782   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2783 If variable `gnus-use-long-file-name' is non-nil, it is
2784 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2785   (let ((default
2786           (expand-file-name
2787            (concat (if (gnus-use-long-file-name 'not-save)
2788                        newsgroup
2789                      (gnus-newsgroup-directory-form newsgroup))
2790                    "/" (int-to-string (mail-header-number headers)))
2791            gnus-article-save-directory)))
2792     (if (and last-file
2793              (string-equal (file-name-directory default)
2794                            (file-name-directory last-file))
2795              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2796         default
2797       (or last-file default))))
2798
2799 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2800   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2801 If variable `gnus-use-long-file-name' is non-nil, it is
2802 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2803   (or last-file
2804       (expand-file-name
2805        (if (gnus-use-long-file-name 'not-save)
2806            newsgroup
2807          (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup)))
2808        gnus-article-save-directory)))
2809
2810 (defun article-verify-x-pgp-sig ()
2811   "Verify X-PGP-Sig."
2812   (interactive)
2813   (if (gnus-buffer-live-p gnus-original-article-buffer)
2814       (let ((sig (with-current-buffer gnus-original-article-buffer
2815                    (gnus-fetch-field "X-PGP-Sig")))
2816             items info headers)
2817         (when (and sig 
2818                    mml2015-use
2819                    (mml2015-clear-verify-function))
2820           (with-temp-buffer
2821             (insert-buffer gnus-original-article-buffer)
2822             (setq items (split-string sig))
2823             (message-narrow-to-head)
2824             (let ((inhibit-point-motion-hooks t)
2825                   (case-fold-search t))
2826               ;; Don't verify multiple headers.
2827               (setq headers (mapconcat (lambda (header)
2828                                          (concat header ": " 
2829                                                  (mail-fetch-field header) "\n"))
2830                                        (split-string (nth 1 items) ",") "")))
2831             (delete-region (point-min) (point-max))
2832             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
2833             (insert "X-Signed-Headers: " (nth 1 items) "\n")
2834             (insert headers)
2835             (widen)
2836             (forward-line)
2837             (while (not (eobp))
2838               (if (looking-at "^-")
2839                   (insert "- "))
2840               (forward-line))
2841             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
2842             (insert "Version: " (car items) "\n\n")
2843             (insert (mapconcat 'identity (cddr items) "\n"))
2844             (insert "\n-----END PGP SIGNATURE-----\n")
2845             (let ((mm-security-handle (list (format "multipart/signed"))))
2846               (mml2015-clean-buffer)
2847               (let ((coding-system-for-write (or gnus-newsgroup-charset
2848                                                  'iso-8859-1)))
2849                 (funcall (mml2015-clear-verify-function)))
2850               (setq info 
2851                     (or (mm-handle-multipart-ctl-parameter 
2852                          mm-security-handle 'gnus-details)
2853                         (mm-handle-multipart-ctl-parameter 
2854                          mm-security-handle 'gnus-info)))))
2855           (when info
2856             (let (buffer-read-only bface eface)
2857               (save-restriction
2858                 (message-narrow-to-head)
2859                 (goto-char (point-max))
2860                 (forward-line -1)
2861                 (setq bface (get-text-property (gnus-point-at-bol) 'face)
2862                       eface (get-text-property (1- (gnus-point-at-eol)) 'face))
2863                 (message-remove-header "X-Gnus-PGP-Verify")
2864                 (if (re-search-forward "^X-PGP-Sig:" nil t)
2865                     (forward-line)
2866                   (goto-char (point-max)))
2867                 (narrow-to-region (point) (point))
2868                 (insert "X-Gnus-PGP-Verify: " info "\n")
2869                 (goto-char (point-min))
2870                 (forward-line)
2871                 (while (not (eobp))
2872                   (if (not (looking-at "^[ \t]"))
2873                       (insert " "))
2874                   (forward-line))
2875                 ;; Do highlighting.
2876                 (goto-char (point-min))
2877                 (when (looking-at "\\([^:]+\\): *")
2878                   (put-text-property (match-beginning 1) (1+ (match-end 1))
2879                                      'face bface)
2880                   (put-text-property (match-end 0) (point-max)
2881                                      'face eface)))))))))
2882
2883 (eval-and-compile
2884   (mapcar
2885    (lambda (func)
2886      (let (afunc gfunc)
2887        (if (consp func)
2888            (setq afunc (car func)
2889                  gfunc (cdr func))
2890          (setq afunc func
2891                gfunc (intern (format "gnus-%s" func))))
2892        (defalias gfunc
2893          (if (fboundp afunc)
2894            `(lambda (&optional interactive &rest args)
2895               ,(documentation afunc t)
2896               (interactive (list t))
2897               (save-excursion
2898                 (set-buffer gnus-article-buffer)
2899                 (if interactive
2900                     (call-interactively ',afunc)
2901                   (apply ',afunc args))))))))
2902    '(article-hide-headers
2903      article-verify-x-pgp-sig
2904      article-hide-boring-headers
2905      article-toggle-headers
2906      article-treat-overstrike
2907      article-fill-long-lines
2908      article-capitalize-sentences
2909      article-remove-cr
2910      article-display-x-face
2911      article-de-quoted-unreadable
2912      article-de-base64-unreadable
2913      article-decode-HZ
2914      article-wash-html
2915      article-hide-list-identifiers
2916      article-hide-pgp
2917      article-strip-banner
2918      article-babel
2919      article-hide-pem
2920      article-hide-signature
2921      article-strip-headers-in-body
2922      article-remove-trailing-blank-lines
2923      article-strip-leading-blank-lines
2924      article-strip-multiple-blank-lines
2925      article-strip-leading-space
2926      article-strip-trailing-space
2927      article-strip-blank-lines
2928      article-strip-all-blank-lines
2929      article-date-local
2930      article-date-iso8601
2931      article-date-original
2932      article-date-ut
2933      article-decode-mime-words
2934      article-decode-charset
2935      article-decode-encoded-words
2936      article-date-user
2937      article-date-lapsed
2938      article-emphasize
2939      article-treat-dumbquotes
2940      article-normalize-headers
2941      (article-show-all-headers . gnus-article-show-all-headers)
2942      (article-show-all . gnus-article-show-all))))
2943 \f
2944 ;;;
2945 ;;; Gnus article mode
2946 ;;;
2947
2948 (put 'gnus-article-mode 'mode-class 'special)
2949
2950 (gnus-define-keys gnus-article-mode-map
2951   " " gnus-article-goto-next-page
2952   "\177" gnus-article-goto-prev-page
2953   [delete] gnus-article-goto-prev-page
2954   [backspace] gnus-article-goto-prev-page
2955   "\C-c^" gnus-article-refer-article
2956   "h" gnus-article-show-summary
2957   "s" gnus-article-show-summary
2958   "\C-c\C-m" gnus-article-mail
2959   "?" gnus-article-describe-briefly
2960   "e" gnus-summary-edit-article
2961   "<" beginning-of-buffer
2962   ">" end-of-buffer
2963   "\C-c\C-i" gnus-info-find-node
2964   "\C-c\C-b" gnus-bug
2965   "\C-hk" gnus-article-describe-key
2966   "\C-hc" gnus-article-describe-key-briefly
2967
2968   "\C-d" gnus-article-read-summary-keys
2969   "\M-*" gnus-article-read-summary-keys
2970   "\M-#" gnus-article-read-summary-keys
2971   "\M-^" gnus-article-read-summary-keys
2972   "\M-g" gnus-article-read-summary-keys)
2973
2974 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
2975 (mapcar
2976  (lambda (key)
2977    (unless (lookup-key gnus-article-mode-map key)
2978      (define-key gnus-article-mode-map key
2979        'gnus-article-read-summary-keys)))
2980  (delq nil
2981        (append
2982         (mapcar
2983          (lambda (elt)
2984            (let ((key (car elt)))
2985              (and (> (length key) 0)
2986                   (not (eq 'menu-bar (aref key 0)))
2987                   (symbolp (lookup-key gnus-summary-mode-map key))
2988                   key)))
2989          (accessible-keymaps gnus-summary-mode-map))
2990         (let ((c 127)
2991               keys)
2992           (while (>= c 32)
2993             (push (char-to-string c) keys)
2994             (decf c))
2995           keys))))
2996
2997 (eval-when-compile 
2998   (defvar gnus-article-commands-menu))
2999
3000 (defun gnus-article-make-menu-bar ()
3001   (gnus-turn-off-edit-menu 'article)
3002   (unless (boundp 'gnus-article-article-menu)
3003     (easy-menu-define
3004      gnus-article-article-menu gnus-article-mode-map ""
3005      '("Article"
3006        ["Scroll forwards" gnus-article-goto-next-page t]
3007        ["Scroll backwards" gnus-article-goto-prev-page t]
3008        ["Show summary" gnus-article-show-summary t]
3009        ["Fetch Message-ID at point" gnus-article-refer-article t]
3010        ["Mail to address at point" gnus-article-mail t]
3011        ["Send a bug report" gnus-bug t]))
3012
3013     (easy-menu-define
3014      gnus-article-treatment-menu gnus-article-mode-map ""
3015      ;; Fixme: this should use :active (and maybe :visible).
3016      '("Treatment"
3017        ["Hide headers" gnus-article-toggle-headers t]
3018        ["Hide signature" gnus-article-hide-signature t]
3019        ["Hide citation" gnus-article-hide-citation t]
3020        ["Treat overstrike" gnus-article-treat-overstrike t]
3021        ["Remove carriage return" gnus-article-remove-cr t]
3022        ["Decode HZ" gnus-article-decode-HZ t]))
3023
3024     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3025
3026     (when (boundp 'gnus-summary-post-menu)
3027       (define-key gnus-article-mode-map [menu-bar post]
3028         (cons "Post" gnus-summary-post-menu)))
3029
3030     (gnus-run-hooks 'gnus-article-menu-hook))
3031   ;; Add the menu.
3032   (when (boundp 'gnus-article-commands-menu)
3033     (easy-menu-add gnus-article-commands-menu gnus-article-mode-map))
3034   (when (boundp 'gnus-summary-post-menu)
3035     (easy-menu-add gnus-summary-post-menu gnus-article-mode-map)))
3036
3037 ;; Fixme: do something for the Emacs tool bar in Article mode a la
3038 ;; Summary.
3039
3040 (defun gnus-article-mode ()
3041   "Major mode for displaying an article.
3042
3043 All normal editing commands are switched off.
3044
3045 The following commands are available in addition to all summary mode
3046 commands:
3047 \\<gnus-article-mode-map>
3048 \\[gnus-article-next-page]\t Scroll the article one page forwards
3049 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3050 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3051 \\[gnus-article-show-summary]\t Display the summary buffer
3052 \\[gnus-article-mail]\t Send a reply to the address near point
3053 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3054 \\[gnus-info-find-node]\t Go to the Gnus info node"
3055   (interactive)
3056   (when (gnus-visual-p 'article-menu 'menu)
3057     (gnus-article-make-menu-bar))
3058   (gnus-simplify-mode-line)
3059   (setq mode-name "Article")
3060   (setq major-mode 'gnus-article-mode)
3061   (make-local-variable 'minor-mode-alist)
3062   (unless (assq 'gnus-show-mime minor-mode-alist)
3063     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
3064   (use-local-map gnus-article-mode-map)
3065   (gnus-update-format-specifications nil 'article-mode)
3066   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3067   (make-local-variable 'gnus-page-broken)
3068   (make-local-variable 'gnus-button-marker-list)
3069   (make-local-variable 'gnus-article-current-summary)
3070   (make-local-variable 'gnus-article-mime-handles)
3071   (make-local-variable 'gnus-article-decoded-p)
3072   (make-local-variable 'gnus-article-mime-handle-alist)
3073   (make-local-variable 'gnus-article-wash-types)
3074   (make-local-variable 'gnus-article-charset)
3075   (make-local-variable 'gnus-article-ignored-charsets)
3076   (gnus-set-default-directory)
3077   (buffer-disable-undo)
3078   (setq buffer-read-only t)
3079   (set-syntax-table gnus-article-mode-syntax-table)
3080   (gnus-run-hooks 'gnus-article-mode-hook))
3081
3082 (defun gnus-article-setup-buffer ()
3083   "Initialize the article buffer."
3084   (let* ((name (if gnus-single-article-buffer "*Article*"
3085                  (concat "*Article " gnus-newsgroup-name "*")))
3086          (original
3087           (progn (string-match "\\*Article" name)
3088                  (concat " *Original Article"
3089                          (substring name (match-end 0))))))
3090     (setq gnus-article-buffer name)
3091     (setq gnus-original-article-buffer original)
3092     (setq gnus-article-mime-handle-alist nil)
3093     ;; This might be a variable local to the summary buffer.
3094     (unless gnus-single-article-buffer
3095       (save-excursion
3096         (set-buffer gnus-summary-buffer)
3097         (setq gnus-article-buffer name)
3098         (setq gnus-original-article-buffer original)
3099         (gnus-set-global-variables)))
3100     (gnus-article-setup-highlight-words)
3101     ;; Init original article buffer.
3102     (save-excursion
3103       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3104       (set-buffer-multibyte nil)
3105       (setq major-mode 'gnus-original-article-mode)
3106       (make-local-variable 'gnus-original-article))
3107     (if (get-buffer name)
3108         (save-excursion
3109           (set-buffer name)
3110           (buffer-disable-undo)
3111           (setq buffer-read-only t)
3112           (unless (eq major-mode 'gnus-article-mode)
3113             (gnus-article-mode))
3114           (current-buffer))
3115       (save-excursion
3116         (set-buffer (gnus-get-buffer-create name))
3117         (gnus-article-mode)
3118         (make-local-variable 'gnus-summary-buffer)
3119         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3120         (current-buffer)))))
3121
3122 ;; Set article window start at LINE, where LINE is the number of lines
3123 ;; from the head of the article.
3124 (defun gnus-article-set-window-start (&optional line)
3125   (set-window-start
3126    (get-buffer-window gnus-article-buffer t)
3127    (save-excursion
3128      (set-buffer gnus-article-buffer)
3129      (goto-char (point-min))
3130      (if (not line)
3131          (point-min)
3132        (gnus-message 6 "Moved to bookmark")
3133        (search-forward "\n\n" nil t)
3134        (forward-line line)
3135        (point)))))
3136
3137 ;;; @@ article filters
3138 ;;;
3139
3140 (defun gnus-article-display-mime-message ()
3141   "Article display method for MIME message."
3142   ;; called from `gnus-original-article-buffer'.
3143   (let (charset all-headers)
3144     (with-current-buffer gnus-summary-buffer
3145       (setq charset default-mime-charset
3146             all-headers gnus-have-all-headers))
3147     (make-local-variable 'default-mime-charset)
3148     (setq default-mime-charset charset)
3149     (with-current-buffer (get-buffer-create gnus-article-buffer)
3150       (make-local-variable 'default-mime-charset)
3151       (setq default-mime-charset charset))
3152     (mime-display-message mime-message-structure
3153                           gnus-article-buffer nil gnus-article-mode-map)
3154     (when all-headers
3155       (gnus-article-hide-headers nil -1)))
3156   (run-hooks 'gnus-mime-article-prepare-hook))
3157
3158 (defun gnus-article-display-traditional-message ()
3159   "Article display method for traditional message."
3160   (set-buffer gnus-article-buffer)
3161   (let (buffer-read-only)
3162     (erase-buffer)
3163     (insert-buffer-substring gnus-original-article-buffer)))
3164
3165 (defun gnus-article-make-full-mail-header (&optional number charset)
3166   "Create a new mail header structure in a raw article buffer."
3167   (unless (and number charset)
3168     (save-current-buffer
3169       (set-buffer gnus-summary-buffer)
3170       (unless number
3171         (setq number (or (cdr gnus-article-current) 0)))
3172       (unless charset
3173         (setq charset (or default-mime-charset 'x-ctext)))))
3174   (goto-char (point-min))
3175   (let ((header-end (if (search-forward "\n\n" nil t)
3176                         (1- (point))
3177                       (goto-char (point-max))))
3178         (chars (- (point-max) (point)))
3179         (lines (count-lines (point) (point-max)))
3180         (default-mime-charset charset)
3181         xref)
3182     (narrow-to-region (point-min) header-end)
3183     (setq xref (std11-fetch-field "xref"))
3184     (prog1
3185         (make-full-mail-header
3186          number
3187          (std11-fetch-field "subject")
3188          (std11-fetch-field "from")
3189          (std11-fetch-field "date")
3190          (std11-fetch-field "message-id")
3191          (std11-fetch-field "references")
3192          chars
3193          lines
3194          (when xref (concat "Xref: " xref)))
3195       (widen))))
3196
3197 (defun gnus-article-prepare (article &optional all-headers header)
3198   "Prepare ARTICLE in article mode buffer.
3199 ARTICLE should either be an article number or a Message-ID.
3200 If ARTICLE is an id, HEADER should be the article headers.
3201 If ALL-HEADERS is non-nil, no headers are hidden."
3202   (save-excursion
3203     ;; Make sure we start in a summary buffer.
3204     (unless (eq major-mode 'gnus-summary-mode)
3205       (set-buffer gnus-summary-buffer))
3206     (setq gnus-summary-buffer (current-buffer))
3207     (let* ((gnus-article (if header (mail-header-number header) article))
3208            (summary-buffer (current-buffer))
3209            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3210            (group gnus-newsgroup-name)
3211            result)
3212       (save-excursion
3213         (gnus-article-setup-buffer)
3214         (set-buffer gnus-original-article-buffer)
3215         ;; Deactivate active regions.
3216         (when (and (boundp 'transient-mark-mode)
3217                    transient-mark-mode)
3218           (setq mark-active nil))
3219         (if (not (setq result (let ((buffer-read-only nil))
3220                                 (gnus-request-article-this-buffer
3221                                  article group))))
3222             ;; There is no such article.
3223             (save-excursion
3224               (when (and (numberp article)
3225                          (not (memq article gnus-newsgroup-sparse)))
3226                 (setq gnus-article-current
3227                       (cons gnus-newsgroup-name article))
3228                 (set-buffer gnus-summary-buffer)
3229                 (setq gnus-current-article article)
3230                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
3231                     (progn
3232                       (gnus-summary-set-agent-mark article)
3233                       (message "Message marked for downloading"))
3234                   (gnus-summary-mark-article article gnus-canceled-mark)
3235                   (unless (memq article gnus-newsgroup-sparse)
3236                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3237           (if (or (eq result 'pseudo)
3238                   (eq result 'nneething))
3239               (progn
3240                 (save-excursion
3241                   (set-buffer summary-buffer)
3242                   (push article gnus-newsgroup-history)
3243                   (setq gnus-last-article gnus-current-article
3244                         gnus-current-article 0
3245                         gnus-current-headers nil
3246                         gnus-article-current nil)
3247                   (if (eq result 'nneething)
3248                       (gnus-configure-windows 'summary)
3249                     (gnus-configure-windows 'article))
3250                   (gnus-set-global-variables))
3251                 (let ((gnus-article-mime-handle-alist-1
3252                        gnus-article-mime-handle-alist))
3253                   (gnus-set-mode-line 'article)))
3254             ;; The result from the `request' was an actual article -
3255             ;; or at least some text that is now displayed in the
3256             ;; article buffer.
3257             (when (and (numberp article)
3258                        (not (eq article gnus-current-article)))
3259               ;; Seems like a new article has been selected.
3260               ;; `gnus-current-article' must be an article number.
3261               (save-excursion
3262                 (set-buffer summary-buffer)
3263                 (push article gnus-newsgroup-history)
3264                 (setq gnus-last-article gnus-current-article
3265                       gnus-current-article article
3266                       gnus-current-headers
3267                       (gnus-summary-article-header gnus-current-article)
3268                       gnus-article-current
3269                       (cons gnus-newsgroup-name gnus-current-article))
3270                 (unless (vectorp gnus-current-headers)
3271                   (setq gnus-current-headers nil))
3272                 (gnus-summary-goto-subject gnus-current-article)
3273                 (when (gnus-summary-show-thread)
3274                   ;; If the summary buffer really was folded, the
3275                   ;; previous goto may not actually have gone to
3276                   ;; the right article, but the thread root instead.
3277                   ;; So we go again.
3278                   (gnus-summary-goto-subject gnus-current-article))
3279                 (gnus-run-hooks 'gnus-mark-article-hook)
3280                 (gnus-set-mode-line 'summary)
3281                 (when (gnus-visual-p 'article-highlight 'highlight)
3282                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3283                 ;; Set the global newsgroup variables here.
3284                 (gnus-set-global-variables)
3285                 (setq gnus-have-all-headers
3286                       (or all-headers gnus-show-all-headers))))
3287             (save-excursion
3288               (gnus-configure-windows 'article))
3289             (when (or (numberp article)
3290                       (stringp article))
3291               (gnus-article-prepare-display)
3292               ;; Do page break.
3293               (goto-char (point-min))
3294               (setq gnus-page-broken
3295                     (when gnus-break-pages
3296                       (gnus-narrow-to-page)
3297                       t)))
3298             (let ((gnus-article-mime-handle-alist-1
3299                    gnus-article-mime-handle-alist))
3300               (gnus-set-mode-line 'article))
3301             (article-goto-body)
3302             (set-window-point (get-buffer-window (current-buffer)) (point))
3303             (gnus-configure-windows 'article)
3304             t))))))
3305
3306 (defun gnus-article-prepare-mime-display (&optional number)
3307   (goto-char (point-min))
3308   (when (re-search-forward "^[^\t ]+:" nil t)
3309     (goto-char (match-beginning 0)))
3310   (let ((entity (if (eq 1 (point-min))
3311                     (get-text-property 1 'mime-view-entity)
3312                   (get-text-property (point) 'mime-view-entity)))
3313         last-entity child-entity next type)
3314     (setq child-entity (mime-entity-children entity))
3315     (if child-entity
3316         (setq last-entity (nth (1- (length child-entity))
3317                                child-entity))
3318       (setq last-entity entity))
3319     (save-restriction
3320       (narrow-to-region (point)
3321                         (if (search-forward "\n\n" nil t)
3322                             (point)
3323                           (point-max)))
3324       (gnus-treat-article 'head)
3325       (put-text-property (point-min) (point-max) 'article-treated-header t)
3326       (goto-char (point-max)))
3327     (while (and (not (eobp)) entity)
3328       (setq next (set-marker
3329                   (make-marker)
3330                   (next-single-property-change (point) 'mime-view-entity
3331                                                nil (point-max))))
3332       (let ((types (mime-entity-content-type entity)))
3333         (while (eq 'multipart (mime-content-type-primary-type types))
3334           (setq entity (car (mime-entity-children entity))
3335                 types (mime-entity-content-type entity)))
3336         (when types
3337           (setq type (format "%s/%s"
3338                              (mime-content-type-primary-type types)
3339                              (mime-content-type-subtype types)))))
3340       (if (string-equal type "message/rfc822")
3341           (progn
3342             (setq next (point))
3343             (let ((children (mime-entity-children entity))
3344                   last-children)
3345               (when children
3346                 (setq last-children (nth (1- (length children)) children))
3347                 (while
3348                     (and
3349                      (not (eq last-children
3350                               (get-text-property next 'mime-view-entity)))
3351                      (setq next
3352                            (next-single-property-change next
3353                                                         'mime-view-entity
3354                                                         nil (point-max)))))))
3355             (setq next (next-single-property-change next 'mime-view-entity
3356                                                     nil (point-max)))
3357             (save-restriction
3358               (narrow-to-region (point) next)
3359               (gnus-article-prepare-mime-display)
3360               (goto-char (point-max)))
3361             (setq entity (get-text-property (point) 'mime-view-entity)))
3362         (save-restriction
3363           (narrow-to-region (point) next)
3364           ;; Kludge. We have to count true number, but for now,
3365           ;; part number is here only to achieve `last'.
3366           (gnus-treat-article nil 1
3367                               (if (eq entity last-entity)
3368                                   1 2)
3369                               type)
3370           (goto-char (point-max)))
3371         (setq entity (get-text-property next 'mime-view-entity))))))
3372
3373 ;;;###autoload
3374 (defun gnus-article-prepare-display ()
3375   "Make the current buffer look like a nice article."
3376   (setq gnus-article-wash-types nil)
3377   (gnus-run-hooks 'gnus-tmp-internal-hook)
3378   (gnus-run-hooks 'gnus-article-prepare-hook)
3379   ;; Display message.
3380   (let (mime-display-header-hook mime-display-text/plain-hook)
3381     (funcall (if gnus-show-mime
3382                  (progn
3383                    (setq mime-message-structure gnus-current-headers)
3384                    (mime-buffer-entity-set-buffer-internal
3385                     mime-message-structure
3386                     gnus-original-article-buffer)
3387                    (mime-entity-set-representation-type-internal
3388                     mime-message-structure 'mime-buffer-entity)
3389                    (luna-send mime-message-structure
3390                               'initialize-instance
3391                               mime-message-structure)
3392                    gnus-article-display-method-for-mime)
3393                gnus-article-display-method-for-traditional)))
3394   ;; Associate this article with the current summary buffer.
3395   (setq gnus-article-current-summary gnus-summary-buffer)
3396   ;; Call the treatment functions.
3397   (let ((inhibit-read-only t)
3398         buffer-read-only)
3399     (save-restriction
3400       (widen)
3401       (if gnus-show-mime
3402           (gnus-article-prepare-mime-display)
3403         (narrow-to-region (goto-char (point-min))
3404                           (if (search-forward "\n\n" nil t)
3405                               (point)
3406                             (point-max)))
3407         (gnus-treat-article 'head)
3408         (put-text-property (point-min) (point-max) 'article-treated-header t)
3409         (goto-char (point-max))
3410         (widen)
3411         (narrow-to-region (point) (point-max))
3412         (gnus-treat-article nil))
3413       (put-text-property (point-min) (point-max) 'read-only nil)))
3414   ;; Perform the article display hooks.  Incidentally, this hook is
3415   ;; an obsolete variable by now.
3416   (gnus-run-hooks 'gnus-article-display-hook))
3417
3418 (defun gnus-article-decode-article-as-default-mime-charset ()
3419   "Decode an article as `default-mime-charset'.  It won't work if the
3420 value of the variable `gnus-show-mime' is non-nil."
3421   (unless gnus-show-mime
3422     (decode-mime-charset-region (point-min) (point-max)
3423                                 (with-current-buffer gnus-summary-buffer
3424                                   default-mime-charset))))
3425
3426 ;;;
3427 ;;; Gnus MIME viewing functions
3428 ;;;
3429
3430 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3431   "The following specs can be used:
3432 %t  The MIME type
3433 %T  MIME type, along with additional info
3434 %n  The `name' parameter
3435 %d  The description, if any
3436 %l  The length of the encoded part
3437 %p  The part identifier number
3438 %e  Dots if the part isn't displayed")
3439
3440 (defvar gnus-mime-button-line-format-alist
3441   '((?t gnus-tmp-type ?s)
3442     (?T gnus-tmp-type-long ?s)
3443     (?n gnus-tmp-name ?s)
3444     (?d gnus-tmp-description ?s)
3445     (?p gnus-tmp-id ?s)
3446     (?l gnus-tmp-length ?d)
3447     (?e gnus-tmp-dots ?s)))
3448
3449 (defvar gnus-mime-button-commands
3450   '((gnus-article-press-button "\r" "Toggle Display")
3451     (gnus-mime-view-part "v" "View Interactively...")
3452     (gnus-mime-view-part-as-type "t" "View As Type...")
3453     (gnus-mime-view-part-as-charset "C" "View As charset...")
3454     (gnus-mime-save-part "o" "Save...")
3455     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
3456     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
3457     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
3458     (gnus-mime-internalize-part "E" "View Internally")
3459     (gnus-mime-externalize-part "e" "View Externally")
3460     (gnus-mime-pipe-part "|" "Pipe To Command...")
3461     (gnus-mime-action-on-part "." "Take action on the part")))
3462
3463 (defun gnus-article-mime-part-status ()
3464   (with-current-buffer gnus-article-buffer
3465     (let ((entity (get-text-property (point-min) 'mime-view-entity)))
3466       (if (and entity (mime-entity-children entity))
3467           (format " (%d parts)" (length (mime-entity-children entity)))
3468         ""))))
3469
3470 (defvar gnus-mime-button-map
3471   (let ((map (make-sparse-keymap)))
3472     ;; Not for Emacs 21: fixme better.
3473     ;; (set-keymap-parent map gnus-article-mode-map)
3474     (define-key map gnus-mouse-2 'gnus-article-push-button)
3475     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
3476     (dolist (c gnus-mime-button-commands)
3477       (define-key map (cadr c) (car c)))
3478     map))
3479
3480 (defun gnus-mime-button-menu (event)
3481   "Construct a context-sensitive menu of MIME commands."
3482   (interactive "e")
3483   (save-window-excursion
3484     (let ((pos (event-start event)))
3485       (select-window (posn-window pos))
3486       (goto-char (posn-point pos))
3487       (gnus-article-check-buffer)
3488       (let ((response (x-popup-menu
3489                        t `("MIME Part"
3490                            ("" ,@(mapcar (lambda (c)
3491                                            (cons (caddr c) (car c)))
3492                                          gnus-mime-button-commands))))))
3493         (if response
3494             (call-interactively response))))))
3495
3496 (defun gnus-mime-view-all-parts (&optional handles)
3497   "View all the MIME parts."
3498   (interactive)
3499   (save-current-buffer
3500     (set-buffer gnus-article-buffer)
3501     (let ((handles (or handles gnus-article-mime-handles))
3502           (mail-parse-charset gnus-newsgroup-charset)
3503           (mail-parse-ignored-charsets 
3504            (with-current-buffer gnus-summary-buffer
3505              gnus-newsgroup-ignored-charsets)))
3506       (when handles
3507         (mm-remove-parts handles)
3508         (goto-char (point-min))
3509         (or (search-forward "\n\n") (goto-char (point-max)))
3510         (let (buffer-read-only)
3511           (delete-region (point) (point-max)))
3512         (mm-display-parts handles)))))
3513
3514 (defun gnus-mime-save-part-and-strip ()
3515   "Save the MIME part under point then replace it with an external body."
3516   (interactive)
3517   (gnus-article-check-buffer)
3518   (let* ((data (get-text-property (point) 'gnus-data)) 
3519          (file (and data (mm-save-part data)))
3520          param)
3521     (when file
3522       (with-current-buffer (mm-handle-buffer data)
3523         (erase-buffer)
3524         (insert "Content-Type: " (mm-handle-media-type data))
3525         (mml-insert-parameter-string (cdr (mm-handle-type data))
3526                                      '(charset))
3527         (insert "\n")
3528         (insert "Content-ID: " (message-make-message-id) "\n")
3529         (insert "Content-Transfer-Encoding: binary\n")
3530         (insert "\n"))
3531       (setcdr data
3532               (cdr (mm-make-handle nil 
3533                                    `("message/external-body"
3534                                      (access-type . "LOCAL-FILE")
3535                                      (name . ,file)))))
3536       (set-buffer gnus-summary-buffer)
3537       (gnus-article-edit-article
3538        `(lambda () 
3539            (erase-buffer)
3540            (let ((mail-parse-charset (or gnus-article-charset 
3541                                          ',gnus-newsgroup-charset))
3542                  (mail-parse-ignored-charsets 
3543                   (or gnus-article-ignored-charsets
3544                       ',gnus-newsgroup-ignored-charsets))
3545                  (mbl mml-buffer-list))
3546              (setq mml-buffer-list nil)
3547              (insert-buffer gnus-original-article-buffer)
3548              (mime-to-mml gnus-article-mime-handles)
3549              (setq gnus-article-mime-handles nil)
3550              (make-local-hook 'kill-buffer-hook)
3551              (let ((mbl1 mml-buffer-list))
3552                (setq mml-buffer-list mbl)
3553                (set (make-local-variable 'mml-buffer-list) mbl1))
3554              (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
3555        `(lambda (no-highlight)
3556           (let ((mail-parse-charset (or gnus-article-charset
3557                                         ',gnus-newsgroup-charset))
3558                 (message-options message-options)
3559                 (message-options-set-recipient)
3560                 (mail-parse-ignored-charsets  
3561                  (or gnus-article-ignored-charsets
3562                      ',gnus-newsgroup-ignored-charsets)))
3563            (mml-to-mime)
3564            (mml-destroy-buffers)
3565            (remove-hook 'kill-buffer-hook 
3566                         'mml-destroy-buffers t)
3567            (kill-local-variable 'mml-buffer-list))
3568           (gnus-summary-edit-article-done
3569            ,(or (mail-header-references gnus-current-headers) "")
3570            ,(gnus-group-read-only-p) 
3571            ,gnus-summary-buffer no-highlight))))))
3572
3573 (defun gnus-mime-save-part ()
3574   "Save the MIME part under point."
3575   (interactive)
3576   (gnus-article-check-buffer)
3577   (let ((data (get-text-property (point) 'gnus-data)))
3578     (when data
3579       (mm-save-part data))))
3580
3581 (defun gnus-mime-pipe-part ()
3582   "Pipe the MIME part under point to a process."
3583   (interactive)
3584   (gnus-article-check-buffer)
3585   (let ((data (get-text-property (point) 'gnus-data)))
3586     (when data
3587       (mm-pipe-part data))))
3588
3589 (defun gnus-mime-view-part ()
3590   "Interactively choose a viewing method for the MIME part under point."
3591   (interactive)
3592   (gnus-article-check-buffer)
3593   (let ((data (get-text-property (point) 'gnus-data)))
3594     (when data
3595       (mm-interactively-view-part data))))
3596
3597 (defun gnus-mime-view-part-as-type-internal ()
3598   (gnus-article-check-buffer)
3599   (let* ((name (mail-content-type-get
3600                 (mm-handle-type (get-text-property (point) 'gnus-data))
3601                 'name))
3602          (def-type (and name (mm-default-file-encoding name))))
3603     (and def-type (cons def-type 0))))
3604
3605 (defun gnus-mime-view-part-as-type (mime-type)
3606   "Choose a MIME media type, and view the part as such."
3607   (interactive
3608    (list (completing-read
3609           "View as MIME type: "
3610           (mapcar #'list (mailcap-mime-types))
3611           nil nil
3612           (gnus-mime-view-part-as-type-internal))))
3613   (gnus-article-check-buffer)
3614   (let ((handle (get-text-property (point) 'gnus-data)))
3615     (when handle
3616       (gnus-mm-display-part
3617        (mm-make-handle (mm-handle-buffer handle)
3618                        (cons mime-type (cdr (mm-handle-type handle)))
3619                        (mm-handle-encoding handle)
3620                        (mm-handle-undisplayer handle)
3621                        (mm-handle-disposition handle)
3622                        (mm-handle-description handle)
3623                        (mm-handle-cache handle)
3624                        (mm-handle-id handle))))))
3625   
3626 (defun gnus-mime-copy-part (&optional handle)
3627   "Put the the MIME part under point into a new buffer."
3628   (interactive)
3629   (gnus-article-check-buffer)
3630   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3631          (contents (and handle (mm-get-part handle)))
3632          (base (and handle 
3633                     (file-name-nondirectory
3634                      (or
3635                       (mail-content-type-get (mm-handle-type handle) 'name)
3636                       (mail-content-type-get (mm-handle-type handle)
3637                                              'filename)
3638                       "*decoded*"))))
3639          (buffer (and base (generate-new-buffer base))))
3640     (when contents
3641       (switch-to-buffer buffer)
3642       (insert contents)
3643       ;; We do it this way to make `normal-mode' set the appropriate mode.
3644       (unwind-protect
3645           (progn
3646             (setq buffer-file-name (expand-file-name base))
3647             (normal-mode))
3648         (setq buffer-file-name nil))
3649       (goto-char (point-min)))))
3650
3651 (defun gnus-mime-inline-part (&optional handle arg)
3652   "Insert the MIME part under point into the current buffer."
3653   (interactive (list nil current-prefix-arg))
3654   (gnus-article-check-buffer)
3655   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3656          contents charset
3657          (b (point))
3658          buffer-read-only)
3659     (when handle
3660       (if (and (not arg) (mm-handle-undisplayer handle))
3661           (mm-remove-part handle)
3662         (setq contents (mm-get-part handle))
3663         (cond
3664          ((not arg)
3665           (setq charset (or (mail-content-type-get
3666                              (mm-handle-type handle) 'charset)
3667                             gnus-newsgroup-charset)))
3668          ((numberp arg)
3669           (if (mm-handle-undisplayer handle)
3670               (mm-remove-part handle))
3671           (setq charset
3672                 (or (cdr (assq arg 
3673                                gnus-summary-show-article-charset-alist))
3674                     (read-coding-system "Charset: ")))))
3675         (forward-line 2)
3676         (mm-insert-inline handle
3677                           (if (and charset 
3678                                    (setq charset (mm-charset-to-coding-system 
3679                                                   charset))
3680                                    (not (eq charset 'ascii)))
3681                               (mm-decode-coding-string contents charset)
3682                             contents))
3683         (goto-char b)))))
3684
3685 (defun gnus-mime-view-part-as-charset (&optional handle arg)
3686   "Insert the MIME part under point into the current buffer."
3687   (interactive (list nil current-prefix-arg))
3688   (gnus-article-check-buffer)
3689   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3690          contents charset
3691          (b (point))
3692          buffer-read-only)
3693     (when handle
3694       (if (mm-handle-undisplayer handle)
3695           (mm-remove-part handle))
3696       (let ((gnus-newsgroup-charset
3697              (or (cdr (assq arg 
3698                             gnus-summary-show-article-charset-alist))
3699                  (read-coding-system "Charset: ")))
3700           (gnus-newsgroup-ignored-charsets 'gnus-all))
3701         (gnus-article-press-button)))))
3702
3703 (defun gnus-mime-externalize-part (&optional handle)
3704   "View the MIME part under point with an external viewer."
3705   (interactive)
3706   (gnus-article-check-buffer)
3707   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3708          (mm-user-display-methods nil)
3709          (mm-inlined-types nil)
3710          (mail-parse-charset gnus-newsgroup-charset)
3711          (mail-parse-ignored-charsets 
3712           (save-excursion (set-buffer gnus-summary-buffer)
3713                           gnus-newsgroup-ignored-charsets)))
3714     (when handle
3715       (if (mm-handle-undisplayer handle)
3716           (mm-remove-part handle)
3717         (mm-display-part handle)))))
3718
3719 (defun gnus-mime-internalize-part (&optional handle)
3720   "View the MIME part under point with an internal viewer.
3721 In no internal viewer is available, use an external viewer."
3722   (interactive)
3723   (gnus-article-check-buffer)
3724   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3725          (mm-inlined-types '(".*"))
3726          (mm-inline-large-images t)
3727          (mail-parse-charset gnus-newsgroup-charset)
3728          (mail-parse-ignored-charsets 
3729           (save-excursion (set-buffer gnus-summary-buffer)
3730                           gnus-newsgroup-ignored-charsets)))
3731     (when handle
3732       (if (mm-handle-undisplayer handle)
3733           (mm-remove-part handle)
3734         (mm-display-part handle)))))
3735
3736 (defun gnus-mime-action-on-part (&optional action)
3737   "Do something with the MIME attachment at \(point\)."
3738   (interactive
3739    (list (completing-read "Action: " gnus-mime-action-alist)))
3740   (gnus-article-check-buffer)
3741   (let ((action-pair (assoc action gnus-mime-action-alist)))
3742     (if action-pair
3743         (funcall (cdr action-pair)))))
3744
3745 (defun gnus-article-part-wrapper (n function)
3746   (save-current-buffer
3747     (set-buffer gnus-article-buffer)
3748     (when (> n (length gnus-article-mime-handle-alist))
3749       (error "No such part"))
3750     (gnus-article-goto-part n)
3751     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3752       (funcall function handle))))
3753
3754 (defun gnus-article-pipe-part (n)
3755   "Pipe MIME part N, which is the numerical prefix."
3756   (interactive "p")
3757   (gnus-article-part-wrapper n 'mm-pipe-part))
3758
3759 (defun gnus-article-save-part (n)
3760   "Save MIME part N, which is the numerical prefix."
3761   (interactive "p")
3762   (gnus-article-part-wrapper n 'mm-save-part))
3763
3764 (defun gnus-article-interactively-view-part (n)
3765   "View MIME part N interactively, which is the numerical prefix."
3766   (interactive "p")
3767   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3768
3769 (defun gnus-article-copy-part (n)
3770   "Copy MIME part N, which is the numerical prefix."
3771   (interactive "p")
3772   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3773
3774 (defun gnus-article-view-part-as-charset (n)
3775   "Copy MIME part N, which is the numerical prefix."
3776   (interactive "p")
3777   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
3778
3779 (defun gnus-article-externalize-part (n)
3780   "View MIME part N externally, which is the numerical prefix."
3781   (interactive "p")
3782   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3783
3784 (defun gnus-article-inline-part (n)
3785   "Inline MIME part N, which is the numerical prefix."
3786   (interactive "p")
3787   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3788
3789 (defun gnus-article-mime-match-handle-first (condition)
3790   (if condition
3791       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3792         (while (setq ihandle (pop alist))
3793           (if (and (cond 
3794                     ((functionp condition)
3795                      (funcall condition (cdr ihandle)))
3796                     ((eq condition 'undisplayed) 
3797                      (not (or (mm-handle-undisplayer (cdr ihandle))
3798                               (equal (mm-handle-media-type (cdr ihandle))
3799                                      "multipart/alternative"))))
3800                     ((eq condition 'undisplayed-alternative)
3801                      (not (mm-handle-undisplayer (cdr ihandle))))
3802                     (t t))
3803                    (gnus-article-goto-part (car ihandle))
3804                    (or (not n) (< (car ihandle) n)))
3805               (setq n (car ihandle))))
3806         (or n 1))
3807     1))
3808
3809 (defun gnus-article-view-part (&optional n)
3810   "View MIME part N, which is the numerical prefix."
3811   (interactive "P")
3812   (save-current-buffer
3813     (set-buffer gnus-article-buffer)
3814     (or (numberp n) (setq n (gnus-article-mime-match-handle-first 
3815                              gnus-article-mime-match-handle-function)))
3816     (when (> n (length gnus-article-mime-handle-alist))
3817       (error "No such part"))
3818     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3819       (when (gnus-article-goto-part n)
3820         (if (equal (car handle) "multipart/alternative")
3821             (gnus-article-press-button)
3822           (when (eq (gnus-mm-display-part handle) 'internal)
3823             (gnus-set-window-start)))))))
3824
3825 (defsubst gnus-article-mime-total-parts ()
3826   (if (bufferp (car gnus-article-mime-handles))
3827       1 ;; single part
3828     (1- (length gnus-article-mime-handles))))
3829
3830 (defun gnus-mm-display-part (handle)
3831   "Display HANDLE and fix MIME button."
3832   (let ((id (get-text-property (point) 'gnus-part))
3833         (point (point))
3834         buffer-read-only)
3835     (forward-line 1)
3836     (prog1
3837         (let ((window (selected-window))
3838               (mail-parse-charset gnus-newsgroup-charset)
3839               (mail-parse-ignored-charsets 
3840                (save-excursion (set-buffer gnus-summary-buffer)
3841                                gnus-newsgroup-ignored-charsets)))
3842           (save-excursion
3843             (unwind-protect
3844                 (let ((win (get-buffer-window (current-buffer) t))
3845                       (beg (point)))
3846                   (when win
3847                     (select-window win))
3848                   (goto-char point)
3849                   (forward-line)
3850                   (if (mm-handle-displayed-p handle)
3851                       ;; This will remove the part.
3852                       (mm-display-part handle)
3853                     (save-restriction
3854                       (narrow-to-region (point) (1+ (point)))
3855                       (mm-display-part handle)
3856                       ;; We narrow to the part itself and
3857                       ;; then call the treatment functions.
3858                       (goto-char (point-min))
3859                       (forward-line 1)
3860                       (narrow-to-region (point) (point-max))
3861                       (gnus-treat-article
3862                        nil id
3863                        (gnus-article-mime-total-parts)
3864                        (mm-handle-media-type handle)))))
3865               (select-window window))))
3866       (goto-char point)
3867       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
3868       (gnus-insert-mime-button
3869        handle id (list (mm-handle-displayed-p handle)))
3870       (goto-char point))))
3871
3872 (defun gnus-article-goto-part (n)
3873   "Go to MIME part N."
3874   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
3875     (when point
3876       (goto-char point))))
3877
3878 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
3879   (let ((gnus-tmp-name
3880          (or (mail-content-type-get (mm-handle-type handle)
3881                                     'name)
3882              (mail-content-type-get (mm-handle-disposition handle)
3883                                     'filename)
3884              (mail-content-type-get (mm-handle-type handle)
3885                                     'url)
3886              ""))
3887         (gnus-tmp-type (mm-handle-media-type handle))
3888         (gnus-tmp-description
3889          (mail-decode-encoded-word-string (or (mm-handle-description handle)
3890                                               "")))
3891         (gnus-tmp-dots
3892          (if (if displayed (car displayed)
3893                (mm-handle-displayed-p handle))
3894              "" "..."))
3895         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
3896                            (buffer-size)))
3897         gnus-tmp-type-long b e)
3898     (when (string-match ".*/" gnus-tmp-name)
3899       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
3900     (setq gnus-tmp-type-long (concat gnus-tmp-type
3901                                      (and (not (equal gnus-tmp-name ""))
3902                                           (concat "; " gnus-tmp-name))))
3903     (or (equal gnus-tmp-description "")
3904         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
3905     (unless (bolp)
3906       (insert "\n"))
3907     (setq b (point))
3908     (gnus-eval-format
3909      gnus-mime-button-line-format gnus-mime-button-line-format-alist
3910      `(keymap ,gnus-mime-button-map
3911               ,@(if (>= (string-to-number emacs-version) 21)
3912                     nil 
3913                   (list 'local-map gnus-mime-button-map))
3914               gnus-callback gnus-mm-display-part
3915               gnus-part ,gnus-tmp-id
3916               article-type annotation
3917               gnus-data ,handle))
3918     (setq e (point))
3919     (widget-convert-button
3920      'link b e
3921      :mime-handle handle
3922      :action 'gnus-widget-press-button
3923      :button-keymap gnus-mime-button-map
3924      :help-echo
3925      (lambda (widget/window &optional overlay pos)
3926        ;; Needed to properly clear the message due to a bug in
3927        ;; wid-edit (XEmacs only).
3928        (if (boundp 'help-echo-owns-message)
3929            (setq help-echo-owns-message t))
3930        (format
3931         "%S: %s the MIME part; %S: more options"
3932         (aref gnus-mouse-2 0)
3933         ;; XEmacs will get a single widget arg; Emacs 21 will get
3934         ;; window, overlay, position.
3935         (if (mm-handle-displayed-p
3936              (if overlay
3937                  (with-current-buffer (gnus-overlay-buffer overlay)
3938                    (widget-get (widget-at (gnus-overlay-start overlay))
3939                                :mime-handle))
3940                (widget-get widget/window :mime-handle)))
3941             "hide" "show")
3942         (aref gnus-down-mouse-3 0))))))
3943
3944 (defun gnus-widget-press-button (elems el)
3945   (goto-char (widget-get elems :from))
3946   (gnus-article-press-button))
3947
3948 (defvar gnus-displaying-mime nil)
3949
3950 (defun gnus-display-mime (&optional ihandles)
3951   "Display the MIME parts."
3952   (save-excursion
3953     (save-selected-window
3954       (let ((window (get-buffer-window gnus-article-buffer))
3955             (point (point)))
3956         (when window
3957           (select-window window)
3958           ;; We have to do this since selecting the window
3959           ;; may change the point.  So we set the window point.
3960           (set-window-point window point)))
3961       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3962              buffer-read-only handle name type b e display)
3963         (when (and (not ihandles)
3964                    (not gnus-displaying-mime))
3965           ;; Top-level call; we clean up.
3966           (when gnus-article-mime-handles
3967             (mm-destroy-parts gnus-article-mime-handles)
3968             (setq gnus-article-mime-handle-alist nil));; A trick.
3969           (setq gnus-article-mime-handles handles)
3970           ;; We allow users to glean info from the handles.
3971           (when gnus-article-mime-part-function
3972             (gnus-mime-part-function handles)))
3973         (if (and handles
3974                  (or (not (stringp (car handles)))
3975                      (cdr handles)))
3976             (progn
3977               (when (and (not ihandles)
3978                          (not gnus-displaying-mime))
3979                 ;; Clean up for mime parts.
3980                 (article-goto-body)
3981                 (delete-region (point) (point-max)))
3982               (let ((gnus-displaying-mime t))
3983                 (gnus-mime-display-part handles)))
3984           (save-restriction
3985             (article-goto-body)
3986             (narrow-to-region (point) (point-max))
3987             (gnus-treat-article nil 1 1)
3988             (widen)))
3989         (unless ihandles
3990           ;; Highlight the headers.
3991           (save-excursion
3992             (save-restriction
3993               (article-goto-body)
3994               (narrow-to-region (point-min) (point))
3995               (gnus-treat-article 'head))))))))
3996
3997 (defvar gnus-mime-display-multipart-as-mixed nil)
3998
3999 (defun gnus-mime-display-part (handle)
4000   (cond
4001    ;; Single part.
4002    ((not (stringp (car handle)))
4003     (gnus-mime-display-single handle))
4004    ;; User-defined multipart
4005    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4006     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4007              handle))
4008    ;; multipart/alternative
4009    ((and (equal (car handle) "multipart/alternative")
4010          (not gnus-mime-display-multipart-as-mixed))
4011     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4012       (push (cons id handle) gnus-article-mime-handle-alist)
4013       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4014    ;; multipart/related
4015    ((and (equal (car handle) "multipart/related")
4016          (not gnus-mime-display-multipart-as-mixed))
4017     ;;;!!!We should find the start part, but we just default
4018     ;;;!!!to the first part.
4019     ;;(gnus-mime-display-part (cadr handle))
4020     ;;;!!! Most multipart/related is an HTML message plus images.
4021     ;;;!!! Unfortunately we are unable to let W3 display those 
4022     ;;;!!! included images, so we just display it as a mixed multipart.
4023     (gnus-mime-display-mixed (cdr handle)))
4024    ((equal (car handle) "multipart/signed")
4025     (or (memq 'signed gnus-article-wash-types)
4026         (push 'signed gnus-article-wash-types))
4027     (gnus-mime-display-security handle))
4028    ((equal (car handle) "multipart/encrypted")
4029     (or (memq 'encrypted gnus-article-wash-types)
4030         (push 'encrypted gnus-article-wash-types))
4031     (gnus-mime-display-security handle))
4032    ;; Other multiparts are handled like multipart/mixed.
4033    (t
4034     (gnus-mime-display-mixed (cdr handle)))))
4035
4036 (defun gnus-mime-part-function (handles)
4037   (if (stringp (car handles))
4038       (mapcar 'gnus-mime-part-function (cdr handles))
4039     (funcall gnus-article-mime-part-function handles)))
4040
4041 (defun gnus-mime-display-mixed (handles)
4042   (mapcar 'gnus-mime-display-part handles))
4043
4044 (defun gnus-mime-display-single (handle)
4045   (let ((type (mm-handle-media-type handle))
4046         (ignored gnus-ignored-mime-types)
4047         (not-attachment t)
4048         (move nil)
4049         display text)
4050     (catch 'ignored
4051       (progn
4052         (while ignored
4053           (when (string-match (pop ignored) type)
4054             (throw 'ignored nil)))
4055         (if (and (setq not-attachment
4056                        (and (not (mm-inline-override-p handle))
4057                             (or (not (mm-handle-disposition handle))
4058                                 (equal (car (mm-handle-disposition handle))
4059                                        "inline")
4060                                 (mm-attachment-override-p handle))))
4061                  (mm-automatic-display-p handle)
4062                  (or (mm-inlined-p handle)
4063                      (mm-automatic-external-display-p type)))
4064             (setq display t)
4065           (when (equal (mm-handle-media-supertype handle) "text")
4066             (setq text t)))
4067         (let ((id (1+ (length gnus-article-mime-handle-alist)))
4068               beg)
4069           (push (cons id handle) gnus-article-mime-handle-alist)
4070           (when (or (not display)
4071                     (not (gnus-unbuttonized-mime-type-p type)))
4072             ;(gnus-article-insert-newline)
4073             (gnus-insert-mime-button
4074              handle id (list (or display (and not-attachment text))))
4075             (gnus-article-insert-newline) 
4076             ;(gnus-article-insert-newline) 
4077             ;; Remember modify the number of forward lines.
4078             (setq move t))
4079           (setq beg (point))
4080           (cond
4081            (display
4082             (when move
4083               (forward-line -1)
4084               (setq beg (point)))
4085             (let ((mail-parse-charset gnus-newsgroup-charset)
4086                   (mail-parse-ignored-charsets 
4087                    (save-excursion (condition-case ()
4088                                        (set-buffer gnus-summary-buffer)
4089                                      (error))
4090                                    gnus-newsgroup-ignored-charsets)))
4091               (mm-display-part handle t))
4092             (goto-char (point-max)))
4093            ((and text not-attachment)
4094             (when move
4095               (forward-line -1)
4096               (setq beg (point)))
4097             (gnus-article-insert-newline)
4098             (mm-insert-inline handle (mm-get-part handle))
4099             (goto-char (point-max))))
4100           ;; Do highlighting.
4101           (save-excursion
4102             (save-restriction
4103               (narrow-to-region beg (point))
4104               (gnus-treat-article
4105                nil id 
4106                (gnus-article-mime-total-parts)
4107                (mm-handle-media-type handle)))))))))
4108
4109 (defun gnus-unbuttonized-mime-type-p (type)
4110   "Say whether TYPE is to be unbuttonized."
4111   (unless gnus-inhibit-mime-unbuttonizing
4112     (catch 'found
4113       (let ((types gnus-unbuttonized-mime-types))
4114         (while types
4115           (when (string-match (pop types) type)
4116             (throw 'found t)))))))
4117
4118 (defun gnus-article-insert-newline ()
4119   "Insert a newline, but mark it as undeletable."
4120   (gnus-put-text-property
4121    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
4122
4123 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
4124   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
4125          (ihandles handles)
4126          (point (point))
4127          handle buffer-read-only from props begend not-pref)
4128     (save-window-excursion
4129       (save-restriction
4130         (when ibegend
4131           (narrow-to-region (car ibegend)
4132                             (or (cdr ibegend)
4133                                 (progn
4134                                   (goto-char (car ibegend))
4135                                   (forward-line 2)
4136                                   (point))))
4137           (delete-region (point-min) (point-max))
4138           (mm-remove-parts handles))
4139         (setq begend (list (point-marker)))
4140         ;; Do the toggle.
4141         (unless (setq not-pref (cadr (member preferred ihandles)))
4142           (setq not-pref (car ihandles)))
4143         (when (or ibegend
4144                   (not preferred)
4145                   (not (gnus-unbuttonized-mime-type-p
4146                         "multipart/alternative")))
4147           (gnus-add-text-properties
4148            (setq from (point))
4149            (progn
4150              (insert (format "%d.  " id))
4151              (point))
4152            `(gnus-callback
4153              (lambda (handles)
4154                (unless ,(not ibegend)
4155                  (setq gnus-article-mime-handle-alist
4156                        ',gnus-article-mime-handle-alist))
4157                (gnus-mime-display-alternative
4158                 ',ihandles ',not-pref ',begend ,id))
4159              ,@(if (>= (string-to-number emacs-version) 21)
4160                    nil ;; XEmacs doesn't care
4161                  (list 'local-map gnus-mime-button-map))
4162              ,gnus-mouse-face-prop ,gnus-article-mouse-face
4163              face ,gnus-article-button-face
4164              keymap ,gnus-mime-button-map
4165              gnus-part ,id
4166              gnus-data ,handle))
4167           (widget-convert-button 'link from (point)
4168                                  :action 'gnus-widget-press-button
4169                                  :button-keymap gnus-widget-button-keymap)
4170           ;; Do the handles
4171           (while (setq handle (pop handles))
4172             (gnus-add-text-properties
4173              (setq from (point))
4174              (progn
4175                (insert (format "(%c) %-18s"
4176                                (if (equal handle preferred) ?* ? )
4177                                (mm-handle-media-type handle)))
4178                (point))
4179              `(gnus-callback
4180                (lambda (handles)
4181                  (unless ,(not ibegend)
4182                    (setq gnus-article-mime-handle-alist
4183                          ',gnus-article-mime-handle-alist))
4184                  (gnus-mime-display-alternative
4185                   ',ihandles ',handle ',begend ,id))
4186                ,@(if (>= (string-to-number emacs-version) 21)
4187                      nil ;; XEmacs doesn't care
4188                    (list 'local-map gnus-mime-button-map))
4189                ,gnus-mouse-face-prop ,gnus-article-mouse-face
4190                face ,gnus-article-button-face
4191                keymap ,gnus-mime-button-map
4192                gnus-part ,id
4193                gnus-data ,handle))
4194             (widget-convert-button 'link from (point)
4195                                    :action 'gnus-widget-press-button
4196                                    :button-keymap gnus-widget-button-keymap)
4197             (insert "  "))
4198           (insert "\n\n"))
4199         (when preferred
4200           (if (stringp (car preferred))
4201               (gnus-display-mime preferred)
4202             (let ((mail-parse-charset gnus-newsgroup-charset)
4203                   (mail-parse-ignored-charsets 
4204                    (save-excursion (set-buffer gnus-summary-buffer)
4205                                    gnus-newsgroup-ignored-charsets)))
4206               (mm-display-part preferred)
4207               ;; Do highlighting.
4208               (save-excursion
4209                 (save-restriction
4210                   (narrow-to-region (car begend) (point-max))
4211                   (gnus-treat-article
4212                    nil (length gnus-article-mime-handle-alist)
4213                    (gnus-article-mime-total-parts)
4214                    (mm-handle-media-type handle))))))
4215           (goto-char (point-max))
4216           (setcdr begend (point-marker)))))
4217     (when ibegend
4218       (goto-char point))))
4219
4220 (defun gnus-article-wash-status ()
4221   "Return a string which display status of article washing."
4222   (save-excursion
4223     (set-buffer gnus-article-buffer)
4224     (let ((cite (memq 'cite gnus-article-wash-types))
4225           (headers (memq 'headers gnus-article-wash-types))
4226           (boring (memq 'boring-headers gnus-article-wash-types))
4227           (pgp (memq 'pgp gnus-article-wash-types))
4228           (pem (memq 'pem gnus-article-wash-types))
4229           (signed (memq 'signed gnus-article-wash-types))
4230           (encrypted (memq 'encrypted gnus-article-wash-types))
4231           (signature (memq 'signature gnus-article-wash-types))
4232           (overstrike (memq 'overstrike gnus-article-wash-types))
4233           (emphasis (memq 'emphasis gnus-article-wash-types)))
4234       (format "%c%c%c%c%c%c%c"
4235               (if cite ?c ? )
4236               (if (or headers boring) ?h ? )
4237               (if (or pgp pem signed encrypted) ?p ? )
4238               (if signature ?s ? )
4239               (if overstrike ?o ? )
4240               (if gnus-show-mime ?m ? )
4241               (if emphasis ?e ? )))))
4242
4243 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
4244
4245 (defun gnus-article-maybe-hide-headers ()
4246   "Hide unwanted headers if `gnus-have-all-headers' is nil.
4247 Provided for backwards compatibility."
4248   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
4249                  (not (save-excursion (set-buffer gnus-summary-buffer)
4250                                       gnus-have-all-headers)))
4251              (not gnus-inhibit-hiding))
4252     (gnus-article-hide-headers)))
4253
4254 ;;; Article savers.
4255
4256 (defun gnus-output-to-file (file-name)
4257   "Append the current article to a file named FILE-NAME."
4258   (let ((artbuf (current-buffer)))
4259     (with-temp-buffer
4260       (insert-buffer-substring artbuf)
4261       ;; Append newline at end of the buffer as separator, and then
4262       ;; save it to file.
4263       (goto-char (point-max))
4264       (insert "\n")
4265       (write-region-as-binary (point-min) (point-max) file-name 'append)
4266       t)))
4267
4268 (defun gnus-narrow-to-page (&optional arg)
4269   "Narrow the article buffer to a page.
4270 If given a numerical ARG, move forward ARG pages."
4271   (interactive "P")
4272   (setq arg (if arg (prefix-numeric-value arg) 0))
4273   (save-excursion
4274     (set-buffer gnus-article-buffer)
4275     (goto-char (point-min))
4276     (widen)
4277     ;; Remove any old next/prev buttons.
4278     (when (gnus-visual-p 'page-marker)
4279       (let ((buffer-read-only nil))
4280         (gnus-remove-text-with-property 'gnus-prev)
4281         (gnus-remove-text-with-property 'gnus-next)))
4282     (when
4283         (cond ((< arg 0)
4284                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
4285               ((> arg 0)
4286                (re-search-forward page-delimiter nil 'move arg)))
4287       (goto-char (match-end 0)))
4288     (narrow-to-region
4289      (point)
4290      (if (re-search-forward page-delimiter nil 'move)
4291          (match-beginning 0)
4292        (point)))
4293     (when (and (gnus-visual-p 'page-marker)
4294                (not (= (point-min) 1)))
4295       (save-excursion
4296         (goto-char (point-min))
4297         (gnus-insert-prev-page-button)))
4298     (when (and (gnus-visual-p 'page-marker)
4299                (< (+ (point-max) 2) (buffer-size)))
4300       (save-excursion
4301         (goto-char (point-max))
4302         (gnus-insert-next-page-button)))))
4303
4304 ;; Article mode commands
4305
4306 (defun gnus-article-goto-next-page ()
4307   "Show the next page of the article."
4308   (interactive)
4309   (when (gnus-article-next-page)
4310     (goto-char (point-min))
4311     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
4312
4313 (defun gnus-article-goto-prev-page ()
4314   "Show the next page of the article."
4315   (interactive)
4316   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
4317     (gnus-article-prev-page nil)))
4318
4319 (defun gnus-article-next-page (&optional lines)
4320   "Show the next page of the current article.
4321 If end of article, return non-nil.  Otherwise return nil.
4322 Argument LINES specifies lines to be scrolled up."
4323   (interactive "p")
4324   (let ((start (window-start))
4325         end-of-buffer end-of-page)
4326     (save-excursion
4327       (move-to-window-line -1)
4328       (if (<= (point) start)
4329           (progn
4330             (forward-line 2)
4331             (setq start (point)))
4332         (forward-line 1)
4333         (setq start nil))
4334       (unless (or (cond ((eq (1+ (buffer-size)) (point))
4335                          (and (pos-visible-in-window-p)
4336                               (setq end-of-buffer t)))
4337                         ((eobp)
4338                          (setq end-of-page t)))
4339                   (not lines))
4340         (move-to-window-line lines)
4341         (unless (search-backward "\n\n" nil t)
4342           (setq start (point)))))
4343     (cond (end-of-buffer t)
4344           (end-of-page
4345            (gnus-narrow-to-page 1)
4346            nil)
4347           (t
4348            (if start
4349                (set-window-start (selected-window) start)
4350              (let (window-pixel-scroll-increment)
4351                (scroll-up lines)))
4352            nil))))
4353
4354 (defun gnus-article-prev-page (&optional lines)
4355   "Show previous page of current article.
4356 Argument LINES specifies lines to be scrolled down."
4357   (interactive "p")
4358   (let (beginning-of-buffer beginning-of-page)
4359     (save-excursion
4360       (move-to-window-line 0)
4361       (cond ((eq 1 (point))
4362              (setq beginning-of-buffer t))
4363             ((bobp)
4364              (setq beginning-of-page t))))
4365     (cond (beginning-of-buffer)
4366           (beginning-of-page
4367            (gnus-narrow-to-page -1))
4368           (t
4369            (condition-case nil
4370                (let (window-pixel-scroll-increment)
4371                  (scroll-down lines))
4372              (beginning-of-buffer
4373               (goto-char (point-min))))))))
4374
4375 (defun gnus-article-refer-article ()
4376   "Read article specified by message-id around point."
4377   (interactive)
4378   (let ((point (point)))
4379     (search-forward ">" nil t)          ;Move point to end of "<....>".
4380     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
4381         (let ((message-id (match-string 1)))
4382           (goto-char point)
4383           (set-buffer gnus-summary-buffer)
4384           (gnus-summary-refer-article message-id))
4385       (goto-char (point))
4386       (error "No references around point"))))
4387
4388 (defun gnus-article-show-summary ()
4389   "Reconfigure windows to show summary buffer."
4390   (interactive)
4391   (if (not (gnus-buffer-live-p gnus-summary-buffer))
4392       (error "There is no summary buffer for this article buffer")
4393     (gnus-article-set-globals)
4394     (gnus-configure-windows 'article)
4395     (gnus-summary-goto-subject gnus-current-article)
4396     (gnus-summary-position-point)))
4397
4398 (defun gnus-article-describe-briefly ()
4399   "Describe article mode commands briefly."
4400   (interactive)
4401   (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")))
4402
4403 (defun gnus-article-summary-command ()
4404   "Execute the last keystroke in the summary buffer."
4405   (interactive)
4406   (let ((obuf (current-buffer))
4407         (owin (current-window-configuration))
4408         func)
4409     (switch-to-buffer gnus-article-current-summary 'norecord)
4410     (setq func (lookup-key (current-local-map) (this-command-keys)))
4411     (call-interactively func)
4412     (set-buffer obuf)
4413     (set-window-configuration owin)
4414     (set-window-point (get-buffer-window (current-buffer)) (point))))
4415
4416 (defun gnus-article-summary-command-nosave ()
4417   "Execute the last keystroke in the summary buffer."
4418   (interactive)
4419   (let (func)
4420     (pop-to-buffer gnus-article-current-summary 'norecord)
4421     (setq func (lookup-key (current-local-map) (this-command-keys)))
4422     (call-interactively func)))
4423
4424 (defun gnus-article-check-buffer ()
4425   "Beep if not in an article buffer."
4426   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
4427     (error "Command invoked outside of a Gnus article buffer")))
4428
4429 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
4430   "Read a summary buffer key sequence and execute it from the article buffer."
4431   (interactive "P")
4432   (gnus-article-check-buffer)
4433   (let ((nosaves
4434          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
4435            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
4436            "=" "^" "\M-^" "|"))
4437         (nosave-but-article
4438          '("A\r"))
4439         (nosave-in-article
4440          '("\C-d"))
4441         (up-to-top
4442          '("n" "Gn" "p" "Gp"))
4443         keys new-sum-point)
4444     (save-excursion
4445       (set-buffer gnus-article-current-summary)
4446       (let (gnus-pick-mode)
4447         (push (or key last-command-event) unread-command-events)
4448         (setq keys (static-if (featurep 'xemacs)
4449                        (events-to-keys (read-key-sequence nil))
4450                      (read-key-sequence nil)))))
4451     (message "")
4452
4453     (if (or (member keys nosaves)
4454             (member keys nosave-but-article)
4455             (member keys nosave-in-article))
4456         (let (func)
4457           (save-window-excursion
4458             (pop-to-buffer gnus-article-current-summary 'norecord)
4459             ;; We disable the pick minor mode commands.
4460             (let (gnus-pick-mode)
4461               (setq func (lookup-key (current-local-map) keys))))
4462           (if (or (not func)
4463                   (numberp func))
4464               (ding)
4465             (unless (member keys nosave-in-article)
4466               (set-buffer gnus-article-current-summary))
4467             (call-interactively func)
4468             (setq new-sum-point (point)))
4469           (when (member keys nosave-but-article)
4470             (pop-to-buffer gnus-article-buffer 'norecord)))
4471       ;; These commands should restore window configuration.
4472       (let ((obuf (current-buffer))
4473             (owin (current-window-configuration))
4474             (opoint (point))
4475             (summary gnus-article-current-summary)
4476             func in-buffer selected)
4477         (if not-restore-window
4478             (pop-to-buffer summary 'norecord)
4479           (switch-to-buffer summary 'norecord))
4480         (setq in-buffer (current-buffer))
4481         ;; We disable the pick minor mode commands.
4482         (if (and (setq func (let (gnus-pick-mode)
4483                               (lookup-key (current-local-map) keys)))
4484                  (functionp func))
4485             (progn
4486               (call-interactively func)
4487               (setq new-sum-point (point))
4488               (when (eq in-buffer (current-buffer))
4489                 (setq selected (gnus-summary-select-article))
4490                 (set-buffer obuf)
4491                 (unless not-restore-window
4492                   (set-window-configuration owin))
4493                 (when (eq selected 'old)
4494                   (article-goto-body)
4495                   (set-window-start (get-buffer-window (current-buffer))
4496                                     1)
4497                   (set-window-point (get-buffer-window (current-buffer))
4498                                     (point)))
4499                 (let ((win (get-buffer-window gnus-article-current-summary)))
4500                   (when win
4501                     (set-window-point win new-sum-point))))    )
4502           (switch-to-buffer gnus-article-buffer)
4503           (ding))))))
4504
4505 (defun gnus-article-describe-key (key)
4506   "Display documentation of the function invoked by KEY.  KEY is a string."
4507   (interactive "kDescribe key: ")
4508   (gnus-article-check-buffer)
4509   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4510       (save-excursion
4511         (set-buffer gnus-article-current-summary)
4512         (let (gnus-pick-mode)
4513           (push (elt key 0) unread-command-events)
4514           (setq key (if (featurep 'xemacs)
4515                         (events-to-keys (read-key-sequence "Describe key: "))
4516                       (read-key-sequence "Describe key: "))))
4517         (describe-key key))
4518     (describe-key key)))
4519
4520 (defun gnus-article-describe-key-briefly (key &optional insert)
4521   "Display documentation of the function invoked by KEY.  KEY is a string."
4522   (interactive "kDescribe key: \nP")
4523   (gnus-article-check-buffer)
4524   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4525       (save-excursion
4526         (set-buffer gnus-article-current-summary)
4527         (let (gnus-pick-mode)
4528           (push (elt key 0) unread-command-events)
4529           (setq key (if (featurep 'xemacs)
4530                         (events-to-keys (read-key-sequence "Describe key: "))
4531                       (read-key-sequence "Describe key: "))))
4532         (describe-key-briefly key insert))
4533     (describe-key-briefly key insert)))
4534
4535 (defun gnus-article-hide (&optional arg force)
4536   "Hide all the gruft in the current article.
4537 This means that PGP stuff, signatures, cited text and (some)
4538 headers will be hidden.
4539 If given a prefix, show the hidden text instead."
4540   (interactive (append (gnus-article-hidden-arg) (list 'force)))
4541   (gnus-article-hide-headers arg)
4542   (gnus-article-hide-list-identifiers arg)
4543   (gnus-article-hide-pgp arg)
4544   (gnus-article-hide-citation-maybe arg force)
4545   (gnus-article-hide-signature arg))
4546
4547 (defun gnus-article-maybe-highlight ()
4548   "Do some article highlighting if article highlighting is requested."
4549   (when (gnus-visual-p 'article-highlight 'highlight)
4550     (gnus-article-highlight-some)))
4551
4552 (defun gnus-check-group-server ()
4553   ;; Make sure the connection to the server is alive.
4554   (unless (gnus-server-opened
4555            (gnus-find-method-for-group gnus-newsgroup-name))
4556     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
4557     (gnus-request-group gnus-newsgroup-name t)))
4558
4559 (defun gnus-request-article-this-buffer (article group)
4560   "Get an article and insert it into this buffer."
4561   (let (do-update-line sparse-header)
4562     (prog1
4563         (save-excursion
4564           (erase-buffer)
4565           (gnus-kill-all-overlays)
4566           (setq group (or group gnus-newsgroup-name))
4567
4568           ;; Using `gnus-request-article' directly will insert the article into
4569           ;; `nntp-server-buffer' - so we'll save some time by not having to
4570           ;; copy it from the server buffer into the article buffer.
4571
4572           ;; We only request an article by message-id when we do not have the
4573           ;; headers for it, so we'll have to get those.
4574           (when (stringp article)
4575             (gnus-read-header article))
4576
4577           ;; If the article number is negative, that means that this article
4578           ;; doesn't belong in this newsgroup (possibly), so we find its
4579           ;; message-id and request it by id instead of number.
4580           (when (and (numberp article)
4581                      gnus-summary-buffer
4582                      (get-buffer gnus-summary-buffer)
4583                      (gnus-buffer-exists-p gnus-summary-buffer))
4584             (save-excursion
4585               (set-buffer gnus-summary-buffer)
4586               (let ((header (gnus-summary-article-header article)))
4587                 (when (< article 0)
4588                   (cond
4589                    ((memq article gnus-newsgroup-sparse)
4590                     ;; This is a sparse gap article.
4591                     (setq do-update-line article)
4592                     (setq article (mail-header-id header))
4593                     (setq sparse-header (gnus-read-header article))
4594                     (setq gnus-newsgroup-sparse
4595                           (delq article gnus-newsgroup-sparse)))
4596                    ((vectorp header)
4597                     ;; It's a real article.
4598                     (setq article (mail-header-id header)))
4599                    (t
4600                     ;; It is an extracted pseudo-article.
4601                     (setq article 'pseudo)
4602                     (gnus-request-pseudo-article header))))
4603
4604                 (let ((method (gnus-find-method-for-group
4605                                gnus-newsgroup-name)))
4606                   (when (and (eq (car method) 'nneething)
4607                              (vectorp header))
4608                     (let ((dir (expand-file-name
4609                                 (mail-header-subject header)
4610                                 (file-name-as-directory
4611                                  (or (cadr (assq 'nneething-address method))
4612                                      (nth 1 method))))))
4613                       (when (file-directory-p dir)
4614                         (setq article 'nneething)
4615                         (gnus-group-enter-directory dir))))))))
4616
4617           (cond
4618            ;; Refuse to select canceled articles.
4619            ((and (numberp article)
4620                  gnus-summary-buffer
4621                  (get-buffer gnus-summary-buffer)
4622                  (gnus-buffer-exists-p gnus-summary-buffer)
4623                  (eq (cdr (save-excursion
4624                             (set-buffer gnus-summary-buffer)
4625                             (assq article gnus-newsgroup-reads)))
4626                      gnus-canceled-mark))
4627             nil)
4628            ;; Check the backlog.
4629            ((and gnus-keep-backlog
4630                  (gnus-backlog-request-article group article (current-buffer)))
4631             'article)
4632            ;; Check asynchronous pre-fetch.
4633            ((gnus-async-request-fetched-article group article (current-buffer))
4634             (gnus-async-prefetch-next group article gnus-summary-buffer)
4635             (when (and (numberp article) gnus-keep-backlog)
4636               (gnus-backlog-enter-article group article (current-buffer)))
4637             'article)
4638            ;; Check the cache.
4639            ((and gnus-use-cache
4640                  (numberp article)
4641                  (gnus-cache-request-article article group))
4642             'article)
4643            ;; Get the article and put into the article buffer.
4644            ((or (stringp article)
4645                 (numberp article))
4646             (let ((gnus-override-method gnus-override-method)
4647                   (methods (and (stringp article) 
4648                                 gnus-refer-article-method))
4649                   result
4650                   (buffer-read-only nil))
4651               (if (or (not (listp methods))
4652                       (and (symbolp (car methods))
4653                            (assq (car methods) nnoo-definition-alist)))
4654                   (setq methods (list methods)))
4655               (when (and (null gnus-override-method)
4656                          methods)
4657                 (setq gnus-override-method (pop methods)))
4658               (while (not result)
4659                 (when (eq gnus-override-method 'current)
4660                   (setq gnus-override-method gnus-current-select-method))
4661                 (erase-buffer)
4662                 (gnus-kill-all-overlays)
4663                 (let ((gnus-newsgroup-name group))
4664                   (gnus-check-group-server))
4665                 (when (gnus-request-article article group (current-buffer))
4666                   (when (numberp article)
4667                     (gnus-async-prefetch-next group article 
4668                                               gnus-summary-buffer)
4669                     (when gnus-keep-backlog
4670                       (gnus-backlog-enter-article
4671                        group article (current-buffer))))
4672                   (setq result 'article))
4673                 (if (not result)
4674                     (if methods
4675                         (setq gnus-override-method (pop methods))
4676                       (setq result 'done))))
4677               (and (eq result 'article) 'article)))
4678            ;; It was a pseudo.
4679            (t article)))
4680
4681       ;; Associate this article with the current summary buffer.
4682       (setq gnus-article-current-summary gnus-summary-buffer)
4683
4684       ;; Take the article from the original article buffer
4685       ;; and place it in the buffer it's supposed to be in.
4686       (when (and (get-buffer gnus-article-buffer)
4687                  (equal (buffer-name (current-buffer))
4688                         (buffer-name (get-buffer gnus-article-buffer))))
4689         (save-excursion
4690           (if (get-buffer gnus-original-article-buffer)
4691               (set-buffer gnus-original-article-buffer)
4692             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4693             (set-buffer-multibyte nil)
4694             (buffer-disable-undo)
4695             (setq major-mode 'gnus-original-article-mode)
4696             (setq buffer-read-only t))
4697           (let (buffer-read-only)
4698             (erase-buffer)
4699             (insert-buffer-substring gnus-article-buffer))
4700           (setq gnus-original-article (cons group article)))
4701
4702         ;; Decode charsets.
4703         (run-hooks 'gnus-article-decode-hook)
4704         ;; Mark article as decoded or not.
4705         (setq gnus-article-decoded-p gnus-article-decode-hook))
4706
4707       ;; Update sparse articles.
4708       (when (and do-update-line
4709                  (or (numberp article)
4710                      (stringp article)))
4711         (let ((buf (current-buffer)))
4712           (set-buffer gnus-summary-buffer)
4713           (gnus-summary-update-article do-update-line sparse-header)
4714           (gnus-summary-goto-subject do-update-line nil t)
4715           (set-window-point (get-buffer-window (current-buffer) t)
4716                             (point))
4717           (set-buffer buf))))))
4718
4719 ;;;
4720 ;;; Article editing
4721 ;;;
4722
4723 (defcustom gnus-article-edit-mode-hook nil
4724   "Hook run in article edit mode buffers."
4725   :group 'gnus-article-various
4726   :type 'hook)
4727
4728 (defcustom gnus-article-edit-article-setup-function
4729   'gnus-article-mime-edit-article-setup
4730   "Function called to setup an editing article buffer."
4731   :group 'gnus-article-various
4732   :type 'function)
4733
4734 (defvar gnus-article-edit-done-function nil)
4735
4736 (defvar gnus-article-edit-mode-map nil)
4737
4738 ;; Should we be using derived.el for this?
4739 (unless gnus-article-edit-mode-map
4740   (setq gnus-article-edit-mode-map (make-sparse-keymap))
4741   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
4742
4743   (gnus-define-keys gnus-article-edit-mode-map
4744     "\C-c\C-c" gnus-article-edit-done
4745     "\C-c\C-k" gnus-article-edit-exit)
4746
4747   (gnus-define-keys (gnus-article-edit-wash-map
4748                      "\C-c\C-w" gnus-article-edit-mode-map)
4749     "f" gnus-article-edit-full-stops))
4750
4751 (defun gnus-article-edit-mode ()
4752   "Major mode for editing articles.
4753 This is an extended text-mode.
4754
4755 \\{gnus-article-edit-mode-map}"
4756   (interactive)
4757   (setq major-mode 'gnus-article-edit-mode)
4758   (setq mode-name "Article Edit")
4759   (use-local-map gnus-article-edit-mode-map)
4760   (make-local-variable 'gnus-article-edit-done-function)
4761   (make-local-variable 'gnus-prev-winconf)
4762   (setq buffer-read-only nil)
4763   (buffer-enable-undo)
4764   (widen)
4765   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
4766
4767 (defun gnus-article-edit (&optional force)
4768   "Edit the current article.
4769 This will have permanent effect only in mail groups.
4770 If FORCE is non-nil, allow editing of articles even in read-only
4771 groups."
4772   (interactive "P")
4773   (when (and (not force)
4774              (gnus-group-read-only-p))
4775     (error "The current newsgroup does not support article editing"))
4776   (gnus-article-date-original)
4777   (gnus-article-edit-article
4778    'ignore
4779    `(lambda (no-highlight)
4780       'ignore
4781       (gnus-summary-edit-article-done
4782        ,(or (mail-header-references gnus-current-headers) "")
4783        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
4784
4785 (defun gnus-article-edit-article (start-func exit-func)
4786   "Start editing the contents of the current article buffer."
4787   (let ((winconf (current-window-configuration)))
4788     (set-buffer gnus-article-buffer)
4789     (gnus-article-edit-mode)
4790     (funcall start-func)
4791     (gnus-configure-windows 'edit-article)
4792     (setq gnus-article-edit-done-function exit-func)
4793     (setq gnus-prev-winconf winconf)
4794     (when gnus-article-edit-article-setup-function
4795       (funcall gnus-article-edit-article-setup-function))
4796     (gnus-message 6 "C-c C-c to end edits")))
4797
4798 (defun gnus-article-edit-done (&optional arg)
4799   "Update the article edits and exit."
4800   (interactive "P")
4801   (let ((func gnus-article-edit-done-function)
4802         (buf (current-buffer))
4803         (start (window-start)))
4804     (remove-hook 'gnus-article-mode-hook
4805                  'gnus-article-mime-edit-article-unwind)
4806     (gnus-article-edit-exit)
4807     (save-excursion
4808       (set-buffer buf)
4809       (let ((buffer-read-only nil))
4810         (funcall func arg))
4811       ;; The cache and backlog have to be flushed somewhat.
4812       (when gnus-keep-backlog
4813         (gnus-backlog-remove-article
4814          (car gnus-article-current) (cdr gnus-article-current)))
4815       ;; Flush original article as well.
4816       (save-excursion
4817         (when (get-buffer gnus-original-article-buffer)
4818           (set-buffer gnus-original-article-buffer)
4819           (setq gnus-original-article nil)))
4820       (when gnus-use-cache
4821         (gnus-cache-update-article
4822          (car gnus-article-current) (cdr gnus-article-current))))
4823     (set-buffer buf)
4824     (set-window-start (get-buffer-window buf) start)
4825     (set-window-point (get-buffer-window buf) (point))))
4826
4827 (defun gnus-article-edit-exit ()
4828   "Exit the article editing without updating."
4829   (interactive)
4830   ;; We remove all text props from the article buffer.
4831   (let ((buf (buffer-substring-no-properties (point-min) (point-max)))
4832         (curbuf (current-buffer))
4833         (p (point))
4834         (window-start (window-start)))
4835     (erase-buffer)
4836     (insert buf)
4837     (let ((winconf gnus-prev-winconf))
4838       (gnus-article-mode)
4839       (set-window-configuration winconf)
4840       ;; Tippy-toe some to make sure that point remains where it was.
4841       (save-current-buffer
4842         (set-buffer curbuf)
4843         (set-window-start (get-buffer-window (current-buffer)) window-start)
4844         (goto-char p)))))
4845
4846 (defun gnus-article-edit-full-stops ()
4847   "Interactively repair spacing at end of sentences."
4848   (interactive)
4849   (save-excursion
4850     (goto-char (point-min))
4851     (search-forward-regexp "^$" nil t)
4852     (let ((case-fold-search nil))
4853       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
4854
4855 ;;;
4856 ;;; Article editing with MIME-Edit
4857 ;;;
4858
4859 (defcustom gnus-article-mime-edit-article-setup-hook nil
4860   "Hook run after setting up a MIME editing article buffer."
4861   :group 'gnus-article-various
4862   :type 'hook)
4863
4864 (defun gnus-article-mime-edit-article-unwind ()
4865   "Unwind `gnus-article-buffer' if article editing was given up."
4866   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
4867   (when mime-edit-mode-flag
4868     (mime-edit-exit 'nomime 'no-error)
4869     (message ""))
4870   (when (featurep 'font-lock)
4871     (setq font-lock-defaults nil)
4872     (font-lock-mode 0)))
4873
4874 (defun gnus-article-mime-edit-article-setup ()
4875   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
4876 after replacing with the original article."
4877   (setq gnus-show-mime t)
4878   (setq gnus-article-edit-done-function
4879         `(lambda (&rest args)
4880            (when mime-edit-mode-flag
4881              (mime-edit-exit)
4882              (message ""))
4883            (goto-char (point-min))
4884            (let (case-fold-search)
4885              (when (re-search-forward
4886                     (format "^%s$" (regexp-quote mail-header-separator))
4887                     nil t)
4888                (replace-match "")))
4889            (when (featurep 'font-lock)
4890              (setq font-lock-defaults nil)
4891              (font-lock-mode 0))
4892            (apply ,gnus-article-edit-done-function args)
4893            (set-buffer gnus-original-article-buffer)
4894            (erase-buffer)
4895            (insert-buffer gnus-article-buffer)
4896            (setq gnus-current-headers (gnus-article-make-full-mail-header))
4897            (gnus-article-prepare-display)))
4898   (substitute-key-definition
4899    'gnus-article-edit-exit 'gnus-article-mime-edit-exit
4900    gnus-article-edit-mode-map)
4901   (erase-buffer)
4902   (insert-buffer gnus-original-article-buffer)
4903   (mime-edit-again)
4904   (when (featurep 'font-lock)
4905     (set (make-local-variable 'font-lock-defaults)
4906          '(message-font-lock-keywords t))
4907     (font-lock-set-defaults)
4908     (turn-on-font-lock))
4909   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
4910   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
4911
4912 (defun gnus-article-mime-edit-exit ()
4913   "Exit the article MIME editing without updating."
4914   (interactive)
4915   (let ((winconf gnus-prev-winconf)
4916         buf)
4917     (when mime-edit-mode-flag
4918       (mime-edit-exit)
4919       (message ""))
4920     (goto-char (point-min))
4921     (let (case-fold-search)
4922       (when (re-search-forward
4923              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
4924         (replace-match "")))
4925     (when (featurep 'font-lock)
4926       (setq font-lock-defaults nil)
4927       (font-lock-mode 0))
4928     ;; We remove all text props from the article buffer.
4929     (setq buf (format "%s" (buffer-string)))
4930     (set-buffer (get-buffer-create gnus-original-article-buffer))
4931     (erase-buffer)
4932     (insert buf)
4933     (setq gnus-current-headers (gnus-article-make-full-mail-header))
4934     (gnus-article-prepare-display)
4935     (set-window-configuration winconf)))
4936
4937 ;;;
4938 ;;; Article highlights
4939 ;;;
4940
4941 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
4942
4943 ;;; Internal Variables:
4944
4945 (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\\)\\)"
4946   "Regular expression that matches URLs."
4947   :group 'gnus-article-buttons
4948   :type 'regexp)
4949
4950 (defcustom gnus-button-alist
4951   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
4952      0 t gnus-button-message-id 2)
4953     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
4954     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
4955      1 t
4956      gnus-button-fetch-group 4)
4957     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
4958     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
4959      t gnus-button-message-id 3)
4960     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
4961     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
4962     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
4963     ;; This is how URLs _should_ be embedded in text...
4964     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
4965     ;; Raw URLs.
4966     (,gnus-button-url-regexp 0 t browse-url 0))
4967   "*Alist of regexps matching buttons in article bodies.
4968
4969 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
4970 REGEXP: is the string matching text around the button,
4971 BUTTON: is the number of the regexp grouping actually matching the button,
4972 FORM: is a lisp expression which must eval to true for the button to
4973 be added,
4974 CALLBACK: is the function to call when the user push this button, and each
4975 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
4976
4977 CALLBACK can also be a variable, in that case the value of that
4978 variable it the real callback function."
4979   :group 'gnus-article-buttons
4980   :type '(repeat (list regexp
4981                        (integer :tag "Button")
4982                        (sexp :tag "Form")
4983                        (function :tag "Callback")
4984                        (repeat :tag "Par"
4985                                :inline t
4986                                (integer :tag "Regexp group")))))
4987
4988 (defcustom gnus-header-button-alist
4989   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
4990      0 t gnus-button-message-id 0)
4991     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
4992     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
4993      0 t gnus-button-mailto 0)
4994     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
4995     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
4996     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
4997     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
4998      gnus-button-message-id 3))
4999   "*Alist of headers and regexps to match buttons in article heads.
5000
5001 This alist is very similar to `gnus-button-alist', except that each
5002 alist has an additional HEADER element first in each entry:
5003
5004 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
5005
5006 HEADER is a regexp to match a header.  For a fuller explanation, see
5007 `gnus-button-alist'."
5008   :group 'gnus-article-buttons
5009   :group 'gnus-article-headers
5010   :type '(repeat (list (regexp :tag "Header")
5011                        regexp
5012                        (integer :tag "Button")
5013                        (sexp :tag "Form")
5014                        (function :tag "Callback")
5015                        (repeat :tag "Par"
5016                                :inline t
5017                                (integer :tag "Regexp group")))))
5018
5019 (defvar gnus-button-regexp nil)
5020 (defvar gnus-button-marker-list nil)
5021 ;; Regexp matching any of the regexps from `gnus-button-alist'.
5022
5023 (defvar gnus-button-last nil)
5024 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
5025
5026 ;;; Commands:
5027
5028 (defun gnus-article-push-button (event)
5029   "Check text under the mouse pointer for a callback function.
5030 If the text under the mouse pointer has a `gnus-callback' property,
5031 call it with the value of the `gnus-data' text property."
5032   (interactive "e")
5033   (set-buffer (window-buffer (posn-window (event-start event))))
5034   (let* ((pos (posn-point (event-start event)))
5035          (data (get-text-property pos 'gnus-data))
5036          (fun (get-text-property pos 'gnus-callback)))
5037     (goto-char pos)
5038     (when fun
5039       (funcall fun data))))
5040
5041 (defun gnus-article-press-button ()
5042   "Check text at point for a callback function.
5043 If the text at point has a `gnus-callback' property,
5044 call it with the value of the `gnus-data' text property."
5045   (interactive)
5046   (let* ((data (get-text-property (point) 'gnus-data))
5047          (fun (get-text-property (point) 'gnus-callback)))
5048     (when fun
5049       (funcall fun data))))
5050
5051 (defun gnus-article-prev-button (n)
5052   "Move point to N buttons backward.
5053 If N is negative, move forward instead."
5054   (interactive "p")
5055   (gnus-article-next-button (- n)))
5056
5057 (defun gnus-article-next-button (n)
5058   "Move point to N buttons forward.
5059 If N is negative, move backward instead."
5060   (interactive "p")
5061   (let ((function (if (< n 0) 'previous-single-property-change
5062                     'next-single-property-change))
5063         (inhibit-point-motion-hooks t)
5064         (backward (< n 0))
5065         (limit (if (< n 0) (point-min) (point-max))))
5066     (setq n (abs n))
5067     (while (and (not (= limit (point)))
5068                 (> n 0))
5069       ;; Skip past the current button.
5070       (when (get-text-property (point) 'gnus-callback)
5071         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5072       ;; Go to the next (or previous) button.
5073       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
5074       ;; Put point at the start of the button.
5075       (when (and backward (not (get-text-property (point) 'gnus-callback)))
5076         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5077       ;; Skip past intangible buttons.
5078       (when (get-text-property (point) 'intangible)
5079         (incf n))
5080       (decf n))
5081     (unless (zerop n)
5082       (gnus-message 5 "No more buttons"))
5083     n))
5084
5085 (defun gnus-article-highlight (&optional force)
5086   "Highlight current article.
5087 This function calls `gnus-article-highlight-headers',
5088 `gnus-article-highlight-citation',
5089 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5090 do the highlighting.  See the documentation for those functions."
5091   (interactive (list 'force))
5092   (gnus-article-highlight-headers)
5093   (gnus-article-highlight-citation force)
5094   (gnus-article-highlight-signature)
5095   (gnus-article-add-buttons force)
5096   (gnus-article-add-buttons-to-head))
5097
5098 (defun gnus-article-highlight-some (&optional force)
5099   "Highlight current article.
5100 This function calls `gnus-article-highlight-headers',
5101 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5102 do the highlighting.  See the documentation for those functions."
5103   (interactive (list 'force))
5104   (gnus-article-highlight-headers)
5105   (gnus-article-highlight-signature)
5106   (gnus-article-add-buttons))
5107
5108 (defun gnus-article-highlight-headers ()
5109   "Highlight article headers as specified by `gnus-header-face-alist'."
5110   (interactive)
5111   (save-excursion
5112     (set-buffer gnus-article-buffer)
5113     (save-restriction
5114       (let ((alist gnus-header-face-alist)
5115             (buffer-read-only nil)
5116             (case-fold-search t)
5117             (inhibit-point-motion-hooks t)
5118             entry regexp header-face field-face from hpoints fpoints)
5119         (article-narrow-to-head)
5120         (while (setq entry (pop alist))
5121           (goto-char (point-min))
5122           (setq regexp (concat "^\\("
5123                                (if (string-equal "" (nth 0 entry))
5124                                    "[^\t ]"
5125                                  (nth 0 entry))
5126                                "\\)")
5127                 header-face (nth 1 entry)
5128                 field-face (nth 2 entry))
5129           (while (and (re-search-forward regexp nil t)
5130                       (not (eobp)))
5131             (beginning-of-line)
5132             (setq from (point))
5133             (unless (search-forward ":" nil t)
5134               (forward-char 1))
5135             (when (and header-face
5136                        (not (memq (point) hpoints)))
5137               (push (point) hpoints)
5138               (gnus-put-text-property from (point) 'face header-face))
5139             (when (and field-face
5140                        (not (memq (setq from (point)) fpoints)))
5141               (push from fpoints)
5142               (if (re-search-forward "^[^ \t]" nil t)
5143                   (forward-char -2)
5144                 (goto-char (point-max)))
5145               (gnus-put-text-property from (point) 'face field-face))))))))
5146
5147 (defun gnus-article-highlight-signature ()
5148   "Highlight the signature in an article.
5149 It does this by highlighting everything after
5150 `gnus-signature-separator' using `gnus-signature-face'."
5151   (interactive)
5152   (when gnus-signature-face
5153     (save-excursion
5154       (set-buffer gnus-article-buffer)
5155       (let ((buffer-read-only nil)
5156             (inhibit-point-motion-hooks t))
5157         (save-restriction
5158           (when (gnus-article-narrow-to-signature)
5159             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
5160                               'face gnus-signature-face)))))))
5161
5162 (defun gnus-article-buttonize-signature ()
5163   "Add button to the signature."
5164   (interactive)
5165   (save-excursion
5166     (set-buffer gnus-article-buffer)
5167     (let ((buffer-read-only nil)
5168           (inhibit-point-motion-hooks t))
5169       (when (gnus-article-search-signature)
5170         (gnus-article-add-button (match-beginning 0) (match-end 0)
5171                                  'gnus-signature-toggle
5172                                  (set-marker (make-marker)
5173                                              (1+ (match-end 0))))))))
5174
5175 (defun gnus-button-in-region-p (b e prop)
5176   "Say whether PROP exists in the region."
5177   (text-property-not-all b e prop nil))
5178
5179 (defun gnus-article-add-buttons (&optional force)
5180   "Find external references in the article and make buttons of them.
5181 \"External references\" are things like Message-IDs and URLs, as
5182 specified by `gnus-button-alist'."
5183   (interactive (list 'force))
5184   (save-excursion
5185     (set-buffer gnus-article-buffer)
5186     (let ((buffer-read-only nil)
5187           (inhibit-point-motion-hooks t)
5188           (case-fold-search t)
5189           (alist gnus-button-alist)
5190           beg entry regexp)
5191       ;; Remove all old markers.
5192       (let (marker entry new-list)
5193         (while (setq marker (pop gnus-button-marker-list))
5194           (if (or (< marker (point-min)) (>= marker (point-max)))
5195               (push marker new-list)
5196             (goto-char marker)
5197             (when (setq entry (gnus-button-entry))
5198               (put-text-property (match-beginning (nth 1 entry))
5199                                  (match-end (nth 1 entry))
5200                                  'gnus-callback nil))
5201             (set-marker marker nil)))
5202         (setq gnus-button-marker-list new-list))
5203       ;; We skip the headers.
5204       (article-goto-body)
5205       (setq beg (point))
5206       (while (setq entry (pop alist))
5207         (setq regexp (car entry))
5208         (goto-char beg)
5209         (while (re-search-forward regexp nil t)
5210           (let* ((start (and entry (match-beginning (nth 1 entry))))
5211                  (end (and entry (match-end (nth 1 entry))))
5212                  (from (match-beginning 0)))
5213             (when (and (or (eq t (nth 2 entry))
5214                            (eval (nth 2 entry)))
5215                        (not (gnus-button-in-region-p
5216                              start end 'gnus-callback)))
5217               ;; That optional form returned non-nil, so we add the
5218               ;; button.
5219               (gnus-article-add-button
5220                start end 'gnus-button-push
5221                (car (push (set-marker (make-marker) from)
5222                           gnus-button-marker-list))))))))))
5223
5224 ;; Add buttons to the head of an article.
5225 (defun gnus-article-add-buttons-to-head ()
5226   "Add buttons to the head of the article."
5227   (interactive)
5228   (save-excursion
5229     (set-buffer gnus-article-buffer)
5230     (save-restriction
5231       (let ((buffer-read-only nil)
5232             (inhibit-point-motion-hooks t)
5233             (case-fold-search t)
5234             (alist gnus-header-button-alist)
5235             entry beg end)
5236         (article-narrow-to-head)
5237         (while alist
5238           ;; Each alist entry.
5239           (setq entry (car alist)
5240                 alist (cdr alist))
5241           (goto-char (point-min))
5242           (while (re-search-forward (car entry) nil t)
5243             ;; Each header matching the entry.
5244             (setq beg (match-beginning 0))
5245             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
5246                                (match-beginning 0))
5247                           (point-max)))
5248             (goto-char beg)
5249             (while (re-search-forward (nth 1 entry) end t)
5250               ;; Each match within a header.
5251               (let* ((entry (cdr entry))
5252                      (start (match-beginning (nth 1 entry)))
5253                      (end (match-end (nth 1 entry)))
5254                      (form (nth 2 entry)))
5255                 (goto-char (match-end 0))
5256                 (when (eval form)
5257                   (gnus-article-add-button
5258                    start end (nth 3 entry)
5259                    (buffer-substring (match-beginning (nth 4 entry))
5260                                      (match-end (nth 4 entry)))))))
5261             (goto-char end)))))))
5262
5263 ;;; External functions:
5264
5265 (defun gnus-article-add-button (from to fun &optional data)
5266   "Create a button between FROM and TO with callback FUN and data DATA."
5267   (when gnus-article-button-face
5268     (gnus-overlay-put (gnus-make-overlay from to)
5269                       'face gnus-article-button-face))
5270   (gnus-add-text-properties
5271    from to
5272    (nconc (and gnus-article-mouse-face
5273                (list gnus-mouse-face-prop gnus-article-mouse-face))
5274           (list 'gnus-callback fun)
5275           (and data (list 'gnus-data data))))
5276   (widget-convert-button 'link from to :action 'gnus-widget-press-button
5277                          ;; Quote `:button-keymap' for Mule 2.3
5278                          ;; but it won't work.
5279                          ':button-keymap gnus-widget-button-keymap))
5280
5281 ;;; Internal functions:
5282
5283 (defun gnus-article-set-globals ()
5284   (save-excursion
5285     (set-buffer gnus-summary-buffer)
5286     (gnus-set-global-variables)))
5287
5288 (defun gnus-signature-toggle (end)
5289   (save-excursion
5290     (set-buffer gnus-article-buffer)
5291     (let ((buffer-read-only nil)
5292           (inhibit-point-motion-hooks t)
5293           (limit (next-single-property-change end 'mime-view-entity
5294                                               nil (point-max))))
5295       (if (text-property-any end limit 'article-type 'signature)
5296           (gnus-remove-text-properties-when
5297            'article-type 'signature end limit
5298            (cons 'article-type (cons 'signature
5299                                      gnus-hidden-properties)))
5300         (gnus-add-text-properties-when
5301          'article-type nil end limit
5302          (cons 'article-type (cons 'signature
5303                                    gnus-hidden-properties)))))))
5304
5305 (defun gnus-button-entry ()
5306   ;; Return the first entry in `gnus-button-alist' matching this place.
5307   (let ((alist gnus-button-alist)
5308         (entry nil))
5309     (while alist
5310       (setq entry (pop alist))
5311       (if (looking-at (car entry))
5312           (setq alist nil)
5313         (setq entry nil)))
5314     entry))
5315
5316 (defun gnus-button-push (marker)
5317   ;; Push button starting at MARKER.
5318   (save-excursion
5319     (goto-char marker)
5320     (let* ((entry (gnus-button-entry))
5321            (inhibit-point-motion-hooks t)
5322            (fun (nth 3 entry))
5323            (args (mapcar (lambda (group)
5324                            (let ((string (match-string group)))
5325                              (gnus-set-text-properties
5326                               0 (length string) nil string)
5327                              string))
5328                          (nthcdr 4 entry))))
5329       (cond
5330        ((fboundp fun)
5331         (apply fun args))
5332        ((and (boundp fun)
5333              (fboundp (symbol-value fun)))
5334         (apply (symbol-value fun) args))
5335        (t
5336         (gnus-message 1 "You must define `%S' to use this button"
5337                       (cons fun args)))))))
5338
5339 (defun gnus-button-message-id (message-id)
5340   "Fetch MESSAGE-ID."
5341   (save-excursion
5342     (set-buffer gnus-summary-buffer)
5343     (gnus-summary-refer-article message-id)))
5344
5345 (defun gnus-button-fetch-group (address)
5346   "Fetch GROUP specified by ADDRESS."
5347   (if (not (string-match "[:/]" address))
5348       ;; This is just a simple group url.
5349       (gnus-group-read-ephemeral-group address gnus-select-method)
5350     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
5351                            address))
5352         (error "Can't parse %s" address)
5353       (gnus-group-read-ephemeral-group
5354        (match-string 4 address)
5355        `(nntp ,(match-string 1 address)
5356               (nntp-address ,(match-string 1 address))
5357               (nntp-port-number ,(if (match-end 3)
5358                                      (match-string 3 address)
5359                                    "nntp")))))))
5360
5361 (defun gnus-url-parse-query-string (query &optional downcase)
5362   (let (retval pairs cur key val)
5363     (setq pairs (split-string query "&"))
5364     (while pairs
5365       (setq cur (car pairs)
5366             pairs (cdr pairs))
5367       (if (not (string-match "=" cur))
5368           nil                           ; Grace
5369         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
5370               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
5371         (if downcase
5372             (setq key (downcase key)))
5373         (setq cur (assoc key retval))
5374         (if cur
5375             (setcdr cur (cons val (cdr cur)))
5376           (setq retval (cons (list key val) retval)))))
5377     retval))
5378
5379 (defun gnus-url-unhex (x)
5380   (if (> x ?9)
5381       (if (>= x ?a)
5382           (+ 10 (- x ?a))
5383         (+ 10 (- x ?A)))
5384     (- x ?0)))
5385
5386 (defun gnus-url-unhex-string (str &optional allow-newlines)
5387   "Remove %XXX embedded spaces, etc in a url.
5388 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
5389 decoding of carriage returns and line feeds in the string, which is normally
5390 forbidden in URL encoding."
5391   (setq str (or str ""))
5392   (let ((tmp "")
5393         (case-fold-search t))
5394     (while (string-match "%[0-9a-f][0-9a-f]" str)
5395       (let* ((start (match-beginning 0))
5396              (ch1 (gnus-url-unhex (elt str (+ start 1))))
5397              (code (+ (* 16 ch1)
5398                       (gnus-url-unhex (elt str (+ start 2))))))
5399         (setq tmp (concat
5400                    tmp (substring str 0 start)
5401                    (cond
5402                     (allow-newlines
5403                      (char-to-string code))
5404                     ((or (= code ?\n) (= code ?\r))
5405                      " ")
5406                     (t (char-to-string code))))
5407               str (substring str (match-end 0)))))
5408     (setq tmp (concat tmp str))
5409     tmp))
5410
5411 (defun gnus-url-mailto (url)
5412   ;; Send mail to someone
5413   (when (string-match "mailto:/*\\(.*\\)" url)
5414     (setq url (substring url (match-beginning 1) nil)))
5415   (let (to args subject func)
5416     (if (string-match (regexp-quote "?") url)
5417         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
5418               args (gnus-url-parse-query-string
5419                     (substring url (match-end 0) nil) t))
5420       (setq to (gnus-url-unhex-string url)))
5421     (setq args (cons (list "to" to) args)
5422           subject (cdr-safe (assoc "subject" args)))
5423     (gnus-setup-message 'reply
5424       (message-mail)
5425       (while args
5426         (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
5427         (if (fboundp func)
5428             (funcall func)
5429           (message-position-on-field (caar args)))
5430         (insert (mapconcat 'identity (cdar args) ", "))
5431         (setq args (cdr args)))
5432       (if subject
5433           (message-goto-body)
5434         (message-goto-subject)))))
5435
5436 (defun gnus-button-mailto (address)
5437   "Mail to ADDRESS."
5438   (set-buffer (gnus-copy-article-buffer))
5439   (message-reply address))
5440
5441 (defalias 'gnus-button-reply 'message-reply)
5442
5443 (defun gnus-button-embedded-url (address)
5444   "Activate ADDRESS with `browse-url'."
5445   (browse-url (gnus-strip-whitespace address)))
5446
5447 (defun gnus-article-smiley-display ()
5448   "Display \"smileys\" as small graphical icons."
5449   (smiley-toggle-buffer 1 (current-buffer) (point-min) (point-max)))
5450
5451 ;;; Next/prev buttons in the article buffer.
5452
5453 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
5454 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
5455
5456 (defvar gnus-prev-page-map nil)
5457 (unless gnus-prev-page-map
5458   (setq gnus-prev-page-map (make-sparse-keymap))
5459   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
5460   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
5461
5462 (static-if (featurep 'xemacs)
5463     (defun gnus-insert-prev-page-button ()
5464       (let ((buffer-read-only nil))
5465         (gnus-eval-format
5466          gnus-prev-page-line-format nil
5467          `(gnus-prev t local-map ,gnus-prev-page-map
5468                      gnus-callback gnus-article-button-prev-page
5469                      article-type annotation))))
5470   (defun gnus-insert-prev-page-button ()
5471     (let ((buffer-read-only nil)
5472           (situation (get-text-property (point-min) 'mime-view-situation)))
5473       (set-keymap-parent gnus-prev-page-map (current-local-map))
5474       (gnus-eval-format
5475        gnus-prev-page-line-format nil
5476        `(gnus-prev t local-map ,gnus-prev-page-map
5477                    gnus-callback gnus-article-button-prev-page
5478                    article-type annotation
5479                    mime-view-situation ,situation))))
5480   )
5481
5482 (defvar gnus-next-page-map nil)
5483 (unless gnus-next-page-map
5484   (setq gnus-next-page-map (make-sparse-keymap))
5485   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
5486   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
5487
5488 (defun gnus-button-next-page ()
5489   "Go to the next page."
5490   (interactive)
5491   (let ((win (selected-window)))
5492     (select-window (get-buffer-window gnus-article-buffer t))
5493     (gnus-article-next-page)
5494     (select-window win)))
5495
5496 (defun gnus-button-prev-page ()
5497   "Go to the prev page."
5498   (interactive)
5499   (let ((win (selected-window)))
5500     (select-window (get-buffer-window gnus-article-buffer t))
5501     (gnus-article-prev-page)
5502     (select-window win)))
5503
5504 (static-if (featurep 'xemacs)
5505     (defun gnus-insert-next-page-button ()
5506       (let ((buffer-read-only nil))
5507         (gnus-eval-format gnus-next-page-line-format nil
5508                           `(gnus-next
5509                             t local-map ,gnus-next-page-map
5510                             gnus-callback gnus-article-button-next-page
5511                             article-type annotation))))
5512   (defun gnus-insert-next-page-button ()
5513     (let ((buffer-read-only nil)
5514           (situation (get-text-property (point-min) 'mime-view-situation)))
5515       (set-keymap-parent gnus-next-page-map (current-local-map))
5516       (gnus-eval-format gnus-next-page-line-format nil
5517                         `(gnus-next
5518                           t local-map ,gnus-next-page-map
5519                           gnus-callback gnus-article-button-next-page
5520                           article-type annotation
5521                           mime-view-situation ,situation))))
5522   )
5523
5524 (defun gnus-article-button-next-page (arg)
5525   "Go to the next page."
5526   (interactive "P")
5527   (let ((win (selected-window)))
5528     (select-window (get-buffer-window gnus-article-buffer t))
5529     (gnus-article-next-page)
5530     (select-window win)))
5531
5532 (defun gnus-article-button-prev-page (arg)
5533   "Go to the prev page."
5534   (interactive "P")
5535   (let ((win (selected-window)))
5536     (select-window (get-buffer-window gnus-article-buffer t))
5537     (gnus-article-prev-page)
5538     (select-window win)))
5539
5540 (defvar gnus-decode-header-methods
5541   '(mail-decode-encoded-word-region)
5542   "List of methods used to decode headers.
5543
5544 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
5545 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
5546 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
5547 whose names match REGEXP.
5548
5549 For example:
5550 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
5551  mail-decode-encoded-word-region
5552  (\"chinese\" . rfc1843-decode-region))
5553 ")
5554
5555 (defvar gnus-decode-header-methods-cache nil)
5556
5557 (defun gnus-multi-decode-header (start end)
5558   "Apply the functions from `gnus-encoded-word-methods' that match."
5559   (unless (and gnus-decode-header-methods-cache
5560                (eq gnus-newsgroup-name
5561                    (car gnus-decode-header-methods-cache)))
5562     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
5563     (mapcar (lambda (x)
5564               (if (symbolp x)
5565                   (nconc gnus-decode-header-methods-cache (list x))
5566                 (if (and gnus-newsgroup-name
5567                          (string-match (car x) gnus-newsgroup-name))
5568                     (nconc gnus-decode-header-methods-cache
5569                            (list (cdr x))))))
5570           gnus-decode-header-methods))
5571   (let ((xlist gnus-decode-header-methods-cache))
5572     (pop xlist)
5573     (save-restriction
5574       (narrow-to-region start end)
5575       (while xlist
5576         (funcall (pop xlist) (point-min) (point-max))))))
5577
5578 ;;;
5579 ;;; Treatment top-level handling.
5580 ;;;
5581
5582 (defun gnus-treat-article (condition &optional part-number total-parts type)
5583   (let ((length (- (point-max) (point-min)))
5584         (alist gnus-treatment-function-alist)
5585         (article-goto-body-goes-to-point-min-p t)
5586         (treated-type
5587          (or (not type)
5588              (catch 'found
5589                (let ((list gnus-article-treat-types))
5590                  (while list
5591                    (when (string-match (pop list) type)
5592                      (throw 'found t)))))))
5593         (highlightp (gnus-visual-p 'article-highlight 'highlight))
5594         (entity (static-unless (featurep 'xemacs)
5595                   (when (eq 'head condition)
5596                     (get-text-property (point-min) 'mime-view-entity))))
5597         val elem buttonized)
5598     (gnus-run-hooks 'gnus-part-display-hook)
5599     (unless gnus-inhibit-treatment
5600       (while (setq elem (pop alist))
5601         (setq val
5602               (save-excursion
5603                 (if (gnus-buffer-live-p gnus-summary-buffer)
5604                     (set-buffer gnus-summary-buffer))
5605                 (symbol-value (car elem))))
5606         (when (and (or (consp val)
5607                        treated-type)
5608                    (gnus-treat-predicate val)
5609                    (or (not (get (car elem) 'highlight))
5610                        highlightp))
5611           (when (and (not buttonized)
5612                      (memq (car elem)
5613                            '(gnus-treat-hide-signature
5614                              gnus-treat-highlight-signature)))
5615             (gnus-article-buttonize-signature)
5616             (setq buttonized t))
5617           (save-restriction
5618             (funcall (cadr elem)))))
5619       ;; FSF Emacsen does not inherit the existing text properties
5620       ;; in the new text, so we should do it for `mime-view-entity'.
5621       (static-unless (featurep 'xemacs)
5622         (when entity
5623           (put-text-property (point-min) (point-max)
5624                              'mime-view-entity entity))))))
5625
5626 ;; Dynamic variables.
5627 (eval-when-compile
5628   (defvar part-number)
5629   (defvar total-parts)
5630   (defvar type)
5631   (defvar condition)
5632   (defvar length))
5633
5634 (defun gnus-treat-predicate (val)
5635   (cond
5636    ((null val)
5637     nil)
5638    ((and (listp val)
5639          (stringp (car val)))
5640     (apply 'gnus-or (mapcar `(lambda (s)
5641                                (string-match s ,(or gnus-newsgroup-name "")))
5642                             val)))
5643    ((listp val)
5644     (let ((pred (pop val)))
5645       (cond
5646        ((eq pred 'or)
5647         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
5648        ((eq pred 'and)
5649         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
5650        ((eq pred 'not)
5651         (not (gnus-treat-predicate (car val))))
5652        ((eq pred 'typep)
5653         (equal (car val) type))
5654        (t
5655         (error "%S is not a valid predicate" pred)))))
5656    ((eq val 'mime)
5657     gnus-show-mime)
5658    (condition
5659     (eq condition val))
5660    ((eq val t)
5661     t)
5662    ((eq val 'head)
5663     nil)
5664    ((eq val 'last)
5665     (eq part-number total-parts))
5666    ((numberp val)
5667     (< length val))
5668    (t
5669     (error "%S is not a valid value" val))))
5670
5671 (defun gnus-article-encrypt-body (protocol &optional n)
5672   "Encrypt the article body."
5673   (interactive 
5674    (list
5675     (or gnus-article-encrypt-protocol
5676         (completing-read "Encrypt protocol: "
5677                          gnus-article-encrypt-protocol-alist 
5678                          nil t))
5679     current-prefix-arg))
5680   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
5681     (unless func
5682       (error (format "Can't find the encrypt protocol %s" protocol)))
5683     (if (equal gnus-newsgroup-name "nndraft:drafts")
5684         (error "Can't encrypt the article in group nndraft:drafts."))
5685     (if (equal gnus-newsgroup-name "nndraft:queue")
5686         (error "Don't encrypt the article in group nndraft:queue."))
5687     (gnus-summary-iterate n
5688       (save-excursion
5689         (set-buffer gnus-summary-buffer)
5690         (let ((mail-parse-charset gnus-newsgroup-charset)
5691               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5692               (summary-buffer gnus-summary-buffer)
5693               references point)
5694           (gnus-set-global-variables)
5695           (when (gnus-group-read-only-p)
5696             (error "The current newsgroup does not support article encrypt"))
5697           (gnus-summary-show-article t)
5698           (setq references
5699               (or (mail-header-references gnus-current-headers) ""))
5700           (set-buffer gnus-article-buffer)
5701           (let* ((buffer-read-only nil)
5702                  (headers
5703                   (mapcar (lambda (field)
5704                             (and (save-restriction 
5705                                    (message-narrow-to-head)
5706                                    (goto-char (point-min))
5707                                    (search-forward field nil t))
5708                                  (prog2
5709                                      (message-narrow-to-field)
5710                                      (buffer-substring (point-min) (point-max))
5711                                    (delete-region (point-min) (point-max))
5712                                    (widen))))
5713                           '("Content-Type:" "Content-Transfer-Encoding:"
5714                             "Content-Disposition:"))))
5715             (message-narrow-to-head)
5716             (message-remove-header "MIME-Version")
5717             (goto-char (point-max))
5718             (setq point (point))
5719             (insert (apply 'concat headers))
5720             (widen)
5721             (narrow-to-region point (point-max))
5722             (let ((message-options message-options))
5723               (message-options-set 'message-sender user-mail-address)
5724               (message-options-set 'message-recipients user-mail-address)
5725               (message-options-set 'message-sign-encrypt 'not)
5726               (funcall func))
5727             (goto-char (point-min))
5728             (insert "MIME-Version: 1.0\n")
5729             (widen)
5730             (gnus-summary-edit-article-done
5731              references nil summary-buffer t))
5732           (when gnus-keep-backlog
5733             (gnus-backlog-remove-article
5734              (car gnus-article-current) (cdr gnus-article-current)))
5735           (save-excursion
5736             (when (get-buffer gnus-original-article-buffer)
5737               (set-buffer gnus-original-article-buffer)
5738               (setq gnus-original-article nil)))
5739           (when gnus-use-cache
5740             (gnus-cache-update-article
5741              (car gnus-article-current) (cdr gnus-article-current))))))))
5742
5743 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
5744   "The following specs can be used:
5745 %t  The security MIME type
5746 %i  Additional info
5747 %d  Details
5748 %D  Details if button is pressed")
5749
5750 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
5751   "The following specs can be used:
5752 %t  The security MIME type
5753 %i  Additional info
5754 %d  Details
5755 %D  Details if button is pressed")
5756
5757 (defvar gnus-mime-security-button-line-format-alist
5758   '((?t gnus-tmp-type ?s)
5759     (?i gnus-tmp-info ?s)
5760     (?d gnus-tmp-details ?s)
5761     (?D gnus-tmp-pressed-details ?s)))
5762
5763 (defvar gnus-mime-security-button-map
5764   (let ((map (make-sparse-keymap)))
5765     ;; Not for Emacs 21: fixme better.
5766     ;;(set-keymap-parent map gnus-article-mode-map)
5767     (define-key map gnus-mouse-2 'gnus-article-push-button)
5768     (define-key map "\r" 'gnus-article-press-button)
5769     map))
5770
5771 (defvar gnus-mime-security-details-buffer nil)
5772
5773 (defvar gnus-mime-security-button-pressed nil)
5774
5775 (defvar gnus-mime-security-show-details-inline t
5776   "If non-nil, show details in the article buffer.")
5777
5778 (defun gnus-mime-security-verify-or-decrypt (handle)
5779   (mm-remove-parts (cdr handle))
5780   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
5781         buffer-read-only)
5782     (when region 
5783       (delete-region (car region) (cdr region))
5784       (set-marker (car region) nil)
5785       (set-marker (cdr region) nil)))
5786   (with-current-buffer (mm-handle-multipart-original-buffer handle)
5787     (let* ((mm-verify-option 'known)
5788            (mm-decrypt-option 'known)
5789            (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
5790       (unless (eq nparts (cdr handle))
5791         (mm-destroy-parts (cdr handle))
5792         (setcdr handle nparts))))
5793   (let ((point (point))
5794         buffer-read-only)
5795     (gnus-mime-display-security handle)
5796     (goto-char point)))
5797
5798 (defun gnus-mime-security-show-details (handle)
5799   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
5800     (if details
5801         (if gnus-mime-security-show-details-inline
5802             (let ((gnus-mime-security-button-pressed t)
5803                   (gnus-mime-security-button-line-format 
5804                    (get-text-property (point) 'gnus-line-format))
5805                 buffer-read-only)
5806               (forward-char -1)
5807               (while (eq (get-text-property (point) 'gnus-line-format)
5808                          gnus-mime-security-button-line-format)
5809                 (forward-char -1))
5810               (forward-char)
5811               (delete-region (point)
5812                              (or (text-property-not-all 
5813                                   (point) (point-max)
5814                                 'gnus-line-format   
5815                                 gnus-mime-security-button-line-format)
5816                                  (point-max)))
5817               (gnus-insert-mime-security-button handle))
5818           (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
5819               (with-current-buffer gnus-mime-security-details-buffer
5820                 (erase-buffer)
5821                 t)
5822             (setq gnus-mime-security-details-buffer
5823                   (gnus-get-buffer-create "*MIME Security Details*")))
5824           (with-current-buffer gnus-mime-security-details-buffer
5825             (insert details)
5826             (goto-char (point-min)))
5827           (pop-to-buffer gnus-mime-security-details-buffer))
5828       (gnus-message 5 "No details."))))
5829
5830 (defun gnus-mime-security-press-button (handle)
5831   (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
5832       (gnus-mime-security-show-details handle)
5833     (gnus-mime-security-verify-or-decrypt handle)))
5834
5835 (defun gnus-insert-mime-security-button (handle &optional displayed)
5836   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
5837          (gnus-tmp-type
5838           (concat 
5839            (or (nth 2 (assoc protocol mm-verify-function-alist))
5840                (nth 2 (assoc protocol mm-decrypt-function-alist))
5841                "Unknown")
5842            (if (equal (car handle) "multipart/signed")
5843                " Signed" " Encrypted")
5844            " Part"))
5845          (gnus-tmp-info
5846           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
5847               "Undecided"))
5848          (gnus-tmp-details
5849           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
5850          gnus-tmp-pressed-details
5851          b e)
5852     (setq gnus-tmp-details
5853           (if gnus-tmp-details
5854               (concat "\n" gnus-tmp-details) ""))
5855     (setq gnus-tmp-pressed-details 
5856           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
5857     (unless (bolp)
5858       (insert "\n"))
5859     (setq b (point))
5860     (gnus-eval-format
5861      gnus-mime-security-button-line-format 
5862      gnus-mime-security-button-line-format-alist
5863      `(keymap ,gnus-mime-security-button-map
5864               ,@(if (>= (string-to-number emacs-version) 21)
5865                     nil ;; XEmacs doesn't care
5866                   (list 'local-map gnus-mime-security-button-map))
5867               gnus-callback gnus-mime-security-press-button
5868               gnus-line-format ,gnus-mime-security-button-line-format 
5869               article-type annotation
5870               gnus-data ,handle))
5871     (setq e (point))
5872     (widget-convert-button
5873      'link b e
5874      :mime-handle handle
5875      :action 'gnus-widget-press-button
5876      :button-keymap gnus-mime-security-button-map
5877      :help-echo
5878      (lambda (widget/window &optional overlay pos)
5879        ;; Needed to properly clear the message due to a bug in
5880        ;; wid-edit (XEmacs only).
5881        (if (boundp 'help-echo-owns-message)
5882            (setq help-echo-owns-message t))
5883        (format
5884         "%S: show detail"
5885         (aref gnus-mouse-2 0))))))
5886
5887 (defun gnus-mime-display-security (handle)
5888   (save-restriction
5889     (narrow-to-region (point) (point))
5890     (gnus-insert-mime-security-button handle)
5891     (gnus-mime-display-mixed (cdr handle))
5892     (unless (bolp)
5893       (insert "\n"))
5894     (let ((gnus-mime-security-button-line-format 
5895            gnus-mime-security-button-end-line-format))
5896       (gnus-insert-mime-security-button handle))
5897     (mm-set-handle-multipart-parameter handle 'gnus-region 
5898                                        (cons (set-marker (make-marker)
5899                                                          (point-min))
5900                                              (set-marker (make-marker)
5901                                                          (point-max))))))
5902
5903
5904 ;;; @ for mime-view
5905 ;;;
5906
5907 (defun gnus-article-header-presentation-method (entity situation)
5908   (mime-insert-header entity)
5909   )
5910
5911 (set-alist 'mime-header-presentation-method-alist
5912            'gnus-original-article-mode
5913            #'gnus-article-header-presentation-method)
5914
5915 (defun gnus-mime-preview-quitting-method ()
5916   (mime-preview-kill-buffer)
5917   (delete-other-windows)
5918   (gnus-article-show-summary)
5919   (gnus-summary-select-article gnus-show-all-headers t))
5920
5921 (set-alist 'mime-preview-quitting-method-alist
5922            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
5923
5924 (set-alist 'mime-preview-following-method-alist
5925            'gnus-original-article-mode #'gnus-following-method)
5926
5927 (set-alist 'mime-preview-over-to-previous-method-alist
5928            'gnus-original-article-mode
5929            (lambda ()
5930              (if (> (point-min) 1)
5931                  (gnus-article-prev-page)
5932                (gnus-article-read-summary-keys
5933                 nil (gnus-character-to-event ?P)))))
5934
5935 (set-alist 'mime-preview-over-to-next-method-alist
5936            'gnus-original-article-mode'
5937            (lambda ()
5938              (if (< (point-max) (buffer-size))
5939                  (gnus-article-next-page)
5940                (gnus-article-read-summary-keys
5941                 nil (gnus-character-to-event ?N)))))
5942
5943
5944 ;;; @ end
5945 ;;;
5946
5947 (gnus-ems-redefine)
5948
5949 (provide 'gnus-art)
5950
5951 (run-hooks 'gnus-art-load-hook)
5952
5953 ;;; gnus-art.el ends here