Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 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 | * misc1.c: functions that didn't seem to fit elsewhere |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | #include "version.h" |
| 16 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 17 | #if defined(FEAT_CMDL_COMPL) && defined(MSWIN) |
Bram Moolenaar | 828c3d7 | 2018-06-19 18:58:07 +0200 | [diff] [blame] | 18 | # include <lm.h> |
| 19 | #endif |
| 20 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 21 | static char_u *vim_version_dir(char_u *vimdir); |
| 22 | static char_u *remove_tail(char_u *p, char_u *pend, char_u *name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 23 | |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 24 | #define URL_SLASH 1 /* path_is_url() has found "://" */ |
| 25 | #define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */ |
| 26 | |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 27 | /* All user names (for ~user completion as done by shell). */ |
| 28 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 29 | static garray_T ga_users; |
| 30 | #endif |
| 31 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 32 | /* |
| 33 | * Count the size (in window cells) of the indent in the current line. |
| 34 | */ |
| 35 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 36 | get_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 37 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 38 | #ifdef FEAT_VARTABS |
| 39 | return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts, |
| 40 | curbuf->b_p_vts_array, FALSE); |
| 41 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 42 | return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 43 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Count the size (in window cells) of the indent in line "lnum". |
| 48 | */ |
| 49 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 50 | get_indent_lnum(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 51 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 52 | #ifdef FEAT_VARTABS |
| 53 | return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts, |
| 54 | curbuf->b_p_vts_array, FALSE); |
| 55 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 56 | return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 57 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | #if defined(FEAT_FOLDING) || defined(PROTO) |
| 61 | /* |
| 62 | * Count the size (in window cells) of the indent in line "lnum" of buffer |
| 63 | * "buf". |
| 64 | */ |
| 65 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 66 | get_indent_buf(buf_T *buf, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 67 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 68 | #ifdef FEAT_VARTABS |
| 69 | return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE), |
| 70 | (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE); |
| 71 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 72 | return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 73 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 74 | } |
| 75 | #endif |
| 76 | |
| 77 | /* |
| 78 | * count the size (in window cells) of the indent in line "ptr", with |
| 79 | * 'tabstop' at "ts" |
| 80 | */ |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 81 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 82 | get_indent_str( |
| 83 | char_u *ptr, |
| 84 | int ts, |
| 85 | int list) /* if TRUE, count only screen size for tabs */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 86 | { |
| 87 | int count = 0; |
| 88 | |
| 89 | for ( ; *ptr; ++ptr) |
| 90 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 91 | if (*ptr == TAB) |
| 92 | { |
| 93 | if (!list || lcs_tab1) /* count a tab for what it is worth */ |
| 94 | count += ts - (count % ts); |
| 95 | else |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 96 | /* In list mode, when tab is not set, count screen char width |
| 97 | * for Tab, displays: ^I */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 98 | count += ptr2cells(ptr); |
| 99 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 100 | else if (*ptr == ' ') |
| 101 | ++count; /* count a space for one */ |
| 102 | else |
| 103 | break; |
| 104 | } |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 105 | return count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 108 | #ifdef FEAT_VARTABS |
| 109 | /* |
| 110 | * Count the size (in window cells) of the indent in line "ptr", using |
| 111 | * variable tabstops. |
| 112 | * if "list" is TRUE, count only screen size for tabs. |
| 113 | */ |
| 114 | int |
| 115 | get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list) |
| 116 | { |
| 117 | int count = 0; |
| 118 | |
| 119 | for ( ; *ptr; ++ptr) |
| 120 | { |
| 121 | if (*ptr == TAB) /* count a tab for what it is worth */ |
| 122 | { |
| 123 | if (!list || lcs_tab1) |
| 124 | count += tabstop_padding(count, ts, vts); |
| 125 | else |
| 126 | /* In list mode, when tab is not set, count screen char width |
| 127 | * for Tab, displays: ^I */ |
| 128 | count += ptr2cells(ptr); |
| 129 | } |
| 130 | else if (*ptr == ' ') |
| 131 | ++count; /* count a space for one */ |
| 132 | else |
| 133 | break; |
| 134 | } |
| 135 | return count; |
| 136 | } |
| 137 | #endif |
| 138 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | /* |
| 140 | * Set the indent of the current line. |
| 141 | * Leaves the cursor on the first non-blank in the line. |
| 142 | * Caller must take care of undo. |
| 143 | * "flags": |
| 144 | * SIN_CHANGED: call changed_bytes() if the line was changed. |
| 145 | * SIN_INSERT: insert the indent in front of the line. |
| 146 | * SIN_UNDO: save line for undo before changing it. |
| 147 | * Returns TRUE if the line was changed. |
| 148 | */ |
| 149 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 150 | set_indent( |
| 151 | int size, /* measured in spaces */ |
| 152 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 153 | { |
| 154 | char_u *p; |
| 155 | char_u *newline; |
| 156 | char_u *oldline; |
| 157 | char_u *s; |
| 158 | int todo; |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 159 | int ind_len; /* measured in characters */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | int line_len; |
| 161 | int doit = FALSE; |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 162 | int ind_done = 0; /* measured in spaces */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 163 | #ifdef FEAT_VARTABS |
| 164 | int ind_col = 0; |
| 165 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 166 | int tab_pad; |
Bram Moolenaar | 5409c05 | 2005-03-18 20:27:04 +0000 | [diff] [blame] | 167 | int retval = FALSE; |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 168 | int orig_char_len = -1; /* number of initial whitespace chars when |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 169 | 'et' and 'pi' are both set */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * First check if there is anything to do and compute the number of |
| 173 | * characters needed for the indent. |
| 174 | */ |
| 175 | todo = size; |
| 176 | ind_len = 0; |
| 177 | p = oldline = ml_get_curline(); |
| 178 | |
| 179 | /* Calculate the buffer size for the new indent, and check to see if it |
| 180 | * isn't already set */ |
| 181 | |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 182 | /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and |
| 183 | * 'preserveindent' are set count the number of characters at the |
| 184 | * beginning of the line to be copied */ |
| 185 | if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 186 | { |
| 187 | /* If 'preserveindent' is set then reuse as much as possible of |
| 188 | * the existing indent structure for the new indent */ |
| 189 | if (!(flags & SIN_INSERT) && curbuf->b_p_pi) |
| 190 | { |
| 191 | ind_done = 0; |
| 192 | |
| 193 | /* count as many characters as we can use */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 194 | while (todo > 0 && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 195 | { |
| 196 | if (*p == TAB) |
| 197 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 198 | #ifdef FEAT_VARTABS |
| 199 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 200 | curbuf->b_p_vts_array); |
| 201 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 202 | tab_pad = (int)curbuf->b_p_ts |
| 203 | - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 204 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 205 | /* stop if this tab will overshoot the target */ |
| 206 | if (todo < tab_pad) |
| 207 | break; |
| 208 | todo -= tab_pad; |
| 209 | ++ind_len; |
| 210 | ind_done += tab_pad; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | --todo; |
| 215 | ++ind_len; |
| 216 | ++ind_done; |
| 217 | } |
| 218 | ++p; |
| 219 | } |
| 220 | |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 221 | #ifdef FEAT_VARTABS |
| 222 | /* These diverge from this point. */ |
| 223 | ind_col = ind_done; |
| 224 | #endif |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 225 | /* Set initial number of whitespace chars to copy if we are |
| 226 | * preserving indent but expandtab is set */ |
| 227 | if (curbuf->b_p_et) |
| 228 | orig_char_len = ind_len; |
| 229 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 230 | /* Fill to next tabstop with a tab, if possible */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 231 | #ifdef FEAT_VARTABS |
| 232 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 233 | curbuf->b_p_vts_array); |
| 234 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 235 | tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 236 | #endif |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 237 | if (todo >= tab_pad && orig_char_len == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 238 | { |
| 239 | doit = TRUE; |
| 240 | todo -= tab_pad; |
| 241 | ++ind_len; |
| 242 | /* ind_done += tab_pad; */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 243 | #ifdef FEAT_VARTABS |
| 244 | ind_col += tab_pad; |
| 245 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | /* count tabs required for indent */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 250 | #ifdef FEAT_VARTABS |
| 251 | for (;;) |
| 252 | { |
| 253 | tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts, |
| 254 | curbuf->b_p_vts_array); |
| 255 | if (todo < tab_pad) |
| 256 | break; |
| 257 | if (*p != TAB) |
| 258 | doit = TRUE; |
| 259 | else |
| 260 | ++p; |
| 261 | todo -= tab_pad; |
| 262 | ++ind_len; |
| 263 | ind_col += tab_pad; |
| 264 | } |
| 265 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 266 | while (todo >= (int)curbuf->b_p_ts) |
| 267 | { |
| 268 | if (*p != TAB) |
| 269 | doit = TRUE; |
| 270 | else |
| 271 | ++p; |
| 272 | todo -= (int)curbuf->b_p_ts; |
| 273 | ++ind_len; |
| 274 | /* ind_done += (int)curbuf->b_p_ts; */ |
| 275 | } |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 276 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 277 | } |
| 278 | /* count spaces required for indent */ |
| 279 | while (todo > 0) |
| 280 | { |
| 281 | if (*p != ' ') |
| 282 | doit = TRUE; |
| 283 | else |
| 284 | ++p; |
| 285 | --todo; |
| 286 | ++ind_len; |
| 287 | /* ++ind_done; */ |
| 288 | } |
| 289 | |
| 290 | /* Return if the indent is OK already. */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 291 | if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 292 | return FALSE; |
| 293 | |
| 294 | /* Allocate memory for the new line. */ |
| 295 | if (flags & SIN_INSERT) |
| 296 | p = oldline; |
| 297 | else |
| 298 | p = skipwhite(p); |
| 299 | line_len = (int)STRLEN(p) + 1; |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 300 | |
| 301 | /* If 'preserveindent' and 'expandtab' are both set keep the original |
| 302 | * characters and allocate accordingly. We will fill the rest with spaces |
| 303 | * after the if (!curbuf->b_p_et) below. */ |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 304 | if (orig_char_len != -1) |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 305 | { |
| 306 | newline = alloc(orig_char_len + size - ind_done + line_len); |
| 307 | if (newline == NULL) |
| 308 | return FALSE; |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 309 | todo = size - ind_done; |
| 310 | ind_len = orig_char_len + todo; /* Set total length of indent in |
| 311 | * characters, which may have been |
| 312 | * undercounted until now */ |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 313 | p = oldline; |
| 314 | s = newline; |
| 315 | while (orig_char_len > 0) |
| 316 | { |
| 317 | *s++ = *p++; |
| 318 | orig_char_len--; |
| 319 | } |
Bram Moolenaar | 913626c | 2008-01-03 11:43:42 +0000 | [diff] [blame] | 320 | |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 321 | /* Skip over any additional white space (useful when newindent is less |
| 322 | * than old) */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 323 | while (VIM_ISWHITE(*p)) |
Bram Moolenaar | 913626c | 2008-01-03 11:43:42 +0000 | [diff] [blame] | 324 | ++p; |
Bram Moolenaar | cc00b95 | 2007-08-11 12:32:57 +0000 | [diff] [blame] | 325 | |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 326 | } |
| 327 | else |
| 328 | { |
| 329 | todo = size; |
| 330 | newline = alloc(ind_len + line_len); |
| 331 | if (newline == NULL) |
| 332 | return FALSE; |
| 333 | s = newline; |
| 334 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 335 | |
| 336 | /* Put the characters in the new line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 337 | /* if 'expandtab' isn't set: use TABs */ |
| 338 | if (!curbuf->b_p_et) |
| 339 | { |
| 340 | /* If 'preserveindent' is set then reuse as much as possible of |
| 341 | * the existing indent structure for the new indent */ |
| 342 | if (!(flags & SIN_INSERT) && curbuf->b_p_pi) |
| 343 | { |
| 344 | p = oldline; |
| 345 | ind_done = 0; |
| 346 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 347 | while (todo > 0 && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 348 | { |
| 349 | if (*p == TAB) |
| 350 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 351 | #ifdef FEAT_VARTABS |
| 352 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 353 | curbuf->b_p_vts_array); |
| 354 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 355 | tab_pad = (int)curbuf->b_p_ts |
| 356 | - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 357 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 358 | /* stop if this tab will overshoot the target */ |
| 359 | if (todo < tab_pad) |
| 360 | break; |
| 361 | todo -= tab_pad; |
| 362 | ind_done += tab_pad; |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | --todo; |
| 367 | ++ind_done; |
| 368 | } |
| 369 | *s++ = *p++; |
| 370 | } |
| 371 | |
| 372 | /* Fill to next tabstop with a tab, if possible */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 373 | #ifdef FEAT_VARTABS |
| 374 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 375 | curbuf->b_p_vts_array); |
| 376 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 377 | tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 378 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 379 | if (todo >= tab_pad) |
| 380 | { |
| 381 | *s++ = TAB; |
| 382 | todo -= tab_pad; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 383 | #ifdef FEAT_VARTABS |
| 384 | ind_done += tab_pad; |
| 385 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | p = skipwhite(p); |
| 389 | } |
| 390 | |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 391 | #ifdef FEAT_VARTABS |
| 392 | for (;;) |
| 393 | { |
| 394 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 395 | curbuf->b_p_vts_array); |
| 396 | if (todo < tab_pad) |
| 397 | break; |
| 398 | *s++ = TAB; |
| 399 | todo -= tab_pad; |
| 400 | ind_done += tab_pad; |
| 401 | } |
| 402 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 403 | while (todo >= (int)curbuf->b_p_ts) |
| 404 | { |
| 405 | *s++ = TAB; |
| 406 | todo -= (int)curbuf->b_p_ts; |
| 407 | } |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 408 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 409 | } |
| 410 | while (todo > 0) |
| 411 | { |
| 412 | *s++ = ' '; |
| 413 | --todo; |
| 414 | } |
| 415 | mch_memmove(s, p, (size_t)line_len); |
| 416 | |
Bram Moolenaar | 663bc89 | 2019-01-08 23:07:24 +0100 | [diff] [blame] | 417 | // Replace the line (unless undo fails). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 418 | if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK) |
| 419 | { |
| 420 | ml_replace(curwin->w_cursor.lnum, newline, FALSE); |
| 421 | if (flags & SIN_CHANGED) |
| 422 | changed_bytes(curwin->w_cursor.lnum, 0); |
Bram Moolenaar | 663bc89 | 2019-01-08 23:07:24 +0100 | [diff] [blame] | 423 | |
| 424 | // Correct saved cursor position if it is in this line. |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 425 | if (saved_cursor.lnum == curwin->w_cursor.lnum) |
| 426 | { |
| 427 | if (saved_cursor.col >= (colnr_T)(p - oldline)) |
Bram Moolenaar | 663bc89 | 2019-01-08 23:07:24 +0100 | [diff] [blame] | 428 | // cursor was after the indent, adjust for the number of |
| 429 | // bytes added/removed |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 430 | saved_cursor.col += ind_len - (colnr_T)(p - oldline); |
| 431 | else if (saved_cursor.col >= (colnr_T)(s - newline)) |
Bram Moolenaar | 663bc89 | 2019-01-08 23:07:24 +0100 | [diff] [blame] | 432 | // cursor was in the indent, and is now after it, put it back |
| 433 | // at the start of the indent (replacing spaces with TAB) |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 434 | saved_cursor.col = (colnr_T)(s - newline); |
| 435 | } |
Bram Moolenaar | 663bc89 | 2019-01-08 23:07:24 +0100 | [diff] [blame] | 436 | #ifdef FEAT_TEXT_PROP |
| 437 | adjust_prop_columns(curwin->w_cursor.lnum, (colnr_T)(p - oldline), |
| 438 | ind_len - (colnr_T)(p - oldline)); |
| 439 | #endif |
Bram Moolenaar | 5409c05 | 2005-03-18 20:27:04 +0000 | [diff] [blame] | 440 | retval = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 441 | } |
| 442 | else |
| 443 | vim_free(newline); |
| 444 | |
| 445 | curwin->w_cursor.col = ind_len; |
Bram Moolenaar | 5409c05 | 2005-03-18 20:27:04 +0000 | [diff] [blame] | 446 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Copy the indent from ptr to the current line (and fill to size) |
| 451 | * Leaves the cursor on the first non-blank in the line. |
| 452 | * Returns TRUE if the line was changed. |
| 453 | */ |
| 454 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 455 | copy_indent(int size, char_u *src) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 456 | { |
| 457 | char_u *p = NULL; |
| 458 | char_u *line = NULL; |
| 459 | char_u *s; |
| 460 | int todo; |
| 461 | int ind_len; |
| 462 | int line_len = 0; |
| 463 | int tab_pad; |
| 464 | int ind_done; |
| 465 | int round; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 466 | #ifdef FEAT_VARTABS |
| 467 | int ind_col; |
| 468 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 469 | |
| 470 | /* Round 1: compute the number of characters needed for the indent |
| 471 | * Round 2: copy the characters. */ |
| 472 | for (round = 1; round <= 2; ++round) |
| 473 | { |
| 474 | todo = size; |
| 475 | ind_len = 0; |
| 476 | ind_done = 0; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 477 | #ifdef FEAT_VARTABS |
| 478 | ind_col = 0; |
| 479 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 480 | s = src; |
| 481 | |
| 482 | /* Count/copy the usable portion of the source line */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 483 | while (todo > 0 && VIM_ISWHITE(*s)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 484 | { |
| 485 | if (*s == TAB) |
| 486 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 487 | #ifdef FEAT_VARTABS |
| 488 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 489 | curbuf->b_p_vts_array); |
| 490 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 491 | tab_pad = (int)curbuf->b_p_ts |
| 492 | - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 493 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 494 | /* Stop if this tab will overshoot the target */ |
| 495 | if (todo < tab_pad) |
| 496 | break; |
| 497 | todo -= tab_pad; |
| 498 | ind_done += tab_pad; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 499 | #ifdef FEAT_VARTABS |
| 500 | ind_col += tab_pad; |
| 501 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 502 | } |
| 503 | else |
| 504 | { |
| 505 | --todo; |
| 506 | ++ind_done; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 507 | #ifdef FEAT_VARTABS |
| 508 | ++ind_col; |
| 509 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 510 | } |
| 511 | ++ind_len; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 512 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 513 | *p++ = *s; |
| 514 | ++s; |
| 515 | } |
| 516 | |
| 517 | /* Fill to next tabstop with a tab, if possible */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 518 | #ifdef FEAT_VARTABS |
| 519 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 520 | curbuf->b_p_vts_array); |
| 521 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 522 | tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 523 | #endif |
Bram Moolenaar | c42e7ed | 2011-09-07 19:58:09 +0200 | [diff] [blame] | 524 | if (todo >= tab_pad && !curbuf->b_p_et) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 525 | { |
| 526 | todo -= tab_pad; |
| 527 | ++ind_len; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 528 | #ifdef FEAT_VARTABS |
| 529 | ind_col += tab_pad; |
| 530 | #endif |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 531 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 532 | *p++ = TAB; |
| 533 | } |
| 534 | |
| 535 | /* Add tabs required for indent */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 536 | if (!curbuf->b_p_et) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 537 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 538 | #ifdef FEAT_VARTABS |
| 539 | for (;;) |
| 540 | { |
| 541 | tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts, |
| 542 | curbuf->b_p_vts_array); |
| 543 | if (todo < tab_pad) |
| 544 | break; |
| 545 | todo -= tab_pad; |
| 546 | ++ind_len; |
| 547 | ind_col += tab_pad; |
| 548 | if (p != NULL) |
| 549 | *p++ = TAB; |
| 550 | } |
| 551 | #else |
| 552 | while (todo >= (int)curbuf->b_p_ts) |
| 553 | { |
| 554 | todo -= (int)curbuf->b_p_ts; |
| 555 | ++ind_len; |
| 556 | if (p != NULL) |
| 557 | *p++ = TAB; |
| 558 | } |
| 559 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /* Count/add spaces required for indent */ |
| 563 | while (todo > 0) |
| 564 | { |
| 565 | --todo; |
| 566 | ++ind_len; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 567 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 568 | *p++ = ' '; |
| 569 | } |
| 570 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 571 | if (p == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 572 | { |
| 573 | /* Allocate memory for the result: the copied indent, new indent |
| 574 | * and the rest of the line. */ |
| 575 | line_len = (int)STRLEN(ml_get_curline()) + 1; |
| 576 | line = alloc(ind_len + line_len); |
| 577 | if (line == NULL) |
| 578 | return FALSE; |
| 579 | p = line; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /* Append the original line */ |
| 584 | mch_memmove(p, ml_get_curline(), (size_t)line_len); |
| 585 | |
| 586 | /* Replace the line */ |
| 587 | ml_replace(curwin->w_cursor.lnum, line, FALSE); |
| 588 | |
| 589 | /* Put the cursor after the indent. */ |
| 590 | curwin->w_cursor.col = ind_len; |
| 591 | return TRUE; |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Return the indent of the current line after a number. Return -1 if no |
| 596 | * number was found. Used for 'n' in 'formatoptions': numbered list. |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 597 | * Since a pattern is used it can actually handle more than numbers. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 598 | */ |
| 599 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 600 | get_number_indent(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 601 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 602 | colnr_T col; |
| 603 | pos_T pos; |
| 604 | |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 605 | regmatch_T regmatch; |
| 606 | int lead_len = 0; /* length of comment leader */ |
| 607 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 608 | if (lnum > curbuf->b_ml.ml_line_count) |
| 609 | return -1; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 610 | pos.lnum = 0; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 611 | |
| 612 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 613 | /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */ |
| 614 | if ((State & INSERT) || has_format_option(FO_Q_COMS)) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 615 | lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE); |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 616 | #endif |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 617 | regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC); |
| 618 | if (regmatch.regprog != NULL) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 619 | { |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 620 | regmatch.rm_ic = FALSE; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 621 | |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 622 | /* vim_regexec() expects a pointer to a line. This lets us |
| 623 | * start matching for the flp beyond any comment leader... */ |
| 624 | if (vim_regexec(®match, ml_get(lnum) + lead_len, (colnr_T)0)) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 625 | { |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 626 | pos.lnum = lnum; |
| 627 | pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum)); |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 628 | pos.coladd = 0; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 629 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 630 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 631 | } |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 632 | |
| 633 | if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 634 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 635 | getvcol(curwin, &pos, &col, NULL, NULL); |
| 636 | return (int)col; |
| 637 | } |
| 638 | |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 639 | #if defined(FEAT_LINEBREAK) || defined(PROTO) |
| 640 | /* |
| 641 | * Return appropriate space number for breakindent, taking influencing |
| 642 | * parameters into account. Window must be specified, since it is not |
| 643 | * necessarily always the current one. |
| 644 | */ |
| 645 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 646 | get_breakindent_win( |
| 647 | win_T *wp, |
| 648 | char_u *line) /* start of the line */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 649 | { |
| 650 | static int prev_indent = 0; /* cached indent value */ |
| 651 | static long prev_ts = 0L; /* cached tabstop value */ |
| 652 | static char_u *prev_line = NULL; /* cached pointer to line */ |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 653 | static varnumber_T prev_tick = 0; /* changedtick of cached value */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 654 | #ifdef FEAT_VARTABS |
| 655 | static int *prev_vts = NULL; /* cached vartabs values */ |
| 656 | #endif |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 657 | int bri = 0; |
| 658 | /* window width minus window margin space, i.e. what rests for text */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 659 | const int eff_wwidth = wp->w_width |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 660 | - ((wp->w_p_nu || wp->w_p_rnu) |
| 661 | && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL) |
| 662 | ? number_width(wp) + 1 : 0); |
| 663 | |
| 664 | /* used cached indent, unless pointer or 'tabstop' changed */ |
Bram Moolenaar | a40aa76 | 2014-06-25 22:55:38 +0200 | [diff] [blame] | 665 | if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 666 | || prev_tick != CHANGEDTICK(wp->w_buffer) |
| 667 | #ifdef FEAT_VARTABS |
| 668 | || prev_vts != wp->w_buffer->b_p_vts_array |
| 669 | #endif |
| 670 | ) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 671 | { |
| 672 | prev_line = line; |
| 673 | prev_ts = wp->w_buffer->b_p_ts; |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 674 | prev_tick = CHANGEDTICK(wp->w_buffer); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 675 | #ifdef FEAT_VARTABS |
| 676 | prev_vts = wp->w_buffer->b_p_vts_array; |
| 677 | prev_indent = get_indent_str_vtab(line, |
| 678 | (int)wp->w_buffer->b_p_ts, |
| 679 | wp->w_buffer->b_p_vts_array, wp->w_p_list); |
| 680 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 681 | prev_indent = get_indent_str(line, |
Bram Moolenaar | 9d7a592 | 2014-06-26 21:24:56 +0200 | [diff] [blame] | 682 | (int)wp->w_buffer->b_p_ts, wp->w_p_list); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 683 | #endif |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 684 | } |
Bram Moolenaar | 9d7a592 | 2014-06-26 21:24:56 +0200 | [diff] [blame] | 685 | bri = prev_indent + wp->w_p_brishift; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 686 | |
| 687 | /* indent minus the length of the showbreak string */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 688 | if (wp->w_p_brisbr) |
| 689 | bri -= vim_strsize(p_sbr); |
| 690 | |
| 691 | /* Add offset for number column, if 'n' is in 'cpoptions' */ |
| 692 | bri += win_col_off2(wp); |
| 693 | |
| 694 | /* never indent past left window margin */ |
| 695 | if (bri < 0) |
| 696 | bri = 0; |
| 697 | /* always leave at least bri_min characters on the left, |
| 698 | * if text width is sufficient */ |
| 699 | else if (bri > eff_wwidth - wp->w_p_brimin) |
| 700 | bri = (eff_wwidth - wp->w_p_brimin < 0) |
| 701 | ? 0 : eff_wwidth - wp->w_p_brimin; |
| 702 | |
| 703 | return bri; |
| 704 | } |
| 705 | #endif |
| 706 | |
| 707 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 708 | /* |
| 709 | * open_line: Add a new line below or above the current line. |
| 710 | * |
| 711 | * For VREPLACE mode, we only add a new line when we get to the end of the |
| 712 | * file, otherwise we just start replacing the next line. |
| 713 | * |
| 714 | * Caller must take care of undo. Since VREPLACE may affect any number of |
| 715 | * lines however, it may call u_save_cursor() again when starting to change a |
| 716 | * new line. |
| 717 | * "flags": OPENLINE_DELSPACES delete spaces after cursor |
| 718 | * OPENLINE_DO_COM format comments |
| 719 | * OPENLINE_KEEPTRAIL keep trailing spaces |
| 720 | * OPENLINE_MARKFIX adjust mark positions after the line break |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 721 | * OPENLINE_COM_LIST format comments with list or 2nd line indent |
| 722 | * |
| 723 | * "second_line_indent": indent for after ^^D in Insert mode or if flag |
| 724 | * OPENLINE_COM_LIST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 725 | * |
Bram Moolenaar | 24a2d72 | 2018-04-24 19:36:43 +0200 | [diff] [blame] | 726 | * Return OK for success, FAIL for failure |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 727 | */ |
| 728 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 729 | open_line( |
| 730 | int dir, /* FORWARD or BACKWARD */ |
| 731 | int flags, |
| 732 | int second_line_indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 733 | { |
| 734 | char_u *saved_line; /* copy of the original line */ |
| 735 | char_u *next_line = NULL; /* copy of the next line */ |
| 736 | char_u *p_extra = NULL; /* what goes to next line */ |
| 737 | int less_cols = 0; /* less columns for mark in new line */ |
| 738 | int less_cols_off = 0; /* columns to skip for mark adjust */ |
| 739 | pos_T old_cursor; /* old cursor position */ |
| 740 | int newcol = 0; /* new cursor column */ |
| 741 | int newindent = 0; /* auto-indent of the new line */ |
| 742 | int n; |
| 743 | int trunc_line = FALSE; /* truncate current line afterwards */ |
Bram Moolenaar | 24a2d72 | 2018-04-24 19:36:43 +0200 | [diff] [blame] | 744 | int retval = FAIL; /* return value */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 745 | #ifdef FEAT_COMMENTS |
| 746 | int extra_len = 0; /* length of p_extra string */ |
| 747 | int lead_len; /* length of comment leader */ |
| 748 | char_u *lead_flags; /* position in 'comments' for comment leader */ |
| 749 | char_u *leader = NULL; /* copy of comment leader */ |
| 750 | #endif |
| 751 | char_u *allocated = NULL; /* allocated memory */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 752 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 753 | int saved_char = NUL; /* init for GCC */ |
| 754 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS) |
| 755 | pos_T *pos; |
| 756 | #endif |
| 757 | #ifdef FEAT_SMARTINDENT |
| 758 | int do_si = (!p_paste && curbuf->b_p_si |
| 759 | # ifdef FEAT_CINDENT |
| 760 | && !curbuf->b_p_cin |
| 761 | # endif |
Bram Moolenaar | 69a76fe | 2017-08-03 17:54:03 +0200 | [diff] [blame] | 762 | # ifdef FEAT_EVAL |
| 763 | && *curbuf->b_p_inde == NUL |
| 764 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 765 | ); |
| 766 | int no_si = FALSE; /* reset did_si afterwards */ |
| 767 | int first_char = NUL; /* init for GCC */ |
| 768 | #endif |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 769 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 770 | int vreplace_mode; |
| 771 | #endif |
| 772 | int did_append; /* appended a new line */ |
| 773 | int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */ |
| 774 | |
| 775 | /* |
| 776 | * make a copy of the current line so we can mess with it |
| 777 | */ |
| 778 | saved_line = vim_strsave(ml_get_curline()); |
| 779 | if (saved_line == NULL) /* out of memory! */ |
| 780 | return FALSE; |
| 781 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 782 | if (State & VREPLACE_FLAG) |
| 783 | { |
| 784 | /* |
| 785 | * With VREPLACE we make a copy of the next line, which we will be |
| 786 | * starting to replace. First make the new line empty and let vim play |
| 787 | * with the indenting and comment leader to its heart's content. Then |
| 788 | * we grab what it ended up putting on the new line, put back the |
| 789 | * original line, and call ins_char() to put each new character onto |
| 790 | * the line, replacing what was there before and pushing the right |
| 791 | * stuff onto the replace stack. -- webb. |
| 792 | */ |
| 793 | if (curwin->w_cursor.lnum < orig_line_count) |
| 794 | next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1)); |
| 795 | else |
| 796 | next_line = vim_strsave((char_u *)""); |
| 797 | if (next_line == NULL) /* out of memory! */ |
| 798 | goto theend; |
| 799 | |
| 800 | /* |
| 801 | * In VREPLACE mode, a NL replaces the rest of the line, and starts |
| 802 | * replacing the next line, so push all of the characters left on the |
| 803 | * line onto the replace stack. We'll push any other characters that |
| 804 | * might be replaced at the start of the next line (due to autoindent |
| 805 | * etc) a bit later. |
| 806 | */ |
| 807 | replace_push(NUL); /* Call twice because BS over NL expects it */ |
| 808 | replace_push(NUL); |
| 809 | p = saved_line + curwin->w_cursor.col; |
| 810 | while (*p != NUL) |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 811 | { |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 812 | if (has_mbyte) |
| 813 | p += replace_push_mb(p); |
| 814 | else |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 815 | replace_push(*p++); |
| 816 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 817 | saved_line[curwin->w_cursor.col] = NUL; |
| 818 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 819 | |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 820 | if ((State & INSERT) && !(State & VREPLACE_FLAG)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 821 | { |
| 822 | p_extra = saved_line + curwin->w_cursor.col; |
| 823 | #ifdef FEAT_SMARTINDENT |
| 824 | if (do_si) /* need first char after new line break */ |
| 825 | { |
| 826 | p = skipwhite(p_extra); |
| 827 | first_char = *p; |
| 828 | } |
| 829 | #endif |
| 830 | #ifdef FEAT_COMMENTS |
| 831 | extra_len = (int)STRLEN(p_extra); |
| 832 | #endif |
| 833 | saved_char = *p_extra; |
| 834 | *p_extra = NUL; |
| 835 | } |
| 836 | |
| 837 | u_clearline(); /* cannot do "U" command when adding lines */ |
| 838 | #ifdef FEAT_SMARTINDENT |
| 839 | did_si = FALSE; |
| 840 | #endif |
| 841 | ai_col = 0; |
| 842 | |
| 843 | /* |
| 844 | * If we just did an auto-indent, then we didn't type anything on |
| 845 | * the prior line, and it should be truncated. Do this even if 'ai' is not |
| 846 | * set because automatically inserting a comment leader also sets did_ai. |
| 847 | */ |
| 848 | if (dir == FORWARD && did_ai) |
| 849 | trunc_line = TRUE; |
| 850 | |
| 851 | /* |
| 852 | * If 'autoindent' and/or 'smartindent' is set, try to figure out what |
| 853 | * indent to use for the new line. |
| 854 | */ |
| 855 | if (curbuf->b_p_ai |
| 856 | #ifdef FEAT_SMARTINDENT |
| 857 | || do_si |
| 858 | #endif |
| 859 | ) |
| 860 | { |
| 861 | /* |
| 862 | * count white space on current line |
| 863 | */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 864 | #ifdef FEAT_VARTABS |
| 865 | newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts, |
| 866 | curbuf->b_p_vts_array, FALSE); |
| 867 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 868 | newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 869 | #endif |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 870 | if (newindent == 0 && !(flags & OPENLINE_COM_LIST)) |
| 871 | newindent = second_line_indent; /* for ^^D command in insert mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 872 | |
| 873 | #ifdef FEAT_SMARTINDENT |
| 874 | /* |
| 875 | * Do smart indenting. |
| 876 | * In insert/replace mode (only when dir == FORWARD) |
| 877 | * we may move some text to the next line. If it starts with '{' |
| 878 | * don't add an indent. Fixes inserting a NL before '{' in line |
| 879 | * "if (condition) {" |
| 880 | */ |
| 881 | if (!trunc_line && do_si && *saved_line != NUL |
| 882 | && (p_extra == NULL || first_char != '{')) |
| 883 | { |
| 884 | char_u *ptr; |
| 885 | char_u last_char; |
| 886 | |
| 887 | old_cursor = curwin->w_cursor; |
| 888 | ptr = saved_line; |
| 889 | # ifdef FEAT_COMMENTS |
| 890 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 891 | lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 892 | else |
| 893 | lead_len = 0; |
| 894 | # endif |
| 895 | if (dir == FORWARD) |
| 896 | { |
| 897 | /* |
| 898 | * Skip preprocessor directives, unless they are |
| 899 | * recognised as comments. |
| 900 | */ |
| 901 | if ( |
| 902 | # ifdef FEAT_COMMENTS |
| 903 | lead_len == 0 && |
| 904 | # endif |
| 905 | ptr[0] == '#') |
| 906 | { |
| 907 | while (ptr[0] == '#' && curwin->w_cursor.lnum > 1) |
| 908 | ptr = ml_get(--curwin->w_cursor.lnum); |
| 909 | newindent = get_indent(); |
| 910 | } |
| 911 | # ifdef FEAT_COMMENTS |
| 912 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 913 | lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 914 | else |
| 915 | lead_len = 0; |
| 916 | if (lead_len > 0) |
| 917 | { |
| 918 | /* |
| 919 | * This case gets the following right: |
| 920 | * \* |
| 921 | * * A comment (read '\' as '/'). |
| 922 | * *\ |
| 923 | * #define IN_THE_WAY |
| 924 | * This should line up here; |
| 925 | */ |
| 926 | p = skipwhite(ptr); |
| 927 | if (p[0] == '/' && p[1] == '*') |
| 928 | p++; |
| 929 | if (p[0] == '*') |
| 930 | { |
| 931 | for (p++; *p; p++) |
| 932 | { |
| 933 | if (p[0] == '/' && p[-1] == '*') |
| 934 | { |
| 935 | /* |
| 936 | * End of C comment, indent should line up |
| 937 | * with the line containing the start of |
| 938 | * the comment |
| 939 | */ |
| 940 | curwin->w_cursor.col = (colnr_T)(p - ptr); |
| 941 | if ((pos = findmatch(NULL, NUL)) != NULL) |
| 942 | { |
| 943 | curwin->w_cursor.lnum = pos->lnum; |
| 944 | newindent = get_indent(); |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | else /* Not a comment line */ |
| 951 | # endif |
| 952 | { |
| 953 | /* Find last non-blank in line */ |
| 954 | p = ptr + STRLEN(ptr) - 1; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 955 | while (p > ptr && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 956 | --p; |
| 957 | last_char = *p; |
| 958 | |
| 959 | /* |
| 960 | * find the character just before the '{' or ';' |
| 961 | */ |
| 962 | if (last_char == '{' || last_char == ';') |
| 963 | { |
| 964 | if (p > ptr) |
| 965 | --p; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 966 | while (p > ptr && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 967 | --p; |
| 968 | } |
| 969 | /* |
| 970 | * Try to catch lines that are split over multiple |
| 971 | * lines. eg: |
| 972 | * if (condition && |
| 973 | * condition) { |
| 974 | * Should line up here! |
| 975 | * } |
| 976 | */ |
| 977 | if (*p == ')') |
| 978 | { |
| 979 | curwin->w_cursor.col = (colnr_T)(p - ptr); |
| 980 | if ((pos = findmatch(NULL, '(')) != NULL) |
| 981 | { |
| 982 | curwin->w_cursor.lnum = pos->lnum; |
| 983 | newindent = get_indent(); |
| 984 | ptr = ml_get_curline(); |
| 985 | } |
| 986 | } |
| 987 | /* |
| 988 | * If last character is '{' do indent, without |
| 989 | * checking for "if" and the like. |
| 990 | */ |
| 991 | if (last_char == '{') |
| 992 | { |
| 993 | did_si = TRUE; /* do indent */ |
| 994 | no_si = TRUE; /* don't delete it when '{' typed */ |
| 995 | } |
| 996 | /* |
| 997 | * Look for "if" and the like, use 'cinwords'. |
| 998 | * Don't do this if the previous line ended in ';' or |
| 999 | * '}'. |
| 1000 | */ |
| 1001 | else if (last_char != ';' && last_char != '}' |
| 1002 | && cin_is_cinword(ptr)) |
| 1003 | did_si = TRUE; |
| 1004 | } |
| 1005 | } |
| 1006 | else /* dir == BACKWARD */ |
| 1007 | { |
| 1008 | /* |
| 1009 | * Skip preprocessor directives, unless they are |
| 1010 | * recognised as comments. |
| 1011 | */ |
| 1012 | if ( |
| 1013 | # ifdef FEAT_COMMENTS |
| 1014 | lead_len == 0 && |
| 1015 | # endif |
| 1016 | ptr[0] == '#') |
| 1017 | { |
| 1018 | int was_backslashed = FALSE; |
| 1019 | |
| 1020 | while ((ptr[0] == '#' || was_backslashed) && |
| 1021 | curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 1022 | { |
| 1023 | if (*ptr && ptr[STRLEN(ptr) - 1] == '\\') |
| 1024 | was_backslashed = TRUE; |
| 1025 | else |
| 1026 | was_backslashed = FALSE; |
| 1027 | ptr = ml_get(++curwin->w_cursor.lnum); |
| 1028 | } |
| 1029 | if (was_backslashed) |
| 1030 | newindent = 0; /* Got to end of file */ |
| 1031 | else |
| 1032 | newindent = get_indent(); |
| 1033 | } |
| 1034 | p = skipwhite(ptr); |
| 1035 | if (*p == '}') /* if line starts with '}': do indent */ |
| 1036 | did_si = TRUE; |
| 1037 | else /* can delete indent when '{' typed */ |
| 1038 | can_si_back = TRUE; |
| 1039 | } |
| 1040 | curwin->w_cursor = old_cursor; |
| 1041 | } |
| 1042 | if (do_si) |
| 1043 | can_si = TRUE; |
| 1044 | #endif /* FEAT_SMARTINDENT */ |
| 1045 | |
| 1046 | did_ai = TRUE; |
| 1047 | } |
| 1048 | |
| 1049 | #ifdef FEAT_COMMENTS |
| 1050 | /* |
| 1051 | * Find out if the current line starts with a comment leader. |
| 1052 | * This may then be inserted in front of the new line. |
| 1053 | */ |
| 1054 | end_comment_pending = NUL; |
| 1055 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1056 | lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1057 | else |
| 1058 | lead_len = 0; |
| 1059 | if (lead_len > 0) |
| 1060 | { |
| 1061 | char_u *lead_repl = NULL; /* replaces comment leader */ |
| 1062 | int lead_repl_len = 0; /* length of *lead_repl */ |
| 1063 | char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ |
| 1064 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 1065 | char_u *comment_end = NULL; /* where lead_end has been found */ |
| 1066 | int extra_space = FALSE; /* append extra space */ |
| 1067 | int current_flag; |
| 1068 | int require_blank = FALSE; /* requires blank after middle */ |
| 1069 | char_u *p2; |
| 1070 | |
| 1071 | /* |
| 1072 | * If the comment leader has the start, middle or end flag, it may not |
| 1073 | * be used or may be replaced with the middle leader. |
| 1074 | */ |
| 1075 | for (p = lead_flags; *p && *p != ':'; ++p) |
| 1076 | { |
| 1077 | if (*p == COM_BLANK) |
| 1078 | { |
| 1079 | require_blank = TRUE; |
| 1080 | continue; |
| 1081 | } |
| 1082 | if (*p == COM_START || *p == COM_MIDDLE) |
| 1083 | { |
| 1084 | current_flag = *p; |
| 1085 | if (*p == COM_START) |
| 1086 | { |
| 1087 | /* |
| 1088 | * Doing "O" on a start of comment does not insert leader. |
| 1089 | */ |
| 1090 | if (dir == BACKWARD) |
| 1091 | { |
| 1092 | lead_len = 0; |
| 1093 | break; |
| 1094 | } |
| 1095 | |
| 1096 | /* find start of middle part */ |
| 1097 | (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); |
| 1098 | require_blank = FALSE; |
| 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | * Isolate the strings of the middle and end leader. |
| 1103 | */ |
| 1104 | while (*p && p[-1] != ':') /* find end of middle flags */ |
| 1105 | { |
| 1106 | if (*p == COM_BLANK) |
| 1107 | require_blank = TRUE; |
| 1108 | ++p; |
| 1109 | } |
| 1110 | (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); |
| 1111 | |
| 1112 | while (*p && p[-1] != ':') /* find end of end flags */ |
| 1113 | { |
| 1114 | /* Check whether we allow automatic ending of comments */ |
| 1115 | if (*p == COM_AUTO_END) |
| 1116 | end_comment_pending = -1; /* means we want to set it */ |
| 1117 | ++p; |
| 1118 | } |
| 1119 | n = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 1120 | |
| 1121 | if (end_comment_pending == -1) /* we can set it now */ |
| 1122 | end_comment_pending = lead_end[n - 1]; |
| 1123 | |
| 1124 | /* |
| 1125 | * If the end of the comment is in the same line, don't use |
| 1126 | * the comment leader. |
| 1127 | */ |
| 1128 | if (dir == FORWARD) |
| 1129 | { |
| 1130 | for (p = saved_line + lead_len; *p; ++p) |
| 1131 | if (STRNCMP(p, lead_end, n) == 0) |
| 1132 | { |
| 1133 | comment_end = p; |
| 1134 | lead_len = 0; |
| 1135 | break; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | /* |
| 1140 | * Doing "o" on a start of comment inserts the middle leader. |
| 1141 | */ |
| 1142 | if (lead_len > 0) |
| 1143 | { |
| 1144 | if (current_flag == COM_START) |
| 1145 | { |
| 1146 | lead_repl = lead_middle; |
| 1147 | lead_repl_len = (int)STRLEN(lead_middle); |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * If we have hit RETURN immediately after the start |
| 1152 | * comment leader, then put a space after the middle |
| 1153 | * comment leader on the next line. |
| 1154 | */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1155 | if (!VIM_ISWHITE(saved_line[lead_len - 1]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1156 | && ((p_extra != NULL |
| 1157 | && (int)curwin->w_cursor.col == lead_len) |
| 1158 | || (p_extra == NULL |
| 1159 | && saved_line[lead_len] == NUL) |
| 1160 | || require_blank)) |
| 1161 | extra_space = TRUE; |
| 1162 | } |
| 1163 | break; |
| 1164 | } |
| 1165 | if (*p == COM_END) |
| 1166 | { |
| 1167 | /* |
| 1168 | * Doing "o" on the end of a comment does not insert leader. |
| 1169 | * Remember where the end is, might want to use it to find the |
| 1170 | * start (for C-comments). |
| 1171 | */ |
| 1172 | if (dir == FORWARD) |
| 1173 | { |
| 1174 | comment_end = skipwhite(saved_line); |
| 1175 | lead_len = 0; |
| 1176 | break; |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * Doing "O" on the end of a comment inserts the middle leader. |
| 1181 | * Find the string for the middle leader, searching backwards. |
| 1182 | */ |
| 1183 | while (p > curbuf->b_p_com && *p != ',') |
| 1184 | --p; |
| 1185 | for (lead_repl = p; lead_repl > curbuf->b_p_com |
| 1186 | && lead_repl[-1] != ':'; --lead_repl) |
| 1187 | ; |
| 1188 | lead_repl_len = (int)(p - lead_repl); |
| 1189 | |
| 1190 | /* We can probably always add an extra space when doing "O" on |
| 1191 | * the comment-end */ |
| 1192 | extra_space = TRUE; |
| 1193 | |
| 1194 | /* Check whether we allow automatic ending of comments */ |
| 1195 | for (p2 = p; *p2 && *p2 != ':'; p2++) |
| 1196 | { |
| 1197 | if (*p2 == COM_AUTO_END) |
| 1198 | end_comment_pending = -1; /* means we want to set it */ |
| 1199 | } |
| 1200 | if (end_comment_pending == -1) |
| 1201 | { |
| 1202 | /* Find last character in end-comment string */ |
| 1203 | while (*p2 && *p2 != ',') |
| 1204 | p2++; |
| 1205 | end_comment_pending = p2[-1]; |
| 1206 | } |
| 1207 | break; |
| 1208 | } |
| 1209 | if (*p == COM_FIRST) |
| 1210 | { |
| 1211 | /* |
| 1212 | * Comment leader for first line only: Don't repeat leader |
| 1213 | * when using "O", blank out leader when using "o". |
| 1214 | */ |
| 1215 | if (dir == BACKWARD) |
| 1216 | lead_len = 0; |
| 1217 | else |
| 1218 | { |
| 1219 | lead_repl = (char_u *)""; |
| 1220 | lead_repl_len = 0; |
| 1221 | } |
| 1222 | break; |
| 1223 | } |
| 1224 | } |
| 1225 | if (lead_len) |
| 1226 | { |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 1227 | /* allocate buffer (may concatenate p_extra later) */ |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1228 | leader = alloc(lead_len + lead_repl_len + extra_space + extra_len |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 1229 | + (second_line_indent > 0 ? second_line_indent : 0) + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1230 | allocated = leader; /* remember to free it later */ |
| 1231 | |
| 1232 | if (leader == NULL) |
| 1233 | lead_len = 0; |
| 1234 | else |
| 1235 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 1236 | vim_strncpy(leader, saved_line, lead_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1237 | |
| 1238 | /* |
| 1239 | * Replace leader with lead_repl, right or left adjusted |
| 1240 | */ |
| 1241 | if (lead_repl != NULL) |
| 1242 | { |
| 1243 | int c = 0; |
| 1244 | int off = 0; |
| 1245 | |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1246 | for (p = lead_flags; *p != NUL && *p != ':'; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1247 | { |
| 1248 | if (*p == COM_RIGHT || *p == COM_LEFT) |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1249 | c = *p++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1250 | else if (VIM_ISDIGIT(*p) || *p == '-') |
| 1251 | off = getdigits(&p); |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1252 | else |
| 1253 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1254 | } |
| 1255 | if (c == COM_RIGHT) /* right adjusted leader */ |
| 1256 | { |
| 1257 | /* find last non-white in the leader to line up with */ |
| 1258 | for (p = leader + lead_len - 1; p > leader |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1259 | && VIM_ISWHITE(*p); --p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1260 | ; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1261 | ++p; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1262 | |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1263 | /* Compute the length of the replaced characters in |
| 1264 | * screen characters, not bytes. */ |
| 1265 | { |
| 1266 | int repl_size = vim_strnsize(lead_repl, |
| 1267 | lead_repl_len); |
| 1268 | int old_size = 0; |
| 1269 | char_u *endp = p; |
| 1270 | int l; |
| 1271 | |
| 1272 | while (old_size < repl_size && p > leader) |
| 1273 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 1274 | MB_PTR_BACK(leader, p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1275 | old_size += ptr2cells(p); |
| 1276 | } |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1277 | l = lead_repl_len - (int)(endp - p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1278 | if (l != 0) |
| 1279 | mch_memmove(endp + l, endp, |
| 1280 | (size_t)((leader + lead_len) - endp)); |
| 1281 | lead_len += l; |
| 1282 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1283 | mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
| 1284 | if (p + lead_repl_len > leader + lead_len) |
| 1285 | p[lead_repl_len] = NUL; |
| 1286 | |
| 1287 | /* blank-out any other chars from the old leader. */ |
| 1288 | while (--p >= leader) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1289 | { |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1290 | int l = mb_head_off(leader, p); |
| 1291 | |
| 1292 | if (l > 1) |
| 1293 | { |
| 1294 | p -= l; |
| 1295 | if (ptr2cells(p) > 1) |
| 1296 | { |
| 1297 | p[1] = ' '; |
| 1298 | --l; |
| 1299 | } |
| 1300 | mch_memmove(p + 1, p + l + 1, |
| 1301 | (size_t)((leader + lead_len) - (p + l + 1))); |
| 1302 | lead_len -= l; |
| 1303 | *p = ' '; |
| 1304 | } |
Bram Moolenaar | fc3abf4 | 2019-01-24 15:54:21 +0100 | [diff] [blame] | 1305 | else if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1306 | *p = ' '; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1307 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1308 | } |
| 1309 | else /* left adjusted leader */ |
| 1310 | { |
| 1311 | p = skipwhite(leader); |
Bram Moolenaar | fc3abf4 | 2019-01-24 15:54:21 +0100 | [diff] [blame] | 1312 | |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1313 | /* Compute the length of the replaced characters in |
| 1314 | * screen characters, not bytes. Move the part that is |
| 1315 | * not to be overwritten. */ |
| 1316 | { |
| 1317 | int repl_size = vim_strnsize(lead_repl, |
| 1318 | lead_repl_len); |
| 1319 | int i; |
| 1320 | int l; |
| 1321 | |
Bram Moolenaar | dc633cf | 2016-04-23 14:33:19 +0200 | [diff] [blame] | 1322 | for (i = 0; i < lead_len && p[i] != NUL; i += l) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1323 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1324 | l = (*mb_ptr2len)(p + i); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1325 | if (vim_strnsize(p, i + l) > repl_size) |
| 1326 | break; |
| 1327 | } |
| 1328 | if (i != lead_repl_len) |
| 1329 | { |
| 1330 | mch_memmove(p + lead_repl_len, p + i, |
Bram Moolenaar | 2d7ff05 | 2009-11-17 15:08:26 +0000 | [diff] [blame] | 1331 | (size_t)(lead_len - i - (p - leader))); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1332 | lead_len += lead_repl_len - i; |
| 1333 | } |
| 1334 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1335 | mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
| 1336 | |
| 1337 | /* Replace any remaining non-white chars in the old |
| 1338 | * leader by spaces. Keep Tabs, the indent must |
| 1339 | * remain the same. */ |
| 1340 | for (p += lead_repl_len; p < leader + lead_len; ++p) |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1341 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1342 | { |
| 1343 | /* Don't put a space before a TAB. */ |
| 1344 | if (p + 1 < leader + lead_len && p[1] == TAB) |
| 1345 | { |
| 1346 | --lead_len; |
| 1347 | mch_memmove(p, p + 1, |
| 1348 | (leader + lead_len) - p); |
| 1349 | } |
| 1350 | else |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1351 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1352 | int l = (*mb_ptr2len)(p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1353 | |
| 1354 | if (l > 1) |
| 1355 | { |
| 1356 | if (ptr2cells(p) > 1) |
| 1357 | { |
| 1358 | /* Replace a double-wide char with |
| 1359 | * two spaces */ |
| 1360 | --l; |
| 1361 | *p++ = ' '; |
| 1362 | } |
| 1363 | mch_memmove(p + 1, p + l, |
| 1364 | (leader + lead_len) - p); |
| 1365 | lead_len -= l - 1; |
| 1366 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1367 | *p = ' '; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1368 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1369 | } |
| 1370 | *p = NUL; |
| 1371 | } |
| 1372 | |
| 1373 | /* Recompute the indent, it may have changed. */ |
| 1374 | if (curbuf->b_p_ai |
| 1375 | #ifdef FEAT_SMARTINDENT |
| 1376 | || do_si |
| 1377 | #endif |
| 1378 | ) |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 1379 | #ifdef FEAT_VARTABS |
| 1380 | newindent = get_indent_str_vtab(leader, curbuf->b_p_ts, |
| 1381 | curbuf->b_p_vts_array, FALSE); |
| 1382 | #else |
| 1383 | newindent = get_indent_str(leader, |
| 1384 | (int)curbuf->b_p_ts, FALSE); |
| 1385 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1386 | |
| 1387 | /* Add the indent offset */ |
| 1388 | if (newindent + off < 0) |
| 1389 | { |
| 1390 | off = -newindent; |
| 1391 | newindent = 0; |
| 1392 | } |
| 1393 | else |
| 1394 | newindent += off; |
| 1395 | |
| 1396 | /* Correct trailing spaces for the shift, so that |
| 1397 | * alignment remains equal. */ |
| 1398 | while (off > 0 && lead_len > 0 |
| 1399 | && leader[lead_len - 1] == ' ') |
| 1400 | { |
| 1401 | /* Don't do it when there is a tab before the space */ |
| 1402 | if (vim_strchr(skipwhite(leader), '\t') != NULL) |
| 1403 | break; |
| 1404 | --lead_len; |
| 1405 | --off; |
| 1406 | } |
| 1407 | |
| 1408 | /* If the leader ends in white space, don't add an |
| 1409 | * extra space */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1410 | if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1411 | extra_space = FALSE; |
| 1412 | leader[lead_len] = NUL; |
| 1413 | } |
| 1414 | |
| 1415 | if (extra_space) |
| 1416 | { |
| 1417 | leader[lead_len++] = ' '; |
| 1418 | leader[lead_len] = NUL; |
| 1419 | } |
| 1420 | |
| 1421 | newcol = lead_len; |
| 1422 | |
| 1423 | /* |
| 1424 | * if a new indent will be set below, remove the indent that |
| 1425 | * is in the comment leader |
| 1426 | */ |
| 1427 | if (newindent |
| 1428 | #ifdef FEAT_SMARTINDENT |
| 1429 | || did_si |
| 1430 | #endif |
| 1431 | ) |
| 1432 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1433 | while (lead_len && VIM_ISWHITE(*leader)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1434 | { |
| 1435 | --lead_len; |
| 1436 | --newcol; |
| 1437 | ++leader; |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | } |
| 1442 | #ifdef FEAT_SMARTINDENT |
| 1443 | did_si = can_si = FALSE; |
| 1444 | #endif |
| 1445 | } |
| 1446 | else if (comment_end != NULL) |
| 1447 | { |
| 1448 | /* |
| 1449 | * We have finished a comment, so we don't use the leader. |
| 1450 | * If this was a C-comment and 'ai' or 'si' is set do a normal |
| 1451 | * indent to align with the line containing the start of the |
| 1452 | * comment. |
| 1453 | */ |
| 1454 | if (comment_end[0] == '*' && comment_end[1] == '/' && |
| 1455 | (curbuf->b_p_ai |
| 1456 | #ifdef FEAT_SMARTINDENT |
| 1457 | || do_si |
| 1458 | #endif |
| 1459 | )) |
| 1460 | { |
| 1461 | old_cursor = curwin->w_cursor; |
| 1462 | curwin->w_cursor.col = (colnr_T)(comment_end - saved_line); |
| 1463 | if ((pos = findmatch(NULL, NUL)) != NULL) |
| 1464 | { |
| 1465 | curwin->w_cursor.lnum = pos->lnum; |
| 1466 | newindent = get_indent(); |
| 1467 | } |
| 1468 | curwin->w_cursor = old_cursor; |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | #endif |
| 1473 | |
| 1474 | /* (State == INSERT || State == REPLACE), only when dir == FORWARD */ |
| 1475 | if (p_extra != NULL) |
| 1476 | { |
| 1477 | *p_extra = saved_char; /* restore char that NUL replaced */ |
| 1478 | |
| 1479 | /* |
| 1480 | * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first |
| 1481 | * non-blank. |
| 1482 | * |
| 1483 | * When in REPLACE mode, put the deleted blanks on the replace stack, |
| 1484 | * preceded by a NUL, so they can be put back when a BS is entered. |
| 1485 | */ |
| 1486 | if (REPLACE_NORMAL(State)) |
| 1487 | replace_push(NUL); /* end of extra blanks */ |
| 1488 | if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES)) |
| 1489 | { |
| 1490 | while ((*p_extra == ' ' || *p_extra == '\t') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1491 | && (!enc_utf8 |
Bram Moolenaar | fc3abf4 | 2019-01-24 15:54:21 +0100 | [diff] [blame] | 1492 | || !utf_iscomposing(utf_ptr2char(p_extra + 1)))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1493 | { |
| 1494 | if (REPLACE_NORMAL(State)) |
| 1495 | replace_push(*p_extra); |
| 1496 | ++p_extra; |
| 1497 | ++less_cols_off; |
| 1498 | } |
| 1499 | } |
| 1500 | if (*p_extra != NUL) |
| 1501 | did_ai = FALSE; /* append some text, don't truncate now */ |
| 1502 | |
| 1503 | /* columns for marks adjusted for removed columns */ |
| 1504 | less_cols = (int)(p_extra - saved_line); |
| 1505 | } |
| 1506 | |
| 1507 | if (p_extra == NULL) |
| 1508 | p_extra = (char_u *)""; /* append empty line */ |
| 1509 | |
| 1510 | #ifdef FEAT_COMMENTS |
| 1511 | /* concatenate leader and p_extra, if there is a leader */ |
| 1512 | if (lead_len) |
| 1513 | { |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1514 | if (flags & OPENLINE_COM_LIST && second_line_indent > 0) |
| 1515 | { |
| 1516 | int i; |
Bram Moolenaar | 3610578 | 2012-06-14 20:59:25 +0200 | [diff] [blame] | 1517 | int padding = second_line_indent |
| 1518 | - (newindent + (int)STRLEN(leader)); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1519 | |
| 1520 | /* Here whitespace is inserted after the comment char. |
| 1521 | * Below, set_indent(newindent, SIN_INSERT) will insert the |
| 1522 | * whitespace needed before the comment char. */ |
| 1523 | for (i = 0; i < padding; i++) |
| 1524 | { |
| 1525 | STRCAT(leader, " "); |
Bram Moolenaar | 5fb9ec5 | 2012-07-25 16:10:03 +0200 | [diff] [blame] | 1526 | less_cols--; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1527 | newcol++; |
| 1528 | } |
| 1529 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1530 | STRCAT(leader, p_extra); |
| 1531 | p_extra = leader; |
| 1532 | did_ai = TRUE; /* So truncating blanks works with comments */ |
| 1533 | less_cols -= lead_len; |
| 1534 | } |
| 1535 | else |
| 1536 | end_comment_pending = NUL; /* turns out there was no leader */ |
| 1537 | #endif |
| 1538 | |
| 1539 | old_cursor = curwin->w_cursor; |
| 1540 | if (dir == BACKWARD) |
| 1541 | --curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1542 | if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1543 | { |
| 1544 | if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE) |
| 1545 | == FAIL) |
| 1546 | goto theend; |
| 1547 | /* Postpone calling changed_lines(), because it would mess up folding |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 1548 | * with markers. |
| 1549 | * Skip mark_adjust when adding a line after the last one, there can't |
Bram Moolenaar | f58a847 | 2017-03-05 18:03:04 +0100 | [diff] [blame] | 1550 | * be marks there. But still needed in diff mode. */ |
| 1551 | if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count |
| 1552 | #ifdef FEAT_DIFF |
| 1553 | || curwin->w_p_diff |
| 1554 | #endif |
| 1555 | ) |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 1556 | mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1557 | did_append = TRUE; |
| 1558 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1559 | else |
| 1560 | { |
| 1561 | /* |
| 1562 | * In VREPLACE mode we are starting to replace the next line. |
| 1563 | */ |
| 1564 | curwin->w_cursor.lnum++; |
| 1565 | if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed) |
| 1566 | { |
| 1567 | /* In case we NL to a new line, BS to the previous one, and NL |
| 1568 | * again, we don't want to save the new line for undo twice. |
| 1569 | */ |
| 1570 | (void)u_save_cursor(); /* errors are ignored! */ |
| 1571 | vr_lines_changed++; |
| 1572 | } |
| 1573 | ml_replace(curwin->w_cursor.lnum, p_extra, TRUE); |
| 1574 | changed_bytes(curwin->w_cursor.lnum, 0); |
| 1575 | curwin->w_cursor.lnum--; |
| 1576 | did_append = FALSE; |
| 1577 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1578 | |
| 1579 | if (newindent |
| 1580 | #ifdef FEAT_SMARTINDENT |
| 1581 | || did_si |
| 1582 | #endif |
| 1583 | ) |
| 1584 | { |
| 1585 | ++curwin->w_cursor.lnum; |
| 1586 | #ifdef FEAT_SMARTINDENT |
| 1587 | if (did_si) |
| 1588 | { |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 1589 | int sw = (int)get_sw_value(curbuf); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 1590 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1591 | if (p_sr) |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 1592 | newindent -= newindent % sw; |
| 1593 | newindent += sw; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1594 | } |
| 1595 | #endif |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 1596 | /* Copy the indent */ |
| 1597 | if (curbuf->b_p_ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1598 | { |
| 1599 | (void)copy_indent(newindent, saved_line); |
| 1600 | |
| 1601 | /* |
| 1602 | * Set the 'preserveindent' option so that any further screwing |
| 1603 | * with the line doesn't entirely destroy our efforts to preserve |
| 1604 | * it. It gets restored at the function end. |
| 1605 | */ |
| 1606 | curbuf->b_p_pi = TRUE; |
| 1607 | } |
| 1608 | else |
| 1609 | (void)set_indent(newindent, SIN_INSERT); |
| 1610 | less_cols -= curwin->w_cursor.col; |
| 1611 | |
| 1612 | ai_col = curwin->w_cursor.col; |
| 1613 | |
| 1614 | /* |
| 1615 | * In REPLACE mode, for each character in the new indent, there must |
| 1616 | * be a NUL on the replace stack, for when it is deleted with BS |
| 1617 | */ |
| 1618 | if (REPLACE_NORMAL(State)) |
| 1619 | for (n = 0; n < (int)curwin->w_cursor.col; ++n) |
| 1620 | replace_push(NUL); |
| 1621 | newcol += curwin->w_cursor.col; |
| 1622 | #ifdef FEAT_SMARTINDENT |
| 1623 | if (no_si) |
| 1624 | did_si = FALSE; |
| 1625 | #endif |
| 1626 | } |
| 1627 | |
| 1628 | #ifdef FEAT_COMMENTS |
| 1629 | /* |
| 1630 | * In REPLACE mode, for each character in the extra leader, there must be |
| 1631 | * a NUL on the replace stack, for when it is deleted with BS. |
| 1632 | */ |
| 1633 | if (REPLACE_NORMAL(State)) |
| 1634 | while (lead_len-- > 0) |
| 1635 | replace_push(NUL); |
| 1636 | #endif |
| 1637 | |
| 1638 | curwin->w_cursor = old_cursor; |
| 1639 | |
| 1640 | if (dir == FORWARD) |
| 1641 | { |
| 1642 | if (trunc_line || (State & INSERT)) |
| 1643 | { |
| 1644 | /* truncate current line at cursor */ |
| 1645 | saved_line[curwin->w_cursor.col] = NUL; |
| 1646 | /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */ |
| 1647 | if (trunc_line && !(flags & OPENLINE_KEEPTRAIL)) |
| 1648 | truncate_spaces(saved_line); |
| 1649 | ml_replace(curwin->w_cursor.lnum, saved_line, FALSE); |
| 1650 | saved_line = NULL; |
| 1651 | if (did_append) |
| 1652 | { |
| 1653 | changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, |
| 1654 | curwin->w_cursor.lnum + 1, 1L); |
| 1655 | did_append = FALSE; |
| 1656 | |
| 1657 | /* Move marks after the line break to the new line. */ |
| 1658 | if (flags & OPENLINE_MARKFIX) |
| 1659 | mark_col_adjust(curwin->w_cursor.lnum, |
| 1660 | curwin->w_cursor.col + less_cols_off, |
Bram Moolenaar | e1e714e | 2018-12-31 23:58:24 +0100 | [diff] [blame] | 1661 | 1L, (long)-less_cols, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1662 | } |
| 1663 | else |
| 1664 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 1665 | } |
| 1666 | |
| 1667 | /* |
| 1668 | * Put the cursor on the new line. Careful: the scrollup() above may |
| 1669 | * have moved w_cursor, we must use old_cursor. |
| 1670 | */ |
| 1671 | curwin->w_cursor.lnum = old_cursor.lnum + 1; |
| 1672 | } |
| 1673 | if (did_append) |
| 1674 | changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L); |
| 1675 | |
| 1676 | curwin->w_cursor.col = newcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1677 | curwin->w_cursor.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1678 | |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 1679 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1680 | /* |
| 1681 | * In VREPLACE mode, we are handling the replace stack ourselves, so stop |
| 1682 | * fixthisline() from doing it (via change_indent()) by telling it we're in |
| 1683 | * normal INSERT mode. |
| 1684 | */ |
| 1685 | if (State & VREPLACE_FLAG) |
| 1686 | { |
| 1687 | vreplace_mode = State; /* So we know to put things right later */ |
| 1688 | State = INSERT; |
| 1689 | } |
| 1690 | else |
| 1691 | vreplace_mode = 0; |
| 1692 | #endif |
| 1693 | #ifdef FEAT_LISP |
| 1694 | /* |
| 1695 | * May do lisp indenting. |
| 1696 | */ |
| 1697 | if (!p_paste |
| 1698 | # ifdef FEAT_COMMENTS |
| 1699 | && leader == NULL |
| 1700 | # endif |
| 1701 | && curbuf->b_p_lisp |
| 1702 | && curbuf->b_p_ai) |
| 1703 | { |
| 1704 | fixthisline(get_lisp_indent); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1705 | ai_col = (colnr_T)getwhitecols_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1706 | } |
| 1707 | #endif |
| 1708 | #ifdef FEAT_CINDENT |
| 1709 | /* |
| 1710 | * May do indenting after opening a new line. |
| 1711 | */ |
| 1712 | if (!p_paste |
| 1713 | && (curbuf->b_p_cin |
| 1714 | # ifdef FEAT_EVAL |
| 1715 | || *curbuf->b_p_inde != NUL |
| 1716 | # endif |
| 1717 | ) |
| 1718 | && in_cinkeys(dir == FORWARD |
| 1719 | ? KEY_OPEN_FORW |
| 1720 | : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))) |
| 1721 | { |
| 1722 | do_c_expr_indent(); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1723 | ai_col = (colnr_T)getwhitecols_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1724 | } |
| 1725 | #endif |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 1726 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1727 | if (vreplace_mode != 0) |
| 1728 | State = vreplace_mode; |
| 1729 | #endif |
| 1730 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1731 | /* |
| 1732 | * Finally, VREPLACE gets the stuff on the new line, then puts back the |
| 1733 | * original line, and inserts the new stuff char by char, pushing old stuff |
| 1734 | * onto the replace stack (via ins_char()). |
| 1735 | */ |
| 1736 | if (State & VREPLACE_FLAG) |
| 1737 | { |
| 1738 | /* Put new line in p_extra */ |
| 1739 | p_extra = vim_strsave(ml_get_curline()); |
| 1740 | if (p_extra == NULL) |
| 1741 | goto theend; |
| 1742 | |
| 1743 | /* Put back original line */ |
| 1744 | ml_replace(curwin->w_cursor.lnum, next_line, FALSE); |
| 1745 | |
| 1746 | /* Insert new stuff into line again */ |
| 1747 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1748 | curwin->w_cursor.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1749 | ins_bytes(p_extra); /* will call changed_bytes() */ |
| 1750 | vim_free(p_extra); |
| 1751 | next_line = NULL; |
| 1752 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1753 | |
Bram Moolenaar | 24a2d72 | 2018-04-24 19:36:43 +0200 | [diff] [blame] | 1754 | retval = OK; /* success! */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1755 | theend: |
| 1756 | curbuf->b_p_pi = saved_pi; |
| 1757 | vim_free(saved_line); |
| 1758 | vim_free(next_line); |
| 1759 | vim_free(allocated); |
| 1760 | return retval; |
| 1761 | } |
| 1762 | |
| 1763 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 1764 | /* |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 1765 | * get_leader_len() returns the length in bytes of the prefix of the given |
| 1766 | * string which introduces a comment. If this string is not a comment then |
| 1767 | * 0 is returned. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1768 | * When "flags" is not NULL, it is set to point to the flags of the recognized |
| 1769 | * comment leader. |
| 1770 | * "backward" must be true for the "O" command. |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1771 | * If "include_space" is set, include trailing whitespace while calculating the |
| 1772 | * length. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1773 | */ |
| 1774 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1775 | get_leader_len( |
| 1776 | char_u *line, |
| 1777 | char_u **flags, |
| 1778 | int backward, |
| 1779 | int include_space) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1780 | { |
| 1781 | int i, j; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1782 | int result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1783 | int got_com = FALSE; |
| 1784 | int found_one; |
| 1785 | char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ |
| 1786 | char_u *string; /* pointer to comment string */ |
| 1787 | char_u *list; |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1788 | int middle_match_len = 0; |
| 1789 | char_u *prev_list; |
Bram Moolenaar | 05da428 | 2011-05-10 14:44:11 +0200 | [diff] [blame] | 1790 | char_u *saved_flags = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1791 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1792 | result = i = 0; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1793 | while (VIM_ISWHITE(line[i])) /* leading white space is ignored */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1794 | ++i; |
| 1795 | |
| 1796 | /* |
| 1797 | * Repeat to match several nested comment strings. |
| 1798 | */ |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1799 | while (line[i] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1800 | { |
| 1801 | /* |
| 1802 | * scan through the 'comments' option for a match |
| 1803 | */ |
| 1804 | found_one = FALSE; |
| 1805 | for (list = curbuf->b_p_com; *list; ) |
| 1806 | { |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1807 | /* Get one option part into part_buf[]. Advance "list" to next |
| 1808 | * one. Put "string" at start of string. */ |
| 1809 | if (!got_com && flags != NULL) |
| 1810 | *flags = list; /* remember where flags started */ |
| 1811 | prev_list = list; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1812 | (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
| 1813 | string = vim_strchr(part_buf, ':'); |
| 1814 | if (string == NULL) /* missing ':', ignore this part */ |
| 1815 | continue; |
| 1816 | *string++ = NUL; /* isolate flags from string */ |
| 1817 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1818 | /* If we found a middle match previously, use that match when this |
| 1819 | * is not a middle or end. */ |
| 1820 | if (middle_match_len != 0 |
| 1821 | && vim_strchr(part_buf, COM_MIDDLE) == NULL |
| 1822 | && vim_strchr(part_buf, COM_END) == NULL) |
| 1823 | break; |
| 1824 | |
| 1825 | /* When we already found a nested comment, only accept further |
| 1826 | * nested comments. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1827 | if (got_com && vim_strchr(part_buf, COM_NEST) == NULL) |
| 1828 | continue; |
| 1829 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1830 | /* When 'O' flag present and using "O" command skip this one. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1831 | if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL) |
| 1832 | continue; |
| 1833 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1834 | /* Line contents and string must match. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1835 | * When string starts with white space, must have some white space |
| 1836 | * (but the amount does not need to match, there might be a mix of |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1837 | * TABs and spaces). */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1838 | if (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1839 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1840 | if (i == 0 || !VIM_ISWHITE(line[i - 1])) |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 1841 | continue; /* missing white space */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1842 | while (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1843 | ++string; |
| 1844 | } |
| 1845 | for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) |
| 1846 | ; |
| 1847 | if (string[j] != NUL) |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1848 | continue; /* string doesn't match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1849 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1850 | /* When 'b' flag used, there must be white space or an |
| 1851 | * end-of-line after the string in the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1852 | if (vim_strchr(part_buf, COM_BLANK) != NULL |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1853 | && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1854 | continue; |
| 1855 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1856 | /* We have found a match, stop searching unless this is a middle |
| 1857 | * comment. The middle comment can be a substring of the end |
| 1858 | * comment in which case it's better to return the length of the |
| 1859 | * end comment and its flags. Thus we keep searching with middle |
| 1860 | * and end matches and use an end match if it matches better. */ |
| 1861 | if (vim_strchr(part_buf, COM_MIDDLE) != NULL) |
| 1862 | { |
| 1863 | if (middle_match_len == 0) |
| 1864 | { |
| 1865 | middle_match_len = j; |
| 1866 | saved_flags = prev_list; |
| 1867 | } |
| 1868 | continue; |
| 1869 | } |
| 1870 | if (middle_match_len != 0 && j > middle_match_len) |
| 1871 | /* Use this match instead of the middle match, since it's a |
| 1872 | * longer thus better match. */ |
| 1873 | middle_match_len = 0; |
| 1874 | |
| 1875 | if (middle_match_len == 0) |
| 1876 | i += j; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1877 | found_one = TRUE; |
| 1878 | break; |
| 1879 | } |
| 1880 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1881 | if (middle_match_len != 0) |
| 1882 | { |
| 1883 | /* Use the previously found middle match after failing to find a |
| 1884 | * match with an end. */ |
| 1885 | if (!got_com && flags != NULL) |
| 1886 | *flags = saved_flags; |
| 1887 | i += middle_match_len; |
| 1888 | found_one = TRUE; |
| 1889 | } |
| 1890 | |
| 1891 | /* No match found, stop scanning. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1892 | if (!found_one) |
| 1893 | break; |
| 1894 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1895 | result = i; |
| 1896 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1897 | /* Include any trailing white space. */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1898 | while (VIM_ISWHITE(line[i])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1899 | ++i; |
| 1900 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1901 | if (include_space) |
| 1902 | result = i; |
| 1903 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1904 | /* If this comment doesn't nest, stop here. */ |
| 1905 | got_com = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1906 | if (vim_strchr(part_buf, COM_NEST) == NULL) |
| 1907 | break; |
| 1908 | } |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1909 | return result; |
| 1910 | } |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1911 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1912 | /* |
| 1913 | * Return the offset at which the last comment in line starts. If there is no |
| 1914 | * comment in the whole line, -1 is returned. |
| 1915 | * |
| 1916 | * When "flags" is not null, it is set to point to the flags describing the |
| 1917 | * recognized comment leader. |
| 1918 | */ |
| 1919 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1920 | get_last_leader_offset(char_u *line, char_u **flags) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1921 | { |
| 1922 | int result = -1; |
| 1923 | int i, j; |
| 1924 | int lower_check_bound = 0; |
| 1925 | char_u *string; |
| 1926 | char_u *com_leader; |
| 1927 | char_u *com_flags; |
| 1928 | char_u *list; |
| 1929 | int found_one; |
| 1930 | char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ |
| 1931 | |
| 1932 | /* |
| 1933 | * Repeat to match several nested comment strings. |
| 1934 | */ |
| 1935 | i = (int)STRLEN(line); |
| 1936 | while (--i >= lower_check_bound) |
| 1937 | { |
| 1938 | /* |
| 1939 | * scan through the 'comments' option for a match |
| 1940 | */ |
| 1941 | found_one = FALSE; |
| 1942 | for (list = curbuf->b_p_com; *list; ) |
| 1943 | { |
| 1944 | char_u *flags_save = list; |
| 1945 | |
| 1946 | /* |
| 1947 | * Get one option part into part_buf[]. Advance list to next one. |
| 1948 | * put string at start of string. |
| 1949 | */ |
| 1950 | (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
| 1951 | string = vim_strchr(part_buf, ':'); |
| 1952 | if (string == NULL) /* If everything is fine, this cannot actually |
| 1953 | * happen. */ |
| 1954 | { |
| 1955 | continue; |
| 1956 | } |
| 1957 | *string++ = NUL; /* Isolate flags from string. */ |
| 1958 | com_leader = string; |
| 1959 | |
| 1960 | /* |
| 1961 | * Line contents and string must match. |
| 1962 | * When string starts with white space, must have some white space |
| 1963 | * (but the amount does not need to match, there might be a mix of |
| 1964 | * TABs and spaces). |
| 1965 | */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1966 | if (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1967 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1968 | if (i == 0 || !VIM_ISWHITE(line[i - 1])) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1969 | continue; |
Bram Moolenaar | 5393281 | 2018-12-07 21:08:49 +0100 | [diff] [blame] | 1970 | while (VIM_ISWHITE(*string)) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1971 | ++string; |
| 1972 | } |
| 1973 | for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) |
| 1974 | /* do nothing */; |
| 1975 | if (string[j] != NUL) |
| 1976 | continue; |
| 1977 | |
| 1978 | /* |
| 1979 | * When 'b' flag used, there must be white space or an |
| 1980 | * end-of-line after the string in the line. |
| 1981 | */ |
| 1982 | if (vim_strchr(part_buf, COM_BLANK) != NULL |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1983 | && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1984 | continue; |
Bram Moolenaar | 5393281 | 2018-12-07 21:08:49 +0100 | [diff] [blame] | 1985 | |
Bram Moolenaar | 4af7259 | 2018-12-09 15:00:52 +0100 | [diff] [blame] | 1986 | if (vim_strchr(part_buf, COM_MIDDLE) != NULL) |
Bram Moolenaar | 5393281 | 2018-12-07 21:08:49 +0100 | [diff] [blame] | 1987 | { |
Bram Moolenaar | 4af7259 | 2018-12-09 15:00:52 +0100 | [diff] [blame] | 1988 | // For a middlepart comment, only consider it to match if |
| 1989 | // everything before the current position in the line is |
| 1990 | // whitespace. Otherwise we would think we are inside a |
| 1991 | // comment if the middle part appears somewhere in the middle |
| 1992 | // of the line. E.g. for C the "*" appears often. |
Bram Moolenaar | 5393281 | 2018-12-07 21:08:49 +0100 | [diff] [blame] | 1993 | for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++) |
| 1994 | ; |
| 1995 | if (j < i) |
| 1996 | continue; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | /* |
| 2000 | * We have found a match, stop searching. |
| 2001 | */ |
| 2002 | found_one = TRUE; |
| 2003 | |
| 2004 | if (flags) |
| 2005 | *flags = flags_save; |
| 2006 | com_flags = flags_save; |
| 2007 | |
| 2008 | break; |
| 2009 | } |
| 2010 | |
| 2011 | if (found_one) |
| 2012 | { |
| 2013 | char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */ |
| 2014 | int len1, len2, off; |
| 2015 | |
| 2016 | result = i; |
| 2017 | /* |
| 2018 | * If this comment nests, continue searching. |
| 2019 | */ |
| 2020 | if (vim_strchr(part_buf, COM_NEST) != NULL) |
| 2021 | continue; |
| 2022 | |
| 2023 | lower_check_bound = i; |
| 2024 | |
| 2025 | /* Let's verify whether the comment leader found is a substring |
| 2026 | * of other comment leaders. If it is, let's adjust the |
| 2027 | * lower_check_bound so that we make sure that we have determined |
| 2028 | * the comment leader correctly. |
| 2029 | */ |
| 2030 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2031 | while (VIM_ISWHITE(*com_leader)) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2032 | ++com_leader; |
| 2033 | len1 = (int)STRLEN(com_leader); |
| 2034 | |
| 2035 | for (list = curbuf->b_p_com; *list; ) |
| 2036 | { |
| 2037 | char_u *flags_save = list; |
| 2038 | |
| 2039 | (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ","); |
| 2040 | if (flags_save == com_flags) |
| 2041 | continue; |
| 2042 | string = vim_strchr(part_buf2, ':'); |
| 2043 | ++string; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2044 | while (VIM_ISWHITE(*string)) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2045 | ++string; |
| 2046 | len2 = (int)STRLEN(string); |
| 2047 | if (len2 == 0) |
| 2048 | continue; |
| 2049 | |
| 2050 | /* Now we have to verify whether string ends with a substring |
| 2051 | * beginning the com_leader. */ |
| 2052 | for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;) |
| 2053 | { |
| 2054 | --off; |
| 2055 | if (!STRNCMP(string + off, com_leader, len2 - off)) |
| 2056 | { |
| 2057 | if (i - off < lower_check_bound) |
| 2058 | lower_check_bound = i - off; |
| 2059 | } |
| 2060 | } |
| 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | return result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2065 | } |
| 2066 | #endif |
| 2067 | |
| 2068 | /* |
| 2069 | * Return the number of window lines occupied by buffer line "lnum". |
| 2070 | */ |
| 2071 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2072 | plines(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2073 | { |
| 2074 | return plines_win(curwin, lnum, TRUE); |
| 2075 | } |
| 2076 | |
| 2077 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2078 | plines_win( |
| 2079 | win_T *wp, |
| 2080 | linenr_T lnum, |
| 2081 | int winheight) /* when TRUE limit to window height */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2082 | { |
| 2083 | #if defined(FEAT_DIFF) || defined(PROTO) |
| 2084 | /* Check for filler lines above this buffer line. When folded the result |
| 2085 | * is one line anyway. */ |
| 2086 | return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum); |
| 2087 | } |
| 2088 | |
| 2089 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2090 | plines_nofill(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2091 | { |
| 2092 | return plines_win_nofill(curwin, lnum, TRUE); |
| 2093 | } |
| 2094 | |
| 2095 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2096 | plines_win_nofill( |
| 2097 | win_T *wp, |
| 2098 | linenr_T lnum, |
| 2099 | int winheight) /* when TRUE limit to window height */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2100 | { |
| 2101 | #endif |
| 2102 | int lines; |
| 2103 | |
| 2104 | if (!wp->w_p_wrap) |
| 2105 | return 1; |
| 2106 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2107 | if (wp->w_width == 0) |
| 2108 | return 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2109 | |
| 2110 | #ifdef FEAT_FOLDING |
| 2111 | /* A folded lines is handled just like an empty line. */ |
| 2112 | /* NOTE: Caller must handle lines that are MAYBE folded. */ |
| 2113 | if (lineFolded(wp, lnum) == TRUE) |
| 2114 | return 1; |
| 2115 | #endif |
| 2116 | |
| 2117 | lines = plines_win_nofold(wp, lnum); |
| 2118 | if (winheight > 0 && lines > wp->w_height) |
| 2119 | return (int)wp->w_height; |
| 2120 | return lines; |
| 2121 | } |
| 2122 | |
| 2123 | /* |
| 2124 | * Return number of window lines physical line "lnum" will occupy in window |
| 2125 | * "wp". Does not care about folding, 'wrap' or 'diff'. |
| 2126 | */ |
| 2127 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2128 | plines_win_nofold(win_T *wp, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2129 | { |
| 2130 | char_u *s; |
| 2131 | long col; |
| 2132 | int width; |
| 2133 | |
| 2134 | s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
| 2135 | if (*s == NUL) /* empty line */ |
| 2136 | return 1; |
| 2137 | col = win_linetabsize(wp, s, (colnr_T)MAXCOL); |
| 2138 | |
| 2139 | /* |
| 2140 | * If list mode is on, then the '$' at the end of the line may take up one |
| 2141 | * extra column. |
| 2142 | */ |
| 2143 | if (wp->w_p_list && lcs_eol != NUL) |
| 2144 | col += 1; |
| 2145 | |
| 2146 | /* |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 2147 | * Add column offset for 'number', 'relativenumber' and 'foldcolumn'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2148 | */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2149 | width = wp->w_width - win_col_off(wp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2150 | if (width <= 0) |
| 2151 | return 32000; |
| 2152 | if (col <= width) |
| 2153 | return 1; |
| 2154 | col -= width; |
| 2155 | width += win_col_off2(wp); |
| 2156 | return (col + (width - 1)) / width + 1; |
| 2157 | } |
| 2158 | |
| 2159 | /* |
| 2160 | * Like plines_win(), but only reports the number of physical screen lines |
| 2161 | * used from the start of the line to the given column number. |
| 2162 | */ |
| 2163 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2164 | plines_win_col(win_T *wp, linenr_T lnum, long column) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2165 | { |
| 2166 | long col; |
| 2167 | char_u *s; |
| 2168 | int lines = 0; |
| 2169 | int width; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2170 | char_u *line; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2171 | |
| 2172 | #ifdef FEAT_DIFF |
| 2173 | /* Check for filler lines above this buffer line. When folded the result |
| 2174 | * is one line anyway. */ |
| 2175 | lines = diff_check_fill(wp, lnum); |
| 2176 | #endif |
| 2177 | |
| 2178 | if (!wp->w_p_wrap) |
| 2179 | return lines + 1; |
| 2180 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2181 | if (wp->w_width == 0) |
| 2182 | return lines + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2183 | |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2184 | line = s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2185 | |
| 2186 | col = 0; |
| 2187 | while (*s != NUL && --column >= 0) |
| 2188 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2189 | col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL); |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 2190 | MB_PTR_ADV(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | /* |
| 2194 | * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in |
| 2195 | * INSERT mode, then col must be adjusted so that it represents the last |
| 2196 | * screen position of the TAB. This only fixes an error when the TAB wraps |
| 2197 | * from one screen line to the next (when 'columns' is not a multiple of |
| 2198 | * 'ts') -- webb. |
| 2199 | */ |
| 2200 | if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1)) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2201 | col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2202 | |
| 2203 | /* |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 2204 | * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2205 | */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2206 | width = wp->w_width - win_col_off(wp); |
Bram Moolenaar | 2647063 | 2006-10-24 19:12:40 +0000 | [diff] [blame] | 2207 | if (width <= 0) |
| 2208 | return 9999; |
| 2209 | |
| 2210 | lines += 1; |
| 2211 | if (col > width) |
| 2212 | lines += (col - width) / (width + win_col_off2(wp)) + 1; |
| 2213 | return lines; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2217 | plines_m_win(win_T *wp, linenr_T first, linenr_T last) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2218 | { |
| 2219 | int count = 0; |
| 2220 | |
| 2221 | while (first <= last) |
| 2222 | { |
| 2223 | #ifdef FEAT_FOLDING |
| 2224 | int x; |
| 2225 | |
| 2226 | /* Check if there are any really folded lines, but also included lines |
| 2227 | * that are maybe folded. */ |
| 2228 | x = foldedCount(wp, first, NULL); |
| 2229 | if (x > 0) |
| 2230 | { |
| 2231 | ++count; /* count 1 for "+-- folded" line */ |
| 2232 | first += x; |
| 2233 | } |
| 2234 | else |
| 2235 | #endif |
| 2236 | { |
| 2237 | #ifdef FEAT_DIFF |
| 2238 | if (first == wp->w_topline) |
| 2239 | count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill; |
| 2240 | else |
| 2241 | #endif |
| 2242 | count += plines_win(wp, first, TRUE); |
| 2243 | ++first; |
| 2244 | } |
| 2245 | } |
| 2246 | return (count); |
| 2247 | } |
| 2248 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2249 | /* |
| 2250 | * Insert string "p" at the cursor position. Stops at a NUL byte. |
| 2251 | * Handles Replace mode and multi-byte characters. |
| 2252 | */ |
| 2253 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2254 | ins_bytes(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2255 | { |
| 2256 | ins_bytes_len(p, (int)STRLEN(p)); |
| 2257 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2258 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2259 | /* |
| 2260 | * Insert string "p" with length "len" at the cursor position. |
| 2261 | * Handles Replace mode and multi-byte characters. |
| 2262 | */ |
| 2263 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2264 | ins_bytes_len(char_u *p, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2265 | { |
| 2266 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2267 | int n; |
| 2268 | |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2269 | if (has_mbyte) |
| 2270 | for (i = 0; i < len; i += n) |
| 2271 | { |
| 2272 | if (enc_utf8) |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2273 | // avoid reading past p[len] |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2274 | n = utfc_ptr2len_len(p + i, len - i); |
| 2275 | else |
| 2276 | n = (*mb_ptr2len)(p + i); |
| 2277 | ins_char_bytes(p + i, n); |
| 2278 | } |
| 2279 | else |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2280 | for (i = 0; i < len; ++i) |
| 2281 | ins_char(p[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2282 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2283 | |
| 2284 | /* |
| 2285 | * Insert or replace a single character at the cursor position. |
| 2286 | * When in REPLACE or VREPLACE mode, replace any existing character. |
| 2287 | * Caller must have prepared for undo. |
| 2288 | * For multi-byte characters we get the whole character, the caller must |
| 2289 | * convert bytes to a character. |
| 2290 | */ |
| 2291 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2292 | ins_char(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2293 | { |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 2294 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | fc3abf4 | 2019-01-24 15:54:21 +0100 | [diff] [blame] | 2295 | int n = (*mb_char2bytes)(c, buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2296 | |
| 2297 | /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte. |
| 2298 | * Happens for CTRL-Vu9900. */ |
| 2299 | if (buf[0] == 0) |
| 2300 | buf[0] = '\n'; |
| 2301 | |
| 2302 | ins_char_bytes(buf, n); |
| 2303 | } |
| 2304 | |
| 2305 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2306 | ins_char_bytes(char_u *buf, int charlen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2307 | { |
| 2308 | int c = buf[0]; |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2309 | int newlen; // nr of bytes inserted |
| 2310 | int oldlen; // nr of bytes deleted (0 when not replacing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2311 | char_u *p; |
| 2312 | char_u *newp; |
| 2313 | char_u *oldp; |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2314 | int linelen; // length of old line including NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2315 | colnr_T col; |
| 2316 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2317 | int i; |
| 2318 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2319 | /* Break tabs if needed. */ |
| 2320 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 2321 | coladvance_force(getviscol()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2322 | |
| 2323 | col = curwin->w_cursor.col; |
| 2324 | oldp = ml_get(lnum); |
| 2325 | linelen = (int)STRLEN(oldp) + 1; |
| 2326 | |
| 2327 | /* The lengths default to the values for when not replacing. */ |
| 2328 | oldlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2329 | newlen = charlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2330 | |
| 2331 | if (State & REPLACE_FLAG) |
| 2332 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2333 | if (State & VREPLACE_FLAG) |
| 2334 | { |
| 2335 | colnr_T new_vcol = 0; /* init for GCC */ |
| 2336 | colnr_T vcol; |
| 2337 | int old_list; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2338 | |
| 2339 | /* |
| 2340 | * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag. |
| 2341 | * Returns the old value of list, so when finished, |
| 2342 | * curwin->w_p_list should be set back to this. |
| 2343 | */ |
| 2344 | old_list = curwin->w_p_list; |
| 2345 | if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 2346 | curwin->w_p_list = FALSE; |
| 2347 | |
| 2348 | /* |
| 2349 | * In virtual replace mode each character may replace one or more |
| 2350 | * characters (zero if it's a TAB). Count the number of bytes to |
| 2351 | * be deleted to make room for the new character, counting screen |
| 2352 | * cells. May result in adding spaces to fill a gap. |
| 2353 | */ |
| 2354 | getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2355 | new_vcol = vcol + chartabsize(buf, vcol); |
| 2356 | while (oldp[col + oldlen] != NUL && vcol < new_vcol) |
| 2357 | { |
| 2358 | vcol += chartabsize(oldp + col + oldlen, vcol); |
| 2359 | /* Don't need to remove a TAB that takes us to the right |
| 2360 | * position. */ |
| 2361 | if (vcol > new_vcol && oldp[col + oldlen] == TAB) |
| 2362 | break; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2363 | oldlen += (*mb_ptr2len)(oldp + col + oldlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2364 | /* Deleted a bit too much, insert spaces. */ |
| 2365 | if (vcol > new_vcol) |
| 2366 | newlen += vcol - new_vcol; |
| 2367 | } |
| 2368 | curwin->w_p_list = old_list; |
| 2369 | } |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2370 | else if (oldp[col] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2371 | { |
| 2372 | /* normal replace */ |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2373 | oldlen = (*mb_ptr2len)(oldp + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | |
| 2377 | /* Push the replaced bytes onto the replace stack, so that they can be |
| 2378 | * put back when BS is used. The bytes of a multi-byte character are |
| 2379 | * done the other way around, so that the first byte is popped off |
| 2380 | * first (it tells the byte length of the character). */ |
| 2381 | replace_push(NUL); |
| 2382 | for (i = 0; i < oldlen; ++i) |
| 2383 | { |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 2384 | if (has_mbyte) |
| 2385 | i += replace_push_mb(oldp + col + i) - 1; |
| 2386 | else |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 2387 | replace_push(oldp[col + i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | newp = alloc_check((unsigned)(linelen + newlen - oldlen)); |
| 2392 | if (newp == NULL) |
| 2393 | return; |
| 2394 | |
| 2395 | /* Copy bytes before the cursor. */ |
| 2396 | if (col > 0) |
| 2397 | mch_memmove(newp, oldp, (size_t)col); |
| 2398 | |
| 2399 | /* Copy bytes after the changed character(s). */ |
| 2400 | p = newp + col; |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 2401 | if (linelen > col + oldlen) |
| 2402 | mch_memmove(p + newlen, oldp + col + oldlen, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2403 | (size_t)(linelen - col - oldlen)); |
| 2404 | |
| 2405 | /* Insert or overwrite the new character. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2406 | mch_memmove(p, buf, charlen); |
| 2407 | i = charlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2408 | |
| 2409 | /* Fill with spaces when necessary. */ |
| 2410 | while (i < newlen) |
| 2411 | p[i++] = ' '; |
| 2412 | |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2413 | // Replace the line in the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2414 | ml_replace(lnum, newp, FALSE); |
| 2415 | |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2416 | // mark the buffer as changed and prepare for displaying |
| 2417 | inserted_bytes(lnum, col, newlen - oldlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2418 | |
| 2419 | /* |
| 2420 | * If we're in Insert or Replace mode and 'showmatch' is set, then briefly |
| 2421 | * show the match for right parens and braces. |
| 2422 | */ |
| 2423 | if (p_sm && (State & INSERT) |
| 2424 | && msg_silent == 0 |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 2425 | #ifdef FEAT_INS_EXPAND |
| 2426 | && !ins_compl_active() |
| 2427 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2428 | ) |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2429 | { |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2430 | if (has_mbyte) |
| 2431 | showmatch(mb_ptr2char(buf)); |
| 2432 | else |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2433 | showmatch(c); |
| 2434 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2435 | |
| 2436 | #ifdef FEAT_RIGHTLEFT |
| 2437 | if (!p_ri || (State & REPLACE_FLAG)) |
| 2438 | #endif |
| 2439 | { |
| 2440 | /* Normal insert: move cursor right */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2441 | curwin->w_cursor.col += charlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2442 | } |
| 2443 | /* |
| 2444 | * TODO: should try to update w_row here, to avoid recomputing it later. |
| 2445 | */ |
| 2446 | } |
| 2447 | |
| 2448 | /* |
| 2449 | * Insert a string at the cursor position. |
| 2450 | * Note: Does NOT handle Replace mode. |
| 2451 | * Caller must have prepared for undo. |
| 2452 | */ |
| 2453 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2454 | ins_str(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2455 | { |
| 2456 | char_u *oldp, *newp; |
| 2457 | int newlen = (int)STRLEN(s); |
| 2458 | int oldlen; |
| 2459 | colnr_T col; |
| 2460 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2461 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2462 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 2463 | coladvance_force(getviscol()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2464 | |
| 2465 | col = curwin->w_cursor.col; |
| 2466 | oldp = ml_get(lnum); |
| 2467 | oldlen = (int)STRLEN(oldp); |
| 2468 | |
| 2469 | newp = alloc_check((unsigned)(oldlen + newlen + 1)); |
| 2470 | if (newp == NULL) |
| 2471 | return; |
| 2472 | if (col > 0) |
| 2473 | mch_memmove(newp, oldp, (size_t)col); |
| 2474 | mch_memmove(newp + col, s, (size_t)newlen); |
| 2475 | mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1)); |
| 2476 | ml_replace(lnum, newp, FALSE); |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2477 | inserted_bytes(lnum, col, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2478 | curwin->w_cursor.col += newlen; |
| 2479 | } |
| 2480 | |
| 2481 | /* |
| 2482 | * Delete one character under the cursor. |
| 2483 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 2484 | * Caller must have prepared for undo. |
| 2485 | * |
| 2486 | * return FAIL for failure, OK otherwise |
| 2487 | */ |
| 2488 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2489 | del_char(int fixpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2490 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2491 | if (has_mbyte) |
| 2492 | { |
| 2493 | /* Make sure the cursor is at the start of a character. */ |
| 2494 | mb_adjust_cursor(); |
| 2495 | if (*ml_get_cursor() == NUL) |
| 2496 | return FAIL; |
| 2497 | return del_chars(1L, fixpos); |
| 2498 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2499 | return del_bytes(1L, fixpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2502 | /* |
| 2503 | * Like del_bytes(), but delete characters instead of bytes. |
| 2504 | */ |
| 2505 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2506 | del_chars(long count, int fixpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2507 | { |
| 2508 | long bytes = 0; |
| 2509 | long i; |
| 2510 | char_u *p; |
| 2511 | int l; |
| 2512 | |
| 2513 | p = ml_get_cursor(); |
| 2514 | for (i = 0; i < count && *p != NUL; ++i) |
| 2515 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2516 | l = (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2517 | bytes += l; |
| 2518 | p += l; |
| 2519 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2520 | return del_bytes(bytes, fixpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2521 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2522 | |
| 2523 | /* |
| 2524 | * Delete "count" bytes under the cursor. |
| 2525 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 2526 | * Caller must have prepared for undo. |
| 2527 | * |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2528 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2529 | */ |
| 2530 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2531 | del_bytes( |
| 2532 | long count, |
| 2533 | int fixpos_arg, |
| 2534 | int use_delcombine UNUSED) /* 'delcombine' option applies */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2535 | { |
| 2536 | char_u *oldp, *newp; |
| 2537 | colnr_T oldlen; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2538 | colnr_T newlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2539 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2540 | colnr_T col = curwin->w_cursor.col; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2541 | int alloc_newp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2542 | long movelen; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2543 | int fixpos = fixpos_arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2544 | |
| 2545 | oldp = ml_get(lnum); |
| 2546 | oldlen = (int)STRLEN(oldp); |
| 2547 | |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2548 | /* Can't do anything when the cursor is on the NUL after the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2549 | if (col >= oldlen) |
| 2550 | return FAIL; |
| 2551 | |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2552 | /* If "count" is zero there is nothing to do. */ |
| 2553 | if (count == 0) |
| 2554 | return OK; |
| 2555 | |
| 2556 | /* If "count" is negative the caller must be doing something wrong. */ |
| 2557 | if (count < 1) |
| 2558 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2559 | siemsg("E950: Invalid count for del_bytes(): %ld", count); |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2560 | return FAIL; |
| 2561 | } |
| 2562 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2563 | /* If 'delcombine' is set and deleting (less than) one character, only |
| 2564 | * delete the last combining character. */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2565 | if (p_deco && use_delcombine && enc_utf8 |
| 2566 | && utfc_ptr2len(oldp + col) >= count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2567 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2568 | int cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2569 | int n; |
| 2570 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2571 | (void)utfc_ptr2char(oldp + col, cc); |
| 2572 | if (cc[0] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2573 | { |
| 2574 | /* Find the last composing char, there can be several. */ |
| 2575 | n = col; |
| 2576 | do |
| 2577 | { |
| 2578 | col = n; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2579 | count = utf_ptr2len(oldp + n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2580 | n += count; |
| 2581 | } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n)); |
| 2582 | fixpos = 0; |
| 2583 | } |
| 2584 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2585 | |
| 2586 | /* |
| 2587 | * When count is too big, reduce it. |
| 2588 | */ |
| 2589 | movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */ |
| 2590 | if (movelen <= 1) |
| 2591 | { |
| 2592 | /* |
| 2593 | * If we just took off the last character of a non-blank line, and |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2594 | * fixpos is TRUE, we don't want to end up positioned at the NUL, |
| 2595 | * unless "restart_edit" is set or 'virtualedit' contains "onemore". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2596 | */ |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2597 | if (col > 0 && fixpos && restart_edit == 0 |
Bram Moolenaar | 29ddebe | 2019-01-26 17:28:26 +0100 | [diff] [blame] | 2598 | && (ve_flags & VE_ONEMORE) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2599 | { |
| 2600 | --curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2601 | curwin->w_cursor.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2602 | if (has_mbyte) |
| 2603 | curwin->w_cursor.col -= |
| 2604 | (*mb_head_off)(oldp, oldp + curwin->w_cursor.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2605 | } |
| 2606 | count = oldlen - col; |
| 2607 | movelen = 1; |
| 2608 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2609 | newlen = oldlen - count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2610 | |
| 2611 | /* |
| 2612 | * If the old line has been allocated the deletion can be done in the |
| 2613 | * existing line. Otherwise a new line has to be allocated |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2614 | * Can't do this when using Netbeans, because we would need to invoke |
| 2615 | * netbeans_removed(), which deallocates the line. Let ml_replace() take |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 2616 | * care of notifying Netbeans. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2617 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2618 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2619 | if (netbeans_active()) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2620 | alloc_newp = TRUE; |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2621 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2622 | #endif |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2623 | alloc_newp = !ml_line_alloced(); // check if oldp was allocated |
| 2624 | if (!alloc_newp) |
| 2625 | newp = oldp; // use same allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2626 | else |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2627 | { // need to allocate a new line |
| 2628 | newp = alloc((unsigned)(newlen + 1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2629 | if (newp == NULL) |
| 2630 | return FAIL; |
| 2631 | mch_memmove(newp, oldp, (size_t)col); |
| 2632 | } |
| 2633 | mch_memmove(newp + col, oldp + col + count, (size_t)movelen); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2634 | if (alloc_newp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2635 | ml_replace(lnum, newp, FALSE); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2636 | #ifdef FEAT_TEXT_PROP |
| 2637 | else |
| 2638 | { |
| 2639 | // Also move any following text properties. |
| 2640 | if (oldlen + 1 < curbuf->b_ml.ml_line_len) |
| 2641 | mch_memmove(newp + newlen + 1, oldp + oldlen + 1, |
| 2642 | (size_t)curbuf->b_ml.ml_line_len - oldlen - 1); |
| 2643 | curbuf->b_ml.ml_line_len -= count; |
| 2644 | } |
| 2645 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2646 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2647 | // mark the buffer as changed and prepare for displaying |
Bram Moolenaar | 33c8ca9 | 2019-01-02 18:00:27 +0100 | [diff] [blame] | 2648 | inserted_bytes(lnum, curwin->w_cursor.col, -count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2649 | |
| 2650 | return OK; |
| 2651 | } |
| 2652 | |
| 2653 | /* |
| 2654 | * Delete from cursor to end of line. |
| 2655 | * Caller must have prepared for undo. |
| 2656 | * |
| 2657 | * return FAIL for failure, OK otherwise |
| 2658 | */ |
| 2659 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2660 | truncate_line( |
| 2661 | int fixpos) /* if TRUE fix the cursor position when done */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2662 | { |
| 2663 | char_u *newp; |
| 2664 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2665 | colnr_T col = curwin->w_cursor.col; |
| 2666 | |
| 2667 | if (col == 0) |
| 2668 | newp = vim_strsave((char_u *)""); |
| 2669 | else |
| 2670 | newp = vim_strnsave(ml_get(lnum), col); |
| 2671 | |
| 2672 | if (newp == NULL) |
| 2673 | return FAIL; |
| 2674 | |
| 2675 | ml_replace(lnum, newp, FALSE); |
| 2676 | |
| 2677 | /* mark the buffer as changed and prepare for displaying */ |
| 2678 | changed_bytes(lnum, curwin->w_cursor.col); |
| 2679 | |
| 2680 | /* |
| 2681 | * If "fixpos" is TRUE we don't want to end up positioned at the NUL. |
| 2682 | */ |
| 2683 | if (fixpos && curwin->w_cursor.col > 0) |
| 2684 | --curwin->w_cursor.col; |
| 2685 | |
| 2686 | return OK; |
| 2687 | } |
| 2688 | |
| 2689 | /* |
| 2690 | * Delete "nlines" lines at the cursor. |
| 2691 | * Saves the lines for undo first if "undo" is TRUE. |
| 2692 | */ |
| 2693 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2694 | del_lines( |
| 2695 | long nlines, /* number of lines to delete */ |
| 2696 | int undo) /* if TRUE, prepare for undo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2697 | { |
| 2698 | long n; |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2699 | linenr_T first = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2700 | |
| 2701 | if (nlines <= 0) |
| 2702 | return; |
| 2703 | |
| 2704 | /* save the deleted lines for undo */ |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2705 | if (undo && u_savedel(first, nlines) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2706 | return; |
| 2707 | |
| 2708 | for (n = 0; n < nlines; ) |
| 2709 | { |
| 2710 | if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ |
| 2711 | break; |
| 2712 | |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2713 | ml_delete(first, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2714 | ++n; |
| 2715 | |
| 2716 | /* If we delete the last line in the file, stop */ |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2717 | if (first > curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2718 | break; |
| 2719 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2720 | |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2721 | /* Correct the cursor position before calling deleted_lines_mark(), it may |
| 2722 | * trigger a callback to display the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2723 | curwin->w_cursor.col = 0; |
| 2724 | check_cursor_lnum(); |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2725 | |
| 2726 | /* adjust marks, mark the buffer as changed and prepare for displaying */ |
| 2727 | deleted_lines_mark(first, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2731 | gchar_pos(pos_T *pos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2732 | { |
Bram Moolenaar | 8ada6aa | 2017-12-19 21:23:21 +0100 | [diff] [blame] | 2733 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2734 | |
Bram Moolenaar | 8ada6aa | 2017-12-19 21:23:21 +0100 | [diff] [blame] | 2735 | /* When searching columns is sometimes put at the end of a line. */ |
| 2736 | if (pos->col == MAXCOL) |
| 2737 | return NUL; |
| 2738 | ptr = ml_get_pos(pos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2739 | if (has_mbyte) |
| 2740 | return (*mb_ptr2char)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2741 | return (int)*ptr; |
| 2742 | } |
| 2743 | |
| 2744 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2745 | gchar_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2746 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2747 | if (has_mbyte) |
| 2748 | return (*mb_ptr2char)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2749 | return (int)*ml_get_cursor(); |
| 2750 | } |
| 2751 | |
| 2752 | /* |
| 2753 | * Write a character at the current cursor position. |
| 2754 | * It is directly written into the block. |
| 2755 | */ |
| 2756 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2757 | pchar_cursor(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2758 | { |
| 2759 | *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) |
| 2760 | + curwin->w_cursor.col) = c; |
| 2761 | } |
| 2762 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2763 | /* |
| 2764 | * When extra == 0: Return TRUE if the cursor is before or on the first |
| 2765 | * non-blank in the line. |
| 2766 | * When extra == 1: Return TRUE if the cursor is before the first non-blank in |
| 2767 | * the line. |
| 2768 | */ |
| 2769 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2770 | inindent(int extra) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2771 | { |
| 2772 | char_u *ptr; |
| 2773 | colnr_T col; |
| 2774 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2775 | for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2776 | ++ptr; |
| 2777 | if (col >= curwin->w_cursor.col + extra) |
| 2778 | return TRUE; |
| 2779 | else |
| 2780 | return FALSE; |
| 2781 | } |
| 2782 | |
| 2783 | /* |
| 2784 | * Skip to next part of an option argument: Skip space and comma. |
| 2785 | */ |
| 2786 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2787 | skip_to_option_part(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2788 | { |
| 2789 | if (*p == ',') |
| 2790 | ++p; |
| 2791 | while (*p == ' ') |
| 2792 | ++p; |
| 2793 | return p; |
| 2794 | } |
| 2795 | |
| 2796 | /* |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2797 | * Call this function when something in the current buffer is changed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2798 | * |
| 2799 | * Most often called through changed_bytes() and changed_lines(), which also |
| 2800 | * mark the area of the display to be redrawn. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2801 | * |
| 2802 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2803 | */ |
| 2804 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2805 | changed(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2806 | { |
| 2807 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 2808 | if (p_imst == IM_ON_THE_SPOT) |
| 2809 | { |
| 2810 | /* The text of the preediting area is inserted, but this doesn't |
| 2811 | * mean a change of the buffer yet. That is delayed until the |
| 2812 | * text is committed. (this means preedit becomes empty) */ |
| 2813 | if (im_is_preediting() && !xim_changed_while_preediting) |
| 2814 | return; |
| 2815 | xim_changed_while_preediting = FALSE; |
| 2816 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2817 | #endif |
| 2818 | |
| 2819 | if (!curbuf->b_changed) |
| 2820 | { |
| 2821 | int save_msg_scroll = msg_scroll; |
| 2822 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2823 | /* Give a warning about changing a read-only file. This may also |
| 2824 | * check-out the file, thus change "curbuf"! */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2825 | change_warning(0); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2826 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2827 | /* Create a swap file if that is wanted. |
| 2828 | * Don't do this for "nofile" and "nowrite" buffer types. */ |
| 2829 | if (curbuf->b_may_swap |
| 2830 | #ifdef FEAT_QUICKFIX |
| 2831 | && !bt_dontwrite(curbuf) |
| 2832 | #endif |
| 2833 | ) |
| 2834 | { |
Bram Moolenaar | b01f357 | 2016-01-15 15:17:04 +0100 | [diff] [blame] | 2835 | int save_need_wait_return = need_wait_return; |
| 2836 | |
| 2837 | need_wait_return = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2838 | ml_open_file(curbuf); |
| 2839 | |
| 2840 | /* The ml_open_file() can cause an ATTENTION message. |
| 2841 | * Wait two seconds, to make sure the user reads this unexpected |
| 2842 | * message. Since we could be anywhere, call wait_return() now, |
| 2843 | * and don't let the emsg() set msg_scroll. */ |
| 2844 | if (need_wait_return && emsg_silent == 0) |
| 2845 | { |
| 2846 | out_flush(); |
| 2847 | ui_delay(2000L, TRUE); |
| 2848 | wait_return(TRUE); |
| 2849 | msg_scroll = save_msg_scroll; |
| 2850 | } |
Bram Moolenaar | b01f357 | 2016-01-15 15:17:04 +0100 | [diff] [blame] | 2851 | else |
| 2852 | need_wait_return = save_need_wait_return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2853 | } |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2854 | changed_int(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2855 | } |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 2856 | ++CHANGEDTICK(curbuf); |
Bram Moolenaar | 4a7d2d3 | 2019-02-21 16:25:50 +0100 | [diff] [blame^] | 2857 | |
| 2858 | #ifdef FEAT_SEARCH_EXTRA |
| 2859 | // If a pattern is highlighted, the position may now be invalid. |
| 2860 | highlight_match = FALSE; |
| 2861 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2864 | /* |
| 2865 | * Internal part of changed(), no user interaction. |
| 2866 | */ |
| 2867 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2868 | changed_int(void) |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2869 | { |
| 2870 | curbuf->b_changed = TRUE; |
| 2871 | ml_setflags(curbuf); |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2872 | check_status(curbuf); |
| 2873 | redraw_tabline = TRUE; |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2874 | #ifdef FEAT_TITLE |
| 2875 | need_maketitle = TRUE; /* set window title later */ |
| 2876 | #endif |
| 2877 | } |
| 2878 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 2879 | static void changedOneline(buf_T *buf, linenr_T lnum); |
| 2880 | static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra); |
| 2881 | static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2882 | |
| 2883 | /* |
| 2884 | * Changed bytes within a single line for the current buffer. |
| 2885 | * - marks the windows on this buffer to be redisplayed |
| 2886 | * - marks the buffer changed by calling changed() |
| 2887 | * - invalidates cached values |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2888 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2889 | */ |
| 2890 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2891 | changed_bytes(linenr_T lnum, colnr_T col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2892 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2893 | changedOneline(curbuf, lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2894 | changed_common(lnum, col, lnum + 1, 0L); |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2895 | |
| 2896 | #ifdef FEAT_DIFF |
| 2897 | /* Diff highlighting in other diff windows may need to be updated too. */ |
| 2898 | if (curwin->w_p_diff) |
| 2899 | { |
| 2900 | win_T *wp; |
| 2901 | linenr_T wlnum; |
| 2902 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 2903 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2904 | if (wp->w_p_diff && wp != curwin) |
| 2905 | { |
| 2906 | redraw_win_later(wp, VALID); |
| 2907 | wlnum = diff_lnum_win(lnum, wp); |
| 2908 | if (wlnum > 0) |
| 2909 | changedOneline(wp->w_buffer, wlnum); |
| 2910 | } |
| 2911 | } |
| 2912 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2915 | /* |
| 2916 | * Like changed_bytes() but also adjust text properties for "added" bytes. |
| 2917 | * When "added" is negative text was deleted. |
| 2918 | */ |
| 2919 | void |
Bram Moolenaar | 402385a | 2019-01-11 14:10:03 +0100 | [diff] [blame] | 2920 | inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED) |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2921 | { |
| 2922 | changed_bytes(lnum, col); |
| 2923 | |
| 2924 | #ifdef FEAT_TEXT_PROP |
| 2925 | if (curbuf->b_has_textprop && added != 0) |
| 2926 | adjust_prop_columns(lnum, col, added); |
| 2927 | #endif |
| 2928 | } |
| 2929 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2930 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2931 | changedOneline(buf_T *buf, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2932 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2933 | if (buf->b_mod_set) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2934 | { |
| 2935 | /* find the maximum area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2936 | if (lnum < buf->b_mod_top) |
| 2937 | buf->b_mod_top = lnum; |
| 2938 | else if (lnum >= buf->b_mod_bot) |
| 2939 | buf->b_mod_bot = lnum + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2940 | } |
| 2941 | else |
| 2942 | { |
| 2943 | /* set the area that must be redisplayed to one line */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2944 | buf->b_mod_set = TRUE; |
| 2945 | buf->b_mod_top = lnum; |
| 2946 | buf->b_mod_bot = lnum + 1; |
| 2947 | buf->b_mod_xlines = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2948 | } |
| 2949 | } |
| 2950 | |
| 2951 | /* |
| 2952 | * Appended "count" lines below line "lnum" in the current buffer. |
| 2953 | * Must be called AFTER the change and after mark_adjust(). |
| 2954 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 2955 | */ |
| 2956 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2957 | appended_lines(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2958 | { |
| 2959 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 2960 | } |
| 2961 | |
| 2962 | /* |
| 2963 | * Like appended_lines(), but adjust marks first. |
| 2964 | */ |
| 2965 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2966 | appended_lines_mark(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2967 | { |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 2968 | /* Skip mark_adjust when adding a line after the last one, there can't |
Bram Moolenaar | f58a847 | 2017-03-05 18:03:04 +0100 | [diff] [blame] | 2969 | * be marks there. But it's still needed in diff mode. */ |
| 2970 | if (lnum + count < curbuf->b_ml.ml_line_count |
| 2971 | #ifdef FEAT_DIFF |
| 2972 | || curwin->w_p_diff |
| 2973 | #endif |
| 2974 | ) |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 2975 | mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2976 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 2977 | } |
| 2978 | |
| 2979 | /* |
| 2980 | * Deleted "count" lines at line "lnum" in the current buffer. |
| 2981 | * Must be called AFTER the change and after mark_adjust(). |
| 2982 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 2983 | */ |
| 2984 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2985 | deleted_lines(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2986 | { |
| 2987 | changed_lines(lnum, 0, lnum + count, -count); |
| 2988 | } |
| 2989 | |
| 2990 | /* |
| 2991 | * Like deleted_lines(), but adjust marks first. |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2992 | * Make sure the cursor is on a valid line before calling, a GUI callback may |
| 2993 | * be triggered to display the cursor. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2994 | */ |
| 2995 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2996 | deleted_lines_mark(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2997 | { |
| 2998 | mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count); |
| 2999 | changed_lines(lnum, 0, lnum + count, -count); |
| 3000 | } |
| 3001 | |
| 3002 | /* |
| 3003 | * Changed lines for the current buffer. |
| 3004 | * Must be called AFTER the change and after mark_adjust(). |
| 3005 | * - mark the buffer changed by calling changed() |
| 3006 | * - mark the windows on this buffer to be redisplayed |
| 3007 | * - invalidate cached values |
| 3008 | * "lnum" is the first line that needs displaying, "lnume" the first line |
| 3009 | * below the changed lines (BEFORE the change). |
| 3010 | * When only inserting lines, "lnum" and "lnume" are equal. |
| 3011 | * Takes care of calling changed() and updating b_mod_*. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3012 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3013 | */ |
| 3014 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3015 | changed_lines( |
| 3016 | linenr_T lnum, /* first line with change */ |
| 3017 | colnr_T col, /* column in first line with change */ |
| 3018 | linenr_T lnume, /* line below last changed line */ |
| 3019 | long xtra) /* number of extra lines (negative when deleting) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3020 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3021 | changed_lines_buf(curbuf, lnum, lnume, xtra); |
| 3022 | |
| 3023 | #ifdef FEAT_DIFF |
Bram Moolenaar | e3521d9 | 2018-09-16 14:10:31 +0200 | [diff] [blame] | 3024 | if (xtra == 0 && curwin->w_p_diff && !diff_internal()) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3025 | { |
| 3026 | /* When the number of lines doesn't change then mark_adjust() isn't |
| 3027 | * called and other diff buffers still need to be marked for |
| 3028 | * displaying. */ |
| 3029 | win_T *wp; |
| 3030 | linenr_T wlnum; |
| 3031 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 3032 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3033 | if (wp->w_p_diff && wp != curwin) |
| 3034 | { |
| 3035 | redraw_win_later(wp, VALID); |
| 3036 | wlnum = diff_lnum_win(lnum, wp); |
| 3037 | if (wlnum > 0) |
| 3038 | changed_lines_buf(wp->w_buffer, wlnum, |
| 3039 | lnume - lnum + wlnum, 0L); |
| 3040 | } |
| 3041 | } |
| 3042 | #endif |
| 3043 | |
| 3044 | changed_common(lnum, col, lnume, xtra); |
| 3045 | } |
| 3046 | |
| 3047 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3048 | changed_lines_buf( |
| 3049 | buf_T *buf, |
| 3050 | linenr_T lnum, /* first line with change */ |
| 3051 | linenr_T lnume, /* line below last changed line */ |
| 3052 | long xtra) /* number of extra lines (negative when deleting) */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3053 | { |
| 3054 | if (buf->b_mod_set) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3055 | { |
| 3056 | /* find the maximum area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3057 | if (lnum < buf->b_mod_top) |
| 3058 | buf->b_mod_top = lnum; |
| 3059 | if (lnum < buf->b_mod_bot) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3060 | { |
| 3061 | /* adjust old bot position for xtra lines */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3062 | buf->b_mod_bot += xtra; |
| 3063 | if (buf->b_mod_bot < lnum) |
| 3064 | buf->b_mod_bot = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3065 | } |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3066 | if (lnume + xtra > buf->b_mod_bot) |
| 3067 | buf->b_mod_bot = lnume + xtra; |
| 3068 | buf->b_mod_xlines += xtra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3069 | } |
| 3070 | else |
| 3071 | { |
| 3072 | /* set the area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3073 | buf->b_mod_set = TRUE; |
| 3074 | buf->b_mod_top = lnum; |
| 3075 | buf->b_mod_bot = lnume + xtra; |
| 3076 | buf->b_mod_xlines = xtra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3077 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3080 | /* |
| 3081 | * Common code for when a change is was made. |
| 3082 | * See changed_lines() for the arguments. |
| 3083 | * Careful: may trigger autocommands that reload the buffer. |
| 3084 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3085 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3086 | changed_common( |
| 3087 | linenr_T lnum, |
| 3088 | colnr_T col, |
| 3089 | linenr_T lnume, |
| 3090 | long xtra) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3091 | { |
| 3092 | win_T *wp; |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3093 | tabpage_T *tp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3094 | int i; |
| 3095 | #ifdef FEAT_JUMPLIST |
| 3096 | int cols; |
| 3097 | pos_T *p; |
| 3098 | int add; |
| 3099 | #endif |
| 3100 | |
| 3101 | /* mark the buffer as modified */ |
| 3102 | changed(); |
| 3103 | |
Bram Moolenaar | e3521d9 | 2018-09-16 14:10:31 +0200 | [diff] [blame] | 3104 | #ifdef FEAT_DIFF |
| 3105 | if (curwin->w_p_diff && diff_internal()) |
| 3106 | curtab->tp_diff_update = TRUE; |
| 3107 | #endif |
| 3108 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3109 | /* set the '. mark */ |
| 3110 | if (!cmdmod.keepjumps) |
| 3111 | { |
| 3112 | curbuf->b_last_change.lnum = lnum; |
| 3113 | curbuf->b_last_change.col = col; |
| 3114 | |
| 3115 | #ifdef FEAT_JUMPLIST |
| 3116 | /* Create a new entry if a new undo-able change was started or we |
| 3117 | * don't have an entry yet. */ |
| 3118 | if (curbuf->b_new_change || curbuf->b_changelistlen == 0) |
| 3119 | { |
| 3120 | if (curbuf->b_changelistlen == 0) |
| 3121 | add = TRUE; |
| 3122 | else |
| 3123 | { |
| 3124 | /* Don't create a new entry when the line number is the same |
| 3125 | * as the last one and the column is not too far away. Avoids |
| 3126 | * creating many entries for typing "xxxxx". */ |
| 3127 | p = &curbuf->b_changelist[curbuf->b_changelistlen - 1]; |
| 3128 | if (p->lnum != lnum) |
| 3129 | add = TRUE; |
| 3130 | else |
| 3131 | { |
| 3132 | cols = comp_textwidth(FALSE); |
| 3133 | if (cols == 0) |
| 3134 | cols = 79; |
| 3135 | add = (p->col + cols < col || col + cols < p->col); |
| 3136 | } |
| 3137 | } |
| 3138 | if (add) |
| 3139 | { |
| 3140 | /* This is the first of a new sequence of undo-able changes |
| 3141 | * and it's at some distance of the last change. Use a new |
| 3142 | * position in the changelist. */ |
| 3143 | curbuf->b_new_change = FALSE; |
| 3144 | |
| 3145 | if (curbuf->b_changelistlen == JUMPLISTSIZE) |
| 3146 | { |
| 3147 | /* changelist is full: remove oldest entry */ |
| 3148 | curbuf->b_changelistlen = JUMPLISTSIZE - 1; |
| 3149 | mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1, |
| 3150 | sizeof(pos_T) * (JUMPLISTSIZE - 1)); |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3151 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3152 | { |
| 3153 | /* Correct position in changelist for other windows on |
| 3154 | * this buffer. */ |
| 3155 | if (wp->w_buffer == curbuf && wp->w_changelistidx > 0) |
| 3156 | --wp->w_changelistidx; |
| 3157 | } |
| 3158 | } |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3159 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3160 | { |
| 3161 | /* For other windows, if the position in the changelist is |
| 3162 | * at the end it stays at the end. */ |
| 3163 | if (wp->w_buffer == curbuf |
| 3164 | && wp->w_changelistidx == curbuf->b_changelistlen) |
| 3165 | ++wp->w_changelistidx; |
| 3166 | } |
| 3167 | ++curbuf->b_changelistlen; |
| 3168 | } |
| 3169 | } |
| 3170 | curbuf->b_changelist[curbuf->b_changelistlen - 1] = |
| 3171 | curbuf->b_last_change; |
| 3172 | /* The current window is always after the last change, so that "g," |
| 3173 | * takes you back to it. */ |
| 3174 | curwin->w_changelistidx = curbuf->b_changelistlen; |
| 3175 | #endif |
| 3176 | } |
| 3177 | |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3178 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3179 | { |
| 3180 | if (wp->w_buffer == curbuf) |
| 3181 | { |
| 3182 | /* Mark this window to be redrawn later. */ |
| 3183 | if (wp->w_redr_type < VALID) |
| 3184 | wp->w_redr_type = VALID; |
| 3185 | |
| 3186 | /* Check if a change in the buffer has invalidated the cached |
| 3187 | * values for the cursor. */ |
| 3188 | #ifdef FEAT_FOLDING |
| 3189 | /* |
| 3190 | * Update the folds for this window. Can't postpone this, because |
| 3191 | * a following operator might work on the whole fold: ">>dd". |
| 3192 | */ |
| 3193 | foldUpdate(wp, lnum, lnume + xtra - 1); |
| 3194 | |
| 3195 | /* The change may cause lines above or below the change to become |
| 3196 | * included in a fold. Set lnum/lnume to the first/last line that |
| 3197 | * might be displayed differently. |
| 3198 | * Set w_cline_folded here as an efficient way to update it when |
| 3199 | * inserting lines just above a closed fold. */ |
| 3200 | i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL); |
| 3201 | if (wp->w_cursor.lnum == lnum) |
| 3202 | wp->w_cline_folded = i; |
| 3203 | i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL); |
| 3204 | if (wp->w_cursor.lnum == lnume) |
| 3205 | wp->w_cline_folded = i; |
| 3206 | |
| 3207 | /* If the changed line is in a range of previously folded lines, |
| 3208 | * compare with the first line in that range. */ |
| 3209 | if (wp->w_cursor.lnum <= lnum) |
| 3210 | { |
| 3211 | i = find_wl_entry(wp, lnum); |
| 3212 | if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum) |
| 3213 | changed_line_abv_curs_win(wp); |
| 3214 | } |
| 3215 | #endif |
| 3216 | |
| 3217 | if (wp->w_cursor.lnum > lnum) |
| 3218 | changed_line_abv_curs_win(wp); |
| 3219 | else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col) |
| 3220 | changed_cline_bef_curs_win(wp); |
| 3221 | if (wp->w_botline >= lnum) |
| 3222 | { |
| 3223 | /* Assume that botline doesn't change (inserted lines make |
| 3224 | * other lines scroll down below botline). */ |
| 3225 | approximate_botline_win(wp); |
| 3226 | } |
| 3227 | |
| 3228 | /* Check if any w_lines[] entries have become invalid. |
| 3229 | * For entries below the change: Correct the lnums for |
| 3230 | * inserted/deleted lines. Makes it possible to stop displaying |
| 3231 | * after the change. */ |
| 3232 | for (i = 0; i < wp->w_lines_valid; ++i) |
| 3233 | if (wp->w_lines[i].wl_valid) |
| 3234 | { |
| 3235 | if (wp->w_lines[i].wl_lnum >= lnum) |
| 3236 | { |
| 3237 | if (wp->w_lines[i].wl_lnum < lnume) |
| 3238 | { |
| 3239 | /* line included in change */ |
| 3240 | wp->w_lines[i].wl_valid = FALSE; |
| 3241 | } |
| 3242 | else if (xtra != 0) |
| 3243 | { |
| 3244 | /* line below change */ |
| 3245 | wp->w_lines[i].wl_lnum += xtra; |
| 3246 | #ifdef FEAT_FOLDING |
| 3247 | wp->w_lines[i].wl_lastlnum += xtra; |
| 3248 | #endif |
| 3249 | } |
| 3250 | } |
| 3251 | #ifdef FEAT_FOLDING |
| 3252 | else if (wp->w_lines[i].wl_lastlnum >= lnum) |
| 3253 | { |
| 3254 | /* change somewhere inside this range of folded lines, |
| 3255 | * may need to be redrawn */ |
| 3256 | wp->w_lines[i].wl_valid = FALSE; |
| 3257 | } |
| 3258 | #endif |
| 3259 | } |
Bram Moolenaar | 3234cc6 | 2009-11-03 17:47:12 +0000 | [diff] [blame] | 3260 | |
| 3261 | #ifdef FEAT_FOLDING |
| 3262 | /* Take care of side effects for setting w_topline when folds have |
| 3263 | * changed. Esp. when the buffer was changed in another window. */ |
| 3264 | if (hasAnyFolding(wp)) |
| 3265 | set_topline(wp, wp->w_topline); |
| 3266 | #endif |
Bram Moolenaar | fd859c9 | 2014-05-13 12:44:24 +0200 | [diff] [blame] | 3267 | /* relative numbering may require updating more */ |
| 3268 | if (wp->w_p_rnu) |
| 3269 | redraw_win_later(wp, SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3270 | } |
| 3271 | } |
| 3272 | |
| 3273 | /* Call update_screen() later, which checks out what needs to be redrawn, |
| 3274 | * since it notices b_mod_set and then uses b_mod_*. */ |
| 3275 | if (must_redraw < VALID) |
| 3276 | must_redraw = VALID; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3277 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3278 | /* when the cursor line is changed always trigger CursorMoved */ |
Bram Moolenaar | e163f1c | 2006-10-17 09:12:21 +0000 | [diff] [blame] | 3279 | if (lnum <= curwin->w_cursor.lnum |
| 3280 | && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum) |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3281 | last_cursormoved.lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3282 | } |
| 3283 | |
| 3284 | /* |
| 3285 | * unchanged() is called when the changed flag must be reset for buffer 'buf' |
| 3286 | */ |
| 3287 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3288 | unchanged( |
| 3289 | buf_T *buf, |
| 3290 | int ff) /* also reset 'fileformat' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3291 | { |
Bram Moolenaar | 164c60f | 2011-01-22 00:11:50 +0100 | [diff] [blame] | 3292 | if (buf->b_changed || (ff && file_ff_differs(buf, FALSE))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3293 | { |
| 3294 | buf->b_changed = 0; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3295 | ml_setflags(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3296 | if (ff) |
| 3297 | save_file_ff(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3298 | check_status(buf); |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 3299 | redraw_tabline = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3300 | #ifdef FEAT_TITLE |
| 3301 | need_maketitle = TRUE; /* set window title later */ |
| 3302 | #endif |
| 3303 | } |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 3304 | ++CHANGEDTICK(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3305 | #ifdef FEAT_NETBEANS_INTG |
| 3306 | netbeans_unmodified(buf); |
| 3307 | #endif |
| 3308 | } |
| 3309 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3310 | /* |
| 3311 | * check_status: called when the status bars for the buffer 'buf' |
| 3312 | * need to be updated |
| 3313 | */ |
| 3314 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3315 | check_status(buf_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3316 | { |
| 3317 | win_T *wp; |
| 3318 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 3319 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3320 | if (wp->w_buffer == buf && wp->w_status_height) |
| 3321 | { |
| 3322 | wp->w_redr_status = TRUE; |
| 3323 | if (must_redraw < VALID) |
| 3324 | must_redraw = VALID; |
| 3325 | } |
| 3326 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3327 | |
| 3328 | /* |
| 3329 | * If the file is readonly, give a warning message with the first change. |
| 3330 | * Don't do this for autocommands. |
| 3331 | * Don't use emsg(), because it flushes the macro buffer. |
Bram Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 3332 | * If we have undone all changes b_changed will be FALSE, but "b_did_warn" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3333 | * will be TRUE. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3334 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3335 | */ |
| 3336 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3337 | change_warning( |
| 3338 | int col) /* column for message; non-zero when in insert |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3339 | mode and 'showmode' is on */ |
| 3340 | { |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 3341 | static char *w_readonly = N_("W10: Warning: Changing a readonly file"); |
| 3342 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3343 | if (curbuf->b_did_warn == FALSE |
| 3344 | && curbufIsChanged() == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3345 | && !autocmd_busy |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3346 | && curbuf->b_p_ro) |
| 3347 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3348 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3349 | apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3350 | --curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3351 | if (!curbuf->b_p_ro) |
| 3352 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3353 | /* |
| 3354 | * Do what msg() does, but with a column offset if the warning should |
| 3355 | * be after the mode message. |
| 3356 | */ |
| 3357 | msg_start(); |
| 3358 | if (msg_row == Rows - 1) |
| 3359 | msg_col = col; |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3360 | msg_source(HL_ATTR(HLF_W)); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3361 | msg_puts_attr(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST); |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 3362 | #ifdef FEAT_EVAL |
| 3363 | set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1); |
| 3364 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3365 | msg_clr_eos(); |
| 3366 | (void)msg_end(); |
Bram Moolenaar | e5f2a07 | 2017-02-01 22:31:49 +0100 | [diff] [blame] | 3367 | if (msg_silent == 0 && !silent_mode |
| 3368 | #ifdef FEAT_EVAL |
| 3369 | && time_for_testing != 1 |
| 3370 | #endif |
| 3371 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3372 | { |
| 3373 | out_flush(); |
| 3374 | ui_delay(1000L, TRUE); /* give the user time to think about it */ |
| 3375 | } |
| 3376 | curbuf->b_did_warn = TRUE; |
| 3377 | redraw_cmdline = FALSE; /* don't redraw and erase the message */ |
| 3378 | if (msg_row < Rows - 1) |
| 3379 | showmode(); |
| 3380 | } |
| 3381 | } |
| 3382 | |
| 3383 | /* |
| 3384 | * Ask for a reply from the user, a 'y' or a 'n'. |
| 3385 | * No other characters are accepted, the message is repeated until a valid |
| 3386 | * reply is entered or CTRL-C is hit. |
| 3387 | * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters |
| 3388 | * from any buffers but directly from the user. |
| 3389 | * |
| 3390 | * return the 'y' or 'n' |
| 3391 | */ |
| 3392 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3393 | ask_yesno(char_u *str, int direct) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3394 | { |
| 3395 | int r = ' '; |
| 3396 | int save_State = State; |
| 3397 | |
| 3398 | if (exiting) /* put terminal in raw mode for this question */ |
| 3399 | settmode(TMODE_RAW); |
| 3400 | ++no_wait_return; |
| 3401 | #ifdef USE_ON_FLY_SCROLL |
| 3402 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 3403 | #endif |
| 3404 | State = CONFIRM; /* mouse behaves like with :confirm */ |
| 3405 | #ifdef FEAT_MOUSE |
| 3406 | setmouse(); /* disables mouse for xterm */ |
| 3407 | #endif |
| 3408 | ++no_mapping; |
| 3409 | ++allow_keys; /* no mapping here, but recognize keys */ |
| 3410 | |
| 3411 | while (r != 'y' && r != 'n') |
| 3412 | { |
| 3413 | /* same highlighting as for wait_return */ |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3414 | smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3415 | if (direct) |
| 3416 | r = get_keystroke(); |
| 3417 | else |
Bram Moolenaar | 913626c | 2008-01-03 11:43:42 +0000 | [diff] [blame] | 3418 | r = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3419 | if (r == Ctrl_C || r == ESC) |
| 3420 | r = 'n'; |
| 3421 | msg_putchar(r); /* show what you typed */ |
| 3422 | out_flush(); |
| 3423 | } |
| 3424 | --no_wait_return; |
| 3425 | State = save_State; |
| 3426 | #ifdef FEAT_MOUSE |
| 3427 | setmouse(); |
| 3428 | #endif |
| 3429 | --no_mapping; |
| 3430 | --allow_keys; |
| 3431 | |
| 3432 | return r; |
| 3433 | } |
| 3434 | |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3435 | #if defined(FEAT_MOUSE) || defined(PROTO) |
| 3436 | /* |
| 3437 | * Return TRUE if "c" is a mouse key. |
| 3438 | */ |
| 3439 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3440 | is_mouse_key(int c) |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3441 | { |
| 3442 | return c == K_LEFTMOUSE |
| 3443 | || c == K_LEFTMOUSE_NM |
| 3444 | || c == K_LEFTDRAG |
| 3445 | || c == K_LEFTRELEASE |
| 3446 | || c == K_LEFTRELEASE_NM |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3447 | || c == K_MOUSEMOVE |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3448 | || c == K_MIDDLEMOUSE |
| 3449 | || c == K_MIDDLEDRAG |
| 3450 | || c == K_MIDDLERELEASE |
| 3451 | || c == K_RIGHTMOUSE |
| 3452 | || c == K_RIGHTDRAG |
| 3453 | || c == K_RIGHTRELEASE |
| 3454 | || c == K_MOUSEDOWN |
| 3455 | || c == K_MOUSEUP |
| 3456 | || c == K_MOUSELEFT |
| 3457 | || c == K_MOUSERIGHT |
| 3458 | || c == K_X1MOUSE |
| 3459 | || c == K_X1DRAG |
| 3460 | || c == K_X1RELEASE |
| 3461 | || c == K_X2MOUSE |
| 3462 | || c == K_X2DRAG |
| 3463 | || c == K_X2RELEASE; |
| 3464 | } |
| 3465 | #endif |
| 3466 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3467 | /* |
| 3468 | * Get a key stroke directly from the user. |
| 3469 | * Ignores mouse clicks and scrollbar events, except a click for the left |
| 3470 | * button (used at the more prompt). |
| 3471 | * Doesn't use vgetc(), because it syncs undo and eats mapped characters. |
| 3472 | * Disadvantage: typeahead is ignored. |
| 3473 | * Translates the interrupt character for unix to ESC. |
| 3474 | */ |
| 3475 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3476 | get_keystroke(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3477 | { |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3478 | char_u *buf = NULL; |
| 3479 | int buflen = 150; |
| 3480 | int maxlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3481 | int len = 0; |
| 3482 | int n; |
| 3483 | int save_mapped_ctrl_c = mapped_ctrl_c; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3484 | int waited = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3485 | |
| 3486 | mapped_ctrl_c = FALSE; /* mappings are not used here */ |
| 3487 | for (;;) |
| 3488 | { |
| 3489 | cursor_on(); |
| 3490 | out_flush(); |
| 3491 | |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3492 | /* Leave some room for check_termcode() to insert a key code into (max |
| 3493 | * 5 chars plus NUL). And fix_input_buffer() can triple the number of |
| 3494 | * bytes. */ |
| 3495 | maxlen = (buflen - 6 - len) / 3; |
| 3496 | if (buf == NULL) |
| 3497 | buf = alloc(buflen); |
| 3498 | else if (maxlen < 10) |
| 3499 | { |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 3500 | char_u *t_buf = buf; |
| 3501 | |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 3502 | /* Need some more space. This might happen when receiving a long |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3503 | * escape sequence. */ |
| 3504 | buflen += 100; |
| 3505 | buf = vim_realloc(buf, buflen); |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 3506 | if (buf == NULL) |
| 3507 | vim_free(t_buf); |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3508 | maxlen = (buflen - 6 - len) / 3; |
| 3509 | } |
| 3510 | if (buf == NULL) |
| 3511 | { |
| 3512 | do_outofmem_msg((long_u)buflen); |
| 3513 | return ESC; /* panic! */ |
| 3514 | } |
| 3515 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3516 | /* First time: blocking wait. Second time: wait up to 100ms for a |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3517 | * terminal code to complete. */ |
| 3518 | n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3519 | if (n > 0) |
| 3520 | { |
| 3521 | /* Replace zero and CSI by a special key code. */ |
Bram Moolenaar | 6bff02e | 2016-08-16 22:50:55 +0200 | [diff] [blame] | 3522 | n = fix_input_buffer(buf + len, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3523 | len += n; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3524 | waited = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3525 | } |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3526 | else if (len > 0) |
| 3527 | ++waited; /* keep track of the waiting time */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3528 | |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3529 | /* Incomplete termcode and not timed out yet: get more characters */ |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3530 | if ((n = check_termcode(1, buf, buflen, &len)) < 0 |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3531 | && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3532 | continue; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3533 | |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3534 | if (n == KEYLEN_REMOVED) /* key code removed */ |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3535 | { |
Bram Moolenaar | fd30cd4 | 2011-03-22 13:07:26 +0100 | [diff] [blame] | 3536 | if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0) |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3537 | { |
| 3538 | /* Redrawing was postponed, do it now. */ |
| 3539 | update_screen(0); |
| 3540 | setcursor(); /* put cursor back where it belongs */ |
| 3541 | } |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3542 | continue; |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3543 | } |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3544 | if (n > 0) /* found a termcode: adjust length */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3545 | len = n; |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3546 | if (len == 0) /* nothing typed yet */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3547 | continue; |
| 3548 | |
| 3549 | /* Handle modifier and/or special key code. */ |
| 3550 | n = buf[0]; |
| 3551 | if (n == K_SPECIAL) |
| 3552 | { |
| 3553 | n = TO_SPECIAL(buf[1], buf[2]); |
| 3554 | if (buf[1] == KS_MODIFIER |
| 3555 | || n == K_IGNORE |
Bram Moolenaar | a5be25e | 2013-03-16 21:35:33 +0100 | [diff] [blame] | 3556 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3557 | || (is_mouse_key(n) && n != K_LEFTMOUSE) |
Bram Moolenaar | a5be25e | 2013-03-16 21:35:33 +0100 | [diff] [blame] | 3558 | #endif |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3559 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3560 | || n == K_VER_SCROLLBAR |
| 3561 | || n == K_HOR_SCROLLBAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3562 | #endif |
| 3563 | ) |
| 3564 | { |
| 3565 | if (buf[1] == KS_MODIFIER) |
| 3566 | mod_mask = buf[2]; |
| 3567 | len -= 3; |
| 3568 | if (len > 0) |
| 3569 | mch_memmove(buf, buf + 3, (size_t)len); |
| 3570 | continue; |
| 3571 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 3572 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3573 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3574 | if (has_mbyte) |
| 3575 | { |
| 3576 | if (MB_BYTE2LEN(n) > len) |
| 3577 | continue; /* more bytes to get */ |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3578 | buf[len >= buflen ? buflen - 1 : len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3579 | n = (*mb_ptr2char)(buf); |
| 3580 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3581 | #ifdef UNIX |
| 3582 | if (n == intr_char) |
| 3583 | n = ESC; |
| 3584 | #endif |
| 3585 | break; |
| 3586 | } |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3587 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3588 | |
| 3589 | mapped_ctrl_c = save_mapped_ctrl_c; |
| 3590 | return n; |
| 3591 | } |
| 3592 | |
| 3593 | /* |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3594 | * Get a number from the user. |
| 3595 | * When "mouse_used" is not NULL allow using the mouse. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3596 | */ |
| 3597 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3598 | get_number( |
| 3599 | int colon, /* allow colon to abort */ |
| 3600 | int *mouse_used) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3601 | { |
| 3602 | int n = 0; |
| 3603 | int c; |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3604 | int typed = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3605 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3606 | if (mouse_used != NULL) |
| 3607 | *mouse_used = FALSE; |
| 3608 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3609 | /* When not printing messages, the user won't know what to type, return a |
| 3610 | * zero (as if CR was hit). */ |
| 3611 | if (msg_silent != 0) |
| 3612 | return 0; |
| 3613 | |
| 3614 | #ifdef USE_ON_FLY_SCROLL |
| 3615 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 3616 | #endif |
| 3617 | ++no_mapping; |
| 3618 | ++allow_keys; /* no mapping here, but recognize keys */ |
| 3619 | for (;;) |
| 3620 | { |
| 3621 | windgoto(msg_row, msg_col); |
| 3622 | c = safe_vgetc(); |
| 3623 | if (VIM_ISDIGIT(c)) |
| 3624 | { |
| 3625 | n = n * 10 + c - '0'; |
| 3626 | msg_putchar(c); |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3627 | ++typed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3628 | } |
| 3629 | else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H) |
| 3630 | { |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3631 | if (typed > 0) |
| 3632 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3633 | msg_puts("\b \b"); |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3634 | --typed; |
| 3635 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3636 | n /= 10; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3637 | } |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3638 | #ifdef FEAT_MOUSE |
| 3639 | else if (mouse_used != NULL && c == K_LEFTMOUSE) |
| 3640 | { |
| 3641 | *mouse_used = TRUE; |
| 3642 | n = mouse_row + 1; |
| 3643 | break; |
| 3644 | } |
| 3645 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3646 | else if (n == 0 && c == ':' && colon) |
| 3647 | { |
| 3648 | stuffcharReadbuff(':'); |
| 3649 | if (!exmode_active) |
| 3650 | cmdline_row = msg_row; |
| 3651 | skip_redraw = TRUE; /* skip redraw once */ |
| 3652 | do_redraw = FALSE; |
| 3653 | break; |
| 3654 | } |
| 3655 | else if (c == CAR || c == NL || c == Ctrl_C || c == ESC) |
| 3656 | break; |
| 3657 | } |
| 3658 | --no_mapping; |
| 3659 | --allow_keys; |
| 3660 | return n; |
| 3661 | } |
| 3662 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3663 | /* |
| 3664 | * Ask the user to enter a number. |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3665 | * When "mouse_used" is not NULL allow using the mouse and in that case return |
| 3666 | * the line number. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3667 | */ |
| 3668 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3669 | prompt_for_number(int *mouse_used) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3670 | { |
| 3671 | int i; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3672 | int save_cmdline_row; |
| 3673 | int save_State; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3674 | |
| 3675 | /* When using ":silent" assume that <CR> was entered. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3676 | if (mouse_used != NULL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3677 | msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): ")); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3678 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3679 | msg_puts(_("Type number and <Enter> (empty cancels): ")); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3680 | |
Bram Moolenaar | 4cbdf15 | 2018-08-26 21:23:07 +0200 | [diff] [blame] | 3681 | // Set the state such that text can be selected/copied/pasted and we still |
| 3682 | // get mouse events. redraw_after_callback() will not redraw if cmdline_row |
| 3683 | // is zero. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3684 | save_cmdline_row = cmdline_row; |
Bram Moolenaar | 203335e | 2006-09-03 14:35:42 +0000 | [diff] [blame] | 3685 | cmdline_row = 0; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3686 | save_State = State; |
Bram Moolenaar | 4cbdf15 | 2018-08-26 21:23:07 +0200 | [diff] [blame] | 3687 | State = CMDLINE; |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3688 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 4cbdf15 | 2018-08-26 21:23:07 +0200 | [diff] [blame] | 3689 | // May show different mouse shape. |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3690 | setmouse(); |
| 3691 | #endif |
| 3692 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3693 | i = get_number(TRUE, mouse_used); |
| 3694 | if (KeyTyped) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3695 | { |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3696 | /* don't call wait_return() now */ |
| 3697 | /* msg_putchar('\n'); */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3698 | cmdline_row = msg_row - 1; |
| 3699 | need_wait_return = FALSE; |
| 3700 | msg_didany = FALSE; |
Bram Moolenaar | b245016 | 2009-07-22 09:04:20 +0000 | [diff] [blame] | 3701 | msg_didout = FALSE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3702 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3703 | else |
| 3704 | cmdline_row = save_cmdline_row; |
| 3705 | State = save_State; |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3706 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 4cbdf15 | 2018-08-26 21:23:07 +0200 | [diff] [blame] | 3707 | // May need to restore mouse shape. |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3708 | setmouse(); |
| 3709 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3710 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3711 | return i; |
| 3712 | } |
| 3713 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3714 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3715 | msgmore(long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3716 | { |
| 3717 | long pn; |
| 3718 | |
| 3719 | if (global_busy /* no messages now, wait until global is finished */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3720 | || !messaging()) /* 'lazyredraw' set, don't do messages now */ |
| 3721 | return; |
| 3722 | |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 3723 | /* We don't want to overwrite another important message, but do overwrite |
| 3724 | * a previous "more lines" or "fewer lines" message, so that "5dd" and |
| 3725 | * then "put" reports the last action. */ |
| 3726 | if (keep_msg != NULL && !keep_msg_more) |
| 3727 | return; |
| 3728 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3729 | if (n > 0) |
| 3730 | pn = n; |
| 3731 | else |
| 3732 | pn = -n; |
| 3733 | |
| 3734 | if (pn > p_report) |
| 3735 | { |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 3736 | if (n > 0) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3737 | vim_snprintf(msg_buf, MSG_BUF_LEN, |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 3738 | NGETTEXT("%ld more line", "%ld more lines", pn), pn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3739 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3740 | vim_snprintf(msg_buf, MSG_BUF_LEN, |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 3741 | NGETTEXT("%ld line less", "%ld fewer lines", pn), pn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3742 | if (got_int) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3743 | vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"), |
| 3744 | MSG_BUF_LEN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3745 | if (msg(msg_buf)) |
| 3746 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3747 | set_keep_msg((char_u *)msg_buf, 0); |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 3748 | keep_msg_more = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3749 | } |
| 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | /* |
| 3754 | * flush map and typeahead buffers and give a warning for an error |
| 3755 | */ |
| 3756 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3757 | beep_flush(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3758 | { |
| 3759 | if (emsg_silent == 0) |
| 3760 | { |
Bram Moolenaar | 6a2633b | 2018-10-07 23:16:36 +0200 | [diff] [blame] | 3761 | flush_buffers(FLUSH_MINIMAL); |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3762 | vim_beep(BO_ERROR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | /* |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3767 | * Give a warning for an error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3768 | */ |
| 3769 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3770 | vim_beep( |
| 3771 | unsigned val) /* one of the BO_ values, e.g., BO_OPER */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3772 | { |
Bram Moolenaar | b48e96f | 2018-02-13 12:26:14 +0100 | [diff] [blame] | 3773 | #ifdef FEAT_EVAL |
| 3774 | called_vim_beep = TRUE; |
| 3775 | #endif |
| 3776 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3777 | if (emsg_silent == 0) |
| 3778 | { |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3779 | if (!((bo_flags & val) || (bo_flags & BO_ALL))) |
| 3780 | { |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3781 | #ifdef ELAPSED_FUNC |
| 3782 | static int did_init = FALSE; |
Bram Moolenaar | 1ac56c2 | 2019-01-17 22:28:22 +0100 | [diff] [blame] | 3783 | static elapsed_T start_tv; |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3784 | |
| 3785 | /* Only beep once per half a second, otherwise a sequence of beeps |
| 3786 | * would freeze Vim. */ |
| 3787 | if (!did_init || ELAPSED_FUNC(start_tv) > 500) |
| 3788 | { |
| 3789 | did_init = TRUE; |
| 3790 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3791 | #endif |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3792 | if (p_vb |
| 3793 | #ifdef FEAT_GUI |
| 3794 | /* While the GUI is starting up the termcap is set for |
| 3795 | * the GUI but the output still goes to a terminal. */ |
| 3796 | && !(gui.in_use && gui.starting) |
| 3797 | #endif |
| 3798 | ) |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3799 | { |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3800 | out_str_cf(T_VB); |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3801 | #ifdef FEAT_VTP |
| 3802 | /* No restore color information, refresh the screen. */ |
| 3803 | if (has_vtp_working() != 0 |
| 3804 | # ifdef FEAT_TERMGUICOLORS |
Bram Moolenaar | c5cd885 | 2018-05-01 15:47:38 +0200 | [diff] [blame] | 3805 | && (p_tgc || (!p_tgc && t_colors >= 256)) |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3806 | # endif |
| 3807 | ) |
| 3808 | { |
| 3809 | redraw_later(CLEAR); |
| 3810 | update_screen(0); |
| 3811 | redrawcmd(); |
| 3812 | } |
| 3813 | #endif |
| 3814 | } |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3815 | else |
| 3816 | out_char(BELL); |
| 3817 | #ifdef ELAPSED_FUNC |
| 3818 | } |
| 3819 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3820 | } |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3821 | |
Bram Moolenaar | b48e96f | 2018-02-13 12:26:14 +0100 | [diff] [blame] | 3822 | /* When 'debug' contains "beep" produce a message. If we are sourcing |
| 3823 | * a script or executing a function give the user a hint where the beep |
| 3824 | * comes from. */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3825 | if (vim_strchr(p_debug, 'e') != NULL) |
| 3826 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3827 | msg_source(HL_ATTR(HLF_W)); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3828 | msg_attr(_("Beep!"), HL_ATTR(HLF_W)); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3829 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3830 | } |
| 3831 | } |
| 3832 | |
| 3833 | /* |
| 3834 | * To get the "real" home directory: |
| 3835 | * - get value of $HOME |
| 3836 | * For Unix: |
| 3837 | * - go to that directory |
| 3838 | * - do mch_dirname() to get the real name of that directory. |
| 3839 | * This also works with mounts and links. |
| 3840 | * Don't do this for MS-DOS, it will change the "current dir" for a drive. |
Bram Moolenaar | 25a494c | 2018-11-16 19:39:50 +0100 | [diff] [blame] | 3841 | * For Windows: |
| 3842 | * This code is duplicated in init_homedir() in dosinst.c. Keep in sync! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3843 | */ |
| 3844 | static char_u *homedir = NULL; |
| 3845 | |
| 3846 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3847 | init_homedir(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3848 | { |
| 3849 | char_u *var; |
| 3850 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3851 | /* In case we are called a second time (when 'encoding' changes). */ |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3852 | VIM_CLEAR(homedir); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3853 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3854 | #ifdef VMS |
| 3855 | var = mch_getenv((char_u *)"SYS$LOGIN"); |
| 3856 | #else |
| 3857 | var = mch_getenv((char_u *)"HOME"); |
| 3858 | #endif |
| 3859 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 3860 | #ifdef MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3861 | /* |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3862 | * Typically, $HOME is not defined on Windows, unless the user has |
| 3863 | * specifically defined it for Vim's sake. However, on Windows NT |
| 3864 | * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for |
| 3865 | * each user. Try constructing $HOME from these. |
| 3866 | */ |
Bram Moolenaar | b47a259 | 2017-08-30 13:22:28 +0200 | [diff] [blame] | 3867 | if (var == NULL || *var == NUL) |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3868 | { |
| 3869 | char_u *homedrive, *homepath; |
| 3870 | |
| 3871 | homedrive = mch_getenv((char_u *)"HOMEDRIVE"); |
| 3872 | homepath = mch_getenv((char_u *)"HOMEPATH"); |
| 3873 | if (homepath == NULL || *homepath == NUL) |
| 3874 | homepath = (char_u *)"\\"; |
| 3875 | if (homedrive != NULL |
| 3876 | && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL) |
| 3877 | { |
| 3878 | sprintf((char *)NameBuff, "%s%s", homedrive, homepath); |
| 3879 | if (NameBuff[0] != NUL) |
| 3880 | var = NameBuff; |
| 3881 | } |
| 3882 | } |
| 3883 | |
| 3884 | if (var == NULL) |
| 3885 | var = mch_getenv((char_u *)"USERPROFILE"); |
| 3886 | |
| 3887 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3888 | * Weird but true: $HOME may contain an indirect reference to another |
| 3889 | * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set |
| 3890 | * when $HOME is being set. |
| 3891 | */ |
| 3892 | if (var != NULL && *var == '%') |
| 3893 | { |
| 3894 | char_u *p; |
| 3895 | char_u *exp; |
| 3896 | |
| 3897 | p = vim_strchr(var + 1, '%'); |
| 3898 | if (p != NULL) |
| 3899 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3900 | vim_strncpy(NameBuff, var + 1, p - (var + 1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3901 | exp = mch_getenv(NameBuff); |
| 3902 | if (exp != NULL && *exp != NUL |
| 3903 | && STRLEN(exp) + STRLEN(p) < MAXPATHL) |
| 3904 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 3905 | vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3906 | var = NameBuff; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3907 | } |
| 3908 | } |
| 3909 | } |
| 3910 | |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3911 | if (var != NULL && *var == NUL) /* empty is same as not set */ |
| 3912 | var = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3913 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3914 | if (enc_utf8 && var != NULL) |
| 3915 | { |
| 3916 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 3917 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3918 | |
| 3919 | /* Convert from active codepage to UTF-8. Other conversions are |
| 3920 | * not done, because they would fail for non-ASCII characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3921 | acp_to_enc(var, (int)STRLEN(var), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3922 | if (pp != NULL) |
| 3923 | { |
| 3924 | homedir = pp; |
| 3925 | return; |
| 3926 | } |
| 3927 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3928 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3929 | /* |
| 3930 | * Default home dir is C:/ |
| 3931 | * Best assumption we can make in such a situation. |
| 3932 | */ |
| 3933 | if (var == NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3934 | var = (char_u *)"C:/"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3935 | #endif |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3936 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3937 | if (var != NULL) |
| 3938 | { |
| 3939 | #ifdef UNIX |
| 3940 | /* |
| 3941 | * Change to the directory and get the actual path. This resolves |
| 3942 | * links. Don't do it when we can't return. |
| 3943 | */ |
| 3944 | if (mch_dirname(NameBuff, MAXPATHL) == OK |
| 3945 | && mch_chdir((char *)NameBuff) == 0) |
| 3946 | { |
| 3947 | if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK) |
| 3948 | var = IObuff; |
| 3949 | if (mch_chdir((char *)NameBuff) != 0) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3950 | emsg(_(e_prev_dir)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3951 | } |
| 3952 | #endif |
| 3953 | homedir = vim_strsave(var); |
| 3954 | } |
| 3955 | } |
| 3956 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3957 | #if defined(EXITFREE) || defined(PROTO) |
| 3958 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3959 | free_homedir(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3960 | { |
| 3961 | vim_free(homedir); |
| 3962 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 3963 | |
| 3964 | # ifdef FEAT_CMDL_COMPL |
| 3965 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3966 | free_users(void) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 3967 | { |
| 3968 | ga_clear_strings(&ga_users); |
| 3969 | } |
| 3970 | # endif |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3971 | #endif |
| 3972 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3973 | /* |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3974 | * Call expand_env() and store the result in an allocated string. |
| 3975 | * This is not very memory efficient, this expects the result to be freed |
| 3976 | * again soon. |
| 3977 | */ |
| 3978 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3979 | expand_env_save(char_u *src) |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3980 | { |
| 3981 | return expand_env_save_opt(src, FALSE); |
| 3982 | } |
| 3983 | |
| 3984 | /* |
| 3985 | * Idem, but when "one" is TRUE handle the string as one file name, only |
| 3986 | * expand "~" at the start. |
| 3987 | */ |
| 3988 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3989 | expand_env_save_opt(char_u *src, int one) |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3990 | { |
| 3991 | char_u *p; |
| 3992 | |
| 3993 | p = alloc(MAXPATHL); |
| 3994 | if (p != NULL) |
| 3995 | expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL); |
| 3996 | return p; |
| 3997 | } |
| 3998 | |
| 3999 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4000 | * Expand environment variable with path name. |
| 4001 | * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded. |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4002 | * Skips over "\ ", "\~" and "\$" (not for Win32 though). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4003 | * If anything fails no expansion is done and dst equals src. |
| 4004 | */ |
| 4005 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4006 | expand_env( |
| 4007 | char_u *src, /* input string e.g. "$HOME/vim.hlp" */ |
| 4008 | char_u *dst, /* where to put the result */ |
| 4009 | int dstlen) /* maximum length of the result */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4010 | { |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4011 | expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
| 4014 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4015 | expand_env_esc( |
| 4016 | char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */ |
| 4017 | char_u *dst, /* where to put the result */ |
| 4018 | int dstlen, /* maximum length of the result */ |
| 4019 | int esc, /* escape spaces in expanded variables */ |
| 4020 | int one, /* "srcp" is one file name */ |
| 4021 | char_u *startstr) /* start again after this (can be NULL) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4022 | { |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4023 | char_u *src; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4024 | char_u *tail; |
| 4025 | int c; |
| 4026 | char_u *var; |
| 4027 | int copy_char; |
| 4028 | int mustfree; /* var was allocated, need to free it later */ |
| 4029 | int at_start = TRUE; /* at start of a name */ |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4030 | int startstr_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4031 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4032 | if (startstr != NULL) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4033 | startstr_len = (int)STRLEN(startstr); |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4034 | |
| 4035 | src = skipwhite(srcp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4036 | --dstlen; /* leave one char space for "\," */ |
| 4037 | while (*src && dstlen > 0) |
| 4038 | { |
Bram Moolenaar | be83b73 | 2015-08-25 14:21:19 +0200 | [diff] [blame] | 4039 | #ifdef FEAT_EVAL |
| 4040 | /* Skip over `=expr`. */ |
| 4041 | if (src[0] == '`' && src[1] == '=') |
| 4042 | { |
| 4043 | size_t len; |
| 4044 | |
| 4045 | var = src; |
| 4046 | src += 2; |
| 4047 | (void)skip_expr(&src); |
| 4048 | if (*src == '`') |
| 4049 | ++src; |
| 4050 | len = src - var; |
| 4051 | if (len > (size_t)dstlen) |
| 4052 | len = dstlen; |
| 4053 | vim_strncpy(dst, var, len); |
| 4054 | dst += len; |
Bram Moolenaar | 5df1ed2 | 2015-09-01 16:25:34 +0200 | [diff] [blame] | 4055 | dstlen -= (int)len; |
Bram Moolenaar | be83b73 | 2015-08-25 14:21:19 +0200 | [diff] [blame] | 4056 | continue; |
| 4057 | } |
| 4058 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4059 | copy_char = TRUE; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4060 | if ((*src == '$' |
| 4061 | #ifdef VMS |
| 4062 | && at_start |
| 4063 | #endif |
| 4064 | ) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4065 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4066 | || *src == '%' |
| 4067 | #endif |
| 4068 | || (*src == '~' && at_start)) |
| 4069 | { |
| 4070 | mustfree = FALSE; |
| 4071 | |
| 4072 | /* |
| 4073 | * The variable name is copied into dst temporarily, because it may |
| 4074 | * be a string in read-only memory and a NUL needs to be appended. |
| 4075 | */ |
| 4076 | if (*src != '~') /* environment var */ |
| 4077 | { |
| 4078 | tail = src + 1; |
| 4079 | var = dst; |
| 4080 | c = dstlen - 1; |
| 4081 | |
| 4082 | #ifdef UNIX |
| 4083 | /* Unix has ${var-name} type environment vars */ |
| 4084 | if (*tail == '{' && !vim_isIDc('{')) |
| 4085 | { |
| 4086 | tail++; /* ignore '{' */ |
| 4087 | while (c-- > 0 && *tail && *tail != '}') |
| 4088 | *var++ = *tail++; |
| 4089 | } |
| 4090 | else |
| 4091 | #endif |
| 4092 | { |
| 4093 | while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail)) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4094 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4095 | || (*src == '%' && *tail != '%') |
| 4096 | #endif |
| 4097 | )) |
| 4098 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4099 | *var++ = *tail++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4100 | } |
| 4101 | } |
| 4102 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4103 | #if defined(MSWIN) || defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4104 | # ifdef UNIX |
| 4105 | if (src[1] == '{' && *tail != '}') |
| 4106 | # else |
| 4107 | if (*src == '%' && *tail != '%') |
| 4108 | # endif |
| 4109 | var = NULL; |
| 4110 | else |
| 4111 | { |
| 4112 | # ifdef UNIX |
| 4113 | if (src[1] == '{') |
| 4114 | # else |
| 4115 | if (*src == '%') |
| 4116 | #endif |
| 4117 | ++tail; |
| 4118 | #endif |
| 4119 | *var = NUL; |
| 4120 | var = vim_getenv(dst, &mustfree); |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4121 | #if defined(MSWIN) || defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4122 | } |
| 4123 | #endif |
| 4124 | } |
| 4125 | /* home directory */ |
| 4126 | else if ( src[1] == NUL |
| 4127 | || vim_ispathsep(src[1]) |
| 4128 | || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL) |
| 4129 | { |
| 4130 | var = homedir; |
| 4131 | tail = src + 1; |
| 4132 | } |
| 4133 | else /* user directory */ |
| 4134 | { |
| 4135 | #if defined(UNIX) || (defined(VMS) && defined(USER_HOME)) |
| 4136 | /* |
| 4137 | * Copy ~user to dst[], so we can put a NUL after it. |
| 4138 | */ |
| 4139 | tail = src; |
| 4140 | var = dst; |
| 4141 | c = dstlen - 1; |
| 4142 | while ( c-- > 0 |
| 4143 | && *tail |
| 4144 | && vim_isfilec(*tail) |
| 4145 | && !vim_ispathsep(*tail)) |
| 4146 | *var++ = *tail++; |
| 4147 | *var = NUL; |
| 4148 | # ifdef UNIX |
| 4149 | /* |
| 4150 | * If the system supports getpwnam(), use it. |
| 4151 | * Otherwise, or if getpwnam() fails, the shell is used to |
| 4152 | * expand ~user. This is slower and may fail if the shell |
| 4153 | * does not support ~user (old versions of /bin/sh). |
| 4154 | */ |
| 4155 | # if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H) |
| 4156 | { |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 4157 | /* Note: memory allocated by getpwnam() is never freed. |
| 4158 | * Calling endpwent() apparently doesn't help. */ |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 4159 | struct passwd *pw = (*dst == NUL) |
| 4160 | ? NULL : getpwnam((char *)dst + 1); |
| 4161 | |
| 4162 | var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4163 | } |
| 4164 | if (var == NULL) |
| 4165 | # endif |
| 4166 | { |
| 4167 | expand_T xpc; |
| 4168 | |
| 4169 | ExpandInit(&xpc); |
| 4170 | xpc.xp_context = EXPAND_FILES; |
| 4171 | var = ExpandOne(&xpc, dst, NULL, |
| 4172 | WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4173 | mustfree = TRUE; |
| 4174 | } |
| 4175 | |
| 4176 | # else /* !UNIX, thus VMS */ |
| 4177 | /* |
| 4178 | * USER_HOME is a comma-separated list of |
| 4179 | * directories to search for the user account in. |
| 4180 | */ |
| 4181 | { |
| 4182 | char_u test[MAXPATHL], paths[MAXPATHL]; |
| 4183 | char_u *path, *next_path, *ptr; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4184 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4185 | |
| 4186 | STRCPY(paths, USER_HOME); |
| 4187 | next_path = paths; |
| 4188 | while (*next_path) |
| 4189 | { |
| 4190 | for (path = next_path; *next_path && *next_path != ','; |
| 4191 | next_path++); |
| 4192 | if (*next_path) |
| 4193 | *next_path++ = NUL; |
| 4194 | STRCPY(test, path); |
| 4195 | STRCAT(test, "/"); |
| 4196 | STRCAT(test, dst + 1); |
| 4197 | if (mch_stat(test, &st) == 0) |
| 4198 | { |
| 4199 | var = alloc(STRLEN(test) + 1); |
| 4200 | STRCPY(var, test); |
| 4201 | mustfree = TRUE; |
| 4202 | break; |
| 4203 | } |
| 4204 | } |
| 4205 | } |
| 4206 | # endif /* UNIX */ |
| 4207 | #else |
| 4208 | /* cannot expand user's home directory, so don't try */ |
| 4209 | var = NULL; |
| 4210 | tail = (char_u *)""; /* for gcc */ |
| 4211 | #endif /* UNIX || VMS */ |
| 4212 | } |
| 4213 | |
| 4214 | #ifdef BACKSLASH_IN_FILENAME |
| 4215 | /* If 'shellslash' is set change backslashes to forward slashes. |
| 4216 | * Can't use slash_adjust(), p_ssl may be set temporarily. */ |
| 4217 | if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL) |
| 4218 | { |
| 4219 | char_u *p = vim_strsave(var); |
| 4220 | |
| 4221 | if (p != NULL) |
| 4222 | { |
| 4223 | if (mustfree) |
| 4224 | vim_free(var); |
| 4225 | var = p; |
| 4226 | mustfree = TRUE; |
| 4227 | forward_slash(var); |
| 4228 | } |
| 4229 | } |
| 4230 | #endif |
| 4231 | |
| 4232 | /* If "var" contains white space, escape it with a backslash. |
| 4233 | * Required for ":e ~/tt" when $HOME includes a space. */ |
| 4234 | if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) |
| 4235 | { |
| 4236 | char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); |
| 4237 | |
| 4238 | if (p != NULL) |
| 4239 | { |
| 4240 | if (mustfree) |
| 4241 | vim_free(var); |
| 4242 | var = p; |
| 4243 | mustfree = TRUE; |
| 4244 | } |
| 4245 | } |
| 4246 | |
| 4247 | if (var != NULL && *var != NUL |
| 4248 | && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) |
| 4249 | { |
| 4250 | STRCPY(dst, var); |
| 4251 | dstlen -= (int)STRLEN(var); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4252 | c = (int)STRLEN(var); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4253 | /* if var[] ends in a path separator and tail[] starts |
| 4254 | * with it, skip a character */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4255 | if (*var != NUL && after_pathsep(dst, dst + c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4256 | #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) |
| 4257 | && dst[-1] != ':' |
| 4258 | #endif |
| 4259 | && vim_ispathsep(*tail)) |
| 4260 | ++tail; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4261 | dst += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4262 | src = tail; |
| 4263 | copy_char = FALSE; |
| 4264 | } |
| 4265 | if (mustfree) |
| 4266 | vim_free(var); |
| 4267 | } |
| 4268 | |
| 4269 | if (copy_char) /* copy at least one char */ |
| 4270 | { |
| 4271 | /* |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 4272 | * Recognize the start of a new name, for '~'. |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4273 | * Don't do this when "one" is TRUE, to avoid expanding "~" in |
| 4274 | * ":edit foo ~ foo". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4275 | */ |
| 4276 | at_start = FALSE; |
| 4277 | if (src[0] == '\\' && src[1] != NUL) |
| 4278 | { |
| 4279 | *dst++ = *src++; |
| 4280 | --dstlen; |
| 4281 | } |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4282 | else if ((src[0] == ' ' || src[0] == ',') && !one) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4283 | at_start = TRUE; |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4284 | if (dstlen > 0) |
| 4285 | { |
| 4286 | *dst++ = *src++; |
| 4287 | --dstlen; |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4288 | |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4289 | if (startstr != NULL && src - startstr_len >= srcp |
| 4290 | && STRNCMP(src - startstr_len, startstr, |
| 4291 | startstr_len) == 0) |
| 4292 | at_start = TRUE; |
| 4293 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4294 | } |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4295 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4296 | } |
| 4297 | *dst = NUL; |
| 4298 | } |
| 4299 | |
| 4300 | /* |
| 4301 | * Vim's version of getenv(). |
| 4302 | * Special handling of $HOME, $VIM and $VIMRUNTIME. |
Bram Moolenaar | 2f6b0b8 | 2005-03-08 22:43:10 +0000 | [diff] [blame] | 4303 | * Also does ACP to 'enc' conversion for Win32. |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4304 | * "mustfree" is set to TRUE when returned is allocated, it must be |
| 4305 | * initialized to FALSE by the caller. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4306 | */ |
| 4307 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4308 | vim_getenv(char_u *name, int *mustfree) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4309 | { |
| 4310 | char_u *p; |
| 4311 | char_u *pend; |
| 4312 | int vimruntime; |
| 4313 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4314 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4315 | /* use "C:/" when $HOME is not set */ |
| 4316 | if (STRCMP(name, "HOME") == 0) |
| 4317 | return homedir; |
| 4318 | #endif |
| 4319 | |
| 4320 | p = mch_getenv(name); |
| 4321 | if (p != NULL && *p == NUL) /* empty is the same as not set */ |
| 4322 | p = NULL; |
| 4323 | |
| 4324 | if (p != NULL) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4325 | { |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 4326 | #if defined(MSWIN) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4327 | if (enc_utf8) |
| 4328 | { |
| 4329 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4330 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4331 | |
| 4332 | /* Convert from active codepage to UTF-8. Other conversions are |
| 4333 | * not done, because they would fail for non-ASCII characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4334 | acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4335 | if (pp != NULL) |
| 4336 | { |
| 4337 | p = pp; |
| 4338 | *mustfree = TRUE; |
| 4339 | } |
| 4340 | } |
| 4341 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4342 | return p; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4343 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4344 | |
| 4345 | vimruntime = (STRCMP(name, "VIMRUNTIME") == 0); |
| 4346 | if (!vimruntime && STRCMP(name, "VIM") != 0) |
| 4347 | return NULL; |
| 4348 | |
| 4349 | /* |
| 4350 | * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM. |
| 4351 | * Don't do this when default_vimruntime_dir is non-empty. |
| 4352 | */ |
| 4353 | if (vimruntime |
| 4354 | #ifdef HAVE_PATHDEF |
| 4355 | && *default_vimruntime_dir == NUL |
| 4356 | #endif |
| 4357 | ) |
| 4358 | { |
| 4359 | p = mch_getenv((char_u *)"VIM"); |
| 4360 | if (p != NULL && *p == NUL) /* empty is the same as not set */ |
| 4361 | p = NULL; |
| 4362 | if (p != NULL) |
| 4363 | { |
| 4364 | p = vim_version_dir(p); |
| 4365 | if (p != NULL) |
| 4366 | *mustfree = TRUE; |
| 4367 | else |
| 4368 | p = mch_getenv((char_u *)"VIM"); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4369 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 4370 | #if defined(MSWIN) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4371 | if (enc_utf8) |
| 4372 | { |
| 4373 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4374 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4375 | |
| 4376 | /* Convert from active codepage to UTF-8. Other conversions |
| 4377 | * are not done, because they would fail for non-ASCII |
| 4378 | * characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4379 | acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4380 | if (pp != NULL) |
| 4381 | { |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4382 | if (*mustfree) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4383 | vim_free(p); |
| 4384 | p = pp; |
| 4385 | *mustfree = TRUE; |
| 4386 | } |
| 4387 | } |
| 4388 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4389 | } |
| 4390 | } |
| 4391 | |
| 4392 | /* |
| 4393 | * When expanding $VIM or $VIMRUNTIME fails, try using: |
| 4394 | * - the directory name from 'helpfile' (unless it contains '$') |
| 4395 | * - the executable name from argv[0] |
| 4396 | */ |
| 4397 | if (p == NULL) |
| 4398 | { |
| 4399 | if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL) |
| 4400 | p = p_hf; |
| 4401 | #ifdef USE_EXE_NAME |
| 4402 | /* |
| 4403 | * Use the name of the executable, obtained from argv[0]. |
| 4404 | */ |
| 4405 | else |
| 4406 | p = exe_name; |
| 4407 | #endif |
| 4408 | if (p != NULL) |
| 4409 | { |
| 4410 | /* remove the file name */ |
| 4411 | pend = gettail(p); |
| 4412 | |
| 4413 | /* remove "doc/" from 'helpfile', if present */ |
| 4414 | if (p == p_hf) |
| 4415 | pend = remove_tail(p, pend, (char_u *)"doc"); |
| 4416 | |
| 4417 | #ifdef USE_EXE_NAME |
| 4418 | # ifdef MACOS_X |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4419 | /* remove "MacOS" from exe_name and add "Resources/vim" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4420 | if (p == exe_name) |
| 4421 | { |
| 4422 | char_u *pend1; |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4423 | char_u *pnew; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4424 | |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4425 | pend1 = remove_tail(p, pend, (char_u *)"MacOS"); |
| 4426 | if (pend1 != pend) |
| 4427 | { |
| 4428 | pnew = alloc((unsigned)(pend1 - p) + 15); |
| 4429 | if (pnew != NULL) |
| 4430 | { |
| 4431 | STRNCPY(pnew, p, (pend1 - p)); |
| 4432 | STRCPY(pnew + (pend1 - p), "Resources/vim"); |
| 4433 | p = pnew; |
| 4434 | pend = p + STRLEN(p); |
| 4435 | } |
| 4436 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4437 | } |
| 4438 | # endif |
| 4439 | /* remove "src/" from exe_name, if present */ |
| 4440 | if (p == exe_name) |
| 4441 | pend = remove_tail(p, pend, (char_u *)"src"); |
| 4442 | #endif |
| 4443 | |
| 4444 | /* for $VIM, remove "runtime/" or "vim54/", if present */ |
| 4445 | if (!vimruntime) |
| 4446 | { |
| 4447 | pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME); |
| 4448 | pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT); |
| 4449 | } |
| 4450 | |
| 4451 | /* remove trailing path separator */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4452 | if (pend > p && after_pathsep(p, pend)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4453 | --pend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4454 | |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4455 | #ifdef MACOS_X |
| 4456 | if (p == exe_name || p == p_hf) |
| 4457 | #endif |
| 4458 | /* check that the result is a directory name */ |
| 4459 | p = vim_strnsave(p, (int)(pend - p)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4460 | |
| 4461 | if (p != NULL && !mch_isdir(p)) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 4462 | VIM_CLEAR(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4463 | else |
| 4464 | { |
| 4465 | #ifdef USE_EXE_NAME |
| 4466 | /* may add "/vim54" or "/runtime" if it exists */ |
| 4467 | if (vimruntime && (pend = vim_version_dir(p)) != NULL) |
| 4468 | { |
| 4469 | vim_free(p); |
| 4470 | p = pend; |
| 4471 | } |
| 4472 | #endif |
| 4473 | *mustfree = TRUE; |
| 4474 | } |
| 4475 | } |
| 4476 | } |
| 4477 | |
| 4478 | #ifdef HAVE_PATHDEF |
| 4479 | /* When there is a pathdef.c file we can use default_vim_dir and |
| 4480 | * default_vimruntime_dir */ |
| 4481 | if (p == NULL) |
| 4482 | { |
| 4483 | /* Only use default_vimruntime_dir when it is not empty */ |
| 4484 | if (vimruntime && *default_vimruntime_dir != NUL) |
| 4485 | { |
| 4486 | p = default_vimruntime_dir; |
| 4487 | *mustfree = FALSE; |
| 4488 | } |
| 4489 | else if (*default_vim_dir != NUL) |
| 4490 | { |
| 4491 | if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) |
| 4492 | *mustfree = TRUE; |
| 4493 | else |
| 4494 | { |
| 4495 | p = default_vim_dir; |
| 4496 | *mustfree = FALSE; |
| 4497 | } |
| 4498 | } |
| 4499 | } |
| 4500 | #endif |
| 4501 | |
| 4502 | /* |
| 4503 | * Set the environment variable, so that the new value can be found fast |
| 4504 | * next time, and others can also use it (e.g. Perl). |
| 4505 | */ |
| 4506 | if (p != NULL) |
| 4507 | { |
| 4508 | if (vimruntime) |
| 4509 | { |
| 4510 | vim_setenv((char_u *)"VIMRUNTIME", p); |
| 4511 | didset_vimruntime = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4512 | } |
| 4513 | else |
| 4514 | { |
| 4515 | vim_setenv((char_u *)"VIM", p); |
| 4516 | didset_vim = TRUE; |
| 4517 | } |
| 4518 | } |
| 4519 | return p; |
| 4520 | } |
| 4521 | |
| 4522 | /* |
| 4523 | * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. |
| 4524 | * Return NULL if not, return its name in allocated memory otherwise. |
| 4525 | */ |
| 4526 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4527 | vim_version_dir(char_u *vimdir) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4528 | { |
| 4529 | char_u *p; |
| 4530 | |
| 4531 | if (vimdir == NULL || *vimdir == NUL) |
| 4532 | return NULL; |
| 4533 | p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); |
| 4534 | if (p != NULL && mch_isdir(p)) |
| 4535 | return p; |
| 4536 | vim_free(p); |
| 4537 | p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); |
| 4538 | if (p != NULL && mch_isdir(p)) |
| 4539 | return p; |
| 4540 | vim_free(p); |
| 4541 | return NULL; |
| 4542 | } |
| 4543 | |
| 4544 | /* |
| 4545 | * If the string between "p" and "pend" ends in "name/", return "pend" minus |
| 4546 | * the length of "name/". Otherwise return "pend". |
| 4547 | */ |
| 4548 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4549 | remove_tail(char_u *p, char_u *pend, char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4550 | { |
| 4551 | int len = (int)STRLEN(name) + 1; |
| 4552 | char_u *newend = pend - len; |
| 4553 | |
| 4554 | if (newend >= p |
| 4555 | && fnamencmp(newend, name, len - 1) == 0 |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4556 | && (newend == p || after_pathsep(p, newend))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4557 | return newend; |
| 4558 | return pend; |
| 4559 | } |
| 4560 | |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 4561 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 137374f | 2018-05-13 15:59:50 +0200 | [diff] [blame] | 4562 | void |
| 4563 | vim_unsetenv(char_u *var) |
| 4564 | { |
| 4565 | #ifdef HAVE_UNSETENV |
| 4566 | unsetenv((char *)var); |
| 4567 | #else |
Bram Moolenaar | 1af6a4b | 2018-05-14 22:58:34 +0200 | [diff] [blame] | 4568 | vim_setenv(var, (char_u *)""); |
Bram Moolenaar | 137374f | 2018-05-13 15:59:50 +0200 | [diff] [blame] | 4569 | #endif |
| 4570 | } |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 4571 | #endif |
Bram Moolenaar | 137374f | 2018-05-13 15:59:50 +0200 | [diff] [blame] | 4572 | |
| 4573 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4574 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4575 | * Our portable version of setenv. |
| 4576 | */ |
| 4577 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4578 | vim_setenv(char_u *name, char_u *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4579 | { |
| 4580 | #ifdef HAVE_SETENV |
| 4581 | mch_setenv((char *)name, (char *)val, 1); |
| 4582 | #else |
| 4583 | char_u *envbuf; |
| 4584 | |
| 4585 | /* |
| 4586 | * Putenv does not copy the string, it has to remain |
| 4587 | * valid. The allocated memory will never be freed. |
| 4588 | */ |
| 4589 | envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2)); |
| 4590 | if (envbuf != NULL) |
| 4591 | { |
| 4592 | sprintf((char *)envbuf, "%s=%s", name, val); |
| 4593 | putenv((char *)envbuf); |
| 4594 | } |
| 4595 | #endif |
Bram Moolenaar | 011a34d | 2012-02-29 13:49:09 +0100 | [diff] [blame] | 4596 | #ifdef FEAT_GETTEXT |
| 4597 | /* |
| 4598 | * When setting $VIMRUNTIME adjust the directory to find message |
| 4599 | * translations to $VIMRUNTIME/lang. |
| 4600 | */ |
| 4601 | if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) |
| 4602 | { |
| 4603 | char_u *buf = concat_str(val, (char_u *)"/lang"); |
| 4604 | |
| 4605 | if (buf != NULL) |
| 4606 | { |
| 4607 | bindtextdomain(VIMPACKAGE, (char *)buf); |
| 4608 | vim_free(buf); |
| 4609 | } |
| 4610 | } |
| 4611 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4612 | } |
| 4613 | |
| 4614 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 4615 | /* |
| 4616 | * Function given to ExpandGeneric() to obtain an environment variable name. |
| 4617 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4618 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4619 | get_env_name( |
| 4620 | expand_T *xp UNUSED, |
| 4621 | int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4622 | { |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 4623 | # if defined(AMIGA) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4624 | /* |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 4625 | * No environ[] on the Amiga. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4626 | */ |
| 4627 | return NULL; |
| 4628 | # else |
| 4629 | # ifndef __WIN32__ |
| 4630 | /* Borland C++ 5.2 has this in a header file. */ |
| 4631 | extern char **environ; |
| 4632 | # endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 4633 | # define ENVNAMELEN 100 |
| 4634 | static char_u name[ENVNAMELEN]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4635 | char_u *str; |
| 4636 | int n; |
| 4637 | |
| 4638 | str = (char_u *)environ[idx]; |
| 4639 | if (str == NULL) |
| 4640 | return NULL; |
| 4641 | |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 4642 | for (n = 0; n < ENVNAMELEN - 1; ++n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4643 | { |
| 4644 | if (str[n] == '=' || str[n] == NUL) |
| 4645 | break; |
| 4646 | name[n] = str[n]; |
| 4647 | } |
| 4648 | name[n] = NUL; |
| 4649 | return name; |
| 4650 | # endif |
| 4651 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4652 | |
| 4653 | /* |
Bram Moolenaar | 6b0b83f | 2018-09-10 19:03:05 +0200 | [diff] [blame] | 4654 | * Add a user name to the list of users in ga_users. |
| 4655 | * Do nothing if user name is NULL or empty. |
| 4656 | */ |
| 4657 | static void |
| 4658 | add_user(char_u *user, int need_copy) |
| 4659 | { |
| 4660 | char_u *user_copy = (user != NULL && need_copy) |
| 4661 | ? vim_strsave(user) : user; |
| 4662 | |
| 4663 | if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL) |
| 4664 | { |
| 4665 | if (need_copy) |
| 4666 | vim_free(user); |
| 4667 | return; |
| 4668 | } |
| 4669 | ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy; |
| 4670 | } |
| 4671 | |
| 4672 | /* |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4673 | * Find all user names for user completion. |
| 4674 | * Done only once and then cached. |
| 4675 | */ |
| 4676 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4677 | init_users(void) |
Bram Moolenaar | 01b626c | 2013-06-16 22:49:14 +0200 | [diff] [blame] | 4678 | { |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4679 | static int lazy_init_done = FALSE; |
| 4680 | |
| 4681 | if (lazy_init_done) |
| 4682 | return; |
| 4683 | |
| 4684 | lazy_init_done = TRUE; |
| 4685 | ga_init2(&ga_users, sizeof(char_u *), 20); |
| 4686 | |
| 4687 | # if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H) |
| 4688 | { |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4689 | struct passwd* pw; |
| 4690 | |
| 4691 | setpwent(); |
| 4692 | while ((pw = getpwent()) != NULL) |
Bram Moolenaar | 6b0b83f | 2018-09-10 19:03:05 +0200 | [diff] [blame] | 4693 | add_user((char_u *)pw->pw_name, TRUE); |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4694 | endpwent(); |
| 4695 | } |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 4696 | # elif defined(MSWIN) |
Bram Moolenaar | 828c3d7 | 2018-06-19 18:58:07 +0200 | [diff] [blame] | 4697 | { |
Bram Moolenaar | 828c3d7 | 2018-06-19 18:58:07 +0200 | [diff] [blame] | 4698 | DWORD nusers = 0, ntotal = 0, i; |
| 4699 | PUSER_INFO_0 uinfo; |
| 4700 | |
| 4701 | if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH, |
| 4702 | &nusers, &ntotal, NULL) == NERR_Success) |
| 4703 | { |
| 4704 | for (i = 0; i < nusers; i++) |
Bram Moolenaar | 6b0b83f | 2018-09-10 19:03:05 +0200 | [diff] [blame] | 4705 | add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE); |
Bram Moolenaar | 828c3d7 | 2018-06-19 18:58:07 +0200 | [diff] [blame] | 4706 | |
| 4707 | NetApiBufferFree(uinfo); |
| 4708 | } |
| 4709 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4710 | # endif |
Bram Moolenaar | 6b0b83f | 2018-09-10 19:03:05 +0200 | [diff] [blame] | 4711 | # if defined(HAVE_GETPWNAM) |
| 4712 | { |
| 4713 | char_u *user_env = mch_getenv((char_u *)"USER"); |
| 4714 | |
| 4715 | // The $USER environment variable may be a valid remote user name (NIS, |
| 4716 | // LDAP) not already listed by getpwent(), as getpwent() only lists |
| 4717 | // local user names. If $USER is not already listed, check whether it |
| 4718 | // is a valid remote user name using getpwnam() and if it is, add it to |
| 4719 | // the list of user names. |
| 4720 | |
| 4721 | if (user_env != NULL && *user_env != NUL) |
| 4722 | { |
| 4723 | int i; |
| 4724 | |
| 4725 | for (i = 0; i < ga_users.ga_len; i++) |
| 4726 | { |
| 4727 | char_u *local_user = ((char_u **)ga_users.ga_data)[i]; |
| 4728 | |
| 4729 | if (STRCMP(local_user, user_env) == 0) |
| 4730 | break; |
| 4731 | } |
| 4732 | |
| 4733 | if (i == ga_users.ga_len) |
| 4734 | { |
| 4735 | struct passwd *pw = getpwnam((char *)user_env); |
| 4736 | |
| 4737 | if (pw != NULL) |
| 4738 | add_user((char_u *)pw->pw_name, TRUE); |
| 4739 | } |
| 4740 | } |
| 4741 | } |
| 4742 | # endif |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4743 | } |
| 4744 | |
| 4745 | /* |
| 4746 | * Function given to ExpandGeneric() to obtain an user names. |
| 4747 | */ |
| 4748 | char_u* |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4749 | get_users(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4750 | { |
| 4751 | init_users(); |
| 4752 | if (idx < ga_users.ga_len) |
| 4753 | return ((char_u **)ga_users.ga_data)[idx]; |
| 4754 | return NULL; |
| 4755 | } |
| 4756 | |
| 4757 | /* |
| 4758 | * Check whether name matches a user name. Return: |
| 4759 | * 0 if name does not match any user name. |
| 4760 | * 1 if name partially matches the beginning of a user name. |
| 4761 | * 2 is name fully matches a user name. |
| 4762 | */ |
Bram Moolenaar | 6c5d104 | 2018-07-07 16:41:13 +0200 | [diff] [blame] | 4763 | int |
| 4764 | match_user(char_u *name) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4765 | { |
| 4766 | int i; |
| 4767 | int n = (int)STRLEN(name); |
| 4768 | int result = 0; |
| 4769 | |
| 4770 | init_users(); |
| 4771 | for (i = 0; i < ga_users.ga_len; i++) |
| 4772 | { |
| 4773 | if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0) |
| 4774 | return 2; /* full match */ |
| 4775 | if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0) |
| 4776 | result = 1; /* partial match */ |
| 4777 | } |
| 4778 | return result; |
| 4779 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4780 | #endif |
| 4781 | |
| 4782 | /* |
| 4783 | * Replace home directory by "~" in each space or comma separated file name in |
| 4784 | * 'src'. |
| 4785 | * If anything fails (except when out of space) dst equals src. |
| 4786 | */ |
| 4787 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4788 | home_replace( |
| 4789 | buf_T *buf, /* when not NULL, check for help files */ |
| 4790 | char_u *src, /* input file name */ |
| 4791 | char_u *dst, /* where to put the result */ |
| 4792 | int dstlen, /* maximum length of the result */ |
| 4793 | int one) /* if TRUE, only replace one file name, include |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4794 | spaces and commas in the file name. */ |
| 4795 | { |
| 4796 | size_t dirlen = 0, envlen = 0; |
| 4797 | size_t len; |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4798 | char_u *homedir_env, *homedir_env_orig; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4799 | char_u *p; |
| 4800 | |
| 4801 | if (src == NULL) |
| 4802 | { |
| 4803 | *dst = NUL; |
| 4804 | return; |
| 4805 | } |
| 4806 | |
| 4807 | /* |
| 4808 | * If the file is a help file, remove the path completely. |
| 4809 | */ |
| 4810 | if (buf != NULL && buf->b_help) |
| 4811 | { |
Bram Moolenaar | 0af2d32 | 2017-08-05 23:00:53 +0200 | [diff] [blame] | 4812 | vim_snprintf((char *)dst, dstlen, "%s", gettail(src)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4813 | return; |
| 4814 | } |
| 4815 | |
| 4816 | /* |
| 4817 | * We check both the value of the $HOME environment variable and the |
| 4818 | * "real" home directory. |
| 4819 | */ |
| 4820 | if (homedir != NULL) |
| 4821 | dirlen = STRLEN(homedir); |
| 4822 | |
| 4823 | #ifdef VMS |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4824 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4825 | #else |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4826 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); |
| 4827 | #endif |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 4828 | #ifdef MSWIN |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 4829 | if (homedir_env == NULL) |
| 4830 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE"); |
| 4831 | #endif |
Bram Moolenaar | bef4790 | 2012-07-06 16:49:40 +0200 | [diff] [blame] | 4832 | /* Empty is the same as not set. */ |
| 4833 | if (homedir_env != NULL && *homedir_env == NUL) |
| 4834 | homedir_env = NULL; |
| 4835 | |
Bram Moolenaar | e60c2e5 | 2013-06-05 19:35:38 +0200 | [diff] [blame] | 4836 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) |
Bram Moolenaar | ef0a1d5 | 2018-12-30 11:38:57 +0100 | [diff] [blame] | 4837 | if (homedir_env != NULL && *homedir_env == '~') |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4838 | { |
| 4839 | int usedlen = 0; |
| 4840 | int flen; |
| 4841 | char_u *fbuf = NULL; |
| 4842 | |
| 4843 | flen = (int)STRLEN(homedir_env); |
Bram Moolenaar | 00136dc | 2018-07-25 21:19:13 +0200 | [diff] [blame] | 4844 | (void)modify_fname((char_u *)":p", FALSE, &usedlen, |
Bram Moolenaar | d12f811 | 2012-06-20 17:56:09 +0200 | [diff] [blame] | 4845 | &homedir_env, &fbuf, &flen); |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4846 | flen = (int)STRLEN(homedir_env); |
| 4847 | if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) |
| 4848 | /* Remove the trailing / that is added to a directory. */ |
| 4849 | homedir_env[flen - 1] = NUL; |
| 4850 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4851 | #endif |
| 4852 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4853 | if (homedir_env != NULL) |
| 4854 | envlen = STRLEN(homedir_env); |
| 4855 | |
| 4856 | if (!one) |
| 4857 | src = skipwhite(src); |
| 4858 | while (*src && dstlen > 0) |
| 4859 | { |
| 4860 | /* |
| 4861 | * Here we are at the beginning of a file name. |
| 4862 | * First, check to see if the beginning of the file name matches |
| 4863 | * $HOME or the "real" home directory. Check that there is a '/' |
| 4864 | * after the match (so that if e.g. the file is "/home/pieter/bla", |
| 4865 | * and the home directory is "/home/piet", the file does not end up |
| 4866 | * as "~er/bla" (which would seem to indicate the file "bla" in user |
| 4867 | * er's home directory)). |
| 4868 | */ |
| 4869 | p = homedir; |
| 4870 | len = dirlen; |
| 4871 | for (;;) |
| 4872 | { |
| 4873 | if ( len |
| 4874 | && fnamencmp(src, p, len) == 0 |
| 4875 | && (vim_ispathsep(src[len]) |
| 4876 | || (!one && (src[len] == ',' || src[len] == ' ')) |
| 4877 | || src[len] == NUL)) |
| 4878 | { |
| 4879 | src += len; |
| 4880 | if (--dstlen > 0) |
| 4881 | *dst++ = '~'; |
| 4882 | |
| 4883 | /* |
| 4884 | * If it's just the home directory, add "/". |
| 4885 | */ |
| 4886 | if (!vim_ispathsep(src[0]) && --dstlen > 0) |
| 4887 | *dst++ = '/'; |
| 4888 | break; |
| 4889 | } |
| 4890 | if (p == homedir_env) |
| 4891 | break; |
| 4892 | p = homedir_env; |
| 4893 | len = envlen; |
| 4894 | } |
| 4895 | |
| 4896 | /* if (!one) skip to separator: space or comma */ |
| 4897 | while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) |
| 4898 | *dst++ = *src++; |
| 4899 | /* skip separator */ |
| 4900 | while ((*src == ' ' || *src == ',') && --dstlen > 0) |
| 4901 | *dst++ = *src++; |
| 4902 | } |
| 4903 | /* if (dstlen == 0) out of space, what to do??? */ |
| 4904 | |
| 4905 | *dst = NUL; |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4906 | |
| 4907 | if (homedir_env != homedir_env_orig) |
| 4908 | vim_free(homedir_env); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | /* |
| 4912 | * Like home_replace, store the replaced string in allocated memory. |
| 4913 | * When something fails, NULL is returned. |
| 4914 | */ |
| 4915 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4916 | home_replace_save( |
| 4917 | buf_T *buf, /* when not NULL, check for help files */ |
| 4918 | char_u *src) /* input file name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4919 | { |
| 4920 | char_u *dst; |
| 4921 | unsigned len; |
| 4922 | |
| 4923 | len = 3; /* space for "~/" and trailing NUL */ |
| 4924 | if (src != NULL) /* just in case */ |
| 4925 | len += (unsigned)STRLEN(src); |
| 4926 | dst = alloc(len); |
| 4927 | if (dst != NULL) |
| 4928 | home_replace(buf, src, dst, len, TRUE); |
| 4929 | return dst; |
| 4930 | } |
| 4931 | |
| 4932 | /* |
| 4933 | * Compare two file names and return: |
| 4934 | * FPC_SAME if they both exist and are the same file. |
| 4935 | * FPC_SAMEX if they both don't exist and have the same file name. |
| 4936 | * FPC_DIFF if they both exist and are different files. |
| 4937 | * FPC_NOTX if they both don't exist. |
| 4938 | * FPC_DIFFX if one of them doesn't exist. |
| 4939 | * For the first name environment variables are expanded |
| 4940 | */ |
| 4941 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4942 | fullpathcmp( |
| 4943 | char_u *s1, |
| 4944 | char_u *s2, |
| 4945 | int checkname) /* when both don't exist, check file names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4946 | { |
| 4947 | #ifdef UNIX |
| 4948 | char_u exp1[MAXPATHL]; |
| 4949 | char_u full1[MAXPATHL]; |
| 4950 | char_u full2[MAXPATHL]; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4951 | stat_T st1, st2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4952 | int r1, r2; |
| 4953 | |
| 4954 | expand_env(s1, exp1, MAXPATHL); |
| 4955 | r1 = mch_stat((char *)exp1, &st1); |
| 4956 | r2 = mch_stat((char *)s2, &st2); |
| 4957 | if (r1 != 0 && r2 != 0) |
| 4958 | { |
| 4959 | /* if mch_stat() doesn't work, may compare the names */ |
| 4960 | if (checkname) |
| 4961 | { |
| 4962 | if (fnamecmp(exp1, s2) == 0) |
| 4963 | return FPC_SAMEX; |
| 4964 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 4965 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 4966 | if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) |
| 4967 | return FPC_SAMEX; |
| 4968 | } |
| 4969 | return FPC_NOTX; |
| 4970 | } |
| 4971 | if (r1 != 0 || r2 != 0) |
| 4972 | return FPC_DIFFX; |
| 4973 | if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) |
| 4974 | return FPC_SAME; |
| 4975 | return FPC_DIFF; |
| 4976 | #else |
| 4977 | char_u *exp1; /* expanded s1 */ |
| 4978 | char_u *full1; /* full path of s1 */ |
| 4979 | char_u *full2; /* full path of s2 */ |
| 4980 | int retval = FPC_DIFF; |
| 4981 | int r1, r2; |
| 4982 | |
| 4983 | /* allocate one buffer to store three paths (alloc()/free() is slow!) */ |
| 4984 | if ((exp1 = alloc(MAXPATHL * 3)) != NULL) |
| 4985 | { |
| 4986 | full1 = exp1 + MAXPATHL; |
| 4987 | full2 = full1 + MAXPATHL; |
| 4988 | |
| 4989 | expand_env(s1, exp1, MAXPATHL); |
| 4990 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 4991 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 4992 | |
| 4993 | /* If vim_FullName() fails, the file probably doesn't exist. */ |
| 4994 | if (r1 != OK && r2 != OK) |
| 4995 | { |
| 4996 | if (checkname && fnamecmp(exp1, s2) == 0) |
| 4997 | retval = FPC_SAMEX; |
| 4998 | else |
| 4999 | retval = FPC_NOTX; |
| 5000 | } |
| 5001 | else if (r1 != OK || r2 != OK) |
| 5002 | retval = FPC_DIFFX; |
| 5003 | else if (fnamecmp(full1, full2)) |
| 5004 | retval = FPC_DIFF; |
| 5005 | else |
| 5006 | retval = FPC_SAME; |
| 5007 | vim_free(exp1); |
| 5008 | } |
| 5009 | return retval; |
| 5010 | #endif |
| 5011 | } |
| 5012 | |
| 5013 | /* |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5014 | * Get the tail of a path: the file name. |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 5015 | * When the path ends in a path separator the tail is the NUL after it. |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5016 | * Fail safe: never returns NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5017 | */ |
| 5018 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5019 | gettail(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5020 | { |
| 5021 | char_u *p1, *p2; |
| 5022 | |
| 5023 | if (fname == NULL) |
| 5024 | return (char_u *)""; |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5025 | for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5026 | { |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5027 | if (vim_ispathsep_nocolon(*p2)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5028 | p1 = p2 + 1; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5029 | MB_PTR_ADV(p2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5030 | } |
| 5031 | return p1; |
| 5032 | } |
| 5033 | |
| 5034 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5035 | * Get pointer to tail of "fname", including path separators. Putting a NUL |
| 5036 | * here leaves the directory name. Takes care of "c:/" and "//". |
| 5037 | * Always returns a valid pointer. |
| 5038 | */ |
| 5039 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5040 | gettail_sep(char_u *fname) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5041 | { |
| 5042 | char_u *p; |
| 5043 | char_u *t; |
| 5044 | |
| 5045 | p = get_past_head(fname); /* don't remove the '/' from "c:/file" */ |
| 5046 | t = gettail(fname); |
| 5047 | while (t > p && after_pathsep(fname, t)) |
| 5048 | --t; |
| 5049 | #ifdef VMS |
| 5050 | /* path separator is part of the path */ |
| 5051 | ++t; |
| 5052 | #endif |
| 5053 | return t; |
| 5054 | } |
| 5055 | |
| 5056 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5057 | * get the next path component (just after the next path separator). |
| 5058 | */ |
| 5059 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5060 | getnextcomp(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5061 | { |
| 5062 | while (*fname && !vim_ispathsep(*fname)) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5063 | MB_PTR_ADV(fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5064 | if (*fname) |
| 5065 | ++fname; |
| 5066 | return fname; |
| 5067 | } |
| 5068 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5069 | /* |
| 5070 | * Get a pointer to one character past the head of a path name. |
| 5071 | * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. |
| 5072 | * If there is no head, path is returned. |
| 5073 | */ |
| 5074 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5075 | get_past_head(char_u *path) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5076 | { |
| 5077 | char_u *retval; |
| 5078 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5079 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5080 | /* may skip "c:" */ |
| 5081 | if (isalpha(path[0]) && path[1] == ':') |
| 5082 | retval = path + 2; |
| 5083 | else |
| 5084 | retval = path; |
| 5085 | #else |
| 5086 | # if defined(AMIGA) |
| 5087 | /* may skip "label:" */ |
| 5088 | retval = vim_strchr(path, ':'); |
| 5089 | if (retval == NULL) |
| 5090 | retval = path; |
| 5091 | # else /* Unix */ |
| 5092 | retval = path; |
| 5093 | # endif |
| 5094 | #endif |
| 5095 | |
| 5096 | while (vim_ispathsep(*retval)) |
| 5097 | ++retval; |
| 5098 | |
| 5099 | return retval; |
| 5100 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5101 | |
| 5102 | /* |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5103 | * Return TRUE if 'c' is a path separator. |
| 5104 | * Note that for MS-Windows this includes the colon. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5105 | */ |
| 5106 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5107 | vim_ispathsep(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5108 | { |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5109 | #ifdef UNIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5110 | return (c == '/'); /* UNIX has ':' inside file names */ |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5111 | #else |
| 5112 | # ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5113 | return (c == ':' || c == '/' || c == '\\'); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5114 | # else |
| 5115 | # ifdef VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5116 | /* server"user passwd"::device:[full.path.name]fname.extension;version" */ |
| 5117 | return (c == ':' || c == '[' || c == ']' || c == '/' |
| 5118 | || c == '<' || c == '>' || c == '"' ); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5119 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5120 | return (c == ':' || c == '/'); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5121 | # endif /* VMS */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5122 | # endif |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5123 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5126 | /* |
| 5127 | * Like vim_ispathsep(c), but exclude the colon for MS-Windows. |
| 5128 | */ |
| 5129 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5130 | vim_ispathsep_nocolon(int c) |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5131 | { |
| 5132 | return vim_ispathsep(c) |
| 5133 | #ifdef BACKSLASH_IN_FILENAME |
| 5134 | && c != ':' |
| 5135 | #endif |
| 5136 | ; |
| 5137 | } |
| 5138 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5139 | /* |
| 5140 | * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" |
| 5141 | * It's done in-place. |
| 5142 | */ |
| 5143 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5144 | shorten_dir(char_u *str) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5145 | { |
| 5146 | char_u *tail, *s, *d; |
| 5147 | int skip = FALSE; |
| 5148 | |
| 5149 | tail = gettail(str); |
| 5150 | d = str; |
| 5151 | for (s = str; ; ++s) |
| 5152 | { |
| 5153 | if (s >= tail) /* copy the whole tail */ |
| 5154 | { |
| 5155 | *d++ = *s; |
| 5156 | if (*s == NUL) |
| 5157 | break; |
| 5158 | } |
| 5159 | else if (vim_ispathsep(*s)) /* copy '/' and next char */ |
| 5160 | { |
| 5161 | *d++ = *s; |
| 5162 | skip = FALSE; |
| 5163 | } |
| 5164 | else if (!skip) |
| 5165 | { |
| 5166 | *d++ = *s; /* copy next char */ |
| 5167 | if (*s != '~' && *s != '.') /* and leading "~" and "." */ |
| 5168 | skip = TRUE; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5169 | if (has_mbyte) |
| 5170 | { |
| 5171 | int l = mb_ptr2len(s); |
| 5172 | |
| 5173 | while (--l > 0) |
Bram Moolenaar | b6baca5 | 2006-08-15 20:24:14 +0000 | [diff] [blame] | 5174 | *d++ = *++s; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5175 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5176 | } |
| 5177 | } |
| 5178 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5179 | |
Bram Moolenaar | 900b4d7 | 2005-12-12 22:05:50 +0000 | [diff] [blame] | 5180 | /* |
| 5181 | * Return TRUE if the directory of "fname" exists, FALSE otherwise. |
| 5182 | * Also returns TRUE if there is no directory name. |
| 5183 | * "fname" must be writable!. |
| 5184 | */ |
| 5185 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5186 | dir_of_file_exists(char_u *fname) |
Bram Moolenaar | 900b4d7 | 2005-12-12 22:05:50 +0000 | [diff] [blame] | 5187 | { |
| 5188 | char_u *p; |
| 5189 | int c; |
| 5190 | int retval; |
| 5191 | |
| 5192 | p = gettail_sep(fname); |
| 5193 | if (p == fname) |
| 5194 | return TRUE; |
| 5195 | c = *p; |
| 5196 | *p = NUL; |
| 5197 | retval = mch_isdir(fname); |
| 5198 | *p = c; |
| 5199 | return retval; |
| 5200 | } |
| 5201 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5202 | /* |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5203 | * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally |
| 5204 | * and deal with 'fileignorecase'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5205 | */ |
| 5206 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5207 | vim_fnamecmp(char_u *x, char_u *y) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5208 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5209 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5210 | return vim_fnamencmp(x, y, MAXPATHL); |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5211 | #else |
| 5212 | if (p_fic) |
| 5213 | return MB_STRICMP(x, y); |
| 5214 | return STRCMP(x, y); |
| 5215 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5216 | } |
| 5217 | |
| 5218 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5219 | vim_fnamencmp(char_u *x, char_u *y, size_t len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5220 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5221 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5222 | char_u *px = x; |
| 5223 | char_u *py = y; |
| 5224 | int cx = NUL; |
| 5225 | int cy = NUL; |
| 5226 | |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5227 | while (len > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5228 | { |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5229 | cx = PTR2CHAR(px); |
| 5230 | cy = PTR2CHAR(py); |
| 5231 | if (cx == NUL || cy == NUL |
| 5232 | || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy) |
| 5233 | && !(cx == '/' && cy == '\\') |
| 5234 | && !(cx == '\\' && cy == '/'))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5235 | break; |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5236 | len -= MB_PTR2LEN(px); |
| 5237 | px += MB_PTR2LEN(px); |
| 5238 | py += MB_PTR2LEN(py); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5239 | } |
| 5240 | if (len == 0) |
| 5241 | return 0; |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5242 | return (cx - cy); |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5243 | #else |
| 5244 | if (p_fic) |
| 5245 | return MB_STRNICMP(x, y, len); |
| 5246 | return STRNCMP(x, y, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5247 | #endif |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5248 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5249 | |
| 5250 | /* |
| 5251 | * Concatenate file names fname1 and fname2 into allocated memory. |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 5252 | * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5253 | */ |
| 5254 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5255 | concat_fnames(char_u *fname1, char_u *fname2, int sep) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5256 | { |
| 5257 | char_u *dest; |
| 5258 | |
| 5259 | dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3)); |
| 5260 | if (dest != NULL) |
| 5261 | { |
| 5262 | STRCPY(dest, fname1); |
| 5263 | if (sep) |
| 5264 | add_pathsep(dest); |
| 5265 | STRCAT(dest, fname2); |
| 5266 | } |
| 5267 | return dest; |
| 5268 | } |
| 5269 | |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5270 | /* |
| 5271 | * Concatenate two strings and return the result in allocated memory. |
| 5272 | * Returns NULL when out of memory. |
| 5273 | */ |
| 5274 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5275 | concat_str(char_u *str1, char_u *str2) |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5276 | { |
| 5277 | char_u *dest; |
| 5278 | size_t l = STRLEN(str1); |
| 5279 | |
| 5280 | dest = alloc((unsigned)(l + STRLEN(str2) + 1L)); |
| 5281 | if (dest != NULL) |
| 5282 | { |
| 5283 | STRCPY(dest, str1); |
| 5284 | STRCPY(dest + l, str2); |
| 5285 | } |
| 5286 | return dest; |
| 5287 | } |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5288 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5289 | /* |
| 5290 | * Add a path separator to a file name, unless it already ends in a path |
| 5291 | * separator. |
| 5292 | */ |
| 5293 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5294 | add_pathsep(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5295 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5296 | if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5297 | STRCAT(p, PATHSEPSTR); |
| 5298 | } |
| 5299 | |
| 5300 | /* |
| 5301 | * FullName_save - Make an allocated copy of a full file name. |
| 5302 | * Returns NULL when out of memory. |
| 5303 | */ |
| 5304 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5305 | FullName_save( |
| 5306 | char_u *fname, |
| 5307 | int force) /* force expansion, even when it already looks |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 5308 | * like a full path name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5309 | { |
| 5310 | char_u *buf; |
| 5311 | char_u *new_fname = NULL; |
| 5312 | |
| 5313 | if (fname == NULL) |
| 5314 | return NULL; |
| 5315 | |
| 5316 | buf = alloc((unsigned)MAXPATHL); |
| 5317 | if (buf != NULL) |
| 5318 | { |
| 5319 | if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) |
| 5320 | new_fname = vim_strsave(buf); |
| 5321 | else |
| 5322 | new_fname = vim_strsave(fname); |
| 5323 | vim_free(buf); |
| 5324 | } |
| 5325 | return new_fname; |
| 5326 | } |
| 5327 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5328 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5329 | prepare_to_exit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5330 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5331 | #if defined(SIGHUP) && defined(SIG_IGN) |
| 5332 | /* Ignore SIGHUP, because a dropped connection causes a read error, which |
| 5333 | * makes Vim exit and then handling SIGHUP causes various reentrance |
| 5334 | * problems. */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 5335 | signal(SIGHUP, SIG_IGN); |
| 5336 | #endif |
| 5337 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5338 | #ifdef FEAT_GUI |
| 5339 | if (gui.in_use) |
| 5340 | { |
| 5341 | gui.dying = TRUE; |
| 5342 | out_trash(); /* trash any pending output */ |
| 5343 | } |
| 5344 | else |
| 5345 | #endif |
| 5346 | { |
| 5347 | windgoto((int)Rows - 1, 0); |
| 5348 | |
| 5349 | /* |
| 5350 | * Switch terminal mode back now, so messages end up on the "normal" |
| 5351 | * screen (if there are two screens). |
| 5352 | */ |
| 5353 | settmode(TMODE_COOK); |
Bram Moolenaar | cea912a | 2016-10-12 14:20:24 +0200 | [diff] [blame] | 5354 | stoptermcap(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5355 | out_flush(); |
| 5356 | } |
| 5357 | } |
| 5358 | |
| 5359 | /* |
| 5360 | * Preserve files and exit. |
| 5361 | * When called IObuff must contain a message. |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 5362 | * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe |
| 5363 | * functions, such as allocating memory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5364 | */ |
| 5365 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5366 | preserve_exit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5367 | { |
| 5368 | buf_T *buf; |
| 5369 | |
| 5370 | prepare_to_exit(); |
| 5371 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5372 | /* Setting this will prevent free() calls. That avoids calling free() |
| 5373 | * recursively when free() was invoked with a bad pointer. */ |
| 5374 | really_exiting = TRUE; |
| 5375 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5376 | out_str(IObuff); |
| 5377 | screen_start(); /* don't know where cursor is now */ |
| 5378 | out_flush(); |
| 5379 | |
| 5380 | ml_close_notmod(); /* close all not-modified buffers */ |
| 5381 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 5382 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5383 | { |
| 5384 | if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) |
| 5385 | { |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 5386 | OUT_STR("Vim: preserving files...\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5387 | screen_start(); /* don't know where cursor is now */ |
| 5388 | out_flush(); |
| 5389 | ml_sync_all(FALSE, FALSE); /* preserve all swap files */ |
| 5390 | break; |
| 5391 | } |
| 5392 | } |
| 5393 | |
| 5394 | ml_close_all(FALSE); /* close all memfiles, without deleting */ |
| 5395 | |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 5396 | OUT_STR("Vim: Finished.\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5397 | |
| 5398 | getout(1); |
| 5399 | } |
| 5400 | |
| 5401 | /* |
| 5402 | * return TRUE if "fname" exists. |
| 5403 | */ |
| 5404 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5405 | vim_fexists(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5406 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 5407 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5408 | |
| 5409 | if (mch_stat((char *)fname, &st)) |
| 5410 | return FALSE; |
| 5411 | return TRUE; |
| 5412 | } |
| 5413 | |
| 5414 | /* |
| 5415 | * Check for CTRL-C pressed, but only once in a while. |
| 5416 | * Should be used instead of ui_breakcheck() for functions that check for |
| 5417 | * each line in the file. Calling ui_breakcheck() each time takes too much |
| 5418 | * time, because it can be a system call. |
| 5419 | */ |
| 5420 | |
| 5421 | #ifndef BREAKCHECK_SKIP |
| 5422 | # ifdef FEAT_GUI /* assume the GUI only runs on fast computers */ |
| 5423 | # define BREAKCHECK_SKIP 200 |
| 5424 | # else |
| 5425 | # define BREAKCHECK_SKIP 32 |
| 5426 | # endif |
| 5427 | #endif |
| 5428 | |
| 5429 | static int breakcheck_count = 0; |
| 5430 | |
| 5431 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5432 | line_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5433 | { |
| 5434 | if (++breakcheck_count >= BREAKCHECK_SKIP) |
| 5435 | { |
| 5436 | breakcheck_count = 0; |
| 5437 | ui_breakcheck(); |
| 5438 | } |
| 5439 | } |
| 5440 | |
| 5441 | /* |
| 5442 | * Like line_breakcheck() but check 10 times less often. |
| 5443 | */ |
| 5444 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5445 | fast_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5446 | { |
| 5447 | if (++breakcheck_count >= BREAKCHECK_SKIP * 10) |
| 5448 | { |
| 5449 | breakcheck_count = 0; |
| 5450 | ui_breakcheck(); |
| 5451 | } |
| 5452 | } |
| 5453 | |
| 5454 | /* |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 5455 | * Invoke expand_wildcards() for one pattern. |
| 5456 | * Expand items like "%:h" before the expansion. |
| 5457 | * Returns OK or FAIL. |
| 5458 | */ |
| 5459 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5460 | expand_wildcards_eval( |
| 5461 | char_u **pat, /* pointer to input pattern */ |
| 5462 | int *num_file, /* resulting number of files */ |
| 5463 | char_u ***file, /* array of resulting files */ |
| 5464 | int flags) /* EW_DIR, etc. */ |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 5465 | { |
| 5466 | int ret = FAIL; |
| 5467 | char_u *eval_pat = NULL; |
| 5468 | char_u *exp_pat = *pat; |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 5469 | char *ignored_msg; |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 5470 | int usedlen; |
| 5471 | |
| 5472 | if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') |
| 5473 | { |
| 5474 | ++emsg_off; |
| 5475 | eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, |
| 5476 | NULL, &ignored_msg, NULL); |
| 5477 | --emsg_off; |
| 5478 | if (eval_pat != NULL) |
| 5479 | exp_pat = concat_str(eval_pat, exp_pat + usedlen); |
| 5480 | } |
| 5481 | |
| 5482 | if (exp_pat != NULL) |
| 5483 | ret = expand_wildcards(1, &exp_pat, num_file, file, flags); |
| 5484 | |
| 5485 | if (eval_pat != NULL) |
| 5486 | { |
| 5487 | vim_free(exp_pat); |
| 5488 | vim_free(eval_pat); |
| 5489 | } |
| 5490 | |
| 5491 | return ret; |
| 5492 | } |
| 5493 | |
| 5494 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5495 | * Expand wildcards. Calls gen_expand_wildcards() and removes files matching |
| 5496 | * 'wildignore'. |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5497 | * Returns OK or FAIL. When FAIL then "num_files" won't be set. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5498 | */ |
| 5499 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5500 | expand_wildcards( |
| 5501 | int num_pat, /* number of input patterns */ |
| 5502 | char_u **pat, /* array of input patterns */ |
| 5503 | int *num_files, /* resulting number of files */ |
| 5504 | char_u ***files, /* array of resulting files */ |
| 5505 | int flags) /* EW_DIR, etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5506 | { |
| 5507 | int retval; |
| 5508 | int i, j; |
| 5509 | char_u *p; |
| 5510 | int non_suf_match; /* number without matching suffix */ |
| 5511 | |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5512 | retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5513 | |
| 5514 | /* When keeping all matches, return here */ |
Bram Moolenaar | 9e193ac | 2010-07-19 23:11:27 +0200 | [diff] [blame] | 5515 | if ((flags & EW_KEEPALL) || retval == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5516 | return retval; |
| 5517 | |
| 5518 | #ifdef FEAT_WILDIGN |
| 5519 | /* |
| 5520 | * Remove names that match 'wildignore'. |
| 5521 | */ |
| 5522 | if (*p_wig) |
| 5523 | { |
| 5524 | char_u *ffname; |
| 5525 | |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5526 | /* check all files in (*files)[] */ |
| 5527 | for (i = 0; i < *num_files; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5528 | { |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5529 | ffname = FullName_save((*files)[i], FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5530 | if (ffname == NULL) /* out of memory */ |
| 5531 | break; |
| 5532 | # ifdef VMS |
| 5533 | vms_remove_version(ffname); |
| 5534 | # endif |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5535 | if (match_file_list(p_wig, (*files)[i], ffname)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5536 | { |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 5537 | /* remove this matching file from the list */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5538 | vim_free((*files)[i]); |
| 5539 | for (j = i; j + 1 < *num_files; ++j) |
| 5540 | (*files)[j] = (*files)[j + 1]; |
| 5541 | --*num_files; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5542 | --i; |
| 5543 | } |
| 5544 | vim_free(ffname); |
| 5545 | } |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5546 | |
| 5547 | /* If the number of matches is now zero, we fail. */ |
| 5548 | if (*num_files == 0) |
| 5549 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 5550 | VIM_CLEAR(*files); |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5551 | return FAIL; |
| 5552 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5553 | } |
| 5554 | #endif |
| 5555 | |
| 5556 | /* |
| 5557 | * Move the names where 'suffixes' match to the end. |
| 5558 | */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5559 | if (*num_files > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5560 | { |
| 5561 | non_suf_match = 0; |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5562 | for (i = 0; i < *num_files; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5563 | { |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5564 | if (!match_suffix((*files)[i])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5565 | { |
| 5566 | /* |
| 5567 | * Move the name without matching suffix to the front |
| 5568 | * of the list. |
| 5569 | */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5570 | p = (*files)[i]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5571 | for (j = i; j > non_suf_match; --j) |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5572 | (*files)[j] = (*files)[j - 1]; |
| 5573 | (*files)[non_suf_match++] = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5574 | } |
| 5575 | } |
| 5576 | } |
| 5577 | |
| 5578 | return retval; |
| 5579 | } |
| 5580 | |
| 5581 | /* |
| 5582 | * Return TRUE if "fname" matches with an entry in 'suffixes'. |
| 5583 | */ |
| 5584 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5585 | match_suffix(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5586 | { |
| 5587 | int fnamelen, setsuflen; |
| 5588 | char_u *setsuf; |
| 5589 | #define MAXSUFLEN 30 /* maximum length of a file suffix */ |
| 5590 | char_u suf_buf[MAXSUFLEN]; |
| 5591 | |
| 5592 | fnamelen = (int)STRLEN(fname); |
| 5593 | setsuflen = 0; |
| 5594 | for (setsuf = p_su; *setsuf; ) |
| 5595 | { |
| 5596 | setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); |
Bram Moolenaar | 055a2ba | 2009-07-14 19:40:21 +0000 | [diff] [blame] | 5597 | if (setsuflen == 0) |
| 5598 | { |
| 5599 | char_u *tail = gettail(fname); |
| 5600 | |
| 5601 | /* empty entry: match name without a '.' */ |
| 5602 | if (vim_strchr(tail, '.') == NULL) |
| 5603 | { |
| 5604 | setsuflen = 1; |
| 5605 | break; |
| 5606 | } |
| 5607 | } |
| 5608 | else |
| 5609 | { |
| 5610 | if (fnamelen >= setsuflen |
| 5611 | && fnamencmp(suf_buf, fname + fnamelen - setsuflen, |
| 5612 | (size_t)setsuflen) == 0) |
| 5613 | break; |
| 5614 | setsuflen = 0; |
| 5615 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5616 | } |
| 5617 | return (setsuflen != 0); |
| 5618 | } |
| 5619 | |
| 5620 | #if !defined(NO_EXPANDPATH) || defined(PROTO) |
| 5621 | |
| 5622 | # ifdef VIM_BACKTICK |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5623 | static int vim_backtick(char_u *p); |
| 5624 | static int expand_backtick(garray_T *gap, char_u *pat, int flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5625 | # endif |
| 5626 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 5627 | # if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5628 | /* |
| 5629 | * File name expansion code for MS-DOS, Win16 and Win32. It's here because |
| 5630 | * it's shared between these systems. |
| 5631 | */ |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5632 | # if defined(PROTO) |
| 5633 | # define _cdecl |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5634 | # else |
| 5635 | # ifdef __BORLANDC__ |
| 5636 | # define _cdecl _RTLENTRYF |
| 5637 | # endif |
| 5638 | # endif |
| 5639 | |
| 5640 | /* |
| 5641 | * comparison function for qsort in dos_expandpath() |
| 5642 | */ |
| 5643 | static int _cdecl |
| 5644 | pstrcmp(const void *a, const void *b) |
| 5645 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5646 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5647 | } |
| 5648 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5649 | /* |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5650 | * Recursively expand one path component into all matching files and/or |
| 5651 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5652 | * Return the number of matches found. |
| 5653 | * "path" has backslashes before chars that are not to be expanded, starting |
| 5654 | * at "path[wildoff]". |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5655 | * Return the number of matches found. |
| 5656 | * NOTE: much of this is identical to unix_expandpath(), keep in sync! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5657 | */ |
| 5658 | static int |
| 5659 | dos_expandpath( |
| 5660 | garray_T *gap, |
| 5661 | char_u *path, |
| 5662 | int wildoff, |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5663 | int flags, /* EW_* flags */ |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 5664 | int didstar) /* expanded "**" once already */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5665 | { |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5666 | char_u *buf; |
| 5667 | char_u *path_end; |
| 5668 | char_u *p, *s, *e; |
| 5669 | int start_len = gap->ga_len; |
| 5670 | char_u *pat; |
| 5671 | regmatch_T regmatch; |
| 5672 | int starts_with_dot; |
| 5673 | int matches; |
| 5674 | int len; |
| 5675 | int starstar = FALSE; |
| 5676 | static int stardepth = 0; /* depth for "**" expansion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5677 | WIN32_FIND_DATA fb; |
| 5678 | HANDLE hFind = (HANDLE)0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5679 | WIN32_FIND_DATAW wfb; |
| 5680 | WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5681 | char_u *matchname; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5682 | int ok; |
| 5683 | |
| 5684 | /* Expanding "**" may take a long time, check for CTRL-C. */ |
| 5685 | if (stardepth > 0) |
| 5686 | { |
| 5687 | ui_breakcheck(); |
| 5688 | if (got_int) |
| 5689 | return 0; |
| 5690 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5691 | |
Bram Moolenaar | fc3abf4 | 2019-01-24 15:54:21 +0100 | [diff] [blame] | 5692 | // Make room for file name. When doing encoding conversion the actual |
| 5693 | // length may be quite a bit longer, thus use the maximum possible length. |
Bram Moolenaar | 7314efd | 2015-10-31 15:32:52 +0100 | [diff] [blame] | 5694 | buf = alloc((int)MAXPATHL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5695 | if (buf == NULL) |
| 5696 | return 0; |
| 5697 | |
| 5698 | /* |
| 5699 | * Find the first part in the path name that contains a wildcard or a ~1. |
| 5700 | * Copy it into buf, including the preceding characters. |
| 5701 | */ |
| 5702 | p = buf; |
| 5703 | s = buf; |
| 5704 | e = NULL; |
| 5705 | path_end = path; |
| 5706 | while (*path_end != NUL) |
| 5707 | { |
| 5708 | /* May ignore a wildcard that has a backslash before it; it will |
| 5709 | * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ |
| 5710 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 5711 | *p++ = *path_end++; |
| 5712 | else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') |
| 5713 | { |
| 5714 | if (e != NULL) |
| 5715 | break; |
| 5716 | s = p + 1; |
| 5717 | } |
| 5718 | else if (path_end >= path + wildoff |
| 5719 | && vim_strchr((char_u *)"*?[~", *path_end) != NULL) |
| 5720 | e = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5721 | if (has_mbyte) |
| 5722 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5723 | len = (*mb_ptr2len)(path_end); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5724 | STRNCPY(p, path_end, len); |
| 5725 | p += len; |
| 5726 | path_end += len; |
| 5727 | } |
| 5728 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5729 | *p++ = *path_end++; |
| 5730 | } |
| 5731 | e = p; |
| 5732 | *e = NUL; |
| 5733 | |
| 5734 | /* now we have one wildcard component between s and e */ |
| 5735 | /* Remove backslashes between "wildoff" and the start of the wildcard |
| 5736 | * component. */ |
| 5737 | for (p = buf + wildoff; p < s; ++p) |
| 5738 | if (rem_backslash(p)) |
| 5739 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 5740 | STRMOVE(p, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5741 | --e; |
| 5742 | --s; |
| 5743 | } |
| 5744 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5745 | /* Check for "**" between "s" and "e". */ |
| 5746 | for (p = s; p < e; ++p) |
| 5747 | if (p[0] == '*' && p[1] == '*') |
| 5748 | starstar = TRUE; |
| 5749 | |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 5750 | starts_with_dot = *s == '.'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5751 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 5752 | if (pat == NULL) |
| 5753 | { |
| 5754 | vim_free(buf); |
| 5755 | return 0; |
| 5756 | } |
| 5757 | |
| 5758 | /* compile the regexp into a program */ |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 5759 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
Bram Moolenaar | b560983 | 2011-07-20 15:04:58 +0200 | [diff] [blame] | 5760 | ++emsg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5761 | regmatch.rm_ic = TRUE; /* Always ignore case */ |
| 5762 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 5763 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
Bram Moolenaar | b560983 | 2011-07-20 15:04:58 +0200 | [diff] [blame] | 5764 | --emsg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5765 | vim_free(pat); |
| 5766 | |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 5767 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5768 | { |
| 5769 | vim_free(buf); |
| 5770 | return 0; |
| 5771 | } |
| 5772 | |
| 5773 | /* remember the pattern or file name being looked for */ |
| 5774 | matchname = vim_strsave(s); |
| 5775 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5776 | /* If "**" is by itself, this is the first time we encounter it and more |
| 5777 | * is following then find matches without any directory. */ |
| 5778 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 5779 | && *path_end == '/') |
| 5780 | { |
| 5781 | STRCPY(s, path_end + 1); |
| 5782 | ++stardepth; |
| 5783 | (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 5784 | --stardepth; |
| 5785 | } |
| 5786 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5787 | /* Scan all files in the directory with "dir/ *.*" */ |
| 5788 | STRCPY(s, "*.*"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5789 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 5790 | { |
| 5791 | /* The active codepage differs from 'encoding'. Attempt using the |
| 5792 | * wide function. If it fails because it is not implemented fall back |
| 5793 | * to the non-wide version (for Windows 98) */ |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 5794 | wn = enc_to_utf16(buf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5795 | if (wn != NULL) |
| 5796 | { |
| 5797 | hFind = FindFirstFileW(wn, &wfb); |
| 5798 | if (hFind == INVALID_HANDLE_VALUE |
| 5799 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 5800 | VIM_CLEAR(wn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5801 | } |
| 5802 | } |
| 5803 | |
| 5804 | if (wn == NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 5805 | hFind = FindFirstFile((LPCSTR)buf, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5806 | ok = (hFind != INVALID_HANDLE_VALUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5807 | |
| 5808 | while (ok) |
| 5809 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5810 | if (wn != NULL) |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 5811 | p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5812 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5813 | p = (char_u *)fb.cFileName; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5814 | /* Ignore entries starting with a dot, unless when asked for. Accept |
| 5815 | * all entries found with "matchname". */ |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 5816 | if ((p[0] != '.' || starts_with_dot |
| 5817 | || ((flags & EW_DODOT) |
| 5818 | && p[1] != NUL && (p[1] != '.' || p[2] != NUL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5819 | && (matchname == NULL |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 5820 | || (regmatch.regprog != NULL |
| 5821 | && vim_regexec(®match, p, (colnr_T)0)) |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 5822 | || ((flags & EW_NOTWILD) |
| 5823 | && fnamencmp(path + (s - buf), p, e - s) == 0))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5824 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5825 | STRCPY(s, p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5826 | len = (int)STRLEN(buf); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5827 | |
| 5828 | if (starstar && stardepth < 100) |
| 5829 | { |
| 5830 | /* For "**" in the pattern first go deeper in the tree to |
| 5831 | * find matches. */ |
| 5832 | STRCPY(buf + len, "/**"); |
| 5833 | STRCPY(buf + len + 3, path_end); |
| 5834 | ++stardepth; |
| 5835 | (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); |
| 5836 | --stardepth; |
| 5837 | } |
| 5838 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5839 | STRCPY(buf + len, path_end); |
| 5840 | if (mch_has_exp_wildcard(path_end)) |
| 5841 | { |
| 5842 | /* need to expand another component of the path */ |
| 5843 | /* remove backslashes for the remaining components only */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5844 | (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5845 | } |
| 5846 | else |
| 5847 | { |
| 5848 | /* no more wildcards, check if there is a match */ |
| 5849 | /* remove backslashes for the remaining components only */ |
| 5850 | if (*path_end != 0) |
| 5851 | backslash_halve(buf + len + 1); |
| 5852 | if (mch_getperm(buf) >= 0) /* add existing file */ |
| 5853 | addfile(gap, buf, flags); |
| 5854 | } |
| 5855 | } |
| 5856 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5857 | if (wn != NULL) |
| 5858 | { |
| 5859 | vim_free(p); |
| 5860 | ok = FindNextFileW(hFind, &wfb); |
| 5861 | } |
| 5862 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5863 | ok = FindNextFile(hFind, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5864 | |
| 5865 | /* If no more matches and no match was used, try expanding the name |
| 5866 | * itself. Finds the long name of a short filename. */ |
| 5867 | if (!ok && matchname != NULL && gap->ga_len == start_len) |
| 5868 | { |
| 5869 | STRCPY(s, matchname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5870 | FindClose(hFind); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5871 | if (wn != NULL) |
| 5872 | { |
| 5873 | vim_free(wn); |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 5874 | wn = enc_to_utf16(buf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5875 | if (wn != NULL) |
| 5876 | hFind = FindFirstFileW(wn, &wfb); |
| 5877 | } |
| 5878 | if (wn == NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 5879 | hFind = FindFirstFile((LPCSTR)buf, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5880 | ok = (hFind != INVALID_HANDLE_VALUE); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 5881 | VIM_CLEAR(matchname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5882 | } |
| 5883 | } |
| 5884 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5885 | FindClose(hFind); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5886 | vim_free(wn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5887 | vim_free(buf); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5888 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5889 | vim_free(matchname); |
| 5890 | |
| 5891 | matches = gap->ga_len - start_len; |
| 5892 | if (matches > 0) |
| 5893 | qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, |
| 5894 | sizeof(char_u *), pstrcmp); |
| 5895 | return matches; |
| 5896 | } |
| 5897 | |
| 5898 | int |
| 5899 | mch_expandpath( |
| 5900 | garray_T *gap, |
| 5901 | char_u *path, |
| 5902 | int flags) /* EW_* flags */ |
| 5903 | { |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5904 | return dos_expandpath(gap, path, 0, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5905 | } |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 5906 | # endif // MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5907 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5908 | #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ |
| 5909 | || defined(PROTO) |
| 5910 | /* |
| 5911 | * Unix style wildcard expansion code. |
| 5912 | * It's here because it's used both for Unix and Mac. |
| 5913 | */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5914 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5915 | pstrcmp(const void *a, const void *b) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5916 | { |
| 5917 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
| 5918 | } |
| 5919 | |
| 5920 | /* |
| 5921 | * Recursively expand one path component into all matching files and/or |
| 5922 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
| 5923 | * "path" has backslashes before chars that are not to be expanded, starting |
| 5924 | * at "path + wildoff". |
| 5925 | * Return the number of matches found. |
| 5926 | * NOTE: much of this is identical to dos_expandpath(), keep in sync! |
| 5927 | */ |
| 5928 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5929 | unix_expandpath( |
| 5930 | garray_T *gap, |
| 5931 | char_u *path, |
| 5932 | int wildoff, |
| 5933 | int flags, /* EW_* flags */ |
| 5934 | int didstar) /* expanded "**" once already */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5935 | { |
| 5936 | char_u *buf; |
| 5937 | char_u *path_end; |
| 5938 | char_u *p, *s, *e; |
| 5939 | int start_len = gap->ga_len; |
| 5940 | char_u *pat; |
| 5941 | regmatch_T regmatch; |
| 5942 | int starts_with_dot; |
| 5943 | int matches; |
| 5944 | int len; |
| 5945 | int starstar = FALSE; |
| 5946 | static int stardepth = 0; /* depth for "**" expansion */ |
| 5947 | |
| 5948 | DIR *dirp; |
| 5949 | struct dirent *dp; |
| 5950 | |
| 5951 | /* Expanding "**" may take a long time, check for CTRL-C. */ |
| 5952 | if (stardepth > 0) |
| 5953 | { |
| 5954 | ui_breakcheck(); |
| 5955 | if (got_int) |
| 5956 | return 0; |
| 5957 | } |
| 5958 | |
| 5959 | /* make room for file name */ |
| 5960 | buf = alloc((int)STRLEN(path) + BASENAMELEN + 5); |
| 5961 | if (buf == NULL) |
| 5962 | return 0; |
| 5963 | |
| 5964 | /* |
| 5965 | * Find the first part in the path name that contains a wildcard. |
Bram Moolenaar | 2d0b92f | 2012-04-30 21:09:43 +0200 | [diff] [blame] | 5966 | * When EW_ICASE is set every letter is considered to be a wildcard. |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5967 | * Copy it into "buf", including the preceding characters. |
| 5968 | */ |
| 5969 | p = buf; |
| 5970 | s = buf; |
| 5971 | e = NULL; |
| 5972 | path_end = path; |
| 5973 | while (*path_end != NUL) |
| 5974 | { |
| 5975 | /* May ignore a wildcard that has a backslash before it; it will |
| 5976 | * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ |
| 5977 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 5978 | *p++ = *path_end++; |
| 5979 | else if (*path_end == '/') |
| 5980 | { |
| 5981 | if (e != NULL) |
| 5982 | break; |
| 5983 | s = p + 1; |
| 5984 | } |
| 5985 | else if (path_end >= path + wildoff |
Bram Moolenaar | 2d0b92f | 2012-04-30 21:09:43 +0200 | [diff] [blame] | 5986 | && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5987 | || (!p_fic && (flags & EW_ICASE) |
| 5988 | && isalpha(PTR2CHAR(path_end))))) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5989 | e = p; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5990 | if (has_mbyte) |
| 5991 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5992 | len = (*mb_ptr2len)(path_end); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5993 | STRNCPY(p, path_end, len); |
| 5994 | p += len; |
| 5995 | path_end += len; |
| 5996 | } |
| 5997 | else |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5998 | *p++ = *path_end++; |
| 5999 | } |
| 6000 | e = p; |
| 6001 | *e = NUL; |
| 6002 | |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 6003 | /* Now we have one wildcard component between "s" and "e". */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6004 | /* Remove backslashes between "wildoff" and the start of the wildcard |
| 6005 | * component. */ |
| 6006 | for (p = buf + wildoff; p < s; ++p) |
| 6007 | if (rem_backslash(p)) |
| 6008 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 6009 | STRMOVE(p, p + 1); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6010 | --e; |
| 6011 | --s; |
| 6012 | } |
| 6013 | |
| 6014 | /* Check for "**" between "s" and "e". */ |
| 6015 | for (p = s; p < e; ++p) |
| 6016 | if (p[0] == '*' && p[1] == '*') |
| 6017 | starstar = TRUE; |
| 6018 | |
| 6019 | /* convert the file pattern to a regexp pattern */ |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 6020 | starts_with_dot = *s == '.'; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6021 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 6022 | if (pat == NULL) |
| 6023 | { |
| 6024 | vim_free(buf); |
| 6025 | return 0; |
| 6026 | } |
| 6027 | |
| 6028 | /* compile the regexp into a program */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 6029 | if (flags & EW_ICASE) |
| 6030 | regmatch.rm_ic = TRUE; /* 'wildignorecase' set */ |
| 6031 | else |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 6032 | regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */ |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 6033 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 6034 | ++emsg_silent; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6035 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 6036 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 6037 | --emsg_silent; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6038 | vim_free(pat); |
| 6039 | |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 6040 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6041 | { |
| 6042 | vim_free(buf); |
| 6043 | return 0; |
| 6044 | } |
| 6045 | |
| 6046 | /* If "**" is by itself, this is the first time we encounter it and more |
| 6047 | * is following then find matches without any directory. */ |
| 6048 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 6049 | && *path_end == '/') |
| 6050 | { |
| 6051 | STRCPY(s, path_end + 1); |
| 6052 | ++stardepth; |
| 6053 | (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 6054 | --stardepth; |
| 6055 | } |
| 6056 | |
| 6057 | /* open the directory for scanning */ |
| 6058 | *s = NUL; |
| 6059 | dirp = opendir(*buf == NUL ? "." : (char *)buf); |
| 6060 | |
| 6061 | /* Find all matching entries */ |
| 6062 | if (dirp != NULL) |
| 6063 | { |
| 6064 | for (;;) |
| 6065 | { |
| 6066 | dp = readdir(dirp); |
| 6067 | if (dp == NULL) |
| 6068 | break; |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 6069 | if ((dp->d_name[0] != '.' || starts_with_dot |
| 6070 | || ((flags & EW_DODOT) |
| 6071 | && dp->d_name[1] != NUL |
| 6072 | && (dp->d_name[1] != '.' || dp->d_name[2] != NUL))) |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 6073 | && ((regmatch.regprog != NULL && vim_regexec(®match, |
| 6074 | (char_u *)dp->d_name, (colnr_T)0)) |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 6075 | || ((flags & EW_NOTWILD) |
| 6076 | && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0))) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6077 | { |
| 6078 | STRCPY(s, dp->d_name); |
| 6079 | len = STRLEN(buf); |
| 6080 | |
| 6081 | if (starstar && stardepth < 100) |
| 6082 | { |
| 6083 | /* For "**" in the pattern first go deeper in the tree to |
| 6084 | * find matches. */ |
| 6085 | STRCPY(buf + len, "/**"); |
| 6086 | STRCPY(buf + len + 3, path_end); |
| 6087 | ++stardepth; |
| 6088 | (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); |
| 6089 | --stardepth; |
| 6090 | } |
| 6091 | |
| 6092 | STRCPY(buf + len, path_end); |
| 6093 | if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */ |
| 6094 | { |
| 6095 | /* need to expand another component of the path */ |
| 6096 | /* remove backslashes for the remaining components only */ |
| 6097 | (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); |
| 6098 | } |
| 6099 | else |
| 6100 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 6101 | stat_T sb; |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 6102 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6103 | /* no more wildcards, check if there is a match */ |
| 6104 | /* remove backslashes for the remaining components only */ |
| 6105 | if (*path_end != NUL) |
| 6106 | backslash_halve(buf + len + 1); |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 6107 | /* add existing file or symbolic link */ |
Bram Moolenaar | ab11a59 | 2015-03-06 22:00:11 +0100 | [diff] [blame] | 6108 | if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0 |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 6109 | : mch_getperm(buf) >= 0) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6110 | { |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 6111 | #ifdef MACOS_CONVERT |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6112 | size_t precomp_len = STRLEN(buf)+1; |
| 6113 | char_u *precomp_buf = |
| 6114 | mac_precompose_path(buf, precomp_len, &precomp_len); |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 6115 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6116 | if (precomp_buf) |
| 6117 | { |
| 6118 | mch_memmove(buf, precomp_buf, precomp_len); |
| 6119 | vim_free(precomp_buf); |
| 6120 | } |
| 6121 | #endif |
| 6122 | addfile(gap, buf, flags); |
| 6123 | } |
| 6124 | } |
| 6125 | } |
| 6126 | } |
| 6127 | |
| 6128 | closedir(dirp); |
| 6129 | } |
| 6130 | |
| 6131 | vim_free(buf); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6132 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6133 | |
| 6134 | matches = gap->ga_len - start_len; |
| 6135 | if (matches > 0) |
| 6136 | qsort(((char_u **)gap->ga_data) + start_len, matches, |
| 6137 | sizeof(char_u *), pstrcmp); |
| 6138 | return matches; |
| 6139 | } |
| 6140 | #endif |
| 6141 | |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 6142 | #if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 6143 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 6144 | * Sort "gap" and remove duplicate entries. "gap" is expected to contain a |
| 6145 | * list of file names in allocated memory. |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 6146 | */ |
| 6147 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6148 | remove_duplicates(garray_T *gap) |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 6149 | { |
| 6150 | int i; |
| 6151 | int j; |
| 6152 | char_u **fnames = (char_u **)gap->ga_data; |
| 6153 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 6154 | sort_strings(fnames, gap->ga_len); |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 6155 | for (i = gap->ga_len - 1; i > 0; --i) |
| 6156 | if (fnamecmp(fnames[i - 1], fnames[i]) == 0) |
| 6157 | { |
| 6158 | vim_free(fnames[i]); |
| 6159 | for (j = i + 1; j < gap->ga_len; ++j) |
| 6160 | fnames[j - 1] = fnames[j]; |
| 6161 | --gap->ga_len; |
| 6162 | } |
| 6163 | } |
| 6164 | #endif |
| 6165 | |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6166 | /* |
| 6167 | * Return TRUE if "p" contains what looks like an environment variable. |
| 6168 | * Allowing for escaping. |
| 6169 | */ |
| 6170 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6171 | has_env_var(char_u *p) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6172 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 6173 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6174 | { |
| 6175 | if (*p == '\\' && p[1] != NUL) |
| 6176 | ++p; |
| 6177 | else if (vim_strchr((char_u *) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 6178 | #if defined(MSWIN) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6179 | "$%" |
| 6180 | #else |
| 6181 | "$" |
| 6182 | #endif |
| 6183 | , *p) != NULL) |
| 6184 | return TRUE; |
| 6185 | } |
| 6186 | return FALSE; |
| 6187 | } |
| 6188 | |
| 6189 | #ifdef SPECIAL_WILDCHAR |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6190 | /* |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 6191 | * Return TRUE if "p" contains a special wildcard character, one that Vim |
| 6192 | * cannot expand, requires using a shell. |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6193 | */ |
| 6194 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6195 | has_special_wildchar(char_u *p) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6196 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 6197 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6198 | { |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 6199 | /* Allow for escaping. */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6200 | if (*p == '\\' && p[1] != NUL) |
| 6201 | ++p; |
| 6202 | else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL) |
| 6203 | return TRUE; |
| 6204 | } |
| 6205 | return FALSE; |
| 6206 | } |
| 6207 | #endif |
| 6208 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6209 | /* |
| 6210 | * Generic wildcard expansion code. |
| 6211 | * |
| 6212 | * Characters in "pat" that should not be expanded must be preceded with a |
| 6213 | * backslash. E.g., "/path\ with\ spaces/my\*star*" |
| 6214 | * |
| 6215 | * Return FAIL when no single file was found. In this case "num_file" is not |
| 6216 | * set, and "file" may contain an error message. |
| 6217 | * Return OK when some files found. "num_file" is set to the number of |
| 6218 | * matches, "file" to the array of matches. Call FreeWild() later. |
| 6219 | */ |
| 6220 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6221 | gen_expand_wildcards( |
| 6222 | int num_pat, /* number of input patterns */ |
| 6223 | char_u **pat, /* array of input patterns */ |
| 6224 | int *num_file, /* resulting number of files */ |
| 6225 | char_u ***file, /* array of resulting files */ |
| 6226 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6227 | { |
| 6228 | int i; |
| 6229 | garray_T ga; |
| 6230 | char_u *p; |
| 6231 | static int recursive = FALSE; |
| 6232 | int add_pat; |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 6233 | int retval = OK; |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 6234 | #if defined(FEAT_SEARCHPATH) |
| 6235 | int did_expand_in_path = FALSE; |
| 6236 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6237 | |
| 6238 | /* |
| 6239 | * expand_env() is called to expand things like "~user". If this fails, |
| 6240 | * it calls ExpandOne(), which brings us back here. In this case, always |
| 6241 | * call the machine specific expansion function, if possible. Otherwise, |
| 6242 | * return FAIL. |
| 6243 | */ |
| 6244 | if (recursive) |
| 6245 | #ifdef SPECIAL_WILDCHAR |
| 6246 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 6247 | #else |
| 6248 | return FAIL; |
| 6249 | #endif |
| 6250 | |
| 6251 | #ifdef SPECIAL_WILDCHAR |
| 6252 | /* |
| 6253 | * If there are any special wildcard characters which we cannot handle |
| 6254 | * here, call machine specific function for all the expansion. This |
| 6255 | * avoids starting the shell for each argument separately. |
| 6256 | * For `=expr` do use the internal function. |
| 6257 | */ |
| 6258 | for (i = 0; i < num_pat; i++) |
| 6259 | { |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6260 | if (has_special_wildchar(pat[i]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6261 | # ifdef VIM_BACKTICK |
| 6262 | && !(vim_backtick(pat[i]) && pat[i][1] == '=') |
| 6263 | # endif |
| 6264 | ) |
| 6265 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 6266 | } |
| 6267 | #endif |
| 6268 | |
| 6269 | recursive = TRUE; |
| 6270 | |
| 6271 | /* |
| 6272 | * The matching file names are stored in a growarray. Init it empty. |
| 6273 | */ |
| 6274 | ga_init2(&ga, (int)sizeof(char_u *), 30); |
| 6275 | |
| 6276 | for (i = 0; i < num_pat; ++i) |
| 6277 | { |
| 6278 | add_pat = -1; |
| 6279 | p = pat[i]; |
| 6280 | |
| 6281 | #ifdef VIM_BACKTICK |
| 6282 | if (vim_backtick(p)) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 6283 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6284 | add_pat = expand_backtick(&ga, p, flags); |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 6285 | if (add_pat == -1) |
| 6286 | retval = FAIL; |
| 6287 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6288 | else |
| 6289 | #endif |
| 6290 | { |
| 6291 | /* |
| 6292 | * First expand environment variables, "~/" and "~user/". |
| 6293 | */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6294 | if (has_env_var(p) || *p == '~') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6295 | { |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 6296 | p = expand_env_save_opt(p, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6297 | if (p == NULL) |
| 6298 | p = pat[i]; |
| 6299 | #ifdef UNIX |
| 6300 | /* |
| 6301 | * On Unix, if expand_env() can't expand an environment |
| 6302 | * variable, use the shell to do that. Discard previously |
| 6303 | * found file names and start all over again. |
| 6304 | */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6305 | else if (has_env_var(p) || *p == '~') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6306 | { |
| 6307 | vim_free(p); |
Bram Moolenaar | 782027e | 2009-06-24 14:25:49 +0000 | [diff] [blame] | 6308 | ga_clear_strings(&ga); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6309 | i = mch_expand_wildcards(num_pat, pat, num_file, file, |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 6310 | flags|EW_KEEPDOLLAR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6311 | recursive = FALSE; |
| 6312 | return i; |
| 6313 | } |
| 6314 | #endif |
| 6315 | } |
| 6316 | |
| 6317 | /* |
| 6318 | * If there are wildcards: Expand file names and add each match to |
| 6319 | * the list. If there is no match, and EW_NOTFOUND is given, add |
| 6320 | * the pattern. |
| 6321 | * If there are no wildcards: Add the file name if it exists or |
| 6322 | * when EW_NOTFOUND is given. |
| 6323 | */ |
| 6324 | if (mch_has_exp_wildcard(p)) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 6325 | { |
| 6326 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 6327 | if ((flags & EW_PATH) |
| 6328 | && !mch_isFullName(p) |
| 6329 | && !(p[0] == '.' |
| 6330 | && (vim_ispathsep(p[1]) |
| 6331 | || (p[1] == '.' && vim_ispathsep(p[2])))) |
| 6332 | ) |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 6333 | { |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 6334 | /* :find completion where 'path' is used. |
| 6335 | * Recursiveness is OK here. */ |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 6336 | recursive = FALSE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 6337 | add_pat = expand_in_path(&ga, p, flags); |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 6338 | recursive = TRUE; |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 6339 | did_expand_in_path = TRUE; |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 6340 | } |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 6341 | else |
| 6342 | #endif |
| 6343 | add_pat = mch_expandpath(&ga, p, flags); |
| 6344 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6345 | } |
| 6346 | |
| 6347 | if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) |
| 6348 | { |
| 6349 | char_u *t = backslash_halve_save(p); |
| 6350 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6351 | /* When EW_NOTFOUND is used, always add files and dirs. Makes |
| 6352 | * "vim c:/" work. */ |
| 6353 | if (flags & EW_NOTFOUND) |
| 6354 | addfile(&ga, t, flags | EW_DIR | EW_FILE); |
Bram Moolenaar | 00efded | 2016-07-07 14:29:10 +0200 | [diff] [blame] | 6355 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6356 | addfile(&ga, t, flags); |
| 6357 | vim_free(t); |
| 6358 | } |
| 6359 | |
Bram Moolenaar | b28ebbc | 2010-07-14 16:59:57 +0200 | [diff] [blame] | 6360 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 6361 | if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH)) |
Bram Moolenaar | b28ebbc | 2010-07-14 16:59:57 +0200 | [diff] [blame] | 6362 | uniquefy_paths(&ga, p); |
| 6363 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6364 | if (p != pat[i]) |
| 6365 | vim_free(p); |
| 6366 | } |
| 6367 | |
| 6368 | *num_file = ga.ga_len; |
| 6369 | *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; |
| 6370 | |
| 6371 | recursive = FALSE; |
| 6372 | |
Bram Moolenaar | 336bd62 | 2016-01-17 18:23:58 +0100 | [diff] [blame] | 6373 | return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6374 | } |
| 6375 | |
| 6376 | # ifdef VIM_BACKTICK |
| 6377 | |
| 6378 | /* |
| 6379 | * Return TRUE if we can expand this backtick thing here. |
| 6380 | */ |
| 6381 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6382 | vim_backtick(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6383 | { |
| 6384 | return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); |
| 6385 | } |
| 6386 | |
| 6387 | /* |
| 6388 | * Expand an item in `backticks` by executing it as a command. |
| 6389 | * Currently only works when pat[] starts and ends with a `. |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 6390 | * Returns number of file names found, -1 if an error is encountered. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6391 | */ |
| 6392 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6393 | expand_backtick( |
| 6394 | garray_T *gap, |
| 6395 | char_u *pat, |
| 6396 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6397 | { |
| 6398 | char_u *p; |
| 6399 | char_u *cmd; |
| 6400 | char_u *buffer; |
| 6401 | int cnt = 0; |
| 6402 | int i; |
| 6403 | |
| 6404 | /* Create the command: lop off the backticks. */ |
| 6405 | cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); |
| 6406 | if (cmd == NULL) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 6407 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6408 | |
| 6409 | #ifdef FEAT_EVAL |
| 6410 | if (*cmd == '=') /* `={expr}`: Expand expression */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 6411 | buffer = eval_to_string(cmd + 1, &p, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6412 | else |
| 6413 | #endif |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 6414 | buffer = get_cmd_output(cmd, NULL, |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 6415 | (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6416 | vim_free(cmd); |
| 6417 | if (buffer == NULL) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 6418 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6419 | |
| 6420 | cmd = buffer; |
| 6421 | while (*cmd != NUL) |
| 6422 | { |
| 6423 | cmd = skipwhite(cmd); /* skip over white space */ |
| 6424 | p = cmd; |
| 6425 | while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */ |
| 6426 | ++p; |
| 6427 | /* add an entry if it is not empty */ |
| 6428 | if (p > cmd) |
| 6429 | { |
| 6430 | i = *p; |
| 6431 | *p = NUL; |
| 6432 | addfile(gap, cmd, flags); |
| 6433 | *p = i; |
| 6434 | ++cnt; |
| 6435 | } |
| 6436 | cmd = p; |
| 6437 | while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) |
| 6438 | ++cmd; |
| 6439 | } |
| 6440 | |
| 6441 | vim_free(buffer); |
| 6442 | return cnt; |
| 6443 | } |
| 6444 | # endif /* VIM_BACKTICK */ |
| 6445 | |
| 6446 | /* |
| 6447 | * Add a file to a file list. Accepted flags: |
| 6448 | * EW_DIR add directories |
| 6449 | * EW_FILE add files |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 6450 | * EW_EXEC add executable files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6451 | * EW_NOTFOUND add even when it doesn't exist |
| 6452 | * EW_ADDSLASH add slash after directory name |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 6453 | * EW_ALLLINKS add symlink also when the referred file does not exist |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6454 | */ |
| 6455 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6456 | addfile( |
| 6457 | garray_T *gap, |
| 6458 | char_u *f, /* filename */ |
| 6459 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6460 | { |
| 6461 | char_u *p; |
| 6462 | int isdir; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 6463 | stat_T sb; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6464 | |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 6465 | /* if the file/dir/link doesn't exist, may not add it */ |
| 6466 | if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS) |
Bram Moolenaar | ab11a59 | 2015-03-06 22:00:11 +0100 | [diff] [blame] | 6467 | ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6468 | return; |
| 6469 | |
| 6470 | #ifdef FNAME_ILLEGAL |
| 6471 | /* if the file/dir contains illegal characters, don't add it */ |
| 6472 | if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) |
| 6473 | return; |
| 6474 | #endif |
| 6475 | |
| 6476 | isdir = mch_isdir(f); |
| 6477 | if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) |
| 6478 | return; |
| 6479 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 6480 | /* If the file isn't executable, may not add it. Do accept directories. |
| 6481 | * When invoked from expand_shellcmd() do not use $PATH. */ |
| 6482 | if (!isdir && (flags & EW_EXEC) |
| 6483 | && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD))) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 6484 | return; |
| 6485 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6486 | /* Make room for another item in the file list. */ |
| 6487 | if (ga_grow(gap, 1) == FAIL) |
| 6488 | return; |
| 6489 | |
| 6490 | p = alloc((unsigned)(STRLEN(f) + 1 + isdir)); |
| 6491 | if (p == NULL) |
| 6492 | return; |
| 6493 | |
| 6494 | STRCPY(p, f); |
| 6495 | #ifdef BACKSLASH_IN_FILENAME |
| 6496 | slash_adjust(p); |
| 6497 | #endif |
| 6498 | /* |
| 6499 | * Append a slash or backslash after directory names if none is present. |
| 6500 | */ |
| 6501 | #ifndef DONT_ADD_PATHSEP_TO_DIR |
| 6502 | if (isdir && (flags & EW_ADDSLASH)) |
| 6503 | add_pathsep(p); |
| 6504 | #endif |
| 6505 | ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6506 | } |
| 6507 | #endif /* !NO_EXPANDPATH */ |
| 6508 | |
| 6509 | #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO) |
| 6510 | |
| 6511 | #ifndef SEEK_SET |
| 6512 | # define SEEK_SET 0 |
| 6513 | #endif |
| 6514 | #ifndef SEEK_END |
| 6515 | # define SEEK_END 2 |
| 6516 | #endif |
| 6517 | |
| 6518 | /* |
| 6519 | * Get the stdout of an external command. |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 6520 | * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not |
| 6521 | * NULL store the length there. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6522 | * Returns an allocated string, or NULL for error. |
| 6523 | */ |
| 6524 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6525 | get_cmd_output( |
| 6526 | char_u *cmd, |
| 6527 | char_u *infile, /* optional input file name */ |
| 6528 | int flags, /* can be SHELL_SILENT */ |
| 6529 | int *ret_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6530 | { |
| 6531 | char_u *tempname; |
| 6532 | char_u *command; |
| 6533 | char_u *buffer = NULL; |
| 6534 | int len; |
| 6535 | int i = 0; |
| 6536 | FILE *fd; |
| 6537 | |
| 6538 | if (check_restricted() || check_secure()) |
| 6539 | return NULL; |
| 6540 | |
| 6541 | /* get a name for the temp file */ |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 6542 | if ((tempname = vim_tempname('o', FALSE)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6543 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 6544 | emsg(_(e_notmp)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6545 | return NULL; |
| 6546 | } |
| 6547 | |
| 6548 | /* Add the redirection stuff */ |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 6549 | command = make_filter_cmd(cmd, infile, tempname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6550 | if (command == NULL) |
| 6551 | goto done; |
| 6552 | |
| 6553 | /* |
| 6554 | * Call the shell to execute the command (errors are ignored). |
| 6555 | * Don't check timestamps here. |
| 6556 | */ |
| 6557 | ++no_check_timestamps; |
| 6558 | call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags); |
| 6559 | --no_check_timestamps; |
| 6560 | |
| 6561 | vim_free(command); |
| 6562 | |
| 6563 | /* |
| 6564 | * read the names from the file into memory |
| 6565 | */ |
| 6566 | # ifdef VMS |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 6567 | /* created temporary file is not always readable as binary */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6568 | fd = mch_fopen((char *)tempname, "r"); |
| 6569 | # else |
| 6570 | fd = mch_fopen((char *)tempname, READBIN); |
| 6571 | # endif |
| 6572 | |
| 6573 | if (fd == NULL) |
| 6574 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 6575 | semsg(_(e_notopen), tempname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6576 | goto done; |
| 6577 | } |
| 6578 | |
| 6579 | fseek(fd, 0L, SEEK_END); |
| 6580 | len = ftell(fd); /* get size of temp file */ |
| 6581 | fseek(fd, 0L, SEEK_SET); |
| 6582 | |
| 6583 | buffer = alloc(len + 1); |
| 6584 | if (buffer != NULL) |
| 6585 | i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd); |
| 6586 | fclose(fd); |
| 6587 | mch_remove(tempname); |
| 6588 | if (buffer == NULL) |
| 6589 | goto done; |
| 6590 | #ifdef VMS |
| 6591 | len = i; /* VMS doesn't give us what we asked for... */ |
| 6592 | #endif |
| 6593 | if (i != len) |
| 6594 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 6595 | semsg(_(e_notread), tempname); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 6596 | VIM_CLEAR(buffer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6597 | } |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 6598 | else if (ret_len == NULL) |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 6599 | { |
| 6600 | /* Change NUL into SOH, otherwise the string is truncated. */ |
| 6601 | for (i = 0; i < len; ++i) |
Bram Moolenaar | f40f4ab | 2013-08-03 17:31:28 +0200 | [diff] [blame] | 6602 | if (buffer[i] == NUL) |
| 6603 | buffer[i] = 1; |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 6604 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 6605 | buffer[len] = NUL; /* make sure the buffer is terminated */ |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 6606 | } |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 6607 | else |
| 6608 | *ret_len = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6609 | |
| 6610 | done: |
| 6611 | vim_free(tempname); |
| 6612 | return buffer; |
| 6613 | } |
| 6614 | #endif |
| 6615 | |
| 6616 | /* |
| 6617 | * Free the list of files returned by expand_wildcards() or other expansion |
| 6618 | * functions. |
| 6619 | */ |
| 6620 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6621 | FreeWild(int count, char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6622 | { |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 6623 | if (count <= 0 || files == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6624 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6625 | while (count--) |
| 6626 | vim_free(files[count]); |
| 6627 | vim_free(files); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6628 | } |
| 6629 | |
| 6630 | /* |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 6631 | * Return TRUE when need to go to Insert mode because of 'insertmode'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6632 | * Don't do this when still processing a command or a mapping. |
| 6633 | * Don't do this when inside a ":normal" command. |
| 6634 | */ |
| 6635 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6636 | goto_im(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6637 | { |
| 6638 | return (p_im && stuff_empty() && typebuf_typed()); |
| 6639 | } |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 6640 | |
| 6641 | /* |
Bram Moolenaar | 050fe7e | 2014-05-22 14:00:16 +0200 | [diff] [blame] | 6642 | * Returns the isolated name of the shell in allocated memory: |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 6643 | * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f". |
| 6644 | * - Remove any argument. E.g., "csh -f" -> "csh". |
| 6645 | * But don't allow a space in the path, so that this works: |
| 6646 | * "/usr/bin/csh --rcfile ~/.cshrc" |
| 6647 | * But don't do that for Windows, it's common to have a space in the path. |
| 6648 | */ |
| 6649 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6650 | get_isolated_shell_name(void) |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 6651 | { |
| 6652 | char_u *p; |
| 6653 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 6654 | #ifdef MSWIN |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 6655 | p = gettail(p_sh); |
| 6656 | p = vim_strnsave(p, (int)(skiptowhite(p) - p)); |
| 6657 | #else |
| 6658 | p = skiptowhite(p_sh); |
| 6659 | if (*p == NUL) |
| 6660 | { |
| 6661 | /* No white space, use the tail. */ |
| 6662 | p = vim_strsave(gettail(p_sh)); |
| 6663 | } |
| 6664 | else |
| 6665 | { |
| 6666 | char_u *p1, *p2; |
| 6667 | |
| 6668 | /* Find the last path separator before the space. */ |
| 6669 | p1 = p_sh; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 6670 | for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2)) |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 6671 | if (vim_ispathsep(*p2)) |
| 6672 | p1 = p2 + 1; |
| 6673 | p = vim_strnsave(p1, (int)(p - p1)); |
| 6674 | } |
| 6675 | #endif |
| 6676 | return p; |
| 6677 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 6678 | |
| 6679 | /* |
| 6680 | * Check if the "://" of a URL is at the pointer, return URL_SLASH. |
| 6681 | * Also check for ":\\", which MS Internet Explorer accepts, return |
| 6682 | * URL_BACKSLASH. |
| 6683 | */ |
| 6684 | int |
| 6685 | path_is_url(char_u *p) |
| 6686 | { |
| 6687 | if (STRNCMP(p, "://", (size_t)3) == 0) |
| 6688 | return URL_SLASH; |
| 6689 | else if (STRNCMP(p, ":\\\\", (size_t)3) == 0) |
| 6690 | return URL_BACKSLASH; |
| 6691 | return 0; |
| 6692 | } |
| 6693 | |
| 6694 | /* |
| 6695 | * Check if "fname" starts with "name://". Return URL_SLASH if it does. |
| 6696 | * Return URL_BACKSLASH for "name:\\". |
| 6697 | * Return zero otherwise. |
| 6698 | */ |
| 6699 | int |
| 6700 | path_with_url(char_u *fname) |
| 6701 | { |
| 6702 | char_u *p; |
| 6703 | |
| 6704 | for (p = fname; isalpha(*p); ++p) |
| 6705 | ; |
| 6706 | return path_is_url(p); |
| 6707 | } |
| 6708 | |
| 6709 | /* |
| 6710 | * Return TRUE if "name" is a full (absolute) path name or URL. |
| 6711 | */ |
| 6712 | int |
| 6713 | vim_isAbsName(char_u *name) |
| 6714 | { |
| 6715 | return (path_with_url(name) != 0 || mch_isFullName(name)); |
| 6716 | } |
| 6717 | |
| 6718 | /* |
| 6719 | * Get absolute file name into buffer "buf[len]". |
| 6720 | * |
| 6721 | * return FAIL for failure, OK otherwise |
| 6722 | */ |
| 6723 | int |
| 6724 | vim_FullName( |
| 6725 | char_u *fname, |
| 6726 | char_u *buf, |
| 6727 | int len, |
| 6728 | int force) /* force expansion even when already absolute */ |
| 6729 | { |
| 6730 | int retval = OK; |
| 6731 | int url; |
| 6732 | |
| 6733 | *buf = NUL; |
| 6734 | if (fname == NULL) |
| 6735 | return FAIL; |
| 6736 | |
| 6737 | url = path_with_url(fname); |
| 6738 | if (!url) |
| 6739 | retval = mch_FullName(fname, buf, len, force); |
| 6740 | if (url || retval == FAIL) |
| 6741 | { |
| 6742 | /* something failed; use the file name (truncate when too long) */ |
| 6743 | vim_strncpy(buf, fname, len - 1); |
| 6744 | } |
| 6745 | #if defined(MSWIN) |
| 6746 | slash_adjust(buf); |
| 6747 | #endif |
| 6748 | return retval; |
| 6749 | } |