From: Eshel Yaron Date: Fri, 9 Dec 2022 15:10:02 +0000 (+0200) Subject: * (sweeprolog-align-spaces): extend to also work in comments X-Git-Tag: V9.1.1-sweep-0.9.5~4 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=201a54fe70af84d3c6915f0d9cdb2ce8011b7083;p=dict.git * (sweeprolog-align-spaces): extend to also work in comments --- diff --git a/sweeprolog-tests.el b/sweeprolog-tests.el index 43abff1..eb08bcc 100644 --- a/sweeprolog-tests.el +++ b/sweeprolog-tests.el @@ -1020,6 +1020,21 @@ scasp_and_show(Q, Model, Tree) :- (sweeprolog-end-of-top-term) (should (= (point) 252)))) +(ert-deftest align-spacs-in-line-comment () + "Test using `sweeprolog-align-spaces' in a line comment." + (with-temp-buffer + (sweeprolog-mode) + (insert " +%! foo is det. +% +%") + (sweeprolog-align-spaces) + (should (string= (buffer-string) + " +%! foo is det. +% +% ")))) + (ert-deftest electric-layout () "Test `sweeprolog-electric-layout-mode'." (with-temp-buffer @@ -1104,7 +1119,8 @@ foo :- (with-temp-buffer (sweeprolog-mode) (insert given) - (indent-region-line-by-line (point-min) (point-max)) + (let ((inhibit-message t)) + (indent-region-line-by-line (point-min) (point-max))) (should (string= (buffer-substring-no-properties (point-min) (point-max)) expected)))) diff --git a/sweeprolog.el b/sweeprolog.el index 742b42a..2161dc5 100644 --- a/sweeprolog.el +++ b/sweeprolog.el @@ -3457,14 +3457,28 @@ non-exported predicates defined in the current buffer." (user-error "No export list found")))))) (defun sweeprolog-align-spaces (&optional _) - "Adjust in-line whitespace between the previous next Prolog tokens. + "Adjust in-line spaces between the previous and next Prolog tokens. This command ensures that the next token starts at a column distanced from the beginning of the previous by a multiple of four columns, which accommodates the convetional alignment for -if-then-else constructs in SWI-Prolog." +if-then-else constructs and other common layouts in SWI-Prolog." (interactive "" sweeprolog-mode) (let ((bol (line-beginning-position))) + (if (nth 4 (syntax-ppss)) + (combine-after-change-calls + (delete-horizontal-space) + (let* ((lend (point)) + (lbeg (save-excursion + (while (and (< bol (point)) + (not (= (char-syntax (char-before)) + ? ))) + (forward-char -1)) + (point))) + (num (- 4 (% (- lend lbeg) 4)))) + (insert (make-string (if (< 0 num) + num + 4) ? ))))) (pcase (sweeprolog-last-token-boundaries) (`(,_ ,lbeg ,lend) (when (<= bol lend)