Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
222 lines (200 sloc) 6.15 KB
# Apparix - bookmarks for the command line with comprehensive tab completion.
#
# BASH and ZSH functions
#
# This file should work for modern bourne-style shells, not including the
# bourne shell. Name this file for example .bourne-apparix in your $HOME
# directory, and put the line 'source $HOME/.bourne-apparix' (without quotes)
# in the file $HOME/.bashrc or $HOME/.bash_login if you use bash, in the file
# $HOME/.zshrc if you use zsh. If you use zsh, you may need to additionally
# put the lines
#
# autoload -Uz compinit
# compinit
#
# (without outcommenting them as above) *BEFORE* the line where you source
# $HOME/.bourne-apparix. This is not the case for example if you use
# oh-my-zsh.
#
# If you use the relevant functions, make sure $EDITOR is set to the name of
# an available editor.
#
# I find it useful to have this alias:
# alias a=apparix
# as I use it in command substitution, e.g.
# echo cp myfile $(a bm)
# cp myfile $(a bm)
#
# This is a big decision from a Huffman point of view.
# If you want to remove it, go to all the places in the lines below
# where the name Huffman is mentioned and remove the relevant part.
#
# Thanks to Sitaram Chamarty for all the important parts of the bash completion
# code, and thanks to Izaak van Dongen for figuring out the zsh completion code,
# and subsequently improving and standardising the bash completion code.
#
function toot () {
if [[ 3 == $# ]]; then
file="$(apparix -favour rOl "$1" "$2")/$3"
elif [[ 2 == $# ]]; then
file="$(apparix -favour rOl "$1")/$2"
else
echo "toot tag dir file OR toot tag file"
return
fi
if [[ $? == 0 ]]; then
$EDITOR $file
fi
}
function todo () {
toot $@ TODO
}
function ald () {
if [[ 2 == $# ]]; then
loc=$(apparix -favour rOl "$1" "$2")
elif [[ 1 == $# ]]; then
loc=$(apparix --try-current-first -favour rOl "$1")
fi
if [[ $? == 0 ]]; then
ls -d "$loc"
fi
}
function als () {
if [[ 2 == $# ]]; then
loc=$(apparix -favour rOl "$1" "$2")
elif [[ 1 == $# ]]; then
loc=$(apparix --try-current-first -favour rOl "$1")
fi
if [[ $? == 0 ]]; then
ls "$loc"
fi
}
function ae () {
if [[ 2 == $# ]]; then
loc=$(apparix -favour rOl "$1" "$2")
elif [[ 1 == $# ]]; then
loc=$(apparix --try-current-first -favour rOl "$1")
fi
if [[ $? == 0 ]]; then
$EDITOR "$loc"
fi
}
function whence () {
if [[ 2 == $# ]]; then
loc=$(apparix -pick $2 "$1")
elif [[ 1 == $# ]]; then
loc=$(apparix "$1")
else
loc=$HOME
fi
if [[ $? == 0 ]]; then
cd "$loc"
fi
}
function bm () {
if [[ 2 == $# ]]; then
apparix --add-mark "$1" "$2";
elif [[ 1 == $# ]]; then
apparix --add-mark "$1";
else
apparix --add-mark;
fi
}
function portal () {
if [[ 1 == $# ]]; then
apparix --add-portal "$1";
else
apparix --add-portal;
fi
}
function to () {
# local IFS=$'\n'
if [[ 2 == $# ]]; then
loc=$(apparix --try-current-last -favour rOl "$1" "$2")
elif [[ 1 == $# ]]; then
if [[ "$1" == '-' ]]; then
loc="-"
else
loc=$(apparix --try-current-last -favour rOl "$1")
fi
else
loc=$HOME
fi
if [[ $? == 0 ]]; then
cd "$loc"
fi
}
if [[ -n $BASH_VERSION ]]; then
# function to complete sensibly on filenames and directories
# https://stackoverflow.com/questions/12933362/getting-compgen-to-include-slashes-on-directories-when-looking-for-files
function _my_compgen {
local cur="$1"
# Files, excluding directories:
comm -3 <(compgen -f -- "$cur" | sort) <(compgen -d -- "$cur" | sort) # | sed -e 's/$/ /'
# Directories (add -S / for slash separator):
compgen -d -- "$cur"
}
# function completing a file, used by _apparix_comp
function _apparix_comp_file {
local caller="$1"
local cur_file="$2"
# local IFS=$'\n'
case $caller in
# complete on directories. this is easy with compgen.
to|als|ald)
# Directories (add -S / for slash separator):
compgen -d -- "$cur_file"
;;
# complete on filenames. this is a little harder to do nicely.
a|ae|apparix) # Huffman (remove a|)
_my_compgen "$cur_file"
;;
*)
echo "please register this function in ~/.bash_apparix:_apparix_dirs" 1>&2
;;
esac
}
# function to complete an apparix tag followed by a file inside that tag's
# directory
function _apparix_comp {
local tag="${COMP_WORDS[1]}"
local IFS=$'\n'
COMPREPLY=()
if [[ $COMP_CWORD == 1 ]]; then
local tags=( $(cut -f2 -d, $HOME/.apparix{rc,expand}) )
COMPREPLY=( $(compgen -W "${tags[*]}" -- "$tag") )
else
local cur_file="${COMP_WORDS[2]}"
local app_dir=$(apparix --try-current-last -favour rOl $tag 2>/dev/null)
if [[ -d $app_dir ]]; then
# run in subshell so cd isn't permanent
COMPREPLY=( $(cd $app_dir && _apparix_comp_file $1 $cur_file) )
else
COMPREPLY=()
fi
fi
if (( ${#COMPREPLY[@]} > 0 )); then
# The line below makes all know cases with spaces in directory names work.
COMPREPLY=($(printf "%q\n" "${COMPREPLY[@]}"))
fi
return 0
}
# register completions
complete -o nospace -F _apparix_comp a to als ald ae apparix # Huffman (remove a)
elif [[ -n $ZSH_VERSION ]]; then
function _apparix_file {
# local IFS=$'\n'
_arguments '1:mark:_values "\n" $(cut -d, -f2 $HOME/.apparix{rc,expand})' \
'2:file:_path_files -W "$(apparix --try-current-last -favour rOl $words[2] 2>/dev/null)"'
}
function _apparix_directory {
# local IFS=$'\n'
_arguments '1:mark:_values "\n" $(cut -d, -f2 $HOME/.apparix{rc,expand})' \
'2:file:_path_files -/W "$(apparix --try-current-last -favour rOl $words[2] 2>/dev/null)"'
}
compdef _apparix_file ae apparix a # Huffman (remove a)
compdef _apparix_directory to ald als
fi
export APPARIXLOG=$HOME/.apparixlog
alias a='apparix -favour O' # Huffman (remove entire line)
alias via='vi $HOME/.apparixrc'