mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-18 76bbd07de7add0f9d13c6914f158d19630fe2f62
commit | author | age
76bbd0 1 ;;  ob-hledger.el --- Babel Functions for hledger      -*- lexical-binding: t; -*-
C 2
3 ;; Copyright (C) 2010-2018 Free Software Foundation, Inc.
4
5 ;; Author: Simon Michael
6 ;; Keywords: literate programming, reproducible research, plain text accounting
7 ;; Homepage: https://orgmode.org
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Babel support for evaluating hledger entries.
27 ;;
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.
32
33 ;;; Code:
34 (require 'ob)
35
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.")
39
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))
51                  "")
52                    " " cmdline)))
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))))
61
62 (defun org-babel-prep-session:hledger (_session _params)
63   (error "hledger does not support sessions"))
64
65 (provide 'ob-hledger)
66
67
68
69 ;;; ob-hledger.el ends here
70 ;; TODO Unit tests are more than welcome, too.