├── LICENSE ├── README.md ├── _zload ├── zload └── zload.plugin.zsh /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Hideaki Miyake 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zload 2 | 3 | ## Synopsis 4 | zsh plugin to load functions as an autoloading function. 5 | 6 | ## How to set up 7 | 8 | ### Manually install 9 | 10 | Put zload and _zload files somewhere in your $fpath and add the following line to your .zshrc: 11 | 12 | ``` 13 | autoload -Uz zload 14 | ``` 15 | 16 | #### For example 17 | 18 | ``` 19 | # download all files 20 | % cd /path/to/dir 21 | % git clone https://github.com/mollifier/zload.git 22 | ``` 23 | 24 | And add the following lines to your .zshrc: 25 | 26 | ``` 27 | fpath=(/path/to/dir/zload(N-/) $fpath) 28 | 29 | autoload -Uz zload 30 | ``` 31 | 32 | ### Installing using Antigen 33 | If you use [Antigen](https://github.com/zsh-users/antigen), add the following line to your .zshrc: 34 | 35 | ``` 36 | antigen bundle mollifier/zload 37 | ``` 38 | 39 | ## Usage 40 | 41 | ``` 42 | % zload [-a|-d] FILE... 43 | ``` 44 | 45 | Load FILEs as an autoloading function. 46 | zload can load autoloading function or zsh completion files as input file 47 | 48 | When -a option is specified, from then on come to reload functions automatically. -d option stops this feature. 49 | 50 | ## Options 51 | * `-a` enable auto reload 52 | * `-d` disable auto reload 53 | * `-h` display help and exit 54 | 55 | ## Examples 56 | 57 | ``` 58 | # Load myfunc and _myfunc. 59 | % zload myfunc _myfunc 60 | 61 | # Now we can call myfunc even if it isn't in fpath. 62 | % myfunc 63 | 64 | # Load myfunc again and enable auto reload. 65 | % zload -a myfunc 66 | 67 | # edit myfunc 68 | # ... 69 | # Changes are applied immediately if auto reload is enabled. 70 | % myfunc 71 | 72 | # Disable auto reload. 73 | % zload -d 74 | 75 | # Enable auto reload. 76 | % zload -a 77 | ``` 78 | 79 | -------------------------------------------------------------------------------- /_zload: -------------------------------------------------------------------------------- 1 | #compdef zload 2 | # ------------------------------------------------------------------------------ 3 | # Copyright (c) 2014 Hideaki Miyake 4 | # Licensed under the MIT License (MIT) 5 | # ------------------------------------------------------------------------------ 6 | # Description 7 | # ----------- 8 | # 9 | # Completion script for zload (https://github.com/mollifier/zload) 10 | # 11 | # ------------------------------------------------------------------------------ 12 | # Authors 13 | # ------- 14 | # 15 | # * Hideaki Miyake (https://github.com/mollifier) 16 | # 17 | # ------------------------------------------------------------------------------ 18 | 19 | declare -a opts args 20 | args=( 21 | '-h[display this help and exit]' 22 | '(-d)-a[enable auto reload]' 23 | '(-a)-d[disable auto reload]' 24 | '(-)*:files:_files' 25 | ) 26 | local curcontext=$curcontext state line ret=1 27 | declare -A opt_args 28 | 29 | _arguments -C $opts \ 30 | $args && ret=0 31 | 32 | return $ret 33 | 34 | # Local Variables: 35 | # mode: Shell-Script 36 | # sh-indentation: 2 37 | # indent-tabs-mode: nil 38 | # sh-basic-offset: 2 39 | # End: 40 | # vim: ft=zsh sw=2 ts=2 et 41 | 42 | -------------------------------------------------------------------------------- /zload: -------------------------------------------------------------------------------- 1 | # zload - zsh plugin to load functions as an autoloading function. 2 | # 3 | # Copyright (c) 2014 Hideaki Miyake 4 | # Licensed under the MIT License (MIT) 5 | # 6 | # Author : Hideaki Miyake (https://github.com/mollifier) 7 | # URL : https://github.com/mollifier/zload 8 | # 9 | # How to set up 10 | # Put zload and _zload files somewhere in your $fpath 11 | # and add this line to your .zshrc: 12 | # 13 | # autoload -Uz zload 14 | # 15 | # Usage: zload [-a|-d] FILE... 16 | # 17 | 18 | emulate -L zsh 19 | 20 | typeset -r SCRIPT_NAME="zload" 21 | 22 | # global variables 23 | if (( ! ${+_zload_loaded_function_path_list} )) ; then 24 | _zload_loaded_function_path_list=() 25 | fi 26 | 27 | function _zload_print_usage() 28 | { 29 | cat << EOF 30 | Usage: $SCRIPT_NAME [-a|-d] FILE... 31 | Load files as an autoloading function. 32 | $SCRIPT_NAME can load autoloading functions or zsh completion functions. 33 | When -a option is specified, from then on come to reload functions automatically. 34 | 35 | -a enable auto reload 36 | -d disable auto reload 37 | -h display this help and exit 38 | 39 | Examples: 40 | $SCRIPT_NAME myfunc _myfunc Load myfunc and _myfunc. 41 | $SCRIPT_NAME -a myfunc Load myfunc and enable auto reload 42 | $SCRIPT_NAME -a Enable auto reload. 43 | $SCRIPT_NAME -d Disable auto reload. 44 | EOF 45 | } 46 | 47 | function _zload_print_error() 48 | { 49 | echo "$SCRIPT_NAME: $@" 1>&2 50 | echo "Try \`-h' option for more information." 1>&2 51 | } 52 | 53 | # load specified files as an autoloading function 54 | function _zload_load_files() 55 | { 56 | local file function_path function_name 57 | for file in "$@"; do 58 | if [[ -z "$file" ]]; then 59 | continue 60 | fi 61 | 62 | if [[ -d "${file:A}" ]]; then 63 | echo "$SCRIPT_NAME: $file: Is a directory" 1>&2 64 | continue 65 | fi 66 | if [[ ! -f "${file:A}" ]]; then 67 | echo "$SCRIPT_NAME: $file: No such file" 1>&2 68 | continue 69 | fi 70 | 71 | function_path="${file:h}" 72 | function_name="${file:t}" 73 | 74 | if (( $+functions[$function_name] )) ; then 75 | # "function_name" is defined 76 | unfunction -- "$function_name" 77 | fi 78 | FPATH="$function_path" autoload -Uz +X -- "$function_name" 79 | 80 | if [[ "$function_name" == _* ]]; then 81 | # "function_name" is a completion script 82 | 83 | # fpath requires absolute path 84 | # convert relative path to absolute path with :a modifier 85 | fpath=("${function_path:a}" $fpath) compinit 86 | fi 87 | 88 | # store file fullpath even if zload auto reloading is disabled 89 | local file_fullpath="${file:a}" 90 | if (( ${_zload_loaded_function_path_list[(I)$file_fullpath]} == 0 )); then 91 | # $file_fullpath isn't stored yet 92 | _zload_loaded_function_path_list=($_zload_loaded_function_path_list "$file_fullpath") 93 | fi 94 | 95 | done 96 | } 97 | 98 | # reload autoloading functions which are already defined 99 | function _zload_reload_files() 100 | { 101 | local file function_path function_name 102 | for file in "$@"; do 103 | local file_found=1 # file found 104 | if [[ -z "$file" ]]; then 105 | file_found=0 106 | fi 107 | 108 | if [[ -d "${file:A}" ]]; then 109 | file_found=0 110 | fi 111 | if [[ ! -f "${file:A}" ]]; then 112 | file_found=0 113 | fi 114 | 115 | function_path="${file:h}" 116 | function_name="${file:t}" 117 | 118 | if (( $+functions[$function_name] )) ; then 119 | # "function_name" is defined 120 | unfunction -- "$function_name" 121 | 122 | if [[ "$file_found" == "1" ]]; then 123 | FPATH="$function_path" autoload -Uz +X -- "$function_name" 124 | fi 125 | fi 126 | done 127 | } 128 | 129 | function _zload_auto_reload_hook() 130 | { 131 | # reload functions automatically 132 | if (( ${#_zload_loaded_function_path_list} )); then 133 | _zload_reload_files $_zload_loaded_function_path_list 134 | fi 135 | } 136 | 137 | function _zload_enable_auto_load() 138 | { 139 | # add _zload_auto_reload_hook to precmd_functions array 140 | if (( ! ${+precmd_functions} )); then 141 | precmd_functions=() 142 | fi 143 | 144 | if (( ${precmd_functions[(I)_zload_auto_reload_hook]} == 0 )); then 145 | # array dosen't contain _zload_auto_reload_hook function 146 | precmd_functions=($precmd_functions _zload_auto_reload_hook) 147 | fi 148 | } 149 | 150 | function _zload_disable_auto_load() 151 | { 152 | # remove _zload_auto_reload_hook from precmd_functions array 153 | if (( ${+precmd_functions} )); then 154 | precmd_functions=(${precmd_functions:#_zload_auto_reload_hook}) 155 | fi 156 | 157 | # unset precmd_functions if it is empty 158 | if (( ! ${#precmd_functions} )); then 159 | unset precmd_functions 160 | fi 161 | } 162 | 163 | # main function 164 | function _zload_main() 165 | { 166 | # 'enable' or 'disable' 167 | local autoreload_mode="" 168 | 169 | local option OPTARG OPTIND 170 | while getopts ':adh' option; do 171 | case $option in 172 | a) 173 | autoreload_mode="enable" 174 | ;; 175 | d) 176 | autoreload_mode="disable" 177 | ;; 178 | h) 179 | _zload_print_usage 180 | return 0 181 | ;; 182 | :) 183 | _zload_print_error "option requires an argument -- $OPTARG" 184 | return 1 185 | ;; 186 | *) 187 | _zload_print_error "invalid option -- $OPTARG" 188 | return 1 189 | ;; 190 | esac 191 | done 192 | shift $(expr $OPTIND - 1) 193 | 194 | if [[ "$autoreload_mode" == "enable" ]]; then 195 | _zload_enable_auto_load 196 | elif [[ "$autoreload_mode" == "disable" ]]; then 197 | _zload_disable_auto_load 198 | fi 199 | 200 | if [[ "${#}" -le 0 ]]; then 201 | if [[ -n "$autoreload_mode" ]]; then 202 | return 0 203 | else 204 | _zload_print_error 'you must specify filename' 205 | return 1 206 | fi 207 | fi 208 | 209 | _zload_load_files "$@" 210 | } 211 | 212 | _zload_main "$@" 213 | 214 | # Local Variables: 215 | # mode: Shell-Script 216 | # sh-indentation: 2 217 | # indent-tabs-mode: nil 218 | # sh-basic-offset: 2 219 | # End: 220 | # vim: ft=zsh sw=2 ts=2 et 221 | 222 | -------------------------------------------------------------------------------- /zload.plugin.zsh: -------------------------------------------------------------------------------- 1 | # init script for antigen 2 | autoload -Uz zload 3 | --------------------------------------------------------------------------------