95d3bcd6938517f92540a577cb5206cea82c73a7
[gnus] / lisp / gnus-undo.el
1 ;;; gnus-undo.el --- minor mode for undoing in Gnus
2 ;; Copyright (C) 1996 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
49 (defvar gnus-undo-mode nil
50   "Minor mode for undoing in Gnus buffers.")
51
52 (defvar gnus-undo-mode-hook nil
53   "Hook called in all `gnus-undo-mode' buffers.")
54
55 ;;; Internal variables.
56
57 (defvar gnus-undo-actions nil)
58 (defvar gnus-undo-boundary t)
59 (defvar gnus-undo-last nil)
60
61 ;;; Minor mode definition.
62
63 (defvar gnus-undo-mode-map nil)
64
65 (unless gnus-undo-mode-map
66   (setq gnus-undo-mode-map (make-sparse-keymap))
67
68   (gnus-define-keys gnus-undo-mode-map
69    "\M-\C-_" gnus-undo))
70
71 (defun gnus-undo-make-menu-bar ()
72   (unless (boundp 'gnus-undo-menu)
73     (easy-menu-define
74      gnus-undo-menu gnus-undo-mode-map ""
75      '("Undo"
76        ("Undo"
77         ["Undo" gnus-undo gnus-undo-actions])))))
78
79 (defun gnus-undo-mode (&optional arg)
80   "Minor mode for providing `undo' in Gnus buffers.
81
82 \\{gnus-undo-mode-map}"
83   (interactive "P")
84   (set (make-local-variable 'gnus-undo-mode)
85        (if (null arg) (not gnus-undo-mode)
86          (> (prefix-numeric-value arg) 0)))
87   (set (make-local-variable 'gnus-undo-actions) nil)
88   (set (make-local-variable 'gnus-undo-boundary) t)
89   (when gnus-undo-mode
90     ;; Set up the menu.
91     (when (and menu-bar-mode
92                (gnus-visual-p 'undo-menu 'menu))
93       (gnus-undo-make-menu-bar))
94     ;; Don't display anything in the mode line -- too annoying.
95     ;;(unless (assq 'gnus-undo-mode minor-mode-alist)
96     ;;  (push '(gnus-undo-mode " Undo") minor-mode-alist))
97     (unless (assq 'gnus-undo-mode minor-mode-map-alist)
98       (push (cons 'gnus-undo-mode gnus-undo-mode-map)
99             minor-mode-map-alist))
100     (gnus-make-local-hook 'post-command-hook)
101     (gnus-add-hook 'post-command-hook 'gnus-undo-boundary nil t)
102     (run-hooks 'gnus-undo-mode-hook)))
103
104 ;;; Interface functions.
105
106 (defun gnus-disable-undo (&optional buffer)
107   "Disable undoing in the current buffer."
108   (interactive)
109   (save-excursion
110     (when buffer
111       (set-buffer buffer))
112     (gnus-undo-mode -1)))
113
114 (defun gnus-undo-boundary ()
115   "Set Gnus undo boundary."
116   (setq gnus-undo-boundary t))
117
118 (defun gnus-undo-register (function)
119   "Register FUNCTION as something to be performed to undo a change."
120   (when gnus-undo-mode
121     (cond
122      ;; We are on a boundary, so we create a new action.
123      (gnus-undo-boundary
124       (push (list function) gnus-undo-actions)
125       (setq gnus-undo-boundary nil))
126      ;; Prepend the function to an old action.
127      (gnus-undo-actions
128       (setcar gnus-undo-actions (cons function (car gnus-undo-actions))))
129      ;; Initialize list.
130      (t
131       (setq gnus-undo-actions (list (list function)))))))
132
133 (defun gnus-undo (n)
134   "Undo some previous changes in Gnus buffers.
135 Repeat this command to undo more changes.
136 A numeric argument serves as a repeat count."
137   (interactive "p")
138   (unless gnus-undo-mode
139     (error "Undoing is not enabled in this buffer"))
140   (when (or (not (eq last-command 'gnus-undo))
141             (not gnus-undo-last))
142     (setq gnus-undo-last gnus-undo-actions))
143   (let (actions action)
144     (while (setq actions (pop gnus-undo-last))
145       (unless action
146         (errror "Nothing further to undo"))
147       (setq gnus-undo-actions (delq action gnus-undo-actions))
148       (while action
149         (funcall (pop action))))))
150
151 (provide 'gnus-undo)
152
153 ;;; gnus-undo.el ends here