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

Prompt expansion on signals



Consider the following user configuration:

    setopt prompt_subst
    my_var='%#'
    PS1='$my_var '

For this to work correctly, prompt_subst must be set when prompt is
expanded. However, this requirement can be violated when prompt is
expanded in a signal handler. One example where this happens is upon
the completion of a background job.

    % sleep 1 &
    [1] 3566
    %
    [1]  + done       sleep 1
    %

In this example all went well -- the job completed while zle was idle,
so prompt got expanded with correct options.

If the background job completes while zle is running a function,
prompt gets expanded with the current options of the function.

    % zle-line-init() { emulate -LR zsh; while (($#jobstates)) : }
    % zle -N zle-line-init
    % sleep 1 &
    [1] 3592
    %
    [1]  + done       sleep 1
    $my_var

Note "$my_var" as the last prompt instead of the expected "%".

The same issue arises when the terminal is resized. Here prompt gets
expanded on SIGWINCH with whatever options are in effect.

    % typeset -p LINES
    typeset -i10 LINES=46
    % zle-line-init() { emulate -LR zsh; while ((LINES > 24)) =true }
    $my_var typeset -p LINES
    typeset -i10 LINES=22

Here I redefined zle-line-init while my terminal was 46 lines tall and
then I resized it to 22 lines (more specifically, I split it). Note
"$my_var" in prompt. Also note =true in the loop. It's needed because
zsh postpones the processing of SIGWINCH until the next fork or until
zle becomes idle.

Here's an alternative way to trigger the same code without resizing
the terminal:

    % zle-line-init() { emulate -LR zsh; LINES=0 }
    $my_var

This approach is the easiest to test with.

It appears that one is at risk of prompt expansion with incorrect
options if both of the following conditions are met:

1. Using zle widgets that change options. Using
zsh-syntax-highlighting (just to name a popular plugin) qualifies.
2. Using background jobs without nonotify or resizing the terminal.

Many (most?) zsh users are affected.

Prompt expansion can be affected by options other than prompt_*. For example:

    setopt prompt_subst ksh_arrays
    my_var=('%#')
    PS1='${my_var[0]} '

Expanding this prompt under `emulate -L zsh` (without -R) will produce
incorrect results.

Expanding prompt with incorrect options can be a minor nuisance (like
in the examples above) or a major issue (when prompt is expanded with
prompt_subst when it's not intended).

Is there a solution for this problem that I'm missing? Are there
workarounds? How could it be fixed?

Roman.




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