/python/engine/EnglishWriter/English.py

http://scim-python.googlecode.com/ · Python · 265 lines · 209 code · 30 blank · 26 comment · 56 complexity · 0b49339bc1d4727f12d60e375984b724 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. # vim: set noet ts=4:
  3. #
  4. # scim-python
  5. #
  6. # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  7. #
  8. #
  9. # This library is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU Lesser General Public
  11. # License as published by the Free Software Foundation; either
  12. # version 2 of the License, or (at your option) any later version.
  13. #
  14. # This library is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Lesser General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser General Public
  20. # License along with this program; if not, write to the
  21. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  22. # Boston, MA 02111-1307 USA
  23. #
  24. # $Id: $
  25. #
  26. import scim
  27. from scim import KeyCode
  28. from scim import KeyMask
  29. from scim import IMEngine
  30. from scim import IMEngineFactory
  31. import os
  32. import traceback
  33. import enchant
  34. import locale
  35. from time import strftime
  36. from gettext import dgettext
  37. _ = lambda a : dgettext ("scim-python", a)
  38. N_ = lambda a : a
  39. class Engine (IMEngine):
  40. def __init__ (self, factory, config, db, encoding, id):
  41. IMEngine.__init__ (self, factory, config, encoding, id)
  42. self._config = config
  43. self._lookup_table = scim.LookupTable (9)
  44. self._preedit_string = u""
  45. self._db = db
  46. self._cursor = 0
  47. self._candidates = []
  48. self._always_show_candidates = config.read ("/IMEngine/Python/EnglishWriter/AlwaysShowCandidates", False)
  49. self._commit_space = config.read ("/IMEngine/Python/EnglishWriter/CommitSpace", True)
  50. def get_candidates (self, check = True):
  51. candidates = []
  52. if self._preedit_string == u"date":
  53. candidates.append (u"date")
  54. candidates.append (unicode (strftime ("%x")))
  55. elif self._preedit_string == u"time":
  56. candidates.append (u"time")
  57. candidates.append (unicode (strftime ("%X")))
  58. candidates.append (unicode (strftime ("%c")))
  59. if self._db.check (self._preedit_string) and check:
  60. self._candidates = candidates + []
  61. else:
  62. words = map (unicode, self._db.suggest(self._preedit_string))
  63. lower = self._preedit_string.islower ()
  64. self._candidates = candidates + [c for c in words if c.islower() == lower]
  65. def update (self):
  66. if not self._candidates:
  67. self.hide_lookup_table ()
  68. self._lookup_table.clear ()
  69. for can in self._candidates:
  70. self._lookup_table.append_candidate (can)
  71. self.update_lookup_table (self._lookup_table)
  72. attrs = []
  73. if self._candidates:
  74. attrs.append (scim.Attribute (0, len (self._preedit_string), scim.ATTR_FOREGROUND, 0x00ff0000))
  75. attrs.append (scim.Attribute (0, len (self._preedit_string), scim.ATTR_DECORATE, scim.ATTR_DECORATE_UNDERLINE))
  76. self.update_preedit_string (self._preedit_string, attrs)
  77. self.update_preedit_caret (self._cursor)
  78. if self._preedit_string:
  79. self.show_preedit_string ()
  80. else:
  81. self.hide_preedit_string ()
  82. if self._candidates:
  83. self.show_lookup_table ()
  84. def process_key_event (self, key):
  85. if key.mask & KeyMask.ReleaseMask: # Ignore release event
  86. return False
  87. if key.mask & KeyMask.ControlMask:
  88. return False
  89. if key.mask & KeyMask.CapsLockMask:
  90. return False
  91. if (key.code >= KeyCode.KEY_a and key.code <= KeyCode.KEY_z) or \
  92. (key.code >= KeyCode.KEY_A and key.code <= KeyCode.KEY_Z) or \
  93. (key.code == KeyCode.KEY_apostrophe and self._cursor != 0):
  94. ls = list (self._preedit_string)
  95. ls.insert (self._cursor, unichr (key.code))
  96. self._preedit_string = u"".join (ls)
  97. self._cursor += 1
  98. self.get_candidates (not self._always_show_candidates)
  99. self.update ()
  100. return True
  101. elif key.code == KeyCode.KEY_Page_Up:
  102. if not self._preedit_string:
  103. return False
  104. if self._candidates:
  105. self.lookup_table_page_up ()
  106. else:
  107. self.get_candidates (False)
  108. self.update ()
  109. return True
  110. elif key.code == KeyCode.KEY_Page_Down:
  111. if not self._preedit_string:
  112. return False
  113. if self._candidates:
  114. self.lookup_table_page_down ()
  115. else:
  116. self.get_candidates (False)
  117. self.update ()
  118. return True
  119. elif key.code == KeyCode.KEY_Left:
  120. if self._cursor > 0:
  121. self._cursor -= 1
  122. self.update_preedit_caret (self._cursor)
  123. if self._preedit_string:
  124. return True
  125. return False
  126. elif key.code == KeyCode.KEY_Right:
  127. if self._cursor < len (self._preedit_string):
  128. self._cursor += 1
  129. self.update_preedit_caret (self._cursor)
  130. if self._preedit_string:
  131. return True
  132. return False
  133. elif key.code >= KeyCode.KEY_1 and key.code <= KeyCode.KEY_9:
  134. i = key.code - KeyCode.KEY_1 + self._lookup_table.get_current_page_start ()
  135. if i >= len (self._candidates):
  136. self.commit_string ()
  137. return False
  138. if self._commit_space:
  139. self.commit_string (self._candidates[i] + u" ")
  140. else:
  141. self.commit_string (self._candidates[i])
  142. return True
  143. elif key.code == KeyCode.KEY_Return:
  144. if self._preedit_string:
  145. self.commit_string ()
  146. return True
  147. return False
  148. elif key.code in (KeyCode.KEY_KP_Space, KeyCode.KEY_space):
  149. if self._candidates:
  150. self.commit_string (self._candidates[0])
  151. elif self._preedit_string:
  152. self.commit_string ()
  153. else:
  154. return False
  155. if self._commit_space:
  156. return False
  157. else:
  158. return True
  159. elif key.code == KeyCode.KEY_BackSpace:
  160. if self._preedit_string and self._cursor > 0:
  161. ls = list (self._preedit_string)
  162. del ls[self._cursor - 1]
  163. self._preedit_string = u"".join (ls)
  164. self._cursor -= 1
  165. self.get_candidates ()
  166. self.update ()
  167. return True
  168. return False
  169. if self._preedit_string:
  170. self.commit_string ()
  171. return False
  172. def commit_string (self, string = None):
  173. if string == None:
  174. string = self._preedit_string
  175. self._preedit_string = u""
  176. self._candidates = []
  177. self._cursor = 0
  178. IMEngine.commit_string (self, string)
  179. self.update ()
  180. def move_preedit_caret (self, pos):
  181. IMEngine.move_preedit_caret (self, pos)
  182. def select_candidate (self, index):
  183. IMEngine.select_candidate (self, index)
  184. def update_lookup_table_page_size (self, page_size):
  185. IMEngine.update_lookup_table_page_size (self, page_size)
  186. def lookup_table_page_up (self):
  187. if self._lookup_table.page_up ():
  188. self.update_lookup_table (self._lookup_table)
  189. return True
  190. IMEngine.lookup_table_page_up (self)
  191. return False
  192. def lookup_table_page_down (self):
  193. if self._lookup_table.page_down ():
  194. self.update_lookup_table (self._lookup_table)
  195. return True
  196. IMEngine.lookup_table_page_down (self)
  197. return False
  198. def reset (self):
  199. self._preedit_string = u""
  200. self._candidates = []
  201. self._cursor = 0
  202. self.update ()
  203. IMEngine.reset (self)
  204. def focus_in (self):
  205. IMEngine.focus_in (self)
  206. self.update ()
  207. def focus_out (self):
  208. self.reset ()
  209. IMEngine.focus_out (self)
  210. def trigger_property (self, property):
  211. IMEngine.trigger_property (self, property)
  212. def process_helper_event (self, helper_uuid, trans):
  213. IMEngine.process_helper_event (self, helper_uuid, trans)
  214. def update_client_capabilities (self, cap):
  215. IMEngine.update_client_capabilities (self, cap)
  216. def reload_config (self, config):
  217. self._always_show_candidates = config.read ("/IMEngine/Python/EnglishWriter/AlwaysShowCandidates", False)
  218. self._commit_space = config.read ("/IMEngine/Python/EnglishWriter/CommitSpace", True)
  219. class Factory (IMEngineFactory):
  220. def __init__ (self, config):
  221. IMEngineFactory.__init__ (self, config)
  222. self._config = config
  223. self.name = _(u"English Writer")
  224. self.uuid = "7fb43ae9-0d72-4d7a-b255-f437ce28d599"
  225. self.authors = u"Huang Peng <shawn.p.huang@gmail.com>"
  226. self.icon_file = "/usr/share/scim/icons/scim-python.png"
  227. self.credits = u"GPL"
  228. self.help = _(u"Help For English")
  229. self.set_languages ("en")
  230. self._dict = enchant.Dict ("en")
  231. # locale.setlocale (locale.LC_ALL, "en_US.UTF-8")
  232. def create_instance (self, encoding, id):
  233. return Engine (self, self._config, self._dict, encoding, id)
  234. def reload_config (self, config):
  235. pass