Add 2008 to copyright years.
[bpt/emacs.git] / lisp / vcursor.el
CommitLineData
55535639 1;;; vcursor.el --- manipulate an alternative ("virtual") cursor
bf078b46 2
c90f2757 3;; Copyright (C) 1994, 1996, 1998, 2001, 2002, 2003,
409cc4a3 4;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
bf078b46 5
2d6a4733 6;; Author: Peter Stephenson <pws@ibmth.df.unipi.it>
19b4af82 7;; Maintainer: FSF
949b810e 8;; Keywords: virtual cursor, convenience
bf078b46 9
83d7d14c
RS
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
bf078b46 13;; it under the terms of the GNU General Public License as published by
b4aa6026 14;; the Free Software Foundation; either version 3, or (at your option)
83d7d14c 15;; any later version.
bf078b46 16
83d7d14c 17;; GNU Emacs is distributed in the hope that it will be useful,
bf078b46
RS
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
bf078b46
RS
26
27;;; Commentary:
28
2d6a4733
RS
29;; Latest changes
30;; ==============
29fddfff 31;;
2d6a4733
RS
32;; - *IMPORTANT* vcursor-key-bindings is now nil by default, to avoid
33;; side-effects when the package is loaded. This means no keys are
34;; bound by default. Use customize to change it to t to restore
35;; the old behaviour. (If you do it by hand in .emacs, it
36;; must come before vcursor is loaded.)
37;; - You can alter the main variables and the vcursor face via
38;; M-x customize: go to the Editing group and find Vcursor.
39;; - vcursor-auto-disable can now be 'copy (actually any value not nil
40;; or t), which means that copying from the vcursor will be turned
41;; off after any operation not involving the vcursor, but the
42;; vcursor itself will be left alone.
29fddfff
RS
43;; - works on dumb terminals with Emacs 19.29 and later
44;; - new keymap vcursor-map for binding to a prefix key
45;; - vcursor-compare-windows substantially improved
46;; - vcursor-execute-{key,command} much better about using the
47;; right keymaps and arranging for the correct windows to be used
48;; - vcursor-window-funcall can call functions interactively
49;; - vcursor-interpret-input for special effects
50;;
51;; Introduction
52;; ============
53;;
bf078b46 54;; Virtual cursor commands. I got this idea from the old BBC micro.
29fddfff
RS
55;; You need Emacs 19 or 20 and a window system for the best effects.
56;; For character terminals, at least Emacs 19.29 is required
57;; (special behaviour for the overlay property
58;; "before-string" must be implemented). Search for "dumb terminals"
59;; for more information.
bf078b46
RS
60;;
61;; This is much easier to use than the instructions are to read.
2d6a4733
RS
62;; First, you need to let vcursor define some keys: setting
63;; vcursor-key-bindings to t before loading, or by customize, will
64;; define various keys with the prefix C-S. You'll have to read
65;; further if you don't want this. Then I suggest you simply load it
66;; and play around with holding down Ctrl and Shift and pressing up,
67;; down, left, right, tab, return, and see what happens. (Find a
68;; scratch buffer before using C-S-tab: that toggles copying.)
bf078b46
RS
69;;
70;; Most of the functions described in this documentation are in
71;; parentheses so that if you have the package loaded you can type C-h
72;; f on top of them for help.
73;;
74;; Using the cursor keys with both control and shift held down moves
75;; around a virtual cursor, which is initially at point. When active,
76;; it appears with an underline through it to distinguish it from the
77;; normal cursor. You can then use one of the other commands to copy
78;; characters from the location of the virtual cursor to point. This
79;; is very useful, for example, when copying some previous text while
80;; making changes to it at the same time, since you never have to move
81;; the "real" cursor away from where you are inserting.
82;;
83;; The remaining default key bindings are based around the PC-type
84;; cluster found above the cursor keys on a lot of keyboards, the
85;; function keys which my limited knowledge of X terminals expects to
86;; find at the top. Some functions are duplicated in more obvious
87;; places for the X version.
88;;
89;; All the keybindings require you to hold down control and shift at
90;; once. I assumed this combination wouldn't be heavily bound by most
91;; people and that it would be easy to type with the left hand.
92;; Inevitably it will clash with some other packages, but I can't help
93;; that: an intuitive binding is a prerequisite here. See below for
29fddfff
RS
94;; other alternatives (search for "Oemacs"). There is also a keymap
95;; which you can bind to a prefix key, which may give some more
96;; intuitive alternatives in some cases, see `The vcursor keymap' below.
d49e7392 97;;
bf078b46
RS
98;; Holding down control and shift and pressing insert (vcursor-copy)
99;; copies one character from wherever the virtual cursor is to point;
100;; point and the virtual cursor advance in the separate and equal
101;; station to which... (etc.). M-C-S-return (vcursor-copy-line)
102;; copies to the end of the line instead of just one character,
103;; C-S-delete or C-S-remove (vcursor-copy-word) copies a word.
d49e7392 104;;
bf078b46
RS
105;; A more general way of copying is to use C-S-tab, which is a toggle.
106;; In the "on" state, moving the virtual cursor will copy the
107;; moved-over text to the normal cursor position (including when going
108;; backwards, though each piece of text moved over is copied forwards:
109;; compare the behaviour of C-S-up and C-S-left).
110;;
111;; However, that's just a small part of the magic. If the virtual
112;; cursor goes off the display, it will be redisplayed in some other
113;; window. (See the function (vcursor-find-window) for details of how
114;; this window is chosen.) This gives you fingertip control over two
115;; windows at once.
d49e7392 116;;
bf078b46
RS
117;; C-S-return (vcursor-disable) disables the virtual cursor, removing
118;; it so that it starts from point whenever you move it again --- note
119;; that simply moving the cursor and virtual cursor on top of one
120;; another does not have this effect.
d49e7392 121;;
29fddfff 122;; If you give C-S-return a positive prefix arg, it will also delete the
bf078b46
RS
123;; window (unless it's the current one). Whenever the virtual cursor
124;; goes off-screen in its own window, point in that window is moved as
125;; well to restore it to view. (It's easier that way, that's why.
126;; However, point doesn't move unless the view in the window does, so
127;; it's not tied to the virtual cursor location.)
128;;
129;; You can also use C-S-return with a negative prefix argument which
130;; forces the vcursor to appear at point. This is particularly useful if
131;; you actually want to edit in another window but would like to
132;; remember the current cursor location for examining or copying from
133;; that buffer. (I just hit C-S-right C-S-left, but I'm a hopeless
134;; lowbrow.)
d49e7392 135;;
bf078b46
RS
136;; There is also C-S-f6 (vcursor-other-window) which behaves like
137;; C-x o on the virtual rather than the real cursor, except that it
138;; will create another window if necessary.
139;;
140;; The keys C-S-prior (vcursor-scroll-down) and C-S-next
141;; (vcursor-scroll-up) (i.e., PageUp and PageDown) will scroll the
142;; virtual cursor window, appropriately chosen. They will always
143;; create a new window or take over an old one if necessary.
144;; Likewise, M-C-S-left and M-C-S-right move you to the
145;; beginning or end of a line, C-S-home and C-S-end the
146;; beginning or end of a buffer (these are also on M-C-S-up and
147;; M-C-S-down for those of us stuck with DEC keyboards).
148;;
149;; C-S-f7 (vcursor-goto) will take you to the vcursor position
150;; (swapping windows if it seems sensible) and (unless you give it a
151;; prefix argument) delete the virtual cursor, so this is useful for
152;; you to take over editing at the virtual cursor position. It is not
153;; an error if the virtual cursor is not active; it simply leaves you
154;; at point, because that is where the virtual cursor would start
155;; from.
156;;
157;; In a similar vein, M-C-S-tab (hope your left hand's flexible;
158;; C-S-select on DEC keyboards) (vcursor-swap-point) will take you to
159;; the virtual cursor position but simultaneously put the virtual
160;; cursor at the old cursor position. It is also supposed to ensure
161;; that both are visible.
162;;
163;; C-S-f8 (C-S-find on DEC keyboards) (vcursor-isearch-forward)
164;; allows you to do an isearch in another window. It works a bit like
165;; vcursor-scroll-*; it moves into another window, calls isearch
166;; there, and sets the virtual cursor position to the point found. In
167;; other words, it works just like isearch but with the virtual cursor
168;; instead of the real one (that's why it's called a "virtual
169;; cursor"). While you are isearching, you are editing in the virtual
170;; cursor window, but when you have finished you return to where you
171;; started. Note that once you are in isearch all the keys are normal
172;; --- use C-s, not C-S-f8, to search for the next occurrence.
173;;
174;; If you set the variable vcursor-auto-disable, then any command
175;; which does not involve moving or copying from the virtual cursor
2d6a4733
RS
176;; causes the virtual cursor to be disabled. If you set it to non-nil
177;; but not t, then the vcursor itself will remain active, but copying
178;; will be turned off, so that the next time the vcursor is moved no
179;; text is copied over. Experience shows that this setting is
180;; particularly useful. If you don't intend to use this, you can
181;; comment out the `add-hook' line at the bottom of this file. (This
182;; feature partially emulates the way the "copy" key on the BBC micro
183;; worked; actually, the copy cursor was homed when you hit return.
184;; This was in keeping with the line-by-line way of entering BASIC,
185;; but is less appropriate here.)
bf078b46 186;;
29fddfff
RS
187;; vcursor-compare-windows is now a reliable adaption of
188;; compare-windows, which compares between point in the current buffer
189;; and the vcursor location in the other one. It is an error if
190;; vcursor is not set, however it will be brought up in another window
191;; if it is not currently visible. The prefix argument acts just like
192;; compare-windows, ignoring whitespace if set. (In versions before
193;; 1.6, this simply called compare-windows, which was much less likely
194;; to pick the two windows you wanted.)
195;;
bf078b46
RS
196;; There is a way of moving the virtual cursor using ordinary
197;; commands: C-S-f9 (vcursor-execute-key) reads a key string,
198;; moves to the virtual cursor position, executes the command bound to
199;; the string, then returns to the original point. Thus C-S-f9 M-m
200;; moves the virtual cursor back to the first non-whitespace character
201;; on its line. As the command is called interactively all the usual
202;; ways of passing information to the command called, such as by a
29fddfff
RS
203;; prefix argument, are available. This has many uses not necessarily
204;; related to moving the vcursor itself; it can do essentially
205;; everything that the \C-x 4 series of commands can do and a lot
206;; more. Note, however, that a new window is not used if the vcursor
207;; is visible in the current one: this can lead to some strange effects,
208;; but it is preferable to making a new window every time the vcursor
209;; is moved in this may.
210;;
211;; C-S-f10 (C-S-x) (vcursor-execute-command) behaves the same way but
212;; you enter the name of the command. To do anything really
bf078b46
RS
213;; complicated, you are better off using M-C-S-tab
214;; (vcursor-swap-point), doing whatever it is, then calling M-C-S-tab
215;; again.
216;;
217;; If you want to add your own moving or copying functions you should
218;; be able to do this fairly easily with (vcursor-relative-move) and
219;; (vcursor-copy) together with (vcursor-get-char-count). If you want to
220;; do something in a different window, use (vcursor-window-funcall).
221;;
29fddfff
RS
222;; Key bindings
223;; ============
d49e7392 224;;
bf078b46
RS
225;; There is an alternative set of key bindings which will be used
226;; automatically for a PC if Oemacs is detected. This set uses separate
227;; control, shift and meta keys with function keys 1 to 10. In
228;; particular, movement keys are concentrated on f5 to f8 with (in
229;; increasing order of distance travelled) C-, M- and S- as prefixes.
230;; See the actual bindings below (search for C-f1). This is because the
231;; C-S- prefix is represented by weird key sequences and the set is
232;; incomplete; if you don't mind that, some hints are given in comments
233;; below.
234;;
235;; You can specify the usual or the Oemacs bindings by setting the
236;; variable vcursor-key-bindings to `xterm' or `oemacs'. You can also set
237;; it to nil, in which case vcursor will not make any key bindings
238;; and you can define your own. The default is t, which makes vcursor
239;; guess (it will use xterm unless it thinks Oemacs is running). The
240;; oemacs set will work on an X terminal with function keys, but the
241;; xterm set will not work under Oemacs.
242;;
29fddfff
RS
243;; Usage on dumb terminals
244;; =======================
245;;
246;; If Emacs has set the variable window-system to nil, vcursor will
247;; assume that overlays cannot be displayed in a different face,
d49e7392 248;; and will instead use a string (the variable vcursor-string, by
29fddfff
RS
249;; default "**>") to show its position. This was first implemented
250;; in Emacs 19.29. Unlike the old-fashioned overlay arrow (as used
251;; by debuggers), this appears between existing text, which can
252;; make it hard to read if you're not used to it. (This seemed the
253;; better option here.) This means moving the vcursor up and down is
254;; a very efficient way of locating it!
255;;
256;; Everything else should function as expected, but there is no way to
257;; get an easy key binding for the vcursor keys on a generic terminal.
258;; Consequently a special keymap is defined for you to use traditional
259;; methods: the keymap, however, is available on any terminal type.
d49e7392 260;;
29fddfff
RS
261;; The vcursor keymap
262;; ==================
263;;
264;; In addition to any other bindings, vcursor-map contains key definitions
265;; for handling the vcursor. You should assign this to a prefix key
266;; in the usual way, e.g.
267;; (global-set-key [f14] vcursor-map)
268;; and also as usual \C-h in this map will list the key definitions, which
269;; are designed to be easy to remember.
270;;
271;; A special feature is provided by (vcursor-toggle-vcursor-map), bound
272;; to t in that keymap. With this in effect, the main keymap
273;; is overridden by the vcursor map, so keys like \C-p and so on
274;; move the vcursor instead. Remember how to turn it off (type t),
275;; or you are in serious trouble! Note that the cursor keys are not
276;; bound by default in this keymap and will continue to move the
277;; ordinary cursor.
278;;
279;; Interpreted input
280;; =================
281;;
282;; Just occasionally, you may want to pretend the strings copied from
283;; the vcursor position are to be interpreted as if you had typed them
284;; from the keyboard. Normally, they will just insert themselves anyway,
285;; but in some modes (Info and calc for example) typing ordinary characters
286;; does something else. To get this effect, set
287;; vcursor-interpret-input to t. This is normally not a good idea as
288;; interpreting input is very much slower than copying text.
d49e7392 289;;
29fddfff
RS
290;; Un-features
291;; ===========
292;;
bf078b46
RS
293;; - The vcursor will not move to point-max, since otherwise it would
294;; disappear. However, no error is flagged as point-max is a valid
295;; point in the buffer. Thus cursor right or down at the second
296;; last point in the file does not flag an error, which is inconsistent,
297;; and if copying is on the last character (typically newline) will
298;; be repeatedly copied. (I've tried making it flag an error
299;; instead and that's worse since often the vcursor is sent to
300;; point in some other window, which may be point-max.)
301;; - The vcursor widens when over a tab character or right at the
302;; end of the line. You're welcome to consider this a feature;
303;; it's just a part of how overlays work.
304;; - The vcursor obscures the real cursor. Creative use of overlays
305;; could cure this.
306;; - The vcursor does not remember its own previous positions. If
307;; you cycle it back into a window it was in before, it will be at
308;; point in that window. Often, that is where a previous recenter
309;; left point, not where the vcursor was before.
310;; (Note, however, that the vcursor does remember where it *is*,
311;; even if it's off-screen. This can also lead to surprises, but I
312;; don't think it's a bug.)
313;; - vcursor-window-funcall could perhaps be smarter about restoring
314;; the previous window state on failure.
315;; - The logic in vcursor-find-window is rather complicated and
316;; therefore bug-prone, though in practice it seems to work OK.
317;;
69e82404 318;; Possible enhancements:
bf078b46
RS
319;; It would be easy to implement vcursor-push (save vcursor position
320;; as mark and deactivate) and vcursor-pop (deactivate vcursor and
321;; move to last pushed position) functions.
322
323;;; Code:
324
46a610ab
DL
325(eval-when-compile (require 'compare-w))
326
4bef9110
SE
327(defgroup vcursor nil
328 "Manipulate an alternative (\"virtual\") cursor."
329 :prefix "vcursor-"
949b810e 330 :group 'editing)
4bef9110 331
2d6a4733
RS
332(defface vcursor
333 '((((class color)) (:foreground "blue" :background "cyan" :underline t))
334 (t (:inverse-video t :underline t)))
335 "Face for the virtual cursor."
336 :group 'vcursor)
bf078b46 337
4bef9110 338(defcustom vcursor-auto-disable nil
bf078b46 339 "*If non-nil, disable the virtual cursor after use.
2d6a4733
RS
340Any non-vcursor command will force `vcursor-disable' to be called.
341If non-nil but not t, just make sure copying is toggled off, but don't
342disable the vcursor."
343 :type '(choice (const t) (const nil) (const copy))
4bef9110 344 :group 'vcursor)
bf078b46 345
69e82404
EZ
346(defcustom vcursor-modifiers (list 'control 'shift)
347 "*A list of modifiers that are used to define vcursor key bindings."
348 :type '(repeat symbol)
349 :group 'vcursor)
350
2d6a4733 351;; Needed for defcustom, must be up here
df003ee6 352(defun vcursor-cs-binding (base &optional meta)
69e82404 353 (vector (let ((key (append vcursor-modifiers (list (intern base)))))
df003ee6
DL
354 (if meta
355 (cons 'meta key)
356 key))))
2d6a4733
RS
357
358(defun vcursor-bind-keys (var value)
359 "Alter the value of the variable VAR to VALUE, binding keys as required.
5a7383fd 360VAR is usually `vcursor-key-bindings'. Normally this function is called
2d6a4733
RS
361on loading vcursor and from the customize package."
362 (set var value)
363 (cond
364 ((not value));; don't set any key bindings
365 ((or (eq value 'oemacs)
366 (and (eq value t) (fboundp 'oemacs-version)))
367 (global-set-key [C-f1] 'vcursor-toggle-copy)
368 (global-set-key [C-f2] 'vcursor-copy)
369 (global-set-key [C-f3] 'vcursor-copy-word)
370 (global-set-key [C-f4] 'vcursor-copy-line)
371
372 (global-set-key [S-f1] 'vcursor-disable)
373 (global-set-key [S-f2] 'vcursor-other-window)
374 (global-set-key [S-f3] 'vcursor-goto)
375 (global-set-key [S-f4] 'vcursor-swap-point)
376
377 (global-set-key [C-f5] 'vcursor-backward-char)
378 (global-set-key [C-f6] 'vcursor-previous-line)
379 (global-set-key [C-f7] 'vcursor-next-line)
380 (global-set-key [C-f8] 'vcursor-forward-char)
381
382 (global-set-key [M-f5] 'vcursor-beginning-of-line)
383 (global-set-key [M-f6] 'vcursor-backward-word)
384 (global-set-key [M-f6] 'vcursor-forward-word)
385 (global-set-key [M-f8] 'vcursor-end-of-line)
386
387 (global-set-key [S-f5] 'vcursor-beginning-of-buffer)
388 (global-set-key [S-f6] 'vcursor-scroll-down)
389 (global-set-key [S-f7] 'vcursor-scroll-up)
390 (global-set-key [S-f8] 'vcursor-end-of-buffer)
391
392 (global-set-key [C-f9] 'vcursor-isearch-forward)
393
394 (global-set-key [S-f9] 'vcursor-execute-key)
395 (global-set-key [S-f10] 'vcursor-execute-command)
396
397;;; Partial dictionary of Oemacs key sequences for you to roll your own,
398;;; e.g C-S-up: (global-set-key "\M-[\C-f\M-\C-m" 'vcursor-previous-line)
399;;; Sequence: Sends:
400;;; "\M-[\C-f\M-\C-m" C-S-up
401;;; "\M-[\C-f\M-\C-q" C-S-down
402;;; "\M-[\C-fs" C-S-left
403;;; "\M-[\C-ft" C-S-right
404;;;
405;;; "\M-[\C-fw" C-S-home
406;;; "\M-[\C-b\C-o" S-tab
407;;; "\M-[\C-f\M-\C-r" C-S-insert
408;;; "\M-[\C-fu" C-S-end
409;;; "\M-[\C-f\M-\C-s" C-S-delete
410;;; "\M-[\C-f\M-\C-d" C-S-prior
411;;; "\M-[\C-fv" C-S-next
d49e7392 412;;;
2d6a4733
RS
413;;; "\M-[\C-f^" C-S-f1
414;;; "\M-[\C-f_" C-S-f2
415;;; "\M-[\C-f`" C-S-f3
416;;; "\M-[\C-fa" C-S-f4
417;;; "\M-[\C-fb" C-S-f5
418;;; "\M-[\C-fc" C-S-f6
419;;; "\M-[\C-fd" C-S-f7
420;;; "\M-[\C-fe" C-S-f8
421;;; "\M-[\C-ff" C-S-f9
422;;; "\M-[\C-fg" C-S-f10
423 )
424 (t
425 (global-set-key (vcursor-cs-binding "up") 'vcursor-previous-line)
426 (global-set-key (vcursor-cs-binding "down") 'vcursor-next-line)
427 (global-set-key (vcursor-cs-binding "left") 'vcursor-backward-char)
428 (global-set-key (vcursor-cs-binding "right") 'vcursor-forward-char)
d49e7392 429
2d6a4733
RS
430 (global-set-key (vcursor-cs-binding "return") 'vcursor-disable)
431 (global-set-key (vcursor-cs-binding "insert") 'vcursor-copy)
432 (global-set-key (vcursor-cs-binding "delete") 'vcursor-copy-word)
433 (global-set-key (vcursor-cs-binding "remove") 'vcursor-copy-word)
434 (global-set-key (vcursor-cs-binding "tab") 'vcursor-toggle-copy)
435 (global-set-key (vcursor-cs-binding "backtab") 'vcursor-toggle-copy)
436 (global-set-key (vcursor-cs-binding "home") 'vcursor-beginning-of-buffer)
437 (global-set-key (vcursor-cs-binding "up" t) 'vcursor-beginning-of-buffer)
438 (global-set-key (vcursor-cs-binding "end") 'vcursor-end-of-buffer)
439 (global-set-key (vcursor-cs-binding "down" t) 'vcursor-end-of-buffer)
440 (global-set-key (vcursor-cs-binding "prior") 'vcursor-scroll-down)
441 (global-set-key (vcursor-cs-binding "next") 'vcursor-scroll-up)
d49e7392 442
2d6a4733
RS
443 (global-set-key (vcursor-cs-binding "f6") 'vcursor-other-window)
444 (global-set-key (vcursor-cs-binding "f7") 'vcursor-goto)
445
d49e7392 446 (global-set-key (vcursor-cs-binding "select")
2d6a4733
RS
447 'vcursor-swap-point) ; DEC keyboards
448 (global-set-key (vcursor-cs-binding "tab" t) 'vcursor-swap-point)
449
d49e7392 450 (global-set-key (vcursor-cs-binding "find")
2d6a4733
RS
451 'vcursor-isearch-forward) ; DEC keyboards
452 (global-set-key (vcursor-cs-binding "f8") 'vcursor-isearch-forward)
453
454 (global-set-key (vcursor-cs-binding "left" t) 'vcursor-beginning-of-line)
455 (global-set-key (vcursor-cs-binding "right" t) 'vcursor-end-of-line)
456
457 (global-set-key (vcursor-cs-binding "prior" t) 'vcursor-backward-word)
458 (global-set-key (vcursor-cs-binding "next" t) 'vcursor-forward-word)
459
460 (global-set-key (vcursor-cs-binding "return" t) 'vcursor-copy-line)
461
462 (global-set-key (vcursor-cs-binding "f9") 'vcursor-execute-key)
463 (global-set-key (vcursor-cs-binding "f10") 'vcursor-execute-command)
464 )))
465
466(defcustom vcursor-key-bindings nil
bf078b46 467 "*How to bind keys when vcursor is loaded.
5a7383fd
JB
468If t, guess; if `xterm', use bindings suitable for an X terminal; if
469`oemacs', use bindings which work on a PC with Oemacs. If nil, don't
2d6a4733
RS
470define any key bindings.
471
472Default is nil."
473 :type '(choice (const t) (const nil) (const xterm) (const oemacs))
474 :group 'vcursor
475 :set 'vcursor-bind-keys
476 :version "20.3")
bf078b46 477
4bef9110 478(defcustom vcursor-interpret-input nil
29fddfff
RS
479 "*If non-nil, input from the vcursor is treated as interactive input.
480This will cause text insertion to be much slower. Note that no special
481interpretation of strings is done: \"\C-x\" is a string of four
4bef9110
SE
482characters. The default is simply to copy strings."
483 :type 'boolean
cd32a7ba
DN
484 :group 'vcursor
485 :version "20.3")
29fddfff 486
4bef9110
SE
487(defcustom vcursor-string "**>"
488 "String used to show the vcursor position on dumb terminals."
489 :type 'string
cd32a7ba
DN
490 :group 'vcursor
491 :version "20.3")
29fddfff 492
d49e7392 493(defvar vcursor-overlay nil
bf078b46
RS
494 "Overlay for the virtual cursor.
495It is nil if that is not enabled.")
496
497(defvar vcursor-window nil
498 "Last window to have displayed the virtual cursor.
499See the function `vcursor-find-window' for how this is used.")
500
501(defvar vcursor-last-command nil
502 "Non-nil if last command was a vcursor command.
503The commands `vcursor-copy', `vcursor-relative-move' and the ones for
504scrolling set this. It is used by the `vcursor-auto-disable' code.")
505;; could do some memq-ing with last-command instead, but this will
506;; automatically handle any new commands using the primitives.
507
d49e7392 508(defcustom vcursor-copy-flag nil
4bef9110
SE
509 "*Non-nil means moving vcursor should copy characters moved over to point."
510 :type 'boolean
511 :group 'vcursor)
bf078b46
RS
512
513(defvar vcursor-temp-goal-column nil
514 "Keeps track of temporary goal columns for the virtual cursor.")
515
29fddfff
RS
516(defvar vcursor-use-vcursor-map nil
517 "Non-nil if the vcursor map is mapped directly onto the main keymap.
5a7383fd 518See `vcursor-toggle-vcursor-map'.")
29fddfff
RS
519(make-variable-buffer-local 'vcursor-use-vcursor-map)
520
521(defvar vcursor-map nil "Keymap for vcursor command.")
522(define-prefix-command 'vcursor-map)
523
524(define-key vcursor-map "t" 'vcursor-toggle-vcursor-map)
525
526(define-key vcursor-map "\C-p" 'vcursor-previous-line)
527(define-key vcursor-map "\C-n" 'vcursor-next-line)
528(define-key vcursor-map "\C-b" 'vcursor-backward-char)
529(define-key vcursor-map "\C-f" 'vcursor-forward-char)
530
531(define-key vcursor-map "\r" 'vcursor-disable)
532(define-key vcursor-map " " 'vcursor-copy)
533(define-key vcursor-map "\C-y" 'vcursor-copy-word)
534(define-key vcursor-map "\C-i" 'vcursor-toggle-copy)
535(define-key vcursor-map "<" 'vcursor-beginning-of-buffer)
536(define-key vcursor-map ">" 'vcursor-end-of-buffer)
537(define-key vcursor-map "\M-v" 'vcursor-scroll-down)
538(define-key vcursor-map "\C-v" 'vcursor-scroll-up)
539(define-key vcursor-map "o" 'vcursor-other-window)
540(define-key vcursor-map "g" 'vcursor-goto)
541(define-key vcursor-map "x" 'vcursor-swap-point)
542(define-key vcursor-map "\C-s" 'vcursor-isearch-forward)
543(define-key vcursor-map "\C-r" 'vcursor-isearch-backward)
544(define-key vcursor-map "\C-a" 'vcursor-beginning-of-line)
545(define-key vcursor-map "\C-e" 'vcursor-end-of-line)
546(define-key vcursor-map "\M-w" 'vcursor-forward-word)
547(define-key vcursor-map "\M-b" 'vcursor-backward-word)
548(define-key vcursor-map "\M-l" 'vcursor-copy-line)
549(define-key vcursor-map "c" 'vcursor-compare-windows)
550(define-key vcursor-map "k" 'vcursor-execute-key)
551(define-key vcursor-map "\M-x" 'vcursor-execute-command)
552
2d6a4733
RS
553;; If vcursor-key-bindings is already set on loading, bind the keys now.
554;; This hybrid way of doing it retains compatibility while allowing
555;; customize to work smoothly.
d49e7392 556(if vcursor-key-bindings
2d6a4733 557 (vcursor-bind-keys 'vcursor-key-bindings vcursor-key-bindings))
bf078b46
RS
558
559(defun vcursor-locate ()
560 "Go to the starting point of the virtual cursor.
561If that's disabled, don't go anywhere but don't complain."
562 ;; This is where we go off-mass-shell. Assume there is a
563 ;; save-excursion to get us back to the pole, er, point.
564
565 (and (overlayp vcursor-overlay)
566 (overlay-buffer vcursor-overlay)
567 (set-buffer (overlay-buffer vcursor-overlay))
568 (goto-char (overlay-start vcursor-overlay)))
569 )
570
571(defun vcursor-find-window (&optional not-this new-win this-frame)
572 "Return a suitable window for displaying the virtual cursor.
573This is the first window in cyclic order where the vcursor is visible.
574
575With optional NOT-THIS non-nil never return the current window.
576
577With NEW-WIN non-nil, display the virtual cursor buffer in another
578window if the virtual cursor is not currently visible \(note, however,
5a7383fd 579that this function never changes `window-point'\).
bf078b46
RS
580
581With THIS-FRAME non-nil, don't search other frames for a new window
582\(though if the vcursor is already off-frame then its current window is
583always considered, and the value of `pop-up-frames' is always respected\).
584
585Returns nil if the virtual cursor is not visible anywhere suitable.
586Set `vcursor-window' to the returned value as a side effect."
587
588 ;; The order of priorities (respecting NOT-THIS) is (1)
589 ;; vcursor-window if the virtual cursor is visible there (2) any
590 ;; window displaying the virtual cursor (3) vcursor-window provided
591 ;; it is still displaying the buffer containing the virtual cursor and
592 ;; is not selected (4) any unselected window displaying the vcursor
593 ;; buffer (5) with NEW-WIN, a window selected by display-buffer (so
594 ;; the variables pop-up-windows and pop-up-frames are significant)
595 ;; (6) nil.
596
597 (let ((thiswin (selected-window)) winok winbuf)
598 (save-excursion
599 (vcursor-locate)
600 (or (and (window-live-p vcursor-window)
601 (eq (current-buffer) (window-buffer vcursor-window))
602 (not (and not-this (eq thiswin vcursor-window))))
603 (setq vcursor-window nil))
604 (or (and vcursor-window ; choice 1
605 (pos-visible-in-window-p (point) vcursor-window))
606 (progn
607 (walk-windows
d49e7392 608 (function
bf078b46
RS
609 (lambda (win)
610 (and (not winok)
611 (eq (current-buffer) (window-buffer win))
612 (not (and not-this (eq thiswin win)))
613 (cond
614 ((pos-visible-in-window-p (point) win) (setq winok win))
615 ((eq thiswin win))
616 ((not winbuf) (setq winbuf win))))))
617 nil (not this-frame))
618 (setq vcursor-window
619 (cond
620 (winok) ; choice 2
621 ((and vcursor-window ; choice 3
622 (not (eq thiswin vcursor-window))) vcursor-window)
623 (winbuf) ; choice 4
624 (new-win (display-buffer (current-buffer) t)) ; choice 5
625 (t nil))))))) ; default (choice 6)
626 vcursor-window
627 )
628
629(defun vcursor-toggle-copy (&optional arg nomsg)
630 "Toggle copying to point when the vcursor is moved.
631With a prefix ARG, turn on if non-negative, off if negative.
632Display a message unless optional NOMSG is non-nil."
633 (interactive "P")
634 (setq vcursor-copy-flag
635 (cond ((not arg) (not vcursor-copy-flag))
636 ((< (prefix-numeric-value arg) 0) nil)
637 (t))
638 vcursor-last-command t)
639 (or nomsg (message "Copying from the vcursor is now %s."
640 (if vcursor-copy-flag "on" "off")))
641 )
642
29fddfff 643(defun vcursor-move (pt &optional leave-b leave-w)
bf078b46 644 "Move the virtual cursor to the character to the right of PT.
29fddfff
RS
645PT is an absolute location in the current buffer. With optional
646LEAVE-B, PT is in the same buffer the vcursor is currently in.
bf078b46
RS
647
648If the new virtual cursor location would not be visible, display it in
29fddfff 649another window. With LEAVE-W, use the current `vcursor-window'."
bf078b46
RS
650 ;; this works even if we're on-mass-shell, but usually we won't be.
651
29fddfff
RS
652 (save-excursion
653 (and leave-b (vcursor-check t)
654 (set-buffer (overlay-buffer vcursor-overlay)))
655 (if (eq pt (point-max))
656 (setq pt (1- pt)))
657 (if (vcursor-check t)
658 (move-overlay vcursor-overlay pt (+ pt 1) (current-buffer))
659 (setq vcursor-overlay (make-overlay pt (+ pt 1)))
660 (or window-system
46a610ab 661 (display-color-p)
29fddfff
RS
662 (overlay-put vcursor-overlay 'before-string vcursor-string))
663 (overlay-put vcursor-overlay 'face 'vcursor))
664 (or leave-w (vcursor-find-window nil t))
665 ;; vcursor-window now contains the right buffer
666 (or (pos-visible-in-window-p pt vcursor-window)
667 (set-window-point vcursor-window pt)))
668 )
669
670(defun vcursor-insert (text)
671 "Insert TEXT, respecting `vcursor-interpret-input'."
672 (if vcursor-interpret-input
673 (setq unread-command-events
674 (append (listify-key-sequence text) unread-command-events))
675 (insert text))
bf078b46
RS
676 )
677
5a7383fd
JB
678(defun vcursor-relative-move (func &rest args)
679 "Call FUNC with arbitrary ARGS ... to move the virtual cursor.
bf078b46
RS
680
681This is called by most of the virtual-cursor motion commands."
682 (let (text opoint)
683 (save-excursion
684 (vcursor-locate)
685 (setq opoint (point))
5a7383fd 686 (apply func args)
bf078b46
RS
687 (and (eq opoint (point-max)) (eq opoint (point))
688 (signal 'end-of-buffer nil))
689 (vcursor-move (point))
690 (if vcursor-copy-flag (setq text (buffer-substring opoint (point)))))
29fddfff 691 (if text (vcursor-insert text)))
bf078b46
RS
692 (setq vcursor-last-command t)
693 )
694
695(defun vcursor-goto (&optional arg)
696 "Move the real cursor to the virtual cursor position.
697If the virtual cursor is (or was recently) visible in another window,
698switch to that first. Without a prefix ARG, disable the virtual
699cursor as well."
700
701 (interactive "P")
702 (and (vcursor-find-window) (select-window vcursor-window))
703 (let ((buf (and vcursor-overlay (overlay-buffer vcursor-overlay))))
704 (and buf (not (eq (current-buffer) buf)) (switch-to-buffer buf)))
705 (vcursor-locate)
706 (or arg (vcursor-disable))
707 )
708
709(defun vcursor-swap-point ()
710 "Swap the location of point and that of the virtual cursor.
711
712The virtual cursor window becomes the selected window and the old
713window becomes the virtual cursor window. If the virtual cursor would
714not be visible otherwise, display it in another window."
715
716 (interactive)
717 (let ((buf (current-buffer)) (here (point)) (win (selected-window)))
718 (vcursor-goto) ; will disable the vcursor
719 (save-excursion
720 (set-buffer buf)
721 (setq vcursor-window win)
722 (vcursor-move here)))
723)
724
725(defun vcursor-scroll-up (&optional n)
726 "Scroll up the vcursor window ARG lines or near full screen if none.
727The vcursor will always appear in an unselected window."
728
729 (interactive "P")
730 (vcursor-window-funcall 'scroll-up n)
731)
732
733(defun vcursor-scroll-down (&optional n)
5a7383fd 734 "Scroll down the vcursor window ARG lines or near full screen if none.
bf078b46
RS
735The vcursor will always appear in an unselected window."
736
737 (interactive "P")
738 (vcursor-window-funcall 'scroll-down n)
739 )
740
741(defun vcursor-isearch-forward (&optional rep norecurs)
742 "Perform forward incremental search in the virtual cursor window.
743The virtual cursor is moved to the resulting point; the ordinary
744cursor stays where it was."
745
746 (interactive "P")
747 (vcursor-window-funcall 'isearch-forward rep norecurs)
748 )
749
29fddfff
RS
750(defun vcursor-isearch-backward (&optional rep norecurs)
751 "Perform backward incremental search in the virtual cursor window.
752The virtual cursor is moved to the resulting point; the ordinary
753cursor stays where it was."
754
755 (interactive "P")
756 (vcursor-window-funcall 'isearch-backward rep norecurs)
757 )
758
bf078b46
RS
759(defun vcursor-window-funcall (func &rest args)
760 "Call FUNC with ARGS ... in a virtual cursor window.
761A window other than the currently-selected one will always be used.
762The virtual cursor is moved to the value of point when the function
29fddfff
RS
763returns.
764
765If FUNC is a list, call the car of the list interactively, ignoring
766ARGS. In this case, a new window will not be created if the vcursor
767is visible in the current one."
768;; that's to avoid messing up compatibility with old versions
769;; by introducing a new argument, which would have to come before ARGS.
770
771 (vcursor-find-window (not (and (listp func) (vcursor-check t))) t)
772 (save-excursion
773 (let ((sw (selected-window)) text)
774 ;; We can't use save-window-excursion because that would restore
775 ;; the original display in the window we may want to alter.
776 (unwind-protect
777 (let ((here (point)))
778 (select-window vcursor-window)
779 (vcursor-locate)
780 (if (listp func)
781 (call-interactively (car func))
782 (apply func args))
783 (setq vcursor-window (selected-window))
784 (and vcursor-copy-flag
785 (eq (current-buffer) (overlay-buffer vcursor-overlay))
786 (setq text (buffer-substring here (point))))
787 ;; vcursor-window and the current buffer are definitely
788 ;; right, so make sure vcursor-move doesn't pick others.
789 (vcursor-move (point) nil t))
790 (select-window sw))
791 (if text (vcursor-insert text))))
bf078b46
RS
792 (setq vcursor-last-command t)
793 )
794
5a7383fd
JB
795(defun vcursor-get-char-count (func &rest args)
796 "Apply FUNC to ARGS ... and return the number of characters moved.
797Point is temporarily set to the virtual cursor position before FUNC
798is called.
bf078b46
RS
799
800This is called by most of the virtual-cursor copying commands to find
801out how much to copy."
802
803 (vcursor-check)
804 (save-excursion
805 (set-buffer (overlay-buffer vcursor-overlay))
806 (let ((start (goto-char (overlay-start vcursor-overlay))))
5a7383fd 807 (- (progn (apply func args) (point)) start)))
bf078b46
RS
808 )
809
810;; Make sure the virtual cursor is active. Unless arg is non-nil,
811;; report an error if it is not.
812(defun vcursor-check (&optional arg)
813 (cond
814 ((and (overlayp vcursor-overlay) (overlay-start vcursor-overlay))
815 t)
816 (arg nil)
55535639 817 (t (error "The virtual cursor is not active now")))
bf078b46
RS
818 )
819
820(defun vcursor-disable (&optional arg)
821 "Disable the virtual cursor.
822Next time you use it, it will start from point.
823
824With a positive prefix ARG, the first window in cyclic order
825displaying the virtual cursor (or which was recently displaying the
5a7383fd 826virtual cursor) will be deleted unless it's the selected window.
bf078b46
RS
827
828With a negative prefix argument, enable the virtual cursor: make it
829active at the same point as the real cursor.
830
5a7383fd 831Copying mode is always turned off: the next use of the vcursor will
bf078b46
RS
832not copy text until you turn it on again."
833
834 (interactive "P")
835 (if (overlayp vcursor-overlay)
836 (progn
837 (delete-overlay vcursor-overlay)
838 (setq vcursor-overlay nil)))
839 (cond
840 ((not (vcursor-find-window t)))
841 ((or (not arg) (< (prefix-numeric-value arg) 0)))
842 ((delete-window vcursor-window)))
29fddfff
RS
843 (cond
844 ((and arg (< (prefix-numeric-value arg) 0))
845 (vcursor-move (point))
846 (setq vcursor-window (selected-window)))
847 (vcursor-use-vcursor-map (vcursor-toggle-vcursor-map 0)))
bf078b46
RS
848 (setq vcursor-copy-flag nil)
849 )
850
851(defun vcursor-other-window (n &optional all-frames)
852 "Activate the virtual cursor in another window.
d49e7392 853This is the next window cyclically after one currently showing the
bf078b46
RS
854virtual cursor, or else after the current selected window. If there
855is no other window, the current window is split.
856
5a7383fd 857Arguments N and optional ALL-FRAMES are the same as with `other-window'.
bf078b46
RS
858ALL-FRAMES is also used to decide whether to split the window."
859
860 (interactive "p")
861 (if (if (fboundp 'oemacs-version)
862 (one-window-p nil)
863 (one-window-p nil all-frames))
864 (display-buffer (current-buffer) t))
865 (save-excursion
866 (save-window-excursion
867 ;; We don't use fancy vcursor-find-window trickery, since we're
868 ;; quite happy to have the vcursor cycle back into the current
869 ;; window.
870 (let ((sw (selected-window))
871 (win (vcursor-find-window nil nil (not all-frames))))
872 (if win (select-window win))
873 ;; else start from here
874 (other-window n all-frames)
875 (vcursor-disable -1))))
876 )
877
29fddfff
RS
878;; vcursor-compare-windows is copied from compare-w.el with only
879;; minor modifications; these are too bound up with the function
880;; to make it really useful to call compare-windows itself.
881(defun vcursor-compare-windows (&optional ignore-whitespace)
882 "Compare text in current window with text in window with vcursor.
883Compares the text starting at point in the current window and at the
884vcursor position in the other window, moving over text in each one as
885far as they match.
886
887A prefix argument, if any, means ignore changes in whitespace.
888The variable `compare-windows-whitespace' controls how whitespace is skipped.
889If `compare-ignore-case' is non-nil, changes in case are also ignored."
bf078b46 890 (interactive "P")
29fddfff
RS
891 ;; (vcursor-window-funcall 'compare-windows arg)
892 (require 'compare-w)
893 (let* (p1 p2 maxp1 maxp2 b1 b2 w2
894 success size
895 (opoint1 (point))
896 opoint2
897 (skip-whitespace (if ignore-whitespace
898 compare-windows-whitespace)))
899 (setq p1 (point) b1 (current-buffer))
900 (setq w2 (vcursor-find-window t t))
901 (if (or (eq w2 (selected-window)) (not w2))
902 (error "No other window with vcursor"))
903 (save-excursion
904 (vcursor-locate)
905 (setq p2 (point) b2 (current-buffer)))
906 (setq opoint2 p2)
907 (setq maxp1 (point-max))
908 (save-excursion
909 (set-buffer b2)
910 (setq maxp2 (point-max)))
911
912 (setq success t)
913 (while success
914 (setq success nil)
915 ;; if interrupted, show how far we've gotten
916 (goto-char p1)
917 (vcursor-move p2 t)
918
919 ;; If both buffers have whitespace next to point,
920 ;; optionally skip over it.
921
922 (and skip-whitespace
923 (save-excursion
924 (let (p1a p2a w1 w2 result1 result2)
925 (setq result1
926 (if (stringp skip-whitespace)
927 (compare-windows-skip-whitespace opoint1)
928 (funcall skip-whitespace opoint1)))
929 (setq p1a (point))
930 (set-buffer b2)
931 (goto-char p2)
932 (setq result2
933 (if (stringp skip-whitespace)
934 (compare-windows-skip-whitespace opoint2)
935 (funcall skip-whitespace opoint2)))
936 (setq p2a (point))
937 (if (or (stringp skip-whitespace)
938 (and result1 result2 (eq result1 result2)))
939 (setq p1 p1a
940 p2 p2a)))))
941
942 ;; Try advancing comparing 1000 chars at a time.
943 ;; When that fails, go 500 chars at a time, and so on.
944 (let ((size 1000)
945 success-1
946 (case-fold-search compare-ignore-case))
947 (while (> size 0)
948 (setq success-1 t)
949 ;; Try comparing SIZE chars at a time, repeatedly, till that fails.
950 (while success-1
951 (setq size (min size (- maxp1 p1) (- maxp2 p2)))
952 (setq success-1
953 (and (> size 0)
954 (= 0 (compare-buffer-substrings b2 p2 (+ size p2)
955 b1 p1 (+ size p1)))))
956 (if success-1
957 (setq p1 (+ p1 size) p2 (+ p2 size)
958 success t)))
959 ;; If SIZE chars don't match, try fewer.
960 (setq size (/ size 2)))))
961
962 (goto-char p1)
963 (vcursor-move p2 t)
964 (if (= (point) opoint1)
965 (ding)))
966)
bf078b46
RS
967
968(defun vcursor-next-line (arg)
969 "Move the virtual cursor forward ARG lines."
970 ;; This is next-line rewritten for the vcursor. Maybe it would
971 ;; be easier simply to rewrite line-move.
972 (interactive "p")
973 (let (temporary-goal-column opoint text)
974 (save-excursion
975 (vcursor-locate)
976 (setq temporary-goal-column
977 (if (or (eq last-command 'vcursor-next-line)
978 (eq last-command 'vcursor-previous-line))
979 (progn
980 (setq last-command 'next-line) ; trick line-move
981 vcursor-temp-goal-column)
982 (if (and track-eol (eolp)
983 (or (not (bolp)) (eq last-command 'end-of-line)))
984 9999
985 (current-column)))
986 opoint (point))
987 (line-move arg)
988 (and (eq opoint (point-max)) (eq opoint (point))
989 (signal 'end-of-buffer nil))
990 (if vcursor-copy-flag (setq text (buffer-substring opoint (point))))
991 (vcursor-move (point))
992 (setq vcursor-temp-goal-column temporary-goal-column
993 vcursor-last-command t))
29fddfff 994 (if text (vcursor-insert text)))
bf078b46
RS
995 )
996
997(defun vcursor-previous-line (arg)
998 "Move the virtual cursor back ARG lines."
999 (interactive "p")
1000 (vcursor-next-line (- arg))
1001 )
1002
1003(defun vcursor-forward-char (arg)
1004 "Move the virtual cursor forward ARG characters."
1005 (interactive "p")
1006 (vcursor-relative-move 'forward-char arg)
1007 )
1008
1009(defun vcursor-backward-char (arg)
1010 "Move the virtual cursor backward ARG characters."
1011 (interactive "p")
1012 (vcursor-relative-move 'backward-char arg)
1013 )
1014
1015(defun vcursor-forward-word (arg)
1016 "Move the virtual cursor forward ARG words."
1017 (interactive "p")
1018 (vcursor-relative-move 'forward-word arg)
1019 )
1020
1021(defun vcursor-backward-word (arg)
1022 "Move the virtual cursor backward ARG words."
1023 (interactive "p")
1024 (vcursor-relative-move 'backward-word arg)
1025 )
1026
1027(defun vcursor-beginning-of-line (arg)
1028 "Move the virtual cursor to beginning of its current line.
1029ARG is as for `beginning-of-line'."
1030 (interactive "P")
1031 (vcursor-relative-move 'beginning-of-line
1032 (if arg (prefix-numeric-value arg)))
1033 )
1034
1035(defun vcursor-end-of-line (arg)
1036 "Move the virtual cursor to end of its current line.
1037ARG is as for `end-of-line'."
1038 (interactive "P")
1039 (vcursor-relative-move 'end-of-line
1040 (if arg (prefix-numeric-value arg)))
1041 )
1042
1043(defun vcursor-beginning-of-buffer (&optional arg)
1044 "Move the virtual cursor to the beginning of its buffer.
5a7383fd 1045ARG is as for `beginning-of-buffer'."
bf078b46
RS
1046 (interactive "P")
1047 (vcursor-relative-move
1048 (lambda (arg)
1049 (goto-char (if arg (/ (* arg (- (point-max) (point-min))) 10)
1050 (point-min))))
1051 (if arg (prefix-numeric-value arg)))
1052 )
1053
1054(defun vcursor-end-of-buffer (&optional arg)
1055 "Move the virtual cursor to the end of its buffer.
5a7383fd 1056ARG is as for `end-of-buffer'.
bf078b46
RS
1057
1058Actually, the vcursor is moved to the second from last character or it
1059would be invisible."
1060 (interactive "P")
1061 (vcursor-relative-move
1062 (lambda (arg)
1063 (goto-char (if arg (- (point-max)
1064 (/ (* arg (- (point-max) (point-min))) 10))
1065 (point-max))))
1066 (if arg (prefix-numeric-value arg)))
1067 )
1068
1069(defun vcursor-execute-command (cmd)
1070 "Execute COMMAND for the virtual cursor.
1071COMMAND is called interactively. Not all commands (in fact, only a
1072small subset) are useful."
1073 (interactive "CCommand: ")
29fddfff 1074 (vcursor-window-funcall (list cmd))
bf078b46
RS
1075 )
1076
29fddfff
RS
1077(defun vcursor-execute-key ()
1078 "Read a key sequence and execute the bound command for the virtual cursor.
1079The key sequence is read at the vcursor location. The command found
1080is called interactively, so prefix argument etc. are usable."
1081 (interactive)
1082 (let (cmd)
1083 (save-excursion
1084 ;; We'd like to avoid the display changing when we locate
1085 ;; to the vcursor position and read a key sequence.
1086 (vcursor-find-window (not (vcursor-check t)) t)
1087 (save-window-excursion
1088 (select-window vcursor-window)
1089 (vcursor-locate)
1090 (setq cmd (key-binding (read-key-sequence "Key sequence: ")))))
1091 (vcursor-window-funcall (list cmd)))
bf078b46
RS
1092 )
1093
1094(defun vcursor-copy (arg)
1095 "Copy ARG characters from the virtual cursor position to point."
1096 (interactive "p")
1097 (vcursor-check)
29fddfff 1098 (vcursor-insert
bf078b46
RS
1099 (save-excursion
1100 (set-buffer (overlay-buffer vcursor-overlay))
1101 (let* ((ostart (overlay-start vcursor-overlay))
1102 (end (+ ostart arg)))
1103 (prog1
1104 (buffer-substring ostart end)
1105 (vcursor-move end)))))
1106 (setq vcursor-last-command t)
1107)
1108
1109(defun vcursor-copy-word (arg)
1110 "Copy ARG words from the virtual cursor position to point."
1111 (interactive "p")
1112 (vcursor-copy (vcursor-get-char-count 'forward-word arg))
1113 )
1114
1115(defun vcursor-copy-line (arg)
1116 "Copy up to ARGth line after virtual cursor position.
1117With no argument, copy to the end of the current line.
1118
491a5bba 1119Behavior with regard to newlines is similar (but not identical) to
bf078b46
RS
1120`kill-line'; the main difference is that whitespace at the end of the
1121line is treated like ordinary characters."
1122
1123 (interactive "P")
1124 (let* ((num (prefix-numeric-value arg))
1125 (count (vcursor-get-char-count 'end-of-line num)))
1126 (vcursor-copy (if (or (= count 0) arg) (1+ count) count)))
1127 )
1128
29fddfff
RS
1129(defun vcursor-toggle-vcursor-map (&optional force noredisp)
1130 "Toggle the state of the vcursor key map.
1131When on, the keys defined in it are mapped directly on top of the main
8f2bf1bc 1132keymap, allowing you to move the vcursor with ordinary motion keys.
29fddfff
RS
1133An indication \"!VC\" appears in the mode list. The effect is
1134local to the current buffer.
1135With prefix FORCE, turn on, or off if it is 0.
1136With NOREDISP, don't force redisplay.
1137Disabling the vcursor automatically turns this off."
1138 (interactive "P")
1139 (let ((new (cond ((not force) (not vcursor-use-vcursor-map))
1140 ((eq force 0) nil)
1141 (t))))
1142 (or (eq new vcursor-use-vcursor-map)
1143 (progn
1144 (setq vcursor-use-vcursor-map new)
1145 (or (assq 'vcursor-use-vcursor-map minor-mode-map-alist)
1146 (setq minor-mode-map-alist
1147 (cons (cons 'vcursor-use-vcursor-map vcursor-map)
1148 minor-mode-map-alist)))
1149 (or (assq 'vcursor-use-vcursor-map minor-mode-alist)
1150 (setq minor-mode-alist
1151 (cons (list 'vcursor-use-vcursor-map " !VC")
1152 minor-mode-alist)))
1153 (or noredisp (redraw-display)))))
1154 )
1155
bf078b46
RS
1156(defun vcursor-post-command ()
1157 (and vcursor-auto-disable (not vcursor-last-command)
2d6a4733
RS
1158 vcursor-overlay
1159 (if (eq vcursor-auto-disable t)
1160 (vcursor-disable)
1161 (vcursor-toggle-copy -1 t)))
bf078b46
RS
1162 (setq vcursor-last-command nil)
1163 )
1164
1165(add-hook 'post-command-hook 'vcursor-post-command)
1166
1167(provide 'vcursor)
1168
ab5796a9 1169;;; arch-tag: cdfe1cdc-2c46-4046-88e4-ed57d20f7aca
55535639 1170;;; vcursor.el ends here