Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Methods of shadowing a builtin call



On Jan 29, 10:33pm, Sebastian Gniazdowski wrote:
}
} I shadow setopt to gather data of what is being done in sourced
} script.

As I alluded to in an earlier message, there is *no* effective way
to shadow "setopt" because of the way "localoptions" works.  If you
have

    --shadow-setopt() { builtin setopt "$@" }

and you execute e.g.

    --shadow-setopt localoptions shglob shwordsplit

then when --shadow-setopt exits, shglob and shwordplit will revert
to their previous settings and the whole thing is a no-op.  The same
thing will happen if you have

    emulate -L zsh
    --shadow-setopt shglob shwordsplit

because "emulate -L" turns on "localoptions".

And if you try to avoid that with this:

    --shadow-setopt() { unsetopt localoptions; builtin setopt "$@" }

then

    some_func() {
	--shadow-setopt localoptions shglob shwordsplit
    }
    some_func

will incorrectly leave shglob and shwordsplit turned *on* after the
function completes, because localoptions will never be seen in the
context of some_func.

The only way to track option changes is by examining deltas of values
in the $options hash.

} I currently use alias setopt=--setopt-shadow and then source a
} script. This works fine except for z-sy-h. For that project, when I
} source it with the alias being in place, interiors of it then still
} use the shadowing function during their later operations. How to
} suppress this?

It depends how z-sy-h declares or defines its functions.  If they are
declared with "autoload" but the -U option is not being used, then any
aliases defined at the time the function is first executed will expand
(because parsing and execution are effectively simultaneous, see next
sentence).

If instead the functions are fully defined, body and all, "in-line" in
the z-sy-h source files, there is no way to suppress this, because the
aliases expand when the function is *parsed*, and therefore the expanded
command text is already in place later when it is executed.



Messages sorted by: Reverse Date, Date, Thread, Author