summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Kamat <jaygkamat@gmail.com>2017-07-16 21:55:24 -0700
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2017-08-01 10:20:15 +0200
commit0c1b4da1f620067c8e0925f47d13769a302b9546 (patch)
tree12791a93abb2b42de4c12392a790bcad7fa402b2
parent27e5beaa64a0720e4935d68da2dd61de79762f4e (diff)
downloadorg-mode-0c1b4da1f620067c8e0925f47d13769a302b9546.tar.gz
ob-C.el: Add support for specifying namespaces in C/C++
* lisp/ob-C.el (org-babel-C-expand-C): Add a :namespaces export option to C++ org babel blocks. Namespaces specified here will be added to the file in the format 'using namespace %s;'. Multiple namespaces can be specified, separated by spaces. TINYCHANGE
-rw-r--r--etc/ORG-NEWS14
-rw-r--r--lisp/ob-C.el31
2 files changed, 40 insertions, 5 deletions
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index d7bd3e2..e0fe4e5 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -218,6 +218,20 @@ To use =vertica= in an sql =SRC_BLK= set the =:engine= like this:
SELECT * FROM nodes;
,#+END_SRC
#+END_EXAMPLE
+**** C++: New header ~:namespaces~
+
+The new ~:namespaces~ export option can be used to specify namespaces
+to be used within a C++ org source block. Its usage is similar to
+~:includes~, in that it can accept multiple, space-separated
+namespaces to use. This header is equivalent to adding ~using
+namespace <name>;~ in the source block. Here is a "Hello World" in C++
+using ~:namespaces~:
+
+#+begin_example
+ ,#+BEGIN_SRC C++ :results output :namespaces std :includes <iostream>
+ cout << "Hello World" << endl;
+ ,#+END_SRC
+#+end_example
*** New ~function~ scope argument for the Clock Table
Added a nullary function that returns a list of files as a possible
diff --git a/lisp/ob-C.el b/lisp/ob-C.el
index 2bdda68..f655e6e 100644
--- a/lisp/ob-C.el
+++ b/lisp/ob-C.el
@@ -46,6 +46,19 @@
(defvar org-babel-default-header-args:C '())
+(defconst org-babel-header-args:C '((includes . :any)
+ (defines . :any)
+ (main . :any)
+ (flags . :any)
+ (cmdline . :any)
+ (libs . :any))
+ "C/C++-specific header arguments.")
+
+(defconst org-babel-header-args:C++
+ (append '((namespaces . :any))
+ org-babel-header-args:C)
+ "C++-specific header arguments.")
+
(defcustom org-babel-C-compiler "gcc"
"Command used to compile a C source code file into an executable.
May be either a command in the path, like gcc
@@ -196,15 +209,18 @@ its header arguments."
(colnames (cdr (assq :colname-names params)))
(main-p (not (string= (cdr (assq :main params)) "no")))
(includes (org-babel-read
- (or (cdr (assq :includes params))
- (org-entry-get nil "includes" t))
+ (cdr (assq :includes params))
nil))
(defines (org-babel-read
- (or (cdr (assq :defines params))
- (org-entry-get nil "defines" t))
- nil)))
+ (cdr (assq :defines params))
+ nil))
+ (namespaces (org-babel-read
+ (cdr (assq :namespaces params))
+ nil)))
(when (stringp includes)
(setq includes (split-string includes)))
+ (when (stringp namespaces)
+ (setq namespaces (split-string namespaces)))
(when (stringp defines)
(let ((y nil)
(result (list t)))
@@ -224,6 +240,11 @@ its header arguments."
(mapconcat
(lambda (inc) (format "#define %s" inc))
(if (listp defines) defines (list defines)) "\n")
+ ;; namespaces
+ (mapconcat
+ (lambda (inc) (format "using namespace %s;" inc))
+ namespaces
+ "\n")
;; variables
(mapconcat 'org-babel-C-var-to-C vars "\n")
;; table sizes