Merged from mwolson@gnu.org--2006 (patch 34)
[planner-el.git] / planner-ledger.el
blobeb3552c9e5a6dd341c9cc3147aadc746034cc535
1 ;;; planner-ledger.el --- ledger support for planner
3 ;; Copyright (C) 2004 Will Glozer (will AT glozer DOT net)
4 ;; Parts copyright (C) 2004, 2005 Free Software Foundation, Inc.
6 ;; Author: Will Glozer (will AT glozer DOT net)
8 ;; This file is part of Planner. It is not part of GNU Emacs.
10 ;; Planner is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; Planner is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with Planner; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; This planner module provides integration between planner and
28 ;; John Wiegley's ledger accounting program available at:
30 ;; http://newartisans.com/johnw/ledger.tar.gz
32 ;; planner-ledger can insert a ledger balance overview and a list of pending
33 ;; transactions into a planner day page. To do so, simply add a hook:
35 ;; (add-hook 'planner-goto-hook 'planner-ledger-insert-maybe)
37 ;; and make sure `planner-day-page-template' includes sections that match
38 ;; `planner-ledger-balance-regexp' and `planner-ledger-pending-regexp'.
40 ;; planner-ledger can also create a new ledger entry based on a planner
41 ;; task that matches `planner-ledger-payment-task-regexp', which by default
42 ;; matches entries like:
44 ;; #B0 _ payment due: Payee, $100.00 from 2004.07.01
46 ;; Bind `planner-ledger-add-entry-from-task' to a convenient key stroke and
47 ;; execute it when in a payment task.
49 ;;; Contributors
51 ;; Travis B. Hartwell made this usable with new versions of ledger and
52 ;; made it more flexible.
54 (require 'ledger)
55 (require 'planner)
57 ;;; Code:
58 (defcustom planner-ledger-balance-regexp
59 "^* Ledger\n\n$"
60 "Section marker for insertion of ledger balance."
61 :type 'regexp
62 :group 'planner-ledger)
64 (defcustom planner-ledger-pending-regexp
65 "^** Pending Transactions\n\n$"
66 "Section marker for insertion of pending ledger transactions."
67 :type 'regexp
68 :group 'planner-ledger)
70 (defcustom planner-ledger-balance-accounts
71 '("Assets" "Liabilities" "-Equity")
72 "Accounts to include or exclude from ledger balance overview."
73 :type '(repeat string)
74 :group 'planner-ledger)
76 (defcustom planner-ledger-balance-args
77 '("-s" "-e" "\"next month\"" "balance")
78 "Command line arguments for ledger balance."
79 :type '(repeat string)
80 :group 'planner-ledger)
82 (defcustom planner-ledger-payment-task-regexp
83 (concat planner-task-regexp
84 "payment\\s-+due:\\s-+\\([^,]+?\\),\\s-*\\([[:graph:]]+\\)")
85 "Regular expression matching planner tasks for ledger payment.
86 The first parenthesized group should match the payee. The second
87 group should match the amount.
89 Example task:
90 #A0 _ payment due: foobar, $1000.00 some comment here"
91 :type 'regexp
92 :group 'planner-ledger)
94 ;;;###autoload
95 (defun planner-ledger-insert-maybe ()
96 "Maybe insert ledger sections into a planner page."
97 (interactive)
98 (apply 'planner-ledger-insert-section-maybe
99 planner-ledger-balance-regexp
100 (append planner-ledger-balance-args
101 planner-ledger-balance-accounts))
102 (planner-ledger-insert-section-maybe planner-ledger-pending-regexp
103 "-U" "register" "."))
105 (defun planner-ledger-insert-section-maybe (regexp &rest ledger-args)
106 "Maybe insert a ledger section into a planner page.
107 Argument REGEXP is the section heading to find. Optional argument
108 LEDGER-ARGS contains the arguments to pass to
109 `ledger-run-ledger'."
110 (save-excursion
111 (when (re-search-forward regexp nil t)
112 (apply 'ledger-run-ledger ledger-args))))
114 (defun planner-ledger-add-entry-from-task ()
115 "Add a new ledger entry from the task at point."
116 (interactive)
117 (save-excursion
118 (beginning-of-line)
119 (if (re-search-forward planner-ledger-payment-task-regexp
120 (planner-line-end-position)
122 (let* ((payee (match-string 1))
123 (amount (match-string 2))
124 (date (planner-filename-to-calendar-date (buffer-name)))
125 (buffer (find-buffer-visiting ledger-data-file)))
126 (unless buffer (setq buffer (find-file ledger-data-file)))
127 (pop-to-buffer buffer)
128 (ledger-add-entry (format "%d/%d/%d %s %s"
129 (extract-calendar-year date)
130 (extract-calendar-month date)
131 (extract-calendar-day date)
132 payee
133 amount)))
134 (message "Not in a ledger payment task"))))
136 (provide 'planner-ledger)
138 ;;; planner-ledger.el ends here