Subject: bug#21526: 24.5; prolog-mode: broken indentation for if-then-else construct

From: Stefan Monnier <[email-masked]>
Date: Sun, 20 Sep 2015 14:04:01 -0400

>    (setq prolog-indent-width 8
>          prolog-electric-tab-flag nil
>          prolog-electric-if-then-else-flag t
>          prolog-paren-indent 4
>          prolog-electric-dot-flag nil
>          prolog-paren-indent-p t
>          prolog-char-quote-workaround nil)

Thanks.  We indeed have some problems with the new indentation code, and
I hope you can help me figure out what it should do.

Could you give me some examples of what prolog-paren-indent-p
should do?  Currently it's simply unused :-(

>    t e s t SPC : - RET ( a SPC - > RET b RET ; c RET ) .

> then I get the following indentation (shown in untabified form):

> test :-
>         (   a ->
>                     b
>          ;c
>                   ).

I see a few different problems:
- The indentation of ) is wrong simply because nothing caused it to be
  reindented (hitting TAB or RET at the end brings the close paren to
  the right spot).  I guess "." should cause re-indentation.  Adding it
  to electric-indent-chars should do the trick.

- there are two desired indentations for "b".  IIUC You want

    (a ->
     b1,
     b2
    ;c1,
     c2)

  whereas the current code tries to accommodate

    (a ->
         b1,
         b2;
     c1,
     c2)

- the current code also tries to accommodate

    (    a
     ->
         b1,
         b2
     ;   c1,
         c2)

  so I'm not sure how to combine this with your use case.  E.g. would
  you prefer
   
    (    a
    ->
         b1,
         b2
    ;    c1,
         c2)

  or do you only want the ; at paren-level in the case where -> was not
  at the beginning of the line?

- After hitting "b RET" you get indented to a bogus column.  This is
  because SMIE thinks that

       predicate
           (arg2, arg2)

  is a possibility, so after "b RET" it thinks you might be about to
  enter a list of arguments to "b".  This is a general problem with
  SMIE's handling of empty lines (where it's often valid but unlikely),
  and even more so here in Prolog where such things aren't even valid.

The patch below fixes some of those problems.  After this patch, you
should hopefully see something more like

    (   a ->
            b
     ;  c
    ).

in your buffer.

        Stefan

diff --git a/lisp/progmodes/prolog.el b/lisp/progmodes/prolog.el
index b36df21..2f4c03e 100644
--- a/lisp/progmodes/prolog.el
+++ b/lisp/progmodes/prolog.el
@@ -1121,6 +1121,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 +2081,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 +2091,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))