Improve Scheme code evaluation
[org-mode/org-tableheadings.git] / lisp / ob-vala.el
blob1282e162bec2d01582450d06e60dbdab661e4f6b
1 ;;; ob-vala.el --- Babel functions for Vala evaluation -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2017 Free Software Foundation, Inc.
5 ;; Author: Christian Garbs <mitch@cgarbs.de>
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;;; License:
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; ob-vala.el provides Babel support for the Vala language
27 ;; (see http://live.gnome.org/Vala for details)
29 ;;; Requirements:
31 ;; - Vala compiler binary (valac)
32 ;; - Vala development environment (Vala libraries etc.)
34 ;; vala-mode.el is nice to have for code formatting, but is not needed
35 ;; for ob-vala.el
37 ;;; Code:
39 (require 'ob)
41 (declare-function org-trim "org" (s &optional keep-lead))
43 ;; File extension.
44 (add-to-list 'org-babel-tangle-lang-exts '("vala" . "vala"))
46 ;; Header arguments empty by default.
47 (defvar org-babel-default-header-args:vala '())
49 (defcustom org-babel-vala-compiler "valac"
50 "Command used to compile a C source code file into an executable.
51 May be either a command in the path, like \"valac\"
52 or an absolute path name, like \"/usr/local/bin/valac\".
53 Parameters may be used like this: \"valac -v\""
54 :group 'org-babel
55 :version "26.1"
56 :package-version '(Org . "9.1")
57 :type 'string)
59 ;; This is the main function which is called to evaluate a code
60 ;; block.
62 ;; - run Vala compiler and create a binary in a temporary file
63 ;; - compiler/linker flags can be set via :flags header argument
64 ;; - if compilation succeeded, run the binary
65 ;; - commandline parameters to the binary can be set via :cmdline
66 ;; header argument
67 ;; - stdout will be parsed as RESULT (control via :result-params
68 ;; header argument)
70 ;; There is no session support because Vala is a compiled language.
72 ;; This function is heavily based on ob-C.el
73 (defun org-babel-execute:vala (body params)
74 "Execute a block of Vala code with Babel.
75 This function is called by `org-babel-execute-src-block'."
76 (message "executing Vala source code block")
77 (let* ((tmp-src-file (org-babel-temp-file
78 "vala-src-"
79 ".vala"))
80 (tmp-bin-file (org-babel-temp-file "vala-bin-" org-babel-exeext))
81 (cmdline (cdr (assq :cmdline params)))
82 (flags (cdr (assq :flags params))))
83 (with-temp-file tmp-src-file (insert body))
84 (org-babel-eval
85 (format "%s %s -o %s %s"
86 org-babel-vala-compiler
87 (mapconcat #'identity
88 (if (listp flags) flags (list flags)) " ")
89 (org-babel-process-file-name tmp-bin-file)
90 (org-babel-process-file-name tmp-src-file)) "")
91 (when (file-executable-p tmp-bin-file)
92 (let ((results
93 (org-trim
94 (org-babel-eval
95 (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) ""))))
96 (org-babel-reassemble-table
97 (org-babel-result-cond (cdr (assq :result-params params))
98 (org-babel-read results)
99 (let ((tmp-file (org-babel-temp-file "vala-")))
100 (with-temp-file tmp-file (insert results))
101 (org-babel-import-elisp-from-file tmp-file)))
102 (org-babel-pick-name
103 (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
104 (org-babel-pick-name
105 (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))))
107 (defun org-babel-prep-session:vala (_session _params)
108 "Prepare a session.
109 This function does nothing as Vala is a compiled language with no
110 support for sessions."
111 (error "Vala is a compiled language -- no support for sessions"))
113 (provide 'ob-vala)
115 ;;; ob-vala.el ends here