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