help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: re-evaluating prompt strings from a bind -x function


From: Grisha Levit
Subject: Re: re-evaluating prompt strings from a bind -x function
Date: Fri, 6 Oct 2023 22:14:30 -0400

On Sun, Oct 1, 2023, 20:54 Christoph Anton Mitterer <calestyo@scientia.org>
wrote:

> III) I need to place three additional functions in the users
>      environment:
>        _mycdwidget(), _save() and _restore()
>      as well as the variable
>        BINDTMP
>      Of course I can choose better names, less likely to collide, but
>      still.
>

You should just use prefixed names for functions and global variables and
not worry about collisions. Like __fzf_save_bindings, etc.

     So is there any good way to store/restore the previous state of
>      functions and variables, including whether set or unset, any
>      attributes (like readonly or local). Also if the variable is
>      an array?
>

If the variable is readonly you cannot change it in the first place so any
question about restoring it is moot. And since we're talking about a
function invoked directly from a `bind -x` binding, it's not possible for
the variable to be local to any other function either.

     _mycdwidget() could save the previous state of BINDTMP, _save()
>      and _restore() inside BINDTMP itself (being made an array).
>      Something like:
>        BINDTMP=("$(declare -p BINDTMP 2>/dev/null)" "$(declare -f _save
> 2>/dev/null)" "$(declare -f _restore 2>/dev/null)" '')


Again, I don't think this is necessary for the present application (for
which it is completely appropriate to use prefixes for namespacing) but, if
you needed to do such a thing, your approach will work. The only thing
missing is a `-p` flag for `declare -f`, which is needed to make sure
function attributes (export, trace) are preserved as well.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]