*** empty log message ***
[gnus] / lisp / gnus-undo.el
1 ;;; gnus-undo.el --- minor mode for undoing in Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
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 (require 'custom)
52
53 (defgroup gnus-undo nil
54   "Undoing in Gnus buffers."
55   :group 'gnus)
56
57 (defcustom gnus-undo-limit 2000
58   "The number of undoable actions recorded."
59   :type 'integer
60   :group 'gnus-undo)
61
62 (defcustom gnus-undo-mode nil
63   "Minor mode for undoing in Gnus buffers."
64   :type 'boolean
65   :group 'gnus-undo)
66
67 (defcustom gnus-undo-mode-hook nil
68   "Hook called in all `gnus-undo-mode' buffers."
69   :type 'hook
70   :group 'gnus-undo)
71
72 ;;; Internal variables.
73
74 (defvar gnus-undo-actions nil)
75 (defvar gnus-undo-boundary t)
76 (defvar gnus-undo-last nil)
77 (defvar gnus-undo-boundary-inhibit nil)
78
79 ;;; Minor mode definition.
80
81 (defvar gnus-undo-mode-map nil)
82
83 (unless gnus-undo-mode-map
84   (setq gnus-undo-mode-map (make-sparse-keymap))
85
86   (gnus-define-keys gnus-undo-mode-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
93 (defun gnus-undo-make-menu-bar ()
94   ;; This is disabled for the time being.
95   (when nil
96     (define-key-after (current-local-map) [menu-bar file gnus-undo]
97       (cons "Undo" 'gnus-undo-actions)
98       [menu-bar file whatever])))
99
100 (defun gnus-undo-mode (&optional arg)
101   "Minor mode for providing `undo' in Gnus buffers.
102
103 \\{gnus-undo-mode-map}"
104   (interactive "P")
105   (set (make-local-variable 'gnus-undo-mode)
106        (if (null arg) (not gnus-undo-mode)
107          (> (prefix-numeric-value arg) 0)))
108   (set (make-local-variable 'gnus-undo-actions) nil)
109   (set (make-local-variable 'gnus-undo-boundary) t)
110   (when gnus-undo-mode
111     ;; Set up the menu.
112     (when (gnus-visual-p 'undo-menu 'menu)
113       (gnus-undo-make-menu-bar))
114     (gnus-add-minor-mode 'gnus-undo-mode "" gnus-undo-mode-map)
115     (make-local-hook 'post-command-hook)
116     (add-hook 'post-command-hook 'gnus-undo-boundary nil t)
117     (gnus-run-hooks 'gnus-undo-mode-hook)))
118
119 ;;; Interface functions.
120
121 (defun gnus-disable-undo (&optional buffer)
122   "Disable undoing in the current buffer."
123   (interactive)
124   (save-excursion
125     (when buffer
126       (set-buffer buffer))
127     (gnus-undo-mode -1)))
128
129 (defun gnus-undo-boundary ()
130   "Set Gnus undo boundary."
131   (if gnus-undo-boundary-inhibit
132       (setq gnus-undo-boundary-inhibit nil)
133     (setq gnus-undo-boundary t)))
134
135 (defun gnus-undo-force-boundary ()
136   "Set Gnus undo boundary."
137   (setq gnus-undo-boundary-inhibit nil
138         gnus-undo-boundary t))
139
140 (defun gnus-undo-register (form)
141   "Register FORMS as something to be performed to undo a change.
142 FORMS may use backtick quote syntax."
143   (when gnus-undo-mode
144     (gnus-undo-register-1
145      `(lambda ()
146         ,form))))
147
148 (put 'gnus-undo-register 'lisp-indent-function 0)
149 (put 'gnus-undo-register 'edebug-form-spec '(body))
150
151 (defun gnus-undo-register-1 (function)
152   "Register FUNCTION as something to be performed to undo a change."
153   (when gnus-undo-mode
154     (cond
155      ;; We are on a boundary, so we create a new action.
156      (gnus-undo-boundary
157       (push (list function) gnus-undo-actions)
158       (setq gnus-undo-boundary nil))
159      ;; Prepend the function to an old action.
160      (gnus-undo-actions
161       (setcar gnus-undo-actions (cons function (car gnus-undo-actions))))
162      ;; Initialize list.
163      (t
164       (setq gnus-undo-actions (list (list function)))))
165     ;; Limit the length of the undo list.
166     (let ((next (nthcdr gnus-undo-limit gnus-undo-actions)))
167       (when next
168         (setcdr next nil)))
169     ;; We are not at a boundary...
170     (setq gnus-undo-boundary-inhibit t)))
171
172 (defun gnus-undo (n)
173   "Undo some previous changes in Gnus buffers.
174 Repeat this command to undo more changes.
175 A numeric argument serves as a repeat count."
176   (interactive "p")
177   (unless gnus-undo-mode
178     (error "Undoing is not enabled in this buffer"))
179   (message "%s" last-command)
180   (when (or (not (eq last-command 'gnus-undo))
181             (not gnus-undo-last))
182     (setq gnus-undo-last gnus-undo-actions))
183   (let ((action (pop gnus-undo-last)))
184     (unless action
185       (error "Nothing further to undo"))
186     (setq gnus-undo-actions (delq action gnus-undo-actions))
187     (setq gnus-undo-boundary t)
188     (while action
189       (funcall (pop action)))))
190
191 (provide 'gnus-undo)
192
193 ;;; gnus-undo.el ends here