zsh-users
 help / color / mirror / code / Atom feed
* Why are prompt expansions of %v sequences quoted in bindkey style?
@ 2024-01-23 11:03 Marlon Richert
  2024-01-23 13:05 ` Roman Perepelitsa
  0 siblings, 1 reply; 13+ messages in thread
From: Marlon Richert @ 2024-01-23 11:03 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 666 bytes --]

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.

Is this intentional? If so, why? Is there a way to circumvent this?

I would like strings in $psvar to be inserted literally when using %v
prompt sequences.

[-- Attachment #2: Type: text/html, Size: 1645 bytes --]

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

* Re: Why are prompt expansions of %v sequences quoted in bindkey style?
  2024-01-23 11:03 Why are prompt expansions of %v sequences quoted in bindkey style? Marlon Richert
@ 2024-01-23 13:05 ` Roman Perepelitsa
  2024-01-23 18:43   ` Marlon Richert
  0 siblings, 1 reply; 13+ messages in thread
From: Roman Perepelitsa @ 2024-01-23 13:05 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Zsh Users

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.


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

* Re: Why are prompt expansions of %v sequences quoted in bindkey style?
  2024-01-23 13:05 ` Roman Perepelitsa
@ 2024-01-23 18:43   ` Marlon Richert
  2024-01-23 18:55     ` Roman Perepelitsa
  2024-01-23 19:15     ` Bart Schaefer
  0 siblings, 2 replies; 13+ messages in thread
From: Marlon Richert @ 2024-01-23 18:43 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Zsh Users

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.


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

* Re: Why are prompt expansions of %v sequences quoted in bindkey style?
  2024-01-23 18:43   ` Marlon Richert
@ 2024-01-23 18:55     ` Roman Perepelitsa
  2024-01-23 19:15     ` Bart Schaefer
  1 sibling, 0 replies; 13+ messages in thread
From: Roman Perepelitsa @ 2024-01-23 18:55 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Zsh Users

On Tue, Jan 23, 2024 at 7:44 PM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> 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.

This is correct and I believe intended. I appreciate this feature for
what it is and find it very useful.

> Is there no way to work around this?

