Commits

Yuya Nishihara committed e51d18f

revised version in upstream

Comments (0)

Files changed (2)

mbcs-textwrap.mbox

-# HG changeset patch
-# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
-# Date 1274008290 -32400
-# Node ID e09a563164ace7f73e4325aca71e881f497ec3f1
-# Parent  4e72138a0a394972feb5793b98131b30af1046dc
-replace Python standard textwrap by MBCS sensitive one for i18n text
-
-MBCS sensitive textwrap is originally implemented
-by ITO Nobuaki <daydream.trippers@gmail.com>.
-
-diff --git a/mercurial/encoding.py b/mercurial/encoding.py
---- a/mercurial/encoding.py
-+++ b/mercurial/encoding.py
-@@ -67,11 +67,21 @@ def fromlocal(s):
-     except LookupError, k:
-         raise error.Abort("%s, please check your locale settings" % k)
- 
-+# east asian character width
-+eacwidth = {
-+    "W": 2, # Wide
-+    "F": 2, # Full-width
-+}
-+
-+try:
-+    eacwidth["A"] = int(os.environ.get("HGUCACWIDTH"))
-+except:
-+    pass # ignore all error
-+
- def colwidth(s):
-     "Find the column width of a UTF-8 string for display"
-     d = s.decode(encoding, 'replace')
-     if hasattr(unicodedata, 'east_asian_width'):
-         w = unicodedata.east_asian_width
--        return sum([w(c) in 'WF' and 2 or 1 for c in d])
-+        return sum([eacwidth.get(w(c), 1) for c in d])
-     return len(d)
--
-diff --git a/mercurial/help/environment.txt b/mercurial/help/environment.txt
---- a/mercurial/help/environment.txt
-+++ b/mercurial/help/environment.txt
-@@ -24,6 +24,14 @@ HGENCODINGMODE
-     "ignore", which drops them. This setting can be overridden with
-     the --encodingmode command-line option.
- 
-+HGUCACWIDTH
-+    This sets column width of east asian ambiguous characters. This
-+    influences line wrapping for help documents. The default is '1'.
-+
-+    This should be set as 2 for:
-+
-+    - Japanese
-+
- HGMERGE
-     An executable to use for resolving merge conflicts. The program
-     will be executed with three arguments: local file, remote file,
-diff --git a/mercurial/i18n.py b/mercurial/i18n.py
---- a/mercurial/i18n.py
-+++ b/mercurial/i18n.py
-@@ -6,7 +6,7 @@
- # GNU General Public License version 2 or any later version.
- 
- import encoding
--import gettext, sys, os
-+import gettext, sys, os, textwrap, unicodedata
- 
- # modelled after templater.templatepath:
- if hasattr(sys, 'frozen'):
-@@ -53,3 +53,37 @@ if 'HGPLAIN' in os.environ:
- else:
-     _ = gettext
- 
-+#### naming convention of below implementation follows 'textwrap' module
-+
-+class MBTextWrapper(textwrap.TextWrapper):
-+    def __init__(self, **kwargs):
-+        textwrap.TextWrapper.__init__(self, **kwargs)
-+
-+    def _cutdown(self, str, space_left):
-+        l = 0
-+        ucstr = unicode(str, encoding.encoding)
-+        w = unicodedata.east_asian_width
-+        eacwidth = encoding.eacwidth
-+        for i in xrange(len(ucstr)):
-+            l += eacwidth.get(w(ucstr[i]), 1)
-+            if space_left < l:
-+                return (ucstr[:i].encode(encoding.encoding),
-+                        ucstr[i:].encode(encoding.encoding))
-+        return str, ''
-+
-+    # ----------------------------------------
-+    # overriding of base class
-+
-+    def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
-+        space_left = max(width - cur_len, 1)
-+
-+        if self.break_long_words:
-+            cut, res = self._cutdown(reversed_chunks[-1], space_left)
-+            cur_line.append(cut)
-+            reversed_chunks[-1] = res
-+        elif not cur_line:
-+            cur_line.append(reversed_chunks.pop())
-+
-+# these are overwritable by custom wrapping/filling methods
-+wraptext = lambda t, **kwa: MBTextWrapper(**kwa).wrap(t)
-+filltext = lambda t, **kwa: '\n'.join(MBTextWrapper(**kwa).wrap(t))
-diff --git a/mercurial/minirst.py b/mercurial/minirst.py
---- a/mercurial/minirst.py
-+++ b/mercurial/minirst.py
-@@ -35,8 +35,8 @@ It only supports a small subset of reStr
- - inline literals (no other inline markup is not recognized)
- """
- 
--import re, sys, textwrap
--
-+import re, sys
-+import i18n
- 
- def findblocks(text):
-     """Find continuous blocks of lines in text.
-@@ -304,7 +304,7 @@ def formatblock(block, width):
-         hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip())
-         defindent = indent + hang * ' '
-         text = ' '.join(map(str.strip, block['lines'][1:]))
--        return "%s\n%s" % (term, textwrap.fill(text, width=width,
-+        return "%s\n%s" % (term, i18n.filltext(text, width=width,
-                                                initial_indent=defindent,
-                                                subsequent_indent=defindent))
-     subindent = indent
-@@ -338,7 +338,7 @@ def formatblock(block, width):
-         subindent = indent + (len(option) + len(arg)) * ' '
- 
-     text = ' '.join(map(str.strip, block['lines']))
--    return textwrap.fill(text, width=width,
-+    return i18n.filltext(text, width=width,
-                          initial_indent=indent,
-                          subsequent_indent=subindent)
- 
-diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
---- a/mercurial/templatefilters.py
-+++ b/mercurial/templatefilters.py
-@@ -5,8 +5,8 @@
- # This software may be used and distributed according to the terms of the
- # GNU General Public License version 2 or any later version.
- 
--import cgi, re, os, time, urllib, textwrap
--import util, encoding
-+import cgi, re, os, time, urllib
-+import util, encoding, i18n
- 
- def stringify(thing):
-     '''turn nested template iterator into string.'''
-@@ -69,7 +69,7 @@ def fill(text, width):
-             yield text[start:m.start(0)], m.group(1)
-             start = m.end(1)
- 
--    return "".join([space_re.sub(' ', textwrap.fill(para, width)) + rest
-+    return "".join([space_re.sub(' ', i18n.filltext(para, width=width)) + rest
-                     for para, rest in findparas()])
- 
- def firstline(text):
-diff --git a/mercurial/util.py b/mercurial/util.py
---- a/mercurial/util.py
-+++ b/mercurial/util.py
-@@ -14,9 +14,9 @@ hide platform-specific details from the 
- """
- 
- from i18n import _
--import error, osutil, encoding
-+import error, osutil, encoding, i18n
- import cStringIO, errno, re, shutil, sys, tempfile, traceback
--import os, stat, time, calendar, textwrap, signal
-+import os, stat, time, calendar, signal
- import imp
- 
- # Python compatibility
-@@ -1265,14 +1265,7 @@ def wrap(line, hangindent, width=None):
-         # adjust for weird terminal size
-         width = max(78, hangindent + 1)
-     padding = '\n' + ' ' * hangindent
--    # To avoid corrupting multi-byte characters in line, we must wrap
--    # a Unicode string instead of a bytestring.
--    try:
--        u = line.decode(encoding.encoding)
--        w = padding.join(textwrap.wrap(u, width=width - hangindent))
--        return w.encode(encoding.encoding)
--    except UnicodeDecodeError:
--        return padding.join(textwrap.wrap(line, width=width - hangindent))
-+    return padding.join(i18n.wraptext(line, width=width - hangindent))
- 
- def iterlines(iterator):
-     for chunk in iterator:
 mq-qrename-subdir.diff
 atomictemp-fileattr.diff
 atomictemp-symlink.diff
-mbcs-textwrap.mbox
 main-is-frozen.diff
 chad-dispatch-refactor.mbox
 chad-pydispatch.mbox
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.