From b34e0502d32b6cfaa147287ddec4d718ffdb0365 Mon Sep 17 00:00:00 2001 From: Sorin Ionescu Date: Wed, 28 Mar 2012 18:50:36 -0400 Subject: [PATCH] [#23] Add a module loading function --- helper.zsh | 29 +++++++++++++++++ init.zsh | 62 ++++++++++--------------------------- modules/completion/init.zsh | 3 ++ 3 files changed, 48 insertions(+), 46 deletions(-) diff --git a/helper.zsh b/helper.zsh index 8478d78c..db246e29 100644 --- a/helper.zsh +++ b/helper.zsh @@ -10,6 +10,35 @@ function autoloadable { ( unfunction $1 ; autoload -U +X $1 ) &> /dev/null } +# Loads Oh My Zsh modules. +function omodload { + local omodule + local OMZ="${funcsourcetrace[1]:h}" + + for omodule in "$argv[@]"; do + if [[ ! -d "$OMZ/modules/$omodule" ]]; then + print "omz: no such module: $omodule" >&2 + continue + fi + + # Add functions and completetions to fpath. + fpath=($OMZ/modules/$omodule/{functions,completions}(/FN) $fpath) + + # Autoload module functions. + for func in $OMZ/modules/$omodule/functions/[^_.]*(N.:t); do + autoload -Uz $func + done + + if [[ -f "$OMZ/modules/$omodule/init.zsh" ]]; then + source "$OMZ/modules/$omodule/init.zsh" + fi + + if (( $? == 0 )); then + zstyle ":omz:module:$omodule" enable 'yes' + fi + done +} + # 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]|)) ]] diff --git a/init.zsh b/init.zsh index b2e083ba..8021a1eb 100644 --- a/init.zsh +++ b/init.zsh @@ -19,61 +19,23 @@ if [[ "$TERM" == 'dumb' ]]; then zstyle ':omz:prompt' theme 'off' fi -# Get enabled modules. -zstyle -a ':omz:load' module 'omodules' - -# Add functions to fpath. -fpath=( - ${0:h}/themes/*(/FN) - ${omodules:+${0:h}/modules/${^omodules}/{functions,completions}(/FN)} - $fpath -) - -# Load and initialize the completion system ignoring insecure directories. -autoload -Uz compinit && compinit -i - -# Source files (the order matters). -source "${0:h}/helper.zsh" - # Autoload Zsh functions. autoload -Uz age autoload -Uz zargs autoload -Uz zcalc autoload -Uz zmv -# 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 - - if [[ -f "${0:h}/modules/$omodule/init.zsh" ]]; then - source "${0:h}/modules/$omodule/init.zsh" - fi +# Source files (the order matters). +source "${0:h}/helper.zsh" - if (( $? == 0 )); then - zstyle ":omz:module:$omodule" enable 'yes' - fi -done -unset omodule omodules +# Get enabled modules. +zstyle -a ':omz:module' enable 'omodules' -# Autoload Oh My Zsh functions. -for fdir in "$fpath[@]"; do - if [[ "$fdir" == ${0:h}/(|*/)functions ]]; then - for func in $fdir/[^_.]*(N.:t); do - autoload -Uz $func - done - fi -done -unset fdir func +# Source modules defined in ~/.zshrc. +omodload "$omodules[@]" -# 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 @@ -87,6 +49,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 diff --git a/modules/completion/init.zsh b/modules/completion/init.zsh index dc75c6fb..dd8cdc73 100644 --- a/modules/completion/init.zsh +++ b/modules/completion/init.zsh @@ -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.