vcursor.el 45 KB

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