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