about summary refs log tree commit diff
path: root/Src/Zle/zle_move.c
blob: d5f464c2c328dc6e685bbce4aea2696676d66ae7 (plain) (blame)
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
/*
 * zle_move.c - editor movement
 *
 * This file is part of zsh, the Z shell.
 *
 * Copyright (c) 1992-1997 Paul Falstad
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and to distribute modified versions of this software for any
 * purpose, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * In no event shall Paul Falstad or the Zsh Development Group be liable
 * to any party for direct, indirect, special, incidental, or consequential
 * damages arising out of the use of this software and its documentation,
 * even if Paul Falstad and the Zsh Development Group have been advised of
 * the possibility of such damage.
 *
 * Paul Falstad and the Zsh Development Group specifically disclaim any
 * warranties, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose.  The software
 * provided hereunder is on an "as is" basis, and Paul Falstad and the
 * Zsh Development Group have no obligation to provide maintenance,
 * support, updates, enhancements, or modifications.
 *
 */

#include "zle.mdh"
#include "zle_move.pro"

static int vimarkcs[26], vimarkline[26];

#ifdef MULTIBYTE_SUPPORT
/*
 * Take account of combining characters when moving left.  If
 * we are on a zero-width printable wide character and are
 * treating these as part of the base character for display purposes,
 * move left until we reach a non-zero-width printable character
 * (the base character).  If we reach something else first, stay where we
 * were.
 *
 * If setpos is non-zero, update zlecs on success.
 * Return 1 if we were on a combining char and could move, else 0.
 */
/**/
int
alignmultiwordleft(int *pos, int setpos)
{
    int loccs = *pos;

    /* generic nothing to do test */
    if (!isset(COMBININGCHARS) || loccs == zlell || loccs == 0)
	return 0;

    /* need to be on combining character */
    if (!IS_COMBINING(zleline[loccs]))
	 return 0;

    /* yes, go left */
    loccs--;

    for (;;) {
	if (IS_BASECHAR(zleline[loccs])) {
	    /* found start position */
	    if (setpos)
		*pos = loccs;
	    return 1;
	} else if (!IS_COMBINING(zleline[loccs])) {
	    /* no go */
	    return 0;
	}
	/* combining char, keep going */
	if (loccs-- == 0)
	    return 0;
    }
}


/*
 * Same principle when moving right.  We need to check if
 * alignmultiwordleft() would be successful in order to decide
 * if we're on a combining character, and if so we move right to
 * anything that isn't one.
 */
/**/
int
alignmultiwordright(int *pos, int setpos)
{
    int loccs;

    /*
     * Are we on a suitable character?
     */
    if (!alignmultiwordleft(pos, 0))
	return 0;

    /* yes, go right */
    loccs = *pos + 1;

    while (loccs < zlell) {
	/* Anything other than a combining char will do here */
	if (!IS_COMBINING(zleline[loccs])) {
	    if (setpos)
		*pos = loccs;
	    return 1;
	}
	loccs++;
    }

    if (setpos)
	*pos = loccs;
    return 1;
}


/* Move cursor right, checking for combining characters */

/**/
mod_export void
inccs(void)
{
    zlecs++;
    alignmultiwordright(&zlecs, 1);
}


/* Move cursor left, checking for combining characters */

/**/
mod_export void
deccs(void)
{
    zlecs--;
    alignmultiwordleft(&zlecs, 1);
}

/* Same utilities for general position */

/**/
mod_export void
incpos(int *pos)
{
    (*pos)++;
    alignmultiwordright(pos, 1);
}


/**/
mod_export void
decpos(int *pos)
{
    (*pos)--;
    alignmultiwordleft(pos, 1);
}
#endif


/* Size of buffer in the following function */
#define BMC_BUFSIZE MB_CUR_MAX
/*
 * For a metafied string that starts at "start" and where the
 * current position is "ptr", go back one full character,
 * taking account of combining characters if necessary.
 */

