1 /*--------------------------------------------------------------------
2   lineedit module.
3 
4     Copyright (c) 1998,1999,2000 SASAKI Shunsuke.
5     All rights reserved.
6 --------------------------------------------------------------------*/
7 #include "ed.h"
8 #include "sh.h"
9 #include <ctype.h>
10 
11 
12 /* ����lineedit������ */
le_getcsx(le_t * lep)13 int 	le_getcsx(le_t *lep)	// local�ؿ�
14 {
15 	int 	csx,i;
16 
17 	csx=0;
18 	for (i=0; i< lep->lx;)
19 		{
20 		 if (lep->buf[i]=='\0')
21 		 	break;
22 		 csx+= kanji_countdsp(lep->buf[i], csx);
23 		 i=kanji_posnext(i, lep->buf);
24 		}
25 	csx+= lep->lx-i;
26 	return csx;
27 }
28 
29 #define	N_scr	8
30 
le_setlx(le_t * lep,int lx)31 void	le_setlx(le_t *lep,int lx)
32 {
33 	int 	csx;
34 
35 	lep->lx=kanji_poscanon(lx, lep->buf);
36 
37 	csx=le_getcsx(lep);
38 	if (csx==lep->cx+ lep->sx)
39 		return;
40 
41 	if (csx<=lep->sx)		// ����������֤�꺸��
42 		lep->sx=(csx-1)/N_scr*N_scr; else
43 		{
44 		 if (csx>=lep->sx+lep->dsize-1)	// ����������֤�걦��
45 		 	lep->sx=((csx- lep->dsize)/N_scr+1)*N_scr;
46 		}
47 	lep->cx=csx- lep->sx;
48 //term_locate(2,2);term_printf("%d %d:%d",csx,lep->sx,lep->cx);term_clrtoe();
49 }
50 
le_csrleftside(le_t * lep)51 void	le_csrleftside(le_t *lep)
52 {
53 	le_setlx(lep,0);
54 }
55 
le_csrrightside(le_t * lep)56 void	le_csrrightside(le_t *lep)
57 {
58 	le_setlx(lep,strlen(lep->buf));
59 }
60 
le_csrleft(le_t * lep)61 void	le_csrleft(le_t *lep)
62 {
63 	if (lep->lx>0)
64 		le_setlx(lep,lep->lx-1);
65 }
66 
le_csrright(le_t * lep)67 void	le_csrright(le_t *lep)
68 {
69 	if (lep->lx<strlen(lep->buf))
70 		le_setlx(lep, lep->lx+1+ (IsThisKanjiPosition(lep->lx,lep->buf)?1:0));
71 }
72 
73 
74 
le_edit(le_t * lep,int ch,int cm)75 void	le_edit(le_t *lep,int ch,int cm)
76 {
77 	int 	i;
78 	int 	strlength;
79 	void	*p;
80 
81 	strlength=strlen(lep->buf);
82 	if (!sysinfo.freecursorf)
83 		{
84 		 if (lep->lx >strlength)
85 		 	lep->lx=strlength;
86 		} else
87 		{
88 		 if (strlength< lep->lx)
89 		 	{
90 		 	 for (i=strlength;i< lep->lx;++i)
91 		 	 	lep->buf[i] = ' ';
92 		 	 lep->buf[i] = '\0';
93 		 	 strlength = i;
94 		 	}
95 		}
96 
97 	switch(cm)
98 		{
99 	 case BACKSPACE:
100 	 	 if (lep->lx <=0)
101 	 	 	break;
102 	 	 le_csrleft(lep);
103 	 case DELETE:
104 	 	 if (lep->lx < strlength)
105 	 	 	{
106 			 p=lep->buf+lep->lx;
107 	 	 	 if (IsThisKanjiPosition(lep->lx,lep->buf))
108 	 	 	 	memmove(p,p+2, strlength- lep->lx-1); else
109 	 	 	 	memmove(p,p+1, strlength- lep->lx);
110 	 	 	}
111 	 	 break;
112 	 default:
113 		 if (strlength< lep->size)
114 		 	{
115 			 p=lep->buf+lep->lx;
116 	 	 	 if (ch>=0x8e00)
117 	 	 	 	{
118 	 	 	 	 memmove(p+1,p, strlength- lep->lx+1);
119 	 	 	 	 *(char *)p =ch>>8;
120 	 	 	 	 ++p;
121 	 	 	 	}
122 	 	 	 memmove(p+1,p, strlength- lep->lx+1);
123 	 	 	 *(char *)p =ch&0xff;
124 
125 	 	 	 le_csrright(lep);
126 		 	}
127 		}
128 }
129 
le_regbuf(const char * s,char * t)130 size_t	le_regbuf(const char *s,char *t)
131 {
132 	int 	n,a;
133 
134 	for (n=0;n<MAXEDITLINE;++s)
135 		{
136 		 if (*s=='\0')
137 		 	break;
138 		 if (*s=='\t')
139 		 	{
140 		 	 char	c;
141 
142 		 	 c=sysinfo.tabcode;
143 		 	 t[n]=sysinfo.tabmarkf&& c!=-1?c:' ';
144 		 	 a= (n/sysinfo.tabstop+1)*sysinfo.tabstop;
145 		 	 for (;n<a;++n)
146 		 	 	t[n+1]=' ';
147 		 	 continue;
148 		 	}
149 		 if (iscnt(*s))
150 		 	{
151 		 	 t[n++]= '^';
152 		 	 t[n++]= *s+'@';
153 		 	 continue;
154 		 	}
155 		 t[n++]= *s;
156 		}
157 	t[n]='\0';
158 	return n;
159 }
160 
161 
162 	/* legets ������ */
163 
legets_hist(le_t * lep,int hn,int hy)164 void	legets_hist(le_t *lep,int hn,int hy)
165 {
166 	int 	tmpListNo;
167 	EditLine	*ed;
168 
169 	tmpListNo = CurrentFileNo;
170 	CurrentFileNo = hn;
171 
172 	ed=GetList(hy+1);
173 	lep->lx=0;
174 	lep->sx=0;
175 
176 	if (ed==NULL||ed->buffer==NULL)			//buffer
177 		*lep->buf='\0'; else
178 		{
179 		 strcpy(lep->buf, ed->buffer);		//buffer
180 		 le_csrrightside(lep);
181 		}
182 
183 	CurrentFileNo=tmpListNo;
184 
185 }
186 
legets_histprev(le_t * lep,int hn,int * hy)187 bool	legets_histprev(le_t *lep,int hn,int *hy)
188 {
189 	if (hn==-1 || *hy<=0)
190 		return FALSE;
191 
192 	--*hy;
193 	legets_hist(lep,hn,*hy);
194 	return TRUE;
195 }
196 
legets_histnext(le_t * lep,int hn,int * hy)197 bool	legets_histnext(le_t *lep,int hn,int *hy)
198 {
199 	int 	tmpListNo;
200 	int 	hym;
201 
202 	tmpListNo = CurrentFileNo;
203 	CurrentFileNo = hn;
204 	hym=GetLastNumber();
205 	CurrentFileNo=tmpListNo;
206 
207 	if (hn==-1)
208 		return FALSE;
209 
210 	++*hy;
211 	if (*hy>=hym)
212 		{
213 		 *hy=hym;
214 		 *lep->buf='\0';
215 		 return TRUE;
216 		}
217 
218 	legets_hist(lep,hn,*hy);
219 	return TRUE;
220 }
221 
222 le_t	*legets_lep;	// !!����Ū
223 
dspreg_legets(void * vp,int a,int sizex,int sizey)224 dspfmt_t	*dspreg_legets(void *vp,int a,int sizex,int sizey)
225 {
226 	dspfmt_t	*dfp,*dfpb;
227 	char	buf[MAXEDITLINE+1];
228 	int 	n;
229 
230 
231 	dfpb=dfp=dsp_fmtinit((char *)vp, NULL);
232 	dfp->col=sysinfo.c_sysmsg;
233 
234 	n=le_regbuf(legets_lep->buf, buf);
235 //	strcpy(buf+legets_lep->sx+ min(n,legets_lep->dsize-1), " <");
236 //	term_printf("%-.*s", min(n,lep->dsize), buf+lep->sx);
237 	dfp=dsp_fmtinit(buf+legets_lep->sx, dfp);
238 
239 	return dfpb;
240 }
241 
legets_gets(const char * msg,char * s,int dsize,int size,int hn)242 int 	legets_gets(const char *msg,char *s,int dsize,int size,int hn)
243 {
244 	dspreg_t	*drp;
245 	le_t	le;
246 	uint 	ch;
247 	int 	hy;
248 	int 	ret;
249 
250 
251 legets_lep=&le;
252 
253 	OnMessage_Flag=TRUE;
254 
255 	drp=dsp_reginit();
256 	drp->y    =dspall.sizey-1;
257 	drp->func =dspreg_legets;
258 	drp->sizex=dsize;
259 	drp->sizey=1;
260 	drp->vp   =(void *)msg;
261 
262 	dsp_regadd(drp);
263 
264 
265 	if (hn!=-1)
266 		{
267 		 int 	tmpListNo;
268 
269 		 tmpListNo = CurrentFileNo;
270 		 CurrentFileNo = hn;
271 		 hy=GetLastNumber();
272 		 CurrentFileNo=tmpListNo;
273 		}
274 
275 	le.dx=strlen(msg);
276 
277 	le.dsize=dsize-strlen(msg)-2;
278 	le.size=size;
279 
280 	strcpy(le.buf,s);
281 	le.sx=0;
282 	le.cx=0;
283 	le.lx=0;
284 	le.l_sx=0;
285 	le_setlx(&le,strlen(s));
286 
287 	for (;;)
288 		{
289 		 dsp_allview();
290 		 term_locate(drp->y, le.dx+le.cx);
291 
292 		 term_csrn();
293 		 ch=get_keyf(1);
294 		 switch(ch)
295 		 	{
296 		  case -1:
297 		  	 continue;
298 		  case KF_SysCursorleft:
299 		  	 le_csrleft(&le);
300 		  	 continue;
301 		  case KF_SysCursorright:
302 		  	 le_csrright(&le);
303 		  	 continue;
304 		  case KF_SysCursorleftside:
305 		  	 le_csrleftside(&le);
306 		  	 continue;
307 		  case KF_SysCursorrightside:
308 		  	 le_csrrightside(&le);
309 		  	 continue;
310 		  case KF_SysCursorup:
311 		  	 legets_histprev(&le,hn,&hy);
312 		  	 continue;
313 		  case KF_SysCursordown:
314 		  	 legets_histnext(&le,hn,&hy);
315 		  	 continue;
316 
317 		  case KF_SysReturn:
318 		  	 strcpy(s,le.buf);
319 		  	 if (hn!=-1&& *s!='\0')
320 		  	 	{
321 		  	 	 int 	tmpListNo;
322 		  	 	 EditLine	*ed;
323 
324 		  	 	 tmpListNo = CurrentFileNo;
325 		  	 	 CurrentFileNo = hn;
326 		  	 	 ed=MakeLine(s);
327 		  	 	 AppendLast(ed);
328 		  	 	 CurrentFileNo=tmpListNo;
329 		  	 	}
330 		  	 ret= TRUE;
331 		  	 break;
332 		  case KF_SysEscape:
333 		  	 ret= ESCAPE;
334 		  	 break;
335 
336 		  case KF_SysBackspace:
337 		  	 le_edit(&le, ' ', BACKSPACE);
338 		  	 continue;
339 		  case KF_SysDeletechar:
340 		  	 le_edit(&le, ' ', DELETE);
341 		  	 continue;
342 		  case KF_SysCntrlInput:
343 		  	 putDoubleKey(CNTRL('P'));
344 		  	 system_guide(); // !!
345 		  	 term_locate(drp->y, le.dx+le.cx);
346 		  	 ch=term_inkey();
347 		  	 delDoubleKey();
348 
349 		  	 le_edit(&le, ch, NONE);
350 		  	 continue;
351 		  default:
352 		  	 if (ch&KF_normalcode);
353 		  	 	{
354 		  	 	 ch&= ~KF_normalcode;
355 		  	 	 if (iseuc(ch) || ch==0x8e)
356 		  	 	 	le_edit(&le, ch<<8|term_inkey(), NONE); else
357 		  	 	 	le_edit(&le, ch, NONE);
358 		  	 	}
359 		  	 continue;
360 		  	}
361 		 break;
362 		}
363 
364 	dsp_regfin(drp);
365 	RefreshMessage();
366 	return ret;
367 }
368 
369