comparison src/evalvars.c @ 17986:5c8906f653f5 v8.1.1989

patch 8.1.1989: the evalfunc.c file is still too big Commit: https://github.com/vim/vim/commit/af7645d3733fdd3cd2df03ec7b653601d26969ef Author: Bram Moolenaar <Bram@vim.org> Date: Thu Sep 5 22:33:28 2019 +0200 patch 8.1.1989: the evalfunc.c file is still too big Problem: The evalfunc.c file is still too big. Solution: Move f_pathshorten() to filepath.c. Move f_cscope_connection() to if_cscope.c. Move diff_ functions to diff.c. Move timer_ functions to ex_cmds2.c. move callback functions to evalvars.c.
author Bram Moolenaar <Bram@vim.org>
date Thu, 05 Sep 2019 22:45:04 +0200
parents 745c02392844
children 9ea364ccf216
comparison
equal deleted inserted replaced
17985:9b43688b26bf 17986:5c8906f653f5
3465 } 3465 }
3466 } 3466 }
3467 } 3467 }
3468 } 3468 }
3469 3469
3470 /*
3471 * Get a callback from "arg". It can be a Funcref or a function name.
3472 * When "arg" is zero return an empty string.
3473 * "cb_name" is not allocated.
3474 * "cb_name" is set to NULL for an invalid argument.
3475 */
3476 callback_T
3477 get_callback(typval_T *arg)
3478 {
3479 callback_T res;
3480
3481 res.cb_free_name = FALSE;
3482 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3483 {
3484 res.cb_partial = arg->vval.v_partial;
3485 ++res.cb_partial->pt_refcount;
3486 res.cb_name = partial_name(res.cb_partial);
3487 }
3488 else
3489 {
3490 res.cb_partial = NULL;
3491 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
3492 {
3493 // Note that we don't make a copy of the string.
3494 res.cb_name = arg->vval.v_string;
3495 func_ref(res.cb_name);
3496 }
3497 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
3498 {
3499 res.cb_name = (char_u *)"";
3500 }
3501 else
3502 {
3503 emsg(_("E921: Invalid callback argument"));
3504 res.cb_name = NULL;
3505 }
3506 }
3507 return res;
3508 }
3509
3510 /*
3511 * Copy a callback into a typval_T.
3512 */
3513 void
3514 put_callback(callback_T *cb, typval_T *tv)
3515 {
3516 if (cb->cb_partial != NULL)
3517 {
3518 tv->v_type = VAR_PARTIAL;
3519 tv->vval.v_partial = cb->cb_partial;
3520 ++tv->vval.v_partial->pt_refcount;
3521 }
3522 else
3523 {
3524 tv->v_type = VAR_FUNC;
3525 tv->vval.v_string = vim_strsave(cb->cb_name);
3526 func_ref(cb->cb_name);
3527 }
3528 }
3529
3530 /*
3531 * Make a copy of "src" into "dest", allocating the function name if needed,
3532 * without incrementing the refcount.
3533 */
3534 void
3535 set_callback(callback_T *dest, callback_T *src)
3536 {
3537 if (src->cb_partial == NULL)
3538 {
3539 // just a function name, make a copy
3540 dest->cb_name = vim_strsave(src->cb_name);
3541 dest->cb_free_name = TRUE;
3542 }
3543 else
3544 {
3545 // cb_name is a pointer into cb_partial
3546 dest->cb_name = src->cb_name;
3547 dest->cb_free_name = FALSE;
3548 }
3549 dest->cb_partial = src->cb_partial;
3550 }
3551
3552 /*
3553 * Unref/free "callback" returned by get_callback() or set_callback().
3554 */
3555 void
3556 free_callback(callback_T *callback)
3557 {
3558 if (callback->cb_partial != NULL)
3559 {
3560 partial_unref(callback->cb_partial);
3561 callback->cb_partial = NULL;
3562 }
3563 else if (callback->cb_name != NULL)
3564 func_unref(callback->cb_name);
3565 if (callback->cb_free_name)
3566 {
3567 vim_free(callback->cb_name);
3568 callback->cb_free_name = FALSE;
3569 }
3570 callback->cb_name = NULL;
3571 }
3572
3470 #endif // FEAT_EVAL 3573 #endif // FEAT_EVAL