org-babel-shell.el 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ;;; org-babel-shell.el --- org-babel functions for shell execution
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://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. ;; Org-Babel support for evaluating sh, bash, and zsh shells
  24. ;;; Code:
  25. (require 'org-babel)
  26. (defun org-babel-shell-add-interpreter (var cmds)
  27. (set-default var cmds)
  28. (mapc (lambda (cmd)
  29. (org-babel-add-interpreter cmd)
  30. (eval
  31. `(defun ,(intern (concat "org-babel-execute:" cmd)) (body params)
  32. ,(concat "Evaluate a block of " cmd " shell with org-babel. This function is
  33. called by `org-babel-execute-src-block'. This function is an
  34. automatically generated wrapper for `org-babel-shell-execute'.")
  35. (org-babel-shell-execute ,cmd body params))))
  36. cmds))
  37. (defcustom org-babel-shell-interpreters '("sh" "bash" "zsh")
  38. "List of interpreters of shelling languages which can be
  39. executed through org-babel."
  40. :group 'org-babel
  41. :set 'org-babel-shell-add-interpreter)
  42. (defun org-babel-shell-execute (cmd body params)
  43. "Run CMD on BODY obeying any options set with PARAMS."
  44. (message (format "executing %s code block..." cmd))
  45. (let ((vars (org-babel-ref-variables params)))
  46. (save-window-excursion
  47. (with-temp-buffer
  48. (if (> (length vars) 0)
  49. (error "currently no support for passing variables to shells"))
  50. (insert body)
  51. (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
  52. (org-babel-shell-to-elisp (buffer-string))))))
  53. (defun org-babel-shell-to-elisp (result)
  54. (let ((tmp-file (make-temp-file "org-babel-shell")))
  55. (with-temp-file tmp-file
  56. (insert result))
  57. (with-temp-buffer
  58. (org-table-import tmp-file nil)
  59. (delete-file tmp-file)
  60. (setq result (mapcar (lambda (row)
  61. (mapcar #'org-babel-read row))
  62. (org-table-to-lisp)))
  63. (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
  64. (if (consp (car result))
  65. (if (null (cdr (car result)))
  66. (caar result)
  67. result)
  68. (car result))
  69. result))))
  70. (provide 'org-babel-shell)
  71. ;;; org-babel-shell.el ends here