Fix bug 916 along amount in region

This commit is contained in:
Craig Earls 2013-03-22 20:56:19 -07:00
parent 75ba85ff8e
commit 0d9250dbe4
3 changed files with 13 additions and 58 deletions

View file

@ -120,6 +120,7 @@
(define-key map [sort-start] '(menu-item "Mark Sort Beginning" ledger-sort-insert-start-mark)) (define-key map [sort-start] '(menu-item "Mark Sort Beginning" ledger-sort-insert-start-mark))
(define-key map [sort-buff] '(menu-item "Sort Buffer" ledger-sort-buffer)) (define-key map [sort-buff] '(menu-item "Sort Buffer" ledger-sort-buffer))
(define-key map [sort-reg] '(menu-item "Sort Region" ledger-sort-region :enable mark-active)) (define-key map [sort-reg] '(menu-item "Sort Region" ledger-sort-region :enable mark-active))
(define-key map [align-reg] '(menu-item "Align Region" ledger-post-align-region :enable mark-active))
(define-key map [sep2] '(menu-item "--")) (define-key map [sep2] '(menu-item "--"))
(define-key map [copy-xact] '(menu-item "Copy Trans at Point" ledger-copy-transaction)) (define-key map [copy-xact] '(menu-item "Copy Trans at Point" ledger-copy-transaction))
(define-key map [toggle-post] '(menu-item "Toggle Current Posting" ledger-toggle-current)) (define-key map [toggle-post] '(menu-item "Toggle Current Posting" ledger-toggle-current))

View file

@ -123,8 +123,8 @@ PROMPT is a string to prompt with. CHOICES is a list of
(- (or (match-end 4) (- (or (match-end 4)
(match-end 3)) (point)))) (match-end 3)) (point))))
(defun ledger-post-align-postings (&optional column) (defun ledger-post-align-posting (&optional column)
"Align amounts and accounts in the current region. "Align amounts and accounts in the current posting.
This is done so that the last digit falls in COLUMN, which This is done so that the last digit falls in COLUMN, which
defaults to 52. ledger-post-account-column positions defaults to 52. ledger-post-account-column positions
the account" the account"
@ -165,62 +165,16 @@ the account"
(insert " "))) (insert " ")))
(forward-line)))))) (forward-line))))))
(defun ledger-post-align-posting ()
"Align the amounts in this posting." (defun ledger-post-align-region (beg end)
(interactive) (interactive "r")
(save-excursion (save-excursion
(set-mark (line-beginning-position)) (goto-char beg)
(goto-char (1+ (line-end-position))) (backward-paragraph) ;; make sure we are at the beginning of an xact
(ledger-post-align-postings))) (while (< (point) end)
(ledger-post-align-posting)
(forward-line))))
;;
;; This is the orignal ledger align amount code it does not attempt to format accounts
;;
(defun ledger-align-amounts (&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
the account"
(interactive "p")
(if (or (null column) (= column 1))
(setq column ledger-post-amount-alignment-column))
(save-excursion
;; Position the account
;; (beginning-of-line)
(set-mark (point))
;; (delete-horizontal-space)
;; (insert ledger-default-acct-transaction-indent)
(goto-char (1+ (line-end-position)))
(let* ((mark-first (< (mark) (point)))
(begin (if mark-first (mark) (point)))
(end (if mark-first (point-marker) (mark-marker)))
offset)
;; Position the amount
(goto-char begin)
(while (setq offset (ledger-next-amount end))
(let ((col (current-column))
(target-col (- column offset))
adjust)
(setq adjust (- target-col col))
(if (< col target-col)
(insert (make-string (- target-col col) ? ))
(move-to-column target-col)
(if (looking-back " ")
(delete-char (- col target-col))
(skip-chars-forward "^ \t")
(delete-horizontal-space)
(insert " ")))
(forward-line))))))
(defun ledger-post-align-amount ()
"Align the amounts in this posting."
(interactive)
(save-excursion
(set-mark (line-beginning-position))
(goto-char (1+ (line-end-position)))
(ledger-align-amounts)))
(defun ledger-post-maybe-align (beg end len) (defun ledger-post-maybe-align (beg end len)
"Align amounts only if point is in a posting. "Align amounts only if point is in a posting.
@ -231,7 +185,7 @@ BEG, END, and LEN control how far it can align."
(when (<= end (line-end-position)) (when (<= end (line-end-position))
(goto-char (line-beginning-position)) (goto-char (line-beginning-position))
(if (looking-at ledger-post-line-regexp) (if (looking-at ledger-post-line-regexp)
(ledger-post-align-postings)))))) (ledger-post-align-posting))))))
(defun ledger-post-edit-amount () (defun ledger-post-edit-amount ()
"Call 'calc-mode' and push the amount in the posting to the top of stack." "Call 'calc-mode' and push the amount in the posting to the top of stack."

View file

@ -76,7 +76,7 @@
(new-end end)) (new-end end))
(save-excursion (save-excursion
(save-restriction (save-restriction
(goto-char beg) (goto-char beg)
(ledger-next-record-function) ;; make sure point is at the (ledger-next-record-function) ;; make sure point is at the
;; beginning of a xact ;; beginning of a xact
(setq new-beg (point)) (setq new-beg (point))