import gtk
import os
import pygtk
+import re
class Controller:
class SearchAction:
+ re_hex = re.compile(r"[0-9a-fA-F]")
+
def __init__(self, entry_search):
self.entry_search = entry_search
+ self.char_util = CharUtil()
self.code = None
self.char = ''
def set_next(self):
en = self.entry_search
buf = en.get_text().decode('utf-8')
+ if len(buf) == 0: return
+
pos = en.get_position()
if pos == len(buf): pos = 0
- en.select_region(pos, pos + 1)
- ch = buf[pos:pos + 1]
+ region = self.get_target_region(buf, pos)
+ target = buf[region[0]:region[1]]
+ en.select_region(region[0], region[1])
+ ch = self.char_util.get_unichar(target)
self.code = hex(ord(ch))
self.char = ch
mu = '<span font="%s">%s</span>' % (font_name, self.char)
label.set_markup(mu)
+ def get_target_region(self, text, pos):
+ ch = text[pos:pos + 1]
+ rlen = 0
+ llen = 0
+ while rlen <= 4 and self.char_util.is_hex_char(ch):
+ rlen += 1
+ if pos + rlen >= len(text): break
+ ch = text[(pos + rlen):(pos + rlen + 1)]
+ if rlen >= 1 and rlen < 5:
+ while rlen + llen <= 4 and pos - llen > 0:
+ ch = text[(pos - llen - 1):(pos - llen)]
+ if not self.char_util.is_hex_char(ch): break
+ llen += 1
+ ret = (pos - llen, pos + rlen)
+ if ret == (pos, pos): return (pos, pos + 1)
+ return ret
+
+class CharUtil:
+ def is_hex_char(self, char):
+ return (char >= '0' and char <= '9') or \
+ (char >= 'a' and char <= 'f') or \
+ (char >= 'A' and char <= 'F')
+
+ def get_unichar(self, buf):
+ if len(buf) <= 1: return buf
+
+ return unichr(int(buf, 16))
+
if __name__ == '__main__':
path = os.path.abspath(os.path.dirname(__file__))