Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[bpt/emacs.git] / lisp / gnus / gnus-eform.el
CommitLineData
eec82323 1;;; gnus-eform.el --- a mode for editing forms for Gnus
e84b4b86
TTN
2
3;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
49f70d46 4;; 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
eec82323 5
6748645f 6;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
7;; Keywords: news
8
9;; This file is part of GNU Emacs.
10
5e809f55 11;; GNU Emacs is free software: you can redistribute it and/or modify
eec82323 12;; it under the terms of the GNU General Public License as published by
5e809f55
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
eec82323
LMI
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
5e809f55 18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eec82323
LMI
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
5e809f55 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
23
24;;; Commentary:
25
26;;; Code:
27
28(require 'gnus)
29(require 'gnus-win)
30
31;;;
32;;; Editing forms
33;;;
34
35(defgroup gnus-edit-form nil
36 "A mode for editing forms."
37 :group 'gnus)
38
39(defcustom gnus-edit-form-mode-hook nil
40 "Hook run in `gnus-edit-form-mode' buffers."
41 :group 'gnus-edit-form
42 :type 'hook)
43
44(defcustom gnus-edit-form-menu-hook nil
45 "Hook run when creating menus in `gnus-edit-form-mode' buffers."
46 :group 'gnus-edit-form
47 :type 'hook)
48
49;;; Internal variables
50
eec82323 51(defvar gnus-edit-form-buffer "*Gnus edit form*")
6748645f 52(defvar gnus-edit-form-done-function nil)
eec82323
LMI
53
54(defvar gnus-edit-form-mode-map nil)
55(unless gnus-edit-form-mode-map
16409b0b
GM
56 (setq gnus-edit-form-mode-map (make-sparse-keymap))
57 (set-keymap-parent gnus-edit-form-mode-map emacs-lisp-mode-map)
eec82323
LMI
58 (gnus-define-keys gnus-edit-form-mode-map
59 "\C-c\C-c" gnus-edit-form-done
60 "\C-c\C-k" gnus-edit-form-exit))
61
62(defun gnus-edit-form-make-menu-bar ()
63 (unless (boundp 'gnus-edit-form-menu)
64 (easy-menu-define
65 gnus-edit-form-menu gnus-edit-form-mode-map ""
66 '("Edit Form"
67 ["Exit and save changes" gnus-edit-form-done t]
68 ["Exit" gnus-edit-form-exit t]))
6748645f 69 (gnus-run-hooks 'gnus-edit-form-menu-hook)))
eec82323
LMI
70
71(defun gnus-edit-form-mode ()
72 "Major mode for editing forms.
73It is a slightly enhanced emacs-lisp-mode.
74
75\\{gnus-edit-form-mode-map}"
76 (interactive)
77 (when (gnus-visual-p 'group-menu 'menu)
78 (gnus-edit-form-make-menu-bar))
79 (kill-all-local-variables)
80 (setq major-mode 'gnus-edit-form-mode)
81 (setq mode-name "Edit Form")
82 (use-local-map gnus-edit-form-mode-map)
83 (make-local-variable 'gnus-edit-form-done-function)
84 (make-local-variable 'gnus-prev-winconf)
cfcd5c91 85 (gnus-run-mode-hooks 'gnus-edit-form-mode-hook))
eec82323 86
01c52d31 87(defun gnus-edit-form (form documentation exit-func &optional layout)
eec82323
LMI
88 "Edit FORM in a new buffer.
89Call EXIT-FUNC on exit. Display DOCUMENTATION in the beginning
01c52d31
MB
90of the buffer.
91The optional LAYOUT overrides the `edit-form' window layout."
eec82323 92 (let ((winconf (current-window-configuration)))
6748645f 93 (set-buffer (gnus-get-buffer-create gnus-edit-form-buffer))
01c52d31 94 (gnus-configure-windows (or layout 'edit-form))
eec82323
LMI
95 (gnus-edit-form-mode)
96 (setq gnus-prev-winconf winconf)
97 (setq gnus-edit-form-done-function exit-func)
98 (erase-buffer)
99 (insert documentation)
100 (unless (bolp)
101 (insert "\n"))
102 (goto-char (point-min))
103 (while (not (eobp))
104 (insert ";;; ")
105 (forward-line 1))
106 (insert ";; Type `C-c C-c' after you've finished editing.\n")
107 (insert "\n")
108 (let ((p (point)))
23f87bed 109 (gnus-pp form)
eec82323
LMI
110 (insert "\n")
111 (goto-char p))))
112
113(defun gnus-edit-form-done ()
114 "Update changes and kill the current buffer."
115 (interactive)
116 (goto-char (point-min))
23f87bed
MB
117 (let ((form (condition-case nil
118 (read (current-buffer))
119 (end-of-file nil)))
eec82323
LMI
120 (func gnus-edit-form-done-function))
121 (gnus-edit-form-exit)
122 (funcall func form)))
123
124(defun gnus-edit-form-exit ()
125 "Kill the current buffer."
126 (interactive)
127 (let ((winconf gnus-prev-winconf))
128 (kill-buffer (current-buffer))
129 (set-window-configuration winconf)))
130
131(provide 'gnus-eform)
132
cbee283d 133;; arch-tag: ef50678c-2c28-49ef-affc-e53b3b2c0bf6
eec82323 134;;; gnus-eform.el ends here