winner.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. ;;; winner.el --- Restore old window configurations
  2. ;; Copyright (C) 1997-1998, 2001-2015 Free Software Foundation, Inc.
  3. ;; Author: Ivar Rummelhoff <ivarru@math.uio.no>
  4. ;; Created: 27 Feb 1997
  5. ;; Keywords: convenience frames
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Winner mode is a global minor mode that records the changes in the
  19. ;; window configuration (i.e. how the frames are partitioned into
  20. ;; windows) so that the changes can be "undone" using the command
  21. ;; `winner-undo'. By default this one is bound to the key sequence
  22. ;; ctrl-c left. If you change your mind (while undoing), you can
  23. ;; press ctrl-c right (calling `winner-redo'). Even though it uses
  24. ;; some features of Emacs20.3, winner.el should also work with
  25. ;; Emacs19.34 and XEmacs20, provided that the installed version of
  26. ;; custom is not obsolete.
  27. ;; Winner mode was improved August 1998.
  28. ;; Further improvements February 2002.
  29. ;;; Code:
  30. (eval-when-compile (require 'cl-lib))
  31. (defun winner-active-region ()
  32. (declare (gv-setter (lambda (store)
  33. (if (featurep 'xemacs)
  34. `(if ,store (zmacs-activate-region)
  35. (zmacs-deactivate-region))
  36. `(if ,store (activate-mark) (deactivate-mark))))))
  37. (region-active-p))
  38. (defalias 'winner-edges
  39. (if (featurep 'xemacs) 'window-pixel-edges 'window-edges))
  40. (defalias 'winner-window-list
  41. (if (featurep 'xemacs)
  42. (lambda () (delq (minibuffer-window) (window-list nil 0)))
  43. (lambda () (window-list nil 0))))
  44. (require 'ring)
  45. (defgroup winner nil
  46. "Restoring window configurations."
  47. :group 'windows)
  48. (defcustom winner-dont-bind-my-keys nil
  49. "Non-nil means do not bind keys in Winner mode."
  50. :type 'boolean
  51. :group 'winner)
  52. (defcustom winner-ring-size 200
  53. "Maximum number of stored window configurations per frame."
  54. :type 'integer
  55. :group 'winner)
  56. (defcustom winner-boring-buffers '("*Completions*")
  57. "List of buffer names whose windows `winner-undo' will not restore.
  58. You may want to include buffer names such as *Help*, *Apropos*,
  59. *Buffer List*, *info* and *Compile-Log*."
  60. :type '(repeat string)
  61. :group 'winner)
  62. ;;;; Saving old configurations (internal variables and subroutines)
  63. ;;; Current configuration
  64. ;; List the windows according to their edges.
  65. (defun winner-sorted-window-list ()
  66. (sort (winner-window-list)
  67. (lambda (x y)
  68. (cl-loop for a in (winner-edges x)
  69. for b in (winner-edges y)
  70. while (= a b)
  71. finally return (< a b)))))
  72. (defun winner-win-data ()
  73. ;; Essential properties of the windows in the selected frame.
  74. (cl-loop for win in (winner-sorted-window-list)
  75. collect (cons (winner-edges win) (window-buffer win))))
  76. ;; This variable is updated with the current window configuration
  77. ;; every time it changes.
  78. (defvar winner-currents nil)
  79. ;; The current configuration (+ the buffers involved).
  80. (defsubst winner-conf ()
  81. (cons (current-window-configuration)
  82. (winner-win-data)))
  83. ;; Save current configuration.
  84. ;; (Called below by `winner-save-old-configurations').
  85. (defun winner-remember ()
  86. (setf (alist-get (selected-frame) winner-currents) (winner-conf)))
  87. ;; Consult `winner-currents'.
  88. (defun winner-configuration (&optional frame)
  89. (or (cdr (assq (or frame (selected-frame)) winner-currents))
  90. (with-selected-frame frame
  91. (winner-conf))))
  92. ;;; Saved configurations
  93. ;; This variable contains the window configuration rings.
  94. ;; The key in this alist is the frame.
  95. (defvar winner-ring-alist nil)
  96. ;; Find the right ring. If it does not exist, create one.
  97. (defsubst winner-ring (frame)
  98. (or (cdr (assq frame winner-ring-alist))
  99. (let ((ring (make-ring winner-ring-size)))
  100. (ring-insert ring (winner-configuration frame))
  101. (push (cons frame ring) winner-ring-alist)
  102. ring)))
  103. ;; If the same command is called several times in a row,
  104. ;; we only save one window configuration.
  105. (defvar winner-last-command nil)
  106. ;; Frames affected by the previous command.
  107. (defvar winner-last-frames nil)
  108. (defsubst winner-equal (a b)
  109. "Check whether two Winner configurations (as produced by
  110. `winner-conf') are equal."
  111. (equal (cdr a) (cdr b)))
  112. ;; Save the current window configuration, if it has changed.
  113. ;; If so return frame, otherwise return nil.
  114. (defun winner-insert-if-new (frame)
  115. (unless (or (memq frame winner-last-frames)
  116. (eq this-command 'winner-redo))
  117. (let ((conf (winner-configuration frame))
  118. (ring (winner-ring frame)))
  119. (when (and (not (ring-empty-p ring))
  120. (winner-equal conf (ring-ref ring 0)))
  121. ;; When the previous configuration was very similar,
  122. ;; keep only the latest.
  123. (ring-remove ring 0))
  124. (ring-insert ring conf)
  125. (push frame winner-last-frames)
  126. frame)))
  127. ;;; Hooks
  128. ;; Frames affected by the current command.
  129. (defvar winner-modified-list nil)
  130. ;; Called whenever the window configuration changes
  131. ;; (a `window-configuration-change-hook').
  132. (defun winner-change-fun ()
  133. ;; Cull dead frames.
  134. (setq winner-modified-list
  135. (cl-loop for frame in winner-modified-list
  136. if (frame-live-p frame) collect frame))
  137. (unless (or (memq (selected-frame) winner-modified-list)
  138. (/= 0 (minibuffer-depth)))
  139. (push (selected-frame) winner-modified-list)))
  140. ;; A `post-command-hook' for emacsen with
  141. ;; `window-configuration-change-hook'.
  142. (defun winner-save-old-configurations ()
  143. (when (zerop (minibuffer-depth))
  144. (unless (eq this-command winner-last-command)
  145. (setq winner-last-frames nil)
  146. (setq winner-last-command this-command))
  147. (dolist (frame winner-modified-list)
  148. (winner-insert-if-new frame))
  149. (setq winner-modified-list nil)
  150. (winner-remember)))
  151. ;; A `minibuffer-setup-hook'.
  152. (defun winner-save-unconditionally ()
  153. (unless (eq this-command winner-last-command)
  154. (setq winner-last-frames nil)
  155. (setq winner-last-command this-command))
  156. (winner-insert-if-new (selected-frame))
  157. (winner-remember))
  158. ;; A `post-command-hook' for other emacsen.
  159. ;; Also called by `winner-undo' before "undoing".
  160. (defun winner-save-conditionally ()
  161. (when (zerop (minibuffer-depth))
  162. (winner-save-unconditionally)))
  163. ;;;; Restoring configurations
  164. ;; Works almost as `set-window-configuration',
  165. ;; but does not change the contents or the size of the minibuffer,
  166. ;; and tries to preserve the selected window.
  167. (defun winner-set-conf (winconf)
  168. (let* ((miniwin (minibuffer-window))
  169. (chosen (selected-window))
  170. (minisize (window-height miniwin)))
  171. (cl-letf (((window-buffer miniwin))
  172. ((window-point miniwin)))
  173. (set-window-configuration winconf))
  174. (cond
  175. ((window-live-p chosen) (select-window chosen))
  176. ((window-minibuffer-p) (other-window 1)))
  177. (when (/= minisize (window-height miniwin))
  178. (with-selected-window miniwin
  179. (setf (window-height) minisize)))))
  180. (defvar winner-point-alist nil)
  181. ;; `set-window-configuration' restores old points and marks. This is
  182. ;; not what we want, so we make a list of the "real" (i.e. new) points
  183. ;; and marks before undoing window configurations.
  184. ;;
  185. ;; Format of entries: (buffer (mark . mark-active) (window . point) ..)
  186. (defun winner-make-point-alist ()
  187. (save-current-buffer
  188. (cl-loop with alist
  189. for win in (winner-window-list)
  190. for entry =
  191. (or (assq (window-buffer win) alist)
  192. (car (push (list (set-buffer (window-buffer win))
  193. (cons (mark t) (winner-active-region)))
  194. alist)))
  195. do (push (cons win (window-point win))
  196. (cddr entry))
  197. finally return alist)))
  198. (defun winner-get-point (buf win)
  199. ;; Consult (and possibly extend) `winner-point-alist'.
  200. ;; Returns nil if buf no longer exists.
  201. (when (buffer-name buf)
  202. (let ((entry (assq buf winner-point-alist)))
  203. (cond
  204. (entry
  205. (or (cdr (assq win (cddr entry)))
  206. (cdr (assq nil (cddr entry)))
  207. (with-current-buffer buf
  208. (push (cons nil (point)) (cddr entry))
  209. (point))))
  210. (t (with-current-buffer buf
  211. (push (list buf
  212. (cons (mark t) (winner-active-region))
  213. (cons nil (point)))
  214. winner-point-alist)
  215. (point)))))))
  216. ;; Make sure point does not end up in the minibuffer and delete
  217. ;; windows displaying dead or boring buffers
  218. ;; (c.f. `winner-boring-buffers'). Return nil if all the windows
  219. ;; should be deleted. Preserve correct points and marks.
  220. (defun winner-set (conf)
  221. ;; For the format of `conf', see `winner-conf'.
  222. (let* ((buffers nil)
  223. (alive
  224. ;; Possibly update `winner-point-alist'
  225. (cl-loop for buf in (mapcar 'cdr (cdr conf))
  226. for pos = (winner-get-point buf nil)
  227. if (and pos (not (memq buf buffers)))
  228. do (push buf buffers)
  229. collect pos)))
  230. (winner-set-conf (car conf))
  231. (let (xwins) ; to be deleted
  232. ;; Restore points
  233. (dolist (win (winner-sorted-window-list))
  234. (unless (and (pop alive)
  235. (setf (window-point win)
  236. (winner-get-point (window-buffer win) win))
  237. (not (member (buffer-name (window-buffer win))
  238. winner-boring-buffers)))
  239. (push win xwins))) ; delete this window
  240. ;; Restore marks
  241. (save-current-buffer
  242. (cl-loop for buf in buffers
  243. for entry = (cadr (assq buf winner-point-alist))
  244. do (progn (set-buffer buf)
  245. (set-mark (car entry))
  246. (setf (winner-active-region) (cdr entry)))))
  247. ;; Delete windows, whose buffers are dead or boring.
  248. ;; Return t if this is still a possible configuration.
  249. (or (null xwins)
  250. (progn
  251. (mapc 'delete-window (cdr xwins)) ; delete all but one
  252. (unless (one-window-p t)
  253. (delete-window (car xwins))
  254. t))))))
  255. ;;;; Winner mode (a minor mode)
  256. (defcustom winner-mode-hook nil
  257. "Functions to run whenever Winner mode is turned on or off."
  258. :type 'hook
  259. :group 'winner)
  260. (define-obsolete-variable-alias 'winner-mode-leave-hook
  261. 'winner-mode-off-hook "24.3")
  262. (defcustom winner-mode-off-hook nil
  263. "Functions to run whenever Winner mode is turned off."
  264. :type 'hook
  265. :group 'winner)
  266. (defvar winner-mode-map
  267. (let ((map (make-sparse-keymap)))
  268. (unless winner-dont-bind-my-keys
  269. (define-key map [(control c) left] 'winner-undo)
  270. (define-key map [(control c) right] 'winner-redo))
  271. map)
  272. "Keymap for Winner mode.")
  273. ;;;###autoload
  274. (define-minor-mode winner-mode nil :global t ; let d-m-m make the doc
  275. (if winner-mode
  276. (progn
  277. (add-hook 'window-configuration-change-hook 'winner-change-fun)
  278. (add-hook 'post-command-hook 'winner-save-old-configurations)
  279. (add-hook 'minibuffer-setup-hook 'winner-save-unconditionally)
  280. (setq winner-modified-list (frame-list))
  281. (winner-save-old-configurations))
  282. (remove-hook 'window-configuration-change-hook 'winner-change-fun)
  283. (remove-hook 'post-command-hook 'winner-save-old-configurations)
  284. (remove-hook 'minibuffer-setup-hook 'winner-save-unconditionally)))
  285. ;; Inspired by undo (simple.el)
  286. (defvar winner-undo-frame nil)
  287. (defvar winner-pending-undo-ring nil
  288. "The ring currently used by `winner-undo'.")
  289. (defvar winner-undo-counter nil)
  290. (defvar winner-undone-data nil) ; There confs have been passed.
  291. (defun winner-undo ()
  292. "Switch back to an earlier window configuration saved by Winner mode.
  293. In other words, \"undo\" changes in window configuration."
  294. (interactive)
  295. (cond
  296. ((not winner-mode) (error "Winner mode is turned off"))
  297. (t (unless (and (eq last-command 'winner-undo)
  298. (eq winner-undo-frame (selected-frame)))
  299. (winner-save-conditionally) ; current configuration->stack
  300. (setq winner-undo-frame (selected-frame))
  301. (setq winner-point-alist (winner-make-point-alist))
  302. (setq winner-pending-undo-ring (winner-ring (selected-frame)))
  303. (setq winner-undo-counter 0)
  304. (setq winner-undone-data (list (winner-win-data))))
  305. (cl-incf winner-undo-counter) ; starting at 1
  306. (when (and (winner-undo-this)
  307. (not (window-minibuffer-p)))
  308. (message "Winner undo (%d / %d)"
  309. winner-undo-counter
  310. (1- (ring-length winner-pending-undo-ring)))))))
  311. (defun winner-undo-this () ; The heart of winner undo.
  312. (cl-loop
  313. (cond
  314. ((>= winner-undo-counter (ring-length winner-pending-undo-ring))
  315. (message "No further window configuration undo information")
  316. (cl-return nil))
  317. ((and ; If possible configuration
  318. (winner-set (ring-ref winner-pending-undo-ring
  319. winner-undo-counter))
  320. ; .. and new configuration
  321. (let ((data (winner-win-data)))
  322. (and (not (member data winner-undone-data))
  323. (push data winner-undone-data))))
  324. (cl-return t)) ; .. then everything is fine.
  325. (t ;; Otherwise, discharge it (and try the next one).
  326. (ring-remove winner-pending-undo-ring winner-undo-counter)))))
  327. (defun winner-redo () ; If you change your mind.
  328. "Restore a more recent window configuration saved by Winner mode."
  329. (interactive)
  330. (cond
  331. ((eq last-command 'winner-undo)
  332. (winner-set
  333. (if (zerop (minibuffer-depth))
  334. (ring-remove winner-pending-undo-ring 0)
  335. (ring-ref winner-pending-undo-ring 0)))
  336. (unless (eq (selected-window) (minibuffer-window))
  337. (message "Winner undid undo")))
  338. (t (user-error "Previous command was not a `winner-undo'"))))
  339. (provide 'winner)
  340. ;;; winner.el ends here