*** empty log message ***
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2 ;; Copyright (C) 1996,97 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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 (require 'custom)
29 (require 'gnus)
30 (require 'gnus-sum)
31 (require 'gnus-spec)
32 (require 'gnus-int)
33 (require 'browse-url)
34
35 (defgroup gnus-article nil
36   "Article display."
37   :link '(custom-manual "(gnus)The Article Buffer")
38   :group 'gnus)
39
40 (defgroup gnus-article-hiding nil
41   "Hiding article parts."
42   :link '(custom-manual "(gnus)Article Hiding")
43   :group 'gnus-article)
44
45 (defgroup gnus-article-highlight nil
46   "Article highlighting."
47   :link '(custom-manual "(gnus)Article Highlighting")
48   :group 'gnus-article
49   :group 'gnus-visual)
50
51 (defgroup gnus-article-signature nil
52   "Article signatures."
53   :link '(custom-manual "(gnus)Article Signature")
54   :group 'gnus-article)
55
56 (defgroup gnus-article-headers nil
57   "Article headers."
58   :link '(custom-manual "(gnus)Hiding Headers")
59   :group 'gnus-article)
60
61 (defgroup gnus-article-washing nil
62   "Special commands on articles."
63   :link '(custom-manual "(gnus)Article Washing")
64   :group 'gnus-article)
65
66 (defgroup gnus-article-emphasis nil
67   "Fontisizing articles."
68   :link '(custom-manual "(gnus)Article Fontisizing")
69   :group 'gnus-article)
70
71 (defgroup gnus-article-saving nil
72   "Saving articles."
73   :link '(custom-manual "(gnus)Saving Articles")
74   :group 'gnus-article)
75
76 (defgroup gnus-article-mime nil
77   "Worshiping the MIME wonder."
78   :link '(custom-manual "(gnus)Using MIME")
79   :group 'gnus-article)
80
81 (defgroup gnus-article-buttons nil
82   "Pushable buttons in the article buffer."
83   :link '(custom-manual "(gnus)Article Buttons")
84   :group 'gnus-article)
85
86 (defgroup gnus-article-various nil
87   "Other article options."
88   :link '(custom-manual "(gnus)Misc Article")
89   :group 'gnus-article)
90
91 (defcustom gnus-ignored-headers
92   '("^Path:" "^Posting-Version:" "^Article-I.D.:" "^Expires:"
93     "^Date-Received:" "^References:" "^Control:" "^Xref:" "^Lines:"
94     "^Posted:" "^Relay-Version:" "^Message-ID:" "^Nf-ID:" "^Nf-From:"
95     "^Approved:" "^Sender:" "^Received:" "^Mail-from:") 
96   "All headers that match this regexp will be hidden.
97 This variable can also be a list of regexps of headers to be ignored.
98 If `gnus-visible-headers' is non-nil, this variable will be ignored."
99   :type '(choice :custom-show nil
100                  regexp
101                  (repeat regexp))
102   :group 'gnus-article-hiding)
103
104 (defcustom gnus-visible-headers 
105   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-"
106   "All headers that do not match this regexp will be hidden.
107 This variable can also be a list of regexp of headers to remain visible.
108 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
109   :type '(repeat :value-to-internal (lambda (widget value)
110                                       (custom-split-regexp-maybe value))
111                  :match (lambda (widget value)
112                           (or (stringp value)
113                               (widget-editable-list-match widget value)))
114                  regexp)
115   :group 'gnus-article-hiding)
116
117 (defcustom gnus-sorted-header-list
118   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
119     "^Cc:" "^Date:" "^Organization:")
120   "This variable is a list of regular expressions.
121 If it is non-nil, headers that match the regular expressions will
122 be placed first in the article buffer in the sequence specified by
123 this list."
124   :type '(repeat regexp)
125   :group 'gnus-article-hiding)
126
127 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
128   "Headers that are only to be displayed if they have interesting data.
129 Possible values in this list are `empty', `newsgroups', `followup-to',
130 `reply-to', and `date'."
131   :type '(set (const :tag "Headers with no content." empty)
132               (const :tag "Newsgroups with only one group." newsgroups)
133               (const :tag "Followup-to identical to newsgroups." followup-to)
134               (const :tag "Reply-to identical to from." reply-to)
135               (const :tag "Date less than four days old." date))
136   :group 'gnus-article-hiding)
137
138 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
139   "Regexp matching signature separator.
140 This can also be a list of regexps.  In that case, it will be checked
141 from head to tail looking for a separator.  Searches will be done from
142 the end of the buffer."
143   :type '(repeat string)
144   :group 'gnus-article-signature)
145
146 (defcustom gnus-signature-limit nil
147    "Provide a limit to what is considered a signature.
148 If it is a number, no signature may not be longer (in characters) than
149 that number.  If it is a floating point number, no signature may be
150 longer (in lines) than that number.  If it is a function, the function
151 will be called without any parameters, and if it returns nil, there is
152 no signature in the buffer.  If it is a string, it will be used as a
153 regexp.  If it matches, the text in question is not a signature."
154   :type '(choice integer number function regexp)
155   :group 'gnus-article-signature)
156
157 (defcustom gnus-hidden-properties '(invisible t intangible t)
158   "Property list to use for hiding text."
159   :type 'sexp 
160   :group 'gnus-article-hiding)
161
162 (defcustom gnus-article-x-face-command
163   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
164   "String or function to be executed to display an X-Face header.
165 If it is a string, the command will be executed in a sub-shell
166 asynchronously.  The compressed face will be piped to this command."
167   :type 'string                         ;Leave function case to Lisp.
168   :group 'gnus-article-washing)
169
170 (defcustom gnus-article-x-face-too-ugly nil
171   "Regexp matching posters whose face shouldn't be shown automatically."
172   :type 'regexp
173   :group 'gnus-article-washing)
174
175 (defcustom gnus-emphasis-alist
176   (let ((format
177          "\\(\\s-\\|^\\|[-\"\(]\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\s-\\|[-?!.,;:\"\)]\\)")
178         (types
179          '(("_" "_" underline)
180            ("/" "/" italic)
181            ("\\*" "\\*" bold)
182            ("_/" "/_" underline-italic)
183            ("_\\*" "\\*_" underline-bold)
184            ("\\*/" "/\\*" bold-italic)
185            ("_\\*/" "/\\*_" underline-bold-italic))))
186     `(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
187        2 3 gnus-emphasis-underline)
188       ,@(mapcar
189          (lambda (spec)
190            (list
191             (format format (car spec) (cadr spec))
192             2 3 (intern (format "gnus-emphasis-%s" (caddr spec)))))
193          types)))
194   "Alist that says how to fontify certain phrases.
195 Each item looks like this:
196
197   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
198
199 The first element is a regular expression to be matched.  The second
200 is a number that says what regular expression grouping used to find
201 the entire emphasized word.  The third is a number that says what
202 regexp grouping should be displayed and highlighted.  The fourth
203 is the face used for highlighting."
204   :type '(repeat (list :value ("" 0 0 default)
205                        regexp
206                        (integer :tag "Match group")
207                        (integer :tag "Emphasize group")
208                        face))
209   :group 'gnus-article-emphasis)
210
211 (defface gnus-emphasis-bold '((t (:bold t)))
212   "Face used for displaying strong emphasized text (*word*)."
213   :group 'gnus-article-emphasis)
214
215 (defface gnus-emphasis-italic '((t (:italic t)))
216   "Face used for displaying italic emphasized text (/word/)."
217   :group 'gnus-article-emphasis)
218
219 (defface gnus-emphasis-underline '((t (:underline t)))
220   "Face used for displaying underlined emphasized text (_word_)."
221   :group 'gnus-article-emphasis)
222
223 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
224   "Face used for displaying underlined bold emphasized text (_*word*_)."
225   :group 'gnus-article-emphasis)
226
227 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
228   "Face used for displaying underlined italic emphasized text (_*word*_)."
229   :group 'gnus-article-emphasis)
230
231 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
232   "Face used for displaying bold italic emphasized text (/*word*/)."
233   :group 'gnus-article-emphasis)
234
235 (defface gnus-emphasis-underline-bold-italic 
236   '((t (:bold t :italic t :underline t)))
237   "Face used for displaying underlined bold italic emphasized text.
238 Esample: (_/*word*/_)."
239   :group 'gnus-article-emphasis)
240
241 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
242   "Format for display of Date headers in article bodies.
243 See `format-time-zone' for the possible values."
244   :type 'string
245   :link '(custom-manual "(gnus)Article Date")
246   :group 'gnus-article-washing)
247
248 (eval-and-compile
249   (autoload 'hexl-hex-string-to-integer "hexl")
250   (autoload 'timezone-make-date-arpa-standard "timezone")
251   (autoload 'mail-extract-address-components "mail-extr"))
252
253 (defcustom gnus-article-save-directory gnus-directory
254   "*Name of the directory articles will be saved in (default \"~/News\")."
255   :group 'gnus-article-saving
256   :type 'directory)
257
258 (defcustom gnus-save-all-headers t
259   "*If non-nil, don't remove any headers before saving."
260   :group 'gnus-article-saving
261   :type 'boolean)
262
263 (defcustom gnus-prompt-before-saving 'always
264   "*This variable says how much prompting is to be done when saving articles.
265 If it is nil, no prompting will be done, and the articles will be
266 saved to the default files.  If this variable is `always', each and
267 every article that is saved will be preceded by a prompt, even when
268 saving large batches of articles.  If this variable is neither nil not
269 `always', there the user will be prompted once for a file name for
270 each invocation of the saving commands."
271   :group 'gnus-article-saving
272   :type '(choice (item always)
273                  (item :tag "never" nil)
274                  (sexp :tag "once" :format "%t")))
275
276 (defcustom gnus-saved-headers gnus-visible-headers
277   "Headers to keep if `gnus-save-all-headers' is nil.
278 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
279 If that variable is nil, however, all headers that match this regexp
280 will be kept while the rest will be deleted before saving."
281   :group 'gnus-article-saving
282   :type '(repeat string))
283
284 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
285   "A function to save articles in your favourite format.
286 The function must be interactively callable (in other words, it must
287 be an Emacs command).
288
289 Gnus provides the following functions:
290
291 * gnus-summary-save-in-rmail (Rmail format)
292 * gnus-summary-save-in-mail (Unix mail format)
293 * gnus-summary-save-in-folder (MH folder)
294 * gnus-summary-save-in-file (article format)
295 * gnus-summary-save-in-vm (use VM's folder format)
296 * gnus-summary-write-to-file (article format -- overwrite)."
297   :group 'gnus-article-saving
298   :type '(radio (function-item gnus-summary-save-in-rmail)
299                 (function-item gnus-summary-save-in-mail)
300                 (function-item gnus-summary-save-in-folder)
301                 (function-item gnus-summary-save-in-file)
302                 (function-item gnus-summary-save-in-vm)
303                 (function-item gnus-summary-write-to-file)))
304
305 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
306   "A function generating a file name to save articles in Rmail format.
307 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
308   :group 'gnus-article-saving
309   :type 'function)
310
311 (defcustom gnus-mail-save-name 'gnus-plain-save-name
312   "A function generating a file name to save articles in Unix mail format.
313 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
314   :group 'gnus-article-saving
315   :type 'function)
316
317 (defcustom gnus-folder-save-name 'gnus-folder-save-name
318   "A function generating a file name to save articles in MH folder.
319 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
320   :group 'gnus-article-saving
321   :type 'function)
322
323 (defcustom gnus-file-save-name 'gnus-numeric-save-name
324   "A function generating a file name to save articles in article format.
325 The function is called with NEWSGROUP, HEADERS, and optional
326 LAST-FILE."
327   :group 'gnus-article-saving
328   :type 'function)
329
330 (defcustom gnus-split-methods
331   '((gnus-article-archive-name))
332   "Variable used to suggest where articles are to be saved.
333 For instance, if you would like to save articles related to Gnus in
334 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
335 you could set this variable to something like:
336
337  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
338    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
339
340 This variable is an alist where the where the key is the match and the
341 value is a list of possible files to save in if the match is non-nil.
342
343 If the match is a string, it is used as a regexp match on the
344 article.  If the match is a symbol, that symbol will be funcalled
345 from the buffer of the article to be saved with the newsgroup as the
346 parameter.  If it is a list, it will be evaled in the same buffer.
347
348 If this form or function returns a string, this string will be used as
349 a possible file name; and if it returns a non-nil list, that list will
350 be used as possible file names."
351   :group 'gnus-article-saving
352   :type '(repeat (choice (list function)
353                          (cons regexp (repeat string))
354                          sexp)))
355
356 (defcustom gnus-strict-mime t
357   "*If nil, MIME-decode even if there is no Mime-Version header."
358   :group 'gnus-article-mime
359   :type 'boolean)
360
361 (defcustom gnus-show-mime-method 'metamail-buffer
362   "Function to process a MIME message.
363 The function is called from the article buffer."
364   :group 'gnus-article-mime
365   :type 'function)
366
367 (defcustom gnus-decode-encoded-word-method 'gnus-article-de-quoted-unreadable
368   "*Function to decode MIME encoded words.
369 The function is called from the article buffer."
370   :group 'gnus-article-mime
371   :type 'function)
372
373 (defcustom gnus-page-delimiter "^\^L"
374   "*Regexp describing what to use as article page delimiters.
375 The default value is \"^\^L\", which is a form linefeed at the
376 beginning of a line."
377   :type 'regexp
378   :group 'gnus-article-various)
379
380 (defcustom gnus-article-mode-line-format "Gnus: %%b %S"
381   "*The format specification for the article mode line.
382 See `gnus-summary-mode-line-format' for a closer description."
383   :type 'string
384   :group 'gnus-article-various)
385
386 (defcustom gnus-article-mode-hook nil
387   "*A hook for Gnus article mode."
388   :type 'hook
389   :group 'gnus-article-various)
390
391 (defcustom gnus-article-menu-hook nil
392   "*Hook run after the creation of the article mode menu."
393   :type 'hook
394   :group 'gnus-article-various)
395
396 (defcustom gnus-article-prepare-hook nil
397   "*A hook called after an article has been prepared in the article buffer.
398 If you want to run a special decoding program like nkf, use this hook."
399   :type 'hook
400   :group 'gnus-article-various)
401
402 (defcustom gnus-article-button-face 'bold
403   "Face used for highlighting buttons in the article buffer.
404
405 An article button is a piece of text that you can activate by pressing
406 `RET' or `mouse-2' above it."
407   :type 'face
408   :group 'gnus-article-buttons)
409
410 (defcustom gnus-article-mouse-face 'highlight
411   "Face used for mouse highlighting in the article buffer.
412
413 Article buttons will be displayed in this face when the cursor is
414 above them."
415   :type 'face
416   :group 'gnus-article-buttons)
417
418 (defcustom gnus-signature-face 'italic
419   "Face used for highlighting a signature in the article buffer."
420   :type 'face
421   :group 'gnus-article-highlight
422   :group 'gnus-article-signature)
423
424 (defface gnus-header-from-face 
425   '((((class color)
426       (background dark))
427      (:foreground "light blue" :bold t :italic t))
428     (((class color)
429       (background light))
430      (:foreground "MidnightBlue" :bold t :italic t))
431     (t 
432      (:bold t :italic t)))
433   "Face used for displaying from headers."
434   :group 'gnus-article-headers
435   :group 'gnus-article-highlight)
436
437 (defface gnus-header-subject-face 
438   '((((class color)
439       (background dark))
440      (:foreground "pink" :bold t :italic t))
441     (((class color)
442       (background light))
443      (:foreground "firebrick" :bold t :italic t))
444     (t 
445      (:bold t :italic t)))
446   "Face used for displaying subject headers."
447   :group 'gnus-article-headers
448   :group 'gnus-article-highlight)
449
450 (defface gnus-header-newsgroups-face 
451   '((((class color)
452       (background dark))
453      (:foreground "yellow" :bold t :italic t))
454     (((class color)
455       (background light))
456      (:foreground "indianred" :bold t :italic t))
457     (t 
458      (:bold t :italic t)))
459   "Face used for displaying newsgroups headers."
460   :group 'gnus-article-headers
461   :group 'gnus-article-highlight)
462
463 (defface gnus-header-name-face 
464   '((((class color)
465       (background dark))
466      (:foreground "cyan" :bold t))
467     (((class color)
468       (background light))
469      (:foreground "DarkGreen" :bold t))
470     (t 
471      (:bold t)))
472   "Face used for displaying header names."
473   :group 'gnus-article-headers
474   :group 'gnus-article-highlight)
475
476 (defface gnus-header-content-face
477   '((((class color)
478       (background dark))
479      (:foreground "forest green" :italic t))
480     (((class color)
481       (background light))
482      (:foreground "DarkGreen" :italic t))
483     (t 
484      (:italic t)))  "Face used for displaying header content."
485   :group 'gnus-article-headers
486   :group 'gnus-article-highlight)
487
488 (defcustom gnus-header-face-alist
489   '(("From" nil gnus-header-from-face)
490     ("Subject" nil gnus-header-subject-face)
491     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
492     ("" gnus-header-name-face gnus-header-content-face))
493   "Controls highlighting of article header.
494
495 An alist of the form (HEADER NAME CONTENT). 
496
497 HEADER is a regular expression which should match the name of an
498 header header and NAME and CONTENT are either face names or nil.
499
500 The name of each header field will be displayed using the face
501 specified by the first element in the list where HEADER match the
502 header name and NAME is non-nil.  Similarly, the content will be
503 displayed by the first non-nil matching CONTENT face."
504   :group 'gnus-article-headers
505   :group 'gnus-article-highlight
506   :type '(repeat (list (regexp :tag "Header")
507                        (choice :tag "Name"
508                                (item :tag "skip" nil)
509                                (face :value default))
510                        (choice :tag "Content"
511                                (item :tag "skip" nil)
512                                (face :value default)))))
513
514 ;;; Internal variables
515
516 (defvar gnus-article-mode-syntax-table
517   (let ((table (copy-syntax-table text-mode-syntax-table)))
518     ;;(modify-syntax-entry ?_ "w" table)
519     table)
520   "Syntax table used in article mode buffers.
521 Initialized from `text-mode-syntax-table.")
522
523 (defvar gnus-save-article-buffer nil)
524
525 (defvar gnus-article-mode-line-format-alist
526     (nconc '((?w (gnus-article-wash-status) ?s))
527            gnus-summary-mode-line-format-alist))
528
529 (defvar gnus-number-of-articles-to-be-saved nil)
530
531 (defvar gnus-inhibit-hiding nil)
532 (defvar gnus-newsgroup-name)
533
534 (defsubst gnus-article-hide-text (b e props)
535   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
536   (add-text-properties b e props)
537   (when (memq 'intangible props)
538     (put-text-property 
539      (max (1- b) (point-min))
540      b 'intangible (cddr (memq 'intangible props)))))
541
542 (defsubst gnus-article-unhide-text (b e)
543   "Remove hidden text properties from region between B and E."
544   (remove-text-properties b e gnus-hidden-properties)
545   (when (memq 'intangible gnus-hidden-properties)
546     (put-text-property (max (1- b) (point-min))
547                        b 'intangible nil)))
548
549 (defun gnus-article-hide-text-type (b e type)
550   "Hide text of TYPE between B and E."
551   (gnus-article-hide-text
552    b e (cons 'article-type (cons type gnus-hidden-properties))))
553
554 (defun gnus-article-unhide-text-type (b e type)
555   "Hide text of TYPE between B and E."
556   (remove-text-properties
557    b e (cons 'article-type (cons type gnus-hidden-properties)))
558   (when (memq 'intangible gnus-hidden-properties)
559     (put-text-property (max (1- b) (point-min))
560                        b 'intangible nil)))
561
562 (defun gnus-article-hide-text-of-type (type)
563   "Hide text of TYPE in the current buffer."
564   (save-excursion
565     (let ((b (point-min))
566           (e (point-max)))
567       (while (setq b (text-property-any b e 'article-type type))
568         (add-text-properties b (incf b) gnus-hidden-properties)))))
569
570 (defun gnus-article-delete-text-of-type (type)
571   "Delete text of TYPE in the current buffer."
572   (save-excursion
573     (let ((b (point-min)))
574       (while (setq b (text-property-any b (point-max) 'article-type type))
575         (delete-region b (incf b))))))
576
577 (defun gnus-article-delete-invisible-text ()
578   "Delete all invisible text in the current buffer."
579   (save-excursion
580     (let ((b (point-min)))
581       (while (setq b (text-property-any b (point-max) 'invisible t))
582         (delete-region b (incf b))))))
583
584 (defun gnus-article-text-type-exists-p (type)
585   "Say whether any text of type TYPE exists in the buffer."
586   (text-property-any (point-min) (point-max) 'article-type type))
587
588 (defsubst gnus-article-header-rank ()
589   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
590   (let ((list gnus-sorted-header-list)
591         (i 0))
592     (while list
593       (when (looking-at (car list))
594         (setq list nil))
595       (setq list (cdr list))
596       (incf i))
597     i))
598
599 (defun article-hide-headers (&optional arg delete)
600   "Toggle whether to hide unwanted headers and possibly sort them as well.
601 If given a negative prefix, always show; if given a positive prefix,
602 always hide."
603   (interactive (gnus-article-hidden-arg))
604   (if (gnus-article-check-hidden-text 'headers arg)
605       ;; Show boring headers as well.
606       (gnus-article-show-hidden-text 'boring-headers)
607     ;; This function might be inhibited.
608     (unless gnus-inhibit-hiding
609       (save-excursion
610         (save-restriction
611           (let ((buffer-read-only nil)
612                 (props (nconc (list 'article-type 'headers)
613                               gnus-hidden-properties))
614                 (max (1+ (length gnus-sorted-header-list)))
615                 (ignored (when (not gnus-visible-headers)
616                            (cond ((stringp gnus-ignored-headers)
617                                   gnus-ignored-headers)
618                                  ((listp gnus-ignored-headers)
619                                   (mapconcat 'identity gnus-ignored-headers
620                                              "\\|")))))
621                 (visible
622                  (cond ((stringp gnus-visible-headers)
623                         gnus-visible-headers)
624                        ((and gnus-visible-headers
625                              (listp gnus-visible-headers))
626                         (mapconcat 'identity gnus-visible-headers "\\|"))))
627                 (inhibit-point-motion-hooks t)
628                 want-list beg)
629             ;; First we narrow to just the headers.
630             (widen)
631             (goto-char (point-min))
632             ;; Hide any "From " lines at the beginning of (mail) articles.
633             (while (looking-at "From ")
634               (forward-line 1))
635             (unless (bobp)
636               (if delete
637                   (delete-region (point-min) (point))
638                 (gnus-article-hide-text (point-min) (point) props)))
639             ;; Then treat the rest of the header lines.
640             (narrow-to-region
641              (point)
642              (if (search-forward "\n\n" nil t) ; if there's a body
643                  (progn (forward-line -1) (point))
644                (point-max)))
645             ;; Then we use the two regular expressions
646             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
647             ;; select which header lines is to remain visible in the
648             ;; article buffer.
649             (goto-char (point-min))
650             (while (re-search-forward "^[^ \t]*:" nil t)
651               (beginning-of-line)
652               ;; Mark the rank of the header.
653               (put-text-property 
654                (point) (1+ (point)) 'message-rank
655                (if (or (and visible (looking-at visible))
656                        (and ignored
657                             (not (looking-at ignored))))
658                    (gnus-article-header-rank) 
659                  (+ 2 max)))
660               (forward-line 1))
661             (message-sort-headers-1)
662             (when (setq beg (text-property-any 
663                              (point-min) (point-max) 'message-rank (+ 2 max)))
664               ;; We make the unwanted headers invisible.
665               (if delete
666                   (delete-region beg (point-max))
667                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
668                 (gnus-article-hide-text-type beg (point-max) 'headers))
669               ;; Work around XEmacs lossage.
670               (put-text-property (point-min) beg 'invisible nil))))))))
671
672 (defun article-hide-boring-headers (&optional arg)
673   "Toggle hiding of headers that aren't very interesting.
674 If given a negative prefix, always show; if given a positive prefix,
675 always hide."
676   (interactive (gnus-article-hidden-arg))
677   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
678              (not gnus-show-all-headers))
679     (save-excursion
680       (save-restriction
681         (let ((buffer-read-only nil)
682               (list gnus-boring-article-headers)
683               (inhibit-point-motion-hooks t)
684               elem)
685           (nnheader-narrow-to-headers)
686           (while list
687             (setq elem (pop list))
688             (goto-char (point-min))
689             (cond
690              ;; Hide empty headers.
691              ((eq elem 'empty)
692               (while (re-search-forward "^[^:]+:[ \t]*\n[^ \t]" nil t)
693                 (forward-line -1)
694                 (gnus-article-hide-text-type
695                  (progn (beginning-of-line) (point))
696                  (progn 
697                    (end-of-line)
698                    (if (re-search-forward "^[^ \t]" nil t)
699                        (match-beginning 0)
700                      (point-max)))
701                  'boring-headers)))
702              ;; Hide boring Newsgroups header.
703              ((eq elem 'newsgroups)
704               (when (equal (gnus-fetch-field "newsgroups")
705                            (gnus-group-real-name
706                             (if (boundp 'gnus-newsgroup-name)
707                                 gnus-newsgroup-name
708                               "")))
709                 (gnus-article-hide-header "newsgroups")))
710              ((eq elem 'followup-to)
711               (when (equal (message-fetch-field "followup-to")
712                            (message-fetch-field "newsgroups"))
713                 (gnus-article-hide-header "followup-to")))
714              ((eq elem 'reply-to)
715               (let ((from (message-fetch-field "from"))
716                     (reply-to (message-fetch-field "reply-to")))
717                 (when (and
718                        from reply-to
719                        (ignore-errors
720                          (equal 
721                           (nth 1 (mail-extract-address-components from))
722                           (nth 1 (mail-extract-address-components reply-to)))))
723                   (gnus-article-hide-header "reply-to"))))
724              ((eq elem 'date)
725               (let ((date (message-fetch-field "date")))
726                 (when (and date
727                            (< (gnus-days-between (current-time-string) date)
728                               4))
729                   (gnus-article-hide-header "date")))))))))))
730
731 (defun gnus-article-hide-header (header)
732   (save-excursion
733     (goto-char (point-min))
734     (when (re-search-forward (concat "^" header ":") nil t)
735       (gnus-article-hide-text-type
736        (progn (beginning-of-line) (point))
737        (progn 
738          (end-of-line)
739          (if (re-search-forward "^[^ \t]" nil t)
740              (match-beginning 0)
741            (point-max)))
742        'boring-headers))))
743
744 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
745 (defun article-treat-overstrike ()
746   "Translate overstrikes into bold text."
747   (interactive)
748   (save-excursion
749     (let ((buffer-read-only nil))
750       (while (search-forward "\b" nil t)
751         (let ((next (following-char))
752               (previous (char-after (- (point) 2))))
753           ;; We do the boldification/underlining by hiding the
754           ;; overstrikes and putting the proper text property
755           ;; on the letters.
756           (cond 
757            ((eq next previous)
758             (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
759             (put-text-property (point) (1+ (point)) 'face 'bold))
760            ((eq next ?_)
761             (gnus-article-hide-text-type (1- (point)) (1+ (point)) 'overstrike)
762             (put-text-property
763              (- (point) 2) (1- (point)) 'face 'underline))
764            ((eq previous ?_)
765             (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
766             (put-text-property
767              (point) (1+ (point)) 'face 'underline))))))))
768
769 (defun article-fill ()
770   "Format too long lines."
771   (interactive)
772   (save-excursion
773     (let ((buffer-read-only nil))
774       (widen)
775       (goto-char (point-min))
776       (search-forward "\n\n" nil t)
777       (end-of-line 1)
778       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
779             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
780             (adaptive-fill-mode t))
781         (while (not (eobp))
782           (and (>= (current-column) (min fill-column (window-width)))
783                (/= (preceding-char) ?:)
784                (fill-paragraph nil))
785           (end-of-line 2))))))
786
787 (defun article-remove-cr ()
788   "Remove carriage returns from an article."
789   (interactive)
790   (save-excursion
791     (let ((buffer-read-only nil))
792       (goto-char (point-min))
793       (while (search-forward "\r" nil t)
794         (replace-match "" t t)))))
795
796 (defun article-remove-trailing-blank-lines ()
797   "Remove all trailing blank lines from the article."
798   (interactive)
799   (save-excursion
800     (let ((buffer-read-only nil))
801       (goto-char (point-max))
802       (delete-region
803        (point)
804        (progn
805          (while (and (not (bobp))
806                      (looking-at "^[ \t]*$"))
807            (forward-line -1))
808          (forward-line 1)
809          (point))))))
810
811 (defun article-display-x-face (&optional force)
812   "Look for an X-Face header and display it if present."
813   (interactive (list 'force))
814   (save-excursion
815     ;; Delete the old process, if any.
816     (when (process-status "article-x-face")
817       (delete-process "article-x-face"))
818     (let ((inhibit-point-motion-hooks t)
819           (case-fold-search nil)
820           from)
821       (save-restriction
822         (nnheader-narrow-to-headers)
823         (setq from (message-fetch-field "from"))
824         (goto-char (point-min))
825         (when (and gnus-article-x-face-command
826                    (or force
827                        ;; Check whether this face is censored.
828                        (not gnus-article-x-face-too-ugly)
829                        (and gnus-article-x-face-too-ugly from
830                             (not (string-match gnus-article-x-face-too-ugly
831                                                from))))
832                    ;; Has to be present.
833                    (re-search-forward "^X-Face: " nil t))
834           ;; We now have the area of the buffer where the X-Face is stored.
835           (let ((beg (point))
836                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
837             ;; We display the face.
838             (if (symbolp gnus-article-x-face-command)
839                 ;; The command is a lisp function, so we call it.
840                 (if (gnus-functionp gnus-article-x-face-command)
841                     (funcall gnus-article-x-face-command beg end)
842                   (error "%s is not a function" gnus-article-x-face-command))
843               ;; The command is a string, so we interpret the command
844               ;; as a, well, command, and fork it off.
845               (let ((process-connection-type nil))
846                 (process-kill-without-query
847                  (start-process
848                   "article-x-face" nil shell-file-name shell-command-switch
849                   gnus-article-x-face-command))
850                 (process-send-region "article-x-face" beg end)
851                 (process-send-eof "article-x-face")))))))))
852
853 (defalias 'gnus-decode-rfc1522 'article-decode-rfc1522)
854 (defalias 'gnus-article-decode-rfc1522 'article-decode-rfc1522)
855 (defun article-decode-rfc1522 ()
856   "Hack to remove QP encoding from headers."
857   (let ((case-fold-search t)
858         (inhibit-point-motion-hooks t)
859         (buffer-read-only nil)
860         string)
861     (save-restriction
862       (narrow-to-region
863        (goto-char (point-min))
864        (or (search-forward "\n\n" nil t) (point-max)))
865       (goto-char (point-min))
866       (while (re-search-forward 
867               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
868         (setq string (match-string 1))
869         (save-restriction
870           (narrow-to-region (match-beginning 0) (match-end 0))
871           (delete-region (point-min) (point-max))
872           (insert string)
873           (article-mime-decode-quoted-printable 
874            (goto-char (point-min)) (point-max))
875           (subst-char-in-region (point-min) (point-max) ?_ ? )
876           (goto-char (point-max)))
877         (goto-char (point-min))))))
878
879 (defun article-de-quoted-unreadable (&optional force)
880   "Do a naive translation of a quoted-printable-encoded article.
881 This is in no way, shape or form meant as a replacement for real MIME
882 processing, but is simply a stop-gap measure until MIME support is
883 written.
884 If FORCE, decode the article whether it is marked as quoted-printable
885 or not."
886   (interactive (list 'force))
887   (save-excursion
888     (let ((case-fold-search t)
889           (buffer-read-only nil)
890           (type (gnus-fetch-field "content-transfer-encoding")))
891       (gnus-article-decode-rfc1522)
892       (when (or force
893                 (and type (string-match "quoted-printable" (downcase type))))
894         (goto-char (point-min))
895         (search-forward "\n\n" nil 'move)
896         (article-mime-decode-quoted-printable (point) (point-max))))))
897
898 (defun article-mime-decode-quoted-printable-buffer ()
899   "Decode Quoted-Printable in the current buffer."
900   (article-mime-decode-quoted-printable (point-min) (point-max)))
901   
902 (defun article-mime-decode-quoted-printable (from to)
903   "Decode Quoted-Printable in the region between FROM and TO."
904   (interactive "r")
905   (goto-char from)
906   (while (search-forward "=" to t)
907     (cond ((eq (following-char) ?\n)
908            (delete-char -1)
909            (delete-char 1))
910           ((looking-at "[0-9A-F][0-9A-F]")
911            (subst-char-in-region
912             (1- (point)) (point) ?=
913             (hexl-hex-string-to-integer
914              (buffer-substring (point) (+ 2 (point)))))
915            (delete-char 2))
916           ((looking-at "=")
917            (delete-char 1))
918           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
919
920 (defun article-hide-pgp (&optional arg)
921   "Toggle hiding of any PGP headers and signatures in the current article.
922 If given a negative prefix, always show; if given a positive prefix,
923 always hide."
924   (interactive (gnus-article-hidden-arg))
925   (unless (gnus-article-check-hidden-text 'pgp arg)
926     (save-excursion
927       (let ((inhibit-point-motion-hooks t)
928             buffer-read-only beg end)
929         (widen)
930         (goto-char (point-min))
931         ;; Hide the "header".
932         (when (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
933           (gnus-article-hide-text-type (match-beginning 0) (match-end 0) 'pgp))
934         (setq beg (point))
935         ;; Hide the actual signature.
936         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
937              (setq end (1+ (match-beginning 0)))
938              (gnus-article-hide-text-type
939               end
940               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
941                   (match-end 0)
942                 ;; Perhaps we shouldn't hide to the end of the buffer
943                 ;; if there is no end to the signature?
944                 (point-max))
945               'pgp))
946         ;; Hide "- " PGP quotation markers.
947         (when (and beg end)
948           (narrow-to-region beg end)
949           (goto-char (point-min))
950           (while (re-search-forward "^- " nil t)
951             (gnus-article-hide-text-type (match-beginning 0) (match-end 0) 'pgp))
952           (widen))))))
953
954 (defun article-hide-pem (&optional arg)
955   "Toggle hiding of any PEM headers and signatures in the current article.
956 If given a negative prefix, always show; if given a positive prefix,
957 always hide."
958   (interactive (gnus-article-hidden-arg))
959   (unless (gnus-article-check-hidden-text 'pem arg)
960     (save-excursion
961       (let (buffer-read-only end)
962         (widen)
963         (goto-char (point-min))
964         ;; hide the horrendously ugly "header".
965         (and (search-forward "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
966                              nil
967                              t)
968              (setq end (1+ (match-beginning 0)))
969              (gnus-article-hide-text-type
970               end
971               (if (search-forward "\n\n" nil t)
972                   (match-end 0)
973                 (point-max))
974               'pem))
975         ;; hide the trailer as well
976         (and (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
977                              nil
978                              t)
979              (gnus-article-hide-text-type
980               (match-beginning 0) (match-end 0) 'pem))))))
981
982 (defun article-hide-signature (&optional arg)
983   "Hide the signature in the current article.
984 If given a negative prefix, always show; if given a positive prefix,
985 always hide."
986   (interactive (gnus-article-hidden-arg))
987   (unless (gnus-article-check-hidden-text 'signature arg)
988     (save-excursion
989       (save-restriction
990         (let ((buffer-read-only nil))
991           (when (gnus-article-narrow-to-signature)
992             (gnus-article-hide-text-type 
993              (point-min) (point-max) 'signature)))))))
994
995 (defun article-strip-leading-blank-lines ()
996   "Remove all blank lines from the beginning of the article."
997   (interactive)
998   (save-excursion
999     (let ((inhibit-point-motion-hooks t)
1000           buffer-read-only)
1001       (goto-char (point-min))
1002       (when (search-forward "\n\n" nil t)
1003         (while (and (not (eobp))
1004                     (looking-at "[ \t]*$"))
1005           (gnus-delete-line))))))
1006
1007 (defun article-strip-multiple-blank-lines ()
1008   "Replace consecutive blank lines with one empty line."
1009   (interactive)
1010   (save-excursion
1011     (let (buffer-read-only)
1012       ;; First make all blank lines empty.
1013       (goto-char (point-min))
1014       (while (re-search-forward "^[ \t]+$" nil t)
1015         (replace-match "" nil t))
1016       ;; Then replace multiple empty lines with a single empty line.
1017       (goto-char (point-min))
1018       (while (re-search-forward "\n\n\n+" nil t)
1019         (replace-match "\n\n" t t)))))
1020
1021 (defun article-strip-blank-lines ()
1022   "Strip leading, trailing and multiple blank lines."
1023   (interactive)
1024   (article-strip-leading-blank-lines)
1025   (article-remove-trailing-blank-lines)
1026   (article-strip-multiple-blank-lines))
1027
1028 (defvar mime::preview/content-list)
1029 (defvar mime::preview-content-info/point-min)
1030 (defun gnus-article-narrow-to-signature ()
1031   "Narrow to the signature; return t if a signature is found, else nil."
1032   (widen)
1033   (when (and (boundp 'mime::preview/content-list)
1034              mime::preview/content-list)
1035     ;; We have a MIMEish article, so we use the MIME data to narrow.
1036     (let ((pcinfo (car (last mime::preview/content-list))))
1037       (ignore-errors
1038         (narrow-to-region
1039          (funcall (intern "mime::preview-content-info/point-min") pcinfo)
1040          (point-max)))))
1041   
1042   (when (gnus-article-search-signature)
1043     (forward-line 1)
1044     ;; Check whether we have some limits to what we consider
1045     ;; to be a signature.
1046     (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1047                     (list gnus-signature-limit)))
1048           limit limited)
1049       (while (setq limit (pop limits))
1050         (if (or (and (integerp limit)
1051                      (< (- (point-max) (point)) limit))
1052                 (and (floatp limit)
1053                      (< (count-lines (point) (point-max)) limit))
1054                 (and (gnus-functionp limit)
1055                      (funcall limit))
1056                 (and (stringp limit)
1057                      (not (re-search-forward limit nil t))))
1058             ()                          ; This limit did not succeed.
1059           (setq limited t
1060                 limits nil)))
1061       (unless limited
1062         (narrow-to-region (point) (point-max))
1063         t))))
1064
1065 (defun gnus-article-search-signature ()
1066   "Search the current buffer for the signature separator.
1067 Put point at the beginning of the signature separator."
1068   (let ((cur (point)))
1069     (goto-char (point-max))
1070     (if (if (stringp gnus-signature-separator)
1071             (re-search-backward gnus-signature-separator nil t)
1072           (let ((seps gnus-signature-separator))
1073             (while (and seps
1074                         (not (re-search-backward (car seps) nil t)))
1075               (pop seps))
1076             seps))
1077         t
1078       (goto-char cur)
1079       nil)))
1080
1081 (defun gnus-article-hidden-arg ()
1082   "Return the current prefix arg as a number, or 0 if no prefix."
1083   (list (if current-prefix-arg
1084             (prefix-numeric-value current-prefix-arg)
1085           0)))
1086
1087 (defun gnus-article-check-hidden-text (type arg)
1088   "Return nil if hiding is necessary.
1089 Arg can be nil or a number.  Nil and positive means hide, negative
1090 means show, 0 means toggle."
1091   (save-excursion
1092     (let ((hide (gnus-article-hidden-text-p type)))
1093       (cond
1094        ((or (null arg)
1095             (> arg 0))
1096         nil)
1097        ((< arg 0)
1098         (gnus-article-show-hidden-text type))
1099        (t
1100         (if (eq hide 'hidden)
1101             (gnus-article-show-hidden-text type)
1102           nil))))))
1103
1104 (defun gnus-article-hidden-text-p (type)
1105   "Say whether the current buffer contains hidden text of type TYPE."
1106   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1107     (when pos
1108       (if (get-text-property pos 'invisible)
1109           'hidden
1110         'shown))))
1111
1112 (defun gnus-article-show-hidden-text (type &optional hide)
1113   "Show all hidden text of type TYPE.
1114 If HIDE, hide the text instead."
1115   (save-excursion
1116     (let ((buffer-read-only nil)
1117           (inhibit-point-motion-hooks t)
1118           (end (point-min))
1119           beg)
1120       (while (setq beg (text-property-any end (point-max) 'article-type type))
1121         (goto-char beg)
1122         (setq end (or
1123                    (text-property-not-all beg (point-max) 'article-type type)
1124                    (point-max)))
1125         (if hide
1126             (gnus-article-hide-text beg end gnus-hidden-properties)
1127           (gnus-article-unhide-text beg end))
1128         (goto-char end))
1129       t)))
1130
1131 (defconst article-time-units
1132   `((year . ,(* 365.25 24 60 60))
1133     (week . ,(* 7 24 60 60))
1134     (day . ,(* 24 60 60))
1135     (hour . ,(* 60 60))
1136     (minute . 60)
1137     (second . 1))
1138   "Mapping from time units to seconds.")
1139
1140 (defun article-date-ut (&optional type highlight header)
1141   "Convert DATE date to universal time in the current article.
1142 If TYPE is `local', convert to local time; if it is `lapsed', output
1143 how much time has lapsed since DATE."
1144   (interactive (list 'ut t))
1145   (let* ((header (or header 
1146                      (mail-header-date gnus-current-headers)
1147                      (message-fetch-field "date")
1148                      ""))
1149          (date (if (vectorp header) (mail-header-date header)
1150                  header))
1151          (date-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1152          (inhibit-point-motion-hooks t)
1153          bface eface)
1154     (when (and date (not (string= date "")))
1155       (save-excursion
1156         (save-restriction
1157           (nnheader-narrow-to-headers)
1158           (let ((buffer-read-only nil))
1159             ;; Delete any old Date headers.
1160             (if (re-search-forward date-regexp nil t)
1161                 (progn
1162                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
1163                         eface (get-text-property (1- (gnus-point-at-eol))
1164                                                  'face))
1165                   (message-remove-header date-regexp t)
1166                   (beginning-of-line))
1167               (goto-char (point-max)))
1168             (insert (article-make-date-line date type))
1169             ;; Do highlighting.
1170             (forward-line -1)
1171             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1172               (put-text-property (match-beginning 1) (match-end 1)
1173                                  'face bface)
1174               (put-text-property (match-beginning 2) (match-end 2)
1175                                  'face eface))))))))
1176
1177 (defun article-make-date-line (date type)
1178   "Return a DATE line of TYPE."
1179   (cond
1180    ;; Convert to the local timezone.  We have to slap a
1181    ;; `condition-case' round the calls to the timezone
1182    ;; functions since they aren't particularly resistant to
1183    ;; buggy dates.
1184    ((eq type 'local)
1185     (concat "Date: " (condition-case ()
1186                          (timezone-make-date-arpa-standard date)
1187                        (error date))
1188             "\n"))
1189    ;; Convert to Universal Time.
1190    ((eq type 'ut)
1191     (concat "Date: "
1192             (condition-case ()
1193                 (timezone-make-date-arpa-standard date nil "UT")
1194               (error date))
1195             "\n"))
1196    ;; Get the original date from the article.
1197    ((eq type 'original)
1198     (concat "Date: " date "\n"))
1199    ;; Let the user define the format.
1200    ((eq type 'user)
1201     (format-time-string gnus-article-time-format
1202                         (ignore-errors
1203                           (gnus-encode-date
1204                            (timezone-make-date-arpa-standard
1205                             date nil "UT")))))
1206    ;; Do an X-Sent lapsed format.
1207    ((eq type 'lapsed)
1208     ;; If the date is seriously mangled, the timezone functions are
1209     ;; liable to bug out, so we ignore all errors.
1210     (let* ((now (current-time))
1211            (real-time
1212             (ignore-errors
1213               (gnus-time-minus
1214                (gnus-encode-date
1215                 (timezone-make-date-arpa-standard
1216                  (current-time-string now)
1217                  (current-time-zone now) "UT"))
1218                (gnus-encode-date
1219                 (timezone-make-date-arpa-standard
1220                  date nil "UT")))))
1221            (real-sec (and real-time
1222                           (+ (* (float (car real-time)) 65536)
1223                              (cadr real-time))))
1224            (sec (and real-time (abs real-sec)))
1225            num prev)
1226       (cond
1227        ((null real-time)
1228         "X-Sent: Unknown\n")
1229        ((zerop sec)
1230         "X-Sent: Now\n")
1231        (t
1232         (concat
1233          "X-Sent: "
1234          ;; This is a bit convoluted, but basically we go
1235          ;; through the time units for years, weeks, etc,
1236          ;; and divide things to see whether that results
1237          ;; in positive answers.
1238          (mapconcat
1239           (lambda (unit)
1240             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1241                 ;; The (remaining) seconds are too few to
1242                 ;; be divided into this time unit.
1243                 ""
1244               ;; It's big enough, so we output it.
1245               (setq sec (- sec (* num (cdr unit))))
1246               (prog1
1247                   (concat (if prev ", " "") (int-to-string
1248                                              (floor num))
1249                           " " (symbol-name (car unit)) 
1250                           (if (> num 1) "s" ""))
1251                 (setq prev t))))
1252           article-time-units "")
1253          ;; If dates are odd, then it might appear like the
1254          ;; article was sent in the future.
1255          (if (> real-sec 0)
1256              " ago\n"
1257            " in the future\n"))))))
1258    (t
1259     (error "Unknown conversion type: %s" type))))
1260
1261 (defun article-date-local (&optional highlight)
1262   "Convert the current article date to the local timezone."
1263   (interactive (list t))
1264   (article-date-ut 'local highlight))
1265
1266 (defun article-date-original (&optional highlight)
1267   "Convert the current article date to what it was originally.
1268 This is only useful if you have used some other date conversion
1269 function and want to see what the date was before converting."
1270   (interactive (list t))
1271   (article-date-ut 'original highlight))
1272
1273 (defun article-date-lapsed (&optional highlight)
1274   "Convert the current article date to time lapsed since it was sent."
1275   (interactive (list t))
1276   (article-date-ut 'lapsed highlight))
1277
1278 (defun article-date-user (&optional highlight)
1279   "Convert the current article date to the user-defined format."
1280   (interactive (list t))
1281   (article-date-ut 'user highlight))
1282
1283 (defun article-show-all ()
1284   "Show all hidden text in the article buffer."
1285   (interactive)
1286   (save-excursion
1287     (let ((buffer-read-only nil))
1288       (gnus-article-unhide-text (point-min) (point-max)))))
1289
1290 (defun article-emphasize (&optional arg)
1291   "Emphasize text according to `gnus-emphasis-alist'."
1292   (interactive (gnus-article-hidden-arg))
1293   (unless (gnus-article-check-hidden-text 'emphasis arg)
1294     (save-excursion
1295       (let ((alist gnus-emphasis-alist)
1296             (buffer-read-only nil)
1297             (props (append '(article-type emphasis)
1298                            gnus-hidden-properties))
1299             regexp elem beg invisible visible face)
1300         (goto-char (point-min))
1301         (search-forward "\n\n" nil t)
1302         (setq beg (point))
1303         (while (setq elem (pop alist))
1304           (goto-char beg)
1305           (setq regexp (car elem)
1306                 invisible (nth 1 elem)
1307                 visible (nth 2 elem)
1308                 face (nth 3 elem))
1309           (while (re-search-forward regexp nil t)
1310             (when (and (match-beginning visible) (match-beginning invisible))
1311               (gnus-article-hide-text
1312                (match-beginning invisible) (match-end invisible) props)
1313               (gnus-article-unhide-text-type
1314                (match-beginning visible) (match-end visible) 'emphasis)
1315               (gnus-put-text-property-excluding-newlines
1316                (match-beginning visible) (match-end visible) 'face face)
1317               (goto-char (match-end invisible)))))))))
1318
1319 (defvar gnus-summary-article-menu)
1320 (defvar gnus-summary-post-menu)
1321
1322 ;;; Saving functions.
1323
1324 (defun gnus-article-save (save-buffer file &optional num)
1325   "Save the currently selected article."
1326   (unless gnus-save-all-headers
1327     ;; Remove headers according to `gnus-saved-headers'.
1328     (let ((gnus-visible-headers
1329            (or gnus-saved-headers gnus-visible-headers))
1330           (gnus-article-buffer save-buffer))
1331       (gnus-article-hide-headers 1 t)))
1332   (save-window-excursion
1333     (if (not gnus-default-article-saver)
1334         (error "No default saver is defined.")
1335       ;; !!! Magic!  The saving functions all save
1336       ;; `gnus-original-article-buffer' (or so they think), but we
1337       ;; bind that variable to our save-buffer.
1338       (set-buffer gnus-article-buffer)
1339       (let* ((gnus-save-article-buffer save-buffer)
1340              (filename
1341               (cond
1342                ((not gnus-prompt-before-saving) 'default)
1343                ((eq gnus-prompt-before-saving 'always) nil)
1344                (t file)))
1345              (gnus-number-of-articles-to-be-saved
1346               (when (eq gnus-prompt-before-saving t)
1347                 num)))                  ; Magic
1348         (set-buffer gnus-summary-buffer)
1349         (funcall gnus-default-article-saver filename))))) 
1350
1351 (defun gnus-read-save-file-name (prompt default-name &optional filename)
1352   (cond
1353    ((eq filename 'default)
1354     default-name)
1355    (filename filename)
1356    (t
1357     (let* ((split-name (gnus-get-split-value gnus-split-methods))
1358            (prompt
1359             (format prompt (if (and gnus-number-of-articles-to-be-saved
1360                                     (> gnus-number-of-articles-to-be-saved 1))
1361                                (format "these %d articles"
1362                                        gnus-number-of-articles-to-be-saved)
1363                              "this article")))
1364            (file
1365             ;; Let the split methods have their say.
1366             (cond
1367              ;; No split name was found.
1368              ((null split-name)
1369               (read-file-name
1370                (concat prompt " (default "
1371                        (file-name-nondirectory default-name) ") ")
1372                (file-name-directory default-name)
1373                default-name))
1374              ;; A single split name was found
1375              ((= 1 (length split-name))
1376               (let* ((name (car split-name))
1377                      (dir (cond ((file-directory-p name)
1378                                  (file-name-as-directory name))
1379                                 ((file-exists-p name) name)
1380                                 (t gnus-article-save-directory))))
1381                 (read-file-name
1382                  (concat prompt " (default " name ") ")
1383                  dir name)))
1384              ;; A list of splits was found.
1385              (t
1386               (setq split-name (nreverse split-name))
1387               (let (result)
1388                 (let ((file-name-history (nconc split-name file-name-history)))
1389                   (setq result
1390                         (expand-file-name
1391                          (read-file-name
1392                           (concat prompt " (`M-p' for defaults) ")
1393                           gnus-article-save-directory
1394                           (car split-name))
1395                          gnus-article-save-directory)))
1396                 (car (push result file-name-history)))))))
1397       ;; Create the directory.
1398       (gnus-make-directory (file-name-directory file))
1399       ;; If we have read a directory, we append the default file name.
1400       (when (file-directory-p file)
1401         (setq file (concat (file-name-as-directory file)
1402                            (file-name-nondirectory default-name))))
1403       ;; Possibly translate some characters.
1404       (nnheader-translate-file-chars file)))))
1405
1406 (defun gnus-article-archive-name (group)
1407   "Return the first instance of an \"Archive-name\" in the current buffer."
1408   (let ((case-fold-search t))
1409     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
1410       (nnheader-concat gnus-article-save-directory
1411                        (match-string 1)))))
1412
1413 (defun gnus-summary-save-in-rmail (&optional filename)
1414   "Append this article to Rmail file.
1415 Optional argument FILENAME specifies file name.
1416 Directory to save to is default to `gnus-article-save-directory'."
1417   (interactive)
1418   (gnus-set-global-variables)
1419   (let ((default-name
1420           (funcall gnus-rmail-save-name gnus-newsgroup-name
1421                    gnus-current-headers gnus-newsgroup-last-rmail)))
1422     (setq filename (gnus-read-save-file-name
1423                     "Save %s in rmail file:" default-name filename))
1424     (gnus-make-directory (file-name-directory filename))
1425     (gnus-eval-in-buffer-window gnus-save-article-buffer
1426       (save-excursion
1427         (save-restriction
1428           (widen)
1429           (gnus-output-to-rmail filename))))
1430     ;; Remember the directory name to save articles
1431     (setq gnus-newsgroup-last-rmail filename)))
1432
1433 (defun gnus-summary-save-in-mail (&optional filename)
1434   "Append this article to Unix mail file.
1435 Optional argument FILENAME specifies file name.
1436 Directory to save to is default to `gnus-article-save-directory'."
1437   (interactive)
1438   (gnus-set-global-variables)
1439   (let ((default-name
1440           (funcall gnus-mail-save-name gnus-newsgroup-name
1441                    gnus-current-headers gnus-newsgroup-last-mail)))
1442     (setq filename (gnus-read-save-file-name
1443                     "Save %s in Unix mail file:" default-name filename))
1444     (setq filename
1445           (expand-file-name filename
1446                             (and default-name
1447                                  (file-name-directory default-name))))
1448     (gnus-make-directory (file-name-directory filename))
1449     (gnus-eval-in-buffer-window gnus-save-article-buffer
1450       (save-excursion
1451         (save-restriction
1452           (widen)
1453           (if (and (file-readable-p filename) (mail-file-babyl-p filename))
1454               (gnus-output-to-rmail filename)
1455             (let ((mail-use-rfc822 t))
1456               (rmail-output filename 1 t t))))))
1457     ;; Remember the directory name to save articles.
1458     (setq gnus-newsgroup-last-mail filename)))
1459
1460 (defun gnus-summary-save-in-file (&optional filename overwrite)
1461   "Append this article to file.
1462 Optional argument FILENAME specifies file name.
1463 Directory to save to is default to `gnus-article-save-directory'."
1464   (interactive)
1465   (gnus-set-global-variables)
1466   (let ((default-name
1467           (funcall gnus-file-save-name gnus-newsgroup-name
1468                    gnus-current-headers gnus-newsgroup-last-file)))
1469     (setq filename (gnus-read-save-file-name
1470                     "Save %s in file:" default-name filename))
1471     (gnus-make-directory (file-name-directory filename))
1472     (gnus-eval-in-buffer-window gnus-save-article-buffer
1473       (save-excursion
1474         (save-restriction
1475           (widen)
1476           (when (and overwrite
1477                      (file-exists-p filename))
1478             (delete-file filename))
1479           (gnus-output-to-file filename))))
1480     ;; Remember the directory name to save articles.
1481     (setq gnus-newsgroup-last-file filename)))
1482
1483 (defun gnus-summary-write-to-file (&optional filename)
1484   "Write this article to a file.
1485 Optional argument FILENAME specifies file name.
1486 The directory to save in defaults to `gnus-article-save-directory'."
1487   (interactive)
1488   (gnus-summary-save-in-file nil t))
1489
1490 (defun gnus-summary-save-body-in-file (&optional filename)
1491   "Append this article body to a file.
1492 Optional argument FILENAME specifies file name.
1493 The directory to save in defaults to `gnus-article-save-directory'."
1494   (interactive)
1495   (gnus-set-global-variables)
1496   (let ((default-name
1497           (funcall gnus-file-save-name gnus-newsgroup-name
1498                    gnus-current-headers gnus-newsgroup-last-file)))
1499     (setq filename (gnus-read-save-file-name
1500                     "Save %s body in file:" default-name filename))
1501     (gnus-make-directory (file-name-directory filename))
1502     (gnus-eval-in-buffer-window gnus-save-article-buffer
1503       (save-excursion
1504         (save-restriction
1505           (widen)
1506           (goto-char (point-min))
1507           (when (search-forward "\n\n" nil t)
1508             (narrow-to-region (point) (point-max)))
1509           (gnus-output-to-file filename))))
1510     ;; Remember the directory name to save articles.
1511     (setq gnus-newsgroup-last-file filename)))
1512
1513 (defun gnus-summary-save-in-pipe (&optional command)
1514   "Pipe this article to subprocess."
1515   (interactive)
1516   (gnus-set-global-variables)
1517   (setq command
1518         (cond ((eq command 'default)
1519                gnus-last-shell-command)
1520               (command command)
1521               (t (read-string 
1522                   (format
1523                    "Shell command on %s: "
1524                    (if (and gnus-number-of-articles-to-be-saved
1525                             (> gnus-number-of-articles-to-be-saved 1))
1526                        (format "these %d articles"
1527                                gnus-number-of-articles-to-be-saved)
1528                      "this article"))
1529                   gnus-last-shell-command))))
1530   (when (string-equal command "")
1531     (setq command gnus-last-shell-command))
1532   (gnus-eval-in-buffer-window gnus-article-buffer
1533     (save-restriction
1534       (widen)
1535       (shell-command-on-region (point-min) (point-max) command nil)))
1536   (setq gnus-last-shell-command command))
1537
1538 ;;; Article file names when saving.
1539
1540 (defun gnus-capitalize-newsgroup (newsgroup)
1541   "Capitalize NEWSGROUP name."
1542   (when (not (zerop (length newsgroup)))
1543     (concat (char-to-string (upcase (aref newsgroup 0)))
1544             (substring newsgroup 1))))
1545
1546 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1547   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1548 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
1549 Otherwise, it is like ~/News/news/group/num."
1550   (let ((default
1551           (expand-file-name
1552            (concat (if (gnus-use-long-file-name 'not-save)
1553                        (gnus-capitalize-newsgroup newsgroup)
1554                      (gnus-newsgroup-directory-form newsgroup))
1555                    "/" (int-to-string (mail-header-number headers)))
1556            gnus-article-save-directory)))
1557     (if (and last-file
1558              (string-equal (file-name-directory default)
1559                            (file-name-directory last-file))
1560              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1561         default
1562       (or last-file default))))
1563
1564 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1565   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1566 If variable `gnus-use-long-file-name' is non-nil, it is
1567 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
1568   (let ((default
1569           (expand-file-name
1570            (concat (if (gnus-use-long-file-name 'not-save)
1571                        newsgroup
1572                      (gnus-newsgroup-directory-form newsgroup))
1573                    "/" (int-to-string (mail-header-number headers)))
1574            gnus-article-save-directory)))
1575     (if (and last-file
1576              (string-equal (file-name-directory default)
1577                            (file-name-directory last-file))
1578              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1579         default
1580       (or last-file default))))
1581
1582 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1583   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1584 If variable `gnus-use-long-file-name' is non-nil, it is
1585 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
1586   (or last-file
1587       (expand-file-name
1588        (if (gnus-use-long-file-name 'not-save)
1589            (gnus-capitalize-newsgroup newsgroup)
1590          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1591        gnus-article-save-directory)))
1592
1593 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1594   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1595 If variable `gnus-use-long-file-name' is non-nil, it is
1596 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
1597   (or last-file
1598       (expand-file-name
1599        (if (gnus-use-long-file-name 'not-save)
1600            newsgroup
1601          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1602        gnus-article-save-directory)))
1603
1604 (eval-and-compile
1605   (mapcar
1606    (lambda (func)
1607      (let (afunc gfunc)
1608        (if (consp func)
1609            (setq afunc (car func)
1610                  gfunc (cdr func))
1611          (setq afunc func
1612                gfunc (intern (format "gnus-%s" func))))
1613        (fset gfunc 
1614              (if (not (fboundp afunc))
1615                  nil
1616                `(lambda (&optional interactive &rest args)
1617                   ,(documentation afunc t)
1618                   (interactive (list t))
1619                   (save-excursion
1620                     (set-buffer gnus-article-buffer)
1621                     (if interactive
1622                         (call-interactively ',afunc)
1623                       (apply ',afunc args))))))))
1624    '(article-hide-headers
1625      article-hide-boring-headers
1626      article-treat-overstrike
1627      (article-fill . gnus-article-word-wrap)
1628      article-remove-cr
1629      article-display-x-face
1630      article-de-quoted-unreadable
1631      article-mime-decode-quoted-printable
1632      article-hide-pgp
1633      article-hide-pem
1634      article-hide-signature
1635      article-remove-trailing-blank-lines
1636      article-strip-leading-blank-lines
1637      article-strip-multiple-blank-lines
1638      article-strip-blank-lines
1639      article-date-local
1640      article-date-original
1641      article-date-ut
1642      article-date-user
1643      article-date-lapsed
1644      article-emphasize
1645      (article-show-all . gnus-article-show-all-headers))))
1646 \f
1647 ;;;
1648 ;;; Gnus article mode
1649 ;;;
1650
1651 (put 'gnus-article-mode 'mode-class 'special)
1652
1653 (when t
1654   (gnus-define-keys gnus-article-mode-map
1655     " " gnus-article-goto-next-page
1656     "\177" gnus-article-goto-prev-page
1657     [delete] gnus-article-goto-prev-page
1658     "\C-c^" gnus-article-refer-article
1659     "h" gnus-article-show-summary
1660     "s" gnus-article-show-summary
1661     "\C-c\C-m" gnus-article-mail
1662     "?" gnus-article-describe-briefly
1663     gnus-mouse-2 gnus-article-push-button
1664     "\r" gnus-article-press-button
1665     "\t" gnus-article-next-button
1666     "\M-\t" gnus-article-prev-button
1667     "e" gnus-article-edit
1668     "<" beginning-of-buffer
1669     ">" end-of-buffer
1670     "\C-c\C-i" gnus-info-find-node
1671     "\C-c\C-b" gnus-bug
1672
1673     "\C-d" gnus-article-read-summary-keys
1674     "\M-*" gnus-article-read-summary-keys
1675     "\M-^" gnus-article-read-summary-keys
1676     "\M-g" gnus-article-read-summary-keys)
1677
1678   (substitute-key-definition
1679    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
1680
1681 (defun gnus-article-make-menu-bar ()
1682   (gnus-turn-off-edit-menu 'article)
1683   (unless (boundp 'gnus-article-article-menu)
1684     (easy-menu-define
1685      gnus-article-article-menu gnus-article-mode-map ""
1686      '("Article"
1687        ["Scroll forwards" gnus-article-goto-next-page t]
1688        ["Scroll backwards" gnus-article-goto-prev-page t]
1689        ["Show summary" gnus-article-show-summary t]
1690        ["Fetch Message-ID at point" gnus-article-refer-article t]
1691        ["Mail to address at point" gnus-article-mail t]))
1692
1693     (easy-menu-define
1694      gnus-article-treatment-menu gnus-article-mode-map ""
1695      '("Treatment"
1696        ["Hide headers" gnus-article-hide-headers t]
1697        ["Hide signature" gnus-article-hide-signature t]
1698        ["Hide citation" gnus-article-hide-citation t]
1699        ["Treat overstrike" gnus-article-treat-overstrike t]
1700        ["Remove carriage return" gnus-article-remove-cr t]
1701        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]))
1702
1703     (when (boundp 'gnus-summary-article-menu)
1704       (define-key gnus-article-mode-map [menu-bar commands]
1705         (cons "Commands" gnus-summary-article-menu)))
1706
1707     (when (boundp 'gnus-summary-post-menu)
1708       (define-key gnus-article-mode-map [menu-bar post]
1709         (cons "Post" gnus-summary-post-menu)))
1710
1711     (run-hooks 'gnus-article-menu-hook)))
1712
1713 (defun gnus-article-mode ()
1714   "Major mode for displaying an article.
1715
1716 All normal editing commands are switched off.
1717
1718 The following commands are available in addition to all summary mode
1719 commands:
1720 \\<gnus-article-mode-map>
1721 \\[gnus-article-next-page]\t Scroll the article one page forwards
1722 \\[gnus-article-prev-page]\t Scroll the article one page backwards
1723 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
1724 \\[gnus-article-show-summary]\t Display the summary buffer
1725 \\[gnus-article-mail]\t Send a reply to the address near point
1726 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
1727 \\[gnus-info-find-node]\t Go to the Gnus info node"
1728   (interactive)
1729   (when (gnus-visual-p 'article-menu 'menu)
1730     (gnus-article-make-menu-bar))
1731   (kill-all-local-variables)
1732   (gnus-simplify-mode-line)
1733   (setq mode-name "Article")
1734   (setq major-mode 'gnus-article-mode)
1735   (make-local-variable 'minor-mode-alist)
1736   (unless (assq 'gnus-show-mime minor-mode-alist)
1737     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
1738   (use-local-map gnus-article-mode-map)
1739   (gnus-update-format-specifications nil 'article-mode)
1740   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
1741   (gnus-set-default-directory)
1742   (buffer-disable-undo (current-buffer))
1743   (setq buffer-read-only t)
1744   (set-syntax-table gnus-article-mode-syntax-table)
1745   (run-hooks 'gnus-article-mode-hook))
1746
1747 (defun gnus-article-setup-buffer ()
1748   "Initialize the article buffer."
1749   (let* ((name (if gnus-single-article-buffer "*Article*"
1750                  (concat "*Article " gnus-newsgroup-name "*")))
1751          (original
1752           (progn (string-match "\\*Article" name)
1753                  (concat " *Original Article"
1754                          (substring name (match-end 0))))))
1755     (setq gnus-article-buffer name)
1756     (setq gnus-original-article-buffer original)
1757     ;; This might be a variable local to the summary buffer.
1758     (unless gnus-single-article-buffer
1759       (save-excursion
1760         (set-buffer gnus-summary-buffer)
1761         (setq gnus-article-buffer name)
1762         (setq gnus-original-article-buffer original)
1763         (gnus-set-global-variables)))
1764     ;; Init original article buffer.
1765     (save-excursion
1766       (set-buffer (get-buffer-create gnus-original-article-buffer))
1767       (buffer-disable-undo (current-buffer))
1768       (setq major-mode 'gnus-original-article-mode)
1769       (gnus-add-current-to-buffer-list)
1770       (make-local-variable 'gnus-original-article))
1771     (if (get-buffer name)
1772         (save-excursion
1773           (set-buffer name)
1774           (buffer-disable-undo (current-buffer))
1775           (setq buffer-read-only t)
1776           (gnus-add-current-to-buffer-list)
1777           (unless (eq major-mode 'gnus-article-mode)
1778             (gnus-article-mode))
1779           (current-buffer))
1780       (save-excursion
1781         (set-buffer (get-buffer-create name))
1782         (gnus-add-current-to-buffer-list)
1783         (gnus-article-mode)
1784         (make-local-variable 'gnus-summary-buffer)
1785         (current-buffer)))))
1786
1787 ;; Set article window start at LINE, where LINE is the number of lines
1788 ;; from the head of the article.
1789 (defun gnus-article-set-window-start (&optional line)
1790   (set-window-start
1791    (get-buffer-window gnus-article-buffer t)
1792    (save-excursion
1793      (set-buffer gnus-article-buffer)
1794      (goto-char (point-min))
1795      (if (not line)
1796          (point-min)
1797        (gnus-message 6 "Moved to bookmark")
1798        (search-forward "\n\n" nil t)
1799        (forward-line line)
1800        (point)))))
1801
1802 (defun gnus-article-prepare (article &optional all-headers header)
1803   "Prepare ARTICLE in article mode buffer.
1804 ARTICLE should either be an article number or a Message-ID.
1805 If ARTICLE is an id, HEADER should be the article headers.
1806 If ALL-HEADERS is non-nil, no headers are hidden."
1807   (save-excursion
1808     ;; Make sure we start in a summary buffer.
1809     (unless (eq major-mode 'gnus-summary-mode)
1810       (set-buffer gnus-summary-buffer))
1811     (setq gnus-summary-buffer (current-buffer))
1812     ;; Make sure the connection to the server is alive.
1813     (unless (gnus-server-opened
1814              (gnus-find-method-for-group gnus-newsgroup-name))
1815       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
1816       (gnus-request-group gnus-newsgroup-name t))
1817     (let* ((gnus-article (if header (mail-header-number header) article))
1818            (summary-buffer (current-buffer))
1819            (internal-hook gnus-article-internal-prepare-hook)
1820            (group gnus-newsgroup-name)
1821            result)
1822       (save-excursion
1823         (gnus-article-setup-buffer)
1824         (set-buffer gnus-article-buffer)
1825         ;; Deactivate active regions.
1826         (when (and (boundp 'transient-mark-mode)
1827                    transient-mark-mode)
1828           (setq mark-active nil))
1829         (if (not (setq result (let ((buffer-read-only nil))
1830                                 (gnus-request-article-this-buffer
1831                                  article group))))
1832             ;; There is no such article.
1833             (save-excursion
1834               (when (and (numberp article)
1835                          (not (memq article gnus-newsgroup-sparse)))
1836                 (setq gnus-article-current
1837                       (cons gnus-newsgroup-name article))
1838                 (set-buffer gnus-summary-buffer)
1839                 (setq gnus-current-article article)
1840                 (gnus-summary-mark-article article gnus-canceled-mark))
1841               (unless (memq article gnus-newsgroup-sparse)
1842                 (gnus-error
1843                  1 "No such article (may have expired or been canceled)")))
1844           (if (or (eq result 'pseudo) (eq result 'nneething))
1845               (progn
1846                 (save-excursion
1847                   (set-buffer summary-buffer)
1848                   (setq gnus-last-article gnus-current-article
1849                         gnus-newsgroup-history (cons gnus-current-article
1850                                                      gnus-newsgroup-history)
1851                         gnus-current-article 0
1852                         gnus-current-headers nil
1853                         gnus-article-current nil)
1854                   (if (eq result 'nneething)
1855                       (gnus-configure-windows 'summary)
1856                     (gnus-configure-windows 'article))
1857                   (gnus-set-global-variables))
1858                 (gnus-set-mode-line 'article))
1859             ;; The result from the `request' was an actual article -
1860             ;; or at least some text that is now displayed in the
1861             ;; article buffer.
1862             (when (and (numberp article)
1863                        (not (eq article gnus-current-article)))
1864               ;; Seems like a new article has been selected.
1865               ;; `gnus-current-article' must be an article number.
1866               (save-excursion
1867                 (set-buffer summary-buffer)
1868                 (setq gnus-last-article gnus-current-article
1869                       gnus-newsgroup-history (cons gnus-current-article
1870                                                    gnus-newsgroup-history)
1871                       gnus-current-article article
1872                       gnus-current-headers
1873                       (gnus-summary-article-header gnus-current-article)
1874                       gnus-article-current
1875                       (cons gnus-newsgroup-name gnus-current-article))
1876                 (unless (vectorp gnus-current-headers)
1877                   (setq gnus-current-headers nil))
1878                 (gnus-summary-show-thread)
1879                 (run-hooks 'gnus-mark-article-hook)
1880                 (gnus-set-mode-line 'summary)
1881                 (when (gnus-visual-p 'article-highlight 'highlight)
1882                   (run-hooks 'gnus-visual-mark-article-hook))
1883                 ;; Set the global newsgroup variables here.
1884                 ;; Suggested by Jim Sisolak
1885                 ;; <sisolak@trans4.neep.wisc.edu>.
1886                 (gnus-set-global-variables)
1887                 (setq gnus-have-all-headers
1888                       (or all-headers gnus-show-all-headers))
1889                 (and gnus-use-cache
1890                      (vectorp (gnus-summary-article-header article))
1891                      (gnus-cache-possibly-enter-article
1892                       group article
1893                       (gnus-summary-article-header article)
1894                       (memq article gnus-newsgroup-marked)
1895                       (memq article gnus-newsgroup-dormant)
1896                       (memq article gnus-newsgroup-unreads)))))
1897             (when (or (numberp article)
1898                       (stringp article))
1899               ;; Hooks for getting information from the article.
1900               ;; This hook must be called before being narrowed.
1901               (let (buffer-read-only)
1902                 (run-hooks 'internal-hook)
1903                 (run-hooks 'gnus-article-prepare-hook)
1904                 ;; Decode MIME message.
1905                 (when gnus-show-mime
1906                   (if (or (not gnus-strict-mime)
1907                           (gnus-fetch-field "Mime-Version"))
1908                       (funcall gnus-show-mime-method)
1909                     (funcall gnus-decode-encoded-word-method)))
1910                 ;; Perform the article display hooks.
1911                 (run-hooks 'gnus-article-display-hook))
1912               ;; Do page break.
1913               (goto-char (point-min))
1914               (when gnus-break-pages
1915                 (gnus-narrow-to-page)))
1916             (gnus-set-mode-line 'article)
1917             (gnus-configure-windows 'article)
1918             (goto-char (point-min))
1919             t))))))
1920
1921 (defun gnus-article-wash-status ()
1922   "Return a string which display status of article washing."
1923   (save-excursion
1924     (set-buffer gnus-article-buffer)
1925     (let ((cite (gnus-article-hidden-text-p 'cite))
1926           (headers (gnus-article-hidden-text-p 'headers))
1927           (boring (gnus-article-hidden-text-p 'boring-headers))
1928           (pgp (gnus-article-hidden-text-p 'pgp))
1929           (pem (gnus-article-hidden-text-p 'pem))
1930           (signature (gnus-article-hidden-text-p 'signature))
1931           (overstrike (gnus-article-hidden-text-p 'overstrike))
1932           (emphasis (gnus-article-hidden-text-p 'emphasis))
1933           (mime gnus-show-mime))
1934       (format "%c%c%c%c%c%c%c"
1935               (if cite ?c ? )
1936               (if (or headers boring) ?h ? )
1937               (if (or pgp pem) ?p ? )
1938               (if signature ?s ? )
1939               (if overstrike ?o ? )
1940               (if mime ?m ? )
1941               (if emphasis ?e ? )))))
1942
1943 (defun gnus-article-hide-headers-if-wanted ()
1944   "Hide unwanted headers if `gnus-have-all-headers' is nil.
1945 Provided for backwards compatibility."
1946   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
1947       gnus-inhibit-hiding
1948       (gnus-article-hide-headers)))
1949
1950 ;;; Article savers.
1951
1952 (defun gnus-output-to-rmail (file-name)
1953   "Append the current article to an Rmail file named FILE-NAME."
1954   (require 'rmail)
1955   ;; Most of these codes are borrowed from rmailout.el.
1956   (setq file-name (expand-file-name file-name))
1957   (setq rmail-default-rmail-file file-name)
1958   (let ((artbuf (current-buffer))
1959         (tmpbuf (get-buffer-create " *Gnus-output*")))
1960     (save-excursion
1961       (or (get-file-buffer file-name)
1962           (file-exists-p file-name)
1963           (if (gnus-yes-or-no-p
1964                (concat "\"" file-name "\" does not exist, create it? "))
1965               (let ((file-buffer (create-file-buffer file-name)))
1966                 (save-excursion
1967                   (set-buffer file-buffer)
1968                   (rmail-insert-rmail-file-header)
1969                   (let ((require-final-newline nil))
1970                     (gnus-write-buffer file-name)))
1971                 (kill-buffer file-buffer))
1972             (error "Output file does not exist")))
1973       (set-buffer tmpbuf)
1974       (buffer-disable-undo (current-buffer))
1975       (erase-buffer)
1976       (insert-buffer-substring artbuf)
1977       (gnus-convert-article-to-rmail)
1978       ;; Decide whether to append to a file or to an Emacs buffer.
1979       (let ((outbuf (get-file-buffer file-name)))
1980         (if (not outbuf)
1981             (append-to-file (point-min) (point-max) file-name)
1982           ;; File has been visited, in buffer OUTBUF.
1983           (set-buffer outbuf)
1984           (let ((buffer-read-only nil)
1985                 (msg (and (boundp 'rmail-current-message)
1986                           (symbol-value 'rmail-current-message))))
1987             ;; If MSG is non-nil, buffer is in RMAIL mode.
1988             (when msg
1989               (widen)
1990               (narrow-to-region (point-max) (point-max)))
1991             (insert-buffer-substring tmpbuf)
1992             (when msg
1993               (goto-char (point-min))
1994               (widen)
1995               (search-backward "\^_")
1996               (narrow-to-region (point) (point-max))
1997               (goto-char (1+ (point-min)))
1998               (rmail-count-new-messages t)
1999               (rmail-show-message msg))))))
2000     (kill-buffer tmpbuf)))
2001
2002 (defun gnus-output-to-file (file-name)
2003   "Append the current article to a file named FILE-NAME."
2004   (let ((artbuf (current-buffer)))
2005     (nnheader-temp-write nil
2006       (insert-buffer-substring artbuf)
2007       ;; Append newline at end of the buffer as separator, and then
2008       ;; save it to file.
2009       (goto-char (point-max))
2010       (insert "\n")
2011       (append-to-file (point-min) (point-max) file-name))))
2012
2013 (defun gnus-convert-article-to-rmail ()
2014   "Convert article in current buffer to Rmail message format."
2015   (let ((buffer-read-only nil))
2016     ;; Convert article directly into Babyl format.
2017     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
2018     (goto-char (point-min))
2019     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
2020     (while (search-forward "\n\^_" nil t) ;single char
2021       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
2022     (goto-char (point-max))
2023     (insert "\^_")))
2024
2025 (defun gnus-narrow-to-page (&optional arg)
2026   "Narrow the article buffer to a page.
2027 If given a numerical ARG, move forward ARG pages."
2028   (interactive "P")
2029   (setq arg (if arg (prefix-numeric-value arg) 0))
2030   (save-excursion
2031     (set-buffer gnus-article-buffer)
2032     (goto-char (point-min))
2033     (widen)
2034     ;; Remove any old next/prev buttons.
2035     (when (gnus-visual-p 'page-marker)
2036       (let ((buffer-read-only nil))
2037         (gnus-remove-text-with-property 'gnus-prev)
2038         (gnus-remove-text-with-property 'gnus-next)))
2039     (when
2040         (cond ((< arg 0)
2041                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
2042               ((> arg 0)
2043                (re-search-forward page-delimiter nil 'move arg)))
2044       (goto-char (match-end 0)))
2045     (narrow-to-region
2046      (point)
2047      (if (re-search-forward page-delimiter nil 'move)
2048          (match-beginning 0)
2049        (point)))
2050     (when (and (gnus-visual-p 'page-marker)
2051                (not (= (point-min) 1)))
2052       (save-excursion
2053         (goto-char (point-min))
2054         (gnus-insert-prev-page-button)))
2055     (when (and (gnus-visual-p 'page-marker)
2056                (< (+ (point-max) 2) (buffer-size)))
2057       (save-excursion
2058         (goto-char (point-max))
2059         (gnus-insert-next-page-button)))))
2060
2061 ;; Article mode commands
2062
2063 (defun gnus-article-goto-next-page ()
2064   "Show the next page of the article."
2065   (interactive)
2066   (when (gnus-article-next-page)
2067     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
2068
2069 (defun gnus-article-goto-prev-page ()
2070   "Show the next page of the article."
2071   (interactive)
2072   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
2073     (gnus-article-prev-page nil)))
2074
2075 (defun gnus-article-next-page (&optional lines)
2076   "Show the next page of the current article.
2077 If end of article, return non-nil.  Otherwise return nil.
2078 Argument LINES specifies lines to be scrolled up."
2079   (interactive "p")
2080   (move-to-window-line -1)
2081   (if (save-excursion
2082         (end-of-line)
2083         (and (pos-visible-in-window-p)  ;Not continuation line.
2084              (eobp)))
2085       ;; Nothing in this page.
2086       (if (or (not gnus-break-pages)
2087               (save-excursion
2088                 (save-restriction
2089                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
2090           t                             ;Nothing more.
2091         (gnus-narrow-to-page 1)         ;Go to next page.
2092         nil)
2093     ;; More in this page.
2094     (let ((scroll-in-place nil))
2095       (condition-case ()
2096           (scroll-up lines)
2097         (end-of-buffer
2098          ;; Long lines may cause an end-of-buffer error.
2099          (goto-char (point-max)))))
2100     (move-to-window-line 0)
2101     nil))
2102
2103 (defun gnus-article-prev-page (&optional lines)
2104   "Show previous page of current article.
2105 Argument LINES specifies lines to be scrolled down."
2106   (interactive "p")
2107   (move-to-window-line 0)
2108   (if (and gnus-break-pages
2109            (bobp)
2110            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
2111       (progn
2112         (gnus-narrow-to-page -1)        ;Go to previous page.
2113         (goto-char (point-max))
2114         (recenter -1))
2115     (let ((scroll-in-place nil))
2116       (prog1
2117           (ignore-errors
2118             (scroll-down lines))
2119         (move-to-window-line 0)))))
2120
2121 (defun gnus-article-refer-article ()
2122   "Read article specified by message-id around point."
2123   (interactive)
2124   (let ((point (point)))
2125     (search-forward ">" nil t)          ;Move point to end of "<....>".
2126     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
2127         (let ((message-id (match-string 1)))
2128           (goto-char point)
2129           (set-buffer gnus-summary-buffer)
2130           (gnus-summary-refer-article message-id))
2131       (goto-char (point))
2132       (error "No references around point"))))
2133
2134 (defun gnus-article-show-summary ()
2135   "Reconfigure windows to show summary buffer."
2136   (interactive)
2137   (gnus-configure-windows 'article)
2138   (gnus-summary-goto-subject gnus-current-article))
2139
2140 (defun gnus-article-describe-briefly ()
2141   "Describe article mode commands briefly."
2142   (interactive)
2143   (gnus-message 6
2144                 (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")))
2145
2146 (defun gnus-article-summary-command ()
2147   "Execute the last keystroke in the summary buffer."
2148   (interactive)
2149   (let ((obuf (current-buffer))
2150         (owin (current-window-configuration))
2151         func)
2152     (switch-to-buffer gnus-summary-buffer 'norecord)
2153     (setq func (lookup-key (current-local-map) (this-command-keys)))
2154     (call-interactively func)
2155     (set-buffer obuf)
2156     (set-window-configuration owin)
2157     (set-window-point (get-buffer-window (current-buffer)) (point))))
2158
2159 (defun gnus-article-summary-command-nosave ()
2160   "Execute the last keystroke in the summary buffer."
2161   (interactive)
2162   (let (func)
2163     (pop-to-buffer gnus-summary-buffer 'norecord)
2164     (setq func (lookup-key (current-local-map) (this-command-keys)))
2165     (call-interactively func)))
2166
2167 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
2168   "Read a summary buffer key sequence and execute it from the article buffer."
2169   (interactive "P")
2170   (let ((nosaves
2171          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
2172            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
2173            "=" "^" "\M-^" "|"))
2174         (nosave-but-article
2175          '("A\r"))
2176         (nosave-in-article
2177          '("\C-d"))
2178         keys)
2179     (save-excursion
2180       (set-buffer gnus-summary-buffer)
2181       (let (gnus-pick-mode)
2182         (push (or key last-command-event) unread-command-events)
2183         (setq keys (read-key-sequence nil))))
2184     (message "")
2185
2186     (if (or (member keys nosaves)
2187             (member keys nosave-but-article)
2188             (member keys nosave-in-article))
2189         (let (func)
2190           (save-window-excursion
2191             (pop-to-buffer gnus-summary-buffer 'norecord)
2192             ;; We disable the pick minor mode commands.
2193             (let (gnus-pick-mode)
2194               (setq func (lookup-key (current-local-map) keys))))
2195           (if (not func)
2196               (ding)
2197             (unless (member keys nosave-in-article)
2198               (set-buffer gnus-summary-buffer))
2199             (call-interactively func))
2200           (when (member keys nosave-but-article)
2201             (pop-to-buffer gnus-article-buffer 'norecord)))
2202       ;; These commands should restore window configuration.
2203       (let ((obuf (current-buffer))
2204             (owin (current-window-configuration))
2205             (opoint (point))
2206             func in-buffer)
2207         (if not-restore-window
2208             (pop-to-buffer gnus-summary-buffer 'norecord)
2209           (switch-to-buffer gnus-summary-buffer 'norecord))
2210         (setq in-buffer (current-buffer))
2211         ;; We disable the pick minor mode commands.
2212         (if (setq func (let (gnus-pick-mode)
2213                          (lookup-key (current-local-map) keys)))
2214             (call-interactively func)
2215           (ding))
2216         (when (eq in-buffer (current-buffer))
2217           (set-buffer obuf)
2218           (unless not-restore-window
2219             (set-window-configuration owin))
2220           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
2221
2222 (defun gnus-article-hide (&optional arg force)
2223   "Hide all the gruft in the current article.
2224 This means that PGP stuff, signatures, cited text and (some)
2225 headers will be hidden.
2226 If given a prefix, show the hidden text instead."
2227   (interactive (list current-prefix-arg 'force))
2228   (gnus-article-hide-headers arg)
2229   (gnus-article-hide-pgp arg)
2230   (gnus-article-hide-citation-maybe arg force)
2231   (gnus-article-hide-signature arg))
2232
2233 (defun gnus-article-maybe-highlight ()
2234   "Do some article highlighting if `article-visual' is non-nil."
2235   (when (gnus-visual-p 'article-highlight 'highlight)
2236     (gnus-article-highlight-some)))
2237
2238 (defun gnus-request-article-this-buffer (article group)
2239   "Get an article and insert it into this buffer."
2240   (let (do-update-line)
2241     (prog1
2242         (save-excursion
2243           (erase-buffer)
2244           (gnus-kill-all-overlays)
2245           (setq group (or group gnus-newsgroup-name))
2246
2247           ;; Open server if it has closed.
2248           (gnus-check-server (gnus-find-method-for-group group))
2249
2250           ;; Using `gnus-request-article' directly will insert the article into
2251           ;; `nntp-server-buffer' - so we'll save some time by not having to
2252           ;; copy it from the server buffer into the article buffer.
2253
2254           ;; We only request an article by message-id when we do not have the
2255           ;; headers for it, so we'll have to get those.
2256           (when (stringp article)
2257             (let ((gnus-override-method gnus-refer-article-method))
2258               (gnus-read-header article)))
2259
2260           ;; If the article number is negative, that means that this article
2261           ;; doesn't belong in this newsgroup (possibly), so we find its
2262           ;; message-id and request it by id instead of number.
2263           (when (and (numberp article)
2264                      gnus-summary-buffer
2265                      (get-buffer gnus-summary-buffer)
2266                      (buffer-name (get-buffer gnus-summary-buffer)))
2267             (save-excursion
2268               (set-buffer gnus-summary-buffer)
2269               (let ((header (gnus-summary-article-header article)))
2270                 (when (< article 0)
2271                   (cond 
2272                    ((memq article gnus-newsgroup-sparse)
2273                     ;; This is a sparse gap article.
2274                     (setq do-update-line article)
2275                     (setq article (mail-header-id header))
2276                     (let ((gnus-override-method gnus-refer-article-method))
2277                       (gnus-read-header article))
2278                     (setq gnus-newsgroup-sparse
2279                           (delq article gnus-newsgroup-sparse)))
2280                    ((vectorp header)
2281                     ;; It's a real article.
2282                     (setq article (mail-header-id header)))
2283                    (t
2284                     ;; It is an extracted pseudo-article.
2285                     (setq article 'pseudo)
2286                     (gnus-request-pseudo-article header))))
2287                 
2288                 (let ((method (gnus-find-method-for-group 
2289                                gnus-newsgroup-name)))
2290                   (if (not (eq (car method) 'nneething))
2291                       ()
2292                     (let ((dir (concat (file-name-as-directory (nth 1 method))
2293                                        (mail-header-subject header))))
2294                       (when (file-directory-p dir)
2295                         (setq article 'nneething)
2296                         (gnus-group-enter-directory dir))))))))
2297
2298           (cond
2299            ;; Refuse to select canceled articles.
2300            ((and (numberp article)
2301                  gnus-summary-buffer
2302                  (get-buffer gnus-summary-buffer)
2303                  (buffer-name (get-buffer gnus-summary-buffer))
2304                  (eq (cdr (save-excursion
2305                             (set-buffer gnus-summary-buffer)
2306                             (assq article gnus-newsgroup-reads)))
2307                      gnus-canceled-mark))
2308             nil)
2309            ;; We first check `gnus-original-article-buffer'.
2310            ((and (get-buffer gnus-original-article-buffer)
2311                  (numberp article)
2312                  (save-excursion
2313                    (set-buffer gnus-original-article-buffer)
2314                    (and (equal (car gnus-original-article) group)
2315                         (eq (cdr gnus-original-article) article))))
2316             (insert-buffer-substring gnus-original-article-buffer)
2317             'article)
2318            ;; Check the backlog.
2319            ((and gnus-keep-backlog
2320                  (gnus-backlog-request-article group article (current-buffer)))
2321             'article)
2322            ;; Check asynchronous pre-fetch.
2323            ((gnus-async-request-fetched-article group article (current-buffer))
2324             (gnus-async-prefetch-next group article gnus-summary-buffer)
2325             'article)
2326            ;; Check the cache.
2327            ((and gnus-use-cache
2328                  (numberp article)
2329                  (gnus-cache-request-article article group))
2330             'article)
2331            ;; Get the article and put into the article buffer.
2332            ((or (stringp article) (numberp article))
2333             (let ((gnus-override-method
2334                    (and (stringp article) gnus-refer-article-method))
2335                   (buffer-read-only nil))
2336               (erase-buffer)
2337               (gnus-kill-all-overlays)
2338               (when (gnus-request-article article group (current-buffer))
2339                 (when (numberp article)
2340                   (gnus-async-prefetch-next group article gnus-summary-buffer)
2341                   (when gnus-keep-backlog
2342                     (gnus-backlog-enter-article 
2343                      group article (current-buffer))))
2344                 'article)))
2345            ;; It was a pseudo.
2346            (t article)))
2347
2348       ;; Take the article from the original article buffer
2349       ;; and place it in the buffer it's supposed to be in.
2350       (when (and (get-buffer gnus-article-buffer)
2351                  ;;(numberp article)
2352                  (equal (buffer-name (current-buffer))
2353                         (buffer-name (get-buffer gnus-article-buffer))))
2354         (save-excursion
2355           (if (get-buffer gnus-original-article-buffer)
2356               (set-buffer (get-buffer gnus-original-article-buffer))
2357             (set-buffer (get-buffer-create gnus-original-article-buffer))
2358             (buffer-disable-undo (current-buffer))
2359             (setq major-mode 'gnus-original-article-mode)
2360             (setq buffer-read-only t)
2361             (gnus-add-current-to-buffer-list))
2362           (let (buffer-read-only)
2363             (erase-buffer)
2364             (insert-buffer-substring gnus-article-buffer))
2365           (setq gnus-original-article (cons group article))))
2366     
2367       ;; Update sparse articles.
2368       (when (and do-update-line
2369                  (or (numberp article)
2370                      (stringp article)))
2371         (let ((buf (current-buffer)))
2372           (set-buffer gnus-summary-buffer)
2373           (gnus-summary-update-article do-update-line)
2374           (gnus-summary-goto-subject do-update-line nil t)
2375           (set-window-point (get-buffer-window (current-buffer) t)
2376                             (point))
2377           (set-buffer buf))))))
2378
2379 ;;;
2380 ;;; Article editing
2381 ;;;
2382
2383 (defcustom gnus-article-edit-mode-hook nil
2384   "Hook run in article edit mode buffers."
2385   :group 'gnus-article-various
2386   :type 'hook)
2387
2388 (defvar gnus-article-edit-done-function nil)
2389
2390 (defvar gnus-article-edit-mode-map nil)
2391
2392 (unless gnus-article-edit-mode-map 
2393   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
2394
2395   (gnus-define-keys gnus-article-edit-mode-map
2396     "\C-c\C-c" gnus-article-edit-done
2397     "\C-c\C-k" gnus-article-edit-exit)
2398
2399   (gnus-define-keys (gnus-article-edit-wash-map
2400                      "\C-c\C-w" gnus-article-edit-mode-map)
2401     "f" gnus-article-edit-full-stops))
2402
2403 (defun gnus-article-edit-mode ()
2404   "Major mode for editing articles.
2405 This is an extended text-mode.
2406
2407 \\{gnus-article-edit-mode-map}"
2408   (interactive)
2409   (kill-all-local-variables)
2410   (setq major-mode 'gnus-article-edit-mode)
2411   (setq mode-name "Article Edit")
2412   (use-local-map gnus-article-edit-mode-map)
2413   (make-local-variable 'gnus-article-edit-done-function)
2414   (make-local-variable 'gnus-prev-winconf)
2415   (setq buffer-read-only nil)
2416   (buffer-enable-undo)
2417   (widen)
2418   (run-hooks 'text-mode 'gnus-article-edit-mode-hook))
2419
2420 (defun gnus-article-edit (&optional force)
2421   "Edit the current article.
2422 This will have permanent effect only in mail groups.
2423 If FORCE is non-nil, allow editing of articles even in read-only
2424 groups."
2425   (interactive "P")
2426   (when (and (not force)
2427              (gnus-group-read-only-p))
2428     (error "The current newsgroup does not support article editing."))
2429   (gnus-article-edit-article
2430    `(lambda ()
2431       (gnus-summary-edit-article-done
2432        ,(or (mail-header-references gnus-current-headers) "")
2433        ,(gnus-group-read-only-p) ,gnus-summary-buffer))))
2434
2435 (defun gnus-article-edit-article (exit-func)
2436   "Start editing the contents of the current article buffer."
2437   (let ((winconf (current-window-configuration)))
2438     (set-buffer gnus-article-buffer)
2439     (gnus-article-edit-mode)
2440     (set-text-properties (point-min) (point-max) nil)
2441     (gnus-configure-windows 'edit-article)
2442     (setq gnus-article-edit-done-function exit-func)
2443     (setq gnus-prev-winconf winconf)
2444     (gnus-message 6 "C-c C-c to end edits")))
2445
2446 (defun gnus-article-edit-done ()
2447   "Update the article edits and exit."
2448   (interactive)
2449   (let ((func gnus-article-edit-done-function)
2450         (buf (current-buffer))
2451         (start (window-start)))
2452     (gnus-article-edit-exit)
2453     (save-excursion
2454       (set-buffer buf)
2455       (let ((buffer-read-only nil))
2456         (funcall func)))
2457     (set-buffer buf)
2458     (set-window-start (get-buffer-window buf) start)
2459     (set-window-point (get-buffer-window buf) (point))))
2460
2461 (defun gnus-article-edit-exit ()
2462   "Exit the article editing without updating."
2463   (interactive)
2464   ;; We remove all text props from the article buffer.
2465   (let ((buf (format "%s" (buffer-string)))
2466         (curbuf (current-buffer))
2467         (p (point))
2468         (window-start (window-start)))
2469     (erase-buffer)
2470     (insert buf)
2471     (let ((winconf gnus-prev-winconf))
2472       (gnus-article-mode)
2473       ;; The cache and backlog have to be flushed somewhat.
2474       (when gnus-use-cache
2475         (gnus-cache-update-article      
2476          (car gnus-article-current) (cdr gnus-article-current)))
2477       (when gnus-keep-backlog
2478         (gnus-backlog-remove-article 
2479          (car gnus-article-current) (cdr gnus-article-current)))
2480       ;; Flush original article as well.
2481       (save-excursion
2482         (when (get-buffer gnus-original-article-buffer)
2483           (set-buffer gnus-original-article-buffer)
2484           (setq gnus-original-article nil)))
2485       (set-window-configuration winconf)
2486       ;; Tippy-toe some to make sure that point remains where it was.
2487       (let ((buf (current-buffer)))
2488         (set-buffer curbuf)
2489         (set-window-start (get-buffer-window (current-buffer)) window-start)
2490         (goto-char p)
2491         (set-buffer buf)))))
2492       
2493 (defun gnus-article-edit-full-stops ()
2494   "Interactively repair spacing at end of sentences."
2495   (interactive)
2496   (save-excursion
2497     (goto-char (point-min))
2498     (search-forward-regexp "^$" nil t)
2499     (let ((case-fold-search nil))
2500       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
2501
2502 ;;; 
2503 ;;; Article highlights
2504 ;;;
2505
2506 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
2507
2508 ;;; Internal Variables:
2509
2510 (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\\)"
2511   "Regular expression that matches URLs."
2512   :group 'gnus-article-buttons
2513   :type 'regexp)
2514
2515 (defcustom gnus-button-alist 
2516   `(("\\(\\b<?\\(url: ?\\)?news:\\([^>\n\t ]*\\)>?\\)" 1 t
2517      gnus-button-message-id 3)
2518     ("\\(\\b<?\\(url: ?\\)?news:\\(//\\)?\\([^>\n\t ]*\\)>?\\)" 1 t
2519      gnus-button-fetch-group 4)
2520     ("\\bin\\( +article\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2 
2521      t gnus-button-message-id 3)
2522     ("\\(<URL: *\\)?mailto: *\\([^> \n\t]+\\)>?" 0 t gnus-url-mailto 2)
2523     ;; This is how URLs _should_ be embedded in text...
2524     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
2525     ;; Raw URLs.
2526     (,gnus-button-url-regexp 0 t gnus-button-url 0))
2527   "Alist of regexps matching buttons in article bodies.
2528
2529 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
2530 REGEXP: is the string matching text around the button,
2531 BUTTON: is the number of the regexp grouping actually matching the button,
2532 FORM: is a lisp expression which must eval to true for the button to
2533 be added, 
2534 CALLBACK: is the function to call when the user push this button, and each
2535 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
2536
2537 CALLBACK can also be a variable, in that case the value of that
2538 variable it the real callback function."
2539   :group 'gnus-article-buttons
2540   :type '(repeat (list regexp 
2541                        (integer :tag "Button")
2542                        (sexp :tag "Form")
2543                        (function :tag "Callback")
2544                        (repeat :tag "Par"
2545                                :inline t
2546                                (integer :tag "Regexp group")))))
2547
2548 (defcustom gnus-header-button-alist 
2549   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
2550      0 t gnus-button-message-id 0)
2551     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
2552     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+" 
2553      0 t gnus-button-mailto 0)
2554     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2555     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2556     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
2557      gnus-button-message-id 3))
2558   "Alist of headers and regexps to match buttons in article heads.
2559
2560 This alist is very similar to `gnus-button-alist', except that each
2561 alist has an additional HEADER element first in each entry:
2562
2563 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
2564
2565 HEADER is a regexp to match a header.  For a fuller explanation, see
2566 `gnus-button-alist'."
2567   :group 'gnus-article-buttons
2568   :group 'gnus-article-headers
2569   :type '(repeat (list (regexp :tag "Header")
2570                        regexp 
2571                        (integer :tag "Button")
2572                        (sexp :tag "Form")
2573                        (function :tag "Callback")
2574                        (repeat :tag "Par"
2575                                :inline t
2576                                (integer :tag "Regexp group")))))
2577
2578 (defvar gnus-button-regexp nil)
2579 (defvar gnus-button-marker-list nil)
2580 ;; Regexp matching any of the regexps from `gnus-button-alist'.
2581
2582 (defvar gnus-button-last nil)
2583 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
2584
2585 ;;; Commands:
2586
2587 (defun gnus-article-push-button (event)
2588   "Check text under the mouse pointer for a callback function.
2589 If the text under the mouse pointer has a `gnus-callback' property,
2590 call it with the value of the `gnus-data' text property."
2591   (interactive "e")
2592   (set-buffer (window-buffer (posn-window (event-start event))))
2593   (let* ((pos (posn-point (event-start event)))
2594          (data (get-text-property pos 'gnus-data))
2595          (fun (get-text-property pos 'gnus-callback)))
2596     (when fun
2597       (funcall fun data))))
2598
2599 (defun gnus-article-press-button ()
2600   "Check text at point for a callback function.
2601 If the text at point has a `gnus-callback' property,
2602 call it with the value of the `gnus-data' text property."
2603   (interactive)
2604   (let* ((data (get-text-property (point) 'gnus-data))
2605          (fun (get-text-property (point) 'gnus-callback)))
2606     (when fun
2607       (funcall fun data))))
2608
2609 (defun gnus-article-prev-button (n)
2610   "Move point to N buttons backward.
2611 If N is negative, move forward instead."
2612   (interactive "p")
2613   (gnus-article-next-button (- n)))
2614
2615 (defun gnus-article-next-button (n)
2616   "Move point to N buttons forward.
2617 If N is negative, move backward instead."
2618   (interactive "p")
2619   (let ((function (if (< n 0) 'previous-single-property-change
2620                     'next-single-property-change))
2621         (inhibit-point-motion-hooks t)
2622         (backward (< n 0))
2623         (limit (if (< n 0) (point-min) (point-max))))
2624     (setq n (abs n))
2625     (while (and (not (= limit (point)))
2626                 (> n 0))
2627       ;; Skip past the current button.
2628       (when (get-text-property (point) 'gnus-callback)
2629         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2630       ;; Go to the next (or previous) button.
2631       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
2632       ;; Put point at the start of the button.
2633       (when (and backward (not (get-text-property (point) 'gnus-callback)))
2634         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2635       ;; Skip past intangible buttons.
2636       (when (get-text-property (point) 'intangible)
2637         (incf n))
2638       (decf n))
2639     (unless (zerop n)
2640       (gnus-message 5 "No more buttons"))
2641     n))
2642
2643 (defun gnus-article-highlight (&optional force)
2644   "Highlight current article.
2645 This function calls `gnus-article-highlight-headers',
2646 `gnus-article-highlight-citation', 
2647 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2648 do the highlighting.  See the documentation for those functions."
2649   (interactive (list 'force))
2650   (gnus-article-highlight-headers)
2651   (gnus-article-highlight-citation force)
2652   (gnus-article-highlight-signature)
2653   (gnus-article-add-buttons force)
2654   (gnus-article-add-buttons-to-head))
2655
2656 (defun gnus-article-highlight-some (&optional force)
2657   "Highlight current article.
2658 This function calls `gnus-article-highlight-headers',
2659 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2660 do the highlighting.  See the documentation for those functions."
2661   (interactive (list 'force))
2662   (gnus-article-highlight-headers)
2663   (gnus-article-highlight-signature)
2664   (gnus-article-add-buttons))
2665
2666 (defun gnus-article-highlight-headers ()
2667   "Highlight article headers as specified by `gnus-header-face-alist'."
2668   (interactive)
2669   (save-excursion
2670     (set-buffer gnus-article-buffer)
2671     (save-restriction
2672       (let ((alist gnus-header-face-alist)
2673             (buffer-read-only nil)
2674             (case-fold-search t)
2675             (inhibit-point-motion-hooks t)
2676             entry regexp header-face field-face from hpoints fpoints)
2677         (goto-char (point-min))
2678         (when (search-forward "\n\n" nil t)
2679           (narrow-to-region (1- (point)) (point-min))
2680           (while (setq entry (pop alist))
2681             (goto-char (point-min))
2682             (setq regexp (concat "^\\("
2683                                  (if (string-equal "" (nth 0 entry))
2684                                      "[^\t ]"
2685                                    (nth 0 entry))
2686                                  "\\)")
2687                   header-face (nth 1 entry)
2688                   field-face (nth 2 entry))
2689             (while (and (re-search-forward regexp nil t)
2690                         (not (eobp)))
2691               (beginning-of-line)
2692               (setq from (point))
2693               (unless (search-forward ":" nil t)
2694                 (forward-char 1))
2695               (when (and header-face
2696                          (not (memq (point) hpoints)))
2697                 (push (point) hpoints)
2698                 (gnus-put-text-property from (point) 'face header-face))
2699               (when (and field-face
2700                          (not (memq (setq from (point)) fpoints)))
2701                 (push from fpoints)
2702                 (if (re-search-forward "^[^ \t]" nil t)
2703                     (forward-char -2)
2704                   (goto-char (point-max)))
2705                 (gnus-put-text-property from (point) 'face field-face)))))))))
2706
2707 (defun gnus-article-highlight-signature ()
2708   "Highlight the signature in an article.
2709 It does this by highlighting everything after
2710 `gnus-signature-separator' using `gnus-signature-face'." 
2711   (interactive)
2712   (save-excursion
2713     (set-buffer gnus-article-buffer)
2714     (let ((buffer-read-only nil)
2715           (inhibit-point-motion-hooks t))
2716       (save-restriction
2717         (when (and gnus-signature-face
2718                    (gnus-article-narrow-to-signature))
2719           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
2720                             'face gnus-signature-face)
2721           (widen)
2722           (gnus-article-search-signature)
2723           (let ((start (match-beginning 0))
2724                 (end (set-marker (make-marker) (1+ (match-end 0)))))
2725             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
2726                                      end)))))))
2727
2728 (defun gnus-article-add-buttons (&optional force)
2729   "Find external references in the article and make buttons of them.
2730 \"External references\" are things like Message-IDs and URLs, as
2731 specified by `gnus-button-alist'."
2732   (interactive (list 'force))
2733   (save-excursion
2734     (set-buffer gnus-article-buffer)
2735     ;; Remove all old markers.
2736     (while gnus-button-marker-list
2737       (set-marker (pop gnus-button-marker-list) nil))
2738     (let ((buffer-read-only nil)
2739           (inhibit-point-motion-hooks t)
2740           (case-fold-search t)
2741           (alist gnus-button-alist)
2742           beg entry regexp)
2743       (goto-char (point-min))
2744       ;; We skip the headers.
2745       (unless (search-forward "\n\n" nil t)
2746         (goto-char (point-max)))
2747       (setq beg (point))
2748       (while (setq entry (pop alist))
2749         (setq regexp (car entry))
2750         (goto-char beg)
2751         (while (re-search-forward regexp nil t)
2752           (let* ((start (and entry (match-beginning (nth 1 entry))))
2753                  (end (and entry (match-end (nth 1 entry))))
2754                  (from (match-beginning 0)))
2755             (when (and (or (eq t (nth 1 entry))
2756                            (eval (nth 1 entry)))
2757                        (not (get-text-property (point) 'gnus-callback)))
2758               ;; That optional form returned non-nil, so we add the
2759               ;; button. 
2760               (gnus-article-add-button 
2761                start end 'gnus-button-push 
2762                (car (push (set-marker (make-marker) from)
2763                           gnus-button-marker-list))))))))))
2764
2765 ;; Add buttons to the head of an article.
2766 (defun gnus-article-add-buttons-to-head ()
2767   "Add buttons to the head of the article."
2768   (interactive)
2769   (save-excursion
2770     (set-buffer gnus-article-buffer)
2771     (let ((buffer-read-only nil)
2772           (inhibit-point-motion-hooks t)
2773           (case-fold-search t)
2774           (alist gnus-header-button-alist)
2775           entry beg end)
2776       (nnheader-narrow-to-headers)
2777       (while alist
2778         ;; Each alist entry.
2779         (setq entry (car alist)
2780               alist (cdr alist))
2781         (goto-char (point-min))
2782         (while (re-search-forward (car entry) nil t)
2783           ;; Each header matching the entry.
2784           (setq beg (match-beginning 0))
2785           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
2786                              (match-beginning 0))
2787                         (point-max)))
2788           (goto-char beg)
2789           (while (re-search-forward (nth 1 entry) end t)
2790             ;; Each match within a header.
2791             (let* ((entry (cdr entry))
2792                    (start (match-beginning (nth 1 entry)))
2793                    (end (match-end (nth 1 entry)))
2794                    (form (nth 2 entry)))
2795               (goto-char (match-end 0))
2796               (when (eval form)
2797                 (gnus-article-add-button 
2798                  start end (nth 3 entry)
2799                  (buffer-substring (match-beginning (nth 4 entry))
2800                                    (match-end (nth 4 entry)))))))
2801           (goto-char end))))
2802     (widen)))
2803
2804 ;;; External functions:
2805
2806 (defun gnus-article-add-button (from to fun &optional data)
2807   "Create a button between FROM and TO with callback FUN and data DATA."
2808   (when gnus-article-button-face
2809     (gnus-overlay-put (gnus-make-overlay from to)
2810                       'face gnus-article-button-face))
2811   (gnus-add-text-properties 
2812    from to
2813    (nconc (and gnus-article-mouse-face
2814                (list gnus-mouse-face-prop gnus-article-mouse-face))
2815           (list 'gnus-callback fun)
2816           (and data (list 'gnus-data data)))))
2817
2818 ;;; Internal functions:
2819
2820 (defun gnus-signature-toggle (end)
2821   (save-excursion
2822     (set-buffer gnus-article-buffer)
2823     (let ((buffer-read-only nil)
2824           (inhibit-point-motion-hooks t))
2825       (if (get-text-property end 'invisible)
2826           (gnus-article-unhide-text end (point-max))
2827         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
2828
2829 (defun gnus-button-entry ()
2830   ;; Return the first entry in `gnus-button-alist' matching this place.
2831   (let ((alist gnus-button-alist)
2832         (entry nil))
2833     (while alist
2834       (setq entry (pop alist))
2835       (if (looking-at (car entry))
2836           (setq alist nil)
2837         (setq entry nil)))
2838     entry))
2839
2840 (defun gnus-button-push (marker)
2841   ;; Push button starting at MARKER.
2842   (save-excursion
2843     (set-buffer gnus-article-buffer)
2844     (goto-char marker)
2845     (let* ((entry (gnus-button-entry))
2846            (inhibit-point-motion-hooks t)
2847            (fun (nth 3 entry))
2848            (args (mapcar (lambda (group)
2849                            (let ((string (match-string group)))
2850                              (gnus-set-text-properties
2851                               0 (length string) nil string)
2852                              string))
2853                          (nthcdr 4 entry))))
2854       (cond
2855        ((fboundp fun)
2856         (apply fun args))
2857        ((and (boundp fun)
2858              (fboundp (symbol-value fun)))
2859         (apply (symbol-value fun) args))
2860        (t
2861         (gnus-message 1 "You must define `%S' to use this button"
2862                       (cons fun args)))))))
2863
2864 (defun gnus-button-message-id (message-id)
2865   "Fetch MESSAGE-ID."
2866   (save-excursion
2867     (set-buffer gnus-summary-buffer)
2868     (gnus-summary-refer-article message-id)))
2869
2870 (defun gnus-button-fetch-group (address)
2871   "Fetch GROUP specified by ADDRESS."
2872   (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\(.*\\)$" address))
2873       (error "Can't parse %s" address)
2874     (gnus-group-read-ephemeral-group
2875      (match-string 4 address)
2876      `(nntp ,(match-string 1 address) (nntp-address ,(match-string 1 address))
2877             (nntp-port-number ,(if (match-end 3)
2878                                    (match-string 3 address)
2879                                  "nntp"))))))
2880
2881 (defun gnus-split-string (string pattern)
2882   "Return a list of substrings of STRING which are separated by PATTERN."
2883   (let (parts (start 0))
2884     (while (string-match pattern string start)
2885       (setq parts (cons (substring string start (match-beginning 0)) parts)
2886             start (match-end 0)))
2887     (nreverse (cons (substring string start) parts))))
2888  
2889 (defun gnus-url-parse-query-string (query &optional downcase)
2890   (let (retval pairs cur key val)
2891     (setq pairs (gnus-split-string query "&"))
2892     (while pairs
2893       (setq cur (car pairs)
2894             pairs (cdr pairs))
2895       (if (not (string-match "=" cur))
2896           nil                           ; Grace
2897         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
2898               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
2899         (if downcase
2900             (setq key (downcase key)))
2901         (setq cur (assoc key retval))
2902         (if cur
2903             (setcdr cur (cons val (cdr cur)))
2904           (setq retval (cons (list key val) retval)))))
2905     retval))
2906  
2907 (defun gnus-url-unhex (x)
2908   (if (> x ?9)
2909       (if (>= x ?a)
2910           (+ 10 (- x ?a))
2911         (+ 10 (- x ?A)))
2912     (- x ?0)))
2913  
2914 (defun gnus-url-unhex-string (str &optional allow-newlines)
2915   "Remove %XXX embedded spaces, etc in a url.
2916 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
2917 decoding of carriage returns and line feeds in the string, which is normally
2918 forbidden in URL encoding."
2919   (setq str (or str ""))
2920   (let ((tmp "")
2921         (case-fold-search t))
2922     (while (string-match "%[0-9a-f][0-9a-f]" str)
2923       (let* ((start (match-beginning 0))
2924              (ch1 (gnus-url-unhex (elt str (+ start 1))))
2925              (code (+ (* 16 ch1)
2926                       (gnus-url-unhex (elt str (+ start 2))))))
2927         (setq tmp (concat 
2928                    tmp (substring str 0 start)
2929                    (cond
2930                     (allow-newlines
2931                      (char-to-string code))
2932                     ((or (= code ?\n) (= code ?\r))
2933                      " ")
2934                     (t (char-to-string code))))
2935               str (substring str (match-end 0)))))
2936     (setq tmp (concat tmp str))
2937     tmp))
2938  
2939 (defun gnus-url-mailto (url)
2940   ;; Send mail to someone
2941   (when (string-match "mailto:/*\\(.*\\)" url)
2942     (setq url (substring url (match-beginning 1) nil)))
2943   (let (to args source-url subject func)
2944     (if (string-match (regexp-quote "?") url)
2945         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
2946               args (gnus-url-parse-query-string
2947                     (substring url (match-end 0) nil) t))
2948       (setq to (gnus-url-unhex-string url)))
2949     (setq args (cons (list "to" to) args)
2950           subject (cdr-safe (assoc "subject" args)))
2951     (message-mail)
2952     (while args
2953       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
2954       (if (fboundp func)
2955           (funcall func)
2956         (message-position-on-field (caar args)))
2957       (insert (mapconcat 'identity (cdar args) ", "))
2958       (setq args (cdr args)))
2959     (if subject
2960         (message-goto-body)
2961       (message-goto-subject))))
2962
2963 (defun gnus-button-mailto (address)
2964   ;; Mail to ADDRESS.
2965   (set-buffer (gnus-copy-article-buffer))
2966   (message-reply address))
2967
2968 (defun gnus-button-reply (address)
2969   ;; Reply to ADDRESS.
2970   (message-reply address))
2971
2972 (defun gnus-button-url (address)
2973   "Browse ADDRESS."
2974   (funcall browse-url-browser-function address))
2975
2976 (defun gnus-button-embedded-url (address)
2977   "Browse ADDRESS."
2978   (funcall browse-url-browser-function (gnus-strip-whitespace address)))
2979
2980 ;;; Next/prev buttons in the article buffer.
2981
2982 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
2983 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
2984
2985 (defvar gnus-prev-page-map nil)
2986 (unless gnus-prev-page-map
2987   (setq gnus-prev-page-map (make-sparse-keymap))
2988   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
2989   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
2990
2991 (defun gnus-insert-prev-page-button ()
2992   (let ((buffer-read-only nil))
2993     (gnus-eval-format 
2994      gnus-prev-page-line-format nil
2995      `(gnus-prev t local-map ,gnus-prev-page-map
2996                  gnus-callback gnus-article-button-prev-page))))
2997
2998 (defvar gnus-next-page-map nil)
2999 (unless gnus-next-page-map
3000   (setq gnus-next-page-map (make-keymap))
3001   (suppress-keymap gnus-prev-page-map)
3002   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
3003   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
3004
3005 (defun gnus-button-next-page ()
3006   "Go to the next page."
3007   (interactive)
3008   (let ((win (selected-window)))
3009     (select-window (get-buffer-window gnus-article-buffer t))
3010     (gnus-article-next-page)
3011     (select-window win)))
3012
3013 (defun gnus-button-prev-page ()
3014   "Go to the prev page."
3015   (interactive)
3016   (let ((win (selected-window)))
3017     (select-window (get-buffer-window gnus-article-buffer t))
3018     (gnus-article-prev-page)
3019     (select-window win)))
3020
3021 (defun gnus-insert-next-page-button ()
3022   (let ((buffer-read-only nil))
3023     (gnus-eval-format gnus-next-page-line-format nil
3024                       `(gnus-next t local-map ,gnus-next-page-map
3025                                   gnus-callback 
3026                                   gnus-article-button-next-page))))
3027
3028 (defun gnus-article-button-next-page (arg)
3029   "Go to the next page."
3030   (interactive "P")
3031   (let ((win (selected-window)))
3032     (select-window (get-buffer-window gnus-article-buffer t))
3033     (gnus-article-next-page)
3034     (select-window win)))
3035
3036 (defun gnus-article-button-prev-page (arg)
3037   "Go to the prev page."
3038   (interactive "P")
3039   (let ((win (selected-window)))
3040     (select-window (get-buffer-window gnus-article-buffer t))
3041     (gnus-article-prev-page)
3042     (select-window win))) 
3043
3044 (gnus-ems-redefine)
3045
3046 (provide 'gnus-art)
3047
3048 (run-hooks 'gnus-art-load-hook)
3049
3050 ;;; gnus-art.el ends here