Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 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 | * evalfunc.c: Builtin functions |
| 12 | */ |
| 13 | #define USING_FLOAT_STUFF |
| 14 | |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | #ifdef AMIGA |
| 20 | # include <time.h> /* for strftime() */ |
| 21 | #endif |
| 22 | |
| 23 | #ifdef VMS |
| 24 | # include <float.h> |
| 25 | #endif |
| 26 | |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 27 | #ifdef MACOS_X |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 28 | # include <time.h> /* for time_t */ |
| 29 | #endif |
| 30 | |
| 31 | static char *e_listarg = N_("E686: Argument of %s must be a List"); |
| 32 | #ifdef FEAT_QUICKFIX |
| 33 | static char *e_stringreq = N_("E928: String required"); |
| 34 | #endif |
| 35 | |
| 36 | #ifdef FEAT_FLOAT |
| 37 | static void f_abs(typval_T *argvars, typval_T *rettv); |
| 38 | static void f_acos(typval_T *argvars, typval_T *rettv); |
| 39 | #endif |
| 40 | static void f_add(typval_T *argvars, typval_T *rettv); |
| 41 | static void f_and(typval_T *argvars, typval_T *rettv); |
| 42 | static void f_append(typval_T *argvars, typval_T *rettv); |
| 43 | static void f_argc(typval_T *argvars, typval_T *rettv); |
| 44 | static void f_argidx(typval_T *argvars, typval_T *rettv); |
| 45 | static void f_arglistid(typval_T *argvars, typval_T *rettv); |
| 46 | static void f_argv(typval_T *argvars, typval_T *rettv); |
| 47 | static void f_assert_equal(typval_T *argvars, typval_T *rettv); |
| 48 | static void f_assert_exception(typval_T *argvars, typval_T *rettv); |
| 49 | static void f_assert_fails(typval_T *argvars, typval_T *rettv); |
| 50 | static void f_assert_false(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 61c0449 | 2016-07-23 15:35:35 +0200 | [diff] [blame] | 51 | static void f_assert_inrange(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 52 | static void f_assert_match(typval_T *argvars, typval_T *rettv); |
| 53 | static void f_assert_notequal(typval_T *argvars, typval_T *rettv); |
| 54 | static void f_assert_notmatch(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 55 | static void f_assert_report(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 56 | static void f_assert_true(typval_T *argvars, typval_T *rettv); |
| 57 | #ifdef FEAT_FLOAT |
| 58 | static void f_asin(typval_T *argvars, typval_T *rettv); |
| 59 | static void f_atan(typval_T *argvars, typval_T *rettv); |
| 60 | static void f_atan2(typval_T *argvars, typval_T *rettv); |
| 61 | #endif |
Bram Moolenaar | 59716a2 | 2017-03-01 20:32:44 +0100 | [diff] [blame] | 62 | #ifdef FEAT_BEVAL |
| 63 | static void f_balloon_show(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 669a828 | 2017-11-19 20:13:05 +0100 | [diff] [blame] | 64 | # if defined(FEAT_BEVAL_TERM) |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 65 | static void f_balloon_split(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 669a828 | 2017-11-19 20:13:05 +0100 | [diff] [blame] | 66 | # endif |
Bram Moolenaar | 59716a2 | 2017-03-01 20:32:44 +0100 | [diff] [blame] | 67 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 68 | static void f_browse(typval_T *argvars, typval_T *rettv); |
| 69 | static void f_browsedir(typval_T *argvars, typval_T *rettv); |
| 70 | static void f_bufexists(typval_T *argvars, typval_T *rettv); |
| 71 | static void f_buflisted(typval_T *argvars, typval_T *rettv); |
| 72 | static void f_bufloaded(typval_T *argvars, typval_T *rettv); |
| 73 | static void f_bufname(typval_T *argvars, typval_T *rettv); |
| 74 | static void f_bufnr(typval_T *argvars, typval_T *rettv); |
| 75 | static void f_bufwinid(typval_T *argvars, typval_T *rettv); |
| 76 | static void f_bufwinnr(typval_T *argvars, typval_T *rettv); |
| 77 | static void f_byte2line(typval_T *argvars, typval_T *rettv); |
| 78 | static void byteidx(typval_T *argvars, typval_T *rettv, int comp); |
| 79 | static void f_byteidx(typval_T *argvars, typval_T *rettv); |
| 80 | static void f_byteidxcomp(typval_T *argvars, typval_T *rettv); |
| 81 | static void f_call(typval_T *argvars, typval_T *rettv); |
| 82 | #ifdef FEAT_FLOAT |
| 83 | static void f_ceil(typval_T *argvars, typval_T *rettv); |
| 84 | #endif |
| 85 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 4b785f6 | 2016-11-29 21:54:44 +0100 | [diff] [blame] | 86 | static void f_ch_canread(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 87 | static void f_ch_close(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 88 | static void f_ch_close_in(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 89 | static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv); |
| 90 | static void f_ch_evalraw(typval_T *argvars, typval_T *rettv); |
| 91 | static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv); |
| 92 | static void f_ch_getjob(typval_T *argvars, typval_T *rettv); |
| 93 | static void f_ch_info(typval_T *argvars, typval_T *rettv); |
| 94 | static void f_ch_log(typval_T *argvars, typval_T *rettv); |
| 95 | static void f_ch_logfile(typval_T *argvars, typval_T *rettv); |
| 96 | static void f_ch_open(typval_T *argvars, typval_T *rettv); |
| 97 | static void f_ch_read(typval_T *argvars, typval_T *rettv); |
| 98 | static void f_ch_readraw(typval_T *argvars, typval_T *rettv); |
| 99 | static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv); |
| 100 | static void f_ch_sendraw(typval_T *argvars, typval_T *rettv); |
| 101 | static void f_ch_setoptions(typval_T *argvars, typval_T *rettv); |
| 102 | static void f_ch_status(typval_T *argvars, typval_T *rettv); |
| 103 | #endif |
| 104 | static void f_changenr(typval_T *argvars, typval_T *rettv); |
| 105 | static void f_char2nr(typval_T *argvars, typval_T *rettv); |
| 106 | static void f_cindent(typval_T *argvars, typval_T *rettv); |
| 107 | static void f_clearmatches(typval_T *argvars, typval_T *rettv); |
| 108 | static void f_col(typval_T *argvars, typval_T *rettv); |
| 109 | #if defined(FEAT_INS_EXPAND) |
| 110 | static void f_complete(typval_T *argvars, typval_T *rettv); |
| 111 | static void f_complete_add(typval_T *argvars, typval_T *rettv); |
| 112 | static void f_complete_check(typval_T *argvars, typval_T *rettv); |
| 113 | #endif |
| 114 | static void f_confirm(typval_T *argvars, typval_T *rettv); |
| 115 | static void f_copy(typval_T *argvars, typval_T *rettv); |
| 116 | #ifdef FEAT_FLOAT |
| 117 | static void f_cos(typval_T *argvars, typval_T *rettv); |
| 118 | static void f_cosh(typval_T *argvars, typval_T *rettv); |
| 119 | #endif |
| 120 | static void f_count(typval_T *argvars, typval_T *rettv); |
| 121 | static void f_cscope_connection(typval_T *argvars, typval_T *rettv); |
| 122 | static void f_cursor(typval_T *argsvars, typval_T *rettv); |
| 123 | static void f_deepcopy(typval_T *argvars, typval_T *rettv); |
| 124 | static void f_delete(typval_T *argvars, typval_T *rettv); |
| 125 | static void f_did_filetype(typval_T *argvars, typval_T *rettv); |
| 126 | static void f_diff_filler(typval_T *argvars, typval_T *rettv); |
| 127 | static void f_diff_hlID(typval_T *argvars, typval_T *rettv); |
| 128 | static void f_empty(typval_T *argvars, typval_T *rettv); |
| 129 | static void f_escape(typval_T *argvars, typval_T *rettv); |
| 130 | static void f_eval(typval_T *argvars, typval_T *rettv); |
| 131 | static void f_eventhandler(typval_T *argvars, typval_T *rettv); |
| 132 | static void f_executable(typval_T *argvars, typval_T *rettv); |
| 133 | static void f_execute(typval_T *argvars, typval_T *rettv); |
| 134 | static void f_exepath(typval_T *argvars, typval_T *rettv); |
| 135 | static void f_exists(typval_T *argvars, typval_T *rettv); |
| 136 | #ifdef FEAT_FLOAT |
| 137 | static void f_exp(typval_T *argvars, typval_T *rettv); |
| 138 | #endif |
| 139 | static void f_expand(typval_T *argvars, typval_T *rettv); |
| 140 | static void f_extend(typval_T *argvars, typval_T *rettv); |
| 141 | static void f_feedkeys(typval_T *argvars, typval_T *rettv); |
| 142 | static void f_filereadable(typval_T *argvars, typval_T *rettv); |
| 143 | static void f_filewritable(typval_T *argvars, typval_T *rettv); |
| 144 | static void f_filter(typval_T *argvars, typval_T *rettv); |
| 145 | static void f_finddir(typval_T *argvars, typval_T *rettv); |
| 146 | static void f_findfile(typval_T *argvars, typval_T *rettv); |
| 147 | #ifdef FEAT_FLOAT |
| 148 | static void f_float2nr(typval_T *argvars, typval_T *rettv); |
| 149 | static void f_floor(typval_T *argvars, typval_T *rettv); |
| 150 | static void f_fmod(typval_T *argvars, typval_T *rettv); |
| 151 | #endif |
| 152 | static void f_fnameescape(typval_T *argvars, typval_T *rettv); |
| 153 | static void f_fnamemodify(typval_T *argvars, typval_T *rettv); |
| 154 | static void f_foldclosed(typval_T *argvars, typval_T *rettv); |
| 155 | static void f_foldclosedend(typval_T *argvars, typval_T *rettv); |
| 156 | static void f_foldlevel(typval_T *argvars, typval_T *rettv); |
| 157 | static void f_foldtext(typval_T *argvars, typval_T *rettv); |
| 158 | static void f_foldtextresult(typval_T *argvars, typval_T *rettv); |
| 159 | static void f_foreground(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 160 | static void f_funcref(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 161 | static void f_function(typval_T *argvars, typval_T *rettv); |
| 162 | static void f_garbagecollect(typval_T *argvars, typval_T *rettv); |
| 163 | static void f_get(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 164 | static void f_getbufinfo(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 165 | static void f_getbufline(typval_T *argvars, typval_T *rettv); |
| 166 | static void f_getbufvar(typval_T *argvars, typval_T *rettv); |
| 167 | static void f_getchar(typval_T *argvars, typval_T *rettv); |
| 168 | static void f_getcharmod(typval_T *argvars, typval_T *rettv); |
| 169 | static void f_getcharsearch(typval_T *argvars, typval_T *rettv); |
| 170 | static void f_getcmdline(typval_T *argvars, typval_T *rettv); |
| 171 | #if defined(FEAT_CMDL_COMPL) |
| 172 | static void f_getcompletion(typval_T *argvars, typval_T *rettv); |
| 173 | #endif |
| 174 | static void f_getcmdpos(typval_T *argvars, typval_T *rettv); |
| 175 | static void f_getcmdtype(typval_T *argvars, typval_T *rettv); |
| 176 | static void f_getcmdwintype(typval_T *argvars, typval_T *rettv); |
| 177 | static void f_getcwd(typval_T *argvars, typval_T *rettv); |
| 178 | static void f_getfontname(typval_T *argvars, typval_T *rettv); |
| 179 | static void f_getfperm(typval_T *argvars, typval_T *rettv); |
| 180 | static void f_getfsize(typval_T *argvars, typval_T *rettv); |
| 181 | static void f_getftime(typval_T *argvars, typval_T *rettv); |
| 182 | static void f_getftype(typval_T *argvars, typval_T *rettv); |
| 183 | static void f_getline(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 184 | static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 185 | static void f_getmatches(typval_T *argvars, typval_T *rettv); |
| 186 | static void f_getpid(typval_T *argvars, typval_T *rettv); |
| 187 | static void f_getcurpos(typval_T *argvars, typval_T *rettv); |
| 188 | static void f_getpos(typval_T *argvars, typval_T *rettv); |
| 189 | static void f_getqflist(typval_T *argvars, typval_T *rettv); |
| 190 | static void f_getreg(typval_T *argvars, typval_T *rettv); |
| 191 | static void f_getregtype(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 192 | static void f_gettabinfo(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 193 | static void f_gettabvar(typval_T *argvars, typval_T *rettv); |
| 194 | static void f_gettabwinvar(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 195 | static void f_getwininfo(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 196 | static void f_getwinposx(typval_T *argvars, typval_T *rettv); |
| 197 | static void f_getwinposy(typval_T *argvars, typval_T *rettv); |
| 198 | static void f_getwinvar(typval_T *argvars, typval_T *rettv); |
| 199 | static void f_glob(typval_T *argvars, typval_T *rettv); |
| 200 | static void f_globpath(typval_T *argvars, typval_T *rettv); |
| 201 | static void f_glob2regpat(typval_T *argvars, typval_T *rettv); |
| 202 | static void f_has(typval_T *argvars, typval_T *rettv); |
| 203 | static void f_has_key(typval_T *argvars, typval_T *rettv); |
| 204 | static void f_haslocaldir(typval_T *argvars, typval_T *rettv); |
| 205 | static void f_hasmapto(typval_T *argvars, typval_T *rettv); |
| 206 | static void f_histadd(typval_T *argvars, typval_T *rettv); |
| 207 | static void f_histdel(typval_T *argvars, typval_T *rettv); |
| 208 | static void f_histget(typval_T *argvars, typval_T *rettv); |
| 209 | static void f_histnr(typval_T *argvars, typval_T *rettv); |
| 210 | static void f_hlID(typval_T *argvars, typval_T *rettv); |
| 211 | static void f_hlexists(typval_T *argvars, typval_T *rettv); |
| 212 | static void f_hostname(typval_T *argvars, typval_T *rettv); |
| 213 | static void f_iconv(typval_T *argvars, typval_T *rettv); |
| 214 | static void f_indent(typval_T *argvars, typval_T *rettv); |
| 215 | static void f_index(typval_T *argvars, typval_T *rettv); |
| 216 | static void f_input(typval_T *argvars, typval_T *rettv); |
| 217 | static void f_inputdialog(typval_T *argvars, typval_T *rettv); |
| 218 | static void f_inputlist(typval_T *argvars, typval_T *rettv); |
| 219 | static void f_inputrestore(typval_T *argvars, typval_T *rettv); |
| 220 | static void f_inputsave(typval_T *argvars, typval_T *rettv); |
| 221 | static void f_inputsecret(typval_T *argvars, typval_T *rettv); |
| 222 | static void f_insert(typval_T *argvars, typval_T *rettv); |
| 223 | static void f_invert(typval_T *argvars, typval_T *rettv); |
| 224 | static void f_isdirectory(typval_T *argvars, typval_T *rettv); |
| 225 | static void f_islocked(typval_T *argvars, typval_T *rettv); |
| 226 | #if defined(FEAT_FLOAT) && defined(HAVE_MATH_H) |
| 227 | static void f_isnan(typval_T *argvars, typval_T *rettv); |
| 228 | #endif |
| 229 | static void f_items(typval_T *argvars, typval_T *rettv); |
| 230 | #ifdef FEAT_JOB_CHANNEL |
| 231 | static void f_job_getchannel(typval_T *argvars, typval_T *rettv); |
| 232 | static void f_job_info(typval_T *argvars, typval_T *rettv); |
| 233 | static void f_job_setoptions(typval_T *argvars, typval_T *rettv); |
| 234 | static void f_job_start(typval_T *argvars, typval_T *rettv); |
| 235 | static void f_job_stop(typval_T *argvars, typval_T *rettv); |
| 236 | static void f_job_status(typval_T *argvars, typval_T *rettv); |
| 237 | #endif |
| 238 | static void f_join(typval_T *argvars, typval_T *rettv); |
| 239 | static void f_js_decode(typval_T *argvars, typval_T *rettv); |
| 240 | static void f_js_encode(typval_T *argvars, typval_T *rettv); |
| 241 | static void f_json_decode(typval_T *argvars, typval_T *rettv); |
| 242 | static void f_json_encode(typval_T *argvars, typval_T *rettv); |
| 243 | static void f_keys(typval_T *argvars, typval_T *rettv); |
| 244 | static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv); |
| 245 | static void f_len(typval_T *argvars, typval_T *rettv); |
| 246 | static void f_libcall(typval_T *argvars, typval_T *rettv); |
| 247 | static void f_libcallnr(typval_T *argvars, typval_T *rettv); |
| 248 | static void f_line(typval_T *argvars, typval_T *rettv); |
| 249 | static void f_line2byte(typval_T *argvars, typval_T *rettv); |
| 250 | static void f_lispindent(typval_T *argvars, typval_T *rettv); |
| 251 | static void f_localtime(typval_T *argvars, typval_T *rettv); |
| 252 | #ifdef FEAT_FLOAT |
| 253 | static void f_log(typval_T *argvars, typval_T *rettv); |
| 254 | static void f_log10(typval_T *argvars, typval_T *rettv); |
| 255 | #endif |
| 256 | #ifdef FEAT_LUA |
| 257 | static void f_luaeval(typval_T *argvars, typval_T *rettv); |
| 258 | #endif |
| 259 | static void f_map(typval_T *argvars, typval_T *rettv); |
| 260 | static void f_maparg(typval_T *argvars, typval_T *rettv); |
| 261 | static void f_mapcheck(typval_T *argvars, typval_T *rettv); |
| 262 | static void f_match(typval_T *argvars, typval_T *rettv); |
| 263 | static void f_matchadd(typval_T *argvars, typval_T *rettv); |
| 264 | static void f_matchaddpos(typval_T *argvars, typval_T *rettv); |
| 265 | static void f_matcharg(typval_T *argvars, typval_T *rettv); |
| 266 | static void f_matchdelete(typval_T *argvars, typval_T *rettv); |
| 267 | static void f_matchend(typval_T *argvars, typval_T *rettv); |
| 268 | static void f_matchlist(typval_T *argvars, typval_T *rettv); |
| 269 | static void f_matchstr(typval_T *argvars, typval_T *rettv); |
| 270 | static void f_matchstrpos(typval_T *argvars, typval_T *rettv); |
| 271 | static void f_max(typval_T *argvars, typval_T *rettv); |
| 272 | static void f_min(typval_T *argvars, typval_T *rettv); |
| 273 | #ifdef vim_mkdir |
| 274 | static void f_mkdir(typval_T *argvars, typval_T *rettv); |
| 275 | #endif |
| 276 | static void f_mode(typval_T *argvars, typval_T *rettv); |
| 277 | #ifdef FEAT_MZSCHEME |
| 278 | static void f_mzeval(typval_T *argvars, typval_T *rettv); |
| 279 | #endif |
| 280 | static void f_nextnonblank(typval_T *argvars, typval_T *rettv); |
| 281 | static void f_nr2char(typval_T *argvars, typval_T *rettv); |
| 282 | static void f_or(typval_T *argvars, typval_T *rettv); |
| 283 | static void f_pathshorten(typval_T *argvars, typval_T *rettv); |
| 284 | #ifdef FEAT_PERL |
| 285 | static void f_perleval(typval_T *argvars, typval_T *rettv); |
| 286 | #endif |
| 287 | #ifdef FEAT_FLOAT |
| 288 | static void f_pow(typval_T *argvars, typval_T *rettv); |
| 289 | #endif |
| 290 | static void f_prevnonblank(typval_T *argvars, typval_T *rettv); |
| 291 | static void f_printf(typval_T *argvars, typval_T *rettv); |
| 292 | static void f_pumvisible(typval_T *argvars, typval_T *rettv); |
| 293 | #ifdef FEAT_PYTHON3 |
| 294 | static void f_py3eval(typval_T *argvars, typval_T *rettv); |
| 295 | #endif |
| 296 | #ifdef FEAT_PYTHON |
| 297 | static void f_pyeval(typval_T *argvars, typval_T *rettv); |
| 298 | #endif |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 299 | #if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) |
| 300 | static void f_pyxeval(typval_T *argvars, typval_T *rettv); |
| 301 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 302 | static void f_range(typval_T *argvars, typval_T *rettv); |
| 303 | static void f_readfile(typval_T *argvars, typval_T *rettv); |
| 304 | static void f_reltime(typval_T *argvars, typval_T *rettv); |
| 305 | #ifdef FEAT_FLOAT |
| 306 | static void f_reltimefloat(typval_T *argvars, typval_T *rettv); |
| 307 | #endif |
| 308 | static void f_reltimestr(typval_T *argvars, typval_T *rettv); |
| 309 | static void f_remote_expr(typval_T *argvars, typval_T *rettv); |
| 310 | static void f_remote_foreground(typval_T *argvars, typval_T *rettv); |
| 311 | static void f_remote_peek(typval_T *argvars, typval_T *rettv); |
| 312 | static void f_remote_read(typval_T *argvars, typval_T *rettv); |
| 313 | static void f_remote_send(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 314 | static void f_remote_startserver(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 315 | static void f_remove(typval_T *argvars, typval_T *rettv); |
| 316 | static void f_rename(typval_T *argvars, typval_T *rettv); |
| 317 | static void f_repeat(typval_T *argvars, typval_T *rettv); |
| 318 | static void f_resolve(typval_T *argvars, typval_T *rettv); |
| 319 | static void f_reverse(typval_T *argvars, typval_T *rettv); |
| 320 | #ifdef FEAT_FLOAT |
| 321 | static void f_round(typval_T *argvars, typval_T *rettv); |
| 322 | #endif |
| 323 | static void f_screenattr(typval_T *argvars, typval_T *rettv); |
| 324 | static void f_screenchar(typval_T *argvars, typval_T *rettv); |
| 325 | static void f_screencol(typval_T *argvars, typval_T *rettv); |
| 326 | static void f_screenrow(typval_T *argvars, typval_T *rettv); |
| 327 | static void f_search(typval_T *argvars, typval_T *rettv); |
| 328 | static void f_searchdecl(typval_T *argvars, typval_T *rettv); |
| 329 | static void f_searchpair(typval_T *argvars, typval_T *rettv); |
| 330 | static void f_searchpairpos(typval_T *argvars, typval_T *rettv); |
| 331 | static void f_searchpos(typval_T *argvars, typval_T *rettv); |
| 332 | static void f_server2client(typval_T *argvars, typval_T *rettv); |
| 333 | static void f_serverlist(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 334 | static void f_setbufline(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 335 | static void f_setbufvar(typval_T *argvars, typval_T *rettv); |
| 336 | static void f_setcharsearch(typval_T *argvars, typval_T *rettv); |
| 337 | static void f_setcmdpos(typval_T *argvars, typval_T *rettv); |
| 338 | static void f_setfperm(typval_T *argvars, typval_T *rettv); |
| 339 | static void f_setline(typval_T *argvars, typval_T *rettv); |
| 340 | static void f_setloclist(typval_T *argvars, typval_T *rettv); |
| 341 | static void f_setmatches(typval_T *argvars, typval_T *rettv); |
| 342 | static void f_setpos(typval_T *argvars, typval_T *rettv); |
| 343 | static void f_setqflist(typval_T *argvars, typval_T *rettv); |
| 344 | static void f_setreg(typval_T *argvars, typval_T *rettv); |
| 345 | static void f_settabvar(typval_T *argvars, typval_T *rettv); |
| 346 | static void f_settabwinvar(typval_T *argvars, typval_T *rettv); |
| 347 | static void f_setwinvar(typval_T *argvars, typval_T *rettv); |
| 348 | #ifdef FEAT_CRYPT |
| 349 | static void f_sha256(typval_T *argvars, typval_T *rettv); |
| 350 | #endif /* FEAT_CRYPT */ |
| 351 | static void f_shellescape(typval_T *argvars, typval_T *rettv); |
| 352 | static void f_shiftwidth(typval_T *argvars, typval_T *rettv); |
| 353 | static void f_simplify(typval_T *argvars, typval_T *rettv); |
| 354 | #ifdef FEAT_FLOAT |
| 355 | static void f_sin(typval_T *argvars, typval_T *rettv); |
| 356 | static void f_sinh(typval_T *argvars, typval_T *rettv); |
| 357 | #endif |
| 358 | static void f_sort(typval_T *argvars, typval_T *rettv); |
| 359 | static void f_soundfold(typval_T *argvars, typval_T *rettv); |
| 360 | static void f_spellbadword(typval_T *argvars, typval_T *rettv); |
| 361 | static void f_spellsuggest(typval_T *argvars, typval_T *rettv); |
| 362 | static void f_split(typval_T *argvars, typval_T *rettv); |
| 363 | #ifdef FEAT_FLOAT |
| 364 | static void f_sqrt(typval_T *argvars, typval_T *rettv); |
| 365 | static void f_str2float(typval_T *argvars, typval_T *rettv); |
| 366 | #endif |
| 367 | static void f_str2nr(typval_T *argvars, typval_T *rettv); |
| 368 | static void f_strchars(typval_T *argvars, typval_T *rettv); |
| 369 | #ifdef HAVE_STRFTIME |
| 370 | static void f_strftime(typval_T *argvars, typval_T *rettv); |
| 371 | #endif |
| 372 | static void f_strgetchar(typval_T *argvars, typval_T *rettv); |
| 373 | static void f_stridx(typval_T *argvars, typval_T *rettv); |
| 374 | static void f_string(typval_T *argvars, typval_T *rettv); |
| 375 | static void f_strlen(typval_T *argvars, typval_T *rettv); |
| 376 | static void f_strcharpart(typval_T *argvars, typval_T *rettv); |
| 377 | static void f_strpart(typval_T *argvars, typval_T *rettv); |
| 378 | static void f_strridx(typval_T *argvars, typval_T *rettv); |
| 379 | static void f_strtrans(typval_T *argvars, typval_T *rettv); |
| 380 | static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv); |
| 381 | static void f_strwidth(typval_T *argvars, typval_T *rettv); |
| 382 | static void f_submatch(typval_T *argvars, typval_T *rettv); |
| 383 | static void f_substitute(typval_T *argvars, typval_T *rettv); |
| 384 | static void f_synID(typval_T *argvars, typval_T *rettv); |
| 385 | static void f_synIDattr(typval_T *argvars, typval_T *rettv); |
| 386 | static void f_synIDtrans(typval_T *argvars, typval_T *rettv); |
| 387 | static void f_synstack(typval_T *argvars, typval_T *rettv); |
| 388 | static void f_synconcealed(typval_T *argvars, typval_T *rettv); |
| 389 | static void f_system(typval_T *argvars, typval_T *rettv); |
| 390 | static void f_systemlist(typval_T *argvars, typval_T *rettv); |
| 391 | static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv); |
| 392 | static void f_tabpagenr(typval_T *argvars, typval_T *rettv); |
| 393 | static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv); |
| 394 | static void f_taglist(typval_T *argvars, typval_T *rettv); |
| 395 | static void f_tagfiles(typval_T *argvars, typval_T *rettv); |
| 396 | static void f_tempname(typval_T *argvars, typval_T *rettv); |
| 397 | static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv); |
| 398 | static void f_test_autochdir(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 5e80de3 | 2017-09-03 15:48:12 +0200 | [diff] [blame] | 399 | static void f_test_feedinput(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 400 | static void f_test_override(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 401 | static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | e0c31f6 | 2017-03-01 15:07:05 +0100 | [diff] [blame] | 402 | static void f_test_ignore_error(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 403 | #ifdef FEAT_JOB_CHANNEL |
| 404 | static void f_test_null_channel(typval_T *argvars, typval_T *rettv); |
| 405 | #endif |
| 406 | static void f_test_null_dict(typval_T *argvars, typval_T *rettv); |
| 407 | #ifdef FEAT_JOB_CHANNEL |
| 408 | static void f_test_null_job(typval_T *argvars, typval_T *rettv); |
| 409 | #endif |
| 410 | static void f_test_null_list(typval_T *argvars, typval_T *rettv); |
| 411 | static void f_test_null_partial(typval_T *argvars, typval_T *rettv); |
| 412 | static void f_test_null_string(typval_T *argvars, typval_T *rettv); |
| 413 | static void f_test_settime(typval_T *argvars, typval_T *rettv); |
| 414 | #ifdef FEAT_FLOAT |
| 415 | static void f_tan(typval_T *argvars, typval_T *rettv); |
| 416 | static void f_tanh(typval_T *argvars, typval_T *rettv); |
| 417 | #endif |
| 418 | #ifdef FEAT_TIMERS |
Bram Moolenaar | 8e97bd7 | 2016-08-06 22:05:07 +0200 | [diff] [blame] | 419 | static void f_timer_info(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | b73598e | 2016-08-07 18:22:53 +0200 | [diff] [blame] | 420 | static void f_timer_pause(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 421 | static void f_timer_start(typval_T *argvars, typval_T *rettv); |
| 422 | static void f_timer_stop(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | b73598e | 2016-08-07 18:22:53 +0200 | [diff] [blame] | 423 | static void f_timer_stopall(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 424 | #endif |
| 425 | static void f_tolower(typval_T *argvars, typval_T *rettv); |
| 426 | static void f_toupper(typval_T *argvars, typval_T *rettv); |
| 427 | static void f_tr(typval_T *argvars, typval_T *rettv); |
| 428 | #ifdef FEAT_FLOAT |
| 429 | static void f_trunc(typval_T *argvars, typval_T *rettv); |
| 430 | #endif |
| 431 | static void f_type(typval_T *argvars, typval_T *rettv); |
| 432 | static void f_undofile(typval_T *argvars, typval_T *rettv); |
| 433 | static void f_undotree(typval_T *argvars, typval_T *rettv); |
| 434 | static void f_uniq(typval_T *argvars, typval_T *rettv); |
| 435 | static void f_values(typval_T *argvars, typval_T *rettv); |
| 436 | static void f_virtcol(typval_T *argvars, typval_T *rettv); |
| 437 | static void f_visualmode(typval_T *argvars, typval_T *rettv); |
| 438 | static void f_wildmenumode(typval_T *argvars, typval_T *rettv); |
| 439 | static void f_win_findbuf(typval_T *argvars, typval_T *rettv); |
| 440 | static void f_win_getid(typval_T *argvars, typval_T *rettv); |
| 441 | static void f_win_gotoid(typval_T *argvars, typval_T *rettv); |
| 442 | static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv); |
| 443 | static void f_win_id2win(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 22044dc | 2017-12-02 15:43:37 +0100 | [diff] [blame] | 444 | static void f_win_screenpos(typval_T *argvars, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 445 | static void f_winbufnr(typval_T *argvars, typval_T *rettv); |
| 446 | static void f_wincol(typval_T *argvars, typval_T *rettv); |
| 447 | static void f_winheight(typval_T *argvars, typval_T *rettv); |
| 448 | static void f_winline(typval_T *argvars, typval_T *rettv); |
| 449 | static void f_winnr(typval_T *argvars, typval_T *rettv); |
| 450 | static void f_winrestcmd(typval_T *argvars, typval_T *rettv); |
| 451 | static void f_winrestview(typval_T *argvars, typval_T *rettv); |
| 452 | static void f_winsaveview(typval_T *argvars, typval_T *rettv); |
| 453 | static void f_winwidth(typval_T *argvars, typval_T *rettv); |
| 454 | static void f_writefile(typval_T *argvars, typval_T *rettv); |
| 455 | static void f_wordcount(typval_T *argvars, typval_T *rettv); |
| 456 | static void f_xor(typval_T *argvars, typval_T *rettv); |
| 457 | |
| 458 | /* |
| 459 | * Array with names and number of arguments of all internal functions |
| 460 | * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH! |
| 461 | */ |
| 462 | static struct fst |
| 463 | { |
| 464 | char *f_name; /* function name */ |
| 465 | char f_min_argc; /* minimal number of arguments */ |
| 466 | char f_max_argc; /* maximal number of arguments */ |
| 467 | void (*f_func)(typval_T *args, typval_T *rvar); |
| 468 | /* implementation of function */ |
| 469 | } functions[] = |
| 470 | { |
| 471 | #ifdef FEAT_FLOAT |
| 472 | {"abs", 1, 1, f_abs}, |
| 473 | {"acos", 1, 1, f_acos}, /* WJMc */ |
| 474 | #endif |
| 475 | {"add", 2, 2, f_add}, |
| 476 | {"and", 2, 2, f_and}, |
| 477 | {"append", 2, 2, f_append}, |
| 478 | {"argc", 0, 0, f_argc}, |
| 479 | {"argidx", 0, 0, f_argidx}, |
| 480 | {"arglistid", 0, 2, f_arglistid}, |
| 481 | {"argv", 0, 1, f_argv}, |
| 482 | #ifdef FEAT_FLOAT |
| 483 | {"asin", 1, 1, f_asin}, /* WJMc */ |
| 484 | #endif |
| 485 | {"assert_equal", 2, 3, f_assert_equal}, |
| 486 | {"assert_exception", 1, 2, f_assert_exception}, |
| 487 | {"assert_fails", 1, 2, f_assert_fails}, |
| 488 | {"assert_false", 1, 2, f_assert_false}, |
Bram Moolenaar | 3421566 | 2016-12-04 13:37:41 +0100 | [diff] [blame] | 489 | {"assert_inrange", 3, 4, f_assert_inrange}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 490 | {"assert_match", 2, 3, f_assert_match}, |
| 491 | {"assert_notequal", 2, 3, f_assert_notequal}, |
| 492 | {"assert_notmatch", 2, 3, f_assert_notmatch}, |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 493 | {"assert_report", 1, 1, f_assert_report}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 494 | {"assert_true", 1, 2, f_assert_true}, |
| 495 | #ifdef FEAT_FLOAT |
| 496 | {"atan", 1, 1, f_atan}, |
| 497 | {"atan2", 2, 2, f_atan2}, |
| 498 | #endif |
Bram Moolenaar | 59716a2 | 2017-03-01 20:32:44 +0100 | [diff] [blame] | 499 | #ifdef FEAT_BEVAL |
| 500 | {"balloon_show", 1, 1, f_balloon_show}, |
Bram Moolenaar | 669a828 | 2017-11-19 20:13:05 +0100 | [diff] [blame] | 501 | # if defined(FEAT_BEVAL_TERM) |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 502 | {"balloon_split", 1, 1, f_balloon_split}, |
Bram Moolenaar | 669a828 | 2017-11-19 20:13:05 +0100 | [diff] [blame] | 503 | # endif |
Bram Moolenaar | 59716a2 | 2017-03-01 20:32:44 +0100 | [diff] [blame] | 504 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 505 | {"browse", 4, 4, f_browse}, |
| 506 | {"browsedir", 2, 2, f_browsedir}, |
| 507 | {"bufexists", 1, 1, f_bufexists}, |
| 508 | {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */ |
| 509 | {"buffer_name", 1, 1, f_bufname}, /* obsolete */ |
| 510 | {"buffer_number", 1, 1, f_bufnr}, /* obsolete */ |
| 511 | {"buflisted", 1, 1, f_buflisted}, |
| 512 | {"bufloaded", 1, 1, f_bufloaded}, |
| 513 | {"bufname", 1, 1, f_bufname}, |
| 514 | {"bufnr", 1, 2, f_bufnr}, |
| 515 | {"bufwinid", 1, 1, f_bufwinid}, |
| 516 | {"bufwinnr", 1, 1, f_bufwinnr}, |
| 517 | {"byte2line", 1, 1, f_byte2line}, |
| 518 | {"byteidx", 2, 2, f_byteidx}, |
| 519 | {"byteidxcomp", 2, 2, f_byteidxcomp}, |
| 520 | {"call", 2, 3, f_call}, |
| 521 | #ifdef FEAT_FLOAT |
| 522 | {"ceil", 1, 1, f_ceil}, |
| 523 | #endif |
| 524 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 4b785f6 | 2016-11-29 21:54:44 +0100 | [diff] [blame] | 525 | {"ch_canread", 1, 1, f_ch_canread}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 526 | {"ch_close", 1, 1, f_ch_close}, |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 527 | {"ch_close_in", 1, 1, f_ch_close_in}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 528 | {"ch_evalexpr", 2, 3, f_ch_evalexpr}, |
| 529 | {"ch_evalraw", 2, 3, f_ch_evalraw}, |
| 530 | {"ch_getbufnr", 2, 2, f_ch_getbufnr}, |
| 531 | {"ch_getjob", 1, 1, f_ch_getjob}, |
| 532 | {"ch_info", 1, 1, f_ch_info}, |
| 533 | {"ch_log", 1, 2, f_ch_log}, |
| 534 | {"ch_logfile", 1, 2, f_ch_logfile}, |
| 535 | {"ch_open", 1, 2, f_ch_open}, |
| 536 | {"ch_read", 1, 2, f_ch_read}, |
| 537 | {"ch_readraw", 1, 2, f_ch_readraw}, |
| 538 | {"ch_sendexpr", 2, 3, f_ch_sendexpr}, |
| 539 | {"ch_sendraw", 2, 3, f_ch_sendraw}, |
| 540 | {"ch_setoptions", 2, 2, f_ch_setoptions}, |
Bram Moolenaar | 7ef3810 | 2016-09-26 22:36:58 +0200 | [diff] [blame] | 541 | {"ch_status", 1, 2, f_ch_status}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 542 | #endif |
| 543 | {"changenr", 0, 0, f_changenr}, |
| 544 | {"char2nr", 1, 2, f_char2nr}, |
| 545 | {"cindent", 1, 1, f_cindent}, |
| 546 | {"clearmatches", 0, 0, f_clearmatches}, |
| 547 | {"col", 1, 1, f_col}, |
| 548 | #if defined(FEAT_INS_EXPAND) |
| 549 | {"complete", 2, 2, f_complete}, |
| 550 | {"complete_add", 1, 1, f_complete_add}, |
| 551 | {"complete_check", 0, 0, f_complete_check}, |
| 552 | #endif |
| 553 | {"confirm", 1, 4, f_confirm}, |
| 554 | {"copy", 1, 1, f_copy}, |
| 555 | #ifdef FEAT_FLOAT |
| 556 | {"cos", 1, 1, f_cos}, |
| 557 | {"cosh", 1, 1, f_cosh}, |
| 558 | #endif |
| 559 | {"count", 2, 4, f_count}, |
| 560 | {"cscope_connection",0,3, f_cscope_connection}, |
| 561 | {"cursor", 1, 3, f_cursor}, |
| 562 | {"deepcopy", 1, 2, f_deepcopy}, |
| 563 | {"delete", 1, 2, f_delete}, |
| 564 | {"did_filetype", 0, 0, f_did_filetype}, |
| 565 | {"diff_filler", 1, 1, f_diff_filler}, |
| 566 | {"diff_hlID", 2, 2, f_diff_hlID}, |
| 567 | {"empty", 1, 1, f_empty}, |
| 568 | {"escape", 2, 2, f_escape}, |
| 569 | {"eval", 1, 1, f_eval}, |
| 570 | {"eventhandler", 0, 0, f_eventhandler}, |
| 571 | {"executable", 1, 1, f_executable}, |
| 572 | {"execute", 1, 2, f_execute}, |
| 573 | {"exepath", 1, 1, f_exepath}, |
| 574 | {"exists", 1, 1, f_exists}, |
| 575 | #ifdef FEAT_FLOAT |
| 576 | {"exp", 1, 1, f_exp}, |
| 577 | #endif |
| 578 | {"expand", 1, 3, f_expand}, |
| 579 | {"extend", 2, 3, f_extend}, |
| 580 | {"feedkeys", 1, 2, f_feedkeys}, |
| 581 | {"file_readable", 1, 1, f_filereadable}, /* obsolete */ |
| 582 | {"filereadable", 1, 1, f_filereadable}, |
| 583 | {"filewritable", 1, 1, f_filewritable}, |
| 584 | {"filter", 2, 2, f_filter}, |
| 585 | {"finddir", 1, 3, f_finddir}, |
| 586 | {"findfile", 1, 3, f_findfile}, |
| 587 | #ifdef FEAT_FLOAT |
| 588 | {"float2nr", 1, 1, f_float2nr}, |
| 589 | {"floor", 1, 1, f_floor}, |
| 590 | {"fmod", 2, 2, f_fmod}, |
| 591 | #endif |
| 592 | {"fnameescape", 1, 1, f_fnameescape}, |
| 593 | {"fnamemodify", 2, 2, f_fnamemodify}, |
| 594 | {"foldclosed", 1, 1, f_foldclosed}, |
| 595 | {"foldclosedend", 1, 1, f_foldclosedend}, |
| 596 | {"foldlevel", 1, 1, f_foldlevel}, |
| 597 | {"foldtext", 0, 0, f_foldtext}, |
| 598 | {"foldtextresult", 1, 1, f_foldtextresult}, |
| 599 | {"foreground", 0, 0, f_foreground}, |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 600 | {"funcref", 1, 3, f_funcref}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 601 | {"function", 1, 3, f_function}, |
| 602 | {"garbagecollect", 0, 1, f_garbagecollect}, |
| 603 | {"get", 2, 3, f_get}, |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 604 | {"getbufinfo", 0, 1, f_getbufinfo}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 605 | {"getbufline", 2, 3, f_getbufline}, |
| 606 | {"getbufvar", 2, 3, f_getbufvar}, |
| 607 | {"getchar", 0, 1, f_getchar}, |
| 608 | {"getcharmod", 0, 0, f_getcharmod}, |
| 609 | {"getcharsearch", 0, 0, f_getcharsearch}, |
| 610 | {"getcmdline", 0, 0, f_getcmdline}, |
| 611 | {"getcmdpos", 0, 0, f_getcmdpos}, |
| 612 | {"getcmdtype", 0, 0, f_getcmdtype}, |
| 613 | {"getcmdwintype", 0, 0, f_getcmdwintype}, |
| 614 | #if defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | e9d58a6 | 2016-08-13 15:07:41 +0200 | [diff] [blame] | 615 | {"getcompletion", 2, 3, f_getcompletion}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 616 | #endif |
| 617 | {"getcurpos", 0, 0, f_getcurpos}, |
| 618 | {"getcwd", 0, 2, f_getcwd}, |
| 619 | {"getfontname", 0, 1, f_getfontname}, |
| 620 | {"getfperm", 1, 1, f_getfperm}, |
| 621 | {"getfsize", 1, 1, f_getfsize}, |
| 622 | {"getftime", 1, 1, f_getftime}, |
| 623 | {"getftype", 1, 1, f_getftype}, |
| 624 | {"getline", 1, 2, f_getline}, |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 625 | {"getloclist", 1, 2, f_getloclist}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 626 | {"getmatches", 0, 0, f_getmatches}, |
| 627 | {"getpid", 0, 0, f_getpid}, |
| 628 | {"getpos", 1, 1, f_getpos}, |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 629 | {"getqflist", 0, 1, f_getqflist}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 630 | {"getreg", 0, 3, f_getreg}, |
| 631 | {"getregtype", 0, 1, f_getregtype}, |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 632 | {"gettabinfo", 0, 1, f_gettabinfo}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 633 | {"gettabvar", 2, 3, f_gettabvar}, |
| 634 | {"gettabwinvar", 3, 4, f_gettabwinvar}, |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 635 | {"getwininfo", 0, 1, f_getwininfo}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 636 | {"getwinposx", 0, 0, f_getwinposx}, |
| 637 | {"getwinposy", 0, 0, f_getwinposy}, |
| 638 | {"getwinvar", 2, 3, f_getwinvar}, |
| 639 | {"glob", 1, 4, f_glob}, |
| 640 | {"glob2regpat", 1, 1, f_glob2regpat}, |
| 641 | {"globpath", 2, 5, f_globpath}, |
| 642 | {"has", 1, 1, f_has}, |
| 643 | {"has_key", 2, 2, f_has_key}, |
| 644 | {"haslocaldir", 0, 2, f_haslocaldir}, |
| 645 | {"hasmapto", 1, 3, f_hasmapto}, |
| 646 | {"highlightID", 1, 1, f_hlID}, /* obsolete */ |
| 647 | {"highlight_exists",1, 1, f_hlexists}, /* obsolete */ |
| 648 | {"histadd", 2, 2, f_histadd}, |
| 649 | {"histdel", 1, 2, f_histdel}, |
| 650 | {"histget", 1, 2, f_histget}, |
| 651 | {"histnr", 1, 1, f_histnr}, |
| 652 | {"hlID", 1, 1, f_hlID}, |
| 653 | {"hlexists", 1, 1, f_hlexists}, |
| 654 | {"hostname", 0, 0, f_hostname}, |
| 655 | {"iconv", 3, 3, f_iconv}, |
| 656 | {"indent", 1, 1, f_indent}, |
| 657 | {"index", 2, 4, f_index}, |
| 658 | {"input", 1, 3, f_input}, |
| 659 | {"inputdialog", 1, 3, f_inputdialog}, |
| 660 | {"inputlist", 1, 1, f_inputlist}, |
| 661 | {"inputrestore", 0, 0, f_inputrestore}, |
| 662 | {"inputsave", 0, 0, f_inputsave}, |
| 663 | {"inputsecret", 1, 2, f_inputsecret}, |
| 664 | {"insert", 2, 3, f_insert}, |
| 665 | {"invert", 1, 1, f_invert}, |
| 666 | {"isdirectory", 1, 1, f_isdirectory}, |
| 667 | {"islocked", 1, 1, f_islocked}, |
| 668 | #if defined(FEAT_FLOAT) && defined(HAVE_MATH_H) |
| 669 | {"isnan", 1, 1, f_isnan}, |
| 670 | #endif |
| 671 | {"items", 1, 1, f_items}, |
| 672 | #ifdef FEAT_JOB_CHANNEL |
| 673 | {"job_getchannel", 1, 1, f_job_getchannel}, |
| 674 | {"job_info", 1, 1, f_job_info}, |
| 675 | {"job_setoptions", 2, 2, f_job_setoptions}, |
| 676 | {"job_start", 1, 2, f_job_start}, |
| 677 | {"job_status", 1, 1, f_job_status}, |
| 678 | {"job_stop", 1, 2, f_job_stop}, |
| 679 | #endif |
| 680 | {"join", 1, 2, f_join}, |
| 681 | {"js_decode", 1, 1, f_js_decode}, |
| 682 | {"js_encode", 1, 1, f_js_encode}, |
| 683 | {"json_decode", 1, 1, f_json_decode}, |
| 684 | {"json_encode", 1, 1, f_json_encode}, |
| 685 | {"keys", 1, 1, f_keys}, |
| 686 | {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */ |
| 687 | {"len", 1, 1, f_len}, |
| 688 | {"libcall", 3, 3, f_libcall}, |
| 689 | {"libcallnr", 3, 3, f_libcallnr}, |
| 690 | {"line", 1, 1, f_line}, |
| 691 | {"line2byte", 1, 1, f_line2byte}, |
| 692 | {"lispindent", 1, 1, f_lispindent}, |
| 693 | {"localtime", 0, 0, f_localtime}, |
| 694 | #ifdef FEAT_FLOAT |
| 695 | {"log", 1, 1, f_log}, |
| 696 | {"log10", 1, 1, f_log10}, |
| 697 | #endif |
| 698 | #ifdef FEAT_LUA |
| 699 | {"luaeval", 1, 2, f_luaeval}, |
| 700 | #endif |
| 701 | {"map", 2, 2, f_map}, |
| 702 | {"maparg", 1, 4, f_maparg}, |
| 703 | {"mapcheck", 1, 3, f_mapcheck}, |
| 704 | {"match", 2, 4, f_match}, |
| 705 | {"matchadd", 2, 5, f_matchadd}, |
| 706 | {"matchaddpos", 2, 5, f_matchaddpos}, |
| 707 | {"matcharg", 1, 1, f_matcharg}, |
| 708 | {"matchdelete", 1, 1, f_matchdelete}, |
| 709 | {"matchend", 2, 4, f_matchend}, |
| 710 | {"matchlist", 2, 4, f_matchlist}, |
| 711 | {"matchstr", 2, 4, f_matchstr}, |
| 712 | {"matchstrpos", 2, 4, f_matchstrpos}, |
| 713 | {"max", 1, 1, f_max}, |
| 714 | {"min", 1, 1, f_min}, |
| 715 | #ifdef vim_mkdir |
| 716 | {"mkdir", 1, 3, f_mkdir}, |
| 717 | #endif |
| 718 | {"mode", 0, 1, f_mode}, |
| 719 | #ifdef FEAT_MZSCHEME |
| 720 | {"mzeval", 1, 1, f_mzeval}, |
| 721 | #endif |
| 722 | {"nextnonblank", 1, 1, f_nextnonblank}, |
| 723 | {"nr2char", 1, 2, f_nr2char}, |
| 724 | {"or", 2, 2, f_or}, |
| 725 | {"pathshorten", 1, 1, f_pathshorten}, |
| 726 | #ifdef FEAT_PERL |
| 727 | {"perleval", 1, 1, f_perleval}, |
| 728 | #endif |
| 729 | #ifdef FEAT_FLOAT |
| 730 | {"pow", 2, 2, f_pow}, |
| 731 | #endif |
| 732 | {"prevnonblank", 1, 1, f_prevnonblank}, |
| 733 | {"printf", 2, 19, f_printf}, |
| 734 | {"pumvisible", 0, 0, f_pumvisible}, |
| 735 | #ifdef FEAT_PYTHON3 |
| 736 | {"py3eval", 1, 1, f_py3eval}, |
| 737 | #endif |
| 738 | #ifdef FEAT_PYTHON |
| 739 | {"pyeval", 1, 1, f_pyeval}, |
| 740 | #endif |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 741 | #if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) |
| 742 | {"pyxeval", 1, 1, f_pyxeval}, |
| 743 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 744 | {"range", 1, 3, f_range}, |
| 745 | {"readfile", 1, 3, f_readfile}, |
| 746 | {"reltime", 0, 2, f_reltime}, |
| 747 | #ifdef FEAT_FLOAT |
| 748 | {"reltimefloat", 1, 1, f_reltimefloat}, |
| 749 | #endif |
| 750 | {"reltimestr", 1, 1, f_reltimestr}, |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 751 | {"remote_expr", 2, 4, f_remote_expr}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 752 | {"remote_foreground", 1, 1, f_remote_foreground}, |
| 753 | {"remote_peek", 1, 2, f_remote_peek}, |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 754 | {"remote_read", 1, 2, f_remote_read}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 755 | {"remote_send", 2, 3, f_remote_send}, |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 756 | {"remote_startserver", 1, 1, f_remote_startserver}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 757 | {"remove", 2, 3, f_remove}, |
| 758 | {"rename", 2, 2, f_rename}, |
| 759 | {"repeat", 2, 2, f_repeat}, |
| 760 | {"resolve", 1, 1, f_resolve}, |
| 761 | {"reverse", 1, 1, f_reverse}, |
| 762 | #ifdef FEAT_FLOAT |
| 763 | {"round", 1, 1, f_round}, |
| 764 | #endif |
| 765 | {"screenattr", 2, 2, f_screenattr}, |
| 766 | {"screenchar", 2, 2, f_screenchar}, |
| 767 | {"screencol", 0, 0, f_screencol}, |
| 768 | {"screenrow", 0, 0, f_screenrow}, |
| 769 | {"search", 1, 4, f_search}, |
| 770 | {"searchdecl", 1, 3, f_searchdecl}, |
| 771 | {"searchpair", 3, 7, f_searchpair}, |
| 772 | {"searchpairpos", 3, 7, f_searchpairpos}, |
| 773 | {"searchpos", 1, 4, f_searchpos}, |
| 774 | {"server2client", 2, 2, f_server2client}, |
| 775 | {"serverlist", 0, 0, f_serverlist}, |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 776 | {"setbufline", 3, 3, f_setbufline}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 777 | {"setbufvar", 3, 3, f_setbufvar}, |
| 778 | {"setcharsearch", 1, 1, f_setcharsearch}, |
| 779 | {"setcmdpos", 1, 1, f_setcmdpos}, |
| 780 | {"setfperm", 2, 2, f_setfperm}, |
| 781 | {"setline", 2, 2, f_setline}, |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 782 | {"setloclist", 2, 4, f_setloclist}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 783 | {"setmatches", 1, 1, f_setmatches}, |
| 784 | {"setpos", 2, 2, f_setpos}, |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 785 | {"setqflist", 1, 3, f_setqflist}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 786 | {"setreg", 2, 3, f_setreg}, |
| 787 | {"settabvar", 3, 3, f_settabvar}, |
| 788 | {"settabwinvar", 4, 4, f_settabwinvar}, |
| 789 | {"setwinvar", 3, 3, f_setwinvar}, |
| 790 | #ifdef FEAT_CRYPT |
| 791 | {"sha256", 1, 1, f_sha256}, |
| 792 | #endif |
| 793 | {"shellescape", 1, 2, f_shellescape}, |
| 794 | {"shiftwidth", 0, 0, f_shiftwidth}, |
| 795 | {"simplify", 1, 1, f_simplify}, |
| 796 | #ifdef FEAT_FLOAT |
| 797 | {"sin", 1, 1, f_sin}, |
| 798 | {"sinh", 1, 1, f_sinh}, |
| 799 | #endif |
| 800 | {"sort", 1, 3, f_sort}, |
| 801 | {"soundfold", 1, 1, f_soundfold}, |
| 802 | {"spellbadword", 0, 1, f_spellbadword}, |
| 803 | {"spellsuggest", 1, 3, f_spellsuggest}, |
| 804 | {"split", 1, 3, f_split}, |
| 805 | #ifdef FEAT_FLOAT |
| 806 | {"sqrt", 1, 1, f_sqrt}, |
| 807 | {"str2float", 1, 1, f_str2float}, |
| 808 | #endif |
| 809 | {"str2nr", 1, 2, f_str2nr}, |
| 810 | {"strcharpart", 2, 3, f_strcharpart}, |
| 811 | {"strchars", 1, 2, f_strchars}, |
| 812 | {"strdisplaywidth", 1, 2, f_strdisplaywidth}, |
| 813 | #ifdef HAVE_STRFTIME |
| 814 | {"strftime", 1, 2, f_strftime}, |
| 815 | #endif |
| 816 | {"strgetchar", 2, 2, f_strgetchar}, |
| 817 | {"stridx", 2, 3, f_stridx}, |
| 818 | {"string", 1, 1, f_string}, |
| 819 | {"strlen", 1, 1, f_strlen}, |
| 820 | {"strpart", 2, 3, f_strpart}, |
| 821 | {"strridx", 2, 3, f_strridx}, |
| 822 | {"strtrans", 1, 1, f_strtrans}, |
| 823 | {"strwidth", 1, 1, f_strwidth}, |
| 824 | {"submatch", 1, 2, f_submatch}, |
| 825 | {"substitute", 4, 4, f_substitute}, |
| 826 | {"synID", 3, 3, f_synID}, |
| 827 | {"synIDattr", 2, 3, f_synIDattr}, |
| 828 | {"synIDtrans", 1, 1, f_synIDtrans}, |
| 829 | {"synconcealed", 2, 2, f_synconcealed}, |
| 830 | {"synstack", 2, 2, f_synstack}, |
| 831 | {"system", 1, 2, f_system}, |
| 832 | {"systemlist", 1, 2, f_systemlist}, |
| 833 | {"tabpagebuflist", 0, 1, f_tabpagebuflist}, |
| 834 | {"tabpagenr", 0, 1, f_tabpagenr}, |
| 835 | {"tabpagewinnr", 1, 2, f_tabpagewinnr}, |
| 836 | {"tagfiles", 0, 0, f_tagfiles}, |
Bram Moolenaar | c6aafba | 2017-03-21 17:09:10 +0100 | [diff] [blame] | 837 | {"taglist", 1, 2, f_taglist}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 838 | #ifdef FEAT_FLOAT |
| 839 | {"tan", 1, 1, f_tan}, |
| 840 | {"tanh", 1, 1, f_tanh}, |
| 841 | #endif |
| 842 | {"tempname", 0, 0, f_tempname}, |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 843 | #ifdef FEAT_TERMINAL |
Bram Moolenaar | e41e3b4 | 2017-08-11 16:24:50 +0200 | [diff] [blame] | 844 | {"term_getaltscreen", 1, 1, f_term_getaltscreen}, |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 845 | {"term_getattr", 2, 2, f_term_getattr}, |
Bram Moolenaar | 9787000 | 2017-07-30 18:28:38 +0200 | [diff] [blame] | 846 | {"term_getcursor", 1, 1, f_term_getcursor}, |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 847 | {"term_getjob", 1, 1, f_term_getjob}, |
Bram Moolenaar | 4535654 | 2017-08-06 17:53:31 +0200 | [diff] [blame] | 848 | {"term_getline", 2, 2, f_term_getline}, |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 849 | {"term_getscrolled", 1, 1, f_term_getscrolled}, |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 850 | {"term_getsize", 1, 1, f_term_getsize}, |
Bram Moolenaar | b000e32 | 2017-07-30 19:38:21 +0200 | [diff] [blame] | 851 | {"term_getstatus", 1, 1, f_term_getstatus}, |
| 852 | {"term_gettitle", 1, 1, f_term_gettitle}, |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 853 | {"term_gettty", 1, 2, f_term_gettty}, |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 854 | {"term_list", 0, 0, f_term_list}, |
Bram Moolenaar | 4535654 | 2017-08-06 17:53:31 +0200 | [diff] [blame] | 855 | {"term_scrape", 2, 2, f_term_scrape}, |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 856 | {"term_sendkeys", 2, 2, f_term_sendkeys}, |
| 857 | {"term_start", 1, 2, f_term_start}, |
Bram Moolenaar | f3402b1 | 2017-08-06 19:07:08 +0200 | [diff] [blame] | 858 | {"term_wait", 1, 2, f_term_wait}, |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 859 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 860 | {"test_alloc_fail", 3, 3, f_test_alloc_fail}, |
| 861 | {"test_autochdir", 0, 0, f_test_autochdir}, |
Bram Moolenaar | 5e80de3 | 2017-09-03 15:48:12 +0200 | [diff] [blame] | 862 | {"test_feedinput", 1, 1, f_test_feedinput}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 863 | {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now}, |
Bram Moolenaar | e0c31f6 | 2017-03-01 15:07:05 +0100 | [diff] [blame] | 864 | {"test_ignore_error", 1, 1, f_test_ignore_error}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 865 | #ifdef FEAT_JOB_CHANNEL |
| 866 | {"test_null_channel", 0, 0, f_test_null_channel}, |
| 867 | #endif |
| 868 | {"test_null_dict", 0, 0, f_test_null_dict}, |
| 869 | #ifdef FEAT_JOB_CHANNEL |
| 870 | {"test_null_job", 0, 0, f_test_null_job}, |
| 871 | #endif |
| 872 | {"test_null_list", 0, 0, f_test_null_list}, |
| 873 | {"test_null_partial", 0, 0, f_test_null_partial}, |
| 874 | {"test_null_string", 0, 0, f_test_null_string}, |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 875 | {"test_override", 2, 2, f_test_override}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 876 | {"test_settime", 1, 1, f_test_settime}, |
| 877 | #ifdef FEAT_TIMERS |
Bram Moolenaar | 8e97bd7 | 2016-08-06 22:05:07 +0200 | [diff] [blame] | 878 | {"timer_info", 0, 1, f_timer_info}, |
Bram Moolenaar | b73598e | 2016-08-07 18:22:53 +0200 | [diff] [blame] | 879 | {"timer_pause", 2, 2, f_timer_pause}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 880 | {"timer_start", 2, 3, f_timer_start}, |
| 881 | {"timer_stop", 1, 1, f_timer_stop}, |
Bram Moolenaar | b73598e | 2016-08-07 18:22:53 +0200 | [diff] [blame] | 882 | {"timer_stopall", 0, 0, f_timer_stopall}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 883 | #endif |
| 884 | {"tolower", 1, 1, f_tolower}, |
| 885 | {"toupper", 1, 1, f_toupper}, |
| 886 | {"tr", 3, 3, f_tr}, |
| 887 | #ifdef FEAT_FLOAT |
| 888 | {"trunc", 1, 1, f_trunc}, |
| 889 | #endif |
| 890 | {"type", 1, 1, f_type}, |
| 891 | {"undofile", 1, 1, f_undofile}, |
| 892 | {"undotree", 0, 0, f_undotree}, |
| 893 | {"uniq", 1, 3, f_uniq}, |
| 894 | {"values", 1, 1, f_values}, |
| 895 | {"virtcol", 1, 1, f_virtcol}, |
| 896 | {"visualmode", 0, 1, f_visualmode}, |
| 897 | {"wildmenumode", 0, 0, f_wildmenumode}, |
| 898 | {"win_findbuf", 1, 1, f_win_findbuf}, |
| 899 | {"win_getid", 0, 2, f_win_getid}, |
| 900 | {"win_gotoid", 1, 1, f_win_gotoid}, |
| 901 | {"win_id2tabwin", 1, 1, f_win_id2tabwin}, |
| 902 | {"win_id2win", 1, 1, f_win_id2win}, |
Bram Moolenaar | 22044dc | 2017-12-02 15:43:37 +0100 | [diff] [blame] | 903 | {"win_screenpos", 1, 1, f_win_screenpos}, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 904 | {"winbufnr", 1, 1, f_winbufnr}, |
| 905 | {"wincol", 0, 0, f_wincol}, |
| 906 | {"winheight", 1, 1, f_winheight}, |
| 907 | {"winline", 0, 0, f_winline}, |
| 908 | {"winnr", 0, 1, f_winnr}, |
| 909 | {"winrestcmd", 0, 0, f_winrestcmd}, |
| 910 | {"winrestview", 1, 1, f_winrestview}, |
| 911 | {"winsaveview", 0, 0, f_winsaveview}, |
| 912 | {"winwidth", 1, 1, f_winwidth}, |
| 913 | {"wordcount", 0, 0, f_wordcount}, |
| 914 | {"writefile", 2, 3, f_writefile}, |
| 915 | {"xor", 2, 2, f_xor}, |
| 916 | }; |
| 917 | |
| 918 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 919 | |
| 920 | /* |
| 921 | * Function given to ExpandGeneric() to obtain the list of internal |
| 922 | * or user defined function names. |
| 923 | */ |
| 924 | char_u * |
| 925 | get_function_name(expand_T *xp, int idx) |
| 926 | { |
| 927 | static int intidx = -1; |
| 928 | char_u *name; |
| 929 | |
| 930 | if (idx == 0) |
| 931 | intidx = -1; |
| 932 | if (intidx < 0) |
| 933 | { |
| 934 | name = get_user_func_name(xp, idx); |
| 935 | if (name != NULL) |
| 936 | return name; |
| 937 | } |
| 938 | if (++intidx < (int)(sizeof(functions) / sizeof(struct fst))) |
| 939 | { |
| 940 | STRCPY(IObuff, functions[intidx].f_name); |
| 941 | STRCAT(IObuff, "("); |
| 942 | if (functions[intidx].f_max_argc == 0) |
| 943 | STRCAT(IObuff, ")"); |
| 944 | return IObuff; |
| 945 | } |
| 946 | |
| 947 | return NULL; |
| 948 | } |
| 949 | |
| 950 | /* |
| 951 | * Function given to ExpandGeneric() to obtain the list of internal or |
| 952 | * user defined variable or function names. |
| 953 | */ |
| 954 | char_u * |
| 955 | get_expr_name(expand_T *xp, int idx) |
| 956 | { |
| 957 | static int intidx = -1; |
| 958 | char_u *name; |
| 959 | |
| 960 | if (idx == 0) |
| 961 | intidx = -1; |
| 962 | if (intidx < 0) |
| 963 | { |
| 964 | name = get_function_name(xp, idx); |
| 965 | if (name != NULL) |
| 966 | return name; |
| 967 | } |
| 968 | return get_user_var_name(xp, ++intidx); |
| 969 | } |
| 970 | |
| 971 | #endif /* FEAT_CMDL_COMPL */ |
| 972 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 973 | /* |
| 974 | * Find internal function in table above. |
| 975 | * Return index, or -1 if not found |
| 976 | */ |
| 977 | int |
| 978 | find_internal_func( |
| 979 | char_u *name) /* name of the function */ |
| 980 | { |
| 981 | int first = 0; |
| 982 | int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1; |
| 983 | int cmp; |
| 984 | int x; |
| 985 | |
| 986 | /* |
| 987 | * Find the function name in the table. Binary search. |
| 988 | */ |
| 989 | while (first <= last) |
| 990 | { |
| 991 | x = first + ((unsigned)(last - first) >> 1); |
| 992 | cmp = STRCMP(name, functions[x].f_name); |
| 993 | if (cmp < 0) |
| 994 | last = x - 1; |
| 995 | else if (cmp > 0) |
| 996 | first = x + 1; |
| 997 | else |
| 998 | return x; |
| 999 | } |
| 1000 | return -1; |
| 1001 | } |
| 1002 | |
| 1003 | int |
| 1004 | call_internal_func( |
| 1005 | char_u *name, |
| 1006 | int argcount, |
| 1007 | typval_T *argvars, |
| 1008 | typval_T *rettv) |
| 1009 | { |
| 1010 | int i; |
| 1011 | |
| 1012 | i = find_internal_func(name); |
| 1013 | if (i < 0) |
| 1014 | return ERROR_UNKNOWN; |
| 1015 | if (argcount < functions[i].f_min_argc) |
| 1016 | return ERROR_TOOFEW; |
| 1017 | if (argcount > functions[i].f_max_argc) |
| 1018 | return ERROR_TOOMANY; |
| 1019 | argvars[argcount].v_type = VAR_UNKNOWN; |
| 1020 | functions[i].f_func(argvars, rettv); |
| 1021 | return ERROR_NONE; |
| 1022 | } |
| 1023 | |
| 1024 | /* |
| 1025 | * Return TRUE for a non-zero Number and a non-empty String. |
| 1026 | */ |
| 1027 | static int |
| 1028 | non_zero_arg(typval_T *argvars) |
| 1029 | { |
| 1030 | return ((argvars[0].v_type == VAR_NUMBER |
| 1031 | && argvars[0].vval.v_number != 0) |
| 1032 | || (argvars[0].v_type == VAR_SPECIAL |
| 1033 | && argvars[0].vval.v_number == VVAL_TRUE) |
| 1034 | || (argvars[0].v_type == VAR_STRING |
| 1035 | && argvars[0].vval.v_string != NULL |
| 1036 | && *argvars[0].vval.v_string != NUL)); |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * Get the lnum from the first argument. |
| 1041 | * Also accepts ".", "$", etc., but that only works for the current buffer. |
| 1042 | * Returns -1 on error. |
| 1043 | */ |
| 1044 | static linenr_T |
| 1045 | get_tv_lnum(typval_T *argvars) |
| 1046 | { |
| 1047 | typval_T rettv; |
| 1048 | linenr_T lnum; |
| 1049 | |
| 1050 | lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL); |
| 1051 | if (lnum == 0) /* no valid number, try using line() */ |
| 1052 | { |
| 1053 | rettv.v_type = VAR_NUMBER; |
| 1054 | f_line(argvars, &rettv); |
| 1055 | lnum = (linenr_T)rettv.vval.v_number; |
| 1056 | clear_tv(&rettv); |
| 1057 | } |
| 1058 | return lnum; |
| 1059 | } |
| 1060 | |
| 1061 | #ifdef FEAT_FLOAT |
| 1062 | static int get_float_arg(typval_T *argvars, float_T *f); |
| 1063 | |
| 1064 | /* |
| 1065 | * Get the float value of "argvars[0]" into "f". |
| 1066 | * Returns FAIL when the argument is not a Number or Float. |
| 1067 | */ |
| 1068 | static int |
| 1069 | get_float_arg(typval_T *argvars, float_T *f) |
| 1070 | { |
| 1071 | if (argvars[0].v_type == VAR_FLOAT) |
| 1072 | { |
| 1073 | *f = argvars[0].vval.v_float; |
| 1074 | return OK; |
| 1075 | } |
| 1076 | if (argvars[0].v_type == VAR_NUMBER) |
| 1077 | { |
| 1078 | *f = (float_T)argvars[0].vval.v_number; |
| 1079 | return OK; |
| 1080 | } |
| 1081 | EMSG(_("E808: Number or Float required")); |
| 1082 | return FAIL; |
| 1083 | } |
| 1084 | |
| 1085 | /* |
| 1086 | * "abs(expr)" function |
| 1087 | */ |
| 1088 | static void |
| 1089 | f_abs(typval_T *argvars, typval_T *rettv) |
| 1090 | { |
| 1091 | if (argvars[0].v_type == VAR_FLOAT) |
| 1092 | { |
| 1093 | rettv->v_type = VAR_FLOAT; |
| 1094 | rettv->vval.v_float = fabs(argvars[0].vval.v_float); |
| 1095 | } |
| 1096 | else |
| 1097 | { |
| 1098 | varnumber_T n; |
| 1099 | int error = FALSE; |
| 1100 | |
| 1101 | n = get_tv_number_chk(&argvars[0], &error); |
| 1102 | if (error) |
| 1103 | rettv->vval.v_number = -1; |
| 1104 | else if (n > 0) |
| 1105 | rettv->vval.v_number = n; |
| 1106 | else |
| 1107 | rettv->vval.v_number = -n; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | * "acos()" function |
| 1113 | */ |
| 1114 | static void |
| 1115 | f_acos(typval_T *argvars, typval_T *rettv) |
| 1116 | { |
| 1117 | float_T f = 0.0; |
| 1118 | |
| 1119 | rettv->v_type = VAR_FLOAT; |
| 1120 | if (get_float_arg(argvars, &f) == OK) |
| 1121 | rettv->vval.v_float = acos(f); |
| 1122 | else |
| 1123 | rettv->vval.v_float = 0.0; |
| 1124 | } |
| 1125 | #endif |
| 1126 | |
| 1127 | /* |
| 1128 | * "add(list, item)" function |
| 1129 | */ |
| 1130 | static void |
| 1131 | f_add(typval_T *argvars, typval_T *rettv) |
| 1132 | { |
| 1133 | list_T *l; |
| 1134 | |
| 1135 | rettv->vval.v_number = 1; /* Default: Failed */ |
| 1136 | if (argvars[0].v_type == VAR_LIST) |
| 1137 | { |
| 1138 | if ((l = argvars[0].vval.v_list) != NULL |
| 1139 | && !tv_check_lock(l->lv_lock, |
| 1140 | (char_u *)N_("add() argument"), TRUE) |
| 1141 | && list_append_tv(l, &argvars[1]) == OK) |
| 1142 | copy_tv(&argvars[0], rettv); |
| 1143 | } |
| 1144 | else |
| 1145 | EMSG(_(e_listreq)); |
| 1146 | } |
| 1147 | |
| 1148 | /* |
| 1149 | * "and(expr, expr)" function |
| 1150 | */ |
| 1151 | static void |
| 1152 | f_and(typval_T *argvars, typval_T *rettv) |
| 1153 | { |
| 1154 | rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL) |
| 1155 | & get_tv_number_chk(&argvars[1], NULL); |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * "append(lnum, string/list)" function |
| 1160 | */ |
| 1161 | static void |
| 1162 | f_append(typval_T *argvars, typval_T *rettv) |
| 1163 | { |
| 1164 | long lnum; |
| 1165 | char_u *line; |
| 1166 | list_T *l = NULL; |
| 1167 | listitem_T *li = NULL; |
| 1168 | typval_T *tv; |
| 1169 | long added = 0; |
| 1170 | |
| 1171 | /* When coming here from Insert mode, sync undo, so that this can be |
| 1172 | * undone separately from what was previously inserted. */ |
| 1173 | if (u_sync_once == 2) |
| 1174 | { |
| 1175 | u_sync_once = 1; /* notify that u_sync() was called */ |
| 1176 | u_sync(TRUE); |
| 1177 | } |
| 1178 | |
| 1179 | lnum = get_tv_lnum(argvars); |
| 1180 | if (lnum >= 0 |
| 1181 | && lnum <= curbuf->b_ml.ml_line_count |
| 1182 | && u_save(lnum, lnum + 1) == OK) |
| 1183 | { |
| 1184 | if (argvars[1].v_type == VAR_LIST) |
| 1185 | { |
| 1186 | l = argvars[1].vval.v_list; |
| 1187 | if (l == NULL) |
| 1188 | return; |
| 1189 | li = l->lv_first; |
| 1190 | } |
| 1191 | for (;;) |
| 1192 | { |
| 1193 | if (l == NULL) |
| 1194 | tv = &argvars[1]; /* append a string */ |
| 1195 | else if (li == NULL) |
| 1196 | break; /* end of list */ |
| 1197 | else |
| 1198 | tv = &li->li_tv; /* append item from list */ |
| 1199 | line = get_tv_string_chk(tv); |
| 1200 | if (line == NULL) /* type error */ |
| 1201 | { |
| 1202 | rettv->vval.v_number = 1; /* Failed */ |
| 1203 | break; |
| 1204 | } |
| 1205 | ml_append(lnum + added, line, (colnr_T)0, FALSE); |
| 1206 | ++added; |
| 1207 | if (l == NULL) |
| 1208 | break; |
| 1209 | li = li->li_next; |
| 1210 | } |
| 1211 | |
| 1212 | appended_lines_mark(lnum, added); |
| 1213 | if (curwin->w_cursor.lnum > lnum) |
| 1214 | curwin->w_cursor.lnum += added; |
| 1215 | } |
| 1216 | else |
| 1217 | rettv->vval.v_number = 1; /* Failed */ |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | * "argc()" function |
| 1222 | */ |
| 1223 | static void |
| 1224 | f_argc(typval_T *argvars UNUSED, typval_T *rettv) |
| 1225 | { |
| 1226 | rettv->vval.v_number = ARGCOUNT; |
| 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | * "argidx()" function |
| 1231 | */ |
| 1232 | static void |
| 1233 | f_argidx(typval_T *argvars UNUSED, typval_T *rettv) |
| 1234 | { |
| 1235 | rettv->vval.v_number = curwin->w_arg_idx; |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * "arglistid()" function |
| 1240 | */ |
| 1241 | static void |
| 1242 | f_arglistid(typval_T *argvars, typval_T *rettv) |
| 1243 | { |
| 1244 | win_T *wp; |
| 1245 | |
| 1246 | rettv->vval.v_number = -1; |
| 1247 | wp = find_tabwin(&argvars[0], &argvars[1]); |
| 1248 | if (wp != NULL) |
| 1249 | rettv->vval.v_number = wp->w_alist->id; |
| 1250 | } |
| 1251 | |
| 1252 | /* |
| 1253 | * "argv(nr)" function |
| 1254 | */ |
| 1255 | static void |
| 1256 | f_argv(typval_T *argvars, typval_T *rettv) |
| 1257 | { |
| 1258 | int idx; |
| 1259 | |
| 1260 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 1261 | { |
| 1262 | idx = (int)get_tv_number_chk(&argvars[0], NULL); |
| 1263 | if (idx >= 0 && idx < ARGCOUNT) |
| 1264 | rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx])); |
| 1265 | else |
| 1266 | rettv->vval.v_string = NULL; |
| 1267 | rettv->v_type = VAR_STRING; |
| 1268 | } |
| 1269 | else if (rettv_list_alloc(rettv) == OK) |
| 1270 | for (idx = 0; idx < ARGCOUNT; ++idx) |
| 1271 | list_append_string(rettv->vval.v_list, |
| 1272 | alist_name(&ARGLIST[idx]), -1); |
| 1273 | } |
| 1274 | |
| 1275 | /* |
| 1276 | * "assert_equal(expected, actual[, msg])" function |
| 1277 | */ |
| 1278 | static void |
| 1279 | f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED) |
| 1280 | { |
| 1281 | assert_equal_common(argvars, ASSERT_EQUAL); |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * "assert_notequal(expected, actual[, msg])" function |
| 1286 | */ |
| 1287 | static void |
| 1288 | f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED) |
| 1289 | { |
| 1290 | assert_equal_common(argvars, ASSERT_NOTEQUAL); |
| 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * "assert_exception(string[, msg])" function |
| 1295 | */ |
| 1296 | static void |
| 1297 | f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED) |
| 1298 | { |
| 1299 | assert_exception(argvars); |
| 1300 | } |
| 1301 | |
| 1302 | /* |
| 1303 | * "assert_fails(cmd [, error])" function |
| 1304 | */ |
| 1305 | static void |
| 1306 | f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED) |
| 1307 | { |
| 1308 | assert_fails(argvars); |
| 1309 | } |
| 1310 | |
| 1311 | /* |
| 1312 | * "assert_false(actual[, msg])" function |
| 1313 | */ |
| 1314 | static void |
| 1315 | f_assert_false(typval_T *argvars, typval_T *rettv UNUSED) |
| 1316 | { |
| 1317 | assert_bool(argvars, FALSE); |
| 1318 | } |
| 1319 | |
| 1320 | /* |
Bram Moolenaar | 61c0449 | 2016-07-23 15:35:35 +0200 | [diff] [blame] | 1321 | * "assert_inrange(lower, upper[, msg])" function |
| 1322 | */ |
| 1323 | static void |
| 1324 | f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED) |
| 1325 | { |
| 1326 | assert_inrange(argvars); |
| 1327 | } |
| 1328 | |
| 1329 | /* |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1330 | * "assert_match(pattern, actual[, msg])" function |
| 1331 | */ |
| 1332 | static void |
| 1333 | f_assert_match(typval_T *argvars, typval_T *rettv UNUSED) |
| 1334 | { |
| 1335 | assert_match_common(argvars, ASSERT_MATCH); |
| 1336 | } |
| 1337 | |
| 1338 | /* |
| 1339 | * "assert_notmatch(pattern, actual[, msg])" function |
| 1340 | */ |
| 1341 | static void |
| 1342 | f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED) |
| 1343 | { |
| 1344 | assert_match_common(argvars, ASSERT_NOTMATCH); |
| 1345 | } |
| 1346 | |
| 1347 | /* |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 1348 | * "assert_report(msg)" function |
| 1349 | */ |
| 1350 | static void |
| 1351 | f_assert_report(typval_T *argvars, typval_T *rettv UNUSED) |
| 1352 | { |
| 1353 | assert_report(argvars); |
| 1354 | } |
| 1355 | |
| 1356 | /* |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1357 | * "assert_true(actual[, msg])" function |
| 1358 | */ |
| 1359 | static void |
| 1360 | f_assert_true(typval_T *argvars, typval_T *rettv UNUSED) |
| 1361 | { |
| 1362 | assert_bool(argvars, TRUE); |
| 1363 | } |
| 1364 | |
| 1365 | #ifdef FEAT_FLOAT |
| 1366 | /* |
| 1367 | * "asin()" function |
| 1368 | */ |
| 1369 | static void |
| 1370 | f_asin(typval_T *argvars, typval_T *rettv) |
| 1371 | { |
| 1372 | float_T f = 0.0; |
| 1373 | |
| 1374 | rettv->v_type = VAR_FLOAT; |
| 1375 | if (get_float_arg(argvars, &f) == OK) |
| 1376 | rettv->vval.v_float = asin(f); |
| 1377 | else |
| 1378 | rettv->vval.v_float = 0.0; |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | * "atan()" function |
| 1383 | */ |
| 1384 | static void |
| 1385 | f_atan(typval_T *argvars, typval_T *rettv) |
| 1386 | { |
| 1387 | float_T f = 0.0; |
| 1388 | |
| 1389 | rettv->v_type = VAR_FLOAT; |
| 1390 | if (get_float_arg(argvars, &f) == OK) |
| 1391 | rettv->vval.v_float = atan(f); |
| 1392 | else |
| 1393 | rettv->vval.v_float = 0.0; |
| 1394 | } |
| 1395 | |
| 1396 | /* |
| 1397 | * "atan2()" function |
| 1398 | */ |
| 1399 | static void |
| 1400 | f_atan2(typval_T *argvars, typval_T *rettv) |
| 1401 | { |
| 1402 | float_T fx = 0.0, fy = 0.0; |
| 1403 | |
| 1404 | rettv->v_type = VAR_FLOAT; |
| 1405 | if (get_float_arg(argvars, &fx) == OK |
| 1406 | && get_float_arg(&argvars[1], &fy) == OK) |
| 1407 | rettv->vval.v_float = atan2(fx, fy); |
| 1408 | else |
| 1409 | rettv->vval.v_float = 0.0; |
| 1410 | } |
| 1411 | #endif |
| 1412 | |
| 1413 | /* |
Bram Moolenaar | 59716a2 | 2017-03-01 20:32:44 +0100 | [diff] [blame] | 1414 | * "balloon_show()" function |
| 1415 | */ |
| 1416 | #ifdef FEAT_BEVAL |
| 1417 | static void |
| 1418 | f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED) |
| 1419 | { |
Bram Moolenaar | caf6434 | 2017-03-02 22:11:33 +0100 | [diff] [blame] | 1420 | if (balloonEval != NULL) |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 1421 | { |
| 1422 | if (argvars[0].v_type == VAR_LIST |
| 1423 | # ifdef FEAT_GUI |
| 1424 | && !gui.in_use |
| 1425 | # endif |
| 1426 | ) |
| 1427 | post_balloon(balloonEval, NULL, argvars[0].vval.v_list); |
| 1428 | else |
| 1429 | post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL); |
| 1430 | } |
| 1431 | } |
| 1432 | |
Bram Moolenaar | 669a828 | 2017-11-19 20:13:05 +0100 | [diff] [blame] | 1433 | # if defined(FEAT_BEVAL_TERM) |
Bram Moolenaar | 246fe03 | 2017-11-19 19:56:27 +0100 | [diff] [blame] | 1434 | static void |
| 1435 | f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED) |
| 1436 | { |
| 1437 | if (rettv_list_alloc(rettv) == OK) |
| 1438 | { |
| 1439 | char_u *msg = get_tv_string_chk(&argvars[0]); |
| 1440 | |
| 1441 | if (msg != NULL) |
| 1442 | { |
| 1443 | pumitem_T *array; |
| 1444 | int size = split_message(msg, &array); |
| 1445 | int i; |
| 1446 | |
| 1447 | /* Skip the first and last item, they are always empty. */ |
| 1448 | for (i = 1; i < size - 1; ++i) |
| 1449 | list_append_string(rettv->vval.v_list, array[i].pum_text, -1); |
| 1450 | vim_free(array); |
| 1451 | } |
| 1452 | } |
Bram Moolenaar | 59716a2 | 2017-03-01 20:32:44 +0100 | [diff] [blame] | 1453 | } |
Bram Moolenaar | 669a828 | 2017-11-19 20:13:05 +0100 | [diff] [blame] | 1454 | # endif |
Bram Moolenaar | 59716a2 | 2017-03-01 20:32:44 +0100 | [diff] [blame] | 1455 | #endif |
| 1456 | |
| 1457 | /* |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1458 | * "browse(save, title, initdir, default)" function |
| 1459 | */ |
| 1460 | static void |
| 1461 | f_browse(typval_T *argvars UNUSED, typval_T *rettv) |
| 1462 | { |
| 1463 | #ifdef FEAT_BROWSE |
| 1464 | int save; |
| 1465 | char_u *title; |
| 1466 | char_u *initdir; |
| 1467 | char_u *defname; |
| 1468 | char_u buf[NUMBUFLEN]; |
| 1469 | char_u buf2[NUMBUFLEN]; |
| 1470 | int error = FALSE; |
| 1471 | |
| 1472 | save = (int)get_tv_number_chk(&argvars[0], &error); |
| 1473 | title = get_tv_string_chk(&argvars[1]); |
| 1474 | initdir = get_tv_string_buf_chk(&argvars[2], buf); |
| 1475 | defname = get_tv_string_buf_chk(&argvars[3], buf2); |
| 1476 | |
| 1477 | if (error || title == NULL || initdir == NULL || defname == NULL) |
| 1478 | rettv->vval.v_string = NULL; |
| 1479 | else |
| 1480 | rettv->vval.v_string = |
| 1481 | do_browse(save ? BROWSE_SAVE : 0, |
| 1482 | title, defname, NULL, initdir, NULL, curbuf); |
| 1483 | #else |
| 1484 | rettv->vval.v_string = NULL; |
| 1485 | #endif |
| 1486 | rettv->v_type = VAR_STRING; |
| 1487 | } |
| 1488 | |
| 1489 | /* |
| 1490 | * "browsedir(title, initdir)" function |
| 1491 | */ |
| 1492 | static void |
| 1493 | f_browsedir(typval_T *argvars UNUSED, typval_T *rettv) |
| 1494 | { |
| 1495 | #ifdef FEAT_BROWSE |
| 1496 | char_u *title; |
| 1497 | char_u *initdir; |
| 1498 | char_u buf[NUMBUFLEN]; |
| 1499 | |
| 1500 | title = get_tv_string_chk(&argvars[0]); |
| 1501 | initdir = get_tv_string_buf_chk(&argvars[1], buf); |
| 1502 | |
| 1503 | if (title == NULL || initdir == NULL) |
| 1504 | rettv->vval.v_string = NULL; |
| 1505 | else |
| 1506 | rettv->vval.v_string = do_browse(BROWSE_DIR, |
| 1507 | title, NULL, NULL, initdir, NULL, curbuf); |
| 1508 | #else |
| 1509 | rettv->vval.v_string = NULL; |
| 1510 | #endif |
| 1511 | rettv->v_type = VAR_STRING; |
| 1512 | } |
| 1513 | |
| 1514 | static buf_T *find_buffer(typval_T *avar); |
| 1515 | |
| 1516 | /* |
| 1517 | * Find a buffer by number or exact name. |
| 1518 | */ |
| 1519 | static buf_T * |
| 1520 | find_buffer(typval_T *avar) |
| 1521 | { |
| 1522 | buf_T *buf = NULL; |
| 1523 | |
| 1524 | if (avar->v_type == VAR_NUMBER) |
| 1525 | buf = buflist_findnr((int)avar->vval.v_number); |
| 1526 | else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL) |
| 1527 | { |
| 1528 | buf = buflist_findname_exp(avar->vval.v_string); |
| 1529 | if (buf == NULL) |
| 1530 | { |
| 1531 | /* No full path name match, try a match with a URL or a "nofile" |
| 1532 | * buffer, these don't use the full path. */ |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 1533 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1534 | if (buf->b_fname != NULL |
| 1535 | && (path_with_url(buf->b_fname) |
| 1536 | #ifdef FEAT_QUICKFIX |
| 1537 | || bt_nofile(buf) |
| 1538 | #endif |
| 1539 | ) |
| 1540 | && STRCMP(buf->b_fname, avar->vval.v_string) == 0) |
| 1541 | break; |
| 1542 | } |
| 1543 | } |
| 1544 | return buf; |
| 1545 | } |
| 1546 | |
| 1547 | /* |
| 1548 | * "bufexists(expr)" function |
| 1549 | */ |
| 1550 | static void |
| 1551 | f_bufexists(typval_T *argvars, typval_T *rettv) |
| 1552 | { |
| 1553 | rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL); |
| 1554 | } |
| 1555 | |
| 1556 | /* |
| 1557 | * "buflisted(expr)" function |
| 1558 | */ |
| 1559 | static void |
| 1560 | f_buflisted(typval_T *argvars, typval_T *rettv) |
| 1561 | { |
| 1562 | buf_T *buf; |
| 1563 | |
| 1564 | buf = find_buffer(&argvars[0]); |
| 1565 | rettv->vval.v_number = (buf != NULL && buf->b_p_bl); |
| 1566 | } |
| 1567 | |
| 1568 | /* |
| 1569 | * "bufloaded(expr)" function |
| 1570 | */ |
| 1571 | static void |
| 1572 | f_bufloaded(typval_T *argvars, typval_T *rettv) |
| 1573 | { |
| 1574 | buf_T *buf; |
| 1575 | |
| 1576 | buf = find_buffer(&argvars[0]); |
| 1577 | rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL); |
| 1578 | } |
| 1579 | |
| 1580 | buf_T * |
| 1581 | buflist_find_by_name(char_u *name, int curtab_only) |
| 1582 | { |
| 1583 | int save_magic; |
| 1584 | char_u *save_cpo; |
| 1585 | buf_T *buf; |
| 1586 | |
| 1587 | /* Ignore 'magic' and 'cpoptions' here to make scripts portable */ |
| 1588 | save_magic = p_magic; |
| 1589 | p_magic = TRUE; |
| 1590 | save_cpo = p_cpo; |
| 1591 | p_cpo = (char_u *)""; |
| 1592 | |
| 1593 | buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name), |
| 1594 | TRUE, FALSE, curtab_only)); |
| 1595 | |
| 1596 | p_magic = save_magic; |
| 1597 | p_cpo = save_cpo; |
| 1598 | return buf; |
| 1599 | } |
| 1600 | |
| 1601 | /* |
| 1602 | * Get buffer by number or pattern. |
| 1603 | */ |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 1604 | buf_T * |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1605 | get_buf_tv(typval_T *tv, int curtab_only) |
| 1606 | { |
| 1607 | char_u *name = tv->vval.v_string; |
| 1608 | buf_T *buf; |
| 1609 | |
| 1610 | if (tv->v_type == VAR_NUMBER) |
| 1611 | return buflist_findnr((int)tv->vval.v_number); |
| 1612 | if (tv->v_type != VAR_STRING) |
| 1613 | return NULL; |
| 1614 | if (name == NULL || *name == NUL) |
| 1615 | return curbuf; |
| 1616 | if (name[0] == '$' && name[1] == NUL) |
| 1617 | return lastbuf; |
| 1618 | |
| 1619 | buf = buflist_find_by_name(name, curtab_only); |
| 1620 | |
| 1621 | /* If not found, try expanding the name, like done for bufexists(). */ |
| 1622 | if (buf == NULL) |
| 1623 | buf = find_buffer(tv); |
| 1624 | |
| 1625 | return buf; |
| 1626 | } |
| 1627 | |
| 1628 | /* |
| 1629 | * "bufname(expr)" function |
| 1630 | */ |
| 1631 | static void |
| 1632 | f_bufname(typval_T *argvars, typval_T *rettv) |
| 1633 | { |
| 1634 | buf_T *buf; |
| 1635 | |
| 1636 | (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ |
| 1637 | ++emsg_off; |
| 1638 | buf = get_buf_tv(&argvars[0], FALSE); |
| 1639 | rettv->v_type = VAR_STRING; |
| 1640 | if (buf != NULL && buf->b_fname != NULL) |
| 1641 | rettv->vval.v_string = vim_strsave(buf->b_fname); |
| 1642 | else |
| 1643 | rettv->vval.v_string = NULL; |
| 1644 | --emsg_off; |
| 1645 | } |
| 1646 | |
| 1647 | /* |
| 1648 | * "bufnr(expr)" function |
| 1649 | */ |
| 1650 | static void |
| 1651 | f_bufnr(typval_T *argvars, typval_T *rettv) |
| 1652 | { |
| 1653 | buf_T *buf; |
| 1654 | int error = FALSE; |
| 1655 | char_u *name; |
| 1656 | |
| 1657 | (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ |
| 1658 | ++emsg_off; |
| 1659 | buf = get_buf_tv(&argvars[0], FALSE); |
| 1660 | --emsg_off; |
| 1661 | |
| 1662 | /* If the buffer isn't found and the second argument is not zero create a |
| 1663 | * new buffer. */ |
| 1664 | if (buf == NULL |
| 1665 | && argvars[1].v_type != VAR_UNKNOWN |
| 1666 | && get_tv_number_chk(&argvars[1], &error) != 0 |
| 1667 | && !error |
| 1668 | && (name = get_tv_string_chk(&argvars[0])) != NULL |
| 1669 | && !error) |
| 1670 | buf = buflist_new(name, NULL, (linenr_T)1, 0); |
| 1671 | |
| 1672 | if (buf != NULL) |
| 1673 | rettv->vval.v_number = buf->b_fnum; |
| 1674 | else |
| 1675 | rettv->vval.v_number = -1; |
| 1676 | } |
| 1677 | |
| 1678 | static void |
| 1679 | buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr) |
| 1680 | { |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1681 | win_T *wp; |
| 1682 | int winnr = 0; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1683 | buf_T *buf; |
| 1684 | |
| 1685 | (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ |
| 1686 | ++emsg_off; |
| 1687 | buf = get_buf_tv(&argvars[0], TRUE); |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 1688 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1689 | { |
| 1690 | ++winnr; |
| 1691 | if (wp->w_buffer == buf) |
| 1692 | break; |
| 1693 | } |
| 1694 | rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1695 | --emsg_off; |
| 1696 | } |
| 1697 | |
| 1698 | /* |
| 1699 | * "bufwinid(nr)" function |
| 1700 | */ |
| 1701 | static void |
| 1702 | f_bufwinid(typval_T *argvars, typval_T *rettv) |
| 1703 | { |
| 1704 | buf_win_common(argvars, rettv, FALSE); |
| 1705 | } |
| 1706 | |
| 1707 | /* |
| 1708 | * "bufwinnr(nr)" function |
| 1709 | */ |
| 1710 | static void |
| 1711 | f_bufwinnr(typval_T *argvars, typval_T *rettv) |
| 1712 | { |
| 1713 | buf_win_common(argvars, rettv, TRUE); |
| 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | * "byte2line(byte)" function |
| 1718 | */ |
| 1719 | static void |
| 1720 | f_byte2line(typval_T *argvars UNUSED, typval_T *rettv) |
| 1721 | { |
| 1722 | #ifndef FEAT_BYTEOFF |
| 1723 | rettv->vval.v_number = -1; |
| 1724 | #else |
| 1725 | long boff = 0; |
| 1726 | |
| 1727 | boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */ |
| 1728 | if (boff < 0) |
| 1729 | rettv->vval.v_number = -1; |
| 1730 | else |
| 1731 | rettv->vval.v_number = ml_find_line_or_offset(curbuf, |
| 1732 | (linenr_T)0, &boff); |
| 1733 | #endif |
| 1734 | } |
| 1735 | |
| 1736 | static void |
| 1737 | byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED) |
| 1738 | { |
| 1739 | #ifdef FEAT_MBYTE |
| 1740 | char_u *t; |
| 1741 | #endif |
| 1742 | char_u *str; |
| 1743 | varnumber_T idx; |
| 1744 | |
| 1745 | str = get_tv_string_chk(&argvars[0]); |
| 1746 | idx = get_tv_number_chk(&argvars[1], NULL); |
| 1747 | rettv->vval.v_number = -1; |
| 1748 | if (str == NULL || idx < 0) |
| 1749 | return; |
| 1750 | |
| 1751 | #ifdef FEAT_MBYTE |
| 1752 | t = str; |
| 1753 | for ( ; idx > 0; idx--) |
| 1754 | { |
| 1755 | if (*t == NUL) /* EOL reached */ |
| 1756 | return; |
| 1757 | if (enc_utf8 && comp) |
| 1758 | t += utf_ptr2len(t); |
| 1759 | else |
| 1760 | t += (*mb_ptr2len)(t); |
| 1761 | } |
| 1762 | rettv->vval.v_number = (varnumber_T)(t - str); |
| 1763 | #else |
| 1764 | if ((size_t)idx <= STRLEN(str)) |
| 1765 | rettv->vval.v_number = idx; |
| 1766 | #endif |
| 1767 | } |
| 1768 | |
| 1769 | /* |
| 1770 | * "byteidx()" function |
| 1771 | */ |
| 1772 | static void |
| 1773 | f_byteidx(typval_T *argvars, typval_T *rettv) |
| 1774 | { |
| 1775 | byteidx(argvars, rettv, FALSE); |
| 1776 | } |
| 1777 | |
| 1778 | /* |
| 1779 | * "byteidxcomp()" function |
| 1780 | */ |
| 1781 | static void |
| 1782 | f_byteidxcomp(typval_T *argvars, typval_T *rettv) |
| 1783 | { |
| 1784 | byteidx(argvars, rettv, TRUE); |
| 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | * "call(func, arglist [, dict])" function |
| 1789 | */ |
| 1790 | static void |
| 1791 | f_call(typval_T *argvars, typval_T *rettv) |
| 1792 | { |
| 1793 | char_u *func; |
| 1794 | partial_T *partial = NULL; |
| 1795 | dict_T *selfdict = NULL; |
| 1796 | |
| 1797 | if (argvars[1].v_type != VAR_LIST) |
| 1798 | { |
| 1799 | EMSG(_(e_listreq)); |
| 1800 | return; |
| 1801 | } |
| 1802 | if (argvars[1].vval.v_list == NULL) |
| 1803 | return; |
| 1804 | |
| 1805 | if (argvars[0].v_type == VAR_FUNC) |
| 1806 | func = argvars[0].vval.v_string; |
| 1807 | else if (argvars[0].v_type == VAR_PARTIAL) |
| 1808 | { |
| 1809 | partial = argvars[0].vval.v_partial; |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 1810 | func = partial_name(partial); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1811 | } |
| 1812 | else |
| 1813 | func = get_tv_string(&argvars[0]); |
| 1814 | if (*func == NUL) |
| 1815 | return; /* type error or empty name */ |
| 1816 | |
| 1817 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1818 | { |
| 1819 | if (argvars[2].v_type != VAR_DICT) |
| 1820 | { |
| 1821 | EMSG(_(e_dictreq)); |
| 1822 | return; |
| 1823 | } |
| 1824 | selfdict = argvars[2].vval.v_dict; |
| 1825 | } |
| 1826 | |
| 1827 | (void)func_call(func, &argvars[1], partial, selfdict, rettv); |
| 1828 | } |
| 1829 | |
| 1830 | #ifdef FEAT_FLOAT |
| 1831 | /* |
| 1832 | * "ceil({float})" function |
| 1833 | */ |
| 1834 | static void |
| 1835 | f_ceil(typval_T *argvars, typval_T *rettv) |
| 1836 | { |
| 1837 | float_T f = 0.0; |
| 1838 | |
| 1839 | rettv->v_type = VAR_FLOAT; |
| 1840 | if (get_float_arg(argvars, &f) == OK) |
| 1841 | rettv->vval.v_float = ceil(f); |
| 1842 | else |
| 1843 | rettv->vval.v_float = 0.0; |
| 1844 | } |
| 1845 | #endif |
| 1846 | |
| 1847 | #ifdef FEAT_JOB_CHANNEL |
| 1848 | /* |
Bram Moolenaar | 4b785f6 | 2016-11-29 21:54:44 +0100 | [diff] [blame] | 1849 | * "ch_canread()" function |
| 1850 | */ |
| 1851 | static void |
| 1852 | f_ch_canread(typval_T *argvars, typval_T *rettv) |
| 1853 | { |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 1854 | channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0); |
Bram Moolenaar | 4b785f6 | 2016-11-29 21:54:44 +0100 | [diff] [blame] | 1855 | |
| 1856 | rettv->vval.v_number = 0; |
| 1857 | if (channel != NULL) |
| 1858 | rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK) |
| 1859 | || channel_has_readahead(channel, PART_OUT) |
| 1860 | || channel_has_readahead(channel, PART_ERR); |
| 1861 | } |
| 1862 | |
| 1863 | /* |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1864 | * "ch_close()" function |
| 1865 | */ |
| 1866 | static void |
| 1867 | f_ch_close(typval_T *argvars, typval_T *rettv UNUSED) |
| 1868 | { |
| 1869 | channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0); |
| 1870 | |
| 1871 | if (channel != NULL) |
| 1872 | { |
| 1873 | channel_close(channel, FALSE); |
| 1874 | channel_clear(channel); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | /* |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 1879 | * "ch_close()" function |
| 1880 | */ |
| 1881 | static void |
| 1882 | f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED) |
| 1883 | { |
| 1884 | channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0); |
| 1885 | |
| 1886 | if (channel != NULL) |
| 1887 | channel_close_in(channel); |
| 1888 | } |
| 1889 | |
| 1890 | /* |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1891 | * "ch_getbufnr()" function |
| 1892 | */ |
| 1893 | static void |
| 1894 | f_ch_getbufnr(typval_T *argvars, typval_T *rettv) |
| 1895 | { |
| 1896 | channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0); |
| 1897 | |
| 1898 | rettv->vval.v_number = -1; |
| 1899 | if (channel != NULL) |
| 1900 | { |
| 1901 | char_u *what = get_tv_string(&argvars[1]); |
| 1902 | int part; |
| 1903 | |
| 1904 | if (STRCMP(what, "err") == 0) |
| 1905 | part = PART_ERR; |
| 1906 | else if (STRCMP(what, "out") == 0) |
| 1907 | part = PART_OUT; |
| 1908 | else if (STRCMP(what, "in") == 0) |
| 1909 | part = PART_IN; |
| 1910 | else |
| 1911 | part = PART_SOCK; |
| 1912 | if (channel->ch_part[part].ch_bufref.br_buf != NULL) |
| 1913 | rettv->vval.v_number = |
| 1914 | channel->ch_part[part].ch_bufref.br_buf->b_fnum; |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | /* |
| 1919 | * "ch_getjob()" function |
| 1920 | */ |
| 1921 | static void |
| 1922 | f_ch_getjob(typval_T *argvars, typval_T *rettv) |
| 1923 | { |
| 1924 | channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0); |
| 1925 | |
| 1926 | if (channel != NULL) |
| 1927 | { |
| 1928 | rettv->v_type = VAR_JOB; |
| 1929 | rettv->vval.v_job = channel->ch_job; |
| 1930 | if (channel->ch_job != NULL) |
| 1931 | ++channel->ch_job->jv_refcount; |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | /* |
| 1936 | * "ch_info()" function |
| 1937 | */ |
| 1938 | static void |
| 1939 | f_ch_info(typval_T *argvars, typval_T *rettv UNUSED) |
| 1940 | { |
| 1941 | channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0); |
| 1942 | |
| 1943 | if (channel != NULL && rettv_dict_alloc(rettv) != FAIL) |
| 1944 | channel_info(channel, rettv->vval.v_dict); |
| 1945 | } |
| 1946 | |
| 1947 | /* |
| 1948 | * "ch_log()" function |
| 1949 | */ |
| 1950 | static void |
| 1951 | f_ch_log(typval_T *argvars, typval_T *rettv UNUSED) |
| 1952 | { |
| 1953 | char_u *msg = get_tv_string(&argvars[0]); |
| 1954 | channel_T *channel = NULL; |
| 1955 | |
| 1956 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1957 | channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0); |
| 1958 | |
| 1959 | ch_log(channel, (char *)msg); |
| 1960 | } |
| 1961 | |
| 1962 | /* |
| 1963 | * "ch_logfile()" function |
| 1964 | */ |
| 1965 | static void |
| 1966 | f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED) |
| 1967 | { |
| 1968 | char_u *fname; |
| 1969 | char_u *opt = (char_u *)""; |
| 1970 | char_u buf[NUMBUFLEN]; |
| 1971 | |
Bram Moolenaar | 6d87e9e | 2017-08-07 20:51:51 +0200 | [diff] [blame] | 1972 | /* Don't open a file in restricted mode. */ |
| 1973 | if (check_restricted() || check_secure()) |
| 1974 | return; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 1975 | fname = get_tv_string(&argvars[0]); |
| 1976 | if (argvars[1].v_type == VAR_STRING) |
| 1977 | opt = get_tv_string_buf(&argvars[1], buf); |
| 1978 | ch_logfile(fname, opt); |
| 1979 | } |
| 1980 | |
| 1981 | /* |
| 1982 | * "ch_open()" function |
| 1983 | */ |
| 1984 | static void |
| 1985 | f_ch_open(typval_T *argvars, typval_T *rettv) |
| 1986 | { |
| 1987 | rettv->v_type = VAR_CHANNEL; |
| 1988 | if (check_restricted() || check_secure()) |
| 1989 | return; |
| 1990 | rettv->vval.v_channel = channel_open_func(argvars); |
| 1991 | } |
| 1992 | |
| 1993 | /* |
| 1994 | * "ch_read()" function |
| 1995 | */ |
| 1996 | static void |
| 1997 | f_ch_read(typval_T *argvars, typval_T *rettv) |
| 1998 | { |
| 1999 | common_channel_read(argvars, rettv, FALSE); |
| 2000 | } |
| 2001 | |
| 2002 | /* |
| 2003 | * "ch_readraw()" function |
| 2004 | */ |
| 2005 | static void |
| 2006 | f_ch_readraw(typval_T *argvars, typval_T *rettv) |
| 2007 | { |
| 2008 | common_channel_read(argvars, rettv, TRUE); |
| 2009 | } |
| 2010 | |
| 2011 | /* |
| 2012 | * "ch_evalexpr()" function |
| 2013 | */ |
| 2014 | static void |
| 2015 | f_ch_evalexpr(typval_T *argvars, typval_T *rettv) |
| 2016 | { |
| 2017 | ch_expr_common(argvars, rettv, TRUE); |
| 2018 | } |
| 2019 | |
| 2020 | /* |
| 2021 | * "ch_sendexpr()" function |
| 2022 | */ |
| 2023 | static void |
| 2024 | f_ch_sendexpr(typval_T *argvars, typval_T *rettv) |
| 2025 | { |
| 2026 | ch_expr_common(argvars, rettv, FALSE); |
| 2027 | } |
| 2028 | |
| 2029 | /* |
| 2030 | * "ch_evalraw()" function |
| 2031 | */ |
| 2032 | static void |
| 2033 | f_ch_evalraw(typval_T *argvars, typval_T *rettv) |
| 2034 | { |
| 2035 | ch_raw_common(argvars, rettv, TRUE); |
| 2036 | } |
| 2037 | |
| 2038 | /* |
| 2039 | * "ch_sendraw()" function |
| 2040 | */ |
| 2041 | static void |
| 2042 | f_ch_sendraw(typval_T *argvars, typval_T *rettv) |
| 2043 | { |
| 2044 | ch_raw_common(argvars, rettv, FALSE); |
| 2045 | } |
| 2046 | |
| 2047 | /* |
| 2048 | * "ch_setoptions()" function |
| 2049 | */ |
| 2050 | static void |
| 2051 | f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED) |
| 2052 | { |
| 2053 | channel_T *channel; |
| 2054 | jobopt_T opt; |
| 2055 | |
| 2056 | channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0); |
| 2057 | if (channel == NULL) |
| 2058 | return; |
| 2059 | clear_job_options(&opt); |
| 2060 | if (get_job_options(&argvars[1], &opt, |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 2061 | JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2062 | channel_set_options(channel, &opt); |
| 2063 | free_job_options(&opt); |
| 2064 | } |
| 2065 | |
| 2066 | /* |
| 2067 | * "ch_status()" function |
| 2068 | */ |
| 2069 | static void |
| 2070 | f_ch_status(typval_T *argvars, typval_T *rettv) |
| 2071 | { |
| 2072 | channel_T *channel; |
Bram Moolenaar | 7ef3810 | 2016-09-26 22:36:58 +0200 | [diff] [blame] | 2073 | jobopt_T opt; |
| 2074 | int part = -1; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2075 | |
| 2076 | /* return an empty string by default */ |
| 2077 | rettv->v_type = VAR_STRING; |
| 2078 | rettv->vval.v_string = NULL; |
| 2079 | |
| 2080 | channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0); |
Bram Moolenaar | 7ef3810 | 2016-09-26 22:36:58 +0200 | [diff] [blame] | 2081 | |
| 2082 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 2083 | { |
| 2084 | clear_job_options(&opt); |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 2085 | if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK |
Bram Moolenaar | 7ef3810 | 2016-09-26 22:36:58 +0200 | [diff] [blame] | 2086 | && (opt.jo_set & JO_PART)) |
| 2087 | part = opt.jo_part; |
| 2088 | } |
| 2089 | |
| 2090 | rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part)); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2091 | } |
| 2092 | #endif |
| 2093 | |
| 2094 | /* |
| 2095 | * "changenr()" function |
| 2096 | */ |
| 2097 | static void |
| 2098 | f_changenr(typval_T *argvars UNUSED, typval_T *rettv) |
| 2099 | { |
| 2100 | rettv->vval.v_number = curbuf->b_u_seq_cur; |
| 2101 | } |
| 2102 | |
| 2103 | /* |
| 2104 | * "char2nr(string)" function |
| 2105 | */ |
| 2106 | static void |
| 2107 | f_char2nr(typval_T *argvars, typval_T *rettv) |
| 2108 | { |
| 2109 | #ifdef FEAT_MBYTE |
| 2110 | if (has_mbyte) |
| 2111 | { |
| 2112 | int utf8 = 0; |
| 2113 | |
| 2114 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 2115 | utf8 = (int)get_tv_number_chk(&argvars[1], NULL); |
| 2116 | |
| 2117 | if (utf8) |
| 2118 | rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0])); |
| 2119 | else |
| 2120 | rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0])); |
| 2121 | } |
| 2122 | else |
| 2123 | #endif |
| 2124 | rettv->vval.v_number = get_tv_string(&argvars[0])[0]; |
| 2125 | } |
| 2126 | |
| 2127 | /* |
| 2128 | * "cindent(lnum)" function |
| 2129 | */ |
| 2130 | static void |
| 2131 | f_cindent(typval_T *argvars UNUSED, typval_T *rettv) |
| 2132 | { |
| 2133 | #ifdef FEAT_CINDENT |
| 2134 | pos_T pos; |
| 2135 | linenr_T lnum; |
| 2136 | |
| 2137 | pos = curwin->w_cursor; |
| 2138 | lnum = get_tv_lnum(argvars); |
| 2139 | if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) |
| 2140 | { |
| 2141 | curwin->w_cursor.lnum = lnum; |
| 2142 | rettv->vval.v_number = get_c_indent(); |
| 2143 | curwin->w_cursor = pos; |
| 2144 | } |
| 2145 | else |
| 2146 | #endif |
| 2147 | rettv->vval.v_number = -1; |
| 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * "clearmatches()" function |
| 2152 | */ |
| 2153 | static void |
| 2154 | f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 2155 | { |
| 2156 | #ifdef FEAT_SEARCH_EXTRA |
| 2157 | clear_matches(curwin); |
| 2158 | #endif |
| 2159 | } |
| 2160 | |
| 2161 | /* |
| 2162 | * "col(string)" function |
| 2163 | */ |
| 2164 | static void |
| 2165 | f_col(typval_T *argvars, typval_T *rettv) |
| 2166 | { |
| 2167 | colnr_T col = 0; |
| 2168 | pos_T *fp; |
| 2169 | int fnum = curbuf->b_fnum; |
| 2170 | |
| 2171 | fp = var2fpos(&argvars[0], FALSE, &fnum); |
| 2172 | if (fp != NULL && fnum == curbuf->b_fnum) |
| 2173 | { |
| 2174 | if (fp->col == MAXCOL) |
| 2175 | { |
| 2176 | /* '> can be MAXCOL, get the length of the line then */ |
| 2177 | if (fp->lnum <= curbuf->b_ml.ml_line_count) |
| 2178 | col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1; |
| 2179 | else |
| 2180 | col = MAXCOL; |
| 2181 | } |
| 2182 | else |
| 2183 | { |
| 2184 | col = fp->col + 1; |
| 2185 | #ifdef FEAT_VIRTUALEDIT |
| 2186 | /* col(".") when the cursor is on the NUL at the end of the line |
| 2187 | * because of "coladd" can be seen as an extra column. */ |
| 2188 | if (virtual_active() && fp == &curwin->w_cursor) |
| 2189 | { |
| 2190 | char_u *p = ml_get_cursor(); |
| 2191 | |
| 2192 | if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p, |
| 2193 | curwin->w_virtcol - curwin->w_cursor.coladd)) |
| 2194 | { |
| 2195 | # ifdef FEAT_MBYTE |
| 2196 | int l; |
| 2197 | |
| 2198 | if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL) |
| 2199 | col += l; |
| 2200 | # else |
| 2201 | if (*p != NUL && p[1] == NUL) |
| 2202 | ++col; |
| 2203 | # endif |
| 2204 | } |
| 2205 | } |
| 2206 | #endif |
| 2207 | } |
| 2208 | } |
| 2209 | rettv->vval.v_number = col; |
| 2210 | } |
| 2211 | |
| 2212 | #if defined(FEAT_INS_EXPAND) |
| 2213 | /* |
| 2214 | * "complete()" function |
| 2215 | */ |
| 2216 | static void |
| 2217 | f_complete(typval_T *argvars, typval_T *rettv UNUSED) |
| 2218 | { |
| 2219 | int startcol; |
| 2220 | |
| 2221 | if ((State & INSERT) == 0) |
| 2222 | { |
| 2223 | EMSG(_("E785: complete() can only be used in Insert mode")); |
| 2224 | return; |
| 2225 | } |
| 2226 | |
| 2227 | /* Check for undo allowed here, because if something was already inserted |
| 2228 | * the line was already saved for undo and this check isn't done. */ |
| 2229 | if (!undo_allowed()) |
| 2230 | return; |
| 2231 | |
| 2232 | if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL) |
| 2233 | { |
| 2234 | EMSG(_(e_invarg)); |
| 2235 | return; |
| 2236 | } |
| 2237 | |
| 2238 | startcol = (int)get_tv_number_chk(&argvars[0], NULL); |
| 2239 | if (startcol <= 0) |
| 2240 | return; |
| 2241 | |
| 2242 | set_completion(startcol - 1, argvars[1].vval.v_list); |
| 2243 | } |
| 2244 | |
| 2245 | /* |
| 2246 | * "complete_add()" function |
| 2247 | */ |
| 2248 | static void |
| 2249 | f_complete_add(typval_T *argvars, typval_T *rettv) |
| 2250 | { |
| 2251 | rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0); |
| 2252 | } |
| 2253 | |
| 2254 | /* |
| 2255 | * "complete_check()" function |
| 2256 | */ |
| 2257 | static void |
| 2258 | f_complete_check(typval_T *argvars UNUSED, typval_T *rettv) |
| 2259 | { |
| 2260 | int saved = RedrawingDisabled; |
| 2261 | |
| 2262 | RedrawingDisabled = 0; |
Bram Moolenaar | 472e859 | 2016-10-15 17:06:47 +0200 | [diff] [blame] | 2263 | ins_compl_check_keys(0, TRUE); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2264 | rettv->vval.v_number = compl_interrupted; |
| 2265 | RedrawingDisabled = saved; |
| 2266 | } |
| 2267 | #endif |
| 2268 | |
| 2269 | /* |
| 2270 | * "confirm(message, buttons[, default [, type]])" function |
| 2271 | */ |
| 2272 | static void |
| 2273 | f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 2274 | { |
| 2275 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 2276 | char_u *message; |
| 2277 | char_u *buttons = NULL; |
| 2278 | char_u buf[NUMBUFLEN]; |
| 2279 | char_u buf2[NUMBUFLEN]; |
| 2280 | int def = 1; |
| 2281 | int type = VIM_GENERIC; |
| 2282 | char_u *typestr; |
| 2283 | int error = FALSE; |
| 2284 | |
| 2285 | message = get_tv_string_chk(&argvars[0]); |
| 2286 | if (message == NULL) |
| 2287 | error = TRUE; |
| 2288 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 2289 | { |
| 2290 | buttons = get_tv_string_buf_chk(&argvars[1], buf); |
| 2291 | if (buttons == NULL) |
| 2292 | error = TRUE; |
| 2293 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2294 | { |
| 2295 | def = (int)get_tv_number_chk(&argvars[2], &error); |
| 2296 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 2297 | { |
| 2298 | typestr = get_tv_string_buf_chk(&argvars[3], buf2); |
| 2299 | if (typestr == NULL) |
| 2300 | error = TRUE; |
| 2301 | else |
| 2302 | { |
| 2303 | switch (TOUPPER_ASC(*typestr)) |
| 2304 | { |
| 2305 | case 'E': type = VIM_ERROR; break; |
| 2306 | case 'Q': type = VIM_QUESTION; break; |
| 2307 | case 'I': type = VIM_INFO; break; |
| 2308 | case 'W': type = VIM_WARNING; break; |
| 2309 | case 'G': type = VIM_GENERIC; break; |
| 2310 | } |
| 2311 | } |
| 2312 | } |
| 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | if (buttons == NULL || *buttons == NUL) |
| 2317 | buttons = (char_u *)_("&Ok"); |
| 2318 | |
| 2319 | if (!error) |
| 2320 | rettv->vval.v_number = do_dialog(type, NULL, message, buttons, |
| 2321 | def, NULL, FALSE); |
| 2322 | #endif |
| 2323 | } |
| 2324 | |
| 2325 | /* |
| 2326 | * "copy()" function |
| 2327 | */ |
| 2328 | static void |
| 2329 | f_copy(typval_T *argvars, typval_T *rettv) |
| 2330 | { |
| 2331 | item_copy(&argvars[0], rettv, FALSE, 0); |
| 2332 | } |
| 2333 | |
| 2334 | #ifdef FEAT_FLOAT |
| 2335 | /* |
| 2336 | * "cos()" function |
| 2337 | */ |
| 2338 | static void |
| 2339 | f_cos(typval_T *argvars, typval_T *rettv) |
| 2340 | { |
| 2341 | float_T f = 0.0; |
| 2342 | |
| 2343 | rettv->v_type = VAR_FLOAT; |
| 2344 | if (get_float_arg(argvars, &f) == OK) |
| 2345 | rettv->vval.v_float = cos(f); |
| 2346 | else |
| 2347 | rettv->vval.v_float = 0.0; |
| 2348 | } |
| 2349 | |
| 2350 | /* |
| 2351 | * "cosh()" function |
| 2352 | */ |
| 2353 | static void |
| 2354 | f_cosh(typval_T *argvars, typval_T *rettv) |
| 2355 | { |
| 2356 | float_T f = 0.0; |
| 2357 | |
| 2358 | rettv->v_type = VAR_FLOAT; |
| 2359 | if (get_float_arg(argvars, &f) == OK) |
| 2360 | rettv->vval.v_float = cosh(f); |
| 2361 | else |
| 2362 | rettv->vval.v_float = 0.0; |
| 2363 | } |
| 2364 | #endif |
| 2365 | |
| 2366 | /* |
| 2367 | * "count()" function |
| 2368 | */ |
| 2369 | static void |
| 2370 | f_count(typval_T *argvars, typval_T *rettv) |
| 2371 | { |
| 2372 | long n = 0; |
| 2373 | int ic = FALSE; |
Bram Moolenaar | 9966b21 | 2017-07-28 16:46:57 +0200 | [diff] [blame] | 2374 | int error = FALSE; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2375 | |
Bram Moolenaar | 9966b21 | 2017-07-28 16:46:57 +0200 | [diff] [blame] | 2376 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2377 | ic = (int)get_tv_number_chk(&argvars[2], &error); |
| 2378 | |
| 2379 | if (argvars[0].v_type == VAR_STRING) |
| 2380 | { |
| 2381 | char_u *expr = get_tv_string_chk(&argvars[1]); |
| 2382 | char_u *p = argvars[0].vval.v_string; |
| 2383 | char_u *next; |
| 2384 | |
Bram Moolenaar | 338e47f | 2017-12-19 11:55:26 +0100 | [diff] [blame] | 2385 | if (!error && expr != NULL && *expr != NUL && p != NULL) |
Bram Moolenaar | 9966b21 | 2017-07-28 16:46:57 +0200 | [diff] [blame] | 2386 | { |
| 2387 | if (ic) |
| 2388 | { |
| 2389 | size_t len = STRLEN(expr); |
| 2390 | |
| 2391 | while (*p != NUL) |
| 2392 | { |
| 2393 | if (MB_STRNICMP(p, expr, len) == 0) |
| 2394 | { |
| 2395 | ++n; |
| 2396 | p += len; |
| 2397 | } |
| 2398 | else |
| 2399 | MB_PTR_ADV(p); |
| 2400 | } |
| 2401 | } |
| 2402 | else |
| 2403 | while ((next = (char_u *)strstr((char *)p, (char *)expr)) |
| 2404 | != NULL) |
| 2405 | { |
| 2406 | ++n; |
| 2407 | p = next + STRLEN(expr); |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | } |
| 2412 | else if (argvars[0].v_type == VAR_LIST) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2413 | { |
| 2414 | listitem_T *li; |
| 2415 | list_T *l; |
| 2416 | long idx; |
| 2417 | |
| 2418 | if ((l = argvars[0].vval.v_list) != NULL) |
| 2419 | { |
| 2420 | li = l->lv_first; |
| 2421 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2422 | { |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2423 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 2424 | { |
| 2425 | idx = (long)get_tv_number_chk(&argvars[3], &error); |
| 2426 | if (!error) |
| 2427 | { |
| 2428 | li = list_find(l, idx); |
| 2429 | if (li == NULL) |
| 2430 | EMSGN(_(e_listidx), idx); |
| 2431 | } |
| 2432 | } |
| 2433 | if (error) |
| 2434 | li = NULL; |
| 2435 | } |
| 2436 | |
| 2437 | for ( ; li != NULL; li = li->li_next) |
| 2438 | if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE)) |
| 2439 | ++n; |
| 2440 | } |
| 2441 | } |
| 2442 | else if (argvars[0].v_type == VAR_DICT) |
| 2443 | { |
| 2444 | int todo; |
| 2445 | dict_T *d; |
| 2446 | hashitem_T *hi; |
| 2447 | |
| 2448 | if ((d = argvars[0].vval.v_dict) != NULL) |
| 2449 | { |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2450 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2451 | { |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2452 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 2453 | EMSG(_(e_invarg)); |
| 2454 | } |
| 2455 | |
| 2456 | todo = error ? 0 : (int)d->dv_hashtab.ht_used; |
| 2457 | for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) |
| 2458 | { |
| 2459 | if (!HASHITEM_EMPTY(hi)) |
| 2460 | { |
| 2461 | --todo; |
| 2462 | if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE)) |
| 2463 | ++n; |
| 2464 | } |
| 2465 | } |
| 2466 | } |
| 2467 | } |
| 2468 | else |
| 2469 | EMSG2(_(e_listdictarg), "count()"); |
| 2470 | rettv->vval.v_number = n; |
| 2471 | } |
| 2472 | |
| 2473 | /* |
| 2474 | * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function |
| 2475 | * |
| 2476 | * Checks the existence of a cscope connection. |
| 2477 | */ |
| 2478 | static void |
| 2479 | f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 2480 | { |
| 2481 | #ifdef FEAT_CSCOPE |
| 2482 | int num = 0; |
| 2483 | char_u *dbpath = NULL; |
| 2484 | char_u *prepend = NULL; |
| 2485 | char_u buf[NUMBUFLEN]; |
| 2486 | |
| 2487 | if (argvars[0].v_type != VAR_UNKNOWN |
| 2488 | && argvars[1].v_type != VAR_UNKNOWN) |
| 2489 | { |
| 2490 | num = (int)get_tv_number(&argvars[0]); |
| 2491 | dbpath = get_tv_string(&argvars[1]); |
| 2492 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2493 | prepend = get_tv_string_buf(&argvars[2], buf); |
| 2494 | } |
| 2495 | |
| 2496 | rettv->vval.v_number = cs_connection(num, dbpath, prepend); |
| 2497 | #endif |
| 2498 | } |
| 2499 | |
| 2500 | /* |
| 2501 | * "cursor(lnum, col)" function, or |
| 2502 | * "cursor(list)" |
| 2503 | * |
| 2504 | * Moves the cursor to the specified line and column. |
| 2505 | * Returns 0 when the position could be set, -1 otherwise. |
| 2506 | */ |
| 2507 | static void |
| 2508 | f_cursor(typval_T *argvars, typval_T *rettv) |
| 2509 | { |
| 2510 | long line, col; |
| 2511 | #ifdef FEAT_VIRTUALEDIT |
| 2512 | long coladd = 0; |
| 2513 | #endif |
| 2514 | int set_curswant = TRUE; |
| 2515 | |
| 2516 | rettv->vval.v_number = -1; |
| 2517 | if (argvars[1].v_type == VAR_UNKNOWN) |
| 2518 | { |
| 2519 | pos_T pos; |
| 2520 | colnr_T curswant = -1; |
| 2521 | |
| 2522 | if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL) |
| 2523 | { |
| 2524 | EMSG(_(e_invarg)); |
| 2525 | return; |
| 2526 | } |
| 2527 | line = pos.lnum; |
| 2528 | col = pos.col; |
| 2529 | #ifdef FEAT_VIRTUALEDIT |
| 2530 | coladd = pos.coladd; |
| 2531 | #endif |
| 2532 | if (curswant >= 0) |
| 2533 | { |
| 2534 | curwin->w_curswant = curswant - 1; |
| 2535 | set_curswant = FALSE; |
| 2536 | } |
| 2537 | } |
| 2538 | else |
| 2539 | { |
| 2540 | line = get_tv_lnum(argvars); |
| 2541 | col = (long)get_tv_number_chk(&argvars[1], NULL); |
| 2542 | #ifdef FEAT_VIRTUALEDIT |
| 2543 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 2544 | coladd = (long)get_tv_number_chk(&argvars[2], NULL); |
| 2545 | #endif |
| 2546 | } |
| 2547 | if (line < 0 || col < 0 |
| 2548 | #ifdef FEAT_VIRTUALEDIT |
| 2549 | || coladd < 0 |
| 2550 | #endif |
| 2551 | ) |
| 2552 | return; /* type error; errmsg already given */ |
| 2553 | if (line > 0) |
| 2554 | curwin->w_cursor.lnum = line; |
| 2555 | if (col > 0) |
| 2556 | curwin->w_cursor.col = col - 1; |
| 2557 | #ifdef FEAT_VIRTUALEDIT |
| 2558 | curwin->w_cursor.coladd = coladd; |
| 2559 | #endif |
| 2560 | |
| 2561 | /* Make sure the cursor is in a valid position. */ |
| 2562 | check_cursor(); |
| 2563 | #ifdef FEAT_MBYTE |
| 2564 | /* Correct cursor for multi-byte character. */ |
| 2565 | if (has_mbyte) |
| 2566 | mb_adjust_cursor(); |
| 2567 | #endif |
| 2568 | |
| 2569 | curwin->w_set_curswant = set_curswant; |
| 2570 | rettv->vval.v_number = 0; |
| 2571 | } |
| 2572 | |
| 2573 | /* |
| 2574 | * "deepcopy()" function |
| 2575 | */ |
| 2576 | static void |
| 2577 | f_deepcopy(typval_T *argvars, typval_T *rettv) |
| 2578 | { |
| 2579 | int noref = 0; |
| 2580 | int copyID; |
| 2581 | |
| 2582 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 2583 | noref = (int)get_tv_number_chk(&argvars[1], NULL); |
| 2584 | if (noref < 0 || noref > 1) |
| 2585 | EMSG(_(e_invarg)); |
| 2586 | else |
| 2587 | { |
| 2588 | copyID = get_copyID(); |
| 2589 | item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0); |
| 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | /* |
| 2594 | * "delete()" function |
| 2595 | */ |
| 2596 | static void |
| 2597 | f_delete(typval_T *argvars, typval_T *rettv) |
| 2598 | { |
| 2599 | char_u nbuf[NUMBUFLEN]; |
| 2600 | char_u *name; |
| 2601 | char_u *flags; |
| 2602 | |
| 2603 | rettv->vval.v_number = -1; |
| 2604 | if (check_restricted() || check_secure()) |
| 2605 | return; |
| 2606 | |
| 2607 | name = get_tv_string(&argvars[0]); |
| 2608 | if (name == NULL || *name == NUL) |
| 2609 | { |
| 2610 | EMSG(_(e_invarg)); |
| 2611 | return; |
| 2612 | } |
| 2613 | |
| 2614 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 2615 | flags = get_tv_string_buf(&argvars[1], nbuf); |
| 2616 | else |
| 2617 | flags = (char_u *)""; |
| 2618 | |
| 2619 | if (*flags == NUL) |
| 2620 | /* delete a file */ |
| 2621 | rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1; |
| 2622 | else if (STRCMP(flags, "d") == 0) |
| 2623 | /* delete an empty directory */ |
| 2624 | rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1; |
| 2625 | else if (STRCMP(flags, "rf") == 0) |
| 2626 | /* delete a directory recursively */ |
| 2627 | rettv->vval.v_number = delete_recursive(name); |
| 2628 | else |
| 2629 | EMSG2(_(e_invexpr2), flags); |
| 2630 | } |
| 2631 | |
| 2632 | /* |
| 2633 | * "did_filetype()" function |
| 2634 | */ |
| 2635 | static void |
| 2636 | f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 2637 | { |
| 2638 | #ifdef FEAT_AUTOCMD |
| 2639 | rettv->vval.v_number = did_filetype; |
| 2640 | #endif |
| 2641 | } |
| 2642 | |
| 2643 | /* |
| 2644 | * "diff_filler()" function |
| 2645 | */ |
| 2646 | static void |
| 2647 | f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 2648 | { |
| 2649 | #ifdef FEAT_DIFF |
| 2650 | rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars)); |
| 2651 | #endif |
| 2652 | } |
| 2653 | |
| 2654 | /* |
| 2655 | * "diff_hlID()" function |
| 2656 | */ |
| 2657 | static void |
| 2658 | f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 2659 | { |
| 2660 | #ifdef FEAT_DIFF |
| 2661 | linenr_T lnum = get_tv_lnum(argvars); |
| 2662 | static linenr_T prev_lnum = 0; |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 2663 | static varnumber_T changedtick = 0; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2664 | static int fnum = 0; |
| 2665 | static int change_start = 0; |
| 2666 | static int change_end = 0; |
| 2667 | static hlf_T hlID = (hlf_T)0; |
| 2668 | int filler_lines; |
| 2669 | int col; |
| 2670 | |
| 2671 | if (lnum < 0) /* ignore type error in {lnum} arg */ |
| 2672 | lnum = 0; |
| 2673 | if (lnum != prev_lnum |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 2674 | || changedtick != CHANGEDTICK(curbuf) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2675 | || fnum != curbuf->b_fnum) |
| 2676 | { |
| 2677 | /* New line, buffer, change: need to get the values. */ |
| 2678 | filler_lines = diff_check(curwin, lnum); |
| 2679 | if (filler_lines < 0) |
| 2680 | { |
| 2681 | if (filler_lines == -1) |
| 2682 | { |
| 2683 | change_start = MAXCOL; |
| 2684 | change_end = -1; |
| 2685 | if (diff_find_change(curwin, lnum, &change_start, &change_end)) |
| 2686 | hlID = HLF_ADD; /* added line */ |
| 2687 | else |
| 2688 | hlID = HLF_CHD; /* changed line */ |
| 2689 | } |
| 2690 | else |
| 2691 | hlID = HLF_ADD; /* added line */ |
| 2692 | } |
| 2693 | else |
| 2694 | hlID = (hlf_T)0; |
| 2695 | prev_lnum = lnum; |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 2696 | changedtick = CHANGEDTICK(curbuf); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2697 | fnum = curbuf->b_fnum; |
| 2698 | } |
| 2699 | |
| 2700 | if (hlID == HLF_CHD || hlID == HLF_TXD) |
| 2701 | { |
| 2702 | col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */ |
| 2703 | if (col >= change_start && col <= change_end) |
| 2704 | hlID = HLF_TXD; /* changed text */ |
| 2705 | else |
| 2706 | hlID = HLF_CHD; /* changed line */ |
| 2707 | } |
| 2708 | rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID; |
| 2709 | #endif |
| 2710 | } |
| 2711 | |
| 2712 | /* |
| 2713 | * "empty({expr})" function |
| 2714 | */ |
| 2715 | static void |
| 2716 | f_empty(typval_T *argvars, typval_T *rettv) |
| 2717 | { |
| 2718 | int n = FALSE; |
| 2719 | |
| 2720 | switch (argvars[0].v_type) |
| 2721 | { |
| 2722 | case VAR_STRING: |
| 2723 | case VAR_FUNC: |
| 2724 | n = argvars[0].vval.v_string == NULL |
| 2725 | || *argvars[0].vval.v_string == NUL; |
| 2726 | break; |
| 2727 | case VAR_PARTIAL: |
| 2728 | n = FALSE; |
| 2729 | break; |
| 2730 | case VAR_NUMBER: |
| 2731 | n = argvars[0].vval.v_number == 0; |
| 2732 | break; |
| 2733 | case VAR_FLOAT: |
| 2734 | #ifdef FEAT_FLOAT |
| 2735 | n = argvars[0].vval.v_float == 0.0; |
| 2736 | break; |
| 2737 | #endif |
| 2738 | case VAR_LIST: |
| 2739 | n = argvars[0].vval.v_list == NULL |
| 2740 | || argvars[0].vval.v_list->lv_first == NULL; |
| 2741 | break; |
| 2742 | case VAR_DICT: |
| 2743 | n = argvars[0].vval.v_dict == NULL |
| 2744 | || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0; |
| 2745 | break; |
| 2746 | case VAR_SPECIAL: |
| 2747 | n = argvars[0].vval.v_number != VVAL_TRUE; |
| 2748 | break; |
| 2749 | |
| 2750 | case VAR_JOB: |
| 2751 | #ifdef FEAT_JOB_CHANNEL |
| 2752 | n = argvars[0].vval.v_job == NULL |
| 2753 | || argvars[0].vval.v_job->jv_status != JOB_STARTED; |
| 2754 | break; |
| 2755 | #endif |
| 2756 | case VAR_CHANNEL: |
| 2757 | #ifdef FEAT_JOB_CHANNEL |
| 2758 | n = argvars[0].vval.v_channel == NULL |
| 2759 | || !channel_is_open(argvars[0].vval.v_channel); |
| 2760 | break; |
| 2761 | #endif |
| 2762 | case VAR_UNKNOWN: |
Bram Moolenaar | 95f0960 | 2016-11-10 20:01:45 +0100 | [diff] [blame] | 2763 | internal_error("f_empty(UNKNOWN)"); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2764 | n = TRUE; |
| 2765 | break; |
| 2766 | } |
| 2767 | |
| 2768 | rettv->vval.v_number = n; |
| 2769 | } |
| 2770 | |
| 2771 | /* |
| 2772 | * "escape({string}, {chars})" function |
| 2773 | */ |
| 2774 | static void |
| 2775 | f_escape(typval_T *argvars, typval_T *rettv) |
| 2776 | { |
| 2777 | char_u buf[NUMBUFLEN]; |
| 2778 | |
| 2779 | rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]), |
| 2780 | get_tv_string_buf(&argvars[1], buf)); |
| 2781 | rettv->v_type = VAR_STRING; |
| 2782 | } |
| 2783 | |
| 2784 | /* |
| 2785 | * "eval()" function |
| 2786 | */ |
| 2787 | static void |
| 2788 | f_eval(typval_T *argvars, typval_T *rettv) |
| 2789 | { |
| 2790 | char_u *s, *p; |
| 2791 | |
| 2792 | s = get_tv_string_chk(&argvars[0]); |
| 2793 | if (s != NULL) |
| 2794 | s = skipwhite(s); |
| 2795 | |
| 2796 | p = s; |
| 2797 | if (s == NULL || eval1(&s, rettv, TRUE) == FAIL) |
| 2798 | { |
| 2799 | if (p != NULL && !aborting()) |
| 2800 | EMSG2(_(e_invexpr2), p); |
| 2801 | need_clr_eos = FALSE; |
| 2802 | rettv->v_type = VAR_NUMBER; |
| 2803 | rettv->vval.v_number = 0; |
| 2804 | } |
| 2805 | else if (*s != NUL) |
| 2806 | EMSG(_(e_trailing)); |
| 2807 | } |
| 2808 | |
| 2809 | /* |
| 2810 | * "eventhandler()" function |
| 2811 | */ |
| 2812 | static void |
| 2813 | f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv) |
| 2814 | { |
| 2815 | rettv->vval.v_number = vgetc_busy; |
| 2816 | } |
| 2817 | |
| 2818 | /* |
| 2819 | * "executable()" function |
| 2820 | */ |
| 2821 | static void |
| 2822 | f_executable(typval_T *argvars, typval_T *rettv) |
| 2823 | { |
| 2824 | char_u *name = get_tv_string(&argvars[0]); |
| 2825 | |
| 2826 | /* Check in $PATH and also check directly if there is a directory name. */ |
| 2827 | rettv->vval.v_number = mch_can_exe(name, NULL, TRUE) |
| 2828 | || (gettail(name) != name && mch_can_exe(name, NULL, FALSE)); |
| 2829 | } |
| 2830 | |
| 2831 | static garray_T redir_execute_ga; |
| 2832 | |
| 2833 | /* |
| 2834 | * Append "value[value_len]" to the execute() output. |
| 2835 | */ |
| 2836 | void |
| 2837 | execute_redir_str(char_u *value, int value_len) |
| 2838 | { |
| 2839 | int len; |
| 2840 | |
| 2841 | if (value_len == -1) |
| 2842 | len = (int)STRLEN(value); /* Append the entire string */ |
| 2843 | else |
| 2844 | len = value_len; /* Append only "value_len" characters */ |
| 2845 | if (ga_grow(&redir_execute_ga, len) == OK) |
| 2846 | { |
| 2847 | mch_memmove((char *)redir_execute_ga.ga_data |
| 2848 | + redir_execute_ga.ga_len, value, len); |
| 2849 | redir_execute_ga.ga_len += len; |
| 2850 | } |
| 2851 | } |
| 2852 | |
| 2853 | /* |
| 2854 | * Get next line from a list. |
| 2855 | * Called by do_cmdline() to get the next line. |
| 2856 | * Returns allocated string, or NULL for end of function. |
| 2857 | */ |
| 2858 | |
| 2859 | static char_u * |
| 2860 | get_list_line( |
| 2861 | int c UNUSED, |
| 2862 | void *cookie, |
| 2863 | int indent UNUSED) |
| 2864 | { |
| 2865 | listitem_T **p = (listitem_T **)cookie; |
| 2866 | listitem_T *item = *p; |
| 2867 | char_u buf[NUMBUFLEN]; |
| 2868 | char_u *s; |
| 2869 | |
| 2870 | if (item == NULL) |
| 2871 | return NULL; |
| 2872 | s = get_tv_string_buf_chk(&item->li_tv, buf); |
| 2873 | *p = item->li_next; |
| 2874 | return s == NULL ? NULL : vim_strsave(s); |
| 2875 | } |
| 2876 | |
| 2877 | /* |
| 2878 | * "execute()" function |
| 2879 | */ |
| 2880 | static void |
| 2881 | f_execute(typval_T *argvars, typval_T *rettv) |
| 2882 | { |
| 2883 | char_u *cmd = NULL; |
| 2884 | list_T *list = NULL; |
| 2885 | int save_msg_silent = msg_silent; |
| 2886 | int save_emsg_silent = emsg_silent; |
| 2887 | int save_emsg_noredir = emsg_noredir; |
| 2888 | int save_redir_execute = redir_execute; |
Bram Moolenaar | 2095148 | 2017-12-25 13:44:43 +0100 | [diff] [blame] | 2889 | int save_redir_off = redir_off; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2890 | garray_T save_ga; |
| 2891 | |
| 2892 | rettv->vval.v_string = NULL; |
| 2893 | rettv->v_type = VAR_STRING; |
| 2894 | |
| 2895 | if (argvars[0].v_type == VAR_LIST) |
| 2896 | { |
| 2897 | list = argvars[0].vval.v_list; |
| 2898 | if (list == NULL || list->lv_first == NULL) |
| 2899 | /* empty list, no commands, empty output */ |
| 2900 | return; |
| 2901 | ++list->lv_refcount; |
| 2902 | } |
| 2903 | else |
| 2904 | { |
| 2905 | cmd = get_tv_string_chk(&argvars[0]); |
| 2906 | if (cmd == NULL) |
| 2907 | return; |
| 2908 | } |
| 2909 | |
| 2910 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 2911 | { |
| 2912 | char_u buf[NUMBUFLEN]; |
| 2913 | char_u *s = get_tv_string_buf_chk(&argvars[1], buf); |
| 2914 | |
| 2915 | if (s == NULL) |
| 2916 | return; |
| 2917 | if (STRNCMP(s, "silent", 6) == 0) |
| 2918 | ++msg_silent; |
| 2919 | if (STRCMP(s, "silent!") == 0) |
| 2920 | { |
| 2921 | emsg_silent = TRUE; |
| 2922 | emsg_noredir = TRUE; |
| 2923 | } |
| 2924 | } |
| 2925 | else |
| 2926 | ++msg_silent; |
| 2927 | |
| 2928 | if (redir_execute) |
| 2929 | save_ga = redir_execute_ga; |
| 2930 | ga_init2(&redir_execute_ga, (int)sizeof(char), 500); |
| 2931 | redir_execute = TRUE; |
Bram Moolenaar | 2095148 | 2017-12-25 13:44:43 +0100 | [diff] [blame] | 2932 | redir_off = FALSE; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2933 | |
| 2934 | if (cmd != NULL) |
| 2935 | do_cmdline_cmd(cmd); |
| 2936 | else |
| 2937 | { |
| 2938 | listitem_T *item = list->lv_first; |
| 2939 | |
| 2940 | do_cmdline(NULL, get_list_line, (void *)&item, |
| 2941 | DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED); |
| 2942 | --list->lv_refcount; |
| 2943 | } |
| 2944 | |
Bram Moolenaar | d297f35 | 2017-01-29 20:31:21 +0100 | [diff] [blame] | 2945 | /* Need to append a NUL to the result. */ |
| 2946 | if (ga_grow(&redir_execute_ga, 1) == OK) |
| 2947 | { |
| 2948 | ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL; |
| 2949 | rettv->vval.v_string = redir_execute_ga.ga_data; |
| 2950 | } |
| 2951 | else |
| 2952 | { |
| 2953 | ga_clear(&redir_execute_ga); |
| 2954 | rettv->vval.v_string = NULL; |
| 2955 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2956 | msg_silent = save_msg_silent; |
| 2957 | emsg_silent = save_emsg_silent; |
| 2958 | emsg_noredir = save_emsg_noredir; |
| 2959 | |
| 2960 | redir_execute = save_redir_execute; |
| 2961 | if (redir_execute) |
| 2962 | redir_execute_ga = save_ga; |
Bram Moolenaar | 2095148 | 2017-12-25 13:44:43 +0100 | [diff] [blame] | 2963 | redir_off = save_redir_off; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 2964 | |
| 2965 | /* "silent reg" or "silent echo x" leaves msg_col somewhere in the |
| 2966 | * line. Put it back in the first column. */ |
| 2967 | msg_col = 0; |
| 2968 | } |
| 2969 | |
| 2970 | /* |
| 2971 | * "exepath()" function |
| 2972 | */ |
| 2973 | static void |
| 2974 | f_exepath(typval_T *argvars, typval_T *rettv) |
| 2975 | { |
| 2976 | char_u *p = NULL; |
| 2977 | |
| 2978 | (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE); |
| 2979 | rettv->v_type = VAR_STRING; |
| 2980 | rettv->vval.v_string = p; |
| 2981 | } |
| 2982 | |
| 2983 | /* |
| 2984 | * "exists()" function |
| 2985 | */ |
| 2986 | static void |
| 2987 | f_exists(typval_T *argvars, typval_T *rettv) |
| 2988 | { |
| 2989 | char_u *p; |
| 2990 | char_u *name; |
| 2991 | int n = FALSE; |
| 2992 | int len = 0; |
| 2993 | |
| 2994 | p = get_tv_string(&argvars[0]); |
| 2995 | if (*p == '$') /* environment variable */ |
| 2996 | { |
| 2997 | /* first try "normal" environment variables (fast) */ |
| 2998 | if (mch_getenv(p + 1) != NULL) |
| 2999 | n = TRUE; |
| 3000 | else |
| 3001 | { |
| 3002 | /* try expanding things like $VIM and ${HOME} */ |
| 3003 | p = expand_env_save(p); |
| 3004 | if (p != NULL && *p != '$') |
| 3005 | n = TRUE; |
| 3006 | vim_free(p); |
| 3007 | } |
| 3008 | } |
| 3009 | else if (*p == '&' || *p == '+') /* option */ |
| 3010 | { |
| 3011 | n = (get_option_tv(&p, NULL, TRUE) == OK); |
| 3012 | if (*skipwhite(p) != NUL) |
| 3013 | n = FALSE; /* trailing garbage */ |
| 3014 | } |
| 3015 | else if (*p == '*') /* internal or user defined function */ |
| 3016 | { |
Bram Moolenaar | b54c3ff | 2016-07-31 14:11:58 +0200 | [diff] [blame] | 3017 | n = function_exists(p + 1, FALSE); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3018 | } |
| 3019 | else if (*p == ':') |
| 3020 | { |
| 3021 | n = cmd_exists(p + 1); |
| 3022 | } |
| 3023 | else if (*p == '#') |
| 3024 | { |
| 3025 | #ifdef FEAT_AUTOCMD |
| 3026 | if (p[1] == '#') |
| 3027 | n = autocmd_supported(p + 2); |
| 3028 | else |
| 3029 | n = au_exists(p + 1); |
| 3030 | #endif |
| 3031 | } |
| 3032 | else /* internal variable */ |
| 3033 | { |
| 3034 | char_u *tofree; |
| 3035 | typval_T tv; |
| 3036 | |
| 3037 | /* get_name_len() takes care of expanding curly braces */ |
| 3038 | name = p; |
| 3039 | len = get_name_len(&p, &tofree, TRUE, FALSE); |
| 3040 | if (len > 0) |
| 3041 | { |
| 3042 | if (tofree != NULL) |
| 3043 | name = tofree; |
| 3044 | n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK); |
| 3045 | if (n) |
| 3046 | { |
| 3047 | /* handle d.key, l[idx], f(expr) */ |
| 3048 | n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK); |
| 3049 | if (n) |
| 3050 | clear_tv(&tv); |
| 3051 | } |
| 3052 | } |
| 3053 | if (*p != NUL) |
| 3054 | n = FALSE; |
| 3055 | |
| 3056 | vim_free(tofree); |
| 3057 | } |
| 3058 | |
| 3059 | rettv->vval.v_number = n; |
| 3060 | } |
| 3061 | |
| 3062 | #ifdef FEAT_FLOAT |
| 3063 | /* |
| 3064 | * "exp()" function |
| 3065 | */ |
| 3066 | static void |
| 3067 | f_exp(typval_T *argvars, typval_T *rettv) |
| 3068 | { |
| 3069 | float_T f = 0.0; |
| 3070 | |
| 3071 | rettv->v_type = VAR_FLOAT; |
| 3072 | if (get_float_arg(argvars, &f) == OK) |
| 3073 | rettv->vval.v_float = exp(f); |
| 3074 | else |
| 3075 | rettv->vval.v_float = 0.0; |
| 3076 | } |
| 3077 | #endif |
| 3078 | |
| 3079 | /* |
| 3080 | * "expand()" function |
| 3081 | */ |
| 3082 | static void |
| 3083 | f_expand(typval_T *argvars, typval_T *rettv) |
| 3084 | { |
| 3085 | char_u *s; |
| 3086 | int len; |
| 3087 | char_u *errormsg; |
| 3088 | int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND; |
| 3089 | expand_T xpc; |
| 3090 | int error = FALSE; |
| 3091 | char_u *result; |
| 3092 | |
| 3093 | rettv->v_type = VAR_STRING; |
| 3094 | if (argvars[1].v_type != VAR_UNKNOWN |
| 3095 | && argvars[2].v_type != VAR_UNKNOWN |
| 3096 | && get_tv_number_chk(&argvars[2], &error) |
| 3097 | && !error) |
| 3098 | { |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 3099 | rettv_list_set(rettv, NULL); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | s = get_tv_string(&argvars[0]); |
| 3103 | if (*s == '%' || *s == '#' || *s == '<') |
| 3104 | { |
| 3105 | ++emsg_off; |
| 3106 | result = eval_vars(s, s, &len, NULL, &errormsg, NULL); |
| 3107 | --emsg_off; |
| 3108 | if (rettv->v_type == VAR_LIST) |
| 3109 | { |
| 3110 | if (rettv_list_alloc(rettv) != FAIL && result != NULL) |
| 3111 | list_append_string(rettv->vval.v_list, result, -1); |
| 3112 | else |
| 3113 | vim_free(result); |
| 3114 | } |
| 3115 | else |
| 3116 | rettv->vval.v_string = result; |
| 3117 | } |
| 3118 | else |
| 3119 | { |
| 3120 | /* When the optional second argument is non-zero, don't remove matches |
| 3121 | * for 'wildignore' and don't put matches for 'suffixes' at the end. */ |
| 3122 | if (argvars[1].v_type != VAR_UNKNOWN |
| 3123 | && get_tv_number_chk(&argvars[1], &error)) |
| 3124 | options |= WILD_KEEP_ALL; |
| 3125 | if (!error) |
| 3126 | { |
| 3127 | ExpandInit(&xpc); |
| 3128 | xpc.xp_context = EXPAND_FILES; |
| 3129 | if (p_wic) |
| 3130 | options += WILD_ICASE; |
| 3131 | if (rettv->v_type == VAR_STRING) |
| 3132 | rettv->vval.v_string = ExpandOne(&xpc, s, NULL, |
| 3133 | options, WILD_ALL); |
| 3134 | else if (rettv_list_alloc(rettv) != FAIL) |
| 3135 | { |
| 3136 | int i; |
| 3137 | |
| 3138 | ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP); |
| 3139 | for (i = 0; i < xpc.xp_numfiles; i++) |
| 3140 | list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1); |
| 3141 | ExpandCleanup(&xpc); |
| 3142 | } |
| 3143 | } |
| 3144 | else |
| 3145 | rettv->vval.v_string = NULL; |
| 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | /* |
| 3150 | * "extend(list, list [, idx])" function |
| 3151 | * "extend(dict, dict [, action])" function |
| 3152 | */ |
| 3153 | static void |
| 3154 | f_extend(typval_T *argvars, typval_T *rettv) |
| 3155 | { |
| 3156 | char_u *arg_errmsg = (char_u *)N_("extend() argument"); |
| 3157 | |
| 3158 | if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST) |
| 3159 | { |
| 3160 | list_T *l1, *l2; |
| 3161 | listitem_T *item; |
| 3162 | long before; |
| 3163 | int error = FALSE; |
| 3164 | |
| 3165 | l1 = argvars[0].vval.v_list; |
| 3166 | l2 = argvars[1].vval.v_list; |
| 3167 | if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE) |
| 3168 | && l2 != NULL) |
| 3169 | { |
| 3170 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 3171 | { |
| 3172 | before = (long)get_tv_number_chk(&argvars[2], &error); |
| 3173 | if (error) |
| 3174 | return; /* type error; errmsg already given */ |
| 3175 | |
| 3176 | if (before == l1->lv_len) |
| 3177 | item = NULL; |
| 3178 | else |
| 3179 | { |
| 3180 | item = list_find(l1, before); |
| 3181 | if (item == NULL) |
| 3182 | { |
| 3183 | EMSGN(_(e_listidx), before); |
| 3184 | return; |
| 3185 | } |
| 3186 | } |
| 3187 | } |
| 3188 | else |
| 3189 | item = NULL; |
| 3190 | list_extend(l1, l2, item); |
| 3191 | |
| 3192 | copy_tv(&argvars[0], rettv); |
| 3193 | } |
| 3194 | } |
| 3195 | else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT) |
| 3196 | { |
| 3197 | dict_T *d1, *d2; |
| 3198 | char_u *action; |
| 3199 | int i; |
| 3200 | |
| 3201 | d1 = argvars[0].vval.v_dict; |
| 3202 | d2 = argvars[1].vval.v_dict; |
| 3203 | if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE) |
| 3204 | && d2 != NULL) |
| 3205 | { |
| 3206 | /* Check the third argument. */ |
| 3207 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 3208 | { |
| 3209 | static char *(av[]) = {"keep", "force", "error"}; |
| 3210 | |
| 3211 | action = get_tv_string_chk(&argvars[2]); |
| 3212 | if (action == NULL) |
| 3213 | return; /* type error; errmsg already given */ |
| 3214 | for (i = 0; i < 3; ++i) |
| 3215 | if (STRCMP(action, av[i]) == 0) |
| 3216 | break; |
| 3217 | if (i == 3) |
| 3218 | { |
| 3219 | EMSG2(_(e_invarg2), action); |
| 3220 | return; |
| 3221 | } |
| 3222 | } |
| 3223 | else |
| 3224 | action = (char_u *)"force"; |
| 3225 | |
| 3226 | dict_extend(d1, d2, action); |
| 3227 | |
| 3228 | copy_tv(&argvars[0], rettv); |
| 3229 | } |
| 3230 | } |
| 3231 | else |
| 3232 | EMSG2(_(e_listdictarg), "extend()"); |
| 3233 | } |
| 3234 | |
| 3235 | /* |
| 3236 | * "feedkeys()" function |
| 3237 | */ |
| 3238 | static void |
| 3239 | f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED) |
| 3240 | { |
| 3241 | int remap = TRUE; |
| 3242 | int insert = FALSE; |
| 3243 | char_u *keys, *flags; |
| 3244 | char_u nbuf[NUMBUFLEN]; |
| 3245 | int typed = FALSE; |
| 3246 | int execute = FALSE; |
| 3247 | int dangerous = FALSE; |
| 3248 | char_u *keys_esc; |
| 3249 | |
| 3250 | /* This is not allowed in the sandbox. If the commands would still be |
| 3251 | * executed in the sandbox it would be OK, but it probably happens later, |
| 3252 | * when "sandbox" is no longer set. */ |
| 3253 | if (check_secure()) |
| 3254 | return; |
| 3255 | |
| 3256 | keys = get_tv_string(&argvars[0]); |
| 3257 | |
| 3258 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 3259 | { |
| 3260 | flags = get_tv_string_buf(&argvars[1], nbuf); |
| 3261 | for ( ; *flags != NUL; ++flags) |
| 3262 | { |
| 3263 | switch (*flags) |
| 3264 | { |
| 3265 | case 'n': remap = FALSE; break; |
| 3266 | case 'm': remap = TRUE; break; |
| 3267 | case 't': typed = TRUE; break; |
| 3268 | case 'i': insert = TRUE; break; |
| 3269 | case 'x': execute = TRUE; break; |
| 3270 | case '!': dangerous = TRUE; break; |
| 3271 | } |
| 3272 | } |
| 3273 | } |
| 3274 | |
| 3275 | if (*keys != NUL || execute) |
| 3276 | { |
| 3277 | /* Need to escape K_SPECIAL and CSI before putting the string in the |
| 3278 | * typeahead buffer. */ |
| 3279 | keys_esc = vim_strsave_escape_csi(keys); |
| 3280 | if (keys_esc != NULL) |
| 3281 | { |
| 3282 | ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE), |
| 3283 | insert ? 0 : typebuf.tb_len, !typed, FALSE); |
| 3284 | vim_free(keys_esc); |
Bram Moolenaar | 5d7be4f | 2017-06-25 13:40:17 +0200 | [diff] [blame] | 3285 | if (vgetc_busy |
| 3286 | #ifdef FEAT_TIMERS |
| 3287 | || timer_busy |
| 3288 | #endif |
| 3289 | ) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3290 | typebuf_was_filled = TRUE; |
| 3291 | if (execute) |
| 3292 | { |
| 3293 | int save_msg_scroll = msg_scroll; |
| 3294 | |
| 3295 | /* Avoid a 1 second delay when the keys start Insert mode. */ |
| 3296 | msg_scroll = FALSE; |
| 3297 | |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 3298 | #ifdef FEAT_TERMINAL |
| 3299 | if (term_use_loop()) |
| 3300 | terminal_loop(FALSE); |
| 3301 | else |
| 3302 | #endif |
| 3303 | { |
| 3304 | if (!dangerous) |
| 3305 | ++ex_normal_busy; |
| 3306 | exec_normal(TRUE); |
| 3307 | if (!dangerous) |
| 3308 | --ex_normal_busy; |
| 3309 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3310 | msg_scroll |= save_msg_scroll; |
| 3311 | } |
| 3312 | } |
| 3313 | } |
| 3314 | } |
| 3315 | |
| 3316 | /* |
| 3317 | * "filereadable()" function |
| 3318 | */ |
| 3319 | static void |
| 3320 | f_filereadable(typval_T *argvars, typval_T *rettv) |
| 3321 | { |
| 3322 | int fd; |
| 3323 | char_u *p; |
| 3324 | int n; |
| 3325 | |
| 3326 | #ifndef O_NONBLOCK |
| 3327 | # define O_NONBLOCK 0 |
| 3328 | #endif |
| 3329 | p = get_tv_string(&argvars[0]); |
| 3330 | if (*p && !mch_isdir(p) && (fd = mch_open((char *)p, |
| 3331 | O_RDONLY | O_NONBLOCK, 0)) >= 0) |
| 3332 | { |
| 3333 | n = TRUE; |
| 3334 | close(fd); |
| 3335 | } |
| 3336 | else |
| 3337 | n = FALSE; |
| 3338 | |
| 3339 | rettv->vval.v_number = n; |
| 3340 | } |
| 3341 | |
| 3342 | /* |
| 3343 | * Return 0 for not writable, 1 for writable file, 2 for a dir which we have |
| 3344 | * rights to write into. |
| 3345 | */ |
| 3346 | static void |
| 3347 | f_filewritable(typval_T *argvars, typval_T *rettv) |
| 3348 | { |
| 3349 | rettv->vval.v_number = filewritable(get_tv_string(&argvars[0])); |
| 3350 | } |
| 3351 | |
| 3352 | static void |
| 3353 | findfilendir( |
| 3354 | typval_T *argvars UNUSED, |
| 3355 | typval_T *rettv, |
| 3356 | int find_what UNUSED) |
| 3357 | { |
| 3358 | #ifdef FEAT_SEARCHPATH |
| 3359 | char_u *fname; |
| 3360 | char_u *fresult = NULL; |
| 3361 | char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path; |
| 3362 | char_u *p; |
| 3363 | char_u pathbuf[NUMBUFLEN]; |
| 3364 | int count = 1; |
| 3365 | int first = TRUE; |
| 3366 | int error = FALSE; |
| 3367 | #endif |
| 3368 | |
| 3369 | rettv->vval.v_string = NULL; |
| 3370 | rettv->v_type = VAR_STRING; |
| 3371 | |
| 3372 | #ifdef FEAT_SEARCHPATH |
| 3373 | fname = get_tv_string(&argvars[0]); |
| 3374 | |
| 3375 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 3376 | { |
| 3377 | p = get_tv_string_buf_chk(&argvars[1], pathbuf); |
| 3378 | if (p == NULL) |
| 3379 | error = TRUE; |
| 3380 | else |
| 3381 | { |
| 3382 | if (*p != NUL) |
| 3383 | path = p; |
| 3384 | |
| 3385 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 3386 | count = (int)get_tv_number_chk(&argvars[2], &error); |
| 3387 | } |
| 3388 | } |
| 3389 | |
| 3390 | if (count < 0 && rettv_list_alloc(rettv) == FAIL) |
| 3391 | error = TRUE; |
| 3392 | |
| 3393 | if (*fname != NUL && !error) |
| 3394 | { |
| 3395 | do |
| 3396 | { |
| 3397 | if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST) |
| 3398 | vim_free(fresult); |
| 3399 | fresult = find_file_in_path_option(first ? fname : NULL, |
| 3400 | first ? (int)STRLEN(fname) : 0, |
| 3401 | 0, first, path, |
| 3402 | find_what, |
| 3403 | curbuf->b_ffname, |
| 3404 | find_what == FINDFILE_DIR |
| 3405 | ? (char_u *)"" : curbuf->b_p_sua); |
| 3406 | first = FALSE; |
| 3407 | |
| 3408 | if (fresult != NULL && rettv->v_type == VAR_LIST) |
| 3409 | list_append_string(rettv->vval.v_list, fresult, -1); |
| 3410 | |
| 3411 | } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL); |
| 3412 | } |
| 3413 | |
| 3414 | if (rettv->v_type == VAR_STRING) |
| 3415 | rettv->vval.v_string = fresult; |
| 3416 | #endif |
| 3417 | } |
| 3418 | |
| 3419 | /* |
| 3420 | * "filter()" function |
| 3421 | */ |
| 3422 | static void |
| 3423 | f_filter(typval_T *argvars, typval_T *rettv) |
| 3424 | { |
| 3425 | filter_map(argvars, rettv, FALSE); |
| 3426 | } |
| 3427 | |
| 3428 | /* |
| 3429 | * "finddir({fname}[, {path}[, {count}]])" function |
| 3430 | */ |
| 3431 | static void |
| 3432 | f_finddir(typval_T *argvars, typval_T *rettv) |
| 3433 | { |
| 3434 | findfilendir(argvars, rettv, FINDFILE_DIR); |
| 3435 | } |
| 3436 | |
| 3437 | /* |
| 3438 | * "findfile({fname}[, {path}[, {count}]])" function |
| 3439 | */ |
| 3440 | static void |
| 3441 | f_findfile(typval_T *argvars, typval_T *rettv) |
| 3442 | { |
| 3443 | findfilendir(argvars, rettv, FINDFILE_FILE); |
| 3444 | } |
| 3445 | |
| 3446 | #ifdef FEAT_FLOAT |
| 3447 | /* |
| 3448 | * "float2nr({float})" function |
| 3449 | */ |
| 3450 | static void |
| 3451 | f_float2nr(typval_T *argvars, typval_T *rettv) |
| 3452 | { |
| 3453 | float_T f = 0.0; |
| 3454 | |
| 3455 | if (get_float_arg(argvars, &f) == OK) |
| 3456 | { |
Bram Moolenaar | 863e80b | 2017-06-04 20:30:00 +0200 | [diff] [blame] | 3457 | if (f <= -VARNUM_MAX + DBL_EPSILON) |
Bram Moolenaar | 7a40ea2 | 2017-01-22 18:34:57 +0100 | [diff] [blame] | 3458 | rettv->vval.v_number = -VARNUM_MAX; |
Bram Moolenaar | 863e80b | 2017-06-04 20:30:00 +0200 | [diff] [blame] | 3459 | else if (f >= VARNUM_MAX - DBL_EPSILON) |
Bram Moolenaar | 7a40ea2 | 2017-01-22 18:34:57 +0100 | [diff] [blame] | 3460 | rettv->vval.v_number = VARNUM_MAX; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3461 | else |
| 3462 | rettv->vval.v_number = (varnumber_T)f; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3463 | } |
| 3464 | } |
| 3465 | |
| 3466 | /* |
| 3467 | * "floor({float})" function |
| 3468 | */ |
| 3469 | static void |
| 3470 | f_floor(typval_T *argvars, typval_T *rettv) |
| 3471 | { |
| 3472 | float_T f = 0.0; |
| 3473 | |
| 3474 | rettv->v_type = VAR_FLOAT; |
| 3475 | if (get_float_arg(argvars, &f) == OK) |
| 3476 | rettv->vval.v_float = floor(f); |
| 3477 | else |
| 3478 | rettv->vval.v_float = 0.0; |
| 3479 | } |
| 3480 | |
| 3481 | /* |
| 3482 | * "fmod()" function |
| 3483 | */ |
| 3484 | static void |
| 3485 | f_fmod(typval_T *argvars, typval_T *rettv) |
| 3486 | { |
| 3487 | float_T fx = 0.0, fy = 0.0; |
| 3488 | |
| 3489 | rettv->v_type = VAR_FLOAT; |
| 3490 | if (get_float_arg(argvars, &fx) == OK |
| 3491 | && get_float_arg(&argvars[1], &fy) == OK) |
| 3492 | rettv->vval.v_float = fmod(fx, fy); |
| 3493 | else |
| 3494 | rettv->vval.v_float = 0.0; |
| 3495 | } |
| 3496 | #endif |
| 3497 | |
| 3498 | /* |
| 3499 | * "fnameescape({string})" function |
| 3500 | */ |
| 3501 | static void |
| 3502 | f_fnameescape(typval_T *argvars, typval_T *rettv) |
| 3503 | { |
| 3504 | rettv->vval.v_string = vim_strsave_fnameescape( |
| 3505 | get_tv_string(&argvars[0]), FALSE); |
| 3506 | rettv->v_type = VAR_STRING; |
| 3507 | } |
| 3508 | |
| 3509 | /* |
| 3510 | * "fnamemodify({fname}, {mods})" function |
| 3511 | */ |
| 3512 | static void |
| 3513 | f_fnamemodify(typval_T *argvars, typval_T *rettv) |
| 3514 | { |
| 3515 | char_u *fname; |
| 3516 | char_u *mods; |
| 3517 | int usedlen = 0; |
| 3518 | int len; |
| 3519 | char_u *fbuf = NULL; |
| 3520 | char_u buf[NUMBUFLEN]; |
| 3521 | |
| 3522 | fname = get_tv_string_chk(&argvars[0]); |
| 3523 | mods = get_tv_string_buf_chk(&argvars[1], buf); |
| 3524 | if (fname == NULL || mods == NULL) |
| 3525 | fname = NULL; |
| 3526 | else |
| 3527 | { |
| 3528 | len = (int)STRLEN(fname); |
| 3529 | (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len); |
| 3530 | } |
| 3531 | |
| 3532 | rettv->v_type = VAR_STRING; |
| 3533 | if (fname == NULL) |
| 3534 | rettv->vval.v_string = NULL; |
| 3535 | else |
| 3536 | rettv->vval.v_string = vim_strnsave(fname, len); |
| 3537 | vim_free(fbuf); |
| 3538 | } |
| 3539 | |
| 3540 | static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end); |
| 3541 | |
| 3542 | /* |
| 3543 | * "foldclosed()" function |
| 3544 | */ |
| 3545 | static void |
| 3546 | foldclosed_both( |
| 3547 | typval_T *argvars UNUSED, |
| 3548 | typval_T *rettv, |
| 3549 | int end UNUSED) |
| 3550 | { |
| 3551 | #ifdef FEAT_FOLDING |
| 3552 | linenr_T lnum; |
| 3553 | linenr_T first, last; |
| 3554 | |
| 3555 | lnum = get_tv_lnum(argvars); |
| 3556 | if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) |
| 3557 | { |
| 3558 | if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL)) |
| 3559 | { |
| 3560 | if (end) |
| 3561 | rettv->vval.v_number = (varnumber_T)last; |
| 3562 | else |
| 3563 | rettv->vval.v_number = (varnumber_T)first; |
| 3564 | return; |
| 3565 | } |
| 3566 | } |
| 3567 | #endif |
| 3568 | rettv->vval.v_number = -1; |
| 3569 | } |
| 3570 | |
| 3571 | /* |
| 3572 | * "foldclosed()" function |
| 3573 | */ |
| 3574 | static void |
| 3575 | f_foldclosed(typval_T *argvars, typval_T *rettv) |
| 3576 | { |
| 3577 | foldclosed_both(argvars, rettv, FALSE); |
| 3578 | } |
| 3579 | |
| 3580 | /* |
| 3581 | * "foldclosedend()" function |
| 3582 | */ |
| 3583 | static void |
| 3584 | f_foldclosedend(typval_T *argvars, typval_T *rettv) |
| 3585 | { |
| 3586 | foldclosed_both(argvars, rettv, TRUE); |
| 3587 | } |
| 3588 | |
| 3589 | /* |
| 3590 | * "foldlevel()" function |
| 3591 | */ |
| 3592 | static void |
| 3593 | f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 3594 | { |
| 3595 | #ifdef FEAT_FOLDING |
| 3596 | linenr_T lnum; |
| 3597 | |
| 3598 | lnum = get_tv_lnum(argvars); |
| 3599 | if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) |
| 3600 | rettv->vval.v_number = foldLevel(lnum); |
| 3601 | #endif |
| 3602 | } |
| 3603 | |
| 3604 | /* |
| 3605 | * "foldtext()" function |
| 3606 | */ |
| 3607 | static void |
| 3608 | f_foldtext(typval_T *argvars UNUSED, typval_T *rettv) |
| 3609 | { |
| 3610 | #ifdef FEAT_FOLDING |
| 3611 | linenr_T foldstart; |
| 3612 | linenr_T foldend; |
| 3613 | char_u *dashes; |
| 3614 | linenr_T lnum; |
| 3615 | char_u *s; |
| 3616 | char_u *r; |
| 3617 | int len; |
| 3618 | char *txt; |
Bram Moolenaar | ee695f7 | 2016-08-03 22:08:45 +0200 | [diff] [blame] | 3619 | long count; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3620 | #endif |
| 3621 | |
| 3622 | rettv->v_type = VAR_STRING; |
| 3623 | rettv->vval.v_string = NULL; |
| 3624 | #ifdef FEAT_FOLDING |
| 3625 | foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART); |
| 3626 | foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND); |
| 3627 | dashes = get_vim_var_str(VV_FOLDDASHES); |
| 3628 | if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count |
| 3629 | && dashes != NULL) |
| 3630 | { |
| 3631 | /* Find first non-empty line in the fold. */ |
Bram Moolenaar | 69aa099 | 2016-07-17 22:33:53 +0200 | [diff] [blame] | 3632 | for (lnum = foldstart; lnum < foldend; ++lnum) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3633 | if (!linewhite(lnum)) |
| 3634 | break; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3635 | |
| 3636 | /* Find interesting text in this line. */ |
| 3637 | s = skipwhite(ml_get(lnum)); |
| 3638 | /* skip C comment-start */ |
| 3639 | if (s[0] == '/' && (s[1] == '*' || s[1] == '/')) |
| 3640 | { |
| 3641 | s = skipwhite(s + 2); |
| 3642 | if (*skipwhite(s) == NUL |
| 3643 | && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND)) |
| 3644 | { |
| 3645 | s = skipwhite(ml_get(lnum + 1)); |
| 3646 | if (*s == '*') |
| 3647 | s = skipwhite(s + 1); |
| 3648 | } |
| 3649 | } |
Bram Moolenaar | ee695f7 | 2016-08-03 22:08:45 +0200 | [diff] [blame] | 3650 | count = (long)(foldend - foldstart + 1); |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 3651 | txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3652 | r = alloc((unsigned)(STRLEN(txt) |
| 3653 | + STRLEN(dashes) /* for %s */ |
| 3654 | + 20 /* for %3ld */ |
| 3655 | + STRLEN(s))); /* concatenated */ |
| 3656 | if (r != NULL) |
| 3657 | { |
Bram Moolenaar | ee695f7 | 2016-08-03 22:08:45 +0200 | [diff] [blame] | 3658 | sprintf((char *)r, txt, dashes, count); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3659 | len = (int)STRLEN(r); |
| 3660 | STRCAT(r, s); |
| 3661 | /* remove 'foldmarker' and 'commentstring' */ |
| 3662 | foldtext_cleanup(r + len); |
| 3663 | rettv->vval.v_string = r; |
| 3664 | } |
| 3665 | } |
| 3666 | #endif |
| 3667 | } |
| 3668 | |
| 3669 | /* |
| 3670 | * "foldtextresult(lnum)" function |
| 3671 | */ |
| 3672 | static void |
| 3673 | f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv) |
| 3674 | { |
| 3675 | #ifdef FEAT_FOLDING |
| 3676 | linenr_T lnum; |
| 3677 | char_u *text; |
Bram Moolenaar | ee695f7 | 2016-08-03 22:08:45 +0200 | [diff] [blame] | 3678 | char_u buf[FOLD_TEXT_LEN]; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3679 | foldinfo_T foldinfo; |
| 3680 | int fold_count; |
Bram Moolenaar | 495b7dd | 2017-09-16 17:19:22 +0200 | [diff] [blame] | 3681 | static int entered = FALSE; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3682 | #endif |
| 3683 | |
| 3684 | rettv->v_type = VAR_STRING; |
| 3685 | rettv->vval.v_string = NULL; |
| 3686 | #ifdef FEAT_FOLDING |
Bram Moolenaar | 495b7dd | 2017-09-16 17:19:22 +0200 | [diff] [blame] | 3687 | if (entered) |
| 3688 | return; /* reject recursive use */ |
| 3689 | entered = TRUE; |
| 3690 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3691 | lnum = get_tv_lnum(argvars); |
| 3692 | /* treat illegal types and illegal string values for {lnum} the same */ |
| 3693 | if (lnum < 0) |
| 3694 | lnum = 0; |
| 3695 | fold_count = foldedCount(curwin, lnum, &foldinfo); |
| 3696 | if (fold_count > 0) |
| 3697 | { |
Bram Moolenaar | c6aafba | 2017-03-21 17:09:10 +0100 | [diff] [blame] | 3698 | text = get_foldtext(curwin, lnum, lnum + fold_count - 1, |
| 3699 | &foldinfo, buf); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3700 | if (text == buf) |
| 3701 | text = vim_strsave(text); |
| 3702 | rettv->vval.v_string = text; |
| 3703 | } |
Bram Moolenaar | 495b7dd | 2017-09-16 17:19:22 +0200 | [diff] [blame] | 3704 | |
| 3705 | entered = FALSE; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3706 | #endif |
| 3707 | } |
| 3708 | |
| 3709 | /* |
| 3710 | * "foreground()" function |
| 3711 | */ |
| 3712 | static void |
| 3713 | f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 3714 | { |
| 3715 | #ifdef FEAT_GUI |
| 3716 | if (gui.in_use) |
| 3717 | gui_mch_set_foreground(); |
| 3718 | #else |
| 3719 | # ifdef WIN32 |
| 3720 | win32_set_foreground(); |
| 3721 | # endif |
| 3722 | #endif |
| 3723 | } |
| 3724 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3725 | static void |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 3726 | common_function(typval_T *argvars, typval_T *rettv, int is_funcref) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3727 | { |
| 3728 | char_u *s; |
| 3729 | char_u *name; |
| 3730 | int use_string = FALSE; |
| 3731 | partial_T *arg_pt = NULL; |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 3732 | char_u *trans_name = NULL; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3733 | |
| 3734 | if (argvars[0].v_type == VAR_FUNC) |
| 3735 | { |
| 3736 | /* function(MyFunc, [arg], dict) */ |
| 3737 | s = argvars[0].vval.v_string; |
| 3738 | } |
| 3739 | else if (argvars[0].v_type == VAR_PARTIAL |
| 3740 | && argvars[0].vval.v_partial != NULL) |
| 3741 | { |
| 3742 | /* function(dict.MyFunc, [arg]) */ |
| 3743 | arg_pt = argvars[0].vval.v_partial; |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 3744 | s = partial_name(arg_pt); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3745 | } |
| 3746 | else |
| 3747 | { |
| 3748 | /* function('MyFunc', [arg], dict) */ |
| 3749 | s = get_tv_string(&argvars[0]); |
| 3750 | use_string = TRUE; |
| 3751 | } |
| 3752 | |
Bram Moolenaar | 843b884 | 2016-08-21 14:36:15 +0200 | [diff] [blame] | 3753 | if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref) |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 3754 | { |
| 3755 | name = s; |
| 3756 | trans_name = trans_function_name(&name, FALSE, |
| 3757 | TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL); |
| 3758 | if (*name != NUL) |
| 3759 | s = NULL; |
| 3760 | } |
| 3761 | |
Bram Moolenaar | 843b884 | 2016-08-21 14:36:15 +0200 | [diff] [blame] | 3762 | if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)) |
| 3763 | || (is_funcref && trans_name == NULL)) |
Bram Moolenaar | 5582ef1 | 2016-09-14 22:16:13 +0200 | [diff] [blame] | 3764 | EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3765 | /* Don't check an autoload name for existence here. */ |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 3766 | else if (trans_name != NULL && (is_funcref |
| 3767 | ? find_func(trans_name) == NULL |
| 3768 | : !translated_function_exists(trans_name))) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3769 | EMSG2(_("E700: Unknown function: %s"), s); |
| 3770 | else |
| 3771 | { |
| 3772 | int dict_idx = 0; |
| 3773 | int arg_idx = 0; |
| 3774 | list_T *list = NULL; |
| 3775 | |
| 3776 | if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0) |
| 3777 | { |
| 3778 | char sid_buf[25]; |
| 3779 | int off = *s == 's' ? 2 : 5; |
| 3780 | |
| 3781 | /* Expand s: and <SID> into <SNR>nr_, so that the function can |
| 3782 | * also be called from another script. Using trans_function_name() |
| 3783 | * would also work, but some plugins depend on the name being |
| 3784 | * printable text. */ |
| 3785 | sprintf(sid_buf, "<SNR>%ld_", (long)current_SID); |
| 3786 | name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1)); |
| 3787 | if (name != NULL) |
| 3788 | { |
| 3789 | STRCPY(name, sid_buf); |
| 3790 | STRCAT(name, s + off); |
| 3791 | } |
| 3792 | } |
| 3793 | else |
| 3794 | name = vim_strsave(s); |
| 3795 | |
| 3796 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 3797 | { |
| 3798 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 3799 | { |
| 3800 | /* function(name, [args], dict) */ |
| 3801 | arg_idx = 1; |
| 3802 | dict_idx = 2; |
| 3803 | } |
| 3804 | else if (argvars[1].v_type == VAR_DICT) |
| 3805 | /* function(name, dict) */ |
| 3806 | dict_idx = 1; |
| 3807 | else |
| 3808 | /* function(name, [args]) */ |
| 3809 | arg_idx = 1; |
| 3810 | if (dict_idx > 0) |
| 3811 | { |
| 3812 | if (argvars[dict_idx].v_type != VAR_DICT) |
| 3813 | { |
| 3814 | EMSG(_("E922: expected a dict")); |
| 3815 | vim_free(name); |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 3816 | goto theend; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3817 | } |
| 3818 | if (argvars[dict_idx].vval.v_dict == NULL) |
| 3819 | dict_idx = 0; |
| 3820 | } |
| 3821 | if (arg_idx > 0) |
| 3822 | { |
| 3823 | if (argvars[arg_idx].v_type != VAR_LIST) |
| 3824 | { |
| 3825 | EMSG(_("E923: Second argument of function() must be a list or a dict")); |
| 3826 | vim_free(name); |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 3827 | goto theend; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3828 | } |
| 3829 | list = argvars[arg_idx].vval.v_list; |
| 3830 | if (list == NULL || list->lv_len == 0) |
| 3831 | arg_idx = 0; |
| 3832 | } |
| 3833 | } |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 3834 | if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3835 | { |
| 3836 | partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T)); |
| 3837 | |
| 3838 | /* result is a VAR_PARTIAL */ |
| 3839 | if (pt == NULL) |
| 3840 | vim_free(name); |
| 3841 | else |
| 3842 | { |
| 3843 | if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0)) |
| 3844 | { |
| 3845 | listitem_T *li; |
| 3846 | int i = 0; |
| 3847 | int arg_len = 0; |
| 3848 | int lv_len = 0; |
| 3849 | |
| 3850 | if (arg_pt != NULL) |
| 3851 | arg_len = arg_pt->pt_argc; |
| 3852 | if (list != NULL) |
| 3853 | lv_len = list->lv_len; |
| 3854 | pt->pt_argc = arg_len + lv_len; |
| 3855 | pt->pt_argv = (typval_T *)alloc( |
| 3856 | sizeof(typval_T) * pt->pt_argc); |
| 3857 | if (pt->pt_argv == NULL) |
| 3858 | { |
| 3859 | vim_free(pt); |
| 3860 | vim_free(name); |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 3861 | goto theend; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3862 | } |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 3863 | for (i = 0; i < arg_len; i++) |
| 3864 | copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]); |
| 3865 | if (lv_len > 0) |
| 3866 | for (li = list->lv_first; li != NULL; |
| 3867 | li = li->li_next) |
| 3868 | copy_tv(&li->li_tv, &pt->pt_argv[i++]); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3869 | } |
| 3870 | |
| 3871 | /* For "function(dict.func, [], dict)" and "func" is a partial |
| 3872 | * use "dict". That is backwards compatible. */ |
| 3873 | if (dict_idx > 0) |
| 3874 | { |
| 3875 | /* The dict is bound explicitly, pt_auto is FALSE. */ |
| 3876 | pt->pt_dict = argvars[dict_idx].vval.v_dict; |
| 3877 | ++pt->pt_dict->dv_refcount; |
| 3878 | } |
| 3879 | else if (arg_pt != NULL) |
| 3880 | { |
| 3881 | /* If the dict was bound automatically the result is also |
| 3882 | * bound automatically. */ |
| 3883 | pt->pt_dict = arg_pt->pt_dict; |
| 3884 | pt->pt_auto = arg_pt->pt_auto; |
| 3885 | if (pt->pt_dict != NULL) |
| 3886 | ++pt->pt_dict->dv_refcount; |
| 3887 | } |
| 3888 | |
| 3889 | pt->pt_refcount = 1; |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 3890 | if (arg_pt != NULL && arg_pt->pt_func != NULL) |
| 3891 | { |
| 3892 | pt->pt_func = arg_pt->pt_func; |
| 3893 | func_ptr_ref(pt->pt_func); |
| 3894 | vim_free(name); |
| 3895 | } |
| 3896 | else if (is_funcref) |
| 3897 | { |
| 3898 | pt->pt_func = find_func(trans_name); |
| 3899 | func_ptr_ref(pt->pt_func); |
| 3900 | vim_free(name); |
| 3901 | } |
| 3902 | else |
| 3903 | { |
| 3904 | pt->pt_name = name; |
| 3905 | func_ref(name); |
| 3906 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3907 | } |
| 3908 | rettv->v_type = VAR_PARTIAL; |
| 3909 | rettv->vval.v_partial = pt; |
| 3910 | } |
| 3911 | else |
| 3912 | { |
| 3913 | /* result is a VAR_FUNC */ |
| 3914 | rettv->v_type = VAR_FUNC; |
| 3915 | rettv->vval.v_string = name; |
| 3916 | func_ref(name); |
| 3917 | } |
| 3918 | } |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 3919 | theend: |
| 3920 | vim_free(trans_name); |
| 3921 | } |
| 3922 | |
| 3923 | /* |
| 3924 | * "funcref()" function |
| 3925 | */ |
| 3926 | static void |
| 3927 | f_funcref(typval_T *argvars, typval_T *rettv) |
| 3928 | { |
| 3929 | common_function(argvars, rettv, TRUE); |
| 3930 | } |
| 3931 | |
| 3932 | /* |
| 3933 | * "function()" function |
| 3934 | */ |
| 3935 | static void |
| 3936 | f_function(typval_T *argvars, typval_T *rettv) |
| 3937 | { |
| 3938 | common_function(argvars, rettv, FALSE); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 3939 | } |
| 3940 | |
| 3941 | /* |
| 3942 | * "garbagecollect()" function |
| 3943 | */ |
| 3944 | static void |
| 3945 | f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED) |
| 3946 | { |
| 3947 | /* This is postponed until we are back at the toplevel, because we may be |
| 3948 | * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */ |
| 3949 | want_garbage_collect = TRUE; |
| 3950 | |
| 3951 | if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1) |
| 3952 | garbage_collect_at_exit = TRUE; |
| 3953 | } |
| 3954 | |
| 3955 | /* |
| 3956 | * "get()" function |
| 3957 | */ |
| 3958 | static void |
| 3959 | f_get(typval_T *argvars, typval_T *rettv) |
| 3960 | { |
| 3961 | listitem_T *li; |
| 3962 | list_T *l; |
| 3963 | dictitem_T *di; |
| 3964 | dict_T *d; |
| 3965 | typval_T *tv = NULL; |
| 3966 | |
| 3967 | if (argvars[0].v_type == VAR_LIST) |
| 3968 | { |
| 3969 | if ((l = argvars[0].vval.v_list) != NULL) |
| 3970 | { |
| 3971 | int error = FALSE; |
| 3972 | |
| 3973 | li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error)); |
| 3974 | if (!error && li != NULL) |
| 3975 | tv = &li->li_tv; |
| 3976 | } |
| 3977 | } |
| 3978 | else if (argvars[0].v_type == VAR_DICT) |
| 3979 | { |
| 3980 | if ((d = argvars[0].vval.v_dict) != NULL) |
| 3981 | { |
| 3982 | di = dict_find(d, get_tv_string(&argvars[1]), -1); |
| 3983 | if (di != NULL) |
| 3984 | tv = &di->di_tv; |
| 3985 | } |
| 3986 | } |
| 3987 | else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC) |
| 3988 | { |
| 3989 | partial_T *pt; |
| 3990 | partial_T fref_pt; |
| 3991 | |
| 3992 | if (argvars[0].v_type == VAR_PARTIAL) |
| 3993 | pt = argvars[0].vval.v_partial; |
| 3994 | else |
| 3995 | { |
| 3996 | vim_memset(&fref_pt, 0, sizeof(fref_pt)); |
| 3997 | fref_pt.pt_name = argvars[0].vval.v_string; |
| 3998 | pt = &fref_pt; |
| 3999 | } |
| 4000 | |
| 4001 | if (pt != NULL) |
| 4002 | { |
| 4003 | char_u *what = get_tv_string(&argvars[1]); |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 4004 | char_u *n; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4005 | |
| 4006 | if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0) |
| 4007 | { |
| 4008 | rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING); |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 4009 | n = partial_name(pt); |
| 4010 | if (n == NULL) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4011 | rettv->vval.v_string = NULL; |
| 4012 | else |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 4013 | { |
| 4014 | rettv->vval.v_string = vim_strsave(n); |
| 4015 | if (rettv->v_type == VAR_FUNC) |
| 4016 | func_ref(rettv->vval.v_string); |
| 4017 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4018 | } |
| 4019 | else if (STRCMP(what, "dict") == 0) |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 4020 | rettv_dict_set(rettv, pt->pt_dict); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4021 | else if (STRCMP(what, "args") == 0) |
| 4022 | { |
| 4023 | rettv->v_type = VAR_LIST; |
| 4024 | if (rettv_list_alloc(rettv) == OK) |
| 4025 | { |
| 4026 | int i; |
| 4027 | |
| 4028 | for (i = 0; i < pt->pt_argc; ++i) |
| 4029 | list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]); |
| 4030 | } |
| 4031 | } |
| 4032 | else |
| 4033 | EMSG2(_(e_invarg2), what); |
| 4034 | return; |
| 4035 | } |
| 4036 | } |
| 4037 | else |
| 4038 | EMSG2(_(e_listdictarg), "get()"); |
| 4039 | |
| 4040 | if (tv == NULL) |
| 4041 | { |
| 4042 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 4043 | copy_tv(&argvars[2], rettv); |
| 4044 | } |
| 4045 | else |
| 4046 | copy_tv(tv, rettv); |
| 4047 | } |
| 4048 | |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 4049 | #ifdef FEAT_SIGNS |
| 4050 | /* |
| 4051 | * Returns information about signs placed in a buffer as list of dicts. |
| 4052 | */ |
| 4053 | static void |
| 4054 | get_buffer_signs(buf_T *buf, list_T *l) |
| 4055 | { |
| 4056 | signlist_T *sign; |
| 4057 | |
| 4058 | for (sign = buf->b_signlist; sign; sign = sign->next) |
| 4059 | { |
| 4060 | dict_T *d = dict_alloc(); |
| 4061 | |
| 4062 | if (d != NULL) |
| 4063 | { |
| 4064 | dict_add_nr_str(d, "id", sign->id, NULL); |
| 4065 | dict_add_nr_str(d, "lnum", sign->lnum, NULL); |
Bram Moolenaar | 6a402ed | 2016-08-28 14:11:24 +0200 | [diff] [blame] | 4066 | dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr)); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 4067 | |
| 4068 | list_append_dict(l, d); |
| 4069 | } |
| 4070 | } |
| 4071 | } |
| 4072 | #endif |
| 4073 | |
| 4074 | /* |
| 4075 | * Returns buffer options, variables and other attributes in a dictionary. |
| 4076 | */ |
| 4077 | static dict_T * |
| 4078 | get_buffer_info(buf_T *buf) |
| 4079 | { |
| 4080 | dict_T *dict; |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 4081 | tabpage_T *tp; |
| 4082 | win_T *wp; |
| 4083 | list_T *windows; |
| 4084 | |
| 4085 | dict = dict_alloc(); |
| 4086 | if (dict == NULL) |
| 4087 | return NULL; |
| 4088 | |
Bram Moolenaar | 3392883 | 2016-08-18 21:22:04 +0200 | [diff] [blame] | 4089 | dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 4090 | dict_add_nr_str(dict, "name", 0L, |
| 4091 | buf->b_ffname != NULL ? buf->b_ffname : (char_u *)""); |
Bram Moolenaar | f845b87 | 2017-01-06 14:04:54 +0100 | [diff] [blame] | 4092 | dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum |
| 4093 | : buflist_findlnum(buf), NULL); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 4094 | dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL); |
| 4095 | dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL); |
| 4096 | dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL); |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 4097 | dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 4098 | dict_add_nr_str(dict, "hidden", |
| 4099 | buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0, |
| 4100 | NULL); |
| 4101 | |
Bram Moolenaar | 9f8187c | 2016-08-27 20:34:01 +0200 | [diff] [blame] | 4102 | /* Get a reference to buffer variables */ |
| 4103 | dict_add_dict(dict, "variables", buf->b_vars); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 4104 | |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 4105 | /* List of windows displaying this buffer */ |
| 4106 | windows = list_alloc(); |
| 4107 | if (windows != NULL) |
| 4108 | { |
| 4109 | FOR_ALL_TAB_WINDOWS(tp, wp) |
| 4110 | if (wp->w_buffer == buf) |
| 4111 | list_append_number(windows, (varnumber_T)wp->w_id); |
| 4112 | dict_add_list(dict, "windows", windows); |
| 4113 | } |
| 4114 | |
| 4115 | #ifdef FEAT_SIGNS |
| 4116 | if (buf->b_signlist != NULL) |
| 4117 | { |
| 4118 | /* List of signs placed in this buffer */ |
| 4119 | list_T *signs = list_alloc(); |
| 4120 | if (signs != NULL) |
| 4121 | { |
| 4122 | get_buffer_signs(buf, signs); |
| 4123 | dict_add_list(dict, "signs", signs); |
| 4124 | } |
| 4125 | } |
| 4126 | #endif |
| 4127 | |
| 4128 | return dict; |
| 4129 | } |
| 4130 | |
| 4131 | /* |
| 4132 | * "getbufinfo()" function |
| 4133 | */ |
| 4134 | static void |
| 4135 | f_getbufinfo(typval_T *argvars, typval_T *rettv) |
| 4136 | { |
| 4137 | buf_T *buf = NULL; |
| 4138 | buf_T *argbuf = NULL; |
| 4139 | dict_T *d; |
| 4140 | int filtered = FALSE; |
| 4141 | int sel_buflisted = FALSE; |
| 4142 | int sel_bufloaded = FALSE; |
Bram Moolenaar | 8e6a31d | 2017-12-10 21:06:22 +0100 | [diff] [blame] | 4143 | int sel_bufmodified = FALSE; |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 4144 | |
| 4145 | if (rettv_list_alloc(rettv) != OK) |
| 4146 | return; |
| 4147 | |
| 4148 | /* List of all the buffers or selected buffers */ |
| 4149 | if (argvars[0].v_type == VAR_DICT) |
| 4150 | { |
| 4151 | dict_T *sel_d = argvars[0].vval.v_dict; |
| 4152 | |
| 4153 | if (sel_d != NULL) |
| 4154 | { |
| 4155 | dictitem_T *di; |
| 4156 | |
| 4157 | filtered = TRUE; |
| 4158 | |
| 4159 | di = dict_find(sel_d, (char_u *)"buflisted", -1); |
| 4160 | if (di != NULL && get_tv_number(&di->di_tv)) |
| 4161 | sel_buflisted = TRUE; |
| 4162 | |
| 4163 | di = dict_find(sel_d, (char_u *)"bufloaded", -1); |
| 4164 | if (di != NULL && get_tv_number(&di->di_tv)) |
| 4165 | sel_bufloaded = TRUE; |
Bram Moolenaar | 8e6a31d | 2017-12-10 21:06:22 +0100 | [diff] [blame] | 4166 | |
| 4167 | di = dict_find(sel_d, (char_u *)"bufmodified", -1); |
| 4168 | if (di != NULL && get_tv_number(&di->di_tv)) |
| 4169 | sel_bufmodified = TRUE; |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 4170 | } |
| 4171 | } |
| 4172 | else if (argvars[0].v_type != VAR_UNKNOWN) |
| 4173 | { |
| 4174 | /* Information about one buffer. Argument specifies the buffer */ |
| 4175 | (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ |
| 4176 | ++emsg_off; |
| 4177 | argbuf = get_buf_tv(&argvars[0], FALSE); |
| 4178 | --emsg_off; |
| 4179 | if (argbuf == NULL) |
| 4180 | return; |
| 4181 | } |
| 4182 | |
| 4183 | /* Return information about all the buffers or a specified buffer */ |
Bram Moolenaar | 386600f | 2016-08-15 22:16:25 +0200 | [diff] [blame] | 4184 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 4185 | { |
| 4186 | if (argbuf != NULL && argbuf != buf) |
| 4187 | continue; |
| 4188 | if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL) |
Bram Moolenaar | 8e6a31d | 2017-12-10 21:06:22 +0100 | [diff] [blame] | 4189 | || (sel_buflisted && !buf->b_p_bl) |
| 4190 | || (sel_bufmodified && !buf->b_changed))) |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 4191 | continue; |
| 4192 | |
| 4193 | d = get_buffer_info(buf); |
| 4194 | if (d != NULL) |
| 4195 | list_append_dict(rettv->vval.v_list, d); |
| 4196 | if (argbuf != NULL) |
| 4197 | return; |
| 4198 | } |
| 4199 | } |
| 4200 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4201 | static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv); |
| 4202 | |
| 4203 | /* |
| 4204 | * Get line or list of lines from buffer "buf" into "rettv". |
| 4205 | * Return a range (from start to end) of lines in rettv from the specified |
| 4206 | * buffer. |
| 4207 | * If 'retlist' is TRUE, then the lines are returned as a Vim List. |
| 4208 | */ |
| 4209 | static void |
| 4210 | get_buffer_lines( |
| 4211 | buf_T *buf, |
| 4212 | linenr_T start, |
| 4213 | linenr_T end, |
| 4214 | int retlist, |
| 4215 | typval_T *rettv) |
| 4216 | { |
| 4217 | char_u *p; |
| 4218 | |
| 4219 | rettv->v_type = VAR_STRING; |
| 4220 | rettv->vval.v_string = NULL; |
| 4221 | if (retlist && rettv_list_alloc(rettv) == FAIL) |
| 4222 | return; |
| 4223 | |
| 4224 | if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0) |
| 4225 | return; |
| 4226 | |
| 4227 | if (!retlist) |
| 4228 | { |
| 4229 | if (start >= 1 && start <= buf->b_ml.ml_line_count) |
| 4230 | p = ml_get_buf(buf, start, FALSE); |
| 4231 | else |
| 4232 | p = (char_u *)""; |
| 4233 | rettv->vval.v_string = vim_strsave(p); |
| 4234 | } |
| 4235 | else |
| 4236 | { |
| 4237 | if (end < start) |
| 4238 | return; |
| 4239 | |
| 4240 | if (start < 1) |
| 4241 | start = 1; |
| 4242 | if (end > buf->b_ml.ml_line_count) |
| 4243 | end = buf->b_ml.ml_line_count; |
| 4244 | while (start <= end) |
| 4245 | if (list_append_string(rettv->vval.v_list, |
| 4246 | ml_get_buf(buf, start++, FALSE), -1) == FAIL) |
| 4247 | break; |
| 4248 | } |
| 4249 | } |
| 4250 | |
| 4251 | /* |
| 4252 | * Get the lnum from the first argument. |
| 4253 | * Also accepts "$", then "buf" is used. |
| 4254 | * Returns 0 on error. |
| 4255 | */ |
| 4256 | static linenr_T |
| 4257 | get_tv_lnum_buf(typval_T *argvars, buf_T *buf) |
| 4258 | { |
| 4259 | if (argvars[0].v_type == VAR_STRING |
| 4260 | && argvars[0].vval.v_string != NULL |
| 4261 | && argvars[0].vval.v_string[0] == '$' |
| 4262 | && buf != NULL) |
| 4263 | return buf->b_ml.ml_line_count; |
| 4264 | return (linenr_T)get_tv_number_chk(&argvars[0], NULL); |
| 4265 | } |
| 4266 | |
| 4267 | /* |
| 4268 | * "getbufline()" function |
| 4269 | */ |
| 4270 | static void |
| 4271 | f_getbufline(typval_T *argvars, typval_T *rettv) |
| 4272 | { |
| 4273 | linenr_T lnum; |
| 4274 | linenr_T end; |
| 4275 | buf_T *buf; |
| 4276 | |
| 4277 | (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ |
| 4278 | ++emsg_off; |
| 4279 | buf = get_buf_tv(&argvars[0], FALSE); |
| 4280 | --emsg_off; |
| 4281 | |
| 4282 | lnum = get_tv_lnum_buf(&argvars[1], buf); |
| 4283 | if (argvars[2].v_type == VAR_UNKNOWN) |
| 4284 | end = lnum; |
| 4285 | else |
| 4286 | end = get_tv_lnum_buf(&argvars[2], buf); |
| 4287 | |
| 4288 | get_buffer_lines(buf, lnum, end, TRUE, rettv); |
| 4289 | } |
| 4290 | |
| 4291 | /* |
| 4292 | * "getbufvar()" function |
| 4293 | */ |
| 4294 | static void |
| 4295 | f_getbufvar(typval_T *argvars, typval_T *rettv) |
| 4296 | { |
| 4297 | buf_T *buf; |
| 4298 | buf_T *save_curbuf; |
| 4299 | char_u *varname; |
| 4300 | dictitem_T *v; |
| 4301 | int done = FALSE; |
| 4302 | |
| 4303 | (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ |
| 4304 | varname = get_tv_string_chk(&argvars[1]); |
| 4305 | ++emsg_off; |
| 4306 | buf = get_buf_tv(&argvars[0], FALSE); |
| 4307 | |
| 4308 | rettv->v_type = VAR_STRING; |
| 4309 | rettv->vval.v_string = NULL; |
| 4310 | |
| 4311 | if (buf != NULL && varname != NULL) |
| 4312 | { |
| 4313 | /* set curbuf to be our buf, temporarily */ |
| 4314 | save_curbuf = curbuf; |
| 4315 | curbuf = buf; |
| 4316 | |
Bram Moolenaar | 3056735 | 2016-08-27 21:25:44 +0200 | [diff] [blame] | 4317 | if (*varname == '&') |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4318 | { |
Bram Moolenaar | 3056735 | 2016-08-27 21:25:44 +0200 | [diff] [blame] | 4319 | if (varname[1] == NUL) |
| 4320 | { |
| 4321 | /* get all buffer-local options in a dict */ |
| 4322 | dict_T *opts = get_winbuf_options(TRUE); |
| 4323 | |
| 4324 | if (opts != NULL) |
| 4325 | { |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 4326 | rettv_dict_set(rettv, opts); |
Bram Moolenaar | 3056735 | 2016-08-27 21:25:44 +0200 | [diff] [blame] | 4327 | done = TRUE; |
| 4328 | } |
| 4329 | } |
| 4330 | else if (get_option_tv(&varname, rettv, TRUE) == OK) |
| 4331 | /* buffer-local-option */ |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4332 | done = TRUE; |
| 4333 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4334 | else |
| 4335 | { |
| 4336 | /* Look up the variable. */ |
| 4337 | /* Let getbufvar({nr}, "") return the "b:" dictionary. */ |
| 4338 | v = find_var_in_ht(&curbuf->b_vars->dv_hashtab, |
| 4339 | 'b', varname, FALSE); |
| 4340 | if (v != NULL) |
| 4341 | { |
| 4342 | copy_tv(&v->di_tv, rettv); |
| 4343 | done = TRUE; |
| 4344 | } |
| 4345 | } |
| 4346 | |
| 4347 | /* restore previous notion of curbuf */ |
| 4348 | curbuf = save_curbuf; |
| 4349 | } |
| 4350 | |
| 4351 | if (!done && argvars[2].v_type != VAR_UNKNOWN) |
| 4352 | /* use the default value */ |
| 4353 | copy_tv(&argvars[2], rettv); |
| 4354 | |
| 4355 | --emsg_off; |
| 4356 | } |
| 4357 | |
| 4358 | /* |
| 4359 | * "getchar()" function |
| 4360 | */ |
| 4361 | static void |
| 4362 | f_getchar(typval_T *argvars, typval_T *rettv) |
| 4363 | { |
| 4364 | varnumber_T n; |
| 4365 | int error = FALSE; |
| 4366 | |
| 4367 | /* Position the cursor. Needed after a message that ends in a space. */ |
| 4368 | windgoto(msg_row, msg_col); |
| 4369 | |
| 4370 | ++no_mapping; |
| 4371 | ++allow_keys; |
| 4372 | for (;;) |
| 4373 | { |
| 4374 | if (argvars[0].v_type == VAR_UNKNOWN) |
| 4375 | /* getchar(): blocking wait. */ |
Bram Moolenaar | ec2da36 | 2017-01-21 20:04:22 +0100 | [diff] [blame] | 4376 | n = plain_vgetc(); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4377 | else if (get_tv_number_chk(&argvars[0], &error) == 1) |
| 4378 | /* getchar(1): only check if char avail */ |
| 4379 | n = vpeekc_any(); |
| 4380 | else if (error || vpeekc_any() == NUL) |
| 4381 | /* illegal argument or getchar(0) and no char avail: return zero */ |
| 4382 | n = 0; |
| 4383 | else |
| 4384 | /* getchar(0) and char avail: return char */ |
Bram Moolenaar | ec2da36 | 2017-01-21 20:04:22 +0100 | [diff] [blame] | 4385 | n = plain_vgetc(); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4386 | |
| 4387 | if (n == K_IGNORE) |
| 4388 | continue; |
| 4389 | break; |
| 4390 | } |
| 4391 | --no_mapping; |
| 4392 | --allow_keys; |
| 4393 | |
| 4394 | set_vim_var_nr(VV_MOUSE_WIN, 0); |
| 4395 | set_vim_var_nr(VV_MOUSE_WINID, 0); |
| 4396 | set_vim_var_nr(VV_MOUSE_LNUM, 0); |
| 4397 | set_vim_var_nr(VV_MOUSE_COL, 0); |
| 4398 | |
| 4399 | rettv->vval.v_number = n; |
| 4400 | if (IS_SPECIAL(n) || mod_mask != 0) |
| 4401 | { |
| 4402 | char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */ |
| 4403 | int i = 0; |
| 4404 | |
| 4405 | /* Turn a special key into three bytes, plus modifier. */ |
| 4406 | if (mod_mask != 0) |
| 4407 | { |
| 4408 | temp[i++] = K_SPECIAL; |
| 4409 | temp[i++] = KS_MODIFIER; |
| 4410 | temp[i++] = mod_mask; |
| 4411 | } |
| 4412 | if (IS_SPECIAL(n)) |
| 4413 | { |
| 4414 | temp[i++] = K_SPECIAL; |
| 4415 | temp[i++] = K_SECOND(n); |
| 4416 | temp[i++] = K_THIRD(n); |
| 4417 | } |
| 4418 | #ifdef FEAT_MBYTE |
| 4419 | else if (has_mbyte) |
| 4420 | i += (*mb_char2bytes)(n, temp + i); |
| 4421 | #endif |
| 4422 | else |
| 4423 | temp[i++] = n; |
| 4424 | temp[i++] = NUL; |
| 4425 | rettv->v_type = VAR_STRING; |
| 4426 | rettv->vval.v_string = vim_strsave(temp); |
| 4427 | |
| 4428 | #ifdef FEAT_MOUSE |
| 4429 | if (is_mouse_key(n)) |
| 4430 | { |
| 4431 | int row = mouse_row; |
| 4432 | int col = mouse_col; |
| 4433 | win_T *win; |
| 4434 | linenr_T lnum; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4435 | win_T *wp; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4436 | int winnr = 1; |
| 4437 | |
| 4438 | if (row >= 0 && col >= 0) |
| 4439 | { |
| 4440 | /* Find the window at the mouse coordinates and compute the |
| 4441 | * text position. */ |
| 4442 | win = mouse_find_win(&row, &col); |
Bram Moolenaar | 989a70c | 2017-08-16 22:46:01 +0200 | [diff] [blame] | 4443 | if (win == NULL) |
| 4444 | return; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4445 | (void)mouse_comp_pos(win, &row, &col, &lnum); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4446 | for (wp = firstwin; wp != win; wp = wp->w_next) |
| 4447 | ++winnr; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4448 | set_vim_var_nr(VV_MOUSE_WIN, winnr); |
| 4449 | set_vim_var_nr(VV_MOUSE_WINID, win->w_id); |
| 4450 | set_vim_var_nr(VV_MOUSE_LNUM, lnum); |
| 4451 | set_vim_var_nr(VV_MOUSE_COL, col + 1); |
| 4452 | } |
| 4453 | } |
| 4454 | #endif |
| 4455 | } |
| 4456 | } |
| 4457 | |
| 4458 | /* |
| 4459 | * "getcharmod()" function |
| 4460 | */ |
| 4461 | static void |
| 4462 | f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv) |
| 4463 | { |
| 4464 | rettv->vval.v_number = mod_mask; |
| 4465 | } |
| 4466 | |
| 4467 | /* |
| 4468 | * "getcharsearch()" function |
| 4469 | */ |
| 4470 | static void |
| 4471 | f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv) |
| 4472 | { |
| 4473 | if (rettv_dict_alloc(rettv) != FAIL) |
| 4474 | { |
| 4475 | dict_T *dict = rettv->vval.v_dict; |
| 4476 | |
| 4477 | dict_add_nr_str(dict, "char", 0L, last_csearch()); |
| 4478 | dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL); |
| 4479 | dict_add_nr_str(dict, "until", last_csearch_until(), NULL); |
| 4480 | } |
| 4481 | } |
| 4482 | |
| 4483 | /* |
| 4484 | * "getcmdline()" function |
| 4485 | */ |
| 4486 | static void |
| 4487 | f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv) |
| 4488 | { |
| 4489 | rettv->v_type = VAR_STRING; |
| 4490 | rettv->vval.v_string = get_cmdline_str(); |
| 4491 | } |
| 4492 | |
| 4493 | /* |
| 4494 | * "getcmdpos()" function |
| 4495 | */ |
| 4496 | static void |
| 4497 | f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv) |
| 4498 | { |
| 4499 | rettv->vval.v_number = get_cmdline_pos() + 1; |
| 4500 | } |
| 4501 | |
| 4502 | /* |
| 4503 | * "getcmdtype()" function |
| 4504 | */ |
| 4505 | static void |
| 4506 | f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv) |
| 4507 | { |
| 4508 | rettv->v_type = VAR_STRING; |
| 4509 | rettv->vval.v_string = alloc(2); |
| 4510 | if (rettv->vval.v_string != NULL) |
| 4511 | { |
| 4512 | rettv->vval.v_string[0] = get_cmdline_type(); |
| 4513 | rettv->vval.v_string[1] = NUL; |
| 4514 | } |
| 4515 | } |
| 4516 | |
| 4517 | /* |
| 4518 | * "getcmdwintype()" function |
| 4519 | */ |
| 4520 | static void |
| 4521 | f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv) |
| 4522 | { |
| 4523 | rettv->v_type = VAR_STRING; |
| 4524 | rettv->vval.v_string = NULL; |
| 4525 | #ifdef FEAT_CMDWIN |
| 4526 | rettv->vval.v_string = alloc(2); |
| 4527 | if (rettv->vval.v_string != NULL) |
| 4528 | { |
| 4529 | rettv->vval.v_string[0] = cmdwin_type; |
| 4530 | rettv->vval.v_string[1] = NUL; |
| 4531 | } |
| 4532 | #endif |
| 4533 | } |
| 4534 | |
| 4535 | #if defined(FEAT_CMDL_COMPL) |
| 4536 | /* |
| 4537 | * "getcompletion()" function |
| 4538 | */ |
| 4539 | static void |
| 4540 | f_getcompletion(typval_T *argvars, typval_T *rettv) |
| 4541 | { |
| 4542 | char_u *pat; |
| 4543 | expand_T xpc; |
Bram Moolenaar | e9d58a6 | 2016-08-13 15:07:41 +0200 | [diff] [blame] | 4544 | int filtered = FALSE; |
Bram Moolenaar | b56195e | 2016-07-28 22:53:37 +0200 | [diff] [blame] | 4545 | int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH |
| 4546 | | WILD_NO_BEEP; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4547 | |
Bram Moolenaar | e9d58a6 | 2016-08-13 15:07:41 +0200 | [diff] [blame] | 4548 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 4549 | filtered = get_tv_number_chk(&argvars[2], NULL); |
| 4550 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4551 | if (p_wic) |
| 4552 | options |= WILD_ICASE; |
| 4553 | |
Bram Moolenaar | e9d58a6 | 2016-08-13 15:07:41 +0200 | [diff] [blame] | 4554 | /* For filtered results, 'wildignore' is used */ |
| 4555 | if (!filtered) |
| 4556 | options |= WILD_KEEP_ALL; |
| 4557 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4558 | ExpandInit(&xpc); |
| 4559 | xpc.xp_pattern = get_tv_string(&argvars[0]); |
| 4560 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
| 4561 | xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1])); |
| 4562 | if (xpc.xp_context == EXPAND_NOTHING) |
| 4563 | { |
| 4564 | if (argvars[1].v_type == VAR_STRING) |
| 4565 | EMSG2(_(e_invarg2), argvars[1].vval.v_string); |
| 4566 | else |
| 4567 | EMSG(_(e_invarg)); |
| 4568 | return; |
| 4569 | } |
| 4570 | |
| 4571 | # if defined(FEAT_MENU) |
| 4572 | if (xpc.xp_context == EXPAND_MENUS) |
| 4573 | { |
| 4574 | set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE); |
| 4575 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
| 4576 | } |
| 4577 | # endif |
Bram Moolenaar | b650b98 | 2016-08-05 20:35:13 +0200 | [diff] [blame] | 4578 | #ifdef FEAT_CSCOPE |
| 4579 | if (xpc.xp_context == EXPAND_CSCOPE) |
| 4580 | { |
| 4581 | set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope); |
| 4582 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
| 4583 | } |
| 4584 | #endif |
Bram Moolenaar | 7522f69 | 2016-08-06 14:12:50 +0200 | [diff] [blame] | 4585 | #ifdef FEAT_SIGNS |
| 4586 | if (xpc.xp_context == EXPAND_SIGN) |
| 4587 | { |
| 4588 | set_context_in_sign_cmd(&xpc, xpc.xp_pattern); |
| 4589 | xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern); |
| 4590 | } |
| 4591 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4592 | |
| 4593 | pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context); |
| 4594 | if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL)) |
| 4595 | { |
Bram Moolenaar | b56195e | 2016-07-28 22:53:37 +0200 | [diff] [blame] | 4596 | int i; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4597 | |
| 4598 | ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP); |
| 4599 | |
| 4600 | for (i = 0; i < xpc.xp_numfiles; i++) |
| 4601 | list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1); |
| 4602 | } |
| 4603 | vim_free(pat); |
| 4604 | ExpandCleanup(&xpc); |
| 4605 | } |
| 4606 | #endif |
| 4607 | |
| 4608 | /* |
| 4609 | * "getcwd()" function |
| 4610 | */ |
| 4611 | static void |
| 4612 | f_getcwd(typval_T *argvars, typval_T *rettv) |
| 4613 | { |
| 4614 | win_T *wp = NULL; |
| 4615 | char_u *cwd; |
| 4616 | |
| 4617 | rettv->v_type = VAR_STRING; |
| 4618 | rettv->vval.v_string = NULL; |
| 4619 | |
| 4620 | wp = find_tabwin(&argvars[0], &argvars[1]); |
| 4621 | if (wp != NULL) |
| 4622 | { |
| 4623 | if (wp->w_localdir != NULL) |
| 4624 | rettv->vval.v_string = vim_strsave(wp->w_localdir); |
| 4625 | else if (globaldir != NULL) |
| 4626 | rettv->vval.v_string = vim_strsave(globaldir); |
| 4627 | else |
| 4628 | { |
| 4629 | cwd = alloc(MAXPATHL); |
| 4630 | if (cwd != NULL) |
| 4631 | { |
| 4632 | if (mch_dirname(cwd, MAXPATHL) != FAIL) |
| 4633 | rettv->vval.v_string = vim_strsave(cwd); |
| 4634 | vim_free(cwd); |
| 4635 | } |
| 4636 | } |
| 4637 | #ifdef BACKSLASH_IN_FILENAME |
| 4638 | if (rettv->vval.v_string != NULL) |
| 4639 | slash_adjust(rettv->vval.v_string); |
| 4640 | #endif |
| 4641 | } |
| 4642 | } |
| 4643 | |
| 4644 | /* |
| 4645 | * "getfontname()" function |
| 4646 | */ |
| 4647 | static void |
| 4648 | f_getfontname(typval_T *argvars UNUSED, typval_T *rettv) |
| 4649 | { |
| 4650 | rettv->v_type = VAR_STRING; |
| 4651 | rettv->vval.v_string = NULL; |
| 4652 | #ifdef FEAT_GUI |
| 4653 | if (gui.in_use) |
| 4654 | { |
| 4655 | GuiFont font; |
| 4656 | char_u *name = NULL; |
| 4657 | |
| 4658 | if (argvars[0].v_type == VAR_UNKNOWN) |
| 4659 | { |
| 4660 | /* Get the "Normal" font. Either the name saved by |
| 4661 | * hl_set_font_name() or from the font ID. */ |
| 4662 | font = gui.norm_font; |
| 4663 | name = hl_get_font_name(); |
| 4664 | } |
| 4665 | else |
| 4666 | { |
| 4667 | name = get_tv_string(&argvars[0]); |
| 4668 | if (STRCMP(name, "*") == 0) /* don't use font dialog */ |
| 4669 | return; |
| 4670 | font = gui_mch_get_font(name, FALSE); |
| 4671 | if (font == NOFONT) |
| 4672 | return; /* Invalid font name, return empty string. */ |
| 4673 | } |
| 4674 | rettv->vval.v_string = gui_mch_get_fontname(font, name); |
| 4675 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 4676 | gui_mch_free_font(font); |
| 4677 | } |
| 4678 | #endif |
| 4679 | } |
| 4680 | |
| 4681 | /* |
| 4682 | * "getfperm({fname})" function |
| 4683 | */ |
| 4684 | static void |
| 4685 | f_getfperm(typval_T *argvars, typval_T *rettv) |
| 4686 | { |
| 4687 | char_u *fname; |
| 4688 | stat_T st; |
| 4689 | char_u *perm = NULL; |
| 4690 | char_u flags[] = "rwx"; |
| 4691 | int i; |
| 4692 | |
| 4693 | fname = get_tv_string(&argvars[0]); |
| 4694 | |
| 4695 | rettv->v_type = VAR_STRING; |
| 4696 | if (mch_stat((char *)fname, &st) >= 0) |
| 4697 | { |
| 4698 | perm = vim_strsave((char_u *)"---------"); |
| 4699 | if (perm != NULL) |
| 4700 | { |
| 4701 | for (i = 0; i < 9; i++) |
| 4702 | { |
| 4703 | if (st.st_mode & (1 << (8 - i))) |
| 4704 | perm[i] = flags[i % 3]; |
| 4705 | } |
| 4706 | } |
| 4707 | } |
| 4708 | rettv->vval.v_string = perm; |
| 4709 | } |
| 4710 | |
| 4711 | /* |
| 4712 | * "getfsize({fname})" function |
| 4713 | */ |
| 4714 | static void |
| 4715 | f_getfsize(typval_T *argvars, typval_T *rettv) |
| 4716 | { |
| 4717 | char_u *fname; |
| 4718 | stat_T st; |
| 4719 | |
| 4720 | fname = get_tv_string(&argvars[0]); |
| 4721 | |
| 4722 | rettv->v_type = VAR_NUMBER; |
| 4723 | |
| 4724 | if (mch_stat((char *)fname, &st) >= 0) |
| 4725 | { |
| 4726 | if (mch_isdir(fname)) |
| 4727 | rettv->vval.v_number = 0; |
| 4728 | else |
| 4729 | { |
| 4730 | rettv->vval.v_number = (varnumber_T)st.st_size; |
| 4731 | |
| 4732 | /* non-perfect check for overflow */ |
| 4733 | if ((off_T)rettv->vval.v_number != (off_T)st.st_size) |
| 4734 | rettv->vval.v_number = -2; |
| 4735 | } |
| 4736 | } |
| 4737 | else |
| 4738 | rettv->vval.v_number = -1; |
| 4739 | } |
| 4740 | |
| 4741 | /* |
| 4742 | * "getftime({fname})" function |
| 4743 | */ |
| 4744 | static void |
| 4745 | f_getftime(typval_T *argvars, typval_T *rettv) |
| 4746 | { |
| 4747 | char_u *fname; |
| 4748 | stat_T st; |
| 4749 | |
| 4750 | fname = get_tv_string(&argvars[0]); |
| 4751 | |
| 4752 | if (mch_stat((char *)fname, &st) >= 0) |
| 4753 | rettv->vval.v_number = (varnumber_T)st.st_mtime; |
| 4754 | else |
| 4755 | rettv->vval.v_number = -1; |
| 4756 | } |
| 4757 | |
| 4758 | /* |
| 4759 | * "getftype({fname})" function |
| 4760 | */ |
| 4761 | static void |
| 4762 | f_getftype(typval_T *argvars, typval_T *rettv) |
| 4763 | { |
| 4764 | char_u *fname; |
| 4765 | stat_T st; |
| 4766 | char_u *type = NULL; |
| 4767 | char *t; |
| 4768 | |
| 4769 | fname = get_tv_string(&argvars[0]); |
| 4770 | |
| 4771 | rettv->v_type = VAR_STRING; |
| 4772 | if (mch_lstat((char *)fname, &st) >= 0) |
| 4773 | { |
| 4774 | #ifdef S_ISREG |
| 4775 | if (S_ISREG(st.st_mode)) |
| 4776 | t = "file"; |
| 4777 | else if (S_ISDIR(st.st_mode)) |
| 4778 | t = "dir"; |
| 4779 | # ifdef S_ISLNK |
| 4780 | else if (S_ISLNK(st.st_mode)) |
| 4781 | t = "link"; |
| 4782 | # endif |
| 4783 | # ifdef S_ISBLK |
| 4784 | else if (S_ISBLK(st.st_mode)) |
| 4785 | t = "bdev"; |
| 4786 | # endif |
| 4787 | # ifdef S_ISCHR |
| 4788 | else if (S_ISCHR(st.st_mode)) |
| 4789 | t = "cdev"; |
| 4790 | # endif |
| 4791 | # ifdef S_ISFIFO |
| 4792 | else if (S_ISFIFO(st.st_mode)) |
| 4793 | t = "fifo"; |
| 4794 | # endif |
| 4795 | # ifdef S_ISSOCK |
| 4796 | else if (S_ISSOCK(st.st_mode)) |
| 4797 | t = "fifo"; |
| 4798 | # endif |
| 4799 | else |
| 4800 | t = "other"; |
| 4801 | #else |
| 4802 | # ifdef S_IFMT |
| 4803 | switch (st.st_mode & S_IFMT) |
| 4804 | { |
| 4805 | case S_IFREG: t = "file"; break; |
| 4806 | case S_IFDIR: t = "dir"; break; |
| 4807 | # ifdef S_IFLNK |
| 4808 | case S_IFLNK: t = "link"; break; |
| 4809 | # endif |
| 4810 | # ifdef S_IFBLK |
| 4811 | case S_IFBLK: t = "bdev"; break; |
| 4812 | # endif |
| 4813 | # ifdef S_IFCHR |
| 4814 | case S_IFCHR: t = "cdev"; break; |
| 4815 | # endif |
| 4816 | # ifdef S_IFIFO |
| 4817 | case S_IFIFO: t = "fifo"; break; |
| 4818 | # endif |
| 4819 | # ifdef S_IFSOCK |
| 4820 | case S_IFSOCK: t = "socket"; break; |
| 4821 | # endif |
| 4822 | default: t = "other"; |
| 4823 | } |
| 4824 | # else |
| 4825 | if (mch_isdir(fname)) |
| 4826 | t = "dir"; |
| 4827 | else |
| 4828 | t = "file"; |
| 4829 | # endif |
| 4830 | #endif |
| 4831 | type = vim_strsave((char_u *)t); |
| 4832 | } |
| 4833 | rettv->vval.v_string = type; |
| 4834 | } |
| 4835 | |
| 4836 | /* |
| 4837 | * "getline(lnum, [end])" function |
| 4838 | */ |
| 4839 | static void |
| 4840 | f_getline(typval_T *argvars, typval_T *rettv) |
| 4841 | { |
| 4842 | linenr_T lnum; |
| 4843 | linenr_T end; |
| 4844 | int retlist; |
| 4845 | |
| 4846 | lnum = get_tv_lnum(argvars); |
| 4847 | if (argvars[1].v_type == VAR_UNKNOWN) |
| 4848 | { |
| 4849 | end = 0; |
| 4850 | retlist = FALSE; |
| 4851 | } |
| 4852 | else |
| 4853 | { |
| 4854 | end = get_tv_lnum(&argvars[1]); |
| 4855 | retlist = TRUE; |
| 4856 | } |
| 4857 | |
| 4858 | get_buffer_lines(curbuf, lnum, end, retlist, rettv); |
| 4859 | } |
| 4860 | |
Bram Moolenaar | 4ae2095 | 2016-08-13 15:29:14 +0200 | [diff] [blame] | 4861 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 4862 | static void |
| 4863 | get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv) |
| 4864 | { |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 4865 | if (what_arg->v_type == VAR_UNKNOWN) |
| 4866 | { |
| 4867 | if (rettv_list_alloc(rettv) == OK) |
| 4868 | if (is_qf || wp != NULL) |
Bram Moolenaar | 7adf06f | 2017-08-27 15:23:41 +0200 | [diff] [blame] | 4869 | (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list); |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 4870 | } |
| 4871 | else |
| 4872 | { |
| 4873 | if (rettv_dict_alloc(rettv) == OK) |
| 4874 | if (is_qf || (wp != NULL)) |
| 4875 | { |
| 4876 | if (what_arg->v_type == VAR_DICT) |
| 4877 | { |
| 4878 | dict_T *d = what_arg->vval.v_dict; |
| 4879 | |
| 4880 | if (d != NULL) |
Bram Moolenaar | b4d5fba | 2017-09-11 19:31:28 +0200 | [diff] [blame] | 4881 | qf_get_properties(wp, d, rettv->vval.v_dict); |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 4882 | } |
| 4883 | else |
| 4884 | EMSG(_(e_dictreq)); |
| 4885 | } |
| 4886 | } |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 4887 | } |
Bram Moolenaar | 4ae2095 | 2016-08-13 15:29:14 +0200 | [diff] [blame] | 4888 | #endif |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 4889 | |
| 4890 | /* |
| 4891 | * "getloclist()" function |
| 4892 | */ |
| 4893 | static void |
| 4894 | f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 4895 | { |
| 4896 | #ifdef FEAT_QUICKFIX |
| 4897 | win_T *wp; |
| 4898 | |
| 4899 | wp = find_win_by_nr(&argvars[0], NULL); |
| 4900 | get_qf_loc_list(FALSE, wp, &argvars[1], rettv); |
| 4901 | #endif |
| 4902 | } |
| 4903 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 4904 | /* |
| 4905 | * "getmatches()" function |
| 4906 | */ |
| 4907 | static void |
| 4908 | f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 4909 | { |
| 4910 | #ifdef FEAT_SEARCH_EXTRA |
| 4911 | dict_T *dict; |
| 4912 | matchitem_T *cur = curwin->w_match_head; |
| 4913 | int i; |
| 4914 | |
| 4915 | if (rettv_list_alloc(rettv) == OK) |
| 4916 | { |
| 4917 | while (cur != NULL) |
| 4918 | { |
| 4919 | dict = dict_alloc(); |
| 4920 | if (dict == NULL) |
| 4921 | return; |
| 4922 | if (cur->match.regprog == NULL) |
| 4923 | { |
| 4924 | /* match added with matchaddpos() */ |
| 4925 | for (i = 0; i < MAXPOSMATCH; ++i) |
| 4926 | { |
| 4927 | llpos_T *llpos; |
| 4928 | char buf[6]; |
| 4929 | list_T *l; |
| 4930 | |
| 4931 | llpos = &cur->pos.pos[i]; |
| 4932 | if (llpos->lnum == 0) |
| 4933 | break; |
| 4934 | l = list_alloc(); |
| 4935 | if (l == NULL) |
| 4936 | break; |
| 4937 | list_append_number(l, (varnumber_T)llpos->lnum); |
| 4938 | if (llpos->col > 0) |
| 4939 | { |
| 4940 | list_append_number(l, (varnumber_T)llpos->col); |
| 4941 | list_append_number(l, (varnumber_T)llpos->len); |
| 4942 | } |
| 4943 | sprintf(buf, "pos%d", i + 1); |
| 4944 | dict_add_list(dict, buf, l); |
| 4945 | } |
| 4946 | } |
| 4947 | else |
| 4948 | { |
| 4949 | dict_add_nr_str(dict, "pattern", 0L, cur->pattern); |
| 4950 | } |
| 4951 | dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id)); |
| 4952 | dict_add_nr_str(dict, "priority", (long)cur->priority, NULL); |
| 4953 | dict_add_nr_str(dict, "id", (long)cur->id, NULL); |
| 4954 | # if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE) |
| 4955 | if (cur->conceal_char) |
| 4956 | { |
| 4957 | char_u buf[MB_MAXBYTES + 1]; |
| 4958 | |
| 4959 | buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL; |
| 4960 | dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf); |
| 4961 | } |
| 4962 | # endif |
| 4963 | list_append_dict(rettv->vval.v_list, dict); |
| 4964 | cur = cur->next; |
| 4965 | } |
| 4966 | } |
| 4967 | #endif |
| 4968 | } |
| 4969 | |
| 4970 | /* |
| 4971 | * "getpid()" function |
| 4972 | */ |
| 4973 | static void |
| 4974 | f_getpid(typval_T *argvars UNUSED, typval_T *rettv) |
| 4975 | { |
| 4976 | rettv->vval.v_number = mch_get_pid(); |
| 4977 | } |
| 4978 | |
| 4979 | static void |
| 4980 | getpos_both( |
| 4981 | typval_T *argvars, |
| 4982 | typval_T *rettv, |
| 4983 | int getcurpos) |
| 4984 | { |
| 4985 | pos_T *fp; |
| 4986 | list_T *l; |
| 4987 | int fnum = -1; |
| 4988 | |
| 4989 | if (rettv_list_alloc(rettv) == OK) |
| 4990 | { |
| 4991 | l = rettv->vval.v_list; |
| 4992 | if (getcurpos) |
| 4993 | fp = &curwin->w_cursor; |
| 4994 | else |
| 4995 | fp = var2fpos(&argvars[0], TRUE, &fnum); |
| 4996 | if (fnum != -1) |
| 4997 | list_append_number(l, (varnumber_T)fnum); |
| 4998 | else |
| 4999 | list_append_number(l, (varnumber_T)0); |
| 5000 | list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum |
| 5001 | : (varnumber_T)0); |
| 5002 | list_append_number(l, (fp != NULL) |
| 5003 | ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1) |
| 5004 | : (varnumber_T)0); |
| 5005 | list_append_number(l, |
| 5006 | #ifdef FEAT_VIRTUALEDIT |
| 5007 | (fp != NULL) ? (varnumber_T)fp->coladd : |
| 5008 | #endif |
| 5009 | (varnumber_T)0); |
| 5010 | if (getcurpos) |
| 5011 | { |
| 5012 | update_curswant(); |
| 5013 | list_append_number(l, curwin->w_curswant == MAXCOL ? |
| 5014 | (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1); |
| 5015 | } |
| 5016 | } |
| 5017 | else |
| 5018 | rettv->vval.v_number = FALSE; |
| 5019 | } |
| 5020 | |
| 5021 | |
| 5022 | /* |
| 5023 | * "getcurpos()" function |
| 5024 | */ |
| 5025 | static void |
| 5026 | f_getcurpos(typval_T *argvars, typval_T *rettv) |
| 5027 | { |
| 5028 | getpos_both(argvars, rettv, TRUE); |
| 5029 | } |
| 5030 | |
| 5031 | /* |
| 5032 | * "getpos(string)" function |
| 5033 | */ |
| 5034 | static void |
| 5035 | f_getpos(typval_T *argvars, typval_T *rettv) |
| 5036 | { |
| 5037 | getpos_both(argvars, rettv, FALSE); |
| 5038 | } |
| 5039 | |
| 5040 | /* |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 5041 | * "getqflist()" function |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5042 | */ |
| 5043 | static void |
| 5044 | f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 5045 | { |
| 5046 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 5047 | get_qf_loc_list(TRUE, NULL, &argvars[0], rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5048 | #endif |
| 5049 | } |
| 5050 | |
| 5051 | /* |
| 5052 | * "getreg()" function |
| 5053 | */ |
| 5054 | static void |
| 5055 | f_getreg(typval_T *argvars, typval_T *rettv) |
| 5056 | { |
| 5057 | char_u *strregname; |
| 5058 | int regname; |
| 5059 | int arg2 = FALSE; |
| 5060 | int return_list = FALSE; |
| 5061 | int error = FALSE; |
| 5062 | |
| 5063 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 5064 | { |
| 5065 | strregname = get_tv_string_chk(&argvars[0]); |
| 5066 | error = strregname == NULL; |
| 5067 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 5068 | { |
| 5069 | arg2 = (int)get_tv_number_chk(&argvars[1], &error); |
| 5070 | if (!error && argvars[2].v_type != VAR_UNKNOWN) |
| 5071 | return_list = (int)get_tv_number_chk(&argvars[2], &error); |
| 5072 | } |
| 5073 | } |
| 5074 | else |
| 5075 | strregname = get_vim_var_str(VV_REG); |
| 5076 | |
| 5077 | if (error) |
| 5078 | return; |
| 5079 | |
| 5080 | regname = (strregname == NULL ? '"' : *strregname); |
| 5081 | if (regname == 0) |
| 5082 | regname = '"'; |
| 5083 | |
| 5084 | if (return_list) |
| 5085 | { |
| 5086 | rettv->v_type = VAR_LIST; |
| 5087 | rettv->vval.v_list = (list_T *)get_reg_contents(regname, |
| 5088 | (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST); |
| 5089 | if (rettv->vval.v_list == NULL) |
| 5090 | (void)rettv_list_alloc(rettv); |
| 5091 | else |
| 5092 | ++rettv->vval.v_list->lv_refcount; |
| 5093 | } |
| 5094 | else |
| 5095 | { |
| 5096 | rettv->v_type = VAR_STRING; |
| 5097 | rettv->vval.v_string = get_reg_contents(regname, |
| 5098 | arg2 ? GREG_EXPR_SRC : 0); |
| 5099 | } |
| 5100 | } |
| 5101 | |
| 5102 | /* |
| 5103 | * "getregtype()" function |
| 5104 | */ |
| 5105 | static void |
| 5106 | f_getregtype(typval_T *argvars, typval_T *rettv) |
| 5107 | { |
| 5108 | char_u *strregname; |
| 5109 | int regname; |
| 5110 | char_u buf[NUMBUFLEN + 2]; |
| 5111 | long reglen = 0; |
| 5112 | |
| 5113 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 5114 | { |
| 5115 | strregname = get_tv_string_chk(&argvars[0]); |
| 5116 | if (strregname == NULL) /* type error; errmsg already given */ |
| 5117 | { |
| 5118 | rettv->v_type = VAR_STRING; |
| 5119 | rettv->vval.v_string = NULL; |
| 5120 | return; |
| 5121 | } |
| 5122 | } |
| 5123 | else |
| 5124 | /* Default to v:register */ |
| 5125 | strregname = get_vim_var_str(VV_REG); |
| 5126 | |
| 5127 | regname = (strregname == NULL ? '"' : *strregname); |
| 5128 | if (regname == 0) |
| 5129 | regname = '"'; |
| 5130 | |
| 5131 | buf[0] = NUL; |
| 5132 | buf[1] = NUL; |
| 5133 | switch (get_reg_type(regname, ®len)) |
| 5134 | { |
| 5135 | case MLINE: buf[0] = 'V'; break; |
| 5136 | case MCHAR: buf[0] = 'v'; break; |
| 5137 | case MBLOCK: |
| 5138 | buf[0] = Ctrl_V; |
| 5139 | sprintf((char *)buf + 1, "%ld", reglen + 1); |
| 5140 | break; |
| 5141 | } |
| 5142 | rettv->v_type = VAR_STRING; |
| 5143 | rettv->vval.v_string = vim_strsave(buf); |
| 5144 | } |
| 5145 | |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5146 | /* |
| 5147 | * Returns information (variables, options, etc.) about a tab page |
| 5148 | * as a dictionary. |
| 5149 | */ |
| 5150 | static dict_T * |
| 5151 | get_tabpage_info(tabpage_T *tp, int tp_idx) |
| 5152 | { |
| 5153 | win_T *wp; |
| 5154 | dict_T *dict; |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5155 | list_T *l; |
| 5156 | |
| 5157 | dict = dict_alloc(); |
| 5158 | if (dict == NULL) |
| 5159 | return NULL; |
| 5160 | |
Bram Moolenaar | 3392883 | 2016-08-18 21:22:04 +0200 | [diff] [blame] | 5161 | dict_add_nr_str(dict, "tabnr", tp_idx, NULL); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5162 | |
| 5163 | l = list_alloc(); |
| 5164 | if (l != NULL) |
| 5165 | { |
| 5166 | for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin; |
| 5167 | wp; wp = wp->w_next) |
| 5168 | list_append_number(l, (varnumber_T)wp->w_id); |
| 5169 | dict_add_list(dict, "windows", l); |
| 5170 | } |
| 5171 | |
Bram Moolenaar | 9f8187c | 2016-08-27 20:34:01 +0200 | [diff] [blame] | 5172 | /* Make a reference to tabpage variables */ |
| 5173 | dict_add_dict(dict, "variables", tp->tp_vars); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5174 | |
| 5175 | return dict; |
| 5176 | } |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5177 | |
| 5178 | /* |
| 5179 | * "gettabinfo()" function |
| 5180 | */ |
| 5181 | static void |
| 5182 | f_gettabinfo(typval_T *argvars, typval_T *rettv) |
| 5183 | { |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5184 | tabpage_T *tp, *tparg = NULL; |
| 5185 | dict_T *d; |
Bram Moolenaar | 386600f | 2016-08-15 22:16:25 +0200 | [diff] [blame] | 5186 | int tpnr = 0; |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5187 | |
| 5188 | if (rettv_list_alloc(rettv) != OK) |
| 5189 | return; |
| 5190 | |
| 5191 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 5192 | { |
| 5193 | /* Information about one tab page */ |
| 5194 | tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL)); |
| 5195 | if (tparg == NULL) |
| 5196 | return; |
| 5197 | } |
| 5198 | |
| 5199 | /* Get information about a specific tab page or all tab pages */ |
Bram Moolenaar | 386600f | 2016-08-15 22:16:25 +0200 | [diff] [blame] | 5200 | FOR_ALL_TABPAGES(tp) |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5201 | { |
Bram Moolenaar | 386600f | 2016-08-15 22:16:25 +0200 | [diff] [blame] | 5202 | tpnr++; |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5203 | if (tparg != NULL && tp != tparg) |
| 5204 | continue; |
| 5205 | d = get_tabpage_info(tp, tpnr); |
| 5206 | if (d != NULL) |
| 5207 | list_append_dict(rettv->vval.v_list, d); |
| 5208 | if (tparg != NULL) |
| 5209 | return; |
| 5210 | } |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5211 | } |
| 5212 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5213 | /* |
| 5214 | * "gettabvar()" function |
| 5215 | */ |
| 5216 | static void |
| 5217 | f_gettabvar(typval_T *argvars, typval_T *rettv) |
| 5218 | { |
| 5219 | win_T *oldcurwin; |
| 5220 | tabpage_T *tp, *oldtabpage; |
| 5221 | dictitem_T *v; |
| 5222 | char_u *varname; |
| 5223 | int done = FALSE; |
| 5224 | |
| 5225 | rettv->v_type = VAR_STRING; |
| 5226 | rettv->vval.v_string = NULL; |
| 5227 | |
| 5228 | varname = get_tv_string_chk(&argvars[1]); |
| 5229 | tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL)); |
| 5230 | if (tp != NULL && varname != NULL) |
| 5231 | { |
| 5232 | /* Set tp to be our tabpage, temporarily. Also set the window to the |
| 5233 | * first window in the tabpage, otherwise the window is not valid. */ |
| 5234 | if (switch_win(&oldcurwin, &oldtabpage, |
Bram Moolenaar | 816968d | 2017-09-29 21:29:18 +0200 | [diff] [blame] | 5235 | tp == curtab || tp->tp_firstwin == NULL ? firstwin |
| 5236 | : tp->tp_firstwin, tp, TRUE) == OK) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5237 | { |
| 5238 | /* look up the variable */ |
| 5239 | /* Let gettabvar({nr}, "") return the "t:" dictionary. */ |
| 5240 | v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE); |
| 5241 | if (v != NULL) |
| 5242 | { |
| 5243 | copy_tv(&v->di_tv, rettv); |
| 5244 | done = TRUE; |
| 5245 | } |
| 5246 | } |
| 5247 | |
| 5248 | /* restore previous notion of curwin */ |
| 5249 | restore_win(oldcurwin, oldtabpage, TRUE); |
| 5250 | } |
| 5251 | |
| 5252 | if (!done && argvars[2].v_type != VAR_UNKNOWN) |
| 5253 | copy_tv(&argvars[2], rettv); |
| 5254 | } |
| 5255 | |
| 5256 | /* |
| 5257 | * "gettabwinvar()" function |
| 5258 | */ |
| 5259 | static void |
| 5260 | f_gettabwinvar(typval_T *argvars, typval_T *rettv) |
| 5261 | { |
| 5262 | getwinvar(argvars, rettv, 1); |
| 5263 | } |
| 5264 | |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5265 | /* |
| 5266 | * Returns information about a window as a dictionary. |
| 5267 | */ |
| 5268 | static dict_T * |
| 5269 | get_win_info(win_T *wp, short tpnr, short winnr) |
| 5270 | { |
| 5271 | dict_T *dict; |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5272 | |
| 5273 | dict = dict_alloc(); |
| 5274 | if (dict == NULL) |
| 5275 | return NULL; |
| 5276 | |
Bram Moolenaar | 3392883 | 2016-08-18 21:22:04 +0200 | [diff] [blame] | 5277 | dict_add_nr_str(dict, "tabnr", tpnr, NULL); |
| 5278 | dict_add_nr_str(dict, "winnr", winnr, NULL); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5279 | dict_add_nr_str(dict, "winid", wp->w_id, NULL); |
| 5280 | dict_add_nr_str(dict, "height", wp->w_height, NULL); |
Bram Moolenaar | 1b9645d | 2017-09-17 23:03:31 +0200 | [diff] [blame] | 5281 | #ifdef FEAT_MENU |
| 5282 | dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL); |
| 5283 | #endif |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5284 | dict_add_nr_str(dict, "width", wp->w_width, NULL); |
Bram Moolenaar | 3392883 | 2016-08-18 21:22:04 +0200 | [diff] [blame] | 5285 | dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5286 | |
Bram Moolenaar | 69905d1 | 2017-08-13 18:14:47 +0200 | [diff] [blame] | 5287 | #ifdef FEAT_TERMINAL |
| 5288 | dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL); |
| 5289 | #endif |
Bram Moolenaar | 386600f | 2016-08-15 22:16:25 +0200 | [diff] [blame] | 5290 | #ifdef FEAT_QUICKFIX |
| 5291 | dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL); |
| 5292 | dict_add_nr_str(dict, "loclist", |
| 5293 | (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL); |
| 5294 | #endif |
| 5295 | |
Bram Moolenaar | 3056735 | 2016-08-27 21:25:44 +0200 | [diff] [blame] | 5296 | /* Add a reference to window variables */ |
Bram Moolenaar | 9f8187c | 2016-08-27 20:34:01 +0200 | [diff] [blame] | 5297 | dict_add_dict(dict, "variables", wp->w_vars); |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5298 | |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5299 | return dict; |
| 5300 | } |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5301 | |
| 5302 | /* |
| 5303 | * "getwininfo()" function |
| 5304 | */ |
| 5305 | static void |
| 5306 | f_getwininfo(typval_T *argvars, typval_T *rettv) |
| 5307 | { |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5308 | tabpage_T *tp; |
| 5309 | win_T *wp = NULL, *wparg = NULL; |
| 5310 | dict_T *d; |
Bram Moolenaar | 386600f | 2016-08-15 22:16:25 +0200 | [diff] [blame] | 5311 | short tabnr = 0, winnr; |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5312 | |
| 5313 | if (rettv_list_alloc(rettv) != OK) |
| 5314 | return; |
| 5315 | |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5316 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 5317 | { |
| 5318 | wparg = win_id2wp(argvars); |
| 5319 | if (wparg == NULL) |
| 5320 | return; |
| 5321 | } |
| 5322 | |
| 5323 | /* Collect information about either all the windows across all the tab |
| 5324 | * pages or one particular window. |
| 5325 | */ |
Bram Moolenaar | 386600f | 2016-08-15 22:16:25 +0200 | [diff] [blame] | 5326 | FOR_ALL_TABPAGES(tp) |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5327 | { |
Bram Moolenaar | 386600f | 2016-08-15 22:16:25 +0200 | [diff] [blame] | 5328 | tabnr++; |
| 5329 | winnr = 0; |
| 5330 | FOR_ALL_WINDOWS_IN_TAB(tp, wp) |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5331 | { |
Bram Moolenaar | 386600f | 2016-08-15 22:16:25 +0200 | [diff] [blame] | 5332 | winnr++; |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5333 | if (wparg != NULL && wp != wparg) |
| 5334 | continue; |
| 5335 | d = get_win_info(wp, tabnr, winnr); |
| 5336 | if (d != NULL) |
| 5337 | list_append_dict(rettv->vval.v_list, d); |
| 5338 | if (wparg != NULL) |
| 5339 | /* found information about a specific window */ |
| 5340 | return; |
| 5341 | } |
| 5342 | } |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 5343 | } |
| 5344 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5345 | /* |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5346 | * "win_findbuf()" function |
| 5347 | */ |
| 5348 | static void |
| 5349 | f_win_findbuf(typval_T *argvars, typval_T *rettv) |
| 5350 | { |
| 5351 | if (rettv_list_alloc(rettv) != FAIL) |
| 5352 | win_findbuf(argvars, rettv->vval.v_list); |
| 5353 | } |
| 5354 | |
| 5355 | /* |
| 5356 | * "win_getid()" function |
| 5357 | */ |
| 5358 | static void |
| 5359 | f_win_getid(typval_T *argvars, typval_T *rettv) |
| 5360 | { |
| 5361 | rettv->vval.v_number = win_getid(argvars); |
| 5362 | } |
| 5363 | |
| 5364 | /* |
| 5365 | * "win_gotoid()" function |
| 5366 | */ |
| 5367 | static void |
| 5368 | f_win_gotoid(typval_T *argvars, typval_T *rettv) |
| 5369 | { |
| 5370 | rettv->vval.v_number = win_gotoid(argvars); |
| 5371 | } |
| 5372 | |
| 5373 | /* |
| 5374 | * "win_id2tabwin()" function |
| 5375 | */ |
| 5376 | static void |
| 5377 | f_win_id2tabwin(typval_T *argvars, typval_T *rettv) |
| 5378 | { |
| 5379 | if (rettv_list_alloc(rettv) != FAIL) |
| 5380 | win_id2tabwin(argvars, rettv->vval.v_list); |
| 5381 | } |
| 5382 | |
| 5383 | /* |
| 5384 | * "win_id2win()" function |
| 5385 | */ |
| 5386 | static void |
| 5387 | f_win_id2win(typval_T *argvars, typval_T *rettv) |
| 5388 | { |
| 5389 | rettv->vval.v_number = win_id2win(argvars); |
| 5390 | } |
| 5391 | |
| 5392 | /* |
Bram Moolenaar | 22044dc | 2017-12-02 15:43:37 +0100 | [diff] [blame] | 5393 | * "win_screenpos()" function |
| 5394 | */ |
| 5395 | static void |
| 5396 | f_win_screenpos(typval_T *argvars, typval_T *rettv) |
| 5397 | { |
| 5398 | win_T *wp; |
| 5399 | |
| 5400 | if (rettv_list_alloc(rettv) == FAIL) |
| 5401 | return; |
| 5402 | |
| 5403 | wp = find_win_by_nr(&argvars[0], NULL); |
| 5404 | list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1); |
| 5405 | list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1); |
| 5406 | } |
| 5407 | |
| 5408 | /* |
Bram Moolenaar | ba6ec18 | 2017-04-04 22:41:10 +0200 | [diff] [blame] | 5409 | * "getwinposx()" function |
| 5410 | */ |
| 5411 | static void |
| 5412 | f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv) |
| 5413 | { |
| 5414 | rettv->vval.v_number = -1; |
| 5415 | #ifdef FEAT_GUI |
| 5416 | if (gui.in_use) |
| 5417 | { |
| 5418 | int x, y; |
| 5419 | |
| 5420 | if (gui_mch_get_winpos(&x, &y) == OK) |
| 5421 | rettv->vval.v_number = x; |
Bram Moolenaar | 7860bac | 2017-04-09 15:03:15 +0200 | [diff] [blame] | 5422 | return; |
Bram Moolenaar | ba6ec18 | 2017-04-04 22:41:10 +0200 | [diff] [blame] | 5423 | } |
| 5424 | #endif |
| 5425 | #if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE) |
| 5426 | { |
| 5427 | int x, y; |
| 5428 | |
| 5429 | if (term_get_winpos(&x, &y) == OK) |
| 5430 | rettv->vval.v_number = x; |
| 5431 | } |
| 5432 | #endif |
| 5433 | } |
| 5434 | |
| 5435 | /* |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5436 | * "getwinposy()" function |
| 5437 | */ |
| 5438 | static void |
| 5439 | f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv) |
| 5440 | { |
| 5441 | rettv->vval.v_number = -1; |
| 5442 | #ifdef FEAT_GUI |
| 5443 | if (gui.in_use) |
| 5444 | { |
| 5445 | int x, y; |
| 5446 | |
| 5447 | if (gui_mch_get_winpos(&x, &y) == OK) |
| 5448 | rettv->vval.v_number = y; |
Bram Moolenaar | 7860bac | 2017-04-09 15:03:15 +0200 | [diff] [blame] | 5449 | return; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5450 | } |
| 5451 | #endif |
Bram Moolenaar | ba6ec18 | 2017-04-04 22:41:10 +0200 | [diff] [blame] | 5452 | #if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE) |
| 5453 | { |
| 5454 | int x, y; |
| 5455 | |
| 5456 | if (term_get_winpos(&x, &y) == OK) |
| 5457 | rettv->vval.v_number = y; |
| 5458 | } |
| 5459 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5460 | } |
| 5461 | |
| 5462 | /* |
| 5463 | * "getwinvar()" function |
| 5464 | */ |
| 5465 | static void |
| 5466 | f_getwinvar(typval_T *argvars, typval_T *rettv) |
| 5467 | { |
| 5468 | getwinvar(argvars, rettv, 0); |
| 5469 | } |
| 5470 | |
| 5471 | /* |
| 5472 | * "glob()" function |
| 5473 | */ |
| 5474 | static void |
| 5475 | f_glob(typval_T *argvars, typval_T *rettv) |
| 5476 | { |
| 5477 | int options = WILD_SILENT|WILD_USE_NL; |
| 5478 | expand_T xpc; |
| 5479 | int error = FALSE; |
| 5480 | |
| 5481 | /* When the optional second argument is non-zero, don't remove matches |
| 5482 | * for 'wildignore' and don't put matches for 'suffixes' at the end. */ |
| 5483 | rettv->v_type = VAR_STRING; |
| 5484 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 5485 | { |
| 5486 | if (get_tv_number_chk(&argvars[1], &error)) |
| 5487 | options |= WILD_KEEP_ALL; |
| 5488 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 5489 | { |
| 5490 | if (get_tv_number_chk(&argvars[2], &error)) |
| 5491 | { |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 5492 | rettv_list_set(rettv, NULL); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5493 | } |
| 5494 | if (argvars[3].v_type != VAR_UNKNOWN |
| 5495 | && get_tv_number_chk(&argvars[3], &error)) |
| 5496 | options |= WILD_ALLLINKS; |
| 5497 | } |
| 5498 | } |
| 5499 | if (!error) |
| 5500 | { |
| 5501 | ExpandInit(&xpc); |
| 5502 | xpc.xp_context = EXPAND_FILES; |
| 5503 | if (p_wic) |
| 5504 | options += WILD_ICASE; |
| 5505 | if (rettv->v_type == VAR_STRING) |
| 5506 | rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]), |
| 5507 | NULL, options, WILD_ALL); |
| 5508 | else if (rettv_list_alloc(rettv) != FAIL) |
| 5509 | { |
| 5510 | int i; |
| 5511 | |
| 5512 | ExpandOne(&xpc, get_tv_string(&argvars[0]), |
| 5513 | NULL, options, WILD_ALL_KEEP); |
| 5514 | for (i = 0; i < xpc.xp_numfiles; i++) |
| 5515 | list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1); |
| 5516 | |
| 5517 | ExpandCleanup(&xpc); |
| 5518 | } |
| 5519 | } |
| 5520 | else |
| 5521 | rettv->vval.v_string = NULL; |
| 5522 | } |
| 5523 | |
| 5524 | /* |
| 5525 | * "globpath()" function |
| 5526 | */ |
| 5527 | static void |
| 5528 | f_globpath(typval_T *argvars, typval_T *rettv) |
| 5529 | { |
| 5530 | int flags = 0; |
| 5531 | char_u buf1[NUMBUFLEN]; |
| 5532 | char_u *file = get_tv_string_buf_chk(&argvars[1], buf1); |
| 5533 | int error = FALSE; |
| 5534 | garray_T ga; |
| 5535 | int i; |
| 5536 | |
| 5537 | /* When the optional second argument is non-zero, don't remove matches |
| 5538 | * for 'wildignore' and don't put matches for 'suffixes' at the end. */ |
| 5539 | rettv->v_type = VAR_STRING; |
| 5540 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 5541 | { |
| 5542 | if (get_tv_number_chk(&argvars[2], &error)) |
| 5543 | flags |= WILD_KEEP_ALL; |
| 5544 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 5545 | { |
| 5546 | if (get_tv_number_chk(&argvars[3], &error)) |
| 5547 | { |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 5548 | rettv_list_set(rettv, NULL); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5549 | } |
| 5550 | if (argvars[4].v_type != VAR_UNKNOWN |
| 5551 | && get_tv_number_chk(&argvars[4], &error)) |
| 5552 | flags |= WILD_ALLLINKS; |
| 5553 | } |
| 5554 | } |
| 5555 | if (file != NULL && !error) |
| 5556 | { |
| 5557 | ga_init2(&ga, (int)sizeof(char_u *), 10); |
| 5558 | globpath(get_tv_string(&argvars[0]), file, &ga, flags); |
| 5559 | if (rettv->v_type == VAR_STRING) |
| 5560 | rettv->vval.v_string = ga_concat_strings(&ga, "\n"); |
| 5561 | else if (rettv_list_alloc(rettv) != FAIL) |
| 5562 | for (i = 0; i < ga.ga_len; ++i) |
| 5563 | list_append_string(rettv->vval.v_list, |
| 5564 | ((char_u **)(ga.ga_data))[i], -1); |
| 5565 | ga_clear_strings(&ga); |
| 5566 | } |
| 5567 | else |
| 5568 | rettv->vval.v_string = NULL; |
| 5569 | } |
| 5570 | |
| 5571 | /* |
| 5572 | * "glob2regpat()" function |
| 5573 | */ |
| 5574 | static void |
| 5575 | f_glob2regpat(typval_T *argvars, typval_T *rettv) |
| 5576 | { |
| 5577 | char_u *pat = get_tv_string_chk(&argvars[0]); |
| 5578 | |
| 5579 | rettv->v_type = VAR_STRING; |
| 5580 | rettv->vval.v_string = (pat == NULL) |
| 5581 | ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE); |
| 5582 | } |
| 5583 | |
| 5584 | /* for VIM_VERSION_ defines */ |
| 5585 | #include "version.h" |
| 5586 | |
| 5587 | /* |
| 5588 | * "has()" function |
| 5589 | */ |
| 5590 | static void |
| 5591 | f_has(typval_T *argvars, typval_T *rettv) |
| 5592 | { |
| 5593 | int i; |
| 5594 | char_u *name; |
| 5595 | int n = FALSE; |
| 5596 | static char *(has_list[]) = |
| 5597 | { |
| 5598 | #ifdef AMIGA |
| 5599 | "amiga", |
| 5600 | # ifdef FEAT_ARP |
| 5601 | "arp", |
| 5602 | # endif |
| 5603 | #endif |
| 5604 | #ifdef __BEOS__ |
| 5605 | "beos", |
| 5606 | #endif |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 5607 | #ifdef MACOS_X |
| 5608 | "mac", /* Mac OS X (and, once, Mac OS Classic) */ |
| 5609 | "osx", /* Mac OS X */ |
| 5610 | # ifdef MACOS_X_DARWIN |
| 5611 | "macunix", /* Mac OS X, with the darwin feature */ |
| 5612 | "osxdarwin", /* synonym for macunix */ |
| 5613 | # endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5614 | #endif |
| 5615 | #ifdef __QNX__ |
| 5616 | "qnx", |
| 5617 | #endif |
| 5618 | #ifdef UNIX |
| 5619 | "unix", |
| 5620 | #endif |
| 5621 | #ifdef VMS |
| 5622 | "vms", |
| 5623 | #endif |
| 5624 | #ifdef WIN32 |
| 5625 | "win32", |
| 5626 | #endif |
| 5627 | #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__)) |
| 5628 | "win32unix", |
| 5629 | #endif |
| 5630 | #if defined(WIN64) || defined(_WIN64) |
| 5631 | "win64", |
| 5632 | #endif |
| 5633 | #ifdef EBCDIC |
| 5634 | "ebcdic", |
| 5635 | #endif |
| 5636 | #ifndef CASE_INSENSITIVE_FILENAME |
| 5637 | "fname_case", |
| 5638 | #endif |
| 5639 | #ifdef HAVE_ACL |
| 5640 | "acl", |
| 5641 | #endif |
| 5642 | #ifdef FEAT_ARABIC |
| 5643 | "arabic", |
| 5644 | #endif |
| 5645 | #ifdef FEAT_AUTOCMD |
| 5646 | "autocmd", |
| 5647 | #endif |
Bram Moolenaar | e42a6d2 | 2017-11-12 19:21:51 +0100 | [diff] [blame] | 5648 | #ifdef FEAT_AUTOSERVERNAME |
| 5649 | "autoservername", |
| 5650 | #endif |
Bram Moolenaar | c3719bd | 2017-11-18 22:13:31 +0100 | [diff] [blame] | 5651 | #ifdef FEAT_BEVAL_GUI |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5652 | "balloon_eval", |
| 5653 | # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */ |
| 5654 | "balloon_multiline", |
| 5655 | # endif |
| 5656 | #endif |
Bram Moolenaar | c3719bd | 2017-11-18 22:13:31 +0100 | [diff] [blame] | 5657 | #ifdef FEAT_BEVAL_TERM |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 5658 | "balloon_eval_term", |
| 5659 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5660 | #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS) |
| 5661 | "builtin_terms", |
| 5662 | # ifdef ALL_BUILTIN_TCAPS |
| 5663 | "all_builtin_terms", |
| 5664 | # endif |
| 5665 | #endif |
| 5666 | #if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \ |
| 5667 | || defined(FEAT_GUI_W32) \ |
| 5668 | || defined(FEAT_GUI_MOTIF)) |
| 5669 | "browsefilter", |
| 5670 | #endif |
| 5671 | #ifdef FEAT_BYTEOFF |
| 5672 | "byte_offset", |
| 5673 | #endif |
| 5674 | #ifdef FEAT_JOB_CHANNEL |
| 5675 | "channel", |
| 5676 | #endif |
| 5677 | #ifdef FEAT_CINDENT |
| 5678 | "cindent", |
| 5679 | #endif |
| 5680 | #ifdef FEAT_CLIENTSERVER |
| 5681 | "clientserver", |
| 5682 | #endif |
| 5683 | #ifdef FEAT_CLIPBOARD |
| 5684 | "clipboard", |
| 5685 | #endif |
| 5686 | #ifdef FEAT_CMDL_COMPL |
| 5687 | "cmdline_compl", |
| 5688 | #endif |
| 5689 | #ifdef FEAT_CMDHIST |
| 5690 | "cmdline_hist", |
| 5691 | #endif |
| 5692 | #ifdef FEAT_COMMENTS |
| 5693 | "comments", |
| 5694 | #endif |
| 5695 | #ifdef FEAT_CONCEAL |
| 5696 | "conceal", |
| 5697 | #endif |
| 5698 | #ifdef FEAT_CRYPT |
| 5699 | "cryptv", |
| 5700 | "crypt-blowfish", |
| 5701 | "crypt-blowfish2", |
| 5702 | #endif |
| 5703 | #ifdef FEAT_CSCOPE |
| 5704 | "cscope", |
| 5705 | #endif |
| 5706 | #ifdef FEAT_CURSORBIND |
| 5707 | "cursorbind", |
| 5708 | #endif |
| 5709 | #ifdef CURSOR_SHAPE |
| 5710 | "cursorshape", |
| 5711 | #endif |
| 5712 | #ifdef DEBUG |
| 5713 | "debug", |
| 5714 | #endif |
| 5715 | #ifdef FEAT_CON_DIALOG |
| 5716 | "dialog_con", |
| 5717 | #endif |
| 5718 | #ifdef FEAT_GUI_DIALOG |
| 5719 | "dialog_gui", |
| 5720 | #endif |
| 5721 | #ifdef FEAT_DIFF |
| 5722 | "diff", |
| 5723 | #endif |
| 5724 | #ifdef FEAT_DIGRAPHS |
| 5725 | "digraphs", |
| 5726 | #endif |
| 5727 | #ifdef FEAT_DIRECTX |
| 5728 | "directx", |
| 5729 | #endif |
| 5730 | #ifdef FEAT_DND |
| 5731 | "dnd", |
| 5732 | #endif |
| 5733 | #ifdef FEAT_EMACS_TAGS |
| 5734 | "emacs_tags", |
| 5735 | #endif |
| 5736 | "eval", /* always present, of course! */ |
| 5737 | "ex_extra", /* graduated feature */ |
| 5738 | #ifdef FEAT_SEARCH_EXTRA |
| 5739 | "extra_search", |
| 5740 | #endif |
| 5741 | #ifdef FEAT_FKMAP |
| 5742 | "farsi", |
| 5743 | #endif |
| 5744 | #ifdef FEAT_SEARCHPATH |
| 5745 | "file_in_path", |
| 5746 | #endif |
| 5747 | #ifdef FEAT_FILTERPIPE |
| 5748 | "filterpipe", |
| 5749 | #endif |
| 5750 | #ifdef FEAT_FIND_ID |
| 5751 | "find_in_path", |
| 5752 | #endif |
| 5753 | #ifdef FEAT_FLOAT |
| 5754 | "float", |
| 5755 | #endif |
| 5756 | #ifdef FEAT_FOLDING |
| 5757 | "folding", |
| 5758 | #endif |
| 5759 | #ifdef FEAT_FOOTER |
| 5760 | "footer", |
| 5761 | #endif |
| 5762 | #if !defined(USE_SYSTEM) && defined(UNIX) |
| 5763 | "fork", |
| 5764 | #endif |
| 5765 | #ifdef FEAT_GETTEXT |
| 5766 | "gettext", |
| 5767 | #endif |
| 5768 | #ifdef FEAT_GUI |
| 5769 | "gui", |
| 5770 | #endif |
| 5771 | #ifdef FEAT_GUI_ATHENA |
| 5772 | # ifdef FEAT_GUI_NEXTAW |
| 5773 | "gui_neXtaw", |
| 5774 | # else |
| 5775 | "gui_athena", |
| 5776 | # endif |
| 5777 | #endif |
| 5778 | #ifdef FEAT_GUI_GTK |
| 5779 | "gui_gtk", |
| 5780 | # ifdef USE_GTK3 |
| 5781 | "gui_gtk3", |
| 5782 | # else |
| 5783 | "gui_gtk2", |
| 5784 | # endif |
| 5785 | #endif |
| 5786 | #ifdef FEAT_GUI_GNOME |
| 5787 | "gui_gnome", |
| 5788 | #endif |
| 5789 | #ifdef FEAT_GUI_MAC |
| 5790 | "gui_mac", |
| 5791 | #endif |
| 5792 | #ifdef FEAT_GUI_MOTIF |
| 5793 | "gui_motif", |
| 5794 | #endif |
| 5795 | #ifdef FEAT_GUI_PHOTON |
| 5796 | "gui_photon", |
| 5797 | #endif |
| 5798 | #ifdef FEAT_GUI_W32 |
| 5799 | "gui_win32", |
| 5800 | #endif |
| 5801 | #ifdef FEAT_HANGULIN |
| 5802 | "hangul_input", |
| 5803 | #endif |
| 5804 | #if defined(HAVE_ICONV_H) && defined(USE_ICONV) |
| 5805 | "iconv", |
| 5806 | #endif |
| 5807 | #ifdef FEAT_INS_EXPAND |
| 5808 | "insert_expand", |
| 5809 | #endif |
| 5810 | #ifdef FEAT_JOB_CHANNEL |
| 5811 | "job", |
| 5812 | #endif |
| 5813 | #ifdef FEAT_JUMPLIST |
| 5814 | "jumplist", |
| 5815 | #endif |
| 5816 | #ifdef FEAT_KEYMAP |
| 5817 | "keymap", |
| 5818 | #endif |
Bram Moolenaar | 9532fe7 | 2016-07-29 22:50:35 +0200 | [diff] [blame] | 5819 | "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */ |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5820 | #ifdef FEAT_LANGMAP |
| 5821 | "langmap", |
| 5822 | #endif |
| 5823 | #ifdef FEAT_LIBCALL |
| 5824 | "libcall", |
| 5825 | #endif |
| 5826 | #ifdef FEAT_LINEBREAK |
| 5827 | "linebreak", |
| 5828 | #endif |
| 5829 | #ifdef FEAT_LISP |
| 5830 | "lispindent", |
| 5831 | #endif |
| 5832 | #ifdef FEAT_LISTCMDS |
| 5833 | "listcmds", |
| 5834 | #endif |
| 5835 | #ifdef FEAT_LOCALMAP |
| 5836 | "localmap", |
| 5837 | #endif |
| 5838 | #ifdef FEAT_LUA |
| 5839 | # ifndef DYNAMIC_LUA |
| 5840 | "lua", |
| 5841 | # endif |
| 5842 | #endif |
| 5843 | #ifdef FEAT_MENU |
| 5844 | "menu", |
| 5845 | #endif |
| 5846 | #ifdef FEAT_SESSION |
| 5847 | "mksession", |
| 5848 | #endif |
| 5849 | #ifdef FEAT_MODIFY_FNAME |
| 5850 | "modify_fname", |
| 5851 | #endif |
| 5852 | #ifdef FEAT_MOUSE |
| 5853 | "mouse", |
| 5854 | #endif |
| 5855 | #ifdef FEAT_MOUSESHAPE |
| 5856 | "mouseshape", |
| 5857 | #endif |
| 5858 | #if defined(UNIX) || defined(VMS) |
| 5859 | # ifdef FEAT_MOUSE_DEC |
| 5860 | "mouse_dec", |
| 5861 | # endif |
| 5862 | # ifdef FEAT_MOUSE_GPM |
| 5863 | "mouse_gpm", |
| 5864 | # endif |
| 5865 | # ifdef FEAT_MOUSE_JSB |
| 5866 | "mouse_jsbterm", |
| 5867 | # endif |
| 5868 | # ifdef FEAT_MOUSE_NET |
| 5869 | "mouse_netterm", |
| 5870 | # endif |
| 5871 | # ifdef FEAT_MOUSE_PTERM |
| 5872 | "mouse_pterm", |
| 5873 | # endif |
| 5874 | # ifdef FEAT_MOUSE_SGR |
| 5875 | "mouse_sgr", |
| 5876 | # endif |
| 5877 | # ifdef FEAT_SYSMOUSE |
| 5878 | "mouse_sysmouse", |
| 5879 | # endif |
| 5880 | # ifdef FEAT_MOUSE_URXVT |
| 5881 | "mouse_urxvt", |
| 5882 | # endif |
| 5883 | # ifdef FEAT_MOUSE_XTERM |
| 5884 | "mouse_xterm", |
| 5885 | # endif |
| 5886 | #endif |
| 5887 | #ifdef FEAT_MBYTE |
| 5888 | "multi_byte", |
| 5889 | #endif |
| 5890 | #ifdef FEAT_MBYTE_IME |
| 5891 | "multi_byte_ime", |
| 5892 | #endif |
| 5893 | #ifdef FEAT_MULTI_LANG |
| 5894 | "multi_lang", |
| 5895 | #endif |
| 5896 | #ifdef FEAT_MZSCHEME |
| 5897 | #ifndef DYNAMIC_MZSCHEME |
| 5898 | "mzscheme", |
| 5899 | #endif |
| 5900 | #endif |
| 5901 | #ifdef FEAT_NUM64 |
| 5902 | "num64", |
| 5903 | #endif |
| 5904 | #ifdef FEAT_OLE |
| 5905 | "ole", |
| 5906 | #endif |
| 5907 | "packages", |
| 5908 | #ifdef FEAT_PATH_EXTRA |
| 5909 | "path_extra", |
| 5910 | #endif |
| 5911 | #ifdef FEAT_PERL |
| 5912 | #ifndef DYNAMIC_PERL |
| 5913 | "perl", |
| 5914 | #endif |
| 5915 | #endif |
| 5916 | #ifdef FEAT_PERSISTENT_UNDO |
| 5917 | "persistent_undo", |
| 5918 | #endif |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 5919 | #if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5920 | "python", |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 5921 | "pythonx", |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5922 | #endif |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 5923 | #if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5924 | "python3", |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 5925 | "pythonx", |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 5926 | #endif |
| 5927 | #ifdef FEAT_POSTSCRIPT |
| 5928 | "postscript", |
| 5929 | #endif |
| 5930 | #ifdef FEAT_PRINTER |
| 5931 | "printer", |
| 5932 | #endif |
| 5933 | #ifdef FEAT_PROFILE |
| 5934 | "profile", |
| 5935 | #endif |
| 5936 | #ifdef FEAT_RELTIME |
| 5937 | "reltime", |
| 5938 | #endif |
| 5939 | #ifdef FEAT_QUICKFIX |
| 5940 | "quickfix", |
| 5941 | #endif |
| 5942 | #ifdef FEAT_RIGHTLEFT |
| 5943 | "rightleft", |
| 5944 | #endif |
| 5945 | #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY) |
| 5946 | "ruby", |
| 5947 | #endif |
| 5948 | #ifdef FEAT_SCROLLBIND |
| 5949 | "scrollbind", |
| 5950 | #endif |
| 5951 | #ifdef FEAT_CMDL_INFO |
| 5952 | "showcmd", |
| 5953 | "cmdline_info", |
| 5954 | #endif |
| 5955 | #ifdef FEAT_SIGNS |
| 5956 | "signs", |
| 5957 | #endif |
| 5958 | #ifdef FEAT_SMARTINDENT |
| 5959 | "smartindent", |
| 5960 | #endif |
| 5961 | #ifdef STARTUPTIME |
| 5962 | "startuptime", |
| 5963 | #endif |
| 5964 | #ifdef FEAT_STL_OPT |
| 5965 | "statusline", |
| 5966 | #endif |
| 5967 | #ifdef FEAT_SUN_WORKSHOP |
| 5968 | "sun_workshop", |
| 5969 | #endif |
| 5970 | #ifdef FEAT_NETBEANS_INTG |
| 5971 | "netbeans_intg", |
| 5972 | #endif |
| 5973 | #ifdef FEAT_SPELL |
| 5974 | "spell", |
| 5975 | #endif |
| 5976 | #ifdef FEAT_SYN_HL |
| 5977 | "syntax", |
| 5978 | #endif |
| 5979 | #if defined(USE_SYSTEM) || !defined(UNIX) |
| 5980 | "system", |
| 5981 | #endif |
| 5982 | #ifdef FEAT_TAG_BINS |
| 5983 | "tag_binary", |
| 5984 | #endif |
| 5985 | #ifdef FEAT_TAG_OLDSTATIC |
| 5986 | "tag_old_static", |
| 5987 | #endif |
| 5988 | #ifdef FEAT_TAG_ANYWHITE |
| 5989 | "tag_any_white", |
| 5990 | #endif |
| 5991 | #ifdef FEAT_TCL |
| 5992 | # ifndef DYNAMIC_TCL |
| 5993 | "tcl", |
| 5994 | # endif |
| 5995 | #endif |
| 5996 | #ifdef FEAT_TERMGUICOLORS |
| 5997 | "termguicolors", |
| 5998 | #endif |
Bram Moolenaar | a83e396 | 2017-08-17 14:39:07 +0200 | [diff] [blame] | 5999 | #if defined(FEAT_TERMINAL) && !defined(WIN3264) |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 6000 | "terminal", |
| 6001 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6002 | #ifdef TERMINFO |
| 6003 | "terminfo", |
| 6004 | #endif |
| 6005 | #ifdef FEAT_TERMRESPONSE |
| 6006 | "termresponse", |
| 6007 | #endif |
| 6008 | #ifdef FEAT_TEXTOBJ |
| 6009 | "textobjects", |
| 6010 | #endif |
| 6011 | #ifdef HAVE_TGETENT |
| 6012 | "tgetent", |
| 6013 | #endif |
| 6014 | #ifdef FEAT_TIMERS |
| 6015 | "timers", |
| 6016 | #endif |
| 6017 | #ifdef FEAT_TITLE |
| 6018 | "title", |
| 6019 | #endif |
| 6020 | #ifdef FEAT_TOOLBAR |
| 6021 | "toolbar", |
| 6022 | #endif |
| 6023 | #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
| 6024 | "unnamedplus", |
| 6025 | #endif |
| 6026 | #ifdef FEAT_USR_CMDS |
| 6027 | "user-commands", /* was accidentally included in 5.4 */ |
| 6028 | "user_commands", |
| 6029 | #endif |
| 6030 | #ifdef FEAT_VIMINFO |
| 6031 | "viminfo", |
| 6032 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6033 | "vertsplit", |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6034 | #ifdef FEAT_VIRTUALEDIT |
| 6035 | "virtualedit", |
| 6036 | #endif |
| 6037 | "visual", |
| 6038 | #ifdef FEAT_VISUALEXTRA |
| 6039 | "visualextra", |
| 6040 | #endif |
| 6041 | #ifdef FEAT_VREPLACE |
| 6042 | "vreplace", |
| 6043 | #endif |
| 6044 | #ifdef FEAT_WILDIGN |
| 6045 | "wildignore", |
| 6046 | #endif |
| 6047 | #ifdef FEAT_WILDMENU |
| 6048 | "wildmenu", |
| 6049 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6050 | "windows", |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6051 | #ifdef FEAT_WAK |
| 6052 | "winaltkeys", |
| 6053 | #endif |
| 6054 | #ifdef FEAT_WRITEBACKUP |
| 6055 | "writebackup", |
| 6056 | #endif |
| 6057 | #ifdef FEAT_XIM |
| 6058 | "xim", |
| 6059 | #endif |
| 6060 | #ifdef FEAT_XFONTSET |
| 6061 | "xfontset", |
| 6062 | #endif |
| 6063 | #ifdef FEAT_XPM_W32 |
| 6064 | "xpm", |
| 6065 | "xpm_w32", /* for backward compatibility */ |
| 6066 | #else |
| 6067 | # if defined(HAVE_XPM) |
| 6068 | "xpm", |
| 6069 | # endif |
| 6070 | #endif |
| 6071 | #ifdef USE_XSMP |
| 6072 | "xsmp", |
| 6073 | #endif |
| 6074 | #ifdef USE_XSMP_INTERACT |
| 6075 | "xsmp_interact", |
| 6076 | #endif |
| 6077 | #ifdef FEAT_XCLIPBOARD |
| 6078 | "xterm_clipboard", |
| 6079 | #endif |
| 6080 | #ifdef FEAT_XTERM_SAVE |
| 6081 | "xterm_save", |
| 6082 | #endif |
| 6083 | #if defined(UNIX) && defined(FEAT_X11) |
| 6084 | "X11", |
| 6085 | #endif |
| 6086 | NULL |
| 6087 | }; |
| 6088 | |
| 6089 | name = get_tv_string(&argvars[0]); |
| 6090 | for (i = 0; has_list[i] != NULL; ++i) |
| 6091 | if (STRICMP(name, has_list[i]) == 0) |
| 6092 | { |
| 6093 | n = TRUE; |
| 6094 | break; |
| 6095 | } |
| 6096 | |
| 6097 | if (n == FALSE) |
| 6098 | { |
| 6099 | if (STRNICMP(name, "patch", 5) == 0) |
| 6100 | { |
| 6101 | if (name[5] == '-' |
| 6102 | && STRLEN(name) >= 11 |
| 6103 | && vim_isdigit(name[6]) |
| 6104 | && vim_isdigit(name[8]) |
| 6105 | && vim_isdigit(name[10])) |
| 6106 | { |
| 6107 | int major = atoi((char *)name + 6); |
| 6108 | int minor = atoi((char *)name + 8); |
| 6109 | |
| 6110 | /* Expect "patch-9.9.01234". */ |
| 6111 | n = (major < VIM_VERSION_MAJOR |
| 6112 | || (major == VIM_VERSION_MAJOR |
| 6113 | && (minor < VIM_VERSION_MINOR |
| 6114 | || (minor == VIM_VERSION_MINOR |
| 6115 | && has_patch(atoi((char *)name + 10)))))); |
| 6116 | } |
| 6117 | else |
| 6118 | n = has_patch(atoi((char *)name + 5)); |
| 6119 | } |
| 6120 | else if (STRICMP(name, "vim_starting") == 0) |
| 6121 | n = (starting != 0); |
Bram Moolenaar | 2cab0e1 | 2016-11-24 15:09:07 +0100 | [diff] [blame] | 6122 | else if (STRICMP(name, "ttyin") == 0) |
| 6123 | n = mch_input_isatty(); |
| 6124 | else if (STRICMP(name, "ttyout") == 0) |
| 6125 | n = stdout_isatty; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6126 | #ifdef FEAT_MBYTE |
| 6127 | else if (STRICMP(name, "multi_byte_encoding") == 0) |
| 6128 | n = has_mbyte; |
| 6129 | #endif |
| 6130 | #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32) |
| 6131 | else if (STRICMP(name, "balloon_multiline") == 0) |
| 6132 | n = multiline_balloon_available(); |
| 6133 | #endif |
| 6134 | #ifdef DYNAMIC_TCL |
| 6135 | else if (STRICMP(name, "tcl") == 0) |
| 6136 | n = tcl_enabled(FALSE); |
| 6137 | #endif |
| 6138 | #if defined(USE_ICONV) && defined(DYNAMIC_ICONV) |
| 6139 | else if (STRICMP(name, "iconv") == 0) |
| 6140 | n = iconv_enabled(FALSE); |
| 6141 | #endif |
| 6142 | #ifdef DYNAMIC_LUA |
| 6143 | else if (STRICMP(name, "lua") == 0) |
| 6144 | n = lua_enabled(FALSE); |
| 6145 | #endif |
| 6146 | #ifdef DYNAMIC_MZSCHEME |
| 6147 | else if (STRICMP(name, "mzscheme") == 0) |
| 6148 | n = mzscheme_enabled(FALSE); |
| 6149 | #endif |
| 6150 | #ifdef DYNAMIC_RUBY |
| 6151 | else if (STRICMP(name, "ruby") == 0) |
| 6152 | n = ruby_enabled(FALSE); |
| 6153 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6154 | #ifdef DYNAMIC_PYTHON |
| 6155 | else if (STRICMP(name, "python") == 0) |
| 6156 | n = python_enabled(FALSE); |
| 6157 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6158 | #ifdef DYNAMIC_PYTHON3 |
| 6159 | else if (STRICMP(name, "python3") == 0) |
| 6160 | n = python3_enabled(FALSE); |
| 6161 | #endif |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 6162 | #if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3) |
| 6163 | else if (STRICMP(name, "pythonx") == 0) |
| 6164 | { |
| 6165 | # if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3) |
| 6166 | if (p_pyx == 0) |
| 6167 | n = python3_enabled(FALSE) || python_enabled(FALSE); |
| 6168 | else if (p_pyx == 3) |
| 6169 | n = python3_enabled(FALSE); |
| 6170 | else if (p_pyx == 2) |
| 6171 | n = python_enabled(FALSE); |
| 6172 | # elif defined(DYNAMIC_PYTHON) |
| 6173 | n = python_enabled(FALSE); |
| 6174 | # elif defined(DYNAMIC_PYTHON3) |
| 6175 | n = python3_enabled(FALSE); |
| 6176 | # endif |
| 6177 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6178 | #endif |
| 6179 | #ifdef DYNAMIC_PERL |
| 6180 | else if (STRICMP(name, "perl") == 0) |
| 6181 | n = perl_enabled(FALSE); |
| 6182 | #endif |
| 6183 | #ifdef FEAT_GUI |
| 6184 | else if (STRICMP(name, "gui_running") == 0) |
| 6185 | n = (gui.in_use || gui.starting); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6186 | # ifdef FEAT_BROWSE |
| 6187 | else if (STRICMP(name, "browse") == 0) |
| 6188 | n = gui.in_use; /* gui_mch_browse() works when GUI is running */ |
| 6189 | # endif |
| 6190 | #endif |
| 6191 | #ifdef FEAT_SYN_HL |
| 6192 | else if (STRICMP(name, "syntax_items") == 0) |
| 6193 | n = syntax_present(curwin); |
| 6194 | #endif |
| 6195 | #if defined(WIN3264) |
| 6196 | else if (STRICMP(name, "win95") == 0) |
Bram Moolenaar | cea912a | 2016-10-12 14:20:24 +0200 | [diff] [blame] | 6197 | n = FALSE; /* Win9x is no more supported. */ |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6198 | #endif |
| 6199 | #ifdef FEAT_NETBEANS_INTG |
| 6200 | else if (STRICMP(name, "netbeans_enabled") == 0) |
| 6201 | n = netbeans_active(); |
| 6202 | #endif |
Bram Moolenaar | a83e396 | 2017-08-17 14:39:07 +0200 | [diff] [blame] | 6203 | #if defined(FEAT_TERMINAL) && defined(WIN3264) |
| 6204 | else if (STRICMP(name, "terminal") == 0) |
| 6205 | n = terminal_enabled(); |
| 6206 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6207 | } |
| 6208 | |
| 6209 | rettv->vval.v_number = n; |
| 6210 | } |
| 6211 | |
| 6212 | /* |
| 6213 | * "has_key()" function |
| 6214 | */ |
| 6215 | static void |
| 6216 | f_has_key(typval_T *argvars, typval_T *rettv) |
| 6217 | { |
| 6218 | if (argvars[0].v_type != VAR_DICT) |
| 6219 | { |
| 6220 | EMSG(_(e_dictreq)); |
| 6221 | return; |
| 6222 | } |
| 6223 | if (argvars[0].vval.v_dict == NULL) |
| 6224 | return; |
| 6225 | |
| 6226 | rettv->vval.v_number = dict_find(argvars[0].vval.v_dict, |
| 6227 | get_tv_string(&argvars[1]), -1) != NULL; |
| 6228 | } |
| 6229 | |
| 6230 | /* |
| 6231 | * "haslocaldir()" function |
| 6232 | */ |
| 6233 | static void |
| 6234 | f_haslocaldir(typval_T *argvars, typval_T *rettv) |
| 6235 | { |
| 6236 | win_T *wp = NULL; |
| 6237 | |
| 6238 | wp = find_tabwin(&argvars[0], &argvars[1]); |
| 6239 | rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL); |
| 6240 | } |
| 6241 | |
| 6242 | /* |
| 6243 | * "hasmapto()" function |
| 6244 | */ |
| 6245 | static void |
| 6246 | f_hasmapto(typval_T *argvars, typval_T *rettv) |
| 6247 | { |
| 6248 | char_u *name; |
| 6249 | char_u *mode; |
| 6250 | char_u buf[NUMBUFLEN]; |
| 6251 | int abbr = FALSE; |
| 6252 | |
| 6253 | name = get_tv_string(&argvars[0]); |
| 6254 | if (argvars[1].v_type == VAR_UNKNOWN) |
| 6255 | mode = (char_u *)"nvo"; |
| 6256 | else |
| 6257 | { |
| 6258 | mode = get_tv_string_buf(&argvars[1], buf); |
| 6259 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 6260 | abbr = (int)get_tv_number(&argvars[2]); |
| 6261 | } |
| 6262 | |
| 6263 | if (map_to_exists(name, mode, abbr)) |
| 6264 | rettv->vval.v_number = TRUE; |
| 6265 | else |
| 6266 | rettv->vval.v_number = FALSE; |
| 6267 | } |
| 6268 | |
| 6269 | /* |
| 6270 | * "histadd()" function |
| 6271 | */ |
| 6272 | static void |
| 6273 | f_histadd(typval_T *argvars UNUSED, typval_T *rettv) |
| 6274 | { |
| 6275 | #ifdef FEAT_CMDHIST |
| 6276 | int histype; |
| 6277 | char_u *str; |
| 6278 | char_u buf[NUMBUFLEN]; |
| 6279 | #endif |
| 6280 | |
| 6281 | rettv->vval.v_number = FALSE; |
| 6282 | if (check_restricted() || check_secure()) |
| 6283 | return; |
| 6284 | #ifdef FEAT_CMDHIST |
| 6285 | str = get_tv_string_chk(&argvars[0]); /* NULL on type error */ |
| 6286 | histype = str != NULL ? get_histtype(str) : -1; |
| 6287 | if (histype >= 0) |
| 6288 | { |
| 6289 | str = get_tv_string_buf(&argvars[1], buf); |
| 6290 | if (*str != NUL) |
| 6291 | { |
| 6292 | init_history(); |
| 6293 | add_to_history(histype, str, FALSE, NUL); |
| 6294 | rettv->vval.v_number = TRUE; |
| 6295 | return; |
| 6296 | } |
| 6297 | } |
| 6298 | #endif |
| 6299 | } |
| 6300 | |
| 6301 | /* |
| 6302 | * "histdel()" function |
| 6303 | */ |
| 6304 | static void |
| 6305 | f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 6306 | { |
| 6307 | #ifdef FEAT_CMDHIST |
| 6308 | int n; |
| 6309 | char_u buf[NUMBUFLEN]; |
| 6310 | char_u *str; |
| 6311 | |
| 6312 | str = get_tv_string_chk(&argvars[0]); /* NULL on type error */ |
| 6313 | if (str == NULL) |
| 6314 | n = 0; |
| 6315 | else if (argvars[1].v_type == VAR_UNKNOWN) |
| 6316 | /* only one argument: clear entire history */ |
| 6317 | n = clr_history(get_histtype(str)); |
| 6318 | else if (argvars[1].v_type == VAR_NUMBER) |
| 6319 | /* index given: remove that entry */ |
| 6320 | n = del_history_idx(get_histtype(str), |
| 6321 | (int)get_tv_number(&argvars[1])); |
| 6322 | else |
| 6323 | /* string given: remove all matching entries */ |
| 6324 | n = del_history_entry(get_histtype(str), |
| 6325 | get_tv_string_buf(&argvars[1], buf)); |
| 6326 | rettv->vval.v_number = n; |
| 6327 | #endif |
| 6328 | } |
| 6329 | |
| 6330 | /* |
| 6331 | * "histget()" function |
| 6332 | */ |
| 6333 | static void |
| 6334 | f_histget(typval_T *argvars UNUSED, typval_T *rettv) |
| 6335 | { |
| 6336 | #ifdef FEAT_CMDHIST |
| 6337 | int type; |
| 6338 | int idx; |
| 6339 | char_u *str; |
| 6340 | |
| 6341 | str = get_tv_string_chk(&argvars[0]); /* NULL on type error */ |
| 6342 | if (str == NULL) |
| 6343 | rettv->vval.v_string = NULL; |
| 6344 | else |
| 6345 | { |
| 6346 | type = get_histtype(str); |
| 6347 | if (argvars[1].v_type == VAR_UNKNOWN) |
| 6348 | idx = get_history_idx(type); |
| 6349 | else |
| 6350 | idx = (int)get_tv_number_chk(&argvars[1], NULL); |
| 6351 | /* -1 on type error */ |
| 6352 | rettv->vval.v_string = vim_strsave(get_history_entry(type, idx)); |
| 6353 | } |
| 6354 | #else |
| 6355 | rettv->vval.v_string = NULL; |
| 6356 | #endif |
| 6357 | rettv->v_type = VAR_STRING; |
| 6358 | } |
| 6359 | |
| 6360 | /* |
| 6361 | * "histnr()" function |
| 6362 | */ |
| 6363 | static void |
| 6364 | f_histnr(typval_T *argvars UNUSED, typval_T *rettv) |
| 6365 | { |
| 6366 | int i; |
| 6367 | |
| 6368 | #ifdef FEAT_CMDHIST |
| 6369 | char_u *history = get_tv_string_chk(&argvars[0]); |
| 6370 | |
| 6371 | i = history == NULL ? HIST_CMD - 1 : get_histtype(history); |
| 6372 | if (i >= HIST_CMD && i < HIST_COUNT) |
| 6373 | i = get_history_idx(i); |
| 6374 | else |
| 6375 | #endif |
| 6376 | i = -1; |
| 6377 | rettv->vval.v_number = i; |
| 6378 | } |
| 6379 | |
| 6380 | /* |
| 6381 | * "highlightID(name)" function |
| 6382 | */ |
| 6383 | static void |
| 6384 | f_hlID(typval_T *argvars, typval_T *rettv) |
| 6385 | { |
| 6386 | rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0])); |
| 6387 | } |
| 6388 | |
| 6389 | /* |
| 6390 | * "highlight_exists()" function |
| 6391 | */ |
| 6392 | static void |
| 6393 | f_hlexists(typval_T *argvars, typval_T *rettv) |
| 6394 | { |
| 6395 | rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0])); |
| 6396 | } |
| 6397 | |
| 6398 | /* |
| 6399 | * "hostname()" function |
| 6400 | */ |
| 6401 | static void |
| 6402 | f_hostname(typval_T *argvars UNUSED, typval_T *rettv) |
| 6403 | { |
| 6404 | char_u hostname[256]; |
| 6405 | |
| 6406 | mch_get_host_name(hostname, 256); |
| 6407 | rettv->v_type = VAR_STRING; |
| 6408 | rettv->vval.v_string = vim_strsave(hostname); |
| 6409 | } |
| 6410 | |
| 6411 | /* |
| 6412 | * iconv() function |
| 6413 | */ |
| 6414 | static void |
| 6415 | f_iconv(typval_T *argvars UNUSED, typval_T *rettv) |
| 6416 | { |
| 6417 | #ifdef FEAT_MBYTE |
| 6418 | char_u buf1[NUMBUFLEN]; |
| 6419 | char_u buf2[NUMBUFLEN]; |
| 6420 | char_u *from, *to, *str; |
| 6421 | vimconv_T vimconv; |
| 6422 | #endif |
| 6423 | |
| 6424 | rettv->v_type = VAR_STRING; |
| 6425 | rettv->vval.v_string = NULL; |
| 6426 | |
| 6427 | #ifdef FEAT_MBYTE |
| 6428 | str = get_tv_string(&argvars[0]); |
| 6429 | from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1))); |
| 6430 | to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2))); |
| 6431 | vimconv.vc_type = CONV_NONE; |
| 6432 | convert_setup(&vimconv, from, to); |
| 6433 | |
| 6434 | /* If the encodings are equal, no conversion needed. */ |
| 6435 | if (vimconv.vc_type == CONV_NONE) |
| 6436 | rettv->vval.v_string = vim_strsave(str); |
| 6437 | else |
| 6438 | rettv->vval.v_string = string_convert(&vimconv, str, NULL); |
| 6439 | |
| 6440 | convert_setup(&vimconv, NULL, NULL); |
| 6441 | vim_free(from); |
| 6442 | vim_free(to); |
| 6443 | #endif |
| 6444 | } |
| 6445 | |
| 6446 | /* |
| 6447 | * "indent()" function |
| 6448 | */ |
| 6449 | static void |
| 6450 | f_indent(typval_T *argvars, typval_T *rettv) |
| 6451 | { |
| 6452 | linenr_T lnum; |
| 6453 | |
| 6454 | lnum = get_tv_lnum(argvars); |
| 6455 | if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) |
| 6456 | rettv->vval.v_number = get_indent_lnum(lnum); |
| 6457 | else |
| 6458 | rettv->vval.v_number = -1; |
| 6459 | } |
| 6460 | |
| 6461 | /* |
| 6462 | * "index()" function |
| 6463 | */ |
| 6464 | static void |
| 6465 | f_index(typval_T *argvars, typval_T *rettv) |
| 6466 | { |
| 6467 | list_T *l; |
| 6468 | listitem_T *item; |
| 6469 | long idx = 0; |
| 6470 | int ic = FALSE; |
| 6471 | |
| 6472 | rettv->vval.v_number = -1; |
| 6473 | if (argvars[0].v_type != VAR_LIST) |
| 6474 | { |
| 6475 | EMSG(_(e_listreq)); |
| 6476 | return; |
| 6477 | } |
| 6478 | l = argvars[0].vval.v_list; |
| 6479 | if (l != NULL) |
| 6480 | { |
| 6481 | item = l->lv_first; |
| 6482 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 6483 | { |
| 6484 | int error = FALSE; |
| 6485 | |
| 6486 | /* Start at specified item. Use the cached index that list_find() |
| 6487 | * sets, so that a negative number also works. */ |
| 6488 | item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error)); |
| 6489 | idx = l->lv_idx; |
| 6490 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 6491 | ic = (int)get_tv_number_chk(&argvars[3], &error); |
| 6492 | if (error) |
| 6493 | item = NULL; |
| 6494 | } |
| 6495 | |
| 6496 | for ( ; item != NULL; item = item->li_next, ++idx) |
| 6497 | if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE)) |
| 6498 | { |
| 6499 | rettv->vval.v_number = idx; |
| 6500 | break; |
| 6501 | } |
| 6502 | } |
| 6503 | } |
| 6504 | |
| 6505 | static int inputsecret_flag = 0; |
| 6506 | |
| 6507 | /* |
| 6508 | * "input()" function |
| 6509 | * Also handles inputsecret() when inputsecret is set. |
| 6510 | */ |
| 6511 | static void |
| 6512 | f_input(typval_T *argvars, typval_T *rettv) |
| 6513 | { |
| 6514 | get_user_input(argvars, rettv, FALSE, inputsecret_flag); |
| 6515 | } |
| 6516 | |
| 6517 | /* |
| 6518 | * "inputdialog()" function |
| 6519 | */ |
| 6520 | static void |
| 6521 | f_inputdialog(typval_T *argvars, typval_T *rettv) |
| 6522 | { |
| 6523 | #if defined(FEAT_GUI_TEXTDIALOG) |
| 6524 | /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */ |
| 6525 | if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL) |
| 6526 | { |
| 6527 | char_u *message; |
| 6528 | char_u buf[NUMBUFLEN]; |
| 6529 | char_u *defstr = (char_u *)""; |
| 6530 | |
| 6531 | message = get_tv_string_chk(&argvars[0]); |
| 6532 | if (argvars[1].v_type != VAR_UNKNOWN |
| 6533 | && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL) |
| 6534 | vim_strncpy(IObuff, defstr, IOSIZE - 1); |
| 6535 | else |
| 6536 | IObuff[0] = NUL; |
| 6537 | if (message != NULL && defstr != NULL |
| 6538 | && do_dialog(VIM_QUESTION, NULL, message, |
| 6539 | (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1) |
| 6540 | rettv->vval.v_string = vim_strsave(IObuff); |
| 6541 | else |
| 6542 | { |
| 6543 | if (message != NULL && defstr != NULL |
| 6544 | && argvars[1].v_type != VAR_UNKNOWN |
| 6545 | && argvars[2].v_type != VAR_UNKNOWN) |
| 6546 | rettv->vval.v_string = vim_strsave( |
| 6547 | get_tv_string_buf(&argvars[2], buf)); |
| 6548 | else |
| 6549 | rettv->vval.v_string = NULL; |
| 6550 | } |
| 6551 | rettv->v_type = VAR_STRING; |
| 6552 | } |
| 6553 | else |
| 6554 | #endif |
| 6555 | get_user_input(argvars, rettv, TRUE, inputsecret_flag); |
| 6556 | } |
| 6557 | |
| 6558 | /* |
| 6559 | * "inputlist()" function |
| 6560 | */ |
| 6561 | static void |
| 6562 | f_inputlist(typval_T *argvars, typval_T *rettv) |
| 6563 | { |
| 6564 | listitem_T *li; |
| 6565 | int selected; |
| 6566 | int mouse_used; |
| 6567 | |
| 6568 | #ifdef NO_CONSOLE_INPUT |
Bram Moolenaar | 91d348a | 2017-07-29 20:16:03 +0200 | [diff] [blame] | 6569 | /* While starting up, there is no place to enter text. When running tests |
| 6570 | * with --not-a-term we assume feedkeys() will be used. */ |
| 6571 | if (no_console_input() && !is_not_a_term()) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6572 | return; |
| 6573 | #endif |
| 6574 | if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL) |
| 6575 | { |
| 6576 | EMSG2(_(e_listarg), "inputlist()"); |
| 6577 | return; |
| 6578 | } |
| 6579 | |
| 6580 | msg_start(); |
| 6581 | msg_row = Rows - 1; /* for when 'cmdheight' > 1 */ |
| 6582 | lines_left = Rows; /* avoid more prompt */ |
| 6583 | msg_scroll = TRUE; |
| 6584 | msg_clr_eos(); |
| 6585 | |
| 6586 | for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next) |
| 6587 | { |
| 6588 | msg_puts(get_tv_string(&li->li_tv)); |
| 6589 | msg_putchar('\n'); |
| 6590 | } |
| 6591 | |
| 6592 | /* Ask for choice. */ |
| 6593 | selected = prompt_for_number(&mouse_used); |
| 6594 | if (mouse_used) |
| 6595 | selected -= lines_left; |
| 6596 | |
| 6597 | rettv->vval.v_number = selected; |
| 6598 | } |
| 6599 | |
| 6600 | |
| 6601 | static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL}; |
| 6602 | |
| 6603 | /* |
| 6604 | * "inputrestore()" function |
| 6605 | */ |
| 6606 | static void |
| 6607 | f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv) |
| 6608 | { |
| 6609 | if (ga_userinput.ga_len > 0) |
| 6610 | { |
| 6611 | --ga_userinput.ga_len; |
| 6612 | restore_typeahead((tasave_T *)(ga_userinput.ga_data) |
| 6613 | + ga_userinput.ga_len); |
| 6614 | /* default return is zero == OK */ |
| 6615 | } |
| 6616 | else if (p_verbose > 1) |
| 6617 | { |
| 6618 | verb_msg((char_u *)_("called inputrestore() more often than inputsave()")); |
| 6619 | rettv->vval.v_number = 1; /* Failed */ |
| 6620 | } |
| 6621 | } |
| 6622 | |
| 6623 | /* |
| 6624 | * "inputsave()" function |
| 6625 | */ |
| 6626 | static void |
| 6627 | f_inputsave(typval_T *argvars UNUSED, typval_T *rettv) |
| 6628 | { |
| 6629 | /* Add an entry to the stack of typeahead storage. */ |
| 6630 | if (ga_grow(&ga_userinput, 1) == OK) |
| 6631 | { |
| 6632 | save_typeahead((tasave_T *)(ga_userinput.ga_data) |
| 6633 | + ga_userinput.ga_len); |
| 6634 | ++ga_userinput.ga_len; |
| 6635 | /* default return is zero == OK */ |
| 6636 | } |
| 6637 | else |
| 6638 | rettv->vval.v_number = 1; /* Failed */ |
| 6639 | } |
| 6640 | |
| 6641 | /* |
| 6642 | * "inputsecret()" function |
| 6643 | */ |
| 6644 | static void |
| 6645 | f_inputsecret(typval_T *argvars, typval_T *rettv) |
| 6646 | { |
| 6647 | ++cmdline_star; |
| 6648 | ++inputsecret_flag; |
| 6649 | f_input(argvars, rettv); |
| 6650 | --cmdline_star; |
| 6651 | --inputsecret_flag; |
| 6652 | } |
| 6653 | |
| 6654 | /* |
| 6655 | * "insert()" function |
| 6656 | */ |
| 6657 | static void |
| 6658 | f_insert(typval_T *argvars, typval_T *rettv) |
| 6659 | { |
| 6660 | long before = 0; |
| 6661 | listitem_T *item; |
| 6662 | list_T *l; |
| 6663 | int error = FALSE; |
| 6664 | |
| 6665 | if (argvars[0].v_type != VAR_LIST) |
| 6666 | EMSG2(_(e_listarg), "insert()"); |
| 6667 | else if ((l = argvars[0].vval.v_list) != NULL |
| 6668 | && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE)) |
| 6669 | { |
| 6670 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 6671 | before = (long)get_tv_number_chk(&argvars[2], &error); |
| 6672 | if (error) |
| 6673 | return; /* type error; errmsg already given */ |
| 6674 | |
| 6675 | if (before == l->lv_len) |
| 6676 | item = NULL; |
| 6677 | else |
| 6678 | { |
| 6679 | item = list_find(l, before); |
| 6680 | if (item == NULL) |
| 6681 | { |
| 6682 | EMSGN(_(e_listidx), before); |
| 6683 | l = NULL; |
| 6684 | } |
| 6685 | } |
| 6686 | if (l != NULL) |
| 6687 | { |
| 6688 | list_insert_tv(l, &argvars[1], item); |
| 6689 | copy_tv(&argvars[0], rettv); |
| 6690 | } |
| 6691 | } |
| 6692 | } |
| 6693 | |
| 6694 | /* |
| 6695 | * "invert(expr)" function |
| 6696 | */ |
| 6697 | static void |
| 6698 | f_invert(typval_T *argvars, typval_T *rettv) |
| 6699 | { |
| 6700 | rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL); |
| 6701 | } |
| 6702 | |
| 6703 | /* |
| 6704 | * "isdirectory()" function |
| 6705 | */ |
| 6706 | static void |
| 6707 | f_isdirectory(typval_T *argvars, typval_T *rettv) |
| 6708 | { |
| 6709 | rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0])); |
| 6710 | } |
| 6711 | |
| 6712 | /* |
| 6713 | * Return TRUE if typeval "tv" is locked: Either that value is locked itself |
| 6714 | * or it refers to a List or Dictionary that is locked. |
| 6715 | */ |
| 6716 | static int |
| 6717 | tv_islocked(typval_T *tv) |
| 6718 | { |
| 6719 | return (tv->v_lock & VAR_LOCKED) |
| 6720 | || (tv->v_type == VAR_LIST |
| 6721 | && tv->vval.v_list != NULL |
| 6722 | && (tv->vval.v_list->lv_lock & VAR_LOCKED)) |
| 6723 | || (tv->v_type == VAR_DICT |
| 6724 | && tv->vval.v_dict != NULL |
| 6725 | && (tv->vval.v_dict->dv_lock & VAR_LOCKED)); |
| 6726 | } |
| 6727 | |
| 6728 | /* |
| 6729 | * "islocked()" function |
| 6730 | */ |
| 6731 | static void |
| 6732 | f_islocked(typval_T *argvars, typval_T *rettv) |
| 6733 | { |
| 6734 | lval_T lv; |
| 6735 | char_u *end; |
| 6736 | dictitem_T *di; |
| 6737 | |
| 6738 | rettv->vval.v_number = -1; |
| 6739 | end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, |
Bram Moolenaar | 3a25773 | 2017-02-21 20:47:13 +0100 | [diff] [blame] | 6740 | GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6741 | if (end != NULL && lv.ll_name != NULL) |
| 6742 | { |
| 6743 | if (*end != NUL) |
| 6744 | EMSG(_(e_trailing)); |
| 6745 | else |
| 6746 | { |
| 6747 | if (lv.ll_tv == NULL) |
| 6748 | { |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 6749 | di = find_var(lv.ll_name, NULL, TRUE); |
| 6750 | if (di != NULL) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6751 | { |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 6752 | /* Consider a variable locked when: |
| 6753 | * 1. the variable itself is locked |
| 6754 | * 2. the value of the variable is locked. |
| 6755 | * 3. the List or Dict value is locked. |
| 6756 | */ |
| 6757 | rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK) |
| 6758 | || tv_islocked(&di->di_tv)); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6759 | } |
| 6760 | } |
| 6761 | else if (lv.ll_range) |
| 6762 | EMSG(_("E786: Range not allowed")); |
| 6763 | else if (lv.ll_newkey != NULL) |
| 6764 | EMSG2(_(e_dictkey), lv.ll_newkey); |
| 6765 | else if (lv.ll_list != NULL) |
| 6766 | /* List item. */ |
| 6767 | rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv); |
| 6768 | else |
| 6769 | /* Dictionary item. */ |
| 6770 | rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv); |
| 6771 | } |
| 6772 | } |
| 6773 | |
| 6774 | clear_lval(&lv); |
| 6775 | } |
| 6776 | |
| 6777 | #if defined(FEAT_FLOAT) && defined(HAVE_MATH_H) |
| 6778 | /* |
| 6779 | * "isnan()" function |
| 6780 | */ |
| 6781 | static void |
| 6782 | f_isnan(typval_T *argvars, typval_T *rettv) |
| 6783 | { |
| 6784 | rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT |
| 6785 | && isnan(argvars[0].vval.v_float); |
| 6786 | } |
| 6787 | #endif |
| 6788 | |
| 6789 | /* |
| 6790 | * "items(dict)" function |
| 6791 | */ |
| 6792 | static void |
| 6793 | f_items(typval_T *argvars, typval_T *rettv) |
| 6794 | { |
| 6795 | dict_list(argvars, rettv, 2); |
| 6796 | } |
| 6797 | |
| 6798 | #if defined(FEAT_JOB_CHANNEL) || defined(PROTO) |
| 6799 | /* |
| 6800 | * Get the job from the argument. |
| 6801 | * Returns NULL if the job is invalid. |
| 6802 | */ |
| 6803 | static job_T * |
| 6804 | get_job_arg(typval_T *tv) |
| 6805 | { |
| 6806 | job_T *job; |
| 6807 | |
| 6808 | if (tv->v_type != VAR_JOB) |
| 6809 | { |
| 6810 | EMSG2(_(e_invarg2), get_tv_string(tv)); |
| 6811 | return NULL; |
| 6812 | } |
| 6813 | job = tv->vval.v_job; |
| 6814 | |
| 6815 | if (job == NULL) |
| 6816 | EMSG(_("E916: not a valid job")); |
| 6817 | return job; |
| 6818 | } |
| 6819 | |
| 6820 | /* |
| 6821 | * "job_getchannel()" function |
| 6822 | */ |
| 6823 | static void |
| 6824 | f_job_getchannel(typval_T *argvars, typval_T *rettv) |
| 6825 | { |
| 6826 | job_T *job = get_job_arg(&argvars[0]); |
| 6827 | |
| 6828 | if (job != NULL) |
| 6829 | { |
| 6830 | rettv->v_type = VAR_CHANNEL; |
| 6831 | rettv->vval.v_channel = job->jv_channel; |
| 6832 | if (job->jv_channel != NULL) |
| 6833 | ++job->jv_channel->ch_refcount; |
| 6834 | } |
| 6835 | } |
| 6836 | |
| 6837 | /* |
| 6838 | * "job_info()" function |
| 6839 | */ |
| 6840 | static void |
| 6841 | f_job_info(typval_T *argvars, typval_T *rettv) |
| 6842 | { |
| 6843 | job_T *job = get_job_arg(&argvars[0]); |
| 6844 | |
| 6845 | if (job != NULL && rettv_dict_alloc(rettv) != FAIL) |
| 6846 | job_info(job, rettv->vval.v_dict); |
| 6847 | } |
| 6848 | |
| 6849 | /* |
| 6850 | * "job_setoptions()" function |
| 6851 | */ |
| 6852 | static void |
| 6853 | f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED) |
| 6854 | { |
| 6855 | job_T *job = get_job_arg(&argvars[0]); |
| 6856 | jobopt_T opt; |
| 6857 | |
| 6858 | if (job == NULL) |
| 6859 | return; |
| 6860 | clear_job_options(&opt); |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 6861 | if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6862 | job_set_options(job, &opt); |
| 6863 | free_job_options(&opt); |
| 6864 | } |
| 6865 | |
| 6866 | /* |
| 6867 | * "job_start()" function |
| 6868 | */ |
| 6869 | static void |
| 6870 | f_job_start(typval_T *argvars, typval_T *rettv) |
| 6871 | { |
| 6872 | rettv->v_type = VAR_JOB; |
| 6873 | if (check_restricted() || check_secure()) |
| 6874 | return; |
Bram Moolenaar | cb8bbe9 | 2017-07-16 13:48:22 +0200 | [diff] [blame] | 6875 | rettv->vval.v_job = job_start(argvars, NULL); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6876 | } |
| 6877 | |
| 6878 | /* |
| 6879 | * "job_status()" function |
| 6880 | */ |
| 6881 | static void |
| 6882 | f_job_status(typval_T *argvars, typval_T *rettv) |
| 6883 | { |
| 6884 | job_T *job = get_job_arg(&argvars[0]); |
| 6885 | |
| 6886 | if (job != NULL) |
| 6887 | { |
| 6888 | rettv->v_type = VAR_STRING; |
| 6889 | rettv->vval.v_string = vim_strsave((char_u *)job_status(job)); |
| 6890 | } |
| 6891 | } |
| 6892 | |
| 6893 | /* |
| 6894 | * "job_stop()" function |
| 6895 | */ |
| 6896 | static void |
| 6897 | f_job_stop(typval_T *argvars, typval_T *rettv) |
| 6898 | { |
| 6899 | job_T *job = get_job_arg(&argvars[0]); |
| 6900 | |
| 6901 | if (job != NULL) |
Bram Moolenaar | 96ca27a | 2017-07-17 23:20:24 +0200 | [diff] [blame] | 6902 | rettv->vval.v_number = job_stop(job, argvars, NULL); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6903 | } |
| 6904 | #endif |
| 6905 | |
| 6906 | /* |
| 6907 | * "join()" function |
| 6908 | */ |
| 6909 | static void |
| 6910 | f_join(typval_T *argvars, typval_T *rettv) |
| 6911 | { |
| 6912 | garray_T ga; |
| 6913 | char_u *sep; |
| 6914 | |
| 6915 | if (argvars[0].v_type != VAR_LIST) |
| 6916 | { |
| 6917 | EMSG(_(e_listreq)); |
| 6918 | return; |
| 6919 | } |
| 6920 | if (argvars[0].vval.v_list == NULL) |
| 6921 | return; |
| 6922 | if (argvars[1].v_type == VAR_UNKNOWN) |
| 6923 | sep = (char_u *)" "; |
| 6924 | else |
| 6925 | sep = get_tv_string_chk(&argvars[1]); |
| 6926 | |
| 6927 | rettv->v_type = VAR_STRING; |
| 6928 | |
| 6929 | if (sep != NULL) |
| 6930 | { |
| 6931 | ga_init2(&ga, (int)sizeof(char), 80); |
| 6932 | list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0); |
| 6933 | ga_append(&ga, NUL); |
| 6934 | rettv->vval.v_string = (char_u *)ga.ga_data; |
| 6935 | } |
| 6936 | else |
| 6937 | rettv->vval.v_string = NULL; |
| 6938 | } |
| 6939 | |
| 6940 | /* |
| 6941 | * "js_decode()" function |
| 6942 | */ |
| 6943 | static void |
| 6944 | f_js_decode(typval_T *argvars, typval_T *rettv) |
| 6945 | { |
| 6946 | js_read_T reader; |
| 6947 | |
| 6948 | reader.js_buf = get_tv_string(&argvars[0]); |
| 6949 | reader.js_fill = NULL; |
| 6950 | reader.js_used = 0; |
| 6951 | if (json_decode_all(&reader, rettv, JSON_JS) != OK) |
| 6952 | EMSG(_(e_invarg)); |
| 6953 | } |
| 6954 | |
| 6955 | /* |
| 6956 | * "js_encode()" function |
| 6957 | */ |
| 6958 | static void |
| 6959 | f_js_encode(typval_T *argvars, typval_T *rettv) |
| 6960 | { |
| 6961 | rettv->v_type = VAR_STRING; |
| 6962 | rettv->vval.v_string = json_encode(&argvars[0], JSON_JS); |
| 6963 | } |
| 6964 | |
| 6965 | /* |
| 6966 | * "json_decode()" function |
| 6967 | */ |
| 6968 | static void |
| 6969 | f_json_decode(typval_T *argvars, typval_T *rettv) |
| 6970 | { |
| 6971 | js_read_T reader; |
| 6972 | |
| 6973 | reader.js_buf = get_tv_string(&argvars[0]); |
| 6974 | reader.js_fill = NULL; |
| 6975 | reader.js_used = 0; |
Bram Moolenaar | 03c60c1 | 2017-01-10 15:15:37 +0100 | [diff] [blame] | 6976 | json_decode_all(&reader, rettv, 0); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 6977 | } |
| 6978 | |
| 6979 | /* |
| 6980 | * "json_encode()" function |
| 6981 | */ |
| 6982 | static void |
| 6983 | f_json_encode(typval_T *argvars, typval_T *rettv) |
| 6984 | { |
| 6985 | rettv->v_type = VAR_STRING; |
| 6986 | rettv->vval.v_string = json_encode(&argvars[0], 0); |
| 6987 | } |
| 6988 | |
| 6989 | /* |
| 6990 | * "keys()" function |
| 6991 | */ |
| 6992 | static void |
| 6993 | f_keys(typval_T *argvars, typval_T *rettv) |
| 6994 | { |
| 6995 | dict_list(argvars, rettv, 0); |
| 6996 | } |
| 6997 | |
| 6998 | /* |
| 6999 | * "last_buffer_nr()" function. |
| 7000 | */ |
| 7001 | static void |
| 7002 | f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv) |
| 7003 | { |
| 7004 | int n = 0; |
| 7005 | buf_T *buf; |
| 7006 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 7007 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7008 | if (n < buf->b_fnum) |
| 7009 | n = buf->b_fnum; |
| 7010 | |
| 7011 | rettv->vval.v_number = n; |
| 7012 | } |
| 7013 | |
| 7014 | /* |
| 7015 | * "len()" function |
| 7016 | */ |
| 7017 | static void |
| 7018 | f_len(typval_T *argvars, typval_T *rettv) |
| 7019 | { |
| 7020 | switch (argvars[0].v_type) |
| 7021 | { |
| 7022 | case VAR_STRING: |
| 7023 | case VAR_NUMBER: |
| 7024 | rettv->vval.v_number = (varnumber_T)STRLEN( |
| 7025 | get_tv_string(&argvars[0])); |
| 7026 | break; |
| 7027 | case VAR_LIST: |
| 7028 | rettv->vval.v_number = list_len(argvars[0].vval.v_list); |
| 7029 | break; |
| 7030 | case VAR_DICT: |
| 7031 | rettv->vval.v_number = dict_len(argvars[0].vval.v_dict); |
| 7032 | break; |
| 7033 | case VAR_UNKNOWN: |
| 7034 | case VAR_SPECIAL: |
| 7035 | case VAR_FLOAT: |
| 7036 | case VAR_FUNC: |
| 7037 | case VAR_PARTIAL: |
| 7038 | case VAR_JOB: |
| 7039 | case VAR_CHANNEL: |
| 7040 | EMSG(_("E701: Invalid type for len()")); |
| 7041 | break; |
| 7042 | } |
| 7043 | } |
| 7044 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7045 | static void |
Bram Moolenaar | 6d721c7 | 2017-01-17 16:56:28 +0100 | [diff] [blame] | 7046 | libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7047 | { |
| 7048 | #ifdef FEAT_LIBCALL |
| 7049 | char_u *string_in; |
| 7050 | char_u **string_result; |
| 7051 | int nr_result; |
| 7052 | #endif |
| 7053 | |
| 7054 | rettv->v_type = type; |
| 7055 | if (type != VAR_NUMBER) |
| 7056 | rettv->vval.v_string = NULL; |
| 7057 | |
| 7058 | if (check_restricted() || check_secure()) |
| 7059 | return; |
| 7060 | |
| 7061 | #ifdef FEAT_LIBCALL |
Bram Moolenaar | 9af4184 | 2016-09-25 21:45:05 +0200 | [diff] [blame] | 7062 | /* The first two args must be strings, otherwise it's meaningless */ |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7063 | if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING) |
| 7064 | { |
| 7065 | string_in = NULL; |
| 7066 | if (argvars[2].v_type == VAR_STRING) |
| 7067 | string_in = argvars[2].vval.v_string; |
| 7068 | if (type == VAR_NUMBER) |
| 7069 | string_result = NULL; |
| 7070 | else |
| 7071 | string_result = &rettv->vval.v_string; |
| 7072 | if (mch_libcall(argvars[0].vval.v_string, |
| 7073 | argvars[1].vval.v_string, |
| 7074 | string_in, |
| 7075 | argvars[2].vval.v_number, |
| 7076 | string_result, |
| 7077 | &nr_result) == OK |
| 7078 | && type == VAR_NUMBER) |
| 7079 | rettv->vval.v_number = nr_result; |
| 7080 | } |
| 7081 | #endif |
| 7082 | } |
| 7083 | |
| 7084 | /* |
| 7085 | * "libcall()" function |
| 7086 | */ |
| 7087 | static void |
| 7088 | f_libcall(typval_T *argvars, typval_T *rettv) |
| 7089 | { |
| 7090 | libcall_common(argvars, rettv, VAR_STRING); |
| 7091 | } |
| 7092 | |
| 7093 | /* |
| 7094 | * "libcallnr()" function |
| 7095 | */ |
| 7096 | static void |
| 7097 | f_libcallnr(typval_T *argvars, typval_T *rettv) |
| 7098 | { |
| 7099 | libcall_common(argvars, rettv, VAR_NUMBER); |
| 7100 | } |
| 7101 | |
| 7102 | /* |
| 7103 | * "line(string)" function |
| 7104 | */ |
| 7105 | static void |
| 7106 | f_line(typval_T *argvars, typval_T *rettv) |
| 7107 | { |
| 7108 | linenr_T lnum = 0; |
| 7109 | pos_T *fp; |
| 7110 | int fnum; |
| 7111 | |
| 7112 | fp = var2fpos(&argvars[0], TRUE, &fnum); |
| 7113 | if (fp != NULL) |
| 7114 | lnum = fp->lnum; |
| 7115 | rettv->vval.v_number = lnum; |
| 7116 | } |
| 7117 | |
| 7118 | /* |
| 7119 | * "line2byte(lnum)" function |
| 7120 | */ |
| 7121 | static void |
| 7122 | f_line2byte(typval_T *argvars UNUSED, typval_T *rettv) |
| 7123 | { |
| 7124 | #ifndef FEAT_BYTEOFF |
| 7125 | rettv->vval.v_number = -1; |
| 7126 | #else |
| 7127 | linenr_T lnum; |
| 7128 | |
| 7129 | lnum = get_tv_lnum(argvars); |
| 7130 | if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1) |
| 7131 | rettv->vval.v_number = -1; |
| 7132 | else |
| 7133 | rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL); |
| 7134 | if (rettv->vval.v_number >= 0) |
| 7135 | ++rettv->vval.v_number; |
| 7136 | #endif |
| 7137 | } |
| 7138 | |
| 7139 | /* |
| 7140 | * "lispindent(lnum)" function |
| 7141 | */ |
| 7142 | static void |
| 7143 | f_lispindent(typval_T *argvars UNUSED, typval_T *rettv) |
| 7144 | { |
| 7145 | #ifdef FEAT_LISP |
| 7146 | pos_T pos; |
| 7147 | linenr_T lnum; |
| 7148 | |
| 7149 | pos = curwin->w_cursor; |
| 7150 | lnum = get_tv_lnum(argvars); |
| 7151 | if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) |
| 7152 | { |
| 7153 | curwin->w_cursor.lnum = lnum; |
| 7154 | rettv->vval.v_number = get_lisp_indent(); |
| 7155 | curwin->w_cursor = pos; |
| 7156 | } |
| 7157 | else |
| 7158 | #endif |
| 7159 | rettv->vval.v_number = -1; |
| 7160 | } |
| 7161 | |
| 7162 | /* |
| 7163 | * "localtime()" function |
| 7164 | */ |
| 7165 | static void |
| 7166 | f_localtime(typval_T *argvars UNUSED, typval_T *rettv) |
| 7167 | { |
| 7168 | rettv->vval.v_number = (varnumber_T)time(NULL); |
| 7169 | } |
| 7170 | |
| 7171 | static void get_maparg(typval_T *argvars, typval_T *rettv, int exact); |
| 7172 | |
| 7173 | static void |
| 7174 | get_maparg(typval_T *argvars, typval_T *rettv, int exact) |
| 7175 | { |
| 7176 | char_u *keys; |
| 7177 | char_u *which; |
| 7178 | char_u buf[NUMBUFLEN]; |
| 7179 | char_u *keys_buf = NULL; |
| 7180 | char_u *rhs; |
| 7181 | int mode; |
| 7182 | int abbr = FALSE; |
| 7183 | int get_dict = FALSE; |
| 7184 | mapblock_T *mp; |
| 7185 | int buffer_local; |
| 7186 | |
| 7187 | /* return empty string for failure */ |
| 7188 | rettv->v_type = VAR_STRING; |
| 7189 | rettv->vval.v_string = NULL; |
| 7190 | |
| 7191 | keys = get_tv_string(&argvars[0]); |
| 7192 | if (*keys == NUL) |
| 7193 | return; |
| 7194 | |
| 7195 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 7196 | { |
| 7197 | which = get_tv_string_buf_chk(&argvars[1], buf); |
| 7198 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 7199 | { |
| 7200 | abbr = (int)get_tv_number(&argvars[2]); |
| 7201 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 7202 | get_dict = (int)get_tv_number(&argvars[3]); |
| 7203 | } |
| 7204 | } |
| 7205 | else |
| 7206 | which = (char_u *)""; |
| 7207 | if (which == NULL) |
| 7208 | return; |
| 7209 | |
| 7210 | mode = get_map_mode(&which, 0); |
| 7211 | |
| 7212 | keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE); |
| 7213 | rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local); |
| 7214 | vim_free(keys_buf); |
| 7215 | |
| 7216 | if (!get_dict) |
| 7217 | { |
| 7218 | /* Return a string. */ |
| 7219 | if (rhs != NULL) |
| 7220 | rettv->vval.v_string = str2special_save(rhs, FALSE); |
| 7221 | |
| 7222 | } |
| 7223 | else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL) |
| 7224 | { |
| 7225 | /* Return a dictionary. */ |
| 7226 | char_u *lhs = str2special_save(mp->m_keys, TRUE); |
| 7227 | char_u *mapmode = map_mode_to_chars(mp->m_mode); |
| 7228 | dict_T *dict = rettv->vval.v_dict; |
| 7229 | |
| 7230 | dict_add_nr_str(dict, "lhs", 0L, lhs); |
| 7231 | dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str); |
| 7232 | dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL); |
| 7233 | dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL); |
| 7234 | dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL); |
| 7235 | dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL); |
| 7236 | dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL); |
| 7237 | dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL); |
| 7238 | dict_add_nr_str(dict, "mode", 0L, mapmode); |
| 7239 | |
| 7240 | vim_free(lhs); |
| 7241 | vim_free(mapmode); |
| 7242 | } |
| 7243 | } |
| 7244 | |
| 7245 | #ifdef FEAT_FLOAT |
| 7246 | /* |
| 7247 | * "log()" function |
| 7248 | */ |
| 7249 | static void |
| 7250 | f_log(typval_T *argvars, typval_T *rettv) |
| 7251 | { |
| 7252 | float_T f = 0.0; |
| 7253 | |
| 7254 | rettv->v_type = VAR_FLOAT; |
| 7255 | if (get_float_arg(argvars, &f) == OK) |
| 7256 | rettv->vval.v_float = log(f); |
| 7257 | else |
| 7258 | rettv->vval.v_float = 0.0; |
| 7259 | } |
| 7260 | |
| 7261 | /* |
| 7262 | * "log10()" function |
| 7263 | */ |
| 7264 | static void |
| 7265 | f_log10(typval_T *argvars, typval_T *rettv) |
| 7266 | { |
| 7267 | float_T f = 0.0; |
| 7268 | |
| 7269 | rettv->v_type = VAR_FLOAT; |
| 7270 | if (get_float_arg(argvars, &f) == OK) |
| 7271 | rettv->vval.v_float = log10(f); |
| 7272 | else |
| 7273 | rettv->vval.v_float = 0.0; |
| 7274 | } |
| 7275 | #endif |
| 7276 | |
| 7277 | #ifdef FEAT_LUA |
| 7278 | /* |
| 7279 | * "luaeval()" function |
| 7280 | */ |
| 7281 | static void |
| 7282 | f_luaeval(typval_T *argvars, typval_T *rettv) |
| 7283 | { |
| 7284 | char_u *str; |
| 7285 | char_u buf[NUMBUFLEN]; |
| 7286 | |
| 7287 | str = get_tv_string_buf(&argvars[0], buf); |
| 7288 | do_luaeval(str, argvars + 1, rettv); |
| 7289 | } |
| 7290 | #endif |
| 7291 | |
| 7292 | /* |
| 7293 | * "map()" function |
| 7294 | */ |
| 7295 | static void |
| 7296 | f_map(typval_T *argvars, typval_T *rettv) |
| 7297 | { |
| 7298 | filter_map(argvars, rettv, TRUE); |
| 7299 | } |
| 7300 | |
| 7301 | /* |
| 7302 | * "maparg()" function |
| 7303 | */ |
| 7304 | static void |
| 7305 | f_maparg(typval_T *argvars, typval_T *rettv) |
| 7306 | { |
| 7307 | get_maparg(argvars, rettv, TRUE); |
| 7308 | } |
| 7309 | |
| 7310 | /* |
| 7311 | * "mapcheck()" function |
| 7312 | */ |
| 7313 | static void |
| 7314 | f_mapcheck(typval_T *argvars, typval_T *rettv) |
| 7315 | { |
| 7316 | get_maparg(argvars, rettv, FALSE); |
| 7317 | } |
| 7318 | |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7319 | typedef enum |
| 7320 | { |
| 7321 | MATCH_END, /* matchend() */ |
| 7322 | MATCH_MATCH, /* match() */ |
| 7323 | MATCH_STR, /* matchstr() */ |
| 7324 | MATCH_LIST, /* matchlist() */ |
| 7325 | MATCH_POS /* matchstrpos() */ |
| 7326 | } matchtype_T; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7327 | |
| 7328 | static void |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7329 | find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7330 | { |
| 7331 | char_u *str = NULL; |
| 7332 | long len = 0; |
| 7333 | char_u *expr = NULL; |
| 7334 | char_u *pat; |
| 7335 | regmatch_T regmatch; |
| 7336 | char_u patbuf[NUMBUFLEN]; |
| 7337 | char_u strbuf[NUMBUFLEN]; |
| 7338 | char_u *save_cpo; |
| 7339 | long start = 0; |
| 7340 | long nth = 1; |
| 7341 | colnr_T startcol = 0; |
| 7342 | int match = 0; |
| 7343 | list_T *l = NULL; |
| 7344 | listitem_T *li = NULL; |
| 7345 | long idx = 0; |
| 7346 | char_u *tofree = NULL; |
| 7347 | |
| 7348 | /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ |
| 7349 | save_cpo = p_cpo; |
| 7350 | p_cpo = (char_u *)""; |
| 7351 | |
| 7352 | rettv->vval.v_number = -1; |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7353 | if (type == MATCH_LIST || type == MATCH_POS) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7354 | { |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7355 | /* type MATCH_LIST: return empty list when there are no matches. |
| 7356 | * type MATCH_POS: return ["", -1, -1, -1] */ |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7357 | if (rettv_list_alloc(rettv) == FAIL) |
| 7358 | goto theend; |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7359 | if (type == MATCH_POS |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7360 | && (list_append_string(rettv->vval.v_list, |
| 7361 | (char_u *)"", 0) == FAIL |
| 7362 | || list_append_number(rettv->vval.v_list, |
| 7363 | (varnumber_T)-1) == FAIL |
| 7364 | || list_append_number(rettv->vval.v_list, |
| 7365 | (varnumber_T)-1) == FAIL |
| 7366 | || list_append_number(rettv->vval.v_list, |
| 7367 | (varnumber_T)-1) == FAIL)) |
| 7368 | { |
| 7369 | list_free(rettv->vval.v_list); |
| 7370 | rettv->vval.v_list = NULL; |
| 7371 | goto theend; |
| 7372 | } |
| 7373 | } |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7374 | else if (type == MATCH_STR) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7375 | { |
| 7376 | rettv->v_type = VAR_STRING; |
| 7377 | rettv->vval.v_string = NULL; |
| 7378 | } |
| 7379 | |
| 7380 | if (argvars[0].v_type == VAR_LIST) |
| 7381 | { |
| 7382 | if ((l = argvars[0].vval.v_list) == NULL) |
| 7383 | goto theend; |
| 7384 | li = l->lv_first; |
| 7385 | } |
| 7386 | else |
| 7387 | { |
| 7388 | expr = str = get_tv_string(&argvars[0]); |
| 7389 | len = (long)STRLEN(str); |
| 7390 | } |
| 7391 | |
| 7392 | pat = get_tv_string_buf_chk(&argvars[1], patbuf); |
| 7393 | if (pat == NULL) |
| 7394 | goto theend; |
| 7395 | |
| 7396 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 7397 | { |
| 7398 | int error = FALSE; |
| 7399 | |
| 7400 | start = (long)get_tv_number_chk(&argvars[2], &error); |
| 7401 | if (error) |
| 7402 | goto theend; |
| 7403 | if (l != NULL) |
| 7404 | { |
| 7405 | li = list_find(l, start); |
| 7406 | if (li == NULL) |
| 7407 | goto theend; |
| 7408 | idx = l->lv_idx; /* use the cached index */ |
| 7409 | } |
| 7410 | else |
| 7411 | { |
| 7412 | if (start < 0) |
| 7413 | start = 0; |
| 7414 | if (start > len) |
| 7415 | goto theend; |
| 7416 | /* When "count" argument is there ignore matches before "start", |
| 7417 | * otherwise skip part of the string. Differs when pattern is "^" |
| 7418 | * or "\<". */ |
| 7419 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 7420 | startcol = start; |
| 7421 | else |
| 7422 | { |
| 7423 | str += start; |
| 7424 | len -= start; |
| 7425 | } |
| 7426 | } |
| 7427 | |
| 7428 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 7429 | nth = (long)get_tv_number_chk(&argvars[3], &error); |
| 7430 | if (error) |
| 7431 | goto theend; |
| 7432 | } |
| 7433 | |
| 7434 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); |
| 7435 | if (regmatch.regprog != NULL) |
| 7436 | { |
| 7437 | regmatch.rm_ic = p_ic; |
| 7438 | |
| 7439 | for (;;) |
| 7440 | { |
| 7441 | if (l != NULL) |
| 7442 | { |
| 7443 | if (li == NULL) |
| 7444 | { |
| 7445 | match = FALSE; |
| 7446 | break; |
| 7447 | } |
| 7448 | vim_free(tofree); |
| 7449 | expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0); |
| 7450 | if (str == NULL) |
| 7451 | break; |
| 7452 | } |
| 7453 | |
| 7454 | match = vim_regexec_nl(®match, str, (colnr_T)startcol); |
| 7455 | |
| 7456 | if (match && --nth <= 0) |
| 7457 | break; |
| 7458 | if (l == NULL && !match) |
| 7459 | break; |
| 7460 | |
| 7461 | /* Advance to just after the match. */ |
| 7462 | if (l != NULL) |
| 7463 | { |
| 7464 | li = li->li_next; |
| 7465 | ++idx; |
| 7466 | } |
| 7467 | else |
| 7468 | { |
| 7469 | #ifdef FEAT_MBYTE |
| 7470 | startcol = (colnr_T)(regmatch.startp[0] |
| 7471 | + (*mb_ptr2len)(regmatch.startp[0]) - str); |
| 7472 | #else |
| 7473 | startcol = (colnr_T)(regmatch.startp[0] + 1 - str); |
| 7474 | #endif |
| 7475 | if (startcol > (colnr_T)len |
| 7476 | || str + startcol <= regmatch.startp[0]) |
| 7477 | { |
| 7478 | match = FALSE; |
| 7479 | break; |
| 7480 | } |
| 7481 | } |
| 7482 | } |
| 7483 | |
| 7484 | if (match) |
| 7485 | { |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7486 | if (type == MATCH_POS) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7487 | { |
| 7488 | listitem_T *li1 = rettv->vval.v_list->lv_first; |
| 7489 | listitem_T *li2 = li1->li_next; |
| 7490 | listitem_T *li3 = li2->li_next; |
| 7491 | listitem_T *li4 = li3->li_next; |
| 7492 | |
| 7493 | vim_free(li1->li_tv.vval.v_string); |
| 7494 | li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0], |
| 7495 | (int)(regmatch.endp[0] - regmatch.startp[0])); |
| 7496 | li3->li_tv.vval.v_number = |
| 7497 | (varnumber_T)(regmatch.startp[0] - expr); |
| 7498 | li4->li_tv.vval.v_number = |
| 7499 | (varnumber_T)(regmatch.endp[0] - expr); |
| 7500 | if (l != NULL) |
| 7501 | li2->li_tv.vval.v_number = (varnumber_T)idx; |
| 7502 | } |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7503 | else if (type == MATCH_LIST) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7504 | { |
| 7505 | int i; |
| 7506 | |
| 7507 | /* return list with matched string and submatches */ |
| 7508 | for (i = 0; i < NSUBEXP; ++i) |
| 7509 | { |
| 7510 | if (regmatch.endp[i] == NULL) |
| 7511 | { |
| 7512 | if (list_append_string(rettv->vval.v_list, |
| 7513 | (char_u *)"", 0) == FAIL) |
| 7514 | break; |
| 7515 | } |
| 7516 | else if (list_append_string(rettv->vval.v_list, |
| 7517 | regmatch.startp[i], |
| 7518 | (int)(regmatch.endp[i] - regmatch.startp[i])) |
| 7519 | == FAIL) |
| 7520 | break; |
| 7521 | } |
| 7522 | } |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7523 | else if (type == MATCH_STR) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7524 | { |
| 7525 | /* return matched string */ |
| 7526 | if (l != NULL) |
| 7527 | copy_tv(&li->li_tv, rettv); |
| 7528 | else |
| 7529 | rettv->vval.v_string = vim_strnsave(regmatch.startp[0], |
| 7530 | (int)(regmatch.endp[0] - regmatch.startp[0])); |
| 7531 | } |
| 7532 | else if (l != NULL) |
| 7533 | rettv->vval.v_number = idx; |
| 7534 | else |
| 7535 | { |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7536 | if (type != MATCH_END) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7537 | rettv->vval.v_number = |
| 7538 | (varnumber_T)(regmatch.startp[0] - str); |
| 7539 | else |
| 7540 | rettv->vval.v_number = |
| 7541 | (varnumber_T)(regmatch.endp[0] - str); |
| 7542 | rettv->vval.v_number += (varnumber_T)(str - expr); |
| 7543 | } |
| 7544 | } |
| 7545 | vim_regfree(regmatch.regprog); |
| 7546 | } |
| 7547 | |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7548 | theend: |
| 7549 | if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7550 | /* matchstrpos() without a list: drop the second item. */ |
| 7551 | listitem_remove(rettv->vval.v_list, |
| 7552 | rettv->vval.v_list->lv_first->li_next); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7553 | vim_free(tofree); |
| 7554 | p_cpo = save_cpo; |
| 7555 | } |
| 7556 | |
| 7557 | /* |
| 7558 | * "match()" function |
| 7559 | */ |
| 7560 | static void |
| 7561 | f_match(typval_T *argvars, typval_T *rettv) |
| 7562 | { |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7563 | find_some_match(argvars, rettv, MATCH_MATCH); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7564 | } |
| 7565 | |
| 7566 | /* |
| 7567 | * "matchadd()" function |
| 7568 | */ |
| 7569 | static void |
| 7570 | f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 7571 | { |
| 7572 | #ifdef FEAT_SEARCH_EXTRA |
| 7573 | char_u buf[NUMBUFLEN]; |
| 7574 | char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */ |
| 7575 | char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */ |
| 7576 | int prio = 10; /* default priority */ |
| 7577 | int id = -1; |
| 7578 | int error = FALSE; |
| 7579 | char_u *conceal_char = NULL; |
| 7580 | |
| 7581 | rettv->vval.v_number = -1; |
| 7582 | |
| 7583 | if (grp == NULL || pat == NULL) |
| 7584 | return; |
| 7585 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 7586 | { |
| 7587 | prio = (int)get_tv_number_chk(&argvars[2], &error); |
| 7588 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 7589 | { |
| 7590 | id = (int)get_tv_number_chk(&argvars[3], &error); |
| 7591 | if (argvars[4].v_type != VAR_UNKNOWN) |
| 7592 | { |
| 7593 | if (argvars[4].v_type != VAR_DICT) |
| 7594 | { |
| 7595 | EMSG(_(e_dictreq)); |
| 7596 | return; |
| 7597 | } |
| 7598 | if (dict_find(argvars[4].vval.v_dict, |
| 7599 | (char_u *)"conceal", -1) != NULL) |
| 7600 | conceal_char = get_dict_string(argvars[4].vval.v_dict, |
| 7601 | (char_u *)"conceal", FALSE); |
| 7602 | } |
| 7603 | } |
| 7604 | } |
| 7605 | if (error == TRUE) |
| 7606 | return; |
| 7607 | if (id >= 1 && id <= 3) |
| 7608 | { |
Bram Moolenaar | 5b30291 | 2016-08-24 22:11:55 +0200 | [diff] [blame] | 7609 | EMSGN(_("E798: ID is reserved for \":match\": %ld"), id); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7610 | return; |
| 7611 | } |
| 7612 | |
| 7613 | rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL, |
| 7614 | conceal_char); |
| 7615 | #endif |
| 7616 | } |
| 7617 | |
| 7618 | /* |
| 7619 | * "matchaddpos()" function |
| 7620 | */ |
| 7621 | static void |
| 7622 | f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 7623 | { |
| 7624 | #ifdef FEAT_SEARCH_EXTRA |
| 7625 | char_u buf[NUMBUFLEN]; |
| 7626 | char_u *group; |
| 7627 | int prio = 10; |
| 7628 | int id = -1; |
| 7629 | int error = FALSE; |
| 7630 | list_T *l; |
| 7631 | char_u *conceal_char = NULL; |
| 7632 | |
| 7633 | rettv->vval.v_number = -1; |
| 7634 | |
| 7635 | group = get_tv_string_buf_chk(&argvars[0], buf); |
| 7636 | if (group == NULL) |
| 7637 | return; |
| 7638 | |
| 7639 | if (argvars[1].v_type != VAR_LIST) |
| 7640 | { |
| 7641 | EMSG2(_(e_listarg), "matchaddpos()"); |
| 7642 | return; |
| 7643 | } |
| 7644 | l = argvars[1].vval.v_list; |
| 7645 | if (l == NULL) |
| 7646 | return; |
| 7647 | |
| 7648 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 7649 | { |
| 7650 | prio = (int)get_tv_number_chk(&argvars[2], &error); |
| 7651 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 7652 | { |
| 7653 | id = (int)get_tv_number_chk(&argvars[3], &error); |
| 7654 | if (argvars[4].v_type != VAR_UNKNOWN) |
| 7655 | { |
| 7656 | if (argvars[4].v_type != VAR_DICT) |
| 7657 | { |
| 7658 | EMSG(_(e_dictreq)); |
| 7659 | return; |
| 7660 | } |
| 7661 | if (dict_find(argvars[4].vval.v_dict, |
| 7662 | (char_u *)"conceal", -1) != NULL) |
| 7663 | conceal_char = get_dict_string(argvars[4].vval.v_dict, |
| 7664 | (char_u *)"conceal", FALSE); |
| 7665 | } |
| 7666 | } |
| 7667 | } |
| 7668 | if (error == TRUE) |
| 7669 | return; |
| 7670 | |
| 7671 | /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */ |
| 7672 | if (id == 1 || id == 2) |
| 7673 | { |
Bram Moolenaar | 5b30291 | 2016-08-24 22:11:55 +0200 | [diff] [blame] | 7674 | EMSGN(_("E798: ID is reserved for \":match\": %ld"), id); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7675 | return; |
| 7676 | } |
| 7677 | |
| 7678 | rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l, |
| 7679 | conceal_char); |
| 7680 | #endif |
| 7681 | } |
| 7682 | |
| 7683 | /* |
| 7684 | * "matcharg()" function |
| 7685 | */ |
| 7686 | static void |
| 7687 | f_matcharg(typval_T *argvars UNUSED, typval_T *rettv) |
| 7688 | { |
| 7689 | if (rettv_list_alloc(rettv) == OK) |
| 7690 | { |
| 7691 | #ifdef FEAT_SEARCH_EXTRA |
| 7692 | int id = (int)get_tv_number(&argvars[0]); |
| 7693 | matchitem_T *m; |
| 7694 | |
| 7695 | if (id >= 1 && id <= 3) |
| 7696 | { |
| 7697 | if ((m = (matchitem_T *)get_match(curwin, id)) != NULL) |
| 7698 | { |
| 7699 | list_append_string(rettv->vval.v_list, |
| 7700 | syn_id2name(m->hlg_id), -1); |
| 7701 | list_append_string(rettv->vval.v_list, m->pattern, -1); |
| 7702 | } |
| 7703 | else |
| 7704 | { |
| 7705 | list_append_string(rettv->vval.v_list, NULL, -1); |
| 7706 | list_append_string(rettv->vval.v_list, NULL, -1); |
| 7707 | } |
| 7708 | } |
| 7709 | #endif |
| 7710 | } |
| 7711 | } |
| 7712 | |
| 7713 | /* |
| 7714 | * "matchdelete()" function |
| 7715 | */ |
| 7716 | static void |
| 7717 | f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 7718 | { |
| 7719 | #ifdef FEAT_SEARCH_EXTRA |
| 7720 | rettv->vval.v_number = match_delete(curwin, |
| 7721 | (int)get_tv_number(&argvars[0]), TRUE); |
| 7722 | #endif |
| 7723 | } |
| 7724 | |
| 7725 | /* |
| 7726 | * "matchend()" function |
| 7727 | */ |
| 7728 | static void |
| 7729 | f_matchend(typval_T *argvars, typval_T *rettv) |
| 7730 | { |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7731 | find_some_match(argvars, rettv, MATCH_END); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7732 | } |
| 7733 | |
| 7734 | /* |
| 7735 | * "matchlist()" function |
| 7736 | */ |
| 7737 | static void |
| 7738 | f_matchlist(typval_T *argvars, typval_T *rettv) |
| 7739 | { |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7740 | find_some_match(argvars, rettv, MATCH_LIST); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7741 | } |
| 7742 | |
| 7743 | /* |
| 7744 | * "matchstr()" function |
| 7745 | */ |
| 7746 | static void |
| 7747 | f_matchstr(typval_T *argvars, typval_T *rettv) |
| 7748 | { |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7749 | find_some_match(argvars, rettv, MATCH_STR); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7750 | } |
| 7751 | |
| 7752 | /* |
| 7753 | * "matchstrpos()" function |
| 7754 | */ |
| 7755 | static void |
| 7756 | f_matchstrpos(typval_T *argvars, typval_T *rettv) |
| 7757 | { |
Bram Moolenaar | 8d9f0ef | 2017-08-27 13:51:01 +0200 | [diff] [blame] | 7758 | find_some_match(argvars, rettv, MATCH_POS); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7759 | } |
| 7760 | |
| 7761 | static void max_min(typval_T *argvars, typval_T *rettv, int domax); |
| 7762 | |
| 7763 | static void |
| 7764 | max_min(typval_T *argvars, typval_T *rettv, int domax) |
| 7765 | { |
| 7766 | varnumber_T n = 0; |
| 7767 | varnumber_T i; |
| 7768 | int error = FALSE; |
| 7769 | |
| 7770 | if (argvars[0].v_type == VAR_LIST) |
| 7771 | { |
| 7772 | list_T *l; |
| 7773 | listitem_T *li; |
| 7774 | |
| 7775 | l = argvars[0].vval.v_list; |
| 7776 | if (l != NULL) |
| 7777 | { |
| 7778 | li = l->lv_first; |
| 7779 | if (li != NULL) |
| 7780 | { |
| 7781 | n = get_tv_number_chk(&li->li_tv, &error); |
| 7782 | for (;;) |
| 7783 | { |
| 7784 | li = li->li_next; |
| 7785 | if (li == NULL) |
| 7786 | break; |
| 7787 | i = get_tv_number_chk(&li->li_tv, &error); |
| 7788 | if (domax ? i > n : i < n) |
| 7789 | n = i; |
| 7790 | } |
| 7791 | } |
| 7792 | } |
| 7793 | } |
| 7794 | else if (argvars[0].v_type == VAR_DICT) |
| 7795 | { |
| 7796 | dict_T *d; |
| 7797 | int first = TRUE; |
| 7798 | hashitem_T *hi; |
| 7799 | int todo; |
| 7800 | |
| 7801 | d = argvars[0].vval.v_dict; |
| 7802 | if (d != NULL) |
| 7803 | { |
| 7804 | todo = (int)d->dv_hashtab.ht_used; |
| 7805 | for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) |
| 7806 | { |
| 7807 | if (!HASHITEM_EMPTY(hi)) |
| 7808 | { |
| 7809 | --todo; |
| 7810 | i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error); |
| 7811 | if (first) |
| 7812 | { |
| 7813 | n = i; |
| 7814 | first = FALSE; |
| 7815 | } |
| 7816 | else if (domax ? i > n : i < n) |
| 7817 | n = i; |
| 7818 | } |
| 7819 | } |
| 7820 | } |
| 7821 | } |
| 7822 | else |
Bram Moolenaar | 26b8433 | 2016-09-04 21:42:36 +0200 | [diff] [blame] | 7823 | EMSG2(_(e_listdictarg), domax ? "max()" : "min()"); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7824 | rettv->vval.v_number = error ? 0 : n; |
| 7825 | } |
| 7826 | |
| 7827 | /* |
| 7828 | * "max()" function |
| 7829 | */ |
| 7830 | static void |
| 7831 | f_max(typval_T *argvars, typval_T *rettv) |
| 7832 | { |
| 7833 | max_min(argvars, rettv, TRUE); |
| 7834 | } |
| 7835 | |
| 7836 | /* |
| 7837 | * "min()" function |
| 7838 | */ |
| 7839 | static void |
| 7840 | f_min(typval_T *argvars, typval_T *rettv) |
| 7841 | { |
| 7842 | max_min(argvars, rettv, FALSE); |
| 7843 | } |
| 7844 | |
| 7845 | static int mkdir_recurse(char_u *dir, int prot); |
| 7846 | |
| 7847 | /* |
| 7848 | * Create the directory in which "dir" is located, and higher levels when |
| 7849 | * needed. |
Bram Moolenaar | 7860bac | 2017-04-09 15:03:15 +0200 | [diff] [blame] | 7850 | * Return OK or FAIL. |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7851 | */ |
| 7852 | static int |
| 7853 | mkdir_recurse(char_u *dir, int prot) |
| 7854 | { |
| 7855 | char_u *p; |
| 7856 | char_u *updir; |
| 7857 | int r = FAIL; |
| 7858 | |
| 7859 | /* Get end of directory name in "dir". |
| 7860 | * We're done when it's "/" or "c:/". */ |
| 7861 | p = gettail_sep(dir); |
| 7862 | if (p <= get_past_head(dir)) |
| 7863 | return OK; |
| 7864 | |
| 7865 | /* If the directory exists we're done. Otherwise: create it.*/ |
| 7866 | updir = vim_strnsave(dir, (int)(p - dir)); |
| 7867 | if (updir == NULL) |
| 7868 | return FAIL; |
| 7869 | if (mch_isdir(updir)) |
| 7870 | r = OK; |
| 7871 | else if (mkdir_recurse(updir, prot) == OK) |
| 7872 | r = vim_mkdir_emsg(updir, prot); |
| 7873 | vim_free(updir); |
| 7874 | return r; |
| 7875 | } |
| 7876 | |
| 7877 | #ifdef vim_mkdir |
| 7878 | /* |
| 7879 | * "mkdir()" function |
| 7880 | */ |
| 7881 | static void |
| 7882 | f_mkdir(typval_T *argvars, typval_T *rettv) |
| 7883 | { |
| 7884 | char_u *dir; |
| 7885 | char_u buf[NUMBUFLEN]; |
| 7886 | int prot = 0755; |
| 7887 | |
| 7888 | rettv->vval.v_number = FAIL; |
| 7889 | if (check_restricted() || check_secure()) |
| 7890 | return; |
| 7891 | |
| 7892 | dir = get_tv_string_buf(&argvars[0], buf); |
| 7893 | if (*dir == NUL) |
| 7894 | rettv->vval.v_number = FAIL; |
| 7895 | else |
| 7896 | { |
| 7897 | if (*gettail(dir) == NUL) |
| 7898 | /* remove trailing slashes */ |
| 7899 | *gettail_sep(dir) = NUL; |
| 7900 | |
| 7901 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 7902 | { |
| 7903 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 7904 | prot = (int)get_tv_number_chk(&argvars[2], NULL); |
| 7905 | if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0) |
| 7906 | mkdir_recurse(dir, prot); |
| 7907 | } |
| 7908 | rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot); |
| 7909 | } |
| 7910 | } |
| 7911 | #endif |
| 7912 | |
| 7913 | /* |
| 7914 | * "mode()" function |
| 7915 | */ |
| 7916 | static void |
| 7917 | f_mode(typval_T *argvars, typval_T *rettv) |
| 7918 | { |
| 7919 | char_u buf[3]; |
| 7920 | |
| 7921 | buf[1] = NUL; |
| 7922 | buf[2] = NUL; |
| 7923 | |
| 7924 | if (time_for_testing == 93784) |
| 7925 | { |
| 7926 | /* Testing the two-character code. */ |
| 7927 | buf[0] = 'x'; |
| 7928 | buf[1] = '!'; |
| 7929 | } |
Bram Moolenaar | 2bb7b6b | 2017-08-13 20:58:33 +0200 | [diff] [blame] | 7930 | #ifdef FEAT_TERMINAL |
| 7931 | else if (term_use_loop()) |
| 7932 | buf[0] = 't'; |
| 7933 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7934 | else if (VIsual_active) |
| 7935 | { |
| 7936 | if (VIsual_select) |
| 7937 | buf[0] = VIsual_mode + 's' - 'v'; |
| 7938 | else |
| 7939 | buf[0] = VIsual_mode; |
| 7940 | } |
| 7941 | else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE |
| 7942 | || State == CONFIRM) |
| 7943 | { |
| 7944 | buf[0] = 'r'; |
| 7945 | if (State == ASKMORE) |
| 7946 | buf[1] = 'm'; |
| 7947 | else if (State == CONFIRM) |
| 7948 | buf[1] = '?'; |
| 7949 | } |
| 7950 | else if (State == EXTERNCMD) |
| 7951 | buf[0] = '!'; |
| 7952 | else if (State & INSERT) |
| 7953 | { |
| 7954 | #ifdef FEAT_VREPLACE |
| 7955 | if (State & VREPLACE_FLAG) |
| 7956 | { |
| 7957 | buf[0] = 'R'; |
| 7958 | buf[1] = 'v'; |
| 7959 | } |
| 7960 | else |
| 7961 | #endif |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 7962 | { |
| 7963 | if (State & REPLACE_FLAG) |
| 7964 | buf[0] = 'R'; |
| 7965 | else |
| 7966 | buf[0] = 'i'; |
| 7967 | #ifdef FEAT_INS_EXPAND |
| 7968 | if (ins_compl_active()) |
| 7969 | buf[1] = 'c'; |
| 7970 | else if (ctrl_x_mode == 1) |
| 7971 | buf[1] = 'x'; |
| 7972 | #endif |
| 7973 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7974 | } |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 7975 | else if ((State & CMDLINE) || exmode_active) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7976 | { |
| 7977 | buf[0] = 'c'; |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 7978 | if (exmode_active == EXMODE_VIM) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7979 | buf[1] = 'v'; |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 7980 | else if (exmode_active == EXMODE_NORMAL) |
| 7981 | buf[1] = 'e'; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 7982 | } |
| 7983 | else |
| 7984 | { |
| 7985 | buf[0] = 'n'; |
| 7986 | if (finish_op) |
| 7987 | buf[1] = 'o'; |
| 7988 | } |
| 7989 | |
| 7990 | /* Clear out the minor mode when the argument is not a non-zero number or |
| 7991 | * non-empty string. */ |
| 7992 | if (!non_zero_arg(&argvars[0])) |
| 7993 | buf[1] = NUL; |
| 7994 | |
| 7995 | rettv->vval.v_string = vim_strsave(buf); |
| 7996 | rettv->v_type = VAR_STRING; |
| 7997 | } |
| 7998 | |
| 7999 | #if defined(FEAT_MZSCHEME) || defined(PROTO) |
| 8000 | /* |
| 8001 | * "mzeval()" function |
| 8002 | */ |
| 8003 | static void |
| 8004 | f_mzeval(typval_T *argvars, typval_T *rettv) |
| 8005 | { |
| 8006 | char_u *str; |
| 8007 | char_u buf[NUMBUFLEN]; |
| 8008 | |
| 8009 | str = get_tv_string_buf(&argvars[0], buf); |
| 8010 | do_mzeval(str, rettv); |
| 8011 | } |
| 8012 | |
| 8013 | void |
| 8014 | mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv) |
| 8015 | { |
| 8016 | typval_T argvars[3]; |
| 8017 | |
| 8018 | argvars[0].v_type = VAR_STRING; |
| 8019 | argvars[0].vval.v_string = name; |
| 8020 | copy_tv(args, &argvars[1]); |
| 8021 | argvars[2].v_type = VAR_UNKNOWN; |
| 8022 | f_call(argvars, rettv); |
| 8023 | clear_tv(&argvars[1]); |
| 8024 | } |
| 8025 | #endif |
| 8026 | |
| 8027 | /* |
| 8028 | * "nextnonblank()" function |
| 8029 | */ |
| 8030 | static void |
| 8031 | f_nextnonblank(typval_T *argvars, typval_T *rettv) |
| 8032 | { |
| 8033 | linenr_T lnum; |
| 8034 | |
| 8035 | for (lnum = get_tv_lnum(argvars); ; ++lnum) |
| 8036 | { |
| 8037 | if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count) |
| 8038 | { |
| 8039 | lnum = 0; |
| 8040 | break; |
| 8041 | } |
| 8042 | if (*skipwhite(ml_get(lnum)) != NUL) |
| 8043 | break; |
| 8044 | } |
| 8045 | rettv->vval.v_number = lnum; |
| 8046 | } |
| 8047 | |
| 8048 | /* |
| 8049 | * "nr2char()" function |
| 8050 | */ |
| 8051 | static void |
| 8052 | f_nr2char(typval_T *argvars, typval_T *rettv) |
| 8053 | { |
| 8054 | char_u buf[NUMBUFLEN]; |
| 8055 | |
| 8056 | #ifdef FEAT_MBYTE |
| 8057 | if (has_mbyte) |
| 8058 | { |
| 8059 | int utf8 = 0; |
| 8060 | |
| 8061 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 8062 | utf8 = (int)get_tv_number_chk(&argvars[1], NULL); |
| 8063 | if (utf8) |
| 8064 | buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL; |
| 8065 | else |
| 8066 | buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL; |
| 8067 | } |
| 8068 | else |
| 8069 | #endif |
| 8070 | { |
| 8071 | buf[0] = (char_u)get_tv_number(&argvars[0]); |
| 8072 | buf[1] = NUL; |
| 8073 | } |
| 8074 | rettv->v_type = VAR_STRING; |
| 8075 | rettv->vval.v_string = vim_strsave(buf); |
| 8076 | } |
| 8077 | |
| 8078 | /* |
| 8079 | * "or(expr, expr)" function |
| 8080 | */ |
| 8081 | static void |
| 8082 | f_or(typval_T *argvars, typval_T *rettv) |
| 8083 | { |
| 8084 | rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL) |
| 8085 | | get_tv_number_chk(&argvars[1], NULL); |
| 8086 | } |
| 8087 | |
| 8088 | /* |
| 8089 | * "pathshorten()" function |
| 8090 | */ |
| 8091 | static void |
| 8092 | f_pathshorten(typval_T *argvars, typval_T *rettv) |
| 8093 | { |
| 8094 | char_u *p; |
| 8095 | |
| 8096 | rettv->v_type = VAR_STRING; |
| 8097 | p = get_tv_string_chk(&argvars[0]); |
| 8098 | if (p == NULL) |
| 8099 | rettv->vval.v_string = NULL; |
| 8100 | else |
| 8101 | { |
| 8102 | p = vim_strsave(p); |
| 8103 | rettv->vval.v_string = p; |
| 8104 | if (p != NULL) |
| 8105 | shorten_dir(p); |
| 8106 | } |
| 8107 | } |
| 8108 | |
| 8109 | #ifdef FEAT_PERL |
| 8110 | /* |
| 8111 | * "perleval()" function |
| 8112 | */ |
| 8113 | static void |
| 8114 | f_perleval(typval_T *argvars, typval_T *rettv) |
| 8115 | { |
| 8116 | char_u *str; |
| 8117 | char_u buf[NUMBUFLEN]; |
| 8118 | |
| 8119 | str = get_tv_string_buf(&argvars[0], buf); |
| 8120 | do_perleval(str, rettv); |
| 8121 | } |
| 8122 | #endif |
| 8123 | |
| 8124 | #ifdef FEAT_FLOAT |
| 8125 | /* |
| 8126 | * "pow()" function |
| 8127 | */ |
| 8128 | static void |
| 8129 | f_pow(typval_T *argvars, typval_T *rettv) |
| 8130 | { |
| 8131 | float_T fx = 0.0, fy = 0.0; |
| 8132 | |
| 8133 | rettv->v_type = VAR_FLOAT; |
| 8134 | if (get_float_arg(argvars, &fx) == OK |
| 8135 | && get_float_arg(&argvars[1], &fy) == OK) |
| 8136 | rettv->vval.v_float = pow(fx, fy); |
| 8137 | else |
| 8138 | rettv->vval.v_float = 0.0; |
| 8139 | } |
| 8140 | #endif |
| 8141 | |
| 8142 | /* |
| 8143 | * "prevnonblank()" function |
| 8144 | */ |
| 8145 | static void |
| 8146 | f_prevnonblank(typval_T *argvars, typval_T *rettv) |
| 8147 | { |
| 8148 | linenr_T lnum; |
| 8149 | |
| 8150 | lnum = get_tv_lnum(argvars); |
| 8151 | if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) |
| 8152 | lnum = 0; |
| 8153 | else |
| 8154 | while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL) |
| 8155 | --lnum; |
| 8156 | rettv->vval.v_number = lnum; |
| 8157 | } |
| 8158 | |
| 8159 | /* This dummy va_list is here because: |
| 8160 | * - passing a NULL pointer doesn't work when va_list isn't a pointer |
| 8161 | * - locally in the function results in a "used before set" warning |
| 8162 | * - using va_start() to initialize it gives "function with fixed args" error */ |
| 8163 | static va_list ap; |
| 8164 | |
| 8165 | /* |
| 8166 | * "printf()" function |
| 8167 | */ |
| 8168 | static void |
| 8169 | f_printf(typval_T *argvars, typval_T *rettv) |
| 8170 | { |
| 8171 | char_u buf[NUMBUFLEN]; |
| 8172 | int len; |
| 8173 | char_u *s; |
| 8174 | int saved_did_emsg = did_emsg; |
| 8175 | char *fmt; |
| 8176 | |
| 8177 | rettv->v_type = VAR_STRING; |
| 8178 | rettv->vval.v_string = NULL; |
| 8179 | |
| 8180 | /* Get the required length, allocate the buffer and do it for real. */ |
| 8181 | did_emsg = FALSE; |
| 8182 | fmt = (char *)get_tv_string_buf(&argvars[0], buf); |
Bram Moolenaar | 8327d1d | 2017-07-11 22:34:51 +0200 | [diff] [blame] | 8183 | len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8184 | if (!did_emsg) |
| 8185 | { |
| 8186 | s = alloc(len + 1); |
| 8187 | if (s != NULL) |
| 8188 | { |
| 8189 | rettv->vval.v_string = s; |
Bram Moolenaar | 8327d1d | 2017-07-11 22:34:51 +0200 | [diff] [blame] | 8190 | (void)vim_vsnprintf_typval((char *)s, len + 1, fmt, |
| 8191 | ap, argvars + 1); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8192 | } |
| 8193 | } |
| 8194 | did_emsg |= saved_did_emsg; |
| 8195 | } |
| 8196 | |
| 8197 | /* |
| 8198 | * "pumvisible()" function |
| 8199 | */ |
| 8200 | static void |
| 8201 | f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 8202 | { |
| 8203 | #ifdef FEAT_INS_EXPAND |
| 8204 | if (pum_visible()) |
| 8205 | rettv->vval.v_number = 1; |
| 8206 | #endif |
| 8207 | } |
| 8208 | |
| 8209 | #ifdef FEAT_PYTHON3 |
| 8210 | /* |
| 8211 | * "py3eval()" function |
| 8212 | */ |
| 8213 | static void |
| 8214 | f_py3eval(typval_T *argvars, typval_T *rettv) |
| 8215 | { |
| 8216 | char_u *str; |
| 8217 | char_u buf[NUMBUFLEN]; |
| 8218 | |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 8219 | if (p_pyx == 0) |
| 8220 | p_pyx = 3; |
| 8221 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8222 | str = get_tv_string_buf(&argvars[0], buf); |
| 8223 | do_py3eval(str, rettv); |
| 8224 | } |
| 8225 | #endif |
| 8226 | |
| 8227 | #ifdef FEAT_PYTHON |
| 8228 | /* |
| 8229 | * "pyeval()" function |
| 8230 | */ |
| 8231 | static void |
| 8232 | f_pyeval(typval_T *argvars, typval_T *rettv) |
| 8233 | { |
| 8234 | char_u *str; |
| 8235 | char_u buf[NUMBUFLEN]; |
| 8236 | |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 8237 | if (p_pyx == 0) |
| 8238 | p_pyx = 2; |
| 8239 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8240 | str = get_tv_string_buf(&argvars[0], buf); |
| 8241 | do_pyeval(str, rettv); |
| 8242 | } |
| 8243 | #endif |
| 8244 | |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 8245 | #if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) |
| 8246 | /* |
| 8247 | * "pyxeval()" function |
| 8248 | */ |
| 8249 | static void |
| 8250 | f_pyxeval(typval_T *argvars, typval_T *rettv) |
| 8251 | { |
| 8252 | # if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3) |
| 8253 | init_pyxversion(); |
| 8254 | if (p_pyx == 2) |
| 8255 | f_pyeval(argvars, rettv); |
| 8256 | else |
| 8257 | f_py3eval(argvars, rettv); |
| 8258 | # elif defined(FEAT_PYTHON) |
| 8259 | f_pyeval(argvars, rettv); |
| 8260 | # elif defined(FEAT_PYTHON3) |
| 8261 | f_py3eval(argvars, rettv); |
| 8262 | # endif |
| 8263 | } |
| 8264 | #endif |
| 8265 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8266 | /* |
| 8267 | * "range()" function |
| 8268 | */ |
| 8269 | static void |
| 8270 | f_range(typval_T *argvars, typval_T *rettv) |
| 8271 | { |
| 8272 | varnumber_T start; |
| 8273 | varnumber_T end; |
| 8274 | varnumber_T stride = 1; |
| 8275 | varnumber_T i; |
| 8276 | int error = FALSE; |
| 8277 | |
| 8278 | start = get_tv_number_chk(&argvars[0], &error); |
| 8279 | if (argvars[1].v_type == VAR_UNKNOWN) |
| 8280 | { |
| 8281 | end = start - 1; |
| 8282 | start = 0; |
| 8283 | } |
| 8284 | else |
| 8285 | { |
| 8286 | end = get_tv_number_chk(&argvars[1], &error); |
| 8287 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 8288 | stride = get_tv_number_chk(&argvars[2], &error); |
| 8289 | } |
| 8290 | |
| 8291 | if (error) |
| 8292 | return; /* type error; errmsg already given */ |
| 8293 | if (stride == 0) |
| 8294 | EMSG(_("E726: Stride is zero")); |
| 8295 | else if (stride > 0 ? end + 1 < start : end - 1 > start) |
| 8296 | EMSG(_("E727: Start past end")); |
| 8297 | else |
| 8298 | { |
| 8299 | if (rettv_list_alloc(rettv) == OK) |
| 8300 | for (i = start; stride > 0 ? i <= end : i >= end; i += stride) |
| 8301 | if (list_append_number(rettv->vval.v_list, |
| 8302 | (varnumber_T)i) == FAIL) |
| 8303 | break; |
| 8304 | } |
| 8305 | } |
| 8306 | |
| 8307 | /* |
| 8308 | * "readfile()" function |
| 8309 | */ |
| 8310 | static void |
| 8311 | f_readfile(typval_T *argvars, typval_T *rettv) |
| 8312 | { |
| 8313 | int binary = FALSE; |
| 8314 | int failed = FALSE; |
| 8315 | char_u *fname; |
| 8316 | FILE *fd; |
| 8317 | char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */ |
| 8318 | int io_size = sizeof(buf); |
| 8319 | int readlen; /* size of last fread() */ |
| 8320 | char_u *prev = NULL; /* previously read bytes, if any */ |
| 8321 | long prevlen = 0; /* length of data in prev */ |
| 8322 | long prevsize = 0; /* size of prev buffer */ |
| 8323 | long maxline = MAXLNUM; |
| 8324 | long cnt = 0; |
| 8325 | char_u *p; /* position in buf */ |
| 8326 | char_u *start; /* start of current line */ |
| 8327 | |
| 8328 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 8329 | { |
| 8330 | if (STRCMP(get_tv_string(&argvars[1]), "b") == 0) |
| 8331 | binary = TRUE; |
| 8332 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 8333 | maxline = (long)get_tv_number(&argvars[2]); |
| 8334 | } |
| 8335 | |
| 8336 | if (rettv_list_alloc(rettv) == FAIL) |
| 8337 | return; |
| 8338 | |
| 8339 | /* Always open the file in binary mode, library functions have a mind of |
| 8340 | * their own about CR-LF conversion. */ |
| 8341 | fname = get_tv_string(&argvars[0]); |
| 8342 | if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL) |
| 8343 | { |
| 8344 | EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname); |
| 8345 | return; |
| 8346 | } |
| 8347 | |
| 8348 | while (cnt < maxline || maxline < 0) |
| 8349 | { |
| 8350 | readlen = (int)fread(buf, 1, io_size, fd); |
| 8351 | |
| 8352 | /* This for loop processes what was read, but is also entered at end |
| 8353 | * of file so that either: |
| 8354 | * - an incomplete line gets written |
| 8355 | * - a "binary" file gets an empty line at the end if it ends in a |
| 8356 | * newline. */ |
| 8357 | for (p = buf, start = buf; |
| 8358 | p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary)); |
| 8359 | ++p) |
| 8360 | { |
| 8361 | if (*p == '\n' || readlen <= 0) |
| 8362 | { |
| 8363 | listitem_T *li; |
| 8364 | char_u *s = NULL; |
| 8365 | long_u len = p - start; |
| 8366 | |
| 8367 | /* Finished a line. Remove CRs before NL. */ |
| 8368 | if (readlen > 0 && !binary) |
| 8369 | { |
| 8370 | while (len > 0 && start[len - 1] == '\r') |
| 8371 | --len; |
| 8372 | /* removal may cross back to the "prev" string */ |
| 8373 | if (len == 0) |
| 8374 | while (prevlen > 0 && prev[prevlen - 1] == '\r') |
| 8375 | --prevlen; |
| 8376 | } |
| 8377 | if (prevlen == 0) |
| 8378 | s = vim_strnsave(start, (int)len); |
| 8379 | else |
| 8380 | { |
| 8381 | /* Change "prev" buffer to be the right size. This way |
| 8382 | * the bytes are only copied once, and very long lines are |
| 8383 | * allocated only once. */ |
| 8384 | if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL) |
| 8385 | { |
| 8386 | mch_memmove(s + prevlen, start, len); |
| 8387 | s[prevlen + len] = NUL; |
| 8388 | prev = NULL; /* the list will own the string */ |
| 8389 | prevlen = prevsize = 0; |
| 8390 | } |
| 8391 | } |
| 8392 | if (s == NULL) |
| 8393 | { |
| 8394 | do_outofmem_msg((long_u) prevlen + len + 1); |
| 8395 | failed = TRUE; |
| 8396 | break; |
| 8397 | } |
| 8398 | |
| 8399 | if ((li = listitem_alloc()) == NULL) |
| 8400 | { |
| 8401 | vim_free(s); |
| 8402 | failed = TRUE; |
| 8403 | break; |
| 8404 | } |
| 8405 | li->li_tv.v_type = VAR_STRING; |
| 8406 | li->li_tv.v_lock = 0; |
| 8407 | li->li_tv.vval.v_string = s; |
| 8408 | list_append(rettv->vval.v_list, li); |
| 8409 | |
| 8410 | start = p + 1; /* step over newline */ |
| 8411 | if ((++cnt >= maxline && maxline >= 0) || readlen <= 0) |
| 8412 | break; |
| 8413 | } |
| 8414 | else if (*p == NUL) |
| 8415 | *p = '\n'; |
| 8416 | #ifdef FEAT_MBYTE |
| 8417 | /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this |
| 8418 | * when finding the BF and check the previous two bytes. */ |
| 8419 | else if (*p == 0xbf && enc_utf8 && !binary) |
| 8420 | { |
| 8421 | /* Find the two bytes before the 0xbf. If p is at buf, or buf |
| 8422 | * + 1, these may be in the "prev" string. */ |
| 8423 | char_u back1 = p >= buf + 1 ? p[-1] |
| 8424 | : prevlen >= 1 ? prev[prevlen - 1] : NUL; |
| 8425 | char_u back2 = p >= buf + 2 ? p[-2] |
| 8426 | : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1] |
| 8427 | : prevlen >= 2 ? prev[prevlen - 2] : NUL; |
| 8428 | |
| 8429 | if (back2 == 0xef && back1 == 0xbb) |
| 8430 | { |
| 8431 | char_u *dest = p - 2; |
| 8432 | |
| 8433 | /* Usually a BOM is at the beginning of a file, and so at |
| 8434 | * the beginning of a line; then we can just step over it. |
| 8435 | */ |
| 8436 | if (start == dest) |
| 8437 | start = p + 1; |
| 8438 | else |
| 8439 | { |
| 8440 | /* have to shuffle buf to close gap */ |
| 8441 | int adjust_prevlen = 0; |
| 8442 | |
| 8443 | if (dest < buf) |
| 8444 | { |
| 8445 | adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */ |
| 8446 | dest = buf; |
| 8447 | } |
| 8448 | if (readlen > p - buf + 1) |
| 8449 | mch_memmove(dest, p + 1, readlen - (p - buf) - 1); |
| 8450 | readlen -= 3 - adjust_prevlen; |
| 8451 | prevlen -= adjust_prevlen; |
| 8452 | p = dest - 1; |
| 8453 | } |
| 8454 | } |
| 8455 | } |
| 8456 | #endif |
| 8457 | } /* for */ |
| 8458 | |
| 8459 | if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0) |
| 8460 | break; |
| 8461 | if (start < p) |
| 8462 | { |
| 8463 | /* There's part of a line in buf, store it in "prev". */ |
| 8464 | if (p - start + prevlen >= prevsize) |
| 8465 | { |
| 8466 | /* need bigger "prev" buffer */ |
| 8467 | char_u *newprev; |
| 8468 | |
| 8469 | /* A common use case is ordinary text files and "prev" gets a |
| 8470 | * fragment of a line, so the first allocation is made |
| 8471 | * small, to avoid repeatedly 'allocing' large and |
| 8472 | * 'reallocing' small. */ |
| 8473 | if (prevsize == 0) |
| 8474 | prevsize = (long)(p - start); |
| 8475 | else |
| 8476 | { |
| 8477 | long grow50pc = (prevsize * 3) / 2; |
| 8478 | long growmin = (long)((p - start) * 2 + prevlen); |
| 8479 | prevsize = grow50pc > growmin ? grow50pc : growmin; |
| 8480 | } |
| 8481 | newprev = prev == NULL ? alloc(prevsize) |
| 8482 | : vim_realloc(prev, prevsize); |
| 8483 | if (newprev == NULL) |
| 8484 | { |
| 8485 | do_outofmem_msg((long_u)prevsize); |
| 8486 | failed = TRUE; |
| 8487 | break; |
| 8488 | } |
| 8489 | prev = newprev; |
| 8490 | } |
| 8491 | /* Add the line part to end of "prev". */ |
| 8492 | mch_memmove(prev + prevlen, start, p - start); |
| 8493 | prevlen += (long)(p - start); |
| 8494 | } |
| 8495 | } /* while */ |
| 8496 | |
| 8497 | /* |
| 8498 | * For a negative line count use only the lines at the end of the file, |
| 8499 | * free the rest. |
| 8500 | */ |
| 8501 | if (!failed && maxline < 0) |
| 8502 | while (cnt > -maxline) |
| 8503 | { |
| 8504 | listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first); |
| 8505 | --cnt; |
| 8506 | } |
| 8507 | |
| 8508 | if (failed) |
| 8509 | { |
| 8510 | list_free(rettv->vval.v_list); |
| 8511 | /* readfile doc says an empty list is returned on error */ |
| 8512 | rettv->vval.v_list = list_alloc(); |
| 8513 | } |
| 8514 | |
| 8515 | vim_free(prev); |
| 8516 | fclose(fd); |
| 8517 | } |
| 8518 | |
| 8519 | #if defined(FEAT_RELTIME) |
| 8520 | static int list2proftime(typval_T *arg, proftime_T *tm); |
| 8521 | |
| 8522 | /* |
| 8523 | * Convert a List to proftime_T. |
| 8524 | * Return FAIL when there is something wrong. |
| 8525 | */ |
| 8526 | static int |
| 8527 | list2proftime(typval_T *arg, proftime_T *tm) |
| 8528 | { |
| 8529 | long n1, n2; |
| 8530 | int error = FALSE; |
| 8531 | |
| 8532 | if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL |
| 8533 | || arg->vval.v_list->lv_len != 2) |
| 8534 | return FAIL; |
| 8535 | n1 = list_find_nr(arg->vval.v_list, 0L, &error); |
| 8536 | n2 = list_find_nr(arg->vval.v_list, 1L, &error); |
| 8537 | # ifdef WIN3264 |
| 8538 | tm->HighPart = n1; |
| 8539 | tm->LowPart = n2; |
| 8540 | # else |
| 8541 | tm->tv_sec = n1; |
| 8542 | tm->tv_usec = n2; |
| 8543 | # endif |
| 8544 | return error ? FAIL : OK; |
| 8545 | } |
| 8546 | #endif /* FEAT_RELTIME */ |
| 8547 | |
| 8548 | /* |
| 8549 | * "reltime()" function |
| 8550 | */ |
| 8551 | static void |
| 8552 | f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 8553 | { |
| 8554 | #ifdef FEAT_RELTIME |
| 8555 | proftime_T res; |
| 8556 | proftime_T start; |
| 8557 | |
| 8558 | if (argvars[0].v_type == VAR_UNKNOWN) |
| 8559 | { |
| 8560 | /* No arguments: get current time. */ |
| 8561 | profile_start(&res); |
| 8562 | } |
| 8563 | else if (argvars[1].v_type == VAR_UNKNOWN) |
| 8564 | { |
| 8565 | if (list2proftime(&argvars[0], &res) == FAIL) |
| 8566 | return; |
| 8567 | profile_end(&res); |
| 8568 | } |
| 8569 | else |
| 8570 | { |
| 8571 | /* Two arguments: compute the difference. */ |
| 8572 | if (list2proftime(&argvars[0], &start) == FAIL |
| 8573 | || list2proftime(&argvars[1], &res) == FAIL) |
| 8574 | return; |
| 8575 | profile_sub(&res, &start); |
| 8576 | } |
| 8577 | |
| 8578 | if (rettv_list_alloc(rettv) == OK) |
| 8579 | { |
| 8580 | long n1, n2; |
| 8581 | |
| 8582 | # ifdef WIN3264 |
| 8583 | n1 = res.HighPart; |
| 8584 | n2 = res.LowPart; |
| 8585 | # else |
| 8586 | n1 = res.tv_sec; |
| 8587 | n2 = res.tv_usec; |
| 8588 | # endif |
| 8589 | list_append_number(rettv->vval.v_list, (varnumber_T)n1); |
| 8590 | list_append_number(rettv->vval.v_list, (varnumber_T)n2); |
| 8591 | } |
| 8592 | #endif |
| 8593 | } |
| 8594 | |
| 8595 | #ifdef FEAT_FLOAT |
| 8596 | /* |
| 8597 | * "reltimefloat()" function |
| 8598 | */ |
| 8599 | static void |
| 8600 | f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv) |
| 8601 | { |
| 8602 | # ifdef FEAT_RELTIME |
| 8603 | proftime_T tm; |
| 8604 | # endif |
| 8605 | |
| 8606 | rettv->v_type = VAR_FLOAT; |
| 8607 | rettv->vval.v_float = 0; |
| 8608 | # ifdef FEAT_RELTIME |
| 8609 | if (list2proftime(&argvars[0], &tm) == OK) |
| 8610 | rettv->vval.v_float = profile_float(&tm); |
| 8611 | # endif |
| 8612 | } |
| 8613 | #endif |
| 8614 | |
| 8615 | /* |
| 8616 | * "reltimestr()" function |
| 8617 | */ |
| 8618 | static void |
| 8619 | f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv) |
| 8620 | { |
| 8621 | #ifdef FEAT_RELTIME |
| 8622 | proftime_T tm; |
| 8623 | #endif |
| 8624 | |
| 8625 | rettv->v_type = VAR_STRING; |
| 8626 | rettv->vval.v_string = NULL; |
| 8627 | #ifdef FEAT_RELTIME |
| 8628 | if (list2proftime(&argvars[0], &tm) == OK) |
| 8629 | rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm)); |
| 8630 | #endif |
| 8631 | } |
| 8632 | |
| 8633 | #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11) |
| 8634 | static void make_connection(void); |
| 8635 | static int check_connection(void); |
| 8636 | |
| 8637 | static void |
| 8638 | make_connection(void) |
| 8639 | { |
| 8640 | if (X_DISPLAY == NULL |
| 8641 | # ifdef FEAT_GUI |
| 8642 | && !gui.in_use |
| 8643 | # endif |
| 8644 | ) |
| 8645 | { |
| 8646 | x_force_connect = TRUE; |
| 8647 | setup_term_clip(); |
| 8648 | x_force_connect = FALSE; |
| 8649 | } |
| 8650 | } |
| 8651 | |
| 8652 | static int |
| 8653 | check_connection(void) |
| 8654 | { |
| 8655 | make_connection(); |
| 8656 | if (X_DISPLAY == NULL) |
| 8657 | { |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 8658 | EMSG(_("E240: No connection to the X server")); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8659 | return FAIL; |
| 8660 | } |
| 8661 | return OK; |
| 8662 | } |
| 8663 | #endif |
| 8664 | |
| 8665 | #ifdef FEAT_CLIENTSERVER |
| 8666 | static void |
| 8667 | remote_common(typval_T *argvars, typval_T *rettv, int expr) |
| 8668 | { |
| 8669 | char_u *server_name; |
| 8670 | char_u *keys; |
| 8671 | char_u *r = NULL; |
| 8672 | char_u buf[NUMBUFLEN]; |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 8673 | int timeout = 0; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8674 | # ifdef WIN32 |
| 8675 | HWND w; |
| 8676 | # else |
| 8677 | Window w; |
| 8678 | # endif |
| 8679 | |
| 8680 | if (check_restricted() || check_secure()) |
| 8681 | return; |
| 8682 | |
| 8683 | # ifdef FEAT_X11 |
| 8684 | if (check_connection() == FAIL) |
| 8685 | return; |
| 8686 | # endif |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 8687 | if (argvars[2].v_type != VAR_UNKNOWN |
| 8688 | && argvars[3].v_type != VAR_UNKNOWN) |
| 8689 | timeout = get_tv_number(&argvars[3]); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8690 | |
| 8691 | server_name = get_tv_string_chk(&argvars[0]); |
| 8692 | if (server_name == NULL) |
| 8693 | return; /* type error; errmsg already given */ |
| 8694 | keys = get_tv_string_buf(&argvars[1], buf); |
| 8695 | # ifdef WIN32 |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 8696 | if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8697 | # else |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 8698 | if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout, |
| 8699 | 0, TRUE) < 0) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8700 | # endif |
| 8701 | { |
| 8702 | if (r != NULL) |
Bram Moolenaar | 09d6c38 | 2017-09-09 16:25:54 +0200 | [diff] [blame] | 8703 | { |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8704 | EMSG(r); /* sending worked but evaluation failed */ |
Bram Moolenaar | 09d6c38 | 2017-09-09 16:25:54 +0200 | [diff] [blame] | 8705 | vim_free(r); |
| 8706 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8707 | else |
| 8708 | EMSG2(_("E241: Unable to send to %s"), server_name); |
| 8709 | return; |
| 8710 | } |
| 8711 | |
| 8712 | rettv->vval.v_string = r; |
| 8713 | |
| 8714 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 8715 | { |
| 8716 | dictitem_T v; |
| 8717 | char_u str[30]; |
| 8718 | char_u *idvar; |
| 8719 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8720 | idvar = get_tv_string_chk(&argvars[2]); |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 8721 | if (idvar != NULL && *idvar != NUL) |
| 8722 | { |
| 8723 | sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w); |
| 8724 | v.di_tv.v_type = VAR_STRING; |
| 8725 | v.di_tv.vval.v_string = vim_strsave(str); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8726 | set_var(idvar, &v.di_tv, FALSE); |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 8727 | vim_free(v.di_tv.vval.v_string); |
| 8728 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8729 | } |
| 8730 | } |
| 8731 | #endif |
| 8732 | |
| 8733 | /* |
| 8734 | * "remote_expr()" function |
| 8735 | */ |
| 8736 | static void |
| 8737 | f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv) |
| 8738 | { |
| 8739 | rettv->v_type = VAR_STRING; |
| 8740 | rettv->vval.v_string = NULL; |
| 8741 | #ifdef FEAT_CLIENTSERVER |
| 8742 | remote_common(argvars, rettv, TRUE); |
| 8743 | #endif |
| 8744 | } |
| 8745 | |
| 8746 | /* |
| 8747 | * "remote_foreground()" function |
| 8748 | */ |
| 8749 | static void |
| 8750 | f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 8751 | { |
| 8752 | #ifdef FEAT_CLIENTSERVER |
| 8753 | # ifdef WIN32 |
| 8754 | /* On Win32 it's done in this application. */ |
| 8755 | { |
| 8756 | char_u *server_name = get_tv_string_chk(&argvars[0]); |
| 8757 | |
| 8758 | if (server_name != NULL) |
| 8759 | serverForeground(server_name); |
| 8760 | } |
| 8761 | # else |
| 8762 | /* Send a foreground() expression to the server. */ |
| 8763 | argvars[1].v_type = VAR_STRING; |
| 8764 | argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()"); |
| 8765 | argvars[2].v_type = VAR_UNKNOWN; |
Bram Moolenaar | 09d6c38 | 2017-09-09 16:25:54 +0200 | [diff] [blame] | 8766 | rettv->v_type = VAR_STRING; |
| 8767 | rettv->vval.v_string = NULL; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8768 | remote_common(argvars, rettv, TRUE); |
| 8769 | vim_free(argvars[1].vval.v_string); |
| 8770 | # endif |
| 8771 | #endif |
| 8772 | } |
| 8773 | |
| 8774 | static void |
| 8775 | f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv) |
| 8776 | { |
| 8777 | #ifdef FEAT_CLIENTSERVER |
| 8778 | dictitem_T v; |
| 8779 | char_u *s = NULL; |
| 8780 | # ifdef WIN32 |
| 8781 | long_u n = 0; |
| 8782 | # endif |
| 8783 | char_u *serverid; |
| 8784 | |
| 8785 | if (check_restricted() || check_secure()) |
| 8786 | { |
| 8787 | rettv->vval.v_number = -1; |
| 8788 | return; |
| 8789 | } |
| 8790 | serverid = get_tv_string_chk(&argvars[0]); |
| 8791 | if (serverid == NULL) |
| 8792 | { |
| 8793 | rettv->vval.v_number = -1; |
| 8794 | return; /* type error; errmsg already given */ |
| 8795 | } |
| 8796 | # ifdef WIN32 |
| 8797 | sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n); |
| 8798 | if (n == 0) |
| 8799 | rettv->vval.v_number = -1; |
| 8800 | else |
| 8801 | { |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 8802 | s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8803 | rettv->vval.v_number = (s != NULL); |
| 8804 | } |
| 8805 | # else |
| 8806 | if (check_connection() == FAIL) |
| 8807 | return; |
| 8808 | |
| 8809 | rettv->vval.v_number = serverPeekReply(X_DISPLAY, |
| 8810 | serverStrToWin(serverid), &s); |
| 8811 | # endif |
| 8812 | |
| 8813 | if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0) |
| 8814 | { |
| 8815 | char_u *retvar; |
| 8816 | |
| 8817 | v.di_tv.v_type = VAR_STRING; |
| 8818 | v.di_tv.vval.v_string = vim_strsave(s); |
| 8819 | retvar = get_tv_string_chk(&argvars[1]); |
| 8820 | if (retvar != NULL) |
| 8821 | set_var(retvar, &v.di_tv, FALSE); |
| 8822 | vim_free(v.di_tv.vval.v_string); |
| 8823 | } |
| 8824 | #else |
| 8825 | rettv->vval.v_number = -1; |
| 8826 | #endif |
| 8827 | } |
| 8828 | |
| 8829 | static void |
| 8830 | f_remote_read(typval_T *argvars UNUSED, typval_T *rettv) |
| 8831 | { |
| 8832 | char_u *r = NULL; |
| 8833 | |
| 8834 | #ifdef FEAT_CLIENTSERVER |
| 8835 | char_u *serverid = get_tv_string_chk(&argvars[0]); |
| 8836 | |
| 8837 | if (serverid != NULL && !check_restricted() && !check_secure()) |
| 8838 | { |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 8839 | int timeout = 0; |
Bram Moolenaar | 1662ce1 | 2017-03-19 21:47:50 +0100 | [diff] [blame] | 8840 | # ifdef WIN32 |
| 8841 | /* The server's HWND is encoded in the 'id' parameter */ |
| 8842 | long_u n = 0; |
| 8843 | # endif |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 8844 | |
| 8845 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 8846 | timeout = get_tv_number(&argvars[1]); |
| 8847 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8848 | # ifdef WIN32 |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8849 | sscanf((char *)serverid, SCANF_HEX_LONG_U, &n); |
| 8850 | if (n != 0) |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 8851 | r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8852 | if (r == NULL) |
| 8853 | # else |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 8854 | if (check_connection() == FAIL |
| 8855 | || serverReadReply(X_DISPLAY, serverStrToWin(serverid), |
| 8856 | &r, FALSE, timeout) < 0) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8857 | # endif |
| 8858 | EMSG(_("E277: Unable to read a server reply")); |
| 8859 | } |
| 8860 | #endif |
| 8861 | rettv->v_type = VAR_STRING; |
| 8862 | rettv->vval.v_string = r; |
| 8863 | } |
| 8864 | |
| 8865 | /* |
| 8866 | * "remote_send()" function |
| 8867 | */ |
| 8868 | static void |
| 8869 | f_remote_send(typval_T *argvars UNUSED, typval_T *rettv) |
| 8870 | { |
| 8871 | rettv->v_type = VAR_STRING; |
| 8872 | rettv->vval.v_string = NULL; |
| 8873 | #ifdef FEAT_CLIENTSERVER |
| 8874 | remote_common(argvars, rettv, FALSE); |
| 8875 | #endif |
| 8876 | } |
| 8877 | |
| 8878 | /* |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 8879 | * "remote_startserver()" function |
| 8880 | */ |
| 8881 | static void |
| 8882 | f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 8883 | { |
| 8884 | #ifdef FEAT_CLIENTSERVER |
| 8885 | char_u *server = get_tv_string_chk(&argvars[0]); |
| 8886 | |
| 8887 | if (server == NULL) |
| 8888 | return; /* type error; errmsg already given */ |
| 8889 | if (serverName != NULL) |
| 8890 | EMSG(_("E941: already started a server")); |
| 8891 | else |
| 8892 | { |
| 8893 | # ifdef FEAT_X11 |
| 8894 | if (check_connection() == OK) |
| 8895 | serverRegisterName(X_DISPLAY, server); |
| 8896 | # else |
| 8897 | serverSetName(server); |
| 8898 | # endif |
| 8899 | } |
| 8900 | #else |
| 8901 | EMSG(_("E942: +clientserver feature not available")); |
| 8902 | #endif |
| 8903 | } |
| 8904 | |
| 8905 | /* |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 8906 | * "remove()" function |
| 8907 | */ |
| 8908 | static void |
| 8909 | f_remove(typval_T *argvars, typval_T *rettv) |
| 8910 | { |
| 8911 | list_T *l; |
| 8912 | listitem_T *item, *item2; |
| 8913 | listitem_T *li; |
| 8914 | long idx; |
| 8915 | long end; |
| 8916 | char_u *key; |
| 8917 | dict_T *d; |
| 8918 | dictitem_T *di; |
| 8919 | char_u *arg_errmsg = (char_u *)N_("remove() argument"); |
| 8920 | |
| 8921 | if (argvars[0].v_type == VAR_DICT) |
| 8922 | { |
| 8923 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 8924 | EMSG2(_(e_toomanyarg), "remove()"); |
| 8925 | else if ((d = argvars[0].vval.v_dict) != NULL |
| 8926 | && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE)) |
| 8927 | { |
| 8928 | key = get_tv_string_chk(&argvars[1]); |
| 8929 | if (key != NULL) |
| 8930 | { |
| 8931 | di = dict_find(d, key, -1); |
| 8932 | if (di == NULL) |
| 8933 | EMSG2(_(e_dictkey), key); |
| 8934 | else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE) |
| 8935 | && !var_check_ro(di->di_flags, arg_errmsg, TRUE)) |
| 8936 | { |
| 8937 | *rettv = di->di_tv; |
| 8938 | init_tv(&di->di_tv); |
| 8939 | dictitem_remove(d, di); |
| 8940 | } |
| 8941 | } |
| 8942 | } |
| 8943 | } |
| 8944 | else if (argvars[0].v_type != VAR_LIST) |
| 8945 | EMSG2(_(e_listdictarg), "remove()"); |
| 8946 | else if ((l = argvars[0].vval.v_list) != NULL |
| 8947 | && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE)) |
| 8948 | { |
| 8949 | int error = FALSE; |
| 8950 | |
| 8951 | idx = (long)get_tv_number_chk(&argvars[1], &error); |
| 8952 | if (error) |
| 8953 | ; /* type error: do nothing, errmsg already given */ |
| 8954 | else if ((item = list_find(l, idx)) == NULL) |
| 8955 | EMSGN(_(e_listidx), idx); |
| 8956 | else |
| 8957 | { |
| 8958 | if (argvars[2].v_type == VAR_UNKNOWN) |
| 8959 | { |
| 8960 | /* Remove one item, return its value. */ |
| 8961 | vimlist_remove(l, item, item); |
| 8962 | *rettv = item->li_tv; |
| 8963 | vim_free(item); |
| 8964 | } |
| 8965 | else |
| 8966 | { |
| 8967 | /* Remove range of items, return list with values. */ |
| 8968 | end = (long)get_tv_number_chk(&argvars[2], &error); |
| 8969 | if (error) |
| 8970 | ; /* type error: do nothing */ |
| 8971 | else if ((item2 = list_find(l, end)) == NULL) |
| 8972 | EMSGN(_(e_listidx), end); |
| 8973 | else |
| 8974 | { |
| 8975 | int cnt = 0; |
| 8976 | |
| 8977 | for (li = item; li != NULL; li = li->li_next) |
| 8978 | { |
| 8979 | ++cnt; |
| 8980 | if (li == item2) |
| 8981 | break; |
| 8982 | } |
| 8983 | if (li == NULL) /* didn't find "item2" after "item" */ |
| 8984 | EMSG(_(e_invrange)); |
| 8985 | else |
| 8986 | { |
| 8987 | vimlist_remove(l, item, item2); |
| 8988 | if (rettv_list_alloc(rettv) == OK) |
| 8989 | { |
| 8990 | l = rettv->vval.v_list; |
| 8991 | l->lv_first = item; |
| 8992 | l->lv_last = item2; |
| 8993 | item->li_prev = NULL; |
| 8994 | item2->li_next = NULL; |
| 8995 | l->lv_len = cnt; |
| 8996 | } |
| 8997 | } |
| 8998 | } |
| 8999 | } |
| 9000 | } |
| 9001 | } |
| 9002 | } |
| 9003 | |
| 9004 | /* |
| 9005 | * "rename({from}, {to})" function |
| 9006 | */ |
| 9007 | static void |
| 9008 | f_rename(typval_T *argvars, typval_T *rettv) |
| 9009 | { |
| 9010 | char_u buf[NUMBUFLEN]; |
| 9011 | |
| 9012 | if (check_restricted() || check_secure()) |
| 9013 | rettv->vval.v_number = -1; |
| 9014 | else |
| 9015 | rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]), |
| 9016 | get_tv_string_buf(&argvars[1], buf)); |
| 9017 | } |
| 9018 | |
| 9019 | /* |
| 9020 | * "repeat()" function |
| 9021 | */ |
| 9022 | static void |
| 9023 | f_repeat(typval_T *argvars, typval_T *rettv) |
| 9024 | { |
| 9025 | char_u *p; |
| 9026 | int n; |
| 9027 | int slen; |
| 9028 | int len; |
| 9029 | char_u *r; |
| 9030 | int i; |
| 9031 | |
| 9032 | n = (int)get_tv_number(&argvars[1]); |
| 9033 | if (argvars[0].v_type == VAR_LIST) |
| 9034 | { |
| 9035 | if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL) |
| 9036 | while (n-- > 0) |
| 9037 | if (list_extend(rettv->vval.v_list, |
| 9038 | argvars[0].vval.v_list, NULL) == FAIL) |
| 9039 | break; |
| 9040 | } |
| 9041 | else |
| 9042 | { |
| 9043 | p = get_tv_string(&argvars[0]); |
| 9044 | rettv->v_type = VAR_STRING; |
| 9045 | rettv->vval.v_string = NULL; |
| 9046 | |
| 9047 | slen = (int)STRLEN(p); |
| 9048 | len = slen * n; |
| 9049 | if (len <= 0) |
| 9050 | return; |
| 9051 | |
| 9052 | r = alloc(len + 1); |
| 9053 | if (r != NULL) |
| 9054 | { |
| 9055 | for (i = 0; i < n; i++) |
| 9056 | mch_memmove(r + i * slen, p, (size_t)slen); |
| 9057 | r[len] = NUL; |
| 9058 | } |
| 9059 | |
| 9060 | rettv->vval.v_string = r; |
| 9061 | } |
| 9062 | } |
| 9063 | |
| 9064 | /* |
| 9065 | * "resolve()" function |
| 9066 | */ |
| 9067 | static void |
| 9068 | f_resolve(typval_T *argvars, typval_T *rettv) |
| 9069 | { |
| 9070 | char_u *p; |
| 9071 | #ifdef HAVE_READLINK |
| 9072 | char_u *buf = NULL; |
| 9073 | #endif |
| 9074 | |
| 9075 | p = get_tv_string(&argvars[0]); |
| 9076 | #ifdef FEAT_SHORTCUT |
| 9077 | { |
| 9078 | char_u *v = NULL; |
| 9079 | |
| 9080 | v = mch_resolve_shortcut(p); |
| 9081 | if (v != NULL) |
| 9082 | rettv->vval.v_string = v; |
| 9083 | else |
| 9084 | rettv->vval.v_string = vim_strsave(p); |
| 9085 | } |
| 9086 | #else |
| 9087 | # ifdef HAVE_READLINK |
| 9088 | { |
| 9089 | char_u *cpy; |
| 9090 | int len; |
| 9091 | char_u *remain = NULL; |
| 9092 | char_u *q; |
| 9093 | int is_relative_to_current = FALSE; |
| 9094 | int has_trailing_pathsep = FALSE; |
| 9095 | int limit = 100; |
| 9096 | |
| 9097 | p = vim_strsave(p); |
| 9098 | |
| 9099 | if (p[0] == '.' && (vim_ispathsep(p[1]) |
| 9100 | || (p[1] == '.' && (vim_ispathsep(p[2]))))) |
| 9101 | is_relative_to_current = TRUE; |
| 9102 | |
| 9103 | len = STRLEN(p); |
| 9104 | if (len > 0 && after_pathsep(p, p + len)) |
| 9105 | { |
| 9106 | has_trailing_pathsep = TRUE; |
| 9107 | p[len - 1] = NUL; /* the trailing slash breaks readlink() */ |
| 9108 | } |
| 9109 | |
| 9110 | q = getnextcomp(p); |
| 9111 | if (*q != NUL) |
| 9112 | { |
| 9113 | /* Separate the first path component in "p", and keep the |
| 9114 | * remainder (beginning with the path separator). */ |
| 9115 | remain = vim_strsave(q - 1); |
| 9116 | q[-1] = NUL; |
| 9117 | } |
| 9118 | |
| 9119 | buf = alloc(MAXPATHL + 1); |
| 9120 | if (buf == NULL) |
| 9121 | goto fail; |
| 9122 | |
| 9123 | for (;;) |
| 9124 | { |
| 9125 | for (;;) |
| 9126 | { |
| 9127 | len = readlink((char *)p, (char *)buf, MAXPATHL); |
| 9128 | if (len <= 0) |
| 9129 | break; |
| 9130 | buf[len] = NUL; |
| 9131 | |
| 9132 | if (limit-- == 0) |
| 9133 | { |
| 9134 | vim_free(p); |
| 9135 | vim_free(remain); |
| 9136 | EMSG(_("E655: Too many symbolic links (cycle?)")); |
| 9137 | rettv->vval.v_string = NULL; |
| 9138 | goto fail; |
| 9139 | } |
| 9140 | |
| 9141 | /* Ensure that the result will have a trailing path separator |
| 9142 | * if the argument has one. */ |
| 9143 | if (remain == NULL && has_trailing_pathsep) |
| 9144 | add_pathsep(buf); |
| 9145 | |
| 9146 | /* Separate the first path component in the link value and |
| 9147 | * concatenate the remainders. */ |
| 9148 | q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf); |
| 9149 | if (*q != NUL) |
| 9150 | { |
| 9151 | if (remain == NULL) |
| 9152 | remain = vim_strsave(q - 1); |
| 9153 | else |
| 9154 | { |
| 9155 | cpy = concat_str(q - 1, remain); |
| 9156 | if (cpy != NULL) |
| 9157 | { |
| 9158 | vim_free(remain); |
| 9159 | remain = cpy; |
| 9160 | } |
| 9161 | } |
| 9162 | q[-1] = NUL; |
| 9163 | } |
| 9164 | |
| 9165 | q = gettail(p); |
| 9166 | if (q > p && *q == NUL) |
| 9167 | { |
| 9168 | /* Ignore trailing path separator. */ |
| 9169 | q[-1] = NUL; |
| 9170 | q = gettail(p); |
| 9171 | } |
| 9172 | if (q > p && !mch_isFullName(buf)) |
| 9173 | { |
| 9174 | /* symlink is relative to directory of argument */ |
| 9175 | cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1)); |
| 9176 | if (cpy != NULL) |
| 9177 | { |
| 9178 | STRCPY(cpy, p); |
| 9179 | STRCPY(gettail(cpy), buf); |
| 9180 | vim_free(p); |
| 9181 | p = cpy; |
| 9182 | } |
| 9183 | } |
| 9184 | else |
| 9185 | { |
| 9186 | vim_free(p); |
| 9187 | p = vim_strsave(buf); |
| 9188 | } |
| 9189 | } |
| 9190 | |
| 9191 | if (remain == NULL) |
| 9192 | break; |
| 9193 | |
| 9194 | /* Append the first path component of "remain" to "p". */ |
| 9195 | q = getnextcomp(remain + 1); |
| 9196 | len = q - remain - (*q != NUL); |
| 9197 | cpy = vim_strnsave(p, STRLEN(p) + len); |
| 9198 | if (cpy != NULL) |
| 9199 | { |
| 9200 | STRNCAT(cpy, remain, len); |
| 9201 | vim_free(p); |
| 9202 | p = cpy; |
| 9203 | } |
| 9204 | /* Shorten "remain". */ |
| 9205 | if (*q != NUL) |
| 9206 | STRMOVE(remain, q - 1); |
| 9207 | else |
| 9208 | { |
| 9209 | vim_free(remain); |
| 9210 | remain = NULL; |
| 9211 | } |
| 9212 | } |
| 9213 | |
| 9214 | /* If the result is a relative path name, make it explicitly relative to |
| 9215 | * the current directory if and only if the argument had this form. */ |
| 9216 | if (!vim_ispathsep(*p)) |
| 9217 | { |
| 9218 | if (is_relative_to_current |
| 9219 | && *p != NUL |
| 9220 | && !(p[0] == '.' |
| 9221 | && (p[1] == NUL |
| 9222 | || vim_ispathsep(p[1]) |
| 9223 | || (p[1] == '.' |
| 9224 | && (p[2] == NUL |
| 9225 | || vim_ispathsep(p[2])))))) |
| 9226 | { |
| 9227 | /* Prepend "./". */ |
| 9228 | cpy = concat_str((char_u *)"./", p); |
| 9229 | if (cpy != NULL) |
| 9230 | { |
| 9231 | vim_free(p); |
| 9232 | p = cpy; |
| 9233 | } |
| 9234 | } |
| 9235 | else if (!is_relative_to_current) |
| 9236 | { |
| 9237 | /* Strip leading "./". */ |
| 9238 | q = p; |
| 9239 | while (q[0] == '.' && vim_ispathsep(q[1])) |
| 9240 | q += 2; |
| 9241 | if (q > p) |
| 9242 | STRMOVE(p, p + 2); |
| 9243 | } |
| 9244 | } |
| 9245 | |
| 9246 | /* Ensure that the result will have no trailing path separator |
| 9247 | * if the argument had none. But keep "/" or "//". */ |
| 9248 | if (!has_trailing_pathsep) |
| 9249 | { |
| 9250 | q = p + STRLEN(p); |
| 9251 | if (after_pathsep(p, q)) |
| 9252 | *gettail_sep(p) = NUL; |
| 9253 | } |
| 9254 | |
| 9255 | rettv->vval.v_string = p; |
| 9256 | } |
| 9257 | # else |
| 9258 | rettv->vval.v_string = vim_strsave(p); |
| 9259 | # endif |
| 9260 | #endif |
| 9261 | |
| 9262 | simplify_filename(rettv->vval.v_string); |
| 9263 | |
| 9264 | #ifdef HAVE_READLINK |
| 9265 | fail: |
| 9266 | vim_free(buf); |
| 9267 | #endif |
| 9268 | rettv->v_type = VAR_STRING; |
| 9269 | } |
| 9270 | |
| 9271 | /* |
| 9272 | * "reverse({list})" function |
| 9273 | */ |
| 9274 | static void |
| 9275 | f_reverse(typval_T *argvars, typval_T *rettv) |
| 9276 | { |
| 9277 | list_T *l; |
| 9278 | listitem_T *li, *ni; |
| 9279 | |
| 9280 | if (argvars[0].v_type != VAR_LIST) |
| 9281 | EMSG2(_(e_listarg), "reverse()"); |
| 9282 | else if ((l = argvars[0].vval.v_list) != NULL |
| 9283 | && !tv_check_lock(l->lv_lock, |
| 9284 | (char_u *)N_("reverse() argument"), TRUE)) |
| 9285 | { |
| 9286 | li = l->lv_last; |
| 9287 | l->lv_first = l->lv_last = NULL; |
| 9288 | l->lv_len = 0; |
| 9289 | while (li != NULL) |
| 9290 | { |
| 9291 | ni = li->li_prev; |
| 9292 | list_append(l, li); |
| 9293 | li = ni; |
| 9294 | } |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 9295 | rettv_list_set(rettv, l); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9296 | l->lv_idx = l->lv_len - l->lv_idx - 1; |
| 9297 | } |
| 9298 | } |
| 9299 | |
| 9300 | #define SP_NOMOVE 0x01 /* don't move cursor */ |
| 9301 | #define SP_REPEAT 0x02 /* repeat to find outer pair */ |
| 9302 | #define SP_RETCOUNT 0x04 /* return matchcount */ |
| 9303 | #define SP_SETPCMARK 0x08 /* set previous context mark */ |
| 9304 | #define SP_START 0x10 /* accept match at start position */ |
| 9305 | #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */ |
| 9306 | #define SP_END 0x40 /* leave cursor at end of match */ |
| 9307 | #define SP_COLUMN 0x80 /* start at cursor column */ |
| 9308 | |
| 9309 | static int get_search_arg(typval_T *varp, int *flagsp); |
| 9310 | |
| 9311 | /* |
| 9312 | * Get flags for a search function. |
| 9313 | * Possibly sets "p_ws". |
| 9314 | * Returns BACKWARD, FORWARD or zero (for an error). |
| 9315 | */ |
| 9316 | static int |
| 9317 | get_search_arg(typval_T *varp, int *flagsp) |
| 9318 | { |
| 9319 | int dir = FORWARD; |
| 9320 | char_u *flags; |
| 9321 | char_u nbuf[NUMBUFLEN]; |
| 9322 | int mask; |
| 9323 | |
| 9324 | if (varp->v_type != VAR_UNKNOWN) |
| 9325 | { |
| 9326 | flags = get_tv_string_buf_chk(varp, nbuf); |
| 9327 | if (flags == NULL) |
| 9328 | return 0; /* type error; errmsg already given */ |
| 9329 | while (*flags != NUL) |
| 9330 | { |
| 9331 | switch (*flags) |
| 9332 | { |
| 9333 | case 'b': dir = BACKWARD; break; |
| 9334 | case 'w': p_ws = TRUE; break; |
| 9335 | case 'W': p_ws = FALSE; break; |
| 9336 | default: mask = 0; |
| 9337 | if (flagsp != NULL) |
| 9338 | switch (*flags) |
| 9339 | { |
| 9340 | case 'c': mask = SP_START; break; |
| 9341 | case 'e': mask = SP_END; break; |
| 9342 | case 'm': mask = SP_RETCOUNT; break; |
| 9343 | case 'n': mask = SP_NOMOVE; break; |
| 9344 | case 'p': mask = SP_SUBPAT; break; |
| 9345 | case 'r': mask = SP_REPEAT; break; |
| 9346 | case 's': mask = SP_SETPCMARK; break; |
| 9347 | case 'z': mask = SP_COLUMN; break; |
| 9348 | } |
| 9349 | if (mask == 0) |
| 9350 | { |
| 9351 | EMSG2(_(e_invarg2), flags); |
| 9352 | dir = 0; |
| 9353 | } |
| 9354 | else |
| 9355 | *flagsp |= mask; |
| 9356 | } |
| 9357 | if (dir == 0) |
| 9358 | break; |
| 9359 | ++flags; |
| 9360 | } |
| 9361 | } |
| 9362 | return dir; |
| 9363 | } |
| 9364 | |
| 9365 | /* |
| 9366 | * Shared by search() and searchpos() functions. |
| 9367 | */ |
| 9368 | static int |
| 9369 | search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp) |
| 9370 | { |
| 9371 | int flags; |
| 9372 | char_u *pat; |
| 9373 | pos_T pos; |
| 9374 | pos_T save_cursor; |
| 9375 | int save_p_ws = p_ws; |
| 9376 | int dir; |
| 9377 | int retval = 0; /* default: FAIL */ |
| 9378 | long lnum_stop = 0; |
| 9379 | proftime_T tm; |
| 9380 | #ifdef FEAT_RELTIME |
| 9381 | long time_limit = 0; |
| 9382 | #endif |
| 9383 | int options = SEARCH_KEEP; |
| 9384 | int subpatnum; |
| 9385 | |
| 9386 | pat = get_tv_string(&argvars[0]); |
| 9387 | dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */ |
| 9388 | if (dir == 0) |
| 9389 | goto theend; |
| 9390 | flags = *flagsp; |
| 9391 | if (flags & SP_START) |
| 9392 | options |= SEARCH_START; |
| 9393 | if (flags & SP_END) |
| 9394 | options |= SEARCH_END; |
| 9395 | if (flags & SP_COLUMN) |
| 9396 | options |= SEARCH_COL; |
| 9397 | |
| 9398 | /* Optional arguments: line number to stop searching and timeout. */ |
| 9399 | if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN) |
| 9400 | { |
| 9401 | lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL); |
| 9402 | if (lnum_stop < 0) |
| 9403 | goto theend; |
| 9404 | #ifdef FEAT_RELTIME |
| 9405 | if (argvars[3].v_type != VAR_UNKNOWN) |
| 9406 | { |
| 9407 | time_limit = (long)get_tv_number_chk(&argvars[3], NULL); |
| 9408 | if (time_limit < 0) |
| 9409 | goto theend; |
| 9410 | } |
| 9411 | #endif |
| 9412 | } |
| 9413 | |
| 9414 | #ifdef FEAT_RELTIME |
| 9415 | /* Set the time limit, if there is one. */ |
| 9416 | profile_setlimit(time_limit, &tm); |
| 9417 | #endif |
| 9418 | |
| 9419 | /* |
| 9420 | * This function does not accept SP_REPEAT and SP_RETCOUNT flags. |
| 9421 | * Check to make sure only those flags are set. |
| 9422 | * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both |
| 9423 | * flags cannot be set. Check for that condition also. |
| 9424 | */ |
| 9425 | if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0) |
| 9426 | || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))) |
| 9427 | { |
| 9428 | EMSG2(_(e_invarg2), get_tv_string(&argvars[1])); |
| 9429 | goto theend; |
| 9430 | } |
| 9431 | |
| 9432 | pos = save_cursor = curwin->w_cursor; |
| 9433 | subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L, |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 9434 | options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9435 | if (subpatnum != FAIL) |
| 9436 | { |
| 9437 | if (flags & SP_SUBPAT) |
| 9438 | retval = subpatnum; |
| 9439 | else |
| 9440 | retval = pos.lnum; |
| 9441 | if (flags & SP_SETPCMARK) |
| 9442 | setpcmark(); |
| 9443 | curwin->w_cursor = pos; |
| 9444 | if (match_pos != NULL) |
| 9445 | { |
| 9446 | /* Store the match cursor position */ |
| 9447 | match_pos->lnum = pos.lnum; |
| 9448 | match_pos->col = pos.col + 1; |
| 9449 | } |
| 9450 | /* "/$" will put the cursor after the end of the line, may need to |
| 9451 | * correct that here */ |
| 9452 | check_cursor(); |
| 9453 | } |
| 9454 | |
| 9455 | /* If 'n' flag is used: restore cursor position. */ |
| 9456 | if (flags & SP_NOMOVE) |
| 9457 | curwin->w_cursor = save_cursor; |
| 9458 | else |
| 9459 | curwin->w_set_curswant = TRUE; |
| 9460 | theend: |
| 9461 | p_ws = save_p_ws; |
| 9462 | |
| 9463 | return retval; |
| 9464 | } |
| 9465 | |
| 9466 | #ifdef FEAT_FLOAT |
| 9467 | |
| 9468 | /* |
| 9469 | * round() is not in C90, use ceil() or floor() instead. |
| 9470 | */ |
| 9471 | float_T |
| 9472 | vim_round(float_T f) |
| 9473 | { |
| 9474 | return f > 0 ? floor(f + 0.5) : ceil(f - 0.5); |
| 9475 | } |
| 9476 | |
| 9477 | /* |
| 9478 | * "round({float})" function |
| 9479 | */ |
| 9480 | static void |
| 9481 | f_round(typval_T *argvars, typval_T *rettv) |
| 9482 | { |
| 9483 | float_T f = 0.0; |
| 9484 | |
| 9485 | rettv->v_type = VAR_FLOAT; |
| 9486 | if (get_float_arg(argvars, &f) == OK) |
| 9487 | rettv->vval.v_float = vim_round(f); |
| 9488 | else |
| 9489 | rettv->vval.v_float = 0.0; |
| 9490 | } |
| 9491 | #endif |
| 9492 | |
| 9493 | /* |
| 9494 | * "screenattr()" function |
| 9495 | */ |
| 9496 | static void |
| 9497 | f_screenattr(typval_T *argvars, typval_T *rettv) |
| 9498 | { |
| 9499 | int row; |
| 9500 | int col; |
| 9501 | int c; |
| 9502 | |
| 9503 | row = (int)get_tv_number_chk(&argvars[0], NULL) - 1; |
| 9504 | col = (int)get_tv_number_chk(&argvars[1], NULL) - 1; |
| 9505 | if (row < 0 || row >= screen_Rows |
| 9506 | || col < 0 || col >= screen_Columns) |
| 9507 | c = -1; |
| 9508 | else |
| 9509 | c = ScreenAttrs[LineOffset[row] + col]; |
| 9510 | rettv->vval.v_number = c; |
| 9511 | } |
| 9512 | |
| 9513 | /* |
| 9514 | * "screenchar()" function |
| 9515 | */ |
| 9516 | static void |
| 9517 | f_screenchar(typval_T *argvars, typval_T *rettv) |
| 9518 | { |
| 9519 | int row; |
| 9520 | int col; |
| 9521 | int off; |
| 9522 | int c; |
| 9523 | |
| 9524 | row = (int)get_tv_number_chk(&argvars[0], NULL) - 1; |
| 9525 | col = (int)get_tv_number_chk(&argvars[1], NULL) - 1; |
| 9526 | if (row < 0 || row >= screen_Rows |
| 9527 | || col < 0 || col >= screen_Columns) |
| 9528 | c = -1; |
| 9529 | else |
| 9530 | { |
| 9531 | off = LineOffset[row] + col; |
| 9532 | #ifdef FEAT_MBYTE |
| 9533 | if (enc_utf8 && ScreenLinesUC[off] != 0) |
| 9534 | c = ScreenLinesUC[off]; |
| 9535 | else |
| 9536 | #endif |
| 9537 | c = ScreenLines[off]; |
| 9538 | } |
| 9539 | rettv->vval.v_number = c; |
| 9540 | } |
| 9541 | |
| 9542 | /* |
| 9543 | * "screencol()" function |
| 9544 | * |
| 9545 | * First column is 1 to be consistent with virtcol(). |
| 9546 | */ |
| 9547 | static void |
| 9548 | f_screencol(typval_T *argvars UNUSED, typval_T *rettv) |
| 9549 | { |
| 9550 | rettv->vval.v_number = screen_screencol() + 1; |
| 9551 | } |
| 9552 | |
| 9553 | /* |
| 9554 | * "screenrow()" function |
| 9555 | */ |
| 9556 | static void |
| 9557 | f_screenrow(typval_T *argvars UNUSED, typval_T *rettv) |
| 9558 | { |
| 9559 | rettv->vval.v_number = screen_screenrow() + 1; |
| 9560 | } |
| 9561 | |
| 9562 | /* |
| 9563 | * "search()" function |
| 9564 | */ |
| 9565 | static void |
| 9566 | f_search(typval_T *argvars, typval_T *rettv) |
| 9567 | { |
| 9568 | int flags = 0; |
| 9569 | |
| 9570 | rettv->vval.v_number = search_cmn(argvars, NULL, &flags); |
| 9571 | } |
| 9572 | |
| 9573 | /* |
| 9574 | * "searchdecl()" function |
| 9575 | */ |
| 9576 | static void |
| 9577 | f_searchdecl(typval_T *argvars, typval_T *rettv) |
| 9578 | { |
| 9579 | int locally = 1; |
| 9580 | int thisblock = 0; |
| 9581 | int error = FALSE; |
| 9582 | char_u *name; |
| 9583 | |
| 9584 | rettv->vval.v_number = 1; /* default: FAIL */ |
| 9585 | |
| 9586 | name = get_tv_string_chk(&argvars[0]); |
| 9587 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 9588 | { |
| 9589 | locally = (int)get_tv_number_chk(&argvars[1], &error) == 0; |
| 9590 | if (!error && argvars[2].v_type != VAR_UNKNOWN) |
| 9591 | thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0; |
| 9592 | } |
| 9593 | if (!error && name != NULL) |
| 9594 | rettv->vval.v_number = find_decl(name, (int)STRLEN(name), |
| 9595 | locally, thisblock, SEARCH_KEEP) == FAIL; |
| 9596 | } |
| 9597 | |
| 9598 | /* |
| 9599 | * Used by searchpair() and searchpairpos() |
| 9600 | */ |
| 9601 | static int |
| 9602 | searchpair_cmn(typval_T *argvars, pos_T *match_pos) |
| 9603 | { |
| 9604 | char_u *spat, *mpat, *epat; |
Bram Moolenaar | 4857048 | 2017-10-30 21:48:41 +0100 | [diff] [blame] | 9605 | typval_T *skip; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9606 | int save_p_ws = p_ws; |
| 9607 | int dir; |
| 9608 | int flags = 0; |
| 9609 | char_u nbuf1[NUMBUFLEN]; |
| 9610 | char_u nbuf2[NUMBUFLEN]; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9611 | int retval = 0; /* default: FAIL */ |
| 9612 | long lnum_stop = 0; |
| 9613 | long time_limit = 0; |
| 9614 | |
| 9615 | /* Get the three pattern arguments: start, middle, end. */ |
| 9616 | spat = get_tv_string_chk(&argvars[0]); |
| 9617 | mpat = get_tv_string_buf_chk(&argvars[1], nbuf1); |
| 9618 | epat = get_tv_string_buf_chk(&argvars[2], nbuf2); |
| 9619 | if (spat == NULL || mpat == NULL || epat == NULL) |
| 9620 | goto theend; /* type error */ |
| 9621 | |
| 9622 | /* Handle the optional fourth argument: flags */ |
| 9623 | dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */ |
| 9624 | if (dir == 0) |
| 9625 | goto theend; |
| 9626 | |
| 9627 | /* Don't accept SP_END or SP_SUBPAT. |
| 9628 | * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set. |
| 9629 | */ |
| 9630 | if ((flags & (SP_END | SP_SUBPAT)) != 0 |
| 9631 | || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))) |
| 9632 | { |
| 9633 | EMSG2(_(e_invarg2), get_tv_string(&argvars[3])); |
| 9634 | goto theend; |
| 9635 | } |
| 9636 | |
| 9637 | /* Using 'r' implies 'W', otherwise it doesn't work. */ |
| 9638 | if (flags & SP_REPEAT) |
| 9639 | p_ws = FALSE; |
| 9640 | |
| 9641 | /* Optional fifth argument: skip expression */ |
| 9642 | if (argvars[3].v_type == VAR_UNKNOWN |
| 9643 | || argvars[4].v_type == VAR_UNKNOWN) |
Bram Moolenaar | 4857048 | 2017-10-30 21:48:41 +0100 | [diff] [blame] | 9644 | skip = NULL; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9645 | else |
| 9646 | { |
Bram Moolenaar | 4857048 | 2017-10-30 21:48:41 +0100 | [diff] [blame] | 9647 | skip = &argvars[4]; |
| 9648 | if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL |
| 9649 | && skip->v_type != VAR_STRING) |
| 9650 | { |
| 9651 | /* Type error */ |
| 9652 | goto theend; |
| 9653 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9654 | if (argvars[5].v_type != VAR_UNKNOWN) |
| 9655 | { |
| 9656 | lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL); |
| 9657 | if (lnum_stop < 0) |
| 9658 | goto theend; |
| 9659 | #ifdef FEAT_RELTIME |
| 9660 | if (argvars[6].v_type != VAR_UNKNOWN) |
| 9661 | { |
| 9662 | time_limit = (long)get_tv_number_chk(&argvars[6], NULL); |
| 9663 | if (time_limit < 0) |
| 9664 | goto theend; |
| 9665 | } |
| 9666 | #endif |
| 9667 | } |
| 9668 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9669 | |
| 9670 | retval = do_searchpair(spat, mpat, epat, dir, skip, flags, |
| 9671 | match_pos, lnum_stop, time_limit); |
| 9672 | |
| 9673 | theend: |
| 9674 | p_ws = save_p_ws; |
| 9675 | |
| 9676 | return retval; |
| 9677 | } |
| 9678 | |
| 9679 | /* |
| 9680 | * "searchpair()" function |
| 9681 | */ |
| 9682 | static void |
| 9683 | f_searchpair(typval_T *argvars, typval_T *rettv) |
| 9684 | { |
| 9685 | rettv->vval.v_number = searchpair_cmn(argvars, NULL); |
| 9686 | } |
| 9687 | |
| 9688 | /* |
| 9689 | * "searchpairpos()" function |
| 9690 | */ |
| 9691 | static void |
| 9692 | f_searchpairpos(typval_T *argvars, typval_T *rettv) |
| 9693 | { |
| 9694 | pos_T match_pos; |
| 9695 | int lnum = 0; |
| 9696 | int col = 0; |
| 9697 | |
| 9698 | if (rettv_list_alloc(rettv) == FAIL) |
| 9699 | return; |
| 9700 | |
| 9701 | if (searchpair_cmn(argvars, &match_pos) > 0) |
| 9702 | { |
| 9703 | lnum = match_pos.lnum; |
| 9704 | col = match_pos.col; |
| 9705 | } |
| 9706 | |
| 9707 | list_append_number(rettv->vval.v_list, (varnumber_T)lnum); |
| 9708 | list_append_number(rettv->vval.v_list, (varnumber_T)col); |
| 9709 | } |
| 9710 | |
| 9711 | /* |
| 9712 | * Search for a start/middle/end thing. |
| 9713 | * Used by searchpair(), see its documentation for the details. |
| 9714 | * Returns 0 or -1 for no match, |
| 9715 | */ |
| 9716 | long |
| 9717 | do_searchpair( |
| 9718 | char_u *spat, /* start pattern */ |
| 9719 | char_u *mpat, /* middle pattern */ |
| 9720 | char_u *epat, /* end pattern */ |
| 9721 | int dir, /* BACKWARD or FORWARD */ |
Bram Moolenaar | 4857048 | 2017-10-30 21:48:41 +0100 | [diff] [blame] | 9722 | typval_T *skip, /* skip expression */ |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9723 | int flags, /* SP_SETPCMARK and other SP_ values */ |
| 9724 | pos_T *match_pos, |
| 9725 | linenr_T lnum_stop, /* stop at this line if not zero */ |
| 9726 | long time_limit UNUSED) /* stop after this many msec */ |
| 9727 | { |
| 9728 | char_u *save_cpo; |
| 9729 | char_u *pat, *pat2 = NULL, *pat3 = NULL; |
| 9730 | long retval = 0; |
| 9731 | pos_T pos; |
| 9732 | pos_T firstpos; |
| 9733 | pos_T foundpos; |
| 9734 | pos_T save_cursor; |
| 9735 | pos_T save_pos; |
| 9736 | int n; |
| 9737 | int r; |
| 9738 | int nest = 1; |
Bram Moolenaar | 4857048 | 2017-10-30 21:48:41 +0100 | [diff] [blame] | 9739 | int use_skip = FALSE; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9740 | int err; |
| 9741 | int options = SEARCH_KEEP; |
| 9742 | proftime_T tm; |
| 9743 | |
| 9744 | /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ |
| 9745 | save_cpo = p_cpo; |
| 9746 | p_cpo = empty_option; |
| 9747 | |
| 9748 | #ifdef FEAT_RELTIME |
| 9749 | /* Set the time limit, if there is one. */ |
| 9750 | profile_setlimit(time_limit, &tm); |
| 9751 | #endif |
| 9752 | |
| 9753 | /* Make two search patterns: start/end (pat2, for in nested pairs) and |
| 9754 | * start/middle/end (pat3, for the top pair). */ |
Bram Moolenaar | 6e450a5 | 2017-01-06 20:03:58 +0100 | [diff] [blame] | 9755 | pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17)); |
| 9756 | pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25)); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9757 | if (pat2 == NULL || pat3 == NULL) |
| 9758 | goto theend; |
Bram Moolenaar | 6e450a5 | 2017-01-06 20:03:58 +0100 | [diff] [blame] | 9759 | sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9760 | if (*mpat == NUL) |
| 9761 | STRCPY(pat3, pat2); |
| 9762 | else |
Bram Moolenaar | 6e450a5 | 2017-01-06 20:03:58 +0100 | [diff] [blame] | 9763 | sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)", |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9764 | spat, epat, mpat); |
| 9765 | if (flags & SP_START) |
| 9766 | options |= SEARCH_START; |
| 9767 | |
Bram Moolenaar | 4857048 | 2017-10-30 21:48:41 +0100 | [diff] [blame] | 9768 | if (skip != NULL) |
| 9769 | { |
| 9770 | /* Empty string means to not use the skip expression. */ |
| 9771 | if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC) |
| 9772 | use_skip = skip->vval.v_string != NULL |
| 9773 | && *skip->vval.v_string != NUL; |
| 9774 | } |
| 9775 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9776 | save_cursor = curwin->w_cursor; |
| 9777 | pos = curwin->w_cursor; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 9778 | CLEAR_POS(&firstpos); |
| 9779 | CLEAR_POS(&foundpos); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9780 | pat = pat3; |
| 9781 | for (;;) |
| 9782 | { |
| 9783 | n = searchit(curwin, curbuf, &pos, dir, pat, 1L, |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 9784 | options, RE_SEARCH, lnum_stop, &tm, NULL); |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 9785 | if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9786 | /* didn't find it or found the first match again: FAIL */ |
| 9787 | break; |
| 9788 | |
| 9789 | if (firstpos.lnum == 0) |
| 9790 | firstpos = pos; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 9791 | if (EQUAL_POS(pos, foundpos)) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9792 | { |
| 9793 | /* Found the same position again. Can happen with a pattern that |
| 9794 | * has "\zs" at the end and searching backwards. Advance one |
| 9795 | * character and try again. */ |
| 9796 | if (dir == BACKWARD) |
| 9797 | decl(&pos); |
| 9798 | else |
| 9799 | incl(&pos); |
| 9800 | } |
| 9801 | foundpos = pos; |
| 9802 | |
| 9803 | /* clear the start flag to avoid getting stuck here */ |
| 9804 | options &= ~SEARCH_START; |
| 9805 | |
| 9806 | /* If the skip pattern matches, ignore this match. */ |
Bram Moolenaar | 4857048 | 2017-10-30 21:48:41 +0100 | [diff] [blame] | 9807 | if (use_skip) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9808 | { |
| 9809 | save_pos = curwin->w_cursor; |
| 9810 | curwin->w_cursor = pos; |
Bram Moolenaar | 4857048 | 2017-10-30 21:48:41 +0100 | [diff] [blame] | 9811 | err = FALSE; |
| 9812 | r = eval_expr_to_bool(skip, &err); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 9813 | curwin->w_cursor = save_pos; |
| 9814 | if (err) |
| 9815 | { |
| 9816 | /* Evaluating {skip} caused an error, break here. */ |
| 9817 | curwin->w_cursor = save_cursor; |
| 9818 | retval = -1; |
| 9819 | break; |
| 9820 | } |
| 9821 | if (r) |
| 9822 | continue; |
| 9823 | } |
| 9824 | |
| 9825 | if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2)) |
| 9826 | { |
| 9827 | /* Found end when searching backwards or start when searching |
| 9828 | * forward: nested pair. */ |
| 9829 | ++nest; |
| 9830 | pat = pat2; /* nested, don't search for middle */ |
| 9831 | } |
| 9832 | else |
| 9833 | { |
| 9834 | /* Found end when searching forward or start when searching |
| 9835 | * backward: end of (nested) pair; or found middle in outer pair. */ |
| 9836 | if (--nest == 1) |
| 9837 | pat = pat3; /* outer level, search for middle */ |
| 9838 | } |
| 9839 | |
| 9840 | if (nest == 0) |
| 9841 | { |
| 9842 | /* Found the match: return matchcount or line number. */ |
| 9843 | if (flags & SP_RETCOUNT) |
| 9844 | ++retval; |
| 9845 | else |
| 9846 | retval = pos.lnum; |
| 9847 | if (flags & SP_SETPCMARK) |
| 9848 | setpcmark(); |
| 9849 | curwin->w_cursor = pos; |
| 9850 | if (!(flags & SP_REPEAT)) |
| 9851 | break; |
| 9852 | nest = 1; /* search for next unmatched */ |
| 9853 | } |
| 9854 | } |
| 9855 | |
| 9856 | if (match_pos != NULL) |
| 9857 | { |
| 9858 | /* Store the match cursor position */ |
| 9859 | match_pos->lnum = curwin->w_cursor.lnum; |
| 9860 | match_pos->col = curwin->w_cursor.col + 1; |
| 9861 | } |
| 9862 | |
| 9863 | /* If 'n' flag is used or search failed: restore cursor position. */ |
| 9864 | if ((flags & SP_NOMOVE) || retval == 0) |
| 9865 | curwin->w_cursor = save_cursor; |
| 9866 | |
| 9867 | theend: |
| 9868 | vim_free(pat2); |
| 9869 | vim_free(pat3); |
| 9870 | if (p_cpo == empty_option) |
| 9871 | p_cpo = save_cpo; |
| 9872 | else |
| 9873 | /* Darn, evaluating the {skip} expression changed the value. */ |
| 9874 | free_string_option(save_cpo); |
| 9875 | |
| 9876 | return retval; |
| 9877 | } |
| 9878 | |
| 9879 | /* |
| 9880 | * "searchpos()" function |
| 9881 | */ |
| 9882 | static void |
| 9883 | f_searchpos(typval_T *argvars, typval_T *rettv) |
| 9884 | { |
| 9885 | pos_T match_pos; |
| 9886 | int lnum = 0; |
| 9887 | int col = 0; |
| 9888 | int n; |
| 9889 | int flags = 0; |
| 9890 | |
| 9891 | if (rettv_list_alloc(rettv) == FAIL) |
| 9892 | return; |
| 9893 | |
| 9894 | n = search_cmn(argvars, &match_pos, &flags); |
| 9895 | if (n > 0) |
| 9896 | { |
| 9897 | lnum = match_pos.lnum; |
| 9898 | col = match_pos.col; |
| 9899 | } |
| 9900 | |
| 9901 | list_append_number(rettv->vval.v_list, (varnumber_T)lnum); |
| 9902 | list_append_number(rettv->vval.v_list, (varnumber_T)col); |
| 9903 | if (flags & SP_SUBPAT) |
| 9904 | list_append_number(rettv->vval.v_list, (varnumber_T)n); |
| 9905 | } |
| 9906 | |
| 9907 | static void |
| 9908 | f_server2client(typval_T *argvars UNUSED, typval_T *rettv) |
| 9909 | { |
| 9910 | #ifdef FEAT_CLIENTSERVER |
| 9911 | char_u buf[NUMBUFLEN]; |
| 9912 | char_u *server = get_tv_string_chk(&argvars[0]); |
| 9913 | char_u *reply = get_tv_string_buf_chk(&argvars[1], buf); |
| 9914 | |
| 9915 | rettv->vval.v_number = -1; |
| 9916 | if (server == NULL || reply == NULL) |
| 9917 | return; |
| 9918 | if (check_restricted() || check_secure()) |
| 9919 | return; |
| 9920 | # ifdef FEAT_X11 |
| 9921 | if (check_connection() == FAIL) |
| 9922 | return; |
| 9923 | # endif |
| 9924 | |
| 9925 | if (serverSendReply(server, reply) < 0) |
| 9926 | { |
| 9927 | EMSG(_("E258: Unable to send to client")); |
| 9928 | return; |
| 9929 | } |
| 9930 | rettv->vval.v_number = 0; |
| 9931 | #else |
| 9932 | rettv->vval.v_number = -1; |
| 9933 | #endif |
| 9934 | } |
| 9935 | |
| 9936 | static void |
| 9937 | f_serverlist(typval_T *argvars UNUSED, typval_T *rettv) |
| 9938 | { |
| 9939 | char_u *r = NULL; |
| 9940 | |
| 9941 | #ifdef FEAT_CLIENTSERVER |
| 9942 | # ifdef WIN32 |
| 9943 | r = serverGetVimNames(); |
| 9944 | # else |
| 9945 | make_connection(); |
| 9946 | if (X_DISPLAY != NULL) |
| 9947 | r = serverGetVimNames(X_DISPLAY); |
| 9948 | # endif |
| 9949 | #endif |
| 9950 | rettv->v_type = VAR_STRING; |
| 9951 | rettv->vval.v_string = r; |
| 9952 | } |
| 9953 | |
| 9954 | /* |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 9955 | * Set line or list of lines in buffer "buf". |
| 9956 | */ |
| 9957 | static void |
| 9958 | set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv) |
| 9959 | { |
| 9960 | char_u *line = NULL; |
| 9961 | list_T *l = NULL; |
| 9962 | listitem_T *li = NULL; |
| 9963 | long added = 0; |
| 9964 | linenr_T lcount; |
Bram Moolenaar | 0c4dc88 | 2017-11-06 21:32:54 +0100 | [diff] [blame] | 9965 | buf_T *curbuf_save = NULL; |
| 9966 | win_T *curwin_save = NULL; |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 9967 | int is_curbuf = buf == curbuf; |
| 9968 | |
Bram Moolenaar | 9d95420 | 2017-09-04 20:34:19 +0200 | [diff] [blame] | 9969 | /* When using the current buffer ml_mfp will be set if needed. Useful when |
| 9970 | * setline() is used on startup. For other buffers the buffer must be |
| 9971 | * loaded. */ |
| 9972 | if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1) |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 9973 | { |
| 9974 | rettv->vval.v_number = 1; /* FAIL */ |
| 9975 | return; |
| 9976 | } |
| 9977 | |
Bram Moolenaar | 0c4dc88 | 2017-11-06 21:32:54 +0100 | [diff] [blame] | 9978 | if (!is_curbuf) |
| 9979 | { |
| 9980 | wininfo_T *wip; |
| 9981 | |
| 9982 | curbuf_save = curbuf; |
| 9983 | curwin_save = curwin; |
| 9984 | curbuf = buf; |
| 9985 | for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) |
| 9986 | { |
| 9987 | if (wip->wi_win != NULL) |
| 9988 | { |
| 9989 | curwin = wip->wi_win; |
| 9990 | break; |
| 9991 | } |
| 9992 | } |
| 9993 | } |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 9994 | |
| 9995 | lcount = curbuf->b_ml.ml_line_count; |
| 9996 | |
| 9997 | if (lines->v_type == VAR_LIST) |
| 9998 | { |
| 9999 | l = lines->vval.v_list; |
| 10000 | li = l->lv_first; |
| 10001 | } |
| 10002 | else |
| 10003 | line = get_tv_string_chk(lines); |
| 10004 | |
| 10005 | /* default result is zero == OK */ |
| 10006 | for (;;) |
| 10007 | { |
| 10008 | if (l != NULL) |
| 10009 | { |
| 10010 | /* list argument, get next string */ |
| 10011 | if (li == NULL) |
| 10012 | break; |
| 10013 | line = get_tv_string_chk(&li->li_tv); |
| 10014 | li = li->li_next; |
| 10015 | } |
| 10016 | |
| 10017 | rettv->vval.v_number = 1; /* FAIL */ |
| 10018 | if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1) |
| 10019 | break; |
| 10020 | |
| 10021 | /* When coming here from Insert mode, sync undo, so that this can be |
| 10022 | * undone separately from what was previously inserted. */ |
| 10023 | if (u_sync_once == 2) |
| 10024 | { |
| 10025 | u_sync_once = 1; /* notify that u_sync() was called */ |
| 10026 | u_sync(TRUE); |
| 10027 | } |
| 10028 | |
| 10029 | if (lnum <= curbuf->b_ml.ml_line_count) |
| 10030 | { |
| 10031 | /* existing line, replace it */ |
| 10032 | if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK) |
| 10033 | { |
| 10034 | changed_bytes(lnum, 0); |
| 10035 | if (is_curbuf && lnum == curwin->w_cursor.lnum) |
| 10036 | check_cursor_col(); |
| 10037 | rettv->vval.v_number = 0; /* OK */ |
| 10038 | } |
| 10039 | } |
| 10040 | else if (added > 0 || u_save(lnum - 1, lnum) == OK) |
| 10041 | { |
| 10042 | /* lnum is one past the last line, append the line */ |
| 10043 | ++added; |
| 10044 | if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK) |
| 10045 | rettv->vval.v_number = 0; /* OK */ |
| 10046 | } |
| 10047 | |
| 10048 | if (l == NULL) /* only one string argument */ |
| 10049 | break; |
| 10050 | ++lnum; |
| 10051 | } |
| 10052 | |
| 10053 | if (added > 0) |
| 10054 | appended_lines_mark(lcount, added); |
| 10055 | |
Bram Moolenaar | 0c4dc88 | 2017-11-06 21:32:54 +0100 | [diff] [blame] | 10056 | if (!is_curbuf) |
| 10057 | { |
| 10058 | curbuf = curbuf_save; |
| 10059 | curwin = curwin_save; |
| 10060 | } |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 10061 | } |
| 10062 | |
| 10063 | /* |
| 10064 | * "setbufline()" function |
| 10065 | */ |
| 10066 | static void |
| 10067 | f_setbufline(argvars, rettv) |
| 10068 | typval_T *argvars; |
| 10069 | typval_T *rettv; |
| 10070 | { |
| 10071 | linenr_T lnum; |
| 10072 | buf_T *buf; |
| 10073 | |
| 10074 | buf = get_buf_tv(&argvars[0], FALSE); |
| 10075 | if (buf == NULL) |
| 10076 | rettv->vval.v_number = 1; /* FAIL */ |
| 10077 | else |
| 10078 | { |
| 10079 | lnum = get_tv_lnum_buf(&argvars[1], buf); |
| 10080 | |
| 10081 | set_buffer_lines(buf, lnum, &argvars[2], rettv); |
| 10082 | } |
| 10083 | } |
| 10084 | |
| 10085 | /* |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10086 | * "setbufvar()" function |
| 10087 | */ |
| 10088 | static void |
| 10089 | f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED) |
| 10090 | { |
| 10091 | buf_T *buf; |
| 10092 | char_u *varname, *bufvarname; |
| 10093 | typval_T *varp; |
| 10094 | char_u nbuf[NUMBUFLEN]; |
| 10095 | |
| 10096 | if (check_restricted() || check_secure()) |
| 10097 | return; |
| 10098 | (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */ |
| 10099 | varname = get_tv_string_chk(&argvars[1]); |
| 10100 | buf = get_buf_tv(&argvars[0], FALSE); |
| 10101 | varp = &argvars[2]; |
| 10102 | |
| 10103 | if (buf != NULL && varname != NULL && varp != NULL) |
| 10104 | { |
| 10105 | if (*varname == '&') |
| 10106 | { |
| 10107 | long numval; |
| 10108 | char_u *strval; |
| 10109 | int error = FALSE; |
| 10110 | aco_save_T aco; |
| 10111 | |
| 10112 | /* set curbuf to be our buf, temporarily */ |
| 10113 | aucmd_prepbuf(&aco, buf); |
| 10114 | |
| 10115 | ++varname; |
| 10116 | numval = (long)get_tv_number_chk(varp, &error); |
| 10117 | strval = get_tv_string_buf_chk(varp, nbuf); |
| 10118 | if (!error && strval != NULL) |
| 10119 | set_option_value(varname, numval, strval, OPT_LOCAL); |
| 10120 | |
| 10121 | /* reset notion of buffer */ |
| 10122 | aucmd_restbuf(&aco); |
| 10123 | } |
| 10124 | else |
| 10125 | { |
| 10126 | buf_T *save_curbuf = curbuf; |
| 10127 | |
| 10128 | bufvarname = alloc((unsigned)STRLEN(varname) + 3); |
| 10129 | if (bufvarname != NULL) |
| 10130 | { |
| 10131 | curbuf = buf; |
| 10132 | STRCPY(bufvarname, "b:"); |
| 10133 | STRCPY(bufvarname + 2, varname); |
| 10134 | set_var(bufvarname, varp, TRUE); |
| 10135 | vim_free(bufvarname); |
| 10136 | curbuf = save_curbuf; |
| 10137 | } |
| 10138 | } |
| 10139 | } |
| 10140 | } |
| 10141 | |
| 10142 | static void |
| 10143 | f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED) |
| 10144 | { |
| 10145 | dict_T *d; |
| 10146 | dictitem_T *di; |
| 10147 | char_u *csearch; |
| 10148 | |
| 10149 | if (argvars[0].v_type != VAR_DICT) |
| 10150 | { |
| 10151 | EMSG(_(e_dictreq)); |
| 10152 | return; |
| 10153 | } |
| 10154 | |
| 10155 | if ((d = argvars[0].vval.v_dict) != NULL) |
| 10156 | { |
| 10157 | csearch = get_dict_string(d, (char_u *)"char", FALSE); |
| 10158 | if (csearch != NULL) |
| 10159 | { |
| 10160 | #ifdef FEAT_MBYTE |
| 10161 | if (enc_utf8) |
| 10162 | { |
| 10163 | int pcc[MAX_MCO]; |
| 10164 | int c = utfc_ptr2char(csearch, pcc); |
| 10165 | |
| 10166 | set_last_csearch(c, csearch, utfc_ptr2len(csearch)); |
| 10167 | } |
| 10168 | else |
| 10169 | #endif |
| 10170 | set_last_csearch(PTR2CHAR(csearch), |
| 10171 | csearch, MB_PTR2LEN(csearch)); |
| 10172 | } |
| 10173 | |
| 10174 | di = dict_find(d, (char_u *)"forward", -1); |
| 10175 | if (di != NULL) |
| 10176 | set_csearch_direction((int)get_tv_number(&di->di_tv) |
| 10177 | ? FORWARD : BACKWARD); |
| 10178 | |
| 10179 | di = dict_find(d, (char_u *)"until", -1); |
| 10180 | if (di != NULL) |
| 10181 | set_csearch_until(!!get_tv_number(&di->di_tv)); |
| 10182 | } |
| 10183 | } |
| 10184 | |
| 10185 | /* |
| 10186 | * "setcmdpos()" function |
| 10187 | */ |
| 10188 | static void |
| 10189 | f_setcmdpos(typval_T *argvars, typval_T *rettv) |
| 10190 | { |
| 10191 | int pos = (int)get_tv_number(&argvars[0]) - 1; |
| 10192 | |
| 10193 | if (pos >= 0) |
| 10194 | rettv->vval.v_number = set_cmdline_pos(pos); |
| 10195 | } |
| 10196 | |
| 10197 | /* |
| 10198 | * "setfperm({fname}, {mode})" function |
| 10199 | */ |
| 10200 | static void |
| 10201 | f_setfperm(typval_T *argvars, typval_T *rettv) |
| 10202 | { |
| 10203 | char_u *fname; |
| 10204 | char_u modebuf[NUMBUFLEN]; |
| 10205 | char_u *mode_str; |
| 10206 | int i; |
| 10207 | int mask; |
| 10208 | int mode = 0; |
| 10209 | |
| 10210 | rettv->vval.v_number = 0; |
| 10211 | fname = get_tv_string_chk(&argvars[0]); |
| 10212 | if (fname == NULL) |
| 10213 | return; |
| 10214 | mode_str = get_tv_string_buf_chk(&argvars[1], modebuf); |
| 10215 | if (mode_str == NULL) |
| 10216 | return; |
| 10217 | if (STRLEN(mode_str) != 9) |
| 10218 | { |
| 10219 | EMSG2(_(e_invarg2), mode_str); |
| 10220 | return; |
| 10221 | } |
| 10222 | |
| 10223 | mask = 1; |
| 10224 | for (i = 8; i >= 0; --i) |
| 10225 | { |
| 10226 | if (mode_str[i] != '-') |
| 10227 | mode |= mask; |
| 10228 | mask = mask << 1; |
| 10229 | } |
| 10230 | rettv->vval.v_number = mch_setperm(fname, mode) == OK; |
| 10231 | } |
| 10232 | |
| 10233 | /* |
| 10234 | * "setline()" function |
| 10235 | */ |
| 10236 | static void |
| 10237 | f_setline(typval_T *argvars, typval_T *rettv) |
| 10238 | { |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 10239 | linenr_T lnum = get_tv_lnum(&argvars[0]); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10240 | |
Bram Moolenaar | b31cf2b | 2017-09-02 19:45:19 +0200 | [diff] [blame] | 10241 | set_buffer_lines(curbuf, lnum, &argvars[1], rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10242 | } |
| 10243 | |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 10244 | static void set_qf_ll_list(win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *what_arg, typval_T *rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10245 | |
| 10246 | /* |
| 10247 | * Used by "setqflist()" and "setloclist()" functions |
| 10248 | */ |
| 10249 | static void |
| 10250 | set_qf_ll_list( |
| 10251 | win_T *wp UNUSED, |
| 10252 | typval_T *list_arg UNUSED, |
| 10253 | typval_T *action_arg UNUSED, |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 10254 | typval_T *what_arg UNUSED, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10255 | typval_T *rettv) |
| 10256 | { |
| 10257 | #ifdef FEAT_QUICKFIX |
| 10258 | static char *e_invact = N_("E927: Invalid action: '%s'"); |
| 10259 | char_u *act; |
| 10260 | int action = 0; |
| 10261 | #endif |
| 10262 | |
| 10263 | rettv->vval.v_number = -1; |
| 10264 | |
| 10265 | #ifdef FEAT_QUICKFIX |
| 10266 | if (list_arg->v_type != VAR_LIST) |
| 10267 | EMSG(_(e_listreq)); |
| 10268 | else |
| 10269 | { |
| 10270 | list_T *l = list_arg->vval.v_list; |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 10271 | dict_T *d = NULL; |
| 10272 | int valid_dict = TRUE; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10273 | |
| 10274 | if (action_arg->v_type == VAR_STRING) |
| 10275 | { |
| 10276 | act = get_tv_string_chk(action_arg); |
| 10277 | if (act == NULL) |
| 10278 | return; /* type error; errmsg already given */ |
Bram Moolenaar | b6fa30c | 2017-03-29 14:19:25 +0200 | [diff] [blame] | 10279 | if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') && |
| 10280 | act[1] == NUL) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10281 | action = *act; |
| 10282 | else |
| 10283 | EMSG2(_(e_invact), act); |
| 10284 | } |
| 10285 | else if (action_arg->v_type == VAR_UNKNOWN) |
| 10286 | action = ' '; |
| 10287 | else |
| 10288 | EMSG(_(e_stringreq)); |
| 10289 | |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 10290 | if (action_arg->v_type != VAR_UNKNOWN |
| 10291 | && what_arg->v_type != VAR_UNKNOWN) |
| 10292 | { |
| 10293 | if (what_arg->v_type == VAR_DICT) |
| 10294 | d = what_arg->vval.v_dict; |
| 10295 | else |
| 10296 | { |
| 10297 | EMSG(_(e_dictreq)); |
| 10298 | valid_dict = FALSE; |
| 10299 | } |
| 10300 | } |
| 10301 | |
| 10302 | if (l != NULL && action && valid_dict && set_errorlist(wp, l, action, |
| 10303 | (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10304 | rettv->vval.v_number = 0; |
| 10305 | } |
| 10306 | #endif |
| 10307 | } |
| 10308 | |
| 10309 | /* |
| 10310 | * "setloclist()" function |
| 10311 | */ |
| 10312 | static void |
| 10313 | f_setloclist(typval_T *argvars, typval_T *rettv) |
| 10314 | { |
| 10315 | win_T *win; |
| 10316 | |
| 10317 | rettv->vval.v_number = -1; |
| 10318 | |
| 10319 | win = find_win_by_nr(&argvars[0], NULL); |
| 10320 | if (win != NULL) |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 10321 | set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10322 | } |
| 10323 | |
| 10324 | /* |
| 10325 | * "setmatches()" function |
| 10326 | */ |
| 10327 | static void |
| 10328 | f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 10329 | { |
| 10330 | #ifdef FEAT_SEARCH_EXTRA |
| 10331 | list_T *l; |
| 10332 | listitem_T *li; |
| 10333 | dict_T *d; |
| 10334 | list_T *s = NULL; |
| 10335 | |
| 10336 | rettv->vval.v_number = -1; |
| 10337 | if (argvars[0].v_type != VAR_LIST) |
| 10338 | { |
| 10339 | EMSG(_(e_listreq)); |
| 10340 | return; |
| 10341 | } |
| 10342 | if ((l = argvars[0].vval.v_list) != NULL) |
| 10343 | { |
| 10344 | |
| 10345 | /* To some extent make sure that we are dealing with a list from |
| 10346 | * "getmatches()". */ |
| 10347 | li = l->lv_first; |
| 10348 | while (li != NULL) |
| 10349 | { |
| 10350 | if (li->li_tv.v_type != VAR_DICT |
| 10351 | || (d = li->li_tv.vval.v_dict) == NULL) |
| 10352 | { |
| 10353 | EMSG(_(e_invarg)); |
| 10354 | return; |
| 10355 | } |
| 10356 | if (!(dict_find(d, (char_u *)"group", -1) != NULL |
| 10357 | && (dict_find(d, (char_u *)"pattern", -1) != NULL |
| 10358 | || dict_find(d, (char_u *)"pos1", -1) != NULL) |
| 10359 | && dict_find(d, (char_u *)"priority", -1) != NULL |
| 10360 | && dict_find(d, (char_u *)"id", -1) != NULL)) |
| 10361 | { |
| 10362 | EMSG(_(e_invarg)); |
| 10363 | return; |
| 10364 | } |
| 10365 | li = li->li_next; |
| 10366 | } |
| 10367 | |
| 10368 | clear_matches(curwin); |
| 10369 | li = l->lv_first; |
| 10370 | while (li != NULL) |
| 10371 | { |
| 10372 | int i = 0; |
| 10373 | char_u buf[5]; |
| 10374 | dictitem_T *di; |
| 10375 | char_u *group; |
| 10376 | int priority; |
| 10377 | int id; |
| 10378 | char_u *conceal; |
| 10379 | |
| 10380 | d = li->li_tv.vval.v_dict; |
| 10381 | if (dict_find(d, (char_u *)"pattern", -1) == NULL) |
| 10382 | { |
| 10383 | if (s == NULL) |
| 10384 | { |
| 10385 | s = list_alloc(); |
| 10386 | if (s == NULL) |
| 10387 | return; |
| 10388 | } |
| 10389 | |
| 10390 | /* match from matchaddpos() */ |
| 10391 | for (i = 1; i < 9; i++) |
| 10392 | { |
| 10393 | sprintf((char *)buf, (char *)"pos%d", i); |
| 10394 | if ((di = dict_find(d, (char_u *)buf, -1)) != NULL) |
| 10395 | { |
| 10396 | if (di->di_tv.v_type != VAR_LIST) |
| 10397 | return; |
| 10398 | |
| 10399 | list_append_tv(s, &di->di_tv); |
| 10400 | s->lv_refcount++; |
| 10401 | } |
| 10402 | else |
| 10403 | break; |
| 10404 | } |
| 10405 | } |
| 10406 | |
Bram Moolenaar | 7dc5e2e | 2016-08-05 22:22:06 +0200 | [diff] [blame] | 10407 | group = get_dict_string(d, (char_u *)"group", TRUE); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10408 | priority = (int)get_dict_number(d, (char_u *)"priority"); |
| 10409 | id = (int)get_dict_number(d, (char_u *)"id"); |
| 10410 | conceal = dict_find(d, (char_u *)"conceal", -1) != NULL |
Bram Moolenaar | 7dc5e2e | 2016-08-05 22:22:06 +0200 | [diff] [blame] | 10411 | ? get_dict_string(d, (char_u *)"conceal", TRUE) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10412 | : NULL; |
| 10413 | if (i == 0) |
| 10414 | { |
| 10415 | match_add(curwin, group, |
| 10416 | get_dict_string(d, (char_u *)"pattern", FALSE), |
| 10417 | priority, id, NULL, conceal); |
| 10418 | } |
| 10419 | else |
| 10420 | { |
| 10421 | match_add(curwin, group, NULL, priority, id, s, conceal); |
| 10422 | list_unref(s); |
| 10423 | s = NULL; |
| 10424 | } |
Bram Moolenaar | 7dc5e2e | 2016-08-05 22:22:06 +0200 | [diff] [blame] | 10425 | vim_free(group); |
| 10426 | vim_free(conceal); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10427 | |
| 10428 | li = li->li_next; |
| 10429 | } |
| 10430 | rettv->vval.v_number = 0; |
| 10431 | } |
| 10432 | #endif |
| 10433 | } |
| 10434 | |
| 10435 | /* |
| 10436 | * "setpos()" function |
| 10437 | */ |
| 10438 | static void |
| 10439 | f_setpos(typval_T *argvars, typval_T *rettv) |
| 10440 | { |
| 10441 | pos_T pos; |
| 10442 | int fnum; |
| 10443 | char_u *name; |
| 10444 | colnr_T curswant = -1; |
| 10445 | |
| 10446 | rettv->vval.v_number = -1; |
| 10447 | name = get_tv_string_chk(argvars); |
| 10448 | if (name != NULL) |
| 10449 | { |
| 10450 | if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK) |
| 10451 | { |
| 10452 | if (--pos.col < 0) |
| 10453 | pos.col = 0; |
| 10454 | if (name[0] == '.' && name[1] == NUL) |
| 10455 | { |
Bram Moolenaar | 3a29abc | 2017-01-28 18:31:41 +0100 | [diff] [blame] | 10456 | /* set cursor; "fnum" is ignored */ |
| 10457 | curwin->w_cursor = pos; |
| 10458 | if (curswant >= 0) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10459 | { |
Bram Moolenaar | 3a29abc | 2017-01-28 18:31:41 +0100 | [diff] [blame] | 10460 | curwin->w_curswant = curswant - 1; |
| 10461 | curwin->w_set_curswant = FALSE; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10462 | } |
Bram Moolenaar | 3a29abc | 2017-01-28 18:31:41 +0100 | [diff] [blame] | 10463 | check_cursor(); |
| 10464 | rettv->vval.v_number = 0; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10465 | } |
| 10466 | else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL) |
| 10467 | { |
| 10468 | /* set mark */ |
| 10469 | if (setmark_pos(name[1], &pos, fnum) == OK) |
| 10470 | rettv->vval.v_number = 0; |
| 10471 | } |
| 10472 | else |
| 10473 | EMSG(_(e_invarg)); |
| 10474 | } |
| 10475 | } |
| 10476 | } |
| 10477 | |
| 10478 | /* |
| 10479 | * "setqflist()" function |
| 10480 | */ |
| 10481 | static void |
| 10482 | f_setqflist(typval_T *argvars, typval_T *rettv) |
| 10483 | { |
Bram Moolenaar | d823fa9 | 2016-08-12 16:29:27 +0200 | [diff] [blame] | 10484 | set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10485 | } |
| 10486 | |
| 10487 | /* |
| 10488 | * "setreg()" function |
| 10489 | */ |
| 10490 | static void |
| 10491 | f_setreg(typval_T *argvars, typval_T *rettv) |
| 10492 | { |
| 10493 | int regname; |
| 10494 | char_u *strregname; |
| 10495 | char_u *stropt; |
| 10496 | char_u *strval; |
| 10497 | int append; |
| 10498 | char_u yank_type; |
| 10499 | long block_len; |
| 10500 | |
| 10501 | block_len = -1; |
| 10502 | yank_type = MAUTO; |
| 10503 | append = FALSE; |
| 10504 | |
| 10505 | strregname = get_tv_string_chk(argvars); |
| 10506 | rettv->vval.v_number = 1; /* FAIL is default */ |
| 10507 | |
| 10508 | if (strregname == NULL) |
| 10509 | return; /* type error; errmsg already given */ |
| 10510 | regname = *strregname; |
| 10511 | if (regname == 0 || regname == '@') |
| 10512 | regname = '"'; |
| 10513 | |
| 10514 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 10515 | { |
| 10516 | stropt = get_tv_string_chk(&argvars[2]); |
| 10517 | if (stropt == NULL) |
| 10518 | return; /* type error */ |
| 10519 | for (; *stropt != NUL; ++stropt) |
| 10520 | switch (*stropt) |
| 10521 | { |
| 10522 | case 'a': case 'A': /* append */ |
| 10523 | append = TRUE; |
| 10524 | break; |
| 10525 | case 'v': case 'c': /* character-wise selection */ |
| 10526 | yank_type = MCHAR; |
| 10527 | break; |
| 10528 | case 'V': case 'l': /* line-wise selection */ |
| 10529 | yank_type = MLINE; |
| 10530 | break; |
| 10531 | case 'b': case Ctrl_V: /* block-wise selection */ |
| 10532 | yank_type = MBLOCK; |
| 10533 | if (VIM_ISDIGIT(stropt[1])) |
| 10534 | { |
| 10535 | ++stropt; |
| 10536 | block_len = getdigits(&stropt) - 1; |
| 10537 | --stropt; |
| 10538 | } |
| 10539 | break; |
| 10540 | } |
| 10541 | } |
| 10542 | |
| 10543 | if (argvars[1].v_type == VAR_LIST) |
| 10544 | { |
| 10545 | char_u **lstval; |
| 10546 | char_u **allocval; |
| 10547 | char_u buf[NUMBUFLEN]; |
| 10548 | char_u **curval; |
| 10549 | char_u **curallocval; |
| 10550 | list_T *ll = argvars[1].vval.v_list; |
| 10551 | listitem_T *li; |
| 10552 | int len; |
| 10553 | |
| 10554 | /* If the list is NULL handle like an empty list. */ |
| 10555 | len = ll == NULL ? 0 : ll->lv_len; |
| 10556 | |
| 10557 | /* First half: use for pointers to result lines; second half: use for |
| 10558 | * pointers to allocated copies. */ |
| 10559 | lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2)); |
| 10560 | if (lstval == NULL) |
| 10561 | return; |
| 10562 | curval = lstval; |
| 10563 | allocval = lstval + len + 2; |
| 10564 | curallocval = allocval; |
| 10565 | |
| 10566 | for (li = ll == NULL ? NULL : ll->lv_first; li != NULL; |
| 10567 | li = li->li_next) |
| 10568 | { |
| 10569 | strval = get_tv_string_buf_chk(&li->li_tv, buf); |
| 10570 | if (strval == NULL) |
| 10571 | goto free_lstval; |
| 10572 | if (strval == buf) |
| 10573 | { |
| 10574 | /* Need to make a copy, next get_tv_string_buf_chk() will |
| 10575 | * overwrite the string. */ |
| 10576 | strval = vim_strsave(buf); |
| 10577 | if (strval == NULL) |
| 10578 | goto free_lstval; |
| 10579 | *curallocval++ = strval; |
| 10580 | } |
| 10581 | *curval++ = strval; |
| 10582 | } |
| 10583 | *curval++ = NULL; |
| 10584 | |
| 10585 | write_reg_contents_lst(regname, lstval, -1, |
| 10586 | append, yank_type, block_len); |
| 10587 | free_lstval: |
| 10588 | while (curallocval > allocval) |
| 10589 | vim_free(*--curallocval); |
| 10590 | vim_free(lstval); |
| 10591 | } |
| 10592 | else |
| 10593 | { |
| 10594 | strval = get_tv_string_chk(&argvars[1]); |
| 10595 | if (strval == NULL) |
| 10596 | return; |
| 10597 | write_reg_contents_ex(regname, strval, -1, |
| 10598 | append, yank_type, block_len); |
| 10599 | } |
| 10600 | rettv->vval.v_number = 0; |
| 10601 | } |
| 10602 | |
| 10603 | /* |
| 10604 | * "settabvar()" function |
| 10605 | */ |
| 10606 | static void |
| 10607 | f_settabvar(typval_T *argvars, typval_T *rettv) |
| 10608 | { |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10609 | tabpage_T *save_curtab; |
| 10610 | tabpage_T *tp; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10611 | char_u *varname, *tabvarname; |
| 10612 | typval_T *varp; |
| 10613 | |
| 10614 | rettv->vval.v_number = 0; |
| 10615 | |
| 10616 | if (check_restricted() || check_secure()) |
| 10617 | return; |
| 10618 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10619 | tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL)); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10620 | varname = get_tv_string_chk(&argvars[1]); |
| 10621 | varp = &argvars[2]; |
| 10622 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 10623 | if (varname != NULL && varp != NULL && tp != NULL) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10624 | { |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10625 | save_curtab = curtab; |
| 10626 | goto_tabpage_tp(tp, FALSE, FALSE); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10627 | |
| 10628 | tabvarname = alloc((unsigned)STRLEN(varname) + 3); |
| 10629 | if (tabvarname != NULL) |
| 10630 | { |
| 10631 | STRCPY(tabvarname, "t:"); |
| 10632 | STRCPY(tabvarname + 2, varname); |
| 10633 | set_var(tabvarname, varp, TRUE); |
| 10634 | vim_free(tabvarname); |
| 10635 | } |
| 10636 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10637 | /* Restore current tabpage */ |
| 10638 | if (valid_tabpage(save_curtab)) |
| 10639 | goto_tabpage_tp(save_curtab, FALSE, FALSE); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10640 | } |
| 10641 | } |
| 10642 | |
| 10643 | /* |
| 10644 | * "settabwinvar()" function |
| 10645 | */ |
| 10646 | static void |
| 10647 | f_settabwinvar(typval_T *argvars, typval_T *rettv) |
| 10648 | { |
| 10649 | setwinvar(argvars, rettv, 1); |
| 10650 | } |
| 10651 | |
| 10652 | /* |
| 10653 | * "setwinvar()" function |
| 10654 | */ |
| 10655 | static void |
| 10656 | f_setwinvar(typval_T *argvars, typval_T *rettv) |
| 10657 | { |
| 10658 | setwinvar(argvars, rettv, 0); |
| 10659 | } |
| 10660 | |
| 10661 | #ifdef FEAT_CRYPT |
| 10662 | /* |
| 10663 | * "sha256({string})" function |
| 10664 | */ |
| 10665 | static void |
| 10666 | f_sha256(typval_T *argvars, typval_T *rettv) |
| 10667 | { |
| 10668 | char_u *p; |
| 10669 | |
| 10670 | p = get_tv_string(&argvars[0]); |
| 10671 | rettv->vval.v_string = vim_strsave( |
| 10672 | sha256_bytes(p, (int)STRLEN(p), NULL, 0)); |
| 10673 | rettv->v_type = VAR_STRING; |
| 10674 | } |
| 10675 | #endif /* FEAT_CRYPT */ |
| 10676 | |
| 10677 | /* |
| 10678 | * "shellescape({string})" function |
| 10679 | */ |
| 10680 | static void |
| 10681 | f_shellescape(typval_T *argvars, typval_T *rettv) |
| 10682 | { |
Bram Moolenaar | 2061552 | 2017-06-05 18:46:26 +0200 | [diff] [blame] | 10683 | int do_special = non_zero_arg(&argvars[1]); |
| 10684 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10685 | rettv->vval.v_string = vim_strsave_shellescape( |
Bram Moolenaar | 2061552 | 2017-06-05 18:46:26 +0200 | [diff] [blame] | 10686 | get_tv_string(&argvars[0]), do_special, do_special); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10687 | rettv->v_type = VAR_STRING; |
| 10688 | } |
| 10689 | |
| 10690 | /* |
| 10691 | * shiftwidth() function |
| 10692 | */ |
| 10693 | static void |
| 10694 | f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv) |
| 10695 | { |
| 10696 | rettv->vval.v_number = get_sw_value(curbuf); |
| 10697 | } |
| 10698 | |
| 10699 | /* |
| 10700 | * "simplify()" function |
| 10701 | */ |
| 10702 | static void |
| 10703 | f_simplify(typval_T *argvars, typval_T *rettv) |
| 10704 | { |
| 10705 | char_u *p; |
| 10706 | |
| 10707 | p = get_tv_string(&argvars[0]); |
| 10708 | rettv->vval.v_string = vim_strsave(p); |
| 10709 | simplify_filename(rettv->vval.v_string); /* simplify in place */ |
| 10710 | rettv->v_type = VAR_STRING; |
| 10711 | } |
| 10712 | |
| 10713 | #ifdef FEAT_FLOAT |
| 10714 | /* |
| 10715 | * "sin()" function |
| 10716 | */ |
| 10717 | static void |
| 10718 | f_sin(typval_T *argvars, typval_T *rettv) |
| 10719 | { |
| 10720 | float_T f = 0.0; |
| 10721 | |
| 10722 | rettv->v_type = VAR_FLOAT; |
| 10723 | if (get_float_arg(argvars, &f) == OK) |
| 10724 | rettv->vval.v_float = sin(f); |
| 10725 | else |
| 10726 | rettv->vval.v_float = 0.0; |
| 10727 | } |
| 10728 | |
| 10729 | /* |
| 10730 | * "sinh()" function |
| 10731 | */ |
| 10732 | static void |
| 10733 | f_sinh(typval_T *argvars, typval_T *rettv) |
| 10734 | { |
| 10735 | float_T f = 0.0; |
| 10736 | |
| 10737 | rettv->v_type = VAR_FLOAT; |
| 10738 | if (get_float_arg(argvars, &f) == OK) |
| 10739 | rettv->vval.v_float = sinh(f); |
| 10740 | else |
| 10741 | rettv->vval.v_float = 0.0; |
| 10742 | } |
| 10743 | #endif |
| 10744 | |
| 10745 | static int |
| 10746 | #ifdef __BORLANDC__ |
| 10747 | _RTLENTRYF |
| 10748 | #endif |
| 10749 | item_compare(const void *s1, const void *s2); |
| 10750 | static int |
| 10751 | #ifdef __BORLANDC__ |
| 10752 | _RTLENTRYF |
| 10753 | #endif |
| 10754 | item_compare2(const void *s1, const void *s2); |
| 10755 | |
| 10756 | /* struct used in the array that's given to qsort() */ |
| 10757 | typedef struct |
| 10758 | { |
| 10759 | listitem_T *item; |
| 10760 | int idx; |
| 10761 | } sortItem_T; |
| 10762 | |
| 10763 | /* struct storing information about current sort */ |
| 10764 | typedef struct |
| 10765 | { |
| 10766 | int item_compare_ic; |
| 10767 | int item_compare_numeric; |
| 10768 | int item_compare_numbers; |
| 10769 | #ifdef FEAT_FLOAT |
| 10770 | int item_compare_float; |
| 10771 | #endif |
| 10772 | char_u *item_compare_func; |
| 10773 | partial_T *item_compare_partial; |
| 10774 | dict_T *item_compare_selfdict; |
| 10775 | int item_compare_func_err; |
| 10776 | int item_compare_keep_zero; |
| 10777 | } sortinfo_T; |
| 10778 | static sortinfo_T *sortinfo = NULL; |
| 10779 | static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort); |
| 10780 | #define ITEM_COMPARE_FAIL 999 |
| 10781 | |
| 10782 | /* |
| 10783 | * Compare functions for f_sort() and f_uniq() below. |
| 10784 | */ |
| 10785 | static int |
| 10786 | #ifdef __BORLANDC__ |
| 10787 | _RTLENTRYF |
| 10788 | #endif |
| 10789 | item_compare(const void *s1, const void *s2) |
| 10790 | { |
| 10791 | sortItem_T *si1, *si2; |
| 10792 | typval_T *tv1, *tv2; |
| 10793 | char_u *p1, *p2; |
| 10794 | char_u *tofree1 = NULL, *tofree2 = NULL; |
| 10795 | int res; |
| 10796 | char_u numbuf1[NUMBUFLEN]; |
| 10797 | char_u numbuf2[NUMBUFLEN]; |
| 10798 | |
| 10799 | si1 = (sortItem_T *)s1; |
| 10800 | si2 = (sortItem_T *)s2; |
| 10801 | tv1 = &si1->item->li_tv; |
| 10802 | tv2 = &si2->item->li_tv; |
| 10803 | |
| 10804 | if (sortinfo->item_compare_numbers) |
| 10805 | { |
| 10806 | varnumber_T v1 = get_tv_number(tv1); |
| 10807 | varnumber_T v2 = get_tv_number(tv2); |
| 10808 | |
| 10809 | return v1 == v2 ? 0 : v1 > v2 ? 1 : -1; |
| 10810 | } |
| 10811 | |
| 10812 | #ifdef FEAT_FLOAT |
| 10813 | if (sortinfo->item_compare_float) |
| 10814 | { |
| 10815 | float_T v1 = get_tv_float(tv1); |
| 10816 | float_T v2 = get_tv_float(tv2); |
| 10817 | |
| 10818 | return v1 == v2 ? 0 : v1 > v2 ? 1 : -1; |
| 10819 | } |
| 10820 | #endif |
| 10821 | |
| 10822 | /* tv2string() puts quotes around a string and allocates memory. Don't do |
| 10823 | * that for string variables. Use a single quote when comparing with a |
| 10824 | * non-string to do what the docs promise. */ |
| 10825 | if (tv1->v_type == VAR_STRING) |
| 10826 | { |
| 10827 | if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric) |
| 10828 | p1 = (char_u *)"'"; |
| 10829 | else |
| 10830 | p1 = tv1->vval.v_string; |
| 10831 | } |
| 10832 | else |
| 10833 | p1 = tv2string(tv1, &tofree1, numbuf1, 0); |
| 10834 | if (tv2->v_type == VAR_STRING) |
| 10835 | { |
| 10836 | if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric) |
| 10837 | p2 = (char_u *)"'"; |
| 10838 | else |
| 10839 | p2 = tv2->vval.v_string; |
| 10840 | } |
| 10841 | else |
| 10842 | p2 = tv2string(tv2, &tofree2, numbuf2, 0); |
| 10843 | if (p1 == NULL) |
| 10844 | p1 = (char_u *)""; |
| 10845 | if (p2 == NULL) |
| 10846 | p2 = (char_u *)""; |
| 10847 | if (!sortinfo->item_compare_numeric) |
| 10848 | { |
| 10849 | if (sortinfo->item_compare_ic) |
| 10850 | res = STRICMP(p1, p2); |
| 10851 | else |
| 10852 | res = STRCMP(p1, p2); |
| 10853 | } |
| 10854 | else |
| 10855 | { |
| 10856 | double n1, n2; |
| 10857 | n1 = strtod((char *)p1, (char **)&p1); |
| 10858 | n2 = strtod((char *)p2, (char **)&p2); |
| 10859 | res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1; |
| 10860 | } |
| 10861 | |
| 10862 | /* When the result would be zero, compare the item indexes. Makes the |
| 10863 | * sort stable. */ |
| 10864 | if (res == 0 && !sortinfo->item_compare_keep_zero) |
| 10865 | res = si1->idx > si2->idx ? 1 : -1; |
| 10866 | |
| 10867 | vim_free(tofree1); |
| 10868 | vim_free(tofree2); |
| 10869 | return res; |
| 10870 | } |
| 10871 | |
| 10872 | static int |
| 10873 | #ifdef __BORLANDC__ |
| 10874 | _RTLENTRYF |
| 10875 | #endif |
| 10876 | item_compare2(const void *s1, const void *s2) |
| 10877 | { |
| 10878 | sortItem_T *si1, *si2; |
| 10879 | int res; |
| 10880 | typval_T rettv; |
| 10881 | typval_T argv[3]; |
| 10882 | int dummy; |
| 10883 | char_u *func_name; |
| 10884 | partial_T *partial = sortinfo->item_compare_partial; |
| 10885 | |
| 10886 | /* shortcut after failure in previous call; compare all items equal */ |
| 10887 | if (sortinfo->item_compare_func_err) |
| 10888 | return 0; |
| 10889 | |
| 10890 | si1 = (sortItem_T *)s1; |
| 10891 | si2 = (sortItem_T *)s2; |
| 10892 | |
| 10893 | if (partial == NULL) |
| 10894 | func_name = sortinfo->item_compare_func; |
| 10895 | else |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 10896 | func_name = partial_name(partial); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10897 | |
| 10898 | /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED |
| 10899 | * in the copy without changing the original list items. */ |
| 10900 | copy_tv(&si1->item->li_tv, &argv[0]); |
| 10901 | copy_tv(&si2->item->li_tv, &argv[1]); |
| 10902 | |
| 10903 | rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */ |
| 10904 | res = call_func(func_name, (int)STRLEN(func_name), |
Bram Moolenaar | df48fb4 | 2016-07-22 21:50:18 +0200 | [diff] [blame] | 10905 | &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10906 | partial, sortinfo->item_compare_selfdict); |
| 10907 | clear_tv(&argv[0]); |
| 10908 | clear_tv(&argv[1]); |
| 10909 | |
| 10910 | if (res == FAIL) |
| 10911 | res = ITEM_COMPARE_FAIL; |
| 10912 | else |
| 10913 | res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err); |
| 10914 | if (sortinfo->item_compare_func_err) |
| 10915 | res = ITEM_COMPARE_FAIL; /* return value has wrong type */ |
| 10916 | clear_tv(&rettv); |
| 10917 | |
| 10918 | /* When the result would be zero, compare the pointers themselves. Makes |
| 10919 | * the sort stable. */ |
| 10920 | if (res == 0 && !sortinfo->item_compare_keep_zero) |
| 10921 | res = si1->idx > si2->idx ? 1 : -1; |
| 10922 | |
| 10923 | return res; |
| 10924 | } |
| 10925 | |
| 10926 | /* |
| 10927 | * "sort({list})" function |
| 10928 | */ |
| 10929 | static void |
| 10930 | do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort) |
| 10931 | { |
| 10932 | list_T *l; |
| 10933 | listitem_T *li; |
| 10934 | sortItem_T *ptrs; |
| 10935 | sortinfo_T *old_sortinfo; |
| 10936 | sortinfo_T info; |
| 10937 | long len; |
| 10938 | long i; |
| 10939 | |
| 10940 | /* Pointer to current info struct used in compare function. Save and |
| 10941 | * restore the current one for nested calls. */ |
| 10942 | old_sortinfo = sortinfo; |
| 10943 | sortinfo = &info; |
| 10944 | |
| 10945 | if (argvars[0].v_type != VAR_LIST) |
| 10946 | EMSG2(_(e_listarg), sort ? "sort()" : "uniq()"); |
| 10947 | else |
| 10948 | { |
| 10949 | l = argvars[0].vval.v_list; |
| 10950 | if (l == NULL || tv_check_lock(l->lv_lock, |
| 10951 | (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")), |
| 10952 | TRUE)) |
| 10953 | goto theend; |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 10954 | rettv_list_set(rettv, l); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 10955 | |
| 10956 | len = list_len(l); |
| 10957 | if (len <= 1) |
| 10958 | goto theend; /* short list sorts pretty quickly */ |
| 10959 | |
| 10960 | info.item_compare_ic = FALSE; |
| 10961 | info.item_compare_numeric = FALSE; |
| 10962 | info.item_compare_numbers = FALSE; |
| 10963 | #ifdef FEAT_FLOAT |
| 10964 | info.item_compare_float = FALSE; |
| 10965 | #endif |
| 10966 | info.item_compare_func = NULL; |
| 10967 | info.item_compare_partial = NULL; |
| 10968 | info.item_compare_selfdict = NULL; |
| 10969 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 10970 | { |
| 10971 | /* optional second argument: {func} */ |
| 10972 | if (argvars[1].v_type == VAR_FUNC) |
| 10973 | info.item_compare_func = argvars[1].vval.v_string; |
| 10974 | else if (argvars[1].v_type == VAR_PARTIAL) |
| 10975 | info.item_compare_partial = argvars[1].vval.v_partial; |
| 10976 | else |
| 10977 | { |
| 10978 | int error = FALSE; |
| 10979 | |
| 10980 | i = (long)get_tv_number_chk(&argvars[1], &error); |
| 10981 | if (error) |
| 10982 | goto theend; /* type error; errmsg already given */ |
| 10983 | if (i == 1) |
| 10984 | info.item_compare_ic = TRUE; |
| 10985 | else if (argvars[1].v_type != VAR_NUMBER) |
| 10986 | info.item_compare_func = get_tv_string(&argvars[1]); |
| 10987 | else if (i != 0) |
| 10988 | { |
| 10989 | EMSG(_(e_invarg)); |
| 10990 | goto theend; |
| 10991 | } |
| 10992 | if (info.item_compare_func != NULL) |
| 10993 | { |
| 10994 | if (*info.item_compare_func == NUL) |
| 10995 | { |
| 10996 | /* empty string means default sort */ |
| 10997 | info.item_compare_func = NULL; |
| 10998 | } |
| 10999 | else if (STRCMP(info.item_compare_func, "n") == 0) |
| 11000 | { |
| 11001 | info.item_compare_func = NULL; |
| 11002 | info.item_compare_numeric = TRUE; |
| 11003 | } |
| 11004 | else if (STRCMP(info.item_compare_func, "N") == 0) |
| 11005 | { |
| 11006 | info.item_compare_func = NULL; |
| 11007 | info.item_compare_numbers = TRUE; |
| 11008 | } |
| 11009 | #ifdef FEAT_FLOAT |
| 11010 | else if (STRCMP(info.item_compare_func, "f") == 0) |
| 11011 | { |
| 11012 | info.item_compare_func = NULL; |
| 11013 | info.item_compare_float = TRUE; |
| 11014 | } |
| 11015 | #endif |
| 11016 | else if (STRCMP(info.item_compare_func, "i") == 0) |
| 11017 | { |
| 11018 | info.item_compare_func = NULL; |
| 11019 | info.item_compare_ic = TRUE; |
| 11020 | } |
| 11021 | } |
| 11022 | } |
| 11023 | |
| 11024 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 11025 | { |
| 11026 | /* optional third argument: {dict} */ |
| 11027 | if (argvars[2].v_type != VAR_DICT) |
| 11028 | { |
| 11029 | EMSG(_(e_dictreq)); |
| 11030 | goto theend; |
| 11031 | } |
| 11032 | info.item_compare_selfdict = argvars[2].vval.v_dict; |
| 11033 | } |
| 11034 | } |
| 11035 | |
| 11036 | /* Make an array with each entry pointing to an item in the List. */ |
| 11037 | ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T))); |
| 11038 | if (ptrs == NULL) |
| 11039 | goto theend; |
| 11040 | |
| 11041 | i = 0; |
| 11042 | if (sort) |
| 11043 | { |
| 11044 | /* sort(): ptrs will be the list to sort */ |
| 11045 | for (li = l->lv_first; li != NULL; li = li->li_next) |
| 11046 | { |
| 11047 | ptrs[i].item = li; |
| 11048 | ptrs[i].idx = i; |
| 11049 | ++i; |
| 11050 | } |
| 11051 | |
| 11052 | info.item_compare_func_err = FALSE; |
| 11053 | info.item_compare_keep_zero = FALSE; |
| 11054 | /* test the compare function */ |
| 11055 | if ((info.item_compare_func != NULL |
| 11056 | || info.item_compare_partial != NULL) |
| 11057 | && item_compare2((void *)&ptrs[0], (void *)&ptrs[1]) |
| 11058 | == ITEM_COMPARE_FAIL) |
| 11059 | EMSG(_("E702: Sort compare function failed")); |
| 11060 | else |
| 11061 | { |
| 11062 | /* Sort the array with item pointers. */ |
| 11063 | qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T), |
| 11064 | info.item_compare_func == NULL |
| 11065 | && info.item_compare_partial == NULL |
| 11066 | ? item_compare : item_compare2); |
| 11067 | |
| 11068 | if (!info.item_compare_func_err) |
| 11069 | { |
| 11070 | /* Clear the List and append the items in sorted order. */ |
| 11071 | l->lv_first = l->lv_last = l->lv_idx_item = NULL; |
| 11072 | l->lv_len = 0; |
| 11073 | for (i = 0; i < len; ++i) |
| 11074 | list_append(l, ptrs[i].item); |
| 11075 | } |
| 11076 | } |
| 11077 | } |
| 11078 | else |
| 11079 | { |
| 11080 | int (*item_compare_func_ptr)(const void *, const void *); |
| 11081 | |
| 11082 | /* f_uniq(): ptrs will be a stack of items to remove */ |
| 11083 | info.item_compare_func_err = FALSE; |
| 11084 | info.item_compare_keep_zero = TRUE; |
| 11085 | item_compare_func_ptr = info.item_compare_func != NULL |
| 11086 | || info.item_compare_partial != NULL |
| 11087 | ? item_compare2 : item_compare; |
| 11088 | |
| 11089 | for (li = l->lv_first; li != NULL && li->li_next != NULL; |
| 11090 | li = li->li_next) |
| 11091 | { |
| 11092 | if (item_compare_func_ptr((void *)&li, (void *)&li->li_next) |
| 11093 | == 0) |
| 11094 | ptrs[i++].item = li; |
| 11095 | if (info.item_compare_func_err) |
| 11096 | { |
| 11097 | EMSG(_("E882: Uniq compare function failed")); |
| 11098 | break; |
| 11099 | } |
| 11100 | } |
| 11101 | |
| 11102 | if (!info.item_compare_func_err) |
| 11103 | { |
| 11104 | while (--i >= 0) |
| 11105 | { |
| 11106 | li = ptrs[i].item->li_next; |
| 11107 | ptrs[i].item->li_next = li->li_next; |
| 11108 | if (li->li_next != NULL) |
| 11109 | li->li_next->li_prev = ptrs[i].item; |
| 11110 | else |
| 11111 | l->lv_last = ptrs[i].item; |
| 11112 | list_fix_watch(l, li); |
| 11113 | listitem_free(li); |
| 11114 | l->lv_len--; |
| 11115 | } |
| 11116 | } |
| 11117 | } |
| 11118 | |
| 11119 | vim_free(ptrs); |
| 11120 | } |
| 11121 | theend: |
| 11122 | sortinfo = old_sortinfo; |
| 11123 | } |
| 11124 | |
| 11125 | /* |
| 11126 | * "sort({list})" function |
| 11127 | */ |
| 11128 | static void |
| 11129 | f_sort(typval_T *argvars, typval_T *rettv) |
| 11130 | { |
| 11131 | do_sort_uniq(argvars, rettv, TRUE); |
| 11132 | } |
| 11133 | |
| 11134 | /* |
| 11135 | * "uniq({list})" function |
| 11136 | */ |
| 11137 | static void |
| 11138 | f_uniq(typval_T *argvars, typval_T *rettv) |
| 11139 | { |
| 11140 | do_sort_uniq(argvars, rettv, FALSE); |
| 11141 | } |
| 11142 | |
| 11143 | /* |
| 11144 | * "soundfold({word})" function |
| 11145 | */ |
| 11146 | static void |
| 11147 | f_soundfold(typval_T *argvars, typval_T *rettv) |
| 11148 | { |
| 11149 | char_u *s; |
| 11150 | |
| 11151 | rettv->v_type = VAR_STRING; |
| 11152 | s = get_tv_string(&argvars[0]); |
| 11153 | #ifdef FEAT_SPELL |
| 11154 | rettv->vval.v_string = eval_soundfold(s); |
| 11155 | #else |
| 11156 | rettv->vval.v_string = vim_strsave(s); |
| 11157 | #endif |
| 11158 | } |
| 11159 | |
| 11160 | /* |
| 11161 | * "spellbadword()" function |
| 11162 | */ |
| 11163 | static void |
| 11164 | f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv) |
| 11165 | { |
| 11166 | char_u *word = (char_u *)""; |
| 11167 | hlf_T attr = HLF_COUNT; |
| 11168 | int len = 0; |
| 11169 | |
| 11170 | if (rettv_list_alloc(rettv) == FAIL) |
| 11171 | return; |
| 11172 | |
| 11173 | #ifdef FEAT_SPELL |
| 11174 | if (argvars[0].v_type == VAR_UNKNOWN) |
| 11175 | { |
| 11176 | /* Find the start and length of the badly spelled word. */ |
| 11177 | len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr); |
| 11178 | if (len != 0) |
Bram Moolenaar | b73fa62 | 2017-12-21 20:27:47 +0100 | [diff] [blame] | 11179 | { |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11180 | word = ml_get_cursor(); |
Bram Moolenaar | b73fa62 | 2017-12-21 20:27:47 +0100 | [diff] [blame] | 11181 | curwin->w_set_curswant = TRUE; |
| 11182 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11183 | } |
| 11184 | else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL) |
| 11185 | { |
| 11186 | char_u *str = get_tv_string_chk(&argvars[0]); |
| 11187 | int capcol = -1; |
| 11188 | |
| 11189 | if (str != NULL) |
| 11190 | { |
| 11191 | /* Check the argument for spelling. */ |
| 11192 | while (*str != NUL) |
| 11193 | { |
| 11194 | len = spell_check(curwin, str, &attr, &capcol, FALSE); |
| 11195 | if (attr != HLF_COUNT) |
| 11196 | { |
| 11197 | word = str; |
| 11198 | break; |
| 11199 | } |
| 11200 | str += len; |
| 11201 | } |
| 11202 | } |
| 11203 | } |
| 11204 | #endif |
| 11205 | |
| 11206 | list_append_string(rettv->vval.v_list, word, len); |
| 11207 | list_append_string(rettv->vval.v_list, (char_u *)( |
| 11208 | attr == HLF_SPB ? "bad" : |
| 11209 | attr == HLF_SPR ? "rare" : |
| 11210 | attr == HLF_SPL ? "local" : |
| 11211 | attr == HLF_SPC ? "caps" : |
| 11212 | ""), -1); |
| 11213 | } |
| 11214 | |
| 11215 | /* |
| 11216 | * "spellsuggest()" function |
| 11217 | */ |
| 11218 | static void |
| 11219 | f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv) |
| 11220 | { |
| 11221 | #ifdef FEAT_SPELL |
| 11222 | char_u *str; |
| 11223 | int typeerr = FALSE; |
| 11224 | int maxcount; |
| 11225 | garray_T ga; |
| 11226 | int i; |
| 11227 | listitem_T *li; |
| 11228 | int need_capital = FALSE; |
| 11229 | #endif |
| 11230 | |
| 11231 | if (rettv_list_alloc(rettv) == FAIL) |
| 11232 | return; |
| 11233 | |
| 11234 | #ifdef FEAT_SPELL |
| 11235 | if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL) |
| 11236 | { |
| 11237 | str = get_tv_string(&argvars[0]); |
| 11238 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 11239 | { |
| 11240 | maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr); |
| 11241 | if (maxcount <= 0) |
| 11242 | return; |
| 11243 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 11244 | { |
| 11245 | need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr); |
| 11246 | if (typeerr) |
| 11247 | return; |
| 11248 | } |
| 11249 | } |
| 11250 | else |
| 11251 | maxcount = 25; |
| 11252 | |
| 11253 | spell_suggest_list(&ga, str, maxcount, need_capital, FALSE); |
| 11254 | |
| 11255 | for (i = 0; i < ga.ga_len; ++i) |
| 11256 | { |
| 11257 | str = ((char_u **)ga.ga_data)[i]; |
| 11258 | |
| 11259 | li = listitem_alloc(); |
| 11260 | if (li == NULL) |
| 11261 | vim_free(str); |
| 11262 | else |
| 11263 | { |
| 11264 | li->li_tv.v_type = VAR_STRING; |
| 11265 | li->li_tv.v_lock = 0; |
| 11266 | li->li_tv.vval.v_string = str; |
| 11267 | list_append(rettv->vval.v_list, li); |
| 11268 | } |
| 11269 | } |
| 11270 | ga_clear(&ga); |
| 11271 | } |
| 11272 | #endif |
| 11273 | } |
| 11274 | |
| 11275 | static void |
| 11276 | f_split(typval_T *argvars, typval_T *rettv) |
| 11277 | { |
| 11278 | char_u *str; |
| 11279 | char_u *end; |
| 11280 | char_u *pat = NULL; |
| 11281 | regmatch_T regmatch; |
| 11282 | char_u patbuf[NUMBUFLEN]; |
| 11283 | char_u *save_cpo; |
| 11284 | int match; |
| 11285 | colnr_T col = 0; |
| 11286 | int keepempty = FALSE; |
| 11287 | int typeerr = FALSE; |
| 11288 | |
| 11289 | /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ |
| 11290 | save_cpo = p_cpo; |
| 11291 | p_cpo = (char_u *)""; |
| 11292 | |
| 11293 | str = get_tv_string(&argvars[0]); |
| 11294 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 11295 | { |
| 11296 | pat = get_tv_string_buf_chk(&argvars[1], patbuf); |
| 11297 | if (pat == NULL) |
| 11298 | typeerr = TRUE; |
| 11299 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 11300 | keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr); |
| 11301 | } |
| 11302 | if (pat == NULL || *pat == NUL) |
| 11303 | pat = (char_u *)"[\\x01- ]\\+"; |
| 11304 | |
| 11305 | if (rettv_list_alloc(rettv) == FAIL) |
| 11306 | return; |
| 11307 | if (typeerr) |
| 11308 | return; |
| 11309 | |
| 11310 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); |
| 11311 | if (regmatch.regprog != NULL) |
| 11312 | { |
| 11313 | regmatch.rm_ic = FALSE; |
| 11314 | while (*str != NUL || keepempty) |
| 11315 | { |
| 11316 | if (*str == NUL) |
| 11317 | match = FALSE; /* empty item at the end */ |
| 11318 | else |
| 11319 | match = vim_regexec_nl(®match, str, col); |
| 11320 | if (match) |
| 11321 | end = regmatch.startp[0]; |
| 11322 | else |
| 11323 | end = str + STRLEN(str); |
| 11324 | if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0 |
| 11325 | && *str != NUL && match && end < regmatch.endp[0])) |
| 11326 | { |
| 11327 | if (list_append_string(rettv->vval.v_list, str, |
| 11328 | (int)(end - str)) == FAIL) |
| 11329 | break; |
| 11330 | } |
| 11331 | if (!match) |
| 11332 | break; |
| 11333 | /* Advance to just after the match. */ |
| 11334 | if (regmatch.endp[0] > str) |
| 11335 | col = 0; |
| 11336 | else |
| 11337 | { |
| 11338 | /* Don't get stuck at the same match. */ |
| 11339 | #ifdef FEAT_MBYTE |
| 11340 | col = (*mb_ptr2len)(regmatch.endp[0]); |
| 11341 | #else |
| 11342 | col = 1; |
| 11343 | #endif |
| 11344 | } |
| 11345 | str = regmatch.endp[0]; |
| 11346 | } |
| 11347 | |
| 11348 | vim_regfree(regmatch.regprog); |
| 11349 | } |
| 11350 | |
| 11351 | p_cpo = save_cpo; |
| 11352 | } |
| 11353 | |
| 11354 | #ifdef FEAT_FLOAT |
| 11355 | /* |
| 11356 | * "sqrt()" function |
| 11357 | */ |
| 11358 | static void |
| 11359 | f_sqrt(typval_T *argvars, typval_T *rettv) |
| 11360 | { |
| 11361 | float_T f = 0.0; |
| 11362 | |
| 11363 | rettv->v_type = VAR_FLOAT; |
| 11364 | if (get_float_arg(argvars, &f) == OK) |
| 11365 | rettv->vval.v_float = sqrt(f); |
| 11366 | else |
| 11367 | rettv->vval.v_float = 0.0; |
| 11368 | } |
| 11369 | |
| 11370 | /* |
| 11371 | * "str2float()" function |
| 11372 | */ |
| 11373 | static void |
| 11374 | f_str2float(typval_T *argvars, typval_T *rettv) |
| 11375 | { |
| 11376 | char_u *p = skipwhite(get_tv_string(&argvars[0])); |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 11377 | int isneg = (*p == '-'); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11378 | |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 11379 | if (*p == '+' || *p == '-') |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11380 | p = skipwhite(p + 1); |
| 11381 | (void)string2float(p, &rettv->vval.v_float); |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 11382 | if (isneg) |
| 11383 | rettv->vval.v_float *= -1; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11384 | rettv->v_type = VAR_FLOAT; |
| 11385 | } |
| 11386 | #endif |
| 11387 | |
| 11388 | /* |
| 11389 | * "str2nr()" function |
| 11390 | */ |
| 11391 | static void |
| 11392 | f_str2nr(typval_T *argvars, typval_T *rettv) |
| 11393 | { |
| 11394 | int base = 10; |
| 11395 | char_u *p; |
| 11396 | varnumber_T n; |
| 11397 | int what; |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 11398 | int isneg; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11399 | |
| 11400 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 11401 | { |
| 11402 | base = (int)get_tv_number(&argvars[1]); |
| 11403 | if (base != 2 && base != 8 && base != 10 && base != 16) |
| 11404 | { |
| 11405 | EMSG(_(e_invarg)); |
| 11406 | return; |
| 11407 | } |
| 11408 | } |
| 11409 | |
| 11410 | p = skipwhite(get_tv_string(&argvars[0])); |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 11411 | isneg = (*p == '-'); |
| 11412 | if (*p == '+' || *p == '-') |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11413 | p = skipwhite(p + 1); |
| 11414 | switch (base) |
| 11415 | { |
| 11416 | case 2: what = STR2NR_BIN + STR2NR_FORCE; break; |
| 11417 | case 8: what = STR2NR_OCT + STR2NR_FORCE; break; |
| 11418 | case 16: what = STR2NR_HEX + STR2NR_FORCE; break; |
| 11419 | default: what = 0; |
| 11420 | } |
| 11421 | vim_str2nr(p, NULL, NULL, what, &n, NULL, 0); |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 11422 | if (isneg) |
| 11423 | rettv->vval.v_number = -n; |
| 11424 | else |
| 11425 | rettv->vval.v_number = n; |
| 11426 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11427 | } |
| 11428 | |
| 11429 | #ifdef HAVE_STRFTIME |
| 11430 | /* |
| 11431 | * "strftime({format}[, {time}])" function |
| 11432 | */ |
| 11433 | static void |
| 11434 | f_strftime(typval_T *argvars, typval_T *rettv) |
| 11435 | { |
| 11436 | char_u result_buf[256]; |
| 11437 | struct tm *curtime; |
| 11438 | time_t seconds; |
| 11439 | char_u *p; |
| 11440 | |
| 11441 | rettv->v_type = VAR_STRING; |
| 11442 | |
| 11443 | p = get_tv_string(&argvars[0]); |
| 11444 | if (argvars[1].v_type == VAR_UNKNOWN) |
| 11445 | seconds = time(NULL); |
| 11446 | else |
| 11447 | seconds = (time_t)get_tv_number(&argvars[1]); |
| 11448 | curtime = localtime(&seconds); |
| 11449 | /* MSVC returns NULL for an invalid value of seconds. */ |
| 11450 | if (curtime == NULL) |
| 11451 | rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)")); |
| 11452 | else |
| 11453 | { |
| 11454 | # ifdef FEAT_MBYTE |
| 11455 | vimconv_T conv; |
| 11456 | char_u *enc; |
| 11457 | |
| 11458 | conv.vc_type = CONV_NONE; |
| 11459 | enc = enc_locale(); |
| 11460 | convert_setup(&conv, p_enc, enc); |
| 11461 | if (conv.vc_type != CONV_NONE) |
| 11462 | p = string_convert(&conv, p, NULL); |
| 11463 | # endif |
| 11464 | if (p != NULL) |
| 11465 | (void)strftime((char *)result_buf, sizeof(result_buf), |
| 11466 | (char *)p, curtime); |
| 11467 | else |
| 11468 | result_buf[0] = NUL; |
| 11469 | |
| 11470 | # ifdef FEAT_MBYTE |
| 11471 | if (conv.vc_type != CONV_NONE) |
| 11472 | vim_free(p); |
| 11473 | convert_setup(&conv, enc, p_enc); |
| 11474 | if (conv.vc_type != CONV_NONE) |
| 11475 | rettv->vval.v_string = string_convert(&conv, result_buf, NULL); |
| 11476 | else |
| 11477 | # endif |
| 11478 | rettv->vval.v_string = vim_strsave(result_buf); |
| 11479 | |
| 11480 | # ifdef FEAT_MBYTE |
| 11481 | /* Release conversion descriptors */ |
| 11482 | convert_setup(&conv, NULL, NULL); |
| 11483 | vim_free(enc); |
| 11484 | # endif |
| 11485 | } |
| 11486 | } |
| 11487 | #endif |
| 11488 | |
| 11489 | /* |
| 11490 | * "strgetchar()" function |
| 11491 | */ |
| 11492 | static void |
| 11493 | f_strgetchar(typval_T *argvars, typval_T *rettv) |
| 11494 | { |
| 11495 | char_u *str; |
| 11496 | int len; |
| 11497 | int error = FALSE; |
| 11498 | int charidx; |
| 11499 | |
| 11500 | rettv->vval.v_number = -1; |
| 11501 | str = get_tv_string_chk(&argvars[0]); |
| 11502 | if (str == NULL) |
| 11503 | return; |
| 11504 | len = (int)STRLEN(str); |
| 11505 | charidx = (int)get_tv_number_chk(&argvars[1], &error); |
| 11506 | if (error) |
| 11507 | return; |
| 11508 | #ifdef FEAT_MBYTE |
| 11509 | { |
| 11510 | int byteidx = 0; |
| 11511 | |
| 11512 | while (charidx >= 0 && byteidx < len) |
| 11513 | { |
| 11514 | if (charidx == 0) |
| 11515 | { |
| 11516 | rettv->vval.v_number = mb_ptr2char(str + byteidx); |
| 11517 | break; |
| 11518 | } |
| 11519 | --charidx; |
Bram Moolenaar | d3c907b | 2016-08-17 21:32:09 +0200 | [diff] [blame] | 11520 | byteidx += MB_CPTR2LEN(str + byteidx); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11521 | } |
| 11522 | } |
| 11523 | #else |
| 11524 | if (charidx < len) |
| 11525 | rettv->vval.v_number = str[charidx]; |
| 11526 | #endif |
| 11527 | } |
| 11528 | |
| 11529 | /* |
| 11530 | * "stridx()" function |
| 11531 | */ |
| 11532 | static void |
| 11533 | f_stridx(typval_T *argvars, typval_T *rettv) |
| 11534 | { |
| 11535 | char_u buf[NUMBUFLEN]; |
| 11536 | char_u *needle; |
| 11537 | char_u *haystack; |
| 11538 | char_u *save_haystack; |
| 11539 | char_u *pos; |
| 11540 | int start_idx; |
| 11541 | |
| 11542 | needle = get_tv_string_chk(&argvars[1]); |
| 11543 | save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf); |
| 11544 | rettv->vval.v_number = -1; |
| 11545 | if (needle == NULL || haystack == NULL) |
| 11546 | return; /* type error; errmsg already given */ |
| 11547 | |
| 11548 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 11549 | { |
| 11550 | int error = FALSE; |
| 11551 | |
| 11552 | start_idx = (int)get_tv_number_chk(&argvars[2], &error); |
| 11553 | if (error || start_idx >= (int)STRLEN(haystack)) |
| 11554 | return; |
| 11555 | if (start_idx >= 0) |
| 11556 | haystack += start_idx; |
| 11557 | } |
| 11558 | |
| 11559 | pos = (char_u *)strstr((char *)haystack, (char *)needle); |
| 11560 | if (pos != NULL) |
| 11561 | rettv->vval.v_number = (varnumber_T)(pos - save_haystack); |
| 11562 | } |
| 11563 | |
| 11564 | /* |
| 11565 | * "string()" function |
| 11566 | */ |
| 11567 | static void |
| 11568 | f_string(typval_T *argvars, typval_T *rettv) |
| 11569 | { |
| 11570 | char_u *tofree; |
| 11571 | char_u numbuf[NUMBUFLEN]; |
| 11572 | |
| 11573 | rettv->v_type = VAR_STRING; |
| 11574 | rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, |
| 11575 | get_copyID()); |
| 11576 | /* Make a copy if we have a value but it's not in allocated memory. */ |
| 11577 | if (rettv->vval.v_string != NULL && tofree == NULL) |
| 11578 | rettv->vval.v_string = vim_strsave(rettv->vval.v_string); |
| 11579 | } |
| 11580 | |
| 11581 | /* |
| 11582 | * "strlen()" function |
| 11583 | */ |
| 11584 | static void |
| 11585 | f_strlen(typval_T *argvars, typval_T *rettv) |
| 11586 | { |
| 11587 | rettv->vval.v_number = (varnumber_T)(STRLEN( |
| 11588 | get_tv_string(&argvars[0]))); |
| 11589 | } |
| 11590 | |
| 11591 | /* |
| 11592 | * "strchars()" function |
| 11593 | */ |
| 11594 | static void |
| 11595 | f_strchars(typval_T *argvars, typval_T *rettv) |
| 11596 | { |
| 11597 | char_u *s = get_tv_string(&argvars[0]); |
| 11598 | int skipcc = 0; |
| 11599 | #ifdef FEAT_MBYTE |
| 11600 | varnumber_T len = 0; |
| 11601 | int (*func_mb_ptr2char_adv)(char_u **pp); |
| 11602 | #endif |
| 11603 | |
| 11604 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 11605 | skipcc = (int)get_tv_number_chk(&argvars[1], NULL); |
| 11606 | if (skipcc < 0 || skipcc > 1) |
| 11607 | EMSG(_(e_invarg)); |
| 11608 | else |
| 11609 | { |
| 11610 | #ifdef FEAT_MBYTE |
| 11611 | func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv; |
| 11612 | while (*s != NUL) |
| 11613 | { |
| 11614 | func_mb_ptr2char_adv(&s); |
| 11615 | ++len; |
| 11616 | } |
| 11617 | rettv->vval.v_number = len; |
| 11618 | #else |
| 11619 | rettv->vval.v_number = (varnumber_T)(STRLEN(s)); |
| 11620 | #endif |
| 11621 | } |
| 11622 | } |
| 11623 | |
| 11624 | /* |
| 11625 | * "strdisplaywidth()" function |
| 11626 | */ |
| 11627 | static void |
| 11628 | f_strdisplaywidth(typval_T *argvars, typval_T *rettv) |
| 11629 | { |
| 11630 | char_u *s = get_tv_string(&argvars[0]); |
| 11631 | int col = 0; |
| 11632 | |
| 11633 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 11634 | col = (int)get_tv_number(&argvars[1]); |
| 11635 | |
| 11636 | rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col); |
| 11637 | } |
| 11638 | |
| 11639 | /* |
| 11640 | * "strwidth()" function |
| 11641 | */ |
| 11642 | static void |
| 11643 | f_strwidth(typval_T *argvars, typval_T *rettv) |
| 11644 | { |
| 11645 | char_u *s = get_tv_string(&argvars[0]); |
| 11646 | |
| 11647 | rettv->vval.v_number = (varnumber_T)( |
| 11648 | #ifdef FEAT_MBYTE |
| 11649 | mb_string2cells(s, -1) |
| 11650 | #else |
| 11651 | STRLEN(s) |
| 11652 | #endif |
| 11653 | ); |
| 11654 | } |
| 11655 | |
| 11656 | /* |
| 11657 | * "strcharpart()" function |
| 11658 | */ |
| 11659 | static void |
| 11660 | f_strcharpart(typval_T *argvars, typval_T *rettv) |
| 11661 | { |
| 11662 | #ifdef FEAT_MBYTE |
| 11663 | char_u *p; |
| 11664 | int nchar; |
| 11665 | int nbyte = 0; |
| 11666 | int charlen; |
| 11667 | int len = 0; |
| 11668 | int slen; |
| 11669 | int error = FALSE; |
| 11670 | |
| 11671 | p = get_tv_string(&argvars[0]); |
| 11672 | slen = (int)STRLEN(p); |
| 11673 | |
| 11674 | nchar = (int)get_tv_number_chk(&argvars[1], &error); |
| 11675 | if (!error) |
| 11676 | { |
| 11677 | if (nchar > 0) |
| 11678 | while (nchar > 0 && nbyte < slen) |
| 11679 | { |
Bram Moolenaar | d3c907b | 2016-08-17 21:32:09 +0200 | [diff] [blame] | 11680 | nbyte += MB_CPTR2LEN(p + nbyte); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11681 | --nchar; |
| 11682 | } |
| 11683 | else |
| 11684 | nbyte = nchar; |
| 11685 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 11686 | { |
| 11687 | charlen = (int)get_tv_number(&argvars[2]); |
| 11688 | while (charlen > 0 && nbyte + len < slen) |
| 11689 | { |
| 11690 | int off = nbyte + len; |
| 11691 | |
| 11692 | if (off < 0) |
| 11693 | len += 1; |
| 11694 | else |
Bram Moolenaar | d3c907b | 2016-08-17 21:32:09 +0200 | [diff] [blame] | 11695 | len += MB_CPTR2LEN(p + off); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11696 | --charlen; |
| 11697 | } |
| 11698 | } |
| 11699 | else |
| 11700 | len = slen - nbyte; /* default: all bytes that are available. */ |
| 11701 | } |
| 11702 | |
| 11703 | /* |
| 11704 | * Only return the overlap between the specified part and the actual |
| 11705 | * string. |
| 11706 | */ |
| 11707 | if (nbyte < 0) |
| 11708 | { |
| 11709 | len += nbyte; |
| 11710 | nbyte = 0; |
| 11711 | } |
| 11712 | else if (nbyte > slen) |
| 11713 | nbyte = slen; |
| 11714 | if (len < 0) |
| 11715 | len = 0; |
| 11716 | else if (nbyte + len > slen) |
| 11717 | len = slen - nbyte; |
| 11718 | |
| 11719 | rettv->v_type = VAR_STRING; |
| 11720 | rettv->vval.v_string = vim_strnsave(p + nbyte, len); |
| 11721 | #else |
| 11722 | f_strpart(argvars, rettv); |
| 11723 | #endif |
| 11724 | } |
| 11725 | |
| 11726 | /* |
| 11727 | * "strpart()" function |
| 11728 | */ |
| 11729 | static void |
| 11730 | f_strpart(typval_T *argvars, typval_T *rettv) |
| 11731 | { |
| 11732 | char_u *p; |
| 11733 | int n; |
| 11734 | int len; |
| 11735 | int slen; |
| 11736 | int error = FALSE; |
| 11737 | |
| 11738 | p = get_tv_string(&argvars[0]); |
| 11739 | slen = (int)STRLEN(p); |
| 11740 | |
| 11741 | n = (int)get_tv_number_chk(&argvars[1], &error); |
| 11742 | if (error) |
| 11743 | len = 0; |
| 11744 | else if (argvars[2].v_type != VAR_UNKNOWN) |
| 11745 | len = (int)get_tv_number(&argvars[2]); |
| 11746 | else |
| 11747 | len = slen - n; /* default len: all bytes that are available. */ |
| 11748 | |
| 11749 | /* |
| 11750 | * Only return the overlap between the specified part and the actual |
| 11751 | * string. |
| 11752 | */ |
| 11753 | if (n < 0) |
| 11754 | { |
| 11755 | len += n; |
| 11756 | n = 0; |
| 11757 | } |
| 11758 | else if (n > slen) |
| 11759 | n = slen; |
| 11760 | if (len < 0) |
| 11761 | len = 0; |
| 11762 | else if (n + len > slen) |
| 11763 | len = slen - n; |
| 11764 | |
| 11765 | rettv->v_type = VAR_STRING; |
| 11766 | rettv->vval.v_string = vim_strnsave(p + n, len); |
| 11767 | } |
| 11768 | |
| 11769 | /* |
| 11770 | * "strridx()" function |
| 11771 | */ |
| 11772 | static void |
| 11773 | f_strridx(typval_T *argvars, typval_T *rettv) |
| 11774 | { |
| 11775 | char_u buf[NUMBUFLEN]; |
| 11776 | char_u *needle; |
| 11777 | char_u *haystack; |
| 11778 | char_u *rest; |
| 11779 | char_u *lastmatch = NULL; |
| 11780 | int haystack_len, end_idx; |
| 11781 | |
| 11782 | needle = get_tv_string_chk(&argvars[1]); |
| 11783 | haystack = get_tv_string_buf_chk(&argvars[0], buf); |
| 11784 | |
| 11785 | rettv->vval.v_number = -1; |
| 11786 | if (needle == NULL || haystack == NULL) |
| 11787 | return; /* type error; errmsg already given */ |
| 11788 | |
| 11789 | haystack_len = (int)STRLEN(haystack); |
| 11790 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 11791 | { |
| 11792 | /* Third argument: upper limit for index */ |
| 11793 | end_idx = (int)get_tv_number_chk(&argvars[2], NULL); |
| 11794 | if (end_idx < 0) |
| 11795 | return; /* can never find a match */ |
| 11796 | } |
| 11797 | else |
| 11798 | end_idx = haystack_len; |
| 11799 | |
| 11800 | if (*needle == NUL) |
| 11801 | { |
| 11802 | /* Empty string matches past the end. */ |
| 11803 | lastmatch = haystack + end_idx; |
| 11804 | } |
| 11805 | else |
| 11806 | { |
| 11807 | for (rest = haystack; *rest != '\0'; ++rest) |
| 11808 | { |
| 11809 | rest = (char_u *)strstr((char *)rest, (char *)needle); |
| 11810 | if (rest == NULL || rest > haystack + end_idx) |
| 11811 | break; |
| 11812 | lastmatch = rest; |
| 11813 | } |
| 11814 | } |
| 11815 | |
| 11816 | if (lastmatch == NULL) |
| 11817 | rettv->vval.v_number = -1; |
| 11818 | else |
| 11819 | rettv->vval.v_number = (varnumber_T)(lastmatch - haystack); |
| 11820 | } |
| 11821 | |
| 11822 | /* |
| 11823 | * "strtrans()" function |
| 11824 | */ |
| 11825 | static void |
| 11826 | f_strtrans(typval_T *argvars, typval_T *rettv) |
| 11827 | { |
| 11828 | rettv->v_type = VAR_STRING; |
| 11829 | rettv->vval.v_string = transstr(get_tv_string(&argvars[0])); |
| 11830 | } |
| 11831 | |
| 11832 | /* |
| 11833 | * "submatch()" function |
| 11834 | */ |
| 11835 | static void |
| 11836 | f_submatch(typval_T *argvars, typval_T *rettv) |
| 11837 | { |
| 11838 | int error = FALSE; |
| 11839 | int no; |
| 11840 | int retList = 0; |
| 11841 | |
| 11842 | no = (int)get_tv_number_chk(&argvars[0], &error); |
| 11843 | if (error) |
| 11844 | return; |
Bram Moolenaar | 989f592 | 2016-08-21 15:26:54 +0200 | [diff] [blame] | 11845 | if (no < 0 || no >= NSUBEXP) |
| 11846 | { |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 11847 | EMSGN(_("E935: invalid submatch number: %d"), no); |
| 11848 | return; |
Bram Moolenaar | 989f592 | 2016-08-21 15:26:54 +0200 | [diff] [blame] | 11849 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11850 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 11851 | retList = (int)get_tv_number_chk(&argvars[1], &error); |
| 11852 | if (error) |
| 11853 | return; |
| 11854 | |
| 11855 | if (retList == 0) |
| 11856 | { |
| 11857 | rettv->v_type = VAR_STRING; |
| 11858 | rettv->vval.v_string = reg_submatch(no); |
| 11859 | } |
| 11860 | else |
| 11861 | { |
| 11862 | rettv->v_type = VAR_LIST; |
| 11863 | rettv->vval.v_list = reg_submatch_list(no); |
| 11864 | } |
| 11865 | } |
| 11866 | |
| 11867 | /* |
| 11868 | * "substitute()" function |
| 11869 | */ |
| 11870 | static void |
| 11871 | f_substitute(typval_T *argvars, typval_T *rettv) |
| 11872 | { |
| 11873 | char_u patbuf[NUMBUFLEN]; |
| 11874 | char_u subbuf[NUMBUFLEN]; |
| 11875 | char_u flagsbuf[NUMBUFLEN]; |
| 11876 | |
| 11877 | char_u *str = get_tv_string_chk(&argvars[0]); |
| 11878 | char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf); |
Bram Moolenaar | 72ab729 | 2016-07-19 19:10:51 +0200 | [diff] [blame] | 11879 | char_u *sub = NULL; |
| 11880 | typval_T *expr = NULL; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11881 | char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf); |
| 11882 | |
Bram Moolenaar | 72ab729 | 2016-07-19 19:10:51 +0200 | [diff] [blame] | 11883 | if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL) |
| 11884 | expr = &argvars[2]; |
| 11885 | else |
| 11886 | sub = get_tv_string_buf_chk(&argvars[2], subbuf); |
| 11887 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11888 | rettv->v_type = VAR_STRING; |
Bram Moolenaar | 72ab729 | 2016-07-19 19:10:51 +0200 | [diff] [blame] | 11889 | if (str == NULL || pat == NULL || (sub == NULL && expr == NULL) |
| 11890 | || flg == NULL) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11891 | rettv->vval.v_string = NULL; |
| 11892 | else |
Bram Moolenaar | 72ab729 | 2016-07-19 19:10:51 +0200 | [diff] [blame] | 11893 | rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11894 | } |
| 11895 | |
| 11896 | /* |
| 11897 | * "synID(lnum, col, trans)" function |
| 11898 | */ |
| 11899 | static void |
| 11900 | f_synID(typval_T *argvars UNUSED, typval_T *rettv) |
| 11901 | { |
| 11902 | int id = 0; |
| 11903 | #ifdef FEAT_SYN_HL |
| 11904 | linenr_T lnum; |
| 11905 | colnr_T col; |
| 11906 | int trans; |
| 11907 | int transerr = FALSE; |
| 11908 | |
| 11909 | lnum = get_tv_lnum(argvars); /* -1 on type error */ |
| 11910 | col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */ |
| 11911 | trans = (int)get_tv_number_chk(&argvars[2], &transerr); |
| 11912 | |
| 11913 | if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count |
| 11914 | && col >= 0 && col < (long)STRLEN(ml_get(lnum))) |
| 11915 | id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE); |
| 11916 | #endif |
| 11917 | |
| 11918 | rettv->vval.v_number = id; |
| 11919 | } |
| 11920 | |
| 11921 | /* |
| 11922 | * "synIDattr(id, what [, mode])" function |
| 11923 | */ |
| 11924 | static void |
| 11925 | f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv) |
| 11926 | { |
| 11927 | char_u *p = NULL; |
| 11928 | #ifdef FEAT_SYN_HL |
| 11929 | int id; |
| 11930 | char_u *what; |
| 11931 | char_u *mode; |
| 11932 | char_u modebuf[NUMBUFLEN]; |
| 11933 | int modec; |
| 11934 | |
| 11935 | id = (int)get_tv_number(&argvars[0]); |
| 11936 | what = get_tv_string(&argvars[1]); |
| 11937 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 11938 | { |
| 11939 | mode = get_tv_string_buf(&argvars[2], modebuf); |
| 11940 | modec = TOLOWER_ASC(mode[0]); |
| 11941 | if (modec != 't' && modec != 'c' && modec != 'g') |
| 11942 | modec = 0; /* replace invalid with current */ |
| 11943 | } |
| 11944 | else |
| 11945 | { |
| 11946 | #if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) |
| 11947 | if (USE_24BIT) |
| 11948 | modec = 'g'; |
| 11949 | else |
| 11950 | #endif |
| 11951 | if (t_colors > 1) |
| 11952 | modec = 'c'; |
| 11953 | else |
| 11954 | modec = 't'; |
| 11955 | } |
| 11956 | |
| 11957 | |
| 11958 | switch (TOLOWER_ASC(what[0])) |
| 11959 | { |
| 11960 | case 'b': |
| 11961 | if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */ |
| 11962 | p = highlight_color(id, what, modec); |
| 11963 | else /* bold */ |
| 11964 | p = highlight_has_attr(id, HL_BOLD, modec); |
| 11965 | break; |
| 11966 | |
| 11967 | case 'f': /* fg[#] or font */ |
| 11968 | p = highlight_color(id, what, modec); |
| 11969 | break; |
| 11970 | |
| 11971 | case 'i': |
| 11972 | if (TOLOWER_ASC(what[1]) == 'n') /* inverse */ |
| 11973 | p = highlight_has_attr(id, HL_INVERSE, modec); |
| 11974 | else /* italic */ |
| 11975 | p = highlight_has_attr(id, HL_ITALIC, modec); |
| 11976 | break; |
| 11977 | |
| 11978 | case 'n': /* name */ |
Bram Moolenaar | c96272e | 2017-03-26 13:50:09 +0200 | [diff] [blame] | 11979 | p = get_highlight_name_ext(NULL, id - 1, FALSE); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11980 | break; |
| 11981 | |
| 11982 | case 'r': /* reverse */ |
| 11983 | p = highlight_has_attr(id, HL_INVERSE, modec); |
| 11984 | break; |
| 11985 | |
| 11986 | case 's': |
| 11987 | if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */ |
| 11988 | p = highlight_color(id, what, modec); |
Bram Moolenaar | cf4b00c | 2017-09-02 18:33:56 +0200 | [diff] [blame] | 11989 | /* strikeout */ |
| 11990 | else if (TOLOWER_ASC(what[1]) == 't' && |
| 11991 | TOLOWER_ASC(what[2]) == 'r') |
| 11992 | p = highlight_has_attr(id, HL_STRIKETHROUGH, modec); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 11993 | else /* standout */ |
| 11994 | p = highlight_has_attr(id, HL_STANDOUT, modec); |
| 11995 | break; |
| 11996 | |
| 11997 | case 'u': |
| 11998 | if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c') |
| 11999 | /* underline */ |
| 12000 | p = highlight_has_attr(id, HL_UNDERLINE, modec); |
| 12001 | else |
| 12002 | /* undercurl */ |
| 12003 | p = highlight_has_attr(id, HL_UNDERCURL, modec); |
| 12004 | break; |
| 12005 | } |
| 12006 | |
| 12007 | if (p != NULL) |
| 12008 | p = vim_strsave(p); |
| 12009 | #endif |
| 12010 | rettv->v_type = VAR_STRING; |
| 12011 | rettv->vval.v_string = p; |
| 12012 | } |
| 12013 | |
| 12014 | /* |
| 12015 | * "synIDtrans(id)" function |
| 12016 | */ |
| 12017 | static void |
| 12018 | f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv) |
| 12019 | { |
| 12020 | int id; |
| 12021 | |
| 12022 | #ifdef FEAT_SYN_HL |
| 12023 | id = (int)get_tv_number(&argvars[0]); |
| 12024 | |
| 12025 | if (id > 0) |
| 12026 | id = syn_get_final_id(id); |
| 12027 | else |
| 12028 | #endif |
| 12029 | id = 0; |
| 12030 | |
| 12031 | rettv->vval.v_number = id; |
| 12032 | } |
| 12033 | |
| 12034 | /* |
| 12035 | * "synconcealed(lnum, col)" function |
| 12036 | */ |
| 12037 | static void |
| 12038 | f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv) |
| 12039 | { |
| 12040 | #if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL) |
| 12041 | linenr_T lnum; |
| 12042 | colnr_T col; |
| 12043 | int syntax_flags = 0; |
| 12044 | int cchar; |
| 12045 | int matchid = 0; |
| 12046 | char_u str[NUMBUFLEN]; |
| 12047 | #endif |
| 12048 | |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 12049 | rettv_list_set(rettv, NULL); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12050 | |
| 12051 | #if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL) |
| 12052 | lnum = get_tv_lnum(argvars); /* -1 on type error */ |
| 12053 | col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */ |
| 12054 | |
| 12055 | vim_memset(str, NUL, sizeof(str)); |
| 12056 | |
| 12057 | if (rettv_list_alloc(rettv) != FAIL) |
| 12058 | { |
| 12059 | if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count |
| 12060 | && col >= 0 && col <= (long)STRLEN(ml_get(lnum)) |
| 12061 | && curwin->w_p_cole > 0) |
| 12062 | { |
| 12063 | (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE); |
| 12064 | syntax_flags = get_syntax_info(&matchid); |
| 12065 | |
| 12066 | /* get the conceal character */ |
| 12067 | if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3) |
| 12068 | { |
| 12069 | cchar = syn_get_sub_char(); |
Bram Moolenaar | 4d78589 | 2017-06-22 22:00:50 +0200 | [diff] [blame] | 12070 | if (cchar == NUL && curwin->w_p_cole == 1) |
| 12071 | cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12072 | if (cchar != NUL) |
| 12073 | { |
| 12074 | # ifdef FEAT_MBYTE |
| 12075 | if (has_mbyte) |
| 12076 | (*mb_char2bytes)(cchar, str); |
| 12077 | else |
| 12078 | # endif |
| 12079 | str[0] = cchar; |
| 12080 | } |
| 12081 | } |
| 12082 | } |
| 12083 | |
| 12084 | list_append_number(rettv->vval.v_list, |
| 12085 | (syntax_flags & HL_CONCEAL) != 0); |
| 12086 | /* -1 to auto-determine strlen */ |
| 12087 | list_append_string(rettv->vval.v_list, str, -1); |
| 12088 | list_append_number(rettv->vval.v_list, matchid); |
| 12089 | } |
| 12090 | #endif |
| 12091 | } |
| 12092 | |
| 12093 | /* |
| 12094 | * "synstack(lnum, col)" function |
| 12095 | */ |
| 12096 | static void |
| 12097 | f_synstack(typval_T *argvars UNUSED, typval_T *rettv) |
| 12098 | { |
| 12099 | #ifdef FEAT_SYN_HL |
| 12100 | linenr_T lnum; |
| 12101 | colnr_T col; |
| 12102 | int i; |
| 12103 | int id; |
| 12104 | #endif |
| 12105 | |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 12106 | rettv_list_set(rettv, NULL); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12107 | |
| 12108 | #ifdef FEAT_SYN_HL |
| 12109 | lnum = get_tv_lnum(argvars); /* -1 on type error */ |
| 12110 | col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */ |
| 12111 | |
| 12112 | if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count |
| 12113 | && col >= 0 && col <= (long)STRLEN(ml_get(lnum)) |
| 12114 | && rettv_list_alloc(rettv) != FAIL) |
| 12115 | { |
| 12116 | (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE); |
| 12117 | for (i = 0; ; ++i) |
| 12118 | { |
| 12119 | id = syn_get_stack_item(i); |
| 12120 | if (id < 0) |
| 12121 | break; |
| 12122 | if (list_append_number(rettv->vval.v_list, id) == FAIL) |
| 12123 | break; |
| 12124 | } |
| 12125 | } |
| 12126 | #endif |
| 12127 | } |
| 12128 | |
| 12129 | static void |
| 12130 | get_cmd_output_as_rettv( |
| 12131 | typval_T *argvars, |
| 12132 | typval_T *rettv, |
| 12133 | int retlist) |
| 12134 | { |
| 12135 | char_u *res = NULL; |
| 12136 | char_u *p; |
| 12137 | char_u *infile = NULL; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12138 | int err = FALSE; |
| 12139 | FILE *fd; |
| 12140 | list_T *list = NULL; |
| 12141 | int flags = SHELL_SILENT; |
| 12142 | |
| 12143 | rettv->v_type = VAR_STRING; |
| 12144 | rettv->vval.v_string = NULL; |
| 12145 | if (check_restricted() || check_secure()) |
| 12146 | goto errret; |
| 12147 | |
| 12148 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 12149 | { |
| 12150 | /* |
Bram Moolenaar | 12c4492 | 2017-01-08 13:26:03 +0100 | [diff] [blame] | 12151 | * Write the text to a temp file, to be used for input of the shell |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12152 | * command. |
| 12153 | */ |
| 12154 | if ((infile = vim_tempname('i', TRUE)) == NULL) |
| 12155 | { |
| 12156 | EMSG(_(e_notmp)); |
| 12157 | goto errret; |
| 12158 | } |
| 12159 | |
| 12160 | fd = mch_fopen((char *)infile, WRITEBIN); |
| 12161 | if (fd == NULL) |
| 12162 | { |
| 12163 | EMSG2(_(e_notopen), infile); |
| 12164 | goto errret; |
| 12165 | } |
Bram Moolenaar | 12c4492 | 2017-01-08 13:26:03 +0100 | [diff] [blame] | 12166 | if (argvars[1].v_type == VAR_NUMBER) |
| 12167 | { |
| 12168 | linenr_T lnum; |
| 12169 | buf_T *buf; |
| 12170 | |
| 12171 | buf = buflist_findnr(argvars[1].vval.v_number); |
| 12172 | if (buf == NULL) |
| 12173 | { |
| 12174 | EMSGN(_(e_nobufnr), argvars[1].vval.v_number); |
Bram Moolenaar | 23c9e8b | 2017-01-20 19:59:54 +0100 | [diff] [blame] | 12175 | fclose(fd); |
Bram Moolenaar | 12c4492 | 2017-01-08 13:26:03 +0100 | [diff] [blame] | 12176 | goto errret; |
| 12177 | } |
| 12178 | |
| 12179 | for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++) |
| 12180 | { |
| 12181 | for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p) |
| 12182 | if (putc(*p == '\n' ? NUL : *p, fd) == EOF) |
| 12183 | { |
| 12184 | err = TRUE; |
| 12185 | break; |
| 12186 | } |
| 12187 | if (putc(NL, fd) == EOF) |
| 12188 | { |
| 12189 | err = TRUE; |
| 12190 | break; |
| 12191 | } |
| 12192 | } |
| 12193 | } |
| 12194 | else if (argvars[1].v_type == VAR_LIST) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12195 | { |
| 12196 | if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL) |
| 12197 | err = TRUE; |
| 12198 | } |
| 12199 | else |
| 12200 | { |
Bram Moolenaar | 12c4492 | 2017-01-08 13:26:03 +0100 | [diff] [blame] | 12201 | size_t len; |
| 12202 | char_u buf[NUMBUFLEN]; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12203 | |
| 12204 | p = get_tv_string_buf_chk(&argvars[1], buf); |
| 12205 | if (p == NULL) |
| 12206 | { |
| 12207 | fclose(fd); |
| 12208 | goto errret; /* type error; errmsg already given */ |
| 12209 | } |
| 12210 | len = STRLEN(p); |
| 12211 | if (len > 0 && fwrite(p, len, 1, fd) != 1) |
| 12212 | err = TRUE; |
| 12213 | } |
| 12214 | if (fclose(fd) != 0) |
| 12215 | err = TRUE; |
| 12216 | if (err) |
| 12217 | { |
| 12218 | EMSG(_("E677: Error writing temp file")); |
| 12219 | goto errret; |
| 12220 | } |
| 12221 | } |
| 12222 | |
| 12223 | /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell |
| 12224 | * echoes typeahead, that messes up the display. */ |
| 12225 | if (!msg_silent) |
| 12226 | flags += SHELL_COOKED; |
| 12227 | |
| 12228 | if (retlist) |
| 12229 | { |
| 12230 | int len; |
| 12231 | listitem_T *li; |
| 12232 | char_u *s = NULL; |
| 12233 | char_u *start; |
| 12234 | char_u *end; |
| 12235 | int i; |
| 12236 | |
| 12237 | res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len); |
| 12238 | if (res == NULL) |
| 12239 | goto errret; |
| 12240 | |
| 12241 | list = list_alloc(); |
| 12242 | if (list == NULL) |
| 12243 | goto errret; |
| 12244 | |
| 12245 | for (i = 0; i < len; ++i) |
| 12246 | { |
| 12247 | start = res + i; |
| 12248 | while (i < len && res[i] != NL) |
| 12249 | ++i; |
| 12250 | end = res + i; |
| 12251 | |
| 12252 | s = alloc((unsigned)(end - start + 1)); |
| 12253 | if (s == NULL) |
| 12254 | goto errret; |
| 12255 | |
| 12256 | for (p = s; start < end; ++p, ++start) |
| 12257 | *p = *start == NUL ? NL : *start; |
| 12258 | *p = NUL; |
| 12259 | |
| 12260 | li = listitem_alloc(); |
| 12261 | if (li == NULL) |
| 12262 | { |
| 12263 | vim_free(s); |
| 12264 | goto errret; |
| 12265 | } |
| 12266 | li->li_tv.v_type = VAR_STRING; |
| 12267 | li->li_tv.v_lock = 0; |
| 12268 | li->li_tv.vval.v_string = s; |
| 12269 | list_append(list, li); |
| 12270 | } |
| 12271 | |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 12272 | rettv_list_set(rettv, list); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12273 | list = NULL; |
| 12274 | } |
| 12275 | else |
| 12276 | { |
| 12277 | res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL); |
| 12278 | #ifdef USE_CR |
| 12279 | /* translate <CR> into <NL> */ |
| 12280 | if (res != NULL) |
| 12281 | { |
| 12282 | char_u *s; |
| 12283 | |
| 12284 | for (s = res; *s; ++s) |
| 12285 | { |
| 12286 | if (*s == CAR) |
| 12287 | *s = NL; |
| 12288 | } |
| 12289 | } |
| 12290 | #else |
| 12291 | # ifdef USE_CRNL |
| 12292 | /* translate <CR><NL> into <NL> */ |
| 12293 | if (res != NULL) |
| 12294 | { |
| 12295 | char_u *s, *d; |
| 12296 | |
| 12297 | d = res; |
| 12298 | for (s = res; *s; ++s) |
| 12299 | { |
| 12300 | if (s[0] == CAR && s[1] == NL) |
| 12301 | ++s; |
| 12302 | *d++ = *s; |
| 12303 | } |
| 12304 | *d = NUL; |
| 12305 | } |
| 12306 | # endif |
| 12307 | #endif |
| 12308 | rettv->vval.v_string = res; |
| 12309 | res = NULL; |
| 12310 | } |
| 12311 | |
| 12312 | errret: |
| 12313 | if (infile != NULL) |
| 12314 | { |
| 12315 | mch_remove(infile); |
| 12316 | vim_free(infile); |
| 12317 | } |
| 12318 | if (res != NULL) |
| 12319 | vim_free(res); |
| 12320 | if (list != NULL) |
| 12321 | list_free(list); |
| 12322 | } |
| 12323 | |
| 12324 | /* |
| 12325 | * "system()" function |
| 12326 | */ |
| 12327 | static void |
| 12328 | f_system(typval_T *argvars, typval_T *rettv) |
| 12329 | { |
| 12330 | get_cmd_output_as_rettv(argvars, rettv, FALSE); |
| 12331 | } |
| 12332 | |
| 12333 | /* |
| 12334 | * "systemlist()" function |
| 12335 | */ |
| 12336 | static void |
| 12337 | f_systemlist(typval_T *argvars, typval_T *rettv) |
| 12338 | { |
| 12339 | get_cmd_output_as_rettv(argvars, rettv, TRUE); |
| 12340 | } |
| 12341 | |
| 12342 | /* |
| 12343 | * "tabpagebuflist()" function |
| 12344 | */ |
| 12345 | static void |
| 12346 | f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 12347 | { |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12348 | tabpage_T *tp; |
| 12349 | win_T *wp = NULL; |
| 12350 | |
| 12351 | if (argvars[0].v_type == VAR_UNKNOWN) |
| 12352 | wp = firstwin; |
| 12353 | else |
| 12354 | { |
| 12355 | tp = find_tabpage((int)get_tv_number(&argvars[0])); |
| 12356 | if (tp != NULL) |
| 12357 | wp = (tp == curtab) ? firstwin : tp->tp_firstwin; |
| 12358 | } |
| 12359 | if (wp != NULL && rettv_list_alloc(rettv) != FAIL) |
| 12360 | { |
| 12361 | for (; wp != NULL; wp = wp->w_next) |
| 12362 | if (list_append_number(rettv->vval.v_list, |
| 12363 | wp->w_buffer->b_fnum) == FAIL) |
| 12364 | break; |
| 12365 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12366 | } |
| 12367 | |
| 12368 | |
| 12369 | /* |
| 12370 | * "tabpagenr()" function |
| 12371 | */ |
| 12372 | static void |
| 12373 | f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv) |
| 12374 | { |
| 12375 | int nr = 1; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12376 | char_u *arg; |
| 12377 | |
| 12378 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 12379 | { |
| 12380 | arg = get_tv_string_chk(&argvars[0]); |
| 12381 | nr = 0; |
| 12382 | if (arg != NULL) |
| 12383 | { |
| 12384 | if (STRCMP(arg, "$") == 0) |
| 12385 | nr = tabpage_index(NULL) - 1; |
| 12386 | else |
| 12387 | EMSG2(_(e_invexpr2), arg); |
| 12388 | } |
| 12389 | } |
| 12390 | else |
| 12391 | nr = tabpage_index(curtab); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12392 | rettv->vval.v_number = nr; |
| 12393 | } |
| 12394 | |
| 12395 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12396 | static int get_winnr(tabpage_T *tp, typval_T *argvar); |
| 12397 | |
| 12398 | /* |
| 12399 | * Common code for tabpagewinnr() and winnr(). |
| 12400 | */ |
| 12401 | static int |
| 12402 | get_winnr(tabpage_T *tp, typval_T *argvar) |
| 12403 | { |
| 12404 | win_T *twin; |
| 12405 | int nr = 1; |
| 12406 | win_T *wp; |
| 12407 | char_u *arg; |
| 12408 | |
| 12409 | twin = (tp == curtab) ? curwin : tp->tp_curwin; |
| 12410 | if (argvar->v_type != VAR_UNKNOWN) |
| 12411 | { |
| 12412 | arg = get_tv_string_chk(argvar); |
| 12413 | if (arg == NULL) |
| 12414 | nr = 0; /* type error; errmsg already given */ |
| 12415 | else if (STRCMP(arg, "$") == 0) |
| 12416 | twin = (tp == curtab) ? lastwin : tp->tp_lastwin; |
| 12417 | else if (STRCMP(arg, "#") == 0) |
| 12418 | { |
| 12419 | twin = (tp == curtab) ? prevwin : tp->tp_prevwin; |
| 12420 | if (twin == NULL) |
| 12421 | nr = 0; |
| 12422 | } |
| 12423 | else |
| 12424 | { |
| 12425 | EMSG2(_(e_invexpr2), arg); |
| 12426 | nr = 0; |
| 12427 | } |
| 12428 | } |
| 12429 | |
| 12430 | if (nr > 0) |
| 12431 | for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin; |
| 12432 | wp != twin; wp = wp->w_next) |
| 12433 | { |
| 12434 | if (wp == NULL) |
| 12435 | { |
| 12436 | /* didn't find it in this tabpage */ |
| 12437 | nr = 0; |
| 12438 | break; |
| 12439 | } |
| 12440 | ++nr; |
| 12441 | } |
| 12442 | return nr; |
| 12443 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12444 | |
| 12445 | /* |
| 12446 | * "tabpagewinnr()" function |
| 12447 | */ |
| 12448 | static void |
| 12449 | f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv) |
| 12450 | { |
| 12451 | int nr = 1; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12452 | tabpage_T *tp; |
| 12453 | |
| 12454 | tp = find_tabpage((int)get_tv_number(&argvars[0])); |
| 12455 | if (tp == NULL) |
| 12456 | nr = 0; |
| 12457 | else |
| 12458 | nr = get_winnr(tp, &argvars[1]); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12459 | rettv->vval.v_number = nr; |
| 12460 | } |
| 12461 | |
| 12462 | |
| 12463 | /* |
| 12464 | * "tagfiles()" function |
| 12465 | */ |
| 12466 | static void |
| 12467 | f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv) |
| 12468 | { |
| 12469 | char_u *fname; |
| 12470 | tagname_T tn; |
| 12471 | int first; |
| 12472 | |
| 12473 | if (rettv_list_alloc(rettv) == FAIL) |
| 12474 | return; |
| 12475 | fname = alloc(MAXPATHL); |
| 12476 | if (fname == NULL) |
| 12477 | return; |
| 12478 | |
| 12479 | for (first = TRUE; ; first = FALSE) |
| 12480 | if (get_tagfname(&tn, first, fname) == FAIL |
| 12481 | || list_append_string(rettv->vval.v_list, fname, -1) == FAIL) |
| 12482 | break; |
| 12483 | tagname_free(&tn); |
| 12484 | vim_free(fname); |
| 12485 | } |
| 12486 | |
| 12487 | /* |
| 12488 | * "taglist()" function |
| 12489 | */ |
| 12490 | static void |
| 12491 | f_taglist(typval_T *argvars, typval_T *rettv) |
| 12492 | { |
Bram Moolenaar | c6aafba | 2017-03-21 17:09:10 +0100 | [diff] [blame] | 12493 | char_u *fname = NULL; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12494 | char_u *tag_pattern; |
| 12495 | |
| 12496 | tag_pattern = get_tv_string(&argvars[0]); |
| 12497 | |
| 12498 | rettv->vval.v_number = FALSE; |
| 12499 | if (*tag_pattern == NUL) |
| 12500 | return; |
| 12501 | |
Bram Moolenaar | c6aafba | 2017-03-21 17:09:10 +0100 | [diff] [blame] | 12502 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 12503 | fname = get_tv_string(&argvars[1]); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12504 | if (rettv_list_alloc(rettv) == OK) |
Bram Moolenaar | c6aafba | 2017-03-21 17:09:10 +0100 | [diff] [blame] | 12505 | (void)get_tags(rettv->vval.v_list, tag_pattern, fname); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12506 | } |
| 12507 | |
| 12508 | /* |
| 12509 | * "tempname()" function |
| 12510 | */ |
| 12511 | static void |
| 12512 | f_tempname(typval_T *argvars UNUSED, typval_T *rettv) |
| 12513 | { |
| 12514 | static int x = 'A'; |
| 12515 | |
| 12516 | rettv->v_type = VAR_STRING; |
| 12517 | rettv->vval.v_string = vim_tempname(x, FALSE); |
| 12518 | |
| 12519 | /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different |
| 12520 | * names. Skip 'I' and 'O', they are used for shell redirection. */ |
| 12521 | do |
| 12522 | { |
| 12523 | if (x == 'Z') |
| 12524 | x = '0'; |
| 12525 | else if (x == '9') |
| 12526 | x = 'A'; |
| 12527 | else |
| 12528 | { |
| 12529 | #ifdef EBCDIC |
| 12530 | if (x == 'I') |
| 12531 | x = 'J'; |
| 12532 | else if (x == 'R') |
| 12533 | x = 'S'; |
| 12534 | else |
| 12535 | #endif |
| 12536 | ++x; |
| 12537 | } |
| 12538 | } while (x == 'I' || x == 'O'); |
| 12539 | } |
| 12540 | |
| 12541 | #ifdef FEAT_FLOAT |
| 12542 | /* |
| 12543 | * "tan()" function |
| 12544 | */ |
| 12545 | static void |
| 12546 | f_tan(typval_T *argvars, typval_T *rettv) |
| 12547 | { |
| 12548 | float_T f = 0.0; |
| 12549 | |
| 12550 | rettv->v_type = VAR_FLOAT; |
| 12551 | if (get_float_arg(argvars, &f) == OK) |
| 12552 | rettv->vval.v_float = tan(f); |
| 12553 | else |
| 12554 | rettv->vval.v_float = 0.0; |
| 12555 | } |
| 12556 | |
| 12557 | /* |
| 12558 | * "tanh()" function |
| 12559 | */ |
| 12560 | static void |
| 12561 | f_tanh(typval_T *argvars, typval_T *rettv) |
| 12562 | { |
| 12563 | float_T f = 0.0; |
| 12564 | |
| 12565 | rettv->v_type = VAR_FLOAT; |
| 12566 | if (get_float_arg(argvars, &f) == OK) |
| 12567 | rettv->vval.v_float = tanh(f); |
| 12568 | else |
| 12569 | rettv->vval.v_float = 0.0; |
| 12570 | } |
| 12571 | #endif |
| 12572 | |
| 12573 | /* |
| 12574 | * "test_alloc_fail(id, countdown, repeat)" function |
| 12575 | */ |
| 12576 | static void |
| 12577 | f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED) |
| 12578 | { |
| 12579 | if (argvars[0].v_type != VAR_NUMBER |
| 12580 | || argvars[0].vval.v_number <= 0 |
| 12581 | || argvars[1].v_type != VAR_NUMBER |
| 12582 | || argvars[1].vval.v_number < 0 |
| 12583 | || argvars[2].v_type != VAR_NUMBER) |
| 12584 | EMSG(_(e_invarg)); |
| 12585 | else |
| 12586 | { |
| 12587 | alloc_fail_id = argvars[0].vval.v_number; |
| 12588 | if (alloc_fail_id >= aid_last) |
| 12589 | EMSG(_(e_invarg)); |
| 12590 | alloc_fail_countdown = argvars[1].vval.v_number; |
| 12591 | alloc_fail_repeat = argvars[2].vval.v_number; |
| 12592 | did_outofmem_msg = FALSE; |
| 12593 | } |
| 12594 | } |
| 12595 | |
| 12596 | /* |
| 12597 | * "test_autochdir()" |
| 12598 | */ |
| 12599 | static void |
| 12600 | f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 12601 | { |
| 12602 | #if defined(FEAT_AUTOCHDIR) |
| 12603 | test_autochdir = TRUE; |
| 12604 | #endif |
| 12605 | } |
| 12606 | |
| 12607 | /* |
Bram Moolenaar | 5e80de3 | 2017-09-03 15:48:12 +0200 | [diff] [blame] | 12608 | * "test_feedinput()" |
| 12609 | */ |
| 12610 | static void |
| 12611 | f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED) |
| 12612 | { |
| 12613 | #ifdef USE_INPUT_BUF |
| 12614 | char_u *val = get_tv_string_chk(&argvars[0]); |
| 12615 | |
| 12616 | if (val != NULL) |
| 12617 | { |
| 12618 | trash_input_buf(); |
| 12619 | add_to_input_buf_csi(val, (int)STRLEN(val)); |
| 12620 | } |
| 12621 | #endif |
| 12622 | } |
| 12623 | |
| 12624 | /* |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 12625 | * "test_disable({name}, {val})" function |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12626 | */ |
| 12627 | static void |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 12628 | f_test_override(typval_T *argvars, typval_T *rettv UNUSED) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12629 | { |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 12630 | char_u *name = (char_u *)""; |
| 12631 | int val; |
Bram Moolenaar | 182a17b | 2017-06-25 20:57:18 +0200 | [diff] [blame] | 12632 | static int save_starting = -1; |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 12633 | |
| 12634 | if (argvars[0].v_type != VAR_STRING |
| 12635 | || (argvars[1].v_type) != VAR_NUMBER) |
| 12636 | EMSG(_(e_invarg)); |
| 12637 | else |
| 12638 | { |
| 12639 | name = get_tv_string_chk(&argvars[0]); |
| 12640 | val = (int)get_tv_number(&argvars[1]); |
| 12641 | |
| 12642 | if (STRCMP(name, (char_u *)"redraw") == 0) |
| 12643 | disable_redraw_for_testing = val; |
| 12644 | else if (STRCMP(name, (char_u *)"char_avail") == 0) |
| 12645 | disable_char_avail_for_testing = val; |
Bram Moolenaar | 182a17b | 2017-06-25 20:57:18 +0200 | [diff] [blame] | 12646 | else if (STRCMP(name, (char_u *)"starting") == 0) |
| 12647 | { |
| 12648 | if (val) |
| 12649 | { |
| 12650 | if (save_starting < 0) |
| 12651 | save_starting = starting; |
| 12652 | starting = 0; |
| 12653 | } |
| 12654 | else |
| 12655 | { |
| 12656 | starting = save_starting; |
| 12657 | save_starting = -1; |
| 12658 | } |
| 12659 | } |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 12660 | else if (STRCMP(name, (char_u *)"ALL") == 0) |
| 12661 | { |
| 12662 | disable_char_avail_for_testing = FALSE; |
| 12663 | disable_redraw_for_testing = FALSE; |
Bram Moolenaar | 182a17b | 2017-06-25 20:57:18 +0200 | [diff] [blame] | 12664 | if (save_starting >= 0) |
| 12665 | { |
| 12666 | starting = save_starting; |
| 12667 | save_starting = -1; |
| 12668 | } |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 12669 | } |
| 12670 | else |
| 12671 | EMSG2(_(e_invarg2), name); |
| 12672 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12673 | } |
| 12674 | |
| 12675 | /* |
| 12676 | * "test_garbagecollect_now()" function |
| 12677 | */ |
| 12678 | static void |
| 12679 | f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 12680 | { |
| 12681 | /* This is dangerous, any Lists and Dicts used internally may be freed |
| 12682 | * while still in use. */ |
| 12683 | garbage_collect(TRUE); |
| 12684 | } |
| 12685 | |
Bram Moolenaar | e0c31f6 | 2017-03-01 15:07:05 +0100 | [diff] [blame] | 12686 | /* |
| 12687 | * "test_ignore_error()" function |
| 12688 | */ |
| 12689 | static void |
| 12690 | f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED) |
| 12691 | { |
| 12692 | ignore_error_for_testing(get_tv_string(&argvars[0])); |
| 12693 | } |
| 12694 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12695 | #ifdef FEAT_JOB_CHANNEL |
| 12696 | static void |
| 12697 | f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv) |
| 12698 | { |
| 12699 | rettv->v_type = VAR_CHANNEL; |
| 12700 | rettv->vval.v_channel = NULL; |
| 12701 | } |
| 12702 | #endif |
| 12703 | |
| 12704 | static void |
| 12705 | f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv) |
| 12706 | { |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 12707 | rettv_dict_set(rettv, NULL); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12708 | } |
| 12709 | |
| 12710 | #ifdef FEAT_JOB_CHANNEL |
| 12711 | static void |
| 12712 | f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv) |
| 12713 | { |
| 12714 | rettv->v_type = VAR_JOB; |
| 12715 | rettv->vval.v_job = NULL; |
| 12716 | } |
| 12717 | #endif |
| 12718 | |
| 12719 | static void |
| 12720 | f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv) |
| 12721 | { |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 12722 | rettv_list_set(rettv, NULL); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12723 | } |
| 12724 | |
| 12725 | static void |
| 12726 | f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv) |
| 12727 | { |
| 12728 | rettv->v_type = VAR_PARTIAL; |
| 12729 | rettv->vval.v_partial = NULL; |
| 12730 | } |
| 12731 | |
| 12732 | static void |
| 12733 | f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv) |
| 12734 | { |
| 12735 | rettv->v_type = VAR_STRING; |
| 12736 | rettv->vval.v_string = NULL; |
| 12737 | } |
| 12738 | |
| 12739 | static void |
| 12740 | f_test_settime(typval_T *argvars, typval_T *rettv UNUSED) |
| 12741 | { |
| 12742 | time_for_testing = (time_t)get_tv_number(&argvars[0]); |
| 12743 | } |
| 12744 | |
| 12745 | #if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO) |
| 12746 | /* |
| 12747 | * Get a callback from "arg". It can be a Funcref or a function name. |
| 12748 | * When "arg" is zero return an empty string. |
| 12749 | * Return NULL for an invalid argument. |
| 12750 | */ |
| 12751 | char_u * |
| 12752 | get_callback(typval_T *arg, partial_T **pp) |
| 12753 | { |
| 12754 | if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL) |
| 12755 | { |
| 12756 | *pp = arg->vval.v_partial; |
| 12757 | ++(*pp)->pt_refcount; |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 12758 | return partial_name(*pp); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12759 | } |
| 12760 | *pp = NULL; |
Bram Moolenaar | 437bafe | 2016-08-01 15:40:54 +0200 | [diff] [blame] | 12761 | if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12762 | { |
| 12763 | func_ref(arg->vval.v_string); |
| 12764 | return arg->vval.v_string; |
| 12765 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12766 | if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0) |
| 12767 | return (char_u *)""; |
| 12768 | EMSG(_("E921: Invalid callback argument")); |
| 12769 | return NULL; |
| 12770 | } |
| 12771 | |
| 12772 | /* |
| 12773 | * Unref/free "callback" and "partial" retured by get_callback(). |
| 12774 | */ |
| 12775 | void |
| 12776 | free_callback(char_u *callback, partial_T *partial) |
| 12777 | { |
| 12778 | if (partial != NULL) |
| 12779 | partial_unref(partial); |
| 12780 | else if (callback != NULL) |
| 12781 | { |
| 12782 | func_unref(callback); |
| 12783 | vim_free(callback); |
| 12784 | } |
| 12785 | } |
| 12786 | #endif |
| 12787 | |
| 12788 | #ifdef FEAT_TIMERS |
| 12789 | /* |
Bram Moolenaar | 8e97bd7 | 2016-08-06 22:05:07 +0200 | [diff] [blame] | 12790 | * "timer_info([timer])" function |
| 12791 | */ |
| 12792 | static void |
| 12793 | f_timer_info(typval_T *argvars, typval_T *rettv) |
| 12794 | { |
| 12795 | timer_T *timer = NULL; |
| 12796 | |
| 12797 | if (rettv_list_alloc(rettv) != OK) |
| 12798 | return; |
| 12799 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 12800 | { |
| 12801 | if (argvars[0].v_type != VAR_NUMBER) |
| 12802 | EMSG(_(e_number_exp)); |
| 12803 | else |
| 12804 | { |
| 12805 | timer = find_timer((int)get_tv_number(&argvars[0])); |
| 12806 | if (timer != NULL) |
| 12807 | add_timer_info(rettv, timer); |
| 12808 | } |
| 12809 | } |
| 12810 | else |
| 12811 | add_timer_info_all(rettv); |
| 12812 | } |
| 12813 | |
| 12814 | /* |
Bram Moolenaar | b73598e | 2016-08-07 18:22:53 +0200 | [diff] [blame] | 12815 | * "timer_pause(timer, paused)" function |
| 12816 | */ |
| 12817 | static void |
| 12818 | f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED) |
| 12819 | { |
| 12820 | timer_T *timer = NULL; |
| 12821 | int paused = (int)get_tv_number(&argvars[1]); |
| 12822 | |
| 12823 | if (argvars[0].v_type != VAR_NUMBER) |
| 12824 | EMSG(_(e_number_exp)); |
| 12825 | else |
| 12826 | { |
| 12827 | timer = find_timer((int)get_tv_number(&argvars[0])); |
| 12828 | if (timer != NULL) |
| 12829 | timer->tr_paused = paused; |
| 12830 | } |
| 12831 | } |
| 12832 | |
| 12833 | /* |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12834 | * "timer_start(time, callback [, options])" function |
| 12835 | */ |
| 12836 | static void |
| 12837 | f_timer_start(typval_T *argvars, typval_T *rettv) |
| 12838 | { |
Bram Moolenaar | 75537a9 | 2016-09-05 22:45:28 +0200 | [diff] [blame] | 12839 | long msec = (long)get_tv_number(&argvars[0]); |
| 12840 | timer_T *timer; |
| 12841 | int repeat = 0; |
| 12842 | char_u *callback; |
| 12843 | dict_T *dict; |
| 12844 | partial_T *partial; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12845 | |
Bram Moolenaar | 75537a9 | 2016-09-05 22:45:28 +0200 | [diff] [blame] | 12846 | rettv->vval.v_number = -1; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12847 | if (check_secure()) |
| 12848 | return; |
| 12849 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 12850 | { |
| 12851 | if (argvars[2].v_type != VAR_DICT |
| 12852 | || (dict = argvars[2].vval.v_dict) == NULL) |
| 12853 | { |
| 12854 | EMSG2(_(e_invarg2), get_tv_string(&argvars[2])); |
| 12855 | return; |
| 12856 | } |
| 12857 | if (dict_find(dict, (char_u *)"repeat", -1) != NULL) |
| 12858 | repeat = get_dict_number(dict, (char_u *)"repeat"); |
| 12859 | } |
| 12860 | |
Bram Moolenaar | 75537a9 | 2016-09-05 22:45:28 +0200 | [diff] [blame] | 12861 | callback = get_callback(&argvars[1], &partial); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12862 | if (callback == NULL) |
Bram Moolenaar | 75537a9 | 2016-09-05 22:45:28 +0200 | [diff] [blame] | 12863 | return; |
| 12864 | |
| 12865 | timer = create_timer(msec, repeat); |
| 12866 | if (timer == NULL) |
| 12867 | free_callback(callback, partial); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12868 | else |
| 12869 | { |
Bram Moolenaar | 26fe0d5 | 2016-09-10 14:27:30 +0200 | [diff] [blame] | 12870 | if (partial == NULL) |
Bram Moolenaar | 3ab1435 | 2016-07-30 22:32:11 +0200 | [diff] [blame] | 12871 | timer->tr_callback = vim_strsave(callback); |
| 12872 | else |
| 12873 | /* pointer into the partial */ |
| 12874 | timer->tr_callback = callback; |
Bram Moolenaar | 75537a9 | 2016-09-05 22:45:28 +0200 | [diff] [blame] | 12875 | timer->tr_partial = partial; |
| 12876 | rettv->vval.v_number = (varnumber_T)timer->tr_id; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12877 | } |
| 12878 | } |
| 12879 | |
| 12880 | /* |
| 12881 | * "timer_stop(timer)" function |
| 12882 | */ |
| 12883 | static void |
| 12884 | f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED) |
| 12885 | { |
| 12886 | timer_T *timer; |
| 12887 | |
| 12888 | if (argvars[0].v_type != VAR_NUMBER) |
| 12889 | { |
| 12890 | EMSG(_(e_number_exp)); |
| 12891 | return; |
| 12892 | } |
| 12893 | timer = find_timer((int)get_tv_number(&argvars[0])); |
| 12894 | if (timer != NULL) |
| 12895 | stop_timer(timer); |
| 12896 | } |
Bram Moolenaar | b73598e | 2016-08-07 18:22:53 +0200 | [diff] [blame] | 12897 | |
| 12898 | /* |
| 12899 | * "timer_stopall()" function |
| 12900 | */ |
| 12901 | static void |
| 12902 | f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 12903 | { |
| 12904 | stop_all_timers(); |
| 12905 | } |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12906 | #endif |
| 12907 | |
| 12908 | /* |
| 12909 | * "tolower(string)" function |
| 12910 | */ |
| 12911 | static void |
| 12912 | f_tolower(typval_T *argvars, typval_T *rettv) |
| 12913 | { |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12914 | rettv->v_type = VAR_STRING; |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 12915 | rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0])); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 12916 | } |
| 12917 | |
| 12918 | /* |
| 12919 | * "toupper(string)" function |
| 12920 | */ |
| 12921 | static void |
| 12922 | f_toupper(typval_T *argvars, typval_T *rettv) |
| 12923 | { |
| 12924 | rettv->v_type = VAR_STRING; |
| 12925 | rettv->vval.v_string = strup_save(get_tv_string(&argvars[0])); |
| 12926 | } |
| 12927 | |
| 12928 | /* |
| 12929 | * "tr(string, fromstr, tostr)" function |
| 12930 | */ |
| 12931 | static void |
| 12932 | f_tr(typval_T *argvars, typval_T *rettv) |
| 12933 | { |
| 12934 | char_u *in_str; |
| 12935 | char_u *fromstr; |
| 12936 | char_u *tostr; |
| 12937 | char_u *p; |
| 12938 | #ifdef FEAT_MBYTE |
| 12939 | int inlen; |
| 12940 | int fromlen; |
| 12941 | int tolen; |
| 12942 | int idx; |
| 12943 | char_u *cpstr; |
| 12944 | int cplen; |
| 12945 | int first = TRUE; |
| 12946 | #endif |
| 12947 | char_u buf[NUMBUFLEN]; |
| 12948 | char_u buf2[NUMBUFLEN]; |
| 12949 | garray_T ga; |
| 12950 | |
| 12951 | in_str = get_tv_string(&argvars[0]); |
| 12952 | fromstr = get_tv_string_buf_chk(&argvars[1], buf); |
| 12953 | tostr = get_tv_string_buf_chk(&argvars[2], buf2); |
| 12954 | |
| 12955 | /* Default return value: empty string. */ |
| 12956 | rettv->v_type = VAR_STRING; |
| 12957 | rettv->vval.v_string = NULL; |
| 12958 | if (fromstr == NULL || tostr == NULL) |
| 12959 | return; /* type error; errmsg already given */ |
| 12960 | ga_init2(&ga, (int)sizeof(char), 80); |
| 12961 | |
| 12962 | #ifdef FEAT_MBYTE |
| 12963 | if (!has_mbyte) |
| 12964 | #endif |
| 12965 | /* not multi-byte: fromstr and tostr must be the same length */ |
| 12966 | if (STRLEN(fromstr) != STRLEN(tostr)) |
| 12967 | { |
| 12968 | #ifdef FEAT_MBYTE |
| 12969 | error: |
| 12970 | #endif |
| 12971 | EMSG2(_(e_invarg2), fromstr); |
| 12972 | ga_clear(&ga); |
| 12973 | return; |
| 12974 | } |
| 12975 | |
| 12976 | /* fromstr and tostr have to contain the same number of chars */ |
| 12977 | while (*in_str != NUL) |
| 12978 | { |
| 12979 | #ifdef FEAT_MBYTE |
| 12980 | if (has_mbyte) |
| 12981 | { |
| 12982 | inlen = (*mb_ptr2len)(in_str); |
| 12983 | cpstr = in_str; |
| 12984 | cplen = inlen; |
| 12985 | idx = 0; |
| 12986 | for (p = fromstr; *p != NUL; p += fromlen) |
| 12987 | { |
| 12988 | fromlen = (*mb_ptr2len)(p); |
| 12989 | if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0) |
| 12990 | { |
| 12991 | for (p = tostr; *p != NUL; p += tolen) |
| 12992 | { |
| 12993 | tolen = (*mb_ptr2len)(p); |
| 12994 | if (idx-- == 0) |
| 12995 | { |
| 12996 | cplen = tolen; |
| 12997 | cpstr = p; |
| 12998 | break; |
| 12999 | } |
| 13000 | } |
| 13001 | if (*p == NUL) /* tostr is shorter than fromstr */ |
| 13002 | goto error; |
| 13003 | break; |
| 13004 | } |
| 13005 | ++idx; |
| 13006 | } |
| 13007 | |
| 13008 | if (first && cpstr == in_str) |
| 13009 | { |
| 13010 | /* Check that fromstr and tostr have the same number of |
| 13011 | * (multi-byte) characters. Done only once when a character |
| 13012 | * of in_str doesn't appear in fromstr. */ |
| 13013 | first = FALSE; |
| 13014 | for (p = tostr; *p != NUL; p += tolen) |
| 13015 | { |
| 13016 | tolen = (*mb_ptr2len)(p); |
| 13017 | --idx; |
| 13018 | } |
| 13019 | if (idx != 0) |
| 13020 | goto error; |
| 13021 | } |
| 13022 | |
| 13023 | (void)ga_grow(&ga, cplen); |
| 13024 | mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen); |
| 13025 | ga.ga_len += cplen; |
| 13026 | |
| 13027 | in_str += inlen; |
| 13028 | } |
| 13029 | else |
| 13030 | #endif |
| 13031 | { |
| 13032 | /* When not using multi-byte chars we can do it faster. */ |
| 13033 | p = vim_strchr(fromstr, *in_str); |
| 13034 | if (p != NULL) |
| 13035 | ga_append(&ga, tostr[p - fromstr]); |
| 13036 | else |
| 13037 | ga_append(&ga, *in_str); |
| 13038 | ++in_str; |
| 13039 | } |
| 13040 | } |
| 13041 | |
| 13042 | /* add a terminating NUL */ |
| 13043 | (void)ga_grow(&ga, 1); |
| 13044 | ga_append(&ga, NUL); |
| 13045 | |
| 13046 | rettv->vval.v_string = ga.ga_data; |
| 13047 | } |
| 13048 | |
| 13049 | #ifdef FEAT_FLOAT |
| 13050 | /* |
| 13051 | * "trunc({float})" function |
| 13052 | */ |
| 13053 | static void |
| 13054 | f_trunc(typval_T *argvars, typval_T *rettv) |
| 13055 | { |
| 13056 | float_T f = 0.0; |
| 13057 | |
| 13058 | rettv->v_type = VAR_FLOAT; |
| 13059 | if (get_float_arg(argvars, &f) == OK) |
| 13060 | /* trunc() is not in C90, use floor() or ceil() instead. */ |
| 13061 | rettv->vval.v_float = f > 0 ? floor(f) : ceil(f); |
| 13062 | else |
| 13063 | rettv->vval.v_float = 0.0; |
| 13064 | } |
| 13065 | #endif |
| 13066 | |
| 13067 | /* |
| 13068 | * "type(expr)" function |
| 13069 | */ |
| 13070 | static void |
| 13071 | f_type(typval_T *argvars, typval_T *rettv) |
| 13072 | { |
| 13073 | int n = -1; |
| 13074 | |
| 13075 | switch (argvars[0].v_type) |
| 13076 | { |
Bram Moolenaar | f562e72 | 2016-07-19 17:25:25 +0200 | [diff] [blame] | 13077 | case VAR_NUMBER: n = VAR_TYPE_NUMBER; break; |
| 13078 | case VAR_STRING: n = VAR_TYPE_STRING; break; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13079 | case VAR_PARTIAL: |
Bram Moolenaar | f562e72 | 2016-07-19 17:25:25 +0200 | [diff] [blame] | 13080 | case VAR_FUNC: n = VAR_TYPE_FUNC; break; |
| 13081 | case VAR_LIST: n = VAR_TYPE_LIST; break; |
| 13082 | case VAR_DICT: n = VAR_TYPE_DICT; break; |
| 13083 | case VAR_FLOAT: n = VAR_TYPE_FLOAT; break; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13084 | case VAR_SPECIAL: |
| 13085 | if (argvars[0].vval.v_number == VVAL_FALSE |
| 13086 | || argvars[0].vval.v_number == VVAL_TRUE) |
Bram Moolenaar | f562e72 | 2016-07-19 17:25:25 +0200 | [diff] [blame] | 13087 | n = VAR_TYPE_BOOL; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13088 | else |
Bram Moolenaar | f562e72 | 2016-07-19 17:25:25 +0200 | [diff] [blame] | 13089 | n = VAR_TYPE_NONE; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13090 | break; |
Bram Moolenaar | f562e72 | 2016-07-19 17:25:25 +0200 | [diff] [blame] | 13091 | case VAR_JOB: n = VAR_TYPE_JOB; break; |
| 13092 | case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13093 | case VAR_UNKNOWN: |
Bram Moolenaar | 95f0960 | 2016-11-10 20:01:45 +0100 | [diff] [blame] | 13094 | internal_error("f_type(UNKNOWN)"); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13095 | n = -1; |
| 13096 | break; |
| 13097 | } |
| 13098 | rettv->vval.v_number = n; |
| 13099 | } |
| 13100 | |
| 13101 | /* |
| 13102 | * "undofile(name)" function |
| 13103 | */ |
| 13104 | static void |
| 13105 | f_undofile(typval_T *argvars UNUSED, typval_T *rettv) |
| 13106 | { |
| 13107 | rettv->v_type = VAR_STRING; |
| 13108 | #ifdef FEAT_PERSISTENT_UNDO |
| 13109 | { |
| 13110 | char_u *fname = get_tv_string(&argvars[0]); |
| 13111 | |
| 13112 | if (*fname == NUL) |
| 13113 | { |
| 13114 | /* If there is no file name there will be no undo file. */ |
| 13115 | rettv->vval.v_string = NULL; |
| 13116 | } |
| 13117 | else |
| 13118 | { |
| 13119 | char_u *ffname = FullName_save(fname, FALSE); |
| 13120 | |
| 13121 | if (ffname != NULL) |
| 13122 | rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE); |
| 13123 | vim_free(ffname); |
| 13124 | } |
| 13125 | } |
| 13126 | #else |
| 13127 | rettv->vval.v_string = NULL; |
| 13128 | #endif |
| 13129 | } |
| 13130 | |
| 13131 | /* |
| 13132 | * "undotree()" function |
| 13133 | */ |
| 13134 | static void |
| 13135 | f_undotree(typval_T *argvars UNUSED, typval_T *rettv) |
| 13136 | { |
| 13137 | if (rettv_dict_alloc(rettv) == OK) |
| 13138 | { |
| 13139 | dict_T *dict = rettv->vval.v_dict; |
| 13140 | list_T *list; |
| 13141 | |
| 13142 | dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL); |
| 13143 | dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL); |
| 13144 | dict_add_nr_str(dict, "save_last", |
| 13145 | (long)curbuf->b_u_save_nr_last, NULL); |
| 13146 | dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL); |
| 13147 | dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL); |
| 13148 | dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL); |
| 13149 | |
| 13150 | list = list_alloc(); |
| 13151 | if (list != NULL) |
| 13152 | { |
| 13153 | u_eval_tree(curbuf->b_u_oldhead, list); |
| 13154 | dict_add_list(dict, "entries", list); |
| 13155 | } |
| 13156 | } |
| 13157 | } |
| 13158 | |
| 13159 | /* |
| 13160 | * "values(dict)" function |
| 13161 | */ |
| 13162 | static void |
| 13163 | f_values(typval_T *argvars, typval_T *rettv) |
| 13164 | { |
| 13165 | dict_list(argvars, rettv, 1); |
| 13166 | } |
| 13167 | |
| 13168 | /* |
| 13169 | * "virtcol(string)" function |
| 13170 | */ |
| 13171 | static void |
| 13172 | f_virtcol(typval_T *argvars, typval_T *rettv) |
| 13173 | { |
| 13174 | colnr_T vcol = 0; |
| 13175 | pos_T *fp; |
| 13176 | int fnum = curbuf->b_fnum; |
| 13177 | |
| 13178 | fp = var2fpos(&argvars[0], FALSE, &fnum); |
| 13179 | if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count |
| 13180 | && fnum == curbuf->b_fnum) |
| 13181 | { |
| 13182 | getvvcol(curwin, fp, NULL, NULL, &vcol); |
| 13183 | ++vcol; |
| 13184 | } |
| 13185 | |
| 13186 | rettv->vval.v_number = vcol; |
| 13187 | } |
| 13188 | |
| 13189 | /* |
| 13190 | * "visualmode()" function |
| 13191 | */ |
| 13192 | static void |
| 13193 | f_visualmode(typval_T *argvars, typval_T *rettv) |
| 13194 | { |
| 13195 | char_u str[2]; |
| 13196 | |
| 13197 | rettv->v_type = VAR_STRING; |
| 13198 | str[0] = curbuf->b_visual_mode_eval; |
| 13199 | str[1] = NUL; |
| 13200 | rettv->vval.v_string = vim_strsave(str); |
| 13201 | |
| 13202 | /* A non-zero number or non-empty string argument: reset mode. */ |
| 13203 | if (non_zero_arg(&argvars[0])) |
| 13204 | curbuf->b_visual_mode_eval = NUL; |
| 13205 | } |
| 13206 | |
| 13207 | /* |
| 13208 | * "wildmenumode()" function |
| 13209 | */ |
| 13210 | static void |
| 13211 | f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 13212 | { |
| 13213 | #ifdef FEAT_WILDMENU |
| 13214 | if (wild_menu_showing) |
| 13215 | rettv->vval.v_number = 1; |
| 13216 | #endif |
| 13217 | } |
| 13218 | |
| 13219 | /* |
| 13220 | * "winbufnr(nr)" function |
| 13221 | */ |
| 13222 | static void |
| 13223 | f_winbufnr(typval_T *argvars, typval_T *rettv) |
| 13224 | { |
| 13225 | win_T *wp; |
| 13226 | |
| 13227 | wp = find_win_by_nr(&argvars[0], NULL); |
| 13228 | if (wp == NULL) |
| 13229 | rettv->vval.v_number = -1; |
| 13230 | else |
| 13231 | rettv->vval.v_number = wp->w_buffer->b_fnum; |
| 13232 | } |
| 13233 | |
| 13234 | /* |
| 13235 | * "wincol()" function |
| 13236 | */ |
| 13237 | static void |
| 13238 | f_wincol(typval_T *argvars UNUSED, typval_T *rettv) |
| 13239 | { |
| 13240 | validate_cursor(); |
| 13241 | rettv->vval.v_number = curwin->w_wcol + 1; |
| 13242 | } |
| 13243 | |
| 13244 | /* |
| 13245 | * "winheight(nr)" function |
| 13246 | */ |
| 13247 | static void |
| 13248 | f_winheight(typval_T *argvars, typval_T *rettv) |
| 13249 | { |
| 13250 | win_T *wp; |
| 13251 | |
| 13252 | wp = find_win_by_nr(&argvars[0], NULL); |
| 13253 | if (wp == NULL) |
| 13254 | rettv->vval.v_number = -1; |
| 13255 | else |
| 13256 | rettv->vval.v_number = wp->w_height; |
| 13257 | } |
| 13258 | |
| 13259 | /* |
| 13260 | * "winline()" function |
| 13261 | */ |
| 13262 | static void |
| 13263 | f_winline(typval_T *argvars UNUSED, typval_T *rettv) |
| 13264 | { |
| 13265 | validate_cursor(); |
| 13266 | rettv->vval.v_number = curwin->w_wrow + 1; |
| 13267 | } |
| 13268 | |
| 13269 | /* |
| 13270 | * "winnr()" function |
| 13271 | */ |
| 13272 | static void |
| 13273 | f_winnr(typval_T *argvars UNUSED, typval_T *rettv) |
| 13274 | { |
| 13275 | int nr = 1; |
| 13276 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13277 | nr = get_winnr(curtab, &argvars[0]); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13278 | rettv->vval.v_number = nr; |
| 13279 | } |
| 13280 | |
| 13281 | /* |
| 13282 | * "winrestcmd()" function |
| 13283 | */ |
| 13284 | static void |
| 13285 | f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv) |
| 13286 | { |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13287 | win_T *wp; |
| 13288 | int winnr = 1; |
| 13289 | garray_T ga; |
| 13290 | char_u buf[50]; |
| 13291 | |
| 13292 | ga_init2(&ga, (int)sizeof(char), 70); |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 13293 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13294 | { |
| 13295 | sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height); |
| 13296 | ga_concat(&ga, buf); |
| 13297 | sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width); |
| 13298 | ga_concat(&ga, buf); |
| 13299 | ++winnr; |
| 13300 | } |
| 13301 | ga_append(&ga, NUL); |
| 13302 | |
| 13303 | rettv->vval.v_string = ga.ga_data; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13304 | rettv->v_type = VAR_STRING; |
| 13305 | } |
| 13306 | |
| 13307 | /* |
| 13308 | * "winrestview()" function |
| 13309 | */ |
| 13310 | static void |
| 13311 | f_winrestview(typval_T *argvars, typval_T *rettv UNUSED) |
| 13312 | { |
| 13313 | dict_T *dict; |
| 13314 | |
| 13315 | if (argvars[0].v_type != VAR_DICT |
| 13316 | || (dict = argvars[0].vval.v_dict) == NULL) |
| 13317 | EMSG(_(e_invarg)); |
| 13318 | else |
| 13319 | { |
| 13320 | if (dict_find(dict, (char_u *)"lnum", -1) != NULL) |
| 13321 | curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum"); |
| 13322 | if (dict_find(dict, (char_u *)"col", -1) != NULL) |
| 13323 | curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col"); |
| 13324 | #ifdef FEAT_VIRTUALEDIT |
| 13325 | if (dict_find(dict, (char_u *)"coladd", -1) != NULL) |
| 13326 | curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd"); |
| 13327 | #endif |
| 13328 | if (dict_find(dict, (char_u *)"curswant", -1) != NULL) |
| 13329 | { |
| 13330 | curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant"); |
| 13331 | curwin->w_set_curswant = FALSE; |
| 13332 | } |
| 13333 | |
| 13334 | if (dict_find(dict, (char_u *)"topline", -1) != NULL) |
| 13335 | set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline")); |
| 13336 | #ifdef FEAT_DIFF |
| 13337 | if (dict_find(dict, (char_u *)"topfill", -1) != NULL) |
| 13338 | curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill"); |
| 13339 | #endif |
| 13340 | if (dict_find(dict, (char_u *)"leftcol", -1) != NULL) |
| 13341 | curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol"); |
| 13342 | if (dict_find(dict, (char_u *)"skipcol", -1) != NULL) |
| 13343 | curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol"); |
| 13344 | |
| 13345 | check_cursor(); |
| 13346 | win_new_height(curwin, curwin->w_height); |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 13347 | win_new_width(curwin, curwin->w_width); |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13348 | changed_window_setting(); |
| 13349 | |
| 13350 | if (curwin->w_topline <= 0) |
| 13351 | curwin->w_topline = 1; |
| 13352 | if (curwin->w_topline > curbuf->b_ml.ml_line_count) |
| 13353 | curwin->w_topline = curbuf->b_ml.ml_line_count; |
| 13354 | #ifdef FEAT_DIFF |
| 13355 | check_topfill(curwin, TRUE); |
| 13356 | #endif |
| 13357 | } |
| 13358 | } |
| 13359 | |
| 13360 | /* |
| 13361 | * "winsaveview()" function |
| 13362 | */ |
| 13363 | static void |
| 13364 | f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv) |
| 13365 | { |
| 13366 | dict_T *dict; |
| 13367 | |
| 13368 | if (rettv_dict_alloc(rettv) == FAIL) |
| 13369 | return; |
| 13370 | dict = rettv->vval.v_dict; |
| 13371 | |
| 13372 | dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL); |
| 13373 | dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL); |
| 13374 | #ifdef FEAT_VIRTUALEDIT |
| 13375 | dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL); |
| 13376 | #endif |
| 13377 | update_curswant(); |
| 13378 | dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL); |
| 13379 | |
| 13380 | dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL); |
| 13381 | #ifdef FEAT_DIFF |
| 13382 | dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL); |
| 13383 | #endif |
| 13384 | dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL); |
| 13385 | dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL); |
| 13386 | } |
| 13387 | |
| 13388 | /* |
| 13389 | * "winwidth(nr)" function |
| 13390 | */ |
| 13391 | static void |
| 13392 | f_winwidth(typval_T *argvars, typval_T *rettv) |
| 13393 | { |
| 13394 | win_T *wp; |
| 13395 | |
| 13396 | wp = find_win_by_nr(&argvars[0], NULL); |
| 13397 | if (wp == NULL) |
| 13398 | rettv->vval.v_number = -1; |
| 13399 | else |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13400 | rettv->vval.v_number = wp->w_width; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13401 | } |
| 13402 | |
| 13403 | /* |
| 13404 | * "wordcount()" function |
| 13405 | */ |
| 13406 | static void |
| 13407 | f_wordcount(typval_T *argvars UNUSED, typval_T *rettv) |
| 13408 | { |
| 13409 | if (rettv_dict_alloc(rettv) == FAIL) |
| 13410 | return; |
| 13411 | cursor_pos_info(rettv->vval.v_dict); |
| 13412 | } |
| 13413 | |
| 13414 | /* |
| 13415 | * "writefile()" function |
| 13416 | */ |
| 13417 | static void |
| 13418 | f_writefile(typval_T *argvars, typval_T *rettv) |
| 13419 | { |
| 13420 | int binary = FALSE; |
| 13421 | int append = FALSE; |
Bram Moolenaar | 7567d0b | 2017-11-16 23:04:15 +0100 | [diff] [blame] | 13422 | #ifdef HAVE_FSYNC |
| 13423 | int do_fsync = p_fs; |
| 13424 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13425 | char_u *fname; |
| 13426 | FILE *fd; |
| 13427 | int ret = 0; |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 13428 | listitem_T *li; |
| 13429 | list_T *list; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13430 | |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 13431 | rettv->vval.v_number = -1; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13432 | if (check_restricted() || check_secure()) |
| 13433 | return; |
| 13434 | |
| 13435 | if (argvars[0].v_type != VAR_LIST) |
| 13436 | { |
| 13437 | EMSG2(_(e_listarg), "writefile()"); |
| 13438 | return; |
| 13439 | } |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 13440 | list = argvars[0].vval.v_list; |
| 13441 | if (list == NULL) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13442 | return; |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 13443 | for (li = list->lv_first; li != NULL; li = li->li_next) |
| 13444 | if (get_tv_string_chk(&li->li_tv) == NULL) |
| 13445 | return; |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13446 | |
| 13447 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 13448 | { |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 13449 | char_u *arg2 = get_tv_string_chk(&argvars[2]); |
| 13450 | |
| 13451 | if (arg2 == NULL) |
| 13452 | return; |
| 13453 | if (vim_strchr(arg2, 'b') != NULL) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13454 | binary = TRUE; |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 13455 | if (vim_strchr(arg2, 'a') != NULL) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13456 | append = TRUE; |
Bram Moolenaar | 7567d0b | 2017-11-16 23:04:15 +0100 | [diff] [blame] | 13457 | #ifdef HAVE_FSYNC |
| 13458 | if (vim_strchr(arg2, 's') != NULL) |
| 13459 | do_fsync = TRUE; |
| 13460 | else if (vim_strchr(arg2, 'S') != NULL) |
| 13461 | do_fsync = FALSE; |
| 13462 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13463 | } |
| 13464 | |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 13465 | fname = get_tv_string_chk(&argvars[1]); |
| 13466 | if (fname == NULL) |
| 13467 | return; |
| 13468 | |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13469 | /* Always open the file in binary mode, library functions have a mind of |
| 13470 | * their own about CR-LF conversion. */ |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13471 | if (*fname == NUL || (fd = mch_fopen((char *)fname, |
| 13472 | append ? APPENDBIN : WRITEBIN)) == NULL) |
| 13473 | { |
| 13474 | EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname); |
| 13475 | ret = -1; |
| 13476 | } |
| 13477 | else |
| 13478 | { |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 13479 | if (write_list(fd, list, binary) == FAIL) |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13480 | ret = -1; |
Bram Moolenaar | 7567d0b | 2017-11-16 23:04:15 +0100 | [diff] [blame] | 13481 | #ifdef HAVE_FSYNC |
Bram Moolenaar | 291a9d1 | 2017-11-25 14:37:11 +0100 | [diff] [blame] | 13482 | else if (do_fsync) |
| 13483 | /* Ignore the error, the user wouldn't know what to do about it. |
| 13484 | * May happen for a device. */ |
| 13485 | ignored = fsync(fileno(fd)); |
Bram Moolenaar | 7567d0b | 2017-11-16 23:04:15 +0100 | [diff] [blame] | 13486 | #endif |
Bram Moolenaar | 73dad1e | 2016-07-17 22:13:49 +0200 | [diff] [blame] | 13487 | fclose(fd); |
| 13488 | } |
| 13489 | |
| 13490 | rettv->vval.v_number = ret; |
| 13491 | } |
| 13492 | |
| 13493 | /* |
| 13494 | * "xor(expr, expr)" function |
| 13495 | */ |
| 13496 | static void |
| 13497 | f_xor(typval_T *argvars, typval_T *rettv) |
| 13498 | { |
| 13499 | rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL) |
| 13500 | ^ get_tv_number_chk(&argvars[1], NULL); |
| 13501 | } |
| 13502 | |
| 13503 | |
| 13504 | #endif /* FEAT_EVAL */ |