1 /*--------------------------------------------------------------------
2 	nxeditor
3 			FILE NAME:block.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 
13 #define	blck	edbuf[CurrentFileNo].block
14 
block_set(block_t * bp)15 void	block_set(block_t *bp)
16 {
17 	block_t	*bbp= &blck;
18 
19 	bp->blkm=bbp->blkm;
20 	if (bp->blkm==BLKM_none)
21 		return;
22 
23 	if (bbp->y_st> bbp->y_ed ||
24 			(bbp->y_st==bbp->y_ed && bbp->x_st>bbp->x_ed))
25 		{
26 		 bp->x_st=bbp->x_ed;
27 		 bp->x_ed=bbp->x_st;
28 		 bp->y_st=bbp->y_ed;
29 		 bp->y_ed=bbp->y_st;
30 		} else
31 		{
32 		 bp->x_st=bbp->x_st;
33 		 bp->x_ed=bbp->x_ed;
34 		 bp->y_st=bbp->y_st;
35 		 bp->y_ed=bbp->y_ed;
36 		}
37 }
38 
block_range(long n,block_t * bp,int * x_st,int * x_ed)39 bool	block_range(long n, block_t *bp, int *x_st, int *x_ed)
40 {
41 	if (bp->blkm==BLKM_none || bp->y_st >n || bp->y_ed <n)
42 		return FALSE;
43 
44 	if (bp->blkm==BLKM_y)
45 		{
46 		 if (bp->y_ed==n)
47 		 	return FALSE;
48 		} else
49 		{
50 		 if (bp->y_st == bp->y_ed)
51 		 	{
52 		 	 *x_st= bp->x_st;
53 		 	 *x_ed= bp->x_ed;
54 		 	 return TRUE;
55 		 	}
56 
57 		 if (bp->y_st==n)
58 		 	{
59 		 	 *x_st=bp->x_st;
60 		 	 *x_ed=strlen(GetList(n)->buffer)+1;
61 		 	 return TRUE;
62 		 	}
63 		 if (bp->y_ed==n)
64 		 	{
65 		 	 *x_st=0;
66 		 	 *x_ed=bp->x_ed;
67 		 	 return TRUE;
68 		 	}
69 		}
70 
71 	*x_st=0;
72 	*x_ed=strlen(GetList(n)->buffer)+1;
73 	return TRUE;
74 }
75 
76 
77 
GetBlockFlg()78 int 	GetBlockFlg()
79 {return blck.blkm!=BLKM_none;}
80 
81 
block_cmove()82 void	block_cmove()
83 {
84 	if (!GetBlockFlg())
85 		return;
86 
87 	if (blck.y_ed != GetLineOffset())
88 		blck.blkm= BLKM_y; else
89 		{
90 		 if (blck.x_ed != GetBufferOffset())
91 		 	blck.blkm= BLKM_x;
92 		}
93 
94 	blck.x_ed= GetBufferOffset();
95 	blck.y_ed= GetLineOffset();
96 }
97 
98 
99 #define	MAX_bstack	1024
100 
101 typedef	struct
102 {
103 	char	*s;
104 	blkm_t	blkm;
105 }	bstack_t;
106 
107 bstack_t	bstack[MAX_bstack];
108 int 		bstack_nums;
109 
110 block_t	bkm;
111 
112 
113 
bstack_init()114 void	bstack_init()
115 {
116 	bstack_nums=0;
117 }
118 
bstack_fin()119 void	bstack_fin()
120 {
121 	int 	i;
122 	for (i=bstack_nums;i>0;)
123 		free(bstack[--i].s);
124 	bstack_nums=0;
125 }
126 
127 
block_size(block_t * bp)128 int 	block_size(block_t *bp)
129 {
130 	int 	ln;
131 	int		x_st, x_ed;
132 	long	n;
133 
134 	ln=0;
135 	n=min(bp->y_st, bp->y_ed);
136 	while (block_range(n, &bkm, &x_st, &x_ed))
137 		ln += x_ed-x_st, ++n;
138 	return ln;
139 }
140 
bstack_copy()141 bool	bstack_copy()
142 {
143 	EditLine	*ed;
144 	char	*p;
145 	long	n;
146 	int 	x_st, x_ed;
147 	char	buf[MAXEDITLINE+1];
148 
149 	if (bstack_nums>=MAX_bstack)
150 		{
151 		 if (!keysel_ynq("�֥�å��Хåե������դǤ���������ޤ���?"))
152 		 	return FALSE;
153 		 bstack_fin();
154 		}
155 
156 	bstack[bstack_nums].s= p= (char *)mem_alloc(block_size(&bkm)+1);
157 	bstack[bstack_nums].blkm= bkm.blkm;
158 	++bstack_nums;
159 
160 	*p='\0';
161 	n= bkm.y_st;
162 	ed=GetList(n);
163 	while (block_range(n, &bkm, &x_st, &x_ed))
164 		{
165 		 if (ed->next==NULL)
166 		 	strcpy(buf,ed->buffer); else
167 		 	sprintf(buf,"%s\n",ed->buffer);
168 		 buf[x_ed]='\0';
169 		 strcat(p, buf+x_st);
170 		 ed=ed->next;
171 		 ++n;
172 		}
173 
174 	return TRUE;
175 }
176 
block_cut()177 void	block_cut()
178 {
179 	EditLine	*ed,*ed_next;
180 	int 	x_st, x_ed;
181 	char	buf[MAXEDITLINE+1];
182 
183 	if (bkm.blkm==BLKM_y && bkm.y_st<GetLastNumber())
184 		lists_delete(bkm.y_st, bkm.y_ed-1);else
185 		{
186 		 ed=GetList(bkm.y_st);
187 		 block_range(bkm.y_st, &bkm, &x_st, &x_ed);
188 		 strcpy(buf, ed->buffer);
189 		 buf[x_st]='\0';
190 
191 		 ed_next=GetList(bkm.y_ed);
192 		 block_range(bkm.y_ed, &bkm, &x_st, &x_ed);
193 		 strcat(buf, ed_next->buffer+x_ed);
194 
195 		 Realloc(ed, buf);
196 
197 		 if (bkm.y_st+1<=bkm.y_ed)
198 		 	lists_delete(bkm.y_st+1, bkm.y_ed);
199 		}
200 
201 	SetFileChangeFlag();
202 	csr_lenew();
203 
204 	csr_setdy(GetRow()+bkm.y_st-GetLineOffset());
205 	csr_setly(bkm.y_st);
206 
207 	if (bkm.blkm==BLKM_x)
208 		csr_setlx(bkm.x_st);
209 }
210 
211 
212 
213 
BlockInit()214 void	BlockInit()
215 {
216 	blck.blkm=BLKM_none;
217 }
218 
op_block_start()219 SHELL	void	op_block_start()
220 {
221 	csr_leupdate();
222 
223 	if (GetBlockFlg())
224 		BlockInit(); else
225 		{
226 		 blck.blkm= BLKM_x;
227 		 blck.y_st= GetLineOffset();
228 		 blck.y_ed= GetLineOffset();
229 		 blck.x_st= csrle.lx;
230 		 blck.x_ed= csrle.lx;
231 		}
232 }
233 
BlockCommand()234 bool	BlockCommand()		/* �֥�å����ޥ�ɤν����� */
235 {
236 	csr_leupdate();
237 
238 	block_set(&bkm);
239 	if (GetBlockFlg())
240 		blck.blkm= BLKM_none; else
241 		{
242 		 bkm.y_st=GetLineOffset();
243 		 bkm.y_ed=bkm.y_st+1;
244 
245 		 if (bkm.y_st != GetLastNumber())
246 		 	bkm.blkm= BLKM_y; else
247 		 	{
248 		 	 bkm.blkm= BLKM_x;
249 		 	 --bkm.y_ed;
250 		 	 bkm.x_st=0;
251 		 	 bkm.x_ed=strlen(GetList(GetLineOffset())->buffer);
252 		 	}
253 		}
254 
255 	return block_size(&bkm)>0;
256 }
257 
op_block_yanc()258 SHELL	void	op_block_yanc()	/* �֥�å����ԡ�����Τߡ�*/
259 {
260 	if (!BlockCommand())
261 		return;
262 	bstack_copy();
263 }
264 
op_block_cut()265 SHELL	void	op_block_cut()
266 {
267 	if (!BlockCommand())
268 		return;
269 
270 	if (!bstack_copy())
271 		return;
272 	block_cut();
273 }
274 
str_paste(char * s,const char * p)275 const	char	*str_paste(char *s, const char *p)
276 {
277 	while (*p!='\n' && *p!='\0')
278 		*s++=*p++;
279 	*s='\0';
280 
281 	return *p=='\n'? p+1: NULL;
282 }
283 
bstack_paste()284 void	bstack_paste()
285 {
286 	char	buf[MAXEDITLINE+1], buf_a[MAXEDITLINE+1], *p;
287 	const	char	*q;
288 	EditLine	*ed,*edn;
289 	long	n;
290 
291 	if (bstack_nums==0)
292 		return;
293 
294 	q=bstack[bstack_nums-1].s;
295 	csr_leupdate();
296 
297 	if (bstack[bstack_nums-1].blkm==BLKM_y)
298 		{
299 		 edn=ed=GetList(GetLineOffset()-1);
300 		 for(;;)
301 		 	{
302 		 	 q=str_paste(buf, q);
303 		 	 if (q==NULL)
304 		 	 	break;
305 		 	 InsertLine(ed, MakeLine(buf));
306 		 	 ed=ed->next;
307 		 	}
308 		 csrse.ed = edn -> next;
309 		} else
310 		{
311 		 ed=GetList(GetLineOffset());
312 		 strcpy(buf, ed->buffer);
313 		 buf[GetBufferOffset()]='\0';
314 		 strcpy(buf_a, ed->buffer+GetBufferOffset());
315 
316 		 q=str_paste(buf+GetBufferOffset(), q);
317 		 if (q==NULL)
318 		 	{
319 		 	 strcat(buf, buf_a);
320 		 	 Realloc(ed, buf);
321 		 	} else
322 		 	{
323 		 	 Realloc(ed, buf);
324 
325 			 for(;;)
326 			 	{
327 		 	 	 q=str_paste(buf, q);
328 		 	 	 if (q==NULL)
329 		 	 	 	break;
330 		 	 	 InsertLine(ed, MakeLine(buf));
331 		 	 	 ed=ed->next;
332 		 	 	}
333 		 	 strcat(buf, buf_a);
334 		 	 InsertLine(ed, MakeLine(buf));
335 		 	}
336 		}
337 
338 	SetFileChangeFlag();
339 
340 	csr_lenew();
341 	OffsetSetByColumn();
342 }
343 
344 
op_block_paste()345 SHELL	void	op_block_paste()
346 {
347 	bstack_paste();
348 	if (bstack_nums>0)
349 		--bstack_nums;
350 }
351 
op_block_copy()352 SHELL	void	op_block_copy()
353 {
354 	bstack_paste();
355 }
356 
op_block_dup()357 SHELL	void	op_block_dup()
358 {
359 	if (!BlockCommand())
360 		return;
361 
362 	bstack_copy();
363 	op_block_paste();
364 }
365 
op_block_kill()366 SHELL	void	op_block_kill()
367 {
368 	if (keysel_ynq("����Хåե��������ޤ���"))
369 		bstack_fin();
370 }
371 
op_block_chlast()372 SHELL	void	op_block_chlast()
373 {
374 	long	a;
375 	int 	b;
376 	blkm_t	blkm;
377 
378 	if (GetBlockFlg())
379 		{
380 		 a=blck.y_st;
381 		 blck.y_st=GetLineOffset();
382 
383 		 b=blck.x_st;
384 		 blck.x_st=GetBufferOffset();
385 
386 		 blkm=blck.blkm;
387 
388 		 csr_setly(a);
389 		 csr_setlx(b);
390 
391 		 blck.blkm=blkm;
392 		}
393 }
394 
395