Merge branch 'master' into library-of-babel

Conflicts:
	lisp/org-babel-ref.el
	org-babel.org
This commit is contained in:
Eric Schulte 2009-06-14 15:24:41 -07:00
commit 8605c848bf
9 changed files with 1080 additions and 829 deletions

View File

@ -63,6 +63,14 @@ called by `org-babel-execute-src-block'."
(list (list results)) (list (list results))
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) (defun org-babel-R-quote-tsv-field (s)
"Quote field S for export to R." "Quote field S for export to R."
(if (stringp s) (if (stringp s)
@ -86,8 +94,6 @@ R process in `org-babel-R-buffer'."
name transition-file)) name transition-file))
(format "%s <- %s" name (org-babel-R-quote-tsv-field value))))) (format "%s <- %s" name (org-babel-R-quote-tsv-field value)))))
;; functions for comint evaluation
(defun org-babel-R-initiate-session (session) (defun org-babel-R-initiate-session (session)
"If there is not a current R process then create one." "If there is not a current R process then create one."
(setq session (or session "*R*")) (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 BODY, if RESULT-TYPE equals 'value then return the value of the
last statement in BODY." last statement in BODY."
(org-babel-comint-in-buffer buffer (org-babel-comint-in-buffer buffer
(let* ((string-buffer "") (let* ((tmp-file (make-temp-file "org-babel-R"))
(tmp-file (make-temp-file "org-babel-R"))
(last-value-eval (last-value-eval
(format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)" (format "write.table(.Last.value, file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.names=FALSE, quote=FALSE)"
tmp-file)) tmp-file))
(full-body (mapconcat #'org-babel-chomp (list body last-value-eval org-babel-R-eoe-indicator) "\n")) (full-body (mapconcat #'org-babel-chomp (list body last-value-eval org-babel-R-eoe-indicator) "\n"))
results) (raw (org-babel-comint-with-output buffer org-babel-R-eoe-output nil
(flet ((my-filt (text) (setq string-buffer (concat string-buffer text)))) (insert full-body) (inferior-ess-send-input)))
;; setup filter (results (let ((broke nil))
(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))
(delete nil (mapcar (lambda (el) (delete nil (mapcar (lambda (el)
(if (or broke (if (or broke
(and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t))) (and (string-match (regexp-quote org-babel-R-eoe-output) el) (setq broke t)))
nil nil
(if (= (length el) 0) (if (= (length el) 0)
nil nil
(if (string-match comint-prompt-regexp el) (if (string-match comint-prompt-regexp el)
(substring el (match-end 0)) (substring el (match-end 0))
el)))) el))))
(mapcar #'org-babel-trim (split-string string-buffer comint-prompt-regexp)))))) (mapcar #'org-babel-trim raw))))))
(case result-type (case result-type
(output (org-babel-trim (mapconcat #'identity results "\n"))) (output (org-babel-trim (mapconcat #'identity results "\n")))
(value (org-babel-trim (with-temp-buffer (insert-file-contents tmp-file) (buffer-string)))) (value (org-babel-trim (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))

View File

@ -51,7 +51,7 @@ called by `org-babel-execute-src-block'."
(org-babel-python-var-to-python (cdr pair)))) (org-babel-python-var-to-python (cdr pair))))
vars "\n") "\n" (org-babel-trim body) "\n")) ;; then the source block body vars "\n") "\n" (org-babel-trim body) "\n")) ;; then the source block body
(session (org-babel-python-initiate-session (cdr (assoc :session params)))) (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) (if (member "scalar" result-params)
results results
(setq results (case result-type ;; process results based on the result-type (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)) (list (list results))
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) (defun org-babel-python-var-to-python (var)
"Convert an elisp var into a string of python source code "Convert an elisp var into a string of python source code
specifying a var of the same value." specifying a var of the same value."
@ -83,14 +100,12 @@ Emacs-lisp table, otherwise return the results as a string."
"'" "\"" results))))) "'" "\"" results)))))
results))) results)))
;; functions for comint evaluation
(defvar org-babel-python-buffers '(:default . nil)) (defvar org-babel-python-buffers '(:default . nil))
(defun org-babel-python-session-buffer (session) (defun org-babel-python-session-buffer (session)
(cdr (assoc session org-babel-python-buffers))) (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 "If there is not a current inferior-process-buffer in SESSION
then create. Return the initialized session." then create. Return the initialized session."
(save-window-excursion (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))) (setq org-babel-python-buffers (cons (cons session python-buffer) (assq-delete-all session org-babel-python-buffers)))
session))) 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 "_" (defvar org-babel-python-last-value-eval "_"
"When evaluated by Python this returns the return value of the last statement.") "When evaluated by Python this returns the return value of the last statement.")
(defvar org-babel-python-eoe-indicator "'org_babel_python_eoe'" (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 BODY, if RESULT-TYPE equals 'value then return the value of the
last statement in BODY." last statement in BODY."
(org-babel-comint-in-buffer buffer (org-babel-comint-in-buffer buffer
(let ((string-buffer "") (let* ((full-body (mapconcat #'org-babel-trim
(full-body (mapconcat #'org-babel-trim (list body org-babel-python-last-value-eval org-babel-python-eoe-indicator) "\n"))
(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
results) ;; for some reason python is fussy, and likes enters after every input
(flet ((my-filt (text) (setq string-buffer (concat string-buffer text)))) (mapc (lambda (statement) (insert statement) (comint-send-input nil t))
;; setup filter (split-string full-body "[\r\n]+"))))
(add-hook 'comint-output-filter-functions 'my-filt) (results (delete org-babel-python-eoe-indicator
;; 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
(cdr (member org-babel-python-eoe-indicator (cdr (member org-babel-python-eoe-indicator
(reverse (mapcar #'org-babel-trim (reverse (mapcar #'org-babel-trim raw)))))))
(split-string string-buffer comint-prompt-regexp)))))))
(setq results (mapcar #'org-babel-python-read-string results)) (setq results (mapcar #'org-babel-python-read-string results))
(org-babel-trim (case result-type (org-babel-trim (case result-type
(output (mapconcat #'identity (reverse (cdr results)) "\n")) (output (mapconcat #'identity (reverse (cdr results)) "\n"))

View File

@ -60,6 +60,27 @@ called by `org-babel-execute-src-block'."
(org-babel-import-elisp-from-file tmp-file))) (org-babel-import-elisp-from-file tmp-file)))
('value (org-babel-ruby-table-or-results results)))))) ('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) (defun org-babel-ruby-var-to-ruby (var)
"Convert an elisp var into a string of ruby source code "Convert an elisp var into a string of ruby source code
specifying a var of the same value." 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 "If the results look like a table, then convert them into an
Emacs-lisp table, otherwise return the results as a string." Emacs-lisp table, otherwise return the results as a string."
(org-babel-read (org-babel-read
(if (string-match "^\\[.+\\]$" results) (if (and (stringp results) (string-match "^\\[.+\\]$" results))
(org-babel-read (org-babel-read
(replace-regexp-in-string (replace-regexp-in-string
"\\[" "(" (replace-regexp-in-string "\\[" "(" (replace-regexp-in-string
@ -80,12 +101,14 @@ Emacs-lisp table, otherwise return the results as a string."
"'" "\"" results))))) "'" "\"" results)))))
results))) results)))
;; functions for comint evaluation
(defun org-babel-ruby-initiate-session (&optional session) (defun org-babel-ruby-initiate-session (&optional session)
"If there is not a current inferior-process-buffer in SESSION "If there is not a current inferior-process-buffer in SESSION
then create. Return the initialized 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 "_" (defvar org-babel-ruby-last-value-eval "_"
"When evaluated by Ruby this returns the return value of the last statement.") "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 'output then return a list of the outputs of the statements in
BODY, if RESULT-TYPE equals 'value then return the value of the BODY, if RESULT-TYPE equals 'value then return the value of the
last statement in BODY." last statement in BODY."
(org-babel-comint-in-buffer buffer (let* ((full-body (mapconcat #'org-babel-chomp
(let ((string-buffer "") (list body org-babel-ruby-last-value-eval org-babel-ruby-eoe-indicator) "\n"))
(full-body (mapconcat #'org-babel-chomp (raw (org-babel-comint-with-output buffer org-babel-ruby-eoe-indicator t
(list body org-babel-ruby-last-value-eval org-babel-ruby-eoe-indicator) "\n")) (insert full-body) (comint-send-input nil t)))
results) (results (cdr (member org-babel-ruby-eoe-indicator
(flet ((my-filt (text) (setq string-buffer (concat string-buffer text)))) (reverse (mapcar #'org-babel-ruby-read-string
;; setup filter (mapcar #'org-babel-trim raw)))))))
(add-hook 'comint-output-filter-functions 'my-filt) (case result-type
;; pass FULL-BODY to process (output (mapconcat #'identity (reverse (cdr results)) "\n"))
(goto-char (process-mark (get-buffer-process buffer))) (value (car results))
(insert full-body) (t (reverse results)))))
(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))))))
(defun org-babel-ruby-read-string (string) (defun org-babel-ruby-read-string (string)
"Strip \\\"s from around ruby string" "Strip \\\"s from around ruby string"

View File

@ -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 ;; Copyright (C) 2009 Eric Schulte
@ -48,27 +48,44 @@ function is called by `org-babel-execute-src-block'."
(lambda (pair) (lambda (pair)
(format "%s=%s" (format "%s=%s"
(car pair) (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 vars "\n") "\n" body "\n\n")) ;; then the source block body
(session (org-babel-shell-initiate-session (cdr (assoc :session params)))) (session (org-babel-sh-initiate-session (cdr (assoc :session params))))
(results (org-babel-shell-evaluate (org-babel-shell-session-buffer session) full-body result-type))) (results (org-babel-sh-evaluate session full-body result-type)))
(if (member "scalar" result-params) (if (member "scalar" result-params)
results 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)) (with-temp-file tmp-file (insert results))
(org-babel-import-elisp-from-file tmp-file))) (org-babel-import-elisp-from-file tmp-file)))
(if (and (member "vector" results) (not (listp results))) (if (and (member "vector" results) (not (listp results)))
(list (list results)) (list (list results))
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 "Convert an elisp var into a string of shell commands
specifying a var of the same value." specifying a var of the same value."
(if (listp var) (if (listp var)
(concat "[" (mapconcat #'org-babel-shell-var-to-shell var ", ") "]") (concat "[" (mapconcat #'org-babel-sh-var-to-sh var ", ") "]")
(format "%S" 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 "If the results look like a table, then convert them into an
Emacs-lisp table, otherwise return the results as a string." Emacs-lisp table, otherwise return the results as a string."
(org-babel-read (org-babel-read
@ -81,73 +98,57 @@ Emacs-lisp table, otherwise return the results as a string."
"'" "\"" results))))) "'" "\"" results)))))
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) (defun org-babel-sh-initiate-session-by-key (&optional session)
(cdr (assoc session org-babel-shell-buffers)))
(defun org-babel-shell-initiate-session (&optional session)
"If there is not a current inferior-process-buffer in SESSION "If there is not a current inferior-process-buffer in SESSION
then create. Return the initialized session." then create. Return the initialized session."
(save-window-excursion (save-window-excursion
(let* ((session (if session (intern session) :default)) (let* ((session (if session (intern session) :default))
(shell-buffer (org-babel-shell-session-buffer session)) (sh-buffer (org-babel-sh-session-buffer session))
(newp (not (org-babel-comint-buffer-livep shell-buffer)))) (newp (not (org-babel-comint-buffer-livep sh-buffer))))
(shell shell-buffer) (if (and sh-buffer (get-buffer sh-buffer) (not (buffer-live-p sh-buffer)))
(setq sh-buffer nil))
(shell sh-buffer)
(when newp (when newp
(setq shell-buffer (current-buffer)) (setq sh-buffer (current-buffer))
(org-babel-comint-wait-for-output shell-buffer)) (org-babel-comint-wait-for-output sh-buffer))
(setq org-babel-shell-buffers (cons (cons session shell-buffer) (assq-delete-all session org-babel-shell-buffers))) (setq org-babel-sh-buffers (cons (cons session sh-buffer) (assq-delete-all session org-babel-sh-buffers)))
session))) 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.") "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.") "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 "Pass BODY to the Shell process in BUFFER. If RESULT-TYPE equals
'output then return a list of the outputs of the statements in 'output then return a list of the outputs of the statements in
BODY, if RESULT-TYPE equals 'value then return the value of the BODY, if RESULT-TYPE equals 'value then return the value of the
last statement in BODY." last statement in BODY."
(org-babel-comint-in-buffer buffer (let* ((full-body (mapconcat #'org-babel-chomp
(let ((string-buffer "") (list body org-babel-sh-eoe-indicator) "\n"))
(full-body (mapconcat #'org-babel-chomp (raw (org-babel-comint-with-output buffer org-babel-sh-eoe-output nil
(list body org-babel-shell-eoe-indicator) "\n")) (insert full-body) (comint-send-input nil t)))
results) (results (cdr (member org-babel-sh-eoe-output
(flet ((my-filt (text) (setq string-buffer (concat string-buffer text)))) (reverse (mapcar #'org-babel-sh-strip-weird-long-prompt
;; setup filter (mapcar #'org-babel-trim raw)))))))
(add-hook 'comint-output-filter-functions 'my-filt) ;; (message (replace-regexp-in-string "%" "%%" (format "processed-results=%S" results))) ;; debugging
;; pass FULL-BODY to process (or (case result-type
(goto-char (process-mark (get-buffer-process buffer))) (output (org-babel-trim (mapconcat #'org-babel-trim (reverse results) "\n")))
(insert full-body) (value (car results))
(comint-send-input) (t (reverse results))) "")))
;; 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))) ""))))
(defun org-babel-shell-strip-weird-long-prompt (string) (defun org-babel-sh-strip-weird-long-prompt (string)
(while (string-match "^% +[\r\n$]+ *" string) (while (string-match "^% +[\r\n$]+ *" string)
(setq string (substring string (match-end 0)))) (setq string (substring string (match-end 0))))
string) string)
(provide 'org-babel-shell) (provide 'org-babel-sh)
;;; org-babel-shell.el ends here ;;; org-babel-sh.el ends here

View File

@ -47,11 +47,43 @@ body inside the protection of `save-window-excursion' and
(declare (indent 1)) (declare (indent 1))
`(save-window-excursion `(save-window-excursion
(save-match-data (save-match-data
(unless (org-babel-comint-buffer-livep buffer) (unless (org-babel-comint-buffer-livep ,buffer)
(error (format "buffer %s doesn't exist or has no process" buffer))) (error (format "buffer %s doesn't exist or has no process" ,buffer)))
(set-buffer buffer) (set-buffer ,buffer)
,@body))) ,@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) (defun org-babel-comint-input-command (buffer cmd)
"Pass CMD to BUFFER The input will not be echoed." "Pass CMD to BUFFER The input will not be echoed."
(org-babel-comint-in-buffer buffer (org-babel-comint-in-buffer buffer

View File

@ -42,7 +42,7 @@
(add-to-list 'load-path (expand-file-name "langs" (file-name-directory (or load-file-name buffer-file-name)))) (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-ruby)
(require 'org-babel-python) (require 'org-babel-python)
(require 'org-babel-shell) (require 'org-babel-sh)
(require 'org-babel-lisp) (require 'org-babel-lisp)
(require 'org-babel-R) (require 'org-babel-R)

View File

@ -105,16 +105,17 @@ return nil."
(find-file (match-string 1 ref)) (find-file (match-string 1 ref))
(setf ref (match-string 2 ref))) (setf ref (match-string 2 ref)))
(goto-char (point-min)) (goto-char (point-min))
(unless (let ((result_regexp (concat "^#\\+\\(TBL\\|RES\\)NAME:[ \t]*" (if (let ((result_regexp (concat "^#\\+\\(TBL\\|RES\\)NAME:[ \t]*"
(regexp-quote ref) "[ \t]*$")) (regexp-quote ref) "[ \t]*$"))
(regexp (concat "^#\\+SRCNAME:[ \t]*" (regexp (concat "^#\\+SRCNAME:[ \t]*"
(regexp-quote ref) "[ \t]*$"))) (regexp-quote ref) "[ \t]*$")))
(or (re-search-forward result_regexp nil t) (or (re-search-forward result_regexp nil t)
(re-search-forward result_regexp nil t) (re-search-forward result_regexp nil t)
(re-search-forward regexp nil t) (re-search-forward regexp nil t)
(re-search-backward regexp nil t) (re-search-backward regexp nil t)
;; check the Library of Babel ;; check the Library of Babel
(setq lob-info (cdr (assoc (intern ref) org-babel-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 ;; ;; TODO: allow searching for names in other buffers
;; (setq id-loc (org-id-find ref 'marker) ;; (setq id-loc (org-id-find ref 'marker)
;; buffer (marker-buffer id-loc) ;; buffer (marker-buffer id-loc)
@ -130,11 +131,8 @@ return nil."
(if (or (= (point) (point-min)) (= (point) (point-max))) (if (or (= (point) (point-min)) (= (point) (point-max)))
(error "reference not found")))) (error "reference not found"))))
(case type (case type
('table ('results-line (org-babel-ref-read-result))
(mapcar (lambda (row) ('table (org-babel-ref-read-table))
(if (and (symbolp row) (equal row 'hline)) row
(mapcar #'org-babel-read row)))
(org-table-to-lisp)))
('source-block ('source-block
(setq result (org-babel-execute-src-block (setq result (org-babel-execute-src-block
t nil (org-combine-plists args nil))) t nil (org-combine-plists args nil)))
@ -146,7 +144,33 @@ return nil."
of the supported reference types are found. Supported reference of the supported reference types are found. Supported reference
types are tables and source blocks." types are tables and source blocks."
(cond ((org-at-table-p) 'table) (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) (provide 'org-babel-ref)
;;; org-babel-ref.el ends here ;;; org-babel-ref.el ends here

View File

@ -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) (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 '() (defvar org-babel-default-header-args '()
"Default arguments to use when evaluating a source block.") "Default arguments to use when evaluating a source block.")
@ -115,6 +124,27 @@ lisp code use the `org-babel-add-interpreter' function."
(const "babel"))) (const "babel")))
;;; functions ;;; 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) (defun org-babel-execute-src-block (&optional arg info params)
"Execute the current source code block, and dump the results "Execute the current source code block, and dump the results
into the buffer immediately following the block. Results are into the buffer immediately following the block. Results are
@ -336,16 +366,19 @@ relies on `org-babel-insert-result'."
(interactive) (interactive)
(save-excursion (save-excursion
(goto-char (org-babel-where-is-src-block-result)) (forward-line 1) (goto-char (org-babel-where-is-src-block-result)) (forward-line 1)
(delete-region (point) (delete-region (point) (org-babel-result-end))))
(save-excursion
(if (org-at-table-p) (defun org-babel-result-end ()
(org-table-end) "Return the point at the end of the current set of results"
(while (if (looking-at "\\(: \\|\\[\\[\\)") (save-excursion
(progn (while (looking-at "\\(: \\|\\[\\[\\)") (if (org-at-table-p)
(forward-line 1)) t)) (org-table-end)
(forward-line 1)) (while (if (looking-at "\\(: \\|\\[\\[\\)")
(forward-line -1) (progn (while (looking-at "\\(: \\|\\[\\[\\)")
(point)))))) (forward-line 1)) t))
(forward-line 1))
(forward-line -1)
(point))))
(defun org-babel-result-to-file (result) (defun org-babel-result-to-file (result)
"Return an `org-mode' link with the path being the value or "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'." This is taken almost directly from `org-read-prop'."
(if (and (stringp cell) (not (equal cell ""))) (if (and (stringp cell) (not (equal cell "")))
(if (org-babel-number-p cell) (or (org-babel-number-p cell)
(string-to-number cell) (if (or (equal "(" (substring cell 0 1))
(if (or (equal "(" (substring cell 0 1)) (equal "'" (substring cell 0 2)))
(equal "'" (substring cell 0 2))) (read cell)
(read cell) (progn (set-text-properties 0 (length cell) nil cell) cell)))
(progn (set-text-properties 0 (length cell) nil cell) cell)))
cell)) cell))
(defun org-babel-number-p (string) (defun org-babel-number-p (string)
"Return t if STRING represents a number" "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) (defun org-babel-import-elisp-from-file (file-name)
"Read the results located at FILE-NAME into an elisp table. If "Read the results located at FILE-NAME into an elisp table. If

File diff suppressed because it is too large Load Diff