ob-ledger.el 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ;;; ob-ledger.el --- Babel Functions for Ledger -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2019 Free Software Foundation, Inc.
  3. ;; Author: Eric S Fraga
  4. ;; Keywords: literate programming, reproducible research, accounting
  5. ;; Homepage: https://orgmode.org
  6. ;; This file is part of GNU Emacs.
  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. ;; Org-Babel support for evaluating ledger entries.
  19. ;;
  20. ;; This differs from most standard languages in that
  21. ;;
  22. ;; 1) there is no such thing as a "session" in ledger
  23. ;;
  24. ;; 2) we are generally only going to return output from the ledger program
  25. ;;
  26. ;; 3) we are adding the "cmdline" header argument
  27. ;;
  28. ;; 4) there are no variables
  29. ;;; Code:
  30. (require 'ob)
  31. (defvar org-babel-default-header-args:ledger
  32. '((:results . "output") (:cmdline . "bal"))
  33. "Default arguments to use when evaluating a ledger source block.")
  34. (defun org-babel-execute:ledger (body params)
  35. "Execute a block of Ledger entries with org-babel. This function is
  36. called by `org-babel-execute-src-block'."
  37. (message "executing Ledger source code block")
  38. (let ((cmdline (cdr (assq :cmdline params)))
  39. (in-file (org-babel-temp-file "ledger-"))
  40. (out-file (org-babel-temp-file "ledger-output-")))
  41. (with-temp-file in-file (insert body))
  42. (message "%s" (concat "ledger"
  43. " -f " (org-babel-process-file-name in-file)
  44. " " cmdline))
  45. (with-output-to-string
  46. (shell-command (concat "ledger"
  47. " -f " (org-babel-process-file-name in-file)
  48. " " cmdline
  49. " > " (org-babel-process-file-name out-file))))
  50. (with-temp-buffer (insert-file-contents out-file) (buffer-string))))
  51. (defun org-babel-prep-session:ledger (_session _params)
  52. (error "Ledger does not support sessions"))
  53. (provide 'ob-ledger)
  54. ;;; ob-ledger.el ends here