Source

tmsu / misc / zsh / _tmsu

Full commit
#compdef tmsu

# Zsh completion script for tmsu. Copy this file to
# your Zsh function path, e.g. /usr/share/zsh/site-functions/_tmsu.

local context curcontext="$curcontext" state state_descr line
typeset -A opt_args

_tmsu() {
	local cmd ret=1

	_arguments -C \
	    {--verbose,-v}'[show verbose messages]' \
	    {--version,-V}'[show version information and exit]' \
	    {--database,-d+}'[use the specified database]:database_path:_files' \
	    {--help,-h}'[show help and exit]' \
		'1: :_tmsu_commands' \
		'*::arg:->args' \
		&& ret=0

	case $state in
		(args)
			cmd="$words[1]"
			if (( $+functions[_tmsu_cmd_${cmd}] ))
			then
				_tmsu_cmd_$cmd
			fi
		;;
	esac
}

_tmsu_commands() {
	typeset -a command_list
	local line

	_call_program tmsu tmsu help --list | \
	while read -A line
	do
		command_list+=($line[1])
	done

	_describe -t commands 'command' command_list "$@"
}

_tmsu_tags() {
	typeset -a tag_list
	local tag

	_call_program tmsu tmsu tags --all | while read -A tag
	do
		tag_list+=$tag[1]:gs/:/\\:/
	done

	_describe -t tags 'tags' tag_list
}

_tmsu_query() {
	typeset -a tag_list
	local tag

	_call_program tmsu tmsu tags --all | while read -A tag
	do
		tag_list+=$tag[1]:gs/:/\\:/
	done
	tag_list+='and'
	tag_list+='or'
	tag_list+='not'

	_describe -t tags 'tags' tag_list
}

# commands

_tmsu_cmd_copy() {
    _arguments -s -w '1:tag:_tmsu_tags' && ret=0
}

_tmsu_cmd_delete() {
	_arguments -s -w '*:tag:_tmsu_tags' && ret=0
}

_tmsu_cmd_dupes() {
	_arguments -s -w ''{--recursive,-r}'[recursively check directory contents]' \
	                 '*:file:_files' \
	&& ret=0
}

_tmsu_cmd_files() {
	_arguments -s -w ''{--all,-a}'[list the complete set of tagged files]' \
	                 ''{--directory,-d}'[list only items that are directories]' \
                     ''{--file,-f}'[list only items that are files]' \
                     ''{--top,-t}'[list only top-most matching items (excludes the contents of matching direcotries)]' \
                     ''{--leaf,-l}'[list only the bottom-most (leaf) items]' \
                     ''{--recursive,-r}'[read all files on the file-system under each matching directory, recursively]' \
                     ''{--count,-c}'[lists the number of files rather than their names]' \
	                 '*:tag:_tmsu_query' \
	&& ret=0
}

_tmsu_cmd_help() {
	_arguments -s -w ''{--list,-l}'[list commands]' \
	                 '1:command:_tmsu_commands' \
	&& ret=0
}

_tmsu_cmd_imply() {
    _arguments -s -w ''{--delete,-d}'[deletes the tag implication]' \
                     ''{--list,-l}'[lists the tag implications]' \
                     '*:tag:_tmsu_tags' \
    && ret=0
}

_tmsu_cmd_merge() {
	_arguments -s -w '*:tag:_tmsu_tags' && ret=0
}

_tmsu_cmd_mount() {
    _arguments -s -w ''{--allow-other,-o}'[allow other users access to the VFS (requires root or setting in fuse.conf)]' \
                     '1:file:_files' \
	                 '2:mountpoint:_dirs' \
	&& ret=0
}

_tmsu_cmd_rename() {
	_arguments -s -w '1:tag:_tmsu_tags' && ret=0
}

_tmsu_cmd_repair() {
	_arguments -s -w ''{--force,-f}'[remove missing files from the database]' \
	                 ''{--pretend,-p}'[do not make any changes]' \
	                 '*:file:_files' \
    && ret=0
}

_tmsu_cmd_stats() {
    # no arguments
}

_tmsu_cmd_status() {
	_arguments -s -w ''{--directory,-d}'[list directory entries only: do not list contents]' \
	                 '*:file:_files' \
	&& ret=0
}

_tmsu_cmd_tag() {
	_arguments -s -w ''{--tags,-t}'[apply set of tags to multiple files]' \
	                 ''{--recursive,-r}'[apply tags recursively to contents of directories]' \
	                 ''{--from+,-f}'[copy tags from the specified file]:source:_files' \
	                 '*:: :->items' \
	&& ret=0

	case $state in
		(items)
			if (( ${+opt_args[--tags]} ))
			then
				if (( CURRENT == 1 ))
				then
					local expl
					_wanted tags expl 'tags' _tmsu_tags
				else
					local expl
					_wanted files expl 'files' _files
				fi
			else
				if (( CURRENT == 1 ))
				then
					local expl
					_wanted files expl 'files' _files
				else
					local expl
					_wanted tags expl 'tags' _tmsu_tags
				fi
			fi
		;;
	esac
}

_tmsu_cmd_tags() {
	_arguments -s -w ''{--all,-a}'[show all tags]' \
	                 ''{--count,-c}'[lists the tag count]' \
	                 ''{--count,-c}'[lists the number of tags rather than their names]' \
	                 '*:file:_files' \
	&& ret=0
}

_tmsu_cmd_unmount() {
	_arguments -s -w ''{--all,-a}'[unmount all]' \
	                 '1:mountpoint:_files' \
	&& ret=0
}

_tmsu_cmd_untag() {
	_arguments -s -w ''{--all,-a}'[remove all tags]' \
	                 ''{--tags,-t}'[remove set of tags from multiple files]' \
	                 ''{--recursive,-r}'[remove tags recursively from contents of directories]' \
	                 '*:: :->items' \
	&& ret=0

	case $state in
		(items)
			if (( ${+opt_args[--tags]} ))
			then
				if (( CURRENT == 1 ))
				then
					local expl
					_wanted tags expl 'tags' _tmsu_tags
				else
					local expl
					_wanted files expl 'files' _files
				fi
			elif (( ${+opt_args[--all]} ))
			then
				local expl
				_wanted files expl 'files' _files
			else
				if (( CURRENT == 1 ))
				then
					local expl
					_wanted files expl 'files' _files
				else
					local expl
					_wanted tags expl 'tags' _tmsu_tags
				fi
			fi
		;;
	esac
}

_tmsu_cmd_version() {
	# no arguments
}

_tmsu "$@"