1 /* # skkinput (Simple Kana-Kanji Input)
2  *
3  * This file is part of skkinput.
4  * Copyright (C) 2002
5  * Takashi SAKAMOTO (PXG01715@nifty.ne.jp)
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with skkinput; see the file COPYING.  If not, write to
19  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 #include "local.h"
22 #include <stdio.h>
23 #include <assert.h>
24 #include "lispmgrp.h"
25 #include "cstring.h"
26 
27 static	Boolean	lispKeymap_lookupWithStringFound	(TLispManager*, TLispEntity*, const Char*, int, Boolean, Boolean, int*, TLispEntity** const) ;
28 static	Boolean	lispKeymap_lookupWithVectorFound	(TLispManager*, TLispEntity*, TLispEntity**, int, Boolean, Boolean, int*, TLispEntity** const) ;
29 static	Boolean	lispKeymap_lookupWithListFound		(TLispManager*, TLispEntity*, TLispEntity*, Boolean, Boolean, int*, TLispEntity** const) ;
30 static	Boolean	lispKeymap_defineSubkey				(TLispManager*, const Char*, int, TLispEntity*, TLispEntity** const) ;
31 static	Boolean	lispKeymap_defineSubkeyWithVector	(TLispManager*, TLispEntity**, int, TLispEntity*, TLispEntity** const) ;
32 static	Boolean	lispKeymap_equalChar				(Char, Char) ;
33 
34 /*
35  *[����]
36  *	̩�� keymap �� (keymap [nil ... nil]) �η������Ƥ����ΤȤ��롣
37  *	[nil ... nil] �ϡ���Ȥ�Ȥ� char-table ���� 384 �Ĥ��ä��褦�˻פ�
38  *	�뤬��char-table �ϼ������Ƥ��ʤ���vector �����Ѥ��롣
39  */
40 Boolean
lispMgr_CreateKeymap(register TLispManager * pLispMgr,register TLispEntity * pString,register TLispEntity ** const ppEntReturn)41 lispMgr_CreateKeymap (
42 	register TLispManager*			pLispMgr,
43 	register TLispEntity*			pString,
44 	register TLispEntity** const	ppEntReturn)
45 {
46 	TLispEntity*	apCharTable [128] ;
47 	TLispEntity*	pNil ;
48 	TLispEntity**	ppEntity ;
49 	TLispEntity*	apNode [2] ;
50 	register Boolean	fRetval ;
51 	register int	nCounter, nNodes ;
52 
53 	lispMgr_CreateNil (pLispMgr, &pNil) ;
54 	ppEntity	= apCharTable ;
55 	nCounter	= NELEMENTS (apCharTable) ;
56 
57 	while (nCounter -- > 0)
58 		*ppEntity ++	= pNil ;
59 
60 	apNode [0]	= lispMgr_GetReservedEntity (pLispMgr, LISPMGR_INDEX_KEYMAP) ;
61 	lispEntity_AddRef (pLispMgr, apNode [0]) ;
62 	if (TFAILED (lispMgr_CreateVector (pLispMgr, apCharTable, NELEMENTS (apCharTable), &apNode [1]))) {
63 		lispEntity_Release (pLispMgr, apNode [0]) ;
64 		return	False ;
65 	}
66 	lispEntity_AddRef (pLispMgr, apNode [1]) ;
67 
68 	if (pString != NULL && TFAILED (lispEntity_Nullp (pLispMgr, pString))) {
69 		apNode [2]	= pString ;
70 		nNodes		= 3 ;
71 	} else {
72 		nNodes		= 2 ;
73 	}
74 	fRetval	= lispMgr_CreateList (pLispMgr, apNode, nNodes, ppEntReturn) ;
75 	lispEntity_Release (pLispMgr, apNode [1]) ;
76 	lispEntity_Release (pLispMgr, apNode [0]) ;
77 	return	fRetval ;
78 }
79 
80 /*
81  *	�¤ʥ����ޥåפ��������ؿ���
82  */
83 Boolean
lispMgr_CreateSparseKeymap(register TLispManager * pLispMgr,register TLispEntity * pEntString,register TLispEntity ** const ppEntReturn)84 lispMgr_CreateSparseKeymap (
85 	register TLispManager*			pLispMgr,
86 	register TLispEntity*			pEntString,
87 	register TLispEntity** const	ppEntReturn)
88 {
89 	TLispEntity*	apEntNode [2] ;
90 	register int	nNode ;
91 	register Boolean	fRetval ;
92 
93 	nNode			= 0 ;
94 	apEntNode [0]	= lispMgr_GetReservedEntity (pLispMgr, LISPMGR_INDEX_KEYMAP) ;
95 	lispEntity_AddRef (pLispMgr, apEntNode [nNode ++]) ;
96 	if (pEntString != NULL &&
97 		TFAILED (lispEntity_Nullp (pLispMgr, pEntString))) {
98 		lispEntity_AddRef (pLispMgr, pEntString) ;
99 		apEntNode [nNode ++]	= pEntString ;
100 	}
101 	fRetval	= lispMgr_CreateList (pLispMgr, apEntNode, nNode, ppEntReturn) ;
102 	while (nNode > 0)
103 		lispEntity_Release (pLispMgr, apEntNode [-- nNode]) ;
104 	return	fRetval ;
105 }
106 
107 Boolean
lispMgr_CopyKeymap(register TLispManager * pLispMgr,register TLispEntity * pEntKeymap,register TLispEntity ** const ppEntReturn)108 lispMgr_CopyKeymap (
109 	register TLispManager*			pLispMgr,
110 	register TLispEntity*			pEntKeymap,
111 	register TLispEntity** const	ppEntReturn)
112 {
113 	if (TFAILED (lispEntity_Keymapp (pLispMgr, pEntKeymap)))
114 		return	False ;
115 	return	lispEntity_Copy (pLispMgr, pEntKeymap, ppEntReturn) ;
116 }
117 
118 /*
119  *	�����ޥåפ��ɤ�����Ƚ�ꤹ��ؿ���
120  */
121 Boolean
lispEntity_Keymapp(register TLispManager * pLispMgr,register TLispEntity * pEntity)122 lispEntity_Keymapp (
123 	register TLispManager*	pLispMgr,
124 	register TLispEntity*	pEntity)
125 {
126 	TLispEntity*	pCar ;
127 
128 	assert (pLispMgr != NULL) ;
129 	assert (pEntity  != NULL) ;
130 
131 	if (TFAILED (lispEntity_GetCar (pLispMgr, pEntity, &pCar)))
132 		return	False ;
133 
134 	return	lispEntity_Eq (pLispMgr, pCar, lispMgr_GetReservedEntity (pLispMgr, LISPMGR_INDEX_KEYMAP)) ;
135 }
136 
137 /*
138  *	hEntity �� array �Ǥ��뤳�ȡ�array �� vector �� string �Ǥ��롣
139  *	pnMatch �ϲ�ʸ���ޥå����������֤�������ʸ���Dz��������ä����ϡ�phEntityRet
140  *	�����롣��Ȥ�Ȥ� lookup ���ȡ��������֤��������ˤʤ뤱�ɡ����줸�㤢��
141  *	�ټ�֤��ʡ��Ȥ���
142  */
143 Boolean
lispKeymap_Lookup(register TLispManager * pLispMgr,register TLispEntity * pKeymap,register TLispEntity * pEntity,register Boolean fAcceptDefault,register Boolean fNearMatch,register int * pnMatch,register TLispEntity ** const ppEntReturn)144 lispKeymap_Lookup (
145 	register TLispManager*			pLispMgr,
146 	register TLispEntity*			pKeymap,
147 	register TLispEntity*			pEntity,
148 	register Boolean				fAcceptDefault,
149 	register Boolean				fNearMatch,
150 	register int*					pnMatch,
151 	register TLispEntity** const	ppEntReturn)
152 {
153 	int		iType ;
154 
155 	assert (pLispMgr    != NULL) ;
156 	assert (pKeymap     != NULL) ;
157 	assert (pEntity     != NULL) ;
158 	assert (ppEntReturn != NULL) ;
159 
160 	if (TFAILED (lispEntity_GetType (pLispMgr, pEntity, &iType)))
161 		return	False ;
162 
163 	switch (iType) {
164 	case	LISPENTITY_STRING:
165 	{
166 		const Char*	pString ;
167 		int			nLength ;
168 
169 		(void) lispEntity_GetStringValue (pLispMgr, pEntity, &pString, &nLength) ;
170 		if (pString == NULL || nLength <= 0)
171 			break ;
172 		return	lispKeymap_LookupWithString (pLispMgr, pKeymap, pString, nLength, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
173 	}
174 
175 	case	LISPENTITY_VECTOR:
176 	{
177 		TLispEntity**	ppElement ;
178 		int				nElement ;
179 
180 		(void) lispEntity_GetVectorValue (pLispMgr, pEntity, &ppElement, &nElement) ;
181 		if (ppElement == NULL || nElement <= 0)
182 			break ;
183 		return	lispKeymap_LookupWithVector (pLispMgr, pKeymap, ppElement, nElement, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
184 	}
185 	case	LISPENTITY_CONSCELL:
186 	{
187 		if (TSUCCEEDED (lispEntity_Nullp (pLispMgr, pEntity)))
188 			break ;
189 		return	lispKeymap_LookupWithList (pLispMgr, pKeymap, pEntity, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
190 	}
191 	default:
192 		return	False ;
193 	}
194 	*pnMatch		= 0 ;
195 	*ppEntReturn	= pKeymap ;
196 	return	True ;
197 }
198 
199 Boolean
lispKeymap_LookupWithString(register TLispManager * pLispMgr,register TLispEntity * pKeymap,register const Char * pString,register int nString,register Boolean fAcceptDefault,register Boolean fNearMatch,register int * pnMatch,register TLispEntity ** const ppEntReturn)200 lispKeymap_LookupWithString (
201 	register TLispManager*			pLispMgr,
202 	register TLispEntity*			pKeymap,
203 	register const Char*			pString,
204 	register int					nString,
205 	register Boolean				fAcceptDefault,
206 	register Boolean				fNearMatch,
207 	register int*					pnMatch,
208 	register TLispEntity** const	ppEntReturn)
209 {
210 	TLispEntity*	pEntry ;
211 	TLispEntity*	pList ;
212 	TLispEntity*	pDefaultFunc	= NULL ;
213 	TLispEntity*	pEntNearHit ;
214 	Char			cc, ccNear ;
215 	int				iType ;
216 
217 	if (TFAILED (lispEntity_Keymapp (pLispMgr, pKeymap)) ||
218 		TFAILED (lispEntity_GetCdr (pLispMgr, pKeymap, &pList)))
219 		return	False ;
220 
221 	cc			= *pString ++ ;
222 	nString	-- ;
223 	ccNear	= (fNearMatch && Char_Charset (cc) == KCHARSET_XCHAR)?
224 		Char_MakeAscii (Char_Code (cc) & 0x7F) : -1 ;
225 	pEntNearHit	= NULL ;
226 
227 	while (TFAILED (lispEntity_Nullp (pLispMgr, pList))) {
228 		if (TFAILED (lispEntity_GetCar (pLispMgr, pList, &pEntry)))
229 			return	False ;
230 
231 		lispEntity_GetType (pLispMgr, pEntry, &iType) ;
232 		switch (iType) {
233 		case	LISPENTITY_CONSCELL:
234 		{
235 			TLispEntity*	pCar ;
236 			long			lValue ;
237 
238 			(void) lispEntity_GetCar (pLispMgr, pEntry, &pCar) ;
239 
240 			/*	ACCEPT-DEFAULT �����äƤ�����硣*/
241 			if (fAcceptDefault && TSUCCEEDED (lispEntity_Tp (pLispMgr, pCar))) {
242 				(void) lispEntity_GetCdr (pLispMgr, pEntry, &pDefaultFunc) ;
243 				assert (pDefaultFunc != NULL) ;
244 			}
245 
246 			if (TSUCCEEDED (lispEntity_GetIntegerValue (pLispMgr, pCar, &lValue)) &&
247 				(cc == (Char) lValue || (fNearMatch && ccNear == (Char) lValue))) {
248 				(void) lispEntity_GetCdr (pLispMgr, pEntry, &pEntry) ;
249 				return	lispKeymap_lookupWithStringFound (pLispMgr, pEntry, pString, nString, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
250 			}
251 			break ;
252 		}
253 
254 		case	LISPENTITY_VECTOR:
255 		{
256 			TLispEntity**	ppElement ;
257 			int				nElement ;
258 			register int	nIndex ;
259 
260 			(void) lispEntity_GetVectorValue (pLispMgr, pEntry, &ppElement, &nElement) ;
261 			nIndex	= (int) cc ;
262 			if (0 <= nIndex && nIndex < nElement) {
263 				pEntry		= *(ppElement + nIndex) ;
264 				return	lispKeymap_lookupWithStringFound (pLispMgr, pEntry, pString, nString, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
265 			}
266 			if (fNearMatch) {
267 				nIndex	= (int) ccNear ;
268 				if (pEntNearHit == NULL && 0 <= nIndex && nIndex < nElement)
269 					pEntNearHit	= *(ppElement + nIndex) ;
270 			}
271 			break ;
272 		}
273 
274 		/*	���ξ��ˤϡ�keymap ���Ĥʤ��äƤ��������ǤϤʤ��Τ�����
275 		 *	�פ���*/
276 		case	LISPENTITY_SYMBOL:
277 			if (TSUCCEEDED (lispEntity_Keymapp (pLispMgr, pList)))
278 				return	lispKeymap_LookupWithString (pLispMgr, pList, pString, nString, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
279 			break ;
280 
281 		default:
282 			break ;
283 		}
284 		if (TFAILED (lispEntity_GetCdr (pLispMgr, pList, &pList)))
285 			return	False ;
286 	}
287 
288 	/*	Exact Hit �˼��Ԥ������ˤ� Near Hit ��Ȥ���*/
289 	if (fNearMatch && pEntNearHit != NULL)
290 		return	lispKeymap_lookupWithStringFound (pLispMgr, pEntNearHit, pString, nString, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
291 
292 	/*	default keymap ��˺��Ƥ��뤾��(^^;; */
293 	++	*pnMatch ;
294 	if (pDefaultFunc != NULL) {
295 		*ppEntReturn	= pDefaultFunc ;
296 	} else {
297 		*ppEntReturn	= pList ;
298 	}
299 	return	True ;
300 }
301 
302 Boolean
lispKeymap_lookupWithStringFound(register TLispManager * pLispMgr,register TLispEntity * pEntKeymap,register const Char * pString,register int nString,register Boolean fAcceptDefault,register Boolean fNearMatch,register int * pnMatch,register TLispEntity ** const ppEntReturn)303 lispKeymap_lookupWithStringFound (
304 	register TLispManager*			pLispMgr,
305 	register TLispEntity*			pEntKeymap,
306 	register const Char*			pString,
307 	register int					nString,
308 	register Boolean				fAcceptDefault,
309 	register Boolean				fNearMatch,
310 	register int*					pnMatch,
311 	register TLispEntity** const	ppEntReturn)
312 {
313 	/*	�����ޤǤ��ʳ����֤��ͤϷ��ꤷ�Ƥ��롣*/
314 	++ *pnMatch ;
315 	/*	�ޤ�����Ƥߤ���ġ�*/
316 	if (nString > 0) {
317 		/*	�����ޥåפǤʤ���� False ����ä���롣
318 		 *	���λ��ϡֺ����ʳ��ޤ��������Ƥ���������פ�
319 		 *	�������Ȥˤʤ롣*/
320 		if (TFAILED (lispKeymap_LookupWithString (pLispMgr, pEntKeymap, pString, nString, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn)))
321 			*ppEntReturn	= pEntKeymap ;
322 	} else {
323 		*ppEntReturn	= pEntKeymap ;
324 	}
325 	return	True ;
326 }
327 
328 Boolean
lispKeymap_LookupWithVector(register TLispManager * pLispMgr,register TLispEntity * pKeymap,register TLispEntity ** ppElement,register int nElement,register Boolean fAcceptDefault,register Boolean fNearMatch,register int * pnMatch,register TLispEntity ** const ppEntReturn)329 lispKeymap_LookupWithVector (
330 	register TLispManager*			pLispMgr,
331 	register TLispEntity*			pKeymap,
332 	register TLispEntity**			ppElement,
333 	register int					nElement,
334 	register Boolean				fAcceptDefault,
335 	register Boolean				fNearMatch,
336 	register int*					pnMatch,
337 	register TLispEntity** const	ppEntReturn)
338 {
339 	TLispEntity*	pEntry ;
340 	TLispEntity*	pList ;
341 	TLispEntity*	pDefaultFunc	= NULL ;
342 	TLispEntity*	pElement ;
343 	TLispEntity*	pEntNearHit ;
344 	long			lValue, lValueNear ;
345 	int				iType ;
346 
347 	assert (pLispMgr    != NULL) ;
348 	assert (pnMatch     != NULL) ;
349 	assert (ppEntReturn != NULL) ;
350 
351 	if (TFAILED (lispEntity_Keymapp (pLispMgr, pKeymap)) ||
352 		TFAILED (lispEntity_GetCdr (pLispMgr, pKeymap, &pList)))
353 		return	False ;
354 
355 	assert (ppElement != NULL && nElement > 0) ;
356 	pElement	= *ppElement ++ ;
357 	nElement	-- ;
358 
359 	if (TFAILED (lispEntity_GetIntegerValue (pLispMgr, pElement, &lValue)))
360 		lValue	= -1 ;
361 	lValueNear	= (fNearMatch && Char_Charset ((Char) lValue) == KCHARSET_XCHAR)?
362 		Char_MakeAscii (Char_Code ((Char)lValue) & 0x7F) : -1 ;
363 	pEntNearHit	= NULL ;
364 
365 	while (TFAILED (lispEntity_Nullp (pLispMgr, pList))) {
366 		if (TFAILED (lispEntity_GetCar (pLispMgr, pList, &pEntry)))
367 			return	False ;
368 
369 		lispEntity_GetType (pLispMgr, pEntry, &iType) ;
370 		switch (iType) {
371 		case	LISPENTITY_CONSCELL:
372 		{
373 			TLispEntity*	pCar ;
374 			long			lCar ;
375 
376 			(void) lispEntity_GetCar (pLispMgr, pEntry, &pCar) ;
377 
378 			/*	ACCEPT-DEFAULT �����äƤ�����硣*/
379 			if (fAcceptDefault && TSUCCEEDED (lispEntity_Tp (pLispMgr, pCar))) {
380 				(void) lispEntity_GetCdr (pLispMgr, pEntry, &pDefaultFunc) ;
381 				assert (pDefaultFunc != NULL) ;
382 			}
383 
384 			if (TSUCCEEDED (lispEntity_Eq (pLispMgr, pCar, pElement)) ||
385 				(lValue >= 0 &&
386 				 TSUCCEEDED (lispEntity_GetIntegerValue (pLispMgr, pCar, &lCar)) &&
387 				 (lValue == lCar || (fNearMatch && lValueNear == lCar)))) {
388 				(void) lispEntity_GetCdr (pLispMgr, pEntry, &pEntry) ;
389 				return	lispKeymap_lookupWithVectorFound (pLispMgr, pEntry, ppElement, nElement, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
390 			}
391 			break ;
392 		}
393 
394 		case	LISPENTITY_VECTOR:
395 		{
396 			TLispEntity**	ppLcElement ;
397 			int				nLcElement ;
398 
399 			(void) lispEntity_GetVectorValue (pLispMgr, pEntry, &ppLcElement, &nLcElement) ;
400 			if (0 <= lValue && lValue < nLcElement) {
401 				pEntry		= *(ppLcElement + lValue) ;
402 				return	lispKeymap_lookupWithVectorFound (pLispMgr, pEntry, ppElement, nElement, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
403 			}
404 			if (fNearMatch) {
405 				if (pEntNearHit == NULL && 0 <= lValueNear && lValueNear < nLcElement)
406 					pEntNearHit	= *(ppLcElement + lValueNear) ;
407 			}
408 			break ;
409 		}
410 
411 		/*	���ξ��ˤϡ�keymap ���Ĥʤ��äƤ��������ǤϤʤ��Τ�����
412 		 *	�פ���*/
413 		case	LISPENTITY_SYMBOL:
414 			if (TSUCCEEDED (lispEntity_Keymapp (pLispMgr, pList)))
415 				return	lispKeymap_LookupWithVector (pLispMgr, pList, ppElement, nElement, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
416 			break ;
417 
418 		default:
419 			break ;
420 		}
421 		if (TFAILED (lispEntity_GetCdr (pLispMgr, pList, &pList)))
422 			return	False ;
423 	}
424 
425 	if (fNearMatch && pEntNearHit != NULL)
426 		return	lispKeymap_lookupWithVectorFound (pLispMgr, pEntNearHit, ppElement, nElement, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
427 
428 	/*	default keymap ��˺��Ƥ��뤾��(^^;; */
429 	++	*pnMatch ;
430 	if (pDefaultFunc != NULL) {
431 		*ppEntReturn	= pDefaultFunc ;
432 	} else {
433 		*ppEntReturn	= pList ;
434 	}
435 	return	True ;
436 }
437 
438 Boolean
lispKeymap_lookupWithVectorFound(register TLispManager * pLispMgr,register TLispEntity * pEntKeymap,register TLispEntity ** ppElement,register int nElement,register Boolean fAcceptDefault,register Boolean fNearMatch,register int * pnMatch,register TLispEntity ** const ppEntReturn)439 lispKeymap_lookupWithVectorFound (
440 	register TLispManager*			pLispMgr,
441 	register TLispEntity*			pEntKeymap,
442 	register TLispEntity**			ppElement,
443 	register int					nElement,
444 	register Boolean				fAcceptDefault,
445 	register Boolean				fNearMatch,
446 	register int*					pnMatch,
447 	register TLispEntity** const	ppEntReturn)
448 {
449 	/*	�����ޤǤ��ʳ����֤��ͤϷ��ꤷ�Ƥ��롣*/
450 	++ *pnMatch ;
451 	/*	�ޤ�����Ƥߤ���ġ�*/
452 	if (nElement > 0) {
453 		/*	�����ޥåפǤʤ���� False ����ä���롣
454 		 *	���λ��ϡֺ����ʳ��ޤ��������Ƥ���������פ�
455 		 *	�������Ȥˤʤ롣*/
456 		if (TFAILED (lispKeymap_LookupWithVector (pLispMgr, pEntKeymap, ppElement, nElement, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn)))
457 			*ppEntReturn	= pEntKeymap ;
458 	} else {
459 		*ppEntReturn	= pEntKeymap ;
460 	}
461 	return	True ;
462 }
463 
464 Boolean
lispKeymap_LookupWithList(register TLispManager * pLispMgr,register TLispEntity * pKeymap,register TLispEntity * pKeyList,register Boolean fAcceptDefault,register Boolean fNearMatch,register int * pnMatch,register TLispEntity ** const ppEntReturn)465 lispKeymap_LookupWithList (
466 	register TLispManager*			pLispMgr,
467 	register TLispEntity*			pKeymap,
468 	register TLispEntity*			pKeyList,
469 	register Boolean				fAcceptDefault,
470 	register Boolean				fNearMatch,
471 	register int*					pnMatch,
472 	register TLispEntity** const	ppEntReturn)
473 {
474 	TLispEntity*	pEntry ;
475 	TLispEntity*	pList ;
476 	TLispEntity*	pDefaultFunc	= NULL ;
477 	TLispEntity*	pElement ;
478 	TLispEntity*	pNextKeyList ;
479 	TLispEntity*	pEntNearHit ;
480 	long			lValue, lValueNear ;
481 	int				iType ;
482 
483 	assert (pLispMgr    != NULL) ;
484 	assert (pnMatch     != NULL) ;
485 	assert (ppEntReturn != NULL) ;
486 	assert (pKeyList != NULL) ;
487 
488 	if (TFAILED (lispEntity_Keymapp (pLispMgr, pKeymap)) ||
489 		TFAILED (lispEntity_GetCdr (pLispMgr, pKeymap, &pList)) ||
490 		TSUCCEEDED (lispEntity_Nullp (pLispMgr, pKeyList)))
491 		return	False ;
492 
493 	if (TFAILED (lispEntity_GetCar (pLispMgr, pKeyList, &pElement)) ||
494 		TFAILED (lispEntity_GetCdr (pLispMgr, pKeyList, &pNextKeyList)))
495 		return	False ;
496 	pKeyList	= pNextKeyList ;
497 
498 	if (TFAILED (lispEntity_GetIntegerValue (pLispMgr, pElement, &lValue)))
499 		lValue	= -1 ;
500 	lValueNear	= (fNearMatch && Char_Charset ((Char) lValue) == KCHARSET_XCHAR)?
501 		Char_MakeAscii (Char_Code ((Char)lValue) & 0x7F) : -1 ;
502 	pEntNearHit	= NULL ;
503 
504 	while (TFAILED (lispEntity_Nullp (pLispMgr, pList))) {
505 		if (TFAILED (lispEntity_GetCar (pLispMgr, pList, &pEntry)))
506 			return	False ;
507 
508 		lispEntity_GetType (pLispMgr, pEntry, &iType) ;
509 		switch (iType) {
510 		case	LISPENTITY_CONSCELL:
511 		{
512 			TLispEntity*	pCar ;
513 			long			lCar ;
514 
515 			(void) lispEntity_GetCar (pLispMgr, pEntry, &pCar) ;
516 
517 			/*	ACCEPT-DEFAULT �����äƤ�����硣*/
518 			if (fAcceptDefault && TSUCCEEDED (lispEntity_Tp (pLispMgr, pCar))) {
519 				(void) lispEntity_GetCdr (pLispMgr, pEntry, &pDefaultFunc) ;
520 				assert (pDefaultFunc != NULL) ;
521 			}
522 
523 			if (TSUCCEEDED (lispEntity_Eq (pLispMgr, pCar, pElement)) ||
524 				(lValue >= 0 &&
525 				 TSUCCEEDED (lispEntity_GetIntegerValue (pLispMgr, pCar, &lCar)) &&
526 				 (lValue == lCar || (fNearMatch && lValueNear == lCar)))) {
527 				(void) lispEntity_GetCdr (pLispMgr, pEntry, &pEntry) ;
528 				return	lispKeymap_lookupWithListFound (pLispMgr, pEntry, pKeyList, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
529 			}
530 			break ;
531 		}
532 
533 		case	LISPENTITY_VECTOR:
534 		{
535 			TLispEntity**	ppLcElement ;
536 			int				nLcElement ;
537 
538 			(void) lispEntity_GetVectorValue (pLispMgr, pEntry, &ppLcElement, &nLcElement) ;
539 			if (0 <= lValue && lValue < nLcElement) {
540 				pEntry		= *(ppLcElement + lValue) ;
541 				return	lispKeymap_lookupWithListFound (pLispMgr, pEntry, pKeyList, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
542 			}
543 			if (fNearMatch) {
544 				if (pEntNearHit == NULL && 0 <= lValueNear && lValueNear < nLcElement)
545 					pEntNearHit	= *(ppLcElement + lValueNear) ;
546 			}
547 			break ;
548 		}
549 
550 		/*	���ξ��ˤϡ�keymap ���Ĥʤ��äƤ��������ǤϤʤ��Τ�����
551 		 *	�פ���*/
552 		case	LISPENTITY_SYMBOL:
553 			if (TSUCCEEDED (lispEntity_Keymapp (pLispMgr, pList)))
554 				return	lispKeymap_LookupWithList (pLispMgr, pList, pKeyList, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
555 			break ;
556 
557 		default:
558 			break ;
559 		}
560 		if (TFAILED (lispEntity_GetCdr (pLispMgr, pList, &pList)))
561 			return	False ;
562 	}
563 
564 	if (fNearMatch && pEntNearHit != NULL)
565 		return	lispKeymap_lookupWithListFound (pLispMgr, pEntNearHit, pKeyList, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn) ;
566 
567 	/*	default keymap ��˺��Ƥ��뤾��(^^;; */
568 	++	*pnMatch ;
569 	if (pDefaultFunc != NULL) {
570 		*ppEntReturn	= pDefaultFunc ;
571 	} else {
572 		*ppEntReturn	= pList ;
573 	}
574 	return	True ;
575 }
576 
577 Boolean
lispKeymap_lookupWithListFound(register TLispManager * pLispMgr,register TLispEntity * pEntKeymap,register TLispEntity * pEntKeyList,register Boolean fAcceptDefault,register Boolean fNearMatch,register int * pnMatch,register TLispEntity ** const ppEntReturn)578 lispKeymap_lookupWithListFound (
579 	register TLispManager*			pLispMgr,
580 	register TLispEntity*			pEntKeymap,
581 	register TLispEntity*			pEntKeyList,
582 	register Boolean				fAcceptDefault,
583 	register Boolean				fNearMatch,
584 	register int*					pnMatch,
585 	register TLispEntity** const	ppEntReturn)
586 {
587 	/*	�����ޤǤ��ʳ����֤��ͤϷ��ꤷ�Ƥ��롣*/
588 	++ *pnMatch ;
589 	/*	�ޤ�����Ƥߤ���ġ�*/
590 	if (TFAILED (lispEntity_Nullp (pLispMgr, pEntKeyList))) {
591 		/*	�����ޥåפǤʤ���� False ����ä���롣
592 		 *	���λ��ϡֺ����ʳ��ޤ��������Ƥ���������פ�
593 		 *	�������Ȥˤʤ롣*/
594 		if (TFAILED (lispKeymap_LookupWithList (pLispMgr, pEntKeymap, pEntKeyList, fAcceptDefault, fNearMatch, pnMatch, ppEntReturn)))
595 			*ppEntReturn	= pEntKeymap ;
596 	} else {
597 		*ppEntReturn	= pEntKeymap ;
598 	}
599 	return	True ;
600 }
601 
602 Boolean
lispKeymap_DefineKeyWithVector(register TLispManager * pLispMgr,register TLispEntity * pEntKeymap,register TLispEntity ** ppEntKeySeq,register int nKeySeq,register TLispEntity * pEntBind)603 lispKeymap_DefineKeyWithVector (
604 	register TLispManager*	pLispMgr,
605 	register TLispEntity*	pEntKeymap,
606 	register TLispEntity**	ppEntKeySeq,
607 	register int			nKeySeq,
608 	register TLispEntity*	pEntBind)
609 {
610 	TLispEntity*	pEntKeylist ;
611 	TLispEntity*	pEntKey ;
612 	TLispEntity*	pEntNextKeylist ;
613 	TLispEntity*	pEntSubkeymap ;
614 
615 	pEntKeylist	= pEntKeymap ;
616 	if (TFAILED (lispEntity_Keymapp (pLispMgr, pEntKeylist)))
617 		return	False ;
618 
619 	while (TFAILED (lispEntity_Nullp (pLispMgr, pEntKeylist))) {
620 		if (TFAILED (lispEntity_GetCar (pLispMgr, pEntKeylist, &pEntKey)))
621 			break ;
622 		if (TSUCCEEDED (lispEntity_Vectorp  (pLispMgr, pEntKey)) &&
623 			TSUCCEEDED (lispEntity_Integerp (pLispMgr, *ppEntKeySeq))) {
624 			int		nLength ;
625 			long	nIndex ;
626 			lispEntity_GetLength (pLispMgr, pEntKey, &nLength) ;
627 			lispEntity_GetIntegerValue (pLispMgr, *ppEntKeySeq, &nIndex) ;
628 			if (0 <= nIndex && nIndex < nLength) {
629 				ppEntKeySeq	++ ;
630 				nKeySeq	-- ;
631 				if (nKeySeq == 0)
632 					return	lispEntity_SetVectorElement (pLispMgr, pEntKey, nIndex, pEntBind) ;
633 				lispEntity_GetVectorElement (pLispMgr, pEntKey, nIndex, &pEntNextKeylist) ;
634 				if (TFAILED (lispEntity_Keymapp (pLispMgr, pEntNextKeylist))) {
635 					if (TFAILED (lispKeymap_defineSubkeyWithVector (pLispMgr, ppEntKeySeq, nKeySeq, pEntBind, &pEntSubkeymap)))
636 						return	False ;
637 					return	lispEntity_SetVectorElement (pLispMgr, pEntKey, nIndex, pEntSubkeymap) ;
638 				}
639 			}
640 		} else if (TSUCCEEDED (lispEntity_Consp  (pLispMgr, pEntKey))) {
641 			TLispEntity*	pEntKeyCode ;
642 
643 			lispEntity_GetCar (pLispMgr, pEntKey, &pEntKeyCode) ;
644 			if (pEntKeyCode == *ppEntKeySeq) {
645 				ppEntKeySeq	++ ;
646 				nKeySeq	-- ;
647 				/*	���ξ�꤬����С������ΥХ���ɤ��֤������롣*/
648 				if (nKeySeq == 0)
649 					return	lispEntity_SetCdr (pLispMgr, pEntKey, pEntBind) ;
650 				/*	�����ޥåפ��롣*/
651 				lispEntity_GetCdr (pLispMgr, pEntKey, &pEntNextKeylist) ;
652 				if (TFAILED (lispEntity_Keymapp (pLispMgr, pEntNextKeylist)))
653 					break ;
654 				pEntKeylist	= pEntNextKeylist ;
655 			}
656 		}
657 		lispEntity_GetCdr (pLispMgr, pEntKeylist, &pEntNextKeylist) ;
658 		pEntKeylist	= pEntNextKeylist ;
659 	}
660 	/*
661 	 *	�ʤ�ۤɡ��ꥹ�Ȥ���Ƭ���ɲäȤ����櫓����
662 	 */
663 	lispEntity_GetCdr (pLispMgr, pEntKeymap, &pEntKeylist) ;
664 	if (TFAILED (lispKeymap_defineSubkeyWithVector (pLispMgr, ppEntKeySeq, nKeySeq, pEntBind, &pEntSubkeymap)))
665 		return	False ;
666 	lispEntity_AddRef (pLispMgr, pEntSubkeymap) ;
667 	if (TFAILED (lispMgr_CreateConscell (pLispMgr, pEntSubkeymap, pEntKeylist, &pEntKeylist)))
668 		return	False ;
669 	lispEntity_SetCdr (pLispMgr, pEntKeymap, pEntKeylist) ;
670 	lispEntity_Release (pLispMgr, pEntSubkeymap) ;
671 	return	True ;
672 }
673 
674 /*
675  *
676  */
677 Boolean
lispKeymap_DefineKey(register TLispManager * pLispMgr,register TLispEntity * pEntKeymap,register const Char * pString,register int nString,register TLispEntity * pEntBind)678 lispKeymap_DefineKey (
679 	register TLispManager*	pLispMgr,
680 	register TLispEntity*	pEntKeymap,
681 	register const Char*	pString,
682 	register int			nString,
683 	register TLispEntity*	pEntBind)
684 {
685 	TLispEntity*	pEntKeylist ;
686 	TLispEntity*	pEntKey ;
687 	TLispEntity*	pEntNextKeylist ;
688 	TLispEntity*	pEntSubkeymap ;
689 
690 	pEntKeylist	= pEntKeymap ;
691 	if (TFAILED (lispEntity_Keymapp (pLispMgr, pEntKeylist)))
692 		return	False ;
693 
694 	while (TFAILED (lispEntity_Nullp (pLispMgr, pEntKeylist))) {
695 		if (TFAILED (lispEntity_GetCar (pLispMgr, pEntKeylist, &pEntKey)))
696 			break ;
697 		if (TSUCCEEDED (lispEntity_Vectorp (pLispMgr, pEntKey))) {
698 			int	nLength, nIndex ;
699 			lispEntity_GetLength (pLispMgr, pEntKey, &nLength) ;
700 			nIndex	= (int) *pString ;
701 			if (0 <= nIndex && nIndex < nLength) {
702 				pString	++ ;
703 				nString	-- ;
704 				if (nString == 0)
705 					return	lispEntity_SetVectorElement (pLispMgr, pEntKey, nIndex, pEntBind) ;
706 
707 				lispEntity_GetVectorElement (pLispMgr, pEntKey, nIndex, &pEntNextKeylist) ;
708 				if (TFAILED (lispEntity_Keymapp (pLispMgr, pEntNextKeylist))) {
709 					if (TFAILED (lispKeymap_defineSubkey (pLispMgr, pString, nString, pEntBind, &pEntSubkeymap)))
710 						return	False ;
711 					return	lispEntity_SetVectorElement (pLispMgr, pEntKey, nIndex, pEntSubkeymap) ;
712 				}
713 			}
714 		} else if (TSUCCEEDED (lispEntity_Consp  (pLispMgr, pEntKey))) {
715 			TLispEntity*	pEntKeyCode ;
716 			long			lCode ;
717 
718 			lispEntity_GetCar (pLispMgr, pEntKey, &pEntKeyCode) ;
719 			if (TSUCCEEDED (lispEntity_GetIntegerValue (pLispMgr, pEntKeyCode, &lCode)) &&
720 				(Char)lCode == *pString) {
721 				pString	++ ;
722 				nString	-- ;
723 				/*	���ξ�꤬����С������ΥХ���ɤ��֤������롣*/
724 				if (nString == 0)
725 					return	lispEntity_SetCdr (pLispMgr, pEntKey, pEntBind) ;
726 				/*	�����ޥåפ��롣*/
727 				lispEntity_GetCdr (pLispMgr, pEntKey, &pEntNextKeylist) ;
728 				if (TFAILED (lispEntity_Keymapp (pLispMgr, pEntNextKeylist)))
729 					break ;
730 				pEntKeylist	= pEntNextKeylist ;
731 			}
732 		}
733 		lispEntity_GetCdr (pLispMgr, pEntKeylist, &pEntNextKeylist) ;
734 		pEntKeylist	= pEntNextKeylist ;
735 	}
736 	/*
737 	 *	�ʤ�ۤɡ��ꥹ�Ȥ���Ƭ���ɲäȤ����櫓����
738 	 */
739 	lispEntity_GetCdr (pLispMgr, pEntKeymap, &pEntKeylist) ;
740 	if (TFAILED (lispKeymap_defineSubkey (pLispMgr, pString, nString, pEntBind, &pEntSubkeymap)))
741 		return	False ;
742 	lispEntity_AddRef (pLispMgr, pEntSubkeymap) ;
743 	if (TFAILED (lispMgr_CreateConscell (pLispMgr, pEntSubkeymap, pEntKeylist, &pEntKeylist)))
744 		return	False ;
745 	lispEntity_SetCdr (pLispMgr, pEntKeymap, pEntKeylist) ;
746 	lispEntity_Release (pLispMgr, pEntSubkeymap) ;
747 	return	True ;
748 }
749 
750 /*	private functions */
751 
752 Boolean
lispKeymap_defineSubkey(register TLispManager * pLispMgr,register const Char * pString,register int nString,register TLispEntity * pEntBind,register TLispEntity ** const ppEntRetval)753 lispKeymap_defineSubkey (
754 	register TLispManager*			pLispMgr,
755 	register const Char*			pString,
756 	register int					nString,
757 	register TLispEntity*			pEntBind,
758 	register TLispEntity** const	ppEntRetval)
759 {
760 	TLispEntity*	pEntKeyCode ;
761 	TLispEntity*	pEntKeymap ;
762 	TLispEntity*	pEntKeylist ;
763 	TLispEntity*	pEntNextKeylist ;
764 	const Char*	ptr ;
765 
766 	assert (pString != NULL && nString > 0) ;
767 
768 	ptr	= pString + nString - 1 ;
769 	if (TFAILED (lispMgr_CreateInteger      (pLispMgr, *ptr, &pEntKeyCode)))
770 		return	False ;
771 	lispEntity_AddRef  (pLispMgr, pEntKeyCode) ;
772 	if (TFAILED (lispMgr_CreateConscell     (pLispMgr, pEntKeyCode, pEntBind, &pEntKeylist)))
773 		return	False ;
774 	lispEntity_AddRef  (pLispMgr, pEntKeylist) ;
775 	lispEntity_Release (pLispMgr, pEntKeyCode) ;
776 	ptr		-- ;
777 	nString -- ;
778 	while (nString > 0) {
779 		if (TFAILED (lispMgr_CreateInteger      (pLispMgr, *ptr, &pEntKeyCode)))
780 			return	False ;
781 		lispEntity_AddRef  (pLispMgr, pEntKeyCode) ;
782 		if (TFAILED (lispMgr_CreateSparseKeymap (pLispMgr, pEntKeylist, &pEntKeymap)))
783 			return	False ;
784 		lispEntity_AddRef  (pLispMgr, pEntKeymap) ;
785 		if (TFAILED (lispMgr_CreateConscell     (pLispMgr, pEntKeyCode, pEntKeymap, &pEntNextKeylist)))
786 			return	False ;
787 		lispEntity_AddRef  (pLispMgr, pEntNextKeylist) ;
788 		lispEntity_Release (pLispMgr, pEntKeyCode) ;
789 		lispEntity_Release (pLispMgr, pEntKeymap) ;
790 		lispEntity_Release (pLispMgr, pEntKeylist) ;
791 		pEntKeylist	= pEntNextKeylist ;
792 		ptr		-- ;
793 		nString	-- ;
794 	}
795 	lispEntity_Release (pLispMgr, pEntKeylist) ;
796 	*ppEntRetval	= pEntKeylist ;
797 	return	True ;
798 }
799 
800 Boolean
lispKeymap_defineSubkeyWithVector(register TLispManager * pLispMgr,register TLispEntity ** ppEntKeySeq,register int nKeySeq,register TLispEntity * pEntBind,register TLispEntity ** const ppEntRetval)801 lispKeymap_defineSubkeyWithVector (
802 	register TLispManager*			pLispMgr,
803 	register TLispEntity**			ppEntKeySeq,
804 	register int					nKeySeq,
805 	register TLispEntity*			pEntBind,
806 	register TLispEntity** const	ppEntRetval)
807 {
808 	TLispEntity*	pEntKeymap ;
809 	TLispEntity*	pEntKeylist ;
810 	TLispEntity*	pEntNextKeylist ;
811 
812 	assert (ppEntKeySeq != NULL && nKeySeq > 0) ;
813 
814 	ppEntKeySeq	+= nKeySeq - 1 ;
815 	if (TFAILED (lispMgr_CreateConscell (pLispMgr, *ppEntKeySeq, pEntBind, &pEntKeylist)))
816 		return	False ;
817 	lispEntity_AddRef  (pLispMgr, pEntKeylist) ;
818 	ppEntKeySeq	-- ;
819 	nKeySeq	-- ;
820 	while (nKeySeq > 0) {
821 		if (TFAILED (lispMgr_CreateSparseKeymap (pLispMgr, pEntKeylist, &pEntKeymap)))
822 			return	False ;
823 		lispEntity_AddRef  (pLispMgr, pEntKeymap) ;
824 		if (TFAILED (lispMgr_CreateConscell     (pLispMgr, *ppEntKeySeq, pEntKeymap, &pEntNextKeylist)))
825 			return	False ;
826 		lispEntity_AddRef  (pLispMgr, pEntNextKeylist) ;
827 		lispEntity_Release (pLispMgr, pEntKeymap) ;
828 		lispEntity_Release (pLispMgr, pEntKeylist) ;
829 		pEntKeylist	= pEntNextKeylist ;
830 		ppEntKeySeq	-- ;
831 		nKeySeq	-- ;
832 	}
833 	lispEntity_Release (pLispMgr, pEntKeylist) ;
834 	*ppEntRetval	= pEntKeylist ;
835 	return	True ;
836 }
837 
838 Boolean
lispKeymap_equalChar(register Char ccKey,register Char ccKeymap)839 lispKeymap_equalChar (
840 	register Char		ccKey,
841 	register Char		ccKeymap)
842 {
843 	if (ccKey == ccKeymap)
844 		return	True ;
845 	if (Char_Charset (ccKey) == KCHARSET_XCHAR)
846 		ccKey	= Char_MakeAscii (Char_Code (ccKey) & 0x7F) ;
847 	return	ccKey == ccKeymap ;
848 }
849 
850