blob: e03ca4936dca608d10a2e519a90a6870214b349d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc2.c: Various functions.
12 */
13#include "vim.h"
14
Bram Moolenaar85a20022019-12-21 18:25:54 +010015static char_u *username = NULL; // cached result of mch_get_user_name()
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000016
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010017static int coladvance2(pos_T *pos, int addspaces, int finetune, colnr_T wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
19/*
20 * Return TRUE if in the current mode we need to use virtual.
21 */
22 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010023virtual_active(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024{
Gary Johnson53ba05b2021-07-26 22:19:10 +020025 unsigned int cur_ve_flags = get_ve_flags();
26
Bram Moolenaar85a20022019-12-21 18:25:54 +010027 // While an operator is being executed we return "virtual_op", because
28 // VIsual_active has already been reset, thus we can't check for "block"
29 // being used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000030 if (virtual_op != MAYBE)
31 return virtual_op;
Gary Johnson53ba05b2021-07-26 22:19:10 +020032 return (cur_ve_flags == VE_ALL
Bram Moolenaar24959102022-05-07 20:01:16 +010033 || ((cur_ve_flags & VE_BLOCK) && VIsual_active
34 && VIsual_mode == Ctrl_V)
35 || ((cur_ve_flags & VE_INSERT) && (State & MODE_INSERT)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000036}
37
38/*
39 * Get the screen position of the cursor.
40 */
41 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010042getviscol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000043{
44 colnr_T x;
45
46 getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL);
47 return (int)x;
48}
49
50/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +000051 * Go to column "wcol", and add/insert white space as necessary to get the
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 * cursor in that column.
53 * The caller must have saved the cursor line for undo!
54 */
55 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010056coladvance_force(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000057{
58 int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
59
60 if (wcol == MAXCOL)
61 curwin->w_valid &= ~VALID_VIRTCOL;
62 else
63 {
Bram Moolenaar85a20022019-12-21 18:25:54 +010064 // Virtcol is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 curwin->w_valid |= VALID_VIRTCOL;
66 curwin->w_virtcol = wcol;
67 }
68 return rc;
69}
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71/*
Bram Moolenaar977239e2019-01-11 16:16:01 +010072 * Get the screen position of character col with a coladd in the cursor line.
73 */
74 int
Bram Moolenaarfc3abf42019-01-24 15:54:21 +010075getviscol2(colnr_T col, colnr_T coladd UNUSED)
Bram Moolenaar977239e2019-01-11 16:16:01 +010076{
77 colnr_T x;
78 pos_T pos;
79
80 pos.lnum = curwin->w_cursor.lnum;
81 pos.col = col;
Bram Moolenaar977239e2019-01-11 16:16:01 +010082 pos.coladd = coladd;
Bram Moolenaar977239e2019-01-11 16:16:01 +010083 getvvcol(curwin, &pos, &x, NULL, NULL);
84 return (int)x;
85}
86
87/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 * Try to advance the Cursor to the specified screen column.
89 * If virtual editing: fine tune the cursor position.
90 * Note that all virtual positions off the end of a line should share
91 * a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)),
92 * beginning at coladd 0.
93 *
94 * return OK if desired column is reached, FAIL if not
95 */
96 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010097coladvance(colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000098{
99 int rc = getvpos(&curwin->w_cursor, wcol);
100
101 if (wcol == MAXCOL || rc == FAIL)
102 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaardfccaf02004-12-31 20:56:11 +0000103 else if (*ml_get_cursor() != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100105 // Virtcol is valid when not on a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 curwin->w_valid |= VALID_VIRTCOL;
107 curwin->w_virtcol = wcol;
108 }
109 return rc;
110}
111
112/*
113 * Return in "pos" the position of the cursor advanced to screen column "wcol".
114 * return OK if desired column is reached, FAIL if not
115 */
116 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100117getvpos(pos_T *pos, colnr_T wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 return coladvance2(pos, FALSE, virtual_active(), wcol);
120}
121
122 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100123coladvance2(
124 pos_T *pos,
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200125 int addspaces, // change the text to achieve our goal?
126 int finetune, // change char offset for the exact column
127 colnr_T wcol_arg) // column to move to (can be negative)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128{
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200129 colnr_T wcol = wcol_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 int idx;
131 char_u *ptr;
132 char_u *line;
133 colnr_T col = 0;
134 int csize = 0;
135 int one_more;
136#ifdef FEAT_LINEBREAK
137 int head = 0;
138#endif
139
Bram Moolenaar24959102022-05-07 20:01:16 +0100140 one_more = (State & MODE_INSERT)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000141 || restart_edit != NUL
Bram Moolenaarefd2bf12006-03-16 21:41:35 +0000142 || (VIsual_active && *p_sel != 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +0200143 || ((get_ve_flags() & VE_ONEMORE) && wcol < MAXCOL);
Bram Moolenaara1381de2009-11-03 15:44:21 +0000144 line = ml_get_buf(curbuf, pos->lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
146 if (wcol >= MAXCOL)
147 {
148 idx = (int)STRLEN(line) - 1 + one_more;
149 col = wcol;
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 if ((addspaces || finetune) && !VIsual_active)
152 {
153 curwin->w_curswant = linetabsize(line) + one_more;
154 if (curwin->w_curswant > 0)
155 --curwin->w_curswant;
156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 }
158 else
159 {
Bram Moolenaar02631462017-09-22 15:20:32 +0200160 int width = curwin->w_width - win_col_off(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161
Bram Moolenaarebefac62005-12-28 22:39:57 +0000162 if (finetune
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 && curwin->w_p_wrap
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 && curwin->w_width != 0
Bram Moolenaar02f86942021-08-17 22:14:29 +0200165 && wcol >= (colnr_T)width
166 && width > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 {
168 csize = linetabsize(line);
169 if (csize > 0)
170 csize--;
171
Bram Moolenaarebefac62005-12-28 22:39:57 +0000172 if (wcol / width > (colnr_T)csize / width
Bram Moolenaar24959102022-05-07 20:01:16 +0100173 && ((State & MODE_INSERT) == 0 || (int)wcol > csize + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100175 // In case of line wrapping don't move the cursor beyond the
176 // right screen edge. In Insert mode allow going just beyond
177 // the last character (like what happens when typing and
178 // reaching the right window edge).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 wcol = (csize / width + 1) * width - 1;
180 }
181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 ptr = line;
184 while (col <= wcol && *ptr != NUL)
185 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100186 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#ifdef FEAT_LINEBREAK
Bram Moolenaar597a4222014-06-25 14:39:50 +0200188 csize = win_lbr_chartabsize(curwin, line, ptr, col, &head);
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100189 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200191 csize = lbr_chartabsize_adv(line, &ptr, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192#endif
193 col += csize;
194 }
195 idx = (int)(ptr - line);
196 /*
197 * Handle all the special cases. The virtual_active() check
198 * is needed to ensure that a virtual position off the end of
199 * a line has the correct indexing. The one_more comparison
200 * replaces an explicit add of one_more later on.
201 */
202 if (col > wcol || (!virtual_active() && one_more == 0))
203 {
204 idx -= 1;
205# ifdef FEAT_LINEBREAK
Bram Moolenaar85a20022019-12-21 18:25:54 +0100206 // Don't count the chars from 'showbreak'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 csize -= head;
208# endif
209 col -= csize;
210 }
211
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 if (virtual_active()
213 && addspaces
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200214 && wcol >= 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 && ((col != wcol && col != wcol + 1) || csize > 1))
216 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100217 // 'virtualedit' is set: The difference between wcol and col is
218 // filled with spaces.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
220 if (line[idx] == NUL)
221 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 // Append spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 int correct = wcol - col;
224 char_u *newline = alloc(idx + correct + 1);
225 int t;
226
227 if (newline == NULL)
228 return FAIL;
229
230 for (t = 0; t < idx; ++t)
231 newline[t] = line[t];
232
233 for (t = 0; t < correct; ++t)
234 newline[t + idx] = ' ';
235
236 newline[idx + correct] = NUL;
237
238 ml_replace(pos->lnum, newline, FALSE);
239 changed_bytes(pos->lnum, (colnr_T)idx);
240 idx += correct;
241 col = wcol;
242 }
243 else
244 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100245 // Break a tab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 int linelen = (int)STRLEN(line);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100247 int correct = wcol - col - csize + 1; // negative!!
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000248 char_u *newline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 int t, s = 0;
250 int v;
251
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000252 if (-correct > csize)
253 return FAIL;
254
255 newline = alloc(linelen + csize);
256 if (newline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 return FAIL;
258
259 for (t = 0; t < linelen; t++)
260 {
261 if (t != idx)
262 newline[s++] = line[t];
263 else
264 for (v = 0; v < csize; v++)
265 newline[s++] = ' ';
266 }
267
268 newline[linelen + csize - 1] = NUL;
269
270 ml_replace(pos->lnum, newline, FALSE);
271 changed_bytes(pos->lnum, idx);
272 idx += (csize - 1 + correct);
273 col += correct;
274 }
275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 }
277
278 if (idx < 0)
279 pos->col = 0;
280 else
281 pos->col = idx;
282
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 pos->coladd = 0;
284
285 if (finetune)
286 {
287 if (wcol == MAXCOL)
288 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100289 // The width of the last character is used to set coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 if (!one_more)
291 {
292 colnr_T scol, ecol;
293
294 getvcol(curwin, pos, &scol, NULL, &ecol);
295 pos->coladd = ecol - scol;
296 }
297 }
298 else
299 {
300 int b = (int)wcol - (int)col;
301
Bram Moolenaar85a20022019-12-21 18:25:54 +0100302 // The difference between wcol and col is used to set coladd.
Bram Moolenaar02631462017-09-22 15:20:32 +0200303 if (b > 0 && b < (MAXCOL - 2 * curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 pos->coladd = b;
305
306 col += b;
307 }
308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309
Bram Moolenaar85a20022019-12-21 18:25:54 +0100310 // prevent from moving onto a trail byte
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200312 mb_adjustpos(curbuf, pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
Bram Moolenaarceba3dd2019-10-12 16:12:54 +0200314 if (wcol < 0 || col < wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 return FAIL;
316 return OK;
317}
318
319/*
Bram Moolenaar446cb832008-06-24 21:56:24 +0000320 * Increment the cursor position. See inc() for return values.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 */
322 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100323inc_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324{
325 return inc(&curwin->w_cursor);
326}
327
Bram Moolenaar446cb832008-06-24 21:56:24 +0000328/*
329 * Increment the line pointer "lp" crossing line boundaries as necessary.
330 * Return 1 when going to the next line.
331 * Return 2 when moving forward onto a NUL at the end of the line).
332 * Return -1 when at the end of file.
333 * Return 0 otherwise.
334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100336inc(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100338 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339
Bram Moolenaar85a20022019-12-21 18:25:54 +0100340 // when searching position may be set to end of a line
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100341 if (lp->col != MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100343 p = ml_get_pos(lp);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100344 if (*p != NUL) // still within line, move to next char (may be NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100346 if (has_mbyte)
347 {
348 int l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100350 lp->col += l;
351 return ((p[l] != NUL) ? 0 : 2);
352 }
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100353 lp->col++;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100354 lp->coladd = 0;
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100355 return ((p[1] != NUL) ? 0 : 2);
356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100358 if (lp->lnum != curbuf->b_ml.ml_line_count) // there is a next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 {
360 lp->col = 0;
361 lp->lnum++;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 lp->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 return 1;
364 }
365 return -1;
366}
367
368/*
369 * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
370 */
371 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100372incl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
374 int r;
375
376 if ((r = inc(lp)) >= 1 && lp->col)
377 r = inc(lp);
378 return r;
379}
380
381/*
382 * dec(p)
383 *
384 * Decrement the line pointer 'p' crossing line boundaries as necessary.
385 * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
386 */
387 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100388dec_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100390 return dec(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391}
392
393 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100394dec(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395{
396 char_u *p;
397
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 lp->coladd = 0;
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100399 if (lp->col == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100401 // past end of line
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100402 p = ml_get(lp->lnum);
403 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100404 if (has_mbyte)
405 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100406 return 0;
407 }
408
409 if (lp->col > 0)
410 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100411 // still within line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 lp->col--;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 if (has_mbyte)
414 {
415 p = ml_get(lp->lnum);
416 lp->col -= (*mb_head_off)(p, p + lp->col);
417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 return 0;
419 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100420
421 if (lp->lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100423 // there is a prior line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 lp->lnum--;
425 p = ml_get(lp->lnum);
426 lp->col = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 if (has_mbyte)
428 lp->col -= (*mb_head_off)(p, p + lp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 return 1;
430 }
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100431
Bram Moolenaar85a20022019-12-21 18:25:54 +0100432 // at start of file
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100433 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434}
435
436/*
437 * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
438 */
439 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100440decl(pos_T *lp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
442 int r;
443
444 if ((r = dec(lp)) == 1 && lp->col)
445 r = dec(lp);
446 return r;
447}
448
449/*
Bram Moolenaar64486672010-05-16 15:46:46 +0200450 * Get the line number relative to the current cursor position, i.e. the
451 * difference between line number and cursor position. Only look for lines that
452 * can be visible, folded lines don't count.
453 */
454 linenr_T
Bram Moolenaar9b578142016-01-30 19:39:49 +0100455get_cursor_rel_lnum(
456 win_T *wp,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100457 linenr_T lnum) // line number to get the result for
Bram Moolenaar64486672010-05-16 15:46:46 +0200458{
459 linenr_T cursor = wp->w_cursor.lnum;
460 linenr_T retval = 0;
461
462#ifdef FEAT_FOLDING
463 if (hasAnyFolding(wp))
464 {
465 if (lnum > cursor)
466 {
467 while (lnum > cursor)
468 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100469 (void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100470 // if lnum and cursor are in the same fold,
471 // now lnum <= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200472 if (lnum > cursor)
473 retval++;
474 lnum--;
475 }
476 }
477 else if (lnum < cursor)
478 {
479 while (lnum < cursor)
480 {
Bram Moolenaar0bd7b3f2013-12-14 12:48:58 +0100481 (void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100482 // if lnum and cursor are in the same fold,
483 // now lnum >= cursor
Bram Moolenaar64486672010-05-16 15:46:46 +0200484 if (lnum < cursor)
485 retval--;
486 lnum++;
487 }
488 }
Bram Moolenaar85a20022019-12-21 18:25:54 +0100489 // else if (lnum == cursor)
490 // retval = 0;
Bram Moolenaar64486672010-05-16 15:46:46 +0200491 }
492 else
493#endif
494 retval = lnum - cursor;
495
496 return retval;
497}
498
499/*
Bram Moolenaard5824ce2016-09-04 20:35:01 +0200500 * Make sure "pos.lnum" and "pos.col" are valid in "buf".
501 * This allows for the col to be on the NUL byte.
502 */
503 void
504check_pos(buf_T *buf, pos_T *pos)
505{
506 char_u *line;
507 colnr_T len;
508
509 if (pos->lnum > buf->b_ml.ml_line_count)
510 pos->lnum = buf->b_ml.ml_line_count;
511
512 if (pos->col > 0)
513 {
514 line = ml_get_buf(buf, pos->lnum, FALSE);
515 len = (colnr_T)STRLEN(line);
516 if (pos->col > len)
517 pos->col = len;
518 }
519}
520
521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 * Make sure curwin->w_cursor.lnum is valid.
523 */
524 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100525check_cursor_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526{
527 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
528 {
529#ifdef FEAT_FOLDING
Bram Moolenaar85a20022019-12-21 18:25:54 +0100530 // If there is a closed fold at the end of the file, put the cursor in
531 // its first line. Otherwise in the last line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (!hasFolding(curbuf->b_ml.ml_line_count,
533 &curwin->w_cursor.lnum, NULL))
534#endif
535 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
536 }
537 if (curwin->w_cursor.lnum <= 0)
538 curwin->w_cursor.lnum = 1;
539}
540
541/*
542 * Make sure curwin->w_cursor.col is valid.
543 */
544 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100545check_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200547 check_cursor_col_win(curwin);
548}
549
550/*
551 * Make sure win->w_cursor.col is valid.
552 */
553 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100554check_cursor_col_win(win_T *win)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200555{
Gary Johnson53ba05b2021-07-26 22:19:10 +0200556 colnr_T len;
557 colnr_T oldcol = win->w_cursor.col;
558 colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
559 unsigned int cur_ve_flags = get_ve_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200561 len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 if (len == 0)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200563 win->w_cursor.col = 0;
564 else if (win->w_cursor.col >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100566 // Allow cursor past end-of-line when:
567 // - in Insert mode or restarting Insert mode
568 // - in Visual mode and 'selection' isn't "old"
569 // - 'virtualedit' is set
Bram Moolenaar24959102022-05-07 20:01:16 +0100570 if ((State & MODE_INSERT) || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 || (VIsual_active && *p_sel != 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +0200572 || (cur_ve_flags & VE_ONEMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 || virtual_active())
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200574 win->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 else
Bram Moolenaar87c19962007-04-26 08:54:21 +0000576 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200577 win->w_cursor.col = len - 1;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100578 // Move the cursor to the head byte.
Bram Moolenaar87c19962007-04-26 08:54:21 +0000579 if (has_mbyte)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200580 mb_adjustpos(win->w_buffer, &win->w_cursor);
Bram Moolenaar87c19962007-04-26 08:54:21 +0000581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200583 else if (win->w_cursor.col < 0)
584 win->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
Bram Moolenaar85a20022019-12-21 18:25:54 +0100586 // If virtual editing is on, we can leave the cursor on the old position,
587 // only we must set it to virtual. But don't do it when at the end of the
588 // line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 if (oldcol == MAXCOL)
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200590 win->w_cursor.coladd = 0;
Gary Johnson53ba05b2021-07-26 22:19:10 +0200591 else if (cur_ve_flags == VE_ALL)
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000592 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200593 if (oldcoladd > win->w_cursor.col)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200594 {
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200595 win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
Bram Moolenaard41babe2017-08-30 17:01:35 +0200596
Bram Moolenaar85a20022019-12-21 18:25:54 +0100597 // Make sure that coladd is not more than the char width.
598 // Not for the last character, coladd is then used when the cursor
599 // is actually after the last character.
Bram Moolenaarfe154992022-03-22 20:42:12 +0000600 if (win->w_cursor.col + 1 < len)
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200601 {
602 int cs, ce;
603
Bram Moolenaar9aa15692017-08-19 15:05:32 +0200604 getvcol(win, &win->w_cursor, &cs, NULL, &ce);
605 if (win->w_cursor.coladd > ce - cs)
606 win->w_cursor.coladd = ce - cs;
607 }
608 }
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000609 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100610 // avoid weird number when there is a miscalculation or overflow
Bram Moolenaar03a807a2011-07-07 15:08:58 +0200611 win->w_cursor.coladd = 0;
Bram Moolenaar552c8a52009-03-11 16:29:20 +0000612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613}
614
615/*
616 * make sure curwin->w_cursor in on a valid character
617 */
618 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100619check_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 check_cursor_lnum();
622 check_cursor_col();
623}
624
Bram Moolenaar7ce5b2b2022-05-16 19:40:59 +0100625/*
626 * Check if VIsual position is valid, correct it if not.
627 * Can be called when in Visual mode and a change has been made.
628 */
629 void
630check_visual_pos(void)
631{
632 if (VIsual.lnum > curbuf->b_ml.ml_line_count)
633 {
634 VIsual.lnum = curbuf->b_ml.ml_line_count;
635 VIsual.col = 0;
636 VIsual.coladd = 0;
637 }
638 else
639 {
640 int len = (int)STRLEN(ml_get(VIsual.lnum));
641
642 if (VIsual.col > len)
643 {
644 VIsual.col = len;
645 VIsual.coladd = 0;
646 }
647 }
648}
649
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650#if defined(FEAT_TEXTOBJ) || defined(PROTO)
651/*
652 * Make sure curwin->w_cursor is not on the NUL at the end of the line.
653 * Allow it when in Visual mode and 'selection' is not "old".
654 */
655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100656adjust_cursor_col(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 if (curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 && gchar_cursor() == NUL)
661 --curwin->w_cursor.col;
662}
663#endif
664
665/*
666 * When curwin->w_leftcol has changed, adjust the cursor position.
667 * Return TRUE if the cursor was moved.
668 */
669 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100670leftcol_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
672 long lastcol;
673 colnr_T s, e;
674 int retval = FALSE;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100675 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
677 changed_cline_bef_curs();
Bram Moolenaar02631462017-09-22 15:20:32 +0200678 lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 validate_virtcol();
680
681 /*
682 * If the cursor is right or left of the screen, move it to last or first
683 * character.
684 */
Bram Moolenaar375e3392019-01-31 18:26:10 +0100685 if (curwin->w_virtcol > (colnr_T)(lastcol - siso))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {
687 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100688 coladvance((colnr_T)(lastcol - siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
Bram Moolenaar375e3392019-01-31 18:26:10 +0100690 else if (curwin->w_virtcol < curwin->w_leftcol + siso)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
692 retval = TRUE;
Bram Moolenaar375e3392019-01-31 18:26:10 +0100693 (void)coladvance((colnr_T)(curwin->w_leftcol + siso));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
695
696 /*
697 * If the start of the character under the cursor is not on the screen,
698 * advance the cursor one more char. If this fails (last char of the
699 * line) adjust the scrolling.
700 */
701 getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
702 if (e > (colnr_T)lastcol)
703 {
704 retval = TRUE;
705 coladvance(s - 1);
706 }
707 else if (s < curwin->w_leftcol)
708 {
709 retval = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100710 if (coladvance(e + 1) == FAIL) // there isn't another character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100712 curwin->w_leftcol = s; // adjust w_leftcol instead
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 changed_cline_bef_curs();
714 }
715 }
716
717 if (retval)
718 curwin->w_set_curswant = TRUE;
719 redraw_later(NOT_VALID);
720 return retval;
721}
722
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +0100723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 * Isolate one part of a string option where parts are separated with
725 * "sep_chars".
Bram Moolenaar83bab712005-08-01 21:58:57 +0000726 * The part is copied into "buf[maxlen]".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 * "*option" is advanced to the next part.
728 * The length is returned.
729 */
730 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100731copy_option_part(
732 char_u **option,
733 char_u *buf,
734 int maxlen,
735 char *sep_chars)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736{
737 int len = 0;
738 char_u *p = *option;
739
Bram Moolenaar85a20022019-12-21 18:25:54 +0100740 // skip '.' at start of option part, for 'suffixes'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 if (*p == '.')
742 buf[len++] = *p++;
743 while (*p != NUL && vim_strchr((char_u *)sep_chars, *p) == NULL)
744 {
745 /*
746 * Skip backslash before a separator character and space.
747 */
748 if (p[0] == '\\' && vim_strchr((char_u *)sep_chars, p[1]) != NULL)
749 ++p;
750 if (len < maxlen - 1)
751 buf[len++] = *p;
752 ++p;
753 }
754 buf[len] = NUL;
755
Bram Moolenaar85a20022019-12-21 18:25:54 +0100756 if (*p != NUL && *p != ',') // skip non-standard separator
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 ++p;
Bram Moolenaar85a20022019-12-21 18:25:54 +0100758 p = skip_to_option_part(p); // p points to next file name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
760 *option = p;
761 return len;
762}
763
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764#ifndef HAVE_MEMSET
765 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100766vim_memset(void *ptr, int c, size_t size)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767{
768 char *p = ptr;
769
770 while (size-- > 0)
771 *p++ = c;
772 return ptr;
773}
774#endif
775
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776/*
777 * Vim has its own isspace() function, because on some machines isspace()
778 * can't handle characters above 128.
779 */
780 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100781vim_isspace(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
783 return ((x >= 9 && x <= 13) || x == ' ');
784}
785
786/************************************************************************
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 * functions that use lookup tables for various things, generally to do with
788 * special key codes.
789 */
790
791/*
792 * Some useful tables.
793 */
794
795static struct modmasktable
796{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100797 short mod_mask; // Bit-mask for particular key modifier
798 short mod_flag; // Bit(s) for particular key modifier
799 char_u name; // Single letter name of modifier
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800} mod_mask_table[] =
801{
802 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000803 {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
805 {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
806 {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
807 {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
808 {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
Bram Moolenaard0573012017-10-28 21:11:06 +0200809#ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
811#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +0100812 // 'A' must be the last one
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
814 {0, 0, NUL}
Bram Moolenaar85a20022019-12-21 18:25:54 +0100815 // NOTE: when adding an entry, update MAX_KEY_NAME_LEN!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816};
817
818/*
819 * Shifted key terminal codes and their unshifted equivalent.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +0000820 * Don't add mouse codes here, they are handled separately!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 */
822#define MOD_KEYS_ENTRY_SIZE 5
823
824static char_u modifier_keys_table[] =
825{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100826// mod mask with modifier without modifier
827 MOD_MASK_SHIFT, '&', '9', '@', '1', // begin
828 MOD_MASK_SHIFT, '&', '0', '@', '2', // cancel
829 MOD_MASK_SHIFT, '*', '1', '@', '4', // command
830 MOD_MASK_SHIFT, '*', '2', '@', '5', // copy
831 MOD_MASK_SHIFT, '*', '3', '@', '6', // create
832 MOD_MASK_SHIFT, '*', '4', 'k', 'D', // delete char
833 MOD_MASK_SHIFT, '*', '5', 'k', 'L', // delete line
834 MOD_MASK_SHIFT, '*', '7', '@', '7', // end
835 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7', // end
836 MOD_MASK_SHIFT, '*', '9', '@', '9', // exit
837 MOD_MASK_SHIFT, '*', '0', '@', '0', // find
838 MOD_MASK_SHIFT, '#', '1', '%', '1', // help
839 MOD_MASK_SHIFT, '#', '2', 'k', 'h', // home
840 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h', // home
841 MOD_MASK_SHIFT, '#', '3', 'k', 'I', // insert
842 MOD_MASK_SHIFT, '#', '4', 'k', 'l', // left arrow
843 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l', // left arrow
844 MOD_MASK_SHIFT, '%', 'a', '%', '3', // message
845 MOD_MASK_SHIFT, '%', 'b', '%', '4', // move
846 MOD_MASK_SHIFT, '%', 'c', '%', '5', // next
847 MOD_MASK_SHIFT, '%', 'd', '%', '7', // options
848 MOD_MASK_SHIFT, '%', 'e', '%', '8', // previous
849 MOD_MASK_SHIFT, '%', 'f', '%', '9', // print
850 MOD_MASK_SHIFT, '%', 'g', '%', '0', // redo
851 MOD_MASK_SHIFT, '%', 'h', '&', '3', // replace
852 MOD_MASK_SHIFT, '%', 'i', 'k', 'r', // right arr.
853 MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r', // right arr.
854 MOD_MASK_SHIFT, '%', 'j', '&', '5', // resume
855 MOD_MASK_SHIFT, '!', '1', '&', '6', // save
856 MOD_MASK_SHIFT, '!', '2', '&', '7', // suspend
857 MOD_MASK_SHIFT, '!', '3', '&', '8', // undo
858 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u', // up arrow
859 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd', // down arrow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
Bram Moolenaar85a20022019-12-21 18:25:54 +0100861 // vt100 F1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
863 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
864 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
865 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
866
Bram Moolenaar85a20022019-12-21 18:25:54 +0100867 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1', // F1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
869 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
870 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
871 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
872 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
873 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
874 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
875 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
Bram Moolenaar85a20022019-12-21 18:25:54 +0100876 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';', // F10
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877
878 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
879 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
880 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
881 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
882 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
883 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
884 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
885 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
886 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
887 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
888
889 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
890 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
891 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
892 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
893 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
894 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
895 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
896 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
897 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
898 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
899
900 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
901 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
902 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
903 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
904 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
905 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
906 MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
907
Bram Moolenaar85a20022019-12-21 18:25:54 +0100908 // TAB pseudo code
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
910
911 NUL
912};
913
914static struct key_name_entry
915{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100916 int key; // Special key code or ascii value
917 char_u *name; // Name of key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918} key_names_table[] =
919{
920 {' ', (char_u *)"Space"},
921 {TAB, (char_u *)"Tab"},
922 {K_TAB, (char_u *)"Tab"},
923 {NL, (char_u *)"NL"},
Bram Moolenaar85a20022019-12-21 18:25:54 +0100924 {NL, (char_u *)"NewLine"}, // Alternative name
925 {NL, (char_u *)"LineFeed"}, // Alternative name
926 {NL, (char_u *)"LF"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {CAR, (char_u *)"CR"},
Bram Moolenaar85a20022019-12-21 18:25:54 +0100928 {CAR, (char_u *)"Return"}, // Alternative name
929 {CAR, (char_u *)"Enter"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 {K_BS, (char_u *)"BS"},
Bram Moolenaar85a20022019-12-21 18:25:54 +0100931 {K_BS, (char_u *)"BackSpace"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 {ESC, (char_u *)"Esc"},
933 {CSI, (char_u *)"CSI"},
934 {K_CSI, (char_u *)"xCSI"},
935 {'|', (char_u *)"Bar"},
936 {'\\', (char_u *)"Bslash"},
937 {K_DEL, (char_u *)"Del"},
Bram Moolenaar85a20022019-12-21 18:25:54 +0100938 {K_DEL, (char_u *)"Delete"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {K_KDEL, (char_u *)"kDel"},
940 {K_UP, (char_u *)"Up"},
941 {K_DOWN, (char_u *)"Down"},
942 {K_LEFT, (char_u *)"Left"},
943 {K_RIGHT, (char_u *)"Right"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000944 {K_XUP, (char_u *)"xUp"},
945 {K_XDOWN, (char_u *)"xDown"},
946 {K_XLEFT, (char_u *)"xLeft"},
947 {K_XRIGHT, (char_u *)"xRight"},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100948 {K_PS, (char_u *)"PasteStart"},
949 {K_PE, (char_u *)"PasteEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
951 {K_F1, (char_u *)"F1"},
952 {K_F2, (char_u *)"F2"},
953 {K_F3, (char_u *)"F3"},
954 {K_F4, (char_u *)"F4"},
955 {K_F5, (char_u *)"F5"},
956 {K_F6, (char_u *)"F6"},
957 {K_F7, (char_u *)"F7"},
958 {K_F8, (char_u *)"F8"},
959 {K_F9, (char_u *)"F9"},
960 {K_F10, (char_u *)"F10"},
961
962 {K_F11, (char_u *)"F11"},
963 {K_F12, (char_u *)"F12"},
964 {K_F13, (char_u *)"F13"},
965 {K_F14, (char_u *)"F14"},
966 {K_F15, (char_u *)"F15"},
967 {K_F16, (char_u *)"F16"},
968 {K_F17, (char_u *)"F17"},
969 {K_F18, (char_u *)"F18"},
970 {K_F19, (char_u *)"F19"},
971 {K_F20, (char_u *)"F20"},
972
973 {K_F21, (char_u *)"F21"},
974 {K_F22, (char_u *)"F22"},
975 {K_F23, (char_u *)"F23"},
976 {K_F24, (char_u *)"F24"},
977 {K_F25, (char_u *)"F25"},
978 {K_F26, (char_u *)"F26"},
979 {K_F27, (char_u *)"F27"},
980 {K_F28, (char_u *)"F28"},
981 {K_F29, (char_u *)"F29"},
982 {K_F30, (char_u *)"F30"},
983
984 {K_F31, (char_u *)"F31"},
985 {K_F32, (char_u *)"F32"},
986 {K_F33, (char_u *)"F33"},
987 {K_F34, (char_u *)"F34"},
988 {K_F35, (char_u *)"F35"},
989 {K_F36, (char_u *)"F36"},
990 {K_F37, (char_u *)"F37"},
991
992 {K_XF1, (char_u *)"xF1"},
993 {K_XF2, (char_u *)"xF2"},
994 {K_XF3, (char_u *)"xF3"},
995 {K_XF4, (char_u *)"xF4"},
996
997 {K_HELP, (char_u *)"Help"},
998 {K_UNDO, (char_u *)"Undo"},
999 {K_INS, (char_u *)"Insert"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001000 {K_INS, (char_u *)"Ins"}, // Alternative name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 {K_KINS, (char_u *)"kInsert"},
1002 {K_HOME, (char_u *)"Home"},
1003 {K_KHOME, (char_u *)"kHome"},
1004 {K_XHOME, (char_u *)"xHome"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001005 {K_ZHOME, (char_u *)"zHome"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 {K_END, (char_u *)"End"},
1007 {K_KEND, (char_u *)"kEnd"},
1008 {K_XEND, (char_u *)"xEnd"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001009 {K_ZEND, (char_u *)"zEnd"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {K_PAGEUP, (char_u *)"PageUp"},
1011 {K_PAGEDOWN, (char_u *)"PageDown"},
1012 {K_KPAGEUP, (char_u *)"kPageUp"},
1013 {K_KPAGEDOWN, (char_u *)"kPageDown"},
1014
1015 {K_KPLUS, (char_u *)"kPlus"},
1016 {K_KMINUS, (char_u *)"kMinus"},
1017 {K_KDIVIDE, (char_u *)"kDivide"},
1018 {K_KMULTIPLY, (char_u *)"kMultiply"},
1019 {K_KENTER, (char_u *)"kEnter"},
1020 {K_KPOINT, (char_u *)"kPoint"},
1021
1022 {K_K0, (char_u *)"k0"},
1023 {K_K1, (char_u *)"k1"},
1024 {K_K2, (char_u *)"k2"},
1025 {K_K3, (char_u *)"k3"},
1026 {K_K4, (char_u *)"k4"},
1027 {K_K5, (char_u *)"k5"},
1028 {K_K6, (char_u *)"k6"},
1029 {K_K7, (char_u *)"k7"},
1030 {K_K8, (char_u *)"k8"},
1031 {K_K9, (char_u *)"k9"},
1032
1033 {'<', (char_u *)"lt"},
1034
1035 {K_MOUSE, (char_u *)"Mouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001036#ifdef FEAT_MOUSE_NET
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 {K_NETTERM_MOUSE, (char_u *)"NetMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001038#endif
1039#ifdef FEAT_MOUSE_DEC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {K_DEC_MOUSE, (char_u *)"DecMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001041#endif
1042#ifdef FEAT_MOUSE_JSB
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {K_JSBTERM_MOUSE, (char_u *)"JsbMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001044#endif
1045#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {K_PTERM_MOUSE, (char_u *)"PtermMouse"},
Bram Moolenaar5af7d712012-01-20 17:15:51 +01001047#endif
1048#ifdef FEAT_MOUSE_URXVT
1049 {K_URXVT_MOUSE, (char_u *)"UrxvtMouse"},
1050#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02001051 {K_SGR_MOUSE, (char_u *)"SgrMouse"},
Bram Moolenaar21a83bd2021-02-23 19:19:58 +01001052 {K_SGR_MOUSERELEASE, (char_u *)"SgrMouseRelease"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 {K_LEFTMOUSE, (char_u *)"LeftMouse"},
1054 {K_LEFTMOUSE_NM, (char_u *)"LeftMouseNM"},
1055 {K_LEFTDRAG, (char_u *)"LeftDrag"},
1056 {K_LEFTRELEASE, (char_u *)"LeftRelease"},
1057 {K_LEFTRELEASE_NM, (char_u *)"LeftReleaseNM"},
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001058 {K_MOUSEMOVE, (char_u *)"MouseMove"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {K_MIDDLEMOUSE, (char_u *)"MiddleMouse"},
1060 {K_MIDDLEDRAG, (char_u *)"MiddleDrag"},
1061 {K_MIDDLERELEASE, (char_u *)"MiddleRelease"},
1062 {K_RIGHTMOUSE, (char_u *)"RightMouse"},
1063 {K_RIGHTDRAG, (char_u *)"RightDrag"},
1064 {K_RIGHTRELEASE, (char_u *)"RightRelease"},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001065 {K_MOUSEDOWN, (char_u *)"ScrollWheelUp"},
1066 {K_MOUSEUP, (char_u *)"ScrollWheelDown"},
1067 {K_MOUSELEFT, (char_u *)"ScrollWheelRight"},
1068 {K_MOUSERIGHT, (char_u *)"ScrollWheelLeft"},
Bram Moolenaar85a20022019-12-21 18:25:54 +01001069 {K_MOUSEDOWN, (char_u *)"MouseDown"}, // OBSOLETE: Use
1070 {K_MOUSEUP, (char_u *)"MouseUp"}, // ScrollWheelXXX instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 {K_X1MOUSE, (char_u *)"X1Mouse"},
1072 {K_X1DRAG, (char_u *)"X1Drag"},
1073 {K_X1RELEASE, (char_u *)"X1Release"},
1074 {K_X2MOUSE, (char_u *)"X2Mouse"},
1075 {K_X2DRAG, (char_u *)"X2Drag"},
1076 {K_X2RELEASE, (char_u *)"X2Release"},
1077 {K_DROP, (char_u *)"Drop"},
1078 {K_ZERO, (char_u *)"Nul"},
1079#ifdef FEAT_EVAL
1080 {K_SNR, (char_u *)"SNR"},
1081#endif
1082 {K_PLUG, (char_u *)"Plug"},
Bram Moolenaar1db60c42014-09-23 16:49:46 +02001083 {K_CURSORHOLD, (char_u *)"CursorHold"},
Bram Moolenaar2f106582019-05-08 21:59:25 +02001084 {K_IGNORE, (char_u *)"Ignore"},
Bram Moolenaar957cf672020-11-12 14:21:06 +01001085 {K_COMMAND, (char_u *)"Cmd"},
Bram Moolenaare32c3c42022-01-15 18:26:04 +00001086 {K_SCRIPT_COMMAND, (char_u *)"ScriptCmd"},
Bram Moolenaarccb47a22021-01-21 13:36:43 +01001087 {K_FOCUSGAINED, (char_u *)"FocusGained"},
1088 {K_FOCUSLOST, (char_u *)"FocusLost"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {0, NULL}
Bram Moolenaar85a20022019-12-21 18:25:54 +01001090 // NOTE: When adding a long name update MAX_KEY_NAME_LEN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091};
1092
K.Takataeeec2542021-06-02 13:28:16 +02001093#define KEY_NAMES_TABLE_LEN ARRAY_LENGTH(key_names_table)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095/*
1096 * Return the modifier mask bit (MOD_MASK_*) which corresponds to the given
1097 * modifier name ('S' for Shift, 'C' for Ctrl etc).
1098 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001099 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001100name_to_mod_mask(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
1102 int i;
1103
1104 c = TOUPPER_ASC(c);
1105 for (i = 0; mod_mask_table[i].mod_mask != 0; i++)
1106 if (c == mod_mask_table[i].name)
1107 return mod_mask_table[i].mod_flag;
1108 return 0;
1109}
1110
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111/*
1112 * Check if if there is a special key code for "key" that includes the
1113 * modifiers specified.
1114 */
1115 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001116simplify_key(int key, int *modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
1118 int i;
1119 int key0;
1120 int key1;
1121
1122 if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))
1123 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001124 // TAB is a special case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (key == TAB && (*modifiers & MOD_MASK_SHIFT))
1126 {
1127 *modifiers &= ~MOD_MASK_SHIFT;
1128 return K_S_TAB;
1129 }
1130 key0 = KEY2TERMCAP0(key);
1131 key1 = KEY2TERMCAP1(key);
1132 for (i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE)
1133 if (key0 == modifier_keys_table[i + 3]
1134 && key1 == modifier_keys_table[i + 4]
1135 && (*modifiers & modifier_keys_table[i]))
1136 {
1137 *modifiers &= ~modifier_keys_table[i];
1138 return TERMCAP2KEY(modifier_keys_table[i + 1],
1139 modifier_keys_table[i + 2]);
1140 }
1141 }
1142 return key;
1143}
1144
1145/*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001146 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001147 */
1148 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001149handle_x_keys(int key)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001150{
1151 switch (key)
1152 {
1153 case K_XUP: return K_UP;
1154 case K_XDOWN: return K_DOWN;
1155 case K_XLEFT: return K_LEFT;
1156 case K_XRIGHT: return K_RIGHT;
1157 case K_XHOME: return K_HOME;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001158 case K_ZHOME: return K_HOME;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001159 case K_XEND: return K_END;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001160 case K_ZEND: return K_END;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001161 case K_XF1: return K_F1;
1162 case K_XF2: return K_F2;
1163 case K_XF3: return K_F3;
1164 case K_XF4: return K_F4;
1165 case K_S_XF1: return K_S_F1;
1166 case K_S_XF2: return K_S_F2;
1167 case K_S_XF3: return K_S_F3;
1168 case K_S_XF4: return K_S_F4;
1169 }
1170 return key;
1171}
1172
1173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 * Return a string which contains the name of the given key when the given
1175 * modifiers are down.
1176 */
1177 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001178get_special_key_name(int c, int modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179{
1180 static char_u string[MAX_KEY_NAME_LEN + 1];
1181
1182 int i, idx;
1183 int table_idx;
1184 char_u *s;
1185
1186 string[0] = '<';
1187 idx = 1;
1188
Bram Moolenaar85a20022019-12-21 18:25:54 +01001189 // Key that stands for a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (IS_SPECIAL(c) && KEY2TERMCAP0(c) == KS_KEY)
1191 c = KEY2TERMCAP1(c);
1192
1193 /*
1194 * Translate shifted special keys into unshifted keys and set modifier.
1195 * Same for CTRL and ALT modifiers.
1196 */
1197 if (IS_SPECIAL(c))
1198 {
1199 for (i = 0; modifier_keys_table[i] != 0; i += MOD_KEYS_ENTRY_SIZE)
1200 if ( KEY2TERMCAP0(c) == (int)modifier_keys_table[i + 1]
1201 && (int)KEY2TERMCAP1(c) == (int)modifier_keys_table[i + 2])
1202 {
1203 modifiers |= modifier_keys_table[i];
1204 c = TERMCAP2KEY(modifier_keys_table[i + 3],
1205 modifier_keys_table[i + 4]);
1206 break;
1207 }
1208 }
1209
Bram Moolenaar85a20022019-12-21 18:25:54 +01001210 // try to find the key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 table_idx = find_special_key_in_table(c);
1212
1213 /*
1214 * When not a known special key, and not a printable character, try to
1215 * extract modifiers.
1216 */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001217 if (c > 0 && (*mb_char2len)(c) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
1219 if (table_idx < 0
1220 && (!vim_isprintc(c) || (c & 0x7f) == ' ')
1221 && (c & 0x80))
1222 {
1223 c &= 0x7f;
1224 modifiers |= MOD_MASK_ALT;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001225 // try again, to find the un-alted key in the special key table
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 table_idx = find_special_key_in_table(c);
1227 }
1228 if (table_idx < 0 && !vim_isprintc(c) && c < ' ')
1229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 c += '@';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 modifiers |= MOD_MASK_CTRL;
1232 }
1233 }
1234
Bram Moolenaar85a20022019-12-21 18:25:54 +01001235 // translate the modifier into a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 for (i = 0; mod_mask_table[i].name != 'A'; i++)
1237 if ((modifiers & mod_mask_table[i].mod_mask)
1238 == mod_mask_table[i].mod_flag)
1239 {
1240 string[idx++] = mod_mask_table[i].name;
1241 string[idx++] = (char_u)'-';
1242 }
1243
Bram Moolenaar85a20022019-12-21 18:25:54 +01001244 if (table_idx < 0) // unknown special key, may output t_xx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
1246 if (IS_SPECIAL(c))
1247 {
1248 string[idx++] = 't';
1249 string[idx++] = '_';
1250 string[idx++] = KEY2TERMCAP0(c);
1251 string[idx++] = KEY2TERMCAP1(c);
1252 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001253 // Not a special key, only modifiers, output directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 else
1255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (has_mbyte && (*mb_char2len)(c) > 1)
1257 idx += (*mb_char2bytes)(c, string + idx);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001258 else if (vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 string[idx++] = c;
1260 else
1261 {
1262 s = transchar(c);
1263 while (*s)
1264 string[idx++] = *s++;
1265 }
1266 }
1267 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001268 else // use name of special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 {
Bram Moolenaar423977d2017-01-22 15:05:12 +01001270 size_t len = STRLEN(key_names_table[table_idx].name);
1271
1272 if (len + idx + 2 <= MAX_KEY_NAME_LEN)
1273 {
1274 STRCPY(string + idx, key_names_table[table_idx].name);
1275 idx += (int)len;
1276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
1278 string[idx++] = '>';
1279 string[idx] = NUL;
1280 return string;
1281}
1282
1283/*
1284 * Try translating a <> name at (*srcp)[] to dst[].
1285 * Return the number of characters added to dst[], zero for no match.
1286 * If there is a match, srcp is advanced to after the <> name.
1287 * dst[] must be big enough to hold the result (up to six characters)!
1288 */
1289 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001290trans_special(
1291 char_u **srcp,
1292 char_u *dst,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001293 int flags, // FSK_ values
zeertzjqdb088872022-05-02 22:53:45 +01001294 int escape_ks, // escape K_SPECIAL bytes in the character
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001295 int *did_simplify) // FSK_SIMPLIFY and found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 int modifiers = 0;
1298 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001300 key = find_special_key(srcp, &modifiers, flags, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 if (key == 0)
1302 return 0;
1303
zeertzjqdb088872022-05-02 22:53:45 +01001304 return special_to_buf(key, modifiers, escape_ks, dst);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001305}
1306
1307/*
1308 * Put the character sequence for "key" with "modifiers" into "dst" and return
1309 * the resulting length.
zeertzjqdb088872022-05-02 22:53:45 +01001310 * When "escape_ks" is TRUE escape K_SPECIAL bytes in the character.
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001311 * The sequence is not NUL terminated.
1312 * This is how characters in a string are encoded.
1313 */
1314 int
zeertzjqdb088872022-05-02 22:53:45 +01001315special_to_buf(int key, int modifiers, int escape_ks, char_u *dst)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001316{
1317 int dlen = 0;
1318
Bram Moolenaar85a20022019-12-21 18:25:54 +01001319 // Put the appropriate modifier in a string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 if (modifiers != 0)
1321 {
1322 dst[dlen++] = K_SPECIAL;
1323 dst[dlen++] = KS_MODIFIER;
1324 dst[dlen++] = modifiers;
1325 }
1326
1327 if (IS_SPECIAL(key))
1328 {
1329 dst[dlen++] = K_SPECIAL;
1330 dst[dlen++] = KEY2TERMCAP0(key);
1331 dst[dlen++] = KEY2TERMCAP1(key);
1332 }
zeertzjqdb088872022-05-02 22:53:45 +01001333 else if (escape_ks)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 dlen = (int)(add_char2buf(key, dst + dlen) - dst);
zeertzjqdb088872022-05-02 22:53:45 +01001335 else if (has_mbyte)
1336 dlen += (*mb_char2bytes)(key, dst + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 else
1338 dst[dlen++] = key;
1339
1340 return dlen;
1341}
1342
1343/*
1344 * Try translating a <> name at (*srcp)[], return the key and modifiers.
1345 * srcp is advanced to after the <> name.
1346 * returns 0 if there is no match.
1347 */
1348 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001349find_special_key(
1350 char_u **srcp,
1351 int *modp,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001352 int flags, // FSK_ values
Bram Moolenaar459fd782019-10-13 16:43:39 +02001353 int *did_simplify) // found <C-H> or <A-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354{
1355 char_u *last_dash;
1356 char_u *end_of_name;
1357 char_u *src;
1358 char_u *bp;
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001359 int in_string = flags & FSK_IN_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 int modifiers;
1361 int bit;
1362 int key;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001363 uvarnumber_T n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001364 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
1366 src = *srcp;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001367 if (src[0] != '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 return 0;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001369 if (src[1] == '*') // <*xxx>: do not simplify
1370 ++src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
Bram Moolenaar85a20022019-12-21 18:25:54 +01001372 // Find end of modifier list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 last_dash = src;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02001374 for (bp = src + 1; *bp == '-' || vim_isNormalIDc(*bp); bp++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
1376 if (*bp == '-')
1377 {
1378 last_dash = bp;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001379 if (bp[1] != NUL)
1380 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001381 if (has_mbyte)
1382 l = mb_ptr2len(bp + 1);
1383 else
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001384 l = 1;
Bram Moolenaarc8fd33d2019-08-16 20:33:05 +02001385 // Anything accepted, like <C-?>.
1386 // <C-"> or <M-"> are not special in strings as " is
1387 // the string delimiter. With a backslash it works: <M-\">
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001388 if (!(in_string && bp[1] == '"') && bp[l + 1] == '>')
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +02001389 bp += l;
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001390 else if (in_string && bp[1] == '\\' && bp[2] == '"'
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001391 && bp[3] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001392 bp += 2;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395 if (bp[0] == 't' && bp[1] == '_' && bp[2] && bp[3])
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001396 bp += 3; // skip t_xx, xx may be '-' or '>'
Bram Moolenaar792826c2011-08-19 22:29:02 +02001397 else if (STRNICMP(bp, "char-", 5) == 0)
1398 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001399 vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0, TRUE);
1400 if (l == 0)
1401 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001402 emsg(_(e_invalid_argument));
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001403 return 0;
1404 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02001405 bp += l + 5;
1406 break;
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001410 if (*bp == '>') // found matching '>'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 {
1412 end_of_name = bp + 1;
1413
Bram Moolenaar85a20022019-12-21 18:25:54 +01001414 // Which modifiers are given?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 modifiers = 0x0;
1416 for (bp = src + 1; bp < last_dash; bp++)
1417 {
1418 if (*bp != '-')
1419 {
1420 bit = name_to_mod_mask(*bp);
1421 if (bit == 0x0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001422 break; // Illegal modifier name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 modifiers |= bit;
1424 }
1425 }
1426
1427 /*
1428 * Legal modifier name.
1429 */
1430 if (bp >= last_dash)
1431 {
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001432 if (STRNICMP(last_dash + 1, "char-", 5) == 0
1433 && VIM_ISDIGIT(last_dash[6]))
1434 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001435 // <Char-123> or <Char-033> or <Char-0x33>
Bram Moolenaar459fd782019-10-13 16:43:39 +02001436 vim_str2nr(last_dash + 6, NULL, &l, STR2NR_ALL, NULL,
1437 &n, 0, TRUE);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001438 if (l == 0)
1439 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001440 emsg(_(e_invalid_argument));
Bram Moolenaar16e9b852019-05-19 19:59:35 +02001441 return 0;
1442 }
Bram Moolenaar792826c2011-08-19 22:29:02 +02001443 key = (int)n;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001446 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001447 int off = 1;
1448
Bram Moolenaar85a20022019-12-21 18:25:54 +01001449 // Modifier with single letter, or special key name.
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001450 if (in_string && last_dash[1] == '\\' && last_dash[2] == '"')
1451 off = 2;
Bram Moolenaar792826c2011-08-19 22:29:02 +02001452 if (has_mbyte)
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001453 l = mb_ptr2len(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02001454 else
Bram Moolenaar792826c2011-08-19 22:29:02 +02001455 l = 1;
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02001456 if (modifiers != 0 && last_dash[l + off] == '>')
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001457 key = PTR2CHAR(last_dash + off);
Bram Moolenaar792826c2011-08-19 22:29:02 +02001458 else
1459 {
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02001460 key = get_special_key_code(last_dash + off);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001461 if (!(flags & FSK_KEEP_X_KEY))
Bram Moolenaar792826c2011-08-19 22:29:02 +02001462 key = handle_x_keys(key);
1463 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
1466 /*
1467 * get_special_key_code() may return NUL for invalid
1468 * special key name.
1469 */
1470 if (key != NUL)
1471 {
1472 /*
1473 * Only use a modifier when there is no special key code that
1474 * includes the modifier.
1475 */
1476 key = simplify_key(key, &modifiers);
1477
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001478 if (!(flags & FSK_KEYCODE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001480 // don't want keycode, use single byte code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (key == K_BS)
1482 key = BS;
1483 else if (key == K_DEL || key == K_KDEL)
1484 key = DEL;
1485 }
1486
Bram Moolenaar459fd782019-10-13 16:43:39 +02001487 // Normal Key with modifier: Try to make a single byte code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (!IS_SPECIAL(key))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001489 key = extract_modifiers(key, &modifiers,
Bram Moolenaarebe9d342020-05-30 21:52:54 +02001490 flags & FSK_SIMPLIFY, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 *modp = modifiers;
1493 *srcp = end_of_name;
1494 return key;
1495 }
1496 }
1497 }
1498 return 0;
1499}
1500
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001501
1502/*
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02001503 * Some keys are used with Ctrl without Shift and are still expected to be
1504 * mapped as if Shift was pressed:
1505 * CTRL-2 is CTRL-@
1506 * CTRL-6 is CTRL-^
1507 * CTRL-- is CTRL-_
1508 * Also, <C-H> and <C-h> mean the same thing, always use "H".
1509 * Returns the possibly adjusted key.
1510 */
1511 int
1512may_adjust_key_for_ctrl(int modifiers, int key)
1513{
1514 if (modifiers & MOD_MASK_CTRL)
1515 {
1516 if (ASCII_ISALPHA(key))
1517 return TOUPPER_ASC(key);
1518 if (key == '2')
1519 return '@';
1520 if (key == '6')
1521 return '^';
1522 if (key == '-')
1523 return '_';
1524 }
1525 return key;
1526}
1527
1528/*
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001529 * Some keys already have Shift included, pass them as normal keys.
Bram Moolenaar9a033d72020-10-07 17:29:48 +02001530 * When Ctrl is also used <C-H> and <C-S-H> are different, but <C-S-{> should
1531 * be <C-{>. Same for <C-S-}> and <C-S-|>.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001532 * Also for <A-S-a> and <M-S-a>.
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02001533 * This includes all printable ASCII characters except numbers and a-z.
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001534 */
1535 int
1536may_remove_shift_modifier(int modifiers, int key)
1537{
1538 if ((modifiers == MOD_MASK_SHIFT
1539 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
1540 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02001541 && ((key >= '!' && key <= '/')
1542 || (key >= ':' && key <= 'Z')
1543 || (key >= '[' && key <= '`')
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001544 || (key >= '{' && key <= '~')))
1545 return modifiers & ~MOD_MASK_SHIFT;
Bram Moolenaar9a033d72020-10-07 17:29:48 +02001546
1547 if (modifiers == (MOD_MASK_SHIFT | MOD_MASK_CTRL)
1548 && (key == '{' || key == '}' || key == '|'))
1549 return modifiers & ~MOD_MASK_SHIFT;
1550
Bram Moolenaaref6746f2020-06-20 14:43:23 +02001551 return modifiers;
1552}
1553
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554/*
1555 * Try to include modifiers in the key.
1556 * Changes "Shift-a" to 'A', "Alt-A" to 0xc0, etc.
Bram Moolenaar459fd782019-10-13 16:43:39 +02001557 * When "simplify" is FALSE don't do Ctrl and Alt.
1558 * When "simplify" is TRUE and Ctrl or Alt is removed from modifiers set
1559 * "did_simplify" when it's not NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
1561 int
Bram Moolenaar459fd782019-10-13 16:43:39 +02001562extract_modifiers(int key, int *modp, int simplify, int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564 int modifiers = *modp;
1565
Bram Moolenaard0573012017-10-28 21:11:06 +02001566#ifdef MACOS_X
Bram Moolenaar459fd782019-10-13 16:43:39 +02001567 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (!(modifiers & MOD_MASK_CMD))
1569#endif
1570 if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key))
1571 {
1572 key = TOUPPER_ASC(key);
Bram Moolenaar20298ce2020-06-19 21:46:52 +02001573 // With <C-S-a> we keep the shift modifier.
1574 // With <S-a>, <A-S-a> and <S-A> we don't keep the shift modifier.
1575 if (simplify || modifiers == MOD_MASK_SHIFT
1576 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_ALT)
1577 || modifiers == (MOD_MASK_SHIFT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001578 modifiers &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02001580
1581 // <C-H> and <C-h> mean the same thing, always use "H"
1582 if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key))
1583 key = TOUPPER_ASC(key);
1584
1585 if (simplify && (modifiers & MOD_MASK_CTRL)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001586 && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
1588 key = Ctrl_chr(key);
1589 modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001590 // <C-@> is <Nul>
zeertzjq17c95d92022-04-26 12:51:07 +01001591 if (key == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 key = K_ZERO;
Bram Moolenaar459fd782019-10-13 16:43:39 +02001593 if (did_simplify != NULL)
1594 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
Bram Moolenaar459fd782019-10-13 16:43:39 +02001596
Bram Moolenaard0573012017-10-28 21:11:06 +02001597#ifdef MACOS_X
Bram Moolenaar85a20022019-12-21 18:25:54 +01001598 // Command-key really special, no fancynest
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (!(modifiers & MOD_MASK_CMD))
1600#endif
Bram Moolenaar459fd782019-10-13 16:43:39 +02001601 if (simplify && (modifiers & MOD_MASK_ALT) && key < 0x80
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001602 && !enc_dbcs) // avoid creating a lead byte
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604 key |= 0x80;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001605 modifiers &= ~MOD_MASK_ALT; // remove the META modifier
Bram Moolenaar459fd782019-10-13 16:43:39 +02001606 if (did_simplify != NULL)
1607 *did_simplify = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609
1610 *modp = modifiers;
1611 return key;
1612}
1613
1614/*
1615 * Try to find key "c" in the special key table.
1616 * Return the index when found, -1 when not found.
1617 */
1618 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001619find_special_key_in_table(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620{
1621 int i;
1622
1623 for (i = 0; key_names_table[i].name != NULL; i++)
1624 if (c == key_names_table[i].key)
1625 break;
1626 if (key_names_table[i].name == NULL)
1627 i = -1;
1628 return i;
1629}
1630
1631/*
1632 * Find the special key with the given name (the given string does not have to
1633 * end with NUL, the name is assumed to end before the first non-idchar).
1634 * If the name starts with "t_" the next two characters are interpreted as a
1635 * termcap name.
1636 * Return the key code, or 0 if not found.
1637 */
1638 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001639get_special_key_code(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640{
1641 char_u *table_name;
1642 char_u string[3];
1643 int i, j;
1644
1645 /*
1646 * If it's <t_xx> we get the code for xx from the termcap
1647 */
1648 if (name[0] == 't' && name[1] == '_' && name[2] != NUL && name[3] != NUL)
1649 {
1650 string[0] = name[2];
1651 string[1] = name[3];
1652 string[2] = NUL;
1653 if (add_termcap_entry(string, FALSE) == OK)
1654 return TERMCAP2KEY(name[2], name[3]);
1655 }
1656 else
1657 for (i = 0; key_names_table[i].name != NULL; i++)
1658 {
1659 table_name = key_names_table[i].name;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02001660 for (j = 0; vim_isNormalIDc(name[j]) && table_name[j] != NUL; j++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j]))
1662 break;
Bram Moolenaare3d1f4c2021-04-06 20:21:59 +02001663 if (!vim_isNormalIDc(name[j]) && table_name[j] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 return key_names_table[i].key;
1665 }
1666 return 0;
1667}
1668
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001670get_key_name(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
Bram Moolenaare1fbddc2009-05-16 19:07:03 +00001672 if (i >= (int)KEY_NAMES_TABLE_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 return NULL;
1674 return key_names_table[i].name;
1675}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677/*
1678 * Return the current end-of-line type: EOL_DOS, EOL_UNIX or EOL_MAC.
1679 */
1680 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001681get_fileformat(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
1683 int c = *buf->b_p_ff;
1684
1685 if (buf->b_p_bin || c == 'u')
1686 return EOL_UNIX;
1687 if (c == 'm')
1688 return EOL_MAC;
1689 return EOL_DOS;
1690}
1691
1692/*
1693 * Like get_fileformat(), but override 'fileformat' with "p" for "++opt=val"
1694 * argument.
1695 */
1696 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001697get_fileformat_force(
1698 buf_T *buf,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001699 exarg_T *eap) // can be NULL!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700{
1701 int c;
1702
1703 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001704 c = eap->force_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 else
1706 {
1707 if ((eap != NULL && eap->force_bin != 0)
1708 ? (eap->force_bin == FORCE_BIN) : buf->b_p_bin)
1709 return EOL_UNIX;
1710 c = *buf->b_p_ff;
1711 }
1712 if (c == 'u')
1713 return EOL_UNIX;
1714 if (c == 'm')
1715 return EOL_MAC;
1716 return EOL_DOS;
1717}
1718
1719/*
1720 * Set the current end-of-line type to EOL_DOS, EOL_UNIX or EOL_MAC.
1721 * Sets both 'textmode' and 'fileformat'.
1722 * Note: Does _not_ set global value of 'textmode'!
1723 */
1724 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001725set_fileformat(
1726 int t,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001727 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
1729 char *p = NULL;
1730
1731 switch (t)
1732 {
1733 case EOL_DOS:
1734 p = FF_DOS;
1735 curbuf->b_p_tx = TRUE;
1736 break;
1737 case EOL_UNIX:
1738 p = FF_UNIX;
1739 curbuf->b_p_tx = FALSE;
1740 break;
1741 case EOL_MAC:
1742 p = FF_MAC;
1743 curbuf->b_p_tx = FALSE;
1744 break;
1745 }
1746 if (p != NULL)
1747 set_string_option_direct((char_u *)"ff", -1, (char_u *)p,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001748 OPT_FREE | opt_flags, 0);
1749
Bram Moolenaar85a20022019-12-21 18:25:54 +01001750 // This may cause the buffer to become (un)modified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 check_status(curbuf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001752 redraw_tabline = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001753 need_maketitle = TRUE; // set window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754}
1755
1756/*
1757 * Return the default fileformat from 'fileformats'.
1758 */
1759 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001760default_fileformat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762 switch (*p_ffs)
1763 {
1764 case 'm': return EOL_MAC;
1765 case 'd': return EOL_DOS;
1766 }
1767 return EOL_UNIX;
1768}
1769
1770/*
1771 * Call shell. Calls mch_call_shell, with 'shellxquote' added.
1772 */
1773 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001774call_shell(char_u *cmd, int opt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
1776 char_u *ncmd;
1777 int retval;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001778#ifdef FEAT_PROFILE
1779 proftime_T wait_time;
1780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 if (p_verbose > 3)
1783 {
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001784 verbose_enter();
Bram Moolenaar06f08532020-05-12 23:45:16 +02001785 smsg(_("Calling shell to execute: \"%s\""), cmd == NULL ? p_sh : cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 out_char('\n');
1787 cursor_on();
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00001788 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 }
1790
Bram Moolenaar05159a02005-02-26 23:04:13 +00001791#ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00001792 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001793 prof_child_enter(&wait_time);
1794#endif
1795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (*p_sh == NUL)
1797 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001798 emsg(_(e_shell_option_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 retval = -1;
1800 }
1801 else
1802 {
1803#ifdef FEAT_GUI_MSWIN
Bram Moolenaar85a20022019-12-21 18:25:54 +01001804 // Don't hide the pointer while executing a shell command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 gui_mch_mousehide(FALSE);
1806#endif
1807#ifdef FEAT_GUI
1808 ++hold_gui_events;
1809#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01001810 // The external command may update a tags file, clear cached tags.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 tag_freematch();
1812
Bram Moolenaar01257a72019-06-10 14:46:04 +02001813 if (cmd == NULL || *p_sxq == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 retval = mch_call_shell(cmd, opt);
1815 else
1816 {
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001817 char_u *ecmd = cmd;
1818
Bram Moolenaar1a613392019-09-28 15:51:37 +02001819 if (*p_sxe != NUL && *p_sxq == '(')
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001820 {
1821 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
1822 if (ecmd == NULL)
1823 ecmd = cmd;
1824 }
Bram Moolenaar964b3742019-05-24 18:54:09 +02001825 ncmd = alloc(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (ncmd != NULL)
1827 {
1828 STRCPY(ncmd, p_sxq);
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001829 STRCAT(ncmd, ecmd);
Bram Moolenaar1a613392019-09-28 15:51:37 +02001830 // When 'shellxquote' is ( append ).
1831 // When 'shellxquote' is "( append )".
1832 STRCAT(ncmd, *p_sxq == '(' ? (char_u *)")"
1833 : *p_sxq == '"' && *(p_sxq+1) == '(' ? (char_u *)")\""
1834 : p_sxq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 retval = mch_call_shell(ncmd, opt);
1836 vim_free(ncmd);
1837 }
1838 else
1839 retval = -1;
Bram Moolenaarf66b3fc2012-02-20 22:18:30 +01001840 if (ecmd != cmd)
1841 vim_free(ecmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 }
1843#ifdef FEAT_GUI
1844 --hold_gui_events;
1845#endif
1846 /*
1847 * Check the window size, in case it changed while executing the
1848 * external command.
1849 */
1850 shell_resized_check();
1851 }
1852
1853#ifdef FEAT_EVAL
1854 set_vim_var_nr(VV_SHELL_ERROR, (long)retval);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001855# ifdef FEAT_PROFILE
Bram Moolenaar01265852006-03-20 21:50:15 +00001856 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001857 prof_child_exit(&wait_time);
1858# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859#endif
1860
1861 return retval;
1862}
1863
1864/*
Bram Moolenaar24959102022-05-07 20:01:16 +01001865 * MODE_VISUAL, MODE_SELECT and MODE_OP_PENDING State are never set, they are
1866 * equal to MODE_NORMAL State with a condition. This function returns the real
1867 * State.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 */
1869 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001870get_real_state(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871{
Bram Moolenaar24959102022-05-07 20:01:16 +01001872 if (State & MODE_NORMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (VIsual_active)
Bram Moolenaar01265852006-03-20 21:50:15 +00001875 {
1876 if (VIsual_select)
Bram Moolenaar24959102022-05-07 20:01:16 +01001877 return MODE_SELECT;
1878 return MODE_VISUAL;
Bram Moolenaar01265852006-03-20 21:50:15 +00001879 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001880 else if (finish_op)
Bram Moolenaar24959102022-05-07 20:01:16 +01001881 return MODE_OP_PENDING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 }
1883 return State;
1884}
1885
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001886/*
1887 * Return TRUE if "p" points to just after a path separator.
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02001888 * Takes care of multi-byte characters.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001889 * "b" must point to the start of the file name
1890 */
1891 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001892after_pathsep(char_u *b, char_u *p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001893{
Bram Moolenaarb5ce04d2011-07-07 17:15:33 +02001894 return p > b && vim_ispathsep(p[-1])
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001895 && (!has_mbyte || (*mb_head_off)(b, p - 1) == 0);
1896}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001897
1898/*
1899 * Return TRUE if file names "f1" and "f2" are in the same directory.
1900 * "f1" may be a short name, "f2" must be a full path.
1901 */
1902 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001903same_directory(char_u *f1, char_u *f2)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001904{
1905 char_u ffname[MAXPATHL];
1906 char_u *t1;
1907 char_u *t2;
1908
Bram Moolenaar85a20022019-12-21 18:25:54 +01001909 // safety check
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001910 if (f1 == NULL || f2 == NULL)
1911 return FALSE;
1912
1913 (void)vim_FullName(f1, ffname, MAXPATHL, FALSE);
1914 t1 = gettail_sep(ffname);
1915 t2 = gettail_sep(f2);
1916 return (t1 - ffname == t2 - f2
1917 && pathcmp((char *)ffname, (char *)f2, (int)(t1 - ffname)) == 0);
1918}
1919
Bram Moolenaar7c365fb2018-06-29 20:28:31 +02001920#if defined(FEAT_SESSION) || defined(FEAT_AUTOCHDIR) \
Bram Moolenaar097148e2020-08-11 21:58:20 +02001921 || defined(MSWIN) || defined(FEAT_GUI_GTK) \
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001922 || defined(FEAT_NETBEANS_INTG) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 || defined(PROTO)
1924/*
1925 * Change to a file's directory.
1926 * Caller must call shorten_fnames()!
1927 * Return OK or FAIL.
1928 */
1929 int
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001930vim_chdirfile(char_u *fname, char *trigger_autocmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001932 char_u old_dir[MAXPATHL];
1933 char_u new_dir[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001935 if (mch_dirname(old_dir, MAXPATHL) != OK)
1936 *old_dir = NUL;
1937
1938 vim_strncpy(new_dir, fname, MAXPATHL - 1);
1939 *gettail_sep(new_dir) = NUL;
1940
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01001941 if (pathcmp((char *)old_dir, (char *)new_dir, -1) == 0)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001942 // nothing to do
zeertzjq73257142022-02-02 13:16:37 +00001943 return OK;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001944
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001945 if (trigger_autocmd != NULL)
1946 trigger_DirChangedPre((char_u *)trigger_autocmd, new_dir);
1947
zeertzjq73257142022-02-02 13:16:37 +00001948 if (mch_chdir((char *)new_dir) != 0)
1949 return FAIL;
1950
1951 if (trigger_autocmd != NULL)
1952 apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd,
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001953 new_dir, FALSE, curbuf);
zeertzjq73257142022-02-02 13:16:37 +00001954 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955}
1956#endif
1957
1958#if defined(STAT_IGNORES_SLASH) || defined(PROTO)
1959/*
1960 * Check if "name" ends in a slash and is not a directory.
1961 * Used for systems where stat() ignores a trailing slash on a file name.
1962 * The Vim code assumes a trailing slash is only ignored for a directory.
1963 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001964 static int
Bram Moolenaard8492792017-03-16 12:22:38 +01001965illegal_slash(const char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 if (name[0] == NUL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001968 return FALSE; // no file name is not illegal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (name[strlen(name) - 1] != '/')
Bram Moolenaar85a20022019-12-21 18:25:54 +01001970 return FALSE; // no trailing slash
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 if (mch_isdir((char_u *)name))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001972 return FALSE; // trailing slash for a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 return TRUE;
1974}
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001975
1976/*
1977 * Special implementation of mch_stat() for Solaris.
1978 */
1979 int
1980vim_stat(const char *name, stat_T *stp)
1981{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001982 // On Solaris stat() accepts "file/" as if it was "file". Return -1 if
1983 // the name ends in "/" and it's not a directory.
Bram Moolenaard8492792017-03-16 12:22:38 +01001984 return illegal_slash(name) ? -1 : stat(name, stp);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001985}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986#endif
1987
1988#if defined(CURSOR_SHAPE) || defined(PROTO)
1989
1990/*
1991 * Handling of cursor and mouse pointer shapes in various modes.
1992 */
1993
1994cursorentry_T shape_table[SHAPE_IDX_COUNT] =
1995{
Bram Moolenaar85a20022019-12-21 18:25:54 +01001996 // The values will be filled in from the 'guicursor' and 'mouseshape'
1997 // defaults when Vim starts.
1998 // Adjust the SHAPE_IDX_ defines when making changes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {0, 0, 0, 700L, 400L, 250L, 0, 0, "n", SHAPE_CURSOR+SHAPE_MOUSE},
2000 {0, 0, 0, 700L, 400L, 250L, 0, 0, "v", SHAPE_CURSOR+SHAPE_MOUSE},
2001 {0, 0, 0, 700L, 400L, 250L, 0, 0, "i", SHAPE_CURSOR+SHAPE_MOUSE},
2002 {0, 0, 0, 700L, 400L, 250L, 0, 0, "r", SHAPE_CURSOR+SHAPE_MOUSE},
2003 {0, 0, 0, 700L, 400L, 250L, 0, 0, "c", SHAPE_CURSOR+SHAPE_MOUSE},
2004 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ci", SHAPE_CURSOR+SHAPE_MOUSE},
2005 {0, 0, 0, 700L, 400L, 250L, 0, 0, "cr", SHAPE_CURSOR+SHAPE_MOUSE},
2006 {0, 0, 0, 700L, 400L, 250L, 0, 0, "o", SHAPE_CURSOR+SHAPE_MOUSE},
2007 {0, 0, 0, 700L, 400L, 250L, 0, 0, "ve", SHAPE_CURSOR+SHAPE_MOUSE},
2008 {0, 0, 0, 0L, 0L, 0L, 0, 0, "e", SHAPE_MOUSE},
2009 {0, 0, 0, 0L, 0L, 0L, 0, 0, "s", SHAPE_MOUSE},
2010 {0, 0, 0, 0L, 0L, 0L, 0, 0, "sd", SHAPE_MOUSE},
2011 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vs", SHAPE_MOUSE},
2012 {0, 0, 0, 0L, 0L, 0L, 0, 0, "vd", SHAPE_MOUSE},
2013 {0, 0, 0, 0L, 0L, 0L, 0, 0, "m", SHAPE_MOUSE},
2014 {0, 0, 0, 0L, 0L, 0L, 0, 0, "ml", SHAPE_MOUSE},
2015 {0, 0, 0, 100L, 100L, 100L, 0, 0, "sm", SHAPE_CURSOR},
2016};
2017
2018#ifdef FEAT_MOUSESHAPE
2019/*
2020 * Table with names for mouse shapes. Keep in sync with all the tables for
2021 * mch_set_mouse_shape()!.
2022 */
2023static char * mshape_names[] =
2024{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002025 "arrow", // default, must be the first one
2026 "blank", // hidden
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 "beam",
2028 "updown",
2029 "udsizing",
2030 "leftright",
2031 "lrsizing",
2032 "busy",
2033 "no",
2034 "crosshair",
2035 "hand1",
2036 "hand2",
2037 "pencil",
2038 "question",
2039 "rightup-arrow",
2040 "up-arrow",
2041 NULL
2042};
2043#endif
2044
2045/*
2046 * Parse the 'guicursor' option ("what" is SHAPE_CURSOR) or 'mouseshape'
2047 * ("what" is SHAPE_MOUSE).
2048 * Returns error message for an illegal option, NULL otherwise.
2049 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002050 char *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002051parse_shape_opt(int what)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052{
2053 char_u *modep;
2054 char_u *colonp;
2055 char_u *commap;
2056 char_u *slashp;
2057 char_u *p, *endp;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002058 int idx = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 int all_idx;
2060 int len;
2061 int i;
2062 long n;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002063 int found_ve = FALSE; // found "ve" flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 int round;
2065
2066 /*
2067 * First round: check for errors; second round: do it for real.
2068 */
2069 for (round = 1; round <= 2; ++round)
2070 {
2071 /*
2072 * Repeat for all comma separated parts.
2073 */
2074#ifdef FEAT_MOUSESHAPE
2075 if (what == SHAPE_MOUSE)
2076 modep = p_mouseshape;
2077 else
2078#endif
2079 modep = p_guicursor;
2080 while (*modep != NUL)
2081 {
2082 colonp = vim_strchr(modep, ':');
Bram Moolenaar24922ec2017-02-23 17:59:22 +01002083 commap = vim_strchr(modep, ',');
2084
2085 if (colonp == NULL || (commap != NULL && commap < colonp))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002086 return e_missing_colon_2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 if (colonp == modep)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002088 return e_illegal_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
2090 /*
2091 * Repeat for all mode's before the colon.
2092 * For the 'a' mode, we loop to handle all the modes.
2093 */
2094 all_idx = -1;
2095 while (modep < colonp || all_idx >= 0)
2096 {
2097 if (all_idx < 0)
2098 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002099 // Find the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 if (modep[1] == '-' || modep[1] == ':')
2101 len = 1;
2102 else
2103 len = 2;
2104 if (len == 1 && TOLOWER_ASC(modep[0]) == 'a')
2105 all_idx = SHAPE_IDX_COUNT - 1;
2106 else
2107 {
2108 for (idx = 0; idx < SHAPE_IDX_COUNT; ++idx)
2109 if (STRNICMP(modep, shape_table[idx].name, len)
2110 == 0)
2111 break;
2112 if (idx == SHAPE_IDX_COUNT
2113 || (shape_table[idx].used_for & what) == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002114 return e_illegal_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 if (len == 2 && modep[0] == 'v' && modep[1] == 'e')
2116 found_ve = TRUE;
2117 }
2118 modep += len + 1;
2119 }
2120
2121 if (all_idx >= 0)
2122 idx = all_idx--;
2123 else if (round == 2)
2124 {
2125#ifdef FEAT_MOUSESHAPE
2126 if (what == SHAPE_MOUSE)
2127 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002128 // Set the default, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 shape_table[idx].mshape = 0;
2130 }
2131 else
2132#endif
2133 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002134 // Set the defaults, for the missing parts
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 shape_table[idx].shape = SHAPE_BLOCK;
2136 shape_table[idx].blinkwait = 700L;
2137 shape_table[idx].blinkon = 400L;
2138 shape_table[idx].blinkoff = 250L;
2139 }
2140 }
2141
Bram Moolenaar85a20022019-12-21 18:25:54 +01002142 // Parse the part after the colon
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 for (p = colonp + 1; *p && *p != ','; )
2144 {
2145#ifdef FEAT_MOUSESHAPE
2146 if (what == SHAPE_MOUSE)
2147 {
2148 for (i = 0; ; ++i)
2149 {
2150 if (mshape_names[i] == NULL)
2151 {
2152 if (!VIM_ISDIGIT(*p))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002153 return e_illegal_mouseshape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 if (round == 2)
2155 shape_table[idx].mshape =
2156 getdigits(&p) + MSHAPE_NUMBERED;
2157 else
2158 (void)getdigits(&p);
2159 break;
2160 }
2161 len = (int)STRLEN(mshape_names[i]);
2162 if (STRNICMP(p, mshape_names[i], len) == 0)
2163 {
2164 if (round == 2)
2165 shape_table[idx].mshape = i;
2166 p += len;
2167 break;
2168 }
2169 }
2170 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002171 else // if (what == SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172#endif
2173 {
2174 /*
2175 * First handle the ones with a number argument.
2176 */
2177 i = *p;
2178 len = 0;
2179 if (STRNICMP(p, "ver", 3) == 0)
2180 len = 3;
2181 else if (STRNICMP(p, "hor", 3) == 0)
2182 len = 3;
2183 else if (STRNICMP(p, "blinkwait", 9) == 0)
2184 len = 9;
2185 else if (STRNICMP(p, "blinkon", 7) == 0)
2186 len = 7;
2187 else if (STRNICMP(p, "blinkoff", 8) == 0)
2188 len = 8;
2189 if (len != 0)
2190 {
2191 p += len;
2192 if (!VIM_ISDIGIT(*p))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002193 return e_digit_expected;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 n = getdigits(&p);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002195 if (len == 3) // "ver" or "hor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 {
2197 if (n == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002198 return e_illegal_percentage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 if (round == 2)
2200 {
2201 if (TOLOWER_ASC(i) == 'v')
2202 shape_table[idx].shape = SHAPE_VER;
2203 else
2204 shape_table[idx].shape = SHAPE_HOR;
2205 shape_table[idx].percentage = n;
2206 }
2207 }
2208 else if (round == 2)
2209 {
2210 if (len == 9)
2211 shape_table[idx].blinkwait = n;
2212 else if (len == 7)
2213 shape_table[idx].blinkon = n;
2214 else
2215 shape_table[idx].blinkoff = n;
2216 }
2217 }
2218 else if (STRNICMP(p, "block", 5) == 0)
2219 {
2220 if (round == 2)
2221 shape_table[idx].shape = SHAPE_BLOCK;
2222 p += 5;
2223 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002224 else // must be a highlight group name then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
2226 endp = vim_strchr(p, '-');
Bram Moolenaar85a20022019-12-21 18:25:54 +01002227 if (commap == NULL) // last part
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
2229 if (endp == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002230 endp = p + STRLEN(p); // find end of part
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232 else if (endp > commap || endp == NULL)
2233 endp = commap;
2234 slashp = vim_strchr(p, '/');
2235 if (slashp != NULL && slashp < endp)
2236 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002237 // "group/langmap_group"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 i = syn_check_group(p, (int)(slashp - p));
2239 p = slashp + 1;
2240 }
2241 if (round == 2)
2242 {
2243 shape_table[idx].id = syn_check_group(p,
2244 (int)(endp - p));
2245 shape_table[idx].id_lm = shape_table[idx].id;
2246 if (slashp != NULL && slashp < endp)
2247 shape_table[idx].id = i;
2248 }
2249 p = endp;
2250 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002251 } // if (what != SHAPE_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253 if (*p == '-')
2254 ++p;
2255 }
2256 }
2257 modep = p;
2258 if (*modep == ',')
2259 ++modep;
2260 }
2261 }
2262
Bram Moolenaar85a20022019-12-21 18:25:54 +01002263 // If the 's' flag is not given, use the 'v' cursor for 's'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 if (!found_ve)
2265 {
2266#ifdef FEAT_MOUSESHAPE
2267 if (what == SHAPE_MOUSE)
2268 {
2269 shape_table[SHAPE_IDX_VE].mshape = shape_table[SHAPE_IDX_V].mshape;
2270 }
2271 else
2272#endif
2273 {
2274 shape_table[SHAPE_IDX_VE].shape = shape_table[SHAPE_IDX_V].shape;
2275 shape_table[SHAPE_IDX_VE].percentage =
2276 shape_table[SHAPE_IDX_V].percentage;
2277 shape_table[SHAPE_IDX_VE].blinkwait =
2278 shape_table[SHAPE_IDX_V].blinkwait;
2279 shape_table[SHAPE_IDX_VE].blinkon =
2280 shape_table[SHAPE_IDX_V].blinkon;
2281 shape_table[SHAPE_IDX_VE].blinkoff =
2282 shape_table[SHAPE_IDX_V].blinkoff;
2283 shape_table[SHAPE_IDX_VE].id = shape_table[SHAPE_IDX_V].id;
2284 shape_table[SHAPE_IDX_VE].id_lm = shape_table[SHAPE_IDX_V].id_lm;
2285 }
2286 }
2287
2288 return NULL;
2289}
2290
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002291# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2292 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293/*
2294 * Return the index into shape_table[] for the current mode.
2295 * When "mouse" is TRUE, consider indexes valid for the mouse pointer.
2296 */
2297 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002298get_shape_idx(int mouse)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299{
2300#ifdef FEAT_MOUSESHAPE
Bram Moolenaar24959102022-05-07 20:01:16 +01002301 if (mouse && (State == MODE_HITRETURN || State == MODE_ASKMORE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303# ifdef FEAT_GUI
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00002304 int x, y;
2305 gui_mch_getmouse(&x, &y);
2306 if (Y_2_ROW(y) == Rows - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 return SHAPE_IDX_MOREL;
2308# endif
2309 return SHAPE_IDX_MORE;
2310 }
2311 if (mouse && drag_status_line)
2312 return SHAPE_IDX_SDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (mouse && drag_sep_line)
2314 return SHAPE_IDX_VDRAG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002316 if (!mouse && State == MODE_SHOWMATCH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 return SHAPE_IDX_SM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 if (State & VREPLACE_FLAG)
2319 return SHAPE_IDX_R;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (State & REPLACE_FLAG)
2321 return SHAPE_IDX_R;
Bram Moolenaar24959102022-05-07 20:01:16 +01002322 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 return SHAPE_IDX_I;
Bram Moolenaar24959102022-05-07 20:01:16 +01002324 if (State & MODE_CMDLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
2326 if (cmdline_at_end())
2327 return SHAPE_IDX_C;
2328 if (cmdline_overstrike())
2329 return SHAPE_IDX_CR;
2330 return SHAPE_IDX_CI;
2331 }
2332 if (finish_op)
2333 return SHAPE_IDX_O;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (VIsual_active)
2335 {
2336 if (*p_sel == 'e')
2337 return SHAPE_IDX_VE;
2338 else
2339 return SHAPE_IDX_V;
2340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 return SHAPE_IDX_N;
2342}
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
2345# if defined(FEAT_MOUSESHAPE) || defined(PROTO)
2346static int old_mouse_shape = 0;
2347
2348/*
2349 * Set the mouse shape:
2350 * If "shape" is -1, use shape depending on the current mode,
2351 * depending on the current state.
2352 * If "shape" is -2, only update the shape when it's CLINE or STATUS (used
2353 * when the mouse moves off the status or command line).
2354 */
2355 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002356update_mouseshape(int shape_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357{
2358 int new_mouse_shape;
2359
Bram Moolenaar85a20022019-12-21 18:25:54 +01002360 // Only works in GUI mode.
Bram Moolenaar6bb68362005-03-22 23:03:44 +00002361 if (!gui.in_use || gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 return;
2363
Bram Moolenaar85a20022019-12-21 18:25:54 +01002364 // Postpone the updating when more is to come. Speeds up executing of
2365 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 if (shape_idx == -1 && char_avail())
2367 {
2368 postponed_mouseshape = TRUE;
2369 return;
2370 }
2371
Bram Moolenaar85a20022019-12-21 18:25:54 +01002372 // When ignoring the mouse don't change shape on the statusline.
Bram Moolenaar14716812006-05-04 21:54:08 +00002373 if (*p_mouse == NUL
2374 && (shape_idx == SHAPE_IDX_CLINE
2375 || shape_idx == SHAPE_IDX_STATUS
2376 || shape_idx == SHAPE_IDX_VSEP))
2377 shape_idx = -2;
2378
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 if (shape_idx == -2
2380 && old_mouse_shape != shape_table[SHAPE_IDX_CLINE].mshape
2381 && old_mouse_shape != shape_table[SHAPE_IDX_STATUS].mshape
2382 && old_mouse_shape != shape_table[SHAPE_IDX_VSEP].mshape)
2383 return;
2384 if (shape_idx < 0)
2385 new_mouse_shape = shape_table[get_shape_idx(TRUE)].mshape;
2386 else
2387 new_mouse_shape = shape_table[shape_idx].mshape;
2388 if (new_mouse_shape != old_mouse_shape)
2389 {
2390 mch_set_mouse_shape(new_mouse_shape);
2391 old_mouse_shape = new_mouse_shape;
2392 }
2393 postponed_mouseshape = FALSE;
2394}
2395# endif
2396
Bram Moolenaar85a20022019-12-21 18:25:54 +01002397#endif // CURSOR_SHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400/*
2401 * Change directory to "new_dir". If FEAT_SEARCHPATH is defined, search
2402 * 'cdpath' for relative directory names, otherwise just mch_chdir().
2403 */
2404 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002405vim_chdir(char_u *new_dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
2407#ifndef FEAT_SEARCHPATH
2408 return mch_chdir((char *)new_dir);
2409#else
2410 char_u *dir_name;
2411 int r;
2412
2413 dir_name = find_directory_in_path(new_dir, (int)STRLEN(new_dir),
2414 FNAME_MESS, curbuf->b_ffname);
2415 if (dir_name == NULL)
2416 return -1;
2417 r = mch_chdir((char *)dir_name);
2418 vim_free(dir_name);
2419 return r;
2420#endif
2421}
2422
2423/*
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002424 * Get user name from machine-specific function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 * Returns the user name in "buf[len]".
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002426 * Some systems are quite slow in obtaining the user name (Windows NT), thus
2427 * cache the result.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 * Returns OK or FAIL.
2429 */
2430 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002431get_user_name(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002433 if (username == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
2435 if (mch_get_user_name(buf, len) == FAIL)
2436 return FAIL;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002437 username = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439 else
Bram Moolenaarbbebc852005-07-18 21:47:53 +00002440 vim_strncpy(buf, username, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 return OK;
2442}
2443
Bram Moolenaar7ce5b2b2022-05-16 19:40:59 +01002444#if defined(EXITFREE) || defined(PROTO)
Yegappan Lakshmanancbae5802021-08-06 21:51:55 +02002445/*
2446 * Free the memory allocated by get_user_name()
2447 */
2448 void
2449free_username(void)
2450{
2451 vim_free(username);
2452}
Dominique Pelle748b3082022-01-08 12:41:16 +00002453#endif
Yegappan Lakshmanancbae5802021-08-06 21:51:55 +02002454
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455#ifndef HAVE_QSORT
2456/*
2457 * Our own qsort(), for systems that don't have it.
2458 * It's simple and slow. From the K&R C book.
2459 */
2460 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002461qsort(
2462 void *base,
2463 size_t elm_count,
2464 size_t elm_size,
2465 int (*cmp)(const void *, const void *))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 char_u *buf;
2468 char_u *p1;
2469 char_u *p2;
2470 int i, j;
2471 int gap;
2472
Bram Moolenaar964b3742019-05-24 18:54:09 +02002473 buf = alloc(elm_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (buf == NULL)
2475 return;
2476
2477 for (gap = elm_count / 2; gap > 0; gap /= 2)
2478 for (i = gap; i < elm_count; ++i)
2479 for (j = i - gap; j >= 0; j -= gap)
2480 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002481 // Compare the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 p1 = (char_u *)base + j * elm_size;
2483 p2 = (char_u *)base + (j + gap) * elm_size;
2484 if ((*cmp)((void *)p1, (void *)p2) <= 0)
2485 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002486 // Exchange the elements.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 mch_memmove(buf, p1, elm_size);
2488 mch_memmove(p1, p2, elm_size);
2489 mch_memmove(p2, buf, elm_size);
2490 }
2491
2492 vim_free(buf);
2493}
2494#endif
2495
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 * The putenv() implementation below comes from the "screen" program.
2498 * Included with permission from Juergen Weigert.
2499 * See pty.c for the copyright notice.
2500 */
2501
2502/*
2503 * putenv -- put value into environment
2504 *
2505 * Usage: i = putenv (string)
2506 * int i;
2507 * char *string;
2508 *
2509 * where string is of the form <name>=<value>.
2510 * Putenv returns 0 normally, -1 on error (not enough core for malloc).
2511 *
2512 * Putenv may need to add a new name into the environment, or to
2513 * associate a value longer than the current value with a particular
2514 * name. So, to make life simpler, putenv() copies your entire
2515 * environment into the heap (i.e. malloc()) from the stack
2516 * (i.e. where it resides when your process is initiated) the first
2517 * time you call it.
2518 *
2519 * (history removed, not very interesting. See the "screen" sources.)
2520 */
2521
2522#if !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
2523
Bram Moolenaar85a20022019-12-21 18:25:54 +01002524#define EXTRASIZE 5 // increment to add to env. size
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
Bram Moolenaar85a20022019-12-21 18:25:54 +01002526static int envsize = -1; // current size of environment
2527extern char **environ; // the global which is your env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaar85a20022019-12-21 18:25:54 +01002529static int findenv(char *name); // look for a name in the env.
2530static int newenv(void); // copy env. from stack to heap
2531static int moreenv(void); // incr. size of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
2533 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002534putenv(const char *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
2536 int i;
2537 char *p;
2538
2539 if (envsize < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002540 { // first time putenv called
2541 if (newenv() < 0) // copy env. to heap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 return -1;
2543 }
2544
Bram Moolenaar85a20022019-12-21 18:25:54 +01002545 i = findenv((char *)string); // look for name in environment
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
2547 if (i < 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002548 { // name must be added
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 for (i = 0; environ[i]; i++);
2550 if (i >= (envsize - 1))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002551 { // need new slot
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (moreenv() < 0)
2553 return -1;
2554 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002555 p = alloc(strlen(string) + 1);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002556 if (p == NULL) // not enough core
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 return -1;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002558 environ[i + 1] = 0; // new end of env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
2560 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01002561 { // name already in env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 p = vim_realloc(environ[i], strlen(string) + 1);
2563 if (p == NULL)
2564 return -1;
2565 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002566 sprintf(p, "%s", string); // copy into env.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 environ[i] = p;
2568
2569 return 0;
2570}
2571
2572 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002573findenv(char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
2575 char *namechar, *envchar;
2576 int i, found;
2577
2578 found = 0;
2579 for (i = 0; environ[i] && !found; i++)
2580 {
2581 envchar = environ[i];
2582 namechar = name;
2583 while (*namechar && *namechar != '=' && (*namechar == *envchar))
2584 {
2585 namechar++;
2586 envchar++;
2587 }
2588 found = ((*namechar == '\0' || *namechar == '=') && *envchar == '=');
2589 }
2590 return found ? i - 1 : -1;
2591}
2592
2593 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002594newenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
2596 char **env, *elem;
2597 int i, esize;
2598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 for (i = 0; environ[i]; i++)
2600 ;
Bram Moolenaard0573012017-10-28 21:11:06 +02002601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 esize = i + EXTRASIZE + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002603 env = ALLOC_MULT(char *, esize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (env == NULL)
2605 return -1;
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 for (i = 0; environ[i]; i++)
2608 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002609 elem = alloc(strlen(environ[i]) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 if (elem == NULL)
2611 return -1;
2612 env[i] = elem;
2613 strcpy(elem, environ[i]);
2614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615
2616 env[i] = 0;
2617 environ = env;
2618 envsize = esize;
2619 return 0;
2620}
2621
2622 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002623moreenv(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
2625 int esize;
2626 char **env;
2627
2628 esize = envsize + EXTRASIZE;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002629 env = vim_realloc((char *)environ, esize * sizeof (*env));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 if (env == 0)
2631 return -1;
2632 environ = env;
2633 envsize = esize;
2634 return 0;
2635}
2636
2637# ifdef USE_VIMPTY_GETENV
Bram Moolenaar613fe7a2017-07-22 21:11:53 +02002638/*
2639 * Used for mch_getenv() for Mac.
2640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002642vimpty_getenv(const char_u *string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
2644 int i;
2645 char_u *p;
2646
2647 if (envsize < 0)
2648 return NULL;
2649
2650 i = findenv((char *)string);
2651
2652 if (i < 0)
2653 return NULL;
2654
2655 p = vim_strchr((char_u *)environ[i], '=');
2656 return (p + 1);
2657}
2658# endif
2659
Bram Moolenaar85a20022019-12-21 18:25:54 +01002660#endif // !defined(HAVE_SETENV) && !defined(HAVE_PUTENV)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002661
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002662#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002663/*
2664 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
2665 * rights to write into.
2666 */
2667 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002668filewritable(char_u *fname)
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002669{
2670 int retval = 0;
2671#if defined(UNIX) || defined(VMS)
2672 int perm = 0;
2673#endif
2674
2675#if defined(UNIX) || defined(VMS)
2676 perm = mch_getperm(fname);
2677#endif
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002678 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +01002679# ifdef MSWIN
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002680 mch_writable(fname) &&
2681# else
2682# if defined(UNIX) || defined(VMS)
2683 (perm & 0222) &&
2684# endif
2685# endif
2686 mch_access((char *)fname, W_OK) == 0
2687 )
Bram Moolenaarc4a06d32005-06-07 21:04:49 +00002688 {
2689 ++retval;
2690 if (mch_isdir(fname))
2691 ++retval;
2692 }
2693 return retval;
2694}
2695#endif
Bram Moolenaar6bab4d12005-06-16 21:53:56 +00002696
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002697#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
2698/*
2699 * Read 2 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002700 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002701 */
2702 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002703get2c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002704{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002705 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002706
2707 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002708 if (n == EOF) return -1;
2709 c = getc(fd);
2710 if (c == EOF) return -1;
2711 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002712}
2713
2714/*
2715 * Read 3 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002716 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002717 */
2718 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002719get3c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002720{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002721 int c, n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002722
2723 n = getc(fd);
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002724 if (n == EOF) return -1;
2725 c = getc(fd);
2726 if (c == EOF) return -1;
2727 n = (n << 8) + c;
2728 c = getc(fd);
2729 if (c == EOF) return -1;
2730 return (n << 8) + c;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002731}
2732
2733/*
2734 * Read 4 bytes from "fd" and turn them into an int, MSB first.
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002735 * Returns -1 when encountering EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002736 */
2737 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002738get4c(FILE *fd)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002739{
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002740 int c;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002741 // Use unsigned rather than int otherwise result is undefined
2742 // when left-shift sets the MSB.
Bram Moolenaar95235e62013-09-08 16:07:07 +02002743 unsigned n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002744
Bram Moolenaare26e0d22018-03-20 12:34:04 +01002745 c = getc(fd);
2746 if (c == EOF) return -1;
2747 n = (unsigned)c;
2748 c = getc(fd);
2749 if (c == EOF) return -1;
2750 n = (n << 8) + (unsigned)c;
2751 c = getc(fd);
2752 if (c == EOF) return -1;
2753 n = (n << 8) + (unsigned)c;
2754 c = getc(fd);
2755 if (c == EOF) return -1;
2756 n = (n << 8) + (unsigned)c;
Bram Moolenaar95235e62013-09-08 16:07:07 +02002757 return (int)n;
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002758}
2759
2760/*
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002761 * Read a string of length "cnt" from "fd" into allocated memory.
2762 * Returns NULL when out of memory or unable to read that many bytes.
2763 */
2764 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002765read_string(FILE *fd, int cnt)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002766{
2767 char_u *str;
2768 int i;
2769 int c;
2770
Bram Moolenaar85a20022019-12-21 18:25:54 +01002771 // allocate memory
Bram Moolenaar964b3742019-05-24 18:54:09 +02002772 str = alloc(cnt + 1);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002773 if (str != NULL)
2774 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002775 // Read the string. Quit when running into the EOF.
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002776 for (i = 0; i < cnt; ++i)
2777 {
2778 c = getc(fd);
2779 if (c == EOF)
2780 {
2781 vim_free(str);
2782 return NULL;
2783 }
2784 str[i] = c;
2785 }
2786 str[i] = NUL;
2787 }
2788 return str;
2789}
2790
2791/*
2792 * Write a number to file "fd", MSB first, in "len" bytes.
2793 */
2794 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002795put_bytes(FILE *fd, long_u nr, int len)
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002796{
2797 int i;
2798
2799 for (i = len - 1; i >= 0; --i)
2800 if (putc((int)(nr >> (i * 8)), fd) == EOF)
2801 return FAIL;
2802 return OK;
2803}
2804
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002805#endif
Bram Moolenaar10b7b392012-01-10 16:28:45 +01002806
Bram Moolenaar85a20022019-12-21 18:25:54 +01002807#ifndef PROTO // proto is defined in vim.h
Bram Moolenaar51628222016-12-01 23:03:28 +01002808# ifdef ELAPSED_TIMEVAL
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002809/*
2810 * Return time in msec since "start_tv".
2811 */
2812 long
2813elapsed(struct timeval *start_tv)
2814{
2815 struct timeval now_tv;
2816
2817 gettimeofday(&now_tv, NULL);
2818 return (now_tv.tv_sec - start_tv->tv_sec) * 1000L
2819 + (now_tv.tv_usec - start_tv->tv_usec) / 1000L;
2820}
Bram Moolenaar51628222016-12-01 23:03:28 +01002821# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002822
Bram Moolenaar51628222016-12-01 23:03:28 +01002823# ifdef ELAPSED_TICKCOUNT
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002824/*
2825 * Return time in msec since "start_tick".
2826 */
2827 long
2828elapsed(DWORD start_tick)
2829{
2830 DWORD now = GetTickCount();
2831
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002832 return (long)now - (long)start_tick;
2833}
Bram Moolenaar51628222016-12-01 23:03:28 +01002834# endif
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01002835#endif
Bram Moolenaar20608922018-04-21 22:30:08 +02002836
2837#if defined(FEAT_JOB_CHANNEL) \
2838 || (defined(UNIX) && (!defined(USE_SYSTEM) \
2839 || (defined(FEAT_GUI) && defined(FEAT_TERMINAL)))) \
2840 || defined(PROTO)
2841/*
2842 * Parse "cmd" and put the white-separated parts in "argv".
2843 * "argv" is an allocated array with "argc" entries and room for 4 more.
2844 * Returns FAIL when out of memory.
2845 */
2846 int
2847mch_parse_cmd(char_u *cmd, int use_shcf, char ***argv, int *argc)
2848{
2849 int i;
2850 char_u *p, *d;
2851 int inquote;
2852
2853 /*
2854 * Do this loop twice:
2855 * 1: find number of arguments
2856 * 2: separate them and build argv[]
2857 */
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002858 for (i = 1; i <= 2; ++i)
Bram Moolenaar20608922018-04-21 22:30:08 +02002859 {
2860 p = skipwhite(cmd);
2861 inquote = FALSE;
2862 *argc = 0;
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002863 while (*p != NUL)
Bram Moolenaar20608922018-04-21 22:30:08 +02002864 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002865 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02002866 (*argv)[*argc] = (char *)p;
2867 ++*argc;
2868 d = p;
2869 while (*p != NUL && (inquote || (*p != ' ' && *p != TAB)))
2870 {
2871 if (p[0] == '"')
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02002872 // quotes surrounding an argument and are dropped
Bram Moolenaar20608922018-04-21 22:30:08 +02002873 inquote = !inquote;
2874 else
2875 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02002876 if (rem_backslash(p))
Bram Moolenaar20608922018-04-21 22:30:08 +02002877 {
Bram Moolenaar1df2fa42018-10-07 21:36:11 +02002878 // First pass: skip over "\ " and "\"".
2879 // Second pass: Remove the backslash.
Bram Moolenaar20608922018-04-21 22:30:08 +02002880 ++p;
2881 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002882 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02002883 *d++ = *p;
2884 }
2885 ++p;
2886 }
2887 if (*p == NUL)
2888 {
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002889 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02002890 *d++ = NUL;
2891 break;
2892 }
Bram Moolenaar7851b1c2020-03-26 16:27:38 +01002893 if (i == 2)
Bram Moolenaar20608922018-04-21 22:30:08 +02002894 *d++ = NUL;
2895 p = skipwhite(p + 1);
2896 }
2897 if (*argv == NULL)
2898 {
2899 if (use_shcf)
2900 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002901 // Account for possible multiple args in p_shcf.
Bram Moolenaar20608922018-04-21 22:30:08 +02002902 p = p_shcf;
2903 for (;;)
2904 {
2905 p = skiptowhite(p);
2906 if (*p == NUL)
2907 break;
2908 ++*argc;
2909 p = skipwhite(p);
2910 }
2911 }
2912
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002913 *argv = ALLOC_MULT(char *, *argc + 4);
Bram Moolenaar85a20022019-12-21 18:25:54 +01002914 if (*argv == NULL) // out of memory
Bram Moolenaar20608922018-04-21 22:30:08 +02002915 return FAIL;
2916 }
2917 }
2918 return OK;
2919}
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002920
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002921/*
2922 * Build "argv[argc]" from the string "cmd".
2923 * "argv[argc]" is set to NULL;
2924 * Return FAIL when out of memory.
2925 */
2926 int
2927build_argv_from_string(char_u *cmd, char ***argv, int *argc)
2928{
2929 char_u *cmd_copy;
2930 int i;
2931
Bram Moolenaar85a20022019-12-21 18:25:54 +01002932 // Make a copy, parsing will modify "cmd".
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002933 cmd_copy = vim_strsave(cmd);
2934 if (cmd_copy == NULL
2935 || mch_parse_cmd(cmd_copy, FALSE, argv, argc) == FAIL)
2936 {
2937 vim_free(cmd_copy);
2938 return FAIL;
2939 }
2940 for (i = 0; i < *argc; i++)
2941 (*argv)[i] = (char *)vim_strsave((char_u *)(*argv)[i]);
2942 (*argv)[*argc] = NULL;
2943 vim_free(cmd_copy);
2944 return OK;
2945}
2946
Dominique Pellee8741a72022-01-17 11:23:45 +00002947# if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002948/*
2949 * Build "argv[argc]" from the list "l".
2950 * "argv[argc]" is set to NULL;
2951 * Return FAIL when out of memory.
2952 */
2953 int
2954build_argv_from_list(list_T *l, char ***argv, int *argc)
2955{
2956 listitem_T *li;
2957 char_u *s;
2958
Bram Moolenaar85a20022019-12-21 18:25:54 +01002959 // Pass argv[] to mch_call_shell().
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002960 *argv = ALLOC_MULT(char *, l->lv_len + 1);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002961 if (*argv == NULL)
2962 return FAIL;
2963 *argc = 0;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002964 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002965 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002966 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002967 if (s == NULL)
2968 {
2969 int i;
2970
2971 for (i = 0; i < *argc; ++i)
Bram Moolenaardf195602020-04-13 18:13:33 +02002972 VIM_CLEAR((*argv)[i]);
Bram Moolenaar7c25a7c2021-10-05 19:19:35 +01002973 (*argv)[0] = NULL;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02002974 return FAIL;
2975 }
2976 (*argv)[*argc] = (char *)vim_strsave(s);
2977 *argc += 1;
2978 }
2979 (*argv)[*argc] = NULL;
2980 return OK;
2981}
2982# endif
Bram Moolenaar20608922018-04-21 22:30:08 +02002983#endif
Bram Moolenaar57da6982019-09-13 22:30:11 +02002984
2985/*
2986 * Change the behavior of vterm.
2987 * 0: As usual.
2988 * 1: Windows 10 version 1809
2989 * The bug causes unstable handling of ambiguous width character.
Bram Moolenaar36e7a822019-11-13 21:49:24 +01002990 * 2: Windows 10 version 1903 & 1909
Bram Moolenaar57da6982019-09-13 22:30:11 +02002991 * Use the wrong result because each result is different.
2992 * 3: Windows 10 insider preview (current latest logic)
2993 */
2994 int
2995get_special_pty_type(void)
2996{
2997#ifdef MSWIN
2998 return get_conpty_type();
2999#else
3000 return 0;
3001#endif
3002}