Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/encoding.py @ 12866:eddc20306ab6 stable
encoding: default ambiguous character to narrow
The current implementation of colwidth was treating 'A'mbiguous
characters as wide, which was incorrect in a non-East Asian context.
As per http://unicode.org/reports/tr11/#Recommendations, we should
instead default to 'narrow' if we don't know better. As character
width is dependent on the particular font used and we have no idea
what fonts are in use, this recommendation applies.
This introduces HGENCODINGAMBIGUOUS to get the old behavior back.
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Wed, 27 Oct 2010 15:35:21 -0500 |
parents | 614f0d8724ab |
children | 7cc4263e07a9 |
comparison
equal
deleted
inserted
replaced
12865:4c50552fc9bc | 12866:eddc20306ab6 |
---|---|
85 sub = s[max(0, inst.start - 10):inst.start + 10] | 85 sub = s[max(0, inst.start - 10):inst.start + 10] |
86 raise error.Abort("decoding near '%s': %s!" % (sub, inst)) | 86 raise error.Abort("decoding near '%s': %s!" % (sub, inst)) |
87 except LookupError, k: | 87 except LookupError, k: |
88 raise error.Abort("%s, please check your locale settings" % k) | 88 raise error.Abort("%s, please check your locale settings" % k) |
89 | 89 |
90 # How to treat ambiguous-width characters. Set to 'wide' to treat as wide. | |
91 ambiguous = os.environ.get("HGENCODINGAMBIGUOUS", "narrow") | |
92 | |
90 def colwidth(s): | 93 def colwidth(s): |
91 "Find the column width of a UTF-8 string for display" | 94 "Find the column width of a UTF-8 string for display" |
92 d = s.decode(encoding, 'replace') | 95 d = s.decode(encoding, 'replace') |
93 if hasattr(unicodedata, 'east_asian_width'): | 96 if hasattr(unicodedata, 'east_asian_width'): |
97 wide = "WF" | |
98 if ambiguous == "wide": | |
99 wide = "WFA" | |
94 w = unicodedata.east_asian_width | 100 w = unicodedata.east_asian_width |
95 return sum([w(c) in 'WFA' and 2 or 1 for c in d]) | 101 return sum([w(c) in wide and 2 or 1 for c in d]) |
96 return len(d) | 102 return len(d) |
97 | 103 |