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 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1500 | |
| 1501 | /* columns for marks adjusted for removed columns */ |
| 1502 | less_cols = (int)(p_extra - saved_line); |
| 1503 | } |
| 1504 | |
| 1505 | if (p_extra == NULL) |
| 1506 | p_extra = (char_u *)""; /* append empty line */ |
| 1507 | |
| 1508 | #ifdef FEAT_COMMENTS |
| 1509 | /* concatenate leader and p_extra, if there is a leader */ |
| 1510 | if (lead_len) |
| 1511 | { |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1512 | if (flags & OPENLINE_COM_LIST && second_line_indent > 0) |
| 1513 | { |
| 1514 | int i; |
Bram Moolenaar | 3610578 | 2012-06-14 20:59:25 +0200 | [diff] [blame] | 1515 | int padding = second_line_indent |
| 1516 | - (newindent + (int)STRLEN(leader)); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1517 | |
| 1518 | /* Here whitespace is inserted after the comment char. |
| 1519 | * Below, set_indent(newindent, SIN_INSERT) will insert the |
| 1520 | * whitespace needed before the comment char. */ |
| 1521 | for (i = 0; i < padding; i++) |
| 1522 | { |
| 1523 | STRCAT(leader, " "); |
Bram Moolenaar | 5fb9ec5 | 2012-07-25 16:10:03 +0200 | [diff] [blame] | 1524 | less_cols--; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1525 | newcol++; |
| 1526 | } |
| 1527 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1528 | STRCAT(leader, p_extra); |
| 1529 | p_extra = leader; |
| 1530 | did_ai = TRUE; /* So truncating blanks works with comments */ |
| 1531 | less_cols -= lead_len; |
| 1532 | } |
| 1533 | else |
| 1534 | end_comment_pending = NUL; /* turns out there was no leader */ |
| 1535 | #endif |
| 1536 | |
| 1537 | old_cursor = curwin->w_cursor; |
| 1538 | if (dir == BACKWARD) |
| 1539 | --curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1540 | if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1541 | { |
| 1542 | if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE) |
| 1543 | == FAIL) |
| 1544 | goto theend; |
| 1545 | /* Postpone calling changed_lines(), because it would mess up folding |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 1546 | * with markers. |
| 1547 | * 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] | 1548 | * be marks there. But still needed in diff mode. */ |
| 1549 | if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count |
| 1550 | #ifdef FEAT_DIFF |
| 1551 | || curwin->w_p_diff |
| 1552 | #endif |
| 1553 | ) |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 1554 | mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1555 | did_append = TRUE; |
| 1556 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1557 | else |
| 1558 | { |
| 1559 | /* |
| 1560 | * In VREPLACE mode we are starting to replace the next line. |
| 1561 | */ |
| 1562 | curwin->w_cursor.lnum++; |
| 1563 | if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed) |
| 1564 | { |
| 1565 | /* In case we NL to a new line, BS to the previous one, and NL |
| 1566 | * again, we don't want to save the new line for undo twice. |
| 1567 | */ |
| 1568 | (void)u_save_cursor(); /* errors are ignored! */ |
| 1569 | vr_lines_changed++; |
| 1570 | } |
| 1571 | ml_replace(curwin->w_cursor.lnum, p_extra, TRUE); |
| 1572 | changed_bytes(curwin->w_cursor.lnum, 0); |
| 1573 | curwin->w_cursor.lnum--; |
| 1574 | did_append = FALSE; |
| 1575 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1576 | |
| 1577 | if (newindent |
| 1578 | #ifdef FEAT_SMARTINDENT |
| 1579 | || did_si |
| 1580 | #endif |
| 1581 | ) |
| 1582 | { |
| 1583 | ++curwin->w_cursor.lnum; |
| 1584 | #ifdef FEAT_SMARTINDENT |
| 1585 | if (did_si) |
| 1586 | { |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 1587 | int sw = (int)get_sw_value(curbuf); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 1588 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1589 | if (p_sr) |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 1590 | newindent -= newindent % sw; |
| 1591 | newindent += sw; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1592 | } |
| 1593 | #endif |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 1594 | /* Copy the indent */ |
| 1595 | if (curbuf->b_p_ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1596 | { |
| 1597 | (void)copy_indent(newindent, saved_line); |
| 1598 | |
| 1599 | /* |
| 1600 | * Set the 'preserveindent' option so that any further screwing |
| 1601 | * with the line doesn't entirely destroy our efforts to preserve |
| 1602 | * it. It gets restored at the function end. |
| 1603 | */ |
| 1604 | curbuf->b_p_pi = TRUE; |
| 1605 | } |
| 1606 | else |
| 1607 | (void)set_indent(newindent, SIN_INSERT); |
| 1608 | less_cols -= curwin->w_cursor.col; |
| 1609 | |
| 1610 | ai_col = curwin->w_cursor.col; |
| 1611 | |
| 1612 | /* |
| 1613 | * In REPLACE mode, for each character in the new indent, there must |
| 1614 | * be a NUL on the replace stack, for when it is deleted with BS |
| 1615 | */ |
| 1616 | if (REPLACE_NORMAL(State)) |
| 1617 | for (n = 0; n < (int)curwin->w_cursor.col; ++n) |
| 1618 | replace_push(NUL); |
| 1619 | newcol += curwin->w_cursor.col; |
| 1620 | #ifdef FEAT_SMARTINDENT |
| 1621 | if (no_si) |
| 1622 | did_si = FALSE; |
| 1623 | #endif |
| 1624 | } |
| 1625 | |
| 1626 | #ifdef FEAT_COMMENTS |
| 1627 | /* |
| 1628 | * In REPLACE mode, for each character in the extra leader, there must be |
| 1629 | * a NUL on the replace stack, for when it is deleted with BS. |
| 1630 | */ |
| 1631 | if (REPLACE_NORMAL(State)) |
| 1632 | while (lead_len-- > 0) |
| 1633 | replace_push(NUL); |
| 1634 | #endif |
| 1635 | |
| 1636 | curwin->w_cursor = old_cursor; |
| 1637 | |
| 1638 | if (dir == FORWARD) |
| 1639 | { |
| 1640 | if (trunc_line || (State & INSERT)) |
| 1641 | { |
| 1642 | /* truncate current line at cursor */ |
| 1643 | saved_line[curwin->w_cursor.col] = NUL; |
| 1644 | /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */ |
| 1645 | if (trunc_line && !(flags & OPENLINE_KEEPTRAIL)) |
| 1646 | truncate_spaces(saved_line); |
| 1647 | ml_replace(curwin->w_cursor.lnum, saved_line, FALSE); |
| 1648 | saved_line = NULL; |
| 1649 | if (did_append) |
| 1650 | { |
| 1651 | changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, |
| 1652 | curwin->w_cursor.lnum + 1, 1L); |
| 1653 | did_append = FALSE; |
| 1654 | |
| 1655 | /* Move marks after the line break to the new line. */ |
| 1656 | if (flags & OPENLINE_MARKFIX) |
| 1657 | mark_col_adjust(curwin->w_cursor.lnum, |
| 1658 | curwin->w_cursor.col + less_cols_off, |
Bram Moolenaar | e1e714e | 2018-12-31 23:58:24 +0100 | [diff] [blame] | 1659 | 1L, (long)-less_cols, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1660 | } |
| 1661 | else |
| 1662 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | * Put the cursor on the new line. Careful: the scrollup() above may |
| 1667 | * have moved w_cursor, we must use old_cursor. |
| 1668 | */ |
| 1669 | curwin->w_cursor.lnum = old_cursor.lnum + 1; |
| 1670 | } |
| 1671 | if (did_append) |
| 1672 | changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L); |
| 1673 | |
| 1674 | curwin->w_cursor.col = newcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1675 | curwin->w_cursor.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1676 | |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 1677 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1678 | /* |
| 1679 | * In VREPLACE mode, we are handling the replace stack ourselves, so stop |
| 1680 | * fixthisline() from doing it (via change_indent()) by telling it we're in |
| 1681 | * normal INSERT mode. |
| 1682 | */ |
| 1683 | if (State & VREPLACE_FLAG) |
| 1684 | { |
| 1685 | vreplace_mode = State; /* So we know to put things right later */ |
| 1686 | State = INSERT; |
| 1687 | } |
| 1688 | else |
| 1689 | vreplace_mode = 0; |
| 1690 | #endif |
| 1691 | #ifdef FEAT_LISP |
| 1692 | /* |
| 1693 | * May do lisp indenting. |
| 1694 | */ |
| 1695 | if (!p_paste |
| 1696 | # ifdef FEAT_COMMENTS |
| 1697 | && leader == NULL |
| 1698 | # endif |
| 1699 | && curbuf->b_p_lisp |
| 1700 | && curbuf->b_p_ai) |
| 1701 | { |
| 1702 | fixthisline(get_lisp_indent); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1703 | ai_col = (colnr_T)getwhitecols_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1704 | } |
| 1705 | #endif |
| 1706 | #ifdef FEAT_CINDENT |
| 1707 | /* |
| 1708 | * May do indenting after opening a new line. |
| 1709 | */ |
| 1710 | if (!p_paste |
| 1711 | && (curbuf->b_p_cin |
| 1712 | # ifdef FEAT_EVAL |
| 1713 | || *curbuf->b_p_inde != NUL |
| 1714 | # endif |
| 1715 | ) |
| 1716 | && in_cinkeys(dir == FORWARD |
| 1717 | ? KEY_OPEN_FORW |
| 1718 | : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))) |
| 1719 | { |
| 1720 | do_c_expr_indent(); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1721 | ai_col = (colnr_T)getwhitecols_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1722 | } |
| 1723 | #endif |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 1724 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1725 | if (vreplace_mode != 0) |
| 1726 | State = vreplace_mode; |
| 1727 | #endif |
| 1728 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1729 | /* |
| 1730 | * Finally, VREPLACE gets the stuff on the new line, then puts back the |
| 1731 | * original line, and inserts the new stuff char by char, pushing old stuff |
| 1732 | * onto the replace stack (via ins_char()). |
| 1733 | */ |
| 1734 | if (State & VREPLACE_FLAG) |
| 1735 | { |
| 1736 | /* Put new line in p_extra */ |
| 1737 | p_extra = vim_strsave(ml_get_curline()); |
| 1738 | if (p_extra == NULL) |
| 1739 | goto theend; |
| 1740 | |
| 1741 | /* Put back original line */ |
| 1742 | ml_replace(curwin->w_cursor.lnum, next_line, FALSE); |
| 1743 | |
| 1744 | /* Insert new stuff into line again */ |
| 1745 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1746 | curwin->w_cursor.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1747 | ins_bytes(p_extra); /* will call changed_bytes() */ |
| 1748 | vim_free(p_extra); |
| 1749 | next_line = NULL; |
| 1750 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1751 | |
Bram Moolenaar | 24a2d72 | 2018-04-24 19:36:43 +0200 | [diff] [blame] | 1752 | retval = OK; /* success! */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1753 | theend: |
| 1754 | curbuf->b_p_pi = saved_pi; |
| 1755 | vim_free(saved_line); |
| 1756 | vim_free(next_line); |
| 1757 | vim_free(allocated); |
| 1758 | return retval; |
| 1759 | } |
| 1760 | |
| 1761 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 1762 | /* |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 1763 | * get_leader_len() returns the length in bytes of the prefix of the given |
| 1764 | * string which introduces a comment. If this string is not a comment then |
| 1765 | * 0 is returned. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1766 | * When "flags" is not NULL, it is set to point to the flags of the recognized |
| 1767 | * comment leader. |
| 1768 | * "backward" must be true for the "O" command. |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1769 | * If "include_space" is set, include trailing whitespace while calculating the |
| 1770 | * length. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1771 | */ |
| 1772 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1773 | get_leader_len( |
| 1774 | char_u *line, |
| 1775 | char_u **flags, |
| 1776 | int backward, |
| 1777 | int include_space) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1778 | { |
| 1779 | int i, j; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1780 | int result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1781 | int got_com = FALSE; |
| 1782 | int found_one; |
| 1783 | char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ |
| 1784 | char_u *string; /* pointer to comment string */ |
| 1785 | char_u *list; |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1786 | int middle_match_len = 0; |
| 1787 | char_u *prev_list; |
Bram Moolenaar | 05da428 | 2011-05-10 14:44:11 +0200 | [diff] [blame] | 1788 | char_u *saved_flags = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1789 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1790 | result = i = 0; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1791 | while (VIM_ISWHITE(line[i])) /* leading white space is ignored */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1792 | ++i; |
| 1793 | |
| 1794 | /* |
| 1795 | * Repeat to match several nested comment strings. |
| 1796 | */ |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1797 | while (line[i] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1798 | { |
| 1799 | /* |
| 1800 | * scan through the 'comments' option for a match |
| 1801 | */ |
| 1802 | found_one = FALSE; |
| 1803 | for (list = curbuf->b_p_com; *list; ) |
| 1804 | { |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1805 | /* Get one option part into part_buf[]. Advance "list" to next |
| 1806 | * one. Put "string" at start of string. */ |
| 1807 | if (!got_com && flags != NULL) |
| 1808 | *flags = list; /* remember where flags started */ |
| 1809 | prev_list = list; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1810 | (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
| 1811 | string = vim_strchr(part_buf, ':'); |
| 1812 | if (string == NULL) /* missing ':', ignore this part */ |
| 1813 | continue; |
| 1814 | *string++ = NUL; /* isolate flags from string */ |
| 1815 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1816 | /* If we found a middle match previously, use that match when this |
| 1817 | * is not a middle or end. */ |
| 1818 | if (middle_match_len != 0 |
| 1819 | && vim_strchr(part_buf, COM_MIDDLE) == NULL |
| 1820 | && vim_strchr(part_buf, COM_END) == NULL) |
| 1821 | break; |
| 1822 | |
| 1823 | /* When we already found a nested comment, only accept further |
| 1824 | * nested comments. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1825 | if (got_com && vim_strchr(part_buf, COM_NEST) == NULL) |
| 1826 | continue; |
| 1827 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1828 | /* When 'O' flag present and using "O" command skip this one. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1829 | if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL) |
| 1830 | continue; |
| 1831 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1832 | /* Line contents and string must match. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1833 | * When string starts with white space, must have some white space |
| 1834 | * (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] | 1835 | * TABs and spaces). */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1836 | if (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1837 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1838 | if (i == 0 || !VIM_ISWHITE(line[i - 1])) |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 1839 | continue; /* missing white space */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1840 | while (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1841 | ++string; |
| 1842 | } |
| 1843 | for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) |
| 1844 | ; |
| 1845 | if (string[j] != NUL) |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1846 | continue; /* string doesn't match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1847 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1848 | /* When 'b' flag used, there must be white space or an |
| 1849 | * end-of-line after the string in the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1850 | if (vim_strchr(part_buf, COM_BLANK) != NULL |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1851 | && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1852 | continue; |
| 1853 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1854 | /* We have found a match, stop searching unless this is a middle |
| 1855 | * comment. The middle comment can be a substring of the end |
| 1856 | * comment in which case it's better to return the length of the |
| 1857 | * end comment and its flags. Thus we keep searching with middle |
| 1858 | * and end matches and use an end match if it matches better. */ |
| 1859 | if (vim_strchr(part_buf, COM_MIDDLE) != NULL) |
| 1860 | { |
| 1861 | if (middle_match_len == 0) |
| 1862 | { |
| 1863 | middle_match_len = j; |
| 1864 | saved_flags = prev_list; |
| 1865 | } |
| 1866 | continue; |
| 1867 | } |
| 1868 | if (middle_match_len != 0 && j > middle_match_len) |
| 1869 | /* Use this match instead of the middle match, since it's a |
| 1870 | * longer thus better match. */ |
| 1871 | middle_match_len = 0; |
| 1872 | |
| 1873 | if (middle_match_len == 0) |
| 1874 | i += j; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1875 | found_one = TRUE; |
| 1876 | break; |
| 1877 | } |
| 1878 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1879 | if (middle_match_len != 0) |
| 1880 | { |
| 1881 | /* Use the previously found middle match after failing to find a |
| 1882 | * match with an end. */ |
| 1883 | if (!got_com && flags != NULL) |
| 1884 | *flags = saved_flags; |
| 1885 | i += middle_match_len; |
| 1886 | found_one = TRUE; |
| 1887 | } |
| 1888 | |
| 1889 | /* No match found, stop scanning. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1890 | if (!found_one) |
| 1891 | break; |
| 1892 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1893 | result = i; |
| 1894 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1895 | /* Include any trailing white space. */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1896 | while (VIM_ISWHITE(line[i])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1897 | ++i; |
| 1898 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1899 | if (include_space) |
| 1900 | result = i; |
| 1901 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1902 | /* If this comment doesn't nest, stop here. */ |
| 1903 | got_com = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1904 | if (vim_strchr(part_buf, COM_NEST) == NULL) |
| 1905 | break; |
| 1906 | } |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1907 | return result; |
| 1908 | } |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1909 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1910 | /* |
| 1911 | * Return the offset at which the last comment in line starts. If there is no |
| 1912 | * comment in the whole line, -1 is returned. |
| 1913 | * |
| 1914 | * When "flags" is not null, it is set to point to the flags describing the |
| 1915 | * recognized comment leader. |
| 1916 | */ |
| 1917 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1918 | get_last_leader_offset(char_u *line, char_u **flags) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1919 | { |
| 1920 | int result = -1; |
| 1921 | int i, j; |
| 1922 | int lower_check_bound = 0; |
| 1923 | char_u *string; |
| 1924 | char_u *com_leader; |
| 1925 | char_u *com_flags; |
| 1926 | char_u *list; |
| 1927 | int found_one; |
| 1928 | char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ |
| 1929 | |
| 1930 | /* |
| 1931 | * Repeat to match several nested comment strings. |
| 1932 | */ |
| 1933 | i = (int)STRLEN(line); |
| 1934 | while (--i >= lower_check_bound) |
| 1935 | { |
| 1936 | /* |
| 1937 | * scan through the 'comments' option for a match |
| 1938 | */ |
| 1939 | found_one = FALSE; |
| 1940 | for (list = curbuf->b_p_com; *list; ) |
| 1941 | { |
| 1942 | char_u *flags_save = list; |
| 1943 | |
| 1944 | /* |
| 1945 | * Get one option part into part_buf[]. Advance list to next one. |
| 1946 | * put string at start of string. |
| 1947 | */ |
| 1948 | (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
| 1949 | string = vim_strchr(part_buf, ':'); |
| 1950 | if (string == NULL) /* If everything is fine, this cannot actually |
| 1951 | * happen. */ |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1952 | continue; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1953 | *string++ = NUL; /* Isolate flags from string. */ |
| 1954 | com_leader = string; |
| 1955 | |
| 1956 | /* |
| 1957 | * Line contents and string must match. |
| 1958 | * When string starts with white space, must have some white space |
| 1959 | * (but the amount does not need to match, there might be a mix of |
| 1960 | * TABs and spaces). |
| 1961 | */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1962 | if (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1963 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1964 | if (i == 0 || !VIM_ISWHITE(line[i - 1])) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1965 | continue; |
Bram Moolenaar | 5393281 | 2018-12-07 21:08:49 +0100 | [diff] [blame] | 1966 | while (VIM_ISWHITE(*string)) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1967 | ++string; |
| 1968 | } |
| 1969 | for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) |
| 1970 | /* do nothing */; |
| 1971 | if (string[j] != NUL) |
| 1972 | continue; |
| 1973 | |
| 1974 | /* |
| 1975 | * When 'b' flag used, there must be white space or an |
| 1976 | * end-of-line after the string in the line. |
| 1977 | */ |
| 1978 | if (vim_strchr(part_buf, COM_BLANK) != NULL |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1979 | && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1980 | continue; |
Bram Moolenaar | 5393281 | 2018-12-07 21:08:49 +0100 | [diff] [blame] | 1981 | |
Bram Moolenaar | 4af7259 | 2018-12-09 15:00:52 +0100 | [diff] [blame] | 1982 | if (vim_strchr(part_buf, COM_MIDDLE) != NULL) |
Bram Moolenaar | 5393281 | 2018-12-07 21:08:49 +0100 | [diff] [blame] | 1983 | { |
Bram Moolenaar | 4af7259 | 2018-12-09 15:00:52 +0100 | [diff] [blame] | 1984 | // For a middlepart comment, only consider it to match if |
| 1985 | // everything before the current position in the line is |
| 1986 | // whitespace. Otherwise we would think we are inside a |
| 1987 | // comment if the middle part appears somewhere in the middle |
| 1988 | // of the line. E.g. for C the "*" appears often. |
Bram Moolenaar | 5393281 | 2018-12-07 21:08:49 +0100 | [diff] [blame] | 1989 | for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++) |
| 1990 | ; |
| 1991 | if (j < i) |
| 1992 | continue; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | /* |
| 1996 | * We have found a match, stop searching. |
| 1997 | */ |
| 1998 | found_one = TRUE; |
| 1999 | |
| 2000 | if (flags) |
| 2001 | *flags = flags_save; |
| 2002 | com_flags = flags_save; |
| 2003 | |
| 2004 | break; |
| 2005 | } |
| 2006 | |
| 2007 | if (found_one) |
| 2008 | { |
| 2009 | char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */ |
| 2010 | int len1, len2, off; |
| 2011 | |
| 2012 | result = i; |
| 2013 | /* |
| 2014 | * If this comment nests, continue searching. |
| 2015 | */ |
| 2016 | if (vim_strchr(part_buf, COM_NEST) != NULL) |
| 2017 | continue; |
| 2018 | |
| 2019 | lower_check_bound = i; |
| 2020 | |
| 2021 | /* Let's verify whether the comment leader found is a substring |
| 2022 | * of other comment leaders. If it is, let's adjust the |
| 2023 | * lower_check_bound so that we make sure that we have determined |
| 2024 | * the comment leader correctly. |
| 2025 | */ |
| 2026 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2027 | while (VIM_ISWHITE(*com_leader)) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2028 | ++com_leader; |
| 2029 | len1 = (int)STRLEN(com_leader); |
| 2030 | |
| 2031 | for (list = curbuf->b_p_com; *list; ) |
| 2032 | { |
| 2033 | char_u *flags_save = list; |
| 2034 | |
| 2035 | (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ","); |
| 2036 | if (flags_save == com_flags) |
| 2037 | continue; |
| 2038 | string = vim_strchr(part_buf2, ':'); |
| 2039 | ++string; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2040 | while (VIM_ISWHITE(*string)) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2041 | ++string; |
| 2042 | len2 = (int)STRLEN(string); |
| 2043 | if (len2 == 0) |
| 2044 | continue; |
| 2045 | |
| 2046 | /* Now we have to verify whether string ends with a substring |
| 2047 | * beginning the com_leader. */ |
| 2048 | for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;) |
| 2049 | { |
| 2050 | --off; |
| 2051 | if (!STRNCMP(string + off, com_leader, len2 - off)) |
| 2052 | { |
| 2053 | if (i - off < lower_check_bound) |
| 2054 | lower_check_bound = i - off; |
| 2055 | } |
| 2056 | } |
| 2057 | } |
| 2058 | } |
| 2059 | } |
| 2060 | return result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2061 | } |
| 2062 | #endif |
| 2063 | |
| 2064 | /* |
| 2065 | * Return the number of window lines occupied by buffer line "lnum". |
| 2066 | */ |
| 2067 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2068 | plines(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2069 | { |
| 2070 | return plines_win(curwin, lnum, TRUE); |
| 2071 | } |
| 2072 | |
| 2073 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2074 | plines_win( |
| 2075 | win_T *wp, |
| 2076 | linenr_T lnum, |
| 2077 | int winheight) /* when TRUE limit to window height */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2078 | { |
| 2079 | #if defined(FEAT_DIFF) || defined(PROTO) |
| 2080 | /* Check for filler lines above this buffer line. When folded the result |
| 2081 | * is one line anyway. */ |
| 2082 | return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum); |
| 2083 | } |
| 2084 | |
| 2085 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2086 | plines_nofill(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2087 | { |
| 2088 | return plines_win_nofill(curwin, lnum, TRUE); |
| 2089 | } |
| 2090 | |
| 2091 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2092 | plines_win_nofill( |
| 2093 | win_T *wp, |
| 2094 | linenr_T lnum, |
| 2095 | int winheight) /* when TRUE limit to window height */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2096 | { |
| 2097 | #endif |
| 2098 | int lines; |
| 2099 | |
| 2100 | if (!wp->w_p_wrap) |
| 2101 | return 1; |
| 2102 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2103 | if (wp->w_width == 0) |
| 2104 | return 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2105 | |
| 2106 | #ifdef FEAT_FOLDING |
| 2107 | /* A folded lines is handled just like an empty line. */ |
| 2108 | /* NOTE: Caller must handle lines that are MAYBE folded. */ |
| 2109 | if (lineFolded(wp, lnum) == TRUE) |
| 2110 | return 1; |
| 2111 | #endif |
| 2112 | |
| 2113 | lines = plines_win_nofold(wp, lnum); |
| 2114 | if (winheight > 0 && lines > wp->w_height) |
| 2115 | return (int)wp->w_height; |
| 2116 | return lines; |
| 2117 | } |
| 2118 | |
| 2119 | /* |
| 2120 | * Return number of window lines physical line "lnum" will occupy in window |
| 2121 | * "wp". Does not care about folding, 'wrap' or 'diff'. |
| 2122 | */ |
| 2123 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2124 | plines_win_nofold(win_T *wp, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2125 | { |
| 2126 | char_u *s; |
| 2127 | long col; |
| 2128 | int width; |
| 2129 | |
| 2130 | s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
| 2131 | if (*s == NUL) /* empty line */ |
| 2132 | return 1; |
| 2133 | col = win_linetabsize(wp, s, (colnr_T)MAXCOL); |
| 2134 | |
| 2135 | /* |
| 2136 | * If list mode is on, then the '$' at the end of the line may take up one |
| 2137 | * extra column. |
| 2138 | */ |
| 2139 | if (wp->w_p_list && lcs_eol != NUL) |
| 2140 | col += 1; |
| 2141 | |
| 2142 | /* |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 2143 | * Add column offset for 'number', 'relativenumber' and 'foldcolumn'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2144 | */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2145 | width = wp->w_width - win_col_off(wp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2146 | if (width <= 0) |
| 2147 | return 32000; |
| 2148 | if (col <= width) |
| 2149 | return 1; |
| 2150 | col -= width; |
| 2151 | width += win_col_off2(wp); |
| 2152 | return (col + (width - 1)) / width + 1; |
| 2153 | } |
| 2154 | |
| 2155 | /* |
| 2156 | * Like plines_win(), but only reports the number of physical screen lines |
| 2157 | * used from the start of the line to the given column number. |
| 2158 | */ |
| 2159 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2160 | plines_win_col(win_T *wp, linenr_T lnum, long column) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2161 | { |
| 2162 | long col; |
| 2163 | char_u *s; |
| 2164 | int lines = 0; |
| 2165 | int width; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2166 | char_u *line; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2167 | |
| 2168 | #ifdef FEAT_DIFF |
| 2169 | /* Check for filler lines above this buffer line. When folded the result |
| 2170 | * is one line anyway. */ |
| 2171 | lines = diff_check_fill(wp, lnum); |
| 2172 | #endif |
| 2173 | |
| 2174 | if (!wp->w_p_wrap) |
| 2175 | return lines + 1; |
| 2176 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2177 | if (wp->w_width == 0) |
| 2178 | return lines + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2179 | |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2180 | line = s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2181 | |
| 2182 | col = 0; |
| 2183 | while (*s != NUL && --column >= 0) |
| 2184 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2185 | col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL); |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 2186 | MB_PTR_ADV(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | /* |
| 2190 | * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in |
| 2191 | * INSERT mode, then col must be adjusted so that it represents the last |
| 2192 | * screen position of the TAB. This only fixes an error when the TAB wraps |
| 2193 | * from one screen line to the next (when 'columns' is not a multiple of |
| 2194 | * 'ts') -- webb. |
| 2195 | */ |
| 2196 | if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1)) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2197 | col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2198 | |
| 2199 | /* |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 2200 | * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2201 | */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2202 | width = wp->w_width - win_col_off(wp); |
Bram Moolenaar | 2647063 | 2006-10-24 19:12:40 +0000 | [diff] [blame] | 2203 | if (width <= 0) |
| 2204 | return 9999; |
| 2205 | |
| 2206 | lines += 1; |
| 2207 | if (col > width) |
| 2208 | lines += (col - width) / (width + win_col_off2(wp)) + 1; |
| 2209 | return lines; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2213 | plines_m_win(win_T *wp, linenr_T first, linenr_T last) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2214 | { |
| 2215 | int count = 0; |
| 2216 | |
| 2217 | while (first <= last) |
| 2218 | { |
| 2219 | #ifdef FEAT_FOLDING |
| 2220 | int x; |
| 2221 | |
| 2222 | /* Check if there are any really folded lines, but also included lines |
| 2223 | * that are maybe folded. */ |
| 2224 | x = foldedCount(wp, first, NULL); |
| 2225 | if (x > 0) |
| 2226 | { |
| 2227 | ++count; /* count 1 for "+-- folded" line */ |
| 2228 | first += x; |
| 2229 | } |
| 2230 | else |
| 2231 | #endif |
| 2232 | { |
| 2233 | #ifdef FEAT_DIFF |
| 2234 | if (first == wp->w_topline) |
| 2235 | count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill; |
| 2236 | else |
| 2237 | #endif |
| 2238 | count += plines_win(wp, first, TRUE); |
| 2239 | ++first; |
| 2240 | } |
| 2241 | } |
| 2242 | return (count); |
| 2243 | } |
| 2244 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2245 | /* |
| 2246 | * Insert string "p" at the cursor position. Stops at a NUL byte. |
| 2247 | * Handles Replace mode and multi-byte characters. |
| 2248 | */ |
| 2249 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2250 | ins_bytes(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2251 | { |
| 2252 | ins_bytes_len(p, (int)STRLEN(p)); |
| 2253 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2254 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2255 | /* |
| 2256 | * Insert string "p" with length "len" at the cursor position. |
| 2257 | * Handles Replace mode and multi-byte characters. |
| 2258 | */ |
| 2259 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2260 | ins_bytes_len(char_u *p, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2261 | { |
| 2262 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2263 | int n; |
| 2264 | |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2265 | if (has_mbyte) |
| 2266 | for (i = 0; i < len; i += n) |
| 2267 | { |
| 2268 | if (enc_utf8) |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2269 | // avoid reading past p[len] |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2270 | n = utfc_ptr2len_len(p + i, len - i); |
| 2271 | else |
| 2272 | n = (*mb_ptr2len)(p + i); |
| 2273 | ins_char_bytes(p + i, n); |
| 2274 | } |
| 2275 | else |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2276 | for (i = 0; i < len; ++i) |
| 2277 | ins_char(p[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2278 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2279 | |
| 2280 | /* |
| 2281 | * Insert or replace a single character at the cursor position. |
| 2282 | * When in REPLACE or VREPLACE mode, replace any existing character. |
| 2283 | * Caller must have prepared for undo. |
| 2284 | * For multi-byte characters we get the whole character, the caller must |
| 2285 | * convert bytes to a character. |
| 2286 | */ |
| 2287 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2288 | ins_char(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2289 | { |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 2290 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | fc3abf4 | 2019-01-24 15:54:21 +0100 | [diff] [blame] | 2291 | int n = (*mb_char2bytes)(c, buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2292 | |
| 2293 | /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte. |
| 2294 | * Happens for CTRL-Vu9900. */ |
| 2295 | if (buf[0] == 0) |
| 2296 | buf[0] = '\n'; |
| 2297 | |
| 2298 | ins_char_bytes(buf, n); |
| 2299 | } |
| 2300 | |
| 2301 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2302 | ins_char_bytes(char_u *buf, int charlen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2303 | { |
| 2304 | int c = buf[0]; |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2305 | int newlen; // nr of bytes inserted |
| 2306 | int oldlen; // nr of bytes deleted (0 when not replacing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2307 | char_u *p; |
| 2308 | char_u *newp; |
| 2309 | char_u *oldp; |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2310 | int linelen; // length of old line including NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2311 | colnr_T col; |
| 2312 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2313 | int i; |
| 2314 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2315 | /* Break tabs if needed. */ |
| 2316 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 2317 | coladvance_force(getviscol()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2318 | |
| 2319 | col = curwin->w_cursor.col; |
| 2320 | oldp = ml_get(lnum); |
| 2321 | linelen = (int)STRLEN(oldp) + 1; |
| 2322 | |
| 2323 | /* The lengths default to the values for when not replacing. */ |
| 2324 | oldlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2325 | newlen = charlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2326 | |
| 2327 | if (State & REPLACE_FLAG) |
| 2328 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2329 | if (State & VREPLACE_FLAG) |
| 2330 | { |
| 2331 | colnr_T new_vcol = 0; /* init for GCC */ |
| 2332 | colnr_T vcol; |
| 2333 | int old_list; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2334 | |
| 2335 | /* |
| 2336 | * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag. |
| 2337 | * Returns the old value of list, so when finished, |
| 2338 | * curwin->w_p_list should be set back to this. |
| 2339 | */ |
| 2340 | old_list = curwin->w_p_list; |
| 2341 | if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 2342 | curwin->w_p_list = FALSE; |
| 2343 | |
| 2344 | /* |
| 2345 | * In virtual replace mode each character may replace one or more |
| 2346 | * characters (zero if it's a TAB). Count the number of bytes to |
| 2347 | * be deleted to make room for the new character, counting screen |
| 2348 | * cells. May result in adding spaces to fill a gap. |
| 2349 | */ |
| 2350 | getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2351 | new_vcol = vcol + chartabsize(buf, vcol); |
| 2352 | while (oldp[col + oldlen] != NUL && vcol < new_vcol) |
| 2353 | { |
| 2354 | vcol += chartabsize(oldp + col + oldlen, vcol); |
| 2355 | /* Don't need to remove a TAB that takes us to the right |
| 2356 | * position. */ |
| 2357 | if (vcol > new_vcol && oldp[col + oldlen] == TAB) |
| 2358 | break; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2359 | oldlen += (*mb_ptr2len)(oldp + col + oldlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2360 | /* Deleted a bit too much, insert spaces. */ |
| 2361 | if (vcol > new_vcol) |
| 2362 | newlen += vcol - new_vcol; |
| 2363 | } |
| 2364 | curwin->w_p_list = old_list; |
| 2365 | } |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2366 | else if (oldp[col] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2367 | { |
| 2368 | /* normal replace */ |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2369 | oldlen = (*mb_ptr2len)(oldp + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | |
| 2373 | /* Push the replaced bytes onto the replace stack, so that they can be |
| 2374 | * put back when BS is used. The bytes of a multi-byte character are |
| 2375 | * done the other way around, so that the first byte is popped off |
| 2376 | * first (it tells the byte length of the character). */ |
| 2377 | replace_push(NUL); |
| 2378 | for (i = 0; i < oldlen; ++i) |
| 2379 | { |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 2380 | if (has_mbyte) |
| 2381 | i += replace_push_mb(oldp + col + i) - 1; |
| 2382 | else |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 2383 | replace_push(oldp[col + i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | newp = alloc_check((unsigned)(linelen + newlen - oldlen)); |
| 2388 | if (newp == NULL) |
| 2389 | return; |
| 2390 | |
| 2391 | /* Copy bytes before the cursor. */ |
| 2392 | if (col > 0) |
| 2393 | mch_memmove(newp, oldp, (size_t)col); |
| 2394 | |
| 2395 | /* Copy bytes after the changed character(s). */ |
| 2396 | p = newp + col; |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 2397 | if (linelen > col + oldlen) |
| 2398 | mch_memmove(p + newlen, oldp + col + oldlen, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2399 | (size_t)(linelen - col - oldlen)); |
| 2400 | |
| 2401 | /* Insert or overwrite the new character. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2402 | mch_memmove(p, buf, charlen); |
| 2403 | i = charlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2404 | |
| 2405 | /* Fill with spaces when necessary. */ |
| 2406 | while (i < newlen) |
| 2407 | p[i++] = ' '; |
| 2408 | |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2409 | // Replace the line in the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2410 | ml_replace(lnum, newp, FALSE); |
| 2411 | |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2412 | // mark the buffer as changed and prepare for displaying |
| 2413 | inserted_bytes(lnum, col, newlen - oldlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2414 | |
| 2415 | /* |
| 2416 | * If we're in Insert or Replace mode and 'showmatch' is set, then briefly |
| 2417 | * show the match for right parens and braces. |
| 2418 | */ |
| 2419 | if (p_sm && (State & INSERT) |
| 2420 | && msg_silent == 0 |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 2421 | #ifdef FEAT_INS_EXPAND |
| 2422 | && !ins_compl_active() |
| 2423 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2424 | ) |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2425 | { |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2426 | if (has_mbyte) |
| 2427 | showmatch(mb_ptr2char(buf)); |
| 2428 | else |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2429 | showmatch(c); |
| 2430 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2431 | |
| 2432 | #ifdef FEAT_RIGHTLEFT |
| 2433 | if (!p_ri || (State & REPLACE_FLAG)) |
| 2434 | #endif |
| 2435 | { |
| 2436 | /* Normal insert: move cursor right */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2437 | curwin->w_cursor.col += charlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2438 | } |
| 2439 | /* |
| 2440 | * TODO: should try to update w_row here, to avoid recomputing it later. |
| 2441 | */ |
| 2442 | } |
| 2443 | |
| 2444 | /* |
| 2445 | * Insert a string at the cursor position. |
| 2446 | * Note: Does NOT handle Replace mode. |
| 2447 | * Caller must have prepared for undo. |
| 2448 | */ |
| 2449 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2450 | ins_str(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2451 | { |
| 2452 | char_u *oldp, *newp; |
| 2453 | int newlen = (int)STRLEN(s); |
| 2454 | int oldlen; |
| 2455 | colnr_T col; |
| 2456 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2457 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2458 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 2459 | coladvance_force(getviscol()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2460 | |
| 2461 | col = curwin->w_cursor.col; |
| 2462 | oldp = ml_get(lnum); |
| 2463 | oldlen = (int)STRLEN(oldp); |
| 2464 | |
| 2465 | newp = alloc_check((unsigned)(oldlen + newlen + 1)); |
| 2466 | if (newp == NULL) |
| 2467 | return; |
| 2468 | if (col > 0) |
| 2469 | mch_memmove(newp, oldp, (size_t)col); |
| 2470 | mch_memmove(newp + col, s, (size_t)newlen); |
| 2471 | mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1)); |
| 2472 | ml_replace(lnum, newp, FALSE); |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2473 | inserted_bytes(lnum, col, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2474 | curwin->w_cursor.col += newlen; |
| 2475 | } |
| 2476 | |
| 2477 | /* |
| 2478 | * Delete one character under the cursor. |
| 2479 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 2480 | * Caller must have prepared for undo. |
| 2481 | * |
| 2482 | * return FAIL for failure, OK otherwise |
| 2483 | */ |
| 2484 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2485 | del_char(int fixpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2486 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2487 | if (has_mbyte) |
| 2488 | { |
| 2489 | /* Make sure the cursor is at the start of a character. */ |
| 2490 | mb_adjust_cursor(); |
| 2491 | if (*ml_get_cursor() == NUL) |
| 2492 | return FAIL; |
| 2493 | return del_chars(1L, fixpos); |
| 2494 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2495 | return del_bytes(1L, fixpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2498 | /* |
| 2499 | * Like del_bytes(), but delete characters instead of bytes. |
| 2500 | */ |
| 2501 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2502 | del_chars(long count, int fixpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2503 | { |
| 2504 | long bytes = 0; |
| 2505 | long i; |
| 2506 | char_u *p; |
| 2507 | int l; |
| 2508 | |
| 2509 | p = ml_get_cursor(); |
| 2510 | for (i = 0; i < count && *p != NUL; ++i) |
| 2511 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2512 | l = (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2513 | bytes += l; |
| 2514 | p += l; |
| 2515 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2516 | return del_bytes(bytes, fixpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2517 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2518 | |
| 2519 | /* |
| 2520 | * Delete "count" bytes under the cursor. |
| 2521 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 2522 | * Caller must have prepared for undo. |
| 2523 | * |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2524 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2525 | */ |
| 2526 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2527 | del_bytes( |
| 2528 | long count, |
| 2529 | int fixpos_arg, |
| 2530 | int use_delcombine UNUSED) /* 'delcombine' option applies */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2531 | { |
| 2532 | char_u *oldp, *newp; |
| 2533 | colnr_T oldlen; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2534 | colnr_T newlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2535 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2536 | colnr_T col = curwin->w_cursor.col; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2537 | int alloc_newp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2538 | long movelen; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2539 | int fixpos = fixpos_arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2540 | |
| 2541 | oldp = ml_get(lnum); |
| 2542 | oldlen = (int)STRLEN(oldp); |
| 2543 | |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2544 | /* 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] | 2545 | if (col >= oldlen) |
| 2546 | return FAIL; |
| 2547 | |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2548 | /* If "count" is zero there is nothing to do. */ |
| 2549 | if (count == 0) |
| 2550 | return OK; |
| 2551 | |
| 2552 | /* If "count" is negative the caller must be doing something wrong. */ |
| 2553 | if (count < 1) |
| 2554 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2555 | siemsg("E950: Invalid count for del_bytes(): %ld", count); |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2556 | return FAIL; |
| 2557 | } |
| 2558 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2559 | /* If 'delcombine' is set and deleting (less than) one character, only |
| 2560 | * delete the last combining character. */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2561 | if (p_deco && use_delcombine && enc_utf8 |
| 2562 | && utfc_ptr2len(oldp + col) >= count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2563 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2564 | int cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2565 | int n; |
| 2566 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2567 | (void)utfc_ptr2char(oldp + col, cc); |
| 2568 | if (cc[0] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2569 | { |
| 2570 | /* Find the last composing char, there can be several. */ |
| 2571 | n = col; |
| 2572 | do |
| 2573 | { |
| 2574 | col = n; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2575 | count = utf_ptr2len(oldp + n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2576 | n += count; |
| 2577 | } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n)); |
| 2578 | fixpos = 0; |
| 2579 | } |
| 2580 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2581 | |
| 2582 | /* |
| 2583 | * When count is too big, reduce it. |
| 2584 | */ |
| 2585 | movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */ |
| 2586 | if (movelen <= 1) |
| 2587 | { |
| 2588 | /* |
| 2589 | * 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] | 2590 | * fixpos is TRUE, we don't want to end up positioned at the NUL, |
| 2591 | * unless "restart_edit" is set or 'virtualedit' contains "onemore". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2592 | */ |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2593 | if (col > 0 && fixpos && restart_edit == 0 |
Bram Moolenaar | 29ddebe | 2019-01-26 17:28:26 +0100 | [diff] [blame] | 2594 | && (ve_flags & VE_ONEMORE) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2595 | { |
| 2596 | --curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2597 | curwin->w_cursor.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2598 | if (has_mbyte) |
| 2599 | curwin->w_cursor.col -= |
| 2600 | (*mb_head_off)(oldp, oldp + curwin->w_cursor.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2601 | } |
| 2602 | count = oldlen - col; |
| 2603 | movelen = 1; |
| 2604 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2605 | newlen = oldlen - count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2606 | |
| 2607 | /* |
| 2608 | * If the old line has been allocated the deletion can be done in the |
| 2609 | * existing line. Otherwise a new line has to be allocated |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2610 | * Can't do this when using Netbeans, because we would need to invoke |
| 2611 | * netbeans_removed(), which deallocates the line. Let ml_replace() take |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 2612 | * care of notifying Netbeans. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2613 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2614 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2615 | if (netbeans_active()) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2616 | alloc_newp = TRUE; |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2617 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2618 | #endif |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2619 | alloc_newp = !ml_line_alloced(); // check if oldp was allocated |
| 2620 | if (!alloc_newp) |
| 2621 | newp = oldp; // use same allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2622 | else |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2623 | { // need to allocate a new line |
| 2624 | newp = alloc((unsigned)(newlen + 1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2625 | if (newp == NULL) |
| 2626 | return FAIL; |
| 2627 | mch_memmove(newp, oldp, (size_t)col); |
| 2628 | } |
| 2629 | mch_memmove(newp + col, oldp + col + count, (size_t)movelen); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2630 | if (alloc_newp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2631 | ml_replace(lnum, newp, FALSE); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2632 | #ifdef FEAT_TEXT_PROP |
| 2633 | else |
| 2634 | { |
| 2635 | // Also move any following text properties. |
| 2636 | if (oldlen + 1 < curbuf->b_ml.ml_line_len) |
| 2637 | mch_memmove(newp + newlen + 1, oldp + oldlen + 1, |
| 2638 | (size_t)curbuf->b_ml.ml_line_len - oldlen - 1); |
| 2639 | curbuf->b_ml.ml_line_len -= count; |
| 2640 | } |
| 2641 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2642 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2643 | // mark the buffer as changed and prepare for displaying |
Bram Moolenaar | 33c8ca9 | 2019-01-02 18:00:27 +0100 | [diff] [blame] | 2644 | inserted_bytes(lnum, curwin->w_cursor.col, -count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2645 | |
| 2646 | return OK; |
| 2647 | } |
| 2648 | |
| 2649 | /* |
| 2650 | * Delete from cursor to end of line. |
| 2651 | * Caller must have prepared for undo. |
| 2652 | * |
| 2653 | * return FAIL for failure, OK otherwise |
| 2654 | */ |
| 2655 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2656 | truncate_line( |
| 2657 | int fixpos) /* if TRUE fix the cursor position when done */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2658 | { |
| 2659 | char_u *newp; |
| 2660 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2661 | colnr_T col = curwin->w_cursor.col; |
| 2662 | |
| 2663 | if (col == 0) |
| 2664 | newp = vim_strsave((char_u *)""); |
| 2665 | else |
| 2666 | newp = vim_strnsave(ml_get(lnum), col); |
| 2667 | |
| 2668 | if (newp == NULL) |
| 2669 | return FAIL; |
| 2670 | |
| 2671 | ml_replace(lnum, newp, FALSE); |
| 2672 | |
| 2673 | /* mark the buffer as changed and prepare for displaying */ |
| 2674 | changed_bytes(lnum, curwin->w_cursor.col); |
| 2675 | |
| 2676 | /* |
| 2677 | * If "fixpos" is TRUE we don't want to end up positioned at the NUL. |
| 2678 | */ |
| 2679 | if (fixpos && curwin->w_cursor.col > 0) |
| 2680 | --curwin->w_cursor.col; |
| 2681 | |
| 2682 | return OK; |
| 2683 | } |
| 2684 | |
| 2685 | /* |
| 2686 | * Delete "nlines" lines at the cursor. |
| 2687 | * Saves the lines for undo first if "undo" is TRUE. |
| 2688 | */ |
| 2689 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2690 | del_lines( |
| 2691 | long nlines, /* number of lines to delete */ |
| 2692 | int undo) /* if TRUE, prepare for undo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2693 | { |
| 2694 | long n; |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2695 | linenr_T first = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2696 | |
| 2697 | if (nlines <= 0) |
| 2698 | return; |
| 2699 | |
| 2700 | /* save the deleted lines for undo */ |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2701 | if (undo && u_savedel(first, nlines) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2702 | return; |
| 2703 | |
| 2704 | for (n = 0; n < nlines; ) |
| 2705 | { |
| 2706 | if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ |
| 2707 | break; |
| 2708 | |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2709 | ml_delete(first, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2710 | ++n; |
| 2711 | |
| 2712 | /* If we delete the last line in the file, stop */ |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2713 | if (first > curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2714 | break; |
| 2715 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2716 | |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2717 | /* Correct the cursor position before calling deleted_lines_mark(), it may |
| 2718 | * trigger a callback to display the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2719 | curwin->w_cursor.col = 0; |
| 2720 | check_cursor_lnum(); |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2721 | |
| 2722 | /* adjust marks, mark the buffer as changed and prepare for displaying */ |
| 2723 | deleted_lines_mark(first, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2727 | gchar_pos(pos_T *pos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2728 | { |
Bram Moolenaar | 8ada6aa | 2017-12-19 21:23:21 +0100 | [diff] [blame] | 2729 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2730 | |
Bram Moolenaar | 8ada6aa | 2017-12-19 21:23:21 +0100 | [diff] [blame] | 2731 | /* When searching columns is sometimes put at the end of a line. */ |
| 2732 | if (pos->col == MAXCOL) |
| 2733 | return NUL; |
| 2734 | ptr = ml_get_pos(pos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2735 | if (has_mbyte) |
| 2736 | return (*mb_ptr2char)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2737 | return (int)*ptr; |
| 2738 | } |
| 2739 | |
| 2740 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2741 | gchar_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2742 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2743 | if (has_mbyte) |
| 2744 | return (*mb_ptr2char)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2745 | return (int)*ml_get_cursor(); |
| 2746 | } |
| 2747 | |
| 2748 | /* |
| 2749 | * Write a character at the current cursor position. |
| 2750 | * It is directly written into the block. |
| 2751 | */ |
| 2752 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2753 | pchar_cursor(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2754 | { |
| 2755 | *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) |
| 2756 | + curwin->w_cursor.col) = c; |
| 2757 | } |
| 2758 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2759 | /* |
| 2760 | * When extra == 0: Return TRUE if the cursor is before or on the first |
| 2761 | * non-blank in the line. |
| 2762 | * When extra == 1: Return TRUE if the cursor is before the first non-blank in |
| 2763 | * the line. |
| 2764 | */ |
| 2765 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2766 | inindent(int extra) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2767 | { |
| 2768 | char_u *ptr; |
| 2769 | colnr_T col; |
| 2770 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2771 | for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2772 | ++ptr; |
| 2773 | if (col >= curwin->w_cursor.col + extra) |
| 2774 | return TRUE; |
| 2775 | else |
| 2776 | return FALSE; |
| 2777 | } |
| 2778 | |
| 2779 | /* |
| 2780 | * Skip to next part of an option argument: Skip space and comma. |
| 2781 | */ |
| 2782 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2783 | skip_to_option_part(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2784 | { |
| 2785 | if (*p == ',') |
| 2786 | ++p; |
| 2787 | while (*p == ' ') |
| 2788 | ++p; |
| 2789 | return p; |
| 2790 | } |
| 2791 | |
| 2792 | /* |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2793 | * Call this function when something in the current buffer is changed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2794 | * |
| 2795 | * Most often called through changed_bytes() and changed_lines(), which also |
| 2796 | * mark the area of the display to be redrawn. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2797 | * |
| 2798 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2799 | */ |
| 2800 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2801 | changed(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2802 | { |
| 2803 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 2804 | if (p_imst == IM_ON_THE_SPOT) |
| 2805 | { |
| 2806 | /* The text of the preediting area is inserted, but this doesn't |
| 2807 | * mean a change of the buffer yet. That is delayed until the |
| 2808 | * text is committed. (this means preedit becomes empty) */ |
| 2809 | if (im_is_preediting() && !xim_changed_while_preediting) |
| 2810 | return; |
| 2811 | xim_changed_while_preediting = FALSE; |
| 2812 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2813 | #endif |
| 2814 | |
| 2815 | if (!curbuf->b_changed) |
| 2816 | { |
| 2817 | int save_msg_scroll = msg_scroll; |
| 2818 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2819 | /* Give a warning about changing a read-only file. This may also |
| 2820 | * check-out the file, thus change "curbuf"! */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2821 | change_warning(0); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2822 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2823 | /* Create a swap file if that is wanted. |
| 2824 | * Don't do this for "nofile" and "nowrite" buffer types. */ |
| 2825 | if (curbuf->b_may_swap |
| 2826 | #ifdef FEAT_QUICKFIX |
| 2827 | && !bt_dontwrite(curbuf) |
| 2828 | #endif |
| 2829 | ) |
| 2830 | { |
Bram Moolenaar | b01f357 | 2016-01-15 15:17:04 +0100 | [diff] [blame] | 2831 | int save_need_wait_return = need_wait_return; |
| 2832 | |
| 2833 | need_wait_return = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2834 | ml_open_file(curbuf); |
| 2835 | |
| 2836 | /* The ml_open_file() can cause an ATTENTION message. |
| 2837 | * Wait two seconds, to make sure the user reads this unexpected |
| 2838 | * message. Since we could be anywhere, call wait_return() now, |
| 2839 | * and don't let the emsg() set msg_scroll. */ |
| 2840 | if (need_wait_return && emsg_silent == 0) |
| 2841 | { |
| 2842 | out_flush(); |
| 2843 | ui_delay(2000L, TRUE); |
| 2844 | wait_return(TRUE); |
| 2845 | msg_scroll = save_msg_scroll; |
| 2846 | } |
Bram Moolenaar | b01f357 | 2016-01-15 15:17:04 +0100 | [diff] [blame] | 2847 | else |
| 2848 | need_wait_return = save_need_wait_return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2849 | } |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2850 | changed_int(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2851 | } |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 2852 | ++CHANGEDTICK(curbuf); |
Bram Moolenaar | 4a7d2d3 | 2019-02-21 16:25:50 +0100 | [diff] [blame] | 2853 | |
| 2854 | #ifdef FEAT_SEARCH_EXTRA |
| 2855 | // If a pattern is highlighted, the position may now be invalid. |
| 2856 | highlight_match = FALSE; |
| 2857 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2860 | /* |
| 2861 | * Internal part of changed(), no user interaction. |
| 2862 | */ |
| 2863 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2864 | changed_int(void) |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2865 | { |
| 2866 | curbuf->b_changed = TRUE; |
| 2867 | ml_setflags(curbuf); |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2868 | check_status(curbuf); |
| 2869 | redraw_tabline = TRUE; |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2870 | #ifdef FEAT_TITLE |
| 2871 | need_maketitle = TRUE; /* set window title later */ |
| 2872 | #endif |
| 2873 | } |
| 2874 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 2875 | static void changedOneline(buf_T *buf, linenr_T lnum); |
| 2876 | static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra); |
| 2877 | 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] | 2878 | |
| 2879 | /* |
| 2880 | * Changed bytes within a single line for the current buffer. |
| 2881 | * - marks the windows on this buffer to be redisplayed |
| 2882 | * - marks the buffer changed by calling changed() |
| 2883 | * - invalidates cached values |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2884 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2885 | */ |
| 2886 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2887 | changed_bytes(linenr_T lnum, colnr_T col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2888 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2889 | changedOneline(curbuf, lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2890 | changed_common(lnum, col, lnum + 1, 0L); |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2891 | |
| 2892 | #ifdef FEAT_DIFF |
| 2893 | /* Diff highlighting in other diff windows may need to be updated too. */ |
| 2894 | if (curwin->w_p_diff) |
| 2895 | { |
| 2896 | win_T *wp; |
| 2897 | linenr_T wlnum; |
| 2898 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 2899 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2900 | if (wp->w_p_diff && wp != curwin) |
| 2901 | { |
| 2902 | redraw_win_later(wp, VALID); |
| 2903 | wlnum = diff_lnum_win(lnum, wp); |
| 2904 | if (wlnum > 0) |
| 2905 | changedOneline(wp->w_buffer, wlnum); |
| 2906 | } |
| 2907 | } |
| 2908 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2911 | /* |
| 2912 | * Like changed_bytes() but also adjust text properties for "added" bytes. |
| 2913 | * When "added" is negative text was deleted. |
| 2914 | */ |
| 2915 | void |
Bram Moolenaar | 402385a | 2019-01-11 14:10:03 +0100 | [diff] [blame] | 2916 | inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED) |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2917 | { |
| 2918 | changed_bytes(lnum, col); |
| 2919 | |
| 2920 | #ifdef FEAT_TEXT_PROP |
| 2921 | if (curbuf->b_has_textprop && added != 0) |
| 2922 | adjust_prop_columns(lnum, col, added); |
| 2923 | #endif |
| 2924 | } |
| 2925 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2926 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2927 | changedOneline(buf_T *buf, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2928 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2929 | if (buf->b_mod_set) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2930 | { |
| 2931 | /* find the maximum area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2932 | if (lnum < buf->b_mod_top) |
| 2933 | buf->b_mod_top = lnum; |
| 2934 | else if (lnum >= buf->b_mod_bot) |
| 2935 | buf->b_mod_bot = lnum + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2936 | } |
| 2937 | else |
| 2938 | { |
| 2939 | /* set the area that must be redisplayed to one line */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2940 | buf->b_mod_set = TRUE; |
| 2941 | buf->b_mod_top = lnum; |
| 2942 | buf->b_mod_bot = lnum + 1; |
| 2943 | buf->b_mod_xlines = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2944 | } |
| 2945 | } |
| 2946 | |
| 2947 | /* |
| 2948 | * Appended "count" lines below line "lnum" in the current buffer. |
| 2949 | * Must be called AFTER the change and after mark_adjust(). |
| 2950 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 2951 | */ |
| 2952 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2953 | appended_lines(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2954 | { |
| 2955 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 2956 | } |
| 2957 | |
| 2958 | /* |
| 2959 | * Like appended_lines(), but adjust marks first. |
| 2960 | */ |
| 2961 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2962 | appended_lines_mark(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2963 | { |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 2964 | /* 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] | 2965 | * be marks there. But it's still needed in diff mode. */ |
| 2966 | if (lnum + count < curbuf->b_ml.ml_line_count |
| 2967 | #ifdef FEAT_DIFF |
| 2968 | || curwin->w_p_diff |
| 2969 | #endif |
| 2970 | ) |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 2971 | mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2972 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 2973 | } |
| 2974 | |
| 2975 | /* |
| 2976 | * Deleted "count" lines at line "lnum" in the current buffer. |
| 2977 | * Must be called AFTER the change and after mark_adjust(). |
| 2978 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 2979 | */ |
| 2980 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2981 | deleted_lines(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2982 | { |
| 2983 | changed_lines(lnum, 0, lnum + count, -count); |
| 2984 | } |
| 2985 | |
| 2986 | /* |
| 2987 | * Like deleted_lines(), but adjust marks first. |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2988 | * Make sure the cursor is on a valid line before calling, a GUI callback may |
| 2989 | * be triggered to display the cursor. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2990 | */ |
| 2991 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2992 | deleted_lines_mark(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2993 | { |
| 2994 | mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count); |
| 2995 | changed_lines(lnum, 0, lnum + count, -count); |
| 2996 | } |
| 2997 | |
| 2998 | /* |
| 2999 | * Changed lines for the current buffer. |
| 3000 | * Must be called AFTER the change and after mark_adjust(). |
| 3001 | * - mark the buffer changed by calling changed() |
| 3002 | * - mark the windows on this buffer to be redisplayed |
| 3003 | * - invalidate cached values |
| 3004 | * "lnum" is the first line that needs displaying, "lnume" the first line |
| 3005 | * below the changed lines (BEFORE the change). |
| 3006 | * When only inserting lines, "lnum" and "lnume" are equal. |
| 3007 | * Takes care of calling changed() and updating b_mod_*. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3008 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3009 | */ |
| 3010 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3011 | changed_lines( |
| 3012 | linenr_T lnum, /* first line with change */ |
| 3013 | colnr_T col, /* column in first line with change */ |
| 3014 | linenr_T lnume, /* line below last changed line */ |
| 3015 | long xtra) /* number of extra lines (negative when deleting) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3016 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3017 | changed_lines_buf(curbuf, lnum, lnume, xtra); |
| 3018 | |
| 3019 | #ifdef FEAT_DIFF |
Bram Moolenaar | e3521d9 | 2018-09-16 14:10:31 +0200 | [diff] [blame] | 3020 | if (xtra == 0 && curwin->w_p_diff && !diff_internal()) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3021 | { |
| 3022 | /* When the number of lines doesn't change then mark_adjust() isn't |
| 3023 | * called and other diff buffers still need to be marked for |
| 3024 | * displaying. */ |
| 3025 | win_T *wp; |
| 3026 | linenr_T wlnum; |
| 3027 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 3028 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3029 | if (wp->w_p_diff && wp != curwin) |
| 3030 | { |
| 3031 | redraw_win_later(wp, VALID); |
| 3032 | wlnum = diff_lnum_win(lnum, wp); |
| 3033 | if (wlnum > 0) |
| 3034 | changed_lines_buf(wp->w_buffer, wlnum, |
| 3035 | lnume - lnum + wlnum, 0L); |
| 3036 | } |
| 3037 | } |
| 3038 | #endif |
| 3039 | |
| 3040 | changed_common(lnum, col, lnume, xtra); |
| 3041 | } |
| 3042 | |
| 3043 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3044 | changed_lines_buf( |
| 3045 | buf_T *buf, |
| 3046 | linenr_T lnum, /* first line with change */ |
| 3047 | linenr_T lnume, /* line below last changed line */ |
| 3048 | long xtra) /* number of extra lines (negative when deleting) */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3049 | { |
| 3050 | if (buf->b_mod_set) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3051 | { |
| 3052 | /* find the maximum area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3053 | if (lnum < buf->b_mod_top) |
| 3054 | buf->b_mod_top = lnum; |
| 3055 | if (lnum < buf->b_mod_bot) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3056 | { |
| 3057 | /* adjust old bot position for xtra lines */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3058 | buf->b_mod_bot += xtra; |
| 3059 | if (buf->b_mod_bot < lnum) |
| 3060 | buf->b_mod_bot = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3061 | } |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3062 | if (lnume + xtra > buf->b_mod_bot) |
| 3063 | buf->b_mod_bot = lnume + xtra; |
| 3064 | buf->b_mod_xlines += xtra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3065 | } |
| 3066 | else |
| 3067 | { |
| 3068 | /* set the area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3069 | buf->b_mod_set = TRUE; |
| 3070 | buf->b_mod_top = lnum; |
| 3071 | buf->b_mod_bot = lnume + xtra; |
| 3072 | buf->b_mod_xlines = xtra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3073 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3076 | /* |
| 3077 | * Common code for when a change is was made. |
| 3078 | * See changed_lines() for the arguments. |
| 3079 | * Careful: may trigger autocommands that reload the buffer. |
| 3080 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3081 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3082 | changed_common( |
| 3083 | linenr_T lnum, |
| 3084 | colnr_T col, |
| 3085 | linenr_T lnume, |
| 3086 | long xtra) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3087 | { |
| 3088 | win_T *wp; |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3089 | tabpage_T *tp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3090 | int i; |
| 3091 | #ifdef FEAT_JUMPLIST |
| 3092 | int cols; |
| 3093 | pos_T *p; |
| 3094 | int add; |
| 3095 | #endif |
| 3096 | |
| 3097 | /* mark the buffer as modified */ |
| 3098 | changed(); |
| 3099 | |
Bram Moolenaar | e3521d9 | 2018-09-16 14:10:31 +0200 | [diff] [blame] | 3100 | #ifdef FEAT_DIFF |
| 3101 | if (curwin->w_p_diff && diff_internal()) |
| 3102 | curtab->tp_diff_update = TRUE; |
| 3103 | #endif |
| 3104 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3105 | /* set the '. mark */ |
| 3106 | if (!cmdmod.keepjumps) |
| 3107 | { |
| 3108 | curbuf->b_last_change.lnum = lnum; |
| 3109 | curbuf->b_last_change.col = col; |
| 3110 | |
| 3111 | #ifdef FEAT_JUMPLIST |
| 3112 | /* Create a new entry if a new undo-able change was started or we |
| 3113 | * don't have an entry yet. */ |
| 3114 | if (curbuf->b_new_change || curbuf->b_changelistlen == 0) |
| 3115 | { |
| 3116 | if (curbuf->b_changelistlen == 0) |
| 3117 | add = TRUE; |
| 3118 | else |
| 3119 | { |
| 3120 | /* Don't create a new entry when the line number is the same |
| 3121 | * as the last one and the column is not too far away. Avoids |
| 3122 | * creating many entries for typing "xxxxx". */ |
| 3123 | p = &curbuf->b_changelist[curbuf->b_changelistlen - 1]; |
| 3124 | if (p->lnum != lnum) |
| 3125 | add = TRUE; |
| 3126 | else |
| 3127 | { |
| 3128 | cols = comp_textwidth(FALSE); |
| 3129 | if (cols == 0) |
| 3130 | cols = 79; |
| 3131 | add = (p->col + cols < col || col + cols < p->col); |
| 3132 | } |
| 3133 | } |
| 3134 | if (add) |
| 3135 | { |
| 3136 | /* This is the first of a new sequence of undo-able changes |
| 3137 | * and it's at some distance of the last change. Use a new |
| 3138 | * position in the changelist. */ |
| 3139 | curbuf->b_new_change = FALSE; |
| 3140 | |
| 3141 | if (curbuf->b_changelistlen == JUMPLISTSIZE) |
| 3142 | { |
| 3143 | /* changelist is full: remove oldest entry */ |
| 3144 | curbuf->b_changelistlen = JUMPLISTSIZE - 1; |
| 3145 | mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1, |
| 3146 | sizeof(pos_T) * (JUMPLISTSIZE - 1)); |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3147 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3148 | { |
| 3149 | /* Correct position in changelist for other windows on |
| 3150 | * this buffer. */ |
| 3151 | if (wp->w_buffer == curbuf && wp->w_changelistidx > 0) |
| 3152 | --wp->w_changelistidx; |
| 3153 | } |
| 3154 | } |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3155 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3156 | { |
| 3157 | /* For other windows, if the position in the changelist is |
| 3158 | * at the end it stays at the end. */ |
| 3159 | if (wp->w_buffer == curbuf |
| 3160 | && wp->w_changelistidx == curbuf->b_changelistlen) |
| 3161 | ++wp->w_changelistidx; |
| 3162 | } |
| 3163 | ++curbuf->b_changelistlen; |
| 3164 | } |
| 3165 | } |
| 3166 | curbuf->b_changelist[curbuf->b_changelistlen - 1] = |
| 3167 | curbuf->b_last_change; |
| 3168 | /* The current window is always after the last change, so that "g," |
| 3169 | * takes you back to it. */ |
| 3170 | curwin->w_changelistidx = curbuf->b_changelistlen; |
| 3171 | #endif |
| 3172 | } |
| 3173 | |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3174 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3175 | { |
| 3176 | if (wp->w_buffer == curbuf) |
| 3177 | { |
| 3178 | /* Mark this window to be redrawn later. */ |
| 3179 | if (wp->w_redr_type < VALID) |
| 3180 | wp->w_redr_type = VALID; |
| 3181 | |
| 3182 | /* Check if a change in the buffer has invalidated the cached |
| 3183 | * values for the cursor. */ |
| 3184 | #ifdef FEAT_FOLDING |
| 3185 | /* |
| 3186 | * Update the folds for this window. Can't postpone this, because |
| 3187 | * a following operator might work on the whole fold: ">>dd". |
| 3188 | */ |
| 3189 | foldUpdate(wp, lnum, lnume + xtra - 1); |
| 3190 | |
| 3191 | /* The change may cause lines above or below the change to become |
| 3192 | * included in a fold. Set lnum/lnume to the first/last line that |
| 3193 | * might be displayed differently. |
| 3194 | * Set w_cline_folded here as an efficient way to update it when |
| 3195 | * inserting lines just above a closed fold. */ |
| 3196 | i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL); |
| 3197 | if (wp->w_cursor.lnum == lnum) |
| 3198 | wp->w_cline_folded = i; |
| 3199 | i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL); |
| 3200 | if (wp->w_cursor.lnum == lnume) |
| 3201 | wp->w_cline_folded = i; |
| 3202 | |
| 3203 | /* If the changed line is in a range of previously folded lines, |
| 3204 | * compare with the first line in that range. */ |
| 3205 | if (wp->w_cursor.lnum <= lnum) |
| 3206 | { |
| 3207 | i = find_wl_entry(wp, lnum); |
| 3208 | if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum) |
| 3209 | changed_line_abv_curs_win(wp); |
| 3210 | } |
| 3211 | #endif |
| 3212 | |
| 3213 | if (wp->w_cursor.lnum > lnum) |
| 3214 | changed_line_abv_curs_win(wp); |
| 3215 | else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col) |
| 3216 | changed_cline_bef_curs_win(wp); |
| 3217 | if (wp->w_botline >= lnum) |
| 3218 | { |
| 3219 | /* Assume that botline doesn't change (inserted lines make |
| 3220 | * other lines scroll down below botline). */ |
| 3221 | approximate_botline_win(wp); |
| 3222 | } |
| 3223 | |
| 3224 | /* Check if any w_lines[] entries have become invalid. |
| 3225 | * For entries below the change: Correct the lnums for |
| 3226 | * inserted/deleted lines. Makes it possible to stop displaying |
| 3227 | * after the change. */ |
| 3228 | for (i = 0; i < wp->w_lines_valid; ++i) |
| 3229 | if (wp->w_lines[i].wl_valid) |
| 3230 | { |
| 3231 | if (wp->w_lines[i].wl_lnum >= lnum) |
| 3232 | { |
| 3233 | if (wp->w_lines[i].wl_lnum < lnume) |
| 3234 | { |
| 3235 | /* line included in change */ |
| 3236 | wp->w_lines[i].wl_valid = FALSE; |
| 3237 | } |
| 3238 | else if (xtra != 0) |
| 3239 | { |
| 3240 | /* line below change */ |
| 3241 | wp->w_lines[i].wl_lnum += xtra; |
| 3242 | #ifdef FEAT_FOLDING |
| 3243 | wp->w_lines[i].wl_lastlnum += xtra; |
| 3244 | #endif |
| 3245 | } |
| 3246 | } |
| 3247 | #ifdef FEAT_FOLDING |
| 3248 | else if (wp->w_lines[i].wl_lastlnum >= lnum) |
| 3249 | { |
| 3250 | /* change somewhere inside this range of folded lines, |
| 3251 | * may need to be redrawn */ |
| 3252 | wp->w_lines[i].wl_valid = FALSE; |
| 3253 | } |
| 3254 | #endif |
| 3255 | } |
Bram Moolenaar | 3234cc6 | 2009-11-03 17:47:12 +0000 | [diff] [blame] | 3256 | |
| 3257 | #ifdef FEAT_FOLDING |
| 3258 | /* Take care of side effects for setting w_topline when folds have |
| 3259 | * changed. Esp. when the buffer was changed in another window. */ |
| 3260 | if (hasAnyFolding(wp)) |
| 3261 | set_topline(wp, wp->w_topline); |
| 3262 | #endif |
Bram Moolenaar | fd859c9 | 2014-05-13 12:44:24 +0200 | [diff] [blame] | 3263 | /* relative numbering may require updating more */ |
| 3264 | if (wp->w_p_rnu) |
| 3265 | redraw_win_later(wp, SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | /* Call update_screen() later, which checks out what needs to be redrawn, |
| 3270 | * since it notices b_mod_set and then uses b_mod_*. */ |
| 3271 | if (must_redraw < VALID) |
| 3272 | must_redraw = VALID; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3273 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3274 | /* when the cursor line is changed always trigger CursorMoved */ |
Bram Moolenaar | e163f1c | 2006-10-17 09:12:21 +0000 | [diff] [blame] | 3275 | if (lnum <= curwin->w_cursor.lnum |
| 3276 | && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum) |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3277 | last_cursormoved.lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | /* |
| 3281 | * unchanged() is called when the changed flag must be reset for buffer 'buf' |
| 3282 | */ |
| 3283 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3284 | unchanged( |
| 3285 | buf_T *buf, |
| 3286 | int ff) /* also reset 'fileformat' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3287 | { |
Bram Moolenaar | 164c60f | 2011-01-22 00:11:50 +0100 | [diff] [blame] | 3288 | if (buf->b_changed || (ff && file_ff_differs(buf, FALSE))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3289 | { |
| 3290 | buf->b_changed = 0; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3291 | ml_setflags(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3292 | if (ff) |
| 3293 | save_file_ff(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3294 | check_status(buf); |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 3295 | redraw_tabline = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3296 | #ifdef FEAT_TITLE |
| 3297 | need_maketitle = TRUE; /* set window title later */ |
| 3298 | #endif |
| 3299 | } |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 3300 | ++CHANGEDTICK(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3301 | #ifdef FEAT_NETBEANS_INTG |
| 3302 | netbeans_unmodified(buf); |
| 3303 | #endif |
| 3304 | } |
| 3305 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3306 | /* |
| 3307 | * check_status: called when the status bars for the buffer 'buf' |
| 3308 | * need to be updated |
| 3309 | */ |
| 3310 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3311 | check_status(buf_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3312 | { |
| 3313 | win_T *wp; |
| 3314 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 3315 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3316 | if (wp->w_buffer == buf && wp->w_status_height) |
| 3317 | { |
| 3318 | wp->w_redr_status = TRUE; |
| 3319 | if (must_redraw < VALID) |
| 3320 | must_redraw = VALID; |
| 3321 | } |
| 3322 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3323 | |
| 3324 | /* |
| 3325 | * If the file is readonly, give a warning message with the first change. |
| 3326 | * Don't do this for autocommands. |
| 3327 | * Don't use emsg(), because it flushes the macro buffer. |
Bram Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 3328 | * 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] | 3329 | * will be TRUE. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3330 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3331 | */ |
| 3332 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3333 | change_warning( |
| 3334 | int col) /* column for message; non-zero when in insert |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3335 | mode and 'showmode' is on */ |
| 3336 | { |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 3337 | static char *w_readonly = N_("W10: Warning: Changing a readonly file"); |
| 3338 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3339 | if (curbuf->b_did_warn == FALSE |
| 3340 | && curbufIsChanged() == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3341 | && !autocmd_busy |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3342 | && curbuf->b_p_ro) |
| 3343 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3344 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3345 | apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3346 | --curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3347 | if (!curbuf->b_p_ro) |
| 3348 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3349 | /* |
| 3350 | * Do what msg() does, but with a column offset if the warning should |
| 3351 | * be after the mode message. |
| 3352 | */ |
| 3353 | msg_start(); |
| 3354 | if (msg_row == Rows - 1) |
| 3355 | msg_col = col; |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3356 | msg_source(HL_ATTR(HLF_W)); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3357 | msg_puts_attr(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST); |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 3358 | #ifdef FEAT_EVAL |
| 3359 | set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1); |
| 3360 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3361 | msg_clr_eos(); |
| 3362 | (void)msg_end(); |
Bram Moolenaar | e5f2a07 | 2017-02-01 22:31:49 +0100 | [diff] [blame] | 3363 | if (msg_silent == 0 && !silent_mode |
| 3364 | #ifdef FEAT_EVAL |
| 3365 | && time_for_testing != 1 |
| 3366 | #endif |
| 3367 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3368 | { |
| 3369 | out_flush(); |
| 3370 | ui_delay(1000L, TRUE); /* give the user time to think about it */ |
| 3371 | } |
| 3372 | curbuf->b_did_warn = TRUE; |
| 3373 | redraw_cmdline = FALSE; /* don't redraw and erase the message */ |
| 3374 | if (msg_row < Rows - 1) |
| 3375 | showmode(); |
| 3376 | } |
| 3377 | } |
| 3378 | |
| 3379 | /* |
| 3380 | * Ask for a reply from the user, a 'y' or a 'n'. |
| 3381 | * No other characters are accepted, the message is repeated until a valid |
| 3382 | * reply is entered or CTRL-C is hit. |
| 3383 | * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters |
| 3384 | * from any buffers but directly from the user. |
| 3385 | * |
| 3386 | * return the 'y' or 'n' |
| 3387 | */ |
| 3388 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3389 | ask_yesno(char_u *str, int direct) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3390 | { |
| 3391 | int r = ' '; |
| 3392 | int save_State = State; |
| 3393 | |
| 3394 | if (exiting) /* put terminal in raw mode for this question */ |
| 3395 | settmode(TMODE_RAW); |
| 3396 | ++no_wait_return; |
| 3397 | #ifdef USE_ON_FLY_SCROLL |
| 3398 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 3399 | #endif |
| 3400 | State = CONFIRM; /* mouse behaves like with :confirm */ |
| 3401 | #ifdef FEAT_MOUSE |
| 3402 | setmouse(); /* disables mouse for xterm */ |
| 3403 | #endif |
| 3404 | ++no_mapping; |
| 3405 | ++allow_keys; /* no mapping here, but recognize keys */ |
| 3406 | |
| 3407 | while (r != 'y' && r != 'n') |
| 3408 | { |
| 3409 | /* same highlighting as for wait_return */ |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3410 | smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3411 | if (direct) |
| 3412 | r = get_keystroke(); |
| 3413 | else |
Bram Moolenaar | 913626c | 2008-01-03 11:43:42 +0000 | [diff] [blame] | 3414 | r = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3415 | if (r == Ctrl_C || r == ESC) |
| 3416 | r = 'n'; |
| 3417 | msg_putchar(r); /* show what you typed */ |
| 3418 | out_flush(); |
| 3419 | } |
| 3420 | --no_wait_return; |
| 3421 | State = save_State; |
| 3422 | #ifdef FEAT_MOUSE |
| 3423 | setmouse(); |
| 3424 | #endif |
| 3425 | --no_mapping; |
| 3426 | --allow_keys; |
| 3427 | |
| 3428 | return r; |
| 3429 | } |
| 3430 | |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3431 | #if defined(FEAT_MOUSE) || defined(PROTO) |
| 3432 | /* |
| 3433 | * Return TRUE if "c" is a mouse key. |
| 3434 | */ |
| 3435 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3436 | is_mouse_key(int c) |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3437 | { |
| 3438 | return c == K_LEFTMOUSE |
| 3439 | || c == K_LEFTMOUSE_NM |
| 3440 | || c == K_LEFTDRAG |
| 3441 | || c == K_LEFTRELEASE |
| 3442 | || c == K_LEFTRELEASE_NM |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3443 | || c == K_MOUSEMOVE |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3444 | || c == K_MIDDLEMOUSE |
| 3445 | || c == K_MIDDLEDRAG |
| 3446 | || c == K_MIDDLERELEASE |
| 3447 | || c == K_RIGHTMOUSE |
| 3448 | || c == K_RIGHTDRAG |
| 3449 | || c == K_RIGHTRELEASE |
| 3450 | || c == K_MOUSEDOWN |
| 3451 | || c == K_MOUSEUP |
| 3452 | || c == K_MOUSELEFT |
| 3453 | || c == K_MOUSERIGHT |
| 3454 | || c == K_X1MOUSE |
| 3455 | || c == K_X1DRAG |
| 3456 | || c == K_X1RELEASE |
| 3457 | || c == K_X2MOUSE |
| 3458 | || c == K_X2DRAG |
| 3459 | || c == K_X2RELEASE; |
| 3460 | } |
| 3461 | #endif |
| 3462 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3463 | /* |
| 3464 | * Get a key stroke directly from the user. |
| 3465 | * Ignores mouse clicks and scrollbar events, except a click for the left |
| 3466 | * button (used at the more prompt). |
| 3467 | * Doesn't use vgetc(), because it syncs undo and eats mapped characters. |
| 3468 | * Disadvantage: typeahead is ignored. |
| 3469 | * Translates the interrupt character for unix to ESC. |
| 3470 | */ |
| 3471 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3472 | get_keystroke(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3473 | { |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3474 | char_u *buf = NULL; |
| 3475 | int buflen = 150; |
| 3476 | int maxlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3477 | int len = 0; |
| 3478 | int n; |
| 3479 | int save_mapped_ctrl_c = mapped_ctrl_c; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3480 | int waited = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3481 | |
| 3482 | mapped_ctrl_c = FALSE; /* mappings are not used here */ |
| 3483 | for (;;) |
| 3484 | { |
| 3485 | cursor_on(); |
| 3486 | out_flush(); |
| 3487 | |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3488 | /* Leave some room for check_termcode() to insert a key code into (max |
| 3489 | * 5 chars plus NUL). And fix_input_buffer() can triple the number of |
| 3490 | * bytes. */ |
| 3491 | maxlen = (buflen - 6 - len) / 3; |
| 3492 | if (buf == NULL) |
| 3493 | buf = alloc(buflen); |
| 3494 | else if (maxlen < 10) |
| 3495 | { |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 3496 | char_u *t_buf = buf; |
| 3497 | |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 3498 | /* Need some more space. This might happen when receiving a long |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3499 | * escape sequence. */ |
| 3500 | buflen += 100; |
| 3501 | buf = vim_realloc(buf, buflen); |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 3502 | if (buf == NULL) |
| 3503 | vim_free(t_buf); |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3504 | maxlen = (buflen - 6 - len) / 3; |
| 3505 | } |
| 3506 | if (buf == NULL) |
| 3507 | { |
| 3508 | do_outofmem_msg((long_u)buflen); |
| 3509 | return ESC; /* panic! */ |
| 3510 | } |
| 3511 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3512 | /* First time: blocking wait. Second time: wait up to 100ms for a |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3513 | * terminal code to complete. */ |
| 3514 | n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3515 | if (n > 0) |
| 3516 | { |
| 3517 | /* Replace zero and CSI by a special key code. */ |
Bram Moolenaar | 6bff02e | 2016-08-16 22:50:55 +0200 | [diff] [blame] | 3518 | n = fix_input_buffer(buf + len, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3519 | len += n; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3520 | waited = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3521 | } |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3522 | else if (len > 0) |
| 3523 | ++waited; /* keep track of the waiting time */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3524 | |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3525 | /* Incomplete termcode and not timed out yet: get more characters */ |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3526 | if ((n = check_termcode(1, buf, buflen, &len)) < 0 |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3527 | && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3528 | continue; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3529 | |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3530 | if (n == KEYLEN_REMOVED) /* key code removed */ |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3531 | { |
Bram Moolenaar | fd30cd4 | 2011-03-22 13:07:26 +0100 | [diff] [blame] | 3532 | if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0) |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3533 | { |
| 3534 | /* Redrawing was postponed, do it now. */ |
| 3535 | update_screen(0); |
| 3536 | setcursor(); /* put cursor back where it belongs */ |
| 3537 | } |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3538 | continue; |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3539 | } |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3540 | if (n > 0) /* found a termcode: adjust length */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3541 | len = n; |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3542 | if (len == 0) /* nothing typed yet */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3543 | continue; |
| 3544 | |
| 3545 | /* Handle modifier and/or special key code. */ |
| 3546 | n = buf[0]; |
| 3547 | if (n == K_SPECIAL) |
| 3548 | { |
| 3549 | n = TO_SPECIAL(buf[1], buf[2]); |
| 3550 | if (buf[1] == KS_MODIFIER |
| 3551 | || n == K_IGNORE |
Bram Moolenaar | a5be25e | 2013-03-16 21:35:33 +0100 | [diff] [blame] | 3552 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3553 | || (is_mouse_key(n) && n != K_LEFTMOUSE) |
Bram Moolenaar | a5be25e | 2013-03-16 21:35:33 +0100 | [diff] [blame] | 3554 | #endif |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3555 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3556 | || n == K_VER_SCROLLBAR |
| 3557 | || n == K_HOR_SCROLLBAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3558 | #endif |
| 3559 | ) |
| 3560 | { |
| 3561 | if (buf[1] == KS_MODIFIER) |
| 3562 | mod_mask = buf[2]; |
| 3563 | len -= 3; |
| 3564 | if (len > 0) |
| 3565 | mch_memmove(buf, buf + 3, (size_t)len); |
| 3566 | continue; |
| 3567 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 3568 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3569 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3570 | if (has_mbyte) |
| 3571 | { |
| 3572 | if (MB_BYTE2LEN(n) > len) |
| 3573 | continue; /* more bytes to get */ |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3574 | buf[len >= buflen ? buflen - 1 : len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3575 | n = (*mb_ptr2char)(buf); |
| 3576 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3577 | #ifdef UNIX |
| 3578 | if (n == intr_char) |
| 3579 | n = ESC; |
| 3580 | #endif |
| 3581 | break; |
| 3582 | } |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3583 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3584 | |
| 3585 | mapped_ctrl_c = save_mapped_ctrl_c; |
| 3586 | return n; |
| 3587 | } |
| 3588 | |
| 3589 | /* |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3590 | * Get a number from the user. |
| 3591 | * When "mouse_used" is not NULL allow using the mouse. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3592 | */ |
| 3593 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3594 | get_number( |
| 3595 | int colon, /* allow colon to abort */ |
| 3596 | int *mouse_used) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3597 | { |
| 3598 | int n = 0; |
| 3599 | int c; |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3600 | int typed = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3601 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3602 | if (mouse_used != NULL) |
| 3603 | *mouse_used = FALSE; |
| 3604 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3605 | /* When not printing messages, the user won't know what to type, return a |
| 3606 | * zero (as if CR was hit). */ |
| 3607 | if (msg_silent != 0) |
| 3608 | return 0; |
| 3609 | |
| 3610 | #ifdef USE_ON_FLY_SCROLL |
| 3611 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 3612 | #endif |
| 3613 | ++no_mapping; |
| 3614 | ++allow_keys; /* no mapping here, but recognize keys */ |
| 3615 | for (;;) |
| 3616 | { |
| 3617 | windgoto(msg_row, msg_col); |
| 3618 | c = safe_vgetc(); |
| 3619 | if (VIM_ISDIGIT(c)) |
| 3620 | { |
| 3621 | n = n * 10 + c - '0'; |
| 3622 | msg_putchar(c); |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3623 | ++typed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3624 | } |
| 3625 | else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H) |
| 3626 | { |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3627 | if (typed > 0) |
| 3628 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3629 | msg_puts("\b \b"); |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3630 | --typed; |
| 3631 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3632 | n /= 10; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3633 | } |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3634 | #ifdef FEAT_MOUSE |
| 3635 | else if (mouse_used != NULL && c == K_LEFTMOUSE) |
| 3636 | { |
| 3637 | *mouse_used = TRUE; |
| 3638 | n = mouse_row + 1; |
| 3639 | break; |
| 3640 | } |
| 3641 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3642 | else if (n == 0 && c == ':' && colon) |
| 3643 | { |
| 3644 | stuffcharReadbuff(':'); |
| 3645 | if (!exmode_active) |
| 3646 | cmdline_row = msg_row; |
| 3647 | skip_redraw = TRUE; /* skip redraw once */ |
| 3648 | do_redraw = FALSE; |
| 3649 | break; |
| 3650 | } |
| 3651 | else if (c == CAR || c == NL || c == Ctrl_C || c == ESC) |
| 3652 | break; |
| 3653 | } |
| 3654 | --no_mapping; |
| 3655 | --allow_keys; |
| 3656 | return n; |
| 3657 | } |
| 3658 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3659 | /* |
| 3660 | * Ask the user to enter a number. |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3661 | * When "mouse_used" is not NULL allow using the mouse and in that case return |
| 3662 | * the line number. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3663 | */ |
| 3664 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3665 | prompt_for_number(int *mouse_used) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3666 | { |
| 3667 | int i; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3668 | int save_cmdline_row; |
| 3669 | int save_State; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3670 | |
| 3671 | /* When using ":silent" assume that <CR> was entered. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3672 | if (mouse_used != NULL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3673 | msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): ")); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3674 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3675 | msg_puts(_("Type number and <Enter> (empty cancels): ")); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3676 | |
Bram Moolenaar | 4cbdf15 | 2018-08-26 21:23:07 +0200 | [diff] [blame] | 3677 | // Set the state such that text can be selected/copied/pasted and we still |
| 3678 | // get mouse events. redraw_after_callback() will not redraw if cmdline_row |
| 3679 | // is zero. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3680 | save_cmdline_row = cmdline_row; |
Bram Moolenaar | 203335e | 2006-09-03 14:35:42 +0000 | [diff] [blame] | 3681 | cmdline_row = 0; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3682 | save_State = State; |
Bram Moolenaar | 4cbdf15 | 2018-08-26 21:23:07 +0200 | [diff] [blame] | 3683 | State = CMDLINE; |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3684 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 4cbdf15 | 2018-08-26 21:23:07 +0200 | [diff] [blame] | 3685 | // May show different mouse shape. |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3686 | setmouse(); |
| 3687 | #endif |
| 3688 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3689 | i = get_number(TRUE, mouse_used); |
| 3690 | if (KeyTyped) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3691 | { |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3692 | /* don't call wait_return() now */ |
| 3693 | /* msg_putchar('\n'); */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3694 | cmdline_row = msg_row - 1; |
| 3695 | need_wait_return = FALSE; |
| 3696 | msg_didany = FALSE; |
Bram Moolenaar | b245016 | 2009-07-22 09:04:20 +0000 | [diff] [blame] | 3697 | msg_didout = FALSE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3698 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3699 | else |
| 3700 | cmdline_row = save_cmdline_row; |
| 3701 | State = save_State; |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3702 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 4cbdf15 | 2018-08-26 21:23:07 +0200 | [diff] [blame] | 3703 | // May need to restore mouse shape. |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3704 | setmouse(); |
| 3705 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3706 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3707 | return i; |
| 3708 | } |
| 3709 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3710 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3711 | msgmore(long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3712 | { |
| 3713 | long pn; |
| 3714 | |
| 3715 | if (global_busy /* no messages now, wait until global is finished */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3716 | || !messaging()) /* 'lazyredraw' set, don't do messages now */ |
| 3717 | return; |
| 3718 | |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 3719 | /* We don't want to overwrite another important message, but do overwrite |
| 3720 | * a previous "more lines" or "fewer lines" message, so that "5dd" and |
| 3721 | * then "put" reports the last action. */ |
| 3722 | if (keep_msg != NULL && !keep_msg_more) |
| 3723 | return; |
| 3724 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3725 | if (n > 0) |
| 3726 | pn = n; |
| 3727 | else |
| 3728 | pn = -n; |
| 3729 | |
| 3730 | if (pn > p_report) |
| 3731 | { |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 3732 | if (n > 0) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3733 | vim_snprintf(msg_buf, MSG_BUF_LEN, |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 3734 | NGETTEXT("%ld more line", "%ld more lines", pn), pn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3735 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3736 | vim_snprintf(msg_buf, MSG_BUF_LEN, |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 3737 | NGETTEXT("%ld line less", "%ld fewer lines", pn), pn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3738 | if (got_int) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3739 | vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"), |
| 3740 | MSG_BUF_LEN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3741 | if (msg(msg_buf)) |
| 3742 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3743 | set_keep_msg((char_u *)msg_buf, 0); |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 3744 | keep_msg_more = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3745 | } |
| 3746 | } |
| 3747 | } |
| 3748 | |
| 3749 | /* |
| 3750 | * flush map and typeahead buffers and give a warning for an error |
| 3751 | */ |
| 3752 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3753 | beep_flush(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3754 | { |
| 3755 | if (emsg_silent == 0) |
| 3756 | { |
Bram Moolenaar | 6a2633b | 2018-10-07 23:16:36 +0200 | [diff] [blame] | 3757 | flush_buffers(FLUSH_MINIMAL); |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3758 | vim_beep(BO_ERROR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3759 | } |
| 3760 | } |
| 3761 | |
| 3762 | /* |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3763 | * Give a warning for an error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3764 | */ |
| 3765 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3766 | vim_beep( |
| 3767 | unsigned val) /* one of the BO_ values, e.g., BO_OPER */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3768 | { |
Bram Moolenaar | b48e96f | 2018-02-13 12:26:14 +0100 | [diff] [blame] | 3769 | #ifdef FEAT_EVAL |
| 3770 | called_vim_beep = TRUE; |
| 3771 | #endif |
| 3772 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3773 | if (emsg_silent == 0) |
| 3774 | { |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3775 | if (!((bo_flags & val) || (bo_flags & BO_ALL))) |
| 3776 | { |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3777 | #ifdef ELAPSED_FUNC |
| 3778 | static int did_init = FALSE; |
Bram Moolenaar | 1ac56c2 | 2019-01-17 22:28:22 +0100 | [diff] [blame] | 3779 | static elapsed_T start_tv; |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3780 | |
| 3781 | /* Only beep once per half a second, otherwise a sequence of beeps |
| 3782 | * would freeze Vim. */ |
| 3783 | if (!did_init || ELAPSED_FUNC(start_tv) > 500) |
| 3784 | { |
| 3785 | did_init = TRUE; |
| 3786 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3787 | #endif |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3788 | if (p_vb |
| 3789 | #ifdef FEAT_GUI |
| 3790 | /* While the GUI is starting up the termcap is set for |
| 3791 | * the GUI but the output still goes to a terminal. */ |
| 3792 | && !(gui.in_use && gui.starting) |
| 3793 | #endif |
| 3794 | ) |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3795 | { |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3796 | out_str_cf(T_VB); |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3797 | #ifdef FEAT_VTP |
| 3798 | /* No restore color information, refresh the screen. */ |
| 3799 | if (has_vtp_working() != 0 |
| 3800 | # ifdef FEAT_TERMGUICOLORS |
Bram Moolenaar | c5cd885 | 2018-05-01 15:47:38 +0200 | [diff] [blame] | 3801 | && (p_tgc || (!p_tgc && t_colors >= 256)) |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3802 | # endif |
| 3803 | ) |
| 3804 | { |
| 3805 | redraw_later(CLEAR); |
| 3806 | update_screen(0); |
| 3807 | redrawcmd(); |
| 3808 | } |
| 3809 | #endif |
| 3810 | } |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3811 | else |
| 3812 | out_char(BELL); |
| 3813 | #ifdef ELAPSED_FUNC |
| 3814 | } |
| 3815 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3816 | } |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3817 | |
Bram Moolenaar | b48e96f | 2018-02-13 12:26:14 +0100 | [diff] [blame] | 3818 | /* When 'debug' contains "beep" produce a message. If we are sourcing |
| 3819 | * a script or executing a function give the user a hint where the beep |
| 3820 | * comes from. */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3821 | if (vim_strchr(p_debug, 'e') != NULL) |
| 3822 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3823 | msg_source(HL_ATTR(HLF_W)); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3824 | msg_attr(_("Beep!"), HL_ATTR(HLF_W)); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3825 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3826 | } |
| 3827 | } |
| 3828 | |
| 3829 | /* |
| 3830 | * To get the "real" home directory: |
| 3831 | * - get value of $HOME |
| 3832 | * For Unix: |
| 3833 | * - go to that directory |
| 3834 | * - do mch_dirname() to get the real name of that directory. |
| 3835 | * This also works with mounts and links. |
| 3836 | * 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] | 3837 | * For Windows: |
| 3838 | * 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] | 3839 | */ |
| 3840 | static char_u *homedir = NULL; |
| 3841 | |
| 3842 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3843 | init_homedir(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3844 | { |
| 3845 | char_u *var; |
| 3846 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3847 | /* In case we are called a second time (when 'encoding' changes). */ |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3848 | VIM_CLEAR(homedir); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3849 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3850 | #ifdef VMS |
| 3851 | var = mch_getenv((char_u *)"SYS$LOGIN"); |
| 3852 | #else |
| 3853 | var = mch_getenv((char_u *)"HOME"); |
| 3854 | #endif |
| 3855 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 3856 | #ifdef MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3857 | /* |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3858 | * Typically, $HOME is not defined on Windows, unless the user has |
| 3859 | * specifically defined it for Vim's sake. However, on Windows NT |
| 3860 | * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for |
| 3861 | * each user. Try constructing $HOME from these. |
| 3862 | */ |
Bram Moolenaar | b47a259 | 2017-08-30 13:22:28 +0200 | [diff] [blame] | 3863 | if (var == NULL || *var == NUL) |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3864 | { |
| 3865 | char_u *homedrive, *homepath; |
| 3866 | |
| 3867 | homedrive = mch_getenv((char_u *)"HOMEDRIVE"); |
| 3868 | homepath = mch_getenv((char_u *)"HOMEPATH"); |
| 3869 | if (homepath == NULL || *homepath == NUL) |
| 3870 | homepath = (char_u *)"\\"; |
| 3871 | if (homedrive != NULL |
| 3872 | && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL) |
| 3873 | { |
| 3874 | sprintf((char *)NameBuff, "%s%s", homedrive, homepath); |
| 3875 | if (NameBuff[0] != NUL) |
| 3876 | var = NameBuff; |
| 3877 | } |
| 3878 | } |
| 3879 | |
| 3880 | if (var == NULL) |
| 3881 | var = mch_getenv((char_u *)"USERPROFILE"); |
| 3882 | |
| 3883 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3884 | * Weird but true: $HOME may contain an indirect reference to another |
| 3885 | * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set |
| 3886 | * when $HOME is being set. |
| 3887 | */ |
| 3888 | if (var != NULL && *var == '%') |
| 3889 | { |
| 3890 | char_u *p; |
| 3891 | char_u *exp; |
| 3892 | |
| 3893 | p = vim_strchr(var + 1, '%'); |
| 3894 | if (p != NULL) |
| 3895 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3896 | vim_strncpy(NameBuff, var + 1, p - (var + 1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3897 | exp = mch_getenv(NameBuff); |
| 3898 | if (exp != NULL && *exp != NUL |
| 3899 | && STRLEN(exp) + STRLEN(p) < MAXPATHL) |
| 3900 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 3901 | vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3902 | var = NameBuff; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3903 | } |
| 3904 | } |
| 3905 | } |
| 3906 | |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3907 | if (var != NULL && *var == NUL) /* empty is same as not set */ |
| 3908 | var = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3909 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3910 | if (enc_utf8 && var != NULL) |
| 3911 | { |
| 3912 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 3913 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3914 | |
| 3915 | /* Convert from active codepage to UTF-8. Other conversions are |
| 3916 | * not done, because they would fail for non-ASCII characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3917 | acp_to_enc(var, (int)STRLEN(var), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3918 | if (pp != NULL) |
| 3919 | { |
| 3920 | homedir = pp; |
| 3921 | return; |
| 3922 | } |
| 3923 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3924 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3925 | /* |
| 3926 | * Default home dir is C:/ |
| 3927 | * Best assumption we can make in such a situation. |
| 3928 | */ |
| 3929 | if (var == NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3930 | var = (char_u *)"C:/"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3931 | #endif |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3932 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3933 | if (var != NULL) |
| 3934 | { |
| 3935 | #ifdef UNIX |
| 3936 | /* |
| 3937 | * Change to the directory and get the actual path. This resolves |
| 3938 | * links. Don't do it when we can't return. |
| 3939 | */ |
| 3940 | if (mch_dirname(NameBuff, MAXPATHL) == OK |
| 3941 | && mch_chdir((char *)NameBuff) == 0) |
| 3942 | { |
| 3943 | if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK) |
| 3944 | var = IObuff; |
| 3945 | if (mch_chdir((char *)NameBuff) != 0) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3946 | emsg(_(e_prev_dir)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3947 | } |
| 3948 | #endif |
| 3949 | homedir = vim_strsave(var); |
| 3950 | } |
| 3951 | } |
| 3952 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3953 | #if defined(EXITFREE) || defined(PROTO) |
| 3954 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3955 | free_homedir(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3956 | { |
| 3957 | vim_free(homedir); |
| 3958 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 3959 | |
| 3960 | # ifdef FEAT_CMDL_COMPL |
| 3961 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3962 | free_users(void) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 3963 | { |
| 3964 | ga_clear_strings(&ga_users); |
| 3965 | } |
| 3966 | # endif |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3967 | #endif |
| 3968 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3969 | /* |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3970 | * Call expand_env() and store the result in an allocated string. |
| 3971 | * This is not very memory efficient, this expects the result to be freed |
| 3972 | * again soon. |
| 3973 | */ |
| 3974 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3975 | expand_env_save(char_u *src) |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3976 | { |
| 3977 | return expand_env_save_opt(src, FALSE); |
| 3978 | } |
| 3979 | |
| 3980 | /* |
| 3981 | * Idem, but when "one" is TRUE handle the string as one file name, only |
| 3982 | * expand "~" at the start. |
| 3983 | */ |
| 3984 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3985 | expand_env_save_opt(char_u *src, int one) |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3986 | { |
| 3987 | char_u *p; |
| 3988 | |
| 3989 | p = alloc(MAXPATHL); |
| 3990 | if (p != NULL) |
| 3991 | expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL); |
| 3992 | return p; |
| 3993 | } |
| 3994 | |
| 3995 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3996 | * Expand environment variable with path name. |
| 3997 | * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded. |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3998 | * Skips over "\ ", "\~" and "\$" (not for Win32 though). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3999 | * If anything fails no expansion is done and dst equals src. |
| 4000 | */ |
| 4001 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4002 | expand_env( |
| 4003 | char_u *src, /* input string e.g. "$HOME/vim.hlp" */ |
| 4004 | char_u *dst, /* where to put the result */ |
| 4005 | int dstlen) /* maximum length of the result */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4006 | { |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4007 | expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
| 4010 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4011 | expand_env_esc( |
| 4012 | char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */ |
| 4013 | char_u *dst, /* where to put the result */ |
| 4014 | int dstlen, /* maximum length of the result */ |
| 4015 | int esc, /* escape spaces in expanded variables */ |
| 4016 | int one, /* "srcp" is one file name */ |
| 4017 | char_u *startstr) /* start again after this (can be NULL) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4018 | { |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4019 | char_u *src; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4020 | char_u *tail; |
| 4021 | int c; |
| 4022 | char_u *var; |
| 4023 | int copy_char; |
| 4024 | int mustfree; /* var was allocated, need to free it later */ |
| 4025 | int at_start = TRUE; /* at start of a name */ |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4026 | int startstr_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4027 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4028 | if (startstr != NULL) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4029 | startstr_len = (int)STRLEN(startstr); |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4030 | |
| 4031 | src = skipwhite(srcp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4032 | --dstlen; /* leave one char space for "\," */ |
| 4033 | while (*src && dstlen > 0) |
| 4034 | { |
Bram Moolenaar | be83b73 | 2015-08-25 14:21:19 +0200 | [diff] [blame] | 4035 | #ifdef FEAT_EVAL |
| 4036 | /* Skip over `=expr`. */ |
| 4037 | if (src[0] == '`' && src[1] == '=') |
| 4038 | { |
| 4039 | size_t len; |
| 4040 | |
| 4041 | var = src; |
| 4042 | src += 2; |
| 4043 | (void)skip_expr(&src); |
| 4044 | if (*src == '`') |
| 4045 | ++src; |
| 4046 | len = src - var; |
| 4047 | if (len > (size_t)dstlen) |
| 4048 | len = dstlen; |
| 4049 | vim_strncpy(dst, var, len); |
| 4050 | dst += len; |
Bram Moolenaar | 5df1ed2 | 2015-09-01 16:25:34 +0200 | [diff] [blame] | 4051 | dstlen -= (int)len; |
Bram Moolenaar | be83b73 | 2015-08-25 14:21:19 +0200 | [diff] [blame] | 4052 | continue; |
| 4053 | } |
| 4054 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4055 | copy_char = TRUE; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4056 | if ((*src == '$' |
| 4057 | #ifdef VMS |
| 4058 | && at_start |
| 4059 | #endif |
| 4060 | ) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4061 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4062 | || *src == '%' |
| 4063 | #endif |
| 4064 | || (*src == '~' && at_start)) |
| 4065 | { |
| 4066 | mustfree = FALSE; |
| 4067 | |
| 4068 | /* |
| 4069 | * The variable name is copied into dst temporarily, because it may |
| 4070 | * be a string in read-only memory and a NUL needs to be appended. |
| 4071 | */ |
| 4072 | if (*src != '~') /* environment var */ |
| 4073 | { |
| 4074 | tail = src + 1; |
| 4075 | var = dst; |
| 4076 | c = dstlen - 1; |
| 4077 | |
| 4078 | #ifdef UNIX |
| 4079 | /* Unix has ${var-name} type environment vars */ |
| 4080 | if (*tail == '{' && !vim_isIDc('{')) |
| 4081 | { |
| 4082 | tail++; /* ignore '{' */ |
| 4083 | while (c-- > 0 && *tail && *tail != '}') |
| 4084 | *var++ = *tail++; |
| 4085 | } |
| 4086 | else |
| 4087 | #endif |
| 4088 | { |
| 4089 | while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail)) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4090 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4091 | || (*src == '%' && *tail != '%') |
| 4092 | #endif |
| 4093 | )) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4094 | *var++ = *tail++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4097 | #if defined(MSWIN) || defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4098 | # ifdef UNIX |
| 4099 | if (src[1] == '{' && *tail != '}') |
| 4100 | # else |
| 4101 | if (*src == '%' && *tail != '%') |
| 4102 | # endif |
| 4103 | var = NULL; |
| 4104 | else |
| 4105 | { |
| 4106 | # ifdef UNIX |
| 4107 | if (src[1] == '{') |
| 4108 | # else |
| 4109 | if (*src == '%') |
| 4110 | #endif |
| 4111 | ++tail; |
| 4112 | #endif |
| 4113 | *var = NUL; |
| 4114 | var = vim_getenv(dst, &mustfree); |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4115 | #if defined(MSWIN) || defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4116 | } |
| 4117 | #endif |
| 4118 | } |
| 4119 | /* home directory */ |
| 4120 | else if ( src[1] == NUL |
| 4121 | || vim_ispathsep(src[1]) |
| 4122 | || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL) |
| 4123 | { |
| 4124 | var = homedir; |
| 4125 | tail = src + 1; |
| 4126 | } |
| 4127 | else /* user directory */ |
| 4128 | { |
| 4129 | #if defined(UNIX) || (defined(VMS) && defined(USER_HOME)) |
| 4130 | /* |
| 4131 | * Copy ~user to dst[], so we can put a NUL after it. |
| 4132 | */ |
| 4133 | tail = src; |
| 4134 | var = dst; |
| 4135 | c = dstlen - 1; |
| 4136 | while ( c-- > 0 |
| 4137 | && *tail |
| 4138 | && vim_isfilec(*tail) |
| 4139 | && !vim_ispathsep(*tail)) |
| 4140 | *var++ = *tail++; |
| 4141 | *var = NUL; |
| 4142 | # ifdef UNIX |
| 4143 | /* |
| 4144 | * If the system supports getpwnam(), use it. |
| 4145 | * Otherwise, or if getpwnam() fails, the shell is used to |
| 4146 | * expand ~user. This is slower and may fail if the shell |
| 4147 | * does not support ~user (old versions of /bin/sh). |
| 4148 | */ |
| 4149 | # if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H) |
| 4150 | { |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 4151 | /* Note: memory allocated by getpwnam() is never freed. |
| 4152 | * Calling endpwent() apparently doesn't help. */ |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 4153 | struct passwd *pw = (*dst == NUL) |
| 4154 | ? NULL : getpwnam((char *)dst + 1); |
| 4155 | |
| 4156 | var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4157 | } |
| 4158 | if (var == NULL) |
| 4159 | # endif |
| 4160 | { |
| 4161 | expand_T xpc; |
| 4162 | |
| 4163 | ExpandInit(&xpc); |
| 4164 | xpc.xp_context = EXPAND_FILES; |
| 4165 | var = ExpandOne(&xpc, dst, NULL, |
| 4166 | WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4167 | mustfree = TRUE; |
| 4168 | } |
| 4169 | |
| 4170 | # else /* !UNIX, thus VMS */ |
| 4171 | /* |
| 4172 | * USER_HOME is a comma-separated list of |
| 4173 | * directories to search for the user account in. |
| 4174 | */ |
| 4175 | { |
| 4176 | char_u test[MAXPATHL], paths[MAXPATHL]; |
| 4177 | char_u *path, *next_path, *ptr; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4178 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4179 | |
| 4180 | STRCPY(paths, USER_HOME); |
| 4181 | next_path = paths; |
| 4182 | while (*next_path) |
| 4183 | { |
| 4184 | for (path = next_path; *next_path && *next_path != ','; |
| 4185 | next_path++); |
| 4186 | if (*next_path) |
| 4187 | *next_path++ = NUL; |
| 4188 | STRCPY(test, path); |
| 4189 | STRCAT(test, "/"); |
| 4190 | STRCAT(test, dst + 1); |
| 4191 | if (mch_stat(test, &st) == 0) |
| 4192 | { |
| 4193 | var = alloc(STRLEN(test) + 1); |
| 4194 | STRCPY(var, test); |
| 4195 | mustfree = TRUE; |
| 4196 | break; |
| 4197 | } |
| 4198 | } |
| 4199 | } |
| 4200 | # endif /* UNIX */ |
| 4201 | #else |
| 4202 | /* cannot expand user's home directory, so don't try */ |
| 4203 | var = NULL; |
| 4204 | tail = (char_u *)""; /* for gcc */ |
| 4205 | #endif /* UNIX || VMS */ |
| 4206 | } |
| 4207 | |
| 4208 | #ifdef BACKSLASH_IN_FILENAME |
| 4209 | /* If 'shellslash' is set change backslashes to forward slashes. |
| 4210 | * Can't use slash_adjust(), p_ssl may be set temporarily. */ |
| 4211 | if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL) |
| 4212 | { |
| 4213 | char_u *p = vim_strsave(var); |
| 4214 | |
| 4215 | if (p != NULL) |
| 4216 | { |
| 4217 | if (mustfree) |
| 4218 | vim_free(var); |
| 4219 | var = p; |
| 4220 | mustfree = TRUE; |
| 4221 | forward_slash(var); |
| 4222 | } |
| 4223 | } |
| 4224 | #endif |
| 4225 | |
| 4226 | /* If "var" contains white space, escape it with a backslash. |
| 4227 | * Required for ":e ~/tt" when $HOME includes a space. */ |
| 4228 | if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) |
| 4229 | { |
| 4230 | char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); |
| 4231 | |
| 4232 | if (p != NULL) |
| 4233 | { |
| 4234 | if (mustfree) |
| 4235 | vim_free(var); |
| 4236 | var = p; |
| 4237 | mustfree = TRUE; |
| 4238 | } |
| 4239 | } |
| 4240 | |
| 4241 | if (var != NULL && *var != NUL |
| 4242 | && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) |
| 4243 | { |
| 4244 | STRCPY(dst, var); |
| 4245 | dstlen -= (int)STRLEN(var); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4246 | c = (int)STRLEN(var); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4247 | /* if var[] ends in a path separator and tail[] starts |
| 4248 | * with it, skip a character */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4249 | if (*var != NUL && after_pathsep(dst, dst + c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4250 | #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) |
| 4251 | && dst[-1] != ':' |
| 4252 | #endif |
| 4253 | && vim_ispathsep(*tail)) |
| 4254 | ++tail; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4255 | dst += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4256 | src = tail; |
| 4257 | copy_char = FALSE; |
| 4258 | } |
| 4259 | if (mustfree) |
| 4260 | vim_free(var); |
| 4261 | } |
| 4262 | |
| 4263 | if (copy_char) /* copy at least one char */ |
| 4264 | { |
| 4265 | /* |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 4266 | * Recognize the start of a new name, for '~'. |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4267 | * Don't do this when "one" is TRUE, to avoid expanding "~" in |
| 4268 | * ":edit foo ~ foo". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4269 | */ |
| 4270 | at_start = FALSE; |
| 4271 | if (src[0] == '\\' && src[1] != NUL) |
| 4272 | { |
| 4273 | *dst++ = *src++; |
| 4274 | --dstlen; |
| 4275 | } |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4276 | else if ((src[0] == ' ' || src[0] == ',') && !one) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4277 | at_start = TRUE; |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4278 | if (dstlen > 0) |
| 4279 | { |
| 4280 | *dst++ = *src++; |
| 4281 | --dstlen; |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4282 | |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4283 | if (startstr != NULL && src - startstr_len >= srcp |
| 4284 | && STRNCMP(src - startstr_len, startstr, |
| 4285 | startstr_len) == 0) |
| 4286 | at_start = TRUE; |
| 4287 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4288 | } |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4289 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4290 | } |
| 4291 | *dst = NUL; |
| 4292 | } |
| 4293 | |
| 4294 | /* |
| 4295 | * Vim's version of getenv(). |
| 4296 | * Special handling of $HOME, $VIM and $VIMRUNTIME. |
Bram Moolenaar | 2f6b0b8 | 2005-03-08 22:43:10 +0000 | [diff] [blame] | 4297 | * Also does ACP to 'enc' conversion for Win32. |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4298 | * "mustfree" is set to TRUE when returned is allocated, it must be |
| 4299 | * initialized to FALSE by the caller. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4300 | */ |
| 4301 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4302 | vim_getenv(char_u *name, int *mustfree) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4303 | { |
Bram Moolenaar | f0908e6 | 2019-03-30 20:11:50 +0100 | [diff] [blame] | 4304 | char_u *p = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4305 | char_u *pend; |
| 4306 | int vimruntime; |
Bram Moolenaar | f0908e6 | 2019-03-30 20:11:50 +0100 | [diff] [blame] | 4307 | #ifdef MSWIN |
| 4308 | WCHAR *wn, *wp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4309 | |
Bram Moolenaar | f0908e6 | 2019-03-30 20:11:50 +0100 | [diff] [blame] | 4310 | // use "C:/" when $HOME is not set |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4311 | if (STRCMP(name, "HOME") == 0) |
| 4312 | return homedir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4313 | |
Bram Moolenaar | f0908e6 | 2019-03-30 20:11:50 +0100 | [diff] [blame] | 4314 | // Use Wide function |
| 4315 | wn = enc_to_utf16(name, NULL); |
| 4316 | if (wn == NULL) |
| 4317 | return NULL; |
| 4318 | |
| 4319 | wp = _wgetenv(wn); |
| 4320 | vim_free(wn); |
| 4321 | |
| 4322 | if (wp != NULL && *wp == NUL) // empty is the same as not set |
| 4323 | wp = NULL; |
| 4324 | |
| 4325 | if (wp != NULL) |
| 4326 | { |
| 4327 | p = utf16_to_enc(wp, NULL); |
| 4328 | if (p == NULL) |
| 4329 | return NULL; |
| 4330 | |
| 4331 | *mustfree = TRUE; |
| 4332 | return p; |
| 4333 | } |
| 4334 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4335 | p = mch_getenv(name); |
Bram Moolenaar | f0908e6 | 2019-03-30 20:11:50 +0100 | [diff] [blame] | 4336 | if (p != NULL && *p == NUL) // empty is the same as not set |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4337 | p = NULL; |
| 4338 | |
| 4339 | if (p != NULL) |
| 4340 | return p; |
Bram Moolenaar | f0908e6 | 2019-03-30 20:11:50 +0100 | [diff] [blame] | 4341 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4342 | |
Bram Moolenaar | f0908e6 | 2019-03-30 20:11:50 +0100 | [diff] [blame] | 4343 | // handling $VIMRUNTIME and $VIM is below, bail out if it's another name. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4344 | vimruntime = (STRCMP(name, "VIMRUNTIME") == 0); |
| 4345 | if (!vimruntime && STRCMP(name, "VIM") != 0) |
| 4346 | return NULL; |
| 4347 | |
| 4348 | /* |
| 4349 | * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM. |
| 4350 | * Don't do this when default_vimruntime_dir is non-empty. |
| 4351 | */ |
| 4352 | if (vimruntime |
| 4353 | #ifdef HAVE_PATHDEF |
| 4354 | && *default_vimruntime_dir == NUL |
| 4355 | #endif |
| 4356 | ) |
| 4357 | { |
Bram Moolenaar | f0908e6 | 2019-03-30 20:11:50 +0100 | [diff] [blame] | 4358 | #ifdef MSWIN |
| 4359 | // Use Wide function |
| 4360 | wp = _wgetenv(L"VIM"); |
| 4361 | if (wp != NULL && *wp == NUL) // empty is the same as not set |
| 4362 | wp = NULL; |
| 4363 | if (wp != NULL) |
| 4364 | { |
| 4365 | char_u *q = utf16_to_enc(wp, NULL); |
| 4366 | if (q != NULL) |
| 4367 | { |
| 4368 | p = vim_version_dir(q); |
| 4369 | *mustfree = TRUE; |
| 4370 | if (p == NULL) |
| 4371 | p = q; |
| 4372 | } |
| 4373 | } |
| 4374 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4375 | p = mch_getenv((char_u *)"VIM"); |
Bram Moolenaar | f0908e6 | 2019-03-30 20:11:50 +0100 | [diff] [blame] | 4376 | if (p != NULL && *p == NUL) // empty is the same as not set |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4377 | p = NULL; |
| 4378 | if (p != NULL) |
| 4379 | { |
| 4380 | p = vim_version_dir(p); |
| 4381 | if (p != NULL) |
| 4382 | *mustfree = TRUE; |
| 4383 | else |
| 4384 | p = mch_getenv((char_u *)"VIM"); |
| 4385 | } |
Bram Moolenaar | f0908e6 | 2019-03-30 20:11:50 +0100 | [diff] [blame] | 4386 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4387 | } |
| 4388 | |
| 4389 | /* |
| 4390 | * When expanding $VIM or $VIMRUNTIME fails, try using: |
| 4391 | * - the directory name from 'helpfile' (unless it contains '$') |
| 4392 | * - the executable name from argv[0] |
| 4393 | */ |
| 4394 | if (p == NULL) |
| 4395 | { |
| 4396 | if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL) |
| 4397 | p = p_hf; |
| 4398 | #ifdef USE_EXE_NAME |
| 4399 | /* |
| 4400 | * Use the name of the executable, obtained from argv[0]. |
| 4401 | */ |
| 4402 | else |
| 4403 | p = exe_name; |
| 4404 | #endif |
| 4405 | if (p != NULL) |
| 4406 | { |
| 4407 | /* remove the file name */ |
| 4408 | pend = gettail(p); |
| 4409 | |
| 4410 | /* remove "doc/" from 'helpfile', if present */ |
| 4411 | if (p == p_hf) |
| 4412 | pend = remove_tail(p, pend, (char_u *)"doc"); |
| 4413 | |
| 4414 | #ifdef USE_EXE_NAME |
| 4415 | # ifdef MACOS_X |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4416 | /* remove "MacOS" from exe_name and add "Resources/vim" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4417 | if (p == exe_name) |
| 4418 | { |
| 4419 | char_u *pend1; |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4420 | char_u *pnew; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4421 | |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4422 | pend1 = remove_tail(p, pend, (char_u *)"MacOS"); |
| 4423 | if (pend1 != pend) |
| 4424 | { |
| 4425 | pnew = alloc((unsigned)(pend1 - p) + 15); |
| 4426 | if (pnew != NULL) |
| 4427 | { |
| 4428 | STRNCPY(pnew, p, (pend1 - p)); |
| 4429 | STRCPY(pnew + (pend1 - p), "Resources/vim"); |
| 4430 | p = pnew; |
| 4431 | pend = p + STRLEN(p); |
| 4432 | } |
| 4433 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4434 | } |
| 4435 | # endif |
| 4436 | /* remove "src/" from exe_name, if present */ |
| 4437 | if (p == exe_name) |
| 4438 | pend = remove_tail(p, pend, (char_u *)"src"); |
| 4439 | #endif |
| 4440 | |
| 4441 | /* for $VIM, remove "runtime/" or "vim54/", if present */ |
| 4442 | if (!vimruntime) |
| 4443 | { |
| 4444 | pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME); |
| 4445 | pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT); |
| 4446 | } |
| 4447 | |
| 4448 | /* remove trailing path separator */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4449 | if (pend > p && after_pathsep(p, pend)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4450 | --pend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4451 | |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4452 | #ifdef MACOS_X |
| 4453 | if (p == exe_name || p == p_hf) |
| 4454 | #endif |
| 4455 | /* check that the result is a directory name */ |
| 4456 | p = vim_strnsave(p, (int)(pend - p)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4457 | |
| 4458 | if (p != NULL && !mch_isdir(p)) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 4459 | VIM_CLEAR(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4460 | else |
| 4461 | { |
| 4462 | #ifdef USE_EXE_NAME |
| 4463 | /* may add "/vim54" or "/runtime" if it exists */ |
| 4464 | if (vimruntime && (pend = vim_version_dir(p)) != NULL) |
| 4465 | { |
| 4466 | vim_free(p); |
| 4467 | p = pend; |
| 4468 | } |
| 4469 | #endif |
| 4470 | *mustfree = TRUE; |
| 4471 | } |
| 4472 | } |
| 4473 | } |
| 4474 | |
| 4475 | #ifdef HAVE_PATHDEF |
| 4476 | /* When there is a pathdef.c file we can use default_vim_dir and |
| 4477 | * default_vimruntime_dir */ |
| 4478 | if (p == NULL) |
| 4479 | { |
| 4480 | /* Only use default_vimruntime_dir when it is not empty */ |
| 4481 | if (vimruntime && *default_vimruntime_dir != NUL) |
| 4482 | { |
| 4483 | p = default_vimruntime_dir; |
| 4484 | *mustfree = FALSE; |
| 4485 | } |
| 4486 | else if (*default_vim_dir != NUL) |
| 4487 | { |
| 4488 | if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) |
| 4489 | *mustfree = TRUE; |
| 4490 | else |
| 4491 | { |
| 4492 | p = default_vim_dir; |
| 4493 | *mustfree = FALSE; |
| 4494 | } |
| 4495 | } |
| 4496 | } |
| 4497 | #endif |
| 4498 | |
| 4499 | /* |
| 4500 | * Set the environment variable, so that the new value can be found fast |
| 4501 | * next time, and others can also use it (e.g. Perl). |
| 4502 | */ |
| 4503 | if (p != NULL) |
| 4504 | { |
| 4505 | if (vimruntime) |
| 4506 | { |
| 4507 | vim_setenv((char_u *)"VIMRUNTIME", p); |
| 4508 | didset_vimruntime = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4509 | } |
| 4510 | else |
| 4511 | { |
| 4512 | vim_setenv((char_u *)"VIM", p); |
| 4513 | didset_vim = TRUE; |
| 4514 | } |
| 4515 | } |
| 4516 | return p; |
| 4517 | } |
| 4518 | |
| 4519 | /* |
| 4520 | * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. |
| 4521 | * Return NULL if not, return its name in allocated memory otherwise. |
| 4522 | */ |
| 4523 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4524 | vim_version_dir(char_u *vimdir) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4525 | { |
| 4526 | char_u *p; |
| 4527 | |
| 4528 | if (vimdir == NULL || *vimdir == NUL) |
| 4529 | return NULL; |
| 4530 | p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); |
| 4531 | if (p != NULL && mch_isdir(p)) |
| 4532 | return p; |
| 4533 | vim_free(p); |
| 4534 | p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); |
| 4535 | if (p != NULL && mch_isdir(p)) |
| 4536 | return p; |
| 4537 | vim_free(p); |
| 4538 | return NULL; |
| 4539 | } |
| 4540 | |
| 4541 | /* |
| 4542 | * If the string between "p" and "pend" ends in "name/", return "pend" minus |
| 4543 | * the length of "name/". Otherwise return "pend". |
| 4544 | */ |
| 4545 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4546 | remove_tail(char_u *p, char_u *pend, char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4547 | { |
| 4548 | int len = (int)STRLEN(name) + 1; |
| 4549 | char_u *newend = pend - len; |
| 4550 | |
| 4551 | if (newend >= p |
| 4552 | && fnamencmp(newend, name, len - 1) == 0 |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4553 | && (newend == p || after_pathsep(p, newend))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4554 | return newend; |
| 4555 | return pend; |
| 4556 | } |
| 4557 | |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 4558 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 137374f | 2018-05-13 15:59:50 +0200 | [diff] [blame] | 4559 | void |
| 4560 | vim_unsetenv(char_u *var) |
| 4561 | { |
| 4562 | #ifdef HAVE_UNSETENV |
| 4563 | unsetenv((char *)var); |
| 4564 | #else |
Bram Moolenaar | 1af6a4b | 2018-05-14 22:58:34 +0200 | [diff] [blame] | 4565 | vim_setenv(var, (char_u *)""); |
Bram Moolenaar | 137374f | 2018-05-13 15:59:50 +0200 | [diff] [blame] | 4566 | #endif |
| 4567 | } |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 4568 | #endif |
Bram Moolenaar | 137374f | 2018-05-13 15:59:50 +0200 | [diff] [blame] | 4569 | |
| 4570 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4571 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4572 | * Our portable version of setenv. |
| 4573 | */ |
| 4574 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4575 | vim_setenv(char_u *name, char_u *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4576 | { |
| 4577 | #ifdef HAVE_SETENV |
| 4578 | mch_setenv((char *)name, (char *)val, 1); |
| 4579 | #else |
| 4580 | char_u *envbuf; |
| 4581 | |
| 4582 | /* |
| 4583 | * Putenv does not copy the string, it has to remain |
| 4584 | * valid. The allocated memory will never be freed. |
| 4585 | */ |
| 4586 | envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2)); |
| 4587 | if (envbuf != NULL) |
| 4588 | { |
| 4589 | sprintf((char *)envbuf, "%s=%s", name, val); |
| 4590 | putenv((char *)envbuf); |
| 4591 | } |
| 4592 | #endif |
Bram Moolenaar | 011a34d | 2012-02-29 13:49:09 +0100 | [diff] [blame] | 4593 | #ifdef FEAT_GETTEXT |
| 4594 | /* |
| 4595 | * When setting $VIMRUNTIME adjust the directory to find message |
| 4596 | * translations to $VIMRUNTIME/lang. |
| 4597 | */ |
| 4598 | if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) |
| 4599 | { |
| 4600 | char_u *buf = concat_str(val, (char_u *)"/lang"); |
| 4601 | |
| 4602 | if (buf != NULL) |
| 4603 | { |
| 4604 | bindtextdomain(VIMPACKAGE, (char *)buf); |
| 4605 | vim_free(buf); |
| 4606 | } |
| 4607 | } |
| 4608 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
| 4611 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 4612 | /* |
| 4613 | * Function given to ExpandGeneric() to obtain an environment variable name. |
| 4614 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4615 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4616 | get_env_name( |
| 4617 | expand_T *xp UNUSED, |
| 4618 | int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4619 | { |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 4620 | # if defined(AMIGA) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4621 | /* |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 4622 | * No environ[] on the Amiga. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4623 | */ |
| 4624 | return NULL; |
| 4625 | # else |
| 4626 | # ifndef __WIN32__ |
| 4627 | /* Borland C++ 5.2 has this in a header file. */ |
| 4628 | extern char **environ; |
| 4629 | # endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 4630 | # define ENVNAMELEN 100 |
| 4631 | static char_u name[ENVNAMELEN]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4632 | char_u *str; |
| 4633 | int n; |
| 4634 | |
| 4635 | str = (char_u *)environ[idx]; |
| 4636 | if (str == NULL) |
| 4637 | return NULL; |
| 4638 | |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 4639 | for (n = 0; n < ENVNAMELEN - 1; ++n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4640 | { |
| 4641 | if (str[n] == '=' || str[n] == NUL) |
| 4642 | break; |
| 4643 | name[n] = str[n]; |
| 4644 | } |
| 4645 | name[n] = NUL; |
| 4646 | return name; |
| 4647 | # endif |
| 4648 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4649 | |
| 4650 | /* |
Bram Moolenaar | 6b0b83f | 2018-09-10 19:03:05 +0200 | [diff] [blame] | 4651 | * Add a user name to the list of users in ga_users. |
| 4652 | * Do nothing if user name is NULL or empty. |
| 4653 | */ |
| 4654 | static void |
| 4655 | add_user(char_u *user, int need_copy) |
| 4656 | { |
| 4657 | char_u *user_copy = (user != NULL && need_copy) |
| 4658 | ? vim_strsave(user) : user; |
| 4659 | |
| 4660 | if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL) |
| 4661 | { |
| 4662 | if (need_copy) |
| 4663 | vim_free(user); |
| 4664 | return; |
| 4665 | } |
| 4666 | ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy; |
| 4667 | } |
| 4668 | |
| 4669 | /* |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4670 | * Find all user names for user completion. |
| 4671 | * Done only once and then cached. |
| 4672 | */ |
| 4673 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4674 | init_users(void) |
Bram Moolenaar | 01b626c | 2013-06-16 22:49:14 +0200 | [diff] [blame] | 4675 | { |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4676 | static int lazy_init_done = FALSE; |
| 4677 | |
| 4678 | if (lazy_init_done) |
| 4679 | return; |
| 4680 | |
| 4681 | lazy_init_done = TRUE; |
| 4682 | ga_init2(&ga_users, sizeof(char_u *), 20); |
| 4683 | |
| 4684 | # if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H) |
| 4685 | { |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4686 | struct passwd* pw; |
| 4687 | |
| 4688 | setpwent(); |
| 4689 | while ((pw = getpwent()) != NULL) |
Bram Moolenaar | 6b0b83f | 2018-09-10 19:03:05 +0200 | [diff] [blame] | 4690 | add_user((char_u *)pw->pw_name, TRUE); |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4691 | endpwent(); |
| 4692 | } |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 4693 | # elif defined(MSWIN) |
Bram Moolenaar | 828c3d7 | 2018-06-19 18:58:07 +0200 | [diff] [blame] | 4694 | { |
Bram Moolenaar | 828c3d7 | 2018-06-19 18:58:07 +0200 | [diff] [blame] | 4695 | DWORD nusers = 0, ntotal = 0, i; |
| 4696 | PUSER_INFO_0 uinfo; |
| 4697 | |
| 4698 | if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH, |
| 4699 | &nusers, &ntotal, NULL) == NERR_Success) |
| 4700 | { |
| 4701 | for (i = 0; i < nusers; i++) |
Bram Moolenaar | 6b0b83f | 2018-09-10 19:03:05 +0200 | [diff] [blame] | 4702 | add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE); |
Bram Moolenaar | 828c3d7 | 2018-06-19 18:58:07 +0200 | [diff] [blame] | 4703 | |
| 4704 | NetApiBufferFree(uinfo); |
| 4705 | } |
| 4706 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4707 | # endif |
Bram Moolenaar | 6b0b83f | 2018-09-10 19:03:05 +0200 | [diff] [blame] | 4708 | # if defined(HAVE_GETPWNAM) |
| 4709 | { |
| 4710 | char_u *user_env = mch_getenv((char_u *)"USER"); |
| 4711 | |
| 4712 | // The $USER environment variable may be a valid remote user name (NIS, |
| 4713 | // LDAP) not already listed by getpwent(), as getpwent() only lists |
| 4714 | // local user names. If $USER is not already listed, check whether it |
| 4715 | // is a valid remote user name using getpwnam() and if it is, add it to |
| 4716 | // the list of user names. |
| 4717 | |
| 4718 | if (user_env != NULL && *user_env != NUL) |
| 4719 | { |
| 4720 | int i; |
| 4721 | |
| 4722 | for (i = 0; i < ga_users.ga_len; i++) |
| 4723 | { |
| 4724 | char_u *local_user = ((char_u **)ga_users.ga_data)[i]; |
| 4725 | |
| 4726 | if (STRCMP(local_user, user_env) == 0) |
| 4727 | break; |
| 4728 | } |
| 4729 | |
| 4730 | if (i == ga_users.ga_len) |
| 4731 | { |
| 4732 | struct passwd *pw = getpwnam((char *)user_env); |
| 4733 | |
| 4734 | if (pw != NULL) |
| 4735 | add_user((char_u *)pw->pw_name, TRUE); |
| 4736 | } |
| 4737 | } |
| 4738 | } |
| 4739 | # endif |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4740 | } |
| 4741 | |
| 4742 | /* |
| 4743 | * Function given to ExpandGeneric() to obtain an user names. |
| 4744 | */ |
| 4745 | char_u* |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4746 | get_users(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4747 | { |
| 4748 | init_users(); |
| 4749 | if (idx < ga_users.ga_len) |
| 4750 | return ((char_u **)ga_users.ga_data)[idx]; |
| 4751 | return NULL; |
| 4752 | } |
| 4753 | |
| 4754 | /* |
| 4755 | * Check whether name matches a user name. Return: |
| 4756 | * 0 if name does not match any user name. |
| 4757 | * 1 if name partially matches the beginning of a user name. |
| 4758 | * 2 is name fully matches a user name. |
| 4759 | */ |
Bram Moolenaar | 6c5d104 | 2018-07-07 16:41:13 +0200 | [diff] [blame] | 4760 | int |
| 4761 | match_user(char_u *name) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4762 | { |
| 4763 | int i; |
| 4764 | int n = (int)STRLEN(name); |
| 4765 | int result = 0; |
| 4766 | |
| 4767 | init_users(); |
| 4768 | for (i = 0; i < ga_users.ga_len; i++) |
| 4769 | { |
| 4770 | if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0) |
| 4771 | return 2; /* full match */ |
| 4772 | if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0) |
| 4773 | result = 1; /* partial match */ |
| 4774 | } |
| 4775 | return result; |
| 4776 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4777 | #endif |
| 4778 | |
| 4779 | /* |
| 4780 | * Replace home directory by "~" in each space or comma separated file name in |
| 4781 | * 'src'. |
| 4782 | * If anything fails (except when out of space) dst equals src. |
| 4783 | */ |
| 4784 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4785 | home_replace( |
| 4786 | buf_T *buf, /* when not NULL, check for help files */ |
| 4787 | char_u *src, /* input file name */ |
| 4788 | char_u *dst, /* where to put the result */ |
| 4789 | int dstlen, /* maximum length of the result */ |
| 4790 | int one) /* if TRUE, only replace one file name, include |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4791 | spaces and commas in the file name. */ |
| 4792 | { |
| 4793 | size_t dirlen = 0, envlen = 0; |
| 4794 | size_t len; |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4795 | char_u *homedir_env, *homedir_env_orig; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4796 | char_u *p; |
| 4797 | |
| 4798 | if (src == NULL) |
| 4799 | { |
| 4800 | *dst = NUL; |
| 4801 | return; |
| 4802 | } |
| 4803 | |
| 4804 | /* |
| 4805 | * If the file is a help file, remove the path completely. |
| 4806 | */ |
| 4807 | if (buf != NULL && buf->b_help) |
| 4808 | { |
Bram Moolenaar | 0af2d32 | 2017-08-05 23:00:53 +0200 | [diff] [blame] | 4809 | vim_snprintf((char *)dst, dstlen, "%s", gettail(src)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4810 | return; |
| 4811 | } |
| 4812 | |
| 4813 | /* |
| 4814 | * We check both the value of the $HOME environment variable and the |
| 4815 | * "real" home directory. |
| 4816 | */ |
| 4817 | if (homedir != NULL) |
| 4818 | dirlen = STRLEN(homedir); |
| 4819 | |
| 4820 | #ifdef VMS |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4821 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4822 | #else |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4823 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); |
| 4824 | #endif |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 4825 | #ifdef MSWIN |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 4826 | if (homedir_env == NULL) |
| 4827 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE"); |
| 4828 | #endif |
Bram Moolenaar | bef4790 | 2012-07-06 16:49:40 +0200 | [diff] [blame] | 4829 | /* Empty is the same as not set. */ |
| 4830 | if (homedir_env != NULL && *homedir_env == NUL) |
| 4831 | homedir_env = NULL; |
| 4832 | |
Bram Moolenaar | e60c2e5 | 2013-06-05 19:35:38 +0200 | [diff] [blame] | 4833 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) |
Bram Moolenaar | ef0a1d5 | 2018-12-30 11:38:57 +0100 | [diff] [blame] | 4834 | if (homedir_env != NULL && *homedir_env == '~') |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4835 | { |
| 4836 | int usedlen = 0; |
| 4837 | int flen; |
| 4838 | char_u *fbuf = NULL; |
| 4839 | |
| 4840 | flen = (int)STRLEN(homedir_env); |
Bram Moolenaar | 00136dc | 2018-07-25 21:19:13 +0200 | [diff] [blame] | 4841 | (void)modify_fname((char_u *)":p", FALSE, &usedlen, |
Bram Moolenaar | d12f811 | 2012-06-20 17:56:09 +0200 | [diff] [blame] | 4842 | &homedir_env, &fbuf, &flen); |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4843 | flen = (int)STRLEN(homedir_env); |
| 4844 | if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) |
| 4845 | /* Remove the trailing / that is added to a directory. */ |
| 4846 | homedir_env[flen - 1] = NUL; |
| 4847 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4848 | #endif |
| 4849 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4850 | if (homedir_env != NULL) |
| 4851 | envlen = STRLEN(homedir_env); |
| 4852 | |
| 4853 | if (!one) |
| 4854 | src = skipwhite(src); |
| 4855 | while (*src && dstlen > 0) |
| 4856 | { |
| 4857 | /* |
| 4858 | * Here we are at the beginning of a file name. |
| 4859 | * First, check to see if the beginning of the file name matches |
| 4860 | * $HOME or the "real" home directory. Check that there is a '/' |
| 4861 | * after the match (so that if e.g. the file is "/home/pieter/bla", |
| 4862 | * and the home directory is "/home/piet", the file does not end up |
| 4863 | * as "~er/bla" (which would seem to indicate the file "bla" in user |
| 4864 | * er's home directory)). |
| 4865 | */ |
| 4866 | p = homedir; |
| 4867 | len = dirlen; |
| 4868 | for (;;) |
| 4869 | { |
| 4870 | if ( len |
| 4871 | && fnamencmp(src, p, len) == 0 |
| 4872 | && (vim_ispathsep(src[len]) |
| 4873 | || (!one && (src[len] == ',' || src[len] == ' ')) |
| 4874 | || src[len] == NUL)) |
| 4875 | { |
| 4876 | src += len; |
| 4877 | if (--dstlen > 0) |
| 4878 | *dst++ = '~'; |
| 4879 | |
| 4880 | /* |
| 4881 | * If it's just the home directory, add "/". |
| 4882 | */ |
| 4883 | if (!vim_ispathsep(src[0]) && --dstlen > 0) |
| 4884 | *dst++ = '/'; |
| 4885 | break; |
| 4886 | } |
| 4887 | if (p == homedir_env) |
| 4888 | break; |
| 4889 | p = homedir_env; |
| 4890 | len = envlen; |
| 4891 | } |
| 4892 | |
| 4893 | /* if (!one) skip to separator: space or comma */ |
| 4894 | while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) |
| 4895 | *dst++ = *src++; |
| 4896 | /* skip separator */ |
| 4897 | while ((*src == ' ' || *src == ',') && --dstlen > 0) |
| 4898 | *dst++ = *src++; |
| 4899 | } |
| 4900 | /* if (dstlen == 0) out of space, what to do??? */ |
| 4901 | |
| 4902 | *dst = NUL; |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4903 | |
| 4904 | if (homedir_env != homedir_env_orig) |
| 4905 | vim_free(homedir_env); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
| 4908 | /* |
| 4909 | * Like home_replace, store the replaced string in allocated memory. |
| 4910 | * When something fails, NULL is returned. |
| 4911 | */ |
| 4912 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4913 | home_replace_save( |
| 4914 | buf_T *buf, /* when not NULL, check for help files */ |
| 4915 | char_u *src) /* input file name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4916 | { |
| 4917 | char_u *dst; |
| 4918 | unsigned len; |
| 4919 | |
| 4920 | len = 3; /* space for "~/" and trailing NUL */ |
| 4921 | if (src != NULL) /* just in case */ |
| 4922 | len += (unsigned)STRLEN(src); |
| 4923 | dst = alloc(len); |
| 4924 | if (dst != NULL) |
| 4925 | home_replace(buf, src, dst, len, TRUE); |
| 4926 | return dst; |
| 4927 | } |
| 4928 | |
| 4929 | /* |
| 4930 | * Compare two file names and return: |
| 4931 | * FPC_SAME if they both exist and are the same file. |
| 4932 | * FPC_SAMEX if they both don't exist and have the same file name. |
| 4933 | * FPC_DIFF if they both exist and are different files. |
| 4934 | * FPC_NOTX if they both don't exist. |
| 4935 | * FPC_DIFFX if one of them doesn't exist. |
| 4936 | * For the first name environment variables are expanded |
| 4937 | */ |
| 4938 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4939 | fullpathcmp( |
| 4940 | char_u *s1, |
| 4941 | char_u *s2, |
| 4942 | int checkname) /* when both don't exist, check file names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4943 | { |
| 4944 | #ifdef UNIX |
| 4945 | char_u exp1[MAXPATHL]; |
| 4946 | char_u full1[MAXPATHL]; |
| 4947 | char_u full2[MAXPATHL]; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4948 | stat_T st1, st2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4949 | int r1, r2; |
| 4950 | |
| 4951 | expand_env(s1, exp1, MAXPATHL); |
| 4952 | r1 = mch_stat((char *)exp1, &st1); |
| 4953 | r2 = mch_stat((char *)s2, &st2); |
| 4954 | if (r1 != 0 && r2 != 0) |
| 4955 | { |
| 4956 | /* if mch_stat() doesn't work, may compare the names */ |
| 4957 | if (checkname) |
| 4958 | { |
| 4959 | if (fnamecmp(exp1, s2) == 0) |
| 4960 | return FPC_SAMEX; |
| 4961 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 4962 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 4963 | if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) |
| 4964 | return FPC_SAMEX; |
| 4965 | } |
| 4966 | return FPC_NOTX; |
| 4967 | } |
| 4968 | if (r1 != 0 || r2 != 0) |
| 4969 | return FPC_DIFFX; |
| 4970 | if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) |
| 4971 | return FPC_SAME; |
| 4972 | return FPC_DIFF; |
| 4973 | #else |
| 4974 | char_u *exp1; /* expanded s1 */ |
| 4975 | char_u *full1; /* full path of s1 */ |
| 4976 | char_u *full2; /* full path of s2 */ |
| 4977 | int retval = FPC_DIFF; |
| 4978 | int r1, r2; |
| 4979 | |
| 4980 | /* allocate one buffer to store three paths (alloc()/free() is slow!) */ |
| 4981 | if ((exp1 = alloc(MAXPATHL * 3)) != NULL) |
| 4982 | { |
| 4983 | full1 = exp1 + MAXPATHL; |
| 4984 | full2 = full1 + MAXPATHL; |
| 4985 | |
| 4986 | expand_env(s1, exp1, MAXPATHL); |
| 4987 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 4988 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 4989 | |
| 4990 | /* If vim_FullName() fails, the file probably doesn't exist. */ |
| 4991 | if (r1 != OK && r2 != OK) |
| 4992 | { |
| 4993 | if (checkname && fnamecmp(exp1, s2) == 0) |
| 4994 | retval = FPC_SAMEX; |
| 4995 | else |
| 4996 | retval = FPC_NOTX; |
| 4997 | } |
| 4998 | else if (r1 != OK || r2 != OK) |
| 4999 | retval = FPC_DIFFX; |
| 5000 | else if (fnamecmp(full1, full2)) |
| 5001 | retval = FPC_DIFF; |
| 5002 | else |
| 5003 | retval = FPC_SAME; |
| 5004 | vim_free(exp1); |
| 5005 | } |
| 5006 | return retval; |
| 5007 | #endif |
| 5008 | } |
| 5009 | |
| 5010 | /* |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5011 | * Get the tail of a path: the file name. |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 5012 | * 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] | 5013 | * Fail safe: never returns NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5014 | */ |
| 5015 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5016 | gettail(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5017 | { |
| 5018 | char_u *p1, *p2; |
| 5019 | |
| 5020 | if (fname == NULL) |
| 5021 | return (char_u *)""; |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5022 | 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] | 5023 | { |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5024 | if (vim_ispathsep_nocolon(*p2)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5025 | p1 = p2 + 1; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5026 | MB_PTR_ADV(p2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5027 | } |
| 5028 | return p1; |
| 5029 | } |
| 5030 | |
| 5031 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5032 | * Get pointer to tail of "fname", including path separators. Putting a NUL |
| 5033 | * here leaves the directory name. Takes care of "c:/" and "//". |
| 5034 | * Always returns a valid pointer. |
| 5035 | */ |
| 5036 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5037 | gettail_sep(char_u *fname) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5038 | { |
| 5039 | char_u *p; |
| 5040 | char_u *t; |
| 5041 | |
| 5042 | p = get_past_head(fname); /* don't remove the '/' from "c:/file" */ |
| 5043 | t = gettail(fname); |
| 5044 | while (t > p && after_pathsep(fname, t)) |
| 5045 | --t; |
| 5046 | #ifdef VMS |
| 5047 | /* path separator is part of the path */ |
| 5048 | ++t; |
| 5049 | #endif |
| 5050 | return t; |
| 5051 | } |
| 5052 | |
| 5053 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5054 | * get the next path component (just after the next path separator). |
| 5055 | */ |
| 5056 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5057 | getnextcomp(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5058 | { |
| 5059 | while (*fname && !vim_ispathsep(*fname)) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5060 | MB_PTR_ADV(fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5061 | if (*fname) |
| 5062 | ++fname; |
| 5063 | return fname; |
| 5064 | } |
| 5065 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5066 | /* |
| 5067 | * Get a pointer to one character past the head of a path name. |
| 5068 | * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. |
| 5069 | * If there is no head, path is returned. |
| 5070 | */ |
| 5071 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5072 | get_past_head(char_u *path) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5073 | { |
| 5074 | char_u *retval; |
| 5075 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5076 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5077 | /* may skip "c:" */ |
| 5078 | if (isalpha(path[0]) && path[1] == ':') |
| 5079 | retval = path + 2; |
| 5080 | else |
| 5081 | retval = path; |
| 5082 | #else |
| 5083 | # if defined(AMIGA) |
| 5084 | /* may skip "label:" */ |
| 5085 | retval = vim_strchr(path, ':'); |
| 5086 | if (retval == NULL) |
| 5087 | retval = path; |
| 5088 | # else /* Unix */ |
| 5089 | retval = path; |
| 5090 | # endif |
| 5091 | #endif |
| 5092 | |
| 5093 | while (vim_ispathsep(*retval)) |
| 5094 | ++retval; |
| 5095 | |
| 5096 | return retval; |
| 5097 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5098 | |
| 5099 | /* |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5100 | * Return TRUE if 'c' is a path separator. |
| 5101 | * Note that for MS-Windows this includes the colon. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5102 | */ |
| 5103 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5104 | vim_ispathsep(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5105 | { |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5106 | #ifdef UNIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5107 | return (c == '/'); /* UNIX has ':' inside file names */ |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5108 | #else |
| 5109 | # ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5110 | return (c == ':' || c == '/' || c == '\\'); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5111 | # else |
| 5112 | # ifdef VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5113 | /* server"user passwd"::device:[full.path.name]fname.extension;version" */ |
| 5114 | return (c == ':' || c == '[' || c == ']' || c == '/' |
| 5115 | || c == '<' || c == '>' || c == '"' ); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5116 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5117 | return (c == ':' || c == '/'); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5118 | # endif /* VMS */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5119 | # endif |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5120 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5123 | /* |
| 5124 | * Like vim_ispathsep(c), but exclude the colon for MS-Windows. |
| 5125 | */ |
| 5126 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5127 | vim_ispathsep_nocolon(int c) |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5128 | { |
| 5129 | return vim_ispathsep(c) |
| 5130 | #ifdef BACKSLASH_IN_FILENAME |
| 5131 | && c != ':' |
| 5132 | #endif |
| 5133 | ; |
| 5134 | } |
| 5135 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5136 | /* |
| 5137 | * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" |
| 5138 | * It's done in-place. |
| 5139 | */ |
| 5140 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5141 | shorten_dir(char_u *str) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5142 | { |
| 5143 | char_u *tail, *s, *d; |
| 5144 | int skip = FALSE; |
| 5145 | |
| 5146 | tail = gettail(str); |
| 5147 | d = str; |
| 5148 | for (s = str; ; ++s) |
| 5149 | { |
| 5150 | if (s >= tail) /* copy the whole tail */ |
| 5151 | { |
| 5152 | *d++ = *s; |
| 5153 | if (*s == NUL) |
| 5154 | break; |
| 5155 | } |
| 5156 | else if (vim_ispathsep(*s)) /* copy '/' and next char */ |
| 5157 | { |
| 5158 | *d++ = *s; |
| 5159 | skip = FALSE; |
| 5160 | } |
| 5161 | else if (!skip) |
| 5162 | { |
| 5163 | *d++ = *s; /* copy next char */ |
| 5164 | if (*s != '~' && *s != '.') /* and leading "~" and "." */ |
| 5165 | skip = TRUE; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5166 | if (has_mbyte) |
| 5167 | { |
| 5168 | int l = mb_ptr2len(s); |
| 5169 | |
| 5170 | while (--l > 0) |
Bram Moolenaar | b6baca5 | 2006-08-15 20:24:14 +0000 | [diff] [blame] | 5171 | *d++ = *++s; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5172 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5173 | } |
| 5174 | } |
| 5175 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5176 | |
Bram Moolenaar | 900b4d7 | 2005-12-12 22:05:50 +0000 | [diff] [blame] | 5177 | /* |
| 5178 | * Return TRUE if the directory of "fname" exists, FALSE otherwise. |
| 5179 | * Also returns TRUE if there is no directory name. |
| 5180 | * "fname" must be writable!. |
| 5181 | */ |
| 5182 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5183 | dir_of_file_exists(char_u *fname) |
Bram Moolenaar | 900b4d7 | 2005-12-12 22:05:50 +0000 | [diff] [blame] | 5184 | { |
| 5185 | char_u *p; |
| 5186 | int c; |
| 5187 | int retval; |
| 5188 | |
| 5189 | p = gettail_sep(fname); |
| 5190 | if (p == fname) |
| 5191 | return TRUE; |
| 5192 | c = *p; |
| 5193 | *p = NUL; |
| 5194 | retval = mch_isdir(fname); |
| 5195 | *p = c; |
| 5196 | return retval; |
| 5197 | } |
| 5198 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5199 | /* |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5200 | * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally |
| 5201 | * and deal with 'fileignorecase'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5202 | */ |
| 5203 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5204 | vim_fnamecmp(char_u *x, char_u *y) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5205 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5206 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5207 | return vim_fnamencmp(x, y, MAXPATHL); |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5208 | #else |
| 5209 | if (p_fic) |
| 5210 | return MB_STRICMP(x, y); |
| 5211 | return STRCMP(x, y); |
| 5212 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5213 | } |
| 5214 | |
| 5215 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5216 | vim_fnamencmp(char_u *x, char_u *y, size_t len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5217 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5218 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5219 | char_u *px = x; |
| 5220 | char_u *py = y; |
| 5221 | int cx = NUL; |
| 5222 | int cy = NUL; |
| 5223 | |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5224 | while (len > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5225 | { |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5226 | cx = PTR2CHAR(px); |
| 5227 | cy = PTR2CHAR(py); |
| 5228 | if (cx == NUL || cy == NUL |
| 5229 | || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy) |
| 5230 | && !(cx == '/' && cy == '\\') |
| 5231 | && !(cx == '\\' && cy == '/'))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5232 | break; |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5233 | len -= MB_PTR2LEN(px); |
| 5234 | px += MB_PTR2LEN(px); |
| 5235 | py += MB_PTR2LEN(py); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5236 | } |
| 5237 | if (len == 0) |
| 5238 | return 0; |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5239 | return (cx - cy); |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5240 | #else |
| 5241 | if (p_fic) |
| 5242 | return MB_STRNICMP(x, y, len); |
| 5243 | return STRNCMP(x, y, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5244 | #endif |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5245 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5246 | |
| 5247 | /* |
| 5248 | * Concatenate file names fname1 and fname2 into allocated memory. |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 5249 | * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5250 | */ |
| 5251 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5252 | concat_fnames(char_u *fname1, char_u *fname2, int sep) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5253 | { |
| 5254 | char_u *dest; |
| 5255 | |
| 5256 | dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3)); |
| 5257 | if (dest != NULL) |
| 5258 | { |
| 5259 | STRCPY(dest, fname1); |
| 5260 | if (sep) |
| 5261 | add_pathsep(dest); |
| 5262 | STRCAT(dest, fname2); |
| 5263 | } |
| 5264 | return dest; |
| 5265 | } |
| 5266 | |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5267 | /* |
| 5268 | * Concatenate two strings and return the result in allocated memory. |
| 5269 | * Returns NULL when out of memory. |
| 5270 | */ |
| 5271 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5272 | concat_str(char_u *str1, char_u *str2) |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5273 | { |
| 5274 | char_u *dest; |
| 5275 | size_t l = STRLEN(str1); |
| 5276 | |
| 5277 | dest = alloc((unsigned)(l + STRLEN(str2) + 1L)); |
| 5278 | if (dest != NULL) |
| 5279 | { |
| 5280 | STRCPY(dest, str1); |
| 5281 | STRCPY(dest + l, str2); |
| 5282 | } |
| 5283 | return dest; |
| 5284 | } |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5285 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5286 | /* |
| 5287 | * Add a path separator to a file name, unless it already ends in a path |
| 5288 | * separator. |
| 5289 | */ |
| 5290 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5291 | add_pathsep(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5292 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5293 | if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5294 | STRCAT(p, PATHSEPSTR); |
| 5295 | } |
| 5296 | |
| 5297 | /* |
| 5298 | * FullName_save - Make an allocated copy of a full file name. |
| 5299 | * Returns NULL when out of memory. |
| 5300 | */ |
| 5301 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5302 | FullName_save( |
| 5303 | char_u *fname, |
| 5304 | int force) /* force expansion, even when it already looks |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 5305 | * like a full path name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5306 | { |
| 5307 | char_u *buf; |
| 5308 | char_u *new_fname = NULL; |
| 5309 | |
| 5310 | if (fname == NULL) |
| 5311 | return NULL; |
| 5312 | |
| 5313 | buf = alloc((unsigned)MAXPATHL); |
| 5314 | if (buf != NULL) |
| 5315 | { |
| 5316 | if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) |
| 5317 | new_fname = vim_strsave(buf); |
| 5318 | else |
| 5319 | new_fname = vim_strsave(fname); |
| 5320 | vim_free(buf); |
| 5321 | } |
| 5322 | return new_fname; |
| 5323 | } |
| 5324 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5325 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5326 | prepare_to_exit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5327 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5328 | #if defined(SIGHUP) && defined(SIG_IGN) |
| 5329 | /* Ignore SIGHUP, because a dropped connection causes a read error, which |
| 5330 | * makes Vim exit and then handling SIGHUP causes various reentrance |
| 5331 | * problems. */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 5332 | signal(SIGHUP, SIG_IGN); |
| 5333 | #endif |
| 5334 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5335 | #ifdef FEAT_GUI |
| 5336 | if (gui.in_use) |
| 5337 | { |
| 5338 | gui.dying = TRUE; |
| 5339 | out_trash(); /* trash any pending output */ |
| 5340 | } |
| 5341 | else |
| 5342 | #endif |
| 5343 | { |
| 5344 | windgoto((int)Rows - 1, 0); |
| 5345 | |
| 5346 | /* |
| 5347 | * Switch terminal mode back now, so messages end up on the "normal" |
| 5348 | * screen (if there are two screens). |
| 5349 | */ |
| 5350 | settmode(TMODE_COOK); |
Bram Moolenaar | cea912a | 2016-10-12 14:20:24 +0200 | [diff] [blame] | 5351 | stoptermcap(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5352 | out_flush(); |
| 5353 | } |
| 5354 | } |
| 5355 | |
| 5356 | /* |
| 5357 | * Preserve files and exit. |
| 5358 | * When called IObuff must contain a message. |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 5359 | * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe |
| 5360 | * functions, such as allocating memory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5361 | */ |
| 5362 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5363 | preserve_exit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5364 | { |
| 5365 | buf_T *buf; |
| 5366 | |
| 5367 | prepare_to_exit(); |
| 5368 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 5369 | /* Setting this will prevent free() calls. That avoids calling free() |
| 5370 | * recursively when free() was invoked with a bad pointer. */ |
| 5371 | really_exiting = TRUE; |
| 5372 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5373 | out_str(IObuff); |
| 5374 | screen_start(); /* don't know where cursor is now */ |
| 5375 | out_flush(); |
| 5376 | |
| 5377 | ml_close_notmod(); /* close all not-modified buffers */ |
| 5378 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 5379 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5380 | { |
| 5381 | if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) |
| 5382 | { |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 5383 | OUT_STR("Vim: preserving files...\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5384 | screen_start(); /* don't know where cursor is now */ |
| 5385 | out_flush(); |
| 5386 | ml_sync_all(FALSE, FALSE); /* preserve all swap files */ |
| 5387 | break; |
| 5388 | } |
| 5389 | } |
| 5390 | |
| 5391 | ml_close_all(FALSE); /* close all memfiles, without deleting */ |
| 5392 | |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 5393 | OUT_STR("Vim: Finished.\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5394 | |
| 5395 | getout(1); |
| 5396 | } |
| 5397 | |
| 5398 | /* |
| 5399 | * return TRUE if "fname" exists. |
| 5400 | */ |
| 5401 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5402 | vim_fexists(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5403 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 5404 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5405 | |
| 5406 | if (mch_stat((char *)fname, &st)) |
| 5407 | return FALSE; |
| 5408 | return TRUE; |
| 5409 | } |
| 5410 | |
| 5411 | /* |
| 5412 | * Check for CTRL-C pressed, but only once in a while. |
| 5413 | * Should be used instead of ui_breakcheck() for functions that check for |
| 5414 | * each line in the file. Calling ui_breakcheck() each time takes too much |
| 5415 | * time, because it can be a system call. |
| 5416 | */ |
| 5417 | |
| 5418 | #ifndef BREAKCHECK_SKIP |
| 5419 | # ifdef FEAT_GUI /* assume the GUI only runs on fast computers */ |
| 5420 | # define BREAKCHECK_SKIP 200 |
| 5421 | # else |
| 5422 | # define BREAKCHECK_SKIP 32 |
| 5423 | # endif |
| 5424 | #endif |
| 5425 | |
| 5426 | static int breakcheck_count = 0; |
| 5427 | |
| 5428 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5429 | line_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5430 | { |
| 5431 | if (++breakcheck_count >= BREAKCHECK_SKIP) |
| 5432 | { |
| 5433 | breakcheck_count = 0; |
| 5434 | ui_breakcheck(); |
| 5435 | } |
| 5436 | } |
| 5437 | |
| 5438 | /* |
| 5439 | * Like line_breakcheck() but check 10 times less often. |
| 5440 | */ |
| 5441 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5442 | fast_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5443 | { |
| 5444 | if (++breakcheck_count >= BREAKCHECK_SKIP * 10) |
| 5445 | { |
| 5446 | breakcheck_count = 0; |
| 5447 | ui_breakcheck(); |
| 5448 | } |
| 5449 | } |
| 5450 | |
| 5451 | /* |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 5452 | * Invoke expand_wildcards() for one pattern. |
| 5453 | * Expand items like "%:h" before the expansion. |
| 5454 | * Returns OK or FAIL. |
| 5455 | */ |
| 5456 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5457 | expand_wildcards_eval( |
| 5458 | char_u **pat, /* pointer to input pattern */ |
| 5459 | int *num_file, /* resulting number of files */ |
| 5460 | char_u ***file, /* array of resulting files */ |
| 5461 | int flags) /* EW_DIR, etc. */ |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 5462 | { |
| 5463 | int ret = FAIL; |
| 5464 | char_u *eval_pat = NULL; |
| 5465 | char_u *exp_pat = *pat; |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 5466 | char *ignored_msg; |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 5467 | int usedlen; |
| 5468 | |
| 5469 | if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') |
| 5470 | { |
| 5471 | ++emsg_off; |
| 5472 | eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, |
| 5473 | NULL, &ignored_msg, NULL); |
| 5474 | --emsg_off; |
| 5475 | if (eval_pat != NULL) |
| 5476 | exp_pat = concat_str(eval_pat, exp_pat + usedlen); |
| 5477 | } |
| 5478 | |
| 5479 | if (exp_pat != NULL) |
| 5480 | ret = expand_wildcards(1, &exp_pat, num_file, file, flags); |
| 5481 | |
| 5482 | if (eval_pat != NULL) |
| 5483 | { |
| 5484 | vim_free(exp_pat); |
| 5485 | vim_free(eval_pat); |
| 5486 | } |
| 5487 | |
| 5488 | return ret; |
| 5489 | } |
| 5490 | |
| 5491 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5492 | * Expand wildcards. Calls gen_expand_wildcards() and removes files matching |
| 5493 | * 'wildignore'. |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5494 | * 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] | 5495 | */ |
| 5496 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5497 | expand_wildcards( |
| 5498 | int num_pat, /* number of input patterns */ |
| 5499 | char_u **pat, /* array of input patterns */ |
| 5500 | int *num_files, /* resulting number of files */ |
| 5501 | char_u ***files, /* array of resulting files */ |
| 5502 | int flags) /* EW_DIR, etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5503 | { |
| 5504 | int retval; |
| 5505 | int i, j; |
| 5506 | char_u *p; |
| 5507 | int non_suf_match; /* number without matching suffix */ |
| 5508 | |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5509 | retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5510 | |
| 5511 | /* When keeping all matches, return here */ |
Bram Moolenaar | 9e193ac | 2010-07-19 23:11:27 +0200 | [diff] [blame] | 5512 | if ((flags & EW_KEEPALL) || retval == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5513 | return retval; |
| 5514 | |
| 5515 | #ifdef FEAT_WILDIGN |
| 5516 | /* |
| 5517 | * Remove names that match 'wildignore'. |
| 5518 | */ |
| 5519 | if (*p_wig) |
| 5520 | { |
| 5521 | char_u *ffname; |
| 5522 | |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5523 | /* check all files in (*files)[] */ |
| 5524 | for (i = 0; i < *num_files; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5525 | { |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5526 | ffname = FullName_save((*files)[i], FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5527 | if (ffname == NULL) /* out of memory */ |
| 5528 | break; |
| 5529 | # ifdef VMS |
| 5530 | vms_remove_version(ffname); |
| 5531 | # endif |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5532 | if (match_file_list(p_wig, (*files)[i], ffname)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5533 | { |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 5534 | /* remove this matching file from the list */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5535 | vim_free((*files)[i]); |
| 5536 | for (j = i; j + 1 < *num_files; ++j) |
| 5537 | (*files)[j] = (*files)[j + 1]; |
| 5538 | --*num_files; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5539 | --i; |
| 5540 | } |
| 5541 | vim_free(ffname); |
| 5542 | } |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5543 | |
| 5544 | /* If the number of matches is now zero, we fail. */ |
| 5545 | if (*num_files == 0) |
| 5546 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 5547 | VIM_CLEAR(*files); |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5548 | return FAIL; |
| 5549 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5550 | } |
| 5551 | #endif |
| 5552 | |
| 5553 | /* |
| 5554 | * Move the names where 'suffixes' match to the end. |
| 5555 | */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5556 | if (*num_files > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5557 | { |
| 5558 | non_suf_match = 0; |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5559 | for (i = 0; i < *num_files; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5560 | { |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5561 | if (!match_suffix((*files)[i])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5562 | { |
| 5563 | /* |
| 5564 | * Move the name without matching suffix to the front |
| 5565 | * of the list. |
| 5566 | */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5567 | p = (*files)[i]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5568 | for (j = i; j > non_suf_match; --j) |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 5569 | (*files)[j] = (*files)[j - 1]; |
| 5570 | (*files)[non_suf_match++] = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5571 | } |
| 5572 | } |
| 5573 | } |
| 5574 | |
| 5575 | return retval; |
| 5576 | } |
| 5577 | |
| 5578 | /* |
| 5579 | * Return TRUE if "fname" matches with an entry in 'suffixes'. |
| 5580 | */ |
| 5581 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5582 | match_suffix(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5583 | { |
| 5584 | int fnamelen, setsuflen; |
| 5585 | char_u *setsuf; |
| 5586 | #define MAXSUFLEN 30 /* maximum length of a file suffix */ |
| 5587 | char_u suf_buf[MAXSUFLEN]; |
| 5588 | |
| 5589 | fnamelen = (int)STRLEN(fname); |
| 5590 | setsuflen = 0; |
| 5591 | for (setsuf = p_su; *setsuf; ) |
| 5592 | { |
| 5593 | setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); |
Bram Moolenaar | 055a2ba | 2009-07-14 19:40:21 +0000 | [diff] [blame] | 5594 | if (setsuflen == 0) |
| 5595 | { |
| 5596 | char_u *tail = gettail(fname); |
| 5597 | |
| 5598 | /* empty entry: match name without a '.' */ |
| 5599 | if (vim_strchr(tail, '.') == NULL) |
| 5600 | { |
| 5601 | setsuflen = 1; |
| 5602 | break; |
| 5603 | } |
| 5604 | } |
| 5605 | else |
| 5606 | { |
| 5607 | if (fnamelen >= setsuflen |
| 5608 | && fnamencmp(suf_buf, fname + fnamelen - setsuflen, |
| 5609 | (size_t)setsuflen) == 0) |
| 5610 | break; |
| 5611 | setsuflen = 0; |
| 5612 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5613 | } |
| 5614 | return (setsuflen != 0); |
| 5615 | } |
| 5616 | |
| 5617 | #if !defined(NO_EXPANDPATH) || defined(PROTO) |
| 5618 | |
| 5619 | # ifdef VIM_BACKTICK |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5620 | static int vim_backtick(char_u *p); |
| 5621 | static int expand_backtick(garray_T *gap, char_u *pat, int flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5622 | # endif |
| 5623 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 5624 | # if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5625 | /* |
| 5626 | * File name expansion code for MS-DOS, Win16 and Win32. It's here because |
| 5627 | * it's shared between these systems. |
| 5628 | */ |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5629 | # if defined(PROTO) |
| 5630 | # define _cdecl |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5631 | # else |
| 5632 | # ifdef __BORLANDC__ |
| 5633 | # define _cdecl _RTLENTRYF |
| 5634 | # endif |
| 5635 | # endif |
| 5636 | |
| 5637 | /* |
| 5638 | * comparison function for qsort in dos_expandpath() |
| 5639 | */ |
| 5640 | static int _cdecl |
| 5641 | pstrcmp(const void *a, const void *b) |
| 5642 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5643 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5644 | } |
| 5645 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5646 | /* |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5647 | * Recursively expand one path component into all matching files and/or |
| 5648 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5649 | * Return the number of matches found. |
| 5650 | * "path" has backslashes before chars that are not to be expanded, starting |
| 5651 | * at "path[wildoff]". |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5652 | * Return the number of matches found. |
| 5653 | * NOTE: much of this is identical to unix_expandpath(), keep in sync! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5654 | */ |
| 5655 | static int |
| 5656 | dos_expandpath( |
| 5657 | garray_T *gap, |
| 5658 | char_u *path, |
| 5659 | int wildoff, |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5660 | int flags, /* EW_* flags */ |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 5661 | int didstar) /* expanded "**" once already */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5662 | { |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5663 | char_u *buf; |
| 5664 | char_u *path_end; |
| 5665 | char_u *p, *s, *e; |
| 5666 | int start_len = gap->ga_len; |
| 5667 | char_u *pat; |
| 5668 | regmatch_T regmatch; |
| 5669 | int starts_with_dot; |
| 5670 | int matches; |
| 5671 | int len; |
| 5672 | int starstar = FALSE; |
Bram Moolenaar | 0eb035c | 2019-04-02 22:15:55 +0200 | [diff] [blame] | 5673 | static int stardepth = 0; // depth for "**" expansion |
| 5674 | HANDLE hFind = INVALID_HANDLE_VALUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5675 | WIN32_FIND_DATAW wfb; |
Bram Moolenaar | 0eb035c | 2019-04-02 22:15:55 +0200 | [diff] [blame] | 5676 | WCHAR *wn = NULL; // UCS-2 name, NULL when not used. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5677 | char_u *matchname; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5678 | int ok; |
| 5679 | |
| 5680 | /* Expanding "**" may take a long time, check for CTRL-C. */ |
| 5681 | if (stardepth > 0) |
| 5682 | { |
| 5683 | ui_breakcheck(); |
| 5684 | if (got_int) |
| 5685 | return 0; |
| 5686 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5687 | |
Bram Moolenaar | fc3abf4 | 2019-01-24 15:54:21 +0100 | [diff] [blame] | 5688 | // Make room for file name. When doing encoding conversion the actual |
| 5689 | // 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] | 5690 | buf = alloc((int)MAXPATHL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5691 | if (buf == NULL) |
| 5692 | return 0; |
| 5693 | |
| 5694 | /* |
| 5695 | * Find the first part in the path name that contains a wildcard or a ~1. |
| 5696 | * Copy it into buf, including the preceding characters. |
| 5697 | */ |
| 5698 | p = buf; |
| 5699 | s = buf; |
| 5700 | e = NULL; |
| 5701 | path_end = path; |
| 5702 | while (*path_end != NUL) |
| 5703 | { |
| 5704 | /* May ignore a wildcard that has a backslash before it; it will |
| 5705 | * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ |
| 5706 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 5707 | *p++ = *path_end++; |
| 5708 | else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') |
| 5709 | { |
| 5710 | if (e != NULL) |
| 5711 | break; |
| 5712 | s = p + 1; |
| 5713 | } |
| 5714 | else if (path_end >= path + wildoff |
| 5715 | && vim_strchr((char_u *)"*?[~", *path_end) != NULL) |
| 5716 | e = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5717 | if (has_mbyte) |
| 5718 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5719 | len = (*mb_ptr2len)(path_end); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5720 | STRNCPY(p, path_end, len); |
| 5721 | p += len; |
| 5722 | path_end += len; |
| 5723 | } |
| 5724 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5725 | *p++ = *path_end++; |
| 5726 | } |
| 5727 | e = p; |
| 5728 | *e = NUL; |
| 5729 | |
| 5730 | /* now we have one wildcard component between s and e */ |
| 5731 | /* Remove backslashes between "wildoff" and the start of the wildcard |
| 5732 | * component. */ |
| 5733 | for (p = buf + wildoff; p < s; ++p) |
| 5734 | if (rem_backslash(p)) |
| 5735 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 5736 | STRMOVE(p, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5737 | --e; |
| 5738 | --s; |
| 5739 | } |
| 5740 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5741 | /* Check for "**" between "s" and "e". */ |
| 5742 | for (p = s; p < e; ++p) |
| 5743 | if (p[0] == '*' && p[1] == '*') |
| 5744 | starstar = TRUE; |
| 5745 | |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 5746 | starts_with_dot = *s == '.'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5747 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 5748 | if (pat == NULL) |
| 5749 | { |
| 5750 | vim_free(buf); |
| 5751 | return 0; |
| 5752 | } |
| 5753 | |
| 5754 | /* compile the regexp into a program */ |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 5755 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
Bram Moolenaar | b560983 | 2011-07-20 15:04:58 +0200 | [diff] [blame] | 5756 | ++emsg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5757 | regmatch.rm_ic = TRUE; /* Always ignore case */ |
| 5758 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
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 | vim_free(pat); |
| 5762 | |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 5763 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5764 | { |
| 5765 | vim_free(buf); |
| 5766 | return 0; |
| 5767 | } |
| 5768 | |
| 5769 | /* remember the pattern or file name being looked for */ |
| 5770 | matchname = vim_strsave(s); |
| 5771 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5772 | /* If "**" is by itself, this is the first time we encounter it and more |
| 5773 | * is following then find matches without any directory. */ |
| 5774 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 5775 | && *path_end == '/') |
| 5776 | { |
| 5777 | STRCPY(s, path_end + 1); |
| 5778 | ++stardepth; |
| 5779 | (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 5780 | --stardepth; |
| 5781 | } |
| 5782 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5783 | /* Scan all files in the directory with "dir/ *.*" */ |
| 5784 | STRCPY(s, "*.*"); |
Bram Moolenaar | 0eb035c | 2019-04-02 22:15:55 +0200 | [diff] [blame] | 5785 | wn = enc_to_utf16(buf, NULL); |
| 5786 | if (wn != NULL) |
| 5787 | hFind = FindFirstFileW(wn, &wfb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5788 | ok = (hFind != INVALID_HANDLE_VALUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5789 | |
| 5790 | while (ok) |
| 5791 | { |
Bram Moolenaar | 0eb035c | 2019-04-02 22:15:55 +0200 | [diff] [blame] | 5792 | p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 5793 | if (p == NULL) |
| 5794 | break; // out of memory |
| 5795 | |
Bram Moolenaar | 0eb035c | 2019-04-02 22:15:55 +0200 | [diff] [blame] | 5796 | // Ignore entries starting with a dot, unless when asked for. Accept |
| 5797 | // all entries found with "matchname". |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 5798 | if ((p[0] != '.' || starts_with_dot |
| 5799 | || ((flags & EW_DODOT) |
| 5800 | && p[1] != NUL && (p[1] != '.' || p[2] != NUL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5801 | && (matchname == NULL |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 5802 | || (regmatch.regprog != NULL |
| 5803 | && vim_regexec(®match, p, (colnr_T)0)) |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 5804 | || ((flags & EW_NOTWILD) |
| 5805 | && fnamencmp(path + (s - buf), p, e - s) == 0))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5806 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5807 | STRCPY(s, p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5808 | len = (int)STRLEN(buf); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5809 | |
| 5810 | if (starstar && stardepth < 100) |
| 5811 | { |
| 5812 | /* For "**" in the pattern first go deeper in the tree to |
| 5813 | * find matches. */ |
| 5814 | STRCPY(buf + len, "/**"); |
| 5815 | STRCPY(buf + len + 3, path_end); |
| 5816 | ++stardepth; |
| 5817 | (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); |
| 5818 | --stardepth; |
| 5819 | } |
| 5820 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5821 | STRCPY(buf + len, path_end); |
| 5822 | if (mch_has_exp_wildcard(path_end)) |
| 5823 | { |
| 5824 | /* need to expand another component of the path */ |
| 5825 | /* remove backslashes for the remaining components only */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5826 | (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5827 | } |
| 5828 | else |
| 5829 | { |
| 5830 | /* no more wildcards, check if there is a match */ |
| 5831 | /* remove backslashes for the remaining components only */ |
| 5832 | if (*path_end != 0) |
| 5833 | backslash_halve(buf + len + 1); |
| 5834 | if (mch_getperm(buf) >= 0) /* add existing file */ |
| 5835 | addfile(gap, buf, flags); |
| 5836 | } |
| 5837 | } |
| 5838 | |
Bram Moolenaar | 0eb035c | 2019-04-02 22:15:55 +0200 | [diff] [blame] | 5839 | vim_free(p); |
| 5840 | ok = FindNextFileW(hFind, &wfb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5841 | |
| 5842 | /* If no more matches and no match was used, try expanding the name |
| 5843 | * itself. Finds the long name of a short filename. */ |
| 5844 | if (!ok && matchname != NULL && gap->ga_len == start_len) |
| 5845 | { |
| 5846 | STRCPY(s, matchname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5847 | FindClose(hFind); |
Bram Moolenaar | 0eb035c | 2019-04-02 22:15:55 +0200 | [diff] [blame] | 5848 | vim_free(wn); |
| 5849 | wn = enc_to_utf16(buf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5850 | if (wn != NULL) |
Bram Moolenaar | 0eb035c | 2019-04-02 22:15:55 +0200 | [diff] [blame] | 5851 | hFind = FindFirstFileW(wn, &wfb); |
| 5852 | else |
| 5853 | hFind = INVALID_HANDLE_VALUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5854 | ok = (hFind != INVALID_HANDLE_VALUE); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 5855 | VIM_CLEAR(matchname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5856 | } |
| 5857 | } |
| 5858 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5859 | FindClose(hFind); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5860 | vim_free(wn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5861 | vim_free(buf); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5862 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5863 | vim_free(matchname); |
| 5864 | |
| 5865 | matches = gap->ga_len - start_len; |
| 5866 | if (matches > 0) |
| 5867 | qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, |
| 5868 | sizeof(char_u *), pstrcmp); |
| 5869 | return matches; |
| 5870 | } |
| 5871 | |
| 5872 | int |
| 5873 | mch_expandpath( |
| 5874 | garray_T *gap, |
| 5875 | char_u *path, |
| 5876 | int flags) /* EW_* flags */ |
| 5877 | { |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5878 | return dos_expandpath(gap, path, 0, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5879 | } |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 5880 | # endif // MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5881 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5882 | #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ |
| 5883 | || defined(PROTO) |
| 5884 | /* |
| 5885 | * Unix style wildcard expansion code. |
| 5886 | * It's here because it's used both for Unix and Mac. |
| 5887 | */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5888 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5889 | pstrcmp(const void *a, const void *b) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5890 | { |
| 5891 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
| 5892 | } |
| 5893 | |
| 5894 | /* |
| 5895 | * Recursively expand one path component into all matching files and/or |
| 5896 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
| 5897 | * "path" has backslashes before chars that are not to be expanded, starting |
| 5898 | * at "path + wildoff". |
| 5899 | * Return the number of matches found. |
| 5900 | * NOTE: much of this is identical to dos_expandpath(), keep in sync! |
| 5901 | */ |
| 5902 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5903 | unix_expandpath( |
| 5904 | garray_T *gap, |
| 5905 | char_u *path, |
| 5906 | int wildoff, |
| 5907 | int flags, /* EW_* flags */ |
| 5908 | int didstar) /* expanded "**" once already */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5909 | { |
| 5910 | char_u *buf; |
| 5911 | char_u *path_end; |
| 5912 | char_u *p, *s, *e; |
| 5913 | int start_len = gap->ga_len; |
| 5914 | char_u *pat; |
| 5915 | regmatch_T regmatch; |
| 5916 | int starts_with_dot; |
| 5917 | int matches; |
| 5918 | int len; |
| 5919 | int starstar = FALSE; |
| 5920 | static int stardepth = 0; /* depth for "**" expansion */ |
| 5921 | |
| 5922 | DIR *dirp; |
| 5923 | struct dirent *dp; |
| 5924 | |
| 5925 | /* Expanding "**" may take a long time, check for CTRL-C. */ |
| 5926 | if (stardepth > 0) |
| 5927 | { |
| 5928 | ui_breakcheck(); |
| 5929 | if (got_int) |
| 5930 | return 0; |
| 5931 | } |
| 5932 | |
| 5933 | /* make room for file name */ |
| 5934 | buf = alloc((int)STRLEN(path) + BASENAMELEN + 5); |
| 5935 | if (buf == NULL) |
| 5936 | return 0; |
| 5937 | |
| 5938 | /* |
| 5939 | * Find the first part in the path name that contains a wildcard. |
Bram Moolenaar | 2d0b92f | 2012-04-30 21:09:43 +0200 | [diff] [blame] | 5940 | * 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] | 5941 | * Copy it into "buf", including the preceding characters. |
| 5942 | */ |
| 5943 | p = buf; |
| 5944 | s = buf; |
| 5945 | e = NULL; |
| 5946 | path_end = path; |
| 5947 | while (*path_end != NUL) |
| 5948 | { |
| 5949 | /* May ignore a wildcard that has a backslash before it; it will |
| 5950 | * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ |
| 5951 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 5952 | *p++ = *path_end++; |
| 5953 | else if (*path_end == '/') |
| 5954 | { |
| 5955 | if (e != NULL) |
| 5956 | break; |
| 5957 | s = p + 1; |
| 5958 | } |
| 5959 | else if (path_end >= path + wildoff |
Bram Moolenaar | 2d0b92f | 2012-04-30 21:09:43 +0200 | [diff] [blame] | 5960 | && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5961 | || (!p_fic && (flags & EW_ICASE) |
| 5962 | && isalpha(PTR2CHAR(path_end))))) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5963 | e = p; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5964 | if (has_mbyte) |
| 5965 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5966 | len = (*mb_ptr2len)(path_end); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5967 | STRNCPY(p, path_end, len); |
| 5968 | p += len; |
| 5969 | path_end += len; |
| 5970 | } |
| 5971 | else |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5972 | *p++ = *path_end++; |
| 5973 | } |
| 5974 | e = p; |
| 5975 | *e = NUL; |
| 5976 | |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 5977 | /* Now we have one wildcard component between "s" and "e". */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5978 | /* Remove backslashes between "wildoff" and the start of the wildcard |
| 5979 | * component. */ |
| 5980 | for (p = buf + wildoff; p < s; ++p) |
| 5981 | if (rem_backslash(p)) |
| 5982 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 5983 | STRMOVE(p, p + 1); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5984 | --e; |
| 5985 | --s; |
| 5986 | } |
| 5987 | |
| 5988 | /* Check for "**" between "s" and "e". */ |
| 5989 | for (p = s; p < e; ++p) |
| 5990 | if (p[0] == '*' && p[1] == '*') |
| 5991 | starstar = TRUE; |
| 5992 | |
| 5993 | /* convert the file pattern to a regexp pattern */ |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 5994 | starts_with_dot = *s == '.'; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 5995 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 5996 | if (pat == NULL) |
| 5997 | { |
| 5998 | vim_free(buf); |
| 5999 | return 0; |
| 6000 | } |
| 6001 | |
| 6002 | /* compile the regexp into a program */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 6003 | if (flags & EW_ICASE) |
| 6004 | regmatch.rm_ic = TRUE; /* 'wildignorecase' set */ |
| 6005 | else |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 6006 | regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */ |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 6007 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 6008 | ++emsg_silent; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6009 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 6010 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 6011 | --emsg_silent; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6012 | vim_free(pat); |
| 6013 | |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 6014 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6015 | { |
| 6016 | vim_free(buf); |
| 6017 | return 0; |
| 6018 | } |
| 6019 | |
| 6020 | /* If "**" is by itself, this is the first time we encounter it and more |
| 6021 | * is following then find matches without any directory. */ |
| 6022 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 6023 | && *path_end == '/') |
| 6024 | { |
| 6025 | STRCPY(s, path_end + 1); |
| 6026 | ++stardepth; |
| 6027 | (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 6028 | --stardepth; |
| 6029 | } |
| 6030 | |
| 6031 | /* open the directory for scanning */ |
| 6032 | *s = NUL; |
| 6033 | dirp = opendir(*buf == NUL ? "." : (char *)buf); |
| 6034 | |
| 6035 | /* Find all matching entries */ |
| 6036 | if (dirp != NULL) |
| 6037 | { |
| 6038 | for (;;) |
| 6039 | { |
| 6040 | dp = readdir(dirp); |
| 6041 | if (dp == NULL) |
| 6042 | break; |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 6043 | if ((dp->d_name[0] != '.' || starts_with_dot |
| 6044 | || ((flags & EW_DODOT) |
| 6045 | && dp->d_name[1] != NUL |
| 6046 | && (dp->d_name[1] != '.' || dp->d_name[2] != NUL))) |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 6047 | && ((regmatch.regprog != NULL && vim_regexec(®match, |
| 6048 | (char_u *)dp->d_name, (colnr_T)0)) |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 6049 | || ((flags & EW_NOTWILD) |
| 6050 | && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0))) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6051 | { |
| 6052 | STRCPY(s, dp->d_name); |
| 6053 | len = STRLEN(buf); |
| 6054 | |
| 6055 | if (starstar && stardepth < 100) |
| 6056 | { |
| 6057 | /* For "**" in the pattern first go deeper in the tree to |
| 6058 | * find matches. */ |
| 6059 | STRCPY(buf + len, "/**"); |
| 6060 | STRCPY(buf + len + 3, path_end); |
| 6061 | ++stardepth; |
| 6062 | (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); |
| 6063 | --stardepth; |
| 6064 | } |
| 6065 | |
| 6066 | STRCPY(buf + len, path_end); |
| 6067 | if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */ |
| 6068 | { |
| 6069 | /* need to expand another component of the path */ |
| 6070 | /* remove backslashes for the remaining components only */ |
| 6071 | (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); |
| 6072 | } |
| 6073 | else |
| 6074 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 6075 | stat_T sb; |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 6076 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6077 | /* no more wildcards, check if there is a match */ |
| 6078 | /* remove backslashes for the remaining components only */ |
| 6079 | if (*path_end != NUL) |
| 6080 | backslash_halve(buf + len + 1); |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 6081 | /* add existing file or symbolic link */ |
Bram Moolenaar | ab11a59 | 2015-03-06 22:00:11 +0100 | [diff] [blame] | 6082 | if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0 |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 6083 | : mch_getperm(buf) >= 0) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6084 | { |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 6085 | #ifdef MACOS_CONVERT |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6086 | size_t precomp_len = STRLEN(buf)+1; |
| 6087 | char_u *precomp_buf = |
| 6088 | mac_precompose_path(buf, precomp_len, &precomp_len); |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 6089 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6090 | if (precomp_buf) |
| 6091 | { |
| 6092 | mch_memmove(buf, precomp_buf, precomp_len); |
| 6093 | vim_free(precomp_buf); |
| 6094 | } |
| 6095 | #endif |
| 6096 | addfile(gap, buf, flags); |
| 6097 | } |
| 6098 | } |
| 6099 | } |
| 6100 | } |
| 6101 | |
| 6102 | closedir(dirp); |
| 6103 | } |
| 6104 | |
| 6105 | vim_free(buf); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6106 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 6107 | |
| 6108 | matches = gap->ga_len - start_len; |
| 6109 | if (matches > 0) |
| 6110 | qsort(((char_u **)gap->ga_data) + start_len, matches, |
| 6111 | sizeof(char_u *), pstrcmp); |
| 6112 | return matches; |
| 6113 | } |
| 6114 | #endif |
| 6115 | |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 6116 | #if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 6117 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 6118 | * Sort "gap" and remove duplicate entries. "gap" is expected to contain a |
| 6119 | * list of file names in allocated memory. |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 6120 | */ |
| 6121 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6122 | remove_duplicates(garray_T *gap) |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 6123 | { |
| 6124 | int i; |
| 6125 | int j; |
| 6126 | char_u **fnames = (char_u **)gap->ga_data; |
| 6127 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 6128 | sort_strings(fnames, gap->ga_len); |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 6129 | for (i = gap->ga_len - 1; i > 0; --i) |
| 6130 | if (fnamecmp(fnames[i - 1], fnames[i]) == 0) |
| 6131 | { |
| 6132 | vim_free(fnames[i]); |
| 6133 | for (j = i + 1; j < gap->ga_len; ++j) |
| 6134 | fnames[j - 1] = fnames[j]; |
| 6135 | --gap->ga_len; |
| 6136 | } |
| 6137 | } |
| 6138 | #endif |
| 6139 | |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6140 | /* |
| 6141 | * Return TRUE if "p" contains what looks like an environment variable. |
| 6142 | * Allowing for escaping. |
| 6143 | */ |
| 6144 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6145 | has_env_var(char_u *p) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6146 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 6147 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6148 | { |
| 6149 | if (*p == '\\' && p[1] != NUL) |
| 6150 | ++p; |
| 6151 | else if (vim_strchr((char_u *) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 6152 | #if defined(MSWIN) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6153 | "$%" |
| 6154 | #else |
| 6155 | "$" |
| 6156 | #endif |
| 6157 | , *p) != NULL) |
| 6158 | return TRUE; |
| 6159 | } |
| 6160 | return FALSE; |
| 6161 | } |
| 6162 | |
| 6163 | #ifdef SPECIAL_WILDCHAR |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6164 | /* |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 6165 | * Return TRUE if "p" contains a special wildcard character, one that Vim |
| 6166 | * cannot expand, requires using a shell. |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6167 | */ |
| 6168 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6169 | has_special_wildchar(char_u *p) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6170 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 6171 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6172 | { |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 6173 | /* Allow for escaping. */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6174 | if (*p == '\\' && p[1] != NUL) |
| 6175 | ++p; |
| 6176 | else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL) |
| 6177 | return TRUE; |
| 6178 | } |
| 6179 | return FALSE; |
| 6180 | } |
| 6181 | #endif |
| 6182 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6183 | /* |
| 6184 | * Generic wildcard expansion code. |
| 6185 | * |
| 6186 | * Characters in "pat" that should not be expanded must be preceded with a |
| 6187 | * backslash. E.g., "/path\ with\ spaces/my\*star*" |
| 6188 | * |
| 6189 | * Return FAIL when no single file was found. In this case "num_file" is not |
| 6190 | * set, and "file" may contain an error message. |
| 6191 | * Return OK when some files found. "num_file" is set to the number of |
| 6192 | * matches, "file" to the array of matches. Call FreeWild() later. |
| 6193 | */ |
| 6194 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6195 | gen_expand_wildcards( |
| 6196 | int num_pat, /* number of input patterns */ |
| 6197 | char_u **pat, /* array of input patterns */ |
| 6198 | int *num_file, /* resulting number of files */ |
| 6199 | char_u ***file, /* array of resulting files */ |
| 6200 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6201 | { |
| 6202 | int i; |
| 6203 | garray_T ga; |
| 6204 | char_u *p; |
| 6205 | static int recursive = FALSE; |
| 6206 | int add_pat; |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 6207 | int retval = OK; |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 6208 | #if defined(FEAT_SEARCHPATH) |
| 6209 | int did_expand_in_path = FALSE; |
| 6210 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6211 | |
| 6212 | /* |
| 6213 | * expand_env() is called to expand things like "~user". If this fails, |
| 6214 | * it calls ExpandOne(), which brings us back here. In this case, always |
| 6215 | * call the machine specific expansion function, if possible. Otherwise, |
| 6216 | * return FAIL. |
| 6217 | */ |
| 6218 | if (recursive) |
| 6219 | #ifdef SPECIAL_WILDCHAR |
| 6220 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 6221 | #else |
| 6222 | return FAIL; |
| 6223 | #endif |
| 6224 | |
| 6225 | #ifdef SPECIAL_WILDCHAR |
| 6226 | /* |
| 6227 | * If there are any special wildcard characters which we cannot handle |
| 6228 | * here, call machine specific function for all the expansion. This |
| 6229 | * avoids starting the shell for each argument separately. |
| 6230 | * For `=expr` do use the internal function. |
| 6231 | */ |
| 6232 | for (i = 0; i < num_pat; i++) |
| 6233 | { |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6234 | if (has_special_wildchar(pat[i]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6235 | # ifdef VIM_BACKTICK |
| 6236 | && !(vim_backtick(pat[i]) && pat[i][1] == '=') |
| 6237 | # endif |
| 6238 | ) |
| 6239 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 6240 | } |
| 6241 | #endif |
| 6242 | |
| 6243 | recursive = TRUE; |
| 6244 | |
| 6245 | /* |
| 6246 | * The matching file names are stored in a growarray. Init it empty. |
| 6247 | */ |
| 6248 | ga_init2(&ga, (int)sizeof(char_u *), 30); |
| 6249 | |
| 6250 | for (i = 0; i < num_pat; ++i) |
| 6251 | { |
| 6252 | add_pat = -1; |
| 6253 | p = pat[i]; |
| 6254 | |
| 6255 | #ifdef VIM_BACKTICK |
| 6256 | if (vim_backtick(p)) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 6257 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6258 | add_pat = expand_backtick(&ga, p, flags); |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 6259 | if (add_pat == -1) |
| 6260 | retval = FAIL; |
| 6261 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6262 | else |
| 6263 | #endif |
| 6264 | { |
| 6265 | /* |
| 6266 | * First expand environment variables, "~/" and "~user/". |
| 6267 | */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6268 | if (has_env_var(p) || *p == '~') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6269 | { |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 6270 | p = expand_env_save_opt(p, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6271 | if (p == NULL) |
| 6272 | p = pat[i]; |
| 6273 | #ifdef UNIX |
| 6274 | /* |
| 6275 | * On Unix, if expand_env() can't expand an environment |
| 6276 | * variable, use the shell to do that. Discard previously |
| 6277 | * found file names and start all over again. |
| 6278 | */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 6279 | else if (has_env_var(p) || *p == '~') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6280 | { |
| 6281 | vim_free(p); |
Bram Moolenaar | 782027e | 2009-06-24 14:25:49 +0000 | [diff] [blame] | 6282 | ga_clear_strings(&ga); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6283 | i = mch_expand_wildcards(num_pat, pat, num_file, file, |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 6284 | flags|EW_KEEPDOLLAR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6285 | recursive = FALSE; |
| 6286 | return i; |
| 6287 | } |
| 6288 | #endif |
| 6289 | } |
| 6290 | |
| 6291 | /* |
| 6292 | * If there are wildcards: Expand file names and add each match to |
| 6293 | * the list. If there is no match, and EW_NOTFOUND is given, add |
| 6294 | * the pattern. |
| 6295 | * If there are no wildcards: Add the file name if it exists or |
| 6296 | * when EW_NOTFOUND is given. |
| 6297 | */ |
| 6298 | if (mch_has_exp_wildcard(p)) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 6299 | { |
| 6300 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 6301 | if ((flags & EW_PATH) |
| 6302 | && !mch_isFullName(p) |
| 6303 | && !(p[0] == '.' |
| 6304 | && (vim_ispathsep(p[1]) |
| 6305 | || (p[1] == '.' && vim_ispathsep(p[2])))) |
| 6306 | ) |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 6307 | { |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 6308 | /* :find completion where 'path' is used. |
| 6309 | * Recursiveness is OK here. */ |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 6310 | recursive = FALSE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 6311 | add_pat = expand_in_path(&ga, p, flags); |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 6312 | recursive = TRUE; |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 6313 | did_expand_in_path = TRUE; |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 6314 | } |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 6315 | else |
| 6316 | #endif |
| 6317 | add_pat = mch_expandpath(&ga, p, flags); |
| 6318 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6319 | } |
| 6320 | |
| 6321 | if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) |
| 6322 | { |
| 6323 | char_u *t = backslash_halve_save(p); |
| 6324 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6325 | /* When EW_NOTFOUND is used, always add files and dirs. Makes |
| 6326 | * "vim c:/" work. */ |
| 6327 | if (flags & EW_NOTFOUND) |
| 6328 | addfile(&ga, t, flags | EW_DIR | EW_FILE); |
Bram Moolenaar | 00efded | 2016-07-07 14:29:10 +0200 | [diff] [blame] | 6329 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6330 | addfile(&ga, t, flags); |
| 6331 | vim_free(t); |
| 6332 | } |
| 6333 | |
Bram Moolenaar | b28ebbc | 2010-07-14 16:59:57 +0200 | [diff] [blame] | 6334 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 6335 | if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH)) |
Bram Moolenaar | b28ebbc | 2010-07-14 16:59:57 +0200 | [diff] [blame] | 6336 | uniquefy_paths(&ga, p); |
| 6337 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6338 | if (p != pat[i]) |
| 6339 | vim_free(p); |
| 6340 | } |
| 6341 | |
| 6342 | *num_file = ga.ga_len; |
| 6343 | *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; |
| 6344 | |
| 6345 | recursive = FALSE; |
| 6346 | |
Bram Moolenaar | 336bd62 | 2016-01-17 18:23:58 +0100 | [diff] [blame] | 6347 | return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6348 | } |
| 6349 | |
| 6350 | # ifdef VIM_BACKTICK |
| 6351 | |
| 6352 | /* |
| 6353 | * Return TRUE if we can expand this backtick thing here. |
| 6354 | */ |
| 6355 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6356 | vim_backtick(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6357 | { |
| 6358 | return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); |
| 6359 | } |
| 6360 | |
| 6361 | /* |
| 6362 | * Expand an item in `backticks` by executing it as a command. |
| 6363 | * Currently only works when pat[] starts and ends with a `. |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 6364 | * Returns number of file names found, -1 if an error is encountered. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6365 | */ |
| 6366 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6367 | expand_backtick( |
| 6368 | garray_T *gap, |
| 6369 | char_u *pat, |
| 6370 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6371 | { |
| 6372 | char_u *p; |
| 6373 | char_u *cmd; |
| 6374 | char_u *buffer; |
| 6375 | int cnt = 0; |
| 6376 | int i; |
| 6377 | |
| 6378 | /* Create the command: lop off the backticks. */ |
| 6379 | cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); |
| 6380 | if (cmd == NULL) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 6381 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6382 | |
| 6383 | #ifdef FEAT_EVAL |
| 6384 | if (*cmd == '=') /* `={expr}`: Expand expression */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 6385 | buffer = eval_to_string(cmd + 1, &p, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6386 | else |
| 6387 | #endif |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 6388 | buffer = get_cmd_output(cmd, NULL, |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 6389 | (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6390 | vim_free(cmd); |
| 6391 | if (buffer == NULL) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 6392 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6393 | |
| 6394 | cmd = buffer; |
| 6395 | while (*cmd != NUL) |
| 6396 | { |
| 6397 | cmd = skipwhite(cmd); /* skip over white space */ |
| 6398 | p = cmd; |
| 6399 | while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */ |
| 6400 | ++p; |
| 6401 | /* add an entry if it is not empty */ |
| 6402 | if (p > cmd) |
| 6403 | { |
| 6404 | i = *p; |
| 6405 | *p = NUL; |
| 6406 | addfile(gap, cmd, flags); |
| 6407 | *p = i; |
| 6408 | ++cnt; |
| 6409 | } |
| 6410 | cmd = p; |
| 6411 | while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) |
| 6412 | ++cmd; |
| 6413 | } |
| 6414 | |
| 6415 | vim_free(buffer); |
| 6416 | return cnt; |
| 6417 | } |
| 6418 | # endif /* VIM_BACKTICK */ |
| 6419 | |
| 6420 | /* |
| 6421 | * Add a file to a file list. Accepted flags: |
| 6422 | * EW_DIR add directories |
| 6423 | * EW_FILE add files |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 6424 | * EW_EXEC add executable files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6425 | * EW_NOTFOUND add even when it doesn't exist |
| 6426 | * EW_ADDSLASH add slash after directory name |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 6427 | * EW_ALLLINKS add symlink also when the referred file does not exist |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6428 | */ |
| 6429 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6430 | addfile( |
| 6431 | garray_T *gap, |
| 6432 | char_u *f, /* filename */ |
| 6433 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6434 | { |
| 6435 | char_u *p; |
| 6436 | int isdir; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 6437 | stat_T sb; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6438 | |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 6439 | /* if the file/dir/link doesn't exist, may not add it */ |
| 6440 | if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS) |
Bram Moolenaar | ab11a59 | 2015-03-06 22:00:11 +0100 | [diff] [blame] | 6441 | ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6442 | return; |
| 6443 | |
| 6444 | #ifdef FNAME_ILLEGAL |
| 6445 | /* if the file/dir contains illegal characters, don't add it */ |
| 6446 | if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) |
| 6447 | return; |
| 6448 | #endif |
| 6449 | |
| 6450 | isdir = mch_isdir(f); |
| 6451 | if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) |
| 6452 | return; |
| 6453 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 6454 | /* If the file isn't executable, may not add it. Do accept directories. |
| 6455 | * When invoked from expand_shellcmd() do not use $PATH. */ |
| 6456 | if (!isdir && (flags & EW_EXEC) |
| 6457 | && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD))) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 6458 | return; |
| 6459 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6460 | /* Make room for another item in the file list. */ |
| 6461 | if (ga_grow(gap, 1) == FAIL) |
| 6462 | return; |
| 6463 | |
| 6464 | p = alloc((unsigned)(STRLEN(f) + 1 + isdir)); |
| 6465 | if (p == NULL) |
| 6466 | return; |
| 6467 | |
| 6468 | STRCPY(p, f); |
| 6469 | #ifdef BACKSLASH_IN_FILENAME |
| 6470 | slash_adjust(p); |
| 6471 | #endif |
| 6472 | /* |
| 6473 | * Append a slash or backslash after directory names if none is present. |
| 6474 | */ |
| 6475 | #ifndef DONT_ADD_PATHSEP_TO_DIR |
| 6476 | if (isdir && (flags & EW_ADDSLASH)) |
| 6477 | add_pathsep(p); |
| 6478 | #endif |
| 6479 | ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6480 | } |
| 6481 | #endif /* !NO_EXPANDPATH */ |
| 6482 | |
| 6483 | #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO) |
| 6484 | |
| 6485 | #ifndef SEEK_SET |
| 6486 | # define SEEK_SET 0 |
| 6487 | #endif |
| 6488 | #ifndef SEEK_END |
| 6489 | # define SEEK_END 2 |
| 6490 | #endif |
| 6491 | |
| 6492 | /* |
| 6493 | * Get the stdout of an external command. |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 6494 | * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not |
| 6495 | * NULL store the length there. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6496 | * Returns an allocated string, or NULL for error. |
| 6497 | */ |
| 6498 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6499 | get_cmd_output( |
| 6500 | char_u *cmd, |
| 6501 | char_u *infile, /* optional input file name */ |
| 6502 | int flags, /* can be SHELL_SILENT */ |
| 6503 | int *ret_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6504 | { |
| 6505 | char_u *tempname; |
| 6506 | char_u *command; |
| 6507 | char_u *buffer = NULL; |
| 6508 | int len; |
| 6509 | int i = 0; |
| 6510 | FILE *fd; |
| 6511 | |
| 6512 | if (check_restricted() || check_secure()) |
| 6513 | return NULL; |
| 6514 | |
| 6515 | /* get a name for the temp file */ |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 6516 | if ((tempname = vim_tempname('o', FALSE)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6517 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 6518 | emsg(_(e_notmp)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6519 | return NULL; |
| 6520 | } |
| 6521 | |
| 6522 | /* Add the redirection stuff */ |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 6523 | command = make_filter_cmd(cmd, infile, tempname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6524 | if (command == NULL) |
| 6525 | goto done; |
| 6526 | |
| 6527 | /* |
| 6528 | * Call the shell to execute the command (errors are ignored). |
| 6529 | * Don't check timestamps here. |
| 6530 | */ |
| 6531 | ++no_check_timestamps; |
| 6532 | call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags); |
| 6533 | --no_check_timestamps; |
| 6534 | |
| 6535 | vim_free(command); |
| 6536 | |
| 6537 | /* |
| 6538 | * read the names from the file into memory |
| 6539 | */ |
| 6540 | # ifdef VMS |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 6541 | /* created temporary file is not always readable as binary */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6542 | fd = mch_fopen((char *)tempname, "r"); |
| 6543 | # else |
| 6544 | fd = mch_fopen((char *)tempname, READBIN); |
| 6545 | # endif |
| 6546 | |
| 6547 | if (fd == NULL) |
| 6548 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 6549 | semsg(_(e_notopen), tempname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6550 | goto done; |
| 6551 | } |
| 6552 | |
| 6553 | fseek(fd, 0L, SEEK_END); |
| 6554 | len = ftell(fd); /* get size of temp file */ |
| 6555 | fseek(fd, 0L, SEEK_SET); |
| 6556 | |
| 6557 | buffer = alloc(len + 1); |
| 6558 | if (buffer != NULL) |
| 6559 | i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd); |
| 6560 | fclose(fd); |
| 6561 | mch_remove(tempname); |
| 6562 | if (buffer == NULL) |
| 6563 | goto done; |
| 6564 | #ifdef VMS |
| 6565 | len = i; /* VMS doesn't give us what we asked for... */ |
| 6566 | #endif |
| 6567 | if (i != len) |
| 6568 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 6569 | semsg(_(e_notread), tempname); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 6570 | VIM_CLEAR(buffer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6571 | } |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 6572 | else if (ret_len == NULL) |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 6573 | { |
| 6574 | /* Change NUL into SOH, otherwise the string is truncated. */ |
| 6575 | for (i = 0; i < len; ++i) |
Bram Moolenaar | f40f4ab | 2013-08-03 17:31:28 +0200 | [diff] [blame] | 6576 | if (buffer[i] == NUL) |
| 6577 | buffer[i] = 1; |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 6578 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 6579 | buffer[len] = NUL; /* make sure the buffer is terminated */ |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 6580 | } |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 6581 | else |
| 6582 | *ret_len = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6583 | |
| 6584 | done: |
| 6585 | vim_free(tempname); |
| 6586 | return buffer; |
| 6587 | } |
| 6588 | #endif |
| 6589 | |
| 6590 | /* |
| 6591 | * Free the list of files returned by expand_wildcards() or other expansion |
| 6592 | * functions. |
| 6593 | */ |
| 6594 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6595 | FreeWild(int count, char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6596 | { |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 6597 | if (count <= 0 || files == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6598 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6599 | while (count--) |
| 6600 | vim_free(files[count]); |
| 6601 | vim_free(files); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6602 | } |
| 6603 | |
| 6604 | /* |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 6605 | * Return TRUE when need to go to Insert mode because of 'insertmode'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6606 | * Don't do this when still processing a command or a mapping. |
| 6607 | * Don't do this when inside a ":normal" command. |
| 6608 | */ |
| 6609 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6610 | goto_im(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6611 | { |
| 6612 | return (p_im && stuff_empty() && typebuf_typed()); |
| 6613 | } |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 6614 | |
| 6615 | /* |
Bram Moolenaar | 050fe7e | 2014-05-22 14:00:16 +0200 | [diff] [blame] | 6616 | * Returns the isolated name of the shell in allocated memory: |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 6617 | * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f". |
| 6618 | * - Remove any argument. E.g., "csh -f" -> "csh". |
| 6619 | * But don't allow a space in the path, so that this works: |
| 6620 | * "/usr/bin/csh --rcfile ~/.cshrc" |
| 6621 | * But don't do that for Windows, it's common to have a space in the path. |
| 6622 | */ |
| 6623 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6624 | get_isolated_shell_name(void) |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 6625 | { |
| 6626 | char_u *p; |
| 6627 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 6628 | #ifdef MSWIN |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 6629 | p = gettail(p_sh); |
| 6630 | p = vim_strnsave(p, (int)(skiptowhite(p) - p)); |
| 6631 | #else |
| 6632 | p = skiptowhite(p_sh); |
| 6633 | if (*p == NUL) |
| 6634 | { |
| 6635 | /* No white space, use the tail. */ |
| 6636 | p = vim_strsave(gettail(p_sh)); |
| 6637 | } |
| 6638 | else |
| 6639 | { |
| 6640 | char_u *p1, *p2; |
| 6641 | |
| 6642 | /* Find the last path separator before the space. */ |
| 6643 | p1 = p_sh; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 6644 | for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2)) |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 6645 | if (vim_ispathsep(*p2)) |
| 6646 | p1 = p2 + 1; |
| 6647 | p = vim_strnsave(p1, (int)(p - p1)); |
| 6648 | } |
| 6649 | #endif |
| 6650 | return p; |
| 6651 | } |
Bram Moolenaar | 5fd0f50 | 2019-02-13 23:13:28 +0100 | [diff] [blame] | 6652 | |
| 6653 | /* |
| 6654 | * Check if the "://" of a URL is at the pointer, return URL_SLASH. |
| 6655 | * Also check for ":\\", which MS Internet Explorer accepts, return |
| 6656 | * URL_BACKSLASH. |
| 6657 | */ |
| 6658 | int |
| 6659 | path_is_url(char_u *p) |
| 6660 | { |
| 6661 | if (STRNCMP(p, "://", (size_t)3) == 0) |
| 6662 | return URL_SLASH; |
| 6663 | else if (STRNCMP(p, ":\\\\", (size_t)3) == 0) |
| 6664 | return URL_BACKSLASH; |
| 6665 | return 0; |
| 6666 | } |
| 6667 | |
| 6668 | /* |
| 6669 | * Check if "fname" starts with "name://". Return URL_SLASH if it does. |
| 6670 | * Return URL_BACKSLASH for "name:\\". |
| 6671 | * Return zero otherwise. |
| 6672 | */ |
| 6673 | int |
| 6674 | path_with_url(char_u *fname) |
| 6675 | { |
| 6676 | char_u *p; |
| 6677 | |
| 6678 | for (p = fname; isalpha(*p); ++p) |
| 6679 | ; |
| 6680 | return path_is_url(p); |
| 6681 | } |
| 6682 | |
| 6683 | /* |
| 6684 | * Return TRUE if "name" is a full (absolute) path name or URL. |
| 6685 | */ |
| 6686 | int |
| 6687 | vim_isAbsName(char_u *name) |
| 6688 | { |
| 6689 | return (path_with_url(name) != 0 || mch_isFullName(name)); |
| 6690 | } |
| 6691 | |
| 6692 | /* |
| 6693 | * Get absolute file name into buffer "buf[len]". |
| 6694 | * |
| 6695 | * return FAIL for failure, OK otherwise |
| 6696 | */ |
| 6697 | int |
| 6698 | vim_FullName( |
| 6699 | char_u *fname, |
| 6700 | char_u *buf, |
| 6701 | int len, |
| 6702 | int force) /* force expansion even when already absolute */ |
| 6703 | { |
| 6704 | int retval = OK; |
| 6705 | int url; |
| 6706 | |
| 6707 | *buf = NUL; |
| 6708 | if (fname == NULL) |
| 6709 | return FAIL; |
| 6710 | |
| 6711 | url = path_with_url(fname); |
| 6712 | if (!url) |
| 6713 | retval = mch_FullName(fname, buf, len, force); |
| 6714 | if (url || retval == FAIL) |
| 6715 | { |
| 6716 | /* something failed; use the file name (truncate when too long) */ |
| 6717 | vim_strncpy(buf, fname, len - 1); |
| 6718 | } |
| 6719 | #if defined(MSWIN) |
| 6720 | slash_adjust(buf); |
| 6721 | #endif |
| 6722 | return retval; |
| 6723 | } |