2019-12-01 16:00:47 -05:00
zmodule() {
2020-01-13 13:14:31 -05:00
local -r zusage="Usage: %B${0}%b <url> [%B-n%b|%B--name%b <module_name>] [options]
2019-12-01 16:00:47 -05:00
2020-05-02 19:47:38 -04:00
Add %Bzmodule%b calls to your %B${ZDOTDIR:-${HOME}}/.zimrc%b file to define the modules to be initialized.
The modules are initialized in the same order they are defined.
2020-10-11 13:24:01 -04:00
<url> Module absolute path or repository URL. The following URL formats
are equivalent: %Bname%b, %Bzimfw/name%b, %Bhttps://github.com/zimfw/name.git%b.
%B-n%b|%B--name%b <module_name> Set a custom module name. Default: the last component in the <url>.
2021-07-11 16:58:15 -04:00
Use slashes inside the name to organize the module into subdirecto-
ries.
2020-05-02 19:47:38 -04:00
2019-12-01 16:00:47 -05:00
Repository options:
2020-10-11 13:24:01 -04:00
%B-b%b|%B--branch%b <branch_name> Use specified branch when installing and updating the module.
2021-01-02 19:09:52 -05:00
Overrides the tag option. Default: the repository's default branch.
2020-10-11 13:24:01 -04:00
%B-t%b|%B--tag%b <tag_name> Use specified tag when installing and updating the module.
Overrides the branch option.
2021-07-11 16:58:15 -04:00
%B-u%b|%B--use%b <%%Bgit%b|%Bdegit%b> Install and update the module using the defined tool. Default is
defined by %Bzstyle ':zim:zmodule' use '%b<%%Bgit%b|%Bdegit%b>%B'%b, or %Bgit%b if none
is provided.
2021-08-25 17:09:10 -04:00
%Bgit%b requires git itself. Local changes are preserved during updates.
2021-08-10 10:56:25 -04:00
%Bdegit%b requires curl or wget, and currently only works with GitHub
URLs. Modules install faster and take less disk space. Local changes
are lost during updates. Git submodules are not supported.
2021-07-01 19:06:20 -04:00
%B-z%b|%B--frozen%b Don't install or update the module.
2019-12-01 16:00:47 -05:00
2020-05-02 19:47:38 -04:00
Initialization options:
2020-10-11 13:24:01 -04:00
%B-f%b|%B--fpath%b <path> Add specified path to fpath. The path is relative to the module
root directory. Default: %Bfunctions%b, if the subdirectory exists.
%B-a%b|%B--autoload%b <func_name> Autoload specified function. Default: all valid names inside the
module's specified fpath paths.
%B-s%b|%B--source%b <file_path> Source specified file. The file path is relative to the module root
2021-03-19 19:44:53 -04:00
directory. Default: %Binit.zsh%b, if the %Bfunctions%b subdirectory also
exists, or the file with largest size matching
2020-10-11 13:24:01 -04:00
%B{init.zsh,module_name.{zsh,plugin.zsh,zsh-theme,sh}}%b, if any exist.
%B-c%b|%B--cmd%b <command> Execute specified command. Occurrences of the %B{}%b placeholder in the
command are substituted by the module root directory path.
2021-07-11 16:58:15 -04:00
I.e., %B-s 'script.zsh'%b and %B-c 'source {}/script.zsh'%b are equivalent.
2021-07-01 19:06:20 -04:00
%B-d%b|%B--disabled%b Don't initialize or uninstall the module."
2019-12-01 16:00:47 -05:00
if [[ ${${funcfiletrace[1]%:*}:t} != .zimrc ]]; then
2020-01-13 13:14:31 -05:00
print -u2 -PR "%F{red}${0}: Must be called from %B<%= home %>/.zimrc%b%f"$'\n\n'${zusage}
2021-03-19 18:13:21 -04:00
return 2
2019-12-01 16:00:47 -05:00
fi
if (( ! # )); then
2020-05-25 15:01:00 -04:00
print -u2 -PR "%F{red}<%= error %>${funcfiletrace[1]}: Missing zmodule url%f"$'\n\n'${zusage}
2019-12-01 16:00:47 -05:00
_zfailed=1
2021-03-19 18:13:21 -04:00
return 2
2019-12-01 16:00:47 -05:00
fi
2019-12-12 09:00:19 -05:00
setopt LOCAL_OPTIONS CASE_GLOB EXTENDED_GLOB
2021-07-11 16:58:15 -04:00
local zurl=${1} zmodule=${1:t} ztool zdir ztype zrev zarg
2019-12-01 16:00:47 -05:00
local -i zdisabled=0 zfrozen=0
2020-07-02 18:16:44 -04:00
local -a zfpaths zfunctions zcmds
2021-07-11 16:58:15 -04:00
zstyle -s ':zim:zmodule' use 'ztool' || ztool=git
2019-12-01 16:00:47 -05:00
if [[ ${zurl} =~ ^[^:/]+: ]]; then
zmodule=${zmodule%.git}
elif [[ ${zurl} != /* ]]; then
# Count number of slashes
case ${#zurl//[^\/]/} in
0) zurl="https://github.com/zimfw/${zurl}.git" ;;
1) zurl="https://github.com/${zurl}.git" ;;
esac
fi
shift
if [[ ${1} == (-n|--name) ]]; then
if (( # < 2 )); then
2020-05-25 15:01:00 -04:00
print -u2 -PR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zmodule}:%b Missing argument for zmodule option ${1}%f"$'\n\n'${zusage}
2019-12-01 16:00:47 -05:00
_zfailed=1
2021-03-19 18:13:21 -04:00
return 2
2019-12-01 16:00:47 -05:00
fi
shift
2021-06-17 21:01:13 -04:00
zmodule=${${1%%/##}##/##}
2019-12-01 16:00:47 -05:00
shift
fi
2020-06-05 23:09:23 -04:00
if [[ ${zurl} == /* ]]; then
2021-06-17 21:01:13 -04:00
zdir=${zurl%%/##}
2020-06-05 23:09:23 -04:00
else
zdir=${ZIM_HOME}/modules/${zmodule}
fi
2019-12-01 16:00:47 -05:00
while (( # > 0 )); do
case ${1} in
2021-04-03 10:35:28 -04:00
-b|--branch|-t|--tag|-u|--use|-f|--fpath|-a|--autoload|-s|--source|-c|--cmd)
2019-12-01 16:00:47 -05:00
if (( # < 2 )); then
2020-05-25 15:01:00 -04:00
print -u2 -PR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zmodule}:%b Missing argument for zmodule option ${1}%f"$'\n\n'${zusage}
2019-12-01 16:00:47 -05:00
_zfailed=1
2021-03-19 18:13:21 -04:00
return 2
2019-12-01 16:00:47 -05:00
fi
;;
esac
case ${1} in
-b|--branch)
shift
ztype=branch
zrev=${1}
;;
-t|--tag)
shift
ztype=tag
zrev=${1}
;;
2021-04-03 10:35:28 -04:00
-u|--use)
shift
ztool=${1}
;;
2019-12-01 16:00:47 -05:00
-z|--frozen) zfrozen=1 ;;
-f|--fpath)
shift
zarg=${1}
2020-02-03 20:43:36 -05:00
if [[ ${zarg} != /* ]] zarg=${zdir}/${zarg}
2019-12-01 16:00:47 -05:00
zfpaths+=(${zarg})
;;
-a|--autoload)
shift
zfunctions+=(${1})
;;
-s|--source)
shift
zarg=${1}
2020-02-03 20:43:36 -05:00
if [[ ${zarg} != /* ]] zarg=${zdir}/${zarg}
2020-07-02 18:16:44 -04:00
zcmds+=("source ${zarg:A}")
;;
-c|--cmd)
shift
zcmds+=(${1//{}/${zdir:A}})
2019-12-01 16:00:47 -05:00
;;
-d|--disabled) zdisabled=1 ;;
*)
2020-05-25 15:01:00 -04:00
print -u2 -PR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zmodule}:%b Unknown zmodule option ${1}%f"$'\n\n'${zusage}
2019-12-01 16:00:47 -05:00
_zfailed=1
2021-03-19 18:13:21 -04:00
return 2
2019-12-01 16:00:47 -05:00
;;
esac
shift
done
2021-04-03 10:35:28 -04:00
if [[ -n ${_zargs_action} ]]; then
2019-12-03 07:07:59 -05:00
if (( ! zfrozen )); then
2021-04-03 10:35:28 -04:00
_zmodules_zargs+=("${ztool}" "${_zargs_action}" "${zmodule}" "${zdir}" "${zurl}" "${ztype}" "${zrev}")
2019-12-01 16:00:47 -05:00
fi
2019-12-03 07:07:59 -05:00
else
if (( zdisabled )); then
_zdisableds+=(${zmodule})
else
2019-12-07 21:17:40 -05:00
if [[ ! -d ${zdir} ]]; then
2020-05-02 10:05:11 -04:00
print -u2 -PR "%F{red}<%= error %>${funcfiletrace[1]}:%B${zmodule}:%b Not installed. Run %Bzimfw install%b to install.%f"
2019-12-07 21:17:40 -05:00
_zfailed=1
return 1
fi
2021-07-11 16:58:15 -04:00
local -ra prezto_fpaths=(${zdir}/functions(NF)) prezto_scripts=(${zdir}/init.zsh(N))
2021-03-16 18:04:40 -04:00
if (( ! ${#zfpaths} && ! ${#zcmds} && ${#prezto_fpaths} && ${#prezto_scripts} )); then
# this follows the prezto module format, no need to check for other scripts
zfpaths=(${prezto_fpaths})
zcmds=("source ${^prezto_scripts[@]:A}")
else
if (( ! ${#zfpaths} )) zfpaths=(${prezto_fpaths})
if (( ! ${#zcmds} )); then
# get script with largest size (descending `O`rder by `L`ength, and return only `[1]` first)
2021-08-10 10:56:25 -04:00
local -ra zscripts=(${zdir}/(init.zsh|${zmodule:t}.(zsh|plugin.zsh|zsh-theme|sh))(NOL[1]))
zcmds=("source ${^zscripts[@]:A}")
2021-03-16 18:04:40 -04:00
fi
fi
2019-12-03 07:07:59 -05:00
if (( ! ${#zfunctions} )); then
# _* functions are autoloaded by compinit
# prompt_*_setup functions are autoloaded by promptinit
2021-03-16 18:04:40 -04:00
zfunctions=(${^zfpaths}/^(*~|*.zwc(|.old)|_*|prompt_*_setup)(N-.:t))
2019-12-03 07:07:59 -05:00
fi
2020-07-02 18:16:44 -04:00
if (( ! ${#zfpaths} && ! ${#zfunctions} && ! ${#zcmds} )); then
2020-05-25 15:01:00 -04:00
print -u2 -PR "%F{yellow}<%= warn %>${funcfiletrace[1]}:%B${zmodule}:%b Nothing found to be initialized. Customize the module name or initialization with %Bzmodule%b options.%f"$'\n\n'${zusage}
fi
2020-07-02 18:16:44 -04:00
_zmodules+=(${zmodule})
_zdirs+=(${zdir})
2019-12-03 07:07:59 -05:00
_zfpaths+=(${zfpaths})
_zfunctions+=(${zfunctions})
2020-07-02 18:16:44 -04:00
_zcmds+=(${zcmds})
2019-12-01 16:00:47 -05:00
fi
fi
}