blob: 2e89fb5859b82491b683f8181c4b8cc16f6215df [file] [log] [blame]
Bram Moolenaardefa0672019-07-21 19:25:37 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * viminfo.c: viminfo related functions
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaar6bd1d772019-10-09 22:01:25 +020017/*
18 * Structure used for reading from the viminfo file.
19 */
20typedef struct
21{
22 char_u *vir_line; // text of the current line
23 FILE *vir_fd; // file descriptor
24 vimconv_T vir_conv; // encoding conversion
25 int vir_version; // viminfo version detected or -1
26 garray_T vir_barlines; // lines starting with |
27} vir_T;
28
Bram Moolenaar408030e2020-02-10 22:44:32 +010029typedef enum {
30 BVAL_NR,
31 BVAL_STRING,
32 BVAL_EMPTY
33} btype_T;
34
35typedef struct {
36 btype_T bv_type;
37 long bv_nr;
38 char_u *bv_string;
39 char_u *bv_tofree; // free later when not NULL
40 int bv_len; // length of bv_string
41 int bv_allocated; // bv_string was allocated
42} bval_T;
43
Bram Moolenaardefa0672019-07-21 19:25:37 +020044#if defined(FEAT_VIMINFO) || defined(PROTO)
45
46static int viminfo_errcnt;
47
48/*
Bram Moolenaarc3328162019-07-23 22:15:25 +020049 * Find the parameter represented by the given character (eg ''', ':', '"', or
50 * '/') in the 'viminfo' option and return a pointer to the string after it.
51 * Return NULL if the parameter is not specified in the string.
52 */
53 static char_u *
54find_viminfo_parameter(int type)
55{
56 char_u *p;
57
58 for (p = p_viminfo; *p; ++p)
59 {
60 if (*p == type)
61 return p + 1;
62 if (*p == 'n') // 'n' is always the last one
63 break;
64 p = vim_strchr(p, ','); // skip until next ','
65 if (p == NULL) // hit the end without finding parameter
66 break;
67 }
68 return NULL;
69}
70
71/*
72 * Find the parameter represented by the given character (eg ', :, ", or /),
73 * and return its associated value in the 'viminfo' string.
74 * Only works for number parameters, not for 'r' or 'n'.
75 * If the parameter is not specified in the string or there is no following
76 * number, return -1.
77 */
78 int
79get_viminfo_parameter(int type)
80{
81 char_u *p;
82
83 p = find_viminfo_parameter(type);
84 if (p != NULL && VIM_ISDIGIT(*p))
85 return atoi((char *)p);
86 return -1;
87}
88
89/*
Bram Moolenaardefa0672019-07-21 19:25:37 +020090 * Get the viminfo file name to use.
91 * If "file" is given and not empty, use it (has already been expanded by
92 * cmdline functions).
93 * Otherwise use "-i file_name", value from 'viminfo' or the default, and
94 * expand environment variables.
95 * Returns an allocated string. NULL when out of memory.
96 */
97 static char_u *
98viminfo_filename(char_u *file)
99{
100 if (file == NULL || *file == NUL)
101 {
102 if (*p_viminfofile != NUL)
103 file = p_viminfofile;
104 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
105 {
106#ifdef VIMINFO_FILE2
107# ifdef VMS
108 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
109# else
110# ifdef MSWIN
111 // Use $VIM only if $HOME is the default "C:/".
112 if (STRCMP(vim_getenv((char_u *)"HOME", NULL), "C:/") == 0
113 && mch_getenv((char_u *)"HOME") == NULL)
114# else
115 if (mch_getenv((char_u *)"HOME") == NULL)
116# endif
117# endif
118 {
119 // don't use $VIM when not available.
120 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
121 if (STRCMP("$VIM", NameBuff) != 0) // $VIM was expanded
122 file = (char_u *)VIMINFO_FILE2;
123 else
124 file = (char_u *)VIMINFO_FILE;
125 }
126 else
127#endif
128 file = (char_u *)VIMINFO_FILE;
129 }
130 expand_env(file, NameBuff, MAXPATHL);
131 file = NameBuff;
132 }
133 return vim_strsave(file);
134}
135
Bram Moolenaarc3328162019-07-23 22:15:25 +0200136/*
137 * write string to viminfo file
138 * - replace CTRL-V with CTRL-V CTRL-V
139 * - replace '\n' with CTRL-V 'n'
140 * - add a '\n' at the end
141 *
142 * For a long line:
143 * - write " CTRL-V <length> \n " in first line
144 * - write " < <string> \n " in second line
145 */
146 static void
147viminfo_writestring(FILE *fd, char_u *p)
148{
149 int c;
150 char_u *s;
151 int len = 0;
152
153 for (s = p; *s != NUL; ++s)
154 {
155 if (*s == Ctrl_V || *s == '\n')
156 ++len;
157 ++len;
158 }
159
160 // If the string will be too long, write its length and put it in the next
161 // line. Take into account that some room is needed for what comes before
162 // the string (e.g., variable name). Add something to the length for the
163 // '<', NL and trailing NUL.
164 if (len > LSIZE / 2)
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000165 fprintf(fd, "\026%d\n<", len + 3);
Bram Moolenaarc3328162019-07-23 22:15:25 +0200166
167 while ((c = *p++) != NUL)
168 {
169 if (c == Ctrl_V || c == '\n')
170 {
171 putc(Ctrl_V, fd);
172 if (c == '\n')
173 c = 'n';
174 }
175 putc(c, fd);
176 }
177 putc('\n', fd);
178}
179
180/*
181 * Write a string in quotes that barline_parse() can read back.
182 * Breaks the line in less than LSIZE pieces when needed.
183 * Returns remaining characters in the line.
184 */
185 static int
186barline_writestring(FILE *fd, char_u *s, int remaining_start)
187{
188 char_u *p;
189 int remaining = remaining_start;
190 int len = 2;
191
192 // Count the number of characters produced, including quotes.
193 for (p = s; *p != NUL; ++p)
194 {
195 if (*p == NL)
196 len += 2;
197 else if (*p == '"' || *p == '\\')
198 len += 2;
199 else
200 ++len;
201 }
202 if (len > remaining - 2)
203 {
204 fprintf(fd, ">%d\n|<", len);
205 remaining = LSIZE - 20;
206 }
207
208 putc('"', fd);
209 for (p = s; *p != NUL; ++p)
210 {
211 if (*p == NL)
212 {
213 putc('\\', fd);
214 putc('n', fd);
215 --remaining;
216 }
217 else if (*p == '"' || *p == '\\')
218 {
219 putc('\\', fd);
220 putc(*p, fd);
221 --remaining;
222 }
223 else
224 putc(*p, fd);
225 --remaining;
226
227 if (remaining < 3)
228 {
229 putc('\n', fd);
230 putc('|', fd);
231 putc('<', fd);
232 // Leave enough space for another continuation.
233 remaining = LSIZE - 20;
234 }
235 }
236 putc('"', fd);
237 return remaining - 2;
238}
239
240/*
241 * Check string read from viminfo file.
242 * Remove '\n' at the end of the line.
243 * - replace CTRL-V CTRL-V with CTRL-V
244 * - replace CTRL-V 'n' with '\n'
245 *
246 * Check for a long line as written by viminfo_writestring().
247 *
248 * Return the string in allocated memory (NULL when out of memory).
249 */
250 static char_u *
251viminfo_readstring(
252 vir_T *virp,
253 int off, // offset for virp->vir_line
254 int convert UNUSED) // convert the string
255{
Bram Moolenaared7cb2d2021-08-11 17:13:54 +0200256 char_u *retval = NULL;
Bram Moolenaarc3328162019-07-23 22:15:25 +0200257 char_u *s, *d;
258 long len;
259
260 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
261 {
262 len = atol((char *)virp->vir_line + off + 1);
Bram Moolenaared7cb2d2021-08-11 17:13:54 +0200263 if (len > 0 && len < 1000000)
264 retval = lalloc(len, TRUE);
Bram Moolenaarc3328162019-07-23 22:15:25 +0200265 if (retval == NULL)
266 {
Bram Moolenaared7cb2d2021-08-11 17:13:54 +0200267 // Invalid length, line too long, out of memory? Skip next line.
Bram Moolenaarc3328162019-07-23 22:15:25 +0200268 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
269 return NULL;
270 }
271 (void)vim_fgets(retval, (int)len, virp->vir_fd);
272 s = retval + 1; // Skip the leading '<'
273 }
274 else
275 {
276 retval = vim_strsave(virp->vir_line + off);
277 if (retval == NULL)
278 return NULL;
279 s = retval;
280 }
281
282 // Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place.
283 d = retval;
284 while (*s != NUL && *s != '\n')
285 {
286 if (s[0] == Ctrl_V && s[1] != NUL)
287 {
288 if (s[1] == 'n')
289 *d++ = '\n';
290 else
291 *d++ = Ctrl_V;
292 s += 2;
293 }
294 else
295 *d++ = *s++;
296 }
297 *d = NUL;
298
299 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
300 {
301 d = string_convert(&virp->vir_conv, retval, NULL);
302 if (d != NULL)
303 {
304 vim_free(retval);
305 retval = d;
306 }
307 }
308
309 return retval;
310}
311
312/*
313 * Read a line from the viminfo file.
314 * Returns TRUE for end-of-file;
315 */
316 static int
317viminfo_readline(vir_T *virp)
318{
319 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
320}
321
Bram Moolenaardefa0672019-07-21 19:25:37 +0200322 static int
323read_viminfo_bufferlist(
324 vir_T *virp,
325 int writing)
326{
327 char_u *tab;
328 linenr_T lnum;
329 colnr_T col;
330 buf_T *buf;
331 char_u *sfname;
332 char_u *xline;
333
334 // Handle long line and escaped characters.
335 xline = viminfo_readstring(virp, 1, FALSE);
336
337 // don't read in if there are files on the command-line or if writing:
338 if (xline != NULL && !writing && ARGCOUNT == 0
339 && find_viminfo_parameter('%') != NULL)
340 {
341 // Format is: <fname> Tab <lnum> Tab <col>.
342 // Watch out for a Tab in the file name, work from the end.
343 lnum = 0;
344 col = 0;
345 tab = vim_strrchr(xline, '\t');
346 if (tab != NULL)
347 {
348 *tab++ = '\0';
349 col = (colnr_T)atoi((char *)tab);
350 tab = vim_strrchr(xline, '\t');
351 if (tab != NULL)
352 {
353 *tab++ = '\0';
354 lnum = atol((char *)tab);
355 }
356 }
357
358 // Expand "~/" in the file name at "line + 1" to a full path.
359 // Then try shortening it by comparing with the current directory
360 expand_env(xline, NameBuff, MAXPATHL);
361 sfname = shorten_fname1(NameBuff);
362
363 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
364 if (buf != NULL) // just in case...
365 {
366 buf->b_last_cursor.lnum = lnum;
367 buf->b_last_cursor.col = col;
368 buflist_setfpos(buf, curwin, lnum, col, FALSE);
369 }
370 }
371 vim_free(xline);
372
373 return viminfo_readline(virp);
374}
375
Bram Moolenaarc3328162019-07-23 22:15:25 +0200376/*
377 * Return TRUE if "name" is on removable media (depending on 'viminfo').
378 */
379 static int
380removable(char_u *name)
381{
382 char_u *p;
383 char_u part[51];
384 int retval = FALSE;
385 size_t n;
386
387 name = home_replace_save(NULL, name);
388 if (name != NULL)
389 {
390 for (p = p_viminfo; *p; )
391 {
392 copy_option_part(&p, part, 51, ", ");
393 if (part[0] == 'r')
394 {
395 n = STRLEN(part + 1);
396 if (MB_STRNICMP(part + 1, name, n) == 0)
397 {
398 retval = TRUE;
399 break;
400 }
401 }
402 }
403 vim_free(name);
404 }
405 return retval;
406}
407
Bram Moolenaardefa0672019-07-21 19:25:37 +0200408 static void
409write_viminfo_bufferlist(FILE *fp)
410{
411 buf_T *buf;
412 win_T *win;
413 tabpage_T *tp;
414 char_u *line;
415 int max_buffers;
416
417 if (find_viminfo_parameter('%') == NULL)
418 return;
419
420 // Without a number -1 is returned: do all buffers.
421 max_buffers = get_viminfo_parameter('%');
422
423 // Allocate room for the file name, lnum and col.
424#define LINE_BUF_LEN (MAXPATHL + 40)
425 line = alloc(LINE_BUF_LEN);
426 if (line == NULL)
427 return;
428
429 FOR_ALL_TAB_WINDOWS(tp, win)
430 set_last_cursor(win);
431
432 fputs(_("\n# Buffer list:\n"), fp);
433 FOR_ALL_BUFFERS(buf)
434 {
435 if (buf->b_fname == NULL
436 || !buf->b_p_bl
Bram Moolenaardefa0672019-07-21 19:25:37 +0200437 || bt_quickfix(buf)
Bram Moolenaardefa0672019-07-21 19:25:37 +0200438 || bt_terminal(buf)
Bram Moolenaardefa0672019-07-21 19:25:37 +0200439 || removable(buf->b_ffname))
440 continue;
441
442 if (max_buffers-- == 0)
443 break;
444 putc('%', fp);
445 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
446 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
447 (long)buf->b_last_cursor.lnum,
448 buf->b_last_cursor.col);
449 viminfo_writestring(fp, line);
450 }
451 vim_free(line);
452}
453
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200454/*
455 * Buffers for history read from a viminfo file. Only valid while reading.
456 */
457static histentry_T *viminfo_history[HIST_COUNT] =
458 {NULL, NULL, NULL, NULL, NULL};
459static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
460static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
461static int viminfo_add_at_front = FALSE;
462
463/*
464 * Translate a history type number to the associated character.
465 */
466 static int
467hist_type2char(
468 int type,
469 int use_question) // use '?' instead of '/'
470{
471 if (type == HIST_CMD)
472 return ':';
473 if (type == HIST_SEARCH)
474 {
475 if (use_question)
476 return '?';
477 else
478 return '/';
479 }
480 if (type == HIST_EXPR)
481 return '=';
482 return '@';
483}
484
485/*
486 * Prepare for reading the history from the viminfo file.
487 * This allocates history arrays to store the read history lines.
488 */
489 static void
490prepare_viminfo_history(int asklen, int writing)
491{
492 int i;
493 int num;
494 int type;
495 int len;
Bram Moolenaar26b654a2019-07-22 20:50:17 +0200496 int hislen;
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200497
498 init_history();
Bram Moolenaar26b654a2019-07-22 20:50:17 +0200499 hislen = get_hislen();
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200500 viminfo_add_at_front = (asklen != 0 && !writing);
501 if (asklen > hislen)
502 asklen = hislen;
503
504 for (type = 0; type < HIST_COUNT; ++type)
505 {
506 histentry_T *histentry = get_histentry(type);
507
508 // Count the number of empty spaces in the history list. Entries read
509 // from viminfo previously are also considered empty. If there are
510 // more spaces available than we request, then fill them up.
511 for (i = 0, num = 0; i < hislen; i++)
512 if (histentry[i].hisstr == NULL || histentry[i].viminfo)
513 num++;
514 len = asklen;
515 if (num > len)
516 len = num;
517 if (len <= 0)
518 viminfo_history[type] = NULL;
519 else
520 viminfo_history[type] = LALLOC_MULT(histentry_T, len);
521 if (viminfo_history[type] == NULL)
522 len = 0;
523 viminfo_hislen[type] = len;
524 viminfo_hisidx[type] = 0;
525 }
526}
527
528/*
529 * Accept a line from the viminfo, store it in the history array when it's
530 * new.
531 */
532 static int
533read_viminfo_history(vir_T *virp, int writing)
534{
535 int type;
536 long_u len;
537 char_u *val;
538 char_u *p;
539
540 type = hist_char2type(virp->vir_line[0]);
541 if (viminfo_hisidx[type] < viminfo_hislen[type])
542 {
543 val = viminfo_readstring(virp, 1, TRUE);
544 if (val != NULL && *val != NUL)
545 {
546 int sep = (*val == ' ' ? NUL : *val);
547
548 if (!in_history(type, val + (type == HIST_SEARCH),
549 viminfo_add_at_front, sep, writing))
550 {
551 // Need to re-allocate to append the separator byte.
552 len = STRLEN(val);
553 p = alloc(len + 2);
554 if (p != NULL)
555 {
556 if (type == HIST_SEARCH)
557 {
558 // Search entry: Move the separator from the first
559 // column to after the NUL.
560 mch_memmove(p, val + 1, (size_t)len);
561 p[len] = sep;
562 }
563 else
564 {
565 // Not a search entry: No separator in the viminfo
566 // file, add a NUL separator.
567 mch_memmove(p, val, (size_t)len + 1);
568 p[len + 1] = NUL;
569 }
570 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
571 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
572 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
573 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
574 viminfo_hisidx[type]++;
575 }
576 }
577 }
578 vim_free(val);
579 }
580 return viminfo_readline(virp);
581}
582
583/*
584 * Accept a new style history line from the viminfo, store it in the history
585 * array when it's new.
586 */
587 static void
588handle_viminfo_history(
589 garray_T *values,
590 int writing)
591{
592 int type;
593 long_u len;
594 char_u *val;
595 char_u *p;
596 bval_T *vp = (bval_T *)values->ga_data;
597
598 // Check the format:
599 // |{bartype},{histtype},{timestamp},{separator},"text"
600 if (values->ga_len < 4
601 || vp[0].bv_type != BVAL_NR
602 || vp[1].bv_type != BVAL_NR
603 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
604 || vp[3].bv_type != BVAL_STRING)
605 return;
606
607 type = vp[0].bv_nr;
608 if (type >= HIST_COUNT)
609 return;
610 if (viminfo_hisidx[type] < viminfo_hislen[type])
611 {
612 val = vp[3].bv_string;
613 if (val != NULL && *val != NUL)
614 {
615 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
616 ? vp[2].bv_nr : NUL;
617 int idx;
618 int overwrite = FALSE;
619
620 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
621 {
622 // If lines were written by an older Vim we need to avoid
623 // getting duplicates. See if the entry already exists.
624 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
625 {
626 p = viminfo_history[type][idx].hisstr;
627 if (STRCMP(val, p) == 0
628 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
629 {
630 overwrite = TRUE;
631 break;
632 }
633 }
634
635 if (!overwrite)
636 {
637 // Need to re-allocate to append the separator byte.
638 len = vp[3].bv_len;
639 p = alloc(len + 2);
640 }
641 else
642 len = 0; // for picky compilers
643 if (p != NULL)
644 {
645 viminfo_history[type][idx].time_set = vp[1].bv_nr;
646 if (!overwrite)
647 {
648 mch_memmove(p, val, (size_t)len + 1);
649 // Put the separator after the NUL.
650 p[len + 1] = sep;
651 viminfo_history[type][idx].hisstr = p;
652 viminfo_history[type][idx].hisnum = 0;
653 viminfo_history[type][idx].viminfo = TRUE;
654 viminfo_hisidx[type]++;
655 }
656 }
657 }
658 }
659 }
660}
661
662/*
663 * Concatenate history lines from viminfo after the lines typed in this Vim.
664 */
665 static void
666concat_history(int type)
667{
668 int idx;
669 int i;
670 int hislen = get_hislen();
671 histentry_T *histentry = get_histentry(type);
672 int *hisidx = get_hisidx(type);
673 int *hisnum = get_hisnum(type);
674
675 idx = *hisidx + viminfo_hisidx[type];
676 if (idx >= hislen)
677 idx -= hislen;
678 else if (idx < 0)
679 idx = hislen - 1;
680 if (viminfo_add_at_front)
681 *hisidx = idx;
682 else
683 {
684 if (*hisidx == -1)
685 *hisidx = hislen - 1;
686 do
687 {
688 if (histentry[idx].hisstr != NULL || histentry[idx].viminfo)
689 break;
690 if (++idx == hislen)
691 idx = 0;
692 } while (idx != *hisidx);
693 if (idx != *hisidx && --idx < 0)
694 idx = hislen - 1;
695 }
696 for (i = 0; i < viminfo_hisidx[type]; i++)
697 {
698 vim_free(histentry[idx].hisstr);
699 histentry[idx].hisstr = viminfo_history[type][i].hisstr;
700 histentry[idx].viminfo = TRUE;
701 histentry[idx].time_set = viminfo_history[type][i].time_set;
702 if (--idx < 0)
703 idx = hislen - 1;
704 }
705 idx += 1;
706 idx %= hislen;
707 for (i = 0; i < viminfo_hisidx[type]; i++)
708 {
709 histentry[idx++].hisnum = ++*hisnum;
710 idx %= hislen;
711 }
712}
713
714 static int
715sort_hist(const void *s1, const void *s2)
716{
717 histentry_T *p1 = *(histentry_T **)s1;
718 histentry_T *p2 = *(histentry_T **)s2;
719
720 if (p1->time_set < p2->time_set) return -1;
721 if (p1->time_set > p2->time_set) return 1;
722 return 0;
723}
724
725/*
726 * Merge history lines from viminfo and lines typed in this Vim based on the
727 * timestamp;
728 */
729 static void
730merge_history(int type)
731{
732 int max_len;
733 histentry_T **tot_hist;
734 histentry_T *new_hist;
735 int i;
736 int len;
737 int hislen = get_hislen();
738 histentry_T *histentry = get_histentry(type);
739 int *hisidx = get_hisidx(type);
740 int *hisnum = get_hisnum(type);
741
742 // Make one long list with all entries.
743 max_len = hislen + viminfo_hisidx[type];
744 tot_hist = ALLOC_MULT(histentry_T *, max_len);
Bram Moolenaar26b654a2019-07-22 20:50:17 +0200745 new_hist = ALLOC_MULT(histentry_T, hislen);
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200746 if (tot_hist == NULL || new_hist == NULL)
747 {
748 vim_free(tot_hist);
749 vim_free(new_hist);
750 return;
751 }
752 for (i = 0; i < viminfo_hisidx[type]; i++)
753 tot_hist[i] = &viminfo_history[type][i];
754 len = i;
755 for (i = 0; i < hislen; i++)
756 if (histentry[i].hisstr != NULL)
757 tot_hist[len++] = &histentry[i];
758
759 // Sort the list on timestamp.
760 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
761
762 // Keep the newest ones.
763 for (i = 0; i < hislen; i++)
764 {
765 if (i < len)
766 {
767 new_hist[i] = *tot_hist[i];
768 tot_hist[i]->hisstr = NULL;
769 if (new_hist[i].hisnum == 0)
770 new_hist[i].hisnum = ++*hisnum;
771 }
772 else
773 clear_hist_entry(&new_hist[i]);
774 }
775 *hisidx = (i < len ? i : len) - 1;
776
777 // Free what is not kept.
778 for (i = 0; i < viminfo_hisidx[type]; i++)
779 vim_free(viminfo_history[type][i].hisstr);
780 for (i = 0; i < hislen; i++)
781 vim_free(histentry[i].hisstr);
782 vim_free(histentry);
783 set_histentry(type, new_hist);
784 vim_free(tot_hist);
785}
786
787/*
788 * Finish reading history lines from viminfo. Not used when writing viminfo.
789 */
790 static void
791finish_viminfo_history(vir_T *virp)
792{
793 int type;
794 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
795
796 for (type = 0; type < HIST_COUNT; ++type)
797 {
798 if (get_histentry(type) == NULL)
799 continue;
800
801 if (merge)
802 merge_history(type);
803 else
804 concat_history(type);
805
806 VIM_CLEAR(viminfo_history[type]);
807 viminfo_hisidx[type] = 0;
808 }
809}
810
811/*
812 * Write history to viminfo file in "fp".
813 * When "merge" is TRUE merge history lines with a previously read viminfo
814 * file, data is in viminfo_history[].
815 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
816 */
817 static void
818write_viminfo_history(FILE *fp, int merge)
819{
820 int i;
821 int type;
822 int num_saved;
823 int round;
824 int hislen;
825
826 init_history();
827 hislen = get_hislen();
828 if (hislen == 0)
829 return;
830 for (type = 0; type < HIST_COUNT; ++type)
831 {
832 histentry_T *histentry = get_histentry(type);
833 int *hisidx = get_hisidx(type);
834
835 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
836 if (num_saved == 0)
837 continue;
838 if (num_saved < 0) // Use default
839 num_saved = hislen;
840 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
841 type == HIST_CMD ? _("Command Line") :
842 type == HIST_SEARCH ? _("Search String") :
843 type == HIST_EXPR ? _("Expression") :
844 type == HIST_INPUT ? _("Input Line") :
845 _("Debug Line"));
846 if (num_saved > hislen)
847 num_saved = hislen;
848
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200849 // Merge typed and viminfo history:
850 // round 1: history of typed commands.
851 // round 2: history from recently read viminfo.
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200852 for (round = 1; round <= 2; ++round)
853 {
854 if (round == 1)
855 // start at newest entry, somewhere in the list
856 i = *hisidx;
857 else if (viminfo_hisidx[type] > 0)
858 // start at newest entry, first in the list
859 i = 0;
860 else
861 // empty list
862 i = -1;
863 if (i >= 0)
864 while (num_saved > 0
865 && !(round == 2 && i >= viminfo_hisidx[type]))
866 {
867 char_u *p;
868 time_t timestamp;
869 int c = NUL;
870
871 if (round == 1)
872 {
873 p = histentry[i].hisstr;
874 timestamp = histentry[i].time_set;
875 }
876 else
877 {
878 p = viminfo_history[type] == NULL ? NULL
879 : viminfo_history[type][i].hisstr;
880 timestamp = viminfo_history[type] == NULL ? 0
881 : viminfo_history[type][i].time_set;
882 }
883
884 if (p != NULL && (round == 2
885 || !merge
886 || !histentry[i].viminfo))
887 {
888 --num_saved;
889 fputc(hist_type2char(type, TRUE), fp);
890 // For the search history: put the separator in the
891 // second column; use a space if there isn't one.
892 if (type == HIST_SEARCH)
893 {
894 c = p[STRLEN(p) + 1];
895 putc(c == NUL ? ' ' : c, fp);
896 }
897 viminfo_writestring(fp, p);
898
899 {
900 char cbuf[NUMBUFLEN];
901
902 // New style history with a bar line. Format:
903 // |{bartype},{histtype},{timestamp},{separator},"text"
904 if (c == NUL)
905 cbuf[0] = NUL;
906 else
907 sprintf(cbuf, "%d", c);
908 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
909 type, (long)timestamp, cbuf);
910 barline_writestring(fp, p, LSIZE - 20);
911 putc('\n', fp);
912 }
913 }
914 if (round == 1)
915 {
916 // Decrement index, loop around and stop when back at
917 // the start.
918 if (--i < 0)
919 i = hislen - 1;
920 if (i == *hisidx)
921 break;
922 }
923 else
924 {
925 // Increment index. Stop at the end in the while.
926 ++i;
927 }
928 }
929 }
930 for (i = 0; i < viminfo_hisidx[type]; ++i)
931 if (viminfo_history[type] != NULL)
932 vim_free(viminfo_history[type][i].hisstr);
933 VIM_CLEAR(viminfo_history[type]);
934 viminfo_hisidx[type] = 0;
935 }
936}
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200937
Bram Moolenaardefa0672019-07-21 19:25:37 +0200938 static void
939write_viminfo_barlines(vir_T *virp, FILE *fp_out)
940{
941 int i;
942 garray_T *gap = &virp->vir_barlines;
943 int seen_useful = FALSE;
944 char *line;
945
946 if (gap->ga_len > 0)
947 {
948 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
949
950 // Skip over continuation lines until seeing a useful line.
951 for (i = 0; i < gap->ga_len; ++i)
952 {
953 line = ((char **)(gap->ga_data))[i];
954 if (seen_useful || line[1] != '<')
955 {
956 fputs(line, fp_out);
957 seen_useful = TRUE;
958 }
959 }
960 }
961}
962
963/*
964 * Parse a viminfo line starting with '|'.
965 * Add each decoded value to "values".
966 * Returns TRUE if the next line is to be read after using the parsed values.
967 */
968 static int
969barline_parse(vir_T *virp, char_u *text, garray_T *values)
970{
971 char_u *p = text;
972 char_u *nextp = NULL;
973 char_u *buf = NULL;
974 bval_T *value;
975 int i;
976 int allocated = FALSE;
977 int eof;
978 char_u *sconv;
979 int converted;
980
981 while (*p == ',')
982 {
983 ++p;
984 if (ga_grow(values, 1) == FAIL)
985 break;
986 value = (bval_T *)(values->ga_data) + values->ga_len;
987
988 if (*p == '>')
989 {
990 // Need to read a continuation line. Put strings in allocated
991 // memory, because virp->vir_line is overwritten.
992 if (!allocated)
993 {
994 for (i = 0; i < values->ga_len; ++i)
995 {
996 bval_T *vp = (bval_T *)(values->ga_data) + i;
997
998 if (vp->bv_type == BVAL_STRING && !vp->bv_allocated)
999 {
1000 vp->bv_string = vim_strnsave(vp->bv_string, vp->bv_len);
1001 vp->bv_allocated = TRUE;
1002 }
1003 }
1004 allocated = TRUE;
1005 }
1006
1007 if (vim_isdigit(p[1]))
1008 {
1009 size_t len;
1010 size_t todo;
1011 size_t n;
1012
1013 // String value was split into lines that are each shorter
1014 // than LSIZE:
1015 // |{bartype},>{length of "{text}{text2}"}
1016 // |<"{text1}
1017 // |<{text2}",{value}
1018 // Length includes the quotes.
1019 ++p;
1020 len = getdigits(&p);
1021 buf = alloc((int)(len + 1));
1022 if (buf == NULL)
1023 return TRUE;
1024 p = buf;
1025 for (todo = len; todo > 0; todo -= n)
1026 {
1027 eof = viminfo_readline(virp);
1028 if (eof || virp->vir_line[0] != '|'
1029 || virp->vir_line[1] != '<')
1030 {
1031 // File was truncated or garbled. Read another line if
1032 // this one starts with '|'.
1033 vim_free(buf);
1034 return eof || virp->vir_line[0] == '|';
1035 }
1036 // Get length of text, excluding |< and NL chars.
1037 n = STRLEN(virp->vir_line);
1038 while (n > 0 && (virp->vir_line[n - 1] == NL
1039 || virp->vir_line[n - 1] == CAR))
1040 --n;
1041 n -= 2;
1042 if (n > todo)
1043 {
1044 // more values follow after the string
1045 nextp = virp->vir_line + 2 + todo;
1046 n = todo;
1047 }
1048 mch_memmove(p, virp->vir_line + 2, n);
1049 p += n;
1050 }
1051 *p = NUL;
1052 p = buf;
1053 }
1054 else
1055 {
1056 // Line ending in ">" continues in the next line:
1057 // |{bartype},{lots of values},>
1058 // |<{value},{value}
1059 eof = viminfo_readline(virp);
1060 if (eof || virp->vir_line[0] != '|'
1061 || virp->vir_line[1] != '<')
1062 // File was truncated or garbled. Read another line if
1063 // this one starts with '|'.
1064 return eof || virp->vir_line[0] == '|';
1065 p = virp->vir_line + 2;
1066 }
1067 }
1068
1069 if (isdigit(*p))
1070 {
1071 value->bv_type = BVAL_NR;
1072 value->bv_nr = getdigits(&p);
1073 ++values->ga_len;
1074 }
1075 else if (*p == '"')
1076 {
1077 int len = 0;
1078 char_u *s = p;
1079
1080 // Unescape special characters in-place.
1081 ++p;
1082 while (*p != '"')
1083 {
1084 if (*p == NL || *p == NUL)
1085 return TRUE; // syntax error, drop the value
1086 if (*p == '\\')
1087 {
1088 ++p;
1089 if (*p == 'n')
1090 s[len++] = '\n';
1091 else
1092 s[len++] = *p;
1093 ++p;
1094 }
1095 else
1096 s[len++] = *p++;
1097 }
1098 ++p;
1099 s[len] = NUL;
1100
1101 converted = FALSE;
Bram Moolenaar408030e2020-02-10 22:44:32 +01001102 value->bv_tofree = NULL;
Bram Moolenaardefa0672019-07-21 19:25:37 +02001103 if (virp->vir_conv.vc_type != CONV_NONE && *s != NUL)
1104 {
1105 sconv = string_convert(&virp->vir_conv, s, NULL);
1106 if (sconv != NULL)
1107 {
1108 if (s == buf)
Bram Moolenaar408030e2020-02-10 22:44:32 +01001109 // the converted string is stored in bv_string and
1110 // freed later, also need to free "buf" later
1111 value->bv_tofree = buf;
Bram Moolenaardefa0672019-07-21 19:25:37 +02001112 s = sconv;
Bram Moolenaardefa0672019-07-21 19:25:37 +02001113 converted = TRUE;
1114 }
1115 }
1116
1117 // Need to copy in allocated memory if the string wasn't allocated
1118 // above and we did allocate before, thus vir_line may change.
Bram Moolenaar408030e2020-02-10 22:44:32 +01001119 if (s != buf && allocated && !converted)
Bram Moolenaardefa0672019-07-21 19:25:37 +02001120 s = vim_strsave(s);
1121 value->bv_string = s;
1122 value->bv_type = BVAL_STRING;
1123 value->bv_len = len;
1124 value->bv_allocated = allocated || converted;
1125 ++values->ga_len;
1126 if (nextp != NULL)
1127 {
1128 // values following a long string
1129 p = nextp;
1130 nextp = NULL;
1131 }
1132 }
1133 else if (*p == ',')
1134 {
1135 value->bv_type = BVAL_EMPTY;
1136 ++values->ga_len;
1137 }
1138 else
1139 break;
1140 }
1141 return TRUE;
1142}
1143
Bram Moolenaardefa0672019-07-21 19:25:37 +02001144 static void
1145write_viminfo_version(FILE *fp_out)
1146{
1147 fprintf(fp_out, "# Viminfo version\n|%d,%d\n\n",
1148 BARTYPE_VERSION, VIMINFO_VERSION);
1149}
1150
1151 static int
1152no_viminfo(void)
1153{
1154 // "vim -i NONE" does not read or write a viminfo file
1155 return STRCMP(p_viminfofile, "NONE") == 0;
1156}
1157
1158/*
1159 * Report an error for reading a viminfo file.
1160 * Count the number of errors. When there are more than 10, return TRUE.
1161 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02001162 static int
Bram Moolenaardefa0672019-07-21 19:25:37 +02001163viminfo_error(char *errnum, char *message, char_u *line)
1164{
1165 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1166 errnum, message);
1167 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
1168 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1169 IObuff[STRLEN(IObuff) - 1] = NUL;
1170 emsg((char *)IObuff);
1171 if (++viminfo_errcnt >= 10)
1172 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00001173 emsg(_(e_viminfo_too_many_errors_skipping_rest_of_file));
Bram Moolenaardefa0672019-07-21 19:25:37 +02001174 return TRUE;
1175 }
1176 return FALSE;
1177}
1178
1179/*
1180 * Compare the 'encoding' value in the viminfo file with the current value of
1181 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
1182 * conversion of text with iconv() in viminfo_readstring().
1183 */
1184 static int
1185viminfo_encoding(vir_T *virp)
1186{
1187 char_u *p;
1188 int i;
1189
1190 if (get_viminfo_parameter('c') != 0)
1191 {
1192 p = vim_strchr(virp->vir_line, '=');
1193 if (p != NULL)
1194 {
1195 // remove trailing newline
1196 ++p;
1197 for (i = 0; vim_isprintc(p[i]); ++i)
1198 ;
1199 p[i] = NUL;
1200
1201 convert_setup(&virp->vir_conv, p, p_enc);
1202 }
1203 }
1204 return viminfo_readline(virp);
1205}
1206
1207#if defined(FEAT_EVAL) || defined(PROTO)
1208/*
1209 * Restore global vars that start with a capital from the viminfo file
1210 */
1211 static int
1212read_viminfo_varlist(vir_T *virp, int writing)
1213{
1214 char_u *tab;
1215 int type = VAR_NUMBER;
1216 typval_T tv;
1217 funccal_entry_T funccal_entry;
1218
1219 if (!writing && (find_viminfo_parameter('!') != NULL))
1220 {
1221 tab = vim_strchr(virp->vir_line + 1, '\t');
1222 if (tab != NULL)
1223 {
1224 *tab++ = '\0'; // isolate the variable name
1225 switch (*tab)
1226 {
1227 case 'S': type = VAR_STRING; break;
Bram Moolenaardefa0672019-07-21 19:25:37 +02001228 case 'F': type = VAR_FLOAT; break;
Bram Moolenaardefa0672019-07-21 19:25:37 +02001229 case 'D': type = VAR_DICT; break;
1230 case 'L': type = VAR_LIST; break;
1231 case 'B': type = VAR_BLOB; break;
1232 case 'X': type = VAR_SPECIAL; break;
1233 }
1234
1235 tab = vim_strchr(tab, '\t');
1236 if (tab != NULL)
1237 {
1238 tv.v_type = type;
1239 if (type == VAR_STRING || type == VAR_DICT
1240 || type == VAR_LIST || type == VAR_BLOB)
1241 tv.vval.v_string = viminfo_readstring(virp,
1242 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaardefa0672019-07-21 19:25:37 +02001243 else if (type == VAR_FLOAT)
Bram Moolenaar29500652021-08-08 15:43:34 +02001244 (void)string2float(tab + 1, &tv.vval.v_float, FALSE);
Bram Moolenaardefa0672019-07-21 19:25:37 +02001245 else
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001246 {
Bram Moolenaardefa0672019-07-21 19:25:37 +02001247 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001248 if (type == VAR_SPECIAL && (tv.vval.v_number == VVAL_FALSE
1249 || tv.vval.v_number == VVAL_TRUE))
1250 tv.v_type = VAR_BOOL;
1251 }
Bram Moolenaardefa0672019-07-21 19:25:37 +02001252 if (type == VAR_DICT || type == VAR_LIST)
1253 {
1254 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
1255
1256 if (etv == NULL)
1257 // Failed to parse back the dict or list, use it as a
1258 // string.
1259 tv.v_type = VAR_STRING;
1260 else
1261 {
1262 vim_free(tv.vval.v_string);
1263 tv = *etv;
1264 vim_free(etv);
1265 }
1266 }
1267 else if (type == VAR_BLOB)
1268 {
1269 blob_T *blob = string2blob(tv.vval.v_string);
1270
1271 if (blob == NULL)
1272 // Failed to parse back the blob, use it as a string.
1273 tv.v_type = VAR_STRING;
1274 else
1275 {
1276 vim_free(tv.vval.v_string);
1277 tv.v_type = VAR_BLOB;
1278 tv.vval.v_blob = blob;
1279 }
1280 }
1281
1282 // when in a function use global variables
1283 save_funccal(&funccal_entry);
1284 set_var(virp->vir_line + 1, &tv, FALSE);
1285 restore_funccal();
1286
1287 if (tv.v_type == VAR_STRING)
1288 vim_free(tv.vval.v_string);
1289 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
1290 tv.v_type == VAR_BLOB)
1291 clear_tv(&tv);
1292 }
1293 }
1294 }
1295
1296 return viminfo_readline(virp);
1297}
1298
1299/*
1300 * Write global vars that start with a capital to the viminfo file
1301 */
1302 static void
1303write_viminfo_varlist(FILE *fp)
1304{
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001305 hashtab_T *gvht = get_globvar_ht();
Bram Moolenaardefa0672019-07-21 19:25:37 +02001306 hashitem_T *hi;
1307 dictitem_T *this_var;
1308 int todo;
1309 char *s = "";
1310 char_u *p;
1311 char_u *tofree;
1312 char_u numbuf[NUMBUFLEN];
1313
1314 if (find_viminfo_parameter('!') == NULL)
1315 return;
1316
1317 fputs(_("\n# global variables:\n"), fp);
1318
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001319 todo = (int)gvht->ht_used;
1320 for (hi = gvht->ht_array; todo > 0; ++hi)
Bram Moolenaardefa0672019-07-21 19:25:37 +02001321 {
1322 if (!HASHITEM_EMPTY(hi))
1323 {
1324 --todo;
1325 this_var = HI2DI(hi);
1326 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
1327 {
1328 switch (this_var->di_tv.v_type)
1329 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001330 case VAR_STRING: s = "STR"; break;
1331 case VAR_NUMBER: s = "NUM"; break;
1332 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar5b157fe2020-06-07 16:08:08 +02001333 case VAR_DICT:
1334 {
1335 dict_T *di = this_var->di_tv.vval.v_dict;
1336 int copyID = get_copyID();
1337
1338 s = "DIC";
1339 if (di != NULL && !set_ref_in_ht(
1340 &di->dv_hashtab, copyID, NULL)
1341 && di->dv_copyID == copyID)
1342 // has a circular reference, can't turn the
1343 // value into a string
1344 continue;
1345 break;
1346 }
1347 case VAR_LIST:
1348 {
1349 list_T *l = this_var->di_tv.vval.v_list;
1350 int copyID = get_copyID();
1351
1352 s = "LIS";
1353 if (l != NULL && !set_ref_in_list_items(
1354 l, copyID, NULL)
1355 && l->lv_copyID == copyID)
1356 // has a circular reference, can't turn the
1357 // value into a string
1358 continue;
1359 break;
1360 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001361 case VAR_BLOB: s = "BLO"; break;
1362 case VAR_BOOL: s = "XPL"; break; // backwards compat.
Bram Moolenaardefa0672019-07-21 19:25:37 +02001363 case VAR_SPECIAL: s = "XPL"; break;
1364
1365 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001366 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 case VAR_VOID:
Bram Moolenaardefa0672019-07-21 19:25:37 +02001368 case VAR_FUNC:
1369 case VAR_PARTIAL:
1370 case VAR_JOB:
1371 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001372 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001373 case VAR_CLASS:
1374 case VAR_OBJECT:
Bram Moolenaardefa0672019-07-21 19:25:37 +02001375 continue;
1376 }
1377 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001378 if (this_var->di_tv.v_type == VAR_BOOL
1379 || this_var->di_tv.v_type == VAR_SPECIAL)
Bram Moolenaardefa0672019-07-21 19:25:37 +02001380 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001381 // do not use "v:true" but "1"
Bram Moolenaardefa0672019-07-21 19:25:37 +02001382 sprintf((char *)numbuf, "%ld",
1383 (long)this_var->di_tv.vval.v_number);
1384 p = numbuf;
1385 tofree = NULL;
1386 }
1387 else
1388 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
1389 if (p != NULL)
1390 viminfo_writestring(fp, p);
1391 vim_free(tofree);
1392 }
1393 }
1394 }
1395}
1396#endif // FEAT_EVAL
1397
Bram Moolenaarc3328162019-07-23 22:15:25 +02001398 static int
1399read_viminfo_sub_string(vir_T *virp, int force)
1400{
1401 if (force || get_old_sub() == NULL)
1402 set_old_sub(viminfo_readstring(virp, 1, TRUE));
1403 return viminfo_readline(virp);
1404}
1405
1406 static void
1407write_viminfo_sub_string(FILE *fp)
1408{
1409 char_u *old_sub = get_old_sub();
1410
1411 if (get_viminfo_parameter('/') != 0 && old_sub != NULL)
1412 {
1413 fputs(_("\n# Last Substitute String:\n$"), fp);
1414 viminfo_writestring(fp, old_sub);
1415 }
1416}
1417
1418/*
1419 * Functions relating to reading/writing the search pattern from viminfo
1420 */
1421
1422 static int
1423read_viminfo_search_pattern(vir_T *virp, int force)
1424{
1425 char_u *lp;
1426 int idx = -1;
1427 int magic = FALSE;
1428 int no_scs = FALSE;
1429 int off_line = FALSE;
1430 int off_end = 0;
1431 long off = 0;
1432 int setlast = FALSE;
1433#ifdef FEAT_SEARCH_EXTRA
1434 static int hlsearch_on = FALSE;
1435#endif
1436 char_u *val;
1437 spat_T *spat;
1438
1439 // Old line types:
1440 // "/pat", "&pat": search/subst. pat
1441 // "~/pat", "~&pat": last used search/subst. pat
1442 // New line types:
1443 // "~h", "~H": hlsearch highlighting off/on
1444 // "~<magic><smartcase><line><end><off><last><which>pat"
1445 // <magic>: 'm' off, 'M' on
1446 // <smartcase>: 's' off, 'S' on
1447 // <line>: 'L' line offset, 'l' char offset
1448 // <end>: 'E' from end, 'e' from start
1449 // <off>: decimal, offset
1450 // <last>: '~' last used pattern
1451 // <which>: '/' search pat, '&' subst. pat
1452 lp = virp->vir_line;
1453 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) // new line type
1454 {
1455 if (lp[1] == 'M') // magic on
1456 magic = TRUE;
1457 if (lp[2] == 's')
1458 no_scs = TRUE;
1459 if (lp[3] == 'L')
1460 off_line = TRUE;
1461 if (lp[4] == 'E')
1462 off_end = SEARCH_END;
1463 lp += 5;
1464 off = getdigits(&lp);
1465 }
1466 if (lp[0] == '~') // use this pattern for last-used pattern
1467 {
1468 setlast = TRUE;
1469 lp++;
1470 }
1471 if (lp[0] == '/')
1472 idx = RE_SEARCH;
1473 else if (lp[0] == '&')
1474 idx = RE_SUBST;
1475#ifdef FEAT_SEARCH_EXTRA
1476 else if (lp[0] == 'h') // ~h: 'hlsearch' highlighting off
1477 hlsearch_on = FALSE;
1478 else if (lp[0] == 'H') // ~H: 'hlsearch' highlighting on
1479 hlsearch_on = TRUE;
1480#endif
Bram Moolenaarc3328162019-07-23 22:15:25 +02001481 if (idx >= 0)
1482 {
Bram Moolenaar736cd2c2019-07-25 21:58:19 +02001483 spat = get_spat(idx);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001484 if (force || spat->pat == NULL)
1485 {
1486 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
1487 TRUE);
1488 if (val != NULL)
1489 {
1490 set_last_search_pat(val, idx, magic, setlast);
1491 vim_free(val);
1492 spat->no_scs = no_scs;
1493 spat->off.line = off_line;
1494 spat->off.end = off_end;
1495 spat->off.off = off;
1496#ifdef FEAT_SEARCH_EXTRA
1497 if (setlast)
1498 set_no_hlsearch(!hlsearch_on);
1499#endif
1500 }
1501 }
1502 }
1503 return viminfo_readline(virp);
1504}
1505
1506 static void
1507wvsp_one(
1508 FILE *fp, // file to write to
1509 int idx, // spats[] index
1510 char *s, // search pat
1511 int sc) // dir char
1512{
1513 spat_T *spat = get_spat(idx);
1514 if (spat->pat != NULL)
1515 {
1516 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
1517 // off.dir is not stored, it's reset to forward
1518 fprintf(fp, "%c%c%c%c%ld%s%c",
1519 spat->magic ? 'M' : 'm', // magic
1520 spat->no_scs ? 's' : 'S', // smartcase
1521 spat->off.line ? 'L' : 'l', // line offset
1522 spat->off.end ? 'E' : 'e', // offset from end
1523 spat->off.off, // offset
1524 get_spat_last_idx() == idx ? "~" : "", // last used pat
1525 sc);
1526 viminfo_writestring(fp, spat->pat);
1527 }
1528}
1529
1530 static void
1531write_viminfo_search_pattern(FILE *fp)
1532{
1533 if (get_viminfo_parameter('/') != 0)
1534 {
1535#ifdef FEAT_SEARCH_EXTRA
1536 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
1537 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
1538#endif
1539 wvsp_one(fp, RE_SEARCH, "", '/');
1540 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
1541 }
1542}
1543
1544/*
1545 * Functions relating to reading/writing registers from viminfo
1546 */
1547
1548static yankreg_T *y_read_regs = NULL;
1549
1550#define REG_PREVIOUS 1
1551#define REG_EXEC 2
1552
1553/*
1554 * Prepare for reading viminfo registers when writing viminfo later.
1555 */
1556 static void
1557prepare_viminfo_registers(void)
1558{
1559 y_read_regs = ALLOC_CLEAR_MULT(yankreg_T, NUM_REGISTERS);
1560}
1561
1562 static void
1563finish_viminfo_registers(void)
1564{
1565 int i;
1566 int j;
1567
1568 if (y_read_regs != NULL)
1569 {
1570 for (i = 0; i < NUM_REGISTERS; ++i)
1571 if (y_read_regs[i].y_array != NULL)
1572 {
1573 for (j = 0; j < y_read_regs[i].y_size; j++)
1574 vim_free(y_read_regs[i].y_array[j]);
1575 vim_free(y_read_regs[i].y_array);
1576 }
1577 VIM_CLEAR(y_read_regs);
1578 }
1579}
1580
1581 static int
1582read_viminfo_register(vir_T *virp, int force)
1583{
1584 int eof;
1585 int do_it = TRUE;
1586 int size;
1587 int limit;
1588 int i;
1589 int set_prev = FALSE;
1590 char_u *str;
1591 char_u **array = NULL;
1592 int new_type = MCHAR; // init to shut up compiler
1593 colnr_T new_width = 0; // init to shut up compiler
1594 yankreg_T *y_current_p;
1595
1596 // We only get here (hopefully) if line[0] == '"'
1597 str = virp->vir_line + 1;
1598
1599 // If the line starts with "" this is the y_previous register.
1600 if (*str == '"')
1601 {
1602 set_prev = TRUE;
1603 str++;
1604 }
1605
1606 if (!ASCII_ISALNUM(*str) && *str != '-')
1607 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001608 if (viminfo_error("E577: ", _(e_illegal_register_name), virp->vir_line))
Bram Moolenaarc3328162019-07-23 22:15:25 +02001609 return TRUE; // too many errors, pretend end-of-file
1610 do_it = FALSE;
1611 }
1612 get_yank_register(*str++, FALSE);
1613 y_current_p = get_y_current();
1614 if (!force && y_current_p->y_array != NULL)
1615 do_it = FALSE;
1616
1617 if (*str == '@')
1618 {
1619 // "x@: register x used for @@
1620 if (force || get_execreg_lastc() == NUL)
1621 set_execreg_lastc(str[-1]);
1622 }
1623
1624 size = 0;
1625 limit = 100; // Optimized for registers containing <= 100 lines
1626 if (do_it)
1627 {
1628 // Build the new register in array[].
1629 // y_array is kept as-is until done.
1630 // The "do_it" flag is reset when something is wrong, in which case
1631 // array[] needs to be freed.
1632 if (set_prev)
1633 set_y_previous(y_current_p);
1634 array = ALLOC_MULT(char_u *, limit);
1635 str = skipwhite(skiptowhite(str));
1636 if (STRNCMP(str, "CHAR", 4) == 0)
1637 new_type = MCHAR;
1638 else if (STRNCMP(str, "BLOCK", 5) == 0)
1639 new_type = MBLOCK;
1640 else
1641 new_type = MLINE;
1642 // get the block width; if it's missing we get a zero, which is OK
1643 str = skipwhite(skiptowhite(str));
1644 new_width = getdigits(&str);
1645 }
1646
1647 while (!(eof = viminfo_readline(virp))
1648 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
1649 {
1650 if (do_it)
1651 {
1652 if (size == limit)
1653 {
1654 char_u **new_array = (char_u **)
1655 alloc(limit * 2 * sizeof(char_u *));
1656
1657 if (new_array == NULL)
1658 {
1659 do_it = FALSE;
1660 break;
1661 }
1662 for (i = 0; i < limit; i++)
1663 new_array[i] = array[i];
1664 vim_free(array);
1665 array = new_array;
1666 limit *= 2;
1667 }
1668 str = viminfo_readstring(virp, 1, TRUE);
1669 if (str != NULL)
1670 array[size++] = str;
1671 else
1672 // error, don't store the result
1673 do_it = FALSE;
1674 }
1675 }
1676
1677 if (do_it)
1678 {
1679 // free y_array[]
1680 for (i = 0; i < y_current_p->y_size; i++)
1681 vim_free(y_current_p->y_array[i]);
1682 vim_free(y_current_p->y_array);
1683
1684 y_current_p->y_type = new_type;
1685 y_current_p->y_width = new_width;
1686 y_current_p->y_size = size;
1687 y_current_p->y_time_set = 0;
1688 if (size == 0)
1689 {
1690 y_current_p->y_array = NULL;
1691 }
1692 else
1693 {
1694 // Move the lines from array[] to y_array[].
1695 y_current_p->y_array = ALLOC_MULT(char_u *, size);
1696 for (i = 0; i < size; i++)
1697 {
1698 if (y_current_p->y_array == NULL)
1699 vim_free(array[i]);
1700 else
1701 y_current_p->y_array[i] = array[i];
1702 }
1703 }
1704 }
1705 else
1706 {
1707 // Free array[] if it was filled.
1708 for (i = 0; i < size; i++)
1709 vim_free(array[i]);
1710 }
1711 vim_free(array);
1712
1713 return eof;
1714}
1715
1716/*
1717 * Accept a new style register line from the viminfo, store it when it's new.
1718 */
1719 static void
1720handle_viminfo_register(garray_T *values, int force)
1721{
1722 bval_T *vp = (bval_T *)values->ga_data;
1723 int flags;
1724 int name;
1725 int type;
1726 int linecount;
1727 int width;
1728 time_t timestamp;
1729 yankreg_T *y_ptr;
1730 yankreg_T *y_regs_p = get_y_regs();
1731 int i;
1732
1733 // Check the format:
1734 // |{bartype},{flags},{name},{type},
1735 // {linecount},{width},{timestamp},"line1","line2"
1736 if (values->ga_len < 6
1737 || vp[0].bv_type != BVAL_NR
1738 || vp[1].bv_type != BVAL_NR
1739 || vp[2].bv_type != BVAL_NR
1740 || vp[3].bv_type != BVAL_NR
1741 || vp[4].bv_type != BVAL_NR
1742 || vp[5].bv_type != BVAL_NR)
1743 return;
1744 flags = vp[0].bv_nr;
1745 name = vp[1].bv_nr;
1746 if (name < 0 || name >= NUM_REGISTERS)
1747 return;
1748 type = vp[2].bv_nr;
1749 if (type != MCHAR && type != MLINE && type != MBLOCK)
1750 return;
1751 linecount = vp[3].bv_nr;
1752 if (values->ga_len < 6 + linecount)
1753 return;
1754 width = vp[4].bv_nr;
1755 if (width < 0)
1756 return;
1757
1758 if (y_read_regs != NULL)
1759 // Reading viminfo for merging and writing. Store the register
1760 // content, don't update the current registers.
1761 y_ptr = &y_read_regs[name];
1762 else
1763 y_ptr = &y_regs_p[name];
1764
1765 // Do not overwrite unless forced or the timestamp is newer.
1766 timestamp = (time_t)vp[5].bv_nr;
1767 if (y_ptr->y_array != NULL && !force
1768 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
1769 return;
1770
1771 if (y_ptr->y_array != NULL)
1772 for (i = 0; i < y_ptr->y_size; i++)
1773 vim_free(y_ptr->y_array[i]);
1774 vim_free(y_ptr->y_array);
1775
1776 if (y_read_regs == NULL)
1777 {
1778 if (flags & REG_PREVIOUS)
1779 set_y_previous(y_ptr);
1780 if ((flags & REG_EXEC) && (force || get_execreg_lastc() == NUL))
1781 set_execreg_lastc(get_register_name(name));
1782 }
1783 y_ptr->y_type = type;
1784 y_ptr->y_width = width;
1785 y_ptr->y_size = linecount;
1786 y_ptr->y_time_set = timestamp;
1787 if (linecount == 0)
1788 {
1789 y_ptr->y_array = NULL;
1790 return;
1791 }
1792 y_ptr->y_array = ALLOC_MULT(char_u *, linecount);
1793 if (y_ptr->y_array == NULL)
1794 {
1795 y_ptr->y_size = 0; // ensure object state is consistent
1796 return;
1797 }
1798 for (i = 0; i < linecount; i++)
1799 {
1800 if (vp[i + 6].bv_allocated)
1801 {
1802 y_ptr->y_array[i] = vp[i + 6].bv_string;
1803 vp[i + 6].bv_string = NULL;
1804 }
1805 else
1806 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
1807 }
1808}
1809
1810 static void
1811write_viminfo_registers(FILE *fp)
1812{
1813 int i, j;
1814 char_u *type;
1815 char_u c;
1816 int num_lines;
1817 int max_num_lines;
1818 int max_kbyte;
1819 long len;
1820 yankreg_T *y_ptr;
1821 yankreg_T *y_regs_p = get_y_regs();;
1822
1823 fputs(_("\n# Registers:\n"), fp);
1824
1825 // Get '<' value, use old '"' value if '<' is not found.
1826 max_num_lines = get_viminfo_parameter('<');
1827 if (max_num_lines < 0)
1828 max_num_lines = get_viminfo_parameter('"');
1829 if (max_num_lines == 0)
1830 return;
1831 max_kbyte = get_viminfo_parameter('s');
1832 if (max_kbyte == 0)
1833 return;
1834
1835 for (i = 0; i < NUM_REGISTERS; i++)
1836 {
1837#ifdef FEAT_CLIPBOARD
1838 // Skip '*'/'+' register, we don't want them back next time
1839 if (i == STAR_REGISTER || i == PLUS_REGISTER)
1840 continue;
1841#endif
1842#ifdef FEAT_DND
1843 // Neither do we want the '~' register
1844 if (i == TILDE_REGISTER)
1845 continue;
1846#endif
1847 // When reading viminfo for merging and writing: Use the register from
1848 // viminfo if it's newer.
1849 if (y_read_regs != NULL
1850 && y_read_regs[i].y_array != NULL
1851 && (y_regs_p[i].y_array == NULL ||
1852 y_read_regs[i].y_time_set > y_regs_p[i].y_time_set))
1853 y_ptr = &y_read_regs[i];
1854 else if (y_regs_p[i].y_array == NULL)
1855 continue;
1856 else
1857 y_ptr = &y_regs_p[i];
1858
1859 // Skip empty registers.
1860 num_lines = y_ptr->y_size;
1861 if (num_lines == 0
1862 || (num_lines == 1 && y_ptr->y_type == MCHAR
1863 && *y_ptr->y_array[0] == NUL))
1864 continue;
1865
1866 if (max_kbyte > 0)
1867 {
1868 // Skip register if there is more text than the maximum size.
1869 len = 0;
1870 for (j = 0; j < num_lines; j++)
1871 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
1872 if (len > (long)max_kbyte * 1024L)
1873 continue;
1874 }
1875
1876 switch (y_ptr->y_type)
1877 {
1878 case MLINE:
1879 type = (char_u *)"LINE";
1880 break;
1881 case MCHAR:
1882 type = (char_u *)"CHAR";
1883 break;
1884 case MBLOCK:
1885 type = (char_u *)"BLOCK";
1886 break;
1887 default:
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001888 semsg(_(e_unknown_register_type_nr), y_ptr->y_type);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001889 type = (char_u *)"LINE";
1890 break;
1891 }
1892 if (get_y_previous() == &y_regs_p[i])
1893 fprintf(fp, "\"");
1894 c = get_register_name(i);
1895 fprintf(fp, "\"%c", c);
1896 if (c == get_execreg_lastc())
1897 fprintf(fp, "@");
1898 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
1899
1900 // If max_num_lines < 0, then we save ALL the lines in the register
1901 if (max_num_lines > 0 && num_lines > max_num_lines)
1902 num_lines = max_num_lines;
1903 for (j = 0; j < num_lines; j++)
1904 {
1905 putc('\t', fp);
1906 viminfo_writestring(fp, y_ptr->y_array[j]);
1907 }
1908
1909 {
1910 int flags = 0;
1911 int remaining;
1912
1913 // New style with a bar line. Format:
1914 // |{bartype},{flags},{name},{type},
1915 // {linecount},{width},{timestamp},"line1","line2"
1916 // flags: REG_PREVIOUS - register is y_previous
1917 // REG_EXEC - used for @@
1918 if (get_y_previous() == &y_regs_p[i])
1919 flags |= REG_PREVIOUS;
1920 if (c == get_execreg_lastc())
1921 flags |= REG_EXEC;
1922 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
1923 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
1924 (long)y_ptr->y_time_set);
1925 // 11 chars for type/flags/name/type, 3 * 20 for numbers
1926 remaining = LSIZE - 71;
1927 for (j = 0; j < num_lines; j++)
1928 {
1929 putc(',', fp);
1930 --remaining;
1931 remaining = barline_writestring(fp, y_ptr->y_array[j],
1932 remaining);
1933 }
1934 putc('\n', fp);
1935 }
1936 }
1937}
1938
1939/*
1940 * Functions relating to reading/writing marks from viminfo
1941 */
1942
1943static xfmark_T *vi_namedfm = NULL;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001944static xfmark_T *vi_jumplist = NULL;
1945static int vi_jumplist_len = 0;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001946
1947 static void
1948write_one_mark(FILE *fp_out, int c, pos_T *pos)
1949{
1950 if (pos->lnum != 0)
1951 fprintf(fp_out, "\t%c\t%ld\t%d\n", c, (long)pos->lnum, (int)pos->col);
1952}
1953
1954 static void
1955write_buffer_marks(buf_T *buf, FILE *fp_out)
1956{
1957 int i;
1958 pos_T pos;
1959
1960 home_replace(NULL, buf->b_ffname, IObuff, IOSIZE, TRUE);
1961 fprintf(fp_out, "\n> ");
1962 viminfo_writestring(fp_out, IObuff);
1963
1964 // Write the last used timestamp as the lnum of the non-existing mark '*'.
1965 // Older Vims will ignore it and/or copy it.
1966 pos.lnum = (linenr_T)buf->b_last_used;
1967 pos.col = 0;
1968 write_one_mark(fp_out, '*', &pos);
1969
1970 write_one_mark(fp_out, '"', &buf->b_last_cursor);
1971 write_one_mark(fp_out, '^', &buf->b_last_insert);
1972 write_one_mark(fp_out, '.', &buf->b_last_change);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001973 // changelist positions are stored oldest first
1974 for (i = 0; i < buf->b_changelistlen; ++i)
1975 {
1976 // skip duplicates
1977 if (i == 0 || !EQUAL_POS(buf->b_changelist[i - 1],
1978 buf->b_changelist[i]))
1979 write_one_mark(fp_out, '+', &buf->b_changelist[i]);
1980 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02001981 for (i = 0; i < NMARKS; i++)
1982 write_one_mark(fp_out, 'a' + i, &buf->b_namedm[i]);
1983}
1984
1985/*
1986 * Return TRUE if marks for "buf" should not be written.
1987 */
1988 static int
1989skip_for_viminfo(buf_T *buf)
1990{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001991 return bt_terminal(buf) || removable(buf->b_ffname);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001992}
1993
1994/*
1995 * Write all the named marks for all buffers.
1996 * When "buflist" is not NULL fill it with the buffers for which marks are to
1997 * be written.
1998 */
1999 static void
2000write_viminfo_marks(FILE *fp_out, garray_T *buflist)
2001{
2002 buf_T *buf;
2003 int is_mark_set;
2004 int i;
2005 win_T *win;
2006 tabpage_T *tp;
2007
2008 // Set b_last_cursor for the all buffers that have a window.
2009 FOR_ALL_TAB_WINDOWS(tp, win)
2010 set_last_cursor(win);
2011
2012 fputs(_("\n# History of marks within files (newest to oldest):\n"), fp_out);
2013 FOR_ALL_BUFFERS(buf)
2014 {
2015 // Only write something if buffer has been loaded and at least one
2016 // mark is set.
2017 if (buf->b_marks_read)
2018 {
2019 if (buf->b_last_cursor.lnum != 0)
2020 is_mark_set = TRUE;
2021 else
2022 {
2023 is_mark_set = FALSE;
2024 for (i = 0; i < NMARKS; i++)
2025 if (buf->b_namedm[i].lnum != 0)
2026 {
2027 is_mark_set = TRUE;
2028 break;
2029 }
2030 }
2031 if (is_mark_set && buf->b_ffname != NULL
2032 && buf->b_ffname[0] != NUL
2033 && !skip_for_viminfo(buf))
2034 {
2035 if (buflist == NULL)
2036 write_buffer_marks(buf, fp_out);
2037 else if (ga_grow(buflist, 1) == OK)
2038 ((buf_T **)buflist->ga_data)[buflist->ga_len++] = buf;
2039 }
2040 }
2041 }
2042}
2043
2044 static void
2045write_one_filemark(
2046 FILE *fp,
2047 xfmark_T *fm,
2048 int c1,
2049 int c2)
2050{
2051 char_u *name;
2052
2053 if (fm->fmark.mark.lnum == 0) // not set
2054 return;
2055
2056 if (fm->fmark.fnum != 0) // there is a buffer
2057 name = buflist_nr2name(fm->fmark.fnum, TRUE, FALSE);
2058 else
2059 name = fm->fname; // use name from .viminfo
2060 if (name != NULL && *name != NUL)
2061 {
2062 fprintf(fp, "%c%c %ld %ld ", c1, c2, (long)fm->fmark.mark.lnum,
2063 (long)fm->fmark.mark.col);
2064 viminfo_writestring(fp, name);
2065
2066 // Barline: |{bartype},{name},{lnum},{col},{timestamp},{filename}
2067 // size up to filename: 8 + 3 * 20
2068 fprintf(fp, "|%d,%d,%ld,%ld,%ld,", BARTYPE_MARK, c2,
2069 (long)fm->fmark.mark.lnum, (long)fm->fmark.mark.col,
2070 (long)fm->time_set);
2071 barline_writestring(fp, name, LSIZE - 70);
2072 putc('\n', fp);
2073 }
2074
2075 if (fm->fmark.fnum != 0)
2076 vim_free(name);
2077}
2078
2079 static void
2080write_viminfo_filemarks(FILE *fp)
2081{
2082 int i;
2083 char_u *name;
2084 buf_T *buf;
2085 xfmark_T *namedfm_p = get_namedfm();
2086 xfmark_T *fm;
2087 int vi_idx;
2088 int idx;
2089
2090 if (get_viminfo_parameter('f') == 0)
2091 return;
2092
2093 fputs(_("\n# File marks:\n"), fp);
2094
2095 // Write the filemarks 'A - 'Z
2096 for (i = 0; i < NMARKS; i++)
2097 {
2098 if (vi_namedfm != NULL
Bram Moolenaar8cd6cd82019-12-27 17:33:26 +01002099 && (vi_namedfm[i].time_set > namedfm_p[i].time_set))
Bram Moolenaarc3328162019-07-23 22:15:25 +02002100 fm = &vi_namedfm[i];
2101 else
2102 fm = &namedfm_p[i];
2103 write_one_filemark(fp, fm, '\'', i + 'A');
2104 }
2105
2106 // Find a mark that is the same file and position as the cursor.
2107 // That one, or else the last one is deleted.
2108 // Move '0 to '1, '1 to '2, etc. until the matching one or '9
2109 // Set the '0 mark to current cursor position.
2110 if (curbuf->b_ffname != NULL && !skip_for_viminfo(curbuf))
2111 {
2112 name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE);
2113 for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i)
2114 if (namedfm_p[i].fmark.mark.lnum == curwin->w_cursor.lnum
2115 && (namedfm_p[i].fname == NULL
2116 ? namedfm_p[i].fmark.fnum == curbuf->b_fnum
2117 : (name != NULL
2118 && STRCMP(name, namedfm_p[i].fname) == 0)))
2119 break;
2120 vim_free(name);
2121
2122 vim_free(namedfm_p[i].fname);
2123 for ( ; i > NMARKS; --i)
2124 namedfm_p[i] = namedfm_p[i - 1];
2125 namedfm_p[NMARKS].fmark.mark = curwin->w_cursor;
2126 namedfm_p[NMARKS].fmark.fnum = curbuf->b_fnum;
2127 namedfm_p[NMARKS].fname = NULL;
2128 namedfm_p[NMARKS].time_set = vim_time();
2129 }
2130
2131 // Write the filemarks '0 - '9. Newest (highest timestamp) first.
2132 vi_idx = NMARKS;
2133 idx = NMARKS;
2134 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
2135 {
2136 xfmark_T *vi_fm = vi_namedfm != NULL ? &vi_namedfm[vi_idx] : NULL;
2137
2138 if (vi_fm != NULL
2139 && vi_fm->fmark.mark.lnum != 0
2140 && (vi_fm->time_set > namedfm_p[idx].time_set
2141 || namedfm_p[idx].fmark.mark.lnum == 0))
2142 {
2143 fm = vi_fm;
2144 ++vi_idx;
2145 }
2146 else
2147 {
2148 fm = &namedfm_p[idx++];
2149 if (vi_fm != NULL
2150 && vi_fm->fmark.mark.lnum == fm->fmark.mark.lnum
2151 && vi_fm->time_set == fm->time_set
2152 && ((vi_fm->fmark.fnum != 0
2153 && vi_fm->fmark.fnum == fm->fmark.fnum)
2154 || (vi_fm->fname != NULL
2155 && fm->fname != NULL
2156 && STRCMP(vi_fm->fname, fm->fname) == 0)))
2157 ++vi_idx; // skip duplicate
2158 }
2159 write_one_filemark(fp, fm, '\'', i - NMARKS + '0');
2160 }
2161
Bram Moolenaarc3328162019-07-23 22:15:25 +02002162 // Write the jumplist with -'
2163 fputs(_("\n# Jumplist (newest first):\n"), fp);
2164 setpcmark(); // add current cursor position
2165 cleanup_jumplist(curwin, FALSE);
2166 vi_idx = 0;
2167 idx = curwin->w_jumplistlen - 1;
2168 for (i = 0; i < JUMPLISTSIZE; ++i)
2169 {
2170 xfmark_T *vi_fm;
2171
2172 fm = idx >= 0 ? &curwin->w_jumplist[idx] : NULL;
Bram Moolenaar4ad739f2020-09-02 10:25:45 +02002173 vi_fm = (vi_jumplist != NULL && vi_idx < vi_jumplist_len)
2174 ? &vi_jumplist[vi_idx] : NULL;
Bram Moolenaarc3328162019-07-23 22:15:25 +02002175 if (fm == NULL && vi_fm == NULL)
2176 break;
2177 if (fm == NULL || (vi_fm != NULL && fm->time_set < vi_fm->time_set))
2178 {
2179 fm = vi_fm;
2180 ++vi_idx;
2181 }
2182 else
2183 --idx;
2184 if (fm->fmark.fnum == 0
2185 || ((buf = buflist_findnr(fm->fmark.fnum)) != NULL
2186 && !skip_for_viminfo(buf)))
2187 write_one_filemark(fp, fm, '-', '\'');
2188 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02002189}
2190
2191/*
2192 * Compare functions for qsort() below, that compares b_last_used.
2193 */
Bram Moolenaar52410572019-10-27 05:12:45 +01002194 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02002195buf_compare(const void *s1, const void *s2)
2196{
2197 buf_T *buf1 = *(buf_T **)s1;
2198 buf_T *buf2 = *(buf_T **)s2;
2199
2200 if (buf1->b_last_used == buf2->b_last_used)
2201 return 0;
2202 return buf1->b_last_used > buf2->b_last_used ? -1 : 1;
2203}
2204
2205/*
2206 * Handle marks in the viminfo file:
2207 * fp_out != NULL: copy marks, in time order with buffers in "buflist".
Bram Moolenaar3ff656f2021-02-10 19:22:15 +01002208 * fp_out == NULL && (flags & VIF_WANT_MARKS): read marks for curbuf
2209 * fp_out == NULL && (flags & VIF_ONLY_CURBUF): bail out after curbuf marks
Bram Moolenaarc3328162019-07-23 22:15:25 +02002210 * fp_out == NULL && (flags & VIF_GET_OLDFILES | VIF_FORCEIT): fill v:oldfiles
2211 */
2212 static void
2213copy_viminfo_marks(
2214 vir_T *virp,
2215 FILE *fp_out,
2216 garray_T *buflist,
2217 int eof,
2218 int flags)
2219{
2220 char_u *line = virp->vir_line;
2221 buf_T *buf;
2222 int num_marked_files;
2223 int load_marks;
2224 int copy_marks_out;
2225 char_u *str;
2226 int i;
2227 char_u *p;
2228 char_u *name_buf;
2229 pos_T pos;
2230#ifdef FEAT_EVAL
2231 list_T *list = NULL;
2232#endif
2233 int count = 0;
2234 int buflist_used = 0;
2235 buf_T *buflist_buf = NULL;
2236
2237 if ((name_buf = alloc(LSIZE)) == NULL)
2238 return;
2239 *name_buf = NUL;
2240
2241 if (fp_out != NULL && buflist->ga_len > 0)
2242 {
2243 // Sort the list of buffers on b_last_used.
2244 qsort(buflist->ga_data, (size_t)buflist->ga_len,
2245 sizeof(buf_T *), buf_compare);
2246 buflist_buf = ((buf_T **)buflist->ga_data)[0];
2247 }
2248
2249#ifdef FEAT_EVAL
2250 if (fp_out == NULL && (flags & (VIF_GET_OLDFILES | VIF_FORCEIT)))
2251 {
2252 list = list_alloc();
2253 if (list != NULL)
2254 set_vim_var_list(VV_OLDFILES, list);
2255 }
2256#endif
2257
2258 num_marked_files = get_viminfo_parameter('\'');
2259 while (!eof && (count < num_marked_files || fp_out == NULL))
2260 {
2261 if (line[0] != '>')
2262 {
2263 if (line[0] != '\n' && line[0] != '\r' && line[0] != '#')
2264 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002265 if (viminfo_error("E576: ", _(e_nonr_missing_gt), line))
Bram Moolenaarc3328162019-07-23 22:15:25 +02002266 break; // too many errors, return now
2267 }
2268 eof = vim_fgets(line, LSIZE, virp->vir_fd);
2269 continue; // Skip this dud line
2270 }
2271
2272 // Handle long line and translate escaped characters.
2273 // Find file name, set str to start.
2274 // Ignore leading and trailing white space.
2275 str = skipwhite(line + 1);
2276 str = viminfo_readstring(virp, (int)(str - virp->vir_line), FALSE);
2277 if (str == NULL)
2278 continue;
2279 p = str + STRLEN(str);
2280 while (p != str && (*p == NUL || vim_isspace(*p)))
2281 p--;
2282 if (*p)
2283 p++;
2284 *p = NUL;
2285
2286#ifdef FEAT_EVAL
2287 if (list != NULL)
2288 list_append_string(list, str, -1);
2289#endif
2290
2291 // If fp_out == NULL, load marks for current buffer.
2292 // If fp_out != NULL, copy marks for buffers not in buflist.
2293 load_marks = copy_marks_out = FALSE;
2294 if (fp_out == NULL)
2295 {
2296 if ((flags & VIF_WANT_MARKS) && curbuf->b_ffname != NULL)
2297 {
2298 if (*name_buf == NUL) // only need to do this once
2299 home_replace(NULL, curbuf->b_ffname, name_buf, LSIZE, TRUE);
2300 if (fnamecmp(str, name_buf) == 0)
2301 load_marks = TRUE;
2302 }
2303 }
2304 else // fp_out != NULL
2305 {
2306 // This is slow if there are many buffers!!
2307 FOR_ALL_BUFFERS(buf)
2308 if (buf->b_ffname != NULL)
2309 {
2310 home_replace(NULL, buf->b_ffname, name_buf, LSIZE, TRUE);
2311 if (fnamecmp(str, name_buf) == 0)
2312 break;
2313 }
2314
2315 // Copy marks if the buffer has not been loaded.
2316 if (buf == NULL || !buf->b_marks_read)
2317 {
2318 int did_read_line = FALSE;
2319
2320 if (buflist_buf != NULL)
2321 {
2322 // Read the next line. If it has the "*" mark compare the
2323 // time stamps. Write entries from "buflist" that are
2324 // newer.
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01002325 if (!viminfo_readline(virp) && line[0] == TAB)
Bram Moolenaarc3328162019-07-23 22:15:25 +02002326 {
2327 did_read_line = TRUE;
2328 if (line[1] == '*')
2329 {
2330 long ltime;
2331
2332 sscanf((char *)line + 2, "%ld ", &ltime);
2333 while ((time_T)ltime < buflist_buf->b_last_used)
2334 {
2335 write_buffer_marks(buflist_buf, fp_out);
2336 if (++count >= num_marked_files)
2337 break;
2338 if (++buflist_used == buflist->ga_len)
2339 {
2340 buflist_buf = NULL;
2341 break;
2342 }
2343 buflist_buf =
2344 ((buf_T **)buflist->ga_data)[buflist_used];
2345 }
2346 }
2347 else
2348 {
2349 // No timestamp, must be written by an older Vim.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002350 // Assume all remaining buffers are older than
Bram Moolenaarc3328162019-07-23 22:15:25 +02002351 // ours.
2352 while (count < num_marked_files
2353 && buflist_used < buflist->ga_len)
2354 {
2355 buflist_buf = ((buf_T **)buflist->ga_data)
2356 [buflist_used++];
2357 write_buffer_marks(buflist_buf, fp_out);
2358 ++count;
2359 }
2360 buflist_buf = NULL;
2361 }
2362
2363 if (count >= num_marked_files)
2364 {
2365 vim_free(str);
2366 break;
2367 }
2368 }
2369 }
2370
2371 fputs("\n> ", fp_out);
2372 viminfo_writestring(fp_out, str);
2373 if (did_read_line)
2374 fputs((char *)line, fp_out);
2375
2376 count++;
2377 copy_marks_out = TRUE;
2378 }
2379 }
2380 vim_free(str);
2381
2382 pos.coladd = 0;
2383 while (!(eof = viminfo_readline(virp)) && line[0] == TAB)
2384 {
2385 if (load_marks)
2386 {
2387 if (line[1] != NUL)
2388 {
2389 unsigned u;
2390
2391 sscanf((char *)line + 2, "%ld %u", &pos.lnum, &u);
2392 pos.col = u;
2393 switch (line[1])
2394 {
2395 case '"': curbuf->b_last_cursor = pos; break;
2396 case '^': curbuf->b_last_insert = pos; break;
2397 case '.': curbuf->b_last_change = pos; break;
2398 case '+':
Bram Moolenaarc3328162019-07-23 22:15:25 +02002399 // changelist positions are stored oldest
2400 // first
2401 if (curbuf->b_changelistlen == JUMPLISTSIZE)
2402 // list is full, remove oldest entry
2403 mch_memmove(curbuf->b_changelist,
2404 curbuf->b_changelist + 1,
2405 sizeof(pos_T) * (JUMPLISTSIZE - 1));
2406 else
2407 ++curbuf->b_changelistlen;
2408 curbuf->b_changelist[
2409 curbuf->b_changelistlen - 1] = pos;
Bram Moolenaarc3328162019-07-23 22:15:25 +02002410 break;
2411
2412 // Using the line number for the last-used
2413 // timestamp.
2414 case '*': curbuf->b_last_used = pos.lnum; break;
2415
2416 default: if ((i = line[1] - 'a') >= 0 && i < NMARKS)
2417 curbuf->b_namedm[i] = pos;
2418 }
2419 }
2420 }
2421 else if (copy_marks_out)
2422 fputs((char *)line, fp_out);
2423 }
2424
2425 if (load_marks)
2426 {
Bram Moolenaarc3328162019-07-23 22:15:25 +02002427 win_T *wp;
2428
2429 FOR_ALL_WINDOWS(wp)
2430 {
2431 if (wp->w_buffer == curbuf)
2432 wp->w_changelistidx = curbuf->b_changelistlen;
2433 }
Bram Moolenaar3ff656f2021-02-10 19:22:15 +01002434 if (flags & VIF_ONLY_CURBUF)
2435 break;
Bram Moolenaarc3328162019-07-23 22:15:25 +02002436 }
2437 }
2438
2439 if (fp_out != NULL)
2440 // Write any remaining entries from buflist.
2441 while (count < num_marked_files && buflist_used < buflist->ga_len)
2442 {
2443 buflist_buf = ((buf_T **)buflist->ga_data)[buflist_used++];
2444 write_buffer_marks(buflist_buf, fp_out);
2445 ++count;
2446 }
2447
2448 vim_free(name_buf);
2449}
2450
2451/*
2452 * Read marks for the current buffer from the viminfo file, when we support
2453 * buffer marks and the buffer has a name.
2454 */
2455 void
2456check_marks_read(void)
2457{
2458 if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0
2459 && curbuf->b_ffname != NULL)
Bram Moolenaar3ff656f2021-02-10 19:22:15 +01002460 read_viminfo(NULL, VIF_WANT_MARKS | VIF_ONLY_CURBUF);
Bram Moolenaarc3328162019-07-23 22:15:25 +02002461
2462 // Always set b_marks_read; needed when 'viminfo' is changed to include
2463 // the ' parameter after opening a buffer.
2464 curbuf->b_marks_read = TRUE;
2465}
2466
2467 static int
2468read_viminfo_filemark(vir_T *virp, int force)
2469{
2470 char_u *str;
2471 xfmark_T *namedfm_p = get_namedfm();
2472 xfmark_T *fm;
2473 int i;
2474
2475 // We only get here if line[0] == '\'' or '-'.
2476 // Illegal mark names are ignored (for future expansion).
2477 str = virp->vir_line + 1;
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002478 if (*str <= 127
2479 && ((*virp->vir_line == '\''
2480 && (VIM_ISDIGIT(*str) || isupper(*str)))
Bram Moolenaarc3328162019-07-23 22:15:25 +02002481 || (*virp->vir_line == '-' && *str == '\'')))
2482 {
2483 if (*str == '\'')
2484 {
Bram Moolenaarc3328162019-07-23 22:15:25 +02002485 // If the jumplist isn't full insert fmark as oldest entry
2486 if (curwin->w_jumplistlen == JUMPLISTSIZE)
2487 fm = NULL;
2488 else
2489 {
2490 for (i = curwin->w_jumplistlen; i > 0; --i)
2491 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
2492 ++curwin->w_jumplistidx;
2493 ++curwin->w_jumplistlen;
2494 fm = &curwin->w_jumplist[0];
2495 fm->fmark.mark.lnum = 0;
2496 fm->fname = NULL;
2497 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02002498 }
2499 else if (VIM_ISDIGIT(*str))
2500 fm = &namedfm_p[*str - '0' + NMARKS];
2501 else
2502 fm = &namedfm_p[*str - 'A'];
2503 if (fm != NULL && (fm->fmark.mark.lnum == 0 || force))
2504 {
2505 str = skipwhite(str + 1);
2506 fm->fmark.mark.lnum = getdigits(&str);
2507 str = skipwhite(str);
2508 fm->fmark.mark.col = getdigits(&str);
2509 fm->fmark.mark.coladd = 0;
2510 fm->fmark.fnum = 0;
2511 str = skipwhite(str);
2512 vim_free(fm->fname);
2513 fm->fname = viminfo_readstring(virp, (int)(str - virp->vir_line),
2514 FALSE);
2515 fm->time_set = 0;
2516 }
2517 }
2518 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2519}
2520
2521/*
2522 * Prepare for reading viminfo marks when writing viminfo later.
2523 */
2524 static void
2525prepare_viminfo_marks(void)
2526{
2527 vi_namedfm = ALLOC_CLEAR_MULT(xfmark_T, NMARKS + EXTRA_MARKS);
Bram Moolenaarc3328162019-07-23 22:15:25 +02002528 vi_jumplist = ALLOC_CLEAR_MULT(xfmark_T, JUMPLISTSIZE);
2529 vi_jumplist_len = 0;
Bram Moolenaarc3328162019-07-23 22:15:25 +02002530}
2531
2532 static void
2533finish_viminfo_marks(void)
2534{
2535 int i;
2536
2537 if (vi_namedfm != NULL)
2538 {
2539 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
2540 vim_free(vi_namedfm[i].fname);
2541 VIM_CLEAR(vi_namedfm);
2542 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02002543 if (vi_jumplist != NULL)
2544 {
2545 for (i = 0; i < vi_jumplist_len; ++i)
2546 vim_free(vi_jumplist[i].fname);
2547 VIM_CLEAR(vi_jumplist);
2548 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02002549}
2550
2551/*
2552 * Accept a new style mark line from the viminfo, store it when it's new.
2553 */
2554 static void
2555handle_viminfo_mark(garray_T *values, int force)
2556{
2557 bval_T *vp = (bval_T *)values->ga_data;
2558 int name;
2559 linenr_T lnum;
2560 colnr_T col;
2561 time_t timestamp;
2562 xfmark_T *fm = NULL;
2563
2564 // Check the format:
2565 // |{bartype},{name},{lnum},{col},{timestamp},{filename}
2566 if (values->ga_len < 5
2567 || vp[0].bv_type != BVAL_NR
2568 || vp[1].bv_type != BVAL_NR
2569 || vp[2].bv_type != BVAL_NR
2570 || vp[3].bv_type != BVAL_NR
2571 || vp[4].bv_type != BVAL_STRING)
2572 return;
2573
2574 name = vp[0].bv_nr;
2575 if (name != '\'' && !VIM_ISDIGIT(name) && !ASCII_ISUPPER(name))
2576 return;
2577 lnum = vp[1].bv_nr;
2578 col = vp[2].bv_nr;
2579 if (lnum <= 0 || col < 0)
2580 return;
2581 timestamp = (time_t)vp[3].bv_nr;
2582
2583 if (name == '\'')
2584 {
Bram Moolenaarc3328162019-07-23 22:15:25 +02002585 if (vi_jumplist != NULL)
2586 {
2587 if (vi_jumplist_len < JUMPLISTSIZE)
2588 fm = &vi_jumplist[vi_jumplist_len++];
2589 }
2590 else
2591 {
2592 int idx;
2593 int i;
2594
2595 // If we have a timestamp insert it in the right place.
2596 if (timestamp != 0)
2597 {
2598 for (idx = curwin->w_jumplistlen - 1; idx >= 0; --idx)
2599 if (curwin->w_jumplist[idx].time_set < timestamp)
2600 {
2601 ++idx;
2602 break;
2603 }
2604 // idx cannot be zero now
2605 if (idx < 0 && curwin->w_jumplistlen < JUMPLISTSIZE)
2606 // insert as the oldest entry
2607 idx = 0;
2608 }
2609 else if (curwin->w_jumplistlen < JUMPLISTSIZE)
2610 // insert as oldest entry
2611 idx = 0;
2612 else
2613 idx = -1;
2614
2615 if (idx >= 0)
2616 {
2617 if (curwin->w_jumplistlen == JUMPLISTSIZE)
2618 {
2619 // Drop the oldest entry.
2620 --idx;
2621 vim_free(curwin->w_jumplist[0].fname);
2622 for (i = 0; i < idx; ++i)
2623 curwin->w_jumplist[i] = curwin->w_jumplist[i + 1];
2624 }
2625 else
2626 {
2627 // Move newer entries forward.
2628 for (i = curwin->w_jumplistlen; i > idx; --i)
2629 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
2630 ++curwin->w_jumplistidx;
2631 ++curwin->w_jumplistlen;
2632 }
2633 fm = &curwin->w_jumplist[idx];
2634 fm->fmark.mark.lnum = 0;
2635 fm->fname = NULL;
2636 fm->time_set = 0;
2637 }
2638 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02002639 }
2640 else
2641 {
2642 int idx;
2643 xfmark_T *namedfm_p = get_namedfm();
2644
2645 if (VIM_ISDIGIT(name))
2646 {
2647 if (vi_namedfm != NULL)
2648 idx = name - '0' + NMARKS;
2649 else
2650 {
2651 int i;
2652
2653 // Do not use the name from the viminfo file, insert in time
2654 // order.
2655 for (idx = NMARKS; idx < NMARKS + EXTRA_MARKS; ++idx)
2656 if (namedfm_p[idx].time_set < timestamp)
2657 break;
2658 if (idx == NMARKS + EXTRA_MARKS)
2659 // All existing entries are newer.
2660 return;
2661 i = NMARKS + EXTRA_MARKS - 1;
2662
2663 vim_free(namedfm_p[i].fname);
2664 for ( ; i > idx; --i)
2665 namedfm_p[i] = namedfm_p[i - 1];
2666 namedfm_p[idx].fname = NULL;
2667 }
2668 }
2669 else
2670 idx = name - 'A';
2671 if (vi_namedfm != NULL)
2672 fm = &vi_namedfm[idx];
2673 else
2674 fm = &namedfm_p[idx];
2675 }
2676
2677 if (fm != NULL)
2678 {
2679 if (vi_namedfm != NULL || fm->fmark.mark.lnum == 0
2680 || fm->time_set < timestamp || force)
2681 {
2682 fm->fmark.mark.lnum = lnum;
2683 fm->fmark.mark.col = col;
2684 fm->fmark.mark.coladd = 0;
2685 fm->fmark.fnum = 0;
2686 vim_free(fm->fname);
2687 if (vp[4].bv_allocated)
2688 {
2689 fm->fname = vp[4].bv_string;
2690 vp[4].bv_string = NULL;
2691 }
2692 else
2693 fm->fname = vim_strsave(vp[4].bv_string);
2694 fm->time_set = timestamp;
2695 }
2696 }
2697}
2698
2699 static int
2700read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing)
2701{
2702 char_u *p = virp->vir_line + 1;
2703 int bartype;
2704 garray_T values;
2705 bval_T *vp;
2706 int i;
2707 int read_next = TRUE;
2708
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02002709 // The format is: |{bartype},{value},...
2710 // For a very long string:
2711 // |{bartype},>{length of "{text}{text2}"}
2712 // |<{text1}
2713 // |<{text2},{value}
2714 // For a long line not using a string
2715 // |{bartype},{lots of values},>
2716 // |<{value},{value}
Bram Moolenaarc3328162019-07-23 22:15:25 +02002717 if (*p == '<')
2718 {
2719 // Continuation line of an unrecognized item.
2720 if (writing)
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00002721 ga_copy_string(&virp->vir_barlines, virp->vir_line);
Bram Moolenaarc3328162019-07-23 22:15:25 +02002722 }
2723 else
2724 {
2725 ga_init2(&values, sizeof(bval_T), 20);
2726 bartype = getdigits(&p);
2727 switch (bartype)
2728 {
2729 case BARTYPE_VERSION:
2730 // Only use the version when it comes before the encoding.
2731 // If it comes later it was copied by a Vim version that
2732 // doesn't understand the version.
2733 if (!got_encoding)
2734 {
2735 read_next = barline_parse(virp, p, &values);
2736 vp = (bval_T *)values.ga_data;
2737 if (values.ga_len > 0 && vp->bv_type == BVAL_NR)
2738 virp->vir_version = vp->bv_nr;
2739 }
2740 break;
2741
2742 case BARTYPE_HISTORY:
2743 read_next = barline_parse(virp, p, &values);
2744 handle_viminfo_history(&values, writing);
2745 break;
2746
2747 case BARTYPE_REGISTER:
2748 read_next = barline_parse(virp, p, &values);
2749 handle_viminfo_register(&values, force);
2750 break;
2751
2752 case BARTYPE_MARK:
2753 read_next = barline_parse(virp, p, &values);
2754 handle_viminfo_mark(&values, force);
2755 break;
2756
2757 default:
2758 // copy unrecognized line (for future use)
2759 if (writing)
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00002760 ga_copy_string(&virp->vir_barlines, virp->vir_line);
Bram Moolenaarc3328162019-07-23 22:15:25 +02002761 }
2762 for (i = 0; i < values.ga_len; ++i)
2763 {
2764 vp = (bval_T *)values.ga_data + i;
2765 if (vp->bv_type == BVAL_STRING && vp->bv_allocated)
2766 vim_free(vp->bv_string);
Bram Moolenaar408030e2020-02-10 22:44:32 +01002767 vim_free(vp->bv_tofree);
Bram Moolenaarc3328162019-07-23 22:15:25 +02002768 }
2769 ga_clear(&values);
2770 }
2771
2772 if (read_next)
2773 return viminfo_readline(virp);
2774 return FALSE;
2775}
2776
Bram Moolenaardefa0672019-07-21 19:25:37 +02002777/*
2778 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2779 * first part of the viminfo file which contains everything but the marks that
2780 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2781 */
2782 static int
2783read_viminfo_up_to_marks(
2784 vir_T *virp,
2785 int forceit,
2786 int writing)
2787{
2788 int eof;
2789 buf_T *buf;
2790 int got_encoding = FALSE;
2791
Bram Moolenaardefa0672019-07-21 19:25:37 +02002792 prepare_viminfo_history(forceit ? 9999 : 0, writing);
Bram Moolenaardefa0672019-07-21 19:25:37 +02002793
2794 eof = viminfo_readline(virp);
2795 while (!eof && virp->vir_line[0] != '>')
2796 {
2797 switch (virp->vir_line[0])
2798 {
2799 // Characters reserved for future expansion, ignored now
2800 case '+': // "+40 /path/dir file", for running vim without args
2801 case '^': // to be defined
2802 case '<': // long line - ignored
2803 // A comment or empty line.
2804 case NUL:
2805 case '\r':
2806 case '\n':
2807 case '#':
2808 eof = viminfo_readline(virp);
2809 break;
2810 case '|':
2811 eof = read_viminfo_barline(virp, got_encoding,
2812 forceit, writing);
2813 break;
2814 case '*': // "*encoding=value"
2815 got_encoding = TRUE;
2816 eof = viminfo_encoding(virp);
2817 break;
2818 case '!': // global variable
2819#ifdef FEAT_EVAL
2820 eof = read_viminfo_varlist(virp, writing);
2821#else
2822 eof = viminfo_readline(virp);
2823#endif
2824 break;
2825 case '%': // entry for buffer list
2826 eof = read_viminfo_bufferlist(virp, writing);
2827 break;
2828 case '"':
2829 // When registers are in bar lines skip the old style register
2830 // lines.
2831 if (virp->vir_version < VIMINFO_VERSION_WITH_REGISTERS)
2832 eof = read_viminfo_register(virp, forceit);
2833 else
2834 do {
2835 eof = viminfo_readline(virp);
2836 } while (!eof && (virp->vir_line[0] == TAB
2837 || virp->vir_line[0] == '<'));
2838 break;
2839 case '/': // Search string
2840 case '&': // Substitute search string
2841 case '~': // Last search string, followed by '/' or '&'
2842 eof = read_viminfo_search_pattern(virp, forceit);
2843 break;
2844 case '$':
2845 eof = read_viminfo_sub_string(virp, forceit);
2846 break;
2847 case ':':
2848 case '?':
2849 case '=':
2850 case '@':
Bram Moolenaardefa0672019-07-21 19:25:37 +02002851 // When history is in bar lines skip the old style history
2852 // lines.
2853 if (virp->vir_version < VIMINFO_VERSION_WITH_HISTORY)
2854 eof = read_viminfo_history(virp, writing);
2855 else
Bram Moolenaardefa0672019-07-21 19:25:37 +02002856 eof = viminfo_readline(virp);
2857 break;
2858 case '-':
2859 case '\'':
2860 // When file marks are in bar lines skip the old style lines.
2861 if (virp->vir_version < VIMINFO_VERSION_WITH_MARKS)
2862 eof = read_viminfo_filemark(virp, forceit);
2863 else
2864 eof = viminfo_readline(virp);
2865 break;
2866 default:
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002867 if (viminfo_error("E575: ", _(e_illegal_starting_char),
Bram Moolenaardefa0672019-07-21 19:25:37 +02002868 virp->vir_line))
2869 eof = TRUE;
2870 else
2871 eof = viminfo_readline(virp);
2872 break;
2873 }
2874 }
2875
Bram Moolenaardefa0672019-07-21 19:25:37 +02002876 // Finish reading history items.
2877 if (!writing)
2878 finish_viminfo_history(virp);
Bram Moolenaardefa0672019-07-21 19:25:37 +02002879
2880 // Change file names to buffer numbers for fmarks.
2881 FOR_ALL_BUFFERS(buf)
2882 fmarks_check_names(buf);
2883
2884 return eof;
2885}
2886
2887/*
2888 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2889 */
2890 static void
2891do_viminfo(FILE *fp_in, FILE *fp_out, int flags)
2892{
2893 int eof = FALSE;
2894 vir_T vir;
2895 int merge = FALSE;
2896 int do_copy_marks = FALSE;
2897 garray_T buflist;
2898
2899 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2900 return;
2901 vir.vir_fd = fp_in;
2902 vir.vir_conv.vc_type = CONV_NONE;
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002903 ga_init2(&vir.vir_barlines, sizeof(char_u *), 100);
Bram Moolenaardefa0672019-07-21 19:25:37 +02002904 vir.vir_version = -1;
2905
2906 if (fp_in != NULL)
2907 {
2908 if (flags & VIF_WANT_INFO)
2909 {
2910 if (fp_out != NULL)
2911 {
2912 // Registers and marks are read and kept separate from what
2913 // this Vim is using. They are merged when writing.
2914 prepare_viminfo_registers();
2915 prepare_viminfo_marks();
2916 }
2917
2918 eof = read_viminfo_up_to_marks(&vir,
2919 flags & VIF_FORCEIT, fp_out != NULL);
2920 merge = TRUE;
2921 }
2922 else if (flags != 0)
2923 // Skip info, find start of marks
2924 while (!(eof = viminfo_readline(&vir))
2925 && vir.vir_line[0] != '>')
2926 ;
2927
Bram Moolenaar3ff656f2021-02-10 19:22:15 +01002928 do_copy_marks = (flags & (VIF_WANT_MARKS | VIF_ONLY_CURBUF
2929 | VIF_GET_OLDFILES | VIF_FORCEIT));
Bram Moolenaardefa0672019-07-21 19:25:37 +02002930 }
2931
2932 if (fp_out != NULL)
2933 {
2934 // Write the info:
2935 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
2936 VIM_VERSION_MEDIUM);
2937 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
2938 write_viminfo_version(fp_out);
2939 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
2940 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
2941 write_viminfo_search_pattern(fp_out);
2942 write_viminfo_sub_string(fp_out);
Bram Moolenaardefa0672019-07-21 19:25:37 +02002943 write_viminfo_history(fp_out, merge);
Bram Moolenaardefa0672019-07-21 19:25:37 +02002944 write_viminfo_registers(fp_out);
2945 finish_viminfo_registers();
2946#ifdef FEAT_EVAL
2947 write_viminfo_varlist(fp_out);
2948#endif
2949 write_viminfo_filemarks(fp_out);
2950 finish_viminfo_marks();
2951 write_viminfo_bufferlist(fp_out);
2952 write_viminfo_barlines(&vir, fp_out);
2953
2954 if (do_copy_marks)
2955 ga_init2(&buflist, sizeof(buf_T *), 50);
2956 write_viminfo_marks(fp_out, do_copy_marks ? &buflist : NULL);
2957 }
2958
2959 if (do_copy_marks)
2960 {
2961 copy_viminfo_marks(&vir, fp_out, &buflist, eof, flags);
2962 if (fp_out != NULL)
2963 ga_clear(&buflist);
2964 }
2965
2966 vim_free(vir.vir_line);
2967 if (vir.vir_conv.vc_type != CONV_NONE)
2968 convert_setup(&vir.vir_conv, NULL, NULL);
2969 ga_clear_strings(&vir.vir_barlines);
2970}
2971
2972/*
2973 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
2974 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
2975 */
2976 int
2977read_viminfo(
2978 char_u *file, // file name or NULL to use default name
2979 int flags) // VIF_WANT_INFO et al.
2980{
2981 FILE *fp;
2982 char_u *fname;
Bram Moolenaarb86abad2020-08-01 16:08:19 +02002983 stat_T st; // mch_stat() of existing viminfo file
Bram Moolenaardefa0672019-07-21 19:25:37 +02002984
2985 if (no_viminfo())
2986 return FAIL;
2987
2988 fname = viminfo_filename(file); // get file name in allocated buffer
2989 if (fname == NULL)
2990 return FAIL;
2991 fp = mch_fopen((char *)fname, READBIN);
2992
2993 if (p_verbose > 0)
2994 {
2995 verbose_enter();
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002996 smsg(_("Reading viminfo file \"%s\"%s%s%s%s"),
Bram Moolenaardefa0672019-07-21 19:25:37 +02002997 fname,
2998 (flags & VIF_WANT_INFO) ? _(" info") : "",
2999 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
3000 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
3001 fp == NULL ? _(" FAILED") : "");
3002 verbose_leave();
3003 }
3004
3005 vim_free(fname);
3006 if (fp == NULL)
3007 return FAIL;
Bram Moolenaarb86abad2020-08-01 16:08:19 +02003008 if (mch_fstat(fileno(fp), &st) < 0 || S_ISDIR(st.st_mode))
3009 {
3010 fclose(fp);
3011 return FAIL;
3012 }
Bram Moolenaardefa0672019-07-21 19:25:37 +02003013
3014 viminfo_errcnt = 0;
3015 do_viminfo(fp, NULL, flags);
3016
3017 fclose(fp);
3018 return OK;
3019}
3020
3021/*
3022 * Write the viminfo file. The old one is read in first so that effectively a
3023 * merge of current info and old info is done. This allows multiple vims to
3024 * run simultaneously, without losing any marks etc.
3025 * If "forceit" is TRUE, then the old file is not read in, and only internal
3026 * info is written to the file.
3027 */
3028 void
3029write_viminfo(char_u *file, int forceit)
3030{
3031 char_u *fname;
3032 FILE *fp_in = NULL; // input viminfo file, if any
3033 FILE *fp_out = NULL; // output viminfo file
3034 char_u *tempname = NULL; // name of temp viminfo file
3035 stat_T st_new; // mch_stat() of potential new file
Bram Moolenaarb86abad2020-08-01 16:08:19 +02003036 stat_T st_old; // mch_stat() of existing viminfo file
Bram Moolenaardefa0672019-07-21 19:25:37 +02003037#if defined(UNIX) || defined(VMS)
3038 mode_t umask_save;
3039#endif
3040#ifdef UNIX
3041 int shortname = FALSE; // use 8.3 file name
Bram Moolenaardefa0672019-07-21 19:25:37 +02003042#endif
3043#ifdef MSWIN
3044 int hidden = FALSE;
3045#endif
3046
3047 if (no_viminfo())
3048 return;
3049
3050 fname = viminfo_filename(file); // may set to default if NULL
3051 if (fname == NULL)
3052 return;
3053
3054 fp_in = mch_fopen((char *)fname, READBIN);
3055 if (fp_in == NULL)
3056 {
3057 int fd;
3058
3059 // if it does exist, but we can't read it, don't try writing
3060 if (mch_stat((char *)fname, &st_new) == 0)
3061 goto end;
3062
3063 // Create the new .viminfo non-accessible for others, because it may
3064 // contain text from non-accessible documents. It is up to the user to
3065 // widen access (e.g. to a group). This may also fail if there is a
3066 // race condition, then just give up.
3067 fd = mch_open((char *)fname,
3068 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
3069 if (fd < 0)
3070 goto end;
3071 fp_out = fdopen(fd, WRITEBIN);
3072 }
3073 else
3074 {
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003075 // There is an existing viminfo file. Create a temporary file to
3076 // write the new viminfo into, in the same directory as the
3077 // existing viminfo file, which will be renamed once all writing is
3078 // successful.
Bram Moolenaarb86abad2020-08-01 16:08:19 +02003079 if (mch_fstat(fileno(fp_in), &st_old) < 0
3080 || S_ISDIR(st_old.st_mode)
Bram Moolenaardefa0672019-07-21 19:25:37 +02003081#ifdef UNIX
Bram Moolenaarb86abad2020-08-01 16:08:19 +02003082 // For Unix we check the owner of the file. It's not very nice
3083 // to overwrite a user's viminfo file after a "su root", with a
3084 // viminfo file that the user can't read.
3085 || (getuid() != ROOT_UID
3086 && !(st_old.st_uid == getuid()
3087 ? (st_old.st_mode & 0200)
3088 : (st_old.st_gid == getgid()
3089 ? (st_old.st_mode & 0020)
3090 : (st_old.st_mode & 0002))))
3091#endif
3092 )
Bram Moolenaardefa0672019-07-21 19:25:37 +02003093 {
3094 int tt = msg_didany;
3095
Bram Moolenaar13608d82022-08-29 15:06:50 +01003096 // avoid a wait_return() for this message, it's annoying
Bram Moolenaarc553a212021-12-26 20:20:34 +00003097 semsg(_(e_viminfo_file_is_not_writable_str), fname);
Bram Moolenaardefa0672019-07-21 19:25:37 +02003098 msg_didany = tt;
3099 fclose(fp_in);
3100 goto end;
3101 }
Bram Moolenaardefa0672019-07-21 19:25:37 +02003102#ifdef MSWIN
3103 // Get the file attributes of the existing viminfo file.
3104 hidden = mch_ishidden(fname);
3105#endif
3106
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003107 // Make tempname, find one that does not exist yet.
3108 // Beware of a race condition: If someone logs out and all Vim
3109 // instances exit at the same time a temp file might be created between
3110 // stat() and open(). Use mch_open() with O_EXCL to avoid that.
3111 // May try twice: Once normal and once with shortname set, just in
3112 // case somebody puts his viminfo file in an 8.3 filesystem.
Bram Moolenaardefa0672019-07-21 19:25:37 +02003113 for (;;)
3114 {
3115 int next_char = 'z';
3116 char_u *wp;
3117
3118 tempname = buf_modname(
3119#ifdef UNIX
3120 shortname,
3121#else
3122 FALSE,
3123#endif
3124 fname,
3125#ifdef VMS
3126 (char_u *)"-tmp",
3127#else
3128 (char_u *)".tmp",
3129#endif
3130 FALSE);
3131 if (tempname == NULL) // out of memory
3132 break;
3133
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003134 // Try a series of names. Change one character, just before
3135 // the extension. This should also work for an 8.3
3136 // file name, when after adding the extension it still is
3137 // the same file as the original.
Bram Moolenaardefa0672019-07-21 19:25:37 +02003138 wp = tempname + STRLEN(tempname) - 5;
3139 if (wp < gettail(tempname)) // empty file name?
3140 wp = gettail(tempname);
3141 for (;;)
3142 {
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003143 // Check if tempfile already exists. Never overwrite an
3144 // existing file!
Bram Moolenaardefa0672019-07-21 19:25:37 +02003145 if (mch_stat((char *)tempname, &st_new) == 0)
3146 {
3147#ifdef UNIX
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003148 // Check if tempfile is same as original file. May happen
3149 // when modname() gave the same file back. E.g. silly
3150 // link, or file name-length reached. Try again with
3151 // shortname set.
Bram Moolenaardefa0672019-07-21 19:25:37 +02003152 if (!shortname && st_new.st_dev == st_old.st_dev
3153 && st_new.st_ino == st_old.st_ino)
3154 {
3155 VIM_CLEAR(tempname);
3156 shortname = TRUE;
3157 break;
3158 }
3159#endif
3160 }
3161 else
3162 {
3163 // Try creating the file exclusively. This may fail if
3164 // another Vim tries to do it at the same time.
3165#ifdef VMS
3166 // fdopen() fails for some reason
3167 umask_save = umask(077);
3168 fp_out = mch_fopen((char *)tempname, WRITEBIN);
3169 (void)umask(umask_save);
3170#else
3171 int fd;
3172
3173 // Use mch_open() to be able to use O_NOFOLLOW and set file
3174 // protection:
3175 // Unix: same as original file, but strip s-bit. Reset
3176 // umask to avoid it getting in the way.
3177 // Others: r&w for user only.
3178# ifdef UNIX
3179 umask_save = umask(0);
3180 fd = mch_open((char *)tempname,
3181 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
3182 (int)((st_old.st_mode & 0777) | 0600));
3183 (void)umask(umask_save);
3184# else
3185 fd = mch_open((char *)tempname,
3186 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
3187# endif
3188 if (fd < 0)
3189 {
3190 fp_out = NULL;
3191# ifdef EEXIST
3192 // Avoid trying lots of names while the problem is lack
3193 // of permission, only retry if the file already
3194 // exists.
3195 if (errno != EEXIST)
3196 break;
3197# endif
3198 }
3199 else
3200 fp_out = fdopen(fd, WRITEBIN);
3201#endif // VMS
3202 if (fp_out != NULL)
3203 break;
3204 }
3205
3206 // Assume file exists, try again with another name.
3207 if (next_char == 'a' - 1)
3208 {
3209 // They all exist? Must be something wrong! Don't write
3210 // the viminfo file then.
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003211 semsg(_(e_too_many_viminfo_temp_files_like_str), tempname);
Bram Moolenaardefa0672019-07-21 19:25:37 +02003212 break;
3213 }
3214 *wp = next_char;
3215 --next_char;
3216 }
3217
3218 if (tempname != NULL)
3219 break;
3220 // continue if shortname was set
3221 }
3222
3223#if defined(UNIX) && defined(HAVE_FCHOWN)
3224 if (tempname != NULL && fp_out != NULL)
3225 {
3226 stat_T tmp_st;
3227
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003228 // Make sure the original owner can read/write the tempfile and
3229 // otherwise preserve permissions, making sure the group matches.
Bram Moolenaardefa0672019-07-21 19:25:37 +02003230 if (mch_stat((char *)tempname, &tmp_st) >= 0)
3231 {
3232 if (st_old.st_uid != tmp_st.st_uid)
3233 // Changing the owner might fail, in which case the
Bram Moolenaar32aa1022019-11-02 22:54:41 +01003234 // file will now be owned by the current user, oh well.
Bram Moolenaardefa0672019-07-21 19:25:37 +02003235 vim_ignored = fchown(fileno(fp_out), st_old.st_uid, -1);
3236 if (st_old.st_gid != tmp_st.st_gid
3237 && fchown(fileno(fp_out), -1, st_old.st_gid) == -1)
3238 // can't set the group to what it should be, remove
3239 // group permissions
3240 (void)mch_setperm(tempname, 0600);
3241 }
3242 else
3243 // can't stat the file, set conservative permissions
3244 (void)mch_setperm(tempname, 0600);
3245 }
3246#endif
3247 }
3248
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003249 // Check if the new viminfo file can be written to.
Bram Moolenaardefa0672019-07-21 19:25:37 +02003250 if (fp_out == NULL)
3251 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00003252 semsg(_(e_cant_write_viminfo_file_str),
Bram Moolenaardefa0672019-07-21 19:25:37 +02003253 (fp_in == NULL || tempname == NULL) ? fname : tempname);
3254 if (fp_in != NULL)
3255 fclose(fp_in);
3256 goto end;
3257 }
3258
3259 if (p_verbose > 0)
3260 {
3261 verbose_enter();
3262 smsg(_("Writing viminfo file \"%s\""), fname);
3263 verbose_leave();
3264 }
3265
3266 viminfo_errcnt = 0;
3267 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
3268
3269 if (fclose(fp_out) == EOF)
3270 ++viminfo_errcnt;
3271
3272 if (fp_in != NULL)
3273 {
3274 fclose(fp_in);
3275
3276 // In case of an error keep the original viminfo file. Otherwise
3277 // rename the newly written file. Give an error if that fails.
3278 if (viminfo_errcnt == 0)
3279 {
3280 if (vim_rename(tempname, fname) == -1)
3281 {
3282 ++viminfo_errcnt;
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003283 semsg(_(e_cant_rename_viminfo_file_to_str), fname);
Bram Moolenaardefa0672019-07-21 19:25:37 +02003284 }
3285# ifdef MSWIN
3286 // If the viminfo file was hidden then also hide the new file.
3287 else if (hidden)
3288 mch_hide(fname);
3289# endif
3290 }
3291 if (viminfo_errcnt > 0)
3292 mch_remove(tempname);
3293 }
3294
3295end:
3296 vim_free(fname);
3297 vim_free(tempname);
3298}
3299
3300/*
Bram Moolenaardefa0672019-07-21 19:25:37 +02003301 * ":rviminfo" and ":wviminfo".
3302 */
3303 void
3304ex_viminfo(
3305 exarg_T *eap)
3306{
3307 char_u *save_viminfo;
3308
3309 save_viminfo = p_viminfo;
3310 if (*p_viminfo == NUL)
3311 p_viminfo = (char_u *)"'100";
3312 if (eap->cmdidx == CMD_rviminfo)
3313 {
3314 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
3315 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003316 emsg(_(e_cannot_open_viminfo_file_for_reading));
Bram Moolenaardefa0672019-07-21 19:25:37 +02003317 }
3318 else
3319 write_viminfo(eap->arg, eap->forceit);
3320 p_viminfo = save_viminfo;
3321}
3322
Bram Moolenaardefa0672019-07-21 19:25:37 +02003323#endif // FEAT_VIMINFO