ob-hledger.el 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ;; ob-hledger.el --- Babel Functions for hledger -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2021 Free Software Foundation, Inc.
  3. ;; Author: Simon Michael
  4. ;; Keywords: literate programming, reproducible research, plain text 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. ;; Babel support for evaluating hledger entries.
  19. ;;
  20. ;; Based on ob-ledger.el.
  21. ;; If the source block is empty, hledger will use a default journal file,
  22. ;; probably ~/.hledger.journal (it may not notice your $LEDGER_FILE env var).
  23. ;; So make ~/.hledger.journal a symbolic link to the real file if necessary.
  24. ;; TODO Unit tests are more than welcome, too.
  25. ;;; Code:
  26. (require 'ob)
  27. (defvar org-babel-default-header-args:hledger
  28. '((:results . "output") (:exports . "results") (:cmdline . "bal"))
  29. "Default arguments to use when evaluating a hledger source block.")
  30. (defun org-babel-execute:hledger (body params)
  31. "Execute a block of hledger entries with org-babel.
  32. This function is called by `org-babel-execute-src-block'."
  33. (message "executing hledger source code block")
  34. (letrec ( ;(result-params (split-string (or (cdr (assq :results params)) "")))
  35. (cmdline (cdr (assq :cmdline params)))
  36. (in-file (org-babel-temp-file "hledger-"))
  37. (out-file (org-babel-temp-file "hledger-output-"))
  38. (hledgercmd (concat "hledger"
  39. (if (> (length body) 0)
  40. (concat " -f " (org-babel-process-file-name in-file))
  41. "")
  42. " " cmdline)))
  43. (with-temp-file in-file (insert body))
  44. ;; TODO This is calling for some refactoring:
  45. ;; (concat "hledger" (if ...) " " cmdline)
  46. ;; could be built only once and bound to a symbol.
  47. (message "%s" hledgercmd)
  48. (with-output-to-string
  49. (shell-command (concat hledgercmd " > " (org-babel-process-file-name out-file))))
  50. (with-temp-buffer (insert-file-contents out-file) (buffer-string))))
  51. (defun org-babel-prep-session:hledger (_session _params)
  52. (error "hledger does not support sessions"))
  53. (provide 'ob-hledger)
  54. ;;; ob-hledger.el ends here