Sync Org 7.9.2 from the commit tagged "release_7.9.2" in Org's Git repo.
[bpt/emacs.git] / lisp / org / ob-C.el
CommitLineData
86fbb8ca
CD
1;;; ob-C.el --- org-babel functions for C and similar languages
2
b9db31c7 3;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
86fbb8ca
CD
4
5;; Author: Eric Schulte
6;; Keywords: literate programming, reproducible research
7;; Homepage: http://orgmode.org
86fbb8ca
CD
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software: you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24;;; Commentary:
25
26;; Org-Babel support for evaluating C code.
27;;
28;; very limited implementation:
29;; - currently only support :results output
30;; - not much in the way of error feedback
31
32;;; Code:
33(require 'ob)
34(require 'ob-eval)
86fbb8ca
CD
35(require 'cc-mode)
36
37(declare-function org-entry-get "org"
38 (pom property &optional inherit literal-nil))
39
3ab2c837
BG
40
41(defvar org-babel-tangle-lang-exts)
42(add-to-list 'org-babel-tangle-lang-exts '("C++" . "cpp"))
86fbb8ca
CD
43
44(defvar org-babel-default-header-args:C '())
45
46(defvar org-babel-C-compiler "gcc"
47 "Command used to compile a C source code file into an
48 executable.")
49
3ab2c837
BG
50(defvar org-babel-C++-compiler "g++"
51 "Command used to compile a C++ source code file into an
86fbb8ca
CD
52 executable.")
53
54(defvar org-babel-c-variant nil
55 "Internal variable used to hold which type of C (e.g. C or C++)
56is currently being evaluated.")
57
58(defun org-babel-execute:cpp (body params)
59 "Execute BODY according to PARAMS. This function calls
3ab2c837
BG
60`org-babel-execute:C++'."
61 (org-babel-execute:C++ body params))
86fbb8ca 62
3ab2c837 63(defun org-babel-execute:C++ (body params)
8223b1d2 64 "Execute a block of C++ code with org-babel. This function is
86fbb8ca
CD
65called by `org-babel-execute-src-block'."
66 (let ((org-babel-c-variant 'cpp)) (org-babel-C-execute body params)))
67
3ab2c837 68(defun org-babel-expand-body:C++ (body params)
86fbb8ca
CD
69 "Expand a block of C++ code with org-babel according to it's
70header arguments (calls `org-babel-C-expand')."
afe98dfa 71 (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand body params)))
86fbb8ca
CD
72
73(defun org-babel-execute:C (body params)
74 "Execute a block of C code with org-babel. This function is
75called by `org-babel-execute-src-block'."
76 (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params)))
77
afe98dfa 78(defun org-babel-expand-body:c (body params)
86fbb8ca
CD
79 "Expand a block of C code with org-babel according to it's
80header arguments (calls `org-babel-C-expand')."
afe98dfa 81 (let ((org-babel-c-variant 'c)) (org-babel-C-expand body params)))
86fbb8ca
CD
82
83(defun org-babel-C-execute (body params)
84 "This function should only be called by `org-babel-execute:C'
3ab2c837 85or `org-babel-execute:C++'."
afe98dfa
CD
86 (let* ((tmp-src-file (org-babel-temp-file
87 "C-src-"
88 (cond
89 ((equal org-babel-c-variant 'c) ".c")
90 ((equal org-babel-c-variant 'cpp) ".cpp"))))
8223b1d2 91 (tmp-bin-file (org-babel-temp-file "C-bin-" org-babel-exeext))
86fbb8ca
CD
92 (cmdline (cdr (assoc :cmdline params)))
93 (flags (cdr (assoc :flags params)))
94 (full-body (org-babel-C-expand body params))
95 (compile
96 (progn
97 (with-temp-file tmp-src-file (insert full-body))
98 (org-babel-eval
99 (format "%s -o %s %s %s"
100 (cond
101 ((equal org-babel-c-variant 'c) org-babel-C-compiler)
3ab2c837 102 ((equal org-babel-c-variant 'cpp) org-babel-C++-compiler))
afe98dfa 103 (org-babel-process-file-name tmp-bin-file)
86fbb8ca
CD
104 (mapconcat 'identity
105 (if (listp flags) flags (list flags)) " ")
afe98dfa 106 (org-babel-process-file-name tmp-src-file)) ""))))
86fbb8ca
CD
107 ((lambda (results)
108 (org-babel-reassemble-table
afe98dfa
CD
109 (if (member "vector" (cdr (assoc :result-params params)))
110 (let ((tmp-file (org-babel-temp-file "c-")))
86fbb8ca
CD
111 (with-temp-file tmp-file (insert results))
112 (org-babel-import-elisp-from-file tmp-file))
113 (org-babel-read results))
114 (org-babel-pick-name
afe98dfa 115 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
86fbb8ca 116 (org-babel-pick-name
afe98dfa 117 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
86fbb8ca 118 (org-babel-trim
8223b1d2
BG
119 (org-babel-eval
120 (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))
86fbb8ca 121
afe98dfa 122(defun org-babel-C-expand (body params)
86fbb8ca
CD
123 "Expand a block of C or C++ code with org-babel according to
124it's header arguments."
afe98dfa 125 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
86fbb8ca
CD
126 (main-p (not (string= (cdr (assoc :main params)) "no")))
127 (includes (or (cdr (assoc :includes params))
128 (org-babel-read (org-entry-get nil "includes" t))))
129 (defines (org-babel-read
130 (or (cdr (assoc :defines params))
131 (org-babel-read (org-entry-get nil "defines" t))))))
8223b1d2
BG
132 (mapconcat 'identity
133 (list
134 ;; includes
135 (mapconcat
136 (lambda (inc) (format "#include %s" inc))
137 (if (listp includes) includes (list includes)) "\n")
138 ;; defines
139 (mapconcat
140 (lambda (inc) (format "#define %s" inc))
141 (if (listp defines) defines (list defines)) "\n")
142 ;; variables
143 (mapconcat 'org-babel-C-var-to-C vars "\n")
144 ;; body
145 (if main-p
146 (org-babel-C-ensure-main-wrap body)
147 body) "\n") "\n")))
86fbb8ca
CD
148
149(defun org-babel-C-ensure-main-wrap (body)
150 "Wrap body in a \"main\" function call if none exists."
afe98dfa 151 (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
86fbb8ca 152 body
801a68c8 153 (format "int main() {\n%s\nreturn(0);\n}\n" body)))
86fbb8ca
CD
154
155(defun org-babel-prep-session:C (session params)
156 "This function does nothing as C is a compiled language with no
157support for sessions"
158 (error "C is a compiled languages -- no support for sessions"))
159
160(defun org-babel-load-session:C (session body params)
161 "This function does nothing as C is a compiled language with no
162support for sessions"
163 (error "C is a compiled languages -- no support for sessions"))
164
165;; helper functions
166
167(defun org-babel-C-var-to-C (pair)
168 "Convert an elisp val into a string of C code specifying a var
169of the same value."
170 ;; TODO list support
171 (let ((var (car pair))
172 (val (cdr pair)))
173 (when (symbolp val)
174 (setq val (symbol-name val))
175 (when (= (length val) 1)
176 (setq val (string-to-char val))))
177 (cond
178 ((integerp val)
179 (format "int %S = %S;" var val))
180 ((floatp val)
181 (format "double %S = %S;" var val))
e66ba1df 182 ((or (integerp val))
86fbb8ca
CD
183 (format "char %S = '%S';" var val))
184 ((stringp val)
185 (format "char %S[%d] = \"%s\";"
186 var (+ 1 (length val)) val))
187 (t
188 (format "u32 %S = %S;" var val)))))
189
190
191(provide 'ob-C)
192
5b409b39 193
86fbb8ca
CD
194
195;;; ob-C.el ends here