emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Christian Garbs <mitch@cgarbs.de>
To: emacs-orgmode@gnu.org
Subject: [PATCH v3] ob-vala.el: Add Vala support to Babel
Date: Tue, 1 Aug 2017 00:04:34 +0200	[thread overview]
Message-ID: <E1deRf7-0000Sr-Qd@yggdrasil.mitch.h.shuttle.de> (raw)
In-Reply-To: <87mv7jq78i.fsf@nicolasgoaziou.fr>

* lisp/ob-vala.el: Add support for the Vala language to Babel.

* testing/lisp/test-ob-vala.el: Add tests for ob-vala.el.

* doc/org.texi (Working with source code): Add Vala to the list of
  supported languages.
---
 doc/org.texi                 |   1 +
 etc/ORG-NEWS                 |  13 +++++
 lisp/ob-vala.el              | 115 +++++++++++++++++++++++++++++++++++++++++++
 testing/lisp/test-ob-vala.el | 104 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 233 insertions(+)
 create mode 100644 lisp/ob-vala.el
 create mode 100644 testing/lisp/test-ob-vala.el

diff --git a/doc/org.texi b/doc/org.texi
index db28bfee..3493ebb5 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -15535,6 +15535,7 @@ Org supports the following languages for the @samp{src} code blocks:
 @item Scheme @tab scheme @tab GNU Screen @tab screen
 @item Sed @tab sed @tab shell @tab sh
 @item SQL @tab sql @tab SQLite @tab sqlite
+@item Vala @tab vala
 @end multitable
 
 Additional documentation for some languages are at
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 5fb763c6..6192dbed 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -233,6 +233,19 @@ using ~:namespaces~:
   ,#+END_SRC
 #+end_example
 
