1 ;;; gnus-undo.el --- minor mode for undoing in Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
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 2, or (at your option)
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This package allows arbitrary undoing in Gnus buffers. As all the
29 ;; Gnus buffers aren't very text-oriented (what is in the buffers is
30 ;; just some arbitrary representation of the actual data), normal Emacs
31 ;; undoing doesn't work at all for Gnus.
33 ;; This package works by letting Gnus register functions for reversing
34 ;; actions, and then calling these functions when the user pushes the
35 ;; `undo' key. As with normal `undo', there it is possible to set
36 ;; undo boundaries and so on.
38 ;; Internally, the undo sequence is represented by the
39 ;; `gnus-undo-actions' list, where each element is a list of functions
40 ;; to be called, in sequence, to undo some action. (An "action" is a
41 ;; collection of functions.)
43 ;; For instance, a function for killing a group will call
44 ;; `gnus-undo-register' with a function that un-kills the group. This
45 ;; package will put that function into an action.
49 (eval-when-compile (require 'cl
))
55 (defgroup gnus-undo nil
56 "Undoing in Gnus buffers."
59 (defcustom gnus-undo-limit
2000
60 "The number of undoable actions recorded."
64 (defcustom gnus-undo-mode nil
65 "Minor mode for undoing in Gnus buffers."
69 (defcustom gnus-undo-mode-hook nil
70 "Hook called in all `gnus-undo-mode' buffers."
74 ;;; Internal variables.
76 (defvar gnus-undo-actions nil
)
77 (defvar gnus-undo-boundary t
)
78 (defvar gnus-undo-last nil
)
79 (defvar gnus-undo-boundary-inhibit nil
)
81 ;;; Minor mode definition.
83 (defvar gnus-undo-mode-map nil
)
85 (unless gnus-undo-mode-map
86 (setq gnus-undo-mode-map
(make-sparse-keymap))
88 (gnus-define-keys gnus-undo-mode-map
92 ;; many people are used to type `C-/' on X terminals and get `C-_'.
93 [(control /)] gnus-undo
))
95 (defun gnus-undo-make-menu-bar ()
96 ;; This is disabled for the time being.
98 (define-key-after (current-local-map) [menu-bar file gnus-undo
]
99 (cons "Undo" 'gnus-undo-actions
)
100 [menu-bar file whatever
])))
102 (defun gnus-undo-mode (&optional arg
)
103 "Minor mode for providing `undo' in Gnus buffers.
105 \\{gnus-undo-mode-map}"
107 (set (make-local-variable 'gnus-undo-mode
)
108 (if (null arg
) (not gnus-undo-mode
)
109 (> (prefix-numeric-value arg
) 0)))
110 (set (make-local-variable 'gnus-undo-actions
) nil
)
111 (set (make-local-variable 'gnus-undo-boundary
) t
)
114 (when (gnus-visual-p 'undo-menu
'menu
)
115 (gnus-undo-make-menu-bar))
116 (gnus-add-minor-mode 'gnus-undo-mode
"" gnus-undo-mode-map
)
117 (gnus-make-local-hook 'post-command-hook
)
118 (add-hook 'post-command-hook
'gnus-undo-boundary nil t
)
119 (gnus-run-hooks 'gnus-undo-mode-hook
)))
121 ;;; Interface functions.
123 (defun gnus-disable-undo (&optional buffer
)
124 "Disable undoing in the current buffer."
129 (gnus-undo-mode -
1)))
131 (defun gnus-undo-boundary ()
132 "Set Gnus undo boundary."
133 (if gnus-undo-boundary-inhibit
134 (setq gnus-undo-boundary-inhibit nil
)
135 (setq gnus-undo-boundary t
)))
137 (defun gnus-undo-force-boundary ()
138 "Set Gnus undo boundary."
139 (setq gnus-undo-boundary-inhibit nil
140 gnus-undo-boundary t
))
142 (defun gnus-undo-register (form)
143 "Register FORMS as something to be performed to undo a change.
144 FORMS may use backtick quote syntax."
146 (gnus-undo-register-1
150 (put 'gnus-undo-register
'lisp-indent-function
0)
151 (put 'gnus-undo-register
'edebug-form-spec
'(body))
153 (defun gnus-undo-register-1 (function)
154 "Register FUNCTION as something to be performed to undo a change."
157 ;; We are on a boundary, so we create a new action.
159 (push (list function
) gnus-undo-actions
)
160 (setq gnus-undo-boundary nil
))
161 ;; Prepend the function to an old action.
163 (setcar gnus-undo-actions
(cons function
(car gnus-undo-actions
))))
166 (setq gnus-undo-actions
(list (list function
)))))
167 ;; Limit the length of the undo list.
168 (let ((next (nthcdr gnus-undo-limit gnus-undo-actions
)))
171 ;; We are not at a boundary...
172 (setq gnus-undo-boundary-inhibit t
)))
175 "Undo some previous changes in Gnus buffers.
176 Repeat this command to undo more changes.
177 A numeric argument serves as a repeat count."
179 (unless gnus-undo-mode
180 (error "Undoing is not enabled in this buffer"))
181 (message "%s" last-command
)
182 (when (or (not (eq last-command
'gnus-undo
))
183 (not gnus-undo-last
))
184 (setq gnus-undo-last gnus-undo-actions
))
185 (let ((action (pop gnus-undo-last
)))
187 (error "Nothing further to undo"))
188 (setq gnus-undo-actions
(delq action gnus-undo-actions
))
189 (setq gnus-undo-boundary t
)
191 (funcall (pop action
)))))
195 ;;; arch-tag: 0d787bc7-787d-499a-837f-211d2cb07f2e
196 ;;; gnus-undo.el ends here