/
insexpand.c
5113 lines (4623 loc) · 133 KB
/
insexpand.c
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* insexpand.c: functions for Insert mode completion
*/
#include "vim.h"
/*
* Definitions used for CTRL-X submode.
* Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] and
* ctrl_x_mode_names[] below.
*/
# define CTRL_X_WANT_IDENT 0x100
# define CTRL_X_NORMAL 0 // CTRL-N CTRL-P completion, default
# define CTRL_X_NOT_DEFINED_YET 1
# define CTRL_X_SCROLL 2
# define CTRL_X_WHOLE_LINE 3
# define CTRL_X_FILES 4
# define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
# define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
# define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
# define CTRL_X_FINISHED 8
# define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
# define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
# define CTRL_X_CMDLINE 11
# define CTRL_X_FUNCTION 12
# define CTRL_X_OMNI 13
# define CTRL_X_SPELL 14
# define CTRL_X_LOCAL_MSG 15 // only used in "ctrl_x_msgs"
# define CTRL_X_EVAL 16 // for builtin function complete()
# define CTRL_X_CMDLINE_CTRL_X 17 // CTRL-X typed in CTRL_X_CMDLINE
# define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
// Message for CTRL-X mode, index is ctrl_x_mode.
static char *ctrl_x_msgs[] =
{
N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl.
N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
NULL, // CTRL_X_SCROLL: depends on state
N_(" Whole line completion (^L^N^P)"),
N_(" File name completion (^F^N^P)"),
N_(" Tag completion (^]^N^P)"),
N_(" Path pattern completion (^N^P)"),
N_(" Definition completion (^D^N^P)"),
NULL, // CTRL_X_FINISHED
N_(" Dictionary completion (^K^N^P)"),
N_(" Thesaurus completion (^T^N^P)"),
N_(" Command-line completion (^V^N^P)"),
N_(" User defined completion (^U^N^P)"),
N_(" Omni completion (^O^N^P)"),
N_(" Spelling suggestion (s^N^P)"),
N_(" Keyword Local completion (^N^P)"),
NULL, // CTRL_X_EVAL doesn't use msg.
N_(" Command-line completion (^V^N^P)"),
};
#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
static char *ctrl_x_mode_names[] = {
"keyword",
"ctrl_x",
"scroll",
"whole_line",
"files",
"tags",
"path_patterns",
"path_defines",
"unknown", // CTRL_X_FINISHED
"dictionary",
"thesaurus",
"cmdline",
"function",
"omni",
"spell",
NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs"
"eval",
"cmdline",
};
#endif
/*
* Array indexes used for cp_text[].
*/
#define CPT_ABBR 0 // "abbr"
#define CPT_MENU 1 // "menu"
#define CPT_KIND 2 // "kind"
#define CPT_INFO 3 // "info"
#define CPT_COUNT 4 // Number of entries
/*
* Structure used to store one match for insert completion.
*/
typedef struct compl_S compl_T;
struct compl_S
{
compl_T *cp_next;
compl_T *cp_prev;
char_u *cp_str; // matched text
char_u *(cp_text[CPT_COUNT]); // text for the menu
#ifdef FEAT_EVAL
typval_T cp_user_data;
#endif
char_u *cp_fname; // file containing the match, allocated when
// cp_flags has CP_FREE_FNAME
int cp_flags; // CP_ values
int cp_number; // sequence number
};
// values for cp_flags
# define CP_ORIGINAL_TEXT 1 // the original text when the expansion begun
# define CP_FREE_FNAME 2 // cp_fname is allocated
# define CP_CONT_S_IPOS 4 // use CONT_S_IPOS for compl_cont_status
# define CP_EQUAL 8 // ins_compl_equal() always returns TRUE
# define CP_ICASE 16 // ins_compl_equal() ignores case
# define CP_FAST 32 // use fast_breakcheck instead of ui_breakcheck
static char e_hitend[] = N_("Hit end of paragraph");
/*
* All the current matches are stored in a list.
* "compl_first_match" points to the start of the list.
* "compl_curr_match" points to the currently selected entry.
* "compl_shown_match" is different from compl_curr_match during
* ins_compl_get_exp().
* "compl_old_match" points to previous "compl_curr_match".
*/
static compl_T *compl_first_match = NULL;
static compl_T *compl_curr_match = NULL;
static compl_T *compl_shown_match = NULL;
static compl_T *compl_old_match = NULL;
// After using a cursor key <Enter> selects a match in the popup menu,
// otherwise it inserts a line break.
static int compl_enter_selects = FALSE;
// When "compl_leader" is not NULL only matches that start with this string
// are used.
static char_u *compl_leader = NULL;
static int compl_get_longest = FALSE; // put longest common string
// in compl_leader
static int compl_no_insert = FALSE; // FALSE: select & insert
// TRUE: noinsert
static int compl_no_select = FALSE; // FALSE: select & insert
// TRUE: noselect
// Selected one of the matches. When FALSE the match was edited or using the
// longest common string.
static int compl_used_match;
// didn't finish finding completions.
static int compl_was_interrupted = FALSE;
// Set when character typed while looking for matches and it means we should
// stop looking for matches.
static int compl_interrupted = FALSE;
static int compl_restarting = FALSE; // don't insert match
// When the first completion is done "compl_started" is set. When it's
// FALSE the word to be completed must be located.
static int compl_started = FALSE;
// Which Ctrl-X mode are we in?
static int ctrl_x_mode = CTRL_X_NORMAL;
static int compl_matches = 0; // number of completion matches
static char_u *compl_pattern = NULL;
static int compl_direction = FORWARD;
static int compl_shows_dir = FORWARD;
static int compl_pending = 0; // > 1 for postponed CTRL-N
static pos_T compl_startpos;
// Length in bytes of the text being completed (this is deleted to be replaced
// by the match.)
static int compl_length = 0;
static colnr_T compl_col = 0; // column where the text starts
// that is being completed
static char_u *compl_orig_text = NULL; // text as it was before
// completion started
static int compl_cont_mode = 0;
static expand_T compl_xp;
// List of flags for method of completion.
static int compl_cont_status = 0;
# define CONT_ADDING 1 // "normal" or "adding" expansion
# define CONT_INTRPT (2 + 4) // a ^X interrupted the current expansion
// it's set only iff N_ADDS is set
# define CONT_N_ADDS 4 // next ^X<> will add-new or expand-current
# define CONT_S_IPOS 8 // next ^X<> will set initial_pos?
// if so, word-wise-expansion will set SOL
# define CONT_SOL 16 // pattern includes start of line, just for
// word-wise expansion, not set for ^X^L
# define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local
// expansion, (eg use complete=.)
static int compl_opt_refresh_always = FALSE;
static int compl_opt_suppress_empty = FALSE;
static int ins_compl_add(char_u *str, int len, char_u *fname, char_u **cptext, typval_T *user_data, int cdir, int flags, int adup);
static void ins_compl_longest_match(compl_T *match);
static void ins_compl_del_pum(void);
static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
static char_u *find_line_end(char_u *ptr);
static void ins_compl_free(void);
static int ins_compl_need_restart(void);
static void ins_compl_new_leader(void);
static int get_compl_len(void);
static void ins_compl_restart(void);
static void ins_compl_set_original_text(char_u *str);
static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
# if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
static void ins_compl_add_list(list_T *list);
static void ins_compl_add_dict(dict_T *dict);
# endif
static int ins_compl_key2dir(int c);
static int ins_compl_pum_key(int c);
static int ins_compl_key2count(int c);
static void show_pum(int prev_w_wrow, int prev_w_leftcol);
static unsigned quote_meta(char_u *dest, char_u *str, int len);
#ifdef FEAT_SPELL
static void spell_back_to_badword(void);
static int spell_bad_len = 0; // length of located bad word
#endif
/*
* CTRL-X pressed in Insert mode.
*/
void
ins_ctrl_x(void)
{
if (!ctrl_x_mode_cmdline())
{
// if the next ^X<> won't ADD nothing, then reset compl_cont_status
if (compl_cont_status & CONT_N_ADDS)
compl_cont_status |= CONT_INTRPT;
else
compl_cont_status = 0;
// We're not sure which CTRL-X mode it will be yet
ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
edit_submode_pre = NULL;
showmode();
}
else
// CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X
// CTRL-V look like CTRL-N
ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X;
may_trigger_modechanged();
}
/*
* Functions to check the current CTRL-X mode.
*/
int ctrl_x_mode_none(void) { return ctrl_x_mode == 0; }
int ctrl_x_mode_normal(void) { return ctrl_x_mode == CTRL_X_NORMAL; }
int ctrl_x_mode_scroll(void) { return ctrl_x_mode == CTRL_X_SCROLL; }
int ctrl_x_mode_whole_line(void) { return ctrl_x_mode == CTRL_X_WHOLE_LINE; }
int ctrl_x_mode_files(void) { return ctrl_x_mode == CTRL_X_FILES; }
int ctrl_x_mode_tags(void) { return ctrl_x_mode == CTRL_X_TAGS; }
int ctrl_x_mode_path_patterns(void) {
return ctrl_x_mode == CTRL_X_PATH_PATTERNS; }
int ctrl_x_mode_path_defines(void) {
return ctrl_x_mode == CTRL_X_PATH_DEFINES; }
int ctrl_x_mode_dictionary(void) { return ctrl_x_mode == CTRL_X_DICTIONARY; }
int ctrl_x_mode_thesaurus(void) { return ctrl_x_mode == CTRL_X_THESAURUS; }
int ctrl_x_mode_cmdline(void) {
return ctrl_x_mode == CTRL_X_CMDLINE
|| ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; }
int ctrl_x_mode_function(void) { return ctrl_x_mode == CTRL_X_FUNCTION; }
int ctrl_x_mode_omni(void) { return ctrl_x_mode == CTRL_X_OMNI; }
int ctrl_x_mode_spell(void) { return ctrl_x_mode == CTRL_X_SPELL; }
static int ctrl_x_mode_eval(void) { return ctrl_x_mode == CTRL_X_EVAL; }
int ctrl_x_mode_line_or_eval(void) {
return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; }
/*
* Whether other than default completion has been selected.
*/
int
ctrl_x_mode_not_default(void)
{
return ctrl_x_mode != CTRL_X_NORMAL;
}
/*
* Whether CTRL-X was typed without a following character,
* not including when in CTRL-X CTRL-V mode.
*/
int
ctrl_x_mode_not_defined_yet(void)
{
return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET;
}
/*
* Return TRUE if currently in "normal" or "adding" insert completion matches
* state
*/
int
compl_status_adding(void)
{
return compl_cont_status & CONT_ADDING;
}
/*
* Return TRUE if the completion pattern includes start of line, just for
* word-wise expansion.
*/
int
compl_status_sol(void)
{
return compl_cont_status & CONT_SOL;
}
/*
* Return TRUE if ^X^P/^X^N will do a local completion (i.e. use complete=.)
*/
int
compl_status_local(void)
{
return compl_cont_status & CONT_LOCAL;
}
/*
* Clear the completion status flags
*/
void
compl_status_clear(void)
{
compl_cont_status = 0;
}
/*
* Return TRUE if completion is using the forward direction matches
*/
static int
compl_dir_forward(void)
{
return compl_direction == FORWARD;
}
/*
* Return TRUE if currently showing forward completion matches
*/
static int
compl_shows_dir_forward(void)
{
return compl_shows_dir == FORWARD;
}
/*
* Return TRUE if currently showing backward completion matches
*/
static int
compl_shows_dir_backward(void)
{
return compl_shows_dir == BACKWARD;
}
/*
* Return TRUE if the 'dictionary' or 'thesaurus' option can be used.
*/
int
has_compl_option(int dict_opt)
{
if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
#ifdef FEAT_SPELL
&& !curwin->w_p_spell
#endif
)
: (*curbuf->b_p_tsr == NUL && *p_tsr == NUL
#ifdef FEAT_COMPL_FUNC
&& *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL
#endif
))
{
ctrl_x_mode = CTRL_X_NORMAL;
edit_submode = NULL;
msg_attr(dict_opt ? _("'dictionary' option is empty")
: _("'thesaurus' option is empty"),
HL_ATTR(HLF_E));
if (emsg_silent == 0 && !in_assert_fails)
{
vim_beep(BO_COMPL);
setcursor();
out_flush();
#ifdef FEAT_EVAL
if (!get_vim_var_nr(VV_TESTING))
#endif
ui_delay(2004L, FALSE);
}
return FALSE;
}
return TRUE;
}
/*
* Is the character "c" a valid key to go to or keep us in CTRL-X mode?
* This depends on the current mode.
*/
int
vim_is_ctrl_x_key(int c)
{
// Always allow ^R - let its results then be checked
if (c == Ctrl_R)
return TRUE;
// Accept <PageUp> and <PageDown> if the popup menu is visible.
if (ins_compl_pum_key(c))
return TRUE;
switch (ctrl_x_mode)
{
case 0: // Not in any CTRL-X mode
return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
case CTRL_X_NOT_DEFINED_YET:
case CTRL_X_CMDLINE_CTRL_X:
return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
|| c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
|| c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
|| c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
|| c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
|| c == Ctrl_S || c == Ctrl_K || c == 's'
|| c == Ctrl_Z);
case CTRL_X_SCROLL:
return (c == Ctrl_Y || c == Ctrl_E);
case CTRL_X_WHOLE_LINE:
return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
case CTRL_X_FILES:
return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
case CTRL_X_DICTIONARY:
return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
case CTRL_X_THESAURUS:
return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
case CTRL_X_TAGS:
return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
#ifdef FEAT_FIND_ID
case CTRL_X_PATH_PATTERNS:
return (c == Ctrl_P || c == Ctrl_N);
case CTRL_X_PATH_DEFINES:
return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
#endif
case CTRL_X_CMDLINE:
return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
|| c == Ctrl_X);
#ifdef FEAT_COMPL_FUNC
case CTRL_X_FUNCTION:
return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
case CTRL_X_OMNI:
return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
#endif
case CTRL_X_SPELL:
return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
case CTRL_X_EVAL:
return (c == Ctrl_P || c == Ctrl_N);
}
internal_error("vim_is_ctrl_x_key()");
return FALSE;
}
/*
* Return TRUE if "match" is the original text when the completion began.
*/
static int
match_at_original_text(compl_T *match)
{
return match->cp_flags & CP_ORIGINAL_TEXT;
}
/*
* Returns TRUE if "match" is the first match in the completion list.
*/
static int
is_first_match(compl_T *match)
{
return match == compl_first_match;
}
/*
* Return TRUE when character "c" is part of the item currently being
* completed. Used to decide whether to abandon complete mode when the menu
* is visible.
*/
int
ins_compl_accept_char(int c)
{
if (ctrl_x_mode & CTRL_X_WANT_IDENT)
// When expanding an identifier only accept identifier chars.
return vim_isIDc(c);
switch (ctrl_x_mode)
{
case CTRL_X_FILES:
// When expanding file name only accept file name chars. But not
// path separators, so that "proto/<Tab>" expands files in
// "proto", not "proto/" as a whole
return vim_isfilec(c) && !vim_ispathsep(c);
case CTRL_X_CMDLINE:
case CTRL_X_CMDLINE_CTRL_X:
case CTRL_X_OMNI:
// Command line and Omni completion can work with just about any
// printable character, but do stop at white space.
return vim_isprintc(c) && !VIM_ISWHITE(c);
case CTRL_X_WHOLE_LINE:
// For while line completion a space can be part of the line.
return vim_isprintc(c);
}
return vim_iswordc(c);
}
/*
* Get the completed text by inferring the case of the originally typed text.
*/
static char_u *
ins_compl_infercase_gettext(
char_u *str,
int actual_len,
int actual_compl_length,
int min_len)
{
int *wca; // Wide character array.
char_u *p;
int i, c;
int has_lower = FALSE;
int was_letter = FALSE;
IObuff[0] = NUL;
// Allocate wide character array for the completion and fill it.
wca = ALLOC_MULT(int, actual_len);
if (wca == NULL)
return IObuff;
p = str;
for (i = 0; i < actual_len; ++i)
if (has_mbyte)
wca[i] = mb_ptr2char_adv(&p);
else
wca[i] = *(p++);
// Rule 1: Were any chars converted to lower?
p = compl_orig_text;
for (i = 0; i < min_len; ++i)
{
if (has_mbyte)
c = mb_ptr2char_adv(&p);
else
c = *(p++);
if (MB_ISLOWER(c))
{
has_lower = TRUE;
if (MB_ISUPPER(wca[i]))
{
// Rule 1 is satisfied.
for (i = actual_compl_length; i < actual_len; ++i)
wca[i] = MB_TOLOWER(wca[i]);
break;
}
}
}
// Rule 2: No lower case, 2nd consecutive letter converted to
// upper case.
if (!has_lower)
{
p = compl_orig_text;
for (i = 0; i < min_len; ++i)
{
if (has_mbyte)
c = mb_ptr2char_adv(&p);
else
c = *(p++);
if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
{
// Rule 2 is satisfied.
for (i = actual_compl_length; i < actual_len; ++i)
wca[i] = MB_TOUPPER(wca[i]);
break;
}
was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
}
}
// Copy the original case of the part we typed.
p = compl_orig_text;
for (i = 0; i < min_len; ++i)
{
if (has_mbyte)
c = mb_ptr2char_adv(&p);
else
c = *(p++);
if (MB_ISLOWER(c))
wca[i] = MB_TOLOWER(wca[i]);
else if (MB_ISUPPER(c))
wca[i] = MB_TOUPPER(wca[i]);
}
// Generate encoding specific output from wide character array.
// Multi-byte characters can occupy up to five bytes more than
// ASCII characters, and we also need one byte for NUL, so stay
// six bytes away from the edge of IObuff.
p = IObuff;
i = 0;
while (i < actual_len && (p - IObuff + 6) < IOSIZE)
if (has_mbyte)
p += (*mb_char2bytes)(wca[i++], p);
else
*(p++) = wca[i++];
*p = NUL;
vim_free(wca);
return IObuff;
}
/*
* This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
* case of the originally typed text is used, and the case of the completed
* text is inferred, ie this tries to work out what case you probably wanted
* the rest of the word to be in -- webb
*/
int
ins_compl_add_infercase(
char_u *str_arg,
int len,
int icase,
char_u *fname,
int dir,
int cont_s_ipos) // next ^X<> will set initial_pos
{
char_u *str = str_arg;
char_u *p;
int actual_len; // Take multi-byte characters
int actual_compl_length; // into account.
int min_len;
int flags = 0;
if (p_ic && curbuf->b_p_inf && len > 0)
{
// Infer case of completed part.
// Find actual length of completion.
if (has_mbyte)
{
p = str;
actual_len = 0;
while (*p != NUL)
{
MB_PTR_ADV(p);
++actual_len;
}
}
else
actual_len = len;
// Find actual length of original text.
if (has_mbyte)
{
p = compl_orig_text;
actual_compl_length = 0;
while (*p != NUL)
{
MB_PTR_ADV(p);
++actual_compl_length;
}
}
else
actual_compl_length = compl_length;
// "actual_len" may be smaller than "actual_compl_length" when using
// thesaurus, only use the minimum when comparing.
min_len = actual_len < actual_compl_length
? actual_len : actual_compl_length;
str = ins_compl_infercase_gettext(str, actual_len, actual_compl_length,
min_len);
}
if (cont_s_ipos)
flags |= CP_CONT_S_IPOS;
if (icase)
flags |= CP_ICASE;
return ins_compl_add(str, len, fname, NULL, NULL, dir, flags, FALSE);
}
/*
* Add a match to the list of matches. The arguments are:
* str - text of the match to add
* len - length of "str". If -1, then the length of "str" is
* computed.
* fname - file name to associate with this match.
* cptext - list of strings to use with this match (for abbr, menu, info
* and kind)
* user_data - user supplied data (any vim type) for this match
* cdir - match direction. If 0, use "compl_direction".
* flags_arg - match flags (cp_flags)
* adup - accept this match even if it is already present.
* If "cdir" is FORWARD, then the match is added after the current match.
* Otherwise, it is added before the current match.
*
* If the given string is already in the list of completions, then return
* NOTDONE, otherwise add it to the list and return OK. If there is an error,
* maybe because alloc() returns NULL, then FAIL is returned.
*/
static int
ins_compl_add(
char_u *str,
int len,
char_u *fname,
char_u **cptext, // extra text for popup menu or NULL
typval_T *user_data UNUSED, // "user_data" entry or NULL
int cdir,
int flags_arg,
int adup) // accept duplicate match
{
compl_T *match;
int dir = (cdir == 0 ? compl_direction : cdir);
int flags = flags_arg;
if (flags & CP_FAST)
fast_breakcheck();
else
ui_breakcheck();
if (got_int)
return FAIL;
if (len < 0)
len = (int)STRLEN(str);
// If the same match is already present, don't add it.
if (compl_first_match != NULL && !adup)
{
match = compl_first_match;
do
{
if (!match_at_original_text(match)
&& STRNCMP(match->cp_str, str, len) == 0
&& match->cp_str[len] == NUL)
return NOTDONE;
match = match->cp_next;
} while (match != NULL && !is_first_match(match));
}
// Remove any popup menu before changing the list of matches.
ins_compl_del_pum();
// Allocate a new match structure.
// Copy the values to the new match structure.
match = ALLOC_CLEAR_ONE(compl_T);
if (match == NULL)
return FAIL;
match->cp_number = -1;
if (flags & CP_ORIGINAL_TEXT)
match->cp_number = 0;
if ((match->cp_str = vim_strnsave(str, len)) == NULL)
{
vim_free(match);
return FAIL;
}
// match-fname is:
// - compl_curr_match->cp_fname if it is a string equal to fname.
// - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
// - NULL otherwise. --Acevedo
if (fname != NULL
&& compl_curr_match != NULL
&& compl_curr_match->cp_fname != NULL
&& STRCMP(fname, compl_curr_match->cp_fname) == 0)
match->cp_fname = compl_curr_match->cp_fname;
else if (fname != NULL)
{
match->cp_fname = vim_strsave(fname);
flags |= CP_FREE_FNAME;
}
else
match->cp_fname = NULL;
match->cp_flags = flags;
if (cptext != NULL)
{
int i;
for (i = 0; i < CPT_COUNT; ++i)
if (cptext[i] != NULL && *cptext[i] != NUL)
match->cp_text[i] = vim_strsave(cptext[i]);
}
#ifdef FEAT_EVAL
if (user_data != NULL)
match->cp_user_data = *user_data;
#endif
// Link the new match structure after (FORWARD) or before (BACKWARD) the
// current match in the list of matches .
if (compl_first_match == NULL)
match->cp_next = match->cp_prev = NULL;
else if (dir == FORWARD)
{
match->cp_next = compl_curr_match->cp_next;
match->cp_prev = compl_curr_match;
}
else // BACKWARD
{
match->cp_next = compl_curr_match;
match->cp_prev = compl_curr_match->cp_prev;
}
if (match->cp_next)
match->cp_next->cp_prev = match;
if (match->cp_prev)
match->cp_prev->cp_next = match;
else // if there's nothing before, it is the first match
compl_first_match = match;
compl_curr_match = match;
// Find the longest common string if still doing that.
if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0)
ins_compl_longest_match(match);
return OK;
}
/*
* Return TRUE if "str[len]" matches with match->cp_str, considering
* match->cp_flags.
*/
static int
ins_compl_equal(compl_T *match, char_u *str, int len)
{
if (match->cp_flags & CP_EQUAL)
return TRUE;
if (match->cp_flags & CP_ICASE)
return STRNICMP(match->cp_str, str, (size_t)len) == 0;
return STRNCMP(match->cp_str, str, (size_t)len) == 0;
}
/*
* Reduce the longest common string for match "match".
*/
static void
ins_compl_longest_match(compl_T *match)
{
char_u *p, *s;
int c1, c2;
int had_match;
if (compl_leader == NULL)
{
// First match, use it as a whole.
compl_leader = vim_strsave(match->cp_str);
if (compl_leader == NULL)
return;
had_match = (curwin->w_cursor.col > compl_col);
ins_compl_delete();
ins_bytes(compl_leader + get_compl_len());
ins_redraw(FALSE);
// When the match isn't there (to avoid matching itself) remove it
// again after redrawing.
if (!had_match)
ins_compl_delete();
compl_used_match = FALSE;
return;
}
// Reduce the text if this match differs from compl_leader.
p = compl_leader;
s = match->cp_str;
while (*p != NUL)
{
if (has_mbyte)
{
c1 = mb_ptr2char(p);
c2 = mb_ptr2char(s);
}
else
{
c1 = *p;
c2 = *s;
}
if ((match->cp_flags & CP_ICASE)
? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) : (c1 != c2))
break;
if (has_mbyte)
{
MB_PTR_ADV(p);
MB_PTR_ADV(s);
}
else
{
++p;
++s;
}
}
if (*p != NUL)
{
// Leader was shortened, need to change the inserted text.
*p = NUL;
had_match = (curwin->w_cursor.col > compl_col);
ins_compl_delete();
ins_bytes(compl_leader + get_compl_len());
ins_redraw(FALSE);
// When the match isn't there (to avoid matching itself) remove it
// again after redrawing.
if (!had_match)
ins_compl_delete();
}
compl_used_match = FALSE;
}
/*
* Add an array of matches to the list of matches.
* Frees matches[].
*/
static void
ins_compl_add_matches(
int num_matches,
char_u **matches,
int icase)
{
int i;
int add_r = OK;
int dir = compl_direction;
for (i = 0; i < num_matches && add_r != FAIL; i++)
if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, NULL, dir,
CP_FAST | (icase ? CP_ICASE : 0), FALSE)) == OK)
// if dir was BACKWARD then honor it just once
dir = FORWARD;
FreeWild(num_matches, matches);
}
/*
* Make the completion list cyclic.
* Return the number of matches (excluding the original).
*/
static int
ins_compl_make_cyclic(void)
{
compl_T *match;
int count = 0;
if (compl_first_match == NULL)
return 0;
// Find the end of the list.
match = compl_first_match;
// there's always an entry for the compl_orig_text, it doesn't count.
while (match->cp_next != NULL && !is_first_match(match->cp_next))
{
match = match->cp_next;
++count;
}
match->cp_next = compl_first_match;
compl_first_match->cp_prev = match;
return count;
}
/*
* Return whether there currently is a shown match.
*/
int
ins_compl_has_shown_match(void)
{
return compl_shown_match == NULL
|| compl_shown_match != compl_shown_match->cp_next;
}
/*
* Return whether the shown match is long enough.
*/
int
ins_compl_long_shown_match(void)
{
return (int)STRLEN(compl_shown_match->cp_str)
> curwin->w_cursor.col - compl_col;
}
/*
* Set variables that store noselect and noinsert behavior from the
* 'completeopt' value.
*/
void