ob-shell: Small refactoring
* lisp/ob-shell.el (org-babel-sh-evaluate): Slightly refactor code.
This commit is contained in:
parent
cc9233c1f4
commit
bb03551246
110
lisp/ob-shell.el
110
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
|
||||
|
|
Loading…
Reference in New Issue