/**/
char *
backwardmetafiedchar(char *start, char *endptr, convchar_t *retchr)
{
#ifdef MULTIBYTE_SUPPORT
    int charlen = 0;
    char *last = NULL, *bufptr, *ptr = endptr;
    convchar_t lastc = (convchar_t)0; /* not used, silence compiler */
    mbstate_t mbs;
    size_t ret;
    wchar_t wc;
    VARARR(char, buf, BMC_BUFSIZE);

    bufptr = buf + BMC_BUFSIZE;
    while (ptr > start) {
	ptr--;
	/*
	 * Scanning backwards we're not guaranteed ever to find a
	 * valid character.  If we've looked as far as we should
	 * need to, give up.
	 */
	if (bufptr-- == buf)
	    break;
	charlen++;
	if (ptr > start && ptr[-1] == Meta)
	    *bufptr = *ptr-- ^ 32;
	else
	    *bufptr = *ptr;

	/* we always need to restart the character from scratch */
	memset(&mbs, 0, sizeof(mbs));
	ret = mbrtowc(&wc, bufptr, charlen, &mbs);
	if (ret == 0) {
	    /* NULL: unlikely, but handle anyway. */
	    if (last) {
		if (retchr)
		    *retchr = lastc;
		return last;
	    } else {
		if (retchr)
		    *retchr = wc;
		return ptr;
	    }
	}
	if (ret != (size_t)-1) {
	    if (ret < (size_t)charlen) {
		/* The last character didn't convert, so use it raw. */
		break;
	    }
	    if (!isset(COMBININGCHARS)) {
		if (retchr)
		    *retchr = wc;
		return ptr;
	    }
	    if (!IS_COMBINING(wc)) {
		/* not a combining character... */
		if (last) {
		    /*
		     * ... but we were looking for a suitable base character,
		     * test it.
		     */
		    if (IS_BASECHAR(wc)) {
			/*
			 * Yes, this will do.
			 */
			if (retchr)
			    *retchr = wc;
			return ptr;
		    } else {
			/* No, just return the first character we found */
			if (retchr)
			    *retchr = lastc;
			return last;
		    }
		}
		/* This is the first character, so just return it. */
		if (retchr)
		    *retchr = wc;
		return ptr;
	    }
	    if (!last) {
		/* still looking for the character immediately before ptr */
		last = ptr;
		lastc = wc;
	    }
	    /* searching for base character of combining character */
	    charlen = 0;
	    bufptr = buf + BMC_BUFSIZE;
	}
	/*
	 * Else keep scanning this character even if MB_INVALID:  we can't
	 * expect MB_INCOMPLETE to work when moving backwards.
	 */
    }
    /*
     * Found something we didn't like, was there a good character
     * immediately before ptr?
     */
    if (last) {
	if (retchr)
	    *retchr = lastc;
	return last;
    }
    /*
     * No, we couldn't find any good character, so just treat
     * the last unmetafied byte we found as a character.
     */
#endif
    if (endptr > start) {
	if (endptr > start - 1 && endptr[-2] == Meta)
	{
	    if (retchr)
		*retchr = (convchar_t)(endptr[-1] ^ 32);
	    return endptr - 2;
	}
	else
	{
	    if (retchr)
		*retchr = (convchar_t)endptr[-1];
	    return endptr - 1;
	}
    }
    if (retchr)
	*retchr = (convchar_t)0;
    return endptr;
}


/**/
int
beginningofline(char **args)
{
    int n = zmult;

    if (n < 0) {
	int ret;
	zmult = -n;
	ret = endofline(args);
	zmult = n;
	return ret;
    }
    while (n--) {
	int pos;

	if (zlecs == 0)
	    return 0;
	pos = zlecs;
	DECPOS(pos);
	if (zleline[pos] == '\n') {
	    zlecs = pos;
	    if (!zlecs)
		return 0;
	}

	/* works OK with combining chars since '\n' must be on its own */
	while (zlecs && zleline[zlecs - 1] != '\n')
	    zlecs--;
    }
    return 0;
}

/**/
int
endofline(char **args)
{
    int n = zmult;

    if (n < 0) {
	int ret;
	zmult = -n;
	ret = beginningofline(args);
	zmult = n;
	return ret;
    }
    while (n--) {
	if (zlecs >= zlell) {
	    zlecs = zlell;
	    return 0;
	}
	if (zleline[zlecs] == '\n')
	    if (++zlecs == zlell)
		return 0;
	while (zlecs != zlell && zleline[zlecs] != '\n')
	    zlecs++;
    }
    return 0;
}

/**/
int
beginningoflinehist(char **args)
{
    int n = zmult;

    if (n < 0) {
	int ret;
	zmult = -n;
	ret = endoflinehist(args);
	zmult = n;
	return ret;
    }
    while (n) {
	int pos;

	if (zlecs == 0)
	    break;
	pos = zlecs;
	DECPOS(pos);
	if (zleline[pos] == '\n') {
	    zlecs = pos;
	    if (!pos)
		break;
	}

	/* works OK with combining chars since '\n' must be on its own */
	while (zlecs && zleline[zlecs - 1] != '\n')
	    zlecs--;
	n--;
    }
    if (n) {
	int m = zmult, ret;

	zmult = n;
	ret = uphistory(args);
	zmult = m;
	zlecs = 0;
	return ret;
    }
    return 0;
}

