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