/
misc1.c
4493 lines (4071 loc) · 103 KB
/
misc1.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.
*/
/*
* misc1.c: functions that didn't seem to fit elsewhere
*/
#include "vim.h"
#include "version.h"
#if defined(FEAT_CMDL_COMPL) && defined(MSWIN)
# include <lm.h>
#endif
static char_u *vim_version_dir(char_u *vimdir);
static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
#define URL_SLASH 1 /* path_is_url() has found "://" */
#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
/* All user names (for ~user completion as done by shell). */
#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
static garray_T ga_users;
#endif
/*
* Count the size (in window cells) of the indent in the current line.
*/
int
get_indent(void)
{
#ifdef FEAT_VARTABS
return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
curbuf->b_p_vts_array, FALSE);
#else
return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
#endif
}
/*
* Count the size (in window cells) of the indent in line "lnum".
*/
int
get_indent_lnum(linenr_T lnum)
{
#ifdef FEAT_VARTABS
return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
curbuf->b_p_vts_array, FALSE);
#else
return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
#endif
}
#if defined(FEAT_FOLDING) || defined(PROTO)
/*
* Count the size (in window cells) of the indent in line "lnum" of buffer
* "buf".
*/
int
get_indent_buf(buf_T *buf, linenr_T lnum)
{
#ifdef FEAT_VARTABS
return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
(int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
#else
return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
#endif
}
#endif
/*
* count the size (in window cells) of the indent in line "ptr", with
* 'tabstop' at "ts"
*/
int
get_indent_str(
char_u *ptr,
int ts,
int list) /* if TRUE, count only screen size for tabs */
{
int count = 0;
for ( ; *ptr; ++ptr)
{
if (*ptr == TAB)
{
if (!list || lcs_tab1) /* count a tab for what it is worth */
count += ts - (count % ts);
else
/* In list mode, when tab is not set, count screen char width
* for Tab, displays: ^I */
count += ptr2cells(ptr);
}
else if (*ptr == ' ')
++count; /* count a space for one */
else
break;
}
return count;
}
#ifdef FEAT_VARTABS
/*
* Count the size (in window cells) of the indent in line "ptr", using
* variable tabstops.
* if "list" is TRUE, count only screen size for tabs.
*/
int
get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
{
int count = 0;
for ( ; *ptr; ++ptr)
{
if (*ptr == TAB) /* count a tab for what it is worth */
{
if (!list || lcs_tab1)
count += tabstop_padding(count, ts, vts);
else
/* In list mode, when tab is not set, count screen char width
* for Tab, displays: ^I */
count += ptr2cells(ptr);
}
else if (*ptr == ' ')
++count; /* count a space for one */
else
break;
}
return count;
}
#endif
/*
* Set the indent of the current line.
* Leaves the cursor on the first non-blank in the line.
* Caller must take care of undo.
* "flags":
* SIN_CHANGED: call changed_bytes() if the line was changed.
* SIN_INSERT: insert the indent in front of the line.
* SIN_UNDO: save line for undo before changing it.
* Returns TRUE if the line was changed.
*/
int
set_indent(
int size, /* measured in spaces */
int flags)
{
char_u *p;
char_u *newline;
char_u *oldline;
char_u *s;
int todo;
int ind_len; /* measured in characters */
int line_len;
int doit = FALSE;
int ind_done = 0; /* measured in spaces */
#ifdef FEAT_VARTABS
int ind_col = 0;
#endif
int tab_pad;
int retval = FALSE;
int orig_char_len = -1; /* number of initial whitespace chars when
'et' and 'pi' are both set */
/*
* First check if there is anything to do and compute the number of
* characters needed for the indent.
*/
todo = size;
ind_len = 0;
p = oldline = ml_get_curline();
/* Calculate the buffer size for the new indent, and check to see if it
* isn't already set */
/* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
* 'preserveindent' are set count the number of characters at the
* beginning of the line to be copied */
if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
{
/* If 'preserveindent' is set then reuse as much as possible of
* the existing indent structure for the new indent */
if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
{
ind_done = 0;
/* count as many characters as we can use */
while (todo > 0 && VIM_ISWHITE(*p))
{
if (*p == TAB)
{
#ifdef FEAT_VARTABS
tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
curbuf->b_p_vts_array);
#else
tab_pad = (int)curbuf->b_p_ts
- (ind_done % (int)curbuf->b_p_ts);
#endif
/* stop if this tab will overshoot the target */
if (todo < tab_pad)
break;
todo -= tab_pad;
++ind_len;
ind_done += tab_pad;
}
else
{
--todo;
++ind_len;
++ind_done;
}
++p;
}
#ifdef FEAT_VARTABS
/* These diverge from this point. */
ind_col = ind_done;
#endif
/* Set initial number of whitespace chars to copy if we are
* preserving indent but expandtab is set */
if (curbuf->b_p_et)
orig_char_len = ind_len;
/* Fill to next tabstop with a tab, if possible */
#ifdef FEAT_VARTABS
tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
curbuf->b_p_vts_array);
#else
tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
#endif
if (todo >= tab_pad && orig_char_len == -1)
{
doit = TRUE;
todo -= tab_pad;
++ind_len;
/* ind_done += tab_pad; */
#ifdef FEAT_VARTABS
ind_col += tab_pad;
#endif
}
}
/* count tabs required for indent */
#ifdef FEAT_VARTABS
for (;;)
{
tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
curbuf->b_p_vts_array);
if (todo < tab_pad)
break;
if (*p != TAB)
doit = TRUE;
else
++p;
todo -= tab_pad;
++ind_len;
ind_col += tab_pad;
}
#else
while (todo >= (int)curbuf->b_p_ts)
{
if (*p != TAB)
doit = TRUE;
else
++p;
todo -= (int)curbuf->b_p_ts;
++ind_len;
/* ind_done += (int)curbuf->b_p_ts; */
}
#endif
}
/* count spaces required for indent */
while (todo > 0)
{
if (*p != ' ')
doit = TRUE;
else
++p;
--todo;
++ind_len;
/* ++ind_done; */
}
/* Return if the indent is OK already. */
if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
return FALSE;
/* Allocate memory for the new line. */
if (flags & SIN_INSERT)
p = oldline;
else
p = skipwhite(p);
line_len = (int)STRLEN(p) + 1;
/* If 'preserveindent' and 'expandtab' are both set keep the original
* characters and allocate accordingly. We will fill the rest with spaces
* after the if (!curbuf->b_p_et) below. */
if (orig_char_len != -1)
{
newline = alloc(orig_char_len + size - ind_done + line_len);
if (newline == NULL)
return FALSE;
todo = size - ind_done;
ind_len = orig_char_len + todo; /* Set total length of indent in
* characters, which may have been
* undercounted until now */
p = oldline;
s = newline;
while (orig_char_len > 0)
{
*s++ = *p++;
orig_char_len--;
}
/* Skip over any additional white space (useful when newindent is less
* than old) */
while (VIM_ISWHITE(*p))
++p;
}
else
{
todo = size;
newline = alloc(ind_len + line_len);
if (newline == NULL)
return FALSE;
s = newline;
}
/* Put the characters in the new line. */
/* if 'expandtab' isn't set: use TABs */
if (!curbuf->b_p_et)
{
/* If 'preserveindent' is set then reuse as much as possible of
* the existing indent structure for the new indent */
if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
{
p = oldline;
ind_done = 0;
while (todo > 0 && VIM_ISWHITE(*p))
{
if (*p == TAB)
{
#ifdef FEAT_VARTABS
tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
curbuf->b_p_vts_array);
#else
tab_pad = (int)curbuf->b_p_ts
- (ind_done % (int)curbuf->b_p_ts);
#endif
/* stop if this tab will overshoot the target */
if (todo < tab_pad)
break;
todo -= tab_pad;
ind_done += tab_pad;
}
else
{
--todo;
++ind_done;
}
*s++ = *p++;
}
/* Fill to next tabstop with a tab, if possible */
#ifdef FEAT_VARTABS
tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
curbuf->b_p_vts_array);
#else
tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
#endif
if (todo >= tab_pad)
{
*s++ = TAB;
todo -= tab_pad;
#ifdef FEAT_VARTABS
ind_done += tab_pad;
#endif
}
p = skipwhite(p);
}
#ifdef FEAT_VARTABS
for (;;)
{
tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
curbuf->b_p_vts_array);
if (todo < tab_pad)
break;
*s++ = TAB;
todo -= tab_pad;
ind_done += tab_pad;
}
#else
while (todo >= (int)curbuf->b_p_ts)
{
*s++ = TAB;
todo -= (int)curbuf->b_p_ts;
}
#endif
}
while (todo > 0)
{
*s++ = ' ';
--todo;
}
mch_memmove(s, p, (size_t)line_len);
// Replace the line (unless undo fails).
if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
{
ml_replace(curwin->w_cursor.lnum, newline, FALSE);
if (flags & SIN_CHANGED)
changed_bytes(curwin->w_cursor.lnum, 0);
// Correct saved cursor position if it is in this line.
if (saved_cursor.lnum == curwin->w_cursor.lnum)
{
if (saved_cursor.col >= (colnr_T)(p - oldline))
// cursor was after the indent, adjust for the number of
// bytes added/removed
saved_cursor.col += ind_len - (colnr_T)(p - oldline);
else if (saved_cursor.col >= (colnr_T)(s - newline))
// cursor was in the indent, and is now after it, put it back
// at the start of the indent (replacing spaces with TAB)
saved_cursor.col = (colnr_T)(s - newline);
}
#ifdef FEAT_TEXT_PROP
{
int added = ind_len - (colnr_T)(p - oldline);
// When increasing indent this behaves like spaces were inserted at
// the old indent, when decreasing indent it behaves like spaces
// were deleted at the new indent.
adjust_prop_columns(curwin->w_cursor.lnum,
(colnr_T)(added > 0 ? (p - oldline) : ind_len), added, 0);
}
#endif
retval = TRUE;
}
else
vim_free(newline);
curwin->w_cursor.col = ind_len;
return retval;
}
/*
* Return the indent of the current line after a number. Return -1 if no
* number was found. Used for 'n' in 'formatoptions': numbered list.
* Since a pattern is used it can actually handle more than numbers.
*/
int
get_number_indent(linenr_T lnum)
{
colnr_T col;
pos_T pos;
regmatch_T regmatch;
int lead_len = 0; /* length of comment leader */
if (lnum > curbuf->b_ml.ml_line_count)
return -1;
pos.lnum = 0;
#ifdef FEAT_COMMENTS
/* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
if ((State & INSERT) || has_format_option(FO_Q_COMS))
lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
#endif
regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
if (regmatch.regprog != NULL)
{
regmatch.rm_ic = FALSE;
/* vim_regexec() expects a pointer to a line. This lets us
* start matching for the flp beyond any comment leader... */
if (vim_regexec(®match, ml_get(lnum) + lead_len, (colnr_T)0))
{
pos.lnum = lnum;
pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
pos.coladd = 0;
}
vim_regfree(regmatch.regprog);
}
if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
return -1;
getvcol(curwin, &pos, &col, NULL, NULL);
return (int)col;
}
#if defined(FEAT_LINEBREAK) || defined(PROTO)
/*
* Return appropriate space number for breakindent, taking influencing
* parameters into account. Window must be specified, since it is not
* necessarily always the current one.
*/
int
get_breakindent_win(
win_T *wp,
char_u *line) /* start of the line */
{
static int prev_indent = 0; /* cached indent value */
static long prev_ts = 0L; /* cached tabstop value */
static char_u *prev_line = NULL; /* cached pointer to line */
static varnumber_T prev_tick = 0; /* changedtick of cached value */
#ifdef FEAT_VARTABS
static int *prev_vts = NULL; /* cached vartabs values */
#endif
int bri = 0;
/* window width minus window margin space, i.e. what rests for text */
const int eff_wwidth = wp->w_width
- ((wp->w_p_nu || wp->w_p_rnu)
&& (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
? number_width(wp) + 1 : 0);
/* used cached indent, unless pointer or 'tabstop' changed */
if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
|| prev_tick != CHANGEDTICK(wp->w_buffer)
#ifdef FEAT_VARTABS
|| prev_vts != wp->w_buffer->b_p_vts_array
#endif
)
{
prev_line = line;
prev_ts = wp->w_buffer->b_p_ts;
prev_tick = CHANGEDTICK(wp->w_buffer);
#ifdef FEAT_VARTABS
prev_vts = wp->w_buffer->b_p_vts_array;
prev_indent = get_indent_str_vtab(line,
(int)wp->w_buffer->b_p_ts,
wp->w_buffer->b_p_vts_array, wp->w_p_list);
#else
prev_indent = get_indent_str(line,
(int)wp->w_buffer->b_p_ts, wp->w_p_list);
#endif
}
bri = prev_indent + wp->w_p_brishift;
/* indent minus the length of the showbreak string */
if (wp->w_p_brisbr)
bri -= vim_strsize(p_sbr);
/* Add offset for number column, if 'n' is in 'cpoptions' */
bri += win_col_off2(wp);
/* never indent past left window margin */
if (bri < 0)
bri = 0;
/* always leave at least bri_min characters on the left,
* if text width is sufficient */
else if (bri > eff_wwidth - wp->w_p_brimin)
bri = (eff_wwidth - wp->w_p_brimin < 0)
? 0 : eff_wwidth - wp->w_p_brimin;
return bri;
}
#endif
#if defined(FEAT_COMMENTS) || defined(PROTO)
/*
* get_leader_len() returns the length in bytes of the prefix of the given
* string which introduces a comment. If this string is not a comment then
* 0 is returned.
* When "flags" is not NULL, it is set to point to the flags of the recognized
* comment leader.
* "backward" must be true for the "O" command.
* If "include_space" is set, include trailing whitespace while calculating the
* length.
*/
int
get_leader_len(
char_u *line,
char_u **flags,
int backward,
int include_space)
{
int i, j;
int result;
int got_com = FALSE;
int found_one;
char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
char_u *string; /* pointer to comment string */
char_u *list;
int middle_match_len = 0;
char_u *prev_list;
char_u *saved_flags = NULL;
result = i = 0;
while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
++i;
/*
* Repeat to match several nested comment strings.
*/
while (line[i] != NUL)
{
/*
* scan through the 'comments' option for a match
*/
found_one = FALSE;
for (list = curbuf->b_p_com; *list; )
{
/* Get one option part into part_buf[]. Advance "list" to next
* one. Put "string" at start of string. */
if (!got_com && flags != NULL)
*flags = list; /* remember where flags started */
prev_list = list;
(void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
string = vim_strchr(part_buf, ':');
if (string == NULL) /* missing ':', ignore this part */
continue;
*string++ = NUL; /* isolate flags from string */
/* If we found a middle match previously, use that match when this
* is not a middle or end. */
if (middle_match_len != 0
&& vim_strchr(part_buf, COM_MIDDLE) == NULL
&& vim_strchr(part_buf, COM_END) == NULL)
break;
/* When we already found a nested comment, only accept further
* nested comments. */
if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
continue;
/* When 'O' flag present and using "O" command skip this one. */
if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
continue;
/* Line contents and string must match.
* When string starts with white space, must have some white space
* (but the amount does not need to match, there might be a mix of
* TABs and spaces). */
if (VIM_ISWHITE(string[0]))
{
if (i == 0 || !VIM_ISWHITE(line[i - 1]))
continue; /* missing white space */
while (VIM_ISWHITE(string[0]))
++string;
}
for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
;
if (string[j] != NUL)
continue; /* string doesn't match */
/* When 'b' flag used, there must be white space or an
* end-of-line after the string in the line. */
if (vim_strchr(part_buf, COM_BLANK) != NULL
&& !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
continue;
/* We have found a match, stop searching unless this is a middle
* comment. The middle comment can be a substring of the end
* comment in which case it's better to return the length of the
* end comment and its flags. Thus we keep searching with middle
* and end matches and use an end match if it matches better. */
if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
{
if (middle_match_len == 0)
{
middle_match_len = j;
saved_flags = prev_list;
}
continue;
}
if (middle_match_len != 0 && j > middle_match_len)
/* Use this match instead of the middle match, since it's a
* longer thus better match. */
middle_match_len = 0;
if (middle_match_len == 0)
i += j;
found_one = TRUE;
break;
}
if (middle_match_len != 0)
{
/* Use the previously found middle match after failing to find a
* match with an end. */
if (!got_com && flags != NULL)
*flags = saved_flags;
i += middle_match_len;
found_one = TRUE;
}
/* No match found, stop scanning. */
if (!found_one)
break;
result = i;
/* Include any trailing white space. */
while (VIM_ISWHITE(line[i]))
++i;
if (include_space)
result = i;
/* If this comment doesn't nest, stop here. */
got_com = TRUE;
if (vim_strchr(part_buf, COM_NEST) == NULL)
break;
}
return result;
}
/*
* Return the offset at which the last comment in line starts. If there is no
* comment in the whole line, -1 is returned.
*
* When "flags" is not null, it is set to point to the flags describing the
* recognized comment leader.
*/
int
get_last_leader_offset(char_u *line, char_u **flags)
{
int result = -1;
int i, j;
int lower_check_bound = 0;
char_u *string;
char_u *com_leader;
char_u *com_flags;
char_u *list;
int found_one;
char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
/*
* Repeat to match several nested comment strings.
*/
i = (int)STRLEN(line);
while (--i >= lower_check_bound)
{
/*
* scan through the 'comments' option for a match
*/
found_one = FALSE;
for (list = curbuf->b_p_com; *list; )
{
char_u *flags_save = list;
/*
* Get one option part into part_buf[]. Advance list to next one.
* put string at start of string.
*/
(void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
string = vim_strchr(part_buf, ':');
if (string == NULL) /* If everything is fine, this cannot actually
* happen. */
continue;
*string++ = NUL; /* Isolate flags from string. */
com_leader = string;
/*
* Line contents and string must match.
* When string starts with white space, must have some white space
* (but the amount does not need to match, there might be a mix of
* TABs and spaces).
*/
if (VIM_ISWHITE(string[0]))
{
if (i == 0 || !VIM_ISWHITE(line[i - 1]))
continue;
while (VIM_ISWHITE(*string))
++string;
}
for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
/* do nothing */;
if (string[j] != NUL)
continue;
/*
* When 'b' flag used, there must be white space or an
* end-of-line after the string in the line.
*/
if (vim_strchr(part_buf, COM_BLANK) != NULL
&& !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
continue;
if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
{
// For a middlepart comment, only consider it to match if
// everything before the current position in the line is
// whitespace. Otherwise we would think we are inside a
// comment if the middle part appears somewhere in the middle
// of the line. E.g. for C the "*" appears often.
for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
;
if (j < i)
continue;
}
/*
* We have found a match, stop searching.
*/
found_one = TRUE;
if (flags)
*flags = flags_save;
com_flags = flags_save;
break;
}
if (found_one)
{
char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
int len1, len2, off;
result = i;
/*
* If this comment nests, continue searching.
*/
if (vim_strchr(part_buf, COM_NEST) != NULL)
continue;
lower_check_bound = i;
/* Let's verify whether the comment leader found is a substring
* of other comment leaders. If it is, let's adjust the
* lower_check_bound so that we make sure that we have determined
* the comment leader correctly.
*/
while (VIM_ISWHITE(*com_leader))
++com_leader;
len1 = (int)STRLEN(com_leader);
for (list = curbuf->b_p_com; *list; )
{
char_u *flags_save = list;
(void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
if (flags_save == com_flags)
continue;
string = vim_strchr(part_buf2, ':');
++string;
while (VIM_ISWHITE(*string))
++string;
len2 = (int)STRLEN(string);
if (len2 == 0)
continue;
/* Now we have to verify whether string ends with a substring
* beginning the com_leader. */
for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
{
--off;
if (!STRNCMP(string + off, com_leader, len2 - off))
{
if (i - off < lower_check_bound)
lower_check_bound = i - off;
}
}
}
}
}
return result;
}
#endif
/*
* Return the number of window lines occupied by buffer line "lnum".
*/
int
plines(linenr_T lnum)
{
return plines_win(curwin, lnum, TRUE);
}
int
plines_win(
win_T *wp,
linenr_T lnum,
int winheight) /* when TRUE limit to window height */
{
#if defined(FEAT_DIFF) || defined(PROTO)
/* Check for filler lines above this buffer line. When folded the result
* is one line anyway. */
return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
}
int
plines_nofill(linenr_T lnum)
{
return plines_win_nofill(curwin, lnum, TRUE);
}
int
plines_win_nofill(
win_T *wp,
linenr_T lnum,
int winheight) /* when TRUE limit to window height */
{
#endif
int lines;
if (!wp->w_p_wrap)
return 1;
if (wp->w_width == 0)
return 1;
#ifdef FEAT_FOLDING
/* A folded lines is handled just like an empty line. */
/* NOTE: Caller must handle lines that are MAYBE folded. */
if (lineFolded(wp, lnum) == TRUE)
return 1;
#endif
lines = plines_win_nofold(wp, lnum);
if (winheight > 0 && lines > wp->w_height)
return (int)wp->w_height;
return lines;
}
/*
* Return number of window lines physical line "lnum" will occupy in window
* "wp". Does not care about folding, 'wrap' or 'diff'.
*/
int
plines_win_nofold(win_T *wp, linenr_T lnum)
{
char_u *s;
long col;
int width;
s = ml_get_buf(wp->w_buffer, lnum, FALSE);
if (*s == NUL) /* empty line */
return 1;
col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
/*
* If list mode is on, then the '$' at the end of the line may take up one
* extra column.
*/
if (wp->w_p_list && lcs_eol != NUL)
col += 1;
/*
* Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
*/
width = wp->w_width - win_col_off(wp);
if (width <= 0)
return 32000;
if (col <= width)
return 1;
col -= width;
width += win_col_off2(wp);
return (col + (width - 1)) / width + 1;
}
/*
* Like plines_win(), but only reports the number of physical screen lines
* used from the start of the line to the given column number.
*/
int
plines_win_col(win_T *wp, linenr_T lnum, long column)
{
long col;
char_u *s;
int lines = 0;
int width;
char_u *line;
#ifdef FEAT_DIFF
/* Check for filler lines above this buffer line. When folded the result
* is one line anyway. */
lines = diff_check_fill(wp, lnum);
#endif
if (!wp->w_p_wrap)
return lines + 1;
if (wp->w_width == 0)
return lines + 1;
line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
col = 0;
while (*s != NUL && --column >= 0)
{
col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
MB_PTR_ADV(s);
}
/*
* If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
* INSERT mode, then col must be adjusted so that it represents the last
* screen position of the TAB. This only fixes an error when the TAB wraps