In ledger-mode, if TAB is pressed in an entry, call out to "entry".

This commit is contained in:
John Wiegley 2009-02-11 20:16:45 -04:00
parent 78418ad92b
commit 20bf2547d1
2 changed files with 53 additions and 26 deletions

View file

@ -201,38 +201,36 @@ Return the difference in the format of a time value."
(if (ledger-time-less-p moment date) (if (ledger-time-less-p moment date)
(throw 'found t))))))) (throw 'found t)))))))
(defun ledger-add-entry (entry-text) (defun ledger-add-entry (entry-text &optional insert-at-point)
(interactive (interactive
(list (list
(read-string "Entry: " (concat ledger-year "/" ledger-month "/")))) (read-string "Entry: " (concat ledger-year "/" ledger-month "/"))))
(let* ((args (with-temp-buffer (let* ((args (with-temp-buffer
(insert entry-text) (insert entry-text)
(eshell-parse-arguments (point-min) (point-max)))) (eshell-parse-arguments (point-min) (point-max))))
(date (car args))
(insert-year t)
(ledger-buf (current-buffer)) (ledger-buf (current-buffer))
exit-code) exit-code)
(unless insert-at-point
(let ((date (car args)))
(if (string-match "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)" date) (if (string-match "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)" date)
(setq date (setq date
(encode-time 0 0 0 (string-to-number (match-string 3 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 2 date))
(string-to-number (match-string 1 date))))) (string-to-number (match-string 1 date)))))
(ledger-find-slot date) (ledger-find-slot date)))
(save-excursion
(if (re-search-backward "^Y " nil t)
(setq insert-year nil)))
(save-excursion (save-excursion
(insert (insert
(with-temp-buffer (with-temp-buffer
(setq exit-code (setq exit-code
(apply #'ledger-run-ledger ledger-buf "entry" (apply #'ledger-run-ledger ledger-buf "entry"
(mapcar 'eval args))) (mapcar 'eval args)))
(if (= 0 exit-code) (goto-char (point-min))
(if insert-year (if (looking-at "Error: ")
(buffer-substring 2 (point-max)) (progn
(buffer-substring 7 (point-max))) (message (buffer-string))
(concat (if insert-year entry-text (error))
(substring entry-text 6)) "\n"))) "\n")))) (buffer-string)))
"\n"))))
(defun ledger-current-entry-bounds () (defun ledger-current-entry-bounds ()
(save-excursion (save-excursion
@ -1172,7 +1170,17 @@ the default."
(while (pcomplete-here (while (pcomplete-here
(if (eq (save-excursion (if (eq (save-excursion
(ledger-thing-at-point)) 'entry) (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))))) (ledger-accounts)))))
(defun ledger-fully-complete-entry () (defun ledger-fully-complete-entry ()

View file

@ -167,6 +167,9 @@ namespace {
else if (arg == "note") { else if (arg == "note") {
tmpl.note = (*++begin).to_string(); tmpl.note = (*++begin).to_string();
} }
else if (arg == "rest") {
; // just ignore this argument
}
else { else {
// Without a preposition, it is either: // Without a preposition, it is either:
// //
@ -281,6 +284,14 @@ namespace {
"No accounts, and no past entry matching '" << tmpl.payee_mask <<"'"); "No accounts, and no past entry matching '" << tmpl.payee_mask <<"'");
} }
} else { } 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) { foreach (entry_template_t::xact_template_t& xact, tmpl.xacts) {
std::auto_ptr<xact_t> new_xact; std::auto_ptr<xact_t> new_xact;
@ -300,11 +311,6 @@ namespace {
else else
new_xact.reset(new xact_t(*matching->xacts.front())); 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()) if (! new_xact.get())
@ -317,7 +323,6 @@ namespace {
acct = journal.find_account_re(xact.account_mask->expr.str()); acct = journal.find_account_re(xact.account_mask->expr.str());
if (! acct) if (! acct)
acct = journal.find_account(xact.account_mask->expr.str()); 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 // Find out the default commodity to use by looking at the last
// commodity used in that account // commodity used in that account
@ -328,12 +333,17 @@ namespace {
j++) { j++) {
foreach (xact_t * x, (*j)->xacts) { foreach (xact_t * x, (*j)->xacts) {
if (x->account == acct && ! x->amount.is_null()) { if (x->account == acct && ! x->amount.is_null()) {
found_commodity = &x->amount.commodity(); new_xact.reset(new xact_t(*x));
break; break;
} }
} }
} }
if (! new_xact.get())
new_xact.reset(new xact_t);
new_xact->account = acct;
} else { } else {
if (xact.from) if (xact.from)
new_xact->account = journal.find_account("Liabilities:Unknown"); new_xact->account = journal.find_account("Liabilities:Unknown");
else 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) { if (xact.amount) {
new_xact->amount = *xact.amount; new_xact->amount = *xact.amount;
if (xact.from) if (xact.from)