1
0
Fork 0

Zsh: Make functions also usable as shell scripts

This commit is contained in:
tastytea 2022-03-19 04:34:14 +01:00
parent 7cc2df5318
commit c00f5dce6e
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
4 changed files with 57 additions and 69 deletions

View File

@ -1,25 +1,22 @@
# -*- mode: shell-script; -*- #!/usr/bin/env zsh
# Highlight cat output with whatever less is using for highlighting. The command # Highlight cat output with whatever less is using for highlighting. The command
# in $LESSOPEN needs to be able to work with multiple files. # 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/|/}" # Check if we are in a terminal and $LESSOPEN is not empty
eval "${catopen/\%s/${files}} | \cat ${args}" if [[ -t 1 && -n "${LESSOPEN}" ]]; then
else local -a args
\cat "${@}" local -a files
fi 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

View File

@ -1,18 +1,15 @@
# -*- mode: shell-script; -*- #!/usr/bin/env zsh
# Export variable to Emacs. # Export variable to Emacs.
function export-emacs
{
if [[ ARGC -eq 0 ]]; then
print -u 2 "Usage: ${0} <variable> …"
return 1
fi
# Fail if we can't communicate with the daemon if [[ ARGC -eq 0 ]]; then
[[ "$(emacsclient -e t)" == 't' ]] || return 1 print -u 2 "Usage: ${0} <variable> …"
return 1
fi
for name in "${@}"; do # Fail if we can't communicate with the daemon
value=$(eval print \"\$${name}\") [[ "$(emacsclient -e t)" == 't' ]] || return 1
emacsclient -e "(setenv \"${name}\" \"${value}\")" >/dev/null
done for name in "${@}"; do
} value=$(eval print \"\$${name}\")
emacsclient -e "(setenv \"${name}\" \"${value}\")" >/dev/null
done

View File

@ -1,19 +1,16 @@
# -*- mode: shell-script; -*- #!/usr/bin/env zsh
# Get backtrace from gdb # Get backtrace from gdb
function gdb_get_backtrace()
{
if [[ ${ARGC} -ne 2 ]]; then
print -u 2 "Usage: ${0} <executable> <core dump>"
return 1
fi
local exe="${1}"
local core="${2}"
gdb "${exe}" \ if [[ ${ARGC} -ne 2 ]]; then
--core "${core}" \ print -u 2 "Usage: ${0} <executable> <core dump>"
--batch \ return 1
--quiet \ fi
-ex "thread apply all bt full" \ local exe="${1}"
-ex "quit" local core="${2}"
}
gdb "${exe}" \
--core "${core}" \
--batch \
--quiet \
-ex "thread apply all bt full" \
-ex "quit"

View File

@ -1,18 +1,15 @@
# -*- mode: shell-script; -*- #!/usr/bin/env zsh
# Makes all files in <directory> lower case. Works with Non-ASCII characters. # Makes all files in <directory> lower case. Works with Non-ASCII characters.
# Equivalent to `zmv "${dir}/(*)" '${dir}/${1:l}'` # Equivalent to `zmv "${dir}/(*)" '${dir}/${1:l}'`
function mksmol()
{
if [[ ${ARGC} -ne 1 ]]; then
print -u 2 "Usage: ${0} <directory>" >&2
return 1
fi
local dir="${1}"
for file in "${dir}"/*; do if [[ ${ARGC} -ne 1 ]]; then
basename="${file##*/}" print -u 2 "Usage: ${0} <directory>" >&2
newfile="${dir}/${basename:l}" return 1
[[ "${file}" != "${newfile}" ]] && mv "${file}" "${newfile}" || : fi
done local dir="${1}"
}
for file in "${dir}"/*; do
basename="${file##*/}"
newfile="${dir}/${basename:l}"
[[ "${file}" != "${newfile}" ]] && mv "${file}" "${newfile}" || :
done