blob: 8e99b01a5e08c4a025c35b3a5bde0ae3b1aca25b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 * misc2.c: Various functions.
12 */
13#include "vim.h"
14
Bram Moolenaar85a20022019-12-21 18:25:54 +010015static char_u *username = NULL; // cached result of mch_get_user_name()
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000016
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010017static int coladvance2(pos_T *pos, int addspaces, int finetune, colnr_T wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
19/*
20 * Return TRUE if in the current mode we need to use virtual.
21 */
22 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010023virtual_active(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024{
Gary Johnson53ba05b2021-07-26 22:19:10 +020025 unsigned int cur_ve_flags = get_ve_flags();
26
Bram Moolenaar85a20022019-12-21 18:25:54 +010027 // While an operator is being executed we return "virtual_op", because
28 // VIsual_active has already been reset, thus we can't check for "block"
29 // being used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 if (virtual_op != MAYBE)
31 return virtual_op;
Gary Johnson53ba05b2021-07-26 22:19:10 +020032 return (cur_ve_flags == VE_ALL
33 || ((cur_ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V)
34 || ((cur_ve_flags & VE_INSERT) && (State & INSERT)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000035}
36
37/*
38 * Get the screen position of the cursor.
39 */
40 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010041getviscol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000042{
43 colnr_T x;
44
45 getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL);
46 return (int)x;
47}
48
49/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +000050 * Go to column "wcol", and add/insert white space as necessary to get the
Bram Moolenaar071d4272004-06-13 20:20:40 +000051 * cursor in that column.
52 * The caller must have saved the cursor line for undo!
53 */
54 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010055coladvance_force(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000056{
57 int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
58
59 if (wcol == MAXCOL)
60 curwin->w_valid &= ~VALID_VIRTCOL;
61 else
62 {
Bram Moolenaar85a20022019-12-21 18:25:54 +010063 // Virtcol is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 curwin->w_valid |= VALID_VIRTCOL;
65 curwin->w_virtcol = wcol;
66 }
67 return rc;
68}
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
70/*
Bram Moolenaar977239e2019-01-11 16:16:01 +010071 * Get the screen position of character col with a coladd in the cursor line.
72 */
73 int
Bram Moolenaarfc3abf42019-01-24 15:54:21 +010074getviscol2(colnr_T col, colnr_T coladd UNUSED)
Bram Moolenaar977239e2019-01-11 16:16:01 +010075{
76 colnr_T x;
77 pos_T pos;
78
79 pos.lnum = curwin->w_cursor.lnum;
80 pos.col = col;
Bram Moolenaar977239e2019-01-11 16:16:01 +010081 pos.coladd = coladd;
Bram Moolenaar977239e2019-01-11 16:16:01 +010082 getvvcol(curwin, &pos, &x, NULL, NULL);
83 return (int)x;
84}
85
86/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 * Try to advance the Cursor to the specified screen column.
88 * If virtual editing: fine tune the cursor position.
89 * Note that all virtual positions off the end of a line should share
90 * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)),
91 * beginning at coladd 0.
92 *
93 * return OK if desired column is reached, FAIL if not
94 */
95 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010096coladvance(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000097{
98 int rc = getvpos(&curwin->w_cursor, wcol);
99
100 if (wcol == MAXCOL || rc == FAIL)
101 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000102 else if (*ml_get_cursor() != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100104 // Virtcol is valid when not on a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 curwin->w_valid |= VALID_VIRTCOL;
106 curwin->w_virtcol = wcol;
107 }
108 return rc;
109}
110
111/*
112 * Return in "pos" the position of the cursor advanced to screen column "wcol".
113 * return OK if desired column is reached, FAIL if not
114 */
115 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100116getvpos(pos_T *pos, colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 return coladvance2(pos, FALSE, virtual_active(), wcol);
119}
120
121 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100122coladvance2(
123 pos_T *pos,
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200124 int addspaces, // change the text to achieve our goal?
125 int finetune, // change char offset for the exact column
126 colnr_T wcol_arg) // column to move to (can be negative)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127{
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200128 colnr_T wcol = wcol_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 int idx;
130 char_u *ptr;
131 char_u *line;
132 colnr_T col = 0;
133 int csize = 0;
134 int one_more;
135#ifdef FEAT_LINEBREAK
136 int head = 0;
137#endif
138
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000139 one_more = (State & INSERT)
140 || restart_edit != NUL
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000141 || (VIsual_active && *p_sel != 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +0200142 || ((get_ve_flags() & VE_ONEMORE) && wcol < MAXCOL);
Bram Moolenaara1381de2009-11-03 15:44:21 +0000143 line = ml_get_buf(curbuf, pos->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145 if (wcol >= MAXCOL)
146 {
147 idx = (int)STRLEN(line) - 1 + one_more;
148 col = wcol;
149
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 if ((addspaces || finetune) && !VIsual_active)
151 {
152 curwin->w_curswant = linetabsize(line) + one_more;
153 if (curwin->w_curswant > 0)
154 --curwin->w_curswant;
155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 }
157 else
158 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200159 int width = curwin->w_width - win_col_off(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160
Bram Moolenaarebefac62005-12-28 22:39:57 +0000161 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 && curwin->w_p_wrap
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 && curwin->w_width != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 && wcol >= (colnr_T)width)
165 {
166 csize = linetabsize(line);
167 if (csize > 0)
168 csize--;
169
Bram Moolenaarebefac62005-12-28 22:39:57 +0000170 if (wcol / width > (colnr_T)csize / width
171 && ((State & INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100173 // In case of line wrapping don't move the cursor beyond the
174 // right screen edge. In Insert mode allow going just beyond
175 // the last character (like what happens when typing and
176 // reaching the right window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 wcol = (csize / width + 1) * width - 1;
178 }
179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 ptr = line;
182 while (col <= wcol && *ptr != NUL)
183 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100184 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifdef FEAT_LINEBREAK
Bram Moolenaar597a4222014-06-25 14:39:50 +0200186 csize = win_lbr_chartabsize(curwin, line, ptr, col, &head);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100187 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200189 csize = lbr_chartabsize_adv(line, &ptr, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#endif
191 col += csize;
192 }
193 idx = (int)(ptr - line);
194 /*
195 * Handle all the special cases. The virtual_active() check
196 * is needed to ensure that a virtual position off the end of
197 * a line has the correct indexing. The one_more comparison
198 * replaces an explicit add of one_more later on.
199 */
200 if (col > wcol || (!virtual_active() && one_more == 0))
201 {
202 idx -= 1;
203# ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +0100204 // Don't count the chars from 'showbreak'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 csize -= head;
206# endif
207 col -= csize;
208 }
209
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 if (virtual_active()
211 && addspaces
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200212 && wcol >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 && ((col != wcol && col != wcol + 1) || csize > 1))
214 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100215 // 'virtualedit' is set: The difference between wcol and col is
216 // filled with spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
218 if (line[idx] == NUL)
219 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100220 // Append spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 int correct = wcol - col;
222 char_u *newline = alloc(idx + correct + 1);
223 int t;
224
225 if (newline == NULL)
226 return FAIL;
227
228 for (t = 0; t < idx; ++t)
229 newline[t] = line[t];
230
231 for (t = 0; t < correct; ++t)
232 newline[t + idx] = ' ';
233
234 newline[idx + correct] = NUL;
235
236 ml_replace(pos->lnum, newline, FALSE);
237 changed_bytes(pos->lnum, (colnr_T)idx);
238 idx += correct;
239 col = wcol;
240 }
241 else
242 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100243 // Break a tab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 int linelen = (int)STRLEN(line);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100245 int correct = wcol - col - csize + 1; // negative!!
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000246 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 int t, s = 0;
248 int v;
249
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000250 if (-correct > csize)
251 return FAIL;
252
253 newline = alloc(linelen + csize);
254 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 return FAIL;
256
257 for (t = 0; t < linelen; t++)
258 {
259 if (t != idx)
260 newline[s++] = line[t];
261 else
262 for (v = 0; v < csize; v++)
263 newline[s++] = ' ';
264 }
265
266 newline[linelen + csize - 1] = NUL;
267
268 ml_replace(pos->lnum, newline, FALSE);
269 changed_bytes(pos->lnum, idx);
270 idx += (csize - 1 + correct);
271 col += correct;
272 }
273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 }
275
276 if (idx < 0)
277 pos->col = 0;
278 else
279 pos->col = idx;
280
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 pos->coladd = 0;
282
283 if (finetune)
284 {
285 if (wcol == MAXCOL)
286 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100287 // The width of the last character is used to set coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 if (!one_more)
289 {
290 colnr_T scol, ecol;
291
292 getvcol(curwin, pos, &scol, NULL, &ecol);
293 pos->coladd = ecol - scol;
294 }
295 }
296 else
297 {
298 int b = (int)wcol - (int)col;
299
Bram Moolenaar85a20022019-12-21 18:25:54 +0100300 // The difference between wcol and col is used to set coladd.
Bram Moolenaar02631462017-09-22 15:20:32 +0200301 if (b > 0 && b < (MAXCOL - 2 * curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 pos->coladd = b;
303
304 col += b;
305 }
306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307
Bram Moolenaar85a20022019-12-21 18:25:54 +0100308 // prevent from moving onto a trail byte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200310 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200312 if (wcol < 0 || col < wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 return FAIL;
314 return OK;
315}
316
317/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000318 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 */
320 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100321inc_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322{
323 return inc(&curwin->w_cursor);
324}
325
Bram Moolenaar446cb832008-06-24 21:56:24 +0000326/*
327 * Increment the line pointer "lp" crossing line boundaries as necessary.
328 * Return 1 when going to the next line.
329 * Return 2 when moving forward onto a NUL at the end of the line).
330 * Return -1 when at the end of file.
331 * Return 0 otherwise.
332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100334inc(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100336 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
Bram Moolenaar85a20022019-12-21 18:25:54 +0100338 // when searching position may be set to end of a line
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100339 if (lp->col != MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100341 p = ml_get_pos(lp);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100342 if (*p != NUL) // still within line, move to next char (may be NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100344 if (has_mbyte)
345 {
346 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100348 lp->col += l;
349 return ((p[l] != NUL) ? 0 : 2);
350 }
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100351 lp->col++;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100352 lp->coladd = 0;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100353 return ((p[1] != NUL) ? 0 : 2);
354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100356 if (lp->lnum != curbuf->b_ml.ml_line_count) // there is a next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {
358 lp->col = 0;
359 lp->lnum++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 lp->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 return 1;
362 }
363 return -1;
364}
365
366/*
367 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
368 */
369 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100370incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371{
372 int r;
373
374 if ((r = inc(lp)) >= 1 && lp->col)
375 r = inc(lp);
376 return r;
377}
378
379/*
380 * dec(p)
381 *
382 * Decrement the line pointer 'p' crossing line boundaries as necessary.
383 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
384 */
385 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100386dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100388 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389}
390
391 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100392dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393{
394 char_u *p;
395
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 lp->coladd = 0;
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100397 if (lp->col == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100399 // past end of line
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100400 p = ml_get(lp->lnum);
401 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100402 if (has_mbyte)
403 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100404 return 0;
405 }
406
407 if (lp->col > 0)
408 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100409 // still within line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 lp->col--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 if (has_mbyte)
412 {
413 p = ml_get(lp->lnum);
414 lp->col -= (*mb_head_off)(p, p + lp->col);
415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 return 0;
417 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100418
419 if (lp->lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100421 // there is a prior line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 lp->lnum--;
423 p = ml_get(lp->lnum);
424 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 if (has_mbyte)
426 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 return 1;
428 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100429
Bram Moolenaar85a20022019-12-21 18:25:54 +0100430 // at start of file
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100431 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432}
433
434/*
435 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
436 */
437 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100438decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439{
440 int r;
441
442 if ((r = dec(lp)) == 1 && lp->col)
443 r = dec(lp);
444 return r;
445}
446
447/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200448 * Get the line number relative to the current cursor position, i.e. the
449 * difference between line number and cursor position. Only look for lines that
450 * can be visible, folded lines don't count.
451 */
452 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100453get_cursor_rel_lnum(
454 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100455 linenr_T lnum) // line number to get the result for
Bram Moolenaar64486672010-05-16 15:46:46 +0200456{
457 linenr_T cursor = wp->w_cursor.lnum;
458 linenr_T retval = 0;
459
460#ifdef FEAT_FOLDING
461 if (hasAnyFolding(wp))
462 {
463 if (lnum > cursor)
464 {
465 while (lnum > cursor)
466 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100467 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100468 // if lnum and cursor are in the same fold,
469 // now lnum <= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200470 if (lnum > cursor)
471 retval++;
472 lnum--;
473 }
474 }
475 else if (lnum < cursor)
476 {
477 while (lnum < cursor)
478 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100479 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100480 // if lnum and cursor are in the same fold,
481 // now lnum >= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200482 if (lnum < cursor)
483 retval--;
484 lnum++;
485 }
486 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100487 // else if (lnum == cursor)
488 // retval = 0;
Bram Moolenaar64486672010-05-16 15:46:46 +0200489 }
490 else
491#endif
492 retval = lnum - cursor;
493
494 return retval;
495}
496
497/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200498 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
499 * This allows for the col to be on the NUL byte.
500 */
501 void
502check_pos(buf_T *buf, pos_T *pos)
503{
504 char_u *line;
505 colnr_T len;
506
507 if (pos->lnum > buf->b_ml.ml_line_count)
508 pos->lnum = buf->b_ml.ml_line_count;
509
510 if (pos->col > 0)
511 {
512 line = ml_get_buf(buf, pos->lnum, FALSE);
513 len = (colnr_T)STRLEN(line);
514 if (pos->col > len)
515 pos->col = len;
516 }
517}
518
519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 * Make sure curwin->w_cursor.lnum is valid.
521 */
522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100523check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
526 {
527#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100528 // If there is a closed fold at the end of the file, put the cursor in
529 // its first line. Otherwise in the last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 if (!hasFolding(curbuf->b_ml.ml_line_count,
531 &curwin->w_cursor.lnum, NULL))
532#endif
533 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
534 }
535 if (curwin->w_cursor.lnum <= 0)
536 curwin->w_cursor.lnum = 1;
537}
538
539/*
540 * Make sure curwin->w_cursor.col is valid.
541 */
542 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100543check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200545 check_cursor_col_win(curwin);
546}
547
548/*
549 * Make sure win->w_cursor.col is valid.
550 */
551 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100552check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200553{
Gary Johnson53ba05b2021-07-26 22:19:10 +0200554 colnr_T len;
555 colnr_T oldcol = win->w_cursor.col;
556 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
557 unsigned int cur_ve_flags = get_ve_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200559 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200561 win->w_cursor.col = 0;
562 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100564 // Allow cursor past end-of-line when:
565 // - in Insert mode or restarting Insert mode
566 // - in Visual mode and 'selection' isn't "old"
567 // - 'virtualedit' is set
Bram Moolenaarebefac62005-12-28 22:39:57 +0000568 if ((State & INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 || (VIsual_active && *p_sel != 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +0200570 || (cur_ve_flags & VE_ONEMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200572 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000574 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200575 win->w_cursor.col = len - 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100576 // Move the cursor to the head byte.
Bram Moolenaar87c19962007-04-26 08:54:21 +0000577 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200578 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200581 else if (win->w_cursor.col < 0)
582 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583
Bram Moolenaar85a20022019-12-21 18:25:54 +0100584 // If virtual editing is on, we can leave the cursor on the old position,
585 // only we must set it to virtual. But don't do it when at the end of the
586 // line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200588 win->w_cursor.coladd = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +0200589 else if (cur_ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000590 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200591 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200592 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200593 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaard41babe2017-08-30 17:01:35 +0200594
Bram Moolenaar85a20022019-12-21 18:25:54 +0100595 // Make sure that coladd is not more than the char width.
596 // Not for the last character, coladd is then used when the cursor
597 // is actually after the last character.
Bram Moolenaard41babe2017-08-30 17:01:35 +0200598 if (win->w_cursor.col + 1 < len && win->w_cursor.coladd > 0)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200599 {
600 int cs, ce;
601
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200602 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
603 if (win->w_cursor.coladd > ce - cs)
604 win->w_cursor.coladd = ce - cs;
605 }
606 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000607 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100608 // avoid weird number when there is a miscalculation or overflow
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200609 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611}
612
613/*
614 * make sure curwin->w_cursor in on a valid character
615 */
616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100617check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
619 check_cursor_lnum();
620 check_cursor_col();
621}
622
623#if defined(FEAT_TEXTOBJ) || defined(PROTO)
624/*
625 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
626 * Allow it when in Visual mode and 'selection' is not "old".
627 */
628 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100629adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630{
631 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 && gchar_cursor() == NUL)
634 --curwin->w_cursor.col;
635}
636#endif
637
638/*
639 * When curwin->w_leftcol has changed, adjust the cursor position.
640 * Return TRUE if the cursor was moved.
641 */
642 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100643leftcol_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644{
645 long lastcol;
646 colnr_T s, e;
647 int retval = FALSE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100648 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650 changed_cline_bef_curs();
Bram Moolenaar02631462017-09-22 15:20:32 +0200651 lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 validate_virtcol();
653
654 /*
655 * If the cursor is right or left of the screen, move it to last or first
656 * character.
657 */
Bram Moolenaar375e3392019-01-31 18:26:10 +0100658 if (curwin->w_virtcol > (colnr_T)(lastcol - siso))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {
660 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100661 coladvance((colnr_T)(lastcol - siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100663 else if (curwin->w_virtcol < curwin->w_leftcol + siso)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 {
665 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100666 (void)coladvance((colnr_T)(curwin->w_leftcol + siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 }
668
669 /*
670 * If the start of the character under the cursor is not on the screen,
671 * advance the cursor one more char. If this fails (last char of the
672 * line) adjust the scrolling.
673 */
674 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
675 if (e > (colnr_T)lastcol)
676 {
677 retval = TRUE;
678 coladvance(s - 1);
679 }
680 else if (s < curwin->w_leftcol)
681 {
682 retval = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100683 if (coladvance(e + 1) == FAIL) // there isn't another character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100685 curwin->w_leftcol = s; // adjust w_leftcol instead
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 changed_cline_bef_curs();
687 }
688 }
689
690 if (retval)
691 curwin->w_set_curswant = TRUE;
692 redraw_later(NOT_VALID);
693 return retval;
694}
695
696/**********************************************************************
697 * Various routines dealing with allocation and deallocation of memory.
698 */
699
700#if defined(MEM_PROFILE) || defined(PROTO)
701
702# define MEM_SIZES 8200
703static long_u mem_allocs[MEM_SIZES];
704static long_u mem_frees[MEM_SIZES];
705static long_u mem_allocated;
706static long_u mem_freed;
707static long_u mem_peak;
708static long_u num_alloc;
709static long_u num_freed;
710
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100712mem_pre_alloc_s(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713{
714 *sizep += sizeof(size_t);
715}
716
717 static void
Bram Moolenaar964b3742019-05-24 18:54:09 +0200718mem_pre_alloc_l(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
720 *sizep += sizeof(size_t);
721}
722
723 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100724mem_post_alloc(
725 void **pp,
726 size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
728 if (*pp == NULL)
729 return;
730 size -= sizeof(size_t);
731 *(long_u *)*pp = size;
732 if (size <= MEM_SIZES-1)
733 mem_allocs[size-1]++;
734 else
735 mem_allocs[MEM_SIZES-1]++;
736 mem_allocated += size;
737 if (mem_allocated - mem_freed > mem_peak)
738 mem_peak = mem_allocated - mem_freed;
739 num_alloc++;
740 *pp = (void *)((char *)*pp + sizeof(size_t));
741}
742
743 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100744mem_pre_free(void **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
746 long_u size;
747
748 *pp = (void *)((char *)*pp - sizeof(size_t));
749 size = *(size_t *)*pp;
750 if (size <= MEM_SIZES-1)
751 mem_frees[size-1]++;
752 else
753 mem_frees[MEM_SIZES-1]++;
754 mem_freed += size;
755 num_freed++;
756}
757
758/*
759 * called on exit via atexit()
760 */
761 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100762vim_mem_profile_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 int i, j;
765
766 printf("\r\n");
767 j = 0;
768 for (i = 0; i < MEM_SIZES - 1; i++)
769 {
770 if (mem_allocs[i] || mem_frees[i])
771 {
772 if (mem_frees[i] > mem_allocs[i])
773 printf("\r\n%s", _("ERROR: "));
774 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
775 j++;
776 if (j > 3)
777 {
778 j = 0;
779 printf("\r\n");
780 }
781 }
782 }
783
784 i = MEM_SIZES - 1;
785 if (mem_allocs[i])
786 {
787 printf("\r\n");
788 if (mem_frees[i] > mem_allocs[i])
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200789 puts(_("ERROR: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
791 }
792
793 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
794 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
795 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
796 num_alloc, num_freed);
797}
798
Bram Moolenaar85a20022019-12-21 18:25:54 +0100799#endif // MEM_PROFILE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100801#ifdef FEAT_EVAL
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100802 int
Bram Moolenaar964b3742019-05-24 18:54:09 +0200803alloc_does_fail(size_t size)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100804{
805 if (alloc_fail_countdown == 0)
806 {
807 if (--alloc_fail_repeat <= 0)
808 alloc_fail_id = 0;
Bram Moolenaara260b872016-01-15 20:48:22 +0100809 do_outofmem_msg(size);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100810 return TRUE;
811 }
812 --alloc_fail_countdown;
813 return FALSE;
814}
815#endif
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817/*
818 * Some memory is reserved for error messages and for being able to
819 * call mf_release_all(), which needs some memory for mf_trans_add().
820 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100821#define KEEP_ROOM (2 * 8192L)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200822#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
824/*
Bram Moolenaar964b3742019-05-24 18:54:09 +0200825 * The normal way to allocate memory. This handles an out-of-memory situation
826 * as well as possible, still returns NULL when we're completely out.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200828 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200829alloc(size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830{
Bram Moolenaar964b3742019-05-24 18:54:09 +0200831 return lalloc(size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833
834/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100835 * alloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100836 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200837 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200838alloc_id(size_t size, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100839{
840#ifdef FEAT_EVAL
Bram Moolenaar964b3742019-05-24 18:54:09 +0200841 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100842 return NULL;
843#endif
Bram Moolenaar964b3742019-05-24 18:54:09 +0200844 return lalloc(size, TRUE);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100845}
846
847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 * Allocate memory and set all bytes to zero.
849 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200850 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200851alloc_clear(size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200853 void *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
Bram Moolenaar964b3742019-05-24 18:54:09 +0200855 p = lalloc(size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 if (p != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200857 (void)vim_memset(p, 0, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 return p;
859}
860
861/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100862 * Same as alloc_clear() but with allocation id for testing
863 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200864 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200865alloc_clear_id(size_t size, alloc_id_T id UNUSED)
Bram Moolenaar162b7142018-12-21 15:17:36 +0100866{
867#ifdef FEAT_EVAL
Bram Moolenaar964b3742019-05-24 18:54:09 +0200868 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar162b7142018-12-21 15:17:36 +0100869 return NULL;
870#endif
871 return alloc_clear(size);
872}
873
874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * Allocate memory like lalloc() and set all bytes to zero.
876 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200877 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200878lalloc_clear(size_t size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200880 void *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200882 p = lalloc(size, message);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (p != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200884 (void)vim_memset(p, 0, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 return p;
886}
887
888/*
889 * Low level memory allocation function.
890 * This is used often, KEEP IT FAST!
891 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200892 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200893lalloc(size_t size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100895 void *p; // pointer to new storage space
896 static int releasing = FALSE; // don't do mf_release_all() recursive
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 int try_again;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100898#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100899 static size_t allocated = 0; // allocated since last avail check
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#endif
901
Bram Moolenaar964b3742019-05-24 18:54:09 +0200902 // Safety check for allocating zero bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (size == 0)
904 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200905 // Don't hide this message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 emsg_silent = 0;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200907 iemsg(_("E341: Internal error: lalloc(0, )"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 return NULL;
909 }
910
911#ifdef MEM_PROFILE
912 mem_pre_alloc_l(&size);
913#endif
914
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 /*
916 * Loop when out of memory: Try to release some memfile blocks and
917 * if some blocks are released call malloc again.
918 */
919 for (;;)
920 {
921 /*
922 * Handle three kind of systems:
923 * 1. No check for available memory: Just return.
924 * 2. Slow check for available memory: call mch_avail_mem() after
925 * allocating KEEP_ROOM amount of memory.
926 * 3. Strict check for available memory: call mch_avail_mem()
927 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200928 if ((p = malloc(size)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
930#ifndef HAVE_AVAIL_MEM
Bram Moolenaar85a20022019-12-21 18:25:54 +0100931 // 1. No check for available memory: Just return.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 goto theend;
933#else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100934 // 2. Slow check for available memory: call mch_avail_mem() after
935 // allocating (KEEP_ROOM / 2) amount of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 allocated += size;
937 if (allocated < KEEP_ROOM / 2)
938 goto theend;
939 allocated = 0;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100940
Bram Moolenaar85a20022019-12-21 18:25:54 +0100941 // 3. check for available memory: call mch_avail_mem()
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200942 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100944 free(p); // System is low... no go!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 p = NULL;
946 }
947 else
948 goto theend;
949#endif
950 }
951 /*
952 * Remember that mf_release_all() is being called to avoid an endless
953 * loop, because mf_release_all() may call alloc() recursively.
954 */
955 if (releasing)
956 break;
957 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +0000958
Bram Moolenaar85a20022019-12-21 18:25:54 +0100959 clear_sb_text(TRUE); // free any scrollback text
960 try_again = mf_release_all(); // release as many blocks as possible
Bram Moolenaar661b1822005-07-28 22:36:45 +0000961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 releasing = FALSE;
963 if (!try_again)
964 break;
965 }
966
967 if (message && p == NULL)
968 do_outofmem_msg(size);
969
970theend:
971#ifdef MEM_PROFILE
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200972 mem_post_alloc(&p, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973#endif
974 return p;
975}
976
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100977/*
978 * lalloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100979 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100980#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200981 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200982lalloc_id(size_t size, int message, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100983{
984#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100985 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100986 return NULL;
987#endif
Bram Moolenaar964b3742019-05-24 18:54:09 +0200988 return (lalloc(size, message));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100989}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100990#endif
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992#if defined(MEM_PROFILE) || defined(PROTO)
993/*
994 * realloc() with memory profiling.
995 */
996 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100997mem_realloc(void *ptr, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998{
999 void *p;
1000
1001 mem_pre_free(&ptr);
1002 mem_pre_alloc_s(&size);
1003
1004 p = realloc(ptr, size);
1005
1006 mem_post_alloc(&p, size);
1007
1008 return p;
1009}
1010#endif
1011
1012/*
1013* Avoid repeating the error message many times (they take 1 second each).
1014* Did_outofmem_msg is reset when a character is read.
1015*/
1016 void
Bram Moolenaar964b3742019-05-24 18:54:09 +02001017do_outofmem_msg(size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018{
1019 if (!did_outofmem_msg)
1020 {
Bram Moolenaar4dc8f492019-08-21 13:06:55 +02001021 // Don't hide this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001023
Bram Moolenaar4dc8f492019-08-21 13:06:55 +02001024 // Must come first to avoid coming back here when printing the error
1025 // message fails, e.g. when setting v:errmsg.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001027
Bram Moolenaar964b3742019-05-24 18:54:09 +02001028 semsg(_("E342: Out of memory! (allocating %lu bytes)"), (long_u)size);
Bram Moolenaar4dc8f492019-08-21 13:06:55 +02001029
1030 if (starting == NO_SCREEN)
1031 // Not even finished with initializations and already out of
1032 // memory? Then nothing is going to work, exit.
1033 mch_exit(123);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
1035}
1036
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001037#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001038
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001039/*
1040 * Free everything that we allocated.
1041 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001042 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1043 * surprised if Vim crashes...
1044 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001045 */
1046 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001047free_all_mem(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001048{
1049 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001050
Bram Moolenaar85a20022019-12-21 18:25:54 +01001051 // When we cause a crash here it is caught and Vim tries to exit cleanly.
1052 // Don't try freeing everything again.
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001053 if (entered_free_all_mem)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001054 return;
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001055 entered_free_all_mem = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001056 // Don't want to trigger autocommands from here on.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001057 block_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001058
Bram Moolenaar85a20022019-12-21 18:25:54 +01001059 // Close all tabs and windows. Reset 'equalalways' to avoid redraws.
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001060 p_ea = FALSE;
Bram Moolenaare5c83282019-05-03 23:15:37 +02001061 if (first_tabpage != NULL && first_tabpage->tp_next != NULL)
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001062 do_cmdline_cmd((char_u *)"tabonly!");
Bram Moolenaar95f09602016-11-10 20:01:45 +01001063 if (!ONE_WINDOW)
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001064 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001065
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001066# if defined(FEAT_SPELL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001067 // Free all spell info.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001068 spell_free_all();
1069# endif
1070
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001071# if defined(FEAT_BEVAL_TERM)
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001072 ui_remove_balloon();
1073# endif
Bram Moolenaar03a9f842020-05-13 13:40:16 +02001074# ifdef FEAT_PROP_POPUP
Bram Moolenaar06f08532020-05-12 23:45:16 +02001075 if (curwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02001076 close_all_popups(TRUE);
Bram Moolenaar06f08532020-05-12 23:45:16 +02001077# endif
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001078
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001079 // Clear user commands (before deleting buffers).
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001080 ex_comclear(NULL);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001081
Bram Moolenaare5c83282019-05-03 23:15:37 +02001082 // When exiting from mainerr_arg_missing curbuf has not been initialized,
1083 // and not much else.
1084 if (curbuf != NULL)
1085 {
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001086# ifdef FEAT_MENU
Bram Moolenaare5c83282019-05-03 23:15:37 +02001087 // Clear menus.
1088 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001089# ifdef FEAT_MULTI_LANG
Bram Moolenaare5c83282019-05-03 23:15:37 +02001090 do_cmdline_cmd((char_u *)"menutranslate clear");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001091# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001092# endif
Bram Moolenaare5c83282019-05-03 23:15:37 +02001093 // Clear mappings, abbreviations, breakpoints.
1094 do_cmdline_cmd((char_u *)"lmapclear");
1095 do_cmdline_cmd((char_u *)"xmapclear");
1096 do_cmdline_cmd((char_u *)"mapclear");
1097 do_cmdline_cmd((char_u *)"mapclear!");
1098 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001099# if defined(FEAT_EVAL)
Bram Moolenaare5c83282019-05-03 23:15:37 +02001100 do_cmdline_cmd((char_u *)"breakdel *");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001101# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001102# if defined(FEAT_PROFILE)
Bram Moolenaare5c83282019-05-03 23:15:37 +02001103 do_cmdline_cmd((char_u *)"profdel *");
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001104# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001105# if defined(FEAT_KEYMAP)
Bram Moolenaare5c83282019-05-03 23:15:37 +02001106 do_cmdline_cmd((char_u *)"set keymap=");
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001107# endif
Bram Moolenaare5c83282019-05-03 23:15:37 +02001108 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001109
1110# ifdef FEAT_TITLE
1111 free_titles();
1112# endif
1113# if defined(FEAT_SEARCHPATH)
1114 free_findfile();
1115# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001116
Bram Moolenaar85a20022019-12-21 18:25:54 +01001117 // Obviously named calls.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001118 free_all_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001119 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001120 free_all_marks();
1121 alist_clear(&global_alist);
1122 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001123 free_users();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001124 free_search_patterns();
1125 free_old_sub();
1126 free_last_insert();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001127 free_insexpand_stuff();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001128 free_prev_shellcmd();
1129 free_regexp_stuff();
1130 free_tag_stuff();
1131 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001132# ifdef FEAT_SIGNS
1133 free_signs();
1134# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001135# ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001136 set_expr_line(NULL, NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001137# endif
1138# ifdef FEAT_DIFF
Bram Moolenaare5c83282019-05-03 23:15:37 +02001139 if (curtab != NULL)
1140 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001141# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001142 clear_sb_text(TRUE); // free any scrollback text
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001143
Bram Moolenaar85a20022019-12-21 18:25:54 +01001144 // Free some global vars.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001145 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001146# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001147 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001148# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001149 vim_free(last_cmdline);
1150 vim_free(new_last_cmdline);
Bram Moolenaar238a5642006-02-21 22:12:05 +00001151 set_keep_msg(NULL, 0);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001152
Bram Moolenaar85a20022019-12-21 18:25:54 +01001153 // Clear cmdline history.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001154 p_hi = 0;
1155 init_history();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001156# ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001157 clear_global_prop_types();
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001158# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001159
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001160# ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001161 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001162 win_T *win;
1163 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001164
1165 qf_free_all(NULL);
Bram Moolenaare5c83282019-05-03 23:15:37 +02001166 // Free all location lists
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001167 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001168 qf_free_all(win);
1169 }
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001170# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001171
Bram Moolenaare5c83282019-05-03 23:15:37 +02001172 // Close all script inputs.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001173 close_all_scripts();
1174
Bram Moolenaare5c83282019-05-03 23:15:37 +02001175 if (curwin != NULL)
1176 // Destroy all windows. Must come before freeing buffers.
1177 win_free_all();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001178
Bram Moolenaar85a20022019-12-21 18:25:54 +01001179 // Free all option values. Must come after closing windows.
Bram Moolenaar4f198282017-10-23 21:53:30 +02001180 free_all_options();
1181
Bram Moolenaar85a20022019-12-21 18:25:54 +01001182 // Free all buffers. Reset 'autochdir' to avoid accessing things that
1183 // were freed already.
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001184# ifdef FEAT_AUTOCHDIR
Bram Moolenaar2a329742008-04-01 12:53:43 +00001185 p_acd = FALSE;
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001186# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001187 for (buf = firstbuf; buf != NULL; )
1188 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001189 bufref_T bufref;
1190
1191 set_bufref(&bufref, buf);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001192 nextbuf = buf->b_next;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001193 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001194 if (bufref_valid(&bufref))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001195 buf = nextbuf; // didn't work, try next one
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001196 else
1197 buf = firstbuf;
1198 }
1199
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001200# ifdef FEAT_ARABIC
1201 free_arshape_buf();
1202# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001203
Bram Moolenaar85a20022019-12-21 18:25:54 +01001204 // Clear registers.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001205 clear_registers();
1206 ResetRedobuff();
1207 ResetRedobuff();
1208
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001209# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001210 vim_free(serverDelayedStartName);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001211# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001212
Bram Moolenaar85a20022019-12-21 18:25:54 +01001213 // highlight info
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001214 free_highlight();
1215
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001216 reset_last_sourcing();
1217
Bram Moolenaare5c83282019-05-03 23:15:37 +02001218 if (first_tabpage != NULL)
1219 {
1220 free_tabpage(first_tabpage);
1221 first_tabpage = NULL;
1222 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001223
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001224# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01001225 // Machine-specific free.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001226 mch_free_mem();
1227# endif
1228
Bram Moolenaar85a20022019-12-21 18:25:54 +01001229 // message history
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001230 for (;;)
1231 if (delete_first_msg() == FAIL)
1232 break;
1233
Bram Moolenaar655da312016-05-28 22:22:34 +02001234# ifdef FEAT_JOB_CHANNEL
1235 channel_free_all();
Bram Moolenaar655da312016-05-28 22:22:34 +02001236# endif
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001237# ifdef FEAT_TIMERS
Bram Moolenaar623e2632016-07-30 22:47:56 +02001238 timer_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001239# endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001240# ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001241 // must be after channel_free_all() with unrefs partials
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001242 eval_clear();
1243# endif
1244# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001245 // must be after eval_clear() with unrefs jobs
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001246 job_free_all();
1247# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001248
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001249 free_termoptions();
1250
Bram Moolenaar85a20022019-12-21 18:25:54 +01001251 // screenlines (can't display anything now!)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001252 free_screenlines();
1253
Bram Moolenaar82febc12019-06-10 14:48:59 +02001254# if defined(FEAT_SOUND)
1255 sound_free();
1256# endif
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001257# if defined(USE_XSMP)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001258 xsmp_close();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001259# endif
1260# ifdef FEAT_GUI_GTK
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001261 gui_mch_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001262# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001263 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001264
1265 vim_free(IObuff);
1266 vim_free(NameBuff);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001267# ifdef FEAT_QUICKFIX
1268 check_quickfix_busy();
1269# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001270}
1271#endif
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001274 * Copy "p[len]" into allocated memory, ignoring NUL characters.
1275 * Returns NULL when out of memory.
1276 */
1277 char_u *
Bram Moolenaar964b3742019-05-24 18:54:09 +02001278vim_memsave(char_u *p, size_t len)
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001279{
Bram Moolenaar964b3742019-05-24 18:54:09 +02001280 char_u *ret = alloc(len);
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001281
1282 if (ret != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001283 mch_memmove(ret, p, len);
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001284 return ret;
1285}
1286
1287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 * Isolate one part of a string option where parts are separated with
1289 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001290 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 * "*option" is advanced to the next part.
1292 * The length is returned.
1293 */
1294 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001295copy_option_part(
1296 char_u **option,
1297 char_u *buf,
1298 int maxlen,
1299 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 int len = 0;
1302 char_u *p = *option;
1303
Bram Moolenaar85a20022019-12-21 18:25:54 +01001304 // skip '.' at start of option part, for 'suffixes'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if (*p == '.')
1306 buf[len++] = *p++;
1307 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1308 {
1309 /*
1310 * Skip backslash before a separator character and space.
1311 */
1312 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1313 ++p;
1314 if (len < maxlen - 1)
1315 buf[len++] = *p;
1316 ++p;
1317 }
1318 buf[len] = NUL;
1319
Bram Moolenaar85a20022019-12-21 18:25:54 +01001320 if (*p != NUL && *p != ',') // skip non-standard separator
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 ++p;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001322 p = skip_to_option_part(p); // p points to next file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
1324 *option = p;
1325 return len;
1326}
1327
1328/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001329 * Replacement for free() that ignores NULL pointers.
1330 * Also skip free() when exiting for sure, this helps when we caught a deadly
1331 * signal that was caused by a crash in free().
Bram Moolenaard23a8232018-02-10 18:45:26 +01001332 * If you want to set NULL after calling this function, you should use
1333 * VIM_CLEAR() instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 */
1335 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001336vim_free(void *x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001338 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 {
1340#ifdef MEM_PROFILE
1341 mem_pre_free(&x);
1342#endif
1343 free(x);
1344 }
1345}
1346
1347#ifndef HAVE_MEMSET
1348 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001349vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
1351 char *p = ptr;
1352
1353 while (size-- > 0)
1354 *p++ = c;
1355 return ptr;
1356}
1357#endif
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359/*
1360 * Vim has its own isspace() function, because on some machines isspace()
1361 * can't handle characters above 128.
1362 */
1363 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001364vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 return ((x >= 9 && x <= 13) || x == ' ');
1367}
1368
1369/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001370 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 */
1372
1373/*
1374 * Clear an allocated growing array.
1375 */
1376 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001377ga_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378{
1379 vim_free(gap->ga_data);
1380 ga_init(gap);
1381}
1382
1383/*
1384 * Clear a growing array that contains a list of strings.
1385 */
1386 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001387ga_clear_strings(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 int i;
1390
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001391 if (gap->ga_data != NULL)
1392 for (i = 0; i < gap->ga_len; ++i)
1393 vim_free(((char_u **)(gap->ga_data))[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 ga_clear(gap);
1395}
1396
1397/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001398 * Copy a growing array that contains a list of strings.
1399 */
1400 int
1401ga_copy_strings(garray_T *from, garray_T *to)
1402{
1403 int i;
1404
1405 ga_init2(to, sizeof(char_u *), 1);
1406 if (ga_grow(to, from->ga_len) == FAIL)
1407 return FAIL;
1408
1409 for (i = 0; i < from->ga_len; ++i)
1410 {
1411 char_u *orig = ((char_u **)from->ga_data)[i];
1412 char_u *copy;
1413
1414 if (orig == NULL)
1415 copy = NULL;
1416 else
1417 {
1418 copy = vim_strsave(orig);
1419 if (copy == NULL)
1420 {
1421 to->ga_len = i;
1422 ga_clear_strings(to);
1423 return FAIL;
1424 }
1425 }
1426 ((char_u **)to->ga_data)[i] = copy;
1427 }
1428 to->ga_len = from->ga_len;
1429 return OK;
1430}
1431
1432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 * Initialize a growing array. Don't forget to set ga_itemsize and
1434 * ga_growsize! Or use ga_init2().
1435 */
1436 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001437ga_init(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438{
1439 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00001440 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 gap->ga_len = 0;
1442}
1443
1444 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001445ga_init2(garray_T *gap, int itemsize, int growsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
1447 ga_init(gap);
1448 gap->ga_itemsize = itemsize;
1449 gap->ga_growsize = growsize;
1450}
1451
1452/*
1453 * Make room in growing array "gap" for at least "n" items.
1454 * Return FAIL for failure, OK otherwise.
1455 */
1456 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001457ga_grow(garray_T *gap, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001459 if (gap->ga_maxlen - gap->ga_len < n)
1460 return ga_grow_inner(gap, n);
1461 return OK;
1462}
1463
1464 int
1465ga_grow_inner(garray_T *gap, int n)
1466{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01001467 size_t old_len;
1468 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 char_u *pp;
1470
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001471 if (n < gap->ga_growsize)
1472 n = gap->ga_growsize;
Bram Moolenaarc47ed442019-06-01 14:36:26 +02001473
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001474 // A linear growth is very inefficient when the array grows big. This
1475 // is a compromise between allocating memory that won't be used and too
1476 // many copy operations. A factor of 1.5 seems reasonable.
1477 if (n < gap->ga_len / 2)
1478 n = gap->ga_len / 2;
Bram Moolenaarc47ed442019-06-01 14:36:26 +02001479
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001480 new_len = gap->ga_itemsize * (gap->ga_len + n);
1481 pp = vim_realloc(gap->ga_data, new_len);
1482 if (pp == NULL)
1483 return FAIL;
1484 old_len = gap->ga_itemsize * gap->ga_maxlen;
1485 vim_memset(pp + old_len, 0, new_len - old_len);
1486 gap->ga_maxlen = gap->ga_len + n;
1487 gap->ga_data = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 return OK;
1489}
1490
Bram Moolenaar113e1072019-01-20 15:30:40 +01001491#if defined(FEAT_EVAL) || defined(FEAT_SEARCHPATH) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001493 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001494 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001495 * Returns NULL when out of memory.
1496 */
1497 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001498ga_concat_strings(garray_T *gap, char *sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001499{
1500 int i;
1501 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001502 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001503 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001504 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001505
1506 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001507 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001508
1509 s = alloc(len + 1);
1510 if (s != NULL)
1511 {
1512 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001513 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001514 for (i = 0; i < gap->ga_len; ++i)
1515 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001516 if (p != s)
1517 {
1518 STRCPY(p, sep);
1519 p += sep_len;
1520 }
1521 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
1522 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001523 }
1524 }
1525 return s;
1526}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001527#endif
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001528
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +01001529#if defined(FEAT_VIMINFO) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001530/*
1531 * Make a copy of string "p" and add it to "gap".
1532 * When out of memory nothing changes.
1533 */
1534 void
1535ga_add_string(garray_T *gap, char_u *p)
1536{
1537 char_u *cp = vim_strsave(p);
1538
1539 if (cp != NULL)
1540 {
1541 if (ga_grow(gap, 1) == OK)
1542 ((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
1543 else
1544 vim_free(cp);
1545 }
1546}
1547#endif
1548
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001549/*
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001550 * Concatenate a string to a growarray which contains bytes.
Bram Moolenaar43345542015-11-29 17:35:35 +01001551 * When "s" is NULL does not do anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 * Note: Does NOT copy the NUL at the end!
1553 */
1554 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001555ga_concat(garray_T *gap, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556{
Bram Moolenaar43345542015-11-29 17:35:35 +01001557 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaar478af672017-04-10 22:22:42 +02001559 if (s == NULL || *s == NUL)
Bram Moolenaar43345542015-11-29 17:35:35 +01001560 return;
1561 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (ga_grow(gap, len) == OK)
1563 {
1564 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
1565 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 }
1567}
1568
1569/*
1570 * Append one byte to a growarray which contains bytes.
1571 */
1572 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001573ga_append(garray_T *gap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
1575 if (ga_grow(gap, 1) == OK)
1576 {
1577 *((char *)gap->ga_data + gap->ga_len) = c;
1578 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580}
1581
Bram Moolenaar4f974752019-02-17 17:44:42 +01001582#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(MSWIN) \
Bram Moolenaar597a4222014-06-25 14:39:50 +02001583 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02001584/*
1585 * Append the text in "gap" below the cursor line and clear "gap".
1586 */
1587 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001588append_ga_line(garray_T *gap)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02001589{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001590 // Remove trailing CR.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02001591 if (gap->ga_len > 0
1592 && !curbuf->b_p_bin
1593 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
1594 --gap->ga_len;
1595 ga_append(gap, NUL);
1596 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
1597 gap->ga_len = 0;
1598}
1599#endif
1600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601/************************************************************************
1602 * functions that use lookup tables for various things, generally to do with
1603 * special key codes.
1604 */
1605
1606/*
1607 * Some useful tables.
1608 */
1609
1610static struct modmasktable
1611{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001612 short mod_mask; // Bit-mask for particular key modifier
1613 short mod_flag; // Bit(s) for particular key modifier
1614 char_u name; // Single letter name of modifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615} mod_mask_table[] =
1616{
1617 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001618 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
1620 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
1621 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
1622 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
1623 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
Bram Moolenaard0573012017-10-28 21:11:06 +02001624#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
1626#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001627 // 'A' must be the last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
1629 {0, 0, NUL}
Bram Moolenaar85a20022019-12-21 18:25:54 +01001630 // NOTE: when adding an entry, update MAX_KEY_NAME_LEN!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631};
1632
1633/*
1634 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00001635 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 */
1637#define MOD_KEYS_ENTRY_SIZE 5
1638
1639static char_u modifier_keys_table[] =
1640{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001641// mod mask with modifier without modifier
1642 MOD_MASK_SHIFT, '&', '9', '@', '1', // begin
1643 MOD_MASK_SHIFT, '&', '0', '@', '2', // cancel
1644 MOD_MASK_SHIFT, '*', '1', '@', '4', // command
1645 MOD_MASK_SHIFT, '*', '2', '@', '5', // copy
1646 MOD_MASK_SHIFT, '*', '3', '@', '6', // create
1647 MOD_MASK_SHIFT, '*', '4', 'k', 'D', // delete char
1648 MOD_MASK_SHIFT, '*', '5', 'k', 'L', // delete line
1649 MOD_MASK_SHIFT, '*', '7', '@', '7', // end
1650 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', // end
1651 MOD_MASK_SHIFT, '*', '9', '@', '9', // exit
1652 MOD_MASK_SHIFT, '*', '0', '@', '0', // find
1653 MOD_MASK_SHIFT, '#', '1', '%', '1', // help
1654 MOD_MASK_SHIFT, '#', '2', 'k', 'h', // home
1655 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', // home
1656 MOD_MASK_SHIFT, '#', '3', 'k', 'I', // insert
1657 MOD_MASK_SHIFT, '#', '4', 'k', 'l', // left arrow
1658 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', // left arrow
1659 MOD_MASK_SHIFT, '%', 'a', '%', '3', // message
1660 MOD_MASK_SHIFT, '%', 'b', '%', '4', // move
1661 MOD_MASK_SHIFT, '%', 'c', '%', '5', // next
1662 MOD_MASK_SHIFT, '%', 'd', '%', '7', // options
1663 MOD_MASK_SHIFT, '%', 'e', '%', '8', // previous
1664 MOD_MASK_SHIFT, '%', 'f', '%', '9', // print
1665 MOD_MASK_SHIFT, '%', 'g', '%', '0', // redo
1666 MOD_MASK_SHIFT, '%', 'h', '&', '3', // replace
1667 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', // right arr.
1668 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', // right arr.
1669 MOD_MASK_SHIFT, '%', 'j', '&', '5', // resume
1670 MOD_MASK_SHIFT, '!', '1', '&', '6', // save
1671 MOD_MASK_SHIFT, '!', '2', '&', '7', // suspend
1672 MOD_MASK_SHIFT, '!', '3', '&', '8', // undo
1673 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', // up arrow
1674 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', // down arrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaar85a20022019-12-21 18:25:54 +01001676 // vt100 F1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
1678 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
1679 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
1680 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
1681
Bram Moolenaar85a20022019-12-21 18:25:54 +01001682 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', // F1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
1684 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
1685 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
1686 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
1687 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
1688 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
1689 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
1690 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
Bram Moolenaar85a20022019-12-21 18:25:54 +01001691 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', // F10
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
1693 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
1694 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
1695 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
1696 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
1697 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
1698 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
1699 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
1700 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
1701 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
1702 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
1703
1704 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
1705 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
1706 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
1707 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
1708 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
1709 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
1710 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
1711 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
1712 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
1713 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
1714
1715 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
1716 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
1717 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
1718 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
1719 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
1720 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
1721 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
1722
Bram Moolenaar85a20022019-12-21 18:25:54 +01001723 // TAB pseudo code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
1725
1726 NUL
1727};
1728
1729static struct key_name_entry
1730{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001731 int key; // Special key code or ascii value
1732 char_u *name; // Name of key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733} key_names_table[] =
1734{
1735 {' ', (char_u *)"Space"},
1736 {TAB, (char_u *)"Tab"},
1737 {K_TAB, (char_u *)"Tab"},
1738 {NL, (char_u *)"NL"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001739 {NL, (char_u *)"NewLine"}, // Alternative name
1740 {NL, (char_u *)"LineFeed"}, // Alternative name
1741 {NL, (char_u *)"LF"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 {CAR, (char_u *)"CR"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001743 {CAR, (char_u *)"Return"}, // Alternative name
1744 {CAR, (char_u *)"Enter"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 {K_BS, (char_u *)"BS"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001746 {K_BS, (char_u *)"BackSpace"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 {ESC, (char_u *)"Esc"},
1748 {CSI, (char_u *)"CSI"},
1749 {K_CSI, (char_u *)"xCSI"},
1750 {'|', (char_u *)"Bar"},
1751 {'\\', (char_u *)"Bslash"},
1752 {K_DEL, (char_u *)"Del"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001753 {K_DEL, (char_u *)"Delete"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 {K_KDEL, (char_u *)"kDel"},
1755 {K_UP, (char_u *)"Up"},
1756 {K_DOWN, (char_u *)"Down"},
1757 {K_LEFT, (char_u *)"Left"},
1758 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001759 {K_XUP, (char_u *)"xUp"},
1760 {K_XDOWN, (char_u *)"xDown"},
1761 {K_XLEFT, (char_u *)"xLeft"},
1762 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001763 {K_PS, (char_u *)"PasteStart"},
1764 {K_PE, (char_u *)"PasteEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
1766 {K_F1, (char_u *)"F1"},
1767 {K_F2, (char_u *)"F2"},
1768 {K_F3, (char_u *)"F3"},
1769 {K_F4, (char_u *)"F4"},
1770 {K_F5, (char_u *)"F5"},
1771 {K_F6, (char_u *)"F6"},
1772 {K_F7, (char_u *)"F7"},
1773 {K_F8, (char_u *)"F8"},
1774 {K_F9, (char_u *)"F9"},
1775 {K_F10, (char_u *)"F10"},
1776
1777 {K_F11, (char_u *)"F11"},
1778 {K_F12, (char_u *)"F12"},
1779 {K_F13, (char_u *)"F13"},
1780 {K_F14, (char_u *)"F14"},
1781 {K_F15, (char_u *)"F15"},
1782 {K_F16, (char_u *)"F16"},
1783 {K_F17, (char_u *)"F17"},
1784 {K_F18, (char_u *)"F18"},
1785 {K_F19, (char_u *)"F19"},
1786 {K_F20, (char_u *)"F20"},
1787
1788 {K_F21, (char_u *)"F21"},
1789 {K_F22, (char_u *)"F22"},
1790 {K_F23, (char_u *)"F23"},
1791 {K_F24, (char_u *)"F24"},
1792 {K_F25, (char_u *)"F25"},
1793 {K_F26, (char_u *)"F26"},
1794 {K_F27, (char_u *)"F27"},
1795 {K_F28, (char_u *)"F28"},
1796 {K_F29, (char_u *)"F29"},
1797 {K_F30, (char_u *)"F30"},
1798
1799 {K_F31, (char_u *)"F31"},
1800 {K_F32, (char_u *)"F32"},
1801 {K_F33, (char_u *)"F33"},
1802 {K_F34, (char_u *)"F34"},
1803 {K_F35, (char_u *)"F35"},
1804 {K_F36, (char_u *)"F36"},
1805 {K_F37, (char_u *)"F37"},
1806
1807 {K_XF1, (char_u *)"xF1"},
1808 {K_XF2, (char_u *)"xF2"},
1809 {K_XF3, (char_u *)"xF3"},
1810 {K_XF4, (char_u *)"xF4"},
1811
1812 {K_HELP, (char_u *)"Help"},
1813 {K_UNDO, (char_u *)"Undo"},
1814 {K_INS, (char_u *)"Insert"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001815 {K_INS, (char_u *)"Ins"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {K_KINS, (char_u *)"kInsert"},
1817 {K_HOME, (char_u *)"Home"},
1818 {K_KHOME, (char_u *)"kHome"},
1819 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001820 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 {K_END, (char_u *)"End"},
1822 {K_KEND, (char_u *)"kEnd"},
1823 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001824 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {K_PAGEUP, (char_u *)"PageUp"},
1826 {K_PAGEDOWN, (char_u *)"PageDown"},
1827 {K_KPAGEUP, (char_u *)"kPageUp"},
1828 {K_KPAGEDOWN, (char_u *)"kPageDown"},
1829
1830 {K_KPLUS, (char_u *)"kPlus"},
1831 {K_KMINUS, (char_u *)"kMinus"},
1832 {K_KDIVIDE, (char_u *)"kDivide"},
1833 {K_KMULTIPLY, (char_u *)"kMultiply"},
1834 {K_KENTER, (char_u *)"kEnter"},
1835 {K_KPOINT, (char_u *)"kPoint"},
1836
1837 {K_K0, (char_u *)"k0"},
1838 {K_K1, (char_u *)"k1"},
1839 {K_K2, (char_u *)"k2"},
1840 {K_K3, (char_u *)"k3"},
1841 {K_K4, (char_u *)"k4"},
1842 {K_K5, (char_u *)"k5"},
1843 {K_K6, (char_u *)"k6"},
1844 {K_K7, (char_u *)"k7"},
1845 {K_K8, (char_u *)"k8"},
1846 {K_K9, (char_u *)"k9"},
1847
1848 {'<', (char_u *)"lt"},
1849
1850 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001851#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001853#endif
1854#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001856#endif
1857#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001859#endif
1860#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001862#endif
1863#ifdef FEAT_MOUSE_URXVT
1864 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
1865#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001866 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
Bram Moolenaar21a83bd2021-02-23 19:19:58 +01001867 {K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelease"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
1869 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
1870 {K_LEFTDRAG, (char_u *)"LeftDrag"},
1871 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
1872 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001873 {K_MOUSEMOVE, (char_u *)"MouseMove"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
1875 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
1876 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
1877 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
1878 {K_RIGHTDRAG, (char_u *)"RightDrag"},
1879 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001880 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
1881 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
1882 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
1883 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001884 {K_MOUSEDOWN, (char_u *)"MouseDown"}, // OBSOLETE: Use
1885 {K_MOUSEUP, (char_u *)"MouseUp"}, // ScrollWheelXXX instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {K_X1MOUSE, (char_u *)"X1Mouse"},
1887 {K_X1DRAG, (char_u *)"X1Drag"},
1888 {K_X1RELEASE, (char_u *)"X1Release"},
1889 {K_X2MOUSE, (char_u *)"X2Mouse"},
1890 {K_X2DRAG, (char_u *)"X2Drag"},
1891 {K_X2RELEASE, (char_u *)"X2Release"},
1892 {K_DROP, (char_u *)"Drop"},
1893 {K_ZERO, (char_u *)"Nul"},
1894#ifdef FEAT_EVAL
1895 {K_SNR, (char_u *)"SNR"},
1896#endif
1897 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02001898 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar2f106582019-05-08 21:59:25 +02001899 {K_IGNORE, (char_u *)"Ignore"},
Bram Moolenaar957cf672020-11-12 14:21:06 +01001900 {K_COMMAND, (char_u *)"Cmd"},
Bram Moolenaarccb47a22021-01-21 13:36:43 +01001901 {K_FOCUSGAINED, (char_u *)"FocusGained"},
1902 {K_FOCUSLOST, (char_u *)"FocusLost"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 {0, NULL}
Bram Moolenaar85a20022019-12-21 18:25:54 +01001904 // NOTE: When adding a long name update MAX_KEY_NAME_LEN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905};
1906
K.Takataeeec2542021-06-02 13:28:16 +02001907#define KEY_NAMES_TABLE_LEN ARRAY_LENGTH(key_names_table)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909/*
1910 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
1911 * modifier name ('S' for Shift, 'C' for Ctrl etc).
1912 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001913 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001914name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915{
1916 int i;
1917
1918 c = TOUPPER_ASC(c);
1919 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
1920 if (c == mod_mask_table[i].name)
1921 return mod_mask_table[i].mod_flag;
1922 return 0;
1923}
1924
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925/*
1926 * Check if if there is a special key code for "key" that includes the
1927 * modifiers specified.
1928 */
1929 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001930simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
1932 int i;
1933 int key0;
1934 int key1;
1935
1936 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
1937 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001938 // TAB is a special case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
1940 {
1941 *modifiers &= ~MOD_MASK_SHIFT;
1942 return K_S_TAB;
1943 }
1944 key0 = KEY2TERMCAP0(key);
1945 key1 = KEY2TERMCAP1(key);
1946 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
1947 if (key0 == modifier_keys_table[i + 3]
1948 && key1 == modifier_keys_table[i + 4]
1949 && (*modifiers & modifier_keys_table[i]))
1950 {
1951 *modifiers &= ~modifier_keys_table[i];
1952 return TERMCAP2KEY(modifier_keys_table[i + 1],
1953 modifier_keys_table[i + 2]);
1954 }
1955 }
1956 return key;
1957}
1958
1959/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001960 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001961 */
1962 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001963handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001964{
1965 switch (key)
1966 {
1967 case K_XUP: return K_UP;
1968 case K_XDOWN: return K_DOWN;
1969 case K_XLEFT: return K_LEFT;
1970 case K_XRIGHT: return K_RIGHT;
1971 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001972 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001973 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001974 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001975 case K_XF1: return K_F1;
1976 case K_XF2: return K_F2;
1977 case K_XF3: return K_F3;
1978 case K_XF4: return K_F4;
1979 case K_S_XF1: return K_S_F1;
1980 case K_S_XF2: return K_S_F2;
1981 case K_S_XF3: return K_S_F3;
1982 case K_S_XF4: return K_S_F4;
1983 }
1984 return key;
1985}
1986
1987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 * Return a string which contains the name of the given key when the given
1989 * modifiers are down.
1990 */
1991 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001992get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993{
1994 static char_u string[MAX_KEY_NAME_LEN + 1];
1995
1996 int i, idx;
1997 int table_idx;
1998 char_u *s;
1999
2000 string[0] = '<';
2001 idx = 1;
2002
Bram Moolenaar85a20022019-12-21 18:25:54 +01002003 // Key that stands for a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2005 c = KEY2TERMCAP1(c);
2006
2007 /*
2008 * Translate shifted special keys into unshifted keys and set modifier.
2009 * Same for CTRL and ALT modifiers.
2010 */
2011 if (IS_SPECIAL(c))
2012 {
2013 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2014 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2015 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2016 {
2017 modifiers |= modifier_keys_table[i];
2018 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2019 modifier_keys_table[i + 4]);
2020 break;
2021 }
2022 }
2023
Bram Moolenaar85a20022019-12-21 18:25:54 +01002024 // try to find the key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 table_idx = find_special_key_in_table(c);
2026
2027 /*
2028 * When not a known special key, and not a printable character, try to
2029 * extract modifiers.
2030 */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002031 if (c > 0 && (*mb_char2len)(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
2033 if (table_idx < 0
2034 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2035 && (c & 0x80))
2036 {
2037 c &= 0x7f;
2038 modifiers |= MOD_MASK_ALT;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002039 // try again, to find the un-alted key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 table_idx = find_special_key_in_table(c);
2041 }
2042 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2043 {
2044#ifdef EBCDIC
2045 c = CtrlChar(c);
2046#else
2047 c += '@';
2048#endif
2049 modifiers |= MOD_MASK_CTRL;
2050 }
2051 }
2052
Bram Moolenaar85a20022019-12-21 18:25:54 +01002053 // translate the modifier into a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2055 if ((modifiers & mod_mask_table[i].mod_mask)
2056 == mod_mask_table[i].mod_flag)
2057 {
2058 string[idx++] = mod_mask_table[i].name;
2059 string[idx++] = (char_u)'-';
2060 }
2061
Bram Moolenaar85a20022019-12-21 18:25:54 +01002062 if (table_idx < 0) // unknown special key, may output t_xx
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
2064 if (IS_SPECIAL(c))
2065 {
2066 string[idx++] = 't';
2067 string[idx++] = '_';
2068 string[idx++] = KEY2TERMCAP0(c);
2069 string[idx++] = KEY2TERMCAP1(c);
2070 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002071 // Not a special key, only modifiers, output directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 else
2073 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 if (has_mbyte && (*mb_char2len)(c) > 1)
2075 idx += (*mb_char2bytes)(c, string + idx);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002076 else if (vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 string[idx++] = c;
2078 else
2079 {
2080 s = transchar(c);
2081 while (*s)
2082 string[idx++] = *s++;
2083 }
2084 }
2085 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002086 else // use name of special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
Bram Moolenaar423977d2017-01-22 15:05:12 +01002088 size_t len = STRLEN(key_names_table[table_idx].name);
2089
2090 if (len + idx + 2 <= MAX_KEY_NAME_LEN)
2091 {
2092 STRCPY(string + idx, key_names_table[table_idx].name);
2093 idx += (int)len;
2094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
2096 string[idx++] = '>';
2097 string[idx] = NUL;
2098 return string;
2099}
2100
2101/*
2102 * Try translating a <> name at (*srcp)[] to dst[].
2103 * Return the number of characters added to dst[], zero for no match.
2104 * If there is a match, srcp is advanced to after the <> name.
2105 * dst[] must be big enough to hold the result (up to six characters)!
2106 */
2107 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002108trans_special(
2109 char_u **srcp,
2110 char_u *dst,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002111 int flags, // FSK_ values
2112 int *did_simplify) // FSK_SIMPLIFY and found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 int modifiers = 0;
2115 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002117 key = find_special_key(srcp, &modifiers, flags, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 if (key == 0)
2119 return 0;
2120
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002121 return special_to_buf(key, modifiers, flags & FSK_KEYCODE, dst);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002122}
2123
2124/*
2125 * Put the character sequence for "key" with "modifiers" into "dst" and return
2126 * the resulting length.
2127 * When "keycode" is TRUE prefer key code, e.g. K_DEL instead of DEL.
2128 * The sequence is not NUL terminated.
2129 * This is how characters in a string are encoded.
2130 */
2131 int
2132special_to_buf(int key, int modifiers, int keycode, char_u *dst)
2133{
2134 int dlen = 0;
2135
Bram Moolenaar85a20022019-12-21 18:25:54 +01002136 // Put the appropriate modifier in a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 if (modifiers != 0)
2138 {
2139 dst[dlen++] = K_SPECIAL;
2140 dst[dlen++] = KS_MODIFIER;
2141 dst[dlen++] = modifiers;
2142 }
2143
2144 if (IS_SPECIAL(key))
2145 {
2146 dst[dlen++] = K_SPECIAL;
2147 dst[dlen++] = KEY2TERMCAP0(key);
2148 dst[dlen++] = KEY2TERMCAP1(key);
2149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 else if (has_mbyte && !keycode)
2151 dlen += (*mb_char2bytes)(key, dst + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 else if (keycode)
2153 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2154 else
2155 dst[dlen++] = key;
2156
2157 return dlen;
2158}
2159
2160/*
2161 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2162 * srcp is advanced to after the <> name.
2163 * returns 0 if there is no match.
2164 */
2165 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002166find_special_key(
2167 char_u **srcp,
2168 int *modp,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002169 int flags, // FSK_ values
Bram Moolenaar459fd782019-10-13 16:43:39 +02002170 int *did_simplify) // found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
2172 char_u *last_dash;
2173 char_u *end_of_name;
2174 char_u *src;
2175 char_u *bp;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002176 int in_string = flags & FSK_IN_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 int modifiers;
2178 int bit;
2179 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002180 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002181 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
2183 src = *srcp;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002184 if (src[0] != '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 return 0;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002186 if (src[1] == '*') // <*xxx>: do not simplify
2187 ++src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
Bram Moolenaar85a20022019-12-21 18:25:54 +01002189 // Find end of modifier list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 last_dash = src;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02002191 for (bp = src + 1; *bp == '-' || vim_isNormalIDc(*bp); bp++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
2193 if (*bp == '-')
2194 {
2195 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002196 if (bp[1] != NUL)
2197 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002198 if (has_mbyte)
2199 l = mb_ptr2len(bp + 1);
2200 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002201 l = 1;
Bram Moolenaarc8fd33d2019-08-16 20:33:05 +02002202 // Anything accepted, like <C-?>.
2203 // <C-"> or <M-"> are not special in strings as " is
2204 // the string delimiter. With a backslash it works: <M-\">
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002205 if (!(in_string && bp[1] == '"') && bp[l + 1] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02002206 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002207 else if (in_string && bp[1] == '\\' && bp[2] == '"'
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002208 && bp[3] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002209 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
2212 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002213 bp += 3; // skip t_xx, xx may be '-' or '>'
Bram Moolenaar792826c2011-08-19 22:29:02 +02002214 else if (STRNICMP(bp, "char-", 5) == 0)
2215 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002216 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0, TRUE);
2217 if (l == 0)
2218 {
2219 emsg(_(e_invarg));
2220 return 0;
2221 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02002222 bp += l + 5;
2223 break;
2224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 }
2226
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002227 if (*bp == '>') // found matching '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
2229 end_of_name = bp + 1;
2230
Bram Moolenaar85a20022019-12-21 18:25:54 +01002231 // Which modifiers are given?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 modifiers = 0x0;
2233 for (bp = src + 1; bp < last_dash; bp++)
2234 {
2235 if (*bp != '-')
2236 {
2237 bit = name_to_mod_mask(*bp);
2238 if (bit == 0x0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002239 break; // Illegal modifier name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 modifiers |= bit;
2241 }
2242 }
2243
2244 /*
2245 * Legal modifier name.
2246 */
2247 if (bp >= last_dash)
2248 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002249 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2250 && VIM_ISDIGIT(last_dash[6]))
2251 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002252 // <Char-123> or <Char-033> or <Char-0x33>
Bram Moolenaar459fd782019-10-13 16:43:39 +02002253 vim_str2nr(last_dash + 6, NULL, &l, STR2NR_ALL, NULL,
2254 &n, 0, TRUE);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002255 if (l == 0)
2256 {
2257 emsg(_(e_invarg));
2258 return 0;
2259 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02002260 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002263 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002264 int off = 1;
2265
Bram Moolenaar85a20022019-12-21 18:25:54 +01002266 // Modifier with single letter, or special key name.
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002267 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
2268 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02002269 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002270 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002271 else
Bram Moolenaar792826c2011-08-19 22:29:02 +02002272 l = 1;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002273 if (modifiers != 0 && last_dash[l + off] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002274 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002275 else
2276 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002277 key = get_special_key_code(last_dash + off);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002278 if (!(flags & FSK_KEEP_X_KEY))
Bram Moolenaar792826c2011-08-19 22:29:02 +02002279 key = handle_x_keys(key);
2280 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
2283 /*
2284 * get_special_key_code() may return NUL for invalid
2285 * special key name.
2286 */
2287 if (key != NUL)
2288 {
2289 /*
2290 * Only use a modifier when there is no special key code that
2291 * includes the modifier.
2292 */
2293 key = simplify_key(key, &modifiers);
2294
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002295 if (!(flags & FSK_KEYCODE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002297 // don't want keycode, use single byte code
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (key == K_BS)
2299 key = BS;
2300 else if (key == K_DEL || key == K_KDEL)
2301 key = DEL;
2302 }
2303
Bram Moolenaar459fd782019-10-13 16:43:39 +02002304 // Normal Key with modifier: Try to make a single byte code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 if (!IS_SPECIAL(key))
Bram Moolenaar459fd782019-10-13 16:43:39 +02002306 key = extract_modifiers(key, &modifiers,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002307 flags & FSK_SIMPLIFY, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
2309 *modp = modifiers;
2310 *srcp = end_of_name;
2311 return key;
2312 }
2313 }
2314 }
2315 return 0;
2316}
2317
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002318
2319/*
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02002320 * Some keys are used with Ctrl without Shift and are still expected to be
2321 * mapped as if Shift was pressed:
2322 * CTRL-2 is CTRL-@
2323 * CTRL-6 is CTRL-^
2324 * CTRL-- is CTRL-_
2325 * Also, <C-H> and <C-h> mean the same thing, always use "H".
2326 * Returns the possibly adjusted key.
2327 */
2328 int
2329may_adjust_key_for_ctrl(int modifiers, int key)
2330{
2331 if (modifiers & MOD_MASK_CTRL)
2332 {
2333 if (ASCII_ISALPHA(key))
2334 return TOUPPER_ASC(key);
2335 if (key == '2')
2336 return '@';
2337 if (key == '6')
2338 return '^';
2339 if (key == '-')
2340 return '_';
2341 }
2342 return key;
2343}
2344
2345/*
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002346 * Some keys already have Shift included, pass them as normal keys.
Bram Moolenaar9a033d72020-10-07 17:29:48 +02002347 * When Ctrl is also used <C-H> and <C-S-H> are different, but <C-S-{> should
2348 * be <C-{>. Same for <C-S-}> and <C-S-|>.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002349 * Also for <A-S-a> and <M-S-a>.
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02002350 * This includes all printable ASCII characters except numbers and a-z.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002351 */
2352 int
2353may_remove_shift_modifier(int modifiers, int key)
2354{
2355 if ((modifiers == MOD_MASK_SHIFT
2356 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
2357 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02002358 && ((key >= '!' && key <= '/')
2359 || (key >= ':' && key <= 'Z')
2360 || (key >= '[' && key <= '`')
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002361 || (key >= '{' && key <= '~')))
2362 return modifiers & ~MOD_MASK_SHIFT;
Bram Moolenaar9a033d72020-10-07 17:29:48 +02002363
2364 if (modifiers == (MOD_MASK_SHIFT | MOD_MASK_CTRL)
2365 && (key == '{' || key == '}' || key == '|'))
2366 return modifiers & ~MOD_MASK_SHIFT;
2367
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002368 return modifiers;
2369}
2370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371/*
2372 * Try to include modifiers in the key.
2373 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
Bram Moolenaar459fd782019-10-13 16:43:39 +02002374 * When "simplify" is FALSE don't do Ctrl and Alt.
2375 * When "simplify" is TRUE and Ctrl or Alt is removed from modifiers set
2376 * "did_simplify" when it's not NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 */
2378 int
Bram Moolenaar459fd782019-10-13 16:43:39 +02002379extract_modifiers(int key, int *modp, int simplify, int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
2381 int modifiers = *modp;
2382
Bram Moolenaard0573012017-10-28 21:11:06 +02002383#ifdef MACOS_X
Bram Moolenaar459fd782019-10-13 16:43:39 +02002384 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 if (!(modifiers & MOD_MASK_CMD))
2386#endif
2387 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2388 {
2389 key = TOUPPER_ASC(key);
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002390 // With <C-S-a> we keep the shift modifier.
2391 // With <S-a>, <A-S-a> and <S-A> we don't keep the shift modifier.
2392 if (simplify || modifiers == MOD_MASK_SHIFT
2393 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
2394 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02002395 modifiers &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02002397
2398 // <C-H> and <C-h> mean the same thing, always use "H"
2399 if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key))
2400 key = TOUPPER_ASC(key);
2401
2402 if (simplify && (modifiers & MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403#ifdef EBCDIC
Bram Moolenaar459fd782019-10-13 16:43:39 +02002404 // TODO: EBCDIC Better use:
2405 // && (Ctrl_chr(key) || key == '?')
2406 // ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2408 != NULL
2409#else
2410 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2411#endif
2412 )
2413 {
2414 key = Ctrl_chr(key);
2415 modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002416 // <C-@> is <Nul>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (key == 0)
2418 key = K_ZERO;
Bram Moolenaar459fd782019-10-13 16:43:39 +02002419 if (did_simplify != NULL)
2420 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02002422
Bram Moolenaard0573012017-10-28 21:11:06 +02002423#ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01002424 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 if (!(modifiers & MOD_MASK_CMD))
2426#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +02002427 if (simplify && (modifiers & MOD_MASK_ALT) && key < 0x80
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002428 && !enc_dbcs) // avoid creating a lead byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
2430 key |= 0x80;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002431 modifiers &= ~MOD_MASK_ALT; // remove the META modifier
Bram Moolenaar459fd782019-10-13 16:43:39 +02002432 if (did_simplify != NULL)
2433 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435
2436 *modp = modifiers;
2437 return key;
2438}
2439
2440/*
2441 * Try to find key "c" in the special key table.
2442 * Return the index when found, -1 when not found.
2443 */
2444 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002445find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446{
2447 int i;
2448
2449 for (i = 0; key_names_table[i].name != NULL; i++)
2450 if (c == key_names_table[i].key)
2451 break;
2452 if (key_names_table[i].name == NULL)
2453 i = -1;
2454 return i;
2455}
2456
2457/*
2458 * Find the special key with the given name (the given string does not have to
2459 * end with NUL, the name is assumed to end before the first non-idchar).
2460 * If the name starts with "t_" the next two characters are interpreted as a
2461 * termcap name.
2462 * Return the key code, or 0 if not found.
2463 */
2464 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002465get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 char_u *table_name;
2468 char_u string[3];
2469 int i, j;
2470
2471 /*
2472 * If it's <t_xx> we get the code for xx from the termcap
2473 */
2474 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
2475 {
2476 string[0] = name[2];
2477 string[1] = name[3];
2478 string[2] = NUL;
2479 if (add_termcap_entry(string, FALSE) == OK)
2480 return TERMCAP2KEY(name[2], name[3]);
2481 }
2482 else
2483 for (i = 0; key_names_table[i].name != NULL; i++)
2484 {
2485 table_name = key_names_table[i].name;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02002486 for (j = 0; vim_isNormalIDc(name[j]) && table_name[j] != NUL; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
2488 break;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02002489 if (!vim_isNormalIDc(name[j]) && table_name[j] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 return key_names_table[i].key;
2491 }
2492 return 0;
2493}
2494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002496get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00002498 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 return NULL;
2500 return key_names_table[i].name;
2501}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503/*
2504 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
2505 */
2506 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002507get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509 int c = *buf->b_p_ff;
2510
2511 if (buf->b_p_bin || c == 'u')
2512 return EOL_UNIX;
2513 if (c == 'm')
2514 return EOL_MAC;
2515 return EOL_DOS;
2516}
2517
2518/*
2519 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
2520 * argument.
2521 */
2522 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002523get_fileformat_force(
2524 buf_T *buf,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002525 exarg_T *eap) // can be NULL!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526{
2527 int c;
2528
2529 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002530 c = eap->force_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 else
2532 {
2533 if ((eap != NULL && eap->force_bin != 0)
2534 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
2535 return EOL_UNIX;
2536 c = *buf->b_p_ff;
2537 }
2538 if (c == 'u')
2539 return EOL_UNIX;
2540 if (c == 'm')
2541 return EOL_MAC;
2542 return EOL_DOS;
2543}
2544
2545/*
2546 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
2547 * Sets both 'textmode' and 'fileformat'.
2548 * Note: Does _not_ set global value of 'textmode'!
2549 */
2550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002551set_fileformat(
2552 int t,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002553 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 char *p = NULL;
2556
2557 switch (t)
2558 {
2559 case EOL_DOS:
2560 p = FF_DOS;
2561 curbuf->b_p_tx = TRUE;
2562 break;
2563 case EOL_UNIX:
2564 p = FF_UNIX;
2565 curbuf->b_p_tx = FALSE;
2566 break;
2567 case EOL_MAC:
2568 p = FF_MAC;
2569 curbuf->b_p_tx = FALSE;
2570 break;
2571 }
2572 if (p != NULL)
2573 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002574 OPT_FREE | opt_flags, 0);
2575
Bram Moolenaar85a20022019-12-21 18:25:54 +01002576 // This may cause the buffer to become (un)modified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002578 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579#ifdef FEAT_TITLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01002580 need_maketitle = TRUE; // set window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581#endif
2582}
2583
2584/*
2585 * Return the default fileformat from 'fileformats'.
2586 */
2587 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002588default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589{
2590 switch (*p_ffs)
2591 {
2592 case 'm': return EOL_MAC;
2593 case 'd': return EOL_DOS;
2594 }
2595 return EOL_UNIX;
2596}
2597
2598/*
2599 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
2600 */
2601 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002602call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603{
2604 char_u *ncmd;
2605 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002606#ifdef FEAT_PROFILE
2607 proftime_T wait_time;
2608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
2610 if (p_verbose > 3)
2611 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002612 verbose_enter();
Bram Moolenaar06f08532020-05-12 23:45:16 +02002613 smsg(_("Calling shell to execute: \"%s\""), cmd == NULL ? p_sh : cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 out_char('\n');
2615 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002616 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
2618
Bram Moolenaar05159a02005-02-26 23:04:13 +00002619#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00002620 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002621 prof_child_enter(&wait_time);
2622#endif
2623
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (*p_sh == NUL)
2625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002626 emsg(_(e_shellempty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 retval = -1;
2628 }
2629 else
2630 {
2631#ifdef FEAT_GUI_MSWIN
Bram Moolenaar85a20022019-12-21 18:25:54 +01002632 // Don't hide the pointer while executing a shell command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 gui_mch_mousehide(FALSE);
2634#endif
2635#ifdef FEAT_GUI
2636 ++hold_gui_events;
2637#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002638 // The external command may update a tags file, clear cached tags.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 tag_freematch();
2640
Bram Moolenaar01257a72019-06-10 14:46:04 +02002641 if (cmd == NULL || *p_sxq == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 retval = mch_call_shell(cmd, opt);
2643 else
2644 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01002645 char_u *ecmd = cmd;
2646
Bram Moolenaar1a613392019-09-28 15:51:37 +02002647 if (*p_sxe != NUL && *p_sxq == '(')
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01002648 {
2649 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
2650 if (ecmd == NULL)
2651 ecmd = cmd;
2652 }
Bram Moolenaar964b3742019-05-24 18:54:09 +02002653 ncmd = alloc(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 if (ncmd != NULL)
2655 {
2656 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01002657 STRCAT(ncmd, ecmd);
Bram Moolenaar1a613392019-09-28 15:51:37 +02002658 // When 'shellxquote' is ( append ).
2659 // When 'shellxquote' is "( append )".
2660 STRCAT(ncmd, *p_sxq == '(' ? (char_u *)")"
2661 : *p_sxq == '"' && *(p_sxq+1) == '(' ? (char_u *)")\""
2662 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 retval = mch_call_shell(ncmd, opt);
2664 vim_free(ncmd);
2665 }
2666 else
2667 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01002668 if (ecmd != cmd)
2669 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
2671#ifdef FEAT_GUI
2672 --hold_gui_events;
2673#endif
2674 /*
2675 * Check the window size, in case it changed while executing the
2676 * external command.
2677 */
2678 shell_resized_check();
2679 }
2680
2681#ifdef FEAT_EVAL
2682 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002683# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00002684 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002685 prof_child_exit(&wait_time);
2686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687#endif
2688
2689 return retval;
2690}
2691
2692/*
Bram Moolenaar01265852006-03-20 21:50:15 +00002693 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
2694 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 */
2696 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002697get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
2699 if (State & NORMAL)
2700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00002702 {
2703 if (VIsual_select)
2704 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00002706 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002707 else if (finish_op)
2708 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 }
2710 return State;
2711}
2712
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002713/*
2714 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02002715 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002716 * "b" must point to the start of the file name
2717 */
2718 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002719after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002720{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02002721 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002722 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
2723}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002724
2725/*
2726 * Return TRUE if file names "f1" and "f2" are in the same directory.
2727 * "f1" may be a short name, "f2" must be a full path.
2728 */
2729 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002730same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002731{
2732 char_u ffname[MAXPATHL];
2733 char_u *t1;
2734 char_u *t2;
2735
Bram Moolenaar85a20022019-12-21 18:25:54 +01002736 // safety check
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002737 if (f1 == NULL || f2 == NULL)
2738 return FALSE;
2739
2740 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
2741 t1 = gettail_sep(ffname);
2742 t2 = gettail_sep(f2);
2743 return (t1 - ffname == t2 - f2
2744 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
2745}
2746
Bram Moolenaar7c365fb2018-06-29 20:28:31 +02002747#if defined(FEAT_SESSION) || defined(FEAT_AUTOCHDIR) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002748 || defined(MSWIN) || defined(FEAT_GUI_GTK) \
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01002749 || defined(FEAT_NETBEANS_INTG) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 || defined(PROTO)
2751/*
2752 * Change to a file's directory.
2753 * Caller must call shorten_fnames()!
2754 * Return OK or FAIL.
2755 */
2756 int
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002757vim_chdirfile(char_u *fname, char *trigger_autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002759 char_u old_dir[MAXPATHL];
2760 char_u new_dir[MAXPATHL];
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01002761 int res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002763 if (mch_dirname(old_dir, MAXPATHL) != OK)
2764 *old_dir = NUL;
2765
2766 vim_strncpy(new_dir, fname, MAXPATHL - 1);
2767 *gettail_sep(new_dir) = NUL;
2768
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01002769 if (pathcmp((char *)old_dir, (char *)new_dir, -1) == 0)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002770 // nothing to do
2771 res = OK;
2772 else
2773 {
2774 res = mch_chdir((char *)new_dir) == 0 ? OK : FAIL;
2775
2776 if (res == OK && trigger_autocmd != NULL)
2777 apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd,
2778 new_dir, FALSE, curbuf);
2779 }
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01002780 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781}
2782#endif
2783
2784#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
2785/*
2786 * Check if "name" ends in a slash and is not a directory.
2787 * Used for systems where stat() ignores a trailing slash on a file name.
2788 * The Vim code assumes a trailing slash is only ignored for a directory.
2789 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002790 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01002791illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792{
2793 if (name[0] == NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002794 return FALSE; // no file name is not illegal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (name[strlen(name) - 1] != '/')
Bram Moolenaar85a20022019-12-21 18:25:54 +01002796 return FALSE; // no trailing slash
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (mch_isdir((char_u *)name))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002798 return FALSE; // trailing slash for a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 return TRUE;
2800}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002801
2802/*
2803 * Special implementation of mch_stat() for Solaris.
2804 */
2805 int
2806vim_stat(const char *name, stat_T *stp)
2807{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002808 // On Solaris stat() accepts "file/" as if it was "file". Return -1 if
2809 // the name ends in "/" and it's not a directory.
Bram Moolenaard8492792017-03-16 12:22:38 +01002810 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002811}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812#endif
2813
2814#if defined(CURSOR_SHAPE) || defined(PROTO)
2815
2816/*
2817 * Handling of cursor and mouse pointer shapes in various modes.
2818 */
2819
2820cursorentry_T shape_table[SHAPE_IDX_COUNT] =
2821{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002822 // The values will be filled in from the 'guicursor' and 'mouseshape'
2823 // defaults when Vim starts.
2824 // Adjust the SHAPE_IDX_ defines when making changes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
2826 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
2827 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
2828 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
2829 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
2830 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
2831 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
2832 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
2833 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
2834 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
2835 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
2836 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
2837 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
2838 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
2839 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
2840 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
2841 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
2842};
2843
2844#ifdef FEAT_MOUSESHAPE
2845/*
2846 * Table with names for mouse shapes. Keep in sync with all the tables for
2847 * mch_set_mouse_shape()!.
2848 */
2849static char * mshape_names[] =
2850{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002851 "arrow", // default, must be the first one
2852 "blank", // hidden
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 "beam",
2854 "updown",
2855 "udsizing",
2856 "leftright",
2857 "lrsizing",
2858 "busy",
2859 "no",
2860 "crosshair",
2861 "hand1",
2862 "hand2",
2863 "pencil",
2864 "question",
2865 "rightup-arrow",
2866 "up-arrow",
2867 NULL
2868};
2869#endif
2870
2871/*
2872 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
2873 * ("what" is SHAPE_MOUSE).
2874 * Returns error message for an illegal option, NULL otherwise.
2875 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002876 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002877parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
2879 char_u *modep;
2880 char_u *colonp;
2881 char_u *commap;
2882 char_u *slashp;
2883 char_u *p, *endp;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002884 int idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 int all_idx;
2886 int len;
2887 int i;
2888 long n;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002889 int found_ve = FALSE; // found "ve" flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 int round;
2891
2892 /*
2893 * First round: check for errors; second round: do it for real.
2894 */
2895 for (round = 1; round <= 2; ++round)
2896 {
2897 /*
2898 * Repeat for all comma separated parts.
2899 */
2900#ifdef FEAT_MOUSESHAPE
2901 if (what == SHAPE_MOUSE)
2902 modep = p_mouseshape;
2903 else
2904#endif
2905 modep = p_guicursor;
2906 while (*modep != NUL)
2907 {
2908 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01002909 commap = vim_strchr(modep, ',');
2910
2911 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002912 return N_("E545: Missing colon");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 if (colonp == modep)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002914 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915
2916 /*
2917 * Repeat for all mode's before the colon.
2918 * For the 'a' mode, we loop to handle all the modes.
2919 */
2920 all_idx = -1;
2921 while (modep < colonp || all_idx >= 0)
2922 {
2923 if (all_idx < 0)
2924 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002925 // Find the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 if (modep[1] == '-' || modep[1] == ':')
2927 len = 1;
2928 else
2929 len = 2;
2930 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
2931 all_idx = SHAPE_IDX_COUNT - 1;
2932 else
2933 {
2934 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
2935 if (STRNICMP(modep, shape_table[idx].name, len)
2936 == 0)
2937 break;
2938 if (idx == SHAPE_IDX_COUNT
2939 || (shape_table[idx].used_for & what) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002940 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
2942 found_ve = TRUE;
2943 }
2944 modep += len + 1;
2945 }
2946
2947 if (all_idx >= 0)
2948 idx = all_idx--;
2949 else if (round == 2)
2950 {
2951#ifdef FEAT_MOUSESHAPE
2952 if (what == SHAPE_MOUSE)
2953 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002954 // Set the default, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 shape_table[idx].mshape = 0;
2956 }
2957 else
2958#endif
2959 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002960 // Set the defaults, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 shape_table[idx].shape = SHAPE_BLOCK;
2962 shape_table[idx].blinkwait = 700L;
2963 shape_table[idx].blinkon = 400L;
2964 shape_table[idx].blinkoff = 250L;
2965 }
2966 }
2967
Bram Moolenaar85a20022019-12-21 18:25:54 +01002968 // Parse the part after the colon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 for (p = colonp + 1; *p && *p != ','; )
2970 {
2971#ifdef FEAT_MOUSESHAPE
2972 if (what == SHAPE_MOUSE)
2973 {
2974 for (i = 0; ; ++i)
2975 {
2976 if (mshape_names[i] == NULL)
2977 {
2978 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002979 return N_("E547: Illegal mouseshape");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 if (round == 2)
2981 shape_table[idx].mshape =
2982 getdigits(&p) + MSHAPE_NUMBERED;
2983 else
2984 (void)getdigits(&p);
2985 break;
2986 }
2987 len = (int)STRLEN(mshape_names[i]);
2988 if (STRNICMP(p, mshape_names[i], len) == 0)
2989 {
2990 if (round == 2)
2991 shape_table[idx].mshape = i;
2992 p += len;
2993 break;
2994 }
2995 }
2996 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002997 else // if (what == SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998#endif
2999 {
3000 /*
3001 * First handle the ones with a number argument.
3002 */
3003 i = *p;
3004 len = 0;
3005 if (STRNICMP(p, "ver", 3) == 0)
3006 len = 3;
3007 else if (STRNICMP(p, "hor", 3) == 0)
3008 len = 3;
3009 else if (STRNICMP(p, "blinkwait", 9) == 0)
3010 len = 9;
3011 else if (STRNICMP(p, "blinkon", 7) == 0)
3012 len = 7;
3013 else if (STRNICMP(p, "blinkoff", 8) == 0)
3014 len = 8;
3015 if (len != 0)
3016 {
3017 p += len;
3018 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003019 return N_("E548: digit expected");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 n = getdigits(&p);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003021 if (len == 3) // "ver" or "hor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 {
3023 if (n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003024 return N_("E549: Illegal percentage");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 if (round == 2)
3026 {
3027 if (TOLOWER_ASC(i) == 'v')
3028 shape_table[idx].shape = SHAPE_VER;
3029 else
3030 shape_table[idx].shape = SHAPE_HOR;
3031 shape_table[idx].percentage = n;
3032 }
3033 }
3034 else if (round == 2)
3035 {
3036 if (len == 9)
3037 shape_table[idx].blinkwait = n;
3038 else if (len == 7)
3039 shape_table[idx].blinkon = n;
3040 else
3041 shape_table[idx].blinkoff = n;
3042 }
3043 }
3044 else if (STRNICMP(p, "block", 5) == 0)
3045 {
3046 if (round == 2)
3047 shape_table[idx].shape = SHAPE_BLOCK;
3048 p += 5;
3049 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003050 else // must be a highlight group name then
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 {
3052 endp = vim_strchr(p, '-');
Bram Moolenaar85a20022019-12-21 18:25:54 +01003053 if (commap == NULL) // last part
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 {
3055 if (endp == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003056 endp = p + STRLEN(p); // find end of part
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 }
3058 else if (endp > commap || endp == NULL)
3059 endp = commap;
3060 slashp = vim_strchr(p, '/');
3061 if (slashp != NULL && slashp < endp)
3062 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003063 // "group/langmap_group"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 i = syn_check_group(p, (int)(slashp - p));
3065 p = slashp + 1;
3066 }
3067 if (round == 2)
3068 {
3069 shape_table[idx].id = syn_check_group(p,
3070 (int)(endp - p));
3071 shape_table[idx].id_lm = shape_table[idx].id;
3072 if (slashp != NULL && slashp < endp)
3073 shape_table[idx].id = i;
3074 }
3075 p = endp;
3076 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003077 } // if (what != SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
3079 if (*p == '-')
3080 ++p;
3081 }
3082 }
3083 modep = p;
3084 if (*modep == ',')
3085 ++modep;
3086 }
3087 }
3088
Bram Moolenaar85a20022019-12-21 18:25:54 +01003089 // If the 's' flag is not given, use the 'v' cursor for 's'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 if (!found_ve)
3091 {
3092#ifdef FEAT_MOUSESHAPE
3093 if (what == SHAPE_MOUSE)
3094 {
3095 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3096 }
3097 else
3098#endif
3099 {
3100 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3101 shape_table[SHAPE_IDX_VE].percentage =
3102 shape_table[SHAPE_IDX_V].percentage;
3103 shape_table[SHAPE_IDX_VE].blinkwait =
3104 shape_table[SHAPE_IDX_V].blinkwait;
3105 shape_table[SHAPE_IDX_VE].blinkon =
3106 shape_table[SHAPE_IDX_V].blinkon;
3107 shape_table[SHAPE_IDX_VE].blinkoff =
3108 shape_table[SHAPE_IDX_V].blinkoff;
3109 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3110 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3111 }
3112 }
3113
3114 return NULL;
3115}
3116
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003117# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3118 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119/*
3120 * Return the index into shape_table[] for the current mode.
3121 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3122 */
3123 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003124get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
3126#ifdef FEAT_MOUSESHAPE
3127 if (mouse && (State == HITRETURN || State == ASKMORE))
3128 {
3129# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003130 int x, y;
3131 gui_mch_getmouse(&x, &y);
3132 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 return SHAPE_IDX_MOREL;
3134# endif
3135 return SHAPE_IDX_MORE;
3136 }
3137 if (mouse && drag_status_line)
3138 return SHAPE_IDX_SDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 if (mouse && drag_sep_line)
3140 return SHAPE_IDX_VDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141#endif
3142 if (!mouse && State == SHOWMATCH)
3143 return SHAPE_IDX_SM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 if (State & VREPLACE_FLAG)
3145 return SHAPE_IDX_R;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 if (State & REPLACE_FLAG)
3147 return SHAPE_IDX_R;
3148 if (State & INSERT)
3149 return SHAPE_IDX_I;
3150 if (State & CMDLINE)
3151 {
3152 if (cmdline_at_end())
3153 return SHAPE_IDX_C;
3154 if (cmdline_overstrike())
3155 return SHAPE_IDX_CR;
3156 return SHAPE_IDX_CI;
3157 }
3158 if (finish_op)
3159 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 if (VIsual_active)
3161 {
3162 if (*p_sel == 'e')
3163 return SHAPE_IDX_VE;
3164 else
3165 return SHAPE_IDX_V;
3166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 return SHAPE_IDX_N;
3168}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3172static int old_mouse_shape = 0;
3173
3174/*
3175 * Set the mouse shape:
3176 * If "shape" is -1, use shape depending on the current mode,
3177 * depending on the current state.
3178 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3179 * when the mouse moves off the status or command line).
3180 */
3181 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003182update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
3184 int new_mouse_shape;
3185
Bram Moolenaar85a20022019-12-21 18:25:54 +01003186 // Only works in GUI mode.
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003187 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 return;
3189
Bram Moolenaar85a20022019-12-21 18:25:54 +01003190 // Postpone the updating when more is to come. Speeds up executing of
3191 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 if (shape_idx == -1 && char_avail())
3193 {
3194 postponed_mouseshape = TRUE;
3195 return;
3196 }
3197
Bram Moolenaar85a20022019-12-21 18:25:54 +01003198 // When ignoring the mouse don't change shape on the statusline.
Bram Moolenaar14716812006-05-04 21:54:08 +00003199 if (*p_mouse == NUL
3200 && (shape_idx == SHAPE_IDX_CLINE
3201 || shape_idx == SHAPE_IDX_STATUS
3202 || shape_idx == SHAPE_IDX_VSEP))
3203 shape_idx = -2;
3204
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 if (shape_idx == -2
3206 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3207 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3208 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3209 return;
3210 if (shape_idx < 0)
3211 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3212 else
3213 new_mouse_shape = shape_table[shape_idx].mshape;
3214 if (new_mouse_shape != old_mouse_shape)
3215 {
3216 mch_set_mouse_shape(new_mouse_shape);
3217 old_mouse_shape = new_mouse_shape;
3218 }
3219 postponed_mouseshape = FALSE;
3220}
3221# endif
3222
Bram Moolenaar85a20022019-12-21 18:25:54 +01003223#endif // CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224
3225
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226/*
3227 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
3228 * 'cdpath' for relative directory names, otherwise just mch_chdir().
3229 */
3230 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003231vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
3233#ifndef FEAT_SEARCHPATH
3234 return mch_chdir((char *)new_dir);
3235#else
3236 char_u *dir_name;
3237 int r;
3238
3239 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
3240 FNAME_MESS, curbuf->b_ffname);
3241 if (dir_name == NULL)
3242 return -1;
3243 r = mch_chdir((char *)dir_name);
3244 vim_free(dir_name);
3245 return r;
3246#endif
3247}
3248
3249/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003250 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003252 * Some systems are quite slow in obtaining the user name (Windows NT), thus
3253 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 * Returns OK or FAIL.
3255 */
3256 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003257get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003259 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 {
3261 if (mch_get_user_name(buf, len) == FAIL)
3262 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003263 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 }
3265 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003266 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 return OK;
3268}
3269
3270#ifndef HAVE_QSORT
3271/*
3272 * Our own qsort(), for systems that don't have it.
3273 * It's simple and slow. From the K&R C book.
3274 */
3275 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003276qsort(
3277 void *base,
3278 size_t elm_count,
3279 size_t elm_size,
3280 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281{
3282 char_u *buf;
3283 char_u *p1;
3284 char_u *p2;
3285 int i, j;
3286 int gap;
3287
Bram Moolenaar964b3742019-05-24 18:54:09 +02003288 buf = alloc(elm_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (buf == NULL)
3290 return;
3291
3292 for (gap = elm_count / 2; gap > 0; gap /= 2)
3293 for (i = gap; i < elm_count; ++i)
3294 for (j = i - gap; j >= 0; j -= gap)
3295 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003296 // Compare the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 p1 = (char_u *)base + j * elm_size;
3298 p2 = (char_u *)base + (j + gap) * elm_size;
3299 if ((*cmp)((void *)p1, (void *)p2) <= 0)
3300 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003301 // Exchange the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 mch_memmove(buf, p1, elm_size);
3303 mch_memmove(p1, p2, elm_size);
3304 mch_memmove(p2, buf, elm_size);
3305 }
3306
3307 vim_free(buf);
3308}
3309#endif
3310
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 * The putenv() implementation below comes from the "screen" program.
3313 * Included with permission from Juergen Weigert.
3314 * See pty.c for the copyright notice.
3315 */
3316
3317/*
3318 * putenv -- put value into environment
3319 *
3320 * Usage: i = putenv (string)
3321 * int i;
3322 * char *string;
3323 *
3324 * where string is of the form <name>=<value>.
3325 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
3326 *
3327 * Putenv may need to add a new name into the environment, or to
3328 * associate a value longer than the current value with a particular
3329 * name. So, to make life simpler, putenv() copies your entire
3330 * environment into the heap (i.e. malloc()) from the stack
3331 * (i.e. where it resides when your process is initiated) the first
3332 * time you call it.
3333 *
3334 * (history removed, not very interesting. See the "screen" sources.)
3335 */
3336
3337#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
3338
Bram Moolenaar85a20022019-12-21 18:25:54 +01003339#define EXTRASIZE 5 // increment to add to env. size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar85a20022019-12-21 18:25:54 +01003341static int envsize = -1; // current size of environment
3342extern char **environ; // the global which is your env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
Bram Moolenaar85a20022019-12-21 18:25:54 +01003344static int findenv(char *name); // look for a name in the env.
3345static int newenv(void); // copy env. from stack to heap
3346static int moreenv(void); // incr. size of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
3348 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003349putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351 int i;
3352 char *p;
3353
3354 if (envsize < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003355 { // first time putenv called
3356 if (newenv() < 0) // copy env. to heap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 return -1;
3358 }
3359
Bram Moolenaar85a20022019-12-21 18:25:54 +01003360 i = findenv((char *)string); // look for name in environment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
3362 if (i < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003363 { // name must be added
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 for (i = 0; environ[i]; i++);
3365 if (i >= (envsize - 1))
Bram Moolenaar85a20022019-12-21 18:25:54 +01003366 { // need new slot
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 if (moreenv() < 0)
3368 return -1;
3369 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003370 p = alloc(strlen(string) + 1);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003371 if (p == NULL) // not enough core
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 return -1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003373 environ[i + 1] = 0; // new end of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
3375 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003376 { // name already in env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 p = vim_realloc(environ[i], strlen(string) + 1);
3378 if (p == NULL)
3379 return -1;
3380 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003381 sprintf(p, "%s", string); // copy into env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 environ[i] = p;
3383
3384 return 0;
3385}
3386
3387 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003388findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389{
3390 char *namechar, *envchar;
3391 int i, found;
3392
3393 found = 0;
3394 for (i = 0; environ[i] && !found; i++)
3395 {
3396 envchar = environ[i];
3397 namechar = name;
3398 while (*namechar && *namechar != '=' && (*namechar == *envchar))
3399 {
3400 namechar++;
3401 envchar++;
3402 }
3403 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
3404 }
3405 return found ? i - 1 : -1;
3406}
3407
3408 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003409newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410{
3411 char **env, *elem;
3412 int i, esize;
3413
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 for (i = 0; environ[i]; i++)
3415 ;
Bram Moolenaard0573012017-10-28 21:11:06 +02003416
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 esize = i + EXTRASIZE + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003418 env = ALLOC_MULT(char *, esize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 if (env == NULL)
3420 return -1;
3421
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 for (i = 0; environ[i]; i++)
3423 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003424 elem = alloc(strlen(environ[i]) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 if (elem == NULL)
3426 return -1;
3427 env[i] = elem;
3428 strcpy(elem, environ[i]);
3429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431 env[i] = 0;
3432 environ = env;
3433 envsize = esize;
3434 return 0;
3435}
3436
3437 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003438moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
3440 int esize;
3441 char **env;
3442
3443 esize = envsize + EXTRASIZE;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003444 env = vim_realloc((char *)environ, esize * sizeof (*env));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 if (env == 0)
3446 return -1;
3447 environ = env;
3448 envsize = esize;
3449 return 0;
3450}
3451
3452# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02003453/*
3454 * Used for mch_getenv() for Mac.
3455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003457vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
3459 int i;
3460 char_u *p;
3461
3462 if (envsize < 0)
3463 return NULL;
3464
3465 i = findenv((char *)string);
3466
3467 if (i < 0)
3468 return NULL;
3469
3470 p = vim_strchr((char_u *)environ[i], '=');
3471 return (p + 1);
3472}
3473# endif
3474
Bram Moolenaar85a20022019-12-21 18:25:54 +01003475#endif // !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003476
Bram Moolenaarc4956c82006-03-12 21:58:43 +00003477#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003478/*
3479 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3480 * rights to write into.
3481 */
3482 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003483filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003484{
3485 int retval = 0;
3486#if defined(UNIX) || defined(VMS)
3487 int perm = 0;
3488#endif
3489
3490#if defined(UNIX) || defined(VMS)
3491 perm = mch_getperm(fname);
3492#endif
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003493 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01003494# ifdef MSWIN
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003495 mch_writable(fname) &&
3496# else
3497# if defined(UNIX) || defined(VMS)
3498 (perm & 0222) &&
3499# endif
3500# endif
3501 mch_access((char *)fname, W_OK) == 0
3502 )
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003503 {
3504 ++retval;
3505 if (mch_isdir(fname))
3506 ++retval;
3507 }
3508 return retval;
3509}
3510#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00003511
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003512#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
3513/*
3514 * Read 2 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003515 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003516 */
3517 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003518get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003519{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003520 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003521
3522 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003523 if (n == EOF) return -1;
3524 c = getc(fd);
3525 if (c == EOF) return -1;
3526 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003527}
3528
3529/*
3530 * Read 3 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003531 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003532 */
3533 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003534get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003535{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003536 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003537
3538 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003539 if (n == EOF) return -1;
3540 c = getc(fd);
3541 if (c == EOF) return -1;
3542 n = (n << 8) + c;
3543 c = getc(fd);
3544 if (c == EOF) return -1;
3545 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003546}
3547
3548/*
3549 * Read 4 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003550 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003551 */
3552 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003553get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003554{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003555 int c;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003556 // Use unsigned rather than int otherwise result is undefined
3557 // when left-shift sets the MSB.
Bram Moolenaar95235e62013-09-08 16:07:07 +02003558 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003559
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003560 c = getc(fd);
3561 if (c == EOF) return -1;
3562 n = (unsigned)c;
3563 c = getc(fd);
3564 if (c == EOF) return -1;
3565 n = (n << 8) + (unsigned)c;
3566 c = getc(fd);
3567 if (c == EOF) return -1;
3568 n = (n << 8) + (unsigned)c;
3569 c = getc(fd);
3570 if (c == EOF) return -1;
3571 n = (n << 8) + (unsigned)c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02003572 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003573}
3574
3575/*
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003576 * Read a string of length "cnt" from "fd" into allocated memory.
3577 * Returns NULL when out of memory or unable to read that many bytes.
3578 */
3579 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003580read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003581{
3582 char_u *str;
3583 int i;
3584 int c;
3585
Bram Moolenaar85a20022019-12-21 18:25:54 +01003586 // allocate memory
Bram Moolenaar964b3742019-05-24 18:54:09 +02003587 str = alloc(cnt + 1);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003588 if (str != NULL)
3589 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003590 // Read the string. Quit when running into the EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003591 for (i = 0; i < cnt; ++i)
3592 {
3593 c = getc(fd);
3594 if (c == EOF)
3595 {
3596 vim_free(str);
3597 return NULL;
3598 }
3599 str[i] = c;
3600 }
3601 str[i] = NUL;
3602 }
3603 return str;
3604}
3605
3606/*
3607 * Write a number to file "fd", MSB first, in "len" bytes.
3608 */
3609 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003610put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003611{
3612 int i;
3613
3614 for (i = len - 1; i >= 0; --i)
3615 if (putc((int)(nr >> (i * 8)), fd) == EOF)
3616 return FAIL;
3617 return OK;
3618}
3619
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003620#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003621
Bram Moolenaar85a20022019-12-21 18:25:54 +01003622#ifndef PROTO // proto is defined in vim.h
Bram Moolenaar51628222016-12-01 23:03:28 +01003623# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003624/*
3625 * Return time in msec since "start_tv".
3626 */
3627 long
3628elapsed(struct timeval *start_tv)
3629{
3630 struct timeval now_tv;
3631
3632 gettimeofday(&now_tv, NULL);
3633 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
3634 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
3635}
Bram Moolenaar51628222016-12-01 23:03:28 +01003636# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003637
Bram Moolenaar51628222016-12-01 23:03:28 +01003638# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003639/*
3640 * Return time in msec since "start_tick".
3641 */
3642 long
3643elapsed(DWORD start_tick)
3644{
3645 DWORD now = GetTickCount();
3646
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003647 return (long)now - (long)start_tick;
3648}
Bram Moolenaar51628222016-12-01 23:03:28 +01003649# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003650#endif
Bram Moolenaar20608922018-04-21 22:30:08 +02003651
3652#if defined(FEAT_JOB_CHANNEL) \
3653 || (defined(UNIX) && (!defined(USE_SYSTEM) \
3654 || (defined(FEAT_GUI) && defined(FEAT_TERMINAL)))) \
3655 || defined(PROTO)
3656/*
3657 * Parse "cmd" and put the white-separated parts in "argv".
3658 * "argv" is an allocated array with "argc" entries and room for 4 more.
3659 * Returns FAIL when out of memory.
3660 */
3661 int
3662mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
3663{
3664 int i;
3665 char_u *p, *d;
3666 int inquote;
3667
3668 /*
3669 * Do this loop twice:
3670 * 1: find number of arguments
3671 * 2: separate them and build argv[]
3672 */
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003673 for (i = 1; i <= 2; ++i)
Bram Moolenaar20608922018-04-21 22:30:08 +02003674 {
3675 p = skipwhite(cmd);
3676 inquote = FALSE;
3677 *argc = 0;
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003678 while (*p != NUL)
Bram Moolenaar20608922018-04-21 22:30:08 +02003679 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003680 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003681 (*argv)[*argc] = (char *)p;
3682 ++*argc;
3683 d = p;
3684 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
3685 {
3686 if (p[0] == '"')
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02003687 // quotes surrounding an argument and are dropped
Bram Moolenaar20608922018-04-21 22:30:08 +02003688 inquote = !inquote;
3689 else
3690 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02003691 if (rem_backslash(p))
Bram Moolenaar20608922018-04-21 22:30:08 +02003692 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02003693 // First pass: skip over "\ " and "\"".
3694 // Second pass: Remove the backslash.
Bram Moolenaar20608922018-04-21 22:30:08 +02003695 ++p;
3696 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003697 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003698 *d++ = *p;
3699 }
3700 ++p;
3701 }
3702 if (*p == NUL)
3703 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003704 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003705 *d++ = NUL;
3706 break;
3707 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003708 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003709 *d++ = NUL;
3710 p = skipwhite(p + 1);
3711 }
3712 if (*argv == NULL)
3713 {
3714 if (use_shcf)
3715 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003716 // Account for possible multiple args in p_shcf.
Bram Moolenaar20608922018-04-21 22:30:08 +02003717 p = p_shcf;
3718 for (;;)
3719 {
3720 p = skiptowhite(p);
3721 if (*p == NUL)
3722 break;
3723 ++*argc;
3724 p = skipwhite(p);
3725 }
3726 }
3727
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003728 *argv = ALLOC_MULT(char *, *argc + 4);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003729 if (*argv == NULL) // out of memory
Bram Moolenaar20608922018-04-21 22:30:08 +02003730 return FAIL;
3731 }
3732 }
3733 return OK;
3734}
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003735
3736# if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
3737/*
3738 * Build "argv[argc]" from the string "cmd".
3739 * "argv[argc]" is set to NULL;
3740 * Return FAIL when out of memory.
3741 */
3742 int
3743build_argv_from_string(char_u *cmd, char ***argv, int *argc)
3744{
3745 char_u *cmd_copy;
3746 int i;
3747
Bram Moolenaar85a20022019-12-21 18:25:54 +01003748 // Make a copy, parsing will modify "cmd".
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003749 cmd_copy = vim_strsave(cmd);
3750 if (cmd_copy == NULL
3751 || mch_parse_cmd(cmd_copy, FALSE, argv, argc) == FAIL)
3752 {
3753 vim_free(cmd_copy);
3754 return FAIL;
3755 }
3756 for (i = 0; i < *argc; i++)
3757 (*argv)[i] = (char *)vim_strsave((char_u *)(*argv)[i]);
3758 (*argv)[*argc] = NULL;
3759 vim_free(cmd_copy);
3760 return OK;
3761}
3762
3763/*
3764 * Build "argv[argc]" from the list "l".
3765 * "argv[argc]" is set to NULL;
3766 * Return FAIL when out of memory.
3767 */
3768 int
3769build_argv_from_list(list_T *l, char ***argv, int *argc)
3770{
3771 listitem_T *li;
3772 char_u *s;
3773
Bram Moolenaar85a20022019-12-21 18:25:54 +01003774 // Pass argv[] to mch_call_shell().
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003775 *argv = ALLOC_MULT(char *, l->lv_len + 1);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003776 if (*argv == NULL)
3777 return FAIL;
3778 *argc = 0;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003779 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003780 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003781 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003782 if (s == NULL)
3783 {
3784 int i;
3785
3786 for (i = 0; i < *argc; ++i)
Bram Moolenaardf195602020-04-13 18:13:33 +02003787 VIM_CLEAR((*argv)[i]);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003788 return FAIL;
3789 }
3790 (*argv)[*argc] = (char *)vim_strsave(s);
3791 *argc += 1;
3792 }
3793 (*argv)[*argc] = NULL;
3794 return OK;
3795}
3796# endif
Bram Moolenaar20608922018-04-21 22:30:08 +02003797#endif
Bram Moolenaar57da6982019-09-13 22:30:11 +02003798
3799/*
3800 * Change the behavior of vterm.
3801 * 0: As usual.
3802 * 1: Windows 10 version 1809
3803 * The bug causes unstable handling of ambiguous width character.
Bram Moolenaar36e7a822019-11-13 21:49:24 +01003804 * 2: Windows 10 version 1903 & 1909
Bram Moolenaar57da6982019-09-13 22:30:11 +02003805 * Use the wrong result because each result is different.
3806 * 3: Windows 10 insider preview (current latest logic)
3807 */
3808 int
3809get_special_pty_type(void)
3810{
3811#ifdef MSWIN
3812 return get_conpty_type();
3813#else
3814 return 0;
3815#endif
3816}