*** 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-From"
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 
952              (match-beginning 0) (match-end 0) 'pgp))
953           (widen))))))
954
955 (defun article-hide-pem (&optional arg)
956   "Toggle hiding of any PEM headers and signatures in the current article.
957 If given a negative prefix, always show; if given a positive prefix,
958 always hide."
959   (interactive (gnus-article-hidden-arg))
960   (unless (gnus-article-check-hidden-text 'pem arg)
961     (save-excursion
962       (let (buffer-read-only end)
963         (widen)
964         (goto-char (point-min))
965         ;; hide the horrendously ugly "header".
966         (and (search-forward "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
967                              nil
968                              t)
969              (setq end (1+ (match-beginning 0)))
970              (gnus-article-hide-text-type
971               end
972               (if (search-forward "\n\n" nil t)
973                   (match-end 0)
974                 (point-max))
975               'pem))
976         ;; hide the trailer as well
977         (and (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
978                              nil
979                              t)
980              (gnus-article-hide-text-type
981               (match-beginning 0) (match-end 0) 'pem))))))
982
983 (defun article-hide-signature (&optional arg)
984   "Hide the signature in the current article.
985 If given a negative prefix, always show; if given a positive prefix,
986 always hide."
987   (interactive (gnus-article-hidden-arg))
988   (unless (gnus-article-check-hidden-text 'signature arg)
989     (save-excursion
990       (save-restriction
991         (let ((buffer-read-only nil))
992           (when (gnus-article-narrow-to-signature)
993             (gnus-article-hide-text-type 
994              (point-min) (point-max) 'signature)))))))
995
996 (defun article-strip-leading-blank-lines ()
997   "Remove all blank lines from the beginning of the article."
998   (interactive)
999   (save-excursion
1000     (let ((inhibit-point-motion-hooks t)
1001           buffer-read-only)
1002       (goto-char (point-min))
1003       (when (search-forward "\n\n" nil t)
1004         (while (and (not (eobp))
1005                     (looking-at "[ \t]*$"))
1006           (gnus-delete-line))))))
1007
1008 (defun article-strip-multiple-blank-lines ()
1009   "Replace consecutive blank lines with one empty line."
1010   (interactive)
1011   (save-excursion
1012     (let (buffer-read-only)
1013       ;; First make all blank lines empty.
1014       (goto-char (point-min))
1015       (while (re-search-forward "^[ \t]+$" nil t)
1016         (replace-match "" nil t))
1017       ;; Then replace multiple empty lines with a single empty line.
1018       (goto-char (point-min))
1019       (while (re-search-forward "\n\n\n+" nil t)
1020         (replace-match "\n\n" t t)))))
1021
1022 (defun article-strip-blank-lines ()
1023   "Strip leading, trailing and multiple blank lines."
1024   (interactive)
1025   (article-strip-leading-blank-lines)
1026   (article-remove-trailing-blank-lines)
1027   (article-strip-multiple-blank-lines))
1028
1029 (defvar mime::preview/content-list)
1030 (defvar mime::preview-content-info/point-min)
1031 (defun gnus-article-narrow-to-signature ()
1032   "Narrow to the signature; return t if a signature is found, else nil."
1033   (widen)
1034   (when (and (boundp 'mime::preview/content-list)
1035              mime::preview/content-list)
1036     ;; We have a MIMEish article, so we use the MIME data to narrow.
1037     (let ((pcinfo (car (last mime::preview/content-list))))
1038       (ignore-errors
1039         (narrow-to-region
1040          (funcall (intern "mime::preview-content-info/point-min") pcinfo)
1041          (point-max)))))
1042   
1043   (when (gnus-article-search-signature)
1044     (forward-line 1)
1045     ;; Check whether we have some limits to what we consider
1046     ;; to be a signature.
1047     (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1048                     (list gnus-signature-limit)))
1049           limit limited)
1050       (while (setq limit (pop limits))
1051         (if (or (and (integerp limit)
1052                      (< (- (point-max) (point)) limit))
1053                 (and (floatp limit)
1054                      (< (count-lines (point) (point-max)) limit))
1055                 (and (gnus-functionp limit)
1056                      (funcall limit))
1057                 (and (stringp limit)
1058                      (not (re-search-forward limit nil t))))
1059             ()                          ; This limit did not succeed.
1060           (setq limited t
1061                 limits nil)))
1062       (unless limited
1063         (narrow-to-region (point) (point-max))
1064         t))))
1065
1066 (defun gnus-article-search-signature ()
1067   "Search the current buffer for the signature separator.
1068 Put point at the beginning of the signature separator."
1069   (let ((cur (point)))
1070     (goto-char (point-max))
1071     (if (if (stringp gnus-signature-separator)
1072             (re-search-backward gnus-signature-separator nil t)
1073           (let ((seps gnus-signature-separator))
1074             (while (and seps
1075                         (not (re-search-backward (car seps) nil t)))
1076               (pop seps))
1077             seps))
1078         t
1079       (goto-char cur)
1080       nil)))
1081
1082 (defun gnus-article-hidden-arg ()
1083   "Return the current prefix arg as a number, or 0 if no prefix."
1084   (list (if current-prefix-arg
1085             (prefix-numeric-value current-prefix-arg)
1086           0)))
1087
1088 (defun gnus-article-check-hidden-text (type arg)
1089   "Return nil if hiding is necessary.
1090 Arg can be nil or a number.  Nil and positive means hide, negative
1091 means show, 0 means toggle."
1092   (save-excursion
1093     (save-restriction
1094       (widen)
1095       (let ((hide (gnus-article-hidden-text-p type)))
1096         (cond
1097          ((or (null arg)
1098               (> arg 0))
1099           nil)
1100          ((< arg 0)
1101           (gnus-article-show-hidden-text type))
1102          (t
1103           (if (eq hide 'hidden)
1104               (gnus-article-show-hidden-text type)
1105             nil)))))))
1106
1107 (defun gnus-article-hidden-text-p (type)
1108   "Say whether the current buffer contains hidden text of type TYPE."
1109   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1110     (when pos
1111       (if (get-text-property pos 'invisible)
1112           'hidden
1113         'shown))))
1114
1115 (defun gnus-article-show-hidden-text (type &optional hide)
1116   "Show all hidden text of type TYPE.
1117 If HIDE, hide the text instead."
1118   (save-excursion
1119     (let ((buffer-read-only nil)
1120           (inhibit-point-motion-hooks t)
1121           (end (point-min))
1122           beg)
1123       (while (setq beg (text-property-any end (point-max) 'article-type type))
1124         (goto-char beg)
1125         (setq end (or
1126                    (text-property-not-all beg (point-max) 'article-type type)
1127                    (point-max)))
1128         (if hide
1129             (gnus-article-hide-text beg end gnus-hidden-properties)
1130           (gnus-article-unhide-text beg end))
1131         (goto-char end))
1132       t)))
1133
1134 (defconst article-time-units
1135   `((year . ,(* 365.25 24 60 60))
1136     (week . ,(* 7 24 60 60))
1137     (day . ,(* 24 60 60))
1138     (hour . ,(* 60 60))
1139     (minute . 60)
1140     (second . 1))
1141   "Mapping from time units to seconds.")
1142
1143 (defun article-date-ut (&optional type highlight header)
1144   "Convert DATE date to universal time in the current article.
1145 If TYPE is `local', convert to local time; if it is `lapsed', output
1146 how much time has lapsed since DATE."
1147   (interactive (list 'ut t))
1148   (let* ((header (or header 
1149                      (mail-header-date gnus-current-headers)
1150                      (message-fetch-field "date")
1151                      ""))
1152          (date (if (vectorp header) (mail-header-date header)
1153                  header))
1154          (date-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1155          (inhibit-point-motion-hooks t)
1156          bface eface)
1157     (when (and date (not (string= date "")))
1158       (save-excursion
1159         (save-restriction
1160           (nnheader-narrow-to-headers)
1161           (let ((buffer-read-only nil))
1162             ;; Delete any old Date headers.
1163             (if (re-search-forward date-regexp nil t)
1164                 (progn
1165                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
1166                         eface (get-text-property (1- (gnus-point-at-eol))
1167                                                  'face))
1168                   (message-remove-header date-regexp t)
1169                   (beginning-of-line))
1170               (goto-char (point-max)))
1171             (insert (article-make-date-line date type))
1172             ;; Do highlighting.
1173             (forward-line -1)
1174             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1175               (put-text-property (match-beginning 1) (match-end 1)
1176                                  'face bface)
1177               (put-text-property (match-beginning 2) (match-end 2)
1178                                  'face eface))))))))
1179
1180 (defun article-make-date-line (date type)
1181   "Return a DATE line of TYPE."
1182   (cond
1183    ;; Convert to the local timezone.  We have to slap a
1184    ;; `condition-case' round the calls to the timezone
1185    ;; functions since they aren't particularly resistant to
1186    ;; buggy dates.
1187    ((eq type 'local)
1188     (concat "Date: " (condition-case ()
1189                          (timezone-make-date-arpa-standard date)
1190                        (error date))
1191             "\n"))
1192    ;; Convert to Universal Time.
1193    ((eq type 'ut)
1194     (concat "Date: "
1195             (condition-case ()
1196                 (timezone-make-date-arpa-standard date nil "UT")
1197               (error date))
1198             "\n"))
1199    ;; Get the original date from the article.
1200    ((eq type 'original)
1201     (concat "Date: " date "\n"))
1202    ;; Let the user define the format.
1203    ((eq type 'user)
1204     (format-time-string gnus-article-time-format
1205                         (ignore-errors
1206                           (gnus-encode-date
1207                            (timezone-make-date-arpa-standard
1208                             date nil "UT")))))
1209    ;; Do an X-Sent lapsed format.
1210    ((eq type 'lapsed)
1211     ;; If the date is seriously mangled, the timezone functions are
1212     ;; liable to bug out, so we ignore all errors.
1213     (let* ((now (current-time))
1214            (real-time
1215             (ignore-errors
1216               (gnus-time-minus
1217                (gnus-encode-date
1218                 (timezone-make-date-arpa-standard
1219                  (current-time-string now)
1220                  (current-time-zone now) "UT"))
1221                (gnus-encode-date
1222                 (timezone-make-date-arpa-standard
1223                  date nil "UT")))))
1224            (real-sec (and real-time
1225                           (+ (* (float (car real-time)) 65536)
1226                              (cadr real-time))))
1227            (sec (and real-time (abs real-sec)))
1228            num prev)
1229       (cond
1230        ((null real-time)
1231         "X-Sent: Unknown\n")
1232        ((zerop sec)
1233         "X-Sent: Now\n")
1234        (t
1235         (concat
1236          "X-Sent: "
1237          ;; This is a bit convoluted, but basically we go
1238          ;; through the time units for years, weeks, etc,
1239          ;; and divide things to see whether that results
1240          ;; in positive answers.
1241          (mapconcat
1242           (lambda (unit)
1243             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1244                 ;; The (remaining) seconds are too few to
1245                 ;; be divided into this time unit.
1246                 ""
1247               ;; It's big enough, so we output it.
1248               (setq sec (- sec (* num (cdr unit))))
1249               (prog1
1250                   (concat (if prev ", " "") (int-to-string
1251                                              (floor num))
1252                           " " (symbol-name (car unit)) 
1253                           (if (> num 1) "s" ""))
1254                 (setq prev t))))
1255           article-time-units "")
1256          ;; If dates are odd, then it might appear like the
1257          ;; article was sent in the future.
1258          (if (> real-sec 0)
1259              " ago\n"
1260            " in the future\n"))))))
1261    (t
1262     (error "Unknown conversion type: %s" type))))
1263
1264 (defun article-date-local (&optional highlight)
1265   "Convert the current article date to the local timezone."
1266   (interactive (list t))
1267   (article-date-ut 'local highlight))
1268
1269 (defun article-date-original (&optional highlight)
1270   "Convert the current article date to what it was originally.
1271 This is only useful if you have used some other date conversion
1272 function and want to see what the date was before converting."
1273   (interactive (list t))
1274   (article-date-ut 'original highlight))
1275
1276 (defun article-date-lapsed (&optional highlight)
1277   "Convert the current article date to time lapsed since it was sent."
1278   (interactive (list t))
1279   (article-date-ut 'lapsed highlight))
1280
1281 (defun article-date-user (&optional highlight)
1282   "Convert the current article date to the user-defined format."
1283   (interactive (list t))
1284   (article-date-ut 'user highlight))
1285
1286 (defun article-show-all ()
1287   "Show all hidden text in the article buffer."
1288   (interactive)
1289   (save-excursion
1290     (let ((buffer-read-only nil))
1291       (gnus-article-unhide-text (point-min) (point-max)))))
1292
1293 (defun article-emphasize (&optional arg)
1294   "Emphasize text according to `gnus-emphasis-alist'."
1295   (interactive (gnus-article-hidden-arg))
1296   (unless (gnus-article-check-hidden-text 'emphasis arg)
1297     (save-excursion
1298       (let ((alist gnus-emphasis-alist)
1299             (buffer-read-only nil)
1300             (props (append '(article-type emphasis)
1301                            gnus-hidden-properties))
1302             regexp elem beg invisible visible face)
1303         (goto-char (point-min))
1304         (search-forward "\n\n" nil t)
1305         (setq beg (point))
1306         (while (setq elem (pop alist))
1307           (goto-char beg)
1308           (setq regexp (car elem)
1309                 invisible (nth 1 elem)
1310                 visible (nth 2 elem)
1311                 face (nth 3 elem))
1312           (while (re-search-forward regexp nil t)
1313             (when (and (match-beginning visible) (match-beginning invisible))
1314               (gnus-article-hide-text
1315                (match-beginning invisible) (match-end invisible) props)
1316               (gnus-article-unhide-text-type
1317                (match-beginning visible) (match-end visible) 'emphasis)
1318               (gnus-put-text-property-excluding-newlines
1319                (match-beginning visible) (match-end visible) 'face face)
1320               (goto-char (match-end invisible)))))))))
1321
1322 (defvar gnus-summary-article-menu)
1323 (defvar gnus-summary-post-menu)
1324
1325 ;;; Saving functions.
1326
1327 (defun gnus-article-save (save-buffer file &optional num)
1328   "Save the currently selected article."
1329   (unless gnus-save-all-headers
1330     ;; Remove headers according to `gnus-saved-headers'.
1331     (let ((gnus-visible-headers
1332            (or gnus-saved-headers gnus-visible-headers))
1333           (gnus-article-buffer save-buffer))
1334       (gnus-article-hide-headers 1 t)))
1335   (save-window-excursion
1336     (if (not gnus-default-article-saver)
1337         (error "No default saver is defined.")
1338       ;; !!! Magic!  The saving functions all save
1339       ;; `gnus-original-article-buffer' (or so they think), but we
1340       ;; bind that variable to our save-buffer.
1341       (set-buffer gnus-article-buffer)
1342       (let* ((gnus-save-article-buffer save-buffer)
1343              (filename
1344               (cond
1345                ((not gnus-prompt-before-saving) 'default)
1346                ((eq gnus-prompt-before-saving 'always) nil)
1347                (t file)))
1348              (gnus-number-of-articles-to-be-saved
1349               (when (eq gnus-prompt-before-saving t)
1350                 num)))                  ; Magic
1351         (set-buffer gnus-summary-buffer)
1352         (funcall gnus-default-article-saver filename))))) 
1353
1354 (defun gnus-read-save-file-name (prompt default-name &optional filename)
1355   (cond
1356    ((eq filename 'default)
1357     default-name)
1358    (filename filename)
1359    (t
1360     (let* ((split-name (gnus-get-split-value gnus-split-methods))
1361            (prompt
1362             (format prompt (if (and gnus-number-of-articles-to-be-saved
1363                                     (> gnus-number-of-articles-to-be-saved 1))
1364                                (format "these %d articles"
1365                                        gnus-number-of-articles-to-be-saved)
1366                              "this article")))
1367            (file
1368             ;; Let the split methods have their say.
1369             (cond
1370              ;; No split name was found.
1371              ((null split-name)
1372               (read-file-name
1373                (concat prompt " (default "
1374                        (file-name-nondirectory default-name) ") ")
1375                (file-name-directory default-name)
1376                default-name))
1377              ;; A single split name was found
1378              ((= 1 (length split-name))
1379               (let* ((name (car split-name))
1380                      (dir (cond ((file-directory-p name)
1381                                  (file-name-as-directory name))
1382                                 ((file-exists-p name) name)
1383                                 (t gnus-article-save-directory))))
1384                 (read-file-name
1385                  (concat prompt " (default " name ") ")
1386                  dir name)))
1387              ;; A list of splits was found.
1388              (t
1389               (setq split-name (nreverse split-name))
1390               (let (result)
1391                 (let ((file-name-history (nconc split-name file-name-history)))
1392                   (setq result
1393                         (expand-file-name
1394                          (read-file-name
1395                           (concat prompt " (`M-p' for defaults) ")
1396                           gnus-article-save-directory
1397                           (car split-name))
1398                          gnus-article-save-directory)))
1399                 (car (push result file-name-history)))))))
1400       ;; Create the directory.
1401       (gnus-make-directory (file-name-directory file))
1402       ;; If we have read a directory, we append the default file name.
1403       (when (file-directory-p file)
1404         (setq file (concat (file-name-as-directory file)
1405                            (file-name-nondirectory default-name))))
1406       ;; Possibly translate some characters.
1407       (nnheader-translate-file-chars file)))))
1408
1409 (defun gnus-article-archive-name (group)
1410   "Return the first instance of an \"Archive-name\" in the current buffer."
1411   (let ((case-fold-search t))
1412     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
1413       (nnheader-concat gnus-article-save-directory
1414                        (match-string 1)))))
1415
1416 (defun gnus-summary-save-in-rmail (&optional filename)
1417   "Append this article to Rmail file.
1418 Optional argument FILENAME specifies file name.
1419 Directory to save to is default to `gnus-article-save-directory'."
1420   (interactive)
1421   (gnus-set-global-variables)
1422   (let ((default-name
1423           (funcall gnus-rmail-save-name gnus-newsgroup-name
1424                    gnus-current-headers gnus-newsgroup-last-rmail)))
1425     (setq filename (gnus-read-save-file-name
1426                     "Save %s in rmail file:" default-name filename))
1427     (gnus-make-directory (file-name-directory filename))
1428     (gnus-eval-in-buffer-window gnus-save-article-buffer
1429       (save-excursion
1430         (save-restriction
1431           (widen)
1432           (gnus-output-to-rmail filename))))
1433     ;; Remember the directory name to save articles
1434     (setq gnus-newsgroup-last-rmail filename)))
1435
1436 (defun gnus-summary-save-in-mail (&optional filename)
1437   "Append this article to Unix mail file.
1438 Optional argument FILENAME specifies file name.
1439 Directory to save to is default to `gnus-article-save-directory'."
1440   (interactive)
1441   (gnus-set-global-variables)
1442   (let ((default-name
1443           (funcall gnus-mail-save-name gnus-newsgroup-name
1444                    gnus-current-headers gnus-newsgroup-last-mail)))
1445     (setq filename (gnus-read-save-file-name
1446                     "Save %s in Unix mail file:" default-name filename))
1447     (setq filename
1448           (expand-file-name filename
1449                             (and default-name
1450                                  (file-name-directory default-name))))
1451     (gnus-make-directory (file-name-directory filename))
1452     (gnus-eval-in-buffer-window gnus-save-article-buffer
1453       (save-excursion
1454         (save-restriction
1455           (widen)
1456           (if (and (file-readable-p filename) (mail-file-babyl-p filename))
1457               (gnus-output-to-rmail filename)
1458             (let ((mail-use-rfc822 t))
1459               (rmail-output filename 1 t t))))))
1460     ;; Remember the directory name to save articles.
1461     (setq gnus-newsgroup-last-mail filename)))
1462
1463 (defun gnus-summary-save-in-file (&optional filename overwrite)
1464   "Append this article to file.
1465 Optional argument FILENAME specifies file name.
1466 Directory to save to is default to `gnus-article-save-directory'."
1467   (interactive)
1468   (gnus-set-global-variables)
1469   (let ((default-name
1470           (funcall gnus-file-save-name gnus-newsgroup-name
1471                    gnus-current-headers gnus-newsgroup-last-file)))
1472     (setq filename (gnus-read-save-file-name
1473                     "Save %s in file:" default-name filename))
1474     (gnus-make-directory (file-name-directory filename))
1475     (gnus-eval-in-buffer-window gnus-save-article-buffer
1476       (save-excursion
1477         (save-restriction
1478           (widen)
1479           (when (and overwrite
1480                      (file-exists-p filename))
1481             (delete-file filename))
1482           (gnus-output-to-file filename))))
1483     ;; Remember the directory name to save articles.
1484     (setq gnus-newsgroup-last-file filename)))
1485
1486 (defun gnus-summary-write-to-file (&optional filename)
1487   "Write this article to a file.
1488 Optional argument FILENAME specifies file name.
1489 The directory to save in defaults to `gnus-article-save-directory'."
1490   (interactive)
1491   (gnus-summary-save-in-file nil t))
1492
1493 (defun gnus-summary-save-body-in-file (&optional filename)
1494   "Append this article body to a file.
1495 Optional argument FILENAME specifies file name.
1496 The directory to save in defaults to `gnus-article-save-directory'."
1497   (interactive)
1498   (gnus-set-global-variables)
1499   (let ((default-name
1500           (funcall gnus-file-save-name gnus-newsgroup-name
1501                    gnus-current-headers gnus-newsgroup-last-file)))
1502     (setq filename (gnus-read-save-file-name
1503                     "Save %s body in file:" default-name filename))
1504     (gnus-make-directory (file-name-directory filename))
1505     (gnus-eval-in-buffer-window gnus-save-article-buffer
1506       (save-excursion
1507         (save-restriction
1508           (widen)
1509           (goto-char (point-min))
1510           (when (search-forward "\n\n" nil t)
1511             (narrow-to-region (point) (point-max)))
1512           (gnus-output-to-file filename))))
1513     ;; Remember the directory name to save articles.
1514     (setq gnus-newsgroup-last-file filename)))
1515
1516 (defun gnus-summary-save-in-pipe (&optional command)
1517   "Pipe this article to subprocess."
1518   (interactive)
1519   (gnus-set-global-variables)
1520   (setq command
1521         (cond ((eq command 'default)
1522                gnus-last-shell-command)
1523               (command command)
1524               (t (read-string 
1525                   (format
1526                    "Shell command on %s: "
1527                    (if (and gnus-number-of-articles-to-be-saved
1528                             (> gnus-number-of-articles-to-be-saved 1))
1529                        (format "these %d articles"
1530                                gnus-number-of-articles-to-be-saved)
1531                      "this article"))
1532                   gnus-last-shell-command))))
1533   (when (string-equal command "")
1534     (setq command gnus-last-shell-command))
1535   (gnus-eval-in-buffer-window gnus-article-buffer
1536     (save-restriction
1537       (widen)
1538       (shell-command-on-region (point-min) (point-max) command nil)))
1539   (setq gnus-last-shell-command command))
1540
1541 ;;; Article file names when saving.
1542
1543 (defun gnus-capitalize-newsgroup (newsgroup)
1544   "Capitalize NEWSGROUP name."
1545   (when (not (zerop (length newsgroup)))
1546     (concat (char-to-string (upcase (aref newsgroup 0)))
1547             (substring newsgroup 1))))
1548
1549 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1550   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1551 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
1552 Otherwise, it is like ~/News/news/group/num."
1553   (let ((default
1554           (expand-file-name
1555            (concat (if (gnus-use-long-file-name 'not-save)
1556                        (gnus-capitalize-newsgroup newsgroup)
1557                      (gnus-newsgroup-directory-form newsgroup))
1558                    "/" (int-to-string (mail-header-number headers)))
1559            gnus-article-save-directory)))
1560     (if (and last-file
1561              (string-equal (file-name-directory default)
1562                            (file-name-directory last-file))
1563              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1564         default
1565       (or last-file default))))
1566
1567 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1568   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1569 If variable `gnus-use-long-file-name' is non-nil, it is
1570 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
1571   (let ((default
1572           (expand-file-name
1573            (concat (if (gnus-use-long-file-name 'not-save)
1574                        newsgroup
1575                      (gnus-newsgroup-directory-form newsgroup))
1576                    "/" (int-to-string (mail-header-number headers)))
1577            gnus-article-save-directory)))
1578     (if (and last-file
1579              (string-equal (file-name-directory default)
1580                            (file-name-directory last-file))
1581              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1582         default
1583       (or last-file default))))
1584
1585 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1586   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1587 If variable `gnus-use-long-file-name' is non-nil, it is
1588 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
1589   (or last-file
1590       (expand-file-name
1591        (if (gnus-use-long-file-name 'not-save)
1592            (gnus-capitalize-newsgroup newsgroup)
1593          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1594        gnus-article-save-directory)))
1595
1596 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1597   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1598 If variable `gnus-use-long-file-name' is non-nil, it is
1599 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
1600   (or last-file
1601       (expand-file-name
1602        (if (gnus-use-long-file-name 'not-save)
1603            newsgroup
1604          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1605        gnus-article-save-directory)))
1606
1607 (eval-and-compile
1608   (mapcar
1609    (lambda (func)
1610      (let (afunc gfunc)
1611        (if (consp func)
1612            (setq afunc (car func)
1613                  gfunc (cdr func))
1614          (setq afunc func
1615                gfunc (intern (format "gnus-%s" func))))
1616        (fset gfunc 
1617              (if (not (fboundp afunc))
1618                  nil
1619                `(lambda (&optional interactive &rest args)
1620                   ,(documentation afunc t)
1621                   (interactive (list t))
1622                   (save-excursion
1623                     (set-buffer gnus-article-buffer)
1624                     (if interactive
1625                         (call-interactively ',afunc)
1626                       (apply ',afunc args))))))))
1627    '(article-hide-headers
1628      article-hide-boring-headers
1629      article-treat-overstrike
1630      (article-fill . gnus-article-word-wrap)
1631      article-remove-cr
1632      article-display-x-face
1633      article-de-quoted-unreadable
1634      article-mime-decode-quoted-printable
1635      article-hide-pgp
1636      article-hide-pem
1637      article-hide-signature
1638      article-remove-trailing-blank-lines
1639      article-strip-leading-blank-lines
1640      article-strip-multiple-blank-lines
1641      article-strip-blank-lines
1642      article-date-local
1643      article-date-original
1644      article-date-ut
1645      article-date-user
1646      article-date-lapsed
1647      article-emphasize
1648      (article-show-all . gnus-article-show-all-headers))))
1649 \f
1650 ;;;
1651 ;;; Gnus article mode
1652 ;;;
1653
1654 (put 'gnus-article-mode 'mode-class 'special)
1655
1656 (when t
1657   (gnus-define-keys gnus-article-mode-map
1658     " " gnus-article-goto-next-page
1659     "\177" gnus-article-goto-prev-page
1660     [delete] gnus-article-goto-prev-page
1661     "\C-c^" gnus-article-refer-article
1662     "h" gnus-article-show-summary
1663     "s" gnus-article-show-summary
1664     "\C-c\C-m" gnus-article-mail
1665     "?" gnus-article-describe-briefly
1666     gnus-mouse-2 gnus-article-push-button
1667     "\r" gnus-article-press-button
1668     "\t" gnus-article-next-button
1669     "\M-\t" gnus-article-prev-button
1670     "e" gnus-article-edit
1671     "<" beginning-of-buffer
1672     ">" end-of-buffer
1673     "\C-c\C-i" gnus-info-find-node
1674     "\C-c\C-b" gnus-bug
1675
1676     "\C-d" gnus-article-read-summary-keys
1677     "\M-*" gnus-article-read-summary-keys
1678     "\M-#" gnus-article-read-summary-keys
1679     "\M-^" gnus-article-read-summary-keys
1680     "\M-g" gnus-article-read-summary-keys)
1681
1682   (substitute-key-definition
1683    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
1684
1685 (defun gnus-article-make-menu-bar ()
1686   (gnus-turn-off-edit-menu 'article)
1687   (unless (boundp 'gnus-article-article-menu)
1688     (easy-menu-define
1689      gnus-article-article-menu gnus-article-mode-map ""
1690      '("Article"
1691        ["Scroll forwards" gnus-article-goto-next-page t]
1692        ["Scroll backwards" gnus-article-goto-prev-page t]
1693        ["Show summary" gnus-article-show-summary t]
1694        ["Fetch Message-ID at point" gnus-article-refer-article t]
1695        ["Mail to address at point" gnus-article-mail t]))
1696
1697     (easy-menu-define
1698      gnus-article-treatment-menu gnus-article-mode-map ""
1699      '("Treatment"
1700        ["Hide headers" gnus-article-hide-headers t]
1701        ["Hide signature" gnus-article-hide-signature t]
1702        ["Hide citation" gnus-article-hide-citation t]
1703        ["Treat overstrike" gnus-article-treat-overstrike t]
1704        ["Remove carriage return" gnus-article-remove-cr t]
1705        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]))
1706
1707     (when (boundp 'gnus-summary-article-menu)
1708       (define-key gnus-article-mode-map [menu-bar commands]
1709         (cons "Commands" gnus-summary-article-menu)))
1710
1711     (when (boundp 'gnus-summary-post-menu)
1712       (define-key gnus-article-mode-map [menu-bar post]
1713         (cons "Post" gnus-summary-post-menu)))
1714
1715     (run-hooks 'gnus-article-menu-hook)))
1716
1717 (defun gnus-article-mode ()
1718   "Major mode for displaying an article.
1719
1720 All normal editing commands are switched off.
1721
1722 The following commands are available in addition to all summary mode
1723 commands:
1724 \\<gnus-article-mode-map>
1725 \\[gnus-article-next-page]\t Scroll the article one page forwards
1726 \\[gnus-article-prev-page]\t Scroll the article one page backwards
1727 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
1728 \\[gnus-article-show-summary]\t Display the summary buffer
1729 \\[gnus-article-mail]\t Send a reply to the address near point
1730 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
1731 \\[gnus-info-find-node]\t Go to the Gnus info node"
1732   (interactive)
1733   (when (gnus-visual-p 'article-menu 'menu)
1734     (gnus-article-make-menu-bar))
1735   (kill-all-local-variables)
1736   (gnus-simplify-mode-line)
1737   (setq mode-name "Article")
1738   (setq major-mode 'gnus-article-mode)
1739   (make-local-variable 'minor-mode-alist)
1740   (unless (assq 'gnus-show-mime minor-mode-alist)
1741     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
1742   (use-local-map gnus-article-mode-map)
1743   (gnus-update-format-specifications nil 'article-mode)
1744   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
1745   (gnus-set-default-directory)
1746   (buffer-disable-undo (current-buffer))
1747   (setq buffer-read-only t)
1748   (set-syntax-table gnus-article-mode-syntax-table)
1749   (run-hooks 'gnus-article-mode-hook))
1750
1751 (defun gnus-article-setup-buffer ()
1752   "Initialize the article buffer."
1753   (let* ((name (if gnus-single-article-buffer "*Article*"
1754                  (concat "*Article " gnus-newsgroup-name "*")))
1755          (original
1756           (progn (string-match "\\*Article" name)
1757                  (concat " *Original Article"
1758                          (substring name (match-end 0))))))
1759     (setq gnus-article-buffer name)
1760     (setq gnus-original-article-buffer original)
1761     ;; This might be a variable local to the summary buffer.
1762     (unless gnus-single-article-buffer
1763       (save-excursion
1764         (set-buffer gnus-summary-buffer)
1765         (setq gnus-article-buffer name)
1766         (setq gnus-original-article-buffer original)
1767         (gnus-set-global-variables)))
1768     ;; Init original article buffer.
1769     (save-excursion
1770       (set-buffer (get-buffer-create gnus-original-article-buffer))
1771       (buffer-disable-undo (current-buffer))
1772       (setq major-mode 'gnus-original-article-mode)
1773       (gnus-add-current-to-buffer-list)
1774       (make-local-variable 'gnus-original-article))
1775     (if (get-buffer name)
1776         (save-excursion
1777           (set-buffer name)
1778           (buffer-disable-undo (current-buffer))
1779           (setq buffer-read-only t)
1780           (gnus-add-current-to-buffer-list)
1781           (unless (eq major-mode 'gnus-article-mode)
1782             (gnus-article-mode))
1783           (current-buffer))
1784       (save-excursion
1785         (set-buffer (get-buffer-create name))
1786         (gnus-add-current-to-buffer-list)
1787         (gnus-article-mode)
1788         (make-local-variable 'gnus-summary-buffer)
1789         (current-buffer)))))
1790
1791 ;; Set article window start at LINE, where LINE is the number of lines
1792 ;; from the head of the article.
1793 (defun gnus-article-set-window-start (&optional line)
1794   (set-window-start
1795    (get-buffer-window gnus-article-buffer t)
1796    (save-excursion
1797      (set-buffer gnus-article-buffer)
1798      (goto-char (point-min))
1799      (if (not line)
1800          (point-min)
1801        (gnus-message 6 "Moved to bookmark")
1802        (search-forward "\n\n" nil t)
1803        (forward-line line)
1804        (point)))))
1805
1806 (defun gnus-article-prepare (article &optional all-headers header)
1807   "Prepare ARTICLE in article mode buffer.
1808 ARTICLE should either be an article number or a Message-ID.
1809 If ARTICLE is an id, HEADER should be the article headers.
1810 If ALL-HEADERS is non-nil, no headers are hidden."
1811   (save-excursion
1812     ;; Make sure we start in a summary buffer.
1813     (unless (eq major-mode 'gnus-summary-mode)
1814       (set-buffer gnus-summary-buffer))
1815     (setq gnus-summary-buffer (current-buffer))
1816     ;; Make sure the connection to the server is alive.
1817     (unless (gnus-server-opened
1818              (gnus-find-method-for-group gnus-newsgroup-name))
1819       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
1820       (gnus-request-group gnus-newsgroup-name t))
1821     (let* ((gnus-article (if header (mail-header-number header) article))
1822            (summary-buffer (current-buffer))
1823            (internal-hook gnus-article-internal-prepare-hook)
1824            (group gnus-newsgroup-name)
1825            result)
1826       (save-excursion
1827         (gnus-article-setup-buffer)
1828         (set-buffer gnus-article-buffer)
1829         ;; Deactivate active regions.
1830         (when (and (boundp 'transient-mark-mode)
1831                    transient-mark-mode)
1832           (setq mark-active nil))
1833         (if (not (setq result (let ((buffer-read-only nil))
1834                                 (gnus-request-article-this-buffer
1835                                  article group))))
1836             ;; There is no such article.
1837             (save-excursion
1838               (when (and (numberp article)
1839                          (not (memq article gnus-newsgroup-sparse)))
1840                 (setq gnus-article-current
1841                       (cons gnus-newsgroup-name article))
1842                 (set-buffer gnus-summary-buffer)
1843                 (setq gnus-current-article article)
1844                 (gnus-summary-mark-article article gnus-canceled-mark))
1845               (unless (memq article gnus-newsgroup-sparse)
1846                 (gnus-error
1847                  1 "No such article (may have expired or been canceled)")))
1848           (if (or (eq result 'pseudo) (eq result 'nneething))
1849               (progn
1850                 (save-excursion
1851                   (set-buffer summary-buffer)
1852                   (setq gnus-last-article gnus-current-article
1853                         gnus-newsgroup-history (cons gnus-current-article
1854                                                      gnus-newsgroup-history)
1855                         gnus-current-article 0
1856                         gnus-current-headers nil
1857                         gnus-article-current nil)
1858                   (if (eq result 'nneething)
1859                       (gnus-configure-windows 'summary)
1860                     (gnus-configure-windows 'article))
1861                   (gnus-set-global-variables))
1862                 (gnus-set-mode-line 'article))
1863             ;; The result from the `request' was an actual article -
1864             ;; or at least some text that is now displayed in the
1865             ;; article buffer.
1866             (when (and (numberp article)
1867                        (not (eq article gnus-current-article)))
1868               ;; Seems like a new article has been selected.
1869               ;; `gnus-current-article' must be an article number.
1870               (save-excursion
1871                 (set-buffer summary-buffer)
1872                 (setq gnus-last-article gnus-current-article
1873                       gnus-newsgroup-history (cons gnus-current-article
1874                                                    gnus-newsgroup-history)
1875                       gnus-current-article article
1876                       gnus-current-headers
1877                       (gnus-summary-article-header gnus-current-article)
1878                       gnus-article-current
1879                       (cons gnus-newsgroup-name gnus-current-article))
1880                 (unless (vectorp gnus-current-headers)
1881                   (setq gnus-current-headers nil))
1882                 (gnus-summary-show-thread)
1883                 (run-hooks 'gnus-mark-article-hook)
1884                 (gnus-set-mode-line 'summary)
1885                 (when (gnus-visual-p 'article-highlight 'highlight)
1886                   (run-hooks 'gnus-visual-mark-article-hook))
1887                 ;; Set the global newsgroup variables here.
1888                 ;; Suggested by Jim Sisolak
1889                 ;; <sisolak@trans4.neep.wisc.edu>.
1890                 (gnus-set-global-variables)
1891                 (setq gnus-have-all-headers
1892                       (or all-headers gnus-show-all-headers))
1893                 (and gnus-use-cache
1894                      (vectorp (gnus-summary-article-header article))
1895                      (gnus-cache-possibly-enter-article
1896                       group article
1897                       (gnus-summary-article-header article)
1898                       (memq article gnus-newsgroup-marked)
1899                       (memq article gnus-newsgroup-dormant)
1900                       (memq article gnus-newsgroup-unreads)))))
1901             (when (or (numberp article)
1902                       (stringp article))
1903               ;; Hooks for getting information from the article.
1904               ;; This hook must be called before being narrowed.
1905               (let (buffer-read-only)
1906                 (run-hooks 'internal-hook)
1907                 (run-hooks 'gnus-article-prepare-hook)
1908                 ;; Decode MIME message.
1909                 (when gnus-show-mime
1910                   (if (or (not gnus-strict-mime)
1911                           (gnus-fetch-field "Mime-Version"))
1912                       (funcall gnus-show-mime-method)
1913                     (funcall gnus-decode-encoded-word-method)))
1914                 ;; Perform the article display hooks.
1915                 (run-hooks 'gnus-article-display-hook))
1916               ;; Do page break.
1917               (goto-char (point-min))
1918               (when gnus-break-pages
1919                 (gnus-narrow-to-page)))
1920             (gnus-set-mode-line 'article)
1921             (gnus-configure-windows 'article)
1922             (goto-char (point-min))
1923             t))))))
1924
1925 (defun gnus-article-wash-status ()
1926   "Return a string which display status of article washing."
1927   (save-excursion
1928     (set-buffer gnus-article-buffer)
1929     (let ((cite (gnus-article-hidden-text-p 'cite))
1930           (headers (gnus-article-hidden-text-p 'headers))
1931           (boring (gnus-article-hidden-text-p 'boring-headers))
1932           (pgp (gnus-article-hidden-text-p 'pgp))
1933           (pem (gnus-article-hidden-text-p 'pem))
1934           (signature (gnus-article-hidden-text-p 'signature))
1935           (overstrike (gnus-article-hidden-text-p 'overstrike))
1936           (emphasis (gnus-article-hidden-text-p 'emphasis))
1937           (mime gnus-show-mime))
1938       (format "%c%c%c%c%c%c%c"
1939               (if cite ?c ? )
1940               (if (or headers boring) ?h ? )
1941               (if (or pgp pem) ?p ? )
1942               (if signature ?s ? )
1943               (if overstrike ?o ? )
1944               (if mime ?m ? )
1945               (if emphasis ?e ? )))))
1946
1947 (defun gnus-article-hide-headers-if-wanted ()
1948   "Hide unwanted headers if `gnus-have-all-headers' is nil.
1949 Provided for backwards compatibility."
1950   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
1951       gnus-inhibit-hiding
1952       (gnus-article-hide-headers)))
1953
1954 ;;; Article savers.
1955
1956 (defun gnus-output-to-rmail (file-name)
1957   "Append the current article to an Rmail file named FILE-NAME."
1958   (require 'rmail)
1959   ;; Most of these codes are borrowed from rmailout.el.
1960   (setq file-name (expand-file-name file-name))
1961   (setq rmail-default-rmail-file file-name)
1962   (let ((artbuf (current-buffer))
1963         (tmpbuf (get-buffer-create " *Gnus-output*")))
1964     (save-excursion
1965       (or (get-file-buffer file-name)
1966           (file-exists-p file-name)
1967           (if (gnus-yes-or-no-p
1968                (concat "\"" file-name "\" does not exist, create it? "))
1969               (let ((file-buffer (create-file-buffer file-name)))
1970                 (save-excursion
1971                   (set-buffer file-buffer)
1972                   (rmail-insert-rmail-file-header)
1973                   (let ((require-final-newline nil))
1974                     (gnus-write-buffer file-name)))
1975                 (kill-buffer file-buffer))
1976             (error "Output file does not exist")))
1977       (set-buffer tmpbuf)
1978       (buffer-disable-undo (current-buffer))
1979       (erase-buffer)
1980       (insert-buffer-substring artbuf)
1981       (gnus-convert-article-to-rmail)
1982       ;; Decide whether to append to a file or to an Emacs buffer.
1983       (let ((outbuf (get-file-buffer file-name)))
1984         (if (not outbuf)
1985             (append-to-file (point-min) (point-max) file-name)
1986           ;; File has been visited, in buffer OUTBUF.
1987           (set-buffer outbuf)
1988           (let ((buffer-read-only nil)
1989                 (msg (and (boundp 'rmail-current-message)
1990                           (symbol-value 'rmail-current-message))))
1991             ;; If MSG is non-nil, buffer is in RMAIL mode.
1992             (when msg
1993               (widen)
1994               (narrow-to-region (point-max) (point-max)))
1995             (insert-buffer-substring tmpbuf)
1996             (when msg
1997               (goto-char (point-min))
1998               (widen)
1999               (search-backward "\^_")
2000               (narrow-to-region (point) (point-max))
2001               (goto-char (1+ (point-min)))
2002               (rmail-count-new-messages t)
2003               (rmail-show-message msg))))))
2004     (kill-buffer tmpbuf)))
2005
2006 (defun gnus-output-to-file (file-name)
2007   "Append the current article to a file named FILE-NAME."
2008   (let ((artbuf (current-buffer)))
2009     (nnheader-temp-write nil
2010       (insert-buffer-substring artbuf)
2011       ;; Append newline at end of the buffer as separator, and then
2012       ;; save it to file.
2013       (goto-char (point-max))
2014       (insert "\n")
2015       (append-to-file (point-min) (point-max) file-name))))
2016
2017 (defun gnus-convert-article-to-rmail ()
2018   "Convert article in current buffer to Rmail message format."
2019   (let ((buffer-read-only nil))
2020     ;; Convert article directly into Babyl format.
2021     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
2022     (goto-char (point-min))
2023     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
2024     (while (search-forward "\n\^_" nil t) ;single char
2025       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
2026     (goto-char (point-max))
2027     (insert "\^_")))
2028
2029 (defun gnus-narrow-to-page (&optional arg)
2030   "Narrow the article buffer to a page.
2031 If given a numerical ARG, move forward ARG pages."
2032   (interactive "P")
2033   (setq arg (if arg (prefix-numeric-value arg) 0))
2034   (save-excursion
2035     (set-buffer gnus-article-buffer)
2036     (goto-char (point-min))
2037     (widen)
2038     ;; Remove any old next/prev buttons.
2039     (when (gnus-visual-p 'page-marker)
2040       (let ((buffer-read-only nil))
2041         (gnus-remove-text-with-property 'gnus-prev)
2042         (gnus-remove-text-with-property 'gnus-next)))
2043     (when
2044         (cond ((< arg 0)
2045                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
2046               ((> arg 0)
2047                (re-search-forward page-delimiter nil 'move arg)))
2048       (goto-char (match-end 0)))
2049     (narrow-to-region
2050      (point)
2051      (if (re-search-forward page-delimiter nil 'move)
2052          (match-beginning 0)
2053        (point)))
2054     (when (and (gnus-visual-p 'page-marker)
2055                (not (= (point-min) 1)))
2056       (save-excursion
2057         (goto-char (point-min))
2058         (gnus-insert-prev-page-button)))
2059     (when (and (gnus-visual-p 'page-marker)
2060                (< (+ (point-max) 2) (buffer-size)))
2061       (save-excursion
2062         (goto-char (point-max))
2063         (gnus-insert-next-page-button)))))
2064
2065 ;; Article mode commands
2066
2067 (defun gnus-article-goto-next-page ()
2068   "Show the next page of the article."
2069   (interactive)
2070   (when (gnus-article-next-page)
2071     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
2072
2073 (defun gnus-article-goto-prev-page ()
2074   "Show the next page of the article."
2075   (interactive)
2076   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
2077     (gnus-article-prev-page nil)))
2078
2079 (defun gnus-article-next-page (&optional lines)
2080   "Show the next page of the current article.
2081 If end of article, return non-nil.  Otherwise return nil.
2082 Argument LINES specifies lines to be scrolled up."
2083   (interactive "p")
2084   (move-to-window-line -1)
2085   (if (save-excursion
2086         (end-of-line)
2087         (and (pos-visible-in-window-p)  ;Not continuation line.
2088              (eobp)))
2089       ;; Nothing in this page.
2090       (if (or (not gnus-break-pages)
2091               (save-excursion
2092                 (save-restriction
2093                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
2094           t                             ;Nothing more.
2095         (gnus-narrow-to-page 1)         ;Go to next page.
2096         nil)
2097     ;; More in this page.
2098     (let ((scroll-in-place nil))
2099       (condition-case ()
2100           (scroll-up lines)
2101         (end-of-buffer
2102          ;; Long lines may cause an end-of-buffer error.
2103          (goto-char (point-max)))))
2104     (move-to-window-line 0)
2105     nil))
2106
2107 (defun gnus-article-prev-page (&optional lines)
2108   "Show previous page of current article.
2109 Argument LINES specifies lines to be scrolled down."
2110   (interactive "p")
2111   (move-to-window-line 0)
2112   (if (and gnus-break-pages
2113            (bobp)
2114            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
2115       (progn
2116         (gnus-narrow-to-page -1)        ;Go to previous page.
2117         (goto-char (point-max))
2118         (recenter -1))
2119     (let ((scroll-in-place nil))
2120       (prog1
2121           (ignore-errors
2122             (scroll-down lines))
2123         (move-to-window-line 0)))))
2124
2125 (defun gnus-article-refer-article ()
2126   "Read article specified by message-id around point."
2127   (interactive)
2128   (let ((point (point)))
2129     (search-forward ">" nil t)          ;Move point to end of "<....>".
2130     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
2131         (let ((message-id (match-string 1)))
2132           (goto-char point)
2133           (set-buffer gnus-summary-buffer)
2134           (gnus-summary-refer-article message-id))
2135       (goto-char (point))
2136       (error "No references around point"))))
2137
2138 (defun gnus-article-show-summary ()
2139   "Reconfigure windows to show summary buffer."
2140   (interactive)
2141   (gnus-configure-windows 'article)
2142   (gnus-summary-goto-subject gnus-current-article))
2143
2144 (defun gnus-article-describe-briefly ()
2145   "Describe article mode commands briefly."
2146   (interactive)
2147   (gnus-message 6
2148                 (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")))
2149
2150 (defun gnus-article-summary-command ()
2151   "Execute the last keystroke in the summary buffer."
2152   (interactive)
2153   (let ((obuf (current-buffer))
2154         (owin (current-window-configuration))
2155         func)
2156     (switch-to-buffer gnus-summary-buffer 'norecord)
2157     (setq func (lookup-key (current-local-map) (this-command-keys)))
2158     (call-interactively func)
2159     (set-buffer obuf)
2160     (set-window-configuration owin)
2161     (set-window-point (get-buffer-window (current-buffer)) (point))))
2162
2163 (defun gnus-article-summary-command-nosave ()
2164   "Execute the last keystroke in the summary buffer."
2165   (interactive)
2166   (let (func)
2167     (pop-to-buffer gnus-summary-buffer 'norecord)
2168     (setq func (lookup-key (current-local-map) (this-command-keys)))
2169     (call-interactively func)))
2170
2171 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
2172   "Read a summary buffer key sequence and execute it from the article buffer."
2173   (interactive "P")
2174   (let ((nosaves
2175          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
2176            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
2177            "=" "^" "\M-^" "|"))
2178         (nosave-but-article
2179          '("A\r"))
2180         (nosave-in-article
2181          '("\C-d"))
2182         keys)
2183     (save-excursion
2184       (set-buffer gnus-summary-buffer)
2185       (let (gnus-pick-mode)
2186         (push (or key last-command-event) unread-command-events)
2187         (setq keys (read-key-sequence nil))))
2188     (message "")
2189
2190     (if (or (member keys nosaves)
2191             (member keys nosave-but-article)
2192             (member keys nosave-in-article))
2193         (let (func)
2194           (save-window-excursion
2195             (pop-to-buffer gnus-summary-buffer 'norecord)
2196             ;; We disable the pick minor mode commands.
2197             (let (gnus-pick-mode)
2198               (setq func (lookup-key (current-local-map) keys))))
2199           (if (not func)
2200               (ding)
2201             (unless (member keys nosave-in-article)
2202               (set-buffer gnus-summary-buffer))
2203             (call-interactively func))
2204           (when (member keys nosave-but-article)
2205             (pop-to-buffer gnus-article-buffer 'norecord)))
2206       ;; These commands should restore window configuration.
2207       (let ((obuf (current-buffer))
2208             (owin (current-window-configuration))
2209             (opoint (point))
2210             func in-buffer)
2211         (if not-restore-window
2212             (pop-to-buffer gnus-summary-buffer 'norecord)
2213           (switch-to-buffer gnus-summary-buffer 'norecord))
2214         (setq in-buffer (current-buffer))
2215         ;; We disable the pick minor mode commands.
2216         (if (setq func (let (gnus-pick-mode)
2217                          (lookup-key (current-local-map) keys)))
2218             (call-interactively func)
2219           (ding))
2220         (when (eq in-buffer (current-buffer))
2221           (set-buffer obuf)
2222           (unless not-restore-window
2223             (set-window-configuration owin))
2224           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
2225
2226 (defun gnus-article-hide (&optional arg force)
2227   "Hide all the gruft in the current article.
2228 This means that PGP stuff, signatures, cited text and (some)
2229 headers will be hidden.
2230 If given a prefix, show the hidden text instead."
2231   (interactive (list current-prefix-arg 'force))
2232   (gnus-article-hide-headers arg)
2233   (gnus-article-hide-pgp arg)
2234   (gnus-article-hide-citation-maybe arg force)
2235   (gnus-article-hide-signature arg))
2236
2237 (defun gnus-article-maybe-highlight ()
2238   "Do some article highlighting if `article-visual' is non-nil."
2239   (when (gnus-visual-p 'article-highlight 'highlight)
2240     (gnus-article-highlight-some)))
2241
2242 (defun gnus-request-article-this-buffer (article group)
2243   "Get an article and insert it into this buffer."
2244   (let (do-update-line)
2245     (prog1
2246         (save-excursion
2247           (erase-buffer)
2248           (gnus-kill-all-overlays)
2249           (setq group (or group gnus-newsgroup-name))
2250
2251           ;; Open server if it has closed.
2252           (gnus-check-server (gnus-find-method-for-group group))
2253
2254           ;; Using `gnus-request-article' directly will insert the article into
2255           ;; `nntp-server-buffer' - so we'll save some time by not having to
2256           ;; copy it from the server buffer into the article buffer.
2257
2258           ;; We only request an article by message-id when we do not have the
2259           ;; headers for it, so we'll have to get those.
2260           (when (stringp article)
2261             (let ((gnus-override-method gnus-refer-article-method))
2262               (gnus-read-header article)))
2263
2264           ;; If the article number is negative, that means that this article
2265           ;; doesn't belong in this newsgroup (possibly), so we find its
2266           ;; message-id and request it by id instead of number.
2267           (when (and (numberp article)
2268                      gnus-summary-buffer
2269                      (get-buffer gnus-summary-buffer)
2270                      (buffer-name (get-buffer gnus-summary-buffer)))
2271             (save-excursion
2272               (set-buffer gnus-summary-buffer)
2273               (let ((header (gnus-summary-article-header article)))
2274                 (when (< article 0)
2275                   (cond 
2276                    ((memq article gnus-newsgroup-sparse)
2277                     ;; This is a sparse gap article.
2278                     (setq do-update-line article)
2279                     (setq article (mail-header-id header))
2280                     (let ((gnus-override-method gnus-refer-article-method))
2281                       (gnus-read-header article))
2282                     (setq gnus-newsgroup-sparse
2283                           (delq article gnus-newsgroup-sparse)))
2284                    ((vectorp header)
2285                     ;; It's a real article.
2286                     (setq article (mail-header-id header)))
2287                    (t
2288                     ;; It is an extracted pseudo-article.
2289                     (setq article 'pseudo)
2290                     (gnus-request-pseudo-article header))))
2291                 
2292                 (let ((method (gnus-find-method-for-group 
2293                                gnus-newsgroup-name)))
2294                   (if (not (eq (car method) 'nneething))
2295                       ()
2296                     (let ((dir (concat (file-name-as-directory (nth 1 method))
2297                                        (mail-header-subject header))))
2298                       (when (file-directory-p dir)
2299                         (setq article 'nneething)
2300                         (gnus-group-enter-directory dir))))))))
2301
2302           (cond
2303            ;; Refuse to select canceled articles.
2304            ((and (numberp article)
2305                  gnus-summary-buffer
2306                  (get-buffer gnus-summary-buffer)
2307                  (buffer-name (get-buffer gnus-summary-buffer))
2308                  (eq (cdr (save-excursion
2309                             (set-buffer gnus-summary-buffer)
2310                             (assq article gnus-newsgroup-reads)))
2311                      gnus-canceled-mark))
2312             nil)
2313            ;; We first check `gnus-original-article-buffer'.
2314            ((and (get-buffer gnus-original-article-buffer)
2315                  (numberp article)
2316                  (save-excursion
2317                    (set-buffer gnus-original-article-buffer)
2318                    (and (equal (car gnus-original-article) group)
2319                         (eq (cdr gnus-original-article) article))))
2320             (insert-buffer-substring gnus-original-article-buffer)
2321             'article)
2322            ;; Check the backlog.
2323            ((and gnus-keep-backlog
2324                  (gnus-backlog-request-article group article (current-buffer)))
2325             'article)
2326            ;; Check asynchronous pre-fetch.
2327            ((gnus-async-request-fetched-article group article (current-buffer))
2328             (gnus-async-prefetch-next group article gnus-summary-buffer)
2329             'article)
2330            ;; Check the cache.
2331            ((and gnus-use-cache
2332                  (numberp article)
2333                  (gnus-cache-request-article article group))
2334             'article)
2335            ;; Get the article and put into the article buffer.
2336            ((or (stringp article) (numberp article))
2337             (let ((gnus-override-method
2338                    (and (stringp article) gnus-refer-article-method))
2339                   (buffer-read-only nil))
2340               (erase-buffer)
2341               (gnus-kill-all-overlays)
2342               (when (gnus-request-article article group (current-buffer))
2343                 (when (numberp article)
2344                   (gnus-async-prefetch-next group article gnus-summary-buffer)
2345                   (when gnus-keep-backlog
2346                     (gnus-backlog-enter-article 
2347                      group article (current-buffer))))
2348                 'article)))
2349            ;; It was a pseudo.
2350            (t article)))
2351
2352       ;; Take the article from the original article buffer
2353       ;; and place it in the buffer it's supposed to be in.
2354       (when (and (get-buffer gnus-article-buffer)
2355                  ;;(numberp article)
2356                  (equal (buffer-name (current-buffer))
2357                         (buffer-name (get-buffer gnus-article-buffer))))
2358         (save-excursion
2359           (if (get-buffer gnus-original-article-buffer)
2360               (set-buffer (get-buffer gnus-original-article-buffer))
2361             (set-buffer (get-buffer-create gnus-original-article-buffer))
2362             (buffer-disable-undo (current-buffer))
2363             (setq major-mode 'gnus-original-article-mode)
2364             (setq buffer-read-only t)
2365             (gnus-add-current-to-buffer-list))
2366           (let (buffer-read-only)
2367             (erase-buffer)
2368             (insert-buffer-substring gnus-article-buffer))
2369           (setq gnus-original-article (cons group article))))
2370     
2371       ;; Update sparse articles.
2372       (when (and do-update-line
2373                  (or (numberp article)
2374                      (stringp article)))
2375         (let ((buf (current-buffer)))
2376           (set-buffer gnus-summary-buffer)
2377           (gnus-summary-update-article do-update-line)
2378           (gnus-summary-goto-subject do-update-line nil t)
2379           (set-window-point (get-buffer-window (current-buffer) t)
2380                             (point))
2381           (set-buffer buf))))))
2382
2383 ;;;
2384 ;;; Article editing
2385 ;;;
2386
2387 (defcustom gnus-article-edit-mode-hook nil
2388   "Hook run in article edit mode buffers."
2389   :group 'gnus-article-various
2390   :type 'hook)
2391
2392 (defvar gnus-article-edit-done-function nil)
2393
2394 (defvar gnus-article-edit-mode-map nil)
2395
2396 (unless gnus-article-edit-mode-map 
2397   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
2398
2399   (gnus-define-keys gnus-article-edit-mode-map
2400     "\C-c\C-c" gnus-article-edit-done
2401     "\C-c\C-k" gnus-article-edit-exit)
2402
2403   (gnus-define-keys (gnus-article-edit-wash-map
2404                      "\C-c\C-w" gnus-article-edit-mode-map)
2405     "f" gnus-article-edit-full-stops))
2406
2407 (defun gnus-article-edit-mode ()
2408   "Major mode for editing articles.
2409 This is an extended text-mode.
2410
2411 \\{gnus-article-edit-mode-map}"
2412   (interactive)
2413   (kill-all-local-variables)
2414   (setq major-mode 'gnus-article-edit-mode)
2415   (setq mode-name "Article Edit")
2416   (use-local-map gnus-article-edit-mode-map)
2417   (make-local-variable 'gnus-article-edit-done-function)
2418   (make-local-variable 'gnus-prev-winconf)
2419   (setq buffer-read-only nil)
2420   (buffer-enable-undo)
2421   (widen)
2422   (run-hooks 'text-mode 'gnus-article-edit-mode-hook))
2423
2424 (defun gnus-article-edit (&optional force)
2425   "Edit the current article.
2426 This will have permanent effect only in mail groups.
2427 If FORCE is non-nil, allow editing of articles even in read-only
2428 groups."
2429   (interactive "P")
2430   (when (and (not force)
2431              (gnus-group-read-only-p))
2432     (error "The current newsgroup does not support article editing."))
2433   (gnus-article-edit-article
2434    `(lambda ()
2435       (gnus-summary-edit-article-done
2436        ,(or (mail-header-references gnus-current-headers) "")
2437        ,(gnus-group-read-only-p) ,gnus-summary-buffer))))
2438
2439 (defun gnus-article-edit-article (exit-func)
2440   "Start editing the contents of the current article buffer."
2441   (let ((winconf (current-window-configuration)))
2442     (set-buffer gnus-article-buffer)
2443     (gnus-article-edit-mode)
2444     (set-text-properties (point-min) (point-max) nil)
2445     (gnus-configure-windows 'edit-article)
2446     (setq gnus-article-edit-done-function exit-func)
2447     (setq gnus-prev-winconf winconf)
2448     (gnus-message 6 "C-c C-c to end edits")))
2449
2450 (defun gnus-article-edit-done ()
2451   "Update the article edits and exit."
2452   (interactive)
2453   (let ((func gnus-article-edit-done-function)
2454         (buf (current-buffer))
2455         (start (window-start)))
2456     (gnus-article-edit-exit)
2457     (save-excursion
2458       (set-buffer buf)
2459       (let ((buffer-read-only nil))
2460         (funcall func)))
2461     (set-buffer buf)
2462     (set-window-start (get-buffer-window buf) start)
2463     (set-window-point (get-buffer-window buf) (point))))
2464
2465 (defun gnus-article-edit-exit ()
2466   "Exit the article editing without updating."
2467   (interactive)
2468   ;; We remove all text props from the article buffer.
2469   (let ((buf (format "%s" (buffer-string)))
2470         (curbuf (current-buffer))
2471         (p (point))
2472         (window-start (window-start)))
2473     (erase-buffer)
2474     (insert buf)
2475     (let ((winconf gnus-prev-winconf))
2476       (gnus-article-mode)
2477       ;; The cache and backlog have to be flushed somewhat.
2478       (when gnus-use-cache
2479         (gnus-cache-update-article      
2480          (car gnus-article-current) (cdr gnus-article-current)))
2481       (when gnus-keep-backlog
2482         (gnus-backlog-remove-article 
2483          (car gnus-article-current) (cdr gnus-article-current)))
2484       ;; Flush original article as well.
2485       (save-excursion
2486         (when (get-buffer gnus-original-article-buffer)
2487           (set-buffer gnus-original-article-buffer)
2488           (setq gnus-original-article nil)))
2489       (set-window-configuration winconf)
2490       ;; Tippy-toe some to make sure that point remains where it was.
2491       (let ((buf (current-buffer)))
2492         (set-buffer curbuf)
2493         (set-window-start (get-buffer-window (current-buffer)) window-start)
2494         (goto-char p)
2495         (set-buffer buf)))))
2496       
2497 (defun gnus-article-edit-full-stops ()
2498   "Interactively repair spacing at end of sentences."
2499   (interactive)
2500   (save-excursion
2501     (goto-char (point-min))
2502     (search-forward-regexp "^$" nil t)
2503     (let ((case-fold-search nil))
2504       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
2505
2506 ;;; 
2507 ;;; Article highlights
2508 ;;;
2509
2510 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
2511
2512 ;;; Internal Variables:
2513
2514 (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\\)"
2515   "Regular expression that matches URLs."
2516   :group 'gnus-article-buttons
2517   :type 'regexp)
2518
2519 (defcustom gnus-button-alist 
2520   `(("\\(\\b<?\\(url: ?\\)?news:\\([^>\n\t ]*\\)>?\\)" 1 t
2521      gnus-button-message-id 3)
2522     ("\\(\\b<?\\(url: ?\\)?news:\\(//\\)?\\([^>\n\t ]*\\)>?\\)" 1 t
2523      gnus-button-fetch-group 4)
2524     ("\\bin\\( +article\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2 
2525      t gnus-button-message-id 3)
2526     ("\\(<URL: *\\)?mailto: *\\([^> \n\t]+\\)>?" 0 t gnus-url-mailto 2)
2527     ;; This is how URLs _should_ be embedded in text...
2528     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
2529     ;; Raw URLs.
2530     (,gnus-button-url-regexp 0 t gnus-button-url 0))
2531   "Alist of regexps matching buttons in article bodies.
2532
2533 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
2534 REGEXP: is the string matching text around the button,
2535 BUTTON: is the number of the regexp grouping actually matching the button,
2536 FORM: is a lisp expression which must eval to true for the button to
2537 be added, 
2538 CALLBACK: is the function to call when the user push this button, and each
2539 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
2540
2541 CALLBACK can also be a variable, in that case the value of that
2542 variable it the real callback function."
2543   :group 'gnus-article-buttons
2544   :type '(repeat (list regexp 
2545                        (integer :tag "Button")
2546                        (sexp :tag "Form")
2547                        (function :tag "Callback")
2548                        (repeat :tag "Par"
2549                                :inline t
2550                                (integer :tag "Regexp group")))))
2551
2552 (defcustom gnus-header-button-alist 
2553   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
2554      0 t gnus-button-message-id 0)
2555     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
2556     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+" 
2557      0 t gnus-button-mailto 0)
2558     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2559     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2560     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
2561      gnus-button-message-id 3))
2562   "Alist of headers and regexps to match buttons in article heads.
2563
2564 This alist is very similar to `gnus-button-alist', except that each
2565 alist has an additional HEADER element first in each entry:
2566
2567 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
2568
2569 HEADER is a regexp to match a header.  For a fuller explanation, see
2570 `gnus-button-alist'."
2571   :group 'gnus-article-buttons
2572   :group 'gnus-article-headers
2573   :type '(repeat (list (regexp :tag "Header")
2574                        regexp 
2575                        (integer :tag "Button")
2576                        (sexp :tag "Form")
2577                        (function :tag "Callback")
2578                        (repeat :tag "Par"
2579                                :inline t
2580                                (integer :tag "Regexp group")))))
2581
2582 (defvar gnus-button-regexp nil)
2583 (defvar gnus-button-marker-list nil)
2584 ;; Regexp matching any of the regexps from `gnus-button-alist'.
2585
2586 (defvar gnus-button-last nil)
2587 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
2588
2589 ;;; Commands:
2590
2591 (defun gnus-article-push-button (event)
2592   "Check text under the mouse pointer for a callback function.
2593 If the text under the mouse pointer has a `gnus-callback' property,
2594 call it with the value of the `gnus-data' text property."
2595   (interactive "e")
2596   (set-buffer (window-buffer (posn-window (event-start event))))
2597   (let* ((pos (posn-point (event-start event)))
2598          (data (get-text-property pos 'gnus-data))
2599          (fun (get-text-property pos 'gnus-callback)))
2600     (when fun
2601       (funcall fun data))))
2602
2603 (defun gnus-article-press-button ()
2604   "Check text at point for a callback function.
2605 If the text at point has a `gnus-callback' property,
2606 call it with the value of the `gnus-data' text property."
2607   (interactive)
2608   (let* ((data (get-text-property (point) 'gnus-data))
2609          (fun (get-text-property (point) 'gnus-callback)))
2610     (when fun
2611       (funcall fun data))))
2612
2613 (defun gnus-article-prev-button (n)
2614   "Move point to N buttons backward.
2615 If N is negative, move forward instead."
2616   (interactive "p")
2617   (gnus-article-next-button (- n)))
2618
2619 (defun gnus-article-next-button (n)
2620   "Move point to N buttons forward.
2621 If N is negative, move backward instead."
2622   (interactive "p")
2623   (let ((function (if (< n 0) 'previous-single-property-change
2624                     'next-single-property-change))
2625         (inhibit-point-motion-hooks t)
2626         (backward (< n 0))
2627         (limit (if (< n 0) (point-min) (point-max))))
2628     (setq n (abs n))
2629     (while (and (not (= limit (point)))
2630                 (> n 0))
2631       ;; Skip past the current button.
2632       (when (get-text-property (point) 'gnus-callback)
2633         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2634       ;; Go to the next (or previous) button.
2635       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
2636       ;; Put point at the start of the button.
2637       (when (and backward (not (get-text-property (point) 'gnus-callback)))
2638         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2639       ;; Skip past intangible buttons.
2640       (when (get-text-property (point) 'intangible)
2641         (incf n))
2642       (decf n))
2643     (unless (zerop n)
2644       (gnus-message 5 "No more buttons"))
2645     n))
2646
2647 (defun gnus-article-highlight (&optional force)
2648   "Highlight current article.
2649 This function calls `gnus-article-highlight-headers',
2650 `gnus-article-highlight-citation', 
2651 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2652 do the highlighting.  See the documentation for those functions."
2653   (interactive (list 'force))
2654   (gnus-article-highlight-headers)
2655   (gnus-article-highlight-citation force)
2656   (gnus-article-highlight-signature)
2657   (gnus-article-add-buttons force)
2658   (gnus-article-add-buttons-to-head))
2659
2660 (defun gnus-article-highlight-some (&optional force)
2661   "Highlight current article.
2662 This function calls `gnus-article-highlight-headers',
2663 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2664 do the highlighting.  See the documentation for those functions."
2665   (interactive (list 'force))
2666   (gnus-article-highlight-headers)
2667   (gnus-article-highlight-signature)
2668   (gnus-article-add-buttons))
2669
2670 (defun gnus-article-highlight-headers ()
2671   "Highlight article headers as specified by `gnus-header-face-alist'."
2672   (interactive)
2673   (save-excursion
2674     (set-buffer gnus-article-buffer)
2675     (save-restriction
2676       (let ((alist gnus-header-face-alist)
2677             (buffer-read-only nil)
2678             (case-fold-search t)
2679             (inhibit-point-motion-hooks t)
2680             entry regexp header-face field-face from hpoints fpoints)
2681         (goto-char (point-min))
2682         (when (search-forward "\n\n" nil t)
2683           (narrow-to-region (1- (point)) (point-min))
2684           (while (setq entry (pop alist))
2685             (goto-char (point-min))
2686             (setq regexp (concat "^\\("
2687                                  (if (string-equal "" (nth 0 entry))
2688                                      "[^\t ]"
2689                                    (nth 0 entry))
2690                                  "\\)")
2691                   header-face (nth 1 entry)
2692                   field-face (nth 2 entry))
2693             (while (and (re-search-forward regexp nil t)
2694                         (not (eobp)))
2695               (beginning-of-line)
2696               (setq from (point))
2697               (unless (search-forward ":" nil t)
2698                 (forward-char 1))
2699               (when (and header-face
2700                          (not (memq (point) hpoints)))
2701                 (push (point) hpoints)
2702                 (gnus-put-text-property from (point) 'face header-face))
2703               (when (and field-face
2704                          (not (memq (setq from (point)) fpoints)))
2705                 (push from fpoints)
2706                 (if (re-search-forward "^[^ \t]" nil t)
2707                     (forward-char -2)
2708                   (goto-char (point-max)))
2709                 (gnus-put-text-property from (point) 'face field-face)))))))))
2710
2711 (defun gnus-article-highlight-signature ()
2712   "Highlight the signature in an article.
2713 It does this by highlighting everything after
2714 `gnus-signature-separator' using `gnus-signature-face'." 
2715   (interactive)
2716   (save-excursion
2717     (set-buffer gnus-article-buffer)
2718     (let ((buffer-read-only nil)
2719           (inhibit-point-motion-hooks t))
2720       (save-restriction
2721         (when (and gnus-signature-face
2722                    (gnus-article-narrow-to-signature))
2723           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
2724                             'face gnus-signature-face)
2725           (widen)
2726           (gnus-article-search-signature)
2727           (let ((start (match-beginning 0))
2728                 (end (set-marker (make-marker) (1+ (match-end 0)))))
2729             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
2730                                      end)))))))
2731
2732 (defun gnus-article-add-buttons (&optional force)
2733   "Find external references in the article and make buttons of them.
2734 \"External references\" are things like Message-IDs and URLs, as
2735 specified by `gnus-button-alist'."
2736   (interactive (list 'force))
2737   (save-excursion
2738     (set-buffer gnus-article-buffer)
2739     ;; Remove all old markers.
2740     (while gnus-button-marker-list
2741       (set-marker (pop gnus-button-marker-list) nil))
2742     (let ((buffer-read-only nil)
2743           (inhibit-point-motion-hooks t)
2744           (case-fold-search t)
2745           (alist gnus-button-alist)
2746           beg entry regexp)
2747       (goto-char (point-min))
2748       ;; We skip the headers.
2749       (unless (search-forward "\n\n" nil t)
2750         (goto-char (point-max)))
2751       (setq beg (point))
2752       (while (setq entry (pop alist))
2753         (setq regexp (car entry))
2754         (goto-char beg)
2755         (while (re-search-forward regexp nil t)
2756           (let* ((start (and entry (match-beginning (nth 1 entry))))
2757                  (end (and entry (match-end (nth 1 entry))))
2758                  (from (match-beginning 0)))
2759             (when (and (or (eq t (nth 1 entry))
2760                            (eval (nth 1 entry)))
2761                        (not (get-text-property (point) 'gnus-callback)))
2762               ;; That optional form returned non-nil, so we add the
2763               ;; button. 
2764               (gnus-article-add-button 
2765                start end 'gnus-button-push 
2766                (car (push (set-marker (make-marker) from)
2767                           gnus-button-marker-list))))))))))
2768
2769 ;; Add buttons to the head of an article.
2770 (defun gnus-article-add-buttons-to-head ()
2771   "Add buttons to the head of the article."
2772   (interactive)
2773   (save-excursion
2774     (set-buffer gnus-article-buffer)
2775     (let ((buffer-read-only nil)
2776           (inhibit-point-motion-hooks t)
2777           (case-fold-search t)
2778           (alist gnus-header-button-alist)
2779           entry beg end)
2780       (nnheader-narrow-to-headers)
2781       (while alist
2782         ;; Each alist entry.
2783         (setq entry (car alist)
2784               alist (cdr alist))
2785         (goto-char (point-min))
2786         (while (re-search-forward (car entry) nil t)
2787           ;; Each header matching the entry.
2788           (setq beg (match-beginning 0))
2789           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
2790                              (match-beginning 0))
2791                         (point-max)))
2792           (goto-char beg)
2793           (while (re-search-forward (nth 1 entry) end t)
2794             ;; Each match within a header.
2795             (let* ((entry (cdr entry))
2796                    (start (match-beginning (nth 1 entry)))
2797                    (end (match-end (nth 1 entry)))
2798                    (form (nth 2 entry)))
2799               (goto-char (match-end 0))
2800               (when (eval form)
2801                 (gnus-article-add-button 
2802                  start end (nth 3 entry)
2803                  (buffer-substring (match-beginning (nth 4 entry))
2804                                    (match-end (nth 4 entry)))))))
2805           (goto-char end))))
2806     (widen)))
2807
2808 ;;; External functions:
2809
2810 (defun gnus-article-add-button (from to fun &optional data)
2811   "Create a button between FROM and TO with callback FUN and data DATA."
2812   (when gnus-article-button-face
2813     (gnus-overlay-put (gnus-make-overlay from to)
2814                       'face gnus-article-button-face))
2815   (gnus-add-text-properties 
2816    from to
2817    (nconc (and gnus-article-mouse-face
2818                (list gnus-mouse-face-prop gnus-article-mouse-face))
2819           (list 'gnus-callback fun)
2820           (and data (list 'gnus-data data)))))
2821
2822 ;;; Internal functions:
2823
2824 (defun gnus-signature-toggle (end)
2825   (save-excursion
2826     (set-buffer gnus-article-buffer)
2827     (let ((buffer-read-only nil)
2828           (inhibit-point-motion-hooks t))
2829       (if (get-text-property end 'invisible)
2830           (gnus-article-unhide-text end (point-max))
2831         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
2832
2833 (defun gnus-button-entry ()
2834   ;; Return the first entry in `gnus-button-alist' matching this place.
2835   (let ((alist gnus-button-alist)
2836         (entry nil))
2837     (while alist
2838       (setq entry (pop alist))
2839       (if (looking-at (car entry))
2840           (setq alist nil)
2841         (setq entry nil)))
2842     entry))
2843
2844 (defun gnus-button-push (marker)
2845   ;; Push button starting at MARKER.
2846   (save-excursion
2847     (set-buffer gnus-article-buffer)
2848     (goto-char marker)
2849     (let* ((entry (gnus-button-entry))
2850            (inhibit-point-motion-hooks t)
2851            (fun (nth 3 entry))
2852            (args (mapcar (lambda (group)
2853                            (let ((string (match-string group)))
2854                              (gnus-set-text-properties
2855                               0 (length string) nil string)
2856                              string))
2857                          (nthcdr 4 entry))))
2858       (cond
2859        ((fboundp fun)
2860         (apply fun args))
2861        ((and (boundp fun)
2862              (fboundp (symbol-value fun)))
2863         (apply (symbol-value fun) args))
2864        (t
2865         (gnus-message 1 "You must define `%S' to use this button"
2866                       (cons fun args)))))))
2867
2868 (defun gnus-button-message-id (message-id)
2869   "Fetch MESSAGE-ID."
2870   (save-excursion
2871     (set-buffer gnus-summary-buffer)
2872     (gnus-summary-refer-article message-id)))
2873
2874 (defun gnus-button-fetch-group (address)
2875   "Fetch GROUP specified by ADDRESS."
2876   (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\(.*\\)$" address))
2877       (error "Can't parse %s" address)
2878     (gnus-group-read-ephemeral-group
2879      (match-string 4 address)
2880      `(nntp ,(match-string 1 address) (nntp-address ,(match-string 1 address))
2881             (nntp-port-number ,(if (match-end 3)
2882                                    (match-string 3 address)
2883                                  "nntp"))))))
2884
2885 (defun gnus-split-string (string pattern)
2886   "Return a list of substrings of STRING which are separated by PATTERN."
2887   (let (parts (start 0))
2888     (while (string-match pattern string start)
2889       (setq parts (cons (substring string start (match-beginning 0)) parts)
2890             start (match-end 0)))
2891     (nreverse (cons (substring string start) parts))))
2892  
2893 (defun gnus-url-parse-query-string (query &optional downcase)
2894   (let (retval pairs cur key val)
2895     (setq pairs (gnus-split-string query "&"))
2896     (while pairs
2897       (setq cur (car pairs)
2898             pairs (cdr pairs))
2899       (if (not (string-match "=" cur))
2900           nil                           ; Grace
2901         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
2902               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
2903         (if downcase
2904             (setq key (downcase key)))
2905         (setq cur (assoc key retval))
2906         (if cur
2907             (setcdr cur (cons val (cdr cur)))
2908           (setq retval (cons (list key val) retval)))))
2909     retval))
2910  
2911 (defun gnus-url-unhex (x)
2912   (if (> x ?9)
2913       (if (>= x ?a)
2914           (+ 10 (- x ?a))
2915         (+ 10 (- x ?A)))
2916     (- x ?0)))
2917  
2918 (defun gnus-url-unhex-string (str &optional allow-newlines)
2919   "Remove %XXX embedded spaces, etc in a url.
2920 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
2921 decoding of carriage returns and line feeds in the string, which is normally
2922 forbidden in URL encoding."
2923   (setq str (or str ""))
2924   (let ((tmp "")
2925         (case-fold-search t))
2926     (while (string-match "%[0-9a-f][0-9a-f]" str)
2927       (let* ((start (match-beginning 0))
2928              (ch1 (gnus-url-unhex (elt str (+ start 1))))
2929              (code (+ (* 16 ch1)
2930                       (gnus-url-unhex (elt str (+ start 2))))))
2931         (setq tmp (concat 
2932                    tmp (substring str 0 start)
2933                    (cond
2934                     (allow-newlines
2935                      (char-to-string code))
2936                     ((or (= code ?\n) (= code ?\r))
2937                      " ")
2938                     (t (char-to-string code))))
2939               str (substring str (match-end 0)))))
2940     (setq tmp (concat tmp str))
2941     tmp))
2942  
2943 (defun gnus-url-mailto (url)
2944   ;; Send mail to someone
2945   (when (string-match "mailto:/*\\(.*\\)" url)
2946     (setq url (substring url (match-beginning 1) nil)))
2947   (let (to args source-url subject func)
2948     (if (string-match (regexp-quote "?") url)
2949         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
2950               args (gnus-url-parse-query-string
2951                     (substring url (match-end 0) nil) t))
2952       (setq to (gnus-url-unhex-string url)))
2953     (setq args (cons (list "to" to) args)
2954           subject (cdr-safe (assoc "subject" args)))
2955     (message-mail)
2956     (while args
2957       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
2958       (if (fboundp func)
2959           (funcall func)
2960         (message-position-on-field (caar args)))
2961       (insert (mapconcat 'identity (cdar args) ", "))
2962       (setq args (cdr args)))
2963     (if subject
2964         (message-goto-body)
2965       (message-goto-subject))))
2966
2967 (defun gnus-button-mailto (address)
2968   ;; Mail to ADDRESS.
2969   (set-buffer (gnus-copy-article-buffer))
2970   (message-reply address))
2971
2972 (defun gnus-button-reply (address)
2973   ;; Reply to ADDRESS.
2974   (message-reply address))
2975
2976 (defun gnus-button-url (address)
2977   "Browse ADDRESS."
2978   (funcall browse-url-browser-function address))
2979
2980 (defun gnus-button-embedded-url (address)
2981   "Browse ADDRESS."
2982   (funcall browse-url-browser-function (gnus-strip-whitespace address)))
2983
2984 ;;; Next/prev buttons in the article buffer.
2985
2986 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
2987 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
2988
2989 (defvar gnus-prev-page-map nil)
2990 (unless gnus-prev-page-map
2991   (setq gnus-prev-page-map (make-sparse-keymap))
2992   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
2993   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
2994
2995 (defun gnus-insert-prev-page-button ()
2996   (let ((buffer-read-only nil))
2997     (gnus-eval-format 
2998      gnus-prev-page-line-format nil
2999      `(gnus-prev t local-map ,gnus-prev-page-map
3000                  gnus-callback gnus-article-button-prev-page))))
3001
3002 (defvar gnus-next-page-map nil)
3003 (unless gnus-next-page-map
3004   (setq gnus-next-page-map (make-keymap))
3005   (suppress-keymap gnus-prev-page-map)
3006   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
3007   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
3008
3009 (defun gnus-button-next-page ()
3010   "Go to the next page."
3011   (interactive)
3012   (let ((win (selected-window)))
3013     (select-window (get-buffer-window gnus-article-buffer t))
3014     (gnus-article-next-page)
3015     (select-window win)))
3016
3017 (defun gnus-button-prev-page ()
3018   "Go to the prev page."
3019   (interactive)
3020   (let ((win (selected-window)))
3021     (select-window (get-buffer-window gnus-article-buffer t))
3022     (gnus-article-prev-page)
3023     (select-window win)))
3024
3025 (defun gnus-insert-next-page-button ()
3026   (let ((buffer-read-only nil))
3027     (gnus-eval-format gnus-next-page-line-format nil
3028                       `(gnus-next t local-map ,gnus-next-page-map
3029                                   gnus-callback 
3030                                   gnus-article-button-next-page))))
3031
3032 (defun gnus-article-button-next-page (arg)
3033   "Go to the next page."
3034   (interactive "P")
3035   (let ((win (selected-window)))
3036     (select-window (get-buffer-window gnus-article-buffer t))
3037     (gnus-article-next-page)
3038     (select-window win)))
3039
3040 (defun gnus-article-button-prev-page (arg)
3041   "Go to the prev page."
3042   (interactive "P")
3043   (let ((win (selected-window)))
3044     (select-window (get-buffer-window gnus-article-buffer t))
3045     (gnus-article-prev-page)
3046     (select-window win))) 
3047
3048 (gnus-ems-redefine)
3049
3050 (provide 'gnus-art)
3051
3052 (run-hooks 'gnus-art-load-hook)
3053
3054 ;;; gnus-art.el ends here