blob: 30674142c008cea12f0f8f04b9103eb51af2f6a1 [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 Moolenaar04e0ed12022-09-10 20:00:56 +010088 * Try to advance the Cursor to the specified screen column "wantcol".
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 * 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 Moolenaar04e0ed12022-09-10 20:00:56 +010097coladvance(colnr_T wantcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000098{
Bram Moolenaar04e0ed12022-09-10 20:00:56 +010099 int rc = getvpos(&curwin->w_cursor, wantcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100101 if (wantcol == MAXCOL || rc == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 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;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100107 curwin->w_virtcol = wantcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 }
109 return rc;
110}
111
112/*
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100113 * Return in "pos" the position of the cursor advanced to screen column
114 * "wantcol".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 * return OK if desired column is reached, FAIL if not
116 */
117 int
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100118getvpos(pos_T *pos, colnr_T wantcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119{
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100120 return coladvance2(pos, FALSE, virtual_active(), wantcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121}
122
123 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100124coladvance2(
125 pos_T *pos,
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200126 int addspaces, // change the text to achieve our goal?
127 int finetune, // change char offset for the exact column
128 colnr_T wcol_arg) // column to move to (can be negative)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129{
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200130 colnr_T wcol = wcol_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 char_u *line;
133 colnr_T col = 0;
134 int csize = 0;
135 int one_more;
136#ifdef FEAT_LINEBREAK
137 int head = 0;
138#endif
139
Bram Moolenaar24959102022-05-07 20:01:16 +0100140 one_more = (State & MODE_INSERT)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000141 || restart_edit != NUL
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000142 || (VIsual_active && *p_sel != 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +0200143 || ((get_ve_flags() & VE_ONEMORE) && wcol < MAXCOL);
Bram Moolenaara1381de2009-11-03 15:44:21 +0000144 line = ml_get_buf(curbuf, pos->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
146 if (wcol >= MAXCOL)
147 {
148 idx = (int)STRLEN(line) - 1 + one_more;
149 col = wcol;
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 if ((addspaces || finetune) && !VIsual_active)
152 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +0100153 curwin->w_curswant = linetabsize_str(line) + one_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 if (curwin->w_curswant > 0)
155 --curwin->w_curswant;
156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 }
158 else
159 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100160 int width = curwin->w_width - win_col_off(curwin);
161 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
Bram Moolenaarebefac62005-12-28 22:39:57 +0000163 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 && curwin->w_p_wrap
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 && curwin->w_width != 0
Bram Moolenaar02f86942021-08-17 22:14:29 +0200166 && wcol >= (colnr_T)width
167 && width > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 {
Bram Moolenaarc9121f72022-10-14 20:09:04 +0100169 csize = linetabsize_str(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (csize > 0)
171 csize--;
172
Bram Moolenaarebefac62005-12-28 22:39:57 +0000173 if (wcol / width > (colnr_T)csize / width
Bram Moolenaar24959102022-05-07 20:01:16 +0100174 && ((State & MODE_INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100176 // In case of line wrapping don't move the cursor beyond the
177 // right screen edge. In Insert mode allow going just beyond
178 // the last character (like what happens when typing and
179 // reaching the right window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 wcol = (csize / width + 1) * width - 1;
181 }
182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100184 init_chartabsize_arg(&cts, curwin, pos->lnum, 0, line, line);
185 while (cts.cts_vcol <= wcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100187#ifdef FEAT_PROP_POPUP
188 int at_start = cts.cts_ptr == cts.cts_line;
189#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100190 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#ifdef FEAT_LINEBREAK
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100192 csize = win_lbr_chartabsize(&cts, &head);
193 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194#else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100195 csize = lbr_chartabsize_adv(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100197 cts.cts_vcol += csize;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100198#ifdef FEAT_PROP_POPUP
199 if (at_start)
200 // do not count the columns for virtual text above
201 cts.cts_vcol -= cts.cts_first_char;
202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100204 col = cts.cts_vcol;
205 idx = (int)(cts.cts_ptr - line);
206 clear_chartabsize_arg(&cts);
207
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 /*
209 * Handle all the special cases. The virtual_active() check
210 * is needed to ensure that a virtual position off the end of
211 * a line has the correct indexing. The one_more comparison
212 * replaces an explicit add of one_more later on.
213 */
214 if (col > wcol || (!virtual_active() && one_more == 0))
215 {
216 idx -= 1;
217# ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +0100218 // Don't count the chars from 'showbreak'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 csize -= head;
220# endif
221 col -= csize;
222 }
223
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 if (virtual_active()
225 && addspaces
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200226 && wcol >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 && ((col != wcol && col != wcol + 1) || csize > 1))
228 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100229 // 'virtualedit' is set: The difference between wcol and col is
230 // filled with spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
232 if (line[idx] == NUL)
233 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100234 // Append spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 int correct = wcol - col;
236 char_u *newline = alloc(idx + correct + 1);
237 int t;
238
239 if (newline == NULL)
240 return FAIL;
241
242 for (t = 0; t < idx; ++t)
243 newline[t] = line[t];
244
245 for (t = 0; t < correct; ++t)
246 newline[t + idx] = ' ';
247
248 newline[idx + correct] = NUL;
249
250 ml_replace(pos->lnum, newline, FALSE);
251 changed_bytes(pos->lnum, (colnr_T)idx);
252 idx += correct;
253 col = wcol;
254 }
255 else
256 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100257 // Break a tab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 int linelen = (int)STRLEN(line);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100259 int correct = wcol - col - csize + 1; // negative!!
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000260 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 int t, s = 0;
262 int v;
263
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000264 if (-correct > csize)
265 return FAIL;
266
267 newline = alloc(linelen + csize);
268 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 return FAIL;
270
271 for (t = 0; t < linelen; t++)
272 {
273 if (t != idx)
274 newline[s++] = line[t];
275 else
276 for (v = 0; v < csize; v++)
277 newline[s++] = ' ';
278 }
279
280 newline[linelen + csize - 1] = NUL;
281
282 ml_replace(pos->lnum, newline, FALSE);
283 changed_bytes(pos->lnum, idx);
284 idx += (csize - 1 + correct);
285 col += correct;
286 }
287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 }
289
290 if (idx < 0)
291 pos->col = 0;
292 else
293 pos->col = idx;
294
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 pos->coladd = 0;
296
297 if (finetune)
298 {
299 if (wcol == MAXCOL)
300 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100301 // The width of the last character is used to set coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 if (!one_more)
303 {
304 colnr_T scol, ecol;
305
306 getvcol(curwin, pos, &scol, NULL, &ecol);
307 pos->coladd = ecol - scol;
308 }
309 }
310 else
311 {
312 int b = (int)wcol - (int)col;
313
Bram Moolenaar85a20022019-12-21 18:25:54 +0100314 // The difference between wcol and col is used to set coladd.
Bram Moolenaar02631462017-09-22 15:20:32 +0200315 if (b > 0 && b < (MAXCOL - 2 * curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 pos->coladd = b;
317
318 col += b;
319 }
320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
Bram Moolenaar85a20022019-12-21 18:25:54 +0100322 // prevent from moving onto a trail byte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200324 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200326 if (wcol < 0 || col < wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 return FAIL;
328 return OK;
329}
330
331/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000332 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 */
334 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100335inc_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 return inc(&curwin->w_cursor);
338}
339
Bram Moolenaar446cb832008-06-24 21:56:24 +0000340/*
341 * Increment the line pointer "lp" crossing line boundaries as necessary.
342 * Return 1 when going to the next line.
343 * Return 2 when moving forward onto a NUL at the end of the line).
344 * Return -1 when at the end of file.
345 * Return 0 otherwise.
346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100348inc(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100350 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
Bram Moolenaar85a20022019-12-21 18:25:54 +0100352 // when searching position may be set to end of a line
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100353 if (lp->col != MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100355 p = ml_get_pos(lp);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100356 if (*p != NUL) // still within line, move to next char (may be NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100358 if (has_mbyte)
359 {
360 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100362 lp->col += l;
363 return ((p[l] != NUL) ? 0 : 2);
364 }
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100365 lp->col++;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100366 lp->coladd = 0;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100367 return ((p[1] != NUL) ? 0 : 2);
368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100370 if (lp->lnum != curbuf->b_ml.ml_line_count) // there is a next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 {
372 lp->col = 0;
373 lp->lnum++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 lp->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 return 1;
376 }
377 return -1;
378}
379
380/*
381 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
382 */
383 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100384incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385{
386 int r;
387
388 if ((r = inc(lp)) >= 1 && lp->col)
389 r = inc(lp);
390 return r;
391}
392
393/*
394 * dec(p)
395 *
396 * Decrement the line pointer 'p' crossing line boundaries as necessary.
397 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
398 */
399 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100400dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100402 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403}
404
405 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100406dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407{
408 char_u *p;
409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 lp->coladd = 0;
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100411 if (lp->col == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100413 // past end of line
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100414 p = ml_get(lp->lnum);
415 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100416 if (has_mbyte)
417 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100418 return 0;
419 }
420
421 if (lp->col > 0)
422 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100423 // still within line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 lp->col--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 if (has_mbyte)
426 {
427 p = ml_get(lp->lnum);
428 lp->col -= (*mb_head_off)(p, p + lp->col);
429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 return 0;
431 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100432
433 if (lp->lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100435 // there is a prior line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 lp->lnum--;
437 p = ml_get(lp->lnum);
438 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 if (has_mbyte)
440 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 return 1;
442 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100443
Bram Moolenaar85a20022019-12-21 18:25:54 +0100444 // at start of file
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100445 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446}
447
448/*
449 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
450 */
451 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100452decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453{
454 int r;
455
456 if ((r = dec(lp)) == 1 && lp->col)
457 r = dec(lp);
458 return r;
459}
460
461/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200462 * Get the line number relative to the current cursor position, i.e. the
463 * difference between line number and cursor position. Only look for lines that
464 * can be visible, folded lines don't count.
465 */
466 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100467get_cursor_rel_lnum(
468 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100469 linenr_T lnum) // line number to get the result for
Bram Moolenaar64486672010-05-16 15:46:46 +0200470{
471 linenr_T cursor = wp->w_cursor.lnum;
472 linenr_T retval = 0;
473
474#ifdef FEAT_FOLDING
475 if (hasAnyFolding(wp))
476 {
477 if (lnum > cursor)
478 {
479 while (lnum > cursor)
480 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100481 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100482 // if lnum and cursor are in the same fold,
483 // now lnum <= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200484 if (lnum > cursor)
485 retval++;
486 lnum--;
487 }
488 }
489 else if (lnum < cursor)
490 {
491 while (lnum < cursor)
492 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100493 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100494 // if lnum and cursor are in the same fold,
495 // now lnum >= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200496 if (lnum < cursor)
497 retval--;
498 lnum++;
499 }
500 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100501 // else if (lnum == cursor)
502 // retval = 0;
Bram Moolenaar64486672010-05-16 15:46:46 +0200503 }
504 else
505#endif
506 retval = lnum - cursor;
507
508 return retval;
509}
510
511/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200512 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
513 * This allows for the col to be on the NUL byte.
514 */
515 void
516check_pos(buf_T *buf, pos_T *pos)
517{
518 char_u *line;
519 colnr_T len;
520
521 if (pos->lnum > buf->b_ml.ml_line_count)
522 pos->lnum = buf->b_ml.ml_line_count;
523
524 if (pos->col > 0)
525 {
526 line = ml_get_buf(buf, pos->lnum, FALSE);
527 len = (colnr_T)STRLEN(line);
528 if (pos->col > len)
529 pos->col = len;
530 }
531}
532
533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 * Make sure curwin->w_cursor.lnum is valid.
535 */
536 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100537check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538{
539 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
540 {
541#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100542 // If there is a closed fold at the end of the file, put the cursor in
543 // its first line. Otherwise in the last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 if (!hasFolding(curbuf->b_ml.ml_line_count,
545 &curwin->w_cursor.lnum, NULL))
546#endif
547 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
548 }
549 if (curwin->w_cursor.lnum <= 0)
550 curwin->w_cursor.lnum = 1;
551}
552
553/*
554 * Make sure curwin->w_cursor.col is valid.
555 */
556 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100557check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200559 check_cursor_col_win(curwin);
560}
561
562/*
563 * Make sure win->w_cursor.col is valid.
564 */
565 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100566check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200567{
Gary Johnson53ba05b2021-07-26 22:19:10 +0200568 colnr_T len;
569 colnr_T oldcol = win->w_cursor.col;
570 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
571 unsigned int cur_ve_flags = get_ve_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200573 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200575 win->w_cursor.col = 0;
576 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100578 // Allow cursor past end-of-line when:
579 // - in Insert mode or restarting Insert mode
580 // - in Visual mode and 'selection' isn't "old"
581 // - 'virtualedit' is set
Bram Moolenaar24959102022-05-07 20:01:16 +0100582 if ((State & MODE_INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 || (VIsual_active && *p_sel != 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +0200584 || (cur_ve_flags & VE_ONEMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200586 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000588 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200589 win->w_cursor.col = len - 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100590 // Move the cursor to the head byte.
Bram Moolenaar87c19962007-04-26 08:54:21 +0000591 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200592 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200595 else if (win->w_cursor.col < 0)
596 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaar85a20022019-12-21 18:25:54 +0100598 // If virtual editing is on, we can leave the cursor on the old position,
599 // only we must set it to virtual. But don't do it when at the end of the
600 // line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200602 win->w_cursor.coladd = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +0200603 else if (cur_ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000604 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200605 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200606 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200607 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaard41babe2017-08-30 17:01:35 +0200608
Bram Moolenaar85a20022019-12-21 18:25:54 +0100609 // Make sure that coladd is not more than the char width.
610 // Not for the last character, coladd is then used when the cursor
611 // is actually after the last character.
Bram Moolenaarfe154992022-03-22 20:42:12 +0000612 if (win->w_cursor.col + 1 < len)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200613 {
614 int cs, ce;
615
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200616 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
617 if (win->w_cursor.coladd > ce - cs)
618 win->w_cursor.coladd = ce - cs;
619 }
620 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000621 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100622 // avoid weird number when there is a miscalculation or overflow
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200623 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625}
626
627/*
628 * make sure curwin->w_cursor in on a valid character
629 */
630 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100631check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 check_cursor_lnum();
634 check_cursor_col();
635}
636
Bram Moolenaar7ce5b2b2022-05-16 19:40:59 +0100637/*
638 * Check if VIsual position is valid, correct it if not.
639 * Can be called when in Visual mode and a change has been made.
640 */
641 void
642check_visual_pos(void)
643{
644 if (VIsual.lnum > curbuf->b_ml.ml_line_count)
645 {
646 VIsual.lnum = curbuf->b_ml.ml_line_count;
647 VIsual.col = 0;
648 VIsual.coladd = 0;
649 }
650 else
651 {
652 int len = (int)STRLEN(ml_get(VIsual.lnum));
653
654 if (VIsual.col > len)
655 {
656 VIsual.col = len;
657 VIsual.coladd = 0;
658 }
659 }
660}
661
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662/*
663 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
664 * Allow it when in Visual mode and 'selection' is not "old".
665 */
666 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100667adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668{
669 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 && gchar_cursor() == NUL)
672 --curwin->w_cursor.col;
673}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
675/*
Bram Moolenaar0c34d562022-11-18 14:07:20 +0000676 * Set "curwin->w_leftcol" to "leftcol".
677 * Adjust the cursor position if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 * Return TRUE if the cursor was moved.
679 */
680 int
Bram Moolenaar0c34d562022-11-18 14:07:20 +0000681set_leftcol(colnr_T leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 int retval = FALSE;
Bram Moolenaar0c34d562022-11-18 14:07:20 +0000684
685 // Return quickly when there is no change.
686 if (curwin->w_leftcol == leftcol)
687 return FALSE;
688 curwin->w_leftcol = leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
690 changed_cline_bef_curs();
Bram Moolenaar0c34d562022-11-18 14:07:20 +0000691 long lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 validate_virtcol();
693
Bram Moolenaar0c34d562022-11-18 14:07:20 +0000694 // If the cursor is right or left of the screen, move it to last or first
695 // visible character.
696 long siso = get_sidescrolloff_value();
Bram Moolenaar375e3392019-01-31 18:26:10 +0100697 if (curwin->w_virtcol > (colnr_T)(lastcol - siso))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
699 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100700 coladvance((colnr_T)(lastcol - siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100702 else if (curwin->w_virtcol < curwin->w_leftcol + siso)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100705 (void)coladvance((colnr_T)(curwin->w_leftcol + siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707
Bram Moolenaar0c34d562022-11-18 14:07:20 +0000708 // If the start of the character under the cursor is not on the screen,
709 // advance the cursor one more char. If this fails (last char of the
710 // line) adjust the scrolling.
711 colnr_T s, e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
713 if (e > (colnr_T)lastcol)
714 {
715 retval = TRUE;
716 coladvance(s - 1);
717 }
718 else if (s < curwin->w_leftcol)
719 {
720 retval = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100721 if (coladvance(e + 1) == FAIL) // there isn't another character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100723 curwin->w_leftcol = s; // adjust w_leftcol instead
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 changed_cline_bef_curs();
725 }
726 }
727
728 if (retval)
729 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100730 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 return retval;
732}
733
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 * Isolate one part of a string option where parts are separated with
736 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +0000737 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 * "*option" is advanced to the next part.
739 * The length is returned.
740 */
741 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100742copy_option_part(
743 char_u **option,
744 char_u *buf,
745 int maxlen,
746 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747{
748 int len = 0;
749 char_u *p = *option;
750
Bram Moolenaar85a20022019-12-21 18:25:54 +0100751 // skip '.' at start of option part, for 'suffixes'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 if (*p == '.')
753 buf[len++] = *p++;
754 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
755 {
756 /*
757 * Skip backslash before a separator character and space.
758 */
759 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
760 ++p;
761 if (len < maxlen - 1)
762 buf[len++] = *p;
763 ++p;
764 }
765 buf[len] = NUL;
766
Bram Moolenaar85a20022019-12-21 18:25:54 +0100767 if (*p != NUL && *p != ',') // skip non-standard separator
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 ++p;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100769 p = skip_to_option_part(p); // p points to next file name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
771 *option = p;
772 return len;
773}
774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#ifndef HAVE_MEMSET
776 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100777vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778{
779 char *p = ptr;
780
781 while (size-- > 0)
782 *p++ = c;
783 return ptr;
784}
785#endif
786
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787/*
788 * Vim has its own isspace() function, because on some machines isspace()
789 * can't handle characters above 128.
790 */
791 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100792vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794 return ((x >= 9 && x <= 13) || x == ' ');
795}
796
797/************************************************************************
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 * functions that use lookup tables for various things, generally to do with
799 * special key codes.
800 */
801
802/*
803 * Some useful tables.
804 */
805
806static struct modmasktable
807{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100808 short mod_mask; // Bit-mask for particular key modifier
809 short mod_flag; // Bit(s) for particular key modifier
810 char_u name; // Single letter name of modifier
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811} mod_mask_table[] =
812{
813 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000814 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
816 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
817 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
818 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
819 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
Bram Moolenaard0573012017-10-28 21:11:06 +0200820#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
822#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100823 // 'A' must be the last one
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
825 {0, 0, NUL}
Bram Moolenaar85a20022019-12-21 18:25:54 +0100826 // NOTE: when adding an entry, update MAX_KEY_NAME_LEN!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827};
828
829/*
830 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +0000831 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 */
833#define MOD_KEYS_ENTRY_SIZE 5
834
835static char_u modifier_keys_table[] =
836{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100837// mod mask with modifier without modifier
838 MOD_MASK_SHIFT, '&', '9', '@', '1', // begin
839 MOD_MASK_SHIFT, '&', '0', '@', '2', // cancel
840 MOD_MASK_SHIFT, '*', '1', '@', '4', // command
841 MOD_MASK_SHIFT, '*', '2', '@', '5', // copy
842 MOD_MASK_SHIFT, '*', '3', '@', '6', // create
843 MOD_MASK_SHIFT, '*', '4', 'k', 'D', // delete char
844 MOD_MASK_SHIFT, '*', '5', 'k', 'L', // delete line
845 MOD_MASK_SHIFT, '*', '7', '@', '7', // end
846 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', // end
847 MOD_MASK_SHIFT, '*', '9', '@', '9', // exit
848 MOD_MASK_SHIFT, '*', '0', '@', '0', // find
849 MOD_MASK_SHIFT, '#', '1', '%', '1', // help
850 MOD_MASK_SHIFT, '#', '2', 'k', 'h', // home
851 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', // home
852 MOD_MASK_SHIFT, '#', '3', 'k', 'I', // insert
853 MOD_MASK_SHIFT, '#', '4', 'k', 'l', // left arrow
854 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', // left arrow
855 MOD_MASK_SHIFT, '%', 'a', '%', '3', // message
856 MOD_MASK_SHIFT, '%', 'b', '%', '4', // move
857 MOD_MASK_SHIFT, '%', 'c', '%', '5', // next
858 MOD_MASK_SHIFT, '%', 'd', '%', '7', // options
859 MOD_MASK_SHIFT, '%', 'e', '%', '8', // previous
860 MOD_MASK_SHIFT, '%', 'f', '%', '9', // print
861 MOD_MASK_SHIFT, '%', 'g', '%', '0', // redo
862 MOD_MASK_SHIFT, '%', 'h', '&', '3', // replace
863 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', // right arr.
864 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', // right arr.
865 MOD_MASK_SHIFT, '%', 'j', '&', '5', // resume
866 MOD_MASK_SHIFT, '!', '1', '&', '6', // save
867 MOD_MASK_SHIFT, '!', '2', '&', '7', // suspend
868 MOD_MASK_SHIFT, '!', '3', '&', '8', // undo
869 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', // up arrow
870 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', // down arrow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
Bram Moolenaar85a20022019-12-21 18:25:54 +0100872 // vt100 F1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
874 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
875 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
876 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
877
Bram Moolenaar85a20022019-12-21 18:25:54 +0100878 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', // F1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
880 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
881 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
882 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
883 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
884 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
885 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
886 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
Bram Moolenaar85a20022019-12-21 18:25:54 +0100887 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', // F10
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
890 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
891 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
892 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
893 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
894 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
895 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
896 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
897 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
898 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
899
900 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
901 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
902 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
903 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
904 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
905 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
906 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
907 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
908 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
909 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
910
911 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
912 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
913 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
914 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
915 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
916 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
917 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
918
Bram Moolenaar85a20022019-12-21 18:25:54 +0100919 // TAB pseudo code
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
921
922 NUL
923};
924
925static struct key_name_entry
926{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100927 int key; // Special key code or ascii value
928 char_u *name; // Name of key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929} key_names_table[] =
930{
931 {' ', (char_u *)"Space"},
932 {TAB, (char_u *)"Tab"},
933 {K_TAB, (char_u *)"Tab"},
934 {NL, (char_u *)"NL"},
Bram Moolenaar85a20022019-12-21 18:25:54 +0100935 {NL, (char_u *)"NewLine"}, // Alternative name
936 {NL, (char_u *)"LineFeed"}, // Alternative name
937 {NL, (char_u *)"LF"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {CAR, (char_u *)"CR"},
Bram Moolenaar85a20022019-12-21 18:25:54 +0100939 {CAR, (char_u *)"Return"}, // Alternative name
940 {CAR, (char_u *)"Enter"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {K_BS, (char_u *)"BS"},
Bram Moolenaar85a20022019-12-21 18:25:54 +0100942 {K_BS, (char_u *)"BackSpace"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 {ESC, (char_u *)"Esc"},
944 {CSI, (char_u *)"CSI"},
945 {K_CSI, (char_u *)"xCSI"},
946 {'|', (char_u *)"Bar"},
947 {'\\', (char_u *)"Bslash"},
948 {K_DEL, (char_u *)"Del"},
Bram Moolenaar85a20022019-12-21 18:25:54 +0100949 {K_DEL, (char_u *)"Delete"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 {K_KDEL, (char_u *)"kDel"},
951 {K_UP, (char_u *)"Up"},
952 {K_DOWN, (char_u *)"Down"},
953 {K_LEFT, (char_u *)"Left"},
954 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000955 {K_XUP, (char_u *)"xUp"},
956 {K_XDOWN, (char_u *)"xDown"},
957 {K_XLEFT, (char_u *)"xLeft"},
958 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100959 {K_PS, (char_u *)"PasteStart"},
960 {K_PE, (char_u *)"PasteEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961
962 {K_F1, (char_u *)"F1"},
963 {K_F2, (char_u *)"F2"},
964 {K_F3, (char_u *)"F3"},
965 {K_F4, (char_u *)"F4"},
966 {K_F5, (char_u *)"F5"},
967 {K_F6, (char_u *)"F6"},
968 {K_F7, (char_u *)"F7"},
969 {K_F8, (char_u *)"F8"},
970 {K_F9, (char_u *)"F9"},
971 {K_F10, (char_u *)"F10"},
972
973 {K_F11, (char_u *)"F11"},
974 {K_F12, (char_u *)"F12"},
975 {K_F13, (char_u *)"F13"},
976 {K_F14, (char_u *)"F14"},
977 {K_F15, (char_u *)"F15"},
978 {K_F16, (char_u *)"F16"},
979 {K_F17, (char_u *)"F17"},
980 {K_F18, (char_u *)"F18"},
981 {K_F19, (char_u *)"F19"},
982 {K_F20, (char_u *)"F20"},
983
984 {K_F21, (char_u *)"F21"},
985 {K_F22, (char_u *)"F22"},
986 {K_F23, (char_u *)"F23"},
987 {K_F24, (char_u *)"F24"},
988 {K_F25, (char_u *)"F25"},
989 {K_F26, (char_u *)"F26"},
990 {K_F27, (char_u *)"F27"},
991 {K_F28, (char_u *)"F28"},
992 {K_F29, (char_u *)"F29"},
993 {K_F30, (char_u *)"F30"},
994
995 {K_F31, (char_u *)"F31"},
996 {K_F32, (char_u *)"F32"},
997 {K_F33, (char_u *)"F33"},
998 {K_F34, (char_u *)"F34"},
999 {K_F35, (char_u *)"F35"},
1000 {K_F36, (char_u *)"F36"},
1001 {K_F37, (char_u *)"F37"},
1002
1003 {K_XF1, (char_u *)"xF1"},
1004 {K_XF2, (char_u *)"xF2"},
1005 {K_XF3, (char_u *)"xF3"},
1006 {K_XF4, (char_u *)"xF4"},
1007
1008 {K_HELP, (char_u *)"Help"},
1009 {K_UNDO, (char_u *)"Undo"},
1010 {K_INS, (char_u *)"Insert"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001011 {K_INS, (char_u *)"Ins"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {K_KINS, (char_u *)"kInsert"},
1013 {K_HOME, (char_u *)"Home"},
1014 {K_KHOME, (char_u *)"kHome"},
1015 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001016 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 {K_END, (char_u *)"End"},
1018 {K_KEND, (char_u *)"kEnd"},
1019 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001020 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {K_PAGEUP, (char_u *)"PageUp"},
1022 {K_PAGEDOWN, (char_u *)"PageDown"},
1023 {K_KPAGEUP, (char_u *)"kPageUp"},
1024 {K_KPAGEDOWN, (char_u *)"kPageDown"},
1025
1026 {K_KPLUS, (char_u *)"kPlus"},
1027 {K_KMINUS, (char_u *)"kMinus"},
1028 {K_KDIVIDE, (char_u *)"kDivide"},
1029 {K_KMULTIPLY, (char_u *)"kMultiply"},
1030 {K_KENTER, (char_u *)"kEnter"},
1031 {K_KPOINT, (char_u *)"kPoint"},
1032
1033 {K_K0, (char_u *)"k0"},
1034 {K_K1, (char_u *)"k1"},
1035 {K_K2, (char_u *)"k2"},
1036 {K_K3, (char_u *)"k3"},
1037 {K_K4, (char_u *)"k4"},
1038 {K_K5, (char_u *)"k5"},
1039 {K_K6, (char_u *)"k6"},
1040 {K_K7, (char_u *)"k7"},
1041 {K_K8, (char_u *)"k8"},
1042 {K_K9, (char_u *)"k9"},
1043
1044 {'<', (char_u *)"lt"},
1045
1046 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001047#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001049#endif
1050#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001052#endif
1053#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001055#endif
1056#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001058#endif
1059#ifdef FEAT_MOUSE_URXVT
1060 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
1061#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001062 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
Bram Moolenaar21a83bd2021-02-23 19:19:58 +01001063 {K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelease"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
1065 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
1066 {K_LEFTDRAG, (char_u *)"LeftDrag"},
1067 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
1068 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001069 {K_MOUSEMOVE, (char_u *)"MouseMove"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
1071 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
1072 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
1073 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
1074 {K_RIGHTDRAG, (char_u *)"RightDrag"},
1075 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001076 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
1077 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
1078 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
1079 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001080 {K_MOUSEDOWN, (char_u *)"MouseDown"}, // OBSOLETE: Use
1081 {K_MOUSEUP, (char_u *)"MouseUp"}, // ScrollWheelXXX instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {K_X1MOUSE, (char_u *)"X1Mouse"},
1083 {K_X1DRAG, (char_u *)"X1Drag"},
1084 {K_X1RELEASE, (char_u *)"X1Release"},
1085 {K_X2MOUSE, (char_u *)"X2Mouse"},
1086 {K_X2DRAG, (char_u *)"X2Drag"},
1087 {K_X2RELEASE, (char_u *)"X2Release"},
1088 {K_DROP, (char_u *)"Drop"},
1089 {K_ZERO, (char_u *)"Nul"},
1090#ifdef FEAT_EVAL
1091 {K_SNR, (char_u *)"SNR"},
1092#endif
1093 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02001094 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar2f106582019-05-08 21:59:25 +02001095 {K_IGNORE, (char_u *)"Ignore"},
Bram Moolenaar957cf672020-11-12 14:21:06 +01001096 {K_COMMAND, (char_u *)"Cmd"},
Bram Moolenaare32c3c42022-01-15 18:26:04 +00001097 {K_SCRIPT_COMMAND, (char_u *)"ScriptCmd"},
Bram Moolenaarccb47a22021-01-21 13:36:43 +01001098 {K_FOCUSGAINED, (char_u *)"FocusGained"},
1099 {K_FOCUSLOST, (char_u *)"FocusLost"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 {0, NULL}
Bram Moolenaar85a20022019-12-21 18:25:54 +01001101 // NOTE: When adding a long name update MAX_KEY_NAME_LEN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102};
1103
K.Takataeeec2542021-06-02 13:28:16 +02001104#define KEY_NAMES_TABLE_LEN ARRAY_LENGTH(key_names_table)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106/*
1107 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
1108 * modifier name ('S' for Shift, 'C' for Ctrl etc).
1109 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001110 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001111name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112{
1113 int i;
1114
1115 c = TOUPPER_ASC(c);
1116 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
1117 if (c == mod_mask_table[i].name)
1118 return mod_mask_table[i].mod_flag;
1119 return 0;
1120}
1121
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122/*
1123 * Check if if there is a special key code for "key" that includes the
1124 * modifiers specified.
1125 */
1126 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001127simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128{
1129 int i;
1130 int key0;
1131 int key1;
1132
1133 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
1134 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001135 // TAB is a special case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
1137 {
1138 *modifiers &= ~MOD_MASK_SHIFT;
1139 return K_S_TAB;
1140 }
1141 key0 = KEY2TERMCAP0(key);
1142 key1 = KEY2TERMCAP1(key);
1143 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
1144 if (key0 == modifier_keys_table[i + 3]
1145 && key1 == modifier_keys_table[i + 4]
1146 && (*modifiers & modifier_keys_table[i]))
1147 {
1148 *modifiers &= ~modifier_keys_table[i];
1149 return TERMCAP2KEY(modifier_keys_table[i + 1],
1150 modifier_keys_table[i + 2]);
1151 }
1152 }
1153 return key;
1154}
1155
1156/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001157 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001158 */
1159 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001160handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001161{
1162 switch (key)
1163 {
1164 case K_XUP: return K_UP;
1165 case K_XDOWN: return K_DOWN;
1166 case K_XLEFT: return K_LEFT;
1167 case K_XRIGHT: return K_RIGHT;
1168 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001169 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001170 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001171 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001172 case K_XF1: return K_F1;
1173 case K_XF2: return K_F2;
1174 case K_XF3: return K_F3;
1175 case K_XF4: return K_F4;
1176 case K_S_XF1: return K_S_F1;
1177 case K_S_XF2: return K_S_F2;
1178 case K_S_XF3: return K_S_F3;
1179 case K_S_XF4: return K_S_F4;
1180 }
1181 return key;
1182}
1183
1184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 * Return a string which contains the name of the given key when the given
1186 * modifiers are down.
1187 */
1188 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001189get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190{
1191 static char_u string[MAX_KEY_NAME_LEN + 1];
1192
1193 int i, idx;
1194 int table_idx;
1195 char_u *s;
1196
1197 string[0] = '<';
1198 idx = 1;
1199
Bram Moolenaar85a20022019-12-21 18:25:54 +01001200 // Key that stands for a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
1202 c = KEY2TERMCAP1(c);
1203
1204 /*
1205 * Translate shifted special keys into unshifted keys and set modifier.
1206 * Same for CTRL and ALT modifiers.
1207 */
1208 if (IS_SPECIAL(c))
1209 {
1210 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
1211 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
1212 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
1213 {
1214 modifiers |= modifier_keys_table[i];
1215 c = TERMCAP2KEY(modifier_keys_table[i + 3],
1216 modifier_keys_table[i + 4]);
1217 break;
1218 }
1219 }
1220
Bram Moolenaar85a20022019-12-21 18:25:54 +01001221 // try to find the key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 table_idx = find_special_key_in_table(c);
1223
1224 /*
1225 * When not a known special key, and not a printable character, try to
1226 * extract modifiers.
1227 */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001228 if (c > 0 && (*mb_char2len)(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
1230 if (table_idx < 0
1231 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
1232 && (c & 0x80))
1233 {
1234 c &= 0x7f;
1235 modifiers |= MOD_MASK_ALT;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001236 // try again, to find the un-alted key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 table_idx = find_special_key_in_table(c);
1238 }
1239 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
1240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 c += '@';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 modifiers |= MOD_MASK_CTRL;
1243 }
1244 }
1245
Bram Moolenaar85a20022019-12-21 18:25:54 +01001246 // translate the modifier into a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 for (i = 0; mod_mask_table[i].name != 'A'; i++)
1248 if ((modifiers & mod_mask_table[i].mod_mask)
1249 == mod_mask_table[i].mod_flag)
1250 {
1251 string[idx++] = mod_mask_table[i].name;
1252 string[idx++] = (char_u)'-';
1253 }
1254
Bram Moolenaar85a20022019-12-21 18:25:54 +01001255 if (table_idx < 0) // unknown special key, may output t_xx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 {
1257 if (IS_SPECIAL(c))
1258 {
1259 string[idx++] = 't';
1260 string[idx++] = '_';
1261 string[idx++] = KEY2TERMCAP0(c);
1262 string[idx++] = KEY2TERMCAP1(c);
1263 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001264 // Not a special key, only modifiers, output directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 else
1266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 if (has_mbyte && (*mb_char2len)(c) > 1)
1268 idx += (*mb_char2bytes)(c, string + idx);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001269 else if (vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 string[idx++] = c;
1271 else
1272 {
1273 s = transchar(c);
1274 while (*s)
1275 string[idx++] = *s++;
1276 }
1277 }
1278 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001279 else // use name of special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 {
Bram Moolenaar423977d2017-01-22 15:05:12 +01001281 size_t len = STRLEN(key_names_table[table_idx].name);
1282
1283 if (len + idx + 2 <= MAX_KEY_NAME_LEN)
1284 {
1285 STRCPY(string + idx, key_names_table[table_idx].name);
1286 idx += (int)len;
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289 string[idx++] = '>';
1290 string[idx] = NUL;
1291 return string;
1292}
1293
1294/*
1295 * Try translating a <> name at (*srcp)[] to dst[].
1296 * Return the number of characters added to dst[], zero for no match.
1297 * If there is a match, srcp is advanced to after the <> name.
1298 * dst[] must be big enough to hold the result (up to six characters)!
1299 */
1300 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001301trans_special(
1302 char_u **srcp,
1303 char_u *dst,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001304 int flags, // FSK_ values
zeertzjqdb088872022-05-02 22:53:45 +01001305 int escape_ks, // escape K_SPECIAL bytes in the character
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001306 int *did_simplify) // FSK_SIMPLIFY and found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
1308 int modifiers = 0;
1309 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001311 key = find_special_key(srcp, &modifiers, flags, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 if (key == 0)
1313 return 0;
1314
zeertzjqdb088872022-05-02 22:53:45 +01001315 return special_to_buf(key, modifiers, escape_ks, dst);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001316}
1317
1318/*
1319 * Put the character sequence for "key" with "modifiers" into "dst" and return
1320 * the resulting length.
zeertzjqdb088872022-05-02 22:53:45 +01001321 * When "escape_ks" is TRUE escape K_SPECIAL bytes in the character.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001322 * The sequence is not NUL terminated.
1323 * This is how characters in a string are encoded.
1324 */
1325 int
zeertzjqdb088872022-05-02 22:53:45 +01001326special_to_buf(int key, int modifiers, int escape_ks, char_u *dst)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001327{
1328 int dlen = 0;
1329
Bram Moolenaar85a20022019-12-21 18:25:54 +01001330 // Put the appropriate modifier in a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 if (modifiers != 0)
1332 {
1333 dst[dlen++] = K_SPECIAL;
1334 dst[dlen++] = KS_MODIFIER;
1335 dst[dlen++] = modifiers;
1336 }
1337
1338 if (IS_SPECIAL(key))
1339 {
1340 dst[dlen++] = K_SPECIAL;
1341 dst[dlen++] = KEY2TERMCAP0(key);
1342 dst[dlen++] = KEY2TERMCAP1(key);
1343 }
zeertzjqdb088872022-05-02 22:53:45 +01001344 else if (escape_ks)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
zeertzjqdb088872022-05-02 22:53:45 +01001346 else if (has_mbyte)
1347 dlen += (*mb_char2bytes)(key, dst + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 else
1349 dst[dlen++] = key;
1350
1351 return dlen;
1352}
1353
1354/*
1355 * Try translating a <> name at (*srcp)[], return the key and modifiers.
1356 * srcp is advanced to after the <> name.
1357 * returns 0 if there is no match.
1358 */
1359 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001360find_special_key(
1361 char_u **srcp,
1362 int *modp,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001363 int flags, // FSK_ values
Bram Moolenaar459fd782019-10-13 16:43:39 +02001364 int *did_simplify) // found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 char_u *last_dash;
1367 char_u *end_of_name;
1368 char_u *src;
1369 char_u *bp;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001370 int in_string = flags & FSK_IN_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 int modifiers;
1372 int bit;
1373 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001374 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001375 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377 src = *srcp;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001378 if (src[0] != '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 return 0;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001380 if (src[1] == '*') // <*xxx>: do not simplify
1381 ++src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
Bram Moolenaar85a20022019-12-21 18:25:54 +01001383 // Find end of modifier list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 last_dash = src;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02001385 for (bp = src + 1; *bp == '-' || vim_isNormalIDc(*bp); bp++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 {
1387 if (*bp == '-')
1388 {
1389 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001390 if (bp[1] != NUL)
1391 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001392 if (has_mbyte)
1393 l = mb_ptr2len(bp + 1);
1394 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001395 l = 1;
Bram Moolenaarc8fd33d2019-08-16 20:33:05 +02001396 // Anything accepted, like <C-?>.
1397 // <C-"> or <M-"> are not special in strings as " is
1398 // the string delimiter. With a backslash it works: <M-\">
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001399 if (!(in_string && bp[1] == '"') && bp[l + 1] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02001400 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001401 else if (in_string && bp[1] == '\\' && bp[2] == '"'
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001402 && bp[3] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001403 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 }
1406 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001407 bp += 3; // skip t_xx, xx may be '-' or '>'
Bram Moolenaar792826c2011-08-19 22:29:02 +02001408 else if (STRNICMP(bp, "char-", 5) == 0)
1409 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001410 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0, TRUE);
1411 if (l == 0)
1412 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001413 emsg(_(e_invalid_argument));
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001414 return 0;
1415 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02001416 bp += l + 5;
1417 break;
1418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
1420
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001421 if (*bp == '>') // found matching '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 {
1423 end_of_name = bp + 1;
1424
Bram Moolenaar85a20022019-12-21 18:25:54 +01001425 // Which modifiers are given?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 modifiers = 0x0;
1427 for (bp = src + 1; bp < last_dash; bp++)
1428 {
1429 if (*bp != '-')
1430 {
1431 bit = name_to_mod_mask(*bp);
1432 if (bit == 0x0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001433 break; // Illegal modifier name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 modifiers |= bit;
1435 }
1436 }
1437
1438 /*
1439 * Legal modifier name.
1440 */
1441 if (bp >= last_dash)
1442 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001443 if (STRNICMP(last_dash + 1, "char-", 5) == 0
1444 && VIM_ISDIGIT(last_dash[6]))
1445 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001446 // <Char-123> or <Char-033> or <Char-0x33>
Bram Moolenaar459fd782019-10-13 16:43:39 +02001447 vim_str2nr(last_dash + 6, NULL, &l, STR2NR_ALL, NULL,
1448 &n, 0, TRUE);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001449 if (l == 0)
1450 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001451 emsg(_(e_invalid_argument));
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001452 return 0;
1453 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02001454 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001457 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001458 int off = 1;
1459
Bram Moolenaar85a20022019-12-21 18:25:54 +01001460 // Modifier with single letter, or special key name.
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001461 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
1462 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02001463 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001464 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02001465 else
Bram Moolenaar792826c2011-08-19 22:29:02 +02001466 l = 1;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001467 if (modifiers != 0 && last_dash[l + off] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001468 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02001469 else
1470 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001471 key = get_special_key_code(last_dash + off);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001472 if (!(flags & FSK_KEEP_X_KEY))
Bram Moolenaar792826c2011-08-19 22:29:02 +02001473 key = handle_x_keys(key);
1474 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
1477 /*
1478 * get_special_key_code() may return NUL for invalid
1479 * special key name.
1480 */
1481 if (key != NUL)
1482 {
1483 /*
1484 * Only use a modifier when there is no special key code that
1485 * includes the modifier.
1486 */
1487 key = simplify_key(key, &modifiers);
1488
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001489 if (!(flags & FSK_KEYCODE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001491 // don't want keycode, use single byte code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 if (key == K_BS)
1493 key = BS;
1494 else if (key == K_DEL || key == K_KDEL)
1495 key = DEL;
1496 }
1497
Bram Moolenaar459fd782019-10-13 16:43:39 +02001498 // Normal Key with modifier: Try to make a single byte code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 if (!IS_SPECIAL(key))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001500 key = extract_modifiers(key, &modifiers,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001501 flags & FSK_SIMPLIFY, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
1503 *modp = modifiers;
1504 *srcp = end_of_name;
1505 return key;
1506 }
1507 }
1508 }
1509 return 0;
1510}
1511
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001512
1513/*
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02001514 * Some keys are used with Ctrl without Shift and are still expected to be
1515 * mapped as if Shift was pressed:
1516 * CTRL-2 is CTRL-@
1517 * CTRL-6 is CTRL-^
1518 * CTRL-- is CTRL-_
1519 * Also, <C-H> and <C-h> mean the same thing, always use "H".
1520 * Returns the possibly adjusted key.
1521 */
1522 int
1523may_adjust_key_for_ctrl(int modifiers, int key)
1524{
1525 if (modifiers & MOD_MASK_CTRL)
1526 {
1527 if (ASCII_ISALPHA(key))
1528 return TOUPPER_ASC(key);
1529 if (key == '2')
1530 return '@';
1531 if (key == '6')
1532 return '^';
1533 if (key == '-')
1534 return '_';
1535 }
1536 return key;
1537}
1538
1539/*
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001540 * Some keys already have Shift included, pass them as normal keys.
Bram Moolenaar9a033d72020-10-07 17:29:48 +02001541 * When Ctrl is also used <C-H> and <C-S-H> are different, but <C-S-{> should
1542 * be <C-{>. Same for <C-S-}> and <C-S-|>.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001543 * Also for <A-S-a> and <M-S-a>.
Bram Moolenaar83a19c52022-09-12 20:35:28 +01001544 * This includes all printable ASCII characters except a-z.
1545 * Digits are included because with AZERTY the Shift key is used to get them.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001546 */
1547 int
1548may_remove_shift_modifier(int modifiers, int key)
1549{
1550 if ((modifiers == MOD_MASK_SHIFT
1551 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
1552 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02001553 && ((key >= '!' && key <= '/')
1554 || (key >= ':' && key <= 'Z')
Bram Moolenaar83a19c52022-09-12 20:35:28 +01001555 || vim_isdigit(key)
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02001556 || (key >= '[' && key <= '`')
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001557 || (key >= '{' && key <= '~')))
1558 return modifiers & ~MOD_MASK_SHIFT;
Bram Moolenaar9a033d72020-10-07 17:29:48 +02001559
1560 if (modifiers == (MOD_MASK_SHIFT | MOD_MASK_CTRL)
1561 && (key == '{' || key == '}' || key == '|'))
1562 return modifiers & ~MOD_MASK_SHIFT;
1563
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001564 return modifiers;
1565}
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567/*
1568 * Try to include modifiers in the key.
1569 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
Bram Moolenaar459fd782019-10-13 16:43:39 +02001570 * When "simplify" is FALSE don't do Ctrl and Alt.
1571 * When "simplify" is TRUE and Ctrl or Alt is removed from modifiers set
1572 * "did_simplify" when it's not NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 */
1574 int
Bram Moolenaar459fd782019-10-13 16:43:39 +02001575extract_modifiers(int key, int *modp, int simplify, int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576{
1577 int modifiers = *modp;
1578
Bram Moolenaard0573012017-10-28 21:11:06 +02001579#ifdef MACOS_X
Bram Moolenaar459fd782019-10-13 16:43:39 +02001580 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 if (!(modifiers & MOD_MASK_CMD))
1582#endif
1583 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
1584 {
1585 key = TOUPPER_ASC(key);
Bram Moolenaar20298ce2020-06-19 21:46:52 +02001586 // With <C-S-a> we keep the shift modifier.
1587 // With <S-a>, <A-S-a> and <S-A> we don't keep the shift modifier.
1588 if (simplify || modifiers == MOD_MASK_SHIFT
1589 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
1590 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001591 modifiers &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02001593
1594 // <C-H> and <C-h> mean the same thing, always use "H"
1595 if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key))
1596 key = TOUPPER_ASC(key);
1597
1598 if (simplify && (modifiers & MOD_MASK_CTRL)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001599 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {
1601 key = Ctrl_chr(key);
1602 modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001603 // <C-@> is <Nul>
zeertzjq17c95d92022-04-26 12:51:07 +01001604 if (key == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 key = K_ZERO;
Bram Moolenaar459fd782019-10-13 16:43:39 +02001606 if (did_simplify != NULL)
1607 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02001609
Bram Moolenaard0573012017-10-28 21:11:06 +02001610#ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01001611 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (!(modifiers & MOD_MASK_CMD))
1613#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +02001614 if (simplify && (modifiers & MOD_MASK_ALT) && key < 0x80
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001615 && !enc_dbcs) // avoid creating a lead byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
1617 key |= 0x80;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001618 modifiers &= ~MOD_MASK_ALT; // remove the META modifier
Bram Moolenaar459fd782019-10-13 16:43:39 +02001619 if (did_simplify != NULL)
1620 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 }
1622
1623 *modp = modifiers;
1624 return key;
1625}
1626
1627/*
1628 * Try to find key "c" in the special key table.
1629 * Return the index when found, -1 when not found.
1630 */
1631 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001632find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
1634 int i;
1635
1636 for (i = 0; key_names_table[i].name != NULL; i++)
1637 if (c == key_names_table[i].key)
1638 break;
1639 if (key_names_table[i].name == NULL)
1640 i = -1;
1641 return i;
1642}
1643
1644/*
1645 * Find the special key with the given name (the given string does not have to
1646 * end with NUL, the name is assumed to end before the first non-idchar).
1647 * If the name starts with "t_" the next two characters are interpreted as a
1648 * termcap name.
1649 * Return the key code, or 0 if not found.
1650 */
1651 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001652get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
1654 char_u *table_name;
1655 char_u string[3];
1656 int i, j;
1657
1658 /*
1659 * If it's <t_xx> we get the code for xx from the termcap
1660 */
1661 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
1662 {
1663 string[0] = name[2];
1664 string[1] = name[3];
1665 string[2] = NUL;
1666 if (add_termcap_entry(string, FALSE) == OK)
1667 return TERMCAP2KEY(name[2], name[3]);
1668 }
1669 else
1670 for (i = 0; key_names_table[i].name != NULL; i++)
1671 {
1672 table_name = key_names_table[i].name;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02001673 for (j = 0; vim_isNormalIDc(name[j]) && table_name[j] != NUL; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
1675 break;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02001676 if (!vim_isNormalIDc(name[j]) && table_name[j] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 return key_names_table[i].key;
1678 }
1679 return 0;
1680}
1681
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001683get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00001685 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 return NULL;
1687 return key_names_table[i].name;
1688}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690/*
1691 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
1692 */
1693 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001694get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695{
1696 int c = *buf->b_p_ff;
1697
1698 if (buf->b_p_bin || c == 'u')
1699 return EOL_UNIX;
1700 if (c == 'm')
1701 return EOL_MAC;
1702 return EOL_DOS;
1703}
1704
1705/*
1706 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
1707 * argument.
1708 */
1709 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001710get_fileformat_force(
1711 buf_T *buf,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001712 exarg_T *eap) // can be NULL!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 int c;
1715
1716 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001717 c = eap->force_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 else
1719 {
1720 if ((eap != NULL && eap->force_bin != 0)
1721 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
1722 return EOL_UNIX;
1723 c = *buf->b_p_ff;
1724 }
1725 if (c == 'u')
1726 return EOL_UNIX;
1727 if (c == 'm')
1728 return EOL_MAC;
1729 return EOL_DOS;
1730}
1731
1732/*
1733 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
1734 * Sets both 'textmode' and 'fileformat'.
1735 * Note: Does _not_ set global value of 'textmode'!
1736 */
1737 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001738set_fileformat(
1739 int t,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001740 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
1742 char *p = NULL;
1743
1744 switch (t)
1745 {
1746 case EOL_DOS:
1747 p = FF_DOS;
1748 curbuf->b_p_tx = TRUE;
1749 break;
1750 case EOL_UNIX:
1751 p = FF_UNIX;
1752 curbuf->b_p_tx = FALSE;
1753 break;
1754 case EOL_MAC:
1755 p = FF_MAC;
1756 curbuf->b_p_tx = FALSE;
1757 break;
1758 }
1759 if (p != NULL)
1760 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001761 OPT_FREE | opt_flags, 0);
1762
Bram Moolenaar85a20022019-12-21 18:25:54 +01001763 // This may cause the buffer to become (un)modified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001765 redraw_tabline = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001766 need_maketitle = TRUE; // set window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767}
1768
1769/*
1770 * Return the default fileformat from 'fileformats'.
1771 */
1772 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001773default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 switch (*p_ffs)
1776 {
1777 case 'm': return EOL_MAC;
1778 case 'd': return EOL_DOS;
1779 }
1780 return EOL_UNIX;
1781}
1782
1783/*
1784 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
1785 */
1786 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001787call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788{
1789 char_u *ncmd;
1790 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001791#ifdef FEAT_PROFILE
1792 proftime_T wait_time;
1793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795 if (p_verbose > 3)
1796 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001797 verbose_enter();
Bram Moolenaar06f08532020-05-12 23:45:16 +02001798 smsg(_("Calling shell to execute: \"%s\""), cmd == NULL ? p_sh : cmd);
Bram Moolenaar11901392022-09-26 19:50:44 +01001799 msg_putchar_attr('\n', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001801 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
1803
Bram Moolenaar05159a02005-02-26 23:04:13 +00001804#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00001805 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001806 prof_child_enter(&wait_time);
1807#endif
1808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (*p_sh == NUL)
1810 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001811 emsg(_(e_shell_option_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 retval = -1;
1813 }
1814 else
1815 {
1816#ifdef FEAT_GUI_MSWIN
Bram Moolenaar85a20022019-12-21 18:25:54 +01001817 // Don't hide the pointer while executing a shell command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 gui_mch_mousehide(FALSE);
1819#endif
1820#ifdef FEAT_GUI
1821 ++hold_gui_events;
1822#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001823 // The external command may update a tags file, clear cached tags.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 tag_freematch();
1825
Bram Moolenaar01257a72019-06-10 14:46:04 +02001826 if (cmd == NULL || *p_sxq == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 retval = mch_call_shell(cmd, opt);
1828 else
1829 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001830 char_u *ecmd = cmd;
1831
Bram Moolenaar1a613392019-09-28 15:51:37 +02001832 if (*p_sxe != NUL && *p_sxq == '(')
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001833 {
1834 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
1835 if (ecmd == NULL)
1836 ecmd = cmd;
1837 }
Bram Moolenaar964b3742019-05-24 18:54:09 +02001838 ncmd = alloc(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if (ncmd != NULL)
1840 {
1841 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001842 STRCAT(ncmd, ecmd);
Bram Moolenaar1a613392019-09-28 15:51:37 +02001843 // When 'shellxquote' is ( append ).
1844 // When 'shellxquote' is "( append )".
1845 STRCAT(ncmd, *p_sxq == '(' ? (char_u *)")"
1846 : *p_sxq == '"' && *(p_sxq+1) == '(' ? (char_u *)")\""
1847 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 retval = mch_call_shell(ncmd, opt);
1849 vim_free(ncmd);
1850 }
1851 else
1852 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001853 if (ecmd != cmd)
1854 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 }
1856#ifdef FEAT_GUI
1857 --hold_gui_events;
1858#endif
1859 /*
1860 * Check the window size, in case it changed while executing the
1861 * external command.
1862 */
1863 shell_resized_check();
1864 }
1865
1866#ifdef FEAT_EVAL
1867 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001868# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00001869 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001870 prof_child_exit(&wait_time);
1871# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872#endif
1873
1874 return retval;
1875}
1876
1877/*
Bram Moolenaar24959102022-05-07 20:01:16 +01001878 * MODE_VISUAL, MODE_SELECT and MODE_OP_PENDING State are never set, they are
1879 * equal to MODE_NORMAL State with a condition. This function returns the real
1880 * State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 */
1882 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001883get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884{
Bram Moolenaar24959102022-05-07 20:01:16 +01001885 if (State & MODE_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00001888 {
1889 if (VIsual_select)
Bram Moolenaar24959102022-05-07 20:01:16 +01001890 return MODE_SELECT;
1891 return MODE_VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00001892 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001893 else if (finish_op)
Bram Moolenaar24959102022-05-07 20:01:16 +01001894 return MODE_OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
1896 return State;
1897}
1898
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001899/*
1900 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02001901 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001902 * "b" must point to the start of the file name
1903 */
1904 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001905after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001906{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02001907 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001908 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
1909}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001910
1911/*
1912 * Return TRUE if file names "f1" and "f2" are in the same directory.
1913 * "f1" may be a short name, "f2" must be a full path.
1914 */
1915 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001916same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001917{
1918 char_u ffname[MAXPATHL];
1919 char_u *t1;
1920 char_u *t2;
1921
Bram Moolenaar85a20022019-12-21 18:25:54 +01001922 // safety check
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001923 if (f1 == NULL || f2 == NULL)
1924 return FALSE;
1925
1926 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
1927 t1 = gettail_sep(ffname);
1928 t2 = gettail_sep(f2);
1929 return (t1 - ffname == t2 - f2
1930 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
1931}
1932
Bram Moolenaar7c365fb2018-06-29 20:28:31 +02001933#if defined(FEAT_SESSION) || defined(FEAT_AUTOCHDIR) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02001934 || defined(MSWIN) || defined(FEAT_GUI_GTK) \
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001935 || defined(FEAT_NETBEANS_INTG) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 || defined(PROTO)
1937/*
1938 * Change to a file's directory.
1939 * Caller must call shorten_fnames()!
1940 * Return OK or FAIL.
1941 */
1942 int
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001943vim_chdirfile(char_u *fname, char *trigger_autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001945 char_u old_dir[MAXPATHL];
1946 char_u new_dir[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001948 if (mch_dirname(old_dir, MAXPATHL) != OK)
1949 *old_dir = NUL;
1950
1951 vim_strncpy(new_dir, fname, MAXPATHL - 1);
1952 *gettail_sep(new_dir) = NUL;
1953
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01001954 if (pathcmp((char *)old_dir, (char *)new_dir, -1) == 0)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001955 // nothing to do
zeertzjq73257142022-02-02 13:16:37 +00001956 return OK;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001957
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001958 if (trigger_autocmd != NULL)
1959 trigger_DirChangedPre((char_u *)trigger_autocmd, new_dir);
1960
zeertzjq73257142022-02-02 13:16:37 +00001961 if (mch_chdir((char *)new_dir) != 0)
1962 return FAIL;
1963
1964 if (trigger_autocmd != NULL)
1965 apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd,
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001966 new_dir, FALSE, curbuf);
zeertzjq73257142022-02-02 13:16:37 +00001967 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968}
1969#endif
1970
1971#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
1972/*
1973 * Check if "name" ends in a slash and is not a directory.
1974 * Used for systems where stat() ignores a trailing slash on a file name.
1975 * The Vim code assumes a trailing slash is only ignored for a directory.
1976 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001977 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01001978illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979{
1980 if (name[0] == NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001981 return FALSE; // no file name is not illegal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 if (name[strlen(name) - 1] != '/')
Bram Moolenaar85a20022019-12-21 18:25:54 +01001983 return FALSE; // no trailing slash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (mch_isdir((char_u *)name))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001985 return FALSE; // trailing slash for a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 return TRUE;
1987}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001988
1989/*
1990 * Special implementation of mch_stat() for Solaris.
1991 */
1992 int
1993vim_stat(const char *name, stat_T *stp)
1994{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001995 // On Solaris stat() accepts "file/" as if it was "file". Return -1 if
1996 // the name ends in "/" and it's not a directory.
Bram Moolenaard8492792017-03-16 12:22:38 +01001997 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001998}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999#endif
2000
2001#if defined(CURSOR_SHAPE) || defined(PROTO)
2002
2003/*
2004 * Handling of cursor and mouse pointer shapes in various modes.
2005 */
2006
2007cursorentry_T shape_table[SHAPE_IDX_COUNT] =
2008{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002009 // The values will be filled in from the 'guicursor' and 'mouseshape'
2010 // defaults when Vim starts.
2011 // Adjust the SHAPE_IDX_ defines when making changes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
2013 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
2014 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
2015 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
2016 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
2017 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
2018 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
2019 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
2020 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
2021 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
2022 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
2023 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
2024 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
2025 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
2026 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
2027 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
2028 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
2029};
2030
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002031# ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032/*
2033 * Table with names for mouse shapes. Keep in sync with all the tables for
2034 * mch_set_mouse_shape()!.
2035 */
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002036static char *mshape_names[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002038 "arrow", // default, must be the first one
2039 "blank", // hidden
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 "beam",
2041 "updown",
2042 "udsizing",
2043 "leftright",
2044 "lrsizing",
2045 "busy",
2046 "no",
2047 "crosshair",
2048 "hand1",
2049 "hand2",
2050 "pencil",
2051 "question",
2052 "rightup-arrow",
2053 "up-arrow",
2054 NULL
2055};
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002056
2057# define MSHAPE_NAMES_COUNT (ARRAY_LENGTH(mshape_names) - 1)
2058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059
2060/*
2061 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
2062 * ("what" is SHAPE_MOUSE).
2063 * Returns error message for an illegal option, NULL otherwise.
2064 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002065 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002066parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
2068 char_u *modep;
2069 char_u *colonp;
2070 char_u *commap;
2071 char_u *slashp;
2072 char_u *p, *endp;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002073 int idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 int all_idx;
2075 int len;
2076 int i;
2077 long n;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002078 int found_ve = FALSE; // found "ve" flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 int round;
2080
2081 /*
2082 * First round: check for errors; second round: do it for real.
2083 */
2084 for (round = 1; round <= 2; ++round)
2085 {
2086 /*
2087 * Repeat for all comma separated parts.
2088 */
2089#ifdef FEAT_MOUSESHAPE
2090 if (what == SHAPE_MOUSE)
2091 modep = p_mouseshape;
2092 else
2093#endif
2094 modep = p_guicursor;
2095 while (*modep != NUL)
2096 {
2097 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01002098 commap = vim_strchr(modep, ',');
2099
2100 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002101 return e_missing_colon_2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 if (colonp == modep)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002103 return e_illegal_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105 /*
2106 * Repeat for all mode's before the colon.
2107 * For the 'a' mode, we loop to handle all the modes.
2108 */
2109 all_idx = -1;
2110 while (modep < colonp || all_idx >= 0)
2111 {
2112 if (all_idx < 0)
2113 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002114 // Find the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 if (modep[1] == '-' || modep[1] == ':')
2116 len = 1;
2117 else
2118 len = 2;
2119 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
2120 all_idx = SHAPE_IDX_COUNT - 1;
2121 else
2122 {
2123 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
2124 if (STRNICMP(modep, shape_table[idx].name, len)
2125 == 0)
2126 break;
2127 if (idx == SHAPE_IDX_COUNT
2128 || (shape_table[idx].used_for & what) == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002129 return e_illegal_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
2131 found_ve = TRUE;
2132 }
2133 modep += len + 1;
2134 }
2135
2136 if (all_idx >= 0)
2137 idx = all_idx--;
2138 else if (round == 2)
2139 {
2140#ifdef FEAT_MOUSESHAPE
2141 if (what == SHAPE_MOUSE)
2142 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002143 // Set the default, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 shape_table[idx].mshape = 0;
2145 }
2146 else
2147#endif
2148 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002149 // Set the defaults, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 shape_table[idx].shape = SHAPE_BLOCK;
2151 shape_table[idx].blinkwait = 700L;
2152 shape_table[idx].blinkon = 400L;
2153 shape_table[idx].blinkoff = 250L;
2154 }
2155 }
2156
Bram Moolenaar85a20022019-12-21 18:25:54 +01002157 // Parse the part after the colon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 for (p = colonp + 1; *p && *p != ','; )
2159 {
2160#ifdef FEAT_MOUSESHAPE
2161 if (what == SHAPE_MOUSE)
2162 {
2163 for (i = 0; ; ++i)
2164 {
2165 if (mshape_names[i] == NULL)
2166 {
2167 if (!VIM_ISDIGIT(*p))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002168 return e_illegal_mouseshape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 if (round == 2)
2170 shape_table[idx].mshape =
2171 getdigits(&p) + MSHAPE_NUMBERED;
2172 else
2173 (void)getdigits(&p);
2174 break;
2175 }
2176 len = (int)STRLEN(mshape_names[i]);
2177 if (STRNICMP(p, mshape_names[i], len) == 0)
2178 {
2179 if (round == 2)
2180 shape_table[idx].mshape = i;
2181 p += len;
2182 break;
2183 }
2184 }
2185 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002186 else // if (what == SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187#endif
2188 {
2189 /*
2190 * First handle the ones with a number argument.
2191 */
2192 i = *p;
2193 len = 0;
2194 if (STRNICMP(p, "ver", 3) == 0)
2195 len = 3;
2196 else if (STRNICMP(p, "hor", 3) == 0)
2197 len = 3;
2198 else if (STRNICMP(p, "blinkwait", 9) == 0)
2199 len = 9;
2200 else if (STRNICMP(p, "blinkon", 7) == 0)
2201 len = 7;
2202 else if (STRNICMP(p, "blinkoff", 8) == 0)
2203 len = 8;
2204 if (len != 0)
2205 {
2206 p += len;
2207 if (!VIM_ISDIGIT(*p))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002208 return e_digit_expected;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 n = getdigits(&p);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002210 if (len == 3) // "ver" or "hor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
2212 if (n == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002213 return e_illegal_percentage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (round == 2)
2215 {
2216 if (TOLOWER_ASC(i) == 'v')
2217 shape_table[idx].shape = SHAPE_VER;
2218 else
2219 shape_table[idx].shape = SHAPE_HOR;
2220 shape_table[idx].percentage = n;
2221 }
2222 }
2223 else if (round == 2)
2224 {
2225 if (len == 9)
2226 shape_table[idx].blinkwait = n;
2227 else if (len == 7)
2228 shape_table[idx].blinkon = n;
2229 else
2230 shape_table[idx].blinkoff = n;
2231 }
2232 }
2233 else if (STRNICMP(p, "block", 5) == 0)
2234 {
2235 if (round == 2)
2236 shape_table[idx].shape = SHAPE_BLOCK;
2237 p += 5;
2238 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002239 else // must be a highlight group name then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
2241 endp = vim_strchr(p, '-');
Bram Moolenaar85a20022019-12-21 18:25:54 +01002242 if (commap == NULL) // last part
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
2244 if (endp == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002245 endp = p + STRLEN(p); // find end of part
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247 else if (endp > commap || endp == NULL)
2248 endp = commap;
2249 slashp = vim_strchr(p, '/');
2250 if (slashp != NULL && slashp < endp)
2251 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002252 // "group/langmap_group"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 i = syn_check_group(p, (int)(slashp - p));
2254 p = slashp + 1;
2255 }
2256 if (round == 2)
2257 {
2258 shape_table[idx].id = syn_check_group(p,
2259 (int)(endp - p));
2260 shape_table[idx].id_lm = shape_table[idx].id;
2261 if (slashp != NULL && slashp < endp)
2262 shape_table[idx].id = i;
2263 }
2264 p = endp;
2265 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002266 } // if (what != SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267
2268 if (*p == '-')
2269 ++p;
2270 }
2271 }
2272 modep = p;
2273 if (*modep == ',')
2274 ++modep;
2275 }
2276 }
2277
Bram Moolenaar85a20022019-12-21 18:25:54 +01002278 // If the 's' flag is not given, use the 'v' cursor for 's'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 if (!found_ve)
2280 {
2281#ifdef FEAT_MOUSESHAPE
2282 if (what == SHAPE_MOUSE)
2283 {
2284 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
2285 }
2286 else
2287#endif
2288 {
2289 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
2290 shape_table[SHAPE_IDX_VE].percentage =
2291 shape_table[SHAPE_IDX_V].percentage;
2292 shape_table[SHAPE_IDX_VE].blinkwait =
2293 shape_table[SHAPE_IDX_V].blinkwait;
2294 shape_table[SHAPE_IDX_VE].blinkon =
2295 shape_table[SHAPE_IDX_V].blinkon;
2296 shape_table[SHAPE_IDX_VE].blinkoff =
2297 shape_table[SHAPE_IDX_V].blinkoff;
2298 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
2299 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
2300 }
2301 }
2302
2303 return NULL;
2304}
2305
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002306# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2307 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308/*
2309 * Return the index into shape_table[] for the current mode.
2310 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
2311 */
2312 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002313get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314{
2315#ifdef FEAT_MOUSESHAPE
Bram Moolenaar24959102022-05-07 20:01:16 +01002316 if (mouse && (State == MODE_HITRETURN || State == MODE_ASKMORE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
2318# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002319 int x, y;
2320 gui_mch_getmouse(&x, &y);
2321 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 return SHAPE_IDX_MOREL;
2323# endif
2324 return SHAPE_IDX_MORE;
2325 }
2326 if (mouse && drag_status_line)
2327 return SHAPE_IDX_SDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 if (mouse && drag_sep_line)
2329 return SHAPE_IDX_VDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002331 if (!mouse && State == MODE_SHOWMATCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 return SHAPE_IDX_SM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 if (State & VREPLACE_FLAG)
2334 return SHAPE_IDX_R;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 if (State & REPLACE_FLAG)
2336 return SHAPE_IDX_R;
Bram Moolenaar24959102022-05-07 20:01:16 +01002337 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 return SHAPE_IDX_I;
Bram Moolenaar24959102022-05-07 20:01:16 +01002339 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
2341 if (cmdline_at_end())
2342 return SHAPE_IDX_C;
2343 if (cmdline_overstrike())
2344 return SHAPE_IDX_CR;
2345 return SHAPE_IDX_CI;
2346 }
2347 if (finish_op)
2348 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 if (VIsual_active)
2350 {
2351 if (*p_sel == 'e')
2352 return SHAPE_IDX_VE;
2353 else
2354 return SHAPE_IDX_V;
2355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 return SHAPE_IDX_N;
2357}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002361static int current_mouse_shape = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
2363/*
2364 * Set the mouse shape:
2365 * If "shape" is -1, use shape depending on the current mode,
2366 * depending on the current state.
2367 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
2368 * when the mouse moves off the status or command line).
2369 */
2370 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002371update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 int new_mouse_shape;
2374
Bram Moolenaar85a20022019-12-21 18:25:54 +01002375 // Only works in GUI mode.
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002376 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 return;
2378
Bram Moolenaar85a20022019-12-21 18:25:54 +01002379 // Postpone the updating when more is to come. Speeds up executing of
2380 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 if (shape_idx == -1 && char_avail())
2382 {
2383 postponed_mouseshape = TRUE;
2384 return;
2385 }
2386
Bram Moolenaar85a20022019-12-21 18:25:54 +01002387 // When ignoring the mouse don't change shape on the statusline.
Bram Moolenaar14716812006-05-04 21:54:08 +00002388 if (*p_mouse == NUL
2389 && (shape_idx == SHAPE_IDX_CLINE
2390 || shape_idx == SHAPE_IDX_STATUS
2391 || shape_idx == SHAPE_IDX_VSEP))
2392 shape_idx = -2;
2393
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (shape_idx == -2
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002395 && current_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
2396 && current_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
2397 && current_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 return;
2399 if (shape_idx < 0)
2400 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
2401 else
2402 new_mouse_shape = shape_table[shape_idx].mshape;
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002403 if (new_mouse_shape != current_mouse_shape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
2405 mch_set_mouse_shape(new_mouse_shape);
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002406 current_mouse_shape = new_mouse_shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 }
2408 postponed_mouseshape = FALSE;
2409}
2410# endif
2411
Bram Moolenaar85a20022019-12-21 18:25:54 +01002412#endif // CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002414#if defined(FEAT_EVAL) || defined(PROTO)
2415/*
2416 * Mainly for tests: get the name of the current mouse shape.
2417 */
2418 void
2419f_getmouseshape(typval_T *argvars UNUSED, typval_T *rettv)
2420{
2421 rettv->v_type = VAR_STRING;
2422 rettv->vval.v_string = NULL;
2423# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
2424 if (current_mouse_shape >= 0
2425 && current_mouse_shape < (int)MSHAPE_NAMES_COUNT)
2426 rettv->vval.v_string = vim_strsave(
2427 (char_u *)mshape_names[current_mouse_shape]);
2428# endif
2429}
2430#endif
2431
2432
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434/*
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01002435 * Change directory to "new_dir". Search 'cdpath' for relative directory
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01002436 * names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 */
2438 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002439vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 char_u *dir_name;
2442 int r;
2443
2444 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
2445 FNAME_MESS, curbuf->b_ffname);
2446 if (dir_name == NULL)
2447 return -1;
2448 r = mch_chdir((char *)dir_name);
2449 vim_free(dir_name);
2450 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451}
2452
2453/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002454 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002456 * Some systems are quite slow in obtaining the user name (Windows NT), thus
2457 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 * Returns OK or FAIL.
2459 */
2460 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002461get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002463 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 {
2465 if (mch_get_user_name(buf, len) == FAIL)
2466 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002467 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
2469 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002470 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 return OK;
2472}
2473
Bram Moolenaar7ce5b2b2022-05-16 19:40:59 +01002474#if defined(EXITFREE) || defined(PROTO)
Yegappan Lakshmanancbae5802021-08-06 21:51:55 +02002475/*
2476 * Free the memory allocated by get_user_name()
2477 */
2478 void
2479free_username(void)
2480{
2481 vim_free(username);
2482}
Dominique Pelle748b3082022-01-08 12:41:16 +00002483#endif
Yegappan Lakshmanancbae5802021-08-06 21:51:55 +02002484
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485#ifndef HAVE_QSORT
2486/*
2487 * Our own qsort(), for systems that don't have it.
2488 * It's simple and slow. From the K&R C book.
2489 */
2490 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002491qsort(
2492 void *base,
2493 size_t elm_count,
2494 size_t elm_size,
2495 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 char_u *buf;
2498 char_u *p1;
2499 char_u *p2;
2500 int i, j;
2501 int gap;
2502
Bram Moolenaar964b3742019-05-24 18:54:09 +02002503 buf = alloc(elm_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 if (buf == NULL)
2505 return;
2506
2507 for (gap = elm_count / 2; gap > 0; gap /= 2)
2508 for (i = gap; i < elm_count; ++i)
2509 for (j = i - gap; j >= 0; j -= gap)
2510 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002511 // Compare the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 p1 = (char_u *)base + j * elm_size;
2513 p2 = (char_u *)base + (j + gap) * elm_size;
2514 if ((*cmp)((void *)p1, (void *)p2) <= 0)
2515 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002516 // Exchange the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 mch_memmove(buf, p1, elm_size);
2518 mch_memmove(p1, p2, elm_size);
2519 mch_memmove(p2, buf, elm_size);
2520 }
2521
2522 vim_free(buf);
2523}
2524#endif
2525
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 * The putenv() implementation below comes from the "screen" program.
2528 * Included with permission from Juergen Weigert.
2529 * See pty.c for the copyright notice.
2530 */
2531
2532/*
2533 * putenv -- put value into environment
2534 *
2535 * Usage: i = putenv (string)
2536 * int i;
2537 * char *string;
2538 *
2539 * where string is of the form <name>=<value>.
2540 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
2541 *
2542 * Putenv may need to add a new name into the environment, or to
2543 * associate a value longer than the current value with a particular
2544 * name. So, to make life simpler, putenv() copies your entire
2545 * environment into the heap (i.e. malloc()) from the stack
2546 * (i.e. where it resides when your process is initiated) the first
2547 * time you call it.
2548 *
2549 * (history removed, not very interesting. See the "screen" sources.)
2550 */
2551
2552#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
2553
Bram Moolenaar85a20022019-12-21 18:25:54 +01002554#define EXTRASIZE 5 // increment to add to env. size
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
Bram Moolenaar85a20022019-12-21 18:25:54 +01002556static int envsize = -1; // current size of environment
2557extern char **environ; // the global which is your env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaar85a20022019-12-21 18:25:54 +01002559static int findenv(char *name); // look for a name in the env.
2560static int newenv(void); // copy env. from stack to heap
2561static int moreenv(void); // incr. size of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
2563 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002564putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565{
2566 int i;
2567 char *p;
2568
2569 if (envsize < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002570 { // first time putenv called
2571 if (newenv() < 0) // copy env. to heap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 return -1;
2573 }
2574
Bram Moolenaar85a20022019-12-21 18:25:54 +01002575 i = findenv((char *)string); // look for name in environment
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576
2577 if (i < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002578 { // name must be added
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 for (i = 0; environ[i]; i++);
2580 if (i >= (envsize - 1))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002581 { // need new slot
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (moreenv() < 0)
2583 return -1;
2584 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002585 p = alloc(strlen(string) + 1);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002586 if (p == NULL) // not enough core
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 return -1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002588 environ[i + 1] = 0; // new end of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
2590 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002591 { // name already in env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 p = vim_realloc(environ[i], strlen(string) + 1);
2593 if (p == NULL)
2594 return -1;
2595 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002596 sprintf(p, "%s", string); // copy into env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 environ[i] = p;
2598
2599 return 0;
2600}
2601
2602 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002603findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
2605 char *namechar, *envchar;
2606 int i, found;
2607
2608 found = 0;
2609 for (i = 0; environ[i] && !found; i++)
2610 {
2611 envchar = environ[i];
2612 namechar = name;
2613 while (*namechar && *namechar != '=' && (*namechar == *envchar))
2614 {
2615 namechar++;
2616 envchar++;
2617 }
2618 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
2619 }
2620 return found ? i - 1 : -1;
2621}
2622
2623 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002624newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
2626 char **env, *elem;
2627 int i, esize;
2628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 for (i = 0; environ[i]; i++)
2630 ;
Bram Moolenaard0573012017-10-28 21:11:06 +02002631
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 esize = i + EXTRASIZE + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002633 env = ALLOC_MULT(char *, esize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 if (env == NULL)
2635 return -1;
2636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 for (i = 0; environ[i]; i++)
2638 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002639 elem = alloc(strlen(environ[i]) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (elem == NULL)
2641 return -1;
2642 env[i] = elem;
2643 strcpy(elem, environ[i]);
2644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
2646 env[i] = 0;
2647 environ = env;
2648 envsize = esize;
2649 return 0;
2650}
2651
2652 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002653moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654{
2655 int esize;
2656 char **env;
2657
2658 esize = envsize + EXTRASIZE;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002659 env = vim_realloc((char *)environ, esize * sizeof (*env));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if (env == 0)
2661 return -1;
2662 environ = env;
2663 envsize = esize;
2664 return 0;
2665}
2666
2667# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02002668/*
2669 * Used for mch_getenv() for Mac.
2670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002672vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 int i;
2675 char_u *p;
2676
2677 if (envsize < 0)
2678 return NULL;
2679
2680 i = findenv((char *)string);
2681
2682 if (i < 0)
2683 return NULL;
2684
2685 p = vim_strchr((char_u *)environ[i], '=');
2686 return (p + 1);
2687}
2688# endif
2689
Bram Moolenaar85a20022019-12-21 18:25:54 +01002690#endif // !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002691
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002692#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002693/*
2694 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
2695 * rights to write into.
2696 */
2697 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002698filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002699{
2700 int retval = 0;
2701#if defined(UNIX) || defined(VMS)
2702 int perm = 0;
2703#endif
2704
2705#if defined(UNIX) || defined(VMS)
2706 perm = mch_getperm(fname);
2707#endif
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002708 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01002709# ifdef MSWIN
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002710 mch_writable(fname) &&
2711# else
2712# if defined(UNIX) || defined(VMS)
2713 (perm & 0222) &&
2714# endif
2715# endif
2716 mch_access((char *)fname, W_OK) == 0
2717 )
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002718 {
2719 ++retval;
2720 if (mch_isdir(fname))
2721 ++retval;
2722 }
2723 return retval;
2724}
2725#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00002726
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002727#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
2728/*
2729 * Read 2 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002730 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002731 */
2732 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002733get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002734{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002735 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002736
2737 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002738 if (n == EOF) return -1;
2739 c = getc(fd);
2740 if (c == EOF) return -1;
2741 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002742}
2743
2744/*
2745 * Read 3 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002746 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002747 */
2748 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002749get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002750{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002751 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002752
2753 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002754 if (n == EOF) return -1;
2755 c = getc(fd);
2756 if (c == EOF) return -1;
2757 n = (n << 8) + c;
2758 c = getc(fd);
2759 if (c == EOF) return -1;
2760 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002761}
2762
2763/*
2764 * Read 4 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002765 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002766 */
2767 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002768get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002769{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002770 int c;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002771 // Use unsigned rather than int otherwise result is undefined
2772 // when left-shift sets the MSB.
Bram Moolenaar95235e62013-09-08 16:07:07 +02002773 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002774
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002775 c = getc(fd);
2776 if (c == EOF) return -1;
2777 n = (unsigned)c;
2778 c = getc(fd);
2779 if (c == EOF) return -1;
2780 n = (n << 8) + (unsigned)c;
2781 c = getc(fd);
2782 if (c == EOF) return -1;
2783 n = (n << 8) + (unsigned)c;
2784 c = getc(fd);
2785 if (c == EOF) return -1;
2786 n = (n << 8) + (unsigned)c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02002787 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002788}
2789
2790/*
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002791 * Read a string of length "cnt" from "fd" into allocated memory.
2792 * Returns NULL when out of memory or unable to read that many bytes.
2793 */
2794 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002795read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002796{
2797 char_u *str;
2798 int i;
2799 int c;
2800
Bram Moolenaar85a20022019-12-21 18:25:54 +01002801 // allocate memory
Bram Moolenaar964b3742019-05-24 18:54:09 +02002802 str = alloc(cnt + 1);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002803 if (str != NULL)
2804 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002805 // Read the string. Quit when running into the EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002806 for (i = 0; i < cnt; ++i)
2807 {
2808 c = getc(fd);
2809 if (c == EOF)
2810 {
2811 vim_free(str);
2812 return NULL;
2813 }
2814 str[i] = c;
2815 }
2816 str[i] = NUL;
2817 }
2818 return str;
2819}
2820
2821/*
2822 * Write a number to file "fd", MSB first, in "len" bytes.
2823 */
2824 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002825put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002826{
2827 int i;
2828
2829 for (i = len - 1; i >= 0; --i)
2830 if (putc((int)(nr >> (i * 8)), fd) == EOF)
2831 return FAIL;
2832 return OK;
2833}
2834
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002835#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01002836
Bram Moolenaar85a20022019-12-21 18:25:54 +01002837#ifndef PROTO // proto is defined in vim.h
Bram Moolenaar51628222016-12-01 23:03:28 +01002838# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002839/*
2840 * Return time in msec since "start_tv".
2841 */
2842 long
2843elapsed(struct timeval *start_tv)
2844{
2845 struct timeval now_tv;
2846
2847 gettimeofday(&now_tv, NULL);
2848 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
2849 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
2850}
Bram Moolenaar51628222016-12-01 23:03:28 +01002851# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002852
Bram Moolenaar51628222016-12-01 23:03:28 +01002853# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002854/*
2855 * Return time in msec since "start_tick".
2856 */
2857 long
2858elapsed(DWORD start_tick)
2859{
2860 DWORD now = GetTickCount();
2861
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002862 return (long)now - (long)start_tick;
2863}
Bram Moolenaar51628222016-12-01 23:03:28 +01002864# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002865#endif
Bram Moolenaar20608922018-04-21 22:30:08 +02002866
2867#if defined(FEAT_JOB_CHANNEL) \
2868 || (defined(UNIX) && (!defined(USE_SYSTEM) \
2869 || (defined(FEAT_GUI) && defined(FEAT_TERMINAL)))) \
2870 || defined(PROTO)
2871/*
2872 * Parse "cmd" and put the white-separated parts in "argv".
2873 * "argv" is an allocated array with "argc" entries and room for 4 more.
2874 * Returns FAIL when out of memory.
2875 */
2876 int
2877mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
2878{
2879 int i;
2880 char_u *p, *d;
2881 int inquote;
2882
2883 /*
2884 * Do this loop twice:
2885 * 1: find number of arguments
2886 * 2: separate them and build argv[]
2887 */
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002888 for (i = 1; i <= 2; ++i)
Bram Moolenaar20608922018-04-21 22:30:08 +02002889 {
2890 p = skipwhite(cmd);
2891 inquote = FALSE;
2892 *argc = 0;
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002893 while (*p != NUL)
Bram Moolenaar20608922018-04-21 22:30:08 +02002894 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002895 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02002896 (*argv)[*argc] = (char *)p;
2897 ++*argc;
2898 d = p;
2899 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
2900 {
2901 if (p[0] == '"')
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02002902 // quotes surrounding an argument and are dropped
Bram Moolenaar20608922018-04-21 22:30:08 +02002903 inquote = !inquote;
2904 else
2905 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02002906 if (rem_backslash(p))
Bram Moolenaar20608922018-04-21 22:30:08 +02002907 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02002908 // First pass: skip over "\ " and "\"".
2909 // Second pass: Remove the backslash.
Bram Moolenaar20608922018-04-21 22:30:08 +02002910 ++p;
2911 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002912 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02002913 *d++ = *p;
2914 }
2915 ++p;
2916 }
2917 if (*p == NUL)
2918 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002919 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02002920 *d++ = NUL;
2921 break;
2922 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002923 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02002924 *d++ = NUL;
2925 p = skipwhite(p + 1);
2926 }
2927 if (*argv == NULL)
2928 {
2929 if (use_shcf)
2930 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002931 // Account for possible multiple args in p_shcf.
Bram Moolenaar20608922018-04-21 22:30:08 +02002932 p = p_shcf;
2933 for (;;)
2934 {
2935 p = skiptowhite(p);
2936 if (*p == NUL)
2937 break;
2938 ++*argc;
2939 p = skipwhite(p);
2940 }
2941 }
2942
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002943 *argv = ALLOC_MULT(char *, *argc + 4);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002944 if (*argv == NULL) // out of memory
Bram Moolenaar20608922018-04-21 22:30:08 +02002945 return FAIL;
2946 }
2947 }
2948 return OK;
2949}
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002950
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002951/*
2952 * Build "argv[argc]" from the string "cmd".
2953 * "argv[argc]" is set to NULL;
2954 * Return FAIL when out of memory.
2955 */
2956 int
2957build_argv_from_string(char_u *cmd, char ***argv, int *argc)
2958{
2959 char_u *cmd_copy;
2960 int i;
2961
Bram Moolenaar85a20022019-12-21 18:25:54 +01002962 // Make a copy, parsing will modify "cmd".
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002963 cmd_copy = vim_strsave(cmd);
2964 if (cmd_copy == NULL
2965 || mch_parse_cmd(cmd_copy, FALSE, argv, argc) == FAIL)
2966 {
2967 vim_free(cmd_copy);
2968 return FAIL;
2969 }
2970 for (i = 0; i < *argc; i++)
2971 (*argv)[i] = (char *)vim_strsave((char_u *)(*argv)[i]);
2972 (*argv)[*argc] = NULL;
2973 vim_free(cmd_copy);
2974 return OK;
2975}
2976
Dominique Pellee8741a72022-01-17 11:23:45 +00002977# if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002978/*
2979 * Build "argv[argc]" from the list "l".
2980 * "argv[argc]" is set to NULL;
2981 * Return FAIL when out of memory.
2982 */
2983 int
2984build_argv_from_list(list_T *l, char ***argv, int *argc)
2985{
2986 listitem_T *li;
2987 char_u *s;
2988
Bram Moolenaar85a20022019-12-21 18:25:54 +01002989 // Pass argv[] to mch_call_shell().
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002990 *argv = ALLOC_MULT(char *, l->lv_len + 1);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002991 if (*argv == NULL)
2992 return FAIL;
2993 *argc = 0;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002994 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002995 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002996 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002997 if (s == NULL)
2998 {
2999 int i;
3000
3001 for (i = 0; i < *argc; ++i)
Bram Moolenaardf195602020-04-13 18:13:33 +02003002 VIM_CLEAR((*argv)[i]);
Bram Moolenaar7c25a7c2021-10-05 19:19:35 +01003003 (*argv)[0] = NULL;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003004 return FAIL;
3005 }
3006 (*argv)[*argc] = (char *)vim_strsave(s);
3007 *argc += 1;
3008 }
3009 (*argv)[*argc] = NULL;
3010 return OK;
3011}
3012# endif
Bram Moolenaar20608922018-04-21 22:30:08 +02003013#endif
Bram Moolenaar57da6982019-09-13 22:30:11 +02003014
3015/*
3016 * Change the behavior of vterm.
3017 * 0: As usual.
3018 * 1: Windows 10 version 1809
3019 * The bug causes unstable handling of ambiguous width character.
Bram Moolenaar36e7a822019-11-13 21:49:24 +01003020 * 2: Windows 10 version 1903 & 1909
Bram Moolenaar57da6982019-09-13 22:30:11 +02003021 * Use the wrong result because each result is different.
3022 * 3: Windows 10 insider preview (current latest logic)
3023 */
3024 int
3025get_special_pty_type(void)
3026{
3027#ifdef MSWIN
3028 return get_conpty_type();
3029#else
3030 return 0;
3031#endif
3032}