blob: 38dcb48d178050eaeb69c246cada7330583948c4 [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
Bram Moolenaar24959102022-05-07 20:01:16 +010033 || ((cur_ve_flags & VE_BLOCK) && VIsual_active
34 && VIsual_mode == Ctrl_V)
35 || ((cur_ve_flags & VE_INSERT) && (State & MODE_INSERT)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000036}
37
38/*
39 * Get the screen position of the cursor.
40 */
41 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010042getviscol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000043{
44 colnr_T x;
45
46 getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL);
47 return (int)x;
48}
49
50/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +000051 * Go to column "wcol", and add/insert white space as necessary to get the
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 * cursor in that column.
53 * The caller must have saved the cursor line for undo!
54 */
55 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010056coladvance_force(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000057{
58 int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
59
60 if (wcol == MAXCOL)
61 curwin->w_valid &= ~VALID_VIRTCOL;
62 else
63 {
Bram Moolenaar85a20022019-12-21 18:25:54 +010064 // Virtcol is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 curwin->w_valid |= VALID_VIRTCOL;
66 curwin->w_virtcol = wcol;
67 }
68 return rc;
69}
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71/*
Bram Moolenaar977239e2019-01-11 16:16:01 +010072 * Get the screen position of character col with a coladd in the cursor line.
73 */
74 int
Bram Moolenaarfc3abf42019-01-24 15:54:21 +010075getviscol2(colnr_T col, colnr_T coladd UNUSED)
Bram Moolenaar977239e2019-01-11 16:16:01 +010076{
77 colnr_T x;
78 pos_T pos;
79
80 pos.lnum = curwin->w_cursor.lnum;
81 pos.col = col;
Bram Moolenaar977239e2019-01-11 16:16:01 +010082 pos.coladd = coladd;
Bram Moolenaar977239e2019-01-11 16:16:01 +010083 getvvcol(curwin, &pos, &x, NULL, NULL);
84 return (int)x;
85}
86
87/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 * Try to advance the Cursor to the specified screen column.
89 * If virtual editing: fine tune the cursor position.
90 * Note that all virtual positions off the end of a line should share
91 * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)),
92 * beginning at coladd 0.
93 *
94 * return OK if desired column is reached, FAIL if not
95 */
96 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010097coladvance(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000098{
99 int rc = getvpos(&curwin->w_cursor, wcol);
100
101 if (wcol == MAXCOL || rc == FAIL)
102 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000103 else if (*ml_get_cursor() != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100105 // Virtcol is valid when not on a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 curwin->w_valid |= VALID_VIRTCOL;
107 curwin->w_virtcol = wcol;
108 }
109 return rc;
110}
111
112/*
113 * Return in "pos" the position of the cursor advanced to screen column "wcol".
114 * return OK if desired column is reached, FAIL if not
115 */
116 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100117getvpos(pos_T *pos, colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 return coladvance2(pos, FALSE, virtual_active(), wcol);
120}
121
122 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100123coladvance2(
124 pos_T *pos,
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200125 int addspaces, // change the text to achieve our goal?
126 int finetune, // change char offset for the exact column
127 colnr_T wcol_arg) // column to move to (can be negative)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128{
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200129 colnr_T wcol = wcol_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 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 Moolenaar24959102022-05-07 20:01:16 +0100139 one_more = (State & MODE_INSERT)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000140 || 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 Moolenaar7f9969c2022-07-25 18:13:54 +0100160 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161
Bram Moolenaarebefac62005-12-28 22:39:57 +0000162 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 && curwin->w_p_wrap
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 && curwin->w_width != 0
Bram Moolenaar02f86942021-08-17 22:14:29 +0200165 && wcol >= (colnr_T)width
166 && width > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 {
168 csize = linetabsize(line);
169 if (csize > 0)
170 csize--;
171
Bram Moolenaarebefac62005-12-28 22:39:57 +0000172 if (wcol / width > (colnr_T)csize / width
Bram Moolenaar24959102022-05-07 20:01:16 +0100173 && ((State & MODE_INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100175 // In case of line wrapping don't move the cursor beyond the
176 // right screen edge. In Insert mode allow going just beyond
177 // the last character (like what happens when typing and
178 // reaching the right window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 wcol = (csize / width + 1) * width - 1;
180 }
181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100183 init_chartabsize_arg(&cts, curwin, pos->lnum, 0, line, line);
184 while (cts.cts_vcol <= wcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100186 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#ifdef FEAT_LINEBREAK
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100188 csize = win_lbr_chartabsize(&cts, &head);
189 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100191 csize = lbr_chartabsize_adv(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100193 cts.cts_vcol += csize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100195 col = cts.cts_vcol;
196 idx = (int)(cts.cts_ptr - line);
197 clear_chartabsize_arg(&cts);
198
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 /*
200 * Handle all the special cases. The virtual_active() check
201 * is needed to ensure that a virtual position off the end of
202 * a line has the correct indexing. The one_more comparison
203 * replaces an explicit add of one_more later on.
204 */
205 if (col > wcol || (!virtual_active() && one_more == 0))
206 {
207 idx -= 1;
208# ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +0100209 // Don't count the chars from 'showbreak'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 csize -= head;
211# endif
212 col -= csize;
213 }
214
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 if (virtual_active()
216 && addspaces
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200217 && wcol >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 && ((col != wcol && col != wcol + 1) || csize > 1))
219 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100220 // 'virtualedit' is set: The difference between wcol and col is
221 // filled with spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
223 if (line[idx] == NUL)
224 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100225 // Append spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 int correct = wcol - col;
227 char_u *newline = alloc(idx + correct + 1);
228 int t;
229
230 if (newline == NULL)
231 return FAIL;
232
233 for (t = 0; t < idx; ++t)
234 newline[t] = line[t];
235
236 for (t = 0; t < correct; ++t)
237 newline[t + idx] = ' ';
238
239 newline[idx + correct] = NUL;
240
241 ml_replace(pos->lnum, newline, FALSE);
242 changed_bytes(pos->lnum, (colnr_T)idx);
243 idx += correct;
244 col = wcol;
245 }
246 else
247 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100248 // Break a tab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 int linelen = (int)STRLEN(line);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100250 int correct = wcol - col - csize + 1; // negative!!
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000251 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 int t, s = 0;
253 int v;
254
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000255 if (-correct > csize)
256 return FAIL;
257
258 newline = alloc(linelen + csize);
259 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 return FAIL;
261
262 for (t = 0; t < linelen; t++)
263 {
264 if (t != idx)
265 newline[s++] = line[t];
266 else
267 for (v = 0; v < csize; v++)
268 newline[s++] = ' ';
269 }
270
271 newline[linelen + csize - 1] = NUL;
272
273 ml_replace(pos->lnum, newline, FALSE);
274 changed_bytes(pos->lnum, idx);
275 idx += (csize - 1 + correct);
276 col += correct;
277 }
278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 }
280
281 if (idx < 0)
282 pos->col = 0;
283 else
284 pos->col = idx;
285
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 pos->coladd = 0;
287
288 if (finetune)
289 {
290 if (wcol == MAXCOL)
291 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100292 // The width of the last character is used to set coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 if (!one_more)
294 {
295 colnr_T scol, ecol;
296
297 getvcol(curwin, pos, &scol, NULL, &ecol);
298 pos->coladd = ecol - scol;
299 }
300 }
301 else
302 {
303 int b = (int)wcol - (int)col;
304
Bram Moolenaar85a20022019-12-21 18:25:54 +0100305 // The difference between wcol and col is used to set coladd.
Bram Moolenaar02631462017-09-22 15:20:32 +0200306 if (b > 0 && b < (MAXCOL - 2 * curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 pos->coladd = b;
308
309 col += b;
310 }
311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
Bram Moolenaar85a20022019-12-21 18:25:54 +0100313 // prevent from moving onto a trail byte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200315 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200317 if (wcol < 0 || col < wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 return FAIL;
319 return OK;
320}
321
322/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000323 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 */
325 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100326inc_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327{
328 return inc(&curwin->w_cursor);
329}
330
Bram Moolenaar446cb832008-06-24 21:56:24 +0000331/*
332 * Increment the line pointer "lp" crossing line boundaries as necessary.
333 * Return 1 when going to the next line.
334 * Return 2 when moving forward onto a NUL at the end of the line).
335 * Return -1 when at the end of file.
336 * Return 0 otherwise.
337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100339inc(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100341 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
Bram Moolenaar85a20022019-12-21 18:25:54 +0100343 // when searching position may be set to end of a line
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100344 if (lp->col != MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100346 p = ml_get_pos(lp);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100347 if (*p != NUL) // still within line, move to next char (may be NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100349 if (has_mbyte)
350 {
351 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100353 lp->col += l;
354 return ((p[l] != NUL) ? 0 : 2);
355 }
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100356 lp->col++;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100357 lp->coladd = 0;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100358 return ((p[1] != NUL) ? 0 : 2);
359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100361 if (lp->lnum != curbuf->b_ml.ml_line_count) // there is a next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {
363 lp->col = 0;
364 lp->lnum++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 lp->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 return 1;
367 }
368 return -1;
369}
370
371/*
372 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
373 */
374 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100375incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376{
377 int r;
378
379 if ((r = inc(lp)) >= 1 && lp->col)
380 r = inc(lp);
381 return r;
382}
383
384/*
385 * dec(p)
386 *
387 * Decrement the line pointer 'p' crossing line boundaries as necessary.
388 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
389 */
390 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100391dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100393 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394}
395
396 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100397dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
399 char_u *p;
400
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 lp->coladd = 0;
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100402 if (lp->col == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100404 // past end of line
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100405 p = ml_get(lp->lnum);
406 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100407 if (has_mbyte)
408 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100409 return 0;
410 }
411
412 if (lp->col > 0)
413 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100414 // still within line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 lp->col--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 if (has_mbyte)
417 {
418 p = ml_get(lp->lnum);
419 lp->col -= (*mb_head_off)(p, p + lp->col);
420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 return 0;
422 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100423
424 if (lp->lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100426 // there is a prior line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 lp->lnum--;
428 p = ml_get(lp->lnum);
429 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 if (has_mbyte)
431 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 return 1;
433 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100434
Bram Moolenaar85a20022019-12-21 18:25:54 +0100435 // at start of file
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100436 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437}
438
439/*
440 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
441 */
442 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100443decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444{
445 int r;
446
447 if ((r = dec(lp)) == 1 && lp->col)
448 r = dec(lp);
449 return r;
450}
451
452/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200453 * Get the line number relative to the current cursor position, i.e. the
454 * difference between line number and cursor position. Only look for lines that
455 * can be visible, folded lines don't count.
456 */
457 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100458get_cursor_rel_lnum(
459 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100460 linenr_T lnum) // line number to get the result for
Bram Moolenaar64486672010-05-16 15:46:46 +0200461{
462 linenr_T cursor = wp->w_cursor.lnum;
463 linenr_T retval = 0;
464
465#ifdef FEAT_FOLDING
466 if (hasAnyFolding(wp))
467 {
468 if (lnum > cursor)
469 {
470 while (lnum > cursor)
471 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100472 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100473 // if lnum and cursor are in the same fold,
474 // now lnum <= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200475 if (lnum > cursor)
476 retval++;
477 lnum--;
478 }
479 }
480 else if (lnum < cursor)
481 {
482 while (lnum < cursor)
483 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100484 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100485 // if lnum and cursor are in the same fold,
486 // now lnum >= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200487 if (lnum < cursor)
488 retval--;
489 lnum++;
490 }
491 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100492 // else if (lnum == cursor)
493 // retval = 0;
Bram Moolenaar64486672010-05-16 15:46:46 +0200494 }
495 else
496#endif
497 retval = lnum - cursor;
498
499 return retval;
500}
501
502/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200503 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
504 * This allows for the col to be on the NUL byte.
505 */
506 void
507check_pos(buf_T *buf, pos_T *pos)
508{
509 char_u *line;
510 colnr_T len;
511
512 if (pos->lnum > buf->b_ml.ml_line_count)
513 pos->lnum = buf->b_ml.ml_line_count;
514
515 if (pos->col > 0)
516 {
517 line = ml_get_buf(buf, pos->lnum, FALSE);
518 len = (colnr_T)STRLEN(line);
519 if (pos->col > len)
520 pos->col = len;
521 }
522}
523
524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 * Make sure curwin->w_cursor.lnum is valid.
526 */
527 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100528check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529{
530 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
531 {
532#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100533 // If there is a closed fold at the end of the file, put the cursor in
534 // its first line. Otherwise in the last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 if (!hasFolding(curbuf->b_ml.ml_line_count,
536 &curwin->w_cursor.lnum, NULL))
537#endif
538 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
539 }
540 if (curwin->w_cursor.lnum <= 0)
541 curwin->w_cursor.lnum = 1;
542}
543
544/*
545 * Make sure curwin->w_cursor.col is valid.
546 */
547 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100548check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200550 check_cursor_col_win(curwin);
551}
552
553/*
554 * Make sure win->w_cursor.col is valid.
555 */
556 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100557check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200558{
Gary Johnson53ba05b2021-07-26 22:19:10 +0200559 colnr_T len;
560 colnr_T oldcol = win->w_cursor.col;
561 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
562 unsigned int cur_ve_flags = get_ve_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200564 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200566 win->w_cursor.col = 0;
567 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100569 // Allow cursor past end-of-line when:
570 // - in Insert mode or restarting Insert mode
571 // - in Visual mode and 'selection' isn't "old"
572 // - 'virtualedit' is set
Bram Moolenaar24959102022-05-07 20:01:16 +0100573 if ((State & MODE_INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 || (VIsual_active && *p_sel != 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +0200575 || (cur_ve_flags & VE_ONEMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200577 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000579 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200580 win->w_cursor.col = len - 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100581 // Move the cursor to the head byte.
Bram Moolenaar87c19962007-04-26 08:54:21 +0000582 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200583 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200586 else if (win->w_cursor.col < 0)
587 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588
Bram Moolenaar85a20022019-12-21 18:25:54 +0100589 // If virtual editing is on, we can leave the cursor on the old position,
590 // only we must set it to virtual. But don't do it when at the end of the
591 // line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200593 win->w_cursor.coladd = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +0200594 else if (cur_ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000595 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200596 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200597 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200598 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaard41babe2017-08-30 17:01:35 +0200599
Bram Moolenaar85a20022019-12-21 18:25:54 +0100600 // Make sure that coladd is not more than the char width.
601 // Not for the last character, coladd is then used when the cursor
602 // is actually after the last character.
Bram Moolenaarfe154992022-03-22 20:42:12 +0000603 if (win->w_cursor.col + 1 < len)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200604 {
605 int cs, ce;
606
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200607 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
608 if (win->w_cursor.coladd > ce - cs)
609 win->w_cursor.coladd = ce - cs;
610 }
611 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000612 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100613 // avoid weird number when there is a miscalculation or overflow
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200614 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616}
617
618/*
619 * make sure curwin->w_cursor in on a valid character
620 */
621 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100622check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
624 check_cursor_lnum();
625 check_cursor_col();
626}
627
Bram Moolenaar7ce5b2b2022-05-16 19:40:59 +0100628/*
629 * Check if VIsual position is valid, correct it if not.
630 * Can be called when in Visual mode and a change has been made.
631 */
632 void
633check_visual_pos(void)
634{
635 if (VIsual.lnum > curbuf->b_ml.ml_line_count)
636 {
637 VIsual.lnum = curbuf->b_ml.ml_line_count;
638 VIsual.col = 0;
639 VIsual.coladd = 0;
640 }
641 else
642 {
643 int len = (int)STRLEN(ml_get(VIsual.lnum));
644
645 if (VIsual.col > len)
646 {
647 VIsual.col = len;
648 VIsual.coladd = 0;
649 }
650 }
651}
652
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653/*
654 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
655 * Allow it when in Visual mode and 'selection' is not "old".
656 */
657 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100658adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
660 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 && gchar_cursor() == NUL)
663 --curwin->w_cursor.col;
664}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
666/*
667 * When curwin->w_leftcol has changed, adjust the cursor position.
668 * Return TRUE if the cursor was moved.
669 */
670 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100671leftcol_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672{
673 long lastcol;
674 colnr_T s, e;
675 int retval = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100676 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 changed_cline_bef_curs();
Bram Moolenaar02631462017-09-22 15:20:32 +0200679 lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 validate_virtcol();
681
682 /*
683 * If the cursor is right or left of the screen, move it to last or first
684 * character.
685 */
Bram Moolenaar375e3392019-01-31 18:26:10 +0100686 if (curwin->w_virtcol > (colnr_T)(lastcol - siso))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
688 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100689 coladvance((colnr_T)(lastcol - siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100691 else if (curwin->w_virtcol < curwin->w_leftcol + siso)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
693 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100694 (void)coladvance((colnr_T)(curwin->w_leftcol + siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 }
696
697 /*
698 * If the start of the character under the cursor is not on the screen,
699 * advance the cursor one more char. If this fails (last char of the
700 * line) adjust the scrolling.
701 */
702 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
703 if (e > (colnr_T)lastcol)
704 {
705 retval = TRUE;
706 coladvance(s - 1);
707 }
708 else if (s < curwin->w_leftcol)
709 {
710 retval = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100711 if (coladvance(e + 1) == FAIL) // there isn't another character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100713 curwin->w_leftcol = s; // adjust w_leftcol instead
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 changed_cline_bef_curs();
715 }
716 }
717
718 if (retval)
719 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100720 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 return retval;
722}
723
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 * Isolate one part of a string option where parts are separated with
726 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +0000727 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 * "*option" is advanced to the next part.
729 * The length is returned.
730 */
731 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100732copy_option_part(
733 char_u **option,
734 char_u *buf,
735 int maxlen,
736 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737{
738 int len = 0;
739 char_u *p = *option;
740
Bram Moolenaar85a20022019-12-21 18:25:54 +0100741 // skip '.' at start of option part, for 'suffixes'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 if (*p == '.')
743 buf[len++] = *p++;
744 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
745 {
746 /*
747 * Skip backslash before a separator character and space.
748 */
749 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
750 ++p;
751 if (len < maxlen - 1)
752 buf[len++] = *p;
753 ++p;
754 }
755 buf[len] = NUL;
756
Bram Moolenaar85a20022019-12-21 18:25:54 +0100757 if (*p != NUL && *p != ',') // skip non-standard separator
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 ++p;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100759 p = skip_to_option_part(p); // p points to next file name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760
761 *option = p;
762 return len;
763}
764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765#ifndef HAVE_MEMSET
766 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100767vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768{
769 char *p = ptr;
770
771 while (size-- > 0)
772 *p++ = c;
773 return ptr;
774}
775#endif
776
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777/*
778 * Vim has its own isspace() function, because on some machines isspace()
779 * can't handle characters above 128.
780 */
781 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100782vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783{
784 return ((x >= 9 && x <= 13) || x == ' ');
785}
786
787/************************************************************************
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 * functions that use lookup tables for various things, generally to do with
789 * special key codes.
790 */
791
792/*
793 * Some useful tables.
794 */
795
796static struct modmasktable
797{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100798 short mod_mask; // Bit-mask for particular key modifier
799 short mod_flag; // Bit(s) for particular key modifier
800 char_u name; // Single letter name of modifier
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801} mod_mask_table[] =
802{
803 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000804 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
806 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
807 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
808 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
809 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
Bram Moolenaard0573012017-10-28 21:11:06 +0200810#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
812#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100813 // 'A' must be the last one
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
815 {0, 0, NUL}
Bram Moolenaar85a20022019-12-21 18:25:54 +0100816 // NOTE: when adding an entry, update MAX_KEY_NAME_LEN!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817};
818
819/*
820 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +0000821 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 */
823#define MOD_KEYS_ENTRY_SIZE 5
824
825static char_u modifier_keys_table[] =
826{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100827// mod mask with modifier without modifier
828 MOD_MASK_SHIFT, '&', '9', '@', '1', // begin
829 MOD_MASK_SHIFT, '&', '0', '@', '2', // cancel
830 MOD_MASK_SHIFT, '*', '1', '@', '4', // command
831 MOD_MASK_SHIFT, '*', '2', '@', '5', // copy
832 MOD_MASK_SHIFT, '*', '3', '@', '6', // create
833 MOD_MASK_SHIFT, '*', '4', 'k', 'D', // delete char
834 MOD_MASK_SHIFT, '*', '5', 'k', 'L', // delete line
835 MOD_MASK_SHIFT, '*', '7', '@', '7', // end
836 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', // end
837 MOD_MASK_SHIFT, '*', '9', '@', '9', // exit
838 MOD_MASK_SHIFT, '*', '0', '@', '0', // find
839 MOD_MASK_SHIFT, '#', '1', '%', '1', // help
840 MOD_MASK_SHIFT, '#', '2', 'k', 'h', // home
841 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', // home
842 MOD_MASK_SHIFT, '#', '3', 'k', 'I', // insert
843 MOD_MASK_SHIFT, '#', '4', 'k', 'l', // left arrow
844 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', // left arrow
845 MOD_MASK_SHIFT, '%', 'a', '%', '3', // message
846 MOD_MASK_SHIFT, '%', 'b', '%', '4', // move
847 MOD_MASK_SHIFT, '%', 'c', '%', '5', // next
848 MOD_MASK_SHIFT, '%', 'd', '%', '7', // options
849 MOD_MASK_SHIFT, '%', 'e', '%', '8', // previous
850 MOD_MASK_SHIFT, '%', 'f', '%', '9', // print
851 MOD_MASK_SHIFT, '%', 'g', '%', '0', // redo
852 MOD_MASK_SHIFT, '%', 'h', '&', '3', // replace
853 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', // right arr.
854 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', // right arr.
855 MOD_MASK_SHIFT, '%', 'j', '&', '5', // resume
856 MOD_MASK_SHIFT, '!', '1', '&', '6', // save
857 MOD_MASK_SHIFT, '!', '2', '&', '7', // suspend
858 MOD_MASK_SHIFT, '!', '3', '&', '8', // undo
859 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', // up arrow
860 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', // down arrow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861
Bram Moolenaar85a20022019-12-21 18:25:54 +0100862 // vt100 F1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
864 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
865 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
866 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
867
Bram Moolenaar85a20022019-12-21 18:25:54 +0100868 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', // F1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
870 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
871 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
872 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
873 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
874 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
875 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
876 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
Bram Moolenaar85a20022019-12-21 18:25:54 +0100877 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', // F10
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
879 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
880 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
881 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
882 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
883 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
884 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
885 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
886 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
887 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
888 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
889
890 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
891 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
892 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
893 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
894 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
895 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
896 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
897 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
898 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
899 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
900
901 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
902 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
903 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
904 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
905 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
906 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
907 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
908
Bram Moolenaar85a20022019-12-21 18:25:54 +0100909 // TAB pseudo code
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
911
912 NUL
913};
914
915static struct key_name_entry
916{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100917 int key; // Special key code or ascii value
918 char_u *name; // Name of key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919} key_names_table[] =
920{
921 {' ', (char_u *)"Space"},
922 {TAB, (char_u *)"Tab"},
923 {K_TAB, (char_u *)"Tab"},
924 {NL, (char_u *)"NL"},
Bram Moolenaar85a20022019-12-21 18:25:54 +0100925 {NL, (char_u *)"NewLine"}, // Alternative name
926 {NL, (char_u *)"LineFeed"}, // Alternative name
927 {NL, (char_u *)"LF"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {CAR, (char_u *)"CR"},
Bram Moolenaar85a20022019-12-21 18:25:54 +0100929 {CAR, (char_u *)"Return"}, // Alternative name
930 {CAR, (char_u *)"Enter"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 {K_BS, (char_u *)"BS"},
Bram Moolenaar85a20022019-12-21 18:25:54 +0100932 {K_BS, (char_u *)"BackSpace"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 {ESC, (char_u *)"Esc"},
934 {CSI, (char_u *)"CSI"},
935 {K_CSI, (char_u *)"xCSI"},
936 {'|', (char_u *)"Bar"},
937 {'\\', (char_u *)"Bslash"},
938 {K_DEL, (char_u *)"Del"},
Bram Moolenaar85a20022019-12-21 18:25:54 +0100939 {K_DEL, (char_u *)"Delete"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 {K_KDEL, (char_u *)"kDel"},
941 {K_UP, (char_u *)"Up"},
942 {K_DOWN, (char_u *)"Down"},
943 {K_LEFT, (char_u *)"Left"},
944 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000945 {K_XUP, (char_u *)"xUp"},
946 {K_XDOWN, (char_u *)"xDown"},
947 {K_XLEFT, (char_u *)"xLeft"},
948 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100949 {K_PS, (char_u *)"PasteStart"},
950 {K_PE, (char_u *)"PasteEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
952 {K_F1, (char_u *)"F1"},
953 {K_F2, (char_u *)"F2"},
954 {K_F3, (char_u *)"F3"},
955 {K_F4, (char_u *)"F4"},
956 {K_F5, (char_u *)"F5"},
957 {K_F6, (char_u *)"F6"},
958 {K_F7, (char_u *)"F7"},
959 {K_F8, (char_u *)"F8"},
960 {K_F9, (char_u *)"F9"},
961 {K_F10, (char_u *)"F10"},
962
963 {K_F11, (char_u *)"F11"},
964 {K_F12, (char_u *)"F12"},
965 {K_F13, (char_u *)"F13"},
966 {K_F14, (char_u *)"F14"},
967 {K_F15, (char_u *)"F15"},
968 {K_F16, (char_u *)"F16"},
969 {K_F17, (char_u *)"F17"},
970 {K_F18, (char_u *)"F18"},
971 {K_F19, (char_u *)"F19"},
972 {K_F20, (char_u *)"F20"},
973
974 {K_F21, (char_u *)"F21"},
975 {K_F22, (char_u *)"F22"},
976 {K_F23, (char_u *)"F23"},
977 {K_F24, (char_u *)"F24"},
978 {K_F25, (char_u *)"F25"},
979 {K_F26, (char_u *)"F26"},
980 {K_F27, (char_u *)"F27"},
981 {K_F28, (char_u *)"F28"},
982 {K_F29, (char_u *)"F29"},
983 {K_F30, (char_u *)"F30"},
984
985 {K_F31, (char_u *)"F31"},
986 {K_F32, (char_u *)"F32"},
987 {K_F33, (char_u *)"F33"},
988 {K_F34, (char_u *)"F34"},
989 {K_F35, (char_u *)"F35"},
990 {K_F36, (char_u *)"F36"},
991 {K_F37, (char_u *)"F37"},
992
993 {K_XF1, (char_u *)"xF1"},
994 {K_XF2, (char_u *)"xF2"},
995 {K_XF3, (char_u *)"xF3"},
996 {K_XF4, (char_u *)"xF4"},
997
998 {K_HELP, (char_u *)"Help"},
999 {K_UNDO, (char_u *)"Undo"},
1000 {K_INS, (char_u *)"Insert"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001001 {K_INS, (char_u *)"Ins"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 {K_KINS, (char_u *)"kInsert"},
1003 {K_HOME, (char_u *)"Home"},
1004 {K_KHOME, (char_u *)"kHome"},
1005 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001006 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 {K_END, (char_u *)"End"},
1008 {K_KEND, (char_u *)"kEnd"},
1009 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001010 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 {K_PAGEUP, (char_u *)"PageUp"},
1012 {K_PAGEDOWN, (char_u *)"PageDown"},
1013 {K_KPAGEUP, (char_u *)"kPageUp"},
1014 {K_KPAGEDOWN, (char_u *)"kPageDown"},
1015
1016 {K_KPLUS, (char_u *)"kPlus"},
1017 {K_KMINUS, (char_u *)"kMinus"},
1018 {K_KDIVIDE, (char_u *)"kDivide"},
1019 {K_KMULTIPLY, (char_u *)"kMultiply"},
1020 {K_KENTER, (char_u *)"kEnter"},
1021 {K_KPOINT, (char_u *)"kPoint"},
1022
1023 {K_K0, (char_u *)"k0"},
1024 {K_K1, (char_u *)"k1"},
1025 {K_K2, (char_u *)"k2"},
1026 {K_K3, (char_u *)"k3"},
1027 {K_K4, (char_u *)"k4"},
1028 {K_K5, (char_u *)"k5"},
1029 {K_K6, (char_u *)"k6"},
1030 {K_K7, (char_u *)"k7"},
1031 {K_K8, (char_u *)"k8"},
1032 {K_K9, (char_u *)"k9"},
1033
1034 {'<', (char_u *)"lt"},
1035
1036 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001037#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001039#endif
1040#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001042#endif
1043#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001045#endif
1046#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001048#endif
1049#ifdef FEAT_MOUSE_URXVT
1050 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
1051#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001052 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
Bram Moolenaar21a83bd2021-02-23 19:19:58 +01001053 {K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelease"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
1055 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
1056 {K_LEFTDRAG, (char_u *)"LeftDrag"},
1057 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
1058 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001059 {K_MOUSEMOVE, (char_u *)"MouseMove"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
1061 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
1062 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
1063 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
1064 {K_RIGHTDRAG, (char_u *)"RightDrag"},
1065 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001066 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
1067 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
1068 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
1069 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001070 {K_MOUSEDOWN, (char_u *)"MouseDown"}, // OBSOLETE: Use
1071 {K_MOUSEUP, (char_u *)"MouseUp"}, // ScrollWheelXXX instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {K_X1MOUSE, (char_u *)"X1Mouse"},
1073 {K_X1DRAG, (char_u *)"X1Drag"},
1074 {K_X1RELEASE, (char_u *)"X1Release"},
1075 {K_X2MOUSE, (char_u *)"X2Mouse"},
1076 {K_X2DRAG, (char_u *)"X2Drag"},
1077 {K_X2RELEASE, (char_u *)"X2Release"},
1078 {K_DROP, (char_u *)"Drop"},
1079 {K_ZERO, (char_u *)"Nul"},
1080#ifdef FEAT_EVAL
1081 {K_SNR, (char_u *)"SNR"},
1082#endif
1083 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02001084 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar2f106582019-05-08 21:59:25 +02001085 {K_IGNORE, (char_u *)"Ignore"},
Bram Moolenaar957cf672020-11-12 14:21:06 +01001086 {K_COMMAND, (char_u *)"Cmd"},
Bram Moolenaare32c3c42022-01-15 18:26:04 +00001087 {K_SCRIPT_COMMAND, (char_u *)"ScriptCmd"},
Bram Moolenaarccb47a22021-01-21 13:36:43 +01001088 {K_FOCUSGAINED, (char_u *)"FocusGained"},
1089 {K_FOCUSLOST, (char_u *)"FocusLost"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 {0, NULL}
Bram Moolenaar85a20022019-12-21 18:25:54 +01001091 // NOTE: When adding a long name update MAX_KEY_NAME_LEN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092};
1093
K.Takataeeec2542021-06-02 13:28:16 +02001094#define KEY_NAMES_TABLE_LEN ARRAY_LENGTH(key_names_table)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096/*
1097 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
1098 * modifier name ('S' for Shift, 'C' for Ctrl etc).
1099 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001100 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001101name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
1103 int i;
1104
1105 c = TOUPPER_ASC(c);
1106 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
1107 if (c == mod_mask_table[i].name)
1108 return mod_mask_table[i].mod_flag;
1109 return 0;
1110}
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112/*
1113 * Check if if there is a special key code for "key" that includes the
1114 * modifiers specified.
1115 */
1116 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001117simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
1119 int i;
1120 int key0;
1121 int key1;
1122
1123 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
1124 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001125 // TAB is a special case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
1127 {
1128 *modifiers &= ~MOD_MASK_SHIFT;
1129 return K_S_TAB;
1130 }
1131 key0 = KEY2TERMCAP0(key);
1132 key1 = KEY2TERMCAP1(key);
1133 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
1134 if (key0 == modifier_keys_table[i + 3]
1135 && key1 == modifier_keys_table[i + 4]
1136 && (*modifiers & modifier_keys_table[i]))
1137 {
1138 *modifiers &= ~modifier_keys_table[i];
1139 return TERMCAP2KEY(modifier_keys_table[i + 1],
1140 modifier_keys_table[i + 2]);
1141 }
1142 }
1143 return key;
1144}
1145
1146/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001147 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001148 */
1149 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001150handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001151{
1152 switch (key)
1153 {
1154 case K_XUP: return K_UP;
1155 case K_XDOWN: return K_DOWN;
1156 case K_XLEFT: return K_LEFT;
1157 case K_XRIGHT: return K_RIGHT;
1158 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001159 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001160 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001161 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001162 case K_XF1: return K_F1;
1163 case K_XF2: return K_F2;
1164 case K_XF3: return K_F3;
1165 case K_XF4: return K_F4;
1166 case K_S_XF1: return K_S_F1;
1167 case K_S_XF2: return K_S_F2;
1168 case K_S_XF3: return K_S_F3;
1169 case K_S_XF4: return K_S_F4;
1170 }
1171 return key;
1172}
1173
1174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 * Return a string which contains the name of the given key when the given
1176 * modifiers are down.
1177 */
1178 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001179get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180{
1181 static char_u string[MAX_KEY_NAME_LEN + 1];
1182
1183 int i, idx;
1184 int table_idx;
1185 char_u *s;
1186
1187 string[0] = '<';
1188 idx = 1;
1189
Bram Moolenaar85a20022019-12-21 18:25:54 +01001190 // Key that stands for a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
1192 c = KEY2TERMCAP1(c);
1193
1194 /*
1195 * Translate shifted special keys into unshifted keys and set modifier.
1196 * Same for CTRL and ALT modifiers.
1197 */
1198 if (IS_SPECIAL(c))
1199 {
1200 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
1201 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
1202 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
1203 {
1204 modifiers |= modifier_keys_table[i];
1205 c = TERMCAP2KEY(modifier_keys_table[i + 3],
1206 modifier_keys_table[i + 4]);
1207 break;
1208 }
1209 }
1210
Bram Moolenaar85a20022019-12-21 18:25:54 +01001211 // try to find the key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 table_idx = find_special_key_in_table(c);
1213
1214 /*
1215 * When not a known special key, and not a printable character, try to
1216 * extract modifiers.
1217 */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001218 if (c > 0 && (*mb_char2len)(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {
1220 if (table_idx < 0
1221 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
1222 && (c & 0x80))
1223 {
1224 c &= 0x7f;
1225 modifiers |= MOD_MASK_ALT;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001226 // try again, to find the un-alted key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 table_idx = find_special_key_in_table(c);
1228 }
1229 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
1230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 c += '@';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 modifiers |= MOD_MASK_CTRL;
1233 }
1234 }
1235
Bram Moolenaar85a20022019-12-21 18:25:54 +01001236 // translate the modifier into a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 for (i = 0; mod_mask_table[i].name != 'A'; i++)
1238 if ((modifiers & mod_mask_table[i].mod_mask)
1239 == mod_mask_table[i].mod_flag)
1240 {
1241 string[idx++] = mod_mask_table[i].name;
1242 string[idx++] = (char_u)'-';
1243 }
1244
Bram Moolenaar85a20022019-12-21 18:25:54 +01001245 if (table_idx < 0) // unknown special key, may output t_xx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
1247 if (IS_SPECIAL(c))
1248 {
1249 string[idx++] = 't';
1250 string[idx++] = '_';
1251 string[idx++] = KEY2TERMCAP0(c);
1252 string[idx++] = KEY2TERMCAP1(c);
1253 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001254 // Not a special key, only modifiers, output directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 else
1256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 if (has_mbyte && (*mb_char2len)(c) > 1)
1258 idx += (*mb_char2bytes)(c, string + idx);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001259 else if (vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 string[idx++] = c;
1261 else
1262 {
1263 s = transchar(c);
1264 while (*s)
1265 string[idx++] = *s++;
1266 }
1267 }
1268 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001269 else // use name of special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 {
Bram Moolenaar423977d2017-01-22 15:05:12 +01001271 size_t len = STRLEN(key_names_table[table_idx].name);
1272
1273 if (len + idx + 2 <= MAX_KEY_NAME_LEN)
1274 {
1275 STRCPY(string + idx, key_names_table[table_idx].name);
1276 idx += (int)len;
1277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 string[idx++] = '>';
1280 string[idx] = NUL;
1281 return string;
1282}
1283
1284/*
1285 * Try translating a <> name at (*srcp)[] to dst[].
1286 * Return the number of characters added to dst[], zero for no match.
1287 * If there is a match, srcp is advanced to after the <> name.
1288 * dst[] must be big enough to hold the result (up to six characters)!
1289 */
1290 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001291trans_special(
1292 char_u **srcp,
1293 char_u *dst,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001294 int flags, // FSK_ values
zeertzjqdb088872022-05-02 22:53:45 +01001295 int escape_ks, // escape K_SPECIAL bytes in the character
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001296 int *did_simplify) // FSK_SIMPLIFY and found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297{
1298 int modifiers = 0;
1299 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001301 key = find_special_key(srcp, &modifiers, flags, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 if (key == 0)
1303 return 0;
1304
zeertzjqdb088872022-05-02 22:53:45 +01001305 return special_to_buf(key, modifiers, escape_ks, dst);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001306}
1307
1308/*
1309 * Put the character sequence for "key" with "modifiers" into "dst" and return
1310 * the resulting length.
zeertzjqdb088872022-05-02 22:53:45 +01001311 * When "escape_ks" is TRUE escape K_SPECIAL bytes in the character.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001312 * The sequence is not NUL terminated.
1313 * This is how characters in a string are encoded.
1314 */
1315 int
zeertzjqdb088872022-05-02 22:53:45 +01001316special_to_buf(int key, int modifiers, int escape_ks, char_u *dst)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001317{
1318 int dlen = 0;
1319
Bram Moolenaar85a20022019-12-21 18:25:54 +01001320 // Put the appropriate modifier in a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 if (modifiers != 0)
1322 {
1323 dst[dlen++] = K_SPECIAL;
1324 dst[dlen++] = KS_MODIFIER;
1325 dst[dlen++] = modifiers;
1326 }
1327
1328 if (IS_SPECIAL(key))
1329 {
1330 dst[dlen++] = K_SPECIAL;
1331 dst[dlen++] = KEY2TERMCAP0(key);
1332 dst[dlen++] = KEY2TERMCAP1(key);
1333 }
zeertzjqdb088872022-05-02 22:53:45 +01001334 else if (escape_ks)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
zeertzjqdb088872022-05-02 22:53:45 +01001336 else if (has_mbyte)
1337 dlen += (*mb_char2bytes)(key, dst + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 else
1339 dst[dlen++] = key;
1340
1341 return dlen;
1342}
1343
1344/*
1345 * Try translating a <> name at (*srcp)[], return the key and modifiers.
1346 * srcp is advanced to after the <> name.
1347 * returns 0 if there is no match.
1348 */
1349 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001350find_special_key(
1351 char_u **srcp,
1352 int *modp,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001353 int flags, // FSK_ values
Bram Moolenaar459fd782019-10-13 16:43:39 +02001354 int *did_simplify) // found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355{
1356 char_u *last_dash;
1357 char_u *end_of_name;
1358 char_u *src;
1359 char_u *bp;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001360 int in_string = flags & FSK_IN_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 int modifiers;
1362 int bit;
1363 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001364 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001365 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367 src = *srcp;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001368 if (src[0] != '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 return 0;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001370 if (src[1] == '*') // <*xxx>: do not simplify
1371 ++src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
Bram Moolenaar85a20022019-12-21 18:25:54 +01001373 // Find end of modifier list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 last_dash = src;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02001375 for (bp = src + 1; *bp == '-' || vim_isNormalIDc(*bp); bp++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 {
1377 if (*bp == '-')
1378 {
1379 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001380 if (bp[1] != NUL)
1381 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001382 if (has_mbyte)
1383 l = mb_ptr2len(bp + 1);
1384 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001385 l = 1;
Bram Moolenaarc8fd33d2019-08-16 20:33:05 +02001386 // Anything accepted, like <C-?>.
1387 // <C-"> or <M-"> are not special in strings as " is
1388 // the string delimiter. With a backslash it works: <M-\">
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001389 if (!(in_string && bp[1] == '"') && bp[l + 1] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02001390 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001391 else if (in_string && bp[1] == '\\' && bp[2] == '"'
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001392 && bp[3] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001393 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001397 bp += 3; // skip t_xx, xx may be '-' or '>'
Bram Moolenaar792826c2011-08-19 22:29:02 +02001398 else if (STRNICMP(bp, "char-", 5) == 0)
1399 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001400 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0, TRUE);
1401 if (l == 0)
1402 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001403 emsg(_(e_invalid_argument));
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001404 return 0;
1405 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02001406 bp += l + 5;
1407 break;
1408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 }
1410
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001411 if (*bp == '>') // found matching '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 {
1413 end_of_name = bp + 1;
1414
Bram Moolenaar85a20022019-12-21 18:25:54 +01001415 // Which modifiers are given?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 modifiers = 0x0;
1417 for (bp = src + 1; bp < last_dash; bp++)
1418 {
1419 if (*bp != '-')
1420 {
1421 bit = name_to_mod_mask(*bp);
1422 if (bit == 0x0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001423 break; // Illegal modifier name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 modifiers |= bit;
1425 }
1426 }
1427
1428 /*
1429 * Legal modifier name.
1430 */
1431 if (bp >= last_dash)
1432 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001433 if (STRNICMP(last_dash + 1, "char-", 5) == 0
1434 && VIM_ISDIGIT(last_dash[6]))
1435 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001436 // <Char-123> or <Char-033> or <Char-0x33>
Bram Moolenaar459fd782019-10-13 16:43:39 +02001437 vim_str2nr(last_dash + 6, NULL, &l, STR2NR_ALL, NULL,
1438 &n, 0, TRUE);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001439 if (l == 0)
1440 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001441 emsg(_(e_invalid_argument));
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001442 return 0;
1443 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02001444 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001447 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001448 int off = 1;
1449
Bram Moolenaar85a20022019-12-21 18:25:54 +01001450 // Modifier with single letter, or special key name.
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001451 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
1452 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02001453 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001454 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02001455 else
Bram Moolenaar792826c2011-08-19 22:29:02 +02001456 l = 1;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001457 if (modifiers != 0 && last_dash[l + off] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001458 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02001459 else
1460 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001461 key = get_special_key_code(last_dash + off);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001462 if (!(flags & FSK_KEEP_X_KEY))
Bram Moolenaar792826c2011-08-19 22:29:02 +02001463 key = handle_x_keys(key);
1464 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
1467 /*
1468 * get_special_key_code() may return NUL for invalid
1469 * special key name.
1470 */
1471 if (key != NUL)
1472 {
1473 /*
1474 * Only use a modifier when there is no special key code that
1475 * includes the modifier.
1476 */
1477 key = simplify_key(key, &modifiers);
1478
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001479 if (!(flags & FSK_KEYCODE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001481 // don't want keycode, use single byte code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (key == K_BS)
1483 key = BS;
1484 else if (key == K_DEL || key == K_KDEL)
1485 key = DEL;
1486 }
1487
Bram Moolenaar459fd782019-10-13 16:43:39 +02001488 // Normal Key with modifier: Try to make a single byte code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (!IS_SPECIAL(key))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001490 key = extract_modifiers(key, &modifiers,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001491 flags & FSK_SIMPLIFY, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
1493 *modp = modifiers;
1494 *srcp = end_of_name;
1495 return key;
1496 }
1497 }
1498 }
1499 return 0;
1500}
1501
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001502
1503/*
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02001504 * Some keys are used with Ctrl without Shift and are still expected to be
1505 * mapped as if Shift was pressed:
1506 * CTRL-2 is CTRL-@
1507 * CTRL-6 is CTRL-^
1508 * CTRL-- is CTRL-_
1509 * Also, <C-H> and <C-h> mean the same thing, always use "H".
1510 * Returns the possibly adjusted key.
1511 */
1512 int
1513may_adjust_key_for_ctrl(int modifiers, int key)
1514{
1515 if (modifiers & MOD_MASK_CTRL)
1516 {
1517 if (ASCII_ISALPHA(key))
1518 return TOUPPER_ASC(key);
1519 if (key == '2')
1520 return '@';
1521 if (key == '6')
1522 return '^';
1523 if (key == '-')
1524 return '_';
1525 }
1526 return key;
1527}
1528
1529/*
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001530 * Some keys already have Shift included, pass them as normal keys.
Bram Moolenaar9a033d72020-10-07 17:29:48 +02001531 * When Ctrl is also used <C-H> and <C-S-H> are different, but <C-S-{> should
1532 * be <C-{>. Same for <C-S-}> and <C-S-|>.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001533 * Also for <A-S-a> and <M-S-a>.
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02001534 * This includes all printable ASCII characters except numbers and a-z.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001535 */
1536 int
1537may_remove_shift_modifier(int modifiers, int key)
1538{
1539 if ((modifiers == MOD_MASK_SHIFT
1540 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
1541 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02001542 && ((key >= '!' && key <= '/')
1543 || (key >= ':' && key <= 'Z')
1544 || (key >= '[' && key <= '`')
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001545 || (key >= '{' && key <= '~')))
1546 return modifiers & ~MOD_MASK_SHIFT;
Bram Moolenaar9a033d72020-10-07 17:29:48 +02001547
1548 if (modifiers == (MOD_MASK_SHIFT | MOD_MASK_CTRL)
1549 && (key == '{' || key == '}' || key == '|'))
1550 return modifiers & ~MOD_MASK_SHIFT;
1551
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001552 return modifiers;
1553}
1554
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555/*
1556 * Try to include modifiers in the key.
1557 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
Bram Moolenaar459fd782019-10-13 16:43:39 +02001558 * When "simplify" is FALSE don't do Ctrl and Alt.
1559 * When "simplify" is TRUE and Ctrl or Alt is removed from modifiers set
1560 * "did_simplify" when it's not NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 */
1562 int
Bram Moolenaar459fd782019-10-13 16:43:39 +02001563extract_modifiers(int key, int *modp, int simplify, int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565 int modifiers = *modp;
1566
Bram Moolenaard0573012017-10-28 21:11:06 +02001567#ifdef MACOS_X
Bram Moolenaar459fd782019-10-13 16:43:39 +02001568 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 if (!(modifiers & MOD_MASK_CMD))
1570#endif
1571 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
1572 {
1573 key = TOUPPER_ASC(key);
Bram Moolenaar20298ce2020-06-19 21:46:52 +02001574 // With <C-S-a> we keep the shift modifier.
1575 // With <S-a>, <A-S-a> and <S-A> we don't keep the shift modifier.
1576 if (simplify || modifiers == MOD_MASK_SHIFT
1577 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
1578 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001579 modifiers &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02001581
1582 // <C-H> and <C-h> mean the same thing, always use "H"
1583 if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key))
1584 key = TOUPPER_ASC(key);
1585
1586 if (simplify && (modifiers & MOD_MASK_CTRL)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001587 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 {
1589 key = Ctrl_chr(key);
1590 modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001591 // <C-@> is <Nul>
zeertzjq17c95d92022-04-26 12:51:07 +01001592 if (key == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 key = K_ZERO;
Bram Moolenaar459fd782019-10-13 16:43:39 +02001594 if (did_simplify != NULL)
1595 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02001597
Bram Moolenaard0573012017-10-28 21:11:06 +02001598#ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01001599 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 if (!(modifiers & MOD_MASK_CMD))
1601#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +02001602 if (simplify && (modifiers & MOD_MASK_ALT) && key < 0x80
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001603 && !enc_dbcs) // avoid creating a lead byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
1605 key |= 0x80;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001606 modifiers &= ~MOD_MASK_ALT; // remove the META modifier
Bram Moolenaar459fd782019-10-13 16:43:39 +02001607 if (did_simplify != NULL)
1608 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610
1611 *modp = modifiers;
1612 return key;
1613}
1614
1615/*
1616 * Try to find key "c" in the special key table.
1617 * Return the index when found, -1 when not found.
1618 */
1619 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001620find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
1622 int i;
1623
1624 for (i = 0; key_names_table[i].name != NULL; i++)
1625 if (c == key_names_table[i].key)
1626 break;
1627 if (key_names_table[i].name == NULL)
1628 i = -1;
1629 return i;
1630}
1631
1632/*
1633 * Find the special key with the given name (the given string does not have to
1634 * end with NUL, the name is assumed to end before the first non-idchar).
1635 * If the name starts with "t_" the next two characters are interpreted as a
1636 * termcap name.
1637 * Return the key code, or 0 if not found.
1638 */
1639 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001640get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641{
1642 char_u *table_name;
1643 char_u string[3];
1644 int i, j;
1645
1646 /*
1647 * If it's <t_xx> we get the code for xx from the termcap
1648 */
1649 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
1650 {
1651 string[0] = name[2];
1652 string[1] = name[3];
1653 string[2] = NUL;
1654 if (add_termcap_entry(string, FALSE) == OK)
1655 return TERMCAP2KEY(name[2], name[3]);
1656 }
1657 else
1658 for (i = 0; key_names_table[i].name != NULL; i++)
1659 {
1660 table_name = key_names_table[i].name;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02001661 for (j = 0; vim_isNormalIDc(name[j]) && table_name[j] != NUL; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
1663 break;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02001664 if (!vim_isNormalIDc(name[j]) && table_name[j] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 return key_names_table[i].key;
1666 }
1667 return 0;
1668}
1669
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001671get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00001673 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 return NULL;
1675 return key_names_table[i].name;
1676}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678/*
1679 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
1680 */
1681 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001682get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683{
1684 int c = *buf->b_p_ff;
1685
1686 if (buf->b_p_bin || c == 'u')
1687 return EOL_UNIX;
1688 if (c == 'm')
1689 return EOL_MAC;
1690 return EOL_DOS;
1691}
1692
1693/*
1694 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
1695 * argument.
1696 */
1697 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001698get_fileformat_force(
1699 buf_T *buf,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001700 exarg_T *eap) // can be NULL!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
1702 int c;
1703
1704 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001705 c = eap->force_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 else
1707 {
1708 if ((eap != NULL && eap->force_bin != 0)
1709 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
1710 return EOL_UNIX;
1711 c = *buf->b_p_ff;
1712 }
1713 if (c == 'u')
1714 return EOL_UNIX;
1715 if (c == 'm')
1716 return EOL_MAC;
1717 return EOL_DOS;
1718}
1719
1720/*
1721 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
1722 * Sets both 'textmode' and 'fileformat'.
1723 * Note: Does _not_ set global value of 'textmode'!
1724 */
1725 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001726set_fileformat(
1727 int t,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001728 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
1730 char *p = NULL;
1731
1732 switch (t)
1733 {
1734 case EOL_DOS:
1735 p = FF_DOS;
1736 curbuf->b_p_tx = TRUE;
1737 break;
1738 case EOL_UNIX:
1739 p = FF_UNIX;
1740 curbuf->b_p_tx = FALSE;
1741 break;
1742 case EOL_MAC:
1743 p = FF_MAC;
1744 curbuf->b_p_tx = FALSE;
1745 break;
1746 }
1747 if (p != NULL)
1748 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001749 OPT_FREE | opt_flags, 0);
1750
Bram Moolenaar85a20022019-12-21 18:25:54 +01001751 // This may cause the buffer to become (un)modified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001753 redraw_tabline = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001754 need_maketitle = TRUE; // set window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755}
1756
1757/*
1758 * Return the default fileformat from 'fileformats'.
1759 */
1760 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001761default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
1763 switch (*p_ffs)
1764 {
1765 case 'm': return EOL_MAC;
1766 case 'd': return EOL_DOS;
1767 }
1768 return EOL_UNIX;
1769}
1770
1771/*
1772 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
1773 */
1774 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001775call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 char_u *ncmd;
1778 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001779#ifdef FEAT_PROFILE
1780 proftime_T wait_time;
1781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 if (p_verbose > 3)
1784 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001785 verbose_enter();
Bram Moolenaar06f08532020-05-12 23:45:16 +02001786 smsg(_("Calling shell to execute: \"%s\""), cmd == NULL ? p_sh : cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 out_char('\n');
1788 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001789 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 }
1791
Bram Moolenaar05159a02005-02-26 23:04:13 +00001792#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00001793 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001794 prof_child_enter(&wait_time);
1795#endif
1796
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (*p_sh == NUL)
1798 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001799 emsg(_(e_shell_option_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 retval = -1;
1801 }
1802 else
1803 {
1804#ifdef FEAT_GUI_MSWIN
Bram Moolenaar85a20022019-12-21 18:25:54 +01001805 // Don't hide the pointer while executing a shell command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 gui_mch_mousehide(FALSE);
1807#endif
1808#ifdef FEAT_GUI
1809 ++hold_gui_events;
1810#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001811 // The external command may update a tags file, clear cached tags.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 tag_freematch();
1813
Bram Moolenaar01257a72019-06-10 14:46:04 +02001814 if (cmd == NULL || *p_sxq == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = mch_call_shell(cmd, opt);
1816 else
1817 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001818 char_u *ecmd = cmd;
1819
Bram Moolenaar1a613392019-09-28 15:51:37 +02001820 if (*p_sxe != NUL && *p_sxq == '(')
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001821 {
1822 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
1823 if (ecmd == NULL)
1824 ecmd = cmd;
1825 }
Bram Moolenaar964b3742019-05-24 18:54:09 +02001826 ncmd = alloc(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (ncmd != NULL)
1828 {
1829 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001830 STRCAT(ncmd, ecmd);
Bram Moolenaar1a613392019-09-28 15:51:37 +02001831 // When 'shellxquote' is ( append ).
1832 // When 'shellxquote' is "( append )".
1833 STRCAT(ncmd, *p_sxq == '(' ? (char_u *)")"
1834 : *p_sxq == '"' && *(p_sxq+1) == '(' ? (char_u *)")\""
1835 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 retval = mch_call_shell(ncmd, opt);
1837 vim_free(ncmd);
1838 }
1839 else
1840 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001841 if (ecmd != cmd)
1842 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844#ifdef FEAT_GUI
1845 --hold_gui_events;
1846#endif
1847 /*
1848 * Check the window size, in case it changed while executing the
1849 * external command.
1850 */
1851 shell_resized_check();
1852 }
1853
1854#ifdef FEAT_EVAL
1855 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001856# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00001857 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001858 prof_child_exit(&wait_time);
1859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860#endif
1861
1862 return retval;
1863}
1864
1865/*
Bram Moolenaar24959102022-05-07 20:01:16 +01001866 * MODE_VISUAL, MODE_SELECT and MODE_OP_PENDING State are never set, they are
1867 * equal to MODE_NORMAL State with a condition. This function returns the real
1868 * State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 */
1870 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001871get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872{
Bram Moolenaar24959102022-05-07 20:01:16 +01001873 if (State & MODE_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00001876 {
1877 if (VIsual_select)
Bram Moolenaar24959102022-05-07 20:01:16 +01001878 return MODE_SELECT;
1879 return MODE_VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00001880 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001881 else if (finish_op)
Bram Moolenaar24959102022-05-07 20:01:16 +01001882 return MODE_OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 }
1884 return State;
1885}
1886
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001887/*
1888 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02001889 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001890 * "b" must point to the start of the file name
1891 */
1892 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001893after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001894{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02001895 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001896 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
1897}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001898
1899/*
1900 * Return TRUE if file names "f1" and "f2" are in the same directory.
1901 * "f1" may be a short name, "f2" must be a full path.
1902 */
1903 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001904same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001905{
1906 char_u ffname[MAXPATHL];
1907 char_u *t1;
1908 char_u *t2;
1909
Bram Moolenaar85a20022019-12-21 18:25:54 +01001910 // safety check
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001911 if (f1 == NULL || f2 == NULL)
1912 return FALSE;
1913
1914 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
1915 t1 = gettail_sep(ffname);
1916 t2 = gettail_sep(f2);
1917 return (t1 - ffname == t2 - f2
1918 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
1919}
1920
Bram Moolenaar7c365fb2018-06-29 20:28:31 +02001921#if defined(FEAT_SESSION) || defined(FEAT_AUTOCHDIR) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02001922 || defined(MSWIN) || defined(FEAT_GUI_GTK) \
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001923 || defined(FEAT_NETBEANS_INTG) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 || defined(PROTO)
1925/*
1926 * Change to a file's directory.
1927 * Caller must call shorten_fnames()!
1928 * Return OK or FAIL.
1929 */
1930 int
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001931vim_chdirfile(char_u *fname, char *trigger_autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001933 char_u old_dir[MAXPATHL];
1934 char_u new_dir[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001936 if (mch_dirname(old_dir, MAXPATHL) != OK)
1937 *old_dir = NUL;
1938
1939 vim_strncpy(new_dir, fname, MAXPATHL - 1);
1940 *gettail_sep(new_dir) = NUL;
1941
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01001942 if (pathcmp((char *)old_dir, (char *)new_dir, -1) == 0)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001943 // nothing to do
zeertzjq73257142022-02-02 13:16:37 +00001944 return OK;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001945
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001946 if (trigger_autocmd != NULL)
1947 trigger_DirChangedPre((char_u *)trigger_autocmd, new_dir);
1948
zeertzjq73257142022-02-02 13:16:37 +00001949 if (mch_chdir((char *)new_dir) != 0)
1950 return FAIL;
1951
1952 if (trigger_autocmd != NULL)
1953 apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd,
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001954 new_dir, FALSE, curbuf);
zeertzjq73257142022-02-02 13:16:37 +00001955 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956}
1957#endif
1958
1959#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
1960/*
1961 * Check if "name" ends in a slash and is not a directory.
1962 * Used for systems where stat() ignores a trailing slash on a file name.
1963 * The Vim code assumes a trailing slash is only ignored for a directory.
1964 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001965 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01001966illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967{
1968 if (name[0] == NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001969 return FALSE; // no file name is not illegal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 if (name[strlen(name) - 1] != '/')
Bram Moolenaar85a20022019-12-21 18:25:54 +01001971 return FALSE; // no trailing slash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (mch_isdir((char_u *)name))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001973 return FALSE; // trailing slash for a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 return TRUE;
1975}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001976
1977/*
1978 * Special implementation of mch_stat() for Solaris.
1979 */
1980 int
1981vim_stat(const char *name, stat_T *stp)
1982{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001983 // On Solaris stat() accepts "file/" as if it was "file". Return -1 if
1984 // the name ends in "/" and it's not a directory.
Bram Moolenaard8492792017-03-16 12:22:38 +01001985 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001986}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987#endif
1988
1989#if defined(CURSOR_SHAPE) || defined(PROTO)
1990
1991/*
1992 * Handling of cursor and mouse pointer shapes in various modes.
1993 */
1994
1995cursorentry_T shape_table[SHAPE_IDX_COUNT] =
1996{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001997 // The values will be filled in from the 'guicursor' and 'mouseshape'
1998 // defaults when Vim starts.
1999 // Adjust the SHAPE_IDX_ defines when making changes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
2001 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
2002 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
2003 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
2004 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
2005 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
2006 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
2007 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
2008 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
2009 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
2010 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
2011 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
2012 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
2013 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
2014 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
2015 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
2016 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
2017};
2018
2019#ifdef FEAT_MOUSESHAPE
2020/*
2021 * Table with names for mouse shapes. Keep in sync with all the tables for
2022 * mch_set_mouse_shape()!.
2023 */
2024static char * mshape_names[] =
2025{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002026 "arrow", // default, must be the first one
2027 "blank", // hidden
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 "beam",
2029 "updown",
2030 "udsizing",
2031 "leftright",
2032 "lrsizing",
2033 "busy",
2034 "no",
2035 "crosshair",
2036 "hand1",
2037 "hand2",
2038 "pencil",
2039 "question",
2040 "rightup-arrow",
2041 "up-arrow",
2042 NULL
2043};
2044#endif
2045
2046/*
2047 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
2048 * ("what" is SHAPE_MOUSE).
2049 * Returns error message for an illegal option, NULL otherwise.
2050 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002051 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002052parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053{
2054 char_u *modep;
2055 char_u *colonp;
2056 char_u *commap;
2057 char_u *slashp;
2058 char_u *p, *endp;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002059 int idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 int all_idx;
2061 int len;
2062 int i;
2063 long n;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002064 int found_ve = FALSE; // found "ve" flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 int round;
2066
2067 /*
2068 * First round: check for errors; second round: do it for real.
2069 */
2070 for (round = 1; round <= 2; ++round)
2071 {
2072 /*
2073 * Repeat for all comma separated parts.
2074 */
2075#ifdef FEAT_MOUSESHAPE
2076 if (what == SHAPE_MOUSE)
2077 modep = p_mouseshape;
2078 else
2079#endif
2080 modep = p_guicursor;
2081 while (*modep != NUL)
2082 {
2083 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01002084 commap = vim_strchr(modep, ',');
2085
2086 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002087 return e_missing_colon_2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if (colonp == modep)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002089 return e_illegal_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091 /*
2092 * Repeat for all mode's before the colon.
2093 * For the 'a' mode, we loop to handle all the modes.
2094 */
2095 all_idx = -1;
2096 while (modep < colonp || all_idx >= 0)
2097 {
2098 if (all_idx < 0)
2099 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002100 // Find the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 if (modep[1] == '-' || modep[1] == ':')
2102 len = 1;
2103 else
2104 len = 2;
2105 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
2106 all_idx = SHAPE_IDX_COUNT - 1;
2107 else
2108 {
2109 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
2110 if (STRNICMP(modep, shape_table[idx].name, len)
2111 == 0)
2112 break;
2113 if (idx == SHAPE_IDX_COUNT
2114 || (shape_table[idx].used_for & what) == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002115 return e_illegal_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
2117 found_ve = TRUE;
2118 }
2119 modep += len + 1;
2120 }
2121
2122 if (all_idx >= 0)
2123 idx = all_idx--;
2124 else if (round == 2)
2125 {
2126#ifdef FEAT_MOUSESHAPE
2127 if (what == SHAPE_MOUSE)
2128 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002129 // Set the default, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 shape_table[idx].mshape = 0;
2131 }
2132 else
2133#endif
2134 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002135 // Set the defaults, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 shape_table[idx].shape = SHAPE_BLOCK;
2137 shape_table[idx].blinkwait = 700L;
2138 shape_table[idx].blinkon = 400L;
2139 shape_table[idx].blinkoff = 250L;
2140 }
2141 }
2142
Bram Moolenaar85a20022019-12-21 18:25:54 +01002143 // Parse the part after the colon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 for (p = colonp + 1; *p && *p != ','; )
2145 {
2146#ifdef FEAT_MOUSESHAPE
2147 if (what == SHAPE_MOUSE)
2148 {
2149 for (i = 0; ; ++i)
2150 {
2151 if (mshape_names[i] == NULL)
2152 {
2153 if (!VIM_ISDIGIT(*p))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002154 return e_illegal_mouseshape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 if (round == 2)
2156 shape_table[idx].mshape =
2157 getdigits(&p) + MSHAPE_NUMBERED;
2158 else
2159 (void)getdigits(&p);
2160 break;
2161 }
2162 len = (int)STRLEN(mshape_names[i]);
2163 if (STRNICMP(p, mshape_names[i], len) == 0)
2164 {
2165 if (round == 2)
2166 shape_table[idx].mshape = i;
2167 p += len;
2168 break;
2169 }
2170 }
2171 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002172 else // if (what == SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#endif
2174 {
2175 /*
2176 * First handle the ones with a number argument.
2177 */
2178 i = *p;
2179 len = 0;
2180 if (STRNICMP(p, "ver", 3) == 0)
2181 len = 3;
2182 else if (STRNICMP(p, "hor", 3) == 0)
2183 len = 3;
2184 else if (STRNICMP(p, "blinkwait", 9) == 0)
2185 len = 9;
2186 else if (STRNICMP(p, "blinkon", 7) == 0)
2187 len = 7;
2188 else if (STRNICMP(p, "blinkoff", 8) == 0)
2189 len = 8;
2190 if (len != 0)
2191 {
2192 p += len;
2193 if (!VIM_ISDIGIT(*p))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002194 return e_digit_expected;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 n = getdigits(&p);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002196 if (len == 3) // "ver" or "hor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
2198 if (n == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002199 return e_illegal_percentage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 if (round == 2)
2201 {
2202 if (TOLOWER_ASC(i) == 'v')
2203 shape_table[idx].shape = SHAPE_VER;
2204 else
2205 shape_table[idx].shape = SHAPE_HOR;
2206 shape_table[idx].percentage = n;
2207 }
2208 }
2209 else if (round == 2)
2210 {
2211 if (len == 9)
2212 shape_table[idx].blinkwait = n;
2213 else if (len == 7)
2214 shape_table[idx].blinkon = n;
2215 else
2216 shape_table[idx].blinkoff = n;
2217 }
2218 }
2219 else if (STRNICMP(p, "block", 5) == 0)
2220 {
2221 if (round == 2)
2222 shape_table[idx].shape = SHAPE_BLOCK;
2223 p += 5;
2224 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002225 else // must be a highlight group name then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
2227 endp = vim_strchr(p, '-');
Bram Moolenaar85a20022019-12-21 18:25:54 +01002228 if (commap == NULL) // last part
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
2230 if (endp == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002231 endp = p + STRLEN(p); // find end of part
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233 else if (endp > commap || endp == NULL)
2234 endp = commap;
2235 slashp = vim_strchr(p, '/');
2236 if (slashp != NULL && slashp < endp)
2237 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002238 // "group/langmap_group"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 i = syn_check_group(p, (int)(slashp - p));
2240 p = slashp + 1;
2241 }
2242 if (round == 2)
2243 {
2244 shape_table[idx].id = syn_check_group(p,
2245 (int)(endp - p));
2246 shape_table[idx].id_lm = shape_table[idx].id;
2247 if (slashp != NULL && slashp < endp)
2248 shape_table[idx].id = i;
2249 }
2250 p = endp;
2251 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002252 } // if (what != SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
2254 if (*p == '-')
2255 ++p;
2256 }
2257 }
2258 modep = p;
2259 if (*modep == ',')
2260 ++modep;
2261 }
2262 }
2263
Bram Moolenaar85a20022019-12-21 18:25:54 +01002264 // If the 's' flag is not given, use the 'v' cursor for 's'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (!found_ve)
2266 {
2267#ifdef FEAT_MOUSESHAPE
2268 if (what == SHAPE_MOUSE)
2269 {
2270 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
2271 }
2272 else
2273#endif
2274 {
2275 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
2276 shape_table[SHAPE_IDX_VE].percentage =
2277 shape_table[SHAPE_IDX_V].percentage;
2278 shape_table[SHAPE_IDX_VE].blinkwait =
2279 shape_table[SHAPE_IDX_V].blinkwait;
2280 shape_table[SHAPE_IDX_VE].blinkon =
2281 shape_table[SHAPE_IDX_V].blinkon;
2282 shape_table[SHAPE_IDX_VE].blinkoff =
2283 shape_table[SHAPE_IDX_V].blinkoff;
2284 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
2285 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
2286 }
2287 }
2288
2289 return NULL;
2290}
2291
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002292# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2293 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294/*
2295 * Return the index into shape_table[] for the current mode.
2296 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
2297 */
2298 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002299get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300{
2301#ifdef FEAT_MOUSESHAPE
Bram Moolenaar24959102022-05-07 20:01:16 +01002302 if (mouse && (State == MODE_HITRETURN || State == MODE_ASKMORE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002305 int x, y;
2306 gui_mch_getmouse(&x, &y);
2307 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 return SHAPE_IDX_MOREL;
2309# endif
2310 return SHAPE_IDX_MORE;
2311 }
2312 if (mouse && drag_status_line)
2313 return SHAPE_IDX_SDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (mouse && drag_sep_line)
2315 return SHAPE_IDX_VDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002317 if (!mouse && State == MODE_SHOWMATCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 return SHAPE_IDX_SM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (State & VREPLACE_FLAG)
2320 return SHAPE_IDX_R;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 if (State & REPLACE_FLAG)
2322 return SHAPE_IDX_R;
Bram Moolenaar24959102022-05-07 20:01:16 +01002323 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 return SHAPE_IDX_I;
Bram Moolenaar24959102022-05-07 20:01:16 +01002325 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
2327 if (cmdline_at_end())
2328 return SHAPE_IDX_C;
2329 if (cmdline_overstrike())
2330 return SHAPE_IDX_CR;
2331 return SHAPE_IDX_CI;
2332 }
2333 if (finish_op)
2334 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 if (VIsual_active)
2336 {
2337 if (*p_sel == 'e')
2338 return SHAPE_IDX_VE;
2339 else
2340 return SHAPE_IDX_V;
2341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 return SHAPE_IDX_N;
2343}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
2346# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
2347static int old_mouse_shape = 0;
2348
2349/*
2350 * Set the mouse shape:
2351 * If "shape" is -1, use shape depending on the current mode,
2352 * depending on the current state.
2353 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
2354 * when the mouse moves off the status or command line).
2355 */
2356 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002357update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
2359 int new_mouse_shape;
2360
Bram Moolenaar85a20022019-12-21 18:25:54 +01002361 // Only works in GUI mode.
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002362 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 return;
2364
Bram Moolenaar85a20022019-12-21 18:25:54 +01002365 // Postpone the updating when more is to come. Speeds up executing of
2366 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 if (shape_idx == -1 && char_avail())
2368 {
2369 postponed_mouseshape = TRUE;
2370 return;
2371 }
2372
Bram Moolenaar85a20022019-12-21 18:25:54 +01002373 // When ignoring the mouse don't change shape on the statusline.
Bram Moolenaar14716812006-05-04 21:54:08 +00002374 if (*p_mouse == NUL
2375 && (shape_idx == SHAPE_IDX_CLINE
2376 || shape_idx == SHAPE_IDX_STATUS
2377 || shape_idx == SHAPE_IDX_VSEP))
2378 shape_idx = -2;
2379
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 if (shape_idx == -2
2381 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
2382 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
2383 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
2384 return;
2385 if (shape_idx < 0)
2386 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
2387 else
2388 new_mouse_shape = shape_table[shape_idx].mshape;
2389 if (new_mouse_shape != old_mouse_shape)
2390 {
2391 mch_set_mouse_shape(new_mouse_shape);
2392 old_mouse_shape = new_mouse_shape;
2393 }
2394 postponed_mouseshape = FALSE;
2395}
2396# endif
2397
Bram Moolenaar85a20022019-12-21 18:25:54 +01002398#endif // CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401/*
2402 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
2403 * 'cdpath' for relative directory names, otherwise just mch_chdir().
2404 */
2405 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002406vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
2408#ifndef FEAT_SEARCHPATH
2409 return mch_chdir((char *)new_dir);
2410#else
2411 char_u *dir_name;
2412 int r;
2413
2414 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
2415 FNAME_MESS, curbuf->b_ffname);
2416 if (dir_name == NULL)
2417 return -1;
2418 r = mch_chdir((char *)dir_name);
2419 vim_free(dir_name);
2420 return r;
2421#endif
2422}
2423
2424/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002425 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002427 * Some systems are quite slow in obtaining the user name (Windows NT), thus
2428 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 * Returns OK or FAIL.
2430 */
2431 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002432get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002434 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
2436 if (mch_get_user_name(buf, len) == FAIL)
2437 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002438 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
2440 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002441 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 return OK;
2443}
2444
Bram Moolenaar7ce5b2b2022-05-16 19:40:59 +01002445#if defined(EXITFREE) || defined(PROTO)
Yegappan Lakshmanancbae5802021-08-06 21:51:55 +02002446/*
2447 * Free the memory allocated by get_user_name()
2448 */
2449 void
2450free_username(void)
2451{
2452 vim_free(username);
2453}
Dominique Pelle748b3082022-01-08 12:41:16 +00002454#endif
Yegappan Lakshmanancbae5802021-08-06 21:51:55 +02002455
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456#ifndef HAVE_QSORT
2457/*
2458 * Our own qsort(), for systems that don't have it.
2459 * It's simple and slow. From the K&R C book.
2460 */
2461 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002462qsort(
2463 void *base,
2464 size_t elm_count,
2465 size_t elm_size,
2466 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468 char_u *buf;
2469 char_u *p1;
2470 char_u *p2;
2471 int i, j;
2472 int gap;
2473
Bram Moolenaar964b3742019-05-24 18:54:09 +02002474 buf = alloc(elm_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 if (buf == NULL)
2476 return;
2477
2478 for (gap = elm_count / 2; gap > 0; gap /= 2)
2479 for (i = gap; i < elm_count; ++i)
2480 for (j = i - gap; j >= 0; j -= gap)
2481 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002482 // Compare the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 p1 = (char_u *)base + j * elm_size;
2484 p2 = (char_u *)base + (j + gap) * elm_size;
2485 if ((*cmp)((void *)p1, (void *)p2) <= 0)
2486 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002487 // Exchange the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 mch_memmove(buf, p1, elm_size);
2489 mch_memmove(p1, p2, elm_size);
2490 mch_memmove(p2, buf, elm_size);
2491 }
2492
2493 vim_free(buf);
2494}
2495#endif
2496
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 * The putenv() implementation below comes from the "screen" program.
2499 * Included with permission from Juergen Weigert.
2500 * See pty.c for the copyright notice.
2501 */
2502
2503/*
2504 * putenv -- put value into environment
2505 *
2506 * Usage: i = putenv (string)
2507 * int i;
2508 * char *string;
2509 *
2510 * where string is of the form <name>=<value>.
2511 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
2512 *
2513 * Putenv may need to add a new name into the environment, or to
2514 * associate a value longer than the current value with a particular
2515 * name. So, to make life simpler, putenv() copies your entire
2516 * environment into the heap (i.e. malloc()) from the stack
2517 * (i.e. where it resides when your process is initiated) the first
2518 * time you call it.
2519 *
2520 * (history removed, not very interesting. See the "screen" sources.)
2521 */
2522
2523#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
2524
Bram Moolenaar85a20022019-12-21 18:25:54 +01002525#define EXTRASIZE 5 // increment to add to env. size
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
Bram Moolenaar85a20022019-12-21 18:25:54 +01002527static int envsize = -1; // current size of environment
2528extern char **environ; // the global which is your env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
Bram Moolenaar85a20022019-12-21 18:25:54 +01002530static int findenv(char *name); // look for a name in the env.
2531static int newenv(void); // copy env. from stack to heap
2532static int moreenv(void); // incr. size of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
2534 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002535putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
2537 int i;
2538 char *p;
2539
2540 if (envsize < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002541 { // first time putenv called
2542 if (newenv() < 0) // copy env. to heap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 return -1;
2544 }
2545
Bram Moolenaar85a20022019-12-21 18:25:54 +01002546 i = findenv((char *)string); // look for name in environment
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
2548 if (i < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002549 { // name must be added
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 for (i = 0; environ[i]; i++);
2551 if (i >= (envsize - 1))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002552 { // need new slot
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (moreenv() < 0)
2554 return -1;
2555 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002556 p = alloc(strlen(string) + 1);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002557 if (p == NULL) // not enough core
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 return -1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002559 environ[i + 1] = 0; // new end of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
2561 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002562 { // name already in env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 p = vim_realloc(environ[i], strlen(string) + 1);
2564 if (p == NULL)
2565 return -1;
2566 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002567 sprintf(p, "%s", string); // copy into env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 environ[i] = p;
2569
2570 return 0;
2571}
2572
2573 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002574findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 char *namechar, *envchar;
2577 int i, found;
2578
2579 found = 0;
2580 for (i = 0; environ[i] && !found; i++)
2581 {
2582 envchar = environ[i];
2583 namechar = name;
2584 while (*namechar && *namechar != '=' && (*namechar == *envchar))
2585 {
2586 namechar++;
2587 envchar++;
2588 }
2589 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
2590 }
2591 return found ? i - 1 : -1;
2592}
2593
2594 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002595newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596{
2597 char **env, *elem;
2598 int i, esize;
2599
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 for (i = 0; environ[i]; i++)
2601 ;
Bram Moolenaard0573012017-10-28 21:11:06 +02002602
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 esize = i + EXTRASIZE + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002604 env = ALLOC_MULT(char *, esize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 if (env == NULL)
2606 return -1;
2607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 for (i = 0; environ[i]; i++)
2609 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002610 elem = alloc(strlen(environ[i]) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 if (elem == NULL)
2612 return -1;
2613 env[i] = elem;
2614 strcpy(elem, environ[i]);
2615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
2617 env[i] = 0;
2618 environ = env;
2619 envsize = esize;
2620 return 0;
2621}
2622
2623 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002624moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
2626 int esize;
2627 char **env;
2628
2629 esize = envsize + EXTRASIZE;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002630 env = vim_realloc((char *)environ, esize * sizeof (*env));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 if (env == 0)
2632 return -1;
2633 environ = env;
2634 envsize = esize;
2635 return 0;
2636}
2637
2638# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02002639/*
2640 * Used for mch_getenv() for Mac.
2641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002643vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644{
2645 int i;
2646 char_u *p;
2647
2648 if (envsize < 0)
2649 return NULL;
2650
2651 i = findenv((char *)string);
2652
2653 if (i < 0)
2654 return NULL;
2655
2656 p = vim_strchr((char_u *)environ[i], '=');
2657 return (p + 1);
2658}
2659# endif
2660
Bram Moolenaar85a20022019-12-21 18:25:54 +01002661#endif // !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002662
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002663#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002664/*
2665 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
2666 * rights to write into.
2667 */
2668 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002669filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002670{
2671 int retval = 0;
2672#if defined(UNIX) || defined(VMS)
2673 int perm = 0;
2674#endif
2675
2676#if defined(UNIX) || defined(VMS)
2677 perm = mch_getperm(fname);
2678#endif
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002679 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01002680# ifdef MSWIN
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002681 mch_writable(fname) &&
2682# else
2683# if defined(UNIX) || defined(VMS)
2684 (perm & 0222) &&
2685# endif
2686# endif
2687 mch_access((char *)fname, W_OK) == 0
2688 )
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002689 {
2690 ++retval;
2691 if (mch_isdir(fname))
2692 ++retval;
2693 }
2694 return retval;
2695}
2696#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00002697
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002698#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
2699/*
2700 * Read 2 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002701 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002702 */
2703 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002704get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002705{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002706 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002707
2708 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002709 if (n == EOF) return -1;
2710 c = getc(fd);
2711 if (c == EOF) return -1;
2712 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002713}
2714
2715/*
2716 * Read 3 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002717 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002718 */
2719 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002720get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002721{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002722 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002723
2724 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002725 if (n == EOF) return -1;
2726 c = getc(fd);
2727 if (c == EOF) return -1;
2728 n = (n << 8) + c;
2729 c = getc(fd);
2730 if (c == EOF) return -1;
2731 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002732}
2733
2734/*
2735 * Read 4 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002736 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002737 */
2738 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002739get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002740{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002741 int c;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002742 // Use unsigned rather than int otherwise result is undefined
2743 // when left-shift sets the MSB.
Bram Moolenaar95235e62013-09-08 16:07:07 +02002744 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002745
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002746 c = getc(fd);
2747 if (c == EOF) return -1;
2748 n = (unsigned)c;
2749 c = getc(fd);
2750 if (c == EOF) return -1;
2751 n = (n << 8) + (unsigned)c;
2752 c = getc(fd);
2753 if (c == EOF) return -1;
2754 n = (n << 8) + (unsigned)c;
2755 c = getc(fd);
2756 if (c == EOF) return -1;
2757 n = (n << 8) + (unsigned)c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02002758 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002759}
2760
2761/*
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002762 * Read a string of length "cnt" from "fd" into allocated memory.
2763 * Returns NULL when out of memory or unable to read that many bytes.
2764 */
2765 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002766read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002767{
2768 char_u *str;
2769 int i;
2770 int c;
2771
Bram Moolenaar85a20022019-12-21 18:25:54 +01002772 // allocate memory
Bram Moolenaar964b3742019-05-24 18:54:09 +02002773 str = alloc(cnt + 1);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002774 if (str != NULL)
2775 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002776 // Read the string. Quit when running into the EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002777 for (i = 0; i < cnt; ++i)
2778 {
2779 c = getc(fd);
2780 if (c == EOF)
2781 {
2782 vim_free(str);
2783 return NULL;
2784 }
2785 str[i] = c;
2786 }
2787 str[i] = NUL;
2788 }
2789 return str;
2790}
2791
2792/*
2793 * Write a number to file "fd", MSB first, in "len" bytes.
2794 */
2795 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002796put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002797{
2798 int i;
2799
2800 for (i = len - 1; i >= 0; --i)
2801 if (putc((int)(nr >> (i * 8)), fd) == EOF)
2802 return FAIL;
2803 return OK;
2804}
2805
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002806#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01002807
Bram Moolenaar85a20022019-12-21 18:25:54 +01002808#ifndef PROTO // proto is defined in vim.h
Bram Moolenaar51628222016-12-01 23:03:28 +01002809# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002810/*
2811 * Return time in msec since "start_tv".
2812 */
2813 long
2814elapsed(struct timeval *start_tv)
2815{
2816 struct timeval now_tv;
2817
2818 gettimeofday(&now_tv, NULL);
2819 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
2820 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
2821}
Bram Moolenaar51628222016-12-01 23:03:28 +01002822# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002823
Bram Moolenaar51628222016-12-01 23:03:28 +01002824# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002825/*
2826 * Return time in msec since "start_tick".
2827 */
2828 long
2829elapsed(DWORD start_tick)
2830{
2831 DWORD now = GetTickCount();
2832
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002833 return (long)now - (long)start_tick;
2834}
Bram Moolenaar51628222016-12-01 23:03:28 +01002835# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002836#endif
Bram Moolenaar20608922018-04-21 22:30:08 +02002837
2838#if defined(FEAT_JOB_CHANNEL) \
2839 || (defined(UNIX) && (!defined(USE_SYSTEM) \
2840 || (defined(FEAT_GUI) && defined(FEAT_TERMINAL)))) \
2841 || defined(PROTO)
2842/*
2843 * Parse "cmd" and put the white-separated parts in "argv".
2844 * "argv" is an allocated array with "argc" entries and room for 4 more.
2845 * Returns FAIL when out of memory.
2846 */
2847 int
2848mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
2849{
2850 int i;
2851 char_u *p, *d;
2852 int inquote;
2853
2854 /*
2855 * Do this loop twice:
2856 * 1: find number of arguments
2857 * 2: separate them and build argv[]
2858 */
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002859 for (i = 1; i <= 2; ++i)
Bram Moolenaar20608922018-04-21 22:30:08 +02002860 {
2861 p = skipwhite(cmd);
2862 inquote = FALSE;
2863 *argc = 0;
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002864 while (*p != NUL)
Bram Moolenaar20608922018-04-21 22:30:08 +02002865 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002866 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02002867 (*argv)[*argc] = (char *)p;
2868 ++*argc;
2869 d = p;
2870 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
2871 {
2872 if (p[0] == '"')
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02002873 // quotes surrounding an argument and are dropped
Bram Moolenaar20608922018-04-21 22:30:08 +02002874 inquote = !inquote;
2875 else
2876 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02002877 if (rem_backslash(p))
Bram Moolenaar20608922018-04-21 22:30:08 +02002878 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02002879 // First pass: skip over "\ " and "\"".
2880 // Second pass: Remove the backslash.
Bram Moolenaar20608922018-04-21 22:30:08 +02002881 ++p;
2882 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002883 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02002884 *d++ = *p;
2885 }
2886 ++p;
2887 }
2888 if (*p == NUL)
2889 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002890 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02002891 *d++ = NUL;
2892 break;
2893 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002894 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02002895 *d++ = NUL;
2896 p = skipwhite(p + 1);
2897 }
2898 if (*argv == NULL)
2899 {
2900 if (use_shcf)
2901 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002902 // Account for possible multiple args in p_shcf.
Bram Moolenaar20608922018-04-21 22:30:08 +02002903 p = p_shcf;
2904 for (;;)
2905 {
2906 p = skiptowhite(p);
2907 if (*p == NUL)
2908 break;
2909 ++*argc;
2910 p = skipwhite(p);
2911 }
2912 }
2913
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002914 *argv = ALLOC_MULT(char *, *argc + 4);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002915 if (*argv == NULL) // out of memory
Bram Moolenaar20608922018-04-21 22:30:08 +02002916 return FAIL;
2917 }
2918 }
2919 return OK;
2920}
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002921
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002922/*
2923 * Build "argv[argc]" from the string "cmd".
2924 * "argv[argc]" is set to NULL;
2925 * Return FAIL when out of memory.
2926 */
2927 int
2928build_argv_from_string(char_u *cmd, char ***argv, int *argc)
2929{
2930 char_u *cmd_copy;
2931 int i;
2932
Bram Moolenaar85a20022019-12-21 18:25:54 +01002933 // Make a copy, parsing will modify "cmd".
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002934 cmd_copy = vim_strsave(cmd);
2935 if (cmd_copy == NULL
2936 || mch_parse_cmd(cmd_copy, FALSE, argv, argc) == FAIL)
2937 {
2938 vim_free(cmd_copy);
2939 return FAIL;
2940 }
2941 for (i = 0; i < *argc; i++)
2942 (*argv)[i] = (char *)vim_strsave((char_u *)(*argv)[i]);
2943 (*argv)[*argc] = NULL;
2944 vim_free(cmd_copy);
2945 return OK;
2946}
2947
Dominique Pellee8741a72022-01-17 11:23:45 +00002948# if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002949/*
2950 * Build "argv[argc]" from the list "l".
2951 * "argv[argc]" is set to NULL;
2952 * Return FAIL when out of memory.
2953 */
2954 int
2955build_argv_from_list(list_T *l, char ***argv, int *argc)
2956{
2957 listitem_T *li;
2958 char_u *s;
2959
Bram Moolenaar85a20022019-12-21 18:25:54 +01002960 // Pass argv[] to mch_call_shell().
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002961 *argv = ALLOC_MULT(char *, l->lv_len + 1);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002962 if (*argv == NULL)
2963 return FAIL;
2964 *argc = 0;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002965 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002966 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002967 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002968 if (s == NULL)
2969 {
2970 int i;
2971
2972 for (i = 0; i < *argc; ++i)
Bram Moolenaardf195602020-04-13 18:13:33 +02002973 VIM_CLEAR((*argv)[i]);
Bram Moolenaar7c25a7c2021-10-05 19:19:35 +01002974 (*argv)[0] = NULL;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002975 return FAIL;
2976 }
2977 (*argv)[*argc] = (char *)vim_strsave(s);
2978 *argc += 1;
2979 }
2980 (*argv)[*argc] = NULL;
2981 return OK;
2982}
2983# endif
Bram Moolenaar20608922018-04-21 22:30:08 +02002984#endif
Bram Moolenaar57da6982019-09-13 22:30:11 +02002985
2986/*
2987 * Change the behavior of vterm.
2988 * 0: As usual.
2989 * 1: Windows 10 version 1809
2990 * The bug causes unstable handling of ambiguous width character.
Bram Moolenaar36e7a822019-11-13 21:49:24 +01002991 * 2: Windows 10 version 1903 & 1909
Bram Moolenaar57da6982019-09-13 22:30:11 +02002992 * Use the wrong result because each result is different.
2993 * 3: Windows 10 insider preview (current latest logic)
2994 */
2995 int
2996get_special_pty_type(void)
2997{
2998#ifdef MSWIN
2999 return get_conpty_type();
3000#else
3001 return 0;
3002#endif
3003}