unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Ergus <spacibba@aol.com>
Cc: emacs-devel@gnu.org
Subject: Re: Issue with bash new syntax.
Date: Tue, 26 Jan 2021 17:53:42 -0500	[thread overview]
Message-ID: <jwv8s8fco0y.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <20210126141706.uk5dtaenpdbydfr6@Ergus> (Ergus's message of "Tue,  26 Jan 2021 15:17:06 +0100")

> I am observing some issues with the indentation of the modern for loop
> syntax in bash:
>
> for ((it=0; it<${limit}; ++it)) 
> 	{
> 		echo "whatever $it"
> 	}
>
>
> The question-issue comes in two parts.
>
> Question: How can I configure to remove the extra indentation before `{`?
>    In cc-mode there is a procedure, a bit cumbersome, but working. But
>    sh-mode does not have such a thing.

You can't really configure it easily, currently.
The patch below should take care of it, tho.

> Issue:
>    When this loop is nested inside an if or another for loop the
>    closing indentation seems to be broken
>
> for dim in ${dims[@]}; do
>          for bs in ${blocksizes[@]}; do
>                  if [ something ]; then
>                          for ((it=0; it<${ARGS[R]}; ++it)) 
>                                  {
>                                          whatever...
>                                  }
> -->                             else
>                                          somethingelse
> -->                     fi
> -->                     done
> -->                     done
>
> Should I report a bug for this?

Yes.  The problem is that sh expects `for` to be followed by `do...done`
before the loop is over, so as far as `sh-mode` is concerned we're still
in the part of the `for` loop before the `do...done`.  It's not trivial
to fix, so better make it a real bug report so it doesn't get lost.


        Stefan


diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el
index cc045a1b2d..a8e7c9f70e 100644
--- a/lisp/progmodes/sh-script.el
+++ b/lisp/progmodes/sh-script.el
@@ -1957,12 +1957,17 @@ sh-smie-sh-rules
     ('(:after . "case-)") (- (sh-var-value 'sh-indent-for-case-alt)
                              (sh-var-value 'sh-indent-for-case-label)))
     (`(:before . ,(or "(" "{" "[" "while" "if" "for" "case"))
-     (if (not (smie-rule-prev-p "&&" "||" "|"))
-         (when (smie-rule-hanging-p)
-           (smie-rule-parent))
+     (cond
+      ((and (equal token "{") (smie-rule-parent-p "for"))
+       (let ((data (smie-backward-sexp "in")))
+         `(column . ,(smie-indent-virtual))))
+      ((not (smie-rule-prev-p "&&" "||" "|"))
+       (when (smie-rule-hanging-p)
+         (smie-rule-parent)))
+      (t
        (unless (smie-rule-bolp)
 	 (while (equal "|" (nth 2 (smie-backward-sexp 'halfexp))))
-	 `(column . ,(smie-indent-virtual)))))
+	 `(column . ,(smie-indent-virtual))))))
     ;; FIXME: Maybe this handling of ;; should be made into
     ;; a smie-rule-terminator function that takes the substitute ";" as arg.
     (`(:before . ,(or ";;" ";&" ";;&"))




      reply	other threads:[~2021-01-26 22:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20210126141706.uk5dtaenpdbydfr6.ref@Ergus>
2021-01-26 14:17 ` Issue with bash new syntax Ergus
2021-01-26 22:53   ` Stefan Monnier [this message]

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=jwv8s8fco0y.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=emacs-devel@gnu.org \
    --cc=spacibba@aol.com \
    /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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).