forked from ledger/ledger-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledger-check.el
136 lines (113 loc) · 4.82 KB
/
ledger-check.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
;;; ledger-check.el --- Helper code for use with the "ledger" command-line tool
;; Copyright (C) 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:
;; Provide secial mode to correct errors in ledger when running with --strict and --explicit
;;
;; Adapted to ledger mode by Craig Earls <enderw88 at gmail dot com>
;;; Code:
(require 'easymenu)
(eval-when-compile
(require 'cl))
(defvar ledger-check-buffer-name "*Ledger Check*")
(defvar ledger-check-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [return] 'ledger-report-visit-source)
(define-key map [?q] 'ledger-check-quit)
map)
"Keymap for `ledger-check-mode'.")
(easy-menu-define ledger-check-mode-menu ledger-check-mode-map
"Ledger check menu"
'("Check"
; ["Re-run Check" ledger-check-redo]
"---"
["Visit Source" ledger-report-visit-source]
"---"
["Quit" ledger-check-quit]
))
(define-derived-mode ledger-check-mode text-mode "Ledger-Check"
"A mode for viewing ledger errors and warnings.")
(defun ledger-do-check ()
"Run a check command ."
(goto-char (point-min))
(let ((data-pos (point))
(have-warnings nil))
(shell-command
;; ledger balance command will just return empty if you give it
;; an account name that doesn't exist. I will assume that no
;; one will ever have an account named "e342asd2131". If
;; someones does, this will probably still work for them.
;; I should only highlight error and warning lines.
"ledger bal e342asd2131 --strict --explicit "
t nil)
(goto-char data-pos)
;; format check report to make it navigate the file
(while (re-search-forward "^.*: \"\\(.*\\)\", line \\([0-9]+\\)" nil t)
(let ((file (match-string 1))
(line (string-to-number (match-string 2))))
(when file
(set-text-properties (line-beginning-position) (line-end-position)
(list 'ledger-source (cons file (save-window-excursion
(save-excursion
(find-file file)
(widen)
(ledger-navigate-to-line line)
(point-marker))))))
(add-text-properties (line-beginning-position) (line-end-position)
(list 'font-lock-face 'ledger-font-report-clickable-face))
(setq have-warnings 'true)
(end-of-line))))
(if (not have-warnings)
(insert "No errors or warnings reported."))))
(defun ledger-check-goto ()
"Goto the ledger check buffer."
(interactive)
(let ((rbuf (get-buffer ledger-check-buffer-name)))
(if (not rbuf)
(error "There is no ledger check buffer"))
(pop-to-buffer rbuf)
(shrink-window-if-larger-than-buffer)))
(defun ledger-check-quit ()
"Quit the ledger check buffer."
(interactive)
(ledger-check-goto)
(set-window-configuration ledger-original-window-cfg)
(kill-buffer (get-buffer ledger-check-buffer-name)))
(defun ledger-check-buffer ()
"Run a ledge with --explicit and --strict report errors and assist with fixing them.
The output buffer will be in `ledger-check-mode', which defines
commands for navigating the buffer to the errors found, etc."
(interactive
(progn
(when (and (buffer-modified-p)
(y-or-n-p "Buffer modified, save it? "))
(save-buffer))))
(let ((buf (current-buffer))
(cbuf (get-buffer ledger-check-buffer-name))
(wcfg (current-window-configuration)))
(if cbuf
(kill-buffer cbuf))
(with-current-buffer
(pop-to-buffer (get-buffer-create ledger-check-buffer-name))
(ledger-check-mode)
(set (make-local-variable 'ledger-original-window-cfg) wcfg)
(ledger-do-check)
(shrink-window-if-larger-than-buffer)
(set-buffer-modified-p nil)
(setq buffer-read-only t)
(message "q to quit; r to redo; k to kill"))))
(provide 'ledger-check)