(event-closest-point): New function.
[emacs.git] / lisp / vcursor.el
blob21b7c3abfabcd3001ad292378f1a2bcaae0c2210
1 ;;; vcursor.el --- manipulate an alternative ("virtual") cursor.
3 ;; Copyright (C) 1994, 1996 Peter Stephenson <pws@ifh.de>
5 ;; Author: Peter Stephenson <pws@ifh.de>
6 ;; Keywords: virtual cursor, display, copying
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
23 ;;; Commentary:
25 ;; Virtual cursor commands. I got this idea from the old BBC micro.
26 ;; You need Emacs 19 (I have not tried XEmacs) and a windowing
27 ;; system: I have tried X Windows and Oemacs but any system which
28 ;; supports multiple windows should have the ability to run vcursor.
29 ;; In fact, only overlays are required to work, though some of the
30 ;; key-bindings may need changing.
32 ;; This is much easier to use than the instructions are to read.
33 ;; I suggest you simply load it and play around with holding down Ctrl
34 ;; and Shift and pressing up, down, left, right, tab, return, and see
35 ;; what happens. (Find a scratch buffer before using C-S-tab: that
36 ;; toggles copying.)
38 ;; Most of the functions described in this documentation are in
39 ;; parentheses so that if you have the package loaded you can type C-h
40 ;; f on top of them for help.
42 ;; Using the cursor keys with both control and shift held down moves
43 ;; around a virtual cursor, which is initially at point. When active,
44 ;; it appears with an underline through it to distinguish it from the
45 ;; normal cursor. You can then use one of the other commands to copy
46 ;; characters from the location of the virtual cursor to point. This
47 ;; is very useful, for example, when copying some previous text while
48 ;; making changes to it at the same time, since you never have to move
49 ;; the "real" cursor away from where you are inserting.
51 ;; The remaining default key bindings are based around the PC-type
52 ;; cluster found above the cursor keys on a lot of keyboards, the
53 ;; function keys which my limited knowledge of X terminals expects to
54 ;; find at the top. Some functions are duplicated in more obvious
55 ;; places for the X version.
57 ;; All the keybindings require you to hold down control and shift at
58 ;; once. I assumed this combination wouldn't be heavily bound by most
59 ;; people and that it would be easy to type with the left hand.
60 ;; Inevitably it will clash with some other packages, but I can't help
61 ;; that: an intuitive binding is a prerequisite here. See below for
62 ;; other alternatives (search for "Oemacs").
63 ;;
64 ;; Holding down control and shift and pressing insert (vcursor-copy)
65 ;; copies one character from wherever the virtual cursor is to point;
66 ;; point and the virtual cursor advance in the separate and equal
67 ;; station to which... (etc.). M-C-S-return (vcursor-copy-line)
68 ;; copies to the end of the line instead of just one character,
69 ;; C-S-delete or C-S-remove (vcursor-copy-word) copies a word.
70 ;;
71 ;; A more general way of copying is to use C-S-tab, which is a toggle.
72 ;; In the "on" state, moving the virtual cursor will copy the
73 ;; moved-over text to the normal cursor position (including when going
74 ;; backwards, though each piece of text moved over is copied forwards:
75 ;; compare the behaviour of C-S-up and C-S-left).
77 ;; However, that's just a small part of the magic. If the virtual
78 ;; cursor goes off the display, it will be redisplayed in some other
79 ;; window. (See the function (vcursor-find-window) for details of how
80 ;; this window is chosen.) This gives you fingertip control over two
81 ;; windows at once.
82 ;;
83 ;; C-S-return (vcursor-disable) disables the virtual cursor, removing
84 ;; it so that it starts from point whenever you move it again --- note
85 ;; that simply moving the cursor and virtual cursor on top of one
86 ;; another does not have this effect.
87 ;;
88 ;; If you gave C-S-return a positive prefix arg, it will also delete the
89 ;; window (unless it's the current one). Whenever the virtual cursor
90 ;; goes off-screen in its own window, point in that window is moved as
91 ;; well to restore it to view. (It's easier that way, that's why.
92 ;; However, point doesn't move unless the view in the window does, so
93 ;; it's not tied to the virtual cursor location.)
95 ;; You can also use C-S-return with a negative prefix argument which
96 ;; forces the vcursor to appear at point. This is particularly useful if
97 ;; you actually want to edit in another window but would like to
98 ;; remember the current cursor location for examining or copying from
99 ;; that buffer. (I just hit C-S-right C-S-left, but I'm a hopeless
100 ;; lowbrow.)
102 ;; There is also C-S-f6 (vcursor-other-window) which behaves like
103 ;; C-x o on the virtual rather than the real cursor, except that it
104 ;; will create another window if necessary.
106 ;; The keys C-S-prior (vcursor-scroll-down) and C-S-next
107 ;; (vcursor-scroll-up) (i.e., PageUp and PageDown) will scroll the
108 ;; virtual cursor window, appropriately chosen. They will always
109 ;; create a new window or take over an old one if necessary.
110 ;; Likewise, M-C-S-left and M-C-S-right move you to the
111 ;; beginning or end of a line, C-S-home and C-S-end the
112 ;; beginning or end of a buffer (these are also on M-C-S-up and
113 ;; M-C-S-down for those of us stuck with DEC keyboards).
115 ;; C-S-f7 (vcursor-goto) will take you to the vcursor position
116 ;; (swapping windows if it seems sensible) and (unless you give it a
117 ;; prefix argument) delete the virtual cursor, so this is useful for
118 ;; you to take over editing at the virtual cursor position. It is not
119 ;; an error if the virtual cursor is not active; it simply leaves you
120 ;; at point, because that is where the virtual cursor would start
121 ;; from.
123 ;; In a similar vein, M-C-S-tab (hope your left hand's flexible;
124 ;; C-S-select on DEC keyboards) (vcursor-swap-point) will take you to
125 ;; the virtual cursor position but simultaneously put the virtual
126 ;; cursor at the old cursor position. It is also supposed to ensure
127 ;; that both are visible.
129 ;; C-S-f8 (C-S-find on DEC keyboards) (vcursor-isearch-forward)
130 ;; allows you to do an isearch in another window. It works a bit like
131 ;; vcursor-scroll-*; it moves into another window, calls isearch
132 ;; there, and sets the virtual cursor position to the point found. In
133 ;; other words, it works just like isearch but with the virtual cursor
134 ;; instead of the real one (that's why it's called a "virtual
135 ;; cursor"). While you are isearching, you are editing in the virtual
136 ;; cursor window, but when you have finished you return to where you
137 ;; started. Note that once you are in isearch all the keys are normal
138 ;; --- use C-s, not C-S-f8, to search for the next occurrence.
140 ;; If you set the variable vcursor-auto-disable, then any command
141 ;; which does not involve moving or copying from the virtual cursor
142 ;; causes the virtual cursor to be disabled. If you don't intend to
143 ;; use this, you can comment out the `add-hook' line at the bottom of
144 ;; this file. (This feature partially emulates the way the "copy" key
145 ;; on the BBC micro worked; actually, the copy cursor was homed when
146 ;; you hit return. This was in keeping with the line-by-line way of
147 ;; entering BASIC, but is less appropriate here.)
149 ;; There is a way of moving the virtual cursor using ordinary
150 ;; commands: C-S-f9 (vcursor-execute-key) reads a key string,
151 ;; moves to the virtual cursor position, executes the command bound to
152 ;; the string, then returns to the original point. Thus C-S-f9 M-m
153 ;; moves the virtual cursor back to the first non-whitespace character
154 ;; on its line. As the command is called interactively all the usual
155 ;; ways of passing information to the command called, such as by a
156 ;; prefix argument, are available. C-S-f10 (C-S-x)
157 ;; (vcursor-execute-command) behaves the same way but you enter the
158 ;; name of the command. Of course, only some commands are useful
159 ;; here, mainly simple movement commands. Killing at the virtual
160 ;; cursor position in this way works as well; you can even save
161 ;; another buffer with C-S-f9 C-x C-s. To do anything more
162 ;; complicated, you are better off using M-C-S-tab
163 ;; (vcursor-swap-point), doing whatever it is, then calling M-C-S-tab
164 ;; again.
166 ;; If you want to add your own moving or copying functions you should
167 ;; be able to do this fairly easily with (vcursor-relative-move) and
168 ;; (vcursor-copy) together with (vcursor-get-char-count). If you want to
169 ;; do something in a different window, use (vcursor-window-funcall).
171 ;; There is an alternative set of key bindings which will be used
172 ;; automatically for a PC if Oemacs is detected. This set uses separate
173 ;; control, shift and meta keys with function keys 1 to 10. In
174 ;; particular, movement keys are concentrated on f5 to f8 with (in
175 ;; increasing order of distance travelled) C-, M- and S- as prefixes.
176 ;; See the actual bindings below (search for C-f1). This is because the
177 ;; C-S- prefix is represented by weird key sequences and the set is
178 ;; incomplete; if you don't mind that, some hints are given in comments
179 ;; below.
181 ;; You can specify the usual or the Oemacs bindings by setting the
182 ;; variable vcursor-key-bindings to `xterm' or `oemacs'. You can also set
183 ;; it to nil, in which case vcursor will not make any key bindings
184 ;; and you can define your own. The default is t, which makes vcursor
185 ;; guess (it will use xterm unless it thinks Oemacs is running). The
186 ;; oemacs set will work on an X terminal with function keys, but the
187 ;; xterm set will not work under Oemacs.
189 ;; Un-features:
190 ;; - The vcursor will not move to point-max, since otherwise it would
191 ;; disappear. However, no error is flagged as point-max is a valid
192 ;; point in the buffer. Thus cursor right or down at the second
193 ;; last point in the file does not flag an error, which is inconsistent,
194 ;; and if copying is on the last character (typically newline) will
195 ;; be repeatedly copied. (I've tried making it flag an error
196 ;; instead and that's worse since often the vcursor is sent to
197 ;; point in some other window, which may be point-max.)
198 ;; - The vcursor widens when over a tab character or right at the
199 ;; end of the line. You're welcome to consider this a feature;
200 ;; it's just a part of how overlays work.
201 ;; - The vcursor obscures the real cursor. Creative use of overlays
202 ;; could cure this.
203 ;; - The vcursor does not remember its own previous positions. If
204 ;; you cycle it back into a window it was in before, it will be at
205 ;; point in that window. Often, that is where a previous recenter
206 ;; left point, not where the vcursor was before.
207 ;; (Note, however, that the vcursor does remember where it *is*,
208 ;; even if it's off-screen. This can also lead to surprises, but I
209 ;; don't think it's a bug.)
210 ;; - vcursor-window-funcall could perhaps be smarter about restoring
211 ;; the previous window state on failure.
212 ;; - The logic in vcursor-find-window is rather complicated and
213 ;; therefore bug-prone, though in practice it seems to work OK.
215 ;; Possible enhnacements:
216 ;; It would be easy to implement vcursor-push (save vcursor position
217 ;; as mark and deactivate) and vcursor-pop (deactivate vcursor and
218 ;; move to last pushed position) functions.
220 ;;; Code:
222 (or (memq 'vcursor (face-list))
223 (progn
224 (copy-face 'modeline 'vcursor)
225 (if (or (fboundp 'oemacs-version) (x-display-color-p))
226 (progn
227 (set-face-foreground 'vcursor "blue")
228 (set-face-background 'vcursor "cyan")))
229 (set-face-underline-p 'vcursor t)))
231 (defvar vcursor-auto-disable nil
232 "*If non-nil, disable the virtual cursor after use.
233 Any non-vcursor command will force `vcursor-disable' to be called.")
235 (defvar vcursor-key-bindings t
236 "*How to bind keys when vcursor is loaded.
237 If t (the default), guess; if xterm, use bindings suitable for an
238 X terminal; if oemacs, use bindings which work on a PC with Oemacs.
239 If nil, don't define any key bindings.")
241 (defvar vcursor-overlay nil
242 "Overlay for the virtual cursor.
243 It is nil if that is not enabled.")
245 (defvar vcursor-window nil
246 "Last window to have displayed the virtual cursor.
247 See the function `vcursor-find-window' for how this is used.")
249 (defvar vcursor-last-command nil
250 "Non-nil if last command was a vcursor command.
251 The commands `vcursor-copy', `vcursor-relative-move' and the ones for
252 scrolling set this. It is used by the `vcursor-auto-disable' code.")
253 ;; could do some memq-ing with last-command instead, but this will
254 ;; automatically handle any new commands using the primitives.
256 (defvar vcursor-copy-flag nil
257 "*Non-nil means moving vcursor should copy characters moved over to point.")
259 (defvar vcursor-temp-goal-column nil
260 "Keeps track of temporary goal columns for the virtual cursor.")
262 (cond
263 ((not vcursor-key-bindings)) ;; don't set any key bindings
264 ((or (eq vcursor-key-bindings 'oemacs)
265 (and (eq vcursor-key-bindings t) (fboundp 'oemacs-version)))
266 (global-set-key [C-f1] 'vcursor-toggle-copy)
267 (global-set-key [C-f2] 'vcursor-copy)
268 (global-set-key [C-f3] 'vcursor-copy-word)
269 (global-set-key [C-f4] 'vcursor-copy-line)
271 (global-set-key [S-f1] 'vcursor-disable)
272 (global-set-key [S-f2] 'vcursor-other-window)
273 (global-set-key [S-f3] 'vcursor-goto)
274 (global-set-key [S-f4] 'vcursor-swap-point)
276 (global-set-key [C-f5] 'vcursor-backward-char)
277 (global-set-key [C-f6] 'vcursor-previous-line)
278 (global-set-key [C-f7] 'vcursor-next-line)
279 (global-set-key [C-f8] 'vcursor-forward-char)
281 (global-set-key [M-f5] 'vcursor-beginning-of-line)
282 (global-set-key [M-f6] 'vcursor-backward-word)
283 (global-set-key [M-f6] 'vcursor-forward-word)
284 (global-set-key [M-f8] 'vcursor-end-of-line)
286 (global-set-key [S-f5] 'vcursor-beginning-of-buffer)
287 (global-set-key [S-f6] 'vcursor-scroll-down)
288 (global-set-key [S-f7] 'vcursor-scroll-up)
289 (global-set-key [S-f8] 'vcursor-end-of-buffer)
291 (global-set-key [C-f9] 'vcursor-isearch-forward)
293 (global-set-key [S-f9] 'vcursor-execute-key)
294 (global-set-key [S-f10] 'vcursor-execute-command)
296 ;;; Partial dictionary of Oemacs key sequences for you to roll your own,
297 ;;; e.g C-S-up: (global-set-key "\M-[\C-f\M-\C-m" 'vcursor-previous-line)
298 ;;; Sequence: Sends:
299 ;;; "\M-[\C-f\M-\C-m" C-S-up
300 ;;; "\M-[\C-f\M-\C-q" C-S-down
301 ;;; "\M-[\C-fs" C-S-left
302 ;;; "\M-[\C-ft" C-S-right
304 ;;; "\M-[\C-fw" C-S-home
305 ;;; "\M-[\C-b\C-o" S-tab
306 ;;; "\M-[\C-f\M-\C-r" C-S-insert
307 ;;; "\M-[\C-fu" C-S-end
308 ;;; "\M-[\C-f\M-\C-s" C-S-delete
309 ;;; "\M-[\C-f\M-\C-d" C-S-prior
310 ;;; "\M-[\C-fv" C-S-next
311 ;;;
312 ;;; "\M-[\C-f^" C-S-f1
313 ;;; "\M-[\C-f_" C-S-f2
314 ;;; "\M-[\C-f`" C-S-f3
315 ;;; "\M-[\C-fa" C-S-f4
316 ;;; "\M-[\C-fb" C-S-f5
317 ;;; "\M-[\C-fc" C-S-f6
318 ;;; "\M-[\C-fd" C-S-f7
319 ;;; "\M-[\C-fe" C-S-f8
320 ;;; "\M-[\C-ff" C-S-f9
321 ;;; "\M-[\C-fg" C-S-f10
324 (global-set-key [C-S-up] 'vcursor-previous-line)
325 (global-set-key [C-S-down] 'vcursor-next-line)
326 (global-set-key [C-S-left] 'vcursor-backward-char)
327 (global-set-key [C-S-right] 'vcursor-forward-char)
329 (global-set-key [C-S-return] 'vcursor-disable)
330 (global-set-key [C-S-insert] 'vcursor-copy)
331 (global-set-key [C-S-delete] 'vcursor-copy-word)
332 (global-set-key [C-S-remove] 'vcursor-copy-word)
333 (global-set-key [C-S-tab] 'vcursor-toggle-copy)
334 (global-set-key [C-S-home] 'vcursor-beginning-of-buffer)
335 (global-set-key [M-C-S-up] 'vcursor-beginning-of-buffer)
336 (global-set-key [C-S-end] 'vcursor-end-of-buffer)
337 (global-set-key [M-C-S-down] 'vcursor-end-of-buffer)
338 (global-set-key [C-S-prior] 'vcursor-scroll-down)
339 (global-set-key [C-S-next] 'vcursor-scroll-up)
341 (global-set-key [C-S-f6] 'vcursor-other-window)
342 (global-set-key [C-S-f7] 'vcursor-goto)
344 (global-set-key [C-S-select] 'vcursor-swap-point) ; DEC keyboards
345 (global-set-key [M-C-S-tab] 'vcursor-swap-point)
347 (global-set-key [C-S-find] 'vcursor-isearch-forward) ; DEC keyboards
348 (global-set-key [C-S-f8] 'vcursor-isearch-forward)
350 (global-set-key [M-C-S-left] 'vcursor-beginning-of-line)
351 (global-set-key [M-C-S-right] 'vcursor-end-of-line)
353 (global-set-key [M-C-S-prior] 'vcursor-backward-word)
354 (global-set-key [M-C-S-next] 'vcursor-forward-word)
356 (global-set-key [M-C-S-return] 'vcursor-copy-line)
358 (global-set-key [C-S-f9] 'vcursor-execute-key)
359 (global-set-key [C-S-f10] 'vcursor-execute-command)
362 (defun vcursor-locate ()
363 "Go to the starting point of the virtual cursor.
364 If that's disabled, don't go anywhere but don't complain."
365 ;; This is where we go off-mass-shell. Assume there is a
366 ;; save-excursion to get us back to the pole, er, point.
368 (and (overlayp vcursor-overlay)
369 (overlay-buffer vcursor-overlay)
370 (set-buffer (overlay-buffer vcursor-overlay))
371 (goto-char (overlay-start vcursor-overlay)))
374 (defun vcursor-find-window (&optional not-this new-win this-frame)
375 "Return a suitable window for displaying the virtual cursor.
376 This is the first window in cyclic order where the vcursor is visible.
378 With optional NOT-THIS non-nil never return the current window.
380 With NEW-WIN non-nil, display the virtual cursor buffer in another
381 window if the virtual cursor is not currently visible \(note, however,
382 that this function never changes window-point\).
384 With THIS-FRAME non-nil, don't search other frames for a new window
385 \(though if the vcursor is already off-frame then its current window is
386 always considered, and the value of `pop-up-frames' is always respected\).
388 Returns nil if the virtual cursor is not visible anywhere suitable.
389 Set `vcursor-window' to the returned value as a side effect."
391 ;; The order of priorities (respecting NOT-THIS) is (1)
392 ;; vcursor-window if the virtual cursor is visible there (2) any
393 ;; window displaying the virtual cursor (3) vcursor-window provided
394 ;; it is still displaying the buffer containing the virtual cursor and
395 ;; is not selected (4) any unselected window displaying the vcursor
396 ;; buffer (5) with NEW-WIN, a window selected by display-buffer (so
397 ;; the variables pop-up-windows and pop-up-frames are significant)
398 ;; (6) nil.
400 (let ((thiswin (selected-window)) winok winbuf)
401 (save-excursion
402 (vcursor-locate)
403 (or (and (window-live-p vcursor-window)
404 (eq (current-buffer) (window-buffer vcursor-window))
405 (not (and not-this (eq thiswin vcursor-window))))
406 (setq vcursor-window nil))
407 (or (and vcursor-window ; choice 1
408 (pos-visible-in-window-p (point) vcursor-window))
409 (progn
410 (walk-windows
411 (function
412 (lambda (win)
413 (and (not winok)
414 (eq (current-buffer) (window-buffer win))
415 (not (and not-this (eq thiswin win)))
416 (cond
417 ((pos-visible-in-window-p (point) win) (setq winok win))
418 ((eq thiswin win))
419 ((not winbuf) (setq winbuf win))))))
420 nil (not this-frame))
421 (setq vcursor-window
422 (cond
423 (winok) ; choice 2
424 ((and vcursor-window ; choice 3
425 (not (eq thiswin vcursor-window))) vcursor-window)
426 (winbuf) ; choice 4
427 (new-win (display-buffer (current-buffer) t)) ; choice 5
428 (t nil))))))) ; default (choice 6)
429 vcursor-window
432 (defun vcursor-toggle-copy (&optional arg nomsg)
433 "Toggle copying to point when the vcursor is moved.
434 With a prefix ARG, turn on if non-negative, off if negative.
435 Display a message unless optional NOMSG is non-nil."
436 (interactive "P")
437 (setq vcursor-copy-flag
438 (cond ((not arg) (not vcursor-copy-flag))
439 ((< (prefix-numeric-value arg) 0) nil)
440 (t))
441 vcursor-last-command t)
442 (or nomsg (message "Copying from the vcursor is now %s."
443 (if vcursor-copy-flag "on" "off")))
446 (defun vcursor-move (pt)
447 "Move the virtual cursor to the character to the right of PT.
448 PT is an absolute location in the current buffer.
450 If the new virtual cursor location would not be visible, display it in
451 another window."
452 ;; this works even if we're on-mass-shell, but usually we won't be.
454 (if (eq pt (point-max)) (setq pt (1- pt)))
455 (if (vcursor-check t)
456 (move-overlay vcursor-overlay pt (+ pt 1) (current-buffer))
457 (setq vcursor-overlay (make-overlay pt (+ pt 1)))
458 (overlay-put vcursor-overlay 'face 'vcursor))
459 (vcursor-find-window nil t)
460 ;; vcursor-window now contains the right buffer
461 (or (pos-visible-in-window-p pt vcursor-window)
462 (set-window-point vcursor-window pt))
465 (defun vcursor-relative-move (fn &rest args)
466 "Use FUNCTION with arbitrary ARG1 ... to move the virtual cursor.
468 This is called by most of the virtual-cursor motion commands."
469 (let (text opoint)
470 (save-excursion
471 (vcursor-locate)
472 (setq opoint (point))
473 (apply fn args)
474 (and (eq opoint (point-max)) (eq opoint (point))
475 (signal 'end-of-buffer nil))
476 (vcursor-move (point))
477 (if vcursor-copy-flag (setq text (buffer-substring opoint (point)))))
478 (if text (insert text)))
479 (setq vcursor-last-command t)
482 (defun vcursor-goto (&optional arg)
483 "Move the real cursor to the virtual cursor position.
484 If the virtual cursor is (or was recently) visible in another window,
485 switch to that first. Without a prefix ARG, disable the virtual
486 cursor as well."
488 (interactive "P")
489 (and (vcursor-find-window) (select-window vcursor-window))
490 (let ((buf (and vcursor-overlay (overlay-buffer vcursor-overlay))))
491 (and buf (not (eq (current-buffer) buf)) (switch-to-buffer buf)))
492 (vcursor-locate)
493 (or arg (vcursor-disable))
496 (defun vcursor-swap-point ()
497 "Swap the location of point and that of the virtual cursor.
499 The virtual cursor window becomes the selected window and the old
500 window becomes the virtual cursor window. If the virtual cursor would
501 not be visible otherwise, display it in another window."
503 (interactive)
504 (let ((buf (current-buffer)) (here (point)) (win (selected-window)))
505 (vcursor-goto) ; will disable the vcursor
506 (save-excursion
507 (set-buffer buf)
508 (setq vcursor-window win)
509 (vcursor-move here)))
512 (defun vcursor-scroll-up (&optional n)
513 "Scroll up the vcursor window ARG lines or near full screen if none.
514 The vcursor will always appear in an unselected window."
516 (interactive "P")
517 (vcursor-window-funcall 'scroll-up n)
520 (defun vcursor-scroll-down (&optional n)
521 "Scroll down the vcursor window ARG lines or near-full screen if none.
522 The vcursor will always appear in an unselected window."
524 (interactive "P")
525 (vcursor-window-funcall 'scroll-down n)
528 (defun vcursor-isearch-forward (&optional rep norecurs)
529 "Perform forward incremental search in the virtual cursor window.
530 The virtual cursor is moved to the resulting point; the ordinary
531 cursor stays where it was."
533 (interactive "P")
534 (vcursor-window-funcall 'isearch-forward rep norecurs)
537 (defun vcursor-window-funcall (func &rest args)
538 "Call FUNC with ARGS ... in a virtual cursor window.
539 A window other than the currently-selected one will always be used.
540 The virtual cursor is moved to the value of point when the function
541 returns."
543 (vcursor-find-window t t)
544 (let ((sw (selected-window)) text)
545 ;; We can't use save-window-excursion because that would restore
546 ;; the original display in the window we may want to alter.
547 (unwind-protect
548 (let ((here (point)))
549 (select-window vcursor-window)
550 (vcursor-locate)
551 (apply func args)
552 (if vcursor-copy-flag (setq text (buffer-substring here (point))))
553 (vcursor-move (point)))
554 (select-window sw))
555 (if text (insert text)))
556 (setq vcursor-last-command t)
559 (defun vcursor-get-char-count (fn &rest args)
560 "Apply FN to ARG1 ... and return the number of characters moved.
561 Point is temporarily set to the virtual cursor position before FN is
562 called.
564 This is called by most of the virtual-cursor copying commands to find
565 out how much to copy."
567 (vcursor-check)
568 (save-excursion
569 (set-buffer (overlay-buffer vcursor-overlay))
570 (let ((start (goto-char (overlay-start vcursor-overlay))))
571 (- (progn (apply fn args) (point)) start)))
574 ;; Make sure the virtual cursor is active. Unless arg is non-nil,
575 ;; report an error if it is not.
576 (defun vcursor-check (&optional arg)
577 (cond
578 ((and (overlayp vcursor-overlay) (overlay-start vcursor-overlay))
580 (arg nil)
581 (t (error "The virtual cursor is not active now.")))
584 (defun vcursor-disable (&optional arg)
585 "Disable the virtual cursor.
586 Next time you use it, it will start from point.
588 With a positive prefix ARG, the first window in cyclic order
589 displaying the virtual cursor (or which was recently displaying the
590 virutal cursor) will be deleted unless it's the selected
591 window.
593 With a negative prefix argument, enable the virtual cursor: make it
594 active at the same point as the real cursor.
596 Copying mode is always turned off: the next use of the vcursor will
597 not copy text until you turn it on again."
599 (interactive "P")
600 (if (overlayp vcursor-overlay)
601 (progn
602 (delete-overlay vcursor-overlay)
603 (setq vcursor-overlay nil)))
604 (cond
605 ((not (vcursor-find-window t)))
606 ((or (not arg) (< (prefix-numeric-value arg) 0)))
607 ((delete-window vcursor-window)))
608 (and arg (< (prefix-numeric-value arg) 0)
609 (progn
610 (vcursor-move (point))
611 (setq vcursor-window (selected-window))))
612 (setq vcursor-copy-flag nil)
615 (defun vcursor-other-window (n &optional all-frames)
616 "Activate the virtual cursor in another window.
617 This is the next window cylically after one currently showing the
618 virtual cursor, or else after the current selected window. If there
619 is no other window, the current window is split.
621 Arguments N and optional ALL-FRAMES are the same as with other-window.
622 ALL-FRAMES is also used to decide whether to split the window."
624 (interactive "p")
625 (if (if (fboundp 'oemacs-version)
626 (one-window-p nil)
627 (one-window-p nil all-frames))
628 (display-buffer (current-buffer) t))
629 (save-excursion
630 (save-window-excursion
631 ;; We don't use fancy vcursor-find-window trickery, since we're
632 ;; quite happy to have the vcursor cycle back into the current
633 ;; window.
634 (let ((sw (selected-window))
635 (win (vcursor-find-window nil nil (not all-frames))))
636 (if win (select-window win))
637 ;; else start from here
638 (other-window n all-frames)
639 (vcursor-disable -1))))
642 (defun vcursor-compare-windows (&optional arg)
643 "Call `compare-windows' in the vcursor window.
644 This has the effect of comparing the vcursor window with whichever
645 window `next-window' returns there, which may not be the selected one.
647 A prefix argument, if any, is passed to `compare-windows'."
648 (interactive "P")
649 (vcursor-window-funcall 'compare-windows arg))
651 (defun vcursor-next-line (arg)
652 "Move the virtual cursor forward ARG lines."
653 ;; This is next-line rewritten for the vcursor. Maybe it would
654 ;; be easier simply to rewrite line-move.
655 (interactive "p")
656 (let (temporary-goal-column opoint text)
657 (save-excursion
658 (vcursor-locate)
659 (setq temporary-goal-column
660 (if (or (eq last-command 'vcursor-next-line)
661 (eq last-command 'vcursor-previous-line))
662 (progn
663 (setq last-command 'next-line) ; trick line-move
664 vcursor-temp-goal-column)
665 (if (and track-eol (eolp)
666 (or (not (bolp)) (eq last-command 'end-of-line)))
667 9999
668 (current-column)))
669 opoint (point))
670 (line-move arg)
671 (and (eq opoint (point-max)) (eq opoint (point))
672 (signal 'end-of-buffer nil))
673 (if vcursor-copy-flag (setq text (buffer-substring opoint (point))))
674 (vcursor-move (point))
675 (setq vcursor-temp-goal-column temporary-goal-column
676 vcursor-last-command t))
677 (if text (insert text)))
680 (defun vcursor-previous-line (arg)
681 "Move the virtual cursor back ARG lines."
682 (interactive "p")
683 (vcursor-next-line (- arg))
686 (defun vcursor-forward-char (arg)
687 "Move the virtual cursor forward ARG characters."
688 (interactive "p")
689 (vcursor-relative-move 'forward-char arg)
692 (defun vcursor-backward-char (arg)
693 "Move the virtual cursor backward ARG characters."
694 (interactive "p")
695 (vcursor-relative-move 'backward-char arg)
698 (defun vcursor-forward-word (arg)
699 "Move the virtual cursor forward ARG words."
700 (interactive "p")
701 (vcursor-relative-move 'forward-word arg)
704 (defun vcursor-backward-word (arg)
705 "Move the virtual cursor backward ARG words."
706 (interactive "p")
707 (vcursor-relative-move 'backward-word arg)
710 (defun vcursor-beginning-of-line (arg)
711 "Move the virtual cursor to beginning of its current line.
712 ARG is as for `beginning-of-line'."
713 (interactive "P")
714 (vcursor-relative-move 'beginning-of-line
715 (if arg (prefix-numeric-value arg)))
718 (defun vcursor-end-of-line (arg)
719 "Move the virtual cursor to end of its current line.
720 ARG is as for `end-of-line'."
721 (interactive "P")
722 (vcursor-relative-move 'end-of-line
723 (if arg (prefix-numeric-value arg)))
726 (defun vcursor-beginning-of-buffer (&optional arg)
727 "Move the virtual cursor to the beginning of its buffer.
728 ARG is as for beginning-of-buffer."
729 (interactive "P")
730 (vcursor-relative-move
731 (lambda (arg)
732 (goto-char (if arg (/ (* arg (- (point-max) (point-min))) 10)
733 (point-min))))
734 (if arg (prefix-numeric-value arg)))
737 (defun vcursor-end-of-buffer (&optional arg)
738 "Move the virtual cursor to the end of its buffer.
739 ARG is as for end-of-buffer.
741 Actually, the vcursor is moved to the second from last character or it
742 would be invisible."
743 (interactive "P")
744 (vcursor-relative-move
745 (lambda (arg)
746 (goto-char (if arg (- (point-max)
747 (/ (* arg (- (point-max) (point-min))) 10))
748 (point-max))))
749 (if arg (prefix-numeric-value arg)))
752 (defun vcursor-execute-command (cmd)
753 "Execute COMMAND for the virtual cursor.
754 COMMAND is called interactively. Not all commands (in fact, only a
755 small subset) are useful."
756 (interactive "CCommand: ")
757 (let (text opoint)
758 (save-excursion
759 (vcursor-locate)
760 (setq opoint (point))
761 (call-interactively cmd)
762 (if vcursor-copy-flag (setq text (buffer-substring opoint (point))))
763 (vcursor-move (point)))
764 (if text (insert text)))
765 (setq vcursor-last-command t)
768 (defun vcursor-execute-key (keys)
769 "Execute the command bound to KEYS for the virtual cursor.
770 The command found is called interactively, so prefix argument etc.
771 are usable."
773 (interactive "kKey sequence: ")
774 (let ((cmd (key-binding keys)))
775 (if cmd (vcursor-execute-command (key-binding keys))))
778 (defun vcursor-copy (arg)
779 "Copy ARG characters from the virtual cursor position to point."
780 (interactive "p")
781 (vcursor-check)
782 (insert
783 (save-excursion
784 (set-buffer (overlay-buffer vcursor-overlay))
785 (let* ((ostart (overlay-start vcursor-overlay))
786 (end (+ ostart arg)))
787 (prog1
788 (buffer-substring ostart end)
789 (vcursor-move end)))))
790 (setq vcursor-last-command t)
793 (defun vcursor-copy-word (arg)
794 "Copy ARG words from the virtual cursor position to point."
795 (interactive "p")
796 (vcursor-copy (vcursor-get-char-count 'forward-word arg))
799 (defun vcursor-copy-line (arg)
800 "Copy up to ARGth line after virtual cursor position.
801 With no argument, copy to the end of the current line.
803 Behaviour with regard to newlines is similar (but not identical) to
804 `kill-line'; the main difference is that whitespace at the end of the
805 line is treated like ordinary characters."
807 (interactive "P")
808 (let* ((num (prefix-numeric-value arg))
809 (count (vcursor-get-char-count 'end-of-line num)))
810 (vcursor-copy (if (or (= count 0) arg) (1+ count) count)))
813 (defun vcursor-post-command ()
814 (and vcursor-auto-disable (not vcursor-last-command)
815 vcursor-overlay (vcursor-disable))
816 (setq vcursor-last-command nil)
819 (add-hook 'post-command-hook 'vcursor-post-command)
821 (provide 'vcursor)
823 ;; vcursor.el ends here