-;;; winner.el --- Restore window configuration or change buffer
+;;; winner.el --- Restore old window configurations
-;; Copyright (C) 1997 Free Software Foundation. Inc.
+;; Copyright (C) 1997, 1998 Free Software Foundation. Inc.
;; Author: Ivar Rummelhoff <ivarr@ifi.uio.no>
;; Maintainer: Ivar Rummelhoff <ivarr@ifi.uio.no>
;; Created: 27 Feb 1997
-;; Keywords: extensions,windows
+;; Time-stamp: <1998-03-05 19:01:37 ivarr>
+;; Keywords: windows
;; This file is part of GNU Emacs.
;; Boston, MA 02111-1307, USA.
;;; Commentary:
-;;
-;; winner.el provides a minor mode (`winner-mode') that does
-;; essentially two things:
-;;
-;; 1) It keeps track of changing window configurations, so that
-;; when you wish to go back to a previous view, all you have
-;; to do is to press C-left a couple of times.
-;;
-;; 2) It lets you switch to other buffers by pressing C-right.
-;;
-;; To use Winner mode, put this line in your .emacs file:
-;;
-;; (add-hook 'after-init-hook (lambda () (winner-mode 1)))
-\f
-;; Details:
-;;
-;; 1. You may of course decide to use other bindings than those
-;; mentioned above. Just set these variables in your .emacs:
-;;
-;; `winner-prev-event'
-;; `winner-next-event'
-;;
-;; 2. When you have found the view of your choice
-;; (using your favourite keys), you may press ctrl-space
-;; (`winner-max-event') to `delete-other-windows'.
-;;
-;; 3. Winner now keeps one configuration stack for each frame.
-;;
-;;
-;;
-;; Yours sincerely, Ivar Rummelhoff
-;;
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
-;;; Code:
-
-
-
-;;;; Variables you may want to change
-
-(defvar winner-prev-event 'C-left
- "Winner mode binds this event to the command `winner-previous'.")
-
-(defvar winner-next-event 'C-right
- "Winner mode binds this event to the command `winner-next'.")
-
-(defvar winner-max-event 67108896 ; CTRL-space
- "Event for deleting other windows
-after having selected a view with Winner.
-
-The normal functions of this event will also be performed.
-In the default case (CTRL-SPACE) the mark will be set.")
-
-(defvar winner-skip-buffers
- '("*Messages*",
- "*Compile-Log*",
- ".newsrc-dribble",
- "*Completions*",
- "*Buffer list*")
- "Exclude these buffer names
-from any \(Winner mode\) list of buffers.")
-
-(defvar winner-skip-regexps '("^ ")
- "Exclude buffers with names matching any of these regexps.
-..from any \(Winner mode\) list of buffers.
-
-By default `winner-skip-regexps' is set to \(\"^ \"\),
-which excludes \"invisible buffers\".")
-
-\f
-(defvar winner-limit 50
- "Winner will save no more than 2 * `winner-limit' window configurations.
-\(.. and no less than `winner-limit'.\)")
-
-(defvar winner-mode-hook nil
- "Functions to run whenever Winner mode is turned on.")
-
-(defvar winner-mode-leave-hook nil
- "Functions to run whenever Winner mode is turned off.")
-
-(defvar winner-dont-bind-my-keys nil
- "If non-nil: Do not use `winner-mode-map' in Winner mode.")
-
+;; Winner mode is a global minor mode that records the changes in the
+;; window configuration (i.e. how the frames are partitioned into
+;; windows). This way the changes can be "undone" using the function
+;; `winner-undo'. By default this one is bound to the key sequence
+;; ctrl-x left. If you change your mind (while undoing), you can
+;; press ctrl-x right (calling `winner-redo'). Even though it uses
+;; some features of Emacs20.3, winner.el should also work with
+;; Emacs19.34 and XEmacs20, provided that the installed version of
+;; custom is not obsolete.
-;;;; Winner mode
+\f;;; Code:
(eval-when-compile (require 'cl))
+(require 'ring)
+(when (fboundp 'defgroup)
+ (defgroup winner nil ; Customization by Dave Love
+ "Restoring window configurations."
+ :group 'windows))
-(defvar winner-mode nil) ; For the modeline.
-(defvar winner-mode-map nil "Keymap for Winner mode.")
-
-;;;###autoload
-(defun winner-mode (&optional arg)
- "Toggle Winner mode.
-With arg, turn Winner mode on if and only if arg is positive."
- (interactive "P")
- (let ((on-p (if arg (> (prefix-numeric-value arg) 0)
- (not winner-mode))))
- (cond
- (on-p (let ((winner-frames-changed (frame-list)))
- (winner-do-save)) ; Save current configurations
- (add-hook 'window-configuration-change-hook 'winner-save-configuration)
- (setq winner-mode t)
- (run-hooks 'winner-mode-hook))
- (t (remove-hook 'window-configuration-change-hook 'winner-save-configuration)
- (when winner-mode
- (setq winner-mode nil)
- (run-hooks 'winner-mode-leave-hook))))
- (force-mode-line-update)))
-
-
-;; List of frames which have changed
-(defvar winner-frames-changed nil)
-
-;; Time to save the window configuration.
-(defun winner-save-configuration ()
- (push (selected-frame) winner-frames-changed)
- (add-hook 'post-command-hook 'winner-do-save))
-
-\f
-(defun winner-do-save ()
- (let ((current (selected-frame)))
- (unwind-protect
- (do ((frames winner-frames-changed (cdr frames)))
- ((null frames))
- (unless (memq (car frames) (cdr frames))
- ;; Process each frame once.
- (select-frame (car frames))
- (winner-push (current-window-configuration) (car frames))))
- (setq winner-frames-changed nil)
- (select-frame current)
- (remove-hook 'post-command-hook 'winner-do-save))))
+(unless (fboundp 'defcustom)
+ (defmacro defcustom (symbol &optional initvalue docs &rest rest)
+ (list 'defvar symbol initvalue docs)))
+;;;###autoload
+(defcustom winner-mode nil
+ "Toggle winner-mode.
+You must modify via \\[customize] for this variable to have an effect."
+ :set #'(lambda (symbol value)
+ (winner-mode (or value 0)))
+ :initialize 'custom-initialize-default
+ :type 'boolean
+ :group 'winner
+ :require 'winner)
+(defcustom winner-dont-bind-my-keys nil
+ "If non-nil: Do not use `winner-mode-map' in Winner mode."
+ :type 'boolean
+ :group 'winner)
+(defcustom winner-ring-size 200
+ "Maximum number of stored window configurations per frame."
+ :type 'integer
+ :group 'winner)
-;;;; Configuration stacks (one for each frame)
-(defvar winner-stacks nil) ; ------ " ------
+\f;;;; Internal variables and subroutines
-;; A stack of window configurations with some additional information.
-(defstruct (winner-stack
- (:constructor winner-stack-new
- (config &aux
- (data (list config))
- (place data))))
- data place (count 1))
+;; This variable contains the window cofiguration rings.
+;; The key in this alist is the frame.
+(defvar winner-ring-alist nil)
-;; Return the stack of this frame
-(defun winner-stack (frame)
- (let ((stack (cdr (assq frame winner-stacks))))
- (if stack (winner-stack-data stack)
- ;; Else make new stack
- (letf (((selected-frame) frame))
- (let ((config (current-window-configuration)))
- (push (cons frame (winner-stack-new config))
- winner-stacks)
- (list config))))))
+;; Find the right ring. If it does not exist, create one.
+(defsubst winner-ring (frame)
+ (or (cdr (assq frame winner-ring-alist))
+ (progn
+ (let ((ring (make-ring winner-ring-size)))
+ (ring-insert ring (winner-configuration frame))
+ (push (cons frame ring) winner-ring-alist)
+ ring))))
+(defvar winner-last-saviour nil)
+;; Save the current window configuration, if it has changed and return
+;; frame, else return nil. If the last change was due to the same
+;; command, save only the latest configuration.
+(defun winner-insert-if-new (frame)
+ (let ((conf (winner-configuration))
+ (ring (winner-ring frame)))
+ (cond
+ ((winner-equal conf (ring-ref ring 0)) nil)
+ (t (when (and (eq this-command (car winner-last-saviour))
+ (memq frame (cdr winner-last-saviour)))
+ (ring-remove ring 0))
+ (ring-insert ring conf)
+ frame))))
+
+(defvar winner-modified-list nil) ; Which frames have changed?
+
+;; This function is called when the window configuration changes.
+(defun winner-change-fun ()
+ (unless (memq (selected-frame) winner-modified-list)
+ (push (selected-frame) winner-modified-list)))
+
+;; For Emacs20
+(defun winner-save-new-configurations ()
+ (setq winner-last-saviour
+ (cons this-command
+ (mapcar 'winner-insert-if-new winner-modified-list)))
+ (setq winner-modified-list nil))
+
+;; For compatibility with other emacsen.
+(defun winner-save-unconditionally ()
+ (setq winner-last-saviour
+ (cons this-command
+ (list (winner-insert-if-new (selected-frame))))))
+
+;; Arrgh. This is storing the same information twice.
+(defun winner-configuration (&optional frame)
+ (if frame (letf (((selected-frame) frame)) (winner-configuration))
+ (cons (current-window-configuration)
+ (loop for w being the windows
+ collect (window-buffer w)))))
\f
-;; Push this window configuration on the right stack,
-;; but make sure the stack doesn't get too large etc...
-(defun winner-push (config frame)
- (let ((this (cdr (assq frame winner-stacks))))
- (if (not this) (push (cons frame (winner-stack-new config))
- winner-stacks)
- (push config (winner-stack-data this))
- (when (> (incf (winner-stack-count this)) winner-limit)
- ;; No more than 2*winner-limit configs
- (setcdr (winner-stack-place this) nil)
- (setf (winner-stack-place this)
- (winner-stack-data this))
- (setf (winner-stack-count this) 1)))))
-
-
-
-
-
-
-
-
-;;;; Selecting a window configuration
-
-
-;; Return list of names of other buffers, excluding the current buffer
-;; and buffers specified by the user.
-(defun winner-other-buffers ()
- (loop for buf in (buffer-list)
- for name = (buffer-name buf)
- unless (or (eq (current-buffer) buf)
- (member name winner-skip-buffers)
- (loop for regexp in winner-skip-regexps
- if (string-match regexp name) return t
- finally return nil))
- collect name))
-
+;; The same as `set-window-configuration',
+;; but doesn't touch the minibuffer.
+(defun winner-set-conf (winconf)
+ (let ((min-sel (window-minibuffer-p (selected-window)))
+ (minibuf (window-buffer (minibuffer-window)))
+ (minipoint (letf ((selected-window) (minibuffer-window))
+ (point)))
+ win)
+ (set-window-configuration winconf)
+ (setq win (selected-window))
+ (select-window (minibuffer-window))
+ (set-window-buffer (minibuffer-window) minibuf)
+ (goto-char minipoint)
+ (cond
+ (min-sel)
+ ((window-minibuffer-p win)
+ (other-window 1))
+ (t (select-window win)))))
+
+(defun winner-win-data () ; Information about the windows
+ (loop for win being the windows
+ unless (window-minibuffer-p win)
+ collect (list (window-buffer win)
+ (window-width win)
+ (window-height win))))
+
+;; Make sure point doesn't end up in the minibuffer and
+;; delete windows displaying dead buffers. Return nil
+;; if and only if all the windows should have been deleted.
+(defun winner-set (conf)
+ (let ((origpoints
+ (save-excursion
+ (loop for buf in (cdr conf)
+ collect (if (buffer-name buf)
+ (progn (set-buffer buf) (point))
+ nil)))))
+ (winner-set-conf (car conf))
+ (let* ((win (selected-window))
+ (xwins (loop for window being the windows
+ for pos in origpoints
+ unless (window-minibuffer-p window)
+ if pos do (progn (select-window window)
+ (goto-char pos))
+ else collect window)))
+ (select-window win)
+ ;; Return t if possible configuration
+ (cond
+ ((null xwins) t)
+ ((progn (mapcar 'delete-window (cdr xwins))
+ (one-window-p t))
+ nil) ; No existing buffers
+ (t (delete-window (car xwins)))))))
+
+
+
+
+\f;;;; Winner mode (a minor mode)
+
+(defcustom winner-mode-hook nil
+ "Functions to run whenever Winner mode is turned on."
+ :type 'hook
+ :group 'winner)
+
+(defcustom winner-mode-leave-hook nil
+ "Functions to run whenever Winner mode is turned off."
+ :type 'hook
+ :group 'winner)
+(defvar winner-mode-map nil "Keymap for Winner mode.")
-(defun winner-select (&optional arg)
+;; Is `window-configuration-change-hook' working?
+(defun winner-hook-installed-p ()
+ (save-window-excursion
+ (let ((winner-var nil)
+ (window-configuration-change-hook
+ '((lambda () (setq winner-var t)))))
+ (split-window)
+ winner-var)))
- "Change to previous or new window configuration.
-With arg start at position 1 if arg is positive, and
-at -1 if arg is negative; else start at position 0.
-\(For Winner to record changes in window configurations,
-Winner mode must be turned on.\)"
+;;;###autoload
+(defun winner-mode (&optional arg)
+ "Toggle Winner mode.
+With arg, turn Winner mode on if and only if arg is positive."
(interactive "P")
+ (let ((on-p (if arg (> (prefix-numeric-value arg) 0)
+ (not winner-mode))))
+ (cond
+ ;; Turn mode on
+ (on-p
+ (setq winner-mode t)
+ (cond
+ ((winner-hook-installed-p)
+ (add-hook 'window-configuration-change-hook 'winner-change-fun)
+ (add-hook 'post-command-hook 'winner-save-new-configurations))
+ (t (add-hook 'post-command-hook 'winner-save-unconditionally)))
+ (setq winner-modified-list (frame-list))
+ (winner-save-new-configurations)
+ (run-hooks 'winner-mode-hook))
+ ;; Turn mode off
+ (winner-mode
+ (setq winner-mode nil)
+ (remove-hook 'window-configuration-change-hook 'winner-change-fun)
+ (remove-hook 'post-command-hook 'winner-save-new-configurations)
+ (remove-hook 'post-command-hook 'winner-save-unconditionally)
+ (run-hooks 'winner-mode-leave-hook)))
+ (force-mode-line-update)))
- (setq arg
- (cond
- ((not arg) nil)
- ((> (prefix-numeric-value arg) 0) winner-next-event)
- ((< (prefix-numeric-value arg) 0) winner-prev-event)
- (t nil)))
- (if arg (push arg unread-command-events))
-\f
- (let ((stack (winner-stack (selected-frame)))
- (store nil)
- (buffers (winner-other-buffers))
- (passed nil)
- (config (current-window-configuration))
- (pos 0) event)
- ;; `stack' and `store' are stacks of window configuration while
- ;; `buffers' and `passed' are stacks of buffer names.
-
- (condition-case nil
-
- (loop
- (setq event (read-event))
- (cond
-
- ((eq event winner-prev-event)
- (cond (passed (push (pop passed) buffers)(decf pos))
- ((cdr stack)(push (pop stack) store) (decf pos))
- (t (setq stack (append (nreverse store) stack))
- (setq store nil)
- (setq pos 0))))
-
- ((eq event winner-next-event)
- (cond (store (push (pop store) stack) (incf pos))
- (buffers (push (pop buffers) passed) (incf pos))
- (t (setq buffers (nreverse passed))
- (setq passed nil)
- (setq pos 0))))
-
- ((eq event winner-max-event)
- ;; Delete other windows and leave.
- (delete-other-windows)
- ;; Let this change be saved.
- (setq pos -1)
- ;; Perform other actions of this event.
- (push event unread-command-events)
- (return))
- (t (push event unread-command-events) (return)))
-
- (cond
- ;; Display
- (passed (set-window-buffer (selected-window) (car passed))
- (message (concat "Winner\(%d\): [%s] "
- (mapconcat 'identity buffers " "))
- pos (car passed)))
-
- (t (set-window-configuration (car stack))
- (if (window-minibuffer-p (selected-window))
- (other-window 1))
- (message "Winner\(%d\)" pos))))
-
- (quit (set-window-configuration config)
- (setq pos 0)))
- (if (zerop pos)
- ;; Do not record these changes.
- (remove-hook 'post-command-hook 'winner-do-save)
- ;; Else update the buffer list and make sure that the displayed
- ;; buffer is the same as the current buffer.
- (switch-to-buffer (window-buffer)))))
-\f
-
-
-
-
-(defun winner-previous ()
- "Change to previous window configuration."
- (interactive)
- (winner-select -1))
-
-(defun winner-next ()
- "Change to new window configuration."
+\f;; Inspired by undo (simple.el)
+(defun winner-undo (arg)
+ "Switch back to an earlier window configuration saved by Winner mode.
+In other words, \"undo\" changes in window configuration.
+With prefix arg, undo that many levels."
+ (interactive "p")
+ (cond
+ ((not winner-mode) (error "Winner mode is turned off"))
+ ;; ((eq (selected-window) (minibuffer-window))
+ ;; (error "No winner undo from minibuffer."))
+ (t (setq this-command t)
+ (unless (eq last-command 'winner-undo)
+ (setq winner-pending-undo-ring (winner-ring (selected-frame)))
+ (setq winner-undo-counter 0)
+ (setq winner-undone-data (list (winner-win-data))))
+ (incf winner-undo-counter arg)
+ (winner-undo-this)
+ (unless (window-minibuffer-p (selected-window))
+ (message "Winner undo (%d)" winner-undo-counter))
+ (setq this-command 'winner-undo))))
+
+(defvar winner-pending-undo-ring nil) ; The ring currently used by
+ ; undo.
+(defvar winner-undo-counter nil)
+(defvar winner-undone-data nil) ; There confs have been passed.
+
+(defun winner-undo-this () ; The heart of winner undo.
+ (if (>= winner-undo-counter (ring-length winner-pending-undo-ring))
+ (error "No further window configuration undo information")
+ (unless (and
+ ;; Possible configuration
+ (winner-set
+ (ring-ref winner-pending-undo-ring
+ winner-undo-counter))
+ ;; New configuration
+ (let ((data (winner-win-data)))
+ (if (member data winner-undone-data) nil
+ (push data winner-undone-data))))
+ (ring-remove winner-pending-undo-ring winner-undo-counter)
+ (winner-undo-this))))
+
+(defun winner-redo () ; If you change your mind.
+ "Restore a more recent window configuration saved by Winner mode."
(interactive)
- (winner-select 1))
-
-
+ (cond
+ ((eq last-command 'winner-undo)
+ (ring-remove winner-pending-undo-ring 0)
+ (winner-set
+ (ring-remove winner-pending-undo-ring 0))
+ (or (eq (selected-window) (minibuffer-window))
+ (message "Winner undid undo")))
+ (t (error "Previous command was not a winner-undo"))))
+\f;;;; To be evaluated when the package is loaded:
-;;;; To be evaluated when the package is loaded:
+(if (fboundp 'compare-window-configurations)
+ (defalias 'winner-equal 'compare-window-configurations)
+ (defalias 'winner-equal 'equal))
(unless winner-mode-map
(setq winner-mode-map (make-sparse-keymap))
- (define-key winner-mode-map (vector winner-prev-event) 'winner-previous)
- (define-key winner-mode-map (vector winner-next-event) 'winner-next))
+ (define-key winner-mode-map [(control x) left] 'winner-undo)
+ (define-key winner-mode-map [(control x) right] 'winner-redo))
(unless (or (assq 'winner-mode minor-mode-map-alist)
winner-dont-bind-my-keys)