babel: org-babel-c more robust, better error messages and c++ support
[org-mode/org-tableheadings.git] / contrib / babel / lisp / langs / org-babel-C.el
blob9ed04a26a9dbbd07d666b2fb79a29f1a22490897
1 ;;; org-babel-C.el --- org-babel functions for C and similar languages
3 ;; Copyright (C) 2010 Eric Schulte
5 ;; Author: Eric Schulte
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 ;; Org-Babel support for evaluating C code.
31 ;; very limited implementation:
32 ;; - currently only support :results output
33 ;; - not much in the way of error feedback
35 ;;; Code:
36 (require 'org-babel)
37 (require 'cc-mode)
39 (org-babel-add-interpreter "C")
40 (add-to-list 'org-babel-tangle-langs '("C" "c" nil))
42 (org-babel-add-interpreter "c++")
43 (add-to-list 'org-babel-tangle-langs '("c++" "cpp" nil))
45 (defvar org-babel-C-compiler "gcc"
46 "Command used to compile a C source code file into an
47 executable.")
49 (defvar org-babel-c++-compiler "g++"
50 "Command used to compile a c++ source code file into an
51 executable.")
53 (defun org-babel-execute:cpp (body params)
54 (org-babel-execute:C body params))
56 (defun org-babel-execute:c++ (body params)
57 "Execute a block of C++ code with org-babel. This function is
58 called by `org-babel-execute-src-block'."
59 (let ((c-variant 'cpp)) (org-babel-C-execute body params)))
61 (defun org-babel-execute:C (body params)
62 "Execute a block of C code with org-babel. This function is
63 called by `org-babel-execute-src-block'."
64 (let ((c-variant 'c)) (org-babel-C-execute body params)))
66 (defun org-babel-C-execute (body params)
67 "This should only be called by `org-babel-execute:C' or
68 `org-babel-execute:c++'."
69 (message "executing C source code block")
70 (let* ((processed-params (org-babel-process-params params))
71 (tmp-src-file (make-temp-file "org-babel-C-src" nil
72 (case c-variant
73 ('c ".c")
74 ('cpp ".cpp"))))
75 (tmp-bin-file (make-temp-file "org-babel-C-bin"))
76 (tmp-out-file (make-temp-file "org-babel-C-out"))
77 (flags (cdr (assoc :flags params)))
78 (vars (second processed-params))
79 (includes (org-babel-read
80 (or (cdr (assoc :includes params))
81 (org-entry-get nil "includes" t))))
82 (defines (org-babel-read
83 (or (cdr (assoc :includes params))
84 (org-entry-get nil "defines" t))))
85 (full-body (mapconcat 'identity
86 (list
87 ;; includes
88 (mapconcat
89 (lambda (inc) (format "#include %s" inc))
90 (if (listp includes) includes (list includes)) "\n")
91 ;; defines
92 (mapconcat
93 (lambda (inc) (format "#define %s" inc))
94 (if (listp defines) defines (list defines)) "\n")
95 ;; variables
96 (mapconcat 'org-babel-C-var-to-C vars "\n")
97 ;; body
98 "\n" (org-babel-C-ensure-main-wrap body) "\n") "\n"))
99 (error-buf (get-buffer-create "*Org-Babel Error Output*"))
100 (compile
101 (progn
102 (with-temp-file tmp-src-file (insert full-body))
103 (with-temp-buffer
104 (org-babel-shell-command-on-region
105 (point-min) (point-max)
106 (format "%s -o %s %s %s"
107 (case c-variant
108 ('c org-babel-C-compiler)
109 ('cpp org-babel-c++-compiler))
110 tmp-bin-file
111 (mapconcat 'identity
112 (if (listp flags) flags (list flags)) " ")
113 tmp-src-file)
114 (current-buffer) 'replace error-buf)))))
115 (if (= compile 0)
116 (org-babel-read
117 (org-babel-trim
118 (with-temp-buffer
119 (org-babel-shell-command-on-region
120 (point-min) (point-max) tmp-bin-file (current-buffer) 'replace)
121 (buffer-string))))
122 (progn
123 (with-current-buffer error-buf
124 (goto-char (point-max))
125 (insert (concat "\n\n--body--\n" full-body)))
126 (display-buffer error-buf) nil))))
128 (defun org-babel-C-ensure-main-wrap (body)
129 "Wrap body in a \"main\" function call if none exists."
130 (if (string-match "^[ \t]*[intvod]+[ \t]*main[ \t]*(.*)" body)
131 body
132 (format "int main() {\n%s\n}\n" body)))
134 (defun org-babel-prep-session:C (session params)
135 "C is a compiled languages -- no support for sessions"
136 (error "C is a compiled languages -- no support for sessions"))
138 (defun org-babel-load-session:C (session body params)
139 "C is a compiled languages -- no support for sessions"
140 (error "C is a compiled languages -- no support for sessions"))
142 ;; helper functions
144 (defun org-babel-C-var-to-C (pair)
145 "Convert an elisp val into a string of C code specifying a var
146 of the same value. TODO list support."
147 (let* ((var (car pair))
148 (val (cdr pair))
149 (type (cond
150 ((integerp val) "int")
151 ((floatp val) "double")
152 ((characterp val) "char")
153 ((stringp val) (format "char[%d]" (length val)))
154 (t "u32"))))
155 (format "%s %S = %S;" type var val)))
157 (provide 'org-babel-C)
158 ;;; org-babel-C.el ends here