diff --git a/autoload_completions.zsh b/autoload_completions.zsh index 3ad5930d..182810d5 100755 --- a/autoload_completions.zsh +++ b/autoload_completions.zsh @@ -3,6 +3,10 @@ autoload_completions() { autoload bashcompinit bashcompinit autoload -Uz compinit + # autocompletions + for i in $(ls $DOTFILES/completions); do + autoload $i + done compinit } diff --git a/completions/_src b/completions/_dfe similarity index 93% rename from completions/_src rename to completions/_dfe index abb7b456..2882ce84 100755 --- a/completions/_src +++ b/completions/_dfe @@ -1,4 +1,4 @@ -#compdef rc src +#compdef dfe dfs dfpath="$(wd path df)" out=() diff --git a/completions/_psrc b/completions/_dfp similarity index 93% rename from completions/_psrc rename to completions/_dfp index 57068e1f..996cede0 100755 --- a/completions/_psrc +++ b/completions/_dfp @@ -1,4 +1,4 @@ -#compdef prc sprc +#compdef dfpe dfps dfpath="$(wd path df)" out=() diff --git a/plugins/dotfiles_edit.plugin.zsh b/plugins/dotfiles_edit.plugin.zsh new file mode 100644 index 00000000..adefe409 --- /dev/null +++ b/plugins/dotfiles_edit.plugin.zsh @@ -0,0 +1,147 @@ +#!/usr/bin/env zsh + +# edit a dotfile script and source if there were changes +# supports autocomplete for any editable files +dfe() { + if [[ $# -eq 0 ]]; then + echo "Usage: df [-n] [-q] " + echo "Edit a dotfile script and source if there were changes" + echo "For .zsh files, the extension is optional" + echo " -n: do not source the file after editing" + echo " -q: do not print any messages" + return 1 + fi + no_src=0 + quiet=0 + + while [[ $# -gt 1 ]]; do + case $1 in + -n) + no_src=1 + ;; + -q) + quiet=1 + ;; + esac + shift + done + + if [[ -f "$DOTFILES/$1.sh" ]]; then + file="$DOTFILES/$1.sh" + elif [[ -f "$DOTFILES/$1.zsh" ]]; then + file="$DOTFILES/$1.zsh" + else + file="$DOTFILES/$1" + fi + + if [[ -f $file ]]; then + hash=$(md5 $file) + echo "Opening $(strip-home $file)..." + nvim $file + newhash=$(md5 $file) + + if [[ $? -eq 0 && $hash != $newhash ]]; then + if [[ $no_src -ne 1 ]]; then + if [[ $quiet -ne 1 ]]; then + src $1 + else + src -q $1 + fi + fi + else + echo "No changes made" + return 2 + fi + return 0 + fi + echo_red "File not found: $(strip-home $file)" + return 1 +} + +# source a dotfile script +# supports autocomplete for any editable files +dfs() { + if [[ $# -eq 0 ]]; then + echo "Usage: dfs [-q] " + echo "Source a dotfile script" + echo " -q: do not print any messages" + return 1 + fi + + while [[ $# -gt 1 ]]; do + case $1 in + -q) + quiet=1 + ;; + esac + shift + done + + if [[ -f "$DOTFILES/$1.sh" ]]; then + file="$DOTFILES/$1.sh" + elif [[ -f "$DOTFILES/$1.zsh" ]]; then + file="$DOTFILES/$1.zsh" + else + file="$DOTFILES/$1" + fi + + if [[ -f $file ]]; then + if [[ $quiet -ne 1 ]]; then + echo "Reloading $(strip-home $file)..." + fi + source "$file" + return 0 + fi + echo_red "File not found: $(strip-home $file)" + return 1 +} + +# same as rc, but for plugin files +dfpe() { + local flags=() + + # Parse arguments + while [[ "$#" -gt 1 ]]; do + flags+=("$1") + shift + done + + local file="$1" + # Ensure a file argument was passed + if [[ -z $file ]]; then + echo "Usage: dfp [-n] [-q] " + echo "Edit a dotfile plugin script and source if there were changes" + echo "For .zsh files, the extension is optional" + echo " -n: do not source the file after editing" + echo " -q: do not print any messages" + return 1 + fi + + # Call dfe with flags and transformed file argument + dfe "${flags[@]}" "plugins/$file.plugin" + return $? +} + +# same as src, but for plugin files +dfps() { + local flags=() + + # Parse arguments + while [[ "$#" -gt 1 ]]; do + flags+=("$1") + shift + done + + local file="$1" + # Ensure a file argument was passed + if [[ -z $file ]]; then + echo "Usage: dfps [-q] " + echo "Source a dotfile plugin script" + echo " -q: do not print any messages" + return 1 + fi + + # Call dfs with flags and transformed file argument + dfs "${flags[@]}" "plugins/$file.plugin" + return $? +} diff --git a/plugins/functions.plugin.zsh b/plugins/functions.plugin.zsh index 0da722e4..19450763 100755 --- a/plugins/functions.plugin.zsh +++ b/plugins/functions.plugin.zsh @@ -78,108 +78,6 @@ is_linux() { return $? } -# edit a dotfile script and source if there were changes -# supports autocomplete for any editable files -rc() { - if [[ $# -eq 0 ]]; then - echo_red "Usage: rc [-n] [-q] " - return 1 - fi - no_src=0 - quiet=0 - - while [[ $# -gt 1 ]]; do - case $1 in - -n) - no_src=1 - ;; - -q) - quiet=1 - ;; - esac - shift - done - - if [[ -f "$DOTFILES/$1.sh" ]]; then - file="$DOTFILES/$1.sh" - elif [[ -f "$DOTFILES/$1.zsh" ]]; then - file="$DOTFILES/$1.zsh" - else - file="$DOTFILES/$1" - fi - - if [[ -f $file ]]; then - hash=$(md5 $file) - echo "Opening $file..." - nvim $file - newhash=$(md5 $file) - - if [[ $? -eq 0 && $hash != $newhash ]]; then - if [[ $no_src -ne 1 ]]; then - if [[ $quiet -ne 1 ]]; then - src $1 - else - src -q $1 - fi - fi - else - echo "No changes made" - return 2 - fi - return 0 - fi - echo_red "File not found: $file" - return 1 -} - -# source a dotfile script -# supports autocomplete for any editable files -src() { - if [[ $# -eq 0 ]]; then - echo_red "Usage: src [-q] " - return 1 - fi - - while [[ $# -gt 1 ]]; do - case $1 in - -q) - quiet=1 - ;; - esac - shift - done - - if [[ -f "$DOTFILES/$1.sh" ]]; then - file="$DOTFILES/$1.sh" - elif [[ -f "$DOTFILES/$1.zsh" ]]; then - file="$DOTFILES/$1.zsh" - else - file="$DOTFILES/$1" - fi - - if [[ -f $file ]]; then - if [[ $quiet -ne 1 ]]; then - echo "Reloading $file..." - fi - source "$file" - return 0 - fi - echo_red "File not found: $file" - return 1 -} - -# same as rc, but for plugin files -prc() { - rc "plugins/$1.plugin" - return $? -} - -# same as src, but for plugin files -sprc() { - src "plugins/$1.plugin" - return $? -} - # select random number between min and max rand() { if [[ $# -eq 0 ]]; then @@ -196,10 +94,11 @@ rand() { echo $(($RANDOM % ($max - $min + 1) + $min)) } -# select random element from list +# select random line from file randline() { if [[ $# -eq 0 ]]; then echo_red "Usage: randline " + echo_red "Select a random line from a file" return 1 fi linenum=$(($RANDOM % $(wc -l <$1) + 1)) @@ -219,25 +118,7 @@ find-replace() { sed "s/$find/$replace/g" $file } -# find $1 and replace with $2 in file $3, output to file -find-replace-file() { - if [[ $# -lt 3 || $1 == '-h' ]]; then - echo_red "Find and replace text in file. Modifies the file." - echo_red "Usage: find-replace-file [file]..." - return 1 - fi - - files=( "${@:3}" ) - find=$1 - replace=$2 - - for file in $files; do - echo "Replacing $find with $replace in $file..." - out=$(find-replace $find $replace $file) - echo $out >$file - done -} - +# runs all scripts in directory $1 in order # same as run-parts from debian, but for osx if is_mac; then run-parts() { @@ -265,7 +146,8 @@ fi # search for a file in a directory search-file() { if [[ $# -eq 0 ]]; then - echo "Usage: find-file [dir] " + echo "Usage: search-file [dir] " + echo "Search for a file in a directory (recursively)" return 1 fi if [[ $# -eq 1 ]]; then @@ -284,6 +166,7 @@ search-file() { find-up() { if [[ $# -eq 0 ]]; then echo "Usage: find-up " + echo "Finds a file in the current directory or on one of its ancestors" return 1 fi file=$1 @@ -362,13 +245,6 @@ docker-volume-cd() { cd $(docker-volume-path "$image") } -# autocompletions -autoload _docker-exec -autoload _docker-volume-path -autoload _prj -autoload _src -autoload _psrc - # reload entire shell reload-zsh() { source $HOME/.zshrc