1
0
Fork 0
mirror of synced 2024-12-25 15:33:20 -05:00

Zim script in a single file \o/

Also moved the templates out of this repository, and into the
zimfw/install repo.

This is a second big change after introducing the plugin mechanism. This
makes installation and upgrading of Zim straightforward. Maybe the most
important aspect of having the script in a single file is not having to
manage "git repos inside git repos" (see #297), since the single file
exists by itself and is not version-controlled (with git).

I've implemented a two-stage sourcing of the file, so most of the file
is only sourced when needed (namely when calling `zimfw` with any action
other than `login-init`). The two-stage process is designed to avoid
compromising the startup speed, which is our top priority.

In an effort to help making the script maintainable, I've broken it into
small ERB templates. This also adds the ability to pre-process the Zsh
code with Ruby code. To build the script, use `make`.
This commit is contained in:
Eric Nielsen 2019-01-22 19:40:43 -05:00
parent 27508e363c
commit 1980ca091d
No known key found for this signature in database
GPG key ID: 47D1DBFA0765A1FB
28 changed files with 681 additions and 354 deletions

4
Makefile Normal file
View file

@ -0,0 +1,4 @@
srcfiles := src/zimfw.zsh.erb $(wildcard src/*/*.erb)
zimfw.zsh: $(srcfiles)
erb $< >| $@

View file

@ -9,28 +9,28 @@ Zsh IMproved FrameWork
What is Zim?
------------
Zim is a Zsh configuration framework with [blazing speed][speed] and modular extensions.
Zim is a Zsh configuration framework with [blazing speed] and modular extensions.
Zim is very easy to customize, and comes with a rich set of modules and features without compromising on speed or functionality!
What does Zim offer?
-----------------
If you're here, it means you want to see the cool shit Zim can do. Check out the [available modules][modules]!
If you're here, it means you want to see the cool shit Zim can do. Check out the [available modules]!
Below is a brief showcase of Zim's features.
### Speed
For a speed comparison between Zim and other frameworks, see [this wiki entry][speed].
For a speed comparison between Zim and other frameworks, see [this wiki entry][blazing speed].
### Themes
To preview some of the available themes, check the [themes wiki page][themes].
To preview some of the available themes, check the [themes wiki page].
### Fish-shell history navigation
![history-substring-search][fish_shell]
![history-substring-search]
### Syntax highlighting
![syntax-highlighting][syntax_highlighting]
![syntax-highlighting]
### And much more!
Zim has many modules! Enable as many or as few as you'd like.
@ -39,10 +39,16 @@ Installation
------------
Installing Zim is easy:
curl -s --proto -all,+https https://raw.githubusercontent.com/zimfw/install/develop/install.zsh | zsh
* With curl:
curl -fsSL https://raw.githubusercontent.com/zimfw/install/develop/install.zsh | zsh
* With wget:
wget -nv -O - https://raw.githubusercontent.com/zimfw/install/develop/install.zsh | zsh
Open a new terminal and you're done! Enjoy your Zsh IMproved! Take some time to
read about the [available modules][modules] and tweak your `.zshrc` file.
read about the [available modules] and tweak your `.zshrc` file.
If you have a different shell framework installed (like oh-my-zsh or prezto),
*uninstall those first to prevent conflicts*.
@ -108,8 +114,8 @@ Uninstalling
The best way to remove Zim is to manually delete `~/.zim`, `~/.zimrc`, and
remove the initialization lines from your `~/.zshrc` and `~/.zlogin`.
[fish_shell]: https://i.eriner.me/zim_history-substring-search.gif
[syntax_highlighting]: https://i.eriner.me/zim_syntax-highlighting.gif
[speed]: https://github.com/zimfw/zimfw/wiki/Speed
[modules]: https://github.com/zimfw/zimfw/wiki/Modules
[themes]: https://github.com/zimfw/zimfw/wiki/Themes
[history-substring-search]: https://i.eriner.me/zim_history-substring-search.gif
[syntax-highlighting]: https://i.eriner.me/zim_syntax-highlighting.gif
[blazing speed]: https://github.com/zimfw/zimfw/wiki/Speed
[available modules]: https://github.com/zimfw/zimfw/wiki/Modules
[themes wiki page]: https://github.com/zimfw/zimfw/wiki/Themes

120
init.zsh
View file

@ -1,120 +0,0 @@
autoload -Uz is-at-least && if ! is-at-least 5.2; then
print -u2 "init: error starting Zim: You're using Zsh version ${ZSH_VERSION} and versions < 5.2 are not supported. Update your Zsh."
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 -u2 "init: module ${zmodule} not installed"
else
for zfile in ${zdir}/{init.zsh,${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}(.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
}

View file

@ -1 +0,0 @@
zimfw login-init -q &!

View file

@ -0,0 +1,4 @@
autoload -Uz is-at-least && if ! is-at-least <%= min_zsh_version %>; then
print -u2 "init: error starting Zim: You're using Zsh version ${ZSH_VERSION} and versions < <%= min_zsh_version %> are not supported. Update your Zsh."
return 1
fi

View file

@ -0,0 +1,46 @@
# Define Zim location
: ${ZIM_HOME=${0:h}}
# Source user configuration
[[ -f <%= home %>/.zimrc ]] && source <%= 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/<%= functions_glob %>(-.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 -u2 "init: module ${zmodule} not installed"
else
for zfile in ${zdir}/{init.zsh,${zmodule}.{zsh,plugin.zsh,zsh-theme,sh}}; do
if [[ -f ${zfile} ]]; then
source ${zfile}
break
fi
done
fi
done
}

View file

@ -0,0 +1,35 @@
_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="<%= home %>/.zcompdump"
if [[ -f ${zdumpfile} ]]; then
zrecompile -p ${1} ${zdumpfile} || return 1
fi
# Compile .zshrc
zrecompile -p ${1} <%= home %>/.zshrc || return 1
# Compile enabled modules' autoloaded functions
for zdir in ${ZIM_HOME}/modules/${^zmodules}/functions(/FN); do
zrecompile -p ${1} ${zdir}.zwc ${zdir}/<%= functions_glob %>(-.N) || return 1
done
# Compile enabled modules' scripts
for zfile in ${ZIM_HOME}/modules/${^zmodules}/(^*test*/)#*.zsh{,-theme}(.NLk+1); do
zrecompile -p ${1} ${zfile} || return 1
done
# Compile this script
zrecompile -p ${1} ${ZIM_HOME}/<%= script_filename %> || return 1
if [[ ${1} != -q ]]; then
print -P '%F{green}✓%f Done with compile.'
fi
}

