[zsh] Move func/ directory to zsh/functions

This commit is contained in:
Eryn Wells 2026-01-18 08:43:02 -07:00
parent 91da2fc583
commit 7c08c5131f
53 changed files with 1 additions and 1 deletions

View file

@ -0,0 +1,39 @@
# Eryn Wells <eryn@erynwells.me>
# vim:ft=zsh:
append_to_path() {
local should_export_path=1
local should_be_verbose=0
while getopts "ve" opt; do
case $opt in
"e") should_export_path=1;;
"+e") should_export_path=0;;
"v") should_be_verbose=1;;
*) ;;
esac
done
local result=1
local path_to_add=$@[$OPTIND]
if [[ -d "$path_to_add" ]]; then
if (( $should_be_verbose )); then
echo "Appending $path_to_add to \$path"
fi
path+="$path_to_add"
else
if (( $should_be_verbose )); then
echo "$path_to_add doesn't exist"
fi
result=0
fi
if (( $should_export_path )); then
export path
fi
return $result
}
append_to_path "$@"

View file

@ -0,0 +1,8 @@
# Eryn Wells <eryn@erynwells.me>
binary_exists() {
hash $1 1>/dev/null 2>&1
return $?
}
binary_exists "$@"

31
zsh/functions/bool Normal file
View file

@ -0,0 +1,31 @@
# Eryn Wells <eryn@erynwells.me>
# vim: set ft=zsh:
function bool {
if [[ "$1" =~ '^-?[0-9]+$' ]]; then
if (( $1 == 0 )); then
echo "no"
return 1
else
echo "yes"
return 0
fi
fi
local lowercase_value=${(L)1}
if [[ "$lowercase_value" == "yes" || "$lowercase_value" == "true" ]]; then
echo "yes"
return 0
fi
if [[ "$lowercase_value" == "no" || "$lowercase_value" == "false" ]]; then
echo "no"
return 1
fi
echo "no"
return 1
}
bool "$@"

View file

@ -0,0 +1,17 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function connect_ssh_agent
{
# Start SSH agent for password-less logins
if [[ -z "$SSH_AUTH_SOCK" && -x "$SSHAGENT" ]]; then
if ! pgrep -u $USER ssh-agent 1>/dev/null; then
eval $(ssh-agent -s > ~/.ssh_agent_vars)
trap "kill $SSH_AGENT_PID" 0
else
eval $(cat ~/.ssh_agent_vars)
fi
fi
}
connect_ssh_agent "$@"

View file

@ -0,0 +1,9 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function darwin-icloud-drive-path
{
echo "$HOME/Library/Mobile Documents/com~apple~CloudDocs"
}
darwin-icloud-drive-path "$@"

View file

@ -0,0 +1,11 @@
# Eryn Wells <eryn@erynwells.me>
darwin-hardware-model() {
if [[ -z "$darwin_hardware_model" ]]; then
darwin_hardware_model=`sysctl -n hw.model`
fi
echo "$darwin_hardware_model"
}
darwin-hardware-model "$@"

View file

@ -0,0 +1,11 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
darwin-os-build() {
if [[ -z "$darwin_os_build" ]]; then
darwin_os_build=`sysctl -n kern.osversion`
fi
echo "$darwin_os_build"
}
darwin-os-build "$@"

View file

@ -0,0 +1,11 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
darwin-os-version() {
if [[ -z "$darwin_os_version" ]]; then
darwin_os_version=`sysctl -n kern.osproductversion`
fi
echo "$darwin_os_version"
}
darwin-os-version "$@"

View file

@ -0,0 +1,21 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload darwin_icloud_drive_path
icloud=`darwin_icloud_drive_path`
if [[ ! -d "$icloud" ]]; then
return 1
fi
# Put screenshots in iCloud Drive, in a directory according to hostname
local name=`hostname -s | tr A-Z a-z | tr ' ' -`
local screenshots_dir="$icloud/Screenshots/$name"
mkdir -p "$screenshots_dir"
echo "$screenshots_dir"
defaults write com.apple.screencapture screenshots_diration "$screenshots_dir"
killall Dock
return 0

View file

@ -0,0 +1,22 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload darwin_icloud_drive_path
autoload darwin_configure_screenshots_directory
autoload init_xcode
function darwin_init_once
{
# Dim dock icons of apps that have been hidden.
defaults write com.apple.Dock showhidden -boolean yes
killall Dock
darwin_configure_screenshots_directory
# See https://techstuffer.com/this-app-is-damaged-error-macos-sierra/
sudo spctl --master-disable
init_xcode
}
darwin_init_once "$@"

View file

@ -0,0 +1,8 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
for func in ${(P)${1}}; do
if autoload +X -Uz $func &> /dev/null; then
$func
fi
done

13
zsh/functions/finder Normal file
View file

@ -0,0 +1,13 @@
# vim: set ft=zsh:
# Open a file or folder in Finder.
function finder
{
if [[ -z "$1" ]]; then
open -R .
fi
open -R "$1"
}
finder "$@"

16
zsh/functions/g Normal file
View file

