File: cut-paste.c

package info (click to toggle)
splitvt 1.6.5-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 340 kB
  • ctags: 393
  • sloc: ansic: 4,754; sh: 78; makefile: 37; perl: 15
file content (346 lines) | stat: -rw-r--r-- 7,088 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

/* The cut-paste module for splitvt */

#include	<stdio.h>
#include	<string.h>
#include	<unistd.h>
#include	"vt100.h"
#include	"video.h"
#include	"splitvt.h"
#include	"terminal.h"

#define RIGHT 0x01
#define LEFT  0x02
#define Min(A,B)	((A>B)?B:A)
#define Max(A,B)	((A>B)?A:B)

/* Variables used by the cut-paste motion functions */
static int marked, oldattr;
static unsigned char on;

extern struct physical physical;
static int lastdirection=0;

/* This function assumes that it alone will print selected text */
static void put_sel_char(win, x, y, oldattr, on, direction)
window *win;
int x, y;
int *oldattr;
unsigned char *on;
int direction;
{
	int c, selected=0;

	c = get_video(win, x, y);
	/* Toggle selection highlighting */
	if ( direction ) {
/*fprintf(stderr, "Turning %s...\n", direction == RIGHT ? "right" : "left");*/
		if ( lastdirection && lastdirection != direction )
			/* We turned... */ ;
		else {
			c ^= (SELECTED<<8);
			selected = ( ((c>>8)&SELECTED) ? 1 : 0 );
		}
		lastdirection=direction;
	} else
		c ^= (SELECTED<<8);

	(*oldattr)=check_attr(c, *oldattr, (int)on);
	put_video(c, win, x, y);

	if ( c&0xFF )
		printf("%c", c&0xFF);
	else
		printf(" ");

	if ( direction ) {
		if ( !selected ) {
			if ( direction == RIGHT ) {
				if ( y < win->cols )
					++y;
				else
					goto end;
			} else {
				if ( y > 1 )
					--y;
				else
					goto end;
				vt_left(2);
			}
			c = get_video(win, x, y);
			if ( c&0xFF )
				printf("%c", c&0xFF);
			else
				printf(" ");
			vt_left(1);
		} else if ( direction == LEFT )
			vt_left(2);
	}
end:
	fflush(stdout);
	return;
}


/* If this function returns 0, no selection was made */

static char *extract_sel(win, buf, len, mark1, mark2)
int win;
char *buf;
int len;
position *mark1, *mark2;
{
	position startsel, endsel;

	if ( mark1->x == mark2->x )
	{
		if ( mark1->y == mark2->y ) {
			vt_info("Selection cancelled.");
			clrsel_video(win);
			reset_bar(1);
			return(NULL);
		}
		if ( mark1->y < mark2->y ) {
			startsel = (*mark1);
			endsel = (*mark2);
			++endsel.y;
		} else {
			startsel = (*mark2);
			endsel = (*mark1);
			++endsel.y;
		}
	} else if ( mark1->x < mark2->x ) {
		startsel = (*mark1);
		endsel = (*mark2);
	} else if ( mark1->x > mark2->x ) {
		startsel = (*mark2);
		endsel = (*mark1);
	}
	getsel_video(win,buf,len, startsel.x, endsel.x, startsel.y, endsel.y);
	clrsel_video(win);
	vt_info("Region selected.");
	reset_bar(1);
	return(buf);
}

static void line_down(win, cursor)
window *win;
position *cursor;
{
	int j;

	if ( cursor->x < win->rows ) {
		if ( marked ) {
			for (j=cursor->y; j<win->cols; ++j) {
		put_sel_char(win, cursor->x, j, &oldattr, &on, 0);
			}
			printf("\r");
			vt_down(1);
			++cursor->x;
			for ( j=1; j<cursor->y; ++j ) {
		put_sel_char(win, cursor->x, j, &oldattr, &on, 0);
			}
		} else {
			vt_down(1);
			++cursor->x;
		}
	}
}

static void line_up(win, cursor)
window *win;
position *cursor;
{
	int j;

	if ( cursor->x > 1 ) {
		--cursor->x;
		if ( marked ) {
			vt_up(1);
			for (j=cursor->y; j<win->cols; ++j) {
		put_sel_char(win, cursor->x, j, &oldattr, &on, 0);
			}
			printf("\r");
			vt_down(1);
			++cursor->x;
			for ( j=1; j<cursor->y; ++j ) {
		put_sel_char(win, cursor->x, j, &oldattr, &on, 0);
			}
			vt_up(1);
			--cursor->x;
		} else 
			vt_up(1);
	}
}

