*** empty log message ***
[gnus] / lisp / gnus-undo.el
1 ;;; gnus-undo.el --- minor mode for undoing in Gnus
2 ;; Copyright (C) 1996,97 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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 random 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 (require 'gnus-util)
48 (require 'gnus)
49
50 (defvar gnus-undo-mode nil
51   "Minor mode for undoing in Gnus buffers.")
52
53 (defvar gnus-undo-mode-hook nil
54   "Hook called in all `gnus-undo-mode' buffers.")
55
56 ;;; Internal variables.
57
58 (defvar gnus-undo-actions nil)
59 (defvar gnus-undo-boundary t)
60 (defvar gnus-undo-last nil)
61 (defvar gnus-undo-boundary-inhibit nil)
62
63 ;;; Minor mode definition.
64
65 (defvar gnus-undo-mode-map nil)
66
67 (unless gnus-undo-mode-map
68   (setq gnus-undo-mode-map (make-sparse-keymap))
69
70   (gnus-define-keys gnus-undo-mode-map
71    "\M-\C-_"     gnus-undo
72    "\C-_"        gnus-undo
73    "\C-xu"       gnus-undo
74    [(control /)] gnus-undo    ; many people are used to type `C-/' on
75                               ; X terminals and get `C-_'.
76    ))
77
78 (defun gnus-undo-make-menu-bar ()
79   (when nil
80   (define-key-after (current-local-map) [menu-bar file gnus-undo]
81     (cons "Undo" 'gnus-undo-actions)
82     [menu-bar file whatever])))
83
84 (defun gnus-undo-mode (&optional arg)
85   "Minor mode for providing `undo' in Gnus buffers.
86
87 \\{gnus-undo-mode-map}"
88   (interactive "P")
89   (set (make-local-variable 'gnus-undo-mode)
90        (if (null arg) (not gnus-undo-mode)
91          (> (prefix-numeric-value arg) 0)))
92   (set (make-local-variable 'gnus-undo-actions) nil)
93   (set (make-local-variable 'gnus-undo-boundary) t)
94   (when gnus-undo-mode
95     ;; Set up the menu.
96     (when (gnus-visual-p 'undo-menu 'menu)
97       (gnus-undo-make-menu-bar))
98     ;; Don't display anything in the mode line -- too annoying.
99     ;;(unless (assq 'gnus-undo-mode minor-mode-alist)
100     ;;  (push '(gnus-undo-mode " Undo") minor-mode-alist))
101     (unless (assq 'gnus-undo-mode minor-mode-map-alist)
102       (push (cons 'gnus-undo-mode gnus-undo-mode-map)
103             minor-mode-map-alist))
104     (make-local-hook 'post-command-hook)
105     (add-hook 'post-command-hook 'gnus-undo-boundary nil t)
106     (add-hook 'gnus-summary-exit-hook 'gnus-undo-boundary)
107     (run-hooks 'gnus-undo-mode-hook)))
108
109 ;;; Interface functions.
110
111 (defun gnus-disable-undo (&optional buffer)
112   "Disable undoing in the current buffer."
113   (interactive)
114   (save-excursion
115     (when buffer
116       (set-buffer buffer))
117     (gnus-undo-mode -1)))
118
119 (defun gnus-undo-boundary ()
120   "Set Gnus undo boundary."
121   (if gnus-undo-boundary-inhibit
122       (setq gnus-undo-boundary-inhibit nil)
123     (setq gnus-undo-boundary t)))
124
125 (defun gnus-undo-register (form)
126   "Register FORMS as something to be performed to undo a change.
127 FORMS may use backtick quote syntax."
128   (when gnus-undo-mode
129     (gnus-undo-register-1
130      `(lambda ()
131         ,form))))
132
133 (put 'gnus-undo-register 'lisp-indent-function 0)
134 (put 'gnus-undo-register 'edebug-form-spec '(body))
135
136 (defun gnus-undo-register-1 (function)
137   "Register FUNCTION as something to be performed to undo a change."
138   (when gnus-undo-mode
139     (cond
140      ;; We are on a boundary, so we create a new action.
141      (gnus-undo-boundary
142       (push (list function) gnus-undo-actions)
143       (setq gnus-undo-boundary nil))
144      ;; Prepend the function to an old action.
145      (gnus-undo-actions
146       (setcar gnus-undo-actions (cons function (car gnus-undo-actions))))
147      ;; Initialize list.
148      (t
149       (setq gnus-undo-actions (list (list function)))))
150     (setq gnus-undo-boundary-inhibit t)))
151
152 (defun gnus-undo (n)
153   "Undo some previous changes in Gnus buffers.
154 Repeat this command to undo more changes.
155 A numeric argument serves as a repeat count."
156   (interactive "p")
157   (unless gnus-undo-mode
158     (error "Undoing is not enabled in this buffer"))
159   (message "%s" last-command)
160   (when (or (not (eq last-command 'gnus-undo))
161             (not gnus-undo-last))
162     (setq gnus-undo-last gnus-undo-actions))
163   (let ((action (pop gnus-undo-last)))
164     (unless action
165       (error "Nothing further to undo"))
166     (setq gnus-undo-actions (delq action gnus-undo-actions))
167     (setq gnus-undo-boundary t)
168     (while action
169       (funcall (pop action)))))
170
171 (provide 'gnus-undo)
172
173 ;;; gnus-undo.el ends here