ob-vala.el 4.0 KB

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