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