From 144b6518b0c36692198092c047325f37e8eb99f3 Mon Sep 17 00:00:00 2001 From: Stephane Zermatten Date: Mon, 30 Sep 2024 15:45:49 +0300 Subject: [PATCH] Limit right prompt detection logic to the prompt. Before this change, output containing tabs or otherwise skip characters would trigger the right prompt detection logic, which would then attempt to remove from the yanked text what it sees as part of the prompt. This change restricts right prompt detection to the prompt, the section with bracketed-paste turned on, so that command output is left alone. Fixes #21 --- mistty-term.el | 26 ++++++++++++++------------ test/mistty-test.el | 11 +++++++++++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/mistty-term.el b/mistty-term.el index 8a4c893..caf7591 100644 --- a/mistty-term.el +++ b/mistty-term.el @@ -392,8 +392,7 @@ mistty-reverse-input-decode-map.el to `xterm-function-map'.") This variable evaluates to true when bracketed paste is turned on by the command that controls, to false otherwise. -This variable is available in the work buffer.") - +This variable is available in both the work and term buffers.") (defvar-local mistty--term-properties-to-add-alist nil "An alist of id to text properties to add to the term buffer. @@ -464,11 +463,13 @@ into `mistty-bracketed-paste' in the buffer WORK-BUFFER. (unless (eq ?\n (char-before (point))) (add-text-properties (mistty--bol (point)) (point) props)) (mistty-register-text-properties 'mistty-bracketed-paste props)) + (setq mistty-bracketed-paste t) (mistty--with-live-buffer work-buffer (setq mistty-bracketed-paste t))) ((equal ext "[?2004l") ; disable bracketed paste (term-emulate-terminal proc (substring str start seq-end)) (mistty-unregister-text-properties 'mistty-bracketed-paste) + (setq mistty-bracketed-paste nil) (mistty--with-live-buffer work-buffer (setq mistty-bracketed-paste nil))) ((equal ext "[?25h") ; make cursor visible @@ -596,22 +597,23 @@ BEG and END define the region that was modified." (put-text-property pos fake-nl-end 'yank-handler '(nil "" nil nil)) (setq beg fake-nl-end))) - ;; Detect and mark right prompts. - (let ((bol (mistty--bol beg)) - (eol (mistty--eol beg))) - (when (and (> beg bol) - (<= end eol) - (get-text-property (1- beg) 'mistty-skip) - (not (get-text-property bol 'mistty-skip))) - (add-text-properties - beg end '(mistty-skip t mistty-right-prompt t yank-handler (nil "" nil nil)))))) + (when mistty-bracketed-paste + ;; Detect and mark right prompts. + (let ((bol (mistty--bol beg)) + (eol (mistty--eol beg))) + (when (and (> beg bol) + (<= end eol) + (get-text-property (1- beg) 'mistty-skip) + (not (get-text-property bol 'mistty-skip))) + (add-text-properties + beg end '(mistty-skip t mistty-right-prompt t yank-handler (nil "" nil nil))))))) (defun mistty--around-move-to-column (orig-fun &rest args) "Add property \\='mistty-skip t to spaces added when just moving. ORIG-FUN is the original `move-to-column' function that's being advised and ARGS are its arguments." - (if (eq 'term-mode major-mode) + (if (and (eq 'term-mode major-mode) mistty-bracketed-paste) (let ((initial-end (line-end-position))) (apply orig-fun args) (when (> (point) initial-end) diff --git a/test/mistty-test.el b/test/mistty-test.el index ed194da..3782b1e 100644 --- a/test/mistty-test.el +++ b/test/mistty-test.el @@ -2823,6 +2823,17 @@ (yank) (should (equal "$ echo hello" (mistty-test-content)))))) +(ert-deftest mistty-test-not-right-prompt-yank-in-output () + (mistty-with-test-buffer (:shell fish) + (mistty-send-string "printf 'foo\\t") ; \t would confuse mistty-send-text + (mistty-send-text "bar\n'") + (let ((start (point))) + (mistty-send-and-wait-for-prompt) + (copy-region-as-kill (mistty--bol start 2) (mistty--eol start 2))) + (with-temp-buffer + (yank) + (should (equal "foo bar" (mistty-test-content)))))) + (ert-deftest mistty-test-fish-right-prompt-skip-empty-spaces-at-prompt () (mistty-with-test-buffer (:shell fish :selected t) (mistty-setup-fish-right-prompt)