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