summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2018-04-15 12:26:03 +0200
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2018-04-15 12:26:03 +0200
commitbb035512464fcb7306d7360b3ac2ba2c2f2e8f23 (patch)
tree1c4f0fabe8f7efe7d9a40e697cbccec72a255bcc
parentcc9233c1f4a1e6583c6c5bf501ef58e6733cb71a (diff)
downloadorg-mode-bb035512464fcb7306d7360b3ac2ba2c2f2e8f23.tar.gz
ob-shell: Small refactoring
* lisp/ob-shell.el (org-babel-sh-evaluate): Slightly refactor code.
-rw-r--r--lisp/ob-shell.el110
1 files changed, 54 insertions, 56 deletions
diff --git a/lisp/ob-shell.el b/lisp/ob-shell.el
index 26dd44b..c3c6996 100644
--- a/lisp/ob-shell.el
+++ b/lisp/ob-shell.el
@@ -206,62 +206,60 @@ var of the same value."
If RESULT-TYPE equals `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 ((results
- (cond
- ((or stdin cmdline) ; external shell script w/STDIN
- (let ((script-file (org-babel-temp-file "sh-script-"))
- (stdin-file (org-babel-temp-file "sh-stdin-"))
- (shebang (cdr (assq :shebang params)))
- (padline (not (string= "no" (cdr (assq :padline params))))))
- (with-temp-file script-file
- (when shebang (insert (concat shebang "\n")))
- (when padline (insert "\n"))
- (insert body))
- (set-file-modes script-file #o755)
- (with-temp-file stdin-file (insert (or stdin "")))
- (with-temp-buffer
- (call-process-shell-command
- (concat (if shebang script-file
- (format "%s %s" shell-file-name script-file))
- (and cmdline (concat " " cmdline)))
- stdin-file
- (current-buffer))
- (buffer-string))))
- (session ; session evaluation
- (mapconcat
- #'org-babel-sh-strip-weird-long-prompt
- (mapcar
- #'org-trim
- (butlast
- (org-babel-comint-with-output
- (session org-babel-sh-eoe-output t body)
- (mapc
- (lambda (line)
- (insert line)
- (comint-send-input nil t)
- (while (save-excursion
- (goto-char comint-last-input-end)
- (not (re-search-forward
- comint-prompt-regexp nil t)))
- (accept-process-output
- (get-buffer-process (current-buffer)))))
- (append
- (split-string (org-trim body) "\n")
- (list org-babel-sh-eoe-indicator))))
- 2)) "\n"))
- ('otherwise ; external shell script
- (if (and (cdr (assq :shebang params))
- (> (length (cdr (assq :shebang params))) 0))
- (let ((script-file (org-babel-temp-file "sh-script-"))
- (shebang (cdr (assq :shebang params)))
- (padline (not (equal "no" (cdr (assq :padline params))))))
- (with-temp-file script-file
- (when shebang (insert (concat shebang "\n")))
- (when padline (insert "\n"))
- (insert body))
- (set-file-modes script-file #o755)
- (org-babel-eval script-file ""))
- (org-babel-eval shell-file-name (org-trim body)))))))
+ (let* ((shebang (cdr (assq :shebang params)))
+ (results
+ (cond
+ ((or stdin cmdline) ; external shell script w/STDIN
+ (let ((script-file (org-babel-temp-file "sh-script-"))
+ (stdin-file (org-babel-temp-file "sh-stdin-"))
+ (padline (not (string= "no" (cdr (assq :padline params))))))
+ (with-temp-file script-file
+ (when shebang (insert shebang "\n"))
+ (when padline (insert "\n"))
+ (insert body))
+ (set-file-modes script-file #o755)
+ (with-temp-file stdin-file (insert (or stdin "")))
+ (with-temp-buffer
+ (call-process-shell-command
+ (concat (if shebang script-file
+ (format "%s %s" shell-file-name script-file))
+ (and cmdline (concat " " cmdline)))
+ stdin-file
+ (current-buffer))
+ (buffer-string))))
+ (session ; session evaluation
+ (mapconcat
+ #'org-babel-sh-strip-weird-long-prompt
+ (mapcar
+ #'org-trim
+ (butlast
+ (org-babel-comint-with-output
+ (session org-babel-sh-eoe-output t body)
+ (dolist (line (append (split-string (org-trim body) "\n")
+ (list org-babel-sh-eoe-indicator)))
+ (insert line)
+ (comint-send-input nil t)
+ (while (save-excursion
+ (goto-char comint-last-input-end)
+ (not (re-search-forward
+ comint-prompt-regexp nil t)))
+ (accept-process-output
+ (get-buffer-process (current-buffer))))))
+ 2))
+ "\n"))
+ ;; External shell script, with or without a predefined
+ ;; shebang.
+ ((org-string-nw-p shebang)
+ (let ((script-file (org-babel-temp-file "sh-script-"))
+ (padline (not (equal "no" (cdr (assq :padline params))))))
+ (with-temp-file script-file
+ (insert shebang "\n")
+ (when padline (insert "\n"))
+ (insert body))
+ (set-file-modes script-file #o755)
+ (org-babel-eval script-file "")))
+ (t
+ (org-babel-eval shell-file-name (org-trim body))))))
(when results
(let ((result-params (cdr (assq :result-params params))))
(org-babel-result-cond result-params