PageRenderTime 307ms CodeModel.GetById 101ms app.highlight 95ms RepoModel.GetById 106ms app.codeStats 0ms

/python/engine/EnglishWriter/English.py

http://scim-python.googlecode.com/
Python | 265 lines | 238 code | 2 blank | 25 comment | 0 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#
 26import scim
 27from scim import KeyCode
 28from scim import KeyMask
 29from scim import IMEngine
 30from scim import IMEngineFactory
 31import os
 32import traceback
 33import enchant
 34import locale
 35from time import strftime
 36
 37from gettext import dgettext
 38_  = lambda a : dgettext ("scim-python", a)
 39N_ = lambda a : a
 40
 41class Engine (IMEngine):
 42	def __init__ (self, factory, config, db, encoding, id):
 43		IMEngine.__init__ (self, factory, config, encoding, id)
 44		self._config = config
 45		self._lookup_table = scim.LookupTable (9)
 46		self._preedit_string = u""
 47		self._db = db
 48		self._cursor = 0
 49		self._candidates = []
 50		self._always_show_candidates = config.read ("/IMEngine/Python/EnglishWriter/AlwaysShowCandidates", False)
 51		self._commit_space = config.read ("/IMEngine/Python/EnglishWriter/CommitSpace", True)
 52
 53	def get_candidates (self, check = True):
 54		candidates = []
 55		if self._preedit_string == u"date":
 56			candidates.append (u"date")
 57			candidates.append (unicode (strftime ("%x")))
 58		elif self._preedit_string == u"time":
 59			candidates.append (u"time")
 60			candidates.append (unicode (strftime ("%X")))
 61			candidates.append (unicode (strftime ("%c")))
 62			
 63		if self._db.check (self._preedit_string) and check:
 64			self._candidates = candidates + []
 65		else:
 66			words = map (unicode, self._db.suggest(self._preedit_string))
 67			lower = self._preedit_string.islower ()
 68			self._candidates = candidates + [c for c in words if c.islower() == lower]
 69	
 70	def update (self):
 71		if not self._candidates:
 72			self.hide_lookup_table ()
 73
 74		self._lookup_table.clear ()
 75		
 76		for can in self._candidates:
 77			self._lookup_table.append_candidate (can)
 78		self.update_lookup_table (self._lookup_table)
 79		
 80		attrs = []
 81		if self._candidates:
 82			attrs.append (scim.Attribute (0, len (self._preedit_string), scim.ATTR_FOREGROUND, 0x00ff0000))
 83			attrs.append (scim.Attribute (0, len (self._preedit_string), scim.ATTR_DECORATE, scim.ATTR_DECORATE_UNDERLINE))
 84		self.update_preedit_string (self._preedit_string, attrs)
 85		self.update_preedit_caret (self._cursor)
 86		if self._preedit_string:	
 87			self.show_preedit_string ()
 88		else:
 89			self.hide_preedit_string ()
 90		
 91		if self._candidates:
 92			self.show_lookup_table ()
 93
 94	def process_key_event (self, key):
 95		if key.mask & KeyMask.ReleaseMask: # Ignore release event
 96			return False
 97		if key.mask & KeyMask.ControlMask:
 98			return False
 99		if key.mask & KeyMask.CapsLockMask:
100			return False
101		
102		if (key.code >= KeyCode.KEY_a and key.code <= KeyCode.KEY_z) or \
103			(key.code >= KeyCode.KEY_A and key.code <= KeyCode.KEY_Z) or \
104			(key.code == KeyCode.KEY_apostrophe and self._cursor != 0):
105			ls = list (self._preedit_string)
106			ls.insert (self._cursor, unichr (key.code))
107			self._preedit_string = u"".join (ls)
108			self._cursor += 1
109			self.get_candidates (not self._always_show_candidates)
110			self.update ()
111			return True
112		elif key.code == KeyCode.KEY_Page_Up:
113			if not self._preedit_string:
114				return False
115			if self._candidates:
116				self.lookup_table_page_up ()
117			else:
118				self.get_candidates (False)
119				self.update ()
120			return True
121		elif key.code == KeyCode.KEY_Page_Down:
122			if not self._preedit_string:
123				return False
124			if self._candidates:
125				self.lookup_table_page_down ()
126			else:
127				self.get_candidates (False)
128				self.update ()
129			return True
130		elif key.code == KeyCode.KEY_Left:
131			if self._cursor > 0:
132				self._cursor -= 1
133				self.update_preedit_caret (self._cursor)
134			if self._preedit_string:
135				return True
136			return False
137		elif key.code == KeyCode.KEY_Right:
138			if self._cursor < len (self._preedit_string):
139				self._cursor += 1
140				self.update_preedit_caret (self._cursor)
141			if self._preedit_string:
142				return True
143			return False
144		elif key.code >= KeyCode.KEY_1 and key.code <= KeyCode.KEY_9:
145			i = key.code - KeyCode.KEY_1 + self._lookup_table.get_current_page_start ()
146			if i >= len (self._candidates):
147				self.commit_string ()
148				return False
149			if self._commit_space:
150				self.commit_string (self._candidates[i] + u" ")
151			else:
152				self.commit_string (self._candidates[i])
153
154			return True	
155		elif key.code == KeyCode.KEY_Return:
156			if self._preedit_string:
157				self.commit_string ()
158				return True
159			return False		
160		elif key.code in (KeyCode.KEY_KP_Space, KeyCode.KEY_space):
161			if self._candidates:
162				self.commit_string (self._candidates[0])
163			elif self._preedit_string:
164				self.commit_string ()
165			else:
166				return False			
167			if self._commit_space:
168				return False
169			else:
170				return True
171		elif key.code == KeyCode.KEY_BackSpace:
172			if self._preedit_string and self._cursor > 0:
173				ls = list (self._preedit_string)
174				del ls[self._cursor - 1]
175				self._preedit_string = u"".join (ls)
176				self._cursor -= 1
177				self.get_candidates ()
178				self.update ()
179				return True
180			return False
181		
182		if self._preedit_string:
183			self.commit_string ()
184		return False
185			
186	def commit_string (self, string = None):
187		if string == None:
188			string = self._preedit_string
189		self._preedit_string = u""
190		self._candidates = []
191		self._cursor = 0
192		IMEngine.commit_string (self, string)
193		self.update ()
194
195	def move_preedit_caret (self, pos):
196		IMEngine.move_preedit_caret (self, pos)
197	
198	def select_candidate (self, index):
199		IMEngine.select_candidate (self, index)
200
201	def update_lookup_table_page_size (self, page_size):
202		IMEngine.update_lookup_table_page_size (self, page_size)
203
204	def lookup_table_page_up (self):
205		if self._lookup_table.page_up ():
206			self.update_lookup_table (self._lookup_table)
207			return True
208		IMEngine.lookup_table_page_up (self)
209		return False
210
211	def lookup_table_page_down (self):
212		if self._lookup_table.page_down ():
213			self.update_lookup_table (self._lookup_table)
214			return True
215		IMEngine.lookup_table_page_down (self)
216		return False
217
218	def reset (self):
219		self._preedit_string = u""
220		self._candidates = []
221		self._cursor = 0
222		self.update ()
223		IMEngine.reset (self)
224
225	def focus_in (self):
226		IMEngine.focus_in (self)
227		self.update ()
228	
229	def focus_out (self):
230		self.reset ()
231		IMEngine.focus_out (self)
232
233	def trigger_property (self, property):
234		IMEngine.trigger_property (self, property)
235
236	def process_helper_event (self, helper_uuid, trans):
237		IMEngine.process_helper_event (self, helper_uuid, trans)
238
239	def update_client_capabilities (self, cap):
240		IMEngine.update_client_capabilities (self, cap)
241
242	def reload_config (self, config):
243		self._always_show_candidates = config.read ("/IMEngine/Python/EnglishWriter/AlwaysShowCandidates", False)
244		self._commit_space = config.read ("/IMEngine/Python/EnglishWriter/CommitSpace", True)
245
246
247class Factory (IMEngineFactory):
248	def __init__ (self, config):
249		IMEngineFactory.__init__ (self, config)
250		self._config	= config
251		self.name 		= _(u"English Writer")
252		self.uuid 		= "7fb43ae9-0d72-4d7a-b255-f437ce28d599"
253		self.authors	= u"Huang Peng <shawn.p.huang@gmail.com>"
254		self.icon_file 	= "/usr/share/scim/icons/scim-python.png"
255		self.credits 	= u"GPL"
256		self.help		= _(u"Help For English")
257		self.set_languages ("en")
258		self._dict = enchant.Dict ("en")
259		# locale.setlocale (locale.LC_ALL, "en_US.UTF-8")
260
261	def create_instance (self, encoding, id):
262		return Engine (self, self._config, self._dict, encoding, id)
263
264	def reload_config (self, config):
265		pass