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