ob-C.el 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ;;; ob-C.el --- org-babel functions for C and similar languages
  2. ;; Copyright (C) 2010 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Org-Babel support for evaluating C code.
  24. ;;
  25. ;; very limited implementation:
  26. ;; - currently only support :results output
  27. ;; - not much in the way of error feedback
  28. ;;; Code:
  29. (require 'ob)
  30. (require 'cc-mode)
  31. (defvar org-babel-C-compiler "gcc"
  32. "Command used to compile a C source code file into an
  33. executable.")
  34. (defvar org-babel-c++-compiler "g++"
  35. "Command used to compile a c++ source code file into an
  36. executable.")
  37. (defun org-babel-execute:cpp (body params)
  38. "Execute BODY according to PARAMS. This function calls
  39. `org-babel-execute:C'."
  40. (org-babel-execute:C body params))
  41. (defun org-babel-execute:c++ (body params)
  42. "Execute a block of C++ code with org-babel. This function is
  43. called by `org-babel-execute-src-block'."
  44. (let ((c-variant 'cpp)) (org-babel-C-execute body params)))
  45. (defun org-babel-expand-body:c++ (body params &optional processed-params)
  46. "Expand a block of C++ code with org-babel according to it's
  47. header arguments (calls `org-babel-C-expand')."
  48. (let ((c-variant 'cpp)) (org-babel-C-expand body params processed-params)))
  49. (defun org-babel-execute:C (body params)
  50. "Execute a block of C code with org-babel. This function is
  51. called by `org-babel-execute-src-block'."
  52. (let ((c-variant 'c)) (org-babel-C-execute body params)))
  53. (defun org-babel-expand-body:c (body params &optional processed-params)
  54. "Expand a block of C code with org-babel according to it's
  55. header arguments (calls `org-babel-C-expand')."
  56. (let ((c-variant 'c)) (org-babel-C-expand body params processed-params)))
  57. (defun org-babel-C-execute (body params)
  58. "This function should only be called by `org-babel-execute:C'
  59. or `org-babel-execute:c++'."
  60. (message "executing C source code block")
  61. (let* ((processed-params (org-babel-process-params params))
  62. (tmp-src-file (make-temp-file "org-babel-C-src" nil
  63. (case c-variant
  64. ('c ".c")
  65. ('cpp ".cpp"))))
  66. (tmp-bin-file (make-temp-file "org-babel-C-bin"))
  67. (tmp-out-file (make-temp-file "org-babel-C-out"))
  68. (cmdline (cdr (assoc :cmdline params)))
  69. (flags (cdr (assoc :flags params)))
  70. (full-body (org-babel-C-expand body params))
  71. (error-buf (get-buffer-create "*Org-Babel Error Output*"))
  72. (compile
  73. (progn
  74. (with-temp-file tmp-src-file (insert full-body))
  75. (with-temp-buffer
  76. (org-babel-shell-command-on-region
  77. (point-min) (point-max)
  78. (format "%s -o %s %s %s"
  79. (case c-variant
  80. ('c org-babel-C-compiler)
  81. ('cpp org-babel-c++-compiler))
  82. tmp-bin-file
  83. (mapconcat 'identity
  84. (if (listp flags) flags (list flags)) " ")
  85. tmp-src-file)
  86. (current-buffer) 'replace error-buf)))))
  87. (if (= compile 0)
  88. (org-babel-reassemble-table
  89. (org-babel-read
  90. (org-babel-trim
  91. (with-temp-buffer
  92. (org-babel-shell-command-on-region
  93. (point-min) (point-max)
  94. (concat tmp-bin-file (if cmdline (concat " " cmdline) ""))
  95. (current-buffer) 'replace)
  96. (buffer-string))))
  97. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  98. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params))))
  99. (progn
  100. (with-current-buffer error-buf
  101. (goto-char (point-max))
  102. (insert (concat "\n\n--body--\n" full-body))
  103. (goto-char (point-min)))
  104. (display-buffer error-buf) nil))))
  105. (defun org-babel-C-expand (body params &optional processed-params)
  106. "Expand a block of C or C++ code with org-babel according to
  107. it's header arguments."
  108. (let ((vars (second (or processed-params
  109. (org-babel-process-params params))))
  110. (main-p (not (string= (cdr (assoc :main params)) "no")))
  111. (includes (or (cdr (assoc :includes params))
  112. (org-babel-read (org-entry-get nil "includes" t))))
  113. (defines (org-babel-read
  114. (or (cdr (assoc :defines params))
  115. (org-babel-read (org-entry-get nil "defines" t))))))
  116. (mapconcat 'identity
  117. (list
  118. ;; includes
  119. (mapconcat
  120. (lambda (inc) (format "#include %s" inc))
  121. (if (listp includes) includes (list includes)) "\n")
  122. ;; defines
  123. (mapconcat
  124. (lambda (inc) (format "#define %s" inc))
  125. (if (listp defines) defines (list defines)) "\n")
  126. ;; variables
  127. (mapconcat 'org-babel-C-var-to-C vars "\n")
  128. ;; body
  129. (if main-p
  130. (org-babel-C-ensure-main-wrap body)
  131. body) "\n") "\n")))
  132. (defun org-babel-C-ensure-main-wrap (body)
  133. "Wrap body in a \"main\" function call if none exists."
  134. (if (string-match "^[ \t]*[intvod]+[ \t]*main[ \t]*(.*)" body)
  135. body
  136. (format "int main() {\n%s\n}\n" body)))
  137. (defun org-babel-prep-session:C (session params)
  138. "This function does nothing as C is a compiled language with no
  139. support for sessions"
  140. (error "C is a compiled languages -- no support for sessions"))
  141. (defun org-babel-load-session:C (session body params)
  142. "This function does nothing as C is a compiled language with no
  143. support for sessions"
  144. (error "C is a compiled languages -- no support for sessions"))
  145. ;; helper functions
  146. (defun org-babel-C-var-to-C (pair)
  147. "Convert an elisp val into a string of C code specifying a var
  148. of the same value."
  149. ;; TODO list support
  150. (let ((var (car pair))
  151. (val (cdr pair)))
  152. (when (symbolp val)
  153. (setq val (symbol-name val))
  154. (when (= (length val) 1)
  155. (setq val (string-to-char val))))
  156. (cond
  157. ((integerp val)
  158. (format "int %S = %S;" var val))
  159. ((floatp val)
  160. (format "double %S = %S;" var val))
  161. ((or (characterp val))
  162. (format "char %S = '%S';" var val))
  163. ((stringp val)
  164. (format "char %S[%d] = \"%s\";"
  165. var (+ 1 (length val)) val))
  166. (t
  167. (format "u32 %S = %S;" var val)))))
  168. (provide 'ob-C)
  169. ;;; ob-C.el ends here