litorgy-shell.el 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. ;;; litorgy-shell.el --- litorgy 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. ;; Litorgy support for evaluating sh, bash, and zsh shells
  24. ;;; Code:
  25. (require 'litorgy)
  26. (defun litorgy-shell-add-interpreter (var cmds)
  27. (set-default var cmds)
  28. (mapc (lambda (cmd)
  29. (setq litorgy-interpreters (cons cmd litorgy-interpreters))
  30. (eval
  31. `(defun ,(intern (concat "litorgy-execute:" cmd)) (body params)
  32. ,(concat "Evaluate a block of " cmd " shell with litorgy. This function is
  33. called by `litorgy-execute-src-block'. This function is an
  34. automatically generated wrapper for `litorgy-shell-execute'.")
  35. (litorgy-shell-execute ,cmd body params))))
  36. cmds))
  37. (defcustom litorgy-shell-interpreters '("sh" "bash" "zsh")
  38. "List of interpreters of shelling languages which can be
  39. executed through litorgy."
  40. :group 'litorgy
  41. :set 'litorgy-shell-add-interpreter)
  42. (defun litorgy-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 (litorgy-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. (buffer-string)))))
  53. (provide 'litorgy-shell)
  54. ;;; litorgy-shell.el ends here