Skip to content

Commit 6eaa60b

Browse files
committed
Add ghostel-yank and ghostel-yank-pop for kill ring cycling
C-y pastes from the kill ring with bracketed paste and sets up for M-y to cycle through older entries. ghostel-yank-pop erases the previous paste with backspaces and sends the next kill ring entry.
1 parent aa9207b commit 6eaa60b

1 file changed

Lines changed: 38 additions & 5 deletions

File tree

ghostel.el

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ These keys pass through to Emacs instead."
234234
(define-key map (kbd "C-p") (lambda () (interactive) (ghostel--send-key "\x10")))
235235
(define-key map (kbd "C-r") (lambda () (interactive) (ghostel--send-key "\x12")))
236236
(define-key map (kbd "C-w") (lambda () (interactive) (ghostel--send-key "\x17")))
237-
(define-key map (kbd "C-y") (lambda () (interactive) (ghostel--send-key "\x19")))
237+
(define-key map (kbd "C-y") #'ghostel-yank)
238+
(define-key map (kbd "M-y") #'ghostel-yank-pop)
238239
(define-key map (kbd "C-z") (lambda () (interactive) (ghostel--send-key "\x1a")))
239240
;; Terminal control via C-c prefix (pass through to Emacs, then handled here)
240241
(define-key map (kbd "C-c C-c") #'ghostel-send-C-c)
@@ -447,17 +448,49 @@ Returns the sequence string, or nil for unknown keys."
447448
(interactive)
448449
(ghostel--send-encoded "d" "ctrl"))
449450

450-
;;; Paste
451+
;;; Paste / yank
452+
453+
(defvar-local ghostel--yank-index 0
454+
"Current kill ring index for `ghostel-yank-pop'.")
455+
456+
(defun ghostel--paste-text (text)
457+
"Send TEXT to the terminal using bracketed paste."
458+
(when (and text ghostel--process (process-live-p ghostel--process))
459+
(process-send-string ghostel--process
460+
(concat "\e[200~" text "\e[201~"))))
451461

452462
(defun ghostel-paste ()
453463
"Paste text from the Emacs kill ring into the terminal.
454464
Uses bracketed paste mode so that shells can distinguish
455465
pasted text from typed input."
456466
(interactive)
457-
(let ((text (current-kill 0)))
458-
(when (and text ghostel--process (process-live-p ghostel--process))
467+
(ghostel--paste-text (current-kill 0)))
468+
469+
(defun ghostel-yank ()
470+
"Yank the most recent kill into the terminal.
471+
Use `ghostel-yank-pop' afterwards to cycle through older kills."
472+
(interactive)
473+
(setq ghostel--yank-index 0)
474+
(ghostel--paste-text (current-kill 0))
475+
(setq this-command 'ghostel-yank))
476+
477+
(defun ghostel-yank-pop ()
478+
"Replace the just-yanked text with the next kill ring entry.
479+
Must be called after `ghostel-yank' or `ghostel-yank-pop'.
480+
Sends backspaces to erase the previous yank, then pastes the next entry."
481+
(interactive)
482+
(unless (memq last-command '(ghostel-yank ghostel-yank-pop))
483+
(user-error "Previous command was not a yank"))
484+
(let* ((prev-text (current-kill ghostel--yank-index t))
485+
(prev-len (length prev-text)))
486+
(setq ghostel--yank-index (1+ ghostel--yank-index))
487+
;; Erase previous paste: send backspaces
488+
(when (and ghostel--process (process-live-p ghostel--process))
459489
(process-send-string ghostel--process
460-
(concat "\e[200~" text "\e[201~")))))
490+
(make-string prev-len ?\x7f)))
491+
;; Paste the next entry
492+
(ghostel--paste-text (current-kill ghostel--yank-index t))
493+
(setq this-command 'ghostel-yank-pop)))
461494

462495
;;; Drag and drop
463496

0 commit comments

Comments
 (0)