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