Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Can zsh `else` reserved keyword command be aliased and the lexem itself be repurposed as `fi` keyword command?



29.12.2017, 13:53, "mathieu stumpf guntz" <psychoslave@xxxxxxxxxxxxxxxxx>:
> Le 29/12/2017 à 09:47, Bart Schaefer a écrit :
>>  You're missing a semicolon after the "C". That's the reason you get
>
> Thank you. For some reason however, the seconde run of `export LANC=C;
> enable -r else; if true; then echo 'yep'; else echo 'nop'; fi; disable
> -r else` will return an error message with the system local anyway:
>
>     % enable -r else;
>     % export LANC=C; enable -r else; if true; then echo 'yep'; else echo
>     'nop'; fi; disable -r else
>     yep
>     % export LANC=C; enable -r else; if true; then echo 'yep'; else echo
>     'nop'; fi; disable -r else
>     yep
>     zsh: else: commande inconnue.
>
>>  If I understand your question, the answer is "no": you can't execute
>>  the front part of an "if" until the "fi" has been read. See my
>>  previous email "fully parsed before executed."
>
> I'm rather estonished by this lake of possibility to make the equivalent
> of an "\n" in the middle of a line, but OK.
>
> So the idea would be to have something like
>
>     % whence -w else
>     else: reserved
>     % enable -r else; "\n" whence -w else; if true; then echo 'yep';
>     else echo 'nop'; fi; disable -r else
>     else: reserved
>     yep
>     % whence -w else
>     else: none
>     % enable -r else; "\n" whence -w else; if true; then echo 'yep';
>     else echo 'nop'; fi; disable -r else
>     else: reserved
>     yep
>
> But "\n" doesn't work here as a substitution of an effective linefeed.
>>  (Well, you could switch to csh, which does execute every line as it
>>  goes along, even in complex structures. But no, not in zsh.)
>
> Nice to know, thank you.

If you do not like any part of shell syntax, want to use ZLE for Python REPL or whatever there is a thing which 100% works: just override accept-line widget and have zshaddhistory hook to put original line into history. For example I am using the following to have more extensive escaping for `:h`, `zmw` and `zpy` “commands”:

    function zshaddhistory()
    {
        emulate -L zsh
        if (( ${+_HISTLINE} && ${#_HISTLINE} )) ; then
            print -sr -- "${_HISTLINE}"
            unset _HISTLINE
        elif (( 0x20==#1 )) ; then
            return 1
        elif (( ${#1} )) ; then
            print -sr -- "${1%%$'\n'}"
        fi
        fc -p
    }
    function _-accept-line()
    {
        emulate -L zsh
        local -r autopushd=${options[autopushd]}
        options[autopushd]=off
        cd $PWD || cd
        options[autopushd]=$autopushd
        if [[ ${BUFFER[1,3]} == ":h " ]] ; then
            typeset -g _HISTLINE=$BUFFER
            BUFFER=":h ${(q)BUFFER[4,-1]}"
        elif [[ ${BUFFER[1,4]} == "zmw " ]] ; then
            typeset -g _HISTLINE=$BUFFER
            BUFFER="zmw "${(j. .)${(q)${(z)BUFFER[5,-1]}}}
        elif [[ ${BUFFER[1,4]} == "zpy " ]] ; then
            typeset -g _HISTLINE=$BUFFER
            BUFFER="zpython ${(qqq)BUFFER[5,-1]}"
        fi
        zle .accept-line
    }
    zle -N accept-line                       _-accept-line

The main problem with this approach is that you will have to do more parsing yourself then you probably preferred to have to do though.



Messages sorted by: Reverse Date, Date, Thread, Author