vcursor.el 45 KB

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