;; windmove.el -- directional window-selection routines. ;; ;; Copyright (C) 1998 Hovav Shacham ;; ;; Author: Hovav Shacham (hovav+usenet@leland.stanford.edu) ;; Created: 17 October 1998 ;; Version: 0.92 (beta), 9 November 1998 ;; ;; This file is *NOT* part of GNU Emacs. ;; ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; A copy of the GNU General Public License can be obtained from this ;; program's author (send e-mail to hovav+usenet@leland.stanford.edu) ;; or from the Free Software Foundation, Inc., 675 Mass Ave, ;; Cambridge, MA 02139, USA. ;; ;; Send bug reports to hovav+usenet@leland.stanford.edu ;; ;; -------------------------------------------------------------------- ;; Commentary: ;; ;; This package defines a set of routines, windmove-{left,up,right, ;; down}, for selection of windows in a frame geometrically. For ;; example, `windmove-right' selects the window immediately to the ;; right of the currently-selected one. This functionality is similar ;; to the window-selection controls of the BRIEF editor of yore. ;; ;; One subtle point is what happens when the window to the right has ;; been split vertically; for example, consider a call to ;; `windmove-right' in this setup: ;; ;; ------------- ;; | | A | ;; | | | ;; | |----- ;; | * | | (* is point in the currently ;; | | B | selected window) ;; | | | ;; ------------- ;; ;; There are (at least) three reasonable things to do: ;; (1) Always move to the window to the right of the top edge of the ;; selected window; in this case, this policy selects A. ;; (2) Always move to the window to the right of the bottom edge of ;; the selected window; in this case, this policy selects B. ;; (3) Move to the window to the right of point in the slected ;; window. This may select either A or B, depending on the ;; position of point; in the illustrated example, it would select ;; B. ;; ;; Similar issues arise for all the movement functions. Windmove ;; resolves this problem by allowing the user to specify behavior ;; through a prefix argument. The cases are thus: ;; * if no argument is given to the movement functions, or the ;; argument given is zero, movement is relative to point; ;; * if a positive argument is given, movement is relative to the top ;; or left edge of the selected window, depending on whether the ;; movement is to be horizontal or vertical; ;; * if a negative argument is given, movement is relative to the ;; bottom or right edge of the selected window, depending on whether ;; the movement is to be horizontal or vertical. ;; ;; A set of default keybindings is supplied: shift-{left,up,right,down} ;; invoke the corresponding Windmove function. See the installation ;; section if you wish to use these keybindings. ;; Installation: ;; ;; Put the following lines in your `.emacs' file: ;; ;; (require 'windmove) ; to load the package ;; (windmove-default-keybindings) ; default keybindings ;; ;; If you have an Emacs that manifests a bug that sometimes causes the ;; occasional creation of a "lost column" between windows, so that two ;; adjacent windows do not actually touch, you may want to increase ;; the value of `windmove-window-distance-delta' to 2 (or, under ;; certain conditions, even 3): ;; ;; (setq windmove-window-distance-delta 2) ;; ;; Acknowledgements: ;; ;; Special thanks to Julian Assange (proff@iq.org), whose ;; implementation of much the same functionality -- in ;; change-windows-intuitively.el -- I consulted in writing Windmove. ;; -------------------------------------------------------------------- ;;; Implementation: ;;; User configurable variables. ;; If your Emacs sometimes places an empty column between two adjacent ;; windows, you may wish to set this delta to 2. (defvar windmove-window-distance-delta 1 "How far away from the current window to look for an adjacent window. Measured in characters either horizontally or vertically; setting this to a value larger than 1 may be useful in getting around window- placement bugs in Emacs.") ;;; Code ;; Quick & dirty utility function to add two (x . y) coords. (defun windmove-coord-add (coord1 coord2) "Add the two coordinates." (cons (+ (car coord1) (car coord2)) (+ (cdr coord1) (cdr coord2)))) ;; Another helper -- make sure the other-window reference loc is a ;; valid location for the frame. (defun windmove-constrain-loc (coord) "Constrain COORD so that it is within frame boundaries." (let ((min-x 0) (min-y 0) (max-x (1- (frame-width))) (max-y (1- (frame-height)))) (cons (max min-x (min (car coord) max-x)) (max min-y (min (cdr coord) max-y))))) ;; `my-coordinates-of-position' is stolen and modified from the Emacs ;; Lisp Reference Manual, section 27.2.5. It seems to work okay, ;; although I am bothered by the fact that tab-offset (the cdr of the ;; next-to- last argument) is set to 0. On the other hand, I can't ;; find a single usage of `compute-motion' anywhere that doesn't set ;; this component to zero, and I'm too lazy to grovel through the C ;; source to figure out what's happening in the background. there ;; also seems to be a good deal of fun in calculating the correct ;; width of lines for telling `compute-motion' about; in particular, ;; it seems we need to subtract 1 (for the continuation column) from ;; the number that `window-width' gives, or continuation lines aren't ;; counted correctly. I haven't seen anyone doing this before, ;; though. (defun windmove-coordinates-of-position (pos &optional window) "Calculate the coordinates of position POS in window WINDOW. Returns the coodinates in a cons pair: (hpos . vpos)" (let* ((wind (if (null window) (selected-window) window)) (usable-width (1- (window-width wind))) ; 1- for cont. column (usable-height (1- (window-height wind))) ; 1- for mode line (big-hairy-result (compute-motion (window-start) '(0 . 0) pos (cons usable-width usable-height) usable-width (cons (window-hscroll) 0) ; why zero? wind))) (cons (nth 1 big-hairy-result) ; hpos, not vpos as documented (nth 2 big-hairy-result)))) ; vpos, not hpos as documented ;; This calculates the reference location in the current window: the ;; frame-based (x . y) of either point, the top-left, or the ;; bottom-right of the window, depending on ARG. (defun windmove-reference-loc (&optional arg window) "Calculates the reference location for directional window selection. Retruns a coordinate (hpos . vpos) that is frame-based. If ARG is nil or not supplied, the reference point is the buffer's point; otherwise, it is the top-left or bottom-right corner of the selected window, or WINDOW if supplied, if ARG is greater or smaller than zero, respectively." (let ((effective-arg (if (null arg) 0 (prefix-numeric-value arg))) (edges (window-edges window))) (let ((top-left (cons (nth 0 edges) (nth 1 edges))) ;; if 1-'s are not there, windows actually extend too far. (bottom-right (cons (1- (nth 2 edges)) (1- (nth 3 edges))))) (cond ((> effective-arg 0) top-left) ((< effective-arg 0) bottom-right) ((= effective-arg 0) (windmove-coord-add top-left ;; v0.92 -- point => window-point here. (windmove-coordinates-of-position (window-point window) window))))))) ;; This uses the reference location in the current window (calculated ;; by `windmove-reference-loc' above) to find a reference location ;; that will hopefully be in the window we want to move to. (defun windmove-other-window-loc (dir &optional arg window) "Calculates a location in the window to be moved to. Return value is a frame-based (hpos . vpos) value that should be moved to. DIR is one of `left', `up', `right', or `down'; an optional ARG is handled as by `windmove-reference-loc'; WINDOW is the window that movement is relative to." (let ((edges (window-edges window)) ; edges: (x0, y0, x1, y1) (refpoint (windmove-reference-loc arg window))) ; (x . y) (cond ((eq dir 'left) (cons (- (nth 0 edges) windmove-window-distance-delta) (cdr refpoint))) ; (x0-d, y) ((eq dir 'up) (cons (car refpoint) (- (nth 1 edges) windmove-window-distance-delta))) ; (x, y0-d) ((eq dir 'right) (cons (+ (nth 2 edges) windmove-window-distance-delta) (cdr refpoint))) ; (x1+d, y) ((eq dir 'down) (cons (car refpoint) (+ (nth 3 edges) windmove-window-distance-delta))) ; (x, y1+d) (t (error "Invalid direction of movement: %s" dir))))) ;; Selects the window that's hopefully at the location returned by ;; `windmove-other-window-loc', or screams if there's no window there. (defun windmove-do-window-select (dir &optional arg window) "Moves to the window at direction DIR. DIR, ARG, and WINDOW are handled as by `windmove-other-window-loc'. If no window is at direction DIR, an error is signaled." (let* ((raw-other-window-loc (windmove-other-window-loc dir arg window)) (other-window-loc ;; The bottom is special, because there's always the ;; minibuffer we don't want to miss. So we make sure we ;; don't fall off the end there. (if (eq dir 'down) (windmove-constrain-loc raw-other-window-loc) raw-other-window-loc)) (other-window (window-at (car other-window-loc) (cdr other-window-loc)))) (if (null other-window) (error "No window at %s" dir) (if (and (window-minibuffer-p other-window) (not (minibuffer-window-active-p other-window))) (error "Can't move to inactive minibuffer") (select-window other-window))))) ;;; end-user functions ;; these are all simple interactive wrappers to `windmove-do- ;; window-select', meant to be bound to keys. (defun windmove-left (&optional arg) "Select the window to the left of the current one. With no prefix argument, or with prefix argument equal to zero, \"left\" is relative to the position of point in the window; otherwise it is relative to the top edge (for positive ARG) or the bottom edge (for negative ARG) of the current window. If no window is at the desired location, an error is signalled." (interactive "P") (windmove-do-window-select 'left arg)) (defun windmove-up (&optional arg) "Select the window above the current one. With no prefix argument, or with prefix argument equal to zero, \"up\" is relative to the position of point in the window; otherwise it is relative to the left edge (for positive ARG) or the right edge (for negative ARG) of the current window. If no window is at the desired location, an error is signalled." (interactive "P") (windmove-do-window-select 'up arg)) (defun windmove-right (&optional arg) "Select the window to the right of the current one. With no prefix argument, or with prefix argument equal to zero, \"right\" is relative to the position of point in the window; otherwise it is relative to the top edge (for positive ARG) or the bottom edge (for negative ARG) of the current window. If no window is at the desired location, an error is signalled." (interactive "P") (windmove-do-window-select 'right arg)) (defun windmove-down (&optional arg) "Select the window below the current one. With no prefix argument, or with prefix argument equal to zero, \"down\" is relative to the position of point in the window; otherwise it is relative to the left edge (for positive ARG) or the right edge (for negative ARG) of the current window. If no window is at the desired location, an error is signalled." (interactive "P") (windmove-do-window-select 'down arg)) ;;; set up keybindings ;; Idea for this function is from iswitchb.el, by Stephen Eglen ;; (stephen@cns.ed.ac.uk). ;; I don't think these bindings will work on non-X terminals; you ;; probably want to use different bindings in that case. (defun windmove-default-keybindings () "Set up default keybindings for `windmove'." (interactive) ;; v0.91: read-kbd-macro => xemacs-style key vector ;; (global-set-key (read-kbd-macro "S-") 'windmove-left) ;; (global-set-key (read-kbd-macro "S-") 'windmove-up) ;; (global-set-key (read-kbd-macro "S-") 'windmove-right) ;; (global-set-key (read-kbd-macro "S-") 'windmove-down) (global-set-key [(shift left)] 'windmove-left) (global-set-key [(shift up)] 'windmove-up) (global-set-key [(shift right)] 'windmove-right) (global-set-key [(shift down)] 'windmove-down) ) ;;; finally, provide windmove. (provide 'windmove) ;; -------------------------------------------------------------------- ;;; end of windmove.el