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