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