View file

@ -0,0 +1,9 @@
zimfw() {
case ${1} in
compile|login-init) _zimfw_compile ${2} ;;
*)
source ${ZIM_HOME}/<%= script_filename %> <%= second_stage_guard %>
zimfw "${@}"
;;
esac
}

View file

@ -0,0 +1,27 @@
_zimfw_modules() {
local zmodule zurl ztype zrev
local -a zmodules
local -A zoptions
zstyle -a ':zim' modules 'zmodules'
for zmodule in ${zmodules}; do
zstyle -a ':zim:module' ${zmodule} 'zoptions'
[[ ${zoptions[frozen]} == yes ]] && continue
zurl=${zoptions[url]:-${zmodule}}
if [[ ${zurl} != /* && ${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
if [[ -n ${zoptions[tag]} ]]; then
ztype=tag
zrev=${zoptions[tag]}
else
ztype=branch
zrev=${zoptions[branch]:-master}
fi
# Cannot have an empty space at the EOL because this is read by xargs -L1
print "'${ZIM_HOME}/modules/${zmodule}' '${zurl}' '${ztype}' '${zrev}'${1:+ ${1}}"
done
}

View file

@ -0,0 +1,21 @@
_zimfw_clean_modules() {
local zdir zmodule
local -a zmodules
local -A zoptions
# Source .zimrc to refresh zmodules
[[ -f <%= home %>/.zimrc ]] && source <%= home %>/.zimrc
zstyle -a ':zim' modules 'zmodules'
for zdir in ${ZIM_HOME}/modules/*(/N); do
zmodule=${zdir:t}
# If zmodules does not contain the zmodule
if (( ! ${zmodules[(I)${zmodule}]} )); then
zstyle -a ':zim:module' ${zmodule} 'zoptions'
[[ ${zoptions[frozen]} == yes ]] && continue
command rm -rf ${zdir} || return 1
[[ ${1} != -q ]] && print ${zdir}
fi
done
if [[ ${1} != -q ]]; then
print -P "%F{green}✓%f Done with clean-modules."
fi
}

View file

@ -0,0 +1,13 @@
_zimfw_clean_compiled() {
setopt LOCAL_OPTIONS PIPE_FAIL
local find_opt rm_opt
if [[ ${1} != -q ]]; then
find_opt='-print'
rm_opt='-v'
fi
command find ${ZIM_HOME} \( -name '*.zwc' -o -name '*.zwc.old' \) -delete ${find_opt} || return 1
command rm -f ${rm_opt} <%= home %>/.zshrc.zwc{,.old} || return 1
if [[ ${1} != -q ]]; then
print -P "%F{green}✓%f Done with clean-compiled. Run %Bzimfw compile%b to re-compile."
fi
}

View file

@ -0,0 +1,10 @@
_zimfw_clean_dumpfile() {
setopt LOCAL_OPTIONS PIPE_FAIL
local zdumpfile zout zopt
zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile="<%= home %>/.zcompdump"
[[ ${1} != -q ]] && zopt='-v'
command rm -f ${zopt} ${zdumpfile}{,.zwc{,.old}} || return 1
if [[ ${1} != -q ]]; then
print -P "%F{green}✓%f Done with clean-dumpfile. Restart your terminal to dump an updated configuration."
fi
}

View file

@ -0,0 +1,5 @@
_zimfw_info() {
print 'Zim version: <%= version %> (previous commit is <%= `git rev-parse --short HEAD | tr -d '\r\n'` %>)'
print "Zsh version: ${ZSH_VERSION}"
print "System info: $(command uname -a)"
}

View file

@ -0,0 +1,20 @@
_zimfw_upgrade() {
local zscript=${ZIM_HOME}/<%= script_filename %>
local zurl=https://raw.githubusercontent.com/zimfw/zimfw/develop/<%= script_filename %>
if (( ${+commands[wget]} )); then
command wget -nv ${1} -O ${zscript}.new ${zurl} || return 1
else
command curl -fsSL -o ${zscript}.new ${zurl} || return 1
fi
if command cmp -s ${zscript}{,.new}; then
command rm ${zscript}.new && \
if [[ ${1} != -q ]]; then
print -P "%F{green}✓%f <%= script_filename %>: Already up to date."
fi
else
command mv ${zscript}{,.old} && command mv ${zscript}{.new,} && \
if [[ ${1} != -q ]]; then
print -P "%F{green}✓%f <%= script_filename %>: upgraded. Restart your terminal for changes to take effect."
fi
fi
}

View file

@ -0,0 +1,57 @@
unfunction zimfw
zimfw() {
local zusage="usage: ${0} <action> [-q]
actions:
clean Clean all (see below).
clean-modules Clean unused modules.
clean-compiled Clean Zsh compiled files.
clean-dumpfile Clean completion dump file.
compile Compile Zsh files.
info Print Zim and system info.
install Install new modules.
update Update current modules.
upgrade Upgrade Zim.
options:
-q Quiet, only outputs errors."
if [[ ${#} -ne 1 && ${2} != -q ]]; then
print -u2 ${zusage}
return 1
fi
local ztool
case ${1} in
install)
ztool="<%= render_escaped("src/tools/install.zsh.erb") %>"
;;
update)
ztool="<%= render_escaped("src/tools/update.zsh.erb") %>"
;;
esac
case ${1} in
clean)
_zimfw_clean_modules ${2} && \
_zimfw_clean_compiled ${2} && \
_zimfw_clean_dumpfile ${2}
;;
clean-modules) _zimfw_clean_modules ${2} ;;
clean-compiled) _zimfw_clean_compiled ${2} ;;
clean-dumpfile) _zimfw_clean_dumpfile ${2} ;;
compile|login-init) _zimfw_compile ${2} ;;
info) _zimfw_info ${2} ;;
install|update)
# Source .zimrc to refresh zmodules
[[ -f <%= home %>/.zimrc ]] && source <%= home %>/.zimrc
_zimfw_modules ${2} | xargs -L1 -P10 zsh -c ${ztool} ${1} && \
if [[ ${2} != -q ]]; then
print -P "%F{green}✓%f Done with ${1}. Restart your terminal for any changes to take effect."
fi
;;
upgrade) _zimfw_upgrade ${2} ;;
*)
print -u2 ${zusage}
return 1
;;
esac
}

View file

@ -1,18 +1,20 @@
# This runs in a new shell
URL=${1}
DIR=${2}
DIR=${1}
URL=${2}
REV=${4}
OPT=${5}
MODULE=${DIR:t}
CLEAR_LINE="\033[2K\r"
if [[ -e ${DIR} ]]; then
# Already exists
return 0
fi
[[ ${OPT} != -q ]] && print -n "${CLEAR_LINE}Installing ${MODULE} …"
if ERR=$(command git clone -b ${REV} -q --recursive ${URL} ${DIR} 2>&1); then
if [[ ${OPT} != -q ]]; then
print -P "%F{green}✓%f ${MODULE}: Installed"
print -P "${CLEAR_LINE}%F{green}✓%f ${MODULE}: Installed"
fi
else
print -P "%F{red}✗ ${MODULE}: Error%f\n${ERR}"
print -P "${CLEAR_LINE}%F{red}✗ ${MODULE}: Error%f\n${ERR}"
return 1
fi

View file

@ -1,12 +1,14 @@
# This runs in a new shell
URL=${1}
DIR=${2}
DIR=${1}
URL=${2}
TYPE=${3}
REV=${4}
OPT=${5}
MODULE=${DIR:t}
CLEAR_LINE="\033[2K\r"
[[ ${OPT} != -q ]] && print -n "${CLEAR_LINE}Updating ${MODULE} …"
if ! cd ${DIR} 2>/dev/null; then
print -P "%F{red}✗ ${MODULE}: Not installed%f"
print -P "${CLEAR_LINE}%F{red}✗ ${MODULE}: Not installed%f"
return 1
fi
if [[ ${PWD} != $(command git rev-parse --show-toplevel 2>/dev/null) ]]; then
@ -14,32 +16,32 @@ if [[ ${PWD} != $(command git rev-parse --show-toplevel 2>/dev/null) ]]; then
return 0
fi
if [[ ${URL} != $(command git config --get remote.origin.url) ]]; then
print -P "%F{red}✗ ${MODULE}: URL does not match. Expected ${URL}. Will not try to update.%f"
print -P "${CLEAR_LINE}%F{red}✗ ${MODULE}: URL does not match. Expected ${URL}. Will not try to update.%f"
return 1
fi
if [[ ${TYPE} == 'tag' ]]; then
if [[ ${TYPE} == tag ]]; then
if [[ ${REV} == $(command git describe --tags --exact-match 2>/dev/null) ]]; then
[[ ${OPT} != -q ]] && print -P "%F{green}✓%f ${MODULE}: Already up to date"
[[ ${OPT} != -q ]] && print -P "${CLEAR_LINE}%F{green}✓%f ${MODULE}: Already up to date"
return 0
fi
fi
if ! ERR=$(command git fetch -pq origin ${REV} 2>&1); then
print -P "%F{red}✗ ${MODULE}: Error (1)%f\n${ERR}"
print -P "${CLEAR_LINE}%F{red}✗ ${MODULE}: Error (1)%f\n${ERR}"
return 1
fi
if [[ ${TYPE} == 'branch' ]]; then
if [[ ${TYPE} == branch ]]; then
LOG_REV="${REV}@{u}"
else
LOG_REV=${REV}
fi
LOG=$(command git log --graph --color --format='%C(yellow)%h%C(reset) %s %C(cyan)(%cr)%C(reset)' ..${LOG_REV} 2>/dev/null)
if ! ERR=$(command git checkout -q ${REV} -- 2>&1); then
print -P "%F{red}✗ ${MODULE}: Error (2)%f\n${ERR}"
print -P "${CLEAR_LINE}%F{red}✗ ${MODULE}: Error (2)%f\n${ERR}"
return 1
fi
if [[ ${TYPE} == 'branch' ]]; then
if [[ ${TYPE} == branch ]]; then
if ! OUT=$(command git merge --ff-only --no-progress -n 2>&1); then
print -P "%F{red}✗ ${MODULE}: Error (3)%f\n${OUT}"
print -P "${CLEAR_LINE}%F{red}✗ ${MODULE}: Error (3)%f\n${OUT}"
return 1
fi
# keep just first line of OUT
@ -50,9 +52,9 @@ fi
[[ -n ${LOG} ]] && OUT="${OUT}\n${LOG}"
if ERR=$(command git submodule update --init --recursive -q 2>&1); then
if [[ ${OPT} != -q ]]; then
print -P "%F{green}✓%f ${MODULE}: ${OUT}"
print -P "${CLEAR_LINE}%F{green}✓%f ${MODULE}: ${OUT}"
fi
else
print -P "%F{red}✗ ${MODULE}: Error (4)%f\n${ERR}"
print -P "${CLEAR_LINE}%F{red}✗ ${MODULE}: Error (4)%f\n${ERR}"
return 1
fi

40
src/zimfw.zsh.erb Normal file
View file

@ -0,0 +1,40 @@
<%# coding: UTF-8 %><%
class Zim
attr_reader :functions_glob, :home, :min_zsh_version, :script_filename, :second_stage_guard, :version
def initialize
@functions_glob = "^(_*|*.*|prompt_*_setup)"
@home = "${ZDOTDIR:-${HOME}}"
@min_zsh_version = "5.2"
@script_filename = "zimfw.zsh"
@second_stage_guard = 2
@version = "1.0.0-SNAPSHOT"
end
def render(filename)
ERB.new(File.read(filename)).result(binding)
end
def render_all(pattern)
Dir[pattern].sort.map { |filename| render(filename) }.join("\n")
end
def render_escaped(filename)
render(filename).gsub(/(\$[^']|")/, "\\\\\\1")
end
end
zim = Zim.new
%># AUTOMATICALLY GENERATED FILE. EDIT ONLY THE SOURCE FILES AND THEN COMPILE.
# DO NOT DIRECTLY EDIT THIS FILE!
if (( ! # )); then
# Stage 1 of sourcing this script
<%= zim.render_all("src/stage1/*.erb") %># Stage 1 done
elif [[ ${1} == <%= zim.second_stage_guard %> ]]; then
# Stage 2 of sourcing this script. Should only be done internally by zimfw.
<%= zim.render_all("src/stage2/*.erb") %># Stage 2 done
fi

View file

@ -1,107 +0,0 @@
################
# ZIM SETTINGS #
################
# Set input mode to 'emacs' (default) or 'vi'.
#zstyle ':zim:input' mode 'vi'
# Select what modules you would like enabled. Modules are sourced in the order given.
zstyle ':zim' modules \
directory environment git git-info history input utility \
steeef \
zsh-completions completion \
zsh-autosuggestions zsh-syntax-highlighting history-substring-search
# Modules setup configuration.
# See https://github.com/zimfw/zimfw/blob/develop/README.md#module-customization
zstyle ':zim:module' zsh-completions 'url' 'zsh-users/zsh-completions'
zstyle ':zim:module' zsh-autosuggestions 'url' 'zsh-users/zsh-autosuggestions'
zstyle ':zim:module' zsh-syntax-highlighting 'url' 'zsh-users/zsh-syntax-highlighting'
###################
# MODULE SETTINGS #
###################
#
# completion
#
# Set a custom path for the completion dump file.
# If none is provided, the default ${ZDOTDIR:-${HOME}}/.zcompdump is used.
#zstyle ':zim:completion' dumpfile "${ZDOTDIR:-${HOME}}/.zcompdump-${ZSH_VERSION}"
#
# environment
#
# Set the string below to the desired terminal title format string.
# The terminal title is redrawn upon directory change, however, variables like
# ${PWD} are only evaluated once. Use prompt expansion strings for dynamic data.
# See http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Simple-Prompt-Escapes
# For example, '%n@%m: %1~' corresponds to 'username@host: /current/directory'.
#zstyle ':zim:environment' termtitle '%n@%m: %1~'
#
# history
#
# Save the history in a custom file path.
# If none is provided, the default ${ZDOTDIR:-${HOME}}/.zhistory is used.
#HISTFILE=${ZDOTDIR:-${HOME}}/.zsh_history
#
# input
#
# Enable double-dot expansion.
# This appends '../' to your input for each '.' you type after an initial '..'
#zstyle ':zim:input' double-dot-expand yes
#
# pacman
#
# Set an optional pacman frontend.
#zstyle ':zim:pacman' frontend 'powerpill'
# Load any helper scripts as defined here.
#zstyle ':zim:pacman' helpers 'aur'
#
# ssh
#
# Load these ssh identities with the ssh module.
#zstyle ':zim:ssh' ids 'id_rsa'
#
# utility
#
# Enable spelling correction prompts.
# See http://zsh.sourceforge.net/Doc/Release/Options.html#index-CORRECT
#setopt CORRECT
# Set a custom spelling correction prompt.
#SPROMPT='zsh: correct %F{red}%R%f to %F{green}%r%f [nyae]? '
#
# zsh-autosuggestions
#
# Customize the style that the suggestions are shown with.
# See https://github.com/zsh-users/zsh-autosuggestions/blob/master/README.md#suggestion-highlight-style
#ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=10'
#
# zsh-syntax-highlighting
#
# Set what highlighters will be used.
# See https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)
# Customize the main highlighter styles.
# See https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/main.md#how-to-tweak-it
#typeset -A ZSH_HIGHLIGHT_STYLES
#ZSH_HIGHLIGHT_STYLES[comment]='fg=10'

View file

@ -1,7 +0,0 @@
#
# User configuration sourced by login shells
#
# Initialize Zim
[[ -s ${ZIM_HOME}/login_init.zsh ]] && source ${ZIM_HOME}/login_init.zsh

View file

@ -1,10 +0,0 @@
#
# User configuration sourced by interactive shells
#
# Define Zim location
ZIM_HOME=${ZDOTDIR:-${HOME}}/.zim
# Start Zim
[[ -s ${ZIM_HOME}/init.zsh ]] && source ${ZIM_HOME}/init.zsh

View file

@ -1,11 +0,0 @@
setopt LOCAL_OPTIONS PIPE_FAIL
local find_opt rm_opt
if [[ ${1} != -q ]]; then
find_opt='-print'
rm_opt='-v'
fi
command find ${ZIM_HOME} \( -name '*.zwc' -o -name '*.zwc.old' \) -delete ${find_opt} || return 1
command rm -f ${rm_opt} ${ZDOTDIR:-${HOME}}/.zshrc.zwc{,.old} || return 1
if [[ ${1} != -q ]]; then
print -P "%F{green}✓%f Done with ${0:t:r}. Run %Bzimfw compile%b to re-compile."
fi

View file

@ -1,8 +0,0 @@
setopt LOCAL_OPTIONS PIPE_FAIL
local zdumpfile zout zopt
zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile="${ZDOTDIR:-${HOME}}/.zcompdump"
[[ ${1} != -q ]] && zopt='-v'
command rm -f ${zopt} ${zdumpfile}{,.zwc{,.old}} || return 1
if [[ ${1} != -q ]]; then
print -P "%F{green}✓%f Done with ${0:t:r}. Restart your terminal to dump an updated configuration."
fi

View file

@ -1,19 +0,0 @@
local zdir zmodule
local -a zmodules
local -A zoptions
# Source .zimrc to refresh zmodules
[[ -f ${ZDOTDIR:-${HOME}}/.zimrc ]] && source ${ZDOTDIR:-${HOME}}/.zimrc
zstyle -a ':zim' modules 'zmodules'
for zdir in ${ZIM_HOME}/modules/*(/N); do
zmodule=${zdir:t}
# If zmodules does not contain the zmodule
if (( ! ${zmodules[(I)${zmodule}]} )); then
zstyle -a ':zim:module' ${zmodule} 'zoptions'
[[ ${zoptions[frozen]} == yes ]] && continue
command rm -rf ${zdir} || return 1
[[ ${1} != -q ]] && print ${zdir}
fi
done
if [[ ${1} != -q ]]; then
print -P "%F{green}✓%f Done with ${0:t:r}."
fi

View file

@ -1,3 +0,0 @@
print "Zim commit ref: $(builtin cd ${ZIM_HOME} && command git rev-parse --short HEAD)"
print "Zsh version: ${ZSH_VERSION}"
print "System info: $(command uname -a)"

View file

@ -1,25 +0,0 @@
local zmodule zurl ztype zrev
local -a zmodules
local -A zoptions
zstyle -a ':zim' modules 'zmodules'
for zmodule in ${zmodules}; do
zstyle -a ':zim:module' ${zmodule} 'zoptions'
[[ ${zoptions[frozen]} == yes ]] && continue
zurl=${zoptions[url]:-${zmodule}}
if [[ ${zurl} != /* && ${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
if [[ -n ${zoptions[tag]} ]]; then
ztype=tag
zrev=${zoptions[tag]}
else
ztype=branch
zrev=${zoptions[branch]:-master}
fi
# Cannot have an empty space at the EOL because this is read by xargs -L1
print "${zurl} ${ZIM_HOME}/modules/${zmodule} ${ztype} ${zrev}${1:+ ${1}}"
done

View file

@ -1,13 +0,0 @@
print 'usage: zimfw <action> [-q]
actions:
clean Clean all (see below).
clean-modules Clean unused modules.
clean-compiled Clean Zsh compiled files.
clean-dumpfile Clean completion dump file.
compile Compile Zsh files.
info Print Zim and system info.
install Install new modules.
update Update current modules.
upgrade Upgrade Zim.
options:
-q Quiet, only outputs errors.'

350
zimfw.zsh Normal file
View file

@ -0,0 +1,350 @@
# AUTOMATICALLY GENERATED FILE. EDIT ONLY THE SOURCE FILES AND THEN COMPILE.
# DO NOT DIRECTLY EDIT THIS FILE!
if (( ! # )); then
# Stage 1 of sourcing this script
autoload -Uz is-at-least && if ! is-at-least 5.2; then
print -u2 "init: error starting Zim: You're using Zsh version ${ZSH_VERSION} and versions < 5.2 are not supported. Update your Zsh."
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 -u2 "init: module ${zmodule} not installed"
else
for zfile in ${zdir}/{init.zsh,${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"
if [[ -f ${zdumpfile} ]]; then
zrecompile -p ${1} ${zdumpfile} || return 1
fi
# Compile .zshrc
zrecompile -p ${1} ${ZDOTDIR:-${HOME}}/.zshrc || return 1
# Compile enabled modules' autoloaded functions
for zdir in ${ZIM_HOME}/modules/${^zmodules}/functions(/FN); do
zrecompile -p ${1} ${zdir}.zwc ${zdir}/^(_*|*.*|prompt_*_setup)(-.N) || return 1
done
# Compile enabled modules' scripts
for zfile in ${ZIM_HOME}/modules/${^zmodules}/(^*test*/)#*.zsh{,-theme}(.NLk+1); do
zrecompile -p ${1} ${zfile} || return 1
done
# Compile this script
zrecompile -p ${1} ${ZIM_HOME}/zimfw.zsh || return 1
if [[ ${1} != -q ]]; then
print -P '%F{green}✓%f Done with compile.'
fi
}
zimfw() {
case ${1} in
compile|login-init) _zimfw_compile ${2} ;;
*)
source ${ZIM_HOME}/zimfw.zsh 2
zimfw "${@}"
;;
esac
}
# Stage 1 done
elif [[ ${1} == 2 ]]; then
# Stage 2 of sourcing this script. Should only be done internally by zimfw.
_zimfw_modules() {
local zmodule zurl ztype zrev
local -a zmodules
local -A zoptions
zstyle -a ':zim' modules 'zmodules'
for zmodule in ${zmodules}; do
zstyle -a ':zim:module' ${zmodule} 'zoptions'
[[ ${zoptions[frozen]} == yes ]] && continue
zurl=${zoptions[url]:-${zmodule}}
if [[ ${zurl} != /* && ${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
if [[ -n ${zoptions[tag]} ]]; then
ztype=tag
zrev=${zoptions[tag]}
else
ztype=branch
zrev=${zoptions[branch]:-master}
fi
# Cannot have an empty space at the EOL because this is read by xargs -L1
print "'${ZIM_HOME}/modules/${zmodule}' '${zurl}' '${ztype}' '${zrev}'${1:+ ${1}}"
done
}
_zimfw_clean_modules() {
local zdir zmodule
local -a zmodules
local -A zoptions
# Source .zimrc to refresh zmodules
[[ -f ${ZDOTDIR:-${HOME}}/.zimrc ]] && source ${ZDOTDIR:-${HOME}}/.zimrc
zstyle -a ':zim' modules 'zmodules'
for zdir in ${ZIM_HOME}/modules/*(/N); do
zmodule=${zdir:t}
# If zmodules does not contain the zmodule
if (( ! ${zmodules[(I)${zmodule}]} )); then
zstyle -a ':zim:module' ${zmodule} 'zoptions'
[[ ${zoptions[frozen]} == yes ]] && continue
command rm -rf ${zdir} || return 1
[[ ${1} != -q ]] && print ${zdir}
fi
done
if [[ ${1} != -q ]]; then
print -P "%F{green}✓%f Done with clean-modules."
fi
}
_zimfw_clean_compiled() {
setopt LOCAL_OPTIONS PIPE_FAIL
local find_opt rm_opt
if [[ ${1} != -q ]]; then
find_opt='-print'
rm_opt='-v'
fi
command find ${ZIM_HOME} \( -name '*.zwc' -o -name '*.zwc.old' \) -delete ${find_opt} || return 1
command rm -f ${rm_opt} ${ZDOTDIR:-${HOME}}/.zshrc.zwc{,.old} || return 1
if [[ ${1} != -q ]]; then
print -P "%F{green}✓%f Done with clean-compiled. Run %Bzimfw compile%b to re-compile."
fi
}
_zimfw_clean_dumpfile() {
setopt LOCAL_OPTIONS PIPE_FAIL
local zdumpfile zout zopt
zstyle -s ':zim:completion' dumpfile 'zdumpfile' || zdumpfile="${ZDOTDIR:-${HOME}}/.zcompdump"
[[ ${1} != -q ]] && zopt='-v'
command rm -f ${zopt} ${zdumpfile}{,.zwc{,.old}} || return 1
if [[ ${1} != -q ]]; then
print -P "%F{green}✓%f Done with clean-dumpfile. Restart your terminal to dump an updated configuration."
fi
}
_zimfw_info() {
print 'Zim version: 1.0.0-SNAPSHOT (previous commit is a1291c5)'
print "Zsh version: ${ZSH_VERSION}"
print "System info: $(command uname -a)"
}
_zimfw_upgrade() {
local zscript=${ZIM_HOME}/zimfw.zsh
local zurl=https://raw.githubusercontent.com/zimfw/zimfw/develop/zimfw.zsh
if (( ${+commands[wget]} )); then
command wget -nv ${1} -O ${zscript}.new ${zurl} || return 1
else
command curl -fsSL -o ${zscript}.new ${zurl} || return 1
fi
if command cmp -s ${zscript}{,.new}; then
command rm ${zscript}.new && \
if [[ ${1} != -q ]]; then
print -P "%F{green}✓%f zimfw.zsh: Already up to date."
fi
else
command mv ${zscript}{,.old} && command mv ${zscript}{.new,} && \
if [[ ${1} != -q ]]; then
print -P "%F{green}✓%f zimfw.zsh: upgraded. Restart your terminal for changes to take effect."
fi
fi
}
unfunction zimfw
zimfw() {
local zusage="usage: ${0} <action> [-q]
actions:
clean Clean all (see below).
clean-modules Clean unused modules.
clean-compiled Clean Zsh compiled files.
clean-dumpfile Clean completion dump file.
compile Compile Zsh files.
info Print Zim and system info.
install Install new modules.
update Update current modules.
upgrade Upgrade Zim.
options:
-q Quiet, only outputs errors."
if [[ ${#} -ne 1 && ${2} != -q ]]; then
print -u2 ${zusage}
return 1
fi
local ztool
case ${1} in
install)
ztool="# This runs in a new shell
DIR=\${1}
URL=\${2}
REV=\${4}
OPT=\${5}
MODULE=\${DIR:t}
CLEAR_LINE=\"\033[2K\r\"
if [[ -e \${DIR} ]]; then
# Already exists
return 0
fi
[[ \${OPT} != -q ]] && print -n \"\${CLEAR_LINE}Installing \${MODULE}\"
if ERR=\$(command git clone -b \${REV} -q --recursive \${URL} \${DIR} 2>&1); then
if [[ \${OPT} != -q ]]; then
print -P \"\${CLEAR_LINE}%F{green}✓%f \${MODULE}: Installed\"
fi
else
print -P \"\${CLEAR_LINE}%F{red}\${MODULE}: Error%f\n\${ERR}\"
return 1
fi
"
;;
update)
ztool="# This runs in a new shell
DIR=\${1}
URL=\${2}
TYPE=\${3}
REV=\${4}
OPT=\${5}
MODULE=\${DIR:t}
CLEAR_LINE=\"\033[2K\r\"
[[ \${OPT} != -q ]] && print -n \"\${CLEAR_LINE}Updating \${MODULE}\"
if ! cd \${DIR} 2>/dev/null; then
print -P \"\${CLEAR_LINE}%F{red}\${MODULE}: Not installed%f\"
return 1
fi
if [[ \${PWD} != \$(command git rev-parse --show-toplevel 2>/dev/null) ]]; then
# Not in repo root. Will not try to update.
return 0
fi
if [[ \${URL} != \$(command git config --get remote.origin.url) ]]; then
print -P \"\${CLEAR_LINE}%F{red}\${MODULE}: URL does not match. Expected \${URL}. Will not try to update.%f\"
return 1
fi
if [[ \${TYPE} == tag ]]; then
if [[ \${REV} == \$(command git describe --tags --exact-match 2>/dev/null) ]]; then
[[ \${OPT} != -q ]] && print -P \"\${CLEAR_LINE}%F{green}✓%f \${MODULE}: Already up to date\"
return 0
fi
fi
if ! ERR=\$(command git fetch -pq origin \${REV} 2>&1); then
print -P \"\${CLEAR_LINE}%F{red}\${MODULE}: Error (1)%f\n\${ERR}\"
return 1
fi
if [[ \${TYPE} == branch ]]; then
LOG_REV=\"\${REV}@{u}\"
else
LOG_REV=\${REV}
fi
LOG=\$(command git log --graph --color --format='%C(yellow)%h%C(reset) %s %C(cyan)(%cr)%C(reset)' ..\${LOG_REV} 2>/dev/null)
if ! ERR=\$(command git checkout -q \${REV} -- 2>&1); then
print -P \"\${CLEAR_LINE}%F{red}\${MODULE}: Error (2)%f\n\${ERR}\"
return 1
fi
if [[ \${TYPE} == branch ]]; then
if ! OUT=\$(command git merge --ff-only --no-progress -n 2>&1); then
print -P \"\${CLEAR_LINE}%F{red}\${MODULE}: Error (3)%f\n\${OUT}\"
return 1
fi
# keep just first line of OUT
OUT=\${OUT%%($'\n'|$'\r')*}
else
OUT=\"Updating to \${TYPE} \${REV}\"
fi
[[ -n \${LOG} ]] && OUT=\"\${OUT}\n\${LOG}\"
if ERR=\$(command git submodule update --init --recursive -q 2>&1); then
if [[ \${OPT} != -q ]]; then
print -P \"\${CLEAR_LINE}%F{green}✓%f \${MODULE}: \${OUT}\"
fi
else
print -P \"\${CLEAR_LINE}%F{red}\${MODULE}: Error (4)%f\n\${ERR}\"
return 1
fi
"
;;
esac
case ${1} in
clean)
_zimfw_clean_modules ${2} && \
_zimfw_clean_compiled ${2} && \
_zimfw_clean_dumpfile ${2}
;;
clean-modules) _zimfw_clean_modules ${2} ;;
clean-compiled) _zimfw_clean_compiled ${2} ;;
clean-dumpfile) _zimfw_clean_dumpfile ${2} ;;
compile|login-init) _zimfw_compile ${2} ;;
info) _zimfw_info ${2} ;;
install|update)
# Source .zimrc to refresh zmodules
[[ -f ${ZDOTDIR:-${HOME}}/.zimrc ]] && source ${ZDOTDIR:-${HOME}}/.zimrc
_zimfw_modules ${2} | xargs -L1 -P10 zsh -c ${ztool} ${1} && \
if [[ ${2} != -q ]]; then
print -P "%F{green}✓%f Done with ${1}. Restart your terminal for any changes to take effect."
fi
;;
upgrade) _zimfw_upgrade ${2} ;;
*)
print -u2 ${zusage}
return 1
;;
esac
}
# Stage 2 done
fi