From bf08fb4f89024428a95615bdfede86e3c883d87c Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Sun, 16 Jul 2017 21:55:24 -0700 Subject: [PATCH] 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 --- etc/ORG-NEWS | 14 ++++++++++++++ lisp/ob-C.el | 34 ++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 936ecc36b2..1d08f9ba9d 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -200,6 +200,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 ;~ in the source block. Here is a "Hello World" in C++ +using ~:namespaces~: + +#+begin_example + ,#+BEGIN_SRC C++ :results output :namespaces std :includes + 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 2bdda68d58..ccd150eac9 100644 --- a/lisp/ob-C.el +++ b/lisp/ob-C.el @@ -46,6 +46,20 @@ (defvar org-babel-default-header-args:C '()) +;; org lint header arguments for C and 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 +210,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)) - nil)) + (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 +241,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 -- 2.11.0