Skip to content

Commit

Permalink
New 'ledger-indent-region' function
Browse files Browse the repository at this point in the history
* 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 ledger#197
  • Loading branch information
jabranham committed Sep 24, 2019
1 parent 47a186e commit a87e016
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ledger-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,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))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.ledger\\'" . ledger-mode))
Expand Down
13 changes: 13 additions & 0 deletions ledger-post.el
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ Looks only as far as END, if supplied, otherwise `point-max'."
(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 (looking-at-p "[[:space:]]+")
(cycle-spacing 0))))

(defun ledger-post-align-dwim ()
"Align all the posting of the current xact or the current region.
Expand Down
16 changes: 16 additions & 0 deletions test/post-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -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-for-tab-command)
(should (string= (buffer-substring-no-properties (point-min) (point-max))
"2019/08/13 Test
this 8000 GBP
out")))))


(provide 'post-test)

Expand Down

0 comments on commit a87e016

Please sign in to comment.