*** empty log message ***
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2 ;; Copyright (C) 1996 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 'gnus)
29 (require 'gnus-sum)
30 (require 'article)
31 (require 'gnus-spec)
32 (require 'gnus-int)
33 (require 'browse-url)
34
35 (defcustom gnus-article-save-directory gnus-directory
36   "*Name of the directory articles will be saved in (default \"~/News\")."
37   :group 'article
38   :type 'directory)
39
40 (defcustom gnus-save-all-headers t
41   "*If non-nil, don't remove any headers before saving."
42   :group 'article
43   :type 'boolean)
44
45 (defcustom gnus-prompt-before-saving 'always
46   "*This variable says how much prompting is to be done when saving articles.
47 If it is nil, no prompting will be done, and the articles will be
48 saved to the default files.  If this variable is `always', each and
49 every article that is saved will be preceded by a prompt, even when
50 saving large batches of articles.  If this variable is neither nil not
51 `always', there the user will be prompted once for a file name for
52 each invocation of the saving commands."
53   :group 'article
54   :type '(choice (item always)
55                  (item :tag "never" nil)
56                  (sexp :tag "once" :format "%t")))
57
58 (defcustom gnus-saved-headers gnus-visible-headers
59   "Headers to keep if `gnus-save-all-headers' is nil.
60 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
61 If that variable is nil, however, all headers that match this regexp
62 will be kept while the rest will be deleted before saving."
63   :group 'article
64   :type '(repeat string))
65
66 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
67   "A function to save articles in your favourite format.
68 The function must be interactively callable (in other words, it must
69 be an Emacs command).
70
71 Gnus provides the following functions:
72
73 * gnus-summary-save-in-rmail (Rmail format)
74 * gnus-summary-save-in-mail (Unix mail format)
75 * gnus-summary-save-in-folder (MH folder)
76 * gnus-summary-save-in-file (article format)
77 * gnus-summary-save-in-vm (use VM's folder format)
78 * gnus-summary-write-to-file (article format -- overwrite)."
79   :group 'article
80   :type '(radio (function-item gnus-summary-save-in-rmail)
81                 (function-item gnus-summary-save-in-mail)
82                 (function-item gnus-summary-save-in-folder)
83                 (function-item gnus-summary-save-in-file)
84                 (function-item gnus-summary-save-in-vm)
85                 (function-item gnus-summary-write-to-file)))
86
87 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
88   "A function generating a file name to save articles in Rmail format.
89 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
90   :group 'article
91   :type 'function)
92
93 (defcustom gnus-mail-save-name 'gnus-plain-save-name
94   "A function generating a file name to save articles in Unix mail format.
95 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
96   :group 'article
97   :type 'function)
98
99 (defcustom gnus-folder-save-name 'gnus-folder-save-name
100   "A function generating a file name to save articles in MH folder.
101 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
102   :group 'article
103   :type 'function)
104
105 (defcustom gnus-file-save-name 'gnus-numeric-save-name
106   "A function generating a file name to save articles in article format.
107 The function is called with NEWSGROUP, HEADERS, and optional
108 LAST-FILE."
109   :group 'article
110   :type 'function)
111
112 (defcustom gnus-split-methods
113   '((gnus-article-archive-name))
114   "Variable used to suggest where articles are to be saved.
115 For instance, if you would like to save articles related to Gnus in
116 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
117 you could set this variable to something like:
118
119  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
120    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
121
122 This variable is an alist where the where the key is the match and the
123 value is a list of possible files to save in if the match is non-nil.
124
125 If the match is a string, it is used as a regexp match on the
126 article.  If the match is a symbol, that symbol will be funcalled
127 from the buffer of the article to be saved with the newsgroup as the
128 parameter.  If it is a list, it will be evaled in the same buffer.
129
130 If this form or function returns a string, this string will be used as
131 a possible file name; and if it returns a non-nil list, that list will
132 be used as possible file names."
133   :group 'article
134   :type '(repeat (choice (list function)
135                          (cons regexp (repeat string))
136                          sexp)))
137
138 (defcustom gnus-strict-mime t
139   "*If nil, MIME-decode even if there is no Mime-Version header."
140   :group 'article
141   :type 'boolean)
142
143 (defcustom gnus-show-mime-method 'metamail-buffer
144   "Function to process a MIME message.
145 The function is called from the article buffer."
146   :group 'article
147   :type 'function)
148
149 (defcustom gnus-decode-encoded-word-method (lambda ())
150   "*Function to decode a MIME encoded-words.
151 The function is called from the article buffer."
152   :group 'article
153   :type 'function)
154
155 (defcustom gnus-page-delimiter "^\^L"
156   "*Regexp describing what to use as article page delimiters.
157 The default value is \"^\^L\", which is a form linefeed at the
158 beginning of a line."
159   :type 'regexp
160   :group 'article)
161
162 (defcustom gnus-article-mode-line-format "Gnus: %%b %S"
163   "*The format specification for the article mode line.
164 See `gnus-summary-mode-line-format' for a closer description."
165   :type 'string
166   :group 'article)
167
168 (defcustom gnus-article-mode-hook nil
169   "*A hook for Gnus article mode."
170   :type 'hook
171   :group 'article)
172
173 (defcustom gnus-article-menu-hook nil
174   "*Hook run after the creation of the article mode menu."
175   :type 'hook
176   :group 'article)
177
178 (defcustom gnus-article-prepare-hook nil
179   "*A hook called after an article has been prepared in the article buffer.
180 If you want to run a special decoding program like nkf, use this hook."
181   :type 'hook
182   :group 'article)
183
184 (defcustom gnus-article-button-face 'bold
185   "Face used for highlighting buttons in the article buffer.
186
187 An article button is a piece of text that you can activate by pressing
188 `RET' or `mouse-2' above it."
189   :type 'face
190   :group 'article)
191
192 (defcustom gnus-article-mouse-face 'highlight
193   "Face used for mouse highlighting in the article buffer.
194
195 Article buttons will be displayed in this face when the cursor is
196 above them."
197   :type 'face
198   :group 'article)
199
200 (defcustom gnus-signature-face 'italic
201   "Face used for highlighting a signature in the article buffer."
202   :type 'face
203   :group 'article)
204
205 (defface gnus-header-from-face 
206   '((((class color)
207       (background dark))
208      (:foreground "light blue" :bold t :italic t))
209     (((class color)
210       (background light))
211      (:foreground "MidnightBlue" :bold t :italic t))
212     (t 
213      (:bold t :italic t)))
214   "Face used for displaying from headers."
215   :group 'article)
216
217 (defface gnus-header-subject-face 
218   '((((class color)
219       (background dark))
220      (:foreground "pink" :bold t :italic t))
221     (((class color)
222       (background light))
223      (:foreground "firebrick" :bold t :italic t))
224     (t 
225      (:bold t :italic t)))
226   "Face used for displaying subject headers."
227   :group 'article)
228
229 (defface gnus-header-newsgroups-face 
230   '((((class color)
231       (background dark))
232      (:foreground "yellow" :bold t :italic t))
233     (((class color)
234       (background light))
235      (:foreground "indianred" :bold t :italic t))
236     (t 
237      (:bold t :italic t)))
238   "Face used for displaying newsgroups headers."
239   :group 'article)
240
241 (defface gnus-header-name-face 
242   '((((class color)
243       (background dark))
244      (:foreground "cyan" :bold t))
245     (((class color)
246       (background light))
247      (:foreground "DarkGreen" :bold t))
248     (t 
249      (:bold t)))
250   "Face used for displaying header names."
251   :group 'article)
252
253 (defface gnus-header-content-face
254   '((((class color)
255       (background dark))
256      (:foreground "forest green" :italic t))
257     (((class color)
258       (background light))
259      (:foreground "DarkGreen" :italic t))
260     (t 
261      (:italic t)))  "Face used for displaying header content."
262   :group 'article)
263
264 (defcustom gnus-header-face-alist
265   '(("From" nil gnus-header-from-face)
266     ("Subject" nil gnus-header-subject-face)
267     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
268     ("" gnus-header-name-face gnus-header-content-face))
269   "Controls highlighting of article header.
270
271 An alist of the form (HEADER NAME CONTENT). 
272
273 HEADER is a regular expression which should match the name of an
274 header header and NAME and CONTENT are either face names or nil.
275
276 The name of each header field will be displayed using the face
277 specified by the first element in the list where HEADER match the
278 header name and NAME is non-nil.  Similarly, the content will be
279 displayed by the first non-nil matching CONTENT face."
280   :group 'article
281   :type '(repeat (list (regexp :tag "Header")
282                        (choice :tag "Name"
283                                (item :tag "skip" nil)
284                                (face :value default))
285                        (choice :tag "Content"
286                                (item :tag "skip" nil)
287                                (face :value default)))))
288
289 ;;; Internal variables
290
291 (defvar gnus-article-mode-line-format-alist
292     (nconc '((?w (gnus-article-wash-status) ?s))
293            gnus-summary-mode-line-format-alist))
294
295 (defvar gnus-number-of-articles-to-be-saved nil)
296
297 ;;; Provide a mapping from `gnus-*' commands to Article commands.
298
299 (eval-and-compile
300   (mapcar
301    (lambda (func)
302      (let (afunc gfunc)
303        (if (consp func)
304            (setq afunc (car func)
305                  gfunc (cdr func))
306          (setq afunc func
307                gfunc (intern (format "gnus-%s" func))))
308        (fset gfunc 
309              `(lambda (&optional interactive &rest args)
310                 ,(documentation afunc t)
311                 (interactive (list t))
312                 (save-excursion
313                   (set-buffer gnus-article-buffer)
314                   (if interactive
315                       (call-interactively ',afunc)
316                     (apply ',afunc args)))))))
317    '(article-hide-headers
318      article-hide-boring-headers
319      article-treat-overstrike
320      (article-fill . gnus-article-word-wrap)
321      article-remove-cr
322      article-display-x-face
323      article-de-quoted-unreadable
324      article-mime-decode-quoted-printable
325      article-hide-pgp
326      article-hide-pem
327      article-hide-signature
328      article-remove-trailing-blank-lines
329      article-strip-leading-blank-lines
330      article-strip-multiple-blank-lines
331      article-strip-blank-lines
332      article-date-local
333      article-date-original
334      article-date-lapsed
335      article-emphasize
336      (article-show-all . gnus-article-show-all-headers))))
337
338 (defalias 'gnus-decode-rfc1522 'article-decode-rfc1522)
339
340 ;;; Saving functions.
341
342 (defun gnus-article-save (save-buffer file &optional num)
343   "Save the currently selected article."
344   (unless gnus-save-all-headers
345     ;; Remove headers according to `gnus-saved-headers'.
346     (let ((gnus-visible-headers
347            (or gnus-saved-headers gnus-visible-headers))
348           (gnus-article-buffer save-buffer))
349       (gnus-article-hide-headers 1 t)))
350   (save-window-excursion
351     (if (not gnus-default-article-saver)
352         (error "No default saver is defined.")
353       ;; !!! Magic!  The saving functions all save
354       ;; `gnus-original-article-buffer' (or so they think),
355       ;; but we bind that variable to our save-buffer.
356       (set-buffer gnus-article-buffer)
357       (let* ((gnus-original-article-buffer save-buffer)
358              (filename
359               (cond
360                ((not gnus-prompt-before-saving)
361                 'default)
362                ((eq gnus-prompt-before-saving 'always)
363                 nil)
364                (t file)))
365              (gnus-number-of-articles-to-be-saved
366               (when (eq gnus-prompt-before-saving t)
367                 num)))                  ; Magic
368         (set-buffer gnus-summary-buffer)
369         (funcall gnus-default-article-saver filename))))) 
370
371 (defun gnus-read-save-file-name (prompt default-name &optional filename)
372   (cond
373    ((eq filename 'default)
374     default-name)
375    (filename filename)
376    (t
377     (let* ((split-name (gnus-get-split-value gnus-split-methods))
378            (prompt
379             (format prompt (if (and gnus-number-of-articles-to-be-saved
380                                     (> gnus-number-of-articles-to-be-saved 1))
381                                (format "these %d articles"
382                                        gnus-number-of-articles-to-be-saved)
383                              "this article")))
384            (file
385             ;; Let the split methods have their say.
386             (cond
387              ;; No split name was found.
388              ((null split-name)
389               (read-file-name
390                (concat prompt " (default "
391                        (file-name-nondirectory default-name) ") ")
392                (file-name-directory default-name)
393                default-name))
394              ;; A single split name was found
395              ((= 1 (length split-name))
396               (let* ((name (car split-name))
397                      (dir (cond ((file-directory-p name)
398                                  (file-name-as-directory name))
399                                 ((file-exists-p name) name)
400                                 (t gnus-article-save-directory))))
401                 (read-file-name
402                  (concat prompt " (default " name ") ")
403                  dir name)))
404              ;; A list of splits was found.
405              (t
406               (setq split-name (nreverse split-name))
407               (let (result)
408                 (let ((file-name-history (nconc split-name file-name-history)))
409                   (setq result
410                         (read-file-name
411                          (concat prompt " (`M-p' for defaults) ")
412                          gnus-article-save-directory
413                          (car split-name))))
414                 (car (push result file-name-history)))))))
415       ;; Create the directory.
416       (gnus-make-directory (file-name-directory file))
417       ;; If we have read a directory, we append the default file name.
418       (when (file-directory-p file)
419         (setq file (concat (file-name-as-directory file)
420                            (file-name-nondirectory default-name))))
421       ;; Possibly translate some characters.
422       (nnheader-translate-file-chars file)))))
423
424 (defun gnus-article-archive-name (group)
425   "Return the first instance of an \"Archive-name\" in the current buffer."
426   (let ((case-fold-search t))
427     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
428       (nnheader-concat gnus-article-save-directory
429                        (match-string 1)))))
430
431 (defun gnus-summary-save-in-rmail (&optional filename)
432   "Append this article to Rmail file.
433 Optional argument FILENAME specifies file name.
434 Directory to save to is default to `gnus-article-save-directory'."
435   (interactive)
436   (gnus-set-global-variables)
437   (let ((default-name
438           (funcall gnus-rmail-save-name gnus-newsgroup-name
439                    gnus-current-headers gnus-newsgroup-last-rmail)))
440     (setq filename (gnus-read-save-file-name
441                     "Save %s in rmail file:" default-name filename))
442     (gnus-make-directory (file-name-directory filename))
443     (gnus-eval-in-buffer-window gnus-original-article-buffer
444       (save-excursion
445         (save-restriction
446           (widen)
447           (gnus-output-to-rmail filename))))
448     ;; Remember the directory name to save articles
449     (setq gnus-newsgroup-last-rmail filename)))
450
451 (defun gnus-summary-save-in-mail (&optional filename)
452   "Append this article to Unix mail file.
453 Optional argument FILENAME specifies file name.
454 Directory to save to is default to `gnus-article-save-directory'."
455   (interactive)
456   (gnus-set-global-variables)
457   (let ((default-name
458           (funcall gnus-mail-save-name gnus-newsgroup-name
459                    gnus-current-headers gnus-newsgroup-last-mail)))
460     (setq filename (gnus-read-save-file-name
461                     "Save %s in Unix mail file:" default-name filename))
462     (setq filename
463           (expand-file-name filename
464                             (and default-name
465                                  (file-name-directory default-name))))
466     (gnus-make-directory (file-name-directory filename))
467     (gnus-eval-in-buffer-window gnus-original-article-buffer
468       (save-excursion
469         (save-restriction
470           (widen)
471           (if (and (file-readable-p filename) (mail-file-babyl-p filename))
472               (gnus-output-to-rmail filename)
473             (let ((mail-use-rfc822 t))
474               (rmail-output filename 1 t t))))))
475     ;; Remember the directory name to save articles.
476     (setq gnus-newsgroup-last-mail filename)))
477
478 (defun gnus-summary-save-in-file (&optional filename overwrite)
479   "Append this article to file.
480 Optional argument FILENAME specifies file name.
481 Directory to save to is default to `gnus-article-save-directory'."
482   (interactive)
483   (gnus-set-global-variables)
484   (let ((default-name
485           (funcall gnus-file-save-name gnus-newsgroup-name
486                    gnus-current-headers gnus-newsgroup-last-file)))
487     (setq filename (gnus-read-save-file-name
488                     "Save %s in file:" default-name filename))
489     (gnus-make-directory (file-name-directory filename))
490     (gnus-eval-in-buffer-window gnus-original-article-buffer
491       (save-excursion
492         (save-restriction
493           (widen)
494           (when (and overwrite
495                      (file-exists-p filename))
496             (delete-file filename))
497           (gnus-output-to-file filename))))
498     ;; Remember the directory name to save articles.
499     (setq gnus-newsgroup-last-file filename)))
500
501 (defun gnus-summary-write-to-file (&optional filename)
502   "Append this article body to a file.
503 Optional argument FILENAME specifies file name.
504 The directory to save in defaults to `gnus-article-save-directory'."
505   (interactive)
506   (gnus-summary-save-in-file nil t))
507
508 (defun gnus-summary-save-in-pipe (&optional command)
509   "Pipe this article to subprocess."
510   (interactive)
511   (gnus-set-global-variables)
512   (setq command
513         (cond ((eq command 'default)
514                gnus-last-shell-command)
515               (command command)
516               (t (read-string "Shell command on article: "
517                               gnus-last-shell-command))))
518   (when (string-equal command "")
519     (setq command gnus-last-shell-command))
520   (gnus-eval-in-buffer-window gnus-article-buffer
521     (save-restriction
522       (widen)
523       (shell-command-on-region (point-min) (point-max) command nil)))
524   (setq gnus-last-shell-command command))
525
526 ;;; Article file names when saving.
527
528 (defun gnus-capitalize-newsgroup (newsgroup)
529   "Capitalize NEWSGROUP name."
530   (when (not (zerop (length newsgroup)))
531     (concat (char-to-string (upcase (aref newsgroup 0)))
532             (substring newsgroup 1))))
533
534 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
535   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
536 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
537 Otherwise, it is like ~/News/news/group/num."
538   (let ((default
539           (expand-file-name
540            (concat (if (gnus-use-long-file-name 'not-save)
541                        (gnus-capitalize-newsgroup newsgroup)
542                      (gnus-newsgroup-directory-form newsgroup))
543                    "/" (int-to-string (mail-header-number headers)))
544            gnus-article-save-directory)))
545     (if (and last-file
546              (string-equal (file-name-directory default)
547                            (file-name-directory last-file))
548              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
549         default
550       (or last-file default))))
551
552 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
553   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
554 If variable `gnus-use-long-file-name' is non-nil, it is
555 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
556   (let ((default
557           (expand-file-name
558            (concat (if (gnus-use-long-file-name 'not-save)
559                        newsgroup
560                      (gnus-newsgroup-directory-form newsgroup))
561                    "/" (int-to-string (mail-header-number headers)))
562            gnus-article-save-directory)))
563     (if (and last-file
564              (string-equal (file-name-directory default)
565                            (file-name-directory last-file))
566              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
567         default
568       (or last-file default))))
569
570 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
571   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
572 If variable `gnus-use-long-file-name' is non-nil, it is
573 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
574   (or last-file
575       (expand-file-name
576        (if (gnus-use-long-file-name 'not-save)
577            (gnus-capitalize-newsgroup newsgroup)
578          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
579        gnus-article-save-directory)))
580
581 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
582   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
583 If variable `gnus-use-long-file-name' is non-nil, it is
584 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
585   (or last-file
586       (expand-file-name
587        (if (gnus-use-long-file-name 'not-save)
588            newsgroup
589          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
590        gnus-article-save-directory)))
591
592 \f
593 ;;;
594 ;;; Gnus article mode
595 ;;;
596
597 (put 'gnus-article-mode 'mode-class 'special)
598
599 (when t
600   (gnus-define-keys gnus-article-mode-map
601     " " gnus-article-goto-next-page
602     "\177" gnus-article-goto-prev-page
603     [delete] gnus-article-goto-prev-page
604     "\C-c^" gnus-article-refer-article
605     "h" gnus-article-show-summary
606     "s" gnus-article-show-summary
607     "\C-c\C-m" gnus-article-mail
608     "?" gnus-article-describe-briefly
609     gnus-mouse-2 gnus-article-push-button
610     "\r" gnus-article-press-button
611     "\t" gnus-article-next-button
612     "\M-\t" gnus-article-prev-button
613     "e" gnus-article-edit
614     "<" beginning-of-buffer
615     ">" end-of-buffer
616     "\C-c\C-i" gnus-info-find-node
617     "\C-c\C-b" gnus-bug
618
619     "\C-d" gnus-article-read-summary-keys
620     "\M-*" gnus-article-read-summary-keys
621     "\M-g" gnus-article-read-summary-keys)
622
623   (substitute-key-definition
624    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
625
626 (defun gnus-article-make-menu-bar ()
627   (gnus-turn-off-edit-menu 'article)
628   (unless (boundp 'gnus-article-article-menu)
629     (easy-menu-define
630      gnus-article-article-menu gnus-article-mode-map ""
631      '("Article"
632        ["Scroll forwards" gnus-article-goto-next-page t]
633        ["Scroll backwards" gnus-article-goto-prev-page t]
634        ["Show summary" gnus-article-show-summary t]
635        ["Fetch Message-ID at point" gnus-article-refer-article t]
636        ["Mail to address at point" gnus-article-mail t]
637        ))
638
639     (easy-menu-define
640      gnus-article-treatment-menu gnus-article-mode-map ""
641      '("Treatment"
642        ["Hide headers" gnus-article-hide-headers t]
643        ["Hide signature" gnus-article-hide-signature t]
644        ["Hide citation" gnus-article-hide-citation t]
645        ["Treat overstrike" gnus-article-treat-overstrike t]
646        ["Remove carriage return" gnus-article-remove-cr t]
647        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
648        ))
649     (run-hooks 'gnus-article-menu-hook)))
650
651 (defun gnus-article-mode ()
652   "Major mode for displaying an article.
653
654 All normal editing commands are switched off.
655
656 The following commands are available in addition to all summary mode
657 commands:
658 \\<gnus-article-mode-map>
659 \\[gnus-article-next-page]\t Scroll the article one page forwards
660 \\[gnus-article-prev-page]\t Scroll the article one page backwards
661 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
662 \\[gnus-article-show-summary]\t Display the summary buffer
663 \\[gnus-article-mail]\t Send a reply to the address near point
664 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
665 \\[gnus-info-find-node]\t Go to the Gnus info node"
666   (interactive)
667   (when (and menu-bar-mode
668              (gnus-visual-p 'article-menu 'menu))
669     (gnus-article-make-menu-bar))
670   (kill-all-local-variables)
671   (gnus-simplify-mode-line)
672   (setq mode-name "Article")
673   (setq major-mode 'gnus-article-mode)
674   (make-local-variable 'minor-mode-alist)
675   (unless (assq 'gnus-show-mime minor-mode-alist)
676     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
677   (use-local-map gnus-article-mode-map)
678   (gnus-update-format-specifications nil 'article-mode)
679   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
680   (gnus-set-default-directory)
681   (buffer-disable-undo (current-buffer))
682   (setq buffer-read-only t)
683   (run-hooks 'gnus-article-mode-hook))
684
685 (defun gnus-article-setup-buffer ()
686   "Initialize the article buffer."
687   (let* ((name (if gnus-single-article-buffer "*Article*"
688                  (concat "*Article " gnus-newsgroup-name "*")))
689          (original
690           (progn (string-match "\\*Article" name)
691                  (concat " *Original Article"
692                          (substring name (match-end 0))))))
693     (setq gnus-article-buffer name)
694     (setq gnus-original-article-buffer original)
695     ;; This might be a variable local to the summary buffer.
696     (unless gnus-single-article-buffer
697       (save-excursion
698         (set-buffer gnus-summary-buffer)
699         (setq gnus-article-buffer name)
700         (setq gnus-original-article-buffer original)
701         (gnus-set-global-variables)))
702     ;; Init original article buffer.
703     (save-excursion
704       (set-buffer (get-buffer-create gnus-original-article-buffer))
705       (buffer-disable-undo (current-buffer))
706       (setq major-mode 'gnus-original-article-mode)
707       (gnus-add-current-to-buffer-list)
708       (make-local-variable 'gnus-original-article))
709     (if (get-buffer name)
710         (save-excursion
711           (set-buffer name)
712           (buffer-disable-undo (current-buffer))
713           (setq buffer-read-only t)
714           (gnus-add-current-to-buffer-list)
715           (unless (eq major-mode 'gnus-article-mode)
716             (gnus-article-mode))
717           (current-buffer))
718       (save-excursion
719         (set-buffer (get-buffer-create name))
720         (gnus-add-current-to-buffer-list)
721         (gnus-article-mode)
722         (make-local-variable 'gnus-summary-buffer)
723         (current-buffer)))))
724
725 ;; Set article window start at LINE, where LINE is the number of lines
726 ;; from the head of the article.
727 (defun gnus-article-set-window-start (&optional line)
728   (set-window-start
729    (get-buffer-window gnus-article-buffer t)
730    (save-excursion
731      (set-buffer gnus-article-buffer)
732      (goto-char (point-min))
733      (if (not line)
734          (point-min)
735        (gnus-message 6 "Moved to bookmark")
736        (search-forward "\n\n" nil t)
737        (forward-line line)
738        (point)))))
739
740 (defun gnus-article-prepare (article &optional all-headers header)
741   "Prepare ARTICLE in article mode buffer.
742 ARTICLE should either be an article number or a Message-ID.
743 If ARTICLE is an id, HEADER should be the article headers.
744 If ALL-HEADERS is non-nil, no headers are hidden."
745   (save-excursion
746     ;; Make sure we start in a summary buffer.
747     (unless (eq major-mode 'gnus-summary-mode)
748       (set-buffer gnus-summary-buffer))
749     (setq gnus-summary-buffer (current-buffer))
750     ;; Make sure the connection to the server is alive.
751     (unless (gnus-server-opened
752              (gnus-find-method-for-group gnus-newsgroup-name))
753       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
754       (gnus-request-group gnus-newsgroup-name t))
755     (let* ((article (if header (mail-header-number header) article))
756            (summary-buffer (current-buffer))
757            (internal-hook gnus-article-internal-prepare-hook)
758            (group gnus-newsgroup-name)
759            result)
760       (save-excursion
761         (gnus-article-setup-buffer)
762         (set-buffer gnus-article-buffer)
763         ;; Deactivate active regions.
764         (when (and (boundp 'transient-mark-mode)
765                    transient-mark-mode)
766           (setq mark-active nil))
767         (if (not (setq result (let ((buffer-read-only nil))
768                                 (gnus-request-article-this-buffer
769                                  article group))))
770             ;; There is no such article.
771             (save-excursion
772               (when (and (numberp article)
773                          (not (memq article gnus-newsgroup-sparse)))
774                 (setq gnus-article-current
775                       (cons gnus-newsgroup-name article))
776                 (set-buffer gnus-summary-buffer)
777                 (setq gnus-current-article article)
778                 (gnus-summary-mark-article article gnus-canceled-mark))
779               (unless (memq article gnus-newsgroup-sparse)
780                 (gnus-error
781                  1 "No such article (may have expired or been canceled)")))
782           (if (or (eq result 'pseudo) (eq result 'nneething))
783               (progn
784                 (save-excursion
785                   (set-buffer summary-buffer)
786                   (setq gnus-last-article gnus-current-article
787                         gnus-newsgroup-history (cons gnus-current-article
788                                                      gnus-newsgroup-history)
789                         gnus-current-article 0
790                         gnus-current-headers nil
791                         gnus-article-current nil)
792                   (if (eq result 'nneething)
793                       (gnus-configure-windows 'summary)
794                     (gnus-configure-windows 'article))
795                   (gnus-set-global-variables))
796                 (gnus-set-mode-line 'article))
797             ;; The result from the `request' was an actual article -
798             ;; or at least some text that is now displayed in the
799             ;; article buffer.
800             (when (and (numberp article)
801                        (not (eq article gnus-current-article)))
802               ;; Seems like a new article has been selected.
803               ;; `gnus-current-article' must be an article number.
804               (save-excursion
805                 (set-buffer summary-buffer)
806                 (setq gnus-last-article gnus-current-article
807                       gnus-newsgroup-history (cons gnus-current-article
808                                                    gnus-newsgroup-history)
809                       gnus-current-article article
810                       gnus-current-headers
811                       (gnus-summary-article-header gnus-current-article)
812                       gnus-article-current
813                       (cons gnus-newsgroup-name gnus-current-article))
814                 (unless (vectorp gnus-current-headers)
815                   (setq gnus-current-headers nil))
816                 (gnus-summary-show-thread)
817                 (run-hooks 'gnus-mark-article-hook)
818                 (gnus-set-mode-line 'summary)
819                 (when (gnus-visual-p 'article-highlight 'highlight)
820                   (run-hooks 'gnus-visual-mark-article-hook))
821                 ;; Set the global newsgroup variables here.
822                 ;; Suggested by Jim Sisolak
823                 ;; <sisolak@trans4.neep.wisc.edu>.
824                 (gnus-set-global-variables)
825                 (setq gnus-have-all-headers
826                       (or all-headers gnus-show-all-headers))
827                 (and gnus-use-cache
828                      (vectorp (gnus-summary-article-header article))
829                      (gnus-cache-possibly-enter-article
830                       group article
831                       (gnus-summary-article-header article)
832                       (memq article gnus-newsgroup-marked)
833                       (memq article gnus-newsgroup-dormant)
834                       (memq article gnus-newsgroup-unreads)))))
835             (when (or (numberp article)
836                       (stringp article))
837               ;; Hooks for getting information from the article.
838               ;; This hook must be called before being narrowed.
839               (let (buffer-read-only)
840                 (run-hooks 'internal-hook)
841                 (run-hooks 'gnus-article-prepare-hook)
842                 ;; Decode MIME message.
843                 (when gnus-show-mime
844                   (if (or (not gnus-strict-mime)
845                           (gnus-fetch-field "Mime-Version"))
846                       (funcall gnus-show-mime-method)
847                     (funcall gnus-decode-encoded-word-method)))
848                 ;; Perform the article display hooks.
849                 (run-hooks 'gnus-article-display-hook))
850               ;; Do page break.
851               (goto-char (point-min))
852               (when gnus-break-pages
853                 (gnus-narrow-to-page)))
854             (gnus-set-mode-line 'article)
855             (gnus-configure-windows 'article)
856             (goto-char (point-min))
857             t))))))
858
859 (defun gnus-article-wash-status ()
860   "Return a string which display status of article washing."
861   (save-excursion
862     (set-buffer gnus-article-buffer)
863     (let ((cite (article-hidden-text-p 'cite))
864           (headers (article-hidden-text-p 'headers))
865           (boring (article-hidden-text-p 'boring-headers))
866           (pgp (article-hidden-text-p 'pgp))
867           (pem (article-hidden-text-p 'pem))
868           (signature (article-hidden-text-p 'signature))
869           (overstrike (article-hidden-text-p 'overstrike))
870           (emphasis (article-hidden-text-p 'emphasis))
871           (mime gnus-show-mime))
872       (format "%c%c%c%c%c%c%c"
873               (if cite ?c ? )
874               (if (or headers boring) ?h ? )
875               (if (or pgp pem) ?p ? )
876               (if signature ?s ? )
877               (if overstrike ?o ? )
878               (if mime ?m ? )
879               (if emphasis ?e ? )))))
880
881 (defun gnus-article-hide-headers-if-wanted ()
882   "Hide unwanted headers if `gnus-have-all-headers' is nil.
883 Provided for backwards compatibility."
884   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
885       gnus-inhibit-hiding
886       (gnus-article-hide-headers)))
887
888 ;;; Article savers.
889
890 (defun gnus-output-to-rmail (file-name)
891   "Append the current article to an Rmail file named FILE-NAME."
892   (require 'rmail)
893   ;; Most of these codes are borrowed from rmailout.el.
894   (setq file-name (expand-file-name file-name))
895   (setq rmail-default-rmail-file file-name)
896   (let ((artbuf (current-buffer))
897         (tmpbuf (get-buffer-create " *Gnus-output*")))
898     (save-excursion
899       (or (get-file-buffer file-name)
900           (file-exists-p file-name)
901           (if (gnus-yes-or-no-p
902                (concat "\"" file-name "\" does not exist, create it? "))
903               (let ((file-buffer (create-file-buffer file-name)))
904                 (save-excursion
905                   (set-buffer file-buffer)
906                   (rmail-insert-rmail-file-header)
907                   (let ((require-final-newline nil))
908                     (gnus-write-buffer file-name)))
909                 (kill-buffer file-buffer))
910             (error "Output file does not exist")))
911       (set-buffer tmpbuf)
912       (buffer-disable-undo (current-buffer))
913       (erase-buffer)
914       (insert-buffer-substring artbuf)
915       (gnus-convert-article-to-rmail)
916       ;; Decide whether to append to a file or to an Emacs buffer.
917       (let ((outbuf (get-file-buffer file-name)))
918         (if (not outbuf)
919             (append-to-file (point-min) (point-max) file-name)
920           ;; File has been visited, in buffer OUTBUF.
921           (set-buffer outbuf)
922           (let ((buffer-read-only nil)
923                 (msg (and (boundp 'rmail-current-message)
924                           (symbol-value 'rmail-current-message))))
925             ;; If MSG is non-nil, buffer is in RMAIL mode.
926             (when msg
927               (widen)
928               (narrow-to-region (point-max) (point-max)))
929             (insert-buffer-substring tmpbuf)
930             (when msg
931               (goto-char (point-min))
932               (widen)
933               (search-backward "\^_")
934               (narrow-to-region (point) (point-max))
935               (goto-char (1+ (point-min)))
936               (rmail-count-new-messages t)
937               (rmail-show-message msg))))))
938     (kill-buffer tmpbuf)))
939
940 (defun gnus-output-to-file (file-name)
941   "Append the current article to a file named FILE-NAME."
942   (let ((artbuf (current-buffer)))
943     (nnheader-temp-write nil
944       (insert-buffer-substring artbuf)
945       ;; Append newline at end of the buffer as separator, and then
946       ;; save it to file.
947       (goto-char (point-max))
948       (insert "\n")
949       (append-to-file (point-min) (point-max) file-name))))
950
951 (defun gnus-convert-article-to-rmail ()
952   "Convert article in current buffer to Rmail message format."
953   (let ((buffer-read-only nil))
954     ;; Convert article directly into Babyl format.
955     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
956     (goto-char (point-min))
957     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
958     (while (search-forward "\n\^_" nil t) ;single char
959       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
960     (goto-char (point-max))
961     (insert "\^_")))
962
963 (defun gnus-narrow-to-page (&optional arg)
964   "Narrow the article buffer to a page.
965 If given a numerical ARG, move forward ARG pages."
966   (interactive "P")
967   (setq arg (if arg (prefix-numeric-value arg) 0))
968   (save-excursion
969     (set-buffer gnus-article-buffer)
970     (goto-char (point-min))
971     (widen)
972     ;; Remove any old next/prev buttons.
973     (when (gnus-visual-p 'page-marker)
974       (let ((buffer-read-only nil))
975         (gnus-remove-text-with-property 'gnus-prev)
976         (gnus-remove-text-with-property 'gnus-next)))
977     (when
978         (cond ((< arg 0)
979                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
980               ((> arg 0)
981                (re-search-forward page-delimiter nil 'move arg)))
982       (goto-char (match-end 0)))
983     (narrow-to-region
984      (point)
985      (if (re-search-forward page-delimiter nil 'move)
986          (match-beginning 0)
987        (point)))
988     (when (and (gnus-visual-p 'page-marker)
989                (not (= (point-min) 1)))
990       (save-excursion
991         (goto-char (point-min))
992         (gnus-insert-prev-page-button)))
993     (when (and (gnus-visual-p 'page-marker)
994                (< (+ (point-max) 2) (buffer-size)))
995       (save-excursion
996         (goto-char (point-max))
997         (gnus-insert-next-page-button)))))
998
999 ;; Article mode commands
1000
1001 (defun gnus-article-goto-next-page ()
1002   "Show the next page of the article."
1003   (interactive)
1004   (when (gnus-article-next-page)
1005     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
1006
1007 (defun gnus-article-goto-prev-page ()
1008   "Show the next page of the article."
1009   (interactive)
1010   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
1011     (gnus-article-prev-page nil)))
1012
1013 (defun gnus-article-next-page (&optional lines)
1014   "Show the next page of the current article.
1015 If end of article, return non-nil.  Otherwise return nil.
1016 Argument LINES specifies lines to be scrolled up."
1017   (interactive "p")
1018   (move-to-window-line -1)
1019   (if (save-excursion
1020         (end-of-line)
1021         (and (pos-visible-in-window-p)  ;Not continuation line.
1022              (eobp)))
1023       ;; Nothing in this page.
1024       (if (or (not gnus-break-pages)
1025               (save-excursion
1026                 (save-restriction
1027                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
1028           t                             ;Nothing more.
1029         (gnus-narrow-to-page 1)         ;Go to next page.
1030         nil)
1031     ;; More in this page.
1032     (condition-case ()
1033         (scroll-up lines)
1034       (end-of-buffer
1035        ;; Long lines may cause an end-of-buffer error.
1036        (goto-char (point-max))))
1037     (move-to-window-line 0)
1038     nil))
1039
1040 (defun gnus-article-prev-page (&optional lines)
1041   "Show previous page of current article.
1042 Argument LINES specifies lines to be scrolled down."
1043   (interactive "p")
1044   (move-to-window-line 0)
1045   (if (and gnus-break-pages
1046            (bobp)
1047            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
1048       (progn
1049         (gnus-narrow-to-page -1)        ;Go to previous page.
1050         (goto-char (point-max))
1051         (recenter -1))
1052     (prog1
1053         (condition-case ()
1054             (scroll-down lines)
1055           (error nil))
1056       (move-to-window-line 0))))
1057
1058 (defun gnus-article-refer-article ()
1059   "Read article specified by message-id around point."
1060   (interactive)
1061   (let ((point (point)))
1062     (search-forward ">" nil t)          ;Move point to end of "<....>".
1063     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
1064         (let ((message-id (match-string 1)))
1065           (goto-char point)
1066           (set-buffer gnus-summary-buffer)
1067           (gnus-summary-refer-article message-id))
1068       (goto-char (point))
1069       (error "No references around point"))))
1070
1071 (defun gnus-article-show-summary ()
1072   "Reconfigure windows to show summary buffer."
1073   (interactive)
1074   (gnus-configure-windows 'article)
1075   (gnus-summary-goto-subject gnus-current-article))
1076
1077 (defun gnus-article-describe-briefly ()
1078   "Describe article mode commands briefly."
1079   (interactive)
1080   (gnus-message 6
1081                 (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")))
1082
1083 (defun gnus-article-summary-command ()
1084   "Execute the last keystroke in the summary buffer."
1085   (interactive)
1086   (let ((obuf (current-buffer))
1087         (owin (current-window-configuration))
1088         func)
1089     (switch-to-buffer gnus-summary-buffer 'norecord)
1090     (setq func (lookup-key (current-local-map) (this-command-keys)))
1091     (call-interactively func)
1092     (set-buffer obuf)
1093     (set-window-configuration owin)
1094     (set-window-point (get-buffer-window (current-buffer)) (point))))
1095
1096 (defun gnus-article-summary-command-nosave ()
1097   "Execute the last keystroke in the summary buffer."
1098   (interactive)
1099   (let (func)
1100     (pop-to-buffer gnus-summary-buffer 'norecord)
1101     (setq func (lookup-key (current-local-map) (this-command-keys)))
1102     (call-interactively func)))
1103
1104 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
1105   "Read a summary buffer key sequence and execute it from the article buffer."
1106   (interactive "P")
1107   (let ((nosaves
1108          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
1109            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
1110            "=" "^" "\M-^" "|"))
1111         (nosave-but-article
1112          '("A\r"))
1113         (nosave-in-article
1114          '("\C-d"))
1115         keys)
1116     (save-excursion
1117       (set-buffer gnus-summary-buffer)
1118       (push (or key last-command-event) unread-command-events)
1119       (setq keys (read-key-sequence nil)))
1120     (message "")
1121
1122     (if (or (member keys nosaves)
1123             (member keys nosave-but-article)
1124             (member keys nosave-in-article))
1125         (let (func)
1126           (save-window-excursion
1127             (pop-to-buffer gnus-summary-buffer 'norecord)
1128             (setq func (lookup-key (current-local-map) keys)))
1129           (if (not func)
1130               (ding)
1131             (unless (member keys nosave-in-article)
1132               (set-buffer gnus-summary-buffer))
1133             (call-interactively func))
1134           (when (member keys nosave-but-article)
1135             (pop-to-buffer gnus-article-buffer 'norecord)))
1136       ;; These commands should restore window configuration.
1137       (let ((obuf (current-buffer))
1138             (owin (current-window-configuration))
1139             (opoint (point))
1140             func in-buffer)
1141         (if not-restore-window
1142             (pop-to-buffer gnus-summary-buffer 'norecord)
1143           (switch-to-buffer gnus-summary-buffer 'norecord))
1144         (setq in-buffer (current-buffer))
1145         (if (setq func (lookup-key (current-local-map) keys))
1146             (call-interactively func)
1147           (ding))
1148         (when (eq in-buffer (current-buffer))
1149           (set-buffer obuf)
1150           (unless not-restore-window
1151             (set-window-configuration owin))
1152           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
1153
1154 (defun gnus-article-hide (&optional arg force)
1155   "Hide all the gruft in the current article.
1156 This means that PGP stuff, signatures, cited text and (some)
1157 headers will be hidden.
1158 If given a prefix, show the hidden text instead."
1159   (interactive (list current-prefix-arg 'force))
1160   (gnus-article-hide-headers arg)
1161   (gnus-article-hide-pgp arg)
1162   (gnus-article-hide-citation-maybe arg force)
1163   (gnus-article-hide-signature arg))
1164
1165 (defun gnus-article-maybe-highlight ()
1166   "Do some article highlighting if `article-visual' is non-nil."
1167   (when (gnus-visual-p 'article-highlight 'highlight)
1168     (gnus-article-highlight-some)))
1169
1170 (defun gnus-request-article-this-buffer (article group)
1171   "Get an article and insert it into this buffer."
1172   (let (do-update-line)
1173     (prog1
1174         (save-excursion
1175           (erase-buffer)
1176           (gnus-kill-all-overlays)
1177           (setq group (or group gnus-newsgroup-name))
1178
1179           ;; Open server if it has closed.
1180           (gnus-check-server (gnus-find-method-for-group group))
1181
1182           ;; Using `gnus-request-article' directly will insert the article into
1183           ;; `nntp-server-buffer' - so we'll save some time by not having to
1184           ;; copy it from the server buffer into the article buffer.
1185
1186           ;; We only request an article by message-id when we do not have the
1187           ;; headers for it, so we'll have to get those.
1188           (when (stringp article)
1189             (let ((gnus-override-method gnus-refer-article-method))
1190               (gnus-read-header article)))
1191
1192           ;; If the article number is negative, that means that this article
1193           ;; doesn't belong in this newsgroup (possibly), so we find its
1194           ;; message-id and request it by id instead of number.
1195           (when (and (numberp article)
1196                      gnus-summary-buffer
1197                      (get-buffer gnus-summary-buffer)
1198                      (buffer-name (get-buffer gnus-summary-buffer)))
1199             (save-excursion
1200               (set-buffer gnus-summary-buffer)
1201               (let ((header (gnus-summary-article-header article)))
1202                 (when (< article 0)
1203                   (cond 
1204                    ((memq article gnus-newsgroup-sparse)
1205                     ;; This is a sparse gap article.
1206                     (setq do-update-line article)
1207                     (setq article (mail-header-id header))
1208                     (let ((gnus-override-method gnus-refer-article-method))
1209                       (gnus-read-header article))
1210                     (setq gnus-newsgroup-sparse
1211                           (delq article gnus-newsgroup-sparse)))
1212                    ((vectorp header)
1213                     ;; It's a real article.
1214                     (setq article (mail-header-id header)))
1215                    (t
1216                     ;; It is an extracted pseudo-article.
1217                     (setq article 'pseudo)
1218                     (gnus-request-pseudo-article header))))
1219                 
1220                 (let ((method (gnus-find-method-for-group 
1221                                gnus-newsgroup-name)))
1222                   (if (not (eq (car method) 'nneething))
1223                       ()
1224                     (let ((dir (concat (file-name-as-directory (nth 1 method))
1225                                        (mail-header-subject header))))
1226                       (when (file-directory-p dir)
1227                         (setq article 'nneething)
1228                         (gnus-group-enter-directory dir))))))))
1229
1230           (cond
1231            ;; Refuse to select canceled articles.
1232            ((and (numberp article)
1233                  gnus-summary-buffer
1234                  (get-buffer gnus-summary-buffer)
1235                  (buffer-name (get-buffer gnus-summary-buffer))
1236                  (eq (cdr (save-excursion
1237                             (set-buffer gnus-summary-buffer)
1238                             (assq article gnus-newsgroup-reads)))
1239                      gnus-canceled-mark))
1240             nil)
1241            ;; We first check `gnus-original-article-buffer'.
1242            ((and (get-buffer gnus-original-article-buffer)
1243                  (numberp article)
1244                  (save-excursion
1245                    (set-buffer gnus-original-article-buffer)
1246                    (and (equal (car gnus-original-article) group)
1247                         (eq (cdr gnus-original-article) article))))
1248             (insert-buffer-substring gnus-original-article-buffer)
1249             'article)
1250            ;; Check the backlog.
1251            ((and gnus-keep-backlog
1252                  (gnus-backlog-request-article group article (current-buffer)))
1253             'article)
1254            ;; Check asynchronous pre-fetch.
1255            ((gnus-async-request-fetched-article group article (current-buffer))
1256             (gnus-async-prefetch-next group article gnus-summary-buffer)
1257             'article)
1258            ;; Check the cache.
1259            ((and gnus-use-cache
1260                  (numberp article)
1261                  (gnus-cache-request-article article group))
1262             'article)
1263            ;; Get the article and put into the article buffer.
1264            ((or (stringp article) (numberp article))
1265             (let ((gnus-override-method
1266                    (and (stringp article) gnus-refer-article-method))
1267                   (buffer-read-only nil))
1268               (erase-buffer)
1269               (gnus-kill-all-overlays)
1270               (when (gnus-request-article article group (current-buffer))
1271                 (when (numberp article)
1272                   (gnus-async-prefetch-next group article gnus-summary-buffer)
1273                   (when gnus-keep-backlog
1274                     (gnus-backlog-enter-article 
1275                      group article (current-buffer))))
1276                 'article)))
1277            ;; It was a pseudo.
1278            (t article)))
1279
1280       ;; Take the article from the original article buffer
1281       ;; and place it in the buffer it's supposed to be in.
1282       (when (and (get-buffer gnus-article-buffer)
1283                  ;;(numberp article)
1284                  (equal (buffer-name (current-buffer))
1285                         (buffer-name (get-buffer gnus-article-buffer))))
1286         (save-excursion
1287           (if (get-buffer gnus-original-article-buffer)
1288               (set-buffer (get-buffer gnus-original-article-buffer))
1289             (set-buffer (get-buffer-create gnus-original-article-buffer))
1290             (buffer-disable-undo (current-buffer))
1291             (setq major-mode 'gnus-original-article-mode)
1292             (setq buffer-read-only t)
1293             (gnus-add-current-to-buffer-list))
1294           (let (buffer-read-only)
1295             (erase-buffer)
1296             (insert-buffer-substring gnus-article-buffer))
1297           (setq gnus-original-article (cons group article))))
1298     
1299       ;; Update sparse articles.
1300       (when (and do-update-line
1301                  (or (numberp article)
1302                      (stringp article)))
1303         (let ((buf (current-buffer)))
1304           (set-buffer gnus-summary-buffer)
1305           (gnus-summary-update-article do-update-line)
1306           (gnus-summary-goto-subject do-update-line nil t)
1307           (set-window-point (get-buffer-window (current-buffer) t)
1308                             (point))
1309           (set-buffer buf))))))
1310
1311 (defun gnus-article-date-ut (&optional type highlight)
1312   "Convert DATE date to universal time in the current article.
1313 If TYPE is `local', convert to local time; if it is `lapsed', output
1314 how much time has lapsed since DATE."
1315   (interactive (list 'ut t))
1316   (let ((headers (or gnus-current-headers (gnus-summary-article-header))))
1317     (save-excursion
1318       (set-buffer gnus-article-buffer)
1319       (article-date-ut type highlight headers))))
1320
1321 ;;;
1322 ;;; Article editing
1323 ;;;
1324
1325 (defcustom gnus-article-edit-mode-hook nil
1326   "Hook run in article edit mode buffers."
1327   :group 'article
1328   :type 'hook)
1329
1330 (defvar gnus-article-edit-done-function nil)
1331
1332 (defvar gnus-article-edit-mode-map nil)
1333
1334 (unless gnus-article-edit-mode-map 
1335   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
1336
1337   (gnus-define-keys gnus-article-edit-mode-map
1338     "\C-c\C-c" gnus-article-edit-done
1339     "\C-c\C-k" gnus-article-edit-exit)
1340
1341   (gnus-define-keys (gnus-article-edit-wash-map
1342                      "\C-c\C-w" gnus-article-edit-mode-map)
1343     "f" gnus-article-edit-full-stops))
1344
1345 (defun gnus-article-edit-mode ()
1346   "Major mode for editing articles.
1347 This is an extended text-mode.
1348
1349 \\{gnus-article-edit-mode-map}"
1350   (interactive)
1351   (kill-all-local-variables)
1352   (setq major-mode 'gnus-article-edit-mode)
1353   (setq mode-name "Article Edit")
1354   (use-local-map gnus-article-edit-mode-map)
1355   (make-local-variable 'gnus-article-edit-done-function)
1356   (make-local-variable 'gnus-prev-winconf)
1357   (setq buffer-read-only nil)
1358   (buffer-enable-undo)
1359   (widen)
1360   (run-hooks 'text-mode 'gnus-article-edit-mode-hook))
1361
1362 (defun gnus-article-edit (&optional force)
1363   "Edit the current article.
1364 This will have permanent effect only in mail groups.
1365 If FORCE is non-nil, allow editing of articles even in read-only
1366 groups."
1367   (interactive "P")
1368   (when (and (not force)
1369              (gnus-group-read-only-p))
1370     (error "The current newsgroup does not support article editing."))
1371   (gnus-article-edit-article
1372    `(lambda ()
1373       (gnus-summary-edit-article-done
1374        ,(or (mail-header-references gnus-current-headers) "")
1375        ,(gnus-group-read-only-p) ,gnus-summary-buffer))))
1376
1377 (defun gnus-article-edit-article (exit-func)
1378   "Start editing the contents of the current article buffer."
1379   (let ((winconf (current-window-configuration)))
1380     (set-buffer gnus-article-buffer)
1381     (gnus-article-edit-mode)
1382     (set-text-properties (point-min) (point-max) nil)
1383     (gnus-configure-windows 'edit-article)
1384     (setq gnus-article-edit-done-function exit-func)
1385     (setq gnus-prev-winconf winconf)
1386     (gnus-message 6 "C-c C-c to end edits")))
1387
1388 (defun gnus-article-edit-done ()
1389   "Update the article edits and exit."
1390   (interactive)
1391   (let ((func gnus-article-edit-done-function)
1392         (buf (current-buffer))
1393         (start (window-start)))
1394     (gnus-article-edit-exit)
1395     (save-excursion
1396       (set-buffer buf)
1397       (let ((buffer-read-only nil))
1398         (funcall func)))
1399     (set-buffer buf)
1400     (set-window-start (get-buffer-window buf) start)
1401     (set-window-point (get-buffer-window buf) (point))))
1402
1403 (defun gnus-article-edit-exit ()
1404   "Exit the article editing without updating."
1405   (interactive)
1406   ;; We remove all text props from the article buffer.
1407   (let ((buf (format "%s" (buffer-string)))
1408         (curbuf (current-buffer))
1409         (p (point))
1410         (window-start (window-start)))
1411     (erase-buffer)
1412     (insert buf)
1413     (let ((winconf gnus-prev-winconf))
1414       (gnus-article-mode)
1415       ;; The cache and backlog have to be flushed somewhat.
1416       (when gnus-use-cache
1417         (gnus-cache-update-article      
1418          (car gnus-article-current) (cdr gnus-article-current)))
1419       (when gnus-keep-backlog
1420         (gnus-backlog-remove-article 
1421          (car gnus-article-current) (cdr gnus-article-current)))
1422       ;; Flush original article as well.
1423       (save-excursion
1424         (when (get-buffer gnus-original-article-buffer)
1425           (set-buffer gnus-original-article-buffer)
1426           (setq gnus-original-article nil)))
1427       (set-window-configuration winconf)
1428       ;; Tippy-toe some to make sure that point remains where it was.
1429       (let ((buf (current-buffer)))
1430         (set-buffer curbuf)
1431         (set-window-start (get-buffer-window (current-buffer)) window-start)
1432         (goto-char p)
1433         (set-buffer buf)))))
1434       
1435 (defun gnus-article-edit-full-stops ()
1436   "Interactively repair spacing at end of sentences."
1437   (interactive)
1438   (save-excursion
1439     (goto-char (point-min))
1440     (search-forward-regexp "^$" nil t)
1441     (let ((case-fold-search nil))
1442       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
1443
1444 ;;; 
1445 ;;; Article highlights
1446 ;;;
1447
1448 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
1449
1450 ;;; Internal Variables:
1451
1452 (defcustom gnus-button-url-regexp "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-\\wa-zA-Z0-9_=!?#$@~`%&*+|\\/:;.,]*[-\\wa-zA-Z0-9_=#$@~`%&*+|\\/]"
1453   "Regular expression that matches URLs."
1454   :group 'article
1455   :type 'regexp)
1456
1457 (defcustom gnus-button-alist 
1458   `(("\\bin\\( +article\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2 
1459      t gnus-button-message-id 3)
1460     ("\\(<?\\(url: ?\\)?news://\\([^>\n\t ]*\\)>?\\)" 1 t
1461      gnus-button-fetch-group 3)
1462     ("\\(<?\\(url: ?\\)?news:\\([^>\n\t ]*\\)>?\\)" 1 t
1463      gnus-button-message-id 3)
1464     ("\\(<URL: *\\)?mailto: *\\([^> \n\t]+\\)>?" 0 t gnus-url-mailto 2)
1465     ;; This is how URLs _should_ be embedded in text...
1466     ("<URL: *\\([^\n\r>]*\\)>" 0 t gnus-button-url 1)
1467     ;; Next regexp stolen from highlight-headers.el.
1468     ;; Modified by Vladimir Alexiev.
1469     (,gnus-button-url-regexp 0 t gnus-button-url 0))
1470   "Alist of regexps matching buttons in article bodies.
1471
1472 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
1473 REGEXP: is the string matching text around the button,
1474 BUTTON: is the number of the regexp grouping actually matching the button,
1475 FORM: is a lisp expression which must eval to true for the button to
1476 be added, 
1477 CALLBACK: is the function to call when the user push this button, and each
1478 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
1479
1480 CALLBACK can also be a variable, in that case the value of that
1481 variable it the real callback function."
1482   :group 'article
1483   :type '(repeat (list regexp 
1484                        (integer :tag "Button")
1485                        (sexp :tag "Form")
1486                        (function :tag "Callback")
1487                        (repeat :tag "Par"
1488                                :inline t
1489                                (integer :tag "Regexp group")))))
1490
1491 (defcustom gnus-header-button-alist 
1492   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
1493      0 t gnus-button-message-id 0)
1494     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
1495     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+" 
1496      0 t gnus-button-mailto 0)
1497     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
1498     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
1499     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
1500      gnus-button-message-id 3))
1501   "Alist of headers and regexps to match buttons in article heads.
1502
1503 This alist is very similar to `gnus-button-alist', except that each
1504 alist has an additional HEADER element first in each entry:
1505
1506 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
1507
1508 HEADER is a regexp to match a header.  For a fuller explanation, see
1509 `gnus-button-alist'."
1510   :group 'article
1511   :type '(repeat (list (regexp :tag "Header")
1512                        regexp 
1513                        (integer :tag "Button")
1514                        (sexp :tag "Form")
1515                        (function :tag "Callback")
1516                        (repeat :tag "Par"
1517                                :inline t
1518                                (integer :tag "Regexp group")))))
1519
1520 (defvar gnus-button-regexp nil)
1521 (defvar gnus-button-marker-list nil)
1522 ;; Regexp matching any of the regexps from `gnus-button-alist'.
1523
1524 (defvar gnus-button-last nil)
1525 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
1526
1527 ;;; Commands:
1528
1529 (defun gnus-article-push-button (event)
1530   "Check text under the mouse pointer for a callback function.
1531 If the text under the mouse pointer has a `gnus-callback' property,
1532 call it with the value of the `gnus-data' text property."
1533   (interactive "e")
1534   (set-buffer (window-buffer (posn-window (event-start event))))
1535   (let* ((pos (posn-point (event-start event)))
1536          (data (get-text-property pos 'gnus-data))
1537          (fun (get-text-property pos 'gnus-callback)))
1538     (when fun
1539       (funcall fun data))))
1540
1541 (defun gnus-article-press-button ()
1542   "Check text at point for a callback function.
1543 If the text at point has a `gnus-callback' property,
1544 call it with the value of the `gnus-data' text property."
1545   (interactive)
1546   (let* ((data (get-text-property (point) 'gnus-data))
1547          (fun (get-text-property (point) 'gnus-callback)))
1548     (when fun
1549       (funcall fun data))))
1550
1551 (defun gnus-article-prev-button (n)
1552   "Move point to N buttons backward.
1553 If N is negative, move forward instead."
1554   (interactive "p")
1555   (gnus-article-next-button (- n)))
1556
1557 (defun gnus-article-next-button (n)
1558   "Move point to N buttons forward.
1559 If N is negative, move backward instead."
1560   (interactive "p")
1561   (let ((function (if (< n 0) 'previous-single-property-change
1562                     'next-single-property-change))
1563         (inhibit-point-motion-hooks t)
1564         (backward (< n 0))
1565         (limit (if (< n 0) (point-min) (point-max))))
1566     (setq n (abs n))
1567     (while (and (not (= limit (point)))
1568                 (> n 0))
1569       ;; Skip past the current button.
1570       (when (get-text-property (point) 'gnus-callback)
1571         (goto-char (funcall function (point) 'gnus-callback nil limit)))
1572       ;; Go to the next (or previous) button.
1573       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
1574       ;; Put point at the start of the button.
1575       (when (and backward (not (get-text-property (point) 'gnus-callback)))
1576         (goto-char (funcall function (point) 'gnus-callback nil limit)))
1577       ;; Skip past intangible buttons.
1578       (when (get-text-property (point) 'intangible)
1579         (incf n))
1580       (decf n))
1581     (unless (zerop n)
1582       (gnus-message 5 "No more buttons"))
1583     n))
1584
1585 (defun gnus-article-highlight (&optional force)
1586   "Highlight current article.
1587 This function calls `gnus-article-highlight-headers',
1588 `gnus-article-highlight-citation', 
1589 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
1590 do the highlighting.  See the documentation for those functions."
1591   (interactive (list 'force))
1592   (gnus-article-highlight-headers)
1593   (gnus-article-highlight-citation force)
1594   (gnus-article-highlight-signature)
1595   (gnus-article-add-buttons force)
1596   (gnus-article-add-buttons-to-head))
1597
1598 (defun gnus-article-highlight-some (&optional force)
1599   "Highlight current article.
1600 This function calls `gnus-article-highlight-headers',
1601 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
1602 do the highlighting.  See the documentation for those functions."
1603   (interactive (list 'force))
1604   (gnus-article-highlight-headers)
1605   (gnus-article-highlight-signature)
1606   (gnus-article-add-buttons))
1607
1608 (defun gnus-article-highlight-headers ()
1609   "Highlight article headers as specified by `gnus-header-face-alist'."
1610   (interactive)
1611   (save-excursion
1612     (set-buffer gnus-article-buffer)
1613     (save-restriction
1614       (let ((alist gnus-header-face-alist)
1615             (buffer-read-only nil)
1616             (case-fold-search t)
1617             (inhibit-point-motion-hooks t)
1618             entry regexp header-face field-face from hpoints fpoints)
1619         (goto-char (point-min))
1620         (when (search-forward "\n\n" nil t)
1621           (narrow-to-region (1- (point)) (point-min))
1622           (while (setq entry (pop alist))
1623             (goto-char (point-min))
1624             (setq regexp (concat "^\\("
1625                                  (if (string-equal "" (nth 0 entry))
1626                                      "[^\t ]"
1627                                    (nth 0 entry))
1628                                  "\\)")
1629                   header-face (nth 1 entry)
1630                   field-face (nth 2 entry))
1631             (while (and (re-search-forward regexp nil t)
1632                         (not (eobp)))
1633               (beginning-of-line)
1634               (setq from (point))
1635               (unless (search-forward ":" nil t)
1636                 (forward-char 1))
1637               (when (and header-face
1638                          (not (memq (point) hpoints)))
1639                 (push (point) hpoints)
1640                 (gnus-put-text-property from (point) 'face header-face))
1641               (when (and field-face
1642                          (not (memq (setq from (point)) fpoints)))
1643                 (push from fpoints)
1644                 (if (re-search-forward "^[^ \t]" nil t)
1645                     (forward-char -2)
1646                   (goto-char (point-max)))
1647                 (gnus-put-text-property from (point) 'face field-face)))))))))
1648
1649 (defun gnus-article-highlight-signature ()
1650   "Highlight the signature in an article.
1651 It does this by highlighting everything after
1652 `gnus-signature-separator' using `gnus-signature-face'." 
1653   (interactive)
1654   (save-excursion
1655     (set-buffer gnus-article-buffer)
1656     (let ((buffer-read-only nil)
1657           (inhibit-point-motion-hooks t))
1658       (save-restriction
1659         (when (and gnus-signature-face
1660                    (article-narrow-to-signature))
1661           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
1662                             'face gnus-signature-face)
1663           (widen)
1664           (article-search-signature)
1665           (let ((start (match-beginning 0))
1666                 (end (set-marker (make-marker) (1+ (match-end 0)))))
1667             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
1668                                      end)))))))
1669
1670 (defun gnus-article-add-buttons (&optional force)
1671   "Find external references in the article and make buttons of them.
1672 \"External references\" are things like Message-IDs and URLs, as
1673 specified by `gnus-button-alist'."
1674   (interactive (list 'force))
1675   (save-excursion
1676     (set-buffer gnus-article-buffer)
1677     ;; Remove all old markers.
1678     (while gnus-button-marker-list
1679       (set-marker (pop gnus-button-marker-list) nil))
1680     (let ((buffer-read-only nil)
1681           (inhibit-point-motion-hooks t)
1682           (case-fold-search t)
1683           (alist gnus-button-alist)
1684           beg entry regexp)
1685       (goto-char (point-min))
1686       ;; We skip the headers.
1687       (unless (search-forward "\n\n" nil t)
1688         (goto-char (point-max)))
1689       (setq beg (point))
1690       (while (setq entry (pop alist))
1691         (setq regexp (car entry))
1692         (goto-char beg)
1693         (while (re-search-forward regexp nil t)
1694           (let* ((start (and entry (match-beginning (nth 1 entry))))
1695                  (end (and entry (match-end (nth 1 entry))))
1696                  (from (match-beginning 0)))
1697             (when (or (eq t (nth 1 entry))
1698                       (eval (nth 1 entry)))
1699               ;; That optional form returned non-nil, so we add the
1700               ;; button. 
1701               (gnus-article-add-button 
1702                start end 'gnus-button-push 
1703                (car (push (set-marker (make-marker) from)
1704                           gnus-button-marker-list))))))))))
1705
1706 ;; Add buttons to the head of an article.
1707 (defun gnus-article-add-buttons-to-head ()
1708   "Add buttons to the head of the article."
1709   (interactive)
1710   (save-excursion
1711     (set-buffer gnus-article-buffer)
1712     (let ((buffer-read-only nil)
1713           (inhibit-point-motion-hooks t)
1714           (case-fold-search t)
1715           (alist gnus-header-button-alist)
1716           entry beg end)
1717       (nnheader-narrow-to-headers)
1718       (while alist
1719         ;; Each alist entry.
1720         (setq entry (car alist)
1721               alist (cdr alist))
1722         (goto-char (point-min))
1723         (while (re-search-forward (car entry) nil t)
1724           ;; Each header matching the entry.
1725           (setq beg (match-beginning 0))
1726           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
1727                              (match-beginning 0))
1728                         (point-max)))
1729           (goto-char beg)
1730           (while (re-search-forward (nth 1 entry) end t)
1731             ;; Each match within a header.
1732             (let* ((entry (cdr entry))
1733                    (start (match-beginning (nth 1 entry)))
1734                    (end (match-end (nth 1 entry)))
1735                    (form (nth 2 entry)))
1736               (goto-char (match-end 0))
1737               (when (eval form)
1738                 (gnus-article-add-button 
1739                  start end (nth 3 entry)
1740                  (buffer-substring (match-beginning (nth 4 entry))
1741                                    (match-end (nth 4 entry)))))))
1742           (goto-char end))))
1743     (widen)))
1744
1745 ;;; External functions:
1746
1747 (defun gnus-article-add-button (from to fun &optional data)
1748   "Create a button between FROM and TO with callback FUN and data DATA."
1749   (when gnus-article-button-face
1750     (gnus-overlay-put (gnus-make-overlay from to)
1751                       'face gnus-article-button-face))
1752   (gnus-add-text-properties 
1753    from to
1754    (nconc (and gnus-article-mouse-face
1755                (list gnus-mouse-face-prop gnus-article-mouse-face))
1756           (list 'gnus-callback fun)
1757           (and data (list 'gnus-data data)))))
1758
1759 ;;; Internal functions:
1760
1761 (defun gnus-signature-toggle (end)
1762   (save-excursion
1763     (set-buffer gnus-article-buffer)
1764     (let ((buffer-read-only nil)
1765           (inhibit-point-motion-hooks t))
1766       (if (get-text-property end 'invisible)
1767           (article-unhide-text end (point-max))
1768         (article-hide-text end (point-max) gnus-hidden-properties)))))
1769
1770 (defun gnus-button-entry ()
1771   ;; Return the first entry in `gnus-button-alist' matching this place.
1772   (let ((alist gnus-button-alist)
1773         (entry nil))
1774     (while alist
1775       (setq entry (pop alist))
1776       (if (looking-at (car entry))
1777           (setq alist nil)
1778         (setq entry nil)))
1779     entry))
1780
1781 (defun gnus-button-push (marker)
1782   ;; Push button starting at MARKER.
1783   (save-excursion
1784     (set-buffer gnus-article-buffer)
1785     (goto-char marker)
1786     (let* ((entry (gnus-button-entry))
1787            (inhibit-point-motion-hooks t)
1788            (fun (nth 3 entry))
1789            (args (mapcar (lambda (group)
1790                            (let ((string (buffer-substring
1791                                           (match-beginning group)
1792                                           (match-end group))))
1793                              (gnus-set-text-properties
1794                               0 (length string) nil string)
1795                              string))
1796                          (nthcdr 4 entry))))
1797       (cond
1798        ((fboundp fun)
1799         (apply fun args))
1800        ((and (boundp fun)
1801              (fboundp (symbol-value fun)))
1802         (apply (symbol-value fun) args))
1803        (t
1804         (gnus-message 1 "You must define `%S' to use this button"
1805                       (cons fun args)))))))
1806
1807 (defun gnus-button-message-id (message-id)
1808   "Fetch MESSAGE-ID."
1809   (save-excursion
1810     (set-buffer gnus-summary-buffer)
1811     (gnus-summary-refer-article message-id)))
1812
1813 (defun gnus-button-fetch-group (address)
1814   "Fetch GROUP specified by ADDRESS."
1815   (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\(.*\\)$" address))
1816       (error "Can't parse %s" address)
1817     (gnus-group-read-ephemeral-group
1818      (match-string 4 address)
1819      `(nntp ,(match-string 1 address) (nntp-address ,(match-string 1 address))
1820             (nntp-port-number ,(if (match-end 3)
1821                                    (match-string 3 address)
1822                                  "nntp"))))))
1823
1824 (defun gnus-split-string (string pattern)
1825   "Return a list of substrings of STRING which are separated by PATTERN."
1826   (let (parts (start 0))
1827     (while (string-match pattern string start)
1828       (setq parts (cons (substring string start (match-beginning 0)) parts)
1829             start (match-end 0)))
1830     (nreverse (cons (substring string start) parts))))
1831  
1832 (defun gnus-url-parse-query-string (query &optional downcase)
1833   (let (retval pairs cur key val)
1834     (setq pairs (gnus-split-string query "&"))
1835     (while pairs
1836       (setq cur (car pairs)
1837             pairs (cdr pairs))
1838       (if (not (string-match "=" cur))
1839           nil                           ; Grace
1840         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
1841               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
1842         (if downcase
1843             (setq key (downcase key)))
1844         (setq cur (assoc key retval))
1845         (if cur
1846             (setcdr cur (cons val (cdr cur)))
1847           (setq retval (cons (list key val) retval)))))
1848     retval))
1849  
1850 (defun gnus-url-unhex (x)
1851   (if (> x ?9)
1852       (if (>= x ?a)
1853           (+ 10 (- x ?a))
1854         (+ 10 (- x ?A)))
1855     (- x ?0)))
1856  
1857 (defun gnus-url-unhex-string (str &optional allow-newlines)
1858   "Remove %XXX embedded spaces, etc in a url.
1859 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
1860 decoding of carriage returns and line feeds in the string, which is normally
1861 forbidden in URL encoding."
1862   (setq str (or str ""))
1863   (let ((tmp "")
1864         (case-fold-search t))
1865     (while (string-match "%[0-9a-f][0-9a-f]" str)
1866       (let* ((start (match-beginning 0))
1867              (ch1 (gnus-url-unhex (elt str (+ start 1))))
1868              (code (+ (* 16 ch1)
1869                       (gnus-url-unhex (elt str (+ start 2))))))
1870         (setq tmp (concat 
1871                    tmp (substring str 0 start)
1872                    (cond
1873                     (allow-newlines
1874                      (char-to-string code))
1875                     ((or (= code ?\n) (= code ?\r))
1876                      " ")
1877                     (t (char-to-string code))))
1878               str (substring str (match-end 0)))))
1879     (setq tmp (concat tmp str))
1880     tmp))
1881  
1882 (defun gnus-url-mailto (url)
1883   ;; Send mail to someone
1884   (if (not (string-match "mailto:/*\\(.*\\)" url))
1885       (error "Malformed mailto link: %s" url))
1886   (setq url (substring url (match-beginning 1) nil))
1887   (let (to args source-url subject func)
1888     (if (string-match (regexp-quote "?") url)
1889         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
1890               args (gnus-url-parse-query-string
1891                     (substring url (match-end 0) nil) t))
1892       (setq to (gnus-url-unhex-string url)))
1893     (setq args (cons (list "to" to) args)
1894           subject (cdr-safe (assoc "subject" args)))
1895     (message-mail)
1896     (while args
1897       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
1898       (if (fboundp func)
1899           (funcall func)
1900         (message-position-on-field (caar args)))
1901       (insert (mapconcat 'identity (cdar args) ", "))
1902       (setq args (cdr args)))
1903     (if subject
1904         (message-goto-body)
1905       (message-goto-subject))))
1906
1907 (defun gnus-button-mailto (address)
1908   ;; Mail to ADDRESS.
1909   (set-buffer (gnus-copy-article-buffer))
1910   (message-reply address))
1911
1912 (defun gnus-button-reply (address)
1913   ;; Reply to ADDRESS.
1914   (message-reply address))
1915
1916 (defun gnus-button-url (address)
1917   "Browse ADDRESS."
1918   (funcall browse-url-browser-function address))
1919
1920 ;;; Next/prev buttons in the article buffer.
1921
1922 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
1923 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
1924
1925 (defvar gnus-prev-page-map nil)
1926 (unless gnus-prev-page-map
1927   (setq gnus-prev-page-map (make-sparse-keymap))
1928   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
1929   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
1930
1931 (defun gnus-insert-prev-page-button ()
1932   (let ((buffer-read-only nil))
1933     (gnus-eval-format 
1934      gnus-prev-page-line-format nil
1935      `(gnus-prev t local-map ,gnus-prev-page-map
1936                  gnus-callback gnus-article-button-prev-page))))
1937
1938 (defvar gnus-next-page-map nil)
1939 (unless gnus-next-page-map
1940   (setq gnus-next-page-map (make-keymap))
1941   (suppress-keymap gnus-prev-page-map)
1942   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
1943   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
1944
1945 (defun gnus-button-next-page ()
1946   "Go to the next page."
1947   (interactive)
1948   (let ((win (selected-window)))
1949     (select-window (get-buffer-window gnus-article-buffer t))
1950     (gnus-article-next-page)
1951     (select-window win)))
1952
1953 (defun gnus-button-prev-page ()
1954   "Go to the prev page."
1955   (interactive)
1956   (let ((win (selected-window)))
1957     (select-window (get-buffer-window gnus-article-buffer t))
1958     (gnus-article-prev-page)
1959     (select-window win)))
1960
1961 (defun gnus-insert-next-page-button ()
1962   (let ((buffer-read-only nil))
1963     (gnus-eval-format gnus-next-page-line-format nil
1964                       `(gnus-next t local-map ,gnus-next-page-map
1965                                   gnus-callback 
1966                                   gnus-article-button-next-page))))
1967
1968 (defun gnus-article-button-next-page (arg)
1969   "Go to the next page."
1970   (interactive "P")
1971   (let ((win (selected-window)))
1972     (select-window (get-buffer-window gnus-article-buffer t))
1973     (gnus-article-next-page)
1974     (select-window win)))
1975
1976 (defun gnus-article-button-prev-page (arg)
1977   "Go to the prev page."
1978   (interactive "P")
1979   (let ((win (selected-window)))
1980     (select-window (get-buffer-window gnus-article-buffer t))
1981     (gnus-article-prev-page)
1982     (select-window win))) 
1983
1984 (provide 'gnus-art)
1985
1986 ;;; gnus-art.el ends here