dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

ob-vala.el (4075B)


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