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