aboutsummaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/gdb/readline/vi_mode.c
blob: 3a13cc6c862a9d55d5b24037c10b61a1105774f7 (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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
/*-
 * This code is derived from software copyrighted by the Free Software
 * Foundation.
 *
 * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
 * Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory.
 *
 *	@(#)vi_mode.c	6.4 (Berkeley) 5/8/91
 */

/* vi_mode.c -- A vi emulation mode for Bash.
   Mostly written by Jeff Sparkes (jeff1@????).
 */


/* **************************************************************** */
/*								    */
/*			VI Emulation Mode			    */
/*								    */
/* **************************************************************** */

/* Last string searched for from `/' or `?'. */
static char *vi_last_search = (char *)NULL;
static int vi_histpos;

/* *** UNCLEAN *** */
/* Command keys which do movement for xxx_to commands. */
static char *vi_motion = " hl^$0ftFt;,%wbeWBE|";

/* Keymap used for vi replace characters.  Created dynamically since
   rarely used. */
static Keymap vi_replace_map = (Keymap)NULL;

/* The number of characters inserted in the last replace operation. */
static vi_replace_count = 0;

/* Yank the nth arg from the previous line into this line at point. */
rl_vi_yank_arg (count)
     int count;
{
  rl_yank_nth_arg (count);
}

/* Search again for the last thing searched for. */
rl_vi_search_again (ignore, key)
     int ignore, key;
{
  switch (key)
    {
    case 'n':
      rl_vi_dosearch (vi_last_search, -1);
      break;

    case 'N':
      rl_vi_dosearch (vi_last_search, 1);
      break;
    }
}

/* Do a vi style search. */
rl_vi_search (count, key)
     int count, key;
{
  int dir, c;
  char *p;

  switch (key)
    {
    case '?':
      dir = 1;
      break;

    case '/':
      dir = -1;
      break;

    default:
      ding ();
      return;
    }

  vi_histpos = where_history ();
  maybe_save_line ();

  /* Reuse the line input buffer to read the search string. */
  the_line[0] = 0;
  rl_end = rl_point = 0;
  p = (char *)alloca (2 + (rl_prompt ? strlen (rl_prompt) : 0));

  sprintf (p, "%s%c", rl_prompt ? rl_prompt : "", key);

  rl_message (p);

  while (c = rl_read_key (in_stream))
    {
      switch (c)
	{
	case CTRL('W'):
	case CTRL('U'):
	case CTRL('H'):
	case RUBOUT:
	  rl_dispatch (c, keymap);
	  break;

	case ESC:
	case RETURN:
	case NEWLINE:
	  goto dosearch;
	  break;

	case CTRL('C'):
	  maybe_unsave_line ();
	  rl_clear_message ();
	  rl_point = 0;
	  ding ();
	  return;

	default:
	  rl_insert (1, c);
	  break;
	}
      rl_redisplay ();
    }
 dosearch:
  if (vi_last_search)
    free (vi_last_search);

  vi_last_search = savestring (the_line);
  rl_vi_dosearch (the_line, dir);
}

rl_vi_dosearch (string, dir)
     char *string;
     int dir;
{
  int old, save = vi_histpos;
  HIST_ENTRY *h;

  if (string == 0 || *string == 0 || vi_histpos < 0)
    {
      ding ();
      return;
    }

  if ((save = history_search_pos (string, dir, vi_histpos + dir)) == -1)
    {
      maybe_unsave_line ();
      rl_clear_message ();
      rl_point = 0;
      ding ();
      return;
    }

  vi_histpos = save;

  old = where_history ();
  history_set_pos (vi_histpos);
  h = current_history ();
  history_set_pos (old);

  strcpy (the_line, h->line);
  rl_undo_list = (UNDO_LIST *)h->data;
  rl_end = strlen (the_line);
  rl_point = 0;
  rl_clear_message ();
}

/* Completion, from vi's point of view. */
rl_vi_complete (ignore, key)
     int ignore, key;
{
  if (!whitespace (the_line[rl_point]))
    {
      rl_vi_end_word (1, 'E');
      rl_point++;
    }
  rl_complete_internal ('*');
  rl_vi_insertion_mode ();
}

/* Previous word in vi mode. */
rl_vi_prev_word (count, key)
     int count, key;
{
  if (count < 0)
    {
      rl_vi_next_word (-count, key);
      return;
    }

  if (uppercase_p (key))
    rl_vi_bWord (count);
  else
    rl_vi_bword (count);
}

/* Next word in vi mode. */
rl_vi_next_word (count, key)
     int count;
{
  if (count < 0)
    {
      rl_vi_prev_word (-count, key);
      return;
    }

  if (uppercase_p (key))
    rl_vi_fWord (count);
  else
    rl_vi_fword (count);
}

/* Move to the end of the ?next? word. */
rl_vi_end_word (count, key)
     int count, key;
{
  if (count < 0)
    {
      ding ();
      return;
    }

  if (uppercase_p (key))
    rl_vi_eWord (count);
  else
    rl_vi_eword (count);
}

/* Move forward a word the way that 'W' does. */
rl_vi_fWord (count)
     int count;
{
  while (count-- && rl_point < (rl_end - 1))
    {
      /* Skip until whitespace. */
      while (!whitespace (the_line[rl_point]) && rl_point < rl_end)
	rl_point++;

      /* Now skip whitespace. */
      while (whitespace (the_line[rl_point]) && rl_point < rl_end)
	rl_point++;
    }
}

rl_vi_bWord (count)
     int count;
{
  while (count-- && rl_point > 0)
    {
      while (rl_point-- >= 0 && whitespace (the_line[rl_point]));
      while (rl_point >= 0 && !whitespace (the_line[rl_point]))
	rl_point--;
      rl_point++;
    }
}

rl_vi_eWord (count)
     int count;
{
  while (count -- && rl_point < (rl_end - 1))
    {
      while (rl_point++ < rl_end && whitespace (the_line[rl_point]));
      while (rl_point++ < rl_end && !whitespace (the_line[rl_point]));
      rl_point--;
    }
}

rl_vi_fword (count)
     int count;
{
  while (count -- && rl_point < (rl_end - 1))
    {
      if (isident (the_line[rl_point]))
	{
	  while (isident (the_line[rl_point]) && rl_point < rl_end)
	    rl_point += 1;
	}
      else if (!whitespace (the_line[rl_point]))
	{
	  while (!isident (the_line[rl_point]) &&
		 !whitespace (the_line[rl_point]) && rl_point < rl_end)
	    rl_point += 1;
	}

      while (whitespace (the_line[rl_point]) && rl_point < rl_end)
	rl_point++;
    }
}

rl_vi_bword (count)
     int count;
{
  while (count -- && rl_point > 0)
    {
      while (--rl_point > 0 && whitespace (the_line[rl_point]));
      if (rl_point > 0)
	{
	  if (isident (the_line[rl_point]))
	    while (--rl_point >= 0 && isident (the_line[rl_point]));
	  else
	    while (--rl_point >= 0 && !isident (the_line[rl_point]) &&
		   !whitespace (the_line[rl_point]));
	  rl_point++;
	}
    }
}

rl_vi_eword (count)
     int count;
{
  while (count -- && rl_point < rl_end - 1)
    {
      while (++rl_point < rl_end && whitespace (the_line[rl_point]));

      if (rl_point < rl_end)
	{
	  if (isident (the_line[rl_point]))
	    while (++rl_point < rl_end && isident (the_line[rl_point]));
	  else
	    while (++rl_point < rl_end && !isident (the_line[rl_point])
		   && !whitespace (the_line[rl_point]));
	  rl_point--;
	}
    }
}

rl_vi_insert_beg ()
{
  rl_beg_of_line ();
  rl_vi_insertion_mode ();
  return 0;
}

rl_vi_append_mode ()
{
  if (rl_point < rl_end)
    rl_point += 1;
  rl_vi_insertion_mode ();
  return 0;
}

rl_vi_append_eol ()
{
  rl_end_of_line ();
  rl_vi_append_mode ();
  return 0;
}

/* What to do in the case of C-d. */
rl_vi_eof_maybe (count, c)
     int count, c;
{
  rl_newline (1, '\n');
}

/* Insertion mode stuff. */

/* Switching from one mode to the other really just involves
   switching keymaps. */
rl_vi_insertion_mode ()
{
  keymap = vi_insertion_keymap;
}

rl_vi_movement_mode ()
{
  if (rl_point > 0)
    rl_backward (1);

  keymap = vi_movement_keymap;
  if (vi_doing_insert)
    {
      rl_end_undo_group ();
      vi_doing_insert = 0;
    }
}

rl_vi_arg_digit (count, c)
     int count, c;
{
  if (c == '0' && rl_numeric_arg == 1 && !rl_explicit_arg)
    rl_beg_of_line ();
  else
    rl_digit_argument (count, c);
}

/* Doesn't take an arg count in vi */
rl_vi_change_case (ignore1, ignore2)
     int ignore1, ignore2;
{
  char c = 0;

  if (uppercase_p (the_line[rl_point]))
    c = to_lower (the_line[rl_point]);
  else if (lowercase_p (the_line[rl_point]))
    c = to_upper (the_line[rl_point]);

  /* Vi is kind of strange here. */
  if (c)
    {
      rl_begin_undo_group ();
      rl_delete (1);
      rl_insert (1, c);
      rl_end_undo_group ();
      rl_vi_check ();
    }
  else
    rl_forward (1);
}

rl_vi_put (count, key)
     int count, key;
{
  if (!uppercase_p (key))
    {
    if(rl_point != rl_end)
      rl_point++;
    }

  rl_yank ();
  rl_backward (1);
}

rl_vi_check ()
{
  if (rl_point && rl_point == rl_end)
    rl_point--;
}

rl_vi_column (count)
{
  if (count > rl_end)
    rl_end_of_line ();
  else
    rl_point = count - 1;
}

int
rl_vi_domove ()
{
  int c, save;

  rl_mark = rl_point;
  c = rl_read_key (in_stream);

  if (!member (c, vi_motion))
    {
      if (digit (c))
	{
	  save = rl_numeric_arg;
	  rl_digit_loop1 ();
	  rl_numeric_arg *= save;
	}
      else
	return (-1);
    }

  rl_dispatch (c, keymap);

  /* No change in position means the command failed. */
  if (rl_mark == rl_point)
    return (-1);

  if ((c == 'w' || c == 'W') && rl_point < rl_end)
    {
    rl_point--;
    while((rl_point > 0) && whitespace (the_line[rl_point]))
	rl_point--;
    rl_point++;
    }

  if (rl_mark < rl_point)
    exchange (rl_point, rl_mark);

  return (0);
}

/* A simplified loop for vi. Don't dispatch key at end.
   Don't recognize minus sign? */
rl_digit_loop1 ()
{
  int key, c;

  while (1)
    {
      rl_message ("(arg: %d) ", arg_sign * rl_numeric_arg);
      key = c = rl_read_key ();

      if (keymap[c].type == ISFUNC &&
	  keymap[c].function == rl_universal_argument)
	{
	  rl_numeric_arg *= 4;
	  continue;
	}
      c = UNMETA (c);
      if (numeric (c))
	{
	  if (rl_explicit_arg)
	    rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
	  else
	    rl_numeric_arg = (c - '0');
	  rl_explicit_arg = 1;
	}
      else
	{
	  rl_clear_message ();
	  rl_stuff_char (key);
	}
    }
}

rl_vi_delete_to (count, key)
     int count, key;
{
  if (uppercase_p (key))
    rl_stuff_char ('$');

  if (rl_vi_domove ())
    {
      ding ();
      return;
    }

  rl_kill_text (rl_point, rl_mark);
}

rl_vi_change_to (count, key)
     int count, key;
{
  if (uppercase_p (key))
    rl_stuff_char ('$');

  if (rl_vi_domove ())
    {
      ding ();
      return;
    }

  rl_begin_undo_group ();
  vi_doing_insert = 1;
  rl_kill_text (rl_point, rl_mark);
  rl_vi_insertion_mode ();
}

rl_vi_yank_to (count, key)
     int count, key;
{
  int save = rl_point;

  if (uppercase_p (key))
    rl_stuff_char ('$');

  if (rl_vi_domove ())
    {
      ding ();
      return;
    }

  rl_begin_undo_group ();
  rl_kill_text (rl_point, rl_mark);
  rl_end_undo_group ();
  rl_do_undo ();
  rl_point = save;
}

rl_vi_delete (count)
{
  if (rl_point >= rl_end - 1)
    {
      rl_delete (count);
      if (rl_point > 0)
	rl_backward (1);
    }
  else
    rl_delete (count);
}

/* Turn the current line into a comment in shell history.  A ksh function */
rl_vi_comment ()
{
  rl_beg_of_line ();
  rl_insert_text (": ");	/* # doesn't work in interactive mode */
  rl_redisplay ();
  rl_newline (1, '\010');
}

rl_vi_first_print ()
{
  rl_back_to_indent ();
}

rl_back_to_indent (ignore1, ignore2)
     int ignore1, ignore2;
{
  rl_beg_of_line ();
  while (rl_point < rl_end && whitespace (the_line[rl_point]))
    rl_point++;
}

/* NOTE: it is necessary that opposite directions are inverses */
#define	FTO	 1		/* forward to */
#define BTO	-1		/* backward to */
#define FFIND	 2		/* forward find */
#define BFIND	-2		/* backward find */

rl_vi_char_search (count, key)
     int count, key;
{
  static char target;
  static int orig_dir, dir;
  int pos;

  if (key == ';' || key == ',')
    dir = (key == ';' ? orig_dir : -orig_dir);
  else
    {
      target = rl_read_key();

      switch (key)
	{
	case 't':
	  orig_dir = dir = FTO;
	  break;

	case 'T':
	  orig_dir = dir = BTO;
	  break;

	case 'f':
	  orig_dir = dir = FFIND;
	  break;

	case 'F':
	  orig_dir = dir = BFIND;
	  break;
	}
    }

  pos = rl_point;

  if (dir < 0)
    {
      pos--;
      do
	{
	  if (the_line[pos] == target)
	    {
	      if (dir == BTO)
		rl_point = pos + 1;
	      else
		rl_point = pos;
	      return;
	    }
	}
      while (pos--);

      if (pos < 0)
	{
	  ding ();
	  return;
	}
    }
  else
    {			/* dir > 0 */
      pos++;
      do
	{
	  if (the_line[pos] == target)
	    {
	      if (dir == FTO)
		rl_point = pos - 1;
	      else
		rl_point = pos;
	      return;
	    }
	}
      while (++pos < rl_end);

      if (pos >= (rl_end - 1))
	ding ();
    }
}

/* Match brackets */
rl_vi_match ()
{
  int count = 1, brack, pos;

  pos = rl_point;
  if ((brack = rl_vi_bracktype (the_line[rl_point])) == 0)
    {
      while ((brack = rl_vi_bracktype (the_line[rl_point])) == 0 &&
	     rl_point < rl_end - 1)
	rl_forward (1);

      if (brack <= 0)
	{
	  rl_point = pos;
	  ding ();
	  return;
	}
    }

  pos = rl_point;

  if (brack < 0)
    {
      while (count)
	{
	  if (--pos >= 0)
	    {
	      int b = rl_vi_bracktype (the_line[pos]);
	      if (b == -brack)
		count--;
	      else if (b == brack)
		count++;
	    }
	  else
	    {
	      ding ();
	      return;
	    }
	}
    }
  else
    {			/* brack > 0 */
      while (count)
	{
	  if (++pos < rl_end)
	    {
	      int b = rl_vi_bracktype (the_line[pos]);
	      if (b == -brack)
		count--;
	      else if (b == brack)
		count++;
	    }
	  else
	    {
	      ding ();
	      return;
	    }
	}
    }
  rl_point = pos;
}

int
rl_vi_bracktype (c)
     int c;
{
  switch (c)
    {
    case '(': return  1;
    case ')': return -1;
    case '[': return  2;
    case ']': return -2;
    case '{': return  3;
    case '}': return -3;
    default:  return  0;
    }
}

rl_vi_change_char ()
{
  int c;

  c = rl_read_key();

  switch (c)
    {
    case '\033':
    case CTRL('C'):
      return;

    default:
      rl_begin_undo_group ();
      rl_delete (1);
      rl_insert (1, c);
      rl_end_undo_group ();
      break;
    }
}

rl_vi_subst (count, key)
     int count, key;
{
  rl_begin_undo_group ();
  vi_doing_insert = 1;

  if (uppercase_p (key))
    {
      rl_beg_of_line ();
      rl_kill_line (1);
    }
  else
    rl_delete (1);

  rl_vi_insertion_mode ();
}

rl_vi_overstrike (count, key)
     int count, key;
{
  int i;

  if (vi_doing_insert == 0)
    {
      vi_doing_insert = 1;
      rl_begin_undo_group ();
    }

  for (i = 0; i < count; i++)
    {
      vi_replace_count++;
      rl_begin_undo_group ();

      if (rl_point < rl_end)
	{
	  rl_delete (1);
	  rl_insert (1, key);
	}
      else
	rl_insert (1, key);

      rl_end_undo_group ();
    }
}

rl_vi_overstrike_delete (count)
     int count;
{
  int i, s;

  for (i = 0; i < count; i++)
    {
      if (vi_replace_count == 0)
	{
	  ding ();
	  break;
	}
      s = rl_point;

      if (rl_do_undo ())
	vi_replace_count--;

      if (rl_point == s)
	rl_backward (1);
    }

  if (vi_replace_count == 0 && vi_doing_insert)
    {
      rl_end_undo_group ();
      rl_do_undo ();
      vi_doing_insert = 0;
    }
}

rl_vi_replace ()
{
  int i;

  vi_replace_count = 0;

  vi_replace_map = rl_make_bare_keymap ();

  for (i = ' '; i < 127; i++)
    vi_replace_map[i].function = rl_vi_overstrike;

  vi_replace_map[RUBOUT].function = rl_vi_overstrike_delete;
  vi_replace_map[CTRL('H')].function = rl_vi_overstrike_delete;
  vi_replace_map[ESC].function = rl_vi_movement_mode;
  vi_replace_map[RETURN].function = rl_newline;
  vi_replace_map[NEWLINE].function = rl_newline;
  keymap = vi_replace_map;
}