From 1e12ddf3fabfe359afd69e66dd944a3d5ae32aad Mon Sep 17 00:00:00 2001 From: Alex Branham Date: Tue, 24 Sep 2019 14:55:58 -0500 Subject: [PATCH] New 'ledger-indent-region' function * ledger-mode.el (ledger-mode): Set indent-region-function. * ledger-post.el (ledger-indent-region): New function that indents the region by calling ledger-indent-line for each line. Closes bug #197 Signed-off-by: J. Dekker --- ledger-mode.el | 2 +- ledger-post.el | 14 ++++++++++++++ test/post-test.el | 18 +++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/ledger-mode.el b/ledger-mode.el index 36c6b173e..d15db2c28 100644 --- a/ledger-mode.el +++ b/ledger-mode.el @@ -331,7 +331,7 @@ With a prefix argument, remove the effective date." (ledger-init-load-init-file) (setq-local comment-start ";") (setq-local indent-line-function #'ledger-indent-line) - (setq-local indent-region-function 'ledger-post-align-postings) + (setq-local indent-region-function #'ledger-indent-region) (setq-local beginning-of-defun-function #'ledger-navigate-beginning-of-xact) (setq-local end-of-defun-function #'ledger-navigate-end-of-xact)) diff --git a/ledger-post.el b/ledger-post.el index 474b233cf..95673eb7d 100644 --- a/ledger-post.el +++ b/ledger-post.el @@ -147,6 +147,20 @@ The current region is used, or, if no region, the current line." (when ledger-post-auto-align (ledger-post-align-postings (line-beginning-position) (line-end-position)))) +(defun ledger-indent-region (beg end) + "Indent the region from BEG to END. +This works by calling `ledger-indent-line' for each line." + (save-excursion + (goto-char beg) + (beginning-of-line) + (ledger-indent-line) + (while (< (point) end) + (forward-line) + (ledger-indent-line)) + (when (progn (beginning-of-line) + (looking-at-p "^[[:space:]]*$")) + (delete-region (point) (line-end-position))))) + (defun ledger-post-align-dwim () "Align all the posting of the current xact or the current region. diff --git a/test/post-test.el b/test/post-test.el index 1d913fa36..bc5616108 100644 --- a/test/post-test.el +++ b/test/post-test.el @@ -258,7 +258,7 @@ http://bugs.ledger-cli.org/show_bug.cgi?id=924" " (indent-region (point-min) (point-max)) (should - (equal (buffer-string) + (equal (buffer-substring-no-properties (point-min) (point-max)) "1994/01/10 * Mother Actif:Courant:BnpCc 500,00 F ; Étrennes Revenu:Autre:CadeauReçu @@ -453,6 +453,22 @@ http://bugs.ledger-cli.org/show_bug.cgi?id=946" Assets:Bar " )))) +(ert-deftest ledger-post/test-197 () + "Regress test for Bug #197" + :tags '(post regress) + (ledger-tests-with-temp-file + "2019/08/13 Test +this 8000 GBP +out" + (let ((ledger-post-auto-align nil)) + (set-mark-command nil) + (goto-char (point-max)) + (call-interactively #'indent-region) + (should (string= (buffer-substring-no-properties (point-min) (point-max)) + "2019/08/13 Test + this 8000 GBP + out"))))) + (provide 'post-test)