ledger-report.el (27276B)
1 ;;; ledger-report.el --- Helper code for use with the "ledger" command-line tool -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2003-2016 John Wiegley (johnw AT gnu DOT org) 4 5 ;; This file is not part of GNU Emacs. 6 7 ;; This is free software; you can redistribute it and/or modify it under 8 ;; the terms of the GNU General Public License as published by the Free 9 ;; Software Foundation; either version 2, or (at your option) any later 10 ;; version. 11 ;; 12 ;; This is distributed in the hope that it will be useful, but WITHOUT 13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 ;; for more details. 16 ;; 17 ;; You should have received a copy of the GNU General Public License 18 ;; along with GNU Emacs; see the file COPYING. If not, write to the 19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 20 ;; MA 02110-1301 USA. 21 22 23 ;;; Commentary: 24 ;; Provide facilities for running and saving reports in Emacs 25 26 ;;; Code: 27 28 (require 'ledger-xact) 29 (require 'ledger-navigate) 30 (require 'ledger-commodities) 31 (declare-function ledger-read-string-with-default "ledger-mode" (prompt default)) 32 (declare-function ledger-read-account-with-prompt "ledger-mode" (prompt)) 33 34 (require 'easymenu) 35 (require 'ansi-color) 36 (require 'font-lock) 37 (eval-when-compile 38 (require 'rx) 39 (require 'subr-x)) 40 41 (defgroup ledger-report nil 42 "Customization option for the Report buffer." 43 :group 'ledger) 44 45 (defcustom ledger-reports 46 '(("bal" "%(binary) -f %(ledger-file) bal") 47 ("reg" "%(binary) -f %(ledger-file) reg") 48 ("payee" "%(binary) -f %(ledger-file) reg @%(payee)") 49 ("account" "%(binary) -f %(ledger-file) reg %(account)")) 50 "Definition of reports to run. 51 52 Each element has the form (NAME CMDLINE). The command line can 53 contain format specifiers that are replaced with context sensitive 54 information. Format specifiers have the format '%(<name>)' where 55 <name> is an identifier for the information to be replaced. The 56 `ledger-report-format-specifiers' alist variable contains a mapping 57 from format specifier identifier to a Lisp function that implements 58 the substitution. See the documentation of the individual functions 59 in that variable for more information on the behavior of each 60 specifier." 61 :type '(repeat (list (string :tag "Report Name") 62 (string :tag "Command Line"))) 63 :group 'ledger-report) 64 65 (defcustom ledger-report-format-specifiers 66 '(("ledger-file" . ledger-report-ledger-file-format-specifier) 67 ("binary" . ledger-report-binary-format-specifier) 68 ("payee" . ledger-report-payee-format-specifier) 69 ("account" . ledger-report-account-format-specifier) 70 ("month" . ledger-report-month-format-specifier) 71 ("tagname" . ledger-report-tagname-format-specifier) 72 ("tagvalue" . ledger-report-tagvalue-format-specifier)) 73 "An alist mapping ledger report format specifiers to implementing functions. 74 75 The function is called with no parameters and expected to return 76 a string, or a list of strings, that should replace the format specifier. 77 Single strings are quoted with `shell-quote-argument'; lists of strings are 78 simply concatenated (no quoting)." 79 :type 'alist 80 :group 'ledger-report) 81 82 (defcustom ledger-report-auto-refresh t 83 "If non-nil, automatically rerun the report when the ledger buffer is saved." 84 :type 'boolean 85 :group 'ledger-report) 86 87 (defcustom ledger-report-auto-refresh-sticky-cursor nil 88 "If non-nil, keep cursor's relative position after auto-refresh." 89 :type 'boolean 90 :group 'ledger-report) 91 92 (defcustom ledger-report-links-in-register t 93 "If non-nil, link entries in \"register\" reports to entries in the ledger buffer." 94 :type 'boolean 95 :group 'ledger-report) 96 97 (defcustom ledger-report-use-native-highlighting t 98 "When non-nil, use ledger's native highlighting in reports." 99 :type 'boolean 100 :package-version '(ledger-mode . "4.0.0") 101 :group 'ledger-report) 102 103 (defcustom ledger-report-native-highlighting-arguments '("--color" "--force-color") 104 "List of ledger args needed by `ledger-report-use-native-highlighting'. 105 106 If you are using hledger instead of ledger, you might want to set 107 this variable to `(\"--color=always\")'." 108 :type '(repeat string) 109 :group 'ledger-report) 110 111 (defcustom ledger-report-auto-width t 112 "When non-nil, tell ledger about the width of the report window." 113 :type 'boolean 114 :package-version '(ledger-mode . "4.0.0") 115 :group 'ledger-report) 116 117 (defcustom ledger-report-use-header-line nil 118 "If non-nil, indicate report name/command in the `header-line'. 119 The report name/command won't be printed in the buffer. See 120 `ledger-report-header-line-fn' for how to customize the 121 information reported." 122 :type 'boolean 123 :package-version '(ledger-mode . "4.0.0") 124 :group 'ledger-report) 125 126 (defcustom ledger-report-header-line-fn #'ledger-report--header-function 127 "Evaluate this function in the `header-line' of the report buffer. 128 `ledger-report-use-header-line' must be non-nil for this to have any effect." 129 :type 'function 130 :package-version '(ledger-mode . "4.0.0") 131 :group 'ledger-report) 132 133 (defcustom ledger-report-resize-window t 134 "If non-nil, resize the report window. 135 Calls `shrink-window-if-larger-than-buffer'." 136 :type 'boolean 137 :package-version '(ledger-mode . "4.0.0") 138 :group 'ledger-report) 139 140 (defcustom ledger-report-use-strict nil 141 "When non-nil, `ledger-mode' will use --strict when running reports?" 142 :type 'boolean 143 :package-version '(ledger-mode . "4.0.0") 144 :group 'ledger-report) 145 146 (defcustom ledger-report-after-report-hook nil 147 "Hook run after `ledger-report' has created the buffer and report." 148 :type 'boolean 149 :package-version '(ledger-mode . "4.0.0") 150 :group 'ledger-report) 151 152 (defvar ledger-report-buffer-name "*Ledger Report*") 153 154 (defvar-local ledger-report-name nil) 155 (defvar-local ledger-report-cmd nil) 156 (defvar-local ledger-report-saved nil) 157 (defvar-local ledger-report-current-month nil) 158 (defvar-local ledger-report-is-reversed nil) 159 (defvar-local ledger-report-cursor-line-number nil) 160 (defvar-local ledger-report-ledger-buf nil) 161 (defvar-local ledger-master-file nil 162 "The master file for the current buffer. 163 See documentation for the function `ledger-master-file'") 164 165 (defvar ledger-report-name-prompt-history nil) 166 (defvar ledger-report-cmd-prompt-history nil) 167 (defvar ledger-minibuffer-history nil) 168 (defvar ledger-report-mode-abbrev-table) 169 170 (defun ledger-report-reverse-report () 171 "Reverse the order of the report." 172 (interactive) 173 (ledger-report-reverse-lines) 174 (setq ledger-report-is-reversed (not ledger-report-is-reversed))) 175 176 (defun ledger-report-reverse-lines () 177 "Reverse the lines in the ledger report buffer." 178 (with-silent-modifications 179 (goto-char (point-min)) 180 (unless ledger-report-use-header-line 181 (forward-paragraph) 182 (forward-line)) 183 (save-excursion 184 (reverse-region (point) (point-max))))) 185 186 (defun ledger-report-maybe-shrink-window () 187 "Shrink window if `ledger-report-resize-window' is non-nil." 188 (when ledger-report-resize-window 189 (shrink-window-if-larger-than-buffer))) 190 191 (defvar ledger-report-mode-map 192 (let ((map (make-sparse-keymap))) 193 (define-key map (kbd "r") #'ledger-report-redo) 194 (define-key map (kbd "R") #'ledger-report-reverse-report) 195 (define-key map (kbd "s") #'ledger-report-save) 196 (define-key map (kbd "S") #'ledger-report) 197 (define-key map (kbd "e") #'ledger-report-edit-report) 198 (define-key map (kbd "E") #'ledger-report-edit-reports) 199 (define-key map (kbd "q") #'ledger-report-quit) 200 (define-key map (kbd "C-c C-l C-r") #'ledger-report-redo) 201 (define-key map (kbd "C-c C-l C-S-s") #'ledger-report-save) 202 (define-key map (kbd "C-c C-l C-e") #'ledger-report-edit-report) 203 (define-key map (kbd "C-c C-o C-r") #'ledger-report) 204 (define-key map (kbd "M-p") #'ledger-report-previous-month) 205 (define-key map (kbd "M-n") #'ledger-report-next-month) 206 (define-key map (kbd "$") #'ledger-report-toggle-default-commodity) 207 map) 208 "Keymap for `ledger-report-mode'.") 209 210 (easy-menu-define ledger-report-mode-menu ledger-report-mode-map 211 "Ledger report menu." 212 '("Reports" 213 ["Select Report" ledger-report] 214 ["Save Report" ledger-report-save] 215 ["Edit Current Report" ledger-report-edit-report] 216 ["Edit All Reports" ledger-report-edit-reports] 217 ["Re-run Report" ledger-report-redo] 218 "---" 219 ["Reverse report order" ledger-report-reverse-report] 220 "---" 221 ["Scroll Up" scroll-up] 222 ["Visit Source" ledger-report-visit-source] 223 ["Scroll Down" scroll-down] 224 "---" 225 ["Quit" ledger-report-quit] 226 )) 227 228 (define-derived-mode ledger-report-mode special-mode "Ledger-Report" 229 "A mode for viewing ledger reports." 230 (setq-local revert-buffer-function #'ledger-report-redo) 231 (hack-dir-local-variables-non-file-buffer)) 232 233 (defconst ledger-report--extra-args-marker "[[ledger-mode-flags]]") 234 235 (defun ledger-report-binary-format-specifier () 236 "Return the path to ledger, plus a marker for extra arguments." 237 (list (shell-quote-argument ledger-binary-path) 238 ledger-report--extra-args-marker)) 239 240 (defun ledger-report-tagname-format-specifier () 241 "Return a valid meta-data tag name." 242 ;; It is intended completion should be available on existing tag 243 ;; names, but it remains to be implemented. 244 (ledger-read-string-with-default "Tag Name" nil)) 245 246 (defun ledger-report-tagvalue-format-specifier () 247 "Return a valid meta-data tag name." 248 ;; It is intended completion should be available on existing tag 249 ;; values, but it remains to be implemented. 250 (ledger-read-string-with-default "Tag Value" nil)) 251 252 (defun ledger-report-read-name () 253 "Read the name of a ledger report to use, with completion. 254 255 The empty string and unknown names are allowed." 256 (completing-read "Report name: " 257 ledger-reports nil nil nil 258 'ledger-report-name-prompt-history nil)) 259 260 (defun ledger-report (report-name edit) 261 "Run a user-specified report from `ledger-reports'. 262 263 Prompts the user for the REPORT-NAME of the report to run or 264 EDIT. If no name is entered, the user will be prompted for a 265 command line to run. The command line specified or associated 266 with the selected report name is run and the output is made 267 available in another buffer for viewing. If a prefix argument is 268 given and the user selects a valid report name, the user is 269 prompted with the corresponding command line for editing before 270 the command is run. 271 272 The output buffer will be in `ledger-report-mode', which defines 273 commands for saving a new named report based on the command line 274 used to generate the buffer, navigating the buffer, etc." 275 (interactive 276 (progn 277 (when (and (buffer-modified-p) 278 (y-or-n-p "Buffer modified, save it? ")) 279 (save-buffer)) 280 (let ((rname (ledger-report-read-name)) 281 (edit (not (null current-prefix-arg)))) 282 (list rname edit)))) 283 (let* ((file (ledger-master-file)) 284 (buf (find-file-noselect file))) 285 (with-current-buffer 286 (pop-to-buffer (get-buffer-create ledger-report-buffer-name)) 287 (with-silent-modifications 288 (erase-buffer) 289 (ledger-report-mode) 290 (setq ledger-report-saved nil) 291 (setq ledger-report-ledger-buf buf) 292 (setq ledger-report-name report-name) 293 (setq ledger-report-is-reversed nil) 294 (setq ledger-report-current-month nil) 295 (setq ledger-master-file file) 296 (ledger-do-report (ledger-report-cmd report-name edit))) 297 (ledger-report-maybe-shrink-window) 298 (run-hooks 'ledger-report-after-report-hook) 299 (message (substitute-command-keys (concat "\\[ledger-report-quit] to quit; " 300 "\\[ledger-report-redo] to redo; " 301 "\\[ledger-report-edit-report] to edit; " 302 "\\[ledger-report-save] to save; " 303 "\\[scroll-up-command] and \\[scroll-down-command] to scroll")))))) 304 305 (defun ledger-report--header-function () 306 "Compute the string to be used as the header in the `ledger-report' buffer." 307 (format "Ledger Report: %s -- Buffer: %s -- Command: %s" 308 (propertize ledger-report-name 'face 'font-lock-constant-face) 309 (propertize (buffer-name ledger-report-ledger-buf) 'face 'font-lock-string-face) 310 (propertize ledger-report-cmd 'face 'font-lock-comment-face))) 311 312 (defun ledger-report-name-exists (name) 313 "Check to see if the given report NAME exists. 314 315 If exists, returns the object naming the report, otherwise 316 returns nil." 317 (unless (string-empty-p name) 318 (car (assoc name ledger-reports)))) 319 320 (defun ledger-reports-add (name cmd) 321 "Add a new report NAME and CMD to `ledger-reports'." 322 (setq ledger-reports (cons (list name cmd) ledger-reports))) 323 324 (defun ledger-reports-custom-save () 325 "Save the `ledger-reports' variable using the customize framework." 326 (customize-save-variable 'ledger-reports ledger-reports)) 327 328 (defun ledger-report-read-command (report-cmd) 329 "Read the command line to create a report from REPORT-CMD." 330 (read-from-minibuffer "Report command line: " 331 (if (null report-cmd) "ledger " report-cmd) 332 nil nil 'ledger-report-cmd-prompt-history)) 333 334 (defun ledger-report-ledger-file-format-specifier () 335 "Substitute the full path to master or current ledger file. 336 337 The master file name is determined by the function 338 `ledger-master-file', which depends on the variable of the same 339 name. If it is non-nil, it is used, otherwise the current 340 buffer's file is used." 341 (ledger-master-file)) 342 343 ;; General helper functions 344 345 (defun ledger-master-file () 346 "Return the master file for a ledger file. 347 348 The master file is either the file for the current ledger buffer 349 or the file specified by the buffer-local variable 350 `ledger-master-file'. Typically this variable would be set in a 351 file local variable comment block at the end of a ledger file 352 which is included in some other file." 353 (if ledger-master-file 354 (expand-file-name ledger-master-file) 355 (buffer-file-name))) 356 357 (defun ledger-report-payee-format-specifier () 358 "Substitute a payee name. 359 360 The user is prompted to enter a payee and that is substituted. 361 If point is in an xact, the payee for that xact is used as the 362 default." 363 ;; It is intended completion should be available on existing 364 ;; payees, but the list of possible completions needs to be 365 ;; developed to allow this. 366 (if-let ((payee (ledger-xact-payee))) 367 (ledger-read-string-with-default "Payee" (regexp-quote payee)) 368 (ledger-read-string-with-default "Payee" nil))) 369 370 (defun ledger-report-account-format-specifier () 371 "Substitute an account name. 372 373 The user is prompted to enter an account name, which can be any 374 regular expression identifying an account. If point is on an 375 account posting line for an xact, the full account name on that 376 line is the default." 377 (ledger-read-account-with-prompt "Account")) 378 379 (defun ledger-report--current-month () 380 "Return current month as (YEAR . MONTH-INDEX). 381 382 MONTH-INDEX ranges from 1 (January) to 12 (December) and YEAR is 383 a number." 384 (let* ((time-parts (decode-time)) 385 (year (nth 5 time-parts)) 386 (month-index (nth 4 time-parts))) 387 (cons year month-index))) 388 389 (defun ledger-report--normalize-month (month) 390 "Return (YEAR . NEW-MONTH) where NEW-MONTH is between 1 and 12. 391 392 MONTH is of the form (YEAR . INDEX) where INDEX is an integer. 393 The purpose of this method is then to convert any year/month pair 394 to a meaningful date, e.g., from (2018 . -2) to (2017 . 10)." 395 (let* ((month-index (cdr month)) 396 (year-shift (/ (1- month-index) 12))) 397 (when (<= month-index 0) 398 (setq year-shift (1- year-shift))) 399 (cons (+ (car month) year-shift) 400 (1+ (mod (1- month-index) 12))))) 401 402 (defun ledger-report--shift-month (month shift) 403 "Return (YEAR . NEW-MONTH) where NEW-MONTH is MONTH+SHIFT. 404 405 MONTH is of the form (YEAR . INDEX) where INDEX ranges from 406 1 (January) to 12 (December) and YEAR is a number." 407 (let* ((year (car month)) 408 (new-month (+ (cdr month) shift))) 409 (ledger-report--normalize-month (cons year new-month)))) 410 411 (defun ledger-report-month-format-specifier () 412 "Substitute current month." 413 (with-current-buffer (or ledger-report-buffer-name (current-buffer)) 414 (let* ((month (or ledger-report-current-month (ledger-report--current-month))) 415 (year (car month)) 416 (month-index (cdr month))) 417 (format "%s-%s" year month-index)))) 418 419 (defun ledger-report-expand-format-specifiers (report-cmd) 420 "Expand format specifiers in REPORT-CMD with thing under point." 421 (save-match-data 422 (let ((expanded-cmd report-cmd)) 423 (set-match-data (list 0 0)) 424 (while (string-match "%(\\([^)]*\\))" expanded-cmd 425 (if (> (length expanded-cmd) (match-end 0)) 426 (match-end 0) 427 (1- (length expanded-cmd)))) 428 (let* ((specifier (match-string 1 expanded-cmd)) 429 (f (cdr (assoc specifier ledger-report-format-specifiers)))) 430 (if f 431 (let* ((arg (save-match-data 432 (with-current-buffer ledger-report-ledger-buf 433 (funcall f)))) 434 (quoted (if (listp arg) 435 (mapconcat #'identity arg " ") 436 (save-match-data 437 (shell-quote-argument arg))))) 438 (setq expanded-cmd (replace-match quoted t t expanded-cmd)))))) 439 expanded-cmd))) 440 441 (defun ledger-report--cmd-needs-links-p (cmd) 442 "Check links should be added to the report produced by CMD." 443 ;; --subtotal reports do not produce identifiable transactions, so 444 ;; don't prepend location information for them 445 (and (string-match "\\<reg\\(ister\\)?\\>" cmd) 446 ledger-report-links-in-register 447 (not (string-match "--subtotal" cmd)))) 448 449 (defun ledger-report--compute-extra-args (report-cmd) 450 "Compute extra args to add to REPORT-CMD." 451 `(,@(when (ledger-report--cmd-needs-links-p report-cmd) 452 '("--prepend-format=%(filename):%(beg_line):")) 453 ,@(when ledger-report-auto-width 454 `("--columns" ,(format "%d" (window-max-chars-per-line)))) 455 ,@(when ledger-report-use-native-highlighting 456 ledger-report-native-highlighting-arguments) 457 ,@(when ledger-report-use-strict 458 '("--strict")))) 459 460 (defun ledger-report-cmd (report-name edit) 461 "Get the command line to run the report name REPORT-NAME. 462 Optionally EDIT the command." 463 (let ((report-cmd (car (cdr (assoc report-name ledger-reports))))) 464 ;; logic for substitution goes here 465 (when (or (null report-cmd) edit) 466 (setq report-cmd (ledger-report-read-command report-cmd)) 467 (setq ledger-report-saved nil)) ;; this is a new report, or edited report 468 (setq report-cmd (ledger-report-expand-format-specifiers report-cmd)) 469 (setq ledger-report-cmd report-cmd) 470 (or (string-empty-p report-name) 471 (ledger-report-name-exists report-name) 472 (progn 473 (ledger-reports-add report-name report-cmd) 474 (ledger-reports-custom-save))) 475 report-cmd)) 476 477 (define-button-type 'ledger-report-register-entry 478 'follow-link t 479 'face nil ;; Otherwise make-text-button replaces Ledger's native highlighting 480 'action (lambda (_button) (ledger-report-visit-source))) 481 482 (defun ledger-report--change-month (shift) 483 "Rebuild report with transactions from current month + SHIFT." 484 (let* ((current-month (or ledger-report-current-month (ledger-report--current-month))) 485 (previous-month (ledger-report--shift-month current-month shift))) 486 (setq ledger-report-current-month previous-month) 487 (ledger-report-cmd ledger-report-name nil) 488 (ledger-report-redo))) 489 490 (defun ledger-report--add-links () 491 "Replace file and line annotations with buttons." 492 (while (re-search-forward "^\\(\\(?:/\\|[a-zA-Z]:[\\/]\\)[^:]+\\)?:\\([0-9]+\\)?:" nil t) 493 (let ((file (match-string 1)) 494 (line (string-to-number (match-string 2)))) 495 (delete-region (match-beginning 0) (match-end 0)) 496 (when (and file line) 497 (add-text-properties (line-beginning-position) (line-end-position) 498 (list 'ledger-source (cons file line))) 499 (make-text-button 500 (line-beginning-position) (line-end-position) 501 'type 'ledger-report-register-entry 502 'help-echo (format "mouse-2, RET: Visit %s:%d" file line)) 503 ;; Appending the face preserves Ledger's native highlighting 504 (font-lock-append-text-property (line-beginning-position) (line-end-position) 505 'face 'ledger-font-report-clickable-face) 506 (end-of-line))))) 507 508 (defun ledger-report--compute-header-line (cmd) 509 "Call `ledger-report-header-line-fn' with `ledger-report-cmd' bound to CMD." 510 (let ((ledger-report-cmd cmd)) 511 (funcall ledger-report-header-line-fn))) 512 513 (defun ledger-do-report (cmd) 514 "Run a report command line CMD. 515 CMD may contain a (shell-quoted) version of 516 `ledger-report--extra-args-marker', which will be replaced by 517 arguments returned by `ledger-report--compute-extra-args'." 518 (goto-char (point-min)) 519 (let* ((marker ledger-report--extra-args-marker) 520 (marker-re (concat " *" (regexp-quote marker))) 521 (args (ledger-report--compute-extra-args cmd)) 522 (args-str (concat " " (mapconcat #'shell-quote-argument args " "))) 523 (clean-cmd (replace-regexp-in-string marker-re "" cmd t t)) 524 (real-cmd (replace-regexp-in-string marker-re args-str cmd t t))) 525 (setq header-line-format 526 (and ledger-report-use-header-line 527 `(:eval (ledger-report--compute-header-line ,clean-cmd)))) 528 (unless ledger-report-use-header-line 529 (insert (format "Report: %s\n" ledger-report-name) 530 (format "Command: %s\n" clean-cmd) 531 (make-string (- (window-width) 1) ?=) 532 "\n\n")) 533 (let* ((report (shell-command-to-string real-cmd))) 534 (when ledger-report-use-native-highlighting 535 (setq report (ansi-color-apply report))) 536 (save-excursion 537 (insert report)) 538 (when (ledger-report--cmd-needs-links-p cmd) 539 (save-excursion 540 (ledger-report--add-links)))))) 541 542 (defun ledger-report-visit-source () 543 "Visit the transaction under point in the report window." 544 (interactive) 545 (let* ((prop (get-text-property (point) 'ledger-source)) 546 (file (car prop)) 547 (line (cdr prop))) 548 (when (and file line) 549 (find-file-other-window file) 550 (widen) 551 (goto-char (point-min)) 552 (forward-line (1- line)) 553 (ledger-navigate-beginning-of-xact)))) 554 555 (defun ledger-report-goto () 556 "Goto the ledger report buffer." 557 (interactive) 558 (let ((rbuf (get-buffer ledger-report-buffer-name))) 559 (if (not rbuf) 560 (error "There is no ledger report buffer")) 561 (pop-to-buffer rbuf) 562 (ledger-report-maybe-shrink-window))) 563 564 (defun ledger-report-redo (&optional _ignore-auto _noconfirm) 565 "Redo the report in the current ledger report buffer. 566 IGNORE-AUTO and NOCONFIRM are for compatibility with 567 `revert-buffer-function' and are currently ignored." 568 (interactive) 569 (unless (or (derived-mode-p 'ledger-mode) 570 (derived-mode-p 'ledger-report-mode)) 571 (user-error "Not in a ledger-mode or ledger-report-mode buffer")) 572 (let ((cur-buf (current-buffer))) 573 (when (and ledger-report-auto-refresh 574 (get-buffer ledger-report-buffer-name)) 575 (pop-to-buffer (get-buffer ledger-report-buffer-name)) 576 (ledger-report-maybe-shrink-window) 577 (setq ledger-report-cursor-line-number (line-number-at-pos)) 578 (with-silent-modifications 579 (erase-buffer) 580 (ledger-do-report ledger-report-cmd) 581 (when ledger-report-is-reversed 582 (ledger-report-reverse-lines)) 583 (when ledger-report-auto-refresh-sticky-cursor 584 (forward-line (- ledger-report-cursor-line-number 5)))) 585 (run-hooks 'ledger-report-after-report-hook) 586 (pop-to-buffer cur-buf)))) 587 588 (defun ledger-report-quit () 589 "Quit the ledger report buffer and kill its buffer." 590 (interactive) 591 (unless (buffer-live-p (get-buffer ledger-report-buffer-name)) 592 (user-error "No ledger report buffer")) 593 (quit-windows-on ledger-report-buffer-name 'kill)) 594 595 (define-obsolete-function-alias 'ledger-report-kill #'ledger-report-quit "2018-03-18") 596 597 (defun ledger-report-edit-reports () 598 "Edit the defined ledger reports." 599 (interactive) 600 (customize-variable 'ledger-reports)) 601 602 (defun ledger-report-edit-report () 603 "Edit the current report command in the mini buffer and re-run the report." 604 (interactive) 605 (setq ledger-report-cmd (ledger-report-read-command ledger-report-cmd)) 606 (ledger-report-redo)) 607 608 (define-obsolete-function-alias 'ledger-report-select-report #'ledger-report "ledger 4.0.0") 609 610 (defun ledger-report-read-new-name () 611 "Read the name for a new report from the minibuffer." 612 (let ((name "")) 613 (while (string-empty-p name) 614 (setq name (read-from-minibuffer "Report name: " nil nil nil 615 'ledger-report-name-prompt-history))) 616 name)) 617 618 (defun ledger-report-save () 619 "Save the current report command line as a named report." 620 (interactive) 621 (ledger-report-goto) 622 (when (string-empty-p ledger-report-name) 623 (setq ledger-report-name (ledger-report-read-new-name))) 624 625 (when-let ((existing-name (ledger-report-name-exists ledger-report-name))) 626 (cond ((y-or-n-p (format "Overwrite existing report named '%s'? " 627 ledger-report-name)) 628 (if (string-equal 629 ledger-report-cmd 630 (car (cdr (assq existing-name ledger-reports)))) 631 (message "Nothing to save. Current command is identical to existing saved one") 632 (setq ledger-reports 633 (assq-delete-all existing-name ledger-reports)) 634 (ledger-reports-add ledger-report-name ledger-report-cmd) 635 (ledger-reports-custom-save))) 636 (t 637 (setq ledger-report-name (ledger-report-read-new-name)) 638 (ledger-reports-add ledger-report-name ledger-report-cmd) 639 (ledger-reports-custom-save))))) 640 641 (defun ledger-report-previous-month () 642 "Rebuild report with transactions from the previous month." 643 (interactive) 644 (ledger-report--change-month -1)) 645 646 (defun ledger-report-next-month () 647 "Rebuild report with transactions from the next month." 648 (interactive) 649 (ledger-report--change-month 1)) 650 651 (defun ledger-report-toggle-default-commodity () 652 "Toggle exchange of reported amounts to `ledger-reconcile-default-commodity'." 653 (interactive) 654 (unless (derived-mode-p 'ledger-report-mode) 655 (user-error "Not a ledger report buffer")) 656 (save-match-data 657 (if (string-match 658 (concat (rx (or "--exchange" "-X") (1+ space)) 659 (regexp-quote ledger-reconcile-default-commodity)) 660 ledger-report-cmd) 661 (setq ledger-report-cmd (replace-match "" nil nil ledger-report-cmd)) 662 (setq ledger-report-cmd (concat ledger-report-cmd 663 " --exchange " ledger-reconcile-default-commodity)))) 664 (ledger-report-redo)) 665 666 (provide 'ledger-report) 667 668 ;;; ledger-report.el ends here