File: cmd-move.c

package info (click to toggle)
sex 0.19.1-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 572 kB
  • ctags: 779
  • sloc: ansic: 6,273; sh: 179; makefile: 116
file content (700 lines) | stat: -rw-r--r-- 13,926 bytes parent folder | download | duplicates (2)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
 * File:	cmd-move.c
 * Purpose:	Functions that implement moving commands.
 * Author:	Lars Wirzenius
 * Version:	"@(#)SeX:$Id: cmd-move.c,v 1.27 1996/12/06 20:11:54 liw Exp $"
 */

#include <ctype.h>
#include <stdlib.h>
#include <publib.h>

#include "anchor.h"
#include "cmd.h"
#include "filewin.h"
#include "selections.h"
#include "win.h"
#include "x.h"
#include "error.h"
#include "tab.h"



/*
 * Prototypes for local functions.
 */
static int goto_mark(struct win *, int);
static int set_mark(struct win *, int);
static int line_is_empty(Sbuf *, long);



/*
 * Function:	cmd_forward
 * Purpose:	Move selection to next character.
 */
int cmd_forward(struct win *win) {
	Sbufmark *sel;
	long pos;

	cmd_prev_was_cut = 0;	
	anchor_up(win);
	sel = win_selection(win);
	pos = sbuf_mark_end(sel);
	if (sbuf_mark_length(sel) == 0) {
		if (pos == sbuf_length(win_buf(win)))
			return -1;
		++pos;
	}
	sbuf_remark(sel, pos, 0);
	sbuf_mark_set_columnar(sel, 0);
	win_show(win, pos);
	return 0;
}



/*
 * Function:	cmd_backward
 * Purpose:	Move selection to previous character.
 */
int cmd_backward(struct win *win) {
	Sbufmark *sel;
	long pos;
	
	cmd_prev_was_cut = 0;	
	anchor_up(win);
	sel = win_selection(win);
	pos = sbuf_mark_begin(sel);
	if (sbuf_mark_length(sel) == 0) {
		if (pos == 0)
			return -1;
		--pos;
	}
	sbuf_remark(sel, pos, 0);
	sbuf_mark_set_columnar(sel, 0);
	win_show(win, pos);
	return 0;
}



/*
 * Function:	cmd_next_line
 * Purpose:	Move selection to previous line.
 */
int cmd_next_line(struct win *win) {
	Sbuf *buf;
	Sbufmark *sel;
	long col, pos;
	
	cmd_prev_was_cut = 0;	
	anchor_up(win);
	sel = win_selection(win);
	pos = sbuf_mark_end(sel);
	if (sbuf_mark_length(sel) == 0) {
		buf = win_buf(win);
		col = sbuf_colno(buf, pos, tab_width());
		pos = sbuf_eoln(buf, pos);
		pos = sbuf_colpos(buf, pos, col, tab_width());
	}
	sbuf_remark(sel, pos, 0);
	sbuf_mark_set_columnar(sel, 0);
	win_show(win, pos);
	return 0;
}



/*
 * Function:	cmd_prev_line
 * Purpose:	Move selection to previous line.
 */
int cmd_prev_line(struct win *win) {
	Sbuf *buf;
	Sbufmark *sel;
	long col, pos;
	
	cmd_prev_was_cut = 0;	
	anchor_up(win);
	sel = win_selection(win);
	pos = sbuf_mark_begin(sel);
	if (sbuf_mark_length(sel) == 0) {
		buf = win_buf(win);
		col = sbuf_colno(buf, pos, tab_width());
		pos = sbuf_boln(buf, pos);
		if (pos > 0)
			pos = sbuf_boln(buf, pos-1);
		pos = sbuf_colpos(buf, pos, col, tab_width());
	}
	sbuf_remark(sel, pos, 0);
	sbuf_mark_set_columnar(sel, 0);
	win_show(win, pos);
	return 0;
}



