diff options
author | Craig Earls <enderw88@gmail.com> | 2013-04-18 18:30:27 -0700 |
---|---|---|
committer | Craig Earls <enderw88@gmail.com> | 2013-04-18 18:30:27 -0700 |
commit | 1761e6a447c6514a4260746e6a78e400be5c0e4e (patch) | |
tree | 4913375e35653f3d374c6f061c7c1c91b45346af /lisp/ldg-sort.el | |
parent | 769a73c33bbfcd3cd5d164f02c0fa86af228a828 (diff) | |
download | fork-ledger-1761e6a447c6514a4260746e6a78e400be5c0e4e.tar.gz fork-ledger-1761e6a447c6514a4260746e6a78e400be5c0e4e.tar.bz2 fork-ledger-1761e6a447c6514a4260746e6a78e400be5c0e4e.zip |
Sort buffer now attempts to keep point at the same xact.
Diffstat (limited to 'lisp/ldg-sort.el')
-rw-r--r-- | lisp/ldg-sort.el | 75 |
1 files changed, 44 insertions, 31 deletions
diff --git a/lisp/ldg-sort.el b/lisp/ldg-sort.el index a50cd1cc..06efd348 100644 --- a/lisp/ldg-sort.el +++ b/lisp/ldg-sort.el @@ -49,7 +49,7 @@ (save-excursion (goto-char (point-min)) (if (ledger-sort-find-start) - (delete-region (match-beginning 0) (match-end 0)))) + (delete-region (match-beginning 0) (match-end 0)))) (beginning-of-line) (insert "\n; Ledger-mode: Start sort\n\n")) @@ -58,7 +58,7 @@ (save-excursion (goto-char (point-min)) (if (ledger-sort-find-end) - (delete-region (match-beginning 0) (match-end 0)))) + (delete-region (match-beginning 0) (match-end 0)))) (beginning-of-line) (insert "\n; Ledger-mode: End sort\n\n")) @@ -69,44 +69,57 @@ (defun ledger-sort-region (beg end) "Sort the region from BEG to END in chronological order." (interactive "r") ;; load beg and end from point and mark - ;; automagically + ;; automagically (let ((new-beg beg) - (new-end end)) - (setq inhibit-modification-hooks t) + (new-end end) + point-delta + (bounds (ledger-find-xact-extents (point))) + target-xact) + + (setq point-delta (- (point) (car bounds))) + (setq target-xact (buffer-substring (car bounds) (cadr bounds))) + (setq inhibit-modification-hooks t) (save-excursion (save-restriction - (goto-char beg) - (ledger-next-record-function) ;; make sure point is at the - ;; beginning of a xact - (setq new-beg (point)) - (goto-char end) - (ledger-next-record-function) ;; make sure end of region is at - ;; the beginning of next record - ;; after the region - (setq new-end (point)) - (narrow-to-region new-beg new-end) - (goto-char new-beg) - - (let ((inhibit-field-text-motion t)) - (sort-subr - nil - 'ledger-next-record-function - 'ledger-end-record-function - 'ledger-sort-startkey)))) + (goto-char beg) + (ledger-next-record-function) ;; make sure point is at the + ;; beginning of a xact + (setq new-beg (point)) + (goto-char end) + (ledger-next-record-function) ;; make sure end of region is at + ;; the beginning of next record + ;; after the region + (setq new-end (point)) + (narrow-to-region new-beg new-end) + (goto-char new-beg) + + (let ((inhibit-field-text-motion t)) + (sort-subr + nil + 'ledger-next-record-function + 'ledger-end-record-function + 'ledger-sort-startkey)))) + + (goto-char beg) + (re-search-forward (regexp-quote target-xact)) + (goto-char (+ (match-beginning 0) point-delta)) (setq inhibit-modification-hooks nil))) (defun ledger-sort-buffer () "Sort the entire buffer." (interactive) - (goto-char (point-min)) - (let ((sort-start (ledger-sort-find-start)) - (sort-end (ledger-sort-find-end))) + (let (sort-start + sort-end) + (save-excursion + (goto-char (point-min)) + (setq sort-start (ledger-sort-find-start) + sort-end (ledger-sort-find-end))) (ledger-sort-region (if sort-start - sort-start - (point-min)) - (if sort-end - sort-end - (point-max))))) + sort-start + (point-min)) + (if sort-end + sort-end + (point-max))))) (provide 'ldg-sort) |