static void move_left(win, cursor)
window *win;
position *cursor;
{
	if ( cursor->y > 1 ) {
		if ( marked ) {
	put_sel_char(win, cursor->x, cursor->y, &oldattr, &on, LEFT);
		} else
			vt_left(1);
		--cursor->y;
	}
}

static void move_right(win, cursor)
window *win;
position *cursor;
{
	if ( cursor->y < win->cols ) {
		if ( marked )
	put_sel_char(win, cursor->x, cursor->y, &oldattr, &on, RIGHT);
		else
			vt_right(1);
		++cursor->y;
	}
}

static int use_xcb=0;	/* Do we use xcb to access X selection buffers? */
void vt_initsel()
{
	extern char *pathsearch();	/* From misc.c */

#ifdef USE_XCB
	char *display;
	/* Use xcb if we have both an X display and the command. */
	/* xcb needs to support my hacked '-R' option. */
	if ( (display=(char *)getenv("DISPLAY")) && pathsearch("xcb", 1) )
		use_xcb=1;
	else
#endif
		use_xcb=0;
}

static char selbuf[BUFSIZ];
char *vt_getselbuf()
{
	FILE *safe_popen();		/* From misc.c */
	FILE *xcb;
	char buffer[BUFSIZ];
	int len;

	if ( use_xcb ) {
		if ( (xcb=safe_popen("xcb -S 0 && xcb -p 0", "r")) == NULL )
			return(selbuf);
		len=fread(buffer, sizeof(char), BUFSIZ-1, xcb);
		(void) safe_pclose(xcb);

		if ( len > 0 ) {
			buffer[len]='\0';
			strcpy(selbuf, buffer);
		}
	}
	return(selbuf);
}
char *vt_setselbuf(buffer)
char *buffer;
{
	FILE *safe_popen();		/* From misc.c */
	FILE *xcb;
	
	strncpy(selbuf, buffer, BUFSIZ-1);
	selbuf[BUFSIZ-1]='\0';
	if (use_xcb && (xcb=safe_popen("xcb -s 0; xcb -RT 0", "w")) != NULL) {
		(void) fwrite(selbuf, sizeof(char), strlen(selbuf), xcb);
		(void) safe_pclose(xcb);
	}
	return(selbuf);
}

/* Get a window selection */
char *vt_getsel(win, buf, len)
int win;
char *buf;
int len;
{
	int c;
	position here, cursor, mark1, mark2;
	window *thiswin;
	
	thiswin = physical.subwins[win];
	here=cursor=thiswin->cursor;
	marked=lastdirection=0;
	vt_info("Mark beginning of selection: ");
	while ( (c=getchar()) != EOF ) {
		switch (c) {
			case 'j':	/* Move down one line */
					line_down(thiswin, &cursor);
					break;
			case 'k':	/* Move up one line */
					line_up(thiswin, &cursor);
					break;
			case 'l':	/* Move right one char */
					move_right(thiswin, &cursor);
					break;
			case 'h':	/* Move left one char */
					move_left(thiswin, &cursor);
					break;
			case ' ':	/* Mark selection */
					if ( marked ) {
						mark2=cursor;
						/* Copy and deselect area */
						buf=extract_sel(thiswin, buf,
							len, &mark1, &mark2); 
						/* Repaint the screen */
						paint_video(thiswin);
						return(buf);
					} else {
						mark1=cursor;
						oldattr = get_video(thiswin,
							mark1.x, mark1.y);
						on = (oldattr>>8);
						marked=1;
						vt_info(
						"Mark end of selection: ");
						/* Set reverse here */
						vt_update();
					}
					break;
			case '\033':
			case 'q':	/* Cancel selection */
					clrsel_video(thiswin);
					vt_goto((here.x+thiswin->row_offset),
									here.y);
					vt_info("Selection cancelled.");
					sleep(1);
					/* Repaint the screen */
					paint_video(thiswin);
					return(NULL);
					break;
			default:	break;
		}
	}
	return NULL; /* Hopefully, we never reach here */
}


/* Set a window selection */
char *vt_setsel(buf, len, startx, endx, starty, endy)
char *buf;
int len;
int startx, starty;
int endx, endy;
{
	window *thiswin;

	/* If nothing selected, return old selection */
	if ( (startx == endx) && (starty == endy) )
		return(buf);

	if ( startx == (physical.subwins[LOWER])->row_offset ) {
		/* Separator bar; return */
		return(buf);
	} else if ( startx < (physical.subwins[LOWER])->row_offset ) {
		thiswin = physical.subwins[UPPER];
	} else {
		thiswin = physical.subwins[LOWER];
		startx-=thiswin->row_offset;
		endx-=thiswin->row_offset;
	}
	getsel_video(thiswin, buf, len, startx, endx, starty, endy);
	return(buf);
}