1
0
Fork 0
mirror of synced 2024-11-17 05:25:34 -05:00
zimfw/init.zsh
Eric Nielsen 8dc3e43a0d
Add a plugin mechanism \o/
This is a major change, where Zsh modules/plugins are not git submodules
in the Zim repo anymore, but customized and installed separately as
individual repositories. The discussion about this started more than 2
years ago in #88. Closes #299.

This will allow contributors' modules to live in their own repositories.
See #33, #138, #262, #281, #324.

The current code has what, up to this point, I considered to be the best
balance between simplicity, execution speed and number of files.

One measured decision was to make the initialization of modules depend
only on the `':zim' modules` style, keeping it as fast as possible.
The `':zim:module' module` style is used to install, update and clean
the modules, all operations that happen after the user got his
as-blazing-fast-possible shell prompt.

Even though I didn't care much about making install or update fast,
`xargs` has a nice feature of allowing commands to be executed in
parallel with `-P`. I took advantage of that.

I've also worked on making the `zimfw` utility give the user some nice
(while still minimalistic) output. Also I'm suggesting this as the new
name for the `zmanage` tool, since `zimfw` does not shadow the `zim`
wiki tool.
2020-01-02 12:58:00 -05:00

122 lines
3.6 KiB
Bash

autoload -Uz is-at-least && if ! is-at-least 5.2; then
print "init: error starting Zim: You're using Zsh version ${ZSH_VERSION} and versions < 5.2 are not supported. Update your Zsh." >&2
return 1
fi
# Define Zim location
: ${ZIM_HOME=${0:h}}
# Source user configuration
[[ -f ${ZDOTDIR:-${HOME}}/.zimrc ]] && source ${ZDOTDIR:-${HOME}}/.zimrc
# Set input mode before loading modules
if zstyle -t ':zim:input' mode 'vi'; then
bindkey -v
else
bindkey -e
fi
# Autoload enabled modules' functions
() {
local zfunction
local -a zmodules
zstyle -a ':zim' modules 'zmodules'
setopt LOCAL_OPTIONS EXTENDED_GLOB
fpath=(${ZIM_HOME}/modules/${^zmodules}/functions(/FN) ${fpath})
for zfunction in ${ZIM_HOME}/modules/${^zmodules}/functions/^(_*|*.*|prompt_*_setup)(-.N:t); do
autoload -Uz ${zfunction}
done
}
# Source enabled modules' init scripts
() {
local zmodule zdir zfile
local -a zmodules
zstyle -a ':zim' modules 'zmodules'
for zmodule in ${zmodules}; do
zdir=${ZIM_HOME}/modules/${zmodule}
if [[ ! -d ${zdir} ]]; then
print "init: module ${zmodule} not installed" >&2
elif [[ -f ${zdir}/prompt_${zmodule}_setup ]]; then
fpath=(${zdir} ${fpath}) # Will be loaded by promptinit
else
for zfile in ${zdir}/init.zsh ${zdir}/${zmodule}.{zsh,plugin.zsh,zsh-theme,sh}; do
if [[ -f ${zfile} ]]; then
source ${zfile}
break
fi
done
fi
done
}
_zimfw_compile() {
setopt LOCAL_OPTIONS EXTENDED_GLOB
autoload -U zrecompile
local zdir zfile
local -a zmodules
zstyle -a ':zim' modules 'zmodules'
# Compile the completion cache; significant speedup
local zdumpfile
zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile="${ZDOTDIR:-${HOME}}/.zcompdump"
[[ -f ${zdumpfile} ]] && zrecompile -p ${1} ${zdumpfile}
# Compile .zshrc
zrecompile -p ${1} ${ZDOTDIR:-${HOME}}/.zshrc
# Compile enabled modules' autoloaded functions
for zdir in ${ZIM_HOME}/modules/${^zmodules}/functions(/FN); do
zrecompile -p ${1} ${zdir}.zwc ${zdir}/^(_*|*.*|prompt_*_setup)(-.N)
done
# Compile enabled modules' scripts
for zfile in ${ZIM_HOME}/modules/${^zmodules}/(^*test*/)#{*.zsh{,-theme},prompt_*_setup}(.NLk+1); do
zrecompile -p ${1} ${zfile}
done
# Compile this script
zrecompile -p ${1} ${ZIM_HOME}/init.zsh
if [[ ${1} != -q ]]; then
print -P '%F{green}✓%f Done with compile.'
fi
}
zimfw() {
if [[ ${#} -ne 1 && ${2} != -q ]]; then
source ${ZIM_HOME}/tools/usage.zsh
return 1
fi
case ${1} in
clean)
source ${ZIM_HOME}/tools/clean-modules.zsh ${2} && \
source ${ZIM_HOME}/tools/clean-compiled.zsh ${2} && \
source ${ZIM_HOME}/tools/clean-dumpfile.zsh ${2}
;;
clean-modules) source ${ZIM_HOME}/tools/clean-modules.zsh ${2} ;;
clean-compiled) source ${ZIM_HOME}/tools/clean-compiled.zsh ${2} ;;
clean-dumpfile) source ${ZIM_HOME}/tools/clean-dumpfile.zsh ${2} ;;
compile|login-init) _zimfw_compile ${2} ;;
info) source ${ZIM_HOME}/tools/info.zsh ${2} ;;
install|update)
# Source .zimrc to refresh zmodules
[[ -f ${ZDOTDIR:-${HOME}}/.zimrc ]] && source ${ZDOTDIR:-${HOME}}/.zimrc
source ${ZIM_HOME}/tools/modules.zsh ${2} | xargs -L1 -P10 zsh ${ZIM_HOME}/tools/${1}.zsh && \
if [[ ${2} != -q ]]; then
print -P "%F{green}✓%f Done with ${1}. Restart your terminal for any changes to take effect."
fi
;;
upgrade)
zsh ${ZIM_HOME}/tools/update.zsh 'https://github.com/zimfw/zimfw.git' ${ZIM_HOME} branch develop ${2}
;;
*)
source ${ZIM_HOME}/tools/usage.zsh
return 1
;;
esac
}