Hello!

Sorry for the late reply, I was pretty busy last week. An updated
patch is attached!

> I suggest to add the following to "ob-C.el" so that `org-lint' can issue
> a warning whenever :namespaces is used in a C block.

Done. I needed to tweak the code a bit though to get it to work and to
prevent flagging valid C headers as errors with org lint.

> Using `org-entry-get' is no longer supported. You can replace the `or'
> with
>
>   (cdr (assq :namespaces params))

Done, I also replaced the other uses of 'org-entry-get' around the one
I modified

> Nitpick: I would put the "\n" on another line.

I agree, and done :)

> Could you also provide an ORG-NEWS entry for the feature?

Done. I'm not sure if it went in the right place and if it's formatted
correctly though, so can you give that a look over to make sure it
looks good?

Thanks again for taking the time to review this for me :D

Let me know if you spot anything fishy or wrong.

Also, RE: Copyright, this is the form I need to send, correct?
http://orgmode.org/request-assign-future.txt

-Jay


On Sun, Jul 23, 2017 at 7:44 AM, Nicolas Goaziou <m...@nicolasgoaziou.fr> wrote:
> Hello,
>
> Jay Kamat <jaygka...@gmail.com> writes:
>
>> However, it would be nice to add a "using namespace std" to this
>> source code block, so it can become:
>>
>> #+BEGIN_SRC C++ :includes <iostream> :namespaces std
>>   cout << "Hello world\n";
>> #+END_SRC
>>
>>
>> Which makes it cleaner and easier to read, especially for very short
>> code snippets, using a bunch of std tools.
>
> Good idea.
>
>> One concern that I have is that "using namespace *;" is only available
>> in C++ and not C, but there isn't an easy way I could find to limit
>> it's usage to only C++ blocks without a bunch of restructuring, so
>> this will fail if you attempt to set a namespace on a plain C block.
>
> I suggest to add the following to "ob-C.el" so that `org-lint' can issue
> a warning whenever :namespaces is used in a C block.
>
>   (defconst org-babel-header-args:C '((includes . :any))
>     "C-specific header arguments.")
>
>   (defconst org-babel-header-args:C++
>     `(,(append '((namespaces . :any))
>              org-babel-header-args:C))
>     "C++-specific header arguments.")
>
>> Also, this contribution puts me very close to the 15 line limit before
>> I need to get FSF papers signed. I intend to sign papers soon, but I'm
>> a little busy right now, and I'll get around to submitting the request
>> later on.
>
> Thank you. FYI, in many cases, the whole process is very quick and not
> time-consuming.
>
>> 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.
>
> Some comments follow.
>
>> +     (namespaces (org-babel-read
>> +               (or (cdr (assq :namespaces params))
>> +                   (org-entry-get nil "namespaces" t))
>> +                nil)))
>
> Using `org-entry-get' is no longer supported. You can replace the `or'
> with
>
>   (cdr (assq :namespaces params))
>
>> +             ;; namespaces
>> +             (mapconcat
>> +              (lambda (inc) (format "using namespace %s;" inc))
>> +              namespaces "\n")
>
> Nitpick: I would put the "\n" on another line.
>
> Could you also provide an ORG-NEWS entry for the feature?
>
> Regards,
>
> --
> Nicolas Goaziou
From bf08fb4f89024428a95615bdfede86e3c883d87c Mon Sep 17 00:00:00 2001
From: Jay Kamat <jaygka...@gmail.com>
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 <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 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

Reply via email to