/**/
int
endoflinehist(char **args)
{
    int n = zmult;

    if (n < 0) {
	int ret;
	zmult = -n;
	ret = beginningoflinehist(args);
	zmult = n;
	return ret;
    }
    while (n) {
	if (zlecs >= zlell) {
	    zlecs = zlell;
	    break;
	}
	if (zleline[zlecs] == '\n')
	    if (++zlecs == zlell)
		break;
	while (zlecs != zlell && zleline[zlecs] != '\n')
	    zlecs++;
	n--;
    }
    if (n) {
	int m = zmult, ret;

	zmult = n;
	ret = downhistory(args);
	zmult = m;
	return ret;
    }
    return 0;
}

/**/
int
forwardchar(char **args)
{
    int n = zmult;

    if (n < 0) {
	int ret;
	zmult = -n;
	ret = backwardchar(args);
	zmult = n;
	return ret;
    }

    /*
     * If handling combining characters with the base character,
     * we skip over the whole set in one go, so need to check.
     */
    while (zlecs < zlell && n--)
	INCCS();
    return 0;
}

/**/
int
backwardchar(char **args)
{
    int n = zmult;

    if (n < 0) {
	int ret;
	zmult = -n;
	ret = forwardchar(args);
	zmult = n;
	return ret;
    }

    while (zlecs > 0 && n--)
	DECCS();
    return 0;
}

/**/
int
setmarkcommand(UNUSED(char **args))
{
    if (zmult < 0) {
	region_active = 0;
	return 0;
    }
    mark = zlecs;
    region_active = 1;
    return 0;
}

/**/
int
exchangepointandmark(UNUSED(char **args))
{
    int x;

    if (zmult == 0) {
	region_active = 1;
	return 0;
    }
    x = mark;
    mark = zlecs;
    zlecs = x;
    if (zlecs > zlell)
	zlecs = zlell;
    if (zmult > 0)
	region_active = 1;
    return 0;
}

/**/
int
vigotocolumn(UNUSED(char **args))
{
    int x, y, n = zmult;

    findline(&x, &y);
    if (n >= 0) {
	if (n)
	    n--;
	zlecs = x;
	while (zlecs < y && n--)
	    INCCS();
    } else {
	zlecs = y;
	n = -n;
	while (zlecs > x && n--)
	    DECCS();
    }
    return 0;
}

/**/
int
vimatchbracket(UNUSED(char **args))
{
    int ocs = zlecs, dir, ct;
    unsigned char oth, me;

    if ((zlecs == zlell || zleline[zlecs] == '\n') && zlecs > 0)
	DECCS();

  otog:
    if (zlecs == zlell || zleline[zlecs] == '\n') {
	zlecs = ocs;
	return 1;
    }
    switch (me = zleline[zlecs]) {
    case '{':
	dir = 1;
	oth = '}';
	break;
    case /*{*/ '}':
	virangeflag = -virangeflag;
	dir = -1;
	oth = '{'; /*}*/
	break;
    case '(':
	dir = 1;
	oth = ')';
	break;
    case ')':
	virangeflag = -virangeflag;
	dir = -1;
	oth = '(';
	break;
    case '[':
	dir = 1;
	oth = ']';
	break;
    case ']':
	virangeflag = -virangeflag;
	dir = -1;
	oth = '[';
	break;
    default:
	INCCS();
	goto otog;
    }
    ct = 1;
    while (zlecs >= 0 && zlecs < zlell && ct) {
	if (dir < 0)
	    DECCS();
	else
	    INCCS();
	if (zleline[zlecs] == oth)
	    ct--;
	else if (zleline[zlecs] == me)
	    ct++;
    }
    if (zlecs < 0 || zlecs >= zlell) {
	zlecs = ocs;
	return 1;
    } else if(dir > 0 && virangeflag)
	INCCS();
    return 0;
}

/**/
int
viforwardchar(char **args)
{
    int lim = findeol() - invicmdmode();
    int n = zmult;

    if (n < 0) {
	int ret;
	zmult = -n;
	ret = vibackwardchar(args);
	zmult = n;
	return ret;
    }
    if (zlecs >= lim)
	return 1;
    while (n-- && zlecs < lim)
	INCCS();
    return 0;
}

/**/
int
vibackwardchar(char **args)
{
    int n = zmult;

    if (n < 0) {
	int ret;
	zmult = -n;
	ret = viforwardchar(args);
	zmult = n;
	return ret;
    }
    if (zlecs == findbol())
	return 1;
    while (n-- && zlecs > 0) {
	DECCS();
	if (zleline[zlecs] == '\n') {
	    zlecs++;
	    break;
	}
    }
    return 0;
}

