2000-11-06 14:03:10 ShengHuo ZHU <zsh@cs.rochester.edu>
[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 ;;; Internal variables
970
971 (defvar article-goto-body-goes-to-point-min-p nil)
972 (defvar gnus-article-wash-types nil)
973 (defvar gnus-article-emphasis-alist nil)
974
975 (defvar gnus-article-mime-handle-alist-1 nil)
976 (defvar gnus-treatment-function-alist
977   '((gnus-treat-strip-banner gnus-article-strip-banner)
978     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
979     (gnus-treat-highlight-signature gnus-article-highlight-signature)
980     (gnus-treat-buttonize gnus-article-add-buttons)
981     (gnus-treat-fill-article gnus-article-fill-cited-article)
982     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
983     (gnus-treat-strip-cr gnus-article-remove-cr)
984     (gnus-treat-emphasize gnus-article-emphasize)
985     (gnus-treat-display-xface gnus-article-display-x-face)
986     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
987     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
988     (gnus-treat-hide-signature gnus-article-hide-signature)
989     (gnus-treat-hide-citation gnus-article-hide-citation)
990     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
991     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
992     (gnus-treat-strip-pgp gnus-article-hide-pgp)
993     (gnus-treat-strip-pem gnus-article-hide-pem)
994     (gnus-treat-highlight-headers gnus-article-highlight-headers)
995     (gnus-treat-highlight-citation gnus-article-highlight-citation)
996     (gnus-treat-highlight-signature gnus-article-highlight-signature)
997     (gnus-treat-date-ut gnus-article-date-ut)
998     (gnus-treat-date-local gnus-article-date-local)
999     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1000     (gnus-treat-date-original gnus-article-date-original)
1001     (gnus-treat-date-user-defined gnus-article-date-user)
1002     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1003     (gnus-treat-strip-trailing-blank-lines
1004      gnus-article-remove-trailing-blank-lines)
1005     (gnus-treat-strip-leading-blank-lines
1006      gnus-article-strip-leading-blank-lines)
1007     (gnus-treat-strip-multiple-blank-lines
1008      gnus-article-strip-multiple-blank-lines)
1009     (gnus-treat-overstrike gnus-article-treat-overstrike)
1010     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1011     (gnus-treat-display-smileys gnus-smiley-display)
1012     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1013     (gnus-treat-display-picons gnus-article-display-picons)
1014     (gnus-treat-play-sounds gnus-earcon-display)))
1015
1016 (defvar gnus-article-mime-handle-alist nil)
1017 (defvar article-lapsed-timer nil)
1018 (defvar gnus-article-current-summary nil)
1019
1020 (defvar gnus-article-mode-syntax-table
1021   (let ((table (copy-syntax-table text-mode-syntax-table)))
1022     (modify-syntax-entry ?- "w" table)
1023     (modify-syntax-entry ?> ")" table)
1024     (modify-syntax-entry ?< "(" table)
1025     table)
1026   "Syntax table used in article mode buffers.
1027 Initialized from `text-mode-syntax-table.")
1028
1029 (defvar gnus-save-article-buffer nil)
1030
1031 (defvar gnus-article-mode-line-format-alist
1032   (nconc '((?w (gnus-article-wash-status) ?s)
1033            (?m (gnus-article-mime-part-status) ?s))
1034          gnus-summary-mode-line-format-alist))
1035
1036 (defvar gnus-number-of-articles-to-be-saved nil)
1037
1038 (defvar gnus-inhibit-hiding nil)
1039
1040 (defsubst gnus-article-hide-text (b e props)
1041   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1042   (add-text-properties b e props)
1043   (when (memq 'intangible props)
1044     (put-text-property
1045      (max (1- b) (point-min))
1046      b 'intangible (cddr (memq 'intangible props)))))
1047 (defsubst gnus-article-unhide-text (b e)
1048   "Remove hidden text properties from region between B and E."
1049   (remove-text-properties b e gnus-hidden-properties)
1050   (when (memq 'intangible gnus-hidden-properties)
1051     (put-text-property (max (1- b) (point-min))
1052                        b 'intangible nil)))
1053
1054 (defun gnus-article-hide-text-type (b e type)
1055   "Hide text of TYPE between B and E."
1056   (push type gnus-article-wash-types)
1057   (gnus-article-hide-text
1058    b e (cons 'article-type (cons type gnus-hidden-properties))))
1059
1060 (defun gnus-article-unhide-text-type (b e type)
1061   "Unhide text of TYPE between B and E."
1062   (setq gnus-article-wash-types
1063         (delq type gnus-article-wash-types))
1064   (remove-text-properties
1065    b e (cons 'article-type (cons type gnus-hidden-properties)))
1066   (when (memq 'intangible gnus-hidden-properties)
1067     (put-text-property (max (1- b) (point-min))
1068                        b 'intangible nil)))
1069
1070 (defun gnus-article-hide-text-of-type (type)
1071   "Hide text of TYPE in the current buffer."
1072   (save-excursion
1073     (let ((b (point-min))
1074           (e (point-max)))
1075       (while (setq b (text-property-any b e 'article-type type))
1076         (add-text-properties b (incf b) gnus-hidden-properties)))))
1077
1078 (defun gnus-article-delete-text-of-type (type)
1079   "Delete text of TYPE in the current buffer."
1080   (save-excursion
1081     (let ((b (point-min)))
1082       (while (setq b (text-property-any b (point-max) 'article-type type))
1083         (delete-region
1084          b (or (text-property-not-all b (point-max) 'article-type type)
1085                (point-max)))))))
1086
1087 (defun gnus-article-delete-invisible-text ()
1088   "Delete all invisible text in the current buffer."
1089   (save-excursion
1090     (let ((b (point-min)))
1091       (while (setq b (text-property-any b (point-max) 'invisible t))
1092         (delete-region
1093          b (or (text-property-not-all b (point-max) 'invisible t)
1094                (point-max)))))))
1095
1096 (defun gnus-article-text-type-exists-p (type)
1097   "Say whether any text of type TYPE exists in the buffer."
1098   (text-property-any (point-min) (point-max) 'article-type type))
1099
1100 (defsubst gnus-article-header-rank ()
1101   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1102   (let ((list gnus-sorted-header-list)
1103         (i 0))
1104     (while list
1105       (when (looking-at (car list))
1106         (setq list nil))
1107       (setq list (cdr list))
1108       (incf i))
1109     i))
1110
1111 (defun article-hide-headers (&optional arg delete)
1112   "Hide unwanted headers and possibly sort them as well."
1113   (interactive)
1114   ;; This function might be inhibited.
1115   (unless gnus-inhibit-hiding
1116     (save-excursion
1117       (save-restriction
1118         (let ((buffer-read-only nil)
1119               (case-fold-search t)
1120               (max (1+ (length gnus-sorted-header-list)))
1121               (ignored (when (not gnus-visible-headers)
1122                          (cond ((stringp gnus-ignored-headers)
1123                                 gnus-ignored-headers)
1124                                ((listp gnus-ignored-headers)
1125                                 (mapconcat 'identity gnus-ignored-headers
1126                                            "\\|")))))
1127               (visible
1128                (cond ((stringp gnus-visible-headers)
1129                       gnus-visible-headers)
1130                      ((and gnus-visible-headers
1131                            (listp gnus-visible-headers))
1132                       (mapconcat 'identity gnus-visible-headers "\\|"))))
1133               (inhibit-point-motion-hooks t)
1134               beg)
1135           ;; First we narrow to just the headers.
1136           (article-narrow-to-head)
1137           ;; Hide any "From " lines at the beginning of (mail) articles.
1138           (while (looking-at "From ")
1139             (forward-line 1))
1140           (unless (bobp)
1141             (delete-region (point-min) (point)))
1142           ;; Then treat the rest of the header lines.
1143           ;; Then we use the two regular expressions
1144           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1145           ;; select which header lines is to remain visible in the
1146           ;; article buffer.
1147           (while (re-search-forward "^[^ \t]*:" nil t)
1148             (beginning-of-line)
1149             ;; Mark the rank of the header.
1150             (put-text-property
1151              (point) (1+ (point)) 'message-rank
1152              (if (or (and visible (looking-at visible))
1153                      (and ignored
1154                           (not (looking-at ignored))))
1155                  (gnus-article-header-rank)
1156                (+ 2 max)))
1157             (forward-line 1))
1158           (message-sort-headers-1)
1159           (when (setq beg (text-property-any
1160                            (point-min) (point-max) 'message-rank (+ 2 max)))
1161             ;; We delete the unwanted headers.
1162             (push 'headers gnus-article-wash-types)
1163             (add-text-properties (point-min) (+ 5 (point-min))
1164                                  '(article-type headers dummy-invisible t))
1165             (delete-region beg (point-max))))))))
1166
1167 (defun article-hide-boring-headers (&optional arg)
1168   "Toggle hiding of headers that aren't very interesting.
1169 If given a negative prefix, always show; if given a positive prefix,
1170 always hide."
1171   (interactive (gnus-article-hidden-arg))
1172   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1173              (not gnus-show-all-headers))
1174     (save-excursion
1175       (save-restriction
1176         (let ((buffer-read-only nil)
1177               (list gnus-boring-article-headers)
1178               (inhibit-point-motion-hooks t)
1179               elem)
1180           (article-narrow-to-head)
1181           (while list
1182             (setq elem (pop list))
1183             (goto-char (point-min))
1184             (cond
1185              ;; Hide empty headers.
1186              ((eq elem 'empty)
1187               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1188                 (forward-line -1)
1189                 (gnus-article-hide-text-type
1190                  (progn (beginning-of-line) (point))
1191                  (progn
1192                    (end-of-line)
1193                    (if (re-search-forward "^[^ \t]" nil t)
1194                        (match-beginning 0)
1195                      (point-max)))
1196                  'boring-headers)))
1197              ;; Hide boring Newsgroups header.
1198              ((eq elem 'newsgroups)
1199               (when (equal (gnus-fetch-field "newsgroups")
1200                            (gnus-group-real-name
1201                             (if (boundp 'gnus-newsgroup-name)
1202                                 gnus-newsgroup-name
1203                               "")))
1204                 (gnus-article-hide-header "newsgroups")))
1205              ((eq elem 'followup-to)
1206               (when (equal (message-fetch-field "followup-to")
1207                            (message-fetch-field "newsgroups"))
1208                 (gnus-article-hide-header "followup-to")))
1209              ((eq elem 'reply-to)
1210               (let ((from (message-fetch-field "from"))
1211                     (reply-to (message-fetch-field "reply-to")))
1212                 (when (and
1213                        from reply-to
1214                        (ignore-errors
1215                          (equal
1216                           (nth 1 (mail-extract-address-components from))
1217                           (nth 1 (mail-extract-address-components reply-to)))))
1218                   (gnus-article-hide-header "reply-to"))))
1219              ((eq elem 'date)
1220               (let ((date (message-fetch-field "date")))
1221                 (when (and date
1222                            (< (days-between (current-time-string) date)
1223                               4))
1224                   (gnus-article-hide-header "date"))))
1225              ((eq elem 'long-to)
1226               (let ((to (message-fetch-field "to"))
1227                     (cc (message-fetch-field "cc")))
1228                 (when (> (length to) 1024)
1229                   (gnus-article-hide-header "to"))
1230                 (when (> (length cc) 1024)
1231                   (gnus-article-hide-header "cc"))))
1232              ((eq elem 'many-to)
1233               (let ((to-count 0)
1234                     (cc-count 0))
1235                 (goto-char (point-min))
1236                 (while (re-search-forward "^to:" nil t)
1237                   (setq to-count (1+ to-count)))
1238                 (when (> to-count 1)
1239                   (while (> to-count 0)
1240                     (goto-char (point-min))
1241                     (save-restriction
1242                       (re-search-forward "^to:" nil nil to-count)
1243                       (forward-line -1)
1244                       (narrow-to-region (point) (point-max))
1245                       (gnus-article-hide-header "to"))
1246                     (setq to-count (1- to-count))))
1247                 (goto-char (point-min))
1248                 (while (re-search-forward "^cc:" nil t)
1249                   (setq cc-count (1+ cc-count)))
1250                 (when (> cc-count 1)
1251                   (while (> cc-count 0)
1252                     (goto-char (point-min))
1253                     (save-restriction
1254                       (re-search-forward "^cc:" nil nil cc-count)
1255                       (forward-line -1)
1256                       (narrow-to-region (point) (point-max))
1257                       (gnus-article-hide-header "cc"))
1258                     (setq cc-count (1- cc-count)))))))))))))
1259
1260 (defun gnus-article-hide-header (header)
1261   (save-excursion
1262     (goto-char (point-min))
1263     (when (re-search-forward (concat "^" header ":") nil t)
1264       (gnus-article-hide-text-type
1265        (progn (beginning-of-line) (point))
1266        (progn
1267          (end-of-line)
1268          (if (re-search-forward "^[^ \t]" nil t)
1269              (match-beginning 0)
1270            (point-max)))
1271        'boring-headers))))
1272
1273 (defvar gnus-article-normalized-header-length 40
1274   "Length of normalized headers.")
1275
1276 (defun article-normalize-headers ()
1277   "Make all header lines 40 characters long."
1278   (interactive)
1279   (let ((buffer-read-only nil)
1280         column)
1281     (save-excursion
1282       (save-restriction
1283         (article-narrow-to-head)
1284         (while (not (eobp))
1285           (cond
1286            ((< (setq column (- (gnus-point-at-eol) (point)))
1287                gnus-article-normalized-header-length)
1288             (end-of-line)
1289             (insert (make-string
1290                      (- gnus-article-normalized-header-length column)
1291                      ? )))
1292            ((> column gnus-article-normalized-header-length)
1293             (gnus-put-text-property
1294              (progn
1295                (forward-char gnus-article-normalized-header-length)
1296                (point))
1297              (gnus-point-at-eol)
1298              'invisible t))
1299            (t
1300             ;; Do nothing.
1301             ))
1302           (forward-line 1))))))
1303
1304 (defun article-treat-dumbquotes ()
1305   "Translate M******** sm*rtq**t*s into proper text.
1306 Note that this function guesses whether a character is a sm*rtq**t* or
1307 not, so it should only be used interactively."
1308   (interactive)
1309   (article-translate-strings gnus-article-dumbquotes-map))
1310
1311 (defun article-translate-characters (from to)
1312   "Translate all characters in the body of the article according to FROM and TO.
1313 FROM is a string of characters to translate from; to is a string of
1314 characters to translate to."
1315   (save-excursion
1316     (when (article-goto-body)
1317       (let ((buffer-read-only nil)
1318             (x (make-string 225 ?x))
1319             (i -1))
1320         (while (< (incf i) (length x))
1321           (aset x i i))
1322         (setq i 0)
1323         (while (< i (length from))
1324           (aset x (aref from i) (aref to i))
1325           (incf i))
1326         (translate-region (point) (point-max) x)))))
1327
1328 (defun article-translate-strings (map)
1329   "Translate all string in the body of the article according to MAP.
1330 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1331   (save-excursion
1332     (when (article-goto-body)
1333       (let ((buffer-read-only nil)
1334             elem)
1335         (while (setq elem (pop map))
1336           (save-excursion
1337             (while (search-forward (car elem) nil t)
1338               (replace-match (cadr elem)))))))))
1339
1340 (defun article-treat-overstrike ()
1341   "Translate overstrikes into bold text."
1342   (interactive)
1343   (save-excursion
1344     (when (article-goto-body)
1345       (let ((buffer-read-only nil))
1346         (while (search-forward "\b" nil t)
1347           (let ((next (char-after))
1348                 (previous (char-after (- (point) 2))))
1349             ;; We do the boldification/underlining by hiding the
1350             ;; overstrikes and putting the proper text property
1351             ;; on the letters.
1352             (cond
1353              ((eq next previous)
1354               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1355               (put-text-property (point) (1+ (point)) 'face 'bold))
1356              ((eq next ?_)
1357               (gnus-article-hide-text-type
1358                (1- (point)) (1+ (point)) 'overstrike)
1359               (put-text-property
1360                (- (point) 2) (1- (point)) 'face 'underline))
1361              ((eq previous ?_)
1362               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1363               (put-text-property
1364                (point) (1+ (point)) 'face 'underline)))))))))
1365
1366 (defun article-fill-long-lines ()
1367   "Fill lines that are wider than the window width."
1368   (interactive)
1369   (save-excursion
1370     (let ((buffer-read-only nil)
1371           (width (window-width (get-buffer-window (current-buffer)))))
1372       (save-restriction
1373         (article-goto-body)
1374         (let ((adaptive-fill-mode nil))
1375           (while (not (eobp))
1376             (end-of-line)
1377             (when (>= (current-column) (min fill-column width))
1378               (narrow-to-region (point) (gnus-point-at-bol))
1379               (fill-paragraph nil)
1380               (goto-char (point-max))
1381               (widen))
1382             (forward-line 1)))))))
1383
1384 (defun article-capitalize-sentences ()
1385   "Capitalize the first word in each sentence."
1386   (interactive)
1387   (save-excursion
1388     (let ((buffer-read-only nil)
1389           (paragraph-start "^[\n\^L]"))
1390       (article-goto-body)
1391       (while (not (eobp))
1392         (capitalize-word 1)
1393         (forward-sentence)))))
1394
1395 (defun article-remove-cr ()
1396   "Remove trailing CRs and then translate remaining CRs into LFs."
1397   (interactive)
1398   (save-excursion
1399     (let ((buffer-read-only nil))
1400       (goto-char (point-min))
1401       (while (re-search-forward "\r+$" nil t)
1402         (replace-match "" t t))
1403       (goto-char (point-min))
1404       (while (search-forward "\r" nil t)
1405         (replace-match "\n" t t)))))
1406
1407 (defun article-remove-trailing-blank-lines ()
1408   "Remove all trailing blank lines from the article."
1409   (interactive)
1410   (save-excursion
1411     (let ((buffer-read-only nil))
1412       (goto-char (point-max))
1413       (delete-region
1414        (point)
1415        (progn
1416          (while (and (not (bobp))
1417                      (looking-at "^[ \t]*$")
1418                      (not (gnus-annotation-in-region-p
1419                            (point) (gnus-point-at-eol))))
1420            (forward-line -1))
1421          (forward-line 1)
1422          (point))))))
1423
1424 (defun article-display-x-face (&optional force)
1425   "Look for an X-Face header and display it if present."
1426   (interactive (list 'force))
1427   (save-excursion
1428     ;; Delete the old process, if any.
1429     (when (process-status "article-x-face")
1430       (delete-process "article-x-face"))
1431     (let ((inhibit-point-motion-hooks t)
1432           (case-fold-search t)
1433           from last)
1434       (save-restriction
1435         (article-narrow-to-head)
1436         (goto-char (point-min))
1437         (setq from (message-fetch-field "from"))
1438         (goto-char (point-min))
1439         (while (and gnus-article-x-face-command
1440                     (not last)
1441                     (or force
1442                         ;; Check whether this face is censored.
1443                         (not gnus-article-x-face-too-ugly)
1444                         (and gnus-article-x-face-too-ugly from
1445                              (not (string-match gnus-article-x-face-too-ugly
1446                                                 from))))
1447                     ;; Has to be present.
1448                     (re-search-forward "^X-Face: " nil t))
1449           ;; This used to try to do multiple faces (`while' instead of
1450           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
1451           ;; work (b) it can crash some versions of Emacs (c) are
1452           ;; multiple faces really something to encourage?
1453           (when (stringp gnus-article-x-face-command)
1454             (setq last t))
1455           ;; We now have the area of the buffer where the X-Face is stored.
1456           (save-excursion
1457             (let ((beg (point))
1458                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
1459               ;; We display the face.
1460               (if (symbolp gnus-article-x-face-command)
1461                   ;; The command is a lisp function, so we call it.
1462                   (if (gnus-functionp gnus-article-x-face-command)
1463                       (funcall gnus-article-x-face-command beg end)
1464                     (error "%s is not a function" gnus-article-x-face-command))
1465                 ;; The command is a string, so we interpret the command
1466                 ;; as a, well, command, and fork it off.
1467                 (let ((process-connection-type nil))
1468                   (process-kill-without-query
1469                    (start-process
1470                     "article-x-face" nil shell-file-name shell-command-switch
1471                     gnus-article-x-face-command))
1472                   (process-send-region "article-x-face" beg end)
1473                   (process-send-eof "article-x-face"))))))))))
1474
1475 (defun article-decode-mime-words ()
1476   "Decode all MIME-encoded words in the article."
1477   (interactive)
1478   (save-excursion
1479     (set-buffer gnus-article-buffer)
1480     (let ((inhibit-point-motion-hooks t)
1481           buffer-read-only
1482           (mail-parse-charset gnus-newsgroup-charset)
1483           (mail-parse-ignored-charsets 
1484            (save-excursion (set-buffer gnus-summary-buffer)
1485                            gnus-newsgroup-ignored-charsets)))
1486       (mail-decode-encoded-word-region (point-min) (point-max)))))
1487
1488 (defun article-decode-charset (&optional prompt)
1489   "Decode charset-encoded text in the article.
1490 If PROMPT (the prefix), prompt for a coding system to use."
1491   (interactive "P")
1492   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
1493         buffer-read-only
1494         (mail-parse-charset gnus-newsgroup-charset)
1495         (mail-parse-ignored-charsets 
1496          (save-excursion (condition-case nil
1497                              (set-buffer gnus-summary-buffer)
1498                            (error))
1499                          gnus-newsgroup-ignored-charsets))
1500         ct cte ctl charset format)
1501   (save-excursion
1502     (save-restriction
1503       (article-narrow-to-head)
1504       (setq ct (message-fetch-field "Content-Type" t)
1505             cte (message-fetch-field "Content-Transfer-Encoding" t)
1506             ctl (and ct (ignore-errors
1507                           (mail-header-parse-content-type ct)))
1508             charset (cond
1509                      (prompt
1510                       (mm-read-coding-system "Charset to decode: "))
1511                      (ctl
1512                       (mail-content-type-get ctl 'charset)))
1513             format (and ctl (mail-content-type-get ctl 'format)))
1514       (when cte
1515         (setq cte (mail-header-strip cte)))
1516       (if (and ctl (not (string-match "/" (car ctl)))) 
1517           (setq ctl nil))
1518       (goto-char (point-max)))
1519     (forward-line 1)
1520     (save-restriction
1521       (narrow-to-region (point) (point-max))
1522       (when (and (eq mail-parse-charset 'gnus-decoded)
1523                  (eq (mm-body-7-or-8) '8bit))
1524         ;; The text code could have been decoded.
1525         (setq charset mail-parse-charset))
1526       (when (and (or (not ctl)
1527                      (equal (car ctl) "text/plain"))
1528                  (not format)) ;; article with format will decode later.
1529         (mm-decode-body
1530          charset (and cte (intern (downcase
1531                                    (gnus-strip-whitespace cte))))
1532          (car ctl)))))))
1533
1534 (defun article-decode-encoded-words ()
1535   "Remove encoded-word encoding from headers."
1536   (let ((inhibit-point-motion-hooks t)
1537         (mail-parse-charset gnus-newsgroup-charset)
1538         (mail-parse-ignored-charsets 
1539          (save-excursion (condition-case nil
1540                              (set-buffer gnus-summary-buffer)
1541                            (error))
1542                          gnus-newsgroup-ignored-charsets))
1543         buffer-read-only)
1544     (save-restriction
1545       (article-narrow-to-head)
1546       (funcall gnus-decode-header-function (point-min) (point-max)))))
1547
1548 (defun article-de-quoted-unreadable (&optional force)
1549   "Translate a quoted-printable-encoded article.
1550 If FORCE, decode the article whether it is marked as quoted-printable
1551 or not."
1552   (interactive (list 'force))
1553   (save-excursion
1554     (let ((buffer-read-only nil) type charset)
1555       (if (gnus-buffer-live-p gnus-original-article-buffer)
1556           (with-current-buffer gnus-original-article-buffer
1557             (setq type
1558                   (gnus-fetch-field "content-transfer-encoding"))
1559             (let* ((ct (gnus-fetch-field "content-type"))
1560                    (ctl (and ct 
1561                              (ignore-errors
1562                                (mail-header-parse-content-type ct)))))
1563               (setq charset (and ctl
1564                                  (mail-content-type-get ctl 'charset)))
1565               (if (stringp charset)
1566                   (setq charset (intern (downcase charset)))))))
1567       (unless charset 
1568         (setq charset gnus-newsgroup-charset))
1569       (when (or force
1570                 (and type (let ((case-fold-search t))
1571                             (string-match "quoted-printable" type))))
1572         (article-goto-body)
1573         (quoted-printable-decode-region
1574          (point) (point-max) (mm-charset-to-coding-system charset))))))
1575
1576 (defun article-de-base64-unreadable (&optional force)
1577   "Translate a base64 article.
1578 If FORCE, decode the article whether it is marked as base64 not."
1579   (interactive (list 'force))
1580   (save-excursion
1581     (let ((buffer-read-only nil) type charset)
1582       (if (gnus-buffer-live-p gnus-original-article-buffer)
1583           (with-current-buffer gnus-original-article-buffer
1584             (setq type
1585                   (gnus-fetch-field "content-transfer-encoding"))
1586             (let* ((ct (gnus-fetch-field "content-type"))
1587                    (ctl (and ct 
1588                              (ignore-errors
1589                                (mail-header-parse-content-type ct)))))
1590               (setq charset (and ctl
1591                                  (mail-content-type-get ctl 'charset)))
1592               (if (stringp charset)
1593                   (setq charset (intern (downcase charset)))))))
1594       (unless charset 
1595         (setq charset gnus-newsgroup-charset))
1596       (when (or force
1597                 (and type (let ((case-fold-search t))
1598                             (string-match "base64" type))))
1599         (article-goto-body)
1600         (save-restriction
1601           (narrow-to-region (point) (point-max))
1602           (base64-decode-region (point-min) (point-max))
1603           (mm-decode-coding-region
1604            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
1605
1606 (eval-when-compile
1607   (require 'rfc1843))
1608
1609 (defun article-decode-HZ ()
1610   "Translate a HZ-encoded article."
1611   (interactive)
1612   (require 'rfc1843)
1613   (save-excursion
1614     (let ((buffer-read-only nil))
1615       (rfc1843-decode-region (point-min) (point-max)))))
1616
1617 (defun article-wash-html ()
1618   "Format an html article."
1619   (interactive)
1620   (save-excursion
1621     (let ((buffer-read-only nil)
1622           charset)
1623       (if (gnus-buffer-live-p gnus-original-article-buffer)
1624           (with-current-buffer gnus-original-article-buffer
1625             (let* ((ct (gnus-fetch-field "content-type"))
1626                    (ctl (and ct 
1627                              (ignore-errors
1628                                (mail-header-parse-content-type ct)))))
1629               (setq charset (and ctl
1630                                  (mail-content-type-get ctl 'charset)))
1631               (if (stringp charset)
1632                   (setq charset (intern (downcase charset)))))))
1633       (unless charset 
1634         (setq charset gnus-newsgroup-charset))
1635       (article-goto-body)
1636       (save-window-excursion
1637         (save-restriction
1638           (narrow-to-region (point) (point-max))
1639           (mm-setup-w3)
1640           (let ((w3-strict-width (window-width))
1641                 (url-standalone-mode t))
1642             (condition-case var
1643                 (w3-region (point-min) (point-max))
1644               (error))))))))
1645
1646 (defun article-hide-list-identifiers ()
1647   "Remove list identifies from the Subject header.
1648 The `gnus-list-identifiers' variable specifies what to do."
1649   (interactive)
1650   (save-excursion
1651     (save-restriction
1652       (let ((inhibit-point-motion-hooks t)
1653             buffer-read-only)
1654         (article-narrow-to-head)
1655         (let ((regexp (if (stringp gnus-list-identifiers) gnus-list-identifiers
1656                         (mapconcat 'identity gnus-list-identifiers " *\\|"))))
1657           (when regexp
1658             (goto-char (point-min))
1659             (when (re-search-forward
1660                    (concat "^Subject: +\\(\\(\\(Re: +\\)?\\(" regexp 
1661                            " *\\)\\)+\\(Re: +\\)?\\)")
1662                    nil t)
1663               (let ((s (or (match-string 3) (match-string 5))))
1664                 (delete-region (match-beginning 1) (match-end 1))
1665                 (when s
1666                   (goto-char (match-beginning 1))
1667                   (insert s))))))))))
1668
1669 (defun article-hide-pgp ()
1670   "Remove any PGP headers and signatures in the current article."
1671   (interactive)
1672   (save-excursion
1673     (save-restriction
1674       (let ((inhibit-point-motion-hooks t)
1675             buffer-read-only beg end)
1676         (article-goto-body)
1677         ;; Hide the "header".
1678         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
1679           (push 'pgp gnus-article-wash-types)
1680           (delete-region (match-beginning 0) (match-end 0))
1681           ;; Remove armor headers (rfc2440 6.2)
1682           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
1683                                      (point)))
1684           (setq beg (point))
1685           ;; Hide the actual signature.
1686           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
1687                (setq end (1+ (match-beginning 0)))
1688                (delete-region
1689                 end
1690                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
1691                     (match-end 0)
1692                   ;; Perhaps we shouldn't hide to the end of the buffer
1693                   ;; if there is no end to the signature?
1694                   (point-max))))
1695           ;; Hide "- " PGP quotation markers.
1696           (when (and beg end)
1697             (narrow-to-region beg end)
1698             (goto-char (point-min))
1699             (while (re-search-forward "^- " nil t)
1700               (delete-region
1701                (match-beginning 0) (match-end 0)))
1702             (widen))
1703           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1704
1705 (defun article-hide-pem (&optional arg)
1706   "Toggle hiding of any PEM headers and signatures in the current article.
1707 If given a negative prefix, always show; if given a positive prefix,
1708 always hide."
1709   (interactive (gnus-article-hidden-arg))
1710   (unless (gnus-article-check-hidden-text 'pem arg)
1711     (save-excursion
1712       (let (buffer-read-only end)
1713         (goto-char (point-min))
1714         ;; Hide the horrendously ugly "header".
1715         (when (and (search-forward
1716                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1717                     nil t)
1718                    (setq end (1+ (match-beginning 0))))
1719           (push 'pem gnus-article-wash-types)
1720           (gnus-article-hide-text-type
1721            end
1722            (if (search-forward "\n\n" nil t)
1723                (match-end 0)
1724              (point-max))
1725            'pem)
1726           ;; Hide the trailer as well
1727           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1728                                 nil t)
1729             (gnus-article-hide-text-type
1730              (match-beginning 0) (match-end 0) 'pem)))))))
1731
1732 (defun article-strip-banner ()
1733   "Strip the banner specified by the `banner' group parameter."
1734   (interactive)
1735   (save-excursion
1736     (save-restriction
1737       (let ((inhibit-point-motion-hooks t)
1738             (banner (gnus-group-find-parameter gnus-newsgroup-name 'banner))
1739             (gnus-signature-limit nil)
1740             buffer-read-only beg end)
1741         (when banner
1742           (article-goto-body)
1743           (cond
1744            ((eq banner 'signature)
1745             (when (gnus-article-narrow-to-signature)
1746               (widen)
1747               (forward-line -1)
1748               (delete-region (point) (point-max))))
1749            ((symbolp banner)
1750             (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
1751                 (while (re-search-forward banner nil t)
1752                   (delete-region (match-beginning 0) (match-end 0)))))
1753            ((stringp banner)
1754             (while (re-search-forward banner nil t)
1755               (delete-region (match-beginning 0) (match-end 0))))))))))
1756
1757 (defun article-babel ()
1758   "Translate article using an online translation service."
1759   (interactive)
1760   (require 'babel)
1761   (save-excursion
1762     (set-buffer gnus-article-buffer)
1763     (when (article-goto-body)
1764       (let* ((buffer-read-only nil)
1765              (start (point))
1766              (end (point-max))
1767              (orig (buffer-substring start end))
1768              (trans (babel-as-string orig)))
1769         (save-restriction
1770           (narrow-to-region start end)
1771           (delete-region start end)
1772           (insert trans))))))
1773
1774 (defun article-hide-signature (&optional arg)
1775   "Hide the signature in the current article.
1776 If given a negative prefix, always show; if given a positive prefix,
1777 always hide."
1778   (interactive (gnus-article-hidden-arg))
1779   (unless (gnus-article-check-hidden-text 'signature arg)
1780     (save-excursion
1781       (save-restriction
1782         (let ((buffer-read-only nil))
1783           (when (gnus-article-narrow-to-signature)
1784             (gnus-article-hide-text-type
1785              (point-min) (point-max) 'signature)))))))
1786
1787 (defun article-strip-headers-in-body ()
1788   "Strip offensive headers from bodies."
1789   (interactive)
1790   (save-excursion
1791     (article-goto-body)
1792     (let ((case-fold-search t))
1793       (when (looking-at "x-no-archive:")
1794         (gnus-delete-line)))))
1795
1796 (defun article-strip-leading-blank-lines ()
1797   "Remove all blank lines from the beginning of the article."
1798   (interactive)
1799   (save-excursion
1800     (let ((inhibit-point-motion-hooks t)
1801           buffer-read-only)
1802       (when (article-goto-body)
1803         (while (and (not (eobp))
1804                     (looking-at "[ \t]*$"))
1805           (gnus-delete-line))))))
1806
1807 (defun article-narrow-to-head ()
1808   "Narrow the buffer to the head of the message.
1809 Point is left at the beginning of the narrowed-to region."
1810   (narrow-to-region
1811    (goto-char (point-min))
1812    (if (search-forward "\n\n" nil 1)
1813        (1- (point))
1814      (point-max)))
1815   (goto-char (point-min)))
1816
1817 (defun article-goto-body ()
1818   "Place point at the start of the body."
1819   (goto-char (point-min))
1820   (cond
1821    ;; This variable is only bound when dealing with separate
1822    ;; MIME body parts.
1823    (article-goto-body-goes-to-point-min-p
1824     t)
1825    ((search-forward "\n\n" nil t)
1826     t)
1827    (t
1828     (goto-char (point-max))
1829     nil)))
1830
1831 (defun article-strip-multiple-blank-lines ()
1832   "Replace consecutive blank lines with one empty line."
1833   (interactive)
1834   (save-excursion
1835     (let ((inhibit-point-motion-hooks t)
1836           buffer-read-only)
1837       ;; First make all blank lines empty.
1838       (article-goto-body)
1839       (while (re-search-forward "^[ \t]+$" nil t)
1840         (unless (gnus-annotation-in-region-p
1841                  (match-beginning 0) (match-end 0))
1842           (replace-match "" nil t)))
1843       ;; Then replace multiple empty lines with a single empty line.
1844       (article-goto-body)
1845       (while (re-search-forward "\n\n\n+" nil t)
1846         (unless (gnus-annotation-in-region-p
1847                  (match-beginning 0) (match-end 0))
1848           (replace-match "\n\n" t t))))))
1849
1850 (defun article-strip-leading-space ()
1851   "Remove all white space from the beginning of the lines in the article."
1852   (interactive)
1853   (save-excursion
1854     (let ((inhibit-point-motion-hooks t)
1855           buffer-read-only)
1856       (article-goto-body)
1857       (while (re-search-forward "^[ \t]+" nil t)
1858         (replace-match "" t t)))))
1859
1860 (defun article-strip-trailing-space ()
1861   "Remove all white space from the end of the lines in the article."
1862   (interactive)
1863   (save-excursion
1864     (let ((inhibit-point-motion-hooks t)
1865           buffer-read-only)
1866       (article-goto-body)
1867       (while (re-search-forward "[ \t]+$" nil t)
1868         (replace-match "" t t)))))
1869
1870 (defun article-strip-blank-lines ()
1871   "Strip leading, trailing and multiple blank lines."
1872   (interactive)
1873   (article-strip-leading-blank-lines)
1874   (article-remove-trailing-blank-lines)
1875   (article-strip-multiple-blank-lines))
1876
1877 (defun article-strip-all-blank-lines ()
1878   "Strip all blank lines."
1879   (interactive)
1880   (save-excursion
1881     (let ((inhibit-point-motion-hooks t)
1882           buffer-read-only)
1883       (article-goto-body)
1884       (while (re-search-forward "^[ \t]*\n" nil t)
1885         (replace-match "" t t)))))
1886
1887 (defun gnus-article-narrow-to-signature ()
1888   "Narrow to the signature; return t if a signature is found, else nil."
1889   (let ((inhibit-point-motion-hooks t))
1890     (when (gnus-article-search-signature)
1891       (forward-line 1)
1892       ;; Check whether we have some limits to what we consider
1893       ;; to be a signature.
1894       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1895                       (list gnus-signature-limit)))
1896             limit limited)
1897         (while (setq limit (pop limits))
1898           (if (or (and (integerp limit)
1899                        (< (- (point-max) (point)) limit))
1900                   (and (floatp limit)
1901                        (< (count-lines (point) (point-max)) limit))
1902                   (and (gnus-functionp limit)
1903                        (funcall limit))
1904                   (and (stringp limit)
1905                        (not (re-search-forward limit nil t))))
1906               ()                        ; This limit did not succeed.
1907             (setq limited t
1908                   limits nil)))
1909         (unless limited
1910           (narrow-to-region (point) (point-max))
1911           t)))))
1912
1913 (defun gnus-article-search-signature ()
1914   "Search the current buffer for the signature separator.
1915 Put point at the beginning of the signature separator."
1916   (let ((cur (point)))
1917     (goto-char (point-max))
1918     (if (if (stringp gnus-signature-separator)
1919             (re-search-backward gnus-signature-separator nil t)
1920           (let ((seps gnus-signature-separator))
1921             (while (and seps
1922                         (not (re-search-backward (car seps) nil t)))
1923               (pop seps))
1924             seps))
1925         t
1926       (goto-char cur)
1927       nil)))
1928
1929 (defun gnus-article-hidden-arg ()
1930   "Return the current prefix arg as a number, or 0 if no prefix."
1931   (list (if current-prefix-arg
1932             (prefix-numeric-value current-prefix-arg)
1933           0)))
1934
1935 (defun gnus-article-check-hidden-text (type arg)
1936   "Return nil if hiding is necessary.
1937 Arg can be nil or a number.  Nil and positive means hide, negative
1938 means show, 0 means toggle."
1939   (save-excursion
1940     (save-restriction
1941       (let ((hide (gnus-article-hidden-text-p type)))
1942         (cond
1943          ((or (null arg)
1944               (> arg 0))
1945           nil)
1946          ((< arg 0)
1947           (gnus-article-show-hidden-text type))
1948          (t
1949           (if (eq hide 'hidden)
1950               (gnus-article-show-hidden-text type)
1951             nil)))))))
1952
1953 (defun gnus-article-hidden-text-p (type)
1954   "Say whether the current buffer contains hidden text of type TYPE."
1955   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1956     (while (and pos
1957                 (not (get-text-property pos 'invisible))
1958                 (not (get-text-property pos 'dummy-invisible)))
1959       (setq pos
1960             (text-property-any (1+ pos) (point-max) 'article-type type)))
1961     (if pos
1962         'hidden
1963       nil)))
1964
1965 (defun gnus-article-show-hidden-text (type &optional hide)
1966   "Show all hidden text of type TYPE.
1967 If HIDE, hide the text instead."
1968   (save-excursion
1969     (let ((buffer-read-only nil)
1970           (inhibit-point-motion-hooks t)
1971           (end (point-min))
1972           beg)
1973       (while (setq beg (text-property-any end (point-max) 'article-type type))
1974         (goto-char beg)
1975         (setq end (or
1976                    (text-property-not-all beg (point-max) 'article-type type)
1977                    (point-max)))
1978         (if hide
1979             (gnus-article-hide-text beg end gnus-hidden-properties)
1980           (gnus-article-unhide-text beg end))
1981         (goto-char end))
1982       t)))
1983
1984 (defconst article-time-units
1985   `((year . ,(* 365.25 24 60 60))
1986     (week . ,(* 7 24 60 60))
1987     (day . ,(* 24 60 60))
1988     (hour . ,(* 60 60))
1989     (minute . 60)
1990     (second . 1))
1991   "Mapping from time units to seconds.")
1992
1993 (defun article-date-ut (&optional type highlight header)
1994   "Convert DATE date to universal time in the current article.
1995 If TYPE is `local', convert to local time; if it is `lapsed', output
1996 how much time has lapsed since DATE.  For `lapsed', the value of
1997 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
1998 should replace the \"Date:\" one, or should be added below it."
1999   (interactive (list 'ut t))
2000   (let* ((header (or header
2001                      (message-fetch-field "date")
2002                      ""))
2003          (tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
2004          (date-regexp
2005           (cond
2006            ((not gnus-article-date-lapsed-new-header)
2007             tdate-regexp)
2008            ((eq type 'lapsed)
2009             "^X-Sent:[ \t]")
2010            (t
2011             "^Date:[ \t]")))
2012          (date (if (vectorp header) (mail-header-date header)
2013                  header))
2014          (inhibit-point-motion-hooks t)
2015          pos
2016          bface eface)
2017     (save-excursion
2018       (save-restriction
2019         (article-narrow-to-head)
2020         (when (re-search-forward tdate-regexp nil t)
2021           (setq bface (get-text-property (gnus-point-at-bol) 'face)
2022                 date (or (get-text-property (gnus-point-at-bol)
2023                                             'original-date)
2024                          date)
2025                 eface (get-text-property (1- (gnus-point-at-eol)) 'face))
2026           (forward-line 1))
2027         (when (and date (not (string= date "")))
2028           (goto-char (point-min))
2029           (let ((buffer-read-only nil))
2030             ;; Delete any old Date headers.
2031             (while (re-search-forward date-regexp nil t)
2032               (if pos
2033                   (delete-region (progn (beginning-of-line) (point))
2034                                  (progn (forward-line 1) (point)))
2035                 (delete-region (progn (beginning-of-line) (point))
2036                                (progn (end-of-line) (point)))
2037                 (setq pos (point))))
2038             (when (and (not pos) (re-search-forward tdate-regexp nil t))
2039               (forward-line 1))
2040             (if pos (goto-char pos))
2041             (insert (article-make-date-line date (or type 'ut)))
2042             (when (not pos)
2043               (insert "\n")
2044               (forward-line -1))
2045             ;; Do highlighting.
2046             (beginning-of-line)
2047             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2048               (put-text-property (match-beginning 1) (1+ (match-end 1))
2049                                  'original-date date)
2050               (put-text-property (match-beginning 1) (1+ (match-end 1))
2051                                  'face bface)
2052               (put-text-property (match-beginning 2) (match-end 2)
2053                                  'face eface))))))))
2054
2055 (defun article-make-date-line (date type)
2056   "Return a DATE line of TYPE."
2057   (let ((time (condition-case ()
2058                   (date-to-time date)
2059                 (error '(0 0)))))
2060     (cond
2061      ;; Convert to the local timezone.  We have to slap a
2062      ;; `condition-case' round the calls to the timezone
2063      ;; functions since they aren't particularly resistant to
2064      ;; buggy dates.
2065      ((eq type 'local)
2066       (let ((tz (car (current-time-zone time))))
2067         (format "Date: %s %s%02d%02d" (current-time-string time)
2068                 (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
2069                 (/ (% (abs tz) 3600) 60))))
2070      ;; Convert to Universal Time.
2071      ((eq type 'ut)
2072       (concat "Date: "
2073               (current-time-string
2074                (let* ((e (parse-time-string date))
2075                       (tm (apply 'encode-time e))
2076                       (ms (car tm))
2077                       (ls (- (cadr tm) (car (current-time-zone time)))))
2078                  (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2079                        ((> ls 65535) (list (1+ ms) (- ls 65536)))
2080                        (t (list ms ls)))))
2081               " UT"))
2082      ;; Get the original date from the article.
2083      ((eq type 'original)
2084       (concat "Date: " (if (string-match "\n+$" date)
2085                            (substring date 0 (match-beginning 0))
2086                          date)))
2087      ;; Let the user define the format.
2088      ((eq type 'user)
2089       (if (gnus-functionp gnus-article-time-format)
2090           (funcall gnus-article-time-format time)
2091         (concat
2092          "Date: "
2093          (format-time-string gnus-article-time-format time))))
2094      ;; ISO 8601.
2095      ((eq type 'iso8601)
2096       (let ((tz (car (current-time-zone time))))
2097         (concat
2098          "Date: "
2099          (format-time-string "%Y%m%dT%H%M%S" time)
2100          (format "%s%02d%02d"
2101                  (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
2102                  (/ (% (abs tz) 3600) 60)))))
2103      ;; Do an X-Sent lapsed format.
2104      ((eq type 'lapsed)
2105       ;; If the date is seriously mangled, the timezone functions are
2106       ;; liable to bug out, so we ignore all errors.
2107       (let* ((now (current-time))
2108              (real-time (subtract-time now time))
2109              (real-sec (and real-time
2110                             (+ (* (float (car real-time)) 65536)
2111                                (cadr real-time))))
2112              (sec (and real-time (abs real-sec)))
2113              num prev)
2114         (cond
2115          ((null real-time)
2116           "X-Sent: Unknown")
2117          ((zerop sec)
2118           "X-Sent: Now")
2119          (t
2120           (concat
2121            "X-Sent: "
2122            ;; This is a bit convoluted, but basically we go
2123            ;; through the time units for years, weeks, etc,
2124            ;; and divide things to see whether that results
2125            ;; in positive answers.
2126            (mapconcat
2127             (lambda (unit)
2128               (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2129                   ;; The (remaining) seconds are too few to
2130                   ;; be divided into this time unit.
2131                   ""
2132                 ;; It's big enough, so we output it.
2133                 (setq sec (- sec (* num (cdr unit))))
2134                 (prog1
2135                     (concat (if prev ", " "") (int-to-string
2136                                                (floor num))
2137                             " " (symbol-name (car unit))
2138                             (if (> num 1) "s" ""))
2139                   (setq prev t))))
2140             article-time-units "")
2141            ;; If dates are odd, then it might appear like the
2142            ;; article was sent in the future.
2143            (if (> real-sec 0)
2144                " ago"
2145              " in the future"))))))
2146      (t
2147       (error "Unknown conversion type: %s" type)))))
2148
2149 (defun article-date-local (&optional highlight)
2150   "Convert the current article date to the local timezone."
2151   (interactive (list t))
2152   (article-date-ut 'local highlight))
2153
2154 (defun article-date-original (&optional highlight)
2155   "Convert the current article date to what it was originally.
2156 This is only useful if you have used some other date conversion
2157 function and want to see what the date was before converting."
2158   (interactive (list t))
2159   (article-date-ut 'original highlight))
2160
2161 (defun article-date-lapsed (&optional highlight)
2162   "Convert the current article date to time lapsed since it was sent."
2163   (interactive (list t))
2164   (article-date-ut 'lapsed highlight))
2165
2166 (defun article-update-date-lapsed ()
2167   "Function to be run from a timer to update the lapsed time line."
2168   (let (deactivate-mark)
2169     (save-excursion
2170       (ignore-errors
2171         (walk-windows
2172          (lambda (w)
2173            (set-buffer (window-buffer w))
2174            (when (eq major-mode 'gnus-article-mode)
2175              (goto-char (point-min))
2176              (when (re-search-forward "^X-Sent:" nil t)
2177                (article-date-lapsed t))))
2178          nil 'visible)))))
2179
2180 (defun gnus-start-date-timer (&optional n)
2181   "Start a timer to update the X-Sent header in the article buffers.
2182 The numerical prefix says how frequently (in seconds) the function
2183 is to run."
2184   (interactive "p")
2185   (unless n
2186     (setq n 1))
2187   (gnus-stop-date-timer)
2188   (setq article-lapsed-timer
2189         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2190
2191 (defun gnus-stop-date-timer ()
2192   "Stop the X-Sent timer."
2193   (interactive)
2194   (when article-lapsed-timer
2195     (nnheader-cancel-timer article-lapsed-timer)
2196     (setq article-lapsed-timer nil)))
2197
2198 (defun article-date-user (&optional highlight)
2199   "Convert the current article date to the user-defined format.
2200 This format is defined by the `gnus-article-time-format' variable."
2201   (interactive (list t))
2202   (article-date-ut 'user highlight))
2203
2204 (defun article-date-iso8601 (&optional highlight)
2205   "Convert the current article date to ISO8601."
2206   (interactive (list t))
2207   (article-date-ut 'iso8601 highlight))
2208
2209 (defun article-show-all ()
2210   "Show all hidden text in the article buffer."
2211   (interactive)
2212   (save-excursion
2213     (let ((buffer-read-only nil))
2214       (gnus-article-unhide-text (point-min) (point-max)))))
2215
2216 (defun article-emphasize (&optional arg)
2217   "Emphasize text according to `gnus-emphasis-alist'."
2218   (interactive (gnus-article-hidden-arg))
2219   (unless (gnus-article-check-hidden-text 'emphasis arg)
2220     (save-excursion
2221       (let ((alist (or 
2222                     (condition-case nil
2223                         (with-current-buffer gnus-summary-buffer 
2224                           gnus-article-emphasis-alist) 
2225                       (error))
2226                     gnus-emphasis-alist))
2227             (buffer-read-only nil)
2228             (props (append '(article-type emphasis)
2229                            gnus-hidden-properties))
2230             regexp elem beg invisible visible face)
2231         (article-goto-body)
2232         (setq beg (point))
2233         (while (setq elem (pop alist))
2234           (goto-char beg)
2235           (setq regexp (car elem)
2236                 invisible (nth 1 elem)
2237                 visible (nth 2 elem)
2238                 face (nth 3 elem))
2239           (while (re-search-forward regexp nil t)
2240             (when (and (match-beginning visible) (match-beginning invisible))
2241               (push 'emphasis gnus-article-wash-types)
2242               (gnus-article-hide-text
2243                (match-beginning invisible) (match-end invisible) props)
2244               (gnus-article-unhide-text-type
2245                (match-beginning visible) (match-end visible) 'emphasis)
2246               (gnus-put-text-property-excluding-newlines
2247                (match-beginning visible) (match-end visible) 'face face)
2248               (goto-char (match-end invisible)))))))))
2249
2250 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2251   "Setup newsgroup emphasis alist."
2252   (unless gnus-article-emphasis-alist
2253     (let ((name (and gnus-newsgroup-name
2254                      (gnus-group-real-name gnus-newsgroup-name))))
2255       (make-local-variable 'gnus-article-emphasis-alist)
2256       (setq gnus-article-emphasis-alist 
2257             (nconc 
2258              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2259                (while (setq elem (pop alist))
2260                  (when (and name (string-match (car elem) name))
2261                    (setq alist nil
2262                          highlight (copy-sequence (cdr elem)))))
2263                highlight)
2264              (copy-sequence highlight-words)
2265              (if gnus-newsgroup-name
2266                  (copy-sequence (gnus-group-find-parameter 
2267                                  gnus-newsgroup-name 'highlight-words t)))
2268              gnus-emphasis-alist)))))
2269
2270 (defvar gnus-summary-article-menu)
2271 (defvar gnus-summary-post-menu)
2272
2273 ;;; Saving functions.
2274
2275 (defun gnus-article-save (save-buffer file &optional num)
2276   "Save the currently selected article."
2277   (unless gnus-save-all-headers
2278     ;; Remove headers according to `gnus-saved-headers'.
2279     (let ((gnus-visible-headers
2280            (or gnus-saved-headers gnus-visible-headers))
2281           (gnus-article-buffer save-buffer))
2282       (save-excursion
2283         (set-buffer save-buffer)
2284         (article-hide-headers 1 t))))
2285   (save-window-excursion
2286     (if (not gnus-default-article-saver)
2287         (error "No default saver is defined")
2288       ;; !!! Magic!  The saving functions all save
2289       ;; `gnus-save-article-buffer' (or so they think), but we
2290       ;; bind that variable to our save-buffer.
2291       (set-buffer gnus-article-buffer)
2292       (let* ((gnus-save-article-buffer save-buffer)
2293              (filename
2294               (cond
2295                ((not gnus-prompt-before-saving) 'default)
2296                ((eq gnus-prompt-before-saving 'always) nil)
2297                (t file)))
2298              (gnus-number-of-articles-to-be-saved
2299               (when (eq gnus-prompt-before-saving t)
2300                 num)))                  ; Magic
2301         (set-buffer gnus-article-current-summary)
2302         (funcall gnus-default-article-saver filename)))))
2303
2304 (defun gnus-read-save-file-name (prompt &optional filename
2305                                         function group headers variable)
2306   (let ((default-name
2307           (funcall function group headers (symbol-value variable)))
2308         result)
2309     (setq result
2310           (expand-file-name
2311            (cond
2312             ((eq filename 'default)
2313              default-name)
2314             ((eq filename t)
2315              default-name)
2316             (filename filename)
2317             (t
2318              (let* ((split-name (gnus-get-split-value gnus-split-methods))
2319                     (prompt
2320                      (format prompt
2321                              (if (and gnus-number-of-articles-to-be-saved
2322                                       (> gnus-number-of-articles-to-be-saved 1))
2323                                  (format "these %d articles"
2324                                          gnus-number-of-articles-to-be-saved)
2325                                "this article")))
2326                     (file
2327                      ;; Let the split methods have their say.
2328                      (cond
2329                       ;; No split name was found.
2330                       ((null split-name)
2331                        (read-file-name
2332                         (concat prompt " (default "
2333                                 (file-name-nondirectory default-name) ") ")
2334                         (file-name-directory default-name)
2335                         default-name))
2336                       ;; A single group name is returned.
2337                       ((stringp split-name)
2338                        (setq default-name
2339                              (funcall function split-name headers
2340                                       (symbol-value variable)))
2341                        (read-file-name
2342                         (concat prompt " (default "
2343                                 (file-name-nondirectory default-name) ") ")
2344                         (file-name-directory default-name)
2345                         default-name))
2346                       ;; A single split name was found
2347                       ((= 1 (length split-name))
2348                        (let* ((name (expand-file-name
2349                                      (car split-name)
2350                                      gnus-article-save-directory))
2351                               (dir (cond ((file-directory-p name)
2352                                           (file-name-as-directory name))
2353                                          ((file-exists-p name) name)
2354                                          (t gnus-article-save-directory))))
2355                          (read-file-name
2356                           (concat prompt " (default " name ") ")
2357                           dir name)))
2358                       ;; A list of splits was found.
2359                       (t
2360                        (setq split-name (nreverse split-name))
2361                        (let (result)
2362                          (let ((file-name-history
2363                                 (nconc split-name file-name-history)))
2364                            (setq result
2365                                  (expand-file-name
2366                                   (read-file-name
2367                                    (concat prompt " (`M-p' for defaults) ")
2368                                    gnus-article-save-directory
2369                                    (car split-name))
2370                                   gnus-article-save-directory)))
2371                          (car (push result file-name-history)))))))
2372                ;; Create the directory.
2373                (gnus-make-directory (file-name-directory file))
2374       ;; If we have read a directory, we append the default file name.
2375                (when (file-directory-p file)
2376                  (setq file (expand-file-name (file-name-nondirectory
2377                                                default-name)
2378                                               (file-name-as-directory file))))
2379                ;; Possibly translate some characters.
2380                (nnheader-translate-file-chars file))))))
2381     (gnus-make-directory (file-name-directory result))
2382     (set variable result)))
2383
2384 (defun gnus-article-archive-name (group)
2385   "Return the first instance of an \"Archive-name\" in the current buffer."
2386   (let ((case-fold-search t))
2387     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2388       (nnheader-concat gnus-article-save-directory
2389                        (match-string 1)))))
2390
2391 (defun gnus-article-nndoc-name (group)
2392   "If GROUP is an nndoc group, return the name of the parent group."
2393   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2394     (gnus-group-get-parameter group 'save-article-group)))
2395
2396 (defun gnus-summary-save-in-rmail (&optional filename)
2397   "Append this article to Rmail file.
2398 Optional argument FILENAME specifies file name.
2399 Directory to save to is default to `gnus-article-save-directory'."
2400   (setq filename (gnus-read-save-file-name
2401                   "Save %s in rmail file:" filename
2402                   gnus-rmail-save-name gnus-newsgroup-name
2403                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2404   (gnus-eval-in-buffer-window gnus-save-article-buffer
2405     (save-excursion
2406       (save-restriction
2407         (widen)
2408         (gnus-output-to-rmail filename))))
2409   filename)
2410
2411 (defun gnus-summary-save-in-mail (&optional filename)
2412   "Append this article to Unix mail file.
2413 Optional argument FILENAME specifies file name.
2414 Directory to save to is default to `gnus-article-save-directory'."
2415   (setq filename (gnus-read-save-file-name
2416                   "Save %s in Unix mail file:" filename
2417                   gnus-mail-save-name gnus-newsgroup-name
2418                   gnus-current-headers 'gnus-newsgroup-last-mail))
2419   (gnus-eval-in-buffer-window gnus-save-article-buffer
2420     (save-excursion
2421       (save-restriction
2422         (widen)
2423         (if (and (file-readable-p filename)
2424                  (mail-file-babyl-p filename))
2425             (rmail-output-to-rmail-file filename t)
2426           (gnus-output-to-mail filename)))))
2427   filename)
2428
2429 (defun gnus-summary-save-in-file (&optional filename overwrite)
2430   "Append this article to file.
2431 Optional argument FILENAME specifies file name.
2432 Directory to save to is default to `gnus-article-save-directory'."
2433   (setq filename (gnus-read-save-file-name
2434                   "Save %s in file:" filename
2435                   gnus-file-save-name gnus-newsgroup-name
2436                   gnus-current-headers 'gnus-newsgroup-last-file))
2437   (gnus-eval-in-buffer-window gnus-save-article-buffer
2438     (save-excursion
2439       (save-restriction
2440         (widen)
2441         (when (and overwrite
2442                    (file-exists-p filename))
2443           (delete-file filename))
2444         (gnus-output-to-file filename))))
2445   filename)
2446
2447 (defun gnus-summary-write-to-file (&optional filename)
2448   "Write this article to a file.
2449 Optional argument FILENAME specifies file name.
2450 The directory to save in defaults to `gnus-article-save-directory'."
2451   (gnus-summary-save-in-file nil t))
2452
2453 (defun gnus-summary-save-body-in-file (&optional filename)
2454   "Append this article body to a file.
2455 Optional argument FILENAME specifies file name.
2456 The directory to save in defaults to `gnus-article-save-directory'."
2457   (setq filename (gnus-read-save-file-name
2458                   "Save %s body in file:" filename
2459                   gnus-file-save-name gnus-newsgroup-name
2460                   gnus-current-headers 'gnus-newsgroup-last-file))
2461   (gnus-eval-in-buffer-window gnus-save-article-buffer
2462     (save-excursion
2463       (save-restriction
2464         (widen)
2465         (when (article-goto-body)
2466           (narrow-to-region (point) (point-max)))
2467         (gnus-output-to-file filename))))
2468   filename)
2469
2470 (defun gnus-summary-save-in-pipe (&optional command)
2471   "Pipe this article to subprocess."
2472   (setq command
2473         (cond ((and (eq command 'default)
2474                     gnus-last-shell-command)
2475                gnus-last-shell-command)
2476               (command command)
2477               (t (read-string
2478                   (format
2479                    "Shell command on %s: "
2480                    (if (and gnus-number-of-articles-to-be-saved
2481                             (> gnus-number-of-articles-to-be-saved 1))
2482                        (format "these %d articles"
2483                                gnus-number-of-articles-to-be-saved)
2484                      "this article"))
2485                   gnus-last-shell-command))))
2486   (when (string-equal command "")
2487     (setq command gnus-last-shell-command))
2488   (gnus-eval-in-buffer-window gnus-article-buffer
2489     (save-restriction
2490       (widen)
2491       (shell-command-on-region (point-min) (point-max) command nil)))
2492   (setq gnus-last-shell-command command))
2493
2494 ;;; Article file names when saving.
2495
2496 (defun gnus-capitalize-newsgroup (newsgroup)
2497   "Capitalize NEWSGROUP name."
2498   (when (not (zerop (length newsgroup)))
2499     (concat (char-to-string (upcase (aref newsgroup 0)))
2500             (substring newsgroup 1))))
2501
2502 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2503   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2504 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2505 Otherwise, it is like ~/News/news/group/num."
2506   (let ((default
2507           (expand-file-name
2508            (concat (if (gnus-use-long-file-name 'not-save)
2509                        (gnus-capitalize-newsgroup newsgroup)
2510                      (gnus-newsgroup-directory-form newsgroup))
2511                    "/" (int-to-string (mail-header-number headers)))
2512            gnus-article-save-directory)))
2513     (if (and last-file
2514              (string-equal (file-name-directory default)
2515                            (file-name-directory last-file))
2516              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2517         default
2518       (or last-file default))))
2519
2520 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2521   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2522 If variable `gnus-use-long-file-name' is non-nil, it is
2523 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2524   (let ((default
2525           (expand-file-name
2526            (concat (if (gnus-use-long-file-name 'not-save)
2527                        newsgroup
2528                      (gnus-newsgroup-directory-form newsgroup))
2529                    "/" (int-to-string (mail-header-number headers)))
2530            gnus-article-save-directory)))
2531     (if (and last-file
2532              (string-equal (file-name-directory default)
2533                            (file-name-directory last-file))
2534              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2535         default
2536       (or last-file default))))
2537
2538 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2539   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2540 If variable `gnus-use-long-file-name' is non-nil, it is
2541 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2542   (or last-file
2543       (expand-file-name
2544        (if (gnus-use-long-file-name 'not-save)
2545            newsgroup
2546          (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup)))
2547        gnus-article-save-directory)))
2548
2549 (eval-and-compile
2550   (mapcar
2551    (lambda (func)
2552      (let (afunc gfunc)
2553        (if (consp func)
2554            (setq afunc (car func)
2555                  gfunc (cdr func))
2556          (setq afunc func
2557                gfunc (intern (format "gnus-%s" func))))
2558        (defalias gfunc
2559          (if (fboundp afunc)
2560            `(lambda (&optional interactive &rest args)
2561               ,(documentation afunc t)
2562               (interactive (list t))
2563               (save-excursion
2564                 (set-buffer gnus-article-buffer)
2565                 (if interactive
2566                     (call-interactively ',afunc)
2567                   (apply ',afunc args))))))))
2568    '(article-hide-headers
2569      article-hide-boring-headers
2570      article-treat-overstrike
2571      article-fill-long-lines
2572      article-capitalize-sentences
2573      article-remove-cr
2574      article-display-x-face
2575      article-de-quoted-unreadable
2576      article-de-base64-unreadable
2577      article-decode-HZ
2578      article-wash-html
2579      article-hide-list-identifiers
2580      article-hide-pgp
2581      article-strip-banner
2582      article-babel
2583      article-hide-pem
2584      article-hide-signature
2585      article-strip-headers-in-body
2586      article-remove-trailing-blank-lines
2587      article-strip-leading-blank-lines
2588      article-strip-multiple-blank-lines
2589      article-strip-leading-space
2590      article-strip-trailing-space
2591      article-strip-blank-lines
2592      article-strip-all-blank-lines
2593      article-date-local
2594      article-date-iso8601
2595      article-date-original
2596      article-date-ut
2597      article-decode-mime-words
2598      article-decode-charset
2599      article-decode-encoded-words
2600      article-date-user
2601      article-date-lapsed
2602      article-emphasize
2603      article-treat-dumbquotes
2604      article-normalize-headers
2605      (article-show-all . gnus-article-show-all-headers))))
2606 \f
2607 ;;;
2608 ;;; Gnus article mode
2609 ;;;
2610
2611 (put 'gnus-article-mode 'mode-class 'special)
2612
2613 (set-keymap-parent gnus-article-mode-map widget-keymap)
2614
2615 (gnus-define-keys gnus-article-mode-map
2616   " " gnus-article-goto-next-page
2617   "\177" gnus-article-goto-prev-page
2618   [delete] gnus-article-goto-prev-page
2619   [backspace] gnus-article-goto-prev-page
2620   "\C-c^" gnus-article-refer-article
2621   "h" gnus-article-show-summary
2622   "s" gnus-article-show-summary
2623   "\C-c\C-m" gnus-article-mail
2624   "?" gnus-article-describe-briefly
2625   "e" gnus-summary-edit-article
2626   "<" beginning-of-buffer
2627   ">" end-of-buffer
2628   "\C-c\C-i" gnus-info-find-node
2629   "\C-c\C-b" gnus-bug
2630
2631   "\C-d" gnus-article-read-summary-keys
2632   "\M-*" gnus-article-read-summary-keys
2633   "\M-#" gnus-article-read-summary-keys
2634   "\M-^" gnus-article-read-summary-keys
2635   "\M-g" gnus-article-read-summary-keys)
2636
2637 (substitute-key-definition
2638  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
2639
2640 (defun gnus-article-make-menu-bar ()
2641   (gnus-turn-off-edit-menu 'article)
2642   (unless (boundp 'gnus-article-article-menu)
2643     (easy-menu-define
2644      gnus-article-article-menu gnus-article-mode-map ""
2645      '("Article"
2646        ["Scroll forwards" gnus-article-goto-next-page t]
2647        ["Scroll backwards" gnus-article-goto-prev-page t]
2648        ["Show summary" gnus-article-show-summary t]
2649        ["Fetch Message-ID at point" gnus-article-refer-article t]
2650        ["Mail to address at point" gnus-article-mail t]
2651        ["Send a bug report" gnus-bug t]))
2652
2653     (easy-menu-define
2654      gnus-article-treatment-menu gnus-article-mode-map ""
2655      '("Treatment"
2656        ["Hide headers" gnus-article-hide-headers t]
2657        ["Hide signature" gnus-article-hide-signature t]
2658        ["Hide citation" gnus-article-hide-citation t]
2659        ["Treat overstrike" gnus-article-treat-overstrike t]
2660        ["Remove carriage return" gnus-article-remove-cr t]
2661        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
2662        ["Remove base64" gnus-article-de-base64-unreadable t]
2663        ["Treat html" gnus-article-wash-html t]
2664        ["Decode HZ" gnus-article-decode-HZ t]))
2665
2666     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
2667
2668     (when (boundp 'gnus-summary-post-menu)
2669       (define-key gnus-article-mode-map [menu-bar post]
2670         (cons "Post" gnus-summary-post-menu)))
2671
2672     (gnus-run-hooks 'gnus-article-menu-hook)))
2673
2674 (defun gnus-article-mode ()
2675   "Major mode for displaying an article.
2676
2677 All normal editing commands are switched off.
2678
2679 The following commands are available in addition to all summary mode
2680 commands:
2681 \\<gnus-article-mode-map>
2682 \\[gnus-article-next-page]\t Scroll the article one page forwards
2683 \\[gnus-article-prev-page]\t Scroll the article one page backwards
2684 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
2685 \\[gnus-article-show-summary]\t Display the summary buffer
2686 \\[gnus-article-mail]\t Send a reply to the address near point
2687 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
2688 \\[gnus-info-find-node]\t Go to the Gnus info node"
2689   (interactive)
2690   (when (gnus-visual-p 'article-menu 'menu)
2691     (gnus-article-make-menu-bar))
2692   (gnus-simplify-mode-line)
2693   (setq mode-name "Article")
2694   (setq major-mode 'gnus-article-mode)
2695   (make-local-variable 'minor-mode-alist)
2696   (use-local-map gnus-article-mode-map)
2697   (gnus-update-format-specifications nil 'article-mode)
2698   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
2699   (make-local-variable 'gnus-page-broken)
2700   (make-local-variable 'gnus-button-marker-list)
2701   (make-local-variable 'gnus-article-current-summary)
2702   (make-local-variable 'gnus-article-mime-handles)
2703   (make-local-variable 'gnus-article-decoded-p)
2704   (make-local-variable 'gnus-article-mime-handle-alist)
2705   (make-local-variable 'gnus-article-wash-types)
2706   (make-local-variable 'gnus-article-charset)
2707   (make-local-variable 'gnus-article-ignored-charsets)
2708   (gnus-set-default-directory)
2709   (buffer-disable-undo)
2710   (setq buffer-read-only t)
2711   (set-syntax-table gnus-article-mode-syntax-table)
2712   (mm-enable-multibyte)
2713   (gnus-run-hooks 'gnus-article-mode-hook))
2714
2715 (defun gnus-article-setup-buffer ()
2716   "Initialize the article buffer."
2717   (let* ((name (if gnus-single-article-buffer "*Article*"
2718                  (concat "*Article " gnus-newsgroup-name "*")))
2719          (original
2720           (progn (string-match "\\*Article" name)
2721                  (concat " *Original Article"
2722                          (substring name (match-end 0))))))
2723     (setq gnus-article-buffer name)
2724     (setq gnus-original-article-buffer original)
2725     (setq gnus-article-mime-handle-alist nil)
2726     ;; This might be a variable local to the summary buffer.
2727     (unless gnus-single-article-buffer
2728       (save-excursion
2729         (set-buffer gnus-summary-buffer)
2730         (setq gnus-article-buffer name)
2731         (setq gnus-original-article-buffer original)
2732         (gnus-set-global-variables)))
2733     (gnus-article-setup-highlight-words)
2734     ;; Init original article buffer.
2735     (save-excursion
2736       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2737       (mm-enable-multibyte)
2738       (setq major-mode 'gnus-original-article-mode)
2739       (make-local-variable 'gnus-original-article))
2740     (if (get-buffer name)
2741         (save-excursion
2742           (set-buffer name)
2743           (when gnus-article-mime-handles
2744             (mm-destroy-parts gnus-article-mime-handles)
2745             (setq gnus-article-mime-handles nil))
2746           ;; Set it to nil in article-buffer!
2747           (setq gnus-article-mime-handle-alist nil) 
2748           (buffer-disable-undo)
2749           (setq buffer-read-only t)
2750           (unless (eq major-mode 'gnus-article-mode)
2751             (gnus-article-mode))
2752           (current-buffer))
2753       (save-excursion
2754         (set-buffer (gnus-get-buffer-create name))
2755         (gnus-article-mode)
2756         (make-local-variable 'gnus-summary-buffer)
2757         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2758         (current-buffer)))))
2759
2760 ;; Set article window start at LINE, where LINE is the number of lines
2761 ;; from the head of the article.
2762 (defun gnus-article-set-window-start (&optional line)
2763   (set-window-start
2764    (get-buffer-window gnus-article-buffer t)
2765    (save-excursion
2766      (set-buffer gnus-article-buffer)
2767      (goto-char (point-min))
2768      (if (not line)
2769          (point-min)
2770        (gnus-message 6 "Moved to bookmark")
2771        (search-forward "\n\n" nil t)
2772        (forward-line line)
2773        (point)))))
2774
2775 (defun gnus-article-prepare (article &optional all-headers header)
2776   "Prepare ARTICLE in article mode buffer.
2777 ARTICLE should either be an article number or a Message-ID.
2778 If ARTICLE is an id, HEADER should be the article headers.
2779 If ALL-HEADERS is non-nil, no headers are hidden."
2780   (save-excursion
2781     ;; Make sure we start in a summary buffer.
2782     (unless (eq major-mode 'gnus-summary-mode)
2783       (set-buffer gnus-summary-buffer))
2784     (setq gnus-summary-buffer (current-buffer))
2785     (let* ((gnus-article (if header (mail-header-number header) article))
2786            (summary-buffer (current-buffer))
2787            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2788            (group gnus-newsgroup-name)
2789            result)
2790       (save-excursion
2791         (gnus-article-setup-buffer)
2792         (set-buffer gnus-article-buffer)
2793         ;; Deactivate active regions.
2794         (when (and (boundp 'transient-mark-mode)
2795                    transient-mark-mode)
2796           (setq mark-active nil))
2797         (if (not (setq result (let ((buffer-read-only nil))
2798                                 (gnus-request-article-this-buffer
2799                                  article group))))
2800             ;; There is no such article.
2801             (save-excursion
2802               (when (and (numberp article)
2803                          (not (memq article gnus-newsgroup-sparse)))
2804                 (setq gnus-article-current
2805                       (cons gnus-newsgroup-name article))
2806                 (set-buffer gnus-summary-buffer)
2807                 (setq gnus-current-article article)
2808                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2809                     (progn
2810                       (gnus-summary-set-agent-mark article)
2811                       (message "Message marked for downloading"))
2812                   (gnus-summary-mark-article article gnus-canceled-mark)
2813                   (unless (memq article gnus-newsgroup-sparse)
2814                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
2815           (if (or (eq result 'pseudo)
2816                   (eq result 'nneething))
2817               (progn
2818                 (save-excursion
2819                   (set-buffer summary-buffer)
2820                   (push article gnus-newsgroup-history)
2821                   (setq gnus-last-article gnus-current-article
2822                         gnus-current-article 0
2823                         gnus-current-headers nil
2824                         gnus-article-current nil)
2825                   (if (eq result 'nneething)
2826                       (gnus-configure-windows 'summary)
2827                     (gnus-configure-windows 'article))
2828                   (gnus-set-global-variables))
2829                 (let ((gnus-article-mime-handle-alist-1
2830                        gnus-article-mime-handle-alist))
2831                   (gnus-set-mode-line 'article)))
2832             ;; The result from the `request' was an actual article -
2833             ;; or at least some text that is now displayed in the
2834             ;; article buffer.
2835             (when (and (numberp article)
2836                        (not (eq article gnus-current-article)))
2837               ;; Seems like a new article has been selected.
2838               ;; `gnus-current-article' must be an article number.
2839               (save-excursion
2840                 (set-buffer summary-buffer)
2841                 (push article gnus-newsgroup-history)
2842                 (setq gnus-last-article gnus-current-article
2843                       gnus-current-article article
2844                       gnus-current-headers
2845                       (gnus-summary-article-header gnus-current-article)
2846                       gnus-article-current
2847                       (cons gnus-newsgroup-name gnus-current-article))
2848                 (unless (vectorp gnus-current-headers)
2849                   (setq gnus-current-headers nil))
2850                 (gnus-summary-goto-subject gnus-current-article)
2851                 (when (gnus-summary-show-thread)
2852                   ;; If the summary buffer really was folded, the
2853                   ;; previous goto may not actually have gone to
2854                   ;; the right article, but the thread root instead.
2855                   ;; So we go again.
2856                   (gnus-summary-goto-subject gnus-current-article))
2857                 (gnus-run-hooks 'gnus-mark-article-hook)
2858                 (gnus-set-mode-line 'summary)
2859                 (when (gnus-visual-p 'article-highlight 'highlight)
2860                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2861                 ;; Set the global newsgroup variables here.
2862                 (gnus-set-global-variables)
2863                 (setq gnus-have-all-headers
2864                       (or all-headers gnus-show-all-headers))))
2865             (save-excursion
2866               (gnus-configure-windows 'article))
2867             (when (or (numberp article)
2868                       (stringp article))
2869               (gnus-article-prepare-display)
2870               ;; Do page break.
2871               (goto-char (point-min))
2872               (setq gnus-page-broken
2873                     (when gnus-break-pages
2874                       (gnus-narrow-to-page)
2875                       t)))
2876             (let ((gnus-article-mime-handle-alist-1
2877                    gnus-article-mime-handle-alist))
2878               (gnus-set-mode-line 'article))
2879             (article-goto-body)
2880             (set-window-point (get-buffer-window (current-buffer)) (point))
2881             (gnus-configure-windows 'article)
2882             t))))))
2883
2884 ;;;###autoload
2885 (defun gnus-article-prepare-display ()
2886   "Make the current buffer look like a nice article."
2887   ;; Hooks for getting information from the article.
2888   ;; This hook must be called before being narrowed.
2889   (let ((gnus-article-buffer (current-buffer))
2890         buffer-read-only)
2891     (unless (eq major-mode 'gnus-article-mode)
2892       (gnus-article-mode))
2893     (setq buffer-read-only nil
2894           gnus-article-wash-types nil)
2895     (gnus-run-hooks 'gnus-tmp-internal-hook)
2896     (when gnus-display-mime-function
2897       (funcall gnus-display-mime-function))
2898     (gnus-run-hooks 'gnus-article-prepare-hook)))
2899
2900 ;;;
2901 ;;; Gnus MIME viewing functions
2902 ;;;
2903
2904 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
2905   "The following specs can be used:
2906 %t  The MIME type
2907 %T  MIME type, along with additional info
2908 %n  The `name' parameter
2909 %d  The description, if any
2910 %l  The length of the encoded part
2911 %p  The part identifier number
2912 %e  Dots if the part isn't displayed")
2913
2914 (defvar gnus-mime-button-line-format-alist
2915   '((?t gnus-tmp-type ?s)
2916     (?T gnus-tmp-type-long ?s)
2917     (?n gnus-tmp-name ?s)
2918     (?d gnus-tmp-description ?s)
2919     (?p gnus-tmp-id ?s)
2920     (?l gnus-tmp-length ?d)
2921     (?e gnus-tmp-dots ?s)))
2922
2923 (defvar gnus-mime-button-commands
2924   '((gnus-article-press-button "\r" "Toggle Display")
2925     (gnus-mime-view-part "v" "View Interactively...")
2926     (gnus-mime-view-part-as-type "t" "View As Type...")
2927     (gnus-mime-view-part-as-charset "C" "View As charset...")
2928     (gnus-mime-save-part "o" "Save...")
2929     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
2930     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
2931     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
2932     (gnus-mime-internalize-part "E" "View Internally")
2933     (gnus-mime-externalize-part "e" "View Externally")
2934     (gnus-mime-pipe-part "|" "Pipe To Command...")
2935     (gnus-mime-action-on-part "." "Take action on the part")))
2936
2937 (defun gnus-article-mime-part-status ()
2938   (if gnus-article-mime-handle-alist-1
2939       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
2940     ""))
2941
2942 (defvar gnus-mime-button-map
2943   (let ((map (make-sparse-keymap)))
2944     (set-keymap-parent map gnus-article-mode-map)
2945     (define-key map gnus-mouse-2 'gnus-article-push-button)
2946     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
2947     (dolist (c gnus-mime-button-commands)
2948       (define-key map (cadr c) (car c)))
2949     map))
2950
2951 (defun gnus-mime-button-menu (event)
2952   "Construct a context-sensitive menu of MIME commands."
2953   (interactive "e")
2954   (save-excursion
2955     (let ((pos (event-start event)))
2956       (set-buffer (window-buffer (posn-window pos)))
2957       (goto-char (posn-point pos))
2958       (gnus-article-check-buffer)
2959       (let ((response (x-popup-menu
2960                        t `("MIME Part"
2961                            ("" ,@(mapcar (lambda (c)
2962                                            (cons (caddr c) (car c)))
2963                                          gnus-mime-button-commands))))))
2964         (if response
2965             (call-interactively response))))))
2966
2967 (defun gnus-mime-view-all-parts (&optional handles)
2968   "View all the MIME parts."
2969   (interactive)
2970   (save-current-buffer
2971     (set-buffer gnus-article-buffer)
2972     (let ((handles (or handles gnus-article-mime-handles))
2973           (mail-parse-charset gnus-newsgroup-charset)
2974           (mail-parse-ignored-charsets 
2975            (save-excursion (set-buffer gnus-summary-buffer)
2976                            gnus-newsgroup-ignored-charsets)))
2977       (if (stringp (car handles))
2978           (gnus-mime-view-all-parts (cdr handles))
2979         (mapcar 'mm-display-part handles)))))
2980
2981 (defun gnus-mime-save-part-and-strip ()
2982   "Save the MIME part under point then replace it with an external body."
2983   (interactive)
2984   (gnus-article-check-buffer)
2985   (let* ((data (get-text-property (point) 'gnus-data)) 
2986          (file (mm-save-part data))
2987          param)
2988     (when file
2989       (with-current-buffer (mm-handle-buffer data)
2990         (erase-buffer)
2991         (insert "Content-Type: " (mm-handle-media-type data))
2992         (mml-insert-parameter-string (cdr (mm-handle-type data))
2993                                      '(charset))
2994         (insert "\n")
2995         (insert "Content-ID: " (message-make-message-id) "\n")
2996         (insert "Content-Transfer-Encoding: binary\n")
2997         (insert "\n"))
2998       (setcdr data
2999               (cdr (mm-make-handle nil 
3000                                    `("message/external-body"
3001                                      (access-type . "LOCAL-FILE")
3002                                      (name . ,file)))))
3003       (set-buffer gnus-summary-buffer)
3004       (gnus-article-edit-article
3005        `(lambda () 
3006            (erase-buffer)
3007            (let ((mail-parse-charset (or gnus-article-charset 
3008                                          ',gnus-newsgroup-charset))
3009                  (mail-parse-ignored-charsets 
3010                   (or gnus-article-ignored-charsets
3011                       ',gnus-newsgroup-ignored-charsets))
3012                  (mbl mml-buffer-list))
3013              (setq mml-buffer-list nil)
3014              (insert-buffer gnus-original-article-buffer)
3015              (mime-to-mml gnus-article-mime-handles)
3016              (setq gnus-article-mime-handles nil)
3017              (make-local-hook 'kill-buffer-hook)
3018              (let ((mbl1 mml-buffer-list))
3019                (setq mml-buffer-list mbl)
3020                (set (make-local-variable 'mml-buffer-list) mbl1))
3021              (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
3022        `(lambda (no-highlight)
3023           (let ((mail-parse-charset (or gnus-article-charset
3024                                         ',gnus-newsgroup-charset))
3025                 (message-options message-options)
3026                 (message-options-set-recipient)
3027                 (mail-parse-ignored-charsets  
3028                  (or gnus-article-ignored-charsets
3029                      ',gnus-newsgroup-ignored-charsets)))
3030            (mml-to-mime)
3031            (mml-destroy-buffers)
3032            (remove-hook 'kill-buffer-hook 
3033                         'mml-destroy-buffers t)
3034            (kill-local-variable 'mml-buffer-list))
3035           (gnus-summary-edit-article-done
3036            ,(or (mail-header-references gnus-current-headers) "")
3037            ,(gnus-group-read-only-p) 
3038            ,gnus-summary-buffer no-highlight))))))
3039
3040 (defun gnus-mime-save-part ()
3041   "Save the MIME part under point."
3042   (interactive)
3043   (gnus-article-check-buffer)
3044   (let ((data (get-text-property (point) 'gnus-data)))
3045     (mm-save-part data)))
3046
3047 (defun gnus-mime-pipe-part ()
3048   "Pipe the MIME part under point to a process."
3049   (interactive)
3050   (gnus-article-check-buffer)
3051   (let ((data (get-text-property (point) 'gnus-data)))
3052     (mm-pipe-part data)))
3053
3054 (defun gnus-mime-view-part ()
3055   "Interactively choose a viewing method for the MIME part under point."
3056   (interactive)
3057   (gnus-article-check-buffer)
3058   (let ((data (get-text-property (point) 'gnus-data)))
3059     (mm-interactively-view-part data)))
3060
3061 (defun gnus-mime-view-part-as-type-internal ()
3062   (gnus-article-check-buffer)
3063   (let* ((name (mail-content-type-get
3064                 (mm-handle-type (get-text-property (point) 'gnus-data))
3065                 'name))
3066          (def-type (and name (mm-default-file-encoding name))))
3067     (and def-type (cons def-type 0))))
3068
3069 (defun gnus-mime-view-part-as-type (mime-type)
3070   "Choose a MIME media type, and view the part as such."
3071   (interactive
3072    (list (completing-read
3073           "View as MIME type: "
3074           (mapcar #'list (mailcap-mime-types))
3075           nil nil
3076           (gnus-mime-view-part-as-type-internal))))
3077   (gnus-article-check-buffer)
3078   (let ((handle (get-text-property (point) 'gnus-data)))
3079     (gnus-mm-display-part
3080      (mm-make-handle (mm-handle-buffer handle)
3081                      (cons mime-type (cdr (mm-handle-type handle)))
3082                      (mm-handle-encoding handle)
3083                      (mm-handle-undisplayer handle)
3084                      (mm-handle-disposition handle)
3085                      (mm-handle-description handle)
3086                      (mm-handle-cache handle)
3087                      (mm-handle-id handle)))))
3088
3089 (defun gnus-mime-copy-part (&optional handle)
3090   "Put the the MIME part under point into a new buffer."
3091   (interactive)
3092   (gnus-article-check-buffer)
3093   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3094          (contents (mm-get-part handle))|
3095          (base (file-name-nondirectory
3096                 (or
3097                  (mail-content-type-get (mm-handle-type handle) 'name)
3098                  (mail-content-type-get (mm-handle-type handle)
3099                                         'filename)
3100                  "*decoded*")))
3101          (buffer (generate-new-buffer base)))
3102     (switch-to-buffer buffer)
3103     (insert contents)
3104     ;; We do it this way to make `normal-mode' set the appropriate mode.
3105     (unwind-protect
3106         (progn
3107           (setq buffer-file-name (expand-file-name base))
3108           (normal-mode))
3109       (setq buffer-file-name nil))
3110     (goto-char (point-min))))
3111
3112 (defun gnus-mime-inline-part (&optional handle arg)
3113   "Insert the MIME part under point into the current buffer."
3114   (interactive (list nil current-prefix-arg))
3115   (gnus-article-check-buffer)
3116   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3117          contents charset
3118          (b (point))
3119          buffer-read-only)
3120     (if (and (not arg) (mm-handle-undisplayer handle))
3121         (mm-remove-part handle)
3122       (setq contents (mm-get-part handle))
3123       (cond
3124        ((not arg)
3125         (setq charset (or (mail-content-type-get
3126                            (mm-handle-type handle) 'charset)
3127                           gnus-newsgroup-charset)))
3128        ((numberp arg)
3129         (if (mm-handle-undisplayer handle)
3130             (mm-remove-part handle))
3131         (setq charset
3132               (or (cdr (assq arg 
3133                              gnus-summary-show-article-charset-alist))
3134                   (read-coding-system "Charset: ")))))
3135       (forward-line 2)
3136       (mm-insert-inline handle
3137                         (if (and charset 
3138                                  (setq charset (mm-charset-to-coding-system 
3139                                                 charset))
3140                                  (not (eq charset 'ascii)))
3141                             (mm-decode-coding-string contents charset)
3142                           contents))
3143       (goto-char b))))
3144
3145 (defun gnus-mime-view-part-as-charset (&optional handle arg)
3146   "Insert the MIME part under point into the current buffer."
3147   (interactive (list nil current-prefix-arg))
3148   (gnus-article-check-buffer)
3149   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3150          contents charset
3151          (b (point))
3152          buffer-read-only)
3153     (if (mm-handle-undisplayer handle)
3154         (mm-remove-part handle))
3155     (let ((gnus-newsgroup-charset
3156            (or (cdr (assq arg 
3157                           gnus-summary-show-article-charset-alist))
3158                (read-coding-system "Charset: ")))
3159           (gnus-newsgroup-ignored-charsets 'gnus-all))
3160       (gnus-article-press-button))))
3161
3162 (defun gnus-mime-externalize-part (&optional handle)
3163   "View the MIME part under point with an external viewer."
3164   (interactive)
3165   (gnus-article-check-buffer)
3166   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3167          (mm-user-display-methods nil)
3168          (mm-inlined-types nil)
3169          (mail-parse-charset gnus-newsgroup-charset)
3170          (mail-parse-ignored-charsets 
3171           (save-excursion (set-buffer gnus-summary-buffer)
3172                           gnus-newsgroup-ignored-charsets)))
3173     (if (mm-handle-undisplayer handle)
3174         (mm-remove-part handle)
3175       (mm-display-part handle))))
3176
3177 (defun gnus-mime-internalize-part (&optional handle)
3178   "View the MIME part under point with an internal viewer.
3179 In no internal viewer is available, use an external viewer."
3180   (interactive)
3181   (gnus-article-check-buffer)
3182   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3183          (mm-inlined-types '(".*"))
3184          (mm-inline-large-images t)
3185          (mail-parse-charset gnus-newsgroup-charset)
3186          (mail-parse-ignored-charsets 
3187           (save-excursion (set-buffer gnus-summary-buffer)
3188                           gnus-newsgroup-ignored-charsets)))
3189     (if (mm-handle-undisplayer handle)
3190         (mm-remove-part handle)
3191       (mm-display-part handle))))
3192
3193 (defun gnus-mime-action-on-part (&optional action)
3194   "Do something with the MIME attachment at \(point\)."
3195   (interactive
3196    (list (completing-read "Action: " gnus-mime-action-alist)))
3197   (gnus-article-check-buffer)
3198   (let ((action-pair (assoc action gnus-mime-action-alist)))
3199     (if action-pair
3200         (funcall (cdr action-pair)))))
3201
3202
3203 (defun gnus-article-part-wrapper (n function)
3204   (save-current-buffer
3205     (set-buffer gnus-article-buffer)
3206     (when (> n (length gnus-article-mime-handle-alist))
3207       (error "No such part"))
3208     (gnus-article-goto-part n)
3209     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3210       (funcall function handle))))
3211
3212 (defun gnus-article-pipe-part (n)
3213   "Pipe MIME part N, which is the numerical prefix."
3214   (interactive "p")
3215   (gnus-article-part-wrapper n 'mm-pipe-part))
3216
3217 (defun gnus-article-save-part (n)
3218   "Save MIME part N, which is the numerical prefix."
3219   (interactive "p")
3220   (gnus-article-part-wrapper n 'mm-save-part))
3221
3222 (defun gnus-article-interactively-view-part (n)
3223   "View MIME part N interactively, which is the numerical prefix."
3224   (interactive "p")
3225   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3226
3227 (defun gnus-article-copy-part (n)
3228   "Copy MIME part N, which is the numerical prefix."
3229   (interactive "p")
3230   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3231
3232 (defun gnus-article-view-part-as-charset (n)
3233   "Copy MIME part N, which is the numerical prefix."
3234   (interactive "p")
3235   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
3236
3237 (defun gnus-article-externalize-part (n)
3238   "View MIME part N externally, which is the numerical prefix."
3239   (interactive "p")
3240   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3241
3242 (defun gnus-article-inline-part (n)
3243   "Inline MIME part N, which is the numerical prefix."
3244   (interactive "p")
3245   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3246
3247 (defun gnus-article-mime-match-handle-first (condition)
3248   (if condition
3249       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3250         (while (setq ihandle (pop alist))
3251           (if (and (cond 
3252                     ((functionp condition)
3253                      (funcall condition (cdr ihandle)))
3254                     ((eq condition 'undisplayed) 
3255                      (not (or (mm-handle-undisplayer (cdr ihandle))
3256                               (equal (mm-handle-media-type (cdr ihandle))
3257                                      "multipart/alternative"))))
3258                     ((eq condition 'undisplayed-alternative)
3259                      (not (mm-handle-undisplayer (cdr ihandle))))
3260                     (t t))
3261                    (gnus-article-goto-part (car ihandle))
3262                    (or (not n) (< (car ihandle) n)))
3263               (setq n (car ihandle))))
3264         (or n 1))
3265     1))
3266
3267 (defun gnus-article-view-part (&optional n)
3268   "View MIME part N, which is the numerical prefix."
3269   (interactive "P")
3270   (save-current-buffer
3271     (set-buffer gnus-article-buffer)
3272     (or (numberp n) (setq n (gnus-article-mime-match-handle-first 
3273                              gnus-article-mime-match-handle-function)))
3274     (when (> n (length gnus-article-mime-handle-alist))
3275       (error "No such part"))
3276     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3277       (when (gnus-article-goto-part n)
3278         (if (equal (car handle) "multipart/alternative")
3279             (gnus-article-press-button)
3280           (when (eq (gnus-mm-display-part handle) 'internal)
3281             (gnus-set-window-start)))))))
3282
3283 (defsubst gnus-article-mime-total-parts ()
3284   (if (bufferp (car gnus-article-mime-handles))
3285       1 ;; single part
3286     (1- (length gnus-article-mime-handles))))
3287
3288 (defun gnus-mm-display-part (handle)
3289   "Display HANDLE and fix MIME button."
3290   (let ((id (get-text-property (point) 'gnus-part))
3291         (point (point))
3292         buffer-read-only)
3293     (forward-line 1)
3294     (prog1
3295         (let ((window (selected-window))
3296               (mail-parse-charset gnus-newsgroup-charset)
3297               (mail-parse-ignored-charsets 
3298                (save-excursion (set-buffer gnus-summary-buffer)
3299                                gnus-newsgroup-ignored-charsets)))
3300           (save-excursion
3301             (unwind-protect
3302                 (let ((win (get-buffer-window (current-buffer) t))
3303                       (beg (point)))
3304                   (when win
3305                     (select-window win))
3306                   (goto-char point)
3307                   (forward-line)
3308                   (if (mm-handle-displayed-p handle)
3309                       ;; This will remove the part.
3310                       (mm-display-part handle)
3311                     (save-restriction
3312                       (narrow-to-region (point) (1+ (point)))
3313                       (mm-display-part handle)
3314                       ;; We narrow to the part itself and
3315                       ;; then call the treatment functions.
3316                       (goto-char (point-min))
3317                       (forward-line 1)
3318                       (narrow-to-region (point) (point-max))
3319                       (gnus-treat-article
3320                        nil id
3321                        (gnus-article-mime-total-parts)
3322                        (mm-handle-media-type handle)))))
3323               (select-window window))))
3324       (goto-char point)
3325       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
3326       (gnus-insert-mime-button
3327        handle id (list (mm-handle-displayed-p handle)))
3328       (goto-char point))))
3329
3330 (defun gnus-article-goto-part (n)
3331   "Go to MIME part N."
3332   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
3333     (when point
3334       (goto-char point))))
3335
3336 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
3337   (let ((gnus-tmp-name
3338          (or (mail-content-type-get (mm-handle-type handle)
3339                                     'name)
3340              (mail-content-type-get (mm-handle-disposition handle)
3341                                     'filename)
3342              (mail-content-type-get (mm-handle-type handle)
3343                                     'url)
3344              ""))
3345         (gnus-tmp-type (mm-handle-media-type handle))
3346         (gnus-tmp-description
3347          (mail-decode-encoded-word-string (or (mm-handle-description handle)
3348                                               "")))
3349         (gnus-tmp-dots
3350          (if (if displayed (car displayed)
3351                (mm-handle-displayed-p handle))
3352              "" "..."))
3353         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
3354                            (buffer-size)))
3355         gnus-tmp-type-long b e)
3356     (when (string-match ".*/" gnus-tmp-name)
3357       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
3358     (setq gnus-tmp-type-long (concat gnus-tmp-type
3359                                      (and (not (equal gnus-tmp-name ""))
3360                                           (concat "; " gnus-tmp-name))))
3361     (or (equal gnus-tmp-description "")
3362         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
3363     (unless (bolp)
3364       (insert "\n"))
3365     (setq b (point))
3366     (gnus-eval-format
3367      gnus-mime-button-line-format gnus-mime-button-line-format-alist
3368      `(local-map ,gnus-mime-button-map
3369                  keymap ,gnus-mime-button-map
3370                  gnus-callback gnus-mm-display-part
3371                  gnus-part ,gnus-tmp-id
3372                  article-type annotation
3373                  gnus-data ,handle))
3374     (setq e (point))
3375     (widget-convert-button
3376      'link b e
3377      :mime-handle handle
3378      :action 'gnus-widget-press-button
3379      :button-keymap gnus-mime-button-map
3380      :help-echo
3381      (lambda (widget/window &optional overlay pos)
3382        ;; Needed to properly clear the message due to a bug in
3383        ;; wid-edit (XEmacs only).
3384        (if (boundp 'help-echo-owns-message)
3385            (setq help-echo-owns-message t))
3386        (format
3387         "%S: %s the MIME part; %S: more options"
3388         (aref gnus-mouse-2 0)
3389         ;; XEmacs will get a single widget arg; Emacs 21 will get
3390         ;; window, overlay, position.
3391         (if (mm-handle-displayed-p
3392              (if overlay
3393                  (with-current-buffer (gnus-overlay-buffer overlay)
3394                    (widget-get (widget-at (gnus-overlay-start overlay))
3395                                :mime-handle))
3396                (widget-get widget/window :mime-handle)))
3397             "hide" "show")
3398         (aref gnus-down-mouse-3 0))))))
3399
3400 (defun gnus-widget-press-button (elems el)
3401   (goto-char (widget-get elems :from))
3402   (gnus-article-press-button))
3403
3404 (defvar gnus-displaying-mime nil)
3405
3406 (defun gnus-display-mime (&optional ihandles)
3407   "Display the MIME parts."
3408   (save-excursion
3409     (save-selected-window
3410       (let ((window (get-buffer-window gnus-article-buffer))
3411             (point (point)))
3412         (when window
3413           (select-window window)
3414           ;; We have to do this since selecting the window
3415           ;; may change the point.  So we set the window point.
3416           (set-window-point window point)))
3417       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3418              buffer-read-only handle name type b e display)
3419         (when (and (not ihandles)
3420                    (not gnus-displaying-mime))
3421           ;; Top-level call; we clean up.
3422           (when gnus-article-mime-handles
3423             (mm-destroy-parts gnus-article-mime-handles)
3424             (setq gnus-article-mime-handle-alist nil));; A trick.
3425           (setq gnus-article-mime-handles handles)
3426           ;; We allow users to glean info from the handles.
3427           (when gnus-article-mime-part-function
3428             (gnus-mime-part-function handles)))
3429         (if (and handles
3430                  (or (not (stringp (car handles)))
3431                      (cdr handles)))
3432             (progn
3433               (when (and (not ihandles)
3434                          (not gnus-displaying-mime))
3435                 ;; Clean up for mime parts.
3436                 (article-goto-body)
3437                 (delete-region (point) (point-max)))
3438               (let ((gnus-displaying-mime t))
3439                 (gnus-mime-display-part handles)))
3440           (save-restriction
3441             (article-goto-body)
3442             (narrow-to-region (point) (point-max))
3443             (gnus-treat-article nil 1 1)
3444             (widen)))
3445         (unless ihandles
3446           ;; Highlight the headers.
3447           (save-excursion
3448             (save-restriction
3449               (article-goto-body)
3450               (narrow-to-region (point-min) (point))
3451               (gnus-treat-article 'head))))))))
3452
3453 (defvar gnus-mime-display-multipart-as-mixed nil)
3454
3455 (defun gnus-mime-display-part (handle)
3456   (cond
3457    ;; Single part.
3458    ((not (stringp (car handle)))
3459     (gnus-mime-display-single handle))
3460    ;; User-defined multipart
3461    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
3462     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
3463              handle))
3464    ;; multipart/alternative
3465    ((and (equal (car handle) "multipart/alternative")
3466          (not gnus-mime-display-multipart-as-mixed))
3467     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3468       (push (cons id handle) gnus-article-mime-handle-alist)
3469       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3470    ;; multipart/related
3471    ((and (equal (car handle) "multipart/related")
3472          (not gnus-mime-display-multipart-as-mixed))
3473     ;;;!!!We should find the start part, but we just default
3474     ;;;!!!to the first part.
3475     (gnus-mime-display-part (cadr handle)))
3476    ;; Other multiparts are handled like multipart/mixed.
3477    ((equal (car handle) "multipart/signed")
3478     (or (memq 'signed gnus-article-wash-types)
3479         (push 'signed gnus-article-wash-types))
3480     (gnus-mime-display-mixed (cdr handle)))
3481    ((equal (car handle) "multipart/encrypted")
3482     (or (memq 'encrypted gnus-article-wash-types)
3483         (push 'encrypted gnus-article-wash-types))
3484     (gnus-mime-display-mixed (cdr handle)))
3485    (t
3486     (gnus-mime-display-mixed (cdr handle)))))
3487
3488 (defun gnus-mime-part-function (handles)
3489   (if (stringp (car handles))
3490       (mapcar 'gnus-mime-part-function (cdr handles))
3491     (funcall gnus-article-mime-part-function handles)))
3492
3493 (defun gnus-mime-display-mixed (handles)
3494   (mapcar 'gnus-mime-display-part handles))
3495
3496 (defun gnus-mime-display-single (handle)
3497   (let ((type (mm-handle-media-type handle))
3498         (ignored gnus-ignored-mime-types)
3499         (not-attachment t)
3500         (move nil)
3501         display text)
3502     (catch 'ignored
3503       (progn
3504         (while ignored
3505           (when (string-match (pop ignored) type)
3506             (throw 'ignored nil)))
3507         (if (and (setq not-attachment
3508                        (and (not (mm-inline-override-p handle))
3509                             (or (not (mm-handle-disposition handle))
3510                                 (equal (car (mm-handle-disposition handle))
3511                                        "inline")
3512                                 (mm-attachment-override-p handle))))
3513                  (mm-automatic-display-p handle)
3514                  (or (mm-inlined-p handle)
3515                      (mm-automatic-external-display-p type)))
3516             (setq display t)
3517           (when (equal (mm-handle-media-supertype handle) "text")
3518             (setq text t)))
3519         (let ((id (1+ (length gnus-article-mime-handle-alist)))
3520               beg)
3521           (push (cons id handle) gnus-article-mime-handle-alist)
3522           (when (or (not display)
3523                     (not (gnus-unbuttonized-mime-type-p type)))
3524             ;(gnus-article-insert-newline)
3525             (gnus-insert-mime-button
3526              handle id (list (or display (and not-attachment text))))
3527             (gnus-article-insert-newline) 
3528             ;(gnus-article-insert-newline) 
3529             ;; Remember modify the number of forward lines.
3530             (setq move t))
3531           (setq beg (point))
3532           (cond
3533            (display
3534             (when move
3535               (forward-line -1)
3536               (setq beg (point)))
3537             (let ((mail-parse-charset gnus-newsgroup-charset)
3538                   (mail-parse-ignored-charsets 
3539                    (save-excursion (condition-case ()
3540                                        (set-buffer gnus-summary-buffer)
3541                                      (error))
3542                                    gnus-newsgroup-ignored-charsets)))
3543               (mm-display-part handle t))
3544             (goto-char (point-max)))
3545            ((and text not-attachment)
3546             (when move
3547               (forward-line -1)
3548               (setq beg (point)))
3549             (gnus-article-insert-newline)
3550             (mm-insert-inline handle (mm-get-part handle))
3551             (goto-char (point-max))))
3552           ;; Do highlighting.
3553           (save-excursion
3554             (save-restriction
3555               (narrow-to-region beg (point))
3556               (gnus-treat-article
3557                nil id 
3558                (gnus-article-mime-total-parts)
3559                (mm-handle-media-type handle)))))))))
3560
3561 (defun gnus-unbuttonized-mime-type-p (type)
3562   "Say whether TYPE is to be unbuttonized."
3563   (unless gnus-inhibit-mime-unbuttonizing
3564     (catch 'found
3565       (let ((types gnus-unbuttonized-mime-types))
3566         (while types
3567           (when (string-match (pop types) type)
3568             (throw 'found t)))))))
3569
3570 (defun gnus-article-insert-newline ()
3571   "Insert a newline, but mark it as undeletable."
3572   (gnus-put-text-property
3573    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
3574
3575 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
3576   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
3577          (ihandles handles)
3578          (point (point))
3579          handle buffer-read-only from props begend not-pref)
3580     (save-window-excursion
3581       (save-restriction
3582         (when ibegend
3583           (narrow-to-region (car ibegend)
3584                             (or (cdr ibegend)
3585                                 (progn
3586                                   (goto-char (car ibegend))
3587                                   (forward-line 2)
3588                                   (point))))
3589           (delete-region (point-min) (point-max))
3590           (mm-remove-parts handles))
3591         (setq begend (list (point-marker)))
3592         ;; Do the toggle.
3593         (unless (setq not-pref (cadr (member preferred ihandles)))
3594           (setq not-pref (car ihandles)))
3595         (when (or ibegend
3596                   (not (gnus-unbuttonized-mime-type-p
3597                         "multipart/alternative")))
3598           (gnus-add-text-properties
3599            (setq from (point))
3600            (progn
3601              (insert (format "%d.  " id))
3602              (point))
3603            `(gnus-callback
3604              (lambda (handles)
3605                (unless ,(not ibegend)
3606                  (setq gnus-article-mime-handle-alist
3607                        ',gnus-article-mime-handle-alist))
3608                (gnus-mime-display-alternative
3609                 ',ihandles ',not-pref ',begend ,id))
3610              local-map ,gnus-mime-button-map
3611              ,gnus-mouse-face-prop ,gnus-article-mouse-face
3612              face ,gnus-article-button-face
3613              keymap ,gnus-mime-button-map
3614              gnus-part ,id
3615              gnus-data ,handle))
3616           (widget-convert-button 'link from (point)
3617                                  :action 'gnus-widget-press-button
3618                                  :button-keymap gnus-widget-button-keymap)
3619           ;; Do the handles
3620           (while (setq handle (pop handles))
3621             (gnus-add-text-properties
3622              (setq from (point))
3623              (progn
3624                (insert (format "(%c) %-18s"
3625                                (if (equal handle preferred) ?* ? )
3626                                (mm-handle-media-type handle)))
3627                (point))
3628              `(gnus-callback
3629                (lambda (handles)
3630                  (unless ,(not ibegend)
3631                    (setq gnus-article-mime-handle-alist
3632                          ',gnus-article-mime-handle-alist))
3633                  (gnus-mime-display-alternative
3634                   ',ihandles ',handle ',begend ,id))
3635                local-map ,gnus-mime-button-map
3636                ,gnus-mouse-face-prop ,gnus-article-mouse-face
3637                face ,gnus-article-button-face
3638                keymap ,gnus-mime-button-map
3639                gnus-part ,id
3640                gnus-data ,handle))
3641             (widget-convert-button 'link from (point)
3642                                    :action 'gnus-widget-press-button
3643                                    :button-keymap gnus-widget-button-keymap)
3644             (insert "  "))
3645           (insert "\n\n"))
3646         (when preferred
3647           (if (stringp (car preferred))
3648               (gnus-display-mime preferred)
3649             (let ((mail-parse-charset gnus-newsgroup-charset)
3650                   (mail-parse-ignored-charsets 
3651                    (save-excursion (set-buffer gnus-summary-buffer)
3652                                    gnus-newsgroup-ignored-charsets)))
3653               (mm-display-part preferred)
3654               ;; Do highlighting.
3655               (save-excursion
3656                 (save-restriction
3657                   (narrow-to-region (car begend) (point-max))
3658                   (gnus-treat-article
3659                    nil (length gnus-article-mime-handle-alist)
3660                    (gnus-article-mime-total-parts)
3661                    (mm-handle-media-type handle))))))
3662           (goto-char (point-max))
3663           (setcdr begend (point-marker)))))
3664     (when ibegend
3665       (goto-char point))))
3666
3667 (defun gnus-article-wash-status ()
3668   "Return a string which display status of article washing."
3669   (save-excursion
3670     (set-buffer gnus-article-buffer)
3671     (let ((cite (memq 'cite gnus-article-wash-types))
3672           (headers (memq 'headers gnus-article-wash-types))
3673           (boring (memq 'boring-headers gnus-article-wash-types))
3674           (pgp (memq 'pgp gnus-article-wash-types))
3675           (pem (memq 'pem gnus-article-wash-types))
3676           (signed (memq 'signed gnus-article-wash-types))
3677           (encrypted (memq 'encrypted gnus-article-wash-types))
3678           (signature (memq 'signature gnus-article-wash-types))
3679           (overstrike (memq 'overstrike gnus-article-wash-types))
3680           (emphasis (memq 'emphasis gnus-article-wash-types)))
3681       (format "%c%c%c%c%c%c"
3682               (if cite ?c ? )
3683               (if (or headers boring) ?h ? )
3684               (if (or pgp pem signed encrypted) ?p ? )
3685               (if signature ?s ? )
3686               (if overstrike ?o ? )
3687               (if emphasis ?e ? )))))
3688
3689 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
3690
3691 (defun gnus-article-maybe-hide-headers ()
3692   "Hide unwanted headers if `gnus-have-all-headers' is nil.
3693 Provided for backwards compatibility."
3694   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
3695                  (not (save-excursion (set-buffer gnus-summary-buffer)
3696                                       gnus-have-all-headers)))
3697              (not gnus-inhibit-hiding))
3698     (gnus-article-hide-headers)))
3699
3700 ;;; Article savers.
3701
3702 (defun gnus-output-to-file (file-name)
3703   "Append the current article to a file named FILE-NAME."
3704   (let ((artbuf (current-buffer)))
3705     (with-temp-buffer
3706       (insert-buffer-substring artbuf)
3707       ;; Append newline at end of the buffer as separator, and then
3708       ;; save it to file.
3709       (goto-char (point-max))
3710       (insert "\n")
3711       (mm-append-to-file (point-min) (point-max) file-name)
3712       t)))
3713
3714 (defun gnus-narrow-to-page (&optional arg)
3715   "Narrow the article buffer to a page.
3716 If given a numerical ARG, move forward ARG pages."
3717   (interactive "P")
3718   (setq arg (if arg (prefix-numeric-value arg) 0))
3719   (save-excursion
3720     (set-buffer gnus-article-buffer)
3721     (goto-char (point-min))
3722     (widen)
3723     ;; Remove any old next/prev buttons.
3724     (when (gnus-visual-p 'page-marker)
3725       (let ((buffer-read-only nil))
3726         (gnus-remove-text-with-property 'gnus-prev)
3727         (gnus-remove-text-with-property 'gnus-next)))
3728     (when
3729         (cond ((< arg 0)
3730                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
3731               ((> arg 0)
3732                (re-search-forward page-delimiter nil 'move arg)))
3733       (goto-char (match-end 0)))
3734     (narrow-to-region
3735      (point)
3736      (if (re-search-forward page-delimiter nil 'move)
3737          (match-beginning 0)
3738        (point)))
3739     (when (and (gnus-visual-p 'page-marker)
3740                (not (= (point-min) 1)))
3741       (save-excursion
3742         (goto-char (point-min))
3743         (gnus-insert-prev-page-button)))
3744     (when (and (gnus-visual-p 'page-marker)
3745                (< (+ (point-max) 2) (buffer-size)))
3746       (save-excursion
3747         (goto-char (point-max))
3748         (gnus-insert-next-page-button)))))
3749
3750 ;; Article mode commands
3751
3752 (defun gnus-article-goto-next-page ()
3753   "Show the next page of the article."
3754   (interactive)
3755   (when (gnus-article-next-page)
3756     (goto-char (point-min))
3757     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
3758
3759 (defun gnus-article-goto-prev-page ()
3760   "Show the next page of the article."
3761   (interactive)
3762   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
3763     (gnus-article-prev-page nil)))
3764
3765 (defun gnus-article-next-page (&optional lines)
3766   "Show the next page of the current article.
3767 If end of article, return non-nil.  Otherwise return nil.
3768 Argument LINES specifies lines to be scrolled up."
3769   (interactive "p")
3770   (move-to-window-line -1)
3771   (if (save-excursion
3772         (end-of-line)
3773         (and (pos-visible-in-window-p)  ;Not continuation line.
3774              (eobp)))
3775       ;; Nothing in this page.
3776       (if (or (not gnus-page-broken)
3777               (save-excursion
3778                 (save-restriction
3779                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
3780           t                             ;Nothing more.
3781         (gnus-narrow-to-page 1)         ;Go to next page.
3782         nil)
3783     ;; More in this page.
3784     (let ((scroll-in-place nil))
3785       (condition-case ()
3786           (scroll-up lines)
3787         (end-of-buffer
3788          ;; Long lines may cause an end-of-buffer error.
3789          (goto-char (point-max)))))
3790     (move-to-window-line 0)
3791     nil))
3792
3793 (defun gnus-article-prev-page (&optional lines)
3794   "Show previous page of current article.
3795 Argument LINES specifies lines to be scrolled down."
3796   (interactive "p")
3797   (move-to-window-line 0)
3798   (if (and gnus-page-broken
3799            (bobp)
3800            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
3801       (progn
3802         (gnus-narrow-to-page -1)        ;Go to previous page.
3803         (goto-char (point-max))
3804         (recenter -1))
3805     (let ((scroll-in-place nil))
3806       (prog1
3807           (condition-case ()
3808               (scroll-down lines)
3809             (beginning-of-buffer
3810              (goto-char (point-min))))
3811         (move-to-window-line 0)))))
3812
3813 (defun gnus-article-refer-article ()
3814   "Read article specified by message-id around point."
3815   (interactive)
3816   (let ((point (point)))
3817     (search-forward ">" nil t)          ;Move point to end of "<....>".
3818     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
3819         (let ((message-id (match-string 1)))
3820           (goto-char point)
3821           (set-buffer gnus-summary-buffer)
3822           (gnus-summary-refer-article message-id))
3823       (goto-char (point))
3824       (error "No references around point"))))
3825
3826 (defun gnus-article-show-summary ()
3827   "Reconfigure windows to show summary buffer."
3828   (interactive)
3829   (if (not (gnus-buffer-live-p gnus-summary-buffer))
3830       (error "There is no summary buffer for this article buffer")
3831     (gnus-article-set-globals)
3832     (gnus-configure-windows 'article)
3833     (gnus-summary-goto-subject gnus-current-article)
3834     (gnus-summary-position-point)))
3835
3836 (defun gnus-article-describe-briefly ()
3837   "Describe article mode commands briefly."
3838   (interactive)
3839   (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")))
3840
3841 (defun gnus-article-summary-command ()
3842   "Execute the last keystroke in the summary buffer."
3843   (interactive)
3844   (let ((obuf (current-buffer))
3845         (owin (current-window-configuration))
3846         func)
3847     (switch-to-buffer gnus-article-current-summary 'norecord)
3848     (setq func (lookup-key (current-local-map) (this-command-keys)))
3849     (call-interactively func)
3850     (set-buffer obuf)
3851     (set-window-configuration owin)
3852     (set-window-point (get-buffer-window (current-buffer)) (point))))
3853
3854 (defun gnus-article-summary-command-nosave ()
3855   "Execute the last keystroke in the summary buffer."
3856   (interactive)
3857   (let (func)
3858     (pop-to-buffer gnus-article-current-summary 'norecord)
3859     (setq func (lookup-key (current-local-map) (this-command-keys)))
3860     (call-interactively func)))
3861
3862 (defun gnus-article-check-buffer ()
3863   "Beep if not in an article buffer."
3864   (unless (equal major-mode 'gnus-article-mode)
3865     (error "Command invoked outside of a Gnus article buffer")))
3866
3867 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
3868   "Read a summary buffer key sequence and execute it from the article buffer."
3869   (interactive "P")
3870   (gnus-article-check-buffer)
3871   (let ((nosaves
3872          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
3873            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
3874            "=" "^" "\M-^" "|"))
3875         (nosave-but-article
3876          '("A\r"))
3877         (nosave-in-article
3878          '("\C-d"))
3879         (up-to-top
3880          '("n" "Gn" "p" "Gp"))
3881         keys new-sum-point)
3882     (save-excursion
3883       (set-buffer gnus-article-current-summary)
3884       (let (gnus-pick-mode)
3885         (push (or key last-command-event) unread-command-events)
3886         (setq keys (if (featurep 'xemacs)
3887                        (events-to-keys (read-key-sequence nil))
3888                      (read-key-sequence nil)))))
3889                      
3890     (message "")
3891
3892     (if (or (member keys nosaves)
3893             (member keys nosave-but-article)
3894             (member keys nosave-in-article))
3895         (let (func)
3896           (save-window-excursion
3897             (pop-to-buffer gnus-article-current-summary 'norecord)
3898             ;; We disable the pick minor mode commands.
3899             (let (gnus-pick-mode)
3900               (setq func (lookup-key (current-local-map) keys))))
3901           (if (or (not func)
3902                   (numberp func))
3903               (ding)
3904             (unless (member keys nosave-in-article)
3905               (set-buffer gnus-article-current-summary))
3906             (call-interactively func)
3907             (setq new-sum-point (point)))
3908           (when (member keys nosave-but-article)
3909             (pop-to-buffer gnus-article-buffer 'norecord)))
3910       ;; These commands should restore window configuration.
3911       (let ((obuf (current-buffer))
3912             (owin (current-window-configuration))
3913             (opoint (point))
3914             (summary gnus-article-current-summary)
3915             func in-buffer selected)
3916         (if not-restore-window
3917             (pop-to-buffer summary 'norecord)
3918           (switch-to-buffer summary 'norecord))
3919         (setq in-buffer (current-buffer))
3920         ;; We disable the pick minor mode commands.
3921         (if (setq func (let (gnus-pick-mode)
3922                          (lookup-key (current-local-map) keys)))
3923             (progn
3924               (call-interactively func)
3925               (setq new-sum-point (point)))
3926           (ding))
3927         (when (eq in-buffer (current-buffer))
3928           (setq selected (gnus-summary-select-article))
3929           (set-buffer obuf)
3930           (unless not-restore-window
3931             (set-window-configuration owin))
3932           (when (eq selected 'old)
3933             (article-goto-body)
3934             (set-window-start (get-buffer-window (current-buffer))
3935                               1)
3936             (set-window-point (get-buffer-window (current-buffer))
3937                               (point)))
3938           (let ((win (get-buffer-window gnus-article-current-summary)))
3939             (when win
3940               (set-window-point win new-sum-point))))))))
3941
3942 (defun gnus-article-hide (&optional arg force)
3943   "Hide all the gruft in the current article.
3944 This means that PGP stuff, signatures, cited text and (some)
3945 headers will be hidden.
3946 If given a prefix, show the hidden text instead."
3947   (interactive (append (gnus-article-hidden-arg) (list 'force)))
3948   (gnus-article-hide-headers arg)
3949   (gnus-article-hide-list-identifiers arg)
3950   (gnus-article-hide-pgp arg)
3951   (gnus-article-hide-citation-maybe arg force)
3952   (gnus-article-hide-signature arg))
3953
3954 (defun gnus-article-maybe-highlight ()
3955   "Do some article highlighting if article highlighting is requested."
3956   (when (gnus-visual-p 'article-highlight 'highlight)
3957     (gnus-article-highlight-some)))
3958
3959 (defun gnus-check-group-server ()
3960   ;; Make sure the connection to the server is alive.
3961   (unless (gnus-server-opened
3962            (gnus-find-method-for-group gnus-newsgroup-name))
3963     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
3964     (gnus-request-group gnus-newsgroup-name t)))
3965
3966 (defun gnus-request-article-this-buffer (article group)
3967   "Get an article and insert it into this buffer."
3968   (let (do-update-line sparse-header)
3969     (prog1
3970         (save-excursion
3971           (erase-buffer)
3972           (gnus-kill-all-overlays)
3973           (setq group (or group gnus-newsgroup-name))
3974
3975           ;; Using `gnus-request-article' directly will insert the article into
3976           ;; `nntp-server-buffer' - so we'll save some time by not having to
3977           ;; copy it from the server buffer into the article buffer.
3978
3979           ;; We only request an article by message-id when we do not have the
3980           ;; headers for it, so we'll have to get those.
3981           (when (stringp article)
3982             (gnus-read-header article))
3983
3984           ;; If the article number is negative, that means that this article
3985           ;; doesn't belong in this newsgroup (possibly), so we find its
3986           ;; message-id and request it by id instead of number.
3987           (when (and (numberp article)
3988                      gnus-summary-buffer
3989                      (get-buffer gnus-summary-buffer)
3990                      (gnus-buffer-exists-p gnus-summary-buffer))
3991             (save-excursion
3992               (set-buffer gnus-summary-buffer)
3993               (let ((header (gnus-summary-article-header article)))
3994                 (when (< article 0)
3995                   (cond
3996                    ((memq article gnus-newsgroup-sparse)
3997                     ;; This is a sparse gap article.
3998                     (setq do-update-line article)
3999                     (setq article (mail-header-id header))
4000                     (setq sparse-header (gnus-read-header article))
4001                     (setq gnus-newsgroup-sparse
4002                           (delq article gnus-newsgroup-sparse)))
4003                    ((vectorp header)
4004                     ;; It's a real article.
4005                     (setq article (mail-header-id header)))
4006                    (t
4007                     ;; It is an extracted pseudo-article.
4008                     (setq article 'pseudo)
4009                     (gnus-request-pseudo-article header))))
4010
4011                 (let ((method (gnus-find-method-for-group
4012                                gnus-newsgroup-name)))
4013                   (when (and (eq (car method) 'nneething)
4014                              (vectorp header))
4015                     (let ((dir (expand-file-name
4016                                 (mail-header-subject header)
4017                                 (file-name-as-directory
4018                                  (or (cadr (assq 'nneething-address method))
4019                                      (nth 1 method))))))
4020                       (when (file-directory-p dir)
4021                         (setq article 'nneething)
4022                         (gnus-group-enter-directory dir))))))))
4023
4024           (cond
4025            ;; Refuse to select canceled articles.
4026            ((and (numberp article)
4027                  gnus-summary-buffer
4028                  (get-buffer gnus-summary-buffer)
4029                  (gnus-buffer-exists-p gnus-summary-buffer)
4030                  (eq (cdr (save-excursion
4031                             (set-buffer gnus-summary-buffer)
4032                             (assq article gnus-newsgroup-reads)))
4033                      gnus-canceled-mark))
4034             nil)
4035            ;; We first check `gnus-original-article-buffer'.
4036            ((and (get-buffer gnus-original-article-buffer)
4037                  (numberp article)
4038                  (save-excursion
4039                    (set-buffer gnus-original-article-buffer)
4040                    (and (equal (car gnus-original-article) group)
4041                         (eq (cdr gnus-original-article) article))))
4042             (insert-buffer-substring gnus-original-article-buffer)
4043             'article)
4044            ;; Check the backlog.
4045            ((and gnus-keep-backlog
4046                  (gnus-backlog-request-article group article (current-buffer)))
4047             'article)
4048            ;; Check asynchronous pre-fetch.
4049            ((gnus-async-request-fetched-article group article (current-buffer))
4050             (gnus-async-prefetch-next group article gnus-summary-buffer)
4051             (when (and (numberp article) gnus-keep-backlog)
4052               (gnus-backlog-enter-article group article (current-buffer)))
4053             'article)
4054            ;; Check the cache.
4055            ((and gnus-use-cache
4056                  (numberp article)
4057                  (gnus-cache-request-article article group))
4058             'article)
4059            ;; Get the article and put into the article buffer.
4060            ((or (stringp article)
4061                 (numberp article))
4062             (let ((gnus-override-method gnus-override-method)
4063                   (methods (and (stringp article) 
4064                                 gnus-refer-article-method))
4065                   result
4066                   (buffer-read-only nil))
4067               (if (or (not (listp methods))
4068                       (and (symbolp (car methods))
4069                            (assq (car methods) nnoo-definition-alist)))
4070                   (setq methods (list methods)))
4071               (when (and (null gnus-override-method)
4072                          methods)
4073                 (setq gnus-override-method (pop methods)))
4074               (while (not result)
4075                 (when (eq gnus-override-method 'current)
4076                   (setq gnus-override-method gnus-current-select-method))
4077                 (erase-buffer)
4078                 (gnus-kill-all-overlays)
4079                 (let ((gnus-newsgroup-name group))
4080                   (gnus-check-group-server))
4081                 (when (gnus-request-article article group (current-buffer))
4082                   (when (numberp article)
4083                     (gnus-async-prefetch-next group article 
4084                                               gnus-summary-buffer)
4085                     (when gnus-keep-backlog
4086                       (gnus-backlog-enter-article
4087                        group article (current-buffer))))
4088                   (setq result 'article))
4089                 (if (not result)
4090                     (if methods
4091                         (setq gnus-override-method (pop methods))
4092                       (setq result 'done))))
4093               (and (eq result 'article) 'article)))
4094            ;; It was a pseudo.
4095            (t article)))
4096
4097       ;; Associate this article with the current summary buffer.
4098       (setq gnus-article-current-summary gnus-summary-buffer)
4099
4100       ;; Take the article from the original article buffer
4101       ;; and place it in the buffer it's supposed to be in.
4102       (when (and (get-buffer gnus-article-buffer)
4103                  (equal (buffer-name (current-buffer))
4104                         (buffer-name (get-buffer gnus-article-buffer))))
4105         (save-excursion
4106           (if (get-buffer gnus-original-article-buffer)
4107               (set-buffer gnus-original-article-buffer)
4108             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4109             (buffer-disable-undo)
4110             (setq major-mode 'gnus-original-article-mode)
4111             (setq buffer-read-only t))
4112           (let (buffer-read-only)
4113             (erase-buffer)
4114             (insert-buffer-substring gnus-article-buffer))
4115           (setq gnus-original-article (cons group article)))
4116
4117         ;; Decode charsets.
4118         (run-hooks 'gnus-article-decode-hook)
4119         ;; Mark article as decoded or not.
4120         (setq gnus-article-decoded-p gnus-article-decode-hook))
4121
4122       ;; Update sparse articles.
4123       (when (and do-update-line
4124                  (or (numberp article)
4125                      (stringp article)))
4126         (let ((buf (current-buffer)))
4127           (set-buffer gnus-summary-buffer)
4128           (gnus-summary-update-article do-update-line sparse-header)
4129           (gnus-summary-goto-subject do-update-line nil t)
4130           (set-window-point (get-buffer-window (current-buffer) t)
4131                             (point))
4132           (set-buffer buf))))))
4133
4134 ;;;
4135 ;;; Article editing
4136 ;;;
4137
4138 (defcustom gnus-article-edit-mode-hook nil
4139   "Hook run in article edit mode buffers."
4140   :group 'gnus-article-various
4141   :type 'hook)
4142
4143 (defvar gnus-article-edit-done-function nil)
4144
4145 (defvar gnus-article-edit-mode-map nil)
4146
4147 ;; Should we be using derived.el for this?
4148 (unless gnus-article-edit-mode-map
4149   (setq gnus-article-edit-mode-map (make-sparse-keymap))
4150   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
4151
4152   (gnus-define-keys gnus-article-edit-mode-map
4153     "\C-c\C-c" gnus-article-edit-done
4154     "\C-c\C-k" gnus-article-edit-exit)
4155
4156   (gnus-define-keys (gnus-article-edit-wash-map
4157                      "\C-c\C-w" gnus-article-edit-mode-map)
4158     "f" gnus-article-edit-full-stops))
4159
4160 (defun gnus-article-edit-mode ()
4161   "Major mode for editing articles.
4162 This is an extended text-mode.
4163
4164 \\{gnus-article-edit-mode-map}"
4165   (interactive)
4166   (setq major-mode 'gnus-article-edit-mode)
4167   (setq mode-name "Article Edit")
4168   (use-local-map gnus-article-edit-mode-map)
4169   (make-local-variable 'gnus-article-edit-done-function)
4170   (make-local-variable 'gnus-prev-winconf)
4171   (setq buffer-read-only nil)
4172   (buffer-enable-undo)
4173   (widen)
4174   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
4175
4176 (defun gnus-article-edit (&optional force)
4177   "Edit the current article.
4178 This will have permanent effect only in mail groups.
4179 If FORCE is non-nil, allow editing of articles even in read-only
4180 groups."
4181   (interactive "P")
4182   (when (and (not force)
4183              (gnus-group-read-only-p))
4184     (error "The current newsgroup does not support article editing"))
4185   (gnus-article-date-original)
4186   (gnus-article-edit-article
4187    'ignore
4188    `(lambda (no-highlight)
4189       'ignore
4190       (gnus-summary-edit-article-done
4191        ,(or (mail-header-references gnus-current-headers) "")
4192        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
4193
4194 (defun gnus-article-edit-article (start-func exit-func)
4195   "Start editing the contents of the current article buffer."
4196   (let ((winconf (current-window-configuration)))
4197     (set-buffer gnus-article-buffer)
4198     (gnus-article-edit-mode)
4199     (funcall start-func)
4200     (gnus-configure-windows 'edit-article)
4201     (setq gnus-article-edit-done-function exit-func)
4202     (setq gnus-prev-winconf winconf)
4203     (gnus-message 6 "C-c C-c to end edits")))
4204
4205 (defun gnus-article-edit-done (&optional arg)
4206   "Update the article edits and exit."
4207   (interactive "P")
4208   (let ((func gnus-article-edit-done-function)
4209         (buf (current-buffer))
4210         (start (window-start)))
4211     (gnus-article-edit-exit)
4212     (save-excursion
4213       (set-buffer buf)
4214       (let ((buffer-read-only nil))
4215         (funcall func arg))
4216       ;; The cache and backlog have to be flushed somewhat.
4217       (when gnus-keep-backlog
4218         (gnus-backlog-remove-article
4219          (car gnus-article-current) (cdr gnus-article-current)))
4220       ;; Flush original article as well.
4221       (save-excursion
4222         (when (get-buffer gnus-original-article-buffer)
4223           (set-buffer gnus-original-article-buffer)
4224           (setq gnus-original-article nil)))
4225       (when gnus-use-cache
4226         (gnus-cache-update-article
4227          (car gnus-article-current) (cdr gnus-article-current))))
4228     (set-buffer buf)
4229     (set-window-start (get-buffer-window buf) start)
4230     (set-window-point (get-buffer-window buf) (point))))
4231
4232 (defun gnus-article-edit-exit ()
4233   "Exit the article editing without updating."
4234   (interactive)
4235   ;; We remove all text props from the article buffer.
4236   (let ((buf (buffer-substring-no-properties (point-min) (point-max)))
4237         (curbuf (current-buffer))
4238         (p (point))
4239         (window-start (window-start)))
4240     (erase-buffer)
4241     (insert buf)
4242     (let ((winconf gnus-prev-winconf))
4243       (gnus-article-mode)
4244       (set-window-configuration winconf)
4245       ;; Tippy-toe some to make sure that point remains where it was.
4246       (save-current-buffer
4247         (set-buffer curbuf)
4248         (set-window-start (get-buffer-window (current-buffer)) window-start)
4249         (goto-char p)))))
4250
4251 (defun gnus-article-edit-full-stops ()
4252   "Interactively repair spacing at end of sentences."
4253   (interactive)
4254   (save-excursion
4255     (goto-char (point-min))
4256     (search-forward-regexp "^$" nil t)
4257     (let ((case-fold-search nil))
4258       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
4259
4260 ;;;
4261 ;;; Article highlights
4262 ;;;
4263
4264 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
4265
4266 ;;; Internal Variables:
4267
4268 (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\\)\\)"
4269   "Regular expression that matches URLs."
4270   :group 'gnus-article-buttons
4271   :type 'regexp)
4272
4273 (defcustom gnus-button-alist
4274   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
4275      0 t gnus-button-message-id 2)
4276     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
4277     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
4278      1 t
4279      gnus-button-fetch-group 4)
4280     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
4281     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
4282      t gnus-button-message-id 3)
4283     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
4284     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
4285     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
4286     ;; This is how URLs _should_ be embedded in text...
4287     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
4288     ;; Raw URLs.
4289     (,gnus-button-url-regexp 0 t browse-url 0))
4290   "*Alist of regexps matching buttons in article bodies.
4291
4292 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
4293 REGEXP: is the string matching text around the button,
4294 BUTTON: is the number of the regexp grouping actually matching the button,
4295 FORM: is a lisp expression which must eval to true for the button to
4296 be added,
4297 CALLBACK: is the function to call when the user push this button, and each
4298 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
4299
4300 CALLBACK can also be a variable, in that case the value of that
4301 variable it the real callback function."
4302   :group 'gnus-article-buttons
4303   :type '(repeat (list regexp
4304                        (integer :tag "Button")
4305                        (sexp :tag "Form")
4306                        (function :tag "Callback")
4307                        (repeat :tag "Par"
4308                                :inline t
4309                                (integer :tag "Regexp group")))))
4310
4311 (defcustom gnus-header-button-alist
4312   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
4313      0 t gnus-button-message-id 0)
4314     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
4315     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
4316      0 t gnus-button-mailto 0)
4317     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
4318     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
4319     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
4320     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
4321      gnus-button-message-id 3))
4322   "*Alist of headers and regexps to match buttons in article heads.
4323
4324 This alist is very similar to `gnus-button-alist', except that each
4325 alist has an additional HEADER element first in each entry:
4326
4327 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
4328
4329 HEADER is a regexp to match a header.  For a fuller explanation, see
4330 `gnus-button-alist'."
4331   :group 'gnus-article-buttons
4332   :group 'gnus-article-headers
4333   :type '(repeat (list (regexp :tag "Header")
4334                        regexp
4335                        (integer :tag "Button")
4336                        (sexp :tag "Form")
4337                        (function :tag "Callback")
4338                        (repeat :tag "Par"
4339                                :inline t
4340                                (integer :tag "Regexp group")))))
4341
4342 (defvar gnus-button-regexp nil)
4343 (defvar gnus-button-marker-list nil)
4344 ;; Regexp matching any of the regexps from `gnus-button-alist'.
4345
4346 (defvar gnus-button-last nil)
4347 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
4348
4349 ;;; Commands:
4350
4351 (defun gnus-article-push-button (event)
4352   "Check text under the mouse pointer for a callback function.
4353 If the text under the mouse pointer has a `gnus-callback' property,
4354 call it with the value of the `gnus-data' text property."
4355   (interactive "e")
4356   (set-buffer (window-buffer (posn-window (event-start event))))
4357   (let* ((pos (posn-point (event-start event)))
4358          (data (get-text-property pos 'gnus-data))
4359          (fun (get-text-property pos 'gnus-callback)))
4360     (goto-char pos)
4361     (when fun
4362       (funcall fun data))))
4363
4364 (defun gnus-article-press-button ()
4365   "Check text at point for a callback function.
4366 If the text at point has a `gnus-callback' property,
4367 call it with the value of the `gnus-data' text property."
4368   (interactive)
4369   (let* ((data (get-text-property (point) 'gnus-data))
4370          (fun (get-text-property (point) 'gnus-callback)))
4371     (when fun
4372       (funcall fun data))))
4373
4374 (defun gnus-article-highlight (&optional force)
4375   "Highlight current article.
4376 This function calls `gnus-article-highlight-headers',
4377 `gnus-article-highlight-citation',
4378 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4379 do the highlighting.  See the documentation for those functions."
4380   (interactive (list 'force))
4381   (gnus-article-highlight-headers)
4382   (gnus-article-highlight-citation force)
4383   (gnus-article-highlight-signature)
4384   (gnus-article-add-buttons force)
4385   (gnus-article-add-buttons-to-head))
4386
4387 (defun gnus-article-highlight-some (&optional force)
4388   "Highlight current article.
4389 This function calls `gnus-article-highlight-headers',
4390 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4391 do the highlighting.  See the documentation for those functions."
4392   (interactive (list 'force))
4393   (gnus-article-highlight-headers)
4394   (gnus-article-highlight-signature)
4395   (gnus-article-add-buttons))
4396
4397 (defun gnus-article-highlight-headers ()
4398   "Highlight article headers as specified by `gnus-header-face-alist'."
4399   (interactive)
4400   (save-excursion
4401     (set-buffer gnus-article-buffer)
4402     (save-restriction
4403       (let ((alist gnus-header-face-alist)
4404             (buffer-read-only nil)
4405             (case-fold-search t)
4406             (inhibit-point-motion-hooks t)
4407             entry regexp header-face field-face from hpoints fpoints)
4408         (article-narrow-to-head)
4409         (while (setq entry (pop alist))
4410           (goto-char (point-min))
4411           (setq regexp (concat "^\\("
4412                                (if (string-equal "" (nth 0 entry))
4413                                    "[^\t ]"
4414                                  (nth 0 entry))
4415                                "\\)")
4416                 header-face (nth 1 entry)
4417                 field-face (nth 2 entry))
4418           (while (and (re-search-forward regexp nil t)
4419                       (not (eobp)))
4420             (beginning-of-line)
4421             (setq from (point))
4422             (unless (search-forward ":" nil t)
4423               (forward-char 1))
4424             (when (and header-face
4425                        (not (memq (point) hpoints)))
4426               (push (point) hpoints)
4427               (gnus-put-text-property from (point) 'face header-face))
4428             (when (and field-face
4429                        (not (memq (setq from (point)) fpoints)))
4430               (push from fpoints)
4431               (if (re-search-forward "^[^ \t]" nil t)
4432                   (forward-char -2)
4433                 (goto-char (point-max)))
4434               (gnus-put-text-property from (point) 'face field-face))))))))
4435
4436 (defun gnus-article-highlight-signature ()
4437   "Highlight the signature in an article.
4438 It does this by highlighting everything after
4439 `gnus-signature-separator' using `gnus-signature-face'."
4440   (interactive)
4441   (save-excursion
4442     (set-buffer gnus-article-buffer)
4443     (let ((buffer-read-only nil)
4444           (inhibit-point-motion-hooks t))
4445       (save-restriction
4446         (when (and gnus-signature-face
4447                    (gnus-article-narrow-to-signature))
4448           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
4449                             'face gnus-signature-face)
4450           (widen)
4451           (gnus-article-search-signature)
4452           (let ((start (match-beginning 0))
4453                 (end (set-marker (make-marker) (1+ (match-end 0)))))
4454             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
4455                                      end)))))))
4456
4457 (defun gnus-button-in-region-p (b e prop)
4458   "Say whether PROP exists in the region."
4459   (text-property-not-all b e prop nil))
4460
4461 (defun gnus-article-add-buttons (&optional force)
4462   "Find external references in the article and make buttons of them.
4463 \"External references\" are things like Message-IDs and URLs, as
4464 specified by `gnus-button-alist'."
4465   (interactive (list 'force))
4466   (save-excursion
4467     (set-buffer gnus-article-buffer)
4468     (let ((buffer-read-only nil)
4469           (inhibit-point-motion-hooks t)
4470           (case-fold-search t)
4471           (alist gnus-button-alist)
4472           beg entry regexp)
4473       ;; Remove all old markers.
4474       (let (marker entry new-list)
4475         (while (setq marker (pop gnus-button-marker-list))
4476           (if (or (< marker (point-min)) (>= marker (point-max)))
4477               (push marker new-list)
4478             (goto-char marker)
4479             (when (setq entry (gnus-button-entry))
4480               (put-text-property (match-beginning (nth 1 entry))
4481                                  (match-end (nth 1 entry))
4482                                  'gnus-callback nil))
4483             (set-marker marker nil)))
4484         (setq gnus-button-marker-list new-list))
4485       ;; We skip the headers.
4486       (article-goto-body)
4487       (setq beg (point))
4488       (while (setq entry (pop alist))
4489         (setq regexp (car entry))
4490         (goto-char beg)
4491         (while (re-search-forward regexp nil t)
4492           (let* ((start (and entry (match-beginning (nth 1 entry))))
4493                  (end (and entry (match-end (nth 1 entry))))
4494                  (from (match-beginning 0)))
4495             (when (and (or (eq t (nth 2 entry))
4496                            (eval (nth 2 entry)))
4497                        (not (gnus-button-in-region-p
4498                              start end 'gnus-callback)))
4499               ;; That optional form returned non-nil, so we add the
4500               ;; button.
4501               (gnus-article-add-button
4502                start end 'gnus-button-push
4503                (car (push (set-marker (make-marker) from)
4504                           gnus-button-marker-list))))))))))
4505
4506 ;; Add buttons to the head of an article.
4507 (defun gnus-article-add-buttons-to-head ()
4508   "Add buttons to the head of the article."
4509   (interactive)
4510   (save-excursion
4511     (set-buffer gnus-article-buffer)
4512     (save-restriction
4513       (let ((buffer-read-only nil)
4514             (inhibit-point-motion-hooks t)
4515             (case-fold-search t)
4516             (alist gnus-header-button-alist)
4517             entry beg end)
4518         (article-narrow-to-head)
4519         (while alist
4520           ;; Each alist entry.
4521           (setq entry (car alist)
4522                 alist (cdr alist))
4523           (goto-char (point-min))
4524           (while (re-search-forward (car entry) nil t)
4525             ;; Each header matching the entry.
4526             (setq beg (match-beginning 0))
4527             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
4528                                (match-beginning 0))
4529                           (point-max)))
4530             (goto-char beg)
4531             (while (re-search-forward (nth 1 entry) end t)
4532               ;; Each match within a header.
4533               (let* ((entry (cdr entry))
4534                      (start (match-beginning (nth 1 entry)))
4535                      (end (match-end (nth 1 entry)))
4536                      (form (nth 2 entry)))
4537                 (goto-char (match-end 0))
4538                 (when (eval form)
4539                   (gnus-article-add-button
4540                    start end (nth 3 entry)
4541                    (buffer-substring (match-beginning (nth 4 entry))
4542                                      (match-end (nth 4 entry)))))))
4543             (goto-char end)))))))
4544
4545 ;;; External functions:
4546
4547 (defun gnus-article-add-button (from to fun &optional data)
4548   "Create a button between FROM and TO with callback FUN and data DATA."
4549   (when gnus-article-button-face
4550     (gnus-overlay-put (gnus-make-overlay from to)
4551                       'face gnus-article-button-face))
4552   (gnus-add-text-properties
4553    from to
4554    (nconc (and gnus-article-mouse-face
4555                (list gnus-mouse-face-prop gnus-article-mouse-face))
4556           (list 'gnus-callback fun)
4557           (and data (list 'gnus-data data))))
4558   (widget-convert-button 'link from to :action 'gnus-widget-press-button
4559                          :button-keymap gnus-widget-button-keymap))
4560
4561 ;;; Internal functions:
4562
4563 (defun gnus-article-set-globals ()
4564   (save-excursion
4565     (set-buffer gnus-summary-buffer)
4566     (gnus-set-global-variables)))
4567
4568 (defun gnus-signature-toggle (end)
4569   (save-excursion
4570     (set-buffer gnus-article-buffer)
4571     (let ((buffer-read-only nil)
4572           (inhibit-point-motion-hooks t))
4573       (if (get-text-property end 'invisible)
4574           (gnus-article-unhide-text end (point-max))
4575         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
4576
4577 (defun gnus-button-entry ()
4578   ;; Return the first entry in `gnus-button-alist' matching this place.
4579   (let ((alist gnus-button-alist)
4580         (entry nil))
4581     (while alist
4582       (setq entry (pop alist))
4583       (if (looking-at (car entry))
4584           (setq alist nil)
4585         (setq entry nil)))
4586     entry))
4587
4588 (defun gnus-button-push (marker)
4589   ;; Push button starting at MARKER.
4590   (save-excursion
4591     (goto-char marker)
4592     (let* ((entry (gnus-button-entry))
4593            (inhibit-point-motion-hooks t)
4594            (fun (nth 3 entry))
4595            (args (mapcar (lambda (group)
4596                            (let ((string (match-string group)))
4597                              (gnus-set-text-properties
4598                               0 (length string) nil string)
4599                              string))
4600                          (nthcdr 4 entry))))
4601       (cond
4602        ((fboundp fun)
4603         (apply fun args))
4604        ((and (boundp fun)
4605              (fboundp (symbol-value fun)))
4606         (apply (symbol-value fun) args))
4607        (t
4608         (gnus-message 1 "You must define `%S' to use this button"
4609                       (cons fun args)))))))
4610
4611 (defun gnus-button-message-id (message-id)
4612   "Fetch MESSAGE-ID."
4613   (save-excursion
4614     (set-buffer gnus-summary-buffer)
4615     (gnus-summary-refer-article message-id)))
4616
4617 (defun gnus-button-fetch-group (address)
4618   "Fetch GROUP specified by ADDRESS."
4619   (if (not (string-match "[:/]" address))
4620       ;; This is just a simple group url.
4621       (gnus-group-read-ephemeral-group address gnus-select-method)
4622     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
4623                            address))
4624         (error "Can't parse %s" address)
4625       (gnus-group-read-ephemeral-group
4626        (match-string 4 address)
4627        `(nntp ,(match-string 1 address)
4628               (nntp-address ,(match-string 1 address))
4629               (nntp-port-number ,(if (match-end 3)
4630                                      (match-string 3 address)
4631                                    "nntp")))))))
4632
4633 (defun gnus-url-parse-query-string (query &optional downcase)
4634   (let (retval pairs cur key val)
4635     (setq pairs (split-string query "&"))
4636     (while pairs
4637       (setq cur (car pairs)
4638             pairs (cdr pairs))
4639       (if (not (string-match "=" cur))
4640           nil                           ; Grace
4641         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
4642               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
4643         (if downcase
4644             (setq key (downcase key)))
4645         (setq cur (assoc key retval))
4646         (if cur
4647             (setcdr cur (cons val (cdr cur)))
4648           (setq retval (cons (list key val) retval)))))
4649     retval))
4650
4651 (defun gnus-url-unhex (x)
4652   (if (> x ?9)
4653       (if (>= x ?a)
4654           (+ 10 (- x ?a))
4655         (+ 10 (- x ?A)))
4656     (- x ?0)))
4657
4658 (defun gnus-url-unhex-string (str &optional allow-newlines)
4659   "Remove %XXX embedded spaces, etc in a url.
4660 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
4661 decoding of carriage returns and line feeds in the string, which is normally
4662 forbidden in URL encoding."
4663   (setq str (or str ""))
4664   (let ((tmp "")
4665         (case-fold-search t))
4666     (while (string-match "%[0-9a-f][0-9a-f]" str)
4667       (let* ((start (match-beginning 0))
4668              (ch1 (gnus-url-unhex (elt str (+ start 1))))
4669              (code (+ (* 16 ch1)
4670                       (gnus-url-unhex (elt str (+ start 2))))))
4671         (setq tmp (concat
4672                    tmp (substring str 0 start)
4673                    (cond
4674                     (allow-newlines
4675                      (char-to-string code))
4676                     ((or (= code ?\n) (= code ?\r))
4677                      " ")
4678                     (t (char-to-string code))))
4679               str (substring str (match-end 0)))))
4680     (setq tmp (concat tmp str))
4681     tmp))
4682
4683 (defun gnus-url-mailto (url)
4684   ;; Send mail to someone
4685   (when (string-match "mailto:/*\\(.*\\)" url)
4686     (setq url (substring url (match-beginning 1) nil)))
4687   (let (to args subject func)
4688     (if (string-match (regexp-quote "?") url)
4689         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
4690               args (gnus-url-parse-query-string
4691                     (substring url (match-end 0) nil) t))
4692       (setq to (gnus-url-unhex-string url)))
4693     (setq args (cons (list "to" to) args)
4694           subject (cdr-safe (assoc "subject" args)))
4695     (message-mail)
4696     (while args
4697       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
4698       (if (fboundp func)
4699           (funcall func)
4700         (message-position-on-field (caar args)))
4701       (insert (mapconcat 'identity (cdar args) ", "))
4702       (setq args (cdr args)))
4703     (if subject
4704         (message-goto-body)
4705       (message-goto-subject))))
4706
4707 (defun gnus-button-mailto (address)
4708   "Mail to ADDRESS."
4709   (set-buffer (gnus-copy-article-buffer))
4710   (message-reply address))
4711
4712 (defalias 'gnus-button-reply 'message-reply)
4713
4714 (defun gnus-button-embedded-url (address)
4715   "Activate ADDRESS with `browse-url'."
4716   (browse-url (gnus-strip-whitespace address)))
4717
4718 ;;; Next/prev buttons in the article buffer.
4719
4720 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
4721 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
4722
4723 (defvar gnus-prev-page-map nil)
4724 (unless gnus-prev-page-map
4725   (setq gnus-prev-page-map (make-sparse-keymap))
4726   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
4727   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
4728
4729 (defun gnus-insert-prev-page-button ()
4730   (let ((buffer-read-only nil))
4731     (gnus-eval-format
4732      gnus-prev-page-line-format nil
4733      `(gnus-prev t local-map ,gnus-prev-page-map
4734                  gnus-callback gnus-article-button-prev-page
4735                  article-type annotation))))
4736
4737 (defvar gnus-next-page-map nil)
4738 (unless gnus-next-page-map
4739   (setq gnus-next-page-map (make-keymap))
4740   (suppress-keymap gnus-prev-page-map)
4741   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
4742   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
4743
4744 (defun gnus-button-next-page ()
4745   "Go to the next page."
4746   (interactive)
4747   (let ((win (selected-window)))
4748     (select-window (get-buffer-window gnus-article-buffer t))
4749     (gnus-article-next-page)
4750     (select-window win)))
4751
4752 (defun gnus-button-prev-page ()
4753   "Go to the prev page."
4754   (interactive)
4755   (let ((win (selected-window)))
4756     (select-window (get-buffer-window gnus-article-buffer t))
4757     (gnus-article-prev-page)
4758     (select-window win)))
4759
4760 (defun gnus-insert-next-page-button ()
4761   (let ((buffer-read-only nil))
4762     (gnus-eval-format gnus-next-page-line-format nil
4763                       `(gnus-next
4764                         t local-map ,gnus-next-page-map
4765                         gnus-callback gnus-article-button-next-page
4766                         article-type annotation))))
4767
4768 (defun gnus-article-button-next-page (arg)
4769   "Go to the next page."
4770   (interactive "P")
4771   (let ((win (selected-window)))
4772     (select-window (get-buffer-window gnus-article-buffer t))
4773     (gnus-article-next-page)
4774     (select-window win)))
4775
4776 (defun gnus-article-button-prev-page (arg)
4777   "Go to the prev page."
4778   (interactive "P")
4779   (let ((win (selected-window)))
4780     (select-window (get-buffer-window gnus-article-buffer t))
4781     (gnus-article-prev-page)
4782     (select-window win)))
4783
4784 (defvar gnus-decode-header-methods
4785   '(mail-decode-encoded-word-region)
4786   "List of methods used to decode headers.
4787
4788 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
4789 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
4790 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
4791 whose names match REGEXP.
4792
4793 For example:
4794 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
4795  mail-decode-encoded-word-region
4796  (\"chinese\" . rfc1843-decode-region))
4797 ")
4798
4799 (defvar gnus-decode-header-methods-cache nil)
4800
4801 (defun gnus-multi-decode-header (start end)
4802   "Apply the functions from `gnus-encoded-word-methods' that match."
4803   (unless (and gnus-decode-header-methods-cache
4804                (eq gnus-newsgroup-name
4805                    (car gnus-decode-header-methods-cache)))
4806     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
4807     (mapcar (lambda (x)
4808               (if (symbolp x)
4809                   (nconc gnus-decode-header-methods-cache (list x))
4810                 (if (and gnus-newsgroup-name
4811                          (string-match (car x) gnus-newsgroup-name))
4812                     (nconc gnus-decode-header-methods-cache
4813                            (list (cdr x))))))
4814           gnus-decode-header-methods))
4815   (let ((xlist gnus-decode-header-methods-cache))
4816     (pop xlist)
4817     (save-restriction
4818       (narrow-to-region start end)
4819       (while xlist
4820         (funcall (pop xlist) (point-min) (point-max))))))
4821
4822 ;;;
4823 ;;; Treatment top-level handling.
4824 ;;;
4825
4826 (defun gnus-treat-article (condition &optional part-number total-parts type)
4827   (let ((length (- (point-max) (point-min)))
4828         (alist gnus-treatment-function-alist)
4829         (article-goto-body-goes-to-point-min-p t)
4830         (treated-type
4831          (or (not type)
4832              (catch 'found
4833                (let ((list gnus-article-treat-types))
4834                  (while list
4835                    (when (string-match (pop list) type)
4836                      (throw 'found t)))))))
4837         (highlightp (gnus-visual-p 'article-highlight 'highlight))
4838         val elem)
4839     (gnus-run-hooks 'gnus-part-display-hook)
4840     (while (setq elem (pop alist))
4841       (setq val
4842             (save-excursion
4843               (if (gnus-buffer-live-p gnus-summary-buffer)
4844                   (set-buffer gnus-summary-buffer))
4845               (symbol-value (car elem))))
4846       (when (and (or (consp val)
4847                      treated-type)
4848                  (gnus-treat-predicate val)
4849                  (or (not (get (car elem) 'highlight))
4850                      highlightp))
4851         (save-restriction
4852           (funcall (cadr elem)))))))
4853
4854 ;; Dynamic variables.
4855 (eval-when-compile
4856   (defvar part-number)
4857   (defvar total-parts)
4858   (defvar type)
4859   (defvar condition)
4860   (defvar length))
4861
4862 (defun gnus-treat-predicate (val)
4863   (cond
4864    ((null val)
4865     nil)
4866    ((and (listp val)
4867          (stringp (car val)))
4868     (apply 'gnus-or (mapcar `(lambda (s)
4869                                (string-match s ,(or gnus-newsgroup-name "")))
4870                             val)))
4871    ((listp val)
4872     (let ((pred (pop val)))
4873       (cond
4874        ((eq pred 'or)
4875         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
4876        ((eq pred 'and)
4877         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
4878        ((eq pred 'not)
4879         (not (gnus-treat-predicate (car val))))
4880        ((eq pred 'typep)
4881         (equal (car val) type))
4882        (t
4883         (error "%S is not a valid predicate" pred)))))
4884    (condition
4885     (eq condition val))
4886    ((eq val t)
4887     t)
4888    ((eq val 'head)
4889     nil)
4890    ((eq val 'last)
4891     (eq part-number total-parts))
4892    ((numberp val)
4893     (< length val))
4894    (t
4895     (error "%S is not a valid value" val))))
4896
4897 (gnus-ems-redefine)
4898
4899 (provide 'gnus-art)
4900
4901 (run-hooks 'gnus-art-load-hook)
4902
4903 ;;; gnus-art.el ends here