b7fef0e003355f2c7ef1a81cb5e14d76cdc3a4df
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'custom)
31 (require 'gnus)
32 (require 'gnus-sum)
33 (require 'gnus-spec)
34 (require 'gnus-int)
35 (require 'browse-url)
36 (require 'mm-bodies)
37 (require 'mail-parse)
38 (require 'mm-decode)
39 (require 'mm-view)
40 (require 'wid-edit)
41 (require 'mm-uu)
42
43 (defgroup gnus-article nil
44   "Article display."
45   :link '(custom-manual "(gnus)The Article Buffer")
46   :group 'gnus)
47
48 (defgroup gnus-article-treat nil
49   "Treating article parts."
50   :link '(custom-manual "(gnus)Article Hiding")
51   :group 'gnus-article)
52
53 (defgroup gnus-article-hiding nil
54   "Hiding article parts."
55   :link '(custom-manual "(gnus)Article Hiding")
56   :group 'gnus-article)
57
58 (defgroup gnus-article-highlight nil
59   "Article highlighting."
60   :link '(custom-manual "(gnus)Article Highlighting")
61   :group 'gnus-article
62   :group 'gnus-visual)
63
64 (defgroup gnus-article-signature nil
65   "Article signatures."
66   :link '(custom-manual "(gnus)Article Signature")
67   :group 'gnus-article)
68
69 (defgroup gnus-article-headers nil
70   "Article headers."
71   :link '(custom-manual "(gnus)Hiding Headers")
72   :group 'gnus-article)
73
74 (defgroup gnus-article-washing nil
75   "Special commands on articles."
76   :link '(custom-manual "(gnus)Article Washing")
77   :group 'gnus-article)
78
79 (defgroup gnus-article-emphasis nil
80   "Fontisizing articles."
81   :link '(custom-manual "(gnus)Article Fontisizing")
82   :group 'gnus-article)
83
84 (defgroup gnus-article-saving nil
85   "Saving articles."
86   :link '(custom-manual "(gnus)Saving Articles")
87   :group 'gnus-article)
88
89 (defgroup gnus-article-mime nil
90   "Worshiping the MIME wonder."
91   :link '(custom-manual "(gnus)Using MIME")
92   :group 'gnus-article)
93
94 (defgroup gnus-article-buttons nil
95   "Pushable buttons in the article buffer."
96   :link '(custom-manual "(gnus)Article Buttons")
97   :group 'gnus-article)
98
99 (defgroup gnus-article-various nil
100   "Other article options."
101   :link '(custom-manual "(gnus)Misc Article")
102   :group 'gnus-article)
103
104 (defcustom gnus-ignored-headers
105   '("^Path:" "^Expires:" "^Date-Received:" "^References:" "^Xref:" "^Lines:"
106     "^Relay-Version:" "^Message-ID:" "^Approved:" "^Sender:" "^Received:"
107     "^X-UIDL:" "^MIME-Version:" "^Return-Path:" "^In-Reply-To:"
108     "^Content-Type:" "^Content-Transfer-Encoding:" "^X-WebTV-Signature:"
109     "^X-MimeOLE:" "^X-MSMail-Priority:" "^X-Priority:" "^X-Loop:"
110     "^X-Authentication-Warning:" "^X-MIME-Autoconverted:" "^X-Face:"
111     "^X-Attribution:" "^X-Originating-IP:" "^Delivered-To:"
112     "^NNTP-[-A-Za-z]+:" "^Distribution:" "^X-no-archive:" "^X-Trace:"
113     "^X-Complaints-To:" "^X-NNTP-Posting-Host:" "^X-Orig.*:"
114     "^Abuse-Reports-To:" "^Cache-Post-Path:" "^X-Article-Creation-Date:"
115     "^X-Poster:" "^X-Mail2News-Path:" "^X-Server-Date:" "^X-Cache:"
116     "^Originator:" "^X-Problems-To:" "^X-Auth-User:" "^X-Post-Time:"
117     "^X-Admin:" "^X-UID:" "^Resent-[-A-Za-z]+:" "^X-Mailing-List:"
118     "^Precedence:" "^Original-[-A-Za-z]+:" "^X-filename:" "^X-Orcpt:"
119     "^Old-Received:" "^X-Pgp-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-decode-encoded-word-region (point-min) (point-max)))))
1387
1388 (defun article-decode-charset (&optional prompt)
1389   "Decode charset-encoded text in the article.
1390 If PROMPT (the prefix), prompt for a coding system to use."
1391   (interactive "P")
1392   (save-excursion
1393     (save-restriction
1394       (article-narrow-to-head)
1395       (let* ((inhibit-point-motion-hooks t)
1396              (case-fold-search t)
1397              (ct (message-fetch-field "Content-Type" t))
1398              (cte (message-fetch-field "Content-Transfer-Encoding" t))
1399              (ctl (and ct (ignore-errors
1400                             (mail-header-parse-content-type ct))))
1401              (charset (cond
1402                        (prompt
1403                         (mm-read-coding-system "Charset to decode: "))
1404                        (ctl
1405                         (mail-content-type-get ctl 'charset))))
1406              (mail-parse-charset gnus-newsgroup-charset)
1407              buffer-read-only)
1408         (when (memq charset gnus-newsgroup-ignored-charsets)
1409           (setq charset nil))
1410         (goto-char (point-max))
1411         (widen)
1412         (forward-line 1)
1413         (narrow-to-region (point) (point-max))
1414         (when (and (or (not ctl)
1415                        (equal (car ctl) "text/plain"))
1416                    (not (mm-uu-test)))
1417           (mm-decode-body
1418            charset (and cte (intern (downcase
1419                                      (gnus-strip-whitespace cte))))
1420            (car ctl)))))))
1421
1422 (defun article-decode-encoded-words ()
1423   "Remove encoded-word encoding from headers."
1424   (let ((inhibit-point-motion-hooks t)
1425         (mail-parse-charset gnus-newsgroup-charset)
1426         buffer-read-only)
1427     (save-restriction
1428       (article-narrow-to-head)
1429       (funcall gnus-decode-header-function (point-min) (point-max)))))
1430
1431 (defun article-de-quoted-unreadable (&optional force)
1432   "Translate a quoted-printable-encoded article.
1433 If FORCE, decode the article whether it is marked as quoted-printable
1434 or not."
1435   (interactive (list 'force))
1436   (save-excursion
1437     (let ((buffer-read-only nil)
1438           (type (gnus-fetch-field "content-transfer-encoding"))
1439           (charset gnus-newsgroup-charset))
1440       (when (or force
1441                 (and type (string-match "quoted-printable" (downcase type))))
1442         (article-goto-body)
1443         (save-restriction
1444           (narrow-to-region (point) (point-max))
1445           (quoted-printable-decode-region (point-min) (point-max))
1446           (when charset
1447             (mm-decode-body charset)))))))
1448
1449 (defun article-hide-pgp ()
1450   "Remove any PGP headers and signatures in the current article."
1451   (interactive)
1452   (save-excursion
1453     (save-restriction
1454       (let ((inhibit-point-motion-hooks t)
1455             buffer-read-only beg end)
1456         (article-goto-body)
1457         ;; Hide the "header".
1458         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
1459           (push 'pgp gnus-article-wash-types)
1460           (delete-region (match-beginning 0) (match-end 0))
1461           ;; PGP 5 and GNU PG add a `Hash: <>' comment, hide that too
1462           (when (looking-at "Hash:.*$")
1463             (delete-region (point) (1+ (gnus-point-at-eol))))
1464           (setq beg (point))
1465           ;; Hide the actual signature.
1466           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
1467                (setq end (1+ (match-beginning 0)))
1468                (delete-region
1469                 end
1470                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
1471                     (match-end 0)
1472                   ;; Perhaps we shouldn't hide to the end of the buffer
1473                   ;; if there is no end to the signature?
1474                   (point-max))))
1475           ;; Hide "- " PGP quotation markers.
1476           (when (and beg end)
1477             (narrow-to-region beg end)
1478             (goto-char (point-min))
1479             (while (re-search-forward "^- " nil t)
1480               (delete-region
1481                (match-beginning 0) (match-end 0)))
1482             (widen))
1483           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1484
1485 (defun article-hide-pem (&optional arg)
1486   "Toggle hiding of any PEM headers and signatures in the current article.
1487 If given a negative prefix, always show; if given a positive prefix,
1488 always hide."
1489   (interactive (gnus-article-hidden-arg))
1490   (unless (gnus-article-check-hidden-text 'pem arg)
1491     (save-excursion
1492       (let (buffer-read-only end)
1493         (goto-char (point-min))
1494         ;; Hide the horrendously ugly "header".
1495         (when (and (search-forward
1496                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1497                     nil t)
1498                    (setq end (1+ (match-beginning 0))))
1499           (push 'pem gnus-article-wash-types)
1500           (gnus-article-hide-text-type
1501            end
1502            (if (search-forward "\n\n" nil t)
1503                (match-end 0)
1504              (point-max))
1505            'pem)
1506           ;; Hide the trailer as well
1507           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1508                                 nil t)
1509             (gnus-article-hide-text-type
1510              (match-beginning 0) (match-end 0) 'pem)))))))
1511
1512 (defun article-strip-banner ()
1513   "Strip the banner specified by the `banner' group parameter."
1514   (interactive)
1515   (save-excursion
1516     (save-restriction
1517       (let ((inhibit-point-motion-hooks t)
1518             (banner (gnus-group-get-parameter gnus-newsgroup-name 'banner))
1519             (gnus-signature-limit nil)
1520             buffer-read-only beg end)
1521         (when banner
1522           (article-goto-body)
1523           (cond
1524            ((eq banner 'signature)
1525             (when (gnus-article-narrow-to-signature)
1526               (widen)
1527               (forward-line -1)
1528               (delete-region (point) (point-max))))
1529            ((stringp banner)
1530             (while (re-search-forward banner nil t)
1531               (delete-region (match-beginning 0) (match-end 0))))))))))
1532
1533 (defun article-babel-prompt ()
1534   "Prompt for a babel translation."
1535   (require 'babel)
1536   (completing-read "Translate from: "
1537                    babel-translations nil t
1538                    (car (car babel-translations))
1539                    babel-history))
1540
1541 (defun article-babel (translation)
1542   "Translate article according to TRANSLATION using babelfish."
1543   (interactive (list (article-babel-prompt)))
1544   (require 'babel)
1545   (save-excursion
1546     (set-buffer gnus-article-buffer)
1547     (when (article-goto-body)
1548       (let* ((buffer-read-only nil)
1549              (start (point))
1550              (end (point-max))
1551              (msg (buffer-substring start end)))
1552         (save-restriction
1553           (narrow-to-region start end)
1554           (delete-region start end)
1555           (babel-fetch msg (cdr (assoc translation babel-translations)))
1556           (save-restriction
1557             (narrow-to-region start (point-max))
1558             (babel-wash)))))))
1559
1560 (defun article-hide-signature (&optional arg)
1561   "Hide the signature in the current article.
1562 If given a negative prefix, always show; if given a positive prefix,
1563 always hide."
1564   (interactive (gnus-article-hidden-arg))
1565   (unless (gnus-article-check-hidden-text 'signature arg)
1566     (save-excursion
1567       (save-restriction
1568         (let ((buffer-read-only nil))
1569           (when (gnus-article-narrow-to-signature)
1570             (gnus-article-hide-text-type
1571              (point-min) (point-max) 'signature)))))))
1572
1573 (defun article-strip-headers-in-body ()
1574   "Strip offensive headers from bodies."
1575   (interactive)
1576   (save-excursion
1577     (article-goto-body)
1578     (let ((case-fold-search t))
1579       (when (looking-at "x-no-archive:")
1580         (gnus-delete-line)))))
1581
1582 (defun article-strip-leading-blank-lines ()
1583   "Remove all blank lines from the beginning of the article."
1584   (interactive)
1585   (save-excursion
1586     (let ((inhibit-point-motion-hooks t)
1587           buffer-read-only)
1588       (when (article-goto-body)
1589         (while (and (not (eobp))
1590                     (looking-at "[ \t]*$"))
1591           (gnus-delete-line))))))
1592
1593 (defun article-narrow-to-head ()
1594   "Narrow the buffer to the head of the message.
1595 Point is left at the beginning of the narrowed-to region."
1596   (narrow-to-region
1597    (goto-char (point-min))
1598    (if (search-forward "\n\n" nil 1)
1599        (1- (point))
1600      (point-max)))
1601   (goto-char (point-min)))
1602
1603 (defun article-goto-body ()
1604   "Place point at the start of the body."
1605   (goto-char (point-min))
1606   (cond
1607    ;; This variable is only bound when dealing with separate
1608    ;; MIME body parts.
1609    (article-goto-body-goes-to-point-min-p
1610     t)
1611    ((search-forward "\n\n" nil t)
1612     t)
1613    (t
1614     (goto-char (point-max))
1615     nil)))
1616
1617 (defun article-strip-multiple-blank-lines ()
1618   "Replace consecutive blank lines with one empty line."
1619   (interactive)
1620   (save-excursion
1621     (let ((inhibit-point-motion-hooks t)
1622           buffer-read-only)
1623       ;; First make all blank lines empty.
1624       (article-goto-body)
1625       (while (re-search-forward "^[ \t]+$" nil t)
1626         (unless (gnus-annotation-in-region-p
1627                  (match-beginning 0) (match-end 0))
1628           (replace-match "" nil t)))
1629       ;; Then replace multiple empty lines with a single empty line.
1630       (article-goto-body)
1631       (while (re-search-forward "\n\n\n+" nil t)
1632         (unless (gnus-annotation-in-region-p
1633                  (match-beginning 0) (match-end 0))
1634           (replace-match "\n\n" t t))))))
1635
1636 (defun article-strip-leading-space ()
1637   "Remove all white space from the beginning of the lines in the article."
1638   (interactive)
1639   (save-excursion
1640     (let ((inhibit-point-motion-hooks t)
1641           buffer-read-only)
1642       (article-goto-body)
1643       (while (re-search-forward "^[ \t]+" nil t)
1644         (replace-match "" t t)))))
1645
1646 (defun article-strip-trailing-space ()
1647   "Remove all white space from the end of the lines in the article."
1648   (interactive)
1649   (save-excursion
1650     (let ((inhibit-point-motion-hooks t)
1651           buffer-read-only)
1652       (article-goto-body)
1653       (while (re-search-forward "[ \t]+$" nil t)
1654         (replace-match "" t t)))))
1655
1656 (defun article-strip-blank-lines ()
1657   "Strip leading, trailing and multiple blank lines."
1658   (interactive)
1659   (article-strip-leading-blank-lines)
1660   (article-remove-trailing-blank-lines)
1661   (article-strip-multiple-blank-lines))
1662
1663 (defun article-strip-all-blank-lines ()
1664   "Strip all blank lines."
1665   (interactive)
1666   (save-excursion
1667     (let ((inhibit-point-motion-hooks t)
1668           buffer-read-only)
1669       (article-goto-body)
1670       (while (re-search-forward "^[ \t]*\n" nil t)
1671         (replace-match "" t t)))))
1672
1673 (defun gnus-article-narrow-to-signature ()
1674   "Narrow to the signature; return t if a signature is found, else nil."
1675   (let ((inhibit-point-motion-hooks t))
1676     (when (gnus-article-search-signature)
1677       (forward-line 1)
1678       ;; Check whether we have some limits to what we consider
1679       ;; to be a signature.
1680       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1681                       (list gnus-signature-limit)))
1682             limit limited)
1683         (while (setq limit (pop limits))
1684           (if (or (and (integerp limit)
1685                        (< (- (point-max) (point)) limit))
1686                   (and (floatp limit)
1687                        (< (count-lines (point) (point-max)) limit))
1688                   (and (gnus-functionp limit)
1689                        (funcall limit))
1690                   (and (stringp limit)
1691                        (not (re-search-forward limit nil t))))
1692               ()                        ; This limit did not succeed.
1693             (setq limited t
1694                   limits nil)))
1695         (unless limited
1696           (narrow-to-region (point) (point-max))
1697           t)))))
1698
1699 (defun gnus-article-search-signature ()
1700   "Search the current buffer for the signature separator.
1701 Put point at the beginning of the signature separator."
1702   (let ((cur (point)))
1703     (goto-char (point-max))
1704     (if (if (stringp gnus-signature-separator)
1705             (re-search-backward gnus-signature-separator nil t)
1706           (let ((seps gnus-signature-separator))
1707             (while (and seps
1708                         (not (re-search-backward (car seps) nil t)))
1709               (pop seps))
1710             seps))
1711         t
1712       (goto-char cur)
1713       nil)))
1714
1715 (defun gnus-article-hidden-arg ()
1716   "Return the current prefix arg as a number, or 0 if no prefix."
1717   (list (if current-prefix-arg
1718             (prefix-numeric-value current-prefix-arg)
1719           0)))
1720
1721 (defun gnus-article-check-hidden-text (type arg)
1722   "Return nil if hiding is necessary.
1723 Arg can be nil or a number.  Nil and positive means hide, negative
1724 means show, 0 means toggle."
1725   (save-excursion
1726     (save-restriction
1727       (let ((hide (gnus-article-hidden-text-p type)))
1728         (cond
1729          ((or (null arg)
1730               (> arg 0))
1731           nil)
1732          ((< arg 0)
1733           (gnus-article-show-hidden-text type))
1734          (t
1735           (if (eq hide 'hidden)
1736               (gnus-article-show-hidden-text type)
1737             nil)))))))
1738
1739 (defun gnus-article-hidden-text-p (type)
1740   "Say whether the current buffer contains hidden text of type TYPE."
1741   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1742     (while (and pos
1743                 (not (get-text-property pos 'invisible))
1744                 (not (get-text-property pos 'dummy-invisible)))
1745       (setq pos
1746             (text-property-any (1+ pos) (point-max) 'article-type type)))
1747     (if pos
1748         'hidden
1749       nil)))
1750
1751 (defun gnus-article-show-hidden-text (type &optional hide)
1752   "Show all hidden text of type TYPE.
1753 If HIDE, hide the text instead."
1754   (save-excursion
1755     (let ((buffer-read-only nil)
1756           (inhibit-point-motion-hooks t)
1757           (end (point-min))
1758           beg)
1759       (while (setq beg (text-property-any end (point-max) 'article-type type))
1760         (goto-char beg)
1761         (setq end (or
1762                    (text-property-not-all beg (point-max) 'article-type type)
1763                    (point-max)))
1764         (if hide
1765             (gnus-article-hide-text beg end gnus-hidden-properties)
1766           (gnus-article-unhide-text beg end))
1767         (goto-char end))
1768       t)))
1769
1770 (defconst article-time-units
1771   `((year . ,(* 365.25 24 60 60))
1772     (week . ,(* 7 24 60 60))
1773     (day . ,(* 24 60 60))
1774     (hour . ,(* 60 60))
1775     (minute . 60)
1776     (second . 1))
1777   "Mapping from time units to seconds.")
1778
1779 (defun article-date-ut (&optional type highlight header)
1780   "Convert DATE date to universal time in the current article.
1781 If TYPE is `local', convert to local time; if it is `lapsed', output
1782 how much time has lapsed since DATE. For `lapsed', the value of
1783 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
1784 should replace the \"Date:\" one, or should be added below it."
1785   (interactive (list 'ut t))
1786   (let* ((header (or header
1787                      (mail-header-date (save-excursion
1788                                          (set-buffer gnus-summary-buffer)
1789                                          gnus-current-headers))
1790                      (message-fetch-field "date")
1791                      ""))
1792          (tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1793          (date-regexp
1794           (cond
1795            ((not gnus-article-date-lapsed-new-header)
1796             tdate-regexp)
1797            ((eq type 'lapsed)
1798             "^X-Sent:[ \t]")
1799            (t
1800             "^Date:[ \t]")))
1801          (date (if (vectorp header) (mail-header-date header)
1802                  header))
1803          (inhibit-point-motion-hooks t)
1804          (newline t)
1805          bface eface)
1806     (when (and date (not (string= date "")))
1807       (save-excursion
1808         (save-restriction
1809           (article-narrow-to-head)
1810           (when (re-search-forward tdate-regexp nil t)
1811             (setq bface (get-text-property (gnus-point-at-bol) 'face)
1812                   eface (get-text-property (1- (gnus-point-at-eol))
1813                                            'face))
1814             (forward-line 1))
1815           (goto-char (point-min))
1816           (let ((buffer-read-only nil))
1817             ;; Delete any old Date headers.
1818             (while (re-search-forward date-regexp nil t)
1819               (if newline
1820                   (delete-region (progn (beginning-of-line) (point))
1821                                  (progn (end-of-line) (point)))
1822                 (delete-region (progn (beginning-of-line) (point))
1823                                (progn (forward-line 1) (point))))
1824               (setq newline nil))
1825             (if (re-search-forward tdate-regexp nil t)
1826                 (forward-line 1))
1827             (insert (article-make-date-line date type))
1828             (when newline
1829               (insert "\n")
1830               (forward-line -1))
1831             ;; Do highlighting.
1832             (beginning-of-line)
1833             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1834               (put-text-property (match-beginning 1) (1+ (match-end 1))
1835                                  'face bface)
1836               (put-text-property (match-beginning 2) (match-end 2)
1837                                  'face eface))))))))
1838
1839 (defun article-make-date-line (date type)
1840   "Return a DATE line of TYPE."
1841   (let ((time (condition-case ()
1842                   (date-to-time date)
1843                 (error '(0 0)))))
1844     (cond
1845      ;; Convert to the local timezone.  We have to slap a
1846      ;; `condition-case' round the calls to the timezone
1847      ;; functions since they aren't particularly resistant to
1848      ;; buggy dates.
1849      ((eq type 'local)
1850       (let ((tz (car (current-time-zone))))
1851         (format "Date: %s %s%04d" (current-time-string time)
1852                 (if (> tz 0) "+" "-") (abs (/ tz 36)))))
1853      ;; Convert to Universal Time.
1854      ((eq type 'ut)
1855       (concat "Date: "
1856               (current-time-string
1857                (let* ((e (parse-time-string date))
1858                      (tm (apply 'encode-time e))
1859                      (ms (car tm))
1860                      (ls (- (cadr tm) (car (current-time-zone)))))
1861                  (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
1862                        ((> ls 65535) (list (1+ ms) (- ls 65536)))
1863                        (t (list ms ls)))))
1864               " UT"))
1865      ;; Get the original date from the article.
1866      ((eq type 'original)
1867       (concat "Date: " (if (string-match "\n+$" date)
1868                            (substring date 0 (match-beginning 0))
1869                          date)))
1870      ;; Let the user define the format.
1871      ((eq type 'user)
1872       (if (gnus-functionp gnus-article-time-format)
1873           (funcall gnus-article-time-format time)
1874         (concat
1875          "Date: "
1876          (format-time-string gnus-article-time-format time))))
1877      ;; ISO 8601.
1878      ((eq type 'iso8601)
1879       (concat
1880        "Date: "
1881        (format-time-string "%Y%m%dT%H%M%S" time)))
1882      ;; Do an X-Sent lapsed format.
1883      ((eq type 'lapsed)
1884       ;; If the date is seriously mangled, the timezone functions are
1885       ;; liable to bug out, so we ignore all errors.
1886       (let* ((now (current-time))
1887              (real-time (subtract-time now time))
1888              (real-sec (and real-time
1889                             (+ (* (float (car real-time)) 65536)
1890                                (cadr real-time))))
1891              (sec (and real-time (abs real-sec)))
1892              num prev)
1893         (cond
1894          ((null real-time)
1895           "X-Sent: Unknown")
1896          ((zerop sec)
1897           "X-Sent: Now")
1898          (t
1899           (concat
1900            "X-Sent: "
1901            ;; This is a bit convoluted, but basically we go
1902            ;; through the time units for years, weeks, etc,
1903            ;; and divide things to see whether that results
1904            ;; in positive answers.
1905            (mapconcat
1906             (lambda (unit)
1907               (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1908                   ;; The (remaining) seconds are too few to
1909                   ;; be divided into this time unit.
1910                   ""
1911                 ;; It's big enough, so we output it.
1912                 (setq sec (- sec (* num (cdr unit))))
1913                 (prog1
1914                     (concat (if prev ", " "") (int-to-string
1915                                                (floor num))
1916                             " " (symbol-name (car unit))
1917                             (if (> num 1) "s" ""))
1918                   (setq prev t))))
1919             article-time-units "")
1920            ;; If dates are odd, then it might appear like the
1921            ;; article was sent in the future.
1922            (if (> real-sec 0)
1923                " ago"
1924              " in the future"))))))
1925      (t
1926       (error "Unknown conversion type: %s" type)))))
1927
1928 (defun article-date-local (&optional highlight)
1929   "Convert the current article date to the local timezone."
1930   (interactive (list t))
1931   (article-date-ut 'local highlight))
1932
1933 (defun article-date-original (&optional highlight)
1934   "Convert the current article date to what it was originally.
1935 This is only useful if you have used some other date conversion
1936 function and want to see what the date was before converting."
1937   (interactive (list t))
1938   (article-date-ut 'original highlight))
1939
1940 (defun article-date-lapsed (&optional highlight)
1941   "Convert the current article date to time lapsed since it was sent."
1942   (interactive (list t))
1943   (article-date-ut 'lapsed highlight))
1944
1945 (defun article-update-date-lapsed ()
1946   "Function to be run from a timer to update the lapsed time line."
1947   (let (deactivate-mark)
1948     (save-excursion
1949       (ignore-errors
1950         (walk-windows
1951          (lambda (w)
1952            (set-buffer (window-buffer w))
1953            (when (eq major-mode 'gnus-article-mode)
1954              (goto-char (point-min))
1955              (when (re-search-forward "^X-Sent:" nil t)
1956                (article-date-lapsed t))))
1957          nil 'visible)))))
1958
1959 (defun gnus-start-date-timer (&optional n)
1960   "Start a timer to update the X-Sent header in the article buffers.
1961 The numerical prefix says how frequently (in seconds) the function
1962 is to run."
1963   (interactive "p")
1964   (unless n
1965     (setq n 1))
1966   (gnus-stop-date-timer)
1967   (setq article-lapsed-timer
1968         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
1969
1970 (defun gnus-stop-date-timer ()
1971   "Stop the X-Sent timer."
1972   (interactive)
1973   (when article-lapsed-timer
1974     (nnheader-cancel-timer article-lapsed-timer)
1975     (setq article-lapsed-timer nil)))
1976
1977 (defun article-date-user (&optional highlight)
1978   "Convert the current article date to the user-defined format.
1979 This format is defined by the `gnus-article-time-format' variable."
1980   (interactive (list t))
1981   (article-date-ut 'user highlight))
1982
1983 (defun article-date-iso8601 (&optional highlight)
1984   "Convert the current article date to ISO8601."
1985   (interactive (list t))
1986   (article-date-ut 'iso8601 highlight))
1987
1988 (defun article-show-all ()
1989   "Show all hidden text in the article buffer."
1990   (interactive)
1991   (save-excursion
1992     (let ((buffer-read-only nil))
1993       (gnus-article-unhide-text (point-min) (point-max)))))
1994
1995 (defun article-emphasize (&optional arg)
1996   "Emphasize text according to `gnus-emphasis-alist'."
1997   (interactive (gnus-article-hidden-arg))
1998   (unless (gnus-article-check-hidden-text 'emphasis arg)
1999     (save-excursion
2000       (let ((alist (or gnus-article-emphasis-alist gnus-emphasis-alist))
2001             (buffer-read-only nil)
2002             (props (append '(article-type emphasis)
2003                            gnus-hidden-properties))
2004             regexp elem beg invisible visible face)
2005         (article-goto-body)
2006         (setq beg (point))
2007         (while (setq elem (pop alist))
2008           (goto-char beg)
2009           (setq regexp (car elem)
2010                 invisible (nth 1 elem)
2011                 visible (nth 2 elem)
2012                 face (nth 3 elem))
2013           (while (re-search-forward regexp nil t)
2014             (when (and (match-beginning visible) (match-beginning invisible))
2015               (gnus-article-hide-text
2016                (match-beginning invisible) (match-end invisible) props)
2017               (gnus-article-unhide-text-type
2018                (match-beginning visible) (match-end visible) 'emphasis)
2019               (gnus-put-text-property-excluding-newlines
2020                (match-beginning visible) (match-end visible) 'face face)
2021               (goto-char (match-end invisible)))))))))
2022
2023 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2024   "Setup newsgroup emphasis alist."
2025   (unless gnus-article-emphasis-alist
2026     (let ((name (and gnus-newsgroup-name
2027                      (gnus-group-real-name gnus-newsgroup-name))))
2028       (make-local-variable 'gnus-article-emphasis-alist)
2029       (setq gnus-article-emphasis-alist 
2030             (nconc 
2031              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2032                (while (setq elem (pop alist))
2033                  (when (and name (string-match (car elem) name))
2034                    (setq alist nil
2035                          highlight (copy-list (cdr elem)))))
2036                highlight)
2037              (copy-list highlight-words)
2038              (if gnus-newsgroup-name
2039                  (copy-list (gnus-group-find-parameter 
2040                              gnus-newsgroup-name 'highlight-words t)))
2041              gnus-emphasis-alist)))))
2042
2043 (defvar gnus-summary-article-menu)
2044 (defvar gnus-summary-post-menu)
2045
2046 ;;; Saving functions.
2047
2048 (defun gnus-article-save (save-buffer file &optional num)
2049   "Save the currently selected article."
2050   (unless gnus-save-all-headers
2051     ;; Remove headers according to `gnus-saved-headers'.
2052     (let ((gnus-visible-headers
2053            (or gnus-saved-headers gnus-visible-headers))
2054           (gnus-article-buffer save-buffer))
2055       (save-excursion
2056         (set-buffer save-buffer)
2057         (article-hide-headers 1 t))))
2058   (save-window-excursion
2059     (if (not gnus-default-article-saver)
2060         (error "No default saver is defined")
2061       ;; !!! Magic!  The saving functions all save
2062       ;; `gnus-save-article-buffer' (or so they think), but we
2063       ;; bind that variable to our save-buffer.
2064       (set-buffer gnus-article-buffer)
2065       (let* ((gnus-save-article-buffer save-buffer)
2066              (filename
2067               (cond
2068                ((not gnus-prompt-before-saving) 'default)
2069                ((eq gnus-prompt-before-saving 'always) nil)
2070                (t file)))
2071              (gnus-number-of-articles-to-be-saved
2072               (when (eq gnus-prompt-before-saving t)
2073                 num)))                  ; Magic
2074         (set-buffer gnus-article-current-summary)
2075         (funcall gnus-default-article-saver filename)))))
2076
2077 (defun gnus-read-save-file-name (prompt &optional filename
2078                                         function group headers variable)
2079   (let ((default-name
2080           (funcall function group headers (symbol-value variable)))
2081         result)
2082     (setq
2083      result
2084      (cond
2085       ((eq filename 'default)
2086        default-name)
2087       ((eq filename t)
2088        default-name)
2089       (filename filename)
2090       (t
2091        (let* ((split-name (gnus-get-split-value gnus-split-methods))
2092               (prompt
2093                (format prompt
2094                        (if (and gnus-number-of-articles-to-be-saved
2095                                 (> gnus-number-of-articles-to-be-saved 1))
2096                            (format "these %d articles"
2097                                    gnus-number-of-articles-to-be-saved)
2098                          "this article")))
2099               (file
2100                ;; Let the split methods have their say.
2101                (cond
2102                 ;; No split name was found.
2103                 ((null split-name)
2104                  (read-file-name
2105                   (concat prompt " (default "
2106                           (file-name-nondirectory default-name) ") ")
2107                   (file-name-directory default-name)
2108                   default-name))
2109                 ;; A single group name is returned.
2110                 ((stringp split-name)
2111                  (setq default-name
2112                        (funcall function split-name headers
2113                                 (symbol-value variable)))
2114                  (read-file-name
2115                   (concat prompt " (default "
2116                           (file-name-nondirectory default-name) ") ")
2117                   (file-name-directory default-name)
2118                   default-name))
2119                 ;; A single split name was found
2120                 ((= 1 (length split-name))
2121                  (let* ((name (expand-file-name
2122                                (car split-name) gnus-article-save-directory))
2123                         (dir (cond ((file-directory-p name)
2124                                     (file-name-as-directory name))
2125                                    ((file-exists-p name) name)
2126                                    (t gnus-article-save-directory))))
2127                    (read-file-name
2128                     (concat prompt " (default " name ") ")
2129                     dir name)))
2130                 ;; A list of splits was found.
2131                 (t
2132                  (setq split-name (nreverse split-name))
2133                  (let (result)
2134                    (let ((file-name-history
2135                           (nconc split-name file-name-history)))
2136                      (setq result
2137                            (expand-file-name
2138                             (read-file-name
2139                              (concat prompt " (`M-p' for defaults) ")
2140                              gnus-article-save-directory
2141                              (car split-name))
2142                             gnus-article-save-directory)))
2143                    (car (push result file-name-history)))))))
2144          ;; Create the directory.
2145          (gnus-make-directory (file-name-directory file))
2146          ;; If we have read a directory, we append the default file name.
2147          (when (file-directory-p file)
2148            (setq file (concat (file-name-as-directory file)
2149                               (file-name-nondirectory default-name))))
2150          ;; Possibly translate some characters.
2151          (nnheader-translate-file-chars file)))))
2152     (gnus-make-directory (file-name-directory result))
2153     (set variable result)))
2154
2155 (defun gnus-article-archive-name (group)
2156   "Return the first instance of an \"Archive-name\" in the current buffer."
2157   (let ((case-fold-search t))
2158     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2159       (nnheader-concat gnus-article-save-directory
2160                        (match-string 1)))))
2161
2162 (defun gnus-article-nndoc-name (group)
2163   "If GROUP is an nndoc group, return the name of the parent group."
2164   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2165     (gnus-group-get-parameter group 'save-article-group)))
2166
2167 (defun gnus-summary-save-in-rmail (&optional filename)
2168   "Append this article to Rmail file.
2169 Optional argument FILENAME specifies file name.
2170 Directory to save to is default to `gnus-article-save-directory'."
2171   (setq filename (gnus-read-save-file-name
2172                   "Save %s in rmail file:" filename
2173                   gnus-rmail-save-name gnus-newsgroup-name
2174                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2175   (gnus-eval-in-buffer-window gnus-save-article-buffer
2176     (save-excursion
2177       (save-restriction
2178         (widen)
2179         (gnus-output-to-rmail filename))))
2180   filename)
2181
2182 (defun gnus-summary-save-in-mail (&optional filename)
2183   "Append this article to Unix mail file.
2184 Optional argument FILENAME specifies file name.
2185 Directory to save to is default to `gnus-article-save-directory'."
2186   (setq filename (gnus-read-save-file-name
2187                   "Save %s in Unix mail file:" filename
2188                   gnus-mail-save-name gnus-newsgroup-name
2189                   gnus-current-headers 'gnus-newsgroup-last-mail))
2190   (gnus-eval-in-buffer-window gnus-save-article-buffer
2191     (save-excursion
2192       (save-restriction
2193         (widen)
2194         (if (and (file-readable-p filename)
2195                  (mail-file-babyl-p filename))
2196             (rmail-output-to-rmail-file filename t)
2197           (gnus-output-to-mail filename)))))
2198   filename)
2199
2200 (defun gnus-summary-save-in-file (&optional filename overwrite)
2201   "Append this article to file.
2202 Optional argument FILENAME specifies file name.
2203 Directory to save to is default to `gnus-article-save-directory'."
2204   (setq filename (gnus-read-save-file-name
2205                   "Save %s in file:" filename
2206                   gnus-file-save-name gnus-newsgroup-name
2207                   gnus-current-headers 'gnus-newsgroup-last-file))
2208   (gnus-eval-in-buffer-window gnus-save-article-buffer
2209     (save-excursion
2210       (save-restriction
2211         (widen)
2212         (when (and overwrite
2213                    (file-exists-p filename))
2214           (delete-file filename))
2215         (gnus-output-to-file filename))))
2216   filename)
2217
2218 (defun gnus-summary-write-to-file (&optional filename)
2219   "Write this article to a file.
2220 Optional argument FILENAME specifies file name.
2221 The directory to save in defaults to `gnus-article-save-directory'."
2222   (gnus-summary-save-in-file nil t))
2223
2224 (defun gnus-summary-save-body-in-file (&optional filename)
2225   "Append this article body to a file.
2226 Optional argument FILENAME specifies file name.
2227 The directory to save in defaults to `gnus-article-save-directory'."
2228   (setq filename (gnus-read-save-file-name
2229                   "Save %s body in file:" filename
2230                   gnus-file-save-name gnus-newsgroup-name
2231                   gnus-current-headers 'gnus-newsgroup-last-file))
2232   (gnus-eval-in-buffer-window gnus-save-article-buffer
2233     (save-excursion
2234       (save-restriction
2235         (widen)
2236         (when (article-goto-body)
2237           (narrow-to-region (point) (point-max)))
2238         (gnus-output-to-file filename))))
2239   filename)
2240
2241 (defun gnus-summary-save-in-pipe (&optional command)
2242   "Pipe this article to subprocess."
2243   (setq command
2244         (cond ((and (eq command 'default)
2245                     gnus-last-shell-command)
2246                gnus-last-shell-command)
2247               (command command)
2248               (t (read-string
2249                   (format
2250                    "Shell command on %s: "
2251                    (if (and gnus-number-of-articles-to-be-saved
2252                             (> gnus-number-of-articles-to-be-saved 1))
2253                        (format "these %d articles"
2254                                gnus-number-of-articles-to-be-saved)
2255                      "this article"))
2256                   gnus-last-shell-command))))
2257   (when (string-equal command "")
2258     (setq command gnus-last-shell-command))
2259   (gnus-eval-in-buffer-window gnus-article-buffer
2260     (save-restriction
2261       (widen)
2262       (shell-command-on-region (point-min) (point-max) command nil)))
2263   (setq gnus-last-shell-command command))
2264
2265 ;;; Article file names when saving.
2266
2267 (defun gnus-capitalize-newsgroup (newsgroup)
2268   "Capitalize NEWSGROUP name."
2269   (when (not (zerop (length newsgroup)))
2270     (concat (char-to-string (upcase (aref newsgroup 0)))
2271             (substring newsgroup 1))))
2272
2273 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2274   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2275 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2276 Otherwise, it is like ~/News/news/group/num."
2277   (let ((default
2278           (expand-file-name
2279            (concat (if (gnus-use-long-file-name 'not-save)
2280                        (gnus-capitalize-newsgroup newsgroup)
2281                      (gnus-newsgroup-directory-form newsgroup))
2282                    "/" (int-to-string (mail-header-number headers)))
2283            gnus-article-save-directory)))
2284     (if (and last-file
2285              (string-equal (file-name-directory default)
2286                            (file-name-directory last-file))
2287              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2288         default
2289       (or last-file default))))
2290
2291 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2292   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2293 If variable `gnus-use-long-file-name' is non-nil, it is
2294 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2295   (let ((default
2296           (expand-file-name
2297            (concat (if (gnus-use-long-file-name 'not-save)
2298                        newsgroup
2299                      (gnus-newsgroup-directory-form newsgroup))
2300                    "/" (int-to-string (mail-header-number headers)))
2301            gnus-article-save-directory)))
2302     (if (and last-file
2303              (string-equal (file-name-directory default)
2304                            (file-name-directory last-file))
2305              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2306         default
2307       (or last-file default))))
2308
2309 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2310   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2311 If variable `gnus-use-long-file-name' is non-nil, it is
2312 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2313   (or last-file
2314       (expand-file-name
2315        (if (gnus-use-long-file-name 'not-save)
2316            (gnus-capitalize-newsgroup newsgroup)
2317          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2318        gnus-article-save-directory)))
2319
2320 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2321   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2322 If variable `gnus-use-long-file-name' is non-nil, it is
2323 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2324   (or last-file
2325       (expand-file-name
2326        (if (gnus-use-long-file-name 'not-save)
2327            newsgroup
2328          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2329        gnus-article-save-directory)))
2330
2331 (eval-and-compile
2332   (mapcar
2333    (lambda (func)
2334      (let (afunc gfunc)
2335        (if (consp func)
2336            (setq afunc (car func)
2337                  gfunc (cdr func))
2338          (setq afunc func
2339                gfunc (intern (format "gnus-%s" func))))
2340        (fset gfunc
2341              (if (not (fboundp afunc))
2342                  nil
2343                `(lambda (&optional interactive &rest args)
2344                   ,(documentation afunc t)
2345                   (interactive (list t))
2346                   (save-excursion
2347                     (set-buffer gnus-article-buffer)
2348                     (if interactive
2349                         (call-interactively ',afunc)
2350                       (apply ',afunc args))))))))
2351    '(article-hide-headers
2352      article-hide-boring-headers
2353      article-treat-overstrike
2354      article-fill-long-lines
2355      article-capitalize-sentences
2356      article-remove-cr
2357      article-display-x-face
2358      article-de-quoted-unreadable
2359      article-mime-decode-quoted-printable
2360      article-hide-pgp
2361      article-strip-banner
2362      article-babel
2363      article-hide-pem
2364      article-hide-signature
2365      article-strip-headers-in-body
2366      article-remove-trailing-blank-lines
2367      article-strip-leading-blank-lines
2368      article-strip-multiple-blank-lines
2369      article-strip-leading-space
2370      article-strip-trailing-space
2371      article-strip-blank-lines
2372      article-strip-all-blank-lines
2373      article-date-local
2374      article-date-iso8601
2375      article-date-original
2376      article-date-ut
2377      article-decode-mime-words
2378      article-decode-charset
2379      article-decode-encoded-words
2380      article-date-user
2381      article-date-lapsed
2382      article-emphasize
2383      article-treat-dumbquotes
2384      article-normalize-headers
2385      (article-show-all . gnus-article-show-all-headers))))
2386 \f
2387 ;;;
2388 ;;; Gnus article mode
2389 ;;;
2390
2391 (put 'gnus-article-mode 'mode-class 'special)
2392
2393 (set-keymap-parent gnus-article-mode-map widget-keymap)
2394
2395 (gnus-define-keys gnus-article-mode-map
2396   " " gnus-article-goto-next-page
2397   "\177" gnus-article-goto-prev-page
2398   [delete] gnus-article-goto-prev-page
2399   [backspace] gnus-article-goto-prev-page
2400   "\C-c^" gnus-article-refer-article
2401   "h" gnus-article-show-summary
2402   "s" gnus-article-show-summary
2403   "\C-c\C-m" gnus-article-mail
2404   "?" gnus-article-describe-briefly
2405   "e" gnus-article-edit
2406   "<" beginning-of-buffer
2407   ">" end-of-buffer
2408   "\C-c\C-i" gnus-info-find-node
2409   "\C-c\C-b" gnus-bug
2410
2411   "\C-d" gnus-article-read-summary-keys
2412   "\M-*" gnus-article-read-summary-keys
2413   "\M-#" gnus-article-read-summary-keys
2414   "\M-^" gnus-article-read-summary-keys
2415   "\M-g" gnus-article-read-summary-keys)
2416
2417 (substitute-key-definition
2418  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
2419
2420 (defun gnus-article-make-menu-bar ()
2421   (gnus-turn-off-edit-menu 'article)
2422   (unless (boundp 'gnus-article-article-menu)
2423     (easy-menu-define
2424      gnus-article-article-menu gnus-article-mode-map ""
2425      '("Article"
2426        ["Scroll forwards" gnus-article-goto-next-page t]
2427        ["Scroll backwards" gnus-article-goto-prev-page t]
2428        ["Show summary" gnus-article-show-summary t]
2429        ["Fetch Message-ID at point" gnus-article-refer-article t]
2430        ["Mail to address at point" gnus-article-mail t]
2431        ["Send a bug report" gnus-bug t]))
2432
2433     (easy-menu-define
2434      gnus-article-treatment-menu gnus-article-mode-map ""
2435      '("Treatment"
2436        ["Hide headers" gnus-article-hide-headers t]
2437        ["Hide signature" gnus-article-hide-signature t]
2438        ["Hide citation" gnus-article-hide-citation t]
2439        ["Treat overstrike" gnus-article-treat-overstrike t]
2440        ["Remove carriage return" gnus-article-remove-cr t]
2441        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]))
2442
2443     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
2444
2445     (when (boundp 'gnus-summary-post-menu)
2446       (define-key gnus-article-mode-map [menu-bar post]
2447         (cons "Post" gnus-summary-post-menu)))
2448
2449     (gnus-run-hooks 'gnus-article-menu-hook)))
2450
2451 (defun gnus-article-mode ()
2452   "Major mode for displaying an article.
2453
2454 All normal editing commands are switched off.
2455
2456 The following commands are available in addition to all summary mode
2457 commands:
2458 \\<gnus-article-mode-map>
2459 \\[gnus-article-next-page]\t Scroll the article one page forwards
2460 \\[gnus-article-prev-page]\t Scroll the article one page backwards
2461 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
2462 \\[gnus-article-show-summary]\t Display the summary buffer
2463 \\[gnus-article-mail]\t Send a reply to the address near point
2464 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
2465 \\[gnus-info-find-node]\t Go to the Gnus info node"
2466   (interactive)
2467   (when (gnus-visual-p 'article-menu 'menu)
2468     (gnus-article-make-menu-bar))
2469   (gnus-simplify-mode-line)
2470   (setq mode-name "Article")
2471   (setq major-mode 'gnus-article-mode)
2472   (make-local-variable 'minor-mode-alist)
2473   (use-local-map gnus-article-mode-map)
2474   (gnus-update-format-specifications nil 'article-mode)
2475   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
2476   (make-local-variable 'gnus-page-broken)
2477   (make-local-variable 'gnus-button-marker-list)
2478   (make-local-variable 'gnus-article-current-summary)
2479   (make-local-variable 'gnus-article-mime-handles)
2480   (make-local-variable 'gnus-article-decoded-p)
2481   (make-local-variable 'gnus-article-mime-handle-alist)
2482   (make-local-variable 'gnus-article-washed-types)
2483   (gnus-set-default-directory)
2484   (buffer-disable-undo)
2485   (setq buffer-read-only t)
2486   (set-syntax-table gnus-article-mode-syntax-table)
2487   (mm-enable-multibyte)
2488   (gnus-run-hooks 'gnus-article-mode-hook))
2489
2490 (defun gnus-article-setup-buffer ()
2491   "Initialize the article buffer."
2492   (let* ((name (if gnus-single-article-buffer "*Article*"
2493                  (concat "*Article " gnus-newsgroup-name "*")))
2494          (original
2495           (progn (string-match "\\*Article" name)
2496                  (concat " *Original Article"
2497                          (substring name (match-end 0))))))
2498     (setq gnus-article-buffer name)
2499     (setq gnus-original-article-buffer original)
2500     (setq gnus-article-mime-handle-alist nil)
2501     ;; This might be a variable local to the summary buffer.
2502     (unless gnus-single-article-buffer
2503       (save-excursion
2504         (set-buffer gnus-summary-buffer)
2505         (setq gnus-article-buffer name)
2506         (setq gnus-original-article-buffer original)
2507         (gnus-set-global-variables)))
2508     (gnus-article-setup-highlight-words)
2509     ;; Init original article buffer.
2510     (save-excursion
2511       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2512       (mm-enable-multibyte)
2513       (setq major-mode 'gnus-original-article-mode)
2514       (make-local-variable 'gnus-original-article))
2515     (if (get-buffer name)
2516         (save-excursion
2517           (set-buffer name)
2518           (buffer-disable-undo)
2519           (setq buffer-read-only t)
2520           (unless (eq major-mode 'gnus-article-mode)
2521             (gnus-article-mode))
2522           (current-buffer))
2523       (save-excursion
2524         (set-buffer (gnus-get-buffer-create name))
2525         (gnus-article-mode)
2526         (make-local-variable 'gnus-summary-buffer)
2527         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2528         (current-buffer)))))
2529
2530 ;; Set article window start at LINE, where LINE is the number of lines
2531 ;; from the head of the article.
2532 (defun gnus-article-set-window-start (&optional line)
2533   (set-window-start
2534    (get-buffer-window gnus-article-buffer t)
2535    (save-excursion
2536      (set-buffer gnus-article-buffer)
2537      (goto-char (point-min))
2538      (if (not line)
2539          (point-min)
2540        (gnus-message 6 "Moved to bookmark")
2541        (search-forward "\n\n" nil t)
2542        (forward-line line)
2543        (point)))))
2544
2545 (defun gnus-article-prepare (article &optional all-headers header)
2546   "Prepare ARTICLE in article mode buffer.
2547 ARTICLE should either be an article number or a Message-ID.
2548 If ARTICLE is an id, HEADER should be the article headers.
2549 If ALL-HEADERS is non-nil, no headers are hidden."
2550   (save-excursion
2551     ;; Make sure we start in a summary buffer.
2552     (unless (eq major-mode 'gnus-summary-mode)
2553       (set-buffer gnus-summary-buffer))
2554     (setq gnus-summary-buffer (current-buffer))
2555     (let* ((gnus-article (if header (mail-header-number header) article))
2556            (summary-buffer (current-buffer))
2557            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2558            (group gnus-newsgroup-name)
2559            result)
2560       (save-excursion
2561         (gnus-article-setup-buffer)
2562         (set-buffer gnus-article-buffer)
2563         ;; Deactivate active regions.
2564         (when (and (boundp 'transient-mark-mode)
2565                    transient-mark-mode)
2566           (setq mark-active nil))
2567         (if (not (setq result (let ((buffer-read-only nil))
2568                                 (gnus-request-article-this-buffer
2569                                  article group))))
2570             ;; There is no such article.
2571             (save-excursion
2572               (when (and (numberp article)
2573                          (not (memq article gnus-newsgroup-sparse)))
2574                 (setq gnus-article-current
2575                       (cons gnus-newsgroup-name article))
2576                 (set-buffer gnus-summary-buffer)
2577                 (setq gnus-current-article article)
2578                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2579                     (progn
2580                       (gnus-summary-set-agent-mark article)
2581                       (message "Message marked for downloading"))
2582                   (gnus-summary-mark-article article gnus-canceled-mark)
2583                   (unless (memq article gnus-newsgroup-sparse)
2584                     (gnus-error 1
2585                      "No such article (may have expired or been canceled)")))))
2586           (if (or (eq result 'pseudo)
2587                   (eq result 'nneething))
2588               (progn
2589                 (save-excursion
2590                   (set-buffer summary-buffer)
2591                   (push article gnus-newsgroup-history)
2592                   (setq gnus-last-article gnus-current-article
2593                         gnus-current-article 0
2594                         gnus-current-headers nil
2595                         gnus-article-current nil)
2596                   (if (eq result 'nneething)
2597                       (gnus-configure-windows 'summary)
2598                     (gnus-configure-windows 'article))
2599                   (gnus-set-global-variables))
2600                 (let ((gnus-article-mime-handle-alist-1
2601                        gnus-article-mime-handle-alist))
2602                   (gnus-set-mode-line 'article)))
2603             ;; The result from the `request' was an actual article -
2604             ;; or at least some text that is now displayed in the
2605             ;; article buffer.
2606             (when (and (numberp article)
2607                        (not (eq article gnus-current-article)))
2608               ;; Seems like a new article has been selected.
2609               ;; `gnus-current-article' must be an article number.
2610               (save-excursion
2611                 (set-buffer summary-buffer)
2612                 (push article gnus-newsgroup-history)
2613                 (setq gnus-last-article gnus-current-article
2614                       gnus-current-article article
2615                       gnus-current-headers
2616                       (gnus-summary-article-header gnus-current-article)
2617                       gnus-article-current
2618                       (cons gnus-newsgroup-name gnus-current-article))
2619                 (unless (vectorp gnus-current-headers)
2620                   (setq gnus-current-headers nil))
2621                 (gnus-summary-goto-subject gnus-current-article)
2622                 (when (gnus-summary-show-thread)
2623                   ;; If the summary buffer really was folded, the
2624                   ;; previous goto may not actually have gone to
2625                   ;; the right article, but the thread root instead.
2626                   ;; So we go again.
2627                   (gnus-summary-goto-subject gnus-current-article))
2628                 (gnus-run-hooks 'gnus-mark-article-hook)
2629                 (gnus-set-mode-line 'summary)
2630                 (when (gnus-visual-p 'article-highlight 'highlight)
2631                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2632                 ;; Set the global newsgroup variables here.
2633                 (gnus-set-global-variables)
2634                 (setq gnus-have-all-headers
2635                       (or all-headers gnus-show-all-headers))))
2636             (when (or (numberp article)
2637                       (stringp article))
2638               (gnus-article-prepare-display)
2639               ;; Do page break.
2640               (goto-char (point-min))
2641               (setq gnus-page-broken
2642                     (when gnus-break-pages
2643                       (gnus-narrow-to-page)
2644                       t)))
2645             (let ((gnus-article-mime-handle-alist-1
2646                    gnus-article-mime-handle-alist))
2647               (gnus-set-mode-line 'article))
2648             (article-goto-body)
2649             (set-window-point (get-buffer-window (current-buffer)) (point))
2650             (gnus-configure-windows 'article)
2651             t))))))
2652
2653 ;;;###autoload
2654 (defun gnus-article-prepare-display ()
2655   "Make the current buffer look like a nice article."
2656   ;; Hooks for getting information from the article.
2657   ;; This hook must be called before being narrowed.
2658   (let ((gnus-article-buffer (current-buffer))
2659         buffer-read-only)
2660     (unless (eq major-mode 'gnus-article-mode)
2661       (gnus-article-mode))
2662     (setq buffer-read-only nil)
2663     (gnus-run-hooks 'gnus-tmp-internal-hook)
2664     (gnus-run-hooks 'gnus-article-prepare-hook)
2665     (when gnus-display-mime-function
2666       (funcall gnus-display-mime-function))))
2667
2668 ;;;
2669 ;;; Gnus MIME viewing functions
2670 ;;;
2671
2672 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
2673   "The following specs can be used:
2674 %t  The MIME type
2675 %T  MIME type, along with additional info
2676 %n  The `name' parameter
2677 %d  The description, if any
2678 %l  The length of the encoded part
2679 %p  The part identifier number
2680 %e  Dots if the part isn't displayed")
2681
2682 (defvar gnus-mime-button-line-format-alist
2683   '((?t gnus-tmp-type ?s)
2684     (?T gnus-tmp-type-long ?s)
2685     (?n gnus-tmp-name ?s)
2686     (?d gnus-tmp-description ?s)
2687     (?p gnus-tmp-id ?s)
2688     (?l gnus-tmp-length ?d)
2689     (?e gnus-tmp-dots ?s)))
2690
2691 (defvar gnus-mime-button-commands
2692   '((gnus-article-press-button  "\r"    "Toggle Display")
2693     (gnus-mime-view-part        "v"     "View Interactively...")
2694     (gnus-mime-save-part        "o"     "Save...")
2695     (gnus-mime-copy-part        "c"     "View As Text, In Other Buffer")
2696     (gnus-mime-inline-part      "i"     "View As Text, In This Buffer")
2697     (gnus-mime-internalize-part "E"     "View Internally")
2698     (gnus-mime-externalize-part "e"     "View Externally")
2699     (gnus-mime-pipe-part        "|"     "Pipe To Command...")))
2700
2701 (defun gnus-article-mime-part-status ()
2702   (if gnus-article-mime-handle-alist-1
2703       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
2704     ""))
2705
2706 (defvar gnus-mime-button-map nil)
2707 (unless gnus-mime-button-map
2708   (setq gnus-mime-button-map (make-sparse-keymap))
2709   (set-keymap-parent gnus-mime-button-map gnus-article-mode-map)
2710   (define-key gnus-mime-button-map gnus-mouse-2 'gnus-article-push-button)
2711   (define-key gnus-mime-button-map gnus-down-mouse-3 'gnus-mime-button-menu)
2712   (mapcar (lambda (c)
2713             (define-key gnus-mime-button-map (cadr c) (car c)))
2714           gnus-mime-button-commands))
2715
2716 (defun gnus-mime-button-menu (event)
2717   "Construct a context-sensitive menu of MIME commands."
2718   (interactive "e")
2719   (save-excursion
2720     (let ((pos (event-start event)))
2721       (set-buffer (window-buffer (posn-window pos)))
2722       (goto-char (posn-point pos))
2723       (gnus-article-check-buffer)
2724       (let ((response (x-popup-menu
2725                        t `("MIME Part"
2726                            ("" ,@(mapcar (lambda (c)
2727                                            (cons (caddr c) (car c)))
2728                                          gnus-mime-button-commands))))))
2729         (if response
2730             (funcall response))))))
2731
2732 (defun gnus-mime-view-all-parts (&optional handles)
2733   "View all the MIME parts."
2734   (interactive)
2735   (save-current-buffer
2736     (set-buffer gnus-article-buffer)
2737     (let ((handles (or handles gnus-article-mime-handles))
2738           (mail-parse-charset gnus-newsgroup-charset))
2739       (if (stringp (car handles))
2740           (gnus-mime-view-all-parts (cdr handles))
2741         (mapcar 'mm-display-part handles)))))
2742
2743 (defun gnus-mime-save-part ()
2744   "Save the MIME part under point."
2745   (interactive)
2746   (gnus-article-check-buffer)
2747   (let ((data (get-text-property (point) 'gnus-data)))
2748     (mm-save-part data)))
2749
2750 (defun gnus-mime-pipe-part ()
2751   "Pipe the MIME part under point to a process."
2752   (interactive)
2753   (gnus-article-check-buffer)
2754   (let ((data (get-text-property (point) 'gnus-data)))
2755     (mm-pipe-part data)))
2756
2757 (defun gnus-mime-view-part ()
2758   "Interactively choose a view method for the MIME part under point."
2759   (interactive)
2760   (gnus-article-check-buffer)
2761   (let ((data (get-text-property (point) 'gnus-data)))
2762     (mm-interactively-view-part data)))
2763
2764 (defun gnus-mime-copy-part (&optional handle)
2765   "Put the the MIME part under point into a new buffer."
2766   (interactive)
2767   (gnus-article-check-buffer)
2768   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2769          (contents (mm-get-part handle))|
2770          (base (file-name-nondirectory
2771                 (or
2772                  (mail-content-type-get (mm-handle-type handle) 'name)
2773                  (mail-content-type-get (mm-handle-type handle)
2774                                         'filename)
2775                  "*decoded*")))
2776          (buffer (generate-new-buffer base)))
2777     (switch-to-buffer buffer)
2778     (insert contents)
2779     ;; We do it this way to make `normal-mode' set the appropriate mode.
2780     (unwind-protect
2781         (progn
2782           (setq buffer-file-name (expand-file-name base))
2783           (normal-mode))
2784       (setq buffer-file-name nil))
2785     (goto-char (point-min))))
2786
2787 (defun gnus-mime-inline-part (&optional handle)
2788   "Insert the MIME part under point into the current buffer."
2789   (interactive)
2790   (gnus-article-check-buffer)
2791   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2792          contents
2793          (b (point))
2794          buffer-read-only)
2795     (if (mm-handle-undisplayer handle)
2796         (mm-remove-part handle)
2797       (setq contents (mm-get-part handle))
2798       (forward-line 2)
2799       (mm-insert-inline handle contents)
2800       (goto-char b))))
2801
2802 (defun gnus-mime-externalize-part (&optional handle)
2803   "View the MIME part under point with an external viewer."
2804   (interactive)
2805   (gnus-article-check-buffer)
2806   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2807          (mm-user-display-methods nil)
2808          (mm-all-images-fit t)
2809          (mail-parse-charset gnus-newsgroup-charset))
2810     (if (mm-handle-undisplayer handle)
2811         (mm-remove-part handle)
2812       (mm-display-part handle))))
2813
2814 (defun gnus-mime-internalize-part (&optional handle)
2815   "View the MIME part under point with an internal viewer."
2816   (interactive)
2817   (gnus-article-check-buffer)
2818   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
2819          (mm-user-display-methods '((".*" . inline)))
2820          (mm-all-images-fit t)
2821          (mail-parse-charset gnus-newsgroup-charset))
2822     (if (mm-handle-undisplayer handle)
2823         (mm-remove-part handle)
2824       (mm-display-part handle))))
2825
2826 (defun gnus-article-part-wrapper (n function)
2827   (save-current-buffer
2828     (set-buffer gnus-article-buffer)
2829     (when (> n (length gnus-article-mime-handle-alist))
2830       (error "No such part"))
2831     (gnus-article-goto-part n)
2832     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
2833       (funcall function handle))))
2834
2835 (defun gnus-article-pipe-part (n)
2836   "Pipe MIME part N, which is the numerical prefix."
2837   (interactive "p")
2838   (gnus-article-part-wrapper n 'mm-pipe-part))
2839
2840 (defun gnus-article-save-part (n)
2841   "Save MIME part N, which is the numerical prefix."
2842   (interactive "p")
2843   (gnus-article-part-wrapper n 'mm-save-part))
2844
2845 (defun gnus-article-interactively-view-part (n)
2846   "View MIME part N interactively, which is the numerical prefix."
2847   (interactive "p")
2848   (gnus-article-part-wrapper n 'mm-interactively-view-part))
2849
2850 (defun gnus-article-copy-part (n)
2851   "Copy MIME part N, which is the numerical prefix."
2852   (interactive "p")
2853   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
2854
2855 (defun gnus-article-externalize-part (n)
2856   "View MIME part N externally, which is the numerical prefix."
2857   (interactive "p")
2858   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
2859
2860 (defun gnus-article-inline-part (n)
2861   "Inline MIME part N, which is the numerical prefix."
2862   (interactive "p")
2863   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
2864
2865 (defun gnus-article-view-part (n)
2866   "View MIME part N, which is the numerical prefix."
2867   (interactive "p")
2868   (save-current-buffer
2869     (set-buffer gnus-article-buffer)
2870     (when (> n (length gnus-article-mime-handle-alist))
2871       (error "No such part"))
2872     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
2873       (when (gnus-article-goto-part n)
2874         (if (equal (car handle) "multipart/alternative")
2875             (gnus-article-press-button)
2876           (when (eq (gnus-mm-display-part handle) 'internal)
2877             (gnus-set-window-start)))))))
2878
2879 (defun gnus-mm-display-part (handle)
2880   "Display HANDLE and fix MIME button."
2881   (let ((id (get-text-property (point) 'gnus-part))
2882         (point (point))
2883         buffer-read-only)
2884     (forward-line 1)
2885     (prog1
2886         (let ((window (selected-window))
2887               (mail-parse-charset gnus-newsgroup-charset))
2888           (save-excursion
2889             (unwind-protect
2890                 (let ((win (get-buffer-window (current-buffer) t))
2891                       (beg (point)))
2892                   (when win
2893                     (select-window win))
2894                   (goto-char point)
2895                   (forward-line)
2896                   (if (mm-handle-displayed-p handle)
2897                       ;; This will remove the part.
2898                       (mm-display-part handle)
2899                     (save-restriction
2900                       (narrow-to-region (point) (1+ (point)))
2901                       (mm-display-part handle)
2902                       ;; We narrow to the part itself and
2903                       ;; then call the treatment functions.
2904                       (goto-char (point-min))
2905                       (forward-line 1)
2906                       (narrow-to-region (point) (point-max))
2907                       (gnus-treat-article
2908                        nil id
2909                        (1- (length gnus-article-mime-handles))
2910                        (car (mm-handle-type handle))))))
2911               (select-window window))))
2912       (goto-char point)
2913       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
2914       (gnus-insert-mime-button
2915        handle id (list (mm-handle-displayed-p handle)))
2916       (goto-char point))))
2917
2918 (defun gnus-article-goto-part (n)
2919   "Go to MIME part N."
2920   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
2921     (when point
2922       (goto-char point))))
2923
2924 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
2925   (let ((gnus-tmp-name
2926          (or (mail-content-type-get (mm-handle-type handle)
2927                                     'name)
2928              (mail-content-type-get (mm-handle-disposition handle)
2929                                     'filename)
2930              ""))
2931         (gnus-tmp-type (car (mm-handle-type handle)))
2932         (gnus-tmp-description
2933          (mail-decode-encoded-word-string (or (mm-handle-description handle)
2934                                               "")))
2935         (gnus-tmp-dots
2936          (if (if displayed (car displayed)
2937                (mm-handle-displayed-p handle))
2938              "" "..."))
2939         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
2940                            (buffer-size)))
2941         gnus-tmp-type-long b e)
2942     (when (string-match ".*/" gnus-tmp-name)
2943       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
2944     (setq gnus-tmp-type-long (concat gnus-tmp-type
2945                                      (and (not (equal gnus-tmp-name ""))
2946                                           (concat "; " gnus-tmp-name))))
2947     (or (equal gnus-tmp-description "")
2948         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
2949     (unless (bolp)
2950       (insert "\n"))
2951     (setq b (point))
2952     (gnus-eval-format
2953      gnus-mime-button-line-format gnus-mime-button-line-format-alist
2954      `(local-map ,gnus-mime-button-map
2955                  keymap ,gnus-mime-button-map
2956                  gnus-callback gnus-mm-display-part
2957                  gnus-part ,gnus-tmp-id
2958                  article-type annotation
2959                  gnus-data ,handle))
2960     (setq e (point))
2961     (widget-convert-button 'link b e
2962                            :mime-handle handle
2963                            :action 'gnus-widget-press-button
2964                            :button-keymap gnus-mime-button-map
2965                            :help-echo
2966                            (lambda (widget)
2967                              ;; Needed to properly clear the message
2968                              ;; due to a bug in wid-edit
2969                              (setq help-echo-owns-message t)
2970                              (format
2971                               "Click to %s the MIME part; %s for more options"
2972                               (if (mm-handle-displayed-p
2973                                    (widget-get widget :mime-handle))
2974                                   "hide" "show")
2975                               (if gnus-xemacs "button3" "mouse-3"))))))
2976
2977 (defun gnus-widget-press-button (elems el)
2978   (goto-char (widget-get elems :from))
2979   (gnus-article-press-button))
2980
2981 (defvar gnus-displaying-mime nil)
2982
2983 (defun gnus-display-mime (&optional ihandles)
2984   "Display the MIME parts."
2985   (save-excursion
2986     (save-selected-window
2987       (let ((window (get-buffer-window gnus-article-buffer))
2988             (point (point)))
2989         (when window
2990           (select-window window)
2991           ;; We have to do this since selecting the window
2992           ;; may change the point.  So we set the window point.
2993           (set-window-point window point)))
2994       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
2995              buffer-read-only handle name type b e display)
2996         (when (and (not ihandles)
2997                    (not gnus-displaying-mime))
2998           ;; Top-level call; we clean up.
2999           (mm-destroy-parts gnus-article-mime-handles)
3000           (setq gnus-article-mime-handles handles
3001                 gnus-article-mime-handle-alist nil)
3002           ;; We allow users to glean info from the handles.
3003           (when gnus-article-mime-part-function
3004             (gnus-mime-part-function handles)))
3005         (if (and handles
3006                  (or (not (stringp (car handles)))
3007                      (cdr handles)))
3008             (progn
3009               (when (and (not ihandles)
3010                          (not gnus-displaying-mime))
3011                 ;; Clean up for mime parts.
3012                 (article-goto-body)
3013                 (delete-region (point) (point-max)))
3014               (let ((gnus-displaying-mime t))
3015                 (gnus-mime-display-part handles)))
3016           (save-restriction
3017             (article-goto-body)
3018             (narrow-to-region (point) (point-max))
3019             (gnus-treat-article nil 1 1)
3020             (widen)))
3021         ;; Highlight the headers.
3022         (save-excursion
3023           (save-restriction
3024             (article-goto-body)
3025             (narrow-to-region (point-min) (point))
3026             (gnus-treat-article 'head)))))))
3027
3028 (defvar gnus-mime-display-multipart-as-mixed nil)
3029
3030 (defun gnus-mime-display-part (handle)
3031   (cond
3032    ;; Single part.
3033    ((not (stringp (car handle)))
3034     (gnus-mime-display-single handle))
3035    ;; User-defined multipart
3036    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
3037     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
3038              handle))
3039    ;; multipart/alternative
3040    ((and (equal (car handle) "multipart/alternative")
3041          (not gnus-mime-display-multipart-as-mixed))
3042     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3043       (push (cons id handle) gnus-article-mime-handle-alist)
3044       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3045    ;; multipart/related
3046    ((and (equal (car handle) "multipart/related")
3047          (not gnus-mime-display-multipart-as-mixed))
3048     ;;;!!!We should find the start part, but we just default
3049     ;;;!!!to the first part.
3050     (gnus-mime-display-part (cadr handle)))
3051    ;; Other multiparts are handled like multipart/mixed.
3052    (t
3053     (gnus-mime-display-mixed (cdr handle)))))
3054
3055 (defun gnus-mime-part-function (handles)
3056   (if (stringp (car handles))
3057       (mapcar 'gnus-mime-part-function (cdr handles))
3058     (funcall gnus-article-mime-part-function handles)))
3059
3060 (defun gnus-mime-display-mixed (handles)
3061   (mapcar 'gnus-mime-display-part handles))
3062
3063 (defun gnus-mime-display-single (handle)
3064   (let ((type (car (mm-handle-type handle)))
3065         (ignored gnus-ignored-mime-types)
3066         (not-attachment t)
3067         (move nil)
3068         display text)
3069     (catch 'ignored
3070       (progn
3071         (while ignored
3072           (when (string-match (pop ignored) type)
3073             (throw 'ignored nil)))
3074         (if (and (setq not-attachment
3075                        (or (not (mm-handle-disposition handle))
3076                            (equal (car (mm-handle-disposition handle))
3077                                   "inline")
3078                            (mm-attachment-override-p type)))
3079                  (mm-automatic-display-p type)
3080                  (or (mm-inlinable-part-p type)
3081                      (mm-automatic-external-display-p type)))
3082             (setq display t)
3083           (when (equal (car (split-string type "/"))
3084                        "text")
3085             (setq text t)))
3086         (let ((id (1+ (length gnus-article-mime-handle-alist))))
3087           (push (cons id handle) gnus-article-mime-handle-alist)
3088           (when (or (not display)
3089                     (not (gnus-unbuttonized-mime-type-p type)))
3090             (gnus-article-insert-newline)
3091             (gnus-insert-mime-button
3092              handle id (list (or display (and not-attachment text))))
3093             (gnus-article-insert-newline)
3094             (gnus-article-insert-newline)
3095             (setq move t)))
3096         (let ((beg (point)))
3097           (cond
3098            (display
3099             (when move
3100               (forward-line -2))
3101             (let ((mail-parse-charset gnus-newsgroup-charset))
3102               (mm-display-part handle t))
3103             (goto-char (point-max)))
3104            ((and text not-attachment)
3105             (when move
3106               (forward-line -2))
3107             (gnus-article-insert-newline)
3108             (mm-insert-inline handle (mm-get-part handle))
3109             (goto-char (point-max))))
3110           ;; Do highlighting.
3111           (save-excursion
3112             (save-restriction
3113               (narrow-to-region beg (point))
3114               (gnus-treat-article
3115                nil (length gnus-article-mime-handle-alist)
3116                (1- (length gnus-article-mime-handles))
3117                (car (mm-handle-type handle))))))))))
3118
3119 (defun gnus-unbuttonized-mime-type-p (type)
3120   "Say whether TYPE is to be unbuttonized."
3121   (unless gnus-inhibit-mime-unbuttonizing
3122     (catch 'found
3123       (let ((types gnus-unbuttonized-mime-types))
3124         (while types
3125           (when (string-match (pop types) type)
3126             (throw 'found t)))))))
3127
3128 (defun gnus-article-insert-newline ()
3129   "Insert a newline, but mark it as undeletable."
3130   (gnus-put-text-property
3131    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
3132
3133 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
3134   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
3135          (ihandles handles)
3136          (point (point))
3137          handle buffer-read-only from props begend not-pref)
3138     (save-window-excursion
3139       (save-restriction
3140         (when ibegend
3141           (narrow-to-region (car ibegend)
3142                             (or (cdr ibegend)
3143                                 (progn
3144                                   (goto-char (car ibegend))
3145                                   (forward-line 2)
3146                                   (point))))
3147           (delete-region (point-min) (point-max))
3148           (mm-remove-parts handles))
3149         (setq begend (list (point-marker)))
3150         ;; Do the toggle.
3151         (unless (setq not-pref (cadr (member preferred ihandles)))
3152           (setq not-pref (car ihandles)))
3153         (when (or ibegend
3154                   (not (gnus-unbuttonized-mime-type-p
3155                         "multipart/alternative")))
3156           (gnus-add-text-properties
3157            (setq from (point))
3158            (progn
3159              (insert (format "%d.  " id))
3160              (point))
3161            `(gnus-callback
3162              (lambda (handles)
3163                (unless ,(not ibegend)
3164                  (setq gnus-article-mime-handle-alist
3165                        ',gnus-article-mime-handle-alist))
3166                (gnus-mime-display-alternative
3167                 ',ihandles ',not-pref ',begend ,id))
3168              local-map ,gnus-mime-button-map
3169              ,gnus-mouse-face-prop ,gnus-article-mouse-face
3170              face ,gnus-article-button-face
3171              keymap ,gnus-mime-button-map
3172              gnus-part ,id
3173              gnus-data ,handle))
3174           (widget-convert-button 'link from (point)
3175                                  :action 'gnus-widget-press-button
3176                                  :button-keymap gnus-widget-button-keymap)
3177           ;; Do the handles
3178           (while (setq handle (pop handles))
3179             (gnus-add-text-properties
3180              (setq from (point))
3181              (progn
3182                (insert (format "(%c) %-18s"
3183                                (if (equal handle preferred) ?* ? )
3184                                (if (stringp (car handle))
3185                                    (car handle)
3186                                  (car (mm-handle-type handle)))))
3187                (point))
3188              `(gnus-callback
3189                (lambda (handles)
3190                  (unless ,(not ibegend)
3191                    (setq gnus-article-mime-handle-alist
3192                          ',gnus-article-mime-handle-alist))
3193                  (gnus-mime-display-alternative
3194                   ',ihandles ',handle ',begend ,id))
3195                local-map ,gnus-mime-button-map
3196                ,gnus-mouse-face-prop ,gnus-article-mouse-face
3197                face ,gnus-article-button-face
3198                keymap ,gnus-mime-button-map
3199                gnus-part ,id
3200                gnus-data ,handle))
3201             (widget-convert-button 'link from (point)
3202                                    :action 'gnus-widget-press-button
3203                                    :button-keymap gnus-widget-button-keymap)
3204             (insert "  "))
3205           (insert "\n\n"))
3206         (when preferred
3207           (if (stringp (car preferred))
3208               (gnus-display-mime preferred)
3209             (let ((mail-parse-charset gnus-newsgroup-charset))
3210               (mm-display-part preferred)))
3211           (goto-char (point-max))
3212           (setcdr begend (point-marker)))))
3213     (when ibegend
3214       (goto-char point))))
3215
3216 (defun gnus-article-wash-status ()
3217   "Return a string which display status of article washing."
3218   (save-excursion
3219     (set-buffer gnus-article-buffer)
3220     (let ((cite (memq 'cite gnus-article-wash-types))
3221           (headers (memq 'headers gnus-article-wash-types))
3222           (boring (memq 'boring-headers gnus-article-wash-types))
3223           (pgp (memq 'pgp gnus-article-wash-types))
3224           (pem (memq 'pem gnus-article-wash-types))
3225           (signature (memq 'signature gnus-article-wash-types))
3226           (overstrike (memq 'overstrike gnus-article-wash-types))
3227           (emphasis (memq 'emphasis gnus-article-wash-types)))
3228       (format "%c%c%c%c%c%c"
3229               (if cite ?c ? )
3230               (if (or headers boring) ?h ? )
3231               (if (or pgp pem) ?p ? )
3232               (if signature ?s ? )
3233               (if overstrike ?o ? )
3234               (if emphasis ?e ? )))))
3235
3236 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
3237
3238 (defun gnus-article-maybe-hide-headers ()
3239   "Hide unwanted headers if `gnus-have-all-headers' is nil.
3240 Provided for backwards compatibility."
3241   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
3242                  (not (save-excursion (set-buffer gnus-summary-buffer)
3243                                       gnus-have-all-headers)))
3244              (not gnus-inhibit-hiding))
3245     (gnus-article-hide-headers)))
3246
3247 ;;; Article savers.
3248
3249 (defun gnus-output-to-file (file-name)
3250   "Append the current article to a file named FILE-NAME."
3251   (let ((artbuf (current-buffer)))
3252     (with-temp-buffer
3253       (insert-buffer-substring artbuf)
3254       ;; Append newline at end of the buffer as separator, and then
3255       ;; save it to file.
3256       (goto-char (point-max))
3257       (insert "\n")
3258       (append-to-file (point-min) (point-max) file-name)
3259       t)))
3260
3261 (defun gnus-narrow-to-page (&optional arg)
3262   "Narrow the article buffer to a page.
3263 If given a numerical ARG, move forward ARG pages."
3264   (interactive "P")
3265   (setq arg (if arg (prefix-numeric-value arg) 0))
3266   (save-excursion
3267     (set-buffer gnus-article-buffer)
3268     (goto-char (point-min))
3269     (widen)
3270     ;; Remove any old next/prev buttons.
3271     (when (gnus-visual-p 'page-marker)
3272       (let ((buffer-read-only nil))
3273         (gnus-remove-text-with-property 'gnus-prev)
3274         (gnus-remove-text-with-property 'gnus-next)))
3275     (when
3276         (cond ((< arg 0)
3277                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
3278               ((> arg 0)
3279                (re-search-forward page-delimiter nil 'move arg)))
3280       (goto-char (match-end 0)))
3281     (narrow-to-region
3282      (point)
3283      (if (re-search-forward page-delimiter nil 'move)
3284          (match-beginning 0)
3285        (point)))
3286     (when (and (gnus-visual-p 'page-marker)
3287                (not (= (point-min) 1)))
3288       (save-excursion
3289         (goto-char (point-min))
3290         (gnus-insert-prev-page-button)))
3291     (when (and (gnus-visual-p 'page-marker)
3292                (< (+ (point-max) 2) (buffer-size)))
3293       (save-excursion
3294         (goto-char (point-max))
3295         (gnus-insert-next-page-button)))))
3296
3297 ;; Article mode commands
3298
3299 (defun gnus-article-goto-next-page ()
3300   "Show the next page of the article."
3301   (interactive)
3302   (when (gnus-article-next-page)
3303     (goto-char (point-min))
3304     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
3305
3306 (defun gnus-article-goto-prev-page ()
3307   "Show the next page of the article."
3308   (interactive)
3309   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
3310     (gnus-article-prev-page nil)))
3311
3312 (defun gnus-article-next-page (&optional lines)
3313   "Show the next page of the current article.
3314 If end of article, return non-nil.  Otherwise return nil.
3315 Argument LINES specifies lines to be scrolled up."
3316   (interactive "p")
3317   (move-to-window-line -1)
3318   (if (save-excursion
3319         (end-of-line)
3320         (and (pos-visible-in-window-p)  ;Not continuation line.
3321              (eobp)))
3322       ;; Nothing in this page.
3323       (if (or (not gnus-page-broken)
3324               (save-excursion
3325                 (save-restriction
3326                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
3327           t                             ;Nothing more.
3328         (gnus-narrow-to-page 1)         ;Go to next page.
3329         nil)
3330     ;; More in this page.
3331     (let ((scroll-in-place nil))
3332       (condition-case ()
3333           (scroll-up lines)
3334         (end-of-buffer
3335          ;; Long lines may cause an end-of-buffer error.
3336          (goto-char (point-max)))))
3337     (move-to-window-line 0)
3338     nil))
3339
3340 (defun gnus-article-prev-page (&optional lines)
3341   "Show previous page of current article.
3342 Argument LINES specifies lines to be scrolled down."
3343   (interactive "p")
3344   (move-to-window-line 0)
3345   (if (and gnus-page-broken
3346            (bobp)
3347            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
3348       (progn
3349         (gnus-narrow-to-page -1)        ;Go to previous page.
3350         (goto-char (point-max))
3351         (recenter -1))
3352     (let ((scroll-in-place nil))
3353       (prog1
3354           (condition-case ()
3355               (scroll-down lines)
3356             (beginning-of-buffer
3357              (goto-char (point-min))))
3358         (move-to-window-line 0)))))
3359
3360 (defun gnus-article-refer-article ()
3361   "Read article specified by message-id around point."
3362   (interactive)
3363   (let ((point (point)))
3364     (search-forward ">" nil t)          ;Move point to end of "<....>".
3365     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
3366         (let ((message-id (match-string 1)))
3367           (goto-char point)
3368           (set-buffer gnus-summary-buffer)
3369           (gnus-summary-refer-article message-id))
3370       (goto-char (point))
3371       (error "No references around point"))))
3372
3373 (defun gnus-article-show-summary ()
3374   "Reconfigure windows to show summary buffer."
3375   (interactive)
3376   (if (not (gnus-buffer-live-p gnus-summary-buffer))
3377       (error "There is no summary buffer for this article buffer")
3378     (gnus-article-set-globals)
3379     (gnus-configure-windows 'article)
3380     (gnus-summary-goto-subject gnus-current-article)
3381     (gnus-summary-position-point)))
3382
3383 (defun gnus-article-describe-briefly ()
3384   "Describe article mode commands briefly."
3385   (interactive)
3386   (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")))
3387
3388 (defun gnus-article-summary-command ()
3389   "Execute the last keystroke in the summary buffer."
3390   (interactive)
3391   (let ((obuf (current-buffer))
3392         (owin (current-window-configuration))
3393         func)
3394     (switch-to-buffer gnus-article-current-summary 'norecord)
3395     (setq func (lookup-key (current-local-map) (this-command-keys)))
3396     (call-interactively func)
3397     (set-buffer obuf)
3398     (set-window-configuration owin)
3399     (set-window-point (get-buffer-window (current-buffer)) (point))))
3400
3401 (defun gnus-article-summary-command-nosave ()
3402   "Execute the last keystroke in the summary buffer."
3403   (interactive)
3404   (let (func)
3405     (pop-to-buffer gnus-article-current-summary 'norecord)
3406     (setq func (lookup-key (current-local-map) (this-command-keys)))
3407     (call-interactively func)))
3408
3409 (defun gnus-article-check-buffer ()
3410   "Beep if not in an article buffer."
3411   (unless (equal major-mode 'gnus-article-mode)
3412     (error "Command invoked outside of a Gnus article buffer")))
3413
3414 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
3415   "Read a summary buffer key sequence and execute it from the article buffer."
3416   (interactive "P")
3417   (gnus-article-check-buffer)
3418   (let ((nosaves
3419          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
3420            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
3421            "=" "^" "\M-^" "|"))
3422         (nosave-but-article
3423          '("A\r"))
3424         (nosave-in-article
3425          '("\C-d"))
3426         (up-to-top
3427          '("n" "Gn" "p" "Gp"))
3428         keys new-sum-point)
3429     (save-excursion
3430       (set-buffer gnus-article-current-summary)
3431       (let (gnus-pick-mode)
3432         (push (or key last-command-event) unread-command-events)
3433         (setq keys (read-key-sequence nil))))
3434     (message "")
3435
3436     (if (or (member keys nosaves)
3437             (member keys nosave-but-article)
3438             (member keys nosave-in-article))
3439         (let (func)
3440           (save-window-excursion
3441             (pop-to-buffer gnus-article-current-summary 'norecord)
3442             ;; We disable the pick minor mode commands.
3443             (let (gnus-pick-mode)
3444               (setq func (lookup-key (current-local-map) keys))))
3445           (if (not func)
3446               (ding)
3447             (unless (member keys nosave-in-article)
3448               (set-buffer gnus-article-current-summary))
3449             (call-interactively func)
3450             (setq new-sum-point (point)))
3451           (when (member keys nosave-but-article)
3452             (pop-to-buffer gnus-article-buffer 'norecord)))
3453       ;; These commands should restore window configuration.
3454       (let ((obuf (current-buffer))
3455             (owin (current-window-configuration))
3456             (opoint (point))
3457             (summary gnus-article-current-summary)
3458             func in-buffer selected)
3459         (if not-restore-window
3460             (pop-to-buffer summary 'norecord)
3461           (switch-to-buffer summary 'norecord))
3462         (setq in-buffer (current-buffer))
3463         ;; We disable the pick minor mode commands.
3464         (if (setq func (let (gnus-pick-mode)
3465                          (lookup-key (current-local-map) keys)))
3466             (progn
3467               (call-interactively func)
3468               (setq new-sum-point (point)))
3469           (ding))
3470         (when (eq in-buffer (current-buffer))
3471           (setq selected (gnus-summary-select-article))
3472           (set-buffer obuf)
3473           (unless not-restore-window
3474             (set-window-configuration owin))
3475           (when (eq selected 'old)
3476             (article-goto-body)
3477             (set-window-start (get-buffer-window (current-buffer))
3478                               1)
3479             (set-window-point (get-buffer-window (current-buffer))
3480                               (point)))
3481           (let ((win (get-buffer-window gnus-article-current-summary)))
3482             (when win
3483               (set-window-point win new-sum-point))))))))
3484
3485 (defun gnus-article-hide (&optional arg force)
3486   "Hide all the gruft in the current article.
3487 This means that PGP stuff, signatures, cited text and (some)
3488 headers will be hidden.
3489 If given a prefix, show the hidden text instead."
3490   (interactive (append (gnus-article-hidden-arg) (list 'force)))
3491   (gnus-article-hide-headers arg)
3492   (gnus-article-hide-pgp arg)
3493   (gnus-article-hide-citation-maybe arg force)
3494   (gnus-article-hide-signature arg))
3495
3496 (defun gnus-article-maybe-highlight ()
3497   "Do some article highlighting if article highlighting is requested."
3498   (when (gnus-visual-p 'article-highlight 'highlight)
3499     (gnus-article-highlight-some)))
3500
3501 (defun gnus-check-group-server ()
3502   ;; Make sure the connection to the server is alive.
3503   (unless (gnus-server-opened
3504            (gnus-find-method-for-group gnus-newsgroup-name))
3505     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
3506     (gnus-request-group gnus-newsgroup-name t)))
3507
3508 (defun gnus-request-article-this-buffer (article group)
3509   "Get an article and insert it into this buffer."
3510   (let (do-update-line sparse-header)
3511     (prog1
3512         (save-excursion
3513           (erase-buffer)
3514           (gnus-kill-all-overlays)
3515           (setq group (or group gnus-newsgroup-name))
3516
3517           ;; Using `gnus-request-article' directly will insert the article into
3518           ;; `nntp-server-buffer' - so we'll save some time by not having to
3519           ;; copy it from the server buffer into the article buffer.
3520
3521           ;; We only request an article by message-id when we do not have the
3522           ;; headers for it, so we'll have to get those.
3523           (when (stringp article)
3524             (let ((gnus-override-method gnus-refer-article-method))
3525               (gnus-read-header article)))
3526
3527           ;; If the article number is negative, that means that this article
3528           ;; doesn't belong in this newsgroup (possibly), so we find its
3529           ;; message-id and request it by id instead of number.
3530           (when (and (numberp article)
3531                      gnus-summary-buffer
3532                      (get-buffer gnus-summary-buffer)
3533                      (gnus-buffer-exists-p gnus-summary-buffer))
3534             (save-excursion
3535               (set-buffer gnus-summary-buffer)
3536               (let ((header (gnus-summary-article-header article)))
3537                 (when (< article 0)
3538                   (cond
3539                    ((memq article gnus-newsgroup-sparse)
3540                     ;; This is a sparse gap article.
3541                     (setq do-update-line article)
3542                     (setq article (mail-header-id header))
3543                     (let ((gnus-override-method gnus-refer-article-method))
3544                       (setq sparse-header (gnus-read-header article)))
3545                     (setq gnus-newsgroup-sparse
3546                           (delq article gnus-newsgroup-sparse)))
3547                    ((vectorp header)
3548                     ;; It's a real article.
3549                     (setq article (mail-header-id header)))
3550                    (t
3551                     ;; It is an extracted pseudo-article.
3552                     (setq article 'pseudo)
3553                     (gnus-request-pseudo-article header))))
3554
3555                 (let ((method (gnus-find-method-for-group
3556                                gnus-newsgroup-name)))
3557                   (when (and (eq (car method) 'nneething)
3558                              (vectorp header))
3559                     (let ((dir (concat
3560                                 (file-name-as-directory
3561                                  (or (cadr (assq 'nneething-address method))
3562                                      (nth 1 method)))
3563                                 (mail-header-subject header))))
3564                       (when (file-directory-p dir)
3565                         (setq article 'nneething)
3566                         (gnus-group-enter-directory dir))))))))
3567
3568           (cond
3569            ;; Refuse to select canceled articles.
3570            ((and (numberp article)
3571                  gnus-summary-buffer
3572                  (get-buffer gnus-summary-buffer)
3573                  (gnus-buffer-exists-p gnus-summary-buffer)
3574                  (eq (cdr (save-excursion
3575                             (set-buffer gnus-summary-buffer)
3576                             (assq article gnus-newsgroup-reads)))
3577                      gnus-canceled-mark))
3578             nil)
3579            ;; We first check `gnus-original-article-buffer'.
3580            ((and (get-buffer gnus-original-article-buffer)
3581                  (numberp article)
3582                  (save-excursion
3583                    (set-buffer gnus-original-article-buffer)
3584                    (and (equal (car gnus-original-article) group)
3585                         (eq (cdr gnus-original-article) article))))
3586             (insert-buffer-substring gnus-original-article-buffer)
3587             'article)
3588            ;; Check the backlog.
3589            ((and gnus-keep-backlog
3590                  (gnus-backlog-request-article group article (current-buffer)))
3591             'article)
3592            ;; Check asynchronous pre-fetch.
3593            ((gnus-async-request-fetched-article group article (current-buffer))
3594             (gnus-async-prefetch-next group article gnus-summary-buffer)
3595             (when (and (numberp article) gnus-keep-backlog)
3596               (gnus-backlog-enter-article group article (current-buffer)))
3597             'article)
3598            ;; Check the cache.
3599            ((and gnus-use-cache
3600                  (numberp article)
3601                  (gnus-cache-request-article article group))
3602             'article)
3603            ;; Get the article and put into the article buffer.
3604            ((or (stringp article) (numberp article))
3605             (let ((gnus-override-method
3606                    (and (stringp article) gnus-refer-article-method))
3607                   (buffer-read-only nil))
3608               (erase-buffer)
3609               (gnus-kill-all-overlays)
3610               (gnus-check-group-server)
3611               (when (gnus-request-article article group (current-buffer))
3612                 (when (numberp article)
3613                   (gnus-async-prefetch-next group article gnus-summary-buffer)
3614                   (when gnus-keep-backlog
3615                     (gnus-backlog-enter-article
3616                      group article (current-buffer))))
3617                 'article)))
3618            ;; It was a pseudo.
3619            (t article)))
3620
3621       ;; Associate this article with the current summary buffer.
3622       (setq gnus-article-current-summary gnus-summary-buffer)
3623
3624       ;; Take the article from the original article buffer
3625       ;; and place it in the buffer it's supposed to be in.
3626       (when (and (get-buffer gnus-article-buffer)
3627                  (equal (buffer-name (current-buffer))
3628                         (buffer-name (get-buffer gnus-article-buffer))))
3629         (save-excursion
3630           (if (get-buffer gnus-original-article-buffer)
3631               (set-buffer gnus-original-article-buffer)
3632             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3633             (buffer-disable-undo)
3634             (setq major-mode 'gnus-original-article-mode)
3635             (setq buffer-read-only t))
3636           (let (buffer-read-only)
3637             (erase-buffer)
3638             (insert-buffer-substring gnus-article-buffer))
3639           (setq gnus-original-article (cons group article)))
3640
3641         ;; Decode charsets.
3642         (run-hooks 'gnus-article-decode-hook)
3643         ;; Mark article as decoded or not.
3644         (setq gnus-article-decoded-p gnus-article-decode-hook))
3645
3646       ;; Update sparse articles.
3647       (when (and do-update-line
3648                  (or (numberp article)
3649                      (stringp article)))
3650         (let ((buf (current-buffer)))
3651           (set-buffer gnus-summary-buffer)
3652           (gnus-summary-update-article do-update-line sparse-header)
3653           (gnus-summary-goto-subject do-update-line nil t)
3654           (set-window-point (get-buffer-window (current-buffer) t)
3655                             (point))
3656           (set-buffer buf))))))
3657
3658 ;;;
3659 ;;; Article editing
3660 ;;;
3661
3662 (defcustom gnus-article-edit-mode-hook nil
3663   "Hook run in article edit mode buffers."
3664   :group 'gnus-article-various
3665   :type 'hook)
3666
3667 (defvar gnus-article-edit-done-function nil)
3668
3669 (defvar gnus-article-edit-mode-map nil)
3670
3671 ;; Should we be using derived.el for this?
3672 (unless gnus-article-edit-mode-map
3673   (setq gnus-article-edit-mode-map (make-sparse-keymap))
3674   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
3675
3676   (gnus-define-keys gnus-article-edit-mode-map
3677     "\C-c\C-c" gnus-article-edit-done
3678     "\C-c\C-k" gnus-article-edit-exit)
3679
3680   (gnus-define-keys (gnus-article-edit-wash-map
3681                      "\C-c\C-w" gnus-article-edit-mode-map)
3682     "f" gnus-article-edit-full-stops))
3683
3684 (defun gnus-article-edit-mode ()
3685   "Major mode for editing articles.
3686 This is an extended text-mode.
3687
3688 \\{gnus-article-edit-mode-map}"
3689   (interactive)
3690   (setq major-mode 'gnus-article-edit-mode)
3691   (setq mode-name "Article Edit")
3692   (use-local-map gnus-article-edit-mode-map)
3693   (make-local-variable 'gnus-article-edit-done-function)
3694   (make-local-variable 'gnus-prev-winconf)
3695   (setq buffer-read-only nil)
3696   (buffer-enable-undo)
3697   (widen)
3698   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
3699
3700 (defun gnus-article-edit (&optional force)
3701   "Edit the current article.
3702 This will have permanent effect only in mail groups.
3703 If FORCE is non-nil, allow editing of articles even in read-only
3704 groups."
3705   (interactive "P")
3706   (when (and (not force)
3707              (gnus-group-read-only-p))
3708     (error "The current newsgroup does not support article editing"))
3709   (gnus-article-date-original)
3710   (gnus-article-edit-article
3711    'ignore
3712    `(lambda (no-highlight)
3713       'ignore
3714       (gnus-summary-edit-article-done
3715        ,(or (mail-header-references gnus-current-headers) "")
3716        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
3717
3718 (defun gnus-article-edit-article (start-func exit-func)
3719   "Start editing the contents of the current article buffer."
3720   (let ((winconf (current-window-configuration)))
3721     (set-buffer gnus-article-buffer)
3722     (gnus-article-edit-mode)
3723     (funcall start-func)
3724     ;;(gnus-article-delete-text-of-type 'annotation)
3725     ;;(gnus-set-text-properties (point-min) (point-max) nil)
3726     (gnus-configure-windows 'edit-article)
3727     (setq gnus-article-edit-done-function exit-func)
3728     (setq gnus-prev-winconf winconf)
3729     (gnus-message 6 "C-c C-c to end edits")))
3730
3731 (defun gnus-article-edit-done (&optional arg)
3732   "Update the article edits and exit."
3733   (interactive "P")
3734   (save-excursion
3735     (save-restriction
3736       (widen)
3737       (when (article-goto-body)
3738         (let ((lines (count-lines (point) (point-max)))
3739               (length (- (point-max) (point)))
3740               (case-fold-search t)
3741               (body (copy-marker (point))))
3742           (goto-char (point-min))
3743           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
3744             (delete-region (match-beginning 1) (match-end 1))
3745             (insert (number-to-string length)))
3746           (goto-char (point-min))
3747           (when (re-search-forward
3748                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
3749             (delete-region (match-beginning 1) (match-end 1))
3750             (insert (number-to-string length)))
3751           (goto-char (point-min))
3752           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
3753             (delete-region (match-beginning 1) (match-end 1))
3754             (insert (number-to-string lines)))))))
3755   (let ((func gnus-article-edit-done-function)
3756         (buf (current-buffer))
3757         (start (window-start)))
3758     (gnus-article-edit-exit)
3759     (save-excursion
3760       (set-buffer buf)
3761       (let ((buffer-read-only nil))
3762         (funcall func arg))
3763       ;; The cache and backlog have to be flushed somewhat.
3764       (when gnus-keep-backlog
3765         (gnus-backlog-remove-article
3766          (car gnus-article-current) (cdr gnus-article-current)))
3767       ;; Flush original article as well.
3768       (save-excursion
3769         (when (get-buffer gnus-original-article-buffer)
3770           (set-buffer gnus-original-article-buffer)
3771           (setq gnus-original-article nil)))
3772       (when gnus-use-cache
3773         (gnus-cache-update-article
3774          (car gnus-article-current) (cdr gnus-article-current))))
3775     (set-buffer buf)
3776     (set-window-start (get-buffer-window buf) start)
3777     (set-window-point (get-buffer-window buf) (point))))
3778
3779 (defun gnus-article-edit-exit ()
3780   "Exit the article editing without updating."
3781   (interactive)
3782   ;; We remove all text props from the article buffer.
3783   (let ((buf (format "%s" (buffer-string)))
3784         (curbuf (current-buffer))
3785         (p (point))
3786         (window-start (window-start)))
3787     (erase-buffer)
3788     (insert buf)
3789     (let ((winconf gnus-prev-winconf))
3790       (gnus-article-mode)
3791       (set-window-configuration winconf)
3792       ;; Tippy-toe some to make sure that point remains where it was.
3793       (save-current-buffer
3794         (set-buffer curbuf)
3795         (set-window-start (get-buffer-window (current-buffer)) window-start)
3796         (goto-char p)))))
3797
3798 (defun gnus-article-edit-full-stops ()
3799   "Interactively repair spacing at end of sentences."
3800   (interactive)
3801   (save-excursion
3802     (goto-char (point-min))
3803     (search-forward-regexp "^$" nil t)
3804     (let ((case-fold-search nil))
3805       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
3806
3807 ;;;
3808 ;;; Article highlights
3809 ;;;
3810
3811 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
3812
3813 ;;; Internal Variables:
3814
3815 (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\\)"
3816   "Regular expression that matches URLs."
3817   :group 'gnus-article-buttons
3818   :type 'regexp)
3819
3820 (defcustom gnus-button-alist
3821   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
3822      0 t gnus-button-message-id 2)
3823     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
3824     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
3825      1 t
3826      gnus-button-fetch-group 4)
3827     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
3828     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
3829      t gnus-button-message-id 3)
3830     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
3831     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
3832     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
3833     ;; This is how URLs _should_ be embedded in text...
3834     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
3835     ;; Raw URLs.
3836     (,gnus-button-url-regexp 0 t gnus-button-url 0))
3837   "*Alist of regexps matching buttons in article bodies.
3838
3839 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
3840 REGEXP: is the string matching text around the button,
3841 BUTTON: is the number of the regexp grouping actually matching the button,
3842 FORM: is a lisp expression which must eval to true for the button to
3843 be added,
3844 CALLBACK: is the function to call when the user push this button, and each
3845 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
3846
3847 CALLBACK can also be a variable, in that case the value of that
3848 variable it the real callback function."
3849   :group 'gnus-article-buttons
3850   :type '(repeat (list regexp
3851                        (integer :tag "Button")
3852                        (sexp :tag "Form")
3853                        (function :tag "Callback")
3854                        (repeat :tag "Par"
3855                                :inline t
3856                                (integer :tag "Regexp group")))))
3857
3858 (defcustom gnus-header-button-alist
3859   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
3860      0 t gnus-button-message-id 0)
3861     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
3862     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
3863      0 t gnus-button-mailto 0)
3864     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3865     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3866     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
3867     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
3868      gnus-button-message-id 3))
3869   "*Alist of headers and regexps to match buttons in article heads.
3870
3871 This alist is very similar to `gnus-button-alist', except that each
3872 alist has an additional HEADER element first in each entry:
3873
3874 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
3875
3876 HEADER is a regexp to match a header.  For a fuller explanation, see
3877 `gnus-button-alist'."
3878   :group 'gnus-article-buttons
3879   :group 'gnus-article-headers
3880   :type '(repeat (list (regexp :tag "Header")
3881                        regexp
3882                        (integer :tag "Button")
3883                        (sexp :tag "Form")
3884                        (function :tag "Callback")
3885                        (repeat :tag "Par"
3886                                :inline t
3887                                (integer :tag "Regexp group")))))
3888
3889 (defvar gnus-button-regexp nil)
3890 (defvar gnus-button-marker-list nil)
3891 ;; Regexp matching any of the regexps from `gnus-button-alist'.
3892
3893 (defvar gnus-button-last nil)
3894 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
3895
3896 ;;; Commands:
3897
3898 (defun gnus-article-push-button (event)
3899   "Check text under the mouse pointer for a callback function.
3900 If the text under the mouse pointer has a `gnus-callback' property,
3901 call it with the value of the `gnus-data' text property."
3902   (interactive "e")
3903   (set-buffer (window-buffer (posn-window (event-start event))))
3904   (let* ((pos (posn-point (event-start event)))
3905          (data (get-text-property pos 'gnus-data))
3906          (fun (get-text-property pos 'gnus-callback)))
3907     (goto-char pos)
3908     (when fun
3909       (funcall fun data))))
3910
3911 (defun gnus-article-press-button ()
3912   "Check text at point for a callback function.
3913 If the text at point has a `gnus-callback' property,
3914 call it with the value of the `gnus-data' text property."
3915   (interactive)
3916   (let* ((data (get-text-property (point) 'gnus-data))
3917          (fun (get-text-property (point) 'gnus-callback)))
3918     (when fun
3919       (funcall fun data))))
3920
3921 (defun gnus-article-highlight (&optional force)
3922   "Highlight current article.
3923 This function calls `gnus-article-highlight-headers',
3924 `gnus-article-highlight-citation',
3925 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
3926 do the highlighting.  See the documentation for those functions."
3927   (interactive (list 'force))
3928   (gnus-article-highlight-headers)
3929   (gnus-article-highlight-citation force)
3930   (gnus-article-highlight-signature)
3931   (gnus-article-add-buttons force)
3932   (gnus-article-add-buttons-to-head))
3933
3934 (defun gnus-article-highlight-some (&optional force)
3935   "Highlight current article.
3936 This function calls `gnus-article-highlight-headers',
3937 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
3938 do the highlighting.  See the documentation for those functions."
3939   (interactive (list 'force))
3940   (gnus-article-highlight-headers)
3941   (gnus-article-highlight-signature)
3942   (gnus-article-add-buttons))
3943
3944 (defun gnus-article-highlight-headers ()
3945   "Highlight article headers as specified by `gnus-header-face-alist'."
3946   (interactive)
3947   (save-excursion
3948     (set-buffer gnus-article-buffer)
3949     (save-restriction
3950       (let ((alist gnus-header-face-alist)
3951             (buffer-read-only nil)
3952             (case-fold-search t)
3953             (inhibit-point-motion-hooks t)
3954             entry regexp header-face field-face from hpoints fpoints)
3955         (article-narrow-to-head)
3956         (while (setq entry (pop alist))
3957           (goto-char (point-min))
3958           (setq regexp (concat "^\\("
3959                                (if (string-equal "" (nth 0 entry))
3960                                    "[^\t ]"
3961                                  (nth 0 entry))
3962                                "\\)")
3963                 header-face (nth 1 entry)
3964                 field-face (nth 2 entry))
3965           (while (and (re-search-forward regexp nil t)
3966                       (not (eobp)))
3967             (beginning-of-line)
3968             (setq from (point))
3969             (unless (search-forward ":" nil t)
3970               (forward-char 1))
3971             (when (and header-face
3972                        (not (memq (point) hpoints)))
3973               (push (point) hpoints)
3974               (gnus-put-text-property from (point) 'face header-face))
3975             (when (and field-face
3976                        (not (memq (setq from (point)) fpoints)))
3977               (push from fpoints)
3978               (if (re-search-forward "^[^ \t]" nil t)
3979                   (forward-char -2)
3980                 (goto-char (point-max)))
3981               (gnus-put-text-property from (point) 'face field-face))))))))
3982
3983 (defun gnus-article-highlight-signature ()
3984   "Highlight the signature in an article.
3985 It does this by highlighting everything after
3986 `gnus-signature-separator' using `gnus-signature-face'."
3987   (interactive)
3988   (save-excursion
3989     (set-buffer gnus-article-buffer)
3990     (let ((buffer-read-only nil)
3991           (inhibit-point-motion-hooks t))
3992       (save-restriction
3993         (when (and gnus-signature-face
3994                    (gnus-article-narrow-to-signature))
3995           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
3996                             'face gnus-signature-face)
3997           (widen)
3998           (gnus-article-search-signature)
3999           (let ((start (match-beginning 0))
4000                 (end (set-marker (make-marker) (1+ (match-end 0)))))
4001             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
4002                                      end)))))))
4003
4004 (defun gnus-button-in-region-p (b e prop)
4005   "Say whether PROP exists in the region."
4006   (text-property-not-all b e prop nil))
4007
4008 (defun gnus-article-add-buttons (&optional force)
4009   "Find external references in the article and make buttons of them.
4010 \"External references\" are things like Message-IDs and URLs, as
4011 specified by `gnus-button-alist'."
4012   (interactive (list 'force))
4013   (save-excursion
4014     (set-buffer gnus-article-buffer)
4015     (let ((buffer-read-only nil)
4016           (inhibit-point-motion-hooks t)
4017           (case-fold-search t)
4018           (alist gnus-button-alist)
4019           beg entry regexp)
4020       ;; Remove all old markers.
4021       (let (marker entry)
4022         (while (setq marker (pop gnus-button-marker-list))
4023           (goto-char marker)
4024           (when (setq entry (gnus-button-entry))
4025             (put-text-property (match-beginning (nth 1 entry))
4026                                (match-end (nth 1 entry))
4027                                'gnus-callback nil))
4028           (set-marker marker nil)))
4029       ;; We skip the headers.
4030       (article-goto-body)
4031       (setq beg (point))
4032       (while (setq entry (pop alist))
4033         (setq regexp (car entry))
4034         (goto-char beg)
4035         (while (re-search-forward regexp nil t)
4036           (let* ((start (and entry (match-beginning (nth 1 entry))))
4037                  (end (and entry (match-end (nth 1 entry))))
4038                  (from (match-beginning 0)))
4039             (when (and (or (eq t (nth 2 entry))
4040                            (eval (nth 2 entry)))
4041                        (not (gnus-button-in-region-p
4042                              start end 'gnus-callback)))
4043               ;; That optional form returned non-nil, so we add the
4044               ;; button.
4045               (gnus-article-add-button
4046                start end 'gnus-button-push
4047                (car (push (set-marker (make-marker) from)
4048                           gnus-button-marker-list))))))))))
4049
4050 ;; Add buttons to the head of an article.
4051 (defun gnus-article-add-buttons-to-head ()
4052   "Add buttons to the head of the article."
4053   (interactive)
4054   (save-excursion
4055     (set-buffer gnus-article-buffer)
4056     (save-restriction
4057       (let ((buffer-read-only nil)
4058             (inhibit-point-motion-hooks t)
4059             (case-fold-search t)
4060             (alist gnus-header-button-alist)
4061             entry beg end)
4062         (article-narrow-to-head)
4063         (while alist
4064           ;; Each alist entry.
4065           (setq entry (car alist)
4066                 alist (cdr alist))
4067           (goto-char (point-min))
4068           (while (re-search-forward (car entry) nil t)
4069             ;; Each header matching the entry.
4070             (setq beg (match-beginning 0))
4071             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
4072                                (match-beginning 0))
4073                           (point-max)))
4074             (goto-char beg)
4075             (while (re-search-forward (nth 1 entry) end t)
4076               ;; Each match within a header.
4077               (let* ((entry (cdr entry))
4078                      (start (match-beginning (nth 1 entry)))
4079                      (end (match-end (nth 1 entry)))
4080                      (form (nth 2 entry)))
4081                 (goto-char (match-end 0))
4082                 (when (eval form)
4083                   (gnus-article-add-button
4084                    start end (nth 3 entry)
4085                    (buffer-substring (match-beginning (nth 4 entry))
4086                                      (match-end (nth 4 entry)))))))
4087             (goto-char end)))))))
4088
4089 ;;; External functions:
4090
4091 (defun gnus-article-add-button (from to fun &optional data)
4092   "Create a button between FROM and TO with callback FUN and data DATA."
4093   (when gnus-article-button-face
4094     (gnus-overlay-put (gnus-make-overlay from to)
4095                       'face gnus-article-button-face))
4096   (gnus-add-text-properties
4097    from to
4098    (nconc (and gnus-article-mouse-face
4099                (list gnus-mouse-face-prop gnus-article-mouse-face))
4100           (list 'gnus-callback fun)
4101           (and data (list 'gnus-data data))))
4102   (widget-convert-button 'link from to :action 'gnus-widget-press-button
4103                          :button-keymap gnus-widget-button-keymap))
4104
4105 ;;; Internal functions:
4106
4107 (defun gnus-article-set-globals ()
4108   (save-excursion
4109     (set-buffer gnus-summary-buffer)
4110     (gnus-set-global-variables)))
4111
4112 (defun gnus-signature-toggle (end)
4113   (save-excursion
4114     (set-buffer gnus-article-buffer)
4115     (let ((buffer-read-only nil)
4116           (inhibit-point-motion-hooks t))
4117       (if (get-text-property end 'invisible)
4118           (gnus-article-unhide-text end (point-max))
4119         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
4120
4121 (defun gnus-button-entry ()
4122   ;; Return the first entry in `gnus-button-alist' matching this place.
4123   (let ((alist gnus-button-alist)
4124         (entry nil))
4125     (while alist
4126       (setq entry (pop alist))
4127       (if (looking-at (car entry))
4128           (setq alist nil)
4129         (setq entry nil)))
4130     entry))
4131
4132 (defun gnus-button-push (marker)
4133   ;; Push button starting at MARKER.
4134   (save-excursion
4135     (goto-char marker)
4136     (let* ((entry (gnus-button-entry))
4137            (inhibit-point-motion-hooks t)
4138            (fun (nth 3 entry))
4139            (args (mapcar (lambda (group)
4140                            (let ((string (match-string group)))
4141                              (gnus-set-text-properties
4142                               0 (length string) nil string)
4143                              string))
4144                          (nthcdr 4 entry))))
4145       (cond
4146        ((fboundp fun)
4147         (apply fun args))
4148        ((and (boundp fun)
4149              (fboundp (symbol-value fun)))
4150         (apply (symbol-value fun) args))
4151        (t
4152         (gnus-message 1 "You must define `%S' to use this button"
4153                       (cons fun args)))))))
4154
4155 (defun gnus-button-message-id (message-id)
4156   "Fetch MESSAGE-ID."
4157   (save-excursion
4158     (set-buffer gnus-summary-buffer)
4159     (gnus-summary-refer-article message-id)))
4160
4161 (defun gnus-button-fetch-group (address)
4162   "Fetch GROUP specified by ADDRESS."
4163   (if (not (string-match "[:/]" address))
4164       ;; This is just a simple group url.
4165       (gnus-group-read-ephemeral-group address gnus-select-method)
4166     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
4167                            address))
4168         (error "Can't parse %s" address)
4169       (gnus-group-read-ephemeral-group
4170        (match-string 4 address)
4171        `(nntp ,(match-string 1 address)
4172               (nntp-address ,(match-string 1 address))
4173               (nntp-port-number ,(if (match-end 3)
4174                                      (match-string 3 address)
4175                                    "nntp")))))))
4176
4177 (defun gnus-url-parse-query-string (query &optional downcase)
4178   (let (retval pairs cur key val)
4179     (setq pairs (split-string query "&"))
4180     (while pairs
4181       (setq cur (car pairs)
4182             pairs (cdr pairs))
4183       (if (not (string-match "=" cur))
4184           nil                           ; Grace
4185         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
4186               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
4187         (if downcase
4188             (setq key (downcase key)))
4189         (setq cur (assoc key retval))
4190         (if cur
4191             (setcdr cur (cons val (cdr cur)))
4192           (setq retval (cons (list key val) retval)))))
4193     retval))
4194
4195 (defun gnus-url-unhex (x)
4196   (if (> x ?9)
4197       (if (>= x ?a)
4198           (+ 10 (- x ?a))
4199         (+ 10 (- x ?A)))
4200     (- x ?0)))
4201
4202 (defun gnus-url-unhex-string (str &optional allow-newlines)
4203   "Remove %XXX embedded spaces, etc in a url.
4204 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
4205 decoding of carriage returns and line feeds in the string, which is normally
4206 forbidden in URL encoding."
4207   (setq str (or str ""))
4208   (let ((tmp "")
4209         (case-fold-search t))
4210     (while (string-match "%[0-9a-f][0-9a-f]" str)
4211       (let* ((start (match-beginning 0))
4212              (ch1 (gnus-url-unhex (elt str (+ start 1))))
4213              (code (+ (* 16 ch1)
4214                       (gnus-url-unhex (elt str (+ start 2))))))
4215         (setq tmp (concat
4216                    tmp (substring str 0 start)
4217                    (cond
4218                     (allow-newlines
4219                      (char-to-string code))
4220                     ((or (= code ?\n) (= code ?\r))
4221                      " ")
4222                     (t (char-to-string code))))
4223               str (substring str (match-end 0)))))
4224     (setq tmp (concat tmp str))
4225     tmp))
4226
4227 (defun gnus-url-mailto (url)
4228   ;; Send mail to someone
4229   (when (string-match "mailto:/*\\(.*\\)" url)
4230     (setq url (substring url (match-beginning 1) nil)))
4231   (let (to args subject func)
4232     (if (string-match (regexp-quote "?") url)
4233         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
4234               args (gnus-url-parse-query-string
4235                     (substring url (match-end 0) nil) t))
4236       (setq to (gnus-url-unhex-string url)))
4237     (setq args (cons (list "to" to) args)
4238           subject (cdr-safe (assoc "subject" args)))
4239     (message-mail)
4240     (while args
4241       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
4242       (if (fboundp func)
4243           (funcall func)
4244         (message-position-on-field (caar args)))
4245       (insert (mapconcat 'identity (cdar args) ", "))
4246       (setq args (cdr args)))
4247     (if subject
4248         (message-goto-body)
4249       (message-goto-subject))))
4250
4251 (defun gnus-button-mailto (address)
4252   ;; Mail to ADDRESS.
4253   (set-buffer (gnus-copy-article-buffer))
4254   (message-reply address))
4255
4256 (defun gnus-button-reply (address)
4257   ;; Reply to ADDRESS.
4258   (message-reply address))
4259
4260 (defun gnus-button-url (address)
4261   "Browse ADDRESS."
4262   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
4263   (if (listp browse-url-browser-function)
4264       (browse-url address)
4265     (funcall browse-url-browser-function address)))
4266
4267 (defun gnus-button-embedded-url (address)
4268   "Browse ADDRESS."
4269   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
4270   (if (listp browse-url-browser-function)
4271       (browse-url (gnus-strip-whitespace address))
4272     (funcall browse-url-browser-function (gnus-strip-whitespace address))))
4273
4274 ;;; Next/prev buttons in the article buffer.
4275
4276 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
4277 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
4278
4279 (defvar gnus-prev-page-map nil)
4280 (unless gnus-prev-page-map
4281   (setq gnus-prev-page-map (make-sparse-keymap))
4282   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
4283   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
4284
4285 (defun gnus-insert-prev-page-button ()
4286   (let ((buffer-read-only nil))
4287     (gnus-eval-format
4288      gnus-prev-page-line-format nil
4289      `(gnus-prev t local-map ,gnus-prev-page-map
4290                  gnus-callback gnus-article-button-prev-page
4291                  article-type annotation))))
4292
4293 (defvar gnus-next-page-map nil)
4294 (unless gnus-next-page-map
4295   (setq gnus-next-page-map (make-keymap))
4296   (suppress-keymap gnus-prev-page-map)
4297   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
4298   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
4299
4300 (defun gnus-button-next-page ()
4301   "Go to the next page."
4302   (interactive)
4303   (let ((win (selected-window)))
4304     (select-window (get-buffer-window gnus-article-buffer t))
4305     (gnus-article-next-page)
4306     (select-window win)))
4307
4308 (defun gnus-button-prev-page ()
4309   "Go to the prev page."
4310   (interactive)
4311   (let ((win (selected-window)))
4312     (select-window (get-buffer-window gnus-article-buffer t))
4313     (gnus-article-prev-page)
4314     (select-window win)))
4315
4316 (defun gnus-insert-next-page-button ()
4317   (let ((buffer-read-only nil))
4318     (gnus-eval-format gnus-next-page-line-format nil
4319                       `(gnus-next
4320                         t local-map ,gnus-next-page-map
4321                         gnus-callback gnus-article-button-next-page
4322                         article-type annotation))))
4323
4324 (defun gnus-article-button-next-page (arg)
4325   "Go to the next page."
4326   (interactive "P")
4327   (let ((win (selected-window)))
4328     (select-window (get-buffer-window gnus-article-buffer t))
4329     (gnus-article-next-page)
4330     (select-window win)))
4331
4332 (defun gnus-article-button-prev-page (arg)
4333   "Go to the prev page."
4334   (interactive "P")
4335   (let ((win (selected-window)))
4336     (select-window (get-buffer-window gnus-article-buffer t))
4337     (gnus-article-prev-page)
4338     (select-window win)))
4339
4340 (defvar gnus-decode-header-methods
4341   '(mail-decode-encoded-word-region)
4342   "List of methods used to decode headers.
4343
4344 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
4345 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
4346 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
4347 whose names match REGEXP.
4348
4349 For example:
4350 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
4351  mail-decode-encoded-word-region
4352  (\"chinese\" . rfc1843-decode-region))
4353 ")
4354
4355 (defvar gnus-decode-header-methods-cache nil)
4356
4357 (defun gnus-multi-decode-header (start end)
4358   "Apply the functions from `gnus-encoded-word-methods' that match."
4359   (unless (and gnus-decode-header-methods-cache
4360                (eq gnus-newsgroup-name
4361                    (car gnus-decode-header-methods-cache)))
4362     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
4363     (mapc '(lambda (x)
4364              (if (symbolp x)
4365                  (nconc gnus-decode-header-methods-cache (list x))
4366                (if (and gnus-newsgroup-name
4367                         (string-match (car x) gnus-newsgroup-name))
4368                    (nconc gnus-decode-header-methods-cache
4369                           (list (cdr x))))))
4370           gnus-decode-header-methods))
4371   (let ((xlist gnus-decode-header-methods-cache))
4372     (pop xlist)
4373     (save-restriction
4374       (narrow-to-region start end)
4375       (while xlist
4376         (funcall (pop xlist) (point-min) (point-max))))))
4377
4378 ;;;
4379 ;;; Treatment top-level handling.
4380 ;;;
4381
4382 (defun gnus-treat-article (condition &optional part-number total-parts type)
4383   (let ((length (- (point-max) (point-min)))
4384         (alist gnus-treatment-function-alist)
4385         (article-goto-body-goes-to-point-min-p t)
4386         (treated-type
4387          (or (not type)
4388              (catch 'found
4389                (let ((list gnus-article-treat-types))
4390                  (while list
4391                    (when (string-match (pop list) type)
4392                      (throw 'found t)))))))
4393         (highlightp (gnus-visual-p 'article-highlight 'highlight))
4394         val elem)
4395     (gnus-run-hooks 'gnus-part-display-hook)
4396     (while (setq elem (pop alist))
4397       (setq val (symbol-value (car elem)))
4398       (when (and (or (consp val)
4399                      treated-type)
4400                  (gnus-treat-predicate val)
4401                  (or (not (get (car elem) 'highlight))
4402                      highlightp))
4403         (save-restriction
4404           (funcall (cadr elem)))))))
4405
4406 ;; Dynamic variables.
4407 (defvar part-number)
4408 (defvar total-parts)
4409 (defvar type)
4410 (defvar condition)
4411 (defvar length)
4412 (defun gnus-treat-predicate (val)
4413   (cond
4414    (condition
4415     (eq condition val))
4416    ((null val)
4417     nil)
4418    ((eq val t)
4419     t)
4420    ((eq val 'head)
4421     nil)
4422    ((eq val 'last)
4423     (eq part-number total-parts))
4424    ((numberp val)
4425     (< length val))
4426    ((and (listp val)
4427          (stringp (car val)))
4428     (apply 'gnus-or (mapcar `(lambda (s)
4429                                (string-match s ,(or gnus-newsgroup-name "")))
4430                             val)))
4431    ((listp val)
4432     (let ((pred (pop val)))
4433       (cond
4434        ((eq pred 'or)
4435         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
4436        ((eq pred 'and)
4437         (apply 'gnus-and (mapcar 'gnus-tread-predicate val)))
4438        ((eq pred 'not)
4439         (not (gnus-treat-predicate val)))
4440        ((eq pred 'typep)
4441         (equal (cadr val) type))
4442        (t
4443         (error "%S is not a valid predicate" pred)))))
4444    (t
4445     (error "%S is not a valid value" val))))
4446
4447 (gnus-ems-redefine)
4448
4449 (provide 'gnus-art)
4450
4451 (run-hooks 'gnus-art-load-hook)
4452
4453 ;;; gnus-art.el ends here