(eval-when-compile (require 'compare-w))
(defgroup vcursor nil
"Manipulate an alternative (\"virtual\") cursor."
:prefix "vcursor-"
:group 'editing)
(defface vcursor
'((((class color)) (:foreground "blue" :background "cyan" :underline t))
(t (:inverse-video t :underline t)))
"Face for the virtual cursor."
:group 'vcursor)
(defcustom vcursor-auto-disable nil
"*If non-nil, disable the virtual cursor after use.
Any non-vcursor command will force `vcursor-disable' to be called.
If non-nil but not t, just make sure copying is toggled off, but don't
disable the vcursor."
:type '(choice (const t) (const nil) (const copy))
:group 'vcursor)
(defcustom vcursor-modifiers (list 'control 'shift)
"*A list of modifiers that are used to define vcursor key bindings."
:type '(repeat symbol)
:group 'vcursor)
(defun vcursor-cs-binding (base &optional meta)
(vector (let ((key (append vcursor-modifiers (list (intern base)))))
(if meta
(cons 'meta key)
key))))
(defun vcursor-bind-keys (var value)
"Alter the value of the variable VAR to VALUE, binding keys as required.
VAR is usually `vcursor-key-bindings'. Normally this function is called
on loading vcursor and from the customize package."
(set var value)
(cond
((not value)) ((or (eq value 'oemacs)
(and (eq value t) (fboundp 'oemacs-version)))
(global-set-key [C-f1] 'vcursor-toggle-copy)
(global-set-key [C-f2] 'vcursor-copy)
(global-set-key [C-f3] 'vcursor-copy-word)
(global-set-key [C-f4] 'vcursor-copy-line)
(global-set-key [S-f1] 'vcursor-disable)
(global-set-key [S-f2] 'vcursor-other-window)
(global-set-key [S-f3] 'vcursor-goto)
(global-set-key [S-f4] 'vcursor-swap-point)
(global-set-key [C-f5] 'vcursor-backward-char)
(global-set-key [C-f6] 'vcursor-previous-line)
(global-set-key [C-f7] 'vcursor-next-line)
(global-set-key [C-f8] 'vcursor-forward-char)
(global-set-key [M-f5] 'vcursor-beginning-of-line)
(global-set-key [M-f6] 'vcursor-backward-word)
(global-set-key [M-f6] 'vcursor-forward-word)
(global-set-key [M-f8] 'vcursor-end-of-line)
(global-set-key [S-f5] 'vcursor-beginning-of-buffer)
(global-set-key [S-f6] 'vcursor-scroll-down)
(global-set-key [S-f7] 'vcursor-scroll-up)
(global-set-key [S-f8] 'vcursor-end-of-buffer)
(global-set-key [C-f9] 'vcursor-isearch-forward)
(global-set-key [S-f9] 'vcursor-execute-key)
(global-set-key [S-f10] 'vcursor-execute-command)
)
(t
(global-set-key (vcursor-cs-binding "up") 'vcursor-previous-line)
(global-set-key (vcursor-cs-binding "down") 'vcursor-next-line)
(global-set-key (vcursor-cs-binding "left") 'vcursor-backward-char)
(global-set-key (vcursor-cs-binding "right") 'vcursor-forward-char)
(global-set-key (vcursor-cs-binding "return") 'vcursor-disable)
(global-set-key (vcursor-cs-binding "insert") 'vcursor-copy)
(global-set-key (vcursor-cs-binding "delete") 'vcursor-copy-word)
(global-set-key (vcursor-cs-binding "remove") 'vcursor-copy-word)
(global-set-key (vcursor-cs-binding "tab") 'vcursor-toggle-copy)
(global-set-key (vcursor-cs-binding "backtab") 'vcursor-toggle-copy)
(global-set-key (vcursor-cs-binding "home") 'vcursor-beginning-of-buffer)
(global-set-key (vcursor-cs-binding "up" t) 'vcursor-beginning-of-buffer)
(global-set-key (vcursor-cs-binding "end") 'vcursor-end-of-buffer)
(global-set-key (vcursor-cs-binding "down" t) 'vcursor-end-of-buffer)
(global-set-key (vcursor-cs-binding "prior") 'vcursor-scroll-down)
(global-set-key (vcursor-cs-binding "next") 'vcursor-scroll-up)
(global-set-key (vcursor-cs-binding "f6") 'vcursor-other-window)
(global-set-key (vcursor-cs-binding "f7") 'vcursor-goto)
(global-set-key (vcursor-cs-binding "select")
'vcursor-swap-point) (global-set-key (vcursor-cs-binding "tab" t) 'vcursor-swap-point)
(global-set-key (vcursor-cs-binding "find")
'vcursor-isearch-forward) (global-set-key (vcursor-cs-binding "f8") 'vcursor-isearch-forward)
(global-set-key (vcursor-cs-binding "left" t) 'vcursor-beginning-of-line)
(global-set-key (vcursor-cs-binding "right" t) 'vcursor-end-of-line)
(global-set-key (vcursor-cs-binding "prior" t) 'vcursor-backward-word)
(global-set-key (vcursor-cs-binding "next" t) 'vcursor-forward-word)
(global-set-key (vcursor-cs-binding "return" t) 'vcursor-copy-line)
(global-set-key (vcursor-cs-binding "f9") 'vcursor-execute-key)
(global-set-key (vcursor-cs-binding "f10") 'vcursor-execute-command)
)))
(defcustom vcursor-key-bindings nil
"*How to bind keys when vcursor is loaded.
If t, guess; if `xterm', use bindings suitable for an X terminal; if
`oemacs', use bindings which work on a PC with Oemacs. If nil, don't
define any key bindings.
Default is nil."
:type '(choice (const t) (const nil) (const xterm) (const oemacs))
:group 'vcursor
:set 'vcursor-bind-keys
:version "20.3")
(defcustom vcursor-interpret-input nil
"*If non-nil, input from the vcursor is treated as interactive input.
This will cause text insertion to be much slower. Note that no special
interpretation of strings is done: \"\C-x\" is a string of four
characters. The default is simply to copy strings."
:type 'boolean
:group 'vcursor
:version "20.3")
(defcustom vcursor-string "**>"
"String used to show the vcursor position on dumb terminals."
:type 'string
:group 'vcursor
:version "20.3")
(defvar vcursor-overlay nil
"Overlay for the virtual cursor.
It is nil if that is not enabled.")
(defvar vcursor-window nil
"Last window to have displayed the virtual cursor.
See the function `vcursor-find-window' for how this is used.")
(defvar vcursor-last-command nil
"Non-nil if last command was a vcursor command.
The commands `vcursor-copy', `vcursor-relative-move' and the ones for
scrolling set this. It is used by the `vcursor-auto-disable' code.")
(defcustom vcursor-copy-flag nil
"*Non-nil means moving vcursor should copy characters moved over to point."
:type 'boolean
:group 'vcursor)
(defvar vcursor-temp-goal-column nil
"Keeps track of temporary goal columns for the virtual cursor.")
(defvar vcursor-use-vcursor-map nil
"Non-nil if the vcursor map is mapped directly onto the main keymap.
See `vcursor-toggle-vcursor-map'.")
(make-variable-buffer-local 'vcursor-use-vcursor-map)
(defvar vcursor-map nil "Keymap for vcursor command.")
(define-prefix-command 'vcursor-map)
(define-key vcursor-map "t" 'vcursor-toggle-vcursor-map)
(define-key vcursor-map "\C-p" 'vcursor-previous-line)
(define-key vcursor-map "\C-n" 'vcursor-next-line)
(define-key vcursor-map "\C-b" 'vcursor-backward-char)
(define-key vcursor-map "\C-f" 'vcursor-forward-char)
(define-key vcursor-map "\r" 'vcursor-disable)
(define-key vcursor-map " " 'vcursor-copy)
(define-key vcursor-map "\C-y" 'vcursor-copy-word)
(define-key vcursor-map "\C-i" 'vcursor-toggle-copy)
(define-key vcursor-map "<" 'vcursor-beginning-of-buffer)
(define-key vcursor-map ">" 'vcursor-end-of-buffer)
(define-key vcursor-map "\M-v" 'vcursor-scroll-down)
(define-key vcursor-map "\C-v" 'vcursor-scroll-up)
(define-key vcursor-map "o" 'vcursor-other-window)
(define-key vcursor-map "g" 'vcursor-goto)
(define-key vcursor-map "x" 'vcursor-swap-point)
(define-key vcursor-map "\C-s" 'vcursor-isearch-forward)
(define-key vcursor-map "\C-r" 'vcursor-isearch-backward)
(define-key vcursor-map "\C-a" 'vcursor-beginning-of-line)
(define-key vcursor-map "\C-e" 'vcursor-end-of-line)
(define-key vcursor-map "\M-w" 'vcursor-forward-word)
(define-key vcursor-map "\M-b" 'vcursor-backward-word)
(define-key vcursor-map "\M-l" 'vcursor-copy-line)
(define-key vcursor-map "c" 'vcursor-compare-windows)
(define-key vcursor-map "k" 'vcursor-execute-key)
(define-key vcursor-map "\M-x" 'vcursor-execute-command)
(if vcursor-key-bindings
(vcursor-bind-keys 'vcursor-key-bindings vcursor-key-bindings))
(defun vcursor-locate ()
"Go to the starting point of the virtual cursor.
If that's disabled, don't go anywhere but don't complain."
(and (overlayp vcursor-overlay)
(overlay-buffer vcursor-overlay)
(set-buffer (overlay-buffer vcursor-overlay))
(goto-char (overlay-start vcursor-overlay)))
)
(defun vcursor-find-window (&optional not-this new-win this-frame)
"Return a suitable window for displaying the virtual cursor.
This is the first window in cyclic order where the vcursor is visible.
With optional NOT-THIS non-nil never return the current window.
With NEW-WIN non-nil, display the virtual cursor buffer in another
window if the virtual cursor is not currently visible \(note, however,
that this function never changes `window-point'\).
With THIS-FRAME non-nil, don't search other frames for a new window
\(though if the vcursor is already off-frame then its current window is
always considered, and the value of `pop-up-frames' is always respected\).
Returns nil if the virtual cursor is not visible anywhere suitable.
Set `vcursor-window' to the returned value as a side effect."
(let ((thiswin (selected-window)) winok winbuf)
(save-excursion
(vcursor-locate)
(or (and (window-live-p vcursor-window)
(eq (current-buffer) (window-buffer vcursor-window))
(not (and not-this (eq thiswin vcursor-window))))
(setq vcursor-window nil))
(or (and vcursor-window (pos-visible-in-window-p (point) vcursor-window))
(progn
(walk-windows
(function
(lambda (win)
(and (not winok)
(eq (current-buffer) (window-buffer win))
(not (and not-this (eq thiswin win)))
(cond
((pos-visible-in-window-p (point) win) (setq winok win))
((eq thiswin win))
((not winbuf) (setq winbuf win))))))
nil (not this-frame))
(setq vcursor-window
(cond
(winok) ((and vcursor-window (not (eq thiswin vcursor-window))) vcursor-window)
(winbuf) (new-win (display-buffer (current-buffer) t)) (t nil))))))) vcursor-window
)
(defun vcursor-toggle-copy (&optional arg nomsg)
"Toggle copying to point when the vcursor is moved.
With a prefix ARG, turn on if non-negative, off if negative.
Display a message unless optional NOMSG is non-nil."
(interactive "P")
(setq vcursor-copy-flag
(cond ((not arg) (not vcursor-copy-flag))
((< (prefix-numeric-value arg) 0) nil)
(t))
vcursor-last-command t)
(or nomsg (message "Copying from the vcursor is now %s."
(if vcursor-copy-flag "on" "off")))
)
(defun vcursor-move (pt &optional leave-b leave-w)
"Move the virtual cursor to the character to the right of PT.
PT is an absolute location in the current buffer. With optional
LEAVE-B, PT is in the same buffer the vcursor is currently in.
If the new virtual cursor location would not be visible, display it in
another window. With LEAVE-W, use the current `vcursor-window'."
(save-excursion
(and leave-b (vcursor-check t)
(set-buffer (overlay-buffer vcursor-overlay)))
(if (eq pt (point-max))
(setq pt (1- pt)))
(if (vcursor-check t)
(move-overlay vcursor-overlay pt (+ pt 1) (current-buffer))
(setq vcursor-overlay (make-overlay pt (+ pt 1)))
(or window-system
(display-color-p)
(overlay-put vcursor-overlay 'before-string vcursor-string))
(overlay-put vcursor-overlay 'face 'vcursor))
(or leave-w (vcursor-find-window nil t))
(or (pos-visible-in-window-p pt vcursor-window)
(set-window-point vcursor-window pt)))
)
(defun vcursor-insert (text)
"Insert TEXT, respecting `vcursor-interpret-input'."
(if vcursor-interpret-input
(setq unread-command-events
(append (listify-key-sequence text) unread-command-events))
(insert text))
)
(defun vcursor-relative-move (func &rest args)
"Call FUNC with arbitrary ARGS ... to move the virtual cursor.
This is called by most of the virtual-cursor motion commands."
(let (text opoint)
(save-excursion
(vcursor-locate)
(setq opoint (point))
(apply func args)
(and (eq opoint (point-max)) (eq opoint (point))
(signal 'end-of-buffer nil))
(vcursor-move (point))
(if vcursor-copy-flag (setq text (buffer-substring opoint (point)))))
(if text (vcursor-insert text)))
(setq vcursor-last-command t)
)
(defun vcursor-goto (&optional arg)
"Move the real cursor to the virtual cursor position.
If the virtual cursor is (or was recently) visible in another window,
switch to that first. Without a prefix ARG, disable the virtual
cursor as well."
(interactive "P")
(and (vcursor-find-window) (select-window vcursor-window))
(let ((buf (and vcursor-overlay (overlay-buffer vcursor-overlay))))
(and buf (not (eq (current-buffer) buf)) (switch-to-buffer buf)))
(vcursor-locate)
(or arg (vcursor-disable))
)
(defun vcursor-swap-point ()
"Swap the location of point and that of the virtual cursor.
The virtual cursor window becomes the selected window and the old
window becomes the virtual cursor window. If the virtual cursor would
not be visible otherwise, display it in another window."
(interactive)
(let ((buf (current-buffer)) (here (point)) (win (selected-window)))
(vcursor-goto) (save-excursion
(set-buffer buf)
(setq vcursor-window win)
(vcursor-move here)))
)
(defun vcursor-scroll-up (&optional n)
"Scroll up the vcursor window ARG lines or near full screen if none.
The vcursor will always appear in an unselected window."
(interactive "P")
(vcursor-window-funcall 'scroll-up n)
)
(defun vcursor-scroll-down (&optional n)
"Scroll down the vcursor window ARG lines or near full screen if none.
The vcursor will always appear in an unselected window."
(interactive "P")
(vcursor-window-funcall 'scroll-down n)
)
(defun vcursor-isearch-forward (&optional rep norecurs)
"Perform forward incremental search in the virtual cursor window.
The virtual cursor is moved to the resulting point; the ordinary
cursor stays where it was."
(interactive "P")
(vcursor-window-funcall 'isearch-forward rep norecurs)
)
(defun vcursor-isearch-backward (&optional rep norecurs)
"Perform backward incremental search in the virtual cursor window.
The virtual cursor is moved to the resulting point; the ordinary
cursor stays where it was."
(interactive "P")
(vcursor-window-funcall 'isearch-backward rep norecurs)
)
(defun vcursor-window-funcall (func &rest args)
"Call FUNC with ARGS ... in a virtual cursor window.
A window other than the currently-selected one will always be used.
The virtual cursor is moved to the value of point when the function
returns.
If FUNC is a list, call the car of the list interactively, ignoring
ARGS. In this case, a new window will not be created if the vcursor
is visible in the current one."
(vcursor-find-window (not (and (listp func) (vcursor-check t))) t)
(save-excursion
(let ((sw (selected-window)) text)
(unwind-protect
(let ((here (point)))
(select-window vcursor-window)
(vcursor-locate)
(if (listp func)
(call-interactively (car func))
(apply func args))
(setq vcursor-window (selected-window))
(and vcursor-copy-flag
(eq (current-buffer) (overlay-buffer vcursor-overlay))
(setq text (buffer-substring here (point))))
(vcursor-move (point) nil t))
(select-window sw))
(if text (vcursor-insert text))))
(setq vcursor-last-command t)
)
(defun vcursor-get-char-count (func &rest args)
"Apply FUNC to ARGS ... and return the number of characters moved.
Point is temporarily set to the virtual cursor position before FUNC
is called.
This is called by most of the virtual-cursor copying commands to find
out how much to copy."
(vcursor-check)
(save-excursion
(set-buffer (overlay-buffer vcursor-overlay))
(let ((start (goto-char (overlay-start vcursor-overlay))))
(- (progn (apply func args) (point)) start)))
)
(defun vcursor-check (&optional arg)
(cond
((and (overlayp vcursor-overlay) (overlay-start vcursor-overlay))
t)
(arg nil)
(t (error "The virtual cursor is not active now")))
)
(defun vcursor-disable (&optional arg)
"Disable the virtual cursor.
Next time you use it, it will start from point.
With a positive prefix ARG, the first window in cyclic order
displaying the virtual cursor (or which was recently displaying the
virtual cursor) will be deleted unless it's the selected window.
With a negative prefix argument, enable the virtual cursor: make it
active at the same point as the real cursor.
Copying mode is always turned off: the next use of the vcursor will
not copy text until you turn it on again."
(interactive "P")
(if (overlayp vcursor-overlay)
(progn
(delete-overlay vcursor-overlay)
(setq vcursor-overlay nil)))
(cond
((not (vcursor-find-window t)))
((or (not arg) (< (prefix-numeric-value arg) 0)))
((delete-window vcursor-window)))
(cond
((and arg (< (prefix-numeric-value arg) 0))
(vcursor-move (point))
(setq vcursor-window (selected-window)))
(vcursor-use-vcursor-map (vcursor-toggle-vcursor-map 0)))
(setq vcursor-copy-flag nil)
)
(defun vcursor-other-window (n &optional all-frames)
"Activate the virtual cursor in another window.
This is the next window cyclically after one currently showing the
virtual cursor, or else after the current selected window. If there
is no other window, the current window is split.
Arguments N and optional ALL-FRAMES are the same as with `other-window'.
ALL-FRAMES is also used to decide whether to split the window."
(interactive "p")
(if (if (fboundp 'oemacs-version)
(one-window-p nil)
(one-window-p nil all-frames))
(display-buffer (current-buffer) t))
(save-excursion
(save-window-excursion
(let ((sw (selected-window))
(win (vcursor-find-window nil nil (not all-frames))))
(if win (select-window win))
(other-window n all-frames)
(vcursor-disable -1))))
)
(defun vcursor-compare-windows (&optional ignore-whitespace)
"Compare text in current window with text in window with vcursor.
Compares the text starting at point in the current window and at the
vcursor position in the other window, moving over text in each one as
far as they match.
A prefix argument, if any, means ignore changes in whitespace.
The variable `compare-windows-whitespace' controls how whitespace is skipped.
If `compare-ignore-case' is non-nil, changes in case are also ignored."
(interactive "P")
(require 'compare-w)
(let* (p1 p2 maxp1 maxp2 b1 b2 w2
success size
(opoint1 (point))
opoint2
(skip-whitespace (if ignore-whitespace
compare-windows-whitespace)))
(setq p1 (point) b1 (current-buffer))
(setq w2 (vcursor-find-window t t))
(if (or (eq w2 (selected-window)) (not w2))
(error "No other window with vcursor"))
(save-excursion
(vcursor-locate)
(setq p2 (point) b2 (current-buffer)))
(setq opoint2 p2)
(setq maxp1 (point-max))
(save-excursion
(set-buffer b2)
(setq maxp2 (point-max)))
(setq success t)
(while success
(setq success nil)
(goto-char p1)
(vcursor-move p2 t)
(and skip-whitespace
(save-excursion
(let (p1a p2a w1 w2 result1 result2)
(setq result1
(if (stringp skip-whitespace)
(compare-windows-skip-whitespace opoint1)
(funcall skip-whitespace opoint1)))
(setq p1a (point))
(set-buffer b2)
(goto-char p2)
(setq result2
(if (stringp skip-whitespace)
(compare-windows-skip-whitespace opoint2)
(funcall skip-whitespace opoint2)))
(setq p2a (point))
(if (or (stringp skip-whitespace)
(and result1 result2 (eq result1 result2)))
(setq p1 p1a
p2 p2a)))))
(let ((size 1000)
success-1
(case-fold-search compare-ignore-case))
(while (> size 0)
(setq success-1 t)
(while success-1
(setq size (min size (- maxp1 p1) (- maxp2 p2)))
(setq success-1
(and (> size 0)
(= 0 (compare-buffer-substrings b2 p2 (+ size p2)
b1 p1 (+ size p1)))))
(if success-1
(setq p1 (+ p1 size) p2 (+ p2 size)
success t)))
(setq size (/ size 2)))))
(goto-char p1)
(vcursor-move p2 t)
(if (= (point) opoint1)
(ding)))
)
(defun vcursor-next-line (arg)
"Move the virtual cursor forward ARG lines."
(interactive "p")
(let (temporary-goal-column opoint text)
(save-excursion
(vcursor-locate)
(setq temporary-goal-column
(if (or (eq last-command 'vcursor-next-line)
(eq last-command 'vcursor-previous-line))
(progn
(setq last-command 'next-line) vcursor-temp-goal-column)
(if (and track-eol (eolp)
(or (not (bolp)) (eq last-command 'end-of-line)))
9999
(current-column)))
opoint (point))
(line-move arg)
(and (eq opoint (point-max)) (eq opoint (point))
(signal 'end-of-buffer nil))
(if vcursor-copy-flag (setq text (buffer-substring opoint (point))))
(vcursor-move (point))
(setq vcursor-temp-goal-column temporary-goal-column
vcursor-last-command t))
(if text (vcursor-insert text)))
)
(defun vcursor-previous-line (arg)
"Move the virtual cursor back ARG lines."
(interactive "p")
(vcursor-next-line (- arg))
)
(defun vcursor-forward-char (arg)
"Move the virtual cursor forward ARG characters."
(interactive "p")
(vcursor-relative-move 'forward-char arg)
)
(defun vcursor-backward-char (arg)
"Move the virtual cursor backward ARG characters."
(interactive "p")
(vcursor-relative-move 'backward-char arg)
)
(defun vcursor-forward-word (arg)
"Move the virtual cursor forward ARG words."
(interactive "p")
(vcursor-relative-move 'forward-word arg)
)
(defun vcursor-backward-word (arg)
"Move the virtual cursor backward ARG words."
(interactive "p")
(vcursor-relative-move 'backward-word arg)
)
(defun vcursor-beginning-of-line (arg)
"Move the virtual cursor to beginning of its current line.
ARG is as for `beginning-of-line'."
(interactive "P")
(vcursor-relative-move 'beginning-of-line
(if arg (prefix-numeric-value arg)))
)
(defun vcursor-end-of-line (arg)
"Move the virtual cursor to end of its current line.
ARG is as for `end-of-line'."
(interactive "P")
(vcursor-relative-move 'end-of-line
(if arg (prefix-numeric-value arg)))
)
(defun vcursor-beginning-of-buffer (&optional arg)
"Move the virtual cursor to the beginning of its buffer.
ARG is as for `beginning-of-buffer'."
(interactive "P")
(vcursor-relative-move
(lambda (arg)
(goto-char (if arg (/ (* arg (- (point-max) (point-min))) 10)
(point-min))))
(if arg (prefix-numeric-value arg)))
)
(defun vcursor-end-of-buffer (&optional arg)
"Move the virtual cursor to the end of its buffer.
ARG is as for `end-of-buffer'.
Actually, the vcursor is moved to the second from last character or it
would be invisible."
(interactive "P")
(vcursor-relative-move
(lambda (arg)
(goto-char (if arg (- (point-max)
(/ (* arg (- (point-max) (point-min))) 10))
(point-max))))
(if arg (prefix-numeric-value arg)))
)
(defun vcursor-execute-command (cmd)
"Execute COMMAND for the virtual cursor.
COMMAND is called interactively. Not all commands (in fact, only a
small subset) are useful."
(interactive "CCommand: ")
(vcursor-window-funcall (list cmd))
)
(defun vcursor-execute-key ()
"Read a key sequence and execute the bound command for the virtual cursor.
The key sequence is read at the vcursor location. The command found
is called interactively, so prefix argument etc. are usable."
(interactive)
(let (cmd)
(save-excursion
(vcursor-find-window (not (vcursor-check t)) t)
(save-window-excursion
(select-window vcursor-window)
(vcursor-locate)
(setq cmd (key-binding (read-key-sequence "Key sequence: ")))))
(vcursor-window-funcall (list cmd)))
)
(defun vcursor-copy (arg)
"Copy ARG characters from the virtual cursor position to point."
(interactive "p")
(vcursor-check)
(vcursor-insert
(save-excursion
(set-buffer (overlay-buffer vcursor-overlay))
(let* ((ostart (overlay-start vcursor-overlay))
(end (+ ostart arg)))
(prog1
(buffer-substring ostart end)
(vcursor-move end)))))
(setq vcursor-last-command t)
)
(defun vcursor-copy-word (arg)
"Copy ARG words from the virtual cursor position to point."
(interactive "p")
(vcursor-copy (vcursor-get-char-count 'forward-word arg))
)
(defun vcursor-copy-line (arg)
"Copy up to ARGth line after virtual cursor position.
With no argument, copy to the end of the current line.
Behavior with regard to newlines is similar (but not identical) to
`kill-line'; the main difference is that whitespace at the end of the
line is treated like ordinary characters."
(interactive "P")
(let* ((num (prefix-numeric-value arg))
(count (vcursor-get-char-count 'end-of-line num)))
(vcursor-copy (if (or (= count 0) arg) (1+ count) count)))
)
(defun vcursor-toggle-vcursor-map (&optional force noredisp)
"Toggle the state of the vcursor key map.
When on, the keys defined in it are mapped directly on top of the main
keymap, allowing you to move the vcursor with ordinary motion keys.
An indication \"!VC\" appears in the mode list. The effect is
local to the current buffer.
With prefix FORCE, turn on, or off if it is 0.
With NOREDISP, don't force redisplay.
Disabling the vcursor automatically turns this off."
(interactive "P")
(let ((new (cond ((not force) (not vcursor-use-vcursor-map))
((eq force 0) nil)
(t))))
(or (eq new vcursor-use-vcursor-map)
(progn
(setq vcursor-use-vcursor-map new)
(or (assq 'vcursor-use-vcursor-map minor-mode-map-alist)
(setq minor-mode-map-alist
(cons (cons 'vcursor-use-vcursor-map vcursor-map)
minor-mode-map-alist)))
(or (assq 'vcursor-use-vcursor-map minor-mode-alist)
(setq minor-mode-alist
(cons (list 'vcursor-use-vcursor-map " !VC")
minor-mode-alist)))
(or noredisp (redraw-display)))))
)
(defun vcursor-post-command ()
(and vcursor-auto-disable (not vcursor-last-command)
vcursor-overlay
(if (eq vcursor-auto-disable t)
(vcursor-disable)
(vcursor-toggle-copy -1 t)))
(setq vcursor-last-command nil)
)
(add-hook 'post-command-hook 'vcursor-post-command)
(provide 'vcursor)