zsh-workers
 help / color / mirror / code / Atom feed
* Prompt expansion on signals
@ 2021-11-28  9:59 Roman Perepelitsa
  2021-11-28 19:50 ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Roman Perepelitsa @ 2021-11-28  9:59 UTC (permalink / raw)
  To: Zsh hackers list

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.


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-11-29 17:43 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-28  9:59 Prompt expansion on signals Roman Perepelitsa
2021-11-28 19:50 ` Bart Schaefer
2021-11-28 20:31   ` Roman Perepelitsa
2021-11-28 21:10     ` Oliver Kiddle
2021-11-28 21:36       ` Roman Perepelitsa
2021-11-29  1:38     ` Bart Schaefer
2021-11-29 11:36       ` Roman Perepelitsa
2021-11-29 17:42         ` Roman Perepelitsa

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).