2019-01-22 19:40:43 -05:00
# AUTOMATICALLY GENERATED FILE. EDIT ONLY THE SOURCE FILES AND THEN COMPILE.
# DO NOT DIRECTLY EDIT THIS FILE!
2019-12-08 20:03:45 -05:00
# MIT License
#
# Copyright (c) 2015-2016 Matt Hamilton and contributors
2022-01-25 17:19:34 -05:00
# Copyright (c) 2016-2022 Eric Nielsen, Matt Hamilton and contributors
2019-12-08 20:03:45 -05:00
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2019-01-22 19:40:43 -05:00
autoload -Uz is-at-least && if ! is-at-least 5.2; then
2020-01-13 13:14:31 -05:00
print -u2 -PR " %F{red} ${ 0 } : Error starting Zim. You're using Zsh version %B ${ ZSH_VERSION } %b and versions < %B5.2%b are not supported. Upgrade your Zsh.%f "
2019-01-22 19:40:43 -05:00
return 1
fi
2021-09-19 14:37:13 -04:00
autoload -Uz zargs
2019-01-22 19:40:43 -05:00
# Define Zim location
2021-08-31 19:41:26 -04:00
if ( ( ! ${ +ZIM_HOME } ) ) typeset -g ZIM_HOME = ${ 0 : A : h }
2019-01-22 19:40:43 -05:00
2020-01-09 07:13:31 -05:00
_zimfw_print( ) {
2020-02-03 20:43:36 -05:00
if ( ( _zprintlevel > 0 ) ) print " ${ @ } "
2020-01-09 07:13:31 -05:00
}
2019-12-14 22:21:34 -05:00
_zimfw_mv( ) {
2020-05-25 14:40:34 -04:00
local -a cklines
if cklines = ( ${ (f) " $( command cksum ${ 1 } ${ 2 } 2>/dev/null) " } ) && \
[ [ ${ ${ (z)cklines[1] } [1,2] } = = ${ ${ (z)cklines[2] } [1,2] } ] ] ; then
2020-01-09 07:13:31 -05:00
_zimfw_print -PR " %F{green})%f %B ${ 2 } :%b Already up to date "
2019-12-14 22:21:34 -05:00
else
if [ [ -e ${ 2 } ] ] ; then
command mv -f ${ 2 } { ,.old} || return 1
fi
2022-01-10 09:29:32 -05:00
command mv -f ${ 1 } ${ 2 } && _zimfw_print -PR " %F{green})%f %B ${ 2 } :%b Updated. ${ _zrestartmsg } "
2019-12-14 22:21:34 -05:00
fi
}
_zimfw_build_init( ) {
local -r ztarget = ${ ZIM_HOME } /init.zsh
# Force update of init.zsh if it's older than .zimrc
2022-10-23 20:34:14 -04:00
if [ [ ${ ztarget } -ot ${ ZIM_CONFIG_FILE :- ${ ZDOTDIR :- ${ HOME } } /.zimrc } ] ] ; then
2019-12-14 22:21:34 -05:00
command mv -f ${ ztarget } { ,.old} || return 1
fi
_zimfw_mv = (
print -R " zimfw() { source ${ ZIM_HOME } /zimfw.zsh \"\${@}\" } "
2021-09-20 22:14:12 -04:00
print -R " zmodule() { source ${ ZIM_HOME } /zimfw.zsh \"\${@}\" } "
2022-10-06 20:52:57 -04:00
local zroot_dir zpre
local -a zif_functions zif_cmds zroot_functions zroot_cmds
local -a zfunctions = ( ${ _zfunctions } ) zcmds = ( ${ _zcmds } )
# Keep fpath constant regardless of "if" root dirs, to avoid confusing compinit.
# Move all from zfunctions and zcmds with "if" root dirs prefixes.
for zroot_dir in ${ _zroot_dirs } ; do
if ( ( ${ +_zifs[ ${ zroot_dir } ] } ) ) ; then
zpre = ${ zroot_dir } $'\0'
zif_functions += ( ${ (M)zfunctions : # ${ zpre } * } )
zif_cmds += ( ${ (M)zcmds : # ${ zpre } * } )
zfunctions = ( ${ zfunctions : # ${ zpre } * } )
zcmds = ( ${ zcmds : # ${ zpre } * } )
fi
done
zpre = $'*\0'
print -R 'typeset -gr _zim_fpath=(' ${ ${ _zfpaths # ${ ~zpre } } : A } ')'
2022-01-18 20:34:36 -05:00
if ( ( ${# _zfpaths } ) ) print 'fpath=(${_zim_fpath} ${fpath})'
2022-10-06 20:52:57 -04:00
if ( ( ${# zfunctions } ) ) print -R 'autoload -Uz -- ' ${ zfunctions # ${ ~zpre } }
for zroot_dir in ${ _zroot_dirs } ; do
zpre = ${ zroot_dir } $'\0'
if ( ( ${ +_zifs[ ${ zroot_dir } ] } ) ) ; then
zroot_functions = ( ${ ${ (M)zif_functions : # ${ zpre } * } # ${ zpre } } )
zroot_cmds = ( ${ ${ (M)zif_cmds : # ${ zpre } * } # ${ zpre } } )
if ( ( ${# zroot_functions } || ${# zroot_cmds } ) ) ; then
print -R 'if ' ${ _zifs [ ${ zroot_dir } ] } '; then'
if ( ( ${# zroot_functions } ) ) print -R ' autoload -Uz -- ' ${ zroot_functions }
if ( ( ${# zroot_cmds } ) ) print -R ${ (F) :- ${ ^zroot_cmds } }
print fi
fi
else
zroot_cmds = ( ${ ${ (M)zcmds : # ${ zpre } * } # ${ zpre } } )
if ( ( ${# zroot_cmds } ) ) print -R ${ (F)zroot_cmds }
fi
done
2019-12-14 22:21:34 -05:00
) ${ ztarget }
}
_zimfw_build_login_init( ) {
2022-01-10 20:41:50 -05:00
local -r ztarget = ${ ZIM_HOME } /login_init.zsh
2020-11-23 09:38:08 -05:00
# Force update of login_init.zsh if it's older than .zimrc
2022-10-23 20:34:14 -04:00
if [ [ ${ ztarget } -ot ${ ZIM_CONFIG_FILE :- ${ ZDOTDIR :- ${ HOME } } /.zimrc } ] ] ; then
2020-11-23 09:38:08 -05:00
command mv -f ${ ztarget } { ,.old} || return 1
fi
2019-12-14 22:21:34 -05:00
_zimfw_mv = (
2022-01-10 09:29:32 -05:00
print -nR " # Do nothing. This file is deprecated.
2019-12-14 22:21:34 -05:00
"
) ${ ztarget }
2019-01-22 19:40:43 -05:00
}
2019-12-01 16:00:47 -05:00
_zimfw_build( ) {
2020-01-09 07:13:31 -05:00
_zimfw_build_init && _zimfw_build_login_init && _zimfw_print -P 'Done with build.'
2019-01-22 19:40:43 -05:00
}
2019-12-01 16:00:47 -05:00
zmodule( ) {
2022-10-23 20:34:14 -04:00
local -r ztarget = ${ ZIM_CONFIG_FILE :- ${ ZDOTDIR :- ${ HOME } } /.zimrc }
2022-09-26 21:33:49 -04:00
local -r zusage = " Usage: %B ${ 0 } %b <url> [%B-n%b|%B--name%b <module_name>] [%B-r%b|%B--root%b <path>] [options]
2019-01-22 19:40:43 -05:00
2022-10-23 20:34:14 -04:00
Add %Bzmodule%b calls to your %B${ ztarget } %b file to define the modules to be initialized.
2022-09-26 21:33:49 -04:00
The initialization will be done in the same order it' s defined.
2020-05-02 19:47:38 -04:00
2020-10-11 13:24:01 -04:00
<url> Module absolute path or repository URL. The following URL formats
2021-11-08 20:05:32 -05:00
are equivalent: %Bfoo%b, %Bzimfw/foo%b, %Bhttps://github.com/zimfw/foo.git%b.
2022-09-26 21:33:49 -04:00
If an absolute path is given, the module is considered externally
installed, and won' t be installed or updated by zimfw.
2022-10-06 20:52:57 -04:00
%B-n%b| %B--name%b <module_name> Set a custom module name. Default: the last component in <url>.
Slashes can be used inside the name to organize the module into
subdirectories. The module will be installed at
2022-09-26 21:33:49 -04:00
%B${ ZIM_HOME } /%b<module_name>.
2022-09-27 18:45:16 -04:00
%B-r%b| %B--root%b <path> Relative path to the module root.
2022-09-26 21:33:49 -04:00
Per-module 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-11-08 20:05:32 -05:00
Overrides the tag option. Default: the repository default branch.
%B-t%b| %B--tag%b <tag_name> Use specified tag when installing and updating the module. Over-
rides 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
2021-11-08 20:05:32 -05:00
either defined by %Bzstyle ':zim:zmodule' use '%b<%Bgit%b|%Bdegit%b>%B' %b, or %Bgit%b
if none is provided.
%Bgit%b requires git itself. Local changes are preserved on updates.
2021-08-10 10:56:25 -04:00
%Bdegit%b requires curl or wget, and currently only works with GitHub
2021-11-08 20:05:32 -05:00
URLs. Modules install faster and take less disk space. Local
changes are lost on updates. Git submodules are not supported.
2022-01-25 09:32:50 -05:00
%B--no-submodules%b Don' t install or update git submodules.
2021-07-01 19:06:20 -04:00
%B-z%b| %B--frozen%b Don' t install or update the module.
2022-09-26 21:33:49 -04:00
The per-module options above are carried over multiple zmodule calls for the same module.
Modules are uniquely identified by their name.
Per-module-root options:
2022-12-18 15:58:30 -05:00
%B--if%b <test> Will only initialize module root if specified test returns a zero
exit status. The test is evaluated at every new terminal startup.
2022-05-07 17:31:08 -04:00
%B--on-pull%b <command> Execute command after installing or updating the module. The com-
mand is executed in the module root directory.
2022-09-26 21:33:49 -04:00
%B-d%b| %B--disabled%b Don' t initialize the module root or uninstall the module.
The per-module-root options above are carried over multiple zmodule calls for the same mod-
ule root.
Per-call initialization options:
%B-f%b| %B--fpath%b <path> Will add specified path to fpath. The path is relative to the
module root directory. Default: %Bfunctions%b, if the subdirectory
exists and is non-empty.
%B-a%b| %B--autoload%b <func_name> Will autoload specified function . Default: all valid names inside
the %Bfunctions%b subdirectory, if any.
%B-s%b| %B--source%b <file_path> Will source specified file. The path is relative to the module
root directory. Default: %Binit.zsh%b, if a non-empty %Bfunctions%b sub-
directory exists, else the largest of the files matching the glob
2022-10-06 20:52:57 -04:00
%B( init.zsh| %b<name>%B.( zsh| plugin.zsh| zsh-theme| sh) ) %b, if any.
<name> in the glob is resolved to the last component of the mod-
ule name, or the last component of the path to the module root.
2022-09-26 21:33:49 -04:00
%B-c%b| %B--cmd%b <command> Will execute specified command. Occurrences of the %B{ } %b placeholder
in the command are substituted by the module root directory path.
2021-11-08 20:05:32 -05:00
I.e., %B-s 'foo.zsh' %b and %B-c 'source {}/foo.zsh' %b are equivalent.
2021-09-26 20:47:44 -04:00
2022-09-26 21:33:49 -04:00
Setting any per-call initialization option above will disable the default values from the
other per-call initialization options, so only your provided values will be used. I.e. these
values are either all automatic, or all manual in each zmodule call. To use default values
and also provided values, use separate zmodule calls."
2022-10-23 20:34:14 -04:00
if [ [ ${ ${ funcfiletrace [1]% : * } : A } != ${ ztarget : A } ] ] ; then
print -u2 -PlR " %F{red} ${ 0 } : Must be called from %B ${ ztarget } %b%f " '' ${ zusage }
2021-03-19 18:13:21 -04:00
return 2
2019-12-01 16:00:47 -05:00
fi
if ( ( ! # )); then
2021-11-08 11:38:56 -05:00
print -u2 -PlR " %F{red}x ${ funcfiletrace [1] } : Missing zmodule url%f " '' ${ 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
2022-09-26 21:33:49 -04:00
local zurl = ${ 1 } zname = ${ 1 : t } zroot zarg
2022-05-17 19:43:59 -04:00
local -a zfpaths zfunctions zcmds
2019-12-01 16:00:47 -05:00
if [ [ ${ zurl } = ~ ^[ ^:/] +: ] ] ; then
2022-05-17 19:43:59 -04:00
zname = ${ zname %.git }
2019-12-01 16:00:47 -05:00
elif [ [ ${ zurl } != /* ] ] ; then
# Count number of slashes
case ${# zurl //[^ \/ ]/ } in
2021-09-23 15:29:40 -04:00
0) zurl = https://github.com/zimfw/${ zurl } .git ; ;
1) zurl = https://github.com/${ zurl } .git ; ;
2019-12-01 16:00:47 -05:00
esac
fi
shift
2022-09-26 21:33:49 -04:00
while [ [ ${ 1 } = = ( -n| --name| -r| --root) ] ] ; do
2019-12-01 16:00:47 -05:00
if ( ( # < 2 )); then
2022-05-17 19:43:59 -04:00
print -u2 -PlR " %F{red}x ${ funcfiletrace [1] } :%B ${ zname } :%b Missing argument for zmodule option %B ${ 1 } %b%f " '' ${ zusage }
2019-12-01 16:00:47 -05:00
_zfailed = 1
2021-03-19 18:13:21 -04:00
return 2
2019-01-22 19:40:43 -05:00
fi
2022-09-26 21:33:49 -04:00
case ${ 1 } in
-n| --name)
shift
zname = ${ ${ 1 %%/## } ##/## }
; ;
-r| --root)
shift
zroot = ${ ${ 1 %%/## } ##/## }
; ;
esac
2019-12-01 16:00:47 -05:00
shift
2022-05-17 19:43:59 -04:00
done
2020-06-05 23:09:23 -04:00
if [ [ ${ zurl } = = /* ] ] ; then
2022-05-17 19:43:59 -04:00
_zdirs[ ${ zname } ] = ${ zurl %%/## }
2021-09-23 15:29:40 -04:00
zurl =
2020-06-05 23:09:23 -04:00
else
2022-05-17 19:43:59 -04:00
_zdirs[ ${ zname } ] = ${ ZIM_HOME } /modules/${ zname }
fi
2022-09-26 21:33:49 -04:00
if [ [ ${ +_zurls[ ${ zname } ] } -ne 0 && ${ _zurls [ ${ zname } ] } != ${ zurl } ] ] ; then
print -u2 -PlR " %F{red}x ${ funcfiletrace [1] } :%B ${ zname } :%b Module already defined with a different URL. Expected %B ${ _zurls [ ${ zname } ] } %b%f " '' ${ zusage }
_zfailed = 1
return 2
fi
2022-05-17 19:43:59 -04:00
_zurls[ ${ zname } ] = ${ zurl }
2022-09-26 21:33:49 -04:00
local -r zroot_dir = ${ _zdirs [ ${ zname } ] } ${ zroot : +/ ${ zroot } }
_zroot_dirs += ( ${ zroot_dir } )
2022-05-17 19:43:59 -04:00
# Set default values
if ( ( ! ${ +_ztools[ ${ zname } ] } ) ) ; then
zstyle -s ':zim:zmodule' use " _ztools[ ${ zname } ] " || _ztools[ ${ zname } ] = git
2020-06-05 23:09:23 -04:00
fi
2022-05-17 19:43:59 -04:00
if ( ( ! ${ +_ztypes[ ${ zname } ] } ) ) _ztypes[ ${ zname } ] = branch
if ( ( ! ${ +_zsubmodules[ ${ zname } ] } ) ) _zsubmodules[ ${ zname } ] = 1
# Set values from options
2019-12-01 16:00:47 -05:00
while ( ( # > 0 )); do
case ${ 1 } in
2022-10-06 20:52:57 -04:00
-b| --branch| -t| --tag| -u| --use| --on-pull| --if| -f| --fpath| -a| --autoload| -s| --source| -c| --cmd)
2019-12-01 16:00:47 -05:00
if ( ( # < 2 )); then
2022-05-17 19:43:59 -04:00
print -u2 -PlR " %F{red}x ${ funcfiletrace [1] } :%B ${ zname } :%b Missing argument for zmodule option %B ${ 1 } %b%f " '' ${ 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
2021-09-19 14:37:13 -04:00
case ${ 1 } in
2022-01-25 09:32:50 -05:00
-b| --branch| -t| --tag| -u| --use| --no-submodules)
2022-05-17 19:43:59 -04:00
if [ [ -z ${ zurl } ] ] _zimfw_print -u2 -PR " %F{yellow}! ${ funcfiletrace [1] } :%B ${ zname } :%b The zmodule option %B ${ 1 } %b has no effect for external modules%f "
2021-09-19 14:37:13 -04:00
; ;
esac
2019-12-01 16:00:47 -05:00
case ${ 1 } in
-b| --branch)
shift
2022-05-17 19:43:59 -04:00
_ztypes[ ${ zname } ] = branch
_zrevs[ ${ zname } ] = ${ 1 }
2019-12-01 16:00:47 -05:00
; ;
-t| --tag)
shift
2022-05-17 19:43:59 -04:00
_ztypes[ ${ zname } ] = tag
_zrevs[ ${ zname } ] = ${ 1 }
2019-12-01 16:00:47 -05:00
; ;
2021-04-03 10:35:28 -04:00
-u| --use)
shift
2022-05-17 19:43:59 -04:00
_ztools[ ${ zname } ] = ${ 1 }
2021-04-03 10:35:28 -04:00
; ;
2022-05-17 19:43:59 -04:00
--no-submodules) _zsubmodules[ ${ zname } ] = 0 ; ;
-z| --frozen) _zfrozens[ ${ zname } ] = 1 ; ;
2022-05-07 17:31:08 -04:00
--on-pull)
shift
2022-09-26 21:33:49 -04:00
zarg = ${ 1 }
if [ [ -n ${ zroot } ] ] zarg = " (builtin cd -q ${ zroot } ; ${ zarg } ) "
_zonpulls[ ${ zname } ] = " ${ _zonpulls [ ${ zname } ]+ ${ _zonpulls [ ${ zname } ] } ; } ${ zarg } "
2022-05-07 17:31:08 -04:00
; ;
2022-10-06 20:52:57 -04:00
--if)
shift
_zifs[ ${ zroot_dir } ] = ${ 1 }
; ;
2019-12-01 16:00:47 -05:00
-f| --fpath)
shift
zarg = ${ 1 }
2022-09-26 21:33:49 -04:00
if [ [ ${ zarg } != /* ] ] zarg = ${ zroot_dir } /${ zarg }
2019-12-01 16:00:47 -05:00
zfpaths += ( ${ zarg } )
; ;
-a| --autoload)
shift
zfunctions += ( ${ 1 } )
; ;
-s| --source)
shift
zarg = ${ 1 }
2022-09-26 21:33:49 -04:00
if [ [ ${ zarg } != /* ] ] zarg = ${ zroot_dir } /${ zarg }
2020-07-02 18:16:44 -04:00
zcmds += ( " source ${ zarg : A } " )
; ;
-c| --cmd)
shift
2022-09-26 21:33:49 -04:00
zcmds += ( ${ 1 //{ } /${ zroot_dir : A } } )
2019-12-01 16:00:47 -05:00
; ;
2022-09-26 21:33:49 -04:00
-d| --disabled) _zdisabled_root_dirs += ( ${ zroot_dir } ) ; ;
2019-12-01 16:00:47 -05:00
*)
2022-05-17 19:43:59 -04:00
print -u2 -PlR " %F{red}x ${ funcfiletrace [1] } :%B ${ zname } :%b Unknown zmodule option %B ${ 1 } %b%f " '' ${ 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
2019-01-22 19:40:43 -05:00
done
2021-09-19 14:37:13 -04:00
if ( ( _zflags & 1 ) ) ; then
2022-05-17 19:43:59 -04:00
_znames += ( ${ zname } )
2021-09-19 14:37:13 -04:00
fi
if ( ( _zflags & 2 ) ) ; then
2022-09-26 21:33:49 -04:00
if [ [ ! -e ${ zroot_dir } ] ] ; then
print -u2 -PR " %F{red}x ${ funcfiletrace [1] } :%B ${ zname } : ${ zroot_dir } %b not found%f "
_zfailed = 1
return 1
fi
if ( ( ! ${# zfpaths } && ! ${# zfunctions } && ! ${# zcmds } ) ) ; then
zfpaths = ( ${ zroot_dir } /functions( NF) )
# _* functions are autoloaded by compinit
# prompt_*_setup functions are autoloaded by promptinit
zfunctions = ( ${ ^zfpaths } /^( *~| *.zwc( | .old) | _*| prompt_*_setup) ( N-.:t) )
local -ra prezto_scripts = ( ${ zroot_dir } /init.zsh( N) )
if ( ( ${# zfpaths } && ${# prezto_scripts } ) ) ; then
# this follows the prezto module format, no need to check for other scripts
zcmds = ( 'source ' ${ ^prezto_scripts : A } )
else
# get script with largest size (descending `O`rder by `L`ength, and return only `[1]` first)
2022-10-06 20:52:57 -04:00
local -ra zscripts = ( ${ zroot_dir } /( init.zsh| ( ${ zname : t } | ${ zroot_dir : t } ) .( zsh| plugin.zsh| zsh-theme| sh) ) ( NOL[ 1] ) )
2022-09-26 21:33:49 -04:00
zcmds = ( 'source ' ${ ^zscripts : A } )
2020-05-25 15:01:00 -04:00
fi
2019-12-01 16:00:47 -05:00
fi
2022-09-26 21:33:49 -04:00
if ( ( ! ${# zfpaths } && ! ${# zfunctions } && ! ${# zcmds } ) ) ; then
2022-10-06 20:52:57 -04:00
_zimfw_print -u2 -PlR " %F{yellow}! ${ funcfiletrace [1] } :%B ${ zname } :%b Nothing found to be initialized. Customize the module name, root or initialization with %Bzmodule%b options.%f " '' ${ zusage }
2022-09-26 21:33:49 -04:00
fi
# Prefix is added to all _zfpaths, _zfunctions and _zcmds to distinguish the originating root dir
local -r zpre = ${ zroot_dir } $'\0'
_zfpaths += ( ${ zpre } ${ ^zfpaths } )
_zfunctions += ( ${ zpre } ${ ^zfunctions } )
_zcmds += ( ${ zpre } ${ ^zcmds } )
2019-12-01 16:00:47 -05:00
fi
}
_zimfw_source_zimrc( ) {
2022-10-23 20:34:14 -04:00
local -r ztarget = ${ ZIM_CONFIG_FILE :- ${ ZDOTDIR :- ${ HOME } } /.zimrc } _zflags = ${ 1 }
2019-12-01 16:00:47 -05:00
local -i _zfailed = 0
2020-01-26 15:33:17 -05:00
if ! source ${ ztarget } || ( ( _zfailed ) ) ; then
print -u2 -PR " %F{red}Failed to source %B ${ ztarget } %b%f "
return 1
fi
2022-05-17 19:43:59 -04:00
if ( ( _zflags & 1 && ${# _znames } = = 0 ) ) ; then
2020-01-26 15:33:17 -05:00
print -u2 -PR " %F{red}No modules defined in %B ${ ztarget } %b%f "
2019-12-01 16:00:47 -05:00
return 1
fi
2022-09-26 21:33:49 -04:00
# Remove all from _zfpaths, _zfunctions and _zcmds with disabled root dirs prefixes
local zroot_dir zpre
for zroot_dir in ${ _zdisabled_root_dirs } ; do
zpre = ${ zroot_dir } $'\0'
_zfpaths = ( ${ _zfpaths : # ${ zpre } * } )
_zfunctions = ( ${ _zfunctions : # ${ zpre } * } )
_zcmds = ( ${ _zcmds : # ${ zpre } * } )
done
2019-01-22 19:40:43 -05:00
}
2021-09-19 14:37:13 -04:00
_zimfw_list_unuseds( ) {
local -i i = 1
2022-09-26 21:33:49 -04:00
local zinstalled = ( ${ ZIM_HOME } /modules/*( N/) )
local -r zdirs = ( ${ (v)_zdirs } )
2021-09-19 14:37:13 -04:00
# Search into subdirectories
while ( ( i <= ${# zinstalled } ) ) ; do
2022-09-26 21:33:49 -04:00
if ( ( ${ zdirs [(I) ${ zinstalled [i] } /*] } ) ) ; then
2021-09-23 12:36:38 -04:00
zinstalled += ( ${ zinstalled [i] } /*( N/) )
2021-09-19 14:37:13 -04:00
zinstalled[ i] = ( )
else
( ( i++ ) )
fi
done
2022-09-26 21:33:49 -04:00
# Unused = all installed dirs not in zdirs
_zunused_dirs = ( ${ zinstalled : |zdirs } )
2021-09-19 14:37:13 -04:00
local zunused
2022-05-17 19:43:59 -04:00
for zunused ( ${ _zunused_dirs } ) _zimfw_print -PR " %B ${ zunused : t } :%b ${ zunused } ${ 1 } "
2021-09-19 14:37:13 -04:00
}
2020-01-15 12:35:30 -05:00
_zimfw_version_check( ) {
if ( ( _zprintlevel > 0 ) ) ; then
local -r ztarget = ${ ZIM_HOME } /.latest_version
# If .latest_version does not exist or was not modified in the last 30 days
2020-01-26 15:33:17 -05:00
if [ [ -w ${ ztarget : h } && ! -f ${ ztarget } ( #qNm-30) ]]; then
2021-04-03 10:35:28 -04:00
# Get latest version (get all `v*` tags from repo, delete `*v` from beginning,
# sort in descending `O`rder `n`umerically, and get the `[1]` first)
2022-08-02 13:49:09 -04:00
print -R ${ ${ (On) ${ (f) " $( command git ls-remote --tags --refs \
2021-04-03 10:35:28 -04:00
https://github.com/zimfw/zimfw.git 'v*' 2>/dev/null) " }##*v}[1]} >! ${ ztarget } &!
2020-01-15 12:35:30 -05:00
fi
2020-01-22 12:48:19 -05:00
if [ [ -f ${ ztarget } ] ] ; then
local -r zlatest_version = $( <${ ztarget } )
if [ [ -n ${ zlatest_version } && ${ _zversion } != ${ zlatest_version } ] ] ; then
2021-11-08 11:38:56 -05:00
print -u2 -PlR " %F{yellow}Latest zimfw version is %B ${ zlatest_version } %b. You're using version %B ${ _zversion } %b. Run %Bzimfw upgrade%b to upgrade.%f " ''
2020-01-22 12:48:19 -05:00
fi
2020-01-15 12:35:30 -05:00
fi
fi
}
2022-01-18 20:34:36 -05:00
_zimfw_check_dumpfile( ) {
2022-01-24 18:58:28 -05:00
local zdumpfile zfpath zline
2022-01-18 20:51:11 -05:00
zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile = ${ ZDOTDIR :- ${ HOME } } /.zcompdump
2022-01-18 20:34:36 -05:00
if [ [ -e ${ zdumpfile } ] ] ; then
2022-01-24 18:58:28 -05:00
if ( ( ${ +_zim_fpath } ) ) ; then
local -r zpre = $'*\0'
zfpath = ( ${ ${ _zfpaths # ${ ~zpre } } : A } ${ fpath : |_zim_fpath } )
else
zfpath = ( ${ fpath } )
fi
local -r zcomps = ( ${ ^zfpath } /^( [ ^_] *| *~| *.zwc( | .old) ) ( N:t) )
2022-01-18 20:34:36 -05:00
IFS = $' \t' read -rA zline < ${ zdumpfile } || return 1
2022-01-18 20:51:11 -05:00
if [ [ ${ zline [2] } -eq ${# zcomps } && ${ zline [4] } = = ${ ZSH_VERSION } ] ] ; then
2022-01-18 20:34:36 -05:00
_zimfw_print -PR " %F{green})%f %B ${ zdumpfile } :%b Already up to date "
else
_zimfw_print -PR " %F{green})%f %B ${ zdumpfile } :%b New completion configuration needs to be dumped. Will do %Bclean-dumpfile%b. "
_zimfw_clean_dumpfile
fi
else
_zimfw_print -PR " %F{green})%f %B ${ zdumpfile } :%b Not found "
fi
_zimfw_print 'Done with check-dumpfile.'
}
2019-01-22 19:40:43 -05:00
_zimfw_clean_compiled( ) {
2020-07-31 22:03:47 -04:00
# Array with unique dirs. ${ZIM_HOME} or any subdirectory should only occur once.
2022-05-17 19:43:59 -04:00
local -Ur zscriptdirs = ( ${ ZIM_HOME } ${ ${ (v)_zdirs## ${ ZIM_HOME } /* } : A } )
2019-12-27 09:51:02 -05:00
local zopt
2021-09-23 15:29:40 -04:00
if ( ( _zprintlevel > 0 ) ) zopt = -v
2022-01-18 20:34:36 -05:00
command rm -f ${ zopt } ${ ^zscriptdirs } /**/*.zwc( | .old) ( N) && \
_zimfw_print -P 'Done with clean-compiled. Restart your terminal or run %Bzimfw compile%b to re-compile.'
2019-01-22 19:40:43 -05:00
}
_zimfw_clean_dumpfile( ) {
2019-12-01 16:00:47 -05:00
local zdumpfile zopt
2019-12-14 22:21:34 -05:00
zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile = ${ ZDOTDIR :- ${ HOME } } /.zcompdump
2021-09-23 15:29:40 -04:00
if ( ( _zprintlevel > 0 ) ) zopt = -v
2022-01-18 20:34:36 -05:00
command rm -f ${ zopt } ${ zdumpfile } ( | .zwc( | .old) ) ( N) && \
_zimfw_print -P " Done with clean-dumpfile. ${ _zrestartmsg } "
2019-01-22 19:40:43 -05:00
}
2019-12-14 22:21:34 -05:00
_zimfw_compile( ) {
2022-01-10 09:29:32 -05:00
# Compile Zim scripts
2022-09-26 21:33:49 -04:00
local zroot_dir zfile
for zroot_dir in ${ _zroot_dirs : |_zdisabled_root_dirs } ; do
for zfile in ${ zroot_dir } /( ^*test*/) #*.zsh(|-theme)(N-.); do
if [ [ ! ${ zfile } .zwc -nt ${ zfile } ] ] ; then
zcompile -UR ${ zfile } && _zimfw_print -PR " %F{green})%f %B ${ zfile } .zwc:%b Compiled "
fi
done
2022-01-10 09:29:32 -05:00
done
2022-01-10 20:41:50 -05:00
_zimfw_print -P 'Done with compile.'
2019-12-14 22:21:34 -05:00
}
2019-01-22 19:40:43 -05:00
_zimfw_info( ) {
2022-12-18 15:58:30 -05:00
print -R 'zimfw version: ' ${ _zversion } ' (built at 2022-12-18 21:05:25 UTC, previous commit is e54958b)'
2022-08-02 13:49:09 -04:00
print -R 'OSTYPE: ' ${ OSTYPE }
print -R 'TERM: ' ${ TERM }
print -R 'TERM_PROGRAM: ' ${ TERM_PROGRAM }
print -R 'TERM_PROGRAM_VERSION: ' ${ TERM_PROGRAM_VERSION }
print -R 'ZIM_HOME: ' ${ ZIM_HOME }
print -R 'ZSH_VERSION: ' ${ ZSH_VERSION }
2019-01-22 19:40:43 -05:00
}
2021-10-26 12:15:40 -04:00
_zimfw_install_update( ) {
2022-05-17 19:43:59 -04:00
local -r _zargs_action = ${ 1 }
_zimfw_source_zimrc 1 && zargs -n 1 -P 0 -- " ${ _znames [@] } " -- _zimfw_run_tool
2022-05-24 17:25:32 -04:00
# Ignore return from zargs with -P. Was missing values before zsh 5.9, and
# it's intermittently failing in zsh 5.9 and macOS. See https://www.zsh.org/mla/workers/2022/msg00611.html
2022-05-17 19:43:59 -04:00
return 0
2021-10-26 12:15:40 -04:00
}
2019-12-10 18:19:01 -05:00
_zimfw_uninstall( ) {
2021-09-19 14:37:13 -04:00
local zopt
2021-09-23 15:29:40 -04:00
if ( ( _zprintlevel > 0 ) ) zopt = -v
2021-09-23 12:36:38 -04:00
if ( ( ${# _zunused_dirs } ) ) ; then
if ( ( _zprintlevel <= 0 ) ) || read -q " ?Uninstall ${# _zunused_dirs } module(s) listed above [y/N]? " ; then
2020-10-26 20:32:25 -04:00
_zimfw_print
2021-09-23 12:36:38 -04:00
command rm -rf ${ zopt } ${ _zunused_dirs } || return 1
2019-12-10 18:19:01 -05:00
fi
2020-10-26 20:32:25 -04:00
fi
2020-01-09 07:13:31 -05:00
_zimfw_print -P 'Done with uninstall.'
2019-12-10 18:19:01 -05:00
}
2019-01-22 19:40:43 -05:00
_zimfw_upgrade( ) {
2021-07-11 16:58:15 -04:00
local -r ztarget = ${ ZIM_HOME } /zimfw.zsh zurl = https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh.gz
2019-12-01 16:00:47 -05:00
{
2020-01-11 16:35:59 -05:00
if ( ( ${ +commands[curl] } ) ) ; then
2020-07-23 22:49:39 -04:00
command curl -fsSL -o ${ ztarget } .new.gz ${ zurl } || return 1
2020-01-11 16:35:59 -05:00
else
2020-01-11 16:23:11 -05:00
local zopt
2021-09-23 15:29:40 -04:00
if ( ( _zprintlevel <= 1 ) ) zopt = -q
2020-07-23 22:49:39 -04:00
if ! command wget -nv ${ zopt } -O ${ ztarget } .new.gz ${ zurl } ; then
2021-04-03 10:35:28 -04:00
if ( ( _zprintlevel <= 1 ) ) ; then
print -u2 -PR " %F{red}Failed to download %B ${ zurl } %b. Use %B-v%b option to see details.%f "
fi
2020-01-11 16:23:11 -05:00
return 1
fi
2019-12-01 16:00:47 -05:00
fi
2020-07-23 22:49:39 -04:00
command gunzip -f ${ ztarget } .new.gz || return 1
2020-01-20 07:03:20 -05:00
# .latest_version can be outdated and will yield a false warning if zimfw is
# upgraded before .latest_version is refreshed. Bad thing about having a cache.
_zimfw_mv ${ ztarget } { .new,} && command rm -f ${ ZIM_HOME } /.latest_version && \
_zimfw_print -P 'Done with upgrade.'
2019-12-01 16:00:47 -05:00
} always {
2020-07-23 22:49:39 -04:00
command rm -f ${ ztarget } .new{ ,.gz}
2019-12-01 16:00:47 -05:00
}
2019-01-22 19:40:43 -05:00
}
2021-09-19 14:37:13 -04:00
_zimfw_run_list( ) {
2022-05-17 19:43:59 -04:00
local -r zname = ${ 1 }
2022-09-26 21:33:49 -04:00
local -r zdir = ${ _zdirs [ ${ zname } ] }
print -PnR " %B ${ zname } :%b ${ zdir } "
if [ [ -z ${ _zurls [ ${ zname } ] } ] ] print -n ' (external)'
if ( ( ${ _zfrozens [ ${ zname } ] } ) ) print -n ' (frozen)'
if ( ( ${ _zdisabled_root_dirs [(I) ${ zdir } ] } ) ) print -n ' (disabled)'
2021-09-29 22:22:28 -04:00
print
2021-09-19 14:37:13 -04:00
if ( ( _zprintlevel > 1 ) ) ; then
2022-05-17 19:43:59 -04:00
if [ [ ${ _zfrozens [ ${ zname } ] } -eq 0 && -n ${ _zurls [ ${ zname } ] } ] ] ; then
print -nR " From: ${ _zurls [ ${ zname } ] } , "
if [ [ -z ${ _zrevs [ ${ zname } ] } ] ] ; then
2021-09-19 14:37:13 -04:00
print -n 'default branch'
else
2022-05-17 19:43:59 -04:00
print -nR " ${ _ztypes [ ${ zname } ] } ${ _zrevs [ ${ zname } ] } "
2021-09-19 14:37:13 -04:00
fi
2022-05-17 19:43:59 -04:00
print -nR " , using ${ _ztools [ ${ zname } ] } "
2022-09-26 21:33:49 -04:00
if ( ( ! _zsubmodules[ ${ zname } ] ) ) print -n ', no git submodules'
2022-01-25 09:32:50 -05:00
print
2022-05-17 19:43:59 -04:00
if [ [ -n ${ _zonpulls [ ${ zname } ] } ] ] print -R " On-pull: ${ _zonpulls [ ${ zname } ] } "
2021-09-19 14:37:13 -04:00
fi
2022-09-26 21:33:49 -04:00
# Match the current module dir prefix from _zroot_dirs
local -r zroot_dirs = ( ${ (M)_zroot_dirs : # ${ zdir } /* } )
if ( ( ${# zroot_dirs } ) ) ; then
print ' Additional root:'
local zroot_dir
for zroot_dir in ${ zroot_dirs } ; do
print -nR " ${ zroot_dir } "
if ( ( ${ _zdisabled_root_dirs [(I) ${ zroot_dir } ] } ) ) print -n ' (disabled)'
print
done
fi
# Match and remove the prefix from _zfpaths, _zfunctions and _zcmds
2022-09-28 20:42:05 -04:00
local -r zpre = " ${ (q)zdir } (|/*) " $'\0'
2022-09-26 21:33:49 -04:00
local -r zfpaths = ( ${ ${ (M)_zfpaths : # ${ ~zpre } * } # ${ ~zpre } } ) zfunctions = ( ${ ${ (M)_zfunctions : # ${ ~zpre } * } # ${ ~zpre } } ) zcmds = ( ${ ${ (M)_zcmds : # ${ ~zpre } * } # ${ ~zpre } } )
2022-01-11 11:29:47 -05:00
if ( ( ${# zfpaths } ) ) print -R ' fpath: ' ${ zfpaths }
if ( ( ${# zfunctions } ) ) print -R ' autoload: ' ${ zfunctions }
if ( ( ${# zcmds } ) ) print -R ' cmd: ' ${ (j : ; : )zcmds }
2021-09-19 14:37:13 -04:00
fi
}
2021-04-03 10:35:28 -04:00
_zimfw_run_tool( ) {
2022-05-17 19:43:59 -04:00
local -r zname = ${ 1 }
if [ [ -z ${ _zurls [ ${ zname } ] } ] ] ; then
2022-08-02 13:49:09 -04:00
if ( ( _zprintlevel > 1 ) ) print -PR $'\E[2K\r' " %F{green})%f %B ${ zname } :%b Skipping external module "
2021-09-29 22:22:28 -04:00
return 0
fi
2022-05-17 19:43:59 -04:00
if ( ( _zfrozens[ ${ zname } ] ) ) ; then
2022-08-02 13:49:09 -04:00
if ( ( _zprintlevel > 1 ) ) print -PR $'\E[2K\r' " %F{green})%f %B ${ zname } :%b Skipping frozen module "
2021-09-29 22:22:28 -04:00
return 0
fi
2022-05-17 19:43:59 -04:00
case ${ _zargs_action } in
2021-04-03 10:35:28 -04:00
install)
2022-05-17 19:43:59 -04:00
if [ [ -e ${ _zdirs [ ${ zname } ] } ] ] ; then
2022-08-02 13:49:09 -04:00
if ( ( _zprintlevel > 1 ) ) print -PR $'\E[2K\r' " %F{green})%f %B ${ zname } :%b Skipping already installed module "
2021-04-03 10:35:28 -04:00
return 0
fi
2022-05-17 19:43:59 -04:00
_zimfw_print -nR $'\E[2K\r' " Installing ${ zname } ... "
2021-04-03 10:35:28 -04:00
; ;
update)
2022-05-17 19:43:59 -04:00
if [ [ ! -d ${ _zdirs [ ${ zname } ] } ] ] ; then
print -u2 -PR $'\E[2K\r' " %F{red}x %B ${ zname } :%b Not installed. Run %Bzimfw install%b to install.%f "
2021-04-03 10:35:28 -04:00
return 1
fi
2022-05-17 19:43:59 -04:00
_zimfw_print -nR $'\E[2K\r' " Updating ${ zname } ... "
2021-04-03 10:35:28 -04:00
; ;
*)
2022-05-17 19:43:59 -04:00
print -u2 -PR $'\E[2K\r' " %F{red}x %B ${ zname } :%b Unknown action ${ _zargs_action } %f "
2021-04-03 10:35:28 -04:00
return 1
; ;
esac
local zcmd
2022-05-17 19:43:59 -04:00
case ${ _ztools [ ${ zname } ] } in
2021-04-03 10:35:28 -04:00
degit) zcmd = " # This runs in a new shell
2022-01-10 20:41:50 -05:00
builtin emulate -L zsh -o EXTENDED_GLOB
2022-01-25 09:32:50 -05:00
readonly -i PRINTLEVEL = \$ { 1} SUBMODULES = \$ { 8}
2022-05-07 17:31:08 -04:00
readonly ACTION = \$ { 2} MODULE = \$ { 3} DIR = \$ { 4} URL = \$ { 5} REV = \$ { 7} ONPULL = \$ { 9} TEMP = .zdegit_\$ { RANDOM}
2021-08-09 23:28:13 -04:00
readonly TARBALL_TARGET = \$ { DIR} /\$ { TEMP} _tarball.tar.gz INFO_TARGET = \$ { DIR} /.zdegit
2021-04-03 10:35:28 -04:00
print_error( ) {
2021-11-08 11:38:56 -05:00
print -u2 -PlR $'\E[2K\r' \" %F{ red} x %B\$ { MODULE} :%b \$ { 1} %f\" \$ { 2:+\$ { ( F) :- \$ { ( f) ^2} } }
2021-04-03 10:35:28 -04:00
}
2021-08-09 21:32:32 -04:00
print_okay( ) {
2021-09-21 17:36:32 -04:00
if ( ( PRINTLEVEL > 0 ) ) ; then
2021-09-23 15:29:40 -04:00
local -r log = \$ { 2:+\$ { ( F) :- \$ { ( f) ^2} } }
2022-01-25 09:32:50 -05:00
if [ [ \$ { SUBMODULES} -ne 0 && -e \$ { DIR} /.gitmodules ] ] ; then
2022-05-07 17:31:08 -04:00
print -u2 -PlR $'\E[2K\r' \" %F{ yellow} ! %B\$ { MODULE} :%b \$ { 1} . Module contains git submodules, which are not supported by Zim' s degit. Use zmodule option %B--no-submodules%b to disable this warning.%f\" \$ { log}
2021-04-03 10:35:28 -04:00
else
2022-05-07 17:31:08 -04:00
print -PlR $'\E[2K\r' \" %F{ green} ) %f %B\$ { MODULE} :%b \$ { 1} \" \$ { log}
fi
fi
}
handle( ) {
if [ [ -n \$ { ONPULL} ] ] ; then
2022-05-09 19:36:03 -04:00
if ! ERR = \$ ( builtin cd -q \$ { DIR} 2>& 1 && builtin eval \$ { ONPULL} 2>& 1) ; then
2022-05-07 17:31:08 -04:00
print_error 'Error during on-pull' \$ { ERR}
return 1
elif [ [ \$ { PRINTLEVEL} -gt 1 && -n \$ { ERR} ] ] ; then
2022-05-09 19:36:03 -04:00
builtin set \$ { 1} \$ { 2:+\$ { 2} $'\n' } \" On-pull output:\" $'\n' \$ { ERR}
2021-04-03 10:35:28 -04:00
fi
fi
2022-05-07 17:31:08 -04:00
print_okay \" \$ { @} \"
2021-04-03 10:35:28 -04:00
}
download_tarball( ) {
2021-04-08 17:26:33 -04:00
local host repo
2021-04-03 10:35:28 -04:00
if [ [ \$ { URL} = ~ ^( [ ^:@/] +://) ?( [ ^@] +@) ?( [ ^:/] +) [ :/] ( [ ^/] +/[ ^/] +) /?\$ ] ] ; then
2021-04-08 17:26:33 -04:00
host = \$ { match[ 3] }
repo = \$ { match[ 4] %.git}
2021-04-03 10:35:28 -04:00
fi
if [ [ \$ { host} != github.com || -z \$ { repo} ] ] ; then
2021-08-10 10:56:25 -04:00
print_error \" \$ { URL} is not a valid GitHub URL. Will not try to \$ { ACTION} .\"
2021-04-03 10:35:28 -04:00
return 1
fi
2021-04-21 20:37:47 -04:00
local -r headers_target = \$ { DIR} /\$ { TEMP} _headers
2021-04-03 10:35:28 -04:00
{
2021-07-11 16:58:15 -04:00
local info_header header etag
2021-04-03 10:35:28 -04:00
if [ [ -r \$ { INFO_TARGET} ] ] ; then
2021-04-08 17:26:33 -04:00
local -r info = ( \" \$ { ( @f) \" \$ ( <\$ { INFO_TARGET} ) \" } \" )
2021-04-03 10:35:28 -04:00
if [ [ \$ { URL} != \$ { info[ 1] } ] ] ; then
print_error \" URL does not match. Expected \$ { URL} . Will not try to \$ { ACTION} .\"
return 1
fi
2021-04-08 17:26:33 -04:00
# Previous REV is in line 2, reserved for future use.
info_header = \$ { info[ 3] }
2021-04-03 10:35:28 -04:00
fi
2021-04-08 17:26:33 -04:00
local -r tarball_url = https://api.github.com/repos/\$ { repo} /tarball/\$ { REV}
2021-04-03 10:35:28 -04:00
if ( ( \$ { +commands[ curl] } ) ) ; then
2021-04-08 17:26:33 -04:00
if ! ERR = \$ ( command curl -fsSL \$ { info_header:+-H} \$ { info_header} -o \$ { TARBALL_TARGET} -D \$ { headers_target} \$ { tarball_url} 2>& 1) ; then
2021-04-03 10:35:28 -04:00
print_error \" Error downloading \$ { tarball_url} with curl\" \$ { ERR}
return 1
fi
else
# wget returns 8 when 304 Not Modified, so we cannot use wget's error codes
2021-04-08 17:26:33 -04:00
command wget -q \$ { info_header:+--header= \$ { info_header} } -O \$ { TARBALL_TARGET} -S \$ { tarball_url} 2>\$ { headers_target}
2021-04-03 10:35:28 -04:00
fi
2021-04-08 17:26:33 -04:00
local -i http_code
2021-04-03 10:35:28 -04:00
while IFS = read -r header; do
header = \$ { \$ { header## ##}%%$'\r'##}
if [ [ \$ { header} = = HTTP/* ] ] ; then
http_code = \$ { \$ { ( s: :) header} [ 2] }
2021-09-23 15:29:40 -04:00
elif [ [ \$ { \$ { ( L) header%%:*} %% ##} == etag ]]; then
2021-04-08 17:26:33 -04:00
etag = \$ { \$ { header#*:} ## ##}
2021-04-03 10:35:28 -04:00
fi
done < \$ { headers_target}
if ( ( http_code = = 304 ) ) ; then
# Not Modified
command rm -f \$ { TARBALL_TARGET} 2>/dev/null
return 0
elif ( ( http_code != 200 ) ) ; then
print_error \" Error downloading \$ { tarball_url} , HTTP code \$ { http_code} \"
return 1
fi
2021-04-08 17:26:33 -04:00
if [ [ -z \$ { etag} ] ] ; then
2021-04-03 10:35:28 -04:00
print_error \" Error downloading \$ { tarball_url} , no ETag header found in response\"
return 1
fi
2021-11-08 11:38:56 -05:00
if ! print -lR \" \$ { URL} \" \" \$ { REV} \" \" If-None-Match: \$ { etag} \" >! \$ { INFO_TARGET} 2>/dev/null; then
2021-04-03 10:35:28 -04:00
print_error \" Error creating or updating \$ { INFO_TARGET} \"
return 1
fi
} always {
command rm -f \$ { headers_target} 2>/dev/null
}
}
untar_tarball( ) {
if ! ERR = \$ ( command tar -C \$ { 1} --strip= 1 -xzf \$ { TARBALL_TARGET} 2>& 1) ; then
print_error \" Error extracting \$ { TARBALL_TARGET} \" \$ { ERR}
return 1
fi
}
create_dir( ) {
if ! ERR = \$ ( command mkdir -p \$ { 1} 2>& 1) ; then
print_error \" Error creating \$ { 1} \" \$ { ERR}
return 1
fi
}
2021-09-23 15:29:40 -04:00
case \$ { ACTION} in
install)
{
2022-05-07 17:31:08 -04:00
create_dir \$ { DIR} && download_tarball && untar_tarball \$ { DIR} && handle Installed
2021-09-23 15:29:40 -04:00
} always {
# return 1 does not change \${TRY_BLOCK_ERROR}, only changes \${?}
( ( TRY_BLOCK_ERROR = ? ) )
command rm -f \$ { TARBALL_TARGET} 2>/dev/null
2022-01-25 09:32:50 -05:00
if ( ( TRY_BLOCK_ERROR ) ) command rm -rf \$ { DIR} 2>/dev/null
2021-09-23 15:29:40 -04:00
}
; ;
update)
if [ [ ! -r \$ { INFO_TARGET} ] ] ; then
2021-10-19 09:36:51 -04:00
if ( ( PRINTLEVEL > 0 ) ) ; then
2021-11-15 19:37:40 -05:00
print -u2 -PR $'\E[2K\r' \" %F{ yellow} ! %B\$ { MODULE} :%b Module was not installed using Zim' s degit. Will not try to update. Use zmodule option %B-z%b| %B--frozen%b to disable this warning.%f\"
2021-10-19 09:36:51 -04:00
fi
return 0
2021-09-23 15:29:40 -04:00
fi
readonly DIR_NEW = \$ { DIR} \$ { TEMP}
{
download_tarball || return 1
if [ [ ! -e \$ { TARBALL_TARGET} ] ] ; then
2022-05-07 17:31:08 -04:00
handle 'Already up to date'
return \$ { ?}
2021-09-23 15:29:40 -04:00
fi
create_dir \$ { DIR_NEW} && untar_tarball \$ { DIR_NEW} || return 1
if ( ( \$ { +commands[ diff] } ) ) ; then
LOG = \$ ( command diff -x '.zdegit*' -x '*.zwc' -x '*.zwc.old' -qr \$ { DIR} \$ { DIR_NEW} 2>/dev/null)
LOG = \$ { \$ { LOG//\$ { DIR_NEW} /new} //\$ { DIR} /old}
fi
if ! ERR = \$ ( { command cp -f \$ { INFO_TARGET} \$ { DIR_NEW} && \
command rm -rf \$ { DIR} && command mv -f \$ { DIR_NEW} \$ { DIR} } 2>& 1) ; then
print_error \" Error updating \$ { DIR} \" \$ { ERR}
2021-04-03 10:35:28 -04:00
return 1
fi
2022-05-07 17:31:08 -04:00
handle Updated \$ { LOG}
2021-09-23 15:29:40 -04:00
} always {
command rm -f \$ { TARBALL_TARGET} 2>/dev/null
command rm -rf \$ { DIR_NEW} 2>/dev/null
}
; ;
esac
2021-04-03 10:35:28 -04:00
" ;;
git) zcmd = " # This runs in a new shell
2021-11-21 14:29:33 -05:00
builtin emulate -L zsh
2022-01-25 09:32:50 -05:00
readonly -i PRINTLEVEL = \$ { 1} SUBMODULES = \$ { 8}
2022-05-17 19:43:59 -04:00
readonly ACTION = \$ { 2} MODULE = \$ { 3} DIR = \$ { 4} URL = \$ { 5} TYPE = \$ { 6} ONPULL = \$ { 9}
2021-09-19 14:37:13 -04:00
REV = \$ { 7}
2021-04-03 10:35:28 -04:00
print_error( ) {
2021-11-08 11:38:56 -05:00
print -u2 -PlR $'\E[2K\r' \" %F{ red} x %B\$ { MODULE} :%b \$ { 1} %f\" \$ { 2:+\$ { ( F) :- \$ { ( f) ^2} } }
2021-04-03 10:35:28 -04:00
}
print_okay( ) {
2021-11-08 11:38:56 -05:00
if ( ( PRINTLEVEL > 0 ) ) print -PlR $'\E[2K\r' \" %F{ green} ) %f %B\$ { MODULE} :%b \$ { 1} \" \$ { 2:+\$ { ( F) :- \$ { ( f) ^2} } }
2021-04-03 10:35:28 -04:00
}
2022-05-07 17:31:08 -04:00
handle( ) {
if [ [ -n \$ { ONPULL} ] ] ; then
2022-05-09 19:36:03 -04:00
if ! ERR = \$ ( builtin cd -q \$ { DIR} 2>& 1 && builtin eval \$ { ONPULL} 2>& 1) ; then
2022-05-07 17:31:08 -04:00
print_error 'Error during on-pull' \$ { ERR}
return 1
elif [ [ \$ { PRINTLEVEL} -gt 1 && -n \$ { ERR} ] ] ; then
2022-05-09 19:36:03 -04:00
builtin set \$ { 1} \$ { 2:+\$ { 2} $'\n' } \" On-pull output:\" $'\n' \$ { ERR}
2022-05-07 17:31:08 -04:00
fi
fi
print_okay \" \$ { @} \"
}
2021-04-03 10:35:28 -04:00
case \$ { ACTION} in
install)
2022-01-25 09:32:50 -05:00
if ERR = \$ ( command git clone \$ { REV:+-b} \$ { REV} -q --config core.autocrlf= false \$ { \$ { SUBMODULES:#0} :+--recursive} -- \$ { URL} \$ { DIR} 2>& 1) ; then
2022-05-07 17:31:08 -04:00
handle Installed
2021-04-03 10:35:28 -04:00
else
print_error 'Error during git clone' \$ { ERR}
return 1
fi
; ;
update)
2021-11-11 13:46:28 -05:00
if [ [ ! -r \$ { DIR} /.git ] ] ; then
2021-10-19 09:36:51 -04:00
if ( ( PRINTLEVEL > 0 ) ) ; then
2021-11-15 19:37:40 -05:00
print -u2 -PR $'\E[2K\r' \" %F{ yellow} ! %B\$ { MODULE} :%b Module was not installed using git. Will not try to update. Use zmodule option %B-z%b| %B--frozen%b to disable this warning.%f\"
2021-10-19 09:36:51 -04:00
fi
return 0
2021-04-03 10:35:28 -04:00
fi
2021-11-11 13:46:28 -05:00
if [ [ \$ { URL} != \$ ( command git -C \$ { DIR} config --get remote.origin.url) ] ] ; then
2021-04-03 10:35:28 -04:00
print_error \" URL does not match. Expected \$ { URL} . Will not try to update.\"
return 1
fi
2021-11-11 13:46:28 -05:00
if ! ERR = \$ ( command git -C \$ { DIR} fetch -pq origin 2>& 1) ; then
2021-04-03 10:35:28 -04:00
print_error 'Error during git fetch' \$ { ERR}
return 1
fi
if [ [ \$ { TYPE} = = tag ] ] ; then
2021-11-11 13:46:28 -05:00
if [ [ \$ { REV} = = \$ ( command git -C \$ { DIR} describe --tags --exact-match 2>/dev/null) ] ] ; then
2022-05-07 17:31:08 -04:00
handle 'Already up to date'
return \$ { ?}
2021-04-03 10:35:28 -04:00
fi
elif [ [ -z \$ { REV} ] ] ; then
# Get HEAD remote branch
2021-11-11 13:46:28 -05:00
if ! ERR = \$ ( command git -C \$ { DIR} remote set-head origin -a 2>& 1) ; then
2021-04-03 10:35:28 -04:00
print_error 'Error during git remote set-head' \$ { ERR}
return 1
fi
2021-11-15 19:37:40 -05:00
if REV = \$ ( command git -C \$ { DIR} symbolic-ref --short refs/remotes/origin/HEAD 2>& 1) ; then
REV = \$ { REV#origin/}
else
print_error 'Error during git symbolic-ref' \$ { REV}
return 1
fi
2021-04-03 10:35:28 -04:00
fi
if [ [ \$ { TYPE} = = branch ] ] ; then
LOG_REV = \$ { REV} @{ u}
else
LOG_REV = \$ { REV}
fi
2021-11-11 13:46:28 -05:00
LOG = \$ ( command git -C \$ { DIR} log --graph --color --format= '%C(yellow)%h%C(reset) %s %C(cyan)(%cr)%C(reset)' ..\$ { LOG_REV} -- 2>/dev/null)
if ! ERR = \$ ( command git -C \$ { DIR} checkout -q \$ { REV} -- 2>& 1) ; then
2021-04-03 10:35:28 -04:00
print_error 'Error during git checkout' \$ { ERR}
return 1
fi
if [ [ \$ { TYPE} = = branch ] ] ; then
2021-11-11 13:46:28 -05:00
if ! OUT = \$ ( command git -C \$ { DIR} merge --ff-only --no-progress -n 2>& 1) ; then
2021-04-03 10:35:28 -04:00
print_error 'Error during git merge' \$ { OUT}
return 1
fi
# keep just first line of OUT
OUT = \$ { OUT%%( $'\n' | $'\r' ) *}
else
OUT = \" Updating to \$ { TYPE} \$ { REV} \"
fi
2022-01-25 09:32:50 -05:00
if ( ( SUBMODULES ) ) ; then
2021-11-11 13:46:28 -05:00
if ! ERR = \$ ( command git -C \$ { DIR} submodule update --init --recursive -q -- 2>& 1) ; then
2021-11-10 22:50:17 -05:00
print_error 'Error during git submodule update' \$ { ERR}
return 1
fi
2021-04-03 10:35:28 -04:00
fi
2022-05-07 17:31:08 -04:00
handle \$ { OUT} \$ { LOG}
2021-04-03 10:35:28 -04:00
; ;
esac
" ;;
*)
2022-05-17 19:43:59 -04:00
print -u2 -PR " $'\E[2K\r'%F{red}x %B ${ zname } :%b Unknown tool ${ _ztools [ ${ zname } ] } %f "
2021-04-03 10:35:28 -04:00
return 1
; ;
esac
2022-05-17 19:43:59 -04:00
zsh -c ${ zcmd } ${ _ztools [ ${ zname } ] } " ${ _zprintlevel } " " ${ _zargs_action } " " ${ zname } " " ${ _zdirs [ ${ zname } ] } " " ${ _zurls [ ${ zname } ] } " " ${ _ztypes [ ${ zname } ] } " " ${ _zrevs [ ${ zname } ] } " " ${ _zsubmodules [ ${ zname } ] } " " ${ _zonpulls [ ${ zname } ] } "
2021-04-03 10:35:28 -04:00
}
2019-01-22 19:40:43 -05:00
zimfw( ) {
2022-01-10 20:41:50 -05:00
builtin emulate -L zsh -o EXTENDED_GLOB
2022-12-18 15:58:30 -05:00
local -r _zversion = '1.11.0' zusage = " Usage: %B ${ 0 } %b <action> [%B-q%b|%B-v%b]
2019-01-22 19:40:43 -05:00
2019-12-01 16:00:47 -05:00
Actions:
2021-09-21 09:27:56 -04:00
%Bbuild%b Build %B${ ZIM_HOME } /init.zsh%b and %B${ ZIM_HOME } /login_init.zsh%b.
2022-01-18 20:34:36 -05:00
Also does %Bcheck-dumpfile%b and %Bcompile%b. Use %B-v%b to also see their output.
%Bcheck-dumpfile%b Does %Bclean-dumpfile%b if new completion configuration needs to be dumped.
2021-09-21 09:27:56 -04:00
%Bclean%b Clean all. Does both %Bclean-compiled%b and %Bclean-dumpfile%b.
%Bclean-compiled%b Clean Zsh compiled files.
2022-01-18 20:34:36 -05:00
%Bclean-dumpfile%b Clean completion dumpfile.
2021-09-21 09:27:56 -04:00
%Bcompile%b Compile Zsh files.
%Bhelp%b Print this help.
%Binfo%b Print Zim and system info.
2022-10-23 20:34:14 -04:00
%Blist%b List all modules currently defined in %B${ ZIM_CONFIG_FILE :- ${ ZDOTDIR :- ${ HOME } } /.zimrc } %b.
2021-11-08 20:05:32 -05:00
Use %B-v%b to also see the modules details.
2022-01-10 09:29:32 -05:00
%Binit%b Same as %Binstall%b, but with output tailored to be used at terminal startup.
2022-01-18 20:34:36 -05:00
%Binstall%b Install new modules. Also does %Bbuild%b, %Bcheck-dumpfile%b and %Bcompile%b. Use %B-v%b to
2022-05-07 17:31:08 -04:00
also see their output, any on-pull output, and see skipped modules.
2021-11-15 19:37:40 -05:00
%Buninstall%b Delete unused modules. Prompts for confirmation. Use %B-q%b for quiet uninstall.
2022-01-18 20:34:36 -05:00
%Bupdate%b Update current modules. Also does %Bbuild%b, %Bcheck-dumpfile%b and %Bcompile%b. Use %B-v%b
2022-05-07 17:31:08 -04:00
to also see their output, any on-pull output, and see skipped modules.
2021-09-21 09:27:56 -04:00
%Bupgrade%b Upgrade zimfw. Also does %Bcompile%b. Use %B-v%b to also see its output.
%Bversion%b Print zimfw version.
2019-12-01 16:00:47 -05:00
Options:
2021-09-21 17:36:32 -04:00
%B-q%b Quiet ( yes to prompts, and only outputs errors)
2021-09-21 09:27:56 -04:00
%B-v%b Verbose ( outputs more details) "
2022-09-26 21:33:49 -04:00
local -Ua _znames _zroot_dirs _zdisabled_root_dirs
2022-10-06 20:52:57 -04:00
local -A _zfrozens _ztools _zdirs _zurls _ztypes _zrevs _zsubmodules _zonpulls _zifs
2022-05-17 19:43:59 -04:00
local -a _zfpaths _zfunctions _zcmds _zunused_dirs
2020-01-11 16:23:11 -05:00
local -i _zprintlevel = 1
2019-12-01 16:00:47 -05:00
if ( ( # > 2 )); then
2021-11-08 11:38:56 -05:00
print -u2 -PlR " %F{red} ${ 0 } : Too many options%f " '' ${ zusage }
2021-03-19 18:13:21 -04:00
return 2
2019-12-01 16:00:47 -05:00
elif ( ( # > 1 )); then
case ${ 2 } in
2020-01-11 16:23:11 -05:00
-q) _zprintlevel = 0 ; ;
-v) _zprintlevel = 2 ; ;
2019-12-01 16:00:47 -05:00
*)
2021-11-08 11:38:56 -05:00
print -u2 -PlR " %F{red} ${ 0 } : Unknown option ${ 2 } %f " '' ${ zusage }
2021-03-19 18:13:21 -04:00
return 2
2019-12-01 16:00:47 -05:00
; ;
esac
2019-01-22 19:40:43 -05:00
fi
2020-01-15 12:35:30 -05:00
if ! zstyle -t ':zim' disable-version-check; then
_zimfw_version_check
2020-01-13 13:14:31 -05:00
fi
2022-01-10 09:29:32 -05:00
local _zrestartmsg = ' Restart your terminal for changes to take effect.'
2019-01-22 19:40:43 -05:00
case ${ 1 } in
2020-01-11 16:23:11 -05:00
build)
2021-09-19 14:37:13 -04:00
_zimfw_source_zimrc 2 && _zimfw_build || return 1
2020-01-11 16:23:11 -05:00
( ( _zprintlevel-- ) )
2022-01-18 20:34:36 -05:00
_zimfw_check_dumpfile && _zimfw_compile
2020-01-11 16:23:11 -05:00
; ;
2022-01-18 20:34:36 -05:00
check-dumpfile) _zimfw_source_zimrc 2 && _zimfw_check_dumpfile ; ;
2021-09-19 14:37:13 -04:00
clean) _zimfw_source_zimrc 2 && _zimfw_clean_compiled && _zimfw_clean_dumpfile ; ;
clean-compiled) _zimfw_source_zimrc 2 && _zimfw_clean_compiled ; ;
2019-12-01 16:00:47 -05:00
clean-dumpfile) _zimfw_clean_dumpfile ; ;
2022-05-17 19:43:59 -04:00
compile) _zimfw_source_zimrc 2 && _zimfw_compile ; ;
2020-01-13 13:14:31 -05:00
help ) print -PR ${ zusage } ; ;
2019-12-01 16:00:47 -05:00
info) _zimfw_info ; ;
2021-09-19 14:37:13 -04:00
list)
2022-05-17 19:43:59 -04:00
_zimfw_source_zimrc 3 && zargs -n 1 -- " ${ _znames [@] } " -- _zimfw_run_list && \
2021-09-29 22:22:28 -04:00
_zimfw_list_unuseds ' (unused)'
2021-09-19 14:37:13 -04:00
; ;
2022-01-10 09:29:32 -05:00
init)
_zrestartmsg =
_zimfw_install_update install || return 1
( ( _zprintlevel-- ) )
_zimfw_print -PR " Done with install. ${ _zrestartmsg } " # Only printed in verbose mode
2022-01-18 20:34:36 -05:00
_zimfw_source_zimrc 2 && _zimfw_build && _zimfw_check_dumpfile && _zimfw_compile
2022-01-10 09:29:32 -05:00
; ;
2019-01-22 19:40:43 -05:00
install| update)
2021-10-26 12:15:40 -04:00
_zimfw_install_update ${ 1 } || return 1
2022-01-10 09:29:32 -05:00
_zimfw_print -PR " Done with ${ 1 } . ${ _zrestartmsg } "
2020-01-11 16:23:11 -05:00
( ( _zprintlevel-- ) )
2022-01-18 20:34:36 -05:00
_zimfw_source_zimrc 2 && _zimfw_build && _zimfw_check_dumpfile && _zimfw_compile
2019-01-22 19:40:43 -05:00
; ;
2021-09-19 14:37:13 -04:00
uninstall) _zimfw_source_zimrc 2 && _zimfw_list_unuseds && _zimfw_uninstall ; ;
2020-01-11 16:23:11 -05:00
upgrade)
_zimfw_upgrade || return 1
( ( _zprintlevel-- ) )
2022-05-17 19:43:59 -04:00
_zimfw_source_zimrc 2 && _zimfw_compile
2020-01-11 16:23:11 -05:00
; ;
2020-01-13 13:14:31 -05:00
version) print -PR ${ _zversion } ; ;
2019-01-22 19:40:43 -05:00
*)
2021-11-08 11:38:56 -05:00
print -u2 -PlR " %F{red} ${ 0 } : Unknown action ${ 1 } %f " '' ${ zusage }
2021-03-19 18:13:21 -04:00
return 2
2019-01-22 19:40:43 -05:00
; ;
esac
}
2021-09-20 22:14:12 -04:00
if [ [ ${ functrace [1] } = = zmodule:* ] ] ; then
zmodule " ${ @ } "
else
zimfw " ${ @ } "
fi