Second try: Ensure that file local variables are set
[org-mode/org-tableheadings.git] / lisp / ob-C.el
blob35e8c621f103f91e21808e6d78190c09f0b047bc
1 ;;; ob-C.el --- org-babel functions for C and similar languages
3 ;; Copyright (C) 2010-2013 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating C code.
28 ;; very limited implementation:
29 ;; - currently only support :results output
30 ;; - not much in the way of error feedback
32 ;;; Code:
33 (require 'ob)
34 (require 'cc-mode)
35 (eval-when-compile
36 (require 'cl))
38 (declare-function org-entry-get "org"
39 (pom property &optional inherit literal-nil))
40 (declare-function org-remove-indentation "org" (code &optional n))
42 (defvar org-babel-tangle-lang-exts)
43 (add-to-list 'org-babel-tangle-lang-exts '("C++" . "cpp"))
45 (defvar org-babel-default-header-args:C '())
47 (defvar org-babel-C-compiler "gcc"
48 "Command used to compile a C source code file into an
49 executable.")
51 (defvar org-babel-C++-compiler "g++"
52 "Command used to compile a C++ source code file into an
53 executable.")
55 (defvar org-babel-c-variant nil
56 "Internal variable used to hold which type of C (e.g. C or C++)
57 is currently being evaluated.")
59 (defun org-babel-execute:cpp (body params)
60 "Execute BODY according to PARAMS.
61 This function calls `org-babel-execute:C++'."
62 (org-babel-execute:C++ body params))
64 (defun org-babel-execute:C++ (body params)
65 "Execute a block of C++ code with org-babel.
66 This function is called by `org-babel-execute-src-block'."
67 (let ((org-babel-c-variant 'cpp)) (org-babel-C-execute body params)))
69 (defun org-babel-expand-body:C++ (body params)
70 "Expand a block of C++ code with org-babel according to it's
71 header arguments (calls `org-babel-C-expand')."
72 (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand body params)))
74 (defun org-babel-execute:C (body params)
75 "Execute a block of C code with org-babel.
76 This function is called by `org-babel-execute-src-block'."
77 (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params)))
79 (defun org-babel-expand-body:c (body params)
80 "Expand a block of C code with org-babel according to it's
81 header arguments (calls `org-babel-C-expand')."
82 (let ((org-babel-c-variant 'c)) (org-babel-C-expand body params)))
84 (defun org-babel-C-execute (body params)
85 "This function should only be called by `org-babel-execute:C'
86 or `org-babel-execute:C++'."
87 (let* ((tmp-src-file (org-babel-temp-file
88 "C-src-"
89 (cond
90 ((equal org-babel-c-variant 'c) ".c")
91 ((equal org-babel-c-variant 'cpp) ".cpp"))))
92 (tmp-bin-file (org-babel-temp-file "C-bin-" org-babel-exeext))
93 (cmdline (cdr (assoc :cmdline params)))
94 (flags (cdr (assoc :flags params)))
95 (full-body (org-babel-C-expand body params))
96 (compile
97 (progn
98 (with-temp-file tmp-src-file (insert full-body))
99 (org-babel-eval
100 (format "%s -o %s %s %s"
101 (cond
102 ((equal org-babel-c-variant 'c) org-babel-C-compiler)
103 ((equal org-babel-c-variant 'cpp) org-babel-C++-compiler))
104 (org-babel-process-file-name tmp-bin-file)
105 (mapconcat 'identity
106 (if (listp flags) flags (list flags)) " ")
107 (org-babel-process-file-name tmp-src-file)) ""))))
108 (let ((results
109 (org-babel-trim
110 (org-remove-indentation
111 (org-babel-eval
112 (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))
113 (org-babel-reassemble-table
114 (org-babel-result-cond (cdr (assoc :result-params params))
115 (org-babel-read results t)
116 (let ((tmp-file (org-babel-temp-file "c-")))
117 (with-temp-file tmp-file (insert results))
118 (org-babel-import-elisp-from-file tmp-file)))
119 (org-babel-pick-name
120 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
121 (org-babel-pick-name
122 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
125 (defun org-babel-C-expand (body params)
126 "Expand a block of C or C++ code with org-babel according to
127 it's header arguments."
128 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
129 (main-p (not (string= (cdr (assoc :main params)) "no")))
130 (includes (or (cdr (assoc :includes params))
131 (org-babel-read (org-entry-get nil "includes" t))))
132 (defines (org-babel-read
133 (or (cdr (assoc :defines params))
134 (org-babel-read (org-entry-get nil "defines" t))))))
135 (mapconcat 'identity
136 (list
137 ;; includes
138 (mapconcat
139 (lambda (inc) (format "#include %s" inc))
140 (if (listp includes) includes (list includes)) "\n")
141 ;; defines
142 (mapconcat
143 (lambda (inc) (format "#define %s" inc))
144 (if (listp defines) defines (list defines)) "\n")
145 ;; variables
146 (mapconcat 'org-babel-C-var-to-C vars "\n")
147 ;; body
148 (if main-p
149 (org-babel-C-ensure-main-wrap body)
150 body) "\n") "\n")))
152 (defun org-babel-C-ensure-main-wrap (body)
153 "Wrap BODY in a \"main\" function call if none exists."
154 (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
155 body
156 (format "int main() {\n%s\nreturn 0;\n}\n" body)))
158 (defun org-babel-prep-session:C (session params)
159 "This function does nothing as C is a compiled language with no
160 support for sessions"
161 (error "C is a compiled languages -- no support for sessions"))
163 (defun org-babel-load-session:C (session body params)
164 "This function does nothing as C is a compiled language with no
165 support for sessions"
166 (error "C is a compiled languages -- no support for sessions"))
168 ;; helper functions
170 (defun org-babel-C-format-val (type val)
171 "Handle the FORMAT part of TYPE with the data from VAL."
172 (let ((format-data (cadr type)))
173 (if (stringp format-data)
174 (cons "" (format format-data val))
175 (funcall format-data val))))
177 (defun org-babel-C-val-to-C-type (val)
178 "Determine the type of VAL.
179 Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type.
180 FORMAT can be either a format string or a function which is called with VAL."
181 (cond
182 ((integerp val) '("int" "%d"))
183 ((floatp val) '("double" "%f"))
184 ((or (listp val) (vectorp val))
185 (lexical-let ((type (org-babel-C-val-to-C-list-type val)))
186 (list (car type)
187 (lambda (val)
188 (cons
189 (format "[%d]%s"
190 (length val)
191 (car (org-babel-C-format-val type (elt val 0))))
192 (concat "{ "
193 (mapconcat (lambda (v)
194 (cdr (org-babel-C-format-val type v)))
196 ", ")
197 " }"))))))
198 (t ;; treat unknown types as string
199 '("char" (lambda (val)
200 (let ((s (format "%s" val))) ;; convert to string for unknown types
201 (cons (format "[%d]" (1+ (length s)))
202 (concat "\"" s "\""))))))))
204 (defun org-babel-C-val-to-C-list-type (val)
205 "Determine the C array type of a VAL."
206 (let (type)
207 (mapc
208 #'(lambda (i)
209 (let* ((tmp-type (org-babel-C-val-to-C-type i))
210 (type-name (car type))
211 (tmp-type-name (car tmp-type)))
212 (when (and type (not (string= type-name tmp-type-name)))
213 (if (and (member type-name '("int" "double" "int32_t"))
214 (member tmp-type-name '("int" "double" "int32_t")))
215 (setq tmp-type '("double" "" "%f"))
216 (error "Only homogeneous lists are supported by C. You can not mix %s and %s"
217 type-name
218 tmp-type-name)))
219 (setq type tmp-type)))
220 val)
221 type))
223 (defun org-babel-C-var-to-C (pair)
224 "Convert an elisp val into a string of C code specifying a var
225 of the same value."
226 ;; TODO list support
227 (let ((var (car pair))
228 (val (cdr pair)))
229 (when (symbolp val)
230 (setq val (symbol-name val))
231 (when (= (length val) 1)
232 (setq val (string-to-char val))))
233 (let* ((type-data (org-babel-C-val-to-C-type val))
234 (type (car type-data))
235 (formated (org-babel-C-format-val type-data val))
236 (suffix (car formated))
237 (data (cdr formated)))
238 (format "%s %s%s = %s;"
239 type
241 suffix
242 data))))
244 (provide 'ob-C)
246 ;;; ob-C.el ends here