Saturday, April 05, 2014

Specify a dedicated window for other-window in Emacs

There is a way to specify a window to be used by (other-window).  Also, after a buffer poped up on a unexpected window, you can slide it to the right place.

(winner-mode 1)
(defvar window-locked-p nil)

(defun lockon-window ()
  "Set a window for other-window.  To set the target window,
    move point to the window, then call this.  Technically this sets
    all window dedicated but the target window.  Call again to free
    windows."
  (interactive)
  (walk-windows
   (lambda (win)
     (set-window-dedicated-p win (not window-locked-p))))
  (if window-locked-p
      (message "Windows are free")
    (set-window-dedicated-p (selected-window) nil)
    (message "Window is locked on"))
  (setq window-locked-p (not window-locked-p)))

(defun freeze-window ()
  "This freezes or locks a window where point is.  Call again to unfreeze.
    Technically this toggles window-dedicate-p property of a window."
  (interactive)
  (set-window-dedicated-p
   (selected-window)
   (not (window-dedicated-p (selected-window))))
  (if (window-dedicated-p (selected-window))
      (message (format "%s is freezed" (selected-window)))
    (message (format "%s is free" (selected-window)))))

(defun slide-window ()
  "This slides a buffer in a window where point is to
    next-window.  Technically this recalls previous windows-set by
    winnder-undo then shows the buffer to next-window"
  (interactive)
  (let ((buf (buffer-name)))
    (winner-undo)
    (set-window-buffer (next-window) buf)
    ;; (switch-to-buffer buf)
    (message "Window is slided")
    (other-window 1)))

No comments: