zsh-users
 help / color / mirror / code / Atom feed
* Truncate $functions
@ 2016-02-15  7:55 Sebastian Gniazdowski
  2016-02-15 17:11 ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Sebastian Gniazdowski @ 2016-02-15  7:55 UTC (permalink / raw)
  To: Zsh Users

Hello,
tried functions=( a b ), set -A functions ( a b ), unset functions but
all don't truncate functions.

Test on IRC bot:

>> myf() { echo "test"; }; echo $parameters[functions]; unset functions; functions=( a b c d ); echo $parameters[functions]; echo ${#functions}
association-hide-hideval-special
association-hide-hideval-special
3

Maybe there is a way to do this? I would want to reload all functions,
first serializing them, with say declare -f. Unsetting function
entries one by one with "unset 'functions[entry]'" isn't fully
foolproof because functions named e.g. opp+a[ cannot be unset this
way.

Maybe there is a way to stop unset[$i] with i='opp+a[ ' from stopping
script? I would then fallback to define the function as empty.

Best regards,
Sebastian Gniazdowski


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

* Re: Truncate $functions
  2016-02-15  7:55 Truncate $functions Sebastian Gniazdowski
@ 2016-02-15 17:11 ` Bart Schaefer
  2016-02-15 19:31   ` Sebastian Gniazdowski
  2016-02-16  4:56   ` Bart Schaefer
  0 siblings, 2 replies; 9+ messages in thread
From: Bart Schaefer @ 2016-02-15 17:11 UTC (permalink / raw)
  To: Zsh Users

On Feb 15,  8:55am, Sebastian Gniazdowski wrote:
}
} tried functions=( a b ), set -A functions ( a b ), unset functions but
} all don't truncate functions.

The functions array is special, you can't muck with it that way.  You need

unfunction -m \*


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

* Re: Truncate $functions
  2016-02-15 17:11 ` Bart Schaefer
@ 2016-02-15 19:31   ` Sebastian Gniazdowski
  2016-02-15 19:43     ` Bart Schaefer
  2016-02-16  4:51     ` Bart Schaefer
  2016-02-16  4:56   ` Bart Schaefer
  1 sibling, 2 replies; 9+ messages in thread
From: Sebastian Gniazdowski @ 2016-02-15 19:31 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

Thank you, this works really well. Side note, unset functions[...] has
problems with entries like ops+[, but unfunction doesn't.

Best regards,
Sebastian Gniazdowski


On 15 February 2016 at 18:11, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Feb 15,  8:55am, Sebastian Gniazdowski wrote:
> }
> } tried functions=( a b ), set -A functions ( a b ), unset functions but
> } all don't truncate functions.
>
> The functions array is special, you can't muck with it that way.  You need
>
> unfunction -m \*
>


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

* Re: Truncate $functions
  2016-02-15 19:31   ` Sebastian Gniazdowski
@ 2016-02-15 19:43     ` Bart Schaefer
  2016-02-16  4:51     ` Bart Schaefer
  1 sibling, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 2016-02-15 19:43 UTC (permalink / raw)
  To: Zsh Users

On Feb 15,  8:31pm, Sebastian Gniazdowski wrote:
}
} Side note, unset functions[...] has
} problems with entries like ops+[, but unfunction doesn't.

That's because unfunction doesn't have to parse the function name as an
array subscript.

Git commit 95663e9 should have taken care of the issue with unset.  You
just have to use

    x='ops+['
    unset "functions[${(b)x}]"


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

* Re: Truncate $functions
  2016-02-15 19:31   ` Sebastian Gniazdowski
  2016-02-15 19:43     ` Bart Schaefer
@ 2016-02-16  4:51     ` Bart Schaefer
  2016-02-16  8:05       ` Sebastian Gniazdowski
  1 sibling, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2016-02-16  4:51 UTC (permalink / raw)
  To: Zsh Users

On Feb 15,  8:31pm, Sebastian Gniazdowski wrote:
}
} Thank you, this works really well.

As an additional note -- functions that look like

    sample () {
	# undefined
	builtin autoload -X
    }

should be restored as

    autoload sample

except that you can't know which of the -U -k -z options was specified
the first time the name was marked for autoload, so there's no way to
get this entirely right.

The other drawback to this scheme is that you can't save and restore
the "sticky emulation" bits of any of the functions.


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

* Re: Truncate $functions
  2016-02-15 17:11 ` Bart Schaefer
  2016-02-15 19:31   ` Sebastian Gniazdowski
@ 2016-02-16  4:56   ` Bart Schaefer
  2016-02-16  6:43     ` Sebastian Gniazdowski
  1 sibling, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2016-02-16  4:56 UTC (permalink / raw)
  To: Zsh Users

On Feb 15,  9:11am, Bart Schaefer wrote:
} Subject: Re: Truncate $functions
}
} On Feb 15,  8:55am, Sebastian Gniazdowski wrote:
} }
} } tried functions=( a b ), set -A functions ( a b ), unset functions but
} } all don't truncate functions.
} 
} The functions array is special, you can't muck with it that way.

I've just been prodding at this and "unset functions" removes the special
variable as expected -- to get it back I have to unload and reload the
zsh/parameter module.

Can you show an example of "unset functions" behaving differently?


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

* Re: Truncate $functions
  2016-02-16  4:56   ` Bart Schaefer
@ 2016-02-16  6:43     ` Sebastian Gniazdowski
  0 siblings, 0 replies; 9+ messages in thread
From: Sebastian Gniazdowski @ 2016-02-16  6:43 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

On 16 February 2016 at 05:56, Bart Schaefer <schaefer@brasslantern.com> wrote:
> I've just been prodding at this and "unset functions" removes the special
> variable as expected -- to get it back I have to unload and reload the
> zsh/parameter module.
>
> Can you show an example of "unset functions" behaving differently?

The variables reappear after assigning them and contain previous values:

# unfunction -m \*
# myf() { echo "test"; }; echo $parameters[functions]; unset
functions; functions=( a b c d ); echo $parameters[functions]; echo
${#functions}
association-hide-hideval-special
association-hide-hideval-special
3
# echo ${(k)functions}
a c myf

Three entries, after adding two with functions=( a b c d ).

Best regards,
Sebastian Gniazdowski


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

* Re: Truncate $functions
  2016-02-16  4:51     ` Bart Schaefer
@ 2016-02-16  8:05       ` Sebastian Gniazdowski
  2016-02-16 19:29         ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Sebastian Gniazdowski @ 2016-02-16  8:05 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

On 16 February 2016 at 05:51, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Feb 15,  8:31pm, Sebastian Gniazdowski wrote:
> }
> } Thank you, this works really well.
>
> As an additional note -- functions that look like
>
>     sample () {
>         # undefined
>         builtin autoload -X
>     }
>
> should be restored as
>
>     autoload sample

Indeed.. otherwise problems that the --reload-and-run function fixed
will appear. Or maybe not? Before, we were trying assignment to
functions or eval. Here it's plain script body. Wonder how I could
test for any problems quickly, will probably leave the issue for a
while

> except that you can't know which of the -U -k -z options was specified
> the first time the name was marked for autoload, so there's no way to
> get this entirely right.

Looking at define's -f output I have:

_a2ps () {
        # undefined
        builtin autoload -XUz
}
_a2utils () {
        # undefined
        builtin autoload -XUz
}
_aap () {
        # undefined
        builtin autoload -XUz
}

...

So it looks like I can have the options by parsing functions body?

> The other drawback to this scheme is that you can't save and restore
> the "sticky emulation" bits of any of the functions.

Slightly acceptable limitation, user will be told "don't change
emulate modes". Anyone doing emulate sh will break his session anyway
because of for example SHGLOB. That said, the snapshot could cover
emulate modes and restore them too..

Best regards,
Sebastian Gniazdowski


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

* Re: Truncate $functions
  2016-02-16  8:05       ` Sebastian Gniazdowski
@ 2016-02-16 19:29         ` Bart Schaefer
  0 siblings, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 2016-02-16 19:29 UTC (permalink / raw)
  To: Zsh Users

On Feb 16,  9:05am, Sebastian Gniazdowski wrote:
} Subject: Re: Truncate $functions
}
} Looking at define's -f output I have:
} 
} _a2ps () {
}         # undefined
}         builtin autoload -XUz
} }
} _a2utils () {
}         # undefined
}         builtin autoload -XUz
} }
} _aap () {
}         # undefined
}         builtin autoload -XUz
} }
} 
} ...
} 
} So it looks like I can have the options by parsing functions body?

Yes, that would work.  In fact you should parse for the "# undefined"
line anyway, to identify the autoloads.


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

end of thread, other threads:[~2016-02-16 19:29 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-15  7:55 Truncate $functions Sebastian Gniazdowski
2016-02-15 17:11 ` Bart Schaefer
2016-02-15 19:31   ` Sebastian Gniazdowski
2016-02-15 19:43     ` Bart Schaefer
2016-02-16  4:51     ` Bart Schaefer
2016-02-16  8:05       ` Sebastian Gniazdowski
2016-02-16 19:29         ` Bart Schaefer
2016-02-16  4:56   ` Bart Schaefer
2016-02-16  6:43     ` Sebastian Gniazdowski

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