Skip to content

Commit a43f970

Browse files
committed
avoid garbling of certain multibyte characters .
1 parent 532f35a commit a43f970

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

vterm.el

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ Only background is used."
430430
(defvar-local vterm--redraw-immididately nil)
431431
(defvar-local vterm--linenum-remapping nil)
432432
(defvar-local vterm--prompt-tracking-enabled-p nil)
433+
(defvar-local vterm--undecoded-bytes nil)
433434

434435
(defvar vterm-timer-delay 0.1
435436
"Delay for refreshing the buffer after receiving updates from libvterm.
@@ -960,10 +961,35 @@ be set to BUFFER-NAME, otherwise it will be `vterm'"
960961
Then triggers a redraw from the module."
961962
(let ((inhibit-redisplay t)
962963
(inhibit-read-only t)
963-
(buf (process-buffer process)))
964+
(buf (process-buffer process))
965+
(decoded-str))
964966
(when (buffer-live-p buf)
965967
(with-current-buffer buf
966-
(vterm--write-input vterm--term input)
968+
;; Borrowed from term.el
969+
;;
970+
;; Avoid garbling of certain multibyte characters by decoding the string
971+
;; before counting characters. See,
972+
;; https://github.com/akermu/emacs-libvterm/issues/394, and the
973+
;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=1006 (for term.el).
974+
(when vterm--undecoded-bytes
975+
(setq input (concat vterm--undecoded-bytes input))
976+
(setq vterm--undecoded-bytes nil))
977+
(setq decoded-str
978+
(decode-coding-string input locale-coding-system t))
979+
(let ((partial 0)
980+
(count (length decoded-str)))
981+
(while (and (< partial count)
982+
(eq (char-charset (aref decoded-str
983+
(- count 1 partial)))
984+
'eight-bit))
985+
(cl-incf partial))
986+
(when (> count partial 0)
987+
(setq vterm--undecoded-bytes
988+
(substring decoded-str (- partial)))
989+
(setq decoded-str
990+
(substring decoded-str 0 (- partial)))))
991+
992+
(vterm--write-input vterm--term decoded-str)
967993
(vterm--update vterm--term)))))
968994

969995
(defun vterm--sentinel (process event)

0 commit comments

Comments
 (0)