More armor plating on ledger-post-align-postings

This commit is contained in:
Craig Earls 2013-03-23 21:51:27 -07:00
parent 0bcef93e29
commit 53778317cb

View file

@ -138,48 +138,52 @@ Return the column of the beginning of the account"
"Align all accounts and amounts within region, if there is no "Align all accounts and amounts within region, if there is no
region alight the posting on the current line." region alight the posting on the current line."
(interactive) (interactive)
(save-excursion (let ((region-boundaries-verified nil)) (save-excursion
;; If there is no region set ;; If there is no region set
(when (or (not (mark)) (when (or (not (mark))
(= (point) (mark))) (= (point) (mark)))
(beginning-of-line) (beginning-of-line)
(set-mark (point)) (set-mark (point))
(goto-char (1+ (line-end-position)))) (goto-char (line-end-position))
(setq region-boundaries-verified t))
(let* ((mark-first (< (mark) (point))) (let* ((mark-first (< (mark) (point)))
(begin (if mark-first (mark) (point))) (begin (if mark-first (mark) (point)))
(end (if mark-first (point-marker) (mark-marker))) (end (if mark-first (point-marker) (mark-marker)))
acc-col amt-offset) acc-col amt-offset)
(goto-char end) (if (not region-boundaries-verified)
(end-of-line) (progn
(setq end (point-marker)) (goto-char end)
(goto-char begin) (end-of-line)
(beginning-of-line) (setq end (point-marker))
(setq begin (point-marker)) (goto-char begin)
(while (setq acc-col (ledger-next-account end)) (beginning-of-line)
;; Adjust account position if necessary (setq begin (point-marker)))
(let ((acc-adjust (- ledger-post-account-alignment-column acc-col))) (goto-char begin))
(if (/= acc-adjust 0) (while (setq acc-col (ledger-next-account end))
(if (> acc-adjust 0) ;; Adjust account position if necessary
(insert (make-string acc-adjust ? )) ;; Account too far left (let ((acc-adjust (- ledger-post-account-alignment-column acc-col)))
(if (looking-back " " (- (point) 3)) (if (/= acc-adjust 0)
(delete-char acc-adjust) (if (> acc-adjust 0)
(skip-chars-forward "^ \t") (insert (make-string acc-adjust ? )) ;; Account too far left
(delete-horizontal-space) (if (looking-back " " (- (point) 3))
(insert " "))))) (delete-char acc-adjust)
(when (setq amt-offset (ledger-next-amount end)) (skip-chars-forward "^ \t")
(let* ((amt-adjust (- ledger-post-amount-alignment-column (delete-horizontal-space)
amt-offset (insert " ")))))
(current-column)))) (when (setq amt-offset (ledger-next-amount end))
(if (/= amt-adjust 0) (let* ((amt-adjust (- ledger-post-amount-alignment-column
(if (> amt-adjust 0) amt-offset
(insert (make-string amt-adjust ? )) (current-column))))
(if (looking-back " ") (if (/= amt-adjust 0)
(delete-char amt-adjust) (if (> amt-adjust 0)
(skip-chars-forward "^ \t") (insert (make-string amt-adjust ? ))
(delete-horizontal-space) (if (looking-back " ")
(insert " ")))))) (delete-char amt-adjust)
(forward-line))))) (skip-chars-forward "^ \t")
(delete-horizontal-space)
(insert " "))))))
(forward-line))))))
(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.