Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle invisible transactions when navigating #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions ledger-navigate.el
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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))
Expand Down
25 changes: 25 additions & 0 deletions test/navigate-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down