From 2ccf3ad3a7fb1711035779908cc8b13b9fbd8f6c Mon Sep 17 00:00:00 2001 From: Derek Stavis <dekestavis@gmail.com> Date: Sun, 14 Feb 2016 17:41:58 -0200 Subject: [PATCH] I never write commit messages --- init.fish | 19 ++----------- lib/autoload.fish | 65 +++++++++++++++++++++++++++++++++++++++++++++ lib/available.fish | 3 +++ lib/initialize.fish | 20 ++++++++++++++ lib/reload.fish | 10 +++++++ lib/require.fish | 32 ++++++++++++++++++++++ 6 files changed, 132 insertions(+), 17 deletions(-) create mode 100644 lib/autoload.fish create mode 100644 lib/available.fish create mode 100644 lib/initialize.fish create mode 100644 lib/reload.fish create mode 100644 lib/require.fish diff --git a/init.fish b/init.fish index 8e61a00..abb5568 100644 --- a/init.fish +++ b/init.fish @@ -7,17 +7,11 @@ end # Source custom before.init.fish file source $OMF_CONFIG/before.init.fish ^/dev/null -# Autoload framework structure, keeping user function path as first -set fish_function_path $fish_function_path[1] \ - $OMF_CONFIG/functions \ - $OMF_PATH/{lib,git} \ - $fish_function_path[2..-1] - # Read current theme read -l theme < $OMF_CONFIG/theme # Prepare Oh My Fish paths -set -l omf_function_path {$OMF_CONFIG,$OMF_PATH}/{themes*/$theme/{,functions},pkg/*/{,functions}} +set -l omf_function_path {$OMF_CONFIG,$OMF_PATH}/{themes*/$theme/{,functions},pkg/*/{,functions},functions,lib,lib/git} set -l omf_complete_path {$OMF_CONFIG,$OMF_PATH}/{themes*/$theme/completions,pkg/*/completions} # Autoload functions @@ -30,16 +24,7 @@ set fish_complete_path $fish_complete_path[1] \ $omf_complete_path \ $fish_complete_path[2..-1] -for init in {$OMF_CONFIG,$OMF_PATH}/{pkg/*/init.fish} - begin - source $init >/dev/null ^&1 - - set -l IFS '/' - echo $init | read -la components - - emit init_$components[-2] (printf '/%s' $components[1..-2]) - end -end +initialize {$OMF_CONFIG,$OMF_PATH}/pkg/*/init.fish # Source custom init.fish file source $OMF_CONFIG/init.fish ^/dev/null diff --git a/lib/autoload.fish b/lib/autoload.fish new file mode 100644 index 0000000..3432e2d --- /dev/null +++ b/lib/autoload.fish @@ -0,0 +1,65 @@ +function __autoload_insert + set -l function_path + set -l complete_path + + for path in $argv + not test -d "$path"; and continue + + set -l IFS '/' + echo $path | read -la components + + if test "$components[-1]" = completions + contains -- $path $fish_complete_path + or set complete_path $complete_path $path + else + contains -- $path $fish_function_path + or set function_path $function_path $path + end + end + + test "$function_path" + and set fish_function_path $fish_function_path[1] $function_path $fish_function_path[2..-1] + test "$complete_path" + and set fish_complete_path $fish_complete_path[1] $complete_path $fish_complete_path[2..-1] + + return 0 +end + +function __autoload_erase + set -l function_indexes + set -l complete_indexes + + for path in $argv + set -l IFS '/' + echo $path | read -la components + + test "$components[-1]" = completions + and set complete_indexes $complete_indexes (contains -i $path $fish_complete_path) + or set function_indexes $function_indexes (contains -i $path $fish_function_path) + end + + test -n "$function_indexes" + and set -e fish_function_path[$function_indexes] + test -n "$complete_indexes" + and set -e fish_complete_path[$complete_indexes] + + return 0 +end + +function autoload + set -l paths $argv + + switch "$argv[1]" + case '-e' '--erase' + test (count $argv) -ge 2 + and __autoload_erase $argv[2..-1] + or echo "usage: autoload $argv[1] <path>..." 1>&2 + case "-*" "--*" + echo "autoload: invalid option $argv[1]" + return 1 + case '*' + test (count $argv) -ge 1 + and __autoload_insert $argv + or echo "usage: autoload <path>..." 1>&2 + end +end diff --git a/lib/available.fish b/lib/available.fish new file mode 100644 index 0000000..191673b --- /dev/null +++ b/lib/available.fish @@ -0,0 +1,3 @@ +function available -a name -d "Check if argument a function or command" + type -q "$name" +end diff --git a/lib/initialize.fish b/lib/initialize.fish new file mode 100644 index 0000000..68799ca --- /dev/null +++ b/lib/initialize.fish @@ -0,0 +1,20 @@ +function initialize -d "Initialize a package" + for init in $argv + set -l IFS '/' + echo $init | read -la components + + set package $components[-2] + set path (printf '/%s' $components[1..-2]) + set bundle $path/bundle + + if test -f $bundle + while read -l type dependency + test "$type" = package + and require "$dependency" + end < $bundle + end + + source $init $path + emit init_$package $path + end +end diff --git a/lib/reload.fish b/lib/reload.fish new file mode 100644 index 0000000..e229b90 --- /dev/null +++ b/lib/reload.fish @@ -0,0 +1,10 @@ +function reload -d "Reload fish process via exec" + set -q CI; and return 0 + + set -gx dirprev $dirprev + set -gx dirnext $dirnext + set -gx dirstack $dirstack + set -gx fish_greeting '' + history --save + exec fish +end diff --git a/lib/require.fish b/lib/require.fish new file mode 100644 index 0000000..21a99e6 --- /dev/null +++ b/lib/require.fish @@ -0,0 +1,32 @@ +function require + set packages $argv + + # Skip if plugin has already been loaded. + for package in $packages + if contains -- $package $omf_packages_loaded + set index (contains -i -- $package $packages) + set -e packages[$index] + end + end + + set function_path {$OMF_PATH,$OMF_CONFIG}/pkg*/$packages{,/functions} + set completion_path {$OMF_PATH,$OMF_CONFIG}/pkg*/$packages/completions + + # Autoload functions + test "$function_path" + and set fish_function_path $fish_function_path[1] \ + $function_path \ + $fish_function_path[2..-1] + + # Autoload completions + test "$complete_path" + and set fish_complete_path $fish_complete_path[1] \ + $complete_path \ + $fish_complete_path[2..-1] + + initialize {$OMF_CONFIG,$OMF_PATH}/pkg*/$packages/init.fish + + set -g omf_packages_loaded $packages $omf_packages_loaded + + return 0 +end -- GitLab