2

This question is in two parts:

  1. How to cause emacs linter to recognize an extglob expression as an ability instead of a syntax error?
  2. How to cause emacs indenter to recognize an extglob expression as a complete entry?

I have hacked and uploaded the answer to the linter half of the question. The indentation half remains unanswered.

  • Using emacs 27.1.
  • Editing a bash case statement which takes advantage of the extglob features.
  • Modeline displays "Shell-script[bash]".
  • Successfully installed flycheck and shellcheck.

I created the following code:

#!/bin/bash
shopt -s extglob
case "$1" in
    -pull?(?(-)from|From)) echo pull ;;
    -push?(?(-)to|To)) echo push ;;
    *) >&2 echo "Did not understand ${1@Q}."    
esac
  • $ shellcheck finds nothing wrong the code.
  • Executing the bash proves proper matching. (first extglob test successfully matches -pull, -pullfrom, -pull-from, and pullFrom)

But...

  • sh-mode (child of prog-mode), whose sh-shell value is "bash", incorrectly indents deep after each extglob syntax.
  • flycheck incorrectly reports "syntax error near unexpected token `('".

How can I cause emacs to handle extglob properly?

Paul
  • 183
  • 8
  • 1
    Although it means changing the code to add an additional optional character, I note that `sh-mode` copes if you format your cases as `(...)` rather than `...)`. – phils Nov 11 '22 at 00:15

2 Answers2

1

Flycheck is not related to indentation. Flycheck just runs your code to try to find syntax errors. Shellcheck is another way to locate errors; it too has nothing to do with indentation. You should remove all of those bits from your question because they are all irrelevant. Still, thank you for trying to include relevant information in the question, since so many do not even try.

Indentation in your case is being handled by sh-mode. sh-mode is the part of Emacs that handles syntax highlighting, indentation, and other concerns when editing shell scripts. It is derived from a mode called prog-mode, so that it shares certain characteristics with other modes for programming languages.

You should read the help for sh-mode. Start with C-h f sh-mode. The documentation for this function will give you a fair amount of information. In particular, it lists a keybinding C-c ? for finding out what indentation rule it is currently following and another C-c < for changing those rules. You may however find that none of the indentation rules work for your code, in which case you will need to dive in and improve the parser, possibly adding new indentation rules in the process.

db48x
  • 15,741
  • 1
  • 19
  • 23
1

Phils chimed in with a comment and finished the answer...

Many ways to fix the syntax error:

  • Disable flycheck to remove all errors.
  • Set flycheck-sh-bash-args to '("-O" "extglob") to remove unwanted errors.
  • Use flycheck-select-checker to manually set the value of flycheck-checker to sh-shellcheck to remove unwanted errors.

To solve the horrible indentation problem:

  • Begin every case which uses extglob with an open parentheses (.

Thank You phils. A syntax error can be ignored, but an indentation problem cannot.

Emacs indents the following code properly which executes flawlessly:

#!/bin/bash
shopt -s extglob
case "$1" in
    (-pull?(?(-)from|From)) echo pull ;;
    (-push?(?(-)to|To)) echo push ;;
    *) >&2 echo "Did not understand ${1@Q}."    
esac
Paul
  • 183
  • 8
  • It sounds like you have two entirely separate questions. Please edit things so that you have asked and answered just one question, and then post your remaining question separately. – phils Nov 11 '22 at 00:13