From 479d3a55ef008707f0e2bcdd2838fe6a762ff5b3 Mon Sep 17 00:00:00 2001 From: "Kevin J. Foley" Date: Sat, 9 Nov 2019 11:33:47 -0500 Subject: [PATCH] Handle invisible transactions when navigating This allows next and prev navigate commands to work better with `ledger-occur`. --- ledger-navigate.el | 44 +++++++++++++++++++++++++------------------ test/navigate-test.el | 25 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/ledger-navigate.el b/ledger-navigate.el index 811662ca6..1d1f14794 100644 --- a/ledger-navigate.el +++ b/ledger-navigate.el @@ -38,31 +38,39 @@ (goto-char (match-beginning 0)) (goto-char (point-max)))) -(defun ledger-navigate-start-xact-or-directive-p () - "Return t if at the beginning of an empty or all-whitespace line." - (not (looking-at "[ \t]\\|\\(^$\\)"))) - -(defun ledger-navigate-next-xact-or-directive () +(defun ledger-navigate-start-xact-or-directive-p (&optional invisible) + "Return t if at the beginning of an empty or +all-whitespaceline. If INVISIBLE then consider invisible lines, +otherwise they return nil." + (and (or invisible + (not (invisible-p (point)))) + (not (looking-at "[ \t]\\|\\(^$\\)")))) + +(defun ledger-navigate-next-xact-or-directive (&optional invisible) "Move to the beginning of the next xact or directive." (interactive) (beginning-of-line) - (if (ledger-navigate-start-xact-or-directive-p) ; if we are the start of an xact, move forward to the next xact + (if (ledger-navigate-start-xact-or-directive-p invisible) ; if we are the start of an xact, move forward towards the next xact (progn (forward-line) - (if (not (ledger-navigate-start-xact-or-directive-p)) ; we have moved forward and are not at another xact, recurse forward - (ledger-navigate-next-xact-or-directive))) - (while (not (or (eobp) ; we didn't start off at the beginning of an xact - (ledger-navigate-start-xact-or-directive-p))) + (if (not (ledger-navigate-start-xact-or-directive-p invisible)) ; we have moved forward and are not at another xact, recurse forward + (ledger-navigate-next-xact-or-directive invisible))) + (while (not (or (eobp) ; we didn't start off at the beginning of an xact + (ledger-navigate-start-xact-or-directive-p invisible))) (forward-line)))) -(defun ledger-navigate-prev-xact-or-directive () - "Move point to beginning of previous xact." +(defun ledger-navigate-prev-xact-or-directive (&optional invisible) + "Move to the beginning of the previous xact or directive." (interactive) - (let ((context (car (ledger-context-at-point)))) - (when (equal context 'acct-transaction) - (ledger-navigate-beginning-of-xact)) - (beginning-of-line) - (re-search-backward "^[[:graph:]]" nil t))) + (beginning-of-line) + (if (ledger-navigate-start-xact-or-directive-p invisible) ; if we are the start of an xact, move backward towards the next xact + (progn + (forward-line -1) + (if (not (ledger-navigate-start-xact-or-directive-p invisible)) ; we have moved backward and are not at another xact, recurse backward + (ledger-navigate-prev-xact-or-directive invisible))) + (while (not (or (bobp) ; we didn't start off at the beginning of an xact + (ledger-navigate-start-xact-or-directive-p invisible))) + (forward-line -1)))) (defun ledger-navigate-beginning-of-xact () "Move point to the beginning of the current xact." @@ -78,7 +86,7 @@ (defun ledger-navigate-end-of-xact () "Move point to end of xact." (interactive) - (ledger-navigate-next-xact-or-directive) + (ledger-navigate-next-xact-or-directive t) (re-search-backward ".$") (end-of-line) (point)) diff --git a/test/navigate-test.el b/test/navigate-test.el index 503dbc813..344b2aebd 100644 --- a/test/navigate-test.el +++ b/test/navigate-test.el @@ -71,6 +71,31 @@ http://bugs.ledger-cli.org/show_bug.cgi?id=441" (should (bobp)) )) +(ert-deftest ledger-navigate-invisible () + :tags '(navigate) + (with-temp-buffer + (insert + "2011/01/27 Book Store + Expenses:Books $20.00 + Liabilities:MasterCard + +2011/04/25 Tom's Used Cars + Expenses:Auto $ 5,500.00 + Assets:Checking + +2011/04/27 Bookstore + Expenses:Books $20.00 + Assets:Checking + +2011/12/01 * Sale + Assets:Checking $ 30.00 + Income:Sales") + (ledger-mode) + (goto-char (point-min)) + (ledger-occur "Books") + (ledger-navigate-next-xact-or-directive) + (should (looking-at-p (regexp-quote "2011/04/27 Bookstore"))))) + (provide 'navigate-test)