Subject: bug#18031: closed (Re: bug#18031: 24.3.92; bad indentation in shell script mode)

From: [email-masked] (GNU bug Tracking System)
Date: Thu, 04 Dec 2014 15:11:02 +0000

Your bug report

#18031: 24.3.92; bad indentation in shell script mode

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
18031: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=18031
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#18031: 24.3.92; bad indentation in shell script mode
From: Stefan Monnier <[email-masked]>
To: Gilles Pion <[email-masked]>
Cc: [email-masked]
Date: Thu, 04 Dec 2014 10:09:59 -0500
List-Id: <debbugs-submit.debbugs.gnu.org>

Version:24.5

> Here's the result:
> #!/bin/ksh
> grep -e "^$userregexp:" /etc/passwd | cut -d :  -f 1 | while read user ; do
>                                                            print -u2
> "user=$user"
>                                                            sudo -U
> $user -ll | while read line ; do

>             :

>         done
>                                                        done

After fixing the above line-wrapping to really reproduce what Emacs
does, you'll see that it's indented in a way which is sensible:
- The "print -u2" line is indented 4 chars deeper than the "while" in
  which it's nested.
- The "user=$user" line is actually at the end of the previous line.
- The "sudo" line is aligned with the previous line.
- The ":" is indented 4 chars deeper than the "while" in which it's nested.
- The "done" are aligned with the "while" they close.

Now, I understand that "sensible" doesn't mean "good" or "desirable",
but at least the indentation works sanely (contrary to the bug#18756 case).
So it's a "small matter" of teaching sh-mode to indent the body of loops
less deeply when the loop itself doesn't start at the beginning of the line.

I installed the patch below which seems to do the trick.

        Stefan

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 12e5ef0..26b09a6 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,9 @@
+2014-12-04  Stefan Monnier  <[email-masked]>
+
+	* progmodes/sh-script.el (sh-smie-sh-rules): Go back to the beginning
+	of the whole pipe when indenting an opening keyword after a |.
+	Generalize this treatment to opening keywords like "while" (bug#18031).
+
 2014-12-01  Stefan Monnier  <[email-masked]>
 
 	* simple.el (newline): Place the hook buffer-locally,
diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el
index 724d22a..1165144 100644
--- a/lisp/progmodes/sh-script.el
+++ b/lisp/progmodes/sh-script.el
@@ -1988,12 +1988,12 @@ May return nil if the line should not be treated as continued."
                    (and (numberp indent) (numberp initial)
                         (<= indent initial)))))
      `(column . ,(+ initial sh-indentation)))
-    (`(:before . ,(or `"(" `"{" `"["))
+    (`(:before . ,(or `"(" `"{" `"[" "while" "if" "for" "case"))
      (if (not (smie-rule-prev-p "&&" "||" "|"))
          (when (smie-rule-hanging-p)
            (smie-rule-parent))
        (unless (smie-rule-bolp)
-	 (smie-backward-sexp 'halfexp)
+	 (while (equal "|" (nth 2 (smie-backward-sexp 'halfexp))))
 	 `(column . ,(smie-indent-virtual)))))
     ;; FIXME: Maybe this handling of ;; should be made into
     ;; a smie-rule-terminator function that takes the substitute ";" as arg.

Subject: 24.3.92; bad indentation in shell script mode
From: Gilles Pion <[email-masked]>
To: [email-masked]
Date: Wed, 16 Jul 2014 14:08:26 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Actions to reproduce the bug:

* lauch emacs in failsafe mode ("emacs -Q")
* create a new buffer named "anythingyouwant.ksh" and insert the following lines
========= cut here ===========
#!/bin/ksh
grep -e "^$userregexp:" /etc/passwd | cut -d :  -f 1 | while read user ; do
    print -u2 "user=$user"
     sudo -U $user -ll | while read line ; do
         :
     done
done
========= cut here ===========
* M-x shell-script-mode
* M-x mark-whole-buffer
* M-x indent-region

Here's the result:

#!/bin/ksh

grep -e "^$userregexp:" /etc/passwd | cut -d :  -f 1 | while read user ; do
                                                           print -u2
"user=$user"
                                                           sudo -U
$user -ll | while read line ; do

            :

        done
                                                       done

In GNU Emacs 24.3.92.1 (powerpc-ibm-aix6.1.0.0, X toolkit)
 of 2014-07-01 on ax30001
Windowing system distributor `The Cygwin/X Project', version 11.0.11501000
Configured using:
 `configure --prefix=/<SITE>/opt/emacs-24.3.92
 --exec-prefix=/<SITE>/opt/emacs-24.3.92 --sysconfdir=/etc/<SITE>/opt/emacs
 --localstatedir=/var/<SITE>/opt/emacs --without-all --with-x-toolkit=lucid
 --with-xpm --with-x-toolkit=lucid
 --enable-locallisppath=/<SITE>/share/emacs/site-lisp 'CFLAGS=-O3 -g3'
 'CPPFLAGS=-I/<SITE>/opt/emacs-24.3.92/include -I/opt/freeware/include '
 'LDFLAGS=-L/<SITE>/opt/emacs-24.3.92/lib -Wl,-bstatic -L/opt/freeware/lib
 -lXpm -Wl,-bdynamic''

Important settings:
  value of $LANG: C
  locale-coding-system: iso-latin-1-unix

Major mode: Shell-script

Minor modes in effect:
  sh-electric-here-document-mode: t
  tooltip-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort gnus-util mail-extr emacsbug message format-spec rfc822 mml
mml-sec mm-decode mm-bodies mm-encode mail-parse rfc2231 mailabbrev
gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums mm-util
help-fns mail-prsvr mail-utils help-mode easymenu sh-script smie
executable dired time-date tooltip electric uniquify ediff-hook vc-hooks
lisp-float-type mwheel x-win x-dnd tool-bar dnd fontset image regexp-opt
fringe tabulated-list newcomment lisp-mode prog-mode register page
menu-bar rfn-eshadow timer select scroll-bar mouse jit-lock font-lock
syntax facemenu font-core frame cham georgian utf-8-lang misc-lang
vietnamese tibetan thai tai-viet lao korean japanese hebrew greek
romanian slovak czech european ethiopic indian cyrillic chinese
case-table epa-hook jka-cmpr-hook help simple abbrev minibuffer nadvice
loaddefs button faces cus-face macroexp files text-properties overlay
sha1 md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote make-network-process dynamic-setting
x-toolkit x multi-tty emacs)

Memory information:
((conses 8 87772 8428)
 (symbols 24 18605 0)
 (miscs 20 60 360)
 (strings 16 14441 4333)
 (string-bytes 1 359100)
 (vectors 8 10219)
 (vector-slots 4 413533 5420)
 (floats 8 67 336)
 (intervals 28 783 438)
 (buffers 512 16))

-- 
Gilles