comparison src/eval.c @ 17458:cfdef48743ed v8.1.1727

patch 8.1.1727: code for viminfo support is spread out commit https://github.com/vim/vim/commit/defa067c54874dd987121dd7252c62755e0aebfa Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jul 21 19:25:37 2019 +0200 patch 8.1.1727: code for viminfo support is spread out Problem: Code for viminfo support is spread out. Solution: Move to code to viminfo.c. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/4686)
author Bram Moolenaar <Bram@vim.org>
date Sun, 21 Jul 2019 19:30:06 +0200
parents 509542f1fffb
children e00d12c085a5
comparison
equal deleted inserted replaced
17457:3a368ff28a0b 17458:cfdef48743ed
34 #endif 34 #endif
35 35
36 #define NAMESPACE_CHAR (char_u *)"abglstvw" 36 #define NAMESPACE_CHAR (char_u *)"abglstvw"
37 37
38 static dictitem_T globvars_var; /* variable used for g: */ 38 static dictitem_T globvars_var; /* variable used for g: */
39 #define globvarht globvardict.dv_hashtab
40 39
41 /* 40 /*
42 * Old Vim variables such as "v:version" are also available without the "v:". 41 * Old Vim variables such as "v:version" are also available without the "v:".
43 * Also in functions. We need a special hashtable for them. 42 * Also in functions. We need a special hashtable for them.
44 */ 43 */
9319 vim_free(tofree); 9318 vim_free(tofree);
9320 return ret; 9319 return ret;
9321 } 9320 }
9322 9321
9323 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION) 9322 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
9324 typedef enum 9323 var_flavour_T
9325 {
9326 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
9327 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
9328 VAR_FLAVOUR_VIMINFO /* all uppercase */
9329 } var_flavour_T;
9330
9331 static var_flavour_T
9332 var_flavour(char_u *varname) 9324 var_flavour(char_u *varname)
9333 { 9325 {
9334 char_u *p = varname; 9326 char_u *p = varname;
9335 9327
9336 if (ASCII_ISUPPER(*p)) 9328 if (ASCII_ISUPPER(*p))
9340 return VAR_FLAVOUR_SESSION; 9332 return VAR_FLAVOUR_SESSION;
9341 return VAR_FLAVOUR_VIMINFO; 9333 return VAR_FLAVOUR_VIMINFO;
9342 } 9334 }
9343 else 9335 else
9344 return VAR_FLAVOUR_DEFAULT; 9336 return VAR_FLAVOUR_DEFAULT;
9345 }
9346 #endif
9347
9348 #if defined(FEAT_VIMINFO) || defined(PROTO)
9349 /*
9350 * Restore global vars that start with a capital from the viminfo file
9351 */
9352 int
9353 read_viminfo_varlist(vir_T *virp, int writing)
9354 {
9355 char_u *tab;
9356 int type = VAR_NUMBER;
9357 typval_T tv;
9358 funccal_entry_T funccal_entry;
9359
9360 if (!writing && (find_viminfo_parameter('!') != NULL))
9361 {
9362 tab = vim_strchr(virp->vir_line + 1, '\t');
9363 if (tab != NULL)
9364 {
9365 *tab++ = '\0'; /* isolate the variable name */
9366 switch (*tab)
9367 {
9368 case 'S': type = VAR_STRING; break;
9369 #ifdef FEAT_FLOAT
9370 case 'F': type = VAR_FLOAT; break;
9371 #endif
9372 case 'D': type = VAR_DICT; break;
9373 case 'L': type = VAR_LIST; break;
9374 case 'B': type = VAR_BLOB; break;
9375 case 'X': type = VAR_SPECIAL; break;
9376 }
9377
9378 tab = vim_strchr(tab, '\t');
9379 if (tab != NULL)
9380 {
9381 tv.v_type = type;
9382 if (type == VAR_STRING || type == VAR_DICT
9383 || type == VAR_LIST || type == VAR_BLOB)
9384 tv.vval.v_string = viminfo_readstring(virp,
9385 (int)(tab - virp->vir_line + 1), TRUE);
9386 #ifdef FEAT_FLOAT
9387 else if (type == VAR_FLOAT)
9388 (void)string2float(tab + 1, &tv.vval.v_float);
9389 #endif
9390 else
9391 tv.vval.v_number = atol((char *)tab + 1);
9392 if (type == VAR_DICT || type == VAR_LIST)
9393 {
9394 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9395
9396 if (etv == NULL)
9397 /* Failed to parse back the dict or list, use it as a
9398 * string. */
9399 tv.v_type = VAR_STRING;
9400 else
9401 {
9402 vim_free(tv.vval.v_string);
9403 tv = *etv;
9404 vim_free(etv);
9405 }
9406 }
9407 else if (type == VAR_BLOB)
9408 {
9409 blob_T *blob = string2blob(tv.vval.v_string);
9410
9411 if (blob == NULL)
9412 // Failed to parse back the blob, use it as a string.
9413 tv.v_type = VAR_STRING;
9414 else
9415 {
9416 vim_free(tv.vval.v_string);
9417 tv.v_type = VAR_BLOB;
9418 tv.vval.v_blob = blob;
9419 }
9420 }
9421
9422 /* when in a function use global variables */
9423 save_funccal(&funccal_entry);
9424 set_var(virp->vir_line + 1, &tv, FALSE);
9425 restore_funccal();
9426
9427 if (tv.v_type == VAR_STRING)
9428 vim_free(tv.vval.v_string);
9429 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9430 tv.v_type == VAR_BLOB)
9431 clear_tv(&tv);
9432 }
9433 }
9434 }
9435
9436 return viminfo_readline(virp);
9437 }
9438
9439 /*
9440 * Write global vars that start with a capital to the viminfo file
9441 */
9442 void
9443 write_viminfo_varlist(FILE *fp)
9444 {
9445 hashitem_T *hi;
9446 dictitem_T *this_var;
9447 int todo;
9448 char *s = "";
9449 char_u *p;
9450 char_u *tofree;
9451 char_u numbuf[NUMBUFLEN];
9452
9453 if (find_viminfo_parameter('!') == NULL)
9454 return;
9455
9456 fputs(_("\n# global variables:\n"), fp);
9457
9458 todo = (int)globvarht.ht_used;
9459 for (hi = globvarht.ht_array; todo > 0; ++hi)
9460 {
9461 if (!HASHITEM_EMPTY(hi))
9462 {
9463 --todo;
9464 this_var = HI2DI(hi);
9465 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
9466 {
9467 switch (this_var->di_tv.v_type)
9468 {
9469 case VAR_STRING: s = "STR"; break;
9470 case VAR_NUMBER: s = "NUM"; break;
9471 case VAR_FLOAT: s = "FLO"; break;
9472 case VAR_DICT: s = "DIC"; break;
9473 case VAR_LIST: s = "LIS"; break;
9474 case VAR_BLOB: s = "BLO"; break;
9475 case VAR_SPECIAL: s = "XPL"; break;
9476
9477 case VAR_UNKNOWN:
9478 case VAR_FUNC:
9479 case VAR_PARTIAL:
9480 case VAR_JOB:
9481 case VAR_CHANNEL:
9482 continue;
9483 }
9484 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
9485 if (this_var->di_tv.v_type == VAR_SPECIAL)
9486 {
9487 sprintf((char *)numbuf, "%ld",
9488 (long)this_var->di_tv.vval.v_number);
9489 p = numbuf;
9490 tofree = NULL;
9491 }
9492 else
9493 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
9494 if (p != NULL)
9495 viminfo_writestring(fp, p);
9496 vim_free(tofree);
9497 }
9498 }
9499 }
9500 } 9337 }
9501 #endif 9338 #endif
9502 9339
9503 #if defined(FEAT_SESSION) || defined(PROTO) 9340 #if defined(FEAT_SESSION) || defined(PROTO)
9504 int 9341 int