summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2004-04-04 23:59:20 +0000
committerJohn Wiegley <johnw@newartisans.com>2004-04-04 23:59:20 +0000
commit42a1c0396865a7e606b9d0b35fee7aaa3ebf7166 (patch)
tree6811657dc08c2eb2b098b282c957283794005355
parentf672024e06b7b03c2a9287bf75faf375955fa791 (diff)
downloadfork-ledger-42a1c0396865a7e606b9d0b35fee7aaa3ebf7166.tar.gz
fork-ledger-42a1c0396865a7e606b9d0b35fee7aaa3ebf7166.tar.bz2
fork-ledger-42a1c0396865a7e606b9d0b35fee7aaa3ebf7166.zip
*** empty log message ***
-rw-r--r--amount.cc10
-rw-r--r--ledger.el70
-rw-r--r--ledger.h9
-rw-r--r--reports.cc14
4 files changed, 69 insertions, 34 deletions
diff --git a/amount.cc b/amount.cc
index 22c3816d..d6724640 100644
--- a/amount.cc
+++ b/amount.cc
@@ -229,19 +229,23 @@ amount * gmp_amount::street(bool get_quotes) const
int max = 10;
while (--max >= 0) {
- if (! amt->commdty()->price) {
+ if (! amt->commdty()->price && ! amt->commdty()->sought) {
if (get_quotes)
get_commodity_price(amt->commdty());
+ amt->commdty()->sought = true;
if (! amt->commdty()->price)
break;
}
amount * old = amt;
amt = amt->value(amt->commdty()->price);
- delete old;
- if (amt->commdty() == old->commdty())
+ if (amt->commdty() == old->commdty()) {
+ delete old;
break;
+ }
+
+ delete old;
}
return amt;
diff --git a/ledger.el b/ledger.el
index 46633c95..bfa577f8 100644
--- a/ledger.el
+++ b/ledger.el
@@ -37,6 +37,15 @@
(defvar ledger-version "1.1"
"The version of ledger.el currently loaded")
+(defgroup ledger nil
+ "Interface to the Ledger command-line accounting program."
+ :group 'data)
+
+(defcustom ledger-binary-path (executable-find "ledger")
+ "Path to the ledger executable."
+ :type 'file
+ :group 'ledger)
+
(defun ledger-iterate-entries (callback)
(goto-char (point-min))
(let* ((now (current-time))
@@ -87,7 +96,7 @@
(insert
(with-temp-buffer
(setq exit-code
- (apply 'call-process "/home/johnw/bin/ledger" nil t nil
+ (apply 'call-process ledger-binary-path nil t nil
(cons "entry" args)))
(if (= 0 exit-code)
(buffer-substring (+ (point-min) 5) (point-max))
@@ -123,15 +132,13 @@
(define-key map [(control ?c) (control ?a)] 'ledger-add-entry)
(define-key map [(control ?c) (control ?c)] 'ledger-toggle-current)))
-(defun ledger-parse-entries (account &optional all-p)
- ;; `then' is 45 days ago
- (let ((then (time-subtract (current-time)
- (seconds-to-time (* 45 24 60 60))))
- total entries)
+(defun ledger-parse-entries (account &optional all-p after-date)
+ (let (total entries)
(ledger-iterate-entries
(function
(lambda (start date mark desc)
- (when (or all-p (not mark) (time-less-p then date))
+ (when (and (or all-p (not mark))
+ (time-less-p after-date date))
(forward-line)
(setq total 0.0)
(while (looking-at
@@ -148,53 +155,76 @@
mark date desc (or amt total))
entries))))
(forward-line))))))
- (nreverse entries)))
+ entries))
-(define-derived-mode ledger-reconcile-mode text-mode "Reconcile"
+(defvar ledger-reconcile-text "Reconcile")
+
+(define-derived-mode ledger-reconcile-mode text-mode 'ledger-reconcile-text
"A mode for reconciling ledger entries."
(let ((map (make-sparse-keymap)))
(define-key map [? ] 'ledger-reconcile-toggle)
(use-local-map map)))
+(add-to-list 'minor-mode-alist
+ '(ledger-reconcile-mode ledger-reconcile-text))
+
(defvar ledger-buf nil)
-(make-variable-buffer-local 'ledger-buf)
+(defvar ledger-acct nil)
(defun ledger-reconcile-toggle ()
(interactive)
(let ((where (get-text-property (point) 'where))
+ (account ledger-acct)
cleared)
(with-current-buffer ledger-buf
(goto-char where)
- (setq cleared (ledger-toggle-current)))
+ (setq cleared (ledger-toggle-current))
+ (save-buffer))
(if cleared
(add-text-properties (line-beginning-position)
(line-end-position)
(list 'face 'bold))
(remove-text-properties (line-beginning-position)
(line-end-position)
- (list 'face)))))
+ (list 'face)))
+ (with-temp-buffer
+ (let ((exit-code
+ (apply 'call-process ledger-binary-path nil t nil
+ (list "-C" "balance" account))))
+ (if (/= 0 exit-code)
+ (setq ledger-reconcile-text "Reconcile [ERR]")
+ (goto-char (point-min))
+ (delete-horizontal-space)
+ (skip-syntax-forward "^ ")
+ (setq ledger-reconcile-text
+ (concat "Reconcile ["
+ (buffer-substring-no-properties (point-min) (point))
+ "]")))))))
(defun ledger-reconcile (account)
(interactive "sAccount to reconcile: ")
- (let ((items (save-excursion
- (goto-char (point-min))
- (ledger-parse-entries account)))
- (buf (current-buffer)))
+ (let* ((then (time-subtract (current-time)
+ (seconds-to-time (* 90 24 60 60))))
+ (items (save-excursion
+ (goto-char (point-min))
+ (ledger-parse-entries account t then)))
+ (buf (current-buffer)))
(pop-to-buffer (generate-new-buffer "*Reconcile*"))
(ledger-reconcile-mode)
- (setq ledger-buf buf)
+ (set (make-local-variable 'ledger-buf) buf)
+ (set (make-local-variable 'ledger-acct) account)
(dolist (item items)
(let ((beg (point)))
(insert (format "%s %-30s %8.2f\n"
- (format-time-string "%Y.%m.%d" (nth 2 item))
+ (format-time-string "%Y/%m/%d" (nth 2 item))
(nth 3 item) (nth 4 item)))
(if (nth 1 item)
(set-text-properties beg (1- (point))
(list 'face 'bold
'where (nth 0 item)))
(set-text-properties beg (1- (point))
- (list 'where (nth 0 item))))))
- (goto-char (point-min))))
+ (list 'where (nth 0 item)))))
+ (goto-char (point-min)))))
(provide 'ledger)
diff --git a/ledger.h b/ledger.h
index de30843e..aa7ec507 100644
--- a/ledger.h
+++ b/ledger.h
@@ -1,5 +1,5 @@
#ifndef _LEDGER_H
-#define _LEDGER_H "$Revision: 1.28 $"
+#define _LEDGER_H "$Revision: 1.29 $"
//////////////////////////////////////////////////////////////////////
//
@@ -40,6 +40,7 @@ class commodity
std::string symbol;
mutable amount * price; // the current price
+ mutable bool sought;
bool prefix;
bool separate;
@@ -48,8 +49,8 @@ class commodity
int precision;
- explicit commodity() : price(NULL), prefix(false), separate(true),
- thousands(false), european(false) {}
+ explicit commodity() : price(NULL), sought(false),
+ prefix(false), separate(true), thousands(false), european(false) {}
explicit commodity(const std::string& sym, bool pre = false,
bool sep = true, bool thou = true,
@@ -298,7 +299,7 @@ extern book * main_ledger;
inline commodity::commodity(const std::string& sym, bool pre, bool sep,
bool thou, bool euro, int prec)
- : symbol(sym), price(NULL), prefix(pre), separate(sep),
+ : symbol(sym), price(NULL), sought(false), prefix(pre), separate(sep),
thousands(thou), european(euro), precision(prec) {
#ifdef DEBUG
std::pair<commodities_map_iterator, bool> result =
diff --git a/reports.cc b/reports.cc
index d9efac2b..9827367e 100644
--- a/reports.cc
+++ b/reports.cc
@@ -1,13 +1,13 @@
#include "ledger.h"
-#define LEDGER_VERSION "1.2"
+#define LEDGER_VERSION "1.3"
#include <fstream>
#include <unistd.h>
namespace ledger {
-static bool show_cleared = false;
+static bool cleared_only = false;
static bool show_virtual = true;
static bool get_quotes = false;
static bool show_children = false;
@@ -105,7 +105,7 @@ void report_balances(std::ostream& out, regexps_map& regexps)
for (entries_list_iterator i = main_ledger->entries.begin();
i != main_ledger->entries.end();
i++) {
- if ((show_cleared && ! (*i)->cleared) || ! matches_date_range(*i))
+ if ((cleared_only && ! (*i)->cleared) || ! matches_date_range(*i))
continue;
for (std::list<transaction *>::iterator x = (*i)->xacts.begin();
@@ -203,7 +203,7 @@ void print_register(const std::string& acct_name, std::ostream& out,
i != main_ledger->entries.end();
i++) {
if ((! have_beginning && ! have_ending && ! have_date_mask &&
- ! show_cleared && (*i)->cleared) ||
+ ! (cleared_only ? (*i)->cleared : ! (*i)->cleared)) ||
! matches_date_range(*i) || ! (*i)->matches(regexps))
continue;
@@ -374,7 +374,7 @@ void add_new_entry(int index, int argc, char **argv)
std::exit(1);
}
- added.cleared = show_cleared;
+ added.cleared = cleared_only;
if (index == argc) {
std::cerr << "Error: Too few arguments to 'entry'." << std::endl;
@@ -591,7 +591,7 @@ int main(int argc, char * argv[])
case 'h': show_help(std::cout); break;
case 'f': file = new std::ifstream(optarg); break;
- case 'C': show_cleared = true; break;
+ case 'C': cleared_only = true; break;
case 'R': show_virtual = false; break;
case 's': show_children = true; break;
case 'S': show_sorted = true; break;
@@ -718,7 +718,7 @@ int main(int argc, char * argv[])
else if (command == "print") {
if (show_sorted)
main_ledger->sort(cmp_entry_date());
- main_ledger->print(std::cout, regexps, true);
+ main_ledger->print(std::cout, regexps, ! full_names);
}
else if (command == "equity") {
equity_ledger(std::cout, regexps);