From 060d9064f8be6f420837d9846a785cd8cb58b032 Mon Sep 17 00:00:00 2001 From: Sorin Ionescu Date: Mon, 23 Jul 2012 15:00:44 -0400 Subject: [PATCH] [Fix #202] Load modules all or nothing --- helper.zsh | 28 +++++++++++++++++++------ modules/command-not-found/init.zsh | 7 +++++-- modules/completion/init.zsh | 2 +- modules/dpkg/init.zsh | 5 +++++ modules/editor/init.zsh | 2 +- modules/git/init.zsh | 5 +++++ modules/gnu-utility/init.zsh | 2 +- modules/gpg-agent/init.zsh | 1 + modules/haskell/init.zsh | 5 +++++ modules/macports/init.zsh | 2 +- modules/node/init.zsh | 20 +++++++++++------- modules/osx/init.zsh | 2 +- modules/pacman/init.zsh | 5 +++++ modules/perl/init.zsh | 5 +++++ modules/python/init.zsh | 5 +++++ modules/rails/init.zsh | 5 +++++ modules/rsync/init.zsh | 5 +++++ modules/ruby/init.zsh | 31 +++++++++++++++++----------- modules/screen/init.zsh | 5 +++++ modules/spectrum/init.zsh | 5 +++++ modules/ssh-agent/init.zsh | 1 + modules/syntax-highlighting/init.zsh | 17 ++++++++------- modules/terminal/init.zsh | 2 +- modules/tmux/init.zsh | 5 +++++ modules/yum/init.zsh | 5 +++++ modules/z/init.zsh | 21 +++++++++++-------- 26 files changed, 148 insertions(+), 50 deletions(-) diff --git a/helper.zsh b/helper.zsh index 7c960b2..ef15259 100644 --- a/helper.zsh +++ b/helper.zsh @@ -34,27 +34,27 @@ function autoloadable { function omodload { local -a omodules local omodule + local ofunction_glob='^([_.]*|prompt_*_setup|README*)(.N:t)' # $argv is overridden in the anonymous function. omodules=("$argv[@]") + # Add functions to $fpath. + fpath=(${omodules:+${OMZ}/modules/${^omodules}/functions(/FN)} $fpath) + function { local ofunction # Extended globbing is needed for listing autoloadable function directories. setopt LOCAL_OPTIONS EXTENDED_GLOB - # Add functions to fpath. - fpath=(${omodules:+${OMZ}/modules/${^omodules}/functions(/FN)} $fpath) - # Load Oh My Zsh functions. - for ofunction in \ - $OMZ/modules/${^omodules}/functions/^([_.]*|prompt_*_setup|README*)(.N:t) - do + for ofunction in $OMZ/modules/${^omodules}/functions/$~ofunction_glob; do autoload -Uz "$ofunction" done } + # Load Oh My Zsh modules. for omodule in "$omodules[@]"; do if zstyle -t ":omz:module:$omodule" loaded; then continue @@ -69,6 +69,22 @@ function omodload { if (( $? == 0 )); then zstyle ":omz:module:$omodule" loaded 'yes' else + # Remove the $fpath entry. + fpath[(r)$OMZ/modules/${omodule}/functions]=() + + function { + local ofunction + + # Extended globbing is needed for listing autoloadable function + # directories. + setopt LOCAL_OPTIONS EXTENDED_GLOB + + # Unload Oh My Zsh functions. + for ofunction in $OMZ/modules/$omodule/functions/$~ofunction_glob; do + unfunction "$ofunction" + done + } + zstyle ":omz:module:$omodule" loaded 'no' fi fi diff --git a/modules/command-not-found/init.zsh b/modules/command-not-found/init.zsh index 38f3626..7b554e8 100644 --- a/modules/command-not-found/init.zsh +++ b/modules/command-not-found/init.zsh @@ -5,7 +5,10 @@ # Joseph Jon Booker # -if [[ -s '/etc/zsh_command_not_found' ]]; then - source '/etc/zsh_command_not_found' +# Return if requirements are not found. +if [[ ! -s '/etc/zsh_command_not_found' ]]; then + return 1 fi +source '/etc/zsh_command_not_found' + diff --git a/modules/completion/init.zsh b/modules/completion/init.zsh index 888d886..da8b178 100644 --- a/modules/completion/init.zsh +++ b/modules/completion/init.zsh @@ -6,7 +6,7 @@ # Sorin Ionescu # -# Dumb terminals lack support. +# Return if requirements are not found. if [[ "$TERM" == 'dumb' ]]; then return 1 fi diff --git a/modules/dpkg/init.zsh b/modules/dpkg/init.zsh index eddc51a..edab683 100644 --- a/modules/dpkg/init.zsh +++ b/modules/dpkg/init.zsh @@ -7,6 +7,11 @@ # Sorin Ionescu # +# Return if requirements are not found. +if (( ! $+commands[dpkg] && ! $+commands[apt-get] )); then + return 1 +fi + # Aliases # Cleans the cache. diff --git a/modules/editor/init.zsh b/modules/editor/init.zsh index 0fbd571..b34dc41 100644 --- a/modules/editor/init.zsh +++ b/modules/editor/init.zsh @@ -40,7 +40,7 @@ # zstyle ':omz:module:editor' completing '...' # -# Dumb terminals lack support. +# Return if requirements are not found. if [[ "$TERM" == 'dumb' ]]; then return 1 fi diff --git a/modules/git/init.zsh b/modules/git/init.zsh index e3c3ac8..577ae25 100644 --- a/modules/git/init.zsh +++ b/modules/git/init.zsh @@ -5,6 +5,11 @@ # Sorin Ionescu # +# Return if requirements are not found. +if (( ! $+commands[git] )); then + return 1 +fi + # Source module files. source "${0:h}/alias.zsh" source "${0:h}/hub.zsh" diff --git a/modules/gnu-utility/init.zsh b/modules/gnu-utility/init.zsh index 6547d0f..d62e234 100644 --- a/modules/gnu-utility/init.zsh +++ b/modules/gnu-utility/init.zsh @@ -8,7 +8,7 @@ # Get the prefix or use the default. zstyle -s ':omz:module:gnu-utility' prefix '_gnu_utility_p' || _gnu_utility_p='g' -# Check for the presence of GNU Core Utilities. +# Return if requirements are not found. if (( ! ${+commands[${_gnu_utility_p}whoami]} )); then return 1 fi diff --git a/modules/gpg-agent/init.zsh b/modules/gpg-agent/init.zsh index 35c5eda..902466d 100644 --- a/modules/gpg-agent/init.zsh +++ b/modules/gpg-agent/init.zsh @@ -6,6 +6,7 @@ # Sorin Ionescu # +# Return if requirements are not found. if (( ! $+commands[gpg-agent] )); then return 1 fi diff --git a/modules/haskell/init.zsh b/modules/haskell/init.zsh index 9cd85a0..926f7dd 100644 --- a/modules/haskell/init.zsh +++ b/modules/haskell/init.zsh @@ -5,6 +5,11 @@ # Sebastian Wiesner # +# Return if requirements are not found. +if (( ! $+commands[ghc] )); then + return 1 +fi + # Prepend Cabal per user directories to PATH/MANPATH. if [[ "$OSTYPE" == darwin* ]]; then path=($HOME/Library/Haskell/bin(/N) $path) diff --git a/modules/macports/init.zsh b/modules/macports/init.zsh index eca5762..49b7d37 100644 --- a/modules/macports/init.zsh +++ b/modules/macports/init.zsh @@ -6,7 +6,7 @@ # Sorin Ionescu # -# This module is for Mac OS X only. +# Return if requirements are not found. if [[ "$OSTYPE" != darwin* ]]; then return 1 fi diff --git a/modules/node/init.zsh b/modules/node/init.zsh index dc97f40..16847ab 100644 --- a/modules/node/init.zsh +++ b/modules/node/init.zsh @@ -5,18 +5,22 @@ # Sorin Ionescu # -if (( ! $+commands[npm] )); then +# Return if requirements are not found. +if (( ! $+commands[node] )); then return 1 fi -cache_file="${0:h}/cache.zsh" +# Load NPM completion. +if (( $+commands[npm] )); then + cache_file="${0:h}/cache.zsh" -if [[ "$commands[npm]" -nt "$cache_file" || ! -s "$cache_file" ]]; then - # npm is slow; cache its output. - npm completion >! "$cache_file" 2> /dev/null -fi + if [[ "$commands[npm]" -nt "$cache_file" || ! -s "$cache_file" ]]; then + # npm is slow; cache its output. + npm completion >! "$cache_file" 2> /dev/null + fi -source "$cache_file" + source "$cache_file" -unset cache_file + unset cache_file +fi diff --git a/modules/osx/init.zsh b/modules/osx/init.zsh index 953cba7..87ca5d6 100644 --- a/modules/osx/init.zsh +++ b/modules/osx/init.zsh @@ -5,7 +5,7 @@ # Sorin Ionescu # -# This module is for Mac OS X only. +# Return if requirements are not found. if [[ "$OSTYPE" != darwin* ]]; then return 1 fi diff --git a/modules/pacman/init.zsh b/modules/pacman/init.zsh index b906d93..f4145d8 100644 --- a/modules/pacman/init.zsh +++ b/modules/pacman/init.zsh @@ -9,6 +9,11 @@ # https://wiki.archlinux.org/index.php/Pacman_Tips # +# Return if requirements are not found. +if (( ! $+commands[pacman] )); then + return 1 +fi + # Get the Pacman frontend. zstyle -s ':omz:module:pacman' frontend '_pacman_frontend' diff --git a/modules/perl/init.zsh b/modules/perl/init.zsh index 39c9bed..4278135 100644 --- a/modules/perl/init.zsh +++ b/modules/perl/init.zsh @@ -5,6 +5,11 @@ # Sorin Ionescu # +# Return if requirements are not found. +if (( ! $+commands[perl] )); then + return 1 +fi + if [[ "$OSTYPE" == darwin* ]]; then # Perl is slow; cache its output. cache_file="${0:h}/cache.zsh" diff --git a/modules/python/init.zsh b/modules/python/init.zsh index e9a74d4..485f99d 100644 --- a/modules/python/init.zsh +++ b/modules/python/init.zsh @@ -6,6 +6,11 @@ # Sebastian Wiesner # +# Return if requirements are not found. +if (( ! $+commands[python] )); then + return 1 +fi + # Prepend PEP 370 per user site packages directory, which defaults to # ~/Library/Python on Mac OS X and ~/.local elsewhere, to PATH/MANPATH. if [[ "$OSTYPE" == darwin* ]]; then diff --git a/modules/rails/init.zsh b/modules/rails/init.zsh index d17a13b..81bbbee 100644 --- a/modules/rails/init.zsh +++ b/modules/rails/init.zsh @@ -7,6 +7,11 @@ # Sorin Ionescu # +# Return if requirements are not found. +if (( ! $+commands[rails] )); then + return 1 +fi + # Aliases (Compatible with Rails 2) alias rc='_rails-command console' alias rdc='_rails-command dbconsole' diff --git a/modules/rsync/init.zsh b/modules/rsync/init.zsh index e0f866a..7667472 100644 --- a/modules/rsync/init.zsh +++ b/modules/rsync/init.zsh @@ -5,6 +5,11 @@ # Sorin Ionescu # +# Return if requirements are not found. +if (( ! $+commands[rsync] )); then + return 1 +fi + # Aliases _rsync_cmd='rsync --verbose --progress --human-readable --compress --archive --hard-links --one-file-system' diff --git a/modules/ruby/init.zsh b/modules/ruby/init.zsh index 9b40ed9..0e7f226 100644 --- a/modules/ruby/init.zsh +++ b/modules/ruby/init.zsh @@ -5,6 +5,11 @@ # Authors: Sorin Ionescu # +# Return if requirements are not found. +if (( ! $+commands[ruby] )); then + return 1 +fi + # Load RVM into the shell session. if [[ -s "$HOME/.rvm/scripts/rvm" ]]; then # Unset AUTO_NAME_DIRS since auto adding variable-stored paths to ~ list @@ -34,16 +39,18 @@ fi # Aliases # Bundler -alias b='bundle' -alias be='b exec' -alias bi='b install --path vendor/bundle' -alias bl='b list' -alias bo='b open' -alias bp='b package' -alias bu='b update' -alias bI='bi \ - && b package \ - && print .bundle >>! .gitignore \ - && print vendor/bundle >>! .gitignore \ - && print vendor/cache >>! .gitignore' +if (( $+commands[bundle] )); then + alias b='bundle' + alias be='b exec' + alias bi='b install --path vendor/bundle' + alias bl='b list' + alias bo='b open' + alias bp='b package' + alias bu='b update' + alias bI='bi \ + && b package \ + && print .bundle >>! .gitignore \ + && print vendor/bundle >>! .gitignore \ + && print vendor/cache >>! .gitignore' +fi diff --git a/modules/screen/init.zsh b/modules/screen/init.zsh index 6ee05be..82f3fb2 100644 --- a/modules/screen/init.zsh +++ b/modules/screen/init.zsh @@ -5,6 +5,11 @@ # Sorin Ionescu # +# Return if requirements are not found. +if (( ! $+commands[screen] )); then + return 1 +fi + # Auto Start if [[ -z "$STY" ]] && zstyle -t ':omz:module:screen' auto-start; then session="$( diff --git a/modules/spectrum/init.zsh b/modules/spectrum/init.zsh index 66cd218..3d05138 100644 --- a/modules/spectrum/init.zsh +++ b/modules/spectrum/init.zsh @@ -6,6 +6,11 @@ # Sorin Ionescu # +# Return if requirements are not found. +if [[ "$TERM" == 'dumb' ]]; then + return 1 +fi + typeset -gA FX FG BG FX=( diff --git a/modules/ssh-agent/init.zsh b/modules/ssh-agent/init.zsh index b070f06..0d83e48 100644 --- a/modules/ssh-agent/init.zsh +++ b/modules/ssh-agent/init.zsh @@ -11,6 +11,7 @@ # Sorin Ionescu # +# Return if requirements are not found. if (( ! $+commands[ssh-agent] )); then return 1 fi diff --git a/modules/syntax-highlighting/init.zsh b/modules/syntax-highlighting/init.zsh index 88ec7b4..d57920c 100644 --- a/modules/syntax-highlighting/init.zsh +++ b/modules/syntax-highlighting/init.zsh @@ -5,13 +5,16 @@ # Sorin Ionescu # -if zstyle -t ':omz:module:syntax-highlighting' color; then - source "${0:h}/external/zsh-syntax-highlighting.zsh" +# Return if requirements are not found. +if ! zstyle -t ':omz:module:syntax-highlighting' color; then + return 1 +fi + +source "${0:h}/external/zsh-syntax-highlighting.zsh" - # Set the highlighters. - zstyle -a ':omz:module:syntax-highlighting' highlighters 'ZSH_HIGHLIGHT_HIGHLIGHTERS' - if (( ${#ZSH_HIGHLIGHT_HIGHLIGHTERS[@]} == 0 )); then - ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets cursor) - fi +# Set the highlighters. +zstyle -a ':omz:module:syntax-highlighting' highlighters 'ZSH_HIGHLIGHT_HIGHLIGHTERS' +if (( ${#ZSH_HIGHLIGHT_HIGHLIGHTERS[@]} == 0 )); then + ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets cursor) fi diff --git a/modules/terminal/init.zsh b/modules/terminal/init.zsh index b87f51d..326ec66 100644 --- a/modules/terminal/init.zsh +++ b/modules/terminal/init.zsh @@ -6,7 +6,7 @@ # Sorin Ionescu # -# Dumb terminals lack support. +# Return if requirements are not found. if [[ "$TERM" == 'dumb' ]]; then return 1 fi diff --git a/modules/tmux/init.zsh b/modules/tmux/init.zsh index 9be0f0d..9a6c752 100644 --- a/modules/tmux/init.zsh +++ b/modules/tmux/init.zsh @@ -6,6 +6,11 @@ # Colin Hebert # +# Return if requirements are not found. +if (( ! $+commands[tmux] )); then + return 1 +fi + # Auto Start if [[ -z "$TMUX" ]] && zstyle -t ':omz:module:tmux' auto-start; then tmux_session='#OMZ' diff --git a/modules/yum/init.zsh b/modules/yum/init.zsh index 21f8109..bbee6cc 100644 --- a/modules/yum/init.zsh +++ b/modules/yum/init.zsh @@ -6,6 +6,11 @@ # Sorin Ionescu # +# Return if requirements are not found. +if (( ! $+commands[yum] )); then + return 1 +fi + # Aliases alias yumc='sudo yum clean all' # Cleans the cache. alias yumh='yum history' # Displays history. diff --git a/modules/z/init.zsh b/modules/z/init.zsh index 4175cde..c873408 100644 --- a/modules/z/init.zsh +++ b/modules/z/init.zsh @@ -23,15 +23,18 @@ done unset _z_prefix{es,} _z_sh -if (( $+functions[_z] )); then - function _z-precmd { - _z --add "${PWD:A}" - } +# Return if requirements are not found. +if (( ! $+functions[_z] )); then + return 1 +fi - autoload -Uz add-zsh-hook - add-zsh-hook precmd _z-precmd +function _z-precmd { + _z --add "${PWD:A}" +} - alias z='nocorrect _z' - alias j='nocorrect _z' -fi +autoload -Uz add-zsh-hook +add-zsh-hook precmd _z-precmd + +alias z='nocorrect _z' +alias j='nocorrect _z'