Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / gnus / gnus-undo.el
blobd487262c9314bf83918056d08cfa0a7ffa70d24b
1 ;;; gnus-undo.el --- minor mode for undoing in Gnus
3 ;; Copyright (C) 1996-2018 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
8 ;; This file is part of GNU Emacs.
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.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;;; Commentary:
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.
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.
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.)
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.
44 ;;; Code:
46 (require 'gnus-util)
47 (require 'gnus)
49 (defgroup gnus-undo nil
50 "Undoing in Gnus buffers."
51 :group 'gnus)
53 (defcustom gnus-undo-limit 2000
54 "The number of undoable actions recorded."
55 :type 'integer
56 :group 'gnus-undo)
58 (defcustom gnus-undo-mode nil
59 ;; FIXME: This is a buffer-local minor mode which requires running
60 ;; code upon activation/deactivation, so defining it as a defcustom
61 ;; doesn't seem very useful: setting it to non-nil via Customize
62 ;; probably won't do the right thing.
63 "Minor mode for undoing in Gnus buffers."
64 :type 'boolean
65 :group 'gnus-undo)
67 (defcustom gnus-undo-mode-hook nil
68 "Hook called in all `gnus-undo-mode' buffers."
69 :type 'hook
70 :group 'gnus-undo)
72 ;;; Internal variables.
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)
79 ;;; Minor mode definition.
81 (defvar gnus-undo-mode-map
82 (let ((map (make-sparse-keymap)))
83 (gnus-define-keys map
84 "\M-\C-_" gnus-undo
85 "\C-_" gnus-undo
86 "\C-xu" gnus-undo
87 ;; many people are used to type `C-/' on X terminals and get `C-_'.
88 [(control /)] gnus-undo)
89 map))
91 (defun gnus-undo-make-menu-bar ()
92 ;; This is disabled for the time being.
93 (when nil
94 (define-key-after (current-local-map) [menu-bar file gnus-undo]
95 (cons "Undo" 'gnus-undo-actions)
96 [menu-bar file whatever])))
98 (define-minor-mode gnus-undo-mode
99 "Minor mode for providing `undo' in Gnus buffers.
101 \\{gnus-undo-mode-map}"
102 :keymap gnus-undo-mode-map
103 (set (make-local-variable 'gnus-undo-actions) nil)
104 (set (make-local-variable 'gnus-undo-boundary) t)
105 (when gnus-undo-mode
106 ;; Set up the menu.
107 (when (gnus-visual-p 'undo-menu 'menu)
108 (gnus-undo-make-menu-bar))
109 (add-hook 'post-command-hook 'gnus-undo-boundary nil t)))
111 ;;; Interface functions.
113 (defun gnus-disable-undo (&optional buffer)
114 "Disable undoing in the current buffer."
115 (interactive)
116 (save-excursion
117 (when buffer
118 (set-buffer buffer))
119 (gnus-undo-mode -1)))
121 (defun gnus-undo-boundary ()
122 "Set Gnus undo boundary."
123 (if gnus-undo-boundary-inhibit
124 (setq gnus-undo-boundary-inhibit nil)
125 (setq gnus-undo-boundary t)))
127 (defun gnus-undo-force-boundary ()
128 "Set Gnus undo boundary."
129 (setq gnus-undo-boundary-inhibit nil
130 gnus-undo-boundary t))
132 (defun gnus-undo-register (form)
133 "Register FORMS as something to be performed to undo a change.
134 FORMS may use backtick quote syntax."
135 (when gnus-undo-mode
136 (gnus-undo-register-1
137 `(lambda ()
138 ,form))))
140 (put 'gnus-undo-register 'lisp-indent-function 0)
141 (put 'gnus-undo-register 'edebug-form-spec '(body))
143 (defun gnus-undo-register-1 (function)
144 "Register FUNCTION as something to be performed to undo a change."
145 (when gnus-undo-mode
146 (cond
147 ;; We are on a boundary, so we create a new action.
148 (gnus-undo-boundary
149 (push (list function) gnus-undo-actions)
150 (setq gnus-undo-boundary nil))
151 ;; Prepend the function to an old action.
152 (gnus-undo-actions
153 (setcar gnus-undo-actions (cons function (car gnus-undo-actions))))
154 ;; Initialize list.
156 (setq gnus-undo-actions (list (list function)))))
157 ;; Limit the length of the undo list.
158 (let ((next (nthcdr gnus-undo-limit gnus-undo-actions)))
159 (when next
160 (setcdr next nil)))
161 ;; We are not at a boundary...
162 (setq gnus-undo-boundary-inhibit t)))
164 (defun gnus-undo (n)
165 "Undo some previous changes in Gnus buffers.
166 Repeat this command to undo more changes.
167 A numeric argument serves as a repeat count."
168 (interactive "p")
169 (unless gnus-undo-mode
170 (error "Undoing is not enabled in this buffer"))
171 (message "%s" last-command)
172 (when (or (not (eq last-command 'gnus-undo))
173 (not gnus-undo-last))
174 (setq gnus-undo-last gnus-undo-actions))
175 (let ((action (pop gnus-undo-last)))
176 (unless action
177 (error "Nothing further to undo"))
178 (setq gnus-undo-actions (delq action gnus-undo-actions))
179 (setq gnus-undo-boundary t)
180 (mapc 'funcall action)))
182 (provide 'gnus-undo)
184 ;;; gnus-undo.el ends here