emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] trunk r113962: Add rudimentary inferior shell interaction


From: Sam Steingold
Subject: [Emacs-diffs] trunk r113962: Add rudimentary inferior shell interaction
Date: Wed, 21 Aug 2013 01:16:32 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 113962
revision-id: address@hidden
parent: address@hidden
committer: Sam Steingold <address@hidden>
branch nick: trunk
timestamp: Tue 2013-08-20 21:16:27 -0400
message:
  Add rudimentary inferior shell interaction
  * lisp/progmodes/sh-script.el (sh-shell-process): New buffer-local variable.
  (sh-set-shell): Reset it.
  (sh-show-shell, sh-cd-here, sh-send-line-or-region-and-step): New
  commands (bound to C-c C-z, C-c C-d, and C-c C-n).
modified:
  lisp/ChangeLog                 changelog-20091113204419-o5vbwnq5f7feedwu-1432
  lisp/progmodes/sh-script.el    shscript.el-20091113204419-o5vbwnq5f7feedwu-727
=== modified file 'lisp/ChangeLog'
--- a/lisp/ChangeLog    2013-08-20 22:13:29 +0000
+++ b/lisp/ChangeLog    2013-08-21 01:16:27 +0000
@@ -1,3 +1,11 @@
+2013-08-21  Sam Steingold  <address@hidden>
+
+       Add rudimentary inferior shell interaction
+       * progmodes/sh-script.el (sh-shell-process): New buffer-local variable.
+       (sh-set-shell): Reset it.
+       (sh-show-shell, sh-cd-here, sh-send-line-or-region-and-step): New
+       commands (bound to C-c C-z, C-c C-d, and C-c C-n).
+
 2013-08-20  Stefan Monnier  <address@hidden>
 
        * align.el: Use lexical-binding.

=== modified file 'lisp/progmodes/sh-script.el'
--- a/lisp/progmodes/sh-script.el       2013-07-20 19:20:33 +0000
+++ b/lisp/progmodes/sh-script.el       2013-08-21 01:16:27 +0000
@@ -497,6 +497,9 @@
     (define-key map "\C-c+" 'sh-add)
     (define-key map "\C-\M-x" 'sh-execute-region)
     (define-key map "\C-c\C-x" 'executable-interpret)
+    (define-key map "\C-c\C-n" 'sh-send-line-or-region-and-step)
+    (define-key map "\C-c\C-d" 'sh-cd-here)
+    (define-key map "\C-c\C-z" 'sh-show-shell)
 
     (define-key map [remap delete-backward-char]
       'backward-delete-char-untabify)
@@ -1462,6 +1465,61 @@
 frequently editing existing scripts with different styles.")
 
 
+;; inferior shell interaction
+;; TODO: support multiple interactive shells
+(defvar sh-shell-process nil
+  "The inferior shell process for interaction.")
+(make-variable-buffer-local 'sh-shell-process)
+(defun sh-shell-process (force)
+  "Get a shell process for interaction.
+If FORCE is non-nil and no process found, create one."
+  (if (and sh-shell-process (process-live-p sh-shell-process))
+      sh-shell-process
+    (setq sh-shell-process
+          (let ((found nil) proc
+                (procs (process-list)))
+            (while (and (not found) procs
+                        (process-live-p (setq proc (pop procs)))
+                        (process-command proc))
+              (when (string-equal sh-shell (file-name-nondirectory
+                                            (car (process-command proc))))
+                (setq found proc)))
+            (or found
+                (and force
+                     (get-buffer-process
+                      (let ((explicit-shell-file-name sh-shell-file))
+                        (shell)))))))))
+
+(defun sh-show-shell ()
+  "Pop the shell interaction buffer."
+  (interactive)
+  (pop-to-buffer (process-buffer (sh-shell-process t))))
+
+(defun sh-send-text (text)
+  "Send the text to the `sh-shell-process'."
+  (comint-send-string (sh-shell-process t) (concat text "\n")))
+
+(defun sh-cd-here ()
+  "Change directory in the current interaction shell to the current one."
+  (interactive)
+  (sh-send-text (concat "cd " default-directory)))
+
+(defun sh-send-line-or-region-and-step ()
+  "Send the current line to the inferior shell and step to the next line.
+When the region is active, send the region instead."
+  (interactive)
+  (let (from to end)
+    (if (use-region-p)
+        (setq from (region-beginning)
+              to (region-end)
+              end to)
+      (setq from (line-beginning-position)
+            to (line-end-position)
+            end (1+ to)))
+    (sh-send-text (buffer-substring-no-properties from to))
+    (goto-char end)))
+
+
 ;; mode-command and utility functions
 
 ;;;###autoload
@@ -2169,6 +2227,7 @@
     (setq font-lock-set-defaults nil)
     (font-lock-set-defaults)
     (font-lock-fontify-buffer))
+  (setq sh-shell-process nil)
   (run-hooks 'sh-set-shell-hook))
 
 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]