+**** Support for Vala language
+
+[[https://wiki.gnome.org/Projects/Vala][Vala]] language blocks support two special header arguments:
+
+- ~:flags~ passes arguments to the compiler 
+- ~:cmdline~ passes commandline arguments to the generated executable
+
+Support for [[http://orgmode.org/manual/var.html#var][~:var~]] does not exist yet, also there is no [[http://orgmode.org/manual/session.html#session][~:session~]]
+support because Vala is a compiled language.
+
+The Vala compiler binary can be changed via the ~defcustom~
+~org-babel-vala-compiler~.
+
 *** New ~function~ scope argument for the Clock Table
 Added a nullary function that returns a list of files as a possible
 argument for the scope of the clock table.
diff --git a/lisp/ob-vala.el b/lisp/ob-vala.el
new file mode 100644
index 00000000..1282e162
--- /dev/null
+++ b/lisp/ob-vala.el
@@ -0,0 +1,115 @@
+;;; ob-vala.el --- Babel functions for Vala evaluation -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2017 Free Software Foundation, Inc.
+
+;; Author: Christian Garbs <mitch@cgarbs.de>
+;; Keywords: literate programming, reproducible research
+;; Homepage: http://orgmode.org
+
+;;; License:
+
+;; GNU Emacs 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.
+
+;; GNU Emacs 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.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; ob-vala.el provides Babel support for the Vala language
+;; (see http://live.gnome.org/Vala for details)
+
+;;; Requirements:
+
+;; - Vala compiler binary (valac)
+;; - Vala development environment (Vala libraries etc.)
+;;
+;; vala-mode.el is nice to have for code formatting, but is not needed
+;; for ob-vala.el
+
+;;; Code:
+
+(require 'ob)
+
+(declare-function org-trim "org" (s &optional keep-lead))
+
+;; File extension.
+(add-to-list 'org-babel-tangle-lang-exts '("vala" . "vala"))
+
+;; Header arguments empty by default.
+(defvar org-babel-default-header-args:vala '())
+
+(defcustom org-babel-vala-compiler "valac"
+  "Command used to compile a C source code file into an executable.
+May be either a command in the path, like \"valac\"
+or an absolute path name, like \"/usr/local/bin/valac\".
+Parameters may be used like this: \"valac -v\""
+  :group 'org-babel
+  :version "26.1"
+  :package-version '(Org . "9.1")
+  :type 'string)
+
+;; This is the main function which is called to evaluate a code
+;; block.
+;;
+;; - run Vala compiler and create a binary in a temporary file
+;;   - compiler/linker flags can be set via :flags header argument
+;; - if compilation succeeded, run the binary
+;;   - commandline parameters to the binary can be set via :cmdline
+;;     header argument
+;;   - stdout will be parsed as RESULT (control via :result-params
+;;     header argument)
+;;
+;; There is no session support because Vala is a compiled language.
+;;
+;; This function is heavily based on ob-C.el
+(defun org-babel-execute:vala (body params)
+  "Execute a block of Vala code with Babel.
+This function is called by `org-babel-execute-src-block'."
+  (message "executing Vala source code block")
+  (let* ((tmp-src-file (org-babel-temp-file
+			"vala-src-"
+			".vala"))
+         (tmp-bin-file (org-babel-temp-file "vala-bin-" org-babel-exeext))
+         (cmdline (cdr (assq :cmdline params)))
+         (flags (cdr (assq :flags params))))
+    (with-temp-file tmp-src-file (insert body))
+    (org-babel-eval
+     (format "%s %s -o %s %s"
+	     org-babel-vala-compiler
+	     (mapconcat #'identity
+			(if (listp flags) flags (list flags)) " ")
+	     (org-babel-process-file-name tmp-bin-file)
+	     (org-babel-process-file-name tmp-src-file)) "")
+    (when (file-executable-p tmp-bin-file)
+	(let ((results
+	       (org-trim
+		(org-babel-eval
+		 (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) ""))))
+	  (org-babel-reassemble-table
+	   (org-babel-result-cond (cdr (assq :result-params params))
+	     (org-babel-read results)
+	     (let ((tmp-file (org-babel-temp-file "vala-")))
+	       (with-temp-file tmp-file (insert results))
+	       (org-babel-import-elisp-from-file tmp-file)))
+	   (org-babel-pick-name
+	    (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
+	   (org-babel-pick-name
+	    (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))))
+
+(defun org-babel-prep-session:vala (_session _params)
+  "Prepare a session.
+This function does nothing as Vala is a compiled language with no
+support for sessions."
+  (error "Vala is a compiled language -- no support for sessions"))
+
+(provide 'ob-vala)
+
+;;; ob-vala.el ends here
diff --git a/testing/lisp/test-ob-vala.el b/testing/lisp/test-ob-vala.el
new file mode 100644
index 00000000..92032222
--- /dev/null
+++ b/testing/lisp/test-ob-vala.el
@@ -0,0 +1,104 @@
+;;; test-ob-vala.el --- tests for ob-vala.el
+
+;; Copyright (C) 2017 Christian Garbs
+;; Authors: Christian Garbs
+
+;; This file is not part of GNU Emacs.
+
+;; This program 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.
+
+;; This program 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.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Code:
+(unless (featurep 'ob-vala)
+  (signal 'missing-test-dependency "Support for Vala code blocks"))
+
+(org-test-for-executable org-babel-vala-compiler)
+
+(ert-deftest ob-vala/assert ()
+  (should t))
+
+(ert-deftest ob-vala/static-output ()
+  "Parse static output to variable."
+  (should (= 42
+	     (org-test-with-temp-text
+	      "
+#+begin_src vala
+  class Demo.HelloWorld : GLib.Object {
+      public static int main(string[] args) {
+          stdout.printf(\"42\n\");
+          return 0;
+      }
+  }
+#+end_src"
+	      (org-babel-next-src-block)
+	      (org-babel-execute-src-block)))))
+
+(ert-deftest ob-vala/return-numerics ()
+  "Return multiple numeric values."
+  (should (equal '((0) (1) (2))
+		 (org-test-with-temp-text
+		  "
+#+begin_src vala
+  class Demo.HelloWorld : GLib.Object {
+      public static int main(string[] args) {
+          for (int i=0; i<3; i++) {
+              stdout.printf(\"%d\n\", i);
+          }
+          return 0;
+      }
+  }
+#+end_src"
+		  (org-babel-next-src-block)
+		  (org-babel-execute-src-block)))))
+
+(ert-deftest ob-vala/compiler-args ()
+  "Pass compiler arguments."
+  (should (string= "Foo"
+		   (org-test-with-temp-text
+		    "
+#+begin_src vala :flags -D FOO
+  class Demo.HelloWorld : GLib.Object {
+      public static int main(string[] args) {
+#if FOO
+          stdout.printf(\"Foo\n\");
+#else
+          stdout.printf(\"No foo\n\");
+#endif
+          return 0;
+      }
+  }
+#+end_src"
+		    (org-babel-next-src-block)
+		    (org-babel-execute-src-block)))))
+
+(ert-deftest ob-vala/comdline-args ()
+  "Pass commandline arguments."
+  (should (equal '(("foo") ("bar"))
+		 (org-test-with-temp-text
+		  "
+#+begin_src vala :cmdline foo bar
+  class Demo.HelloWorld : GLib.Object {
+      public static int main(string[] args) {
+          // skip args[0], it is the binary name
+          for (int i=1; i < args.length; i++) {
+              stdout.printf(\"%s\n\" , args[i]);
+          }
+          return 0;
+      }
+  }
+#+end_src"
+		  (org-babel-next-src-block)
+		  (org-babel-execute-src-block)))))
+
+
+;;; test-ob-vala.el ends here
-- 
2.11.0

  reply	other threads:[~2017-08-06 20:36 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-09 20:09 Babel support for Vala Christian Garbs
2017-07-10  9:03 ` Nicolas Goaziou
2017-07-10 17:54   ` Christian Garbs
2017-07-11  8:10     ` Nicolas Goaziou
2017-07-13 21:42       ` Christian Garbs
2017-07-14  8:30         ` Nicolas Goaziou
2017-07-14 19:43           ` [PATCH] ob-vala.el: Add Vala support to Babel Christian Garbs
2017-07-23  9:05             ` Nicolas Goaziou
2017-07-31 22:04               ` [PATCH v2] " Christian Garbs
2017-08-01 21:59                 ` Nicolas Goaziou
2017-07-31 22:04                   ` Christian Garbs [this message]
2017-08-07  9:41                     ` [PATCH v3] " Nicolas Goaziou
2017-08-01 21:20               ` [PATCH] " Christian Garbs
2017-08-01 22:03                 ` Nicolas Goaziou
2017-08-02 20:24                   ` Christian Garbs
2017-08-03 10:49                     ` Nicolas Goaziou
2017-08-05 19:53                       ` Christian Garbs
2017-08-06  0:22                         ` Nicolas Goaziou
2017-08-06 10:06                           ` Christian Garbs
2017-08-07  9:52                             ` Nicolas Goaziou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1deRf7-0000Sr-Qd@yggdrasil.mitch.h.shuttle.de \
    --to=mitch@cgarbs.de \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).