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