1 ;; ob-ledger.el --- Babel Functions for hledger -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2017 Free Software Foundation, Inc.
5 ;; Author: Simon Michael
6 ;; Keywords: literate programming, reproducible research, plain text accounting
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Babel support for evaluating hledger entries.
28 ;; Based on ob-ledger.el.
29 ;; If the source block is empty, hledger will use a default journal file,
30 ;; probably ~/.hledger.journal (it may not notice your $LEDGER_FILE env var).
31 ;; So make ~/.hledger.journal a symbolic link to the real file if necessary.
36 (defvar org-babel-default-header-args
:hledger
37 '((:results .
"output") (:exports .
"results") (:cmdline .
"bal"))
38 "Default arguments to use when evaluating a hledger source block.")
40 (defun org-babel-execute:hledger
(body params
)
41 "Execute a block of hledger entries with org-babel.
42 This function is called by `org-babel-execute-src-block'."
43 (message "executing hledger source code block")
44 (letrec ( ;(result-params (split-string (or (cdr (assq :results params)) "")))
45 (cmdline (cdr (assq :cmdline params
)))
46 (in-file (org-babel-temp-file "hledger-"))
47 (out-file (org-babel-temp-file "hledger-output-"))
48 (hledgercmd (concat "hledger"
49 (if (> (length body
) 0)
50 (concat " -f " (org-babel-process-file-name in-file
))
53 (with-temp-file in-file
(insert body
))
54 ;; TODO This is calling for some refactoring:
55 ;; (concat "hledger" (if ...) " " cmdline)
56 ;; could be built only once and bound to a symbol.
57 (message "%s" hledgercmd
)
58 (with-output-to-string
59 (shell-command (concat hledgercmd
" > " (org-babel-process-file-name out-file
))))
60 (with-temp-buffer (insert-file-contents out-file
) (buffer-string))))
62 (defun org-babel-prep-session:hledger
(_session _params
)
63 (error "hledger does not support sessions"))
69 ;;; ob-hledger.el ends here
70 ;; TODO Unit tests are more than welcome, too.