windmove.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. ;;; windmove.el --- directional window-selection routines
  2. ;;
  3. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Hovav Shacham (hovav@cs.stanford.edu)
  6. ;; Created: 17 October 1998
  7. ;; Keywords: window, movement, convenience
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;
  22. ;; --------------------------------------------------------------------
  23. ;;; Commentary:
  24. ;;
  25. ;; This package defines a set of routines, windmove-{left,up,right,
  26. ;; down}, for selection of windows in a frame geometrically. For
  27. ;; example, `windmove-right' selects the window immediately to the
  28. ;; right of the currently-selected one. This functionality is similar
  29. ;; to the window-selection controls of the BRIEF editor of yore.
  30. ;;
  31. ;; One subtle point is what happens when the window to the right has
  32. ;; been split vertically; for example, consider a call to
  33. ;; `windmove-right' in this setup:
  34. ;;
  35. ;; -------------
  36. ;; | | A |
  37. ;; | | |
  38. ;; | |-----
  39. ;; | * | | (* is point in the currently
  40. ;; | | B | selected window)
  41. ;; | | |
  42. ;; -------------
  43. ;;
  44. ;; There are (at least) three reasonable things to do:
  45. ;; (1) Always move to the window to the right of the top edge of the
  46. ;; selected window; in this case, this policy selects A.
  47. ;; (2) Always move to the window to the right of the bottom edge of
  48. ;; the selected window; in this case, this policy selects B.
  49. ;; (3) Move to the window to the right of point in the selected
  50. ;; window. This may select either A or B, depending on the
  51. ;; position of point; in the illustrated example, it would select
  52. ;; B.
  53. ;;
  54. ;; Similar issues arise for all the movement functions. Windmove
  55. ;; resolves this problem by allowing the user to specify behavior
  56. ;; through a prefix argument. The cases are thus:
  57. ;; * if no argument is given to the movement functions, or the
  58. ;; argument given is zero, movement is relative to point;
  59. ;; * if a positive argument is given, movement is relative to the top
  60. ;; or left edge of the selected window, depending on whether the
  61. ;; movement is to be horizontal or vertical;
  62. ;; * if a negative argument is given, movement is relative to the
  63. ;; bottom or right edge of the selected window, depending on whether
  64. ;; the movement is to be horizontal or vertical.
  65. ;;
  66. ;;
  67. ;; Another feature enables wrap-around mode when the variable
  68. ;; `windmove-wrap-around' is set to a non-nil value. In this mode,
  69. ;; movement that falls off the edge of the frame will wrap around to
  70. ;; find the window on the opposite side of the frame. Windmove does
  71. ;; the Right Thing about the minibuffer; for example, consider:
  72. ;;
  73. ;; -------------
  74. ;; | * |
  75. ;; |-----------|
  76. ;; | A |
  77. ;; |-----------| (* is point in the currently
  78. ;; | B | C | selected window)
  79. ;; | | |
  80. ;; -------------
  81. ;;
  82. ;; With wraparound enabled, windmove-down will move to A, while
  83. ;; windmove-up will move to the minibuffer if it is active, or to
  84. ;; either B or C depending on the prefix argument.
  85. ;;
  86. ;;
  87. ;; A set of default keybindings is supplied: shift-{left,up,right,down}
  88. ;; invoke the corresponding Windmove function. See the installation
  89. ;; section if you wish to use these keybindings.
  90. ;; Installation:
  91. ;;
  92. ;; Put the following line in your `.emacs' file:
  93. ;;
  94. ;; (windmove-default-keybindings) ; shifted arrow keys
  95. ;;
  96. ;; or
  97. ;;
  98. ;; (windmove-default-keybindings 'hyper) ; etc.
  99. ;;
  100. ;; to use another modifier key.
  101. ;;
  102. ;;
  103. ;; If you wish to enable wrap-around, also add a line like:
  104. ;;
  105. ;; (setq windmove-wrap-around t)
  106. ;;
  107. ;;
  108. ;; Note: If you have an Emacs that manifests a bug that sometimes
  109. ;; causes the occasional creation of a "lost column" between windows,
  110. ;; so that two adjacent windows do not actually touch, you may want to
  111. ;; increase the value of `windmove-window-distance-delta' to 2 or 3:
  112. ;;
  113. ;; (setq windmove-window-distance-delta 2)
  114. ;;
  115. ;; Acknowledgements:
  116. ;;
  117. ;; Special thanks to Julian Assange (proff@iq.org), whose
  118. ;; change-windows-intuitively.el predates Windmove, and provided the
  119. ;; inspiration for it. Kin Cho (kin@symmetrycomm.com) was the first
  120. ;; to suggest wrap-around behavior. Thanks also to Gerd Moellmann
  121. ;; (gerd@gnu.org) for his comments and suggestions.
  122. ;;; Code:
  123. ;; User configurable variables:
  124. ;; For customize ...
  125. (defgroup windmove nil
  126. "Directional selection of windows in a frame."
  127. :prefix "windmove-"
  128. :version "21.1"
  129. :group 'windows
  130. :group 'convenience)
  131. (defcustom windmove-wrap-around nil
  132. "Whether movement off the edge of the frame wraps around.
  133. If this variable is set to t, moving left from the leftmost window in
  134. a frame will find the rightmost one, and similarly for the other
  135. directions. The minibuffer is skipped over in up/down movements if it
  136. is inactive."
  137. :type 'boolean
  138. :group 'windmove)
  139. ;; If your Emacs sometimes places an empty column between two adjacent
  140. ;; windows, you may wish to set this delta to 2.
  141. (defcustom windmove-window-distance-delta 1
  142. "How far away from the current window to look for an adjacent window.
  143. Measured in characters either horizontally or vertically; setting this
  144. to a value larger than 1 may be useful in getting around window-
  145. placement bugs in old versions of Emacs."
  146. :type 'number
  147. :group 'windmove)
  148. ;; Implementation overview:
  149. ;;
  150. ;; The conceptual framework behind this code is all fairly simple. We
  151. ;; are on one window; we wish to move to another. The correct window
  152. ;; to move to is determined by the position of point in the current
  153. ;; window as well as the overall window setup.
  154. ;;
  155. ;; Early on, I made the decision to base my implementation around the
  156. ;; built-in function `window-at'. This function takes a frame-based
  157. ;; coordinate, and returns the window that contains it. Using this
  158. ;; function, the job of the various top-level windmove functions can
  159. ;; be decomposed: first, find the current frame-based location of
  160. ;; point; second, manipulate it in some way to give a new location,
  161. ;; that hopefully falls in the window immediately at left (or right,
  162. ;; etc.); third, use `window-at' and `select-window' to select the
  163. ;; window at that new location.
  164. ;;
  165. ;; This is probably not the only possible architecture, and it turns
  166. ;; out to have some inherent cruftiness. (Well, okay, the third step
  167. ;; is pretty clean....) We will consider each step in turn.
  168. ;;
  169. ;; A quick digression about coordinate frames: most of the functions
  170. ;; in the windmove package deal with screen coordinates in one way or
  171. ;; another. These coordinates are always relative to some reference
  172. ;; points. Window-based coordinates have their reference point in the
  173. ;; upper-left-hand corner of whatever window is being talked about;
  174. ;; frame-based coordinates have their reference point in the
  175. ;; upper-left-hand corner of the entire frame (of which the current
  176. ;; window is a component).
  177. ;;
  178. ;; All coordinates are zero-based, which simply means that the
  179. ;; reference point (whatever it is) is assigned the value (x=0, y=0).
  180. ;; X-coordinates grow down the screen, and Y-coordinates grow towards
  181. ;; the right of the screen.
  182. ;;
  183. ;; Okay, back to work. The first step is to gather information about
  184. ;; the frame-based coordinates of point, or rather, the reference
  185. ;; location. The reference location can be point, or the upper-left,
  186. ;; or the lower-right corner of the window; the particular one used is
  187. ;; controlled by the prefix argument to `windmove-left' and all the
  188. ;; rest.
  189. ;;
  190. ;; This work is done by `windmove-reference-loc'. It can figure out
  191. ;; the locations of the corners by calling `window-edges' combined
  192. ;; with the result of `posn-at-point'.
  193. ;;
  194. ;; The second step is more messy. Conceptually, it is fairly simple:
  195. ;; if we know the reference location, and the coordinates of the
  196. ;; current window, we can "throw" our reference point just over the
  197. ;; appropriate edge of the window, and see what other window is
  198. ;; there. More explicitly, consider this example from the user
  199. ;; documentation above.
  200. ;;
  201. ;; -------------
  202. ;; | | A |
  203. ;; | | |
  204. ;; | |-----
  205. ;; | * | | (* is point in the currently
  206. ;; | | B | selected window)
  207. ;; | | |
  208. ;; -------------
  209. ;;
  210. ;; The asterisk marks the reference point; we wish to move right.
  211. ;; Since we are moving horizontally, the Y coordinate of the new
  212. ;; location will be the same. The X coordinate can be such that it is
  213. ;; just past the edge of the present window. Obviously, the new point
  214. ;; will be inside window B. This in itself is fairly simple: using
  215. ;; the result of `windmove-reference-loc' and `window-edges', all the
  216. ;; necessary math can be performed. (Having said that, there is a
  217. ;; good deal of room for off-by-one errors, and Emacs 19.34, at least,
  218. ;; sometimes manifests a bug where two windows don't actually touch,
  219. ;; so a larger skip is required.) The actual math here is done by
  220. ;; `windmove-other-window-loc'.
  221. ;;
  222. ;; But we can't just pass the result of `windmove-other-window-loc' to
  223. ;; `window-at' directly. Why not? Suppose a move would take us off
  224. ;; the edge of the screen, say to the left. We want to give a
  225. ;; descriptive error message to the user. Or, suppose that a move
  226. ;; would place us in the minibuffer. What if the minibuffer is
  227. ;; inactive?
  228. ;;
  229. ;; Actually, the whole subject of the minibuffer edge of the frame is
  230. ;; rather messy. It turns out that with a sufficiently large delta,
  231. ;; we can fly off the bottom edge of the frame and miss the minibuffer
  232. ;; altogether. This, I think, is never right: if there's a minibuffer
  233. ;; and you're not in it, and you move down, the minibuffer should be
  234. ;; in your way.
  235. ;;
  236. ;; (By the way, I'm not totally sure that the code does the right
  237. ;; thing in really weird cases, like a frame with no minibuffer.)
  238. ;;
  239. ;; So, what we need is some ways to do constraining and such. The
  240. ;; early versions of windmove took a fairly simplistic approach to all
  241. ;; this. When I added the wrap-around option, those internals had to
  242. ;; be rewritten. After a *lot* of futzing around, I came up with a
  243. ;; two-step process that I think is general enough to cover the
  244. ;; relevant cases. (I'm not totally happy with having to pass the
  245. ;; window variable as deep as I do, but we can't have everything.)
  246. ;;
  247. ;; In the first phase, we make sure that the new location is sane.
  248. ;; "Sane" means that we can only fall of the edge of the frame in the
  249. ;; direction we're moving in, and that we don't miss the minibuffer if
  250. ;; we're moving down and not already in the minibuffer. The function
  251. ;; `windmove-constrain-loc-for-movement' takes care of all this.
  252. ;;
  253. ;; Then, we handle the wraparound, if it's enabled. The function
  254. ;; `windmove-wrap-loc-for-movement' takes coordinate values (both X
  255. ;; and Y) that fall off the edge of the frame, and replaces them with
  256. ;; values on the other side of the frame. It also has special
  257. ;; minibuffer-handling code again, because we want to wrap through the
  258. ;; minibuffer if it's not enabled.
  259. ;;
  260. ;; So, that's it. Seems to work. All of this work is done by the fun
  261. ;; function `windmove-find-other-window'.
  262. ;;
  263. ;; So, now we have a window to move to (or nil if something's gone
  264. ;; wrong). The function `windmove-do-window-select' is the main
  265. ;; driver function: it actually does the `select-window'. It is
  266. ;; called by four little convenience wrappers, `windmove-left',
  267. ;; `windmove-up', `windmove-right', and `windmove-down', which make
  268. ;; for convenient keybinding.
  269. ;; Quick & dirty utility function to add two (x . y) coords.
  270. (defun windmove-coord-add (coord1 coord2)
  271. "Add the two coordinates.
  272. Both COORD1 and COORD2 are coordinate cons pairs, (HPOS . VPOS). The
  273. result is another coordinate cons pair."
  274. (cons (+ (car coord1) (car coord2))
  275. (+ (cdr coord1) (cdr coord2))))
  276. (defun windmove-constrain-to-range (n min-n max-n)
  277. "Ensure that N is between MIN-N and MAX-N inclusive by constraining.
  278. If N is less than MIN-N, return MIN-N; if greater than MAX-N, return
  279. MAX-N."
  280. (max min-n (min n max-n)))
  281. (defun windmove-constrain-around-range (n min-n max-n)
  282. "Ensure that N is between MIN-N and MAX-N inclusive by wrapping.
  283. If N is less than MIN-N, return MAX-N; if greater than MAX-N, return
  284. MIN-N."
  285. (cond
  286. ((< n min-n) max-n)
  287. ((> n max-n) min-n)
  288. (t n)))
  289. (defun windmove-frame-edges (window)
  290. "Return (X-MIN Y-MIN X-MAX Y-MAX) for the frame containing WINDOW.
  291. If WINDOW is nil, return the edges for the selected frame.
  292. \(X-MIN, Y-MIN) is the zero-based coordinate of the top-left corner
  293. of the frame; (X-MAX, Y-MAX) is the zero-based coordinate of the
  294. bottom-right corner of the frame.
  295. For example, if a frame has 76 rows and 181 columns, the return value
  296. from `windmove-frame-edges' will be the list (0 0 180 75)."
  297. (let* ((frame (if window
  298. (window-frame window)
  299. (selected-frame)))
  300. (top-left (window-edges (frame-first-window frame)))
  301. (x-min (nth 0 top-left))
  302. (y-min (nth 1 top-left))
  303. (x-max (1- (frame-width frame))) ; 1- for last row & col
  304. (y-max (1- (frame-height frame))))
  305. (list x-min y-min x-max y-max)))
  306. ;; it turns out that constraining is always a good thing, even when
  307. ;; wrapping is going to happen. this is because:
  308. ;; first, since we disallow exotic diagonal-around-a-corner type
  309. ;; movements, so we can always fix the unimportant direction (the one
  310. ;; we're not moving in).
  311. ;; second, if we're moving down and we're not in the minibuffer, then
  312. ;; constraining the y coordinate to max-y is okay, because if that
  313. ;; falls in the minibuffer and the minibuffer isn't active, that y
  314. ;; coordinate will still be off the bottom of the frame as the
  315. ;; wrapping function sees it and so will get wrapped around anyway.
  316. (defun windmove-constrain-loc-for-movement (coord window dir)
  317. "Constrain COORD so that it is reasonable for the given movement.
  318. This involves two things: first, make sure that the \"off\" coordinate
  319. -- the one not being moved on, e.g., y for horizontal movement -- is
  320. within frame boundaries; second, if the movement is down and we're not
  321. moving from the minibuffer, make sure that the y coordinate does not
  322. exceed the frame max-y, so that we don't overshoot the minibuffer
  323. accidentally. WINDOW is the window that movement is relative to; DIR
  324. is the direction of the movement, one of `left', `up', `right',
  325. or `down'.
  326. Returns the constrained coordinate."
  327. (let ((frame-edges (windmove-frame-edges window))
  328. (in-minibuffer (window-minibuffer-p window)))
  329. (let ((min-x (nth 0 frame-edges))
  330. (min-y (nth 1 frame-edges))
  331. (max-x (nth 2 frame-edges))
  332. (max-y (nth 3 frame-edges)))
  333. (let ((new-x
  334. (if (memq dir '(up down)) ; vertical movement
  335. (windmove-constrain-to-range (car coord) min-x max-x)
  336. (car coord)))
  337. (new-y
  338. (if (or (memq dir '(left right)) ; horizontal movement
  339. (and (eq dir 'down)
  340. (not in-minibuffer))) ; don't miss minibuffer
  341. ;; (technically, we shouldn't constrain on min-y in the
  342. ;; second case, but this shouldn't do any harm on a
  343. ;; down movement.)
  344. (windmove-constrain-to-range (cdr coord) min-y max-y)
  345. (cdr coord))))
  346. (cons new-x new-y)))))
  347. ;; having constrained in the limited sense of windmove-constrain-loc-
  348. ;; for-movement, the wrapping code is actually much simpler than it
  349. ;; otherwise would be. the only complication is that we need to check
  350. ;; if the minibuffer is active, and, if not, pretend that it's not
  351. ;; even part of the frame.
  352. (defun windmove-wrap-loc-for-movement (coord window)
  353. "Takes the constrained COORD and wraps it around for the movement.
  354. This makes an out-of-range x or y coordinate and wraps it around the
  355. frame, giving a coordinate (hopefully) in the window on the other edge
  356. of the frame. WINDOW is the window that movement is relative to (nil
  357. means the currently selected window). Returns the wrapped coordinate."
  358. (let* ((frame-edges (windmove-frame-edges window))
  359. (frame-minibuffer (minibuffer-window (if window
  360. (window-frame window)
  361. (selected-frame))))
  362. (minibuffer-active (minibuffer-window-active-p
  363. frame-minibuffer)))
  364. (let ((min-x (nth 0 frame-edges))
  365. (min-y (nth 1 frame-edges))
  366. (max-x (nth 2 frame-edges))
  367. (max-y (if (not minibuffer-active)
  368. (- (nth 3 frame-edges)
  369. (window-height frame-minibuffer))
  370. (nth 3 frame-edges))))
  371. (cons
  372. (windmove-constrain-around-range (car coord) min-x max-x)
  373. (windmove-constrain-around-range (cdr coord) min-y max-y)))))
  374. ;; This calculates the reference location in the current window: the
  375. ;; frame-based (x . y) of either point, the top-left, or the
  376. ;; bottom-right of the window, depending on ARG.
  377. (defun windmove-reference-loc (&optional arg window)
  378. "Return the reference location for directional window selection.
  379. Return a coordinate (HPOS . VPOS) that is frame-based. If ARG is nil
  380. or not supplied, the reference point is the buffer's point in the
  381. currently-selected window, or WINDOW if supplied; otherwise, it is the
  382. top-left or bottom-right corner of the selected window, or WINDOW if
  383. supplied, if ARG is greater or smaller than zero, respectively."
  384. (let ((effective-arg (if (null arg) 0 (prefix-numeric-value arg)))
  385. (edges (window-inside-edges window)))
  386. (let ((top-left (cons (nth 0 edges)
  387. (nth 1 edges)))
  388. ;; Subtracting 1 converts the edge to the last column or line
  389. ;; within the window.
  390. (bottom-right (cons (- (nth 2 edges) 1)
  391. (- (nth 3 edges) 1))))
  392. (cond
  393. ((> effective-arg 0)
  394. top-left)
  395. ((< effective-arg 0)
  396. bottom-right)
  397. ((= effective-arg 0)
  398. (windmove-coord-add
  399. top-left
  400. ;; Don't care whether window is horizontally scrolled -
  401. ;; `posn-at-point' handles that already. See also:
  402. ;; http://lists.gnu.org/archive/html/emacs-devel/2012-01/msg00638.html
  403. (posn-col-row
  404. (posn-at-point (window-point window) window))))))))
  405. ;; This uses the reference location in the current window (calculated
  406. ;; by `windmove-reference-loc' above) to find a reference location
  407. ;; that will hopefully be in the window we want to move to.
  408. (defun windmove-other-window-loc (dir &optional arg window)
  409. "Return a location in the window to be moved to.
  410. Return value is a frame-based (HPOS . VPOS) value that should be moved
  411. to. DIR is one of `left', `up', `right', or `down'; an optional ARG
  412. is handled as by `windmove-reference-loc'; WINDOW is the window that
  413. movement is relative to."
  414. (let ((edges (window-edges window)) ; edges: (x0, y0, x1, y1)
  415. (refpoint (windmove-reference-loc arg window))) ; (x . y)
  416. (cond
  417. ((eq dir 'left)
  418. (cons (- (nth 0 edges)
  419. windmove-window-distance-delta)
  420. (cdr refpoint))) ; (x0-d, y)
  421. ((eq dir 'up)
  422. (cons (car refpoint)
  423. (- (nth 1 edges)
  424. windmove-window-distance-delta))) ; (x, y0-d)
  425. ((eq dir 'right)
  426. (cons (+ (1- (nth 2 edges)) ; -1 to get actual max x
  427. windmove-window-distance-delta)
  428. (cdr refpoint))) ; (x1+d-1, y)
  429. ((eq dir 'down) ; -1 to get actual max y
  430. (cons (car refpoint)
  431. (+ (1- (nth 3 edges))
  432. windmove-window-distance-delta))) ; (x, y1+d-1)
  433. (t (error "Invalid direction of movement: %s" dir)))))
  434. (defun windmove-find-other-window (dir &optional arg window)
  435. "Return the window object in direction DIR.
  436. DIR, ARG, and WINDOW are handled as by `windmove-other-window-loc'."
  437. (let* ((actual-current-window (or window (selected-window)))
  438. (raw-other-window-loc
  439. (windmove-other-window-loc dir arg actual-current-window))
  440. (constrained-other-window-loc
  441. (windmove-constrain-loc-for-movement raw-other-window-loc
  442. actual-current-window
  443. dir))
  444. (other-window-loc
  445. (if windmove-wrap-around
  446. (windmove-wrap-loc-for-movement constrained-other-window-loc
  447. actual-current-window)
  448. constrained-other-window-loc)))
  449. (window-at (car other-window-loc)
  450. (cdr other-window-loc))))
  451. ;; Selects the window that's hopefully at the location returned by
  452. ;; `windmove-other-window-loc', or screams if there's no window there.
  453. (defun windmove-do-window-select (dir &optional arg window)
  454. "Move to the window at direction DIR.
  455. DIR, ARG, and WINDOW are handled as by `windmove-other-window-loc'.
  456. If no window is at direction DIR, an error is signaled."
  457. (let ((other-window (windmove-find-other-window dir arg window)))
  458. (cond ((null other-window)
  459. (error "No window %s from selected window" dir))
  460. ((and (window-minibuffer-p other-window)
  461. (not (minibuffer-window-active-p other-window)))
  462. (error "Minibuffer is inactive"))
  463. (t
  464. (select-window other-window)))))
  465. ;;; end-user functions
  466. ;; these are all simple interactive wrappers to `windmove-do-
  467. ;; window-select', meant to be bound to keys.
  468. ;;;###autoload
  469. (defun windmove-left (&optional arg)
  470. "Select the window to the left of the current one.
  471. With no prefix argument, or with prefix argument equal to zero,
  472. \"left\" is relative to the position of point in the window; otherwise
  473. it is relative to the top edge (for positive ARG) or the bottom edge
  474. \(for negative ARG) of the current window.
  475. If no window is at the desired location, an error is signaled."
  476. (interactive "P")
  477. (windmove-do-window-select 'left arg))
  478. ;;;###autoload
  479. (defun windmove-up (&optional arg)
  480. "Select the window above the current one.
  481. With no prefix argument, or with prefix argument equal to zero, \"up\"
  482. is relative to the position of point in the window; otherwise it is
  483. relative to the left edge (for positive ARG) or the right edge (for
  484. negative ARG) of the current window.
  485. If no window is at the desired location, an error is signaled."
  486. (interactive "P")
  487. (windmove-do-window-select 'up arg))
  488. ;;;###autoload
  489. (defun windmove-right (&optional arg)
  490. "Select the window to the right of the current one.
  491. With no prefix argument, or with prefix argument equal to zero,
  492. \"right\" is relative to the position of point in the window;
  493. otherwise it is relative to the top edge (for positive ARG) or the
  494. bottom edge (for negative ARG) of the current window.
  495. If no window is at the desired location, an error is signaled."
  496. (interactive "P")
  497. (windmove-do-window-select 'right arg))
  498. ;;;###autoload
  499. (defun windmove-down (&optional arg)
  500. "Select the window below the current one.
  501. With no prefix argument, or with prefix argument equal to zero,
  502. \"down\" is relative to the position of point in the window; otherwise
  503. it is relative to the left edge (for positive ARG) or the right edge
  504. \(for negative ARG) of the current window.
  505. If no window is at the desired location, an error is signaled."
  506. (interactive "P")
  507. (windmove-do-window-select 'down arg))
  508. ;;; set up keybindings
  509. ;; Idea for this function is from iswitchb.el, by Stephen Eglen
  510. ;; (stephen@cns.ed.ac.uk).
  511. ;; I don't think these bindings will work on non-X terminals; you
  512. ;; probably want to use different bindings in that case.
  513. ;;;###autoload
  514. (defun windmove-default-keybindings (&optional modifier)
  515. "Set up keybindings for `windmove'.
  516. Keybindings are of the form MODIFIER-{left,right,up,down}.
  517. Default MODIFIER is 'shift."
  518. (interactive)
  519. (unless modifier (setq modifier 'shift))
  520. (global-set-key (vector (list modifier 'left)) 'windmove-left)
  521. (global-set-key (vector (list modifier 'right)) 'windmove-right)
  522. (global-set-key (vector (list modifier 'up)) 'windmove-up)
  523. (global-set-key (vector (list modifier 'down)) 'windmove-down))
  524. (provide 'windmove)
  525. ;;; windmove.el ends here