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