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