ob-template.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ;;; ob-template.el --- org-babel functions for template evaluation
  2. ;; Copyright (C) your name here
  3. ;; Author: your name here
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: https://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program 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, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; This file is not intended to ever be loaded by org-babel, rather it
  24. ;; is a template for use in adding new language support to Org-babel.
  25. ;; Good first steps are to copy this file to a file named by the
  26. ;; language you are adding, and then use `query-replace' to replace
  27. ;; all strings of "template" in this file with the name of your new
  28. ;; language.
  29. ;;
  30. ;; If you have questions as to any of the portions of the file defined
  31. ;; below please look to existing language support for guidance.
  32. ;;
  33. ;; If you are planning on adding a language to org-babel we would ask
  34. ;; that if possible you fill out the FSF copyright assignment form
  35. ;; available at https://orgmode.org/request-assign-future.txt as this
  36. ;; will make it possible to include your language support in the core
  37. ;; of Org-mode, otherwise unassigned language support files can still
  38. ;; be included in the contrib/ directory of the Org-mode repository.
  39. ;;; Requirements:
  40. ;; Use this section to list the requirements of this language. Most
  41. ;; languages will require that at least the language be installed on
  42. ;; the user's system, and the Emacs major mode relevant to the
  43. ;; language be installed as well.
  44. ;;; Code:
  45. (require 'ob)
  46. (require 'ob-ref)
  47. (require 'ob-comint)
  48. (require 'ob-eval)
  49. ;; possibly require modes required for your language
  50. ;; optionally define a file extension for this language
  51. (add-to-list 'org-babel-tangle-lang-exts '("template" . "tmp"))
  52. ;; optionally declare default header arguments for this language
  53. (defvar org-babel-default-header-args:template '())
  54. ;; This function expands the body of a source code block by doing
  55. ;; things like prepending argument definitions to the body, it should
  56. ;; be called by the `org-babel-execute:template' function below.
  57. (defun org-babel-expand-body:template (body params &optional processed-params)
  58. "Expand BODY according to PARAMS, return the expanded body."
  59. (require 'inf-template)
  60. (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
  61. (concat
  62. (mapconcat ;; define any variables
  63. (lambda (pair)
  64. (format "%s=%S"
  65. (car pair) (org-babel-template-var-to-template (cdr pair))))
  66. vars "\n") "\n" body "\n")))
  67. ;; This is the main function which is called to evaluate a code
  68. ;; block.
  69. ;;
  70. ;; This function will evaluate the body of the source code and
  71. ;; return the results as emacs-lisp depending on the value of the
  72. ;; :results header argument
  73. ;; - output means that the output to STDOUT will be captured and
  74. ;; returned
  75. ;; - value means that the value of the last statement in the
  76. ;; source code block will be returned
  77. ;;
  78. ;; The most common first step in this function is the expansion of the
  79. ;; PARAMS argument using `org-babel-process-params'.
  80. ;;
  81. ;; Please feel free to not implement options which aren't appropriate
  82. ;; for your language (e.g. not all languages support interactive
  83. ;; "session" evaluation). Also you are free to define any new header
  84. ;; arguments which you feel may be useful -- all header arguments
  85. ;; specified by the user will be available in the PARAMS variable.
  86. (defun org-babel-execute:template (body params)
  87. "Execute a block of Template code with org-babel.
  88. This function is called by `org-babel-execute-src-block'"
  89. (message "executing Template source code block")
  90. (let* ((processed-params (org-babel-process-params params))
  91. ;; set the session if the session variable is non-nil
  92. (session (org-babel-template-initiate-session (first processed-params)))
  93. ;; variables assigned for use in the block
  94. (vars (second processed-params))
  95. (result-params (third processed-params))
  96. ;; either OUTPUT or VALUE which should behave as described above
  97. (result-type (fourth processed-params))
  98. ;; expand the body with `org-babel-expand-body:template'
  99. (full-body (org-babel-expand-body:template
  100. body params processed-params)))
  101. ;; actually execute the source-code block either in a session or
  102. ;; possibly by dropping it to a temporary file and evaluating the
  103. ;; file.
  104. ;;
  105. ;; for session based evaluation the functions defined in
  106. ;; `org-babel-comint' will probably be helpful.
  107. ;;
  108. ;; for external evaluation the functions defined in
  109. ;; `org-babel-eval' will probably be helpful.
  110. ;;
  111. ;; when forming a shell command, or a fragment of code in some
  112. ;; other language, please preprocess any file names involved with
  113. ;; the function `org-babel-process-file-name'. (See the way that
  114. ;; function is used in the language files)
  115. ))
  116. ;; This function should be used to assign any variables in params in
  117. ;; the context of the session environment.
  118. (defun org-babel-prep-session:template (session params)
  119. "Prepare SESSION according to the header arguments specified in PARAMS."
  120. )
  121. (defun org-babel-template-var-to-template (var)
  122. "Convert an elisp var into a string of template source code
  123. specifying a var of the same value."
  124. (format "%S" var))
  125. (defun org-babel-template-table-or-string (results)
  126. "If the results look like a table, then convert them into an
  127. Emacs-lisp table, otherwise return the results as a string."
  128. )
  129. (defun org-babel-template-initiate-session (&optional session)
  130. "If there is not a current inferior-process-buffer in SESSION then create.
  131. Return the initialized session."
  132. (unless (string= session "none")
  133. ))
  134. (provide 'ob-template)
  135. ;;; ob-template.el ends here