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