*** empty log message ***
[gnus] / lisp / gnus-eform.el
1 ;;; gnus-eform.el --- a mode for editing forms 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-load)
29 (require 'gnus-win)
30 (require 'gnus)
31
32 ;;;
33 ;;; Editing forms
34 ;;;
35
36 (defvar gnus-edit-form-mode-hook nil
37   "Hook run in `gnus-edit-form-mode' buffers.")
38
39 (defvar gnus-edit-form-menu-hook nil
40   "Hook run when creating menus in `gnus-edit-form-mode' buffers.")
41
42 ;;; Internal variables
43
44 (defvar gnus-edit-form-done-function nil)
45 (defvar gnus-edit-form-buffer  "*Gnus edit form*")
46
47 (defvar gnus-edit-form-mode-map nil)
48 (unless gnus-edit-form-mode-map
49   (set gnus-edit-form-mode-map (copy-keymap emacs-lisp-mode-map))
50   (gnus-define-keys gnus-edit-form-mode-map
51     "\C-c\C-c" gnus-edit-form-done
52     "\C-c\C-k" gnus-edit-form-exit))
53
54 (defun gnus-edit-form-make-menu-bar ()
55   (unless (boundp 'gnus-edit-form-menu)
56     (easy-menu-define
57      gnus-edit-form-menu gnus-edit-form-mode-map ""
58      '("Edit Form"
59        ["Exit and save changes" gnus-edit-form-done t]
60        ["Exit" gnus-edit-form-exit t]))
61     (run-hooks 'gnus-edit-form-menu-hook)))
62
63 (defun gnus-edit-form-mode ()
64   "Major mode for editing forms.
65 It is a slightly enhanced emacs-lisp-mode.
66
67 \\{gnus-edit-form-mode-map}"
68   (interactive)
69   (when (and menu-bar-mode
70              (gnus-visual-p 'group-menu 'menu))
71     (gnus-edit-form-make-menu-bar))
72   (kill-all-local-variables)
73   (setq major-mode 'gnus-edit-form-mode)
74   (setq mode-name "Edit Form")
75   (use-local-map gnus-edit-form-mode-map)
76   (make-local-variable 'gnus-edit-form-done-function)
77   (make-local-variable 'gnus-prev-winconf)
78   (run-hooks 'gnus-edit-form-mode-hook))
79
80 (defun gnus-edit-form (form documentation exit-func)
81   "Edit FORM in a new buffer.
82 Call EXIT-FUNC on exit.  Display DOCUMENTATION in the beginning
83 of the buffer."
84   (let ((winconf (current-window-configuration)))
85     (set-buffer (setq gnus-edit-form-buffer 
86                       (get-buffer-create gnus-edit-form-buffer)))
87     (gnus-configure-windows 'edit-form)
88     (gnus-add-current-to-buffer-list)
89     (gnus-edit-form-mode)
90     (setq gnus-prev-winconf winconf)
91     (setq gnus-edit-form-done-function exit-func)
92     (erase-buffer)
93     (insert documentation)
94     (unless (bolp)
95       (insert "\n"))
96     (goto-char (point-min))
97     (while (not (eobp))
98       (insert ";;; ")
99       (forward-line 1))
100     (insert ";; Type `C-c C-c' after you've finished editing.\n")
101     (insert "\n")
102     (let ((p (point)))
103       (pp form (current-buffer))
104       (insert "\n")
105       (goto-char p))))
106
107 (defun gnus-edit-form-done ()
108   "Update changes and kill the current buffer."
109   (interactive)
110   (goto-char (point-min))
111   (let ((form (read (current-buffer))))
112     (gnus-edit-form-exit)
113     (funcall gnus-edit-form-done-function form)))
114   
115 (defun gnus-edit-form-exit ()
116   "Kill the current buffer."
117   (interactive)
118   (let ((winconf gnus-prev-winconf))
119     (kill-buffer (current-buffer))
120     (set-window-configuration winconf)))
121
122 (provide 'gnus-eform)
123
124 ;;; gnus-eform.el ends here