Made account formatting and auto complete compatible.

This commit is contained in:
Craig Earls 2013-03-07 15:28:09 -05:00
parent 27d27ecb6c
commit b475e569c4
4 changed files with 72 additions and 63 deletions

View file

@ -32,6 +32,7 @@
;;; Commentary:
;; Load up the ledger mode
(require 'esh-util)
(require 'esh-arg)
(require 'ldg-commodities)
(require 'ldg-complete)

View file

@ -36,11 +36,16 @@
"Options for controlling how Ledger-mode deals with postings and completion"
:group 'ledger)
(defcustom ledger-post-auto-adjust-amounts nil
"If non-nil, ."
(defcustom ledger-post-auto-adjust-postings nil
"If non-nil, adjust account and amount to columns set below"
:type 'boolean
:group 'ledger-post)
(defcustom ledger-post-account-alignment-column 4
"The column Ledger-mode attempts to align accounts to."
:type 'integer
:group 'ledger-post)
(defcustom ledger-post-amount-alignment-column 52
"The column Ledger-mode attempts to align amounts to."
:type 'integer
@ -123,25 +128,25 @@ PROMPT is a string to prompt with. CHOICES is a list of
(- (or (match-end 4)
(match-end 3)) (point))))
(defun ledger-align-amounts (&optional column)
(defun ledger-post-align-postings (&optional column)
"Align amounts and accounts in the current region.
This is done so that the last digit falls in COLUMN, which
defaults to 52. ledger-default-acct-transaction-indent positions
defaults to 52. ledger-post-account-column positions
the account"
(interactive "p")
(if (or (null column) (= column 1))
(setq column ledger-post-amount-alignment-column))
(save-excursion
;; Position the account
(if (not
(and (looking-at "[ \t]+\n")
(looking-back "[ \n]" (- (point) 2))))
(progn
(if (not (and (looking-at "[ \t]+\n")
(looking-back "[ \n]" (- (point) 2))))
(save-excursion
(beginning-of-line)
(set-mark (point))
(delete-horizontal-space)
(insert ledger-default-acct-transaction-indent))
(set-mark (point))
(delete-horizontal-space)
(insert (make-string ledger-post-account-alignment-column ? )))
(set-mark (point)))
(set-mark (point))
(goto-char (1+ (line-end-position)))
(let* ((mark-first (< (mark) (point)))
(begin (if mark-first (mark) (point)))
@ -153,7 +158,7 @@ the account"
(let ((col (current-column))
(target-col (- column offset))
adjust)
(setq adjust (- target-col col))
(setq adjust (- target-col col))
(if (< col target-col)
(insert (make-string (- target-col col) ? ))
(move-to-column target-col)
@ -164,13 +169,13 @@ the account"
(insert " ")))
(forward-line))))))
(defun ledger-post-align-amount ()
(defun ledger-post-align-posting ()
"Align the amounts in this posting."
(interactive)
(save-excursion
(set-mark (line-beginning-position))
(goto-char (1+ (line-end-position)))
(ledger-align-amounts)))
(ledger-post-align-postings)))
(defun ledger-post-maybe-align (beg end len)
"Align amounts only if point is in a posting.
@ -180,7 +185,7 @@ BEG, END, and LEN control how far it can align."
(when (<= end (line-end-position))
(goto-char (line-beginning-position))
(if (looking-at ledger-post-line-regexp)
(ledger-align-amounts)))))
(ledger-post-align-postings)))))
(defun ledger-post-edit-amount ()
"Call 'calc-mode' and push the amount in the posting to the top of stack."
@ -221,8 +226,7 @@ BEG, END, and LEN control how far it can align."
(defun ledger-post-setup ()
"Configure `ledger-mode' to auto-align postings."
(if ledger-post-auto-adjust-amounts
(add-hook 'after-change-functions 'ledger-post-maybe-align t t))
(add-hook 'after-change-functions 'ledger-post-maybe-align t t)
(add-hook 'after-save-hook #'(lambda () (setq ledger-post-current-list nil))))

View file

@ -70,20 +70,20 @@ reconcile-finish will mark all pending posting cleared."
(account ledger-acct)
(val nil))
(with-temp-buffer
;; note that in the line below, the --format option is
;; separated from the actual format string. emacs does not
;; split arguments like the shell does, so you need to
;; specify the individual fields in the command line.
(ledger-exec-ledger buffer (current-buffer)
; note that in the line below, the --format option is
; separated from the actual format string. emacs does not
; split arguments like the shell does, so you need to
; specify the individual fields in the command line.
"balance" "--limit" "cleared or pending" "--empty"
"--format" "%(display_total)" account)
(setq val
(setq val
(ledger-split-commodity-string
(buffer-substring-no-properties (point-min) (point-max)))))))
(defun ledger-display-balance ()
"Display the cleared-or-pending balnce and calculate the
target-delta of the account being reconciled."
"Display the cleared-or-pending balance.
And calculate the target-delta of the account being reconciled."
(interactive)
(let* ((pending (ledger-reconcile-get-cleared-or-pending-balance))
(target-delta (if ledger-target
@ -111,7 +111,7 @@ target-delta of the account being reconciled."
"Return a buffer from WHERE the transaction is."
(if (bufferp (car where))
(car where)
(error "ledger-reconcile-get-buffer: Buffer not set")))
(error "Function ledger-reconcile-get-buffer: Buffer not set")))
(defun ledger-reconcile-toggle ()
"Toggle the current transaction, and mark the recon window."

View file

@ -122,42 +122,48 @@ dropped."
;;this excursion toggles the posting status
(save-excursion
(goto-char (line-beginning-position))
(when (looking-at "[ \t]")
(skip-chars-forward " \t")
(let ((here (point))
(cur-status (ledger-state-from-char (char-after))))
(skip-chars-forward "*! ")
(let ((width (- (point) here)))
(when (> width 0)
(delete-region here (point))
(save-excursion
(if (search-forward " " (line-end-position) t)
(insert (make-string width ? ))))))
(let (inserted)
(if cur-status
(if (and style (eq style 'cleared))
(progn
(insert "* ")
(setq inserted 'cleared)))
(if (and style (eq style 'pending))
(progn
(insert "! ")
(setq inserted 'pending))
(progn
(insert "* ")
(setq inserted 'cleared))))
(if (and inserted
(re-search-forward "\\(\t\\| [ \t]\\)"
(line-end-position) t))
(cond
((looking-at "\t")
(delete-char 1))
((looking-at " [ \t]")
(delete-char 2))
((looking-at " ")
(delete-char 1))))
(setq new-status inserted)))))
(let ((has-align-hook (remove-hook
'after-change-functions
'ledger-post-maybe-align t)))
(goto-char (line-beginning-position))
(when (looking-at "[ \t]")
(skip-chars-forward " \t")
(let ((here (point))
(cur-status (ledger-state-from-char (char-after))))
(skip-chars-forward "*! ")
(let ((width (- (point) here)))
(when (> width 0)
(delete-region here (point))
(save-excursion
(if (search-forward " " (line-end-position) t)
(insert (make-string width ? ))))))
(let (inserted)
(if cur-status
(if (and style (eq style 'cleared))
(progn
(insert "* ")
(setq inserted 'cleared)))
(if (and style (eq style 'pending))
(progn
(insert "! ")
(setq inserted 'pending))
(progn
(insert "* ")
(setq inserted 'cleared))))
(if (and inserted
(re-search-forward "\\(\t\\| [ \t]\\)"
(line-end-position) t))
(cond
((looking-at "\t")
(delete-char 1))
((looking-at " [ \t]")
(delete-char 2))
((looking-at " ")
(delete-char 1))))
(setq new-status inserted))))
(if has-align-hook
(add-hook 'after-change-functions 'ledger-post-maybe-align t t))))
;; This excursion cleans up the entry so that it displays
;; minimally. This means that if all posts are cleared, remove
@ -254,6 +260,4 @@ dropped."
(provide 'ldg-state)
(provide 'ldg-state)
;;; ldg-state.el ends here