gnus-undo.el 5.9 KB

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