ob-tcl.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ;;; ob-tcl.el --- org-babel functions for tcl evaluation
  2. ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  3. ;; Authors: Dan Davison
  4. ;; Eric Schulte
  5. ;; Luis Anaya (tcl)
  6. ;;
  7. ;; Keywords: literate programming, reproducible research
  8. ;; Homepage: http://orgmode.org
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; Org-Babel support for evaluating tcl source code.
  22. ;;; Code:
  23. (require 'ob)
  24. (require 'ob-eval)
  25. (eval-when-compile (require 'cl))
  26. (defvar org-babel-tangle-lang-exts)
  27. (add-to-list 'org-babel-tangle-lang-exts '("tcl" . "tcl"))
  28. (defvar org-babel-default-header-args:tcl nil)
  29. (defvar org-babel-tcl-command "tclsh"
  30. "Name of command to use for executing tcl code.")
  31. (defun org-babel-execute:tcl (body params)
  32. "Execute a block of Tcl code with Babel.
  33. This function is called by `org-babel-execute-src-block'."
  34. (let* ((session (cdr (assoc :session params)))
  35. (result-params (cdr (assoc :result-params params)))
  36. (result-type (cdr (assoc :result-type params)))
  37. (full-body (org-babel-expand-body:generic
  38. body params (org-babel-variable-assignments:tcl params)))
  39. (session (org-babel-tcl-initiate-session session)))
  40. (org-babel-reassemble-table
  41. (org-babel-tcl-evaluate session full-body result-type)
  42. (org-babel-pick-name
  43. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  44. (org-babel-pick-name
  45. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  46. (defun org-babel-prep-session:tcl (session params)
  47. "Prepare SESSION according to the header arguments in PARAMS."
  48. (error "Sessions are not supported for Tcl."))
  49. (defun org-babel-variable-assignments:tcl (params)
  50. "Return list of tcl statements assigning the block's variables."
  51. (mapcar
  52. (lambda (pair)
  53. (format "set %s %s"
  54. (car pair)
  55. (org-babel-tcl-var-to-tcl (cdr pair))))
  56. (mapcar #'cdr (org-babel-get-header params :var))))
  57. ;; helper functions
  58. (defun org-babel-tcl-var-to-tcl (var)
  59. "Convert an elisp value to a tcl variable.
  60. The elisp value, VAR, is converted to a string of tcl source code
  61. specifying a var of the same value."
  62. (if (listp var)
  63. (concat "{" (mapconcat #'org-babel-tcl-var-to-tcl var " ") "}")
  64. (format "%s" var)))
  65. (defvar org-babel-tcl-buffers '(:default . nil))
  66. (defun org-babel-tcl-initiate-session (&optional session params)
  67. "Return nil because sessions are not supported by tcl."
  68. nil)
  69. (defvar org-babel-tcl-wrapper-method
  70. "
  71. proc main {} {
  72. %s
  73. }
  74. set r [eval main]
  75. set o [open \"%s\" \"w\"];
  76. puts $o $r
  77. flush $o
  78. close $o
  79. ")
  80. (defvar org-babel-tcl-pp-wrapper-method
  81. nil)
  82. (defun org-babel-tcl-evaluate (session body &optional result-type)
  83. "Pass BODY to the Tcl process in SESSION.
  84. If RESULT-TYPE equals 'output then return a list of the outputs
  85. of the statements in BODY, if RESULT-TYPE equals 'value then
  86. return the value of the last statement in BODY, as elisp."
  87. (when session (error "Sessions are not supported for Tcl."))
  88. (case result-type
  89. (output (org-babel-eval org-babel-tcl-command body))
  90. (value (let ((tmp-file (org-babel-temp-file "tcl-")))
  91. (org-babel-eval
  92. org-babel-tcl-command
  93. (format org-babel-tcl-wrapper-method body
  94. (org-babel-process-file-name tmp-file 'noquote)))
  95. (org-babel-eval-read-file tmp-file)))))
  96. (provide 'ob-tcl)
  97. ;;; ob-tcl.el ends here