From 4e02a284ebbf190275d6e3f5e8cf4dc08511e5e1 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Tue, 15 Dec 2015 07:47:35 +0100 Subject: [PATCH] Ensure LC_CTYPE uses UTF-8 See #2012 for some context. Many unicode-related tasks need that coaelescing to handle unicode correctly. If for some reason, the user is using a not using a non-unicode locale, we want to ensure that at least LC_CTYPE is set to an unicode locale. It's difficult to know if a locale is an unicode one, so we assume that we want UTF-8 as an unicode codec. Aliases are not handled, so LC_CTYPE may be redefined even if it was already unicode aware. The first available UTF-8 locale is used because there is no guarantee that C.UTF-8 or en_US.UTF-8 are available. For LC_CTYPE, there is no difference between those locales as coaelescing is not dependant on the language. LC_CTYPE is exported as oh-my-zsh may use some shell tools at some point (sed, awk, ...). --- lib/misc.zsh | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/misc.zsh b/lib/misc.zsh index c81dab41..782f8708 100644 --- a/lib/misc.zsh +++ b/lib/misc.zsh @@ -34,9 +34,20 @@ else fi # only define LC_CTYPE if undefined -if [[ -z "$LC_CTYPE" && -z "$LC_ALL" ]]; then - export LC_CTYPE=${LANG%%:*} # pick the first entry from LANG -fi +LC_CTYPE=${LC_CTYPE:-${LC_ALL:-${LANG:-C}}} +case $LC_CTYPE in + *.utf8|*.UTF-8) + # All is correct + ;; + *) + # Need to select an UTF-8 locale + local -a available + available=("${(f)$(locale -a 2> /dev/null)}") + export LC_CTYPE=${${${${(@M)available:#*.UTF-8}[1]}:-${${(@M)available:#*.utf8}[1]}}:-C.UTF-8} + unset available + ;; +esac +export LC_CTYPE # recognize comments setopt interactivecomments