-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathviminfo.c
3325 lines (3039 loc) · 82.9 KB
/
viminfo.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.
*/
/*
* viminfo.c: viminfo related functions
*/
#include "vim.h"
#include "version.h"
/*
* Structure used for reading from the viminfo file.
*/
typedef struct
{
char_u *vir_line; // text of the current line
FILE *vir_fd; // file descriptor
vimconv_T vir_conv; // encoding conversion
int vir_version; // viminfo version detected or -1
garray_T vir_barlines; // lines starting with |
} vir_T;
typedef enum {
BVAL_NR,
BVAL_STRING,
BVAL_EMPTY
} btype_T;
typedef struct {
btype_T bv_type;
long bv_nr;
char_u *bv_string;
char_u *bv_tofree; // free later when not NULL
int bv_len; // length of bv_string
int bv_allocated; // bv_string was allocated
} bval_T;
#if defined(FEAT_VIMINFO) || defined(PROTO)
static int viminfo_errcnt;
/*
* Find the parameter represented by the given character (eg ''', ':', '"', or
* '/') in the 'viminfo' option and return a pointer to the string after it.
* Return NULL if the parameter is not specified in the string.
*/
static char_u *
find_viminfo_parameter(int type)
{
char_u *p;
for (p = p_viminfo; *p; ++p)
{
if (*p == type)
return p + 1;
if (*p == 'n') // 'n' is always the last one
break;
p = vim_strchr(p, ','); // skip until next ','
if (p == NULL) // hit the end without finding parameter
break;
}
return NULL;
}
/*
* Find the parameter represented by the given character (eg ', :, ", or /),
* and return its associated value in the 'viminfo' string.
* Only works for number parameters, not for 'r' or 'n'.
* If the parameter is not specified in the string or there is no following
* number, return -1.
*/
int
get_viminfo_parameter(int type)
{
char_u *p;
p = find_viminfo_parameter(type);
if (p != NULL && VIM_ISDIGIT(*p))
return atoi((char *)p);
return -1;
}
/*
* Get the viminfo file name to use.
* If "file" is given and not empty, use it (has already been expanded by
* cmdline functions).
* Otherwise use "-i file_name", value from 'viminfo' or the default, and
* expand environment variables.
* Returns an allocated string. NULL when out of memory.
*/
static char_u *
viminfo_filename(char_u *file)
{
if (file == NULL || *file == NUL)
{
if (*p_viminfofile != NUL)
file = p_viminfofile;
else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
{
#ifdef VIMINFO_FILE2
# ifdef VMS
if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
# else
# ifdef MSWIN
// Use $VIM only if $HOME is the default "C:/".
if (STRCMP(vim_getenv((char_u *)"HOME", NULL), "C:/") == 0
&& mch_getenv((char_u *)"HOME") == NULL)
# else
if (mch_getenv((char_u *)"HOME") == NULL)
# endif
# endif
{
// don't use $VIM when not available.
expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
if (STRCMP("$VIM", NameBuff) != 0) // $VIM was expanded
file = (char_u *)VIMINFO_FILE2;
else
file = (char_u *)VIMINFO_FILE;
}
else
#endif
file = (char_u *)VIMINFO_FILE;
}
expand_env(file, NameBuff, MAXPATHL);
file = NameBuff;
}
return vim_strsave(file);
}
/*
* write string to viminfo file
* - replace CTRL-V with CTRL-V CTRL-V
* - replace '\n' with CTRL-V 'n'
* - add a '\n' at the end
*
* For a long line:
* - write " CTRL-V <length> \n " in first line
* - write " < <string> \n " in second line
*/
static void
viminfo_writestring(FILE *fd, char_u *p)
{
int c;
char_u *s;
int len = 0;
for (s = p; *s != NUL; ++s)
{
if (*s == Ctrl_V || *s == '\n')
++len;
++len;
}
// If the string will be too long, write its length and put it in the next
// line. Take into account that some room is needed for what comes before
// the string (e.g., variable name). Add something to the length for the
// '<', NL and trailing NUL.
if (len > LSIZE / 2)
fprintf(fd, "\026%d\n<", len + 3);
while ((c = *p++) != NUL)
{
if (c == Ctrl_V || c == '\n')
{
putc(Ctrl_V, fd);
if (c == '\n')
c = 'n';
}
putc(c, fd);
}
putc('\n', fd);
}
/*
* Write a string in quotes that barline_parse() can read back.
* Breaks the line in less than LSIZE pieces when needed.
* Returns remaining characters in the line.
*/
static int
barline_writestring(FILE *fd, char_u *s, int remaining_start)
{
char_u *p;
int remaining = remaining_start;
int len = 2;
// Count the number of characters produced, including quotes.
for (p = s; *p != NUL; ++p)
{
if (*p == NL)
len += 2;
else if (*p == '"' || *p == '\\')
len += 2;
else
++len;
}
if (len > remaining - 2)
{
fprintf(fd, ">%d\n|<", len);
remaining = LSIZE - 20;
}
putc('"', fd);
for (p = s; *p != NUL; ++p)
{
if (*p == NL)
{
putc('\\', fd);
putc('n', fd);
--remaining;
}
else if (*p == '"' || *p == '\\')
{
putc('\\', fd);
putc(*p, fd);
--remaining;
}
else
putc(*p, fd);
--remaining;
if (remaining < 3)
{
putc('\n', fd);
putc('|', fd);
putc('<', fd);
// Leave enough space for another continuation.
remaining = LSIZE - 20;
}
}
putc('"', fd);
return remaining - 2;
}
/*
* Check string read from viminfo file.
* Remove '\n' at the end of the line.
* - replace CTRL-V CTRL-V with CTRL-V
* - replace CTRL-V 'n' with '\n'
*
* Check for a long line as written by viminfo_writestring().
*
* Return the string in allocated memory (NULL when out of memory).
*/
static char_u *
viminfo_readstring(
vir_T *virp,
int off, // offset for virp->vir_line
int convert UNUSED) // convert the string
{
char_u *retval = NULL;
char_u *s, *d;
long len;
if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
{
len = atol((char *)virp->vir_line + off + 1);
if (len > 0 && len < 1000000)
retval = lalloc(len, TRUE);
if (retval == NULL)
{
// Invalid length, line too long, out of memory? Skip next line.
(void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
return NULL;
}
(void)vim_fgets(retval, (int)len, virp->vir_fd);
s = retval + 1; // Skip the leading '<'
}
else
{
retval = vim_strsave(virp->vir_line + off);
if (retval == NULL)
return NULL;
s = retval;
}
// Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place.
d = retval;
while (*s != NUL && *s != '\n')
{
if (s[0] == Ctrl_V && s[1] != NUL)
{
if (s[1] == 'n')
*d++ = '\n';
else
*d++ = Ctrl_V;
s += 2;
}
else
*d++ = *s++;
}
*d = NUL;
if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
{
d = string_convert(&virp->vir_conv, retval, NULL);
if (d != NULL)
{
vim_free(retval);
retval = d;
}
}
return retval;
}
/*
* Read a line from the viminfo file.
* Returns TRUE for end-of-file;
*/
static int
viminfo_readline(vir_T *virp)
{
return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
}
static int
read_viminfo_bufferlist(
vir_T *virp,
int writing)
{
char_u *tab;
linenr_T lnum;
colnr_T col;
buf_T *buf;
char_u *sfname;
char_u *xline;
// Handle long line and escaped characters.
xline = viminfo_readstring(virp, 1, FALSE);
// don't read in if there are files on the command-line or if writing:
if (xline != NULL && !writing && ARGCOUNT == 0
&& find_viminfo_parameter('%') != NULL)
{
// Format is: <fname> Tab <lnum> Tab <col>.
// Watch out for a Tab in the file name, work from the end.
lnum = 0;
col = 0;
tab = vim_strrchr(xline, '\t');
if (tab != NULL)
{
*tab++ = '\0';
col = (colnr_T)atoi((char *)tab);
tab = vim_strrchr(xline, '\t');
if (tab != NULL)
{
*tab++ = '\0';
lnum = atol((char *)tab);
}
}
// Expand "~/" in the file name at "line + 1" to a full path.
// Then try shortening it by comparing with the current directory
expand_env(xline, NameBuff, MAXPATHL);
sfname = shorten_fname1(NameBuff);
buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
if (buf != NULL) // just in case...
{
buf->b_last_cursor.lnum = lnum;
buf->b_last_cursor.col = col;
buflist_setfpos(buf, curwin, lnum, col, FALSE);
}
}
vim_free(xline);
return viminfo_readline(virp);
}
/*
* Return TRUE if "name" is on removable media (depending on 'viminfo').
*/
static int
removable(char_u *name)
{
char_u *p;
char_u part[51];
int retval = FALSE;
size_t n;
name = home_replace_save(NULL, name);
if (name == NULL)
return FALSE;
for (p = p_viminfo; *p; )
{
copy_option_part(&p, part, 51, ", ");
if (part[0] == 'r')
{
n = STRLEN(part + 1);
if (MB_STRNICMP(part + 1, name, n) == 0)
{
retval = TRUE;
break;
}
}
}
vim_free(name);
return retval;
}
static void
write_viminfo_bufferlist(FILE *fp)
{
buf_T *buf;
win_T *win;
tabpage_T *tp;
char_u *line;
int max_buffers;
if (find_viminfo_parameter('%') == NULL)
return;
// Without a number -1 is returned: do all buffers.
max_buffers = get_viminfo_parameter('%');
// Allocate room for the file name, lnum and col.
#define LINE_BUF_LEN (MAXPATHL + 40)
line = alloc(LINE_BUF_LEN);
if (line == NULL)
return;
FOR_ALL_TAB_WINDOWS(tp, win)
set_last_cursor(win);
fputs(_("\n# Buffer list:\n"), fp);
FOR_ALL_BUFFERS(buf)
{
if (buf->b_fname == NULL
|| !buf->b_p_bl
|| bt_quickfix(buf)
|| bt_terminal(buf)
|| removable(buf->b_ffname))
continue;
if (max_buffers-- == 0)
break;
putc('%', fp);
home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
(long)buf->b_last_cursor.lnum,
buf->b_last_cursor.col);
viminfo_writestring(fp, line);
}
vim_free(line);
}
/*
* Buffers for history read from a viminfo file. Only valid while reading.
*/
static histentry_T *viminfo_history[HIST_COUNT] =
{NULL, NULL, NULL, NULL, NULL};
static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
static int viminfo_add_at_front = FALSE;
/*
* Translate a history type number to the associated character.
*/
static int
hist_type2char(
int type,
int use_question) // use '?' instead of '/'
{
if (type == HIST_CMD)
return ':';
if (type == HIST_SEARCH)
{
if (use_question)
return '?';
else
return '/';
}
if (type == HIST_EXPR)
return '=';
return '@';
}
/*
* Prepare for reading the history from the viminfo file.
* This allocates history arrays to store the read history lines.
*/
static void
prepare_viminfo_history(int asklen, int writing)
{
int i;
int num;
int type;
int len;
int hislen;
init_history();
hislen = get_hislen();
viminfo_add_at_front = (asklen != 0 && !writing);
if (asklen > hislen)
asklen = hislen;
for (type = 0; type < HIST_COUNT; ++type)
{
histentry_T *histentry = get_histentry(type);
// Count the number of empty spaces in the history list. Entries read
// from viminfo previously are also considered empty. If there are
// more spaces available than we request, then fill them up.
for (i = 0, num = 0; i < hislen; i++)
if (histentry[i].hisstr == NULL || histentry[i].viminfo)
num++;
len = asklen;
if (num > len)
len = num;
if (len <= 0)
viminfo_history[type] = NULL;
else
viminfo_history[type] = LALLOC_MULT(histentry_T, len);
if (viminfo_history[type] == NULL)
len = 0;
viminfo_hislen[type] = len;
viminfo_hisidx[type] = 0;
}
}
/*
* Accept a line from the viminfo, store it in the history array when it's
* new.
*/
static int
read_viminfo_history(vir_T *virp, int writing)
{
int type;
long_u len;
char_u *val = NULL;
char_u *p;
type = hist_char2type(virp->vir_line[0]);
if (viminfo_hisidx[type] >= viminfo_hislen[type])
goto done;
val = viminfo_readstring(virp, 1, TRUE);
if (val == NULL || *val == NUL)
goto done;
int sep = (*val == ' ' ? NUL : *val);
if (in_history(type, val + (type == HIST_SEARCH), viminfo_add_at_front,
sep, writing))
goto done;
// Need to re-allocate to append the separator byte.
len = STRLEN(val);
p = alloc(len + 2);
if (p == NULL)
goto done;
if (type == HIST_SEARCH)
{
// Search entry: Move the separator from the first
// column to after the NUL.
mch_memmove(p, val + 1, (size_t)len);
p[len] = sep;
}
else
{
// Not a search entry: No separator in the viminfo
// file, add a NUL separator.
mch_memmove(p, val, (size_t)len + 1);
p[len + 1] = NUL;
}
viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
viminfo_hisidx[type]++;
done:
vim_free(val);
return viminfo_readline(virp);
}
/*
* Accept a new style history line from the viminfo, store it in the history
* array when it's new.
*/
static void
handle_viminfo_history(
garray_T *values,
int writing)
{
int type;
long_u len;
char_u *val;
char_u *p;
bval_T *vp = (bval_T *)values->ga_data;
// Check the format:
// |{bartype},{histtype},{timestamp},{separator},"text"
if (values->ga_len < 4
|| vp[0].bv_type != BVAL_NR
|| vp[1].bv_type != BVAL_NR
|| (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
|| vp[3].bv_type != BVAL_STRING)
return;
type = vp[0].bv_nr;
if (type >= HIST_COUNT)
return;
if (viminfo_hisidx[type] >= viminfo_hislen[type])
return;
val = vp[3].bv_string;
if (val == NULL || *val == NUL)
return;
int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
? vp[2].bv_nr : NUL;
int idx;
int overwrite = FALSE;
if (in_history(type, val, viminfo_add_at_front, sep, writing))
return;
// If lines were written by an older Vim we need to avoid
// getting duplicates. See if the entry already exists.
for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
{
p = viminfo_history[type][idx].hisstr;
if (STRCMP(val, p) == 0
&& (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
{
overwrite = TRUE;
break;
}
}
if (!overwrite)
{
// Need to re-allocate to append the separator byte.
len = vp[3].bv_len;
p = alloc(len + 2);
}
else
len = 0; // for picky compilers
if (p != NULL)
{
viminfo_history[type][idx].time_set = vp[1].bv_nr;
if (!overwrite)
{
mch_memmove(p, val, (size_t)len + 1);
// Put the separator after the NUL.
p[len + 1] = sep;
viminfo_history[type][idx].hisstr = p;
viminfo_history[type][idx].hisnum = 0;
viminfo_history[type][idx].viminfo = TRUE;
viminfo_hisidx[type]++;
}
}
}
/*
* Concatenate history lines from viminfo after the lines typed in this Vim.
*/
static void
concat_history(int type)
{
int idx;
int i;
int hislen = get_hislen();
histentry_T *histentry = get_histentry(type);
int *hisidx = get_hisidx(type);
int *hisnum = get_hisnum(type);
idx = *hisidx + viminfo_hisidx[type];
if (idx >= hislen)
idx -= hislen;
else if (idx < 0)
idx = hislen - 1;
if (viminfo_add_at_front)
*hisidx = idx;
else
{
if (*hisidx == -1)
*hisidx = hislen - 1;
do
{
if (histentry[idx].hisstr != NULL || histentry[idx].viminfo)
break;
if (++idx == hislen)
idx = 0;
} while (idx != *hisidx);
if (idx != *hisidx && --idx < 0)
idx = hislen - 1;
}
for (i = 0; i < viminfo_hisidx[type]; i++)
{
vim_free(histentry[idx].hisstr);
histentry[idx].hisstr = viminfo_history[type][i].hisstr;
histentry[idx].viminfo = TRUE;
histentry[idx].time_set = viminfo_history[type][i].time_set;
if (--idx < 0)
idx = hislen - 1;
}
idx += 1;
idx %= hislen;
for (i = 0; i < viminfo_hisidx[type]; i++)
{
histentry[idx++].hisnum = ++*hisnum;
idx %= hislen;
}
}
static int
sort_hist(const void *s1, const void *s2)
{
histentry_T *p1 = *(histentry_T **)s1;
histentry_T *p2 = *(histentry_T **)s2;
if (p1->time_set < p2->time_set) return -1;
if (p1->time_set > p2->time_set) return 1;
return 0;
}
/*
* Merge history lines from viminfo and lines typed in this Vim based on the
* timestamp;
*/
static void
merge_history(int type)
{
int max_len;
histentry_T **tot_hist;
histentry_T *new_hist;
int i;
int len;
int hislen = get_hislen();
histentry_T *histentry = get_histentry(type);
int *hisidx = get_hisidx(type);
int *hisnum = get_hisnum(type);
// Make one long list with all entries.
max_len = hislen + viminfo_hisidx[type];
tot_hist = ALLOC_MULT(histentry_T *, max_len);
new_hist = ALLOC_MULT(histentry_T, hislen);
if (tot_hist == NULL || new_hist == NULL)
{
vim_free(tot_hist);
vim_free(new_hist);
return;
}
for (i = 0; i < viminfo_hisidx[type]; i++)
tot_hist[i] = &viminfo_history[type][i];
len = i;
for (i = 0; i < hislen; i++)
if (histentry[i].hisstr != NULL)
tot_hist[len++] = &histentry[i];
// Sort the list on timestamp.
qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
// Keep the newest ones.
for (i = 0; i < hislen; i++)
{
if (i < len)
{
new_hist[i] = *tot_hist[i];
tot_hist[i]->hisstr = NULL;
if (new_hist[i].hisnum == 0)
new_hist[i].hisnum = ++*hisnum;
}
else
clear_hist_entry(&new_hist[i]);
}
*hisidx = (i < len ? i : len) - 1;
// Free what is not kept.
for (i = 0; i < viminfo_hisidx[type]; i++)
vim_free(viminfo_history[type][i].hisstr);
for (i = 0; i < hislen; i++)
vim_free(histentry[i].hisstr);
vim_free(histentry);
set_histentry(type, new_hist);
vim_free(tot_hist);
}
/*
* Finish reading history lines from viminfo. Not used when writing viminfo.
*/
static void
finish_viminfo_history(vir_T *virp)
{
int type;
int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
for (type = 0; type < HIST_COUNT; ++type)
{
if (get_histentry(type) == NULL)
continue;
if (merge)
merge_history(type);
else
concat_history(type);
VIM_CLEAR(viminfo_history[type]);
viminfo_hisidx[type] = 0;
}
}
/*
* Write history to viminfo file in "fp".
* When "merge" is TRUE merge history lines with a previously read viminfo
* file, data is in viminfo_history[].
* When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
*/
static void
write_viminfo_history(FILE *fp, int merge)
{
int i;
int type;
int num_saved;
int round;
int hislen;
init_history();
hislen = get_hislen();
if (hislen == 0)
return;
for (type = 0; type < HIST_COUNT; ++type)
{
histentry_T *histentry = get_histentry(type);
int *hisidx = get_hisidx(type);
num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
if (num_saved == 0)
continue;
if (num_saved < 0) // Use default
num_saved = hislen;
fprintf(fp, _("\n# %s History (newest to oldest):\n"),
type == HIST_CMD ? _("Command Line") :
type == HIST_SEARCH ? _("Search String") :
type == HIST_EXPR ? _("Expression") :
type == HIST_INPUT ? _("Input Line") :
_("Debug Line"));
if (num_saved > hislen)
num_saved = hislen;
// Merge typed and viminfo history:
// round 1: history of typed commands.
// round 2: history from recently read viminfo.
for (round = 1; round <= 2; ++round)
{
if (round == 1)
// start at newest entry, somewhere in the list
i = *hisidx;
else if (viminfo_hisidx[type] > 0)
// start at newest entry, first in the list
i = 0;
else
// empty list
i = -1;
if (i >= 0)
while (num_saved > 0
&& !(round == 2 && i >= viminfo_hisidx[type]))
{
char_u *p;
time_t timestamp;
int c = NUL;
if (round == 1)
{
p = histentry[i].hisstr;
timestamp = histentry[i].time_set;
}
else
{
p = viminfo_history[type] == NULL ? NULL
: viminfo_history[type][i].hisstr;
timestamp = viminfo_history[type] == NULL ? 0
: viminfo_history[type][i].time_set;
}
if (p != NULL && (round == 2
|| !merge
|| !histentry[i].viminfo))
{
--num_saved;
fputc(hist_type2char(type, TRUE), fp);
// For the search history: put the separator in the
// second column; use a space if there isn't one.
if (type == HIST_SEARCH)
{
c = p[STRLEN(p) + 1];
putc(c == NUL ? ' ' : c, fp);
}
viminfo_writestring(fp, p);
{
char cbuf[NUMBUFLEN];
// New style history with a bar line. Format:
// |{bartype},{histtype},{timestamp},{separator},"text"
if (c == NUL)
cbuf[0] = NUL;
else
sprintf(cbuf, "%d", c);
fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
type, (long)timestamp, cbuf);
barline_writestring(fp, p, LSIZE - 20);
putc('\n', fp);
}
}
if (round == 1)
{
// Decrement index, loop around and stop when back at
// the start.
if (--i < 0)
i = hislen - 1;
if (i == *hisidx)
break;
}
else
{
// Increment index. Stop at the end in the while.
++i;
}
}
}
for (i = 0; i < viminfo_hisidx[type]; ++i)
if (viminfo_history[type] != NULL)
vim_free(viminfo_history[type][i].hisstr);
VIM_CLEAR(viminfo_history[type]);
viminfo_hisidx[type] = 0;
}
}
static void
write_viminfo_barlines(vir_T *virp, FILE *fp_out)
{
int i;
garray_T *gap = &virp->vir_barlines;
int seen_useful = FALSE;
char *line;
if (gap->ga_len <= 0)
return;
fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
// Skip over continuation lines until seeing a useful line.
for (i = 0; i < gap->ga_len; ++i)
{
line = ((char **)(gap->ga_data))[i];
if (seen_useful || line[1] != '<')
{
fputs(line, fp_out);
seen_useful = TRUE;
}
}
}
/*
* Parse a viminfo line starting with '|'.
* Add each decoded value to "values".
* Returns TRUE if the next line is to be read after using the parsed values.
*/
static int
barline_parse(vir_T *virp, char_u *text, garray_T *values)
{
char_u *p = text;
char_u *nextp = NULL;
char_u *buf = NULL;
bval_T *value;
int i;
int allocated = FALSE;
int eof;
char_u *sconv;
int converted;
while (*p == ',')
{
++p;
if (ga_grow(values, 1) == FAIL)
break;
value = (bval_T *)(values->ga_data) + values->ga_len;
if (*p == '>')
{
// Need to read a continuation line. Put strings in allocated
// memory, because virp->vir_line is overwritten.
if (!allocated)
{
for (i = 0; i < values->ga_len; ++i)
{
bval_T *vp = (bval_T *)(values->ga_data) + i;
if (vp->bv_type == BVAL_STRING && !vp->bv_allocated)