Commit | Line | Data |
---|---|---|
eec82323 | 1 | ;;; gnus-undo.el --- minor mode for undoing in Gnus |
16409b0b | 2 | |
b6c2d8c6 | 3 | ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
114f9c96 | 4 | ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
eec82323 | 5 | |
6748645f | 6 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> |
eec82323 LMI |
7 | ;; Keywords: news |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
5e809f55 | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
eec82323 | 12 | ;; it under the terms of the GNU General Public License as published by |
5e809f55 GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
eec82323 LMI |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
5e809f55 | 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
eec82323 LMI |
19 | ;; GNU General Public License for more details. |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
5e809f55 | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
eec82323 LMI |
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 | |
6748645f | 28 | ;; just some arbitrary representation of the actual data), normal Emacs |
eec82323 LMI |
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 | ||
249ffa67 | 47 | (eval-when-compile (require 'cl)) |
d029b5d2 KY |
48 | (eval-when-compile |
49 | (when (featurep 'xemacs) | |
50 | (require 'easy-mmode))) ; for `define-minor-mode' | |
249ffa67 | 51 | |
eec82323 LMI |
52 | (require 'gnus-util) |
53 | (require 'gnus) | |
6748645f LMI |
54 | |
55 | (defgroup gnus-undo nil | |
56 | "Undoing in Gnus buffers." | |
57 | :group 'gnus) | |
58 | ||
59 | (defcustom gnus-undo-limit 2000 | |
60 | "The number of undoable actions recorded." | |
61 | :type 'integer | |
62 | :group 'gnus-undo) | |
eec82323 | 63 | |
6748645f | 64 | (defcustom gnus-undo-mode nil |
bbf52f1e SM |
65 | ;; FIXME: This is a buffer-local minor mode which requires running |
66 | ;; code upon activation/deactivation, so defining it as a defcustom | |
67 | ;; doesn't seem very useful: setting it to non-nil via Customize | |
68 | ;; probably won't do the right thing. | |
6748645f LMI |
69 | "Minor mode for undoing in Gnus buffers." |
70 | :type 'boolean | |
71 | :group 'gnus-undo) | |
eec82323 | 72 | |
6748645f LMI |
73 | (defcustom gnus-undo-mode-hook nil |
74 | "Hook called in all `gnus-undo-mode' buffers." | |
75 | :type 'hook | |
76 | :group 'gnus-undo) | |
eec82323 LMI |
77 | |
78 | ;;; Internal variables. | |
79 | ||
80 | (defvar gnus-undo-actions nil) | |
81 | (defvar gnus-undo-boundary t) | |
82 | (defvar gnus-undo-last nil) | |
83 | (defvar gnus-undo-boundary-inhibit nil) | |
84 | ||
85 | ;;; Minor mode definition. | |
86 | ||
bbf52f1e SM |
87 | (defvar gnus-undo-mode-map |
88 | (let ((map (make-sparse-keymap))) | |
89 | (gnus-define-keys map | |
90 | "\M-\C-_" gnus-undo | |
91 | "\C-_" gnus-undo | |
92 | "\C-xu" gnus-undo | |
93 | ;; many people are used to type `C-/' on X terminals and get `C-_'. | |
94 | [(control /)] gnus-undo) | |
95 | map)) | |
eec82323 LMI |
96 | |
97 | (defun gnus-undo-make-menu-bar () | |
a8151ef7 | 98 | ;; This is disabled for the time being. |
eec82323 | 99 | (when nil |
a8151ef7 LMI |
100 | (define-key-after (current-local-map) [menu-bar file gnus-undo] |
101 | (cons "Undo" 'gnus-undo-actions) | |
102 | [menu-bar file whatever]))) | |
eec82323 | 103 | |
bbf52f1e | 104 | (define-minor-mode gnus-undo-mode |
eec82323 LMI |
105 | "Minor mode for providing `undo' in Gnus buffers. |
106 | ||
107 | \\{gnus-undo-mode-map}" | |
bbf52f1e | 108 | :keymap gnus-undo-mode-map |
eec82323 LMI |
109 | (set (make-local-variable 'gnus-undo-actions) nil) |
110 | (set (make-local-variable 'gnus-undo-boundary) t) | |
111 | (when gnus-undo-mode | |
112 | ;; Set up the menu. | |
113 | (when (gnus-visual-p 'undo-menu 'menu) | |
114 | (gnus-undo-make-menu-bar)) | |
23f87bed | 115 | (gnus-make-local-hook 'post-command-hook) |
bbf52f1e | 116 | (add-hook 'post-command-hook 'gnus-undo-boundary nil t))) |
eec82323 LMI |
117 | |
118 | ;;; Interface functions. | |
119 | ||
120 | (defun gnus-disable-undo (&optional buffer) | |
121 | "Disable undoing in the current buffer." | |
122 | (interactive) | |
123 | (save-excursion | |
124 | (when buffer | |
125 | (set-buffer buffer)) | |
126 | (gnus-undo-mode -1))) | |
127 | ||
128 | (defun gnus-undo-boundary () | |
129 | "Set Gnus undo boundary." | |
130 | (if gnus-undo-boundary-inhibit | |
131 | (setq gnus-undo-boundary-inhibit nil) | |
132 | (setq gnus-undo-boundary t))) | |
133 | ||
a8151ef7 LMI |
134 | (defun gnus-undo-force-boundary () |
135 | "Set Gnus undo boundary." | |
136 | (setq gnus-undo-boundary-inhibit nil | |
137 | gnus-undo-boundary t)) | |
138 | ||
eec82323 LMI |
139 | (defun gnus-undo-register (form) |
140 | "Register FORMS as something to be performed to undo a change. | |
141 | FORMS may use backtick quote syntax." | |
142 | (when gnus-undo-mode | |
143 | (gnus-undo-register-1 | |
144 | `(lambda () | |
145 | ,form)))) | |
146 | ||
147 | (put 'gnus-undo-register 'lisp-indent-function 0) | |
148 | (put 'gnus-undo-register 'edebug-form-spec '(body)) | |
149 | ||
150 | (defun gnus-undo-register-1 (function) | |
151 | "Register FUNCTION as something to be performed to undo a change." | |
152 | (when gnus-undo-mode | |
153 | (cond | |
154 | ;; We are on a boundary, so we create a new action. | |
155 | (gnus-undo-boundary | |
156 | (push (list function) gnus-undo-actions) | |
157 | (setq gnus-undo-boundary nil)) | |
158 | ;; Prepend the function to an old action. | |
159 | (gnus-undo-actions | |
160 | (setcar gnus-undo-actions (cons function (car gnus-undo-actions)))) | |
161 | ;; Initialize list. | |
162 | (t | |
163 | (setq gnus-undo-actions (list (list function))))) | |
6748645f LMI |
164 | ;; Limit the length of the undo list. |
165 | (let ((next (nthcdr gnus-undo-limit gnus-undo-actions))) | |
166 | (when next | |
167 | (setcdr next nil))) | |
168 | ;; We are not at a boundary... | |
eec82323 LMI |
169 | (setq gnus-undo-boundary-inhibit t))) |
170 | ||
171 | (defun gnus-undo (n) | |
172 | "Undo some previous changes in Gnus buffers. | |
173 | Repeat this command to undo more changes. | |
174 | A numeric argument serves as a repeat count." | |
175 | (interactive "p") | |
176 | (unless gnus-undo-mode | |
177 | (error "Undoing is not enabled in this buffer")) | |
178 | (message "%s" last-command) | |
179 | (when (or (not (eq last-command 'gnus-undo)) | |
180 | (not gnus-undo-last)) | |
181 | (setq gnus-undo-last gnus-undo-actions)) | |
182 | (let ((action (pop gnus-undo-last))) | |
183 | (unless action | |
184 | (error "Nothing further to undo")) | |
185 | (setq gnus-undo-actions (delq action gnus-undo-actions)) | |
186 | (setq gnus-undo-boundary t) | |
01c52d31 | 187 | (mapc 'funcall action))) |
eec82323 LMI |
188 | |
189 | (provide 'gnus-undo) | |
190 | ||
191 | ;;; gnus-undo.el ends here |