Skip to content

Commit

Permalink
rewrite vterm-goto-char
Browse files Browse the repository at this point in the history
  • Loading branch information
jixiuf committed Apr 14, 2022
1 parent a2f2286 commit 9a6dbeb
Showing 1 changed file with 57 additions and 21 deletions.
78 changes: 57 additions & 21 deletions vterm.el
Original file line number Diff line number Diff line change
Expand Up @@ -1117,30 +1117,66 @@ It will reset to original position if it can't move there."
(when (and vterm--term
(vterm-cursor-in-command-buffer-p)
(vterm-cursor-in-command-buffer-p pos))
(let ((moved t)
(origin-point (point))
pt cursor-pos succ)
(vterm-reset-cursor-point)
(setq cursor-pos (point))
(setq pt cursor-pos)
(while (and (> pos pt) moved)
(vterm-send-key "<right>" nil nil nil t)
(setq moved (not (= pt (point))))
(setq pt (point)))
(setq pt (point))
(setq moved t)
(while (and (< pos pt) moved)
(vterm-send-key "<left>" nil nil nil t)
(setq moved (not (= pt (point))))
(setq pt (point)))
(setq succ (= pos (point)))
(unless succ
(vterm-goto-char cursor-pos)
(goto-char origin-point))
succ)))
(vterm-reset-cursor-point)
(let ((diff (- pos (point))))
(= diff (vterm--forward-char diff)))))

;;; Internal

(defun vterm--forward-char (&optional n)
"Move point N characters forward (backward if N is negative).
Return the count of moved characeters,
the returned value is negative when backward."
(vterm-reset-cursor-point)
(unless n (setq n 1))
(if (< n 0)
(- (vterm--backward-char (- n)))
(let ((pt (point))
(origin-pt (point))
(count 0)
(moved t))
(while (and moved (< count n))
(vterm-send-key "<right>" nil nil nil t)
(cond
((= (point) (1+ pt))
(setq count (1+ count))
(setq pt (1+ pt)))
((and (= (point) (+ 4 pt))
(looking-back (regexp-quote "^[[C"))) ;escape code for <right>
(dotimes (_ 3) (vterm-send-key "<backspace>" nil nil nil t)) ;;delete "^[[C"
(setq moved nil))
((> (point) (1+ pt)) ;auto suggest
(vterm-send-key "_" nil nil t t) ;undo C-_
(setq moved nil)
)
(t (setq moved nil))))
count)))

(defun vterm--backward-char (&optional n)
"Move point N characters backward.
Return count of moved characeters."
(vterm-reset-cursor-point)
(unless n (setq n 1))
(let ((pt (point))
(origin-pt (point))
(count 0)
(moved t))
(while (and moved (< count n))
(vterm-send-key "<left>" nil nil nil t)
(cond
((= (point) (1- pt))
(setq count (1+ count))
(setq pt (1- pt))
)
((and (= (point) (+ 4 pt))
(looking-back (regexp-quote "^[[D"))) ;escape code for <left>
(dotimes (_ 3) (vterm-send-key "<backspace>" nil nil nil t)) ;;delete "^[[D"
(setq moved nil))
(t (setq moved nil))))
count))

(defun vterm--delete-region(start end)
"A wrapper for `delete-region'."
(funcall vterm--delete-region-function start end))
Expand Down

0 comments on commit 9a6dbeb

Please sign in to comment.