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