summaryrefslogtreecommitdiff
path: root/lisp/ldg-report.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/ldg-report.el')
-rw-r--r--lisp/ldg-report.el66
1 files changed, 65 insertions, 1 deletions
diff --git a/lisp/ldg-report.el b/lisp/ldg-report.el
index 5a668847..f9c6afca 100644
--- a/lisp/ldg-report.el
+++ b/lisp/ldg-report.el
@@ -1,3 +1,6 @@
+(eval-when-compile
+ (require 'cl))
+
(defcustom ledger-reports
'(("bal" "ledger -f %(ledger-file) bal")
("reg" "ledger -f %(ledger-file) reg")
@@ -66,6 +69,7 @@ text that should replace the format specifier."
'ledger-report-kill)
(define-key map [(control ?c) (control ?l) (control ?e)]
'ledger-report-edit)
+ (define-key map [(control ?c) (control ?c)] 'ledger-report-visit-source)
(use-local-map map)))
(defun ledger-report-read-name ()
@@ -227,6 +231,28 @@ the default."
(ledger-reports-custom-save))
report-cmd))
+(defvar ledger-report-patch-alist nil)
+
+(defun ledger-report-patch-reports (buf)
+ (when ledger-report-patch-alist
+ (let ((entry (assoc (expand-file-name (buffer-file-name buf))
+ ledger-report-patch-alist)))
+ (when entry
+ (dolist (b (cdr entry))
+ (if (buffer-live-p b)
+ (with-current-buffer b
+ (save-excursion
+ (goto-char (point-min))
+ (while (not (eobp))
+ (let ((record (get-text-property (point) 'ledger-source)))
+ (if (and record (not (markerp (cdr record))))
+ (setcdr record (with-current-buffer buf
+ (save-excursion
+ (goto-char (point-min))
+ (forward-line (cdr record))
+ (point-marker))))))
+ (forward-line 1))))))))))
+
(defun ledger-do-report (cmd)
"Run a report command line."
(goto-char (point-min))
@@ -234,7 +260,43 @@ the default."
(format "Command: %s\n" cmd)
(make-string (- (window-width) 1) ?=)
"\n")
- (shell-command cmd t nil))
+ (let ((register-report (string-match " reg\\(ister\\)? " cmd))
+ files-in-report)
+ (shell-command
+ (if register-report
+ (concat cmd " --prepend-format='%(filename):%(beg_line):'")
+ cmd) t nil)
+ (when register-report
+ (goto-char (point-min))
+ (while (re-search-forward "^\\([^:]+\\)?:\\([0-9]+\\)?:" nil t)
+ (let ((file (match-string 1))
+ (line (string-to-number (match-string 2))))
+ (delete-region (match-beginning 0) (match-end 0))
+ (set-text-properties (line-beginning-position) (line-end-position)
+ (list 'ledger-source (cons file line)))
+ (let* ((fullpath (expand-file-name file))
+ (entry (assoc fullpath ledger-report-patch-alist)))
+ (if entry
+ (nconc (cdr entry) (list (current-buffer)))
+ (push (cons (expand-file-name file)
+ (list (current-buffer)))
+ ledger-report-patch-alist))
+ (add-to-list 'files-in-report fullpath)))
+
+ (dolist (path files-in-report)
+ (let ((buf (get-file-buffer path)))
+ (if (and buf (buffer-live-p buf))
+ (ledger-report-patch-reports buf))))))))
+
+(defun ledger-report-visit-source ()
+ (interactive)
+ (let ((prop (get-text-property (point) 'ledger-source)))
+ (destructuring-bind (file . line-or-marker) prop
+ (find-file-other-window file)
+ (if (markerp line-or-marker)
+ (goto-char line-or-marker)
+ (goto-char (point-min))
+ (forward-line (1- line-or-marker))))))
(defun ledger-report-goto ()
"Goto the ledger report buffer."
@@ -446,3 +508,5 @@ specified line, returns nil."
(if (eq (ledger-context-line-type context-info) 'entry)
(ledger-context-field-value context-info 'payee)
nil))))
+
+(provide 'ldg-report)