ob-C.el 7.6 KB

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