Mercurial > vim
comparison src/evalfunc.c @ 11418:162bcd0debd7 v8.0.0593
patch 8.0.0593: duplication of code for adding a list or dict return value
commit https://github.com/vim/vim/commit/45cf6e910c6d162775ca9d470fac4b6db844001f
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Apr 30 20:25:19 2017 +0200
patch 8.0.0593: duplication of code for adding a list or dict return value
Problem: Duplication of code for adding a list or dict return value.
Solution: Add rettv_dict_set() and rettv_list_set(). (Yegappan Lakshmanan)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 30 Apr 2017 20:30:04 +0200 |
parents | 98abcd1227dc |
children | 5be73ebf6a15 |
comparison
equal
deleted
inserted
replaced
11417:47bfa931b0e7 | 11418:162bcd0debd7 |
---|---|
3003 if (argvars[1].v_type != VAR_UNKNOWN | 3003 if (argvars[1].v_type != VAR_UNKNOWN |
3004 && argvars[2].v_type != VAR_UNKNOWN | 3004 && argvars[2].v_type != VAR_UNKNOWN |
3005 && get_tv_number_chk(&argvars[2], &error) | 3005 && get_tv_number_chk(&argvars[2], &error) |
3006 && !error) | 3006 && !error) |
3007 { | 3007 { |
3008 rettv->v_type = VAR_LIST; | 3008 rettv_list_set(rettv, NULL); |
3009 rettv->vval.v_list = NULL; | |
3010 } | 3009 } |
3011 | 3010 |
3012 s = get_tv_string(&argvars[0]); | 3011 s = get_tv_string(&argvars[0]); |
3013 if (*s == '%' || *s == '#' || *s == '<') | 3012 if (*s == '%' || *s == '#' || *s == '<') |
3014 { | 3013 { |
3907 if (rettv->v_type == VAR_FUNC) | 3906 if (rettv->v_type == VAR_FUNC) |
3908 func_ref(rettv->vval.v_string); | 3907 func_ref(rettv->vval.v_string); |
3909 } | 3908 } |
3910 } | 3909 } |
3911 else if (STRCMP(what, "dict") == 0) | 3910 else if (STRCMP(what, "dict") == 0) |
3912 { | 3911 rettv_dict_set(rettv, pt->pt_dict); |
3913 rettv->v_type = VAR_DICT; | |
3914 rettv->vval.v_dict = pt->pt_dict; | |
3915 if (pt->pt_dict != NULL) | |
3916 ++pt->pt_dict->dv_refcount; | |
3917 } | |
3918 else if (STRCMP(what, "args") == 0) | 3912 else if (STRCMP(what, "args") == 0) |
3919 { | 3913 { |
3920 rettv->v_type = VAR_LIST; | 3914 rettv->v_type = VAR_LIST; |
3921 if (rettv_list_alloc(rettv) == OK) | 3915 if (rettv_list_alloc(rettv) == OK) |
3922 { | 3916 { |
4212 /* get all buffer-local options in a dict */ | 4206 /* get all buffer-local options in a dict */ |
4213 dict_T *opts = get_winbuf_options(TRUE); | 4207 dict_T *opts = get_winbuf_options(TRUE); |
4214 | 4208 |
4215 if (opts != NULL) | 4209 if (opts != NULL) |
4216 { | 4210 { |
4217 rettv->v_type = VAR_DICT; | 4211 rettv_dict_set(rettv, opts); |
4218 rettv->vval.v_dict = opts; | |
4219 ++opts->dv_refcount; | |
4220 done = TRUE; | 4212 done = TRUE; |
4221 } | 4213 } |
4222 } | 4214 } |
4223 else if (get_option_tv(&varname, rettv, TRUE) == OK) | 4215 else if (get_option_tv(&varname, rettv, TRUE) == OK) |
4224 /* buffer-local-option */ | 4216 /* buffer-local-option */ |
5370 options |= WILD_KEEP_ALL; | 5362 options |= WILD_KEEP_ALL; |
5371 if (argvars[2].v_type != VAR_UNKNOWN) | 5363 if (argvars[2].v_type != VAR_UNKNOWN) |
5372 { | 5364 { |
5373 if (get_tv_number_chk(&argvars[2], &error)) | 5365 if (get_tv_number_chk(&argvars[2], &error)) |
5374 { | 5366 { |
5375 rettv->v_type = VAR_LIST; | 5367 rettv_list_set(rettv, NULL); |
5376 rettv->vval.v_list = NULL; | |
5377 } | 5368 } |
5378 if (argvars[3].v_type != VAR_UNKNOWN | 5369 if (argvars[3].v_type != VAR_UNKNOWN |
5379 && get_tv_number_chk(&argvars[3], &error)) | 5370 && get_tv_number_chk(&argvars[3], &error)) |
5380 options |= WILD_ALLLINKS; | 5371 options |= WILD_ALLLINKS; |
5381 } | 5372 } |
5427 flags |= WILD_KEEP_ALL; | 5418 flags |= WILD_KEEP_ALL; |
5428 if (argvars[3].v_type != VAR_UNKNOWN) | 5419 if (argvars[3].v_type != VAR_UNKNOWN) |
5429 { | 5420 { |
5430 if (get_tv_number_chk(&argvars[3], &error)) | 5421 if (get_tv_number_chk(&argvars[3], &error)) |
5431 { | 5422 { |
5432 rettv->v_type = VAR_LIST; | 5423 rettv_list_set(rettv, NULL); |
5433 rettv->vval.v_list = NULL; | |
5434 } | 5424 } |
5435 if (argvars[4].v_type != VAR_UNKNOWN | 5425 if (argvars[4].v_type != VAR_UNKNOWN |
5436 && get_tv_number_chk(&argvars[4], &error)) | 5426 && get_tv_number_chk(&argvars[4], &error)) |
5437 flags |= WILD_ALLLINKS; | 5427 flags |= WILD_ALLLINKS; |
5438 } | 5428 } |
9150 { | 9140 { |
9151 ni = li->li_prev; | 9141 ni = li->li_prev; |
9152 list_append(l, li); | 9142 list_append(l, li); |
9153 li = ni; | 9143 li = ni; |
9154 } | 9144 } |
9155 rettv->vval.v_list = l; | 9145 rettv_list_set(rettv, l); |
9156 rettv->v_type = VAR_LIST; | |
9157 ++l->lv_refcount; | |
9158 l->lv_idx = l->lv_len - l->lv_idx - 1; | 9146 l->lv_idx = l->lv_len - l->lv_idx - 1; |
9159 } | 9147 } |
9160 } | 9148 } |
9161 | 9149 |
9162 #define SP_NOMOVE 0x01 /* don't move cursor */ | 9150 #define SP_NOMOVE 0x01 /* don't move cursor */ |
10740 l = argvars[0].vval.v_list; | 10728 l = argvars[0].vval.v_list; |
10741 if (l == NULL || tv_check_lock(l->lv_lock, | 10729 if (l == NULL || tv_check_lock(l->lv_lock, |
10742 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")), | 10730 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")), |
10743 TRUE)) | 10731 TRUE)) |
10744 goto theend; | 10732 goto theend; |
10745 rettv->vval.v_list = l; | 10733 rettv_list_set(rettv, l); |
10746 rettv->v_type = VAR_LIST; | |
10747 ++l->lv_refcount; | |
10748 | 10734 |
10749 len = list_len(l); | 10735 len = list_len(l); |
10750 if (len <= 1) | 10736 if (len <= 1) |
10751 goto theend; /* short list sorts pretty quickly */ | 10737 goto theend; /* short list sorts pretty quickly */ |
10752 | 10738 |
11830 int cchar; | 11816 int cchar; |
11831 int matchid = 0; | 11817 int matchid = 0; |
11832 char_u str[NUMBUFLEN]; | 11818 char_u str[NUMBUFLEN]; |
11833 #endif | 11819 #endif |
11834 | 11820 |
11835 rettv->v_type = VAR_LIST; | 11821 rettv_list_set(rettv, NULL); |
11836 rettv->vval.v_list = NULL; | |
11837 | 11822 |
11838 #if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL) | 11823 #if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL) |
11839 lnum = get_tv_lnum(argvars); /* -1 on type error */ | 11824 lnum = get_tv_lnum(argvars); /* -1 on type error */ |
11840 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */ | 11825 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */ |
11841 | 11826 |
11888 colnr_T col; | 11873 colnr_T col; |
11889 int i; | 11874 int i; |
11890 int id; | 11875 int id; |
11891 #endif | 11876 #endif |
11892 | 11877 |
11893 rettv->v_type = VAR_LIST; | 11878 rettv_list_set(rettv, NULL); |
11894 rettv->vval.v_list = NULL; | |
11895 | 11879 |
11896 #ifdef FEAT_SYN_HL | 11880 #ifdef FEAT_SYN_HL |
11897 lnum = get_tv_lnum(argvars); /* -1 on type error */ | 11881 lnum = get_tv_lnum(argvars); /* -1 on type error */ |
11898 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */ | 11882 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */ |
11899 | 11883 |
12055 li->li_tv.v_lock = 0; | 12039 li->li_tv.v_lock = 0; |
12056 li->li_tv.vval.v_string = s; | 12040 li->li_tv.vval.v_string = s; |
12057 list_append(list, li); | 12041 list_append(list, li); |
12058 } | 12042 } |
12059 | 12043 |
12060 ++list->lv_refcount; | 12044 rettv_list_set(rettv, list); |
12061 rettv->v_type = VAR_LIST; | |
12062 rettv->vval.v_list = list; | |
12063 list = NULL; | 12045 list = NULL; |
12064 } | 12046 } |
12065 else | 12047 else |
12066 { | 12048 { |
12067 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL); | 12049 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL); |
12463 #endif | 12445 #endif |
12464 | 12446 |
12465 static void | 12447 static void |
12466 f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv) | 12448 f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv) |
12467 { | 12449 { |
12468 rettv->v_type = VAR_DICT; | 12450 rettv_dict_set(rettv, NULL); |
12469 rettv->vval.v_dict = NULL; | |
12470 } | 12451 } |
12471 | 12452 |
12472 #ifdef FEAT_JOB_CHANNEL | 12453 #ifdef FEAT_JOB_CHANNEL |
12473 static void | 12454 static void |
12474 f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv) | 12455 f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv) |
12479 #endif | 12460 #endif |
12480 | 12461 |
12481 static void | 12462 static void |
12482 f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv) | 12463 f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv) |
12483 { | 12464 { |
12484 rettv->v_type = VAR_LIST; | 12465 rettv_list_set(rettv, NULL); |
12485 rettv->vval.v_list = NULL; | |
12486 } | 12466 } |
12487 | 12467 |
12488 static void | 12468 static void |
12489 f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv) | 12469 f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv) |
12490 { | 12470 { |