blob: a8e34df7ee873ea08df927574d040d20599cd3ad [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{
Bram Moolenaar85a20022019-12-21 18:25:54 +010025 // While an operator is being executed we return "virtual_op", because
26 // VIsual_active has already been reset, thus we can't check for "block"
27 // being used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000028 if (virtual_op != MAYBE)
29 return virtual_op;
30 return (ve_flags == VE_ALL
Bram Moolenaar071d4272004-06-13 20:20:40 +000031 || ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +000032 || ((ve_flags & VE_INSERT) && (State & INSERT)));
33}
34
35/*
36 * Get the screen position of the cursor.
37 */
38 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010039getviscol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000040{
41 colnr_T x;
42
43 getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL);
44 return (int)x;
45}
46
47/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +000048 * Go to column "wcol", and add/insert white space as necessary to get the
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 * cursor in that column.
50 * The caller must have saved the cursor line for undo!
51 */
52 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010053coladvance_force(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000054{
55 int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
56
57 if (wcol == MAXCOL)
58 curwin->w_valid &= ~VALID_VIRTCOL;
59 else
60 {
Bram Moolenaar85a20022019-12-21 18:25:54 +010061 // Virtcol is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +000062 curwin->w_valid |= VALID_VIRTCOL;
63 curwin->w_virtcol = wcol;
64 }
65 return rc;
66}
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/*
Bram Moolenaar977239e2019-01-11 16:16:01 +010069 * Get the screen position of character col with a coladd in the cursor line.
70 */
71 int
Bram Moolenaarfc3abf42019-01-24 15:54:21 +010072getviscol2(colnr_T col, colnr_T coladd UNUSED)
Bram Moolenaar977239e2019-01-11 16:16:01 +010073{
74 colnr_T x;
75 pos_T pos;
76
77 pos.lnum = curwin->w_cursor.lnum;
78 pos.col = col;
Bram Moolenaar977239e2019-01-11 16:16:01 +010079 pos.coladd = coladd;
Bram Moolenaar977239e2019-01-11 16:16:01 +010080 getvvcol(curwin, &pos, &x, NULL, NULL);
81 return (int)x;
82}
83
84/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 * Try to advance the Cursor to the specified screen column.
86 * If virtual editing: fine tune the cursor position.
87 * Note that all virtual positions off the end of a line should share
88 * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)),
89 * beginning at coladd 0.
90 *
91 * return OK if desired column is reached, FAIL if not
92 */
93 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010094coladvance(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000095{
96 int rc = getvpos(&curwin->w_cursor, wcol);
97
98 if (wcol == MAXCOL || rc == FAIL)
99 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000100 else if (*ml_get_cursor() != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100102 // Virtcol is valid when not on a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 curwin->w_valid |= VALID_VIRTCOL;
104 curwin->w_virtcol = wcol;
105 }
106 return rc;
107}
108
109/*
110 * Return in "pos" the position of the cursor advanced to screen column "wcol".
111 * return OK if desired column is reached, FAIL if not
112 */
113 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100114getvpos(pos_T *pos, colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 return coladvance2(pos, FALSE, virtual_active(), wcol);
117}
118
119 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100120coladvance2(
121 pos_T *pos,
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200122 int addspaces, // change the text to achieve our goal?
123 int finetune, // change char offset for the exact column
124 colnr_T wcol_arg) // column to move to (can be negative)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125{
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200126 colnr_T wcol = wcol_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 int idx;
128 char_u *ptr;
129 char_u *line;
130 colnr_T col = 0;
131 int csize = 0;
132 int one_more;
133#ifdef FEAT_LINEBREAK
134 int head = 0;
135#endif
136
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000137 one_more = (State & INSERT)
138 || restart_edit != NUL
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000139 || (VIsual_active && *p_sel != 'o')
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200140 || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL);
Bram Moolenaara1381de2009-11-03 15:44:21 +0000141 line = ml_get_buf(curbuf, pos->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
143 if (wcol >= MAXCOL)
144 {
145 idx = (int)STRLEN(line) - 1 + one_more;
146 col = wcol;
147
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 if ((addspaces || finetune) && !VIsual_active)
149 {
150 curwin->w_curswant = linetabsize(line) + one_more;
151 if (curwin->w_curswant > 0)
152 --curwin->w_curswant;
153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 }
155 else
156 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200157 int width = curwin->w_width - win_col_off(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158
Bram Moolenaarebefac62005-12-28 22:39:57 +0000159 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 && curwin->w_p_wrap
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 && curwin->w_width != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 && wcol >= (colnr_T)width)
163 {
164 csize = linetabsize(line);
165 if (csize > 0)
166 csize--;
167
Bram Moolenaarebefac62005-12-28 22:39:57 +0000168 if (wcol / width > (colnr_T)csize / width
169 && ((State & INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100171 // In case of line wrapping don't move the cursor beyond the
172 // right screen edge. In Insert mode allow going just beyond
173 // the last character (like what happens when typing and
174 // reaching the right window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 wcol = (csize / width + 1) * width - 1;
176 }
177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 ptr = line;
180 while (col <= wcol && *ptr != NUL)
181 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100182 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#ifdef FEAT_LINEBREAK
Bram Moolenaar597a4222014-06-25 14:39:50 +0200184 csize = win_lbr_chartabsize(curwin, line, ptr, col, &head);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100185 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200187 csize = lbr_chartabsize_adv(line, &ptr, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#endif
189 col += csize;
190 }
191 idx = (int)(ptr - line);
192 /*
193 * Handle all the special cases. The virtual_active() check
194 * is needed to ensure that a virtual position off the end of
195 * a line has the correct indexing. The one_more comparison
196 * replaces an explicit add of one_more later on.
197 */
198 if (col > wcol || (!virtual_active() && one_more == 0))
199 {
200 idx -= 1;
201# ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +0100202 // Don't count the chars from 'showbreak'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 csize -= head;
204# endif
205 col -= csize;
206 }
207
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 if (virtual_active()
209 && addspaces
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200210 && wcol >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 && ((col != wcol && col != wcol + 1) || csize > 1))
212 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100213 // 'virtualedit' is set: The difference between wcol and col is
214 // filled with spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215
216 if (line[idx] == NUL)
217 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100218 // Append spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 int correct = wcol - col;
220 char_u *newline = alloc(idx + correct + 1);
221 int t;
222
223 if (newline == NULL)
224 return FAIL;
225
226 for (t = 0; t < idx; ++t)
227 newline[t] = line[t];
228
229 for (t = 0; t < correct; ++t)
230 newline[t + idx] = ' ';
231
232 newline[idx + correct] = NUL;
233
234 ml_replace(pos->lnum, newline, FALSE);
235 changed_bytes(pos->lnum, (colnr_T)idx);
236 idx += correct;
237 col = wcol;
238 }
239 else
240 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100241 // Break a tab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 int linelen = (int)STRLEN(line);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100243 int correct = wcol - col - csize + 1; // negative!!
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000244 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 int t, s = 0;
246 int v;
247
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000248 if (-correct > csize)
249 return FAIL;
250
251 newline = alloc(linelen + csize);
252 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 return FAIL;
254
255 for (t = 0; t < linelen; t++)
256 {
257 if (t != idx)
258 newline[s++] = line[t];
259 else
260 for (v = 0; v < csize; v++)
261 newline[s++] = ' ';
262 }
263
264 newline[linelen + csize - 1] = NUL;
265
266 ml_replace(pos->lnum, newline, FALSE);
267 changed_bytes(pos->lnum, idx);
268 idx += (csize - 1 + correct);
269 col += correct;
270 }
271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 }
273
274 if (idx < 0)
275 pos->col = 0;
276 else
277 pos->col = idx;
278
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 pos->coladd = 0;
280
281 if (finetune)
282 {
283 if (wcol == MAXCOL)
284 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100285 // The width of the last character is used to set coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 if (!one_more)
287 {
288 colnr_T scol, ecol;
289
290 getvcol(curwin, pos, &scol, NULL, &ecol);
291 pos->coladd = ecol - scol;
292 }
293 }
294 else
295 {
296 int b = (int)wcol - (int)col;
297
Bram Moolenaar85a20022019-12-21 18:25:54 +0100298 // The difference between wcol and col is used to set coladd.
Bram Moolenaar02631462017-09-22 15:20:32 +0200299 if (b > 0 && b < (MAXCOL - 2 * curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 pos->coladd = b;
301
302 col += b;
303 }
304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
Bram Moolenaar85a20022019-12-21 18:25:54 +0100306 // prevent from moving onto a trail byte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200308 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200310 if (wcol < 0 || col < wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 return FAIL;
312 return OK;
313}
314
315/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000316 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 */
318 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100319inc_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320{
321 return inc(&curwin->w_cursor);
322}
323
Bram Moolenaar446cb832008-06-24 21:56:24 +0000324/*
325 * Increment the line pointer "lp" crossing line boundaries as necessary.
326 * Return 1 when going to the next line.
327 * Return 2 when moving forward onto a NUL at the end of the line).
328 * Return -1 when at the end of file.
329 * Return 0 otherwise.
330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100332inc(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100334 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
Bram Moolenaar85a20022019-12-21 18:25:54 +0100336 // when searching position may be set to end of a line
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100337 if (lp->col != MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100339 p = ml_get_pos(lp);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100340 if (*p != NUL) // still within line, move to next char (may be NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100342 if (has_mbyte)
343 {
344 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100346 lp->col += l;
347 return ((p[l] != NUL) ? 0 : 2);
348 }
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100349 lp->col++;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100350 lp->coladd = 0;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100351 return ((p[1] != NUL) ? 0 : 2);
352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100354 if (lp->lnum != curbuf->b_ml.ml_line_count) // there is a next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 {
356 lp->col = 0;
357 lp->lnum++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 lp->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 return 1;
360 }
361 return -1;
362}
363
364/*
365 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
366 */
367 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100368incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369{
370 int r;
371
372 if ((r = inc(lp)) >= 1 && lp->col)
373 r = inc(lp);
374 return r;
375}
376
377/*
378 * dec(p)
379 *
380 * Decrement the line pointer 'p' crossing line boundaries as necessary.
381 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
382 */
383 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100384dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100386 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387}
388
389 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100390dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391{
392 char_u *p;
393
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 lp->coladd = 0;
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100395 if (lp->col == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100397 // past end of line
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100398 p = ml_get(lp->lnum);
399 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100400 if (has_mbyte)
401 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100402 return 0;
403 }
404
405 if (lp->col > 0)
406 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100407 // still within line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 lp->col--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 if (has_mbyte)
410 {
411 p = ml_get(lp->lnum);
412 lp->col -= (*mb_head_off)(p, p + lp->col);
413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 return 0;
415 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100416
417 if (lp->lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100419 // there is a prior line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 lp->lnum--;
421 p = ml_get(lp->lnum);
422 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 if (has_mbyte)
424 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 return 1;
426 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100427
Bram Moolenaar85a20022019-12-21 18:25:54 +0100428 // at start of file
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100429 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430}
431
432/*
433 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
434 */
435 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100436decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
438 int r;
439
440 if ((r = dec(lp)) == 1 && lp->col)
441 r = dec(lp);
442 return r;
443}
444
445/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200446 * Get the line number relative to the current cursor position, i.e. the
447 * difference between line number and cursor position. Only look for lines that
448 * can be visible, folded lines don't count.
449 */
450 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100451get_cursor_rel_lnum(
452 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100453 linenr_T lnum) // line number to get the result for
Bram Moolenaar64486672010-05-16 15:46:46 +0200454{
455 linenr_T cursor = wp->w_cursor.lnum;
456 linenr_T retval = 0;
457
458#ifdef FEAT_FOLDING
459 if (hasAnyFolding(wp))
460 {
461 if (lnum > cursor)
462 {
463 while (lnum > cursor)
464 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100465 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100466 // if lnum and cursor are in the same fold,
467 // now lnum <= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200468 if (lnum > cursor)
469 retval++;
470 lnum--;
471 }
472 }
473 else if (lnum < cursor)
474 {
475 while (lnum < cursor)
476 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100477 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100478 // if lnum and cursor are in the same fold,
479 // now lnum >= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200480 if (lnum < cursor)
481 retval--;
482 lnum++;
483 }
484 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100485 // else if (lnum == cursor)
486 // retval = 0;
Bram Moolenaar64486672010-05-16 15:46:46 +0200487 }
488 else
489#endif
490 retval = lnum - cursor;
491
492 return retval;
493}
494
495/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200496 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
497 * This allows for the col to be on the NUL byte.
498 */
499 void
500check_pos(buf_T *buf, pos_T *pos)
501{
502 char_u *line;
503 colnr_T len;
504
505 if (pos->lnum > buf->b_ml.ml_line_count)
506 pos->lnum = buf->b_ml.ml_line_count;
507
508 if (pos->col > 0)
509 {
510 line = ml_get_buf(buf, pos->lnum, FALSE);
511 len = (colnr_T)STRLEN(line);
512 if (pos->col > len)
513 pos->col = len;
514 }
515}
516
517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 * Make sure curwin->w_cursor.lnum is valid.
519 */
520 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100521check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
523 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
524 {
525#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100526 // If there is a closed fold at the end of the file, put the cursor in
527 // its first line. Otherwise in the last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 if (!hasFolding(curbuf->b_ml.ml_line_count,
529 &curwin->w_cursor.lnum, NULL))
530#endif
531 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
532 }
533 if (curwin->w_cursor.lnum <= 0)
534 curwin->w_cursor.lnum = 1;
535}
536
537/*
538 * Make sure curwin->w_cursor.col is valid.
539 */
540 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100541check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200543 check_cursor_col_win(curwin);
544}
545
546/*
547 * Make sure win->w_cursor.col is valid.
548 */
549 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100550check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200551{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 colnr_T len;
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200553 colnr_T oldcol = win->w_cursor.col;
554 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200556 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200558 win->w_cursor.col = 0;
559 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100561 // Allow cursor past end-of-line when:
562 // - in Insert mode or restarting Insert mode
563 // - in Visual mode and 'selection' isn't "old"
564 // - 'virtualedit' is set
Bram Moolenaarebefac62005-12-28 22:39:57 +0000565 if ((State & INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 || (VIsual_active && *p_sel != 'o')
Bram Moolenaar33f54432008-01-04 20:25:44 +0000567 || (ve_flags & VE_ONEMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200569 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000571 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200572 win->w_cursor.col = len - 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100573 // Move the cursor to the head byte.
Bram Moolenaar87c19962007-04-26 08:54:21 +0000574 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200575 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200578 else if (win->w_cursor.col < 0)
579 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580
Bram Moolenaar85a20022019-12-21 18:25:54 +0100581 // If virtual editing is on, we can leave the cursor on the old position,
582 // only we must set it to virtual. But don't do it when at the end of the
583 // line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200585 win->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 else if (ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000587 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200588 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200589 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200590 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaard41babe2017-08-30 17:01:35 +0200591
Bram Moolenaar85a20022019-12-21 18:25:54 +0100592 // Make sure that coladd is not more than the char width.
593 // Not for the last character, coladd is then used when the cursor
594 // is actually after the last character.
Bram Moolenaard41babe2017-08-30 17:01:35 +0200595 if (win->w_cursor.col + 1 < len && win->w_cursor.coladd > 0)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200596 {
597 int cs, ce;
598
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200599 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
600 if (win->w_cursor.coladd > ce - cs)
601 win->w_cursor.coladd = ce - cs;
602 }
603 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000604 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100605 // avoid weird number when there is a miscalculation or overflow
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200606 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608}
609
610/*
611 * make sure curwin->w_cursor in on a valid character
612 */
613 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100614check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
616 check_cursor_lnum();
617 check_cursor_col();
618}
619
620#if defined(FEAT_TEXTOBJ) || defined(PROTO)
621/*
622 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
623 * Allow it when in Visual mode and 'selection' is not "old".
624 */
625 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100626adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627{
628 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 && gchar_cursor() == NUL)
631 --curwin->w_cursor.col;
632}
633#endif
634
635/*
636 * When curwin->w_leftcol has changed, adjust the cursor position.
637 * Return TRUE if the cursor was moved.
638 */
639 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640leftcol_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 long lastcol;
643 colnr_T s, e;
644 int retval = FALSE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100645 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
647 changed_cline_bef_curs();
Bram Moolenaar02631462017-09-22 15:20:32 +0200648 lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 validate_virtcol();
650
651 /*
652 * If the cursor is right or left of the screen, move it to last or first
653 * character.
654 */
Bram Moolenaar375e3392019-01-31 18:26:10 +0100655 if (curwin->w_virtcol > (colnr_T)(lastcol - siso))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {
657 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100658 coladvance((colnr_T)(lastcol - siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100660 else if (curwin->w_virtcol < curwin->w_leftcol + siso)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100663 (void)coladvance((colnr_T)(curwin->w_leftcol + siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
665
666 /*
667 * If the start of the character under the cursor is not on the screen,
668 * advance the cursor one more char. If this fails (last char of the
669 * line) adjust the scrolling.
670 */
671 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
672 if (e > (colnr_T)lastcol)
673 {
674 retval = TRUE;
675 coladvance(s - 1);
676 }
677 else if (s < curwin->w_leftcol)
678 {
679 retval = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100680 if (coladvance(e + 1) == FAIL) // there isn't another character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100682 curwin->w_leftcol = s; // adjust w_leftcol instead
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 changed_cline_bef_curs();
684 }
685 }
686
687 if (retval)
688 curwin->w_set_curswant = TRUE;
689 redraw_later(NOT_VALID);
690 return retval;
691}
692
693/**********************************************************************
694 * Various routines dealing with allocation and deallocation of memory.
695 */
696
697#if defined(MEM_PROFILE) || defined(PROTO)
698
699# define MEM_SIZES 8200
700static long_u mem_allocs[MEM_SIZES];
701static long_u mem_frees[MEM_SIZES];
702static long_u mem_allocated;
703static long_u mem_freed;
704static long_u mem_peak;
705static long_u num_alloc;
706static long_u num_freed;
707
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100709mem_pre_alloc_s(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710{
711 *sizep += sizeof(size_t);
712}
713
714 static void
Bram Moolenaar964b3742019-05-24 18:54:09 +0200715mem_pre_alloc_l(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
717 *sizep += sizeof(size_t);
718}
719
720 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100721mem_post_alloc(
722 void **pp,
723 size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724{
725 if (*pp == NULL)
726 return;
727 size -= sizeof(size_t);
728 *(long_u *)*pp = size;
729 if (size <= MEM_SIZES-1)
730 mem_allocs[size-1]++;
731 else
732 mem_allocs[MEM_SIZES-1]++;
733 mem_allocated += size;
734 if (mem_allocated - mem_freed > mem_peak)
735 mem_peak = mem_allocated - mem_freed;
736 num_alloc++;
737 *pp = (void *)((char *)*pp + sizeof(size_t));
738}
739
740 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100741mem_pre_free(void **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742{
743 long_u size;
744
745 *pp = (void *)((char *)*pp - sizeof(size_t));
746 size = *(size_t *)*pp;
747 if (size <= MEM_SIZES-1)
748 mem_frees[size-1]++;
749 else
750 mem_frees[MEM_SIZES-1]++;
751 mem_freed += size;
752 num_freed++;
753}
754
755/*
756 * called on exit via atexit()
757 */
758 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100759vim_mem_profile_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760{
761 int i, j;
762
763 printf("\r\n");
764 j = 0;
765 for (i = 0; i < MEM_SIZES - 1; i++)
766 {
767 if (mem_allocs[i] || mem_frees[i])
768 {
769 if (mem_frees[i] > mem_allocs[i])
770 printf("\r\n%s", _("ERROR: "));
771 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
772 j++;
773 if (j > 3)
774 {
775 j = 0;
776 printf("\r\n");
777 }
778 }
779 }
780
781 i = MEM_SIZES - 1;
782 if (mem_allocs[i])
783 {
784 printf("\r\n");
785 if (mem_frees[i] > mem_allocs[i])
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200786 puts(_("ERROR: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
788 }
789
790 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
791 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
792 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
793 num_alloc, num_freed);
794}
795
Bram Moolenaar85a20022019-12-21 18:25:54 +0100796#endif // MEM_PROFILE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100798#ifdef FEAT_EVAL
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100799 int
Bram Moolenaar964b3742019-05-24 18:54:09 +0200800alloc_does_fail(size_t size)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100801{
802 if (alloc_fail_countdown == 0)
803 {
804 if (--alloc_fail_repeat <= 0)
805 alloc_fail_id = 0;
Bram Moolenaara260b872016-01-15 20:48:22 +0100806 do_outofmem_msg(size);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100807 return TRUE;
808 }
809 --alloc_fail_countdown;
810 return FALSE;
811}
812#endif
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814/*
815 * Some memory is reserved for error messages and for being able to
816 * call mf_release_all(), which needs some memory for mf_trans_add().
817 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100818#define KEEP_ROOM (2 * 8192L)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200819#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
821/*
Bram Moolenaar964b3742019-05-24 18:54:09 +0200822 * The normal way to allocate memory. This handles an out-of-memory situation
823 * as well as possible, still returns NULL when we're completely out.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200825 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200826alloc(size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
Bram Moolenaar964b3742019-05-24 18:54:09 +0200828 return lalloc(size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829}
830
831/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100832 * alloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100833 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200834 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200835alloc_id(size_t size, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100836{
837#ifdef FEAT_EVAL
Bram Moolenaar964b3742019-05-24 18:54:09 +0200838 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100839 return NULL;
840#endif
Bram Moolenaar964b3742019-05-24 18:54:09 +0200841 return lalloc(size, TRUE);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100842}
843
844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 * Allocate memory and set all bytes to zero.
846 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200847 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200848alloc_clear(size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200850 void *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar964b3742019-05-24 18:54:09 +0200852 p = lalloc(size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (p != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200854 (void)vim_memset(p, 0, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 return p;
856}
857
858/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100859 * Same as alloc_clear() but with allocation id for testing
860 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200861 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200862alloc_clear_id(size_t size, alloc_id_T id UNUSED)
Bram Moolenaar162b7142018-12-21 15:17:36 +0100863{
864#ifdef FEAT_EVAL
Bram Moolenaar964b3742019-05-24 18:54:09 +0200865 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar162b7142018-12-21 15:17:36 +0100866 return NULL;
867#endif
868 return alloc_clear(size);
869}
870
871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 * Allocate memory like lalloc() and set all bytes to zero.
873 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200874 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200875lalloc_clear(size_t size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200877 void *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200879 p = lalloc(size, message);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (p != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200881 (void)vim_memset(p, 0, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 return p;
883}
884
885/*
886 * Low level memory allocation function.
887 * This is used often, KEEP IT FAST!
888 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200889 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200890lalloc(size_t size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100892 void *p; // pointer to new storage space
893 static int releasing = FALSE; // don't do mf_release_all() recursive
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 int try_again;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100895#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100896 static size_t allocated = 0; // allocated since last avail check
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897#endif
898
Bram Moolenaar964b3742019-05-24 18:54:09 +0200899 // Safety check for allocating zero bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 if (size == 0)
901 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200902 // Don't hide this message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 emsg_silent = 0;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200904 iemsg(_("E341: Internal error: lalloc(0, )"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 return NULL;
906 }
907
908#ifdef MEM_PROFILE
909 mem_pre_alloc_l(&size);
910#endif
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 /*
913 * Loop when out of memory: Try to release some memfile blocks and
914 * if some blocks are released call malloc again.
915 */
916 for (;;)
917 {
918 /*
919 * Handle three kind of systems:
920 * 1. No check for available memory: Just return.
921 * 2. Slow check for available memory: call mch_avail_mem() after
922 * allocating KEEP_ROOM amount of memory.
923 * 3. Strict check for available memory: call mch_avail_mem()
924 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200925 if ((p = malloc(size)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
927#ifndef HAVE_AVAIL_MEM
Bram Moolenaar85a20022019-12-21 18:25:54 +0100928 // 1. No check for available memory: Just return.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 goto theend;
930#else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100931 // 2. Slow check for available memory: call mch_avail_mem() after
932 // allocating (KEEP_ROOM / 2) amount of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 allocated += size;
934 if (allocated < KEEP_ROOM / 2)
935 goto theend;
936 allocated = 0;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100937
Bram Moolenaar85a20022019-12-21 18:25:54 +0100938 // 3. check for available memory: call mch_avail_mem()
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200939 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100941 free(p); // System is low... no go!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 p = NULL;
943 }
944 else
945 goto theend;
946#endif
947 }
948 /*
949 * Remember that mf_release_all() is being called to avoid an endless
950 * loop, because mf_release_all() may call alloc() recursively.
951 */
952 if (releasing)
953 break;
954 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +0000955
Bram Moolenaar85a20022019-12-21 18:25:54 +0100956 clear_sb_text(TRUE); // free any scrollback text
957 try_again = mf_release_all(); // release as many blocks as possible
Bram Moolenaar661b1822005-07-28 22:36:45 +0000958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 releasing = FALSE;
960 if (!try_again)
961 break;
962 }
963
964 if (message && p == NULL)
965 do_outofmem_msg(size);
966
967theend:
968#ifdef MEM_PROFILE
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200969 mem_post_alloc(&p, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970#endif
971 return p;
972}
973
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100974/*
975 * lalloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100976 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100977#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200978 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200979lalloc_id(size_t size, int message, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100980{
981#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100982 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100983 return NULL;
984#endif
Bram Moolenaar964b3742019-05-24 18:54:09 +0200985 return (lalloc(size, message));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100986}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100987#endif
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#if defined(MEM_PROFILE) || defined(PROTO)
990/*
991 * realloc() with memory profiling.
992 */
993 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100994mem_realloc(void *ptr, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
996 void *p;
997
998 mem_pre_free(&ptr);
999 mem_pre_alloc_s(&size);
1000
1001 p = realloc(ptr, size);
1002
1003 mem_post_alloc(&p, size);
1004
1005 return p;
1006}
1007#endif
1008
1009/*
1010* Avoid repeating the error message many times (they take 1 second each).
1011* Did_outofmem_msg is reset when a character is read.
1012*/
1013 void
Bram Moolenaar964b3742019-05-24 18:54:09 +02001014do_outofmem_msg(size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015{
1016 if (!did_outofmem_msg)
1017 {
Bram Moolenaar4dc8f492019-08-21 13:06:55 +02001018 // Don't hide this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001020
Bram Moolenaar4dc8f492019-08-21 13:06:55 +02001021 // Must come first to avoid coming back here when printing the error
1022 // message fails, e.g. when setting v:errmsg.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001024
Bram Moolenaar964b3742019-05-24 18:54:09 +02001025 semsg(_("E342: Out of memory! (allocating %lu bytes)"), (long_u)size);
Bram Moolenaar4dc8f492019-08-21 13:06:55 +02001026
1027 if (starting == NO_SCREEN)
1028 // Not even finished with initializations and already out of
1029 // memory? Then nothing is going to work, exit.
1030 mch_exit(123);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032}
1033
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001034#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001035
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001036/*
1037 * Free everything that we allocated.
1038 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001039 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1040 * surprised if Vim crashes...
1041 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001042 */
1043 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001044free_all_mem(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001045{
1046 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001047
Bram Moolenaar85a20022019-12-21 18:25:54 +01001048 // When we cause a crash here it is caught and Vim tries to exit cleanly.
1049 // Don't try freeing everything again.
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001050 if (entered_free_all_mem)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001051 return;
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001052 entered_free_all_mem = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001053 // Don't want to trigger autocommands from here on.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001054 block_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001055
Bram Moolenaar85a20022019-12-21 18:25:54 +01001056 // Close all tabs and windows. Reset 'equalalways' to avoid redraws.
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001057 p_ea = FALSE;
Bram Moolenaare5c83282019-05-03 23:15:37 +02001058 if (first_tabpage != NULL && first_tabpage->tp_next != NULL)
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001059 do_cmdline_cmd((char_u *)"tabonly!");
Bram Moolenaar95f09602016-11-10 20:01:45 +01001060 if (!ONE_WINDOW)
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001061 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001062
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001063# if defined(FEAT_SPELL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001064 // Free all spell info.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001065 spell_free_all();
1066# endif
1067
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001068# if defined(FEAT_BEVAL_TERM)
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001069 ui_remove_balloon();
1070# endif
Bram Moolenaar03a9f842020-05-13 13:40:16 +02001071# ifdef FEAT_PROP_POPUP
Bram Moolenaar06f08532020-05-12 23:45:16 +02001072 if (curwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02001073 close_all_popups(TRUE);
Bram Moolenaar06f08532020-05-12 23:45:16 +02001074# endif
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001075
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001076 // Clear user commands (before deleting buffers).
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001077 ex_comclear(NULL);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001078
Bram Moolenaare5c83282019-05-03 23:15:37 +02001079 // When exiting from mainerr_arg_missing curbuf has not been initialized,
1080 // and not much else.
1081 if (curbuf != NULL)
1082 {
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001083# ifdef FEAT_MENU
Bram Moolenaare5c83282019-05-03 23:15:37 +02001084 // Clear menus.
1085 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001086# ifdef FEAT_MULTI_LANG
Bram Moolenaare5c83282019-05-03 23:15:37 +02001087 do_cmdline_cmd((char_u *)"menutranslate clear");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001088# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001089# endif
Bram Moolenaare5c83282019-05-03 23:15:37 +02001090 // Clear mappings, abbreviations, breakpoints.
1091 do_cmdline_cmd((char_u *)"lmapclear");
1092 do_cmdline_cmd((char_u *)"xmapclear");
1093 do_cmdline_cmd((char_u *)"mapclear");
1094 do_cmdline_cmd((char_u *)"mapclear!");
1095 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001096# if defined(FEAT_EVAL)
Bram Moolenaare5c83282019-05-03 23:15:37 +02001097 do_cmdline_cmd((char_u *)"breakdel *");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001098# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001099# if defined(FEAT_PROFILE)
Bram Moolenaare5c83282019-05-03 23:15:37 +02001100 do_cmdline_cmd((char_u *)"profdel *");
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001101# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001102# if defined(FEAT_KEYMAP)
Bram Moolenaare5c83282019-05-03 23:15:37 +02001103 do_cmdline_cmd((char_u *)"set keymap=");
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001104# endif
Bram Moolenaare5c83282019-05-03 23:15:37 +02001105 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001106
1107# ifdef FEAT_TITLE
1108 free_titles();
1109# endif
1110# if defined(FEAT_SEARCHPATH)
1111 free_findfile();
1112# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001113
Bram Moolenaar85a20022019-12-21 18:25:54 +01001114 // Obviously named calls.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001115 free_all_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001116 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001117 free_all_marks();
1118 alist_clear(&global_alist);
1119 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001120 free_users();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001121 free_search_patterns();
1122 free_old_sub();
1123 free_last_insert();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001124 free_insexpand_stuff();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001125 free_prev_shellcmd();
1126 free_regexp_stuff();
1127 free_tag_stuff();
1128 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001129# ifdef FEAT_SIGNS
1130 free_signs();
1131# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001132# ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001133 set_expr_line(NULL, NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001134# endif
1135# ifdef FEAT_DIFF
Bram Moolenaare5c83282019-05-03 23:15:37 +02001136 if (curtab != NULL)
1137 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001138# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001139 clear_sb_text(TRUE); // free any scrollback text
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001140
Bram Moolenaar85a20022019-12-21 18:25:54 +01001141 // Free some global vars.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001142 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001143# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001144 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001145# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001146 vim_free(last_cmdline);
1147 vim_free(new_last_cmdline);
Bram Moolenaar238a5642006-02-21 22:12:05 +00001148 set_keep_msg(NULL, 0);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001149
Bram Moolenaar85a20022019-12-21 18:25:54 +01001150 // Clear cmdline history.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001151 p_hi = 0;
1152 init_history();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001153# ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001154 clear_global_prop_types();
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001155# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001156
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001157# ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001158 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001159 win_T *win;
1160 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001161
1162 qf_free_all(NULL);
Bram Moolenaare5c83282019-05-03 23:15:37 +02001163 // Free all location lists
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001164 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001165 qf_free_all(win);
1166 }
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001167# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001168
Bram Moolenaare5c83282019-05-03 23:15:37 +02001169 // Close all script inputs.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001170 close_all_scripts();
1171
Bram Moolenaare5c83282019-05-03 23:15:37 +02001172 if (curwin != NULL)
1173 // Destroy all windows. Must come before freeing buffers.
1174 win_free_all();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001175
Bram Moolenaar85a20022019-12-21 18:25:54 +01001176 // Free all option values. Must come after closing windows.
Bram Moolenaar4f198282017-10-23 21:53:30 +02001177 free_all_options();
1178
Bram Moolenaar85a20022019-12-21 18:25:54 +01001179 // Free all buffers. Reset 'autochdir' to avoid accessing things that
1180 // were freed already.
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001181# ifdef FEAT_AUTOCHDIR
Bram Moolenaar2a329742008-04-01 12:53:43 +00001182 p_acd = FALSE;
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001183# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001184 for (buf = firstbuf; buf != NULL; )
1185 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001186 bufref_T bufref;
1187
1188 set_bufref(&bufref, buf);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001189 nextbuf = buf->b_next;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001190 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001191 if (bufref_valid(&bufref))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001192 buf = nextbuf; // didn't work, try next one
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001193 else
1194 buf = firstbuf;
1195 }
1196
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001197# ifdef FEAT_ARABIC
1198 free_arshape_buf();
1199# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001200
Bram Moolenaar85a20022019-12-21 18:25:54 +01001201 // Clear registers.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001202 clear_registers();
1203 ResetRedobuff();
1204 ResetRedobuff();
1205
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001206# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001207 vim_free(serverDelayedStartName);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001208# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001209
Bram Moolenaar85a20022019-12-21 18:25:54 +01001210 // highlight info
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001211 free_highlight();
1212
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001213 reset_last_sourcing();
1214
Bram Moolenaare5c83282019-05-03 23:15:37 +02001215 if (first_tabpage != NULL)
1216 {
1217 free_tabpage(first_tabpage);
1218 first_tabpage = NULL;
1219 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001220
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001221# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01001222 // Machine-specific free.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001223 mch_free_mem();
1224# endif
1225
Bram Moolenaar85a20022019-12-21 18:25:54 +01001226 // message history
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001227 for (;;)
1228 if (delete_first_msg() == FAIL)
1229 break;
1230
Bram Moolenaar655da312016-05-28 22:22:34 +02001231# ifdef FEAT_JOB_CHANNEL
1232 channel_free_all();
Bram Moolenaar655da312016-05-28 22:22:34 +02001233# endif
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001234# ifdef FEAT_TIMERS
Bram Moolenaar623e2632016-07-30 22:47:56 +02001235 timer_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001236# endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001237# ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001238 // must be after channel_free_all() with unrefs partials
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001239 eval_clear();
1240# endif
1241# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001242 // must be after eval_clear() with unrefs jobs
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001243 job_free_all();
1244# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001245
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001246 free_termoptions();
1247
Bram Moolenaar85a20022019-12-21 18:25:54 +01001248 // screenlines (can't display anything now!)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001249 free_screenlines();
1250
Bram Moolenaar82febc12019-06-10 14:48:59 +02001251# if defined(FEAT_SOUND)
1252 sound_free();
1253# endif
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001254# if defined(USE_XSMP)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001255 xsmp_close();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001256# endif
1257# ifdef FEAT_GUI_GTK
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001258 gui_mch_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001259# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001260 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001261
1262 vim_free(IObuff);
1263 vim_free(NameBuff);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001264# ifdef FEAT_QUICKFIX
1265 check_quickfix_busy();
1266# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001267}
1268#endif
1269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001271 * Copy "p[len]" into allocated memory, ignoring NUL characters.
1272 * Returns NULL when out of memory.
1273 */
1274 char_u *
Bram Moolenaar964b3742019-05-24 18:54:09 +02001275vim_memsave(char_u *p, size_t len)
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001276{
Bram Moolenaar964b3742019-05-24 18:54:09 +02001277 char_u *ret = alloc(len);
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001278
1279 if (ret != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001280 mch_memmove(ret, p, len);
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001281 return ret;
1282}
1283
1284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 * Isolate one part of a string option where parts are separated with
1286 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001287 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 * "*option" is advanced to the next part.
1289 * The length is returned.
1290 */
1291 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001292copy_option_part(
1293 char_u **option,
1294 char_u *buf,
1295 int maxlen,
1296 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297{
1298 int len = 0;
1299 char_u *p = *option;
1300
Bram Moolenaar85a20022019-12-21 18:25:54 +01001301 // skip '.' at start of option part, for 'suffixes'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 if (*p == '.')
1303 buf[len++] = *p++;
1304 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1305 {
1306 /*
1307 * Skip backslash before a separator character and space.
1308 */
1309 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1310 ++p;
1311 if (len < maxlen - 1)
1312 buf[len++] = *p;
1313 ++p;
1314 }
1315 buf[len] = NUL;
1316
Bram Moolenaar85a20022019-12-21 18:25:54 +01001317 if (*p != NUL && *p != ',') // skip non-standard separator
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 ++p;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001319 p = skip_to_option_part(p); // p points to next file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
1321 *option = p;
1322 return len;
1323}
1324
1325/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001326 * Replacement for free() that ignores NULL pointers.
1327 * Also skip free() when exiting for sure, this helps when we caught a deadly
1328 * signal that was caused by a crash in free().
Bram Moolenaard23a8232018-02-10 18:45:26 +01001329 * If you want to set NULL after calling this function, you should use
1330 * VIM_CLEAR() instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 */
1332 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001333vim_free(void *x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001335 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 {
1337#ifdef MEM_PROFILE
1338 mem_pre_free(&x);
1339#endif
1340 free(x);
1341 }
1342}
1343
1344#ifndef HAVE_MEMSET
1345 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001346vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
1348 char *p = ptr;
1349
1350 while (size-- > 0)
1351 *p++ = c;
1352 return ptr;
1353}
1354#endif
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356/*
1357 * Vim has its own isspace() function, because on some machines isspace()
1358 * can't handle characters above 128.
1359 */
1360 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001361vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362{
1363 return ((x >= 9 && x <= 13) || x == ' ');
1364}
1365
1366/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001367 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 */
1369
1370/*
1371 * Clear an allocated growing array.
1372 */
1373 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001374ga_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
1376 vim_free(gap->ga_data);
1377 ga_init(gap);
1378}
1379
1380/*
1381 * Clear a growing array that contains a list of strings.
1382 */
1383 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001384ga_clear_strings(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
1386 int i;
1387
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001388 if (gap->ga_data != NULL)
1389 for (i = 0; i < gap->ga_len; ++i)
1390 vim_free(((char_u **)(gap->ga_data))[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 ga_clear(gap);
1392}
1393
1394/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001395 * Copy a growing array that contains a list of strings.
1396 */
1397 int
1398ga_copy_strings(garray_T *from, garray_T *to)
1399{
1400 int i;
1401
1402 ga_init2(to, sizeof(char_u *), 1);
1403 if (ga_grow(to, from->ga_len) == FAIL)
1404 return FAIL;
1405
1406 for (i = 0; i < from->ga_len; ++i)
1407 {
1408 char_u *orig = ((char_u **)from->ga_data)[i];
1409 char_u *copy;
1410
1411 if (orig == NULL)
1412 copy = NULL;
1413 else
1414 {
1415 copy = vim_strsave(orig);
1416 if (copy == NULL)
1417 {
1418 to->ga_len = i;
1419 ga_clear_strings(to);
1420 return FAIL;
1421 }
1422 }
1423 ((char_u **)to->ga_data)[i] = copy;
1424 }
1425 to->ga_len = from->ga_len;
1426 return OK;
1427}
1428
1429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 * Initialize a growing array. Don't forget to set ga_itemsize and
1431 * ga_growsize! Or use ga_init2().
1432 */
1433 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001434ga_init(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435{
1436 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00001437 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 gap->ga_len = 0;
1439}
1440
1441 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001442ga_init2(garray_T *gap, int itemsize, int growsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443{
1444 ga_init(gap);
1445 gap->ga_itemsize = itemsize;
1446 gap->ga_growsize = growsize;
1447}
1448
1449/*
1450 * Make room in growing array "gap" for at least "n" items.
1451 * Return FAIL for failure, OK otherwise.
1452 */
1453 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001454ga_grow(garray_T *gap, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001456 if (gap->ga_maxlen - gap->ga_len < n)
1457 return ga_grow_inner(gap, n);
1458 return OK;
1459}
1460
1461 int
1462ga_grow_inner(garray_T *gap, int n)
1463{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01001464 size_t old_len;
1465 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 char_u *pp;
1467
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001468 if (n < gap->ga_growsize)
1469 n = gap->ga_growsize;
Bram Moolenaarc47ed442019-06-01 14:36:26 +02001470
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001471 // A linear growth is very inefficient when the array grows big. This
1472 // is a compromise between allocating memory that won't be used and too
1473 // many copy operations. A factor of 1.5 seems reasonable.
1474 if (n < gap->ga_len / 2)
1475 n = gap->ga_len / 2;
Bram Moolenaarc47ed442019-06-01 14:36:26 +02001476
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001477 new_len = gap->ga_itemsize * (gap->ga_len + n);
1478 pp = vim_realloc(gap->ga_data, new_len);
1479 if (pp == NULL)
1480 return FAIL;
1481 old_len = gap->ga_itemsize * gap->ga_maxlen;
1482 vim_memset(pp + old_len, 0, new_len - old_len);
1483 gap->ga_maxlen = gap->ga_len + n;
1484 gap->ga_data = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 return OK;
1486}
1487
Bram Moolenaar113e1072019-01-20 15:30:40 +01001488#if defined(FEAT_EVAL) || defined(FEAT_SEARCHPATH) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001490 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001491 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001492 * Returns NULL when out of memory.
1493 */
1494 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001495ga_concat_strings(garray_T *gap, char *sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001496{
1497 int i;
1498 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001499 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001500 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001501 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001502
1503 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001504 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001505
1506 s = alloc(len + 1);
1507 if (s != NULL)
1508 {
1509 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001510 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001511 for (i = 0; i < gap->ga_len; ++i)
1512 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001513 if (p != s)
1514 {
1515 STRCPY(p, sep);
1516 p += sep_len;
1517 }
1518 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
1519 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001520 }
1521 }
1522 return s;
1523}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001524#endif
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001525
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +01001526#if defined(FEAT_VIMINFO) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001527/*
1528 * Make a copy of string "p" and add it to "gap".
1529 * When out of memory nothing changes.
1530 */
1531 void
1532ga_add_string(garray_T *gap, char_u *p)
1533{
1534 char_u *cp = vim_strsave(p);
1535
1536 if (cp != NULL)
1537 {
1538 if (ga_grow(gap, 1) == OK)
1539 ((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
1540 else
1541 vim_free(cp);
1542 }
1543}
1544#endif
1545
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001546/*
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001547 * Concatenate a string to a growarray which contains bytes.
Bram Moolenaar43345542015-11-29 17:35:35 +01001548 * When "s" is NULL does not do anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 * Note: Does NOT copy the NUL at the end!
1550 */
1551 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001552ga_concat(garray_T *gap, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
Bram Moolenaar43345542015-11-29 17:35:35 +01001554 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
Bram Moolenaar478af672017-04-10 22:22:42 +02001556 if (s == NULL || *s == NUL)
Bram Moolenaar43345542015-11-29 17:35:35 +01001557 return;
1558 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (ga_grow(gap, len) == OK)
1560 {
1561 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
1562 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 }
1564}
1565
1566/*
1567 * Append one byte to a growarray which contains bytes.
1568 */
1569 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001570ga_append(garray_T *gap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
1572 if (ga_grow(gap, 1) == OK)
1573 {
1574 *((char *)gap->ga_data + gap->ga_len) = c;
1575 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 }
1577}
1578
Bram Moolenaar4f974752019-02-17 17:44:42 +01001579#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(MSWIN) \
Bram Moolenaar597a4222014-06-25 14:39:50 +02001580 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02001581/*
1582 * Append the text in "gap" below the cursor line and clear "gap".
1583 */
1584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001585append_ga_line(garray_T *gap)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02001586{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001587 // Remove trailing CR.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02001588 if (gap->ga_len > 0
1589 && !curbuf->b_p_bin
1590 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
1591 --gap->ga_len;
1592 ga_append(gap, NUL);
1593 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
1594 gap->ga_len = 0;
1595}
1596#endif
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598/************************************************************************
1599 * functions that use lookup tables for various things, generally to do with
1600 * special key codes.
1601 */
1602
1603/*
1604 * Some useful tables.
1605 */
1606
1607static struct modmasktable
1608{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001609 short mod_mask; // Bit-mask for particular key modifier
1610 short mod_flag; // Bit(s) for particular key modifier
1611 char_u name; // Single letter name of modifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612} mod_mask_table[] =
1613{
1614 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001615 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
1617 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
1618 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
1619 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
1620 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
Bram Moolenaard0573012017-10-28 21:11:06 +02001621#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
1623#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001624 // 'A' must be the last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
1626 {0, 0, NUL}
Bram Moolenaar85a20022019-12-21 18:25:54 +01001627 // NOTE: when adding an entry, update MAX_KEY_NAME_LEN!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628};
1629
1630/*
1631 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00001632 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 */
1634#define MOD_KEYS_ENTRY_SIZE 5
1635
1636static char_u modifier_keys_table[] =
1637{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001638// mod mask with modifier without modifier
1639 MOD_MASK_SHIFT, '&', '9', '@', '1', // begin
1640 MOD_MASK_SHIFT, '&', '0', '@', '2', // cancel
1641 MOD_MASK_SHIFT, '*', '1', '@', '4', // command
1642 MOD_MASK_SHIFT, '*', '2', '@', '5', // copy
1643 MOD_MASK_SHIFT, '*', '3', '@', '6', // create
1644 MOD_MASK_SHIFT, '*', '4', 'k', 'D', // delete char
1645 MOD_MASK_SHIFT, '*', '5', 'k', 'L', // delete line
1646 MOD_MASK_SHIFT, '*', '7', '@', '7', // end
1647 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', // end
1648 MOD_MASK_SHIFT, '*', '9', '@', '9', // exit
1649 MOD_MASK_SHIFT, '*', '0', '@', '0', // find
1650 MOD_MASK_SHIFT, '#', '1', '%', '1', // help
1651 MOD_MASK_SHIFT, '#', '2', 'k', 'h', // home
1652 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', // home
1653 MOD_MASK_SHIFT, '#', '3', 'k', 'I', // insert
1654 MOD_MASK_SHIFT, '#', '4', 'k', 'l', // left arrow
1655 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', // left arrow
1656 MOD_MASK_SHIFT, '%', 'a', '%', '3', // message
1657 MOD_MASK_SHIFT, '%', 'b', '%', '4', // move
1658 MOD_MASK_SHIFT, '%', 'c', '%', '5', // next
1659 MOD_MASK_SHIFT, '%', 'd', '%', '7', // options
1660 MOD_MASK_SHIFT, '%', 'e', '%', '8', // previous
1661 MOD_MASK_SHIFT, '%', 'f', '%', '9', // print
1662 MOD_MASK_SHIFT, '%', 'g', '%', '0', // redo
1663 MOD_MASK_SHIFT, '%', 'h', '&', '3', // replace
1664 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', // right arr.
1665 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', // right arr.
1666 MOD_MASK_SHIFT, '%', 'j', '&', '5', // resume
1667 MOD_MASK_SHIFT, '!', '1', '&', '6', // save
1668 MOD_MASK_SHIFT, '!', '2', '&', '7', // suspend
1669 MOD_MASK_SHIFT, '!', '3', '&', '8', // undo
1670 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', // up arrow
1671 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', // down arrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672
Bram Moolenaar85a20022019-12-21 18:25:54 +01001673 // vt100 F1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
1675 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
1676 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
1677 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
1678
Bram Moolenaar85a20022019-12-21 18:25:54 +01001679 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', // F1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
1681 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
1682 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
1683 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
1684 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
1685 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
1686 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
1687 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
Bram Moolenaar85a20022019-12-21 18:25:54 +01001688 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', // F10
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689
1690 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
1691 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
1692 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
1693 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
1694 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
1695 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
1696 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
1697 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
1698 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
1699 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
1700
1701 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
1702 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
1703 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
1704 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
1705 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
1706 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
1707 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
1708 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
1709 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
1710 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
1711
1712 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
1713 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
1714 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
1715 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
1716 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
1717 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
1718 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
1719
Bram Moolenaar85a20022019-12-21 18:25:54 +01001720 // TAB pseudo code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
1722
1723 NUL
1724};
1725
1726static struct key_name_entry
1727{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001728 int key; // Special key code or ascii value
1729 char_u *name; // Name of key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730} key_names_table[] =
1731{
1732 {' ', (char_u *)"Space"},
1733 {TAB, (char_u *)"Tab"},
1734 {K_TAB, (char_u *)"Tab"},
1735 {NL, (char_u *)"NL"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001736 {NL, (char_u *)"NewLine"}, // Alternative name
1737 {NL, (char_u *)"LineFeed"}, // Alternative name
1738 {NL, (char_u *)"LF"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 {CAR, (char_u *)"CR"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001740 {CAR, (char_u *)"Return"}, // Alternative name
1741 {CAR, (char_u *)"Enter"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 {K_BS, (char_u *)"BS"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001743 {K_BS, (char_u *)"BackSpace"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 {ESC, (char_u *)"Esc"},
1745 {CSI, (char_u *)"CSI"},
1746 {K_CSI, (char_u *)"xCSI"},
1747 {'|', (char_u *)"Bar"},
1748 {'\\', (char_u *)"Bslash"},
1749 {K_DEL, (char_u *)"Del"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001750 {K_DEL, (char_u *)"Delete"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 {K_KDEL, (char_u *)"kDel"},
1752 {K_UP, (char_u *)"Up"},
1753 {K_DOWN, (char_u *)"Down"},
1754 {K_LEFT, (char_u *)"Left"},
1755 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001756 {K_XUP, (char_u *)"xUp"},
1757 {K_XDOWN, (char_u *)"xDown"},
1758 {K_XLEFT, (char_u *)"xLeft"},
1759 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001760 {K_PS, (char_u *)"PasteStart"},
1761 {K_PE, (char_u *)"PasteEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762
1763 {K_F1, (char_u *)"F1"},
1764 {K_F2, (char_u *)"F2"},
1765 {K_F3, (char_u *)"F3"},
1766 {K_F4, (char_u *)"F4"},
1767 {K_F5, (char_u *)"F5"},
1768 {K_F6, (char_u *)"F6"},
1769 {K_F7, (char_u *)"F7"},
1770 {K_F8, (char_u *)"F8"},
1771 {K_F9, (char_u *)"F9"},
1772 {K_F10, (char_u *)"F10"},
1773
1774 {K_F11, (char_u *)"F11"},
1775 {K_F12, (char_u *)"F12"},
1776 {K_F13, (char_u *)"F13"},
1777 {K_F14, (char_u *)"F14"},
1778 {K_F15, (char_u *)"F15"},
1779 {K_F16, (char_u *)"F16"},
1780 {K_F17, (char_u *)"F17"},
1781 {K_F18, (char_u *)"F18"},
1782 {K_F19, (char_u *)"F19"},
1783 {K_F20, (char_u *)"F20"},
1784
1785 {K_F21, (char_u *)"F21"},
1786 {K_F22, (char_u *)"F22"},
1787 {K_F23, (char_u *)"F23"},
1788 {K_F24, (char_u *)"F24"},
1789 {K_F25, (char_u *)"F25"},
1790 {K_F26, (char_u *)"F26"},
1791 {K_F27, (char_u *)"F27"},
1792 {K_F28, (char_u *)"F28"},
1793 {K_F29, (char_u *)"F29"},
1794 {K_F30, (char_u *)"F30"},
1795
1796 {K_F31, (char_u *)"F31"},
1797 {K_F32, (char_u *)"F32"},
1798 {K_F33, (char_u *)"F33"},
1799 {K_F34, (char_u *)"F34"},
1800 {K_F35, (char_u *)"F35"},
1801 {K_F36, (char_u *)"F36"},
1802 {K_F37, (char_u *)"F37"},
1803
1804 {K_XF1, (char_u *)"xF1"},
1805 {K_XF2, (char_u *)"xF2"},
1806 {K_XF3, (char_u *)"xF3"},
1807 {K_XF4, (char_u *)"xF4"},
1808
1809 {K_HELP, (char_u *)"Help"},
1810 {K_UNDO, (char_u *)"Undo"},
1811 {K_INS, (char_u *)"Insert"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001812 {K_INS, (char_u *)"Ins"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 {K_KINS, (char_u *)"kInsert"},
1814 {K_HOME, (char_u *)"Home"},
1815 {K_KHOME, (char_u *)"kHome"},
1816 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001817 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {K_END, (char_u *)"End"},
1819 {K_KEND, (char_u *)"kEnd"},
1820 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001821 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 {K_PAGEUP, (char_u *)"PageUp"},
1823 {K_PAGEDOWN, (char_u *)"PageDown"},
1824 {K_KPAGEUP, (char_u *)"kPageUp"},
1825 {K_KPAGEDOWN, (char_u *)"kPageDown"},
1826
1827 {K_KPLUS, (char_u *)"kPlus"},
1828 {K_KMINUS, (char_u *)"kMinus"},
1829 {K_KDIVIDE, (char_u *)"kDivide"},
1830 {K_KMULTIPLY, (char_u *)"kMultiply"},
1831 {K_KENTER, (char_u *)"kEnter"},
1832 {K_KPOINT, (char_u *)"kPoint"},
1833
1834 {K_K0, (char_u *)"k0"},
1835 {K_K1, (char_u *)"k1"},
1836 {K_K2, (char_u *)"k2"},
1837 {K_K3, (char_u *)"k3"},
1838 {K_K4, (char_u *)"k4"},
1839 {K_K5, (char_u *)"k5"},
1840 {K_K6, (char_u *)"k6"},
1841 {K_K7, (char_u *)"k7"},
1842 {K_K8, (char_u *)"k8"},
1843 {K_K9, (char_u *)"k9"},
1844
1845 {'<', (char_u *)"lt"},
1846
1847 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001848#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001850#endif
1851#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001853#endif
1854#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001856#endif
1857#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001859#endif
1860#ifdef FEAT_MOUSE_URXVT
1861 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
1862#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001863 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
Bram Moolenaar21a83bd2021-02-23 19:19:58 +01001864 {K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelease"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
1866 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
1867 {K_LEFTDRAG, (char_u *)"LeftDrag"},
1868 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
1869 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001870 {K_MOUSEMOVE, (char_u *)"MouseMove"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
1872 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
1873 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
1874 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
1875 {K_RIGHTDRAG, (char_u *)"RightDrag"},
1876 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001877 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
1878 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
1879 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
1880 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001881 {K_MOUSEDOWN, (char_u *)"MouseDown"}, // OBSOLETE: Use
1882 {K_MOUSEUP, (char_u *)"MouseUp"}, // ScrollWheelXXX instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {K_X1MOUSE, (char_u *)"X1Mouse"},
1884 {K_X1DRAG, (char_u *)"X1Drag"},
1885 {K_X1RELEASE, (char_u *)"X1Release"},
1886 {K_X2MOUSE, (char_u *)"X2Mouse"},
1887 {K_X2DRAG, (char_u *)"X2Drag"},
1888 {K_X2RELEASE, (char_u *)"X2Release"},
1889 {K_DROP, (char_u *)"Drop"},
1890 {K_ZERO, (char_u *)"Nul"},
1891#ifdef FEAT_EVAL
1892 {K_SNR, (char_u *)"SNR"},
1893#endif
1894 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02001895 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar2f106582019-05-08 21:59:25 +02001896 {K_IGNORE, (char_u *)"Ignore"},
Bram Moolenaar957cf672020-11-12 14:21:06 +01001897 {K_COMMAND, (char_u *)"Cmd"},
Bram Moolenaarccb47a22021-01-21 13:36:43 +01001898 {K_FOCUSGAINED, (char_u *)"FocusGained"},
1899 {K_FOCUSLOST, (char_u *)"FocusLost"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {0, NULL}
Bram Moolenaar85a20022019-12-21 18:25:54 +01001901 // NOTE: When adding a long name update MAX_KEY_NAME_LEN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902};
1903
K.Takataeeec2542021-06-02 13:28:16 +02001904#define KEY_NAMES_TABLE_LEN ARRAY_LENGTH(key_names_table)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906/*
1907 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
1908 * modifier name ('S' for Shift, 'C' for Ctrl etc).
1909 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001910 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001911name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
1913 int i;
1914
1915 c = TOUPPER_ASC(c);
1916 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
1917 if (c == mod_mask_table[i].name)
1918 return mod_mask_table[i].mod_flag;
1919 return 0;
1920}
1921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922/*
1923 * Check if if there is a special key code for "key" that includes the
1924 * modifiers specified.
1925 */
1926 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001927simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
1929 int i;
1930 int key0;
1931 int key1;
1932
1933 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
1934 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001935 // TAB is a special case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
1937 {
1938 *modifiers &= ~MOD_MASK_SHIFT;
1939 return K_S_TAB;
1940 }
1941 key0 = KEY2TERMCAP0(key);
1942 key1 = KEY2TERMCAP1(key);
1943 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
1944 if (key0 == modifier_keys_table[i + 3]
1945 && key1 == modifier_keys_table[i + 4]
1946 && (*modifiers & modifier_keys_table[i]))
1947 {
1948 *modifiers &= ~modifier_keys_table[i];
1949 return TERMCAP2KEY(modifier_keys_table[i + 1],
1950 modifier_keys_table[i + 2]);
1951 }
1952 }
1953 return key;
1954}
1955
1956/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001957 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001958 */
1959 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001960handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001961{
1962 switch (key)
1963 {
1964 case K_XUP: return K_UP;
1965 case K_XDOWN: return K_DOWN;
1966 case K_XLEFT: return K_LEFT;
1967 case K_XRIGHT: return K_RIGHT;
1968 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001969 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001970 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001971 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001972 case K_XF1: return K_F1;
1973 case K_XF2: return K_F2;
1974 case K_XF3: return K_F3;
1975 case K_XF4: return K_F4;
1976 case K_S_XF1: return K_S_F1;
1977 case K_S_XF2: return K_S_F2;
1978 case K_S_XF3: return K_S_F3;
1979 case K_S_XF4: return K_S_F4;
1980 }
1981 return key;
1982}
1983
1984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 * Return a string which contains the name of the given key when the given
1986 * modifiers are down.
1987 */
1988 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001989get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 static char_u string[MAX_KEY_NAME_LEN + 1];
1992
1993 int i, idx;
1994 int table_idx;
1995 char_u *s;
1996
1997 string[0] = '<';
1998 idx = 1;
1999
Bram Moolenaar85a20022019-12-21 18:25:54 +01002000 // Key that stands for a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2002 c = KEY2TERMCAP1(c);
2003
2004 /*
2005 * Translate shifted special keys into unshifted keys and set modifier.
2006 * Same for CTRL and ALT modifiers.
2007 */
2008 if (IS_SPECIAL(c))
2009 {
2010 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2011 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2012 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2013 {
2014 modifiers |= modifier_keys_table[i];
2015 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2016 modifier_keys_table[i + 4]);
2017 break;
2018 }
2019 }
2020
Bram Moolenaar85a20022019-12-21 18:25:54 +01002021 // try to find the key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 table_idx = find_special_key_in_table(c);
2023
2024 /*
2025 * When not a known special key, and not a printable character, try to
2026 * extract modifiers.
2027 */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002028 if (c > 0 && (*mb_char2len)(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
2030 if (table_idx < 0
2031 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2032 && (c & 0x80))
2033 {
2034 c &= 0x7f;
2035 modifiers |= MOD_MASK_ALT;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002036 // try again, to find the un-alted key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 table_idx = find_special_key_in_table(c);
2038 }
2039 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2040 {
2041#ifdef EBCDIC
2042 c = CtrlChar(c);
2043#else
2044 c += '@';
2045#endif
2046 modifiers |= MOD_MASK_CTRL;
2047 }
2048 }
2049
Bram Moolenaar85a20022019-12-21 18:25:54 +01002050 // translate the modifier into a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2052 if ((modifiers & mod_mask_table[i].mod_mask)
2053 == mod_mask_table[i].mod_flag)
2054 {
2055 string[idx++] = mod_mask_table[i].name;
2056 string[idx++] = (char_u)'-';
2057 }
2058
Bram Moolenaar85a20022019-12-21 18:25:54 +01002059 if (table_idx < 0) // unknown special key, may output t_xx
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
2061 if (IS_SPECIAL(c))
2062 {
2063 string[idx++] = 't';
2064 string[idx++] = '_';
2065 string[idx++] = KEY2TERMCAP0(c);
2066 string[idx++] = KEY2TERMCAP1(c);
2067 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002068 // Not a special key, only modifiers, output directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 else
2070 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (has_mbyte && (*mb_char2len)(c) > 1)
2072 idx += (*mb_char2bytes)(c, string + idx);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002073 else if (vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 string[idx++] = c;
2075 else
2076 {
2077 s = transchar(c);
2078 while (*s)
2079 string[idx++] = *s++;
2080 }
2081 }
2082 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002083 else // use name of special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaar423977d2017-01-22 15:05:12 +01002085 size_t len = STRLEN(key_names_table[table_idx].name);
2086
2087 if (len + idx + 2 <= MAX_KEY_NAME_LEN)
2088 {
2089 STRCPY(string + idx, key_names_table[table_idx].name);
2090 idx += (int)len;
2091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093 string[idx++] = '>';
2094 string[idx] = NUL;
2095 return string;
2096}
2097
2098/*
2099 * Try translating a <> name at (*srcp)[] to dst[].
2100 * Return the number of characters added to dst[], zero for no match.
2101 * If there is a match, srcp is advanced to after the <> name.
2102 * dst[] must be big enough to hold the result (up to six characters)!
2103 */
2104 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002105trans_special(
2106 char_u **srcp,
2107 char_u *dst,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002108 int flags, // FSK_ values
2109 int *did_simplify) // FSK_SIMPLIFY and found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 int modifiers = 0;
2112 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002114 key = find_special_key(srcp, &modifiers, flags, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 if (key == 0)
2116 return 0;
2117
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002118 return special_to_buf(key, modifiers, flags & FSK_KEYCODE, dst);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002119}
2120
2121/*
2122 * Put the character sequence for "key" with "modifiers" into "dst" and return
2123 * the resulting length.
2124 * When "keycode" is TRUE prefer key code, e.g. K_DEL instead of DEL.
2125 * The sequence is not NUL terminated.
2126 * This is how characters in a string are encoded.
2127 */
2128 int
2129special_to_buf(int key, int modifiers, int keycode, char_u *dst)
2130{
2131 int dlen = 0;
2132
Bram Moolenaar85a20022019-12-21 18:25:54 +01002133 // Put the appropriate modifier in a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 if (modifiers != 0)
2135 {
2136 dst[dlen++] = K_SPECIAL;
2137 dst[dlen++] = KS_MODIFIER;
2138 dst[dlen++] = modifiers;
2139 }
2140
2141 if (IS_SPECIAL(key))
2142 {
2143 dst[dlen++] = K_SPECIAL;
2144 dst[dlen++] = KEY2TERMCAP0(key);
2145 dst[dlen++] = KEY2TERMCAP1(key);
2146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 else if (has_mbyte && !keycode)
2148 dlen += (*mb_char2bytes)(key, dst + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 else if (keycode)
2150 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2151 else
2152 dst[dlen++] = key;
2153
2154 return dlen;
2155}
2156
2157/*
2158 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2159 * srcp is advanced to after the <> name.
2160 * returns 0 if there is no match.
2161 */
2162 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002163find_special_key(
2164 char_u **srcp,
2165 int *modp,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002166 int flags, // FSK_ values
Bram Moolenaar459fd782019-10-13 16:43:39 +02002167 int *did_simplify) // found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168{
2169 char_u *last_dash;
2170 char_u *end_of_name;
2171 char_u *src;
2172 char_u *bp;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002173 int in_string = flags & FSK_IN_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 int modifiers;
2175 int bit;
2176 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002177 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002178 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
2180 src = *srcp;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002181 if (src[0] != '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 return 0;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002183 if (src[1] == '*') // <*xxx>: do not simplify
2184 ++src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185
Bram Moolenaar85a20022019-12-21 18:25:54 +01002186 // Find end of modifier list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 last_dash = src;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02002188 for (bp = src + 1; *bp == '-' || vim_isNormalIDc(*bp); bp++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
2190 if (*bp == '-')
2191 {
2192 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002193 if (bp[1] != NUL)
2194 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002195 if (has_mbyte)
2196 l = mb_ptr2len(bp + 1);
2197 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002198 l = 1;
Bram Moolenaarc8fd33d2019-08-16 20:33:05 +02002199 // Anything accepted, like <C-?>.
2200 // <C-"> or <M-"> are not special in strings as " is
2201 // the string delimiter. With a backslash it works: <M-\">
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002202 if (!(in_string && bp[1] == '"') && bp[l + 1] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02002203 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002204 else if (in_string && bp[1] == '\\' && bp[2] == '"'
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002205 && bp[3] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002206 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002210 bp += 3; // skip t_xx, xx may be '-' or '>'
Bram Moolenaar792826c2011-08-19 22:29:02 +02002211 else if (STRNICMP(bp, "char-", 5) == 0)
2212 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002213 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0, TRUE);
2214 if (l == 0)
2215 {
2216 emsg(_(e_invarg));
2217 return 0;
2218 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02002219 bp += l + 5;
2220 break;
2221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 }
2223
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002224 if (*bp == '>') // found matching '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
2226 end_of_name = bp + 1;
2227
Bram Moolenaar85a20022019-12-21 18:25:54 +01002228 // Which modifiers are given?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 modifiers = 0x0;
2230 for (bp = src + 1; bp < last_dash; bp++)
2231 {
2232 if (*bp != '-')
2233 {
2234 bit = name_to_mod_mask(*bp);
2235 if (bit == 0x0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002236 break; // Illegal modifier name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 modifiers |= bit;
2238 }
2239 }
2240
2241 /*
2242 * Legal modifier name.
2243 */
2244 if (bp >= last_dash)
2245 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002246 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2247 && VIM_ISDIGIT(last_dash[6]))
2248 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002249 // <Char-123> or <Char-033> or <Char-0x33>
Bram Moolenaar459fd782019-10-13 16:43:39 +02002250 vim_str2nr(last_dash + 6, NULL, &l, STR2NR_ALL, NULL,
2251 &n, 0, TRUE);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002252 if (l == 0)
2253 {
2254 emsg(_(e_invarg));
2255 return 0;
2256 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02002257 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002260 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002261 int off = 1;
2262
Bram Moolenaar85a20022019-12-21 18:25:54 +01002263 // Modifier with single letter, or special key name.
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002264 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
2265 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02002266 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002267 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002268 else
Bram Moolenaar792826c2011-08-19 22:29:02 +02002269 l = 1;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002270 if (modifiers != 0 && last_dash[l + off] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002271 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002272 else
2273 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002274 key = get_special_key_code(last_dash + off);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002275 if (!(flags & FSK_KEEP_X_KEY))
Bram Moolenaar792826c2011-08-19 22:29:02 +02002276 key = handle_x_keys(key);
2277 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279
2280 /*
2281 * get_special_key_code() may return NUL for invalid
2282 * special key name.
2283 */
2284 if (key != NUL)
2285 {
2286 /*
2287 * Only use a modifier when there is no special key code that
2288 * includes the modifier.
2289 */
2290 key = simplify_key(key, &modifiers);
2291
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002292 if (!(flags & FSK_KEYCODE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002294 // don't want keycode, use single byte code
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 if (key == K_BS)
2296 key = BS;
2297 else if (key == K_DEL || key == K_KDEL)
2298 key = DEL;
2299 }
2300
Bram Moolenaar459fd782019-10-13 16:43:39 +02002301 // Normal Key with modifier: Try to make a single byte code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 if (!IS_SPECIAL(key))
Bram Moolenaar459fd782019-10-13 16:43:39 +02002303 key = extract_modifiers(key, &modifiers,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002304 flags & FSK_SIMPLIFY, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
2306 *modp = modifiers;
2307 *srcp = end_of_name;
2308 return key;
2309 }
2310 }
2311 }
2312 return 0;
2313}
2314
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002315
2316/*
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02002317 * Some keys are used with Ctrl without Shift and are still expected to be
2318 * mapped as if Shift was pressed:
2319 * CTRL-2 is CTRL-@
2320 * CTRL-6 is CTRL-^
2321 * CTRL-- is CTRL-_
2322 * Also, <C-H> and <C-h> mean the same thing, always use "H".
2323 * Returns the possibly adjusted key.
2324 */
2325 int
2326may_adjust_key_for_ctrl(int modifiers, int key)
2327{
2328 if (modifiers & MOD_MASK_CTRL)
2329 {
2330 if (ASCII_ISALPHA(key))
2331 return TOUPPER_ASC(key);
2332 if (key == '2')
2333 return '@';
2334 if (key == '6')
2335 return '^';
2336 if (key == '-')
2337 return '_';
2338 }
2339 return key;
2340}
2341
2342/*
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002343 * Some keys already have Shift included, pass them as normal keys.
Bram Moolenaar9a033d72020-10-07 17:29:48 +02002344 * When Ctrl is also used <C-H> and <C-S-H> are different, but <C-S-{> should
2345 * be <C-{>. Same for <C-S-}> and <C-S-|>.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002346 * Also for <A-S-a> and <M-S-a>.
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02002347 * This includes all printable ASCII characters except numbers and a-z.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002348 */
2349 int
2350may_remove_shift_modifier(int modifiers, int key)
2351{
2352 if ((modifiers == MOD_MASK_SHIFT
2353 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
2354 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02002355 && ((key >= '!' && key <= '/')
2356 || (key >= ':' && key <= 'Z')
2357 || (key >= '[' && key <= '`')
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002358 || (key >= '{' && key <= '~')))
2359 return modifiers & ~MOD_MASK_SHIFT;
Bram Moolenaar9a033d72020-10-07 17:29:48 +02002360
2361 if (modifiers == (MOD_MASK_SHIFT | MOD_MASK_CTRL)
2362 && (key == '{' || key == '}' || key == '|'))
2363 return modifiers & ~MOD_MASK_SHIFT;
2364
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002365 return modifiers;
2366}
2367
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368/*
2369 * Try to include modifiers in the key.
2370 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
Bram Moolenaar459fd782019-10-13 16:43:39 +02002371 * When "simplify" is FALSE don't do Ctrl and Alt.
2372 * When "simplify" is TRUE and Ctrl or Alt is removed from modifiers set
2373 * "did_simplify" when it's not NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 */
2375 int
Bram Moolenaar459fd782019-10-13 16:43:39 +02002376extract_modifiers(int key, int *modp, int simplify, int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
2378 int modifiers = *modp;
2379
Bram Moolenaard0573012017-10-28 21:11:06 +02002380#ifdef MACOS_X
Bram Moolenaar459fd782019-10-13 16:43:39 +02002381 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (!(modifiers & MOD_MASK_CMD))
2383#endif
2384 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2385 {
2386 key = TOUPPER_ASC(key);
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002387 // With <C-S-a> we keep the shift modifier.
2388 // With <S-a>, <A-S-a> and <S-A> we don't keep the shift modifier.
2389 if (simplify || modifiers == MOD_MASK_SHIFT
2390 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
2391 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02002392 modifiers &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02002394
2395 // <C-H> and <C-h> mean the same thing, always use "H"
2396 if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key))
2397 key = TOUPPER_ASC(key);
2398
2399 if (simplify && (modifiers & MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400#ifdef EBCDIC
Bram Moolenaar459fd782019-10-13 16:43:39 +02002401 // TODO: EBCDIC Better use:
2402 // && (Ctrl_chr(key) || key == '?')
2403 // ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2405 != NULL
2406#else
2407 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2408#endif
2409 )
2410 {
2411 key = Ctrl_chr(key);
2412 modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002413 // <C-@> is <Nul>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (key == 0)
2415 key = K_ZERO;
Bram Moolenaar459fd782019-10-13 16:43:39 +02002416 if (did_simplify != NULL)
2417 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02002419
Bram Moolenaard0573012017-10-28 21:11:06 +02002420#ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01002421 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 if (!(modifiers & MOD_MASK_CMD))
2423#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +02002424 if (simplify && (modifiers & MOD_MASK_ALT) && key < 0x80
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002425 && !enc_dbcs) // avoid creating a lead byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
2427 key |= 0x80;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002428 modifiers &= ~MOD_MASK_ALT; // remove the META modifier
Bram Moolenaar459fd782019-10-13 16:43:39 +02002429 if (did_simplify != NULL)
2430 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 }
2432
2433 *modp = modifiers;
2434 return key;
2435}
2436
2437/*
2438 * Try to find key "c" in the special key table.
2439 * Return the index when found, -1 when not found.
2440 */
2441 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002442find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443{
2444 int i;
2445
2446 for (i = 0; key_names_table[i].name != NULL; i++)
2447 if (c == key_names_table[i].key)
2448 break;
2449 if (key_names_table[i].name == NULL)
2450 i = -1;
2451 return i;
2452}
2453
2454/*
2455 * Find the special key with the given name (the given string does not have to
2456 * end with NUL, the name is assumed to end before the first non-idchar).
2457 * If the name starts with "t_" the next two characters are interpreted as a
2458 * termcap name.
2459 * Return the key code, or 0 if not found.
2460 */
2461 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002462get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
2464 char_u *table_name;
2465 char_u string[3];
2466 int i, j;
2467
2468 /*
2469 * If it's <t_xx> we get the code for xx from the termcap
2470 */
2471 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
2472 {
2473 string[0] = name[2];
2474 string[1] = name[3];
2475 string[2] = NUL;
2476 if (add_termcap_entry(string, FALSE) == OK)
2477 return TERMCAP2KEY(name[2], name[3]);
2478 }
2479 else
2480 for (i = 0; key_names_table[i].name != NULL; i++)
2481 {
2482 table_name = key_names_table[i].name;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02002483 for (j = 0; vim_isNormalIDc(name[j]) && table_name[j] != NUL; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
2485 break;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02002486 if (!vim_isNormalIDc(name[j]) && table_name[j] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 return key_names_table[i].key;
2488 }
2489 return 0;
2490}
2491
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002493get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00002495 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 return NULL;
2497 return key_names_table[i].name;
2498}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500/*
2501 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
2502 */
2503 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002504get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505{
2506 int c = *buf->b_p_ff;
2507
2508 if (buf->b_p_bin || c == 'u')
2509 return EOL_UNIX;
2510 if (c == 'm')
2511 return EOL_MAC;
2512 return EOL_DOS;
2513}
2514
2515/*
2516 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
2517 * argument.
2518 */
2519 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002520get_fileformat_force(
2521 buf_T *buf,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002522 exarg_T *eap) // can be NULL!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524 int c;
2525
2526 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002527 c = eap->force_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 else
2529 {
2530 if ((eap != NULL && eap->force_bin != 0)
2531 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
2532 return EOL_UNIX;
2533 c = *buf->b_p_ff;
2534 }
2535 if (c == 'u')
2536 return EOL_UNIX;
2537 if (c == 'm')
2538 return EOL_MAC;
2539 return EOL_DOS;
2540}
2541
2542/*
2543 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
2544 * Sets both 'textmode' and 'fileformat'.
2545 * Note: Does _not_ set global value of 'textmode'!
2546 */
2547 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002548set_fileformat(
2549 int t,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002550 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 char *p = NULL;
2553
2554 switch (t)
2555 {
2556 case EOL_DOS:
2557 p = FF_DOS;
2558 curbuf->b_p_tx = TRUE;
2559 break;
2560 case EOL_UNIX:
2561 p = FF_UNIX;
2562 curbuf->b_p_tx = FALSE;
2563 break;
2564 case EOL_MAC:
2565 p = FF_MAC;
2566 curbuf->b_p_tx = FALSE;
2567 break;
2568 }
2569 if (p != NULL)
2570 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002571 OPT_FREE | opt_flags, 0);
2572
Bram Moolenaar85a20022019-12-21 18:25:54 +01002573 // This may cause the buffer to become (un)modified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002575 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576#ifdef FEAT_TITLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01002577 need_maketitle = TRUE; // set window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578#endif
2579}
2580
2581/*
2582 * Return the default fileformat from 'fileformats'.
2583 */
2584 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002585default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 switch (*p_ffs)
2588 {
2589 case 'm': return EOL_MAC;
2590 case 'd': return EOL_DOS;
2591 }
2592 return EOL_UNIX;
2593}
2594
2595/*
2596 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
2597 */
2598 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002599call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600{
2601 char_u *ncmd;
2602 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002603#ifdef FEAT_PROFILE
2604 proftime_T wait_time;
2605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606
2607 if (p_verbose > 3)
2608 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002609 verbose_enter();
Bram Moolenaar06f08532020-05-12 23:45:16 +02002610 smsg(_("Calling shell to execute: \"%s\""), cmd == NULL ? p_sh : cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 out_char('\n');
2612 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002613 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 }
2615
Bram Moolenaar05159a02005-02-26 23:04:13 +00002616#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00002617 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002618 prof_child_enter(&wait_time);
2619#endif
2620
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 if (*p_sh == NUL)
2622 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002623 emsg(_(e_shellempty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 retval = -1;
2625 }
2626 else
2627 {
2628#ifdef FEAT_GUI_MSWIN
Bram Moolenaar85a20022019-12-21 18:25:54 +01002629 // Don't hide the pointer while executing a shell command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 gui_mch_mousehide(FALSE);
2631#endif
2632#ifdef FEAT_GUI
2633 ++hold_gui_events;
2634#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002635 // The external command may update a tags file, clear cached tags.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 tag_freematch();
2637
Bram Moolenaar01257a72019-06-10 14:46:04 +02002638 if (cmd == NULL || *p_sxq == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 retval = mch_call_shell(cmd, opt);
2640 else
2641 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01002642 char_u *ecmd = cmd;
2643
Bram Moolenaar1a613392019-09-28 15:51:37 +02002644 if (*p_sxe != NUL && *p_sxq == '(')
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01002645 {
2646 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
2647 if (ecmd == NULL)
2648 ecmd = cmd;
2649 }
Bram Moolenaar964b3742019-05-24 18:54:09 +02002650 ncmd = alloc(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (ncmd != NULL)
2652 {
2653 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01002654 STRCAT(ncmd, ecmd);
Bram Moolenaar1a613392019-09-28 15:51:37 +02002655 // When 'shellxquote' is ( append ).
2656 // When 'shellxquote' is "( append )".
2657 STRCAT(ncmd, *p_sxq == '(' ? (char_u *)")"
2658 : *p_sxq == '"' && *(p_sxq+1) == '(' ? (char_u *)")\""
2659 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 retval = mch_call_shell(ncmd, opt);
2661 vim_free(ncmd);
2662 }
2663 else
2664 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01002665 if (ecmd != cmd)
2666 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
2668#ifdef FEAT_GUI
2669 --hold_gui_events;
2670#endif
2671 /*
2672 * Check the window size, in case it changed while executing the
2673 * external command.
2674 */
2675 shell_resized_check();
2676 }
2677
2678#ifdef FEAT_EVAL
2679 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002680# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00002681 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002682 prof_child_exit(&wait_time);
2683# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684#endif
2685
2686 return retval;
2687}
2688
2689/*
Bram Moolenaar01265852006-03-20 21:50:15 +00002690 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
2691 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 */
2693 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002694get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696 if (State & NORMAL)
2697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00002699 {
2700 if (VIsual_select)
2701 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00002703 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002704 else if (finish_op)
2705 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 }
2707 return State;
2708}
2709
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002710/*
2711 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02002712 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002713 * "b" must point to the start of the file name
2714 */
2715 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002716after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002717{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02002718 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002719 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
2720}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002721
2722/*
2723 * Return TRUE if file names "f1" and "f2" are in the same directory.
2724 * "f1" may be a short name, "f2" must be a full path.
2725 */
2726 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002727same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002728{
2729 char_u ffname[MAXPATHL];
2730 char_u *t1;
2731 char_u *t2;
2732
Bram Moolenaar85a20022019-12-21 18:25:54 +01002733 // safety check
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002734 if (f1 == NULL || f2 == NULL)
2735 return FALSE;
2736
2737 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
2738 t1 = gettail_sep(ffname);
2739 t2 = gettail_sep(f2);
2740 return (t1 - ffname == t2 - f2
2741 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
2742}
2743
Bram Moolenaar7c365fb2018-06-29 20:28:31 +02002744#if defined(FEAT_SESSION) || defined(FEAT_AUTOCHDIR) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002745 || defined(MSWIN) || defined(FEAT_GUI_GTK) \
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01002746 || defined(FEAT_NETBEANS_INTG) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 || defined(PROTO)
2748/*
2749 * Change to a file's directory.
2750 * Caller must call shorten_fnames()!
2751 * Return OK or FAIL.
2752 */
2753 int
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002754vim_chdirfile(char_u *fname, char *trigger_autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002756 char_u old_dir[MAXPATHL];
2757 char_u new_dir[MAXPATHL];
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01002758 int res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002760 if (mch_dirname(old_dir, MAXPATHL) != OK)
2761 *old_dir = NUL;
2762
2763 vim_strncpy(new_dir, fname, MAXPATHL - 1);
2764 *gettail_sep(new_dir) = NUL;
2765
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01002766 if (pathcmp((char *)old_dir, (char *)new_dir, -1) == 0)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002767 // nothing to do
2768 res = OK;
2769 else
2770 {
2771 res = mch_chdir((char *)new_dir) == 0 ? OK : FAIL;
2772
2773 if (res == OK && trigger_autocmd != NULL)
2774 apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd,
2775 new_dir, FALSE, curbuf);
2776 }
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01002777 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778}
2779#endif
2780
2781#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
2782/*
2783 * Check if "name" ends in a slash and is not a directory.
2784 * Used for systems where stat() ignores a trailing slash on a file name.
2785 * The Vim code assumes a trailing slash is only ignored for a directory.
2786 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002787 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01002788illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 if (name[0] == NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002791 return FALSE; // no file name is not illegal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (name[strlen(name) - 1] != '/')
Bram Moolenaar85a20022019-12-21 18:25:54 +01002793 return FALSE; // no trailing slash
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 if (mch_isdir((char_u *)name))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002795 return FALSE; // trailing slash for a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 return TRUE;
2797}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002798
2799/*
2800 * Special implementation of mch_stat() for Solaris.
2801 */
2802 int
2803vim_stat(const char *name, stat_T *stp)
2804{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002805 // On Solaris stat() accepts "file/" as if it was "file". Return -1 if
2806 // the name ends in "/" and it's not a directory.
Bram Moolenaard8492792017-03-16 12:22:38 +01002807 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002808}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809#endif
2810
2811#if defined(CURSOR_SHAPE) || defined(PROTO)
2812
2813/*
2814 * Handling of cursor and mouse pointer shapes in various modes.
2815 */
2816
2817cursorentry_T shape_table[SHAPE_IDX_COUNT] =
2818{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002819 // The values will be filled in from the 'guicursor' and 'mouseshape'
2820 // defaults when Vim starts.
2821 // Adjust the SHAPE_IDX_ defines when making changes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
2823 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
2824 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
2825 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
2826 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
2827 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
2828 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
2829 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
2830 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
2831 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
2832 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
2833 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
2834 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
2835 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
2836 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
2837 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
2838 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
2839};
2840
2841#ifdef FEAT_MOUSESHAPE
2842/*
2843 * Table with names for mouse shapes. Keep in sync with all the tables for
2844 * mch_set_mouse_shape()!.
2845 */
2846static char * mshape_names[] =
2847{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002848 "arrow", // default, must be the first one
2849 "blank", // hidden
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 "beam",
2851 "updown",
2852 "udsizing",
2853 "leftright",
2854 "lrsizing",
2855 "busy",
2856 "no",
2857 "crosshair",
2858 "hand1",
2859 "hand2",
2860 "pencil",
2861 "question",
2862 "rightup-arrow",
2863 "up-arrow",
2864 NULL
2865};
2866#endif
2867
2868/*
2869 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
2870 * ("what" is SHAPE_MOUSE).
2871 * Returns error message for an illegal option, NULL otherwise.
2872 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002873 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002874parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 char_u *modep;
2877 char_u *colonp;
2878 char_u *commap;
2879 char_u *slashp;
2880 char_u *p, *endp;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002881 int idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 int all_idx;
2883 int len;
2884 int i;
2885 long n;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002886 int found_ve = FALSE; // found "ve" flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 int round;
2888
2889 /*
2890 * First round: check for errors; second round: do it for real.
2891 */
2892 for (round = 1; round <= 2; ++round)
2893 {
2894 /*
2895 * Repeat for all comma separated parts.
2896 */
2897#ifdef FEAT_MOUSESHAPE
2898 if (what == SHAPE_MOUSE)
2899 modep = p_mouseshape;
2900 else
2901#endif
2902 modep = p_guicursor;
2903 while (*modep != NUL)
2904 {
2905 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01002906 commap = vim_strchr(modep, ',');
2907
2908 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002909 return N_("E545: Missing colon");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 if (colonp == modep)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002911 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
2913 /*
2914 * Repeat for all mode's before the colon.
2915 * For the 'a' mode, we loop to handle all the modes.
2916 */
2917 all_idx = -1;
2918 while (modep < colonp || all_idx >= 0)
2919 {
2920 if (all_idx < 0)
2921 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002922 // Find the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 if (modep[1] == '-' || modep[1] == ':')
2924 len = 1;
2925 else
2926 len = 2;
2927 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
2928 all_idx = SHAPE_IDX_COUNT - 1;
2929 else
2930 {
2931 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
2932 if (STRNICMP(modep, shape_table[idx].name, len)
2933 == 0)
2934 break;
2935 if (idx == SHAPE_IDX_COUNT
2936 || (shape_table[idx].used_for & what) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002937 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
2939 found_ve = TRUE;
2940 }
2941 modep += len + 1;
2942 }
2943
2944 if (all_idx >= 0)
2945 idx = all_idx--;
2946 else if (round == 2)
2947 {
2948#ifdef FEAT_MOUSESHAPE
2949 if (what == SHAPE_MOUSE)
2950 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002951 // Set the default, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 shape_table[idx].mshape = 0;
2953 }
2954 else
2955#endif
2956 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002957 // Set the defaults, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 shape_table[idx].shape = SHAPE_BLOCK;
2959 shape_table[idx].blinkwait = 700L;
2960 shape_table[idx].blinkon = 400L;
2961 shape_table[idx].blinkoff = 250L;
2962 }
2963 }
2964
Bram Moolenaar85a20022019-12-21 18:25:54 +01002965 // Parse the part after the colon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 for (p = colonp + 1; *p && *p != ','; )
2967 {
2968#ifdef FEAT_MOUSESHAPE
2969 if (what == SHAPE_MOUSE)
2970 {
2971 for (i = 0; ; ++i)
2972 {
2973 if (mshape_names[i] == NULL)
2974 {
2975 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002976 return N_("E547: Illegal mouseshape");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 if (round == 2)
2978 shape_table[idx].mshape =
2979 getdigits(&p) + MSHAPE_NUMBERED;
2980 else
2981 (void)getdigits(&p);
2982 break;
2983 }
2984 len = (int)STRLEN(mshape_names[i]);
2985 if (STRNICMP(p, mshape_names[i], len) == 0)
2986 {
2987 if (round == 2)
2988 shape_table[idx].mshape = i;
2989 p += len;
2990 break;
2991 }
2992 }
2993 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002994 else // if (what == SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995#endif
2996 {
2997 /*
2998 * First handle the ones with a number argument.
2999 */
3000 i = *p;
3001 len = 0;
3002 if (STRNICMP(p, "ver", 3) == 0)
3003 len = 3;
3004 else if (STRNICMP(p, "hor", 3) == 0)
3005 len = 3;
3006 else if (STRNICMP(p, "blinkwait", 9) == 0)
3007 len = 9;
3008 else if (STRNICMP(p, "blinkon", 7) == 0)
3009 len = 7;
3010 else if (STRNICMP(p, "blinkoff", 8) == 0)
3011 len = 8;
3012 if (len != 0)
3013 {
3014 p += len;
3015 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003016 return N_("E548: digit expected");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 n = getdigits(&p);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003018 if (len == 3) // "ver" or "hor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 {
3020 if (n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003021 return N_("E549: Illegal percentage");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 if (round == 2)
3023 {
3024 if (TOLOWER_ASC(i) == 'v')
3025 shape_table[idx].shape = SHAPE_VER;
3026 else
3027 shape_table[idx].shape = SHAPE_HOR;
3028 shape_table[idx].percentage = n;
3029 }
3030 }
3031 else if (round == 2)
3032 {
3033 if (len == 9)
3034 shape_table[idx].blinkwait = n;
3035 else if (len == 7)
3036 shape_table[idx].blinkon = n;
3037 else
3038 shape_table[idx].blinkoff = n;
3039 }
3040 }
3041 else if (STRNICMP(p, "block", 5) == 0)
3042 {
3043 if (round == 2)
3044 shape_table[idx].shape = SHAPE_BLOCK;
3045 p += 5;
3046 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003047 else // must be a highlight group name then
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 {
3049 endp = vim_strchr(p, '-');
Bram Moolenaar85a20022019-12-21 18:25:54 +01003050 if (commap == NULL) // last part
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 {
3052 if (endp == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003053 endp = p + STRLEN(p); // find end of part
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 }
3055 else if (endp > commap || endp == NULL)
3056 endp = commap;
3057 slashp = vim_strchr(p, '/');
3058 if (slashp != NULL && slashp < endp)
3059 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003060 // "group/langmap_group"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 i = syn_check_group(p, (int)(slashp - p));
3062 p = slashp + 1;
3063 }
3064 if (round == 2)
3065 {
3066 shape_table[idx].id = syn_check_group(p,
3067 (int)(endp - p));
3068 shape_table[idx].id_lm = shape_table[idx].id;
3069 if (slashp != NULL && slashp < endp)
3070 shape_table[idx].id = i;
3071 }
3072 p = endp;
3073 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003074 } // if (what != SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075
3076 if (*p == '-')
3077 ++p;
3078 }
3079 }
3080 modep = p;
3081 if (*modep == ',')
3082 ++modep;
3083 }
3084 }
3085
Bram Moolenaar85a20022019-12-21 18:25:54 +01003086 // If the 's' flag is not given, use the 'v' cursor for 's'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 if (!found_ve)
3088 {
3089#ifdef FEAT_MOUSESHAPE
3090 if (what == SHAPE_MOUSE)
3091 {
3092 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3093 }
3094 else
3095#endif
3096 {
3097 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3098 shape_table[SHAPE_IDX_VE].percentage =
3099 shape_table[SHAPE_IDX_V].percentage;
3100 shape_table[SHAPE_IDX_VE].blinkwait =
3101 shape_table[SHAPE_IDX_V].blinkwait;
3102 shape_table[SHAPE_IDX_VE].blinkon =
3103 shape_table[SHAPE_IDX_V].blinkon;
3104 shape_table[SHAPE_IDX_VE].blinkoff =
3105 shape_table[SHAPE_IDX_V].blinkoff;
3106 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3107 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3108 }
3109 }
3110
3111 return NULL;
3112}
3113
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003114# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3115 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116/*
3117 * Return the index into shape_table[] for the current mode.
3118 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3119 */
3120 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003121get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
3123#ifdef FEAT_MOUSESHAPE
3124 if (mouse && (State == HITRETURN || State == ASKMORE))
3125 {
3126# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003127 int x, y;
3128 gui_mch_getmouse(&x, &y);
3129 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 return SHAPE_IDX_MOREL;
3131# endif
3132 return SHAPE_IDX_MORE;
3133 }
3134 if (mouse && drag_status_line)
3135 return SHAPE_IDX_SDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 if (mouse && drag_sep_line)
3137 return SHAPE_IDX_VDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#endif
3139 if (!mouse && State == SHOWMATCH)
3140 return SHAPE_IDX_SM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (State & VREPLACE_FLAG)
3142 return SHAPE_IDX_R;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 if (State & REPLACE_FLAG)
3144 return SHAPE_IDX_R;
3145 if (State & INSERT)
3146 return SHAPE_IDX_I;
3147 if (State & CMDLINE)
3148 {
3149 if (cmdline_at_end())
3150 return SHAPE_IDX_C;
3151 if (cmdline_overstrike())
3152 return SHAPE_IDX_CR;
3153 return SHAPE_IDX_CI;
3154 }
3155 if (finish_op)
3156 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 if (VIsual_active)
3158 {
3159 if (*p_sel == 'e')
3160 return SHAPE_IDX_VE;
3161 else
3162 return SHAPE_IDX_V;
3163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 return SHAPE_IDX_N;
3165}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
3168# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3169static int old_mouse_shape = 0;
3170
3171/*
3172 * Set the mouse shape:
3173 * If "shape" is -1, use shape depending on the current mode,
3174 * depending on the current state.
3175 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3176 * when the mouse moves off the status or command line).
3177 */
3178 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003179update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
3181 int new_mouse_shape;
3182
Bram Moolenaar85a20022019-12-21 18:25:54 +01003183 // Only works in GUI mode.
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003184 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 return;
3186
Bram Moolenaar85a20022019-12-21 18:25:54 +01003187 // Postpone the updating when more is to come. Speeds up executing of
3188 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 if (shape_idx == -1 && char_avail())
3190 {
3191 postponed_mouseshape = TRUE;
3192 return;
3193 }
3194
Bram Moolenaar85a20022019-12-21 18:25:54 +01003195 // When ignoring the mouse don't change shape on the statusline.
Bram Moolenaar14716812006-05-04 21:54:08 +00003196 if (*p_mouse == NUL
3197 && (shape_idx == SHAPE_IDX_CLINE
3198 || shape_idx == SHAPE_IDX_STATUS
3199 || shape_idx == SHAPE_IDX_VSEP))
3200 shape_idx = -2;
3201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 if (shape_idx == -2
3203 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3204 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3205 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3206 return;
3207 if (shape_idx < 0)
3208 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3209 else
3210 new_mouse_shape = shape_table[shape_idx].mshape;
3211 if (new_mouse_shape != old_mouse_shape)
3212 {
3213 mch_set_mouse_shape(new_mouse_shape);
3214 old_mouse_shape = new_mouse_shape;
3215 }
3216 postponed_mouseshape = FALSE;
3217}
3218# endif
3219
Bram Moolenaar85a20022019-12-21 18:25:54 +01003220#endif // CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
3222
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223/*
3224 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
3225 * 'cdpath' for relative directory names, otherwise just mch_chdir().
3226 */
3227 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003228vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
3230#ifndef FEAT_SEARCHPATH
3231 return mch_chdir((char *)new_dir);
3232#else
3233 char_u *dir_name;
3234 int r;
3235
3236 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
3237 FNAME_MESS, curbuf->b_ffname);
3238 if (dir_name == NULL)
3239 return -1;
3240 r = mch_chdir((char *)dir_name);
3241 vim_free(dir_name);
3242 return r;
3243#endif
3244}
3245
3246/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003247 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003249 * Some systems are quite slow in obtaining the user name (Windows NT), thus
3250 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 * Returns OK or FAIL.
3252 */
3253 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003254get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003256 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 {
3258 if (mch_get_user_name(buf, len) == FAIL)
3259 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003263 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 return OK;
3265}
3266
3267#ifndef HAVE_QSORT
3268/*
3269 * Our own qsort(), for systems that don't have it.
3270 * It's simple and slow. From the K&R C book.
3271 */
3272 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003273qsort(
3274 void *base,
3275 size_t elm_count,
3276 size_t elm_size,
3277 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
3279 char_u *buf;
3280 char_u *p1;
3281 char_u *p2;
3282 int i, j;
3283 int gap;
3284
Bram Moolenaar964b3742019-05-24 18:54:09 +02003285 buf = alloc(elm_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (buf == NULL)
3287 return;
3288
3289 for (gap = elm_count / 2; gap > 0; gap /= 2)
3290 for (i = gap; i < elm_count; ++i)
3291 for (j = i - gap; j >= 0; j -= gap)
3292 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003293 // Compare the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 p1 = (char_u *)base + j * elm_size;
3295 p2 = (char_u *)base + (j + gap) * elm_size;
3296 if ((*cmp)((void *)p1, (void *)p2) <= 0)
3297 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003298 // Exchange the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 mch_memmove(buf, p1, elm_size);
3300 mch_memmove(p1, p2, elm_size);
3301 mch_memmove(p2, buf, elm_size);
3302 }
3303
3304 vim_free(buf);
3305}
3306#endif
3307
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 * The putenv() implementation below comes from the "screen" program.
3310 * Included with permission from Juergen Weigert.
3311 * See pty.c for the copyright notice.
3312 */
3313
3314/*
3315 * putenv -- put value into environment
3316 *
3317 * Usage: i = putenv (string)
3318 * int i;
3319 * char *string;
3320 *
3321 * where string is of the form <name>=<value>.
3322 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
3323 *
3324 * Putenv may need to add a new name into the environment, or to
3325 * associate a value longer than the current value with a particular
3326 * name. So, to make life simpler, putenv() copies your entire
3327 * environment into the heap (i.e. malloc()) from the stack
3328 * (i.e. where it resides when your process is initiated) the first
3329 * time you call it.
3330 *
3331 * (history removed, not very interesting. See the "screen" sources.)
3332 */
3333
3334#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
3335
Bram Moolenaar85a20022019-12-21 18:25:54 +01003336#define EXTRASIZE 5 // increment to add to env. size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaar85a20022019-12-21 18:25:54 +01003338static int envsize = -1; // current size of environment
3339extern char **environ; // the global which is your env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar85a20022019-12-21 18:25:54 +01003341static int findenv(char *name); // look for a name in the env.
3342static int newenv(void); // copy env. from stack to heap
3343static int moreenv(void); // incr. size of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
3345 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003346putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 int i;
3349 char *p;
3350
3351 if (envsize < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003352 { // first time putenv called
3353 if (newenv() < 0) // copy env. to heap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 return -1;
3355 }
3356
Bram Moolenaar85a20022019-12-21 18:25:54 +01003357 i = findenv((char *)string); // look for name in environment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
3359 if (i < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003360 { // name must be added
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 for (i = 0; environ[i]; i++);
3362 if (i >= (envsize - 1))
Bram Moolenaar85a20022019-12-21 18:25:54 +01003363 { // need new slot
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 if (moreenv() < 0)
3365 return -1;
3366 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003367 p = alloc(strlen(string) + 1);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003368 if (p == NULL) // not enough core
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 return -1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003370 environ[i + 1] = 0; // new end of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 }
3372 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003373 { // name already in env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 p = vim_realloc(environ[i], strlen(string) + 1);
3375 if (p == NULL)
3376 return -1;
3377 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003378 sprintf(p, "%s", string); // copy into env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 environ[i] = p;
3380
3381 return 0;
3382}
3383
3384 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003385findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386{
3387 char *namechar, *envchar;
3388 int i, found;
3389
3390 found = 0;
3391 for (i = 0; environ[i] && !found; i++)
3392 {
3393 envchar = environ[i];
3394 namechar = name;
3395 while (*namechar && *namechar != '=' && (*namechar == *envchar))
3396 {
3397 namechar++;
3398 envchar++;
3399 }
3400 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
3401 }
3402 return found ? i - 1 : -1;
3403}
3404
3405 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003406newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
3408 char **env, *elem;
3409 int i, esize;
3410
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 for (i = 0; environ[i]; i++)
3412 ;
Bram Moolenaard0573012017-10-28 21:11:06 +02003413
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 esize = i + EXTRASIZE + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003415 env = ALLOC_MULT(char *, esize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (env == NULL)
3417 return -1;
3418
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 for (i = 0; environ[i]; i++)
3420 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003421 elem = alloc(strlen(environ[i]) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 if (elem == NULL)
3423 return -1;
3424 env[i] = elem;
3425 strcpy(elem, environ[i]);
3426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
3428 env[i] = 0;
3429 environ = env;
3430 envsize = esize;
3431 return 0;
3432}
3433
3434 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003435moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
3437 int esize;
3438 char **env;
3439
3440 esize = envsize + EXTRASIZE;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003441 env = vim_realloc((char *)environ, esize * sizeof (*env));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (env == 0)
3443 return -1;
3444 environ = env;
3445 envsize = esize;
3446 return 0;
3447}
3448
3449# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02003450/*
3451 * Used for mch_getenv() for Mac.
3452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003454vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
3456 int i;
3457 char_u *p;
3458
3459 if (envsize < 0)
3460 return NULL;
3461
3462 i = findenv((char *)string);
3463
3464 if (i < 0)
3465 return NULL;
3466
3467 p = vim_strchr((char_u *)environ[i], '=');
3468 return (p + 1);
3469}
3470# endif
3471
Bram Moolenaar85a20022019-12-21 18:25:54 +01003472#endif // !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003473
Bram Moolenaarc4956c82006-03-12 21:58:43 +00003474#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003475/*
3476 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3477 * rights to write into.
3478 */
3479 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003480filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003481{
3482 int retval = 0;
3483#if defined(UNIX) || defined(VMS)
3484 int perm = 0;
3485#endif
3486
3487#if defined(UNIX) || defined(VMS)
3488 perm = mch_getperm(fname);
3489#endif
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003490 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01003491# ifdef MSWIN
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003492 mch_writable(fname) &&
3493# else
3494# if defined(UNIX) || defined(VMS)
3495 (perm & 0222) &&
3496# endif
3497# endif
3498 mch_access((char *)fname, W_OK) == 0
3499 )
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003500 {
3501 ++retval;
3502 if (mch_isdir(fname))
3503 ++retval;
3504 }
3505 return retval;
3506}
3507#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00003508
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003509#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
3510/*
3511 * Read 2 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003512 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003513 */
3514 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003515get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003516{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003517 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003518
3519 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003520 if (n == EOF) return -1;
3521 c = getc(fd);
3522 if (c == EOF) return -1;
3523 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003524}
3525
3526/*
3527 * Read 3 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003528 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003529 */
3530 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003531get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003532{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003533 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003534
3535 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003536 if (n == EOF) return -1;
3537 c = getc(fd);
3538 if (c == EOF) return -1;
3539 n = (n << 8) + c;
3540 c = getc(fd);
3541 if (c == EOF) return -1;
3542 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003543}
3544
3545/*
3546 * Read 4 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003547 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003548 */
3549 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003550get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003551{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003552 int c;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003553 // Use unsigned rather than int otherwise result is undefined
3554 // when left-shift sets the MSB.
Bram Moolenaar95235e62013-09-08 16:07:07 +02003555 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003556
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003557 c = getc(fd);
3558 if (c == EOF) return -1;
3559 n = (unsigned)c;
3560 c = getc(fd);
3561 if (c == EOF) return -1;
3562 n = (n << 8) + (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;
Bram Moolenaar95235e62013-09-08 16:07:07 +02003569 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003570}
3571
3572/*
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003573 * Read a string of length "cnt" from "fd" into allocated memory.
3574 * Returns NULL when out of memory or unable to read that many bytes.
3575 */
3576 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003577read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003578{
3579 char_u *str;
3580 int i;
3581 int c;
3582
Bram Moolenaar85a20022019-12-21 18:25:54 +01003583 // allocate memory
Bram Moolenaar964b3742019-05-24 18:54:09 +02003584 str = alloc(cnt + 1);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003585 if (str != NULL)
3586 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003587 // Read the string. Quit when running into the EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003588 for (i = 0; i < cnt; ++i)
3589 {
3590 c = getc(fd);
3591 if (c == EOF)
3592 {
3593 vim_free(str);
3594 return NULL;
3595 }
3596 str[i] = c;
3597 }
3598 str[i] = NUL;
3599 }
3600 return str;
3601}
3602
3603/*
3604 * Write a number to file "fd", MSB first, in "len" bytes.
3605 */
3606 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003607put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003608{
3609 int i;
3610
3611 for (i = len - 1; i >= 0; --i)
3612 if (putc((int)(nr >> (i * 8)), fd) == EOF)
3613 return FAIL;
3614 return OK;
3615}
3616
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003617#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003618
Bram Moolenaar85a20022019-12-21 18:25:54 +01003619#ifndef PROTO // proto is defined in vim.h
Bram Moolenaar51628222016-12-01 23:03:28 +01003620# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003621/*
3622 * Return time in msec since "start_tv".
3623 */
3624 long
3625elapsed(struct timeval *start_tv)
3626{
3627 struct timeval now_tv;
3628
3629 gettimeofday(&now_tv, NULL);
3630 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
3631 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
3632}
Bram Moolenaar51628222016-12-01 23:03:28 +01003633# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003634
Bram Moolenaar51628222016-12-01 23:03:28 +01003635# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003636/*
3637 * Return time in msec since "start_tick".
3638 */
3639 long
3640elapsed(DWORD start_tick)
3641{
3642 DWORD now = GetTickCount();
3643
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003644 return (long)now - (long)start_tick;
3645}
Bram Moolenaar51628222016-12-01 23:03:28 +01003646# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003647#endif
Bram Moolenaar20608922018-04-21 22:30:08 +02003648
3649#if defined(FEAT_JOB_CHANNEL) \
3650 || (defined(UNIX) && (!defined(USE_SYSTEM) \
3651 || (defined(FEAT_GUI) && defined(FEAT_TERMINAL)))) \
3652 || defined(PROTO)
3653/*
3654 * Parse "cmd" and put the white-separated parts in "argv".
3655 * "argv" is an allocated array with "argc" entries and room for 4 more.
3656 * Returns FAIL when out of memory.
3657 */
3658 int
3659mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
3660{
3661 int i;
3662 char_u *p, *d;
3663 int inquote;
3664
3665 /*
3666 * Do this loop twice:
3667 * 1: find number of arguments
3668 * 2: separate them and build argv[]
3669 */
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003670 for (i = 1; i <= 2; ++i)
Bram Moolenaar20608922018-04-21 22:30:08 +02003671 {
3672 p = skipwhite(cmd);
3673 inquote = FALSE;
3674 *argc = 0;
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003675 while (*p != NUL)
Bram Moolenaar20608922018-04-21 22:30:08 +02003676 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003677 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003678 (*argv)[*argc] = (char *)p;
3679 ++*argc;
3680 d = p;
3681 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
3682 {
3683 if (p[0] == '"')
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02003684 // quotes surrounding an argument and are dropped
Bram Moolenaar20608922018-04-21 22:30:08 +02003685 inquote = !inquote;
3686 else
3687 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02003688 if (rem_backslash(p))
Bram Moolenaar20608922018-04-21 22:30:08 +02003689 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02003690 // First pass: skip over "\ " and "\"".
3691 // Second pass: Remove the backslash.
Bram Moolenaar20608922018-04-21 22:30:08 +02003692 ++p;
3693 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003694 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003695 *d++ = *p;
3696 }
3697 ++p;
3698 }
3699 if (*p == NUL)
3700 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003701 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003702 *d++ = NUL;
3703 break;
3704 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003705 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003706 *d++ = NUL;
3707 p = skipwhite(p + 1);
3708 }
3709 if (*argv == NULL)
3710 {
3711 if (use_shcf)
3712 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003713 // Account for possible multiple args in p_shcf.
Bram Moolenaar20608922018-04-21 22:30:08 +02003714 p = p_shcf;
3715 for (;;)
3716 {
3717 p = skiptowhite(p);
3718 if (*p == NUL)
3719 break;
3720 ++*argc;
3721 p = skipwhite(p);
3722 }
3723 }
3724
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003725 *argv = ALLOC_MULT(char *, *argc + 4);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003726 if (*argv == NULL) // out of memory
Bram Moolenaar20608922018-04-21 22:30:08 +02003727 return FAIL;
3728 }
3729 }
3730 return OK;
3731}
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003732
3733# if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
3734/*
3735 * Build "argv[argc]" from the string "cmd".
3736 * "argv[argc]" is set to NULL;
3737 * Return FAIL when out of memory.
3738 */
3739 int
3740build_argv_from_string(char_u *cmd, char ***argv, int *argc)
3741{
3742 char_u *cmd_copy;
3743 int i;
3744
Bram Moolenaar85a20022019-12-21 18:25:54 +01003745 // Make a copy, parsing will modify "cmd".
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003746 cmd_copy = vim_strsave(cmd);
3747 if (cmd_copy == NULL
3748 || mch_parse_cmd(cmd_copy, FALSE, argv, argc) == FAIL)
3749 {
3750 vim_free(cmd_copy);
3751 return FAIL;
3752 }
3753 for (i = 0; i < *argc; i++)
3754 (*argv)[i] = (char *)vim_strsave((char_u *)(*argv)[i]);
3755 (*argv)[*argc] = NULL;
3756 vim_free(cmd_copy);
3757 return OK;
3758}
3759
3760/*
3761 * Build "argv[argc]" from the list "l".
3762 * "argv[argc]" is set to NULL;
3763 * Return FAIL when out of memory.
3764 */
3765 int
3766build_argv_from_list(list_T *l, char ***argv, int *argc)
3767{
3768 listitem_T *li;
3769 char_u *s;
3770
Bram Moolenaar85a20022019-12-21 18:25:54 +01003771 // Pass argv[] to mch_call_shell().
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003772 *argv = ALLOC_MULT(char *, l->lv_len + 1);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003773 if (*argv == NULL)
3774 return FAIL;
3775 *argc = 0;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003776 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003777 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003778 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003779 if (s == NULL)
3780 {
3781 int i;
3782
3783 for (i = 0; i < *argc; ++i)
Bram Moolenaardf195602020-04-13 18:13:33 +02003784 VIM_CLEAR((*argv)[i]);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003785 return FAIL;
3786 }
3787 (*argv)[*argc] = (char *)vim_strsave(s);
3788 *argc += 1;
3789 }
3790 (*argv)[*argc] = NULL;
3791 return OK;
3792}
3793# endif
Bram Moolenaar20608922018-04-21 22:30:08 +02003794#endif
Bram Moolenaar57da6982019-09-13 22:30:11 +02003795
3796/*
3797 * Change the behavior of vterm.
3798 * 0: As usual.
3799 * 1: Windows 10 version 1809
3800 * The bug causes unstable handling of ambiguous width character.
Bram Moolenaar36e7a822019-11-13 21:49:24 +01003801 * 2: Windows 10 version 1903 & 1909
Bram Moolenaar57da6982019-09-13 22:30:11 +02003802 * Use the wrong result because each result is different.
3803 * 3: Windows 10 insider preview (current latest logic)
3804 */
3805 int
3806get_special_pty_type(void)
3807{
3808#ifdef MSWIN
3809 return get_conpty_type();
3810#else
3811 return 0;
3812#endif
3813}