From 3dccefa0684003b32fb5516f6c46342dedf2a1dc Mon Sep 17 00:00:00 2001 From: Konstantin Date: Mon, 25 Dec 2023 23:14:31 -0800 Subject: [PATCH 01/20] allow configuring the directory where functions are stored with the $MFUNCDIR environment variable and respect $ZDOTDIR --- mfunc.plugin.zsh | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/mfunc.plugin.zsh b/mfunc.plugin.zsh index b441876..0c08374 100755 --- a/mfunc.plugin.zsh +++ b/mfunc.plugin.zsh @@ -12,22 +12,26 @@ ###### # this is where our functions live -fdir=$HOME/.functions +[[ -z $MFUNCDIR ]] && + [[ -z $ZDOTDIR ]] && + export MFUNCDIR="$ZDOTDIR/functions" || + export MFUNCDIR="$HOME/.functions" + # check if functions directory exists, create if it doesn't -if [[ ! -d $fdir/ ]]; then - mkdir $fdir/ - echo "mfunc init: functions directory created in $fdir" +if [[ ! -d $MFUNCDIR/ ]]; then + mkdir $MFUNCDIR/ + echo "mfunc init: functions directory created in $MFUNCDIR" fi -# check if fpath contains our fdir, add it if it doesn't -if (( ! ${fpath[(I)$fdir]} )); then - fpath=($fdir $fpath) +# check if fpath contains our MFUNCDIR, add it if it doesn't +if (( ! ${fpath[(I)$MFUNCDIR]} )); then + fpath=($MFUNCDIR $fpath) fi # autoload any functions in functions directory -if [[ -e $fdir/* ]]; then - autoload $(ls $fdir/) +if [[ -e $MFUNCDIR/* ]]; then + autoload $(ls $MFUNCDIR/) fi # @@ -69,12 +73,12 @@ _mf_yesorno() } function _mf_define() { - touch $fdir/$i - chmod +x $fdir/$i + touch $MFUNCDIR/$i + chmod +x $MFUNCDIR/$i echo "enter function '$i' and finish with CTRL-D" cat >$HOME/.functions/$i - echo "new function '$i' created in $fdir" - autoload $(ls $fdir/) + echo "new function '$i' created in $MFUNCDIR" + autoload $(ls $MFUNCDIR/) echo "function is now available" } @@ -89,7 +93,7 @@ function mfunc() { else for i do; # TODO: input sanitization - if [[ -e $fdir/$i ]] + if [[ -e $MFUNCDIR/$i ]] then if _mf_yesorno "function $i already exists, overwrite? (Y/n)" then @@ -116,8 +120,8 @@ function rfunc() { # TODO: autocompletion/wildcards for i; do - if [ -e $fdir/$i ]; then - rm $fdir/$i + if [ -e $MFUNCDIR/$i ]; then + rm $MFUNCDIR/$i echo "function $i removed" else echo "function $i not found" @@ -129,7 +133,7 @@ function rfunc() { # list functions function lfunc() { # TODO: specific functions, wildcards, names only OR name + definition - for f in $(ls $fdir); do - echo $f "() {"; cat $fdir/$f; echo "}\n" + for f in $(ls $MFUNCDIR); do + echo $f "() {"; cat $MFUNCDIR/$f; echo "}\n" done } From 41fc92b9b34f334d2df37d90adaf63a79f301161 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Mon, 25 Dec 2023 23:22:16 -0800 Subject: [PATCH 02/20] fix & update README.md --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d2bcad3..b37ec0f 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ first time). #### Installation -######a) As an oh-my-zsh plugin +###### a) As an oh-my-zsh plugin 1. Run: `cd $ZSH/custom && git clone https://github.com/hlohm/mfunc.git plugins/mfunc` @@ -27,11 +27,11 @@ first time). look something like this: `plugins=(git mfunc)` -######b) using antigen +###### b) using antigen 1. Add this line where you load your antigen bundles in your `.zshrc`: `antigen bundle hlohm/mfunc` -######c) with vanilla ZSH +###### c) with vanilla ZSH 1. `git clone` this repo to a location of your choice 2. add a line `source /location/of/your/choice/mfunc.plugin.zsh` to your .zshrc @@ -39,6 +39,12 @@ look something like this: Upon its first run the plugin will notify you that it created the directory in which it stores your functions. +#### Configuration +The directory where functions are stored can be changed by setting the MFUNCDIR environment variable. + +By default, `$ZDOTDIR/functions` will be used. +If ZDOTDIR isn't set, `$HOME/.functions` will be used instead. + #### Disclaimer This is an early version covering only the most basic functionality. There are From 776a18a864ce3def52931b9b4d34ca3e99b6f77d Mon Sep 17 00:00:00 2001 From: Konstantin <78656278+amogus07@users.noreply.github.com> Date: Tue, 26 Dec 2023 19:57:18 -0800 Subject: [PATCH 03/20] fix mfunc.plugin.zsh should've tested before publishing lol --- mfunc.plugin.zsh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mfunc.plugin.zsh b/mfunc.plugin.zsh index 0c08374..080d3fb 100755 --- a/mfunc.plugin.zsh +++ b/mfunc.plugin.zsh @@ -13,9 +13,9 @@ # this is where our functions live [[ -z $MFUNCDIR ]] && - [[ -z $ZDOTDIR ]] && - export MFUNCDIR="$ZDOTDIR/functions" || - export MFUNCDIR="$HOME/.functions" + MFUNCDIR="$HOME/.functions" + [[ -z $ZDOTDIR ]] || + MFUNCDIR="$ZDOTDIR/functions" # check if functions directory exists, create if it doesn't From 1f2949d6c70979b6fb74fb6ba9b86acdd2f13f28 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 27 Dec 2023 14:16:50 -0800 Subject: [PATCH 04/20] more fixes and improvements, fully restructure the plugin --- functions/_mf_define | 8 +++ functions/_mf_yesorno | 29 +++++++++ functions/lfunc | 4 ++ functions/mfunc | 18 ++++++ functions/rfunc | 17 ++++++ mfunc.plugin.zsh | 138 ++++++------------------------------------ 6 files changed, 93 insertions(+), 121 deletions(-) create mode 100644 functions/_mf_define create mode 100644 functions/_mf_yesorno create mode 100644 functions/lfunc create mode 100644 functions/mfunc create mode 100644 functions/rfunc diff --git a/functions/_mf_define b/functions/_mf_define new file mode 100644 index 0000000..60efbea --- /dev/null +++ b/functions/_mf_define @@ -0,0 +1,8 @@ +touch $MFUNCDIR/$i +chmod u+x $MFUNCDIR/$i +echo "enter function '$i' and finish with CTRL-D" +>$MFUNCDIR/$i +echo +echo "new function '$i' created in $MFUNCDIR" +autoload $i +echo "function is now available" diff --git a/functions/_mf_yesorno b/functions/_mf_yesorno new file mode 100644 index 0000000..05d315d --- /dev/null +++ b/functions/_mf_yesorno @@ -0,0 +1,29 @@ +# variables +local question="${1}" +local prompt="${question} " +local yes_RETVAL="0" +local no_RETVAL="3" +local RETVAL="" +local answer="" + +# read-eval loop +while true ; do +printf $prompt +read -r answer + +case ${answer:=${default}} in + Y|y|YES|yes|Yes ) + RETVAL=${yes_RETVAL} && \ + break + ;; + N|n|NO|no|No ) + RETVAL=${no_RETVAL} && \ + break + ;; + * ) + echo "Please provide a valid answer (y or n)" + ;; +esac +done + +return ${RETVAL} diff --git a/functions/lfunc b/functions/lfunc new file mode 100644 index 0000000..942b974 --- /dev/null +++ b/functions/lfunc @@ -0,0 +1,4 @@ +# TODO: specific functions, wildcards, names only OR name + definition +for f in $MFUNCDIR/*; do + printf "%s () {\n%s\n}\n\n" "$(basename $f)" "$(cat $f)" +done diff --git a/functions/mfunc b/functions/mfunc new file mode 100644 index 0000000..93624b1 --- /dev/null +++ b/functions/mfunc @@ -0,0 +1,18 @@ +# berate user if no arguments given +# TODO: interactive mode +(($# == 0)) && { + echo "usage: mfunc [function name]" + return 1 +} + +for i; do + # TODO: input sanitization + [[ -e $MFUNCDIR/$i ]] && { + _mf_yesorno "function $i already exists, overwrite? (Y/n)" || { + echo "aborted" + return + } + unfunction $i # forget the old version first + } + _mf_define $i +done diff --git a/functions/rfunc b/functions/rfunc new file mode 100644 index 0000000..92c6124 --- /dev/null +++ b/functions/rfunc @@ -0,0 +1,17 @@ +# demand argument +# TODO: interactive mode +(($# == 0)) && { + echo "please name at least one function to delete" + return 1 +} + +# TODO: autocompletion/wildcards +for i; do + [[ -e $MFUNCDIR/$i ]] || { + echo "function $i not found" + return 2 + } + rm $MFUNCDIR/$i + echo "function $i removed" +done +echo "functions might still be available until next login" diff --git a/mfunc.plugin.zsh b/mfunc.plugin.zsh index 080d3fb..5a69cbe 100755 --- a/mfunc.plugin.zsh +++ b/mfunc.plugin.zsh @@ -6,134 +6,30 @@ # github.com/hlohm ################## - -# -# init -###### - # this is where our functions live -[[ -z $MFUNCDIR ]] && - MFUNCDIR="$HOME/.functions" - [[ -z $ZDOTDIR ]] || - MFUNCDIR="$ZDOTDIR/functions" +[[ -v MFUNCDIR ]] || { + MFUNCDIR="$HOME/.functions" + [[ -v ZDOTDIR ]] && + MFUNCDIR="$ZDOTDIR/functions" + export MFUNCDIR +} # check if functions directory exists, create if it doesn't -if [[ ! -d $MFUNCDIR/ ]]; then +[[ -d $MFUNCDIR/ ]] || { mkdir $MFUNCDIR/ echo "mfunc init: functions directory created in $MFUNCDIR" -fi - -# check if fpath contains our MFUNCDIR, add it if it doesn't -if (( ! ${fpath[(I)$MFUNCDIR]} )); then - fpath=($MFUNCDIR $fpath) -fi - -# autoload any functions in functions directory -if [[ -e $MFUNCDIR/* ]]; then - autoload $(ls $MFUNCDIR/) -fi - -# -#functions -########## - -# helpers -_mf_yesorno() -{ - # variables - local question="${1}" - local prompt="${question} " - local yes_RETVAL="0" - local no_RETVAL="3" - local RETVAL="" - local answer="" - - # read-eval loop - while true ; do - printf $prompt - read -r answer - - case ${answer:=${default}} in - Y|y|YES|yes|Yes ) - RETVAL=${yes_RETVAL} && \ - break - ;; - N|n|NO|no|No ) - RETVAL=${no_RETVAL} && \ - break - ;; - * ) - echo "Please provide a valid answer (y or n)" - ;; - esac - done - - return ${RETVAL} -} - -function _mf_define() { - touch $MFUNCDIR/$i - chmod +x $MFUNCDIR/$i - echo "enter function '$i' and finish with CTRL-D" - cat >$HOME/.functions/$i - echo "new function '$i' created in $MFUNCDIR" - autoload $(ls $MFUNCDIR/) - echo "function is now available" -} - -# make function(s) -function mfunc() { - - # berate user if no arguments given - # TODO: interactive mode - if (($# == 0)) - then - echo "usage: mfunc [function name]" - else - for i do; - # TODO: input sanitization - if [[ -e $MFUNCDIR/$i ]] - then - if _mf_yesorno "function $i already exists, overwrite? (Y/n)" - then - unfunction $1 # forget the old version first - _mf_define $i - else - echo "aborted" - fi - else - _mf_define $i - fi - done - fi } -# remove function(s) -function rfunc() { - - # demand argument - # TODO: interactive mode - if (($# == 0)) ; then - echo "please name at least one function to delete"; - fi +# add MFUNC_FUNCTIONS_D to fpath +MFUNC_FUNCTIONS_D="$(dirname $0)/functions" +fpath=($MFUNC_FUNCTIONS_D "${fpath[@]}" ) - # TODO: autocompletion/wildcards - for i; do - if [ -e $MFUNCDIR/$i ]; then - rm $MFUNCDIR/$i - echo "function $i removed" - else - echo "function $i not found" - fi - done - echo "functions might still be available until next login" -} +# check if fpath contains our MFUNCDIR, add it if it doesn't +(( ! ${fpath[(I)$MFUNCDIR]} )) && + fpath=($MFUNCDIR $fpath) -# list functions -function lfunc() { - # TODO: specific functions, wildcards, names only OR name + definition - for f in $(ls $MFUNCDIR); do - echo $f "() {"; cat $MFUNCDIR/$f; echo "}\n" - done -} +# autoload functions +for file in $MFUNC_FUNCTIONS_D/* $MFUNCDIR/*; do + autoload $(basename $file) +done From 58c83ecc50bf9cc1c3c1a9bfe1eedeb27d365105 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 27 Dec 2023 17:19:45 -0800 Subject: [PATCH 05/20] make use of $EDITOR instead of redirecting stdin + more fixes and improvements --- functions/_mf_define | 11 ++++++----- functions/lfunc | 2 +- functions/mfunc | 6 ++++-- functions/rfunc | 4 ++++ mfunc.plugin.zsh | 15 +++++++-------- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/functions/_mf_define b/functions/_mf_define index 60efbea..bc78095 100644 --- a/functions/_mf_define +++ b/functions/_mf_define @@ -1,8 +1,9 @@ -touch $MFUNCDIR/$i -chmod u+x $MFUNCDIR/$i -echo "enter function '$i' and finish with CTRL-D" ->$MFUNCDIR/$i -echo +local editor=$(basename $EDITOR) +[[ $editor == *vim ]] && + local ftopts="-c \"set ft=zsh\"" || +[[ $editor == micro ]] && + local ftopts="-filetype=zsh" +$EDITOR "$ftopts" "${MFUNCDIR}/$i" echo "new function '$i' created in $MFUNCDIR" autoload $i echo "function is now available" diff --git a/functions/lfunc b/functions/lfunc index 942b974..9964438 100644 --- a/functions/lfunc +++ b/functions/lfunc @@ -1,4 +1,4 @@ # TODO: specific functions, wildcards, names only OR name + definition -for f in $MFUNCDIR/*; do +for f in ${MFUNCDIR}/*; do printf "%s () {\n%s\n}\n\n" "$(basename $f)" "$(cat $f)" done diff --git a/functions/mfunc b/functions/mfunc index 93624b1..8520866 100644 --- a/functions/mfunc +++ b/functions/mfunc @@ -8,10 +8,12 @@ for i; do # TODO: input sanitization [[ -e $MFUNCDIR/$i ]] && { - _mf_yesorno "function $i already exists, overwrite? (Y/n)" || { - echo "aborted" + read -q "choice?function $i already exists, press Y/y to edit with $EDITOR or any other key to abort: " || { + echo + echo "aborting..." return } + echo unfunction $i # forget the old version first } _mf_define $i diff --git a/functions/rfunc b/functions/rfunc index 92c6124..5eea073 100644 --- a/functions/rfunc +++ b/functions/rfunc @@ -11,6 +11,10 @@ for i; do echo "function $i not found" return 2 } + read -q "choice?Press Y/y to remove $i from $MFUNCDIR or any other key to skip:" || + echo + continue + echo rm $MFUNCDIR/$i echo "function $i removed" done diff --git a/mfunc.plugin.zsh b/mfunc.plugin.zsh index 5a69cbe..910d79a 100755 --- a/mfunc.plugin.zsh +++ b/mfunc.plugin.zsh @@ -7,12 +7,10 @@ ################## # this is where our functions live -[[ -v MFUNCDIR ]] || { - MFUNCDIR="$HOME/.functions" +[[ -v MFUNCDIR ]] || + MFUNCDIR="$HOME/.functions" && [[ -v ZDOTDIR ]] && MFUNCDIR="$ZDOTDIR/functions" - export MFUNCDIR -} # check if functions directory exists, create if it doesn't @@ -21,13 +19,14 @@ echo "mfunc init: functions directory created in $MFUNCDIR" } -# add MFUNC_FUNCTIONS_D to fpath +# check if fpath contains MFUNC_FUNCTIONS_D, add it if it doesn't MFUNC_FUNCTIONS_D="$(dirname $0)/functions" -fpath=($MFUNC_FUNCTIONS_D "${fpath[@]}" ) +(( ${fpath[(I)$MFUNC_FUNCTIONS_D]} )) || + fpath=($MFUNC_FUNCTIONS_D "${fpath[@]}") # check if fpath contains our MFUNCDIR, add it if it doesn't -(( ! ${fpath[(I)$MFUNCDIR]} )) && - fpath=($MFUNCDIR $fpath) +(( ${fpath[(I)$MFUNCDIR]} )) || + fpath=($MFUNCDIR "${fpath[@]}") # autoload functions for file in $MFUNC_FUNCTIONS_D/* $MFUNCDIR/*; do From 26357be575f25ec1b427e72bbb7a209a26fc4578 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 27 Dec 2023 20:58:32 -0800 Subject: [PATCH 06/20] use string manipulation instead of calling basename --- functions/_mf_define | 7 +++++-- functions/_mf_yesorno | 29 ----------------------------- functions/lfunc | 2 +- mfunc.plugin.zsh | 2 +- 4 files changed, 7 insertions(+), 33 deletions(-) delete mode 100644 functions/_mf_yesorno diff --git a/functions/_mf_define b/functions/_mf_define index bc78095..5b0d0c3 100644 --- a/functions/_mf_define +++ b/functions/_mf_define @@ -1,9 +1,12 @@ -local editor=$(basename $EDITOR) +local editor="${EDITOR##*/}" [[ $editor == *vim ]] && local ftopts="-c \"set ft=zsh\"" || [[ $editor == micro ]] && local ftopts="-filetype=zsh" -$EDITOR "$ftopts" "${MFUNCDIR}/$i" +$EDITOR "$ftopts" "${MFUNCDIR}/$i" || { + echo "aborting..." + return +} echo "new function '$i' created in $MFUNCDIR" autoload $i echo "function is now available" diff --git a/functions/_mf_yesorno b/functions/_mf_yesorno deleted file mode 100644 index 05d315d..0000000 --- a/functions/_mf_yesorno +++ /dev/null @@ -1,29 +0,0 @@ -# variables -local question="${1}" -local prompt="${question} " -local yes_RETVAL="0" -local no_RETVAL="3" -local RETVAL="" -local answer="" - -# read-eval loop -while true ; do -printf $prompt -read -r answer - -case ${answer:=${default}} in - Y|y|YES|yes|Yes ) - RETVAL=${yes_RETVAL} && \ - break - ;; - N|n|NO|no|No ) - RETVAL=${no_RETVAL} && \ - break - ;; - * ) - echo "Please provide a valid answer (y or n)" - ;; -esac -done - -return ${RETVAL} diff --git a/functions/lfunc b/functions/lfunc index 9964438..f0c1189 100644 --- a/functions/lfunc +++ b/functions/lfunc @@ -1,4 +1,4 @@ # TODO: specific functions, wildcards, names only OR name + definition for f in ${MFUNCDIR}/*; do - printf "%s () {\n%s\n}\n\n" "$(basename $f)" "$(cat $f)" + printf "%s () {\n%s\n}\n\n" "${f##*/}" "$(cat $f)" done diff --git a/mfunc.plugin.zsh b/mfunc.plugin.zsh index 910d79a..a7e0b95 100755 --- a/mfunc.plugin.zsh +++ b/mfunc.plugin.zsh @@ -30,5 +30,5 @@ MFUNC_FUNCTIONS_D="$(dirname $0)/functions" # autoload functions for file in $MFUNC_FUNCTIONS_D/* $MFUNCDIR/*; do - autoload $(basename $file) + autoload ${file##*/} done From 435ba325afc5c39bffae2647d56b0f8aaae3443c Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 27 Dec 2023 22:30:58 -0800 Subject: [PATCH 07/20] reduce output when creating multible functions at once --- functions/_mf_define | 1 - functions/mfunc | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/functions/_mf_define b/functions/_mf_define index 5b0d0c3..ed58a06 100644 --- a/functions/_mf_define +++ b/functions/_mf_define @@ -9,4 +9,3 @@ $EDITOR "$ftopts" "${MFUNCDIR}/$i" || { } echo "new function '$i' created in $MFUNCDIR" autoload $i -echo "function is now available" diff --git a/functions/mfunc b/functions/mfunc index 8520866..374fafe 100644 --- a/functions/mfunc +++ b/functions/mfunc @@ -18,3 +18,6 @@ for i; do } _mf_define $i done +(($# > 1)) && + echo "function is now available" || + echo "functions are now available From 13c6737842587c914adc7151002ef2dabf102c28 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Mon, 1 Jan 2024 22:54:41 -0800 Subject: [PATCH 08/20] lots more major changes --- functions/_lf_help | 7 +++++++ functions/_lf_verbose | 9 +++++++++ functions/_mf_define | 13 +++++++------ functions/lfunc | 19 +++++++++++++++---- functions/mfunc | 11 +++++++---- functions/rfunc | 20 ++++++++++++-------- mfunc.plugin.zsh | 11 ++++------- 7 files changed, 61 insertions(+), 29 deletions(-) create mode 100644 functions/_lf_help create mode 100644 functions/_lf_verbose diff --git a/functions/_lf_help b/functions/_lf_help new file mode 100644 index 0000000..58ca3e4 --- /dev/null +++ b/functions/_lf_help @@ -0,0 +1,7 @@ +echo "Part of the mfunc zsh plugin" +echo "Lists all the functions in $MFUNCDIR" +echo +echo "Usage: $0 [-v] [-h]" +echo "Options:" +echo " -v Enable verbose mode (print the content of each function)" +echo " -h Print this help message" diff --git a/functions/_lf_verbose b/functions/_lf_verbose new file mode 100644 index 0000000..2c611fc --- /dev/null +++ b/functions/_lf_verbose @@ -0,0 +1,9 @@ +local str=() +for f in ${MFUNCDIR}/*; do + str+=("\n${f##*/} () {" ' '${^"${(f)$(<$f)}"} "}") +done +hash highlight 2>/dev/null && + highlight -S zsh -i <(print -ln $str) || { + print -ln $str + echo "Hint: install 'highlight' to enable syntax highlighting" +} diff --git a/functions/_mf_define b/functions/_mf_define index ed58a06..31b1184 100644 --- a/functions/_mf_define +++ b/functions/_mf_define @@ -3,9 +3,10 @@ local editor="${EDITOR##*/}" local ftopts="-c \"set ft=zsh\"" || [[ $editor == micro ]] && local ftopts="-filetype=zsh" -$EDITOR "$ftopts" "${MFUNCDIR}/$i" || { - echo "aborting..." - return -} -echo "new function '$i' created in $MFUNCDIR" -autoload $i +$EDITOR "$ftopts" "${MFUNCDIR}/$i" && + [[ -f "${MFUNCDIR}/$i" ]] && + autoload $i || { + echo "aborting..." + return 1 + } +$edit || echo "new function '$i' created in $MFUNCDIR" diff --git a/functions/lfunc b/functions/lfunc index f0c1189..371fd9e 100644 --- a/functions/lfunc +++ b/functions/lfunc @@ -1,4 +1,15 @@ -# TODO: specific functions, wildcards, names only OR name + definition -for f in ${MFUNCDIR}/*; do - printf "%s () {\n%s\n}\n\n" "${f##*/}" "$(cat $f)" -done +# TODO: specific functions, wildcards +getopts "hv" opt && { + shift $((OPTIND -1)) + case ${opt} in + h ) + _lf_help + return + ;; + v ) + _lf_verbose + return + ;; + esac +} +command ls "$MFUNCDIR" diff --git a/functions/mfunc b/functions/mfunc index 374fafe..026f92d 100644 --- a/functions/mfunc +++ b/functions/mfunc @@ -7,6 +7,7 @@ for i; do # TODO: input sanitization + local edit=false [[ -e $MFUNCDIR/$i ]] && { read -q "choice?function $i already exists, press Y/y to edit with $EDITOR or any other key to abort: " || { echo @@ -15,9 +16,11 @@ for i; do } echo unfunction $i # forget the old version first + edit=true } - _mf_define $i + _mf_define $i || return done -(($# > 1)) && - echo "function is now available" || - echo "functions are now available +echo -n "function" +(($# == 1)) && + echo -n " is" ||echo -n "s are" +echo " now available" diff --git a/functions/rfunc b/functions/rfunc index 5eea073..cee9433 100644 --- a/functions/rfunc +++ b/functions/rfunc @@ -6,16 +6,20 @@ } # TODO: autocompletion/wildcards +local count=0 for i; do - [[ -e $MFUNCDIR/$i ]] || { + [[ -f $MFUNCDIR/$i ]] || { echo "function $i not found" return 2 } - read -q "choice?Press Y/y to remove $i from $MFUNCDIR or any other key to skip:" || - echo - continue - echo - rm $MFUNCDIR/$i - echo "function $i removed" + rm -iv $MFUNCDIR/$i + [[ -f $MFUNCDIR/$i ]] || count+=1 done -echo "functions might still be available until next login" + +((count == 0)) && + return + +echo -n "function" +((count > 1)) && + echo -n "s" +echo " are still be available until next login" diff --git a/mfunc.plugin.zsh b/mfunc.plugin.zsh index a7e0b95..bdfb0e1 100755 --- a/mfunc.plugin.zsh +++ b/mfunc.plugin.zsh @@ -21,14 +21,11 @@ # check if fpath contains MFUNC_FUNCTIONS_D, add it if it doesn't MFUNC_FUNCTIONS_D="$(dirname $0)/functions" -(( ${fpath[(I)$MFUNC_FUNCTIONS_D]} )) || - fpath=($MFUNC_FUNCTIONS_D "${fpath[@]}") +(( ${fpath[(I)$MFUNC_FUNCTIONS_D]} )) || fpath=($MFUNC_FUNCTIONS_D "${fpath[@]}") # check if fpath contains our MFUNCDIR, add it if it doesn't -(( ${fpath[(I)$MFUNCDIR]} )) || - fpath=($MFUNCDIR "${fpath[@]}") +(( ${fpath[(I)$MFUNCDIR]} )) || fpath=($MFUNCDIR "${fpath[@]}") # autoload functions -for file in $MFUNC_FUNCTIONS_D/* $MFUNCDIR/*; do - autoload ${file##*/} -done + files=($MFUNC_FUNCTIONS_D/* $MFUNCDIR/*) +autoload ${^files##*/} From 32843b93df36f0e308250fc21ec99926b97c3d3b Mon Sep 17 00:00:00 2001 From: Konstantin Date: Tue, 2 Jan 2024 22:56:17 -0800 Subject: [PATCH 09/20] update mfunc.plugin.zsh --- mfunc.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mfunc.plugin.zsh b/mfunc.plugin.zsh index bdfb0e1..0891c2d 100755 --- a/mfunc.plugin.zsh +++ b/mfunc.plugin.zsh @@ -27,5 +27,5 @@ MFUNC_FUNCTIONS_D="$(dirname $0)/functions" (( ${fpath[(I)$MFUNCDIR]} )) || fpath=($MFUNCDIR "${fpath[@]}") # autoload functions - files=($MFUNC_FUNCTIONS_D/* $MFUNCDIR/*) +files=($MFUNC_FUNCTIONS_D/* $MFUNCDIR/*) autoload ${^files##*/} From 4be8d4d9d58b2639e53b6313f0114ffe6ab0afe2 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Mon, 1 Jul 2024 10:03:54 +0200 Subject: [PATCH 10/20] some fixes and improvements --- functions/_lf_help | 16 +++++++++------- functions/_lf_verbose | 5 +++-- functions/_mf_define | 15 +++++++-------- functions/rfunc | 2 +- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/functions/_lf_help b/functions/_lf_help index 58ca3e4..e355be8 100644 --- a/functions/_lf_help +++ b/functions/_lf_help @@ -1,7 +1,9 @@ -echo "Part of the mfunc zsh plugin" -echo "Lists all the functions in $MFUNCDIR" -echo -echo "Usage: $0 [-v] [-h]" -echo "Options:" -echo " -v Enable verbose mode (print the content of each function)" -echo " -h Print this help message" +echo """ +Part of the mfunc zsh plugin +Lists all the functions in $MFUNCDIR + +Usage: $0 [-v] [-h] +Options: + -v Enable verbose mode (print the content of each function) + -h Print this help message + """ diff --git a/functions/_lf_verbose b/functions/_lf_verbose index 2c611fc..8fa9537 100644 --- a/functions/_lf_verbose +++ b/functions/_lf_verbose @@ -2,8 +2,9 @@ local str=() for f in ${MFUNCDIR}/*; do str+=("\n${f##*/} () {" ' '${^"${(f)$(<$f)}"} "}") done -hash highlight 2>/dev/null && +((${+commands[highlight]})) && highlight -S zsh -i <(print -ln $str) || { - print -ln $str + print -l $str + echo echo "Hint: install 'highlight' to enable syntax highlighting" } diff --git a/functions/_mf_define b/functions/_mf_define index 31b1184..cd71019 100644 --- a/functions/_mf_define +++ b/functions/_mf_define @@ -1,12 +1,11 @@ -local editor="${EDITOR##*/}" -[[ $editor == *vim ]] && - local ftopts="-c \"set ft=zsh\"" || -[[ $editor == micro ]] && +[[ $EDITOR == *vim ]] && + local ftopts="-c 'set ft=zsh'" || +[[ $EDITOR == *micro ]] && local ftopts="-filetype=zsh" -$EDITOR "$ftopts" "${MFUNCDIR}/$i" && - [[ -f "${MFUNCDIR}/$i" ]] && - autoload $i || { +eval $EDITOR "$ftopts" "${MFUNCDIR}/$1" && + [[ -f "${MFUNCDIR}/$1" ]] && + autoload $1 || { echo "aborting..." return 1 } -$edit || echo "new function '$i' created in $MFUNCDIR" +$edit || echo "new function '$1' created in $MFUNCDIR" diff --git a/functions/rfunc b/functions/rfunc index cee9433..07c0a61 100644 --- a/functions/rfunc +++ b/functions/rfunc @@ -22,4 +22,4 @@ done echo -n "function" ((count > 1)) && echo -n "s" -echo " are still be available until next login" +echo " are still available until next login" From 4ddc649b78f7f14651c3a5db85e393d2469d183a Mon Sep 17 00:00:00 2001 From: Konstantin <78656278+amogus07@users.noreply.github.com> Date: Mon, 1 Jul 2024 10:19:26 +0200 Subject: [PATCH 11/20] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b37ec0f..54a8f98 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,9 @@ The plugin defines 3 functions: |-------------------------|---------------------------------------- | `mfunc name [name] ...` | create new function(s) interactively | `rfunc name [name] ...` | delete existing user-defined function(s) -| `lfunc` | list all user-defined functions +| `lfunc [-h|v]` | list all user-defined functions -functions are stored as plain text in $ZSH/functions/ and made available via +functions are stored as plain text in $MFUNDCIR and made available via the autoload builtin (i.e. they are only loaded into memory when called for the first time). @@ -21,7 +21,7 @@ first time). ###### a) As an oh-my-zsh plugin 1. Run: -`cd $ZSH/custom && git clone https://github.com/hlohm/mfunc.git plugins/mfunc` +`cd $ZSH/custom && git clone https://github.com/amogus07/mfunc.git plugins/mfunc` 2. Add `mfunc` to your plugins in your `.zshrc`. The relevant line should look something like this: @@ -30,7 +30,7 @@ look something like this: ###### b) using antigen 1. Add this line where you load your antigen bundles in your `.zshrc`: -`antigen bundle hlohm/mfunc` +`antigen bundle amogus07/mfunc` ###### c) with vanilla ZSH 1. `git clone` this repo to a location of your choice @@ -40,10 +40,10 @@ Upon its first run the plugin will notify you that it created the directory in which it stores your functions. #### Configuration -The directory where functions are stored can be changed by setting the MFUNCDIR environment variable. +The directory where functions are stored can be changed by setting the $MFUNCDIR environment variable. By default, `$ZDOTDIR/functions` will be used. -If ZDOTDIR isn't set, `$HOME/.functions` will be used instead. +If $ZDOTDIR isn't set, `$HOME/.functions` will be used instead. #### Disclaimer From 1853f567120c8c8bb00ea9b747b0209c7f404911 Mon Sep 17 00:00:00 2001 From: Konstantin <78656278+amogus07@users.noreply.github.com> Date: Mon, 1 Jul 2024 10:20:03 +0200 Subject: [PATCH 12/20] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 54a8f98..406cb73 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ The plugin defines 3 functions: |-------------------------|---------------------------------------- | `mfunc name [name] ...` | create new function(s) interactively | `rfunc name [name] ...` | delete existing user-defined function(s) -| `lfunc [-h|v]` | list all user-defined functions +| `lfunc [-h\|v]` | list all user-defined functions functions are stored as plain text in $MFUNDCIR and made available via the autoload builtin (i.e. they are only loaded into memory when called for the From 573e8d530426f9bc154438e9b0823ef6d299df2c Mon Sep 17 00:00:00 2001 From: Konstantin <78656278+amogus07@users.noreply.github.com> Date: Mon, 1 Jul 2024 12:19:58 +0200 Subject: [PATCH 13/20] Update README.md --- README.md | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 406cb73..f8ce403 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,25 @@ -### mfunc -#### a function wrapper plugin for ZSH +# mfunc -Allows you to define persistent functions on-the-fly, without the need to add -them to your config files. Your functions are permanently available until you +A function wrapper plugin for ZSH. + +Define, view, edit and delete persistent functions on-the-fly, without extra steps. They are permanently available until you delete them. The plugin defines 3 functions: | `Command` | Action -|-------------------------|---------------------------------------- +|-------------------------|----------------------------------------- | `mfunc name [name] ...` | create new function(s) interactively | `rfunc name [name] ...` | delete existing user-defined function(s) -| `lfunc [-h\|v]` | list all user-defined functions +| `lfunc [-h\|v]` | list all user-defined functions -functions are stored as plain text in $MFUNDCIR and made available via +Functions are stored as plain text in `$MFUNDCIR` and made available via the autoload builtin (i.e. they are only loaded into memory when called for the first time). -#### Installation +## Installation -###### a) As an oh-my-zsh plugin +### a) As an oh-my-zsh plugin 1. Run: `cd $ZSH/custom && git clone https://github.com/amogus07/mfunc.git plugins/mfunc` @@ -27,11 +27,11 @@ first time). look something like this: `plugins=(git mfunc)` -###### b) using antigen +### b) using antigen 1. Add this line where you load your antigen bundles in your `.zshrc`: `antigen bundle amogus07/mfunc` -###### c) with vanilla ZSH +### c) with vanilla ZSH 1. `git clone` this repo to a location of your choice 2. add a line `source /location/of/your/choice/mfunc.plugin.zsh` to your .zshrc @@ -39,13 +39,19 @@ look something like this: Upon its first run the plugin will notify you that it created the directory in which it stores your functions. -#### Configuration -The directory where functions are stored can be changed by setting the $MFUNCDIR environment variable. +## Configuration + +| Environment Variable | Default | Description +|----------------------|------------------------------------------|------------------ +| `$MFUNCDIR` | `${ZDOTDIR/functions:-$HOME/.functions}` | functions storage + +## Optional dependencies -By default, `$ZDOTDIR/functions` will be used. -If $ZDOTDIR isn't set, `$HOME/.functions` will be used instead. +| Dependency | Description +|-------------------------------------|----------------------------------------------------------- +| [highlight](https://repology.org/project/highlight) | enables syntax highlighting for `lfunc -v` -#### Disclaimer +## Disclaimer This is an early version covering only the most basic functionality. There are no safeguards whatsover, so use at you own risk. Things like tab completion, From fde41654866d65384c13388b7bd5aadeb8ae5323 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Tue, 2 Jul 2024 01:38:00 +0200 Subject: [PATCH 14/20] more fixes and improvements --- functions/_lf_help | 2 +- functions/_lf_verbose | 25 +++++++++++++++++++------ functions/rfunc | 5 +++-- mfunc.plugin.zsh | 15 ++++++++------- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/functions/_lf_help b/functions/_lf_help index e355be8..be9016f 100644 --- a/functions/_lf_help +++ b/functions/_lf_help @@ -2,7 +2,7 @@ echo """ Part of the mfunc zsh plugin Lists all the functions in $MFUNCDIR -Usage: $0 [-v] [-h] +Usage: $0 [-v|h] Options: -v Enable verbose mode (print the content of each function) -h Print this help message diff --git a/functions/_lf_verbose b/functions/_lf_verbose index 8fa9537..a396e83 100644 --- a/functions/_lf_verbose +++ b/functions/_lf_verbose @@ -1,10 +1,23 @@ -local str=() -for f in ${MFUNCDIR}/*; do - str+=("\n${f##*/} () {" ' '${^"${(f)$(<$f)}"} "}") +# Ensure MFUNCDIR is set +[[ -v MFUNCDIR ]] || + MFUNCDIR="${HOME}/.functions" && + [[ -v ZDOTDIR ]] && + MFUNCDIR="${ZDOTDIR}/functions" + +local functions=() + +# get a list of functions managed by mfunc +local function_names=($MFUNCDIR/*) +function_names="${files[@]:t}" # strip the paths + +autoload +X "${(z)function_names[@]}" + +# Iterate over functions and append the body of each function to 'str' +for function_name in "${(z)function_names[@]}"; do + functions+='\n'$(where "$function_name")'\n' done ((${+commands[highlight]})) && - highlight -S zsh -i <(print -ln $str) || { - print -l $str - echo + highlight -S zsh -i <(print -ln "$functions") || { + print -l $functions echo "Hint: install 'highlight' to enable syntax highlighting" } diff --git a/functions/rfunc b/functions/rfunc index 07c0a61..1eab0cc 100644 --- a/functions/rfunc +++ b/functions/rfunc @@ -21,5 +21,6 @@ done echo -n "function" ((count > 1)) && - echo -n "s" -echo " are still available until next login" + echo -n "s are" || + echo -n " is" +echo " still available until next login and can be viewed with the 'where' builtin" diff --git a/mfunc.plugin.zsh b/mfunc.plugin.zsh index 0891c2d..6fd3a24 100755 --- a/mfunc.plugin.zsh +++ b/mfunc.plugin.zsh @@ -8,19 +8,20 @@ # this is where our functions live [[ -v MFUNCDIR ]] || - MFUNCDIR="$HOME/.functions" && + MFUNCDIR="${HOME}/.functions" && [[ -v ZDOTDIR ]] && - MFUNCDIR="$ZDOTDIR/functions" + MFUNCDIR="${ZDOTDIR}/functions" # check if functions directory exists, create if it doesn't -[[ -d $MFUNCDIR/ ]] || { - mkdir $MFUNCDIR/ - echo "mfunc init: functions directory created in $MFUNCDIR" +[[ -d "${MFUNCDIR}/" ]] || { + mkdir "${MFUNCDIR}/" && + echo "mfunc init: functions directory created in $MFUNCDIR" || + return } # check if fpath contains MFUNC_FUNCTIONS_D, add it if it doesn't -MFUNC_FUNCTIONS_D="$(dirname $0)/functions" +MFUNC_FUNCTIONS_D="${0:h}/functions" (( ${fpath[(I)$MFUNC_FUNCTIONS_D]} )) || fpath=($MFUNC_FUNCTIONS_D "${fpath[@]}") # check if fpath contains our MFUNCDIR, add it if it doesn't @@ -28,4 +29,4 @@ MFUNC_FUNCTIONS_D="$(dirname $0)/functions" # autoload functions files=($MFUNC_FUNCTIONS_D/* $MFUNCDIR/*) -autoload ${^files##*/} +autoload ${(z)files[@]:t} From 0b60ef56bfbc2c4d439c3ad938271da99074d6b7 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Fri, 12 Jul 2024 02:43:10 +0200 Subject: [PATCH 15/20] Update README.md --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f8ce403..278be16 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,7 @@ A function wrapper plugin for ZSH. -Define, view, edit and delete persistent functions on-the-fly, without extra steps. They are permanently available until you -delete them. +Define, view, edit and delete persistent functions on-the-fly, without extra steps. They are permanently available until you delete them. The plugin defines 3 functions: @@ -41,7 +40,7 @@ which it stores your functions. ## Configuration -| Environment Variable | Default | Description +| Environment Variable | Default | Description |----------------------|------------------------------------------|------------------ | `$MFUNCDIR` | `${ZDOTDIR/functions:-$HOME/.functions}` | functions storage @@ -54,5 +53,5 @@ which it stores your functions. ## Disclaimer This is an early version covering only the most basic functionality. There are -no safeguards whatsover, so use at you own risk. Things like tab completion, +no safeguards whatsover, so use at you own risk. Things like tab completion, input sanitization and the like are on the TODO list. From 5aff35a4f8d914153956a79e01efe024e97e5d32 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Fri, 12 Jul 2024 03:21:08 +0200 Subject: [PATCH 16/20] Improve functions/_lf_verbose --- functions/_lf_verbose | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/functions/_lf_verbose b/functions/_lf_verbose index a396e83..3f9f10b 100644 --- a/functions/_lf_verbose +++ b/functions/_lf_verbose @@ -4,20 +4,17 @@ [[ -v ZDOTDIR ]] && MFUNCDIR="${ZDOTDIR}/functions" -local functions=() - # get a list of functions managed by mfunc -local function_names=($MFUNCDIR/*) -function_names="${files[@]:t}" # strip the paths +local functions=($MFUNCDIR/*) + +# strip the paths +functions="${functions[@]:t}" -autoload +X "${(z)function_names[@]}" +# load functions into current session +autoload +X "${(z)functions[@]}" -# Iterate over functions and append the body of each function to 'str' -for function_name in "${(z)function_names[@]}"; do - functions+='\n'$(where "$function_name")'\n' -done -((${+commands[highlight]})) && - highlight -S zsh -i <(print -ln "$functions") || { - print -l $functions - echo "Hint: install 'highlight' to enable syntax highlighting" +((${+commands[highlight]})) && + highlight -S zsh -i <(where "${(z)functions[@]}") || { + where "${(z)functions[@]}" + echo "Hint: install 'highlight' to enable syntax highlighting" >&2 } From 891107e96c0eef3d50072d3245f8629c49c46ef8 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Sat, 24 Aug 2024 23:34:07 +0200 Subject: [PATCH 17/20] mostly improved code readability --- functions/_lf_help | 4 ++-- functions/_lf_verbose | 31 ++++++++++++++++++++----------- functions/_mf_define | 34 ++++++++++++++++++++++++---------- functions/lfunc | 6 ++++-- functions/mfunc | 15 +++++++++------ functions/rfunc | 22 +++++++++++++--------- mfunc.plugin.zsh | 22 +++++++++++++--------- 7 files changed, 85 insertions(+), 49 deletions(-) mode change 100644 => 100755 functions/_lf_verbose diff --git a/functions/_lf_help b/functions/_lf_help index be9016f..95929ea 100644 --- a/functions/_lf_help +++ b/functions/_lf_help @@ -1,9 +1,9 @@ -echo """ +echo """\ Part of the mfunc zsh plugin Lists all the functions in $MFUNCDIR Usage: $0 [-v|h] Options: -v Enable verbose mode (print the content of each function) - -h Print this help message + -h Print this help message\ """ diff --git a/functions/_lf_verbose b/functions/_lf_verbose old mode 100644 new mode 100755 index 3f9f10b..c5bb7c3 --- a/functions/_lf_verbose +++ b/functions/_lf_verbose @@ -1,20 +1,29 @@ # Ensure MFUNCDIR is set -[[ -v MFUNCDIR ]] || - MFUNCDIR="${HOME}/.functions" && - [[ -v ZDOTDIR ]] && +if ! [[ -v MFUNCDIR ]] +then + if [[ -v ZDOTDIR ]] + then MFUNCDIR="${ZDOTDIR}/functions" + else + MFUNCDIR="${HOME}/.functions" + fi +fi # get a list of functions managed by mfunc -local functions=($MFUNCDIR/*) +local functions=( $MFUNCDIR/* ) # strip the paths functions="${functions[@]:t}" -# load functions into current session -autoload +X "${(z)functions[@]}" +( + # load functions + autoload +X "${(z)functions[@]}" -((${+commands[highlight]})) && - highlight -S zsh -i <(where "${(z)functions[@]}") || { - where "${(z)functions[@]}" - echo "Hint: install 'highlight' to enable syntax highlighting" >&2 -} + if ((${+commands[highlight]})) + then + highlight -S zsh -i <(where "${(z)functions[@]}") + else + where "${(z)functions[@]}" + echo "Hint: install 'highlight' to enable syntax highlighting" >&2 + fi +) diff --git a/functions/_mf_define b/functions/_mf_define index cd71019..c285798 100644 --- a/functions/_mf_define +++ b/functions/_mf_define @@ -1,11 +1,25 @@ -[[ $EDITOR == *vim ]] && - local ftopts="-c 'set ft=zsh'" || -[[ $EDITOR == *micro ]] && +if [[ $EDITOR == *vim ]] +then + local ftopts="-c 'set ft=zsh'" +elif [[ $EDITOR == *micro ]] +then local ftopts="-filetype=zsh" -eval $EDITOR "$ftopts" "${MFUNCDIR}/$1" && - [[ -f "${MFUNCDIR}/$1" ]] && - autoload $1 || { - echo "aborting..." - return 1 - } -$edit || echo "new function '$1' created in $MFUNCDIR" +fi + +eval $EDITOR "$ftopts" "${MFUNCDIR}/$1" + +if ! [[ -f "${MFUNCDIR}/$1" ]] +then + echo "aborting..." + return 1 +else + $commands[${1}] && unfunction $1 # just in case + autoload $1 +fi + +if $edit +then + echo "updated function '${1}'" +else + echo "new function '${1}' created in $MFUNCDIR" +fi diff --git a/functions/lfunc b/functions/lfunc index 371fd9e..2a42c1a 100644 --- a/functions/lfunc +++ b/functions/lfunc @@ -1,5 +1,6 @@ # TODO: specific functions, wildcards -getopts "hv" opt && { +if getopts "hv" opt +then shift $((OPTIND -1)) case ${opt} in h ) @@ -11,5 +12,6 @@ getopts "hv" opt && { return ;; esac -} +fi + command ls "$MFUNCDIR" diff --git a/functions/mfunc b/functions/mfunc index 026f92d..3f09130 100644 --- a/functions/mfunc +++ b/functions/mfunc @@ -8,19 +8,22 @@ for i; do # TODO: input sanitization local edit=false - [[ -e $MFUNCDIR/$i ]] && { - read -q "choice?function $i already exists, press Y/y to edit with $EDITOR or any other key to abort: " || { + if [[ -e $MFUNCDIR/$i ]] + then + if ! read -q "choice?function $i already exists, press Y/y to edit with $EDITOR or any other key to abort: " + then echo - echo "aborting..." return - } + fi echo unfunction $i # forget the old version first edit=true - } + fi + _mf_define $i || return done + echo -n "function" (($# == 1)) && - echo -n " is" ||echo -n "s are" + echo -n " is" || echo -n "s are" echo " now available" diff --git a/functions/rfunc b/functions/rfunc index 1eab0cc..d5bcdc1 100644 --- a/functions/rfunc +++ b/functions/rfunc @@ -1,26 +1,30 @@ # demand argument # TODO: interactive mode -(($# == 0)) && { +if (($# == 0)) +then echo "please name at least one function to delete" return 1 -} +fi # TODO: autocompletion/wildcards local count=0 -for i; do - [[ -f $MFUNCDIR/$i ]] || { +for i +do + if not [[ -f $MFUNCDIR/$i ]] + then echo "function $i not found" return 2 - } + fi + + autoload +X $i rm -iv $MFUNCDIR/$i - [[ -f $MFUNCDIR/$i ]] || count+=1 + [[ -f $MFUNCDIR/$i ]] || count+=1 done ((count == 0)) && return echo -n "function" -((count > 1)) && - echo -n "s are" || - echo -n " is" +((count == 1)) && + echo -n " is" || echo -n "s are" echo " still available until next login and can be viewed with the 'where' builtin" diff --git a/mfunc.plugin.zsh b/mfunc.plugin.zsh index 6fd3a24..5826672 100755 --- a/mfunc.plugin.zsh +++ b/mfunc.plugin.zsh @@ -7,18 +7,22 @@ ################## # this is where our functions live -[[ -v MFUNCDIR ]] || - MFUNCDIR="${HOME}/.functions" && - [[ -v ZDOTDIR ]] && +if ! [[ -v MFUNCDIR ]] +then + if [[ -v ZDOTDIR ]] + then MFUNCDIR="${ZDOTDIR}/functions" - + else + MFUNCDIR="${HOME}/.functions" + fi +fi # check if functions directory exists, create if it doesn't -[[ -d "${MFUNCDIR}/" ]] || { - mkdir "${MFUNCDIR}/" && - echo "mfunc init: functions directory created in $MFUNCDIR" || - return -} +if ! [[ -d "${MFUNCDIR}/" ]] +then + mkdir -p "${MFUNCDIR}/" + echo "mfunc init: functions directory created in $MFUNCDIR" +fi # check if fpath contains MFUNC_FUNCTIONS_D, add it if it doesn't MFUNC_FUNCTIONS_D="${0:h}/functions" From 14852317984f1c9197570020cda4d549d04422dc Mon Sep 17 00:00:00 2001 From: Konstantin <78656278+amogus07@users.noreply.github.com> Date: Sun, 22 Feb 2026 14:51:46 +0100 Subject: [PATCH 18/20] improve formatting --- functions/_lf_verbose | 4 ++-- functions/_mf_define | 18 +++++++----------- functions/lfunc | 17 ++++++++--------- functions/mfunc | 10 ++++------ functions/rfunc | 23 ++++++++++++----------- 5 files changed, 33 insertions(+), 39 deletions(-) diff --git a/functions/_lf_verbose b/functions/_lf_verbose index c5bb7c3..415ff14 100755 --- a/functions/_lf_verbose +++ b/functions/_lf_verbose @@ -10,10 +10,10 @@ then fi # get a list of functions managed by mfunc -local functions=( $MFUNCDIR/* ) +local functions_array=( "$MFUNCDIR"/* ) # strip the paths -functions="${functions[@]:t}" +local functions="${functions_array[@]:t}" ( # load functions diff --git a/functions/_mf_define b/functions/_mf_define index c285798..bc77ba6 100644 --- a/functions/_mf_define +++ b/functions/_mf_define @@ -1,24 +1,20 @@ -if [[ $EDITOR == *vim ]] -then +if [[ $EDITOR == *vim ]]; then local ftopts="-c 'set ft=zsh'" -elif [[ $EDITOR == *micro ]] -then +elif [[ $EDITOR == *micro ]]; then local ftopts="-filetype=zsh" fi -eval $EDITOR "$ftopts" "${MFUNCDIR}/$1" +eval "$EDITOR" "$ftopts" "${MFUNCDIR}/$1" -if ! [[ -f "${MFUNCDIR}/$1" ]] -then +if ! [[ -f "${MFUNCDIR}/$1" ]]; then echo "aborting..." return 1 else - $commands[${1}] && unfunction $1 # just in case - autoload $1 + ${commands[${1}]} && unfunction "$1" # just in case + autoload "$1" fi -if $edit -then +if "$edit"; then echo "updated function '${1}'" else echo "new function '${1}' created in $MFUNCDIR" diff --git a/functions/lfunc b/functions/lfunc index 2a42c1a..c9dbd71 100644 --- a/functions/lfunc +++ b/functions/lfunc @@ -1,15 +1,14 @@ # TODO: specific functions, wildcards -if getopts "hv" opt -then - shift $((OPTIND -1)) +if getopts "hv" opt; then + shift $((OPTIND - 1)) case ${opt} in - h ) - _lf_help - return + h) + _lf_help + return ;; - v ) - _lf_verbose - return + v) + _lf_verbose + return ;; esac fi diff --git a/functions/mfunc b/functions/mfunc index 3f09130..b242d53 100644 --- a/functions/mfunc +++ b/functions/mfunc @@ -8,19 +8,17 @@ for i; do # TODO: input sanitization local edit=false - if [[ -e $MFUNCDIR/$i ]] - then - if ! read -q "choice?function $i already exists, press Y/y to edit with $EDITOR or any other key to abort: " - then + if [[ -e $MFUNCDIR/$i ]]; then + if ! read -rq "choice?function $i already exists, press Y/y to edit with $EDITOR or any other key to abort: "; then echo return fi echo - unfunction $i # forget the old version first + unfunction "$i" # forget the old version first edit=true fi - _mf_define $i || return + _mf_define "$i" || return done echo -n "function" diff --git a/functions/rfunc b/functions/rfunc index d5bcdc1..afa659f 100644 --- a/functions/rfunc +++ b/functions/rfunc @@ -1,23 +1,21 @@ # demand argument # TODO: interactive mode -if (($# == 0)) -then +if (($# == 0)); then echo "please name at least one function to delete" return 1 fi # TODO: autocompletion/wildcards -local count=0 -for i -do - if not [[ -f $MFUNCDIR/$i ]] - then +local count +typeset -i count=0 +for i; do + if not [[ -f "$MFUNCDIR/$i" ]]; then echo "function $i not found" return 2 fi - autoload +X $i - rm -iv $MFUNCDIR/$i + autoload +X "$i" + rm -iv "$MFUNCDIR/$i" [[ -f $MFUNCDIR/$i ]] || count+=1 done @@ -25,6 +23,9 @@ done return echo -n "function" -((count == 1)) && - echo -n " is" || echo -n "s are" +if ((count == 1)); then + echo -n " is" +else + echo -n "s are" +fi echo " still available until next login and can be viewed with the 'where' builtin" From 046acad783a3a679395da7b97a05913cea65b3ea Mon Sep 17 00:00:00 2001 From: Konstantin <78656278+amogus07@users.noreply.github.com> Date: Sun, 22 Feb 2026 15:10:41 +0100 Subject: [PATCH 19/20] fix invalid syntax --- functions/rfunc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/rfunc b/functions/rfunc index afa659f..52f6105 100644 --- a/functions/rfunc +++ b/functions/rfunc @@ -9,7 +9,7 @@ fi local count typeset -i count=0 for i; do - if not [[ -f "$MFUNCDIR/$i" ]]; then + if ! [[ -f "$MFUNCDIR/$i" ]]; then echo "function $i not found" return 2 fi From df365f33268dd7b9bacc1d7757cda7730fa34843 Mon Sep 17 00:00:00 2001 From: Konstantin <78656278+amogus07@users.noreply.github.com> Date: Sun, 22 Feb 2026 15:11:04 +0100 Subject: [PATCH 20/20] properly check if the function is defined --- functions/_mf_define | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/_mf_define b/functions/_mf_define index bc77ba6..42b32e5 100644 --- a/functions/_mf_define +++ b/functions/_mf_define @@ -10,7 +10,7 @@ if ! [[ -f "${MFUNCDIR}/$1" ]]; then echo "aborting..." return 1 else - ${commands[${1}]} && unfunction "$1" # just in case + [[ ${functions[${1}]} ]] && unfunction "$1" # just in case autoload "$1" fi