From 6233ec6aad99937a2904afe1a8b0488c5576d85e Mon Sep 17 00:00:00 2001 From: Sorin Ionescu Date: Mon, 2 Apr 2012 18:51:00 -0400 Subject: [PATCH] [#23] Add a module loading function --- helper.zsh | 52 ++++++++++++++++++++++++---- init.zsh | 69 ++++++++++--------------------------- modules/completion/init.zsh | 3 ++ templates/zshrc | 8 +++-- 4 files changed, 74 insertions(+), 58 deletions(-) diff --git a/helper.zsh b/helper.zsh index 8478d78c..7714591f 100644 --- a/helper.zsh +++ b/helper.zsh @@ -5,12 +5,8 @@ # Sorin Ionescu # -# 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=( diff --git a/init.zsh b/init.zsh index c0ef5513..73a65976 100644 --- a/init.zsh +++ b/init.zsh @@ -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 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. diff --git a/templates/zshrc b/templates/zshrc index 01f95b63..46e1db6d 100644 --- a/templates/zshrc +++ b/templates/zshrc @@ -5,6 +5,9 @@ # Sorin Ionescu # +# 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...