/*
 * Function:	cmd_goto_boln
 * Purpose:	Move selection to beginning of line.
 */
int cmd_goto_boln(struct win *win) {
	Sbuf *buf;
	Sbufmark *sel;
	long pos;
	
	cmd_prev_was_cut = 0;	
	anchor_up(win);
	buf = win_buf(win);
	sel = win_selection(win);
	pos = sbuf_mark_begin(sel);
	pos = sbuf_boln(buf, pos);
	sbuf_remark(sel, pos, 0);
	sbuf_mark_set_columnar(sel, 0);
	win_show(win, pos);
	return 0;
}



/*
 * Function:	cmd_goto_eoln
 * Purpose:	Move selection to end of line.
 */
int cmd_goto_eoln(struct win *win) {
	Sbuf *buf;
	Sbufmark *sel;
	long pos, orig;
	
	cmd_prev_was_cut = 0;	
	anchor_up(win);
	buf = win_buf(win);
	sel = win_selection(win);
	pos = sbuf_mark_end(sel);
	if (pos != sbuf_mark_begin(sel))
		--pos;
	if (sbuf_charat(buf, pos) != '\n') {
		orig = pos;
		pos = sbuf_eoln(buf, pos);
		if (pos > orig && (pos < sbuf_length(buf) || 
				sbuf_charat(buf, pos-1) == '\n'))
			--pos;
	}
	sbuf_remark(sel, pos, 0);
	sbuf_mark_set_columnar(sel, 0);
	win_show(win, pos);
	return 0;
}



/*
 * Function:	cmd_next_word
 * Purpose:	Select empty string after next word after selection.
 */
int cmd_next_word(struct win *win) {
	Sbuf *buf;
	Sbufmark *sel;
	long pos, newpos;
	int c;
	
	cmd_prev_was_cut = 0;	
	anchor_up(win);
	buf = win_buf(win);
	sel = win_selection(win);
	pos = sbuf_mark_end(sel);
	newpos = pos;
	do {
		pos = newpos++;
		c = sbuf_charat(buf, pos);
	} while (c != EOF && !(isalnum(c) || c == '_'));
	pos = sbuf_eow(buf, pos);
	sbuf_remark(sel, pos, 0);
	sbuf_mark_set_columnar(sel, 0);
	win_show(win, pos);
	return 0;
}



/*
 * Function:	cmd_prev_word
 * Purpose:	Select empty string before word before selection.
 */
int cmd_prev_word(struct win *win) {
	Sbuf *buf;
	Sbufmark *sel;
	long pos;
	int c;
	
	cmd_prev_was_cut = 0;	
	anchor_up(win);
	buf = win_buf(win);
	sel = win_selection(win);
	pos = sbuf_mark_begin(sel);

	if (pos == 0)
		return -1;

	--pos;
	while (pos > 0 && !isalnum(c = sbuf_charat(buf, pos)) && c != '_')
		--pos;

	pos = sbuf_bow(buf, pos);
	sbuf_remark(sel, pos, 0);
	sbuf_mark_set_columnar(sel, 0);
	win_show(win, pos);
	return 0;
}



/*
 * Function:	cmd_query_goto_line
 * Purpose:	Popup dialog box to let user enter line number.
 */
int cmd_query_goto_line(struct win *win) {
	struct filewin *fw;

	cmd_prev_was_cut = 0;	
	anchor_up(win);
	fw = win_filewin(win, WIN_LINENO);
	filewin_popup(fw);
	return 0;
}



/*
 * Function:	cmd_goto_line
 * Purpose:	Go to the line the user has asked for.
 */
int cmd_goto_line(struct win *win) {
	long lineno, pos;
	char *p;

	cmd_prev_was_cut = 0;	
	anchor_up(win);
	p = filewin_filename(win_filewin(win, WIN_LINENO));
	while (*p != '\0' && !isdigit(*p))
		++p;
	lineno = atol(p);
	if (lineno < 1) {
		error(win, "bad line number");
		return -1;
	}

	pos = sbuf_linepos(win_buf(win), lineno-1);

	sbuf_remark(win_selection(win), pos, 0);
	sbuf_mark_set_columnar(win_selection(win), 0);
	win_show(win, pos);

	return 0;
}



