Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bravosierrasierra/3b235a8bc016be1a770f994520dcf1f5 to your computer and use it in GitHub Desktop.
Save bravosierrasierra/3b235a8bc016be1a770f994520dcf1f5 to your computer and use it in GitHub Desktop.
vterm copypaste circuit breaker
(setq
vterm-clipboard-warning-max-lines 20
vterm-clipboard-warning-max-chars 256)
;; (define-key vterm-mode-map (kbd "M-c") 'kill-ring-save)
(defun bss/vterm-kill-ring-save ()
(interactive)
(let* ((str "")
(len 0)
(lines 0)
(lines-raw 0)
(msg "!!! Warning, too big cut into clipboard!!!:")
(need-warning nil)
)
(if (not (region-active-p))
(message "!!! Warning: region not selected !!!")
;; (kill-ring-save nil nil t)
(kill-ring-save (region-beginning) (region-end))
(setq
str (substring-no-properties (current-kill 0))
len (length str)
lines-raw (with-temp-buffer (insert str) (count-lines (point-min) (point-max)))
lines (if (null lines-raw) 0 lines-raw))
(if (length> str vterm-clipboard-warning-max-chars )
(setq need-warning t
msg (format "%s Too many symbols (%d > %d)." msg len vterm-clipboard-warning-max-chars )))
(if (> lines vterm-clipboard-warning-max-lines )
(setq need-warning t
msg (format "%s Too many lines (%d > %d)." msg lines vterm-clipboard-warning-max-lines )))
(if (not need-warning)
(message "%d lines / %d chars copied into clipboard" lines len)
(message "%s\nStarts with '%s',\nEnds with '%s'" msg (substring str 0 60) (substring str -60 -1)))
)))
(define-key vterm-mode-map (kbd "M-c") 'bss/vterm-kill-ring-save)
(define-key vterm-mode-map (kbd "s-c") 'bss/vterm-kill-ring-save)
(defun bss/vterm-kill-ring-paste ()
(interactive)
(let* (
(str (substring-no-properties (current-kill 0)))
(len (length str))
(lines-raw (with-temp-buffer (insert str) (count-lines (point-min) (point-max))))
(lines (if (null lines-raw) 0 lines-raw))
(msg "!!! Warning, too big clipboard to paste!!!:")
(need-warning nil)
)
(if (length> str vterm-clipboard-warning-max-chars )
(setq need-warning t
msg (format "%s Too many symbols (%d > %d)." msg len vterm-clipboard-warning-max-chars )))
(if (> lines vterm-clipboard-warning-max-lines )
(setq need-warning t
msg (format "%s Too many lines (%d > %d)." msg lines vterm-clipboard-warning-max-lines )))
(if need-warning
(setq msg (format "%s\nStarts with '%s',\nEnds with '%s'\nConfirm paste" msg (substring str 0 60) (substring str -60 -1))))
(if (not need-warning)
(call-interactively 'vterm-yank)
(if (y-or-n-p msg)
(call-interactively 'vterm-yank)))
)
)
(define-key vterm-mode-map (kbd "M-v") 'bss/vterm-kill-ring-paste)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment