From c00f5dce6ec963e46f56f86a4e6f51214358b36b Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 19 Mar 2022 04:34:14 +0100 Subject: [PATCH] Zsh: Make functions also usable as shell scripts --- .config/zsh/functions/cat-highlight | 41 ++++++++++++------------- .config/zsh/functions/export-emacs | 27 ++++++++-------- .config/zsh/functions/gdb_get_backtrace | 31 +++++++++---------- .config/zsh/functions/mksmol | 27 ++++++++-------- 4 files changed, 57 insertions(+), 69 deletions(-) diff --git a/.config/zsh/functions/cat-highlight b/.config/zsh/functions/cat-highlight index 2bd550c..e950e7d 100644 --- a/.config/zsh/functions/cat-highlight +++ b/.config/zsh/functions/cat-highlight @@ -1,25 +1,22 @@ -# -*- mode: shell-script; -*- - +#!/usr/bin/env zsh # Highlight cat output with whatever less is using for highlighting. The command # in $LESSOPEN needs to be able to work with multiple files. -function cat-highlight() -{ - # Check if we are in a terminal and $LESSOPEN is not empty - if [[ -t 1 && -n "${LESSOPEN}" ]]; then - local -a args - local -a files - for arg in "${@}"; do - # Filter out file names - if [[ "${arg[1]}" != "-" ]]; then - files+="${arg}" - else - args+="${arg}" - fi - done - local catopen="${LESSOPEN/|/}" - eval "${catopen/\%s/${files}} | \cat ${args}" - else - \cat "${@}" - fi -} +# Check if we are in a terminal and $LESSOPEN is not empty +if [[ -t 1 && -n "${LESSOPEN}" ]]; then + local -a args + local -a files + for arg in "${@}"; do + # Filter out file names + if [[ "${arg[1]}" != "-" ]]; then + files+="${arg}" + else + args+="${arg}" + fi + done + + local catopen="${LESSOPEN/|/}" + eval "${catopen/\%s/${files}} | \cat ${args}" +else + \cat "${@}" +fi diff --git a/.config/zsh/functions/export-emacs b/.config/zsh/functions/export-emacs index d6c159d..dfe1834 100644 --- a/.config/zsh/functions/export-emacs +++ b/.config/zsh/functions/export-emacs @@ -1,18 +1,15 @@ -# -*- mode: shell-script; -*- - +#!/usr/bin/env zsh # Export variable to Emacs. -function export-emacs -{ - if [[ ARGC -eq 0 ]]; then - print -u 2 "Usage: ${0} …" - return 1 - fi - # Fail if we can't communicate with the daemon - [[ "$(emacsclient -e t)" == 't' ]] || return 1 +if [[ ARGC -eq 0 ]]; then + print -u 2 "Usage: ${0} …" + return 1 +fi - for name in "${@}"; do - value=$(eval print \"\$${name}\") - emacsclient -e "(setenv \"${name}\" \"${value}\")" >/dev/null - done -} +# Fail if we can't communicate with the daemon +[[ "$(emacsclient -e t)" == 't' ]] || return 1 + +for name in "${@}"; do + value=$(eval print \"\$${name}\") + emacsclient -e "(setenv \"${name}\" \"${value}\")" >/dev/null +done diff --git a/.config/zsh/functions/gdb_get_backtrace b/.config/zsh/functions/gdb_get_backtrace index 4f733bb..a22dd59 100644 --- a/.config/zsh/functions/gdb_get_backtrace +++ b/.config/zsh/functions/gdb_get_backtrace @@ -1,19 +1,16 @@ -# -*- mode: shell-script; -*- - +#!/usr/bin/env zsh # Get backtrace from gdb -function gdb_get_backtrace() -{ - if [[ ${ARGC} -ne 2 ]]; then - print -u 2 "Usage: ${0} " - return 1 - fi - local exe="${1}" - local core="${2}" - gdb "${exe}" \ - --core "${core}" \ - --batch \ - --quiet \ - -ex "thread apply all bt full" \ - -ex "quit" -} +if [[ ${ARGC} -ne 2 ]]; then + print -u 2 "Usage: ${0} " + return 1 +fi +local exe="${1}" +local core="${2}" + +gdb "${exe}" \ + --core "${core}" \ + --batch \ + --quiet \ + -ex "thread apply all bt full" \ + -ex "quit" diff --git a/.config/zsh/functions/mksmol b/.config/zsh/functions/mksmol index 14f5939..31b8063 100644 --- a/.config/zsh/functions/mksmol +++ b/.config/zsh/functions/mksmol @@ -1,18 +1,15 @@ -# -*- mode: shell-script; -*- - +#!/usr/bin/env zsh # Makes all files in lower case. Works with Non-ASCII characters. # Equivalent to `zmv "${dir}/(*)" '${dir}/${1:l}'` -function mksmol() -{ - if [[ ${ARGC} -ne 1 ]]; then - print -u 2 "Usage: ${0} " >&2 - return 1 - fi - local dir="${1}" - for file in "${dir}"/*; do - basename="${file##*/}" - newfile="${dir}/${basename:l}" - [[ "${file}" != "${newfile}" ]] && mv "${file}" "${newfile}" || : - done -} +if [[ ${ARGC} -ne 1 ]]; then + print -u 2 "Usage: ${0} " >&2 + return 1 +fi +local dir="${1}" + +for file in "${dir}"/*; do + basename="${file##*/}" + newfile="${dir}/${basename:l}" + [[ "${file}" != "${newfile}" ]] && mv "${file}" "${newfile}" || : +done