These are the options if you want to use \e[2m within the prompt:

1. A static PS1 with \e[2m embedded directly in the value without indirection.
2. The same as above but the value of PS1 is set in precmd and thus
can change from command to command.
3. prompt_subst

There are no other options that I'm aware of.

Roman.


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

* Re: Why are prompt expansions of %v sequences quoted in bindkey style?
  2024-01-23 18:43   ` Marlon Richert
  2024-01-23 18:55     ` Roman Perepelitsa
@ 2024-01-23 19:15     ` Bart Schaefer
  2024-01-23 20:21       ` Mikael Magnusson
  1 sibling, 1 reply; 13+ messages in thread
From: Bart Schaefer @ 2024-01-23 19:15 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Roman Perepelitsa, Zsh Users

On Tue, Jan 23, 2024 at 10:44 AM Marlon Richert
<marlon.richert@gmail.com> wrote:
>
> However, the quoting seems to make it impossible to use $psvar for anything involving control characters.

That's correct.  Anything involving control characters would also
require %{...%} or %G to specify the occupied screen width.

> Is there no way to work around this?

As Roman mentioned, the usual way is to use precmd to update prompt values.


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

* Re: Why are prompt expansions of %v sequences quoted in bindkey style?
  2024-01-23 19:15     ` Bart Schaefer
@ 2024-01-23 20:21       ` Mikael Magnusson
  2024-01-24  5:45         ` Marlon Richert
  0 siblings, 1 reply; 13+ messages in thread
From: Mikael Magnusson @ 2024-01-23 20:21 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Marlon Richert, Roman Perepelitsa, Zsh Users

On 1/23/24, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Tue, Jan 23, 2024 at 10:44 AM Marlon Richert
> <marlon.richert@gmail.com> wrote:
>>
>> However, the quoting seems to make it impossible to use $psvar for
>> anything involving control characters.
>
> That's correct.  Anything involving control characters would also
> require %{...%} or %G to specify the occupied screen width.
>
>> Is there no way to work around this?
>
> As Roman mentioned, the usual way is to use precmd to update prompt values.

fwiw, you can do things like %F{%5v}hello or %(3V.%B.) to make things
dynamic without using promptsubst or updating PS1 in precmd, but you
do have to have all the actual formatting in PS1 then.

-- 
Mikael Magnusson


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

* Re: Why are prompt expansions of %v sequences quoted in bindkey style?
  2024-01-23 20:21       ` Mikael Magnusson
@ 2024-01-24  5:45         ` Marlon Richert
  2024-01-24  8:53           ` Mikael Magnusson
  0 siblings, 1 reply; 13+ messages in thread
From: Marlon Richert @ 2024-01-24  5:45 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Bart Schaefer, Roman Perepelitsa, Zsh Users

Thanks, I think that might work for me. 

I am indeed trying to use $psvar to dynamically update my prompt’s visual formatting, without the use of prompsubst or changing $PS1.

> On 23. Jan 2024, at 22.21, Mikael Magnusson <mikachu@gmail.com> wrote:
> 
> On 1/23/24, Bart Schaefer <schaefer@brasslantern.com> wrote:
>>> On Tue, Jan 23, 2024 at 10:44 AM Marlon Richert
>>> <marlon.richert@gmail.com> wrote:
>>> 
>>> However, the quoting seems to make it impossible to use $psvar for
>>> anything involving control characters.
>> 
>> That's correct.  Anything involving control characters would also
>> require %{...%} or %G to specify the occupied screen width.
>> 
>>> Is there no way to work around this?
>> 
>> As Roman mentioned, the usual way is to use precmd to update prompt values.
> 
> fwiw, you can do things like %F{%5v}hello or %(3V.%B.) to make things
> dynamic without using promptsubst or updating PS1 in precmd, but you
> do have to have all the actual formatting in PS1 then.
> 
> --
> Mikael Magnusson


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

* Re: Why are prompt expansions of %v sequences quoted in bindkey style?
  2024-01-24  5:45         ` Marlon Richert
@ 2024-01-24  8:53           ` Mikael Magnusson
  2024-01-24 13:37             ` Marlon Richert
  0 siblings, 1 reply; 13+ messages in thread
From: Mikael Magnusson @ 2024-01-24  8:53 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Bart Schaefer, Roman Perepelitsa, Zsh Users

On 1/24/24, Marlon Richert <marlon.richert@gmail.com> wrote:
>> On 23. Jan 2024, at 22.21, Mikael Magnusson <mikachu@gmail.com> wrote:
>>
>> On 1/23/24, Bart Schaefer <schaefer@brasslantern.com> wrote:
>>>> On Tue, Jan 23, 2024 at 10:44 AM Marlon Richert
>>>> <marlon.richert@gmail.com> wrote:
>>>>
>>>> However, the quoting seems to make it impossible to use $psvar for
>>>> anything involving control characters.
>>>
>>> That's correct.  Anything involving control characters would also
>>> require %{...%} or %G to specify the occupied screen width.
>>>
>>>> Is there no way to work around this?
>>>
>>> As Roman mentioned, the usual way is to use precmd to update prompt
>>> values.
>>
>> fwiw, you can do things like %F{%5v}hello or %(3V.%B.) to make things
>> dynamic without using promptsubst or updating PS1 in precmd, but you
>> do have to have all the actual formatting in PS1 then.
>>
> Thanks, I think that might work for me.
>
> I am indeed trying to use $psvar to dynamically update my prompt’s visual
> formatting, without the use of prompsubst or changing $PS1.

Thinking about this made me realize that namerefs can be quite useful here, eg
typeset -n .prompt.logicaldescription='psvar[3]'
then you don't have to remember what number you decided to use for
what (I always forget what I picked). And it makes code using them
slightly more self descriptive.

-- 
Mikael Magnusson


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

* Re: Why are prompt expansions of %v sequences quoted in bindkey style?
  2024-01-24  8:53           ` Mikael Magnusson
@ 2024-01-24 13:37             ` Marlon Richert
  2024-01-24 19:35               ` Mikael Magnusson
  0 siblings, 1 reply; 13+ messages in thread
From: Marlon Richert @ 2024-01-24 13:37 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Bart Schaefer, Roman Perepelitsa, Zsh Users

[-- Attachment #1: Type: text/plain, Size: 494 bytes --]

On Wed, Jan 24, 2024 at 10:53 AM Mikael Magnusson <mikachu@gmail.com> wrote:

>
> Thinking about this made me realize that namerefs can be quite useful
> here, eg
> typeset -n .prompt.logicaldescription='psvar[3]'
> then you don't have to remember what number you decided to use for
> what (I always forget what I picked). And it makes code using them
> slightly more self descriptive.
>

Can you give an example of how you would use this in practice? I'm not sure
I understand.

[-- Attachment #2: Type: text/html, Size: 1053 bytes --]

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

* Re: Why are prompt expansions of %v sequences quoted in bindkey style?
  2024-01-24 13:37             ` Marlon Richert
@ 2024-01-24 19:35               ` Mikael Magnusson
  2024-01-25  8:42                 ` Marlon Richert
  0 siblings, 1 reply; 13+ messages in thread
From: Mikael Magnusson @ 2024-01-24 19:35 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Bart Schaefer, Roman Perepelitsa, Zsh Users

On 1/24/24, Marlon Richert <marlon.richert@gmail.com> wrote:
> On Wed, Jan 24, 2024 at 10:53 AM Mikael Magnusson <mikachu@gmail.com>
> wrote:
>
>>
>> Thinking about this made me realize that namerefs can be quite useful
>> here, eg
>> typeset -n .prompt.logicaldescription='psvar[3]'
>> then you don't have to remember what number you decided to use for
>> what (I always forget what I picked). And it makes code using them
>> slightly more self descriptive.
>>
>
> Can you give an example of how you would use this in practice? I'm not sure
> I understand.

I have a section in my prompt only shows if psvar[9] contains some string:
{5935|20:34:22|~}% psvar[9]=hello
{5936|20:34:22|~}(hello)%

but with the nameref i can do this:
{5937|20:34:47|~}% .prompt.currenttask=hi\ there
{5938|20:34:47|~}(hi there)%

which is easier (with tabcompletion). It also makes the code that sets
.prompt.gitbranch a bit clearer.

-- 
Mikael Magnusson


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

* Re: Why are prompt expansions of %v sequences quoted in bindkey style?
  2024-01-24 19:35               ` Mikael Magnusson
@ 2024-01-25  8:42                 ` Marlon Richert
  2024-01-25  9:20                   ` Lawrence Velázquez
  0 siblings, 1 reply; 13+ messages in thread
From: Marlon Richert @ 2024-01-25  8:42 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Bart Schaefer, Roman Perepelitsa, Zsh Users

This is the first time I’ve seen `typeset -n`. I didn’t know such a feature exists.

Is this documented anywhere? I’m unable to find it in the typeset documentation. I’m not able to find it by searching for “nameref” either.

> On 24. Jan 2024, at 21.35, Mikael Magnusson <mikachu@gmail.com> wrote:
> 
> On 1/24/24, Marlon Richert <marlon.richert@gmail.com> wrote:
>>> On Wed, Jan 24, 2024 at 10:53 AM Mikael Magnusson <mikachu@gmail.com>
>>> wrote:
>>> 
>>> 
>>> Thinking about this made me realize that namerefs can be quite useful
>>> here, eg
>>> typeset -n .prompt.logicaldescription='psvar[3]'
>>> then you don't have to remember what number you decided to use for
>>> what (I always forget what I picked). And it makes code using them
>>> slightly more self descriptive.
>>> 
>> 
>> Can you give an example of how you would use this in practice? I'm not sure
>> I understand.
> 
> I have a section in my prompt only shows if psvar[9] contains some string:
> {5935|20:34:22|~}% psvar[9]=hello
> {5936|20:34:22|~}(hello)%
> 
> but with the nameref i can do this:
> {5937|20:34:47|~}% .prompt.currenttask=hi\ there
> {5938|20:34:47|~}(hi there)%
> 
> which is easier (with tabcompletion). It also makes the code that sets
> .prompt.gitbranch a bit clearer.
> 
> --
> Mikael Magnusson


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

* Re: Why are prompt expansions of %v sequences quoted in bindkey style?
  2024-01-25  8:42                 ` Marlon Richert
@ 2024-01-25  9:20                   ` Lawrence Velázquez
  2024-01-25 16:16                     ` Mikael Magnusson
  0 siblings, 1 reply; 13+ messages in thread
From: Lawrence Velázquez @ 2024-01-25  9:20 UTC (permalink / raw)
  To: Marlon Richert
  Cc: Mikael Magnusson, Bart Schaefer, Roman Perepelitsa, zsh-users

On Thu, Jan 25, 2024, at 3:42 AM, Marlon Richert wrote:
> This is the first time I’ve seen `typeset -n`. I didn’t know such a 
> feature exists.
>
> Is this documented anywhere? I’m unable to find it in the typeset 
> documentation. I’m not able to find it by searching for “nameref” 
> either.

The namerefs feature is implemented on the master branch but hasn't
been released yet.

-- 
vq


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

* Re: Why are prompt expansions of %v sequences quoted in bindkey style?
  2024-01-25  9:20                   ` Lawrence Velázquez
@ 2024-01-25 16:16                     ` Mikael Magnusson
  0 siblings, 0 replies; 13+ messages in thread
From: Mikael Magnusson @ 2024-01-25 16:16 UTC (permalink / raw)
  To: Lawrence Velázquez
  Cc: Marlon Richert, Bart Schaefer, Roman Perepelitsa, zsh-users

On 1/25/24, Lawrence Velázquez <larryv@zsh.org> wrote:
> On Thu, Jan 25, 2024, at 3:42 AM, Marlon Richert wrote:
>> This is the first time I’ve seen `typeset -n`. I didn’t know such a
>> feature exists.
>>
>> Is this documented anywhere? I’m unable to find it in the typeset
>> documentation. I’m not able to find it by searching for “nameref”
>> either.
>
> The namerefs feature is implemented on the master branch but hasn't
> been released yet.

My bad, I wasn't thinking about that.

-- 
Mikael Magnusson


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

end of thread, other threads:[~2024-01-25 16:18 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-23 11:03 Why are prompt expansions of %v sequences quoted in bindkey style? Marlon Richert
2024-01-23 13:05 ` Roman Perepelitsa
2024-01-23 18:43   ` Marlon Richert
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

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).