Subject: 24.4; sh-mode & zsh: wrong indentation for alternate forms of complex commands

From: Moritz Bunkus <[email-masked]>
Date: Tue, 25 Nov 2014 09:44:59 +0100

Hey,

Emacs' sh-mode when used for zsh scripts doesn't understand zsh's
alternate forms for complex commands[1]. This leads to the indentation
being completely wrong after using one such command. For example:

----------------------------------------
#!/bin/zsh

if [[ -z $1 ]] echo No args
   do_stuff Wrong indentation
----------------------------------------

The »if« ends on the same line it begins, therefore »do_stuff« should
not be indented at all.

Some more examples of what is wrong:

----------------------------------------
#!/bin/zsh

for arg ($@) echo argument $arg
    do_stuff Wrong indentation
----------------------------------------

or

----------------------------------------
#!/bin/zsh

while [[ -n $1 ]]; do
  if [[ $1 == yes ]] echo oh yeah
     do_stuff Wrong indentation
----------------------------------------

Using { … } as the delimiters for the command list is not parsed
correctly either:

----------------------------------------
for arg ($@) { echo argument $arg ; cp $arg /backup/ ; }
    do_stuff Wrong indentation
----------------------------------------

and so on.

[1] http://info2html.sourceforge.net/cgi-bin/info2html-demo/info2html?%28zsh.info.gz%29Alternate%2520Forms%2520For%2520Complex%2520Commands

sh-mode is the original sh-script.el packaged with Emacs 24.4.1: 2.0f.

Kind regards,
mosu

In GNU Emacs 24.4.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.14.3)
 of 2014-10-21 on bitzer.hoetzel.info
Windowing system distributor `The X.Org Foundation', version 11.0.11602000
Configured using:
 `configure --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib
 --localstatedir=/var --with-x-toolkit=gtk3 --with-xft
 'CFLAGS=-march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong
 --param=ssp-buffer-size=4' CPPFLAGS=-D_FORTIFY_SOURCE=2
 LDFLAGS=-Wl,-O1,--sort-common,--as-needed,-z,relro'

Important settings:
  value of $LC_COLLATE: de_DE.UTF-8
  value of $LC_CTYPE: de_DE.UTF-8
  value of $LC_MONETARY: de_DE.UTF-8
  value of $LC_NUMERIC: de_DE.UTF-8
  value of $LC_TIME: de_DE.UTF-8
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Shell-script

Minor modes in effect:
  sh-electric-here-document-mode: t
  magit-auto-revert-mode: t
  ido-everywhere: t
  shell-dirtrack-mode: t
  diff-auto-refine-mode: t
  global-edit-server-edit-mode: t
  which-function-mode: t
  global-hl-line-mode: t
  show-paren-mode: t
  winner-mode: t
  yas-global-mode: t
  yas-minor-mode: t
  tooltip-mode: t
  mouse-wheel-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  column-number-mode: t
  line-number-mode: t
  transient-mark-mode: t

Recent input:
C-p C-a P C-g A C-p A <f11> P P q q P P M o i n , <return>
<return> C-SPC C-n C-SPC C-n C-n C-w <return> C-n C-n
C-e <return> <return> I s t SPC i m p l e m e n t i
e r t SPC z g <backspace> z g l . SPC e i n e r SPC
F i l t e r m ö g l i c h k e i t SPC d <backspace>
n a c h SPC i h r . <return> C-p C-p C-p M-q C-n C-n
C-n C-x 5 s <return> l g <tab> <return> C-x C-s C-c
C-c C-x k <return> C-x k <return> C-x k <return> C-x
C-b C-p C-p C-p C-p C-p k k k k k x C-p C-p C-p k C-n
k x <f5> C-x C-f ~ / b i n / s s h - <return> C-n C-n
C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n
C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n C-n
C-n C-n <return> <return> C-p C-p <return> <tab> C-h
v s h - i n d e n t a t i o n <return> M-x r e p o
r t <return> z s h SPC i n d e C-g M-x i n f o <return>
m z s h <return> c o n u C-s c o n <return> <return>
C-x 1 C-v C-v C-v C-v M-< u C-p C-p C-p C-p C-p C-p
<return> C-n C-n C-n C-n C-n C-n C-p C-n C-n C-n <tab>
C-p <return> <f11> M-x <up> <up> <return> C-g C-h v
m a j o r - m o d e <return> M-x r e <up> <return>

Recent messages:
Type C-x 1 to delete the help window.
Auto-saving...
Quit
Composing main Info directory...done
(zsh) Top
o is undefined
Mark saved where search started
Mark set
Quit
Type C-x 1 to delete the help window.

