Skip to content

Instantly share code, notes, and snippets.

@pavoljuhas
Last active August 29, 2015 13:57
Show Gist options
  • Save pavoljuhas/9414386 to your computer and use it in GitHub Desktop.
Save pavoljuhas/9414386 to your computer and use it in GitHub Desktop.
watch source files and re-run ctags whenever they change.
#!/bin/zsh -f
########################################################################
# setup:
setopt extendedglob
: ${WATCHPERIOD:=10}
: ${IDLETIMEOUT:=5400}
ignored=( '*?.o' '*?.obj' '*?.old' '*?.pro' '*?.*~' '*.bak' '*.pyc'
'*/#CVS/*' '*/#AAPDIR/*' )
ipat="(${(j:|:)ignored})"
watchFiles=( ${^${*:#${~ipat}}}(N-.) )
if [[ $# == 0 || ${argv[(I)-h|--help]} -gt 0 ]]; then
echo "usage: $0:t file1 file2 ..."
exit
fi
watchDirs=()
if [[ ${argv[(I)-R|--recurse]} -gt 0 ]]; then
if [[ $# == 1 ]]; then
argv=( $argv . )
fi
watchDirs=( ${^*}(N-/) )
fi
TAGS=tags
optidx_o=${argv[(I)-[fo]]}
if (( 0 < optidx_o && optidx_o < $# )); then
TAGS=${argv[optidx_o+1]}
fi
watchWithInotify() {
local events
local inotify_opts
events=( modify move create delete delete_self )
inotify_opts=( --quiet --recursive
--timeout=$IDLETIMEOUT --event=${^events} )
inotifywait $inotify_opts \
$watchDirs $watchFiles $TAGS >/dev/null || exit ${?/2/0}
sleep 1
}
watchTediously() {
local tagsfileisrecent
tagsfileisrecent=( ${TAGS}(Nms-${IDLETIMEOUT}) )
[[ -n ${tagsfileisrecent} ]] || exit
sleep $WATCHPERIOD
}
WatchAndWait=watchTediously
if [[ 1 == ${+commands[inotifywait]} ]]; then
WatchAndWait=watchWithInotify
fi
set -- "${@:#${~ipat}}"
ctags "$@" || exit $?
while true; do
changed_files=(
${^watchFiles}(Ne:'[[ $REPLY -nt '$TAGS' ]]':)
${^watchDirs}/**/*(N-.e:'[[ $REPLY -nt '$TAGS' ]]':)
)
changed_files=( ${changed_files:#${~ipat}} )
if [[ ! -f $TAGS || ${#changed_files} != 0 ]]; then
ctags "$@"
fi
$WatchAndWait
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment