]> git.eshelyaron.com Git - emacs.git/commitdiff
Subject: * lisp/progmodes/prolog.el: Improve handling of if/then/else
authorStefan Monnier <monnier@iro.umontreal.ca>
Mon, 21 Sep 2015 03:01:16 +0000 (23:01 -0400)
committerStefan Monnier <monnier@iro.umontreal.ca>
Mon, 21 Sep 2015 03:01:16 +0000 (23:01 -0400)
(prolog-smie-rules): Accomodate standard if/then/else special indentation.
(prolog-mode): Add . to electric-indent-chars.
(prolog-electric--if-then-else): Re-indent the line before adding space
after the new char (bug#21526).

lisp/progmodes/prolog.el
test/indent/prolog.prolog

index b36df213ce1a2f3b70e75593b96437dab6e3c990..24ac8d75960369375cc2c7f084b38542a85a827a 100644 (file)
@@ -925,12 +925,30 @@ This is really kludgy, and unneeded (i.e. obsolete) in Emacs>=24."
     (`(:after . ".") '(column . 0)) ;; To work around smie-closer-alist.
     ;; Allow indentation of if-then-else as:
     ;;    (   test
-    ;;     -> thenrule
-    ;;     ;  elserule
+    ;;    ->  thenrule
+    ;;      elserule
     ;;    )
     (`(:before . ,(or `"->" `";"))
-     (and (smie-rule-bolp) (smie-rule-parent-p "(") (smie-rule-parent 1)))
-    (`(:after . ,(or `":-" `"->" `"-->")) prolog-indent-width)))
+     (and (smie-rule-bolp) (smie-rule-parent-p "(") (smie-rule-parent 0)))
+    (`(:after . ,(or `"->" `"*->"))
+     ;; We distinguish
+     ;;
+     ;;     (a ->
+     ;;          b;
+     ;;      c)
+     ;; and
+     ;;     (    a ->
+     ;;          b
+     ;;     ;    c)
+     ;;
+     ;; based on the space between the open paren and the "a".
+     (unless (and (smie-rule-parent-p "(")
+                  (save-excursion
+                    (smie-indent-forward-token)
+                    (smie-backward-sexp 'halfsexp)
+                    (not (eq ?\( (char-before)))))
+       prolog-indent-width))
+    (`(:after . ,(or `":-" `"-->")) prolog-indent-width)))
 
 \f
 ;;-------------------------------------------------------------------
@@ -1121,6 +1139,9 @@ Commands:
   (dolist (ar prolog-align-rules) (add-to-list 'align-rules-list ar))
   (add-hook 'post-self-insert-hook #'prolog-post-self-insert nil t)
   ;; `imenu' entry moved to the appropriate hook for consistency.
+  (when prolog-electric-dot-flag
+    (setq-local electric-indent-chars
+                (cons ?\. electric-indent-chars)))
 
   ;; Load SICStus debugger if suitable
   (if (and (eq prolog-system 'sicstus)
@@ -2078,6 +2099,7 @@ whitespace characters, parentheses, or then/else branches."
   (when prolog-electric-if-then-else-flag
     (save-excursion
       (let ((regexp (concat "(\\|" prolog-left-indent-regexp))
+            (pos (point))
             level)
         (beginning-of-line)
         (skip-chars-forward " \t")
@@ -2087,6 +2109,9 @@ whitespace characters, parentheses, or then/else branches."
         ;;             prolog-paren-indent))
 
         ;; work on all subsequent "->", "(", ";"
+        (and (looking-at regexp)
+             (= pos (match-end 0))
+             (indent-according-to-mode))
         (while (looking-at regexp)
           (goto-char (match-end 0))
           (setq level (+ (prolog-find-unmatched-paren) prolog-paren-indent))
index 5b5d272c579721f2a0d6ab3c1862473d98980d01..ca4d2c91d3270bf01a444099b32540faf56f5011 100644 (file)
@@ -1,5 +1,27 @@
 %% -*- mode: prolog; coding: utf-8; fill-column: 78 -*-
 
+%% bug#21526
+test1 :-
+    (   a ->
+        b
+    ;   c
+    ).
+
+test2 :-
+    (    a
+    ->   b1,
+         b2
+    ;    c1,
+         c2
+    )
+
+test3 :-
+    (   a,
+        b
+    ;   c
+    ).
+
+
 %% Testing correct tokenizing.
 foo(X) :- 0'= = X.
 foo(X) :- 8'234 = X.
@@ -50,9 +72,9 @@ subst(X, V, FV, lambda(Y, Ti, Bi), lambda(Y1, To, Bo)) :-
      %% Perform substitution on the body.
      subst(X, V, FV, Bi1, Bo)),
     (  X = Y
-     %% If X is equal to Y, X is shadowed, so no subst can take place.
-     ->        Y1 = Y, Bo = Bi
-     ; (member((Y, _), FV)
+    %% If X is equal to Y, X is shadowed, so no subst can take place.
+    -> Y1 = Y, Bo = Bi
+     (member((Y, _), FV)
         %% If Y appears in FV, it can appear in V, so we need to
         %% rename it to avoid name capture.
         -> new_atom(Y, Y1),