/*
 * Function:	cmd_prev_page
 * Purpose:	Select empty string at top of previous windowful.
 */
int cmd_prev_page(struct win *win) {
	Sbuf *buf;
	Sbufmark *sel;
	long pos;
	int i, rows, cols;

	cmd_prev_was_cut = 0;	
	anchor_up(win);
	buf = win_buf(win);
	sel = win_selection(win);

	pos = win_get_top(win);
	if (pos > 0) {
		win_get_dimensions(win, &rows, &cols);
		for (i = 0; i < rows-2 && pos > 0; ++i)
			pos = sbuf_boln(buf, pos-1);
		win_set_top(win, pos);
	}

	sbuf_remark(sel, pos, 0);
	sbuf_mark_set_columnar(sel, 0);

	return 0;
}



/*
 * Function:	cmd_next_page
 * Purpose:	Select empty string at top of next windowful.
 */
int cmd_next_page(struct win *win) {
	Sbuf *buf;
	Sbufmark *sel;
	long pos;
	int i, rows, cols;

	cmd_prev_was_cut = 0;	
	anchor_up(win);
	buf = win_buf(win);
	sel = win_selection(win);

	pos = win_get_top(win);
	win_get_dimensions(win, &rows, &cols);
	for (i = 0; i < rows-2; ++i)
		pos = sbuf_eoln(buf, pos);

	sbuf_remark(sel, pos, 0);
	sbuf_mark_set_columnar(sel, 0);
	win_set_top(win, pos);

	return 0;
}



/*
 * Function:	cmd_center
 * Purpose:	Scroll window so that beginning of selection is at center.
 */
int cmd_center(struct win *win) {
	Sbuf *buf;
	long pos;
	int i, rows, cols;

	cmd_prev_was_cut = 0;	
	anchor_up(win);
	buf = win_buf(win);

	pos = sbuf_mark_begin(win_selection(win));
	win_get_dimensions(win, &rows, &cols);
	for (i = rows/2; i > 0 && pos > 0; --i)
		pos = sbuf_boln(buf, pos-1);
	win_set_top(win, pos);

	return 0;
}



/*
 * Function:	cmd_scroll_prev_page
 * Purpose:	Move window one screenful backwards.
 */
int cmd_scroll_prev_page(struct win *win) {
	Sbuf *buf;
	long pos;
	int i, rows, cols;

	cmd_prev_was_cut = 0;	
	buf = win_buf(win);

	pos = win_get_top(win);
	if (pos > 0) {
		win_get_dimensions(win, &rows, &cols);
		for (i = 0; i < rows-2 && pos > 0; ++i)
			pos = sbuf_boln(buf, pos-1);
		win_set_top(win, pos);
	}

	return 0;
}



/*
 * Function:	cmd_scroll_next_page
 * Purpose:	Move window one screenful forwards.
 */
int cmd_scroll_next_page(struct win *win) {
	Sbuf *buf;
	long pos;
	int i, rows, cols;

	cmd_prev_was_cut = 0;	
	buf = win_buf(win);

	pos = win_get_top(win);
	win_get_dimensions(win, &rows, &cols);
	for (i = 0; i < rows-2; ++i)
		pos = sbuf_eoln(buf, pos);

	win_set_top(win, pos);

	return 0;
}



/*
 * Function:	cmd_scroll_left_one
 * Purpose:	Scroll window one column to the left.
 */
int cmd_scroll_left_one(struct win *win) {
	long col;
	
	col = win_get_left(win);
	if (col == 0) {
		error(win, "Can't scroll further to the left");
		return -1;
	}
	win_set_left(win, col-1);
	return 0;
}



