blob: 2e83d2dd171aac243e37996695233ba8bf38f351 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc2.c: Various functions.
12 */
13#include "vim.h"
14
Bram Moolenaar85a20022019-12-21 18:25:54 +010015static char_u *username = NULL; // cached result of mch_get_user_name()
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000016
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010017static int coladvance2(pos_T *pos, int addspaces, int finetune, colnr_T wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
19/*
20 * Return TRUE if in the current mode we need to use virtual.
21 */
22 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010023virtual_active(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024{
Gary Johnson53ba05b2021-07-26 22:19:10 +020025 unsigned int cur_ve_flags = get_ve_flags();
26
Bram Moolenaar85a20022019-12-21 18:25:54 +010027 // While an operator is being executed we return "virtual_op", because
28 // VIsual_active has already been reset, thus we can't check for "block"
29 // being used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 if (virtual_op != MAYBE)
31 return virtual_op;
Gary Johnson53ba05b2021-07-26 22:19:10 +020032 return (cur_ve_flags == VE_ALL
33 || ((cur_ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V)
34 || ((cur_ve_flags & VE_INSERT) && (State & INSERT)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000035}
36
37/*
38 * Get the screen position of the cursor.
39 */
40 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010041getviscol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000042{
43 colnr_T x;
44
45 getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL);
46 return (int)x;
47}
48
49/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +000050 * Go to column "wcol", and add/insert white space as necessary to get the
Bram Moolenaar071d4272004-06-13 20:20:40 +000051 * cursor in that column.
52 * The caller must have saved the cursor line for undo!
53 */
54 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010055coladvance_force(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000056{
57 int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
58
59 if (wcol == MAXCOL)
60 curwin->w_valid &= ~VALID_VIRTCOL;
61 else
62 {
Bram Moolenaar85a20022019-12-21 18:25:54 +010063 // Virtcol is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 curwin->w_valid |= VALID_VIRTCOL;
65 curwin->w_virtcol = wcol;
66 }
67 return rc;
68}
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
70/*
Bram Moolenaar977239e2019-01-11 16:16:01 +010071 * Get the screen position of character col with a coladd in the cursor line.
72 */
73 int
Bram Moolenaarfc3abf42019-01-24 15:54:21 +010074getviscol2(colnr_T col, colnr_T coladd UNUSED)
Bram Moolenaar977239e2019-01-11 16:16:01 +010075{
76 colnr_T x;
77 pos_T pos;
78
79 pos.lnum = curwin->w_cursor.lnum;
80 pos.col = col;
Bram Moolenaar977239e2019-01-11 16:16:01 +010081 pos.coladd = coladd;
Bram Moolenaar977239e2019-01-11 16:16:01 +010082 getvvcol(curwin, &pos, &x, NULL, NULL);
83 return (int)x;
84}
85
86/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 * Try to advance the Cursor to the specified screen column.
88 * If virtual editing: fine tune the cursor position.
89 * Note that all virtual positions off the end of a line should share
90 * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)),
91 * beginning at coladd 0.
92 *
93 * return OK if desired column is reached, FAIL if not
94 */
95 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010096coladvance(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000097{
98 int rc = getvpos(&curwin->w_cursor, wcol);
99
100 if (wcol == MAXCOL || rc == FAIL)
101 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000102 else if (*ml_get_cursor() != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100104 // Virtcol is valid when not on a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 curwin->w_valid |= VALID_VIRTCOL;
106 curwin->w_virtcol = wcol;
107 }
108 return rc;
109}
110
111/*
112 * Return in "pos" the position of the cursor advanced to screen column "wcol".
113 * return OK if desired column is reached, FAIL if not
114 */
115 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100116getvpos(pos_T *pos, colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 return coladvance2(pos, FALSE, virtual_active(), wcol);
119}
120
121 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100122coladvance2(
123 pos_T *pos,
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200124 int addspaces, // change the text to achieve our goal?
125 int finetune, // change char offset for the exact column
126 colnr_T wcol_arg) // column to move to (can be negative)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127{
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200128 colnr_T wcol = wcol_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 int idx;
130 char_u *ptr;
131 char_u *line;
132 colnr_T col = 0;
133 int csize = 0;
134 int one_more;
135#ifdef FEAT_LINEBREAK
136 int head = 0;
137#endif
138
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000139 one_more = (State & INSERT)
140 || restart_edit != NUL
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000141 || (VIsual_active && *p_sel != 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +0200142 || ((get_ve_flags() & VE_ONEMORE) && wcol < MAXCOL);
Bram Moolenaara1381de2009-11-03 15:44:21 +0000143 line = ml_get_buf(curbuf, pos->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145 if (wcol >= MAXCOL)
146 {
147 idx = (int)STRLEN(line) - 1 + one_more;
148 col = wcol;
149
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 if ((addspaces || finetune) && !VIsual_active)
151 {
152 curwin->w_curswant = linetabsize(line) + one_more;
153 if (curwin->w_curswant > 0)
154 --curwin->w_curswant;
155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 }
157 else
158 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200159 int width = curwin->w_width - win_col_off(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160
Bram Moolenaarebefac62005-12-28 22:39:57 +0000161 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 && curwin->w_p_wrap
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 && curwin->w_width != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 && wcol >= (colnr_T)width)
165 {
166 csize = linetabsize(line);
167 if (csize > 0)
168 csize--;
169
Bram Moolenaarebefac62005-12-28 22:39:57 +0000170 if (wcol / width > (colnr_T)csize / width
171 && ((State & INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100173 // In case of line wrapping don't move the cursor beyond the
174 // right screen edge. In Insert mode allow going just beyond
175 // the last character (like what happens when typing and
176 // reaching the right window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 wcol = (csize / width + 1) * width - 1;
178 }
179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 ptr = line;
182 while (col <= wcol && *ptr != NUL)
183 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100184 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifdef FEAT_LINEBREAK
Bram Moolenaar597a4222014-06-25 14:39:50 +0200186 csize = win_lbr_chartabsize(curwin, line, ptr, col, &head);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100187 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200189 csize = lbr_chartabsize_adv(line, &ptr, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#endif
191 col += csize;
192 }
193 idx = (int)(ptr - line);
194 /*
195 * Handle all the special cases. The virtual_active() check
196 * is needed to ensure that a virtual position off the end of
197 * a line has the correct indexing. The one_more comparison
198 * replaces an explicit add of one_more later on.
199 */
200 if (col > wcol || (!virtual_active() && one_more == 0))
201 {
202 idx -= 1;
203# ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +0100204 // Don't count the chars from 'showbreak'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 csize -= head;
206# endif
207 col -= csize;
208 }
209
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 if (virtual_active()
211 && addspaces
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200212 && wcol >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 && ((col != wcol && col != wcol + 1) || csize > 1))
214 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100215 // 'virtualedit' is set: The difference between wcol and col is
216 // filled with spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
218 if (line[idx] == NUL)
219 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100220 // Append spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 int correct = wcol - col;
222 char_u *newline = alloc(idx + correct + 1);
223 int t;
224
225 if (newline == NULL)
226 return FAIL;
227
228 for (t = 0; t < idx; ++t)
229 newline[t] = line[t];
230
231 for (t = 0; t < correct; ++t)
232 newline[t + idx] = ' ';
233
234 newline[idx + correct] = NUL;
235
236 ml_replace(pos->lnum, newline, FALSE);
237 changed_bytes(pos->lnum, (colnr_T)idx);
238 idx += correct;
239 col = wcol;
240 }
241 else
242 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100243 // Break a tab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 int linelen = (int)STRLEN(line);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100245 int correct = wcol - col - csize + 1; // negative!!
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000246 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 int t, s = 0;
248 int v;
249
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000250 if (-correct > csize)
251 return FAIL;
252
253 newline = alloc(linelen + csize);
254 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 return FAIL;
256
257 for (t = 0; t < linelen; t++)
258 {
259 if (t != idx)
260 newline[s++] = line[t];
261 else
262 for (v = 0; v < csize; v++)
263 newline[s++] = ' ';
264 }
265
266 newline[linelen + csize - 1] = NUL;
267
268 ml_replace(pos->lnum, newline, FALSE);
269 changed_bytes(pos->lnum, idx);
270 idx += (csize - 1 + correct);
271 col += correct;
272 }
273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 }
275
276 if (idx < 0)
277 pos->col = 0;
278 else
279 pos->col = idx;
280
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 pos->coladd = 0;
282
283 if (finetune)
284 {
285 if (wcol == MAXCOL)
286 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100287 // The width of the last character is used to set coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 if (!one_more)
289 {
290 colnr_T scol, ecol;
291
292 getvcol(curwin, pos, &scol, NULL, &ecol);
293 pos->coladd = ecol - scol;
294 }
295 }
296 else
297 {
298 int b = (int)wcol - (int)col;
299
Bram Moolenaar85a20022019-12-21 18:25:54 +0100300 // The difference between wcol and col is used to set coladd.
Bram Moolenaar02631462017-09-22 15:20:32 +0200301 if (b > 0 && b < (MAXCOL - 2 * curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 pos->coladd = b;
303
304 col += b;
305 }
306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307
Bram Moolenaar85a20022019-12-21 18:25:54 +0100308 // prevent from moving onto a trail byte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200310 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200312 if (wcol < 0 || col < wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 return FAIL;
314 return OK;
315}
316
317/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000318 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 */
320 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100321inc_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322{
323 return inc(&curwin->w_cursor);
324}
325
Bram Moolenaar446cb832008-06-24 21:56:24 +0000326/*
327 * Increment the line pointer "lp" crossing line boundaries as necessary.
328 * Return 1 when going to the next line.
329 * Return 2 when moving forward onto a NUL at the end of the line).
330 * Return -1 when at the end of file.
331 * Return 0 otherwise.
332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100334inc(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100336 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
Bram Moolenaar85a20022019-12-21 18:25:54 +0100338 // when searching position may be set to end of a line
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100339 if (lp->col != MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100341 p = ml_get_pos(lp);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100342 if (*p != NUL) // still within line, move to next char (may be NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100344 if (has_mbyte)
345 {
346 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100348 lp->col += l;
349 return ((p[l] != NUL) ? 0 : 2);
350 }
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100351 lp->col++;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100352 lp->coladd = 0;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100353 return ((p[1] != NUL) ? 0 : 2);
354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100356 if (lp->lnum != curbuf->b_ml.ml_line_count) // there is a next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {
358 lp->col = 0;
359 lp->lnum++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 lp->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 return 1;
362 }
363 return -1;
364}
365
366/*
367 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
368 */
369 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100370incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371{
372 int r;
373
374 if ((r = inc(lp)) >= 1 && lp->col)
375 r = inc(lp);
376 return r;
377}
378
379/*
380 * dec(p)
381 *
382 * Decrement the line pointer 'p' crossing line boundaries as necessary.
383 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
384 */
385 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100386dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100388 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389}
390
391 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100392dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393{
394 char_u *p;
395
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 lp->coladd = 0;
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100397 if (lp->col == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100399 // past end of line
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100400 p = ml_get(lp->lnum);
401 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100402 if (has_mbyte)
403 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100404 return 0;
405 }
406
407 if (lp->col > 0)
408 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100409 // still within line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 lp->col--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 if (has_mbyte)
412 {
413 p = ml_get(lp->lnum);
414 lp->col -= (*mb_head_off)(p, p + lp->col);
415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 return 0;
417 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100418
419 if (lp->lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100421 // there is a prior line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 lp->lnum--;
423 p = ml_get(lp->lnum);
424 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 if (has_mbyte)
426 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 return 1;
428 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100429
Bram Moolenaar85a20022019-12-21 18:25:54 +0100430 // at start of file
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100431 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432}
433
434/*
435 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
436 */
437 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100438decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439{
440 int r;
441
442 if ((r = dec(lp)) == 1 && lp->col)
443 r = dec(lp);
444 return r;
445}
446
447/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200448 * Get the line number relative to the current cursor position, i.e. the
449 * difference between line number and cursor position. Only look for lines that
450 * can be visible, folded lines don't count.
451 */
452 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100453get_cursor_rel_lnum(
454 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100455 linenr_T lnum) // line number to get the result for
Bram Moolenaar64486672010-05-16 15:46:46 +0200456{
457 linenr_T cursor = wp->w_cursor.lnum;
458 linenr_T retval = 0;
459
460#ifdef FEAT_FOLDING
461 if (hasAnyFolding(wp))
462 {
463 if (lnum > cursor)
464 {
465 while (lnum > cursor)
466 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100467 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100468 // if lnum and cursor are in the same fold,
469 // now lnum <= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200470 if (lnum > cursor)
471 retval++;
472 lnum--;
473 }
474 }
475 else if (lnum < cursor)
476 {
477 while (lnum < cursor)
478 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100479 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100480 // if lnum and cursor are in the same fold,
481 // now lnum >= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200482 if (lnum < cursor)
483 retval--;
484 lnum++;
485 }
486 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100487 // else if (lnum == cursor)
488 // retval = 0;
Bram Moolenaar64486672010-05-16 15:46:46 +0200489 }
490 else
491#endif
492 retval = lnum - cursor;
493
494 return retval;
495}
496
497/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200498 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
499 * This allows for the col to be on the NUL byte.
500 */
501 void
502check_pos(buf_T *buf, pos_T *pos)
503{
504 char_u *line;
505 colnr_T len;
506
507 if (pos->lnum > buf->b_ml.ml_line_count)
508 pos->lnum = buf->b_ml.ml_line_count;
509
510 if (pos->col > 0)
511 {
512 line = ml_get_buf(buf, pos->lnum, FALSE);
513 len = (colnr_T)STRLEN(line);
514 if (pos->col > len)
515 pos->col = len;
516 }
517}
518
519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 * Make sure curwin->w_cursor.lnum is valid.
521 */
522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100523check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
526 {
527#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100528 // If there is a closed fold at the end of the file, put the cursor in
529 // its first line. Otherwise in the last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 if (!hasFolding(curbuf->b_ml.ml_line_count,
531 &curwin->w_cursor.lnum, NULL))
532#endif
533 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
534 }
535 if (curwin->w_cursor.lnum <= 0)
536 curwin->w_cursor.lnum = 1;
537}
538
539/*
540 * Make sure curwin->w_cursor.col is valid.
541 */
542 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100543check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200545 check_cursor_col_win(curwin);
546}
547
548/*
549 * Make sure win->w_cursor.col is valid.
550 */
551 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100552check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200553{
Gary Johnson53ba05b2021-07-26 22:19:10 +0200554 colnr_T len;
555 colnr_T oldcol = win->w_cursor.col;
556 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
557 unsigned int cur_ve_flags = get_ve_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200559 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200561 win->w_cursor.col = 0;
562 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100564 // Allow cursor past end-of-line when:
565 // - in Insert mode or restarting Insert mode
566 // - in Visual mode and 'selection' isn't "old"
567 // - 'virtualedit' is set
Bram Moolenaarebefac62005-12-28 22:39:57 +0000568 if ((State & INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 || (VIsual_active && *p_sel != 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +0200570 || (cur_ve_flags & VE_ONEMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200572 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000574 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200575 win->w_cursor.col = len - 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100576 // Move the cursor to the head byte.
Bram Moolenaar87c19962007-04-26 08:54:21 +0000577 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200578 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200581 else if (win->w_cursor.col < 0)
582 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583
Bram Moolenaar85a20022019-12-21 18:25:54 +0100584 // If virtual editing is on, we can leave the cursor on the old position,
585 // only we must set it to virtual. But don't do it when at the end of the
586 // line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200588 win->w_cursor.coladd = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +0200589 else if (cur_ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000590 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200591 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200592 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200593 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaard41babe2017-08-30 17:01:35 +0200594
Bram Moolenaar85a20022019-12-21 18:25:54 +0100595 // Make sure that coladd is not more than the char width.
596 // Not for the last character, coladd is then used when the cursor
597 // is actually after the last character.
Bram Moolenaard41babe2017-08-30 17:01:35 +0200598 if (win->w_cursor.col + 1 < len && win->w_cursor.coladd > 0)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200599 {
600 int cs, ce;
601
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200602 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
603 if (win->w_cursor.coladd > ce - cs)
604 win->w_cursor.coladd = ce - cs;
605 }
606 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000607 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100608 // avoid weird number when there is a miscalculation or overflow
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200609 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611}
612
613/*
614 * make sure curwin->w_cursor in on a valid character
615 */
616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100617check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
619 check_cursor_lnum();
620 check_cursor_col();
621}
622
623#if defined(FEAT_TEXTOBJ) || defined(PROTO)
624/*
625 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
626 * Allow it when in Visual mode and 'selection' is not "old".
627 */
628 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100629adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630{
631 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 && gchar_cursor() == NUL)
634 --curwin->w_cursor.col;
635}
636#endif
637
638/*
639 * When curwin->w_leftcol has changed, adjust the cursor position.
640 * Return TRUE if the cursor was moved.
641 */
642 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100643leftcol_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644{
645 long lastcol;
646 colnr_T s, e;
647 int retval = FALSE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100648 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650 changed_cline_bef_curs();
Bram Moolenaar02631462017-09-22 15:20:32 +0200651 lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 validate_virtcol();
653
654 /*
655 * If the cursor is right or left of the screen, move it to last or first
656 * character.
657 */
Bram Moolenaar375e3392019-01-31 18:26:10 +0100658 if (curwin->w_virtcol > (colnr_T)(lastcol - siso))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {
660 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100661 coladvance((colnr_T)(lastcol - siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100663 else if (curwin->w_virtcol < curwin->w_leftcol + siso)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 {
665 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100666 (void)coladvance((colnr_T)(curwin->w_leftcol + siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 }
668
669 /*
670 * If the start of the character under the cursor is not on the screen,
671 * advance the cursor one more char. If this fails (last char of the
672 * line) adjust the scrolling.
673 */
674 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
675 if (e > (colnr_T)lastcol)
676 {
677 retval = TRUE;
678 coladvance(s - 1);
679 }
680 else if (s < curwin->w_leftcol)
681 {
682 retval = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100683 if (coladvance(e + 1) == FAIL) // there isn't another character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100685 curwin->w_leftcol = s; // adjust w_leftcol instead
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 changed_cline_bef_curs();
687 }
688 }
689
690 if (retval)
691 curwin->w_set_curswant = TRUE;
692 redraw_later(NOT_VALID);
693 return retval;
694}
695
696/**********************************************************************
697 * Various routines dealing with allocation and deallocation of memory.
698 */
699
700#if defined(MEM_PROFILE) || defined(PROTO)
701
702# define MEM_SIZES 8200
703static long_u mem_allocs[MEM_SIZES];
704static long_u mem_frees[MEM_SIZES];
705static long_u mem_allocated;
706static long_u mem_freed;
707static long_u mem_peak;
708static long_u num_alloc;
709static long_u num_freed;
710
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100712mem_pre_alloc_s(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713{
714 *sizep += sizeof(size_t);
715}
716
717 static void
Bram Moolenaar964b3742019-05-24 18:54:09 +0200718mem_pre_alloc_l(size_t *sizep)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719{
720 *sizep += sizeof(size_t);
721}
722
723 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100724mem_post_alloc(
725 void **pp,
726 size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
728 if (*pp == NULL)
729 return;
730 size -= sizeof(size_t);
731 *(long_u *)*pp = size;
732 if (size <= MEM_SIZES-1)
733 mem_allocs[size-1]++;
734 else
735 mem_allocs[MEM_SIZES-1]++;
736 mem_allocated += size;
737 if (mem_allocated - mem_freed > mem_peak)
738 mem_peak = mem_allocated - mem_freed;
739 num_alloc++;
740 *pp = (void *)((char *)*pp + sizeof(size_t));
741}
742
743 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100744mem_pre_free(void **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
746 long_u size;
747
748 *pp = (void *)((char *)*pp - sizeof(size_t));
749 size = *(size_t *)*pp;
750 if (size <= MEM_SIZES-1)
751 mem_frees[size-1]++;
752 else
753 mem_frees[MEM_SIZES-1]++;
754 mem_freed += size;
755 num_freed++;
756}
757
758/*
759 * called on exit via atexit()
760 */
761 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100762vim_mem_profile_dump(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 int i, j;
765
766 printf("\r\n");
767 j = 0;
768 for (i = 0; i < MEM_SIZES - 1; i++)
769 {
770 if (mem_allocs[i] || mem_frees[i])
771 {
772 if (mem_frees[i] > mem_allocs[i])
773 printf("\r\n%s", _("ERROR: "));
774 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
775 j++;
776 if (j > 3)
777 {
778 j = 0;
779 printf("\r\n");
780 }
781 }
782 }
783
784 i = MEM_SIZES - 1;
785 if (mem_allocs[i])
786 {
787 printf("\r\n");
788 if (mem_frees[i] > mem_allocs[i])
Bram Moolenaar2f1e0502010-08-13 11:18:02 +0200789 puts(_("ERROR: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
791 }
792
793 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
794 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
795 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
796 num_alloc, num_freed);
797}
798
Bram Moolenaar85a20022019-12-21 18:25:54 +0100799#endif // MEM_PROFILE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100801#ifdef FEAT_EVAL
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100802 int
Bram Moolenaar964b3742019-05-24 18:54:09 +0200803alloc_does_fail(size_t size)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100804{
805 if (alloc_fail_countdown == 0)
806 {
807 if (--alloc_fail_repeat <= 0)
808 alloc_fail_id = 0;
Bram Moolenaara260b872016-01-15 20:48:22 +0100809 do_outofmem_msg(size);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100810 return TRUE;
811 }
812 --alloc_fail_countdown;
813 return FALSE;
814}
815#endif
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817/*
818 * Some memory is reserved for error messages and for being able to
819 * call mf_release_all(), which needs some memory for mf_trans_add().
820 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100821#define KEEP_ROOM (2 * 8192L)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200822#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
824/*
Bram Moolenaar964b3742019-05-24 18:54:09 +0200825 * The normal way to allocate memory. This handles an out-of-memory situation
826 * as well as possible, still returns NULL when we're completely out.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200828 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200829alloc(size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830{
Bram Moolenaar964b3742019-05-24 18:54:09 +0200831 return lalloc(size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833
834/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100835 * alloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100836 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200837 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200838alloc_id(size_t size, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100839{
840#ifdef FEAT_EVAL
Bram Moolenaar964b3742019-05-24 18:54:09 +0200841 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100842 return NULL;
843#endif
Bram Moolenaar964b3742019-05-24 18:54:09 +0200844 return lalloc(size, TRUE);
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100845}
846
847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 * Allocate memory and set all bytes to zero.
849 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200850 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200851alloc_clear(size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200853 void *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
Bram Moolenaar964b3742019-05-24 18:54:09 +0200855 p = lalloc(size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 if (p != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200857 (void)vim_memset(p, 0, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 return p;
859}
860
861/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100862 * Same as alloc_clear() but with allocation id for testing
863 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200864 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200865alloc_clear_id(size_t size, alloc_id_T id UNUSED)
Bram Moolenaar162b7142018-12-21 15:17:36 +0100866{
867#ifdef FEAT_EVAL
Bram Moolenaar964b3742019-05-24 18:54:09 +0200868 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar162b7142018-12-21 15:17:36 +0100869 return NULL;
870#endif
871 return alloc_clear(size);
872}
873
874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 * Allocate memory like lalloc() and set all bytes to zero.
876 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200877 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200878lalloc_clear(size_t size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200880 void *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200882 p = lalloc(size, message);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (p != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +0200884 (void)vim_memset(p, 0, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 return p;
886}
887
888/*
889 * Low level memory allocation function.
890 * This is used often, KEEP IT FAST!
891 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200892 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200893lalloc(size_t size, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100895 void *p; // pointer to new storage space
896 static int releasing = FALSE; // don't do mf_release_all() recursive
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 int try_again;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100898#if defined(HAVE_AVAIL_MEM)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100899 static size_t allocated = 0; // allocated since last avail check
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#endif
901
Bram Moolenaar964b3742019-05-24 18:54:09 +0200902 // Safety check for allocating zero bytes
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (size == 0)
904 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200905 // Don't hide this message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 emsg_silent = 0;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200907 iemsg(_("E341: Internal error: lalloc(0, )"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 return NULL;
909 }
910
911#ifdef MEM_PROFILE
912 mem_pre_alloc_l(&size);
913#endif
914
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 /*
916 * Loop when out of memory: Try to release some memfile blocks and
917 * if some blocks are released call malloc again.
918 */
919 for (;;)
920 {
921 /*
922 * Handle three kind of systems:
923 * 1. No check for available memory: Just return.
924 * 2. Slow check for available memory: call mch_avail_mem() after
925 * allocating KEEP_ROOM amount of memory.
926 * 3. Strict check for available memory: call mch_avail_mem()
927 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200928 if ((p = malloc(size)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
930#ifndef HAVE_AVAIL_MEM
Bram Moolenaar85a20022019-12-21 18:25:54 +0100931 // 1. No check for available memory: Just return.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 goto theend;
933#else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100934 // 2. Slow check for available memory: call mch_avail_mem() after
935 // allocating (KEEP_ROOM / 2) amount of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 allocated += size;
937 if (allocated < KEEP_ROOM / 2)
938 goto theend;
939 allocated = 0;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100940
Bram Moolenaar85a20022019-12-21 18:25:54 +0100941 // 3. check for available memory: call mch_avail_mem()
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200942 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100944 free(p); // System is low... no go!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 p = NULL;
946 }
947 else
948 goto theend;
949#endif
950 }
951 /*
952 * Remember that mf_release_all() is being called to avoid an endless
953 * loop, because mf_release_all() may call alloc() recursively.
954 */
955 if (releasing)
956 break;
957 releasing = TRUE;
Bram Moolenaar661b1822005-07-28 22:36:45 +0000958
Bram Moolenaar85a20022019-12-21 18:25:54 +0100959 clear_sb_text(TRUE); // free any scrollback text
960 try_again = mf_release_all(); // release as many blocks as possible
Bram Moolenaar661b1822005-07-28 22:36:45 +0000961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 releasing = FALSE;
963 if (!try_again)
964 break;
965 }
966
967 if (message && p == NULL)
968 do_outofmem_msg(size);
969
970theend:
971#ifdef MEM_PROFILE
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200972 mem_post_alloc(&p, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973#endif
974 return p;
975}
976
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100977/*
978 * lalloc() with an ID for alloc_fail().
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100979 */
Bram Moolenaar113e1072019-01-20 15:30:40 +0100980#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200981 void *
Bram Moolenaar964b3742019-05-24 18:54:09 +0200982lalloc_id(size_t size, int message, alloc_id_T id UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100983{
984#ifdef FEAT_EVAL
Bram Moolenaara260b872016-01-15 20:48:22 +0100985 if (alloc_fail_id == id && alloc_does_fail(size))
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100986 return NULL;
987#endif
Bram Moolenaar964b3742019-05-24 18:54:09 +0200988 return (lalloc(size, message));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100989}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100990#endif
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992#if defined(MEM_PROFILE) || defined(PROTO)
993/*
994 * realloc() with memory profiling.
995 */
996 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100997mem_realloc(void *ptr, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998{
999 void *p;
1000
1001 mem_pre_free(&ptr);
1002 mem_pre_alloc_s(&size);
1003
1004 p = realloc(ptr, size);
1005
1006 mem_post_alloc(&p, size);
1007
1008 return p;
1009}
1010#endif
1011
1012/*
1013* Avoid repeating the error message many times (they take 1 second each).
1014* Did_outofmem_msg is reset when a character is read.
1015*/
1016 void
Bram Moolenaar964b3742019-05-24 18:54:09 +02001017do_outofmem_msg(size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018{
1019 if (!did_outofmem_msg)
1020 {
Bram Moolenaar4dc8f492019-08-21 13:06:55 +02001021 // Don't hide this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 emsg_silent = 0;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001023
Bram Moolenaar4dc8f492019-08-21 13:06:55 +02001024 // Must come first to avoid coming back here when printing the error
1025 // message fails, e.g. when setting v:errmsg.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 did_outofmem_msg = TRUE;
Bram Moolenaar79739e12011-10-26 11:41:00 +02001027
Bram Moolenaar964b3742019-05-24 18:54:09 +02001028 semsg(_("E342: Out of memory! (allocating %lu bytes)"), (long_u)size);
Bram Moolenaar4dc8f492019-08-21 13:06:55 +02001029
1030 if (starting == NO_SCREEN)
1031 // Not even finished with initializations and already out of
1032 // memory? Then nothing is going to work, exit.
1033 mch_exit(123);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
1035}
1036
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001037#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001038
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001039/*
1040 * Free everything that we allocated.
1041 * Can be used to detect memory leaks, e.g., with ccmalloc.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001042 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
1043 * surprised if Vim crashes...
1044 * Some things can't be freed, esp. things local to a library function.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001045 */
1046 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001047free_all_mem(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001048{
1049 buf_T *buf, *nextbuf;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001050
Bram Moolenaar85a20022019-12-21 18:25:54 +01001051 // When we cause a crash here it is caught and Vim tries to exit cleanly.
1052 // Don't try freeing everything again.
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001053 if (entered_free_all_mem)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001054 return;
Bram Moolenaarb89a25f2016-06-01 23:08:39 +02001055 entered_free_all_mem = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001056 // Don't want to trigger autocommands from here on.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001057 block_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001058
Bram Moolenaar85a20022019-12-21 18:25:54 +01001059 // Close all tabs and windows. Reset 'equalalways' to avoid redraws.
Bram Moolenaar5bedfc62010-07-20 22:30:01 +02001060 p_ea = FALSE;
Bram Moolenaare5c83282019-05-03 23:15:37 +02001061 if (first_tabpage != NULL && first_tabpage->tp_next != NULL)
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001062 do_cmdline_cmd((char_u *)"tabonly!");
Bram Moolenaar95f09602016-11-10 20:01:45 +01001063 if (!ONE_WINDOW)
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001064 do_cmdline_cmd((char_u *)"only!");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001065
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001066# if defined(FEAT_SPELL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001067 // Free all spell info.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001068 spell_free_all();
1069# endif
1070
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001071# if defined(FEAT_BEVAL_TERM)
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001072 ui_remove_balloon();
1073# endif
Bram Moolenaar03a9f842020-05-13 13:40:16 +02001074# ifdef FEAT_PROP_POPUP
Bram Moolenaar06f08532020-05-12 23:45:16 +02001075 if (curwin != NULL)
Bram Moolenaar03a9f842020-05-13 13:40:16 +02001076 close_all_popups(TRUE);
Bram Moolenaar06f08532020-05-12 23:45:16 +02001077# endif
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001078
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001079 // Clear user commands (before deleting buffers).
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001080 ex_comclear(NULL);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001081
Bram Moolenaare5c83282019-05-03 23:15:37 +02001082 // When exiting from mainerr_arg_missing curbuf has not been initialized,
1083 // and not much else.
1084 if (curbuf != NULL)
1085 {
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001086# ifdef FEAT_MENU
Bram Moolenaare5c83282019-05-03 23:15:37 +02001087 // Clear menus.
1088 do_cmdline_cmd((char_u *)"aunmenu *");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001089# ifdef FEAT_MULTI_LANG
Bram Moolenaare5c83282019-05-03 23:15:37 +02001090 do_cmdline_cmd((char_u *)"menutranslate clear");
Bram Moolenaar21160b92009-11-11 15:56:10 +00001091# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001092# endif
Bram Moolenaare5c83282019-05-03 23:15:37 +02001093 // Clear mappings, abbreviations, breakpoints.
1094 do_cmdline_cmd((char_u *)"lmapclear");
1095 do_cmdline_cmd((char_u *)"xmapclear");
1096 do_cmdline_cmd((char_u *)"mapclear");
1097 do_cmdline_cmd((char_u *)"mapclear!");
1098 do_cmdline_cmd((char_u *)"abclear");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001099# if defined(FEAT_EVAL)
Bram Moolenaare5c83282019-05-03 23:15:37 +02001100 do_cmdline_cmd((char_u *)"breakdel *");
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001101# endif
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001102# if defined(FEAT_PROFILE)
Bram Moolenaare5c83282019-05-03 23:15:37 +02001103 do_cmdline_cmd((char_u *)"profdel *");
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001104# endif
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001105# if defined(FEAT_KEYMAP)
Bram Moolenaare5c83282019-05-03 23:15:37 +02001106 do_cmdline_cmd((char_u *)"set keymap=");
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001107# endif
Bram Moolenaare5c83282019-05-03 23:15:37 +02001108 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001109
1110# ifdef FEAT_TITLE
1111 free_titles();
1112# endif
1113# if defined(FEAT_SEARCHPATH)
1114 free_findfile();
1115# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001116
Bram Moolenaar85a20022019-12-21 18:25:54 +01001117 // Obviously named calls.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001118 free_all_autocmds();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001119 clear_termcodes();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001120 free_all_marks();
1121 alist_clear(&global_alist);
1122 free_homedir();
Bram Moolenaar24305862012-08-15 14:05:05 +02001123 free_users();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001124 free_search_patterns();
1125 free_old_sub();
1126 free_last_insert();
Bram Moolenaar7591bb32019-03-30 13:53:47 +01001127 free_insexpand_stuff();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001128 free_prev_shellcmd();
1129 free_regexp_stuff();
1130 free_tag_stuff();
1131 free_cd_dir();
Bram Moolenaarde0dfed2009-02-24 03:30:14 +00001132# ifdef FEAT_SIGNS
1133 free_signs();
1134# endif
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001135# ifdef FEAT_EVAL
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001136 set_expr_line(NULL, NULL);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001137# endif
1138# ifdef FEAT_DIFF
Bram Moolenaare5c83282019-05-03 23:15:37 +02001139 if (curtab != NULL)
1140 diff_clear(curtab);
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001141# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001142 clear_sb_text(TRUE); // free any scrollback text
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001143
Bram Moolenaar85a20022019-12-21 18:25:54 +01001144 // Free some global vars.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001145 vim_free(username);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001146# ifdef FEAT_CLIPBOARD
Bram Moolenaar473de612013-06-08 18:19:48 +02001147 vim_regfree(clip_exclude_prog);
Bram Moolenaaraf92ee82007-10-07 13:45:08 +00001148# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001149 vim_free(last_cmdline);
1150 vim_free(new_last_cmdline);
Bram Moolenaar238a5642006-02-21 22:12:05 +00001151 set_keep_msg(NULL, 0);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001152
Bram Moolenaar85a20022019-12-21 18:25:54 +01001153 // Clear cmdline history.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001154 p_hi = 0;
1155 init_history();
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001156# ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +01001157 clear_global_prop_types();
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001158# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001159
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001160# ifdef FEAT_QUICKFIX
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001161 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001162 win_T *win;
1163 tabpage_T *tab;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001164
1165 qf_free_all(NULL);
Bram Moolenaare5c83282019-05-03 23:15:37 +02001166 // Free all location lists
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001167 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00001168 qf_free_all(win);
1169 }
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001170# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001171
Bram Moolenaare5c83282019-05-03 23:15:37 +02001172 // Close all script inputs.
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001173 close_all_scripts();
1174
Bram Moolenaare5c83282019-05-03 23:15:37 +02001175 if (curwin != NULL)
1176 // Destroy all windows. Must come before freeing buffers.
1177 win_free_all();
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001178
Bram Moolenaar85a20022019-12-21 18:25:54 +01001179 // Free all option values. Must come after closing windows.
Bram Moolenaar4f198282017-10-23 21:53:30 +02001180 free_all_options();
1181
Bram Moolenaar85a20022019-12-21 18:25:54 +01001182 // Free all buffers. Reset 'autochdir' to avoid accessing things that
1183 // were freed already.
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001184# ifdef FEAT_AUTOCHDIR
Bram Moolenaar2a329742008-04-01 12:53:43 +00001185 p_acd = FALSE;
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001186# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001187 for (buf = firstbuf; buf != NULL; )
1188 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001189 bufref_T bufref;
1190
1191 set_bufref(&bufref, buf);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001192 nextbuf = buf->b_next;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001193 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001194 if (bufref_valid(&bufref))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001195 buf = nextbuf; // didn't work, try next one
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001196 else
1197 buf = firstbuf;
1198 }
1199
Bram Moolenaar48ac6712019-07-04 20:26:21 +02001200# ifdef FEAT_ARABIC
1201 free_arshape_buf();
1202# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001203
Bram Moolenaar85a20022019-12-21 18:25:54 +01001204 // Clear registers.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001205 clear_registers();
1206 ResetRedobuff();
1207 ResetRedobuff();
1208
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001209# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001210 vim_free(serverDelayedStartName);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001211# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001212
Bram Moolenaar85a20022019-12-21 18:25:54 +01001213 // highlight info
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001214 free_highlight();
1215
Bram Moolenaar1e498f52005-06-26 22:29:44 +00001216 reset_last_sourcing();
1217
Bram Moolenaare5c83282019-05-03 23:15:37 +02001218 if (first_tabpage != NULL)
1219 {
1220 free_tabpage(first_tabpage);
1221 first_tabpage = NULL;
1222 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00001223
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001224# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01001225 // Machine-specific free.
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001226 mch_free_mem();
1227# endif
1228
Bram Moolenaar85a20022019-12-21 18:25:54 +01001229 // message history
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001230 for (;;)
1231 if (delete_first_msg() == FAIL)
1232 break;
1233
Bram Moolenaar655da312016-05-28 22:22:34 +02001234# ifdef FEAT_JOB_CHANNEL
1235 channel_free_all();
Bram Moolenaar655da312016-05-28 22:22:34 +02001236# endif
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001237# ifdef FEAT_TIMERS
Bram Moolenaar623e2632016-07-30 22:47:56 +02001238 timer_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001239# endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001240# ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001241 // must be after channel_free_all() with unrefs partials
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001242 eval_clear();
1243# endif
1244# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001245 // must be after eval_clear() with unrefs jobs
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001246 job_free_all();
1247# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001248
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001249 free_termoptions();
1250
Bram Moolenaar85a20022019-12-21 18:25:54 +01001251 // screenlines (can't display anything now!)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001252 free_screenlines();
1253
Bram Moolenaar82febc12019-06-10 14:48:59 +02001254# if defined(FEAT_SOUND)
1255 sound_free();
1256# endif
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001257# if defined(USE_XSMP)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001258 xsmp_close();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001259# endif
1260# ifdef FEAT_GUI_GTK
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001261 gui_mch_free_all();
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001262# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001263 clear_hl_tables();
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001264
1265 vim_free(IObuff);
1266 vim_free(NameBuff);
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02001267# ifdef FEAT_QUICKFIX
1268 check_quickfix_busy();
1269# endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00001270}
1271#endif
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001274 * Copy "p[len]" into allocated memory, ignoring NUL characters.
1275 * Returns NULL when out of memory.
1276 */
1277 char_u *
Bram Moolenaar964b3742019-05-24 18:54:09 +02001278vim_memsave(char_u *p, size_t len)
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001279{
Bram Moolenaar964b3742019-05-24 18:54:09 +02001280 char_u *ret = alloc(len);
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001281
1282 if (ret != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001283 mch_memmove(ret, p, len);
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01001284 return ret;
1285}
1286
1287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 * Isolate one part of a string option where parts are separated with
1289 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +00001290 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 * "*option" is advanced to the next part.
1292 * The length is returned.
1293 */
1294 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001295copy_option_part(
1296 char_u **option,
1297 char_u *buf,
1298 int maxlen,
1299 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 int len = 0;
1302 char_u *p = *option;
1303
Bram Moolenaar85a20022019-12-21 18:25:54 +01001304 // skip '.' at start of option part, for 'suffixes'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if (*p == '.')
1306 buf[len++] = *p++;
1307 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
1308 {
1309 /*
1310 * Skip backslash before a separator character and space.
1311 */
1312 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
1313 ++p;
1314 if (len < maxlen - 1)
1315 buf[len++] = *p;
1316 ++p;
1317 }
1318 buf[len] = NUL;
1319
Bram Moolenaar85a20022019-12-21 18:25:54 +01001320 if (*p != NUL && *p != ',') // skip non-standard separator
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 ++p;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001322 p = skip_to_option_part(p); // p points to next file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
1324 *option = p;
1325 return len;
1326}
1327
1328/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001329 * Replacement for free() that ignores NULL pointers.
1330 * Also skip free() when exiting for sure, this helps when we caught a deadly
1331 * signal that was caused by a crash in free().
Bram Moolenaard23a8232018-02-10 18:45:26 +01001332 * If you want to set NULL after calling this function, you should use
1333 * VIM_CLEAR() instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 */
1335 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001336vim_free(void *x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaar4770d092006-01-12 23:22:24 +00001338 if (x != NULL && !really_exiting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 {
1340#ifdef MEM_PROFILE
1341 mem_pre_free(&x);
1342#endif
1343 free(x);
1344 }
1345}
1346
1347#ifndef HAVE_MEMSET
1348 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001349vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
1351 char *p = ptr;
1352
1353 while (size-- > 0)
1354 *p++ = c;
1355 return ptr;
1356}
1357#endif
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359/*
1360 * Vim has its own isspace() function, because on some machines isspace()
1361 * can't handle characters above 128.
1362 */
1363 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001364vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 return ((x >= 9 && x <= 13) || x == ' ');
1367}
1368
1369/************************************************************************
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001370 * Functions for handling growing arrays.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 */
1372
1373/*
1374 * Clear an allocated growing array.
1375 */
1376 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001377ga_clear(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378{
1379 vim_free(gap->ga_data);
1380 ga_init(gap);
1381}
1382
1383/*
1384 * Clear a growing array that contains a list of strings.
1385 */
1386 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001387ga_clear_strings(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 int i;
1390
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001391 if (gap->ga_data != NULL)
1392 for (i = 0; i < gap->ga_len; ++i)
1393 vim_free(((char_u **)(gap->ga_data))[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 ga_clear(gap);
1395}
1396
1397/*
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001398 * Copy a growing array that contains a list of strings.
1399 */
1400 int
1401ga_copy_strings(garray_T *from, garray_T *to)
1402{
1403 int i;
1404
1405 ga_init2(to, sizeof(char_u *), 1);
1406 if (ga_grow(to, from->ga_len) == FAIL)
1407 return FAIL;
1408
1409 for (i = 0; i < from->ga_len; ++i)
1410 {
1411 char_u *orig = ((char_u **)from->ga_data)[i];
1412 char_u *copy;
1413
1414 if (orig == NULL)
1415 copy = NULL;
1416 else
1417 {
1418 copy = vim_strsave(orig);
1419 if (copy == NULL)
1420 {
1421 to->ga_len = i;
1422 ga_clear_strings(to);
1423 return FAIL;
1424 }
1425 }
1426 ((char_u **)to->ga_data)[i] = copy;
1427 }
1428 to->ga_len = from->ga_len;
1429 return OK;
1430}
1431
1432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 * Initialize a growing array. Don't forget to set ga_itemsize and
1434 * ga_growsize! Or use ga_init2().
1435 */
1436 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001437ga_init(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438{
1439 gap->ga_data = NULL;
Bram Moolenaar86b68352004-12-27 21:59:20 +00001440 gap->ga_maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 gap->ga_len = 0;
1442}
1443
1444 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001445ga_init2(garray_T *gap, int itemsize, int growsize)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
1447 ga_init(gap);
1448 gap->ga_itemsize = itemsize;
1449 gap->ga_growsize = growsize;
1450}
1451
1452/*
1453 * Make room in growing array "gap" for at least "n" items.
1454 * Return FAIL for failure, OK otherwise.
1455 */
1456 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001457ga_grow(garray_T *gap, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001459 if (gap->ga_maxlen - gap->ga_len < n)
1460 return ga_grow_inner(gap, n);
1461 return OK;
1462}
1463
1464 int
1465ga_grow_inner(garray_T *gap, int n)
1466{
Bram Moolenaar7282bc32012-02-22 18:12:32 +01001467 size_t old_len;
1468 size_t new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 char_u *pp;
1470
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001471 if (n < gap->ga_growsize)
1472 n = gap->ga_growsize;
Bram Moolenaarc47ed442019-06-01 14:36:26 +02001473
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001474 // A linear growth is very inefficient when the array grows big. This
1475 // is a compromise between allocating memory that won't be used and too
1476 // many copy operations. A factor of 1.5 seems reasonable.
1477 if (n < gap->ga_len / 2)
1478 n = gap->ga_len / 2;
Bram Moolenaarc47ed442019-06-01 14:36:26 +02001479
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001480 new_len = gap->ga_itemsize * (gap->ga_len + n);
1481 pp = vim_realloc(gap->ga_data, new_len);
1482 if (pp == NULL)
1483 return FAIL;
1484 old_len = gap->ga_itemsize * gap->ga_maxlen;
1485 vim_memset(pp + old_len, 0, new_len - old_len);
1486 gap->ga_maxlen = gap->ga_len + n;
1487 gap->ga_data = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 return OK;
1489}
1490
1491/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001492 * For a growing array that contains a list of strings: concatenate all the
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001493 * strings with a separating "sep".
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001494 * Returns NULL when out of memory.
1495 */
1496 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001497ga_concat_strings(garray_T *gap, char *sep)
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001498{
1499 int i;
1500 int len = 0;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001501 int sep_len = (int)STRLEN(sep);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001502 char_u *s;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001503 char_u *p;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001504
1505 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001506 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001507
1508 s = alloc(len + 1);
1509 if (s != NULL)
1510 {
1511 *s = NUL;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001512 p = s;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001513 for (i = 0; i < gap->ga_len; ++i)
1514 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02001515 if (p != s)
1516 {
1517 STRCPY(p, sep);
1518 p += sep_len;
1519 }
1520 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
1521 p += STRLEN(p);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001522 }
1523 }
1524 return s;
1525}
1526
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001527/*
1528 * Make a copy of string "p" and add it to "gap".
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001529 * When out of memory nothing changes and FAIL is returned.
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001530 */
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001531 int
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001532ga_add_string(garray_T *gap, char_u *p)
1533{
1534 char_u *cp = vim_strsave(p);
1535
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001536 if (cp == NULL)
1537 return FAIL;
1538
1539 if (ga_grow(gap, 1) == FAIL)
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001540 {
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001541 vim_free(cp);
1542 return FAIL;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001543 }
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001544 ((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
1545 return OK;
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001546}
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001547
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +02001548/*
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001549 * Concatenate a string to a growarray which contains bytes.
Bram Moolenaar43345542015-11-29 17:35:35 +01001550 * When "s" is NULL does not do anything.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 * Note: Does NOT copy the NUL at the end!
1552 */
1553 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001554ga_concat(garray_T *gap, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
Bram Moolenaar43345542015-11-29 17:35:35 +01001556 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
Bram Moolenaar478af672017-04-10 22:22:42 +02001558 if (s == NULL || *s == NUL)
Bram Moolenaar43345542015-11-29 17:35:35 +01001559 return;
1560 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (ga_grow(gap, len) == OK)
1562 {
1563 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
1564 gap->ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
1566}
1567
1568/*
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001569 * Concatenate 'len' bytes from string 's' to a growarray.
1570 * When "s" is NULL does not do anything.
1571 */
1572 void
1573ga_concat_len(garray_T *gap, char_u *s, size_t len)
1574{
1575 if (s == NULL || *s == NUL)
1576 return;
Yegappan Lakshmanan28d84212021-07-31 12:43:23 +02001577 if (ga_grow(gap, (int)len) == OK)
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001578 {
Yegappan Lakshmanan28d84212021-07-31 12:43:23 +02001579 mch_memmove((char *)gap->ga_data + gap->ga_len, s, len);
1580 gap->ga_len += (int)len;
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001581 }
1582}
1583
1584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 * Append one byte to a growarray which contains bytes.
1586 */
1587 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001588ga_append(garray_T *gap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 if (ga_grow(gap, 1) == OK)
1591 {
1592 *((char *)gap->ga_data + gap->ga_len) = c;
1593 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595}
1596
Bram Moolenaar4f974752019-02-17 17:44:42 +01001597#if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(MSWIN) \
Bram Moolenaar597a4222014-06-25 14:39:50 +02001598 || defined(PROTO)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02001599/*
1600 * Append the text in "gap" below the cursor line and clear "gap".
1601 */
1602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001603append_ga_line(garray_T *gap)
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02001604{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001605 // Remove trailing CR.
Bram Moolenaar4b9669f2011-07-07 16:20:52 +02001606 if (gap->ga_len > 0
1607 && !curbuf->b_p_bin
1608 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
1609 --gap->ga_len;
1610 ga_append(gap, NUL);
1611 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
1612 gap->ga_len = 0;
1613}
1614#endif
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616/************************************************************************
1617 * functions that use lookup tables for various things, generally to do with
1618 * special key codes.
1619 */
1620
1621/*
1622 * Some useful tables.
1623 */
1624
1625static struct modmasktable
1626{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001627 short mod_mask; // Bit-mask for particular key modifier
1628 short mod_flag; // Bit(s) for particular key modifier
1629 char_u name; // Single letter name of modifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630} mod_mask_table[] =
1631{
1632 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001633 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
1635 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
1636 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
1637 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
1638 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
Bram Moolenaard0573012017-10-28 21:11:06 +02001639#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
1641#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001642 // 'A' must be the last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
1644 {0, 0, NUL}
Bram Moolenaar85a20022019-12-21 18:25:54 +01001645 // NOTE: when adding an entry, update MAX_KEY_NAME_LEN!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646};
1647
1648/*
1649 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00001650 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 */
1652#define MOD_KEYS_ENTRY_SIZE 5
1653
1654static char_u modifier_keys_table[] =
1655{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001656// mod mask with modifier without modifier
1657 MOD_MASK_SHIFT, '&', '9', '@', '1', // begin
1658 MOD_MASK_SHIFT, '&', '0', '@', '2', // cancel
1659 MOD_MASK_SHIFT, '*', '1', '@', '4', // command
1660 MOD_MASK_SHIFT, '*', '2', '@', '5', // copy
1661 MOD_MASK_SHIFT, '*', '3', '@', '6', // create
1662 MOD_MASK_SHIFT, '*', '4', 'k', 'D', // delete char
1663 MOD_MASK_SHIFT, '*', '5', 'k', 'L', // delete line
1664 MOD_MASK_SHIFT, '*', '7', '@', '7', // end
1665 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', // end
1666 MOD_MASK_SHIFT, '*', '9', '@', '9', // exit
1667 MOD_MASK_SHIFT, '*', '0', '@', '0', // find
1668 MOD_MASK_SHIFT, '#', '1', '%', '1', // help
1669 MOD_MASK_SHIFT, '#', '2', 'k', 'h', // home
1670 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', // home
1671 MOD_MASK_SHIFT, '#', '3', 'k', 'I', // insert
1672 MOD_MASK_SHIFT, '#', '4', 'k', 'l', // left arrow
1673 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', // left arrow
1674 MOD_MASK_SHIFT, '%', 'a', '%', '3', // message
1675 MOD_MASK_SHIFT, '%', 'b', '%', '4', // move
1676 MOD_MASK_SHIFT, '%', 'c', '%', '5', // next
1677 MOD_MASK_SHIFT, '%', 'd', '%', '7', // options
1678 MOD_MASK_SHIFT, '%', 'e', '%', '8', // previous
1679 MOD_MASK_SHIFT, '%', 'f', '%', '9', // print
1680 MOD_MASK_SHIFT, '%', 'g', '%', '0', // redo
1681 MOD_MASK_SHIFT, '%', 'h', '&', '3', // replace
1682 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', // right arr.
1683 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', // right arr.
1684 MOD_MASK_SHIFT, '%', 'j', '&', '5', // resume
1685 MOD_MASK_SHIFT, '!', '1', '&', '6', // save
1686 MOD_MASK_SHIFT, '!', '2', '&', '7', // suspend
1687 MOD_MASK_SHIFT, '!', '3', '&', '8', // undo
1688 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', // up arrow
1689 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', // down arrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaar85a20022019-12-21 18:25:54 +01001691 // vt100 F1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
1693 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
1694 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
1695 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
1696
Bram Moolenaar85a20022019-12-21 18:25:54 +01001697 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', // F1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
1699 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
1700 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
1701 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
1702 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
1703 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
1704 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
1705 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
Bram Moolenaar85a20022019-12-21 18:25:54 +01001706 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', // F10
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
1708 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
1709 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
1710 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
1711 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
1712 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
1713 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
1714 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
1715 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
1716 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
1717 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
1718
1719 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
1720 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
1721 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
1722 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
1723 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
1724 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
1725 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
1726 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
1727 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
1728 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
1729
1730 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
1731 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
1732 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
1733 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
1734 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
1735 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
1736 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
1737
Bram Moolenaar85a20022019-12-21 18:25:54 +01001738 // TAB pseudo code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
1740
1741 NUL
1742};
1743
1744static struct key_name_entry
1745{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001746 int key; // Special key code or ascii value
1747 char_u *name; // Name of key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748} key_names_table[] =
1749{
1750 {' ', (char_u *)"Space"},
1751 {TAB, (char_u *)"Tab"},
1752 {K_TAB, (char_u *)"Tab"},
1753 {NL, (char_u *)"NL"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001754 {NL, (char_u *)"NewLine"}, // Alternative name
1755 {NL, (char_u *)"LineFeed"}, // Alternative name
1756 {NL, (char_u *)"LF"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 {CAR, (char_u *)"CR"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001758 {CAR, (char_u *)"Return"}, // Alternative name
1759 {CAR, (char_u *)"Enter"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 {K_BS, (char_u *)"BS"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001761 {K_BS, (char_u *)"BackSpace"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 {ESC, (char_u *)"Esc"},
1763 {CSI, (char_u *)"CSI"},
1764 {K_CSI, (char_u *)"xCSI"},
1765 {'|', (char_u *)"Bar"},
1766 {'\\', (char_u *)"Bslash"},
1767 {K_DEL, (char_u *)"Del"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001768 {K_DEL, (char_u *)"Delete"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 {K_KDEL, (char_u *)"kDel"},
1770 {K_UP, (char_u *)"Up"},
1771 {K_DOWN, (char_u *)"Down"},
1772 {K_LEFT, (char_u *)"Left"},
1773 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001774 {K_XUP, (char_u *)"xUp"},
1775 {K_XDOWN, (char_u *)"xDown"},
1776 {K_XLEFT, (char_u *)"xLeft"},
1777 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001778 {K_PS, (char_u *)"PasteStart"},
1779 {K_PE, (char_u *)"PasteEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
1781 {K_F1, (char_u *)"F1"},
1782 {K_F2, (char_u *)"F2"},
1783 {K_F3, (char_u *)"F3"},
1784 {K_F4, (char_u *)"F4"},
1785 {K_F5, (char_u *)"F5"},
1786 {K_F6, (char_u *)"F6"},
1787 {K_F7, (char_u *)"F7"},
1788 {K_F8, (char_u *)"F8"},
1789 {K_F9, (char_u *)"F9"},
1790 {K_F10, (char_u *)"F10"},
1791
1792 {K_F11, (char_u *)"F11"},
1793 {K_F12, (char_u *)"F12"},
1794 {K_F13, (char_u *)"F13"},
1795 {K_F14, (char_u *)"F14"},
1796 {K_F15, (char_u *)"F15"},
1797 {K_F16, (char_u *)"F16"},
1798 {K_F17, (char_u *)"F17"},
1799 {K_F18, (char_u *)"F18"},
1800 {K_F19, (char_u *)"F19"},
1801 {K_F20, (char_u *)"F20"},
1802
1803 {K_F21, (char_u *)"F21"},
1804 {K_F22, (char_u *)"F22"},
1805 {K_F23, (char_u *)"F23"},
1806 {K_F24, (char_u *)"F24"},
1807 {K_F25, (char_u *)"F25"},
1808 {K_F26, (char_u *)"F26"},
1809 {K_F27, (char_u *)"F27"},
1810 {K_F28, (char_u *)"F28"},
1811 {K_F29, (char_u *)"F29"},
1812 {K_F30, (char_u *)"F30"},
1813
1814 {K_F31, (char_u *)"F31"},
1815 {K_F32, (char_u *)"F32"},
1816 {K_F33, (char_u *)"F33"},
1817 {K_F34, (char_u *)"F34"},
1818 {K_F35, (char_u *)"F35"},
1819 {K_F36, (char_u *)"F36"},
1820 {K_F37, (char_u *)"F37"},
1821
1822 {K_XF1, (char_u *)"xF1"},
1823 {K_XF2, (char_u *)"xF2"},
1824 {K_XF3, (char_u *)"xF3"},
1825 {K_XF4, (char_u *)"xF4"},
1826
1827 {K_HELP, (char_u *)"Help"},
1828 {K_UNDO, (char_u *)"Undo"},
1829 {K_INS, (char_u *)"Insert"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001830 {K_INS, (char_u *)"Ins"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 {K_KINS, (char_u *)"kInsert"},
1832 {K_HOME, (char_u *)"Home"},
1833 {K_KHOME, (char_u *)"kHome"},
1834 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001835 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {K_END, (char_u *)"End"},
1837 {K_KEND, (char_u *)"kEnd"},
1838 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001839 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {K_PAGEUP, (char_u *)"PageUp"},
1841 {K_PAGEDOWN, (char_u *)"PageDown"},
1842 {K_KPAGEUP, (char_u *)"kPageUp"},
1843 {K_KPAGEDOWN, (char_u *)"kPageDown"},
1844
1845 {K_KPLUS, (char_u *)"kPlus"},
1846 {K_KMINUS, (char_u *)"kMinus"},
1847 {K_KDIVIDE, (char_u *)"kDivide"},
1848 {K_KMULTIPLY, (char_u *)"kMultiply"},
1849 {K_KENTER, (char_u *)"kEnter"},
1850 {K_KPOINT, (char_u *)"kPoint"},
1851
1852 {K_K0, (char_u *)"k0"},
1853 {K_K1, (char_u *)"k1"},
1854 {K_K2, (char_u *)"k2"},
1855 {K_K3, (char_u *)"k3"},
1856 {K_K4, (char_u *)"k4"},
1857 {K_K5, (char_u *)"k5"},
1858 {K_K6, (char_u *)"k6"},
1859 {K_K7, (char_u *)"k7"},
1860 {K_K8, (char_u *)"k8"},
1861 {K_K9, (char_u *)"k9"},
1862
1863 {'<', (char_u *)"lt"},
1864
1865 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001866#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001868#endif
1869#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001871#endif
1872#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001874#endif
1875#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001877#endif
1878#ifdef FEAT_MOUSE_URXVT
1879 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
1880#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001881 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
Bram Moolenaar21a83bd2021-02-23 19:19:58 +01001882 {K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelease"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
1884 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
1885 {K_LEFTDRAG, (char_u *)"LeftDrag"},
1886 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
1887 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001888 {K_MOUSEMOVE, (char_u *)"MouseMove"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
1890 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
1891 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
1892 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
1893 {K_RIGHTDRAG, (char_u *)"RightDrag"},
1894 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001895 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
1896 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
1897 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
1898 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001899 {K_MOUSEDOWN, (char_u *)"MouseDown"}, // OBSOLETE: Use
1900 {K_MOUSEUP, (char_u *)"MouseUp"}, // ScrollWheelXXX instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 {K_X1MOUSE, (char_u *)"X1Mouse"},
1902 {K_X1DRAG, (char_u *)"X1Drag"},
1903 {K_X1RELEASE, (char_u *)"X1Release"},
1904 {K_X2MOUSE, (char_u *)"X2Mouse"},
1905 {K_X2DRAG, (char_u *)"X2Drag"},
1906 {K_X2RELEASE, (char_u *)"X2Release"},
1907 {K_DROP, (char_u *)"Drop"},
1908 {K_ZERO, (char_u *)"Nul"},
1909#ifdef FEAT_EVAL
1910 {K_SNR, (char_u *)"SNR"},
1911#endif
1912 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02001913 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar2f106582019-05-08 21:59:25 +02001914 {K_IGNORE, (char_u *)"Ignore"},
Bram Moolenaar957cf672020-11-12 14:21:06 +01001915 {K_COMMAND, (char_u *)"Cmd"},
Bram Moolenaarccb47a22021-01-21 13:36:43 +01001916 {K_FOCUSGAINED, (char_u *)"FocusGained"},
1917 {K_FOCUSLOST, (char_u *)"FocusLost"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {0, NULL}
Bram Moolenaar85a20022019-12-21 18:25:54 +01001919 // NOTE: When adding a long name update MAX_KEY_NAME_LEN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920};
1921
K.Takataeeec2542021-06-02 13:28:16 +02001922#define KEY_NAMES_TABLE_LEN ARRAY_LENGTH(key_names_table)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924/*
1925 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
1926 * modifier name ('S' for Shift, 'C' for Ctrl etc).
1927 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001928 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001929name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930{
1931 int i;
1932
1933 c = TOUPPER_ASC(c);
1934 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
1935 if (c == mod_mask_table[i].name)
1936 return mod_mask_table[i].mod_flag;
1937 return 0;
1938}
1939
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940/*
1941 * Check if if there is a special key code for "key" that includes the
1942 * modifiers specified.
1943 */
1944 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001945simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946{
1947 int i;
1948 int key0;
1949 int key1;
1950
1951 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
1952 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001953 // TAB is a special case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
1955 {
1956 *modifiers &= ~MOD_MASK_SHIFT;
1957 return K_S_TAB;
1958 }
1959 key0 = KEY2TERMCAP0(key);
1960 key1 = KEY2TERMCAP1(key);
1961 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
1962 if (key0 == modifier_keys_table[i + 3]
1963 && key1 == modifier_keys_table[i + 4]
1964 && (*modifiers & modifier_keys_table[i]))
1965 {
1966 *modifiers &= ~modifier_keys_table[i];
1967 return TERMCAP2KEY(modifier_keys_table[i + 1],
1968 modifier_keys_table[i + 2]);
1969 }
1970 }
1971 return key;
1972}
1973
1974/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001975 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001976 */
1977 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001978handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001979{
1980 switch (key)
1981 {
1982 case K_XUP: return K_UP;
1983 case K_XDOWN: return K_DOWN;
1984 case K_XLEFT: return K_LEFT;
1985 case K_XRIGHT: return K_RIGHT;
1986 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001987 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001988 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001989 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001990 case K_XF1: return K_F1;
1991 case K_XF2: return K_F2;
1992 case K_XF3: return K_F3;
1993 case K_XF4: return K_F4;
1994 case K_S_XF1: return K_S_F1;
1995 case K_S_XF2: return K_S_F2;
1996 case K_S_XF3: return K_S_F3;
1997 case K_S_XF4: return K_S_F4;
1998 }
1999 return key;
2000}
2001
2002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 * Return a string which contains the name of the given key when the given
2004 * modifiers are down.
2005 */
2006 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002007get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
2009 static char_u string[MAX_KEY_NAME_LEN + 1];
2010
2011 int i, idx;
2012 int table_idx;
2013 char_u *s;
2014
2015 string[0] = '<';
2016 idx = 1;
2017
Bram Moolenaar85a20022019-12-21 18:25:54 +01002018 // Key that stands for a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
2020 c = KEY2TERMCAP1(c);
2021
2022 /*
2023 * Translate shifted special keys into unshifted keys and set modifier.
2024 * Same for CTRL and ALT modifiers.
2025 */
2026 if (IS_SPECIAL(c))
2027 {
2028 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
2029 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
2030 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
2031 {
2032 modifiers |= modifier_keys_table[i];
2033 c = TERMCAP2KEY(modifier_keys_table[i + 3],
2034 modifier_keys_table[i + 4]);
2035 break;
2036 }
2037 }
2038
Bram Moolenaar85a20022019-12-21 18:25:54 +01002039 // try to find the key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 table_idx = find_special_key_in_table(c);
2041
2042 /*
2043 * When not a known special key, and not a printable character, try to
2044 * extract modifiers.
2045 */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002046 if (c > 0 && (*mb_char2len)(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
2048 if (table_idx < 0
2049 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
2050 && (c & 0x80))
2051 {
2052 c &= 0x7f;
2053 modifiers |= MOD_MASK_ALT;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002054 // try again, to find the un-alted key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 table_idx = find_special_key_in_table(c);
2056 }
2057 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
2058 {
2059#ifdef EBCDIC
2060 c = CtrlChar(c);
2061#else
2062 c += '@';
2063#endif
2064 modifiers |= MOD_MASK_CTRL;
2065 }
2066 }
2067
Bram Moolenaar85a20022019-12-21 18:25:54 +01002068 // translate the modifier into a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 for (i = 0; mod_mask_table[i].name != 'A'; i++)
2070 if ((modifiers & mod_mask_table[i].mod_mask)
2071 == mod_mask_table[i].mod_flag)
2072 {
2073 string[idx++] = mod_mask_table[i].name;
2074 string[idx++] = (char_u)'-';
2075 }
2076
Bram Moolenaar85a20022019-12-21 18:25:54 +01002077 if (table_idx < 0) // unknown special key, may output t_xx
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
2079 if (IS_SPECIAL(c))
2080 {
2081 string[idx++] = 't';
2082 string[idx++] = '_';
2083 string[idx++] = KEY2TERMCAP0(c);
2084 string[idx++] = KEY2TERMCAP1(c);
2085 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002086 // Not a special key, only modifiers, output directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 else
2088 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 if (has_mbyte && (*mb_char2len)(c) > 1)
2090 idx += (*mb_char2bytes)(c, string + idx);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002091 else if (vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 string[idx++] = c;
2093 else
2094 {
2095 s = transchar(c);
2096 while (*s)
2097 string[idx++] = *s++;
2098 }
2099 }
2100 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002101 else // use name of special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
Bram Moolenaar423977d2017-01-22 15:05:12 +01002103 size_t len = STRLEN(key_names_table[table_idx].name);
2104
2105 if (len + idx + 2 <= MAX_KEY_NAME_LEN)
2106 {
2107 STRCPY(string + idx, key_names_table[table_idx].name);
2108 idx += (int)len;
2109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
2111 string[idx++] = '>';
2112 string[idx] = NUL;
2113 return string;
2114}
2115
2116/*
2117 * Try translating a <> name at (*srcp)[] to dst[].
2118 * Return the number of characters added to dst[], zero for no match.
2119 * If there is a match, srcp is advanced to after the <> name.
2120 * dst[] must be big enough to hold the result (up to six characters)!
2121 */
2122 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002123trans_special(
2124 char_u **srcp,
2125 char_u *dst,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002126 int flags, // FSK_ values
2127 int *did_simplify) // FSK_SIMPLIFY and found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128{
2129 int modifiers = 0;
2130 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002132 key = find_special_key(srcp, &modifiers, flags, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if (key == 0)
2134 return 0;
2135
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002136 return special_to_buf(key, modifiers, flags & FSK_KEYCODE, dst);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002137}
2138
2139/*
2140 * Put the character sequence for "key" with "modifiers" into "dst" and return
2141 * the resulting length.
2142 * When "keycode" is TRUE prefer key code, e.g. K_DEL instead of DEL.
2143 * The sequence is not NUL terminated.
2144 * This is how characters in a string are encoded.
2145 */
2146 int
2147special_to_buf(int key, int modifiers, int keycode, char_u *dst)
2148{
2149 int dlen = 0;
2150
Bram Moolenaar85a20022019-12-21 18:25:54 +01002151 // Put the appropriate modifier in a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (modifiers != 0)
2153 {
2154 dst[dlen++] = K_SPECIAL;
2155 dst[dlen++] = KS_MODIFIER;
2156 dst[dlen++] = modifiers;
2157 }
2158
2159 if (IS_SPECIAL(key))
2160 {
2161 dst[dlen++] = K_SPECIAL;
2162 dst[dlen++] = KEY2TERMCAP0(key);
2163 dst[dlen++] = KEY2TERMCAP1(key);
2164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 else if (has_mbyte && !keycode)
2166 dlen += (*mb_char2bytes)(key, dst + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 else if (keycode)
2168 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
2169 else
2170 dst[dlen++] = key;
2171
2172 return dlen;
2173}
2174
2175/*
2176 * Try translating a <> name at (*srcp)[], return the key and modifiers.
2177 * srcp is advanced to after the <> name.
2178 * returns 0 if there is no match.
2179 */
2180 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002181find_special_key(
2182 char_u **srcp,
2183 int *modp,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002184 int flags, // FSK_ values
Bram Moolenaar459fd782019-10-13 16:43:39 +02002185 int *did_simplify) // found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186{
2187 char_u *last_dash;
2188 char_u *end_of_name;
2189 char_u *src;
2190 char_u *bp;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002191 int in_string = flags & FSK_IN_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 int modifiers;
2193 int bit;
2194 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002195 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002196 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
2198 src = *srcp;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002199 if (src[0] != '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 return 0;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002201 if (src[1] == '*') // <*xxx>: do not simplify
2202 ++src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203
Bram Moolenaar85a20022019-12-21 18:25:54 +01002204 // Find end of modifier list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 last_dash = src;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02002206 for (bp = src + 1; *bp == '-' || vim_isNormalIDc(*bp); bp++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 {
2208 if (*bp == '-')
2209 {
2210 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002211 if (bp[1] != NUL)
2212 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002213 if (has_mbyte)
2214 l = mb_ptr2len(bp + 1);
2215 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002216 l = 1;
Bram Moolenaarc8fd33d2019-08-16 20:33:05 +02002217 // Anything accepted, like <C-?>.
2218 // <C-"> or <M-"> are not special in strings as " is
2219 // the string delimiter. With a backslash it works: <M-\">
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002220 if (!(in_string && bp[1] == '"') && bp[l + 1] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02002221 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002222 else if (in_string && bp[1] == '\\' && bp[2] == '"'
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002223 && bp[3] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002224 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 }
2227 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002228 bp += 3; // skip t_xx, xx may be '-' or '>'
Bram Moolenaar792826c2011-08-19 22:29:02 +02002229 else if (STRNICMP(bp, "char-", 5) == 0)
2230 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002231 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0, TRUE);
2232 if (l == 0)
2233 {
2234 emsg(_(e_invarg));
2235 return 0;
2236 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02002237 bp += l + 5;
2238 break;
2239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
2241
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002242 if (*bp == '>') // found matching '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
2244 end_of_name = bp + 1;
2245
Bram Moolenaar85a20022019-12-21 18:25:54 +01002246 // Which modifiers are given?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 modifiers = 0x0;
2248 for (bp = src + 1; bp < last_dash; bp++)
2249 {
2250 if (*bp != '-')
2251 {
2252 bit = name_to_mod_mask(*bp);
2253 if (bit == 0x0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002254 break; // Illegal modifier name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 modifiers |= bit;
2256 }
2257 }
2258
2259 /*
2260 * Legal modifier name.
2261 */
2262 if (bp >= last_dash)
2263 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002264 if (STRNICMP(last_dash + 1, "char-", 5) == 0
2265 && VIM_ISDIGIT(last_dash[6]))
2266 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002267 // <Char-123> or <Char-033> or <Char-0x33>
Bram Moolenaar459fd782019-10-13 16:43:39 +02002268 vim_str2nr(last_dash + 6, NULL, &l, STR2NR_ALL, NULL,
2269 &n, 0, TRUE);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002270 if (l == 0)
2271 {
2272 emsg(_(e_invarg));
2273 return 0;
2274 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02002275 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02002276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002278 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002279 int off = 1;
2280
Bram Moolenaar85a20022019-12-21 18:25:54 +01002281 // Modifier with single letter, or special key name.
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002282 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
2283 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02002284 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002285 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002286 else
Bram Moolenaar792826c2011-08-19 22:29:02 +02002287 l = 1;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002288 if (modifiers != 0 && last_dash[l + off] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002289 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02002290 else
2291 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02002292 key = get_special_key_code(last_dash + off);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002293 if (!(flags & FSK_KEEP_X_KEY))
Bram Moolenaar792826c2011-08-19 22:29:02 +02002294 key = handle_x_keys(key);
2295 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298 /*
2299 * get_special_key_code() may return NUL for invalid
2300 * special key name.
2301 */
2302 if (key != NUL)
2303 {
2304 /*
2305 * Only use a modifier when there is no special key code that
2306 * includes the modifier.
2307 */
2308 key = simplify_key(key, &modifiers);
2309
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002310 if (!(flags & FSK_KEYCODE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002312 // don't want keycode, use single byte code
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (key == K_BS)
2314 key = BS;
2315 else if (key == K_DEL || key == K_KDEL)
2316 key = DEL;
2317 }
2318
Bram Moolenaar459fd782019-10-13 16:43:39 +02002319 // Normal Key with modifier: Try to make a single byte code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (!IS_SPECIAL(key))
Bram Moolenaar459fd782019-10-13 16:43:39 +02002321 key = extract_modifiers(key, &modifiers,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002322 flags & FSK_SIMPLIFY, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
2324 *modp = modifiers;
2325 *srcp = end_of_name;
2326 return key;
2327 }
2328 }
2329 }
2330 return 0;
2331}
2332
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002333
2334/*
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02002335 * Some keys are used with Ctrl without Shift and are still expected to be
2336 * mapped as if Shift was pressed:
2337 * CTRL-2 is CTRL-@
2338 * CTRL-6 is CTRL-^
2339 * CTRL-- is CTRL-_
2340 * Also, <C-H> and <C-h> mean the same thing, always use "H".
2341 * Returns the possibly adjusted key.
2342 */
2343 int
2344may_adjust_key_for_ctrl(int modifiers, int key)
2345{
2346 if (modifiers & MOD_MASK_CTRL)
2347 {
2348 if (ASCII_ISALPHA(key))
2349 return TOUPPER_ASC(key);
2350 if (key == '2')
2351 return '@';
2352 if (key == '6')
2353 return '^';
2354 if (key == '-')
2355 return '_';
2356 }
2357 return key;
2358}
2359
2360/*
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002361 * Some keys already have Shift included, pass them as normal keys.
Bram Moolenaar9a033d72020-10-07 17:29:48 +02002362 * When Ctrl is also used <C-H> and <C-S-H> are different, but <C-S-{> should
2363 * be <C-{>. Same for <C-S-}> and <C-S-|>.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002364 * Also for <A-S-a> and <M-S-a>.
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02002365 * This includes all printable ASCII characters except numbers and a-z.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002366 */
2367 int
2368may_remove_shift_modifier(int modifiers, int key)
2369{
2370 if ((modifiers == MOD_MASK_SHIFT
2371 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
2372 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02002373 && ((key >= '!' && key <= '/')
2374 || (key >= ':' && key <= 'Z')
2375 || (key >= '[' && key <= '`')
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002376 || (key >= '{' && key <= '~')))
2377 return modifiers & ~MOD_MASK_SHIFT;
Bram Moolenaar9a033d72020-10-07 17:29:48 +02002378
2379 if (modifiers == (MOD_MASK_SHIFT | MOD_MASK_CTRL)
2380 && (key == '{' || key == '}' || key == '|'))
2381 return modifiers & ~MOD_MASK_SHIFT;
2382
Bram Moolenaaref6746f2020-06-20 14:43:23 +02002383 return modifiers;
2384}
2385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386/*
2387 * Try to include modifiers in the key.
2388 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
Bram Moolenaar459fd782019-10-13 16:43:39 +02002389 * When "simplify" is FALSE don't do Ctrl and Alt.
2390 * When "simplify" is TRUE and Ctrl or Alt is removed from modifiers set
2391 * "did_simplify" when it's not NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
2393 int
Bram Moolenaar459fd782019-10-13 16:43:39 +02002394extract_modifiers(int key, int *modp, int simplify, int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395{
2396 int modifiers = *modp;
2397
Bram Moolenaard0573012017-10-28 21:11:06 +02002398#ifdef MACOS_X
Bram Moolenaar459fd782019-10-13 16:43:39 +02002399 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 if (!(modifiers & MOD_MASK_CMD))
2401#endif
2402 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
2403 {
2404 key = TOUPPER_ASC(key);
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002405 // With <C-S-a> we keep the shift modifier.
2406 // With <S-a>, <A-S-a> and <S-A> we don't keep the shift modifier.
2407 if (simplify || modifiers == MOD_MASK_SHIFT
2408 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
2409 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02002410 modifiers &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02002412
2413 // <C-H> and <C-h> mean the same thing, always use "H"
2414 if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key))
2415 key = TOUPPER_ASC(key);
2416
2417 if (simplify && (modifiers & MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418#ifdef EBCDIC
Bram Moolenaar459fd782019-10-13 16:43:39 +02002419 // TODO: EBCDIC Better use:
2420 // && (Ctrl_chr(key) || key == '?')
2421 // ???
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 && strchr("?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_", key)
2423 != NULL
2424#else
2425 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))
2426#endif
2427 )
2428 {
2429 key = Ctrl_chr(key);
2430 modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002431 // <C-@> is <Nul>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 if (key == 0)
2433 key = K_ZERO;
Bram Moolenaar459fd782019-10-13 16:43:39 +02002434 if (did_simplify != NULL)
2435 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02002437
Bram Moolenaard0573012017-10-28 21:11:06 +02002438#ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01002439 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 if (!(modifiers & MOD_MASK_CMD))
2441#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +02002442 if (simplify && (modifiers & MOD_MASK_ALT) && key < 0x80
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002443 && !enc_dbcs) // avoid creating a lead byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
2445 key |= 0x80;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002446 modifiers &= ~MOD_MASK_ALT; // remove the META modifier
Bram Moolenaar459fd782019-10-13 16:43:39 +02002447 if (did_simplify != NULL)
2448 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450
2451 *modp = modifiers;
2452 return key;
2453}
2454
2455/*
2456 * Try to find key "c" in the special key table.
2457 * Return the index when found, -1 when not found.
2458 */
2459 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002460find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461{
2462 int i;
2463
2464 for (i = 0; key_names_table[i].name != NULL; i++)
2465 if (c == key_names_table[i].key)
2466 break;
2467 if (key_names_table[i].name == NULL)
2468 i = -1;
2469 return i;
2470}
2471
2472/*
2473 * Find the special key with the given name (the given string does not have to
2474 * end with NUL, the name is assumed to end before the first non-idchar).
2475 * If the name starts with "t_" the next two characters are interpreted as a
2476 * termcap name.
2477 * Return the key code, or 0 if not found.
2478 */
2479 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002480get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
2482 char_u *table_name;
2483 char_u string[3];
2484 int i, j;
2485
2486 /*
2487 * If it's <t_xx> we get the code for xx from the termcap
2488 */
2489 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
2490 {
2491 string[0] = name[2];
2492 string[1] = name[3];
2493 string[2] = NUL;
2494 if (add_termcap_entry(string, FALSE) == OK)
2495 return TERMCAP2KEY(name[2], name[3]);
2496 }
2497 else
2498 for (i = 0; key_names_table[i].name != NULL; i++)
2499 {
2500 table_name = key_names_table[i].name;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02002501 for (j = 0; vim_isNormalIDc(name[j]) && table_name[j] != NUL; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
2503 break;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02002504 if (!vim_isNormalIDc(name[j]) && table_name[j] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 return key_names_table[i].key;
2506 }
2507 return 0;
2508}
2509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002511get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00002513 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 return NULL;
2515 return key_names_table[i].name;
2516}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518/*
2519 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
2520 */
2521 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002522get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524 int c = *buf->b_p_ff;
2525
2526 if (buf->b_p_bin || c == 'u')
2527 return EOL_UNIX;
2528 if (c == 'm')
2529 return EOL_MAC;
2530 return EOL_DOS;
2531}
2532
2533/*
2534 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
2535 * argument.
2536 */
2537 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002538get_fileformat_force(
2539 buf_T *buf,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002540 exarg_T *eap) // can be NULL!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541{
2542 int c;
2543
2544 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02002545 c = eap->force_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 else
2547 {
2548 if ((eap != NULL && eap->force_bin != 0)
2549 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
2550 return EOL_UNIX;
2551 c = *buf->b_p_ff;
2552 }
2553 if (c == 'u')
2554 return EOL_UNIX;
2555 if (c == 'm')
2556 return EOL_MAC;
2557 return EOL_DOS;
2558}
2559
2560/*
2561 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
2562 * Sets both 'textmode' and 'fileformat'.
2563 * Note: Does _not_ set global value of 'textmode'!
2564 */
2565 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002566set_fileformat(
2567 int t,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002568 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
2570 char *p = NULL;
2571
2572 switch (t)
2573 {
2574 case EOL_DOS:
2575 p = FF_DOS;
2576 curbuf->b_p_tx = TRUE;
2577 break;
2578 case EOL_UNIX:
2579 p = FF_UNIX;
2580 curbuf->b_p_tx = FALSE;
2581 break;
2582 case EOL_MAC:
2583 p = FF_MAC;
2584 curbuf->b_p_tx = FALSE;
2585 break;
2586 }
2587 if (p != NULL)
2588 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002589 OPT_FREE | opt_flags, 0);
2590
Bram Moolenaar85a20022019-12-21 18:25:54 +01002591 // This may cause the buffer to become (un)modified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002593 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594#ifdef FEAT_TITLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01002595 need_maketitle = TRUE; // set window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596#endif
2597}
2598
2599/*
2600 * Return the default fileformat from 'fileformats'.
2601 */
2602 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002603default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
2605 switch (*p_ffs)
2606 {
2607 case 'm': return EOL_MAC;
2608 case 'd': return EOL_DOS;
2609 }
2610 return EOL_UNIX;
2611}
2612
2613/*
2614 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
2615 */
2616 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002617call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
2619 char_u *ncmd;
2620 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002621#ifdef FEAT_PROFILE
2622 proftime_T wait_time;
2623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
2625 if (p_verbose > 3)
2626 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002627 verbose_enter();
Bram Moolenaar06f08532020-05-12 23:45:16 +02002628 smsg(_("Calling shell to execute: \"%s\""), cmd == NULL ? p_sh : cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 out_char('\n');
2630 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00002631 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 }
2633
Bram Moolenaar05159a02005-02-26 23:04:13 +00002634#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00002635 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002636 prof_child_enter(&wait_time);
2637#endif
2638
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (*p_sh == NUL)
2640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002641 emsg(_(e_shellempty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 retval = -1;
2643 }
2644 else
2645 {
2646#ifdef FEAT_GUI_MSWIN
Bram Moolenaar85a20022019-12-21 18:25:54 +01002647 // Don't hide the pointer while executing a shell command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 gui_mch_mousehide(FALSE);
2649#endif
2650#ifdef FEAT_GUI
2651 ++hold_gui_events;
2652#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002653 // The external command may update a tags file, clear cached tags.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 tag_freematch();
2655
Bram Moolenaar01257a72019-06-10 14:46:04 +02002656 if (cmd == NULL || *p_sxq == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 retval = mch_call_shell(cmd, opt);
2658 else
2659 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01002660 char_u *ecmd = cmd;
2661
Bram Moolenaar1a613392019-09-28 15:51:37 +02002662 if (*p_sxe != NUL && *p_sxq == '(')
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01002663 {
2664 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
2665 if (ecmd == NULL)
2666 ecmd = cmd;
2667 }
Bram Moolenaar964b3742019-05-24 18:54:09 +02002668 ncmd = alloc(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 if (ncmd != NULL)
2670 {
2671 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01002672 STRCAT(ncmd, ecmd);
Bram Moolenaar1a613392019-09-28 15:51:37 +02002673 // When 'shellxquote' is ( append ).
2674 // When 'shellxquote' is "( append )".
2675 STRCAT(ncmd, *p_sxq == '(' ? (char_u *)")"
2676 : *p_sxq == '"' && *(p_sxq+1) == '(' ? (char_u *)")\""
2677 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 retval = mch_call_shell(ncmd, opt);
2679 vim_free(ncmd);
2680 }
2681 else
2682 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01002683 if (ecmd != cmd)
2684 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 }
2686#ifdef FEAT_GUI
2687 --hold_gui_events;
2688#endif
2689 /*
2690 * Check the window size, in case it changed while executing the
2691 * external command.
2692 */
2693 shell_resized_check();
2694 }
2695
2696#ifdef FEAT_EVAL
2697 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002698# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00002699 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002700 prof_child_exit(&wait_time);
2701# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702#endif
2703
2704 return retval;
2705}
2706
2707/*
Bram Moolenaar01265852006-03-20 21:50:15 +00002708 * VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to
2709 * NORMAL State with a condition. This function returns the real State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 */
2711 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002712get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 if (State & NORMAL)
2715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00002717 {
2718 if (VIsual_select)
2719 return SELECTMODE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 return VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00002721 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002722 else if (finish_op)
2723 return OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 return State;
2726}
2727
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002728/*
2729 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02002730 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002731 * "b" must point to the start of the file name
2732 */
2733 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002734after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002735{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02002736 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002737 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
2738}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002739
2740/*
2741 * Return TRUE if file names "f1" and "f2" are in the same directory.
2742 * "f1" may be a short name, "f2" must be a full path.
2743 */
2744 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002745same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002746{
2747 char_u ffname[MAXPATHL];
2748 char_u *t1;
2749 char_u *t2;
2750
Bram Moolenaar85a20022019-12-21 18:25:54 +01002751 // safety check
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002752 if (f1 == NULL || f2 == NULL)
2753 return FALSE;
2754
2755 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
2756 t1 = gettail_sep(ffname);
2757 t2 = gettail_sep(f2);
2758 return (t1 - ffname == t2 - f2
2759 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
2760}
2761
Bram Moolenaar7c365fb2018-06-29 20:28:31 +02002762#if defined(FEAT_SESSION) || defined(FEAT_AUTOCHDIR) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002763 || defined(MSWIN) || defined(FEAT_GUI_GTK) \
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01002764 || defined(FEAT_NETBEANS_INTG) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 || defined(PROTO)
2766/*
2767 * Change to a file's directory.
2768 * Caller must call shorten_fnames()!
2769 * Return OK or FAIL.
2770 */
2771 int
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002772vim_chdirfile(char_u *fname, char *trigger_autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002774 char_u old_dir[MAXPATHL];
2775 char_u new_dir[MAXPATHL];
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01002776 int res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002778 if (mch_dirname(old_dir, MAXPATHL) != OK)
2779 *old_dir = NUL;
2780
2781 vim_strncpy(new_dir, fname, MAXPATHL - 1);
2782 *gettail_sep(new_dir) = NUL;
2783
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01002784 if (pathcmp((char *)old_dir, (char *)new_dir, -1) == 0)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002785 // nothing to do
2786 res = OK;
2787 else
2788 {
2789 res = mch_chdir((char *)new_dir) == 0 ? OK : FAIL;
2790
2791 if (res == OK && trigger_autocmd != NULL)
2792 apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd,
2793 new_dir, FALSE, curbuf);
2794 }
Bram Moolenaarb5cb65b2018-02-03 18:01:37 +01002795 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796}
2797#endif
2798
2799#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
2800/*
2801 * Check if "name" ends in a slash and is not a directory.
2802 * Used for systems where stat() ignores a trailing slash on a file name.
2803 * The Vim code assumes a trailing slash is only ignored for a directory.
2804 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002805 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01002806illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
2808 if (name[0] == NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002809 return FALSE; // no file name is not illegal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 if (name[strlen(name) - 1] != '/')
Bram Moolenaar85a20022019-12-21 18:25:54 +01002811 return FALSE; // no trailing slash
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 if (mch_isdir((char_u *)name))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002813 return FALSE; // trailing slash for a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 return TRUE;
2815}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002816
2817/*
2818 * Special implementation of mch_stat() for Solaris.
2819 */
2820 int
2821vim_stat(const char *name, stat_T *stp)
2822{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002823 // On Solaris stat() accepts "file/" as if it was "file". Return -1 if
2824 // the name ends in "/" and it's not a directory.
Bram Moolenaard8492792017-03-16 12:22:38 +01002825 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002826}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827#endif
2828
2829#if defined(CURSOR_SHAPE) || defined(PROTO)
2830
2831/*
2832 * Handling of cursor and mouse pointer shapes in various modes.
2833 */
2834
2835cursorentry_T shape_table[SHAPE_IDX_COUNT] =
2836{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002837 // The values will be filled in from the 'guicursor' and 'mouseshape'
2838 // defaults when Vim starts.
2839 // Adjust the SHAPE_IDX_ defines when making changes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
2841 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
2842 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
2843 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
2844 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
2845 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
2846 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
2847 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
2848 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
2849 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
2850 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
2851 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
2852 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
2853 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
2854 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
2855 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
2856 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
2857};
2858
2859#ifdef FEAT_MOUSESHAPE
2860/*
2861 * Table with names for mouse shapes. Keep in sync with all the tables for
2862 * mch_set_mouse_shape()!.
2863 */
2864static char * mshape_names[] =
2865{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002866 "arrow", // default, must be the first one
2867 "blank", // hidden
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 "beam",
2869 "updown",
2870 "udsizing",
2871 "leftright",
2872 "lrsizing",
2873 "busy",
2874 "no",
2875 "crosshair",
2876 "hand1",
2877 "hand2",
2878 "pencil",
2879 "question",
2880 "rightup-arrow",
2881 "up-arrow",
2882 NULL
2883};
2884#endif
2885
2886/*
2887 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
2888 * ("what" is SHAPE_MOUSE).
2889 * Returns error message for an illegal option, NULL otherwise.
2890 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002891 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002892parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893{
2894 char_u *modep;
2895 char_u *colonp;
2896 char_u *commap;
2897 char_u *slashp;
2898 char_u *p, *endp;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002899 int idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 int all_idx;
2901 int len;
2902 int i;
2903 long n;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002904 int found_ve = FALSE; // found "ve" flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 int round;
2906
2907 /*
2908 * First round: check for errors; second round: do it for real.
2909 */
2910 for (round = 1; round <= 2; ++round)
2911 {
2912 /*
2913 * Repeat for all comma separated parts.
2914 */
2915#ifdef FEAT_MOUSESHAPE
2916 if (what == SHAPE_MOUSE)
2917 modep = p_mouseshape;
2918 else
2919#endif
2920 modep = p_guicursor;
2921 while (*modep != NUL)
2922 {
2923 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01002924 commap = vim_strchr(modep, ',');
2925
2926 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002927 return N_("E545: Missing colon");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 if (colonp == modep)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002929 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930
2931 /*
2932 * Repeat for all mode's before the colon.
2933 * For the 'a' mode, we loop to handle all the modes.
2934 */
2935 all_idx = -1;
2936 while (modep < colonp || all_idx >= 0)
2937 {
2938 if (all_idx < 0)
2939 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002940 // Find the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 if (modep[1] == '-' || modep[1] == ':')
2942 len = 1;
2943 else
2944 len = 2;
2945 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
2946 all_idx = SHAPE_IDX_COUNT - 1;
2947 else
2948 {
2949 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
2950 if (STRNICMP(modep, shape_table[idx].name, len)
2951 == 0)
2952 break;
2953 if (idx == SHAPE_IDX_COUNT
2954 || (shape_table[idx].used_for & what) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002955 return N_("E546: Illegal mode");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
2957 found_ve = TRUE;
2958 }
2959 modep += len + 1;
2960 }
2961
2962 if (all_idx >= 0)
2963 idx = all_idx--;
2964 else if (round == 2)
2965 {
2966#ifdef FEAT_MOUSESHAPE
2967 if (what == SHAPE_MOUSE)
2968 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002969 // Set the default, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 shape_table[idx].mshape = 0;
2971 }
2972 else
2973#endif
2974 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002975 // Set the defaults, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 shape_table[idx].shape = SHAPE_BLOCK;
2977 shape_table[idx].blinkwait = 700L;
2978 shape_table[idx].blinkon = 400L;
2979 shape_table[idx].blinkoff = 250L;
2980 }
2981 }
2982
Bram Moolenaar85a20022019-12-21 18:25:54 +01002983 // Parse the part after the colon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 for (p = colonp + 1; *p && *p != ','; )
2985 {
2986#ifdef FEAT_MOUSESHAPE
2987 if (what == SHAPE_MOUSE)
2988 {
2989 for (i = 0; ; ++i)
2990 {
2991 if (mshape_names[i] == NULL)
2992 {
2993 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002994 return N_("E547: Illegal mouseshape");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 if (round == 2)
2996 shape_table[idx].mshape =
2997 getdigits(&p) + MSHAPE_NUMBERED;
2998 else
2999 (void)getdigits(&p);
3000 break;
3001 }
3002 len = (int)STRLEN(mshape_names[i]);
3003 if (STRNICMP(p, mshape_names[i], len) == 0)
3004 {
3005 if (round == 2)
3006 shape_table[idx].mshape = i;
3007 p += len;
3008 break;
3009 }
3010 }
3011 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003012 else // if (what == SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013#endif
3014 {
3015 /*
3016 * First handle the ones with a number argument.
3017 */
3018 i = *p;
3019 len = 0;
3020 if (STRNICMP(p, "ver", 3) == 0)
3021 len = 3;
3022 else if (STRNICMP(p, "hor", 3) == 0)
3023 len = 3;
3024 else if (STRNICMP(p, "blinkwait", 9) == 0)
3025 len = 9;
3026 else if (STRNICMP(p, "blinkon", 7) == 0)
3027 len = 7;
3028 else if (STRNICMP(p, "blinkoff", 8) == 0)
3029 len = 8;
3030 if (len != 0)
3031 {
3032 p += len;
3033 if (!VIM_ISDIGIT(*p))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003034 return N_("E548: digit expected");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 n = getdigits(&p);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003036 if (len == 3) // "ver" or "hor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 {
3038 if (n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003039 return N_("E549: Illegal percentage");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 if (round == 2)
3041 {
3042 if (TOLOWER_ASC(i) == 'v')
3043 shape_table[idx].shape = SHAPE_VER;
3044 else
3045 shape_table[idx].shape = SHAPE_HOR;
3046 shape_table[idx].percentage = n;
3047 }
3048 }
3049 else if (round == 2)
3050 {
3051 if (len == 9)
3052 shape_table[idx].blinkwait = n;
3053 else if (len == 7)
3054 shape_table[idx].blinkon = n;
3055 else
3056 shape_table[idx].blinkoff = n;
3057 }
3058 }
3059 else if (STRNICMP(p, "block", 5) == 0)
3060 {
3061 if (round == 2)
3062 shape_table[idx].shape = SHAPE_BLOCK;
3063 p += 5;
3064 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003065 else // must be a highlight group name then
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 {
3067 endp = vim_strchr(p, '-');
Bram Moolenaar85a20022019-12-21 18:25:54 +01003068 if (commap == NULL) // last part
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 {
3070 if (endp == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003071 endp = p + STRLEN(p); // find end of part
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
3073 else if (endp > commap || endp == NULL)
3074 endp = commap;
3075 slashp = vim_strchr(p, '/');
3076 if (slashp != NULL && slashp < endp)
3077 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003078 // "group/langmap_group"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 i = syn_check_group(p, (int)(slashp - p));
3080 p = slashp + 1;
3081 }
3082 if (round == 2)
3083 {
3084 shape_table[idx].id = syn_check_group(p,
3085 (int)(endp - p));
3086 shape_table[idx].id_lm = shape_table[idx].id;
3087 if (slashp != NULL && slashp < endp)
3088 shape_table[idx].id = i;
3089 }
3090 p = endp;
3091 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003092 } // if (what != SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
3094 if (*p == '-')
3095 ++p;
3096 }
3097 }
3098 modep = p;
3099 if (*modep == ',')
3100 ++modep;
3101 }
3102 }
3103
Bram Moolenaar85a20022019-12-21 18:25:54 +01003104 // If the 's' flag is not given, use the 'v' cursor for 's'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 if (!found_ve)
3106 {
3107#ifdef FEAT_MOUSESHAPE
3108 if (what == SHAPE_MOUSE)
3109 {
3110 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
3111 }
3112 else
3113#endif
3114 {
3115 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
3116 shape_table[SHAPE_IDX_VE].percentage =
3117 shape_table[SHAPE_IDX_V].percentage;
3118 shape_table[SHAPE_IDX_VE].blinkwait =
3119 shape_table[SHAPE_IDX_V].blinkwait;
3120 shape_table[SHAPE_IDX_VE].blinkon =
3121 shape_table[SHAPE_IDX_V].blinkon;
3122 shape_table[SHAPE_IDX_VE].blinkoff =
3123 shape_table[SHAPE_IDX_V].blinkoff;
3124 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
3125 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
3126 }
3127 }
3128
3129 return NULL;
3130}
3131
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003132# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3133 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134/*
3135 * Return the index into shape_table[] for the current mode.
3136 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
3137 */
3138 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003139get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140{
3141#ifdef FEAT_MOUSESHAPE
3142 if (mouse && (State == HITRETURN || State == ASKMORE))
3143 {
3144# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00003145 int x, y;
3146 gui_mch_getmouse(&x, &y);
3147 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 return SHAPE_IDX_MOREL;
3149# endif
3150 return SHAPE_IDX_MORE;
3151 }
3152 if (mouse && drag_status_line)
3153 return SHAPE_IDX_SDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 if (mouse && drag_sep_line)
3155 return SHAPE_IDX_VDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156#endif
3157 if (!mouse && State == SHOWMATCH)
3158 return SHAPE_IDX_SM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 if (State & VREPLACE_FLAG)
3160 return SHAPE_IDX_R;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (State & REPLACE_FLAG)
3162 return SHAPE_IDX_R;
3163 if (State & INSERT)
3164 return SHAPE_IDX_I;
3165 if (State & CMDLINE)
3166 {
3167 if (cmdline_at_end())
3168 return SHAPE_IDX_C;
3169 if (cmdline_overstrike())
3170 return SHAPE_IDX_CR;
3171 return SHAPE_IDX_CI;
3172 }
3173 if (finish_op)
3174 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 if (VIsual_active)
3176 {
3177 if (*p_sel == 'e')
3178 return SHAPE_IDX_VE;
3179 else
3180 return SHAPE_IDX_V;
3181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 return SHAPE_IDX_N;
3183}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
3186# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3187static int old_mouse_shape = 0;
3188
3189/*
3190 * Set the mouse shape:
3191 * If "shape" is -1, use shape depending on the current mode,
3192 * depending on the current state.
3193 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
3194 * when the mouse moves off the status or command line).
3195 */
3196 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003197update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
3199 int new_mouse_shape;
3200
Bram Moolenaar85a20022019-12-21 18:25:54 +01003201 // Only works in GUI mode.
Bram Moolenaar6bb68362005-03-22 23:03:44 +00003202 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 return;
3204
Bram Moolenaar85a20022019-12-21 18:25:54 +01003205 // Postpone the updating when more is to come. Speeds up executing of
3206 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 if (shape_idx == -1 && char_avail())
3208 {
3209 postponed_mouseshape = TRUE;
3210 return;
3211 }
3212
Bram Moolenaar85a20022019-12-21 18:25:54 +01003213 // When ignoring the mouse don't change shape on the statusline.
Bram Moolenaar14716812006-05-04 21:54:08 +00003214 if (*p_mouse == NUL
3215 && (shape_idx == SHAPE_IDX_CLINE
3216 || shape_idx == SHAPE_IDX_STATUS
3217 || shape_idx == SHAPE_IDX_VSEP))
3218 shape_idx = -2;
3219
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 if (shape_idx == -2
3221 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
3222 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
3223 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
3224 return;
3225 if (shape_idx < 0)
3226 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
3227 else
3228 new_mouse_shape = shape_table[shape_idx].mshape;
3229 if (new_mouse_shape != old_mouse_shape)
3230 {
3231 mch_set_mouse_shape(new_mouse_shape);
3232 old_mouse_shape = new_mouse_shape;
3233 }
3234 postponed_mouseshape = FALSE;
3235}
3236# endif
3237
Bram Moolenaar85a20022019-12-21 18:25:54 +01003238#endif // CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
3240
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241/*
3242 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
3243 * 'cdpath' for relative directory names, otherwise just mch_chdir().
3244 */
3245 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003246vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
3248#ifndef FEAT_SEARCHPATH
3249 return mch_chdir((char *)new_dir);
3250#else
3251 char_u *dir_name;
3252 int r;
3253
3254 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
3255 FNAME_MESS, curbuf->b_ffname);
3256 if (dir_name == NULL)
3257 return -1;
3258 r = mch_chdir((char *)dir_name);
3259 vim_free(dir_name);
3260 return r;
3261#endif
3262}
3263
3264/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003265 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003267 * Some systems are quite slow in obtaining the user name (Windows NT), thus
3268 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 * Returns OK or FAIL.
3270 */
3271 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003272get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003274 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 {
3276 if (mch_get_user_name(buf, len) == FAIL)
3277 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003281 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 return OK;
3283}
3284
3285#ifndef HAVE_QSORT
3286/*
3287 * Our own qsort(), for systems that don't have it.
3288 * It's simple and slow. From the K&R C book.
3289 */
3290 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003291qsort(
3292 void *base,
3293 size_t elm_count,
3294 size_t elm_size,
3295 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
3297 char_u *buf;
3298 char_u *p1;
3299 char_u *p2;
3300 int i, j;
3301 int gap;
3302
Bram Moolenaar964b3742019-05-24 18:54:09 +02003303 buf = alloc(elm_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 if (buf == NULL)
3305 return;
3306
3307 for (gap = elm_count / 2; gap > 0; gap /= 2)
3308 for (i = gap; i < elm_count; ++i)
3309 for (j = i - gap; j >= 0; j -= gap)
3310 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003311 // Compare the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 p1 = (char_u *)base + j * elm_size;
3313 p2 = (char_u *)base + (j + gap) * elm_size;
3314 if ((*cmp)((void *)p1, (void *)p2) <= 0)
3315 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003316 // Exchange the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 mch_memmove(buf, p1, elm_size);
3318 mch_memmove(p1, p2, elm_size);
3319 mch_memmove(p2, buf, elm_size);
3320 }
3321
3322 vim_free(buf);
3323}
3324#endif
3325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 * The putenv() implementation below comes from the "screen" program.
3328 * Included with permission from Juergen Weigert.
3329 * See pty.c for the copyright notice.
3330 */
3331
3332/*
3333 * putenv -- put value into environment
3334 *
3335 * Usage: i = putenv (string)
3336 * int i;
3337 * char *string;
3338 *
3339 * where string is of the form <name>=<value>.
3340 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
3341 *
3342 * Putenv may need to add a new name into the environment, or to
3343 * associate a value longer than the current value with a particular
3344 * name. So, to make life simpler, putenv() copies your entire
3345 * environment into the heap (i.e. malloc()) from the stack
3346 * (i.e. where it resides when your process is initiated) the first
3347 * time you call it.
3348 *
3349 * (history removed, not very interesting. See the "screen" sources.)
3350 */
3351
3352#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
3353
Bram Moolenaar85a20022019-12-21 18:25:54 +01003354#define EXTRASIZE 5 // increment to add to env. size
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
Bram Moolenaar85a20022019-12-21 18:25:54 +01003356static int envsize = -1; // current size of environment
3357extern char **environ; // the global which is your env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
Bram Moolenaar85a20022019-12-21 18:25:54 +01003359static int findenv(char *name); // look for a name in the env.
3360static int newenv(void); // copy env. from stack to heap
3361static int moreenv(void); // incr. size of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
3363 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003364putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
3366 int i;
3367 char *p;
3368
3369 if (envsize < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003370 { // first time putenv called
3371 if (newenv() < 0) // copy env. to heap
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 return -1;
3373 }
3374
Bram Moolenaar85a20022019-12-21 18:25:54 +01003375 i = findenv((char *)string); // look for name in environment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
3377 if (i < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003378 { // name must be added
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 for (i = 0; environ[i]; i++);
3380 if (i >= (envsize - 1))
Bram Moolenaar85a20022019-12-21 18:25:54 +01003381 { // need new slot
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (moreenv() < 0)
3383 return -1;
3384 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003385 p = alloc(strlen(string) + 1);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003386 if (p == NULL) // not enough core
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 return -1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003388 environ[i + 1] = 0; // new end of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
3390 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003391 { // name already in env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 p = vim_realloc(environ[i], strlen(string) + 1);
3393 if (p == NULL)
3394 return -1;
3395 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003396 sprintf(p, "%s", string); // copy into env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 environ[i] = p;
3398
3399 return 0;
3400}
3401
3402 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003403findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404{
3405 char *namechar, *envchar;
3406 int i, found;
3407
3408 found = 0;
3409 for (i = 0; environ[i] && !found; i++)
3410 {
3411 envchar = environ[i];
3412 namechar = name;
3413 while (*namechar && *namechar != '=' && (*namechar == *envchar))
3414 {
3415 namechar++;
3416 envchar++;
3417 }
3418 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
3419 }
3420 return found ? i - 1 : -1;
3421}
3422
3423 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003424newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425{
3426 char **env, *elem;
3427 int i, esize;
3428
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 for (i = 0; environ[i]; i++)
3430 ;
Bram Moolenaard0573012017-10-28 21:11:06 +02003431
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 esize = i + EXTRASIZE + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003433 env = ALLOC_MULT(char *, esize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 if (env == NULL)
3435 return -1;
3436
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 for (i = 0; environ[i]; i++)
3438 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003439 elem = alloc(strlen(environ[i]) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (elem == NULL)
3441 return -1;
3442 env[i] = elem;
3443 strcpy(elem, environ[i]);
3444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
3446 env[i] = 0;
3447 environ = env;
3448 envsize = esize;
3449 return 0;
3450}
3451
3452 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003453moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
3455 int esize;
3456 char **env;
3457
3458 esize = envsize + EXTRASIZE;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003459 env = vim_realloc((char *)environ, esize * sizeof (*env));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 if (env == 0)
3461 return -1;
3462 environ = env;
3463 envsize = esize;
3464 return 0;
3465}
3466
3467# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02003468/*
3469 * Used for mch_getenv() for Mac.
3470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003472vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
3474 int i;
3475 char_u *p;
3476
3477 if (envsize < 0)
3478 return NULL;
3479
3480 i = findenv((char *)string);
3481
3482 if (i < 0)
3483 return NULL;
3484
3485 p = vim_strchr((char_u *)environ[i], '=');
3486 return (p + 1);
3487}
3488# endif
3489
Bram Moolenaar85a20022019-12-21 18:25:54 +01003490#endif // !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003491
Bram Moolenaarc4956c82006-03-12 21:58:43 +00003492#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003493/*
3494 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3495 * rights to write into.
3496 */
3497 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003498filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003499{
3500 int retval = 0;
3501#if defined(UNIX) || defined(VMS)
3502 int perm = 0;
3503#endif
3504
3505#if defined(UNIX) || defined(VMS)
3506 perm = mch_getperm(fname);
3507#endif
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003508 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01003509# ifdef MSWIN
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003510 mch_writable(fname) &&
3511# else
3512# if defined(UNIX) || defined(VMS)
3513 (perm & 0222) &&
3514# endif
3515# endif
3516 mch_access((char *)fname, W_OK) == 0
3517 )
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00003518 {
3519 ++retval;
3520 if (mch_isdir(fname))
3521 ++retval;
3522 }
3523 return retval;
3524}
3525#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00003526
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003527#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
3528/*
3529 * Read 2 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003530 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003531 */
3532 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003533get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003534{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003535 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003536
3537 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003538 if (n == EOF) return -1;
3539 c = getc(fd);
3540 if (c == EOF) return -1;
3541 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003542}
3543
3544/*
3545 * Read 3 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003546 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003547 */
3548 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003549get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003550{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003551 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003552
3553 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003554 if (n == EOF) return -1;
3555 c = getc(fd);
3556 if (c == EOF) return -1;
3557 n = (n << 8) + c;
3558 c = getc(fd);
3559 if (c == EOF) return -1;
3560 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003561}
3562
3563/*
3564 * Read 4 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003565 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003566 */
3567 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003568get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003569{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003570 int c;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003571 // Use unsigned rather than int otherwise result is undefined
3572 // when left-shift sets the MSB.
Bram Moolenaar95235e62013-09-08 16:07:07 +02003573 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003574
Bram Moolenaare26e0d22018-03-20 12:34:04 +01003575 c = getc(fd);
3576 if (c == EOF) return -1;
3577 n = (unsigned)c;
3578 c = getc(fd);
3579 if (c == EOF) return -1;
3580 n = (n << 8) + (unsigned)c;
3581 c = getc(fd);
3582 if (c == EOF) return -1;
3583 n = (n << 8) + (unsigned)c;
3584 c = getc(fd);
3585 if (c == EOF) return -1;
3586 n = (n << 8) + (unsigned)c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02003587 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003588}
3589
3590/*
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003591 * Read a string of length "cnt" from "fd" into allocated memory.
3592 * Returns NULL when out of memory or unable to read that many bytes.
3593 */
3594 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003595read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003596{
3597 char_u *str;
3598 int i;
3599 int c;
3600
Bram Moolenaar85a20022019-12-21 18:25:54 +01003601 // allocate memory
Bram Moolenaar964b3742019-05-24 18:54:09 +02003602 str = alloc(cnt + 1);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003603 if (str != NULL)
3604 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003605 // Read the string. Quit when running into the EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003606 for (i = 0; i < cnt; ++i)
3607 {
3608 c = getc(fd);
3609 if (c == EOF)
3610 {
3611 vim_free(str);
3612 return NULL;
3613 }
3614 str[i] = c;
3615 }
3616 str[i] = NUL;
3617 }
3618 return str;
3619}
3620
3621/*
3622 * Write a number to file "fd", MSB first, in "len" bytes.
3623 */
3624 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003625put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003626{
3627 int i;
3628
3629 for (i = len - 1; i >= 0; --i)
3630 if (putc((int)(nr >> (i * 8)), fd) == EOF)
3631 return FAIL;
3632 return OK;
3633}
3634
Bram Moolenaarcdf04202010-05-29 15:11:47 +02003635#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01003636
Bram Moolenaar85a20022019-12-21 18:25:54 +01003637#ifndef PROTO // proto is defined in vim.h
Bram Moolenaar51628222016-12-01 23:03:28 +01003638# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003639/*
3640 * Return time in msec since "start_tv".
3641 */
3642 long
3643elapsed(struct timeval *start_tv)
3644{
3645 struct timeval now_tv;
3646
3647 gettimeofday(&now_tv, NULL);
3648 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
3649 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
3650}
Bram Moolenaar51628222016-12-01 23:03:28 +01003651# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003652
Bram Moolenaar51628222016-12-01 23:03:28 +01003653# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003654/*
3655 * Return time in msec since "start_tick".
3656 */
3657 long
3658elapsed(DWORD start_tick)
3659{
3660 DWORD now = GetTickCount();
3661
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003662 return (long)now - (long)start_tick;
3663}
Bram Moolenaar51628222016-12-01 23:03:28 +01003664# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003665#endif
Bram Moolenaar20608922018-04-21 22:30:08 +02003666
3667#if defined(FEAT_JOB_CHANNEL) \
3668 || (defined(UNIX) && (!defined(USE_SYSTEM) \
3669 || (defined(FEAT_GUI) && defined(FEAT_TERMINAL)))) \
3670 || defined(PROTO)
3671/*
3672 * Parse "cmd" and put the white-separated parts in "argv".
3673 * "argv" is an allocated array with "argc" entries and room for 4 more.
3674 * Returns FAIL when out of memory.
3675 */
3676 int
3677mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
3678{
3679 int i;
3680 char_u *p, *d;
3681 int inquote;
3682
3683 /*
3684 * Do this loop twice:
3685 * 1: find number of arguments
3686 * 2: separate them and build argv[]
3687 */
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003688 for (i = 1; i <= 2; ++i)
Bram Moolenaar20608922018-04-21 22:30:08 +02003689 {
3690 p = skipwhite(cmd);
3691 inquote = FALSE;
3692 *argc = 0;
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003693 while (*p != NUL)
Bram Moolenaar20608922018-04-21 22:30:08 +02003694 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003695 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003696 (*argv)[*argc] = (char *)p;
3697 ++*argc;
3698 d = p;
3699 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
3700 {
3701 if (p[0] == '"')
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02003702 // quotes surrounding an argument and are dropped
Bram Moolenaar20608922018-04-21 22:30:08 +02003703 inquote = !inquote;
3704 else
3705 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02003706 if (rem_backslash(p))
Bram Moolenaar20608922018-04-21 22:30:08 +02003707 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02003708 // First pass: skip over "\ " and "\"".
3709 // Second pass: Remove the backslash.
Bram Moolenaar20608922018-04-21 22:30:08 +02003710 ++p;
3711 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003712 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003713 *d++ = *p;
3714 }
3715 ++p;
3716 }
3717 if (*p == NUL)
3718 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003719 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003720 *d++ = NUL;
3721 break;
3722 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003723 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003724 *d++ = NUL;
3725 p = skipwhite(p + 1);
3726 }
3727 if (*argv == NULL)
3728 {
3729 if (use_shcf)
3730 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003731 // Account for possible multiple args in p_shcf.
Bram Moolenaar20608922018-04-21 22:30:08 +02003732 p = p_shcf;
3733 for (;;)
3734 {
3735 p = skiptowhite(p);
3736 if (*p == NUL)
3737 break;
3738 ++*argc;
3739 p = skipwhite(p);
3740 }
3741 }
3742
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003743 *argv = ALLOC_MULT(char *, *argc + 4);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003744 if (*argv == NULL) // out of memory
Bram Moolenaar20608922018-04-21 22:30:08 +02003745 return FAIL;
3746 }
3747 }
3748 return OK;
3749}
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003750
3751# if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
3752/*
3753 * Build "argv[argc]" from the string "cmd".
3754 * "argv[argc]" is set to NULL;
3755 * Return FAIL when out of memory.
3756 */
3757 int
3758build_argv_from_string(char_u *cmd, char ***argv, int *argc)
3759{
3760 char_u *cmd_copy;
3761 int i;
3762
Bram Moolenaar85a20022019-12-21 18:25:54 +01003763 // Make a copy, parsing will modify "cmd".
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003764 cmd_copy = vim_strsave(cmd);
3765 if (cmd_copy == NULL
3766 || mch_parse_cmd(cmd_copy, FALSE, argv, argc) == FAIL)
3767 {
3768 vim_free(cmd_copy);
3769 return FAIL;
3770 }
3771 for (i = 0; i < *argc; i++)
3772 (*argv)[i] = (char *)vim_strsave((char_u *)(*argv)[i]);
3773 (*argv)[*argc] = NULL;
3774 vim_free(cmd_copy);
3775 return OK;
3776}
3777
3778/*
3779 * Build "argv[argc]" from the list "l".
3780 * "argv[argc]" is set to NULL;
3781 * Return FAIL when out of memory.
3782 */
3783 int
3784build_argv_from_list(list_T *l, char ***argv, int *argc)
3785{
3786 listitem_T *li;
3787 char_u *s;
3788
Bram Moolenaar85a20022019-12-21 18:25:54 +01003789 // Pass argv[] to mch_call_shell().
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003790 *argv = ALLOC_MULT(char *, l->lv_len + 1);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003791 if (*argv == NULL)
3792 return FAIL;
3793 *argc = 0;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003794 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003796 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003797 if (s == NULL)
3798 {
3799 int i;
3800
3801 for (i = 0; i < *argc; ++i)
Bram Moolenaardf195602020-04-13 18:13:33 +02003802 VIM_CLEAR((*argv)[i]);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003803 return FAIL;
3804 }
3805 (*argv)[*argc] = (char *)vim_strsave(s);
3806 *argc += 1;
3807 }
3808 (*argv)[*argc] = NULL;
3809 return OK;
3810}
3811# endif
Bram Moolenaar20608922018-04-21 22:30:08 +02003812#endif
Bram Moolenaar57da6982019-09-13 22:30:11 +02003813
3814/*
3815 * Change the behavior of vterm.
3816 * 0: As usual.
3817 * 1: Windows 10 version 1809
3818 * The bug causes unstable handling of ambiguous width character.
Bram Moolenaar36e7a822019-11-13 21:49:24 +01003819 * 2: Windows 10 version 1903 & 1909
Bram Moolenaar57da6982019-09-13 22:30:11 +02003820 * Use the wrong result because each result is different.
3821 * 3: Windows 10 insider preview (current latest logic)
3822 */
3823 int
3824get_special_pty_type(void)
3825{
3826#ifdef MSWIN
3827 return get_conpty_type();
3828#else
3829 return 0;
3830#endif
3831}