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