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