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