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