ob-ledger.el 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ;;; ob-ledger.el --- Babel Functions for Ledger -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2021 Free Software Foundation, Inc.
  3. ;; Author: Eric S Fraga
  4. ;; Maintainer: Eric S Fraga
  5. ;; Keywords: literate programming, reproducible research, accounting
  6. ;; Homepage: https://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating ledger entries.
  20. ;;
  21. ;; This differs from most standard languages in that
  22. ;;
  23. ;; 1) there is no such thing as a "session" in ledger
  24. ;;
  25. ;; 2) we are generally only going to return output from the ledger program
  26. ;;
  27. ;; 3) we are adding the "cmdline" header argument
  28. ;;
  29. ;; 4) there are no variables
  30. ;;; Code:
  31. (require 'ob)
  32. (defvar org-babel-default-header-args:ledger
  33. '((:results . "output") (:cmdline . "bal"))
  34. "Default arguments to use when evaluating a ledger source block.")
  35. (defun org-babel-execute:ledger (body params)
  36. "Execute a block of Ledger entries with org-babel. This function is
  37. called by `org-babel-execute-src-block'."
  38. (message "executing Ledger source code block")
  39. (let ((cmdline (cdr (assq :cmdline params)))
  40. (in-file (org-babel-temp-file "ledger-"))
  41. (out-file (org-babel-temp-file "ledger-output-")))
  42. (with-temp-file in-file (insert body))
  43. (message "%s" (concat "ledger"
  44. " -f " (org-babel-process-file-name in-file)
  45. " " cmdline))
  46. (with-output-to-string
  47. (shell-command (concat "ledger"
  48. " -f " (org-babel-process-file-name in-file)
  49. " " cmdline
  50. " > " (org-babel-process-file-name out-file))))
  51. (with-temp-buffer (insert-file-contents out-file) (buffer-string))))
  52. (defun org-babel-prep-session:ledger (_session _params)
  53. (error "Ledger does not support sessions"))
  54. (provide 'ob-ledger)
  55. ;;; ob-ledger.el ends here