all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Markus Triska <triska@metalevel.at>
To: 21526@debbugs.gnu.org
Subject: bug#21526: 24.5; prolog-mode: broken indentation for if-then-else construct
Date: Sun, 20 Sep 2015 21:33:00 +0200	[thread overview]
Message-ID: <m2vbb47uoj.fsf@metalevel.at> (raw)
In-Reply-To: <m2lhc1dz0c.fsf@metalevel.at>


Stefan Monnier <monnier@iro.umontreal.ca> writes:

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

Thank you for looking into this! I will help in every way I can to
achieve the excellent indentation of Stefan Bruda's original version.

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

Basically, when I set prolog-paren-indent-p to t (and the other settings
I posted), then, after I have already typed a Prolog clause like:

   test :-
           HERE

(throughout the following, "HERE" is a placeholder that only denotes the
location of point, with point at the "H"), and then I press "(", then I
expect to get:

   test :-
           (   HERE

with point again being at the "H". In other words, the electric
parenthesis indents `prolog-paren-indent', which is typically 4. This
way, you spare users having to enter spaces manually to achieve the
typical indentation of the if-then-else construct.

>> 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.

In Stefan Bruda's original version, after I typed:

   test :-
           (   a ->
               b,
               cHERE

with point again at the "H", and then I press RET, I get:

   test :-
           (   a ->
               b,
               c
           HERE

That is, notice in particular that this new line is NOT indented the
same amount as the immediately preceding Prolog goals. There is a clear
reason for this: The immediately preceding Prolog goal, namely c, is
NOT followed by a comma. This means that where HERE appears, only two
things can realistically follow:

   1) ";", denoting a further disjunct
   2) ")", the closing parenthesis for the whole construct

For example, the preceding block can either end with:

   test :-
           (   a ->
               b,
               c
           ;   HERE

(notice the next indentation), or with:

   test :-
           (   a ->
               b,
               c
           )

after which the whole construct is finished. This is exactly the
behaviour users expect from a good indentation engine for Prolog, like
PceEmacs that ships with SWI-Prolog, and also Stefan Bruda's prolog.el.

To (unexpectedly) first indent the block and then only re-indent it when
"." is pressed would in my view be a regression compared to that.

>
> - 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 two most common ways to indent Prolog's if-then-else are:

        (   a ->
            b,
            c
        ;   d,
            e
        ),

and:

        (   a
        ->  b,
            c
        ;   d,
            e
        )

Notice that only the placement of the arrow is different.

I am using the first style throughout all Prolog code I write. The
second version is also common in the Prolog community, and notably a lot
of library code of SWI-Prolog is written in that style. I made sure (via
patches that are integrated as of version 1.15) that both styles are
well supported in the prolog.el supplied directly by Stefan Bruda, even
in the same program and predicate, if you set electric parentheses and
the other settings I posted. Please try it for reference to see the
expected indentation of Prolog code written in either style.

Relatedly, notice that the indentation must also work for disjunctions:

        (   a,
            b
        ;   c,
            d
        )

i.e., in such cases, there is no arrow at all.


>
> - 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.

Exactly, the main point here is that this would be invalid.

> The patch below fixes some of those problems.  After this patch, you
> should hopefully see something more like
>
>     (   a ->
>             b
>      ;  c
>     ).

I've never seen this indentation style in a decade of heavy Prolog use
including publishing, so my guess is that this will not help many users.

Many thanks for looking into this!

All the best,
Markus






  reply	other threads:[~2015-09-20 19:33 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-20 13:02 bug#21526: 24.5; prolog-mode: broken indentation for if-then-else construct Markus Triska
2015-09-20 18:04 ` Stefan Monnier
2015-09-20 19:33   ` Markus Triska [this message]
2015-09-21  2:34     ` Stefan Monnier
2015-09-21  3:03     ` Stefan Monnier
2015-09-21  6:02       ` Markus Triska
2015-09-21 20:22         ` Stefan Monnier
2015-09-22  6:25           ` Markus Triska
2015-09-22 15:12           ` Stefan Monnier
2015-09-22 16:38             ` Markus Triska
2015-09-22 21:04               ` Markus Triska
2015-09-23 21:08                 ` Markus Triska
2015-09-25 16:20                   ` Markus Triska
2015-09-30  2:04                     ` Stefan Monnier
2015-09-30  3:28                   ` Stefan Monnier
2015-09-30  6:38                     ` Markus Triska
2015-09-30  9:23                       ` Stefan Monnier
2015-09-30 18:35                         ` Markus Triska
2015-09-30 20:19                           ` Stefan Monnier
2015-09-30 20:40                             ` Markus Triska
2015-10-01  0:14                               ` Stefan Monnier
2015-10-01  6:22                                 ` Markus Triska
2015-10-01 14:07                                   ` Stefan Monnier
2015-10-02 16:23                                     ` Markus Triska
2015-10-02 20:47                                       ` Stefan Monnier
2015-10-05 22:38                                         ` Markus Triska
2015-10-06  2:23                                           ` Stefan Monnier
2015-09-30 18:03                     ` Markus Triska
2015-09-30 21:19                       ` Stefan Monnier
2015-09-30  3:28                 ` Stefan Monnier
2015-09-30  2:07               ` Stefan Monnier
2015-09-30  6:32                 ` Markus Triska
2015-09-30  8:55                   ` Stefan Monnier
2015-09-30 18:11                     ` Markus Triska
2015-10-05 23:49                     ` Markus Triska
2015-10-06  1:17                       ` Stefan Monnier
2015-10-06 16:45                         ` Markus Triska
2015-10-06 20:09                           ` Stefan Monnier
2015-10-20 23:47                             ` Markus Triska
2015-10-21 16:06                               ` Stefan Monnier
2015-10-21 21:58                                 ` Markus Triska
2015-10-22  1:16                                   ` Stefan Monnier
2015-10-22 19:08                                     ` Markus Triska
2015-10-25 20:01                                       ` Stefan Monnier
2015-11-23 16:36               ` Stefan Monnier
2020-08-24 18:23                 ` Lars Ingebrigtsen
2015-09-29 15:27             ` Stefan Monnier
2015-09-29 16:24               ` Markus Triska

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2vbb47uoj.fsf@metalevel.at \
    --to=triska@metalevel.at \
    --cc=21526@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.