I would like to slightly extend a zsh completion function.
I would like to avoid putting the complete function body into my homedir with only one line changed. Instead I would like to intercept it’s call and then call the original function myself. In quasi code:
<make sure _the_original_function is loaded>
_backup_of_original_function=_the_original_function
_the_original_function() {
_arguments "-superduper[that one option I always need]"
_backup_of_originial_function <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="587c18">[email protected]</a>
}
In my concrete case, I have an cmake property in practically all my projects such that I would like to modify the cmake completion. Not having that option in the completion would annoy me much more than having it in projects where the option does not belong. So instead of copying _cmake somewhere I would just redefine _cmake_define_common_property_names() in my .zshrc:
_cmake_define_common_property_names() {
local properties; properties=(
'MY_GREAT_OPTION:be awesome'
)
_describe -t 'my-property-names' 'my property name' properties <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="466206">[email protected]</a>
**call original _cmake_define_common_property_names here
}
So what’s missing is loading _cmake_define_common_property_names and assigning a new function name to it.
From here I tried
autoload -Uz +X _cmake_define_common_property_names
but this fails (the function is not defined within its own file, but rather in _cmake.
NB: I don’t assign a new function name to avoid having to modify the place from where the function gets called in its original version.
What works partially is autoload -Uz +X _cmake BUT this only ensures that _cmake is loaded (which I verify by calling functions _cmake). It does not load the helper function _cmake_define_common_property_names.
So my two questions are
- how do I load a function from within an
$fpathfile - Once I have a function loaded. How do I copy it in a script / Assign a new function name?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
How to patch a function
The code of a function is stored in the associative array functions. That’s the source code with normalized whitespace and no comments (zsh has done lexical analysis and pretty-prints the tokens). You can change the code of a function by modifying the entry of the functions array. For example, to add extra code at the beginning:
functions[_cmake_define_common_property_names]="
… # your extra code here
$functions[_cmake_define_common_property_names]"
You can also use the functions array to copy a function to another name. This is the easiest way to wrap around an existing function.
functions[_cmake_define_common_property_names_orig]=$functions[_cmake_define_common_property_names]
_cmake_define_common_property_names () {
…
_cmake_define_common_property_names_orig "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="183c58">[email protected]</a>"
}
Loading all the functions
The only sure-fire way to load all the functions from a file made for autoload is to execute the function, if you can arrange to execute it with no side effects. For a completion function, just run the function with errors redirected to the bit bucket. It’ll do nothing but complain that it isn’t being executed in a completion context.
_cmake 2>/dev/null # Now _cmake_xxx is defined
The reason autoload -Uz +X _cmake doesn’t work is that the definitions of the auxiliary functions are in the _cmake function itself.
% echo $functions[_cmake]
builtin autoload -XU
% autoload -Uz +X _cmake
% echo $functions[_cmake]
…
(( $+functions[_cmake_define_property_names] )) || _cmake_define_property_names () {
…
}
…
local cmake_command_actions
cmake_command_actions=('-E[CMake command mode]:*:command')
_cmake_command () {
_arguments -C -s - command "$cmake_command_actions[@]"
}
local cmake_suggest_build
cmake_suggest_build=('--build[build]')
if [ $CURRENT -eq 2 ]
then
_arguments -C -s - help "$cmake_help_actions[@]" - command "$cmake_command_actions[@]" - build_opts "$cmake_build_options[@]" - build_cmds "$cmake_suggest_build[@]" && return 0
elif [[ $words[2] = --help* ]]
then
_cmake_help
elif [[ $words[2] != -E ]]
then
_cmake_on_build
else
_cmake_command
fi
If you really don’t want to execute the toplevel function, you have several choices:
- Patch the definition of
_cmake_define_common_property_namesinside the definition of_cmake. - Extract the definition of
_cmake_define_common_property_namesfrom the definition of_cmakeand use that to define_cmake_define_common_property_names_origor_cmake_define_common_property_names. -
Patch the definition of
_cmaketo remove the parts other than the function definitions, then execute it. It isn’t really workable with_cmake, but some other completion functions are better structured. For example_cvsconsists purely of conditional function definitions (e.g.(( $+functions[_cvs_command] )) || _cvs_command () { … }), a definition of the titular function, and a call of the titular function as the very last thing, thus you can define all the function but not execute anything by removing the last line.autoload -Uz +X _cvs functions[_cvs]=${functions_cvs%'$n'*} _cvs # Patch auxiliary functions here
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0