Merge branch 'master' into library-of-babel
Conflicts: lisp/org-babel-ref.el org-babel.org
This commit is contained in:
commit
8605c848bf
|
@ -63,6 +63,14 @@ called by `org-babel-execute-src-block'."
|
|||
(list (list results))
|
||||
results))))
|
||||
|
||||
(defun org-babel-prep-session:R (session params)
|
||||
"Prepare SESSION according to the header arguments specified in PARAMS."
|
||||
(let* ((session (org-babel-R-initiate-session session))
|
||||
(vars (org-babel-ref-variables params)))
|
||||
(mapc (lambda (pair) (org-babel-R-assign-elisp session (car pair) (cdr pair))) vars)))
|
||||
|
||||
;; helper functions
|
||||
|
||||
(defun org-babel-R-quote-tsv-field (s)
|
||||
"Quote field S for export to R."
|
||||
(if (stringp s)
|
||||
|
@ -86,8 +94,6 @@ R process in `org-babel-R-buffer'."
|
|||
name transition-file))
|
||||
(format "%s <- %s" name (org-babel-R-quote-tsv-field value)))))
|
||||
|
||||
;; functions for comint evaluation
|
||||
|
||||
(defun org-babel-R-initiate-session (session)
|
||||
"If there is not a current R process then create one."
|
||||
(setq session (or session "*R*"))
|
||||
|
@ -105,42 +111,24 @@ R process in `org-babel-R-buffer'."
|
|||
BODY, if RESULT-TYPE equals 'value then return the value of the
|
||||
last statement in BODY."
|
||||
(org-babel-comint-in-buffer buffer
|
||||
(let* ((string-buffer "")
|
||||
(tmp-file (make-temp-file "org-babel-R"))
|
||||
(let* ((tmp-file (make-temp-file "org-babel-R"))
|
||||
(last-value-eval
|
||||
(format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)"
|
||||
tmp-file))
|
||||
(full-body (mapconcat #'org-babel-chomp (list body last-value-eval org-babel-R-eoe-indicator) "\n"))
|
||||
results)
|
||||
(flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
|
||||
;; setup filter
|
||||
(add-hook 'comint-output-filter-functions 'my-filt)
|
||||
;; pass FULL-BODY to process
|
||||
(goto-char (process-mark (get-buffer-process buffer)))
|
||||
(insert full-body)
|
||||
(comint-send-input)
|
||||
;; wait for end-of-evaluation indicator
|
||||
(while (progn
|
||||
(goto-char comint-last-input-end)
|
||||
(not (save-excursion (and (re-search-forward comint-prompt-regexp nil t)
|
||||
(re-search-forward (regexp-quote org-babel-R-eoe-output) nil t)))))
|
||||
(accept-process-output (get-buffer-process buffer)))
|
||||
;; remove filter
|
||||
(remove-hook 'comint-output-filter-functions 'my-filt))
|
||||
;; (message (format "raw-results=%S" string-buffer)) ;; debugging
|
||||
;; ;; split results
|
||||
;; split results with `comint-prompt-regexp'
|
||||
(setq results (let ((broke nil))
|
||||
(raw (org-babel-comint-with-output buffer org-babel-R-eoe-output nil
|
||||
(insert full-body) (inferior-ess-send-input)))
|
||||
(results (let ((broke nil))
|
||||
(delete nil (mapcar (lambda (el)
|
||||
(if (or broke
|
||||
(and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t)))
|
||||
nil
|
||||
(if (= (length el) 0)
|
||||
nil
|
||||
(if (string-match comint-prompt-regexp el)
|
||||
(substring el (match-end 0))
|
||||
el))))
|
||||
(mapcar #'org-babel-trim (split-string string-buffer comint-prompt-regexp))))))
|
||||
(if (or broke
|
||||
(and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t)))
|
||||
nil
|
||||
(if (= (length el) 0)
|
||||
nil
|
||||
(if (string-match comint-prompt-regexp el)
|
||||
(substring el (match-end 0))
|
||||
el))))
|
||||
(mapcar #'org-babel-trim raw))))))
|
||||
(case result-type
|
||||
(output (org-babel-trim (mapconcat #'identity results "\n")))
|
||||
(value (org-babel-trim (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
|
||||
|
|
|
@ -51,7 +51,7 @@ called by `org-babel-execute-src-block'."
|
|||
(org-babel-python-var-to-python (cdr pair))))
|
||||
vars "\n") "\n" (org-babel-trim body) "\n")) ;; then the source block body
|
||||
(session (org-babel-python-initiate-session (cdr (assoc :session params))))
|
||||
(results (org-babel-python-evaluate (org-babel-python-session-buffer session) full-body result-type)))
|
||||
(results (org-babel-python-evaluate session full-body result-type)))
|
||||
(if (member "scalar" result-params)
|
||||
results
|
||||
(setq results (case result-type ;; process results based on the result-type
|
||||
|
@ -63,6 +63,23 @@ called by `org-babel-execute-src-block'."
|
|||
(list (list results))
|
||||
results))))
|
||||
|
||||
(defun org-babel-prep-session:python (session params)
|
||||
"Prepare SESSION according to the header arguments specified in PARAMS."
|
||||
(let* ((session (org-babel-python-initiate-session session))
|
||||
(vars (org-babel-ref-variables params))
|
||||
(var-lines (mapcar ;; define any variables
|
||||
(lambda (pair)
|
||||
(format "%s=%s"
|
||||
(car pair)
|
||||
(org-babel-python-var-to-python (cdr pair))))
|
||||
vars)))
|
||||
(org-babel-comint-in-buffer session
|
||||
(mapc (lambda (var)
|
||||
(move-end-of-line 1) (insert var) (comint-send-input nil t)
|
||||
(org-babel-comint-wait-for-output session)) var-lines))))
|
||||
|
||||
;; helper functions
|
||||
|
||||
(defun org-babel-python-var-to-python (var)
|
||||
"Convert an elisp var into a string of python source code
|
||||
specifying a var of the same value."
|
||||
|
@ -83,14 +100,12 @@ Emacs-lisp table, otherwise return the results as a string."
|
|||
"'" "\"" results)))))
|
||||
results)))
|
||||
|
||||
;; functions for comint evaluation
|
||||
|
||||
(defvar org-babel-python-buffers '(:default . nil))
|
||||
|
||||
(defun org-babel-python-session-buffer (session)
|
||||
(cdr (assoc session org-babel-python-buffers)))
|
||||
|
||||
(defun org-babel-python-initiate-session (&optional session)
|
||||
(defun org-babel-python-initiate-session-by-key (&optional session)
|
||||
"If there is not a current inferior-process-buffer in SESSION
|
||||
then create. Return the initialized session."
|
||||
(save-window-excursion
|
||||
|
@ -100,6 +115,9 @@ then create. Return the initialized session."
|
|||
(setq org-babel-python-buffers (cons (cons session python-buffer) (assq-delete-all session org-babel-python-buffers)))
|
||||
session)))
|
||||
|
||||
(defun org-babel-python-initiate-session (&optional session)
|
||||
(org-babel-python-session-buffer (org-babel-python-initiate-session-by-key session)))
|
||||
|
||||
(defvar org-babel-python-last-value-eval "_"
|
||||
"When evaluated by Python this returns the return value of the last statement.")
|
||||
(defvar org-babel-python-eoe-indicator "'org_babel_python_eoe'"
|
||||
|
@ -111,35 +129,15 @@ then create. Return the initialized session."
|
|||
BODY, if RESULT-TYPE equals 'value then return the value of the
|
||||
last statement in BODY."
|
||||
(org-babel-comint-in-buffer buffer
|
||||
(let ((string-buffer "")
|
||||
(full-body (mapconcat #'org-babel-trim
|
||||
(list body org-babel-python-last-value-eval org-babel-python-eoe-indicator) "\n"))
|
||||
results)
|
||||
(flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
|
||||
;; setup filter
|
||||
(add-hook 'comint-output-filter-functions 'my-filt)
|
||||
;; pass FULL-BODY to process
|
||||
(goto-char (process-mark (get-buffer-process buffer)))
|
||||
;; for some reason python is fussy, and likes enters after every input
|
||||
(mapc (lambda (statement) (insert statement) (comint-send-input))
|
||||
(split-string full-body "[\r\n]+"))
|
||||
(comint-send-input)
|
||||
;; wait for end-of-evaluation indicator
|
||||
(while (progn
|
||||
(goto-char comint-last-input-end)
|
||||
(not (save-excursion (and (re-search-forward comint-prompt-regexp nil t)
|
||||
(re-search-forward (regexp-quote org-babel-python-eoe-indicator) nil t)))))
|
||||
(accept-process-output (get-buffer-process buffer)))
|
||||
;; remove filter
|
||||
(remove-hook 'comint-output-filter-functions 'my-filt))
|
||||
;; remove echo'd FULL-BODY from input
|
||||
(if (string-match (replace-regexp-in-string "\n" "\r\n" (regexp-quote full-body)) string-buffer)
|
||||
(setq string-buffer (substring string-buffer (match-end 0))))
|
||||
;; split results with `comint-prompt-regexp'
|
||||
(setq results (delete org-babel-python-eoe-indicator
|
||||
(let* ((full-body (mapconcat #'org-babel-trim
|
||||
(list body org-babel-python-last-value-eval org-babel-python-eoe-indicator) "\n"))
|
||||
(raw (org-babel-comint-with-output buffer org-babel-python-eoe-indicator t
|
||||
;; for some reason python is fussy, and likes enters after every input
|
||||
(mapc (lambda (statement) (insert statement) (comint-send-input nil t))
|
||||
(split-string full-body "[\r\n]+"))))
|
||||
(results (delete org-babel-python-eoe-indicator
|
||||
(cdr (member org-babel-python-eoe-indicator
|
||||
(reverse (mapcar #'org-babel-trim
|
||||
(split-string string-buffer comint-prompt-regexp)))))))
|
||||
(reverse (mapcar #'org-babel-trim raw)))))))
|
||||
(setq results (mapcar #'org-babel-python-read-string results))
|
||||
(org-babel-trim (case result-type
|
||||
(output (mapconcat #'identity (reverse (cdr results)) "\n"))
|
||||
|
|
|
@ -60,6 +60,27 @@ called by `org-babel-execute-src-block'."
|
|||
(org-babel-import-elisp-from-file tmp-file)))
|
||||
('value (org-babel-ruby-table-or-results results))))))
|
||||
|
||||
(defun org-babel-prep-session:ruby (session params)
|
||||
"Prepare SESSION according to the header arguments specified in PARAMS."
|
||||
;; (message "params=%S" params) ;; debugging
|
||||
(let* ((session (org-babel-ruby-initiate-session session))
|
||||
(vars (org-babel-ref-variables params))
|
||||
(var-lines (mapcar ;; define any variables
|
||||
(lambda (pair)
|
||||
(format "%s=%s"
|
||||
(car pair)
|
||||
(org-babel-ruby-var-to-ruby (cdr pair))))
|
||||
vars)))
|
||||
;; (message "vars=%S" vars) ;; debugging
|
||||
(org-babel-comint-in-buffer session
|
||||
(sit-for .5) (goto-char (point-max))
|
||||
(mapc (lambda (var)
|
||||
(insert var) (comint-send-input nil t)
|
||||
(org-babel-comint-wait-for-output session)
|
||||
(sit-for .1) (goto-char (point-max))) var-lines))))
|
||||
|
||||
;; helper functions
|
||||
|
||||
(defun org-babel-ruby-var-to-ruby (var)
|
||||
"Convert an elisp var into a string of ruby source code
|
||||
specifying a var of the same value."
|
||||
|
@ -71,7 +92,7 @@ specifying a var of the same value."
|
|||
"If the results look like a table, then convert them into an
|
||||
Emacs-lisp table, otherwise return the results as a string."
|
||||
(org-babel-read
|
||||
(if (string-match "^\\[.+\\]$" results)
|
||||
(if (and (stringp results) (string-match "^\\[.+\\]$" results))
|
||||
(org-babel-read
|
||||
(replace-regexp-in-string
|
||||
"\\[" "(" (replace-regexp-in-string
|
||||
|
@ -80,12 +101,14 @@ Emacs-lisp table, otherwise return the results as a string."
|
|||
"'" "\"" results)))))
|
||||
results)))
|
||||
|
||||
;; functions for comint evaluation
|
||||
|
||||
(defun org-babel-ruby-initiate-session (&optional session)
|
||||
"If there is not a current inferior-process-buffer in SESSION
|
||||
then create. Return the initialized session."
|
||||
(save-window-excursion (run-ruby nil session) (current-buffer)))
|
||||
(let ((session-buffer (save-window-excursion (run-ruby nil session) (current-buffer))))
|
||||
(if (org-babel-comint-buffer-livep session-buffer)
|
||||
session-buffer
|
||||
(sit-for .5)
|
||||
(org-babel-ruby-initiate-session session))))
|
||||
|
||||
(defvar org-babel-ruby-last-value-eval "_"
|
||||
"When evaluated by Ruby this returns the return value of the last statement.")
|
||||
|
@ -97,39 +120,17 @@ 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."
|
||||
(org-babel-comint-in-buffer buffer
|
||||
(let ((string-buffer "")
|
||||
(full-body (mapconcat #'org-babel-chomp
|
||||
(list body org-babel-ruby-last-value-eval org-babel-ruby-eoe-indicator) "\n"))
|
||||
results)
|
||||
(flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
|
||||
;; setup filter
|
||||
(add-hook 'comint-output-filter-functions 'my-filt)
|
||||
;; pass FULL-BODY to process
|
||||
(goto-char (process-mark (get-buffer-process buffer)))
|
||||
(insert full-body)
|
||||
(comint-send-input)
|
||||
;; wait for end-of-evaluation indicator
|
||||
(while (progn
|
||||
(goto-char comint-last-input-end)
|
||||
(not (save-excursion (and (re-search-forward comint-prompt-regexp nil t)
|
||||
(re-search-forward (regexp-quote org-babel-ruby-eoe-indicator) nil t)))))
|
||||
(accept-process-output (get-buffer-process buffer)))
|
||||
;; remove filter
|
||||
(remove-hook 'comint-output-filter-functions 'my-filt))
|
||||
;; remove echo'd FULL-BODY from input
|
||||
(if (string-match (replace-regexp-in-string "\n" "\r\n" (regexp-quote full-body)) string-buffer)
|
||||
(setq string-buffer (substring string-buffer (match-end 0))))
|
||||
;; split results with `comint-prompt-regexp'
|
||||
(setq results (cdr (member org-babel-ruby-eoe-indicator
|
||||
(reverse (mapcar #'org-babel-ruby-read-string
|
||||
(mapcar #'org-babel-trim
|
||||
(split-string string-buffer comint-prompt-regexp)))))))
|
||||
(message "near-final=%S" results)
|
||||
(case result-type
|
||||
(output (mapconcat #'identity (reverse (cdr results)) "\n"))
|
||||
(value (car results))
|
||||
(t (reverse results))))))
|
||||
(let* ((full-body (mapconcat #'org-babel-chomp
|
||||
(list body org-babel-ruby-last-value-eval org-babel-ruby-eoe-indicator) "\n"))
|
||||
(raw (org-babel-comint-with-output buffer org-babel-ruby-eoe-indicator t
|
||||
(insert full-body) (comint-send-input nil t)))
|
||||
(results (cdr (member org-babel-ruby-eoe-indicator
|
||||
(reverse (mapcar #'org-babel-ruby-read-string
|
||||
(mapcar #'org-babel-trim raw)))))))
|
||||
(case result-type
|
||||
(output (mapconcat #'identity (reverse (cdr results)) "\n"))
|
||||
(value (car results))
|
||||
(t (reverse results)))))
|
||||
|
||||
(defun org-babel-ruby-read-string (string)
|
||||
"Strip \\\"s from around ruby string"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
;;; org-babel-shell.el --- org-babel functions for shell evaluation
|
||||
;;; org-babel-sh.el --- org-babel functions for shell evaluation
|
||||
|
||||
;; Copyright (C) 2009 Eric Schulte
|
||||
|
||||
|
@ -48,27 +48,44 @@ function is called by `org-babel-execute-src-block'."
|
|||
(lambda (pair)
|
||||
(format "%s=%s"
|
||||
(car pair)
|
||||
(org-babel-shell-var-to-shell (cdr pair))))
|
||||
(org-babel-sh-var-to-sh (cdr pair))))
|
||||
vars "\n") "\n" body "\n\n")) ;; then the source block body
|
||||
(session (org-babel-shell-initiate-session (cdr (assoc :session params))))
|
||||
(results (org-babel-shell-evaluate (org-babel-shell-session-buffer session) full-body result-type)))
|
||||
(session (org-babel-sh-initiate-session (cdr (assoc :session params))))
|
||||
(results (org-babel-sh-evaluate session full-body result-type)))
|
||||
(if (member "scalar" result-params)
|
||||
results
|
||||
(setq results (let ((tmp-file (make-temp-file "org-babel-ruby")))
|
||||
(setq results (let ((tmp-file (make-temp-file "org-babel-shell")))
|
||||
(with-temp-file tmp-file (insert results))
|
||||
(org-babel-import-elisp-from-file tmp-file)))
|
||||
(if (and (member "vector" results) (not (listp results)))
|
||||
(list (list results))
|
||||
results))))
|
||||
|
||||
(defun org-babel-shell-var-to-shell (var)
|
||||
(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))
|
||||
(var-lines (mapcar ;; define any variables
|
||||
(lambda (pair)
|
||||
(format "%s=%s"
|
||||
(car pair)
|
||||
(org-babel-sh-var-to-sh (cdr pair))))
|
||||
vars)))
|
||||
(org-babel-comint-in-buffer session
|
||||
(mapc (lambda (var)
|
||||
(insert var) (comint-send-input nil t)
|
||||
(org-babel-comint-wait-for-output session)) var-lines))))
|
||||
|
||||
;; helper functions
|
||||
|
||||
(defun org-babel-sh-var-to-sh (var)
|
||||
"Convert an elisp var into a string of shell commands
|
||||
specifying a var of the same value."
|
||||
(if (listp var)
|
||||
(concat "[" (mapconcat #'org-babel-shell-var-to-shell var ", ") "]")
|
||||
(concat "[" (mapconcat #'org-babel-sh-var-to-sh var ", ") "]")
|
||||
(format "%S" var)))
|
||||
|
||||
(defun org-babel-shell-table-or-results (results)
|
||||
(defun org-babel-sh-table-or-results (results)
|
||||
"If the results look like a table, then convert them into an
|
||||
Emacs-lisp table, otherwise return the results as a string."
|
||||
(org-babel-read
|
||||
|
@ -81,73 +98,57 @@ Emacs-lisp table, otherwise return the results as a string."
|
|||
"'" "\"" results)))))
|
||||
results)))
|
||||
|
||||
;; functions for comint evaluation
|
||||
(defvar org-babel-sh-buffers '(:default . nil))
|
||||
|
||||
(defvar org-babel-shell-buffers '(:default . nil))
|
||||
(defun org-babel-sh-session-buffer (session)
|
||||
(cdr (assoc session org-babel-sh-buffers)))
|
||||
|
||||
(defun org-babel-shell-session-buffer (session)
|
||||
(cdr (assoc session org-babel-shell-buffers)))
|
||||
|
||||
(defun org-babel-shell-initiate-session (&optional session)
|
||||
(defun org-babel-sh-initiate-session-by-key (&optional session)
|
||||
"If there is not a current inferior-process-buffer in SESSION
|
||||
then create. Return the initialized session."
|
||||
(save-window-excursion
|
||||
(let* ((session (if session (intern session) :default))
|
||||
(shell-buffer (org-babel-shell-session-buffer session))
|
||||
(newp (not (org-babel-comint-buffer-livep shell-buffer))))
|
||||
(shell shell-buffer)
|
||||
(sh-buffer (org-babel-sh-session-buffer session))
|
||||
(newp (not (org-babel-comint-buffer-livep sh-buffer))))
|
||||
(if (and sh-buffer (get-buffer sh-buffer) (not (buffer-live-p sh-buffer)))
|
||||
(setq sh-buffer nil))
|
||||
(shell sh-buffer)
|
||||
(when newp
|
||||
(setq shell-buffer (current-buffer))
|
||||
(org-babel-comint-wait-for-output shell-buffer))
|
||||
(setq org-babel-shell-buffers (cons (cons session shell-buffer) (assq-delete-all session org-babel-shell-buffers)))
|
||||
(setq sh-buffer (current-buffer))
|
||||
(org-babel-comint-wait-for-output sh-buffer))
|
||||
(setq org-babel-sh-buffers (cons (cons session sh-buffer) (assq-delete-all session org-babel-sh-buffers)))
|
||||
session)))
|
||||
|
||||
(defvar org-babel-shell-eoe-indicator "echo 'org_babel_shell_eoe'"
|
||||
(defun org-babel-sh-initiate-session (&optional session)
|
||||
(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.")
|
||||
(defvar org-babel-shell-eoe-output "org_babel_shell_eoe"
|
||||
(defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
|
||||
"Used to indicate that evaluation is has completed.")
|
||||
|
||||
(defun org-babel-shell-evaluate (buffer body &optional result-type)
|
||||
(defun org-babel-sh-evaluate (buffer body &optional result-type)
|
||||
"Pass BODY to the Shell process in BUFFER. 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."
|
||||
(org-babel-comint-in-buffer buffer
|
||||
(let ((string-buffer "")
|
||||
(full-body (mapconcat #'org-babel-chomp
|
||||
(list body org-babel-shell-eoe-indicator) "\n"))
|
||||
results)
|
||||
(flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
|
||||
;; setup filter
|
||||
(add-hook 'comint-output-filter-functions 'my-filt)
|
||||
;; pass FULL-BODY to process
|
||||
(goto-char (process-mark (get-buffer-process buffer)))
|
||||
(insert full-body)
|
||||
(comint-send-input)
|
||||
;; wait for end-of-evaluation indicator
|
||||
(while (progn
|
||||
(goto-char comint-last-input-end)
|
||||
(not (save-excursion (and (re-search-forward comint-prompt-regexp nil t)
|
||||
(re-search-forward (regexp-quote org-babel-shell-eoe-output) nil t)))))
|
||||
(accept-process-output (get-buffer-process buffer)))
|
||||
;; remove filter
|
||||
(remove-hook 'comint-output-filter-functions 'my-filt))
|
||||
;; (message (format "raw-results=%S" string-buffer)) ;; debugging
|
||||
;; (message (format "split-results=%S" (mapcar #'org-babel-trim (split-string string-buffer comint-prompt-regexp)))) ;; debugging
|
||||
;; split results with `comint-prompt-regexp'
|
||||
(setq results (cdr (member org-babel-shell-eoe-output
|
||||
(reverse (mapcar #'org-babel-shell-strip-weird-long-prompt
|
||||
(mapcar #'org-babel-trim (split-string string-buffer comint-prompt-regexp)))))))
|
||||
;; (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))) ""))))
|
||||
(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-shell-strip-weird-long-prompt (string)
|
||||
(defun org-babel-sh-strip-weird-long-prompt (string)
|
||||
(while (string-match "^% +[\r\n$]+ *" string)
|
||||
(setq string (substring string (match-end 0))))
|
||||
string)
|
||||
|
||||
(provide 'org-babel-shell)
|
||||
;;; org-babel-shell.el ends here
|
||||
(provide 'org-babel-sh)
|
||||
;;; org-babel-sh.el ends here
|
|
@ -47,11 +47,43 @@ body inside the protection of `save-window-excursion' and
|
|||
(declare (indent 1))
|
||||
`(save-window-excursion
|
||||
(save-match-data
|
||||
(unless (org-babel-comint-buffer-livep buffer)
|
||||
(error (format "buffer %s doesn't exist or has no process" buffer)))
|
||||
(set-buffer buffer)
|
||||
(unless (org-babel-comint-buffer-livep ,buffer)
|
||||
(error (format "buffer %s doesn't exist or has no process" ,buffer)))
|
||||
(set-buffer ,buffer)
|
||||
,@body)))
|
||||
|
||||
(defmacro org-babel-comint-with-output (buffer eoe-indicator remove-echo &rest body)
|
||||
"Evaluate BODY in BUFFER, wait until EOE-INDICATOR appears in
|
||||
output, then return all process output. This ensures that the
|
||||
filter is removed in case of an error or user `keyboard-quit'
|
||||
during execution of body."
|
||||
(declare (indent 3))
|
||||
`(org-babel-comint-in-buffer buffer
|
||||
(let ((string-buffer ""))
|
||||
(flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
|
||||
;; setup filter
|
||||
(add-hook 'comint-output-filter-functions 'my-filt)
|
||||
(unwind-protect
|
||||
(progn
|
||||
;; pass FULL-BODY to process
|
||||
(goto-char (process-mark (get-buffer-process (current-buffer))))
|
||||
,@body
|
||||
;; wait for end-of-evaluation indicator
|
||||
(while (progn
|
||||
(goto-char comint-last-input-end)
|
||||
(not (save-excursion (and (re-search-forward comint-prompt-regexp nil t)
|
||||
(re-search-forward (regexp-quote ,eoe-indicator) nil t)))))
|
||||
(accept-process-output (get-buffer-process (current-buffer)))
|
||||
;; ;; thought this would allow async background running, but I was wrong...
|
||||
;; (run-with-timer .5 .5 'accept-process-output (get-buffer-process (current-buffer)))
|
||||
))
|
||||
;; remove filter
|
||||
(remove-hook 'comint-output-filter-functions 'my-filt)))
|
||||
;; remove echo'd FULL-BODY from input
|
||||
(if (and ,remove-echo (string-match (replace-regexp-in-string "\n" "\r\n" (regexp-quote ,full-body)) string-buffer))
|
||||
(setq raw (substring string-buffer (match-end 0))))
|
||||
(split-string string-buffer comint-prompt-regexp))))
|
||||
|
||||
(defun org-babel-comint-input-command (buffer cmd)
|
||||
"Pass CMD to BUFFER The input will not be echoed."
|
||||
(org-babel-comint-in-buffer buffer
|
||||
|
|
|
@ -42,7 +42,7 @@
|
|||
(add-to-list 'load-path (expand-file-name "langs" (file-name-directory (or load-file-name buffer-file-name))))
|
||||
(require 'org-babel-ruby)
|
||||
(require 'org-babel-python)
|
||||
(require 'org-babel-shell)
|
||||
(require 'org-babel-sh)
|
||||
(require 'org-babel-lisp)
|
||||
(require 'org-babel-R)
|
||||
|
||||
|
|
|
@ -105,16 +105,17 @@ return nil."
|
|||
(find-file (match-string 1 ref))
|
||||
(setf ref (match-string 2 ref)))
|
||||
(goto-char (point-min))
|
||||
(unless (let ((result_regexp (concat "^#\\+\\(TBL\\|RES\\)NAME:[ \t]*"
|
||||
(regexp-quote ref) "[ \t]*$"))
|
||||
(regexp (concat "^#\\+SRCNAME:[ \t]*"
|
||||
(regexp-quote ref) "[ \t]*$")))
|
||||
(or (re-search-forward result_regexp nil t)
|
||||
(re-search-forward result_regexp nil t)
|
||||
(re-search-forward regexp nil t)
|
||||
(re-search-backward regexp nil t)
|
||||
;; check the Library of Babel
|
||||
(setq lob-info (cdr (assoc (intern ref) org-babel-library-of-babel)))))
|
||||
(if (let ((result_regexp (concat "^#\\+\\(TBL\\|RES\\)NAME:[ \t]*"
|
||||
(regexp-quote ref) "[ \t]*$"))
|
||||
(regexp (concat "^#\\+SRCNAME:[ \t]*"
|
||||
(regexp-quote ref) "[ \t]*$")))
|
||||
(or (re-search-forward result_regexp nil t)
|
||||
(re-search-forward result_regexp nil t)
|
||||
(re-search-forward regexp nil t)
|
||||
(re-search-backward regexp nil t)
|
||||
;; check the Library of Babel
|
||||
(setq lob-info (cdr (assoc (intern ref) org-babel-library-of-babel)))))
|
||||
(goto-char (match-beginning 0))
|
||||
;; ;; TODO: allow searching for names in other buffers
|
||||
;; (setq id-loc (org-id-find ref 'marker)
|
||||
;; buffer (marker-buffer id-loc)
|
||||
|
@ -130,11 +131,8 @@ return nil."
|
|||
(if (or (= (point) (point-min)) (= (point) (point-max)))
|
||||
(error "reference not found"))))
|
||||
(case type
|
||||
('table
|
||||
(mapcar (lambda (row)
|
||||
(if (and (symbolp row) (equal row 'hline)) row
|
||||
(mapcar #'org-babel-read row)))
|
||||
(org-table-to-lisp)))
|
||||
('results-line (org-babel-ref-read-result))
|
||||
('table (org-babel-ref-read-table))
|
||||
('source-block
|
||||
(setq result (org-babel-execute-src-block
|
||||
t nil (org-combine-plists args nil)))
|
||||
|
@ -146,7 +144,33 @@ return nil."
|
|||
of the supported reference types are found. Supported reference
|
||||
types are tables and source blocks."
|
||||
(cond ((org-at-table-p) 'table)
|
||||
((looking-at "^#\\+BEGIN_SRC") 'source-block)))
|
||||
((looking-at "^#\\+BEGIN_SRC") 'source-block)
|
||||
((looking-at "^#\\+RESNAME:") 'results-line)))
|
||||
|
||||
(defun org-babel-ref-read-result ()
|
||||
"Read the result at `point' into emacs-lisp."
|
||||
(cond
|
||||
((org-at-table-p) (org-babel-ref-read-table))
|
||||
((looking-at ": ")
|
||||
(let ((result-string
|
||||
(org-babel-trim
|
||||
(mapconcat (lambda (line) (if (and (> (length line) 1)
|
||||
(string= ": " (substring line 0 2)))
|
||||
(substring line 2)
|
||||
line))
|
||||
(split-string
|
||||
(buffer-substring (point) (org-babel-result-end)) "[\r\n]+")
|
||||
"\n"))))
|
||||
(or (org-babel-number-p result-string) result-string)))
|
||||
((looking-at "^#\\+RESNAME:")
|
||||
(save-excursion (forward-line 1) (org-babel-ref-read-result)))))
|
||||
|
||||
(defun org-babel-ref-read-table ()
|
||||
"Read the table at `point' into emacs-lisp."
|
||||
(mapcar (lambda (row)
|
||||
(if (and (symbolp row) (equal row 'hline)) row
|
||||
(mapcar #'org-babel-read row)))
|
||||
(org-table-to-lisp)))
|
||||
|
||||
(provide 'org-babel-ref)
|
||||
;;; org-babel-ref.el ends here
|
||||
|
|
|
@ -40,6 +40,15 @@ then run `org-babel-execute-src-block'."
|
|||
|
||||
(add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-src-block-maybe)
|
||||
|
||||
(defun org-babel-pop-to-session-maybe ()
|
||||
"Detect if this is context for a org-babel src-block and if so
|
||||
then run `org-babel-pop-to-session'."
|
||||
(interactive)
|
||||
(let ((info (org-babel-get-src-block-info)))
|
||||
(if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil)))
|
||||
|
||||
(add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
|
||||
|
||||
(defvar org-babel-default-header-args '()
|
||||
"Default arguments to use when evaluating a source block.")
|
||||
|
||||
|
@ -115,6 +124,27 @@ lisp code use the `org-babel-add-interpreter' function."
|
|||
(const "babel")))
|
||||
|
||||
;;; functions
|
||||
(defun org-babel-pop-to-session (&optional arg info)
|
||||
"Pop to the session of the current source-code block. If
|
||||
called with a prefix argument then evaluate the header arguments
|
||||
for the source block before entering the session. Copy the body
|
||||
of the source block to the kill ring."
|
||||
(interactive)
|
||||
(let* ((info (or info (org-babel-get-src-block-info)))
|
||||
(lang (first info))
|
||||
(body (second info))
|
||||
(params (third info))
|
||||
(session (cdr (assoc :session params))))
|
||||
(unless (member lang org-babel-interpreters)
|
||||
(error "Language is not in `org-babel-interpreters': %s" lang))
|
||||
;; copy body to the kill ring
|
||||
(with-temp-buffer (insert (org-babel-trim body)) (copy-region-as-kill (point-min) (point-max)))
|
||||
;; if called with a prefix argument, then process header arguments
|
||||
(if arg (funcall (intern (concat "org-babel-prep-session:" lang)) session params))
|
||||
;; just to the session using pop-to-buffer
|
||||
(pop-to-buffer (funcall (intern (format "org-babel-%s-initiate-session" lang)) session))
|
||||
(move-end-of-line 1)))
|
||||
|
||||
(defun org-babel-execute-src-block (&optional arg info params)
|
||||
"Execute the current source code block, and dump the results
|
||||
into the buffer immediately following the block. Results are
|
||||
|
@ -336,16 +366,19 @@ relies on `org-babel-insert-result'."
|
|||
(interactive)
|
||||
(save-excursion
|
||||
(goto-char (org-babel-where-is-src-block-result)) (forward-line 1)
|
||||
(delete-region (point)
|
||||
(save-excursion
|
||||
(if (org-at-table-p)
|
||||
(org-table-end)
|
||||
(while (if (looking-at "\\(: \\|\\[\\[\\)")
|
||||
(progn (while (looking-at "\\(: \\|\\[\\[\\)")
|
||||
(forward-line 1)) t))
|
||||
(forward-line 1))
|
||||
(forward-line -1)
|
||||
(point))))))
|
||||
(delete-region (point) (org-babel-result-end))))
|
||||
|
||||
(defun org-babel-result-end ()
|
||||
"Return the point at the end of the current set of results"
|
||||
(save-excursion
|
||||
(if (org-at-table-p)
|
||||
(org-table-end)
|
||||
(while (if (looking-at "\\(: \\|\\[\\[\\)")
|
||||
(progn (while (looking-at "\\(: \\|\\[\\[\\)")
|
||||
(forward-line 1)) t))
|
||||
(forward-line 1))
|
||||
(forward-line -1)
|
||||
(point))))
|
||||
|
||||
(defun org-babel-result-to-file (result)
|
||||
"Return an `org-mode' link with the path being the value or
|
||||
|
@ -384,17 +417,17 @@ string.
|
|||
|
||||
This is taken almost directly from `org-read-prop'."
|
||||
(if (and (stringp cell) (not (equal cell "")))
|
||||
(if (org-babel-number-p cell)
|
||||
(string-to-number cell)
|
||||
(if (or (equal "(" (substring cell 0 1))
|
||||
(equal "'" (substring cell 0 2)))
|
||||
(read cell)
|
||||
(progn (set-text-properties 0 (length cell) nil cell) cell)))
|
||||
(or (org-babel-number-p cell)
|
||||
(if (or (equal "(" (substring cell 0 1))
|
||||
(equal "'" (substring cell 0 2)))
|
||||
(read cell)
|
||||
(progn (set-text-properties 0 (length cell) nil cell) cell)))
|
||||
cell))
|
||||
|
||||
(defun org-babel-number-p (string)
|
||||
"Return t if STRING represents a number"
|
||||
(string-match "^[[:digit:]]*\\.?[[:digit:]]*$" string))
|
||||
(if (string-match "^[[:digit:]]*\\.?[[:digit:]]*$" string)
|
||||
(string-to-number string)))
|
||||
|
||||
(defun org-babel-import-elisp-from-file (file-name)
|
||||
"Read the results located at FILE-NAME into an elisp table. If
|
||||
|
|
1440
org-babel.org
1440
org-babel.org
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue