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