# Copyright 1999-2007 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Id: $ # Based on java-vm.eselect DESCRIPTION="Manage CJK system and user Input Method" MAINTAINER="cjk@gentoo.org" SVN_DATE='$Date: $' VERSION=$(svn_date_to_version "${SVN_DATE}" ) IM_SYSTEM_BASE="/etc/X11/xinit/xinput.d" IM_SYSTEM="${IM_SYSTEM_BASE}/default" IM_USER_BASE="${HOME}/.xinput.d" IM_USER="${IM_USER_BASE}/default" find_targets() { local f list ( if [ -d "${IM_SYSTEM_BASE}" ] ; then for f in "${IM_SYSTEM_BASE}"/* ; do if [ -x "${f}" -a "${f}" != "${IM_SYSTEM}" ] ; then echo "${f#${IM_SYSTEM_BASE}/}" fi done fi if [ -d "${IM_USER_BASE}" -a "${UID}" != "0" ] ; then for f in "${IM_USER_BASE}"/* ; do if [ -x "${f}" -a "${f}" != "${IM_USER}" ] ; then echo "${f#${IM_USER_BASE}/}" fi done fi ) | sort -u } sym_to_im() { basename $(readlink "${1}") } ### show action ### describe_show() { echo "Show the current Input Method" } describe_show_parameters() { echo "[system|user]" } do_show() { if [ "${1}" == "system" ] ; then my_show "${IM_SYSTEM}" 'system-im' elif [ "${1}" == "user" ] ; then my_show "${IM_USER}" 'user-im' else my_show "${IM_SYSTEM}" 'system-im' my_show "${IM_USER}" 'user-im' fi } my_show() { local symlink="${1}" im_type="${2}" write_list_start "Current ${im_type}" if [[ -L "${symlink}" ]] ; then write_kv_list_entry "$(sym_to_im ${symlink})" "" else write_kv_list_entry "(unset)" "" fi } ### list action ### describe_list() { echo "List Available Input Methods" } do_list() { targets=( $(find_targets) ) write_list_start "Available Input Methods:" if [[ -n ${targets[@]} ]] ; then local i system_name user_name [[ -L "${IM_SYSTEM}" ]] && system_name=$(sym_to_im "${IM_SYSTEM}") [[ -L "${IM_USER}" ]] && user_name=$(sym_to_im "${IM_USER}") for (( i = 0 ; i < ${#targets[@]} ; i = i + 1 )) ; do local mark="" if [[ ${targets[${i}]} == ${system_name} ]]; then mark="${mark} $(highlight 'system-im')" fi if [[ ${targets[${i}]} == ${user_name} ]]; then mark="${mark} $(highlight 'user-im' )" fi targets[${i}]="${targets[${i}]} ${mark}" done write_numbered_list "${targets[@]}" else write_kv_list_entry "(none found)" "" fi } ### set action ### describe_set() { echo "Set the default Input Method" } describe_set_parameters() { echo "" } do_set() { local target="${1}" if is_number "${target}" ; then targets=( $(find_targets) ) target=${targets[$(( ${target} - 1 ))]} fi if [[ ${#} != 1 ]]; then die -q "Usage " elif [[ "${UID}" == "0" ]]; then if [[ -w $(dirname "${IM_SYSTEM}") ]]; then my_set "${IM_SYSTEM}" "${IM_SYSTEM_BASE}/${target}" else die -q "Sorry, you don't have enough premission to set system" fi else if [[ -f "${IM_USER_BASE}/${target}" ]]; then my_set "${IM_USER}" "${IM_USER_BASE}/${target}" else my_set "${IM_USER}" "${IM_SYSTEM_BASE}/${target}" fi fi } my_set() { local target="${2}" symlink="${1}" if [[ -z "${target}" ]] ; then die -q "You didn't tell me what to set the symlink to" elif [[ -L "${symlink}" ]] ; then set_symlink "${target}" "${symlink}" || die -q "Couldn't set a new symlink" elif [[ -e "${symlink}" ]] ; then die -q "Target file already exists and is not a symlink: ${symlink}" else set_symlink "${target}" "${symlink}" || die -q "Couldn't set a new symlink" fi } set_symlink() { local target="${1}" symlink="${2}" if [[ -z "${target}" ]] ; then die -q "Target \"${1}\" doesn't appear to be valid!" else local sym_dir=$(dirname ${symlink}) if [[ ! -d "${sym_dir}" ]]; then mkdir -p "${sym_dir}" || die -q "Could not create ${my_dir}" fi ln -snf "${target}" "${symlink}" fi } # vim: set ft=eselect :