Sync up with Pterodactyl Gnus v0.97.
[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       (setq type (mime-entity-content-type entity)
2940             type (format "%s/%s"
2941                          (mime-content-type-primary-type type)
2942                          (mime-content-type-subtype type)))
2943       (if (string-equal type "message/rfc822")
2944           (save-restriction
2945             (narrow-to-region (point) (point-max))
2946             (gnus-article-prepare-mime-display number)
2947             (goto-char (point-max)))
2948         (setq ids (length (mime-entity-node-id entity))
2949               entity (get-text-property next 'mime-view-entity)
2950               number (1+ number))
2951         (save-restriction
2952           (narrow-to-region (point) next)
2953           (if (or (null entity)
2954                   (< (length (mime-entity-node-id entity)) ids))
2955               (gnus-treat-article 'last number number type)
2956             (gnus-treat-article t number nil type))
2957           (goto-char (point-max)))))
2958     (unless (eobp)
2959       (save-restriction
2960         (narrow-to-region (point) (point-max))
2961         (if entity
2962             (progn
2963               (setq type (mime-entity-content-type entity)
2964                     type (format "%s/%s"
2965                                  (mime-content-type-primary-type type)
2966                                  (mime-content-type-subtype type)))
2967               (if (string-equal type "message/rfc822")
2968                   (gnus-article-prepare-mime-display number)
2969                 (incf number)
2970                 (gnus-treat-article 'last number number type)))
2971           (gnus-treat-article t))))))
2972
2973 ;;;###autoload
2974 (defun gnus-article-prepare-display ()
2975   "Make the current buffer look like a nice article."
2976   (setq gnus-article-wash-types nil)
2977   (gnus-run-hooks 'gnus-tmp-internal-hook)
2978   (gnus-run-hooks 'gnus-article-prepare-hook)
2979   ;; Display message.
2980   (let (mime-display-header-hook)
2981     (funcall (if gnus-show-mime
2982                  (progn
2983                    (setq mime-message-structure gnus-current-headers)
2984                    (mime-buffer-entity-set-buffer-internal
2985                     mime-message-structure
2986                     gnus-original-article-buffer)
2987                    (mime-entity-set-representation-type-internal
2988                     mime-message-structure 'mime-buffer-entity)
2989                    (luna-send mime-message-structure
2990                               'initialize-instance
2991                               mime-message-structure)
2992                    gnus-article-display-method-for-mime)
2993                gnus-article-display-method-for-traditional)))
2994   ;; Associate this article with the current summary buffer.
2995   (setq gnus-article-current-summary gnus-summary-buffer)
2996   ;; Call the treatment functions.
2997   (let ((inhibit-read-only t)
2998         buffer-read-only)
2999     (save-restriction
3000       (widen)
3001       (if gnus-show-mime
3002           (gnus-article-prepare-mime-display)
3003         (narrow-to-region (goto-char (point-min))
3004                           (if (search-forward "\n\n" nil t)
3005                               (point)
3006                             (point-max)))
3007         (gnus-treat-article 'head)
3008         (put-text-property (point-min) (point-max) 'article-treated-header t)
3009         (goto-char (point-max))
3010         (widen)
3011         (narrow-to-region (point) (point-max))
3012         (gnus-treat-article t))
3013       (put-text-property (point-min) (point-max) 'read-only nil)))
3014   ;; Perform the article display hooks.  Incidentally, this hook is
3015   ;; an obsolete variable by now.
3016   (gnus-run-hooks 'gnus-article-display-hook))
3017
3018 (defun gnus-article-decode-article-as-default-mime-charset ()
3019   "Decode an article as `default-mime-charset'.  It won't work if the
3020 value of the variable `gnus-show-mime' is non-nil."
3021   (unless gnus-show-mime
3022     (decode-mime-charset-region (point-min) (point-max)
3023                                 (with-current-buffer gnus-summary-buffer
3024                                   default-mime-charset))))
3025
3026 ;;;
3027 ;;; Gnus MIME viewing functions
3028 ;;;
3029
3030 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3031   "The following specs can be used:
3032 %t  The MIME type
3033 %T  MIME type, along with additional info
3034 %n  The `name' parameter
3035 %d  The description, if any
3036 %l  The length of the encoded part
3037 %p  The part identifier number
3038 %e  Dots if the part isn't displayed")
3039
3040 (defvar gnus-mime-button-line-format-alist
3041   '((?t gnus-tmp-type ?s)
3042     (?T gnus-tmp-type-long ?s)
3043     (?n gnus-tmp-name ?s)
3044     (?d gnus-tmp-description ?s)
3045     (?p gnus-tmp-id ?s)
3046     (?l gnus-tmp-length ?d)
3047     (?e gnus-tmp-dots ?s)))
3048
3049 (defvar gnus-mime-button-commands
3050   '((gnus-article-press-button "\r" "Toggle Display")
3051     (gnus-mime-view-part "v" "View Interactively...")
3052     (gnus-mime-view-part-as-type "t" "View As Type...")
3053     (gnus-mime-save-part "o" "Save...")
3054     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
3055     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
3056     (gnus-mime-internalize-part "E" "View Internally")
3057     (gnus-mime-externalize-part "e" "View Externally")
3058     (gnus-mime-pipe-part "|" "Pipe To Command...")))
3059
3060 (defun gnus-article-mime-part-status ()
3061   (if gnus-article-mime-handle-alist-1
3062       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
3063     ""))
3064
3065 (defvar gnus-mime-button-map nil)
3066 (unless gnus-mime-button-map
3067   (setq gnus-mime-button-map (make-sparse-keymap))
3068   (set-keymap-parent gnus-mime-button-map gnus-article-mode-map)
3069   (define-key gnus-mime-button-map gnus-mouse-2 'gnus-article-push-button)
3070   (define-key gnus-mime-button-map gnus-down-mouse-3 'gnus-mime-button-menu)
3071   (mapcar (lambda (c)
3072             (define-key gnus-mime-button-map (cadr c) (car c)))
3073           gnus-mime-button-commands))
3074
3075 (defun gnus-mime-button-menu (event)
3076   "Construct a context-sensitive menu of MIME commands."
3077   (interactive "e")
3078   (save-excursion
3079     (let ((pos (event-start event)))
3080       (set-buffer (window-buffer (posn-window pos)))
3081       (goto-char (posn-point pos))
3082       (gnus-article-check-buffer)
3083       (let ((response (x-popup-menu
3084                        t `("MIME Part"
3085                            ("" ,@(mapcar (lambda (c)
3086                                            (cons (caddr c) (car c)))
3087                                          gnus-mime-button-commands))))))
3088         (if response
3089             (funcall response))))))
3090
3091 (defun gnus-mime-view-all-parts (&optional handles)
3092   "View all the MIME parts."
3093   (interactive)
3094   (save-current-buffer
3095     (set-buffer gnus-article-buffer)
3096     (let ((handles (or handles gnus-article-mime-handles))
3097           (mail-parse-charset gnus-newsgroup-charset)
3098           (mail-parse-ignored-charsets 
3099            (save-excursion (set-buffer gnus-summary-buffer)
3100                            gnus-newsgroup-ignored-charsets)))
3101       (if (stringp (car handles))
3102           (gnus-mime-view-all-parts (cdr handles))
3103         (mapcar 'mm-display-part handles)))))
3104
3105 (defun gnus-mime-save-part ()
3106   "Save the MIME part under point."
3107   (interactive)
3108   (gnus-article-check-buffer)
3109   (let ((data (get-text-property (point) 'gnus-data)))
3110     (mm-save-part data)))
3111
3112 (defun gnus-mime-pipe-part ()
3113   "Pipe the MIME part under point to a process."
3114   (interactive)
3115   (gnus-article-check-buffer)
3116   (let ((data (get-text-property (point) 'gnus-data)))
3117     (mm-pipe-part data)))
3118
3119 (defun gnus-mime-view-part ()
3120   "Interactively choose a viewing method for the MIME part under point."
3121   (interactive)
3122   (gnus-article-check-buffer)
3123   (let ((data (get-text-property (point) 'gnus-data)))
3124     (mm-interactively-view-part data)))
3125
3126 (defun gnus-mime-view-part-as-type ()
3127   "Choose a MIME media type, and view the part as such."
3128   (interactive
3129    (list (completing-read "View as MIME type: " mailcap-mime-types)))
3130   (gnus-article-check-buffer)
3131   (let ((handle (get-text-property (point) 'gnus-data)))
3132     (gnus-mm-display-part handle)))
3133
3134 (defun gnus-mime-copy-part (&optional handle)
3135   "Put the the MIME part under point into a new buffer."
3136   (interactive)
3137   (gnus-article-check-buffer)
3138   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3139          (contents (mm-get-part handle))|
3140          (base (file-name-nondirectory
3141                 (or
3142                  (mail-content-type-get (mm-handle-type handle) 'name)
3143                  (mail-content-type-get (mm-handle-type handle)
3144                                         'filename)
3145                  "*decoded*")))
3146          (buffer (generate-new-buffer base)))
3147     (switch-to-buffer buffer)
3148     (insert contents)
3149     ;; We do it this way to make `normal-mode' set the appropriate mode.
3150     (unwind-protect
3151         (progn
3152           (setq buffer-file-name (expand-file-name base))
3153           (normal-mode))
3154       (setq buffer-file-name nil))
3155     (goto-char (point-min))))
3156
3157 (defun gnus-mime-inline-part (&optional handle)
3158   "Insert the MIME part under point into the current buffer."
3159   (interactive)
3160   (gnus-article-check-buffer)
3161   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3162          contents
3163          (b (point))
3164          buffer-read-only)
3165     (if (mm-handle-undisplayer handle)
3166         (mm-remove-part handle)
3167       (setq contents (mm-get-part handle))
3168       (forward-line 2)
3169       (mm-insert-inline handle contents)
3170       (goto-char b))))
3171
3172 (defun gnus-mime-externalize-part (&optional handle)
3173   "View the MIME part under point with an external viewer."
3174   (interactive)
3175   (gnus-article-check-buffer)
3176   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3177          (mm-user-display-methods nil)
3178          (mm-inline-large-images nil)
3179          (mail-parse-charset gnus-newsgroup-charset)
3180          (mail-parse-ignored-charsets 
3181           (save-excursion (set-buffer gnus-summary-buffer)
3182                           gnus-newsgroup-ignored-charsets)))
3183     (if (mm-handle-undisplayer handle)
3184         (mm-remove-part handle)
3185       (mm-display-part handle))))
3186
3187 (defun gnus-mime-internalize-part (&optional handle)
3188   "View the MIME part under point with an internal viewer."
3189   (interactive)
3190   (gnus-article-check-buffer)
3191   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3192          (mm-user-display-methods '((".*" . inline)))
3193          (mm-inline-large-images t)
3194          (mail-parse-charset gnus-newsgroup-charset)
3195          (mail-parse-ignored-charsets 
3196           (save-excursion (set-buffer gnus-summary-buffer)
3197                           gnus-newsgroup-ignored-charsets)))
3198     (if (mm-handle-undisplayer handle)
3199         (mm-remove-part handle)
3200       (mm-display-part handle))))
3201
3202 (defun gnus-article-part-wrapper (n function)
3203   (save-current-buffer
3204     (set-buffer gnus-article-buffer)
3205     (when (> n (length gnus-article-mime-handle-alist))
3206       (error "No such part"))
3207     (gnus-article-goto-part n)
3208     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3209       (funcall function handle))))
3210
3211 (defun gnus-article-pipe-part (n)
3212   "Pipe MIME part N, which is the numerical prefix."
3213   (interactive "p")
3214   (gnus-article-part-wrapper n 'mm-pipe-part))
3215
3216 (defun gnus-article-save-part (n)
3217   "Save MIME part N, which is the numerical prefix."
3218   (interactive "p")
3219   (gnus-article-part-wrapper n 'mm-save-part))
3220
3221 (defun gnus-article-interactively-view-part (n)
3222   "View MIME part N interactively, which is the numerical prefix."
3223   (interactive "p")
3224   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3225
3226 (defun gnus-article-copy-part (n)
3227   "Copy MIME part N, which is the numerical prefix."
3228   (interactive "p")
3229   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3230
3231 (defun gnus-article-externalize-part (n)
3232   "View MIME part N externally, which is the numerical prefix."
3233   (interactive "p")
3234   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3235
3236 (defun gnus-article-inline-part (n)
3237   "Inline MIME part N, which is the numerical prefix."
3238   (interactive "p")
3239   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3240
3241 (defun gnus-article-view-part (n)
3242   "View MIME part N, which is the numerical prefix."
3243   (interactive "p")
3244   (save-current-buffer
3245     (set-buffer gnus-article-buffer)
3246     (when (> n (length gnus-article-mime-handle-alist))
3247       (error "No such part"))
3248     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3249       (when (gnus-article-goto-part n)
3250         (if (equal (car handle) "multipart/alternative")
3251             (gnus-article-press-button)
3252           (when (eq (gnus-mm-display-part handle) 'internal)
3253             (gnus-set-window-start)))))))
3254
3255 (defun gnus-mm-display-part (handle)
3256   "Display HANDLE and fix MIME button."
3257   (let ((id (get-text-property (point) 'gnus-part))
3258         (point (point))
3259         buffer-read-only)
3260     (forward-line 1)
3261     (prog1
3262         (let ((window (selected-window))
3263               (mail-parse-charset gnus-newsgroup-charset)
3264               (mail-parse-ignored-charsets 
3265                (save-excursion (set-buffer gnus-summary-buffer)
3266                                gnus-newsgroup-ignored-charsets)))
3267           (save-excursion
3268             (unwind-protect
3269                 (let ((win (get-buffer-window (current-buffer) t))
3270                       (beg (point)))
3271                   (when win
3272                     (select-window win))
3273                   (goto-char point)
3274                   (forward-line)
3275                   (if (mm-handle-displayed-p handle)
3276                       ;; This will remove the part.
3277                       (mm-display-part handle)
3278                     (save-restriction
3279                       (narrow-to-region (point) (1+ (point)))
3280                       (mm-display-part handle)
3281                       ;; We narrow to the part itself and
3282                       ;; then call the treatment functions.
3283                       (goto-char (point-min))
3284                       (forward-line 1)
3285                       (narrow-to-region (point) (point-max))
3286                       (gnus-treat-article
3287                        nil id
3288                        (1- (length gnus-article-mime-handles))
3289                        (mm-handle-media-type handle)))))
3290               (select-window window))))
3291       (goto-char point)
3292       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
3293       (gnus-insert-mime-button
3294        handle id (list (mm-handle-displayed-p handle)))
3295       (goto-char point))))
3296
3297 (defun gnus-article-goto-part (n)
3298   "Go to MIME part N."
3299   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
3300     (when point
3301       (goto-char point))))
3302
3303 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
3304   (let ((gnus-tmp-name
3305          (or (mail-content-type-get (mm-handle-type handle)
3306                                     'name)
3307              (mail-content-type-get (mm-handle-disposition handle)
3308                                     'filename)
3309              ""))
3310         (gnus-tmp-type (mm-handle-media-type handle))
3311         (gnus-tmp-description
3312          (mail-decode-encoded-word-string (or (mm-handle-description handle)
3313                                               "")))
3314         (gnus-tmp-dots
3315          (if (if displayed (car displayed)
3316                (mm-handle-displayed-p handle))
3317              "" "..."))
3318         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
3319                            (buffer-size)))
3320         gnus-tmp-type-long b e)
3321     (when (string-match ".*/" gnus-tmp-name)
3322       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
3323     (setq gnus-tmp-type-long (concat gnus-tmp-type
3324                                      (and (not (equal gnus-tmp-name ""))
3325                                           (concat "; " gnus-tmp-name))))
3326     (or (equal gnus-tmp-description "")
3327         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
3328     (unless (bolp)
3329       (insert "\n"))
3330     (setq b (point))
3331     (gnus-eval-format
3332      gnus-mime-button-line-format gnus-mime-button-line-format-alist
3333      `(local-map ,gnus-mime-button-map
3334                  keymap ,gnus-mime-button-map
3335                  gnus-callback gnus-mm-display-part
3336                  gnus-part ,gnus-tmp-id
3337                  article-type annotation
3338                  gnus-data ,handle))
3339     (setq e (point))
3340     (widget-convert-button 'link b e
3341                            :mime-handle handle
3342                            :action 'gnus-widget-press-button
3343                            :button-keymap gnus-mime-button-map
3344                            :help-echo
3345                            (lambda (widget)
3346                              ;; Needed to properly clear the message
3347                              ;; due to a bug in wid-edit
3348                              (setq help-echo-owns-message t)
3349                              (format
3350                               "Click to %s the MIME part; %s for more options"
3351                               (if (mm-handle-displayed-p
3352                                    (widget-get widget :mime-handle))
3353                                   "hide" "show")
3354                               (if gnus-xemacs "button3" "mouse-3"))))))
3355
3356 (defun gnus-widget-press-button (elems el)
3357   (goto-char (widget-get elems :from))
3358   (gnus-article-press-button))
3359
3360 (defvar gnus-displaying-mime nil)
3361
3362 (defun gnus-display-mime (&optional ihandles)
3363   "Display the MIME parts."
3364   (save-excursion
3365     (save-selected-window
3366       (let ((window (get-buffer-window gnus-article-buffer))
3367             (point (point)))
3368         (when window
3369           (select-window window)
3370           ;; We have to do this since selecting the window
3371           ;; may change the point.  So we set the window point.
3372           (set-window-point window point)))
3373       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3374              buffer-read-only handle name type b e display)
3375         (when (and (not ihandles)
3376                    (not gnus-displaying-mime))
3377           ;; Top-level call; we clean up.
3378           (when gnus-article-mime-handles
3379             (mm-destroy-parts gnus-article-mime-handles)
3380             (setq gnus-article-mime-handle-alist nil)) ;; A trick.
3381           (setq gnus-article-mime-handles handles)
3382           ;; We allow users to glean info from the handles.
3383           (when gnus-article-mime-part-function
3384             (gnus-mime-part-function handles)))
3385         (if (and handles
3386                  (or (not (stringp (car handles)))
3387                      (cdr handles)))
3388             (progn
3389               (when (and (not ihandles)
3390                          (not gnus-displaying-mime))
3391                 ;; Clean up for mime parts.
3392                 (article-goto-body)
3393                 (delete-region (point) (point-max)))
3394               (let ((gnus-displaying-mime t))
3395                 (gnus-mime-display-part handles)))
3396           (save-restriction
3397             (article-goto-body)
3398             (narrow-to-region (point) (point-max))
3399             (gnus-treat-article nil 1 1)
3400             (widen)))
3401         (if (not ihandles)
3402             ;; Highlight the headers.
3403             (save-excursion
3404               (save-restriction
3405                 (article-goto-body)
3406                 (narrow-to-region (point-min) (point))
3407                 (gnus-treat-article 'head))))))))
3408
3409 (defvar gnus-mime-display-multipart-as-mixed nil)
3410
3411 (defun gnus-mime-display-part (handle)
3412   (cond
3413    ;; Single part.
3414    ((not (stringp (car handle)))
3415     (gnus-mime-display-single handle))
3416    ;; User-defined multipart
3417    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
3418     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
3419              handle))
3420    ;; multipart/alternative
3421    ((and (equal (car handle) "multipart/alternative")
3422          (not gnus-mime-display-multipart-as-mixed))
3423     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3424       (push (cons id handle) gnus-article-mime-handle-alist)
3425       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3426    ;; multipart/related
3427    ((and (equal (car handle) "multipart/related")
3428          (not gnus-mime-display-multipart-as-mixed))
3429     ;;;!!!We should find the start part, but we just default
3430     ;;;!!!to the first part.
3431     (gnus-mime-display-part (cadr handle)))
3432    ;; Other multiparts are handled like multipart/mixed.
3433    (t
3434     (gnus-mime-display-mixed (cdr handle)))))
3435
3436 (defun gnus-mime-part-function (handles)
3437   (if (stringp (car handles))
3438       (mapcar 'gnus-mime-part-function (cdr handles))
3439     (funcall gnus-article-mime-part-function handles)))
3440
3441 (defun gnus-mime-display-mixed (handles)
3442   (mapcar 'gnus-mime-display-part handles))
3443
3444 (defun gnus-mime-display-single (handle)
3445   (let ((type (mm-handle-media-type handle))
3446         (ignored gnus-ignored-mime-types)
3447         (not-attachment t)
3448         (move nil)
3449         display text)
3450     (catch 'ignored
3451       (progn
3452         (while ignored
3453           (when (string-match (pop ignored) type)
3454             (throw 'ignored nil)))
3455         (if (and (setq not-attachment
3456                        (or (not (mm-handle-disposition handle))
3457                            (equal (car (mm-handle-disposition handle))
3458                                   "inline")
3459                            (mm-attachment-override-p handle)))
3460                  (mm-automatic-display-p handle)
3461                  (or (mm-inlined-p handle)
3462                      (mm-automatic-external-display-p type)))
3463             (setq display t)
3464           (when (equal (mm-handle-media-supertype handle) "text")
3465             (setq text t)))
3466         (let ((id (1+ (length gnus-article-mime-handle-alist))))
3467           (push (cons id handle) gnus-article-mime-handle-alist)
3468           (when (or (not display)
3469                     (not (gnus-unbuttonized-mime-type-p type)))
3470             (gnus-article-insert-newline)
3471             (gnus-insert-mime-button
3472              handle id (list (or display (and not-attachment text))))
3473             (gnus-article-insert-newline)
3474             (gnus-article-insert-newline)
3475             (setq move t)))
3476         (let ((beg (point)))
3477           (cond
3478            (display
3479             (when move
3480               (forward-line -2)
3481               (setq beg (point)))
3482             (let ((mail-parse-charset gnus-newsgroup-charset)
3483                   (mail-parse-ignored-charsets 
3484                    (save-excursion (set-buffer gnus-summary-buffer)
3485                                    gnus-newsgroup-ignored-charsets)))
3486               (mm-display-part handle t))
3487             (goto-char (point-max)))
3488            ((and text not-attachment)
3489             (when move
3490               (forward-line -2)
3491               (setq beg (point)))
3492             (gnus-article-insert-newline)
3493             (mm-insert-inline handle (mm-get-part handle))
3494             (goto-char (point-max))))
3495           ;; Do highlighting.
3496           (save-excursion
3497             (save-restriction
3498               (narrow-to-region beg (point))
3499               (gnus-treat-article
3500                nil (length gnus-article-mime-handle-alist)
3501                (1- (length gnus-article-mime-handles))
3502                (mm-handle-media-type handle)))))))))
3503
3504 (defun gnus-unbuttonized-mime-type-p (type)
3505   "Say whether TYPE is to be unbuttonized."
3506   (unless gnus-inhibit-mime-unbuttonizing
3507     (catch 'found
3508       (let ((types gnus-unbuttonized-mime-types))
3509         (while types
3510           (when (string-match (pop types) type)
3511             (throw 'found t)))))))
3512
3513 (defun gnus-article-insert-newline ()
3514   "Insert a newline, but mark it as undeletable."
3515   (gnus-put-text-property
3516    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
3517
3518 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
3519   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
3520          (ihandles handles)
3521          (point (point))
3522          handle buffer-read-only from props begend not-pref)
3523     (save-window-excursion
3524       (save-restriction
3525         (when ibegend
3526           (narrow-to-region (car ibegend)
3527                             (or (cdr ibegend)
3528                                 (progn
3529                                   (goto-char (car ibegend))
3530                                   (forward-line 2)
3531                                   (point))))
3532           (delete-region (point-min) (point-max))
3533           (mm-remove-parts handles))
3534         (setq begend (list (point-marker)))
3535         ;; Do the toggle.
3536         (unless (setq not-pref (cadr (member preferred ihandles)))
3537           (setq not-pref (car ihandles)))
3538         (when (or ibegend
3539                   (not (gnus-unbuttonized-mime-type-p
3540                         "multipart/alternative")))
3541           (gnus-add-text-properties
3542            (setq from (point))
3543            (progn
3544              (insert (format "%d.  " id))
3545              (point))
3546            `(gnus-callback
3547              (lambda (handles)
3548                (unless ,(not ibegend)
3549                  (setq gnus-article-mime-handle-alist
3550                        ',gnus-article-mime-handle-alist))
3551                (gnus-mime-display-alternative
3552                 ',ihandles ',not-pref ',begend ,id))
3553              local-map ,gnus-mime-button-map
3554              ,gnus-mouse-face-prop ,gnus-article-mouse-face
3555              face ,gnus-article-button-face
3556              keymap ,gnus-mime-button-map
3557              gnus-part ,id
3558              gnus-data ,handle))
3559           (widget-convert-button 'link from (point)
3560                                  :action 'gnus-widget-press-button
3561                                  :button-keymap gnus-widget-button-keymap)
3562           ;; Do the handles
3563           (while (setq handle (pop handles))
3564             (gnus-add-text-properties
3565              (setq from (point))
3566              (progn
3567                (insert (format "(%c) %-18s"
3568                                (if (equal handle preferred) ?* ? )
3569                                (mm-handle-media-type handle)))
3570                (point))
3571              `(gnus-callback
3572                (lambda (handles)
3573                  (unless ,(not ibegend)
3574                    (setq gnus-article-mime-handle-alist
3575                          ',gnus-article-mime-handle-alist))
3576                  (gnus-mime-display-alternative
3577                   ',ihandles ',handle ',begend ,id))
3578                local-map ,gnus-mime-button-map
3579                ,gnus-mouse-face-prop ,gnus-article-mouse-face
3580                face ,gnus-article-button-face
3581                keymap ,gnus-mime-button-map
3582                gnus-part ,id
3583                gnus-data ,handle))
3584             (widget-convert-button 'link from (point)
3585                                    :action 'gnus-widget-press-button
3586                                    :button-keymap gnus-widget-button-keymap)
3587             (insert "  "))
3588           (insert "\n\n"))
3589         (when preferred
3590           (if (stringp (car preferred))
3591               (gnus-display-mime preferred)
3592             (let ((mail-parse-charset gnus-newsgroup-charset)
3593                   (mail-parse-ignored-charsets 
3594                    (save-excursion (set-buffer gnus-summary-buffer)
3595                                    gnus-newsgroup-ignored-charsets)))
3596               (mm-display-part preferred)
3597               ;; Do highlighting.
3598               (save-excursion
3599                 (save-restriction
3600                   (narrow-to-region (car begend) (point-max))
3601                   (gnus-treat-article
3602                    nil (length gnus-article-mime-handle-alist)
3603                    (1- (length gnus-article-mime-handles))
3604                    (mm-handle-media-type handle))))))
3605           (goto-char (point-max))
3606           (setcdr begend (point-marker)))))
3607     (when ibegend
3608       (goto-char point))))
3609
3610 (defun gnus-article-wash-status ()
3611   "Return a string which display status of article washing."
3612   (save-excursion
3613     (set-buffer gnus-article-buffer)
3614     (let ((cite (gnus-article-hidden-text-p 'cite))
3615           (headers (gnus-article-hidden-text-p 'headers))
3616           (boring (gnus-article-hidden-text-p 'boring-headers))
3617           (pgp (gnus-article-hidden-text-p 'pgp))
3618           (pem (gnus-article-hidden-text-p 'pem))
3619           (signature (gnus-article-hidden-text-p 'signature))
3620           (overstrike (gnus-article-hidden-text-p 'overstrike))
3621           (emphasis (gnus-article-hidden-text-p 'emphasis))
3622           (mime gnus-show-mime))
3623       (format "%c%c%c%c%c%c%c"
3624               (if cite ?c ? )
3625               (if (or headers boring) ?h ? )
3626               (if (or pgp pem) ?p ? )
3627               (if signature ?s ? )
3628               (if overstrike ?o ? )
3629               (if mime ?m ? )
3630               (if emphasis ?e ? )))))
3631
3632 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
3633
3634 (defun gnus-article-maybe-hide-headers ()
3635   "Hide unwanted headers if `gnus-have-all-headers' is nil.
3636 Provided for backwards compatibility."
3637   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
3638                  (not (save-excursion (set-buffer gnus-summary-buffer)
3639                                       gnus-have-all-headers)))
3640              (not gnus-inhibit-hiding))
3641     (gnus-article-hide-headers)))
3642
3643 ;;; Article savers.
3644
3645 (defun gnus-output-to-file (file-name)
3646   "Append the current article to a file named FILE-NAME."
3647   (let ((artbuf (current-buffer)))
3648     (with-temp-buffer
3649       (insert-buffer-substring artbuf)
3650       ;; Append newline at end of the buffer as separator, and then
3651       ;; save it to file.
3652       (goto-char (point-max))
3653       (insert "\n")
3654       (write-region-as-binary (point-min) (point-max) file-name 'append)
3655       t)))
3656
3657 (defun gnus-narrow-to-page (&optional arg)
3658   "Narrow the article buffer to a page.
3659 If given a numerical ARG, move forward ARG pages."
3660   (interactive "P")
3661   (setq arg (if arg (prefix-numeric-value arg) 0))
3662   (save-excursion
3663     (set-buffer gnus-article-buffer)
3664     (goto-char (point-min))
3665     (widen)
3666     ;; Remove any old next/prev buttons.
3667     (when (gnus-visual-p 'page-marker)
3668       (let ((buffer-read-only nil))
3669         (gnus-remove-text-with-property 'gnus-prev)
3670         (gnus-remove-text-with-property 'gnus-next)))
3671     (when
3672         (cond ((< arg 0)
3673                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
3674               ((> arg 0)
3675                (re-search-forward page-delimiter nil 'move arg)))
3676       (goto-char (match-end 0)))
3677     (narrow-to-region
3678      (point)
3679      (if (re-search-forward page-delimiter nil 'move)
3680          (match-beginning 0)
3681        (point)))
3682     (when (and (gnus-visual-p 'page-marker)
3683                (not (= (point-min) 1)))
3684       (save-excursion
3685         (goto-char (point-min))
3686         (gnus-insert-prev-page-button)))
3687     (when (and (gnus-visual-p 'page-marker)
3688                (< (+ (point-max) 2) (buffer-size)))
3689       (save-excursion
3690         (goto-char (point-max))
3691         (gnus-insert-next-page-button)))))
3692
3693 ;; Article mode commands
3694
3695 (defun gnus-article-goto-next-page ()
3696   "Show the next page of the article."
3697   (interactive)
3698   (when (gnus-article-next-page)
3699     (goto-char (point-min))
3700     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
3701
3702 (defun gnus-article-goto-prev-page ()
3703   "Show the next page of the article."
3704   (interactive)
3705   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
3706     (gnus-article-prev-page nil)))
3707
3708 (defun gnus-article-next-page (&optional lines)
3709   "Show the next page of the current article.
3710 If end of article, return non-nil.  Otherwise return nil.
3711 Argument LINES specifies lines to be scrolled up."
3712   (interactive "p")
3713   (let ((start (window-start))
3714         end-of-buffer end-of-page)
3715     (save-excursion
3716       (move-to-window-line -1)
3717       (if (<= (point) start)
3718           (progn
3719             (forward-line 2)
3720             (setq start (point)))
3721         (forward-line 1)
3722         (setq start nil))
3723       (unless (or (cond ((eq (1+ (buffer-size)) (point))
3724                          (setq end-of-buffer t))
3725                         ((eobp)
3726                          (setq end-of-page t)))
3727                   (not lines))
3728         (move-to-window-line lines)
3729         (unless (search-backward "\n\n" nil t)
3730           (setq start (point)))))
3731     (cond (end-of-buffer t)
3732           (end-of-page
3733            (gnus-narrow-to-page 1)
3734            nil)
3735           (t
3736            (if start
3737                (set-window-start (selected-window) start)
3738              (scroll-up lines))
3739            nil))))
3740
3741 (defun gnus-article-prev-page (&optional lines)
3742   "Show previous page of current article.
3743 Argument LINES specifies lines to be scrolled down."
3744   (interactive "p")
3745   (let (beginning-of-buffer beginning-of-page)
3746     (save-excursion
3747       (move-to-window-line 0)
3748       (cond ((eq 1 (point))
3749              (setq beginning-of-buffer t))
3750             ((bobp)
3751              (setq beginning-of-page t))))
3752     (cond (beginning-of-buffer)
3753           (beginning-of-page
3754            (gnus-narrow-to-page -1))
3755           (t
3756            (condition-case nil
3757                (scroll-down lines)
3758              (beginning-of-buffer
3759               (goto-char (point-min))))))))
3760
3761 (defun gnus-article-refer-article ()
3762   "Read article specified by message-id around point."
3763   (interactive)
3764   (let ((point (point)))
3765     (search-forward ">" nil t)          ;Move point to end of "<....>".
3766     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
3767         (let ((message-id (match-string 1)))
3768           (goto-char point)
3769           (set-buffer gnus-summary-buffer)
3770           (gnus-summary-refer-article message-id))
3771       (goto-char (point))
3772       (error "No references around point"))))
3773
3774 (defun gnus-article-show-summary ()
3775   "Reconfigure windows to show summary buffer."
3776   (interactive)
3777   (if (not (gnus-buffer-live-p gnus-summary-buffer))
3778       (error "There is no summary buffer for this article buffer")
3779     (gnus-article-set-globals)
3780     (gnus-configure-windows 'article)
3781     (gnus-summary-goto-subject gnus-current-article)
3782     (gnus-summary-position-point)))
3783
3784 (defun gnus-article-describe-briefly ()
3785   "Describe article mode commands briefly."
3786   (interactive)
3787   (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")))
3788
3789 (defun gnus-article-summary-command ()
3790   "Execute the last keystroke in the summary buffer."
3791   (interactive)
3792   (let ((obuf (current-buffer))
3793         (owin (current-window-configuration))
3794         func)
3795     (switch-to-buffer gnus-article-current-summary 'norecord)
3796     (setq func (lookup-key (current-local-map) (this-command-keys)))
3797     (call-interactively func)
3798     (set-buffer obuf)
3799     (set-window-configuration owin)
3800     (set-window-point (get-buffer-window (current-buffer)) (point))))
3801
3802 (defun gnus-article-summary-command-nosave ()
3803   "Execute the last keystroke in the summary buffer."
3804   (interactive)
3805   (let (func)
3806     (pop-to-buffer gnus-article-current-summary 'norecord)
3807     (setq func (lookup-key (current-local-map) (this-command-keys)))
3808     (call-interactively func)))
3809
3810 (defun gnus-article-check-buffer ()
3811   "Beep if not in an article buffer."
3812   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
3813     (error "Command invoked outside of a Gnus article buffer")))
3814
3815 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
3816   "Read a summary buffer key sequence and execute it from the article buffer."
3817   (interactive "P")
3818   (gnus-article-check-buffer)
3819   (let ((nosaves
3820          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
3821            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
3822            "=" "^" "\M-^" "|"))
3823         (nosave-but-article
3824          '("A\r"))
3825         (nosave-in-article
3826          '("\C-d"))
3827         (up-to-top
3828          '("n" "Gn" "p" "Gp"))
3829         keys new-sum-point)
3830     (save-excursion
3831       (set-buffer gnus-article-current-summary)
3832       (let (gnus-pick-mode)
3833         (push (or key last-command-event) unread-command-events)
3834         (setq keys (static-if (featurep 'xemacs)
3835                        (events-to-keys (read-key-sequence nil))
3836                      (read-key-sequence nil)))))
3837     (message "")
3838
3839     (if (or (member keys nosaves)
3840             (member keys nosave-but-article)
3841             (member keys nosave-in-article))
3842         (let (func)
3843           (save-window-excursion
3844             (pop-to-buffer gnus-article-current-summary 'norecord)
3845             ;; We disable the pick minor mode commands.
3846             (let (gnus-pick-mode)
3847               (setq func (lookup-key (current-local-map) keys))))
3848           (if (not func)
3849               (ding)
3850             (unless (member keys nosave-in-article)
3851               (set-buffer gnus-article-current-summary))
3852             (call-interactively func)
3853             (setq new-sum-point (point)))
3854           (when (member keys nosave-but-article)
3855             (pop-to-buffer gnus-article-buffer 'norecord)))
3856       ;; These commands should restore window configuration.
3857       (let ((obuf (current-buffer))
3858             (owin (current-window-configuration))
3859             (opoint (point))
3860             (summary gnus-article-current-summary)
3861             func in-buffer selected)
3862         (if not-restore-window
3863             (pop-to-buffer summary 'norecord)
3864           (switch-to-buffer summary 'norecord))
3865         (setq in-buffer (current-buffer))
3866         ;; We disable the pick minor mode commands.
3867         (if (setq func (let (gnus-pick-mode)
3868                          (lookup-key (current-local-map) keys)))
3869             (progn
3870               (call-interactively func)
3871               (setq new-sum-point (point)))
3872           (ding))
3873         (when (eq in-buffer (current-buffer))
3874           (setq selected (gnus-summary-select-article))
3875           (set-buffer obuf)
3876           (unless not-restore-window
3877             (set-window-configuration owin))
3878           (when (eq selected 'old)
3879             (article-goto-body)
3880             (set-window-start (get-buffer-window (current-buffer))
3881                               1)
3882             (set-window-point (get-buffer-window (current-buffer))
3883                               (point)))
3884           (let ((win (get-buffer-window gnus-article-current-summary)))
3885             (when win
3886               (set-window-point win new-sum-point))))))))
3887
3888 (defun gnus-article-hide (&optional arg force)
3889   "Hide all the gruft in the current article.
3890 This means that PGP stuff, signatures, cited text and (some)
3891 headers will be hidden.
3892 If given a prefix, show the hidden text instead."
3893   (interactive (append (gnus-article-hidden-arg) (list 'force)))
3894   (gnus-article-hide-headers arg)
3895   (gnus-article-hide-list-identifiers arg)
3896   (gnus-article-hide-pgp arg)
3897   (gnus-article-hide-citation-maybe arg force)
3898   (gnus-article-hide-signature arg))
3899
3900 (defun gnus-article-maybe-highlight ()
3901   "Do some article highlighting if article highlighting is requested."
3902   (when (gnus-visual-p 'article-highlight 'highlight)
3903     (gnus-article-highlight-some)))
3904
3905 (defun gnus-check-group-server ()
3906   ;; Make sure the connection to the server is alive.
3907   (unless (gnus-server-opened
3908            (gnus-find-method-for-group gnus-newsgroup-name))
3909     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
3910     (gnus-request-group gnus-newsgroup-name t)))
3911
3912 (defun gnus-request-article-this-buffer (article group)
3913   "Get an article and insert it into this buffer."
3914   (let (do-update-line sparse-header)
3915     (prog1
3916         (save-excursion
3917           (erase-buffer)
3918           (gnus-kill-all-overlays)
3919           (setq group (or group gnus-newsgroup-name))
3920
3921           ;; Using `gnus-request-article' directly will insert the article into
3922           ;; `nntp-server-buffer' - so we'll save some time by not having to
3923           ;; copy it from the server buffer into the article buffer.
3924
3925           ;; We only request an article by message-id when we do not have the
3926           ;; headers for it, so we'll have to get those.
3927           (when (stringp article)
3928             (let ((gnus-override-method gnus-refer-article-method))
3929               (gnus-read-header article)))
3930
3931           ;; If the article number is negative, that means that this article
3932           ;; doesn't belong in this newsgroup (possibly), so we find its
3933           ;; message-id and request it by id instead of number.
3934           (when (and (numberp article)
3935                      gnus-summary-buffer
3936                      (get-buffer gnus-summary-buffer)
3937                      (gnus-buffer-exists-p gnus-summary-buffer))
3938             (save-excursion
3939               (set-buffer gnus-summary-buffer)
3940               (let ((header (gnus-summary-article-header article)))
3941                 (when (< article 0)
3942                   (cond
3943                    ((memq article gnus-newsgroup-sparse)
3944                     ;; This is a sparse gap article.
3945                     (setq do-update-line article)
3946                     (setq article (mail-header-id header))
3947                     (let ((gnus-override-method gnus-refer-article-method))
3948                       (setq sparse-header (gnus-read-header article)))
3949                     (setq gnus-newsgroup-sparse
3950                           (delq article gnus-newsgroup-sparse)))
3951                    ((vectorp header)
3952                     ;; It's a real article.
3953                     (setq article (mail-header-id header)))
3954                    (t
3955                     ;; It is an extracted pseudo-article.
3956                     (setq article 'pseudo)
3957                     (gnus-request-pseudo-article header))))
3958
3959                 (let ((method (gnus-find-method-for-group
3960                                gnus-newsgroup-name)))
3961                   (when (and (eq (car method) 'nneething)
3962                              (vectorp header))
3963                     (let ((dir (concat
3964                                 (file-name-as-directory
3965                                  (or (cadr (assq 'nneething-address method))
3966                                      (nth 1 method)))
3967                                 (mail-header-subject header))))
3968                       (when (file-directory-p dir)
3969                         (setq article 'nneething)
3970                         (gnus-group-enter-directory dir))))))))
3971
3972           (cond
3973            ;; Refuse to select canceled articles.
3974            ((and (numberp article)
3975                  gnus-summary-buffer
3976                  (get-buffer gnus-summary-buffer)
3977                  (gnus-buffer-exists-p gnus-summary-buffer)
3978                  (eq (cdr (save-excursion
3979                             (set-buffer gnus-summary-buffer)
3980                             (assq article gnus-newsgroup-reads)))
3981                      gnus-canceled-mark))
3982             nil)
3983            ;; Check the backlog.
3984            ((and gnus-keep-backlog
3985                  (gnus-backlog-request-article group article (current-buffer)))
3986             'article)
3987            ;; Check asynchronous pre-fetch.
3988            ((gnus-async-request-fetched-article group article (current-buffer))
3989             (gnus-async-prefetch-next group article gnus-summary-buffer)
3990             (when (and (numberp article) gnus-keep-backlog)
3991               (gnus-backlog-enter-article group article (current-buffer)))
3992             'article)
3993            ;; Check the cache.
3994            ((and gnus-use-cache
3995                  (numberp article)
3996                  (gnus-cache-request-article article group))
3997             'article)
3998            ;; Get the article and put into the article buffer.
3999            ((or (stringp article) (numberp article))
4000             (let ((gnus-override-method
4001                    (and (stringp article) gnus-refer-article-method))
4002                   (buffer-read-only nil))
4003               (erase-buffer)
4004               (gnus-kill-all-overlays)
4005               (gnus-check-group-server)
4006               (when (gnus-request-article article group (current-buffer))
4007                 (when (numberp article)
4008                   (gnus-async-prefetch-next group article gnus-summary-buffer)
4009                   (when gnus-keep-backlog
4010                     (gnus-backlog-enter-article
4011                      group article (current-buffer))))
4012                 'article)))
4013            ;; It was a pseudo.
4014            (t article)))
4015
4016       ;; Associate this article with the current summary buffer.
4017       (setq gnus-article-current-summary gnus-summary-buffer)
4018
4019       ;; Take the article from the original article buffer
4020       ;; and place it in the buffer it's supposed to be in.
4021       (when (and (get-buffer gnus-article-buffer)
4022                  (equal (buffer-name (current-buffer))
4023                         (buffer-name (get-buffer gnus-article-buffer))))
4024         (save-excursion
4025           (if (get-buffer gnus-original-article-buffer)
4026               (set-buffer gnus-original-article-buffer)
4027             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4028             (buffer-disable-undo)
4029             (setq major-mode 'gnus-original-article-mode)
4030             (setq buffer-read-only t))
4031           (let (buffer-read-only)
4032             (erase-buffer)
4033             (insert-buffer-substring gnus-article-buffer))
4034           (setq gnus-original-article (cons group article)))
4035
4036         ;; Decode charsets.
4037         (run-hooks 'gnus-article-decode-hook)
4038         ;; Mark article as decoded or not.
4039         (setq gnus-article-decoded-p gnus-article-decode-hook))
4040
4041       ;; Update sparse articles.
4042       (when (and do-update-line
4043                  (or (numberp article)
4044                      (stringp article)))
4045         (let ((buf (current-buffer)))
4046           (set-buffer gnus-summary-buffer)
4047           (gnus-summary-update-article do-update-line sparse-header)
4048           (gnus-summary-goto-subject do-update-line nil t)
4049           (set-window-point (get-buffer-window (current-buffer) t)
4050                             (point))
4051           (set-buffer buf))))))
4052
4053 ;;;
4054 ;;; Article editing
4055 ;;;
4056
4057 (defcustom gnus-article-edit-mode-hook nil
4058   "Hook run in article edit mode buffers."
4059   :group 'gnus-article-various
4060   :type 'hook)
4061
4062 (defcustom gnus-article-edit-article-setup-function
4063   'gnus-article-mime-edit-article-setup
4064   "Function called to setup an editing article buffer."
4065   :group 'gnus-article-various
4066   :type 'function)
4067
4068 (defvar gnus-article-edit-done-function nil)
4069
4070 (defvar gnus-article-edit-mode-map nil)
4071
4072 ;; Should we be using derived.el for this?
4073 (unless gnus-article-edit-mode-map
4074   (setq gnus-article-edit-mode-map (make-sparse-keymap))
4075   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
4076
4077   (gnus-define-keys gnus-article-edit-mode-map
4078     "\C-c\C-c" gnus-article-edit-done
4079     "\C-c\C-k" gnus-article-edit-exit)
4080
4081   (gnus-define-keys (gnus-article-edit-wash-map
4082                      "\C-c\C-w" gnus-article-edit-mode-map)
4083     "f" gnus-article-edit-full-stops))
4084
4085 (defun gnus-article-edit-mode ()
4086   "Major mode for editing articles.
4087 This is an extended text-mode.
4088
4089 \\{gnus-article-edit-mode-map}"
4090   (interactive)
4091   (setq major-mode 'gnus-article-edit-mode)
4092   (setq mode-name "Article Edit")
4093   (use-local-map gnus-article-edit-mode-map)
4094   (make-local-variable 'gnus-article-edit-done-function)
4095   (make-local-variable 'gnus-prev-winconf)
4096   (setq buffer-read-only nil)
4097   (buffer-enable-undo)
4098   (widen)
4099   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
4100
4101 (defun gnus-article-edit (&optional force)
4102   "Edit the current article.
4103 This will have permanent effect only in mail groups.
4104 If FORCE is non-nil, allow editing of articles even in read-only
4105 groups."
4106   (interactive "P")
4107   (when (and (not force)
4108              (gnus-group-read-only-p))
4109     (error "The current newsgroup does not support article editing"))
4110   (gnus-article-date-original)
4111   (gnus-article-edit-article
4112    'ignore
4113    `(lambda (no-highlight)
4114       'ignore
4115       (gnus-summary-edit-article-done
4116        ,(or (mail-header-references gnus-current-headers) "")
4117        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
4118
4119 (defun gnus-article-edit-article (start-func exit-func)
4120   "Start editing the contents of the current article buffer."
4121   (let ((winconf (current-window-configuration)))
4122     (set-buffer gnus-article-buffer)
4123     (gnus-article-edit-mode)
4124     (funcall start-func)
4125     (gnus-configure-windows 'edit-article)
4126     (setq gnus-article-edit-done-function exit-func)
4127     (setq gnus-prev-winconf winconf)
4128     (when gnus-article-edit-article-setup-function
4129       (funcall gnus-article-edit-article-setup-function))
4130     (gnus-message 6 "C-c C-c to end edits")))
4131
4132 (defun gnus-article-edit-done (&optional arg)
4133   "Update the article edits and exit."
4134   (interactive "P")
4135   (save-excursion
4136     (save-restriction
4137       (widen)
4138       (when (article-goto-body)
4139         (let ((lines (count-lines (point) (point-max)))
4140               (length (- (point-max) (point)))
4141               (case-fold-search t)
4142               (body (copy-marker (point))))
4143           (goto-char (point-min))
4144           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
4145             (delete-region (match-beginning 1) (match-end 1))
4146             (insert (number-to-string length)))
4147           (goto-char (point-min))
4148           (when (re-search-forward
4149                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
4150             (delete-region (match-beginning 1) (match-end 1))
4151             (insert (number-to-string length)))
4152           (goto-char (point-min))
4153           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
4154             (delete-region (match-beginning 1) (match-end 1))
4155             (insert (number-to-string lines)))))))
4156   (let ((func gnus-article-edit-done-function)
4157         (buf (current-buffer))
4158         (start (window-start)))
4159     (remove-hook 'gnus-article-mode-hook
4160                  'gnus-article-mime-edit-article-unwind)
4161     (gnus-article-edit-exit)
4162     (save-excursion
4163       (set-buffer buf)
4164       (let ((buffer-read-only nil))
4165         (funcall func arg))
4166       ;; The cache and backlog have to be flushed somewhat.
4167       (when gnus-keep-backlog
4168         (gnus-backlog-remove-article
4169          (car gnus-article-current) (cdr gnus-article-current)))
4170       ;; Flush original article as well.
4171       (save-excursion
4172         (when (get-buffer gnus-original-article-buffer)
4173           (set-buffer gnus-original-article-buffer)
4174           (setq gnus-original-article nil)))
4175       (when gnus-use-cache
4176         (gnus-cache-update-article
4177          (car gnus-article-current) (cdr gnus-article-current))))
4178     (set-buffer buf)
4179     (set-window-start (get-buffer-window buf) start)
4180     (set-window-point (get-buffer-window buf) (point))))
4181
4182 (defun gnus-article-edit-exit ()
4183   "Exit the article editing without updating."
4184   (interactive)
4185   ;; We remove all text props from the article buffer.
4186   (let ((buf (format "%s" (buffer-string)))
4187         (curbuf (current-buffer))
4188         (p (point))
4189         (window-start (window-start)))
4190     (erase-buffer)
4191     (insert buf)
4192     (let ((winconf gnus-prev-winconf))
4193       (gnus-article-mode)
4194       (set-window-configuration winconf)
4195       ;; Tippy-toe some to make sure that point remains where it was.
4196       (save-current-buffer
4197         (set-buffer curbuf)
4198         (set-window-start (get-buffer-window (current-buffer)) window-start)
4199         (goto-char p)))))
4200
4201 (defun gnus-article-edit-full-stops ()
4202   "Interactively repair spacing at end of sentences."
4203   (interactive)
4204   (save-excursion
4205     (goto-char (point-min))
4206     (search-forward-regexp "^$" nil t)
4207     (let ((case-fold-search nil))
4208       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
4209
4210 ;;;
4211 ;;; Article editing with MIME-Edit
4212 ;;;
4213
4214 (defcustom gnus-article-mime-edit-article-setup-hook nil
4215   "Hook run after setting up a MIME editing article buffer."
4216   :group 'gnus-article-various
4217   :type 'hook)
4218
4219 (defun gnus-article-mime-edit-article-unwind ()
4220   "Unwind `gnus-article-buffer' if article editing was given up."
4221   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
4222   (when mime-edit-mode-flag
4223     (mime-edit-exit 'nomime 'no-error)
4224     (message ""))
4225   (when (featurep 'font-lock)
4226     (setq font-lock-defaults nil)
4227     (font-lock-mode 0)))
4228
4229 (defun gnus-article-mime-edit-article-setup ()
4230   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
4231 after replacing with the original article."
4232   (setq gnus-show-mime t)
4233   (setq gnus-article-edit-done-function
4234         `(lambda (&rest args)
4235            (when mime-edit-mode-flag
4236              (mime-edit-exit)
4237              (message ""))
4238            (goto-char (point-min))
4239            (let (case-fold-search)
4240              (when (re-search-forward
4241                     (format "^%s$" (regexp-quote mail-header-separator))
4242                     nil t)
4243                (replace-match "")))
4244            (when (featurep 'font-lock)
4245              (setq font-lock-defaults nil)
4246              (font-lock-mode 0))
4247            (apply ,gnus-article-edit-done-function args)
4248            (set-buffer gnus-original-article-buffer)
4249            (erase-buffer)
4250            (insert-buffer gnus-article-buffer)
4251            (setq gnus-current-headers (gnus-article-make-full-mail-header))
4252            (gnus-article-prepare-display)))
4253   (substitute-key-definition
4254    'gnus-article-edit-exit 'gnus-article-mime-edit-exit
4255    gnus-article-edit-mode-map)
4256   (erase-buffer)
4257   (insert-buffer gnus-original-article-buffer)
4258   (mime-edit-again)
4259   (when (featurep 'font-lock)
4260     (set (make-local-variable 'font-lock-defaults)
4261          '(message-font-lock-keywords t))
4262     (font-lock-set-defaults)
4263     (turn-on-font-lock))
4264   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
4265   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
4266
4267 (defun gnus-article-mime-edit-exit ()
4268   "Exit the article MIME editing without updating."
4269   (interactive)
4270   (let ((winconf gnus-prev-winconf)
4271         buf)
4272     (when mime-edit-mode-flag
4273       (mime-edit-exit)
4274       (message ""))
4275     (goto-char (point-min))
4276     (let (case-fold-search)
4277       (when (re-search-forward
4278              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
4279         (replace-match "")))
4280     (when (featurep 'font-lock)
4281       (setq font-lock-defaults nil)
4282       (font-lock-mode 0))
4283     ;; We remove all text props from the article buffer.
4284     (setq buf (format "%s" (buffer-string)))
4285     (set-buffer (get-buffer-create gnus-original-article-buffer))
4286     (erase-buffer)
4287     (insert buf)
4288     (setq gnus-current-headers (gnus-article-make-full-mail-header))
4289     (gnus-article-prepare-display)
4290     (set-window-configuration winconf)))
4291
4292 ;;;
4293 ;;; Article highlights
4294 ;;;
4295
4296 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
4297
4298 ;;; Internal Variables:
4299
4300 (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\\)"
4301   "Regular expression that matches URLs."
4302   :group 'gnus-article-buttons
4303   :type 'regexp)
4304
4305 (defcustom gnus-button-alist
4306   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
4307      0 t gnus-button-message-id 2)
4308     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
4309     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
4310      1 t
4311      gnus-button-fetch-group 4)
4312     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
4313     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
4314      t gnus-button-message-id 3)
4315     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
4316     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
4317     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
4318     ;; This is how URLs _should_ be embedded in text...
4319     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
4320     ;; Raw URLs.
4321     (,gnus-button-url-regexp 0 t gnus-button-url 0))
4322   "*Alist of regexps matching buttons in article bodies.
4323
4324 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
4325 REGEXP: is the string matching text around the button,
4326 BUTTON: is the number of the regexp grouping actually matching the button,
4327 FORM: is a lisp expression which must eval to true for the button to
4328 be added,
4329 CALLBACK: is the function to call when the user push this button, and each
4330 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
4331
4332 CALLBACK can also be a variable, in that case the value of that
4333 variable it the real callback function."
4334   :group 'gnus-article-buttons
4335   :type '(repeat (list regexp
4336                        (integer :tag "Button")
4337                        (sexp :tag "Form")
4338                        (function :tag "Callback")
4339                        (repeat :tag "Par"
4340                                :inline t
4341                                (integer :tag "Regexp group")))))
4342
4343 (defcustom gnus-header-button-alist
4344   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
4345      0 t gnus-button-message-id 0)
4346     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
4347     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
4348      0 t gnus-button-mailto 0)
4349     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
4350     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
4351     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
4352     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
4353      gnus-button-message-id 3))
4354   "*Alist of headers and regexps to match buttons in article heads.
4355
4356 This alist is very similar to `gnus-button-alist', except that each
4357 alist has an additional HEADER element first in each entry:
4358
4359 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
4360
4361 HEADER is a regexp to match a header.  For a fuller explanation, see
4362 `gnus-button-alist'."
4363   :group 'gnus-article-buttons
4364   :group 'gnus-article-headers
4365   :type '(repeat (list (regexp :tag "Header")
4366                        regexp
4367                        (integer :tag "Button")
4368                        (sexp :tag "Form")
4369                        (function :tag "Callback")
4370                        (repeat :tag "Par"
4371                                :inline t
4372                                (integer :tag "Regexp group")))))
4373
4374 (defvar gnus-button-regexp nil)
4375 (defvar gnus-button-marker-list nil)
4376 ;; Regexp matching any of the regexps from `gnus-button-alist'.
4377
4378 (defvar gnus-button-last nil)
4379 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
4380
4381 ;;; Commands:
4382
4383 (defun gnus-article-push-button (event)
4384   "Check text under the mouse pointer for a callback function.
4385 If the text under the mouse pointer has a `gnus-callback' property,
4386 call it with the value of the `gnus-data' text property."
4387   (interactive "e")
4388   (set-buffer (window-buffer (posn-window (event-start event))))
4389   (let* ((pos (posn-point (event-start event)))
4390          (data (get-text-property pos 'gnus-data))
4391          (fun (get-text-property pos 'gnus-callback)))
4392     (goto-char pos)
4393     (when fun
4394       (funcall fun data))))
4395
4396 (defun gnus-article-press-button ()
4397   "Check text at point for a callback function.
4398 If the text at point has a `gnus-callback' property,
4399 call it with the value of the `gnus-data' text property."
4400   (interactive)
4401   (let* ((data (get-text-property (point) 'gnus-data))
4402          (fun (get-text-property (point) 'gnus-callback)))
4403     (when fun
4404       (funcall fun data))))
4405
4406 (defun gnus-article-prev-button (n)
4407   "Move point to N buttons backward.
4408 If N is negative, move forward instead."
4409   (interactive "p")
4410   (gnus-article-next-button (- n)))
4411
4412 (defun gnus-article-next-button (n)
4413   "Move point to N buttons forward.
4414 If N is negative, move backward instead."
4415   (interactive "p")
4416   (let ((function (if (< n 0) 'previous-single-property-change
4417                     'next-single-property-change))
4418         (inhibit-point-motion-hooks t)
4419         (backward (< n 0))
4420         (limit (if (< n 0) (point-min) (point-max))))
4421     (setq n (abs n))
4422     (while (and (not (= limit (point)))
4423                 (> n 0))
4424       ;; Skip past the current button.
4425       (when (get-text-property (point) 'gnus-callback)
4426         (goto-char (funcall function (point) 'gnus-callback nil limit)))
4427       ;; Go to the next (or previous) button.
4428       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
4429       ;; Put point at the start of the button.
4430       (when (and backward (not (get-text-property (point) 'gnus-callback)))
4431         (goto-char (funcall function (point) 'gnus-callback nil limit)))
4432       ;; Skip past intangible buttons.
4433       (when (get-text-property (point) 'intangible)
4434         (incf n))
4435       (decf n))
4436     (unless (zerop n)
4437       (gnus-message 5 "No more buttons"))
4438     n))
4439
4440 (defun gnus-article-highlight (&optional force)
4441   "Highlight current article.
4442 This function calls `gnus-article-highlight-headers',
4443 `gnus-article-highlight-citation',
4444 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4445 do the highlighting.  See the documentation for those functions."
4446   (interactive (list 'force))
4447   (gnus-article-highlight-headers)
4448   (gnus-article-highlight-citation force)
4449   (gnus-article-highlight-signature)
4450   (gnus-article-add-buttons force)
4451   (gnus-article-add-buttons-to-head))
4452
4453 (defun gnus-article-highlight-some (&optional force)
4454   "Highlight current article.
4455 This function calls `gnus-article-highlight-headers',
4456 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4457 do the highlighting.  See the documentation for those functions."
4458   (interactive (list 'force))
4459   (gnus-article-highlight-headers)
4460   (gnus-article-highlight-signature)
4461   (gnus-article-add-buttons))
4462
4463 (defun gnus-article-highlight-headers ()
4464   "Highlight article headers as specified by `gnus-header-face-alist'."
4465   (interactive)
4466   (save-excursion
4467     (set-buffer gnus-article-buffer)
4468     (save-restriction
4469       (let ((alist gnus-header-face-alist)
4470             (buffer-read-only nil)
4471             (case-fold-search t)
4472             (inhibit-point-motion-hooks t)
4473             entry regexp header-face field-face from hpoints fpoints)
4474         (article-narrow-to-head)
4475         (while (setq entry (pop alist))
4476           (goto-char (point-min))
4477           (setq regexp (concat "^\\("
4478                                (if (string-equal "" (nth 0 entry))
4479                                    "[^\t ]"
4480                                  (nth 0 entry))
4481                                "\\)")
4482                 header-face (nth 1 entry)
4483                 field-face (nth 2 entry))
4484           (while (and (re-search-forward regexp nil t)
4485                       (not (eobp)))
4486             (beginning-of-line)
4487             (setq from (point))
4488             (unless (search-forward ":" nil t)
4489               (forward-char 1))
4490             (when (and header-face
4491                        (not (memq (point) hpoints)))
4492               (push (point) hpoints)
4493               (gnus-put-text-property from (point) 'face header-face))
4494             (when (and field-face
4495                        (not (memq (setq from (point)) fpoints)))
4496               (push from fpoints)
4497               (if (re-search-forward "^[^ \t]" nil t)
4498                   (forward-char -2)
4499                 (goto-char (point-max)))
4500               (gnus-put-text-property from (point) 'face field-face))))))))
4501
4502 (defun gnus-article-highlight-signature ()
4503   "Highlight the signature in an article.
4504 It does this by highlighting everything after
4505 `gnus-signature-separator' using `gnus-signature-face'."
4506   (interactive)
4507   (save-excursion
4508     (set-buffer gnus-article-buffer)
4509     (let ((buffer-read-only nil)
4510           (inhibit-point-motion-hooks t))
4511       (save-restriction
4512         (when (and gnus-signature-face
4513                    (gnus-article-narrow-to-signature))
4514           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
4515                             'face gnus-signature-face)
4516           (widen)
4517           (gnus-article-search-signature)
4518           (let ((start (match-beginning 0))
4519                 (end (set-marker (make-marker) (1+ (match-end 0)))))
4520             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
4521                                      end)))))))
4522
4523 (defun gnus-button-in-region-p (b e prop)
4524   "Say whether PROP exists in the region."
4525   (text-property-not-all b e prop nil))
4526
4527 (defun gnus-article-add-buttons (&optional force)
4528   "Find external references in the article and make buttons of them.
4529 \"External references\" are things like Message-IDs and URLs, as
4530 specified by `gnus-button-alist'."
4531   (interactive (list 'force))
4532   (save-excursion
4533     (set-buffer gnus-article-buffer)
4534     (let ((buffer-read-only nil)
4535           (inhibit-point-motion-hooks t)
4536           (case-fold-search t)
4537           (alist gnus-button-alist)
4538           beg entry regexp)
4539       ;; Remove all old markers.
4540       (let (marker entry new-list)
4541         (while (setq marker (pop gnus-button-marker-list))
4542           (if (or (< marker (point-min)) (>= marker (point-max)))
4543               (push marker new-list)
4544             (goto-char marker)
4545             (when (setq entry (gnus-button-entry))
4546               (put-text-property (match-beginning (nth 1 entry))
4547                                  (match-end (nth 1 entry))
4548                                  'gnus-callback nil))
4549             (set-marker marker nil)))
4550         (setq gnus-button-marker-list new-list))
4551       ;; We skip the headers.
4552       (article-goto-body)
4553       (setq beg (point))
4554       (while (setq entry (pop alist))
4555         (setq regexp (car entry))
4556         (goto-char beg)
4557         (while (re-search-forward regexp nil t)
4558           (let* ((start (and entry (match-beginning (nth 1 entry))))
4559                  (end (and entry (match-end (nth 1 entry))))
4560                  (from (match-beginning 0)))
4561             (when (and (or (eq t (nth 2 entry))
4562                            (eval (nth 2 entry)))
4563                        (not (gnus-button-in-region-p
4564                              start end 'gnus-callback)))
4565               ;; That optional form returned non-nil, so we add the
4566               ;; button.
4567               (gnus-article-add-button
4568                start end 'gnus-button-push
4569                (car (push (set-marker (make-marker) from)
4570                           gnus-button-marker-list))))))))))
4571
4572 ;; Add buttons to the head of an article.
4573 (defun gnus-article-add-buttons-to-head ()
4574   "Add buttons to the head of the article."
4575   (interactive)
4576   (save-excursion
4577     (set-buffer gnus-article-buffer)
4578     (save-restriction
4579       (let ((buffer-read-only nil)
4580             (inhibit-point-motion-hooks t)
4581             (case-fold-search t)
4582             (alist gnus-header-button-alist)
4583             entry beg end)
4584         (article-narrow-to-head)
4585         (while alist
4586           ;; Each alist entry.
4587           (setq entry (car alist)
4588                 alist (cdr alist))
4589           (goto-char (point-min))
4590           (while (re-search-forward (car entry) nil t)
4591             ;; Each header matching the entry.
4592             (setq beg (match-beginning 0))
4593             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
4594                                (match-beginning 0))
4595                           (point-max)))
4596             (goto-char beg)
4597             (while (re-search-forward (nth 1 entry) end t)
4598               ;; Each match within a header.
4599               (let* ((entry (cdr entry))
4600                      (start (match-beginning (nth 1 entry)))
4601                      (end (match-end (nth 1 entry)))
4602                      (form (nth 2 entry)))
4603                 (goto-char (match-end 0))
4604                 (when (eval form)
4605                   (gnus-article-add-button
4606                    start end (nth 3 entry)
4607                    (buffer-substring (match-beginning (nth 4 entry))
4608                                      (match-end (nth 4 entry)))))))
4609             (goto-char end)))))))
4610
4611 ;;; External functions:
4612
4613 (defun gnus-article-add-button (from to fun &optional data)
4614   "Create a button between FROM and TO with callback FUN and data DATA."
4615   (when gnus-article-button-face
4616     (gnus-overlay-put (gnus-make-overlay from to)
4617                       'face gnus-article-button-face))
4618   (gnus-add-text-properties
4619    from to
4620    (nconc (and gnus-article-mouse-face
4621                (list gnus-mouse-face-prop gnus-article-mouse-face))
4622           (list 'gnus-callback fun)
4623           (and data (list 'gnus-data data)))))
4624
4625 ;;; Internal functions:
4626
4627 (defun gnus-article-set-globals ()
4628   (save-excursion
4629     (set-buffer gnus-summary-buffer)
4630     (gnus-set-global-variables)))
4631
4632 (defun gnus-signature-toggle (end)
4633   (save-excursion
4634     (set-buffer gnus-article-buffer)
4635     (let ((buffer-read-only nil)
4636           (inhibit-point-motion-hooks t))
4637       (if (get-text-property end 'invisible)
4638           (gnus-article-unhide-text end (point-max))
4639         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
4640
4641 (defun gnus-button-entry ()
4642   ;; Return the first entry in `gnus-button-alist' matching this place.
4643   (let ((alist gnus-button-alist)
4644         (entry nil))
4645     (while alist
4646       (setq entry (pop alist))
4647       (if (looking-at (car entry))
4648           (setq alist nil)
4649         (setq entry nil)))
4650     entry))
4651
4652 (defun gnus-button-push (marker)
4653   ;; Push button starting at MARKER.
4654   (save-excursion
4655     (goto-char marker)
4656     (let* ((entry (gnus-button-entry))
4657            (inhibit-point-motion-hooks t)
4658            (fun (nth 3 entry))
4659            (args (mapcar (lambda (group)
4660                            (let ((string (match-string group)))
4661                              (gnus-set-text-properties
4662                               0 (length string) nil string)
4663                              string))
4664                          (nthcdr 4 entry))))
4665       (cond
4666        ((fboundp fun)
4667         (apply fun args))
4668        ((and (boundp fun)
4669              (fboundp (symbol-value fun)))
4670         (apply (symbol-value fun) args))
4671        (t
4672         (gnus-message 1 "You must define `%S' to use this button"
4673                       (cons fun args)))))))
4674
4675 (defun gnus-button-message-id (message-id)
4676   "Fetch MESSAGE-ID."
4677   (save-excursion
4678     (set-buffer gnus-summary-buffer)
4679     (gnus-summary-refer-article message-id)))
4680
4681 (defun gnus-button-fetch-group (address)
4682   "Fetch GROUP specified by ADDRESS."
4683   (if (not (string-match "[:/]" address))
4684       ;; This is just a simple group url.
4685       (gnus-group-read-ephemeral-group address gnus-select-method)
4686     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
4687                            address))
4688         (error "Can't parse %s" address)
4689       (gnus-group-read-ephemeral-group
4690        (match-string 4 address)
4691        `(nntp ,(match-string 1 address)
4692               (nntp-address ,(match-string 1 address))
4693               (nntp-port-number ,(if (match-end 3)
4694                                      (match-string 3 address)
4695                                    "nntp")))))))
4696
4697 (defun gnus-url-parse-query-string (query &optional downcase)
4698   (let (retval pairs cur key val)
4699     (setq pairs (split-string query "&"))
4700     (while pairs
4701       (setq cur (car pairs)
4702             pairs (cdr pairs))
4703       (if (not (string-match "=" cur))
4704           nil                           ; Grace
4705         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
4706               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
4707         (if downcase
4708             (setq key (downcase key)))
4709         (setq cur (assoc key retval))
4710         (if cur
4711             (setcdr cur (cons val (cdr cur)))
4712           (setq retval (cons (list key val) retval)))))
4713     retval))
4714
4715 (defun gnus-url-unhex (x)
4716   (if (> x ?9)
4717       (if (>= x ?a)
4718           (+ 10 (- x ?a))
4719         (+ 10 (- x ?A)))
4720     (- x ?0)))
4721
4722 (defun gnus-url-unhex-string (str &optional allow-newlines)
4723   "Remove %XXX embedded spaces, etc in a url.
4724 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
4725 decoding of carriage returns and line feeds in the string, which is normally
4726 forbidden in URL encoding."
4727   (setq str (or str ""))
4728   (let ((tmp "")
4729         (case-fold-search t))
4730     (while (string-match "%[0-9a-f][0-9a-f]" str)
4731       (let* ((start (match-beginning 0))
4732              (ch1 (gnus-url-unhex (elt str (+ start 1))))
4733              (code (+ (* 16 ch1)
4734                       (gnus-url-unhex (elt str (+ start 2))))))
4735         (setq tmp (concat
4736                    tmp (substring str 0 start)
4737                    (cond
4738                     (allow-newlines
4739                      (char-to-string code))
4740                     ((or (= code ?\n) (= code ?\r))
4741                      " ")
4742                     (t (char-to-string code))))
4743               str (substring str (match-end 0)))))
4744     (setq tmp (concat tmp str))
4745     tmp))
4746
4747 (defun gnus-url-mailto (url)
4748   ;; Send mail to someone
4749   (when (string-match "mailto:/*\\(.*\\)" url)
4750     (setq url (substring url (match-beginning 1) nil)))
4751   (let (to args subject func)
4752     (if (string-match (regexp-quote "?") url)
4753         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
4754               args (gnus-url-parse-query-string
4755                     (substring url (match-end 0) nil) t))
4756       (setq to (gnus-url-unhex-string url)))
4757     (setq args (cons (list "to" to) args)
4758           subject (cdr-safe (assoc "subject" args)))
4759     (gnus-setup-message 'reply
4760       (message-mail)
4761       (while args
4762         (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
4763         (if (fboundp func)
4764             (funcall func)
4765           (message-position-on-field (caar args)))
4766         (insert (mapconcat 'identity (cdar args) ", "))
4767         (setq args (cdr args)))
4768       (if subject
4769           (message-goto-body)
4770         (message-goto-subject)))))
4771
4772 (defun gnus-button-mailto (address)
4773   ;; Mail to ADDRESS.
4774   (set-buffer (gnus-copy-article-buffer))
4775   (gnus-setup-message 'reply
4776     (message-reply address)))
4777
4778 (defun gnus-button-reply (address)
4779   ;; Reply to ADDRESS.
4780   (gnus-setup-message 'reply
4781     (message-reply address)))
4782
4783 (defun gnus-button-url (address)
4784   "Browse ADDRESS."
4785   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
4786   (if (listp browse-url-browser-function)
4787       (browse-url address)
4788     (funcall browse-url-browser-function address)))
4789
4790 (defun gnus-button-embedded-url (address)
4791   "Browse ADDRESS."
4792   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
4793   (if (listp browse-url-browser-function)
4794       (browse-url (gnus-strip-whitespace address))
4795     (funcall browse-url-browser-function (gnus-strip-whitespace address))))
4796
4797 ;;; Next/prev buttons in the article buffer.
4798
4799 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
4800 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
4801
4802 (defvar gnus-prev-page-map nil)
4803 (unless gnus-prev-page-map
4804   (setq gnus-prev-page-map (make-sparse-keymap))
4805   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
4806   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
4807
4808 (static-if (featurep 'xemacs)
4809     (defun gnus-insert-prev-page-button ()
4810       (let ((buffer-read-only nil))
4811         (gnus-eval-format
4812          gnus-prev-page-line-format nil
4813          `(gnus-prev t local-map ,gnus-prev-page-map
4814                      gnus-callback gnus-article-button-prev-page
4815                      article-type annotation))))
4816   (defun gnus-insert-prev-page-button ()
4817     (let ((buffer-read-only nil)
4818           (situation (get-text-property (point-min) 'mime-view-situation)))
4819       (set-keymap-parent gnus-prev-page-map (current-local-map))
4820       (gnus-eval-format
4821        gnus-prev-page-line-format nil
4822        `(gnus-prev t local-map ,gnus-prev-page-map
4823                    gnus-callback gnus-article-button-prev-page
4824                    article-type annotation
4825                    mime-view-situation ,situation))))
4826   )
4827
4828 (defvar gnus-next-page-map nil)
4829 (unless gnus-next-page-map
4830   (setq gnus-next-page-map (make-sparse-keymap))
4831   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
4832   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
4833
4834 (defun gnus-button-next-page ()
4835   "Go to the next page."
4836   (interactive)
4837   (let ((win (selected-window)))
4838     (select-window (get-buffer-window gnus-article-buffer t))
4839     (gnus-article-next-page)
4840     (select-window win)))
4841
4842 (defun gnus-button-prev-page ()
4843   "Go to the prev page."
4844   (interactive)
4845   (let ((win (selected-window)))
4846     (select-window (get-buffer-window gnus-article-buffer t))
4847     (gnus-article-prev-page)
4848     (select-window win)))
4849
4850 (static-if (featurep 'xemacs)
4851     (defun gnus-insert-next-page-button ()
4852       (let ((buffer-read-only nil))
4853         (gnus-eval-format gnus-next-page-line-format nil
4854                           `(gnus-next
4855                             t local-map ,gnus-next-page-map
4856                             gnus-callback gnus-article-button-next-page
4857                             article-type annotation))))
4858   (defun gnus-insert-next-page-button ()
4859     (let ((buffer-read-only nil)
4860           (situation (get-text-property (point-min) 'mime-view-situation)))
4861       (set-keymap-parent gnus-next-page-map (current-local-map))
4862       (gnus-eval-format gnus-next-page-line-format nil
4863                         `(gnus-next
4864                           t local-map ,gnus-next-page-map
4865                           gnus-callback gnus-article-button-next-page
4866                           article-type annotation
4867                           mime-view-situation ,situation))))
4868   )
4869
4870 (defun gnus-article-button-next-page (arg)
4871   "Go to the next page."
4872   (interactive "P")
4873   (let ((win (selected-window)))
4874     (select-window (get-buffer-window gnus-article-buffer t))
4875     (gnus-article-next-page)
4876     (select-window win)))
4877
4878 (defun gnus-article-button-prev-page (arg)
4879   "Go to the prev page."
4880   (interactive "P")
4881   (let ((win (selected-window)))
4882     (select-window (get-buffer-window gnus-article-buffer t))
4883     (gnus-article-prev-page)
4884     (select-window win)))
4885
4886 (defvar gnus-decode-header-methods
4887   '(mail-decode-encoded-word-region)
4888   "List of methods used to decode headers.
4889
4890 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
4891 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
4892 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
4893 whose names match REGEXP.
4894
4895 For example:
4896 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
4897  mail-decode-encoded-word-region
4898  (\"chinese\" . rfc1843-decode-region))
4899 ")
4900
4901 (defvar gnus-decode-header-methods-cache nil)
4902
4903 (defun gnus-multi-decode-header (start end)
4904   "Apply the functions from `gnus-encoded-word-methods' that match."
4905   (unless (and gnus-decode-header-methods-cache
4906                (eq gnus-newsgroup-name
4907                    (car gnus-decode-header-methods-cache)))
4908     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
4909     (mapc '(lambda (x)
4910              (if (symbolp x)
4911                  (nconc gnus-decode-header-methods-cache (list x))
4912                (if (and gnus-newsgroup-name
4913                         (string-match (car x) gnus-newsgroup-name))
4914                    (nconc gnus-decode-header-methods-cache
4915                           (list (cdr x))))))
4916           gnus-decode-header-methods))
4917   (let ((xlist gnus-decode-header-methods-cache))
4918     (pop xlist)
4919     (save-restriction
4920       (narrow-to-region start end)
4921       (while xlist
4922         (funcall (pop xlist) (point-min) (point-max))))))
4923
4924 ;;;
4925 ;;; Treatment top-level handling.
4926 ;;;
4927
4928 (defun gnus-treat-article (condition &optional part-number total-parts type)
4929   (let ((length (- (point-max) (point-min)))
4930         (alist gnus-treatment-function-alist)
4931         (article-goto-body-goes-to-point-min-p t)
4932         (treated-type
4933          (or (not type)
4934              (catch 'found
4935                (let ((list gnus-article-treat-types))
4936                  (while list
4937                    (when (string-match (pop list) type)
4938                      (throw 'found t)))))))
4939         (highlightp (gnus-visual-p 'article-highlight 'highlight))
4940         (entity (static-unless (featurep 'xemacs)
4941                   (when (eq 'head condition)
4942                     (get-text-property (point-min) 'mime-view-entity))))
4943         val elem)
4944     (gnus-run-hooks 'gnus-part-display-hook)
4945     (unless gnus-inhibit-treatment
4946       (while (setq elem (pop alist))
4947         (setq val (symbol-value (car elem)))
4948         (when (and (or (consp val)
4949                        treated-type)
4950                    (gnus-treat-predicate val)
4951                    (or (not (get (car elem) 'highlight))
4952                        highlightp))
4953           (save-restriction
4954             (funcall (cadr elem)))))
4955       ;; FSF Emacsen does not inherit the existing text properties
4956       ;; in the new text, so we should do it for `mime-view-entity'.
4957       (static-unless (featurep 'xemacs)
4958         (when entity
4959           (put-text-property (point-min) (point-max)
4960                              'mime-view-entity entity))))))
4961
4962 ;; Dynamic variables.
4963 (defvar part-number)
4964 (defvar total-parts)
4965 (defvar type)
4966 (defvar condition)
4967 (defvar length)
4968 (defun gnus-treat-predicate (val)
4969   (cond
4970    ((eq val 'mime)
4971     (not (not gnus-show-mime)))
4972    ((null val)
4973     nil)
4974    ((listp val)
4975     (let ((pred (pop val)))
4976       (cond
4977        ((eq pred 'or)
4978         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
4979        ((eq pred 'and)
4980         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
4981        ((eq pred 'not)
4982         (not (gnus-treat-predicate (car val))))
4983        ((eq pred 'typep)
4984         (equal (cadr val) type))
4985        (t
4986         (gnus-treat-predicate pred)))))
4987    (condition
4988     (eq condition val))
4989    ((eq val t)
4990     t)
4991    ((eq val 'head)
4992     nil)
4993    ((eq val 'last)
4994     (eq part-number total-parts))
4995    ((numberp val)
4996     (< length val))
4997    ((and (listp val)
4998          (stringp (car val)))
4999     (apply 'gnus-or (mapcar `(lambda (s)
5000                                (string-match s ,(or gnus-newsgroup-name "")))
5001                             val)))
5002    (t
5003     (error "%S is not a valid value" val))))
5004
5005
5006 ;;; @ for mime-view
5007 ;;;
5008
5009 (defun gnus-article-header-presentation-method (entity situation)
5010   (mime-insert-header entity)
5011   )
5012
5013 (set-alist 'mime-header-presentation-method-alist
5014            'gnus-original-article-mode
5015            #'gnus-article-header-presentation-method)
5016
5017 (defun gnus-mime-preview-quitting-method ()
5018   (mime-preview-kill-buffer)
5019   (delete-other-windows)
5020   (gnus-article-show-summary)
5021   (gnus-summary-select-article gnus-show-all-headers t))
5022
5023 (set-alist 'mime-preview-quitting-method-alist
5024            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
5025
5026 (defun gnus-following-method (buf)
5027   (set-buffer buf)
5028   (message-followup)
5029   (message-yank-original)
5030   (kill-buffer buf)
5031   (goto-char (point-min))
5032   )
5033
5034 (set-alist 'mime-preview-following-method-alist
5035            'gnus-original-article-mode #'gnus-following-method)
5036
5037 (set-alist 'mime-preview-over-to-previous-method-alist
5038            'gnus-original-article-mode
5039            (lambda ()
5040              (if (> (point-min) 1)
5041                  (gnus-article-prev-page)
5042                (gnus-article-read-summary-keys
5043                 nil (gnus-character-to-event ?P)))))
5044
5045 (set-alist 'mime-preview-over-to-next-method-alist
5046            'gnus-original-article-mode'
5047            (lambda ()
5048              (if (< (point-max) (buffer-size))
5049                  (gnus-article-next-page)
5050                (gnus-article-read-summary-keys
5051                 nil (gnus-character-to-event ?N)))))
5052
5053
5054 ;;; @ end
5055 ;;;
5056
5057 (gnus-ems-redefine)
5058
5059 (provide 'gnus-art)
5060
5061 (run-hooks 'gnus-art-load-hook)
5062
5063 ;;; gnus-art.el ends here