Load-path shadows:
/home/mbunkus/.emacs.d/site-start.d/libs/paredit hides /home/mbunkus/.emacs.d/elpa/paredit-20140128.1248/paredit
/home/mbunkus/.emacs.d/site-start.d/libs/apache-mode hides /usr/share/emacs/site-lisp/apache-mode
/home/mbunkus/.emacs.d/elpa/markdown-mode-20140819.602/markdown-mode hides /usr/share/emacs/site-lisp/markdown-mode/markdown-mode
/home/mbunkus/.emacs.d/site-start.d/libs/buff-menu hides /usr/share/emacs/24.4/lisp/buff-menu
/home/mbunkus/.emacs.d/site-start.d/libs/cc-langs hides /usr/share/emacs/24.4/lisp/progmodes/cc-langs
/home/mbunkus/.emacs.d/site-start.d/libs/cc-align hides /usr/share/emacs/24.4/lisp/progmodes/cc-align
/home/mbunkus/.emacs.d/site-start.d/libs/cc-defs hides /usr/share/emacs/24.4/lisp/progmodes/cc-defs
/home/mbunkus/.emacs.d/site-start.d/libs/cc-mode hides /usr/share/emacs/24.4/lisp/progmodes/cc-mode
/home/mbunkus/.emacs.d/site-start.d/libs/cc-fonts hides /usr/share/emacs/24.4/lisp/progmodes/cc-fonts
/home/mbunkus/.emacs.d/site-start.d/libs/cc-styles hides /usr/share/emacs/24.4/lisp/progmodes/cc-styles
/home/mbunkus/.emacs.d/site-start.d/libs/cc-vars hides /usr/share/emacs/24.4/lisp/progmodes/cc-vars
/home/mbunkus/.emacs.d/site-start.d/libs/cc-menus hides /usr/share/emacs/24.4/lisp/progmodes/cc-menus
/home/mbunkus/.emacs.d/site-start.d/libs/cc-cmds hides /usr/share/emacs/24.4/lisp/progmodes/cc-cmds
/home/mbunkus/.emacs.d/site-start.d/libs/cc-awk hides /usr/share/emacs/24.4/lisp/progmodes/cc-awk
/home/mbunkus/.emacs.d/elpa/flymake-0.4.16/flymake hides /usr/share/emacs/24.4/lisp/progmodes/flymake
/home/mbunkus/.emacs.d/site-start.d/libs/cc-compat hides /usr/share/emacs/24.4/lisp/progmodes/cc-compat
/home/mbunkus/.emacs.d/site-start.d/libs/cc-engine hides /usr/share/emacs/24.4/lisp/progmodes/cc-engine
/home/mbunkus/.emacs.d/site-start.d/libs/cfengine hides /usr/share/emacs/24.4/lisp/progmodes/cfengine
/home/mbunkus/.emacs.d/site-start.d/libs/cperl-mode/cperl-mode hides /usr/share/emacs/24.4/lisp/progmodes/cperl-mode
/home/mbunkus/.emacs.d/site-start.d/libs/cc-bytecomp hides /usr/share/emacs/24.4/lisp/progmodes/cc-bytecomp
/home/mbunkus/.emacs.d/site-start.d/libs/cc-guess hides /usr/share/emacs/24.4/lisp/progmodes/cc-guess
/home/mbunkus/.emacs.d/site-start.d/libs/cl-lib hides /usr/share/emacs/24.4/lisp/emacs-lisp/cl-lib

Features:
(shadow sort mail-extr jka-compr emacsbug pp sh-script smie mule-util
flymake cc-defs cc-bytecomp sgml-mode smerge-mode mo-git-blame misearch
multi-isearch vc-git hi-lock executable footnote flyspell ispell f s
paredit two-column 70-perl cperl-mode 70-ido magit-blame dired-sort-map
dired-single magit-key-mode magit iswitchb ido esh-var esh-io esh-cmd
esh-opt esh-ext esh-proc esh-arg eldoc esh-groups eshell esh-module
esh-mode esh-util ediff-merg ediff-wind ediff-diff ediff-mult ediff-help
ediff-init ediff-util ediff dired-x dired view tramp tramp-compat
auth-source gnus-util password-cache tramp-loaddefs trampver shell
pcomplete grep compile comint epa epg diff-mode autorevert filenotify
ansi-color git-rebase-mode thingatpt git-commit-mode server log-edit
message idna sendmail format-spec rfc822 mml mml-sec mm-decode mm-bodies
mm-encode mail-parse rfc2231 rfc2047 rfc2045 ietf-drums mm-util
mail-prsvr mailabbrev mail-utils gmm-utils mailheader pcvs-util add-log
solarized-light-theme solarized-definitions 70-html-xml rng-nxml
rng-valid rng-loc rng-uri rng-parse nxml-parse rng-match rng-dt rng-util
rng-pttrn nxml-ns nxml-mode nxml-outln nxml-rap nxml-util nxml-glyph
nxml-enc xmltok saveplace buff-menu+ buff-menu edit-server 70-helpers
which-func imenu dash-functional dash browse-kill-ring hl-line paren
70-text 70-tabs filladapt winner ring 01-version-test my-site-start
yasnippet advice help-fns derived easy-mmode edmacro kmacro help-mode
ack-and-a-half-autoloads ag-autoloads clojure-test-mode-autoloads
cider-autoloads dash-functional-autoloads elixir-mode-autoloads
expand-region-autoloads f-autoloads dash-autoloads flymake-autoloads
flymake-yaml-autoloads flymake-easy-autoloads gist-autoloads finder-inf
gh-autoloads eieio byte-opt bytecomp byte-compile cconv eieio-core
logito-autoloads info easymenu magit-autoloads git-rebase-mode-autoloads
git-commit-mode-autoloads markdown-mode-autoloads nrepl-autoloads
clojure-mode-autoloads paredit-autoloads pcache-autoloads
pkg-info-autoloads epl-autoloads s-autoloads tracwiki-mode-autoloads
xml-rpc-autoloads yaml-mode-autoloads package epg-config cl-macs cl gv
cl-loaddefs cl-lib 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 dbusbind
gfilenotify dynamic-setting system-font-setting font-render-setting
move-toolbar gtk x-toolkit x multi-tty emacs)

Memory information:
((conses 16 429319 47075)
 (symbols 48 38141 18)
 (miscs 40 1111 3733)
 (strings 32 91728 19469)
 (string-bytes 1 2507381)
 (vectors 16 37275)
 (vector-slots 8 1438804 150917)
 (floats 8 535 694)
 (intervals 56 1735 484)
 (buffers 960 57)
 (heap 1024 53786 2991))