emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Support for 'using namespace *' in ob-C.el
@ 2017-07-17  5:31 Jay Kamat
  2017-07-23 14:44 ` Nicolas Goaziou
  0 siblings, 1 reply; 10+ messages in thread
From: Jay Kamat @ 2017-07-17  5:31 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1951 bytes --]

Hi!

A problem that I came across recently was the difficulty of writing
C++ tutorials for beginners to programming in org mode. In such
tutorials, it would be ideal to keep as much boilerplate away from the
examples so they don't ask questions (until we get to those topics).

The shortest ob-C++ example available is:

#+begin_src C++ :includes <iostream>
  std::cout<<"Hello World!\n";
#+end_src

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.

Attached are patches for adding a :namespaces export option to C++
blocks, and a patch to documentation on worg that documents this
change.

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.
Since it's clear that namespaces aren't part of plain C, I don't think
this is too big of a deal.

Please give this a more thorough review than usual, I'm very new to all of this!

Thanks again for creating/maintaining org mode, it's the greatest!

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.

Thanks,
-Jay

PS:

I'm getting an error when I try to run:
#+BEGIN_SRC C :exports output :includes stdio.h
printf("hello world\n");
#+END_SRC
because 'stdio.h' is not surrounded by quotes. I can do <stdio.h> and
have it work fine, but how can I include quotes in the header line?
They seem to be stripped by org, and backslash ecaping them does not
work.

[-- Attachment #2: 0001-ob-C.el-Add-support-for-specifying-namespaces-in-C-C.patch --]
[-- Type: text/x-patch, Size: 1579 bytes --]

From 1e5fff1741dc853214962b7ea90b0832b4ae3e69 Mon Sep 17 00:00:00 2001
From: Jay Kamat <jaygkamat@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
---
 lisp/ob-C.el | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-C.el b/lisp/ob-C.el
index 2bdda68d5..ba10833d0 100644
--- a/lisp/ob-C.el
+++ b/lisp/ob-C.el
@@ -202,9 +202,15 @@ its header arguments."
 	(defines (org-babel-read
 		  (or (cdr (assq :defines params))
 		      (org-entry-get nil "defines" t))
-		  nil)))
+		   nil))
+	(namespaces (org-babel-read
+		  (or (cdr (assq :namespaces params))
+		      (org-entry-get nil "namespaces" t))
+		   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 +230,10 @@ 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


[-- Attachment #3: 0001-Add-documentation-for-ob-C-namespaces.patch --]
[-- Type: text/x-patch, Size: 1153 bytes --]

From 3267aeac0b90c76f5091104e13389ce6050dd580 Mon Sep 17 00:00:00 2001
From: Jay Kamat <jaygkamat@gmail.com>
Date: Sun, 16 Jul 2017 22:09:06 -0700
Subject: [PATCH] Add documentation for ob-C :namespaces

---
 org-contrib/babel/languages/ob-doc-C.org | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/org-contrib/babel/languages/ob-doc-C.org b/org-contrib/babel/languages/ob-doc-C.org
index b1aac99b..faf2b77f 100644
--- a/org-contrib/babel/languages/ob-doc-C.org
+++ b/org-contrib/babel/languages/ob-doc-C.org
@@ -66,7 +66,7 @@ the results of evaluation inserted into the buffer.
 :   int b=1;
 :   printf("%d\n", a+b);
 : #+end_src
-: 
+:
 : #+results:
 : : 2
 
@@ -154,6 +154,11 @@ It features:
      (C & C++ only) just like =:includes= but for =#defines= lines at the
      top of the code.
 
+- =:namespaces= ::
+     (C++ only)
+     accepts either a single name, or a list of names of namespaces to use.
+     The final format will look like this: =using namespace name;=
+
 - =:libs= ::
      (C & C++ only) useful for linking with a library, may be given
      =-L/path/to/lib= and =-llibrary= instructions.
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2017-08-02  6:49 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-17  5:31 [PATCH] Support for 'using namespace *' in ob-C.el Jay Kamat
2017-07-23 14:44 ` Nicolas Goaziou
2017-07-31  8:03   ` Jay Kamat
2017-07-31 15:34     ` Charles C. Berry
2017-08-01  5:54       ` Jay Kamat
2017-08-01 16:48         ` Nicolas Goaziou
2017-08-01 16:48         ` Nicolas Goaziou
2017-08-02  1:40           ` Jay Kamat
2017-08-02  6:49             ` Nicolas Goaziou
2017-08-01  8:22     ` Nicolas Goaziou

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).