Source

tmsu / misc / zsh / _tmsu

#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

local db

_tmsu() {
	local cmd db ret=1
	integer i=2

    # store which database to use
    while (( i < $#words ))
    do
        if [[ $words[$i] == --database=* ]];
        then
            db="$words[$i]"
        fi

        if [[ $words[$i] == -D ]];
        then
            db="--database=$words[$i+1]"
        fi

        (( i++ ))
    done

	_arguments -C \
	    {--verbose,-v}'[show verbose messages]' \
	    {--version,-V}'[show version information and exit]' \
	    {--database=,-D}'[use the specified database]:file:_files' \
	    {--help,-h}'[show help and exit]' \
		': :_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_tag_values() {
    typeset -a value_list
    local line

    local tagName=${PREFIX%%=*}
    _call_program tmsu tmsu $db values $tagName 2>/dev/null | \
    while read -A line
    do
        value_list+=$tagName=$line[1]
    done

    _describe -t values 'values' value_list
}

_tmsu_tags() {
    typeset -a tag_list
    local tag

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

    _describe -t tags 'tags' tag_list
}

_tmsu_values() {
    typeset -a value_list
    local value

    _call_program tmsu tmsu $db values --all | \
    while read -A value
    do
        value_list+=$value[1]:gs/:/\\:/
    done

    _describe -t values 'values' value_list
}

_tmsu_tags_with_values() {
    if [[ -prefix *= ]] 
    then
        _tmsu_tag_values
    else
        _tmsu_tags
    fi
}

_tmsu_query() {
    if [[ ! $words[$#words-1] =~ '--' && $words[$#words-1] =~ '=|!=|<|>|<=|>=' ]]
    then
        typeset -a value_list
        local line

        local tagName=$words[$#words-2]
        _call_program tmsu tmsu $db values $tagName | \
        while read -A line
        do
            value_list+=$line[1]
        done

        _describe -t values 'values' value_list
    else
        _tmsu_tags

        typeset -a operator_list

        operator_list+='and'
        operator_list+='or'
        operator_list+='not'
        operator_list+='='
        operator_list+='\!='
        operator_list+='\<'
        operator_list+='\>'
        operator_list+='\<='
        operator_list+='\>='
        operator_list+='eq'
        operator_list+='ne'
        operator_list+='lt'
        operator_list+='gt'
        operator_list+='ge'
        operator_list+='le'

        _describe -t operators 'operators' operator_list
    fi
}

_tmsu_file_tags() {
    file=$words[1]

    if [[ $file = "" ]]
    then
        _tmsu_tags_with_values
    else
        typeset -a tag_list
        local tag

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

        _describe -t tags 'tags' tag_list
    fi
}

# commands

_tmsu_cmd_copy() {
    _arguments -s -w ':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]' \
                     ''{--count,-c}'[lists the number of files rather than their names]' \
                     ''{--path=,-p}'[list only items under PATH]':path:_files \
                     ''{--untagged,-u}'[combined with --path, lists untagged files under PATH]' \
                     ''{--explicit,-e}'[list only explicitly tagged files]' \
	                 '*: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 ''{--options=,-o}'[mount options (passed to fusermount)]' \
                     ':file:_files' \
	                 ':mountpoint:_dirs' \
	&& ret=0
}

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

_tmsu_cmd_repair() {
	_arguments -s -w ''{--remove,-R}'[remove missing files from the database]' \
	                 ''{--unmodified,-u}'[recalculate fingerprints for unmodified files]' \
	                 ''{--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]' \
	                 ''{--untagged,-u}'[list untagged entries only]' \
	                 '*:file:_files' \
	&& ret=0
}

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

	case $state in
        (items)
            if (( ${+opt_args[--tags]} || ${+opt_args[-t]} || ${+opt_args[--from]} || ${+opt_args[-f]} ))
            then
                _wanted files expl 'files' _files
            else
                if (( CURRENT == 1 ))
                then
                    _wanted files expl 'file' _files
                else
                    _wanted tags expl 'tags' _tmsu_tags_with_values
                fi
            fi
    esac
}

_tmsu_cmd_tags() {
	_arguments -s -w ''{--all,-a}'[show all tags]' \
	                 ''{--count,-c}'[lists the number of tags rather than their names]' \
	                 '-1[list one tag per line]' \
	                 ''{--explicit,-e}'[do not show implied tags]' \
	                 '*:file:_files' \
	&& ret=0
}

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

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

	case $state in
		(items)
			if (( ${+opt_args[--tags]} || ${+opt_args[-t]} || ${+opt_args[--all]} || ${+opt_args[-a]} ))
			then
                _wanted files expl 'files' _files
			else
				if (( CURRENT == 1 ))
				then
					_wanted files expl 'files' _files
				else
					_wanted tags expl 'tags' _tmsu_file_tags
				fi
			fi
	esac
}

_tmsu_cmd_values() {
	_arguments -s -w ''{--all,-a}'[show all values]' \
	                 ''{--count,-c}'[lists the number of values rather than their names]' \
	                 '-1[lists on value per line]' \
	                 '*:tag:_tmsu_tags' \
	&& ret=0
}

_tmsu_cmd_version() {
	# no arguments
}

_tmsu_cmd_vfs() {
    _arguments -s -w ''{--options,-o}'[mount options (passed to fusermount)]' \
                     '1:file:_files' \
	                 '2:mountpoint:_dirs' \
	&& ret=0
}

_tmsu "$@"