blob: 489cb2db0dc569aa46f072424baa01df3f193763 [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 {
John Marriottfff01322025-06-18 18:15:31 +0200102 size_t len;
103
Bram Moolenaardefa0672019-07-21 19:25:37 +0200104 if (*p_viminfofile != NUL)
105 file = p_viminfofile;
106 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL)
107 {
108#ifdef VIMINFO_FILE2
109# ifdef VMS
110 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL)
111# else
112# ifdef MSWIN
113 // Use $VIM only if $HOME is the default "C:/".
114 if (STRCMP(vim_getenv((char_u *)"HOME", NULL), "C:/") == 0
115 && mch_getenv((char_u *)"HOME") == NULL)
116# else
117 if (mch_getenv((char_u *)"HOME") == NULL)
118# endif
119# endif
120 {
121 // don't use $VIM when not available.
122 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL);
123 if (STRCMP("$VIM", NameBuff) != 0) // $VIM was expanded
124 file = (char_u *)VIMINFO_FILE2;
125 else
126 file = (char_u *)VIMINFO_FILE;
127 }
128 else
129#endif
130 file = (char_u *)VIMINFO_FILE;
131 }
John Marriottfff01322025-06-18 18:15:31 +0200132 len = expand_env(file, NameBuff, MAXPATHL);
Bram Moolenaardefa0672019-07-21 19:25:37 +0200133 file = NameBuff;
John Marriottfff01322025-06-18 18:15:31 +0200134
135 return vim_strnsave(file, len);
Bram Moolenaardefa0672019-07-21 19:25:37 +0200136 }
John Marriottfff01322025-06-18 18:15:31 +0200137
Bram Moolenaardefa0672019-07-21 19:25:37 +0200138 return vim_strsave(file);
139}
140
Bram Moolenaarc3328162019-07-23 22:15:25 +0200141/*
142 * write string to viminfo file
143 * - replace CTRL-V with CTRL-V CTRL-V
144 * - replace '\n' with CTRL-V 'n'
145 * - add a '\n' at the end
146 *
147 * For a long line:
148 * - write " CTRL-V <length> \n " in first line
149 * - write " < <string> \n " in second line
150 */
151 static void
152viminfo_writestring(FILE *fd, char_u *p)
153{
154 int c;
155 char_u *s;
156 int len = 0;
157
158 for (s = p; *s != NUL; ++s)
159 {
160 if (*s == Ctrl_V || *s == '\n')
161 ++len;
162 ++len;
163 }
164
165 // If the string will be too long, write its length and put it in the next
166 // line. Take into account that some room is needed for what comes before
167 // the string (e.g., variable name). Add something to the length for the
168 // '<', NL and trailing NUL.
169 if (len > LSIZE / 2)
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000170 fprintf(fd, "\026%d\n<", len + 3);
Bram Moolenaarc3328162019-07-23 22:15:25 +0200171
172 while ((c = *p++) != NUL)
173 {
174 if (c == Ctrl_V || c == '\n')
175 {
176 putc(Ctrl_V, fd);
177 if (c == '\n')
178 c = 'n';
179 }
180 putc(c, fd);
181 }
182 putc('\n', fd);
183}
184
185/*
186 * Write a string in quotes that barline_parse() can read back.
187 * Breaks the line in less than LSIZE pieces when needed.
188 * Returns remaining characters in the line.
189 */
190 static int
191barline_writestring(FILE *fd, char_u *s, int remaining_start)
192{
193 char_u *p;
194 int remaining = remaining_start;
195 int len = 2;
196
197 // Count the number of characters produced, including quotes.
198 for (p = s; *p != NUL; ++p)
199 {
200 if (*p == NL)
201 len += 2;
202 else if (*p == '"' || *p == '\\')
203 len += 2;
204 else
205 ++len;
206 }
207 if (len > remaining - 2)
208 {
209 fprintf(fd, ">%d\n|<", len);
210 remaining = LSIZE - 20;
211 }
212
213 putc('"', fd);
214 for (p = s; *p != NUL; ++p)
215 {
216 if (*p == NL)
217 {
218 putc('\\', fd);
219 putc('n', fd);
220 --remaining;
221 }
222 else if (*p == '"' || *p == '\\')
223 {
224 putc('\\', fd);
225 putc(*p, fd);
226 --remaining;
227 }
228 else
229 putc(*p, fd);
230 --remaining;
231
232 if (remaining < 3)
233 {
234 putc('\n', fd);
235 putc('|', fd);
236 putc('<', fd);
237 // Leave enough space for another continuation.
238 remaining = LSIZE - 20;
239 }
240 }
241 putc('"', fd);
242 return remaining - 2;
243}
244
245/*
246 * Check string read from viminfo file.
247 * Remove '\n' at the end of the line.
248 * - replace CTRL-V CTRL-V with CTRL-V
249 * - replace CTRL-V 'n' with '\n'
250 *
251 * Check for a long line as written by viminfo_writestring().
252 *
253 * Return the string in allocated memory (NULL when out of memory).
254 */
255 static char_u *
256viminfo_readstring(
257 vir_T *virp,
258 int off, // offset for virp->vir_line
Dominique Pellé0268ff32024-07-28 21:12:20 +0200259 int convert) // convert the string
Bram Moolenaarc3328162019-07-23 22:15:25 +0200260{
Bram Moolenaared7cb2d2021-08-11 17:13:54 +0200261 char_u *retval = NULL;
Bram Moolenaarc3328162019-07-23 22:15:25 +0200262 char_u *s, *d;
263 long len;
264
265 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
266 {
267 len = atol((char *)virp->vir_line + off + 1);
Bram Moolenaared7cb2d2021-08-11 17:13:54 +0200268 if (len > 0 && len < 1000000)
269 retval = lalloc(len, TRUE);
Bram Moolenaarc3328162019-07-23 22:15:25 +0200270 if (retval == NULL)
271 {
Bram Moolenaared7cb2d2021-08-11 17:13:54 +0200272 // Invalid length, line too long, out of memory? Skip next line.
Bram Moolenaarc3328162019-07-23 22:15:25 +0200273 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd);
274 return NULL;
275 }
276 (void)vim_fgets(retval, (int)len, virp->vir_fd);
277 s = retval + 1; // Skip the leading '<'
278 }
279 else
280 {
281 retval = vim_strsave(virp->vir_line + off);
282 if (retval == NULL)
283 return NULL;
284 s = retval;
285 }
286
287 // Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place.
288 d = retval;
289 while (*s != NUL && *s != '\n')
290 {
291 if (s[0] == Ctrl_V && s[1] != NUL)
292 {
293 if (s[1] == 'n')
294 *d++ = '\n';
295 else
296 *d++ = Ctrl_V;
297 s += 2;
298 }
299 else
300 *d++ = *s++;
301 }
302 *d = NUL;
303
304 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL)
305 {
306 d = string_convert(&virp->vir_conv, retval, NULL);
307 if (d != NULL)
308 {
309 vim_free(retval);
310 retval = d;
311 }
312 }
313
314 return retval;
315}
316
317/*
318 * Read a line from the viminfo file.
319 * Returns TRUE for end-of-file;
320 */
321 static int
322viminfo_readline(vir_T *virp)
323{
324 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
325}
326
Bram Moolenaardefa0672019-07-21 19:25:37 +0200327 static int
328read_viminfo_bufferlist(
329 vir_T *virp,
330 int writing)
331{
332 char_u *tab;
333 linenr_T lnum;
334 colnr_T col;
335 buf_T *buf;
336 char_u *sfname;
337 char_u *xline;
338
339 // Handle long line and escaped characters.
340 xline = viminfo_readstring(virp, 1, FALSE);
341
342 // don't read in if there are files on the command-line or if writing:
343 if (xline != NULL && !writing && ARGCOUNT == 0
344 && find_viminfo_parameter('%') != NULL)
345 {
346 // Format is: <fname> Tab <lnum> Tab <col>.
347 // Watch out for a Tab in the file name, work from the end.
348 lnum = 0;
349 col = 0;
350 tab = vim_strrchr(xline, '\t');
351 if (tab != NULL)
352 {
353 *tab++ = '\0';
354 col = (colnr_T)atoi((char *)tab);
355 tab = vim_strrchr(xline, '\t');
356 if (tab != NULL)
357 {
358 *tab++ = '\0';
359 lnum = atol((char *)tab);
360 }
361 }
362
363 // Expand "~/" in the file name at "line + 1" to a full path.
364 // Then try shortening it by comparing with the current directory
365 expand_env(xline, NameBuff, MAXPATHL);
366 sfname = shorten_fname1(NameBuff);
367
368 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
369 if (buf != NULL) // just in case...
370 {
371 buf->b_last_cursor.lnum = lnum;
372 buf->b_last_cursor.col = col;
373 buflist_setfpos(buf, curwin, lnum, col, FALSE);
374 }
375 }
376 vim_free(xline);
377
378 return viminfo_readline(virp);
379}
380
Bram Moolenaarc3328162019-07-23 22:15:25 +0200381/*
382 * Return TRUE if "name" is on removable media (depending on 'viminfo').
383 */
384 static int
385removable(char_u *name)
386{
387 char_u *p;
388 char_u part[51];
389 int retval = FALSE;
390 size_t n;
391
392 name = home_replace_save(NULL, name);
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000393 if (name == NULL)
394 return FALSE;
395 for (p = p_viminfo; *p; )
Bram Moolenaarc3328162019-07-23 22:15:25 +0200396 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000397 copy_option_part(&p, part, 51, ", ");
398 if (part[0] == 'r')
Bram Moolenaarc3328162019-07-23 22:15:25 +0200399 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000400 n = STRLEN(part + 1);
401 if (MB_STRNICMP(part + 1, name, n) == 0)
Bram Moolenaarc3328162019-07-23 22:15:25 +0200402 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000403 retval = TRUE;
404 break;
Bram Moolenaarc3328162019-07-23 22:15:25 +0200405 }
406 }
Bram Moolenaarc3328162019-07-23 22:15:25 +0200407 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000408 vim_free(name);
Bram Moolenaarc3328162019-07-23 22:15:25 +0200409 return retval;
410}
411
Bram Moolenaardefa0672019-07-21 19:25:37 +0200412 static void
413write_viminfo_bufferlist(FILE *fp)
414{
415 buf_T *buf;
416 win_T *win;
417 tabpage_T *tp;
418 char_u *line;
419 int max_buffers;
420
421 if (find_viminfo_parameter('%') == NULL)
422 return;
423
424 // Without a number -1 is returned: do all buffers.
425 max_buffers = get_viminfo_parameter('%');
426
427 // Allocate room for the file name, lnum and col.
428#define LINE_BUF_LEN (MAXPATHL + 40)
429 line = alloc(LINE_BUF_LEN);
430 if (line == NULL)
431 return;
432
433 FOR_ALL_TAB_WINDOWS(tp, win)
434 set_last_cursor(win);
435
436 fputs(_("\n# Buffer list:\n"), fp);
437 FOR_ALL_BUFFERS(buf)
438 {
439 if (buf->b_fname == NULL
440 || !buf->b_p_bl
Bram Moolenaardefa0672019-07-21 19:25:37 +0200441 || bt_quickfix(buf)
Bram Moolenaardefa0672019-07-21 19:25:37 +0200442 || bt_terminal(buf)
Bram Moolenaardefa0672019-07-21 19:25:37 +0200443 || removable(buf->b_ffname))
444 continue;
445
446 if (max_buffers-- == 0)
447 break;
448 putc('%', fp);
449 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
450 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
451 (long)buf->b_last_cursor.lnum,
452 buf->b_last_cursor.col);
453 viminfo_writestring(fp, line);
454 }
455 vim_free(line);
456}
457
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200458/*
459 * Buffers for history read from a viminfo file. Only valid while reading.
460 */
461static histentry_T *viminfo_history[HIST_COUNT] =
462 {NULL, NULL, NULL, NULL, NULL};
463static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
464static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
465static int viminfo_add_at_front = FALSE;
466
467/*
468 * Translate a history type number to the associated character.
469 */
470 static int
471hist_type2char(
472 int type,
473 int use_question) // use '?' instead of '/'
474{
475 if (type == HIST_CMD)
476 return ':';
477 if (type == HIST_SEARCH)
478 {
479 if (use_question)
480 return '?';
481 else
482 return '/';
483 }
484 if (type == HIST_EXPR)
485 return '=';
486 return '@';
487}
488
489/*
490 * Prepare for reading the history from the viminfo file.
491 * This allocates history arrays to store the read history lines.
492 */
493 static void
494prepare_viminfo_history(int asklen, int writing)
495{
496 int i;
497 int num;
498 int type;
499 int len;
Bram Moolenaar26b654a2019-07-22 20:50:17 +0200500 int hislen;
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200501
502 init_history();
Bram Moolenaar26b654a2019-07-22 20:50:17 +0200503 hislen = get_hislen();
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200504 viminfo_add_at_front = (asklen != 0 && !writing);
505 if (asklen > hislen)
506 asklen = hislen;
507
508 for (type = 0; type < HIST_COUNT; ++type)
509 {
510 histentry_T *histentry = get_histentry(type);
511
512 // Count the number of empty spaces in the history list. Entries read
513 // from viminfo previously are also considered empty. If there are
514 // more spaces available than we request, then fill them up.
515 for (i = 0, num = 0; i < hislen; i++)
516 if (histentry[i].hisstr == NULL || histentry[i].viminfo)
517 num++;
518 len = asklen;
519 if (num > len)
520 len = num;
521 if (len <= 0)
522 viminfo_history[type] = NULL;
523 else
524 viminfo_history[type] = LALLOC_MULT(histentry_T, len);
525 if (viminfo_history[type] == NULL)
526 len = 0;
527 viminfo_hislen[type] = len;
528 viminfo_hisidx[type] = 0;
529 }
530}
531
532/*
533 * Accept a line from the viminfo, store it in the history array when it's
534 * new.
535 */
536 static int
537read_viminfo_history(vir_T *virp, int writing)
538{
539 int type;
540 long_u len;
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000541 char_u *val = NULL;
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200542 char_u *p;
543
544 type = hist_char2type(virp->vir_line[0]);
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000545 if (viminfo_hisidx[type] >= viminfo_hislen[type])
546 goto done;
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200547
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000548 val = viminfo_readstring(virp, 1, TRUE);
549 if (val == NULL || *val == NUL)
550 goto done;
551
552 int sep = (*val == ' ' ? NUL : *val);
553
554 if (in_history(type, val + (type == HIST_SEARCH), viminfo_add_at_front,
555 sep, writing))
556 goto done;
557
558 // Need to re-allocate to append the separator byte.
559 len = STRLEN(val);
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000560 if (type == HIST_SEARCH)
561 {
John Marriott8df07d02024-10-21 22:37:07 +0200562 p = alloc((size_t)len + 1); // +1 for the NUL. val already
563 // includes the separator.
564 if (p == NULL)
565 goto done;
566
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000567 // Search entry: Move the separator from the first
568 // column to after the NUL.
569 mch_memmove(p, val + 1, (size_t)len);
570 p[len] = sep;
John Marriott8df07d02024-10-21 22:37:07 +0200571 --len; // take into account the shortened string
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200572 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000573 else
574 {
John Marriott8df07d02024-10-21 22:37:07 +0200575 p = alloc((size_t)len + 2); // +1 for NUL and +1 for separator
576 if (p == NULL)
577 goto done;
578
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000579 // Not a search entry: No separator in the viminfo
580 // file, add a NUL separator.
John Marriott8df07d02024-10-21 22:37:07 +0200581 mch_memmove(p, val, (size_t)len + 1); // +1 to include the NUL
582 p[len + 1] = NUL; // put the separator *after* the string's NUL
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000583 }
584 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
John Marriott8df07d02024-10-21 22:37:07 +0200585 viminfo_history[type][viminfo_hisidx[type]].hisstrlen = (size_t)len;
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000586 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
587 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
588 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
589 viminfo_hisidx[type]++;
590
591done:
592 vim_free(val);
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200593 return viminfo_readline(virp);
594}
595
596/*
597 * Accept a new style history line from the viminfo, store it in the history
598 * array when it's new.
599 */
600 static void
601handle_viminfo_history(
602 garray_T *values,
603 int writing)
604{
605 int type;
606 long_u len;
607 char_u *val;
608 char_u *p;
609 bval_T *vp = (bval_T *)values->ga_data;
610
611 // Check the format:
612 // |{bartype},{histtype},{timestamp},{separator},"text"
613 if (values->ga_len < 4
614 || vp[0].bv_type != BVAL_NR
615 || vp[1].bv_type != BVAL_NR
616 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
617 || vp[3].bv_type != BVAL_STRING)
618 return;
619
620 type = vp[0].bv_nr;
621 if (type >= HIST_COUNT)
622 return;
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000623
624 if (viminfo_hisidx[type] >= viminfo_hislen[type])
625 return;
626
627 val = vp[3].bv_string;
628 if (val == NULL || *val == NUL)
629 return;
630
631 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
632 ? vp[2].bv_nr : NUL;
633 int idx;
634 int overwrite = FALSE;
635
636 if (in_history(type, val, viminfo_add_at_front, sep, writing))
637 return;
638
639 // If lines were written by an older Vim we need to avoid
640 // getting duplicates. See if the entry already exists.
641 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200642 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000643 p = viminfo_history[type][idx].hisstr;
John Marriott8df07d02024-10-21 22:37:07 +0200644 len = viminfo_history[type][idx].hisstrlen;
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000645 if (STRCMP(val, p) == 0
John Marriott8df07d02024-10-21 22:37:07 +0200646 && (type != HIST_SEARCH || sep == p[len + 1]))
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200647 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000648 overwrite = TRUE;
649 break;
650 }
651 }
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200652
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000653 if (!overwrite)
654 {
655 // Need to re-allocate to append the separator byte.
656 len = vp[3].bv_len;
657 p = alloc(len + 2);
658 }
659 else
660 len = 0; // for picky compilers
661 if (p != NULL)
662 {
663 viminfo_history[type][idx].time_set = vp[1].bv_nr;
664 if (!overwrite)
665 {
666 mch_memmove(p, val, (size_t)len + 1);
667 // Put the separator after the NUL.
668 p[len + 1] = sep;
669 viminfo_history[type][idx].hisstr = p;
John Marriott8df07d02024-10-21 22:37:07 +0200670 viminfo_history[type][idx].hisstrlen = (size_t)len;
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000671 viminfo_history[type][idx].hisnum = 0;
672 viminfo_history[type][idx].viminfo = TRUE;
673 viminfo_hisidx[type]++;
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200674 }
675 }
676}
677
678/*
679 * Concatenate history lines from viminfo after the lines typed in this Vim.
680 */
681 static void
682concat_history(int type)
683{
684 int idx;
685 int i;
686 int hislen = get_hislen();
687 histentry_T *histentry = get_histentry(type);
688 int *hisidx = get_hisidx(type);
689 int *hisnum = get_hisnum(type);
690
691 idx = *hisidx + viminfo_hisidx[type];
692 if (idx >= hislen)
693 idx -= hislen;
694 else if (idx < 0)
695 idx = hislen - 1;
696 if (viminfo_add_at_front)
697 *hisidx = idx;
698 else
699 {
700 if (*hisidx == -1)
701 *hisidx = hislen - 1;
702 do
703 {
704 if (histentry[idx].hisstr != NULL || histentry[idx].viminfo)
705 break;
706 if (++idx == hislen)
707 idx = 0;
708 } while (idx != *hisidx);
709 if (idx != *hisidx && --idx < 0)
710 idx = hislen - 1;
711 }
712 for (i = 0; i < viminfo_hisidx[type]; i++)
713 {
714 vim_free(histentry[idx].hisstr);
715 histentry[idx].hisstr = viminfo_history[type][i].hisstr;
John Marriott8df07d02024-10-21 22:37:07 +0200716 histentry[idx].hisstrlen = viminfo_history[type][i].hisstrlen;
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200717 histentry[idx].viminfo = TRUE;
718 histentry[idx].time_set = viminfo_history[type][i].time_set;
719 if (--idx < 0)
720 idx = hislen - 1;
721 }
722 idx += 1;
723 idx %= hislen;
724 for (i = 0; i < viminfo_hisidx[type]; i++)
725 {
726 histentry[idx++].hisnum = ++*hisnum;
727 idx %= hislen;
728 }
729}
730
731 static int
732sort_hist(const void *s1, const void *s2)
733{
734 histentry_T *p1 = *(histentry_T **)s1;
735 histentry_T *p2 = *(histentry_T **)s2;
736
737 if (p1->time_set < p2->time_set) return -1;
738 if (p1->time_set > p2->time_set) return 1;
739 return 0;
740}
741
742/*
743 * Merge history lines from viminfo and lines typed in this Vim based on the
744 * timestamp;
745 */
746 static void
747merge_history(int type)
748{
749 int max_len;
750 histentry_T **tot_hist;
751 histentry_T *new_hist;
752 int i;
753 int len;
754 int hislen = get_hislen();
755 histentry_T *histentry = get_histentry(type);
756 int *hisidx = get_hisidx(type);
757 int *hisnum = get_hisnum(type);
758
759 // Make one long list with all entries.
760 max_len = hislen + viminfo_hisidx[type];
761 tot_hist = ALLOC_MULT(histentry_T *, max_len);
Bram Moolenaar26b654a2019-07-22 20:50:17 +0200762 new_hist = ALLOC_MULT(histentry_T, hislen);
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200763 if (tot_hist == NULL || new_hist == NULL)
764 {
765 vim_free(tot_hist);
766 vim_free(new_hist);
767 return;
768 }
769 for (i = 0; i < viminfo_hisidx[type]; i++)
770 tot_hist[i] = &viminfo_history[type][i];
771 len = i;
772 for (i = 0; i < hislen; i++)
773 if (histentry[i].hisstr != NULL)
774 tot_hist[len++] = &histentry[i];
775
776 // Sort the list on timestamp.
777 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
778
779 // Keep the newest ones.
780 for (i = 0; i < hislen; i++)
781 {
782 if (i < len)
783 {
784 new_hist[i] = *tot_hist[i];
785 tot_hist[i]->hisstr = NULL;
John Marriott8df07d02024-10-21 22:37:07 +0200786 tot_hist[i]->hisstrlen = 0;
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200787 if (new_hist[i].hisnum == 0)
788 new_hist[i].hisnum = ++*hisnum;
789 }
790 else
791 clear_hist_entry(&new_hist[i]);
792 }
793 *hisidx = (i < len ? i : len) - 1;
794
795 // Free what is not kept.
796 for (i = 0; i < viminfo_hisidx[type]; i++)
John Marriott8df07d02024-10-21 22:37:07 +0200797 {
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200798 vim_free(viminfo_history[type][i].hisstr);
John Marriott8df07d02024-10-21 22:37:07 +0200799 viminfo_history[type][i].hisstrlen = 0;
800 }
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200801 for (i = 0; i < hislen; i++)
John Marriott8df07d02024-10-21 22:37:07 +0200802 {
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200803 vim_free(histentry[i].hisstr);
John Marriott8df07d02024-10-21 22:37:07 +0200804 histentry[i].hisstrlen = 0;
805 }
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200806 vim_free(histentry);
807 set_histentry(type, new_hist);
808 vim_free(tot_hist);
809}
810
811/*
812 * Finish reading history lines from viminfo. Not used when writing viminfo.
813 */
814 static void
815finish_viminfo_history(vir_T *virp)
816{
817 int type;
818 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
819
820 for (type = 0; type < HIST_COUNT; ++type)
821 {
822 if (get_histentry(type) == NULL)
823 continue;
824
825 if (merge)
826 merge_history(type);
827 else
828 concat_history(type);
829
830 VIM_CLEAR(viminfo_history[type]);
831 viminfo_hisidx[type] = 0;
832 }
833}
834
835/*
836 * Write history to viminfo file in "fp".
837 * When "merge" is TRUE merge history lines with a previously read viminfo
838 * file, data is in viminfo_history[].
839 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
840 */
841 static void
842write_viminfo_history(FILE *fp, int merge)
843{
844 int i;
845 int type;
846 int num_saved;
847 int round;
848 int hislen;
849
850 init_history();
851 hislen = get_hislen();
852 if (hislen == 0)
853 return;
854 for (type = 0; type < HIST_COUNT; ++type)
855 {
856 histentry_T *histentry = get_histentry(type);
857 int *hisidx = get_hisidx(type);
858
859 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
860 if (num_saved == 0)
861 continue;
862 if (num_saved < 0) // Use default
863 num_saved = hislen;
864 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
865 type == HIST_CMD ? _("Command Line") :
866 type == HIST_SEARCH ? _("Search String") :
867 type == HIST_EXPR ? _("Expression") :
868 type == HIST_INPUT ? _("Input Line") :
869 _("Debug Line"));
870 if (num_saved > hislen)
871 num_saved = hislen;
872
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200873 // Merge typed and viminfo history:
874 // round 1: history of typed commands.
875 // round 2: history from recently read viminfo.
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200876 for (round = 1; round <= 2; ++round)
877 {
878 if (round == 1)
879 // start at newest entry, somewhere in the list
880 i = *hisidx;
881 else if (viminfo_hisidx[type] > 0)
882 // start at newest entry, first in the list
883 i = 0;
884 else
885 // empty list
886 i = -1;
887 if (i >= 0)
888 while (num_saved > 0
889 && !(round == 2 && i >= viminfo_hisidx[type]))
890 {
891 char_u *p;
John Marriott8df07d02024-10-21 22:37:07 +0200892 size_t plen;
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200893 time_t timestamp;
894 int c = NUL;
895
896 if (round == 1)
897 {
898 p = histentry[i].hisstr;
John Marriott8df07d02024-10-21 22:37:07 +0200899 plen = histentry[i].hisstrlen;
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200900 timestamp = histentry[i].time_set;
901 }
902 else
903 {
John Marriott8df07d02024-10-21 22:37:07 +0200904 if (viminfo_history[type] == NULL)
905 {
906 p = NULL;
907 plen = 0;
908 timestamp = 0;
909 }
910 else
911 {
912 p = viminfo_history[type][i].hisstr;
913 plen = viminfo_history[type][i].hisstrlen;
914 timestamp = viminfo_history[type][i].time_set;
915 }
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200916 }
917
918 if (p != NULL && (round == 2
919 || !merge
920 || !histentry[i].viminfo))
921 {
922 --num_saved;
923 fputc(hist_type2char(type, TRUE), fp);
924 // For the search history: put the separator in the
925 // second column; use a space if there isn't one.
926 if (type == HIST_SEARCH)
927 {
John Marriott8df07d02024-10-21 22:37:07 +0200928 c = p[plen + 1];
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200929 putc(c == NUL ? ' ' : c, fp);
930 }
931 viminfo_writestring(fp, p);
932
933 {
934 char cbuf[NUMBUFLEN];
935
936 // New style history with a bar line. Format:
937 // |{bartype},{histtype},{timestamp},{separator},"text"
938 if (c == NUL)
939 cbuf[0] = NUL;
940 else
941 sprintf(cbuf, "%d", c);
942 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
943 type, (long)timestamp, cbuf);
944 barline_writestring(fp, p, LSIZE - 20);
945 putc('\n', fp);
946 }
947 }
948 if (round == 1)
949 {
950 // Decrement index, loop around and stop when back at
951 // the start.
952 if (--i < 0)
953 i = hislen - 1;
954 if (i == *hisidx)
955 break;
956 }
957 else
958 {
959 // Increment index. Stop at the end in the while.
960 ++i;
961 }
962 }
963 }
964 for (i = 0; i < viminfo_hisidx[type]; ++i)
965 if (viminfo_history[type] != NULL)
John Marriott8df07d02024-10-21 22:37:07 +0200966 {
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200967 vim_free(viminfo_history[type][i].hisstr);
John Marriott8df07d02024-10-21 22:37:07 +0200968 viminfo_history[type][i].hisstrlen = 0;
969 }
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200970 VIM_CLEAR(viminfo_history[type]);
971 viminfo_hisidx[type] = 0;
972 }
973}
Bram Moolenaar5f32ece2019-07-21 21:51:59 +0200974
Bram Moolenaardefa0672019-07-21 19:25:37 +0200975 static void
976write_viminfo_barlines(vir_T *virp, FILE *fp_out)
977{
978 int i;
979 garray_T *gap = &virp->vir_barlines;
980 int seen_useful = FALSE;
981 char *line;
982
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000983 if (gap->ga_len <= 0)
984 return;
Bram Moolenaardefa0672019-07-21 19:25:37 +0200985
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000986 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out);
987
988 // Skip over continuation lines until seeing a useful line.
989 for (i = 0; i < gap->ga_len; ++i)
990 {
991 line = ((char **)(gap->ga_data))[i];
992 if (seen_useful || line[1] != '<')
Bram Moolenaardefa0672019-07-21 19:25:37 +0200993 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000994 fputs(line, fp_out);
995 seen_useful = TRUE;
Bram Moolenaardefa0672019-07-21 19:25:37 +0200996 }
997 }
998}
999
1000/*
1001 * Parse a viminfo line starting with '|'.
1002 * Add each decoded value to "values".
1003 * Returns TRUE if the next line is to be read after using the parsed values.
1004 */
1005 static int
1006barline_parse(vir_T *virp, char_u *text, garray_T *values)
1007{
1008 char_u *p = text;
1009 char_u *nextp = NULL;
1010 char_u *buf = NULL;
1011 bval_T *value;
1012 int i;
1013 int allocated = FALSE;
1014 int eof;
1015 char_u *sconv;
1016 int converted;
1017
1018 while (*p == ',')
1019 {
1020 ++p;
1021 if (ga_grow(values, 1) == FAIL)
1022 break;
1023 value = (bval_T *)(values->ga_data) + values->ga_len;
1024
1025 if (*p == '>')
1026 {
1027 // Need to read a continuation line. Put strings in allocated
1028 // memory, because virp->vir_line is overwritten.
1029 if (!allocated)
1030 {
1031 for (i = 0; i < values->ga_len; ++i)
1032 {
1033 bval_T *vp = (bval_T *)(values->ga_data) + i;
1034
1035 if (vp->bv_type == BVAL_STRING && !vp->bv_allocated)
1036 {
1037 vp->bv_string = vim_strnsave(vp->bv_string, vp->bv_len);
1038 vp->bv_allocated = TRUE;
1039 }
1040 }
1041 allocated = TRUE;
1042 }
1043
1044 if (vim_isdigit(p[1]))
1045 {
1046 size_t len;
1047 size_t todo;
1048 size_t n;
1049
1050 // String value was split into lines that are each shorter
1051 // than LSIZE:
1052 // |{bartype},>{length of "{text}{text2}"}
1053 // |<"{text1}
1054 // |<{text2}",{value}
1055 // Length includes the quotes.
1056 ++p;
1057 len = getdigits(&p);
1058 buf = alloc((int)(len + 1));
1059 if (buf == NULL)
1060 return TRUE;
1061 p = buf;
1062 for (todo = len; todo > 0; todo -= n)
1063 {
1064 eof = viminfo_readline(virp);
1065 if (eof || virp->vir_line[0] != '|'
1066 || virp->vir_line[1] != '<')
1067 {
1068 // File was truncated or garbled. Read another line if
1069 // this one starts with '|'.
1070 vim_free(buf);
1071 return eof || virp->vir_line[0] == '|';
1072 }
1073 // Get length of text, excluding |< and NL chars.
1074 n = STRLEN(virp->vir_line);
1075 while (n > 0 && (virp->vir_line[n - 1] == NL
1076 || virp->vir_line[n - 1] == CAR))
1077 --n;
1078 n -= 2;
1079 if (n > todo)
1080 {
1081 // more values follow after the string
1082 nextp = virp->vir_line + 2 + todo;
1083 n = todo;
1084 }
1085 mch_memmove(p, virp->vir_line + 2, n);
1086 p += n;
1087 }
1088 *p = NUL;
1089 p = buf;
1090 }
1091 else
1092 {
1093 // Line ending in ">" continues in the next line:
1094 // |{bartype},{lots of values},>
1095 // |<{value},{value}
1096 eof = viminfo_readline(virp);
1097 if (eof || virp->vir_line[0] != '|'
1098 || virp->vir_line[1] != '<')
1099 // File was truncated or garbled. Read another line if
1100 // this one starts with '|'.
1101 return eof || virp->vir_line[0] == '|';
1102 p = virp->vir_line + 2;
1103 }
1104 }
1105
Keith Thompson184f71c2024-01-04 21:19:04 +01001106 if (SAFE_isdigit(*p))
Bram Moolenaardefa0672019-07-21 19:25:37 +02001107 {
1108 value->bv_type = BVAL_NR;
1109 value->bv_nr = getdigits(&p);
1110 ++values->ga_len;
1111 }
1112 else if (*p == '"')
1113 {
1114 int len = 0;
1115 char_u *s = p;
1116
1117 // Unescape special characters in-place.
1118 ++p;
1119 while (*p != '"')
1120 {
1121 if (*p == NL || *p == NUL)
1122 return TRUE; // syntax error, drop the value
1123 if (*p == '\\')
1124 {
1125 ++p;
1126 if (*p == 'n')
1127 s[len++] = '\n';
1128 else
1129 s[len++] = *p;
1130 ++p;
1131 }
1132 else
1133 s[len++] = *p++;
1134 }
1135 ++p;
1136 s[len] = NUL;
1137
1138 converted = FALSE;
Bram Moolenaar408030e2020-02-10 22:44:32 +01001139 value->bv_tofree = NULL;
Bram Moolenaardefa0672019-07-21 19:25:37 +02001140 if (virp->vir_conv.vc_type != CONV_NONE && *s != NUL)
1141 {
1142 sconv = string_convert(&virp->vir_conv, s, NULL);
1143 if (sconv != NULL)
1144 {
1145 if (s == buf)
Bram Moolenaar408030e2020-02-10 22:44:32 +01001146 // the converted string is stored in bv_string and
1147 // freed later, also need to free "buf" later
1148 value->bv_tofree = buf;
Bram Moolenaardefa0672019-07-21 19:25:37 +02001149 s = sconv;
Yegappan Lakshmanan084529c2024-12-24 09:50:01 +01001150 len = (int)STRLEN(s);
Bram Moolenaardefa0672019-07-21 19:25:37 +02001151 converted = TRUE;
1152 }
1153 }
1154
1155 // Need to copy in allocated memory if the string wasn't allocated
1156 // above and we did allocate before, thus vir_line may change.
Bram Moolenaar408030e2020-02-10 22:44:32 +01001157 if (s != buf && allocated && !converted)
John Marriott8df07d02024-10-21 22:37:07 +02001158 s = vim_strnsave(s, len);
Bram Moolenaardefa0672019-07-21 19:25:37 +02001159 value->bv_string = s;
1160 value->bv_type = BVAL_STRING;
1161 value->bv_len = len;
1162 value->bv_allocated = allocated || converted;
1163 ++values->ga_len;
1164 if (nextp != NULL)
1165 {
1166 // values following a long string
1167 p = nextp;
1168 nextp = NULL;
1169 }
1170 }
1171 else if (*p == ',')
1172 {
1173 value->bv_type = BVAL_EMPTY;
1174 ++values->ga_len;
1175 }
1176 else
1177 break;
1178 }
1179 return TRUE;
1180}
1181
Bram Moolenaardefa0672019-07-21 19:25:37 +02001182 static void
1183write_viminfo_version(FILE *fp_out)
1184{
1185 fprintf(fp_out, "# Viminfo version\n|%d,%d\n\n",
1186 BARTYPE_VERSION, VIMINFO_VERSION);
1187}
1188
1189 static int
1190no_viminfo(void)
1191{
1192 // "vim -i NONE" does not read or write a viminfo file
1193 return STRCMP(p_viminfofile, "NONE") == 0;
1194}
1195
1196/*
1197 * Report an error for reading a viminfo file.
1198 * Count the number of errors. When there are more than 10, return TRUE.
1199 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02001200 static int
Bram Moolenaardefa0672019-07-21 19:25:37 +02001201viminfo_error(char *errnum, char *message, char_u *line)
1202{
1203 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "),
1204 errnum, message);
1205 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1);
1206 if (IObuff[STRLEN(IObuff) - 1] == '\n')
1207 IObuff[STRLEN(IObuff) - 1] = NUL;
1208 emsg((char *)IObuff);
1209 if (++viminfo_errcnt >= 10)
1210 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00001211 emsg(_(e_viminfo_too_many_errors_skipping_rest_of_file));
Bram Moolenaardefa0672019-07-21 19:25:37 +02001212 return TRUE;
1213 }
1214 return FALSE;
1215}
1216
1217/*
1218 * Compare the 'encoding' value in the viminfo file with the current value of
1219 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for
1220 * conversion of text with iconv() in viminfo_readstring().
1221 */
1222 static int
1223viminfo_encoding(vir_T *virp)
1224{
1225 char_u *p;
1226 int i;
1227
1228 if (get_viminfo_parameter('c') != 0)
1229 {
1230 p = vim_strchr(virp->vir_line, '=');
1231 if (p != NULL)
1232 {
1233 // remove trailing newline
1234 ++p;
1235 for (i = 0; vim_isprintc(p[i]); ++i)
1236 ;
1237 p[i] = NUL;
1238
1239 convert_setup(&virp->vir_conv, p, p_enc);
1240 }
1241 }
1242 return viminfo_readline(virp);
1243}
1244
1245#if defined(FEAT_EVAL) || defined(PROTO)
1246/*
1247 * Restore global vars that start with a capital from the viminfo file
1248 */
1249 static int
1250read_viminfo_varlist(vir_T *virp, int writing)
1251{
1252 char_u *tab;
1253 int type = VAR_NUMBER;
1254 typval_T tv;
1255 funccal_entry_T funccal_entry;
1256
1257 if (!writing && (find_viminfo_parameter('!') != NULL))
1258 {
1259 tab = vim_strchr(virp->vir_line + 1, '\t');
1260 if (tab != NULL)
1261 {
1262 *tab++ = '\0'; // isolate the variable name
1263 switch (*tab)
1264 {
1265 case 'S': type = VAR_STRING; break;
Bram Moolenaardefa0672019-07-21 19:25:37 +02001266 case 'F': type = VAR_FLOAT; break;
Bram Moolenaardefa0672019-07-21 19:25:37 +02001267 case 'D': type = VAR_DICT; break;
1268 case 'L': type = VAR_LIST; break;
1269 case 'B': type = VAR_BLOB; break;
1270 case 'X': type = VAR_SPECIAL; break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001271 case 'T': type = VAR_TUPLE; break;
Bram Moolenaardefa0672019-07-21 19:25:37 +02001272 }
1273
1274 tab = vim_strchr(tab, '\t');
1275 if (tab != NULL)
1276 {
1277 tv.v_type = type;
1278 if (type == VAR_STRING || type == VAR_DICT
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001279 || type == VAR_LIST || type == VAR_BLOB
1280 || type == VAR_TUPLE)
Bram Moolenaardefa0672019-07-21 19:25:37 +02001281 tv.vval.v_string = viminfo_readstring(virp,
1282 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaardefa0672019-07-21 19:25:37 +02001283 else if (type == VAR_FLOAT)
Bram Moolenaar29500652021-08-08 15:43:34 +02001284 (void)string2float(tab + 1, &tv.vval.v_float, FALSE);
Bram Moolenaardefa0672019-07-21 19:25:37 +02001285 else
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001286 {
Bram Moolenaardefa0672019-07-21 19:25:37 +02001287 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001288 if (type == VAR_SPECIAL && (tv.vval.v_number == VVAL_FALSE
1289 || tv.vval.v_number == VVAL_TRUE))
1290 tv.v_type = VAR_BOOL;
1291 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001292 if (type == VAR_DICT || type == VAR_LIST || type == VAR_TUPLE)
Bram Moolenaardefa0672019-07-21 19:25:37 +02001293 {
1294 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
1295
1296 if (etv == NULL)
1297 // Failed to parse back the dict or list, use it as a
1298 // string.
1299 tv.v_type = VAR_STRING;
1300 else
1301 {
1302 vim_free(tv.vval.v_string);
1303 tv = *etv;
1304 vim_free(etv);
1305 }
1306 }
1307 else if (type == VAR_BLOB)
1308 {
1309 blob_T *blob = string2blob(tv.vval.v_string);
1310
1311 if (blob == NULL)
1312 // Failed to parse back the blob, use it as a string.
1313 tv.v_type = VAR_STRING;
1314 else
1315 {
1316 vim_free(tv.vval.v_string);
1317 tv.v_type = VAR_BLOB;
1318 tv.vval.v_blob = blob;
1319 }
1320 }
1321
1322 // when in a function use global variables
1323 save_funccal(&funccal_entry);
1324 set_var(virp->vir_line + 1, &tv, FALSE);
1325 restore_funccal();
1326
1327 if (tv.v_type == VAR_STRING)
1328 vim_free(tv.vval.v_string);
1329 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
1330 tv.v_type == VAR_BLOB)
1331 clear_tv(&tv);
1332 }
1333 }
1334 }
1335
1336 return viminfo_readline(virp);
1337}
1338
1339/*
1340 * Write global vars that start with a capital to the viminfo file
1341 */
1342 static void
1343write_viminfo_varlist(FILE *fp)
1344{
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001345 hashtab_T *gvht = get_globvar_ht();
Bram Moolenaardefa0672019-07-21 19:25:37 +02001346 hashitem_T *hi;
1347 dictitem_T *this_var;
1348 int todo;
1349 char *s = "";
1350 char_u *p;
1351 char_u *tofree;
1352 char_u numbuf[NUMBUFLEN];
1353
1354 if (find_viminfo_parameter('!') == NULL)
1355 return;
1356
1357 fputs(_("\n# global variables:\n"), fp);
1358
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001359 todo = (int)gvht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001360 FOR_ALL_HASHTAB_ITEMS(gvht, hi, todo)
Bram Moolenaardefa0672019-07-21 19:25:37 +02001361 {
1362 if (!HASHITEM_EMPTY(hi))
1363 {
1364 --todo;
1365 this_var = HI2DI(hi);
1366 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
1367 {
1368 switch (this_var->di_tv.v_type)
1369 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001370 case VAR_STRING: s = "STR"; break;
1371 case VAR_NUMBER: s = "NUM"; break;
1372 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar5b157fe2020-06-07 16:08:08 +02001373 case VAR_DICT:
1374 {
1375 dict_T *di = this_var->di_tv.vval.v_dict;
1376 int copyID = get_copyID();
1377
1378 s = "DIC";
1379 if (di != NULL && !set_ref_in_ht(
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001380 &di->dv_hashtab, copyID, NULL, NULL)
Bram Moolenaar5b157fe2020-06-07 16:08:08 +02001381 && di->dv_copyID == copyID)
1382 // has a circular reference, can't turn the
1383 // value into a string
1384 continue;
1385 break;
1386 }
1387 case VAR_LIST:
1388 {
1389 list_T *l = this_var->di_tv.vval.v_list;
1390 int copyID = get_copyID();
1391
1392 s = "LIS";
1393 if (l != NULL && !set_ref_in_list_items(
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001394 l, copyID, NULL, NULL)
Bram Moolenaar5b157fe2020-06-07 16:08:08 +02001395 && l->lv_copyID == copyID)
1396 // has a circular reference, can't turn the
1397 // value into a string
1398 continue;
1399 break;
1400 }
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001401 case VAR_TUPLE:
1402 {
1403 tuple_T *tuple = this_var->di_tv.vval.v_tuple;
1404 int copyID = get_copyID();
1405
1406 s = "TUP";
1407 if (tuple != NULL && !set_ref_in_tuple_items(
1408 tuple, copyID, NULL, NULL)
1409 && tuple->tv_copyID == copyID)
1410 // has a circular reference, can't turn the
1411 // value into a string
1412 continue;
1413 break;
1414 }
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001415 case VAR_BLOB: s = "BLO"; break;
1416 case VAR_BOOL: s = "XPL"; break; // backwards compat.
Bram Moolenaardefa0672019-07-21 19:25:37 +02001417 case VAR_SPECIAL: s = "XPL"; break;
1418
1419 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001420 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 case VAR_VOID:
Bram Moolenaardefa0672019-07-21 19:25:37 +02001422 case VAR_FUNC:
1423 case VAR_PARTIAL:
1424 case VAR_JOB:
1425 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001426 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001427 case VAR_CLASS:
1428 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001429 case VAR_TYPEALIAS:
1430 continue;
Bram Moolenaardefa0672019-07-21 19:25:37 +02001431 }
1432 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001433 if (this_var->di_tv.v_type == VAR_BOOL
1434 || this_var->di_tv.v_type == VAR_SPECIAL)
Bram Moolenaardefa0672019-07-21 19:25:37 +02001435 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001436 // do not use "v:true" but "1"
Bram Moolenaardefa0672019-07-21 19:25:37 +02001437 sprintf((char *)numbuf, "%ld",
1438 (long)this_var->di_tv.vval.v_number);
1439 p = numbuf;
1440 tofree = NULL;
1441 }
1442 else
1443 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
1444 if (p != NULL)
1445 viminfo_writestring(fp, p);
1446 vim_free(tofree);
1447 }
1448 }
1449 }
1450}
1451#endif // FEAT_EVAL
1452
Bram Moolenaarc3328162019-07-23 22:15:25 +02001453 static int
1454read_viminfo_sub_string(vir_T *virp, int force)
1455{
1456 if (force || get_old_sub() == NULL)
1457 set_old_sub(viminfo_readstring(virp, 1, TRUE));
1458 return viminfo_readline(virp);
1459}
1460
1461 static void
1462write_viminfo_sub_string(FILE *fp)
1463{
1464 char_u *old_sub = get_old_sub();
1465
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001466 if (get_viminfo_parameter('/') == 0 || old_sub == NULL)
1467 return;
1468
1469 fputs(_("\n# Last Substitute String:\n$"), fp);
1470 viminfo_writestring(fp, old_sub);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001471}
1472
1473/*
1474 * Functions relating to reading/writing the search pattern from viminfo
1475 */
1476
1477 static int
1478read_viminfo_search_pattern(vir_T *virp, int force)
1479{
1480 char_u *lp;
1481 int idx = -1;
1482 int magic = FALSE;
1483 int no_scs = FALSE;
1484 int off_line = FALSE;
1485 int off_end = 0;
1486 long off = 0;
1487 int setlast = FALSE;
1488#ifdef FEAT_SEARCH_EXTRA
1489 static int hlsearch_on = FALSE;
1490#endif
1491 char_u *val;
1492 spat_T *spat;
1493
1494 // Old line types:
1495 // "/pat", "&pat": search/subst. pat
1496 // "~/pat", "~&pat": last used search/subst. pat
1497 // New line types:
1498 // "~h", "~H": hlsearch highlighting off/on
1499 // "~<magic><smartcase><line><end><off><last><which>pat"
1500 // <magic>: 'm' off, 'M' on
1501 // <smartcase>: 's' off, 'S' on
1502 // <line>: 'L' line offset, 'l' char offset
1503 // <end>: 'E' from end, 'e' from start
1504 // <off>: decimal, offset
1505 // <last>: '~' last used pattern
1506 // <which>: '/' search pat, '&' subst. pat
1507 lp = virp->vir_line;
1508 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) // new line type
1509 {
1510 if (lp[1] == 'M') // magic on
1511 magic = TRUE;
1512 if (lp[2] == 's')
1513 no_scs = TRUE;
1514 if (lp[3] == 'L')
1515 off_line = TRUE;
1516 if (lp[4] == 'E')
1517 off_end = SEARCH_END;
1518 lp += 5;
1519 off = getdigits(&lp);
1520 }
1521 if (lp[0] == '~') // use this pattern for last-used pattern
1522 {
1523 setlast = TRUE;
1524 lp++;
1525 }
1526 if (lp[0] == '/')
1527 idx = RE_SEARCH;
1528 else if (lp[0] == '&')
1529 idx = RE_SUBST;
1530#ifdef FEAT_SEARCH_EXTRA
1531 else if (lp[0] == 'h') // ~h: 'hlsearch' highlighting off
1532 hlsearch_on = FALSE;
1533 else if (lp[0] == 'H') // ~H: 'hlsearch' highlighting on
1534 hlsearch_on = TRUE;
1535#endif
Bram Moolenaarc3328162019-07-23 22:15:25 +02001536 if (idx >= 0)
1537 {
Bram Moolenaar736cd2c2019-07-25 21:58:19 +02001538 spat = get_spat(idx);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001539 if (force || spat->pat == NULL)
1540 {
1541 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
1542 TRUE);
1543 if (val != NULL)
1544 {
1545 set_last_search_pat(val, idx, magic, setlast);
1546 vim_free(val);
1547 spat->no_scs = no_scs;
1548 spat->off.line = off_line;
1549 spat->off.end = off_end;
1550 spat->off.off = off;
1551#ifdef FEAT_SEARCH_EXTRA
1552 if (setlast)
1553 set_no_hlsearch(!hlsearch_on);
1554#endif
1555 }
1556 }
1557 }
1558 return viminfo_readline(virp);
1559}
1560
1561 static void
1562wvsp_one(
1563 FILE *fp, // file to write to
1564 int idx, // spats[] index
1565 char *s, // search pat
1566 int sc) // dir char
1567{
1568 spat_T *spat = get_spat(idx);
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001569 if (spat->pat == NULL)
1570 return;
1571
1572 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
1573 // off.dir is not stored, it's reset to forward
1574 fprintf(fp, "%c%c%c%c%ld%s%c",
1575 spat->magic ? 'M' : 'm', // magic
1576 spat->no_scs ? 's' : 'S', // smartcase
1577 spat->off.line ? 'L' : 'l', // line offset
1578 spat->off.end ? 'E' : 'e', // offset from end
1579 spat->off.off, // offset
1580 get_spat_last_idx() == idx ? "~" : "", // last used pat
1581 sc);
1582 viminfo_writestring(fp, spat->pat);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001583}
1584
1585 static void
1586write_viminfo_search_pattern(FILE *fp)
1587{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001588 if (get_viminfo_parameter('/') == 0)
1589 return;
1590
Bram Moolenaarc3328162019-07-23 22:15:25 +02001591#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001592 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
Bram Moolenaarc3328162019-07-23 22:15:25 +02001593 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
1594#endif
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001595 wvsp_one(fp, RE_SEARCH, "", '/');
1596 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
Bram Moolenaarc3328162019-07-23 22:15:25 +02001597}
1598
1599/*
1600 * Functions relating to reading/writing registers from viminfo
1601 */
1602
1603static yankreg_T *y_read_regs = NULL;
1604
1605#define REG_PREVIOUS 1
1606#define REG_EXEC 2
1607
1608/*
1609 * Prepare for reading viminfo registers when writing viminfo later.
1610 */
1611 static void
1612prepare_viminfo_registers(void)
1613{
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02001614 y_read_regs = ALLOC_CLEAR_MULT(yankreg_T, NUM_REGISTERS);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001615}
1616
1617 static void
1618finish_viminfo_registers(void)
1619{
1620 int i;
1621 int j;
1622
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001623 if (y_read_regs == NULL)
1624 return;
1625
1626 for (i = 0; i < NUM_REGISTERS; ++i)
1627 if (y_read_regs[i].y_array != NULL)
1628 {
1629 for (j = 0; j < y_read_regs[i].y_size; j++)
John Marriott79f6ffd2024-10-31 10:06:54 +01001630 vim_free(y_read_regs[i].y_array[j].string);
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001631 vim_free(y_read_regs[i].y_array);
1632 }
1633 VIM_CLEAR(y_read_regs);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001634}
1635
1636 static int
1637read_viminfo_register(vir_T *virp, int force)
1638{
1639 int eof;
1640 int do_it = TRUE;
1641 int size;
1642 int limit;
1643 int i;
1644 int set_prev = FALSE;
1645 char_u *str;
John Marriott79f6ffd2024-10-31 10:06:54 +01001646 string_T *array = NULL;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001647 int new_type = MCHAR; // init to shut up compiler
1648 colnr_T new_width = 0; // init to shut up compiler
1649 yankreg_T *y_current_p;
1650
1651 // We only get here (hopefully) if line[0] == '"'
1652 str = virp->vir_line + 1;
1653
1654 // If the line starts with "" this is the y_previous register.
1655 if (*str == '"')
1656 {
1657 set_prev = TRUE;
1658 str++;
1659 }
1660
1661 if (!ASCII_ISALNUM(*str) && *str != '-')
1662 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001663 if (viminfo_error("E577: ", _(e_illegal_register_name), virp->vir_line))
Bram Moolenaarc3328162019-07-23 22:15:25 +02001664 return TRUE; // too many errors, pretend end-of-file
1665 do_it = FALSE;
1666 }
1667 get_yank_register(*str++, FALSE);
1668 y_current_p = get_y_current();
1669 if (!force && y_current_p->y_array != NULL)
1670 do_it = FALSE;
1671
1672 if (*str == '@')
1673 {
1674 // "x@: register x used for @@
1675 if (force || get_execreg_lastc() == NUL)
1676 set_execreg_lastc(str[-1]);
1677 }
1678
1679 size = 0;
1680 limit = 100; // Optimized for registers containing <= 100 lines
1681 if (do_it)
1682 {
1683 // Build the new register in array[].
1684 // y_array is kept as-is until done.
1685 // The "do_it" flag is reset when something is wrong, in which case
1686 // array[] needs to be freed.
1687 if (set_prev)
1688 set_y_previous(y_current_p);
John Marriott79f6ffd2024-10-31 10:06:54 +01001689 array = ALLOC_MULT(string_T, limit);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001690 str = skipwhite(skiptowhite(str));
1691 if (STRNCMP(str, "CHAR", 4) == 0)
1692 new_type = MCHAR;
1693 else if (STRNCMP(str, "BLOCK", 5) == 0)
1694 new_type = MBLOCK;
1695 else
1696 new_type = MLINE;
1697 // get the block width; if it's missing we get a zero, which is OK
1698 str = skipwhite(skiptowhite(str));
1699 new_width = getdigits(&str);
1700 }
1701
1702 while (!(eof = viminfo_readline(virp))
1703 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
1704 {
1705 if (do_it)
1706 {
1707 if (size == limit)
1708 {
John Marriott79f6ffd2024-10-31 10:06:54 +01001709 string_T *new_array = (string_T *)
1710 alloc(limit * 2 * sizeof(string_T));
Bram Moolenaarc3328162019-07-23 22:15:25 +02001711
1712 if (new_array == NULL)
1713 {
1714 do_it = FALSE;
1715 break;
1716 }
1717 for (i = 0; i < limit; i++)
1718 new_array[i] = array[i];
1719 vim_free(array);
1720 array = new_array;
1721 limit *= 2;
1722 }
1723 str = viminfo_readstring(virp, 1, TRUE);
1724 if (str != NULL)
John Marriott79f6ffd2024-10-31 10:06:54 +01001725 {
1726 array[size].string = str;
1727 array[size].length = STRLEN(str);
1728 ++size;
1729 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02001730 else
1731 // error, don't store the result
1732 do_it = FALSE;
1733 }
1734 }
1735
1736 if (do_it)
1737 {
1738 // free y_array[]
1739 for (i = 0; i < y_current_p->y_size; i++)
John Marriott79f6ffd2024-10-31 10:06:54 +01001740 vim_free(y_current_p->y_array[i].string);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001741 vim_free(y_current_p->y_array);
1742
1743 y_current_p->y_type = new_type;
1744 y_current_p->y_width = new_width;
1745 y_current_p->y_size = size;
1746 y_current_p->y_time_set = 0;
1747 if (size == 0)
1748 {
1749 y_current_p->y_array = NULL;
1750 }
1751 else
1752 {
1753 // Move the lines from array[] to y_array[].
John Marriott79f6ffd2024-10-31 10:06:54 +01001754 y_current_p->y_array = ALLOC_MULT(string_T, size);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001755 for (i = 0; i < size; i++)
1756 {
1757 if (y_current_p->y_array == NULL)
John Marriott79f6ffd2024-10-31 10:06:54 +01001758 {
1759 VIM_CLEAR_STRING(array[i]);
1760 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02001761 else
John Marriott79f6ffd2024-10-31 10:06:54 +01001762 {
1763 y_current_p->y_array[i].string = array[i].string;
1764 y_current_p->y_array[i].length = array[i].length;
1765 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02001766 }
1767 }
1768 }
1769 else
1770 {
1771 // Free array[] if it was filled.
1772 for (i = 0; i < size; i++)
John Marriott79f6ffd2024-10-31 10:06:54 +01001773 vim_free(array[i].string);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001774 }
1775 vim_free(array);
1776
1777 return eof;
1778}
1779
1780/*
1781 * Accept a new style register line from the viminfo, store it when it's new.
1782 */
1783 static void
1784handle_viminfo_register(garray_T *values, int force)
1785{
1786 bval_T *vp = (bval_T *)values->ga_data;
1787 int flags;
1788 int name;
1789 int type;
1790 int linecount;
1791 int width;
1792 time_t timestamp;
1793 yankreg_T *y_ptr;
1794 yankreg_T *y_regs_p = get_y_regs();
1795 int i;
1796
1797 // Check the format:
1798 // |{bartype},{flags},{name},{type},
1799 // {linecount},{width},{timestamp},"line1","line2"
1800 if (values->ga_len < 6
1801 || vp[0].bv_type != BVAL_NR
1802 || vp[1].bv_type != BVAL_NR
1803 || vp[2].bv_type != BVAL_NR
1804 || vp[3].bv_type != BVAL_NR
1805 || vp[4].bv_type != BVAL_NR
1806 || vp[5].bv_type != BVAL_NR)
1807 return;
1808 flags = vp[0].bv_nr;
1809 name = vp[1].bv_nr;
1810 if (name < 0 || name >= NUM_REGISTERS)
1811 return;
1812 type = vp[2].bv_nr;
1813 if (type != MCHAR && type != MLINE && type != MBLOCK)
1814 return;
1815 linecount = vp[3].bv_nr;
1816 if (values->ga_len < 6 + linecount)
1817 return;
1818 width = vp[4].bv_nr;
1819 if (width < 0)
1820 return;
1821
1822 if (y_read_regs != NULL)
1823 // Reading viminfo for merging and writing. Store the register
1824 // content, don't update the current registers.
1825 y_ptr = &y_read_regs[name];
1826 else
1827 y_ptr = &y_regs_p[name];
1828
1829 // Do not overwrite unless forced or the timestamp is newer.
1830 timestamp = (time_t)vp[5].bv_nr;
1831 if (y_ptr->y_array != NULL && !force
1832 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
1833 return;
1834
1835 if (y_ptr->y_array != NULL)
1836 for (i = 0; i < y_ptr->y_size; i++)
John Marriott79f6ffd2024-10-31 10:06:54 +01001837 vim_free(y_ptr->y_array[i].string);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001838 vim_free(y_ptr->y_array);
1839
1840 if (y_read_regs == NULL)
1841 {
1842 if (flags & REG_PREVIOUS)
1843 set_y_previous(y_ptr);
1844 if ((flags & REG_EXEC) && (force || get_execreg_lastc() == NUL))
1845 set_execreg_lastc(get_register_name(name));
1846 }
1847 y_ptr->y_type = type;
1848 y_ptr->y_width = width;
1849 y_ptr->y_size = linecount;
1850 y_ptr->y_time_set = timestamp;
1851 if (linecount == 0)
1852 {
1853 y_ptr->y_array = NULL;
1854 return;
1855 }
John Marriott79f6ffd2024-10-31 10:06:54 +01001856 y_ptr->y_array = ALLOC_MULT(string_T, linecount);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001857 if (y_ptr->y_array == NULL)
1858 {
1859 y_ptr->y_size = 0; // ensure object state is consistent
1860 return;
1861 }
1862 for (i = 0; i < linecount; i++)
1863 {
1864 if (vp[i + 6].bv_allocated)
1865 {
John Marriott79f6ffd2024-10-31 10:06:54 +01001866 y_ptr->y_array[i].string = vp[i + 6].bv_string;
1867 y_ptr->y_array[i].length = vp[i + 6].bv_len;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001868 vp[i + 6].bv_string = NULL;
1869 }
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02001870 else if (vp[i + 6].bv_type != BVAL_STRING)
1871 {
1872 free(y_ptr->y_array);
1873 y_ptr->y_array = NULL;
1874 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02001875 else
John Marriott79f6ffd2024-10-31 10:06:54 +01001876 {
1877 y_ptr->y_array[i].string = vim_strnsave(vp[i + 6].bv_string, vp[i + 6].bv_len);
1878 y_ptr->y_array[i].length = vp[i + 6].bv_len;
1879 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02001880 }
1881}
1882
1883 static void
1884write_viminfo_registers(FILE *fp)
1885{
1886 int i, j;
1887 char_u *type;
1888 char_u c;
1889 int num_lines;
1890 int max_num_lines;
1891 int max_kbyte;
1892 long len;
1893 yankreg_T *y_ptr;
1894 yankreg_T *y_regs_p = get_y_regs();;
1895
1896 fputs(_("\n# Registers:\n"), fp);
1897
1898 // Get '<' value, use old '"' value if '<' is not found.
1899 max_num_lines = get_viminfo_parameter('<');
1900 if (max_num_lines < 0)
1901 max_num_lines = get_viminfo_parameter('"');
1902 if (max_num_lines == 0)
1903 return;
1904 max_kbyte = get_viminfo_parameter('s');
1905 if (max_kbyte == 0)
1906 return;
1907
1908 for (i = 0; i < NUM_REGISTERS; i++)
1909 {
1910#ifdef FEAT_CLIPBOARD
1911 // Skip '*'/'+' register, we don't want them back next time
1912 if (i == STAR_REGISTER || i == PLUS_REGISTER)
1913 continue;
1914#endif
1915#ifdef FEAT_DND
1916 // Neither do we want the '~' register
1917 if (i == TILDE_REGISTER)
1918 continue;
1919#endif
1920 // When reading viminfo for merging and writing: Use the register from
1921 // viminfo if it's newer.
1922 if (y_read_regs != NULL
1923 && y_read_regs[i].y_array != NULL
1924 && (y_regs_p[i].y_array == NULL ||
1925 y_read_regs[i].y_time_set > y_regs_p[i].y_time_set))
1926 y_ptr = &y_read_regs[i];
1927 else if (y_regs_p[i].y_array == NULL)
1928 continue;
1929 else
1930 y_ptr = &y_regs_p[i];
1931
1932 // Skip empty registers.
1933 num_lines = y_ptr->y_size;
1934 if (num_lines == 0
1935 || (num_lines == 1 && y_ptr->y_type == MCHAR
John Marriott79f6ffd2024-10-31 10:06:54 +01001936 && *y_ptr->y_array[0].string == NUL))
Bram Moolenaarc3328162019-07-23 22:15:25 +02001937 continue;
1938
1939 if (max_kbyte > 0)
1940 {
1941 // Skip register if there is more text than the maximum size.
1942 len = 0;
1943 for (j = 0; j < num_lines; j++)
John Marriott79f6ffd2024-10-31 10:06:54 +01001944 len += (long)y_ptr->y_array[j].length + 1L;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001945 if (len > (long)max_kbyte * 1024L)
1946 continue;
1947 }
1948
1949 switch (y_ptr->y_type)
1950 {
1951 case MLINE:
1952 type = (char_u *)"LINE";
1953 break;
1954 case MCHAR:
1955 type = (char_u *)"CHAR";
1956 break;
1957 case MBLOCK:
1958 type = (char_u *)"BLOCK";
1959 break;
1960 default:
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001961 semsg(_(e_unknown_register_type_nr), y_ptr->y_type);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001962 type = (char_u *)"LINE";
1963 break;
1964 }
1965 if (get_y_previous() == &y_regs_p[i])
1966 fprintf(fp, "\"");
1967 c = get_register_name(i);
1968 fprintf(fp, "\"%c", c);
1969 if (c == get_execreg_lastc())
1970 fprintf(fp, "@");
1971 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
1972
1973 // If max_num_lines < 0, then we save ALL the lines in the register
1974 if (max_num_lines > 0 && num_lines > max_num_lines)
1975 num_lines = max_num_lines;
1976 for (j = 0; j < num_lines; j++)
1977 {
1978 putc('\t', fp);
John Marriott79f6ffd2024-10-31 10:06:54 +01001979 viminfo_writestring(fp, y_ptr->y_array[j].string);
Bram Moolenaarc3328162019-07-23 22:15:25 +02001980 }
1981
1982 {
1983 int flags = 0;
1984 int remaining;
1985
1986 // New style with a bar line. Format:
1987 // |{bartype},{flags},{name},{type},
1988 // {linecount},{width},{timestamp},"line1","line2"
1989 // flags: REG_PREVIOUS - register is y_previous
1990 // REG_EXEC - used for @@
1991 if (get_y_previous() == &y_regs_p[i])
1992 flags |= REG_PREVIOUS;
1993 if (c == get_execreg_lastc())
1994 flags |= REG_EXEC;
1995 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
1996 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
1997 (long)y_ptr->y_time_set);
1998 // 11 chars for type/flags/name/type, 3 * 20 for numbers
1999 remaining = LSIZE - 71;
2000 for (j = 0; j < num_lines; j++)
2001 {
2002 putc(',', fp);
2003 --remaining;
John Marriott79f6ffd2024-10-31 10:06:54 +01002004 remaining = barline_writestring(fp, y_ptr->y_array[j].string,
Bram Moolenaarc3328162019-07-23 22:15:25 +02002005 remaining);
2006 }
2007 putc('\n', fp);
2008 }
2009 }
2010}
2011
2012/*
2013 * Functions relating to reading/writing marks from viminfo
2014 */
2015
2016static xfmark_T *vi_namedfm = NULL;
Bram Moolenaarc3328162019-07-23 22:15:25 +02002017static xfmark_T *vi_jumplist = NULL;
2018static int vi_jumplist_len = 0;
Bram Moolenaarc3328162019-07-23 22:15:25 +02002019
2020 static void
2021write_one_mark(FILE *fp_out, int c, pos_T *pos)
2022{
2023 if (pos->lnum != 0)
2024 fprintf(fp_out, "\t%c\t%ld\t%d\n", c, (long)pos->lnum, (int)pos->col);
2025}
2026
2027 static void
2028write_buffer_marks(buf_T *buf, FILE *fp_out)
2029{
2030 int i;
2031 pos_T pos;
2032
2033 home_replace(NULL, buf->b_ffname, IObuff, IOSIZE, TRUE);
2034 fprintf(fp_out, "\n> ");
2035 viminfo_writestring(fp_out, IObuff);
2036
2037 // Write the last used timestamp as the lnum of the non-existing mark '*'.
2038 // Older Vims will ignore it and/or copy it.
2039 pos.lnum = (linenr_T)buf->b_last_used;
2040 pos.col = 0;
2041 write_one_mark(fp_out, '*', &pos);
2042
2043 write_one_mark(fp_out, '"', &buf->b_last_cursor);
2044 write_one_mark(fp_out, '^', &buf->b_last_insert);
2045 write_one_mark(fp_out, '.', &buf->b_last_change);
Bram Moolenaarc3328162019-07-23 22:15:25 +02002046 // changelist positions are stored oldest first
2047 for (i = 0; i < buf->b_changelistlen; ++i)
2048 {
2049 // skip duplicates
2050 if (i == 0 || !EQUAL_POS(buf->b_changelist[i - 1],
2051 buf->b_changelist[i]))
2052 write_one_mark(fp_out, '+', &buf->b_changelist[i]);
2053 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02002054 for (i = 0; i < NMARKS; i++)
2055 write_one_mark(fp_out, 'a' + i, &buf->b_namedm[i]);
2056}
2057
2058/*
2059 * Return TRUE if marks for "buf" should not be written.
2060 */
2061 static int
2062skip_for_viminfo(buf_T *buf)
2063{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01002064 return bt_terminal(buf) || removable(buf->b_ffname);
Bram Moolenaarc3328162019-07-23 22:15:25 +02002065}
2066
2067/*
2068 * Write all the named marks for all buffers.
2069 * When "buflist" is not NULL fill it with the buffers for which marks are to
2070 * be written.
2071 */
2072 static void
2073write_viminfo_marks(FILE *fp_out, garray_T *buflist)
2074{
2075 buf_T *buf;
2076 int is_mark_set;
2077 int i;
2078 win_T *win;
2079 tabpage_T *tp;
2080
2081 // Set b_last_cursor for the all buffers that have a window.
2082 FOR_ALL_TAB_WINDOWS(tp, win)
2083 set_last_cursor(win);
2084
2085 fputs(_("\n# History of marks within files (newest to oldest):\n"), fp_out);
2086 FOR_ALL_BUFFERS(buf)
2087 {
2088 // Only write something if buffer has been loaded and at least one
2089 // mark is set.
2090 if (buf->b_marks_read)
2091 {
2092 if (buf->b_last_cursor.lnum != 0)
2093 is_mark_set = TRUE;
2094 else
2095 {
2096 is_mark_set = FALSE;
2097 for (i = 0; i < NMARKS; i++)
2098 if (buf->b_namedm[i].lnum != 0)
2099 {
2100 is_mark_set = TRUE;
2101 break;
2102 }
2103 }
2104 if (is_mark_set && buf->b_ffname != NULL
2105 && buf->b_ffname[0] != NUL
2106 && !skip_for_viminfo(buf))
2107 {
2108 if (buflist == NULL)
2109 write_buffer_marks(buf, fp_out);
2110 else if (ga_grow(buflist, 1) == OK)
2111 ((buf_T **)buflist->ga_data)[buflist->ga_len++] = buf;
2112 }
2113 }
2114 }
2115}
2116
2117 static void
2118write_one_filemark(
2119 FILE *fp,
2120 xfmark_T *fm,
2121 int c1,
2122 int c2)
2123{
2124 char_u *name;
2125
2126 if (fm->fmark.mark.lnum == 0) // not set
2127 return;
2128
2129 if (fm->fmark.fnum != 0) // there is a buffer
2130 name = buflist_nr2name(fm->fmark.fnum, TRUE, FALSE);
2131 else
2132 name = fm->fname; // use name from .viminfo
2133 if (name != NULL && *name != NUL)
2134 {
2135 fprintf(fp, "%c%c %ld %ld ", c1, c2, (long)fm->fmark.mark.lnum,
2136 (long)fm->fmark.mark.col);
2137 viminfo_writestring(fp, name);
2138
2139 // Barline: |{bartype},{name},{lnum},{col},{timestamp},{filename}
2140 // size up to filename: 8 + 3 * 20
2141 fprintf(fp, "|%d,%d,%ld,%ld,%ld,", BARTYPE_MARK, c2,
2142 (long)fm->fmark.mark.lnum, (long)fm->fmark.mark.col,
2143 (long)fm->time_set);
2144 barline_writestring(fp, name, LSIZE - 70);
2145 putc('\n', fp);
2146 }
2147
2148 if (fm->fmark.fnum != 0)
2149 vim_free(name);
2150}
2151
2152 static void
2153write_viminfo_filemarks(FILE *fp)
2154{
2155 int i;
2156 char_u *name;
2157 buf_T *buf;
2158 xfmark_T *namedfm_p = get_namedfm();
2159 xfmark_T *fm;
2160 int vi_idx;
2161 int idx;
2162
2163 if (get_viminfo_parameter('f') == 0)
2164 return;
2165
2166 fputs(_("\n# File marks:\n"), fp);
2167
2168 // Write the filemarks 'A - 'Z
2169 for (i = 0; i < NMARKS; i++)
2170 {
2171 if (vi_namedfm != NULL
Bram Moolenaar8cd6cd82019-12-27 17:33:26 +01002172 && (vi_namedfm[i].time_set > namedfm_p[i].time_set))
Bram Moolenaarc3328162019-07-23 22:15:25 +02002173 fm = &vi_namedfm[i];
2174 else
2175 fm = &namedfm_p[i];
2176 write_one_filemark(fp, fm, '\'', i + 'A');
2177 }
2178
2179 // Find a mark that is the same file and position as the cursor.
2180 // That one, or else the last one is deleted.
2181 // Move '0 to '1, '1 to '2, etc. until the matching one or '9
2182 // Set the '0 mark to current cursor position.
2183 if (curbuf->b_ffname != NULL && !skip_for_viminfo(curbuf))
2184 {
2185 name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE);
2186 for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i)
2187 if (namedfm_p[i].fmark.mark.lnum == curwin->w_cursor.lnum
2188 && (namedfm_p[i].fname == NULL
2189 ? namedfm_p[i].fmark.fnum == curbuf->b_fnum
2190 : (name != NULL
2191 && STRCMP(name, namedfm_p[i].fname) == 0)))
2192 break;
2193 vim_free(name);
2194
2195 vim_free(namedfm_p[i].fname);
2196 for ( ; i > NMARKS; --i)
2197 namedfm_p[i] = namedfm_p[i - 1];
2198 namedfm_p[NMARKS].fmark.mark = curwin->w_cursor;
2199 namedfm_p[NMARKS].fmark.fnum = curbuf->b_fnum;
2200 namedfm_p[NMARKS].fname = NULL;
2201 namedfm_p[NMARKS].time_set = vim_time();
2202 }
2203
2204 // Write the filemarks '0 - '9. Newest (highest timestamp) first.
2205 vi_idx = NMARKS;
2206 idx = NMARKS;
2207 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
2208 {
2209 xfmark_T *vi_fm = vi_namedfm != NULL ? &vi_namedfm[vi_idx] : NULL;
2210
2211 if (vi_fm != NULL
2212 && vi_fm->fmark.mark.lnum != 0
2213 && (vi_fm->time_set > namedfm_p[idx].time_set
2214 || namedfm_p[idx].fmark.mark.lnum == 0))
2215 {
2216 fm = vi_fm;
2217 ++vi_idx;
2218 }
2219 else
2220 {
2221 fm = &namedfm_p[idx++];
2222 if (vi_fm != NULL
2223 && vi_fm->fmark.mark.lnum == fm->fmark.mark.lnum
2224 && vi_fm->time_set == fm->time_set
2225 && ((vi_fm->fmark.fnum != 0
2226 && vi_fm->fmark.fnum == fm->fmark.fnum)
2227 || (vi_fm->fname != NULL
2228 && fm->fname != NULL
2229 && STRCMP(vi_fm->fname, fm->fname) == 0)))
2230 ++vi_idx; // skip duplicate
2231 }
2232 write_one_filemark(fp, fm, '\'', i - NMARKS + '0');
2233 }
2234
Bram Moolenaarc3328162019-07-23 22:15:25 +02002235 // Write the jumplist with -'
2236 fputs(_("\n# Jumplist (newest first):\n"), fp);
2237 setpcmark(); // add current cursor position
2238 cleanup_jumplist(curwin, FALSE);
2239 vi_idx = 0;
2240 idx = curwin->w_jumplistlen - 1;
2241 for (i = 0; i < JUMPLISTSIZE; ++i)
2242 {
2243 xfmark_T *vi_fm;
2244
2245 fm = idx >= 0 ? &curwin->w_jumplist[idx] : NULL;
Bram Moolenaar4ad739f2020-09-02 10:25:45 +02002246 vi_fm = (vi_jumplist != NULL && vi_idx < vi_jumplist_len)
2247 ? &vi_jumplist[vi_idx] : NULL;
Bram Moolenaarc3328162019-07-23 22:15:25 +02002248 if (fm == NULL && vi_fm == NULL)
2249 break;
2250 if (fm == NULL || (vi_fm != NULL && fm->time_set < vi_fm->time_set))
2251 {
2252 fm = vi_fm;
2253 ++vi_idx;
2254 }
2255 else
2256 --idx;
2257 if (fm->fmark.fnum == 0
2258 || ((buf = buflist_findnr(fm->fmark.fnum)) != NULL
2259 && !skip_for_viminfo(buf)))
2260 write_one_filemark(fp, fm, '-', '\'');
2261 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02002262}
2263
2264/*
2265 * Compare functions for qsort() below, that compares b_last_used.
2266 */
Bram Moolenaar52410572019-10-27 05:12:45 +01002267 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02002268buf_compare(const void *s1, const void *s2)
2269{
2270 buf_T *buf1 = *(buf_T **)s1;
2271 buf_T *buf2 = *(buf_T **)s2;
2272
2273 if (buf1->b_last_used == buf2->b_last_used)
2274 return 0;
2275 return buf1->b_last_used > buf2->b_last_used ? -1 : 1;
2276}
2277
2278/*
2279 * Handle marks in the viminfo file:
2280 * fp_out != NULL: copy marks, in time order with buffers in "buflist".
Bram Moolenaar3ff656f2021-02-10 19:22:15 +01002281 * fp_out == NULL && (flags & VIF_WANT_MARKS): read marks for curbuf
2282 * fp_out == NULL && (flags & VIF_ONLY_CURBUF): bail out after curbuf marks
Bram Moolenaarc3328162019-07-23 22:15:25 +02002283 * fp_out == NULL && (flags & VIF_GET_OLDFILES | VIF_FORCEIT): fill v:oldfiles
2284 */
2285 static void
2286copy_viminfo_marks(
2287 vir_T *virp,
2288 FILE *fp_out,
2289 garray_T *buflist,
2290 int eof,
2291 int flags)
2292{
2293 char_u *line = virp->vir_line;
2294 buf_T *buf;
2295 int num_marked_files;
2296 int load_marks;
2297 int copy_marks_out;
2298 char_u *str;
2299 int i;
2300 char_u *p;
2301 char_u *name_buf;
2302 pos_T pos;
2303#ifdef FEAT_EVAL
2304 list_T *list = NULL;
2305#endif
2306 int count = 0;
2307 int buflist_used = 0;
2308 buf_T *buflist_buf = NULL;
2309
2310 if ((name_buf = alloc(LSIZE)) == NULL)
2311 return;
2312 *name_buf = NUL;
2313
2314 if (fp_out != NULL && buflist->ga_len > 0)
2315 {
2316 // Sort the list of buffers on b_last_used.
2317 qsort(buflist->ga_data, (size_t)buflist->ga_len,
2318 sizeof(buf_T *), buf_compare);
2319 buflist_buf = ((buf_T **)buflist->ga_data)[0];
2320 }
2321
2322#ifdef FEAT_EVAL
2323 if (fp_out == NULL && (flags & (VIF_GET_OLDFILES | VIF_FORCEIT)))
2324 {
2325 list = list_alloc();
2326 if (list != NULL)
2327 set_vim_var_list(VV_OLDFILES, list);
2328 }
2329#endif
2330
2331 num_marked_files = get_viminfo_parameter('\'');
2332 while (!eof && (count < num_marked_files || fp_out == NULL))
2333 {
2334 if (line[0] != '>')
2335 {
2336 if (line[0] != '\n' && line[0] != '\r' && line[0] != '#')
2337 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002338 if (viminfo_error("E576: ", _(e_nonr_missing_gt), line))
Bram Moolenaarc3328162019-07-23 22:15:25 +02002339 break; // too many errors, return now
2340 }
2341 eof = vim_fgets(line, LSIZE, virp->vir_fd);
2342 continue; // Skip this dud line
2343 }
2344
2345 // Handle long line and translate escaped characters.
2346 // Find file name, set str to start.
2347 // Ignore leading and trailing white space.
2348 str = skipwhite(line + 1);
2349 str = viminfo_readstring(virp, (int)(str - virp->vir_line), FALSE);
2350 if (str == NULL)
2351 continue;
2352 p = str + STRLEN(str);
2353 while (p != str && (*p == NUL || vim_isspace(*p)))
2354 p--;
2355 if (*p)
2356 p++;
2357 *p = NUL;
2358
2359#ifdef FEAT_EVAL
2360 if (list != NULL)
2361 list_append_string(list, str, -1);
2362#endif
2363
2364 // If fp_out == NULL, load marks for current buffer.
2365 // If fp_out != NULL, copy marks for buffers not in buflist.
2366 load_marks = copy_marks_out = FALSE;
2367 if (fp_out == NULL)
2368 {
2369 if ((flags & VIF_WANT_MARKS) && curbuf->b_ffname != NULL)
2370 {
2371 if (*name_buf == NUL) // only need to do this once
2372 home_replace(NULL, curbuf->b_ffname, name_buf, LSIZE, TRUE);
2373 if (fnamecmp(str, name_buf) == 0)
2374 load_marks = TRUE;
2375 }
2376 }
2377 else // fp_out != NULL
2378 {
2379 // This is slow if there are many buffers!!
2380 FOR_ALL_BUFFERS(buf)
2381 if (buf->b_ffname != NULL)
2382 {
2383 home_replace(NULL, buf->b_ffname, name_buf, LSIZE, TRUE);
2384 if (fnamecmp(str, name_buf) == 0)
2385 break;
2386 }
2387
2388 // Copy marks if the buffer has not been loaded.
2389 if (buf == NULL || !buf->b_marks_read)
2390 {
2391 int did_read_line = FALSE;
2392
2393 if (buflist_buf != NULL)
2394 {
2395 // Read the next line. If it has the "*" mark compare the
2396 // time stamps. Write entries from "buflist" that are
2397 // newer.
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01002398 if (!viminfo_readline(virp) && line[0] == TAB)
Bram Moolenaarc3328162019-07-23 22:15:25 +02002399 {
2400 did_read_line = TRUE;
2401 if (line[1] == '*')
2402 {
2403 long ltime;
2404
2405 sscanf((char *)line + 2, "%ld ", &ltime);
2406 while ((time_T)ltime < buflist_buf->b_last_used)
2407 {
2408 write_buffer_marks(buflist_buf, fp_out);
2409 if (++count >= num_marked_files)
2410 break;
2411 if (++buflist_used == buflist->ga_len)
2412 {
2413 buflist_buf = NULL;
2414 break;
2415 }
2416 buflist_buf =
2417 ((buf_T **)buflist->ga_data)[buflist_used];
2418 }
2419 }
2420 else
2421 {
2422 // No timestamp, must be written by an older Vim.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002423 // Assume all remaining buffers are older than
Bram Moolenaarc3328162019-07-23 22:15:25 +02002424 // ours.
2425 while (count < num_marked_files
2426 && buflist_used < buflist->ga_len)
2427 {
2428 buflist_buf = ((buf_T **)buflist->ga_data)
2429 [buflist_used++];
2430 write_buffer_marks(buflist_buf, fp_out);
2431 ++count;
2432 }
2433 buflist_buf = NULL;
2434 }
2435
2436 if (count >= num_marked_files)
2437 {
2438 vim_free(str);
2439 break;
2440 }
2441 }
2442 }
2443
2444 fputs("\n> ", fp_out);
2445 viminfo_writestring(fp_out, str);
2446 if (did_read_line)
2447 fputs((char *)line, fp_out);
2448
2449 count++;
2450 copy_marks_out = TRUE;
2451 }
2452 }
2453 vim_free(str);
2454
2455 pos.coladd = 0;
2456 while (!(eof = viminfo_readline(virp)) && line[0] == TAB)
2457 {
2458 if (load_marks)
2459 {
2460 if (line[1] != NUL)
2461 {
2462 unsigned u;
2463
2464 sscanf((char *)line + 2, "%ld %u", &pos.lnum, &u);
2465 pos.col = u;
2466 switch (line[1])
2467 {
2468 case '"': curbuf->b_last_cursor = pos; break;
2469 case '^': curbuf->b_last_insert = pos; break;
2470 case '.': curbuf->b_last_change = pos; break;
2471 case '+':
Bram Moolenaarc3328162019-07-23 22:15:25 +02002472 // changelist positions are stored oldest
2473 // first
2474 if (curbuf->b_changelistlen == JUMPLISTSIZE)
2475 // list is full, remove oldest entry
2476 mch_memmove(curbuf->b_changelist,
2477 curbuf->b_changelist + 1,
2478 sizeof(pos_T) * (JUMPLISTSIZE - 1));
2479 else
2480 ++curbuf->b_changelistlen;
2481 curbuf->b_changelist[
2482 curbuf->b_changelistlen - 1] = pos;
Bram Moolenaarc3328162019-07-23 22:15:25 +02002483 break;
2484
2485 // Using the line number for the last-used
2486 // timestamp.
2487 case '*': curbuf->b_last_used = pos.lnum; break;
2488
2489 default: if ((i = line[1] - 'a') >= 0 && i < NMARKS)
2490 curbuf->b_namedm[i] = pos;
2491 }
2492 }
2493 }
2494 else if (copy_marks_out)
2495 fputs((char *)line, fp_out);
2496 }
2497
2498 if (load_marks)
2499 {
Bram Moolenaarc3328162019-07-23 22:15:25 +02002500 win_T *wp;
2501
2502 FOR_ALL_WINDOWS(wp)
2503 {
2504 if (wp->w_buffer == curbuf)
2505 wp->w_changelistidx = curbuf->b_changelistlen;
2506 }
Bram Moolenaar3ff656f2021-02-10 19:22:15 +01002507 if (flags & VIF_ONLY_CURBUF)
2508 break;
Bram Moolenaarc3328162019-07-23 22:15:25 +02002509 }
2510 }
2511
2512 if (fp_out != NULL)
2513 // Write any remaining entries from buflist.
2514 while (count < num_marked_files && buflist_used < buflist->ga_len)
2515 {
2516 buflist_buf = ((buf_T **)buflist->ga_data)[buflist_used++];
2517 write_buffer_marks(buflist_buf, fp_out);
2518 ++count;
2519 }
2520
2521 vim_free(name_buf);
2522}
2523
2524/*
2525 * Read marks for the current buffer from the viminfo file, when we support
2526 * buffer marks and the buffer has a name.
2527 */
2528 void
2529check_marks_read(void)
2530{
2531 if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0
2532 && curbuf->b_ffname != NULL)
Bram Moolenaar3ff656f2021-02-10 19:22:15 +01002533 read_viminfo(NULL, VIF_WANT_MARKS | VIF_ONLY_CURBUF);
Bram Moolenaarc3328162019-07-23 22:15:25 +02002534
2535 // Always set b_marks_read; needed when 'viminfo' is changed to include
2536 // the ' parameter after opening a buffer.
2537 curbuf->b_marks_read = TRUE;
2538}
2539
2540 static int
2541read_viminfo_filemark(vir_T *virp, int force)
2542{
2543 char_u *str;
2544 xfmark_T *namedfm_p = get_namedfm();
2545 xfmark_T *fm;
2546 int i;
2547
2548 // We only get here if line[0] == '\'' or '-'.
2549 // Illegal mark names are ignored (for future expansion).
2550 str = virp->vir_line + 1;
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002551 if (*str <= 127
2552 && ((*virp->vir_line == '\''
Keith Thompson184f71c2024-01-04 21:19:04 +01002553 && (VIM_ISDIGIT(*str) || SAFE_isupper(*str)))
Bram Moolenaarc3328162019-07-23 22:15:25 +02002554 || (*virp->vir_line == '-' && *str == '\'')))
2555 {
2556 if (*str == '\'')
2557 {
Bram Moolenaarc3328162019-07-23 22:15:25 +02002558 // If the jumplist isn't full insert fmark as oldest entry
2559 if (curwin->w_jumplistlen == JUMPLISTSIZE)
2560 fm = NULL;
2561 else
2562 {
2563 for (i = curwin->w_jumplistlen; i > 0; --i)
2564 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
2565 ++curwin->w_jumplistidx;
2566 ++curwin->w_jumplistlen;
2567 fm = &curwin->w_jumplist[0];
2568 fm->fmark.mark.lnum = 0;
2569 fm->fname = NULL;
2570 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02002571 }
2572 else if (VIM_ISDIGIT(*str))
2573 fm = &namedfm_p[*str - '0' + NMARKS];
2574 else
2575 fm = &namedfm_p[*str - 'A'];
2576 if (fm != NULL && (fm->fmark.mark.lnum == 0 || force))
2577 {
2578 str = skipwhite(str + 1);
2579 fm->fmark.mark.lnum = getdigits(&str);
2580 str = skipwhite(str);
2581 fm->fmark.mark.col = getdigits(&str);
2582 fm->fmark.mark.coladd = 0;
2583 fm->fmark.fnum = 0;
2584 str = skipwhite(str);
2585 vim_free(fm->fname);
2586 fm->fname = viminfo_readstring(virp, (int)(str - virp->vir_line),
2587 FALSE);
2588 fm->time_set = 0;
2589 }
2590 }
2591 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
2592}
2593
2594/*
2595 * Prepare for reading viminfo marks when writing viminfo later.
2596 */
2597 static void
2598prepare_viminfo_marks(void)
2599{
2600 vi_namedfm = ALLOC_CLEAR_MULT(xfmark_T, NMARKS + EXTRA_MARKS);
Bram Moolenaarc3328162019-07-23 22:15:25 +02002601 vi_jumplist = ALLOC_CLEAR_MULT(xfmark_T, JUMPLISTSIZE);
2602 vi_jumplist_len = 0;
Bram Moolenaarc3328162019-07-23 22:15:25 +02002603}
2604
2605 static void
2606finish_viminfo_marks(void)
2607{
2608 int i;
2609
2610 if (vi_namedfm != NULL)
2611 {
2612 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
2613 vim_free(vi_namedfm[i].fname);
2614 VIM_CLEAR(vi_namedfm);
2615 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02002616 if (vi_jumplist != NULL)
2617 {
2618 for (i = 0; i < vi_jumplist_len; ++i)
2619 vim_free(vi_jumplist[i].fname);
2620 VIM_CLEAR(vi_jumplist);
2621 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02002622}
2623
2624/*
2625 * Accept a new style mark line from the viminfo, store it when it's new.
2626 */
2627 static void
2628handle_viminfo_mark(garray_T *values, int force)
2629{
2630 bval_T *vp = (bval_T *)values->ga_data;
2631 int name;
2632 linenr_T lnum;
2633 colnr_T col;
2634 time_t timestamp;
2635 xfmark_T *fm = NULL;
2636
2637 // Check the format:
2638 // |{bartype},{name},{lnum},{col},{timestamp},{filename}
2639 if (values->ga_len < 5
2640 || vp[0].bv_type != BVAL_NR
2641 || vp[1].bv_type != BVAL_NR
2642 || vp[2].bv_type != BVAL_NR
2643 || vp[3].bv_type != BVAL_NR
2644 || vp[4].bv_type != BVAL_STRING)
2645 return;
2646
2647 name = vp[0].bv_nr;
2648 if (name != '\'' && !VIM_ISDIGIT(name) && !ASCII_ISUPPER(name))
2649 return;
2650 lnum = vp[1].bv_nr;
2651 col = vp[2].bv_nr;
2652 if (lnum <= 0 || col < 0)
2653 return;
2654 timestamp = (time_t)vp[3].bv_nr;
2655
2656 if (name == '\'')
2657 {
Bram Moolenaarc3328162019-07-23 22:15:25 +02002658 if (vi_jumplist != NULL)
2659 {
2660 if (vi_jumplist_len < JUMPLISTSIZE)
2661 fm = &vi_jumplist[vi_jumplist_len++];
2662 }
2663 else
2664 {
2665 int idx;
2666 int i;
2667
2668 // If we have a timestamp insert it in the right place.
2669 if (timestamp != 0)
2670 {
2671 for (idx = curwin->w_jumplistlen - 1; idx >= 0; --idx)
2672 if (curwin->w_jumplist[idx].time_set < timestamp)
2673 {
2674 ++idx;
2675 break;
2676 }
2677 // idx cannot be zero now
2678 if (idx < 0 && curwin->w_jumplistlen < JUMPLISTSIZE)
2679 // insert as the oldest entry
2680 idx = 0;
2681 }
2682 else if (curwin->w_jumplistlen < JUMPLISTSIZE)
2683 // insert as oldest entry
2684 idx = 0;
2685 else
2686 idx = -1;
2687
2688 if (idx >= 0)
2689 {
2690 if (curwin->w_jumplistlen == JUMPLISTSIZE)
2691 {
2692 // Drop the oldest entry.
2693 --idx;
2694 vim_free(curwin->w_jumplist[0].fname);
2695 for (i = 0; i < idx; ++i)
2696 curwin->w_jumplist[i] = curwin->w_jumplist[i + 1];
2697 }
2698 else
2699 {
2700 // Move newer entries forward.
2701 for (i = curwin->w_jumplistlen; i > idx; --i)
2702 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
2703 ++curwin->w_jumplistidx;
2704 ++curwin->w_jumplistlen;
2705 }
2706 fm = &curwin->w_jumplist[idx];
2707 fm->fmark.mark.lnum = 0;
2708 fm->fname = NULL;
2709 fm->time_set = 0;
2710 }
2711 }
Bram Moolenaarc3328162019-07-23 22:15:25 +02002712 }
2713 else
2714 {
2715 int idx;
2716 xfmark_T *namedfm_p = get_namedfm();
2717
2718 if (VIM_ISDIGIT(name))
2719 {
2720 if (vi_namedfm != NULL)
2721 idx = name - '0' + NMARKS;
2722 else
2723 {
2724 int i;
2725
2726 // Do not use the name from the viminfo file, insert in time
2727 // order.
2728 for (idx = NMARKS; idx < NMARKS + EXTRA_MARKS; ++idx)
2729 if (namedfm_p[idx].time_set < timestamp)
2730 break;
2731 if (idx == NMARKS + EXTRA_MARKS)
2732 // All existing entries are newer.
2733 return;
2734 i = NMARKS + EXTRA_MARKS - 1;
2735
2736 vim_free(namedfm_p[i].fname);
2737 for ( ; i > idx; --i)
2738 namedfm_p[i] = namedfm_p[i - 1];
2739 namedfm_p[idx].fname = NULL;
2740 }
2741 }
2742 else
2743 idx = name - 'A';
2744 if (vi_namedfm != NULL)
2745 fm = &vi_namedfm[idx];
2746 else
2747 fm = &namedfm_p[idx];
2748 }
2749
2750 if (fm != NULL)
2751 {
2752 if (vi_namedfm != NULL || fm->fmark.mark.lnum == 0
2753 || fm->time_set < timestamp || force)
2754 {
2755 fm->fmark.mark.lnum = lnum;
2756 fm->fmark.mark.col = col;
2757 fm->fmark.mark.coladd = 0;
2758 fm->fmark.fnum = 0;
2759 vim_free(fm->fname);
2760 if (vp[4].bv_allocated)
2761 {
2762 fm->fname = vp[4].bv_string;
2763 vp[4].bv_string = NULL;
2764 }
2765 else
2766 fm->fname = vim_strsave(vp[4].bv_string);
2767 fm->time_set = timestamp;
2768 }
2769 }
2770}
2771
2772 static int
2773read_viminfo_barline(vir_T *virp, int got_encoding, int force, int writing)
2774{
2775 char_u *p = virp->vir_line + 1;
2776 int bartype;
2777 garray_T values;
2778 bval_T *vp;
2779 int i;
2780 int read_next = TRUE;
2781
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02002782 // The format is: |{bartype},{value},...
2783 // For a very long string:
2784 // |{bartype},>{length of "{text}{text2}"}
2785 // |<{text1}
2786 // |<{text2},{value}
2787 // For a long line not using a string
2788 // |{bartype},{lots of values},>
2789 // |<{value},{value}
Bram Moolenaarc3328162019-07-23 22:15:25 +02002790 if (*p == '<')
2791 {
2792 // Continuation line of an unrecognized item.
2793 if (writing)
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00002794 ga_copy_string(&virp->vir_barlines, virp->vir_line);
Bram Moolenaarc3328162019-07-23 22:15:25 +02002795 }
2796 else
2797 {
2798 ga_init2(&values, sizeof(bval_T), 20);
2799 bartype = getdigits(&p);
2800 switch (bartype)
2801 {
2802 case BARTYPE_VERSION:
2803 // Only use the version when it comes before the encoding.
2804 // If it comes later it was copied by a Vim version that
2805 // doesn't understand the version.
2806 if (!got_encoding)
2807 {
2808 read_next = barline_parse(virp, p, &values);
2809 vp = (bval_T *)values.ga_data;
2810 if (values.ga_len > 0 && vp->bv_type == BVAL_NR)
2811 virp->vir_version = vp->bv_nr;
2812 }
2813 break;
2814
2815 case BARTYPE_HISTORY:
2816 read_next = barline_parse(virp, p, &values);
2817 handle_viminfo_history(&values, writing);
2818 break;
2819
2820 case BARTYPE_REGISTER:
2821 read_next = barline_parse(virp, p, &values);
2822 handle_viminfo_register(&values, force);
2823 break;
2824
2825 case BARTYPE_MARK:
2826 read_next = barline_parse(virp, p, &values);
2827 handle_viminfo_mark(&values, force);
2828 break;
2829
2830 default:
2831 // copy unrecognized line (for future use)
2832 if (writing)
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00002833 ga_copy_string(&virp->vir_barlines, virp->vir_line);
Bram Moolenaarc3328162019-07-23 22:15:25 +02002834 }
2835 for (i = 0; i < values.ga_len; ++i)
2836 {
2837 vp = (bval_T *)values.ga_data + i;
2838 if (vp->bv_type == BVAL_STRING && vp->bv_allocated)
2839 vim_free(vp->bv_string);
Bram Moolenaar408030e2020-02-10 22:44:32 +01002840 vim_free(vp->bv_tofree);
Bram Moolenaarc3328162019-07-23 22:15:25 +02002841 }
2842 ga_clear(&values);
2843 }
2844
2845 if (read_next)
2846 return viminfo_readline(virp);
2847 return FALSE;
2848}
2849
Bram Moolenaardefa0672019-07-21 19:25:37 +02002850/*
2851 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the
2852 * first part of the viminfo file which contains everything but the marks that
2853 * are local to a file. Returns TRUE when end-of-file is reached. -- webb
2854 */
2855 static int
2856read_viminfo_up_to_marks(
2857 vir_T *virp,
2858 int forceit,
2859 int writing)
2860{
2861 int eof;
2862 buf_T *buf;
2863 int got_encoding = FALSE;
2864
Bram Moolenaardefa0672019-07-21 19:25:37 +02002865 prepare_viminfo_history(forceit ? 9999 : 0, writing);
Bram Moolenaardefa0672019-07-21 19:25:37 +02002866
2867 eof = viminfo_readline(virp);
2868 while (!eof && virp->vir_line[0] != '>')
2869 {
2870 switch (virp->vir_line[0])
2871 {
2872 // Characters reserved for future expansion, ignored now
2873 case '+': // "+40 /path/dir file", for running vim without args
2874 case '^': // to be defined
2875 case '<': // long line - ignored
2876 // A comment or empty line.
2877 case NUL:
2878 case '\r':
2879 case '\n':
2880 case '#':
2881 eof = viminfo_readline(virp);
2882 break;
2883 case '|':
2884 eof = read_viminfo_barline(virp, got_encoding,
2885 forceit, writing);
2886 break;
2887 case '*': // "*encoding=value"
2888 got_encoding = TRUE;
2889 eof = viminfo_encoding(virp);
2890 break;
2891 case '!': // global variable
2892#ifdef FEAT_EVAL
2893 eof = read_viminfo_varlist(virp, writing);
2894#else
2895 eof = viminfo_readline(virp);
2896#endif
2897 break;
2898 case '%': // entry for buffer list
2899 eof = read_viminfo_bufferlist(virp, writing);
2900 break;
2901 case '"':
2902 // When registers are in bar lines skip the old style register
2903 // lines.
2904 if (virp->vir_version < VIMINFO_VERSION_WITH_REGISTERS)
2905 eof = read_viminfo_register(virp, forceit);
2906 else
Hirohito Higashia4a00a72025-05-08 22:58:31 +02002907 do
2908 {
Bram Moolenaardefa0672019-07-21 19:25:37 +02002909 eof = viminfo_readline(virp);
2910 } while (!eof && (virp->vir_line[0] == TAB
2911 || virp->vir_line[0] == '<'));
2912 break;
2913 case '/': // Search string
2914 case '&': // Substitute search string
2915 case '~': // Last search string, followed by '/' or '&'
2916 eof = read_viminfo_search_pattern(virp, forceit);
2917 break;
2918 case '$':
2919 eof = read_viminfo_sub_string(virp, forceit);
2920 break;
2921 case ':':
2922 case '?':
2923 case '=':
2924 case '@':
Bram Moolenaardefa0672019-07-21 19:25:37 +02002925 // When history is in bar lines skip the old style history
2926 // lines.
2927 if (virp->vir_version < VIMINFO_VERSION_WITH_HISTORY)
2928 eof = read_viminfo_history(virp, writing);
2929 else
Bram Moolenaardefa0672019-07-21 19:25:37 +02002930 eof = viminfo_readline(virp);
2931 break;
2932 case '-':
2933 case '\'':
2934 // When file marks are in bar lines skip the old style lines.
2935 if (virp->vir_version < VIMINFO_VERSION_WITH_MARKS)
2936 eof = read_viminfo_filemark(virp, forceit);
2937 else
2938 eof = viminfo_readline(virp);
2939 break;
2940 default:
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002941 if (viminfo_error("E575: ", _(e_illegal_starting_char),
Bram Moolenaardefa0672019-07-21 19:25:37 +02002942 virp->vir_line))
2943 eof = TRUE;
2944 else
2945 eof = viminfo_readline(virp);
2946 break;
2947 }
2948 }
2949
Bram Moolenaardefa0672019-07-21 19:25:37 +02002950 // Finish reading history items.
2951 if (!writing)
2952 finish_viminfo_history(virp);
Bram Moolenaardefa0672019-07-21 19:25:37 +02002953
2954 // Change file names to buffer numbers for fmarks.
2955 FOR_ALL_BUFFERS(buf)
2956 fmarks_check_names(buf);
2957
2958 return eof;
2959}
2960
2961/*
2962 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo().
2963 */
2964 static void
2965do_viminfo(FILE *fp_in, FILE *fp_out, int flags)
2966{
2967 int eof = FALSE;
2968 vir_T vir;
2969 int merge = FALSE;
2970 int do_copy_marks = FALSE;
2971 garray_T buflist;
2972
2973 if ((vir.vir_line = alloc(LSIZE)) == NULL)
2974 return;
2975 vir.vir_fd = fp_in;
2976 vir.vir_conv.vc_type = CONV_NONE;
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002977 ga_init2(&vir.vir_barlines, sizeof(char_u *), 100);
Bram Moolenaardefa0672019-07-21 19:25:37 +02002978 vir.vir_version = -1;
2979
2980 if (fp_in != NULL)
2981 {
2982 if (flags & VIF_WANT_INFO)
2983 {
2984 if (fp_out != NULL)
2985 {
2986 // Registers and marks are read and kept separate from what
2987 // this Vim is using. They are merged when writing.
2988 prepare_viminfo_registers();
2989 prepare_viminfo_marks();
2990 }
2991
2992 eof = read_viminfo_up_to_marks(&vir,
2993 flags & VIF_FORCEIT, fp_out != NULL);
2994 merge = TRUE;
2995 }
2996 else if (flags != 0)
2997 // Skip info, find start of marks
2998 while (!(eof = viminfo_readline(&vir))
2999 && vir.vir_line[0] != '>')
3000 ;
3001
Bram Moolenaar3ff656f2021-02-10 19:22:15 +01003002 do_copy_marks = (flags & (VIF_WANT_MARKS | VIF_ONLY_CURBUF
3003 | VIF_GET_OLDFILES | VIF_FORCEIT));
Bram Moolenaardefa0672019-07-21 19:25:37 +02003004 }
3005
3006 if (fp_out != NULL)
3007 {
3008 // Write the info:
3009 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"),
3010 VIM_VERSION_MEDIUM);
3011 fputs(_("# You may edit it if you're careful!\n\n"), fp_out);
3012 write_viminfo_version(fp_out);
3013 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out);
3014 fprintf(fp_out, "*encoding=%s\n\n", p_enc);
3015 write_viminfo_search_pattern(fp_out);
3016 write_viminfo_sub_string(fp_out);
Bram Moolenaardefa0672019-07-21 19:25:37 +02003017 write_viminfo_history(fp_out, merge);
Bram Moolenaardefa0672019-07-21 19:25:37 +02003018 write_viminfo_registers(fp_out);
3019 finish_viminfo_registers();
3020#ifdef FEAT_EVAL
3021 write_viminfo_varlist(fp_out);
3022#endif
3023 write_viminfo_filemarks(fp_out);
3024 finish_viminfo_marks();
3025 write_viminfo_bufferlist(fp_out);
3026 write_viminfo_barlines(&vir, fp_out);
3027
3028 if (do_copy_marks)
3029 ga_init2(&buflist, sizeof(buf_T *), 50);
3030 write_viminfo_marks(fp_out, do_copy_marks ? &buflist : NULL);
3031 }
3032
3033 if (do_copy_marks)
3034 {
3035 copy_viminfo_marks(&vir, fp_out, &buflist, eof, flags);
3036 if (fp_out != NULL)
3037 ga_clear(&buflist);
3038 }
3039
3040 vim_free(vir.vir_line);
3041 if (vir.vir_conv.vc_type != CONV_NONE)
3042 convert_setup(&vir.vir_conv, NULL, NULL);
3043 ga_clear_strings(&vir.vir_barlines);
3044}
3045
3046/*
3047 * read_viminfo() -- Read the viminfo file. Registers etc. which are already
3048 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb
3049 */
3050 int
3051read_viminfo(
3052 char_u *file, // file name or NULL to use default name
3053 int flags) // VIF_WANT_INFO et al.
3054{
3055 FILE *fp;
3056 char_u *fname;
Bram Moolenaarb86abad2020-08-01 16:08:19 +02003057 stat_T st; // mch_stat() of existing viminfo file
Bram Moolenaardefa0672019-07-21 19:25:37 +02003058
3059 if (no_viminfo())
3060 return FAIL;
3061
3062 fname = viminfo_filename(file); // get file name in allocated buffer
3063 if (fname == NULL)
3064 return FAIL;
3065 fp = mch_fopen((char *)fname, READBIN);
3066
3067 if (p_verbose > 0)
3068 {
3069 verbose_enter();
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003070 smsg(_("Reading viminfo file \"%s\"%s%s%s%s"),
Bram Moolenaardefa0672019-07-21 19:25:37 +02003071 fname,
3072 (flags & VIF_WANT_INFO) ? _(" info") : "",
3073 (flags & VIF_WANT_MARKS) ? _(" marks") : "",
3074 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "",
3075 fp == NULL ? _(" FAILED") : "");
3076 verbose_leave();
3077 }
3078
3079 vim_free(fname);
3080 if (fp == NULL)
3081 return FAIL;
Bram Moolenaarb86abad2020-08-01 16:08:19 +02003082 if (mch_fstat(fileno(fp), &st) < 0 || S_ISDIR(st.st_mode))
3083 {
3084 fclose(fp);
3085 return FAIL;
3086 }
Bram Moolenaardefa0672019-07-21 19:25:37 +02003087
3088 viminfo_errcnt = 0;
3089 do_viminfo(fp, NULL, flags);
3090
3091 fclose(fp);
3092 return OK;
3093}
3094
3095/*
3096 * Write the viminfo file. The old one is read in first so that effectively a
3097 * merge of current info and old info is done. This allows multiple vims to
3098 * run simultaneously, without losing any marks etc.
3099 * If "forceit" is TRUE, then the old file is not read in, and only internal
3100 * info is written to the file.
3101 */
3102 void
3103write_viminfo(char_u *file, int forceit)
3104{
3105 char_u *fname;
3106 FILE *fp_in = NULL; // input viminfo file, if any
3107 FILE *fp_out = NULL; // output viminfo file
3108 char_u *tempname = NULL; // name of temp viminfo file
3109 stat_T st_new; // mch_stat() of potential new file
Bram Moolenaarb86abad2020-08-01 16:08:19 +02003110 stat_T st_old; // mch_stat() of existing viminfo file
Bram Moolenaardefa0672019-07-21 19:25:37 +02003111#if defined(UNIX) || defined(VMS)
3112 mode_t umask_save;
3113#endif
3114#ifdef UNIX
3115 int shortname = FALSE; // use 8.3 file name
Bram Moolenaardefa0672019-07-21 19:25:37 +02003116#endif
3117#ifdef MSWIN
3118 int hidden = FALSE;
3119#endif
3120
3121 if (no_viminfo())
3122 return;
3123
3124 fname = viminfo_filename(file); // may set to default if NULL
3125 if (fname == NULL)
3126 return;
3127
3128 fp_in = mch_fopen((char *)fname, READBIN);
3129 if (fp_in == NULL)
3130 {
3131 int fd;
3132
3133 // if it does exist, but we can't read it, don't try writing
3134 if (mch_stat((char *)fname, &st_new) == 0)
3135 goto end;
3136
3137 // Create the new .viminfo non-accessible for others, because it may
3138 // contain text from non-accessible documents. It is up to the user to
3139 // widen access (e.g. to a group). This may also fail if there is a
3140 // race condition, then just give up.
3141 fd = mch_open((char *)fname,
3142 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
3143 if (fd < 0)
3144 goto end;
3145 fp_out = fdopen(fd, WRITEBIN);
3146 }
3147 else
3148 {
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003149 // There is an existing viminfo file. Create a temporary file to
3150 // write the new viminfo into, in the same directory as the
3151 // existing viminfo file, which will be renamed once all writing is
3152 // successful.
Bram Moolenaarb86abad2020-08-01 16:08:19 +02003153 if (mch_fstat(fileno(fp_in), &st_old) < 0
3154 || S_ISDIR(st_old.st_mode)
Bram Moolenaardefa0672019-07-21 19:25:37 +02003155#ifdef UNIX
Bram Moolenaarb86abad2020-08-01 16:08:19 +02003156 // For Unix we check the owner of the file. It's not very nice
3157 // to overwrite a user's viminfo file after a "su root", with a
3158 // viminfo file that the user can't read.
3159 || (getuid() != ROOT_UID
3160 && !(st_old.st_uid == getuid()
3161 ? (st_old.st_mode & 0200)
3162 : (st_old.st_gid == getgid()
3163 ? (st_old.st_mode & 0020)
3164 : (st_old.st_mode & 0002))))
3165#endif
3166 )
Bram Moolenaardefa0672019-07-21 19:25:37 +02003167 {
3168 int tt = msg_didany;
3169
Bram Moolenaar13608d82022-08-29 15:06:50 +01003170 // avoid a wait_return() for this message, it's annoying
Bram Moolenaarc553a212021-12-26 20:20:34 +00003171 semsg(_(e_viminfo_file_is_not_writable_str), fname);
Bram Moolenaardefa0672019-07-21 19:25:37 +02003172 msg_didany = tt;
3173 fclose(fp_in);
3174 goto end;
3175 }
Bram Moolenaardefa0672019-07-21 19:25:37 +02003176#ifdef MSWIN
3177 // Get the file attributes of the existing viminfo file.
3178 hidden = mch_ishidden(fname);
3179#endif
3180
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003181 // Make tempname, find one that does not exist yet.
3182 // Beware of a race condition: If someone logs out and all Vim
3183 // instances exit at the same time a temp file might be created between
3184 // stat() and open(). Use mch_open() with O_EXCL to avoid that.
3185 // May try twice: Once normal and once with shortname set, just in
3186 // case somebody puts his viminfo file in an 8.3 filesystem.
Bram Moolenaardefa0672019-07-21 19:25:37 +02003187 for (;;)
3188 {
3189 int next_char = 'z';
3190 char_u *wp;
3191
3192 tempname = buf_modname(
3193#ifdef UNIX
3194 shortname,
3195#else
3196 FALSE,
3197#endif
3198 fname,
3199#ifdef VMS
3200 (char_u *)"-tmp",
3201#else
3202 (char_u *)".tmp",
3203#endif
3204 FALSE);
3205 if (tempname == NULL) // out of memory
3206 break;
3207
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003208 // Try a series of names. Change one character, just before
3209 // the extension. This should also work for an 8.3
3210 // file name, when after adding the extension it still is
3211 // the same file as the original.
Bram Moolenaardefa0672019-07-21 19:25:37 +02003212 wp = tempname + STRLEN(tempname) - 5;
3213 if (wp < gettail(tempname)) // empty file name?
3214 wp = gettail(tempname);
3215 for (;;)
3216 {
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003217 // Check if tempfile already exists. Never overwrite an
3218 // existing file!
Bram Moolenaardefa0672019-07-21 19:25:37 +02003219 if (mch_stat((char *)tempname, &st_new) == 0)
3220 {
3221#ifdef UNIX
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003222 // Check if tempfile is same as original file. May happen
3223 // when modname() gave the same file back. E.g. silly
3224 // link, or file name-length reached. Try again with
3225 // shortname set.
Bram Moolenaardefa0672019-07-21 19:25:37 +02003226 if (!shortname && st_new.st_dev == st_old.st_dev
3227 && st_new.st_ino == st_old.st_ino)
3228 {
3229 VIM_CLEAR(tempname);
3230 shortname = TRUE;
3231 break;
3232 }
3233#endif
3234 }
3235 else
3236 {
3237 // Try creating the file exclusively. This may fail if
3238 // another Vim tries to do it at the same time.
3239#ifdef VMS
3240 // fdopen() fails for some reason
3241 umask_save = umask(077);
3242 fp_out = mch_fopen((char *)tempname, WRITEBIN);
3243 (void)umask(umask_save);
3244#else
3245 int fd;
3246
3247 // Use mch_open() to be able to use O_NOFOLLOW and set file
3248 // protection:
3249 // Unix: same as original file, but strip s-bit. Reset
3250 // umask to avoid it getting in the way.
3251 // Others: r&w for user only.
3252# ifdef UNIX
3253 umask_save = umask(0);
3254 fd = mch_open((char *)tempname,
3255 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW,
3256 (int)((st_old.st_mode & 0777) | 0600));
3257 (void)umask(umask_save);
3258# else
3259 fd = mch_open((char *)tempname,
3260 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
3261# endif
3262 if (fd < 0)
3263 {
3264 fp_out = NULL;
3265# ifdef EEXIST
3266 // Avoid trying lots of names while the problem is lack
3267 // of permission, only retry if the file already
3268 // exists.
3269 if (errno != EEXIST)
3270 break;
3271# endif
3272 }
3273 else
3274 fp_out = fdopen(fd, WRITEBIN);
3275#endif // VMS
3276 if (fp_out != NULL)
3277 break;
3278 }
3279
3280 // Assume file exists, try again with another name.
3281 if (next_char == 'a' - 1)
3282 {
3283 // They all exist? Must be something wrong! Don't write
3284 // the viminfo file then.
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003285 semsg(_(e_too_many_viminfo_temp_files_like_str), tempname);
Bram Moolenaardefa0672019-07-21 19:25:37 +02003286 break;
3287 }
3288 *wp = next_char;
3289 --next_char;
3290 }
3291
3292 if (tempname != NULL)
3293 break;
3294 // continue if shortname was set
3295 }
3296
3297#if defined(UNIX) && defined(HAVE_FCHOWN)
3298 if (tempname != NULL && fp_out != NULL)
3299 {
3300 stat_T tmp_st;
3301
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003302 // Make sure the original owner can read/write the tempfile and
3303 // otherwise preserve permissions, making sure the group matches.
Bram Moolenaardefa0672019-07-21 19:25:37 +02003304 if (mch_stat((char *)tempname, &tmp_st) >= 0)
3305 {
3306 if (st_old.st_uid != tmp_st.st_uid)
3307 // Changing the owner might fail, in which case the
Bram Moolenaar32aa1022019-11-02 22:54:41 +01003308 // file will now be owned by the current user, oh well.
Bram Moolenaardefa0672019-07-21 19:25:37 +02003309 vim_ignored = fchown(fileno(fp_out), st_old.st_uid, -1);
3310 if (st_old.st_gid != tmp_st.st_gid
3311 && fchown(fileno(fp_out), -1, st_old.st_gid) == -1)
3312 // can't set the group to what it should be, remove
3313 // group permissions
3314 (void)mch_setperm(tempname, 0600);
3315 }
3316 else
3317 // can't stat the file, set conservative permissions
3318 (void)mch_setperm(tempname, 0600);
3319 }
3320#endif
3321 }
3322
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003323 // Check if the new viminfo file can be written to.
Bram Moolenaardefa0672019-07-21 19:25:37 +02003324 if (fp_out == NULL)
3325 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00003326 semsg(_(e_cant_write_viminfo_file_str),
Bram Moolenaardefa0672019-07-21 19:25:37 +02003327 (fp_in == NULL || tempname == NULL) ? fname : tempname);
3328 if (fp_in != NULL)
3329 fclose(fp_in);
3330 goto end;
3331 }
3332
3333 if (p_verbose > 0)
3334 {
3335 verbose_enter();
3336 smsg(_("Writing viminfo file \"%s\""), fname);
3337 verbose_leave();
3338 }
3339
3340 viminfo_errcnt = 0;
3341 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS));
3342
3343 if (fclose(fp_out) == EOF)
3344 ++viminfo_errcnt;
3345
3346 if (fp_in != NULL)
3347 {
3348 fclose(fp_in);
3349
3350 // In case of an error keep the original viminfo file. Otherwise
3351 // rename the newly written file. Give an error if that fails.
3352 if (viminfo_errcnt == 0)
3353 {
3354 if (vim_rename(tempname, fname) == -1)
3355 {
3356 ++viminfo_errcnt;
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003357 semsg(_(e_cant_rename_viminfo_file_to_str), fname);
Bram Moolenaardefa0672019-07-21 19:25:37 +02003358 }
3359# ifdef MSWIN
3360 // If the viminfo file was hidden then also hide the new file.
3361 else if (hidden)
3362 mch_hide(fname);
3363# endif
3364 }
3365 if (viminfo_errcnt > 0)
3366 mch_remove(tempname);
3367 }
3368
3369end:
3370 vim_free(fname);
3371 vim_free(tempname);
3372}
3373
3374/*
Bram Moolenaardefa0672019-07-21 19:25:37 +02003375 * ":rviminfo" and ":wviminfo".
3376 */
3377 void
3378ex_viminfo(
3379 exarg_T *eap)
3380{
3381 char_u *save_viminfo;
3382
3383 save_viminfo = p_viminfo;
3384 if (*p_viminfo == NUL)
3385 p_viminfo = (char_u *)"'100";
3386 if (eap->cmdidx == CMD_rviminfo)
3387 {
3388 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
3389 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003390 emsg(_(e_cannot_open_viminfo_file_for_reading));
Bram Moolenaardefa0672019-07-21 19:25:37 +02003391 }
3392 else
3393 write_viminfo(eap->arg, eap->forceit);
3394 p_viminfo = save_viminfo;
3395}
3396
Bram Moolenaardefa0672019-07-21 19:25:37 +02003397#endif // FEAT_VIMINFO