diff --git a/init.fish b/init.fish index b7314b1..c0768d7 100644 --- a/init.fish +++ b/init.fish @@ -41,16 +41,20 @@ source $OMF_CONFIG/before.init.fish ^/dev/null set -l user_function_path $fish_function_path[1] set fish_function_path[1] $OMF_PATH/lib -set -l theme {$OMF_PATH,$OMF_CONFIG}/themes/(cat $OMF_CONFIG/theme) +# Autoload util functions +autoload $OMF_PATH/lib $OMF_PATH/lib/git -for path in $OMF_PATH/lib $OMF_PATH/lib/git {$OMF_PATH,$OMF_CONFIG}/pkg/* $theme - contains -- (basename $path) $OMF_IGNORE; and continue +for path in {$OMF_PATH,$OMF_CONFIG}/pkg/* + set -l name (basename $path) - autoload $path $path/completions - source $path/(basename $path).fish ^/dev/null - and emit init_(basename $path) $path + contains -- $name $OMF_IGNORE; and continue + require $name end +# Autoload theme +autoload {$OMF_PATH,$OMF_CONFIG}/themes/(cat $OMF_CONFIG/theme) + +# Autoload custom functions autoload $OMF_CONFIG/functions autoload $user_function_path diff --git a/lib/autoload.fish b/lib/autoload.fish index 43b4ddd..fd41519 100644 --- a/lib/autoload.fish +++ b/lib/autoload.fish @@ -1,15 +1,20 @@ # SYNOPSIS -# autoload +# autoload ... # # OVERVIEW # Autoload a function or completion path. Add the specified list of # directories to $fish_function_path. Any `completions` directories # are correctly added to the $fish_complete_path. +# +# Returns 0 if one of the paths exist. +# Returns != 0 if all paths are missing. function autoload -d "autoload a function or completion path" for path in $argv + set -l dest fish_function_path + if test -d "$path" - set -l dest fish_function_path + set path_exist if test (basename "$path") = completions set dest fish_complete_path @@ -18,4 +23,6 @@ function autoload -d "autoload a function or completion path" contains "$path" $$dest; or set $dest "$path" $$dest end end + + set -q path_exist end diff --git a/lib/available.fish b/lib/available.fish index 4d44c6c..b6cfa14 100644 --- a/lib/available.fish +++ b/lib/available.fish @@ -2,8 +2,8 @@ # available [name] # # OVERVIEW -# Check if a program is available. +# Check if a function or program is available. -function available -a program -d "check if a program is available." - type "$program" ^/dev/null >&2 +function available -a name -d "Check if a function or program is available." + type "$name" ^/dev/null >&2 end diff --git a/lib/require.fish b/lib/require.fish new file mode 100644 index 0000000..7069559 --- /dev/null +++ b/lib/require.fish @@ -0,0 +1,26 @@ +# SYNOPSIS +# require [name] +# +# OVERVIEW +# Require a plugin: +# - Autoload its functions and completions. +# - Source its initialization file. +# - Emit its initialization event. +# +# If the required plugin has already been loaded, does nothing. + +function require -a name + # Skip if plugin has already been loaded. + contains -- $OMF_PATH/pkg/$name $fish_function_path; + or contains -- $OMF_CONFIG/pkg/$name $fish_function_path; + and return 0 + + for path in {$OMF_PATH,$OMF_CONFIG}/pkg/$name + if autoload $path $path/completions + + source $path/init.fish ^/dev/null; + or source $path/$name.fish ^/dev/null; + and emit init_$name $path + end + end +end diff --git a/pkg/omf/init.fish b/pkg/omf/init.fish new file mode 100644 index 0000000..8d7d70e --- /dev/null +++ b/pkg/omf/init.fish @@ -0,0 +1,3 @@ +function init -a path --on-event init_omf + autoload $path/cli $path/util +end diff --git a/pkg/omf/omf.fish b/pkg/omf/omf.fish index 1b0f57d..adfd502 100644 --- a/pkg/omf/omf.fish +++ b/pkg/omf/omf.fish @@ -29,10 +29,6 @@ function omf::off set_color normal end -function init -a path --on-event init_omf - autoload $path/cli $path/util -end - function omf -d "Oh My Fish" if test (count $argv) -eq 0 omf.help "main"; and return 0