Merge branch 'command-name-fixes'
[org-mode/org-jambu.git] / lisp / ob-C.el
blob1c8eac65acef40011279654bb99bd53eaf6f31fc
1 ;;; ob-C.el --- org-babel functions for C and similar languages
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.3
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 code.
29 ;; very limited implementation:
30 ;; - currently only support :results output
31 ;; - not much in the way of error feedback
33 ;;; Code:
34 (require 'ob)
35 (require 'ob-eval)
36 (require 'cc-mode)
38 (declare-function org-entry-get "org"
39 (pom property &optional inherit literal-nil))
41 (add-to-list 'org-babel-tangle-lang-exts '("c++" . "cpp"))
43 (defvar org-babel-default-header-args:C '())
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 (defvar org-babel-c-variant nil
54 "Internal variable used to hold which type of C (e.g. C or C++)
55 is currently being evaluated.")
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))
62 (defun org-babel-execute:c++ (body params)
63 "Execute a block of C++ code with org-babel. This function is
64 called by `org-babel-execute-src-block'."
65 (let ((org-babel-c-variant 'cpp)) (org-babel-C-execute body params)))
67 (defun org-babel-expand-body:c++ (body params)
68 "Expand a block of C++ code with org-babel according to it's
69 header arguments (calls `org-babel-C-expand')."
70 (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand body params)))
72 (defun org-babel-execute:C (body params)
73 "Execute a block of C code with org-babel. This function is
74 called by `org-babel-execute-src-block'."
75 (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params)))
77 (defun org-babel-expand-body:c (body params)
78 "Expand a block of C code with org-babel according to it's
79 header arguments (calls `org-babel-C-expand')."
80 (let ((org-babel-c-variant 'c)) (org-babel-C-expand body params)))
82 (defun org-babel-C-execute (body params)
83 "This function should only be called by `org-babel-execute:C'
84 or `org-babel-execute:c++'."
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-"))
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))
102 (org-babel-process-file-name tmp-bin-file)
103 (mapconcat 'identity
104 (if (listp flags) flags (list flags)) " ")
105 (org-babel-process-file-name tmp-src-file)) ""))))
106 ((lambda (results)
107 (org-babel-reassemble-table
108 (if (member "vector" (cdr (assoc :result-params params)))
109 (let ((tmp-file (org-babel-temp-file "c-")))
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
114 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
115 (org-babel-pick-name
116 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
117 (org-babel-trim
118 (org-babel-eval
119 (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))
121 (defun org-babel-C-expand (body params)
122 "Expand a block of C or C++ code with org-babel according to
123 it's header arguments."
124 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
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))))))
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)
146 body) "\n") "\n")))
148 (defun org-babel-C-ensure-main-wrap (body)
149 "Wrap body in a \"main\" function call if none exists."
150 (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
151 body
152 (format "int main() {\n%s\n}\n" body)))
154 (defun org-babel-prep-session:C (session params)
155 "This function does nothing as C is a compiled language with no
156 support for sessions"
157 (error "C is a compiled languages -- no support for sessions"))
159 (defun org-babel-load-session:C (session body params)
160 "This function does nothing as C is a compiled language with no
161 support for sessions"
162 (error "C is a compiled languages -- no support for sessions"))
164 ;; helper functions
166 (defun org-babel-C-var-to-C (pair)
167 "Convert an elisp val into a string of C code specifying a var
168 of 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))
187 (format "u32 %S = %S;" var val)))))
190 (provide 'ob-C)
192 ;; arch-tag: 8f49e462-54e3-417b-9a8d-423864893b37
194 ;;; ob-C.el ends here