org-protocol default template should be nil
[org-mode.git] / lisp / ob-C.el
blob18921747c6116fcc715fd3095af53d3d33dfc490
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.01trans
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 'org)
37 (require 'cc-mode)
39 (declare-function org-entry-get "org"
40 (pom property &optional inherit literal-nil))
42 (add-to-list 'org-babel-tangle-lang-exts '("c++" . "cpp"))
44 (defvar org-babel-default-header-args:C '())
46 (defvar org-babel-C-compiler "gcc"
47 "Command used to compile a C source code file into an
48 executable.")
50 (defvar org-babel-c++-compiler "g++"
51 "Command used to compile a c++ source code file into an
52 executable.")
54 (defvar org-babel-c-variant nil
55 "Internal variable used to hold which type of C (e.g. C or C++)
56 is currently being evaluated.")
58 (defun org-babel-execute:cpp (body params)
59 "Execute BODY according to PARAMS. This function calls
60 `org-babel-execute:C'."
61 (org-babel-execute:C body params))
63 (defun org-babel-execute:c++ (body params)
64 "Execute a block of C++ code with org-babel. This function is
65 called by `org-babel-execute-src-block'."
66 (let ((org-babel-c-variant 'cpp)) (org-babel-C-execute body params)))
68 (defun org-babel-expand-body:c++ (body params &optional processed-params)
69 "Expand a block of C++ code with org-babel according to it's
70 header arguments (calls `org-babel-C-expand')."
71 (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand body params processed-params)))
73 (defun org-babel-execute:C (body params)
74 "Execute a block of C code with org-babel. This function is
75 called by `org-babel-execute-src-block'."
76 (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params)))
78 (defun org-babel-expand-body:c (body params &optional processed-params)
79 "Expand a block of C code with org-babel according to it's
80 header arguments (calls `org-babel-C-expand')."
81 (let ((org-babel-c-variant 'c)) (org-babel-C-expand body params processed-params)))
83 (defun org-babel-C-execute (body params)
84 "This function should only be called by `org-babel-execute:C'
85 or `org-babel-execute:c++'."
86 (let* ((processed-params (org-babel-process-params params))
87 (tmp-src-file (org-babel-temp-file
88 "C-src-"
89 (cond
90 ((equal org-babel-c-variant 'c) ".c")
91 ((equal org-babel-c-variant 'cpp) ".cpp"))))
92 (tmp-bin-file (org-babel-temp-file "C-bin-"))
93 (tmp-out-file (org-babel-temp-file "C-out-"))
94 (cmdline (cdr (assoc :cmdline params)))
95 (flags (cdr (assoc :flags params)))
96 (full-body (org-babel-C-expand body params))
97 (compile
98 (progn
99 (with-temp-file tmp-src-file (insert full-body))
100 (org-babel-eval
101 (format "%s -o %s %s %s"
102 (cond
103 ((equal org-babel-c-variant 'c) org-babel-C-compiler)
104 ((equal org-babel-c-variant 'cpp) org-babel-c++-compiler))
105 tmp-bin-file
106 (mapconcat 'identity
107 (if (listp flags) flags (list flags)) " ")
108 tmp-src-file) ""))))
109 ((lambda (results)
110 (org-babel-reassemble-table
111 (if (member "vector" (nth 2 processed-params))
112 (let ((tmp-file (org-babel-temp-file "c-")))
113 (with-temp-file tmp-file (insert results))
114 (org-babel-import-elisp-from-file tmp-file))
115 (org-babel-read results))
116 (org-babel-pick-name
117 (nth 4 processed-params) (cdr (assoc :colnames params)))
118 (org-babel-pick-name
119 (nth 5 processed-params) (cdr (assoc :rownames params)))))
120 (org-babel-trim
121 (org-babel-eval
122 (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))
124 (defun org-babel-C-expand (body params &optional processed-params)
125 "Expand a block of C or C++ code with org-babel according to
126 it's header arguments."
127 (let ((vars (nth 1 (or processed-params
128 (org-babel-process-params params))))
129 (main-p (not (string= (cdr (assoc :main params)) "no")))
130 (includes (or (cdr (assoc :includes params))
131 (org-babel-read (org-entry-get nil "includes" t))))
132 (defines (org-babel-read
133 (or (cdr (assoc :defines params))
134 (org-babel-read (org-entry-get nil "defines" t))))))
135 (org-babel-trim
136 (mapconcat 'identity
137 (list
138 ;; includes
139 (mapconcat
140 (lambda (inc) (format "#include %s" inc))
141 (if (listp includes) includes (list includes)) "\n")
142 ;; defines
143 (mapconcat
144 (lambda (inc) (format "#define %s" inc))
145 (if (listp defines) defines (list defines)) "\n")
146 ;; variables
147 (mapconcat 'org-babel-C-var-to-C vars "\n")
148 ;; body
149 (if main-p
150 (org-babel-C-ensure-main-wrap body)
151 body) "\n") "\n"))))
153 (defun org-babel-C-ensure-main-wrap (body)
154 "Wrap body in a \"main\" function call if none exists."
155 (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
156 body
157 (format "int main() {\n%s\n}\n" body)))
159 (defun org-babel-prep-session:C (session 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 (defun org-babel-load-session:C (session body params)
165 "This function does nothing as C is a compiled language with no
166 support for sessions"
167 (error "C is a compiled languages -- no support for sessions"))
169 ;; helper functions
171 (defun org-babel-C-var-to-C (pair)
172 "Convert an elisp val into a string of C code specifying a var
173 of the same value."
174 ;; TODO list support
175 (let ((var (car pair))
176 (val (cdr pair)))
177 (when (symbolp val)
178 (setq val (symbol-name val))
179 (when (= (length val) 1)
180 (setq val (string-to-char val))))
181 (cond
182 ((integerp val)
183 (format "int %S = %S;" var val))
184 ((floatp val)
185 (format "double %S = %S;" var val))
186 ((or (characterp val))
187 (format "char %S = '%S';" var val))
188 ((stringp val)
189 (format "char %S[%d] = \"%s\";"
190 var (+ 1 (length val)) val))
192 (format "u32 %S = %S;" var val)))))
195 (provide 'ob-C)
197 ;; arch-tag: 8f49e462-54e3-417b-9a8d-423864893b37
199 ;;; ob-C.el ends here