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