summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-02-11 20:16:45 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-02-11 20:16:45 -0400
commit20bf2547d101617ecfc01fcee346fab64ded3d44 (patch)
treec8c2ea32d10bc0ed7c57fe5e14cf775fbe4cd16c
parent78418ad92b2a38161bc13fb153bc6161e5ef8242 (diff)
downloadfork-ledger-20bf2547d101617ecfc01fcee346fab64ded3d44.tar.gz
fork-ledger-20bf2547d101617ecfc01fcee346fab64ded3d44.tar.bz2
fork-ledger-20bf2547d101617ecfc01fcee346fab64ded3d44.zip
In ledger-mode, if TAB is pressed in an entry, call out to "entry".
-rw-r--r--lisp/ledger.el46
-rw-r--r--src/derive.cc33
2 files changed, 53 insertions, 26 deletions
diff --git a/lisp/ledger.el b/lisp/ledger.el
index d7d6d0eb..43aabffe 100644
--- a/lisp/ledger.el
+++ b/lisp/ledger.el
@@ -201,38 +201,36 @@ Return the difference in the format of a time value."
(if (ledger-time-less-p moment date)
(throw 'found t)))))))
-(defun ledger-add-entry (entry-text)
+(defun ledger-add-entry (entry-text &optional insert-at-point)
(interactive
(list
(read-string "Entry: " (concat ledger-year "/" ledger-month "/"))))
(let* ((args (with-temp-buffer
(insert entry-text)
(eshell-parse-arguments (point-min) (point-max))))
- (date (car args))
- (insert-year t)
(ledger-buf (current-buffer))
exit-code)
- (if (string-match "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)" date)
- (setq date
- (encode-time 0 0 0 (string-to-number (match-string 3 date))
- (string-to-number (match-string 2 date))
- (string-to-number (match-string 1 date)))))
- (ledger-find-slot date)
- (save-excursion
- (if (re-search-backward "^Y " nil t)
- (setq insert-year nil)))
+ (unless insert-at-point
+ (let ((date (car args)))
+ (if (string-match "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)" date)
+ (setq date
+ (encode-time 0 0 0 (string-to-number (match-string 3 date))
+ (string-to-number (match-string 2 date))
+ (string-to-number (match-string 1 date)))))
+ (ledger-find-slot date)))
(save-excursion
(insert
(with-temp-buffer
(setq exit-code
(apply #'ledger-run-ledger ledger-buf "entry"
(mapcar 'eval args)))
- (if (= 0 exit-code)
- (if insert-year
- (buffer-substring 2 (point-max))
- (buffer-substring 7 (point-max)))
- (concat (if insert-year entry-text
- (substring entry-text 6)) "\n"))) "\n"))))
+ (goto-char (point-min))
+ (if (looking-at "Error: ")
+ (progn
+ (message (buffer-string))
+ (error))
+ (buffer-string)))
+ "\n"))))
(defun ledger-current-entry-bounds ()
(save-excursion
@@ -1172,7 +1170,17 @@ the default."
(while (pcomplete-here
(if (eq (save-excursion
(ledger-thing-at-point)) 'entry)
- (ledger-entries)
+ (progn
+ (let ((text (buffer-substring (line-beginning-position)
+ (line-end-position))))
+ (delete-region (line-beginning-position)
+ (line-end-position))
+ (condition-case err
+ (ledger-add-entry text t)
+ ((error)
+ (insert text))))
+ (goto-char (line-end-position))
+ (throw 'pcompleted t))
(ledger-accounts)))))
(defun ledger-fully-complete-entry ()
diff --git a/src/derive.cc b/src/derive.cc
index 979c6033..1a69702e 100644
--- a/src/derive.cc
+++ b/src/derive.cc
@@ -167,6 +167,9 @@ namespace {
else if (arg == "note") {
tmpl.note = (*++begin).to_string();
}
+ else if (arg == "rest") {
+ ; // just ignore this argument
+ }
else {
// Without a preposition, it is either:
//
@@ -281,6 +284,14 @@ namespace {
"No accounts, and no past entry matching '" << tmpl.payee_mask <<"'");
}
} else {
+ bool any_xact_has_amount = false;
+ foreach (entry_template_t::xact_template_t& xact, tmpl.xacts) {
+ if (xact.amount) {
+ any_xact_has_amount = true;
+ break;
+ }
+ }
+
foreach (entry_template_t::xact_template_t& xact, tmpl.xacts) {
std::auto_ptr<xact_t> new_xact;
@@ -300,11 +311,6 @@ namespace {
else
new_xact.reset(new xact_t(*matching->xacts.front()));
}
- if (new_xact.get()) {
- found_commodity = &new_xact->amount.commodity();
- // Ignore the past amount from these transactions
- new_xact->amount = amount_t();
- }
}
if (! new_xact.get())
@@ -317,7 +323,6 @@ namespace {
acct = journal.find_account_re(xact.account_mask->expr.str());
if (! acct)
acct = journal.find_account(xact.account_mask->expr.str());
- new_xact->account = acct;
// Find out the default commodity to use by looking at the last
// commodity used in that account
@@ -328,12 +333,17 @@ namespace {
j++) {
foreach (xact_t * x, (*j)->xacts) {
if (x->account == acct && ! x->amount.is_null()) {
- found_commodity = &x->amount.commodity();
+ new_xact.reset(new xact_t(*x));
break;
}
}
}
+ if (! new_xact.get())
+ new_xact.reset(new xact_t);
+
+ new_xact->account = acct;
} else {
+
if (xact.from)
new_xact->account = journal.find_account("Liabilities:Unknown");
else
@@ -341,6 +351,15 @@ namespace {
}
}
+ if (new_xact.get() && ! new_xact->amount.is_null()) {
+ found_commodity = &new_xact->amount.commodity();
+
+ if (any_xact_has_amount)
+ new_xact->amount = amount_t();
+ else
+ any_xact_has_amount = true;
+ }
+
if (xact.amount) {
new_xact->amount = *xact.amount;
if (xact.from)