winner.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. ;;; winner.el --- Restore old window configurations
  2. ;; Copyright (C) 1997-1998, 2001-2012 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
  31. (require 'cl))
  32. (defmacro winner-active-region ()
  33. (if (boundp 'mark-active)
  34. 'mark-active
  35. '(region-active-p)))
  36. (defsetf winner-active-region () (store)
  37. (if (featurep 'xemacs)
  38. `(if ,store (zmacs-activate-region)
  39. (zmacs-deactivate-region))
  40. `(setq mark-active ,store)))
  41. (defalias 'winner-edges
  42. (if (featurep 'xemacs) 'window-pixel-edges 'window-edges))
  43. (defalias 'winner-window-list
  44. (if (featurep 'xemacs)
  45. (lambda () (delq (minibuffer-window) (window-list nil 0)))
  46. (lambda () (window-list nil 0))))
  47. (require 'ring)
  48. (defgroup winner nil
  49. "Restoring window configurations."
  50. :group 'windows)
  51. ;;;###autoload
  52. (defcustom winner-mode nil
  53. "Toggle Winner mode.
  54. Setting this variable directly does not take effect;
  55. use either \\[customize] or the function `winner-mode'."
  56. :set #'(lambda (symbol value) (funcall symbol (or value 0)))
  57. :initialize 'custom-initialize-default
  58. :type 'boolean
  59. :group 'winner
  60. :require 'winner)
  61. (defcustom winner-dont-bind-my-keys nil
  62. "If non-nil: Do not use `winner-mode-map' in Winner mode."
  63. :type 'boolean
  64. :group 'winner)
  65. (defcustom winner-ring-size 200
  66. "Maximum number of stored window configurations per frame."
  67. :type 'integer
  68. :group 'winner)
  69. (defcustom winner-boring-buffers '("*Completions*")
  70. "`winner-undo' will not restore windows displaying any of these buffers.
  71. You may want to include buffer names such as *Help*, *Apropos*,
  72. *Buffer List*, *info* and *Compile-Log*."
  73. :type '(repeat string)
  74. :group 'winner)
  75. ;;;; Saving old configurations (internal variables and subroutines)
  76. ;;; Current configuration
  77. ;; List the windows according to their edges.
  78. (defun winner-sorted-window-list ()
  79. (sort (winner-window-list)
  80. (lambda (x y)
  81. (loop for a in (winner-edges x)
  82. for b in (winner-edges y)
  83. while (= a b)
  84. finally return (< a b)))))
  85. (defun winner-win-data ()
  86. ;; Essential properties of the windows in the selected frame.
  87. (loop for win in (winner-sorted-window-list)
  88. collect (cons (winner-edges win) (window-buffer win))))
  89. ;; This variable is updated with the current window configuration
  90. ;; every time it changes.
  91. (defvar winner-currents nil)
  92. ;; The current configuration (+ the buffers involved).
  93. (defsubst winner-conf ()
  94. (cons (current-window-configuration)
  95. (winner-win-data)))
  96. ;; Save current configuration.
  97. ;; (Called below by `winner-save-old-configurations').
  98. (defun winner-remember ()
  99. (let ((entry (assq (selected-frame) winner-currents)))
  100. (if entry (setcdr entry (winner-conf))
  101. (push (cons (selected-frame) (winner-conf))
  102. winner-currents))))
  103. ;; Consult `winner-currents'.
  104. (defun winner-configuration (&optional frame)
  105. (or (cdr (assq (or frame (selected-frame)) winner-currents))
  106. (letf (((selected-frame) frame))
  107. (winner-conf))))
  108. ;;; Saved configurations
  109. ;; This variable contains the window configuration rings.
  110. ;; The key in this alist is the frame.
  111. (defvar winner-ring-alist nil)
  112. ;; Find the right ring. If it does not exist, create one.
  113. (defsubst winner-ring (frame)
  114. (or (cdr (assq frame winner-ring-alist))
  115. (let ((ring (make-ring winner-ring-size)))
  116. (ring-insert ring (winner-configuration frame))
  117. (push (cons frame ring) winner-ring-alist)
  118. ring)))
  119. ;; If the same command is called several times in a row,
  120. ;; we only save one window configuration.
  121. (defvar winner-last-command nil)
  122. ;; Frames affected by the previous command.
  123. (defvar winner-last-frames nil)
  124. (defsubst winner-equal (a b)
  125. "Check whether two Winner configurations (as produced by
  126. `winner-conf') are equal."
  127. (equal (cdr a) (cdr b)))
  128. ;; Save the current window configuration, if it has changed.
  129. ;; If so return frame, otherwise return nil.
  130. (defun winner-insert-if-new (frame)
  131. (unless (or (memq frame winner-last-frames)
  132. (eq this-command 'winner-redo))
  133. (let ((conf (winner-configuration frame))
  134. (ring (winner-ring frame)))
  135. (when (and (not (ring-empty-p ring))
  136. (winner-equal conf (ring-ref ring 0)))
  137. ;; When the previous configuration was very similar,
  138. ;; keep only the latest.
  139. (ring-remove ring 0))
  140. (ring-insert ring conf)
  141. (push frame winner-last-frames)
  142. frame)))
  143. ;;; Hooks
  144. ;; Frames affected by the current command.
  145. (defvar winner-modified-list nil)
  146. ;; Called whenever the window configuration changes
  147. ;; (a `window-configuration-change-hook').
  148. (defun winner-change-fun ()
  149. (unless (or (memq (selected-frame) winner-modified-list)
  150. (/= 0 (minibuffer-depth)))
  151. (push (selected-frame) winner-modified-list)))
  152. ;; A `post-command-hook' for emacsen with
  153. ;; `window-configuration-change-hook'.
  154. (defun winner-save-old-configurations ()
  155. (when (zerop (minibuffer-depth))
  156. (unless (eq this-command winner-last-command)
  157. (setq winner-last-frames nil)
  158. (setq winner-last-command this-command))
  159. (dolist (frame winner-modified-list)
  160. (winner-insert-if-new frame))
  161. (setq winner-modified-list nil)
  162. (winner-remember)))
  163. ;; A `minibuffer-setup-hook'.
  164. (defun winner-save-unconditionally ()
  165. (unless (eq this-command winner-last-command)
  166. (setq winner-last-frames nil)
  167. (setq winner-last-command this-command))
  168. (winner-insert-if-new (selected-frame))
  169. (winner-remember))
  170. ;; A `post-command-hook' for other emacsen.
  171. ;; Also called by `winner-undo' before "undoing".
  172. (defun winner-save-conditionally ()
  173. (when (zerop (minibuffer-depth))
  174. (winner-save-unconditionally)))
  175. ;;;; Restoring configurations
  176. ;; Works almost as `set-window-configuration',
  177. ;; but does not change the contents or the size of the minibuffer,
  178. ;; and tries to preserve the selected window.
  179. (defun winner-set-conf (winconf)
  180. (let* ((miniwin (minibuffer-window))
  181. (chosen (selected-window))
  182. (minisize (window-height miniwin)))
  183. (letf (((window-buffer miniwin))
  184. ((window-point miniwin)))
  185. (set-window-configuration winconf))
  186. (cond
  187. ((window-live-p chosen) (select-window chosen))
  188. ((window-minibuffer-p (selected-window))
  189. (other-window 1)))
  190. (when (/= minisize (window-height miniwin))
  191. (letf (((selected-window) miniwin) )
  192. (setf (window-height) minisize)))))
  193. (defvar winner-point-alist nil)
  194. ;; `set-window-configuration' restores old points and marks. This is
  195. ;; not what we want, so we make a list of the "real" (i.e. new) points
  196. ;; and marks before undoing window configurations.
  197. ;;
  198. ;; Format of entries: (buffer (mark . mark-active) (window . point) ..)
  199. (defun winner-make-point-alist ()
  200. (letf (((current-buffer)))
  201. (loop with alist
  202. for win in (winner-window-list)
  203. for entry =
  204. (or (assq (window-buffer win) alist)
  205. (car (push (list (set-buffer (window-buffer win))
  206. (cons (mark t) (winner-active-region)))
  207. alist)))
  208. do (push (cons win (window-point win))
  209. (cddr entry))
  210. finally return alist)))
  211. (defun winner-get-point (buf win)
  212. ;; Consult (and possibly extend) `winner-point-alist'.
  213. ;; Returns nil if buf no longer exists.
  214. (when (buffer-name buf)
  215. (let ((entry (assq buf winner-point-alist)))
  216. (cond
  217. (entry
  218. (or (cdr (assq win (cddr entry)))
  219. (cdr (assq nil (cddr entry)))
  220. (letf (((current-buffer) buf))
  221. (push (cons nil (point)) (cddr entry))
  222. (point))))
  223. (t (letf (((current-buffer) buf))
  224. (push (list buf
  225. (cons (mark t) (winner-active-region))
  226. (cons nil (point)))
  227. winner-point-alist)
  228. (point)))))))
  229. ;; Make sure point does not end up in the minibuffer and delete
  230. ;; windows displaying dead or boring buffers
  231. ;; (c.f. `winner-boring-buffers'). Return nil if all the windows
  232. ;; should be deleted. Preserve correct points and marks.
  233. (defun winner-set (conf)
  234. ;; For the format of `conf', see `winner-conf'.
  235. (let* ((buffers nil)
  236. (alive
  237. ;; Possibly update `winner-point-alist'
  238. (loop for buf in (mapcar 'cdr (cdr conf))
  239. for pos = (winner-get-point buf nil)
  240. if (and pos (not (memq buf buffers)))
  241. do (push buf buffers)
  242. collect pos)))
  243. (winner-set-conf (car conf))
  244. (let (xwins) ; to be deleted
  245. ;; Restore points
  246. (dolist (win (winner-sorted-window-list))
  247. (unless (and (pop alive)
  248. (setf (window-point win)
  249. (winner-get-point (window-buffer win) win))
  250. (not (member (buffer-name (window-buffer win))
  251. winner-boring-buffers)))
  252. (push win xwins))) ; delete this window
  253. ;; Restore marks
  254. (letf (((current-buffer)))
  255. (loop for buf in buffers
  256. for entry = (cadr (assq buf winner-point-alist))
  257. do (progn (set-buffer buf)
  258. (set-mark (car entry))
  259. (setf (winner-active-region) (cdr entry)))))
  260. ;; Delete windows, whose buffers are dead or boring.
  261. ;; Return t if this is still a possible configuration.
  262. (or (null xwins)
  263. (progn
  264. (mapc 'delete-window (cdr xwins)) ; delete all but one
  265. (unless (one-window-p t)
  266. (delete-window (car xwins))
  267. t))))))
  268. ;;;; Winner mode (a minor mode)
  269. (defcustom winner-mode-hook nil
  270. "Functions to run whenever Winner mode is turned on."
  271. :type 'hook
  272. :group 'winner)
  273. (defcustom winner-mode-leave-hook nil
  274. "Functions to run whenever Winner mode is turned off."
  275. :type 'hook
  276. :group 'winner)
  277. (defvar winner-mode-map
  278. (let ((map (make-sparse-keymap)))
  279. (define-key map [(control c) left] 'winner-undo)
  280. (define-key map [(control c) right] 'winner-redo)
  281. map)
  282. "Keymap for Winner mode.")
  283. ;; Check if `window-configuration-change-hook' is working.
  284. (defun winner-hook-installed-p ()
  285. (save-window-excursion
  286. (let ((winner-var nil)
  287. (window-configuration-change-hook
  288. '((lambda () (setq winner-var t)))))
  289. (split-window)
  290. winner-var)))
  291. ;;;###autoload
  292. (defun winner-mode (&optional arg)
  293. "Toggle Winner mode.
  294. With arg, turn Winner mode on if and only if arg is positive."
  295. (interactive "P")
  296. (let ((on-p (if arg (> (prefix-numeric-value arg) 0)
  297. (not winner-mode))))
  298. (cond
  299. ;; Turn mode on
  300. (on-p
  301. (setq winner-mode t)
  302. (cond
  303. ((winner-hook-installed-p)
  304. (add-hook 'window-configuration-change-hook 'winner-change-fun)
  305. (add-hook 'post-command-hook 'winner-save-old-configurations))
  306. (t (add-hook 'post-command-hook 'winner-save-conditionally)))
  307. (add-hook 'minibuffer-setup-hook 'winner-save-unconditionally)
  308. (setq winner-modified-list (frame-list))
  309. (winner-save-old-configurations)
  310. (run-hooks 'winner-mode-hook)
  311. (when (called-interactively-p 'interactive)
  312. (message "Winner mode enabled")))
  313. ;; Turn mode off
  314. (winner-mode
  315. (setq winner-mode nil)
  316. (remove-hook 'window-configuration-change-hook 'winner-change-fun)
  317. (remove-hook 'post-command-hook 'winner-save-old-configurations)
  318. (remove-hook 'post-command-hook 'winner-save-conditionally)
  319. (remove-hook 'minibuffer-setup-hook 'winner-save-unconditionally)
  320. (run-hooks 'winner-mode-leave-hook)
  321. (when (called-interactively-p 'interactive)
  322. (message "Winner mode disabled"))))))
  323. ;; Inspired by undo (simple.el)
  324. (defvar winner-undo-frame nil)
  325. (defvar winner-pending-undo-ring nil
  326. "The ring currently used by `winner-undo'.")
  327. (defvar winner-undo-counter nil)
  328. (defvar winner-undone-data nil) ; There confs have been passed.
  329. (defun winner-undo ()
  330. "Switch back to an earlier window configuration saved by Winner mode.
  331. In other words, \"undo\" changes in window configuration."
  332. (interactive)
  333. (cond
  334. ((not winner-mode) (error "Winner mode is turned off"))
  335. (t (unless (and (eq last-command 'winner-undo)
  336. (eq winner-undo-frame (selected-frame)))
  337. (winner-save-conditionally) ; current configuration->stack
  338. (setq winner-undo-frame (selected-frame))
  339. (setq winner-point-alist (winner-make-point-alist))
  340. (setq winner-pending-undo-ring (winner-ring (selected-frame)))
  341. (setq winner-undo-counter 0)
  342. (setq winner-undone-data (list (winner-win-data))))
  343. (incf winner-undo-counter) ; starting at 1
  344. (when (and (winner-undo-this)
  345. (not (window-minibuffer-p (selected-window))))
  346. (message "Winner undo (%d / %d)"
  347. winner-undo-counter
  348. (1- (ring-length winner-pending-undo-ring)))))))
  349. (defun winner-undo-this () ; The heart of winner undo.
  350. (loop
  351. (cond
  352. ((>= winner-undo-counter (ring-length winner-pending-undo-ring))
  353. (message "No further window configuration undo information")
  354. (return nil))
  355. ((and ; If possible configuration
  356. (winner-set (ring-ref winner-pending-undo-ring
  357. winner-undo-counter))
  358. ; .. and new configuration
  359. (let ((data (winner-win-data)))
  360. (and (not (member data winner-undone-data))
  361. (push data winner-undone-data))))
  362. (return t)) ; .. then everything is fine.
  363. (t ;; Otherwise, discharge it (and try the next one).
  364. (ring-remove winner-pending-undo-ring winner-undo-counter)))))
  365. (defun winner-redo () ; If you change your mind.
  366. "Restore a more recent window configuration saved by Winner mode."
  367. (interactive)
  368. (cond
  369. ((eq last-command 'winner-undo)
  370. (winner-set
  371. (if (zerop (minibuffer-depth))
  372. (ring-remove winner-pending-undo-ring 0)
  373. (ring-ref winner-pending-undo-ring 0)))
  374. (unless (eq (selected-window) (minibuffer-window))
  375. (message "Winner undid undo")))
  376. (t (error "Previous command was not a `winner-undo'"))))
  377. ;;; To be evaluated when the package is loaded:
  378. (unless (or (assq 'winner-mode minor-mode-map-alist)
  379. winner-dont-bind-my-keys)
  380. (push (cons 'winner-mode winner-mode-map)
  381. minor-mode-map-alist))
  382. (provide 'winner)
  383. ;;; winner.el ends here