1
0
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2024-11-24 07:20:29 +00:00

babel: sh: Fix variable assignments made by `org-babel-prep-session:sh'

* ob-sh.el (org-babel-expand-body:sh): Refactor: break variable
    assignment part out into a separate function
    (org-babel-sh-variable-assignments): New function constructing
    list of variable assignment statements
    (org-babel-prep-session:sh): Use new function
    `org-babel-sh-variable-assignments' instead of previous
    (incorrect) variable assignment code.
This commit is contained in:
Dan Davison 2010-10-14 21:36:42 +01:00
parent 24a5f8b4e5
commit 8e151c066e

View File

@ -48,15 +48,10 @@ This will be passed to `shell-command-on-region'")
(defun org-babel-expand-body:sh (body params &optional processed-params)
"Expand BODY according to PARAMS, return the expanded body."
(let ((vars (nth 1 (or processed-params (org-babel-process-params params))))
(sep (cdr (assoc :separator params))))
(concat
(mapconcat ;; define any variables
(lambda (pair)
(format "%s=%s"
(car pair)
(org-babel-sh-var-to-sh (cdr pair) sep)))
vars "\n") (if vars "\n" "") body "\n\n")))
(mapconcat
#'identity
(append (org-babel-sh-variable-assignments params processed-params)
(list body)) "\n")
(defun org-babel-execute:sh (body params)
"Execute a block of Shell commands with Babel.
@ -76,14 +71,7 @@ This function is called by `org-babel-execute-src-block'."
(defun org-babel-prep-session:sh (session params)
"Prepare SESSION according to the header arguments specified in PARAMS."
(let* ((session (org-babel-sh-initiate-session session))
(vars (org-babel-ref-variables params))
(sep (cdr (assoc :separator params)))
(var-lines (mapcar ;; define any variables
(lambda (pair)
(format "%s=%s"
(car pair)
(org-babel-sh-var-to-sh (cdr pair) sep)))
vars)))
(var-lines (org-babel-sh-variable-assignments params)))
(org-babel-comint-in-buffer session
(mapc (lambda (var)
(insert var) (comint-send-input nil t)
@ -101,6 +89,15 @@ This function is called by `org-babel-execute-src-block'."
;; helper functions
(defun org-babel-sh-variable-assignments (params &optional processed-params)
(let ((sep (cdr (assoc :separator params))))
(mapcar
(lambda (pair)
(format "%s=%s"
(car pair)
(org-babel-sh-var-to-sh (cdr pair) sep)))
(nth 1 (or processed-params (org-babel-process-params params))))))
(defun org-babel-sh-var-to-sh (var &optional sep)
"Convert an elisp value to a shell variable.
Convert an elisp var into a string of shell commands specifying a