ledger-post.el (7857B)
1 ;;; ledger-post.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 ;; Utility functions for dealing with postings. 25 26 (require 'ledger-regex) 27 (require 'ledger-navigate) 28 29 (declare-function calc-renumber-stack "calc" ()) 30 (declare-function ledger-string-to-number "ledger-commodities" (str &optional decimal-comma)) 31 32 ;;; Code: 33 34 (defgroup ledger-post nil 35 "Options for controlling how Ledger-mode deals with postings and completion" 36 :group 'ledger) 37 38 (defcustom ledger-post-account-alignment-column 4 39 "The column Ledger-mode attempts to align accounts to." 40 :type 'integer 41 :group 'ledger-post 42 :safe 'integerp) 43 44 (defcustom ledger-post-amount-alignment-column 52 45 "The column Ledger-mode attempts to align amounts to." 46 :type 'integer 47 :group 'ledger-post 48 :safe 'integerp) 49 50 (defcustom ledger-post-amount-alignment-at :end 51 "Position at which the amount is aligned. 52 53 Can be :end to align on the last number of the amount (can be 54 followed by unaligned commodity) or :decimal to align at the 55 decimal separator." 56 :type '(radio (const :tag "align at the end of amount" :end) 57 (const :tag "align at the decimal separator" :decimal)) 58 :group 'ledger-post 59 :safe (lambda (x) (memq x '(:end :decimal)))) 60 61 (defcustom ledger-post-auto-align t 62 "When non-nil, realign post amounts when indenting or completing." 63 :type 'boolean 64 :group 'ledger-post 65 :package-version '(ledger-mode . "4.0.0") 66 :safe 'booleanp) 67 68 (defun ledger-next-amount (&optional end) 69 "Move point to the next amount, as long as it is not past END. 70 Return the width of the amount field as an integer and leave 71 point at beginning of the commodity." 72 ;;(beginning-of-line) 73 (let ((case-fold-search nil)) 74 (when (re-search-forward ledger-amount-regex end t) 75 (goto-char (match-beginning 0)) 76 (skip-syntax-forward " ") 77 (cond 78 ((eq ledger-post-amount-alignment-at :end) 79 (- (or (match-end 4) (match-end 3)) (point))) 80 ((eq ledger-post-amount-alignment-at :decimal) 81 (- (match-end 3) (point))))))) 82 83 (defun ledger-next-account (&optional end) 84 "Move to the beginning of the posting, or status marker. 85 Return the column of the beginning of the account and leave point 86 at beginning of account. 87 Looks only as far as END, if supplied, otherwise `point-max'." 88 (let ((end (or end (point-max)))) 89 (if (> end (point)) 90 (when (re-search-forward ledger-account-any-status-regex (1+ end) t) 91 ;; the 1+ is to make sure we can catch the newline 92 (if (match-beginning 1) 93 (goto-char (match-beginning 1)) 94 (goto-char (match-beginning 2))) 95 (current-column))))) 96 97 (defun ledger-post-align-xact (pos) 98 "Align all the posting in the xact at POS." 99 (interactive "d") 100 (let ((bounds (ledger-navigate-find-xact-extents pos))) 101 (ledger-post-align-postings (car bounds) (cadr bounds)))) 102 103 (defun ledger-post-align-postings (beg end) 104 "Align all accounts and amounts between BEG and END. 105 The current region is used, or, if no region, the current line." 106 (interactive "r") 107 (save-match-data 108 (save-excursion 109 (let ((inhibit-modification-hooks t) 110 ;; Extend region to whole lines 111 (beg (save-excursion (goto-char beg) (line-beginning-position))) 112 (end (save-excursion (goto-char end) (move-end-of-line 1) (point-marker)))) 113 (untabify beg end) 114 (goto-char beg) 115 (while (< (point) end) 116 (when (looking-at-p " ") 117 ;; fix spaces at beginning of line: 118 (just-one-space ledger-post-account-alignment-column) 119 ;; fix spaces before amount if any: 120 (when (re-search-forward "\t\\| \\| \t" (line-end-position) t) 121 (goto-char (match-beginning 0)) 122 (let ((acct-end-column (current-column)) 123 (amt-width (ledger-next-amount (line-end-position))) 124 amt-adjust) 125 (when amt-width 126 (if (/= 0 (setq amt-adjust (- (if (> (- ledger-post-amount-alignment-column amt-width) 127 (+ 2 acct-end-column)) 128 ledger-post-amount-alignment-column ;;we have room 129 (+ acct-end-column 2 amt-width)) 130 amt-width 131 (current-column)))) 132 (if (> amt-adjust 0) 133 (insert (make-string amt-adjust ? )) 134 (delete-char amt-adjust))))))) 135 (forward-line 1)))))) 136 137 (defun ledger-indent-line () 138 "Indent the current line." 139 ;; Ensure indent if the previous line was indented 140 (let ((indent-level (save-excursion (if (and (zerop (forward-line -1)) 141 (memq (ledger-thing-at-point) '(transaction posting))) 142 ledger-post-account-alignment-column 143 0)))) 144 (unless (= (current-indentation) indent-level) 145 (back-to-indentation) 146 (delete-horizontal-space t) 147 (indent-to indent-level))) 148 (when ledger-post-auto-align 149 (ledger-post-align-postings (line-beginning-position) (line-end-position)))) 150 151 (defun ledger-post-align-dwim () 152 "Align all the posting of the current xact or the current region. 153 154 If the point is in a comment, fill the comment paragraph as 155 regular text." 156 (interactive) 157 (cond 158 ((nth 4 (syntax-ppss)) 159 (call-interactively 'ledger-post-align-postings) 160 (fill-paragraph)) 161 ((use-region-p) (call-interactively 'ledger-post-align-postings)) 162 (t (call-interactively 'ledger-post-align-xact)))) 163 164 (defun ledger-post-edit-amount () 165 "Call `calc' and push the amount in the posting to the top of stack, if any. 166 167 In the calc buffer, press y to use the top value in the stack as 168 the amount and return to ledger." 169 (interactive) 170 (beginning-of-line) 171 (when (re-search-forward ledger-post-line-regexp (line-end-position) t) 172 (goto-char (match-end ledger-regex-post-line-group-account)) ;; go to the end of the account 173 ;; determine if there is an amount to edit 174 (if (re-search-forward ledger-amount-regexp (line-end-position) t) 175 (let ((val-string (match-string 0))) 176 (goto-char (match-beginning 0)) 177 (delete-region (match-beginning 0) (match-end 0)) 178 (push-mark (point) 'nomsg) 179 (calc) 180 ;; edit the amount, first removing thousands separators and converting 181 ;; decimal commas to calc's input format 182 (calc-eval (number-to-string (ledger-string-to-number val-string)) 'push) 183 (calc-renumber-stack)) 184 ;; make sure there are two spaces after the account name and go to calc 185 (if (search-backward " " (- (point) 3) t) 186 (end-of-line) 187 (insert " ")) 188 (push-mark (point) 'nomsg) 189 (calc)))) 190 191 (provide 'ledger-post) 192 193 194 195 ;;; ledger-post.el ends here