blob: 79125771edbd7b64b9c4cad4c1f48e562f9dbf47 [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
Dominique Pellé0268ff32024-07-28 21:12:20 +020075getviscol2(colnr_T col, colnr_T coladd)
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;
zeertzjq94b7c322024-03-12 21:50:32 +0100133 int linelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 colnr_T col = 0;
135 int csize = 0;
136 int one_more;
137#ifdef FEAT_LINEBREAK
138 int head = 0;
139#endif
140
Bram Moolenaar24959102022-05-07 20:01:16 +0100141 one_more = (State & MODE_INSERT)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000142 || restart_edit != NUL
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000143 || (VIsual_active && *p_sel != 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +0200144 || ((get_ve_flags() & VE_ONEMORE) && wcol < MAXCOL);
Bram Moolenaara1381de2009-11-03 15:44:21 +0000145 line = ml_get_buf(curbuf, pos->lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +0100146 linelen = ml_get_buf_len(curbuf, pos->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
148 if (wcol >= MAXCOL)
149 {
zeertzjq94b7c322024-03-12 21:50:32 +0100150 idx = linelen - 1 + one_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 col = wcol;
152
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 if ((addspaces || finetune) && !VIsual_active)
154 {
zeertzjqd809c0a2023-08-27 11:17:39 +0200155 curwin->w_curswant = linetabsize(curwin, pos->lnum) + one_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 if (curwin->w_curswant > 0)
157 --curwin->w_curswant;
158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 }
160 else
161 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100162 int width = curwin->w_width - win_col_off(curwin);
163 chartabsize_T cts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
Bram Moolenaarebefac62005-12-28 22:39:57 +0000165 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 && curwin->w_p_wrap
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 && curwin->w_width != 0
Bram Moolenaar02f86942021-08-17 22:14:29 +0200168 && wcol >= (colnr_T)width
169 && width > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 {
zeertzjq2c47ab82025-02-13 20:34:34 +0100171 csize = linetabsize_eol(curwin, pos->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 if (csize > 0)
173 csize--;
174
Bram Moolenaarebefac62005-12-28 22:39:57 +0000175 if (wcol / width > (colnr_T)csize / width
Bram Moolenaar24959102022-05-07 20:01:16 +0100176 && ((State & MODE_INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100178 // In case of line wrapping don't move the cursor beyond the
179 // right screen edge. In Insert mode allow going just beyond
180 // the last character (like what happens when typing and
181 // reaching the right window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 wcol = (csize / width + 1) * width - 1;
183 }
184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100186 init_chartabsize_arg(&cts, curwin, pos->lnum, 0, line, line);
187 while (cts.cts_vcol <= wcol && *cts.cts_ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 {
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100189#ifdef FEAT_PROP_POPUP
190 int at_start = cts.cts_ptr == cts.cts_line;
191#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100192 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193#ifdef FEAT_LINEBREAK
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100194 csize = win_lbr_chartabsize(&cts, &head);
195 MB_PTR_ADV(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196#else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100197 csize = lbr_chartabsize_adv(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198#endif
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100199 cts.cts_vcol += csize;
Bram Moolenaar04e0ed12022-09-10 20:00:56 +0100200#ifdef FEAT_PROP_POPUP
201 if (at_start)
202 // do not count the columns for virtual text above
203 cts.cts_vcol -= cts.cts_first_char;
204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +0100206 col = cts.cts_vcol;
207 idx = (int)(cts.cts_ptr - line);
208 clear_chartabsize_arg(&cts);
209
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 /*
211 * Handle all the special cases. The virtual_active() check
212 * is needed to ensure that a virtual position off the end of
213 * a line has the correct indexing. The one_more comparison
214 * replaces an explicit add of one_more later on.
215 */
216 if (col > wcol || (!virtual_active() && one_more == 0))
217 {
218 idx -= 1;
219# ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +0100220 // Don't count the chars from 'showbreak'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 csize -= head;
222# endif
223 col -= csize;
224 }
225
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 if (virtual_active()
227 && addspaces
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200228 && wcol >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 && ((col != wcol && col != wcol + 1) || csize > 1))
230 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100231 // 'virtualedit' is set: The difference between wcol and col is
232 // filled with spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
234 if (line[idx] == NUL)
235 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100236 // Append spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 int correct = wcol - col;
238 char_u *newline = alloc(idx + correct + 1);
239 int t;
240
241 if (newline == NULL)
242 return FAIL;
243
244 for (t = 0; t < idx; ++t)
245 newline[t] = line[t];
246
247 for (t = 0; t < correct; ++t)
248 newline[t + idx] = ' ';
249
250 newline[idx + correct] = NUL;
251
252 ml_replace(pos->lnum, newline, FALSE);
253 changed_bytes(pos->lnum, (colnr_T)idx);
254 idx += correct;
255 col = wcol;
256 }
257 else
258 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100259 // Break a tab
Bram Moolenaar85a20022019-12-21 18:25:54 +0100260 int correct = wcol - col - csize + 1; // negative!!
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000261 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 int t, s = 0;
263 int v;
264
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000265 if (-correct > csize)
266 return FAIL;
267
268 newline = alloc(linelen + csize);
269 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 return FAIL;
271
272 for (t = 0; t < linelen; t++)
273 {
274 if (t != idx)
275 newline[s++] = line[t];
276 else
277 for (v = 0; v < csize; v++)
278 newline[s++] = ' ';
279 }
280
281 newline[linelen + csize - 1] = NUL;
282
283 ml_replace(pos->lnum, newline, FALSE);
284 changed_bytes(pos->lnum, idx);
285 idx += (csize - 1 + correct);
286 col += correct;
287 }
288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 }
290
291 if (idx < 0)
292 pos->col = 0;
293 else
294 pos->col = idx;
295
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 pos->coladd = 0;
297
298 if (finetune)
299 {
300 if (wcol == MAXCOL)
301 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100302 // The width of the last character is used to set coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 if (!one_more)
304 {
305 colnr_T scol, ecol;
306
307 getvcol(curwin, pos, &scol, NULL, &ecol);
308 pos->coladd = ecol - scol;
309 }
310 }
311 else
312 {
313 int b = (int)wcol - (int)col;
314
Bram Moolenaar85a20022019-12-21 18:25:54 +0100315 // The difference between wcol and col is used to set coladd.
Bram Moolenaar02631462017-09-22 15:20:32 +0200316 if (b > 0 && b < (MAXCOL - 2 * curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 pos->coladd = b;
318
319 col += b;
320 }
321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322
Bram Moolenaar85a20022019-12-21 18:25:54 +0100323 // prevent from moving onto a trail byte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200325 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200327 if (wcol < 0 || col < wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 return FAIL;
329 return OK;
330}
331
332/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000333 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 */
335 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100336inc_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337{
338 return inc(&curwin->w_cursor);
339}
340
Bram Moolenaar446cb832008-06-24 21:56:24 +0000341/*
342 * Increment the line pointer "lp" crossing line boundaries as necessary.
343 * Return 1 when going to the next line.
344 * Return 2 when moving forward onto a NUL at the end of the line).
345 * Return -1 when at the end of file.
346 * Return 0 otherwise.
347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100349inc(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100351 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352
Bram Moolenaar85a20022019-12-21 18:25:54 +0100353 // when searching position may be set to end of a line
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100354 if (lp->col != MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100356 p = ml_get_pos(lp);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100357 if (*p != NUL) // still within line, move to next char (may be NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100359 if (has_mbyte)
360 {
361 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100363 lp->col += l;
364 return ((p[l] != NUL) ? 0 : 2);
365 }
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100366 lp->col++;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100367 lp->coladd = 0;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100368 return ((p[1] != NUL) ? 0 : 2);
369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100371 if (lp->lnum != curbuf->b_ml.ml_line_count) // there is a next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 {
373 lp->col = 0;
374 lp->lnum++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 lp->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 return 1;
377 }
378 return -1;
379}
380
381/*
382 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
383 */
384 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100385incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386{
387 int r;
388
389 if ((r = inc(lp)) >= 1 && lp->col)
390 r = inc(lp);
391 return r;
392}
393
394/*
395 * dec(p)
396 *
397 * Decrement the line pointer 'p' crossing line boundaries as necessary.
398 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
399 */
400 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100401dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100403 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404}
405
406 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100407dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408{
409 char_u *p;
410
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 lp->coladd = 0;
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100412 if (lp->col == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100414 // past end of line
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100415 p = ml_get(lp->lnum);
zeertzjq94b7c322024-03-12 21:50:32 +0100416 lp->col = ml_get_len(lp->lnum);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100417 if (has_mbyte)
418 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100419 return 0;
420 }
421
422 if (lp->col > 0)
423 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100424 // still within line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 lp->col--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 if (has_mbyte)
427 {
428 p = ml_get(lp->lnum);
429 lp->col -= (*mb_head_off)(p, p + lp->col);
430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 return 0;
432 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100433
434 if (lp->lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100436 // there is a prior line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 lp->lnum--;
438 p = ml_get(lp->lnum);
zeertzjq94b7c322024-03-12 21:50:32 +0100439 lp->col = ml_get_len(lp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 if (has_mbyte)
441 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 return 1;
443 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100444
Bram Moolenaar85a20022019-12-21 18:25:54 +0100445 // at start of file
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100446 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447}
448
449/*
450 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
451 */
452 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100453decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454{
455 int r;
456
457 if ((r = dec(lp)) == 1 && lp->col)
458 r = dec(lp);
459 return r;
460}
461
462/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200463 * Get the line number relative to the current cursor position, i.e. the
464 * difference between line number and cursor position. Only look for lines that
465 * can be visible, folded lines don't count.
466 */
467 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100468get_cursor_rel_lnum(
469 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100470 linenr_T lnum) // line number to get the result for
Bram Moolenaar64486672010-05-16 15:46:46 +0200471{
472 linenr_T cursor = wp->w_cursor.lnum;
473 linenr_T retval = 0;
474
475#ifdef FEAT_FOLDING
476 if (hasAnyFolding(wp))
477 {
478 if (lnum > cursor)
479 {
480 while (lnum > cursor)
481 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100482 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100483 // if lnum and cursor are in the same fold,
484 // now lnum <= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200485 if (lnum > cursor)
486 retval++;
487 lnum--;
488 }
489 }
490 else if (lnum < cursor)
491 {
492 while (lnum < cursor)
493 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100494 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100495 // if lnum and cursor are in the same fold,
496 // now lnum >= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200497 if (lnum < cursor)
498 retval--;
499 lnum++;
500 }
501 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100502 // else if (lnum == cursor)
503 // retval = 0;
Bram Moolenaar64486672010-05-16 15:46:46 +0200504 }
505 else
506#endif
507 retval = lnum - cursor;
508
509 return retval;
510}
511
512/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200513 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
514 * This allows for the col to be on the NUL byte.
515 */
516 void
517check_pos(buf_T *buf, pos_T *pos)
518{
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200519 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 {
zeertzjq94b7c322024-03-12 21:50:32 +0100526 len = ml_get_buf_len(buf, pos->lnum);
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200527 if (pos->col > len)
528 pos->col = len;
529 }
530}
531
532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 * Make sure curwin->w_cursor.lnum is valid.
534 */
535 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100536check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
539 {
540#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100541 // If there is a closed fold at the end of the file, put the cursor in
542 // its first line. Otherwise in the last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 if (!hasFolding(curbuf->b_ml.ml_line_count,
544 &curwin->w_cursor.lnum, NULL))
545#endif
546 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
547 }
548 if (curwin->w_cursor.lnum <= 0)
549 curwin->w_cursor.lnum = 1;
550}
551
552/*
553 * Make sure curwin->w_cursor.col is valid.
554 */
555 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100556check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200558 check_cursor_col_win(curwin);
559}
560
561/*
562 * Make sure win->w_cursor.col is valid.
563 */
564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100565check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200566{
Gary Johnson53ba05b2021-07-26 22:19:10 +0200567 colnr_T len;
568 colnr_T oldcol = win->w_cursor.col;
569 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
570 unsigned int cur_ve_flags = get_ve_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
zeertzjq94b7c322024-03-12 21:50:32 +0100572 len = ml_get_buf_len(win->w_buffer, win->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200574 win->w_cursor.col = 0;
575 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100577 // Allow cursor past end-of-line when:
578 // - in Insert mode or restarting Insert mode
579 // - in Visual mode and 'selection' isn't "old"
580 // - 'virtualedit' is set
Bram Moolenaar24959102022-05-07 20:01:16 +0100581 if ((State & MODE_INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 || (VIsual_active && *p_sel != 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +0200583 || (cur_ve_flags & VE_ONEMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200585 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000587 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200588 win->w_cursor.col = len - 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100589 // Move the cursor to the head byte.
Bram Moolenaar87c19962007-04-26 08:54:21 +0000590 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200591 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200594 else if (win->w_cursor.col < 0)
595 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596
Bram Moolenaar85a20022019-12-21 18:25:54 +0100597 // If virtual editing is on, we can leave the cursor on the old position,
598 // only we must set it to virtual. But don't do it when at the end of the
599 // line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200601 win->w_cursor.coladd = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +0200602 else if (cur_ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000603 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200604 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200605 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200606 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaard41babe2017-08-30 17:01:35 +0200607
Bram Moolenaar85a20022019-12-21 18:25:54 +0100608 // Make sure that coladd is not more than the char width.
609 // Not for the last character, coladd is then used when the cursor
610 // is actually after the last character.
Bram Moolenaarfe154992022-03-22 20:42:12 +0000611 if (win->w_cursor.col + 1 < len)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200612 {
613 int cs, ce;
614
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200615 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
616 if (win->w_cursor.coladd > ce - cs)
617 win->w_cursor.coladd = ce - cs;
618 }
619 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000620 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100621 // avoid weird number when there is a miscalculation or overflow
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200622 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624}
625
626/*
627 * make sure curwin->w_cursor in on a valid character
628 */
629 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100630check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
632 check_cursor_lnum();
633 check_cursor_col();
634}
635
Bram Moolenaar7ce5b2b2022-05-16 19:40:59 +0100636/*
637 * Check if VIsual position is valid, correct it if not.
638 * Can be called when in Visual mode and a change has been made.
639 */
640 void
641check_visual_pos(void)
642{
643 if (VIsual.lnum > curbuf->b_ml.ml_line_count)
644 {
645 VIsual.lnum = curbuf->b_ml.ml_line_count;
646 VIsual.col = 0;
647 VIsual.coladd = 0;
648 }
649 else
650 {
zeertzjq94b7c322024-03-12 21:50:32 +0100651 int len = ml_get_len(VIsual.lnum);
Bram Moolenaar7ce5b2b2022-05-16 19:40:59 +0100652
653 if (VIsual.col > len)
654 {
655 VIsual.col = len;
656 VIsual.coladd = 0;
657 }
658 }
659}
660
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661/*
662 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
663 * Allow it when in Visual mode and 'selection' is not "old".
664 */
665 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100666adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
668 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 && gchar_cursor() == NUL)
671 --curwin->w_cursor.col;
672}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673
674/*
Bram Moolenaar0c34d562022-11-18 14:07:20 +0000675 * Set "curwin->w_leftcol" to "leftcol".
676 * Adjust the cursor position if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 * Return TRUE if the cursor was moved.
678 */
679 int
Bram Moolenaar0c34d562022-11-18 14:07:20 +0000680set_leftcol(colnr_T leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 int retval = FALSE;
Bram Moolenaar0c34d562022-11-18 14:07:20 +0000683
684 // Return quickly when there is no change.
685 if (curwin->w_leftcol == leftcol)
686 return FALSE;
687 curwin->w_leftcol = leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
689 changed_cline_bef_curs();
Bram Moolenaar0c34d562022-11-18 14:07:20 +0000690 long lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 validate_virtcol();
692
Bram Moolenaar0c34d562022-11-18 14:07:20 +0000693 // If the cursor is right or left of the screen, move it to last or first
694 // visible character.
695 long siso = get_sidescrolloff_value();
Bram Moolenaar375e3392019-01-31 18:26:10 +0100696 if (curwin->w_virtcol > (colnr_T)(lastcol - siso))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 {
698 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100699 coladvance((colnr_T)(lastcol - siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100701 else if (curwin->w_virtcol < curwin->w_leftcol + siso)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {
703 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100704 (void)coladvance((colnr_T)(curwin->w_leftcol + siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
706
Bram Moolenaar0c34d562022-11-18 14:07:20 +0000707 // If the start of the character under the cursor is not on the screen,
708 // advance the cursor one more char. If this fails (last char of the
709 // line) adjust the scrolling.
710 colnr_T s, e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
712 if (e > (colnr_T)lastcol)
713 {
714 retval = TRUE;
715 coladvance(s - 1);
716 }
717 else if (s < curwin->w_leftcol)
718 {
719 retval = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100720 if (coladvance(e + 1) == FAIL) // there isn't another character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100722 curwin->w_leftcol = s; // adjust w_leftcol instead
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 changed_cline_bef_curs();
724 }
725 }
726
727 if (retval)
728 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100729 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 return retval;
731}
732
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 * Isolate one part of a string option where parts are separated with
735 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +0000736 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * "*option" is advanced to the next part.
738 * The length is returned.
739 */
740 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100741copy_option_part(
742 char_u **option,
743 char_u *buf,
744 int maxlen,
745 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
747 int len = 0;
748 char_u *p = *option;
749
Bram Moolenaar85a20022019-12-21 18:25:54 +0100750 // skip '.' at start of option part, for 'suffixes'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 if (*p == '.')
752 buf[len++] = *p++;
753 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
754 {
755 /*
756 * Skip backslash before a separator character and space.
757 */
758 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
759 ++p;
760 if (len < maxlen - 1)
761 buf[len++] = *p;
762 ++p;
763 }
764 buf[len] = NUL;
765
Bram Moolenaar85a20022019-12-21 18:25:54 +0100766 if (*p != NUL && *p != ',') // skip non-standard separator
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 ++p;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100768 p = skip_to_option_part(p); // p points to next file name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
770 *option = p;
771 return len;
772}
773
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#ifndef HAVE_MEMSET
775 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100776vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777{
778 char *p = ptr;
779
780 while (size-- > 0)
781 *p++ = c;
782 return ptr;
783}
784#endif
785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786/*
787 * Vim has its own isspace() function, because on some machines isspace()
788 * can't handle characters above 128.
789 */
790 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100791vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792{
793 return ((x >= 9 && x <= 13) || x == ' ');
794}
795
796/************************************************************************
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 * functions that use lookup tables for various things, generally to do with
798 * special key codes.
799 */
800
801/*
802 * Some useful tables.
803 */
804
805static struct modmasktable
806{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100807 short mod_mask; // Bit-mask for particular key modifier
808 short mod_flag; // Bit(s) for particular key modifier
809 char_u name; // Single letter name of modifier
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810} mod_mask_table[] =
811{
812 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000813 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
815 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
816 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
817 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
818 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
Casey Tucker92e90a12024-01-25 22:44:00 +0100819#if defined(MACOS_X) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
821#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100822 // 'A' must be the last one
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
824 {0, 0, NUL}
Bram Moolenaar85a20022019-12-21 18:25:54 +0100825 // NOTE: when adding an entry, update MAX_KEY_NAME_LEN!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826};
827
828/*
829 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +0000830 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 */
832#define MOD_KEYS_ENTRY_SIZE 5
833
834static char_u modifier_keys_table[] =
835{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100836// mod mask with modifier without modifier
837 MOD_MASK_SHIFT, '&', '9', '@', '1', // begin
838 MOD_MASK_SHIFT, '&', '0', '@', '2', // cancel
839 MOD_MASK_SHIFT, '*', '1', '@', '4', // command
840 MOD_MASK_SHIFT, '*', '2', '@', '5', // copy
841 MOD_MASK_SHIFT, '*', '3', '@', '6', // create
842 MOD_MASK_SHIFT, '*', '4', 'k', 'D', // delete char
843 MOD_MASK_SHIFT, '*', '5', 'k', 'L', // delete line
844 MOD_MASK_SHIFT, '*', '7', '@', '7', // end
845 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', // end
846 MOD_MASK_SHIFT, '*', '9', '@', '9', // exit
847 MOD_MASK_SHIFT, '*', '0', '@', '0', // find
848 MOD_MASK_SHIFT, '#', '1', '%', '1', // help
849 MOD_MASK_SHIFT, '#', '2', 'k', 'h', // home
850 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', // home
851 MOD_MASK_SHIFT, '#', '3', 'k', 'I', // insert
852 MOD_MASK_SHIFT, '#', '4', 'k', 'l', // left arrow
853 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', // left arrow
854 MOD_MASK_SHIFT, '%', 'a', '%', '3', // message
855 MOD_MASK_SHIFT, '%', 'b', '%', '4', // move
856 MOD_MASK_SHIFT, '%', 'c', '%', '5', // next
857 MOD_MASK_SHIFT, '%', 'd', '%', '7', // options
858 MOD_MASK_SHIFT, '%', 'e', '%', '8', // previous
859 MOD_MASK_SHIFT, '%', 'f', '%', '9', // print
860 MOD_MASK_SHIFT, '%', 'g', '%', '0', // redo
861 MOD_MASK_SHIFT, '%', 'h', '&', '3', // replace
862 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', // right arr.
863 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', // right arr.
864 MOD_MASK_SHIFT, '%', 'j', '&', '5', // resume
865 MOD_MASK_SHIFT, '!', '1', '&', '6', // save
866 MOD_MASK_SHIFT, '!', '2', '&', '7', // suspend
867 MOD_MASK_SHIFT, '!', '3', '&', '8', // undo
868 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', // up arrow
869 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', // down arrow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870
Bram Moolenaar85a20022019-12-21 18:25:54 +0100871 // vt100 F1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
873 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
874 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
875 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
876
Bram Moolenaar85a20022019-12-21 18:25:54 +0100877 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', // F1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
879 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
880 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
881 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
882 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
883 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
884 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
885 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
Bram Moolenaar85a20022019-12-21 18:25:54 +0100886 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', // F10
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
889 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
890 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
891 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
892 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
893 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
894 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
895 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
896 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
897 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
898
899 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
900 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
901 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
902 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
903 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
904 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
905 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
906 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
907 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
908 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
909
910 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
911 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
912 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
913 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
914 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
915 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
916 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
917
Bram Moolenaar85a20022019-12-21 18:25:54 +0100918 // TAB pseudo code
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
920
921 NUL
922};
923
John Marriott4a1e6da2025-03-06 22:26:23 +0100924#define STRING_INIT(s) \
925 {(char_u *)(s), STRLEN_LITERAL(s)}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926static struct key_name_entry
927{
John Marriott4a1e6da2025-03-06 22:26:23 +0100928 int enabled; // is this entry available (TRUE/FALSE)?
929 int key; // special key code or ascii value
930 string_T name; // name of key
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +0100931 int is_alt; // is an alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932} key_names_table[] =
John Marriott4a1e6da2025-03-06 22:26:23 +0100933// Must be sorted by the 'name.string' field in ascending order because it is used by bsearch()!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934{
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +0100935 {TRUE, K_BS, STRING_INIT("BackSpace"), TRUE},
936 {TRUE, '|', STRING_INIT("Bar"), FALSE},
937 {TRUE, K_BS, STRING_INIT("BS"), FALSE},
938 {TRUE, '\\', STRING_INIT("Bslash"), FALSE},
939 {TRUE, K_COMMAND, STRING_INIT("Cmd"), FALSE},
940 {TRUE, CAR, STRING_INIT("CR"), FALSE},
941 {TRUE, CSI, STRING_INIT("CSI"), FALSE},
942 {TRUE, K_CURSORHOLD, STRING_INIT("CursorHold"), FALSE},
John Marriott4a1e6da2025-03-06 22:26:23 +0100943 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +0100944#ifdef FEAT_MOUSE_DEC
John Marriott4a1e6da2025-03-06 22:26:23 +0100945 TRUE,
946#else
947 FALSE,
Bram Moolenaar5af7d712012-01-20 17:15:51 +0100948#endif
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +0100949 K_DEC_MOUSE, STRING_INIT("DecMouse"), FALSE},
950 {TRUE, K_DEL, STRING_INIT("Del"), FALSE},
951 {TRUE, K_DEL, STRING_INIT("Delete"), TRUE},
952 {TRUE, K_DOWN, STRING_INIT("Down"), FALSE},
953 {TRUE, K_DROP, STRING_INIT("Drop"), FALSE},
954 {TRUE, K_END, STRING_INIT("End"), FALSE},
955 {TRUE, CAR, STRING_INIT("Enter"), TRUE},
956 {TRUE, ESC, STRING_INIT("Esc"), FALSE},
John Marriott4a1e6da2025-03-06 22:26:23 +0100957
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +0100958 {TRUE, K_F1, STRING_INIT("F1"), FALSE},
959 {TRUE, K_F10, STRING_INIT("F10"), FALSE},
960 {TRUE, K_F11, STRING_INIT("F11"), FALSE},
961 {TRUE, K_F12, STRING_INIT("F12"), FALSE},
962 {TRUE, K_F13, STRING_INIT("F13"), FALSE},
963 {TRUE, K_F14, STRING_INIT("F14"), FALSE},
964 {TRUE, K_F15, STRING_INIT("F15"), FALSE},
965 {TRUE, K_F16, STRING_INIT("F16"), FALSE},
966 {TRUE, K_F17, STRING_INIT("F17"), FALSE},
967 {TRUE, K_F18, STRING_INIT("F18"), FALSE},
968 {TRUE, K_F19, STRING_INIT("F19"), FALSE},
John Marriott4a1e6da2025-03-06 22:26:23 +0100969
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +0100970 {TRUE, K_F2, STRING_INIT("F2"), FALSE},
971 {TRUE, K_F20, STRING_INIT("F20"), FALSE},
972 {TRUE, K_F21, STRING_INIT("F21"), FALSE},
973 {TRUE, K_F22, STRING_INIT("F22"), FALSE},
974 {TRUE, K_F23, STRING_INIT("F23"), FALSE},
975 {TRUE, K_F24, STRING_INIT("F24"), FALSE},
976 {TRUE, K_F25, STRING_INIT("F25"), FALSE},
977 {TRUE, K_F26, STRING_INIT("F26"), FALSE},
978 {TRUE, K_F27, STRING_INIT("F27"), FALSE},
979 {TRUE, K_F28, STRING_INIT("F28"), FALSE},
980 {TRUE, K_F29, STRING_INIT("F29"), FALSE},
John Marriott4a1e6da2025-03-06 22:26:23 +0100981
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +0100982 {TRUE, K_F3, STRING_INIT("F3"), FALSE},
983 {TRUE, K_F30, STRING_INIT("F30"), FALSE},
984 {TRUE, K_F31, STRING_INIT("F31"), FALSE},
985 {TRUE, K_F32, STRING_INIT("F32"), FALSE},
986 {TRUE, K_F33, STRING_INIT("F33"), FALSE},
987 {TRUE, K_F34, STRING_INIT("F34"), FALSE},
988 {TRUE, K_F35, STRING_INIT("F35"), FALSE},
989 {TRUE, K_F36, STRING_INIT("F36"), FALSE},
990 {TRUE, K_F37, STRING_INIT("F37"), FALSE},
John Marriott4a1e6da2025-03-06 22:26:23 +0100991
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +0100992 {TRUE, K_F4, STRING_INIT("F4"), FALSE},
993 {TRUE, K_F5, STRING_INIT("F5"), FALSE},
994 {TRUE, K_F6, STRING_INIT("F6"), FALSE},
995 {TRUE, K_F7, STRING_INIT("F7"), FALSE},
996 {TRUE, K_F8, STRING_INIT("F8"), FALSE},
997 {TRUE, K_F9, STRING_INIT("F9"), FALSE},
John Marriott4a1e6da2025-03-06 22:26:23 +0100998
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +0100999 {TRUE, K_FOCUSGAINED, STRING_INIT("FocusGained"), FALSE},
1000 {TRUE, K_FOCUSLOST, STRING_INIT("FocusLost"), FALSE},
1001 {TRUE, K_HELP, STRING_INIT("Help"), FALSE},
1002 {TRUE, K_HOME, STRING_INIT("Home"), FALSE},
1003 {TRUE, K_IGNORE, STRING_INIT("Ignore"), FALSE},
1004 {TRUE, K_INS, STRING_INIT("Ins"), TRUE},
1005 {TRUE, K_INS, STRING_INIT("Insert"), FALSE},
John Marriott4a1e6da2025-03-06 22:26:23 +01001006 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001007#ifdef FEAT_MOUSE_JSB
John Marriott4a1e6da2025-03-06 22:26:23 +01001008 TRUE,
1009#else
1010 FALSE,
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001011#endif
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +01001012 K_JSBTERM_MOUSE, STRING_INIT("JsbMouse"), FALSE},
1013 {TRUE, K_K0, STRING_INIT("k0"), FALSE},
1014 {TRUE, K_K1, STRING_INIT("k1"), FALSE},
1015 {TRUE, K_K2, STRING_INIT("k2"), FALSE},
1016 {TRUE, K_K3, STRING_INIT("k3"), FALSE},
1017 {TRUE, K_K4, STRING_INIT("k4"), FALSE},
1018 {TRUE, K_K5, STRING_INIT("k5"), FALSE},
1019 {TRUE, K_K6, STRING_INIT("k6"), FALSE},
1020 {TRUE, K_K7, STRING_INIT("k7"), FALSE},
1021 {TRUE, K_K8, STRING_INIT("k8"), FALSE},
1022 {TRUE, K_K9, STRING_INIT("k9"), FALSE},
John Marriott4a1e6da2025-03-06 22:26:23 +01001023
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +01001024 {TRUE, K_KDEL, STRING_INIT("kDel"), FALSE},
1025 {TRUE, K_KDIVIDE, STRING_INIT("kDivide"), FALSE},
1026 {TRUE, K_KEND, STRING_INIT("kEnd"), FALSE},
1027 {TRUE, K_KENTER, STRING_INIT("kEnter"), FALSE},
1028 {TRUE, K_KHOME, STRING_INIT("kHome"), FALSE},
1029 {TRUE, K_KINS, STRING_INIT("kInsert"), FALSE},
1030 {TRUE, K_KMINUS, STRING_INIT("kMinus"), FALSE},
1031 {TRUE, K_KMULTIPLY, STRING_INIT("kMultiply"), FALSE},
1032 {TRUE, K_KPAGEDOWN, STRING_INIT("kPageDown"), FALSE},
1033 {TRUE, K_KPAGEUP, STRING_INIT("kPageUp"), FALSE},
1034 {TRUE, K_KPLUS, STRING_INIT("kPlus"), FALSE},
1035 {TRUE, K_KPOINT, STRING_INIT("kPoint"), FALSE},
1036 {TRUE, K_LEFT, STRING_INIT("Left"), FALSE},
1037 {TRUE, K_LEFTDRAG, STRING_INIT("LeftDrag"), FALSE},
1038 {TRUE, K_LEFTMOUSE, STRING_INIT("LeftMouse"), FALSE},
1039 {TRUE, K_LEFTMOUSE_NM, STRING_INIT("LeftMouseNM"), FALSE},
1040 {TRUE, K_LEFTRELEASE, STRING_INIT("LeftRelease"), FALSE},
1041 {TRUE, K_LEFTRELEASE_NM, STRING_INIT("LeftReleaseNM"), FALSE},
1042 {TRUE, NL, STRING_INIT("LF"), TRUE},
1043 {TRUE, NL, STRING_INIT("LineFeed"), TRUE},
1044 {TRUE, '<', STRING_INIT("lt"), FALSE},
1045 {TRUE, K_MIDDLEDRAG, STRING_INIT("MiddleDrag"), FALSE},
1046 {TRUE, K_MIDDLEMOUSE, STRING_INIT("MiddleMouse"), FALSE},
1047 {TRUE, K_MIDDLERELEASE, STRING_INIT("MiddleRelease"), FALSE},
1048 {TRUE, K_MOUSE, STRING_INIT("Mouse"), FALSE},
1049 {TRUE, K_MOUSEDOWN, STRING_INIT("MouseDown"), TRUE},
1050 {TRUE, K_MOUSEMOVE, STRING_INIT("MouseMove"), FALSE},
1051 {TRUE, K_MOUSEUP, STRING_INIT("MouseUp"), TRUE},
John Marriott4a1e6da2025-03-06 22:26:23 +01001052 {
1053#ifdef FEAT_MOUSE_NET
1054 TRUE,
1055#else
1056 FALSE,
1057#endif
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +01001058 K_NETTERM_MOUSE, STRING_INIT("NetMouse"), FALSE},
1059 {TRUE, NL, STRING_INIT("NewLine"), TRUE},
1060 {TRUE, NL, STRING_INIT("NL"), FALSE},
1061 {TRUE, K_ZERO, STRING_INIT("Nul"), FALSE},
1062 {TRUE, K_PAGEDOWN, STRING_INIT("PageDown"), FALSE},
1063 {TRUE, K_PAGEUP, STRING_INIT("PageUp"), FALSE},
1064 {TRUE, K_PE, STRING_INIT("PasteEnd"), FALSE},
1065 {TRUE, K_PS, STRING_INIT("PasteStart"), FALSE},
1066 {TRUE, K_PLUG, STRING_INIT("Plug"), FALSE},
John Marriott4a1e6da2025-03-06 22:26:23 +01001067 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001068#ifdef FEAT_MOUSE_PTERM
John Marriott4a1e6da2025-03-06 22:26:23 +01001069 TRUE,
1070#else
1071 FALSE,
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001072#endif
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +01001073 K_PTERM_MOUSE, STRING_INIT("PtermMouse"), FALSE},
1074 {TRUE, CAR, STRING_INIT("Return"), TRUE},
1075 {TRUE, K_RIGHT, STRING_INIT("Right"), FALSE},
1076 {TRUE, K_RIGHTDRAG, STRING_INIT("RightDrag"), FALSE},
1077 {TRUE, K_RIGHTMOUSE, STRING_INIT("RightMouse"), FALSE},
1078 {TRUE, K_RIGHTRELEASE, STRING_INIT("RightRelease"), FALSE},
1079 {TRUE, K_SCRIPT_COMMAND, STRING_INIT("ScriptCmd"), FALSE},
1080 {TRUE, K_MOUSEUP, STRING_INIT("ScrollWheelDown"), FALSE},
1081 {TRUE, K_MOUSERIGHT, STRING_INIT("ScrollWheelLeft"), FALSE},
1082 {TRUE, K_MOUSELEFT, STRING_INIT("ScrollWheelRight"), FALSE},
1083 {TRUE, K_MOUSEDOWN, STRING_INIT("ScrollWheelUp"), FALSE},
1084 {TRUE, K_SGR_MOUSE, STRING_INIT("SgrMouse"), FALSE},
1085 {TRUE, K_SGR_MOUSERELEASE, STRING_INIT("SgrMouseRelease"), FALSE},
John Marriott4a1e6da2025-03-06 22:26:23 +01001086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087#ifdef FEAT_EVAL
John Marriott4a1e6da2025-03-06 22:26:23 +01001088 TRUE,
1089#else
1090 FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091#endif
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +01001092 K_SNR, STRING_INIT("SNR"), FALSE},
1093 {TRUE, ' ', STRING_INIT("Space"), FALSE},
1094 {TRUE, TAB, STRING_INIT("Tab"), FALSE},
1095 {TRUE, K_TAB, STRING_INIT("Tab"), FALSE},
1096 {TRUE, K_UNDO, STRING_INIT("Undo"), FALSE},
1097 {TRUE, K_UP, STRING_INIT("Up"), FALSE},
John Marriott4a1e6da2025-03-06 22:26:23 +01001098 {
1099#ifdef FEAT_MOUSE_URXVT
1100 TRUE,
1101#else
1102 FALSE,
1103#endif
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +01001104 K_URXVT_MOUSE, STRING_INIT("UrxvtMouse"), FALSE},
1105 {TRUE, K_X1DRAG, STRING_INIT("X1Drag"), FALSE},
1106 {TRUE, K_X1MOUSE, STRING_INIT("X1Mouse"), FALSE},
1107 {TRUE, K_X1RELEASE, STRING_INIT("X1Release"), FALSE},
1108 {TRUE, K_X2DRAG, STRING_INIT("X2Drag"), FALSE},
1109 {TRUE, K_X2MOUSE, STRING_INIT("X2Mouse"), FALSE},
1110 {TRUE, K_X2RELEASE, STRING_INIT("X2Release"), FALSE},
1111 {TRUE, K_CSI, STRING_INIT("xCSI"), FALSE},
1112 {TRUE, K_XDOWN, STRING_INIT("xDown"), FALSE},
1113 {TRUE, K_XEND, STRING_INIT("xEnd"), FALSE},
1114 {TRUE, K_XF1, STRING_INIT("xF1"), FALSE},
1115 {TRUE, K_XF2, STRING_INIT("xF2"), FALSE},
1116 {TRUE, K_XF3, STRING_INIT("xF3"), FALSE},
1117 {TRUE, K_XF4, STRING_INIT("xF4"), FALSE},
1118 {TRUE, K_XHOME, STRING_INIT("xHome"), FALSE},
1119 {TRUE, K_XLEFT, STRING_INIT("xLeft"), FALSE},
1120 {TRUE, K_XRIGHT, STRING_INIT("xRight"), FALSE},
1121 {TRUE, K_XUP, STRING_INIT("xUp"), FALSE},
1122 {TRUE, K_ZEND, STRING_INIT("zEnd"), FALSE},
1123 {TRUE, K_ZHOME, STRING_INIT("zHome"), FALSE}
Bram Moolenaar85a20022019-12-21 18:25:54 +01001124 // NOTE: When adding a long name update MAX_KEY_NAME_LEN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125};
John Marriott4a1e6da2025-03-06 22:26:23 +01001126#undef STRING_INIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128/*
1129 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
1130 * modifier name ('S' for Shift, 'C' for Ctrl etc).
1131 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001132 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001133name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134{
1135 int i;
1136
1137 c = TOUPPER_ASC(c);
1138 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
1139 if (c == mod_mask_table[i].name)
1140 return mod_mask_table[i].mod_flag;
1141 return 0;
1142}
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144/*
1145 * Check if if there is a special key code for "key" that includes the
1146 * modifiers specified.
1147 */
1148 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001149simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150{
1151 int i;
1152 int key0;
1153 int key1;
1154
zeertzjq0c989e42024-02-03 18:04:05 +01001155 if (!(*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001156 return key;
1157
1158 // TAB is a special case
1159 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001161 *modifiers &= ~MOD_MASK_SHIFT;
1162 return K_S_TAB;
1163 }
1164 key0 = KEY2TERMCAP0(key);
1165 key1 = KEY2TERMCAP1(key);
1166 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
1167 {
1168 if (key0 == modifier_keys_table[i + 3]
1169 && key1 == modifier_keys_table[i + 4]
1170 && (*modifiers & modifier_keys_table[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001172 *modifiers &= ~modifier_keys_table[i];
1173 return TERMCAP2KEY(modifier_keys_table[i + 1],
1174 modifier_keys_table[i + 2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 }
1177 return key;
1178}
1179
1180/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001181 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001182 */
1183 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001184handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001185{
1186 switch (key)
1187 {
1188 case K_XUP: return K_UP;
1189 case K_XDOWN: return K_DOWN;
1190 case K_XLEFT: return K_LEFT;
1191 case K_XRIGHT: return K_RIGHT;
1192 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001193 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001194 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001195 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001196 case K_XF1: return K_F1;
1197 case K_XF2: return K_F2;
1198 case K_XF3: return K_F3;
1199 case K_XF4: return K_F4;
1200 case K_S_XF1: return K_S_F1;
1201 case K_S_XF2: return K_S_F2;
1202 case K_S_XF3: return K_S_F3;
1203 case K_S_XF4: return K_S_F4;
1204 }
1205 return key;
1206}
1207
1208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 * Return a string which contains the name of the given key when the given
1210 * modifiers are down.
1211 */
1212 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001213get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214{
1215 static char_u string[MAX_KEY_NAME_LEN + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 int i, idx;
1217 int table_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
1219 string[0] = '<';
1220 idx = 1;
1221
Bram Moolenaar85a20022019-12-21 18:25:54 +01001222 // Key that stands for a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
1224 c = KEY2TERMCAP1(c);
1225
1226 /*
1227 * Translate shifted special keys into unshifted keys and set modifier.
1228 * Same for CTRL and ALT modifiers.
1229 */
1230 if (IS_SPECIAL(c))
1231 {
1232 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
1233 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
1234 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
1235 {
1236 modifiers |= modifier_keys_table[i];
1237 c = TERMCAP2KEY(modifier_keys_table[i + 3],
1238 modifier_keys_table[i + 4]);
1239 break;
1240 }
1241 }
1242
Bram Moolenaar85a20022019-12-21 18:25:54 +01001243 // try to find the key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 table_idx = find_special_key_in_table(c);
1245
1246 /*
1247 * When not a known special key, and not a printable character, try to
1248 * extract modifiers.
1249 */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001250 if (c > 0 && (*mb_char2len)(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
1252 if (table_idx < 0
1253 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
1254 && (c & 0x80))
1255 {
1256 c &= 0x7f;
1257 modifiers |= MOD_MASK_ALT;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001258 // try again, to find the un-alted key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 table_idx = find_special_key_in_table(c);
1260 }
1261 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
1262 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 c += '@';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 modifiers |= MOD_MASK_CTRL;
1265 }
1266 }
1267
Bram Moolenaar85a20022019-12-21 18:25:54 +01001268 // translate the modifier into a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 for (i = 0; mod_mask_table[i].name != 'A'; i++)
1270 if ((modifiers & mod_mask_table[i].mod_mask)
1271 == mod_mask_table[i].mod_flag)
1272 {
1273 string[idx++] = mod_mask_table[i].name;
1274 string[idx++] = (char_u)'-';
1275 }
1276
Bram Moolenaar85a20022019-12-21 18:25:54 +01001277 if (table_idx < 0) // unknown special key, may output t_xx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {
1279 if (IS_SPECIAL(c))
1280 {
1281 string[idx++] = 't';
1282 string[idx++] = '_';
1283 string[idx++] = KEY2TERMCAP0(c);
1284 string[idx++] = KEY2TERMCAP1(c);
1285 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001286 // Not a special key, only modifiers, output directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 else
1288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (has_mbyte && (*mb_char2len)(c) > 1)
1290 idx += (*mb_char2bytes)(c, string + idx);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001291 else if (vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 string[idx++] = c;
1293 else
1294 {
John Marriott4a1e6da2025-03-06 22:26:23 +01001295 char_u *s = transchar(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 while (*s)
1297 string[idx++] = *s++;
1298 }
1299 }
1300 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001301 else // use name of special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
John Marriott4a1e6da2025-03-06 22:26:23 +01001303 string_T *s;
Bram Moolenaar423977d2017-01-22 15:05:12 +01001304
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +01001305 s = &key_names_table[table_idx].name;
John Marriott4a1e6da2025-03-06 22:26:23 +01001306
1307 if (s->length + idx + 2 <= MAX_KEY_NAME_LEN)
Bram Moolenaar423977d2017-01-22 15:05:12 +01001308 {
John Marriott4a1e6da2025-03-06 22:26:23 +01001309 STRCPY(string + idx, s->string);
1310 idx += (int)s->length;
Bram Moolenaar423977d2017-01-22 15:05:12 +01001311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 string[idx++] = '>';
1314 string[idx] = NUL;
John Marriott4a1e6da2025-03-06 22:26:23 +01001315
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 return string;
1317}
1318
1319/*
Bram Moolenaar32030a92023-01-13 18:46:57 +00001320 * Try translating a <> name at "(*srcp)[]" to "dst[]".
1321 * Return the number of characters added to "dst[]", zero for no match.
1322 * If there is a match, "srcp" is advanced to after the <> name.
1323 * "dst[]" must be big enough to hold the result (up to six characters)!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 */
1325 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001326trans_special(
1327 char_u **srcp,
1328 char_u *dst,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001329 int flags, // FSK_ values
zeertzjqdb088872022-05-02 22:53:45 +01001330 int escape_ks, // escape K_SPECIAL bytes in the character
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001331 int *did_simplify) // FSK_SIMPLIFY and found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
1333 int modifiers = 0;
1334 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001336 key = find_special_key(srcp, &modifiers, flags, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (key == 0)
1338 return 0;
1339
zeertzjqdb088872022-05-02 22:53:45 +01001340 return special_to_buf(key, modifiers, escape_ks, dst);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001341}
1342
1343/*
1344 * Put the character sequence for "key" with "modifiers" into "dst" and return
1345 * the resulting length.
zeertzjqdb088872022-05-02 22:53:45 +01001346 * When "escape_ks" is TRUE escape K_SPECIAL bytes in the character.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001347 * The sequence is not NUL terminated.
1348 * This is how characters in a string are encoded.
1349 */
1350 int
zeertzjqdb088872022-05-02 22:53:45 +01001351special_to_buf(int key, int modifiers, int escape_ks, char_u *dst)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001352{
1353 int dlen = 0;
1354
Bram Moolenaar85a20022019-12-21 18:25:54 +01001355 // Put the appropriate modifier in a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (modifiers != 0)
1357 {
1358 dst[dlen++] = K_SPECIAL;
1359 dst[dlen++] = KS_MODIFIER;
1360 dst[dlen++] = modifiers;
1361 }
1362
1363 if (IS_SPECIAL(key))
1364 {
1365 dst[dlen++] = K_SPECIAL;
1366 dst[dlen++] = KEY2TERMCAP0(key);
1367 dst[dlen++] = KEY2TERMCAP1(key);
1368 }
zeertzjqdb088872022-05-02 22:53:45 +01001369 else if (escape_ks)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
zeertzjqdb088872022-05-02 22:53:45 +01001371 else if (has_mbyte)
1372 dlen += (*mb_char2bytes)(key, dst + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 else
1374 dst[dlen++] = key;
1375
1376 return dlen;
1377}
1378
1379/*
Bram Moolenaar32030a92023-01-13 18:46:57 +00001380 * Try translating a <> name at "(*srcp)[]", return the key and put modifiers
1381 * in "modp".
1382 * "srcp" is advanced to after the <> name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 * returns 0 if there is no match.
1384 */
1385 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001386find_special_key(
1387 char_u **srcp,
1388 int *modp,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001389 int flags, // FSK_ values
Bram Moolenaar459fd782019-10-13 16:43:39 +02001390 int *did_simplify) // found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 char_u *last_dash;
1393 char_u *end_of_name;
1394 char_u *src;
1395 char_u *bp;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001396 int in_string = flags & FSK_IN_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 int modifiers;
1398 int bit;
1399 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001400 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001401 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402
1403 src = *srcp;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001404 if (src[0] != '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 return 0;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001406 if (src[1] == '*') // <*xxx>: do not simplify
1407 ++src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
Bram Moolenaar85a20022019-12-21 18:25:54 +01001409 // Find end of modifier list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 last_dash = src;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02001411 for (bp = src + 1; *bp == '-' || vim_isNormalIDc(*bp); bp++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 {
1413 if (*bp == '-')
1414 {
1415 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001416 if (bp[1] != NUL)
1417 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001418 if (has_mbyte)
1419 l = mb_ptr2len(bp + 1);
1420 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001421 l = 1;
Bram Moolenaarc8fd33d2019-08-16 20:33:05 +02001422 // Anything accepted, like <C-?>.
1423 // <C-"> or <M-"> are not special in strings as " is
1424 // the string delimiter. With a backslash it works: <M-\">
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001425 if (!(in_string && bp[1] == '"') && bp[l + 1] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02001426 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001427 else if (in_string && bp[1] == '\\' && bp[2] == '"'
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001428 && bp[3] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001429 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 }
1432 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001433 bp += 3; // skip t_xx, xx may be '-' or '>'
Bram Moolenaar792826c2011-08-19 22:29:02 +02001434 else if (STRNICMP(bp, "char-", 5) == 0)
1435 {
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00001436 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0, TRUE, NULL);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001437 if (l == 0)
1438 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001439 emsg(_(e_invalid_argument));
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001440 return 0;
1441 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02001442 bp += l + 5;
1443 break;
1444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001447 if (*bp == '>') // found matching '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 {
1449 end_of_name = bp + 1;
1450
Bram Moolenaar85a20022019-12-21 18:25:54 +01001451 // Which modifiers are given?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 modifiers = 0x0;
1453 for (bp = src + 1; bp < last_dash; bp++)
1454 {
1455 if (*bp != '-')
1456 {
1457 bit = name_to_mod_mask(*bp);
1458 if (bit == 0x0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001459 break; // Illegal modifier name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 modifiers |= bit;
1461 }
1462 }
1463
1464 /*
1465 * Legal modifier name.
1466 */
1467 if (bp >= last_dash)
1468 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001469 if (STRNICMP(last_dash + 1, "char-", 5) == 0
1470 && VIM_ISDIGIT(last_dash[6]))
1471 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001472 // <Char-123> or <Char-033> or <Char-0x33>
Bram Moolenaar459fd782019-10-13 16:43:39 +02001473 vim_str2nr(last_dash + 6, NULL, &l, STR2NR_ALL, NULL,
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00001474 &n, 0, TRUE, NULL);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001475 if (l == 0)
1476 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001477 emsg(_(e_invalid_argument));
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001478 return 0;
1479 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02001480 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001483 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001484 int off = 1;
1485
Bram Moolenaar85a20022019-12-21 18:25:54 +01001486 // Modifier with single letter, or special key name.
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001487 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
1488 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02001489 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001490 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02001491 else
Bram Moolenaar792826c2011-08-19 22:29:02 +02001492 l = 1;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001493 if (modifiers != 0 && last_dash[l + off] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001494 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02001495 else
1496 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001497 key = get_special_key_code(last_dash + off);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001498 if (!(flags & FSK_KEEP_X_KEY))
Bram Moolenaar792826c2011-08-19 22:29:02 +02001499 key = handle_x_keys(key);
1500 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
1503 /*
1504 * get_special_key_code() may return NUL for invalid
1505 * special key name.
1506 */
1507 if (key != NUL)
1508 {
1509 /*
1510 * Only use a modifier when there is no special key code that
1511 * includes the modifier.
1512 */
1513 key = simplify_key(key, &modifiers);
1514
Bram Moolenaar32030a92023-01-13 18:46:57 +00001515 if ((flags & FSK_KEYCODE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001517 // don't want keycode, use single byte code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 if (key == K_BS)
1519 key = BS;
1520 else if (key == K_DEL || key == K_KDEL)
1521 key = DEL;
1522 }
Bram Moolenaar32030a92023-01-13 18:46:57 +00001523 else if (key == 27
Bram Moolenaar584b8532023-01-14 21:07:07 +00001524 && (flags & FSK_FROM_PART) != 0
Bram Moolenaar32030a92023-01-13 18:46:57 +00001525 && (kitty_protocol_state == KKPS_ENABLED
1526 || kitty_protocol_state == KKPS_DISABLED))
1527 {
1528 // Using the Kitty key protocol, which uses K_ESC for an
1529 // Esc character. For the simplified keys use the Esc
1530 // character and set did_simplify, then in the
1531 // non-simplified keys use K_ESC.
1532 if ((flags & FSK_SIMPLIFY) != 0)
Bram Moolenaar584b8532023-01-14 21:07:07 +00001533 {
1534 if (did_simplify != NULL)
1535 *did_simplify = TRUE;
1536 }
Bram Moolenaar32030a92023-01-13 18:46:57 +00001537 else
1538 key = K_ESC;
1539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
Bram Moolenaar459fd782019-10-13 16:43:39 +02001541 // Normal Key with modifier: Try to make a single byte code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (!IS_SPECIAL(key))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001543 key = extract_modifiers(key, &modifiers,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001544 flags & FSK_SIMPLIFY, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
1546 *modp = modifiers;
1547 *srcp = end_of_name;
1548 return key;
1549 }
1550 }
1551 }
1552 return 0;
1553}
1554
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001555
1556/*
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02001557 * Some keys are used with Ctrl without Shift and are still expected to be
1558 * mapped as if Shift was pressed:
1559 * CTRL-2 is CTRL-@
1560 * CTRL-6 is CTRL-^
1561 * CTRL-- is CTRL-_
Bram Moolenaarc896adb2022-11-19 19:02:40 +00001562 * Also, unless no_reduce_keys is set then <C-H> and <C-h> mean the same thing,
1563 * use "H".
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02001564 * Returns the possibly adjusted key.
1565 */
1566 int
1567may_adjust_key_for_ctrl(int modifiers, int key)
1568{
Bram Moolenaaraab2ead2023-02-11 16:15:50 +00001569 if ((modifiers & MOD_MASK_CTRL) == 0)
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001570 return key;
1571
1572 if (ASCII_ISALPHA(key))
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02001573 {
Bram Moolenaarc896adb2022-11-19 19:02:40 +00001574#ifdef FEAT_TERMINAL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001575 check_no_reduce_keys(); // may update the no_reduce_keys flag
Bram Moolenaarc896adb2022-11-19 19:02:40 +00001576#endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001577 return no_reduce_keys == 0 ? TOUPPER_ASC(key) : key;
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02001578 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001579 if (key == '2')
1580 return '@';
1581 if (key == '6')
1582 return '^';
1583 if (key == '-')
1584 return '_';
Bram Moolenaaraab2ead2023-02-11 16:15:50 +00001585
1586 // On a Belgian keyboard AltGr $ is ']', on other keyboards '$' can only be
1587 // obtained with Shift. Assume that '$' without shift implies a Belgian
1588 // keyboard, where CTRL-$ means CTRL-].
1589 if (key == '$' && (modifiers & MOD_MASK_SHIFT) == 0)
1590 return ']';
1591
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02001592 return key;
1593}
1594
1595/*
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001596 * Some keys already have Shift included, pass them as normal keys.
Bram Moolenaar9a033d72020-10-07 17:29:48 +02001597 * When Ctrl is also used <C-H> and <C-S-H> are different, but <C-S-{> should
1598 * be <C-{>. Same for <C-S-}> and <C-S-|>.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001599 * Also for <A-S-a> and <M-S-a>.
Bram Moolenaar83a19c52022-09-12 20:35:28 +01001600 * This includes all printable ASCII characters except a-z.
1601 * Digits are included because with AZERTY the Shift key is used to get them.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001602 */
1603 int
1604may_remove_shift_modifier(int modifiers, int key)
1605{
1606 if ((modifiers == MOD_MASK_SHIFT
1607 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
Casey Tucker92e90a12024-01-25 22:44:00 +01001608#ifdef FEAT_GUI_GTK
1609 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_CMD)
1610#endif
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001611 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02001612 && ((key >= '!' && key <= '/')
1613 || (key >= ':' && key <= 'Z')
Bram Moolenaar83a19c52022-09-12 20:35:28 +01001614 || vim_isdigit(key)
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02001615 || (key >= '[' && key <= '`')
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001616 || (key >= '{' && key <= '~')))
1617 return modifiers & ~MOD_MASK_SHIFT;
Bram Moolenaar9a033d72020-10-07 17:29:48 +02001618
1619 if (modifiers == (MOD_MASK_SHIFT | MOD_MASK_CTRL)
1620 && (key == '{' || key == '}' || key == '|'))
1621 return modifiers & ~MOD_MASK_SHIFT;
1622
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001623 return modifiers;
1624}
1625
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626/*
1627 * Try to include modifiers in the key.
1628 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
Bram Moolenaar459fd782019-10-13 16:43:39 +02001629 * When "simplify" is FALSE don't do Ctrl and Alt.
1630 * When "simplify" is TRUE and Ctrl or Alt is removed from modifiers set
1631 * "did_simplify" when it's not NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 */
1633 int
Bram Moolenaar459fd782019-10-13 16:43:39 +02001634extract_modifiers(int key, int *modp, int simplify, int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635{
1636 int modifiers = *modp;
1637
Bram Moolenaard0573012017-10-28 21:11:06 +02001638#ifdef MACOS_X
Bram Moolenaar459fd782019-10-13 16:43:39 +02001639 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (!(modifiers & MOD_MASK_CMD))
1641#endif
1642 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
1643 {
1644 key = TOUPPER_ASC(key);
Bram Moolenaar20298ce2020-06-19 21:46:52 +02001645 // With <C-S-a> we keep the shift modifier.
1646 // With <S-a>, <A-S-a> and <S-A> we don't keep the shift modifier.
1647 if (simplify || modifiers == MOD_MASK_SHIFT
1648 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
1649 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001650 modifiers &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02001652
1653 // <C-H> and <C-h> mean the same thing, always use "H"
1654 if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key))
1655 key = TOUPPER_ASC(key);
1656
1657 if (simplify && (modifiers & MOD_MASK_CTRL)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001658 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 {
1660 key = Ctrl_chr(key);
1661 modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001662 // <C-@> is <Nul>
zeertzjq17c95d92022-04-26 12:51:07 +01001663 if (key == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 key = K_ZERO;
Bram Moolenaar459fd782019-10-13 16:43:39 +02001665 if (did_simplify != NULL)
1666 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02001668
Bram Moolenaard0573012017-10-28 21:11:06 +02001669#ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01001670 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (!(modifiers & MOD_MASK_CMD))
1672#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +02001673 if (simplify && (modifiers & MOD_MASK_ALT) && key < 0x80
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001674 && !enc_dbcs) // avoid creating a lead byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 {
1676 key |= 0x80;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001677 modifiers &= ~MOD_MASK_ALT; // remove the META modifier
Bram Moolenaar459fd782019-10-13 16:43:39 +02001678 if (did_simplify != NULL)
1679 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
1681
1682 *modp = modifiers;
1683 return key;
1684}
1685
1686/*
1687 * Try to find key "c" in the special key table.
1688 * Return the index when found, -1 when not found.
1689 */
1690 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001691find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
1693 int i;
1694
John Marriott4a1e6da2025-03-06 22:26:23 +01001695 for (i = 0; i < (int)ARRAY_LENGTH(key_names_table); i++)
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +01001696 if (c == key_names_table[i].key && !key_names_table[i].is_alt)
John Marriott4a1e6da2025-03-06 22:26:23 +01001697 return key_names_table[i].enabled ? i : -1;
1698
1699 return -1;
1700}
1701
1702
1703/*
1704 * Compare two 'struct key_name_entry' structures.
1705 * Note that the target string (p1) may contain additional trailing characters
1706 * that should not factor into the comparison. Example:
1707 * 'LeftMouse>", "<LeftMouse>"] ...'
1708 * should match with
1709 * 'LeftMouse'.
1710 * These characters are identified by vim_isNormalIDc().
1711 */
1712 static int
1713cmp_key_name_entry(const void *a, const void *b)
1714{
1715 char_u *p1 = ((struct key_name_entry *)a)->name.string;
1716 char_u *p2 = ((struct key_name_entry *)b)->name.string;
1717 int result = 0;
1718
1719 if (p1 == p2)
1720 return 0;
1721
1722 while (vim_isNormalIDc(*p1) && *p2 != NUL)
1723 {
1724 if ((result = TOLOWER_ASC(*p1) - TOLOWER_ASC(*p2)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 break;
John Marriott4a1e6da2025-03-06 22:26:23 +01001726 ++p1;
1727 ++p2;
1728 }
1729
1730 if (result == 0)
1731 {
1732 if (*p2 == NUL)
1733 {
1734 if (vim_isNormalIDc(*p1))
1735 result = 1;
1736 }
1737 else
1738 {
1739 result = -1;
1740 }
1741 }
1742
1743 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744}
1745
1746/*
1747 * Find the special key with the given name (the given string does not have to
1748 * end with NUL, the name is assumed to end before the first non-idchar).
1749 * If the name starts with "t_" the next two characters are interpreted as a
1750 * termcap name.
1751 * Return the key code, or 0 if not found.
1752 */
1753 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001754get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 /*
1757 * If it's <t_xx> we get the code for xx from the termcap
1758 */
1759 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
1760 {
John Marriott4a1e6da2025-03-06 22:26:23 +01001761 char_u string[3];
1762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 string[0] = name[2];
1764 string[1] = name[3];
1765 string[2] = NUL;
1766 if (add_termcap_entry(string, FALSE) == OK)
1767 return TERMCAP2KEY(name[2], name[3]);
1768 }
1769 else
John Marriott4a1e6da2025-03-06 22:26:23 +01001770 {
1771 struct key_name_entry target;
1772 struct key_name_entry *entry;
1773
1774 target.enabled = TRUE;
1775 target.key = 0;
1776 target.name.string = name;
1777 target.name.length = 0;
John Marriott4a1e6da2025-03-06 22:26:23 +01001778
1779 entry = (struct key_name_entry *)bsearch(
1780 &target,
1781 &key_names_table,
1782 ARRAY_LENGTH(key_names_table),
1783 sizeof(key_names_table[0]),
1784 cmp_key_name_entry);
1785 if (entry != NULL && entry->enabled)
zeertzjqb3a91272025-03-07 18:51:40 +01001786 {
1787 int key = entry->key;
1788 // Both TAB and K_TAB have name "Tab", and it's unspecified which
1789 // one bsearch() will return. TAB is the expected one.
1790 return key == K_TAB ? TAB : key;
1791 }
John Marriott4a1e6da2025-03-06 22:26:23 +01001792 }
1793
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 return 0;
1795}
1796
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001798get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
John Marriott4a1e6da2025-03-06 22:26:23 +01001800 if (i < 0 || i >= (int)ARRAY_LENGTH(key_names_table))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 return NULL;
John Marriott4a1e6da2025-03-06 22:26:23 +01001802
Yee Cheng Chin7d8e7df2025-03-27 17:43:41 +01001803 return key_names_table[i].name.string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806/*
1807 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
1808 */
1809 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001810get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811{
1812 int c = *buf->b_p_ff;
1813
1814 if (buf->b_p_bin || c == 'u')
1815 return EOL_UNIX;
1816 if (c == 'm')
1817 return EOL_MAC;
1818 return EOL_DOS;
1819}
1820
1821/*
1822 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
1823 * argument.
1824 */
1825 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001826get_fileformat_force(
1827 buf_T *buf,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001828 exarg_T *eap) // can be NULL!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
1830 int c;
1831
1832 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001833 c = eap->force_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 else
1835 {
1836 if ((eap != NULL && eap->force_bin != 0)
1837 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
1838 return EOL_UNIX;
1839 c = *buf->b_p_ff;
1840 }
1841 if (c == 'u')
1842 return EOL_UNIX;
1843 if (c == 'm')
1844 return EOL_MAC;
1845 return EOL_DOS;
1846}
1847
1848/*
1849 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
1850 * Sets both 'textmode' and 'fileformat'.
1851 * Note: Does _not_ set global value of 'textmode'!
1852 */
1853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001854set_fileformat(
1855 int t,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001856 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
1858 char *p = NULL;
1859
1860 switch (t)
1861 {
1862 case EOL_DOS:
1863 p = FF_DOS;
1864 curbuf->b_p_tx = TRUE;
1865 break;
1866 case EOL_UNIX:
1867 p = FF_UNIX;
1868 curbuf->b_p_tx = FALSE;
1869 break;
1870 case EOL_MAC:
1871 p = FF_MAC;
1872 curbuf->b_p_tx = FALSE;
1873 break;
1874 }
1875 if (p != NULL)
1876 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001877 OPT_FREE | opt_flags, 0);
1878
Bram Moolenaar85a20022019-12-21 18:25:54 +01001879 // This may cause the buffer to become (un)modified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001881 redraw_tabline = TRUE;
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02001882#if defined(FEAT_TABPANEL)
1883 redraw_tabpanel = TRUE;
1884#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001885 need_maketitle = TRUE; // set window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886}
1887
1888/*
1889 * Return the default fileformat from 'fileformats'.
1890 */
1891 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001892default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
1894 switch (*p_ffs)
1895 {
1896 case 'm': return EOL_MAC;
1897 case 'd': return EOL_DOS;
1898 }
1899 return EOL_UNIX;
1900}
1901
1902/*
1903 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
1904 */
1905 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001906call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001909#ifdef FEAT_PROFILE
1910 proftime_T wait_time;
1911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
1913 if (p_verbose > 3)
1914 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001915 verbose_enter();
Bram Moolenaar06f08532020-05-12 23:45:16 +02001916 smsg(_("Calling shell to execute: \"%s\""), cmd == NULL ? p_sh : cmd);
Bram Moolenaar11901392022-09-26 19:50:44 +01001917 msg_putchar_attr('\n', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001919 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 }
1921
Bram Moolenaar05159a02005-02-26 23:04:13 +00001922#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00001923 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001924 prof_child_enter(&wait_time);
1925#endif
1926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (*p_sh == NUL)
1928 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001929 emsg(_(e_shell_option_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 retval = -1;
1931 }
1932 else
1933 {
1934#ifdef FEAT_GUI_MSWIN
Bram Moolenaar85a20022019-12-21 18:25:54 +01001935 // Don't hide the pointer while executing a shell command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 gui_mch_mousehide(FALSE);
1937#endif
1938#ifdef FEAT_GUI
1939 ++hold_gui_events;
1940#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001941 // The external command may update a tags file, clear cached tags.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 tag_freematch();
1943
Bram Moolenaar01257a72019-06-10 14:46:04 +02001944 if (cmd == NULL || *p_sxq == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 retval = mch_call_shell(cmd, opt);
1946 else
1947 {
John Marriott4a1e6da2025-03-06 22:26:23 +01001948 char_u *ncmd;
1949 size_t ncmdsize;
1950 char_u *ecmd = cmd;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001951
Bram Moolenaar1a613392019-09-28 15:51:37 +02001952 if (*p_sxe != NUL && *p_sxq == '(')
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001953 {
1954 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
1955 if (ecmd == NULL)
1956 ecmd = cmd;
1957 }
John Marriott4a1e6da2025-03-06 22:26:23 +01001958 ncmdsize = STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1;
1959 ncmd = alloc(ncmdsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 if (ncmd != NULL)
1961 {
Bram Moolenaar1a613392019-09-28 15:51:37 +02001962 // When 'shellxquote' is ( append ).
1963 // When 'shellxquote' is "( append )".
John Marriott4a1e6da2025-03-06 22:26:23 +01001964 vim_snprintf((char *)ncmd, ncmdsize, "%s%s%s", p_sxq, ecmd, *p_sxq == '(' ? (char_u *)")"
Bram Moolenaar1a613392019-09-28 15:51:37 +02001965 : *p_sxq == '"' && *(p_sxq+1) == '(' ? (char_u *)")\""
1966 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 retval = mch_call_shell(ncmd, opt);
1968 vim_free(ncmd);
1969 }
1970 else
1971 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001972 if (ecmd != cmd)
1973 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
1975#ifdef FEAT_GUI
1976 --hold_gui_events;
1977#endif
1978 /*
1979 * Check the window size, in case it changed while executing the
1980 * external command.
1981 */
1982 shell_resized_check();
1983 }
1984
1985#ifdef FEAT_EVAL
1986 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001987# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00001988 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001989 prof_child_exit(&wait_time);
1990# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991#endif
1992
1993 return retval;
1994}
1995
1996/*
Bram Moolenaar24959102022-05-07 20:01:16 +01001997 * MODE_VISUAL, MODE_SELECT and MODE_OP_PENDING State are never set, they are
1998 * equal to MODE_NORMAL State with a condition. This function returns the real
1999 * State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 */
2001 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002002get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003{
Bram Moolenaar24959102022-05-07 20:01:16 +01002004 if (State & MODE_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00002007 {
2008 if (VIsual_select)
Bram Moolenaar24959102022-05-07 20:01:16 +01002009 return MODE_SELECT;
2010 return MODE_VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00002011 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002012 else if (finish_op)
Bram Moolenaar24959102022-05-07 20:01:16 +01002013 return MODE_OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 }
2015 return State;
2016}
2017
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002018/*
2019 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02002020 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002021 * "b" must point to the start of the file name
2022 */
2023 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002024after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002025{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02002026 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002027 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
2028}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002029
2030/*
2031 * Return TRUE if file names "f1" and "f2" are in the same directory.
2032 * "f1" may be a short name, "f2" must be a full path.
2033 */
2034 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002035same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002036{
2037 char_u ffname[MAXPATHL];
2038 char_u *t1;
2039 char_u *t2;
2040
Bram Moolenaar85a20022019-12-21 18:25:54 +01002041 // safety check
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002042 if (f1 == NULL || f2 == NULL)
2043 return FALSE;
2044
2045 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
2046 t1 = gettail_sep(ffname);
2047 t2 = gettail_sep(f2);
2048 return (t1 - ffname == t2 - f2
2049 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
2050}
2051
Bram Moolenaar7c365fb2018-06-29 20:28:31 +02002052#if defined(FEAT_SESSION) || defined(FEAT_AUTOCHDIR) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02002053 || defined(MSWIN) || defined(FEAT_GUI_GTK) \
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01002054 || defined(FEAT_NETBEANS_INTG) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 || defined(PROTO)
2056/*
2057 * Change to a file's directory.
2058 * Caller must call shorten_fnames()!
2059 * Return OK or FAIL.
2060 */
2061 int
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002062vim_chdirfile(char_u *fname, char *trigger_autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002064 char_u old_dir[MAXPATHL];
2065 char_u new_dir[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002067 if (mch_dirname(old_dir, MAXPATHL) != OK)
2068 *old_dir = NUL;
2069
2070 vim_strncpy(new_dir, fname, MAXPATHL - 1);
2071 *gettail_sep(new_dir) = NUL;
2072
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01002073 if (pathcmp((char *)old_dir, (char *)new_dir, -1) == 0)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002074 // nothing to do
zeertzjq73257142022-02-02 13:16:37 +00002075 return OK;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002076
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002077 if (trigger_autocmd != NULL)
2078 trigger_DirChangedPre((char_u *)trigger_autocmd, new_dir);
2079
zeertzjq73257142022-02-02 13:16:37 +00002080 if (mch_chdir((char *)new_dir) != 0)
2081 return FAIL;
2082
2083 if (trigger_autocmd != NULL)
2084 apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd,
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002085 new_dir, FALSE, curbuf);
zeertzjq73257142022-02-02 13:16:37 +00002086 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087}
2088#endif
2089
2090#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
2091/*
2092 * Check if "name" ends in a slash and is not a directory.
2093 * Used for systems where stat() ignores a trailing slash on a file name.
2094 * The Vim code assumes a trailing slash is only ignored for a directory.
2095 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002096 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01002097illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
2099 if (name[0] == NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002100 return FALSE; // no file name is not illegal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 if (name[strlen(name) - 1] != '/')
Bram Moolenaar85a20022019-12-21 18:25:54 +01002102 return FALSE; // no trailing slash
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 if (mch_isdir((char_u *)name))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002104 return FALSE; // trailing slash for a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 return TRUE;
2106}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002107
2108/*
2109 * Special implementation of mch_stat() for Solaris.
2110 */
2111 int
2112vim_stat(const char *name, stat_T *stp)
2113{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002114 // On Solaris stat() accepts "file/" as if it was "file". Return -1 if
2115 // the name ends in "/" and it's not a directory.
Bram Moolenaard8492792017-03-16 12:22:38 +01002116 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002117}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118#endif
2119
2120#if defined(CURSOR_SHAPE) || defined(PROTO)
2121
2122/*
2123 * Handling of cursor and mouse pointer shapes in various modes.
2124 */
2125
2126cursorentry_T shape_table[SHAPE_IDX_COUNT] =
2127{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002128 // The values will be filled in from the 'guicursor' and 'mouseshape'
2129 // defaults when Vim starts.
2130 // Adjust the SHAPE_IDX_ defines when making changes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
2132 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
2133 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
2134 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
2135 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
2136 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
2137 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
2138 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
2139 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
2140 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
2141 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
2142 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
2143 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
2144 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
2145 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
2146 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
2147 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
2148};
2149
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002150# ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151/*
2152 * Table with names for mouse shapes. Keep in sync with all the tables for
2153 * mch_set_mouse_shape()!.
2154 */
John Marriott4a1e6da2025-03-06 22:26:23 +01002155#define STRING_INIT(s) \
2156 {(char_u *)(s), STRLEN_LITERAL(s)}
2157static string_T mshape_names[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158{
John Marriott4a1e6da2025-03-06 22:26:23 +01002159 STRING_INIT("arrow"), // default, must be the first one
2160 STRING_INIT("blank"), // hidden
2161 STRING_INIT("beam"),
2162 STRING_INIT("updown"),
2163 STRING_INIT("udsizing"),
2164 STRING_INIT("leftright"),
2165 STRING_INIT("lrsizing"),
2166 STRING_INIT("busy"),
2167 STRING_INIT("no"),
2168 STRING_INIT("crosshair"),
2169 STRING_INIT("hand1"),
2170 STRING_INIT("hand2"),
2171 STRING_INIT("pencil"),
2172 STRING_INIT("question"),
2173 STRING_INIT("rightup-arrow"),
2174 STRING_INIT("up-arrow"),
2175 {NULL, 0}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176};
John Marriott4a1e6da2025-03-06 22:26:23 +01002177#undef STRING_INIT
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002178
2179# define MSHAPE_NAMES_COUNT (ARRAY_LENGTH(mshape_names) - 1)
2180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181
2182/*
2183 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
2184 * ("what" is SHAPE_MOUSE).
2185 * Returns error message for an illegal option, NULL otherwise.
2186 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002187 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002188parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189{
2190 char_u *modep;
2191 char_u *colonp;
2192 char_u *commap;
2193 char_u *slashp;
2194 char_u *p, *endp;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002195 int idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 int all_idx;
2197 int len;
2198 int i;
2199 long n;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002200 int found_ve = FALSE; // found "ve" flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 int round;
2202
2203 /*
2204 * First round: check for errors; second round: do it for real.
2205 */
2206 for (round = 1; round <= 2; ++round)
2207 {
2208 /*
2209 * Repeat for all comma separated parts.
2210 */
2211#ifdef FEAT_MOUSESHAPE
2212 if (what == SHAPE_MOUSE)
2213 modep = p_mouseshape;
2214 else
2215#endif
2216 modep = p_guicursor;
2217 while (*modep != NUL)
2218 {
2219 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01002220 commap = vim_strchr(modep, ',');
2221
2222 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002223 return e_missing_colon_2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 if (colonp == modep)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002225 return e_illegal_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 /*
2228 * Repeat for all mode's before the colon.
2229 * For the 'a' mode, we loop to handle all the modes.
2230 */
2231 all_idx = -1;
2232 while (modep < colonp || all_idx >= 0)
2233 {
2234 if (all_idx < 0)
2235 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002236 // Find the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 if (modep[1] == '-' || modep[1] == ':')
2238 len = 1;
2239 else
2240 len = 2;
2241 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
2242 all_idx = SHAPE_IDX_COUNT - 1;
2243 else
2244 {
2245 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
2246 if (STRNICMP(modep, shape_table[idx].name, len)
2247 == 0)
2248 break;
2249 if (idx == SHAPE_IDX_COUNT
2250 || (shape_table[idx].used_for & what) == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002251 return e_illegal_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
2253 found_ve = TRUE;
2254 }
2255 modep += len + 1;
2256 }
2257
2258 if (all_idx >= 0)
2259 idx = all_idx--;
2260 else if (round == 2)
2261 {
2262#ifdef FEAT_MOUSESHAPE
2263 if (what == SHAPE_MOUSE)
2264 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002265 // Set the default, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 shape_table[idx].mshape = 0;
2267 }
2268 else
2269#endif
2270 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002271 // Set the defaults, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 shape_table[idx].shape = SHAPE_BLOCK;
2273 shape_table[idx].blinkwait = 700L;
2274 shape_table[idx].blinkon = 400L;
2275 shape_table[idx].blinkoff = 250L;
2276 }
2277 }
2278
Bram Moolenaar85a20022019-12-21 18:25:54 +01002279 // Parse the part after the colon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 for (p = colonp + 1; *p && *p != ','; )
2281 {
2282#ifdef FEAT_MOUSESHAPE
2283 if (what == SHAPE_MOUSE)
2284 {
2285 for (i = 0; ; ++i)
2286 {
John Marriott4a1e6da2025-03-06 22:26:23 +01002287 if (mshape_names[i].string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 if (!VIM_ISDIGIT(*p))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002290 return e_illegal_mouseshape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (round == 2)
2292 shape_table[idx].mshape =
2293 getdigits(&p) + MSHAPE_NUMBERED;
2294 else
2295 (void)getdigits(&p);
2296 break;
2297 }
John Marriott4a1e6da2025-03-06 22:26:23 +01002298 if (STRNICMP(p, mshape_names[i].string, mshape_names[i].length) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
2300 if (round == 2)
2301 shape_table[idx].mshape = i;
John Marriott4a1e6da2025-03-06 22:26:23 +01002302 p += mshape_names[i].length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 break;
2304 }
2305 }
2306 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002307 else // if (what == SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308#endif
2309 {
2310 /*
2311 * First handle the ones with a number argument.
2312 */
2313 i = *p;
2314 len = 0;
2315 if (STRNICMP(p, "ver", 3) == 0)
2316 len = 3;
2317 else if (STRNICMP(p, "hor", 3) == 0)
2318 len = 3;
2319 else if (STRNICMP(p, "blinkwait", 9) == 0)
2320 len = 9;
2321 else if (STRNICMP(p, "blinkon", 7) == 0)
2322 len = 7;
2323 else if (STRNICMP(p, "blinkoff", 8) == 0)
2324 len = 8;
2325 if (len != 0)
2326 {
2327 p += len;
2328 if (!VIM_ISDIGIT(*p))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002329 return e_digit_expected;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 n = getdigits(&p);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002331 if (len == 3) // "ver" or "hor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
2333 if (n == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002334 return e_illegal_percentage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 if (round == 2)
2336 {
2337 if (TOLOWER_ASC(i) == 'v')
2338 shape_table[idx].shape = SHAPE_VER;
2339 else
2340 shape_table[idx].shape = SHAPE_HOR;
2341 shape_table[idx].percentage = n;
2342 }
2343 }
2344 else if (round == 2)
2345 {
2346 if (len == 9)
2347 shape_table[idx].blinkwait = n;
2348 else if (len == 7)
2349 shape_table[idx].blinkon = n;
2350 else
2351 shape_table[idx].blinkoff = n;
2352 }
2353 }
2354 else if (STRNICMP(p, "block", 5) == 0)
2355 {
2356 if (round == 2)
2357 shape_table[idx].shape = SHAPE_BLOCK;
2358 p += 5;
2359 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002360 else // must be a highlight group name then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
2362 endp = vim_strchr(p, '-');
Bram Moolenaar85a20022019-12-21 18:25:54 +01002363 if (commap == NULL) // last part
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
2365 if (endp == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002366 endp = p + STRLEN(p); // find end of part
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368 else if (endp > commap || endp == NULL)
2369 endp = commap;
2370 slashp = vim_strchr(p, '/');
2371 if (slashp != NULL && slashp < endp)
2372 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002373 // "group/langmap_group"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 i = syn_check_group(p, (int)(slashp - p));
2375 p = slashp + 1;
2376 }
2377 if (round == 2)
2378 {
2379 shape_table[idx].id = syn_check_group(p,
2380 (int)(endp - p));
2381 shape_table[idx].id_lm = shape_table[idx].id;
2382 if (slashp != NULL && slashp < endp)
2383 shape_table[idx].id = i;
2384 }
2385 p = endp;
2386 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002387 } // if (what != SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
2389 if (*p == '-')
2390 ++p;
2391 }
2392 }
2393 modep = p;
2394 if (*modep == ',')
2395 ++modep;
2396 }
2397 }
2398
Bram Moolenaar85a20022019-12-21 18:25:54 +01002399 // If the 's' flag is not given, use the 'v' cursor for 's'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 if (!found_ve)
2401 {
2402#ifdef FEAT_MOUSESHAPE
2403 if (what == SHAPE_MOUSE)
2404 {
2405 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
2406 }
2407 else
2408#endif
2409 {
2410 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
2411 shape_table[SHAPE_IDX_VE].percentage =
2412 shape_table[SHAPE_IDX_V].percentage;
2413 shape_table[SHAPE_IDX_VE].blinkwait =
2414 shape_table[SHAPE_IDX_V].blinkwait;
2415 shape_table[SHAPE_IDX_VE].blinkon =
2416 shape_table[SHAPE_IDX_V].blinkon;
2417 shape_table[SHAPE_IDX_VE].blinkoff =
2418 shape_table[SHAPE_IDX_V].blinkoff;
2419 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
2420 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
2421 }
2422 }
2423
2424 return NULL;
2425}
2426
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002427# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2428 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429/*
2430 * Return the index into shape_table[] for the current mode.
2431 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
2432 */
2433 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002434get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
2436#ifdef FEAT_MOUSESHAPE
Bram Moolenaar24959102022-05-07 20:01:16 +01002437 if (mouse && (State == MODE_HITRETURN || State == MODE_ASKMORE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
2439# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002440 int x, y;
2441 gui_mch_getmouse(&x, &y);
2442 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 return SHAPE_IDX_MOREL;
2444# endif
2445 return SHAPE_IDX_MORE;
2446 }
2447 if (mouse && drag_status_line)
2448 return SHAPE_IDX_SDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 if (mouse && drag_sep_line)
2450 return SHAPE_IDX_VDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002452 if (!mouse && State == MODE_SHOWMATCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 return SHAPE_IDX_SM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 if (State & VREPLACE_FLAG)
2455 return SHAPE_IDX_R;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (State & REPLACE_FLAG)
2457 return SHAPE_IDX_R;
Bram Moolenaar24959102022-05-07 20:01:16 +01002458 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 return SHAPE_IDX_I;
Bram Moolenaar24959102022-05-07 20:01:16 +01002460 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
2462 if (cmdline_at_end())
2463 return SHAPE_IDX_C;
2464 if (cmdline_overstrike())
2465 return SHAPE_IDX_CR;
2466 return SHAPE_IDX_CI;
2467 }
2468 if (finish_op)
2469 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 if (VIsual_active)
2471 {
2472 if (*p_sel == 'e')
2473 return SHAPE_IDX_VE;
2474 else
2475 return SHAPE_IDX_V;
2476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 return SHAPE_IDX_N;
2478}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002482static int current_mouse_shape = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
2484/*
2485 * Set the mouse shape:
2486 * If "shape" is -1, use shape depending on the current mode,
2487 * depending on the current state.
2488 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
2489 * when the mouse moves off the status or command line).
2490 */
2491 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002492update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 int new_mouse_shape;
2495
Bram Moolenaar85a20022019-12-21 18:25:54 +01002496 // Only works in GUI mode.
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002497 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 return;
2499
Bram Moolenaar85a20022019-12-21 18:25:54 +01002500 // Postpone the updating when more is to come. Speeds up executing of
2501 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 if (shape_idx == -1 && char_avail())
2503 {
2504 postponed_mouseshape = TRUE;
2505 return;
2506 }
2507
Bram Moolenaar85a20022019-12-21 18:25:54 +01002508 // When ignoring the mouse don't change shape on the statusline.
Bram Moolenaar14716812006-05-04 21:54:08 +00002509 if (*p_mouse == NUL
2510 && (shape_idx == SHAPE_IDX_CLINE
2511 || shape_idx == SHAPE_IDX_STATUS
2512 || shape_idx == SHAPE_IDX_VSEP))
2513 shape_idx = -2;
2514
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 if (shape_idx == -2
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002516 && current_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
2517 && current_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
2518 && current_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 return;
2520 if (shape_idx < 0)
2521 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
2522 else
2523 new_mouse_shape = shape_table[shape_idx].mshape;
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002524 if (new_mouse_shape != current_mouse_shape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
2526 mch_set_mouse_shape(new_mouse_shape);
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002527 current_mouse_shape = new_mouse_shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
2529 postponed_mouseshape = FALSE;
2530}
2531# endif
2532
Bram Moolenaar85a20022019-12-21 18:25:54 +01002533#endif // CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002535#if defined(FEAT_EVAL) || defined(PROTO)
2536/*
2537 * Mainly for tests: get the name of the current mouse shape.
2538 */
2539 void
2540f_getmouseshape(typval_T *argvars UNUSED, typval_T *rettv)
2541{
2542 rettv->v_type = VAR_STRING;
2543 rettv->vval.v_string = NULL;
2544# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
2545 if (current_mouse_shape >= 0
2546 && current_mouse_shape < (int)MSHAPE_NAMES_COUNT)
John Marriott4a1e6da2025-03-06 22:26:23 +01002547 rettv->vval.v_string = vim_strnsave(
2548 mshape_names[current_mouse_shape].string,
2549 mshape_names[current_mouse_shape].length);
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00002550# endif
2551}
2552#endif
2553
2554
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556/*
Bram Moolenaarf80f40a2022-08-25 16:02:23 +01002557 * Change directory to "new_dir". Search 'cdpath' for relative directory
Bram Moolenaar04e0ed12022-09-10 20:00:56 +01002558 * names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 */
2560 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002561vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 char_u *dir_name;
2564 int r;
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002565 char_u *file_to_find = NULL;
2566 char *search_ctx = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
2568 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002569 FNAME_MESS, curbuf->b_ffname, &file_to_find, &search_ctx);
2570 vim_free(file_to_find);
2571 vim_findfile_cleanup(search_ctx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 if (dir_name == NULL)
2573 return -1;
2574 r = mch_chdir((char *)dir_name);
2575 vim_free(dir_name);
2576 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577}
2578
2579/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002580 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002582 * Some systems are quite slow in obtaining the user name (Windows NT), thus
2583 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 * Returns OK or FAIL.
2585 */
2586 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002587get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002589 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 {
2591 if (mch_get_user_name(buf, len) == FAIL)
2592 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002593 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
2595 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002596 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 return OK;
2598}
2599
Bram Moolenaar7ce5b2b2022-05-16 19:40:59 +01002600#if defined(EXITFREE) || defined(PROTO)
Yegappan Lakshmanancbae5802021-08-06 21:51:55 +02002601/*
2602 * Free the memory allocated by get_user_name()
2603 */
2604 void
2605free_username(void)
2606{
2607 vim_free(username);
2608}
Dominique Pelle748b3082022-01-08 12:41:16 +00002609#endif
Yegappan Lakshmanancbae5802021-08-06 21:51:55 +02002610
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611#ifndef HAVE_QSORT
2612/*
2613 * Our own qsort(), for systems that don't have it.
2614 * It's simple and slow. From the K&R C book.
2615 */
2616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002617qsort(
2618 void *base,
2619 size_t elm_count,
2620 size_t elm_size,
2621 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622{
2623 char_u *buf;
2624 char_u *p1;
2625 char_u *p2;
2626 int i, j;
2627 int gap;
2628
Bram Moolenaar964b3742019-05-24 18:54:09 +02002629 buf = alloc(elm_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 if (buf == NULL)
2631 return;
2632
2633 for (gap = elm_count / 2; gap > 0; gap /= 2)
2634 for (i = gap; i < elm_count; ++i)
2635 for (j = i - gap; j >= 0; j -= gap)
2636 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002637 // Compare the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 p1 = (char_u *)base + j * elm_size;
2639 p2 = (char_u *)base + (j + gap) * elm_size;
2640 if ((*cmp)((void *)p1, (void *)p2) <= 0)
2641 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002642 // Exchange the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 mch_memmove(buf, p1, elm_size);
2644 mch_memmove(p1, p2, elm_size);
2645 mch_memmove(p2, buf, elm_size);
2646 }
2647
2648 vim_free(buf);
2649}
2650#endif
2651
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 * The putenv() implementation below comes from the "screen" program.
2654 * Included with permission from Juergen Weigert.
2655 * See pty.c for the copyright notice.
2656 */
2657
2658/*
2659 * putenv -- put value into environment
2660 *
2661 * Usage: i = putenv (string)
2662 * int i;
2663 * char *string;
2664 *
2665 * where string is of the form <name>=<value>.
2666 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
2667 *
2668 * Putenv may need to add a new name into the environment, or to
2669 * associate a value longer than the current value with a particular
2670 * name. So, to make life simpler, putenv() copies your entire
2671 * environment into the heap (i.e. malloc()) from the stack
2672 * (i.e. where it resides when your process is initiated) the first
2673 * time you call it.
2674 *
2675 * (history removed, not very interesting. See the "screen" sources.)
2676 */
2677
2678#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
2679
Bram Moolenaar85a20022019-12-21 18:25:54 +01002680#define EXTRASIZE 5 // increment to add to env. size
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
Bram Moolenaar85a20022019-12-21 18:25:54 +01002682static int envsize = -1; // current size of environment
2683extern char **environ; // the global which is your env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
Bram Moolenaar85a20022019-12-21 18:25:54 +01002685static int findenv(char *name); // look for a name in the env.
2686static int newenv(void); // copy env. from stack to heap
2687static int moreenv(void); // incr. size of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688
2689 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002690putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 int i;
2693 char *p;
2694
2695 if (envsize < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002696 { // first time putenv called
2697 if (newenv() < 0) // copy env. to heap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 return -1;
2699 }
2700
Bram Moolenaar85a20022019-12-21 18:25:54 +01002701 i = findenv((char *)string); // look for name in environment
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
2703 if (i < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002704 { // name must be added
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 for (i = 0; environ[i]; i++);
2706 if (i >= (envsize - 1))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002707 { // need new slot
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (moreenv() < 0)
2709 return -1;
2710 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002711 p = alloc(strlen(string) + 1);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002712 if (p == NULL) // not enough core
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 return -1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002714 environ[i + 1] = 0; // new end of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
2716 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002717 { // name already in env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 p = vim_realloc(environ[i], strlen(string) + 1);
2719 if (p == NULL)
2720 return -1;
2721 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002722 sprintf(p, "%s", string); // copy into env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 environ[i] = p;
2724
2725 return 0;
2726}
2727
2728 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002729findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730{
2731 char *namechar, *envchar;
2732 int i, found;
2733
2734 found = 0;
2735 for (i = 0; environ[i] && !found; i++)
2736 {
2737 envchar = environ[i];
2738 namechar = name;
2739 while (*namechar && *namechar != '=' && (*namechar == *envchar))
2740 {
2741 namechar++;
2742 envchar++;
2743 }
2744 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
2745 }
2746 return found ? i - 1 : -1;
2747}
2748
2749 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002750newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 char **env, *elem;
2753 int i, esize;
2754
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 for (i = 0; environ[i]; i++)
2756 ;
Bram Moolenaard0573012017-10-28 21:11:06 +02002757
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 esize = i + EXTRASIZE + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002759 env = ALLOC_MULT(char *, esize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (env == NULL)
2761 return -1;
2762
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 for (i = 0; environ[i]; i++)
2764 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002765 elem = alloc(strlen(environ[i]) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 if (elem == NULL)
2767 return -1;
2768 env[i] = elem;
2769 strcpy(elem, environ[i]);
2770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
2772 env[i] = 0;
2773 environ = env;
2774 envsize = esize;
2775 return 0;
2776}
2777
2778 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002779moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780{
2781 int esize;
2782 char **env;
2783
2784 esize = envsize + EXTRASIZE;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002785 env = vim_realloc((char *)environ, esize * sizeof (*env));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (env == 0)
2787 return -1;
2788 environ = env;
2789 envsize = esize;
2790 return 0;
2791}
2792
2793# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02002794/*
2795 * Used for mch_getenv() for Mac.
2796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002798vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
2800 int i;
2801 char_u *p;
2802
2803 if (envsize < 0)
2804 return NULL;
2805
2806 i = findenv((char *)string);
2807
2808 if (i < 0)
2809 return NULL;
2810
2811 p = vim_strchr((char_u *)environ[i], '=');
2812 return (p + 1);
2813}
2814# endif
2815
Bram Moolenaar85a20022019-12-21 18:25:54 +01002816#endif // !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002817
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002818#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002819/*
2820 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
2821 * rights to write into.
2822 */
2823 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002824filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002825{
2826 int retval = 0;
2827#if defined(UNIX) || defined(VMS)
2828 int perm = 0;
2829#endif
2830
2831#if defined(UNIX) || defined(VMS)
2832 perm = mch_getperm(fname);
2833#endif
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002834 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01002835# ifdef MSWIN
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002836 mch_writable(fname) &&
2837# else
2838# if defined(UNIX) || defined(VMS)
2839 (perm & 0222) &&
2840# endif
2841# endif
2842 mch_access((char *)fname, W_OK) == 0
2843 )
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002844 {
2845 ++retval;
2846 if (mch_isdir(fname))
2847 ++retval;
2848 }
2849 return retval;
2850}
2851#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00002852
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002853#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
2854/*
2855 * Read 2 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002856 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002857 */
2858 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002859get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002860{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002861 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002862
2863 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002864 if (n == EOF) return -1;
2865 c = getc(fd);
2866 if (c == EOF) return -1;
2867 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002868}
2869
2870/*
2871 * Read 3 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002872 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002873 */
2874 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002875get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002876{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002877 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002878
2879 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002880 if (n == EOF) return -1;
2881 c = getc(fd);
2882 if (c == EOF) return -1;
2883 n = (n << 8) + c;
2884 c = getc(fd);
2885 if (c == EOF) return -1;
2886 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002887}
2888
2889/*
2890 * Read 4 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002891 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002892 */
2893 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002894get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002895{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002896 int c;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002897 // Use unsigned rather than int otherwise result is undefined
2898 // when left-shift sets the MSB.
Bram Moolenaar95235e62013-09-08 16:07:07 +02002899 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002900
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002901 c = getc(fd);
2902 if (c == EOF) return -1;
2903 n = (unsigned)c;
2904 c = getc(fd);
2905 if (c == EOF) return -1;
2906 n = (n << 8) + (unsigned)c;
2907 c = getc(fd);
2908 if (c == EOF) return -1;
2909 n = (n << 8) + (unsigned)c;
2910 c = getc(fd);
2911 if (c == EOF) return -1;
2912 n = (n << 8) + (unsigned)c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02002913 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002914}
2915
2916/*
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002917 * Read a string of length "cnt" from "fd" into allocated memory.
2918 * Returns NULL when out of memory or unable to read that many bytes.
2919 */
2920 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002921read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002922{
2923 char_u *str;
2924 int i;
2925 int c;
2926
Bram Moolenaar85a20022019-12-21 18:25:54 +01002927 // allocate memory
Bram Moolenaar964b3742019-05-24 18:54:09 +02002928 str = alloc(cnt + 1);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002929 if (str == NULL)
2930 return NULL;
2931
2932 // Read the string. Quit when running into the EOF.
2933 for (i = 0; i < cnt; ++i)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002934 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002935 c = getc(fd);
2936 if (c == EOF)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002937 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002938 vim_free(str);
2939 return NULL;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002940 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002941 str[i] = c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002942 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002943 str[i] = NUL;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002944 return str;
2945}
2946
2947/*
2948 * Write a number to file "fd", MSB first, in "len" bytes.
2949 */
2950 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002951put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002952{
2953 int i;
2954
2955 for (i = len - 1; i >= 0; --i)
2956 if (putc((int)(nr >> (i * 8)), fd) == EOF)
2957 return FAIL;
2958 return OK;
2959}
2960
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002961#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01002962
Bram Moolenaar85a20022019-12-21 18:25:54 +01002963#ifndef PROTO // proto is defined in vim.h
Bram Moolenaar51628222016-12-01 23:03:28 +01002964# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002965/*
2966 * Return time in msec since "start_tv".
2967 */
2968 long
2969elapsed(struct timeval *start_tv)
2970{
2971 struct timeval now_tv;
2972
2973 gettimeofday(&now_tv, NULL);
2974 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
2975 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
2976}
Bram Moolenaar51628222016-12-01 23:03:28 +01002977# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002978
Bram Moolenaar51628222016-12-01 23:03:28 +01002979# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002980/*
2981 * Return time in msec since "start_tick".
2982 */
2983 long
2984elapsed(DWORD start_tick)
2985{
2986 DWORD now = GetTickCount();
2987
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002988 return (long)now - (long)start_tick;
2989}
Bram Moolenaar51628222016-12-01 23:03:28 +01002990# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002991#endif
Bram Moolenaar20608922018-04-21 22:30:08 +02002992
2993#if defined(FEAT_JOB_CHANNEL) \
2994 || (defined(UNIX) && (!defined(USE_SYSTEM) \
2995 || (defined(FEAT_GUI) && defined(FEAT_TERMINAL)))) \
2996 || defined(PROTO)
2997/*
2998 * Parse "cmd" and put the white-separated parts in "argv".
2999 * "argv" is an allocated array with "argc" entries and room for 4 more.
3000 * Returns FAIL when out of memory.
3001 */
3002 int
3003mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
3004{
3005 int i;
3006 char_u *p, *d;
3007 int inquote;
3008
3009 /*
3010 * Do this loop twice:
3011 * 1: find number of arguments
3012 * 2: separate them and build argv[]
3013 */
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003014 for (i = 1; i <= 2; ++i)
Bram Moolenaar20608922018-04-21 22:30:08 +02003015 {
3016 p = skipwhite(cmd);
3017 inquote = FALSE;
3018 *argc = 0;
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003019 while (*p != NUL)
Bram Moolenaar20608922018-04-21 22:30:08 +02003020 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003021 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003022 (*argv)[*argc] = (char *)p;
3023 ++*argc;
3024 d = p;
3025 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
3026 {
3027 if (p[0] == '"')
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02003028 // quotes surrounding an argument and are dropped
Bram Moolenaar20608922018-04-21 22:30:08 +02003029 inquote = !inquote;
3030 else
3031 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02003032 if (rem_backslash(p))
Bram Moolenaar20608922018-04-21 22:30:08 +02003033 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02003034 // First pass: skip over "\ " and "\"".
3035 // Second pass: Remove the backslash.
Bram Moolenaar20608922018-04-21 22:30:08 +02003036 ++p;
3037 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003038 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003039 *d++ = *p;
3040 }
3041 ++p;
3042 }
3043 if (*p == NUL)
3044 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003045 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003046 *d++ = NUL;
3047 break;
3048 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01003049 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02003050 *d++ = NUL;
3051 p = skipwhite(p + 1);
3052 }
3053 if (*argv == NULL)
3054 {
3055 if (use_shcf)
3056 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003057 // Account for possible multiple args in p_shcf.
Bram Moolenaar20608922018-04-21 22:30:08 +02003058 p = p_shcf;
3059 for (;;)
3060 {
3061 p = skiptowhite(p);
3062 if (*p == NUL)
3063 break;
3064 ++*argc;
3065 p = skipwhite(p);
3066 }
3067 }
3068
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003069 *argv = ALLOC_MULT(char *, *argc + 4);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003070 if (*argv == NULL) // out of memory
Bram Moolenaar20608922018-04-21 22:30:08 +02003071 return FAIL;
3072 }
3073 }
3074 return OK;
3075}
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003076
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003077/*
3078 * Build "argv[argc]" from the string "cmd".
3079 * "argv[argc]" is set to NULL;
3080 * Return FAIL when out of memory.
3081 */
3082 int
3083build_argv_from_string(char_u *cmd, char ***argv, int *argc)
3084{
3085 char_u *cmd_copy;
3086 int i;
3087
Bram Moolenaar85a20022019-12-21 18:25:54 +01003088 // Make a copy, parsing will modify "cmd".
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003089 cmd_copy = vim_strsave(cmd);
3090 if (cmd_copy == NULL
3091 || mch_parse_cmd(cmd_copy, FALSE, argv, argc) == FAIL)
3092 {
3093 vim_free(cmd_copy);
3094 return FAIL;
3095 }
3096 for (i = 0; i < *argc; i++)
3097 (*argv)[i] = (char *)vim_strsave((char_u *)(*argv)[i]);
3098 (*argv)[*argc] = NULL;
3099 vim_free(cmd_copy);
3100 return OK;
3101}
3102
Dominique Pellee8741a72022-01-17 11:23:45 +00003103# if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003104/*
3105 * Build "argv[argc]" from the list "l".
3106 * "argv[argc]" is set to NULL;
3107 * Return FAIL when out of memory.
3108 */
3109 int
3110build_argv_from_list(list_T *l, char ***argv, int *argc)
3111{
3112 listitem_T *li;
3113 char_u *s;
3114
Bram Moolenaar85a20022019-12-21 18:25:54 +01003115 // Pass argv[] to mch_call_shell().
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003116 *argv = ALLOC_MULT(char *, l->lv_len + 1);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003117 if (*argv == NULL)
3118 return FAIL;
3119 *argc = 0;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003120 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003122 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003123 if (s == NULL)
3124 {
3125 int i;
3126
3127 for (i = 0; i < *argc; ++i)
Bram Moolenaardf195602020-04-13 18:13:33 +02003128 VIM_CLEAR((*argv)[i]);
Bram Moolenaar7c25a7c2021-10-05 19:19:35 +01003129 (*argv)[0] = NULL;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02003130 return FAIL;
3131 }
3132 (*argv)[*argc] = (char *)vim_strsave(s);
3133 *argc += 1;
3134 }
3135 (*argv)[*argc] = NULL;
3136 return OK;
3137}
3138# endif
Bram Moolenaar20608922018-04-21 22:30:08 +02003139#endif
Bram Moolenaar57da6982019-09-13 22:30:11 +02003140
3141/*
3142 * Change the behavior of vterm.
3143 * 0: As usual.
3144 * 1: Windows 10 version 1809
3145 * The bug causes unstable handling of ambiguous width character.
Bram Moolenaar36e7a822019-11-13 21:49:24 +01003146 * 2: Windows 10 version 1903 & 1909
Bram Moolenaar57da6982019-09-13 22:30:11 +02003147 * Use the wrong result because each result is different.
3148 * 3: Windows 10 insider preview (current latest logic)
3149 */
3150 int
3151get_special_pty_type(void)
3152{
3153#ifdef MSWIN
3154 return get_conpty_type();
3155#else
3156 return 0;
3157#endif
3158}
John Marriott78d742a2024-04-02 20:26:01 +02003159
3160// compare two keyvalue_T structs by case sensitive value
3161 int
3162cmp_keyvalue_value(const void *a, const void *b)
3163{
3164 keyvalue_T *kv1 = (keyvalue_T *)a;
3165 keyvalue_T *kv2 = (keyvalue_T *)b;
3166
John Marriott8d4477e2024-11-02 15:59:01 +01003167 return STRCMP(kv1->value.string, kv2->value.string);
John Marriott78d742a2024-04-02 20:26:01 +02003168}
3169
3170// compare two keyvalue_T structs by value with length
3171 int
3172cmp_keyvalue_value_n(const void *a, const void *b)
3173{
3174 keyvalue_T *kv1 = (keyvalue_T *)a;
3175 keyvalue_T *kv2 = (keyvalue_T *)b;
3176
John Marriott8d4477e2024-11-02 15:59:01 +01003177 return STRNCMP(kv1->value.string, kv2->value.string, MAX(kv1->value.length,
3178 kv2->value.length));
John Marriott78d742a2024-04-02 20:26:01 +02003179}
3180
Christian Brabandtfa16e432024-09-04 22:10:34 +02003181// compare two keyvalue_T structs by case insensitive value
3182 int
3183cmp_keyvalue_value_i(const void *a, const void *b)
3184{
3185 keyvalue_T *kv1 = (keyvalue_T *)a;
3186 keyvalue_T *kv2 = (keyvalue_T *)b;
3187
John Marriott8d4477e2024-11-02 15:59:01 +01003188 return STRICMP(kv1->value.string, kv2->value.string);
Christian Brabandtfa16e432024-09-04 22:10:34 +02003189}
3190
Christian Brabandt84e31752024-09-02 09:59:18 +02003191// compare two keyvalue_T structs by case insensitive ASCII value
John Marriott8d4477e2024-11-02 15:59:01 +01003192// with value.length
John Marriott78d742a2024-04-02 20:26:01 +02003193 int
3194cmp_keyvalue_value_ni(const void *a, const void *b)
3195{
3196 keyvalue_T *kv1 = (keyvalue_T *)a;
3197 keyvalue_T *kv2 = (keyvalue_T *)b;
3198
John Marriott8d4477e2024-11-02 15:59:01 +01003199 return vim_strnicmp_asc((char *)kv1->value.string,
3200 (char *)kv2->value.string, MAX(kv1->value.length,
3201 kv2->value.length));
John Marriott78d742a2024-04-02 20:26:01 +02003202}
3203