babel: Use file-name-as-directory to form default-directory
[org-mode/org-jambu.git] / contrib / babel / lisp / org-babel.el
blob0142deeb9349fade51041c0f64ca93fa4209bda8
1 ;;; org-babel.el --- facilitating communication between programming languages and people
3 ;; Copyright (C) 2009 Eric Schulte, Dan Davison
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
10 ;;; License:
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; See org-babel.org in the parent directory for more information
31 ;;; Code:
32 (require 'org)
34 (defun org-babel-execute-src-block-maybe ()
35 "Detect if this is context for a org-babel src-block and if so
36 then run `org-babel-execute-src-block'."
37 (interactive)
38 (let ((info (org-babel-get-src-block-info)))
39 (if info (progn (org-babel-execute-src-block current-prefix-arg info) t) nil)))
41 (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-src-block-maybe)
43 (defadvice org-edit-special (around org-babel-prep-session-for-edit activate)
44 "Prepare the current source block's session according to it's
45 header arguments before editing in an org-src buffer. This
46 function is called when `org-edit-special' is called with a
47 prefix argument from inside of a source-code block."
48 (when current-prefix-arg
49 (let* ((info (org-babel-get-src-block-info))
50 (lang (first info))
51 (params (third info))
52 (session (cdr (assoc :session params))))
53 (when (and info session) ;; if we are in a source-code block which has a session
54 (funcall (intern (concat "org-babel-prep-session:" lang)) session params))))
55 ad-do-it)
57 (defadvice org-open-at-point (around org-babel-open-at-point activate)
58 "If `point' is on a source code block, then open that block's
59 results with `org-babel-open-src-block-results', otherwise defer
60 to `org-open-at-point'."
61 (interactive "P")
62 (or (call-interactively #'org-babel-open-src-block-result) ad-do-it))
64 (defun org-babel-load-in-session-maybe ()
65 "Detect if this is context for a org-babel src-block and if so
66 then run `org-babel-load-in-session'."
67 (interactive)
68 (let ((info (org-babel-get-src-block-info)))
69 (if info (progn (org-babel-load-in-session current-prefix-arg info) t) nil)))
71 (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
73 (defun org-babel-pop-to-session-maybe ()
74 "Detect if this is context for a org-babel src-block and if so
75 then run `org-babel-pop-to-session'."
76 (interactive)
77 (let ((info (org-babel-get-src-block-info)))
78 (if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil)))
80 (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
82 (defconst org-babel-header-arg-names
83 '(cache cmdline colnames dir exports file noweb results session tangle var)
84 "Common header arguments used by org-babel. Note that
85 individual languages may define their own language specific
86 header arguments as well.")
88 (defvar org-babel-default-header-args
89 '((:session . "none") (:results . "replace") (:exports . "code") (:cache . "no") (:noweb . "no"))
90 "Default arguments to use when evaluating a source block.")
92 (defvar org-babel-default-inline-header-args
93 '((:session . "none") (:results . "silent") (:exports . "results"))
94 "Default arguments to use when evaluating an inline source block.")
96 (defvar org-babel-src-block-regexp nil
97 "Regexp used to test when inside of a org-babel src-block")
99 (defvar org-babel-inline-src-block-regexp nil
100 "Regexp used to test when on an inline org-babel src-block")
102 (defvar org-babel-result-regexp
103 "^[ \t]*#\\+res\\(ults\\|name\\)\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:"
104 "Regular expression used to match result lines. If the
105 results are associated with a hash key then the hash will be
106 saved in the second match data.")
108 (defvar org-babel-source-name-regexp
109 "^[ \t]*#\\+\\(srcname\\|source\\|function\\):[ \t]*"
110 "Regular expression used to match a source name line.")
112 (defvar org-babel-min-lines-for-block-output 10
113 "If number of lines of output is equal to or exceeds this
114 value, the output is placed in a #+begin_example...#+end_example
115 block. Otherwise the output is marked as literal by inserting
116 colons at the starts of the lines. This variable only takes
117 effect if the :results output option is in effect.")
119 (defvar org-babel-noweb-error-langs nil
120 "List of language for which errors should be raised when the
121 source code block satisfying a noweb reference in this language
122 can not be resolved.")
124 (defvar org-babel-hash-show 4
125 "Number of initial characters to show of a hidden results hash.")
127 (defun org-babel-named-src-block-regexp-for-name (name)
128 "Regexp used to match named src block."
129 (concat org-babel-source-name-regexp (regexp-quote name) "[ \t\n]*"
130 (substring org-babel-src-block-regexp 1)))
132 (defun org-babel-set-interpreters (var value)
133 (set-default var value)
134 (setq org-babel-src-block-regexp
135 (concat "^[ \t]*#\\+begin_src[ \t]+\\(" ;; (1) lang
136 (mapconcat 'regexp-quote value "\\|")
137 "\\)[ \t]*"
138 "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)" ;; (2) switches
139 "\\([^\n]*\\)\n" ;; (3) header arguments
140 "\\([^\000]+?\\)#\\+end_src")) ;; (4) body
141 (setq org-babel-inline-src-block-regexp
142 (concat "[ \f\t\n\r\v]\\(src_" ;; (1) replacement target
143 "\\(" ;; (2) lang
144 (mapconcat 'regexp-quote value "\\|")
145 "\\)"
146 "\\(\\|\\[\\(.*\\)\\]\\)" ;; (3,4) (unused, headers)
147 "{\\([^\f\n\r\v]+\\)}" ;; (5) body
148 "\\)")))
150 (defun org-babel-add-interpreter (interpreter)
151 "Add INTERPRETER to `org-babel-interpreters' and update
152 `org-babel-src-block-regexp' appropriately."
153 (unless (member interpreter org-babel-interpreters)
154 (setq org-babel-interpreters (cons interpreter org-babel-interpreters))
155 (org-babel-set-interpreters 'org-babel-interpreters org-babel-interpreters)))
157 (defcustom org-babel-interpreters '()
158 "Interpreters allows for evaluation tags.
159 This is a list of program names (as strings) that can evaluate code and
160 insert the output into an Org-mode buffer. Valid choices are
162 R Evaluate R code
163 emacs-lisp Evaluate Emacs Lisp code and display the result
164 sh Pass command to the shell and display the result
165 perl The perl interpreter
166 python The python interpreter
167 ruby The ruby interpreter
169 The source block regexp `org-babel-src-block-regexp' is updated
170 when a new interpreter is added to this list through the
171 customize interface. To add interpreters to this variable from
172 lisp code use the `org-babel-add-interpreter' function."
173 :group 'org-babel
174 :set 'org-babel-set-interpreters
175 :type '(set :greedy t
176 (const "R")
177 (const "emacs-lisp")
178 (const "sh")
179 (const "perl")
180 (const "python")
181 (const "ruby")))
183 ;;; functions
184 (defun org-babel-execute-src-block (&optional arg info params)
185 "Execute the current source code block, and insert the results
186 into the buffer. Source code execution and the collection and
187 formatting of results can be controlled through a variety of
188 header arguments.
190 Optionally supply a value for INFO in the form returned by
191 `org-babel-get-src-block-info'.
193 Optionally supply a value for PARAMS which will be merged with
194 the header arguments specified at the front of the source code
195 block."
196 (interactive)
197 ;; (message "supplied params=%S" params) ;; debugging
198 (let* ((info (or info (org-babel-get-src-block-info)))
199 (lang (first info))
200 (params (setf (third info)
201 (sort (org-babel-merge-params (third info) params)
202 (lambda (el1 el2) (string< (symbol-name (car el1))
203 (symbol-name (car el2)))))))
204 (new-hash (if (and (cdr (assoc :cache params))
205 (string= "yes" (cdr (assoc :cache params)))) (org-babel-sha1-hash info)))
206 (old-hash (org-babel-result-hash info))
207 (body (setf (second info)
208 (if (and (cdr (assoc :noweb params))
209 (string= "yes" (cdr (assoc :noweb params))))
210 (org-babel-expand-noweb-references info) (second info))))
212 (result-params (split-string (or (cdr (assoc :results params)) "")))
213 (result-type (cond ((member "output" result-params) 'output)
214 ((member "value" result-params) 'value)
215 (t 'value)))
216 (cmd (intern (concat "org-babel-execute:" lang)))
217 (dir (cdr (assoc :dir params)))
218 (default-directory
219 (or (and dir (file-name-as-directory dir)) default-directory))
220 (call-process-region-original
221 (if (boundp 'call-process-region-original) call-process-region-original
222 (symbol-function 'call-process-region)))
223 result)
224 ;; (message "params=%S" params) ;; debugging
225 (flet ((call-process-region (&rest args)
226 (apply 'org-babel-tramp-handle-call-process-region args)))
227 (unless (member lang org-babel-interpreters)
228 (error "Language is not in `org-babel-interpreters': %s" lang))
229 (if (and (not arg) new-hash (equal new-hash old-hash))
230 (save-excursion ;; return cached result
231 (goto-char (org-babel-where-is-src-block-result nil info))
232 (move-end-of-line 1) (forward-char 1)
233 (setq result (org-babel-read-result))
234 (message (replace-regexp-in-string "%" "%%" (format "%S" result))) result)
235 (setq result (funcall cmd body params))
236 (if (eq result-type 'value)
237 (setq result (if (and (or (member "vector" result-params)
238 (member "table" result-params))
239 (not (listp result)))
240 (list (list result))
241 result)))
242 (org-babel-insert-result result result-params info new-hash)
243 result))))
245 (defun org-babel-load-in-session (&optional arg info)
246 "Load the body of the current source-code block. Evaluate the
247 header arguments for the source block before entering the
248 session. After loading the body this pops open the session."
249 (interactive)
250 (let* ((info (or info (org-babel-get-src-block-info)))
251 (lang (first info))
252 (body (second info))
253 (params (third info))
254 (session (cdr (assoc :session params))))
255 (unless (member lang org-babel-interpreters)
256 (error "Language is not in `org-babel-interpreters': %s" lang))
257 ;; if called with a prefix argument, then process header arguments
258 (pop-to-buffer (funcall (intern (concat "org-babel-load-session:" lang)) session body params))
259 (move-end-of-line 1)))
261 (defun org-babel-pop-to-session (&optional arg info)
262 "Pop to the session of the current source-code block. If
263 called with a prefix argument then evaluate the header arguments
264 for the source block before entering the session. Copy the body
265 of the source block to the kill ring."
266 (interactive)
267 (let* ((info (or info (org-babel-get-src-block-info)))
268 (lang (first info))
269 (body (second info))
270 (params (third info))
271 (session (cdr (assoc :session params))))
272 (unless (member lang org-babel-interpreters)
273 (error "Language is not in `org-babel-interpreters': %s" lang))
274 ;; copy body to the kill ring
275 (with-temp-buffer (insert (org-babel-trim body)) (copy-region-as-kill (point-min) (point-max)))
276 ;; if called with a prefix argument, then process header arguments
277 (if arg (funcall (intern (concat "org-babel-prep-session:" lang)) session params))
278 ;; just to the session using pop-to-buffer
279 (pop-to-buffer (funcall (intern (format "org-babel-%s-initiate-session" lang)) session))
280 (move-end-of-line 1)))
282 (defun org-babel-open-src-block-result (&optional re-run)
283 "If `point' is on a src block then open the results of the
284 source code block, otherwise return nil. With optional prefix
285 argument RE-RUN the source-code block is evaluated even if
286 results already exist."
287 (interactive "P")
288 (when (org-babel-get-src-block-info)
289 (save-excursion
290 ;; go to the results, if there aren't any then run the block
291 (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
292 (progn (org-babel-execute-src-block)
293 (org-babel-where-is-src-block-result))))
294 (move-end-of-line 1) (forward-char 1)
295 ;; open the results
296 (if (looking-at org-bracket-link-regexp)
297 ;; file results
298 (org-open-at-point)
299 (let ((results (org-babel-read-result)))
300 (flet ((echo-res (result)
301 (if (stringp result) result (format "%S" result))))
302 (pop-to-buffer (get-buffer-create "org-babel-results"))
303 (delete-region (point-min) (point-max))
304 (if (listp results)
305 ;; table result
306 (insert (orgtbl-to-generic results '(:sep "\t" :fmt echo-res)))
307 ;; scalar result
308 (insert (echo-res results))))))
309 t)))
311 (defun org-babel-execute-buffer (&optional arg)
312 "Call `org-babel-execute-src-block' on every source block in
313 the current buffer."
314 (interactive "P")
315 (save-excursion
316 (goto-char (point-min))
317 (while (re-search-forward org-babel-src-block-regexp nil t)
318 (let ((pos-end (match-end 0)))
319 (goto-char (match-beginning 0))
320 (org-babel-execute-src-block arg)
321 (goto-char pos-end)))))
323 (defun org-babel-execute-subtree (&optional arg)
324 "Call `org-babel-execute-src-block' on every source block in
325 the current subtree."
326 (interactive "P")
327 (save-excursion
328 (org-narrow-to-subtree)
329 (org-babel-execute-buffer)
330 (widen)))
332 (defun org-babel-get-src-block-info (&optional header-vars-only)
333 "Get information of the current source block.
334 Returns a list
335 (language body header-arguments-alist switches name function-args).
336 Unless HEADER-VARS-ONLY is non-nil, any variable
337 references provided in 'function call style' (i.e. in a
338 parenthesised argument list following the src block name) are
339 added to the header-arguments-alist."
340 (let ((case-fold-search t) head info args)
341 (if (setq head (org-babel-where-is-src-block-head))
342 (save-excursion
343 (goto-char head)
344 (setq info (org-babel-parse-src-block-match))
345 (forward-line -1)
346 (when (looking-at (concat org-babel-source-name-regexp
347 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
348 (setq info (append info (list (org-babel-clean-text-properties (match-string 2)))))
349 ;; Note that e.g. "name()" and "name( )" result in ((:var . "")).
350 ;; We maintain that behaviour, and the resulting non-nil sixth
351 ;; element is relied upon in org-babel-exp-code to detect a functional-style
352 ;; block in those cases. However, "name" without any
353 ;; parentheses would result in the same thing, so we
354 ;; explicitly avoid that.
355 (if (setq args (match-string 4))
356 (setq info (append info (list (mapcar (lambda (ref) (cons :var ref))
357 (org-babel-ref-split-args args))))))
358 (unless header-vars-only
359 (setf (third info)
360 (org-babel-merge-params (sixth info) (third info)))))
361 info)
362 (if (save-excursion ;; inline source block
363 (re-search-backward "[ \f\t\n\r\v]" nil t)
364 (looking-at org-babel-inline-src-block-regexp))
365 (org-babel-parse-inline-src-block-match)
366 nil)))) ;; indicate that no source block was found
368 (defun org-babel-sha1-hash (&optional info)
369 (interactive)
370 (let* ((info (or info (org-babel-get-src-block-info)))
371 (hash (sha1 (format "%s-%s" (mapconcat (lambda (arg) (format "%S" arg))
372 (third info) ":")
373 (second info)))))
374 (when (interactive-p) (message hash))
375 hash))
377 (defun org-babel-result-hash (&optional info)
378 (org-babel-where-is-src-block-result nil info)
379 (org-babel-clean-text-properties (match-string 3)))
381 (defun org-babel-hide-hash ()
382 "Hide the hash in the current results line. Only the initial
383 `org-babel-hash-show' characters of the hash will remain
384 visible."
385 (org-add-to-invisibility-spec '(org-babel-hide-hash . t))
386 (save-excursion
387 (when (and (re-search-forward org-babel-result-regexp nil t)
388 (match-string 3))
389 (let* ((start (match-beginning 3))
390 (hide-start (+ org-babel-hash-show start))
391 (end (match-end 3))
392 (hash (match-string 3))
393 ov1 ov2)
394 (setq ov1 (org-make-overlay start hide-start))
395 (setq ov2 (org-make-overlay hide-start end))
396 (org-overlay-put ov2 'invisible 'org-babel-hide-hash)
397 (org-overlay-put ov1 'babel-hash hash)))))
399 (defun org-babel-hide-all-hashes ()
400 "Hide the hash in the current buffer. Only the initial
401 `org-babel-hash-show' characters of each hash will remain
402 visible. This function should be called as part of the
403 `org-mode-hook'."
404 (save-excursion
405 (while (re-search-forward org-babel-result-regexp nil t)
406 (goto-char (match-beginning 0))
407 (org-babel-hide-hash)
408 (goto-char (match-end 0)))))
409 (add-hook 'org-mode-hook 'org-babel-hide-all-hashes)
411 (defun org-babel-hash-at-point (&optional point)
412 "Return the value of the hash at `point'. The hash is also
413 added as the last element of the kill ring. This can be called
414 with C-c C-c."
415 (interactive)
416 (let ((hash (car (delq nil (mapcar
417 (lambda (ol) (org-overlay-get ol 'babel-hash))
418 (org-overlays-at (or point (point))))))))
419 (when hash (kill-new hash) (message hash))))
420 (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point)
422 (defun org-babel-result-hide-spec ()
423 (org-add-to-invisibility-spec '(org-babel-hide-result . t)))
424 (add-hook 'org-mode-hook 'org-babel-result-hide-spec)
426 (defvar org-babel-hide-result-overlays nil
427 "Overlays hiding results.")
429 (defun org-babel-result-hide-all ()
430 "Fold all results in the current buffer."
431 (interactive)
432 (org-babel-show-result-all)
433 (save-excursion
434 (while (re-search-forward org-babel-result-regexp nil t)
435 (save-excursion (goto-char (match-beginning 0))
436 (org-babel-hide-result-toggle-maybe)))))
438 (defun org-babel-show-result-all ()
439 "Unfold all results in the current buffer."
440 (mapc 'org-delete-overlay org-babel-hide-result-overlays)
441 (setq org-babel-hide-result-overlays nil))
443 (defun org-babel-hide-result-toggle-maybe ()
444 "Toggle visibility of result at point."
445 (interactive)
446 (let ((case-fold-search t))
447 (if (save-excursion
448 (beginning-of-line 1)
449 (looking-at org-babel-result-regexp))
450 (progn (org-babel-hide-result-toggle)
451 t) ;; to signal that we took action
452 nil))) ;; to signal that we did not
454 (defun org-babel-hide-result-toggle (&optional force)
455 "Toggle the visibility of the current result."
456 (interactive)
457 (save-excursion
458 (beginning-of-line)
459 (if (re-search-forward org-babel-result-regexp nil t)
460 (let ((start (progn (beginning-of-line 2) (- (point) 1)))
461 (end (progn (goto-char (- (org-babel-result-end) 1)) (point)))
463 (if (memq t (mapcar (lambda (overlay)
464 (eq (org-overlay-get overlay 'invisible)
465 'org-babel-hide-result))
466 (org-overlays-at start)))
467 (if (or (not force) (eq force 'off))
468 (mapc (lambda (ov)
469 (when (member ov org-babel-hide-result-overlays)
470 (setq org-babel-hide-result-overlays
471 (delq ov org-babel-hide-result-overlays)))
472 (when (eq (org-overlay-get ov 'invisible)
473 'org-babel-hide-result)
474 (org-delete-overlay ov)))
475 (org-overlays-at start)))
476 (setq ov (org-make-overlay start end))
477 (org-overlay-put ov 'invisible 'org-babel-hide-result)
478 ;; make the block accessible to isearch
479 (org-overlay-put
480 ov 'isearch-open-invisible
481 (lambda (ov)
482 (when (member ov org-babel-hide-result-overlays)
483 (setq org-babel-hide-result-overlays
484 (delq ov org-babel-hide-result-overlays)))
485 (when (eq (org-overlay-get ov 'invisible)
486 'org-babel-hide-result)
487 (org-delete-overlay ov))))
488 (push ov org-babel-hide-result-overlays)))
489 (error "Not looking at a result line"))))
491 ;; org-tab-after-check-for-cycling-hook
492 (add-hook 'org-tab-first-hook 'org-babel-hide-result-toggle-maybe)
493 ;; Remove overlays when changing major mode
494 (add-hook 'org-mode-hook
495 (lambda () (org-add-hook 'change-major-mode-hook
496 'org-babel-show-result-all 'append 'local)))
498 (defmacro org-babel-map-source-blocks (file &rest body)
499 "Evaluate BODY forms on each source-block in FILE."
500 (declare (indent 1))
501 `(let ((visited-p (get-buffer (file-name-nondirectory ,file))))
502 (save-window-excursion
503 (find-file ,file) (goto-char (point-min))
504 (while (re-search-forward org-babel-src-block-regexp nil t)
505 (goto-char (match-beginning 0))
506 (save-match-data ,@body)
507 (goto-char (match-end 0))))
508 (unless visited-p (kill-buffer (file-name-nondirectory file)))))
510 (defun org-babel-params-from-properties ()
511 "Return an association list of any source block params which
512 may be specified in the properties of the current outline entry."
513 (save-match-data
514 (delq nil
515 (mapcar
516 (lambda (header-arg)
517 (let ((val (or (condition-case nil
518 (org-entry-get (point) header-arg t)
519 (error nil))
520 (cdr (assoc header-arg org-file-properties)))))
521 (when val
522 ;; (message "prop %s=%s" header-arg val) ;; debugging
523 (cons (intern (concat ":" header-arg)) val))))
524 (mapcar 'symbol-name org-babel-header-arg-names)))))
526 (defun org-babel-parse-src-block-match ()
527 (let* ((lang (org-babel-clean-text-properties (match-string 1)))
528 (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
529 (switches (match-string 2))
530 (body (org-babel-clean-text-properties (match-string 4)))
531 (preserve-indentation (or org-src-preserve-indentation
532 (string-match "-i\\>" switches))))
533 (list lang
534 ;; get src block body removing properties, protective commas, and indentation
535 (with-temp-buffer
536 (save-match-data
537 (insert (org-babel-strip-protective-commas body))
538 (unless preserve-indentation (org-do-remove-indentation))
539 (buffer-string)))
540 (org-babel-merge-params
541 org-babel-default-header-args
542 (org-babel-params-from-properties)
543 (if (boundp lang-headers) (eval lang-headers) nil)
544 (org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 3) ""))))
545 switches)))
547 (defun org-babel-parse-inline-src-block-match ()
548 (let* ((lang (org-babel-clean-text-properties (match-string 2)))
549 (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
550 (list lang
551 (org-babel-strip-protective-commas (org-babel-clean-text-properties (match-string 5)))
552 (org-babel-merge-params
553 org-babel-default-inline-header-args
554 (org-babel-params-from-properties)
555 (if (boundp lang-headers) (eval lang-headers) nil)
556 (org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 4) "")))))))
558 (defun org-babel-parse-header-arguments (arg-string)
559 "Parse a string of header arguments returning an alist."
560 (if (> (length arg-string) 0)
561 (delq nil
562 (mapcar
563 (lambda (arg)
564 (if (string-match "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)" arg)
565 (cons (intern (concat ":" (match-string 1 arg)))
566 (let ((raw (org-babel-chomp (match-string 2 arg))))
567 (if (org-babel-number-p raw) raw (eval (org-babel-read raw)))))
568 (cons (intern (concat ":" arg)) nil)))
569 (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:" t)))))
571 (defun org-babel-process-params (params)
572 "Parse params and resolve references.
574 Return a list (session vars result-params result-type)."
575 (let* ((session (cdr (assoc :session params)))
576 (vars (org-babel-ref-variables params))
577 (result-params (split-string (or (cdr (assoc :results params)) "")))
578 (result-type (cond ((member "output" result-params) 'output)
579 ((member "value" result-params) 'value)
580 (t 'value))))
581 (list session vars result-params result-type)))
583 (defun org-babel-where-is-src-block-head ()
584 "Return the point at the beginning of the current source
585 block. Specifically at the beginning of the #+BEGIN_SRC line.
586 If the point is not on a source block then return nil."
587 (let ((initial (point)) top bottom)
589 (save-excursion ;; on a source name line
590 (beginning-of-line 1)
591 (and (looking-at org-babel-source-name-regexp) (forward-line 1)
592 (looking-at org-babel-src-block-regexp)
593 (point)))
594 (save-excursion ;; on a #+begin_src line
595 (beginning-of-line 1)
596 (and (looking-at org-babel-src-block-regexp)
597 (point)))
598 (save-excursion ;; inside a src block
599 (and
600 (re-search-backward "#\\+begin_src" nil t) (setq top (point))
601 (re-search-forward "#\\+end_src" nil t) (setq bottom (point))
602 (< top initial) (< initial bottom)
603 (goto-char top) (move-beginning-of-line 1)
604 (looking-at org-babel-src-block-regexp)
605 (point))))))
607 (defun org-babel-goto-named-source-block (&optional name)
608 "Go to a named source-code block."
609 (interactive "ssource-block name: ")
610 (let ((point (org-babel-find-named-block name)))
611 (if point
612 ;; taken from `org-open-at-point'
613 (progn (goto-char point) (org-show-context))
614 (message "source-code block '%s' not found in this buffer" name))))
616 (defun org-babel-find-named-block (name)
617 "Find a named source-code block.
618 Return the location of the source block identified by source
619 NAME, or nil if no such block exists. Set match data according to
620 org-babel-named-src-block-regexp."
621 (save-excursion
622 (let ((case-fold-search t)
623 (regexp (org-babel-named-src-block-regexp-for-name name)) msg)
624 (goto-char (point-min))
625 (when (or (re-search-forward regexp nil t)
626 (re-search-backward regexp nil t))
627 (match-beginning 0)))))
629 (defun org-babel-find-named-result (name)
630 "Return the location of the result named NAME in the current
631 buffer or nil if no such result exists."
632 (save-excursion
633 (goto-char (point-min))
634 (when (re-search-forward
635 (concat org-babel-result-regexp "[ \t]" (regexp-quote name) "[ \t\n\f\v\r]") nil t)
636 (move-beginning-of-line 0) (point))))
638 (defun org-babel-where-is-src-block-result (&optional insert info hash)
639 "Return the point at the beginning of the result of the current
640 source block. Specifically at the beginning of the results line.
641 If no result exists for this block then create a results line
642 following the source block."
643 (save-excursion
644 (let* ((on-lob-line (progn (beginning-of-line 1)
645 (looking-at org-babel-lob-one-liner-regexp)))
646 (name (if on-lob-line (first (org-babel-lob-get-info))
647 (fifth (or info (org-babel-get-src-block-info)))))
648 (head (unless on-lob-line (org-babel-where-is-src-block-head))) end)
649 (when head (goto-char head))
650 (or (and name (org-babel-find-named-result name))
651 (and (or on-lob-line (re-search-forward "#\\+end_src" nil t))
652 (progn (move-end-of-line 1)
653 (if (eobp) (insert "\n") (forward-char 1))
654 (setq end (point))
655 (or (and (not name)
656 (progn ;; unnamed results line already exists
657 (re-search-forward "[^ \f\t\n\r\v]" nil t)
658 (move-beginning-of-line 1)
659 (looking-at (concat org-babel-result-regexp "\n"))))
660 ;; or (with optional insert) back up and make one ourselves
661 (when insert
662 (goto-char end)
663 (if (looking-at "[\n\r]") (forward-char 1) (insert "\n"))
664 (insert (concat "#+results" (if hash (concat "["hash"]"))
665 ":"(if name (concat " " name)) "\n"))
666 (move-beginning-of-line 0)
667 (if hash (org-babel-hide-hash)) t)))
668 (point))))))
670 (defun org-babel-read-result ()
671 "Read the result at `point' into emacs-lisp."
672 (let ((case-fold-search t) result-string)
673 (cond
674 ((org-at-table-p) (org-babel-read-table))
675 ((looking-at org-bracket-link-regexp) (org-babel-read-link))
676 ((looking-at org-block-regexp) (org-babel-trim (match-string 4)))
677 ((looking-at ": ")
678 (setq result-string
679 (org-babel-trim
680 (mapconcat (lambda (line) (if (and (> (length line) 1)
681 (string= ": " (substring line 0 2)))
682 (substring line 2)
683 line))
684 (split-string
685 (buffer-substring (point) (org-babel-result-end)) "[\r\n]+")
686 "\n")))
687 (or (org-babel-number-p result-string) result-string))
688 ((looking-at org-babel-result-regexp)
689 (save-excursion (forward-line 1) (org-babel-read-result))))))
691 (defun org-babel-read-table ()
692 "Read the table at `point' into emacs-lisp."
693 (mapcar (lambda (row)
694 (if (and (symbolp row) (equal row 'hline)) row
695 (mapcar #'org-babel-read row)))
696 (org-table-to-lisp)))
698 (defun org-babel-read-link ()
699 "Read the link at `point' into emacs-lisp. If the path of the
700 link is a file path it is expanded using `expand-file-name'."
701 (let* ((case-fold-search t)
702 (raw (and (looking-at org-bracket-link-regexp)
703 (org-babel-clean-text-properties (match-string 1))))
704 (type (and (string-match org-link-types-re raw)
705 (match-string 1 raw))))
706 (cond
707 ((not type) (expand-file-name raw))
708 ((string= type "file")
709 (and (string-match "file\\(.*\\):\\(.+\\)" raw)
710 (expand-file-name (match-string 2 raw))))
711 (t raw))))
713 (defun org-babel-insert-result (result &optional result-params info hash)
714 "Insert RESULT into the current buffer after the end of the
715 current source block. With optional argument RESULT-PARAMS
716 controls insertion of results in the org-mode file.
717 RESULT-PARAMS can take the following values...
719 replace - (default option) insert results after the source block
720 replacing any previously inserted results
722 silent -- no results are inserted
724 file ---- the results are interpreted as a file path, and are
725 inserted into the buffer using the Org-mode file syntax
727 raw ----- results are added directly to the org-mode file. This
728 is a good option if you code block will output org-mode
729 formatted text.
731 org ----- this is the same as the 'raw' option
733 html ---- results are added inside of a #+BEGIN_HTML block. This
734 is a good option if you code block will output html
735 formatted text.
737 latex --- results are added inside of a #+BEGIN_LATEX block.
738 This is a good option if you code block will output
739 latex formatted text.
741 code ---- the results are extracted in the syntax of the source
742 code of the language being evaluated and are added
743 inside of a #+BEGIN_SRC block with the source-code
744 language set appropriately."
745 (if (stringp result)
746 (progn
747 (setq result (org-babel-clean-text-properties result))
748 (when (member "file" result-params)
749 (setq result (org-babel-result-to-file result))))
750 (unless (listp result) (setq result (format "%S" result))))
751 (if (and result-params (member "replace" result-params)
752 (not (member "silent" result-params)))
753 (org-babel-remove-result info))
754 (if (= (length result) 0)
755 (if (member "value" result-params)
756 (message "No result returned by source block")
757 (message "Source block produced no output"))
758 (if (and result-params (member "silent" result-params))
759 (progn (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
760 result)
761 (when (and (stringp result) ;; ensure results end in a newline
762 (not (or (string-equal (substring result -1) "\n")
763 (string-equal (substring result -1) "\r"))))
764 (setq result (concat result "\n")))
765 (save-excursion
766 (let ((existing-result (org-babel-where-is-src-block-result t info hash))
767 (results-switches (cdr (assoc :results_switches (third info)))))
768 (when existing-result (goto-char existing-result) (forward-line 1))
769 (setq results-switches
770 (if results-switches (concat " " results-switches) ""))
771 (cond
772 ;; assume the result is a table if it's not a string
773 ((not (stringp result))
774 (insert (concat (orgtbl-to-orgtbl
775 (if (and (listp (car result))
776 (listp (cdr (car result))))
777 result (list result))
778 '(:fmt (lambda (cell) (format "%s" cell)))) "\n"))
779 (forward-line -1) (org-cycle))
780 ((member "file" result-params)
781 (insert result))
782 ((member "html" result-params)
783 (insert (format "#+BEGIN_HTML%s\n%s#+END_HTML\n" results-switches result)))
784 ((member "latex" result-params)
785 (insert (format "#+BEGIN_LaTeX%s\n%s#+END_LaTeX\n" results-switches result)))
786 ((member "code" result-params)
787 (insert (format "#+BEGIN_SRC %s%s\n%s#+END_SRC\n" lang results-switches result)))
788 ((or (member "raw" result-params) (member "org" result-params))
789 (save-excursion (insert result)) (if (org-at-table-p) (org-cycle)))
791 (org-babel-examplize-region
792 (point) (progn (insert result) (point)) results-switches)))))
793 (message "finished"))))
795 (defun org-babel-result-to-org-string (result)
796 "Return RESULT as a string in org-mode format. This function
797 relies on `org-babel-insert-result'."
798 (with-temp-buffer (org-babel-insert-result result) (buffer-string)))
800 (defun org-babel-remove-result (&optional info)
801 "Remove the result of the current source block."
802 (interactive)
803 (let ((location (org-babel-where-is-src-block-result nil info)) start)
804 (when location
805 (save-excursion
806 (goto-char location) (setq start (point)) (forward-line 1)
807 (delete-region start (org-babel-result-end))))))
809 (defun org-babel-result-end ()
810 "Return the point at the end of the current set of results"
811 (save-excursion
812 (if (org-at-table-p)
813 (progn (goto-char (org-table-end)) (point))
814 (let ((case-fold-search t))
815 (cond
816 ((looking-at "#\\+begin_latex")
817 (search-forward "#+end_latex" nil t)
818 (forward-line 1))
819 ((looking-at "#\\+begin_html")
820 (search-forward "#+end_html" nil t)
821 (forward-line 1))
822 ((looking-at "#\\+begin_example")
823 (search-forward "#+end_example" nil t)
824 (forward-line 1))
825 ((looking-at "#\\+begin_src")
826 (search-forward "#+end_src" nil t)
827 (forward-line 1))
828 (t (progn (while (looking-at "\\(: \\|\\[\\[\\)")
829 (forward-line 1))))))
830 (point))))
832 (defun org-babel-result-to-file (result)
833 "Return an `org-mode' link with the path being the value or
834 RESULT, and the display being the `file-name-nondirectory' if
835 non-nil."
836 (concat "[[file:" result "]]"))
838 (defun org-babel-examplize-region (beg end &optional results-switches)
839 "Comment out region using the ': ' org example quote."
840 (interactive "*r")
841 (let ((size (abs (- (line-number-at-pos end)
842 (line-number-at-pos beg)))))
843 (save-excursion
844 (cond ((= size 0)
845 (error "This should be impossible: a newline was appended to result if missing"))
846 ((< size org-babel-min-lines-for-block-output)
847 (goto-char beg)
848 (dotimes (n size)
849 (move-beginning-of-line 1) (insert ": ") (forward-line 1)))
851 (goto-char beg)
852 (insert (if results-switches
853 (format "#+begin_example%s\n" results-switches)
854 "#+begin_example\n"))
855 (forward-char (- end beg))
856 (insert "#+end_example\n"))))))
858 (defun org-babel-merge-params (&rest plists)
859 "Combine all parameter association lists in PLISTS. Later
860 elements of PLISTS override the values of previous element. This
861 takes into account some special considerations for certain
862 parameters when merging lists."
863 (let ((results-exclusive-groups
864 '(("file" "vector" "table" "scalar" "raw" "org" "html" "latex" "code" "pp")
865 ("replace" "silent")
866 ("output" "value")))
867 (exports-exclusive-groups
868 '(("code" "results" "both" "none")))
869 params results exports tangle noweb cache vars var ref shebang comments)
870 (flet ((e-merge (exclusive-groups &rest result-params)
871 ;; maintain exclusivity of mutually exclusive parameters
872 (let (output)
873 (mapc (lambda (new-params)
874 (mapc (lambda (new-param)
875 (mapc (lambda (exclusive-group)
876 (when (member new-param exclusive-group)
877 (mapcar (lambda (excluded-param)
878 (setq output (delete excluded-param output)))
879 exclusive-group)))
880 exclusive-groups)
881 (setq output (org-uniquify (cons new-param output))))
882 new-params))
883 result-params)
884 output)))
885 (mapc (lambda (plist)
886 (mapc (lambda (pair)
887 (case (car pair)
888 (:var
889 ;; we want only one specification per variable
890 (when (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*=[ \t]*\\([^\f\n\r\v]+\\)$" (cdr pair))
891 ;; TODO: When is this not true?
892 (setq var (intern (match-string 1 (cdr pair)))
893 ref (match-string 2 (cdr pair))
894 vars (cons (cons var ref) (assq-delete-all var vars)))))
895 (:results
896 (setq results
897 (e-merge results-exclusive-groups results (split-string (cdr pair)))))
898 (:file
899 (when (cdr pair)
900 (setq results (e-merge results-exclusive-groups results '("file")))
901 (unless (or (member "both" exports)
902 (member "none" exports)
903 (member "code" exports))
904 (setq exports (e-merge exports-exclusive-groups exports '("results"))))
905 (setq params (cons pair (assq-delete-all (car pair) params)))))
906 (:exports
907 (setq exports (e-merge exports-exclusive-groups
908 exports (split-string (cdr pair)))))
909 (:tangle ;; take the latest -- always overwrite
910 (setq tangle (or (list (cdr pair)) tangle)))
911 (:noweb
912 (setq noweb (e-merge '(("yes" "no"))
913 noweb (split-string (or (cdr pair) "")))))
914 (:cache
915 (setq cache (e-merge '(("yes" "no"))
916 cache (split-string (or (cdr pair) "")))))
917 (:shebang ;; take the latest -- always overwrite
918 (setq shebang (or (list (cdr pair)) shebang)))
919 (:comments
920 (setq comments (e-merge '(("yes" "no"))
921 comments (split-string (or (cdr pair) "")))))
922 (t ;; replace: this covers e.g. :session
923 (setq params (cons pair (assq-delete-all (car pair) params))))))
924 plist))
925 plists))
926 (setq vars (mapcar (lambda (pair) (format "%s=%s" (car pair) (cdr pair))) vars))
927 (while vars (setq params (cons (cons :var (pop vars)) params)))
928 (cons (cons :comments (mapconcat 'identity comments " "))
929 (cons (cons :shebang (mapconcat 'identity shebang " "))
930 (cons (cons :cache (mapconcat 'identity cache " "))
931 (cons (cons :noweb (mapconcat 'identity noweb " "))
932 (cons (cons :tangle (mapconcat 'identity tangle " "))
933 (cons (cons :exports (mapconcat 'identity exports " "))
934 (cons (cons :results (mapconcat 'identity results " "))
935 params)))))))))
937 (defun org-babel-expand-noweb-references (&optional info parent-buffer)
938 "This function expands Noweb style references in the body of
939 the current source-code block. For example the following
940 reference would be replaced with the body of the source-code
941 block named 'example-block'.
943 <<example-block>>
945 Note that any text preceding the <<foo>> construct on a line will
946 be interposed between the lines of the replacement text. So for
947 example if <<foo>> is placed behind a comment, then the entire
948 replacement text will also be commented.
950 This function must be called from inside of the buffer containing
951 the source-code block which holds BODY.
953 In addition the following syntax can be used to insert the
954 results of evaluating the source-code block named 'example-block'.
956 <<example-block()>>
958 Any optional arguments can be passed to example-block by placing
959 the arguments inside the parenthesis following the convention
960 defined by `org-babel-lob'. For example
962 <<example-block(a=9)>>
964 would set the value of argument \"a\" equal to \"9\". Note that
965 these arguments are not evaluated in the current source-code
966 block but are passed literally to the \"example-block\"."
967 (let* ((parent-buffer (or parent-buffer (current-buffer)))
968 (info (or info (org-babel-get-src-block-info)))
969 (lang (first info))
970 (body (second info))
971 (new-body "") index source-name evaluate prefix)
972 (flet ((nb-add (text)
973 (setq new-body (concat new-body text))))
974 (with-temp-buffer
975 (insert body) (goto-char (point-min))
976 (funcall (intern (concat (or (and (cdr (assoc lang org-src-lang-modes))
977 (symbol-name
978 (cdr (assoc lang org-src-lang-modes))))
979 lang) "-mode")))
980 (setq index (point))
981 (while (and (re-search-forward "<<\\(.+?\\)>>" nil t))
982 (save-match-data (setf source-name (match-string 1)))
983 (save-match-data (setq evaluate (string-match "\(.*\)" source-name)))
984 (save-match-data
985 (setq prefix (buffer-substring (match-beginning 0)
986 (save-excursion
987 (move-beginning-of-line 1) (point)))))
988 ;; add interval to new-body (removing noweb reference)
989 (goto-char (match-beginning 0))
990 (nb-add (buffer-substring index (point)))
991 (goto-char (match-end 0))
992 (setq index (point))
993 (nb-add (save-excursion
994 (set-buffer parent-buffer)
995 (mapconcat ;; interpose `prefix' between every line
996 #'identity
997 (split-string
998 (if evaluate
999 (let ((raw (org-babel-ref-resolve-reference
1000 source-name nil)))
1001 (if (stringp raw) raw (format "%S" raw)))
1002 (let ((point (org-babel-find-named-block source-name)))
1003 (if point
1004 (save-excursion
1005 (goto-char point)
1006 (org-babel-trim (org-babel-expand-noweb-references
1007 (org-babel-get-src-block-info))))
1008 ;; optionally raise an error if named
1009 ;; source-block doesn't exist
1010 (if (member lang org-babel-noweb-error-langs)
1011 (error
1012 "<<%s>> could not be resolved (see `org-babel-noweb-error-langs')"
1013 source-name)
1014 "")))) "[\n\r]") (concat "\n" prefix)))))
1015 (nb-add (buffer-substring index (point-max)))))
1016 new-body))
1018 (defun org-babel-clean-text-properties (text)
1019 "Strip all properties from text return."
1020 (set-text-properties 0 (length text) nil text) text)
1022 (defun org-babel-strip-protective-commas (body)
1023 "Strip protective commas from bodies of source blocks."
1024 (replace-regexp-in-string "^,#" "#" body))
1026 (defun org-babel-read (cell)
1027 "Convert the string value of CELL to a number if appropriate.
1028 Otherwise if cell looks like lisp (meaning it starts with a
1029 \"(\" or a \"'\") then read it as lisp, otherwise return it
1030 unmodified as a string.
1032 This is taken almost directly from `org-read-prop'."
1033 (if (and (stringp cell) (not (equal cell "")))
1034 (or (org-babel-number-p cell)
1035 (if (or (equal "(" (substring cell 0 1))
1036 (equal "'" (substring cell 0 1)))
1037 (read cell)
1038 (progn (set-text-properties 0 (length cell) nil cell) cell)))
1039 cell))
1041 (defun org-babel-number-p (string)
1042 "Return t if STRING represents a number"
1043 (if (and (string-match "^-?[[:digit:]]*\\.?[[:digit:]]*$" string)
1044 (= (match-end 0) (length string)))
1045 (string-to-number string)))
1047 (defun org-babel-import-elisp-from-file (file-name)
1048 "Read the results located at FILE-NAME into an elisp table. If
1049 the table is trivial, then return it as a scalar."
1050 (let (result)
1051 (with-temp-buffer
1052 (condition-case nil
1053 (progn
1054 (org-table-import file-name nil)
1055 (delete-file file-name)
1056 (setq result (mapcar (lambda (row)
1057 (mapcar #'org-babel-string-read row))
1058 (org-table-to-lisp))))
1059 (error nil)))
1060 (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
1061 (if (consp (car result))
1062 (if (null (cdr (car result)))
1063 (caar result)
1064 result)
1065 (car result))
1066 result)))
1068 (defun org-babel-string-read (cell)
1069 "Strip nested \"s from around strings in exported R values."
1070 (org-babel-read (or (and (stringp cell)
1071 (string-match "\\\"\\(.+\\)\\\"" cell)
1072 (match-string 1 cell))
1073 cell)))
1075 (defun org-babel-reverse-string (string)
1076 (apply 'string (reverse (string-to-list string))))
1078 (defun org-babel-chomp (string &optional regexp)
1079 "Remove any trailing space or carriage returns characters from
1080 STRING. Default regexp used is \"[ \f\t\n\r\v]\" but can be
1081 overwritten by specifying a regexp as a second argument."
1082 (let ((regexp (or regexp "[ \f\t\n\r\v]")))
1083 (while (and (> (length string) 0) (string-match regexp (substring string -1)))
1084 (setq string (substring string 0 -1)))
1085 string))
1087 (defun org-babel-trim (string &optional regexp)
1088 "Like `org-babel-chomp' only it runs on both the front and back of the string"
1089 (org-babel-chomp (org-babel-reverse-string
1090 (org-babel-chomp (org-babel-reverse-string string) regexp)) regexp))
1092 (defun org-babel-tramp-handle-call-process-region
1093 (start end program &optional delete buffer display &rest args)
1094 "Use tramp to handle call-process-region.
1095 Fixes a bug in `tramp-handle-call-process-region'."
1096 (if (and (featurep 'tramp) (file-remote-p default-directory))
1097 (let ((tmpfile (tramp-compat-make-temp-file "")))
1098 (write-region start end tmpfile)
1099 (when delete (delete-region start end))
1100 (unwind-protect
1101 ;; (apply 'call-process program tmpfile buffer display args) ;; bug in tramp
1102 (apply 'process-file program tmpfile buffer display args)
1103 (delete-file tmpfile)))
1104 ;; call-process-region-original is the original emacs definition. It
1105 ;; is in scope from the let binding in org-babel-execute-src-block
1106 (apply call-process-region-original start end program delete buffer display args)))
1108 (defun org-babel-maybe-remote-file (file)
1109 (if (file-remote-p default-directory)
1110 (let* ((vec (tramp-dissect-file-name default-directory))
1111 (user (tramp-file-name-user vec))
1112 (host (tramp-file-name-host vec)))
1113 (concat "/" user (when user "@") host ":" file))
1114 file))
1116 (provide 'org-babel)
1117 ;;; org-babel.el ends here