1 ;;; gnus-undo.el --- minor mode for undoing in Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
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)
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.
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.
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 random representation of the actual data), normal Emacs
29 ;; undoing doesn't work at all for Gnus.
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.
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.)
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.
49 (defvar gnus-undo-mode nil
50 "Minor mode for undoing in Gnus buffers.")
52 (defvar gnus-undo-mode-hook nil
53 "Hook called in all `gnus-undo-mode' buffers.")
55 ;;; Internal variables.
57 (defvar gnus-undo-actions nil)
58 (defvar gnus-undo-boundary t)
59 (defvar gnus-undo-last nil)
60 (defvar gnus-undo-boundary-inhibit nil)
62 ;;; Minor mode definition.
64 (defvar gnus-undo-mode-map nil)
66 (unless gnus-undo-mode-map
67 (setq gnus-undo-mode-map (make-sparse-keymap))
69 (gnus-define-keys gnus-undo-mode-map
72 (defun gnus-undo-make-menu-bar ()
73 (unless (boundp 'gnus-undo-menu)
75 gnus-undo-menu gnus-undo-mode-map ""
78 ["Undo" gnus-undo gnus-undo-actions])))))
80 (defun gnus-undo-mode (&optional arg)
81 "Minor mode for providing `undo' in Gnus buffers.
83 \\{gnus-undo-mode-map}"
85 (set (make-local-variable 'gnus-undo-mode)
86 (if (null arg) (not gnus-undo-mode)
87 (> (prefix-numeric-value arg) 0)))
88 (set (make-local-variable 'gnus-undo-actions) nil)
89 (set (make-local-variable 'gnus-undo-boundary) t)
92 (when (and menu-bar-mode
93 (gnus-visual-p 'undo-menu 'menu))
94 (gnus-undo-make-menu-bar))
95 ;; Don't display anything in the mode line -- too annoying.
96 ;;(unless (assq 'gnus-undo-mode minor-mode-alist)
97 ;; (push '(gnus-undo-mode " Undo") minor-mode-alist))
98 (unless (assq 'gnus-undo-mode minor-mode-map-alist)
99 (push (cons 'gnus-undo-mode gnus-undo-mode-map)
100 minor-mode-map-alist))
101 (gnus-make-local-hook 'post-command-hook)
102 (gnus-add-hook 'post-command-hook 'gnus-undo-boundary nil t)
103 (run-hooks 'gnus-undo-mode-hook)))
105 ;;; Interface functions.
107 (defun gnus-disable-undo (&optional buffer)
108 "Disable undoing in the current buffer."
113 (gnus-undo-mode -1)))
115 (defun gnus-undo-boundary ()
116 "Set Gnus undo boundary."
117 (if gnus-undo-boundary-inhibit
118 (setq gnus-undo-boundary-inhibit nil)
119 (setq gnus-undo-boundary t)))
121 (defun gnus-undo-register (form)
122 "Register FORMS as something to be performed to undo a change.
123 FORMS may use backtick quote syntax."
125 (gnus-undo-register-1
129 (put 'gnus-undo-register 'lisp-indent-function 0)
130 (put 'gnus-undo-register 'lisp-indent-hook 0)
131 (put 'gnus-undo-register 'edebug-form-spec '(body))
133 (defun gnus-undo-register-1 (function)
134 "Register FUNCTION as something to be performed to undo a change."
137 ;; We are on a boundary, so we create a new action.
139 (push (list function) gnus-undo-actions)
140 (setq gnus-undo-boundary nil))
141 ;; Prepend the function to an old action.
143 (setcar gnus-undo-actions (cons function (car gnus-undo-actions))))
146 (setq gnus-undo-actions (list (list function)))))
147 (setq gnus-undo-boundary-inhibit t)))
150 "Undo some previous changes in Gnus buffers.
151 Repeat this command to undo more changes.
152 A numeric argument serves as a repeat count."
154 (unless gnus-undo-mode
155 (error "Undoing is not enabled in this buffer"))
156 (message "%s" last-command)
157 (when (or (not (eq last-command 'gnus-undo))
158 (not gnus-undo-last))
159 (setq gnus-undo-last gnus-undo-actions))
160 (let ((action (pop gnus-undo-last)))
162 (error "Nothing further to undo"))
163 (setq gnus-undo-actions (delq action gnus-undo-actions))
164 (setq gnus-undo-boundary t)
166 (funcall (pop action)))))
170 ;;; gnus-undo.el ends here