Use define-minor-mode in Gnus where applicable.
[bpt/emacs.git] / lisp / gnus / gnus-undo.el
1 ;;; gnus-undo.el --- minor mode for undoing in Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This package allows arbitrary undoing in Gnus buffers. As all the
27 ;; Gnus buffers aren't very text-oriented (what is in the buffers is
28 ;; just some arbitrary representation of the actual data), normal Emacs
29 ;; undoing doesn't work at all for Gnus.
30 ;;
31 ;; This package works by letting Gnus register functions for reversing
32 ;; actions, and then calling these functions when the user pushes the
33 ;; `undo' key. As with normal `undo', there it is possible to set
34 ;; undo boundaries and so on.
35 ;;
36 ;; Internally, the undo sequence is represented by the
37 ;; `gnus-undo-actions' list, where each element is a list of functions
38 ;; to be called, in sequence, to undo some action. (An "action" is a
39 ;; collection of functions.)
40 ;;
41 ;; For instance, a function for killing a group will call
42 ;; `gnus-undo-register' with a function that un-kills the group. This
43 ;; package will put that function into an action.
44
45 ;;; Code:
46
47 (eval-when-compile (require 'cl))
48
49 (require 'gnus-util)
50 (require 'gnus)
51
52 (defgroup gnus-undo nil
53 "Undoing in Gnus buffers."
54 :group 'gnus)
55
56 (defcustom gnus-undo-limit 2000
57 "The number of undoable actions recorded."
58 :type 'integer
59 :group 'gnus-undo)
60
61 (defcustom gnus-undo-mode nil
62 ;; FIXME: This is a buffer-local minor mode which requires running
63 ;; code upon activation/deactivation, so defining it as a defcustom
64 ;; doesn't seem very useful: setting it to non-nil via Customize
65 ;; probably won't do the right thing.
66 "Minor mode for undoing in Gnus buffers."
67 :type 'boolean
68 :group 'gnus-undo)
69
70 (defcustom gnus-undo-mode-hook nil
71 "Hook called in all `gnus-undo-mode' buffers."
72 :type 'hook
73 :group 'gnus-undo)
74
75 ;;; Internal variables.
76
77 (defvar gnus-undo-actions nil)
78 (defvar gnus-undo-boundary t)
79 (defvar gnus-undo-last nil)
80 (defvar gnus-undo-boundary-inhibit nil)
81
82 ;;; Minor mode definition.
83
84 (defvar gnus-undo-mode-map
85 (let ((map (make-sparse-keymap)))
86 (gnus-define-keys map
87 "\M-\C-_" gnus-undo
88 "\C-_" gnus-undo
89 "\C-xu" gnus-undo
90 ;; many people are used to type `C-/' on X terminals and get `C-_'.
91 [(control /)] gnus-undo)
92 map))
93
94 (defun gnus-undo-make-menu-bar ()
95 ;; This is disabled for the time being.
96 (when nil
97 (define-key-after (current-local-map) [menu-bar file gnus-undo]
98 (cons "Undo" 'gnus-undo-actions)
99 [menu-bar file whatever])))
100
101 (define-minor-mode gnus-undo-mode
102 "Minor mode for providing `undo' in Gnus buffers.
103
104 \\{gnus-undo-mode-map}"
105 :keymap gnus-undo-mode-map
106 (set (make-local-variable 'gnus-undo-actions) nil)
107 (set (make-local-variable 'gnus-undo-boundary) t)
108 (when gnus-undo-mode
109 ;; Set up the menu.
110 (when (gnus-visual-p 'undo-menu 'menu)
111 (gnus-undo-make-menu-bar))
112 (gnus-make-local-hook 'post-command-hook)
113 (add-hook 'post-command-hook 'gnus-undo-boundary nil t)))
114
115 ;;; Interface functions.
116
117 (defun gnus-disable-undo (&optional buffer)
118 "Disable undoing in the current buffer."
119 (interactive)
120 (save-excursion
121 (when buffer
122 (set-buffer buffer))
123 (gnus-undo-mode -1)))
124
125 (defun gnus-undo-boundary ()
126 "Set Gnus undo boundary."
127 (if gnus-undo-boundary-inhibit
128 (setq gnus-undo-boundary-inhibit nil)
129 (setq gnus-undo-boundary t)))
130
131 (defun gnus-undo-force-boundary ()
132 "Set Gnus undo boundary."
133 (setq gnus-undo-boundary-inhibit nil
134 gnus-undo-boundary t))
135
136 (defun gnus-undo-register (form)
137 "Register FORMS as something to be performed to undo a change.
138 FORMS may use backtick quote syntax."
139 (when gnus-undo-mode
140 (gnus-undo-register-1
141 `(lambda ()
142 ,form))))
143
144 (put 'gnus-undo-register 'lisp-indent-function 0)
145 (put 'gnus-undo-register 'edebug-form-spec '(body))
146
147 (defun gnus-undo-register-1 (function)
148 "Register FUNCTION as something to be performed to undo a change."
149 (when gnus-undo-mode
150 (cond
151 ;; We are on a boundary, so we create a new action.
152 (gnus-undo-boundary
153 (push (list function) gnus-undo-actions)
154 (setq gnus-undo-boundary nil))
155 ;; Prepend the function to an old action.
156 (gnus-undo-actions
157 (setcar gnus-undo-actions (cons function (car gnus-undo-actions))))
158 ;; Initialize list.
159 (t
160 (setq gnus-undo-actions (list (list function)))))
161 ;; Limit the length of the undo list.
162 (let ((next (nthcdr gnus-undo-limit gnus-undo-actions)))
163 (when next
164 (setcdr next nil)))
165 ;; We are not at a boundary...
166 (setq gnus-undo-boundary-inhibit t)))
167
168 (defun gnus-undo (n)
169 "Undo some previous changes in Gnus buffers.
170 Repeat this command to undo more changes.
171 A numeric argument serves as a repeat count."
172 (interactive "p")
173 (unless gnus-undo-mode
174 (error "Undoing is not enabled in this buffer"))
175 (message "%s" last-command)
176 (when (or (not (eq last-command 'gnus-undo))
177 (not gnus-undo-last))
178 (setq gnus-undo-last gnus-undo-actions))
179 (let ((action (pop gnus-undo-last)))
180 (unless action
181 (error "Nothing further to undo"))
182 (setq gnus-undo-actions (delq action gnus-undo-actions))
183 (setq gnus-undo-boundary t)
184 (mapc 'funcall action)))
185
186 (provide 'gnus-undo)
187
188 ;; arch-tag: 0d787bc7-787d-499a-837f-211d2cb07f2e
189 ;;; gnus-undo.el ends here