]> code.delx.au - gnu-emacs/blob - lisp/gnus/gnus-undo.el
Include versioned preloaded libraries in `package--builtin-versions'
[gnu-emacs] / lisp / gnus / gnus-undo.el
1 ;;; gnus-undo.el --- minor mode for undoing in Gnus
2
3 ;; Copyright (C) 1996-2016 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; This package allows arbitrary undoing in Gnus buffers. As all the
26 ;; Gnus buffers aren't very text-oriented (what is in the buffers is
27 ;; just some arbitrary representation of the actual data), normal Emacs
28 ;; undoing doesn't work at all for Gnus.
29 ;;
30 ;; This package works by letting Gnus register functions for reversing
31 ;; actions, and then calling these functions when the user pushes the
32 ;; `undo' key. As with normal `undo', there it is possible to set
33 ;; undo boundaries and so on.
34 ;;
35 ;; Internally, the undo sequence is represented by the
36 ;; `gnus-undo-actions' list, where each element is a list of functions
37 ;; to be called, in sequence, to undo some action. (An "action" is a
38 ;; collection of functions.)
39 ;;
40 ;; For instance, a function for killing a group will call
41 ;; `gnus-undo-register' with a function that un-kills the group. This
42 ;; package will put that function into an action.
43
44 ;;; Code:
45
46 (eval-when-compile (require 'cl))
47
48 (require 'gnus-util)
49 (require 'gnus)
50
51 (defgroup gnus-undo nil
52 "Undoing in Gnus buffers."
53 :group 'gnus)
54
55 (defcustom gnus-undo-limit 2000
56 "The number of undoable actions recorded."
57 :type 'integer
58 :group 'gnus-undo)
59
60 (defcustom gnus-undo-mode nil
61 ;; FIXME: This is a buffer-local minor mode which requires running
62 ;; code upon activation/deactivation, so defining it as a defcustom
63 ;; doesn't seem very useful: setting it to non-nil via Customize
64 ;; probably won't do the right thing.
65 "Minor mode for undoing in Gnus buffers."
66 :type 'boolean
67 :group 'gnus-undo)
68
69 (defcustom gnus-undo-mode-hook nil
70 "Hook called in all `gnus-undo-mode' buffers."
71 :type 'hook
72 :group 'gnus-undo)
73
74 ;;; Internal variables.
75
76 (defvar gnus-undo-actions nil)
77 (defvar gnus-undo-boundary t)
78 (defvar gnus-undo-last nil)
79 (defvar gnus-undo-boundary-inhibit nil)
80
81 ;;; Minor mode definition.
82
83 (defvar gnus-undo-mode-map
84 (let ((map (make-sparse-keymap)))
85 (gnus-define-keys map
86 "\M-\C-_" gnus-undo
87 "\C-_" gnus-undo
88 "\C-xu" gnus-undo
89 ;; many people are used to type `C-/' on X terminals and get `C-_'.
90 [(control /)] gnus-undo)
91 map))
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 (define-minor-mode gnus-undo-mode
101 "Minor mode for providing `undo' in Gnus buffers.
102
103 \\{gnus-undo-mode-map}"
104 :keymap gnus-undo-mode-map
105 (set (make-local-variable 'gnus-undo-actions) nil)
106 (set (make-local-variable 'gnus-undo-boundary) t)
107 (when gnus-undo-mode
108 ;; Set up the menu.
109 (when (gnus-visual-p 'undo-menu 'menu)
110 (gnus-undo-make-menu-bar))
111 (add-hook 'post-command-hook 'gnus-undo-boundary nil t)))
112
113 ;;; Interface functions.
114
115 (defun gnus-disable-undo (&optional buffer)
116 "Disable undoing in the current buffer."
117 (interactive)
118 (save-excursion
119 (when buffer
120 (set-buffer buffer))
121 (gnus-undo-mode -1)))
122
123 (defun gnus-undo-boundary ()
124 "Set Gnus undo boundary."
125 (if gnus-undo-boundary-inhibit
126 (setq gnus-undo-boundary-inhibit nil)
127 (setq gnus-undo-boundary t)))
128
129 (defun gnus-undo-force-boundary ()
130 "Set Gnus undo boundary."
131 (setq gnus-undo-boundary-inhibit nil
132 gnus-undo-boundary t))
133
134 (defun gnus-undo-register (form)
135 "Register FORMS as something to be performed to undo a change.
136 FORMS may use backtick quote syntax."
137 (when gnus-undo-mode
138 (gnus-undo-register-1
139 `(lambda ()
140 ,form))))
141
142 (put 'gnus-undo-register 'lisp-indent-function 0)
143 (put 'gnus-undo-register 'edebug-form-spec '(body))
144
145 (defun gnus-undo-register-1 (function)
146 "Register FUNCTION as something to be performed to undo a change."
147 (when gnus-undo-mode
148 (cond
149 ;; We are on a boundary, so we create a new action.
150 (gnus-undo-boundary
151 (push (list function) gnus-undo-actions)
152 (setq gnus-undo-boundary nil))
153 ;; Prepend the function to an old action.
154 (gnus-undo-actions
155 (setcar gnus-undo-actions (cons function (car gnus-undo-actions))))
156 ;; Initialize list.
157 (t
158 (setq gnus-undo-actions (list (list function)))))
159 ;; Limit the length of the undo list.
160 (let ((next (nthcdr gnus-undo-limit gnus-undo-actions)))
161 (when next
162 (setcdr next nil)))
163 ;; We are not at a boundary...
164 (setq gnus-undo-boundary-inhibit t)))
165
166 (defun gnus-undo (n)
167 "Undo some previous changes in Gnus buffers.
168 Repeat this command to undo more changes.
169 A numeric argument serves as a repeat count."
170 (interactive "p")
171 (unless gnus-undo-mode
172 (error "Undoing is not enabled in this buffer"))
173 (message "%s" last-command)
174 (when (or (not (eq last-command 'gnus-undo))
175 (not gnus-undo-last))
176 (setq gnus-undo-last gnus-undo-actions))
177 (let ((action (pop gnus-undo-last)))
178 (unless action
179 (error "Nothing further to undo"))
180 (setq gnus-undo-actions (delq action gnus-undo-actions))
181 (setq gnus-undo-boundary t)
182 (mapc 'funcall action)))
183
184 (provide 'gnus-undo)
185
186 ;;; gnus-undo.el ends here