|
@@ -46,7 +46,9 @@ then run `org-babel-execute-src-block'."
|
|
|
then run `org-babel-expand-src-block'."
|
|
then run `org-babel-expand-src-block'."
|
|
|
(interactive)
|
|
(interactive)
|
|
|
(let ((info (org-babel-get-src-block-info)))
|
|
(let ((info (org-babel-get-src-block-info)))
|
|
|
- (if info (progn (org-babel-expand-src-block current-prefix-arg info) t) nil)))
|
|
|
|
|
|
|
+ (if info
|
|
|
|
|
+ (progn (org-babel-expand-src-block current-prefix-arg info) t)
|
|
|
|
|
+ nil)))
|
|
|
|
|
|
|
|
(defadvice org-edit-special (around org-babel-prep-session-for-edit activate)
|
|
(defadvice org-edit-special (around org-babel-prep-session-for-edit activate)
|
|
|
"Prepare the current source block's session according to it's
|
|
"Prepare the current source block's session according to it's
|
|
@@ -75,7 +77,9 @@ to `org-open-at-point'."
|
|
|
then run `org-babel-load-in-session'."
|
|
then run `org-babel-load-in-session'."
|
|
|
(interactive)
|
|
(interactive)
|
|
|
(let ((info (org-babel-get-src-block-info)))
|
|
(let ((info (org-babel-get-src-block-info)))
|
|
|
- (if info (progn (org-babel-load-in-session current-prefix-arg info) t) nil)))
|
|
|
|
|
|
|
+ (if info
|
|
|
|
|
+ (progn (org-babel-load-in-session current-prefix-arg info) t)
|
|
|
|
|
+ nil)))
|
|
|
|
|
|
|
|
(add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
|
|
(add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
|
|
|
|
|
|
|
@@ -89,7 +93,8 @@ then run `org-babel-pop-to-session'."
|
|
|
(add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
|
|
(add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
|
|
|
|
|
|
|
|
(defconst org-babel-header-arg-names
|
|
(defconst org-babel-header-arg-names
|
|
|
- '(cache cmdline colnames dir exports file noweb results session tangle var)
|
|
|
|
|
|
|
+ '(cache cmdline colnames dir exports file noweb results
|
|
|
|
|
+ session tangle var noeval)
|
|
|
"Common header arguments used by org-babel. Note that
|
|
"Common header arguments used by org-babel. Note that
|
|
|
individual languages may define their own language specific
|
|
individual languages may define their own language specific
|
|
|
header arguments as well.")
|
|
header arguments as well.")
|
|
@@ -103,6 +108,9 @@ header arguments as well.")
|
|
|
'((:session . "none") (:results . "silent") (:exports . "results"))
|
|
'((:session . "none") (:results . "silent") (:exports . "results"))
|
|
|
"Default arguments to use when evaluating an inline source block.")
|
|
"Default arguments to use when evaluating an inline source block.")
|
|
|
|
|
|
|
|
|
|
+(defvar org-babel-current-buffer-properties)
|
|
|
|
|
+(make-variable-buffer-local 'org-babel-current-buffer-properties)
|
|
|
|
|
+
|
|
|
(defvar org-babel-src-block-regexp nil
|
|
(defvar org-babel-src-block-regexp nil
|
|
|
"Regexp used to test when inside of a org-babel src-block")
|
|
"Regexp used to test when inside of a org-babel src-block")
|
|
|
|
|
|
|
@@ -145,19 +153,19 @@ can not be resolved.")
|
|
|
(set-default var value)
|
|
(set-default var value)
|
|
|
(setq
|
|
(setq
|
|
|
org-babel-src-block-regexp
|
|
org-babel-src-block-regexp
|
|
|
- (concat "^[ \t]*#\\+begin_src[ \t]+\\(" ;; (1) lang
|
|
|
|
|
|
|
+ (concat "^\\([ \t]*\\)#\\+begin_src[ \t]+\\(" ;; (1) indentation (2) lang
|
|
|
(mapconcat 'regexp-quote value "\\|")
|
|
(mapconcat 'regexp-quote value "\\|")
|
|
|
"\\)[ \t]*"
|
|
"\\)[ \t]*"
|
|
|
- "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)" ;; (2) switches
|
|
|
|
|
- "\\([^\n]*\\)\n" ;; (3) header arguments
|
|
|
|
|
- "\\([^\000]+?\n\\)[ \t]*#\\+end_src"));; (4) body
|
|
|
|
|
|
|
+ "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)" ;; (3) switches
|
|
|
|
|
+ "\\([^\n]*\\)\n" ;; (4) header arguments
|
|
|
|
|
+ "\\([^\000]+?\n\\)[ \t]*#\\+end_src"));; (5) body
|
|
|
(setq org-babel-inline-src-block-regexp
|
|
(setq org-babel-inline-src-block-regexp
|
|
|
(concat "[ \f\t\n\r\v]\\(src_" ;; (1) replacement target
|
|
(concat "[ \f\t\n\r\v]\\(src_" ;; (1) replacement target
|
|
|
"\\(" ;; (2) lang
|
|
"\\(" ;; (2) lang
|
|
|
(mapconcat 'regexp-quote value "\\|")
|
|
(mapconcat 'regexp-quote value "\\|")
|
|
|
"\\)"
|
|
"\\)"
|
|
|
- "\\(\\|\\[\\(.*\\)\\]\\)" ;; (3,4) (unused, headers)
|
|
|
|
|
- "{\\([^\f\n\r\v]+\\)}" ;; (5) body
|
|
|
|
|
|
|
+ "\\(\\|\\[\\(.*?\\)\\]\\)" ;; (3,4) (unused, headers)
|
|
|
|
|
+ "{\\([^\f\n\r\v]+?\\)}" ;; (5) body
|
|
|
"\\)")))
|
|
"\\)")))
|
|
|
|
|
|
|
|
(defun org-babel-add-interpreter (interpreter)
|
|
(defun org-babel-add-interpreter (interpreter)
|
|
@@ -210,7 +218,6 @@ Optionally supply a value for PARAMS which will be merged with
|
|
|
the header arguments specified at the front of the source code
|
|
the header arguments specified at the front of the source code
|
|
|
block."
|
|
block."
|
|
|
(interactive)
|
|
(interactive)
|
|
|
- ;; (message "supplied params=%S" params) ;; debugging
|
|
|
|
|
(let* ((info (or info (org-babel-get-src-block-info)))
|
|
(let* ((info (or info (org-babel-get-src-block-info)))
|
|
|
(lang (first info))
|
|
(lang (first info))
|
|
|
(params (setf (third info)
|
|
(params (setf (third info)
|
|
@@ -225,7 +232,8 @@ block."
|
|
|
(body (setf (second info)
|
|
(body (setf (second info)
|
|
|
(if (and (cdr (assoc :noweb params))
|
|
(if (and (cdr (assoc :noweb params))
|
|
|
(string= "yes" (cdr (assoc :noweb params))))
|
|
(string= "yes" (cdr (assoc :noweb params))))
|
|
|
- (org-babel-expand-noweb-references info) (second info))))
|
|
|
|
|
|
|
+ (org-babel-expand-noweb-references info)
|
|
|
|
|
+ (second info))))
|
|
|
(result-params (split-string (or (cdr (assoc :results params)) "")))
|
|
(result-params (split-string (or (cdr (assoc :results params)) "")))
|
|
|
(result-type (cond ((member "output" result-params) 'output)
|
|
(result-type (cond ((member "output" result-params) 'output)
|
|
|
((member "value" result-params) 'value)
|
|
((member "value" result-params) 'value)
|
|
@@ -237,6 +245,7 @@ block."
|
|
|
(call-process-region-original
|
|
(call-process-region-original
|
|
|
(if (boundp 'call-process-region-original) call-process-region-original
|
|
(if (boundp 'call-process-region-original) call-process-region-original
|
|
|
(symbol-function 'call-process-region)))
|
|
(symbol-function 'call-process-region)))
|
|
|
|
|
+ (indent (car (last info)))
|
|
|
result)
|
|
result)
|
|
|
(unwind-protect
|
|
(unwind-protect
|
|
|
(flet ((call-process-region (&rest args)
|
|
(flet ((call-process-region (&rest args)
|
|
@@ -257,7 +266,7 @@ block."
|
|
|
(not (listp result)))
|
|
(not (listp result)))
|
|
|
(list (list result))
|
|
(list (list result))
|
|
|
result)))
|
|
result)))
|
|
|
- (org-babel-insert-result result result-params info new-hash)
|
|
|
|
|
|
|
+ (org-babel-insert-result result result-params info new-hash indent)
|
|
|
(run-hooks 'org-babel-after-execute-hook)
|
|
(run-hooks 'org-babel-after-execute-hook)
|
|
|
result))
|
|
result))
|
|
|
(setq call-process-region 'call-process-region-original))))
|
|
(setq call-process-region 'call-process-region-original))))
|
|
@@ -272,7 +281,6 @@ org-babel-expand-body:lang function." body)
|
|
|
"Expand the current source code block according to it's header
|
|
"Expand the current source code block according to it's header
|
|
|
arguments, and pop open the results in a preview buffer."
|
|
arguments, and pop open the results in a preview buffer."
|
|
|
(interactive)
|
|
(interactive)
|
|
|
- ;; (message "supplied params=%S" params) ;; debugging
|
|
|
|
|
(let* ((info (or info (org-babel-get-src-block-info)))
|
|
(let* ((info (or info (org-babel-get-src-block-info)))
|
|
|
(lang (first info))
|
|
(lang (first info))
|
|
|
(params (setf (third info)
|
|
(params (setf (third info)
|
|
@@ -349,7 +357,8 @@ results already exist."
|
|
|
(goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
|
|
(goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
|
|
|
(progn (org-babel-execute-src-block)
|
|
(progn (org-babel-execute-src-block)
|
|
|
(org-babel-where-is-src-block-result))))
|
|
(org-babel-where-is-src-block-result))))
|
|
|
- (end-of-line 1) (forward-char 1)
|
|
|
|
|
|
|
+ (end-of-line 1)
|
|
|
|
|
+ (while (looking-at "[\n\r\t\f ]") (forward-char 1))
|
|
|
;; open the results
|
|
;; open the results
|
|
|
(if (looking-at org-bracket-link-regexp)
|
|
(if (looking-at org-bracket-link-regexp)
|
|
|
;; file results
|
|
;; file results
|
|
@@ -384,45 +393,52 @@ the current buffer."
|
|
|
"Call `org-babel-execute-src-block' on every source block in
|
|
"Call `org-babel-execute-src-block' on every source block in
|
|
|
the current subtree."
|
|
the current subtree."
|
|
|
(interactive "P")
|
|
(interactive "P")
|
|
|
- (save-excursion
|
|
|
|
|
- (org-narrow-to-subtree)
|
|
|
|
|
- (org-babel-execute-buffer)
|
|
|
|
|
- (widen)))
|
|
|
|
|
|
|
+ (save-restriction
|
|
|
|
|
+ (save-excursion
|
|
|
|
|
+ (org-narrow-to-subtree)
|
|
|
|
|
+ (org-babel-execute-buffer)
|
|
|
|
|
+ (widen))))
|
|
|
|
|
|
|
|
(defun org-babel-get-src-block-info (&optional header-vars-only)
|
|
(defun org-babel-get-src-block-info (&optional header-vars-only)
|
|
|
"Get information of the current source block.
|
|
"Get information of the current source block.
|
|
|
Returns a list
|
|
Returns a list
|
|
|
- (language body header-arguments-alist switches name function-args).
|
|
|
|
|
|
|
+ (language body header-arguments-alist switches name function-args indent).
|
|
|
Unless HEADER-VARS-ONLY is non-nil, any variable
|
|
Unless HEADER-VARS-ONLY is non-nil, any variable
|
|
|
references provided in 'function call style' (i.e. in a
|
|
references provided in 'function call style' (i.e. in a
|
|
|
parenthesised argument list following the src block name) are
|
|
parenthesised argument list following the src block name) are
|
|
|
added to the header-arguments-alist."
|
|
added to the header-arguments-alist."
|
|
|
- (let ((case-fold-search t) head info args)
|
|
|
|
|
|
|
+ (let ((case-fold-search t) head info args indent)
|
|
|
(if (setq head (org-babel-where-is-src-block-head))
|
|
(if (setq head (org-babel-where-is-src-block-head))
|
|
|
(save-excursion
|
|
(save-excursion
|
|
|
(goto-char head)
|
|
(goto-char head)
|
|
|
(setq info (org-babel-parse-src-block-match))
|
|
(setq info (org-babel-parse-src-block-match))
|
|
|
|
|
+ (setq indent (car (last info)))
|
|
|
|
|
+ (setq info (butlast info))
|
|
|
(forward-line -1)
|
|
(forward-line -1)
|
|
|
- (when (looking-at
|
|
|
|
|
- (concat org-babel-source-name-regexp
|
|
|
|
|
- "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
|
|
|
|
|
- (setq info (append info (list (org-babel-clean-text-properties
|
|
|
|
|
- (match-string 2)))))
|
|
|
|
|
- ;; Note that e.g. "name()" and "name( )" result in ((:var . "")).
|
|
|
|
|
- ;; We maintain that behaviour, and the resulting non-nil sixth
|
|
|
|
|
- ;; element is relied upon in org-babel-exp-code to detect
|
|
|
|
|
- ;; a functional-style block in those cases. However,
|
|
|
|
|
- ;; "name" without any parentheses would result in the same
|
|
|
|
|
- ;; thing, so we explicitly avoid that.
|
|
|
|
|
- (if (setq args (match-string 4))
|
|
|
|
|
- (setq info
|
|
|
|
|
- (append info (list
|
|
|
|
|
- (mapcar (lambda (ref) (cons :var ref))
|
|
|
|
|
- (org-babel-ref-split-args args))))))
|
|
|
|
|
- (unless header-vars-only
|
|
|
|
|
- (setf (third info)
|
|
|
|
|
- (org-babel-merge-params (sixth info) (third info)))))
|
|
|
|
|
- info)
|
|
|
|
|
|
|
+ (if (looking-at
|
|
|
|
|
+ (concat org-babel-source-name-regexp
|
|
|
|
|
+ "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
|
|
|
|
|
+ (progn
|
|
|
|
|
+ (setq info (append info (list (org-babel-clean-text-properties
|
|
|
|
|
+ (match-string 2)))))
|
|
|
|
|
+ ;; Note that e.g. "name()" and "name( )" result in
|
|
|
|
|
+ ;; ((:var . "")). We maintain that behaviour, and the
|
|
|
|
|
+ ;; resulting non-nil sixth element is relied upon in
|
|
|
|
|
+ ;; org-babel-exp-code to detect a functional-style
|
|
|
|
|
+ ;; block in those cases. However, "name" without any
|
|
|
|
|
+ ;; parentheses would result in the same thing, so we
|
|
|
|
|
+ ;; explicitly avoid that.
|
|
|
|
|
+ (if (setq args (match-string 4))
|
|
|
|
|
+ (setq info
|
|
|
|
|
+ (append info (list
|
|
|
|
|
+ (mapcar
|
|
|
|
|
+ (lambda (ref) (cons :var ref))
|
|
|
|
|
+ (org-babel-ref-split-args args))))))
|
|
|
|
|
+ (unless header-vars-only
|
|
|
|
|
+ (setf (third info)
|
|
|
|
|
+ (org-babel-merge-params (sixth info) (third info)))))
|
|
|
|
|
+ (setq info (append info (list nil nil))))
|
|
|
|
|
+ (append info (list indent)))
|
|
|
(if (save-excursion ;; inline source block
|
|
(if (save-excursion ;; inline source block
|
|
|
(re-search-backward "[ \f\t\n\r\v]" nil t)
|
|
(re-search-backward "[ \f\t\n\r\v]" nil t)
|
|
|
(looking-at org-babel-inline-src-block-regexp))
|
|
(looking-at org-babel-inline-src-block-regexp))
|
|
@@ -478,7 +494,7 @@ added as the last element of the kill ring. This can be called
|
|
|
with C-c C-c."
|
|
with C-c C-c."
|
|
|
(interactive)
|
|
(interactive)
|
|
|
(let ((hash (car (delq nil (mapcar
|
|
(let ((hash (car (delq nil (mapcar
|
|
|
- (lambda (ol) (overlay-get ol 'babel-hash))
|
|
|
|
|
|
|
+ (lambda (ol) (overlay-get ol 'babel-hash))
|
|
|
(overlays-at (or point (point))))))))
|
|
(overlays-at (or point (point))))))))
|
|
|
(when hash (kill-new hash) (message hash))))
|
|
(when hash (kill-new hash) (message hash))))
|
|
|
(add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point)
|
|
(add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point)
|
|
@@ -557,7 +573,7 @@ with C-c C-c."
|
|
|
;; Remove overlays when changing major mode
|
|
;; Remove overlays when changing major mode
|
|
|
(add-hook 'org-mode-hook
|
|
(add-hook 'org-mode-hook
|
|
|
(lambda () (org-add-hook 'change-major-mode-hook
|
|
(lambda () (org-add-hook 'change-major-mode-hook
|
|
|
- 'org-babel-show-result-all 'append 'local)))
|
|
|
|
|
|
|
+ 'org-babel-show-result-all 'append 'local)))
|
|
|
|
|
|
|
|
(defmacro org-babel-map-source-blocks (file &rest body)
|
|
(defmacro org-babel-map-source-blocks (file &rest body)
|
|
|
"Evaluate BODY forms on each source-block in FILE."
|
|
"Evaluate BODY forms on each source-block in FILE."
|
|
@@ -597,11 +613,26 @@ may be specified in the properties of the current outline entry."
|
|
|
(setq sym (intern (concat "org-babel-header-arg-names:" lang)))
|
|
(setq sym (intern (concat "org-babel-header-arg-names:" lang)))
|
|
|
(and (boundp sym) (eval sym))))))))))
|
|
(and (boundp sym) (eval sym))))))))))
|
|
|
|
|
|
|
|
|
|
+(defun org-babel-params-from-buffer ()
|
|
|
|
|
+ "Return an association list of any source block params which
|
|
|
|
|
+may be specified at the top of the current buffer."
|
|
|
|
|
+ (or org-babel-current-buffer-properties
|
|
|
|
|
+ (setq org-babel-current-buffer-properties
|
|
|
|
|
+ (save-excursion
|
|
|
|
|
+ (save-restriction
|
|
|
|
|
+ (widen)
|
|
|
|
|
+ (goto-char (point-min))
|
|
|
|
|
+ (when (re-search-forward
|
|
|
|
|
+ (org-make-options-regexp (list "BABEL")) nil t)
|
|
|
|
|
+ (org-babel-parse-header-arguments
|
|
|
|
|
+ (org-match-string-no-properties 2))))))))
|
|
|
|
|
+
|
|
|
(defun org-babel-parse-src-block-match ()
|
|
(defun org-babel-parse-src-block-match ()
|
|
|
- (let* ((lang (org-babel-clean-text-properties (match-string 1)))
|
|
|
|
|
|
|
+ (let* ((block-indentation (length (match-string 1)))
|
|
|
|
|
+ (lang (org-babel-clean-text-properties (match-string 2)))
|
|
|
(lang-headers (intern (concat "org-babel-default-header-args:" lang)))
|
|
(lang-headers (intern (concat "org-babel-default-header-args:" lang)))
|
|
|
- (switches (match-string 2))
|
|
|
|
|
- (body (org-babel-clean-text-properties (match-string 4)))
|
|
|
|
|
|
|
+ (switches (match-string 3))
|
|
|
|
|
+ (body (org-babel-clean-text-properties (match-string 5)))
|
|
|
(preserve-indentation (or org-src-preserve-indentation
|
|
(preserve-indentation (or org-src-preserve-indentation
|
|
|
(string-match "-i\\>" switches))))
|
|
(string-match "-i\\>" switches))))
|
|
|
(list lang
|
|
(list lang
|
|
@@ -613,11 +644,13 @@ may be specified in the properties of the current outline entry."
|
|
|
(buffer-string)))
|
|
(buffer-string)))
|
|
|
(org-babel-merge-params
|
|
(org-babel-merge-params
|
|
|
org-babel-default-header-args
|
|
org-babel-default-header-args
|
|
|
|
|
+ (org-babel-params-from-buffer)
|
|
|
(org-babel-params-from-properties lang)
|
|
(org-babel-params-from-properties lang)
|
|
|
(if (boundp lang-headers) (eval lang-headers) nil)
|
|
(if (boundp lang-headers) (eval lang-headers) nil)
|
|
|
(org-babel-parse-header-arguments
|
|
(org-babel-parse-header-arguments
|
|
|
- (org-babel-clean-text-properties (or (match-string 3) ""))))
|
|
|
|
|
- switches)))
|
|
|
|
|
|
|
+ (org-babel-clean-text-properties (or (match-string 4) ""))))
|
|
|
|
|
+ switches
|
|
|
|
|
+ block-indentation)))
|
|
|
|
|
|
|
|
(defun org-babel-parse-inline-src-block-match ()
|
|
(defun org-babel-parse-inline-src-block-match ()
|
|
|
(let* ((lang (org-babel-clean-text-properties (match-string 2)))
|
|
(let* ((lang (org-babel-clean-text-properties (match-string 2)))
|
|
@@ -627,6 +660,7 @@ may be specified in the properties of the current outline entry."
|
|
|
(org-babel-clean-text-properties (match-string 5)))
|
|
(org-babel-clean-text-properties (match-string 5)))
|
|
|
(org-babel-merge-params
|
|
(org-babel-merge-params
|
|
|
org-babel-default-inline-header-args
|
|
org-babel-default-inline-header-args
|
|
|
|
|
+ (org-babel-params-from-buffer)
|
|
|
(org-babel-params-from-properties lang)
|
|
(org-babel-params-from-properties lang)
|
|
|
(if (boundp lang-headers) (eval lang-headers) nil)
|
|
(if (boundp lang-headers) (eval lang-headers) nil)
|
|
|
(org-babel-parse-header-arguments
|
|
(org-babel-parse-header-arguments
|
|
@@ -814,7 +848,7 @@ buffer or nil if no such result exists."
|
|
|
"[ \t]" (regexp-quote name) "[ \t\n\f\v\r]") nil t)
|
|
"[ \t]" (regexp-quote name) "[ \t\n\f\v\r]") nil t)
|
|
|
(beginning-of-line 0) (point))))
|
|
(beginning-of-line 0) (point))))
|
|
|
|
|
|
|
|
-(defun org-babel-where-is-src-block-result (&optional insert info hash)
|
|
|
|
|
|
|
+(defun org-babel-where-is-src-block-result (&optional insert info hash indent)
|
|
|
"Return the point at the beginning of the result of the current
|
|
"Return the point at the beginning of the result of the current
|
|
|
source block. Specifically at the beginning of the results line.
|
|
source block. Specifically at the beginning of the results line.
|
|
|
If no result exists for this block then create a results line
|
|
If no result exists for this block then create a results line
|
|
@@ -822,7 +856,8 @@ following the source block."
|
|
|
(save-excursion
|
|
(save-excursion
|
|
|
(let* ((on-lob-line (progn (beginning-of-line 1)
|
|
(let* ((on-lob-line (progn (beginning-of-line 1)
|
|
|
(looking-at org-babel-lob-one-liner-regexp)))
|
|
(looking-at org-babel-lob-one-liner-regexp)))
|
|
|
- (name (if on-lob-line (first (org-babel-lob-get-info))
|
|
|
|
|
|
|
+ (name (if on-lob-line
|
|
|
|
|
+ (first (org-babel-lob-get-info))
|
|
|
(fifth (or info (org-babel-get-src-block-info)))))
|
|
(fifth (or info (org-babel-get-src-block-info)))))
|
|
|
(head (unless on-lob-line (org-babel-where-is-src-block-head))) end)
|
|
(head (unless on-lob-line (org-babel-where-is-src-block-head))) end)
|
|
|
(when head (goto-char head))
|
|
(when head (goto-char head))
|
|
@@ -843,10 +878,16 @@ following the source block."
|
|
|
(goto-char end)
|
|
(goto-char end)
|
|
|
(if (looking-at "[\n\r]")
|
|
(if (looking-at "[\n\r]")
|
|
|
(forward-char 1) (insert "\n"))
|
|
(forward-char 1) (insert "\n"))
|
|
|
- (insert (concat "#+results"
|
|
|
|
|
- (when hash (concat "["hash"]"))
|
|
|
|
|
- ":"
|
|
|
|
|
- (when name (concat " " name)) "\n"))
|
|
|
|
|
|
|
+ (insert (concat
|
|
|
|
|
+ (if indent
|
|
|
|
|
+ (mapconcat
|
|
|
|
|
+ (lambda (el) " ")
|
|
|
|
|
+ (number-sequence 1 indent) "")
|
|
|
|
|
+ "")
|
|
|
|
|
+ "#+results"
|
|
|
|
|
+ (when hash (concat "["hash"]"))
|
|
|
|
|
+ ":"
|
|
|
|
|
+ (when name (concat " " name)) "\n"))
|
|
|
(beginning-of-line 0)
|
|
(beginning-of-line 0)
|
|
|
(if hash (org-babel-hide-hash)) t)))
|
|
(if hash (org-babel-hide-hash)) t)))
|
|
|
(point))))))
|
|
(point))))))
|
|
@@ -858,13 +899,13 @@ following the source block."
|
|
|
((org-at-table-p) (org-babel-read-table))
|
|
((org-at-table-p) (org-babel-read-table))
|
|
|
((looking-at org-bracket-link-regexp) (org-babel-read-link))
|
|
((looking-at org-bracket-link-regexp) (org-babel-read-link))
|
|
|
((looking-at org-block-regexp) (org-babel-trim (match-string 4)))
|
|
((looking-at org-block-regexp) (org-babel-trim (match-string 4)))
|
|
|
- ((looking-at ": ")
|
|
|
|
|
|
|
+ ((looking-at "^[ \t]*: ")
|
|
|
(setq result-string
|
|
(setq result-string
|
|
|
(org-babel-trim
|
|
(org-babel-trim
|
|
|
(mapconcat (lambda (line)
|
|
(mapconcat (lambda (line)
|
|
|
(if (and (> (length line) 1)
|
|
(if (and (> (length line) 1)
|
|
|
- (string= ": " (substring line 0 2)))
|
|
|
|
|
- (substring line 2)
|
|
|
|
|
|
|
+ (string-match "^[ \t]*: \\(.+\\)" line))
|
|
|
|
|
+ (match-string 1 line)
|
|
|
line))
|
|
line))
|
|
|
(split-string
|
|
(split-string
|
|
|
(buffer-substring
|
|
(buffer-substring
|
|
@@ -896,7 +937,7 @@ link is a file path it is expanded using `expand-file-name'."
|
|
|
(expand-file-name (match-string 2 raw))))
|
|
(expand-file-name (match-string 2 raw))))
|
|
|
(t raw))))
|
|
(t raw))))
|
|
|
|
|
|
|
|
-(defun org-babel-insert-result (result &optional result-params info hash)
|
|
|
|
|
|
|
+(defun org-babel-insert-result (result &optional result-params info hash indent)
|
|
|
"Insert RESULT into the current buffer after the end of the
|
|
"Insert RESULT into the current buffer after the end of the
|
|
|
current source block. With optional argument RESULT-PARAMS
|
|
current source block. With optional argument RESULT-PARAMS
|
|
|
controls insertion of results in the org-mode file.
|
|
controls insertion of results in the org-mode file.
|
|
@@ -934,38 +975,50 @@ code ---- the results are extracted in the syntax of the source
|
|
|
(when (member "file" result-params)
|
|
(when (member "file" result-params)
|
|
|
(setq result (org-babel-result-to-file result))))
|
|
(setq result (org-babel-result-to-file result))))
|
|
|
(unless (listp result) (setq result (format "%S" result))))
|
|
(unless (listp result) (setq result (format "%S" result))))
|
|
|
- (if (and result-params (member "replace" result-params)
|
|
|
|
|
- (not (member "silent" result-params)))
|
|
|
|
|
- (org-babel-remove-result info))
|
|
|
|
|
(if (= (length result) 0)
|
|
(if (= (length result) 0)
|
|
|
(if (member "value" result-params)
|
|
(if (member "value" result-params)
|
|
|
(message "No result returned by source block")
|
|
(message "No result returned by source block")
|
|
|
(message "Source block produced no output"))
|
|
(message "Source block produced no output"))
|
|
|
(if (and result-params (member "silent" result-params))
|
|
(if (and result-params (member "silent" result-params))
|
|
|
- (progn (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
|
|
|
|
|
- result)
|
|
|
|
|
|
|
+ (progn
|
|
|
|
|
+ (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
|
|
|
|
|
+ result)
|
|
|
(when (and (stringp result) ;; ensure results end in a newline
|
|
(when (and (stringp result) ;; ensure results end in a newline
|
|
|
(not (or (string-equal (substring result -1) "\n")
|
|
(not (or (string-equal (substring result -1) "\n")
|
|
|
(string-equal (substring result -1) "\r"))))
|
|
(string-equal (substring result -1) "\r"))))
|
|
|
(setq result (concat result "\n")))
|
|
(setq result (concat result "\n")))
|
|
|
(save-excursion
|
|
(save-excursion
|
|
|
- (let ((existing-result (org-babel-where-is-src-block-result t info hash))
|
|
|
|
|
|
|
+ (let ((existing-result (org-babel-where-is-src-block-result
|
|
|
|
|
+ t info hash indent))
|
|
|
(results-switches
|
|
(results-switches
|
|
|
- (cdr (assoc :results_switches (third info)))) beg)
|
|
|
|
|
- (when existing-result (goto-char existing-result) (forward-line 1))
|
|
|
|
|
|
|
+ (cdr (assoc :results_switches (third info))))
|
|
|
|
|
+ beg end)
|
|
|
|
|
+ (when existing-result
|
|
|
|
|
+ (goto-char existing-result)
|
|
|
|
|
+ (save-excursion
|
|
|
|
|
+ (re-search-forward "#" nil t)
|
|
|
|
|
+ (setq indent (- (current-column) 1)))
|
|
|
|
|
+ (forward-line 1)
|
|
|
|
|
+ (setq beg (point))
|
|
|
|
|
+ (cond
|
|
|
|
|
+ ((member "replace" result-params)
|
|
|
|
|
+ (delete-region (point) (org-babel-result-end)))
|
|
|
|
|
+ ((member "append" result-params)
|
|
|
|
|
+ (goto-char (org-babel-result-end)) (setq beg (point)))
|
|
|
|
|
+ ((member "prepend" result-params) ;; already there
|
|
|
|
|
+ )))
|
|
|
(setq results-switches
|
|
(setq results-switches
|
|
|
(if results-switches (concat " " results-switches) ""))
|
|
(if results-switches (concat " " results-switches) ""))
|
|
|
(cond
|
|
(cond
|
|
|
;; assume the result is a table if it's not a string
|
|
;; assume the result is a table if it's not a string
|
|
|
((not (stringp result))
|
|
((not (stringp result))
|
|
|
- (setq beg (point))
|
|
|
|
|
(insert (concat (orgtbl-to-orgtbl
|
|
(insert (concat (orgtbl-to-orgtbl
|
|
|
(if (or (eq 'hline (car result))
|
|
(if (or (eq 'hline (car result))
|
|
|
(and (listp (car result))
|
|
(and (listp (car result))
|
|
|
(listp (cdr (car result)))))
|
|
(listp (cdr (car result)))))
|
|
|
result (list result))
|
|
result (list result))
|
|
|
'(:fmt (lambda (cell) (format "%s" cell)))) "\n"))
|
|
'(:fmt (lambda (cell) (format "%s" cell)))) "\n"))
|
|
|
- (goto-char beg) (org-cycle))
|
|
|
|
|
|
|
+ (goto-char beg) (org-table-align))
|
|
|
((member "file" result-params)
|
|
((member "file" result-params)
|
|
|
(insert result))
|
|
(insert result))
|
|
|
((member "html" result-params)
|
|
((member "html" result-params)
|
|
@@ -981,7 +1034,14 @@ code ---- the results are extracted in the syntax of the source
|
|
|
(save-excursion (insert result)) (if (org-at-table-p) (org-cycle)))
|
|
(save-excursion (insert result)) (if (org-at-table-p) (org-cycle)))
|
|
|
(t
|
|
(t
|
|
|
(org-babel-examplize-region
|
|
(org-babel-examplize-region
|
|
|
- (point) (progn (insert result) (point)) results-switches)))))
|
|
|
|
|
|
|
+ (point) (progn (insert result) (point)) results-switches)))
|
|
|
|
|
+ ;; possibly indent the results to match the #+results line
|
|
|
|
|
+ (setq end (if (listp result) (org-table-end) (point)))
|
|
|
|
|
+ (when (and indent (> indent 0)
|
|
|
|
|
+ ;; in this case `table-align' does the work for us
|
|
|
|
|
+ (not (and (listp result)
|
|
|
|
|
+ (member "append" result-params))))
|
|
|
|
|
+ (indent-rigidly beg end indent))))
|
|
|
(message "finished"))))
|
|
(message "finished"))))
|
|
|
|
|
|
|
|
(defun org-babel-result-to-org-string (result)
|
|
(defun org-babel-result-to-org-string (result)
|
|
@@ -1005,19 +1065,19 @@ relies on `org-babel-insert-result'."
|
|
|
(progn (goto-char (org-table-end)) (point))
|
|
(progn (goto-char (org-table-end)) (point))
|
|
|
(let ((case-fold-search t))
|
|
(let ((case-fold-search t))
|
|
|
(cond
|
|
(cond
|
|
|
- ((looking-at "#\\+begin_latex")
|
|
|
|
|
- (search-forward "#+end_latex" nil t)
|
|
|
|
|
|
|
+ ((looking-at "[ \t]*#\\+begin_latex")
|
|
|
|
|
+ (search-forward "[ \t]*#\\+end_latex" nil t)
|
|
|
(forward-line 1))
|
|
(forward-line 1))
|
|
|
- ((looking-at "#\\+begin_html")
|
|
|
|
|
- (search-forward "#+end_html" nil t)
|
|
|
|
|
|
|
+ ((looking-at "[ \t]*#\\+begin_html")
|
|
|
|
|
+ (search-forward "[ \t]*#\\+end_html" nil t)
|
|
|
(forward-line 1))
|
|
(forward-line 1))
|
|
|
- ((looking-at "#\\+begin_example")
|
|
|
|
|
- (search-forward "#+end_example" nil t)
|
|
|
|
|
|
|
+ ((looking-at "[ \t]*#\\+begin_example")
|
|
|
|
|
+ (search-forward "[ \t]*#\\+end_example" nil t)
|
|
|
(forward-line 1))
|
|
(forward-line 1))
|
|
|
- ((looking-at "#\\+begin_src")
|
|
|
|
|
- (search-forward "#+end_src" nil t)
|
|
|
|
|
|
|
+ ((looking-at "[ \t]*#\\+begin_src")
|
|
|
|
|
+ (search-forward "[ \t]*#\\+end_src" nil t)
|
|
|
(forward-line 1))
|
|
(forward-line 1))
|
|
|
- (t (progn (while (looking-at "\\(: \\|\\[\\[\\)")
|
|
|
|
|
|
|
+ (t (progn (while (looking-at "[ \t]*\\(: \\|\\[\\[\\)")
|
|
|
(forward-line 1))))))
|
|
(forward-line 1))))))
|
|
|
(point))))
|
|
(point))))
|
|
|
|
|
|
|
@@ -1063,7 +1123,7 @@ parameters when merging lists."
|
|
|
(let ((results-exclusive-groups
|
|
(let ((results-exclusive-groups
|
|
|
'(("file" "vector" "table" "scalar" "raw" "org"
|
|
'(("file" "vector" "table" "scalar" "raw" "org"
|
|
|
"html" "latex" "code" "pp")
|
|
"html" "latex" "code" "pp")
|
|
|
- ("replace" "silent")
|
|
|
|
|
|
|
+ ("replace" "silent" "append" "prepend")
|
|
|
("output" "value")))
|
|
("output" "value")))
|
|
|
(exports-exclusive-groups
|
|
(exports-exclusive-groups
|
|
|
'(("code" "results" "both" "none")))
|
|
'(("code" "results" "both" "none")))
|
|
@@ -1218,21 +1278,24 @@ block but are passed literally to the \"example-block\"."
|
|
|
(let ((raw (org-babel-ref-resolve-reference
|
|
(let ((raw (org-babel-ref-resolve-reference
|
|
|
source-name nil)))
|
|
source-name nil)))
|
|
|
(if (stringp raw) raw (format "%S" raw)))
|
|
(if (stringp raw) raw (format "%S" raw)))
|
|
|
- (let ((point (org-babel-find-named-block source-name)))
|
|
|
|
|
- (if point
|
|
|
|
|
- (save-excursion
|
|
|
|
|
- (goto-char point)
|
|
|
|
|
- (org-babel-trim
|
|
|
|
|
- (org-babel-expand-noweb-references
|
|
|
|
|
- (org-babel-get-src-block-info))))
|
|
|
|
|
- ;; optionally raise an error if named
|
|
|
|
|
- ;; source-block doesn't exist
|
|
|
|
|
- (if (member lang org-babel-noweb-error-langs)
|
|
|
|
|
- (error
|
|
|
|
|
- (concat "<<%s>> could not be resolved "
|
|
|
|
|
- "(see `org-babel-noweb-error-langs')")
|
|
|
|
|
- source-name)
|
|
|
|
|
- "")))) "[\n\r]") (concat "\n" prefix)))))
|
|
|
|
|
|
|
+ (save-restriction
|
|
|
|
|
+ (widen)
|
|
|
|
|
+ (let ((point (org-babel-find-named-block source-name)))
|
|
|
|
|
+ (if point
|
|
|
|
|
+ (save-excursion
|
|
|
|
|
+ (goto-char point)
|
|
|
|
|
+ (org-babel-trim
|
|
|
|
|
+ (org-babel-expand-noweb-references
|
|
|
|
|
+ (org-babel-get-src-block-info))))
|
|
|
|
|
+ ;; optionally raise an error if named
|
|
|
|
|
+ ;; source-block doesn't exist
|
|
|
|
|
+ (if (member lang org-babel-noweb-error-langs)
|
|
|
|
|
+ (error
|
|
|
|
|
+ (concat "<<%s>> could not be resolved "
|
|
|
|
|
+ "(see `org-babel-noweb-error-langs')")
|
|
|
|
|
+ source-name)
|
|
|
|
|
+ "")))))
|
|
|
|
|
+ "[\n\r]") (concat "\n" prefix)))))
|
|
|
(nb-add (buffer-substring index (point-max)))))
|
|
(nb-add (buffer-substring index (point-max)))))
|
|
|
new-body))
|
|
new-body))
|
|
|
|
|
|
|
@@ -1272,7 +1335,9 @@ This is taken almost directly from `org-read-prop'."
|
|
|
(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"
|
|
|
(if (and (string-match "^-?[0-9]*\\.?[0-9]*$" string)
|
|
(if (and (string-match "^-?[0-9]*\\.?[0-9]*$" string)
|
|
|
- (= (match-end 0) (length string)))
|
|
|
|
|
|
|
+ (= (length (substring string (match-beginning 0)
|
|
|
|
|
+ (match-end 0)))
|
|
|
|
|
+ (length string)))
|
|
|
(string-to-number string)))
|
|
(string-to-number string)))
|
|
|
|
|
|
|
|
(defun org-babel-import-elisp-from-file (file-name)
|
|
(defun org-babel-import-elisp-from-file (file-name)
|