ob-vala.el 4.0 KB

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