[#23] Add a module loading function

This commit is contained in:
Sorin Ionescu 2012-04-02 18:51:00 -04:00
parent 86e4d2e3ff
commit 6233ec6aad
4 changed files with 74 additions and 58 deletions

View file

@ -5,12 +5,8 @@
# Sorin Ionescu <sorin.ionescu@gmail.com>
#
# Checks if a file can be autoloaded by trying to load it in a subshell.
function autoloadable {
( unfunction $1 ; autoload -U +X $1 ) &> /dev/null
}
# Checks boolean variable for "true" (case insensitive "1", "y", "yes", "t", "true", "o", and "on").
# Checks boolean variable for "true".
# Case insensitive: "1", "y", "yes", "t", "true", "o", and "on".
function is-true {
[[ -n "$1" && "$1" == (1|[Yy]([Ee][Ss]|)|[Tt]([Rr][Uu][Ee]|)|[Oo]([Nn]|)) ]]
}
@ -24,6 +20,50 @@ function coalesce {
return 1
}
# Checks if a file can be autoloaded by trying to load it in a subshell.
function autoloadable {
( unfunction $1 ; autoload -U +X $1 ) &> /dev/null
}
# Loads Oh My Zsh modules.
function omodload {
local omodule
# Extended globbing is needed for autoloading of module functions.
setopt EXTENDED_GLOB
# Add functions and completions to fpath.
fpath=(${argv:+${OMZ}/modules/${^argv}/{functions,completions}(/FN)} $fpath)
# Load Oh My Zsh functions.
for ofunction in $OMZ/modules/**/functions/^([_.]*|prompt_*_setup)(.N:t); do
autoload -Uz "$ofunction"
done
unset ofunction
# Extended globbing is no longer needed.
unsetopt EXTENDED_GLOB
for omodule in "$argv[@]"; do
if zstyle -t ":omz:module:$omodule" loaded; then
continue
elif [[ ! -d "$OMZ/modules/$omodule" ]]; then
print "$0: no such module: $omodule" >&2
continue
else
if [[ -f "$OMZ/modules/$omodule/init.zsh" ]]; then
source "$OMZ/modules/$omodule/init.zsh"
fi
if (( $? == 0 )); then
zstyle ":omz:module:$omodule" loaded 'yes'
else
zstyle ":omz:module:$omodule" loaded 'no'
fi
fi
done
}
# Trap signals were generated with 'kill -l'.
# DEBUG, EXIT, and ZERR are Zsh signals.
TRAP_SIGNALS=(

View file

@ -16,72 +16,33 @@ unset min_zsh_version
# Disable color and theme in dumb terminals.
if [[ "$TERM" == 'dumb' ]]; then
zstyle ':omz:*:*' color 'no'
zstyle ':omz:prompt' theme 'off'
zstyle ':omz:module:prompt' theme 'off'
fi
# Get enabled OMZ modules.
zstyle -a ':omz:load' omodule 'omodules'
# Add functions to fpath.
fpath=(
${0:h}/themes/*(/FN)
${omodules:+${0:h}/modules/${^omodules}/{functions,completions}(/FN)}
$fpath
)
# Load Zsh modules.
zstyle -a ':omz:load' module 'zmodules'
zstyle -a ':omz:load' zmodule 'zmodules'
for zmodule in "$zmodules[@]"; do
zmodload "${(z)zmodule}"
zmodload "zsh/${(z)zmodule}"
done
unset zmodules zmodule
unset zmodule{s,}
# Autoload Zsh functions.
zstyle -a ':omz:load' function 'zfunctions'
zstyle -a ':omz:load' zfunction 'zfunctions'
for zfunction in "$zfunctions[@]"; do
autoload -Uz "$zfunction"
done
unset zfunctions zfunction
# Load and initialize the completion system ignoring insecure directories.
autoload -Uz compinit && compinit -i
unset zfunction{s,}
# Source files (the order matters).
source "${0:h}/helper.zsh"
# Source modules defined in ~/.zshrc.
for omodule in "$omodules[@]"; do
if [[ ! -d "${0:h}/modules/$omodule" ]]; then
print "omz: no such module: $omodule" >&2
fi
zstyle -a ':omz:load' omodule 'omodules'
omodload "$omodules[@]"
unset omodules
if [[ -f "${0:h}/modules/$omodule/init.zsh" ]]; then
source "${0:h}/modules/$omodule/init.zsh"
fi
if (( $? == 0 )); then
zstyle ":omz:module:$omodule" loaded 'yes'
fi
done
unset omodule omodules
# Autoload Oh My Zsh functions.
for fdir in "$fpath[@]"; do
if [[ "$fdir" == ${0:h}/(|*/)functions ]]; then
for ofunction in $fdir/[^_.]*(N.:t); do
autoload -Uz "$ofunction"
done
fi
done
unset fdir ofunction
# Set environment variables for launchd processes.
if [[ "$OSTYPE" == darwin* ]]; then
for env_var in PATH MANPATH; do
launchctl setenv "$env_var" "${(P)env_var}" &!
done
unset env_var
fi
# Add themes to fpath.
fpath=(${0:h}/themes/*(/FN) $fpath)
# Load and run the prompt theming system.
autoload -Uz promptinit && promptinit
@ -95,6 +56,14 @@ else
fi
unset prompt_argv
# Set environment variables for launchd processes.
if [[ "$OSTYPE" == darwin* ]]; then
for env_var in PATH MANPATH; do
launchctl setenv "$env_var" "${(P)env_var}" &!
done
unset env_var
fi
# Compile the completion dump, to increase startup speed.
dump_file="$HOME/.zcompdump"
if [[ "$dump_file" -nt "${dump_file}.zwc" || ! -f "${dump_file}.zwc" ]]; then

View file

@ -11,6 +11,9 @@ if [[ "$TERM" == 'dumb' ]]; then
return 1
fi
# Load and initialize the completion system ignoring insecure directories.
autoload -Uz compinit && compinit -i
setopt COMPLETE_IN_WORD # Complete from both ends of a word.
setopt ALWAYS_TO_END # Move cursor to the end of a completed word.
setopt PATH_DIRS # Perform path search even on command names with slashes.

View file

@ -5,6 +5,9 @@
# Sorin Ionescu <sorin.ionescu@gmail.com>
#
# Set the path to Oh My Zsh.
export OMZ="$HOME/.oh-my-zsh"
# Set the key mapping style to 'emacs' or 'vi'.
zstyle ':omz:module:editor' keymap 'emacs'
@ -27,7 +30,8 @@ zstyle ':omz:module:terminal' auto-title 'yes'
# zstyle ':omz:load' zfunction 'zargs' 'zmv'
# Set the Oh My Zsh modules to load (browse modules).
zstyle ':omz:load' omodule 'archive' 'git'
zstyle ':omz:load' omodule 'environment' 'terminal' 'editor' \
'completion' 'history' 'directory' 'spectrum' 'alias' 'utility'
# Set the prompt theme to load.
# Setting it to 'random' loads a random theme.
@ -35,7 +39,7 @@ zstyle ':omz:load' omodule 'archive' 'git'
zstyle ':omz:prompt' theme 'sorin'
# This will make you shout: OH MY ZSHELL!
source "$HOME/.oh-my-zsh/init.zsh"
source "$OMZ/init.zsh"
# Customize to your needs...