org.texi: Fix documentation about C-RET and M-RET
[org-mode.git] / lisp / ob-C.el
blob0bd911ed5a6b88aae454410bf670c01b1369fd26
1 ;;; ob-C.el --- Babel Functions for C and Similar Languages -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2016 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Thierry Banel
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating C, C++, D code.
29 ;; very limited implementation:
30 ;; - currently only support :results output
31 ;; - not much in the way of error feedback
33 ;;; Code:
35 (require 'cc-mode)
36 (require 'ob)
39 (declare-function org-entry-get "org" (pom property &optional inherit literal-nil))
40 (declare-function org-remove-indentation "org" (code &optional n))
41 (declare-function org-trim "org" (s &optional keep-lead))
43 (defvar org-babel-tangle-lang-exts)
44 (add-to-list 'org-babel-tangle-lang-exts '("C++" . "cpp"))
45 (add-to-list 'org-babel-tangle-lang-exts '("D" . "d"))
47 (defvar org-babel-default-header-args:C '())
49 (defcustom org-babel-C-compiler "gcc"
50 "Command used to compile a C source code file into an executable.
51 May be either a command in the path, like gcc
52 or an absolute path name, like /usr/local/bin/gcc
53 parameter may be used, like gcc -v"
54 :group 'org-babel
55 :version "24.3"
56 :type 'string)
58 (defcustom org-babel-C++-compiler "g++"
59 "Command used to compile a C++ source code file into an executable.
60 May be either a command in the path, like g++
61 or an absolute path name, like /usr/local/bin/g++
62 parameter may be used, like g++ -v"
63 :group 'org-babel
64 :version "24.3"
65 :type 'string)
67 (defcustom org-babel-D-compiler "rdmd"
68 "Command used to compile and execute a D source code file.
69 May be either a command in the path, like rdmd
70 or an absolute path name, like /usr/local/bin/rdmd
71 parameter may be used, like rdmd --chatty"
72 :group 'org-babel
73 :version "24.3"
74 :type 'string)
76 (defvar org-babel-c-variant nil
77 "Internal variable used to hold which type of C (e.g. C or C++ or D)
78 is currently being evaluated.")
80 (defun org-babel-execute:cpp (body params)
81 "Execute BODY according to PARAMS.
82 This function calls `org-babel-execute:C++'."
83 (org-babel-execute:C++ body params))
85 (defun org-babel-expand-body:cpp (body params)
86 "Expand a block of C++ code with org-babel according to its
87 header arguments."
88 (org-babel-expand-body:C++ body params))
90 (defun org-babel-execute:C++ (body params)
91 "Execute a block of C++ code with org-babel.
92 This function is called by `org-babel-execute-src-block'."
93 (let ((org-babel-c-variant 'cpp)) (org-babel-C-execute body params)))
95 (defun org-babel-expand-body:C++ (body params)
96 "Expand a block of C++ code with org-babel according to its
97 header arguments."
98 (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand-C++ body params)))
100 (defun org-babel-execute:D (body params)
101 "Execute a block of D code with org-babel.
102 This function is called by `org-babel-execute-src-block'."
103 (let ((org-babel-c-variant 'd)) (org-babel-C-execute body params)))
105 (defun org-babel-expand-body:D (body params)
106 "Expand a block of D code with org-babel according to its
107 header arguments."
108 (let ((org-babel-c-variant 'd)) (org-babel-C-expand-D body params)))
110 (defun org-babel-execute:C (body params)
111 "Execute a block of C code with org-babel.
112 This function is called by `org-babel-execute-src-block'."
113 (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params)))
115 (defun org-babel-expand-body:C (body params)
116 "Expand a block of C code with org-babel according to its
117 header arguments."
118 (let ((org-babel-c-variant 'c)) (org-babel-C-expand-C body params)))
120 (defun org-babel-C-execute (body params)
121 "This function should only be called by `org-babel-execute:C'
122 or `org-babel-execute:C++' or `org-babel-execute:D'."
123 (let* ((tmp-src-file (org-babel-temp-file
124 "C-src-"
125 (pcase org-babel-c-variant
126 (`c ".c") (`cpp ".cpp") (`d ".d"))))
127 (tmp-bin-file ;not used for D
128 (org-babel-temp-file "C-bin-" org-babel-exeext))
129 (cmdline (cdr (assq :cmdline params)))
130 (cmdline (if cmdline (concat " " cmdline) ""))
131 (flags (cdr (assq :flags params)))
132 (flags (mapconcat 'identity
133 (if (listp flags) flags (list flags)) " "))
134 (libs (org-babel-read
135 (or (cdr (assq :libs params))
136 (org-entry-get nil "libs" t))
137 nil))
138 (libs (mapconcat #'identity
139 (if (listp libs) libs (list libs))
140 " "))
141 (full-body
142 (pcase org-babel-c-variant
143 (`c (org-babel-C-expand-C body params))
144 (`cpp (org-babel-C-expand-C++ body params))
145 (`d (org-babel-C-expand-D body params)))))
146 (with-temp-file tmp-src-file (insert full-body))
147 (pcase org-babel-c-variant
148 ((or `c `cpp)
149 (org-babel-eval
150 (format "%s -o %s %s %s %s"
151 (pcase org-babel-c-variant
152 (`c org-babel-C-compiler)
153 (`cpp org-babel-C++-compiler))
154 (org-babel-process-file-name tmp-bin-file)
155 flags
156 (org-babel-process-file-name tmp-src-file)
157 libs)
158 ""))
159 (`d nil)) ;; no separate compilation for D
160 (let ((results
161 (org-babel-eval
162 (pcase org-babel-c-variant
163 ((or `c `cpp)
164 (concat tmp-bin-file cmdline))
166 (format "%s %s %s %s"
167 org-babel-D-compiler
168 flags
169 (org-babel-process-file-name tmp-src-file)
170 cmdline)))
171 "")))
172 (when results
173 (setq results (org-trim (org-remove-indentation results)))
174 (org-babel-reassemble-table
175 (org-babel-result-cond (cdr (assq :result-params params))
176 (org-babel-read results t)
177 (let ((tmp-file (org-babel-temp-file "c-")))
178 (with-temp-file tmp-file (insert results))
179 (org-babel-import-elisp-from-file tmp-file)))
180 (org-babel-pick-name
181 (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
182 (org-babel-pick-name
183 (cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))
186 (defun org-babel-C-expand-C++ (body params)
187 "Expand a block of C or C++ code with org-babel according to
188 its header arguments."
189 (org-babel-C-expand-C body params))
191 (defun org-babel-C-expand-C (body params)
192 "Expand a block of C or C++ code with org-babel according to
193 its header arguments."
194 (let ((vars (org-babel--get-vars params))
195 (colnames (cdr (assq :colname-names params)))
196 (main-p (not (string= (cdr (assq :main params)) "no")))
197 (includes (org-babel-read
198 (or (cdr (assq :includes params))
199 (org-entry-get nil "includes" t))
200 nil))
201 (defines (org-babel-read
202 (or (cdr (assq :defines params))
203 (org-entry-get nil "defines" t))
204 nil)))
205 (when (stringp includes)
206 (setq includes (split-string includes)))
207 (when (stringp defines)
208 (let ((y nil)
209 (result (list t)))
210 (dolist (x (split-string defines))
211 (if (null y)
212 (setq y x)
213 (nconc result (list (concat y " " x)))
214 (setq y nil)))
215 (setq defines (cdr result))))
216 (mapconcat 'identity
217 (list
218 ;; includes
219 (mapconcat
220 (lambda (inc) (format "#include %s" inc))
221 includes "\n")
222 ;; defines
223 (mapconcat
224 (lambda (inc) (format "#define %s" inc))
225 (if (listp defines) defines (list defines)) "\n")
226 ;; variables
227 (mapconcat 'org-babel-C-var-to-C vars "\n")
228 ;; table sizes
229 (mapconcat 'org-babel-C-table-sizes-to-C vars "\n")
230 ;; tables headers utility
231 (when colnames
232 (org-babel-C-utility-header-to-C))
233 ;; tables headers
234 (mapconcat 'org-babel-C-header-to-C colnames "\n")
235 ;; body
236 (if main-p
237 (org-babel-C-ensure-main-wrap body)
238 body) "\n") "\n")))
240 (defun org-babel-C-expand-D (body params)
241 "Expand a block of D code with org-babel according to
242 its header arguments."
243 (let ((vars (org-babel--get-vars params))
244 (colnames (cdr (assq :colname-names params)))
245 (main-p (not (string= (cdr (assq :main params)) "no")))
246 (imports (or (cdr (assq :imports params))
247 (org-babel-read (org-entry-get nil "imports" t)))))
248 (when (stringp imports)
249 (setq imports (split-string imports)))
250 (setq imports (append imports '("std.stdio" "std.conv")))
251 (mapconcat 'identity
252 (list
253 "module mmm;"
254 ;; imports
255 (mapconcat
256 (lambda (inc) (format "import %s;" inc))
257 imports "\n")
258 ;; variables
259 (mapconcat 'org-babel-C-var-to-C vars "\n")
260 ;; table sizes
261 (mapconcat 'org-babel-C-table-sizes-to-C vars "\n")
262 ;; tables headers utility
263 (when colnames
264 (org-babel-C-utility-header-to-C))
265 ;; tables headers
266 (mapconcat 'org-babel-C-header-to-C colnames "\n")
267 ;; body
268 (if main-p
269 (org-babel-C-ensure-main-wrap body)
270 body) "\n") "\n")))
272 (defun org-babel-C-ensure-main-wrap (body)
273 "Wrap BODY in a \"main\" function call if none exists."
274 (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
275 body
276 (format "int main() {\n%s\nreturn 0;\n}\n" body)))
278 (defun org-babel-prep-session:C (_session _params)
279 "This function does nothing as C is a compiled language with no
280 support for sessions"
281 (error "C is a compiled languages -- no support for sessions"))
283 (defun org-babel-load-session:C (_session _body _params)
284 "This function does nothing as C is a compiled language with no
285 support for sessions"
286 (error "C is a compiled languages -- no support for sessions"))
288 ;; helper functions
290 (defun org-babel-C-format-val (type val)
291 "Handle the FORMAT part of TYPE with the data from VAL."
292 (let ((format-data (cadr type)))
293 (if (stringp format-data)
294 (cons "" (format format-data val))
295 (funcall format-data val))))
297 (defun org-babel-C-val-to-C-type (val)
298 "Determine the type of VAL.
299 Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type.
300 FORMAT can be either a format string or a function which is called with VAL."
301 (let* ((basetype (org-babel-C-val-to-base-type val))
302 (type
303 (pcase basetype
304 (`integerp '("int" "%d"))
305 (`floatp '("double" "%f"))
306 (`stringp
307 (list
308 (if (eq org-babel-c-variant 'd) "string" "const char*")
309 "\"%s\""))
310 (_ (error "unknown type %S" basetype)))))
311 (cond
312 ((integerp val) type) ;; an integer declared in the #+begin_src line
313 ((floatp val) type) ;; a numeric declared in the #+begin_src line
314 ((and (listp val) (listp (car val))) ;; a table
315 `(,(car type)
316 (lambda (val)
317 (cons
318 (format "[%d][%d]" (length val) (length (car val)))
319 (concat
320 (if (eq org-babel-c-variant 'd) "[\n" "{\n")
321 (mapconcat
322 (lambda (v)
323 (concat
324 (if (eq org-babel-c-variant 'd) " [" " {")
325 (mapconcat (lambda (w) (format ,(cadr type) w)) v ",")
326 (if (eq org-babel-c-variant 'd) "]" "}")))
328 ",\n")
329 (if (eq org-babel-c-variant 'd) "\n]" "\n}"))))))
330 ((or (listp val) (vectorp val)) ;; a list declared in the #+begin_src line
331 `(,(car type)
332 (lambda (val)
333 (cons
334 (format "[%d]" (length val))
335 (concat
336 (if (eq org-babel-c-variant 'd) "[" "{")
337 (mapconcat (lambda (v) (format ,(cadr type) v)) val ",")
338 (if (eq org-babel-c-variant 'd) "]" "}"))))))
339 (t ;; treat unknown types as string
340 type))))
342 (defun org-babel-C-val-to-base-type (val)
343 "Determine the base type of VAL which may be
344 `integerp' if all base values are integers
345 `floatp' if all base values are either floating points or integers
346 `stringp' otherwise."
347 (cond
348 ((integerp val) 'integerp)
349 ((floatp val) 'floatp)
350 ((or (listp val) (vectorp val))
351 (let ((type nil))
352 (mapc (lambda (v)
353 (pcase (org-babel-C-val-to-base-type v)
354 (`stringp (setq type 'stringp))
355 (`floatp
356 (if (or (not type) (eq type 'integerp))
357 (setq type 'floatp)))
358 (`integerp
359 (unless type (setq type 'integerp)))))
360 val)
361 type))
362 (t 'stringp)))
364 (defun org-babel-C-var-to-C (pair)
365 "Convert an elisp val into a string of C code specifying a var
366 of the same value."
367 ;; TODO list support
368 (let ((var (car pair))
369 (val (cdr pair)))
370 (when (symbolp val)
371 (setq val (symbol-name val))
372 (when (= (length val) 1)
373 (setq val (string-to-char val))))
374 (let* ((type-data (org-babel-C-val-to-C-type val))
375 (type (car type-data))
376 (formated (org-babel-C-format-val type-data val))
377 (suffix (car formated))
378 (data (cdr formated)))
379 (format "%s %s%s = %s;"
380 type
382 suffix
383 data))))
385 (defun org-babel-C-table-sizes-to-C (pair)
386 "Create constants of table dimensions, if PAIR is a table."
387 (when (listp (cdr pair))
388 (cond
389 ((listp (cadr pair)) ;; a table
390 (concat
391 (format "const int %s_rows = %d;" (car pair) (length (cdr pair)))
392 "\n"
393 (format "const int %s_cols = %d;" (car pair) (length (cadr pair)))))
394 (t ;; a list declared in the #+begin_src line
395 (format "const int %s_cols = %d;" (car pair) (length (cdr pair)))))))
397 (defun org-babel-C-utility-header-to-C ()
398 "Generate a utility function to convert a column name
399 into a column number."
400 (pcase org-babel-c-variant
401 ((or `c `cpp)
402 "int get_column_num (int nbcols, const char** header, const char* column)
404 int c;
405 for (c=0; c<nbcols; c++)
406 if (strcmp(header[c],column)==0)
407 return c;
408 return -1;
412 "int get_column_num (string[] header, string column)
414 foreach (c, h; header)
415 if (h==column)
416 return to!int(c);
417 return -1;
419 ")))
421 (defun org-babel-C-header-to-C (head)
422 "Convert an elisp list of header table into a C or D vector
423 specifying a variable with the name of the table."
424 (let ((table (car head))
425 (headers (cdr head)))
426 (concat
427 (format
428 (pcase org-babel-c-variant
429 ((or `c `cpp) "const char* %s_header[%d] = {%s};")
430 (`d "string %s_header[%d] = [%s];"))
431 table
432 (length headers)
433 (mapconcat (lambda (h) (format "%S" h)) headers ","))
434 "\n"
435 (pcase org-babel-c-variant
436 ((or `c `cpp)
437 (format
438 "const char* %s_h (int row, const char* col) { return %s[row][get_column_num(%d,%s_header,col)]; }"
439 table table (length headers) table))
441 (format
442 "string %s_h (size_t row, string col) { return %s[row][get_column_num(%s_header,col)]; }"
443 table table table))))))
445 (provide 'ob-C)
447 ;;; ob-C.el ends here