zsh-users
 help / color / mirror / code / Atom feed
From: Marlon Richert <marlon.richert@gmail.com>
To: Roman Perepelitsa <roman.perepelitsa@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Why are prompt expansions of %v sequences quoted in bindkey style?
Date: Tue, 23 Jan 2024 20:43:54 +0200	[thread overview]
Message-ID: <93F17F14-8DF3-4D02-875F-A7BAE9BA6427@gmail.com> (raw)
In-Reply-To: <CAN=4vMoUnO=v3tWyY3+4ixMEfu_8_bC+c_KEOVCoLK7QH4Kogw@mail.gmail.com>

I missed that a 2nd expansion was needed in my example, but that is easy to work around. 

However, the quoting seems to make it impossible to use $psvar for anything involving control characters.

Is there no way to work around this?

I would like to find a solution that does not involve promptsubst.

> On 23. Jan 2024, at 15.05, Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote:
> 
> On Tue, Jan 23, 2024 at 1:07 PM Marlon Richert <marlon.richert@gmail.com> wrote:
>> 
>> When I declare my prompt as:
>> 
>> PS1=$'%{\e[2m%}%#%{\e[0m%} '
>> 
>> this works as I expect it to: My prompt becomes a dim/faint % followed by a space.
>> 
>> However, this does not work as I expect it to when I store the ANSI sequences in $psvar.
>> 
>> The following code:
>> 
>> psvar=( $'%{\e[2m%}' $'%{\e[0m%}' )
>> PS1='%1v%#%2v '
>> 
>> results in the following prompt with terminal default color:
>> 
>> %{^[[2m%}%%{^[[0m%}
>> 
>> As you can see, the strings stored in $psvar are expanded in a quoted form, à la bindkey.
> 
> Percent expansion isn't recursive. It is done only once. If %v expands
> into %1F, the latter won't be expanded any further and will remain as
> a literal %1F. This behavior makes sense and it alone will preclude
> you from achieving what you are after.
> 
> However, in addition, the expansion of %v is quoted: newline becomes
> \n, escape becomes ^[, etc. This is meant to make the use of %v safe.
> By "safe" I meant that it allows you to ensure a non- broken prompt
> regardless of the content of psvar.
> 
>  # This prompt is never broken: it does not bleed colors,
>  # does not confuse zle w.r.t. the cursor position, etc.
>  PS1='%v%# '
> 
>> Is this intentional?
> 
> I wasn't there when this feature was designed but it works as I would
> expect. If you want an extra percent expansion after the substitution
> of parameters, use prompt_subst. This is a much more powerful and a
> much more dangerous tool.
> 
> Roman.


  reply	other threads:[~2024-01-23 18:45 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-23 11:03 Marlon Richert
2024-01-23 13:05 ` Roman Perepelitsa
2024-01-23 18:43   ` Marlon Richert [this message]
2024-01-23 18:55     ` Roman Perepelitsa
2024-01-23 19:15     ` Bart Schaefer
2024-01-23 20:21       ` Mikael Magnusson
2024-01-24  5:45         ` Marlon Richert
2024-01-24  8:53           ` Mikael Magnusson
2024-01-24 13:37             ` Marlon Richert
2024-01-24 19:35               ` Mikael Magnusson
2024-01-25  8:42                 ` Marlon Richert
2024-01-25  9:20                   ` Lawrence Velázquez
2024-01-25 16:16                     ` Mikael Magnusson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=93F17F14-8DF3-4D02-875F-A7BAE9BA6427@gmail.com \
    --to=marlon.richert@gmail.com \
    --cc=roman.perepelitsa@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).