/*
 * Function:	cmd_scroll_right_one
 * Purpose:	Scroll window one column to the right.
 */
int cmd_scroll_right_one(struct win *win) {
	win_set_left(win, win_get_left(win) + 1);
	return 0;
}



/*
 * Function:	cmd_goto_bof
 * Purpose:	Select empty string at beginning of file.
 */
int cmd_goto_bof(struct win *win) {
	cmd_prev_was_cut = 0;	
	anchor_up(win);
	sbuf_remark(win_selection(win), 0, 0);
	sbuf_mark_set_columnar(win_selection(win), 0);
	win_show(win, 0);
	return 0;
}



/*
 * Function:	cmd_goto_eof
 * Purpose:	Select empty string at end of file.
 */
int cmd_goto_eof(struct win *win) {
	long len;

	cmd_prev_was_cut = 0;	
	anchor_up(win);
	len = sbuf_length(win_buf(win));
	sbuf_remark(win_selection(win), len, 0);
	sbuf_mark_set_columnar(win_selection(win), 0);
	win_show(win, len);
	return 0;
}



/*
 * Function:	cmd_goto_top
 * Purpose:	Select empty string at top of visible text.
 */
int cmd_goto_top(struct win *win) {
	cmd_prev_was_cut = 0;	
	anchor_up(win);
	sbuf_remark(win_selection(win), win_get_top(win), 0);
	sbuf_mark_set_columnar(win_selection(win), 0);
	return 0;
}



/*
 * Function:	cmd_goto_bottom
 * Purpose:	Select empty string at bottom of visible text.
 */
int cmd_goto_bottom(struct win *win) {
	sbuf_remark(win_selection(win), win_get_bottom(win), 0);
	sbuf_mark_set_columnar(win_selection(win), 0);
	return 0;
}



/*
 * Function:	cmd_goto_selection
 * Purpose:	Set search string to selection, then search after selection.
 */

static void do_goto_line(char *str, long len, void *win) {
	filewin_set_filename(win_filewin(win, WIN_LINENO), str);
	(void) cmd_goto_line(win);
}

int cmd_goto_selection(struct win *win) {
	cmd_prev_was_cut = 0;	
	anchor_up(win);
	if (sel_string(win, do_goto_line, win) == -1)
		return -1;
	return 0;
}



/*
 * Function:	cmd_prev_para
 * Purpose:	Select empty string at beginning of previous paragraph.
 */
int cmd_prev_para(struct win *win) {
	Sbuf *buf;
	Sbufmark *sel;
	long pos;

	cmd_prev_was_cut = 0;	
	anchor_up(win);

	buf = win_buf(win);
	sel = win_selection(win);
	pos = sbuf_boln(buf, sbuf_mark_begin(sel));

	if (pos > 0)
		pos = sbuf_boln(buf, pos-1);
	while (pos > 0 && line_is_empty(buf, pos))
		pos = sbuf_boln(buf, pos-1);
	while (pos > 0 && !line_is_empty(buf, pos))
		pos = sbuf_boln(buf, pos-1);
	if (line_is_empty(buf, pos))
		pos = sbuf_eoln(buf, pos);

	sbuf_remark(sel, pos, 0);
	sbuf_mark_set_columnar(sel, 0);
	win_show(win, pos);
	return 0;
}



/*
 * Function:	cmd_next_para
 * Purpose:	Select empty string at next end of previous paragraph.
 */
