Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[Contrib] digraphs / composition pairs



After reading about insert-composed-char, I wanted to be able to add my
own definitions; I don't see a reason why all possible compose sequences
need to be centrally maintained.  Eg, in vim I like having the smiley
faces available (even if I never do use them ...)

I'm a bit unsure about the function naming; vim calls these digraphs and
I'd quite like some compatibility, but normal single-letter options are
also valid composition sequences.  Otherwise, I'd name the main function
"digraph" and give it a -d option to take decimal values, for vim
compatibility.  If these get accepted, they should get renamed by
someone with a better sense of naming aesthetics.

vim: digraph :) 9786 :( 9785
zsh: add_composition_pair ':)' 263A ':(' 2639
     digraph_decimal ':)' 9786 ':(' 9785

This also provides a digraphs command, which produces a list of all
digraphs.  Something's nagging at me about there being a supplied zsh
method of doing "fmt" reliably internally ...

function add_composition_pair {
	emulate -L zsh
	if (( $# % 2 ))
	then
		print -u2 "Usage: $0 <pair> <hexval> [<pair> <hexval> ...]"
		return 1
	fi
	if (( ${+zsh_accented_chars} == 0 ))
	then
		autoload -U define-composed-chars
		define-composed-chars
		unfunction define-composed-chars
	fi
	while (( $# ))
	do
		local pair="$1" val="$2"
		shift 2
		local -A existing
		existing=( ${=zsh_accented_chars[${pair[2]}]} )
		if [[ -n ${(k)existing[${pair[1]}]} ]]
		then
			noglob unset existing[${pair[1]}]
			zsh_accented_chars[${pair[2]}]=" ${(kv)existing}"
		fi
		zsh_accented_chars[${pair[2]}]+=" ${pair[1]} $val"
	done
}

function digraph_decimal {
	local pair val
	local -a acp
	for pair val in "$@"
	do
		[[ -n $val ]] && val=$(( [#16] 10#$val))
		acp+=($pair ${val#0x})
	done
	add_composition_pair $acp
}

function digraphs {
	local p1 p2 p v
	local -A nested all
	if (( ${+zsh_accented_chars} == 0 ))
	then
		autoload -U define-composed-chars
		define-composed-chars
		unfunction define-composed-chars
	fi
	for p2 in ${(k)zsh_accented_chars}
	do
		nested=(${=zsh_accented_chars[$p2]})
		for p1 in ${(k)nested}
		do
			v=${nested[$p1]}
			[[ ${#v} -gt 4 ]] && v=${(l.8..0.)v} || v=${(l.4..0.)v}
			all[$p1$p2]=$v
		done
	done
	for p in ${(ko)all}
	do
		print -- "${(q)p}  ${all[$p]}  \U${(l.8..0.)all[$p]}"
	done
}



Messages sorted by: Reverse Date, Date, Thread, Author