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