@ -0,0 +1,16 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function g {
if [[ $# -gt 0 ]]; then
git $@
else
git status --short --branch
fi
return $?
}
# Use git completion for the g function
compdef g='git'
g "$@"

View file

@ -0,0 +1,19 @@
#!/usr/bin/env zsh
autoload binary_exists
function homebrew-prefix
{
if [[ -e "$SYSTEM_PARAMETERS_FILE" ]] && binary_exists jq; then
jq .parameters.homebrew_prefix.value "$SYSTEM_PARAMETERS_FILE"
return
fi
if ! binary_exists brew; then
return
fi
brew --prefix
}
homebrew-prefix "$@"

View file

@ -0,0 +1,45 @@
# vim:ft=zsh:
#
# Import the CAcert root CA on OS X. Based on the tutorial on their wiki.
#
# http://wiki.cacert.org/MacOSX_X509Anchors
#
# Eryn Wells <eryn@erynwells.me>
function import_cacert
{
# SHA1 fingerprints of the root and class3 keys.
local root_fingerprint="13:5C:EC:36:F4:9C:B8:E9:3B:1A:B2:70:CD:80:88:46:76:CE:8F:33"
local class3_fingerprint="AD:7C:3F:64:FC:44:39:FE:F4:E9:0B:E8:F4:7C:6C:FA:8A:AD:FD:CE"
local savewd=`pwd`
local tmpdir=`mktemp -dt cacert`
cd "$tmpdir"
# Download the certificates.
curl -k -o root.crt "https://www.cacert.org/certs/root.crt"
curl -k -o class3.crt "https://www.cacert.org/certs/class3.crt"
# Verify fingerprints and import.
if openssl x509 -noout -fingerprint < root.crt \
| grep "Fingerprint=$root_fingerprint" 1>/dev/null
then
sudo security add-trusted-cert -d \
-k /Library/Keychains/System.keychain \
-r trustRoot \
root.crt
fi
if openssl x509 -noout -fingerprint < class3.crt \
| grep "Fingerprint=$class3_fingerprint" 1>/dev/null
then
sudo security add-trusted-cert -d \
-k /Library/Keychains/System.keychain \
-r trustAsRoot \
class3.crt
fi
cd "$savewd"
rm -rf "$tmpdir"
}

21
zsh/functions/init-env Normal file
View file

@ -0,0 +1,21 @@
# Eryn Wells <eryn@erynwells.me>
autoload -Uz binary_exists
function init-env
{
export PAGER=less
export GREP_OPTIONS="--color=auto"
export GREP_COLOR="1;32"
if binary_exists gpg2; then
# Make sure gpg2 knows what to do with the curses-based smartcard PIN prompt.
export GPG_TTY=`tty`
fi
# Some helpful aliases for scripting
alias local-array="local -a"
alias local-map="local -A"
}
init-env "$@"

View file

@ -0,0 +1,15 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init-env-darwin
{
local -r xcode_library="$HOME/Library/Developer/Xcode"
if [[ -d "$xcode_library" ]]; then
export XCODE_LIBRARY="$xcode_library"
export XCODE_DERIVED_DATA="$XCODE_LIBRARY/DerivedData"
export XCODE_INSTALLS="$XCODE_LIBRARY/Installs"
export dd="$XCODE_DERIVED_DATA"
fi
}
init-env-darwin "$@"

View file

@ -0,0 +1,34 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
# See https://specifications.freedesktop.org/basedir/latest/ for definitions of
# these paths.
function init-env-default-xdg-vars
{
zmodload zsh/zutil
local -a opt_create
zparseopts -a opt_args -D -E -F - \
{c,-create,-no-create}=opt_create
export \
XDG_BIN_HOME=${XDG_BIN_HOME:-$HOME/.local/bin} \
XDG_CACHE_HOME=${XDG_CACHE_HOME:-$HOME/.cache} \
XDG_CONFIG_HOME=${XDG_CACHE_HOME:-$HOME/.config} \
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share} \
XDG_STATE_HOME=${XDG_DATA_HOME:-$HOME/.local/state}
if [[ $opt_create[(I)--no-create] -ne 0 ]]; then
mkdir -p -m 0700 \
$XDG_BIN_HOME \
$XDG_CACHE_HOME \
$XDG_CONFIG_HOME \
$XDG_DATA_HOME \
$XDG_STATE_HOME
fi
}
init-env-default-xdg-vars "$@"

View file

@ -0,0 +1,29 @@
# Eryn Wells <eryn@erynwells.me>
autoload -Uz update-path
function init-env-path
{
path=()
update-path \
"$HOME/Website/scripts" \
"$HOME/bin" \
"$HOME/.local/bin" \
"$HOME/.local/lib/node_modules/bin" \
"$HOME/.cargo/bin" \
"$HOME/.ghcup/bin" \
"$HOME/.gem/ruby/2.2.0/bin" \
"$HOME/.vim/bundle/vim-tidal/bin" \
/opt/local/bin \
/usr/X11/bin \
/opt/brew/bin \
/opt/homebrew/bin \
/usr/local/bin \
/usr/bin \
/bin \
/usr/local/sbin \
/usr/sbin \
/sbin
}
init-env-path "$@"

View file

@ -0,0 +1,15 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload -Uz prepend_to_path
function init-env-playdate
{
local -r sdk_path="$HOME/Developer/PlaydateSDK"
if [[ -d "$sdk_date" ]]; then
export PLAYDATE_SDK_PATH="$sdk_path"
prepend_to_path "$PLAYDATE_SDK_PATH/bin"
fi
}
init-env-playdate "$@"

View file

@ -0,0 +1,19 @@
# Eryn Wells <eryn@erynwells.me>
autoload -Uz prepend_to_path
autoload -Uz update-path
function init-env-python
{
local -r user_python_root="$HOME/Library/Python"
if [[ -d "$pythonRoot" ]]; then
for f in $pythonRoot/*; do
prepend_to_path "$f/bin"
done
fi
export PYTHON_VIRTUAL_ENVS="$HOME/.local/share/python-virtual-environments"
update-path --prepend "$PYTHON_VIRTUAL_ENVS/eryn/bin"
}
init-env-python "$@"

View file

@ -0,0 +1,22 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init-env-tilde-paths
{
for candidate_code_path in \
"$HOME/Code" \
"$HOME/Developer" \
"$HOME/Documents/Code"
do
if [[ ! -d "$candidate_code_path" ]]; then
continue
fi
c="$candidate_code_path"
break
done
export df="$DOTFILES_HOME"
}
init-env-tilde-paths "$@"

20
zsh/functions/init-env-vi Normal file
View file

@ -0,0 +1,20 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init-env-vi
{
# Prefer nvim and vim, in that order, over standard vi, which is insufferable.
if whence -cp nvim &> /dev/null; then
alias vi=nvim
export EDITOR=nvim
elif whence -cp vim &> /dev/null; then
alias vi=vim
export EDITOR=vim
else
export EDITOR=vi
fi
export VISUAL=$EDITOR
}
init-env-vi "$@"

View file

@ -0,0 +1,37 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload binary_exists
function init-rc-aliases
{
alias j='jobs'
alias h='history'
alias df='df -h'
alias du='du -h'
alias e='emacs'
alias v='vi'
alias chux='chmod u+x'
alias chuw='chmod u+w'
alias chur='chmod u+r'
alias cho="chown $USER"
alias today='date +%Y-%m-%d'
alias addkey="ssh-agent ~/.ssh/id_rsa"
alias gp='g p origin $gitbranch'
alias gpf='g p -f origin $gitbranch'
alias bx='bundle exec'
binary_exists ledger && alias l='ledger'
binary_exists gpg2 && alias gpg='gpg2'
alias -s c='vim'
alias -s tex='vim'
alias -s txt='vim'
alias -s xml='vim'
alias -s jar='java -jar'
}
init-rc-aliases "$@"

View file

@ -0,0 +1,13 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload binary_exists
function init-rc-app-environments
{
# Default ledger file
local -r ledger_file="$HOME/Documents/Ledger/personal.ledger"
[[ -f "$ledger_file" ]] && export LEDGER_FILE="$ledger_file"
}
init-rc-app-environments "$@"

View file

@ -0,0 +1,46 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init-rc-completion
{
autoload -Uz compinit
compinit
# Cache completions
zstyle ':completion::complete:*' use-cache 1
zstyle ':completion::complete:*' cache-path ~/.zsh/cache
# Make ls show completion list in color.
# See also: https://github.com/ohmyzsh/ohmyzsh/issues/6060
if [[ -n "$LS_COLORS" ]]; then
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
else
zstyle ':completion:*:default' list-colors \
'di=34' 'ln=35' 'so=32' 'pi=33' 'ex=31' 'bd=34;46' 'cd=34;43' 'su=30;41' \
'sg=30;46' 'tw=30;42' 'ow=30;43'
fi
# For rm, cp, and mv don't complete if file is on the line already
zstyle ':completion:*:rm:*' ignore-line yes
zstyle ':completion:*:cp:*' ignore-line yes
zstyle ':completion:*:mv:*' ignore-line yes
# Remove trailing slashes in directory arguments
zstyle ':completion:*' squeeze-slashes true
# Never select parent directory
zstyle ':completion:*:cd:*' ignore-parents parent pwd
# Expand partial paths
zstyle ':completion:*' expand 'yes'
# Show a pretty menu of killable processes
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
zstyle ':completion:*:*:kill:*' menu yes select
# Complete man pages by section
zstyle ':completion:*:manuals' separate-sections true
zstyle ':completion:*:manuals.*' insert-sections true
}
init-rc-completion "$@"

View file

@ -0,0 +1,39 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload -Uz binary_exists
function init-rc-darwin
{
alias acls='command ls -le'
# These things might have been installed by Homebrew, and I like the GNU
# versions better.
binary_exists gdircolors && alias dircolors='gdircolors'
binary_exists gfind && alias find='gfind'
binary_exists gnuindent && alias indent='gnuindent'
binary_exists gls && init_rc_configure_ls `which gls`
# ldd doesn't exist on OS X, but otool -L does the same thing.
alias ldd='otool -L'
# From macOS's system zshrc.
# Disable the log builtin, so we don't conflict with /usr/bin/log
disable log
local sounds=/System/Library/Sounds
alias glass="afplay $sounds/Glass.aiff"
alias funk="afplay $sounds/Funk.aiff"
autoload -Uz darwin_init_once
autoload -Uz darwin-icloud-drive-path
autoload -Uz darwin_configure_screenshots_directory
autoload -Uz finder
alias -s app='open'
alias -s xcodeproj='open-xcode'
alias -s xcworkspace='open-xcode'
}
init-rc-darwin "$@"

View file

@ -0,0 +1,11 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init-rc-linux
{
alias iptls='sudo iptables --line-numbers -nv -L'
alias ip6tls='sudo ip6tables --line-numbers -nv -L'
alias rlx="xrdb $HOME/.Xdefaults"
}
init-rc-linux "$@"

27
zsh/functions/init-rc-ls Normal file
View file

@ -0,0 +1,27 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init-rc-ls
{
alias la="ls -A $ls_options"
alias ll="ls -l $ls_options"
alias l.="ls -d $ls_options .*"
# Enable ls colors
export CLICOLOR=1 COLORTERM=1
# Define colors for ls. See the LSCOLORS documentation in ls(1).
# The default is "exfxcxdxbxegedabagacadah".
export LSCOLORS=Exdxcxfxbxegedabagacadah
local dircolors_bin=$(whence -p dircolors || whence -p gdircolors)
if [[ -x "$dircolors_bin" ]]; then
if [[ -f "$HOME/.dircolors/$SYS.cfg" ]]; then
eval $dircolors_bin "$HOME/.dircolors/$SYS.cfg"
elif [[ -f "$HOME/.dircolors/default.cfg" ]]; then
eval $dircolors_bin "$HOME/.dircolors/default.cfg"
fi
fi
}
init-rc-ls "$@"

View file

@ -0,0 +1,29 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init-rc-prompt
{
local theme=loquacious
autoload -U add-zsh-hook
autoload -Uz vcs_info
zstyle ':vcs_info:*' disable p4 bzr cdv darcs mtn svk tla cvs svn
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:git:general:*' formats '%b'
zstyle ':vcs_info:git-svn:general:*' formats '%b'
# Export the current Git branch before every prompt.
function _export-gitbranch {
vcs_info general
export gitbranch=${vcs_info_msg_0_}
}
add-zsh-hook precmd _export-gitbranch
autoload -U promptinit
promptinit
prompt $theme
}
init-rc-prompt "$@"

17
zsh/functions/init-rc-zle Normal file
View file

@ -0,0 +1,17 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init-rc-zle
{
case "${ERYN_ZLE_MODE:-emacs}" in
vim)
bindkey -v
zle -A .backward-delete-char vi-backward-delete-char
;;
emacs)
bindkey -e
;;
esac
}
init-rc-zle "$@"

View file

@ -0,0 +1,21 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init-rc-zsh-history
{
setopt \
APPEND_HISTORY \
EXTENDED_HISTORY \
INC_APPEND_HISTORY \
HIST_FIND_NO_DUPS \
HIST_IGNORE_SPACE \
HIST_NO_STORE \
HIST_IGNORE_DUPS \
HIST_REDUCE_BLANKS
HISTSIZE=1000000
SAVEHIST=1000000
HISTFILE="$HOME/.zhistory"
}
init-rc-zsh-history "$@"

View file

@ -0,0 +1,16 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function init-rc-zsh-options
{
# Report seconds since shell was invoked in milliseconds
typeset -F SECONDS
# See zshoptions(1)
setopt EXTENDED_GLOB \
MULTIOS \
AUTO_REMOVE_SLASH \
COMPLETE_IN_WORD
}
init-rc-zsh-options "$@"

View file

@ -0,0 +1,7 @@
# Eryn Wells <eryn@erynwells.me>
init_profile_darwin() {
( ssh-add --apple-load-keychain & ) 1>/dev/null 2>&1
}
init_profile_darwin "$@"

View file

@ -0,0 +1,34 @@
# Eryn Wells <eryn@erynwells.me>
autoload -Uz binary_exists
autoload -Uz darwin-os-version
autoload -Uz darwin-os-build
autoload -Uz darwin-hardware-model
init_profile_darwin_say_hello() {
local cat_program=cat
if binary_exists lolcat; then
cat_program=lolcat
fi
local hello_message="It's `date +'%H:%M on %A, %B %d'`."
local hardware_model=`darwin-hardware-model`
if [[ -n "$hardware_model" ]]; then
hello_message+="\nThis machine is a $hardware_model."
fi
local os_version=`darwin-os-version`
if [[ -n "$os_version" ]]; then
local os_build=`darwin-os-build`
if [[ -n "$os_build" ]]; then
hello_message+="\nYou're running macOS $os_version ($os_build)."
else
hello_message+="\nYou're running macOS $os_version."
fi
fi
print "$hello_message\n" | $cat_program
}
init_profile_darwin_say_hello "$@"

View file

@ -0,0 +1,18 @@
# Eryn Wells <eryn@erynwells.me>
autoload binary_exists
autoload homebrew-prefix
function init_rc_fpath_darwin
{
if binary_exists brew; then
local brew_fpath="$(homebrew-prefix)/share/zsh/site-functions"
if [[ -d "$brew_fpath" ]]; then
fpath+=($brew_fpath)
fi
export FPATH
fi
}
init_rc_fpath_darwin "$@"

13
zsh/functions/init_xcode Normal file
View file

@ -0,0 +1,13 @@
# Eryn Wells <eryn@erynwells.me>
# vim: ft=zsh:
function init_xcode
{
if [[ -d "~df/Xcode/IDETemplateMacros.plist" ]]; then
echo "Copying IDETemplateMacros.plist"
mkdir -p ~/Library/Developer/Xcode/UserData
cp ~df/Xcode/IDETemplateMacros.plist ~/Library/Developer/Xcode/UserData
fi
}
init_xcode "%@"

View file

@ -0,0 +1,16 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload load_module
local myfpath="$HOME/.zsh/func"
for func in $myfpath/*; do
[[ ! -e "$func" || -d "$func" ]] && continue
local functionName=`basename $func`
[[ "$functionName" =~ "prompt_*" ]] && continue
[[ "$functionName" =~ "init_*" ]] && continue
autoload -Uz $functionName
done

View file

@ -0,0 +1,16 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function list_tmux_session
{
local tmux_out=`tmux list-sessions 2>/dev/null`
[[ -z "$tmux_out" || -n "$TMUX" ]] && return
echo "You have the following active tmux sessions:"
for session in ${(f)tmux_out}; do
echo " $session"
done
echo
}
list_tmux_session "$@"

39
zsh/functions/load_module Normal file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function load_module
{
local mod=$1
if [[ -z "$mod" ]]; then
print "Missing argument: path to module to load." >&2
return -1
fi
# Make sure $mod isn't already in $fpath
local modpath
for p in $fpath; do
modpath=$p/$mod
[[ -d $modpath ]] && break
modpath=''
done
if [[ -z "$modpath" ]]; then
return 1
fi
fpath+=($modpath)
for file in $modpath/*; do
if [[ -f "$file" ]]; then
autoload $func
fi
if [[ -d "$file" ]]; then
load_module "$file"
fi
done
return 0
}
load_module "$@"

View file

@ -0,0 +1,24 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
autoload -Uz binary_exists
neovim_init_once()
{
if binary_exists npm; then
npm install -g \
@tailwindcss/language-server \
eslint \
pyright \
typescript-language-server \
typescript \
vscode-langservers-extracted
else
print "Cannot find npm binary" >&2
return -1
fi
return 0
}
neovim_init_once "$@"

31
zsh/functions/nethack Normal file
View file

@ -0,0 +1,31 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function nethack
{
local remote
while getopts 'lr' opt; do
case $opt in
h) echo "Usage: $0 [-l][-r]";;
l) remote=0;;
r) remote=1;;
*)
echo "Invalid argument: $OPTARG" 1>&2
return -1
;;
esac
done
if (( $remote )); then
ssh nethack@alt.org
return $?
elif binary_exists nethack; then
command nethack "$@[$OPTIND,-1]"
return $?
else
ssh nethack@alt.org
return $?
fi
}
nethack "$@"

12
zsh/functions/open-xcode Normal file
View file

@ -0,0 +1,12 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
open-xcode() {
local selectedXcode=`xcode-select -p`
while [[ ! `basename $selectedXcode` =~ ".app$" ]]; do
selectedXcode=`dirname "$selectedXcode"`
done
open -a "$selectedXcode" $@
}
open-xcode "$@"

View file

@ -0,0 +1,38 @@
# Eryn Wells <eryn@erynwells.me>
prepend_to_path() {
local should_export_path=1
local should_be_verbose=0
while getopts "ve" opt; do
case $opt in
"e") should_export_path=1;;
"+e") should_export_path=0;;
"v") should_be_verbose=1;;
*) ;;
esac
done
local result=1
local path_to_add=$@[$OPTIND]
if [[ -d "$path_to_add" ]]; then
if (( $should_be_verbose )); then
echo "Prepending $path_to_add to \$path"
fi
path=("$path_to_add" $path)
else
if (( $should_be_verbose )); then
echo "$path_to_add doesn't exist"
fi
result=0
fi
if (( $should_export_path )); then
export path
fi
return $result
}
prepend_to_path "$@"

View file

@ -0,0 +1,38 @@
#!/usr/bin/zsh
# Return a prompt string with appropriate color escapes
# Eryn Wells <eryn@erynwells.me>
local bold=0
local foreground=''
local background=''
while getopts 'bf:k:' opt; do
case $opt in
b) bold=1;;
f) foreground=$OPTARG;;
k) background=$OPTARG;;
*) echo "Unknown option: $opt" 1>&2; return -1;;
esac
done
local str=$@[$OPTIND,${#@}]
autoload -U is-at-least
if is-at-least '4.3.7'; then
[[ -n "$foreground" ]] && str="%F{$foreground}$str%f"
[[ -n "$background" ]] && str="%K{$background}$str%k"
[[ $bold -eq 1 ]] && str="%B$str%b"
else
local fg_hash bg_hash
if [[ $bold -eq 1 ]]; then
fg_hash='fg_bold'
bg_hash='bg_bold'
else
fg_hash='fg_no_bold'
bg_hash='bg_no_bold'
fi
[[ -n "$foreground" ]] && str="%{\$${fg_hash}[$foreground]%}$str%{\$reset_color%}"
[[ -n "$background" ]] && str="%%{\$${bg_hash}[$background]%}$str%{\$reset_color%}"
fi
print $str

View file

@ -0,0 +1,175 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function prompt_loquacious_help
{
cat <<EOF
This prompt takes up two lines. At its most verbose, it looks like this:
<user> at <host> in <cwd> on <repo>(<branch>)
%
By default, <host> is only shown when the SSH_CONNECTION variable is set; in
other words, when this shell session was opened via an SSH connection. The
prompt will also show the current repository and branch using vcs_info if the
current directory is in a repo.
EOF
}
function prompt_loquacious_setup
{
typeset -ga precmd_functions
typeset -ga preexec_functions
autoload -U prompt_colorize
autoload -Uz vcs_info
autoload -U add-zsh-hook
add-zsh-hook chpwd set_repo_name
add-zsh-hook precmd set_xterm_title
add-zsh-hook precmd set_tmux_window_name
add-zsh-hook precmd print_newline
add-zsh-hook precmd set_prompt_info
add-zsh-hook preexec print_newline
add-zsh-hook preexec set_tmux_window_name
prompt_opts=(cr subst percent)
# Set up vcs_info
zstyle ':vcs_info:git:loquacious_chpwd:*' formats '%F{cyan}%r%f'
zstyle ':vcs_info:git-svn:loquacious_chpwd:*' formats '[%F{red}svn%f]%F{cyan}%r%f'
zstyle ':vcs_info:git:loquacious_precmd:*' formats '(%F{blue}%b%f)'
zstyle ':vcs_info:git-svn:loquacious_precmd:*' formats '(%F{blue}%b%f)'
PS1='$PS1_PLACE, $PS1_TIME $PS1_ZLE_MODE
$PS1_LINE'
zle -N zle-keymap-select on_keymap_select
}
function prompt_loquacious_preview
{
# TODO: Implement prompt preview.
}
#
# HELPER FUNCTIONS
#
# First prompt flag. See precmd_newline.
is_first_prompt=1
function print_newline
{
# Don't print newlines the first time the prompt is displayed.
if [[ -n $is_first_prompt ]]; then
unset is_first_prompt
[[ -z $SSH_CONNECTION ]] && return
fi
echo
}
function set_repo_name
{
vcs_info loquacious_chpwd
if [[ -n "${vcs_info_msg_0_}" ]]; then
PS1_REPO="on ${vcs_info_msg_0_}"
else
PS1_REPO=""
fi
}
function set_prompt_info
{
PS1_TIMESTAMP="%*"
PS1_STATUS="%(?..%F{red}[$?]%f)"
PS1_HISTORY="№%F{blue}%!%f"
PS1_TIME="$PS1_HISTORY$PS1_STATUS at $PS1_TIMESTAMP"
PS1_NAME="%F{magenta}%n%f"
PS1_HOST="%F{red}%m%f"
PS1_CWD="%F{green}%~%f"
PS1_PLACE="$PS1_NAME"
if [[ -n "$SSH_CONNECTION" && "$TERM_PROGRAM" != "tmux" ]]; then
PS1_PLACE="$PS1_PLACE at $PS1_HOST"
fi
PS1_PLACE="$PS1_PLACE in $PS1_CWD"
# Get git repo information, if it exists. See setup function for
# configuration details.
vcs_info loquacious_precmd
PS1_BRANCH="${vcs_info_msg_0_}"
if [[ -n "$vcs_info_msg_0_" ]]; then
PS1_PLACE="$PS1_PLACE $PS1_REPO$PS1_BRANCH"
fi
# Show background jobs, if any.
if [[ `jobs | wc -l` -ge 1 ]]; then
PS1_JOBS='%j'
fi
# Show background job count if any exist.
RPS1="%(1j.[%F{magenta}%j%f].)"
PS1_LINE='%(!.%F{red}.%F{default})%#%f '
}
function set_xterm_title
{
local tmux_session_name=''
local title='%n@%m'
if [[ -n "$TMUX" ]]; then
local tmux_session_name=`tmux display-message -p '#S'`
title+=" (${tmux_session_name})"
fi
# Set xterm and screen titles
if [[ -n "$DISPLAY" || -n "$TERM_PROGRAM" ]]; then
print -Pn "\e]2;${title}\a"
fi
# For Apple Terminal.app, add a link to the current directory.
if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
print -Pn "\e]7;file://%d\a"
fi
}
function set_tmux_window_name
{
if [[ -z "$TMUX" ]]; then
return
fi
local process="${${(z)1}[1]}"
if [[ -n "$process" ]]; then
tmux rename-window "$process"
else
tmux rename-window "`print -Pn "%2~"`"
fi
}
function set_zle_mode_info
{
if [[ -z "$1" || "$1" == 'viins' || "$1" == 'main' ]]; then
PS1_ZLE_MODE="%F{black}<INS>%f"
elif [[ "$1" == 'vicmd' ]]; then
PS1_ZLE_MODE="%F{black}<CMD>%f"
else
PS1_ZLE_MODE=''
fi
}
function on_keymap_select
{
# Regenerate the prompt with the new mode
set_zle_mode_info $KEYMAP
# Redraw the prompt
zle reset-prompt
}
# Finally, run setup to get everything going
prompt_loquacious_setup "$@"

View file

@ -0,0 +1,34 @@
#!/usr/bin/env zsh
. $HOME/.shell-functions
TAGS_DIR=$HOME/.tags
function refresh_system_tags
{
if [[ "$SYS" == "darwin" ]]; then
print_info "Refreshing Apple frameworks in /System/Library/Frameworks"
find /System/Library/Frameworks -maxdepth 2 -name Headers \
-exec ctags -Ra -f "$TAGS_DIR/apple_frameworks.tags" {} \;
print_info "Refreshing 3rd party frameworks in /Library/Frameworks"
find /Library/Frameworks -maxdepth 2 -name Headers \
-exec ctags -Ra -f "$TAGS_DIR/3rdparty_frameworks.tags" {} \;
fi
# Generic UNIX system include directory
if [[ -d /usr/include ]]; then
print_info "Refreshing system includes in /usr/include"
ctags -Ra -f "$TAGS_DIR/usr.tags" /usr/include
fi
# Generic UNIX local system include directory
if [[ -d /usr/local/include ]]; then
print_info "Refreshing local system includes in /usr/local/include"
ctags -Ra -f "$TAGS_DIR/usr_local.tags" /usr/local/include
fi
}
refresh_system_tags

View file

@ -0,0 +1,88 @@
#!/usr/bin/env zsh
#
# Set up for CPython development.
#
# Eryn Wells <eryn@erynwells.me>
function _usage-setup-cpython
{
cat <<EOF
Usage: $1 [-h] [-r root]
Setup for CPython development.
Arguments:
-h Show this help.
-r root Use the given directory as the root of the CPython source tree.
EOF
}
function setup-cpython
{
root="$HOME/Code/cpython"
coverage="$HOME/Code/coveragepy"
while getopts 'hr:' opt; do
case $opt in
(h)
_usage-setup-cpython "$0"
return 0
;;
(r)
root="$OPTARG"
;;
(*)
echo "Invalid argument: $OPTARG" 1>&2
return -1
;;
esac
done
if [[ ! -d "$root" ]]; then
print_error "Invalid source root: $root"
return 1
fi
export coverage
export lib="$root/Lib"
export root
function cov
{
local cmd
case "$1" in
(html)
local module="$root/Lib/$2"
if [[ ! -d "$module" ]]; then
if [[ -f "$module.py" ]]; then
module="$module.py"
fi
fi
cmd="html -i --include=$module"
;;
(report)
cmd="report"
;;
(run)
cmd="run --pylib --source=$2 Lib/test/regrtest.py test_$2"
;;
esac
(cd "$root"; ./python.exe "$coverage" "$cmd")
}
unsetup_functions=(unsetup-cpython)
}
function unsetup-cpython
{
unset coverage
unset lib
unset root
unfunction cov
}
setup-cpython "$@"

View file

@ -0,0 +1,15 @@
#!/usr/bin/zsh
#
# Setup the shell for Android development
#
# Eryn Wells <eryn@erynwells.me>
function setup_android
{
export ANDROID_ROOT="$HOME/toolchain/android/sdk-r22.0.4"
path=("$ANDROID_ROOT/platform-tools" "$ANDROID_ROOT/tools" $path)
rehash
}
setup_android $@

55
zsh/functions/solarized Normal file
View file

@ -0,0 +1,55 @@
#!/usr/bin/env zsh
# Eryn Wells <eryn@erynwells.me>
function solarized
{
cat <<EOF
SOLARIZED HEX 16/8 TERMCOL XTERM/HEX L*A*B RGB HSB
--------- ------- ---- ------- ----------- ---------- ----------- -----------
base03 #002b36 8/4 brblack 234 #1c1c1c 15 -12 -12 0 43 54 193 100 21
base02 #073642 0/4 black 235 #262626 20 -12 -12 7 54 66 192 90 26
base01 #586e75 10/7 brgreen 240 #585858 45 -07 -07 88 110 117 194 25 46
base00 #657b83 11/7 bryellow 241 #626262 50 -07 -07 101 123 131 195 23 51
base0 #839496 12/6 brblue 244 #808080 60 -06 -03 131 148 150 186 13 59
base1 #93a1a1 14/4 brcyan 245 #8a8a8a 65 -05 -02 147 161 161 180 9 63
base2 #eee8d5 7/7 white 254 #e4e4e4 92 -00 10 238 232 213 44 11 93
base3 #fdf6e3 15/7 brwhite 230 #ffffd7 97 00 10 253 246 227 44 10 99
yellow #b58900 3/3 yellow 136 #af8700 60 10 65 181 137 0 45 100 71
orange #cb4b16 9/3 brred 166 #d75f00 50 50 55 203 75 22 18 89 80
red #dc322f 1/1 red 160 #d70000 50 65 45 220 50 47 1 79 86
magenta #d33682 5/5 magenta 125 #af005f 50 65 -05 211 54 130 331 74 83
violet #6c71c4 13/5 brmagenta 61 #5f5faf 50 15 -45 108 113 196 237 45 77
blue #268bd2 4/4 blue 33 #0087ff 55 -10 -45 38 139 210 205 82 82
cyan #2aa198 6/6 cyan 37 #00afaf 60 -35 -05 42 161 152 175 74 63
green #859900 2/2 green 64 #5f8700 60 -20 65 133 153 0 68 100 60
EOF
if [[ "$1" == "dark" || "$2" == "dark" ]]; then
echo
echo "COLOR USAGE"
echo "-------- ------------------------------------------"
echo "base3 unused"
echo "base2 unused"
echo "base1 optional emphasized content"
echo "base0 body text / default code / primary content"
echo "base00 unused"
echo "base01 comments / secondary content"
echo "base02 background highlights"
echo "base03 background"
elif [[ "$1" == "light" || "$2" == "light" ]]; then
echo
echo "COLOR USAGE"
echo "-------- ------------------------------------------"
echo "base03 unused"
echo "base02 unused"
echo "base01 optional emphasized content"
echo "base00 body text / default code / primary content"
echo "base0 unused"
echo "base1 comments / secondary content"
echo "base2 background highlights"
echo "base3 background"
fi
}
solarized "$@"

15
zsh/functions/up Normal file
View file

@ -0,0 +1,15 @@
#!/usr/bin/zsh
# Go up $1 directories, where $1 is an integer (saves me from having to type ../
# ad nauseum)
# Eryn Wells <eryn@erynwells.me>
if [[ -z $1 ]]; then
cd ..
else
local updir=''
for (( i=0; $i < $1; i++ ))
do
updir="../$updir"
done
cd $updir
fi

147
zsh/functions/update-path Normal file
View file

@ -0,0 +1,147 @@
# Eryn Wells <eryn@erynwells.me>
# vim: set ft=zsh:
function _update-path-usage
{
local -r function_name=$1
local usage_string=$(cat <<EOF
Usage: $function_name <arguments> [<path>...]
Modify the target path-like variable by adding or removing paths provided as
positional arguments. Before adding, check that the target path variable doesn't
already contain the path and that the directory exists.
This function returns the number of paths that were skipped in the input. If all
paths were processed, it returns 0. It returns 255 if an error occurred while
processing arguments.
Arguments
---------
%B-e%b | %B--export%b | %B--no-export%b
Export the variable after modification. The default is to export if the
variable is modified.
%B-f%b | %B--force%b
Unconditionally add the path, even if it doesn't exist.
%B-h%b | %B--help%b
Print this message.
%B-p%b | %B--prepend%b
Prepend paths to the target path variable, instead of appending. When
prepending, directories will be added in reverse order so their precedence
is preserved. That is, the first directory in the list will be at the start
of the path.
%B-P%b | %B--path%b <path_variable_name>
The name of a path-like variable to modify. The default is 'path'.
%B-r%b | %B--remove%b
Remove paths instead of adding. -p and -f are ignored when removing
directories from the path.
%B-v%b | %B--verbose%b
Print extra information while processing directories.
EOF
)
print -P $usage_string
}
function update-path
{
zmodload zsh/zutil
local -a o_args o_export o_force o_help o_prepend o_remove o_verbose o_pathvar
zparseopts -a o_args -D -E -F - \
{e,-export,-no-export}=o_export \
{f,-force}=o_force \
{h,-help}=o_help \
{p,-prepend}=o_prepend \
{P,-path}:=o_pathvar \
{r,-remove}=o_remove \
{v,-verbose}=o_verbose
local -ri parse_status=$?
if (( parse_status )); then
_update-path-usage $0
return 255
fi
if [[ $#o_help -ne 0 ]]; then
_update-path-usage $0
return 0
fi
local -r verbose=$#o_verbose
local -r path_variable_name=${o_pathvar[-1]:-path}
if ! typeset -p $path_variable_name &> /dev/null; then
print "Invalid path variable: \$$path_variable_name" 1>&2
return 255
fi
[[ $verbose -ne 0 ]] && print "Modifying $path_variable_name"
local -i candidates_skipped=0
local did_update_path
local -a candidates=($@)
if [[ $#o_prepend -ne 0 ]]; then
# If prepending, reverse the list so paths are added in the right order.
candidates=(${(Oa)@})
fi
for candidate in $candidates; do
local candidate_index=${${(P)path_variable_name}[(Ie)$candidate]}
# An empty $o_remove means we're adding to the array.
if [[ $#o_remove -eq 0 ]]; then
if [[ $candidate_index -ne 0 ]]; then
[[ $verbose -ne 0 ]] && print "Skipping $candidate"
(( candidates_skipped++ ))
continue
fi
if [[ $#force -eq 0 && ! -d "$candidate" ]]; then
[[ $verbose -ne 0 ]] && print "$candidate doesn't exist"
(( candidates_skipped++ ))
continue
fi
did_update_path=yes
[[ $verbose -ne 0 ]] && print "Adding $candidate"
if [[ $#o_prepend -ne 0 ]]; then
eval "${path_variable_name}=(${candidate} ${path})"
else
eval "${path_variable_name}+=${candidate}"
fi
else
if [[ $candidate_index -eq 0 ]]; then
[[ $verbose -ne 0 ]] && print "Skipping $candidate"
(( candidates_skipped++ ))
continue
fi
[[ $verbose -ne 0 ]] && print "Removing $candidate"
did_update_path=yes
eval "${path_variable_name}[${candidate_index}]=()"
fi
done
if [[ -n $did_update_path && $o_export[(I)--no-export] -ne 0 ]]; then
[[ $verbose -ne 0 ]] && print "Exporting $path_variable_name"
export $path_variable_name
if [[ "$path_variable_name" == "path" ]]; then
rehash
fi
fi
return $candidates_skipped
}
update-path "$@"