babel: now including source code block arguments w/source name on export
[rgr-org-mode.git] / contrib / babel / lisp / org-babel-exp.el
blob639db35d385998f60ad18e3502d799bec5ffe254
1 ;;; org-babel-exp.el --- Exportation of org-babel source blocks
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 ;; for more information see the comments in org-babel.el
31 ;;; Code:
32 (require 'org-babel)
33 (require 'org-exp-blocks)
34 (org-export-blocks-add-block '(src org-babel-exp-src-blocks nil))
35 (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
36 (add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners))
38 (defvar org-babel-function-def-export-keyword "function"
39 "When exporting a source block function, this keyword will
40 appear in the exported version in the place of source name
41 line. A source block is considered to be a source block function
42 if the source name is present and is followed by a parenthesized
43 argument list. The parentheses may be empty or contain
44 whitespace. An example is the following which generates n random
45 (uniform) numbers.
47 #+source: rand(n)
48 #+begin_src R
49 runif(n)
50 #+end_src
53 (defvar org-babel-function-def-export-indent 4
54 "When exporting a source block function, the block contents
55 will be indented by this many characters. See
56 `org-babel-function-def-export-name' for the definition of a
57 source block function.")
59 (defvar obe-marker nil)
61 (defun org-babel-exp-src-blocks (body &rest headers)
62 "Process src block for export. Depending on the 'export'
63 headers argument in replace the source code block with...
65 both ---- display the code and the results
67 code ---- the default, display the code inside the block but do
68 not process
70 results - just like none only the block is run on export ensuring
71 that it's results are present in the org-mode buffer
73 none ----- do not display either code or results upon export"
74 (interactive)
75 (message "org-babel-exp processing...")
76 (when (member (first headers) org-babel-interpreters)
77 (save-excursion
78 (goto-char (match-beginning 0))
79 (org-babel-exp-do-export (org-babel-get-src-block-info) 'block))))
81 (defun org-babel-exp-inline-src-blocks (start end)
82 "Process inline src blocks between START and END for export.
83 See `org-babel-exp-src-blocks' for export options, currently the
84 options and are taken from `org-babel-defualt-inline-header-args'."
85 (interactive)
86 (save-excursion
87 (goto-char start)
88 (while (and (< (point) end)
89 (re-search-forward org-babel-inline-src-block-regexp end t))
90 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
91 (replacement (save-match-data
92 (org-babel-exp-do-export info 'inline))))
93 (setq end (+ end (- (length replacement) (length (match-string 1)))))
94 (replace-match replacement t t nil 1)))))
96 (defun org-babel-exp-lob-one-liners (start end)
97 "Process #+lob (Library of Babel) calls between START and END for export.
98 See `org-babel-exp-src-blocks' for export options. Currently the
99 options are taken from `org-babel-default-header-args'."
100 (interactive)
101 (let (replacement)
102 (save-excursion
103 (goto-char start)
104 (while (and (< (point) end)
105 (re-search-forward org-babel-lob-one-liner-regexp nil t))
106 (setq replacement
107 (save-match-data
108 (org-babel-exp-do-export
109 (list "emacs-lisp" "results"
110 (org-babel-merge-params
111 org-babel-default-header-args
112 (org-babel-parse-header-arguments
113 (org-babel-clean-text-properties
114 (concat ":var results="
115 (mapconcat #'identity (org-babel-lob-get-info) " "))))))
116 'lob)))
117 (setq end (+ end (- (length replacement) (length (match-string 0)))))
118 (replace-match replacement t t)))))
120 (defun org-babel-exp-do-export (info type)
121 (case (intern (or (cdr (assoc :exports (third info))) "code"))
122 ('none "")
123 ('code (org-babel-exp-code info type))
124 ('results (org-babel-exp-results info type))
125 ('both (concat (org-babel-exp-code info type)
126 "\n\n"
127 (org-babel-exp-results info type)))))
129 (defun org-babel-exp-code (info type)
130 (let ((lang (first info))
131 (body (second info))
132 (switches (fourth info))
133 (name (fifth info))
134 (args (mapcar #'cdr
135 (remove-if-not (lambda (el) (eq :var (car el))) (third info)))))
136 (case type
137 ('inline (format "=%s=" body))
138 ('block
139 (let ((str (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
140 (if (string-match "\n$" body) "" "\n"))))
141 (add-text-properties 0 (length str)
142 (list 'org-caption
143 (format "%s(%s)"
144 name (mapconcat #'identity args ", ")))
145 str) str))
146 ('lob
147 (let ((call-line (and (string-match "results=" (car args))
148 (substring (car args) (match-end 0)))))
149 (cond
150 ((eq backend 'html)
151 (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n" call-line))
152 ((t (format ": %s\n" call-line)))))))))
154 (defun org-babel-exp-results (info type)
155 (let ((lang (first info))
156 (body (second info))
157 (params
158 ;; lets ensure that we lookup references in the original file
159 (mapcar (lambda (pair)
160 (if (and org-current-export-file
161 (eq (car pair) :var)
162 (string-match org-babel-ref-split-regexp (cdr pair)))
163 `(:var . ,(concat (match-string 1 (cdr pair))
164 "=" org-current-export-file
165 ":" (match-string 2 (cdr pair))))
166 pair))
167 (third info))))
168 (case type
169 ('inline
170 (let ((raw (org-babel-execute-src-block
171 nil info '((:results . "silent"))))
172 (result-params (split-string (cdr (assoc :results params)))))
173 (cond ;; respect the value of the :results header argument
174 ((member "file" result-params)
175 (org-babel-result-to-file raw))
176 ((or (member "raw" result-params) (member "org" result-params))
177 raw)
178 ((member "code" result-params)
179 (format "src_%s{%s}" lang raw))
181 (if (stringp raw)
182 (if (= 0 (length raw)) "=(no results)="
183 (format "=%s=" raw))
184 (format "=%S=" raw))))))
185 ('block
186 (org-babel-execute-src-block
187 nil nil (org-babel-merge-params params '((:results . "replace"))))
189 ('lob
190 (save-excursion
191 (re-search-backward org-babel-lob-one-liner-regexp nil t)
192 (org-babel-execute-src-block
193 nil (list lang body (org-babel-merge-params params '((:results . "replace"))))) "")))))
195 (provide 'org-babel-exp)
196 ;;; org-babel-exp.el ends here