From fdfa8a5f8ac365c917684aefe6d8f4527125926e Mon Sep 17 00:00:00 2001 From: druckdev <63563978+druckdev@users.noreply.github.com> Date: Mon, 21 Sep 2020 18:06:55 +0200 Subject: [PATCH] Cleanup zsh functions and add unbkp() Add unbkp() that reverses bkp(). Use shorter zsh syntax for disowning a background job. Add usage string to mangrep. Expect clang-format config to be in XDG_CONFIG_HOME. Add functionality of comments. Use `command` where we want a command to be taken as external. --- .config/zsh/plugins/functionsPost.zsh | 35 ++++++++++++++++++--------- .config/zsh/plugins/functionsPre.zsh | 8 +++--- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/.config/zsh/plugins/functionsPost.zsh b/.config/zsh/plugins/functionsPost.zsh index c83fbde..48d19c9 100644 --- a/.config/zsh/plugins/functionsPost.zsh +++ b/.config/zsh/plugins/functionsPost.zsh @@ -9,7 +9,13 @@ function cl() { ## Copy file and append .bkp extension function bkp() { for file in "$@"; do - cp -i "$file" "$file.bkp" + command cp -i "$file" "$file.bkp" + done +} + +unbkp() { + for file in "$@"; do + command cp -i "$file" "${file%.bkp}" done } @@ -18,7 +24,7 @@ function launch() { # eval "$@" ## does not work with special characters? launch_command="$1" shift - $launch_command "$@" &>/dev/null & disown + $launch_command "$@" &>/dev/null &| } ## Compares two pdfs based on the result of pdftotext @@ -287,12 +293,14 @@ function resolve() { ## Grep a keyword at the beginning of a line (ignoring whitespace) in a man page function mangrep() { - mangrep_file="$1" - mangrep_pattern="$2" - shift - shift - man -P 'less -p "^ *'"${mangrep_pattern}\"" "$@" "${mangrep_file}" - unset mangrep_{file,pattern} + if [ $# -lt 2 ]; then + printf "usage: mangrep []\n" >&2 + printf "example: mangrep bash \"(declare|typeset)\"\n" >&2 + return 1 + fi + local page="$1" pattern="$2" + shift 2 + man -P "less -p \"^ *${pattern}\"" "$@" "${file}" } ## Grep in zsh history file @@ -300,10 +308,15 @@ function histgrep() { grep "$@" "${HISTFILE:-$HOME/.zsh_history}" } -function format() { +function cformat() { # TODO: respect manual changes made in meld - CLANG_FORMAT_FILE="$HOME/Projects/C/.clang.format" - FORMAT="{$(sed -E '/^\s*$/d' "$CLANG_FORMAT_FILE" | tr '\n' ',' | sed 's/,$//')}" + CLANG_FORMAT_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/clang/config" + FORMAT="$( + sed -E '/^\s*($|#)/d' "$CLANG_FORMAT_FILE" \ + | tr '\n' ',' \ + | sed 's/,$//' + )" + FORMAT="{${FORMAT}}" if [ $# -eq 1 ]; then meld <(clang-format -style="$FORMAT" $1) $1 fi diff --git a/.config/zsh/plugins/functionsPre.zsh b/.config/zsh/plugins/functionsPre.zsh index 6c38638..626d652 100644 --- a/.config/zsh/plugins/functionsPre.zsh +++ b/.config/zsh/plugins/functionsPre.zsh @@ -4,18 +4,18 @@ ## List items in trash if no argument is specified function _trash_list_default() { if [ $# -eq 0 ]; then - trash-list + command trash-list else - \trash "$@" + command trash "$@" fi } ## Open nemo in current directory if no argument is specified function _nemo_wd_default() { if [ $# -eq 0 ]; then - \nemo ./ + command nemo ./ else - \nemo "$@" + command nemo "$@" fi }