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