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
2021-01-04 21:20:03 -05:00
# Copyright (c) 2016-2021 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
command mv -f ${ 1 } ${ 2 } && \
2020-01-09 07:13:31 -05:00
_zimfw_print -PR " %F{green})%f %B ${ 2 } :%b Updated. Restart your terminal for changes to take effect. "
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
if [ [ ${ ztarget } -ot ${ ZDOTDIR :- ${ HOME } } /.zimrc ] ] ; then
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 \"\${@}\" } "
2021-09-21 09:27:56 -04:00
# Remove all prefixes from _zfpaths, _zfunctions and _zcmds
2021-09-19 14:37:13 -04:00
local -r zpre = $'*\0'
if ( ( ${# _zfpaths } ) ) print -R 'fpath=(' ${ ${ _zfpaths # ${ ~zpre } } : A } ' ${fpath})'
if ( ( ${# _zfunctions } ) ) print -R 'autoload -Uz ' ${ _zfunctions # ${ ~zpre } }
print -R ${ (F)_zcmds# ${ ~zpre } }
2019-12-14 22:21:34 -05:00
) ${ ztarget }
}
_zimfw_build_login_init( ) {
2020-07-04 15:59:37 -04:00
# Array with unique dirs. ${ZIM_HOME} or any subdirectory should only occur once.
local -Ur zscriptdirs = ( ${ ZIM_HOME } ${ ${ _zdirs ## ${ ZIM_HOME } /* } : A } )
2021-07-11 16:58:15 -04:00
local -r zscriptglob = ( " ${ ^zscriptdirs[@] } /(^*test*/)#*.zsh(|-theme)(N-.) " ) 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
if [ [ ${ ztarget } -ot ${ ZDOTDIR :- ${ HOME } } /.zimrc ] ] ; then
command mv -f ${ ztarget } { ,.old} || return 1
fi
2019-12-14 22:21:34 -05:00
_zimfw_mv = (
print -Rn " () {
2019-12-12 09:00:19 -05:00
setopt LOCAL_OPTIONS CASE_GLOB EXTENDED_GLOB
2019-12-27 09:51:02 -05:00
autoload -Uz zrecompile
local zdumpfile zfile
2019-01-22 19:40:43 -05:00
# Compile the completion cache; significant speedup
2019-12-14 22:21:34 -05:00
zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile = \$ { ZDOTDIR:-\$ { HOME} } /.zcompdump
if [ [ -f \$ { zdumpfile} ] ] ; then
zrecompile -p \$ { 1} \$ { zdumpfile} || return 1
2019-01-22 19:40:43 -05:00
fi
2019-12-27 09:51:02 -05:00
# Compile Zsh startup files
2020-06-05 23:09:23 -04:00
for zfile in \$ { ZDOTDIR:-\$ { HOME} } /.z( shenv| profile| shrc| login| logout ) ( N-.) ; do
2019-12-27 09:51:02 -05:00
zrecompile -p \$ { 1} \$ { zfile} || return 1
2019-01-22 19:40:43 -05:00
done
2019-12-27 09:51:02 -05:00
# Compile Zim scripts
2020-06-05 23:09:23 -04:00
for zfile in ${ zscriptglob } ; do
2019-12-14 22:21:34 -05:00
zrecompile -p \$ { 1} \$ { zfile} || return 1
2019-01-22 19:40:43 -05:00
done
2020-06-05 23:09:23 -04:00
if [ [ \$ { 1} != -q ] ] print -P 'Done with compile.'
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( ) {
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-01-22 19:40:43 -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 ${ ZDOTDIR :- ${ 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}x ${ 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}x ${ 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-01-22 19:40:43 -05:00
fi
2019-12-01 16:00:47 -05:00
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 %%/## }
2021-09-19 14:37:13 -04:00
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}x ${ 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
2021-09-19 14:37:13 -04:00
case ${ 1 } in
-b| --branch| -t| --tag| -u| --use)
2021-09-21 17:36:32 -04:00
if [ [ -z ${ zurl } ] ] _zimfw_print -u2 -PR " %F{yellow}! ${ funcfiletrace [1] } :%B ${ zmodule } :%b The zmodule option ${ 1 } 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
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}x ${ 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
2019-01-22 19:40:43 -05:00
done
2021-09-19 14:37:13 -04:00
if ( ( _zflags & 1 ) ) ; then
_zmodules_zargs += ( " ${ ztool } " " ${ _zargs_action } " " ${ zmodule } " " ${ zdir } " " ${ zurl } " " ${ ztype } " " ${ zrev } " " ${ zfrozen } " " ${ zdisabled } " )
fi
if ( ( _zflags & 2 ) ) ; then
2019-12-03 07:07:59 -05:00
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}x ${ 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
2021-09-21 17:36:32 -04:00
_zimfw_print -u2 -PR " %F{yellow}! ${ funcfiletrace [1] } :%B ${ zmodule } :%b Nothing found to be initialized. Customize the module name or initialization with %Bzmodule%b options.%f " $'\n\n' ${ zusage }
2020-05-25 15:01:00 -04:00
fi
2020-07-02 18:16:44 -04:00
_zmodules += ( ${ zmodule } )
_zdirs += ( ${ zdir } )
2021-09-21 09:27:56 -04:00
# Prefix is added to all _zfpaths, _zfunctions and _zcmds to distinguish the originating modules
local -r zpre = ${ zmodule } $'\0'
_zfpaths += ( ${ zpre } ${ ^zfpaths } )
_zfunctions += ( ${ zpre } ${ ^zfunctions } )
_zcmds += ( ${ zpre } ${ ^zcmds } )
2019-12-01 16:00:47 -05:00
fi
fi
}
_zimfw_source_zimrc( ) {
2021-09-19 14:37:13 -04:00
local -r ztarget = ${ ZDOTDIR :- ${ HOME } } /.zimrc _zflags = ${ 1 } _zargs_action = ${ 2 }
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
2021-09-19 14:37:13 -04:00
if ( ( _zflags & 1 && ${# _zmodules_zargs } = = 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
2019-01-22 19:40:43 -05:00
}
2021-09-19 14:37:13 -04:00
_zimfw_list_unuseds( ) {
local -i i = 1
local -a zinstalled = ( ${ ZIM_HOME } /modules/*( N/:t) ) subdirs
# Search into subdirectories
while ( ( i <= ${# zinstalled } ) ) ; do
if ( ( ${ _zmodules [(I) ${ zinstalled [i] } /*] } || ${ _zdisableds [(I) ${ zinstalled [i] } /*] } ) ) ; then
subdirs = ( ${ ZIM_HOME } /modules/${ zinstalled [i] } /*( N/:t) )
zinstalled += ( ${ zinstalled [i] } /${ ^subdirs } )
zinstalled[ i] = ( )
else
( ( i++ ) )
fi
done
# Unused = all installed modules not in _zmodules and _zdisableds
_zunuseds = ( ${ ${ zinstalled : |_zmodules } : |_zdisableds } )
local zunused
for zunused in ${ _zunuseds } ; do
_zimfw_print -PR " %B ${ zunused } :%b ${ ZIM_HOME } /modules/ ${ zunused } ${ 1 } "
done
}
2020-01-15 12:35:30 -05:00
_zimfw_version_check( ) {
if ( ( _zprintlevel > 0 ) ) ; then
setopt LOCAL_OPTIONS EXTENDED_GLOB
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)
print ${ ${ (On) ${ (f) " $( command git ls-remote --tags --refs \
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
print -u2 -PR " %F{yellow}Latest zimfw version is %B ${ zlatest_version } %b. You're using version %B ${ _zversion } %b. Run %Bzimfw upgrade%b to upgrade.%f " $'\n'
fi
2020-01-15 12:35:30 -05:00
fi
fi
}
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.
local -Ur zscriptdirs = ( ${ ZIM_HOME } ${ ${ _zdirs ## ${ ZIM_HOME } /* } : A } )
2019-12-27 09:51:02 -05:00
local zopt
2020-02-03 20:43:36 -05:00
if ( ( _zprintlevel > 0 ) ) zopt = '-v'
2020-07-31 22:03:47 -04:00
command rm -f ${ zopt } ${ ^zscriptdirs } /**/*.zwc( | .old) ( N) || return 1
2020-01-08 08:19:52 -05:00
command rm -f ${ zopt } ${ ZDOTDIR :- ${ HOME } } /.z( shenv| profile| shrc| login| logout ) .zwc( | .old) ( N) || return 1
2021-08-10 10:56:25 -04:00
_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
2020-02-03 20:43:36 -05:00
if ( ( _zprintlevel > 0 ) ) zopt = '-v'
2020-01-09 07:21:16 -05:00
command rm -f ${ zopt } ${ zdumpfile } ( | .zwc( | .old) ) ( N) || return 1
2020-01-09 07:13:31 -05:00
_zimfw_print -P 'Done with clean-dumpfile. Restart your terminal to dump an updated configuration.'
2019-01-22 19:40:43 -05:00
}
2019-12-14 22:21:34 -05:00
_zimfw_compile( ) {
2020-01-11 16:23:11 -05:00
local zopt
2020-02-03 20:43:36 -05:00
if ( ( _zprintlevel <= 0 ) ) zopt = '-q'
2020-01-11 16:23:11 -05:00
source ${ ZIM_HOME } /login_init.zsh ${ zopt }
2019-12-14 22:21:34 -05:00
}
2019-01-22 19:40:43 -05:00
_zimfw_info( ) {
2021-09-21 17:36:32 -04:00
print -R 'zimfw version: ' ${ _zversion } ' (built at 2021-09-21 21:19:43 UTC, previous commit is 9a67adf)'
2020-01-15 12:35:30 -05:00
print -R 'ZIM_HOME: ' ${ ZIM_HOME }
print -R 'Zsh version: ' ${ ZSH_VERSION }
print -R 'System info: ' $( command uname -a)
2019-01-22 19:40:43 -05:00
}
2019-12-10 18:19:01 -05:00
_zimfw_uninstall( ) {
2021-09-19 14:37:13 -04:00
local zopt
2020-02-03 20:43:36 -05:00
if ( ( _zprintlevel > 0 ) ) zopt = '-v'
2021-09-19 14:37:13 -04:00
if ( ( ${# _zunuseds } ) ) ; then
if ( ( _zprintlevel <= 0 ) ) || read -q " ?Uninstall ${# _zunuseds } module(s) listed above [y/N]? " ; then
2020-10-26 20:32:25 -04:00
_zimfw_print
2021-09-19 14:37:13 -04:00
command rm -rf ${ zopt } ${ ZIM_HOME } /modules/${ ^_zunuseds } || 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
2020-02-03 20:43:36 -05: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( ) {
local -r ztool = ${ 1 } zmodule = ${ 3 } zdir = ${ 4 } zurl = ${ 5 } ztype = ${ 6 } zrev = ${ 7 }
local -ri zfrozen = ${ 8 } zdisabled = ${ 9 }
print -PRn " %B ${ zmodule } :%b ${ zdir } "
if [ [ -z ${ zurl } ] ] print -Pn ' %F{blue}%B(external)%b'
if ( ( ${ zfrozen } ) ) print -Pn ' %F{cyan}(frozen)'
if ( ( ${ zdisabled } ) ) print -Pn ' %F{magenta}(disabled)'
print -P '%f'
if ( ( _zprintlevel > 1 ) ) ; then
if [ [ ${ zfrozen } -eq 0 && -n ${ zurl } ] ] ; then
print -Rn " From: ${ zurl } , "
if [ [ -z ${ zrev } ] ] ; then
print -n 'default branch'
else
print -Rn " ${ ztype } ${ zrev } "
fi
print -R " , using ${ ztool } "
fi
2021-09-21 09:27:56 -04:00
# Match and remove the current module prefix from _zfpaths, _zfunctions and _zcmds
2021-09-19 14:37:13 -04:00
local -r zpre = ${ zmodule } $'\0'
local -r zfpaths = ( ${ ${ (M)_zfpaths : # ${ zpre } * } # ${ zpre } } ) zfunctions = ( ${ ${ (M)_zfunctions : # ${ zpre } * } # ${ zpre } } ) zcmds = ( ${ ${ (M)_zcmds : # ${ zpre } * } # ${ zpre } } )
if ( ( ${# zfpaths } ) ) print -R " fpath: ${ zfpaths [@] } "
if ( ( ${# zfunctions } ) ) print -R " autoload: ${ zfunctions [@] } "
if ( ( ${# zcmds } ) ) print -R " cmd: ${ (j : ; : )zcmds[@] } "
fi
}
2021-04-03 10:35:28 -04:00
_zimfw_run_tool( ) {
2021-09-19 14:37:13 -04:00
local -ri zfrozen = ${ 8 }
if ( ( zfrozen ) ) return 0
local -r ztool = ${ 1 } zaction = ${ 2 } zmodule = ${ 3 } zdir = ${ 4 } zurl = ${ 5 } clear_line = $'\E[2K\r'
if [ [ -z ${ zurl } ] ] return 0
2021-04-03 10:35:28 -04:00
case ${ zaction } in
install)
if [ [ -e ${ zdir } ] ] ; then
# Already installed
return 0
fi
_zimfw_print -Rn ${ clear_line } " Installing ${ zmodule } ... "
; ;
update)
if [ [ ! -d ${ zdir } ] ] ; then
print -u2 -PR " %F{red}x %B ${ zmodule } :%b Not installed. Run %Bzimfw install%b to install.%f "
return 1
fi
_zimfw_print -Rn ${ clear_line } " Updating ${ zmodule } ... "
; ;
*)
print -u2 -PR " %F{red}x %B ${ zmodule } :%b Unknown action ${ zaction } %f "
return 1
; ;
esac
local zcmd
case ${ ztool } in
degit) zcmd = " # This runs in a new shell
2021-09-19 14:37:13 -04:00
readonly -i PRINTLEVEL = \$ { 1}
readonly ACTION = \$ { 2} MODULE = \$ { 3} DIR = \$ { 4} URL = \$ { 5} REV = \$ { 7} CLEAR_LINE = $'\E[2K\r'
2021-08-09 23:28:13 -04:00
readonly TEMP = .zdegit_\$ { RANDOM}
readonly TARBALL_TARGET = \$ { DIR} /\$ { TEMP} _tarball.tar.gz INFO_TARGET = \$ { DIR} /.zdegit
2021-04-03 10:35:28 -04:00
print_error( ) {
print -u2 -PR \$ { CLEAR_LINE} \" %F{ red} x %B\$ { MODULE} :%b \$ { 1} %f\" \$ { 2:+$'\n' \$ { ( F) :- \$ { ( f) ^2} } }
}
2021-08-09 21:32:32 -04:00
print_okay( ) {
2021-09-21 17:36:32 -04:00
if ( ( PRINTLEVEL > 0 ) ) ; then
local -r log = \$ { 2:+$'\n' \$ { ( F) :- \$ { ( f) ^2} } }
if [ [ -e \$ { DIR} /.gitmodules ] ] ; then
print -u2 -PR \$ { CLEAR_LINE} \" %F{ yellow} ! %B\$ { MODULE} :%b \$ { ( C) 1} . Module contains git submodules, which are not supported by Zim' s degit and were not \$ { 1} .%f\" \$ { log}
2021-04-03 10:35:28 -04:00
else
2021-09-21 17:36:32 -04:00
print -PR \$ { CLEAR_LINE} \" %F{ green} ) %f %B\$ { MODULE} :%b \$ { ( C) 1} \" \$ { log}
2021-04-03 10:35:28 -04:00
fi
fi
}
download_tarball( ) {
setopt LOCAL_OPTIONS EXTENDED_GLOB
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] }
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-04-08 17:26:33 -04:00
if ! print -R \$ { URL} $'\n' \$ { REV} $'\n' \" 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
}
( ) {
case \$ { ACTION} in
install)
{
2021-08-10 10:56:25 -04:00
create_dir \$ { DIR} && download_tarball && untar_tarball \$ { DIR} && print_okay installed
2021-04-03 10:35:28 -04:00
} always {
2021-04-21 20:37:47 -04:00
# return 1 does not change \${TRY_BLOCK_ERROR}, only changes \${?}
2021-04-03 10:35:28 -04:00
( ( TRY_BLOCK_ERROR = ? ) )
command rm -f \$ { TARBALL_TARGET} 2>/dev/null
if ( ( TRY_BLOCK_ERROR ) ) ; then
command rm -rf \$ { DIR} 2>/dev/null
fi
}
; ;
update)
2021-04-21 20:37:47 -04:00
if [ [ -r \$ { DIR} /.zim_degit_info ] ] command mv -f \$ { DIR} /.zim_degit_info \$ { INFO_TARGET}
2021-04-03 10:35:28 -04:00
if [ [ ! -r \$ { INFO_TARGET} ] ] ; then
print_error \" Module was not installed using Zim' s degit. Will not try to update. You can disable this with the zmodule option -z| --frozen.\"
return 1
fi
2021-04-21 20:37:47 -04:00
local -r dir_new = \$ { DIR} \$ { TEMP}
2021-04-03 10:35:28 -04:00
{
download_tarball || return 1
if [ [ ! -e \$ { TARBALL_TARGET} ] ] ; then
if ( ( PRINTLEVEL > 0 ) ) print -PR \$ { CLEAR_LINE} \" %F{ green} ) %f %B\$ { MODULE} :%b Already up to date\"
return 0
fi
create_dir \$ { dir_new} && untar_tarball \$ { dir_new} || return 1
2021-04-23 20:15:20 -04:00
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
2021-04-08 17:26:33 -04:00
if ! ERR = \$ ( { command cp -f \$ { INFO_TARGET} \$ { dir_new} && \
command rm -rf \$ { DIR} && command mv -f \$ { dir_new} \$ { DIR} } 2>& 1) ; then
2021-04-03 10:35:28 -04:00
print_error \" Error updating \$ { DIR} \" \$ { ERR}
return 1
fi
2021-08-09 21:32:32 -04:00
print_okay updated \$ { LOG}
2021-04-03 10:35:28 -04:00