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