Sends a selection or a paragraph to `term-mode'.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Wed, 12 Oct 2022 00:14:04 +0300
changeset 1757 bda24877724a
parent 1756 2495ee9cf84c
child 1758 f02cdc92b560
Sends a selection or a paragraph to `term-mode'.
.emacs-my
--- a/.emacs-my	Mon Jul 25 11:38:25 2022 +0300
+++ b/.emacs-my	Wed Oct 12 00:14:04 2022 +0300
@@ -703,6 +703,26 @@
 
 (setq term-prompt-regexp "^[^#$%>\n]*[#$%>] *")
 
+(defun my-term-send-smart ()
+  "Sends a selection or a paragraph to `term-mode'."
+  (interactive)
+  (let (beg end cmd term)
+    (if (use-region-p)
+        (setq cmd (buffer-substring (region-beginning) (region-end)))
+      (save-excursion
+        (forward-paragraph -1)
+        (setq beg (point))
+        (forward-paragraph 1)
+        (setq end (point))
+        (setq cmd (string-trim (buffer-substring beg end)))))
+    (save-window-excursion              ; term command switches buffers.
+      (term (or explicit-shell-file-name "bash"))
+      (setq term (current-buffer)))
+    (display-buffer term 'display-buffer-use-some-window)
+    (term-simple-send (get-buffer-process term) cmd)))
+
+(global-set-key (kbd "s-t") 'my-term-send-smart)
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 (message "proced")