int cmd_next_para(struct win *win) {
	Sbuf *buf;
	Sbufmark *sel;
	long len, pos;
	
	cmd_prev_was_cut = 0;	
	anchor_up(win);

	buf = win_buf(win);
	sel = win_selection(win);
	len = sbuf_length(buf);
	pos = sbuf_boln(buf, sbuf_mark_begin(sel));

	if (pos < len)
		pos = sbuf_eoln(buf, pos);
	while (pos < len && line_is_empty(buf, pos))
		pos = sbuf_eoln(buf, pos);
	while (pos < len && !line_is_empty(buf, pos))
		pos = sbuf_eoln(buf, pos);
		
	if (pos > 0 && line_is_empty(buf, pos))
		--pos;

	sbuf_remark(sel, pos, 0);
	sbuf_mark_set_columnar(sel, 0);
	win_show(win, pos);
	return 0;
}



/*
 * Function:	cmd_goto_mark_0, ...
 * Purpose:	Select the empty string at user-settable mark 0/1/2/.../9.
 */
int cmd_goto_mark_0(struct win *win) { return goto_mark(win, 0); }
int cmd_goto_mark_1(struct win *win) { return goto_mark(win, 1); }
int cmd_goto_mark_2(struct win *win) { return goto_mark(win, 2); }
int cmd_goto_mark_3(struct win *win) { return goto_mark(win, 3); }
int cmd_goto_mark_4(struct win *win) { return goto_mark(win, 4); }
int cmd_goto_mark_5(struct win *win) { return goto_mark(win, 5); }
int cmd_goto_mark_6(struct win *win) { return goto_mark(win, 6); }
int cmd_goto_mark_7(struct win *win) { return goto_mark(win, 7); }
int cmd_goto_mark_8(struct win *win) { return goto_mark(win, 8); }
int cmd_goto_mark_9(struct win *win) { return goto_mark(win, 9); }




/*
 * Function:	cmd_set_mark_0, ...
 * Purpose:	Set the user-settable mark 0-9 at beginning of selection.
 */
int cmd_set_mark_0(struct win *win) { return set_mark(win, 0); }
int cmd_set_mark_1(struct win *win) { return set_mark(win, 1); }
int cmd_set_mark_2(struct win *win) { return set_mark(win, 2); }
int cmd_set_mark_3(struct win *win) { return set_mark(win, 3); }
int cmd_set_mark_4(struct win *win) { return set_mark(win, 4); }
int cmd_set_mark_5(struct win *win) { return set_mark(win, 5); }
int cmd_set_mark_6(struct win *win) { return set_mark(win, 6); }
int cmd_set_mark_7(struct win *win) { return set_mark(win, 7); }
int cmd_set_mark_8(struct win *win) { return set_mark(win, 8); }
int cmd_set_mark_9(struct win *win) { return set_mark(win, 9); }



/**********************************************************************
 * Local functions follow.
 */


/*
 * Function:	goto_mark
 * Purpose:	Select the empty string at a user-settable mark.
 * Arguments:	win	the window
 *		i	the number of the mark
 */
static int goto_mark(struct win *win, int i) {
	Sbufmark *m;
	long pos;

	anchor_up(win);
	m = sbuf_find_mark_by_code(win_buf(win), USER_MARK_0 + i);
	if (m == NULL)
		return -1;
	pos = sbuf_mark_begin(m);
	sbuf_remark(win_selection(win), pos, 0);
	sbuf_mark_set_columnar(win_selection(win), 0);
	win_show(win, pos);
	return 0;
}



/*
 * Function:	set_mark
 * Purpose:	Set a user-settable mark at beginning of selection.
 */
static int set_mark(struct win *win, int i) {
	Sbufmark *m;

	anchor_up(win);
	m = sbuf_find_mark_by_code(win_buf(win), USER_MARK_0 + i);
	if (m == NULL)
		return -1;
	sbuf_remark(m, sbuf_mark_begin(win_selection(win)), 0);
	return 0;
}



/*
 * Function:	line_is_empty
 * Purpose:	Is line beginning at pos empty (only whitespace)?
 */
static int line_is_empty(Sbuf *buf, long pos) {
	int c;

	do {
		c = sbuf_charat(buf, pos);
		++pos;
	} while (c != '\n' && isspace(c));
	return c == '\n' || c == EOF;
}