Mercurial > vim
comparison src/userfunc.c @ 9626:172131507c85 v7.4.2090
commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Author: Bram Moolenaar <Bram@vim.org>
Date: Fri Jul 22 21:50:18 2016 +0200
patch 7.4.2090
Problem: Using submatch() in a lambda passed to substitute() is verbose.
Solution: Use a static list and pass it as an optional argument to the
function. Fix memory leak.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Fri, 22 Jul 2016 22:00:07 +0200 |
parents | 0e7912e7064b |
children | af0d98d8836e |
comparison
equal
deleted
inserted
replaced
9625:4c2524dd2403 | 9626:172131507c85 |
---|---|
478 if (ga_grow(&funcargs, 1) == OK) | 478 if (ga_grow(&funcargs, 1) == OK) |
479 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] = | 479 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] = |
480 &argvars[i]; | 480 &argvars[i]; |
481 } | 481 } |
482 | 482 |
483 ret = call_func(name, len, rettv, argcount, argvars, | 483 ret = call_func(name, len, rettv, argcount, argvars, NULL, |
484 firstline, lastline, doesrange, evaluate, partial, selfdict); | 484 firstline, lastline, doesrange, evaluate, partial, selfdict); |
485 | 485 |
486 funcargs.ga_len -= i; | 486 funcargs.ga_len -= i; |
487 } | 487 } |
488 else if (!aborting()) | 488 else if (!aborting()) |
1137 */ | 1137 */ |
1138 copy_tv(&item->li_tv, &argv[argc++]); | 1138 copy_tv(&item->li_tv, &argv[argc++]); |
1139 } | 1139 } |
1140 | 1140 |
1141 if (item == NULL) | 1141 if (item == NULL) |
1142 r = call_func(name, (int)STRLEN(name), rettv, argc, argv, | 1142 r = call_func(name, (int)STRLEN(name), rettv, argc, argv, NULL, |
1143 curwin->w_cursor.lnum, curwin->w_cursor.lnum, | 1143 curwin->w_cursor.lnum, curwin->w_cursor.lnum, |
1144 &dummy, TRUE, partial, selfdict); | 1144 &dummy, TRUE, partial, selfdict); |
1145 | 1145 |
1146 /* Free the arguments. */ | 1146 /* Free the arguments. */ |
1147 while (argc > 0) | 1147 while (argc > 0) |
1150 return r; | 1150 return r; |
1151 } | 1151 } |
1152 | 1152 |
1153 /* | 1153 /* |
1154 * Call a function with its resolved parameters | 1154 * Call a function with its resolved parameters |
1155 * | |
1156 * "argv_func", when not NULL, can be used to fill in arguments only when the | |
1157 * invoked function uses them. It is called like this: | |
1158 * new_argcount = argv_func(current_argcount, argv, called_func_argcount) | |
1159 * | |
1155 * Return FAIL when the function can't be called, OK otherwise. | 1160 * Return FAIL when the function can't be called, OK otherwise. |
1156 * Also returns OK when an error was encountered while executing the function. | 1161 * Also returns OK when an error was encountered while executing the function. |
1157 */ | 1162 */ |
1158 int | 1163 int |
1159 call_func( | 1164 call_func( |
1161 int len, /* length of "name" */ | 1166 int len, /* length of "name" */ |
1162 typval_T *rettv, /* return value goes here */ | 1167 typval_T *rettv, /* return value goes here */ |
1163 int argcount_in, /* number of "argvars" */ | 1168 int argcount_in, /* number of "argvars" */ |
1164 typval_T *argvars_in, /* vars for arguments, must have "argcount" | 1169 typval_T *argvars_in, /* vars for arguments, must have "argcount" |
1165 PLUS ONE elements! */ | 1170 PLUS ONE elements! */ |
1171 int (* argv_func)(int, typval_T *, int), | |
1172 /* function to fill in argvars */ | |
1166 linenr_T firstline, /* first line of range */ | 1173 linenr_T firstline, /* first line of range */ |
1167 linenr_T lastline, /* last line of range */ | 1174 linenr_T lastline, /* last line of range */ |
1168 int *doesrange, /* return: function handled range */ | 1175 int *doesrange, /* return: function handled range */ |
1169 int evaluate, | 1176 int evaluate, |
1170 partial_T *partial, /* optional, can be NULL */ | 1177 partial_T *partial, /* optional, can be NULL */ |
1252 fp = find_func(rfname); | 1259 fp = find_func(rfname); |
1253 } | 1260 } |
1254 | 1261 |
1255 if (fp != NULL) | 1262 if (fp != NULL) |
1256 { | 1263 { |
1264 if (argv_func != NULL) | |
1265 argcount = argv_func(argcount, argvars, fp->uf_args.ga_len); | |
1266 | |
1257 if (fp->uf_flags & FC_RANGE) | 1267 if (fp->uf_flags & FC_RANGE) |
1258 *doesrange = TRUE; | 1268 *doesrange = TRUE; |
1259 if (argcount < fp->uf_args.ga_len) | 1269 if (argcount < fp->uf_args.ga_len) |
1260 error = ERROR_TOOFEW; | 1270 error = ERROR_TOOFEW; |
1261 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len) | 1271 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len) |