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