* Babel support for the D language
@ 2013-11-28 17:06 Thierry Banel
2013-11-28 17:51 ` Thomas S. Dye
2013-12-01 14:13 ` Eric Schulte
0 siblings, 2 replies; 17+ messages in thread
From: Thierry Banel @ 2013-11-28 17:06 UTC (permalink / raw)
To: emacs-orgmode
emacs-orgmode@gnu.org
* Hi all Org fans.
I added support in Org-Babel for executing Digital Mars D code.
This is derived from the code in ob-C.el, by Eric Schulte
(thanks Eric).
I release it under the same GPL license as Emacs and Org-mode.
Comments and enhancements are welcome !
* Example. The following source block:
#+begin_src D
import std.stdio;
void main()
{
foreach (i; 0..9)
writefln ("i= %4d", i);
}
#+end_src
will result in:
#+results:
: i= 0
: i= 1
: i= 2
: i= 3
: i= 4
: i= 5
: i= 6
: i= 7
: i= 8
* About Digital Mars D Language (http://dlang.org/)
D is (imo) quite an interesting language:
- C++ syntax
- Built-in garbage collector
- Strong type system
- Meta-programming
- Seamless assembler support
- Usable as a scripting language
- C binary compatibility
- and much more.
* Installation:
- Install D
be sure to have the rdmd compiler in the path
- add ob-D.el in the lisp/org/ source tree
- in the org.el file, find a line which says:
(const :tag "C" C)
add a similar line for D:
(const :tag "D" D)
- byte-compile the 2 modified *.el
- customize the variable org-babel-load-languages enabling D
- either through M-x customize-variable
(you may need a restart of Emacs prior to customization)
- or in your .emacs
-----8<----------(ob-D.el file to put in lisp/org/)----------->8--------
;;; ob-D.el --- org-babel functions for the D language
;; Copyright (C) 2013 Thierry Banel
;; Author: Thierry Banel, derived from the Eric Schulte work
;; Keywords: literate programming, reproducible research
;; This file is NOT (yet) part of GNU Emacs.
;; ob-D.el is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; ob-D.el is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; the GNU General Public License can be obtained here:
;; <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Org-Babel support for evaluating Digital Mars D Language code.
;; The D language home page is here:
;; http://dlang.org/
;;; Code:
(require 'ob)
(require 'ob-eval)
;;(require 'd-mode)
(declare-function org-entry-get "org"
(pom property &optional inherit literal-nil))
(defvar org-babel-tangle-lang-exts)
(add-to-list 'org-babel-tangle-lang-exts '("D" . "D"))
(defvar org-babel-default-header-args:D '())
(defvar org-babel-D-compiler "rdmd"
"Command used to compile and run a D source code file into an
executable.")
(defun org-babel-execute:D (body params)
"Execute a block of D code with org-babel. This function is
called by `org-babel-execute-src-block'."
(org-babel-D-execute body params))
(defun org-babel-D-execute (body params)
"This function should only be called by `org-babel-execute:D'"
(let* ((tmp-src-file (org-babel-temp-file "Dsrc" ".d"))
(cmdline (cdr (assoc :cmdline params)))
(flags (cdr (assoc :flags params)))
(rdmd (format "%s %s %s"
org-babel-D-compiler
(mapconcat 'identity
(if (listp flags) flags (list flags)) " ")
;; On Unix, keep directory separator
(org-babel-process-file-name tmp-src-file)))
;; On Windows, directory separator must be fixed
;;(replace-regexp-in-string
;; "/" "\\\\"
;; (org-babel-process-file-name tmp-src-file))))
(full-body (org-babel-D-expand body params)))
(with-temp-file tmp-src-file (insert full-body))
;; add path to D binaries if not already there
;;(let ((bin "c:/DLang/dmd2/windows/bin;")
;; (path (getenv "PATH")))
;; (unless (string-match bin path)
;; (setenv "PATH" (concat bin path))))
(org-babel-eval rdmd "")))
(defun org-babel-D-expand (body params)
"Expand a block of D code with org-babel according to
its header arguments."
(let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
(colname-names (cdr (car (org-babel-get-header params :colname-names))))
(main-p (not (string= (cdr (assoc :main params)) "no")))
(imports (mapcar #'cdr (org-babel-get-header params :import))))
(mapconcat 'identity
(list
"module aaa;\n"
;; imports
(mapconcat
(lambda (inc) (format "import %s;" inc))
imports "\n")
;; variables
(mapconcat 'org-babel-D-var-to-D vars "\n")
(mapconcat 'org-babel-D-colnames-to-D colname-names "\n")
;; body
(if main-p
(org-babel-D-ensure-main-wrap body)
body) "\n") "\n")))
(defun org-babel-D-ensure-main-wrap (body)
"Wrap body in a \"main\" function call if none exists."
(if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
body
(format "int main() {\n%s\nreturn(0);\n}\n" body)))
(defun org-babel-prep-session:D (session params)
"This function does nothing as D is a compiled language with no
support for sessions"
(error "D is a compiled languages -- no support for sessions"))
(defun org-babel-load-session:D (session body params)
"This function does nothing as D is a compiled language with no
support for sessions"
(error "D is a compiled languages -- no support for sessions"))
;; helper functions
(defun org-babel-D-var-to-D (pair)
"Convert an elisp value into a string of D code specifying a variable
of the same value."
(let ((var (car pair))
(val (cdr pair)))
(when (symbolp val)
(setq val (symbol-name val))
(when (= (length val) 1)
(setq val (string-to-char val))))
(cond
((integerp val)
(format "int %S = %S;" var val))
((floatp val)
(format "double %S = %S;" var val))
((stringp val)
(format "string %S = \"%s\";" var val))
((listp val)
(if (assoc var colname-names) ()
(setq colname-names
(cons (cons
var
(let ((i 0)) (mapcar (lambda (x) (setq i (1+ i)) (format
"$%s" i))
(car val))))
colname-names)))
(format "string[][] %S = [\n[%s]];" var
(mapconcat (lambda (row)
(if (listp row)
(mapconcat (lambda (v) (format "\"%s\"" v))
row
",")))
val
"],\n[")))
(t
(format "u32 %S = %S;" var val)))))
(defun org-babel-D-colnames-to-D (pair)
"Convert an elisp list of header table into a D vector
specifying a variable with the name of the table"
(let ((table (car pair))
(headers (cdr pair)))
(format "string[] %S_headers = [%s];"
table
(mapconcat (lambda (h) (format "%S" h)) headers ","))))
(provide 'ob-D)
;;; ob-D.el ends here
-----8<----------(end of ob-D.el)----------->8--------
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Babel support for the D language
2013-11-28 17:06 Babel support for the D language Thierry Banel
@ 2013-11-28 17:51 ` Thomas S. Dye
2013-11-28 19:02 ` Thierry Banel
2013-11-30 7:42 ` Thierry Banel
2013-12-01 14:13 ` Eric Schulte
1 sibling, 2 replies; 17+ messages in thread
From: Thomas S. Dye @ 2013-11-28 17:51 UTC (permalink / raw)
To: Thierry Banel; +Cc: emacs-orgmode
Aloha Thierry,
Thanks for this contribution.
Have you looked at
http://orgmode.org/worg/org-contrib/babel/languages.html#develop?
It describes how language support is added to the Org mode core or to
contrib/ and the choices an author has in the process.
Also, it contains a link for a documentation template. It would be great
if your Example section below could be expanded into ob-doc-d.org.
If you do decide to write ob-doc-d.org, I'm happy to proof-read, edit,
etc.
All the best,
Tom
Thierry Banel <tbanelwebmin@free.fr> writes:
> emacs-orgmode@gnu.org
>
> * Hi all Org fans.
>
> I added support in Org-Babel for executing Digital Mars D code.
> This is derived from the code in ob-C.el, by Eric Schulte
> (thanks Eric).
> I release it under the same GPL license as Emacs and Org-mode.
>
> Comments and enhancements are welcome !
>
> * Example. The following source block:
> #+begin_src D
> import std.stdio;
> void main()
> {
> foreach (i; 0..9)
> writefln ("i= %4d", i);
> }
> #+end_src
>
> will result in:
> #+results:
> : i= 0
> : i= 1
> : i= 2
> : i= 3
> : i= 4
> : i= 5
> : i= 6
> : i= 7
> : i= 8
>
> * About Digital Mars D Language (http://dlang.org/)
> D is (imo) quite an interesting language:
> - C++ syntax
> - Built-in garbage collector
> - Strong type system
> - Meta-programming
> - Seamless assembler support
> - Usable as a scripting language
> - C binary compatibility
> - and much more.
>
> * Installation:
> - Install D
> be sure to have the rdmd compiler in the path
> - add ob-D.el in the lisp/org/ source tree
> - in the org.el file, find a line which says:
> (const :tag "C" C)
> add a similar line for D:
> (const :tag "D" D)
> - byte-compile the 2 modified *.el
> - customize the variable org-babel-load-languages enabling D
> - either through M-x customize-variable
> (you may need a restart of Emacs prior to customization)
> - or in your .emacs
>
> -----8<----------(ob-D.el file to put in lisp/org/)----------->8--------
> ;;; ob-D.el --- org-babel functions for the D language
>
> ;; Copyright (C) 2013 Thierry Banel
>
> ;; Author: Thierry Banel, derived from the Eric Schulte work
> ;; Keywords: literate programming, reproducible research
>
> ;; This file is NOT (yet) part of GNU Emacs.
>
> ;; ob-D.el is free software: you can redistribute it and/or modify
> ;; it under the terms of the GNU General Public License as published by
> ;; the Free Software Foundation, either version 3 of the License, or
> ;; (at your option) any later version.
>
> ;; ob-D.el is distributed in the hope that it will be useful,
> ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
> ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> ;; GNU General Public License for more details.
>
> ;; the GNU General Public License can be obtained here:
> ;; <http://www.gnu.org/licenses/>.
>
> ;;; Commentary:
> ;; Org-Babel support for evaluating Digital Mars D Language code.
> ;; The D language home page is here:
> ;; http://dlang.org/
>
> ;;; Code:
> (require 'ob)
> (require 'ob-eval)
> ;;(require 'd-mode)
>
> (declare-function org-entry-get "org"
> (pom property &optional inherit literal-nil))
>
> (defvar org-babel-tangle-lang-exts)
> (add-to-list 'org-babel-tangle-lang-exts '("D" . "D"))
>
> (defvar org-babel-default-header-args:D '())
>
> (defvar org-babel-D-compiler "rdmd"
> "Command used to compile and run a D source code file into an
> executable.")
>
> (defun org-babel-execute:D (body params)
> "Execute a block of D code with org-babel. This function is
> called by `org-babel-execute-src-block'."
> (org-babel-D-execute body params))
>
> (defun org-babel-D-execute (body params)
> "This function should only be called by `org-babel-execute:D'"
> (let* ((tmp-src-file (org-babel-temp-file "Dsrc" ".d"))
> (cmdline (cdr (assoc :cmdline params)))
> (flags (cdr (assoc :flags params)))
> (rdmd (format "%s %s %s"
> org-babel-D-compiler
> (mapconcat 'identity
> (if (listp flags) flags (list flags)) " ")
>
> ;; On Unix, keep directory separator
> (org-babel-process-file-name tmp-src-file)))
>
> ;; On Windows, directory separator must be fixed
> ;;(replace-regexp-in-string
> ;; "/" "\\\\"
> ;; (org-babel-process-file-name tmp-src-file))))
>
> (full-body (org-babel-D-expand body params)))
> (with-temp-file tmp-src-file (insert full-body))
>
> ;; add path to D binaries if not already there
> ;;(let ((bin "c:/DLang/dmd2/windows/bin;")
> ;; (path (getenv "PATH")))
> ;; (unless (string-match bin path)
> ;; (setenv "PATH" (concat bin path))))
>
> (org-babel-eval rdmd "")))
>
> (defun org-babel-D-expand (body params)
> "Expand a block of D code with org-babel according to
> its header arguments."
> (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
> (colname-names (cdr (car (org-babel-get-header params :colname-names))))
> (main-p (not (string= (cdr (assoc :main params)) "no")))
> (imports (mapcar #'cdr (org-babel-get-header params :import))))
> (mapconcat 'identity
> (list
> "module aaa;\n"
> ;; imports
> (mapconcat
> (lambda (inc) (format "import %s;" inc))
> imports "\n")
> ;; variables
> (mapconcat 'org-babel-D-var-to-D vars "\n")
> (mapconcat 'org-babel-D-colnames-to-D colname-names "\n")
> ;; body
> (if main-p
> (org-babel-D-ensure-main-wrap body)
> body) "\n") "\n")))
>
> (defun org-babel-D-ensure-main-wrap (body)
> "Wrap body in a \"main\" function call if none exists."
> (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
> body
> (format "int main() {\n%s\nreturn(0);\n}\n" body)))
>
> (defun org-babel-prep-session:D (session params)
> "This function does nothing as D is a compiled language with no
> support for sessions"
> (error "D is a compiled languages -- no support for sessions"))
>
> (defun org-babel-load-session:D (session body params)
> "This function does nothing as D is a compiled language with no
> support for sessions"
> (error "D is a compiled languages -- no support for sessions"))
>
> ;; helper functions
>
> (defun org-babel-D-var-to-D (pair)
> "Convert an elisp value into a string of D code specifying a variable
> of the same value."
> (let ((var (car pair))
> (val (cdr pair)))
> (when (symbolp val)
> (setq val (symbol-name val))
> (when (= (length val) 1)
> (setq val (string-to-char val))))
> (cond
> ((integerp val)
> (format "int %S = %S;" var val))
> ((floatp val)
> (format "double %S = %S;" var val))
> ((stringp val)
> (format "string %S = \"%s\";" var val))
> ((listp val)
> (if (assoc var colname-names) ()
> (setq colname-names
> (cons (cons
> var
> (let ((i 0)) (mapcar (lambda (x) (setq i (1+ i)) (format
> "$%s" i))
> (car val))))
> colname-names)))
> (format "string[][] %S = [\n[%s]];" var
> (mapconcat (lambda (row)
> (if (listp row)
> (mapconcat (lambda (v) (format "\"%s\"" v))
> row
> ",")))
> val
> "],\n[")))
> (t
> (format "u32 %S = %S;" var val)))))
>
> (defun org-babel-D-colnames-to-D (pair)
> "Convert an elisp list of header table into a D vector
> specifying a variable with the name of the table"
> (let ((table (car pair))
> (headers (cdr pair)))
> (format "string[] %S_headers = [%s];"
> table
> (mapconcat (lambda (h) (format "%S" h)) headers ","))))
>
> (provide 'ob-D)
>
> ;;; ob-D.el ends here
>
> -----8<----------(end of ob-D.el)----------->8--------
>
>
>
>
--
Thomas S. Dye
http://www.tsdye.com
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Babel support for the D language
2013-11-28 17:51 ` Thomas S. Dye
@ 2013-11-28 19:02 ` Thierry Banel
2013-11-28 20:57 ` Thomas S. Dye
2013-11-28 22:32 ` Nick Dokos
2013-11-30 7:42 ` Thierry Banel
1 sibling, 2 replies; 17+ messages in thread
From: Thierry Banel @ 2013-11-28 19:02 UTC (permalink / raw)
To: emacs-orgmode
Thomas S. Dye <tsd <at> tsdye.com> writes:
>
> Aloha Thierry,
>
> Thanks for this contribution.
>
> Have you looked at
> http://orgmode.org/worg/org-contrib/babel/languages.html#develop?
>
> It describes how language support is added to the Org mode core or to
> contrib/ and the choices an author has in the process.
>
> Also, it contains a link for a documentation template. It would be great
> if your Example section below could be expanded into ob-doc-d.org.
>
> If you do decide to write ob-doc-d.org, I'm happy to proof-read, edit,
> etc.
>
> All the best,
> Tom
>
Aloha Thomas !
This is exactly the place I was looking for.
I am new to Worg and I missed it.
By the way, are you aware of a location to store a *.el in Worg ?
I happily accept your help for proof-reading the yet-to-be-written
documentation.
Regards
Thierry
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Babel support for the D language
2013-11-28 19:02 ` Thierry Banel
@ 2013-11-28 20:57 ` Thomas S. Dye
2013-11-28 22:32 ` Nick Dokos
1 sibling, 0 replies; 17+ messages in thread
From: Thomas S. Dye @ 2013-11-28 20:57 UTC (permalink / raw)
To: Thierry Banel; +Cc: emacs-orgmode
Thierry Banel <tbanelwebmin@free.fr> writes:
> By the way, are you aware of a location to store a *.el in Worg ?
poto:worg dk$ find . -name "*.el" -print
./.dir-locals.el
./.dir-settings.el
./code/elisp/davidam.el
./code/elisp/dto-org-gtd.el
./code/elisp/org-collector.el
./code/elisp/org-effectiveness.el
./code/elisp/org-exchange-capture.el
./code/elisp/org-issue.el
./code/elisp/org-license.el
./code/elisp/org-player.el
./code/elisp/worg-fortune.el
./code/elisp/worg.el
./color-themes/color-theme-folio.el
./color-themes/color-theme-manoj.el
./color-themes/color-theme-railscast.el
./color-themes/color-theme-tangotango.el
./color-themes/color-theme-zenash.el
./color-themes/color-theme-zenburn.el
./org-contrib/babel/ob-template.el
./org-tests/ert-publish-test.el
./org-tests/tools/el-expectations.el
./org-tests/tools/el-mock.el
./org-tests/tools/ert.el
./worgtest-init.el
> I happily accept your help for proof-reading the yet-to-be-written
> documentation.
Great. Let me know when you've pushed a draft to Worg.
All the best,
Tom
--
Thomas S. Dye
http://www.tsdye.com
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Babel support for the D language
2013-11-28 19:02 ` Thierry Banel
2013-11-28 20:57 ` Thomas S. Dye
@ 2013-11-28 22:32 ` Nick Dokos
2013-11-29 7:40 ` Thierry Banel
1 sibling, 1 reply; 17+ messages in thread
From: Nick Dokos @ 2013-11-28 22:32 UTC (permalink / raw)
To: emacs-orgmode
Thierry Banel <tbanelwebmin@free.fr> writes:
> By the way, are you aware of a location to store a *.el in Worg ?
>
The .el file should probably end up in contrib/lisp, rather than on
Worg.
--
Nick
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Babel support for the D language
2013-11-28 22:32 ` Nick Dokos
@ 2013-11-29 7:40 ` Thierry Banel
2013-11-29 15:06 ` Nick Dokos
0 siblings, 1 reply; 17+ messages in thread
From: Thierry Banel @ 2013-11-29 7:40 UTC (permalink / raw)
To: emacs-orgmode
Nick Dokos <ndokos <at> gmail.com> writes:
>
> Thierry Banel <tbanelwebmin <at> free.fr> writes:
>
> > By the way, are you aware of a location to store a *.el in Worg ?
> >
>
> The .el file should probably end up in contrib/lisp, rather than on
> Worg.
>
Thanks Nick !
Probably there is some kind of agreement process to put a package in
contrib/lisp.
I have to find out.
If someone knows ?
Thierry
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Babel support for the D language
2013-11-28 17:51 ` Thomas S. Dye
2013-11-28 19:02 ` Thierry Banel
@ 2013-11-30 7:42 ` Thierry Banel
2013-11-30 17:56 ` Thomas S. Dye
1 sibling, 1 reply; 17+ messages in thread
From: Thierry Banel @ 2013-11-30 7:42 UTC (permalink / raw)
To: emacs-orgmode
Thomas S. Dye <tsd <at> tsdye.com> writes:
>
> Aloha Thierry,
>
> Thanks for this contribution.
>
> Have you looked at
> http://orgmode.org/worg/org-contrib/babel/languages.html#develop?
>
> It describes how language support is added to the Org mode core or to
> contrib/ and the choices an author has in the process.
>
> Also, it contains a link for a documentation template. It would be great
> if your Example section below could be expanded into ob-doc-d.org.
>
> If you do decide to write ob-doc-d.org, I'm happy to proof-read, edit,
> etc.
>
> All the best,
> Tom
>
Hi Thomas
Done !
The first draft of
http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-D.html
has been committed.
Have a nice day
Thierry
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Babel support for the D language
2013-11-28 17:06 Babel support for the D language Thierry Banel
2013-11-28 17:51 ` Thomas S. Dye
@ 2013-12-01 14:13 ` Eric Schulte
2013-12-01 16:08 ` Thierry Banel
1 sibling, 1 reply; 17+ messages in thread
From: Eric Schulte @ 2013-12-01 14:13 UTC (permalink / raw)
To: Thierry Banel; +Cc: emacs-orgmode
Hi Thierry,
I've added ob-D.el to the contrib/lisp directory of Org-mode. Would it
make sense to add D as a c-variant in ob-C.el?
Thanks for the contribution, and for the documentation!
Thierry Banel <tbanelwebmin@free.fr> writes:
> emacs-orgmode@gnu.org
>
> * Hi all Org fans.
>
> I added support in Org-Babel for executing Digital Mars D code.
> This is derived from the code in ob-C.el, by Eric Schulte
> (thanks Eric).
> I release it under the same GPL license as Emacs and Org-mode.
>
> Comments and enhancements are welcome !
>
> * Example. The following source block:
> #+begin_src D
> import std.stdio;
> void main()
> {
> foreach (i; 0..9)
> writefln ("i= %4d", i);
> }
> #+end_src
>
> will result in:
> #+results:
> : i= 0
> : i= 1
> : i= 2
> : i= 3
> : i= 4
> : i= 5
> : i= 6
> : i= 7
> : i= 8
>
> * About Digital Mars D Language (http://dlang.org/)
> D is (imo) quite an interesting language:
> - C++ syntax
> - Built-in garbage collector
> - Strong type system
> - Meta-programming
> - Seamless assembler support
> - Usable as a scripting language
> - C binary compatibility
> - and much more.
>
> * Installation:
> - Install D
> be sure to have the rdmd compiler in the path
> - add ob-D.el in the lisp/org/ source tree
> - in the org.el file, find a line which says:
> (const :tag "C" C)
> add a similar line for D:
> (const :tag "D" D)
> - byte-compile the 2 modified *.el
> - customize the variable org-babel-load-languages enabling D
> - either through M-x customize-variable
> (you may need a restart of Emacs prior to customization)
> - or in your .emacs
>
> -----8<----------(ob-D.el file to put in lisp/org/)----------->8--------
> ;;; ob-D.el --- org-babel functions for the D language
>
> ;; Copyright (C) 2013 Thierry Banel
>
> ;; Author: Thierry Banel, derived from the Eric Schulte work
> ;; Keywords: literate programming, reproducible research
>
> ;; This file is NOT (yet) part of GNU Emacs.
>
> ;; ob-D.el is free software: you can redistribute it and/or modify
> ;; it under the terms of the GNU General Public License as published by
> ;; the Free Software Foundation, either version 3 of the License, or
> ;; (at your option) any later version.
>
> ;; ob-D.el is distributed in the hope that it will be useful,
> ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
> ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> ;; GNU General Public License for more details.
>
> ;; the GNU General Public License can be obtained here:
> ;; <http://www.gnu.org/licenses/>.
>
> ;;; Commentary:
> ;; Org-Babel support for evaluating Digital Mars D Language code.
> ;; The D language home page is here:
> ;; http://dlang.org/
>
> ;;; Code:
> (require 'ob)
> (require 'ob-eval)
> ;;(require 'd-mode)
>
> (declare-function org-entry-get "org"
> (pom property &optional inherit literal-nil))
>
> (defvar org-babel-tangle-lang-exts)
> (add-to-list 'org-babel-tangle-lang-exts '("D" . "D"))
>
> (defvar org-babel-default-header-args:D '())
>
> (defvar org-babel-D-compiler "rdmd"
> "Command used to compile and run a D source code file into an
> executable.")
>
> (defun org-babel-execute:D (body params)
> "Execute a block of D code with org-babel. This function is
> called by `org-babel-execute-src-block'."
> (org-babel-D-execute body params))
>
> (defun org-babel-D-execute (body params)
> "This function should only be called by `org-babel-execute:D'"
> (let* ((tmp-src-file (org-babel-temp-file "Dsrc" ".d"))
> (cmdline (cdr (assoc :cmdline params)))
> (flags (cdr (assoc :flags params)))
> (rdmd (format "%s %s %s"
> org-babel-D-compiler
> (mapconcat 'identity
> (if (listp flags) flags (list flags)) " ")
>
> ;; On Unix, keep directory separator
> (org-babel-process-file-name tmp-src-file)))
>
> ;; On Windows, directory separator must be fixed
> ;;(replace-regexp-in-string
> ;; "/" "\\\\"
> ;; (org-babel-process-file-name tmp-src-file))))
>
> (full-body (org-babel-D-expand body params)))
> (with-temp-file tmp-src-file (insert full-body))
>
> ;; add path to D binaries if not already there
> ;;(let ((bin "c:/DLang/dmd2/windows/bin;")
> ;; (path (getenv "PATH")))
> ;; (unless (string-match bin path)
> ;; (setenv "PATH" (concat bin path))))
>
> (org-babel-eval rdmd "")))
>
> (defun org-babel-D-expand (body params)
> "Expand a block of D code with org-babel according to
> its header arguments."
> (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
> (colname-names (cdr (car (org-babel-get-header params :colname-names))))
> (main-p (not (string= (cdr (assoc :main params)) "no")))
> (imports (mapcar #'cdr (org-babel-get-header params :import))))
> (mapconcat 'identity
> (list
> "module aaa;\n"
> ;; imports
> (mapconcat
> (lambda (inc) (format "import %s;" inc))
> imports "\n")
> ;; variables
> (mapconcat 'org-babel-D-var-to-D vars "\n")
> (mapconcat 'org-babel-D-colnames-to-D colname-names "\n")
> ;; body
> (if main-p
> (org-babel-D-ensure-main-wrap body)
> body) "\n") "\n")))
>
> (defun org-babel-D-ensure-main-wrap (body)
> "Wrap body in a \"main\" function call if none exists."
> (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
> body
> (format "int main() {\n%s\nreturn(0);\n}\n" body)))
>
> (defun org-babel-prep-session:D (session params)
> "This function does nothing as D is a compiled language with no
> support for sessions"
> (error "D is a compiled languages -- no support for sessions"))
>
> (defun org-babel-load-session:D (session body params)
> "This function does nothing as D is a compiled language with no
> support for sessions"
> (error "D is a compiled languages -- no support for sessions"))
>
> ;; helper functions
>
> (defun org-babel-D-var-to-D (pair)
> "Convert an elisp value into a string of D code specifying a variable
> of the same value."
> (let ((var (car pair))
> (val (cdr pair)))
> (when (symbolp val)
> (setq val (symbol-name val))
> (when (= (length val) 1)
> (setq val (string-to-char val))))
> (cond
> ((integerp val)
> (format "int %S = %S;" var val))
> ((floatp val)
> (format "double %S = %S;" var val))
> ((stringp val)
> (format "string %S = \"%s\";" var val))
> ((listp val)
> (if (assoc var colname-names) ()
> (setq colname-names
> (cons (cons
> var
> (let ((i 0)) (mapcar (lambda (x) (setq i (1+ i)) (format
> "$%s" i))
> (car val))))
> colname-names)))
> (format "string[][] %S = [\n[%s]];" var
> (mapconcat (lambda (row)
> (if (listp row)
> (mapconcat (lambda (v) (format "\"%s\"" v))
> row
> ",")))
> val
> "],\n[")))
> (t
> (format "u32 %S = %S;" var val)))))
>
> (defun org-babel-D-colnames-to-D (pair)
> "Convert an elisp list of header table into a D vector
> specifying a variable with the name of the table"
> (let ((table (car pair))
> (headers (cdr pair)))
> (format "string[] %S_headers = [%s];"
> table
> (mapconcat (lambda (h) (format "%S" h)) headers ","))))
>
> (provide 'ob-D)
>
> ;;; ob-D.el ends here
>
> -----8<----------(end of ob-D.el)----------->8--------
>
>
>
--
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Babel support for the D language
2013-12-01 14:13 ` Eric Schulte
@ 2013-12-01 16:08 ` Thierry Banel
2013-12-01 18:04 ` Eric Schulte
0 siblings, 1 reply; 17+ messages in thread
From: Thierry Banel @ 2013-12-01 16:08 UTC (permalink / raw)
To: emacs-orgmode
Eric Schulte <schulte.eric <at> gmail.com> writes:
>
> Hi Thierry,
>
> I've added ob-D.el to the contrib/lisp directory of Org-mode. Would it
> make sense to add D as a c-variant in ob-C.el?
>
> Thanks for the contribution, and for the documentation!
>
> Thierry Banel <tbanelwebmin <at> free.fr> writes:
>
Hi Eric
That's awsome !
Yes, it would make sense, as D and C++ share a lot.
There are differences, though:
tables are translated as:
string[][]; in D
char*[]; in C++
includes are translated as:
import std.stdio; in D
#include <stdio> in C++
But yes, probably sharing a single ob-C.el file would save some maintenance
effort. And by the way, ob-D.el was directly inspired by your ob-C.el.
I'm open and willing to go further.
Thierry
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Babel support for the D language
2013-12-01 16:08 ` Thierry Banel
@ 2013-12-01 18:04 ` Eric Schulte
2013-12-01 18:43 ` Thierry Banel
2013-12-14 18:49 ` Thierry Banel
0 siblings, 2 replies; 17+ messages in thread
From: Eric Schulte @ 2013-12-01 18:04 UTC (permalink / raw)
To: Thierry Banel; +Cc: emacs-orgmode
Thierry Banel <tbanelwebmin@free.fr> writes:
> Eric Schulte <schulte.eric <at> gmail.com> writes:
>
>>
>> Hi Thierry,
>>
>> I've added ob-D.el to the contrib/lisp directory of Org-mode. Would it
>> make sense to add D as a c-variant in ob-C.el?
>>
>> Thanks for the contribution, and for the documentation!
>>
>> Thierry Banel <tbanelwebmin <at> free.fr> writes:
>>
>
> Hi Eric
>
> That's awsome !
>
> Yes, it would make sense, as D and C++ share a lot.
> There are differences, though:
> tables are translated as:
> string[][]; in D
> char*[]; in C++
> includes are translated as:
> import std.stdio; in D
> #include <stdio> in C++
>
> But yes, probably sharing a single ob-C.el file would save some maintenance
> effort. And by the way, ob-D.el was directly inspired by your ob-C.el.
>
> I'm open and willing to go further.
> Thierry
>
Great,
I think this incorporation into ob-C.el would be the next logical step.
You'd want to use the `org-babel-c-variant' in the same manner as C++
does currently. But there's no rush, and any changes there couldn't be
merged until after your FSF copyright assignment forms have been
completed.
Best Regards,
>
>
>
>
--
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Babel support for the D language
2013-12-01 18:04 ` Eric Schulte
@ 2013-12-01 18:43 ` Thierry Banel
2013-12-14 18:49 ` Thierry Banel
1 sibling, 0 replies; 17+ messages in thread
From: Thierry Banel @ 2013-12-01 18:43 UTC (permalink / raw)
To: emacs-orgmode
Eric Schulte <schulte.eric <at> gmail.com> writes:
> > Hi Eric
> >
> > That's awsome !
> >
> > Yes, it would make sense, as D and C++ share a lot.
> > There are differences, though:
> > tables are translated as:
> > string[][]; in D
> > char*[]; in C++
> > includes are translated as:
> > import std.stdio; in D
> > #include <stdio> in C++
> >
> > But yes, probably sharing a single ob-C.el file would save some maintenance
> > effort. And by the way, ob-D.el was directly inspired by your ob-C.el.
> >
> > I'm open and willing to go further.
> > Thierry
> >
>
> Great,
>
> I think this incorporation into ob-C.el would be the next logical step.
> You'd want to use the `org-babel-c-variant' in the same manner as C++
> does currently. But there's no rush, and any changes there couldn't be
> merged until after your FSF copyright assignment forms have been
> completed.
>
> Best Regards,
>
> >
> >
> >
> >
>
Differences between D and C++ will introduce more
(cond ((equal org-babel-c-variant ...))
constructs throughout the file.
But that is the very purpose of org-babel-c-variant, isn'it ?
One of the differences is that C or C++ requires two
org-babel-eval -- one for the compiler, another for the executable --
whereas D requires only one.
I will look at that while the copyright stuff is being done.
Thanks, Eric (and Bastien and Thomas and Nick), for your warm welcome in the
Org world.
Thierry
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Babel support for the D language
2013-12-01 18:04 ` Eric Schulte
2013-12-01 18:43 ` Thierry Banel
@ 2013-12-14 18:49 ` Thierry Banel
2013-12-15 21:35 ` Eric Schulte
1 sibling, 1 reply; 17+ messages in thread
From: Thierry Banel @ 2013-12-14 18:49 UTC (permalink / raw)
To: emacs-orgmode
Eric Schulte <schulte.eric <at> gmail.com> writes:
>
> I think this incorporation into ob-C.el would be the next logical step.
> You'd want to use the `org-babel-c-variant' in the same manner as C++
> does currently. But there's no rush, and any changes there couldn't be
> merged until after your FSF copyright assignment forms have been
> completed.
>
> Best Regards,
>
Hi Eric
Done. The merging of ob-C.el and ob-D.el is done.
It is at the end of this file:
http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-D.html
Actually, you are right, it makes sense to share common processing in a
single *.el.
I fixed what seems to be a bug.
Tables were translated in C & C++ as something like:
char MyTable[7][2][11] = { ... };
I changed that to:
const char* MyTable[7][2] = { ... };
Because the former assumed that all content in the Org table were strings
11-1 characters long.
Other than that, the behavior of C & C++ remains the same,
and the code is very close to the original ob-C.el
Also, in the future we may lift the constraint that tables have to be
homogeneous
(all ints, or all doubles, or all strings).
But that's another story.
12 days have passed since I paper-mailed my copyright assignment to the FSF.
No news.
In the meantime, if you want to use my work, do so:
it is copyrighted with the GPL 3 or later.
Regards,
Thierry
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Babel support for the D language
2013-12-14 18:49 ` Thierry Banel
@ 2013-12-15 21:35 ` Eric Schulte
0 siblings, 0 replies; 17+ messages in thread
From: Eric Schulte @ 2013-12-15 21:35 UTC (permalink / raw)
To: Thierry Banel; +Cc: emacs-orgmode
Thierry Banel <tbanelwebmin@free.fr> writes:
> Eric Schulte <schulte.eric <at> gmail.com> writes:
>
>>
>> I think this incorporation into ob-C.el would be the next logical step.
>> You'd want to use the `org-babel-c-variant' in the same manner as C++
>> does currently. But there's no rush, and any changes there couldn't be
>> merged until after your FSF copyright assignment forms have been
>> completed.
>>
>> Best Regards,
>>
>
> Hi Eric
>
> Done. The merging of ob-C.el and ob-D.el is done.
> It is at the end of this file:
> http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-D.html
> Actually, you are right, it makes sense to share common processing in a
> single *.el.
>
> I fixed what seems to be a bug.
> Tables were translated in C & C++ as something like:
> char MyTable[7][2][11] = { ... };
> I changed that to:
> const char* MyTable[7][2] = { ... };
> Because the former assumed that all content in the Org table were strings
> 11-1 characters long.
> Other than that, the behavior of C & C++ remains the same,
> and the code is very close to the original ob-C.el
>
> Also, in the future we may lift the constraint that tables have to be
> homogeneous
> (all ints, or all doubles, or all strings).
> But that's another story.
>
> 12 days have passed since I paper-mailed my copyright assignment to
> the FSF. No news. In the meantime, if you want to use my work, do
> so: it is copyrighted with the GPL 3 or later.
>
Yes, the FSF is occasionally very slow.
Perhaps someone with FSF connections can speed things up. When you do
get the assignment back please let me know and send a patch of your
changes against the Org-mode repository (either to me directly or on
list, formatted with "git format-patch") and I'll apply it.
Thanks!
>
> Regards,
> Thierry
>
>
>
--
Eric Schulte
https://cs.unm.edu/~eschulte
PGP: 0x614CA05D
^ permalink raw reply [flat|nested] 17+ messages in thread
end of thread, other threads:[~2013-12-15 21:40 UTC | newest]
Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-11-28 17:06 Babel support for the D language Thierry Banel
2013-11-28 17:51 ` Thomas S. Dye
2013-11-28 19:02 ` Thierry Banel
2013-11-28 20:57 ` Thomas S. Dye
2013-11-28 22:32 ` Nick Dokos
2013-11-29 7:40 ` Thierry Banel
2013-11-29 15:06 ` Nick Dokos
2013-11-29 20:19 ` Thierry Banel
2013-11-30 7:42 ` Thierry Banel
2013-11-30 17:56 ` Thomas S. Dye
2013-12-01 14:08 ` Thierry Banel
2013-12-01 14:13 ` Eric Schulte
2013-12-01 16:08 ` Thierry Banel
2013-12-01 18:04 ` Eric Schulte
2013-12-01 18:43 ` Thierry Banel
2013-12-14 18:49 ` Thierry Banel
2013-12-15 21:35 ` Eric Schulte
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).