1 /*--------------------------------------------------------------------
2 	nxeditor
3 			FILE NAME:line.c
4 			Programed by : I.Neva
5 			R & D  ADVANCED SYSTEMS. IMAGING PRODUCTS.
6 			1992.06.01
7 
8     Copyright (c) 1998,1999,2000 SASAKI Shunsuke.
9     All rights reserved.
10 --------------------------------------------------------------------*/
11 #include "ed.h"
12 #include <ctype.h>
13 
14 
se_insert(const char * s,bool f)15 void	se_insert(const char *s,bool f)
16 {
17 	int 	lx;
18 	u_char	c;
19 
20 	lx=csrle.lx;
21 	while(*s!='\0')
22 		{
23 		 c=*s++;
24 		 if (c<0xa0 || *(u_char *)s<0xa0)
25 		 	LeditInput(c, NONE); else
26 		 	{
27 		 	 LeditInput(c<<8|*(u_char *)s, NONE);
28 		 	 ++s;
29 		 	}
30 		}
31 	if (f)
32 		csr_setlx(lx);
33 }
34 
se_delete(int n,bool f)35 void	se_delete(int n, bool f)
36 {
37 	for (;n>0;--n)
38 		{
39 		 if (f&& IsKanjiPosition())
40 		 	--n;
41 		 Ledit(DELETE);
42 		}
43 }
44 
se_nazo()45 void	se_nazo()
46 {
47 // FREE�⡼�ɤλ��˰�ö����������ΰ��֤ޤǥ��ڡ��������롣
48 
49 //	LeditInput(CNTRL(' '), NONE);
50 	Ledit(NONE);
51 	Ledit(BACKSPACE);
52 }
53 
line_catnext()54 void	line_catnext()
55 {
56 	if (GetLineOffset() <GetLastNumber())
57 		{
58 		 se_nazo();
59 		 csr_leupdate();
60 
61 		 CatLine();
62 		 DeleteAndDraw();
63 
64 		 undo_add(FALSE,"\n");
65 		}
66 }
67 
line_catprev()68 void	line_catprev()
69 {
70 	if (GetLineOffset()>1)
71 		{
72 		 op_cursor_left();
73 		 CatLine();
74 //		 DeleteAndDraw();
75 
76 		 undo_add(TRUE,"\n");
77 		}
78 }
79 
80 
op_del_tknext()81 SHELL	void	op_del_tknext()
82 {
83 	int 	lx;
84 	char	buf[MAXEDITLINE+1];
85 
86 	if (GetBufferOffset()>=strlen(csrle.buf))
87 		{
88 		 line_catnext();
89 		 return;
90 		}
91 
92 	lx=kanji_tknext(csrle.buf,csrle.lx,FALSE) -csrle.lx;
93 
94 	strncpy(buf, csrle.buf+ csrle.lx, lx);
95 	buf[lx]='\0';
96 
97 	se_delete(lx,TRUE);
98 	undo_add(FALSE,buf);
99 }
100 
op_del_tkprev()101 SHELL	void	op_del_tkprev()
102 {
103 	int 	lx,lx_a;
104 
105 	char	buf[MAXEDITLINE+1];
106 
107 	if (GetBufferOffset()==0)
108 		{
109 		 line_catprev();
110 		 return;
111 		}
112 
113 	lx=kanji_tkprev(csrle.buf,csrle.lx,FALSE);
114 	lx_a=csrle.lx-lx;
115 
116 	strncpy(buf, csrle.buf+lx, lx_a);
117 	buf[lx_a]='\0';
118 
119 	csr_setlx(lx);
120 	se_delete(lx_a,TRUE);
121 	undo_add(TRUE,buf);
122 }
123 
op_del_sright()124 SHELL	void	op_del_sright()
125 {
126 	char	buf[MAXEDITLINE+1];
127 
128 	if (GetBufferOffset()>=strlen(csrle.buf))
129 		return;
130 	strcpy(buf,csrle.buf+csrle.lx);
131 	se_delete(strlen(csrle.buf+csrle.lx),TRUE);
132 	undo_add(FALSE,buf);
133 }
134 
op_del_sleft()135 SHELL	void	op_del_sleft()
136 {
137 	char	buf[MAXEDITLINE+1];
138 	int 	lx;
139 
140 	if (GetBufferOffset()<=0)
141 		return;
142 
143 	lx=csrle.lx;
144 
145 	strncpy(buf,csrle.buf,lx);
146 	buf[lx]='\0';
147 
148 	op_cursor_sleft();
149 	se_delete(lx,TRUE);
150 	undo_add(TRUE,buf);
151 }
152 
153 
154 
op_del_char()155 SHELL	void	op_del_char()
156 {
157 	char	buf[2+1],*p;
158 
159 	se_nazo();
160 
161 	if (GetBufferOffset()>=strlen(csrle.buf)) /* �����ˤ���� */
162 		{
163 		 line_catnext();
164 		 return;
165 		}
166 
167 
168 	p=buf;
169 	*p++=csrle.buf[GetBufferOffset()];
170 	if (IsKanjiPosition())
171 		*p++=csrle.buf[GetBufferOffset()+1];
172 	*p='\0';
173 
174 	se_delete(1,FALSE);
175 	undo_add(FALSE,buf);
176 }
177 
178 
op_del_bs()179 SHELL	void	op_del_bs()
180 {
181 	long LineOffset;
182 	char	buf[2+1],*p;
183 
184 	LineOffset = GetLineOffset();
185 	if (GetBufferOffset() == 0)
186 		{
187 		 line_catprev();
188 		 return;
189 		}
190 
191 	op_cursor_left();
192 
193 	p=buf;
194 	*p++=csrle.buf[GetBufferOffset()];
195 	if (IsKanjiPosition())
196 		*p++=csrle.buf[GetBufferOffset()+1];
197 	*p='\0';
198 
199 	se_delete(1,FALSE);
200 	undo_add(TRUE,buf);
201 }
202 
split(bool f)203 void	split(bool f)
204 {
205 	EditLine	*ed;
206 	char	buf_nl[MAXEDITLINE+1];
207 	int 	a,n;
208 
209 	se_nazo();
210 
211 	n=GetBufferOffset();
212 
213 	a=0;
214 	if (sysinfo.autoindentf)
215 		{
216 		 for (;a<strlen(csrle.buf);++a)
217 		 	{
218 		 	 if (csrle.buf[a]!='\t'&& csrle.buf[a]!=' ')
219 		 	 	break;
220 		 	 buf_nl[a] = csrle.buf[a];
221 		 	}
222 		 if (n<a)
223 		 	a=0;
224 		}
225 
226 	strcpy(buf_nl+a,csrle.buf+n);
227 
228 /* split�����饤�� */
229 	csrle.buf[n]='\0';
230 	csr_leupdate();
231 
232 /* �������ɲä����饤�� */
233 	ed=MakeLine(buf_nl);
234 	InsertLine(GetList(GetLineOffset()), ed);
235 
236 	SetFileChangeFlag();
237 
238 //	CrtDrawLine(GetRow(),GetLineOffset(), FALSE);
239 
240 	if (f)
241 		{
242 		 op_cursor_down();
243 		 csr_setlx(a);
244 		}
245 //		 InsertAndDraw();
246 //		} else
247 //		{
248 //		}
249 }
250 
op_line_cr()251 void	op_line_cr()
252 {
253 	split(TRUE);
254 }
255 
256 
257 
258 
259 
op_line_new()260 SHELL	void	op_line_new()
261 {
262 	EditLine *ed;
263 
264 	csr_leupdate();
265 
266 	ed=MakeLine("");
267 	InsertLine(GetList(GetLineOffset()-1), ed);
268 	csrse.ed=ed;
269 
270 	csr_lenew();
271 	op_cursor_sleft();
272 	SetFileChangeFlag();
273 	InsertAndDraw();
274 }
275 
CatLine()276 int 	CatLine()
277 {
278 	char tmpbuff1[MAXEDITLINE+1];
279 	char tmpbuff2[MAXEDITLINE+1];
280 	EditLine *ed,*edb;
281 
282 	if (GetLineOffset() >= GetLastNumber())
283 		return FALSE;
284 
285 	strcpy(tmpbuff1, csrle.buf);
286 
287 	edb= GetList(GetLineOffset());
288 	ed = edb->next;
289 	strcpy(tmpbuff2, ed->buffer);	//buffer
290 	if (strlen(tmpbuff1)+strlen(tmpbuff2) > MAXEDITLINE)
291 	{
292 		inkey_wait(LINE_BUFFER_OVER_MSG);
293 		return FALSE;
294 	}
295 	DeleteList(ed);
296 	SetFileChangeFlag();
297 	strcat(tmpbuff1, tmpbuff2);
298 
299 	Realloc(edb, tmpbuff1);
300 	csr_lenew();
301 
302 	return TRUE;
303 }
304 
305 
306 
op_char_input()307 SHELL	void	op_char_input()
308 {
309 	int 	c;
310 
311 	putDoubleKey(CNTRL('P'));
312 	system_guide();	// !!
313 	system_msg(CNTRL_INPUT_MSG);
314 
315 	c = term_inkey();
316 
317 	delDoubleKey();
318 //	system_guide();
319 
320 	if (c=='\0')
321 		return;
322 
323 	if (c<0x80)
324 		c&=0x1f;
325 	InputAndCrt(c);
326 }
327 
tagJmp()328 void	tagJmp()
329 {
330 	int count;
331 	int j;
332 	int length;
333 	char buf[MAXEDITLINE+1];
334 	char tagFileName[LN_path+1];
335 	char tagJmpNoBuff[MAXEDITLINE+1];
336 
337 	strcpy(buf, csrle.buf);
338 	length = strlen(buf);
339 
340 	count=0;
341 	/*get filename*/
342 	for (j=0; j<LN_path&& count<length; count++,j++)
343 		{
344 		 if (strchr("*?\"\'():",buf[count])!=NULL)
345 			break;
346 		 tagFileName[j] = buf[count];
347 		}
348 	tagFileName[j] = '\0';
349 
350 	/*to lineno_s*/
351 	while(count<length&& !isdigit(buf[count]))
352 		++count;
353 
354 	/*get lineno_s*/
355 	for (j=0; count < length; count++, j++)
356 		{
357 		 if (!isdigit(buf[count]))
358 		 	break;
359 		 tagJmpNoBuff[j] = buf[count];
360 		}
361 	tagJmpNoBuff[j] = '\0';
362 	system_msg(WAITING_MSG);
363 	op_cursor_down();
364 
365 /*	if (FileOpenOp(tagFileName))
366 		csr_setly(atol(tagJmpNoBuff));*/
367 
368 	FileOpenOp(tagFileName);
369 	csr_setly(atol(tagJmpNoBuff));
370 }
371 
op_jump_tag()372 SHELL	void	op_jump_tag()
373 {
374 	BlockInit();
375 	tagJmp();
376 }
377 
op_jump_line()378 SHELL	void	op_jump_line()
379 {
380 	char	buf[MAXLINESTR+1];
381 	int 	n;
382 
383 	if (keyf_numarg()>0)
384 		n=atoi(keyf_getarg(0)); else
385 		{
386 		 *buf='\0';
387 		 if (GetS("���ֹ�:",buf)==ESCAPE)
388 		 	return ;
389 		 n=atol(buf);
390 		}
391 	lm_mark(GetLineOffset(), 0);
392 	csr_setly(n);
393 
394 
395 
396 
397 }
398 
399 static	int 	lm_lines[5]={0,0,0,0,0};
400 
lm_mark(int ln,int n)401 void	lm_mark(int ln, int n)
402 {
403 	if (n>4)
404 		n=0;
405 
406 	lm_lines[n]=ln;
407 }
408 
lm_line(int n)409 int 	lm_line(int n)
410 {
411 	if (n>4)
412 		n=0;
413 	return lm_lines[n];
414 }
415 
416 
417 
op_jump_mark()418 SHELL	void	op_jump_mark()
419 {
420 	int 	n;
421 
422 	char	buf[LN_dspbuf+1];
423 
424 	n= (keyf_numarg()==0)? 0: atoi(keyf_getarg(0));
425 	if (n>4)
426 		n=0;
427 
428 	lm_mark(GetLineOffset(), n);
429 	sprintf(buf, "mark���ޤ����� #%d",n);
430 	system_msg(buf);
431 }
432 
op_jump_before()433 SHELL	void	op_jump_before()
434 {
435 	int 	n,ln;
436 	char	buf[LN_dspbuf+1];
437 
438 	n= (keyf_numarg()==0)? 0: atoi(keyf_getarg(0));
439 	if (n>4)
440 		n=0;
441 
442 	ln=lm_line(n);
443 	lm_mark(GetLineOffset(), 0);
444 	csr_setly(ln);
445 
446 	sprintf(buf, "#%d",n);
447 	system_msg(buf);
448 }
449 
op_char_undo()450 SHELL	void	op_char_undo()
451 {
452 	undo_paste();
453 }
454 
455 
456 
457 #define	MAX_udbuf	1024
458 
udbuf_init()459 void	udbuf_init()
460 {
461 	int		tmpFileno;
462 	EditLine	*ed;
463 
464 	tmpFileno = CurrentFileNo;
465 	CurrentFileNo = UNDO_SYSTEM;
466 	FileStartInit(FALSE);
467 	ed=MakeLine("");
468 	AppendLast(ed);
469 	CurrentFileNo = tmpFileno;
470 
471 }
472 
udbuf_get(char * s)473 void	udbuf_get(char *s)
474 {
475 	int		tmpFileno;
476 	EditLine	*ed;
477 
478 
479 	tmpFileno = CurrentFileNo;
480 	CurrentFileNo = UNDO_SYSTEM;
481 
482 	ed = GetLast();
483 
484 	strcpy(s,ed->buffer);	// buffer
485 	if (*s!='\0')
486 		DeleteList(ed);
487 
488 	CurrentFileNo = tmpFileno;
489 }
490 
udbuf_set(bool df,const char * s)491 void	udbuf_set(bool df,const char *s)
492 {
493 	int			tmpFileno;
494 	EditLine	*ed;
495 	char		buf[MAXEDITLINE+1];
496 
497 	tmpFileno = CurrentFileNo;
498 	CurrentFileNo = UNDO_SYSTEM;
499 
500 	if (GetLastNumber()>=MAX_udbuf)
501 		{
502 		 DeleteList(GetTop()->next);
503 		 csrse.ed = NULL;
504 		}
505 
506 	buf[0]=df?'1':'2';
507 	strcpy(buf+1,s);
508 	ed=MakeLine(buf);
509 	AppendLast(ed);
510 
511 	CurrentFileNo = tmpFileno;
512 }
513 
undo_add(bool df,const char * s)514 void	undo_add(bool df,const char *s)
515 {
516 	udbuf_set(df,s);
517 }
518 
undo_paste()519 void	undo_paste()
520 {
521 	char	buf[MAXEDITLINE+1];
522 
523 	udbuf_get(buf);
524 
525 	if (buf[0]!='1'&&buf[0]!='2')
526 		return;
527 
528 	if (buf[1]=='\n')
529 		{
530 		 bool	f;
531 
532 		 f= sysinfo.autoindentf;
533 		 sysinfo.autoindentf= FALSE;
534 		 split(buf[0]=='1');
535 		 sysinfo.autoindentf= f;
536 
537 		 return;
538 		}
539 
540 	se_insert(buf+1,buf[0]!='1');
541 }
542 
op_line_undo()543 SHELL	void	op_line_undo()
544 {
545 	csr_lenew();
546 }
547