Browse Source

shell commands are working

Eric Schulte 16 years ago
parent
commit
e2d3cc7830
2 changed files with 41 additions and 16 deletions
  1. 23 13
      lisp/langs/org-babel-sh.el
  2. 18 3
      org-babel.org

+ 23 - 13
lisp/langs/org-babel-sh.el

@@ -120,7 +120,8 @@ then create.  Return the initialized session."
       session)))
 
 (defun org-babel-sh-initiate-session (&optional session)
-  (org-babel-sh-session-buffer (org-babel-sh-initiate-session-by-key session)))
+  (unless (string= session "none")
+    (org-babel-sh-session-buffer (org-babel-sh-initiate-session-by-key session))))
 
 (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
   "Used to indicate that evaluation is has completed.")
@@ -132,18 +133,27 @@ then create.  Return the initialized session."
 'output then return a list of the outputs of the statements in
 BODY, if RESULT-TYPE equals 'value then return the value of the
 last statement in BODY."
-  (let* ((full-body (mapconcat #'org-babel-chomp
-                               (list body org-babel-sh-eoe-indicator) "\n"))
-         (raw (org-babel-comint-with-output buffer org-babel-sh-eoe-output nil
-                (insert full-body) (comint-send-input nil t)))
-         (results (cdr (member org-babel-sh-eoe-output
-                                    (reverse (mapcar #'org-babel-sh-strip-weird-long-prompt
-                                                     (mapcar #'org-babel-trim raw)))))))
-    ;; (message (replace-regexp-in-string "%" "%%" (format "processed-results=%S" results))) ;; debugging
-    (or (case result-type
-          (output (org-babel-trim (mapconcat #'org-babel-trim (reverse results) "\n")))
-          (value (car results))
-          (t (reverse results))) "")))
+  (if (not session)
+      ;; external process evaluation
+      (save-window-excursion
+        (with-temp-buffer ;; TODO: figure out how to return non-output values from shell scripts
+          (insert body)
+          ;; (message "buffer=%s" (buffer-string)) ;; debugging
+          (shell-command-on-region (point-min) (point-max) "sh" 'replace)
+          (buffer-string)))
+    ;; comint session evaluation
+    (let* ((full-body (mapconcat #'org-babel-chomp
+                                 (list body org-babel-sh-eoe-indicator) "\n"))
+           (raw (org-babel-comint-with-output buffer org-babel-sh-eoe-output nil
+                  (insert full-body) (comint-send-input nil t)))
+           (results (cdr (member org-babel-sh-eoe-output
+                                 (reverse (mapcar #'org-babel-sh-strip-weird-long-prompt
+                                                  (mapcar #'org-babel-trim raw)))))))
+      ;; (message (replace-regexp-in-string "%" "%%" (format "processed-results=%S" results))) ;; debugging
+      (or (case result-type
+            (output (org-babel-trim (mapconcat #'org-babel-trim (reverse results) "\n")))
+            (value (car results))
+            (t (reverse results))) ""))))
 
 (defun org-babel-sh-strip-weird-long-prompt (string)
   (while (string-match "^% +[\r\n$]+ *" string)

+ 18 - 3
org-babel.org

@@ -497,7 +497,7 @@ tabel
 
 Another example is in the [[*operations%20in%20on%20tables][grades example]].
 
-** PROPOSED add =:none= session argument (for purely functional execution) [2/4]
+** PROPOSED add =:none= session argument (for purely functional execution) [3/4]
 This would allow source blocks to be run in their own new process
 
 - These blocks could then also be run in the background (since we can
@@ -543,7 +543,17 @@ print 'output'
 #+resname: task-python-none-session
 | 1 | 2 | 3 |
 
-*** TODO sh
+*** DONE sh
+
+#+srcname: task-session-none-sh
+#+begin_src sh :results replace
+echo "first"
+echo "second"
+#+end_src
+
+#+resname: task-session-none-sh
+| "first"  |
+| "second" |
 
 *** TODO R
 ** PROPOSED Are we happy with current behaviour regarding vector/scalar output?
@@ -1802,7 +1812,12 @@ This could probably be added to [[file:lisp/org-babel-script.el][org-babel-scrip
 (see [[* file result types][file result types]])
 
 
-* Bugs [16/20]
+* Bugs [16/21]
+** PROPOSED external shell execution can't isolate return values
+I have no idea how to do this as of yet.  The result is that when
+shell functions are run w/o a session there is no difference between
+the =output= and =value= result arguments.
+
 ** TODO weird escaped characters in shell prompt break shell evaluation
    E.g. this doesn't work. Should the shell sessions set a sane prompt
    when they start up? Or is it a question of altering