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