ob-hledger.el 2.6 KB

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