/**/
int
viendofline(UNUSED(char **args))
{
    int oldcs = zlecs, n = zmult;

    if (n < 1)
	return 1;
    while(n--) {
	if (zlecs > zlell) {
	    zlecs = oldcs;
	    return 1;
	}
	zlecs = findeol() + 1;
    }
    DECCS();
    lastcol = 1<<30;
    return 0;
}

/**/
int
vibeginningofline(UNUSED(char **args))
{
    zlecs = findbol();
    return 0;
}

static ZLE_INT_T vfindchar;
static int vfinddir, tailadd;

/**/
int
vifindnextchar(char **args)
{
    if ((vfindchar = vigetkey()) != ZLEEOF) {
	vfinddir = 1;
	tailadd = 0;
	return vifindchar(0, args);
    }
    return 1;
}

/**/
int
vifindprevchar(char **args)
{
    if ((vfindchar = vigetkey()) != ZLEEOF) {
	vfinddir = -1;
	tailadd = 0;
	return vifindchar(0, args);
    }
    return 1;
}

/**/
int
vifindnextcharskip(char **args)
{
    if ((vfindchar = vigetkey()) != ZLEEOF) {
	vfinddir = 1;
	tailadd = -1;
	return vifindchar(0, args);
    }
    return 1;
}

/**/
int
vifindprevcharskip(char **args)
{
    if ((vfindchar = vigetkey()) != ZLEEOF) {
	vfinddir = -1;
	tailadd = 1;
	return vifindchar(0, args);
    }
    return 1;
}

/**/
int
vifindchar(int repeat, char **args)
{
    int ocs = zlecs, n = zmult;

    if (!vfinddir)
	return 1;
    if (n < 0) {
	int ret;
	zmult = -n;
	ret = virevrepeatfind(args);
	zmult = n;
	return ret;
    }
    if (repeat && tailadd != 0) {
	if (vfinddir > 0) {
	    if(zlecs < zlell && (ZLE_INT_T)zleline[zlecs+1] == vfindchar)
		INCCS();
	}
	else {
	    if(zlecs > 0 && (ZLE_INT_T)zleline[zlecs-1] == vfindchar)
		DECCS();
	}
    }
    while (n--) {
	do {
	    if (vfinddir > 0)
		INCCS();
	    else
		DECCS();
	} while (zlecs >= 0 && zlecs < zlell
	    && (ZLE_INT_T)zleline[zlecs] != vfindchar
	    && zleline[zlecs] != ZWC('\n'));
	if (zlecs < 0 || zlecs >= zlell || zleline[zlecs] == ZWC('\n')) {
	    zlecs = ocs;
	    return 1;
	}
    }
    if (tailadd > 0)
	INCCS();
    else if (tailadd < 0)
	DECCS();
    if (vfinddir == 1 && virangeflag)
	INCCS();
    return 0;
}

/**/
int
virepeatfind(char **args)
{
    return vifindchar(1, args);
}

/**/
int
virevrepeatfind(char **args)
{
    int ret;

    if (zmult < 0) {
	zmult = -zmult;
	ret = vifindchar(1, args);
	zmult = -zmult;
	return ret;
    }
    tailadd = -tailadd;
    vfinddir = -vfinddir;
    ret = vifindchar(1, args);
    vfinddir = -vfinddir;
    tailadd = -tailadd;
    return ret;
}

/**/
int
vifirstnonblank(UNUSED(char **args))
{
    zlecs = findbol();
    while (zlecs != zlell && ZC_iblank(zleline[zlecs]))
	INCCS();
    return 0;
}

/**/
int
visetmark(UNUSED(char **args))
{
    ZLE_INT_T ch;

    ch = getfullchar(0);
    if (ch < ZWC('a') || ch > ZWC('z'))
	return 1;
    ch -= ZWC('a');
    vimarkcs[ch] = zlecs;
    vimarkline[ch] = histline;
    return 0;
}

/**/
int
vigotomark(UNUSED(char **args))
{
    ZLE_INT_T ch;

    ch = getfullchar(0);
    if (ch < ZWC('a') || ch > ZWC('z'))
	return 1;
    ch -= ZWC('a');
    if (!vimarkline[ch])
	return 1;
    if (curhist != vimarkline[ch] && !zle_goto_hist(vimarkline[ch], 0, 0)) {
	vimarkline[ch] = 0;
	return 1;
    }
    zlecs = vimarkcs[ch];
    if (zlecs > zlell)
	zlecs = zlell;
    return 0;
}

/**/
int
vigotomarkline(char **args)
{
    vigotomark(args);
    return vifirstnonblank(zlenoargs);
}