[emacs] Use markers instead of raw positions to make cleaning work better

Sometimes cleaning a buffer would leave the end of the buffer uncleaned,
because the end position would be moved along the way. This commit makes
the corresponding code use markers instead, which track the changes in
the buffer, and therefore ensure that the entire buffer is acted upon.
This commit is contained in:
Steve Purcell 2014-12-09 15:11:44 +00:00
parent c8dd5d4a0a
commit b623306591
2 changed files with 53 additions and 57 deletions

View file

@ -228,17 +228,20 @@ With a prefix argument, remove the effective date."
(defun ledger-mode-clean-buffer () (defun ledger-mode-clean-buffer ()
"Indent, remove multiple line feeds and sort the buffer." "Indent, remove multiple line feeds and sort the buffer."
(interactive) (interactive)
(ledger-navigate-beginning-of-xact) (let ((start (point-min-marker))
(beginning-of-line) (end (point-max-marker)))
(let ((target (buffer-substring (point) (progn (goto-char start)
(end-of-line) (ledger-navigate-beginning-of-xact)
(point))))) (beginning-of-line)
(untabify (point-min) (point-max)) (let ((target (buffer-substring (point) (progn
(ledger-sort-buffer) (end-of-line)
(ledger-post-align-postings (point-min) (point-max)) (point)))))
(ledger-mode-remove-extra-lines) (untabify start end)
(goto-char (point-min)) (ledger-sort-buffer)
(search-forward target))) (ledger-post-align-postings start end)
(ledger-mode-remove-extra-lines)
(goto-char start)
(search-forward target))))
(defvar ledger-mode-map (defvar ledger-mode-map
(let ((map (make-sparse-keymap))) (let ((map (make-sparse-keymap)))

View file

@ -110,55 +110,48 @@ at beginning of account"
(set-mark (point))) (set-mark (point)))
(let ((inhibit-modification-hooks t) (let ((inhibit-modification-hooks t)
(mark-first (< (mark) (point))) (mark-first (< (mark) (point)))
acct-start-column acct-end-column acct-adjust amt-width amt-adjust acct-start-column acct-end-column acct-adjust amt-width amt-adjust
(lines-left 1)) (lines-left 1))
(unless beg (setq beg (if mark-first (mark) (point)))) (unless beg (setq beg (if mark-first (mark) (point))))
(unless end (setq end (if mark-first (mark) (point)))) (unless end (setq end (if mark-first (mark) (point))))
;; Condition point and mark to the beginning and end of lines
(goto-char end)
(setq end (line-end-position))
(goto-char beg)
(goto-char
(setq beg
(line-beginning-position)))
(untabify beg end) ;; Extend region to whole lines
(let ((start-marker (set-marker (make-marker) (save-excursion
(goto-char beg)
(line-beginning-position))))
(end-marker (set-marker (make-marker) (save-excursion
(goto-char end)
(line-end-position)))))
(untabify start-marker end-marker)
(goto-char start-marker)
;; if untabify actually changed anything, then our begin and end are not correct. ;; This is the guts of the alignment loop
(goto-char end) (while (and (or (setq acct-start-column (ledger-next-account (line-end-position)))
(setq end (line-end-position)) lines-left)
(goto-char beg) (< (point) end-marker))
(goto-char (when acct-start-column
(setq beg (setq acct-end-column (save-excursion
(line-beginning-position))) (goto-char (match-end 2))
(current-column)))
;; This is the guts of the alignment loop (when (/= (setq acct-adjust (- ledger-post-account-alignment-column acct-start-column)) 0)
(while (and (or (setq acct-start-column (ledger-next-account (line-end-position))) (setq acct-end-column (+ acct-end-column acct-adjust)) ;;adjust the account ending column
lines-left) (if (> acct-adjust 0)
(< (point) end)) (insert (make-string acct-adjust ? ))
(when acct-start-column (delete-char acct-adjust)))
(setq acct-end-column (save-excursion (when (setq amt-width (ledger-next-amount (line-end-position)))
(goto-char (match-end 2)) (if (/= 0 (setq amt-adjust (- (if (> (- ledger-post-amount-alignment-column amt-width)
(current-column))) (+ 2 acct-end-column))
(when (/= (setq acct-adjust (- ledger-post-account-alignment-column acct-start-column)) 0) ledger-post-amount-alignment-column ;;we have room
(setq acct-end-column (+ acct-end-column acct-adjust)) ;;adjust the account ending column (+ acct-end-column 2 amt-width))
(if (> acct-adjust 0) amt-width
(insert (make-string acct-adjust ? )) (current-column))))
(delete-char acct-adjust))) (if (> amt-adjust 0)
(when (setq amt-width (ledger-next-amount (line-end-position))) (insert (make-string amt-adjust ? ))
(if (/= 0 (setq amt-adjust (- (if (> (- ledger-post-amount-alignment-column amt-width) (delete-char amt-adjust)))))
(+ 2 acct-end-column)) (forward-line)
ledger-post-amount-alignment-column ;;we have room (setq lines-left (not (eobp)))))
(+ acct-end-column 2 amt-width))
amt-width
(current-column))))
(if (> amt-adjust 0)
(insert (make-string amt-adjust ? ))
(delete-char amt-adjust)))))
(forward-line)
(setq lines-left (not (eobp))))
(setq inhibit-modification-hooks nil)))) (setq inhibit-modification-hooks nil))))
(defun ledger-post-edit-amount () (defun ledger-post-edit-amount ()