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