[Momonga-devel.ja:01466] Re: proposed modification of SPECS


さくです。

In article <20030306181250.GA13730%masahiro@xxxxxxxxxxxxxxxxxxxxx>,
Masahiro Nishiyama <masahiro@xxxxxxxxxxxxxxxxxxxxx> writes:

> # _mph-get や _mph_packages が mph に含められれば, 

う、こ、この関数名は……

数ヶ月前に見様見真似で作ったシロモノですが、とりあえず添付してみたりします。
完全ではないと思いますが、個人的に $fpath に転がしてあるものです。

お試しください。

-- 
OZAWA Sakuro                   "Quidquid latine dictum sit, altum videtur."

Mail: mailto:crouton@xxxxxxxxxxxxxxxx       GnuPG: 1C1A 4C26 32E2 A911 7B62
 Web: http://www.weatherlight.org/~crouton/        E194 37C0 8725 F1D8 F388
#compdef mph-get

_mph-get () {
	_arguments -s -w \
		'-k[with kernel]' \
		'-n[no execute]' \
		'-v[verbose mode]' \
		'-f[force mode]' \
		'-c[specifies configuration file]:file:_files' \
		'-q[don'\''t update database]' \
		'-x[ignore $holds in mph.conf]' \
		'*::mph-get command:_mph_get_command'
}

(( $+functions[_mph_get_command] )) ||
_mph_get_command () {
	local cmd
	if (( ! $+_mph_get_cmds )); then
		typeset -gA _mph_get_cmds
		_mph_get_cmds=(
			install 	"install package(s)"
			upgrade		"upgrade all installed packages"
			noinstall	"list packages that are not installed"
			noexist		"list packages that are not available anymore"
			whatprovides	"show package which contains given file"
			get		"download package(s)"
			get-all		"download all packages"
			remove		"uninstall package(s)"
			clean		"clean the download cache"
		)
	fi

	if (( CURRENT == 1 )); then
		_tags commands && { compadd "$@" -k _mph_get_cmds }
	else
		local curcontext="$curcontext"
		cmd="${${(k)_mph_get_cmds[(R)* $words[1] *]}:-${(k)_mph_get_cmds[(i)$words[1]]}}"
		if (( $#cmd )); then
			curcontext="${curcontext%:*:*}:mph-${cmd}:"
			case $cmd in
			install | get | remove | whatprovides)
				_mph_get_$cmd
				;;
			*)
				return 0
			esac
		else
			_message "unknown mph command: $words[1]"
		fi
	fi
}

(( $+functions[_mph_get_install] )) ||
_mph_get_install () {
	_arguments -s \
		'*:available packages:_mph_packages install'
}

(( $+functions[_mph_get_get] )) ||
_mph_get_get () {
	_arguments -s \
		'*:available packages:_mph_packages get'
}

(( $+functions[_mph_get_remove] )) ||
_mph_get_remove () {
	_arguments -s \
		'*:installed packages:_mph_packages remove'
}

(( $+functions[_mph_get_whatprovides] )) ||
_mph_get_whatprovides () {
	_arguments -s \
		'*:file:_files'
}
#autoload

_mph_packages_update_avail () {
	if ( [[ ${+_mph_avails} -eq 0 ]] ||
		_cache_invalid MPH_avails) && ! _retrieve_cache MPH_avails;
	then
		_mph_avails=(
			${(f)"$(mph-get noinstall 2>/dev/null | cut -f2 -d' ')"}
		)
		_store_cache MPH_avails _mph_avails
	fi
	cachevar=_mph_avails
}

_mph_packages_update_installed () {
	if ( [[ ${+_rpms} -eq 0 ]] ||
		_cache_invalid RPMs) && ! _retrieve_cache RPMs;
	then
		_rpms=( $(_call_program packages rpm -qa 2>/dev/null) )
		_store_cache RPMs _rpms
	fi
	cachevar=_rpms
}

_mph_packages () {
	local command="$argv[$#]" expl cachevar pkgset update_policy

	zstyle -s ":completion:*:*:$service:*" cache-policy update_policy
	[[ -z "$update_policy" ]] && zstyle ":completion:*:*:$service:*" cache-policy _mph_caching_policy

	[[ "$command" = (install|get|remove) ]] || {
		_message "_mph_packages:unknown command: $command"
		return
	}

	case $command in
	install | get)
		pkgset=avail
		;;
	remove)
		pkgset=installed
		;;
	esac

	expl=("${(@)argv[1,-2]}")

	_mph_packages_update_$pkgset

	_tags packages && compadd "$expl[@]" - "${(@P)cachevar}"
}

_mph_caching_policy () {
	oldp=( "$1"(mw+1) ) 
	(( $#oldp )) && return 0

	[[ /var/mph/lib/Packages -nt "$1" ]]
}

_mph_packages "$@"