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
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)
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.
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
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
49 (defvar org-babel-c
++-compiler
"g++"
50 "Command used to compile a c++ source code file into an
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
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
89 (lambda (inc) (format "#include %s" inc
))
90 (if (listp includes
) includes
(list includes
)) "\n")
93 (lambda (inc) (format "#define %s" inc
))
94 (if (listp defines
) defines
(list defines
)) "\n")
96 (mapconcat 'org-babel-C-var-to-C vars
"\n")
98 "\n" (org-babel-C-ensure-main-wrap body
) "\n") "\n"))
99 (error-buf (get-buffer-create "*Org-Babel Error Output*"))
102 (with-temp-file tmp-src-file
(insert full-body
))
104 (org-babel-shell-command-on-region
105 (point-min) (point-max)
106 (format "%s -o %s %s %s"
108 ('c org-babel-C-compiler
)
109 ('cpp org-babel-c
++-compiler
))
112 (if (listp flags
) flags
(list flags
)) " ")
114 (current-buffer) 'replace error-buf
)))))
119 (org-babel-shell-command-on-region
120 (point-min) (point-max) tmp-bin-file
(current-buffer) 'replace
)
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
)
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"))
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
))
150 ((integerp val
) "int")
151 ((floatp val
) "double")
152 ((characterp val
) "char")
153 ((stringp val
) (format "char[%d]" (length val
)))
155 (format "%s %S = %S;" type var val
)))
157 (provide 'org-babel-C
)
158 ;;; org-babel-C.el ends here