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