-
Notifications
You must be signed in to change notification settings - Fork 75
/
ledger-navigate.el
199 lines (172 loc) · 6.97 KB
/
ledger-navigate.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
;;; ledger-navigate.el --- Provide navigation services through the ledger buffer. -*- lexical-binding: t; -*-
;; Copyright (C) 2014-2015 Craig Earls (enderw88 AT gmail DOT com)
;; This file is not part of GNU Emacs.
;; This is free software; you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free
;; Software Foundation; either version 2, or (at your option) any later
;; version.
;;
;; This is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
;; for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
;; MA 02110-1301 USA.
;;; Commentary:
;;
;;; Code:
(require 'ledger-regex)
(require 'ledger-context)
(defun ledger-navigate-next-xact ()
"Move point to beginning of next xact."
;; make sure we actually move to the next xact, even if we are the
;; beginning of one now.
(if (looking-at ledger-payee-any-status-regex)
(forward-line))
(if (re-search-forward ledger-payee-any-status-regex nil t)
(goto-char (match-beginning 0))
(goto-char (point-max))))
(defun ledger-navigate-start-xact-or-directive-p ()
"Return t if at the beginning line of an xact or directive.
Assumes point is at the beginning of a line."
(not (looking-at "[ \t]\\|\\(^$\\)")))
(defun ledger-navigate-next-xact-or-directive ()
"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
(progn
(forward-line)
(unless (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)))
(forward-line))))
(defun ledger-navigate-prev-xact-or-directive ()
"Move point to beginning of previous xact."
(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)))
(defun ledger-navigate-beginning-of-xact ()
"Move point to the beginning of the current xact."
(interactive)
;; need to start at the beginning of a line in case we are in the first line of an xact already.
(beginning-of-line)
(let ((sreg (concat "^[=~[:digit:]]")))
(unless (looking-at sreg)
(re-search-backward sreg nil t)
(beginning-of-line)))
(point))
(defun ledger-navigate-end-of-xact ()
"Move point to end of xact."
(interactive)
(ledger-navigate-next-xact-or-directive)
(re-search-backward ".$")
(end-of-line)
(point))
(defun ledger-navigate-to-line (line-number)
"Rapidly move point to line LINE-NUMBER."
(goto-char (point-min))
(forward-line (1- line-number)))
(defun ledger-navigate-find-xact-extents (pos)
"Return list containing point for beginning and end of xact containing POS.
Requires empty line separating xacts."
(interactive "d")
(save-excursion
(goto-char pos)
(list (ledger-navigate-beginning-of-xact)
(ledger-navigate-end-of-xact))))
(defun ledger-navigate-skip-lines-backwards (re)
"Move backwards if necessary until the line beginning does not match RE."
(beginning-of-line)
(while (and (looking-at-p re)
(zerop (forward-line -1)))))
(defun ledger-navigate-skip-lines-forwards (re)
"Move forwards if necessary until the line beginning does not match RE."
(beginning-of-line)
(while (and (looking-at-p re)
(zerop (forward-line 1)))))
(defun ledger-navigate-find-directive-extents (pos)
"Return the extents of the directive at POS."
(goto-char pos)
(let ((begin (progn (ledger-navigate-skip-lines-backwards "[ \t]\\|end[[:blank:]]+\\(?:comment\\|test\\)")
(point)))
(end (progn (forward-line 1)
(ledger-navigate-skip-lines-forwards "[ \t]")
(1- (point))))
(comment-re " *;"))
;; handle block comments here
(goto-char begin)
(cond
((looking-at comment-re)
(ledger-navigate-skip-lines-backwards comment-re)
;; We are either at the beginning of the buffer, or we found
;; a line outside the comment, or both. If we are outside
;; the comment then we need to move forward a line.
(unless (looking-at comment-re)
(forward-line 1)
(beginning-of-line))
(setq begin (point))
(goto-char pos)
(ledger-navigate-skip-lines-forwards comment-re)
(setq end (point)))
((looking-at "\\(?:comment\\|test\\)\\>")
(setq end (or (save-match-data
(re-search-forward "^end[[:blank:]]+\\(?:comment\\|test\\)\\_>"))
(point-max)))))
(list begin end)))
(defun ledger-navigate-block-comment (pos)
"Move past the block comment at POS, and return its extents."
(interactive "d")
(goto-char pos)
(let ((begin (progn (beginning-of-line)
(point)))
(end (progn (end-of-line)
(point)))
(comment-re " *;"))
;; handle block comments here
(beginning-of-line)
(when (looking-at comment-re)
(ledger-navigate-skip-lines-backwards comment-re)
(setq begin (point))
(goto-char pos)
(beginning-of-line)
(ledger-navigate-skip-lines-forwards comment-re)
(setq end (point)))
(list begin end)))
(defun ledger-navigate-find-element-extents (pos)
"Return list containing beginning and end of the entity surrounding POS."
(interactive "d")
(save-excursion
(goto-char pos)
(beginning-of-line)
(ledger-navigate-skip-lines-backwards "[ \t]\\|end[[:blank:]]+\\(?:comment\\|test\\)\\_>")
(if (looking-at "[=~0-9\\[]")
(ledger-navigate-find-xact-extents pos)
(ledger-navigate-find-directive-extents pos))))
(defun ledger-navigate-next-uncleared ()
"Move point to the next uncleared transaction."
(interactive)
(when (looking-at ledger-payee-uncleared-regex)
(forward-line))
(if (re-search-forward ledger-payee-uncleared-regex nil t)
(progn (beginning-of-line)
(point))
(user-error "No next uncleared transactions")))
(defun ledger-navigate-previous-uncleared ()
"Move point to the previous uncleared transaction."
(interactive)
(when (equal (car (ledger-context-at-point)) 'acct-transaction)
(ledger-navigate-beginning-of-xact))
(if (re-search-backward ledger-payee-uncleared-regex nil t)
(progn (beginning-of-line)
(point))
(user-error "No previous uncleared transactions")))
(provide 'ledger-navigate)
;;; ledger-navigate.el ends here