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 | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 17 | static char_u *vim_version_dir(char_u *vimdir); |
| 18 | static char_u *remove_tail(char_u *p, char_u *pend, char_u *name); |
Bram Moolenaar | 06ae70d | 2013-06-17 19:26:36 +0200 | [diff] [blame] | 19 | #if defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 20 | static void init_users(void); |
Bram Moolenaar | 06ae70d | 2013-06-17 19:26:36 +0200 | [diff] [blame] | 21 | #endif |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 22 | static int copy_indent(int size, char_u *src); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 23 | |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 24 | /* All user names (for ~user completion as done by shell). */ |
| 25 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 26 | static garray_T ga_users; |
| 27 | #endif |
| 28 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | /* |
| 30 | * Count the size (in window cells) of the indent in the current line. |
| 31 | */ |
| 32 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 33 | get_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 34 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 35 | return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /* |
| 39 | * Count the size (in window cells) of the indent in line "lnum". |
| 40 | */ |
| 41 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 42 | get_indent_lnum(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 43 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 44 | return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | #if defined(FEAT_FOLDING) || defined(PROTO) |
| 48 | /* |
| 49 | * Count the size (in window cells) of the indent in line "lnum" of buffer |
| 50 | * "buf". |
| 51 | */ |
| 52 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 53 | get_indent_buf(buf_T *buf, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 54 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 55 | return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 56 | } |
| 57 | #endif |
| 58 | |
| 59 | /* |
| 60 | * count the size (in window cells) of the indent in line "ptr", with |
| 61 | * 'tabstop' at "ts" |
| 62 | */ |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 63 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 64 | get_indent_str( |
| 65 | char_u *ptr, |
| 66 | int ts, |
| 67 | int list) /* if TRUE, count only screen size for tabs */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 68 | { |
| 69 | int count = 0; |
| 70 | |
| 71 | for ( ; *ptr; ++ptr) |
| 72 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 73 | if (*ptr == TAB) |
| 74 | { |
| 75 | if (!list || lcs_tab1) /* count a tab for what it is worth */ |
| 76 | count += ts - (count % ts); |
| 77 | else |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 78 | /* In list mode, when tab is not set, count screen char width |
| 79 | * for Tab, displays: ^I */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 80 | count += ptr2cells(ptr); |
| 81 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 82 | else if (*ptr == ' ') |
| 83 | ++count; /* count a space for one */ |
| 84 | else |
| 85 | break; |
| 86 | } |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 87 | return count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Set the indent of the current line. |
| 92 | * Leaves the cursor on the first non-blank in the line. |
| 93 | * Caller must take care of undo. |
| 94 | * "flags": |
| 95 | * SIN_CHANGED: call changed_bytes() if the line was changed. |
| 96 | * SIN_INSERT: insert the indent in front of the line. |
| 97 | * SIN_UNDO: save line for undo before changing it. |
| 98 | * Returns TRUE if the line was changed. |
| 99 | */ |
| 100 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 101 | set_indent( |
| 102 | int size, /* measured in spaces */ |
| 103 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 104 | { |
| 105 | char_u *p; |
| 106 | char_u *newline; |
| 107 | char_u *oldline; |
| 108 | char_u *s; |
| 109 | int todo; |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 110 | int ind_len; /* measured in characters */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 111 | int line_len; |
| 112 | int doit = FALSE; |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 113 | int ind_done = 0; /* measured in spaces */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 114 | int tab_pad; |
Bram Moolenaar | 5409c05 | 2005-03-18 20:27:04 +0000 | [diff] [blame] | 115 | int retval = FALSE; |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 116 | int orig_char_len = -1; /* number of initial whitespace chars when |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 117 | 'et' and 'pi' are both set */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * First check if there is anything to do and compute the number of |
| 121 | * characters needed for the indent. |
| 122 | */ |
| 123 | todo = size; |
| 124 | ind_len = 0; |
| 125 | p = oldline = ml_get_curline(); |
| 126 | |
| 127 | /* Calculate the buffer size for the new indent, and check to see if it |
| 128 | * isn't already set */ |
| 129 | |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 130 | /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and |
| 131 | * 'preserveindent' are set count the number of characters at the |
| 132 | * beginning of the line to be copied */ |
| 133 | if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 134 | { |
| 135 | /* If 'preserveindent' is set then reuse as much as possible of |
| 136 | * the existing indent structure for the new indent */ |
| 137 | if (!(flags & SIN_INSERT) && curbuf->b_p_pi) |
| 138 | { |
| 139 | ind_done = 0; |
| 140 | |
| 141 | /* count as many characters as we can use */ |
| 142 | while (todo > 0 && vim_iswhite(*p)) |
| 143 | { |
| 144 | if (*p == TAB) |
| 145 | { |
| 146 | tab_pad = (int)curbuf->b_p_ts |
| 147 | - (ind_done % (int)curbuf->b_p_ts); |
| 148 | /* stop if this tab will overshoot the target */ |
| 149 | if (todo < tab_pad) |
| 150 | break; |
| 151 | todo -= tab_pad; |
| 152 | ++ind_len; |
| 153 | ind_done += tab_pad; |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | --todo; |
| 158 | ++ind_len; |
| 159 | ++ind_done; |
| 160 | } |
| 161 | ++p; |
| 162 | } |
| 163 | |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 164 | /* Set initial number of whitespace chars to copy if we are |
| 165 | * preserving indent but expandtab is set */ |
| 166 | if (curbuf->b_p_et) |
| 167 | orig_char_len = ind_len; |
| 168 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 169 | /* Fill to next tabstop with a tab, if possible */ |
| 170 | tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 171 | if (todo >= tab_pad && orig_char_len == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 172 | { |
| 173 | doit = TRUE; |
| 174 | todo -= tab_pad; |
| 175 | ++ind_len; |
| 176 | /* ind_done += tab_pad; */ |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* count tabs required for indent */ |
| 181 | while (todo >= (int)curbuf->b_p_ts) |
| 182 | { |
| 183 | if (*p != TAB) |
| 184 | doit = TRUE; |
| 185 | else |
| 186 | ++p; |
| 187 | todo -= (int)curbuf->b_p_ts; |
| 188 | ++ind_len; |
| 189 | /* ind_done += (int)curbuf->b_p_ts; */ |
| 190 | } |
| 191 | } |
| 192 | /* count spaces required for indent */ |
| 193 | while (todo > 0) |
| 194 | { |
| 195 | if (*p != ' ') |
| 196 | doit = TRUE; |
| 197 | else |
| 198 | ++p; |
| 199 | --todo; |
| 200 | ++ind_len; |
| 201 | /* ++ind_done; */ |
| 202 | } |
| 203 | |
| 204 | /* Return if the indent is OK already. */ |
| 205 | if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT)) |
| 206 | return FALSE; |
| 207 | |
| 208 | /* Allocate memory for the new line. */ |
| 209 | if (flags & SIN_INSERT) |
| 210 | p = oldline; |
| 211 | else |
| 212 | p = skipwhite(p); |
| 213 | line_len = (int)STRLEN(p) + 1; |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 214 | |
| 215 | /* If 'preserveindent' and 'expandtab' are both set keep the original |
| 216 | * characters and allocate accordingly. We will fill the rest with spaces |
| 217 | * after the if (!curbuf->b_p_et) below. */ |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 218 | if (orig_char_len != -1) |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 219 | { |
| 220 | newline = alloc(orig_char_len + size - ind_done + line_len); |
| 221 | if (newline == NULL) |
| 222 | return FALSE; |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 223 | todo = size - ind_done; |
| 224 | ind_len = orig_char_len + todo; /* Set total length of indent in |
| 225 | * characters, which may have been |
| 226 | * undercounted until now */ |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 227 | p = oldline; |
| 228 | s = newline; |
| 229 | while (orig_char_len > 0) |
| 230 | { |
| 231 | *s++ = *p++; |
| 232 | orig_char_len--; |
| 233 | } |
Bram Moolenaar | 913626c | 2008-01-03 11:43:42 +0000 | [diff] [blame] | 234 | |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 235 | /* Skip over any additional white space (useful when newindent is less |
| 236 | * than old) */ |
| 237 | while (vim_iswhite(*p)) |
Bram Moolenaar | 913626c | 2008-01-03 11:43:42 +0000 | [diff] [blame] | 238 | ++p; |
Bram Moolenaar | cc00b95 | 2007-08-11 12:32:57 +0000 | [diff] [blame] | 239 | |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 240 | } |
| 241 | else |
| 242 | { |
| 243 | todo = size; |
| 244 | newline = alloc(ind_len + line_len); |
| 245 | if (newline == NULL) |
| 246 | return FALSE; |
| 247 | s = newline; |
| 248 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 249 | |
| 250 | /* Put the characters in the new line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 251 | /* if 'expandtab' isn't set: use TABs */ |
| 252 | if (!curbuf->b_p_et) |
| 253 | { |
| 254 | /* If 'preserveindent' is set then reuse as much as possible of |
| 255 | * the existing indent structure for the new indent */ |
| 256 | if (!(flags & SIN_INSERT) && curbuf->b_p_pi) |
| 257 | { |
| 258 | p = oldline; |
| 259 | ind_done = 0; |
| 260 | |
| 261 | while (todo > 0 && vim_iswhite(*p)) |
| 262 | { |
| 263 | if (*p == TAB) |
| 264 | { |
| 265 | tab_pad = (int)curbuf->b_p_ts |
| 266 | - (ind_done % (int)curbuf->b_p_ts); |
| 267 | /* stop if this tab will overshoot the target */ |
| 268 | if (todo < tab_pad) |
| 269 | break; |
| 270 | todo -= tab_pad; |
| 271 | ind_done += tab_pad; |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | --todo; |
| 276 | ++ind_done; |
| 277 | } |
| 278 | *s++ = *p++; |
| 279 | } |
| 280 | |
| 281 | /* Fill to next tabstop with a tab, if possible */ |
| 282 | tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
| 283 | if (todo >= tab_pad) |
| 284 | { |
| 285 | *s++ = TAB; |
| 286 | todo -= tab_pad; |
| 287 | } |
| 288 | |
| 289 | p = skipwhite(p); |
| 290 | } |
| 291 | |
| 292 | while (todo >= (int)curbuf->b_p_ts) |
| 293 | { |
| 294 | *s++ = TAB; |
| 295 | todo -= (int)curbuf->b_p_ts; |
| 296 | } |
| 297 | } |
| 298 | while (todo > 0) |
| 299 | { |
| 300 | *s++ = ' '; |
| 301 | --todo; |
| 302 | } |
| 303 | mch_memmove(s, p, (size_t)line_len); |
| 304 | |
| 305 | /* Replace the line (unless undo fails). */ |
| 306 | if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK) |
| 307 | { |
| 308 | ml_replace(curwin->w_cursor.lnum, newline, FALSE); |
| 309 | if (flags & SIN_CHANGED) |
| 310 | changed_bytes(curwin->w_cursor.lnum, 0); |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 311 | /* Correct saved cursor position if it is in this line. */ |
| 312 | if (saved_cursor.lnum == curwin->w_cursor.lnum) |
| 313 | { |
| 314 | if (saved_cursor.col >= (colnr_T)(p - oldline)) |
| 315 | /* cursor was after the indent, adjust for the number of |
| 316 | * bytes added/removed */ |
| 317 | saved_cursor.col += ind_len - (colnr_T)(p - oldline); |
| 318 | else if (saved_cursor.col >= (colnr_T)(s - newline)) |
| 319 | /* cursor was in the indent, and is now after it, put it back |
| 320 | * at the start of the indent (replacing spaces with TAB) */ |
| 321 | saved_cursor.col = (colnr_T)(s - newline); |
| 322 | } |
Bram Moolenaar | 5409c05 | 2005-03-18 20:27:04 +0000 | [diff] [blame] | 323 | retval = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 324 | } |
| 325 | else |
| 326 | vim_free(newline); |
| 327 | |
| 328 | curwin->w_cursor.col = ind_len; |
Bram Moolenaar | 5409c05 | 2005-03-18 20:27:04 +0000 | [diff] [blame] | 329 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Copy the indent from ptr to the current line (and fill to size) |
| 334 | * Leaves the cursor on the first non-blank in the line. |
| 335 | * Returns TRUE if the line was changed. |
| 336 | */ |
| 337 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 338 | copy_indent(int size, char_u *src) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 339 | { |
| 340 | char_u *p = NULL; |
| 341 | char_u *line = NULL; |
| 342 | char_u *s; |
| 343 | int todo; |
| 344 | int ind_len; |
| 345 | int line_len = 0; |
| 346 | int tab_pad; |
| 347 | int ind_done; |
| 348 | int round; |
| 349 | |
| 350 | /* Round 1: compute the number of characters needed for the indent |
| 351 | * Round 2: copy the characters. */ |
| 352 | for (round = 1; round <= 2; ++round) |
| 353 | { |
| 354 | todo = size; |
| 355 | ind_len = 0; |
| 356 | ind_done = 0; |
| 357 | s = src; |
| 358 | |
| 359 | /* Count/copy the usable portion of the source line */ |
| 360 | while (todo > 0 && vim_iswhite(*s)) |
| 361 | { |
| 362 | if (*s == TAB) |
| 363 | { |
| 364 | tab_pad = (int)curbuf->b_p_ts |
| 365 | - (ind_done % (int)curbuf->b_p_ts); |
| 366 | /* Stop if this tab will overshoot the target */ |
| 367 | if (todo < tab_pad) |
| 368 | break; |
| 369 | todo -= tab_pad; |
| 370 | ind_done += tab_pad; |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | --todo; |
| 375 | ++ind_done; |
| 376 | } |
| 377 | ++ind_len; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 378 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 379 | *p++ = *s; |
| 380 | ++s; |
| 381 | } |
| 382 | |
| 383 | /* Fill to next tabstop with a tab, if possible */ |
| 384 | tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | c42e7ed | 2011-09-07 19:58:09 +0200 | [diff] [blame] | 385 | if (todo >= tab_pad && !curbuf->b_p_et) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 386 | { |
| 387 | todo -= tab_pad; |
| 388 | ++ind_len; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 389 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 390 | *p++ = TAB; |
| 391 | } |
| 392 | |
| 393 | /* Add tabs required for indent */ |
Bram Moolenaar | c42e7ed | 2011-09-07 19:58:09 +0200 | [diff] [blame] | 394 | while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 395 | { |
| 396 | todo -= (int)curbuf->b_p_ts; |
| 397 | ++ind_len; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 398 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 399 | *p++ = TAB; |
| 400 | } |
| 401 | |
| 402 | /* Count/add spaces required for indent */ |
| 403 | while (todo > 0) |
| 404 | { |
| 405 | --todo; |
| 406 | ++ind_len; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 407 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 408 | *p++ = ' '; |
| 409 | } |
| 410 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 411 | if (p == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 412 | { |
| 413 | /* Allocate memory for the result: the copied indent, new indent |
| 414 | * and the rest of the line. */ |
| 415 | line_len = (int)STRLEN(ml_get_curline()) + 1; |
| 416 | line = alloc(ind_len + line_len); |
| 417 | if (line == NULL) |
| 418 | return FALSE; |
| 419 | p = line; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /* Append the original line */ |
| 424 | mch_memmove(p, ml_get_curline(), (size_t)line_len); |
| 425 | |
| 426 | /* Replace the line */ |
| 427 | ml_replace(curwin->w_cursor.lnum, line, FALSE); |
| 428 | |
| 429 | /* Put the cursor after the indent. */ |
| 430 | curwin->w_cursor.col = ind_len; |
| 431 | return TRUE; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Return the indent of the current line after a number. Return -1 if no |
| 436 | * number was found. Used for 'n' in 'formatoptions': numbered list. |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 437 | * Since a pattern is used it can actually handle more than numbers. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 438 | */ |
| 439 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 440 | get_number_indent(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 441 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 442 | colnr_T col; |
| 443 | pos_T pos; |
| 444 | |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 445 | regmatch_T regmatch; |
| 446 | int lead_len = 0; /* length of comment leader */ |
| 447 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 448 | if (lnum > curbuf->b_ml.ml_line_count) |
| 449 | return -1; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 450 | pos.lnum = 0; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 451 | |
| 452 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 453 | /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */ |
| 454 | if ((State & INSERT) || has_format_option(FO_Q_COMS)) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 455 | lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE); |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 456 | #endif |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 457 | regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC); |
| 458 | if (regmatch.regprog != NULL) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 459 | { |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 460 | regmatch.rm_ic = FALSE; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 461 | |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 462 | /* vim_regexec() expects a pointer to a line. This lets us |
| 463 | * start matching for the flp beyond any comment leader... */ |
| 464 | if (vim_regexec(®match, ml_get(lnum) + lead_len, (colnr_T)0)) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 465 | { |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 466 | pos.lnum = lnum; |
| 467 | pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum)); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 468 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 469 | pos.coladd = 0; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 470 | #endif |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 471 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 472 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 473 | } |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 474 | |
| 475 | if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 476 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 477 | getvcol(curwin, &pos, &col, NULL, NULL); |
| 478 | return (int)col; |
| 479 | } |
| 480 | |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 481 | #if defined(FEAT_LINEBREAK) || defined(PROTO) |
| 482 | /* |
| 483 | * Return appropriate space number for breakindent, taking influencing |
| 484 | * parameters into account. Window must be specified, since it is not |
| 485 | * necessarily always the current one. |
| 486 | */ |
| 487 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 488 | get_breakindent_win( |
| 489 | win_T *wp, |
| 490 | char_u *line) /* start of the line */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 491 | { |
| 492 | static int prev_indent = 0; /* cached indent value */ |
| 493 | static long prev_ts = 0L; /* cached tabstop value */ |
| 494 | static char_u *prev_line = NULL; /* cached pointer to line */ |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 495 | static varnumber_T prev_tick = 0; /* changedtick of cached value */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 496 | int bri = 0; |
| 497 | /* window width minus window margin space, i.e. what rests for text */ |
| 498 | const int eff_wwidth = W_WIDTH(wp) |
| 499 | - ((wp->w_p_nu || wp->w_p_rnu) |
| 500 | && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL) |
| 501 | ? number_width(wp) + 1 : 0); |
| 502 | |
| 503 | /* used cached indent, unless pointer or 'tabstop' changed */ |
Bram Moolenaar | a40aa76 | 2014-06-25 22:55:38 +0200 | [diff] [blame] | 504 | if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 505 | || prev_tick != CHANGEDTICK(wp->w_buffer)) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 506 | { |
| 507 | prev_line = line; |
| 508 | prev_ts = wp->w_buffer->b_p_ts; |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 509 | prev_tick = CHANGEDTICK(wp->w_buffer); |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 510 | prev_indent = get_indent_str(line, |
Bram Moolenaar | 9d7a592 | 2014-06-26 21:24:56 +0200 | [diff] [blame] | 511 | (int)wp->w_buffer->b_p_ts, wp->w_p_list); |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 512 | } |
Bram Moolenaar | 9d7a592 | 2014-06-26 21:24:56 +0200 | [diff] [blame] | 513 | bri = prev_indent + wp->w_p_brishift; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 514 | |
| 515 | /* indent minus the length of the showbreak string */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 516 | if (wp->w_p_brisbr) |
| 517 | bri -= vim_strsize(p_sbr); |
| 518 | |
| 519 | /* Add offset for number column, if 'n' is in 'cpoptions' */ |
| 520 | bri += win_col_off2(wp); |
| 521 | |
| 522 | /* never indent past left window margin */ |
| 523 | if (bri < 0) |
| 524 | bri = 0; |
| 525 | /* always leave at least bri_min characters on the left, |
| 526 | * if text width is sufficient */ |
| 527 | else if (bri > eff_wwidth - wp->w_p_brimin) |
| 528 | bri = (eff_wwidth - wp->w_p_brimin < 0) |
| 529 | ? 0 : eff_wwidth - wp->w_p_brimin; |
| 530 | |
| 531 | return bri; |
| 532 | } |
| 533 | #endif |
| 534 | |
| 535 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 536 | #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) |
| 537 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 538 | static int cin_is_cinword(char_u *line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 539 | |
| 540 | /* |
| 541 | * Return TRUE if the string "line" starts with a word from 'cinwords'. |
| 542 | */ |
| 543 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 544 | cin_is_cinword(char_u *line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 545 | { |
| 546 | char_u *cinw; |
| 547 | char_u *cinw_buf; |
| 548 | int cinw_len; |
| 549 | int retval = FALSE; |
| 550 | int len; |
| 551 | |
| 552 | cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1; |
| 553 | cinw_buf = alloc((unsigned)cinw_len); |
| 554 | if (cinw_buf != NULL) |
| 555 | { |
| 556 | line = skipwhite(line); |
| 557 | for (cinw = curbuf->b_p_cinw; *cinw; ) |
| 558 | { |
| 559 | len = copy_option_part(&cinw, cinw_buf, cinw_len, ","); |
| 560 | if (STRNCMP(line, cinw_buf, len) == 0 |
| 561 | && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1]))) |
| 562 | { |
| 563 | retval = TRUE; |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | vim_free(cinw_buf); |
| 568 | } |
| 569 | return retval; |
| 570 | } |
| 571 | #endif |
| 572 | |
| 573 | /* |
| 574 | * open_line: Add a new line below or above the current line. |
| 575 | * |
| 576 | * For VREPLACE mode, we only add a new line when we get to the end of the |
| 577 | * file, otherwise we just start replacing the next line. |
| 578 | * |
| 579 | * Caller must take care of undo. Since VREPLACE may affect any number of |
| 580 | * lines however, it may call u_save_cursor() again when starting to change a |
| 581 | * new line. |
| 582 | * "flags": OPENLINE_DELSPACES delete spaces after cursor |
| 583 | * OPENLINE_DO_COM format comments |
| 584 | * OPENLINE_KEEPTRAIL keep trailing spaces |
| 585 | * OPENLINE_MARKFIX adjust mark positions after the line break |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 586 | * OPENLINE_COM_LIST format comments with list or 2nd line indent |
| 587 | * |
| 588 | * "second_line_indent": indent for after ^^D in Insert mode or if flag |
| 589 | * OPENLINE_COM_LIST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 590 | * |
| 591 | * Return TRUE for success, FALSE for failure |
| 592 | */ |
| 593 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 594 | open_line( |
| 595 | int dir, /* FORWARD or BACKWARD */ |
| 596 | int flags, |
| 597 | int second_line_indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 598 | { |
| 599 | char_u *saved_line; /* copy of the original line */ |
| 600 | char_u *next_line = NULL; /* copy of the next line */ |
| 601 | char_u *p_extra = NULL; /* what goes to next line */ |
| 602 | int less_cols = 0; /* less columns for mark in new line */ |
| 603 | int less_cols_off = 0; /* columns to skip for mark adjust */ |
| 604 | pos_T old_cursor; /* old cursor position */ |
| 605 | int newcol = 0; /* new cursor column */ |
| 606 | int newindent = 0; /* auto-indent of the new line */ |
| 607 | int n; |
| 608 | int trunc_line = FALSE; /* truncate current line afterwards */ |
| 609 | int retval = FALSE; /* return value, default is FAIL */ |
| 610 | #ifdef FEAT_COMMENTS |
| 611 | int extra_len = 0; /* length of p_extra string */ |
| 612 | int lead_len; /* length of comment leader */ |
| 613 | char_u *lead_flags; /* position in 'comments' for comment leader */ |
| 614 | char_u *leader = NULL; /* copy of comment leader */ |
| 615 | #endif |
| 616 | char_u *allocated = NULL; /* allocated memory */ |
| 617 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \ |
| 618 | || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS) |
| 619 | char_u *p; |
| 620 | #endif |
| 621 | int saved_char = NUL; /* init for GCC */ |
| 622 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS) |
| 623 | pos_T *pos; |
| 624 | #endif |
| 625 | #ifdef FEAT_SMARTINDENT |
| 626 | int do_si = (!p_paste && curbuf->b_p_si |
| 627 | # ifdef FEAT_CINDENT |
| 628 | && !curbuf->b_p_cin |
| 629 | # endif |
| 630 | ); |
| 631 | int no_si = FALSE; /* reset did_si afterwards */ |
| 632 | int first_char = NUL; /* init for GCC */ |
| 633 | #endif |
| 634 | #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) |
| 635 | int vreplace_mode; |
| 636 | #endif |
| 637 | int did_append; /* appended a new line */ |
| 638 | int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */ |
| 639 | |
| 640 | /* |
| 641 | * make a copy of the current line so we can mess with it |
| 642 | */ |
| 643 | saved_line = vim_strsave(ml_get_curline()); |
| 644 | if (saved_line == NULL) /* out of memory! */ |
| 645 | return FALSE; |
| 646 | |
| 647 | #ifdef FEAT_VREPLACE |
| 648 | if (State & VREPLACE_FLAG) |
| 649 | { |
| 650 | /* |
| 651 | * With VREPLACE we make a copy of the next line, which we will be |
| 652 | * starting to replace. First make the new line empty and let vim play |
| 653 | * with the indenting and comment leader to its heart's content. Then |
| 654 | * we grab what it ended up putting on the new line, put back the |
| 655 | * original line, and call ins_char() to put each new character onto |
| 656 | * the line, replacing what was there before and pushing the right |
| 657 | * stuff onto the replace stack. -- webb. |
| 658 | */ |
| 659 | if (curwin->w_cursor.lnum < orig_line_count) |
| 660 | next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1)); |
| 661 | else |
| 662 | next_line = vim_strsave((char_u *)""); |
| 663 | if (next_line == NULL) /* out of memory! */ |
| 664 | goto theend; |
| 665 | |
| 666 | /* |
| 667 | * In VREPLACE mode, a NL replaces the rest of the line, and starts |
| 668 | * replacing the next line, so push all of the characters left on the |
| 669 | * line onto the replace stack. We'll push any other characters that |
| 670 | * might be replaced at the start of the next line (due to autoindent |
| 671 | * etc) a bit later. |
| 672 | */ |
| 673 | replace_push(NUL); /* Call twice because BS over NL expects it */ |
| 674 | replace_push(NUL); |
| 675 | p = saved_line + curwin->w_cursor.col; |
| 676 | while (*p != NUL) |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 677 | { |
| 678 | #ifdef FEAT_MBYTE |
| 679 | if (has_mbyte) |
| 680 | p += replace_push_mb(p); |
| 681 | else |
| 682 | #endif |
| 683 | replace_push(*p++); |
| 684 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 685 | saved_line[curwin->w_cursor.col] = NUL; |
| 686 | } |
| 687 | #endif |
| 688 | |
| 689 | if ((State & INSERT) |
| 690 | #ifdef FEAT_VREPLACE |
| 691 | && !(State & VREPLACE_FLAG) |
| 692 | #endif |
| 693 | ) |
| 694 | { |
| 695 | p_extra = saved_line + curwin->w_cursor.col; |
| 696 | #ifdef FEAT_SMARTINDENT |
| 697 | if (do_si) /* need first char after new line break */ |
| 698 | { |
| 699 | p = skipwhite(p_extra); |
| 700 | first_char = *p; |
| 701 | } |
| 702 | #endif |
| 703 | #ifdef FEAT_COMMENTS |
| 704 | extra_len = (int)STRLEN(p_extra); |
| 705 | #endif |
| 706 | saved_char = *p_extra; |
| 707 | *p_extra = NUL; |
| 708 | } |
| 709 | |
| 710 | u_clearline(); /* cannot do "U" command when adding lines */ |
| 711 | #ifdef FEAT_SMARTINDENT |
| 712 | did_si = FALSE; |
| 713 | #endif |
| 714 | ai_col = 0; |
| 715 | |
| 716 | /* |
| 717 | * If we just did an auto-indent, then we didn't type anything on |
| 718 | * the prior line, and it should be truncated. Do this even if 'ai' is not |
| 719 | * set because automatically inserting a comment leader also sets did_ai. |
| 720 | */ |
| 721 | if (dir == FORWARD && did_ai) |
| 722 | trunc_line = TRUE; |
| 723 | |
| 724 | /* |
| 725 | * If 'autoindent' and/or 'smartindent' is set, try to figure out what |
| 726 | * indent to use for the new line. |
| 727 | */ |
| 728 | if (curbuf->b_p_ai |
| 729 | #ifdef FEAT_SMARTINDENT |
| 730 | || do_si |
| 731 | #endif |
| 732 | ) |
| 733 | { |
| 734 | /* |
| 735 | * count white space on current line |
| 736 | */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 737 | newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 738 | if (newindent == 0 && !(flags & OPENLINE_COM_LIST)) |
| 739 | newindent = second_line_indent; /* for ^^D command in insert mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 740 | |
| 741 | #ifdef FEAT_SMARTINDENT |
| 742 | /* |
| 743 | * Do smart indenting. |
| 744 | * In insert/replace mode (only when dir == FORWARD) |
| 745 | * we may move some text to the next line. If it starts with '{' |
| 746 | * don't add an indent. Fixes inserting a NL before '{' in line |
| 747 | * "if (condition) {" |
| 748 | */ |
| 749 | if (!trunc_line && do_si && *saved_line != NUL |
| 750 | && (p_extra == NULL || first_char != '{')) |
| 751 | { |
| 752 | char_u *ptr; |
| 753 | char_u last_char; |
| 754 | |
| 755 | old_cursor = curwin->w_cursor; |
| 756 | ptr = saved_line; |
| 757 | # ifdef FEAT_COMMENTS |
| 758 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 759 | lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 760 | else |
| 761 | lead_len = 0; |
| 762 | # endif |
| 763 | if (dir == FORWARD) |
| 764 | { |
| 765 | /* |
| 766 | * Skip preprocessor directives, unless they are |
| 767 | * recognised as comments. |
| 768 | */ |
| 769 | if ( |
| 770 | # ifdef FEAT_COMMENTS |
| 771 | lead_len == 0 && |
| 772 | # endif |
| 773 | ptr[0] == '#') |
| 774 | { |
| 775 | while (ptr[0] == '#' && curwin->w_cursor.lnum > 1) |
| 776 | ptr = ml_get(--curwin->w_cursor.lnum); |
| 777 | newindent = get_indent(); |
| 778 | } |
| 779 | # ifdef FEAT_COMMENTS |
| 780 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 781 | lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 782 | else |
| 783 | lead_len = 0; |
| 784 | if (lead_len > 0) |
| 785 | { |
| 786 | /* |
| 787 | * This case gets the following right: |
| 788 | * \* |
| 789 | * * A comment (read '\' as '/'). |
| 790 | * *\ |
| 791 | * #define IN_THE_WAY |
| 792 | * This should line up here; |
| 793 | */ |
| 794 | p = skipwhite(ptr); |
| 795 | if (p[0] == '/' && p[1] == '*') |
| 796 | p++; |
| 797 | if (p[0] == '*') |
| 798 | { |
| 799 | for (p++; *p; p++) |
| 800 | { |
| 801 | if (p[0] == '/' && p[-1] == '*') |
| 802 | { |
| 803 | /* |
| 804 | * End of C comment, indent should line up |
| 805 | * with the line containing the start of |
| 806 | * the comment |
| 807 | */ |
| 808 | curwin->w_cursor.col = (colnr_T)(p - ptr); |
| 809 | if ((pos = findmatch(NULL, NUL)) != NULL) |
| 810 | { |
| 811 | curwin->w_cursor.lnum = pos->lnum; |
| 812 | newindent = get_indent(); |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | else /* Not a comment line */ |
| 819 | # endif |
| 820 | { |
| 821 | /* Find last non-blank in line */ |
| 822 | p = ptr + STRLEN(ptr) - 1; |
| 823 | while (p > ptr && vim_iswhite(*p)) |
| 824 | --p; |
| 825 | last_char = *p; |
| 826 | |
| 827 | /* |
| 828 | * find the character just before the '{' or ';' |
| 829 | */ |
| 830 | if (last_char == '{' || last_char == ';') |
| 831 | { |
| 832 | if (p > ptr) |
| 833 | --p; |
| 834 | while (p > ptr && vim_iswhite(*p)) |
| 835 | --p; |
| 836 | } |
| 837 | /* |
| 838 | * Try to catch lines that are split over multiple |
| 839 | * lines. eg: |
| 840 | * if (condition && |
| 841 | * condition) { |
| 842 | * Should line up here! |
| 843 | * } |
| 844 | */ |
| 845 | if (*p == ')') |
| 846 | { |
| 847 | curwin->w_cursor.col = (colnr_T)(p - ptr); |
| 848 | if ((pos = findmatch(NULL, '(')) != NULL) |
| 849 | { |
| 850 | curwin->w_cursor.lnum = pos->lnum; |
| 851 | newindent = get_indent(); |
| 852 | ptr = ml_get_curline(); |
| 853 | } |
| 854 | } |
| 855 | /* |
| 856 | * If last character is '{' do indent, without |
| 857 | * checking for "if" and the like. |
| 858 | */ |
| 859 | if (last_char == '{') |
| 860 | { |
| 861 | did_si = TRUE; /* do indent */ |
| 862 | no_si = TRUE; /* don't delete it when '{' typed */ |
| 863 | } |
| 864 | /* |
| 865 | * Look for "if" and the like, use 'cinwords'. |
| 866 | * Don't do this if the previous line ended in ';' or |
| 867 | * '}'. |
| 868 | */ |
| 869 | else if (last_char != ';' && last_char != '}' |
| 870 | && cin_is_cinword(ptr)) |
| 871 | did_si = TRUE; |
| 872 | } |
| 873 | } |
| 874 | else /* dir == BACKWARD */ |
| 875 | { |
| 876 | /* |
| 877 | * Skip preprocessor directives, unless they are |
| 878 | * recognised as comments. |
| 879 | */ |
| 880 | if ( |
| 881 | # ifdef FEAT_COMMENTS |
| 882 | lead_len == 0 && |
| 883 | # endif |
| 884 | ptr[0] == '#') |
| 885 | { |
| 886 | int was_backslashed = FALSE; |
| 887 | |
| 888 | while ((ptr[0] == '#' || was_backslashed) && |
| 889 | curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 890 | { |
| 891 | if (*ptr && ptr[STRLEN(ptr) - 1] == '\\') |
| 892 | was_backslashed = TRUE; |
| 893 | else |
| 894 | was_backslashed = FALSE; |
| 895 | ptr = ml_get(++curwin->w_cursor.lnum); |
| 896 | } |
| 897 | if (was_backslashed) |
| 898 | newindent = 0; /* Got to end of file */ |
| 899 | else |
| 900 | newindent = get_indent(); |
| 901 | } |
| 902 | p = skipwhite(ptr); |
| 903 | if (*p == '}') /* if line starts with '}': do indent */ |
| 904 | did_si = TRUE; |
| 905 | else /* can delete indent when '{' typed */ |
| 906 | can_si_back = TRUE; |
| 907 | } |
| 908 | curwin->w_cursor = old_cursor; |
| 909 | } |
| 910 | if (do_si) |
| 911 | can_si = TRUE; |
| 912 | #endif /* FEAT_SMARTINDENT */ |
| 913 | |
| 914 | did_ai = TRUE; |
| 915 | } |
| 916 | |
| 917 | #ifdef FEAT_COMMENTS |
| 918 | /* |
| 919 | * Find out if the current line starts with a comment leader. |
| 920 | * This may then be inserted in front of the new line. |
| 921 | */ |
| 922 | end_comment_pending = NUL; |
| 923 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 924 | lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 925 | else |
| 926 | lead_len = 0; |
| 927 | if (lead_len > 0) |
| 928 | { |
| 929 | char_u *lead_repl = NULL; /* replaces comment leader */ |
| 930 | int lead_repl_len = 0; /* length of *lead_repl */ |
| 931 | char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ |
| 932 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 933 | char_u *comment_end = NULL; /* where lead_end has been found */ |
| 934 | int extra_space = FALSE; /* append extra space */ |
| 935 | int current_flag; |
| 936 | int require_blank = FALSE; /* requires blank after middle */ |
| 937 | char_u *p2; |
| 938 | |
| 939 | /* |
| 940 | * If the comment leader has the start, middle or end flag, it may not |
| 941 | * be used or may be replaced with the middle leader. |
| 942 | */ |
| 943 | for (p = lead_flags; *p && *p != ':'; ++p) |
| 944 | { |
| 945 | if (*p == COM_BLANK) |
| 946 | { |
| 947 | require_blank = TRUE; |
| 948 | continue; |
| 949 | } |
| 950 | if (*p == COM_START || *p == COM_MIDDLE) |
| 951 | { |
| 952 | current_flag = *p; |
| 953 | if (*p == COM_START) |
| 954 | { |
| 955 | /* |
| 956 | * Doing "O" on a start of comment does not insert leader. |
| 957 | */ |
| 958 | if (dir == BACKWARD) |
| 959 | { |
| 960 | lead_len = 0; |
| 961 | break; |
| 962 | } |
| 963 | |
| 964 | /* find start of middle part */ |
| 965 | (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); |
| 966 | require_blank = FALSE; |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * Isolate the strings of the middle and end leader. |
| 971 | */ |
| 972 | while (*p && p[-1] != ':') /* find end of middle flags */ |
| 973 | { |
| 974 | if (*p == COM_BLANK) |
| 975 | require_blank = TRUE; |
| 976 | ++p; |
| 977 | } |
| 978 | (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); |
| 979 | |
| 980 | while (*p && p[-1] != ':') /* find end of end flags */ |
| 981 | { |
| 982 | /* Check whether we allow automatic ending of comments */ |
| 983 | if (*p == COM_AUTO_END) |
| 984 | end_comment_pending = -1; /* means we want to set it */ |
| 985 | ++p; |
| 986 | } |
| 987 | n = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 988 | |
| 989 | if (end_comment_pending == -1) /* we can set it now */ |
| 990 | end_comment_pending = lead_end[n - 1]; |
| 991 | |
| 992 | /* |
| 993 | * If the end of the comment is in the same line, don't use |
| 994 | * the comment leader. |
| 995 | */ |
| 996 | if (dir == FORWARD) |
| 997 | { |
| 998 | for (p = saved_line + lead_len; *p; ++p) |
| 999 | if (STRNCMP(p, lead_end, n) == 0) |
| 1000 | { |
| 1001 | comment_end = p; |
| 1002 | lead_len = 0; |
| 1003 | break; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * Doing "o" on a start of comment inserts the middle leader. |
| 1009 | */ |
| 1010 | if (lead_len > 0) |
| 1011 | { |
| 1012 | if (current_flag == COM_START) |
| 1013 | { |
| 1014 | lead_repl = lead_middle; |
| 1015 | lead_repl_len = (int)STRLEN(lead_middle); |
| 1016 | } |
| 1017 | |
| 1018 | /* |
| 1019 | * If we have hit RETURN immediately after the start |
| 1020 | * comment leader, then put a space after the middle |
| 1021 | * comment leader on the next line. |
| 1022 | */ |
| 1023 | if (!vim_iswhite(saved_line[lead_len - 1]) |
| 1024 | && ((p_extra != NULL |
| 1025 | && (int)curwin->w_cursor.col == lead_len) |
| 1026 | || (p_extra == NULL |
| 1027 | && saved_line[lead_len] == NUL) |
| 1028 | || require_blank)) |
| 1029 | extra_space = TRUE; |
| 1030 | } |
| 1031 | break; |
| 1032 | } |
| 1033 | if (*p == COM_END) |
| 1034 | { |
| 1035 | /* |
| 1036 | * Doing "o" on the end of a comment does not insert leader. |
| 1037 | * Remember where the end is, might want to use it to find the |
| 1038 | * start (for C-comments). |
| 1039 | */ |
| 1040 | if (dir == FORWARD) |
| 1041 | { |
| 1042 | comment_end = skipwhite(saved_line); |
| 1043 | lead_len = 0; |
| 1044 | break; |
| 1045 | } |
| 1046 | |
| 1047 | /* |
| 1048 | * Doing "O" on the end of a comment inserts the middle leader. |
| 1049 | * Find the string for the middle leader, searching backwards. |
| 1050 | */ |
| 1051 | while (p > curbuf->b_p_com && *p != ',') |
| 1052 | --p; |
| 1053 | for (lead_repl = p; lead_repl > curbuf->b_p_com |
| 1054 | && lead_repl[-1] != ':'; --lead_repl) |
| 1055 | ; |
| 1056 | lead_repl_len = (int)(p - lead_repl); |
| 1057 | |
| 1058 | /* We can probably always add an extra space when doing "O" on |
| 1059 | * the comment-end */ |
| 1060 | extra_space = TRUE; |
| 1061 | |
| 1062 | /* Check whether we allow automatic ending of comments */ |
| 1063 | for (p2 = p; *p2 && *p2 != ':'; p2++) |
| 1064 | { |
| 1065 | if (*p2 == COM_AUTO_END) |
| 1066 | end_comment_pending = -1; /* means we want to set it */ |
| 1067 | } |
| 1068 | if (end_comment_pending == -1) |
| 1069 | { |
| 1070 | /* Find last character in end-comment string */ |
| 1071 | while (*p2 && *p2 != ',') |
| 1072 | p2++; |
| 1073 | end_comment_pending = p2[-1]; |
| 1074 | } |
| 1075 | break; |
| 1076 | } |
| 1077 | if (*p == COM_FIRST) |
| 1078 | { |
| 1079 | /* |
| 1080 | * Comment leader for first line only: Don't repeat leader |
| 1081 | * when using "O", blank out leader when using "o". |
| 1082 | */ |
| 1083 | if (dir == BACKWARD) |
| 1084 | lead_len = 0; |
| 1085 | else |
| 1086 | { |
| 1087 | lead_repl = (char_u *)""; |
| 1088 | lead_repl_len = 0; |
| 1089 | } |
| 1090 | break; |
| 1091 | } |
| 1092 | } |
| 1093 | if (lead_len) |
| 1094 | { |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 1095 | /* allocate buffer (may concatenate p_extra later) */ |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1096 | leader = alloc(lead_len + lead_repl_len + extra_space + extra_len |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 1097 | + (second_line_indent > 0 ? second_line_indent : 0) + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1098 | allocated = leader; /* remember to free it later */ |
| 1099 | |
| 1100 | if (leader == NULL) |
| 1101 | lead_len = 0; |
| 1102 | else |
| 1103 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 1104 | vim_strncpy(leader, saved_line, lead_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1105 | |
| 1106 | /* |
| 1107 | * Replace leader with lead_repl, right or left adjusted |
| 1108 | */ |
| 1109 | if (lead_repl != NULL) |
| 1110 | { |
| 1111 | int c = 0; |
| 1112 | int off = 0; |
| 1113 | |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1114 | for (p = lead_flags; *p != NUL && *p != ':'; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1115 | { |
| 1116 | if (*p == COM_RIGHT || *p == COM_LEFT) |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1117 | c = *p++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1118 | else if (VIM_ISDIGIT(*p) || *p == '-') |
| 1119 | off = getdigits(&p); |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1120 | else |
| 1121 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1122 | } |
| 1123 | if (c == COM_RIGHT) /* right adjusted leader */ |
| 1124 | { |
| 1125 | /* find last non-white in the leader to line up with */ |
| 1126 | for (p = leader + lead_len - 1; p > leader |
| 1127 | && vim_iswhite(*p); --p) |
| 1128 | ; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1129 | ++p; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1130 | |
| 1131 | #ifdef FEAT_MBYTE |
| 1132 | /* Compute the length of the replaced characters in |
| 1133 | * screen characters, not bytes. */ |
| 1134 | { |
| 1135 | int repl_size = vim_strnsize(lead_repl, |
| 1136 | lead_repl_len); |
| 1137 | int old_size = 0; |
| 1138 | char_u *endp = p; |
| 1139 | int l; |
| 1140 | |
| 1141 | while (old_size < repl_size && p > leader) |
| 1142 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1143 | mb_ptr_back(leader, p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1144 | old_size += ptr2cells(p); |
| 1145 | } |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1146 | l = lead_repl_len - (int)(endp - p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1147 | if (l != 0) |
| 1148 | mch_memmove(endp + l, endp, |
| 1149 | (size_t)((leader + lead_len) - endp)); |
| 1150 | lead_len += l; |
| 1151 | } |
| 1152 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1153 | if (p < leader + lead_repl_len) |
| 1154 | p = leader; |
| 1155 | else |
| 1156 | p -= lead_repl_len; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1157 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1158 | mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
| 1159 | if (p + lead_repl_len > leader + lead_len) |
| 1160 | p[lead_repl_len] = NUL; |
| 1161 | |
| 1162 | /* blank-out any other chars from the old leader. */ |
| 1163 | while (--p >= leader) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1164 | { |
| 1165 | #ifdef FEAT_MBYTE |
| 1166 | int l = mb_head_off(leader, p); |
| 1167 | |
| 1168 | if (l > 1) |
| 1169 | { |
| 1170 | p -= l; |
| 1171 | if (ptr2cells(p) > 1) |
| 1172 | { |
| 1173 | p[1] = ' '; |
| 1174 | --l; |
| 1175 | } |
| 1176 | mch_memmove(p + 1, p + l + 1, |
| 1177 | (size_t)((leader + lead_len) - (p + l + 1))); |
| 1178 | lead_len -= l; |
| 1179 | *p = ' '; |
| 1180 | } |
| 1181 | else |
| 1182 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1183 | if (!vim_iswhite(*p)) |
| 1184 | *p = ' '; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1185 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1186 | } |
| 1187 | else /* left adjusted leader */ |
| 1188 | { |
| 1189 | p = skipwhite(leader); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1190 | #ifdef FEAT_MBYTE |
| 1191 | /* Compute the length of the replaced characters in |
| 1192 | * screen characters, not bytes. Move the part that is |
| 1193 | * not to be overwritten. */ |
| 1194 | { |
| 1195 | int repl_size = vim_strnsize(lead_repl, |
| 1196 | lead_repl_len); |
| 1197 | int i; |
| 1198 | int l; |
| 1199 | |
Bram Moolenaar | dc633cf | 2016-04-23 14:33:19 +0200 | [diff] [blame] | 1200 | for (i = 0; i < lead_len && p[i] != NUL; i += l) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1201 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1202 | l = (*mb_ptr2len)(p + i); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1203 | if (vim_strnsize(p, i + l) > repl_size) |
| 1204 | break; |
| 1205 | } |
| 1206 | if (i != lead_repl_len) |
| 1207 | { |
| 1208 | mch_memmove(p + lead_repl_len, p + i, |
Bram Moolenaar | 2d7ff05 | 2009-11-17 15:08:26 +0000 | [diff] [blame] | 1209 | (size_t)(lead_len - i - (p - leader))); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1210 | lead_len += lead_repl_len - i; |
| 1211 | } |
| 1212 | } |
| 1213 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1214 | mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
| 1215 | |
| 1216 | /* Replace any remaining non-white chars in the old |
| 1217 | * leader by spaces. Keep Tabs, the indent must |
| 1218 | * remain the same. */ |
| 1219 | for (p += lead_repl_len; p < leader + lead_len; ++p) |
| 1220 | if (!vim_iswhite(*p)) |
| 1221 | { |
| 1222 | /* Don't put a space before a TAB. */ |
| 1223 | if (p + 1 < leader + lead_len && p[1] == TAB) |
| 1224 | { |
| 1225 | --lead_len; |
| 1226 | mch_memmove(p, p + 1, |
| 1227 | (leader + lead_len) - p); |
| 1228 | } |
| 1229 | else |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1230 | { |
| 1231 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1232 | int l = (*mb_ptr2len)(p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1233 | |
| 1234 | if (l > 1) |
| 1235 | { |
| 1236 | if (ptr2cells(p) > 1) |
| 1237 | { |
| 1238 | /* Replace a double-wide char with |
| 1239 | * two spaces */ |
| 1240 | --l; |
| 1241 | *p++ = ' '; |
| 1242 | } |
| 1243 | mch_memmove(p + 1, p + l, |
| 1244 | (leader + lead_len) - p); |
| 1245 | lead_len -= l - 1; |
| 1246 | } |
| 1247 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1248 | *p = ' '; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1249 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1250 | } |
| 1251 | *p = NUL; |
| 1252 | } |
| 1253 | |
| 1254 | /* Recompute the indent, it may have changed. */ |
| 1255 | if (curbuf->b_p_ai |
| 1256 | #ifdef FEAT_SMARTINDENT |
| 1257 | || do_si |
| 1258 | #endif |
| 1259 | ) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 1260 | newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1261 | |
| 1262 | /* Add the indent offset */ |
| 1263 | if (newindent + off < 0) |
| 1264 | { |
| 1265 | off = -newindent; |
| 1266 | newindent = 0; |
| 1267 | } |
| 1268 | else |
| 1269 | newindent += off; |
| 1270 | |
| 1271 | /* Correct trailing spaces for the shift, so that |
| 1272 | * alignment remains equal. */ |
| 1273 | while (off > 0 && lead_len > 0 |
| 1274 | && leader[lead_len - 1] == ' ') |
| 1275 | { |
| 1276 | /* Don't do it when there is a tab before the space */ |
| 1277 | if (vim_strchr(skipwhite(leader), '\t') != NULL) |
| 1278 | break; |
| 1279 | --lead_len; |
| 1280 | --off; |
| 1281 | } |
| 1282 | |
| 1283 | /* If the leader ends in white space, don't add an |
| 1284 | * extra space */ |
| 1285 | if (lead_len > 0 && vim_iswhite(leader[lead_len - 1])) |
| 1286 | extra_space = FALSE; |
| 1287 | leader[lead_len] = NUL; |
| 1288 | } |
| 1289 | |
| 1290 | if (extra_space) |
| 1291 | { |
| 1292 | leader[lead_len++] = ' '; |
| 1293 | leader[lead_len] = NUL; |
| 1294 | } |
| 1295 | |
| 1296 | newcol = lead_len; |
| 1297 | |
| 1298 | /* |
| 1299 | * if a new indent will be set below, remove the indent that |
| 1300 | * is in the comment leader |
| 1301 | */ |
| 1302 | if (newindent |
| 1303 | #ifdef FEAT_SMARTINDENT |
| 1304 | || did_si |
| 1305 | #endif |
| 1306 | ) |
| 1307 | { |
| 1308 | while (lead_len && vim_iswhite(*leader)) |
| 1309 | { |
| 1310 | --lead_len; |
| 1311 | --newcol; |
| 1312 | ++leader; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | } |
| 1317 | #ifdef FEAT_SMARTINDENT |
| 1318 | did_si = can_si = FALSE; |
| 1319 | #endif |
| 1320 | } |
| 1321 | else if (comment_end != NULL) |
| 1322 | { |
| 1323 | /* |
| 1324 | * We have finished a comment, so we don't use the leader. |
| 1325 | * If this was a C-comment and 'ai' or 'si' is set do a normal |
| 1326 | * indent to align with the line containing the start of the |
| 1327 | * comment. |
| 1328 | */ |
| 1329 | if (comment_end[0] == '*' && comment_end[1] == '/' && |
| 1330 | (curbuf->b_p_ai |
| 1331 | #ifdef FEAT_SMARTINDENT |
| 1332 | || do_si |
| 1333 | #endif |
| 1334 | )) |
| 1335 | { |
| 1336 | old_cursor = curwin->w_cursor; |
| 1337 | curwin->w_cursor.col = (colnr_T)(comment_end - saved_line); |
| 1338 | if ((pos = findmatch(NULL, NUL)) != NULL) |
| 1339 | { |
| 1340 | curwin->w_cursor.lnum = pos->lnum; |
| 1341 | newindent = get_indent(); |
| 1342 | } |
| 1343 | curwin->w_cursor = old_cursor; |
| 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | #endif |
| 1348 | |
| 1349 | /* (State == INSERT || State == REPLACE), only when dir == FORWARD */ |
| 1350 | if (p_extra != NULL) |
| 1351 | { |
| 1352 | *p_extra = saved_char; /* restore char that NUL replaced */ |
| 1353 | |
| 1354 | /* |
| 1355 | * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first |
| 1356 | * non-blank. |
| 1357 | * |
| 1358 | * When in REPLACE mode, put the deleted blanks on the replace stack, |
| 1359 | * preceded by a NUL, so they can be put back when a BS is entered. |
| 1360 | */ |
| 1361 | if (REPLACE_NORMAL(State)) |
| 1362 | replace_push(NUL); /* end of extra blanks */ |
| 1363 | if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES)) |
| 1364 | { |
| 1365 | while ((*p_extra == ' ' || *p_extra == '\t') |
| 1366 | #ifdef FEAT_MBYTE |
| 1367 | && (!enc_utf8 |
| 1368 | || !utf_iscomposing(utf_ptr2char(p_extra + 1))) |
| 1369 | #endif |
| 1370 | ) |
| 1371 | { |
| 1372 | if (REPLACE_NORMAL(State)) |
| 1373 | replace_push(*p_extra); |
| 1374 | ++p_extra; |
| 1375 | ++less_cols_off; |
| 1376 | } |
| 1377 | } |
| 1378 | if (*p_extra != NUL) |
| 1379 | did_ai = FALSE; /* append some text, don't truncate now */ |
| 1380 | |
| 1381 | /* columns for marks adjusted for removed columns */ |
| 1382 | less_cols = (int)(p_extra - saved_line); |
| 1383 | } |
| 1384 | |
| 1385 | if (p_extra == NULL) |
| 1386 | p_extra = (char_u *)""; /* append empty line */ |
| 1387 | |
| 1388 | #ifdef FEAT_COMMENTS |
| 1389 | /* concatenate leader and p_extra, if there is a leader */ |
| 1390 | if (lead_len) |
| 1391 | { |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1392 | if (flags & OPENLINE_COM_LIST && second_line_indent > 0) |
| 1393 | { |
| 1394 | int i; |
Bram Moolenaar | 3610578 | 2012-06-14 20:59:25 +0200 | [diff] [blame] | 1395 | int padding = second_line_indent |
| 1396 | - (newindent + (int)STRLEN(leader)); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1397 | |
| 1398 | /* Here whitespace is inserted after the comment char. |
| 1399 | * Below, set_indent(newindent, SIN_INSERT) will insert the |
| 1400 | * whitespace needed before the comment char. */ |
| 1401 | for (i = 0; i < padding; i++) |
| 1402 | { |
| 1403 | STRCAT(leader, " "); |
Bram Moolenaar | 5fb9ec5 | 2012-07-25 16:10:03 +0200 | [diff] [blame] | 1404 | less_cols--; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1405 | newcol++; |
| 1406 | } |
| 1407 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1408 | STRCAT(leader, p_extra); |
| 1409 | p_extra = leader; |
| 1410 | did_ai = TRUE; /* So truncating blanks works with comments */ |
| 1411 | less_cols -= lead_len; |
| 1412 | } |
| 1413 | else |
| 1414 | end_comment_pending = NUL; /* turns out there was no leader */ |
| 1415 | #endif |
| 1416 | |
| 1417 | old_cursor = curwin->w_cursor; |
| 1418 | if (dir == BACKWARD) |
| 1419 | --curwin->w_cursor.lnum; |
| 1420 | #ifdef FEAT_VREPLACE |
| 1421 | if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count) |
| 1422 | #endif |
| 1423 | { |
| 1424 | if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE) |
| 1425 | == FAIL) |
| 1426 | goto theend; |
| 1427 | /* Postpone calling changed_lines(), because it would mess up folding |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 1428 | * with markers. |
| 1429 | * 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] | 1430 | * be marks there. But still needed in diff mode. */ |
| 1431 | if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count |
| 1432 | #ifdef FEAT_DIFF |
| 1433 | || curwin->w_p_diff |
| 1434 | #endif |
| 1435 | ) |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 1436 | mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1437 | did_append = TRUE; |
| 1438 | } |
| 1439 | #ifdef FEAT_VREPLACE |
| 1440 | else |
| 1441 | { |
| 1442 | /* |
| 1443 | * In VREPLACE mode we are starting to replace the next line. |
| 1444 | */ |
| 1445 | curwin->w_cursor.lnum++; |
| 1446 | if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed) |
| 1447 | { |
| 1448 | /* In case we NL to a new line, BS to the previous one, and NL |
| 1449 | * again, we don't want to save the new line for undo twice. |
| 1450 | */ |
| 1451 | (void)u_save_cursor(); /* errors are ignored! */ |
| 1452 | vr_lines_changed++; |
| 1453 | } |
| 1454 | ml_replace(curwin->w_cursor.lnum, p_extra, TRUE); |
| 1455 | changed_bytes(curwin->w_cursor.lnum, 0); |
| 1456 | curwin->w_cursor.lnum--; |
| 1457 | did_append = FALSE; |
| 1458 | } |
| 1459 | #endif |
| 1460 | |
| 1461 | if (newindent |
| 1462 | #ifdef FEAT_SMARTINDENT |
| 1463 | || did_si |
| 1464 | #endif |
| 1465 | ) |
| 1466 | { |
| 1467 | ++curwin->w_cursor.lnum; |
| 1468 | #ifdef FEAT_SMARTINDENT |
| 1469 | if (did_si) |
| 1470 | { |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 1471 | int sw = (int)get_sw_value(curbuf); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 1472 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1473 | if (p_sr) |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 1474 | newindent -= newindent % sw; |
| 1475 | newindent += sw; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1476 | } |
| 1477 | #endif |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 1478 | /* Copy the indent */ |
| 1479 | if (curbuf->b_p_ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1480 | { |
| 1481 | (void)copy_indent(newindent, saved_line); |
| 1482 | |
| 1483 | /* |
| 1484 | * Set the 'preserveindent' option so that any further screwing |
| 1485 | * with the line doesn't entirely destroy our efforts to preserve |
| 1486 | * it. It gets restored at the function end. |
| 1487 | */ |
| 1488 | curbuf->b_p_pi = TRUE; |
| 1489 | } |
| 1490 | else |
| 1491 | (void)set_indent(newindent, SIN_INSERT); |
| 1492 | less_cols -= curwin->w_cursor.col; |
| 1493 | |
| 1494 | ai_col = curwin->w_cursor.col; |
| 1495 | |
| 1496 | /* |
| 1497 | * In REPLACE mode, for each character in the new indent, there must |
| 1498 | * be a NUL on the replace stack, for when it is deleted with BS |
| 1499 | */ |
| 1500 | if (REPLACE_NORMAL(State)) |
| 1501 | for (n = 0; n < (int)curwin->w_cursor.col; ++n) |
| 1502 | replace_push(NUL); |
| 1503 | newcol += curwin->w_cursor.col; |
| 1504 | #ifdef FEAT_SMARTINDENT |
| 1505 | if (no_si) |
| 1506 | did_si = FALSE; |
| 1507 | #endif |
| 1508 | } |
| 1509 | |
| 1510 | #ifdef FEAT_COMMENTS |
| 1511 | /* |
| 1512 | * In REPLACE mode, for each character in the extra leader, there must be |
| 1513 | * a NUL on the replace stack, for when it is deleted with BS. |
| 1514 | */ |
| 1515 | if (REPLACE_NORMAL(State)) |
| 1516 | while (lead_len-- > 0) |
| 1517 | replace_push(NUL); |
| 1518 | #endif |
| 1519 | |
| 1520 | curwin->w_cursor = old_cursor; |
| 1521 | |
| 1522 | if (dir == FORWARD) |
| 1523 | { |
| 1524 | if (trunc_line || (State & INSERT)) |
| 1525 | { |
| 1526 | /* truncate current line at cursor */ |
| 1527 | saved_line[curwin->w_cursor.col] = NUL; |
| 1528 | /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */ |
| 1529 | if (trunc_line && !(flags & OPENLINE_KEEPTRAIL)) |
| 1530 | truncate_spaces(saved_line); |
| 1531 | ml_replace(curwin->w_cursor.lnum, saved_line, FALSE); |
| 1532 | saved_line = NULL; |
| 1533 | if (did_append) |
| 1534 | { |
| 1535 | changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, |
| 1536 | curwin->w_cursor.lnum + 1, 1L); |
| 1537 | did_append = FALSE; |
| 1538 | |
| 1539 | /* Move marks after the line break to the new line. */ |
| 1540 | if (flags & OPENLINE_MARKFIX) |
| 1541 | mark_col_adjust(curwin->w_cursor.lnum, |
| 1542 | curwin->w_cursor.col + less_cols_off, |
| 1543 | 1L, (long)-less_cols); |
| 1544 | } |
| 1545 | else |
| 1546 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | * Put the cursor on the new line. Careful: the scrollup() above may |
| 1551 | * have moved w_cursor, we must use old_cursor. |
| 1552 | */ |
| 1553 | curwin->w_cursor.lnum = old_cursor.lnum + 1; |
| 1554 | } |
| 1555 | if (did_append) |
| 1556 | changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L); |
| 1557 | |
| 1558 | curwin->w_cursor.col = newcol; |
| 1559 | #ifdef FEAT_VIRTUALEDIT |
| 1560 | curwin->w_cursor.coladd = 0; |
| 1561 | #endif |
| 1562 | |
| 1563 | #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) |
| 1564 | /* |
| 1565 | * In VREPLACE mode, we are handling the replace stack ourselves, so stop |
| 1566 | * fixthisline() from doing it (via change_indent()) by telling it we're in |
| 1567 | * normal INSERT mode. |
| 1568 | */ |
| 1569 | if (State & VREPLACE_FLAG) |
| 1570 | { |
| 1571 | vreplace_mode = State; /* So we know to put things right later */ |
| 1572 | State = INSERT; |
| 1573 | } |
| 1574 | else |
| 1575 | vreplace_mode = 0; |
| 1576 | #endif |
| 1577 | #ifdef FEAT_LISP |
| 1578 | /* |
| 1579 | * May do lisp indenting. |
| 1580 | */ |
| 1581 | if (!p_paste |
| 1582 | # ifdef FEAT_COMMENTS |
| 1583 | && leader == NULL |
| 1584 | # endif |
| 1585 | && curbuf->b_p_lisp |
| 1586 | && curbuf->b_p_ai) |
| 1587 | { |
| 1588 | fixthisline(get_lisp_indent); |
| 1589 | p = ml_get_curline(); |
| 1590 | ai_col = (colnr_T)(skipwhite(p) - p); |
| 1591 | } |
| 1592 | #endif |
| 1593 | #ifdef FEAT_CINDENT |
| 1594 | /* |
| 1595 | * May do indenting after opening a new line. |
| 1596 | */ |
| 1597 | if (!p_paste |
| 1598 | && (curbuf->b_p_cin |
| 1599 | # ifdef FEAT_EVAL |
| 1600 | || *curbuf->b_p_inde != NUL |
| 1601 | # endif |
| 1602 | ) |
| 1603 | && in_cinkeys(dir == FORWARD |
| 1604 | ? KEY_OPEN_FORW |
| 1605 | : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))) |
| 1606 | { |
| 1607 | do_c_expr_indent(); |
| 1608 | p = ml_get_curline(); |
| 1609 | ai_col = (colnr_T)(skipwhite(p) - p); |
| 1610 | } |
| 1611 | #endif |
| 1612 | #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) |
| 1613 | if (vreplace_mode != 0) |
| 1614 | State = vreplace_mode; |
| 1615 | #endif |
| 1616 | |
| 1617 | #ifdef FEAT_VREPLACE |
| 1618 | /* |
| 1619 | * Finally, VREPLACE gets the stuff on the new line, then puts back the |
| 1620 | * original line, and inserts the new stuff char by char, pushing old stuff |
| 1621 | * onto the replace stack (via ins_char()). |
| 1622 | */ |
| 1623 | if (State & VREPLACE_FLAG) |
| 1624 | { |
| 1625 | /* Put new line in p_extra */ |
| 1626 | p_extra = vim_strsave(ml_get_curline()); |
| 1627 | if (p_extra == NULL) |
| 1628 | goto theend; |
| 1629 | |
| 1630 | /* Put back original line */ |
| 1631 | ml_replace(curwin->w_cursor.lnum, next_line, FALSE); |
| 1632 | |
| 1633 | /* Insert new stuff into line again */ |
| 1634 | curwin->w_cursor.col = 0; |
| 1635 | #ifdef FEAT_VIRTUALEDIT |
| 1636 | curwin->w_cursor.coladd = 0; |
| 1637 | #endif |
| 1638 | ins_bytes(p_extra); /* will call changed_bytes() */ |
| 1639 | vim_free(p_extra); |
| 1640 | next_line = NULL; |
| 1641 | } |
| 1642 | #endif |
| 1643 | |
| 1644 | retval = TRUE; /* success! */ |
| 1645 | theend: |
| 1646 | curbuf->b_p_pi = saved_pi; |
| 1647 | vim_free(saved_line); |
| 1648 | vim_free(next_line); |
| 1649 | vim_free(allocated); |
| 1650 | return retval; |
| 1651 | } |
| 1652 | |
| 1653 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 1654 | /* |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 1655 | * get_leader_len() returns the length in bytes of the prefix of the given |
| 1656 | * string which introduces a comment. If this string is not a comment then |
| 1657 | * 0 is returned. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1658 | * When "flags" is not NULL, it is set to point to the flags of the recognized |
| 1659 | * comment leader. |
| 1660 | * "backward" must be true for the "O" command. |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1661 | * If "include_space" is set, include trailing whitespace while calculating the |
| 1662 | * length. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1663 | */ |
| 1664 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1665 | get_leader_len( |
| 1666 | char_u *line, |
| 1667 | char_u **flags, |
| 1668 | int backward, |
| 1669 | int include_space) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1670 | { |
| 1671 | int i, j; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1672 | int result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1673 | int got_com = FALSE; |
| 1674 | int found_one; |
| 1675 | char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ |
| 1676 | char_u *string; /* pointer to comment string */ |
| 1677 | char_u *list; |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1678 | int middle_match_len = 0; |
| 1679 | char_u *prev_list; |
Bram Moolenaar | 05da428 | 2011-05-10 14:44:11 +0200 | [diff] [blame] | 1680 | char_u *saved_flags = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1681 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1682 | result = i = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1683 | while (vim_iswhite(line[i])) /* leading white space is ignored */ |
| 1684 | ++i; |
| 1685 | |
| 1686 | /* |
| 1687 | * Repeat to match several nested comment strings. |
| 1688 | */ |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1689 | while (line[i] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1690 | { |
| 1691 | /* |
| 1692 | * scan through the 'comments' option for a match |
| 1693 | */ |
| 1694 | found_one = FALSE; |
| 1695 | for (list = curbuf->b_p_com; *list; ) |
| 1696 | { |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1697 | /* Get one option part into part_buf[]. Advance "list" to next |
| 1698 | * one. Put "string" at start of string. */ |
| 1699 | if (!got_com && flags != NULL) |
| 1700 | *flags = list; /* remember where flags started */ |
| 1701 | prev_list = list; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1702 | (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
| 1703 | string = vim_strchr(part_buf, ':'); |
| 1704 | if (string == NULL) /* missing ':', ignore this part */ |
| 1705 | continue; |
| 1706 | *string++ = NUL; /* isolate flags from string */ |
| 1707 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1708 | /* If we found a middle match previously, use that match when this |
| 1709 | * is not a middle or end. */ |
| 1710 | if (middle_match_len != 0 |
| 1711 | && vim_strchr(part_buf, COM_MIDDLE) == NULL |
| 1712 | && vim_strchr(part_buf, COM_END) == NULL) |
| 1713 | break; |
| 1714 | |
| 1715 | /* When we already found a nested comment, only accept further |
| 1716 | * nested comments. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1717 | if (got_com && vim_strchr(part_buf, COM_NEST) == NULL) |
| 1718 | continue; |
| 1719 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1720 | /* When 'O' flag present and using "O" command skip this one. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1721 | if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL) |
| 1722 | continue; |
| 1723 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1724 | /* Line contents and string must match. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1725 | * When string starts with white space, must have some white space |
| 1726 | * (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] | 1727 | * TABs and spaces). */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1728 | if (vim_iswhite(string[0])) |
| 1729 | { |
| 1730 | if (i == 0 || !vim_iswhite(line[i - 1])) |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 1731 | continue; /* missing white space */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1732 | while (vim_iswhite(string[0])) |
| 1733 | ++string; |
| 1734 | } |
| 1735 | for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) |
| 1736 | ; |
| 1737 | if (string[j] != NUL) |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1738 | continue; /* string doesn't match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1739 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1740 | /* When 'b' flag used, there must be white space or an |
| 1741 | * end-of-line after the string in the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1742 | if (vim_strchr(part_buf, COM_BLANK) != NULL |
| 1743 | && !vim_iswhite(line[i + j]) && line[i + j] != NUL) |
| 1744 | continue; |
| 1745 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1746 | /* We have found a match, stop searching unless this is a middle |
| 1747 | * comment. The middle comment can be a substring of the end |
| 1748 | * comment in which case it's better to return the length of the |
| 1749 | * end comment and its flags. Thus we keep searching with middle |
| 1750 | * and end matches and use an end match if it matches better. */ |
| 1751 | if (vim_strchr(part_buf, COM_MIDDLE) != NULL) |
| 1752 | { |
| 1753 | if (middle_match_len == 0) |
| 1754 | { |
| 1755 | middle_match_len = j; |
| 1756 | saved_flags = prev_list; |
| 1757 | } |
| 1758 | continue; |
| 1759 | } |
| 1760 | if (middle_match_len != 0 && j > middle_match_len) |
| 1761 | /* Use this match instead of the middle match, since it's a |
| 1762 | * longer thus better match. */ |
| 1763 | middle_match_len = 0; |
| 1764 | |
| 1765 | if (middle_match_len == 0) |
| 1766 | i += j; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1767 | found_one = TRUE; |
| 1768 | break; |
| 1769 | } |
| 1770 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1771 | if (middle_match_len != 0) |
| 1772 | { |
| 1773 | /* Use the previously found middle match after failing to find a |
| 1774 | * match with an end. */ |
| 1775 | if (!got_com && flags != NULL) |
| 1776 | *flags = saved_flags; |
| 1777 | i += middle_match_len; |
| 1778 | found_one = TRUE; |
| 1779 | } |
| 1780 | |
| 1781 | /* No match found, stop scanning. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1782 | if (!found_one) |
| 1783 | break; |
| 1784 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1785 | result = i; |
| 1786 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1787 | /* Include any trailing white space. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1788 | while (vim_iswhite(line[i])) |
| 1789 | ++i; |
| 1790 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1791 | if (include_space) |
| 1792 | result = i; |
| 1793 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1794 | /* If this comment doesn't nest, stop here. */ |
| 1795 | got_com = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1796 | if (vim_strchr(part_buf, COM_NEST) == NULL) |
| 1797 | break; |
| 1798 | } |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1799 | return result; |
| 1800 | } |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1801 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1802 | /* |
| 1803 | * Return the offset at which the last comment in line starts. If there is no |
| 1804 | * comment in the whole line, -1 is returned. |
| 1805 | * |
| 1806 | * When "flags" is not null, it is set to point to the flags describing the |
| 1807 | * recognized comment leader. |
| 1808 | */ |
| 1809 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1810 | get_last_leader_offset(char_u *line, char_u **flags) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1811 | { |
| 1812 | int result = -1; |
| 1813 | int i, j; |
| 1814 | int lower_check_bound = 0; |
| 1815 | char_u *string; |
| 1816 | char_u *com_leader; |
| 1817 | char_u *com_flags; |
| 1818 | char_u *list; |
| 1819 | int found_one; |
| 1820 | char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ |
| 1821 | |
| 1822 | /* |
| 1823 | * Repeat to match several nested comment strings. |
| 1824 | */ |
| 1825 | i = (int)STRLEN(line); |
| 1826 | while (--i >= lower_check_bound) |
| 1827 | { |
| 1828 | /* |
| 1829 | * scan through the 'comments' option for a match |
| 1830 | */ |
| 1831 | found_one = FALSE; |
| 1832 | for (list = curbuf->b_p_com; *list; ) |
| 1833 | { |
| 1834 | char_u *flags_save = list; |
| 1835 | |
| 1836 | /* |
| 1837 | * Get one option part into part_buf[]. Advance list to next one. |
| 1838 | * put string at start of string. |
| 1839 | */ |
| 1840 | (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
| 1841 | string = vim_strchr(part_buf, ':'); |
| 1842 | if (string == NULL) /* If everything is fine, this cannot actually |
| 1843 | * happen. */ |
| 1844 | { |
| 1845 | continue; |
| 1846 | } |
| 1847 | *string++ = NUL; /* Isolate flags from string. */ |
| 1848 | com_leader = string; |
| 1849 | |
| 1850 | /* |
| 1851 | * Line contents and string must match. |
| 1852 | * When string starts with white space, must have some white space |
| 1853 | * (but the amount does not need to match, there might be a mix of |
| 1854 | * TABs and spaces). |
| 1855 | */ |
| 1856 | if (vim_iswhite(string[0])) |
| 1857 | { |
| 1858 | if (i == 0 || !vim_iswhite(line[i - 1])) |
| 1859 | continue; |
| 1860 | while (vim_iswhite(string[0])) |
| 1861 | ++string; |
| 1862 | } |
| 1863 | for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) |
| 1864 | /* do nothing */; |
| 1865 | if (string[j] != NUL) |
| 1866 | continue; |
| 1867 | |
| 1868 | /* |
| 1869 | * When 'b' flag used, there must be white space or an |
| 1870 | * end-of-line after the string in the line. |
| 1871 | */ |
| 1872 | if (vim_strchr(part_buf, COM_BLANK) != NULL |
| 1873 | && !vim_iswhite(line[i + j]) && line[i + j] != NUL) |
| 1874 | { |
| 1875 | continue; |
| 1876 | } |
| 1877 | |
| 1878 | /* |
| 1879 | * We have found a match, stop searching. |
| 1880 | */ |
| 1881 | found_one = TRUE; |
| 1882 | |
| 1883 | if (flags) |
| 1884 | *flags = flags_save; |
| 1885 | com_flags = flags_save; |
| 1886 | |
| 1887 | break; |
| 1888 | } |
| 1889 | |
| 1890 | if (found_one) |
| 1891 | { |
| 1892 | char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */ |
| 1893 | int len1, len2, off; |
| 1894 | |
| 1895 | result = i; |
| 1896 | /* |
| 1897 | * If this comment nests, continue searching. |
| 1898 | */ |
| 1899 | if (vim_strchr(part_buf, COM_NEST) != NULL) |
| 1900 | continue; |
| 1901 | |
| 1902 | lower_check_bound = i; |
| 1903 | |
| 1904 | /* Let's verify whether the comment leader found is a substring |
| 1905 | * of other comment leaders. If it is, let's adjust the |
| 1906 | * lower_check_bound so that we make sure that we have determined |
| 1907 | * the comment leader correctly. |
| 1908 | */ |
| 1909 | |
| 1910 | while (vim_iswhite(*com_leader)) |
| 1911 | ++com_leader; |
| 1912 | len1 = (int)STRLEN(com_leader); |
| 1913 | |
| 1914 | for (list = curbuf->b_p_com; *list; ) |
| 1915 | { |
| 1916 | char_u *flags_save = list; |
| 1917 | |
| 1918 | (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ","); |
| 1919 | if (flags_save == com_flags) |
| 1920 | continue; |
| 1921 | string = vim_strchr(part_buf2, ':'); |
| 1922 | ++string; |
| 1923 | while (vim_iswhite(*string)) |
| 1924 | ++string; |
| 1925 | len2 = (int)STRLEN(string); |
| 1926 | if (len2 == 0) |
| 1927 | continue; |
| 1928 | |
| 1929 | /* Now we have to verify whether string ends with a substring |
| 1930 | * beginning the com_leader. */ |
| 1931 | for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;) |
| 1932 | { |
| 1933 | --off; |
| 1934 | if (!STRNCMP(string + off, com_leader, len2 - off)) |
| 1935 | { |
| 1936 | if (i - off < lower_check_bound) |
| 1937 | lower_check_bound = i - off; |
| 1938 | } |
| 1939 | } |
| 1940 | } |
| 1941 | } |
| 1942 | } |
| 1943 | return result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1944 | } |
| 1945 | #endif |
| 1946 | |
| 1947 | /* |
| 1948 | * Return the number of window lines occupied by buffer line "lnum". |
| 1949 | */ |
| 1950 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1951 | plines(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1952 | { |
| 1953 | return plines_win(curwin, lnum, TRUE); |
| 1954 | } |
| 1955 | |
| 1956 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1957 | plines_win( |
| 1958 | win_T *wp, |
| 1959 | linenr_T lnum, |
| 1960 | int winheight) /* when TRUE limit to window height */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1961 | { |
| 1962 | #if defined(FEAT_DIFF) || defined(PROTO) |
| 1963 | /* Check for filler lines above this buffer line. When folded the result |
| 1964 | * is one line anyway. */ |
| 1965 | return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum); |
| 1966 | } |
| 1967 | |
| 1968 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1969 | plines_nofill(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1970 | { |
| 1971 | return plines_win_nofill(curwin, lnum, TRUE); |
| 1972 | } |
| 1973 | |
| 1974 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1975 | plines_win_nofill( |
| 1976 | win_T *wp, |
| 1977 | linenr_T lnum, |
| 1978 | int winheight) /* when TRUE limit to window height */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1979 | { |
| 1980 | #endif |
| 1981 | int lines; |
| 1982 | |
| 1983 | if (!wp->w_p_wrap) |
| 1984 | return 1; |
| 1985 | |
Bram Moolenaar | 44a2f92 | 2016-03-19 22:11:51 +0100 | [diff] [blame] | 1986 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1987 | if (wp->w_width == 0) |
| 1988 | return 1; |
| 1989 | #endif |
| 1990 | |
| 1991 | #ifdef FEAT_FOLDING |
| 1992 | /* A folded lines is handled just like an empty line. */ |
| 1993 | /* NOTE: Caller must handle lines that are MAYBE folded. */ |
| 1994 | if (lineFolded(wp, lnum) == TRUE) |
| 1995 | return 1; |
| 1996 | #endif |
| 1997 | |
| 1998 | lines = plines_win_nofold(wp, lnum); |
| 1999 | if (winheight > 0 && lines > wp->w_height) |
| 2000 | return (int)wp->w_height; |
| 2001 | return lines; |
| 2002 | } |
| 2003 | |
| 2004 | /* |
| 2005 | * Return number of window lines physical line "lnum" will occupy in window |
| 2006 | * "wp". Does not care about folding, 'wrap' or 'diff'. |
| 2007 | */ |
| 2008 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2009 | plines_win_nofold(win_T *wp, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2010 | { |
| 2011 | char_u *s; |
| 2012 | long col; |
| 2013 | int width; |
| 2014 | |
| 2015 | s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
| 2016 | if (*s == NUL) /* empty line */ |
| 2017 | return 1; |
| 2018 | col = win_linetabsize(wp, s, (colnr_T)MAXCOL); |
| 2019 | |
| 2020 | /* |
| 2021 | * If list mode is on, then the '$' at the end of the line may take up one |
| 2022 | * extra column. |
| 2023 | */ |
| 2024 | if (wp->w_p_list && lcs_eol != NUL) |
| 2025 | col += 1; |
| 2026 | |
| 2027 | /* |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 2028 | * Add column offset for 'number', 'relativenumber' and 'foldcolumn'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2029 | */ |
| 2030 | width = W_WIDTH(wp) - win_col_off(wp); |
| 2031 | if (width <= 0) |
| 2032 | return 32000; |
| 2033 | if (col <= width) |
| 2034 | return 1; |
| 2035 | col -= width; |
| 2036 | width += win_col_off2(wp); |
| 2037 | return (col + (width - 1)) / width + 1; |
| 2038 | } |
| 2039 | |
| 2040 | /* |
| 2041 | * Like plines_win(), but only reports the number of physical screen lines |
| 2042 | * used from the start of the line to the given column number. |
| 2043 | */ |
| 2044 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2045 | plines_win_col(win_T *wp, linenr_T lnum, long column) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2046 | { |
| 2047 | long col; |
| 2048 | char_u *s; |
| 2049 | int lines = 0; |
| 2050 | int width; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2051 | char_u *line; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2052 | |
| 2053 | #ifdef FEAT_DIFF |
| 2054 | /* Check for filler lines above this buffer line. When folded the result |
| 2055 | * is one line anyway. */ |
| 2056 | lines = diff_check_fill(wp, lnum); |
| 2057 | #endif |
| 2058 | |
| 2059 | if (!wp->w_p_wrap) |
| 2060 | return lines + 1; |
| 2061 | |
Bram Moolenaar | 44a2f92 | 2016-03-19 22:11:51 +0100 | [diff] [blame] | 2062 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2063 | if (wp->w_width == 0) |
| 2064 | return lines + 1; |
| 2065 | #endif |
| 2066 | |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2067 | line = s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2068 | |
| 2069 | col = 0; |
| 2070 | while (*s != NUL && --column >= 0) |
| 2071 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2072 | col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 2073 | mb_ptr_adv(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | /* |
| 2077 | * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in |
| 2078 | * INSERT mode, then col must be adjusted so that it represents the last |
| 2079 | * screen position of the TAB. This only fixes an error when the TAB wraps |
| 2080 | * from one screen line to the next (when 'columns' is not a multiple of |
| 2081 | * 'ts') -- webb. |
| 2082 | */ |
| 2083 | if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1)) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2084 | col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2085 | |
| 2086 | /* |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 2087 | * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2088 | */ |
| 2089 | width = W_WIDTH(wp) - win_col_off(wp); |
Bram Moolenaar | 2647063 | 2006-10-24 19:12:40 +0000 | [diff] [blame] | 2090 | if (width <= 0) |
| 2091 | return 9999; |
| 2092 | |
| 2093 | lines += 1; |
| 2094 | if (col > width) |
| 2095 | lines += (col - width) / (width + win_col_off2(wp)) + 1; |
| 2096 | return lines; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2100 | plines_m_win(win_T *wp, linenr_T first, linenr_T last) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2101 | { |
| 2102 | int count = 0; |
| 2103 | |
| 2104 | while (first <= last) |
| 2105 | { |
| 2106 | #ifdef FEAT_FOLDING |
| 2107 | int x; |
| 2108 | |
| 2109 | /* Check if there are any really folded lines, but also included lines |
| 2110 | * that are maybe folded. */ |
| 2111 | x = foldedCount(wp, first, NULL); |
| 2112 | if (x > 0) |
| 2113 | { |
| 2114 | ++count; /* count 1 for "+-- folded" line */ |
| 2115 | first += x; |
| 2116 | } |
| 2117 | else |
| 2118 | #endif |
| 2119 | { |
| 2120 | #ifdef FEAT_DIFF |
| 2121 | if (first == wp->w_topline) |
| 2122 | count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill; |
| 2123 | else |
| 2124 | #endif |
| 2125 | count += plines_win(wp, first, TRUE); |
| 2126 | ++first; |
| 2127 | } |
| 2128 | } |
| 2129 | return (count); |
| 2130 | } |
| 2131 | |
| 2132 | #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO) |
| 2133 | /* |
| 2134 | * Insert string "p" at the cursor position. Stops at a NUL byte. |
| 2135 | * Handles Replace mode and multi-byte characters. |
| 2136 | */ |
| 2137 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2138 | ins_bytes(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2139 | { |
| 2140 | ins_bytes_len(p, (int)STRLEN(p)); |
| 2141 | } |
| 2142 | #endif |
| 2143 | |
| 2144 | #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ |
| 2145 | || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO) |
| 2146 | /* |
| 2147 | * Insert string "p" with length "len" at the cursor position. |
| 2148 | * Handles Replace mode and multi-byte characters. |
| 2149 | */ |
| 2150 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2151 | ins_bytes_len(char_u *p, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2152 | { |
| 2153 | int i; |
| 2154 | # ifdef FEAT_MBYTE |
| 2155 | int n; |
| 2156 | |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2157 | if (has_mbyte) |
| 2158 | for (i = 0; i < len; i += n) |
| 2159 | { |
| 2160 | if (enc_utf8) |
| 2161 | /* avoid reading past p[len] */ |
| 2162 | n = utfc_ptr2len_len(p + i, len - i); |
| 2163 | else |
| 2164 | n = (*mb_ptr2len)(p + i); |
| 2165 | ins_char_bytes(p + i, n); |
| 2166 | } |
| 2167 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2168 | # endif |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2169 | for (i = 0; i < len; ++i) |
| 2170 | ins_char(p[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2171 | } |
| 2172 | #endif |
| 2173 | |
| 2174 | /* |
| 2175 | * Insert or replace a single character at the cursor position. |
| 2176 | * When in REPLACE or VREPLACE mode, replace any existing character. |
| 2177 | * Caller must have prepared for undo. |
| 2178 | * For multi-byte characters we get the whole character, the caller must |
| 2179 | * convert bytes to a character. |
| 2180 | */ |
| 2181 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2182 | ins_char(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2183 | { |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 2184 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | 560379d | 2017-01-21 22:50:00 +0100 | [diff] [blame] | 2185 | int n = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2186 | |
Bram Moolenaar | 6a8ede9 | 2017-01-22 19:49:12 +0100 | [diff] [blame] | 2187 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2188 | n = (*mb_char2bytes)(c, buf); |
| 2189 | |
| 2190 | /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte. |
| 2191 | * Happens for CTRL-Vu9900. */ |
| 2192 | if (buf[0] == 0) |
| 2193 | buf[0] = '\n'; |
Bram Moolenaar | 560379d | 2017-01-21 22:50:00 +0100 | [diff] [blame] | 2194 | #else |
| 2195 | buf[0] = c; |
| 2196 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2197 | |
| 2198 | ins_char_bytes(buf, n); |
| 2199 | } |
| 2200 | |
| 2201 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2202 | ins_char_bytes(char_u *buf, int charlen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2203 | { |
| 2204 | int c = buf[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2205 | int newlen; /* nr of bytes inserted */ |
| 2206 | int oldlen; /* nr of bytes deleted (0 when not replacing) */ |
| 2207 | char_u *p; |
| 2208 | char_u *newp; |
| 2209 | char_u *oldp; |
| 2210 | int linelen; /* length of old line including NUL */ |
| 2211 | colnr_T col; |
| 2212 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2213 | int i; |
| 2214 | |
| 2215 | #ifdef FEAT_VIRTUALEDIT |
| 2216 | /* Break tabs if needed. */ |
| 2217 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 2218 | coladvance_force(getviscol()); |
| 2219 | #endif |
| 2220 | |
| 2221 | col = curwin->w_cursor.col; |
| 2222 | oldp = ml_get(lnum); |
| 2223 | linelen = (int)STRLEN(oldp) + 1; |
| 2224 | |
| 2225 | /* The lengths default to the values for when not replacing. */ |
| 2226 | oldlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2227 | newlen = charlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2228 | |
| 2229 | if (State & REPLACE_FLAG) |
| 2230 | { |
| 2231 | #ifdef FEAT_VREPLACE |
| 2232 | if (State & VREPLACE_FLAG) |
| 2233 | { |
| 2234 | colnr_T new_vcol = 0; /* init for GCC */ |
| 2235 | colnr_T vcol; |
| 2236 | int old_list; |
| 2237 | #ifndef FEAT_MBYTE |
| 2238 | char_u buf[2]; |
| 2239 | #endif |
| 2240 | |
| 2241 | /* |
| 2242 | * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag. |
| 2243 | * Returns the old value of list, so when finished, |
| 2244 | * curwin->w_p_list should be set back to this. |
| 2245 | */ |
| 2246 | old_list = curwin->w_p_list; |
| 2247 | if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 2248 | curwin->w_p_list = FALSE; |
| 2249 | |
| 2250 | /* |
| 2251 | * In virtual replace mode each character may replace one or more |
| 2252 | * characters (zero if it's a TAB). Count the number of bytes to |
| 2253 | * be deleted to make room for the new character, counting screen |
| 2254 | * cells. May result in adding spaces to fill a gap. |
| 2255 | */ |
| 2256 | getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL); |
| 2257 | #ifndef FEAT_MBYTE |
| 2258 | buf[0] = c; |
| 2259 | buf[1] = NUL; |
| 2260 | #endif |
| 2261 | new_vcol = vcol + chartabsize(buf, vcol); |
| 2262 | while (oldp[col + oldlen] != NUL && vcol < new_vcol) |
| 2263 | { |
| 2264 | vcol += chartabsize(oldp + col + oldlen, vcol); |
| 2265 | /* Don't need to remove a TAB that takes us to the right |
| 2266 | * position. */ |
| 2267 | if (vcol > new_vcol && oldp[col + oldlen] == TAB) |
| 2268 | break; |
| 2269 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2270 | oldlen += (*mb_ptr2len)(oldp + col + oldlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2271 | #else |
| 2272 | ++oldlen; |
| 2273 | #endif |
| 2274 | /* Deleted a bit too much, insert spaces. */ |
| 2275 | if (vcol > new_vcol) |
| 2276 | newlen += vcol - new_vcol; |
| 2277 | } |
| 2278 | curwin->w_p_list = old_list; |
| 2279 | } |
| 2280 | else |
| 2281 | #endif |
| 2282 | if (oldp[col] != NUL) |
| 2283 | { |
| 2284 | /* normal replace */ |
| 2285 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2286 | oldlen = (*mb_ptr2len)(oldp + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2287 | #else |
| 2288 | oldlen = 1; |
| 2289 | #endif |
| 2290 | } |
| 2291 | |
| 2292 | |
| 2293 | /* Push the replaced bytes onto the replace stack, so that they can be |
| 2294 | * put back when BS is used. The bytes of a multi-byte character are |
| 2295 | * done the other way around, so that the first byte is popped off |
| 2296 | * first (it tells the byte length of the character). */ |
| 2297 | replace_push(NUL); |
| 2298 | for (i = 0; i < oldlen; ++i) |
| 2299 | { |
| 2300 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 2301 | if (has_mbyte) |
| 2302 | i += replace_push_mb(oldp + col + i) - 1; |
| 2303 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2304 | #endif |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 2305 | replace_push(oldp[col + i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | newp = alloc_check((unsigned)(linelen + newlen - oldlen)); |
| 2310 | if (newp == NULL) |
| 2311 | return; |
| 2312 | |
| 2313 | /* Copy bytes before the cursor. */ |
| 2314 | if (col > 0) |
| 2315 | mch_memmove(newp, oldp, (size_t)col); |
| 2316 | |
| 2317 | /* Copy bytes after the changed character(s). */ |
| 2318 | p = newp + col; |
| 2319 | mch_memmove(p + newlen, oldp + col + oldlen, |
| 2320 | (size_t)(linelen - col - oldlen)); |
| 2321 | |
| 2322 | /* Insert or overwrite the new character. */ |
| 2323 | #ifdef FEAT_MBYTE |
| 2324 | mch_memmove(p, buf, charlen); |
| 2325 | i = charlen; |
| 2326 | #else |
| 2327 | *p = c; |
| 2328 | i = 1; |
| 2329 | #endif |
| 2330 | |
| 2331 | /* Fill with spaces when necessary. */ |
| 2332 | while (i < newlen) |
| 2333 | p[i++] = ' '; |
| 2334 | |
| 2335 | /* Replace the line in the buffer. */ |
| 2336 | ml_replace(lnum, newp, FALSE); |
| 2337 | |
| 2338 | /* mark the buffer as changed and prepare for displaying */ |
| 2339 | changed_bytes(lnum, col); |
| 2340 | |
| 2341 | /* |
| 2342 | * If we're in Insert or Replace mode and 'showmatch' is set, then briefly |
| 2343 | * show the match for right parens and braces. |
| 2344 | */ |
| 2345 | if (p_sm && (State & INSERT) |
| 2346 | && msg_silent == 0 |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 2347 | #ifdef FEAT_INS_EXPAND |
| 2348 | && !ins_compl_active() |
| 2349 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2350 | ) |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2351 | { |
| 2352 | #ifdef FEAT_MBYTE |
| 2353 | if (has_mbyte) |
| 2354 | showmatch(mb_ptr2char(buf)); |
| 2355 | else |
| 2356 | #endif |
| 2357 | showmatch(c); |
| 2358 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2359 | |
| 2360 | #ifdef FEAT_RIGHTLEFT |
| 2361 | if (!p_ri || (State & REPLACE_FLAG)) |
| 2362 | #endif |
| 2363 | { |
| 2364 | /* Normal insert: move cursor right */ |
| 2365 | #ifdef FEAT_MBYTE |
| 2366 | curwin->w_cursor.col += charlen; |
| 2367 | #else |
| 2368 | ++curwin->w_cursor.col; |
| 2369 | #endif |
| 2370 | } |
| 2371 | /* |
| 2372 | * TODO: should try to update w_row here, to avoid recomputing it later. |
| 2373 | */ |
| 2374 | } |
| 2375 | |
| 2376 | /* |
| 2377 | * Insert a string at the cursor position. |
| 2378 | * Note: Does NOT handle Replace mode. |
| 2379 | * Caller must have prepared for undo. |
| 2380 | */ |
| 2381 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2382 | ins_str(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2383 | { |
| 2384 | char_u *oldp, *newp; |
| 2385 | int newlen = (int)STRLEN(s); |
| 2386 | int oldlen; |
| 2387 | colnr_T col; |
| 2388 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2389 | |
| 2390 | #ifdef FEAT_VIRTUALEDIT |
| 2391 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 2392 | coladvance_force(getviscol()); |
| 2393 | #endif |
| 2394 | |
| 2395 | col = curwin->w_cursor.col; |
| 2396 | oldp = ml_get(lnum); |
| 2397 | oldlen = (int)STRLEN(oldp); |
| 2398 | |
| 2399 | newp = alloc_check((unsigned)(oldlen + newlen + 1)); |
| 2400 | if (newp == NULL) |
| 2401 | return; |
| 2402 | if (col > 0) |
| 2403 | mch_memmove(newp, oldp, (size_t)col); |
| 2404 | mch_memmove(newp + col, s, (size_t)newlen); |
| 2405 | mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1)); |
| 2406 | ml_replace(lnum, newp, FALSE); |
| 2407 | changed_bytes(lnum, col); |
| 2408 | curwin->w_cursor.col += newlen; |
| 2409 | } |
| 2410 | |
| 2411 | /* |
| 2412 | * Delete one character under the cursor. |
| 2413 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 2414 | * Caller must have prepared for undo. |
| 2415 | * |
| 2416 | * return FAIL for failure, OK otherwise |
| 2417 | */ |
| 2418 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2419 | del_char(int fixpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2420 | { |
| 2421 | #ifdef FEAT_MBYTE |
| 2422 | if (has_mbyte) |
| 2423 | { |
| 2424 | /* Make sure the cursor is at the start of a character. */ |
| 2425 | mb_adjust_cursor(); |
| 2426 | if (*ml_get_cursor() == NUL) |
| 2427 | return FAIL; |
| 2428 | return del_chars(1L, fixpos); |
| 2429 | } |
| 2430 | #endif |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2431 | return del_bytes(1L, fixpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 2435 | /* |
| 2436 | * Like del_bytes(), but delete characters instead of bytes. |
| 2437 | */ |
| 2438 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2439 | del_chars(long count, int fixpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2440 | { |
| 2441 | long bytes = 0; |
| 2442 | long i; |
| 2443 | char_u *p; |
| 2444 | int l; |
| 2445 | |
| 2446 | p = ml_get_cursor(); |
| 2447 | for (i = 0; i < count && *p != NUL; ++i) |
| 2448 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2449 | l = (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2450 | bytes += l; |
| 2451 | p += l; |
| 2452 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2453 | return del_bytes(bytes, fixpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2454 | } |
| 2455 | #endif |
| 2456 | |
| 2457 | /* |
| 2458 | * Delete "count" bytes under the cursor. |
| 2459 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 2460 | * Caller must have prepared for undo. |
| 2461 | * |
| 2462 | * return FAIL for failure, OK otherwise |
| 2463 | */ |
| 2464 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2465 | del_bytes( |
| 2466 | long count, |
| 2467 | int fixpos_arg, |
| 2468 | int use_delcombine UNUSED) /* 'delcombine' option applies */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2469 | { |
| 2470 | char_u *oldp, *newp; |
| 2471 | colnr_T oldlen; |
| 2472 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2473 | colnr_T col = curwin->w_cursor.col; |
| 2474 | int was_alloced; |
| 2475 | long movelen; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2476 | int fixpos = fixpos_arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2477 | |
| 2478 | oldp = ml_get(lnum); |
| 2479 | oldlen = (int)STRLEN(oldp); |
| 2480 | |
| 2481 | /* |
| 2482 | * Can't do anything when the cursor is on the NUL after the line. |
| 2483 | */ |
| 2484 | if (col >= oldlen) |
| 2485 | return FAIL; |
| 2486 | |
| 2487 | #ifdef FEAT_MBYTE |
| 2488 | /* If 'delcombine' is set and deleting (less than) one character, only |
| 2489 | * delete the last combining character. */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2490 | if (p_deco && use_delcombine && enc_utf8 |
| 2491 | && utfc_ptr2len(oldp + col) >= count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2492 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2493 | int cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2494 | int n; |
| 2495 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2496 | (void)utfc_ptr2char(oldp + col, cc); |
| 2497 | if (cc[0] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2498 | { |
| 2499 | /* Find the last composing char, there can be several. */ |
| 2500 | n = col; |
| 2501 | do |
| 2502 | { |
| 2503 | col = n; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2504 | count = utf_ptr2len(oldp + n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2505 | n += count; |
| 2506 | } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n)); |
| 2507 | fixpos = 0; |
| 2508 | } |
| 2509 | } |
| 2510 | #endif |
| 2511 | |
| 2512 | /* |
| 2513 | * When count is too big, reduce it. |
| 2514 | */ |
| 2515 | movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */ |
| 2516 | if (movelen <= 1) |
| 2517 | { |
| 2518 | /* |
| 2519 | * 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] | 2520 | * fixpos is TRUE, we don't want to end up positioned at the NUL, |
| 2521 | * unless "restart_edit" is set or 'virtualedit' contains "onemore". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2522 | */ |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2523 | if (col > 0 && fixpos && restart_edit == 0 |
| 2524 | #ifdef FEAT_VIRTUALEDIT |
| 2525 | && (ve_flags & VE_ONEMORE) == 0 |
| 2526 | #endif |
| 2527 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2528 | { |
| 2529 | --curwin->w_cursor.col; |
| 2530 | #ifdef FEAT_VIRTUALEDIT |
| 2531 | curwin->w_cursor.coladd = 0; |
| 2532 | #endif |
| 2533 | #ifdef FEAT_MBYTE |
| 2534 | if (has_mbyte) |
| 2535 | curwin->w_cursor.col -= |
| 2536 | (*mb_head_off)(oldp, oldp + curwin->w_cursor.col); |
| 2537 | #endif |
| 2538 | } |
| 2539 | count = oldlen - col; |
| 2540 | movelen = 1; |
| 2541 | } |
| 2542 | |
| 2543 | /* |
| 2544 | * If the old line has been allocated the deletion can be done in the |
| 2545 | * existing line. Otherwise a new line has to be allocated |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2546 | * Can't do this when using Netbeans, because we would need to invoke |
| 2547 | * netbeans_removed(), which deallocates the line. Let ml_replace() take |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 2548 | * care of notifying Netbeans. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2549 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2550 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2551 | if (netbeans_active()) |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2552 | was_alloced = FALSE; |
| 2553 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2554 | #endif |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2555 | was_alloced = ml_line_alloced(); /* check if oldp was allocated */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2556 | if (was_alloced) |
| 2557 | newp = oldp; /* use same allocated memory */ |
| 2558 | else |
| 2559 | { /* need to allocate a new line */ |
| 2560 | newp = alloc((unsigned)(oldlen + 1 - count)); |
| 2561 | if (newp == NULL) |
| 2562 | return FAIL; |
| 2563 | mch_memmove(newp, oldp, (size_t)col); |
| 2564 | } |
| 2565 | mch_memmove(newp + col, oldp + col + count, (size_t)movelen); |
| 2566 | if (!was_alloced) |
| 2567 | ml_replace(lnum, newp, FALSE); |
| 2568 | |
| 2569 | /* mark the buffer as changed and prepare for displaying */ |
| 2570 | changed_bytes(lnum, curwin->w_cursor.col); |
| 2571 | |
| 2572 | return OK; |
| 2573 | } |
| 2574 | |
| 2575 | /* |
| 2576 | * Delete from cursor to end of line. |
| 2577 | * Caller must have prepared for undo. |
| 2578 | * |
| 2579 | * return FAIL for failure, OK otherwise |
| 2580 | */ |
| 2581 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2582 | truncate_line( |
| 2583 | int fixpos) /* if TRUE fix the cursor position when done */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2584 | { |
| 2585 | char_u *newp; |
| 2586 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2587 | colnr_T col = curwin->w_cursor.col; |
| 2588 | |
| 2589 | if (col == 0) |
| 2590 | newp = vim_strsave((char_u *)""); |
| 2591 | else |
| 2592 | newp = vim_strnsave(ml_get(lnum), col); |
| 2593 | |
| 2594 | if (newp == NULL) |
| 2595 | return FAIL; |
| 2596 | |
| 2597 | ml_replace(lnum, newp, FALSE); |
| 2598 | |
| 2599 | /* mark the buffer as changed and prepare for displaying */ |
| 2600 | changed_bytes(lnum, curwin->w_cursor.col); |
| 2601 | |
| 2602 | /* |
| 2603 | * If "fixpos" is TRUE we don't want to end up positioned at the NUL. |
| 2604 | */ |
| 2605 | if (fixpos && curwin->w_cursor.col > 0) |
| 2606 | --curwin->w_cursor.col; |
| 2607 | |
| 2608 | return OK; |
| 2609 | } |
| 2610 | |
| 2611 | /* |
| 2612 | * Delete "nlines" lines at the cursor. |
| 2613 | * Saves the lines for undo first if "undo" is TRUE. |
| 2614 | */ |
| 2615 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2616 | del_lines( |
| 2617 | long nlines, /* number of lines to delete */ |
| 2618 | int undo) /* if TRUE, prepare for undo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2619 | { |
| 2620 | long n; |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2621 | linenr_T first = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2622 | |
| 2623 | if (nlines <= 0) |
| 2624 | return; |
| 2625 | |
| 2626 | /* save the deleted lines for undo */ |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2627 | if (undo && u_savedel(first, nlines) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2628 | return; |
| 2629 | |
| 2630 | for (n = 0; n < nlines; ) |
| 2631 | { |
| 2632 | if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ |
| 2633 | break; |
| 2634 | |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2635 | ml_delete(first, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2636 | ++n; |
| 2637 | |
| 2638 | /* If we delete the last line in the file, stop */ |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2639 | if (first > curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2640 | break; |
| 2641 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2642 | |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2643 | /* Correct the cursor position before calling deleted_lines_mark(), it may |
| 2644 | * trigger a callback to display the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2645 | curwin->w_cursor.col = 0; |
| 2646 | check_cursor_lnum(); |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2647 | |
| 2648 | /* adjust marks, mark the buffer as changed and prepare for displaying */ |
| 2649 | deleted_lines_mark(first, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2653 | gchar_pos(pos_T *pos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2654 | { |
| 2655 | char_u *ptr = ml_get_pos(pos); |
| 2656 | |
| 2657 | #ifdef FEAT_MBYTE |
| 2658 | if (has_mbyte) |
| 2659 | return (*mb_ptr2char)(ptr); |
| 2660 | #endif |
| 2661 | return (int)*ptr; |
| 2662 | } |
| 2663 | |
| 2664 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2665 | gchar_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2666 | { |
| 2667 | #ifdef FEAT_MBYTE |
| 2668 | if (has_mbyte) |
| 2669 | return (*mb_ptr2char)(ml_get_cursor()); |
| 2670 | #endif |
| 2671 | return (int)*ml_get_cursor(); |
| 2672 | } |
| 2673 | |
| 2674 | /* |
| 2675 | * Write a character at the current cursor position. |
| 2676 | * It is directly written into the block. |
| 2677 | */ |
| 2678 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2679 | pchar_cursor(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2680 | { |
| 2681 | *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) |
| 2682 | + curwin->w_cursor.col) = c; |
| 2683 | } |
| 2684 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2685 | /* |
| 2686 | * When extra == 0: Return TRUE if the cursor is before or on the first |
| 2687 | * non-blank in the line. |
| 2688 | * When extra == 1: Return TRUE if the cursor is before the first non-blank in |
| 2689 | * the line. |
| 2690 | */ |
| 2691 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2692 | inindent(int extra) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2693 | { |
| 2694 | char_u *ptr; |
| 2695 | colnr_T col; |
| 2696 | |
| 2697 | for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col) |
| 2698 | ++ptr; |
| 2699 | if (col >= curwin->w_cursor.col + extra) |
| 2700 | return TRUE; |
| 2701 | else |
| 2702 | return FALSE; |
| 2703 | } |
| 2704 | |
| 2705 | /* |
| 2706 | * Skip to next part of an option argument: Skip space and comma. |
| 2707 | */ |
| 2708 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2709 | skip_to_option_part(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2710 | { |
| 2711 | if (*p == ',') |
| 2712 | ++p; |
| 2713 | while (*p == ' ') |
| 2714 | ++p; |
| 2715 | return p; |
| 2716 | } |
| 2717 | |
| 2718 | /* |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2719 | * Call this function when something in the current buffer is changed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2720 | * |
| 2721 | * Most often called through changed_bytes() and changed_lines(), which also |
| 2722 | * mark the area of the display to be redrawn. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2723 | * |
| 2724 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2725 | */ |
| 2726 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2727 | changed(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2728 | { |
| 2729 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 2730 | /* The text of the preediting area is inserted, but this doesn't |
| 2731 | * mean a change of the buffer yet. That is delayed until the |
| 2732 | * text is committed. (this means preedit becomes empty) */ |
| 2733 | if (im_is_preediting() && !xim_changed_while_preediting) |
| 2734 | return; |
| 2735 | xim_changed_while_preediting = FALSE; |
| 2736 | #endif |
| 2737 | |
| 2738 | if (!curbuf->b_changed) |
| 2739 | { |
| 2740 | int save_msg_scroll = msg_scroll; |
| 2741 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2742 | /* Give a warning about changing a read-only file. This may also |
| 2743 | * check-out the file, thus change "curbuf"! */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2744 | change_warning(0); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2745 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2746 | /* Create a swap file if that is wanted. |
| 2747 | * Don't do this for "nofile" and "nowrite" buffer types. */ |
| 2748 | if (curbuf->b_may_swap |
| 2749 | #ifdef FEAT_QUICKFIX |
| 2750 | && !bt_dontwrite(curbuf) |
| 2751 | #endif |
| 2752 | ) |
| 2753 | { |
Bram Moolenaar | b01f357 | 2016-01-15 15:17:04 +0100 | [diff] [blame] | 2754 | int save_need_wait_return = need_wait_return; |
| 2755 | |
| 2756 | need_wait_return = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2757 | ml_open_file(curbuf); |
| 2758 | |
| 2759 | /* The ml_open_file() can cause an ATTENTION message. |
| 2760 | * Wait two seconds, to make sure the user reads this unexpected |
| 2761 | * message. Since we could be anywhere, call wait_return() now, |
| 2762 | * and don't let the emsg() set msg_scroll. */ |
| 2763 | if (need_wait_return && emsg_silent == 0) |
| 2764 | { |
| 2765 | out_flush(); |
| 2766 | ui_delay(2000L, TRUE); |
| 2767 | wait_return(TRUE); |
| 2768 | msg_scroll = save_msg_scroll; |
| 2769 | } |
Bram Moolenaar | b01f357 | 2016-01-15 15:17:04 +0100 | [diff] [blame] | 2770 | else |
| 2771 | need_wait_return = save_need_wait_return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2772 | } |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2773 | changed_int(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2774 | } |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 2775 | ++CHANGEDTICK(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2778 | /* |
| 2779 | * Internal part of changed(), no user interaction. |
| 2780 | */ |
| 2781 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2782 | changed_int(void) |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2783 | { |
| 2784 | curbuf->b_changed = TRUE; |
| 2785 | ml_setflags(curbuf); |
| 2786 | #ifdef FEAT_WINDOWS |
| 2787 | check_status(curbuf); |
| 2788 | redraw_tabline = TRUE; |
| 2789 | #endif |
| 2790 | #ifdef FEAT_TITLE |
| 2791 | need_maketitle = TRUE; /* set window title later */ |
| 2792 | #endif |
| 2793 | } |
| 2794 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 2795 | static void changedOneline(buf_T *buf, linenr_T lnum); |
| 2796 | static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra); |
| 2797 | 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] | 2798 | |
| 2799 | /* |
| 2800 | * Changed bytes within a single line for the current buffer. |
| 2801 | * - marks the windows on this buffer to be redisplayed |
| 2802 | * - marks the buffer changed by calling changed() |
| 2803 | * - invalidates cached values |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2804 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2805 | */ |
| 2806 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2807 | changed_bytes(linenr_T lnum, colnr_T col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2808 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2809 | changedOneline(curbuf, lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2810 | changed_common(lnum, col, lnum + 1, 0L); |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2811 | |
| 2812 | #ifdef FEAT_DIFF |
| 2813 | /* Diff highlighting in other diff windows may need to be updated too. */ |
| 2814 | if (curwin->w_p_diff) |
| 2815 | { |
| 2816 | win_T *wp; |
| 2817 | linenr_T wlnum; |
| 2818 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 2819 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2820 | if (wp->w_p_diff && wp != curwin) |
| 2821 | { |
| 2822 | redraw_win_later(wp, VALID); |
| 2823 | wlnum = diff_lnum_win(lnum, wp); |
| 2824 | if (wlnum > 0) |
| 2825 | changedOneline(wp->w_buffer, wlnum); |
| 2826 | } |
| 2827 | } |
| 2828 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2832 | changedOneline(buf_T *buf, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2833 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2834 | if (buf->b_mod_set) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2835 | { |
| 2836 | /* find the maximum area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2837 | if (lnum < buf->b_mod_top) |
| 2838 | buf->b_mod_top = lnum; |
| 2839 | else if (lnum >= buf->b_mod_bot) |
| 2840 | buf->b_mod_bot = lnum + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2841 | } |
| 2842 | else |
| 2843 | { |
| 2844 | /* set the area that must be redisplayed to one line */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2845 | buf->b_mod_set = TRUE; |
| 2846 | buf->b_mod_top = lnum; |
| 2847 | buf->b_mod_bot = lnum + 1; |
| 2848 | buf->b_mod_xlines = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | /* |
| 2853 | * Appended "count" lines below line "lnum" in the current buffer. |
| 2854 | * Must be called AFTER the change and after mark_adjust(). |
| 2855 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 2856 | */ |
| 2857 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2858 | appended_lines(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2859 | { |
| 2860 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 2861 | } |
| 2862 | |
| 2863 | /* |
| 2864 | * Like appended_lines(), but adjust marks first. |
| 2865 | */ |
| 2866 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2867 | appended_lines_mark(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2868 | { |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 2869 | /* 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] | 2870 | * be marks there. But it's still needed in diff mode. */ |
| 2871 | if (lnum + count < curbuf->b_ml.ml_line_count |
| 2872 | #ifdef FEAT_DIFF |
| 2873 | || curwin->w_p_diff |
| 2874 | #endif |
| 2875 | ) |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 2876 | mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2877 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 2878 | } |
| 2879 | |
| 2880 | /* |
| 2881 | * Deleted "count" lines at line "lnum" in the current buffer. |
| 2882 | * Must be called AFTER the change and after mark_adjust(). |
| 2883 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 2884 | */ |
| 2885 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2886 | deleted_lines(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2887 | { |
| 2888 | changed_lines(lnum, 0, lnum + count, -count); |
| 2889 | } |
| 2890 | |
| 2891 | /* |
| 2892 | * Like deleted_lines(), but adjust marks first. |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2893 | * Make sure the cursor is on a valid line before calling, a GUI callback may |
| 2894 | * be triggered to display the cursor. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2895 | */ |
| 2896 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2897 | deleted_lines_mark(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2898 | { |
| 2899 | mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count); |
| 2900 | changed_lines(lnum, 0, lnum + count, -count); |
| 2901 | } |
| 2902 | |
| 2903 | /* |
| 2904 | * Changed lines for the current buffer. |
| 2905 | * Must be called AFTER the change and after mark_adjust(). |
| 2906 | * - mark the buffer changed by calling changed() |
| 2907 | * - mark the windows on this buffer to be redisplayed |
| 2908 | * - invalidate cached values |
| 2909 | * "lnum" is the first line that needs displaying, "lnume" the first line |
| 2910 | * below the changed lines (BEFORE the change). |
| 2911 | * When only inserting lines, "lnum" and "lnume" are equal. |
| 2912 | * Takes care of calling changed() and updating b_mod_*. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2913 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2914 | */ |
| 2915 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2916 | changed_lines( |
| 2917 | linenr_T lnum, /* first line with change */ |
| 2918 | colnr_T col, /* column in first line with change */ |
| 2919 | linenr_T lnume, /* line below last changed line */ |
| 2920 | long xtra) /* number of extra lines (negative when deleting) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2921 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2922 | changed_lines_buf(curbuf, lnum, lnume, xtra); |
| 2923 | |
| 2924 | #ifdef FEAT_DIFF |
| 2925 | if (xtra == 0 && curwin->w_p_diff) |
| 2926 | { |
| 2927 | /* When the number of lines doesn't change then mark_adjust() isn't |
| 2928 | * called and other diff buffers still need to be marked for |
| 2929 | * displaying. */ |
| 2930 | win_T *wp; |
| 2931 | linenr_T wlnum; |
| 2932 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 2933 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2934 | if (wp->w_p_diff && wp != curwin) |
| 2935 | { |
| 2936 | redraw_win_later(wp, VALID); |
| 2937 | wlnum = diff_lnum_win(lnum, wp); |
| 2938 | if (wlnum > 0) |
| 2939 | changed_lines_buf(wp->w_buffer, wlnum, |
| 2940 | lnume - lnum + wlnum, 0L); |
| 2941 | } |
| 2942 | } |
| 2943 | #endif |
| 2944 | |
| 2945 | changed_common(lnum, col, lnume, xtra); |
| 2946 | } |
| 2947 | |
| 2948 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2949 | changed_lines_buf( |
| 2950 | buf_T *buf, |
| 2951 | linenr_T lnum, /* first line with change */ |
| 2952 | linenr_T lnume, /* line below last changed line */ |
| 2953 | long xtra) /* number of extra lines (negative when deleting) */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2954 | { |
| 2955 | if (buf->b_mod_set) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2956 | { |
| 2957 | /* find the maximum area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2958 | if (lnum < buf->b_mod_top) |
| 2959 | buf->b_mod_top = lnum; |
| 2960 | if (lnum < buf->b_mod_bot) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2961 | { |
| 2962 | /* adjust old bot position for xtra lines */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2963 | buf->b_mod_bot += xtra; |
| 2964 | if (buf->b_mod_bot < lnum) |
| 2965 | buf->b_mod_bot = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2966 | } |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2967 | if (lnume + xtra > buf->b_mod_bot) |
| 2968 | buf->b_mod_bot = lnume + xtra; |
| 2969 | buf->b_mod_xlines += xtra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2970 | } |
| 2971 | else |
| 2972 | { |
| 2973 | /* set the area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2974 | buf->b_mod_set = TRUE; |
| 2975 | buf->b_mod_top = lnum; |
| 2976 | buf->b_mod_bot = lnume + xtra; |
| 2977 | buf->b_mod_xlines = xtra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2978 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2981 | /* |
| 2982 | * Common code for when a change is was made. |
| 2983 | * See changed_lines() for the arguments. |
| 2984 | * Careful: may trigger autocommands that reload the buffer. |
| 2985 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2986 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2987 | changed_common( |
| 2988 | linenr_T lnum, |
| 2989 | colnr_T col, |
| 2990 | linenr_T lnume, |
| 2991 | long xtra) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2992 | { |
| 2993 | win_T *wp; |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 2994 | #ifdef FEAT_WINDOWS |
| 2995 | tabpage_T *tp; |
| 2996 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2997 | int i; |
| 2998 | #ifdef FEAT_JUMPLIST |
| 2999 | int cols; |
| 3000 | pos_T *p; |
| 3001 | int add; |
| 3002 | #endif |
| 3003 | |
| 3004 | /* mark the buffer as modified */ |
| 3005 | changed(); |
| 3006 | |
| 3007 | /* set the '. mark */ |
| 3008 | if (!cmdmod.keepjumps) |
| 3009 | { |
| 3010 | curbuf->b_last_change.lnum = lnum; |
| 3011 | curbuf->b_last_change.col = col; |
| 3012 | |
| 3013 | #ifdef FEAT_JUMPLIST |
| 3014 | /* Create a new entry if a new undo-able change was started or we |
| 3015 | * don't have an entry yet. */ |
| 3016 | if (curbuf->b_new_change || curbuf->b_changelistlen == 0) |
| 3017 | { |
| 3018 | if (curbuf->b_changelistlen == 0) |
| 3019 | add = TRUE; |
| 3020 | else |
| 3021 | { |
| 3022 | /* Don't create a new entry when the line number is the same |
| 3023 | * as the last one and the column is not too far away. Avoids |
| 3024 | * creating many entries for typing "xxxxx". */ |
| 3025 | p = &curbuf->b_changelist[curbuf->b_changelistlen - 1]; |
| 3026 | if (p->lnum != lnum) |
| 3027 | add = TRUE; |
| 3028 | else |
| 3029 | { |
| 3030 | cols = comp_textwidth(FALSE); |
| 3031 | if (cols == 0) |
| 3032 | cols = 79; |
| 3033 | add = (p->col + cols < col || col + cols < p->col); |
| 3034 | } |
| 3035 | } |
| 3036 | if (add) |
| 3037 | { |
| 3038 | /* This is the first of a new sequence of undo-able changes |
| 3039 | * and it's at some distance of the last change. Use a new |
| 3040 | * position in the changelist. */ |
| 3041 | curbuf->b_new_change = FALSE; |
| 3042 | |
| 3043 | if (curbuf->b_changelistlen == JUMPLISTSIZE) |
| 3044 | { |
| 3045 | /* changelist is full: remove oldest entry */ |
| 3046 | curbuf->b_changelistlen = JUMPLISTSIZE - 1; |
| 3047 | mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1, |
| 3048 | sizeof(pos_T) * (JUMPLISTSIZE - 1)); |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3049 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3050 | { |
| 3051 | /* Correct position in changelist for other windows on |
| 3052 | * this buffer. */ |
| 3053 | if (wp->w_buffer == curbuf && wp->w_changelistidx > 0) |
| 3054 | --wp->w_changelistidx; |
| 3055 | } |
| 3056 | } |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3057 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3058 | { |
| 3059 | /* For other windows, if the position in the changelist is |
| 3060 | * at the end it stays at the end. */ |
| 3061 | if (wp->w_buffer == curbuf |
| 3062 | && wp->w_changelistidx == curbuf->b_changelistlen) |
| 3063 | ++wp->w_changelistidx; |
| 3064 | } |
| 3065 | ++curbuf->b_changelistlen; |
| 3066 | } |
| 3067 | } |
| 3068 | curbuf->b_changelist[curbuf->b_changelistlen - 1] = |
| 3069 | curbuf->b_last_change; |
| 3070 | /* The current window is always after the last change, so that "g," |
| 3071 | * takes you back to it. */ |
| 3072 | curwin->w_changelistidx = curbuf->b_changelistlen; |
| 3073 | #endif |
| 3074 | } |
| 3075 | |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3076 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3077 | { |
| 3078 | if (wp->w_buffer == curbuf) |
| 3079 | { |
| 3080 | /* Mark this window to be redrawn later. */ |
| 3081 | if (wp->w_redr_type < VALID) |
| 3082 | wp->w_redr_type = VALID; |
| 3083 | |
| 3084 | /* Check if a change in the buffer has invalidated the cached |
| 3085 | * values for the cursor. */ |
| 3086 | #ifdef FEAT_FOLDING |
| 3087 | /* |
| 3088 | * Update the folds for this window. Can't postpone this, because |
| 3089 | * a following operator might work on the whole fold: ">>dd". |
| 3090 | */ |
| 3091 | foldUpdate(wp, lnum, lnume + xtra - 1); |
| 3092 | |
| 3093 | /* The change may cause lines above or below the change to become |
| 3094 | * included in a fold. Set lnum/lnume to the first/last line that |
| 3095 | * might be displayed differently. |
| 3096 | * Set w_cline_folded here as an efficient way to update it when |
| 3097 | * inserting lines just above a closed fold. */ |
| 3098 | i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL); |
| 3099 | if (wp->w_cursor.lnum == lnum) |
| 3100 | wp->w_cline_folded = i; |
| 3101 | i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL); |
| 3102 | if (wp->w_cursor.lnum == lnume) |
| 3103 | wp->w_cline_folded = i; |
| 3104 | |
| 3105 | /* If the changed line is in a range of previously folded lines, |
| 3106 | * compare with the first line in that range. */ |
| 3107 | if (wp->w_cursor.lnum <= lnum) |
| 3108 | { |
| 3109 | i = find_wl_entry(wp, lnum); |
| 3110 | if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum) |
| 3111 | changed_line_abv_curs_win(wp); |
| 3112 | } |
| 3113 | #endif |
| 3114 | |
| 3115 | if (wp->w_cursor.lnum > lnum) |
| 3116 | changed_line_abv_curs_win(wp); |
| 3117 | else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col) |
| 3118 | changed_cline_bef_curs_win(wp); |
| 3119 | if (wp->w_botline >= lnum) |
| 3120 | { |
| 3121 | /* Assume that botline doesn't change (inserted lines make |
| 3122 | * other lines scroll down below botline). */ |
| 3123 | approximate_botline_win(wp); |
| 3124 | } |
| 3125 | |
| 3126 | /* Check if any w_lines[] entries have become invalid. |
| 3127 | * For entries below the change: Correct the lnums for |
| 3128 | * inserted/deleted lines. Makes it possible to stop displaying |
| 3129 | * after the change. */ |
| 3130 | for (i = 0; i < wp->w_lines_valid; ++i) |
| 3131 | if (wp->w_lines[i].wl_valid) |
| 3132 | { |
| 3133 | if (wp->w_lines[i].wl_lnum >= lnum) |
| 3134 | { |
| 3135 | if (wp->w_lines[i].wl_lnum < lnume) |
| 3136 | { |
| 3137 | /* line included in change */ |
| 3138 | wp->w_lines[i].wl_valid = FALSE; |
| 3139 | } |
| 3140 | else if (xtra != 0) |
| 3141 | { |
| 3142 | /* line below change */ |
| 3143 | wp->w_lines[i].wl_lnum += xtra; |
| 3144 | #ifdef FEAT_FOLDING |
| 3145 | wp->w_lines[i].wl_lastlnum += xtra; |
| 3146 | #endif |
| 3147 | } |
| 3148 | } |
| 3149 | #ifdef FEAT_FOLDING |
| 3150 | else if (wp->w_lines[i].wl_lastlnum >= lnum) |
| 3151 | { |
| 3152 | /* change somewhere inside this range of folded lines, |
| 3153 | * may need to be redrawn */ |
| 3154 | wp->w_lines[i].wl_valid = FALSE; |
| 3155 | } |
| 3156 | #endif |
| 3157 | } |
Bram Moolenaar | 3234cc6 | 2009-11-03 17:47:12 +0000 | [diff] [blame] | 3158 | |
| 3159 | #ifdef FEAT_FOLDING |
| 3160 | /* Take care of side effects for setting w_topline when folds have |
| 3161 | * changed. Esp. when the buffer was changed in another window. */ |
| 3162 | if (hasAnyFolding(wp)) |
| 3163 | set_topline(wp, wp->w_topline); |
| 3164 | #endif |
Bram Moolenaar | fd859c9 | 2014-05-13 12:44:24 +0200 | [diff] [blame] | 3165 | /* relative numbering may require updating more */ |
| 3166 | if (wp->w_p_rnu) |
| 3167 | redraw_win_later(wp, SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3168 | } |
| 3169 | } |
| 3170 | |
| 3171 | /* Call update_screen() later, which checks out what needs to be redrawn, |
| 3172 | * since it notices b_mod_set and then uses b_mod_*. */ |
| 3173 | if (must_redraw < VALID) |
| 3174 | must_redraw = VALID; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3175 | |
| 3176 | #ifdef FEAT_AUTOCMD |
| 3177 | /* when the cursor line is changed always trigger CursorMoved */ |
Bram Moolenaar | e163f1c | 2006-10-17 09:12:21 +0000 | [diff] [blame] | 3178 | if (lnum <= curwin->w_cursor.lnum |
| 3179 | && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum) |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3180 | last_cursormoved.lnum = 0; |
| 3181 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | /* |
| 3185 | * unchanged() is called when the changed flag must be reset for buffer 'buf' |
| 3186 | */ |
| 3187 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3188 | unchanged( |
| 3189 | buf_T *buf, |
| 3190 | int ff) /* also reset 'fileformat' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3191 | { |
Bram Moolenaar | 164c60f | 2011-01-22 00:11:50 +0100 | [diff] [blame] | 3192 | if (buf->b_changed || (ff && file_ff_differs(buf, FALSE))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3193 | { |
| 3194 | buf->b_changed = 0; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3195 | ml_setflags(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3196 | if (ff) |
| 3197 | save_file_ff(buf); |
| 3198 | #ifdef FEAT_WINDOWS |
| 3199 | check_status(buf); |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 3200 | redraw_tabline = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3201 | #endif |
| 3202 | #ifdef FEAT_TITLE |
| 3203 | need_maketitle = TRUE; /* set window title later */ |
| 3204 | #endif |
| 3205 | } |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 3206 | ++CHANGEDTICK(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3207 | #ifdef FEAT_NETBEANS_INTG |
| 3208 | netbeans_unmodified(buf); |
| 3209 | #endif |
| 3210 | } |
| 3211 | |
| 3212 | #if defined(FEAT_WINDOWS) || defined(PROTO) |
| 3213 | /* |
| 3214 | * check_status: called when the status bars for the buffer 'buf' |
| 3215 | * need to be updated |
| 3216 | */ |
| 3217 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3218 | check_status(buf_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3219 | { |
| 3220 | win_T *wp; |
| 3221 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 3222 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3223 | if (wp->w_buffer == buf && wp->w_status_height) |
| 3224 | { |
| 3225 | wp->w_redr_status = TRUE; |
| 3226 | if (must_redraw < VALID) |
| 3227 | must_redraw = VALID; |
| 3228 | } |
| 3229 | } |
| 3230 | #endif |
| 3231 | |
| 3232 | /* |
| 3233 | * If the file is readonly, give a warning message with the first change. |
| 3234 | * Don't do this for autocommands. |
| 3235 | * Don't use emsg(), because it flushes the macro buffer. |
Bram Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 3236 | * 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] | 3237 | * will be TRUE. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3238 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3239 | */ |
| 3240 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3241 | change_warning( |
| 3242 | int col) /* column for message; non-zero when in insert |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3243 | mode and 'showmode' is on */ |
| 3244 | { |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 3245 | static char *w_readonly = N_("W10: Warning: Changing a readonly file"); |
| 3246 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3247 | if (curbuf->b_did_warn == FALSE |
| 3248 | && curbufIsChanged() == 0 |
| 3249 | #ifdef FEAT_AUTOCMD |
| 3250 | && !autocmd_busy |
| 3251 | #endif |
| 3252 | && curbuf->b_p_ro) |
| 3253 | { |
| 3254 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3255 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3256 | apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3257 | --curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3258 | if (!curbuf->b_p_ro) |
| 3259 | return; |
| 3260 | #endif |
| 3261 | /* |
| 3262 | * Do what msg() does, but with a column offset if the warning should |
| 3263 | * be after the mode message. |
| 3264 | */ |
| 3265 | msg_start(); |
| 3266 | if (msg_row == Rows - 1) |
| 3267 | msg_col = col; |
Bram Moolenaar | 2df6dcc | 2004-07-12 15:53:54 +0000 | [diff] [blame] | 3268 | msg_source(hl_attr(HLF_W)); |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 3269 | MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST); |
| 3270 | #ifdef FEAT_EVAL |
| 3271 | set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1); |
| 3272 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3273 | msg_clr_eos(); |
| 3274 | (void)msg_end(); |
Bram Moolenaar | e5f2a07 | 2017-02-01 22:31:49 +0100 | [diff] [blame] | 3275 | if (msg_silent == 0 && !silent_mode |
| 3276 | #ifdef FEAT_EVAL |
| 3277 | && time_for_testing != 1 |
| 3278 | #endif |
| 3279 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3280 | { |
| 3281 | out_flush(); |
| 3282 | ui_delay(1000L, TRUE); /* give the user time to think about it */ |
| 3283 | } |
| 3284 | curbuf->b_did_warn = TRUE; |
| 3285 | redraw_cmdline = FALSE; /* don't redraw and erase the message */ |
| 3286 | if (msg_row < Rows - 1) |
| 3287 | showmode(); |
| 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | /* |
| 3292 | * Ask for a reply from the user, a 'y' or a 'n'. |
| 3293 | * No other characters are accepted, the message is repeated until a valid |
| 3294 | * reply is entered or CTRL-C is hit. |
| 3295 | * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters |
| 3296 | * from any buffers but directly from the user. |
| 3297 | * |
| 3298 | * return the 'y' or 'n' |
| 3299 | */ |
| 3300 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3301 | ask_yesno(char_u *str, int direct) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3302 | { |
| 3303 | int r = ' '; |
| 3304 | int save_State = State; |
| 3305 | |
| 3306 | if (exiting) /* put terminal in raw mode for this question */ |
| 3307 | settmode(TMODE_RAW); |
| 3308 | ++no_wait_return; |
| 3309 | #ifdef USE_ON_FLY_SCROLL |
| 3310 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 3311 | #endif |
| 3312 | State = CONFIRM; /* mouse behaves like with :confirm */ |
| 3313 | #ifdef FEAT_MOUSE |
| 3314 | setmouse(); /* disables mouse for xterm */ |
| 3315 | #endif |
| 3316 | ++no_mapping; |
| 3317 | ++allow_keys; /* no mapping here, but recognize keys */ |
| 3318 | |
| 3319 | while (r != 'y' && r != 'n') |
| 3320 | { |
| 3321 | /* same highlighting as for wait_return */ |
| 3322 | smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str); |
| 3323 | if (direct) |
| 3324 | r = get_keystroke(); |
| 3325 | else |
Bram Moolenaar | 913626c | 2008-01-03 11:43:42 +0000 | [diff] [blame] | 3326 | r = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3327 | if (r == Ctrl_C || r == ESC) |
| 3328 | r = 'n'; |
| 3329 | msg_putchar(r); /* show what you typed */ |
| 3330 | out_flush(); |
| 3331 | } |
| 3332 | --no_wait_return; |
| 3333 | State = save_State; |
| 3334 | #ifdef FEAT_MOUSE |
| 3335 | setmouse(); |
| 3336 | #endif |
| 3337 | --no_mapping; |
| 3338 | --allow_keys; |
| 3339 | |
| 3340 | return r; |
| 3341 | } |
| 3342 | |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3343 | #if defined(FEAT_MOUSE) || defined(PROTO) |
| 3344 | /* |
| 3345 | * Return TRUE if "c" is a mouse key. |
| 3346 | */ |
| 3347 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3348 | is_mouse_key(int c) |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3349 | { |
| 3350 | return c == K_LEFTMOUSE |
| 3351 | || c == K_LEFTMOUSE_NM |
| 3352 | || c == K_LEFTDRAG |
| 3353 | || c == K_LEFTRELEASE |
| 3354 | || c == K_LEFTRELEASE_NM |
| 3355 | || c == K_MIDDLEMOUSE |
| 3356 | || c == K_MIDDLEDRAG |
| 3357 | || c == K_MIDDLERELEASE |
| 3358 | || c == K_RIGHTMOUSE |
| 3359 | || c == K_RIGHTDRAG |
| 3360 | || c == K_RIGHTRELEASE |
| 3361 | || c == K_MOUSEDOWN |
| 3362 | || c == K_MOUSEUP |
| 3363 | || c == K_MOUSELEFT |
| 3364 | || c == K_MOUSERIGHT |
| 3365 | || c == K_X1MOUSE |
| 3366 | || c == K_X1DRAG |
| 3367 | || c == K_X1RELEASE |
| 3368 | || c == K_X2MOUSE |
| 3369 | || c == K_X2DRAG |
| 3370 | || c == K_X2RELEASE; |
| 3371 | } |
| 3372 | #endif |
| 3373 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3374 | /* |
| 3375 | * Get a key stroke directly from the user. |
| 3376 | * Ignores mouse clicks and scrollbar events, except a click for the left |
| 3377 | * button (used at the more prompt). |
| 3378 | * Doesn't use vgetc(), because it syncs undo and eats mapped characters. |
| 3379 | * Disadvantage: typeahead is ignored. |
| 3380 | * Translates the interrupt character for unix to ESC. |
| 3381 | */ |
| 3382 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3383 | get_keystroke(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3384 | { |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3385 | char_u *buf = NULL; |
| 3386 | int buflen = 150; |
| 3387 | int maxlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3388 | int len = 0; |
| 3389 | int n; |
| 3390 | int save_mapped_ctrl_c = mapped_ctrl_c; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3391 | int waited = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3392 | |
| 3393 | mapped_ctrl_c = FALSE; /* mappings are not used here */ |
| 3394 | for (;;) |
| 3395 | { |
| 3396 | cursor_on(); |
| 3397 | out_flush(); |
| 3398 | |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3399 | /* Leave some room for check_termcode() to insert a key code into (max |
| 3400 | * 5 chars plus NUL). And fix_input_buffer() can triple the number of |
| 3401 | * bytes. */ |
| 3402 | maxlen = (buflen - 6 - len) / 3; |
| 3403 | if (buf == NULL) |
| 3404 | buf = alloc(buflen); |
| 3405 | else if (maxlen < 10) |
| 3406 | { |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 3407 | char_u *t_buf = buf; |
| 3408 | |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 3409 | /* Need some more space. This might happen when receiving a long |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3410 | * escape sequence. */ |
| 3411 | buflen += 100; |
| 3412 | buf = vim_realloc(buf, buflen); |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 3413 | if (buf == NULL) |
| 3414 | vim_free(t_buf); |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3415 | maxlen = (buflen - 6 - len) / 3; |
| 3416 | } |
| 3417 | if (buf == NULL) |
| 3418 | { |
| 3419 | do_outofmem_msg((long_u)buflen); |
| 3420 | return ESC; /* panic! */ |
| 3421 | } |
| 3422 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3423 | /* First time: blocking wait. Second time: wait up to 100ms for a |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3424 | * terminal code to complete. */ |
| 3425 | n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3426 | if (n > 0) |
| 3427 | { |
| 3428 | /* Replace zero and CSI by a special key code. */ |
Bram Moolenaar | 6bff02e | 2016-08-16 22:50:55 +0200 | [diff] [blame] | 3429 | n = fix_input_buffer(buf + len, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3430 | len += n; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3431 | waited = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3432 | } |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3433 | else if (len > 0) |
| 3434 | ++waited; /* keep track of the waiting time */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3435 | |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3436 | /* Incomplete termcode and not timed out yet: get more characters */ |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3437 | if ((n = check_termcode(1, buf, buflen, &len)) < 0 |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3438 | && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3439 | continue; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3440 | |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3441 | if (n == KEYLEN_REMOVED) /* key code removed */ |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3442 | { |
Bram Moolenaar | fd30cd4 | 2011-03-22 13:07:26 +0100 | [diff] [blame] | 3443 | if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0) |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3444 | { |
| 3445 | /* Redrawing was postponed, do it now. */ |
| 3446 | update_screen(0); |
| 3447 | setcursor(); /* put cursor back where it belongs */ |
| 3448 | } |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3449 | continue; |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3450 | } |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3451 | if (n > 0) /* found a termcode: adjust length */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3452 | len = n; |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3453 | if (len == 0) /* nothing typed yet */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3454 | continue; |
| 3455 | |
| 3456 | /* Handle modifier and/or special key code. */ |
| 3457 | n = buf[0]; |
| 3458 | if (n == K_SPECIAL) |
| 3459 | { |
| 3460 | n = TO_SPECIAL(buf[1], buf[2]); |
| 3461 | if (buf[1] == KS_MODIFIER |
| 3462 | || n == K_IGNORE |
Bram Moolenaar | a5be25e | 2013-03-16 21:35:33 +0100 | [diff] [blame] | 3463 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3464 | || (is_mouse_key(n) && n != K_LEFTMOUSE) |
Bram Moolenaar | a5be25e | 2013-03-16 21:35:33 +0100 | [diff] [blame] | 3465 | #endif |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3466 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3467 | || n == K_VER_SCROLLBAR |
| 3468 | || n == K_HOR_SCROLLBAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3469 | #endif |
| 3470 | ) |
| 3471 | { |
| 3472 | if (buf[1] == KS_MODIFIER) |
| 3473 | mod_mask = buf[2]; |
| 3474 | len -= 3; |
| 3475 | if (len > 0) |
| 3476 | mch_memmove(buf, buf + 3, (size_t)len); |
| 3477 | continue; |
| 3478 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 3479 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3480 | } |
| 3481 | #ifdef FEAT_MBYTE |
| 3482 | if (has_mbyte) |
| 3483 | { |
| 3484 | if (MB_BYTE2LEN(n) > len) |
| 3485 | continue; /* more bytes to get */ |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3486 | buf[len >= buflen ? buflen - 1 : len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3487 | n = (*mb_ptr2char)(buf); |
| 3488 | } |
| 3489 | #endif |
| 3490 | #ifdef UNIX |
| 3491 | if (n == intr_char) |
| 3492 | n = ESC; |
| 3493 | #endif |
| 3494 | break; |
| 3495 | } |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3496 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3497 | |
| 3498 | mapped_ctrl_c = save_mapped_ctrl_c; |
| 3499 | return n; |
| 3500 | } |
| 3501 | |
| 3502 | /* |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3503 | * Get a number from the user. |
| 3504 | * When "mouse_used" is not NULL allow using the mouse. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3505 | */ |
| 3506 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3507 | get_number( |
| 3508 | int colon, /* allow colon to abort */ |
| 3509 | int *mouse_used) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3510 | { |
| 3511 | int n = 0; |
| 3512 | int c; |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3513 | int typed = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3514 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3515 | if (mouse_used != NULL) |
| 3516 | *mouse_used = FALSE; |
| 3517 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3518 | /* When not printing messages, the user won't know what to type, return a |
| 3519 | * zero (as if CR was hit). */ |
| 3520 | if (msg_silent != 0) |
| 3521 | return 0; |
| 3522 | |
| 3523 | #ifdef USE_ON_FLY_SCROLL |
| 3524 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 3525 | #endif |
| 3526 | ++no_mapping; |
| 3527 | ++allow_keys; /* no mapping here, but recognize keys */ |
| 3528 | for (;;) |
| 3529 | { |
| 3530 | windgoto(msg_row, msg_col); |
| 3531 | c = safe_vgetc(); |
| 3532 | if (VIM_ISDIGIT(c)) |
| 3533 | { |
| 3534 | n = n * 10 + c - '0'; |
| 3535 | msg_putchar(c); |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3536 | ++typed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3537 | } |
| 3538 | else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H) |
| 3539 | { |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3540 | if (typed > 0) |
| 3541 | { |
| 3542 | MSG_PUTS("\b \b"); |
| 3543 | --typed; |
| 3544 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3545 | n /= 10; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3546 | } |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3547 | #ifdef FEAT_MOUSE |
| 3548 | else if (mouse_used != NULL && c == K_LEFTMOUSE) |
| 3549 | { |
| 3550 | *mouse_used = TRUE; |
| 3551 | n = mouse_row + 1; |
| 3552 | break; |
| 3553 | } |
| 3554 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3555 | else if (n == 0 && c == ':' && colon) |
| 3556 | { |
| 3557 | stuffcharReadbuff(':'); |
| 3558 | if (!exmode_active) |
| 3559 | cmdline_row = msg_row; |
| 3560 | skip_redraw = TRUE; /* skip redraw once */ |
| 3561 | do_redraw = FALSE; |
| 3562 | break; |
| 3563 | } |
| 3564 | else if (c == CAR || c == NL || c == Ctrl_C || c == ESC) |
| 3565 | break; |
| 3566 | } |
| 3567 | --no_mapping; |
| 3568 | --allow_keys; |
| 3569 | return n; |
| 3570 | } |
| 3571 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3572 | /* |
| 3573 | * Ask the user to enter a number. |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3574 | * When "mouse_used" is not NULL allow using the mouse and in that case return |
| 3575 | * the line number. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3576 | */ |
| 3577 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3578 | prompt_for_number(int *mouse_used) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3579 | { |
| 3580 | int i; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3581 | int save_cmdline_row; |
| 3582 | int save_State; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3583 | |
| 3584 | /* When using ":silent" assume that <CR> was entered. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3585 | if (mouse_used != NULL) |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 3586 | MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): ")); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3587 | else |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 3588 | MSG_PUTS(_("Type number and <Enter> (empty cancels): ")); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3589 | |
Bram Moolenaar | 203335e | 2006-09-03 14:35:42 +0000 | [diff] [blame] | 3590 | /* Set the state such that text can be selected/copied/pasted and we still |
| 3591 | * get mouse events. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3592 | save_cmdline_row = cmdline_row; |
Bram Moolenaar | 203335e | 2006-09-03 14:35:42 +0000 | [diff] [blame] | 3593 | cmdline_row = 0; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3594 | save_State = State; |
Bram Moolenaar | 203335e | 2006-09-03 14:35:42 +0000 | [diff] [blame] | 3595 | State = CMDLINE; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3596 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3597 | i = get_number(TRUE, mouse_used); |
| 3598 | if (KeyTyped) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3599 | { |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3600 | /* don't call wait_return() now */ |
| 3601 | /* msg_putchar('\n'); */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3602 | cmdline_row = msg_row - 1; |
| 3603 | need_wait_return = FALSE; |
| 3604 | msg_didany = FALSE; |
Bram Moolenaar | b245016 | 2009-07-22 09:04:20 +0000 | [diff] [blame] | 3605 | msg_didout = FALSE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3606 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3607 | else |
| 3608 | cmdline_row = save_cmdline_row; |
| 3609 | State = save_State; |
| 3610 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3611 | return i; |
| 3612 | } |
| 3613 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3614 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3615 | msgmore(long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3616 | { |
| 3617 | long pn; |
| 3618 | |
| 3619 | if (global_busy /* no messages now, wait until global is finished */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3620 | || !messaging()) /* 'lazyredraw' set, don't do messages now */ |
| 3621 | return; |
| 3622 | |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 3623 | /* We don't want to overwrite another important message, but do overwrite |
| 3624 | * a previous "more lines" or "fewer lines" message, so that "5dd" and |
| 3625 | * then "put" reports the last action. */ |
| 3626 | if (keep_msg != NULL && !keep_msg_more) |
| 3627 | return; |
| 3628 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3629 | if (n > 0) |
| 3630 | pn = n; |
| 3631 | else |
| 3632 | pn = -n; |
| 3633 | |
| 3634 | if (pn > p_report) |
| 3635 | { |
| 3636 | if (pn == 1) |
| 3637 | { |
| 3638 | if (n > 0) |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3639 | vim_strncpy(msg_buf, (char_u *)_("1 more line"), |
| 3640 | MSG_BUF_LEN - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3641 | else |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3642 | vim_strncpy(msg_buf, (char_u *)_("1 line less"), |
| 3643 | MSG_BUF_LEN - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3644 | } |
| 3645 | else |
| 3646 | { |
| 3647 | if (n > 0) |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3648 | vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
| 3649 | _("%ld more lines"), pn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3650 | else |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3651 | vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
| 3652 | _("%ld fewer lines"), pn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3653 | } |
| 3654 | if (got_int) |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3655 | vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3656 | if (msg(msg_buf)) |
| 3657 | { |
Bram Moolenaar | 238a564 | 2006-02-21 22:12:05 +0000 | [diff] [blame] | 3658 | set_keep_msg(msg_buf, 0); |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 3659 | keep_msg_more = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3660 | } |
| 3661 | } |
| 3662 | } |
| 3663 | |
| 3664 | /* |
| 3665 | * flush map and typeahead buffers and give a warning for an error |
| 3666 | */ |
| 3667 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3668 | beep_flush(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3669 | { |
| 3670 | if (emsg_silent == 0) |
| 3671 | { |
| 3672 | flush_buffers(FALSE); |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3673 | vim_beep(BO_ERROR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3674 | } |
| 3675 | } |
| 3676 | |
| 3677 | /* |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3678 | * Give a warning for an error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3679 | */ |
| 3680 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3681 | vim_beep( |
| 3682 | unsigned val) /* one of the BO_ values, e.g., BO_OPER */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3683 | { |
| 3684 | if (emsg_silent == 0) |
| 3685 | { |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3686 | if (!((bo_flags & val) || (bo_flags & BO_ALL))) |
| 3687 | { |
| 3688 | if (p_vb |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3689 | #ifdef FEAT_GUI |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3690 | /* While the GUI is starting up the termcap is set for the |
| 3691 | * GUI but the output still goes to a terminal. */ |
| 3692 | && !(gui.in_use && gui.starting) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3693 | #endif |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3694 | ) |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3695 | out_str(T_VB); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3696 | else |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3697 | out_char(BELL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3698 | } |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3699 | |
| 3700 | /* When 'verbose' is set and we are sourcing a script or executing a |
| 3701 | * function give the user a hint where the beep comes from. */ |
| 3702 | if (vim_strchr(p_debug, 'e') != NULL) |
| 3703 | { |
| 3704 | msg_source(hl_attr(HLF_W)); |
| 3705 | msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W)); |
| 3706 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3707 | } |
| 3708 | } |
| 3709 | |
| 3710 | /* |
| 3711 | * To get the "real" home directory: |
| 3712 | * - get value of $HOME |
| 3713 | * For Unix: |
| 3714 | * - go to that directory |
| 3715 | * - do mch_dirname() to get the real name of that directory. |
| 3716 | * This also works with mounts and links. |
| 3717 | * Don't do this for MS-DOS, it will change the "current dir" for a drive. |
| 3718 | */ |
| 3719 | static char_u *homedir = NULL; |
| 3720 | |
| 3721 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3722 | init_homedir(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3723 | { |
| 3724 | char_u *var; |
| 3725 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3726 | /* In case we are called a second time (when 'encoding' changes). */ |
| 3727 | vim_free(homedir); |
| 3728 | homedir = NULL; |
| 3729 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3730 | #ifdef VMS |
| 3731 | var = mch_getenv((char_u *)"SYS$LOGIN"); |
| 3732 | #else |
| 3733 | var = mch_getenv((char_u *)"HOME"); |
| 3734 | #endif |
| 3735 | |
| 3736 | if (var != NULL && *var == NUL) /* empty is same as not set */ |
| 3737 | var = NULL; |
| 3738 | |
| 3739 | #ifdef WIN3264 |
| 3740 | /* |
| 3741 | * Weird but true: $HOME may contain an indirect reference to another |
| 3742 | * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set |
| 3743 | * when $HOME is being set. |
| 3744 | */ |
| 3745 | if (var != NULL && *var == '%') |
| 3746 | { |
| 3747 | char_u *p; |
| 3748 | char_u *exp; |
| 3749 | |
| 3750 | p = vim_strchr(var + 1, '%'); |
| 3751 | if (p != NULL) |
| 3752 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3753 | vim_strncpy(NameBuff, var + 1, p - (var + 1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3754 | exp = mch_getenv(NameBuff); |
| 3755 | if (exp != NULL && *exp != NUL |
| 3756 | && STRLEN(exp) + STRLEN(p) < MAXPATHL) |
| 3757 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 3758 | vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3759 | var = NameBuff; |
| 3760 | /* Also set $HOME, it's needed for _viminfo. */ |
| 3761 | vim_setenv((char_u *)"HOME", NameBuff); |
| 3762 | } |
| 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | /* |
| 3767 | * Typically, $HOME is not defined on Windows, unless the user has |
| 3768 | * specifically defined it for Vim's sake. However, on Windows NT |
| 3769 | * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for |
| 3770 | * each user. Try constructing $HOME from these. |
| 3771 | */ |
| 3772 | if (var == NULL) |
| 3773 | { |
| 3774 | char_u *homedrive, *homepath; |
| 3775 | |
| 3776 | homedrive = mch_getenv((char_u *)"HOMEDRIVE"); |
| 3777 | homepath = mch_getenv((char_u *)"HOMEPATH"); |
Bram Moolenaar | 6f97701 | 2010-01-06 17:53:38 +0100 | [diff] [blame] | 3778 | if (homepath == NULL || *homepath == NUL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3779 | homepath = (char_u *)"\\"; |
Bram Moolenaar | 6f97701 | 2010-01-06 17:53:38 +0100 | [diff] [blame] | 3780 | if (homedrive != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3781 | && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL) |
| 3782 | { |
| 3783 | sprintf((char *)NameBuff, "%s%s", homedrive, homepath); |
| 3784 | if (NameBuff[0] != NUL) |
| 3785 | { |
| 3786 | var = NameBuff; |
| 3787 | /* Also set $HOME, it's needed for _viminfo. */ |
| 3788 | vim_setenv((char_u *)"HOME", NameBuff); |
| 3789 | } |
| 3790 | } |
| 3791 | } |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3792 | |
| 3793 | # if defined(FEAT_MBYTE) |
| 3794 | if (enc_utf8 && var != NULL) |
| 3795 | { |
| 3796 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 3797 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3798 | |
| 3799 | /* Convert from active codepage to UTF-8. Other conversions are |
| 3800 | * not done, because they would fail for non-ASCII characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3801 | acp_to_enc(var, (int)STRLEN(var), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3802 | if (pp != NULL) |
| 3803 | { |
| 3804 | homedir = pp; |
| 3805 | return; |
| 3806 | } |
| 3807 | } |
| 3808 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3809 | #endif |
| 3810 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 3811 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3812 | /* |
| 3813 | * Default home dir is C:/ |
| 3814 | * Best assumption we can make in such a situation. |
| 3815 | */ |
| 3816 | if (var == NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3817 | var = (char_u *)"C:/"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3818 | #endif |
| 3819 | if (var != NULL) |
| 3820 | { |
| 3821 | #ifdef UNIX |
| 3822 | /* |
| 3823 | * Change to the directory and get the actual path. This resolves |
| 3824 | * links. Don't do it when we can't return. |
| 3825 | */ |
| 3826 | if (mch_dirname(NameBuff, MAXPATHL) == OK |
| 3827 | && mch_chdir((char *)NameBuff) == 0) |
| 3828 | { |
| 3829 | if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK) |
| 3830 | var = IObuff; |
| 3831 | if (mch_chdir((char *)NameBuff) != 0) |
| 3832 | EMSG(_(e_prev_dir)); |
| 3833 | } |
| 3834 | #endif |
| 3835 | homedir = vim_strsave(var); |
| 3836 | } |
| 3837 | } |
| 3838 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3839 | #if defined(EXITFREE) || defined(PROTO) |
| 3840 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3841 | free_homedir(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3842 | { |
| 3843 | vim_free(homedir); |
| 3844 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 3845 | |
| 3846 | # ifdef FEAT_CMDL_COMPL |
| 3847 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3848 | free_users(void) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 3849 | { |
| 3850 | ga_clear_strings(&ga_users); |
| 3851 | } |
| 3852 | # endif |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3853 | #endif |
| 3854 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3855 | /* |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3856 | * Call expand_env() and store the result in an allocated string. |
| 3857 | * This is not very memory efficient, this expects the result to be freed |
| 3858 | * again soon. |
| 3859 | */ |
| 3860 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3861 | expand_env_save(char_u *src) |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3862 | { |
| 3863 | return expand_env_save_opt(src, FALSE); |
| 3864 | } |
| 3865 | |
| 3866 | /* |
| 3867 | * Idem, but when "one" is TRUE handle the string as one file name, only |
| 3868 | * expand "~" at the start. |
| 3869 | */ |
| 3870 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3871 | expand_env_save_opt(char_u *src, int one) |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3872 | { |
| 3873 | char_u *p; |
| 3874 | |
| 3875 | p = alloc(MAXPATHL); |
| 3876 | if (p != NULL) |
| 3877 | expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL); |
| 3878 | return p; |
| 3879 | } |
| 3880 | |
| 3881 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3882 | * Expand environment variable with path name. |
| 3883 | * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded. |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3884 | * Skips over "\ ", "\~" and "\$" (not for Win32 though). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3885 | * If anything fails no expansion is done and dst equals src. |
| 3886 | */ |
| 3887 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3888 | expand_env( |
| 3889 | char_u *src, /* input string e.g. "$HOME/vim.hlp" */ |
| 3890 | char_u *dst, /* where to put the result */ |
| 3891 | int dstlen) /* maximum length of the result */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3892 | { |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3893 | expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3897 | expand_env_esc( |
| 3898 | char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */ |
| 3899 | char_u *dst, /* where to put the result */ |
| 3900 | int dstlen, /* maximum length of the result */ |
| 3901 | int esc, /* escape spaces in expanded variables */ |
| 3902 | int one, /* "srcp" is one file name */ |
| 3903 | char_u *startstr) /* start again after this (can be NULL) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3904 | { |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3905 | char_u *src; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3906 | char_u *tail; |
| 3907 | int c; |
| 3908 | char_u *var; |
| 3909 | int copy_char; |
| 3910 | int mustfree; /* var was allocated, need to free it later */ |
| 3911 | int at_start = TRUE; /* at start of a name */ |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3912 | int startstr_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3913 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3914 | if (startstr != NULL) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3915 | startstr_len = (int)STRLEN(startstr); |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3916 | |
| 3917 | src = skipwhite(srcp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3918 | --dstlen; /* leave one char space for "\," */ |
| 3919 | while (*src && dstlen > 0) |
| 3920 | { |
Bram Moolenaar | be83b73 | 2015-08-25 14:21:19 +0200 | [diff] [blame] | 3921 | #ifdef FEAT_EVAL |
| 3922 | /* Skip over `=expr`. */ |
| 3923 | if (src[0] == '`' && src[1] == '=') |
| 3924 | { |
| 3925 | size_t len; |
| 3926 | |
| 3927 | var = src; |
| 3928 | src += 2; |
| 3929 | (void)skip_expr(&src); |
| 3930 | if (*src == '`') |
| 3931 | ++src; |
| 3932 | len = src - var; |
| 3933 | if (len > (size_t)dstlen) |
| 3934 | len = dstlen; |
| 3935 | vim_strncpy(dst, var, len); |
| 3936 | dst += len; |
Bram Moolenaar | 5df1ed2 | 2015-09-01 16:25:34 +0200 | [diff] [blame] | 3937 | dstlen -= (int)len; |
Bram Moolenaar | be83b73 | 2015-08-25 14:21:19 +0200 | [diff] [blame] | 3938 | continue; |
| 3939 | } |
| 3940 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3941 | copy_char = TRUE; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 3942 | if ((*src == '$' |
| 3943 | #ifdef VMS |
| 3944 | && at_start |
| 3945 | #endif |
| 3946 | ) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 3947 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3948 | || *src == '%' |
| 3949 | #endif |
| 3950 | || (*src == '~' && at_start)) |
| 3951 | { |
| 3952 | mustfree = FALSE; |
| 3953 | |
| 3954 | /* |
| 3955 | * The variable name is copied into dst temporarily, because it may |
| 3956 | * be a string in read-only memory and a NUL needs to be appended. |
| 3957 | */ |
| 3958 | if (*src != '~') /* environment var */ |
| 3959 | { |
| 3960 | tail = src + 1; |
| 3961 | var = dst; |
| 3962 | c = dstlen - 1; |
| 3963 | |
| 3964 | #ifdef UNIX |
| 3965 | /* Unix has ${var-name} type environment vars */ |
| 3966 | if (*tail == '{' && !vim_isIDc('{')) |
| 3967 | { |
| 3968 | tail++; /* ignore '{' */ |
| 3969 | while (c-- > 0 && *tail && *tail != '}') |
| 3970 | *var++ = *tail++; |
| 3971 | } |
| 3972 | else |
| 3973 | #endif |
| 3974 | { |
| 3975 | while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail)) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 3976 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3977 | || (*src == '%' && *tail != '%') |
| 3978 | #endif |
| 3979 | )) |
| 3980 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3981 | *var++ = *tail++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3982 | } |
| 3983 | } |
| 3984 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 3985 | #if defined(MSWIN) || defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3986 | # ifdef UNIX |
| 3987 | if (src[1] == '{' && *tail != '}') |
| 3988 | # else |
| 3989 | if (*src == '%' && *tail != '%') |
| 3990 | # endif |
| 3991 | var = NULL; |
| 3992 | else |
| 3993 | { |
| 3994 | # ifdef UNIX |
| 3995 | if (src[1] == '{') |
| 3996 | # else |
| 3997 | if (*src == '%') |
| 3998 | #endif |
| 3999 | ++tail; |
| 4000 | #endif |
| 4001 | *var = NUL; |
| 4002 | var = vim_getenv(dst, &mustfree); |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4003 | #if defined(MSWIN) || defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4004 | } |
| 4005 | #endif |
| 4006 | } |
| 4007 | /* home directory */ |
| 4008 | else if ( src[1] == NUL |
| 4009 | || vim_ispathsep(src[1]) |
| 4010 | || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL) |
| 4011 | { |
| 4012 | var = homedir; |
| 4013 | tail = src + 1; |
| 4014 | } |
| 4015 | else /* user directory */ |
| 4016 | { |
| 4017 | #if defined(UNIX) || (defined(VMS) && defined(USER_HOME)) |
| 4018 | /* |
| 4019 | * Copy ~user to dst[], so we can put a NUL after it. |
| 4020 | */ |
| 4021 | tail = src; |
| 4022 | var = dst; |
| 4023 | c = dstlen - 1; |
| 4024 | while ( c-- > 0 |
| 4025 | && *tail |
| 4026 | && vim_isfilec(*tail) |
| 4027 | && !vim_ispathsep(*tail)) |
| 4028 | *var++ = *tail++; |
| 4029 | *var = NUL; |
| 4030 | # ifdef UNIX |
| 4031 | /* |
| 4032 | * If the system supports getpwnam(), use it. |
| 4033 | * Otherwise, or if getpwnam() fails, the shell is used to |
| 4034 | * expand ~user. This is slower and may fail if the shell |
| 4035 | * does not support ~user (old versions of /bin/sh). |
| 4036 | */ |
| 4037 | # if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H) |
| 4038 | { |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 4039 | /* Note: memory allocated by getpwnam() is never freed. |
| 4040 | * Calling endpwent() apparently doesn't help. */ |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 4041 | struct passwd *pw = (*dst == NUL) |
| 4042 | ? NULL : getpwnam((char *)dst + 1); |
| 4043 | |
| 4044 | var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4045 | } |
| 4046 | if (var == NULL) |
| 4047 | # endif |
| 4048 | { |
| 4049 | expand_T xpc; |
| 4050 | |
| 4051 | ExpandInit(&xpc); |
| 4052 | xpc.xp_context = EXPAND_FILES; |
| 4053 | var = ExpandOne(&xpc, dst, NULL, |
| 4054 | WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4055 | mustfree = TRUE; |
| 4056 | } |
| 4057 | |
| 4058 | # else /* !UNIX, thus VMS */ |
| 4059 | /* |
| 4060 | * USER_HOME is a comma-separated list of |
| 4061 | * directories to search for the user account in. |
| 4062 | */ |
| 4063 | { |
| 4064 | char_u test[MAXPATHL], paths[MAXPATHL]; |
| 4065 | char_u *path, *next_path, *ptr; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4066 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4067 | |
| 4068 | STRCPY(paths, USER_HOME); |
| 4069 | next_path = paths; |
| 4070 | while (*next_path) |
| 4071 | { |
| 4072 | for (path = next_path; *next_path && *next_path != ','; |
| 4073 | next_path++); |
| 4074 | if (*next_path) |
| 4075 | *next_path++ = NUL; |
| 4076 | STRCPY(test, path); |
| 4077 | STRCAT(test, "/"); |
| 4078 | STRCAT(test, dst + 1); |
| 4079 | if (mch_stat(test, &st) == 0) |
| 4080 | { |
| 4081 | var = alloc(STRLEN(test) + 1); |
| 4082 | STRCPY(var, test); |
| 4083 | mustfree = TRUE; |
| 4084 | break; |
| 4085 | } |
| 4086 | } |
| 4087 | } |
| 4088 | # endif /* UNIX */ |
| 4089 | #else |
| 4090 | /* cannot expand user's home directory, so don't try */ |
| 4091 | var = NULL; |
| 4092 | tail = (char_u *)""; /* for gcc */ |
| 4093 | #endif /* UNIX || VMS */ |
| 4094 | } |
| 4095 | |
| 4096 | #ifdef BACKSLASH_IN_FILENAME |
| 4097 | /* If 'shellslash' is set change backslashes to forward slashes. |
| 4098 | * Can't use slash_adjust(), p_ssl may be set temporarily. */ |
| 4099 | if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL) |
| 4100 | { |
| 4101 | char_u *p = vim_strsave(var); |
| 4102 | |
| 4103 | if (p != NULL) |
| 4104 | { |
| 4105 | if (mustfree) |
| 4106 | vim_free(var); |
| 4107 | var = p; |
| 4108 | mustfree = TRUE; |
| 4109 | forward_slash(var); |
| 4110 | } |
| 4111 | } |
| 4112 | #endif |
| 4113 | |
| 4114 | /* If "var" contains white space, escape it with a backslash. |
| 4115 | * Required for ":e ~/tt" when $HOME includes a space. */ |
| 4116 | if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) |
| 4117 | { |
| 4118 | char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); |
| 4119 | |
| 4120 | if (p != NULL) |
| 4121 | { |
| 4122 | if (mustfree) |
| 4123 | vim_free(var); |
| 4124 | var = p; |
| 4125 | mustfree = TRUE; |
| 4126 | } |
| 4127 | } |
| 4128 | |
| 4129 | if (var != NULL && *var != NUL |
| 4130 | && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) |
| 4131 | { |
| 4132 | STRCPY(dst, var); |
| 4133 | dstlen -= (int)STRLEN(var); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4134 | c = (int)STRLEN(var); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4135 | /* if var[] ends in a path separator and tail[] starts |
| 4136 | * with it, skip a character */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4137 | if (*var != NUL && after_pathsep(dst, dst + c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4138 | #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) |
| 4139 | && dst[-1] != ':' |
| 4140 | #endif |
| 4141 | && vim_ispathsep(*tail)) |
| 4142 | ++tail; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4143 | dst += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4144 | src = tail; |
| 4145 | copy_char = FALSE; |
| 4146 | } |
| 4147 | if (mustfree) |
| 4148 | vim_free(var); |
| 4149 | } |
| 4150 | |
| 4151 | if (copy_char) /* copy at least one char */ |
| 4152 | { |
| 4153 | /* |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 4154 | * Recognize the start of a new name, for '~'. |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4155 | * Don't do this when "one" is TRUE, to avoid expanding "~" in |
| 4156 | * ":edit foo ~ foo". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4157 | */ |
| 4158 | at_start = FALSE; |
| 4159 | if (src[0] == '\\' && src[1] != NUL) |
| 4160 | { |
| 4161 | *dst++ = *src++; |
| 4162 | --dstlen; |
| 4163 | } |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4164 | else if ((src[0] == ' ' || src[0] == ',') && !one) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4165 | at_start = TRUE; |
| 4166 | *dst++ = *src++; |
| 4167 | --dstlen; |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4168 | |
| 4169 | if (startstr != NULL && src - startstr_len >= srcp |
| 4170 | && STRNCMP(src - startstr_len, startstr, startstr_len) == 0) |
| 4171 | at_start = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4172 | } |
| 4173 | } |
| 4174 | *dst = NUL; |
| 4175 | } |
| 4176 | |
| 4177 | /* |
| 4178 | * Vim's version of getenv(). |
| 4179 | * Special handling of $HOME, $VIM and $VIMRUNTIME. |
Bram Moolenaar | 2f6b0b8 | 2005-03-08 22:43:10 +0000 | [diff] [blame] | 4180 | * Also does ACP to 'enc' conversion for Win32. |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4181 | * "mustfree" is set to TRUE when returned is allocated, it must be |
| 4182 | * initialized to FALSE by the caller. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4183 | */ |
| 4184 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4185 | vim_getenv(char_u *name, int *mustfree) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4186 | { |
| 4187 | char_u *p; |
| 4188 | char_u *pend; |
| 4189 | int vimruntime; |
| 4190 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4191 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4192 | /* use "C:/" when $HOME is not set */ |
| 4193 | if (STRCMP(name, "HOME") == 0) |
| 4194 | return homedir; |
| 4195 | #endif |
| 4196 | |
| 4197 | p = mch_getenv(name); |
| 4198 | if (p != NULL && *p == NUL) /* empty is the same as not set */ |
| 4199 | p = NULL; |
| 4200 | |
| 4201 | if (p != NULL) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4202 | { |
| 4203 | #if defined(FEAT_MBYTE) && defined(WIN3264) |
| 4204 | if (enc_utf8) |
| 4205 | { |
| 4206 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4207 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4208 | |
| 4209 | /* Convert from active codepage to UTF-8. Other conversions are |
| 4210 | * not done, because they would fail for non-ASCII characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4211 | acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4212 | if (pp != NULL) |
| 4213 | { |
| 4214 | p = pp; |
| 4215 | *mustfree = TRUE; |
| 4216 | } |
| 4217 | } |
| 4218 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4219 | return p; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4220 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4221 | |
| 4222 | vimruntime = (STRCMP(name, "VIMRUNTIME") == 0); |
| 4223 | if (!vimruntime && STRCMP(name, "VIM") != 0) |
| 4224 | return NULL; |
| 4225 | |
| 4226 | /* |
| 4227 | * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM. |
| 4228 | * Don't do this when default_vimruntime_dir is non-empty. |
| 4229 | */ |
| 4230 | if (vimruntime |
| 4231 | #ifdef HAVE_PATHDEF |
| 4232 | && *default_vimruntime_dir == NUL |
| 4233 | #endif |
| 4234 | ) |
| 4235 | { |
| 4236 | p = mch_getenv((char_u *)"VIM"); |
| 4237 | if (p != NULL && *p == NUL) /* empty is the same as not set */ |
| 4238 | p = NULL; |
| 4239 | if (p != NULL) |
| 4240 | { |
| 4241 | p = vim_version_dir(p); |
| 4242 | if (p != NULL) |
| 4243 | *mustfree = TRUE; |
| 4244 | else |
| 4245 | p = mch_getenv((char_u *)"VIM"); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4246 | |
| 4247 | #if defined(FEAT_MBYTE) && defined(WIN3264) |
| 4248 | if (enc_utf8) |
| 4249 | { |
| 4250 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4251 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4252 | |
| 4253 | /* Convert from active codepage to UTF-8. Other conversions |
| 4254 | * are not done, because they would fail for non-ASCII |
| 4255 | * characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4256 | acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4257 | if (pp != NULL) |
| 4258 | { |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4259 | if (*mustfree) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4260 | vim_free(p); |
| 4261 | p = pp; |
| 4262 | *mustfree = TRUE; |
| 4263 | } |
| 4264 | } |
| 4265 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4266 | } |
| 4267 | } |
| 4268 | |
| 4269 | /* |
| 4270 | * When expanding $VIM or $VIMRUNTIME fails, try using: |
| 4271 | * - the directory name from 'helpfile' (unless it contains '$') |
| 4272 | * - the executable name from argv[0] |
| 4273 | */ |
| 4274 | if (p == NULL) |
| 4275 | { |
| 4276 | if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL) |
| 4277 | p = p_hf; |
| 4278 | #ifdef USE_EXE_NAME |
| 4279 | /* |
| 4280 | * Use the name of the executable, obtained from argv[0]. |
| 4281 | */ |
| 4282 | else |
| 4283 | p = exe_name; |
| 4284 | #endif |
| 4285 | if (p != NULL) |
| 4286 | { |
| 4287 | /* remove the file name */ |
| 4288 | pend = gettail(p); |
| 4289 | |
| 4290 | /* remove "doc/" from 'helpfile', if present */ |
| 4291 | if (p == p_hf) |
| 4292 | pend = remove_tail(p, pend, (char_u *)"doc"); |
| 4293 | |
| 4294 | #ifdef USE_EXE_NAME |
| 4295 | # ifdef MACOS_X |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4296 | /* remove "MacOS" from exe_name and add "Resources/vim" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4297 | if (p == exe_name) |
| 4298 | { |
| 4299 | char_u *pend1; |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4300 | char_u *pnew; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4301 | |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4302 | pend1 = remove_tail(p, pend, (char_u *)"MacOS"); |
| 4303 | if (pend1 != pend) |
| 4304 | { |
| 4305 | pnew = alloc((unsigned)(pend1 - p) + 15); |
| 4306 | if (pnew != NULL) |
| 4307 | { |
| 4308 | STRNCPY(pnew, p, (pend1 - p)); |
| 4309 | STRCPY(pnew + (pend1 - p), "Resources/vim"); |
| 4310 | p = pnew; |
| 4311 | pend = p + STRLEN(p); |
| 4312 | } |
| 4313 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4314 | } |
| 4315 | # endif |
| 4316 | /* remove "src/" from exe_name, if present */ |
| 4317 | if (p == exe_name) |
| 4318 | pend = remove_tail(p, pend, (char_u *)"src"); |
| 4319 | #endif |
| 4320 | |
| 4321 | /* for $VIM, remove "runtime/" or "vim54/", if present */ |
| 4322 | if (!vimruntime) |
| 4323 | { |
| 4324 | pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME); |
| 4325 | pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT); |
| 4326 | } |
| 4327 | |
| 4328 | /* remove trailing path separator */ |
| 4329 | #ifndef MACOS_CLASSIC |
| 4330 | /* With MacOS path (with colons) the final colon is required */ |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 4331 | /* to avoid confusion between absolute and relative path */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4332 | if (pend > p && after_pathsep(p, pend)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4333 | --pend; |
| 4334 | #endif |
| 4335 | |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4336 | #ifdef MACOS_X |
| 4337 | if (p == exe_name || p == p_hf) |
| 4338 | #endif |
| 4339 | /* check that the result is a directory name */ |
| 4340 | p = vim_strnsave(p, (int)(pend - p)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4341 | |
| 4342 | if (p != NULL && !mch_isdir(p)) |
| 4343 | { |
| 4344 | vim_free(p); |
| 4345 | p = NULL; |
| 4346 | } |
| 4347 | else |
| 4348 | { |
| 4349 | #ifdef USE_EXE_NAME |
| 4350 | /* may add "/vim54" or "/runtime" if it exists */ |
| 4351 | if (vimruntime && (pend = vim_version_dir(p)) != NULL) |
| 4352 | { |
| 4353 | vim_free(p); |
| 4354 | p = pend; |
| 4355 | } |
| 4356 | #endif |
| 4357 | *mustfree = TRUE; |
| 4358 | } |
| 4359 | } |
| 4360 | } |
| 4361 | |
| 4362 | #ifdef HAVE_PATHDEF |
| 4363 | /* When there is a pathdef.c file we can use default_vim_dir and |
| 4364 | * default_vimruntime_dir */ |
| 4365 | if (p == NULL) |
| 4366 | { |
| 4367 | /* Only use default_vimruntime_dir when it is not empty */ |
| 4368 | if (vimruntime && *default_vimruntime_dir != NUL) |
| 4369 | { |
| 4370 | p = default_vimruntime_dir; |
| 4371 | *mustfree = FALSE; |
| 4372 | } |
| 4373 | else if (*default_vim_dir != NUL) |
| 4374 | { |
| 4375 | if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) |
| 4376 | *mustfree = TRUE; |
| 4377 | else |
| 4378 | { |
| 4379 | p = default_vim_dir; |
| 4380 | *mustfree = FALSE; |
| 4381 | } |
| 4382 | } |
| 4383 | } |
| 4384 | #endif |
| 4385 | |
| 4386 | /* |
| 4387 | * Set the environment variable, so that the new value can be found fast |
| 4388 | * next time, and others can also use it (e.g. Perl). |
| 4389 | */ |
| 4390 | if (p != NULL) |
| 4391 | { |
| 4392 | if (vimruntime) |
| 4393 | { |
| 4394 | vim_setenv((char_u *)"VIMRUNTIME", p); |
| 4395 | didset_vimruntime = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4396 | } |
| 4397 | else |
| 4398 | { |
| 4399 | vim_setenv((char_u *)"VIM", p); |
| 4400 | didset_vim = TRUE; |
| 4401 | } |
| 4402 | } |
| 4403 | return p; |
| 4404 | } |
| 4405 | |
| 4406 | /* |
| 4407 | * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. |
| 4408 | * Return NULL if not, return its name in allocated memory otherwise. |
| 4409 | */ |
| 4410 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4411 | vim_version_dir(char_u *vimdir) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4412 | { |
| 4413 | char_u *p; |
| 4414 | |
| 4415 | if (vimdir == NULL || *vimdir == NUL) |
| 4416 | return NULL; |
| 4417 | p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); |
| 4418 | if (p != NULL && mch_isdir(p)) |
| 4419 | return p; |
| 4420 | vim_free(p); |
| 4421 | p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); |
| 4422 | if (p != NULL && mch_isdir(p)) |
| 4423 | return p; |
| 4424 | vim_free(p); |
| 4425 | return NULL; |
| 4426 | } |
| 4427 | |
| 4428 | /* |
| 4429 | * If the string between "p" and "pend" ends in "name/", return "pend" minus |
| 4430 | * the length of "name/". Otherwise return "pend". |
| 4431 | */ |
| 4432 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4433 | remove_tail(char_u *p, char_u *pend, char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4434 | { |
| 4435 | int len = (int)STRLEN(name) + 1; |
| 4436 | char_u *newend = pend - len; |
| 4437 | |
| 4438 | if (newend >= p |
| 4439 | && fnamencmp(newend, name, len - 1) == 0 |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4440 | && (newend == p || after_pathsep(p, newend))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4441 | return newend; |
| 4442 | return pend; |
| 4443 | } |
| 4444 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4445 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4446 | * Our portable version of setenv. |
| 4447 | */ |
| 4448 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4449 | vim_setenv(char_u *name, char_u *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4450 | { |
| 4451 | #ifdef HAVE_SETENV |
| 4452 | mch_setenv((char *)name, (char *)val, 1); |
| 4453 | #else |
| 4454 | char_u *envbuf; |
| 4455 | |
| 4456 | /* |
| 4457 | * Putenv does not copy the string, it has to remain |
| 4458 | * valid. The allocated memory will never be freed. |
| 4459 | */ |
| 4460 | envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2)); |
| 4461 | if (envbuf != NULL) |
| 4462 | { |
| 4463 | sprintf((char *)envbuf, "%s=%s", name, val); |
| 4464 | putenv((char *)envbuf); |
| 4465 | } |
| 4466 | #endif |
Bram Moolenaar | 011a34d | 2012-02-29 13:49:09 +0100 | [diff] [blame] | 4467 | #ifdef FEAT_GETTEXT |
| 4468 | /* |
| 4469 | * When setting $VIMRUNTIME adjust the directory to find message |
| 4470 | * translations to $VIMRUNTIME/lang. |
| 4471 | */ |
| 4472 | if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) |
| 4473 | { |
| 4474 | char_u *buf = concat_str(val, (char_u *)"/lang"); |
| 4475 | |
| 4476 | if (buf != NULL) |
| 4477 | { |
| 4478 | bindtextdomain(VIMPACKAGE, (char *)buf); |
| 4479 | vim_free(buf); |
| 4480 | } |
| 4481 | } |
| 4482 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4483 | } |
| 4484 | |
| 4485 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 4486 | /* |
| 4487 | * Function given to ExpandGeneric() to obtain an environment variable name. |
| 4488 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4489 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4490 | get_env_name( |
| 4491 | expand_T *xp UNUSED, |
| 4492 | int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4493 | { |
| 4494 | # if defined(AMIGA) || defined(__MRC__) || defined(__SC__) |
| 4495 | /* |
| 4496 | * No environ[] on the Amiga and on the Mac (using MPW). |
| 4497 | */ |
| 4498 | return NULL; |
| 4499 | # else |
| 4500 | # ifndef __WIN32__ |
| 4501 | /* Borland C++ 5.2 has this in a header file. */ |
| 4502 | extern char **environ; |
| 4503 | # endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 4504 | # define ENVNAMELEN 100 |
| 4505 | static char_u name[ENVNAMELEN]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4506 | char_u *str; |
| 4507 | int n; |
| 4508 | |
| 4509 | str = (char_u *)environ[idx]; |
| 4510 | if (str == NULL) |
| 4511 | return NULL; |
| 4512 | |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 4513 | for (n = 0; n < ENVNAMELEN - 1; ++n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4514 | { |
| 4515 | if (str[n] == '=' || str[n] == NUL) |
| 4516 | break; |
| 4517 | name[n] = str[n]; |
| 4518 | } |
| 4519 | name[n] = NUL; |
| 4520 | return name; |
| 4521 | # endif |
| 4522 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4523 | |
| 4524 | /* |
| 4525 | * Find all user names for user completion. |
| 4526 | * Done only once and then cached. |
| 4527 | */ |
| 4528 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4529 | init_users(void) |
Bram Moolenaar | 01b626c | 2013-06-16 22:49:14 +0200 | [diff] [blame] | 4530 | { |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4531 | static int lazy_init_done = FALSE; |
| 4532 | |
| 4533 | if (lazy_init_done) |
| 4534 | return; |
| 4535 | |
| 4536 | lazy_init_done = TRUE; |
| 4537 | ga_init2(&ga_users, sizeof(char_u *), 20); |
| 4538 | |
| 4539 | # if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H) |
| 4540 | { |
| 4541 | char_u* user; |
| 4542 | struct passwd* pw; |
| 4543 | |
| 4544 | setpwent(); |
| 4545 | while ((pw = getpwent()) != NULL) |
| 4546 | /* pw->pw_name shouldn't be NULL but just in case... */ |
| 4547 | if (pw->pw_name != NULL) |
| 4548 | { |
| 4549 | if (ga_grow(&ga_users, 1) == FAIL) |
| 4550 | break; |
| 4551 | user = vim_strsave((char_u*)pw->pw_name); |
| 4552 | if (user == NULL) |
| 4553 | break; |
| 4554 | ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user; |
| 4555 | } |
| 4556 | endpwent(); |
| 4557 | } |
| 4558 | # endif |
| 4559 | } |
| 4560 | |
| 4561 | /* |
| 4562 | * Function given to ExpandGeneric() to obtain an user names. |
| 4563 | */ |
| 4564 | char_u* |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4565 | get_users(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4566 | { |
| 4567 | init_users(); |
| 4568 | if (idx < ga_users.ga_len) |
| 4569 | return ((char_u **)ga_users.ga_data)[idx]; |
| 4570 | return NULL; |
| 4571 | } |
| 4572 | |
| 4573 | /* |
| 4574 | * Check whether name matches a user name. Return: |
| 4575 | * 0 if name does not match any user name. |
| 4576 | * 1 if name partially matches the beginning of a user name. |
| 4577 | * 2 is name fully matches a user name. |
| 4578 | */ |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4579 | int match_user(char_u* name) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4580 | { |
| 4581 | int i; |
| 4582 | int n = (int)STRLEN(name); |
| 4583 | int result = 0; |
| 4584 | |
| 4585 | init_users(); |
| 4586 | for (i = 0; i < ga_users.ga_len; i++) |
| 4587 | { |
| 4588 | if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0) |
| 4589 | return 2; /* full match */ |
| 4590 | if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0) |
| 4591 | result = 1; /* partial match */ |
| 4592 | } |
| 4593 | return result; |
| 4594 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4595 | #endif |
| 4596 | |
| 4597 | /* |
| 4598 | * Replace home directory by "~" in each space or comma separated file name in |
| 4599 | * 'src'. |
| 4600 | * If anything fails (except when out of space) dst equals src. |
| 4601 | */ |
| 4602 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4603 | home_replace( |
| 4604 | buf_T *buf, /* when not NULL, check for help files */ |
| 4605 | char_u *src, /* input file name */ |
| 4606 | char_u *dst, /* where to put the result */ |
| 4607 | int dstlen, /* maximum length of the result */ |
| 4608 | int one) /* if TRUE, only replace one file name, include |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4609 | spaces and commas in the file name. */ |
| 4610 | { |
| 4611 | size_t dirlen = 0, envlen = 0; |
| 4612 | size_t len; |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4613 | char_u *homedir_env, *homedir_env_orig; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4614 | char_u *p; |
| 4615 | |
| 4616 | if (src == NULL) |
| 4617 | { |
| 4618 | *dst = NUL; |
| 4619 | return; |
| 4620 | } |
| 4621 | |
| 4622 | /* |
| 4623 | * If the file is a help file, remove the path completely. |
| 4624 | */ |
| 4625 | if (buf != NULL && buf->b_help) |
| 4626 | { |
| 4627 | STRCPY(dst, gettail(src)); |
| 4628 | return; |
| 4629 | } |
| 4630 | |
| 4631 | /* |
| 4632 | * We check both the value of the $HOME environment variable and the |
| 4633 | * "real" home directory. |
| 4634 | */ |
| 4635 | if (homedir != NULL) |
| 4636 | dirlen = STRLEN(homedir); |
| 4637 | |
| 4638 | #ifdef VMS |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4639 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4640 | #else |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4641 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); |
| 4642 | #endif |
Bram Moolenaar | bef4790 | 2012-07-06 16:49:40 +0200 | [diff] [blame] | 4643 | /* Empty is the same as not set. */ |
| 4644 | if (homedir_env != NULL && *homedir_env == NUL) |
| 4645 | homedir_env = NULL; |
| 4646 | |
Bram Moolenaar | e60c2e5 | 2013-06-05 19:35:38 +0200 | [diff] [blame] | 4647 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) |
Bram Moolenaar | bef4790 | 2012-07-06 16:49:40 +0200 | [diff] [blame] | 4648 | if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL) |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4649 | { |
| 4650 | int usedlen = 0; |
| 4651 | int flen; |
| 4652 | char_u *fbuf = NULL; |
| 4653 | |
| 4654 | flen = (int)STRLEN(homedir_env); |
Bram Moolenaar | d12f811 | 2012-06-20 17:56:09 +0200 | [diff] [blame] | 4655 | (void)modify_fname((char_u *)":p", &usedlen, |
| 4656 | &homedir_env, &fbuf, &flen); |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4657 | flen = (int)STRLEN(homedir_env); |
| 4658 | if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) |
| 4659 | /* Remove the trailing / that is added to a directory. */ |
| 4660 | homedir_env[flen - 1] = NUL; |
| 4661 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4662 | #endif |
| 4663 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4664 | if (homedir_env != NULL) |
| 4665 | envlen = STRLEN(homedir_env); |
| 4666 | |
| 4667 | if (!one) |
| 4668 | src = skipwhite(src); |
| 4669 | while (*src && dstlen > 0) |
| 4670 | { |
| 4671 | /* |
| 4672 | * Here we are at the beginning of a file name. |
| 4673 | * First, check to see if the beginning of the file name matches |
| 4674 | * $HOME or the "real" home directory. Check that there is a '/' |
| 4675 | * after the match (so that if e.g. the file is "/home/pieter/bla", |
| 4676 | * and the home directory is "/home/piet", the file does not end up |
| 4677 | * as "~er/bla" (which would seem to indicate the file "bla" in user |
| 4678 | * er's home directory)). |
| 4679 | */ |
| 4680 | p = homedir; |
| 4681 | len = dirlen; |
| 4682 | for (;;) |
| 4683 | { |
| 4684 | if ( len |
| 4685 | && fnamencmp(src, p, len) == 0 |
| 4686 | && (vim_ispathsep(src[len]) |
| 4687 | || (!one && (src[len] == ',' || src[len] == ' ')) |
| 4688 | || src[len] == NUL)) |
| 4689 | { |
| 4690 | src += len; |
| 4691 | if (--dstlen > 0) |
| 4692 | *dst++ = '~'; |
| 4693 | |
| 4694 | /* |
| 4695 | * If it's just the home directory, add "/". |
| 4696 | */ |
| 4697 | if (!vim_ispathsep(src[0]) && --dstlen > 0) |
| 4698 | *dst++ = '/'; |
| 4699 | break; |
| 4700 | } |
| 4701 | if (p == homedir_env) |
| 4702 | break; |
| 4703 | p = homedir_env; |
| 4704 | len = envlen; |
| 4705 | } |
| 4706 | |
| 4707 | /* if (!one) skip to separator: space or comma */ |
| 4708 | while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) |
| 4709 | *dst++ = *src++; |
| 4710 | /* skip separator */ |
| 4711 | while ((*src == ' ' || *src == ',') && --dstlen > 0) |
| 4712 | *dst++ = *src++; |
| 4713 | } |
| 4714 | /* if (dstlen == 0) out of space, what to do??? */ |
| 4715 | |
| 4716 | *dst = NUL; |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4717 | |
| 4718 | if (homedir_env != homedir_env_orig) |
| 4719 | vim_free(homedir_env); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | /* |
| 4723 | * Like home_replace, store the replaced string in allocated memory. |
| 4724 | * When something fails, NULL is returned. |
| 4725 | */ |
| 4726 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4727 | home_replace_save( |
| 4728 | buf_T *buf, /* when not NULL, check for help files */ |
| 4729 | char_u *src) /* input file name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4730 | { |
| 4731 | char_u *dst; |
| 4732 | unsigned len; |
| 4733 | |
| 4734 | len = 3; /* space for "~/" and trailing NUL */ |
| 4735 | if (src != NULL) /* just in case */ |
| 4736 | len += (unsigned)STRLEN(src); |
| 4737 | dst = alloc(len); |
| 4738 | if (dst != NULL) |
| 4739 | home_replace(buf, src, dst, len, TRUE); |
| 4740 | return dst; |
| 4741 | } |
| 4742 | |
| 4743 | /* |
| 4744 | * Compare two file names and return: |
| 4745 | * FPC_SAME if they both exist and are the same file. |
| 4746 | * FPC_SAMEX if they both don't exist and have the same file name. |
| 4747 | * FPC_DIFF if they both exist and are different files. |
| 4748 | * FPC_NOTX if they both don't exist. |
| 4749 | * FPC_DIFFX if one of them doesn't exist. |
| 4750 | * For the first name environment variables are expanded |
| 4751 | */ |
| 4752 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4753 | fullpathcmp( |
| 4754 | char_u *s1, |
| 4755 | char_u *s2, |
| 4756 | int checkname) /* when both don't exist, check file names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4757 | { |
| 4758 | #ifdef UNIX |
| 4759 | char_u exp1[MAXPATHL]; |
| 4760 | char_u full1[MAXPATHL]; |
| 4761 | char_u full2[MAXPATHL]; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4762 | stat_T st1, st2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4763 | int r1, r2; |
| 4764 | |
| 4765 | expand_env(s1, exp1, MAXPATHL); |
| 4766 | r1 = mch_stat((char *)exp1, &st1); |
| 4767 | r2 = mch_stat((char *)s2, &st2); |
| 4768 | if (r1 != 0 && r2 != 0) |
| 4769 | { |
| 4770 | /* if mch_stat() doesn't work, may compare the names */ |
| 4771 | if (checkname) |
| 4772 | { |
| 4773 | if (fnamecmp(exp1, s2) == 0) |
| 4774 | return FPC_SAMEX; |
| 4775 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 4776 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 4777 | if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) |
| 4778 | return FPC_SAMEX; |
| 4779 | } |
| 4780 | return FPC_NOTX; |
| 4781 | } |
| 4782 | if (r1 != 0 || r2 != 0) |
| 4783 | return FPC_DIFFX; |
| 4784 | if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) |
| 4785 | return FPC_SAME; |
| 4786 | return FPC_DIFF; |
| 4787 | #else |
| 4788 | char_u *exp1; /* expanded s1 */ |
| 4789 | char_u *full1; /* full path of s1 */ |
| 4790 | char_u *full2; /* full path of s2 */ |
| 4791 | int retval = FPC_DIFF; |
| 4792 | int r1, r2; |
| 4793 | |
| 4794 | /* allocate one buffer to store three paths (alloc()/free() is slow!) */ |
| 4795 | if ((exp1 = alloc(MAXPATHL * 3)) != NULL) |
| 4796 | { |
| 4797 | full1 = exp1 + MAXPATHL; |
| 4798 | full2 = full1 + MAXPATHL; |
| 4799 | |
| 4800 | expand_env(s1, exp1, MAXPATHL); |
| 4801 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 4802 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 4803 | |
| 4804 | /* If vim_FullName() fails, the file probably doesn't exist. */ |
| 4805 | if (r1 != OK && r2 != OK) |
| 4806 | { |
| 4807 | if (checkname && fnamecmp(exp1, s2) == 0) |
| 4808 | retval = FPC_SAMEX; |
| 4809 | else |
| 4810 | retval = FPC_NOTX; |
| 4811 | } |
| 4812 | else if (r1 != OK || r2 != OK) |
| 4813 | retval = FPC_DIFFX; |
| 4814 | else if (fnamecmp(full1, full2)) |
| 4815 | retval = FPC_DIFF; |
| 4816 | else |
| 4817 | retval = FPC_SAME; |
| 4818 | vim_free(exp1); |
| 4819 | } |
| 4820 | return retval; |
| 4821 | #endif |
| 4822 | } |
| 4823 | |
| 4824 | /* |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 4825 | * Get the tail of a path: the file name. |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 4826 | * 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] | 4827 | * Fail safe: never returns NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4828 | */ |
| 4829 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4830 | gettail(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4831 | { |
| 4832 | char_u *p1, *p2; |
| 4833 | |
| 4834 | if (fname == NULL) |
| 4835 | return (char_u *)""; |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 4836 | 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] | 4837 | { |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 4838 | if (vim_ispathsep_nocolon(*p2)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4839 | p1 = p2 + 1; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4840 | mb_ptr_adv(p2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4841 | } |
| 4842 | return p1; |
| 4843 | } |
| 4844 | |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 4845 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 4846 | static char_u *gettail_dir(char_u *fname); |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 4847 | |
| 4848 | /* |
| 4849 | * Return the end of the directory name, on the first path |
| 4850 | * separator: |
| 4851 | * "/path/file", "/path/dir/", "/path//dir", "/file" |
| 4852 | * ^ ^ ^ ^ |
| 4853 | */ |
| 4854 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4855 | gettail_dir(char_u *fname) |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 4856 | { |
| 4857 | char_u *dir_end = fname; |
| 4858 | char_u *next_dir_end = fname; |
| 4859 | int look_for_sep = TRUE; |
| 4860 | char_u *p; |
| 4861 | |
| 4862 | for (p = fname; *p != NUL; ) |
| 4863 | { |
| 4864 | if (vim_ispathsep(*p)) |
| 4865 | { |
| 4866 | if (look_for_sep) |
| 4867 | { |
| 4868 | next_dir_end = p; |
| 4869 | look_for_sep = FALSE; |
| 4870 | } |
| 4871 | } |
| 4872 | else |
| 4873 | { |
| 4874 | if (!look_for_sep) |
| 4875 | dir_end = next_dir_end; |
| 4876 | look_for_sep = TRUE; |
| 4877 | } |
| 4878 | mb_ptr_adv(p); |
| 4879 | } |
| 4880 | return dir_end; |
| 4881 | } |
| 4882 | #endif |
| 4883 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4884 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4885 | * Get pointer to tail of "fname", including path separators. Putting a NUL |
| 4886 | * here leaves the directory name. Takes care of "c:/" and "//". |
| 4887 | * Always returns a valid pointer. |
| 4888 | */ |
| 4889 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4890 | gettail_sep(char_u *fname) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4891 | { |
| 4892 | char_u *p; |
| 4893 | char_u *t; |
| 4894 | |
| 4895 | p = get_past_head(fname); /* don't remove the '/' from "c:/file" */ |
| 4896 | t = gettail(fname); |
| 4897 | while (t > p && after_pathsep(fname, t)) |
| 4898 | --t; |
| 4899 | #ifdef VMS |
| 4900 | /* path separator is part of the path */ |
| 4901 | ++t; |
| 4902 | #endif |
| 4903 | return t; |
| 4904 | } |
| 4905 | |
| 4906 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4907 | * get the next path component (just after the next path separator). |
| 4908 | */ |
| 4909 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4910 | getnextcomp(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4911 | { |
| 4912 | while (*fname && !vim_ispathsep(*fname)) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4913 | mb_ptr_adv(fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4914 | if (*fname) |
| 4915 | ++fname; |
| 4916 | return fname; |
| 4917 | } |
| 4918 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4919 | /* |
| 4920 | * Get a pointer to one character past the head of a path name. |
| 4921 | * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. |
| 4922 | * If there is no head, path is returned. |
| 4923 | */ |
| 4924 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4925 | get_past_head(char_u *path) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4926 | { |
| 4927 | char_u *retval; |
| 4928 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4929 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4930 | /* may skip "c:" */ |
| 4931 | if (isalpha(path[0]) && path[1] == ':') |
| 4932 | retval = path + 2; |
| 4933 | else |
| 4934 | retval = path; |
| 4935 | #else |
| 4936 | # if defined(AMIGA) |
| 4937 | /* may skip "label:" */ |
| 4938 | retval = vim_strchr(path, ':'); |
| 4939 | if (retval == NULL) |
| 4940 | retval = path; |
| 4941 | # else /* Unix */ |
| 4942 | retval = path; |
| 4943 | # endif |
| 4944 | #endif |
| 4945 | |
| 4946 | while (vim_ispathsep(*retval)) |
| 4947 | ++retval; |
| 4948 | |
| 4949 | return retval; |
| 4950 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4951 | |
| 4952 | /* |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 4953 | * Return TRUE if 'c' is a path separator. |
| 4954 | * Note that for MS-Windows this includes the colon. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4955 | */ |
| 4956 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4957 | vim_ispathsep(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4958 | { |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 4959 | #ifdef UNIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4960 | return (c == '/'); /* UNIX has ':' inside file names */ |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 4961 | #else |
| 4962 | # ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4963 | return (c == ':' || c == '/' || c == '\\'); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 4964 | # else |
| 4965 | # ifdef VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4966 | /* server"user passwd"::device:[full.path.name]fname.extension;version" */ |
| 4967 | return (c == ':' || c == '[' || c == ']' || c == '/' |
| 4968 | || c == '<' || c == '>' || c == '"' ); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 4969 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4970 | return (c == ':' || c == '/'); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 4971 | # endif /* VMS */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4972 | # endif |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 4973 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4974 | } |
| 4975 | |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 4976 | /* |
| 4977 | * Like vim_ispathsep(c), but exclude the colon for MS-Windows. |
| 4978 | */ |
| 4979 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4980 | vim_ispathsep_nocolon(int c) |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 4981 | { |
| 4982 | return vim_ispathsep(c) |
| 4983 | #ifdef BACKSLASH_IN_FILENAME |
| 4984 | && c != ':' |
| 4985 | #endif |
| 4986 | ; |
| 4987 | } |
| 4988 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4989 | #if defined(FEAT_SEARCHPATH) || defined(PROTO) |
| 4990 | /* |
| 4991 | * return TRUE if 'c' is a path list separator. |
| 4992 | */ |
| 4993 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4994 | vim_ispathlistsep(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4995 | { |
| 4996 | #ifdef UNIX |
| 4997 | return (c == ':'); |
| 4998 | #else |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 4999 | return (c == ';'); /* might not be right for every system... */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5000 | #endif |
| 5001 | } |
| 5002 | #endif |
| 5003 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5004 | #if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \ |
| 5005 | || defined(FEAT_EVAL) || defined(PROTO) |
| 5006 | /* |
| 5007 | * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" |
| 5008 | * It's done in-place. |
| 5009 | */ |
| 5010 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5011 | shorten_dir(char_u *str) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5012 | { |
| 5013 | char_u *tail, *s, *d; |
| 5014 | int skip = FALSE; |
| 5015 | |
| 5016 | tail = gettail(str); |
| 5017 | d = str; |
| 5018 | for (s = str; ; ++s) |
| 5019 | { |
| 5020 | if (s >= tail) /* copy the whole tail */ |
| 5021 | { |
| 5022 | *d++ = *s; |
| 5023 | if (*s == NUL) |
| 5024 | break; |
| 5025 | } |
| 5026 | else if (vim_ispathsep(*s)) /* copy '/' and next char */ |
| 5027 | { |
| 5028 | *d++ = *s; |
| 5029 | skip = FALSE; |
| 5030 | } |
| 5031 | else if (!skip) |
| 5032 | { |
| 5033 | *d++ = *s; /* copy next char */ |
| 5034 | if (*s != '~' && *s != '.') /* and leading "~" and "." */ |
| 5035 | skip = TRUE; |
| 5036 | # ifdef FEAT_MBYTE |
| 5037 | if (has_mbyte) |
| 5038 | { |
| 5039 | int l = mb_ptr2len(s); |
| 5040 | |
| 5041 | while (--l > 0) |
Bram Moolenaar | b6baca5 | 2006-08-15 20:24:14 +0000 | [diff] [blame] | 5042 | *d++ = *++s; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5043 | } |
| 5044 | # endif |
| 5045 | } |
| 5046 | } |
| 5047 | } |
| 5048 | #endif |
| 5049 | |
Bram Moolenaar | 900b4d7 | 2005-12-12 22:05:50 +0000 | [diff] [blame] | 5050 | /* |
| 5051 | * Return TRUE if the directory of "fname" exists, FALSE otherwise. |
| 5052 | * Also returns TRUE if there is no directory name. |
| 5053 | * "fname" must be writable!. |
| 5054 | */ |
| 5055 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5056 | dir_of_file_exists(char_u *fname) |
Bram Moolenaar | 900b4d7 | 2005-12-12 22:05:50 +0000 | [diff] [blame] | 5057 | { |
| 5058 | char_u *p; |
| 5059 | int c; |
| 5060 | int retval; |
| 5061 | |
| 5062 | p = gettail_sep(fname); |
| 5063 | if (p == fname) |
| 5064 | return TRUE; |
| 5065 | c = *p; |
| 5066 | *p = NUL; |
| 5067 | retval = mch_isdir(fname); |
| 5068 | *p = c; |
| 5069 | return retval; |
| 5070 | } |
| 5071 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5072 | /* |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5073 | * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally |
| 5074 | * and deal with 'fileignorecase'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5075 | */ |
| 5076 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5077 | vim_fnamecmp(char_u *x, char_u *y) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5078 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5079 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5080 | return vim_fnamencmp(x, y, MAXPATHL); |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5081 | #else |
| 5082 | if (p_fic) |
| 5083 | return MB_STRICMP(x, y); |
| 5084 | return STRCMP(x, y); |
| 5085 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
| 5088 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5089 | vim_fnamencmp(char_u *x, char_u *y, size_t len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5090 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5091 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5092 | char_u *px = x; |
| 5093 | char_u *py = y; |
| 5094 | int cx = NUL; |
| 5095 | int cy = NUL; |
| 5096 | |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5097 | while (len > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5098 | { |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5099 | cx = PTR2CHAR(px); |
| 5100 | cy = PTR2CHAR(py); |
| 5101 | if (cx == NUL || cy == NUL |
| 5102 | || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy) |
| 5103 | && !(cx == '/' && cy == '\\') |
| 5104 | && !(cx == '\\' && cy == '/'))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5105 | break; |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5106 | len -= MB_PTR2LEN(px); |
| 5107 | px += MB_PTR2LEN(px); |
| 5108 | py += MB_PTR2LEN(py); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5109 | } |
| 5110 | if (len == 0) |
| 5111 | return 0; |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5112 | return (cx - cy); |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5113 | #else |
| 5114 | if (p_fic) |
| 5115 | return MB_STRNICMP(x, y, len); |
| 5116 | return STRNCMP(x, y, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5117 | #endif |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5118 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5119 | |
| 5120 | /* |
| 5121 | * Concatenate file names fname1 and fname2 into allocated memory. |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 5122 | * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5123 | */ |
| 5124 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5125 | concat_fnames(char_u *fname1, char_u *fname2, int sep) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5126 | { |
| 5127 | char_u *dest; |
| 5128 | |
| 5129 | dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3)); |
| 5130 | if (dest != NULL) |
| 5131 | { |
| 5132 | STRCPY(dest, fname1); |
| 5133 | if (sep) |
| 5134 | add_pathsep(dest); |
| 5135 | STRCAT(dest, fname2); |
| 5136 | } |
| 5137 | return dest; |
| 5138 | } |
| 5139 | |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5140 | /* |
| 5141 | * Concatenate two strings and return the result in allocated memory. |
| 5142 | * Returns NULL when out of memory. |
| 5143 | */ |
| 5144 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5145 | concat_str(char_u *str1, char_u *str2) |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5146 | { |
| 5147 | char_u *dest; |
| 5148 | size_t l = STRLEN(str1); |
| 5149 | |
| 5150 | dest = alloc((unsigned)(l + STRLEN(str2) + 1L)); |
| 5151 | if (dest != NULL) |
| 5152 | { |
| 5153 | STRCPY(dest, str1); |
| 5154 | STRCPY(dest + l, str2); |
| 5155 | } |
| 5156 | return dest; |
| 5157 | } |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5158 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5159 | /* |
| 5160 | * Add a path separator to a file name, unless it already ends in a path |
| 5161 | * separator. |
| 5162 | */ |
| 5163 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5164 | add_pathsep(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5165 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5166 | if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5167 | STRCAT(p, PATHSEPSTR); |
| 5168 | } |
| 5169 | |
| 5170 | /* |
| 5171 | * FullName_save - Make an allocated copy of a full file name. |
| 5172 | * Returns NULL when out of memory. |
| 5173 | */ |
| 5174 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5175 | FullName_save( |
| 5176 | char_u *fname, |
| 5177 | int force) /* force expansion, even when it already looks |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 5178 | * like a full path name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5179 | { |
| 5180 | char_u *buf; |
| 5181 | char_u *new_fname = NULL; |
| 5182 | |
| 5183 | if (fname == NULL) |
| 5184 | return NULL; |
| 5185 | |
| 5186 | buf = alloc((unsigned)MAXPATHL); |
| 5187 | if (buf != NULL) |
| 5188 | { |
| 5189 | if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) |
| 5190 | new_fname = vim_strsave(buf); |
| 5191 | else |
| 5192 | new_fname = vim_strsave(fname); |
| 5193 | vim_free(buf); |
| 5194 | } |
| 5195 | return new_fname; |
| 5196 | } |
| 5197 | |
| 5198 | #if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL) |
| 5199 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5200 | static char_u *skip_string(char_u *p); |
| 5201 | static pos_T *ind_find_start_comment(void); |
| 5202 | static pos_T *ind_find_start_CORS(void); |
| 5203 | static pos_T *find_start_rawstring(int ind_maxcomment); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5204 | |
| 5205 | /* |
| 5206 | * Find the start of a comment, not knowing if we are in a comment right now. |
| 5207 | * Search starts at w_cursor.lnum and goes backwards. |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5208 | * Return NULL when not inside a comment. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5209 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 5210 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5211 | ind_find_start_comment(void) /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 5212 | { |
| 5213 | return find_start_comment(curbuf->b_ind_maxcomment); |
| 5214 | } |
| 5215 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5216 | pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5217 | find_start_comment(int ind_maxcomment) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5218 | { |
| 5219 | pos_T *pos; |
| 5220 | char_u *line; |
| 5221 | char_u *p; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5222 | int cur_maxcomment = ind_maxcomment; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5223 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5224 | for (;;) |
| 5225 | { |
| 5226 | pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment); |
| 5227 | if (pos == NULL) |
| 5228 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5229 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5230 | /* |
| 5231 | * Check if the comment start we found is inside a string. |
| 5232 | * If it is then restrict the search to below this line and try again. |
| 5233 | */ |
| 5234 | line = ml_get(pos->lnum); |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 5235 | for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5236 | p = skip_string(p); |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 5237 | if ((colnr_T)(p - line) <= pos->col) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5238 | break; |
| 5239 | cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; |
| 5240 | if (cur_maxcomment <= 0) |
| 5241 | { |
| 5242 | pos = NULL; |
| 5243 | break; |
| 5244 | } |
| 5245 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5246 | return pos; |
| 5247 | } |
| 5248 | |
| 5249 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5250 | * Find the start of a comment or raw string, not knowing if we are in a |
| 5251 | * comment or raw string right now. |
| 5252 | * Search starts at w_cursor.lnum and goes backwards. |
| 5253 | * Return NULL when not inside a comment or raw string. |
| 5254 | * "CORS" -> Comment Or Raw String |
| 5255 | */ |
| 5256 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5257 | ind_find_start_CORS(void) /* XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5258 | { |
Bram Moolenaar | 089af18 | 2015-10-07 11:41:49 +0200 | [diff] [blame] | 5259 | static pos_T comment_pos_copy; |
| 5260 | pos_T *comment_pos; |
| 5261 | pos_T *rs_pos; |
| 5262 | |
| 5263 | comment_pos = find_start_comment(curbuf->b_ind_maxcomment); |
| 5264 | if (comment_pos != NULL) |
| 5265 | { |
| 5266 | /* Need to make a copy of the static pos in findmatchlimit(), |
| 5267 | * calling find_start_rawstring() may change it. */ |
| 5268 | comment_pos_copy = *comment_pos; |
| 5269 | comment_pos = &comment_pos_copy; |
| 5270 | } |
| 5271 | rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment); |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5272 | |
| 5273 | /* If comment_pos is before rs_pos the raw string is inside the comment. |
| 5274 | * If rs_pos is before comment_pos the comment is inside the raw string. */ |
| 5275 | if (comment_pos == NULL || (rs_pos != NULL && lt(*rs_pos, *comment_pos))) |
| 5276 | return rs_pos; |
| 5277 | return comment_pos; |
| 5278 | } |
| 5279 | |
| 5280 | /* |
| 5281 | * Find the start of a raw string, not knowing if we are in one right now. |
| 5282 | * Search starts at w_cursor.lnum and goes backwards. |
| 5283 | * Return NULL when not inside a raw string. |
| 5284 | */ |
| 5285 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5286 | find_start_rawstring(int ind_maxcomment) /* XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5287 | { |
| 5288 | pos_T *pos; |
| 5289 | char_u *line; |
| 5290 | char_u *p; |
| 5291 | int cur_maxcomment = ind_maxcomment; |
| 5292 | |
| 5293 | for (;;) |
| 5294 | { |
| 5295 | pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment); |
| 5296 | if (pos == NULL) |
| 5297 | break; |
| 5298 | |
| 5299 | /* |
| 5300 | * Check if the raw string start we found is inside a string. |
| 5301 | * If it is then restrict the search to below this line and try again. |
| 5302 | */ |
| 5303 | line = ml_get(pos->lnum); |
| 5304 | for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) |
| 5305 | p = skip_string(p); |
| 5306 | if ((colnr_T)(p - line) <= pos->col) |
| 5307 | break; |
| 5308 | cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; |
| 5309 | if (cur_maxcomment <= 0) |
| 5310 | { |
| 5311 | pos = NULL; |
| 5312 | break; |
| 5313 | } |
| 5314 | } |
| 5315 | return pos; |
| 5316 | } |
| 5317 | |
| 5318 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5319 | * Skip to the end of a "string" and a 'c' character. |
| 5320 | * If there is no string or character, return argument unmodified. |
| 5321 | */ |
| 5322 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5323 | skip_string(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5324 | { |
| 5325 | int i; |
| 5326 | |
| 5327 | /* |
| 5328 | * We loop, because strings may be concatenated: "date""time". |
| 5329 | */ |
| 5330 | for ( ; ; ++p) |
| 5331 | { |
| 5332 | if (p[0] == '\'') /* 'c' or '\n' or '\000' */ |
| 5333 | { |
| 5334 | if (!p[1]) /* ' at end of line */ |
| 5335 | break; |
| 5336 | i = 2; |
| 5337 | if (p[1] == '\\') /* '\n' or '\000' */ |
| 5338 | { |
| 5339 | ++i; |
| 5340 | while (vim_isdigit(p[i - 1])) /* '\000' */ |
| 5341 | ++i; |
| 5342 | } |
| 5343 | if (p[i] == '\'') /* check for trailing ' */ |
| 5344 | { |
| 5345 | p += i; |
| 5346 | continue; |
| 5347 | } |
| 5348 | } |
| 5349 | else if (p[0] == '"') /* start of string */ |
| 5350 | { |
| 5351 | for (++p; p[0]; ++p) |
| 5352 | { |
| 5353 | if (p[0] == '\\' && p[1] != NUL) |
| 5354 | ++p; |
| 5355 | else if (p[0] == '"') /* end of string */ |
| 5356 | break; |
| 5357 | } |
| 5358 | if (p[0] == '"') |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5359 | continue; /* continue for another string */ |
| 5360 | } |
| 5361 | else if (p[0] == 'R' && p[1] == '"') |
| 5362 | { |
| 5363 | /* Raw string: R"[delim](...)[delim]" */ |
| 5364 | char_u *delim = p + 2; |
| 5365 | char_u *paren = vim_strchr(delim, '('); |
| 5366 | |
| 5367 | if (paren != NULL) |
| 5368 | { |
| 5369 | size_t delim_len = paren - delim; |
| 5370 | |
| 5371 | for (p += 3; *p; ++p) |
| 5372 | if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0 |
| 5373 | && p[delim_len + 1] == '"') |
| 5374 | { |
| 5375 | p += delim_len + 1; |
| 5376 | break; |
| 5377 | } |
| 5378 | if (p[0] == '"') |
| 5379 | continue; /* continue for another string */ |
| 5380 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5381 | } |
| 5382 | break; /* no string found */ |
| 5383 | } |
| 5384 | if (!*p) |
| 5385 | --p; /* backup from NUL */ |
| 5386 | return p; |
| 5387 | } |
| 5388 | #endif /* FEAT_CINDENT || FEAT_SYN_HL */ |
| 5389 | |
| 5390 | #if defined(FEAT_CINDENT) || defined(PROTO) |
| 5391 | |
| 5392 | /* |
| 5393 | * Do C or expression indenting on the current line. |
| 5394 | */ |
| 5395 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5396 | do_c_expr_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5397 | { |
| 5398 | # ifdef FEAT_EVAL |
| 5399 | if (*curbuf->b_p_inde != NUL) |
| 5400 | fixthisline(get_expr_indent); |
| 5401 | else |
| 5402 | # endif |
| 5403 | fixthisline(get_c_indent); |
| 5404 | } |
| 5405 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 5406 | /* Find result cache for cpp_baseclass */ |
| 5407 | typedef struct { |
| 5408 | int found; |
| 5409 | lpos_T lpos; |
| 5410 | } cpp_baseclass_cache_T; |
| 5411 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5412 | /* |
| 5413 | * Functions for C-indenting. |
| 5414 | * Most of this originally comes from Eric Fischer. |
| 5415 | */ |
| 5416 | /* |
| 5417 | * Below "XXX" means that this function may unlock the current line. |
| 5418 | */ |
| 5419 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5420 | static char_u *cin_skipcomment(char_u *); |
| 5421 | static int cin_nocode(char_u *); |
| 5422 | static pos_T *find_line_comment(void); |
| 5423 | static int cin_has_js_key(char_u *text); |
| 5424 | static int cin_islabel_skip(char_u **); |
| 5425 | static int cin_isdefault(char_u *); |
| 5426 | static char_u *after_label(char_u *l); |
| 5427 | static int get_indent_nolabel(linenr_T lnum); |
| 5428 | static int skip_label(linenr_T, char_u **pp); |
| 5429 | static int cin_first_id_amount(void); |
| 5430 | static int cin_get_equal_amount(linenr_T lnum); |
| 5431 | static int cin_ispreproc(char_u *); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5432 | static int cin_iscomment(char_u *); |
| 5433 | static int cin_islinecomment(char_u *); |
| 5434 | static int cin_isterminated(char_u *, int, int); |
| 5435 | static int cin_isinit(void); |
| 5436 | static int cin_isfuncdecl(char_u **, linenr_T, linenr_T); |
| 5437 | static int cin_isif(char_u *); |
| 5438 | static int cin_iselse(char_u *); |
| 5439 | static int cin_isdo(char_u *); |
| 5440 | static int cin_iswhileofdo(char_u *, linenr_T); |
| 5441 | static int cin_is_if_for_while_before_offset(char_u *line, int *poffset); |
| 5442 | static int cin_iswhileofdo_end(int terminated); |
| 5443 | static int cin_isbreak(char_u *); |
| 5444 | static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached); |
| 5445 | static int get_baseclass_amount(int col); |
| 5446 | static int cin_ends_in(char_u *, char_u *, char_u *); |
| 5447 | static int cin_starts_with(char_u *s, char *word); |
| 5448 | static int cin_skip2pos(pos_T *trypos); |
| 5449 | static pos_T *find_start_brace(void); |
| 5450 | static pos_T *find_match_paren(int); |
| 5451 | static pos_T *find_match_char(int c, int ind_maxparen); |
| 5452 | static int corr_ind_maxparen(pos_T *startpos); |
| 5453 | static int find_last_paren(char_u *l, int start, int end); |
| 5454 | static int find_match(int lookfor, linenr_T ourscope); |
| 5455 | static int cin_is_cpp_namespace(char_u *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5456 | |
| 5457 | /* |
| 5458 | * Skip over white space and C comments within the line. |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5459 | * Also skip over Perl/shell comments if desired. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5460 | */ |
| 5461 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5462 | cin_skipcomment(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5463 | { |
| 5464 | while (*s) |
| 5465 | { |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5466 | char_u *prev_s = s; |
| 5467 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5468 | s = skipwhite(s); |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5469 | |
| 5470 | /* Perl/shell # comment comment continues until eol. Require a space |
| 5471 | * before # to avoid recognizing $#array. */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 5472 | if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#') |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5473 | { |
| 5474 | s += STRLEN(s); |
| 5475 | break; |
| 5476 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5477 | if (*s != '/') |
| 5478 | break; |
| 5479 | ++s; |
| 5480 | if (*s == '/') /* slash-slash comment continues till eol */ |
| 5481 | { |
| 5482 | s += STRLEN(s); |
| 5483 | break; |
| 5484 | } |
| 5485 | if (*s != '*') |
| 5486 | break; |
| 5487 | for (++s; *s; ++s) /* skip slash-star comment */ |
| 5488 | if (s[0] == '*' && s[1] == '/') |
| 5489 | { |
| 5490 | s += 2; |
| 5491 | break; |
| 5492 | } |
| 5493 | } |
| 5494 | return s; |
| 5495 | } |
| 5496 | |
| 5497 | /* |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 5498 | * Return TRUE if there is no code at *s. White space and comments are |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5499 | * not considered code. |
| 5500 | */ |
| 5501 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5502 | cin_nocode(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5503 | { |
| 5504 | return *cin_skipcomment(s) == NUL; |
| 5505 | } |
| 5506 | |
| 5507 | /* |
| 5508 | * Check previous lines for a "//" line comment, skipping over blank lines. |
| 5509 | */ |
| 5510 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5511 | find_line_comment(void) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5512 | { |
| 5513 | static pos_T pos; |
| 5514 | char_u *line; |
| 5515 | char_u *p; |
| 5516 | |
| 5517 | pos = curwin->w_cursor; |
| 5518 | while (--pos.lnum > 0) |
| 5519 | { |
| 5520 | line = ml_get(pos.lnum); |
| 5521 | p = skipwhite(line); |
| 5522 | if (cin_islinecomment(p)) |
| 5523 | { |
| 5524 | pos.col = (int)(p - line); |
| 5525 | return &pos; |
| 5526 | } |
| 5527 | if (*p != NUL) |
| 5528 | break; |
| 5529 | } |
| 5530 | return NULL; |
| 5531 | } |
| 5532 | |
| 5533 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5534 | * Return TRUE if "text" starts with "key:". |
| 5535 | */ |
| 5536 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5537 | cin_has_js_key(char_u *text) |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5538 | { |
| 5539 | char_u *s = skipwhite(text); |
Bram Moolenaar | ece29e8 | 2014-08-06 12:49:18 +0200 | [diff] [blame] | 5540 | int quote = -1; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5541 | |
| 5542 | if (*s == '\'' || *s == '"') |
| 5543 | { |
| 5544 | /* can be 'key': or "key": */ |
| 5545 | quote = *s; |
| 5546 | ++s; |
| 5547 | } |
| 5548 | if (!vim_isIDc(*s)) /* need at least one ID character */ |
| 5549 | return FALSE; |
| 5550 | |
| 5551 | while (vim_isIDc(*s)) |
| 5552 | ++s; |
| 5553 | if (*s == quote) |
| 5554 | ++s; |
| 5555 | |
| 5556 | s = cin_skipcomment(s); |
| 5557 | |
| 5558 | /* "::" is not a label, it's C++ */ |
| 5559 | return (*s == ':' && s[1] != ':'); |
| 5560 | } |
| 5561 | |
| 5562 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5563 | * Check if string matches "label:"; move to character after ':' if true. |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5564 | * "*s" must point to the start of the label, if there is one. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5565 | */ |
| 5566 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5567 | cin_islabel_skip(char_u **s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5568 | { |
| 5569 | if (!vim_isIDc(**s)) /* need at least one ID character */ |
| 5570 | return FALSE; |
| 5571 | |
| 5572 | while (vim_isIDc(**s)) |
| 5573 | (*s)++; |
| 5574 | |
| 5575 | *s = cin_skipcomment(*s); |
| 5576 | |
| 5577 | /* "::" is not a label, it's C++ */ |
| 5578 | return (**s == ':' && *++*s != ':'); |
| 5579 | } |
| 5580 | |
| 5581 | /* |
| 5582 | * Recognize a label: "label:". |
| 5583 | * Note: curwin->w_cursor must be where we are looking for the label. |
| 5584 | */ |
| 5585 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5586 | cin_islabel(void) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5587 | { |
| 5588 | char_u *s; |
| 5589 | |
| 5590 | s = cin_skipcomment(ml_get_curline()); |
| 5591 | |
| 5592 | /* |
| 5593 | * Exclude "default" from labels, since it should be indented |
| 5594 | * like a switch label. Same for C++ scope declarations. |
| 5595 | */ |
| 5596 | if (cin_isdefault(s)) |
| 5597 | return FALSE; |
| 5598 | if (cin_isscopedecl(s)) |
| 5599 | return FALSE; |
| 5600 | |
| 5601 | if (cin_islabel_skip(&s)) |
| 5602 | { |
| 5603 | /* |
| 5604 | * Only accept a label if the previous line is terminated or is a case |
| 5605 | * label. |
| 5606 | */ |
| 5607 | pos_T cursor_save; |
| 5608 | pos_T *trypos; |
| 5609 | char_u *line; |
| 5610 | |
| 5611 | cursor_save = curwin->w_cursor; |
| 5612 | while (curwin->w_cursor.lnum > 1) |
| 5613 | { |
| 5614 | --curwin->w_cursor.lnum; |
| 5615 | |
| 5616 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5617 | * If we're in a comment or raw string now, skip to the start of |
| 5618 | * it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5619 | */ |
| 5620 | curwin->w_cursor.col = 0; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5621 | if ((trypos = ind_find_start_CORS()) != NULL) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5622 | curwin->w_cursor = *trypos; |
| 5623 | |
| 5624 | line = ml_get_curline(); |
| 5625 | if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */ |
| 5626 | continue; |
| 5627 | if (*(line = cin_skipcomment(line)) == NUL) |
| 5628 | continue; |
| 5629 | |
| 5630 | curwin->w_cursor = cursor_save; |
| 5631 | if (cin_isterminated(line, TRUE, FALSE) |
| 5632 | || cin_isscopedecl(line) |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5633 | || cin_iscase(line, TRUE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5634 | || (cin_islabel_skip(&line) && cin_nocode(line))) |
| 5635 | return TRUE; |
| 5636 | return FALSE; |
| 5637 | } |
| 5638 | curwin->w_cursor = cursor_save; |
| 5639 | return TRUE; /* label at start of file??? */ |
| 5640 | } |
| 5641 | return FALSE; |
| 5642 | } |
| 5643 | |
| 5644 | /* |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5645 | * Recognize structure initialization and enumerations: |
| 5646 | * "[typedef] [static|public|protected|private] enum" |
| 5647 | * "[typedef] [static|public|protected|private] = {" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5648 | */ |
| 5649 | static int |
| 5650 | cin_isinit(void) |
| 5651 | { |
| 5652 | char_u *s; |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5653 | static char *skip[] = {"static", "public", "protected", "private"}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5654 | |
| 5655 | s = cin_skipcomment(ml_get_curline()); |
| 5656 | |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5657 | if (cin_starts_with(s, "typedef")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5658 | s = cin_skipcomment(s + 7); |
| 5659 | |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5660 | for (;;) |
| 5661 | { |
| 5662 | int i, l; |
Bram Moolenaar | a528565 | 2011-12-14 20:05:21 +0100 | [diff] [blame] | 5663 | |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5664 | for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i) |
| 5665 | { |
Bram Moolenaar | b3cb982 | 2013-03-13 17:01:52 +0100 | [diff] [blame] | 5666 | l = (int)strlen(skip[i]); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5667 | if (cin_starts_with(s, skip[i])) |
| 5668 | { |
| 5669 | s = cin_skipcomment(s + l); |
| 5670 | l = 0; |
| 5671 | break; |
| 5672 | } |
| 5673 | } |
| 5674 | if (l != 0) |
| 5675 | break; |
| 5676 | } |
| 5677 | |
| 5678 | if (cin_starts_with(s, "enum")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5679 | return TRUE; |
| 5680 | |
| 5681 | if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) |
| 5682 | return TRUE; |
| 5683 | |
| 5684 | return FALSE; |
| 5685 | } |
| 5686 | |
| 5687 | /* |
| 5688 | * Recognize a switch label: "case .*:" or "default:". |
| 5689 | */ |
| 5690 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5691 | cin_iscase( |
| 5692 | char_u *s, |
| 5693 | int strict) /* Allow relaxed check of case statement for JS */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5694 | { |
| 5695 | s = cin_skipcomment(s); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5696 | if (cin_starts_with(s, "case")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5697 | { |
| 5698 | for (s += 4; *s; ++s) |
| 5699 | { |
| 5700 | s = cin_skipcomment(s); |
| 5701 | if (*s == ':') |
| 5702 | { |
| 5703 | if (s[1] == ':') /* skip over "::" for C++ */ |
| 5704 | ++s; |
| 5705 | else |
| 5706 | return TRUE; |
| 5707 | } |
| 5708 | if (*s == '\'' && s[1] && s[2] == '\'') |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5709 | s += 2; /* skip over ':' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5710 | else if (*s == '/' && (s[1] == '*' || s[1] == '/')) |
| 5711 | return FALSE; /* stop at comment */ |
| 5712 | else if (*s == '"') |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5713 | { |
| 5714 | /* JS etc. */ |
| 5715 | if (strict) |
| 5716 | return FALSE; /* stop at string */ |
| 5717 | else |
| 5718 | return TRUE; |
| 5719 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5720 | } |
| 5721 | return FALSE; |
| 5722 | } |
| 5723 | |
| 5724 | if (cin_isdefault(s)) |
| 5725 | return TRUE; |
| 5726 | return FALSE; |
| 5727 | } |
| 5728 | |
| 5729 | /* |
| 5730 | * Recognize a "default" switch label. |
| 5731 | */ |
| 5732 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5733 | cin_isdefault(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5734 | { |
| 5735 | return (STRNCMP(s, "default", 7) == 0 |
| 5736 | && *(s = cin_skipcomment(s + 7)) == ':' |
| 5737 | && s[1] != ':'); |
| 5738 | } |
| 5739 | |
| 5740 | /* |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 5741 | * Recognize a "public/private/protected" scope declaration label. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5742 | */ |
| 5743 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5744 | cin_isscopedecl(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5745 | { |
| 5746 | int i; |
| 5747 | |
| 5748 | s = cin_skipcomment(s); |
| 5749 | if (STRNCMP(s, "public", 6) == 0) |
| 5750 | i = 6; |
| 5751 | else if (STRNCMP(s, "protected", 9) == 0) |
| 5752 | i = 9; |
| 5753 | else if (STRNCMP(s, "private", 7) == 0) |
| 5754 | i = 7; |
| 5755 | else |
| 5756 | return FALSE; |
| 5757 | return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':'); |
| 5758 | } |
| 5759 | |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 5760 | /* Maximum number of lines to search back for a "namespace" line. */ |
| 5761 | #define FIND_NAMESPACE_LIM 20 |
| 5762 | |
| 5763 | /* |
| 5764 | * Recognize a "namespace" scope declaration. |
| 5765 | */ |
| 5766 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5767 | cin_is_cpp_namespace(char_u *s) |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 5768 | { |
| 5769 | char_u *p; |
| 5770 | int has_name = FALSE; |
Bram Moolenaar | ca8b8d6 | 2016-11-17 21:30:27 +0100 | [diff] [blame] | 5771 | int has_name_start = FALSE; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 5772 | |
| 5773 | s = cin_skipcomment(s); |
| 5774 | if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9]))) |
| 5775 | { |
| 5776 | p = cin_skipcomment(skipwhite(s + 9)); |
| 5777 | while (*p != NUL) |
| 5778 | { |
| 5779 | if (vim_iswhite(*p)) |
| 5780 | { |
| 5781 | has_name = TRUE; /* found end of a name */ |
| 5782 | p = cin_skipcomment(skipwhite(p)); |
| 5783 | } |
| 5784 | else if (*p == '{') |
| 5785 | { |
| 5786 | break; |
| 5787 | } |
| 5788 | else if (vim_iswordc(*p)) |
| 5789 | { |
Bram Moolenaar | ca8b8d6 | 2016-11-17 21:30:27 +0100 | [diff] [blame] | 5790 | has_name_start = TRUE; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 5791 | if (has_name) |
| 5792 | return FALSE; /* word character after skipping past name */ |
| 5793 | ++p; |
| 5794 | } |
Bram Moolenaar | ca8b8d6 | 2016-11-17 21:30:27 +0100 | [diff] [blame] | 5795 | else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2])) |
| 5796 | { |
| 5797 | if (!has_name_start || has_name) |
| 5798 | return FALSE; |
| 5799 | /* C++ 17 nested namespace */ |
| 5800 | p += 3; |
| 5801 | } |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 5802 | else |
| 5803 | { |
| 5804 | return FALSE; |
| 5805 | } |
| 5806 | } |
| 5807 | return TRUE; |
| 5808 | } |
| 5809 | return FALSE; |
| 5810 | } |
| 5811 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5812 | /* |
| 5813 | * Return a pointer to the first non-empty non-comment character after a ':'. |
| 5814 | * Return NULL if not found. |
| 5815 | * case 234: a = b; |
| 5816 | * ^ |
| 5817 | */ |
| 5818 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5819 | after_label(char_u *l) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5820 | { |
| 5821 | for ( ; *l; ++l) |
| 5822 | { |
| 5823 | if (*l == ':') |
| 5824 | { |
| 5825 | if (l[1] == ':') /* skip over "::" for C++ */ |
| 5826 | ++l; |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5827 | else if (!cin_iscase(l + 1, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5828 | break; |
| 5829 | } |
| 5830 | else if (*l == '\'' && l[1] && l[2] == '\'') |
| 5831 | l += 2; /* skip over 'x' */ |
| 5832 | } |
| 5833 | if (*l == NUL) |
| 5834 | return NULL; |
| 5835 | l = cin_skipcomment(l + 1); |
| 5836 | if (*l == NUL) |
| 5837 | return NULL; |
| 5838 | return l; |
| 5839 | } |
| 5840 | |
| 5841 | /* |
| 5842 | * Get indent of line "lnum", skipping a label. |
| 5843 | * Return 0 if there is nothing after the label. |
| 5844 | */ |
| 5845 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5846 | get_indent_nolabel (linenr_T lnum) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5847 | { |
| 5848 | char_u *l; |
| 5849 | pos_T fp; |
| 5850 | colnr_T col; |
| 5851 | char_u *p; |
| 5852 | |
| 5853 | l = ml_get(lnum); |
| 5854 | p = after_label(l); |
| 5855 | if (p == NULL) |
| 5856 | return 0; |
| 5857 | |
| 5858 | fp.col = (colnr_T)(p - l); |
| 5859 | fp.lnum = lnum; |
| 5860 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 5861 | return (int)col; |
| 5862 | } |
| 5863 | |
| 5864 | /* |
| 5865 | * Find indent for line "lnum", ignoring any case or jump label. |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5866 | * Also return a pointer to the text (after the label) in "pp". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5867 | * label: if (asdf && asdfasdf) |
| 5868 | * ^ |
| 5869 | */ |
| 5870 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5871 | skip_label(linenr_T lnum, char_u **pp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5872 | { |
| 5873 | char_u *l; |
| 5874 | int amount; |
| 5875 | pos_T cursor_save; |
| 5876 | |
| 5877 | cursor_save = curwin->w_cursor; |
| 5878 | curwin->w_cursor.lnum = lnum; |
| 5879 | l = ml_get_curline(); |
| 5880 | /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 5881 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5882 | { |
| 5883 | amount = get_indent_nolabel(lnum); |
| 5884 | l = after_label(ml_get_curline()); |
| 5885 | if (l == NULL) /* just in case */ |
| 5886 | l = ml_get_curline(); |
| 5887 | } |
| 5888 | else |
| 5889 | { |
| 5890 | amount = get_indent(); |
| 5891 | l = ml_get_curline(); |
| 5892 | } |
| 5893 | *pp = l; |
| 5894 | |
| 5895 | curwin->w_cursor = cursor_save; |
| 5896 | return amount; |
| 5897 | } |
| 5898 | |
| 5899 | /* |
| 5900 | * Return the indent of the first variable name after a type in a declaration. |
| 5901 | * int a, indent of "a" |
| 5902 | * static struct foo b, indent of "b" |
| 5903 | * enum bla c, indent of "c" |
| 5904 | * Returns zero when it doesn't look like a declaration. |
| 5905 | */ |
| 5906 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5907 | cin_first_id_amount(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5908 | { |
| 5909 | char_u *line, *p, *s; |
| 5910 | int len; |
| 5911 | pos_T fp; |
| 5912 | colnr_T col; |
| 5913 | |
| 5914 | line = ml_get_curline(); |
| 5915 | p = skipwhite(line); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5916 | len = (int)(skiptowhite(p) - p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5917 | if (len == 6 && STRNCMP(p, "static", 6) == 0) |
| 5918 | { |
| 5919 | p = skipwhite(p + 6); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 5920 | len = (int)(skiptowhite(p) - p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5921 | } |
| 5922 | if (len == 6 && STRNCMP(p, "struct", 6) == 0) |
| 5923 | p = skipwhite(p + 6); |
| 5924 | else if (len == 4 && STRNCMP(p, "enum", 4) == 0) |
| 5925 | p = skipwhite(p + 4); |
| 5926 | else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0) |
| 5927 | || (len == 6 && STRNCMP(p, "signed", 6) == 0)) |
| 5928 | { |
| 5929 | s = skipwhite(p + len); |
| 5930 | if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3])) |
| 5931 | || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4])) |
| 5932 | || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5])) |
| 5933 | || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4]))) |
| 5934 | p = s; |
| 5935 | } |
| 5936 | for (len = 0; vim_isIDc(p[len]); ++len) |
| 5937 | ; |
| 5938 | if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p)) |
| 5939 | return 0; |
| 5940 | |
| 5941 | p = skipwhite(p + len); |
| 5942 | fp.lnum = curwin->w_cursor.lnum; |
| 5943 | fp.col = (colnr_T)(p - line); |
| 5944 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 5945 | return (int)col; |
| 5946 | } |
| 5947 | |
| 5948 | /* |
| 5949 | * Return the indent of the first non-blank after an equal sign. |
| 5950 | * char *foo = "here"; |
| 5951 | * Return zero if no (useful) equal sign found. |
| 5952 | * Return -1 if the line above "lnum" ends in a backslash. |
| 5953 | * foo = "asdf\ |
| 5954 | * asdf\ |
| 5955 | * here"; |
| 5956 | */ |
| 5957 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5958 | cin_get_equal_amount(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5959 | { |
| 5960 | char_u *line; |
| 5961 | char_u *s; |
| 5962 | colnr_T col; |
| 5963 | pos_T fp; |
| 5964 | |
| 5965 | if (lnum > 1) |
| 5966 | { |
| 5967 | line = ml_get(lnum - 1); |
| 5968 | if (*line != NUL && line[STRLEN(line) - 1] == '\\') |
| 5969 | return -1; |
| 5970 | } |
| 5971 | |
| 5972 | line = s = ml_get(lnum); |
| 5973 | while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL) |
| 5974 | { |
| 5975 | if (cin_iscomment(s)) /* ignore comments */ |
| 5976 | s = cin_skipcomment(s); |
| 5977 | else |
| 5978 | ++s; |
| 5979 | } |
| 5980 | if (*s != '=') |
| 5981 | return 0; |
| 5982 | |
| 5983 | s = skipwhite(s + 1); |
| 5984 | if (cin_nocode(s)) |
| 5985 | return 0; |
| 5986 | |
| 5987 | if (*s == '"') /* nice alignment for continued strings */ |
| 5988 | ++s; |
| 5989 | |
| 5990 | fp.lnum = lnum; |
| 5991 | fp.col = (colnr_T)(s - line); |
| 5992 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 5993 | return (int)col; |
| 5994 | } |
| 5995 | |
| 5996 | /* |
| 5997 | * Recognize a preprocessor statement: Any line that starts with '#'. |
| 5998 | */ |
| 5999 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6000 | cin_ispreproc(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6001 | { |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6002 | if (*skipwhite(s) == '#') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6003 | return TRUE; |
| 6004 | return FALSE; |
| 6005 | } |
| 6006 | |
| 6007 | /* |
| 6008 | * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a |
| 6009 | * continuation line of a preprocessor statement. Decrease "*lnump" to the |
| 6010 | * start and return the line in "*pp". |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6011 | * Put the amount of indent in "*amount". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6012 | */ |
| 6013 | static int |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6014 | cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6015 | { |
| 6016 | char_u *line = *pp; |
| 6017 | linenr_T lnum = *lnump; |
| 6018 | int retval = FALSE; |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6019 | int candidate_amount = *amount; |
| 6020 | |
| 6021 | if (*line != NUL && line[STRLEN(line) - 1] == '\\') |
| 6022 | candidate_amount = get_indent_lnum(lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6023 | |
Bram Moolenaar | d8e9bb2 | 2005-07-09 21:14:46 +0000 | [diff] [blame] | 6024 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6025 | { |
| 6026 | if (cin_ispreproc(line)) |
| 6027 | { |
| 6028 | retval = TRUE; |
| 6029 | *lnump = lnum; |
| 6030 | break; |
| 6031 | } |
| 6032 | if (lnum == 1) |
| 6033 | break; |
| 6034 | line = ml_get(--lnum); |
| 6035 | if (*line == NUL || line[STRLEN(line) - 1] != '\\') |
| 6036 | break; |
| 6037 | } |
| 6038 | |
| 6039 | if (lnum != *lnump) |
| 6040 | *pp = ml_get(*lnump); |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6041 | if (retval) |
| 6042 | *amount = candidate_amount; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6043 | return retval; |
| 6044 | } |
| 6045 | |
| 6046 | /* |
| 6047 | * Recognize the start of a C or C++ comment. |
| 6048 | */ |
| 6049 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6050 | cin_iscomment(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6051 | { |
| 6052 | return (p[0] == '/' && (p[1] == '*' || p[1] == '/')); |
| 6053 | } |
| 6054 | |
| 6055 | /* |
| 6056 | * Recognize the start of a "//" comment. |
| 6057 | */ |
| 6058 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6059 | cin_islinecomment(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6060 | { |
| 6061 | return (p[0] == '/' && p[1] == '/'); |
| 6062 | } |
| 6063 | |
| 6064 | /* |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6065 | * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or |
| 6066 | * '}'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6067 | * Don't consider "} else" a terminated line. |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6068 | * If a line begins with an "else", only consider it terminated if no unmatched |
| 6069 | * opening braces follow (handle "else { foo();" correctly). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6070 | * Return the character terminating the line (ending char's have precedence if |
| 6071 | * both apply in order to determine initializations). |
| 6072 | */ |
| 6073 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6074 | cin_isterminated( |
| 6075 | char_u *s, |
| 6076 | int incl_open, /* include '{' at the end as terminator */ |
| 6077 | int incl_comma) /* recognize a trailing comma */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6078 | { |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6079 | char_u found_start = 0; |
| 6080 | unsigned n_open = 0; |
| 6081 | int is_else = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6082 | |
| 6083 | s = cin_skipcomment(s); |
| 6084 | |
| 6085 | if (*s == '{' || (*s == '}' && !cin_iselse(s))) |
| 6086 | found_start = *s; |
| 6087 | |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6088 | if (!found_start) |
| 6089 | is_else = cin_iselse(s); |
| 6090 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6091 | while (*s) |
| 6092 | { |
| 6093 | /* skip over comments, "" strings and 'c'haracters */ |
| 6094 | s = skip_string(cin_skipcomment(s)); |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6095 | if (*s == '}' && n_open > 0) |
| 6096 | --n_open; |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6097 | if ((!is_else || n_open == 0) |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6098 | && (*s == ';' || *s == '}' || (incl_comma && *s == ',')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6099 | && cin_nocode(s + 1)) |
| 6100 | return *s; |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6101 | else if (*s == '{') |
| 6102 | { |
| 6103 | if (incl_open && cin_nocode(s + 1)) |
| 6104 | return *s; |
| 6105 | else |
| 6106 | ++n_open; |
| 6107 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6108 | |
| 6109 | if (*s) |
| 6110 | s++; |
| 6111 | } |
| 6112 | return found_start; |
| 6113 | } |
| 6114 | |
| 6115 | /* |
| 6116 | * Recognize the basic picture of a function declaration -- it needs to |
| 6117 | * have an open paren somewhere and a close paren at the end of the line and |
| 6118 | * no semicolons anywhere. |
| 6119 | * When a line ends in a comma we continue looking in the next line. |
| 6120 | * "sp" points to a string with the line. When looking at other lines it must |
| 6121 | * be restored to the line. When it's NULL fetch lines here. |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6122 | * "first_lnum" is where we start looking. |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6123 | * "min_lnum" is the line before which we will not be looking. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6124 | */ |
| 6125 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6126 | cin_isfuncdecl( |
| 6127 | char_u **sp, |
| 6128 | linenr_T first_lnum, |
| 6129 | linenr_T min_lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6130 | { |
| 6131 | char_u *s; |
| 6132 | linenr_T lnum = first_lnum; |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6133 | linenr_T save_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6134 | int retval = FALSE; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6135 | pos_T *trypos; |
| 6136 | int just_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6137 | |
| 6138 | if (sp == NULL) |
| 6139 | s = ml_get(lnum); |
| 6140 | else |
| 6141 | s = *sp; |
| 6142 | |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6143 | curwin->w_cursor.lnum = lnum; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6144 | if (find_last_paren(s, '(', ')') |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6145 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6146 | { |
| 6147 | lnum = trypos->lnum; |
| 6148 | if (lnum < min_lnum) |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6149 | { |
| 6150 | curwin->w_cursor.lnum = save_lnum; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6151 | return FALSE; |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6152 | } |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6153 | |
| 6154 | s = ml_get(lnum); |
| 6155 | } |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6156 | curwin->w_cursor.lnum = save_lnum; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6157 | |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6158 | /* Ignore line starting with #. */ |
| 6159 | if (cin_ispreproc(s)) |
| 6160 | return FALSE; |
| 6161 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6162 | while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"') |
| 6163 | { |
| 6164 | if (cin_iscomment(s)) /* ignore comments */ |
| 6165 | s = cin_skipcomment(s); |
Bram Moolenaar | e01f4f8 | 2015-11-10 14:06:53 +0100 | [diff] [blame] | 6166 | else if (*s == ':') |
| 6167 | { |
| 6168 | if (*(s + 1) == ':') |
| 6169 | s += 2; |
| 6170 | else |
| 6171 | /* To avoid a mistake in the following situation: |
| 6172 | * A::A(int a, int b) |
| 6173 | * : a(0) // <--not a function decl |
| 6174 | * , b(0) |
| 6175 | * {... |
| 6176 | */ |
| 6177 | return FALSE; |
| 6178 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6179 | else |
| 6180 | ++s; |
| 6181 | } |
| 6182 | if (*s != '(') |
| 6183 | return FALSE; /* ';', ' or " before any () or no '(' */ |
| 6184 | |
| 6185 | while (*s && *s != ';' && *s != '\'' && *s != '"') |
| 6186 | { |
| 6187 | if (*s == ')' && cin_nocode(s + 1)) |
| 6188 | { |
| 6189 | /* ')' at the end: may have found a match |
| 6190 | * Check for he previous line not to end in a backslash: |
| 6191 | * #if defined(x) && \ |
| 6192 | * defined(y) |
| 6193 | */ |
| 6194 | lnum = first_lnum - 1; |
| 6195 | s = ml_get(lnum); |
| 6196 | if (*s == NUL || s[STRLEN(s) - 1] != '\\') |
| 6197 | retval = TRUE; |
| 6198 | goto done; |
| 6199 | } |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6200 | if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6201 | { |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6202 | int comma = (*s == ','); |
| 6203 | |
| 6204 | /* ',' at the end: continue looking in the next line. |
| 6205 | * At the end: check for ',' in the next line, for this style: |
| 6206 | * func(arg1 |
| 6207 | * , arg2) */ |
| 6208 | for (;;) |
| 6209 | { |
| 6210 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 6211 | break; |
| 6212 | s = ml_get(++lnum); |
| 6213 | if (!cin_ispreproc(s)) |
| 6214 | break; |
| 6215 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6216 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 6217 | break; |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6218 | /* Require a comma at end of the line or a comma or ')' at the |
| 6219 | * start of next line. */ |
| 6220 | s = skipwhite(s); |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6221 | if (!just_started && (!comma && *s != ',' && *s != ')')) |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6222 | break; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6223 | just_started = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6224 | } |
| 6225 | else if (cin_iscomment(s)) /* ignore comments */ |
| 6226 | s = cin_skipcomment(s); |
| 6227 | else |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6228 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6229 | ++s; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6230 | just_started = FALSE; |
| 6231 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6232 | } |
| 6233 | |
| 6234 | done: |
| 6235 | if (lnum != first_lnum && sp != NULL) |
| 6236 | *sp = ml_get(first_lnum); |
| 6237 | |
| 6238 | return retval; |
| 6239 | } |
| 6240 | |
| 6241 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6242 | cin_isif(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6243 | { |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6244 | return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2])); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6245 | } |
| 6246 | |
| 6247 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6248 | cin_iselse( |
| 6249 | char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6250 | { |
| 6251 | if (*p == '}') /* accept "} else" */ |
| 6252 | p = cin_skipcomment(p + 1); |
| 6253 | return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4])); |
| 6254 | } |
| 6255 | |
| 6256 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6257 | cin_isdo(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6258 | { |
| 6259 | return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2])); |
| 6260 | } |
| 6261 | |
| 6262 | /* |
| 6263 | * Check if this is a "while" that should have a matching "do". |
| 6264 | * We only accept a "while (condition) ;", with only white space between the |
| 6265 | * ')' and ';'. The condition may be spread over several lines. |
| 6266 | */ |
| 6267 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6268 | cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6269 | { |
| 6270 | pos_T cursor_save; |
| 6271 | pos_T *trypos; |
| 6272 | int retval = FALSE; |
| 6273 | |
| 6274 | p = cin_skipcomment(p); |
| 6275 | if (*p == '}') /* accept "} while (cond);" */ |
| 6276 | p = cin_skipcomment(p + 1); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6277 | if (cin_starts_with(p, "while")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6278 | { |
| 6279 | cursor_save = curwin->w_cursor; |
| 6280 | curwin->w_cursor.lnum = lnum; |
| 6281 | curwin->w_cursor.col = 0; |
| 6282 | p = ml_get_curline(); |
| 6283 | while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */ |
| 6284 | { |
| 6285 | ++p; |
| 6286 | ++curwin->w_cursor.col; |
| 6287 | } |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6288 | if ((trypos = findmatchlimit(NULL, 0, 0, |
| 6289 | curbuf->b_ind_maxparen)) != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6290 | && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';') |
| 6291 | retval = TRUE; |
| 6292 | curwin->w_cursor = cursor_save; |
| 6293 | } |
| 6294 | return retval; |
| 6295 | } |
| 6296 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6297 | /* |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 6298 | * Check whether in "p" there is an "if", "for" or "while" before "*poffset". |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6299 | * Return 0 if there is none. |
| 6300 | * Otherwise return !0 and update "*poffset" to point to the place where the |
| 6301 | * string was found. |
| 6302 | */ |
| 6303 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6304 | cin_is_if_for_while_before_offset(char_u *line, int *poffset) |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6305 | { |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 6306 | int offset = *poffset; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6307 | |
| 6308 | if (offset-- < 2) |
| 6309 | return 0; |
| 6310 | while (offset > 2 && vim_iswhite(line[offset])) |
| 6311 | --offset; |
| 6312 | |
| 6313 | offset -= 1; |
| 6314 | if (!STRNCMP(line + offset, "if", 2)) |
| 6315 | goto probablyFound; |
| 6316 | |
| 6317 | if (offset >= 1) |
| 6318 | { |
| 6319 | offset -= 1; |
| 6320 | if (!STRNCMP(line + offset, "for", 3)) |
| 6321 | goto probablyFound; |
| 6322 | |
| 6323 | if (offset >= 2) |
| 6324 | { |
| 6325 | offset -= 2; |
| 6326 | if (!STRNCMP(line + offset, "while", 5)) |
| 6327 | goto probablyFound; |
| 6328 | } |
| 6329 | } |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6330 | return 0; |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 6331 | |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6332 | probablyFound: |
| 6333 | if (!offset || !vim_isIDc(line[offset - 1])) |
| 6334 | { |
| 6335 | *poffset = offset; |
| 6336 | return 1; |
| 6337 | } |
| 6338 | return 0; |
| 6339 | } |
| 6340 | |
| 6341 | /* |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6342 | * Return TRUE if we are at the end of a do-while. |
| 6343 | * do |
| 6344 | * nothing; |
| 6345 | * while (foo |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 6346 | * && bar); <-- here |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6347 | * Adjust the cursor to the line with "while". |
| 6348 | */ |
| 6349 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6350 | cin_iswhileofdo_end(int terminated) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6351 | { |
| 6352 | char_u *line; |
| 6353 | char_u *p; |
| 6354 | char_u *s; |
| 6355 | pos_T *trypos; |
| 6356 | int i; |
| 6357 | |
| 6358 | if (terminated != ';') /* there must be a ';' at the end */ |
| 6359 | return FALSE; |
| 6360 | |
| 6361 | p = line = ml_get_curline(); |
| 6362 | while (*p != NUL) |
| 6363 | { |
| 6364 | p = cin_skipcomment(p); |
| 6365 | if (*p == ')') |
| 6366 | { |
| 6367 | s = skipwhite(p + 1); |
| 6368 | if (*s == ';' && cin_nocode(s + 1)) |
| 6369 | { |
| 6370 | /* Found ");" at end of the line, now check there is "while" |
| 6371 | * before the matching '('. XXX */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6372 | i = (int)(p - line); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6373 | curwin->w_cursor.col = i; |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6374 | trypos = find_match_paren(curbuf->b_ind_maxparen); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6375 | if (trypos != NULL) |
| 6376 | { |
| 6377 | s = cin_skipcomment(ml_get(trypos->lnum)); |
| 6378 | if (*s == '}') /* accept "} while (cond);" */ |
| 6379 | s = cin_skipcomment(s + 1); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6380 | if (cin_starts_with(s, "while")) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6381 | { |
| 6382 | curwin->w_cursor.lnum = trypos->lnum; |
| 6383 | return TRUE; |
| 6384 | } |
| 6385 | } |
| 6386 | |
| 6387 | /* Searching may have made "line" invalid, get it again. */ |
| 6388 | line = ml_get_curline(); |
| 6389 | p = line + i; |
| 6390 | } |
| 6391 | } |
| 6392 | if (*p != NUL) |
| 6393 | ++p; |
| 6394 | } |
| 6395 | return FALSE; |
| 6396 | } |
| 6397 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6398 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6399 | cin_isbreak(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6400 | { |
| 6401 | return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5])); |
| 6402 | } |
| 6403 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6404 | /* |
| 6405 | * Find the position of a C++ base-class declaration or |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6406 | * constructor-initialization. eg: |
| 6407 | * |
| 6408 | * class MyClass : |
| 6409 | * baseClass <-- here |
| 6410 | * class MyClass : public baseClass, |
| 6411 | * anotherBaseClass <-- here (should probably lineup ??) |
| 6412 | * MyClass::MyClass(...) : |
| 6413 | * baseClass(...) <-- here (constructor-initialization) |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 6414 | * |
| 6415 | * This is a lot of guessing. Watch out for "cond ? func() : foo". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6416 | */ |
| 6417 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6418 | cin_is_cpp_baseclass( |
| 6419 | cpp_baseclass_cache_T *cached) /* input and output */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6420 | { |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6421 | lpos_T *pos = &cached->lpos; /* find position */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6422 | char_u *s; |
| 6423 | int class_or_struct, lookfor_ctor_init, cpp_base_class; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6424 | linenr_T lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6425 | char_u *line = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6426 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6427 | if (pos->lnum <= lnum) |
| 6428 | return cached->found; /* Use the cached result */ |
| 6429 | |
| 6430 | pos->col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6431 | |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 6432 | s = skipwhite(line); |
| 6433 | if (*s == '#') /* skip #define FOO x ? (x) : x */ |
| 6434 | return FALSE; |
| 6435 | s = cin_skipcomment(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6436 | if (*s == NUL) |
| 6437 | return FALSE; |
| 6438 | |
| 6439 | cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; |
| 6440 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6441 | /* Search for a line starting with '#', empty, ending in ';' or containing |
| 6442 | * '{' or '}' and start below it. This handles the following situations: |
| 6443 | * a = cond ? |
| 6444 | * func() : |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 6445 | * asdf; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6446 | * func::foo() |
| 6447 | * : something |
| 6448 | * {} |
| 6449 | * Foo::Foo (int one, int two) |
| 6450 | * : something(4), |
| 6451 | * somethingelse(3) |
| 6452 | * {} |
| 6453 | */ |
| 6454 | while (lnum > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6455 | { |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6456 | line = ml_get(lnum - 1); |
| 6457 | s = skipwhite(line); |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6458 | if (*s == '#' || *s == NUL) |
| 6459 | break; |
| 6460 | while (*s != NUL) |
| 6461 | { |
| 6462 | s = cin_skipcomment(s); |
| 6463 | if (*s == '{' || *s == '}' |
| 6464 | || (*s == ';' && cin_nocode(s + 1))) |
| 6465 | break; |
| 6466 | if (*s != NUL) |
| 6467 | ++s; |
| 6468 | } |
| 6469 | if (*s != NUL) |
| 6470 | break; |
| 6471 | --lnum; |
| 6472 | } |
| 6473 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6474 | pos->lnum = lnum; |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6475 | line = ml_get(lnum); |
Bram Moolenaar | d1b15de | 2015-10-13 16:13:39 +0200 | [diff] [blame] | 6476 | s = line; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6477 | for (;;) |
| 6478 | { |
| 6479 | if (*s == NUL) |
| 6480 | { |
| 6481 | if (lnum == curwin->w_cursor.lnum) |
| 6482 | break; |
| 6483 | /* Continue in the cursor line. */ |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6484 | line = ml_get(++lnum); |
Bram Moolenaar | d1b15de | 2015-10-13 16:13:39 +0200 | [diff] [blame] | 6485 | s = line; |
| 6486 | } |
| 6487 | if (s == line) |
| 6488 | { |
| 6489 | /* don't recognize "case (foo):" as a baseclass */ |
| 6490 | if (cin_iscase(s, FALSE)) |
| 6491 | break; |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6492 | s = cin_skipcomment(line); |
| 6493 | if (*s == NUL) |
| 6494 | continue; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6495 | } |
| 6496 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 6497 | if (s[0] == '"' || (s[0] == 'R' && s[1] == '"')) |
Bram Moolenaar | aede6ce | 2011-05-10 11:56:30 +0200 | [diff] [blame] | 6498 | s = skip_string(s) + 1; |
| 6499 | else if (s[0] == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6500 | { |
| 6501 | if (s[1] == ':') |
| 6502 | { |
| 6503 | /* skip double colon. It can't be a constructor |
| 6504 | * initialization any more */ |
| 6505 | lookfor_ctor_init = FALSE; |
| 6506 | s = cin_skipcomment(s + 2); |
| 6507 | } |
| 6508 | else if (lookfor_ctor_init || class_or_struct) |
| 6509 | { |
| 6510 | /* we have something found, that looks like the start of |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 6511 | * cpp-base-class-declaration or constructor-initialization */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6512 | cpp_base_class = TRUE; |
| 6513 | lookfor_ctor_init = class_or_struct = FALSE; |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6514 | pos->col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6515 | s = cin_skipcomment(s + 1); |
| 6516 | } |
| 6517 | else |
| 6518 | s = cin_skipcomment(s + 1); |
| 6519 | } |
| 6520 | else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5])) |
| 6521 | || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6]))) |
| 6522 | { |
| 6523 | class_or_struct = TRUE; |
| 6524 | lookfor_ctor_init = FALSE; |
| 6525 | |
| 6526 | if (*s == 'c') |
| 6527 | s = cin_skipcomment(s + 5); |
| 6528 | else |
| 6529 | s = cin_skipcomment(s + 6); |
| 6530 | } |
| 6531 | else |
| 6532 | { |
| 6533 | if (s[0] == '{' || s[0] == '}' || s[0] == ';') |
| 6534 | { |
| 6535 | cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; |
| 6536 | } |
| 6537 | else if (s[0] == ')') |
| 6538 | { |
| 6539 | /* Constructor-initialization is assumed if we come across |
| 6540 | * something like "):" */ |
| 6541 | class_or_struct = FALSE; |
| 6542 | lookfor_ctor_init = TRUE; |
| 6543 | } |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 6544 | else if (s[0] == '?') |
| 6545 | { |
| 6546 | /* Avoid seeing '() :' after '?' as constructor init. */ |
| 6547 | return FALSE; |
| 6548 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6549 | else if (!vim_isIDc(s[0])) |
| 6550 | { |
| 6551 | /* if it is not an identifier, we are wrong */ |
| 6552 | class_or_struct = FALSE; |
| 6553 | lookfor_ctor_init = FALSE; |
| 6554 | } |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6555 | else if (pos->col == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6556 | { |
| 6557 | /* it can't be a constructor-initialization any more */ |
| 6558 | lookfor_ctor_init = FALSE; |
| 6559 | |
| 6560 | /* the first statement starts here: lineup with this one... */ |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6561 | if (cpp_base_class) |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6562 | pos->col = (colnr_T)(s - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6563 | } |
| 6564 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6565 | /* When the line ends in a comma don't align with it. */ |
| 6566 | if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1)) |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6567 | pos->col = 0; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6568 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6569 | s = cin_skipcomment(s + 1); |
| 6570 | } |
| 6571 | } |
| 6572 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6573 | cached->found = cpp_base_class; |
| 6574 | if (cpp_base_class) |
| 6575 | pos->lnum = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6576 | return cpp_base_class; |
| 6577 | } |
| 6578 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6579 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6580 | get_baseclass_amount(int col) |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6581 | { |
| 6582 | int amount; |
| 6583 | colnr_T vcol; |
| 6584 | pos_T *trypos; |
| 6585 | |
| 6586 | if (col == 0) |
| 6587 | { |
| 6588 | amount = get_indent(); |
| 6589 | if (find_last_paren(ml_get_curline(), '(', ')') |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6590 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6591 | amount = get_indent_lnum(trypos->lnum); /* XXX */ |
| 6592 | if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL)) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6593 | amount += curbuf->b_ind_cpp_baseclass; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6594 | } |
| 6595 | else |
| 6596 | { |
| 6597 | curwin->w_cursor.col = col; |
| 6598 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 6599 | amount = (int)vcol; |
| 6600 | } |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6601 | if (amount < curbuf->b_ind_cpp_baseclass) |
| 6602 | amount = curbuf->b_ind_cpp_baseclass; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6603 | return amount; |
| 6604 | } |
| 6605 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6606 | /* |
| 6607 | * Return TRUE if string "s" ends with the string "find", possibly followed by |
| 6608 | * white space and comments. Skip strings and comments. |
| 6609 | * Ignore "ignore" after "find" if it's not NULL. |
| 6610 | */ |
| 6611 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6612 | cin_ends_in(char_u *s, char_u *find, char_u *ignore) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6613 | { |
| 6614 | char_u *p = s; |
| 6615 | char_u *r; |
| 6616 | int len = (int)STRLEN(find); |
| 6617 | |
| 6618 | while (*p != NUL) |
| 6619 | { |
| 6620 | p = cin_skipcomment(p); |
| 6621 | if (STRNCMP(p, find, len) == 0) |
| 6622 | { |
| 6623 | r = skipwhite(p + len); |
| 6624 | if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0) |
| 6625 | r = skipwhite(r + STRLEN(ignore)); |
| 6626 | if (cin_nocode(r)) |
| 6627 | return TRUE; |
| 6628 | } |
| 6629 | if (*p != NUL) |
| 6630 | ++p; |
| 6631 | } |
| 6632 | return FALSE; |
| 6633 | } |
| 6634 | |
| 6635 | /* |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6636 | * Return TRUE when "s" starts with "word" and then a non-ID character. |
| 6637 | */ |
| 6638 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6639 | cin_starts_with(char_u *s, char *word) |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6640 | { |
Bram Moolenaar | b3cb982 | 2013-03-13 17:01:52 +0100 | [diff] [blame] | 6641 | int l = (int)STRLEN(word); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6642 | |
| 6643 | return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l])); |
| 6644 | } |
| 6645 | |
| 6646 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6647 | * Skip strings, chars and comments until at or past "trypos". |
| 6648 | * Return the column found. |
| 6649 | */ |
| 6650 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6651 | cin_skip2pos(pos_T *trypos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6652 | { |
| 6653 | char_u *line; |
| 6654 | char_u *p; |
| 6655 | |
| 6656 | p = line = ml_get(trypos->lnum); |
| 6657 | while (*p && (colnr_T)(p - line) < trypos->col) |
| 6658 | { |
| 6659 | if (cin_iscomment(p)) |
| 6660 | p = cin_skipcomment(p); |
| 6661 | else |
| 6662 | { |
| 6663 | p = skip_string(p); |
| 6664 | ++p; |
| 6665 | } |
| 6666 | } |
| 6667 | return (int)(p - line); |
| 6668 | } |
| 6669 | |
| 6670 | /* |
| 6671 | * Find the '{' at the start of the block we are in. |
| 6672 | * Return NULL if no match found. |
| 6673 | * Ignore a '{' that is in a comment, makes indenting the next three lines |
| 6674 | * work. */ |
| 6675 | /* foo() */ |
| 6676 | /* { */ |
| 6677 | /* } */ |
| 6678 | |
| 6679 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6680 | find_start_brace(void) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6681 | { |
| 6682 | pos_T cursor_save; |
| 6683 | pos_T *trypos; |
| 6684 | pos_T *pos; |
| 6685 | static pos_T pos_copy; |
| 6686 | |
| 6687 | cursor_save = curwin->w_cursor; |
| 6688 | while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL) |
| 6689 | { |
| 6690 | pos_copy = *trypos; /* copy pos_T, next findmatch will change it */ |
| 6691 | trypos = &pos_copy; |
| 6692 | curwin->w_cursor = *trypos; |
| 6693 | pos = NULL; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6694 | /* ignore the { if it's in a // or / * * / comment */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6695 | if ((colnr_T)cin_skip2pos(trypos) == trypos->col |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 6696 | && (pos = ind_find_start_CORS()) == NULL) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6697 | break; |
| 6698 | if (pos != NULL) |
| 6699 | curwin->w_cursor.lnum = pos->lnum; |
| 6700 | } |
| 6701 | curwin->w_cursor = cursor_save; |
| 6702 | return trypos; |
| 6703 | } |
| 6704 | |
| 6705 | /* |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 6706 | * Find the matching '(', ignoring it if it is in a comment. |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6707 | * Return NULL if no match found. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6708 | */ |
| 6709 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6710 | find_match_paren(int ind_maxparen) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6711 | { |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6712 | return find_match_char('(', ind_maxparen); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 6713 | } |
| 6714 | |
| 6715 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6716 | find_match_char (int c, int ind_maxparen) /* XXX */ |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 6717 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6718 | pos_T cursor_save; |
| 6719 | pos_T *trypos; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6720 | static pos_T pos_copy; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6721 | int ind_maxp_wk; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6722 | |
| 6723 | cursor_save = curwin->w_cursor; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6724 | ind_maxp_wk = ind_maxparen; |
| 6725 | retry: |
| 6726 | if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6727 | { |
| 6728 | /* check if the ( is in a // comment */ |
| 6729 | if ((colnr_T)cin_skip2pos(trypos) > trypos->col) |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6730 | { |
| 6731 | ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum); |
| 6732 | if (ind_maxp_wk > 0) |
| 6733 | { |
| 6734 | curwin->w_cursor = *trypos; |
| 6735 | curwin->w_cursor.col = 0; /* XXX */ |
| 6736 | goto retry; |
| 6737 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6738 | trypos = NULL; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6739 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6740 | else |
| 6741 | { |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6742 | pos_T *trypos_wk; |
| 6743 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6744 | pos_copy = *trypos; /* copy trypos, findmatch will change it */ |
| 6745 | trypos = &pos_copy; |
| 6746 | curwin->w_cursor = *trypos; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 6747 | if ((trypos_wk = ind_find_start_CORS()) != NULL) /* XXX */ |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6748 | { |
| 6749 | ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum |
| 6750 | - trypos_wk->lnum); |
| 6751 | if (ind_maxp_wk > 0) |
| 6752 | { |
| 6753 | curwin->w_cursor = *trypos_wk; |
| 6754 | goto retry; |
| 6755 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6756 | trypos = NULL; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6757 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6758 | } |
| 6759 | } |
| 6760 | curwin->w_cursor = cursor_save; |
| 6761 | return trypos; |
| 6762 | } |
| 6763 | |
| 6764 | /* |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 6765 | * Find the matching '(', ignoring it if it is in a comment or before an |
| 6766 | * unmatched {. |
| 6767 | * Return NULL if no match found. |
| 6768 | */ |
| 6769 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6770 | find_match_paren_after_brace (int ind_maxparen) /* XXX */ |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 6771 | { |
| 6772 | pos_T *trypos = find_match_paren(ind_maxparen); |
| 6773 | |
| 6774 | if (trypos != NULL) |
| 6775 | { |
| 6776 | pos_T *tryposBrace = find_start_brace(); |
| 6777 | |
| 6778 | /* If both an unmatched '(' and '{' is found. Ignore the '(' |
| 6779 | * position if the '{' is further down. */ |
| 6780 | if (tryposBrace != NULL |
| 6781 | && (trypos->lnum != tryposBrace->lnum |
| 6782 | ? trypos->lnum < tryposBrace->lnum |
| 6783 | : trypos->col < tryposBrace->col)) |
| 6784 | trypos = NULL; |
| 6785 | } |
| 6786 | return trypos; |
| 6787 | } |
| 6788 | |
| 6789 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6790 | * Return ind_maxparen corrected for the difference in line number between the |
| 6791 | * cursor position and "startpos". This makes sure that searching for a |
| 6792 | * matching paren above the cursor line doesn't find a match because of |
| 6793 | * looking a few lines further. |
| 6794 | */ |
| 6795 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6796 | corr_ind_maxparen(pos_T *startpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6797 | { |
| 6798 | long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum; |
| 6799 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6800 | if (n > 0 && n < curbuf->b_ind_maxparen / 2) |
| 6801 | return curbuf->b_ind_maxparen - (int)n; |
| 6802 | return curbuf->b_ind_maxparen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6803 | } |
| 6804 | |
| 6805 | /* |
| 6806 | * Set w_cursor.col to the column number of the last unmatched ')' or '{' in |
Bram Moolenaar | 6d8f9c6 | 2011-11-30 13:03:28 +0100 | [diff] [blame] | 6807 | * line "l". "l" must point to the start of the line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6808 | */ |
| 6809 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6810 | find_last_paren(char_u *l, int start, int end) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6811 | { |
| 6812 | int i; |
| 6813 | int retval = FALSE; |
| 6814 | int open_count = 0; |
| 6815 | |
| 6816 | curwin->w_cursor.col = 0; /* default is start of line */ |
| 6817 | |
Bram Moolenaar | 6d8f9c6 | 2011-11-30 13:03:28 +0100 | [diff] [blame] | 6818 | for (i = 0; l[i] != NUL; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6819 | { |
| 6820 | i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */ |
| 6821 | i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */ |
| 6822 | if (l[i] == start) |
| 6823 | ++open_count; |
| 6824 | else if (l[i] == end) |
| 6825 | { |
| 6826 | if (open_count > 0) |
| 6827 | --open_count; |
| 6828 | else |
| 6829 | { |
| 6830 | curwin->w_cursor.col = i; |
| 6831 | retval = TRUE; |
| 6832 | } |
| 6833 | } |
| 6834 | } |
| 6835 | return retval; |
| 6836 | } |
| 6837 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 6838 | /* |
| 6839 | * Parse 'cinoptions' and set the values in "curbuf". |
| 6840 | * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes. |
| 6841 | */ |
| 6842 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6843 | parse_cino(buf_T *buf) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 6844 | { |
| 6845 | char_u *p; |
| 6846 | char_u *l; |
| 6847 | char_u *digits; |
| 6848 | int n; |
| 6849 | int divider; |
| 6850 | int fraction = 0; |
| 6851 | int sw = (int)get_sw_value(buf); |
| 6852 | |
| 6853 | /* |
| 6854 | * Set the default values. |
| 6855 | */ |
| 6856 | /* Spaces from a block's opening brace the prevailing indent for that |
| 6857 | * block should be. */ |
| 6858 | buf->b_ind_level = sw; |
| 6859 | |
| 6860 | /* Spaces from the edge of the line an open brace that's at the end of a |
| 6861 | * line is imagined to be. */ |
| 6862 | buf->b_ind_open_imag = 0; |
| 6863 | |
| 6864 | /* Spaces from the prevailing indent for a line that is not preceded by |
| 6865 | * an opening brace. */ |
| 6866 | buf->b_ind_no_brace = 0; |
| 6867 | |
| 6868 | /* Column where the first { of a function should be located }. */ |
| 6869 | buf->b_ind_first_open = 0; |
| 6870 | |
| 6871 | /* Spaces from the prevailing indent a leftmost open brace should be |
| 6872 | * located. */ |
| 6873 | buf->b_ind_open_extra = 0; |
| 6874 | |
| 6875 | /* Spaces from the matching open brace (real location for one at the left |
| 6876 | * edge; imaginary location from one that ends a line) the matching close |
| 6877 | * brace should be located. */ |
| 6878 | buf->b_ind_close_extra = 0; |
| 6879 | |
| 6880 | /* Spaces from the edge of the line an open brace sitting in the leftmost |
| 6881 | * column is imagined to be. */ |
| 6882 | buf->b_ind_open_left_imag = 0; |
| 6883 | |
| 6884 | /* Spaces jump labels should be shifted to the left if N is non-negative, |
| 6885 | * otherwise the jump label will be put to column 1. */ |
| 6886 | buf->b_ind_jump_label = -1; |
| 6887 | |
| 6888 | /* Spaces from the switch() indent a "case xx" label should be located. */ |
| 6889 | buf->b_ind_case = sw; |
| 6890 | |
| 6891 | /* Spaces from the "case xx:" code after a switch() should be located. */ |
| 6892 | buf->b_ind_case_code = sw; |
| 6893 | |
| 6894 | /* Lineup break at end of case in switch() with case label. */ |
| 6895 | buf->b_ind_case_break = 0; |
| 6896 | |
| 6897 | /* Spaces from the class declaration indent a scope declaration label |
| 6898 | * should be located. */ |
| 6899 | buf->b_ind_scopedecl = sw; |
| 6900 | |
| 6901 | /* Spaces from the scope declaration label code should be located. */ |
| 6902 | buf->b_ind_scopedecl_code = sw; |
| 6903 | |
| 6904 | /* Amount K&R-style parameters should be indented. */ |
| 6905 | buf->b_ind_param = sw; |
| 6906 | |
| 6907 | /* Amount a function type spec should be indented. */ |
| 6908 | buf->b_ind_func_type = sw; |
| 6909 | |
| 6910 | /* Amount a cpp base class declaration or constructor initialization |
| 6911 | * should be indented. */ |
| 6912 | buf->b_ind_cpp_baseclass = sw; |
| 6913 | |
| 6914 | /* additional spaces beyond the prevailing indent a continuation line |
| 6915 | * should be located. */ |
| 6916 | buf->b_ind_continuation = sw; |
| 6917 | |
| 6918 | /* Spaces from the indent of the line with an unclosed parentheses. */ |
| 6919 | buf->b_ind_unclosed = sw * 2; |
| 6920 | |
| 6921 | /* Spaces from the indent of the line with an unclosed parentheses, which |
| 6922 | * itself is also unclosed. */ |
| 6923 | buf->b_ind_unclosed2 = sw; |
| 6924 | |
| 6925 | /* Suppress ignoring spaces from the indent of a line starting with an |
| 6926 | * unclosed parentheses. */ |
| 6927 | buf->b_ind_unclosed_noignore = 0; |
| 6928 | |
| 6929 | /* If the opening paren is the last nonwhite character on the line, and |
| 6930 | * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer |
| 6931 | * context (for very long lines). */ |
| 6932 | buf->b_ind_unclosed_wrapped = 0; |
| 6933 | |
| 6934 | /* Suppress ignoring white space when lining up with the character after |
| 6935 | * an unclosed parentheses. */ |
| 6936 | buf->b_ind_unclosed_whiteok = 0; |
| 6937 | |
| 6938 | /* Indent a closing parentheses under the line start of the matching |
| 6939 | * opening parentheses. */ |
| 6940 | buf->b_ind_matching_paren = 0; |
| 6941 | |
| 6942 | /* Indent a closing parentheses under the previous line. */ |
| 6943 | buf->b_ind_paren_prev = 0; |
| 6944 | |
| 6945 | /* Extra indent for comments. */ |
| 6946 | buf->b_ind_comment = 0; |
| 6947 | |
| 6948 | /* Spaces from the comment opener when there is nothing after it. */ |
| 6949 | buf->b_ind_in_comment = 3; |
| 6950 | |
| 6951 | /* Boolean: if non-zero, use b_ind_in_comment even if there is something |
| 6952 | * after the comment opener. */ |
| 6953 | buf->b_ind_in_comment2 = 0; |
| 6954 | |
| 6955 | /* Max lines to search for an open paren. */ |
| 6956 | buf->b_ind_maxparen = 20; |
| 6957 | |
| 6958 | /* Max lines to search for an open comment. */ |
| 6959 | buf->b_ind_maxcomment = 70; |
| 6960 | |
| 6961 | /* Handle braces for java code. */ |
| 6962 | buf->b_ind_java = 0; |
| 6963 | |
| 6964 | /* Not to confuse JS object properties with labels. */ |
| 6965 | buf->b_ind_js = 0; |
| 6966 | |
| 6967 | /* Handle blocked cases correctly. */ |
| 6968 | buf->b_ind_keep_case_label = 0; |
| 6969 | |
| 6970 | /* Handle C++ namespace. */ |
| 6971 | buf->b_ind_cpp_namespace = 0; |
| 6972 | |
| 6973 | /* Handle continuation lines containing conditions of if(), for() and |
| 6974 | * while(). */ |
| 6975 | buf->b_ind_if_for_while = 0; |
| 6976 | |
| 6977 | for (p = buf->b_p_cino; *p; ) |
| 6978 | { |
| 6979 | l = p++; |
| 6980 | if (*p == '-') |
| 6981 | ++p; |
| 6982 | digits = p; /* remember where the digits start */ |
| 6983 | n = getdigits(&p); |
| 6984 | divider = 0; |
| 6985 | if (*p == '.') /* ".5s" means a fraction */ |
| 6986 | { |
| 6987 | fraction = atol((char *)++p); |
| 6988 | while (VIM_ISDIGIT(*p)) |
| 6989 | { |
| 6990 | ++p; |
| 6991 | if (divider) |
| 6992 | divider *= 10; |
| 6993 | else |
| 6994 | divider = 10; |
| 6995 | } |
| 6996 | } |
| 6997 | if (*p == 's') /* "2s" means two times 'shiftwidth' */ |
| 6998 | { |
| 6999 | if (p == digits) |
| 7000 | n = sw; /* just "s" is one 'shiftwidth' */ |
| 7001 | else |
| 7002 | { |
| 7003 | n *= sw; |
| 7004 | if (divider) |
| 7005 | n += (sw * fraction + divider / 2) / divider; |
| 7006 | } |
| 7007 | ++p; |
| 7008 | } |
| 7009 | if (l[1] == '-') |
| 7010 | n = -n; |
| 7011 | |
| 7012 | /* When adding an entry here, also update the default 'cinoptions' in |
| 7013 | * doc/indent.txt, and add explanation for it! */ |
| 7014 | switch (*l) |
| 7015 | { |
| 7016 | case '>': buf->b_ind_level = n; break; |
| 7017 | case 'e': buf->b_ind_open_imag = n; break; |
| 7018 | case 'n': buf->b_ind_no_brace = n; break; |
| 7019 | case 'f': buf->b_ind_first_open = n; break; |
| 7020 | case '{': buf->b_ind_open_extra = n; break; |
| 7021 | case '}': buf->b_ind_close_extra = n; break; |
| 7022 | case '^': buf->b_ind_open_left_imag = n; break; |
| 7023 | case 'L': buf->b_ind_jump_label = n; break; |
| 7024 | case ':': buf->b_ind_case = n; break; |
| 7025 | case '=': buf->b_ind_case_code = n; break; |
| 7026 | case 'b': buf->b_ind_case_break = n; break; |
| 7027 | case 'p': buf->b_ind_param = n; break; |
| 7028 | case 't': buf->b_ind_func_type = n; break; |
| 7029 | case '/': buf->b_ind_comment = n; break; |
| 7030 | case 'c': buf->b_ind_in_comment = n; break; |
| 7031 | case 'C': buf->b_ind_in_comment2 = n; break; |
| 7032 | case 'i': buf->b_ind_cpp_baseclass = n; break; |
| 7033 | case '+': buf->b_ind_continuation = n; break; |
| 7034 | case '(': buf->b_ind_unclosed = n; break; |
| 7035 | case 'u': buf->b_ind_unclosed2 = n; break; |
| 7036 | case 'U': buf->b_ind_unclosed_noignore = n; break; |
| 7037 | case 'W': buf->b_ind_unclosed_wrapped = n; break; |
| 7038 | case 'w': buf->b_ind_unclosed_whiteok = n; break; |
| 7039 | case 'm': buf->b_ind_matching_paren = n; break; |
| 7040 | case 'M': buf->b_ind_paren_prev = n; break; |
| 7041 | case ')': buf->b_ind_maxparen = n; break; |
| 7042 | case '*': buf->b_ind_maxcomment = n; break; |
| 7043 | case 'g': buf->b_ind_scopedecl = n; break; |
| 7044 | case 'h': buf->b_ind_scopedecl_code = n; break; |
| 7045 | case 'j': buf->b_ind_java = n; break; |
| 7046 | case 'J': buf->b_ind_js = n; break; |
| 7047 | case 'l': buf->b_ind_keep_case_label = n; break; |
| 7048 | case '#': buf->b_ind_hash_comment = n; break; |
| 7049 | case 'N': buf->b_ind_cpp_namespace = n; break; |
| 7050 | case 'k': buf->b_ind_if_for_while = n; break; |
| 7051 | } |
| 7052 | if (*p == ',') |
| 7053 | ++p; |
| 7054 | } |
| 7055 | } |
| 7056 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7057 | /* |
| 7058 | * Return the desired indent for C code. |
| 7059 | * Return -1 if the indent should be left alone (inside a raw string). |
| 7060 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7061 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7062 | get_c_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7063 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7064 | pos_T cur_curpos; |
| 7065 | int amount; |
| 7066 | int scope_amount; |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 7067 | int cur_amount = MAXCOL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7068 | colnr_T col; |
| 7069 | char_u *theline; |
| 7070 | char_u *linecopy; |
| 7071 | pos_T *trypos; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7072 | pos_T *comment_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7073 | pos_T *tryposBrace = NULL; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7074 | pos_T tryposCopy; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7075 | pos_T our_paren_pos; |
| 7076 | char_u *start; |
| 7077 | int start_brace; |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 7078 | #define BRACE_IN_COL0 1 /* '{' is in column 0 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7079 | #define BRACE_AT_START 2 /* '{' is at start of line */ |
| 7080 | #define BRACE_AT_END 3 /* '{' is at end of line */ |
| 7081 | linenr_T ourscope; |
| 7082 | char_u *l; |
| 7083 | char_u *look; |
| 7084 | char_u terminated; |
| 7085 | int lookfor; |
| 7086 | #define LOOKFOR_INITIAL 0 |
| 7087 | #define LOOKFOR_IF 1 |
| 7088 | #define LOOKFOR_DO 2 |
| 7089 | #define LOOKFOR_CASE 3 |
| 7090 | #define LOOKFOR_ANY 4 |
| 7091 | #define LOOKFOR_TERM 5 |
| 7092 | #define LOOKFOR_UNTERM 6 |
| 7093 | #define LOOKFOR_SCOPEDECL 7 |
| 7094 | #define LOOKFOR_NOBREAK 8 |
| 7095 | #define LOOKFOR_CPP_BASECLASS 9 |
| 7096 | #define LOOKFOR_ENUM_OR_INIT 10 |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7097 | #define LOOKFOR_JS_KEY 11 |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 7098 | #define LOOKFOR_COMMA 12 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7099 | |
| 7100 | int whilelevel; |
| 7101 | linenr_T lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7102 | int n; |
| 7103 | int iscase; |
| 7104 | int lookfor_break; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 7105 | int lookfor_cpp_namespace = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7106 | int cont_amount = 0; /* amount for continuation line */ |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 7107 | int original_line_islabel; |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 7108 | int added_to_amount = 0; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7109 | int js_cur_has_key = 0; |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 7110 | cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } }; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7111 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7112 | /* make a copy, value is changed below */ |
| 7113 | int ind_continuation = curbuf->b_ind_continuation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7114 | |
| 7115 | /* remember where the cursor was when we started */ |
| 7116 | cur_curpos = curwin->w_cursor; |
| 7117 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7118 | /* if we are at line 1 zero indent is fine, right? */ |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 7119 | if (cur_curpos.lnum == 1) |
| 7120 | return 0; |
| 7121 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7122 | /* Get a copy of the current contents of the line. |
| 7123 | * This is required, because only the most recent line obtained with |
| 7124 | * ml_get is valid! */ |
| 7125 | linecopy = vim_strsave(ml_get(cur_curpos.lnum)); |
| 7126 | if (linecopy == NULL) |
| 7127 | return 0; |
| 7128 | |
| 7129 | /* |
| 7130 | * In insert mode and the cursor is on a ')' truncate the line at the |
| 7131 | * cursor position. We don't want to line up with the matching '(' when |
| 7132 | * inserting new stuff. |
| 7133 | * For unknown reasons the cursor might be past the end of the line, thus |
| 7134 | * check for that. |
| 7135 | */ |
| 7136 | if ((State & INSERT) |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 7137 | && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7138 | && linecopy[curwin->w_cursor.col] == ')') |
| 7139 | linecopy[curwin->w_cursor.col] = NUL; |
| 7140 | |
| 7141 | theline = skipwhite(linecopy); |
| 7142 | |
| 7143 | /* move the cursor to the start of the line */ |
| 7144 | |
| 7145 | curwin->w_cursor.col = 0; |
| 7146 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7147 | original_line_islabel = cin_islabel(); /* XXX */ |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 7148 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7149 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7150 | * If we are inside a raw string don't change the indent. |
| 7151 | * Ignore a raw string inside a comment. |
| 7152 | */ |
| 7153 | comment_pos = ind_find_start_comment(); |
| 7154 | if (comment_pos != NULL) |
| 7155 | { |
| 7156 | /* findmatchlimit() static pos is overwritten, make a copy */ |
| 7157 | tryposCopy = *comment_pos; |
| 7158 | comment_pos = &tryposCopy; |
| 7159 | } |
| 7160 | trypos = find_start_rawstring(curbuf->b_ind_maxcomment); |
| 7161 | if (trypos != NULL && (comment_pos == NULL || lt(*trypos, *comment_pos))) |
| 7162 | { |
| 7163 | amount = -1; |
| 7164 | goto laterend; |
| 7165 | } |
| 7166 | |
| 7167 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7168 | * #defines and so on always go at the left when included in 'cinkeys'. |
| 7169 | */ |
| 7170 | if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE))) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7171 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7172 | amount = curbuf->b_ind_hash_comment; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7173 | goto theend; |
| 7174 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7175 | |
| 7176 | /* |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 7177 | * Is it a non-case label? Then that goes at the left margin too unless: |
| 7178 | * - JS flag is set. |
| 7179 | * - 'L' item has a positive value. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7180 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7181 | if (original_line_islabel && !curbuf->b_ind_js |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7182 | && curbuf->b_ind_jump_label < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7183 | { |
| 7184 | amount = 0; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7185 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7186 | } |
| 7187 | |
| 7188 | /* |
| 7189 | * If we're inside a "//" comment and there is a "//" comment in a |
| 7190 | * previous line, lineup with that one. |
| 7191 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7192 | if (cin_islinecomment(theline) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7193 | && (trypos = find_line_comment()) != NULL) /* XXX */ |
| 7194 | { |
| 7195 | /* find how indented the line beginning the comment is */ |
| 7196 | getvcol(curwin, trypos, &col, NULL, NULL); |
| 7197 | amount = col; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7198 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7199 | } |
| 7200 | |
| 7201 | /* |
| 7202 | * If we're inside a comment and not looking at the start of the |
| 7203 | * comment, try using the 'comments' option. |
| 7204 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7205 | if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7206 | { |
| 7207 | int lead_start_len = 2; |
| 7208 | int lead_middle_len = 1; |
| 7209 | char_u lead_start[COM_MAX_LEN]; /* start-comment string */ |
| 7210 | char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ |
| 7211 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 7212 | char_u *p; |
| 7213 | int start_align = 0; |
| 7214 | int start_off = 0; |
| 7215 | int done = FALSE; |
| 7216 | |
| 7217 | /* find how indented the line beginning the comment is */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7218 | getvcol(curwin, comment_pos, &col, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7219 | amount = col; |
Bram Moolenaar | 4aa9742 | 2011-04-11 14:27:38 +0200 | [diff] [blame] | 7220 | *lead_start = NUL; |
| 7221 | *lead_middle = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7222 | |
| 7223 | p = curbuf->b_p_com; |
| 7224 | while (*p != NUL) |
| 7225 | { |
| 7226 | int align = 0; |
| 7227 | int off = 0; |
| 7228 | int what = 0; |
| 7229 | |
| 7230 | while (*p != NUL && *p != ':') |
| 7231 | { |
| 7232 | if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE) |
| 7233 | what = *p++; |
| 7234 | else if (*p == COM_LEFT || *p == COM_RIGHT) |
| 7235 | align = *p++; |
| 7236 | else if (VIM_ISDIGIT(*p) || *p == '-') |
| 7237 | off = getdigits(&p); |
| 7238 | else |
| 7239 | ++p; |
| 7240 | } |
| 7241 | |
| 7242 | if (*p == ':') |
| 7243 | ++p; |
| 7244 | (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 7245 | if (what == COM_START) |
| 7246 | { |
| 7247 | STRCPY(lead_start, lead_end); |
| 7248 | lead_start_len = (int)STRLEN(lead_start); |
| 7249 | start_off = off; |
| 7250 | start_align = align; |
| 7251 | } |
| 7252 | else if (what == COM_MIDDLE) |
| 7253 | { |
| 7254 | STRCPY(lead_middle, lead_end); |
| 7255 | lead_middle_len = (int)STRLEN(lead_middle); |
| 7256 | } |
| 7257 | else if (what == COM_END) |
| 7258 | { |
| 7259 | /* If our line starts with the middle comment string, line it |
| 7260 | * up with the comment opener per the 'comments' option. */ |
| 7261 | if (STRNCMP(theline, lead_middle, lead_middle_len) == 0 |
| 7262 | && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0) |
| 7263 | { |
| 7264 | done = TRUE; |
| 7265 | if (curwin->w_cursor.lnum > 1) |
| 7266 | { |
| 7267 | /* If the start comment string matches in the previous |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 7268 | * line, use the indent of that line plus offset. If |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7269 | * the middle comment string matches in the previous |
| 7270 | * line, use the indent of that line. XXX */ |
| 7271 | look = skipwhite(ml_get(curwin->w_cursor.lnum - 1)); |
| 7272 | if (STRNCMP(look, lead_start, lead_start_len) == 0) |
| 7273 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 7274 | else if (STRNCMP(look, lead_middle, |
| 7275 | lead_middle_len) == 0) |
| 7276 | { |
| 7277 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 7278 | break; |
| 7279 | } |
| 7280 | /* If the start comment string doesn't match with the |
| 7281 | * start of the comment, skip this entry. XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7282 | else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7283 | lead_start, lead_start_len) != 0) |
| 7284 | continue; |
| 7285 | } |
| 7286 | if (start_off != 0) |
| 7287 | amount += start_off; |
| 7288 | else if (start_align == COM_RIGHT) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 7289 | amount += vim_strsize(lead_start) |
| 7290 | - vim_strsize(lead_middle); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7291 | break; |
| 7292 | } |
| 7293 | |
| 7294 | /* If our line starts with the end comment string, line it up |
| 7295 | * with the middle comment */ |
| 7296 | if (STRNCMP(theline, lead_middle, lead_middle_len) != 0 |
| 7297 | && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0) |
| 7298 | { |
| 7299 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 7300 | /* XXX */ |
| 7301 | if (off != 0) |
| 7302 | amount += off; |
| 7303 | else if (align == COM_RIGHT) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 7304 | amount += vim_strsize(lead_start) |
| 7305 | - vim_strsize(lead_middle); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7306 | done = TRUE; |
| 7307 | break; |
| 7308 | } |
| 7309 | } |
| 7310 | } |
| 7311 | |
| 7312 | /* If our line starts with an asterisk, line up with the |
| 7313 | * asterisk in the comment opener; otherwise, line up |
| 7314 | * with the first character of the comment text. |
| 7315 | */ |
| 7316 | if (done) |
| 7317 | ; |
| 7318 | else if (theline[0] == '*') |
| 7319 | amount += 1; |
| 7320 | else |
| 7321 | { |
| 7322 | /* |
| 7323 | * If we are more than one line away from the comment opener, take |
| 7324 | * the indent of the previous non-empty line. If 'cino' has "CO" |
| 7325 | * and we are just below the comment opener and there are any |
| 7326 | * white characters after it line up with the text after it; |
| 7327 | * otherwise, add the amount specified by "c" in 'cino' |
| 7328 | */ |
| 7329 | amount = -1; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7330 | for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7331 | { |
| 7332 | if (linewhite(lnum)) /* skip blank lines */ |
| 7333 | continue; |
| 7334 | amount = get_indent_lnum(lnum); /* XXX */ |
| 7335 | break; |
| 7336 | } |
| 7337 | if (amount == -1) /* use the comment opener */ |
| 7338 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7339 | if (!curbuf->b_ind_in_comment2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7340 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7341 | start = ml_get(comment_pos->lnum); |
| 7342 | look = start + comment_pos->col + 2; /* skip / and * */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7343 | if (*look != NUL) /* if something after it */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7344 | comment_pos->col = (colnr_T)(skipwhite(look) - start); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7345 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7346 | getvcol(curwin, comment_pos, &col, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7347 | amount = col; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7348 | if (curbuf->b_ind_in_comment2 || *look == NUL) |
| 7349 | amount += curbuf->b_ind_in_comment; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7350 | } |
| 7351 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7352 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7353 | } |
| 7354 | |
| 7355 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7356 | * Are we looking at a ']' that has a match? |
| 7357 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7358 | if (*skipwhite(theline) == ']' |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7359 | && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL) |
| 7360 | { |
| 7361 | /* align with the line containing the '['. */ |
| 7362 | amount = get_indent_lnum(trypos->lnum); |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7363 | goto theend; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7364 | } |
| 7365 | |
| 7366 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7367 | * Are we inside parentheses or braces? |
| 7368 | */ /* XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7369 | if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7370 | && curbuf->b_ind_java == 0) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7371 | || (tryposBrace = find_start_brace()) != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7372 | || trypos != NULL) |
| 7373 | { |
| 7374 | if (trypos != NULL && tryposBrace != NULL) |
| 7375 | { |
| 7376 | /* Both an unmatched '(' and '{' is found. Use the one which is |
| 7377 | * closer to the current cursor position, set the other to NULL. */ |
| 7378 | if (trypos->lnum != tryposBrace->lnum |
| 7379 | ? trypos->lnum < tryposBrace->lnum |
| 7380 | : trypos->col < tryposBrace->col) |
| 7381 | trypos = NULL; |
| 7382 | else |
| 7383 | tryposBrace = NULL; |
| 7384 | } |
| 7385 | |
| 7386 | if (trypos != NULL) |
| 7387 | { |
| 7388 | /* |
| 7389 | * If the matching paren is more than one line away, use the indent of |
| 7390 | * a previous non-empty line that matches the same paren. |
| 7391 | */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7392 | if (theline[0] == ')' && curbuf->b_ind_paren_prev) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7393 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7394 | /* Line up with the start of the matching paren line. */ |
| 7395 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */ |
| 7396 | } |
| 7397 | else |
| 7398 | { |
| 7399 | amount = -1; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7400 | our_paren_pos = *trypos; |
| 7401 | for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7402 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7403 | l = skipwhite(ml_get(lnum)); |
| 7404 | if (cin_nocode(l)) /* skip comment lines */ |
| 7405 | continue; |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 7406 | if (cin_ispreproc_cont(&l, &lnum, &amount)) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7407 | continue; /* ignore #define, #if, etc. */ |
| 7408 | curwin->w_cursor.lnum = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7409 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7410 | /* Skip a comment or raw string. XXX */ |
| 7411 | if ((trypos = ind_find_start_CORS()) != NULL) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7412 | { |
| 7413 | lnum = trypos->lnum + 1; |
| 7414 | continue; |
| 7415 | } |
| 7416 | |
| 7417 | /* XXX */ |
| 7418 | if ((trypos = find_match_paren( |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7419 | corr_ind_maxparen(&cur_curpos))) != NULL |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7420 | && trypos->lnum == our_paren_pos.lnum |
| 7421 | && trypos->col == our_paren_pos.col) |
| 7422 | { |
| 7423 | amount = get_indent_lnum(lnum); /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7424 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7425 | if (theline[0] == ')') |
| 7426 | { |
| 7427 | if (our_paren_pos.lnum != lnum |
| 7428 | && cur_amount > amount) |
| 7429 | cur_amount = amount; |
| 7430 | amount = -1; |
| 7431 | } |
| 7432 | break; |
| 7433 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7434 | } |
| 7435 | } |
| 7436 | |
| 7437 | /* |
| 7438 | * Line up with line where the matching paren is. XXX |
| 7439 | * If the line starts with a '(' or the indent for unclosed |
| 7440 | * parentheses is zero, line up with the unclosed parentheses. |
| 7441 | */ |
| 7442 | if (amount == -1) |
| 7443 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7444 | int ignore_paren_col = 0; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7445 | int is_if_for_while = 0; |
| 7446 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7447 | if (curbuf->b_ind_if_for_while) |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7448 | { |
| 7449 | /* Look for the outermost opening parenthesis on this line |
| 7450 | * and check whether it belongs to an "if", "for" or "while". */ |
| 7451 | |
| 7452 | pos_T cursor_save = curwin->w_cursor; |
| 7453 | pos_T outermost; |
| 7454 | char_u *line; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7455 | |
| 7456 | trypos = &our_paren_pos; |
| 7457 | do { |
| 7458 | outermost = *trypos; |
| 7459 | curwin->w_cursor.lnum = outermost.lnum; |
| 7460 | curwin->w_cursor.col = outermost.col; |
| 7461 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7462 | trypos = find_match_paren(curbuf->b_ind_maxparen); |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7463 | } while (trypos && trypos->lnum == outermost.lnum); |
| 7464 | |
| 7465 | curwin->w_cursor = cursor_save; |
| 7466 | |
| 7467 | line = ml_get(outermost.lnum); |
| 7468 | |
| 7469 | is_if_for_while = |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 7470 | cin_is_if_for_while_before_offset(line, &outermost.col); |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7471 | } |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7472 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7473 | amount = skip_label(our_paren_pos.lnum, &look); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7474 | look = skipwhite(look); |
| 7475 | if (*look == '(') |
| 7476 | { |
| 7477 | linenr_T save_lnum = curwin->w_cursor.lnum; |
| 7478 | char_u *line; |
| 7479 | int look_col; |
| 7480 | |
| 7481 | /* Ignore a '(' in front of the line that has a match before |
| 7482 | * our matching '('. */ |
| 7483 | curwin->w_cursor.lnum = our_paren_pos.lnum; |
| 7484 | line = ml_get_curline(); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7485 | look_col = (int)(look - line); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7486 | curwin->w_cursor.col = look_col + 1; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7487 | if ((trypos = findmatchlimit(NULL, ')', 0, |
| 7488 | curbuf->b_ind_maxparen)) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7489 | != NULL |
| 7490 | && trypos->lnum == our_paren_pos.lnum |
| 7491 | && trypos->col < our_paren_pos.col) |
| 7492 | ignore_paren_col = trypos->col + 1; |
| 7493 | |
| 7494 | curwin->w_cursor.lnum = save_lnum; |
| 7495 | look = ml_get(our_paren_pos.lnum) + look_col; |
| 7496 | } |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7497 | if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0 |
| 7498 | && is_if_for_while == 0) |
| 7499 | || (!curbuf->b_ind_unclosed_noignore && *look == '(' |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7500 | && ignore_paren_col == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7501 | { |
| 7502 | /* |
| 7503 | * If we're looking at a close paren, line up right there; |
| 7504 | * otherwise, line up with the next (non-white) character. |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7505 | * When b_ind_unclosed_wrapped is set and the matching paren is |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7506 | * the last nonwhite character of the line, use either the |
| 7507 | * indent of the current line or the indentation of the next |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7508 | * outer paren and add b_ind_unclosed_wrapped (for very long |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7509 | * lines). |
| 7510 | */ |
| 7511 | if (theline[0] != ')') |
| 7512 | { |
| 7513 | cur_amount = MAXCOL; |
| 7514 | l = ml_get(our_paren_pos.lnum); |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7515 | if (curbuf->b_ind_unclosed_wrapped |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7516 | && cin_ends_in(l, (char_u *)"(", NULL)) |
| 7517 | { |
| 7518 | /* look for opening unmatched paren, indent one level |
| 7519 | * for each additional level */ |
| 7520 | n = 1; |
| 7521 | for (col = 0; col < our_paren_pos.col; ++col) |
| 7522 | { |
| 7523 | switch (l[col]) |
| 7524 | { |
| 7525 | case '(': |
| 7526 | case '{': ++n; |
| 7527 | break; |
| 7528 | |
| 7529 | case ')': |
| 7530 | case '}': if (n > 1) |
| 7531 | --n; |
| 7532 | break; |
| 7533 | } |
| 7534 | } |
| 7535 | |
| 7536 | our_paren_pos.col = 0; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7537 | amount += n * curbuf->b_ind_unclosed_wrapped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7538 | } |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7539 | else if (curbuf->b_ind_unclosed_whiteok) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7540 | our_paren_pos.col++; |
| 7541 | else |
| 7542 | { |
| 7543 | col = our_paren_pos.col + 1; |
| 7544 | while (vim_iswhite(l[col])) |
| 7545 | col++; |
| 7546 | if (l[col] != NUL) /* In case of trailing space */ |
| 7547 | our_paren_pos.col = col; |
| 7548 | else |
| 7549 | our_paren_pos.col++; |
| 7550 | } |
| 7551 | } |
| 7552 | |
| 7553 | /* |
| 7554 | * Find how indented the paren is, or the character after it |
| 7555 | * if we did the above "if". |
| 7556 | */ |
| 7557 | if (our_paren_pos.col > 0) |
| 7558 | { |
| 7559 | getvcol(curwin, &our_paren_pos, &col, NULL, NULL); |
| 7560 | if (cur_amount > (int)col) |
| 7561 | cur_amount = col; |
| 7562 | } |
| 7563 | } |
| 7564 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7565 | if (theline[0] == ')' && curbuf->b_ind_matching_paren) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7566 | { |
| 7567 | /* Line up with the start of the matching paren line. */ |
| 7568 | } |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7569 | else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0) |
| 7570 | || (!curbuf->b_ind_unclosed_noignore |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7571 | && *look == '(' && ignore_paren_col == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7572 | { |
| 7573 | if (cur_amount != MAXCOL) |
| 7574 | amount = cur_amount; |
| 7575 | } |
| 7576 | else |
| 7577 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7578 | /* Add b_ind_unclosed2 for each '(' before our matching one, |
| 7579 | * but ignore (void) before the line (ignore_paren_col). */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7580 | col = our_paren_pos.col; |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 7581 | while ((int)our_paren_pos.col > ignore_paren_col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7582 | { |
| 7583 | --our_paren_pos.col; |
| 7584 | switch (*ml_get_pos(&our_paren_pos)) |
| 7585 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7586 | case '(': amount += curbuf->b_ind_unclosed2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7587 | col = our_paren_pos.col; |
| 7588 | break; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7589 | case ')': amount -= curbuf->b_ind_unclosed2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7590 | col = MAXCOL; |
| 7591 | break; |
| 7592 | } |
| 7593 | } |
| 7594 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7595 | /* Use b_ind_unclosed once, when the first '(' is not inside |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7596 | * braces */ |
| 7597 | if (col == MAXCOL) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7598 | amount += curbuf->b_ind_unclosed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7599 | else |
| 7600 | { |
| 7601 | curwin->w_cursor.lnum = our_paren_pos.lnum; |
| 7602 | curwin->w_cursor.col = col; |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 7603 | if (find_match_paren_after_brace(curbuf->b_ind_maxparen) |
| 7604 | != NULL) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7605 | amount += curbuf->b_ind_unclosed2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7606 | else |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7607 | { |
| 7608 | if (is_if_for_while) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7609 | amount += curbuf->b_ind_if_for_while; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7610 | else |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7611 | amount += curbuf->b_ind_unclosed; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7612 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7613 | } |
| 7614 | /* |
| 7615 | * For a line starting with ')' use the minimum of the two |
| 7616 | * positions, to avoid giving it more indent than the previous |
| 7617 | * lines: |
| 7618 | * func_long_name( if (x |
| 7619 | * arg && yy |
| 7620 | * ) ^ not here ) ^ not here |
| 7621 | */ |
| 7622 | if (cur_amount < amount) |
| 7623 | amount = cur_amount; |
| 7624 | } |
| 7625 | } |
| 7626 | |
| 7627 | /* add extra indent for a comment */ |
| 7628 | if (cin_iscomment(theline)) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7629 | amount += curbuf->b_ind_comment; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7630 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7631 | else |
| 7632 | { |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7633 | /* |
| 7634 | * We are inside braces, there is a { before this line at the position |
| 7635 | * stored in tryposBrace. |
Bram Moolenaar | 04d17ae | 2014-08-06 17:44:14 +0200 | [diff] [blame] | 7636 | * Make a copy of tryposBrace, it may point to pos_copy inside |
| 7637 | * find_start_brace(), which may be changed somewhere. |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7638 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7639 | tryposCopy = *tryposBrace; |
| 7640 | tryposBrace = &tryposCopy; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7641 | trypos = tryposBrace; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7642 | ourscope = trypos->lnum; |
| 7643 | start = ml_get(ourscope); |
| 7644 | |
| 7645 | /* |
| 7646 | * Now figure out how indented the line is in general. |
| 7647 | * If the brace was at the start of the line, we use that; |
| 7648 | * otherwise, check out the indentation of the line as |
| 7649 | * a whole and then add the "imaginary indent" to that. |
| 7650 | */ |
| 7651 | look = skipwhite(start); |
| 7652 | if (*look == '{') |
| 7653 | { |
| 7654 | getvcol(curwin, trypos, &col, NULL, NULL); |
| 7655 | amount = col; |
| 7656 | if (*start == '{') |
| 7657 | start_brace = BRACE_IN_COL0; |
| 7658 | else |
| 7659 | start_brace = BRACE_AT_START; |
| 7660 | } |
| 7661 | else |
| 7662 | { |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7663 | /* That opening brace might have been on a continuation |
| 7664 | * line. if so, find the start of the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7665 | curwin->w_cursor.lnum = ourscope; |
| 7666 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7667 | /* Position the cursor over the rightmost paren, so that |
| 7668 | * matching it will take us back to the start of the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7669 | lnum = ourscope; |
| 7670 | if (find_last_paren(start, '(', ')') |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7671 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) |
| 7672 | != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7673 | lnum = trypos->lnum; |
| 7674 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7675 | /* It could have been something like |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7676 | * case 1: if (asdf && |
| 7677 | * ldfd) { |
| 7678 | * } |
| 7679 | */ |
Bram Moolenaar | e7eb789 | 2014-07-02 17:02:36 +0200 | [diff] [blame] | 7680 | if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label) |
| 7681 | && cin_iscase(skipwhite(ml_get_curline()), FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7682 | amount = get_indent(); |
Bram Moolenaar | e7eb789 | 2014-07-02 17:02:36 +0200 | [diff] [blame] | 7683 | else if (curbuf->b_ind_js) |
| 7684 | amount = get_indent_lnum(lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7685 | else |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7686 | amount = skip_label(lnum, &l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7687 | |
| 7688 | start_brace = BRACE_AT_END; |
| 7689 | } |
| 7690 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7691 | /* For Javascript check if the line starts with "key:". */ |
| 7692 | if (curbuf->b_ind_js) |
| 7693 | js_cur_has_key = cin_has_js_key(theline); |
| 7694 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7695 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7696 | * If we're looking at a closing brace, that's where |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7697 | * we want to be. otherwise, add the amount of room |
| 7698 | * that an indent is supposed to be. |
| 7699 | */ |
| 7700 | if (theline[0] == '}') |
| 7701 | { |
| 7702 | /* |
| 7703 | * they may want closing braces to line up with something |
| 7704 | * other than the open brace. indulge them, if so. |
| 7705 | */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7706 | amount += curbuf->b_ind_close_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7707 | } |
| 7708 | else |
| 7709 | { |
| 7710 | /* |
| 7711 | * If we're looking at an "else", try to find an "if" |
| 7712 | * to match it with. |
| 7713 | * If we're looking at a "while", try to find a "do" |
| 7714 | * to match it with. |
| 7715 | */ |
| 7716 | lookfor = LOOKFOR_INITIAL; |
| 7717 | if (cin_iselse(theline)) |
| 7718 | lookfor = LOOKFOR_IF; |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7719 | else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7720 | lookfor = LOOKFOR_DO; |
| 7721 | if (lookfor != LOOKFOR_INITIAL) |
| 7722 | { |
| 7723 | curwin->w_cursor.lnum = cur_curpos.lnum; |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7724 | if (find_match(lookfor, ourscope) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7725 | { |
| 7726 | amount = get_indent(); /* XXX */ |
| 7727 | goto theend; |
| 7728 | } |
| 7729 | } |
| 7730 | |
| 7731 | /* |
| 7732 | * We get here if we are not on an "while-of-do" or "else" (or |
| 7733 | * failed to find a matching "if"). |
| 7734 | * Search backwards for something to line up with. |
| 7735 | * First set amount for when we don't find anything. |
| 7736 | */ |
| 7737 | |
| 7738 | /* |
| 7739 | * if the '{' is _really_ at the left margin, use the imaginary |
| 7740 | * location of a left-margin brace. Otherwise, correct the |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7741 | * location for b_ind_open_extra. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7742 | */ |
| 7743 | |
| 7744 | if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */ |
| 7745 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7746 | amount = curbuf->b_ind_open_left_imag; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 7747 | lookfor_cpp_namespace = TRUE; |
| 7748 | } |
| 7749 | else if (start_brace == BRACE_AT_START && |
| 7750 | lookfor_cpp_namespace) /* '{' is at start */ |
| 7751 | { |
| 7752 | |
| 7753 | lookfor_cpp_namespace = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7754 | } |
| 7755 | else |
| 7756 | { |
| 7757 | if (start_brace == BRACE_AT_END) /* '{' is at end of line */ |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 7758 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7759 | amount += curbuf->b_ind_open_imag; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 7760 | |
| 7761 | l = skipwhite(ml_get_curline()); |
| 7762 | if (cin_is_cpp_namespace(l)) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7763 | amount += curbuf->b_ind_cpp_namespace; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 7764 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7765 | else |
| 7766 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7767 | /* Compensate for adding b_ind_open_extra later. */ |
| 7768 | amount -= curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7769 | if (amount < 0) |
| 7770 | amount = 0; |
| 7771 | } |
| 7772 | } |
| 7773 | |
| 7774 | lookfor_break = FALSE; |
| 7775 | |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 7776 | if (cin_iscase(theline, FALSE)) /* it's a switch() label */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7777 | { |
| 7778 | lookfor = LOOKFOR_CASE; /* find a previous switch() label */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7779 | amount += curbuf->b_ind_case; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7780 | } |
| 7781 | else if (cin_isscopedecl(theline)) /* private:, ... */ |
| 7782 | { |
| 7783 | lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7784 | amount += curbuf->b_ind_scopedecl; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7785 | } |
| 7786 | else |
| 7787 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7788 | if (curbuf->b_ind_case_break && cin_isbreak(theline)) |
| 7789 | /* break; ... */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7790 | lookfor_break = TRUE; |
| 7791 | |
| 7792 | lookfor = LOOKFOR_INITIAL; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7793 | /* b_ind_level from start of block */ |
| 7794 | amount += curbuf->b_ind_level; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7795 | } |
| 7796 | scope_amount = amount; |
| 7797 | whilelevel = 0; |
| 7798 | |
| 7799 | /* |
| 7800 | * Search backwards. If we find something we recognize, line up |
| 7801 | * with that. |
| 7802 | * |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7803 | * If we're looking at an open brace, indent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7804 | * the usual amount relative to the conditional |
| 7805 | * that opens the block. |
| 7806 | */ |
| 7807 | curwin->w_cursor = cur_curpos; |
| 7808 | for (;;) |
| 7809 | { |
| 7810 | curwin->w_cursor.lnum--; |
| 7811 | curwin->w_cursor.col = 0; |
| 7812 | |
| 7813 | /* |
| 7814 | * If we went all the way back to the start of our scope, line |
| 7815 | * up with it. |
| 7816 | */ |
| 7817 | if (curwin->w_cursor.lnum <= ourscope) |
| 7818 | { |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 7819 | /* We reached end of scope: |
| 7820 | * If looking for a enum or structure initialization |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7821 | * go further back: |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 7822 | * If it is an initializer (enum xxx or xxx =), then |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7823 | * don't add ind_continuation, otherwise it is a variable |
| 7824 | * declaration: |
| 7825 | * int x, |
| 7826 | * here; <-- add ind_continuation |
| 7827 | */ |
| 7828 | if (lookfor == LOOKFOR_ENUM_OR_INIT) |
| 7829 | { |
| 7830 | if (curwin->w_cursor.lnum == 0 |
| 7831 | || curwin->w_cursor.lnum |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7832 | < ourscope - curbuf->b_ind_maxparen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7833 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7834 | /* nothing found (abuse curbuf->b_ind_maxparen as |
| 7835 | * limit) assume terminated line (i.e. a variable |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7836 | * initialization) */ |
| 7837 | if (cont_amount > 0) |
| 7838 | amount = cont_amount; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7839 | else if (!curbuf->b_ind_js) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7840 | amount += ind_continuation; |
| 7841 | break; |
| 7842 | } |
| 7843 | |
| 7844 | l = ml_get_curline(); |
| 7845 | |
| 7846 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7847 | * If we're in a comment or raw string now, skip to |
| 7848 | * the start of it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7849 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7850 | trypos = ind_find_start_CORS(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7851 | if (trypos != NULL) |
| 7852 | { |
| 7853 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 7854 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7855 | continue; |
| 7856 | } |
| 7857 | |
| 7858 | /* |
| 7859 | * Skip preprocessor directives and blank lines. |
| 7860 | */ |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 7861 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, |
| 7862 | &amount)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7863 | continue; |
| 7864 | |
| 7865 | if (cin_nocode(l)) |
| 7866 | continue; |
| 7867 | |
| 7868 | terminated = cin_isterminated(l, FALSE, TRUE); |
| 7869 | |
| 7870 | /* |
| 7871 | * If we are at top level and the line looks like a |
| 7872 | * function declaration, we are done |
| 7873 | * (it's a variable declaration). |
| 7874 | */ |
| 7875 | if (start_brace != BRACE_IN_COL0 |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7876 | || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7877 | { |
| 7878 | /* if the line is terminated with another ',' |
| 7879 | * it is a continued variable initialization. |
| 7880 | * don't add extra indent. |
| 7881 | * TODO: does not work, if a function |
| 7882 | * declaration is split over multiple lines: |
| 7883 | * cin_isfuncdecl returns FALSE then. |
| 7884 | */ |
| 7885 | if (terminated == ',') |
| 7886 | break; |
| 7887 | |
| 7888 | /* if it es a enum declaration or an assignment, |
| 7889 | * we are done. |
| 7890 | */ |
| 7891 | if (terminated != ';' && cin_isinit()) |
| 7892 | break; |
| 7893 | |
| 7894 | /* nothing useful found */ |
| 7895 | if (terminated == 0 || terminated == '{') |
| 7896 | continue; |
| 7897 | } |
| 7898 | |
| 7899 | if (terminated != ';') |
| 7900 | { |
| 7901 | /* Skip parens and braces. Position the cursor |
| 7902 | * over the rightmost paren, so that matching it |
| 7903 | * will take us back to the start of the line. |
| 7904 | */ /* XXX */ |
| 7905 | trypos = NULL; |
| 7906 | if (find_last_paren(l, '(', ')')) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7907 | trypos = find_match_paren( |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7908 | curbuf->b_ind_maxparen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7909 | |
| 7910 | if (trypos == NULL && find_last_paren(l, '{', '}')) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7911 | trypos = find_start_brace(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7912 | |
| 7913 | if (trypos != NULL) |
| 7914 | { |
| 7915 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 7916 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7917 | continue; |
| 7918 | } |
| 7919 | } |
| 7920 | |
| 7921 | /* it's a variable declaration, add indentation |
| 7922 | * like in |
| 7923 | * int a, |
| 7924 | * b; |
| 7925 | */ |
| 7926 | if (cont_amount > 0) |
| 7927 | amount = cont_amount; |
| 7928 | else |
| 7929 | amount += ind_continuation; |
| 7930 | } |
| 7931 | else if (lookfor == LOOKFOR_UNTERM) |
| 7932 | { |
| 7933 | if (cont_amount > 0) |
| 7934 | amount = cont_amount; |
| 7935 | else |
| 7936 | amount += ind_continuation; |
| 7937 | } |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 7938 | else |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 7939 | { |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 7940 | if (lookfor != LOOKFOR_TERM |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7941 | && lookfor != LOOKFOR_CPP_BASECLASS |
| 7942 | && lookfor != LOOKFOR_COMMA) |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 7943 | { |
| 7944 | amount = scope_amount; |
| 7945 | if (theline[0] == '{') |
| 7946 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7947 | amount += curbuf->b_ind_open_extra; |
| 7948 | added_to_amount = curbuf->b_ind_open_extra; |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 7949 | } |
| 7950 | } |
| 7951 | |
| 7952 | if (lookfor_cpp_namespace) |
| 7953 | { |
| 7954 | /* |
| 7955 | * Looking for C++ namespace, need to look further |
| 7956 | * back. |
| 7957 | */ |
| 7958 | if (curwin->w_cursor.lnum == ourscope) |
| 7959 | continue; |
| 7960 | |
| 7961 | if (curwin->w_cursor.lnum == 0 |
| 7962 | || curwin->w_cursor.lnum |
| 7963 | < ourscope - FIND_NAMESPACE_LIM) |
| 7964 | break; |
| 7965 | |
| 7966 | l = ml_get_curline(); |
| 7967 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7968 | /* If we're in a comment or raw string now, skip |
| 7969 | * to the start of it. */ |
| 7970 | trypos = ind_find_start_CORS(); |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 7971 | if (trypos != NULL) |
| 7972 | { |
| 7973 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 7974 | curwin->w_cursor.col = 0; |
| 7975 | continue; |
| 7976 | } |
| 7977 | |
| 7978 | /* Skip preprocessor directives and blank lines. */ |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 7979 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, |
| 7980 | &amount)) |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 7981 | continue; |
| 7982 | |
| 7983 | /* Finally the actual check for "namespace". */ |
| 7984 | if (cin_is_cpp_namespace(l)) |
| 7985 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7986 | amount += curbuf->b_ind_cpp_namespace |
| 7987 | - added_to_amount; |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 7988 | break; |
| 7989 | } |
| 7990 | |
| 7991 | if (cin_nocode(l)) |
| 7992 | continue; |
| 7993 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7994 | } |
| 7995 | break; |
| 7996 | } |
| 7997 | |
| 7998 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7999 | * If we're in a comment or raw string now, skip to the start |
| 8000 | * of it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8001 | */ /* XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8002 | if ((trypos = ind_find_start_CORS()) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8003 | { |
| 8004 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8005 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8006 | continue; |
| 8007 | } |
| 8008 | |
| 8009 | l = ml_get_curline(); |
| 8010 | |
| 8011 | /* |
| 8012 | * If this is a switch() label, may line up relative to that. |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8013 | * If this is a C++ scope declaration, do the same. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8014 | */ |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8015 | iscase = cin_iscase(l, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8016 | if (iscase || cin_isscopedecl(l)) |
| 8017 | { |
| 8018 | /* we are only looking for cpp base class |
| 8019 | * declaration/initialization any longer */ |
| 8020 | if (lookfor == LOOKFOR_CPP_BASECLASS) |
| 8021 | break; |
| 8022 | |
| 8023 | /* When looking for a "do" we are not interested in |
| 8024 | * labels. */ |
| 8025 | if (whilelevel > 0) |
| 8026 | continue; |
| 8027 | |
| 8028 | /* |
| 8029 | * case xx: |
| 8030 | * c = 99 + <- this indent plus continuation |
| 8031 | *-> here; |
| 8032 | */ |
| 8033 | if (lookfor == LOOKFOR_UNTERM |
| 8034 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8035 | { |
| 8036 | if (cont_amount > 0) |
| 8037 | amount = cont_amount; |
| 8038 | else |
| 8039 | amount += ind_continuation; |
| 8040 | break; |
| 8041 | } |
| 8042 | |
| 8043 | /* |
| 8044 | * case xx: <- line up with this case |
| 8045 | * x = 333; |
| 8046 | * case yy: |
| 8047 | */ |
| 8048 | if ( (iscase && lookfor == LOOKFOR_CASE) |
| 8049 | || (iscase && lookfor_break) |
| 8050 | || (!iscase && lookfor == LOOKFOR_SCOPEDECL)) |
| 8051 | { |
| 8052 | /* |
| 8053 | * Check that this case label is not for another |
| 8054 | * switch() |
| 8055 | */ /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8056 | if ((trypos = find_start_brace()) == NULL |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8057 | || trypos->lnum == ourscope) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8058 | { |
| 8059 | amount = get_indent(); /* XXX */ |
| 8060 | break; |
| 8061 | } |
| 8062 | continue; |
| 8063 | } |
| 8064 | |
| 8065 | n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */ |
| 8066 | |
| 8067 | /* |
| 8068 | * case xx: if (cond) <- line up with this if |
| 8069 | * y = y + 1; |
| 8070 | * -> s = 99; |
| 8071 | * |
| 8072 | * case xx: |
| 8073 | * if (cond) <- line up with this line |
| 8074 | * y = y + 1; |
| 8075 | * -> s = 99; |
| 8076 | */ |
| 8077 | if (lookfor == LOOKFOR_TERM) |
| 8078 | { |
| 8079 | if (n) |
| 8080 | amount = n; |
| 8081 | |
| 8082 | if (!lookfor_break) |
| 8083 | break; |
| 8084 | } |
| 8085 | |
| 8086 | /* |
| 8087 | * case xx: x = x + 1; <- line up with this x |
| 8088 | * -> y = y + 1; |
| 8089 | * |
| 8090 | * case xx: if (cond) <- line up with this if |
| 8091 | * -> y = y + 1; |
| 8092 | */ |
| 8093 | if (n) |
| 8094 | { |
| 8095 | amount = n; |
| 8096 | l = after_label(ml_get_curline()); |
| 8097 | if (l != NULL && cin_is_cinword(l)) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 8098 | { |
| 8099 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8100 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 8101 | else |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8102 | amount += curbuf->b_ind_level |
| 8103 | + curbuf->b_ind_no_brace; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 8104 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8105 | break; |
| 8106 | } |
| 8107 | |
| 8108 | /* |
| 8109 | * Try to get the indent of a statement before the switch |
| 8110 | * label. If nothing is found, line up relative to the |
| 8111 | * switch label. |
| 8112 | * break; <- may line up with this line |
| 8113 | * case xx: |
| 8114 | * -> y = 1; |
| 8115 | */ |
| 8116 | scope_amount = get_indent() + (iscase /* XXX */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8117 | ? curbuf->b_ind_case_code |
| 8118 | : curbuf->b_ind_scopedecl_code); |
| 8119 | lookfor = curbuf->b_ind_case_break |
| 8120 | ? LOOKFOR_NOBREAK : LOOKFOR_ANY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8121 | continue; |
| 8122 | } |
| 8123 | |
| 8124 | /* |
| 8125 | * Looking for a switch() label or C++ scope declaration, |
| 8126 | * ignore other lines, skip {}-blocks. |
| 8127 | */ |
| 8128 | if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL) |
| 8129 | { |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8130 | if (find_last_paren(l, '{', '}') |
| 8131 | && (trypos = find_start_brace()) != NULL) |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8132 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8133 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8134 | curwin->w_cursor.col = 0; |
| 8135 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8136 | continue; |
| 8137 | } |
| 8138 | |
| 8139 | /* |
| 8140 | * Ignore jump labels with nothing after them. |
| 8141 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8142 | if (!curbuf->b_ind_js && cin_islabel()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8143 | { |
| 8144 | l = after_label(ml_get_curline()); |
| 8145 | if (l == NULL || cin_nocode(l)) |
| 8146 | continue; |
| 8147 | } |
| 8148 | |
| 8149 | /* |
| 8150 | * Ignore #defines, #if, etc. |
| 8151 | * Ignore comment and empty lines. |
| 8152 | * (need to get the line again, cin_islabel() may have |
| 8153 | * unlocked it) |
| 8154 | */ |
| 8155 | l = ml_get_curline(); |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8156 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8157 | || cin_nocode(l)) |
| 8158 | continue; |
| 8159 | |
| 8160 | /* |
| 8161 | * Are we at the start of a cpp base class declaration or |
| 8162 | * constructor initialization? |
| 8163 | */ /* XXX */ |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8164 | n = FALSE; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8165 | if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0) |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8166 | { |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 8167 | n = cin_is_cpp_baseclass(&cache_cpp_baseclass); |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8168 | l = ml_get_curline(); |
| 8169 | } |
| 8170 | if (n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8171 | { |
| 8172 | if (lookfor == LOOKFOR_UNTERM) |
| 8173 | { |
| 8174 | if (cont_amount > 0) |
| 8175 | amount = cont_amount; |
| 8176 | else |
| 8177 | amount += ind_continuation; |
| 8178 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8179 | else if (theline[0] == '{') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8180 | { |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8181 | /* Need to find start of the declaration. */ |
| 8182 | lookfor = LOOKFOR_UNTERM; |
| 8183 | ind_continuation = 0; |
| 8184 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8185 | } |
| 8186 | else |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8187 | /* XXX */ |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 8188 | amount = get_baseclass_amount( |
| 8189 | cache_cpp_baseclass.lpos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8190 | break; |
| 8191 | } |
| 8192 | else if (lookfor == LOOKFOR_CPP_BASECLASS) |
| 8193 | { |
| 8194 | /* only look, whether there is a cpp base class |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8195 | * declaration or initialization before the opening brace. |
| 8196 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8197 | if (cin_isterminated(l, TRUE, FALSE)) |
| 8198 | break; |
| 8199 | else |
| 8200 | continue; |
| 8201 | } |
| 8202 | |
| 8203 | /* |
| 8204 | * What happens next depends on the line being terminated. |
| 8205 | * If terminated with a ',' only consider it terminating if |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 8206 | * there is another unterminated statement behind, eg: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8207 | * 123, |
| 8208 | * sizeof |
| 8209 | * here |
| 8210 | * Otherwise check whether it is a enumeration or structure |
| 8211 | * initialisation (not indented) or a variable declaration |
| 8212 | * (indented). |
| 8213 | */ |
| 8214 | terminated = cin_isterminated(l, FALSE, TRUE); |
| 8215 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8216 | if (js_cur_has_key) |
| 8217 | { |
| 8218 | js_cur_has_key = 0; /* only check the first line */ |
| 8219 | if (curbuf->b_ind_js && terminated == ',') |
| 8220 | { |
| 8221 | /* For Javascript we might be inside an object: |
| 8222 | * key: something, <- align with this |
| 8223 | * key: something |
| 8224 | * or: |
| 8225 | * key: something + <- align with this |
| 8226 | * something, |
| 8227 | * key: something |
| 8228 | */ |
| 8229 | lookfor = LOOKFOR_JS_KEY; |
| 8230 | } |
| 8231 | } |
| 8232 | if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l)) |
| 8233 | { |
| 8234 | amount = get_indent(); |
| 8235 | break; |
| 8236 | } |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8237 | if (lookfor == LOOKFOR_COMMA) |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8238 | { |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8239 | if (tryposBrace != NULL && tryposBrace->lnum |
| 8240 | >= curwin->w_cursor.lnum) |
| 8241 | break; |
| 8242 | if (terminated == ',') |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8243 | /* line below current line is the one that starts a |
| 8244 | * (possibly broken) line ending in a comma. */ |
| 8245 | break; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8246 | else |
| 8247 | { |
| 8248 | amount = get_indent(); |
| 8249 | if (curwin->w_cursor.lnum - 1 == ourscope) |
| 8250 | /* line above is start of the scope, thus current |
| 8251 | * line is the one that stars a (possibly broken) |
| 8252 | * line ending in a comma. */ |
| 8253 | break; |
| 8254 | } |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8255 | } |
| 8256 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8257 | if (terminated == 0 || (lookfor != LOOKFOR_UNTERM |
| 8258 | && terminated == ',')) |
| 8259 | { |
Bram Moolenaar | 089af18 | 2015-10-07 11:41:49 +0200 | [diff] [blame] | 8260 | if (lookfor != LOOKFOR_ENUM_OR_INIT && |
| 8261 | (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '[')) |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8262 | amount += ind_continuation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8263 | /* |
| 8264 | * if we're in the middle of a paren thing, |
| 8265 | * go back to the line that starts it so |
| 8266 | * we can get the right prevailing indent |
| 8267 | * if ( foo && |
| 8268 | * bar ) |
| 8269 | */ |
| 8270 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8271 | * Position the cursor over the rightmost paren, so that |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8272 | * matching it will take us back to the start of the line. |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8273 | * Ignore a match before the start of the block. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8274 | */ |
| 8275 | (void)find_last_paren(l, '(', ')'); |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8276 | trypos = find_match_paren(corr_ind_maxparen(&cur_curpos)); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8277 | if (trypos != NULL && (trypos->lnum < tryposBrace->lnum |
| 8278 | || (trypos->lnum == tryposBrace->lnum |
| 8279 | && trypos->col < tryposBrace->col))) |
| 8280 | trypos = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8281 | |
| 8282 | /* |
| 8283 | * If we are looking for ',', we also look for matching |
| 8284 | * braces. |
| 8285 | */ |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8286 | if (trypos == NULL && terminated == ',' |
| 8287 | && find_last_paren(l, '{', '}')) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8288 | trypos = find_start_brace(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8289 | |
| 8290 | if (trypos != NULL) |
| 8291 | { |
| 8292 | /* |
| 8293 | * Check if we are on a case label now. This is |
| 8294 | * handled above. |
| 8295 | * case xx: if ( asdf && |
| 8296 | * asdf) |
| 8297 | */ |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8298 | curwin->w_cursor = *trypos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8299 | l = ml_get_curline(); |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8300 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8301 | { |
| 8302 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8303 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8304 | continue; |
| 8305 | } |
| 8306 | } |
| 8307 | |
| 8308 | /* |
| 8309 | * Skip over continuation lines to find the one to get the |
| 8310 | * indent from |
| 8311 | * char *usethis = "bla\ |
| 8312 | * bla", |
| 8313 | * here; |
| 8314 | */ |
| 8315 | if (terminated == ',') |
| 8316 | { |
| 8317 | while (curwin->w_cursor.lnum > 1) |
| 8318 | { |
| 8319 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 8320 | if (*l == NUL || l[STRLEN(l) - 1] != '\\') |
| 8321 | break; |
| 8322 | --curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8323 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8324 | } |
| 8325 | } |
| 8326 | |
| 8327 | /* |
| 8328 | * Get indent and pointer to text for current line, |
| 8329 | * ignoring any jump label. XXX |
| 8330 | */ |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8331 | if (curbuf->b_ind_js) |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8332 | cur_amount = get_indent(); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8333 | else |
| 8334 | cur_amount = skip_label(curwin->w_cursor.lnum, &l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8335 | /* |
| 8336 | * If this is just above the line we are indenting, and it |
| 8337 | * starts with a '{', line it up with this line. |
| 8338 | * while (not) |
| 8339 | * -> { |
| 8340 | * } |
| 8341 | */ |
| 8342 | if (terminated != ',' && lookfor != LOOKFOR_TERM |
| 8343 | && theline[0] == '{') |
| 8344 | { |
| 8345 | amount = cur_amount; |
| 8346 | /* |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8347 | * Only add b_ind_open_extra when the current line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8348 | * doesn't start with a '{', which must have a match |
| 8349 | * in the same line (scope is the same). Probably: |
| 8350 | * { 1, 2 }, |
| 8351 | * -> { 3, 4 } |
| 8352 | */ |
| 8353 | if (*skipwhite(l) != '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8354 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8355 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8356 | if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8357 | { |
| 8358 | /* have to look back, whether it is a cpp base |
| 8359 | * class declaration or initialization */ |
| 8360 | lookfor = LOOKFOR_CPP_BASECLASS; |
| 8361 | continue; |
| 8362 | } |
| 8363 | break; |
| 8364 | } |
| 8365 | |
| 8366 | /* |
| 8367 | * Check if we are after an "if", "while", etc. |
| 8368 | * Also allow " } else". |
| 8369 | */ |
| 8370 | if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) |
| 8371 | { |
| 8372 | /* |
| 8373 | * Found an unterminated line after an if (), line up |
| 8374 | * with the last one. |
| 8375 | * if (cond) |
| 8376 | * 100 + |
| 8377 | * -> here; |
| 8378 | */ |
| 8379 | if (lookfor == LOOKFOR_UNTERM |
| 8380 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8381 | { |
| 8382 | if (cont_amount > 0) |
| 8383 | amount = cont_amount; |
| 8384 | else |
| 8385 | amount += ind_continuation; |
| 8386 | break; |
| 8387 | } |
| 8388 | |
| 8389 | /* |
| 8390 | * If this is just above the line we are indenting, we |
| 8391 | * are finished. |
| 8392 | * while (not) |
| 8393 | * -> here; |
| 8394 | * Otherwise this indent can be used when the line |
| 8395 | * before this is terminated. |
| 8396 | * yyy; |
| 8397 | * if (stat) |
| 8398 | * while (not) |
| 8399 | * xxx; |
| 8400 | * -> here; |
| 8401 | */ |
| 8402 | amount = cur_amount; |
| 8403 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8404 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8405 | if (lookfor != LOOKFOR_TERM) |
| 8406 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8407 | amount += curbuf->b_ind_level |
| 8408 | + curbuf->b_ind_no_brace; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8409 | break; |
| 8410 | } |
| 8411 | |
| 8412 | /* |
| 8413 | * Special trick: when expecting the while () after a |
| 8414 | * do, line up with the while() |
| 8415 | * do |
| 8416 | * x = 1; |
| 8417 | * -> here |
| 8418 | */ |
| 8419 | l = skipwhite(ml_get_curline()); |
| 8420 | if (cin_isdo(l)) |
| 8421 | { |
| 8422 | if (whilelevel == 0) |
| 8423 | break; |
| 8424 | --whilelevel; |
| 8425 | } |
| 8426 | |
| 8427 | /* |
| 8428 | * When searching for a terminated line, don't use the |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8429 | * one between the "if" and the matching "else". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8430 | * Need to use the scope of this "else". XXX |
| 8431 | * If whilelevel != 0 continue looking for a "do {". |
| 8432 | */ |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8433 | if (cin_iselse(l) && whilelevel == 0) |
| 8434 | { |
| 8435 | /* If we're looking at "} else", let's make sure we |
| 8436 | * find the opening brace of the enclosing scope, |
| 8437 | * not the one from "if () {". */ |
| 8438 | if (*l == '}') |
| 8439 | curwin->w_cursor.col = |
Bram Moolenaar | 9b83c2f | 2011-05-25 17:29:44 +0200 | [diff] [blame] | 8440 | (colnr_T)(l - ml_get_curline()) + 1; |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8441 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8442 | if ((trypos = find_start_brace()) == NULL |
| 8443 | || find_match(LOOKFOR_IF, trypos->lnum) |
| 8444 | == FAIL) |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8445 | break; |
| 8446 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8447 | } |
| 8448 | |
| 8449 | /* |
| 8450 | * If we're below an unterminated line that is not an |
| 8451 | * "if" or something, we may line up with this line or |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 8452 | * add something for a continuation line, depending on |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8453 | * the line before this one. |
| 8454 | */ |
| 8455 | else |
| 8456 | { |
| 8457 | /* |
| 8458 | * Found two unterminated lines on a row, line up with |
| 8459 | * the last one. |
| 8460 | * c = 99 + |
| 8461 | * 100 + |
| 8462 | * -> here; |
| 8463 | */ |
| 8464 | if (lookfor == LOOKFOR_UNTERM) |
| 8465 | { |
| 8466 | /* When line ends in a comma add extra indent */ |
| 8467 | if (terminated == ',') |
| 8468 | amount += ind_continuation; |
| 8469 | break; |
| 8470 | } |
| 8471 | |
| 8472 | if (lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8473 | { |
| 8474 | /* Found two lines ending in ',', lineup with the |
| 8475 | * lowest one, but check for cpp base class |
| 8476 | * declaration/initialization, if it is an |
| 8477 | * opening brace or we are looking just for |
| 8478 | * enumerations/initializations. */ |
| 8479 | if (terminated == ',') |
| 8480 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8481 | if (curbuf->b_ind_cpp_baseclass == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8482 | break; |
| 8483 | |
| 8484 | lookfor = LOOKFOR_CPP_BASECLASS; |
| 8485 | continue; |
| 8486 | } |
| 8487 | |
| 8488 | /* Ignore unterminated lines in between, but |
| 8489 | * reduce indent. */ |
| 8490 | if (amount > cur_amount) |
| 8491 | amount = cur_amount; |
| 8492 | } |
| 8493 | else |
| 8494 | { |
| 8495 | /* |
| 8496 | * Found first unterminated line on a row, may |
| 8497 | * line up with this line, remember its indent |
| 8498 | * 100 + |
| 8499 | * -> here; |
| 8500 | */ |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8501 | l = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8502 | amount = cur_amount; |
Bram Moolenaar | 089af18 | 2015-10-07 11:41:49 +0200 | [diff] [blame] | 8503 | |
| 8504 | n = (int)STRLEN(l); |
| 8505 | if (terminated == ',' && (*skipwhite(l) == ']' |
| 8506 | || (n >=2 && l[n - 2] == ']'))) |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8507 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8508 | |
| 8509 | /* |
| 8510 | * If previous line ends in ',', check whether we |
| 8511 | * are in an initialization or enum |
| 8512 | * struct xxx = |
| 8513 | * { |
| 8514 | * sizeof a, |
| 8515 | * 124 }; |
| 8516 | * or a normal possible continuation line. |
| 8517 | * but only, of no other statement has been found |
| 8518 | * yet. |
| 8519 | */ |
| 8520 | if (lookfor == LOOKFOR_INITIAL && terminated == ',') |
| 8521 | { |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8522 | if (curbuf->b_ind_js) |
| 8523 | { |
| 8524 | /* Search for a line ending in a comma |
| 8525 | * and line up with the line below it |
| 8526 | * (could be the current line). |
| 8527 | * some = [ |
| 8528 | * 1, <- line up here |
| 8529 | * 2, |
| 8530 | * some = [ |
| 8531 | * 3 + <- line up here |
| 8532 | * 4 * |
| 8533 | * 5, |
| 8534 | * 6, |
| 8535 | */ |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8536 | if (cin_iscomment(skipwhite(l))) |
| 8537 | break; |
| 8538 | lookfor = LOOKFOR_COMMA; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8539 | trypos = find_match_char('[', |
| 8540 | curbuf->b_ind_maxparen); |
| 8541 | if (trypos != NULL) |
| 8542 | { |
| 8543 | if (trypos->lnum |
| 8544 | == curwin->w_cursor.lnum - 1) |
| 8545 | { |
| 8546 | /* Current line is first inside |
| 8547 | * [], line up with it. */ |
| 8548 | break; |
| 8549 | } |
| 8550 | ourscope = trypos->lnum; |
| 8551 | } |
| 8552 | } |
| 8553 | else |
| 8554 | { |
| 8555 | lookfor = LOOKFOR_ENUM_OR_INIT; |
| 8556 | cont_amount = cin_first_id_amount(); |
| 8557 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8558 | } |
| 8559 | else |
| 8560 | { |
| 8561 | if (lookfor == LOOKFOR_INITIAL |
| 8562 | && *l != NUL |
| 8563 | && l[STRLEN(l) - 1] == '\\') |
| 8564 | /* XXX */ |
| 8565 | cont_amount = cin_get_equal_amount( |
| 8566 | curwin->w_cursor.lnum); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8567 | if (lookfor != LOOKFOR_TERM |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8568 | && lookfor != LOOKFOR_JS_KEY |
| 8569 | && lookfor != LOOKFOR_COMMA) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8570 | lookfor = LOOKFOR_UNTERM; |
| 8571 | } |
| 8572 | } |
| 8573 | } |
| 8574 | } |
| 8575 | |
| 8576 | /* |
| 8577 | * Check if we are after a while (cond); |
| 8578 | * If so: Ignore until the matching "do". |
| 8579 | */ |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8580 | else if (cin_iswhileofdo_end(terminated)) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8581 | { |
| 8582 | /* |
| 8583 | * Found an unterminated line after a while ();, line up |
| 8584 | * with the last one. |
| 8585 | * while (cond); |
| 8586 | * 100 + <- line up with this one |
| 8587 | * -> here; |
| 8588 | */ |
| 8589 | if (lookfor == LOOKFOR_UNTERM |
| 8590 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8591 | { |
| 8592 | if (cont_amount > 0) |
| 8593 | amount = cont_amount; |
| 8594 | else |
| 8595 | amount += ind_continuation; |
| 8596 | break; |
| 8597 | } |
| 8598 | |
| 8599 | if (whilelevel == 0) |
| 8600 | { |
| 8601 | lookfor = LOOKFOR_TERM; |
| 8602 | amount = get_indent(); /* XXX */ |
| 8603 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8604 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8605 | } |
| 8606 | ++whilelevel; |
| 8607 | } |
| 8608 | |
| 8609 | /* |
| 8610 | * We are after a "normal" statement. |
| 8611 | * If we had another statement we can stop now and use the |
| 8612 | * indent of that other statement. |
| 8613 | * Otherwise the indent of the current statement may be used, |
| 8614 | * search backwards for the next "normal" statement. |
| 8615 | */ |
| 8616 | else |
| 8617 | { |
| 8618 | /* |
| 8619 | * Skip single break line, if before a switch label. It |
| 8620 | * may be lined up with the case label. |
| 8621 | */ |
| 8622 | if (lookfor == LOOKFOR_NOBREAK |
| 8623 | && cin_isbreak(skipwhite(ml_get_curline()))) |
| 8624 | { |
| 8625 | lookfor = LOOKFOR_ANY; |
| 8626 | continue; |
| 8627 | } |
| 8628 | |
| 8629 | /* |
| 8630 | * Handle "do {" line. |
| 8631 | */ |
| 8632 | if (whilelevel > 0) |
| 8633 | { |
| 8634 | l = cin_skipcomment(ml_get_curline()); |
| 8635 | if (cin_isdo(l)) |
| 8636 | { |
| 8637 | amount = get_indent(); /* XXX */ |
| 8638 | --whilelevel; |
| 8639 | continue; |
| 8640 | } |
| 8641 | } |
| 8642 | |
| 8643 | /* |
| 8644 | * Found a terminated line above an unterminated line. Add |
| 8645 | * the amount for a continuation line. |
| 8646 | * x = 1; |
| 8647 | * y = foo + |
| 8648 | * -> here; |
| 8649 | * or |
| 8650 | * int x = 1; |
| 8651 | * int foo, |
| 8652 | * -> here; |
| 8653 | */ |
| 8654 | if (lookfor == LOOKFOR_UNTERM |
| 8655 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8656 | { |
| 8657 | if (cont_amount > 0) |
| 8658 | amount = cont_amount; |
| 8659 | else |
| 8660 | amount += ind_continuation; |
| 8661 | break; |
| 8662 | } |
| 8663 | |
| 8664 | /* |
| 8665 | * Found a terminated line above a terminated line or "if" |
| 8666 | * etc. line. Use the amount of the line below us. |
| 8667 | * x = 1; x = 1; |
| 8668 | * if (asdf) y = 2; |
| 8669 | * while (asdf) ->here; |
| 8670 | * here; |
| 8671 | * ->foo; |
| 8672 | */ |
| 8673 | if (lookfor == LOOKFOR_TERM) |
| 8674 | { |
| 8675 | if (!lookfor_break && whilelevel == 0) |
| 8676 | break; |
| 8677 | } |
| 8678 | |
| 8679 | /* |
| 8680 | * First line above the one we're indenting is terminated. |
| 8681 | * To know what needs to be done look further backward for |
| 8682 | * a terminated line. |
| 8683 | */ |
| 8684 | else |
| 8685 | { |
| 8686 | /* |
| 8687 | * position the cursor over the rightmost paren, so |
| 8688 | * that matching it will take us back to the start of |
| 8689 | * the line. Helps for: |
| 8690 | * func(asdr, |
| 8691 | * asdfasdf); |
| 8692 | * here; |
| 8693 | */ |
| 8694 | term_again: |
| 8695 | l = ml_get_curline(); |
| 8696 | if (find_last_paren(l, '(', ')') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8697 | && (trypos = find_match_paren( |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8698 | curbuf->b_ind_maxparen)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8699 | { |
| 8700 | /* |
| 8701 | * Check if we are on a case label now. This is |
| 8702 | * handled above. |
| 8703 | * case xx: if ( asdf && |
| 8704 | * asdf) |
| 8705 | */ |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8706 | curwin->w_cursor = *trypos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8707 | l = ml_get_curline(); |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8708 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8709 | { |
| 8710 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8711 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8712 | continue; |
| 8713 | } |
| 8714 | } |
| 8715 | |
| 8716 | /* When aligning with the case statement, don't align |
| 8717 | * with a statement after it. |
| 8718 | * case 1: { <-- don't use this { position |
| 8719 | * stat; |
| 8720 | * } |
| 8721 | * case 2: |
| 8722 | * stat; |
| 8723 | * } |
| 8724 | */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8725 | iscase = (curbuf->b_ind_keep_case_label |
| 8726 | && cin_iscase(l, FALSE)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8727 | |
| 8728 | /* |
| 8729 | * Get indent and pointer to text for current line, |
| 8730 | * ignoring any jump label. |
| 8731 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8732 | amount = skip_label(curwin->w_cursor.lnum, &l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8733 | |
| 8734 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8735 | amount += curbuf->b_ind_open_extra; |
| 8736 | /* See remark above: "Only add b_ind_open_extra.." */ |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8737 | l = skipwhite(l); |
| 8738 | if (*l == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8739 | amount -= curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8740 | lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM; |
| 8741 | |
| 8742 | /* |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8743 | * When a terminated line starts with "else" skip to |
| 8744 | * the matching "if": |
| 8745 | * else 3; |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 8746 | * indent this; |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8747 | * Need to use the scope of this "else". XXX |
| 8748 | * If whilelevel != 0 continue looking for a "do {". |
| 8749 | */ |
| 8750 | if (lookfor == LOOKFOR_TERM |
| 8751 | && *l != '}' |
| 8752 | && cin_iselse(l) |
| 8753 | && whilelevel == 0) |
| 8754 | { |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8755 | if ((trypos = find_start_brace()) == NULL |
| 8756 | || find_match(LOOKFOR_IF, trypos->lnum) |
| 8757 | == FAIL) |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8758 | break; |
| 8759 | continue; |
| 8760 | } |
| 8761 | |
| 8762 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8763 | * If we're at the end of a block, skip to the start of |
| 8764 | * that block. |
| 8765 | */ |
Bram Moolenaar | 6d8f9c6 | 2011-11-30 13:03:28 +0100 | [diff] [blame] | 8766 | l = ml_get_curline(); |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8767 | if (find_last_paren(l, '{', '}') /* XXX */ |
| 8768 | && (trypos = find_start_brace()) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8769 | { |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8770 | curwin->w_cursor = *trypos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8771 | /* if not "else {" check for terminated again */ |
| 8772 | /* but skip block for "} else {" */ |
| 8773 | l = cin_skipcomment(ml_get_curline()); |
| 8774 | if (*l == '}' || !cin_iselse(l)) |
| 8775 | goto term_again; |
| 8776 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8777 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8778 | } |
| 8779 | } |
| 8780 | } |
| 8781 | } |
| 8782 | } |
| 8783 | } |
| 8784 | |
| 8785 | /* add extra indent for a comment */ |
| 8786 | if (cin_iscomment(theline)) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8787 | amount += curbuf->b_ind_comment; |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 8788 | |
| 8789 | /* subtract extra left-shift for jump labels */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8790 | if (curbuf->b_ind_jump_label > 0 && original_line_islabel) |
| 8791 | amount -= curbuf->b_ind_jump_label; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8792 | |
| 8793 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8794 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8795 | |
| 8796 | /* |
| 8797 | * ok -- we're not inside any sort of structure at all! |
| 8798 | * |
| 8799 | * This means we're at the top level, and everything should |
| 8800 | * basically just match where the previous line is, except |
| 8801 | * for the lines immediately following a function declaration, |
| 8802 | * which are K&R-style parameters and need to be indented. |
| 8803 | * |
| 8804 | * if our line starts with an open brace, forget about any |
| 8805 | * prevailing indent and make sure it looks like the start |
| 8806 | * of a function |
| 8807 | */ |
| 8808 | |
| 8809 | if (theline[0] == '{') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8810 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8811 | amount = curbuf->b_ind_first_open; |
| 8812 | goto theend; |
| 8813 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8814 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8815 | /* |
| 8816 | * If the NEXT line is a function declaration, the current |
| 8817 | * line needs to be indented as a function type spec. |
| 8818 | * Don't do this if the current line looks like a comment or if the |
| 8819 | * current line is terminated, ie. ends in ';', or if the current line |
| 8820 | * contains { or }: "void f() {\n if (1)" |
| 8821 | */ |
| 8822 | if (cur_curpos.lnum < curbuf->b_ml.ml_line_count |
| 8823 | && !cin_nocode(theline) |
| 8824 | && vim_strchr(theline, '{') == NULL |
| 8825 | && vim_strchr(theline, '}') == NULL |
| 8826 | && !cin_ends_in(theline, (char_u *)":", NULL) |
| 8827 | && !cin_ends_in(theline, (char_u *)",", NULL) |
| 8828 | && cin_isfuncdecl(NULL, cur_curpos.lnum + 1, |
| 8829 | cur_curpos.lnum + 1) |
| 8830 | && !cin_isterminated(theline, FALSE, TRUE)) |
| 8831 | { |
| 8832 | amount = curbuf->b_ind_func_type; |
| 8833 | goto theend; |
| 8834 | } |
| 8835 | |
| 8836 | /* search backwards until we find something we recognize */ |
| 8837 | amount = 0; |
| 8838 | curwin->w_cursor = cur_curpos; |
| 8839 | while (curwin->w_cursor.lnum > 1) |
| 8840 | { |
| 8841 | curwin->w_cursor.lnum--; |
| 8842 | curwin->w_cursor.col = 0; |
| 8843 | |
| 8844 | l = ml_get_curline(); |
| 8845 | |
| 8846 | /* |
| 8847 | * If we're in a comment or raw string now, skip to the start |
| 8848 | * of it. |
| 8849 | */ /* XXX */ |
| 8850 | if ((trypos = ind_find_start_CORS()) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8851 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8852 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 8853 | curwin->w_cursor.col = 0; |
| 8854 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8855 | } |
| 8856 | |
| 8857 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8858 | * Are we at the start of a cpp base class declaration or |
| 8859 | * constructor initialization? |
| 8860 | */ /* XXX */ |
| 8861 | n = FALSE; |
| 8862 | if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8863 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8864 | n = cin_is_cpp_baseclass(&cache_cpp_baseclass); |
| 8865 | l = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8866 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8867 | if (n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8868 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8869 | /* XXX */ |
| 8870 | amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col); |
| 8871 | break; |
| 8872 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8873 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8874 | /* |
| 8875 | * Skip preprocessor directives and blank lines. |
| 8876 | */ |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8877 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8878 | continue; |
| 8879 | |
| 8880 | if (cin_nocode(l)) |
| 8881 | continue; |
| 8882 | |
| 8883 | /* |
| 8884 | * If the previous line ends in ',', use one level of |
| 8885 | * indentation: |
| 8886 | * int foo, |
| 8887 | * bar; |
| 8888 | * do this before checking for '}' in case of eg. |
| 8889 | * enum foobar |
| 8890 | * { |
| 8891 | * ... |
| 8892 | * } foo, |
| 8893 | * bar; |
| 8894 | */ |
| 8895 | n = 0; |
| 8896 | if (cin_ends_in(l, (char_u *)",", NULL) |
| 8897 | || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\')) |
| 8898 | { |
| 8899 | /* take us back to opening paren */ |
| 8900 | if (find_last_paren(l, '(', ')') |
| 8901 | && (trypos = find_match_paren( |
| 8902 | curbuf->b_ind_maxparen)) != NULL) |
| 8903 | curwin->w_cursor = *trypos; |
| 8904 | |
| 8905 | /* For a line ending in ',' that is a continuation line go |
| 8906 | * back to the first line with a backslash: |
| 8907 | * char *foo = "bla\ |
| 8908 | * bla", |
| 8909 | * here; |
| 8910 | */ |
| 8911 | while (n == 0 && curwin->w_cursor.lnum > 1) |
| 8912 | { |
| 8913 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 8914 | if (*l == NUL || l[STRLEN(l) - 1] != '\\') |
| 8915 | break; |
| 8916 | --curwin->w_cursor.lnum; |
| 8917 | curwin->w_cursor.col = 0; |
| 8918 | } |
| 8919 | |
| 8920 | amount = get_indent(); /* XXX */ |
| 8921 | |
| 8922 | if (amount == 0) |
| 8923 | amount = cin_first_id_amount(); |
| 8924 | if (amount == 0) |
| 8925 | amount = ind_continuation; |
| 8926 | break; |
| 8927 | } |
| 8928 | |
| 8929 | /* |
| 8930 | * If the line looks like a function declaration, and we're |
| 8931 | * not in a comment, put it the left margin. |
| 8932 | */ |
| 8933 | if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */ |
| 8934 | break; |
| 8935 | l = ml_get_curline(); |
| 8936 | |
| 8937 | /* |
| 8938 | * Finding the closing '}' of a previous function. Put |
| 8939 | * current line at the left margin. For when 'cino' has "fs". |
| 8940 | */ |
| 8941 | if (*skipwhite(l) == '}') |
| 8942 | break; |
| 8943 | |
| 8944 | /* (matching {) |
| 8945 | * If the previous line ends on '};' (maybe followed by |
| 8946 | * comments) align at column 0. For example: |
| 8947 | * char *string_array[] = { "foo", |
| 8948 | * / * x * / "b};ar" }; / * foobar * / |
| 8949 | */ |
| 8950 | if (cin_ends_in(l, (char_u *)"};", NULL)) |
| 8951 | break; |
| 8952 | |
| 8953 | /* |
| 8954 | * If the previous line ends on '[' we are probably in an |
| 8955 | * array constant: |
| 8956 | * something = [ |
| 8957 | * 234, <- extra indent |
| 8958 | */ |
| 8959 | if (cin_ends_in(l, (char_u *)"[", NULL)) |
| 8960 | { |
| 8961 | amount = get_indent() + ind_continuation; |
| 8962 | break; |
| 8963 | } |
| 8964 | |
| 8965 | /* |
| 8966 | * Find a line only has a semicolon that belongs to a previous |
| 8967 | * line ending in '}', e.g. before an #endif. Don't increase |
| 8968 | * indent then. |
| 8969 | */ |
| 8970 | if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1)) |
| 8971 | { |
| 8972 | pos_T curpos_save = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8973 | |
| 8974 | while (curwin->w_cursor.lnum > 1) |
| 8975 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8976 | look = ml_get(--curwin->w_cursor.lnum); |
| 8977 | if (!(cin_nocode(look) || cin_ispreproc_cont( |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8978 | &look, &curwin->w_cursor.lnum, &amount))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8979 | break; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8980 | } |
| 8981 | if (curwin->w_cursor.lnum > 0 |
| 8982 | && cin_ends_in(look, (char_u *)"}", NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8983 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8984 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8985 | curwin->w_cursor = curpos_save; |
| 8986 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8987 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8988 | /* |
| 8989 | * If the PREVIOUS line is a function declaration, the current |
| 8990 | * line (and the ones that follow) needs to be indented as |
| 8991 | * parameters. |
| 8992 | */ |
| 8993 | if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
| 8994 | { |
| 8995 | amount = curbuf->b_ind_param; |
| 8996 | break; |
| 8997 | } |
| 8998 | |
| 8999 | /* |
| 9000 | * If the previous line ends in ';' and the line before the |
| 9001 | * previous line ends in ',' or '\', ident to column zero: |
| 9002 | * int foo, |
| 9003 | * bar; |
| 9004 | * indent_to_0 here; |
| 9005 | */ |
| 9006 | if (cin_ends_in(l, (char_u *)";", NULL)) |
| 9007 | { |
| 9008 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 9009 | if (cin_ends_in(l, (char_u *)",", NULL) |
| 9010 | || (*l != NUL && l[STRLEN(l) - 1] == '\\')) |
| 9011 | break; |
| 9012 | l = ml_get_curline(); |
| 9013 | } |
| 9014 | |
| 9015 | /* |
| 9016 | * Doesn't look like anything interesting -- so just |
| 9017 | * use the indent of this line. |
| 9018 | * |
| 9019 | * Position the cursor over the rightmost paren, so that |
| 9020 | * matching it will take us back to the start of the line. |
| 9021 | */ |
| 9022 | find_last_paren(l, '(', ')'); |
| 9023 | |
| 9024 | if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
| 9025 | curwin->w_cursor = *trypos; |
| 9026 | amount = get_indent(); /* XXX */ |
| 9027 | break; |
| 9028 | } |
| 9029 | |
| 9030 | /* add extra indent for a comment */ |
| 9031 | if (cin_iscomment(theline)) |
| 9032 | amount += curbuf->b_ind_comment; |
| 9033 | |
| 9034 | /* add extra indent if the previous line ended in a backslash: |
| 9035 | * "asdfasdf\ |
| 9036 | * here"; |
| 9037 | * char *foo = "asdf\ |
| 9038 | * here"; |
| 9039 | */ |
| 9040 | if (cur_curpos.lnum > 1) |
| 9041 | { |
| 9042 | l = ml_get(cur_curpos.lnum - 1); |
| 9043 | if (*l != NUL && l[STRLEN(l) - 1] == '\\') |
| 9044 | { |
| 9045 | cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1); |
| 9046 | if (cur_amount > 0) |
| 9047 | amount = cur_amount; |
| 9048 | else if (cur_amount == 0) |
| 9049 | amount += ind_continuation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9050 | } |
| 9051 | } |
| 9052 | |
| 9053 | theend: |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9054 | if (amount < 0) |
| 9055 | amount = 0; |
| 9056 | |
| 9057 | laterend: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9058 | /* put the cursor back where it belongs */ |
| 9059 | curwin->w_cursor = cur_curpos; |
| 9060 | |
| 9061 | vim_free(linecopy); |
| 9062 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9063 | return amount; |
| 9064 | } |
| 9065 | |
| 9066 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9067 | find_match(int lookfor, linenr_T ourscope) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9068 | { |
| 9069 | char_u *look; |
| 9070 | pos_T *theirscope; |
| 9071 | char_u *mightbeif; |
| 9072 | int elselevel; |
| 9073 | int whilelevel; |
| 9074 | |
| 9075 | if (lookfor == LOOKFOR_IF) |
| 9076 | { |
| 9077 | elselevel = 1; |
| 9078 | whilelevel = 0; |
| 9079 | } |
| 9080 | else |
| 9081 | { |
| 9082 | elselevel = 0; |
| 9083 | whilelevel = 1; |
| 9084 | } |
| 9085 | |
| 9086 | curwin->w_cursor.col = 0; |
| 9087 | |
| 9088 | while (curwin->w_cursor.lnum > ourscope + 1) |
| 9089 | { |
| 9090 | curwin->w_cursor.lnum--; |
| 9091 | curwin->w_cursor.col = 0; |
| 9092 | |
| 9093 | look = cin_skipcomment(ml_get_curline()); |
| 9094 | if (cin_iselse(look) |
| 9095 | || cin_isif(look) |
| 9096 | || cin_isdo(look) /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9097 | || cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9098 | { |
| 9099 | /* |
| 9100 | * if we've gone outside the braces entirely, |
| 9101 | * we must be out of scope... |
| 9102 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9103 | theirscope = find_start_brace(); /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9104 | if (theirscope == NULL) |
| 9105 | break; |
| 9106 | |
| 9107 | /* |
| 9108 | * and if the brace enclosing this is further |
| 9109 | * back than the one enclosing the else, we're |
| 9110 | * out of luck too. |
| 9111 | */ |
| 9112 | if (theirscope->lnum < ourscope) |
| 9113 | break; |
| 9114 | |
| 9115 | /* |
| 9116 | * and if they're enclosed in a *deeper* brace, |
| 9117 | * then we can ignore it because it's in a |
| 9118 | * different scope... |
| 9119 | */ |
| 9120 | if (theirscope->lnum > ourscope) |
| 9121 | continue; |
| 9122 | |
| 9123 | /* |
| 9124 | * if it was an "else" (that's not an "else if") |
| 9125 | * then we need to go back to another if, so |
| 9126 | * increment elselevel |
| 9127 | */ |
| 9128 | look = cin_skipcomment(ml_get_curline()); |
| 9129 | if (cin_iselse(look)) |
| 9130 | { |
| 9131 | mightbeif = cin_skipcomment(look + 4); |
| 9132 | if (!cin_isif(mightbeif)) |
| 9133 | ++elselevel; |
| 9134 | continue; |
| 9135 | } |
| 9136 | |
| 9137 | /* |
| 9138 | * if it was a "while" then we need to go back to |
| 9139 | * another "do", so increment whilelevel. XXX |
| 9140 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9141 | if (cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9142 | { |
| 9143 | ++whilelevel; |
| 9144 | continue; |
| 9145 | } |
| 9146 | |
| 9147 | /* If it's an "if" decrement elselevel */ |
| 9148 | look = cin_skipcomment(ml_get_curline()); |
| 9149 | if (cin_isif(look)) |
| 9150 | { |
| 9151 | elselevel--; |
| 9152 | /* |
| 9153 | * When looking for an "if" ignore "while"s that |
| 9154 | * get in the way. |
| 9155 | */ |
| 9156 | if (elselevel == 0 && lookfor == LOOKFOR_IF) |
| 9157 | whilelevel = 0; |
| 9158 | } |
| 9159 | |
| 9160 | /* If it's a "do" decrement whilelevel */ |
| 9161 | if (cin_isdo(look)) |
| 9162 | whilelevel--; |
| 9163 | |
| 9164 | /* |
| 9165 | * if we've used up all the elses, then |
| 9166 | * this must be the if that we want! |
| 9167 | * match the indent level of that if. |
| 9168 | */ |
| 9169 | if (elselevel <= 0 && whilelevel <= 0) |
| 9170 | { |
| 9171 | return OK; |
| 9172 | } |
| 9173 | } |
| 9174 | } |
| 9175 | return FAIL; |
| 9176 | } |
| 9177 | |
| 9178 | # if defined(FEAT_EVAL) || defined(PROTO) |
| 9179 | /* |
| 9180 | * Get indent level from 'indentexpr'. |
| 9181 | */ |
| 9182 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9183 | get_expr_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9184 | { |
| 9185 | int indent; |
Bram Moolenaar | 8fe8d9e | 2013-02-13 16:10:17 +0100 | [diff] [blame] | 9186 | pos_T save_pos; |
| 9187 | colnr_T save_curswant; |
| 9188 | int save_set_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9189 | int save_State; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 9190 | int use_sandbox = was_set_insecurely((char_u *)"indentexpr", |
| 9191 | OPT_LOCAL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9192 | |
Bram Moolenaar | 8fe8d9e | 2013-02-13 16:10:17 +0100 | [diff] [blame] | 9193 | /* Save and restore cursor position and curswant, in case it was changed |
| 9194 | * via :normal commands */ |
| 9195 | save_pos = curwin->w_cursor; |
| 9196 | save_curswant = curwin->w_curswant; |
| 9197 | save_set_curswant = curwin->w_set_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9198 | set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 9199 | if (use_sandbox) |
| 9200 | ++sandbox; |
| 9201 | ++textlock; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 9202 | indent = (int)eval_to_number(curbuf->b_p_inde); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 9203 | if (use_sandbox) |
| 9204 | --sandbox; |
| 9205 | --textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9206 | |
| 9207 | /* Restore the cursor position so that 'indentexpr' doesn't need to. |
| 9208 | * Pretend to be in Insert mode, allow cursor past end of line for "o" |
| 9209 | * command. */ |
| 9210 | save_State = State; |
| 9211 | State = INSERT; |
Bram Moolenaar | 8fe8d9e | 2013-02-13 16:10:17 +0100 | [diff] [blame] | 9212 | curwin->w_cursor = save_pos; |
| 9213 | curwin->w_curswant = save_curswant; |
| 9214 | curwin->w_set_curswant = save_set_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9215 | check_cursor(); |
| 9216 | State = save_State; |
| 9217 | |
| 9218 | /* If there is an error, just keep the current indent. */ |
| 9219 | if (indent < 0) |
| 9220 | indent = get_indent(); |
| 9221 | |
| 9222 | return indent; |
| 9223 | } |
| 9224 | # endif |
| 9225 | |
| 9226 | #endif /* FEAT_CINDENT */ |
| 9227 | |
| 9228 | #if defined(FEAT_LISP) || defined(PROTO) |
| 9229 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 9230 | static int lisp_match(char_u *p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9231 | |
| 9232 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9233 | lisp_match(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9234 | { |
| 9235 | char_u buf[LSIZE]; |
| 9236 | int len; |
Bram Moolenaar | af6c131 | 2014-03-12 18:55:58 +0100 | [diff] [blame] | 9237 | char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9238 | |
| 9239 | while (*word != NUL) |
| 9240 | { |
| 9241 | (void)copy_option_part(&word, buf, LSIZE, ","); |
| 9242 | len = (int)STRLEN(buf); |
| 9243 | if (STRNCMP(buf, p, len) == 0 && p[len] == ' ') |
| 9244 | return TRUE; |
| 9245 | } |
| 9246 | return FALSE; |
| 9247 | } |
| 9248 | |
| 9249 | /* |
| 9250 | * When 'p' is present in 'cpoptions, a Vi compatible method is used. |
| 9251 | * The incompatible newer method is quite a bit better at indenting |
| 9252 | * code in lisp-like languages than the traditional one; it's still |
| 9253 | * mostly heuristics however -- Dirk van Deun, dirk@rave.org |
| 9254 | * |
| 9255 | * TODO: |
| 9256 | * Findmatch() should be adapted for lisp, also to make showmatch |
| 9257 | * work correctly: now (v5.3) it seems all C/C++ oriented: |
| 9258 | * - it does not recognize the #\( and #\) notations as character literals |
| 9259 | * - it doesn't know about comments starting with a semicolon |
| 9260 | * - it incorrectly interprets '(' as a character literal |
| 9261 | * All this messes up get_lisp_indent in some rare cases. |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9262 | * Update from Sergey Khorev: |
| 9263 | * I tried to fix the first two issues. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9264 | */ |
| 9265 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9266 | get_lisp_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9267 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9268 | pos_T *pos, realpos, paren; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9269 | int amount; |
| 9270 | char_u *that; |
| 9271 | colnr_T col; |
| 9272 | colnr_T firsttry; |
| 9273 | int parencount, quotecount; |
| 9274 | int vi_lisp; |
| 9275 | |
| 9276 | /* Set vi_lisp to use the vi-compatible method */ |
| 9277 | vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL); |
| 9278 | |
| 9279 | realpos = curwin->w_cursor; |
| 9280 | curwin->w_cursor.col = 0; |
| 9281 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9282 | if ((pos = findmatch(NULL, '(')) == NULL) |
| 9283 | pos = findmatch(NULL, '['); |
| 9284 | else |
| 9285 | { |
| 9286 | paren = *pos; |
| 9287 | pos = findmatch(NULL, '['); |
| 9288 | if (pos == NULL || ltp(pos, &paren)) |
| 9289 | pos = &paren; |
| 9290 | } |
| 9291 | if (pos != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9292 | { |
| 9293 | /* Extra trick: Take the indent of the first previous non-white |
| 9294 | * line that is at the same () level. */ |
| 9295 | amount = -1; |
| 9296 | parencount = 0; |
| 9297 | |
| 9298 | while (--curwin->w_cursor.lnum >= pos->lnum) |
| 9299 | { |
| 9300 | if (linewhite(curwin->w_cursor.lnum)) |
| 9301 | continue; |
| 9302 | for (that = ml_get_curline(); *that != NUL; ++that) |
| 9303 | { |
| 9304 | if (*that == ';') |
| 9305 | { |
| 9306 | while (*(that + 1) != NUL) |
| 9307 | ++that; |
| 9308 | continue; |
| 9309 | } |
| 9310 | if (*that == '\\') |
| 9311 | { |
| 9312 | if (*(that + 1) != NUL) |
| 9313 | ++that; |
| 9314 | continue; |
| 9315 | } |
| 9316 | if (*that == '"' && *(that + 1) != NUL) |
| 9317 | { |
Bram Moolenaar | 15ff6c1 | 2006-09-15 18:18:09 +0000 | [diff] [blame] | 9318 | while (*++that && *that != '"') |
| 9319 | { |
| 9320 | /* skipping escaped characters in the string */ |
| 9321 | if (*that == '\\') |
| 9322 | { |
| 9323 | if (*++that == NUL) |
| 9324 | break; |
| 9325 | if (that[1] == NUL) |
| 9326 | { |
| 9327 | ++that; |
| 9328 | break; |
| 9329 | } |
| 9330 | } |
| 9331 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9332 | } |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9333 | if (*that == '(' || *that == '[') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9334 | ++parencount; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9335 | else if (*that == ')' || *that == ']') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9336 | --parencount; |
| 9337 | } |
| 9338 | if (parencount == 0) |
| 9339 | { |
| 9340 | amount = get_indent(); |
| 9341 | break; |
| 9342 | } |
| 9343 | } |
| 9344 | |
| 9345 | if (amount == -1) |
| 9346 | { |
| 9347 | curwin->w_cursor.lnum = pos->lnum; |
| 9348 | curwin->w_cursor.col = pos->col; |
| 9349 | col = pos->col; |
| 9350 | |
| 9351 | that = ml_get_curline(); |
| 9352 | |
| 9353 | if (vi_lisp && get_indent() == 0) |
| 9354 | amount = 2; |
| 9355 | else |
| 9356 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9357 | char_u *line = that; |
| 9358 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9359 | amount = 0; |
| 9360 | while (*that && col) |
| 9361 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9362 | amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9363 | col--; |
| 9364 | } |
| 9365 | |
| 9366 | /* |
| 9367 | * Some keywords require "body" indenting rules (the |
| 9368 | * non-standard-lisp ones are Scheme special forms): |
| 9369 | * |
| 9370 | * (let ((a 1)) instead (let ((a 1)) |
| 9371 | * (...)) of (...)) |
| 9372 | */ |
| 9373 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9374 | if (!vi_lisp && (*that == '(' || *that == '[') |
| 9375 | && lisp_match(that + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9376 | amount += 2; |
| 9377 | else |
| 9378 | { |
| 9379 | that++; |
| 9380 | amount++; |
| 9381 | firsttry = amount; |
| 9382 | |
| 9383 | while (vim_iswhite(*that)) |
| 9384 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9385 | amount += lbr_chartabsize(line, that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9386 | ++that; |
| 9387 | } |
| 9388 | |
| 9389 | if (*that && *that != ';') /* not a comment line */ |
| 9390 | { |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 9391 | /* test *that != '(' to accommodate first let/do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9392 | * argument if it is more than one line */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9393 | if (!vi_lisp && *that != '(' && *that != '[') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9394 | firsttry++; |
| 9395 | |
| 9396 | parencount = 0; |
| 9397 | quotecount = 0; |
| 9398 | |
| 9399 | if (vi_lisp |
| 9400 | || (*that != '"' |
| 9401 | && *that != '\'' |
| 9402 | && *that != '#' |
| 9403 | && (*that < '0' || *that > '9'))) |
| 9404 | { |
| 9405 | while (*that |
| 9406 | && (!vim_iswhite(*that) |
| 9407 | || quotecount |
| 9408 | || parencount) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9409 | && (!((*that == '(' || *that == '[') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9410 | && !quotecount |
| 9411 | && !parencount |
| 9412 | && vi_lisp))) |
| 9413 | { |
| 9414 | if (*that == '"') |
| 9415 | quotecount = !quotecount; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9416 | if ((*that == '(' || *that == '[') |
| 9417 | && !quotecount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9418 | ++parencount; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9419 | if ((*that == ')' || *that == ']') |
| 9420 | && !quotecount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9421 | --parencount; |
| 9422 | if (*that == '\\' && *(that+1) != NUL) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9423 | amount += lbr_chartabsize_adv( |
| 9424 | line, &that, (colnr_T)amount); |
| 9425 | amount += lbr_chartabsize_adv( |
| 9426 | line, &that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9427 | } |
| 9428 | } |
| 9429 | while (vim_iswhite(*that)) |
| 9430 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9431 | amount += lbr_chartabsize( |
| 9432 | line, that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9433 | that++; |
| 9434 | } |
| 9435 | if (!*that || *that == ';') |
| 9436 | amount = firsttry; |
| 9437 | } |
| 9438 | } |
| 9439 | } |
| 9440 | } |
| 9441 | } |
| 9442 | else |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9443 | amount = 0; /* no matching '(' or '[' found, use zero indent */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9444 | |
| 9445 | curwin->w_cursor = realpos; |
| 9446 | |
| 9447 | return amount; |
| 9448 | } |
| 9449 | #endif /* FEAT_LISP */ |
| 9450 | |
| 9451 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9452 | prepare_to_exit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9453 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 9454 | #if defined(SIGHUP) && defined(SIG_IGN) |
| 9455 | /* Ignore SIGHUP, because a dropped connection causes a read error, which |
| 9456 | * makes Vim exit and then handling SIGHUP causes various reentrance |
| 9457 | * problems. */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 9458 | signal(SIGHUP, SIG_IGN); |
| 9459 | #endif |
| 9460 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9461 | #ifdef FEAT_GUI |
| 9462 | if (gui.in_use) |
| 9463 | { |
| 9464 | gui.dying = TRUE; |
| 9465 | out_trash(); /* trash any pending output */ |
| 9466 | } |
| 9467 | else |
| 9468 | #endif |
| 9469 | { |
| 9470 | windgoto((int)Rows - 1, 0); |
| 9471 | |
| 9472 | /* |
| 9473 | * Switch terminal mode back now, so messages end up on the "normal" |
| 9474 | * screen (if there are two screens). |
| 9475 | */ |
| 9476 | settmode(TMODE_COOK); |
Bram Moolenaar | cea912a | 2016-10-12 14:20:24 +0200 | [diff] [blame] | 9477 | stoptermcap(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9478 | out_flush(); |
| 9479 | } |
| 9480 | } |
| 9481 | |
| 9482 | /* |
| 9483 | * Preserve files and exit. |
| 9484 | * When called IObuff must contain a message. |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 9485 | * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe |
| 9486 | * functions, such as allocating memory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9487 | */ |
| 9488 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9489 | preserve_exit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9490 | { |
| 9491 | buf_T *buf; |
| 9492 | |
| 9493 | prepare_to_exit(); |
| 9494 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9495 | /* Setting this will prevent free() calls. That avoids calling free() |
| 9496 | * recursively when free() was invoked with a bad pointer. */ |
| 9497 | really_exiting = TRUE; |
| 9498 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9499 | out_str(IObuff); |
| 9500 | screen_start(); /* don't know where cursor is now */ |
| 9501 | out_flush(); |
| 9502 | |
| 9503 | ml_close_notmod(); /* close all not-modified buffers */ |
| 9504 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 9505 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9506 | { |
| 9507 | if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) |
| 9508 | { |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 9509 | OUT_STR("Vim: preserving files...\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9510 | screen_start(); /* don't know where cursor is now */ |
| 9511 | out_flush(); |
| 9512 | ml_sync_all(FALSE, FALSE); /* preserve all swap files */ |
| 9513 | break; |
| 9514 | } |
| 9515 | } |
| 9516 | |
| 9517 | ml_close_all(FALSE); /* close all memfiles, without deleting */ |
| 9518 | |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 9519 | OUT_STR("Vim: Finished.\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9520 | |
| 9521 | getout(1); |
| 9522 | } |
| 9523 | |
| 9524 | /* |
| 9525 | * return TRUE if "fname" exists. |
| 9526 | */ |
| 9527 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9528 | vim_fexists(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9529 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 9530 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9531 | |
| 9532 | if (mch_stat((char *)fname, &st)) |
| 9533 | return FALSE; |
| 9534 | return TRUE; |
| 9535 | } |
| 9536 | |
| 9537 | /* |
| 9538 | * Check for CTRL-C pressed, but only once in a while. |
| 9539 | * Should be used instead of ui_breakcheck() for functions that check for |
| 9540 | * each line in the file. Calling ui_breakcheck() each time takes too much |
| 9541 | * time, because it can be a system call. |
| 9542 | */ |
| 9543 | |
| 9544 | #ifndef BREAKCHECK_SKIP |
| 9545 | # ifdef FEAT_GUI /* assume the GUI only runs on fast computers */ |
| 9546 | # define BREAKCHECK_SKIP 200 |
| 9547 | # else |
| 9548 | # define BREAKCHECK_SKIP 32 |
| 9549 | # endif |
| 9550 | #endif |
| 9551 | |
| 9552 | static int breakcheck_count = 0; |
| 9553 | |
| 9554 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9555 | line_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9556 | { |
| 9557 | if (++breakcheck_count >= BREAKCHECK_SKIP) |
| 9558 | { |
| 9559 | breakcheck_count = 0; |
| 9560 | ui_breakcheck(); |
| 9561 | } |
| 9562 | } |
| 9563 | |
| 9564 | /* |
| 9565 | * Like line_breakcheck() but check 10 times less often. |
| 9566 | */ |
| 9567 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9568 | fast_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9569 | { |
| 9570 | if (++breakcheck_count >= BREAKCHECK_SKIP * 10) |
| 9571 | { |
| 9572 | breakcheck_count = 0; |
| 9573 | ui_breakcheck(); |
| 9574 | } |
| 9575 | } |
| 9576 | |
| 9577 | /* |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 9578 | * Invoke expand_wildcards() for one pattern. |
| 9579 | * Expand items like "%:h" before the expansion. |
| 9580 | * Returns OK or FAIL. |
| 9581 | */ |
| 9582 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9583 | expand_wildcards_eval( |
| 9584 | char_u **pat, /* pointer to input pattern */ |
| 9585 | int *num_file, /* resulting number of files */ |
| 9586 | char_u ***file, /* array of resulting files */ |
| 9587 | int flags) /* EW_DIR, etc. */ |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 9588 | { |
| 9589 | int ret = FAIL; |
| 9590 | char_u *eval_pat = NULL; |
| 9591 | char_u *exp_pat = *pat; |
| 9592 | char_u *ignored_msg; |
| 9593 | int usedlen; |
| 9594 | |
| 9595 | if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') |
| 9596 | { |
| 9597 | ++emsg_off; |
| 9598 | eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, |
| 9599 | NULL, &ignored_msg, NULL); |
| 9600 | --emsg_off; |
| 9601 | if (eval_pat != NULL) |
| 9602 | exp_pat = concat_str(eval_pat, exp_pat + usedlen); |
| 9603 | } |
| 9604 | |
| 9605 | if (exp_pat != NULL) |
| 9606 | ret = expand_wildcards(1, &exp_pat, num_file, file, flags); |
| 9607 | |
| 9608 | if (eval_pat != NULL) |
| 9609 | { |
| 9610 | vim_free(exp_pat); |
| 9611 | vim_free(eval_pat); |
| 9612 | } |
| 9613 | |
| 9614 | return ret; |
| 9615 | } |
| 9616 | |
| 9617 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9618 | * Expand wildcards. Calls gen_expand_wildcards() and removes files matching |
| 9619 | * 'wildignore'. |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9620 | * 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] | 9621 | */ |
| 9622 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9623 | expand_wildcards( |
| 9624 | int num_pat, /* number of input patterns */ |
| 9625 | char_u **pat, /* array of input patterns */ |
| 9626 | int *num_files, /* resulting number of files */ |
| 9627 | char_u ***files, /* array of resulting files */ |
| 9628 | int flags) /* EW_DIR, etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9629 | { |
| 9630 | int retval; |
| 9631 | int i, j; |
| 9632 | char_u *p; |
| 9633 | int non_suf_match; /* number without matching suffix */ |
| 9634 | |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9635 | retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9636 | |
| 9637 | /* When keeping all matches, return here */ |
Bram Moolenaar | 9e193ac | 2010-07-19 23:11:27 +0200 | [diff] [blame] | 9638 | if ((flags & EW_KEEPALL) || retval == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9639 | return retval; |
| 9640 | |
| 9641 | #ifdef FEAT_WILDIGN |
| 9642 | /* |
| 9643 | * Remove names that match 'wildignore'. |
| 9644 | */ |
| 9645 | if (*p_wig) |
| 9646 | { |
| 9647 | char_u *ffname; |
| 9648 | |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9649 | /* check all files in (*files)[] */ |
| 9650 | for (i = 0; i < *num_files; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9651 | { |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9652 | ffname = FullName_save((*files)[i], FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9653 | if (ffname == NULL) /* out of memory */ |
| 9654 | break; |
| 9655 | # ifdef VMS |
| 9656 | vms_remove_version(ffname); |
| 9657 | # endif |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9658 | if (match_file_list(p_wig, (*files)[i], ffname)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9659 | { |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 9660 | /* remove this matching file from the list */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9661 | vim_free((*files)[i]); |
| 9662 | for (j = i; j + 1 < *num_files; ++j) |
| 9663 | (*files)[j] = (*files)[j + 1]; |
| 9664 | --*num_files; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9665 | --i; |
| 9666 | } |
| 9667 | vim_free(ffname); |
| 9668 | } |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9669 | |
| 9670 | /* If the number of matches is now zero, we fail. */ |
| 9671 | if (*num_files == 0) |
| 9672 | { |
| 9673 | vim_free(*files); |
| 9674 | *files = NULL; |
| 9675 | return FAIL; |
| 9676 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9677 | } |
| 9678 | #endif |
| 9679 | |
| 9680 | /* |
| 9681 | * Move the names where 'suffixes' match to the end. |
| 9682 | */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9683 | if (*num_files > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9684 | { |
| 9685 | non_suf_match = 0; |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9686 | for (i = 0; i < *num_files; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9687 | { |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9688 | if (!match_suffix((*files)[i])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9689 | { |
| 9690 | /* |
| 9691 | * Move the name without matching suffix to the front |
| 9692 | * of the list. |
| 9693 | */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9694 | p = (*files)[i]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9695 | for (j = i; j > non_suf_match; --j) |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9696 | (*files)[j] = (*files)[j - 1]; |
| 9697 | (*files)[non_suf_match++] = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9698 | } |
| 9699 | } |
| 9700 | } |
| 9701 | |
| 9702 | return retval; |
| 9703 | } |
| 9704 | |
| 9705 | /* |
| 9706 | * Return TRUE if "fname" matches with an entry in 'suffixes'. |
| 9707 | */ |
| 9708 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9709 | match_suffix(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9710 | { |
| 9711 | int fnamelen, setsuflen; |
| 9712 | char_u *setsuf; |
| 9713 | #define MAXSUFLEN 30 /* maximum length of a file suffix */ |
| 9714 | char_u suf_buf[MAXSUFLEN]; |
| 9715 | |
| 9716 | fnamelen = (int)STRLEN(fname); |
| 9717 | setsuflen = 0; |
| 9718 | for (setsuf = p_su; *setsuf; ) |
| 9719 | { |
| 9720 | setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); |
Bram Moolenaar | 055a2ba | 2009-07-14 19:40:21 +0000 | [diff] [blame] | 9721 | if (setsuflen == 0) |
| 9722 | { |
| 9723 | char_u *tail = gettail(fname); |
| 9724 | |
| 9725 | /* empty entry: match name without a '.' */ |
| 9726 | if (vim_strchr(tail, '.') == NULL) |
| 9727 | { |
| 9728 | setsuflen = 1; |
| 9729 | break; |
| 9730 | } |
| 9731 | } |
| 9732 | else |
| 9733 | { |
| 9734 | if (fnamelen >= setsuflen |
| 9735 | && fnamencmp(suf_buf, fname + fnamelen - setsuflen, |
| 9736 | (size_t)setsuflen) == 0) |
| 9737 | break; |
| 9738 | setsuflen = 0; |
| 9739 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9740 | } |
| 9741 | return (setsuflen != 0); |
| 9742 | } |
| 9743 | |
| 9744 | #if !defined(NO_EXPANDPATH) || defined(PROTO) |
| 9745 | |
| 9746 | # ifdef VIM_BACKTICK |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 9747 | static int vim_backtick(char_u *p); |
| 9748 | static int expand_backtick(garray_T *gap, char_u *pat, int flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9749 | # endif |
| 9750 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 9751 | # if defined(WIN3264) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9752 | /* |
| 9753 | * File name expansion code for MS-DOS, Win16 and Win32. It's here because |
| 9754 | * it's shared between these systems. |
| 9755 | */ |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 9756 | # if defined(PROTO) |
| 9757 | # define _cdecl |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9758 | # else |
| 9759 | # ifdef __BORLANDC__ |
| 9760 | # define _cdecl _RTLENTRYF |
| 9761 | # endif |
| 9762 | # endif |
| 9763 | |
| 9764 | /* |
| 9765 | * comparison function for qsort in dos_expandpath() |
| 9766 | */ |
| 9767 | static int _cdecl |
| 9768 | pstrcmp(const void *a, const void *b) |
| 9769 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 9770 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9771 | } |
| 9772 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9773 | /* |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9774 | * Recursively expand one path component into all matching files and/or |
| 9775 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9776 | * Return the number of matches found. |
| 9777 | * "path" has backslashes before chars that are not to be expanded, starting |
| 9778 | * at "path[wildoff]". |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9779 | * Return the number of matches found. |
| 9780 | * NOTE: much of this is identical to unix_expandpath(), keep in sync! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9781 | */ |
| 9782 | static int |
| 9783 | dos_expandpath( |
| 9784 | garray_T *gap, |
| 9785 | char_u *path, |
| 9786 | int wildoff, |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9787 | int flags, /* EW_* flags */ |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 9788 | int didstar) /* expanded "**" once already */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9789 | { |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9790 | char_u *buf; |
| 9791 | char_u *path_end; |
| 9792 | char_u *p, *s, *e; |
| 9793 | int start_len = gap->ga_len; |
| 9794 | char_u *pat; |
| 9795 | regmatch_T regmatch; |
| 9796 | int starts_with_dot; |
| 9797 | int matches; |
| 9798 | int len; |
| 9799 | int starstar = FALSE; |
| 9800 | static int stardepth = 0; /* depth for "**" expansion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9801 | WIN32_FIND_DATA fb; |
| 9802 | HANDLE hFind = (HANDLE)0; |
| 9803 | # ifdef FEAT_MBYTE |
| 9804 | WIN32_FIND_DATAW wfb; |
| 9805 | WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */ |
| 9806 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9807 | char_u *matchname; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9808 | int ok; |
| 9809 | |
| 9810 | /* Expanding "**" may take a long time, check for CTRL-C. */ |
| 9811 | if (stardepth > 0) |
| 9812 | { |
| 9813 | ui_breakcheck(); |
| 9814 | if (got_int) |
| 9815 | return 0; |
| 9816 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9817 | |
Bram Moolenaar | 7314efd | 2015-10-31 15:32:52 +0100 | [diff] [blame] | 9818 | /* Make room for file name. When doing encoding conversion the actual |
| 9819 | * length may be quite a bit longer, thus use the maximum possible length. */ |
| 9820 | buf = alloc((int)MAXPATHL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9821 | if (buf == NULL) |
| 9822 | return 0; |
| 9823 | |
| 9824 | /* |
| 9825 | * Find the first part in the path name that contains a wildcard or a ~1. |
| 9826 | * Copy it into buf, including the preceding characters. |
| 9827 | */ |
| 9828 | p = buf; |
| 9829 | s = buf; |
| 9830 | e = NULL; |
| 9831 | path_end = path; |
| 9832 | while (*path_end != NUL) |
| 9833 | { |
| 9834 | /* May ignore a wildcard that has a backslash before it; it will |
| 9835 | * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ |
| 9836 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 9837 | *p++ = *path_end++; |
| 9838 | else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') |
| 9839 | { |
| 9840 | if (e != NULL) |
| 9841 | break; |
| 9842 | s = p + 1; |
| 9843 | } |
| 9844 | else if (path_end >= path + wildoff |
| 9845 | && vim_strchr((char_u *)"*?[~", *path_end) != NULL) |
| 9846 | e = p; |
Bram Moolenaar | 780d4c3 | 2016-03-25 17:21:19 +0100 | [diff] [blame] | 9847 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9848 | if (has_mbyte) |
| 9849 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 9850 | len = (*mb_ptr2len)(path_end); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9851 | STRNCPY(p, path_end, len); |
| 9852 | p += len; |
| 9853 | path_end += len; |
| 9854 | } |
| 9855 | else |
Bram Moolenaar | 780d4c3 | 2016-03-25 17:21:19 +0100 | [diff] [blame] | 9856 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9857 | *p++ = *path_end++; |
| 9858 | } |
| 9859 | e = p; |
| 9860 | *e = NUL; |
| 9861 | |
| 9862 | /* now we have one wildcard component between s and e */ |
| 9863 | /* Remove backslashes between "wildoff" and the start of the wildcard |
| 9864 | * component. */ |
| 9865 | for (p = buf + wildoff; p < s; ++p) |
| 9866 | if (rem_backslash(p)) |
| 9867 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 9868 | STRMOVE(p, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9869 | --e; |
| 9870 | --s; |
| 9871 | } |
| 9872 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9873 | /* Check for "**" between "s" and "e". */ |
| 9874 | for (p = s; p < e; ++p) |
| 9875 | if (p[0] == '*' && p[1] == '*') |
| 9876 | starstar = TRUE; |
| 9877 | |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 9878 | starts_with_dot = *s == '.'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9879 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 9880 | if (pat == NULL) |
| 9881 | { |
| 9882 | vim_free(buf); |
| 9883 | return 0; |
| 9884 | } |
| 9885 | |
| 9886 | /* compile the regexp into a program */ |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 9887 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
Bram Moolenaar | b560983 | 2011-07-20 15:04:58 +0200 | [diff] [blame] | 9888 | ++emsg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9889 | regmatch.rm_ic = TRUE; /* Always ignore case */ |
| 9890 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 9891 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
Bram Moolenaar | b560983 | 2011-07-20 15:04:58 +0200 | [diff] [blame] | 9892 | --emsg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9893 | vim_free(pat); |
| 9894 | |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 9895 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9896 | { |
| 9897 | vim_free(buf); |
| 9898 | return 0; |
| 9899 | } |
| 9900 | |
| 9901 | /* remember the pattern or file name being looked for */ |
| 9902 | matchname = vim_strsave(s); |
| 9903 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9904 | /* If "**" is by itself, this is the first time we encounter it and more |
| 9905 | * is following then find matches without any directory. */ |
| 9906 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 9907 | && *path_end == '/') |
| 9908 | { |
| 9909 | STRCPY(s, path_end + 1); |
| 9910 | ++stardepth; |
| 9911 | (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 9912 | --stardepth; |
| 9913 | } |
| 9914 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9915 | /* Scan all files in the directory with "dir/ *.*" */ |
| 9916 | STRCPY(s, "*.*"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9917 | # ifdef FEAT_MBYTE |
| 9918 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 9919 | { |
| 9920 | /* The active codepage differs from 'encoding'. Attempt using the |
| 9921 | * wide function. If it fails because it is not implemented fall back |
| 9922 | * to the non-wide version (for Windows 98) */ |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 9923 | wn = enc_to_utf16(buf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9924 | if (wn != NULL) |
| 9925 | { |
| 9926 | hFind = FindFirstFileW(wn, &wfb); |
| 9927 | if (hFind == INVALID_HANDLE_VALUE |
| 9928 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
| 9929 | { |
| 9930 | vim_free(wn); |
| 9931 | wn = NULL; |
| 9932 | } |
| 9933 | } |
| 9934 | } |
| 9935 | |
| 9936 | if (wn == NULL) |
| 9937 | # endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 9938 | hFind = FindFirstFile((LPCSTR)buf, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9939 | ok = (hFind != INVALID_HANDLE_VALUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9940 | |
| 9941 | while (ok) |
| 9942 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9943 | # ifdef FEAT_MBYTE |
| 9944 | if (wn != NULL) |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 9945 | p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9946 | else |
| 9947 | # endif |
| 9948 | p = (char_u *)fb.cFileName; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9949 | /* Ignore entries starting with a dot, unless when asked for. Accept |
| 9950 | * all entries found with "matchname". */ |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 9951 | if ((p[0] != '.' || starts_with_dot |
| 9952 | || ((flags & EW_DODOT) |
| 9953 | && p[1] != NUL && (p[1] != '.' || p[2] != NUL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9954 | && (matchname == NULL |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 9955 | || (regmatch.regprog != NULL |
| 9956 | && vim_regexec(®match, p, (colnr_T)0)) |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 9957 | || ((flags & EW_NOTWILD) |
| 9958 | && fnamencmp(path + (s - buf), p, e - s) == 0))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9959 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9960 | STRCPY(s, p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9961 | len = (int)STRLEN(buf); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9962 | |
| 9963 | if (starstar && stardepth < 100) |
| 9964 | { |
| 9965 | /* For "**" in the pattern first go deeper in the tree to |
| 9966 | * find matches. */ |
| 9967 | STRCPY(buf + len, "/**"); |
| 9968 | STRCPY(buf + len + 3, path_end); |
| 9969 | ++stardepth; |
| 9970 | (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); |
| 9971 | --stardepth; |
| 9972 | } |
| 9973 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9974 | STRCPY(buf + len, path_end); |
| 9975 | if (mch_has_exp_wildcard(path_end)) |
| 9976 | { |
| 9977 | /* need to expand another component of the path */ |
| 9978 | /* remove backslashes for the remaining components only */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9979 | (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9980 | } |
| 9981 | else |
| 9982 | { |
| 9983 | /* no more wildcards, check if there is a match */ |
| 9984 | /* remove backslashes for the remaining components only */ |
| 9985 | if (*path_end != 0) |
| 9986 | backslash_halve(buf + len + 1); |
| 9987 | if (mch_getperm(buf) >= 0) /* add existing file */ |
| 9988 | addfile(gap, buf, flags); |
| 9989 | } |
| 9990 | } |
| 9991 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9992 | # ifdef FEAT_MBYTE |
| 9993 | if (wn != NULL) |
| 9994 | { |
| 9995 | vim_free(p); |
| 9996 | ok = FindNextFileW(hFind, &wfb); |
| 9997 | } |
| 9998 | else |
| 9999 | # endif |
| 10000 | ok = FindNextFile(hFind, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10001 | |
| 10002 | /* If no more matches and no match was used, try expanding the name |
| 10003 | * itself. Finds the long name of a short filename. */ |
| 10004 | if (!ok && matchname != NULL && gap->ga_len == start_len) |
| 10005 | { |
| 10006 | STRCPY(s, matchname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10007 | FindClose(hFind); |
| 10008 | # ifdef FEAT_MBYTE |
| 10009 | if (wn != NULL) |
| 10010 | { |
| 10011 | vim_free(wn); |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 10012 | wn = enc_to_utf16(buf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10013 | if (wn != NULL) |
| 10014 | hFind = FindFirstFileW(wn, &wfb); |
| 10015 | } |
| 10016 | if (wn == NULL) |
| 10017 | # endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 10018 | hFind = FindFirstFile((LPCSTR)buf, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10019 | ok = (hFind != INVALID_HANDLE_VALUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10020 | vim_free(matchname); |
| 10021 | matchname = NULL; |
| 10022 | } |
| 10023 | } |
| 10024 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10025 | FindClose(hFind); |
| 10026 | # ifdef FEAT_MBYTE |
| 10027 | vim_free(wn); |
| 10028 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10029 | vim_free(buf); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10030 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10031 | vim_free(matchname); |
| 10032 | |
| 10033 | matches = gap->ga_len - start_len; |
| 10034 | if (matches > 0) |
| 10035 | qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, |
| 10036 | sizeof(char_u *), pstrcmp); |
| 10037 | return matches; |
| 10038 | } |
| 10039 | |
| 10040 | int |
| 10041 | mch_expandpath( |
| 10042 | garray_T *gap, |
| 10043 | char_u *path, |
| 10044 | int flags) /* EW_* flags */ |
| 10045 | { |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10046 | return dos_expandpath(gap, path, 0, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10047 | } |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10048 | # endif /* WIN3264 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10049 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10050 | #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ |
| 10051 | || defined(PROTO) |
| 10052 | /* |
| 10053 | * Unix style wildcard expansion code. |
| 10054 | * It's here because it's used both for Unix and Mac. |
| 10055 | */ |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 10056 | static int pstrcmp(const void *, const void *); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10057 | |
| 10058 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10059 | pstrcmp(const void *a, const void *b) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10060 | { |
| 10061 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
| 10062 | } |
| 10063 | |
| 10064 | /* |
| 10065 | * Recursively expand one path component into all matching files and/or |
| 10066 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
| 10067 | * "path" has backslashes before chars that are not to be expanded, starting |
| 10068 | * at "path + wildoff". |
| 10069 | * Return the number of matches found. |
| 10070 | * NOTE: much of this is identical to dos_expandpath(), keep in sync! |
| 10071 | */ |
| 10072 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10073 | unix_expandpath( |
| 10074 | garray_T *gap, |
| 10075 | char_u *path, |
| 10076 | int wildoff, |
| 10077 | int flags, /* EW_* flags */ |
| 10078 | int didstar) /* expanded "**" once already */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10079 | { |
| 10080 | char_u *buf; |
| 10081 | char_u *path_end; |
| 10082 | char_u *p, *s, *e; |
| 10083 | int start_len = gap->ga_len; |
| 10084 | char_u *pat; |
| 10085 | regmatch_T regmatch; |
| 10086 | int starts_with_dot; |
| 10087 | int matches; |
| 10088 | int len; |
| 10089 | int starstar = FALSE; |
| 10090 | static int stardepth = 0; /* depth for "**" expansion */ |
| 10091 | |
| 10092 | DIR *dirp; |
| 10093 | struct dirent *dp; |
| 10094 | |
| 10095 | /* Expanding "**" may take a long time, check for CTRL-C. */ |
| 10096 | if (stardepth > 0) |
| 10097 | { |
| 10098 | ui_breakcheck(); |
| 10099 | if (got_int) |
| 10100 | return 0; |
| 10101 | } |
| 10102 | |
| 10103 | /* make room for file name */ |
| 10104 | buf = alloc((int)STRLEN(path) + BASENAMELEN + 5); |
| 10105 | if (buf == NULL) |
| 10106 | return 0; |
| 10107 | |
| 10108 | /* |
| 10109 | * Find the first part in the path name that contains a wildcard. |
Bram Moolenaar | 2d0b92f | 2012-04-30 21:09:43 +0200 | [diff] [blame] | 10110 | * 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] | 10111 | * Copy it into "buf", including the preceding characters. |
| 10112 | */ |
| 10113 | p = buf; |
| 10114 | s = buf; |
| 10115 | e = NULL; |
| 10116 | path_end = path; |
| 10117 | while (*path_end != NUL) |
| 10118 | { |
| 10119 | /* May ignore a wildcard that has a backslash before it; it will |
| 10120 | * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ |
| 10121 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 10122 | *p++ = *path_end++; |
| 10123 | else if (*path_end == '/') |
| 10124 | { |
| 10125 | if (e != NULL) |
| 10126 | break; |
| 10127 | s = p + 1; |
| 10128 | } |
| 10129 | else if (path_end >= path + wildoff |
Bram Moolenaar | 2d0b92f | 2012-04-30 21:09:43 +0200 | [diff] [blame] | 10130 | && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 10131 | || (!p_fic && (flags & EW_ICASE) |
| 10132 | && isalpha(PTR2CHAR(path_end))))) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10133 | e = p; |
| 10134 | #ifdef FEAT_MBYTE |
| 10135 | if (has_mbyte) |
| 10136 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10137 | len = (*mb_ptr2len)(path_end); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10138 | STRNCPY(p, path_end, len); |
| 10139 | p += len; |
| 10140 | path_end += len; |
| 10141 | } |
| 10142 | else |
| 10143 | #endif |
| 10144 | *p++ = *path_end++; |
| 10145 | } |
| 10146 | e = p; |
| 10147 | *e = NUL; |
| 10148 | |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 10149 | /* Now we have one wildcard component between "s" and "e". */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10150 | /* Remove backslashes between "wildoff" and the start of the wildcard |
| 10151 | * component. */ |
| 10152 | for (p = buf + wildoff; p < s; ++p) |
| 10153 | if (rem_backslash(p)) |
| 10154 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 10155 | STRMOVE(p, p + 1); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10156 | --e; |
| 10157 | --s; |
| 10158 | } |
| 10159 | |
| 10160 | /* Check for "**" between "s" and "e". */ |
| 10161 | for (p = s; p < e; ++p) |
| 10162 | if (p[0] == '*' && p[1] == '*') |
| 10163 | starstar = TRUE; |
| 10164 | |
| 10165 | /* convert the file pattern to a regexp pattern */ |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10166 | starts_with_dot = *s == '.'; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10167 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 10168 | if (pat == NULL) |
| 10169 | { |
| 10170 | vim_free(buf); |
| 10171 | return 0; |
| 10172 | } |
| 10173 | |
| 10174 | /* compile the regexp into a program */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 10175 | if (flags & EW_ICASE) |
| 10176 | regmatch.rm_ic = TRUE; /* 'wildignorecase' set */ |
| 10177 | else |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 10178 | regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */ |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10179 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 10180 | ++emsg_silent; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10181 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10182 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 10183 | --emsg_silent; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10184 | vim_free(pat); |
| 10185 | |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10186 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10187 | { |
| 10188 | vim_free(buf); |
| 10189 | return 0; |
| 10190 | } |
| 10191 | |
| 10192 | /* If "**" is by itself, this is the first time we encounter it and more |
| 10193 | * is following then find matches without any directory. */ |
| 10194 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 10195 | && *path_end == '/') |
| 10196 | { |
| 10197 | STRCPY(s, path_end + 1); |
| 10198 | ++stardepth; |
| 10199 | (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 10200 | --stardepth; |
| 10201 | } |
| 10202 | |
| 10203 | /* open the directory for scanning */ |
| 10204 | *s = NUL; |
| 10205 | dirp = opendir(*buf == NUL ? "." : (char *)buf); |
| 10206 | |
| 10207 | /* Find all matching entries */ |
| 10208 | if (dirp != NULL) |
| 10209 | { |
| 10210 | for (;;) |
| 10211 | { |
| 10212 | dp = readdir(dirp); |
| 10213 | if (dp == NULL) |
| 10214 | break; |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10215 | if ((dp->d_name[0] != '.' || starts_with_dot |
| 10216 | || ((flags & EW_DODOT) |
| 10217 | && dp->d_name[1] != NUL |
| 10218 | && (dp->d_name[1] != '.' || dp->d_name[2] != NUL))) |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10219 | && ((regmatch.regprog != NULL && vim_regexec(®match, |
| 10220 | (char_u *)dp->d_name, (colnr_T)0)) |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 10221 | || ((flags & EW_NOTWILD) |
| 10222 | && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0))) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10223 | { |
| 10224 | STRCPY(s, dp->d_name); |
| 10225 | len = STRLEN(buf); |
| 10226 | |
| 10227 | if (starstar && stardepth < 100) |
| 10228 | { |
| 10229 | /* For "**" in the pattern first go deeper in the tree to |
| 10230 | * find matches. */ |
| 10231 | STRCPY(buf + len, "/**"); |
| 10232 | STRCPY(buf + len + 3, path_end); |
| 10233 | ++stardepth; |
| 10234 | (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); |
| 10235 | --stardepth; |
| 10236 | } |
| 10237 | |
| 10238 | STRCPY(buf + len, path_end); |
| 10239 | if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */ |
| 10240 | { |
| 10241 | /* need to expand another component of the path */ |
| 10242 | /* remove backslashes for the remaining components only */ |
| 10243 | (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); |
| 10244 | } |
| 10245 | else |
| 10246 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 10247 | stat_T sb; |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 10248 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10249 | /* no more wildcards, check if there is a match */ |
| 10250 | /* remove backslashes for the remaining components only */ |
| 10251 | if (*path_end != NUL) |
| 10252 | backslash_halve(buf + len + 1); |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 10253 | /* add existing file or symbolic link */ |
Bram Moolenaar | ab11a59 | 2015-03-06 22:00:11 +0100 | [diff] [blame] | 10254 | if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0 |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 10255 | : mch_getperm(buf) >= 0) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10256 | { |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 10257 | #ifdef MACOS_CONVERT |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10258 | size_t precomp_len = STRLEN(buf)+1; |
| 10259 | char_u *precomp_buf = |
| 10260 | mac_precompose_path(buf, precomp_len, &precomp_len); |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 10261 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10262 | if (precomp_buf) |
| 10263 | { |
| 10264 | mch_memmove(buf, precomp_buf, precomp_len); |
| 10265 | vim_free(precomp_buf); |
| 10266 | } |
| 10267 | #endif |
| 10268 | addfile(gap, buf, flags); |
| 10269 | } |
| 10270 | } |
| 10271 | } |
| 10272 | } |
| 10273 | |
| 10274 | closedir(dirp); |
| 10275 | } |
| 10276 | |
| 10277 | vim_free(buf); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10278 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10279 | |
| 10280 | matches = gap->ga_len - start_len; |
| 10281 | if (matches > 0) |
| 10282 | qsort(((char_u **)gap->ga_data) + start_len, matches, |
| 10283 | sizeof(char_u *), pstrcmp); |
| 10284 | return matches; |
| 10285 | } |
| 10286 | #endif |
| 10287 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10288 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 10289 | static int find_previous_pathsep(char_u *path, char_u **psep); |
| 10290 | static int is_unique(char_u *maybe_unique, garray_T *gap, int i); |
| 10291 | static void expand_path_option(char_u *curdir, garray_T *gap); |
| 10292 | static char_u *get_path_cutoff(char_u *fname, garray_T *gap); |
| 10293 | static void uniquefy_paths(garray_T *gap, char_u *pattern); |
| 10294 | static int expand_in_path(garray_T *gap, char_u *pattern, int flags); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10295 | |
| 10296 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10297 | * Moves "*psep" back to the previous path separator in "path". |
| 10298 | * Returns FAIL is "*psep" ends up at the beginning of "path". |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10299 | */ |
| 10300 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10301 | find_previous_pathsep(char_u *path, char_u **psep) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10302 | { |
| 10303 | /* skip the current separator */ |
| 10304 | if (*psep > path && vim_ispathsep(**psep)) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10305 | --*psep; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10306 | |
| 10307 | /* find the previous separator */ |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10308 | while (*psep > path) |
| 10309 | { |
| 10310 | if (vim_ispathsep(**psep)) |
| 10311 | return OK; |
| 10312 | mb_ptr_back(path, *psep); |
| 10313 | } |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10314 | |
| 10315 | return FAIL; |
| 10316 | } |
| 10317 | |
| 10318 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10319 | * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap". |
| 10320 | * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]". |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10321 | */ |
| 10322 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10323 | is_unique(char_u *maybe_unique, garray_T *gap, int i) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10324 | { |
| 10325 | int j; |
| 10326 | int candidate_len; |
| 10327 | int other_path_len; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10328 | char_u **other_paths = (char_u **)gap->ga_data; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10329 | char_u *rival; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10330 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10331 | for (j = 0; j < gap->ga_len; j++) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10332 | { |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10333 | if (j == i) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10334 | continue; /* don't compare it with itself */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10335 | |
Bram Moolenaar | 624c7aa | 2010-07-16 20:38:52 +0200 | [diff] [blame] | 10336 | candidate_len = (int)STRLEN(maybe_unique); |
| 10337 | other_path_len = (int)STRLEN(other_paths[j]); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10338 | if (other_path_len < candidate_len) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10339 | continue; /* it's different when it's shorter */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10340 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10341 | rival = other_paths[j] + other_path_len - candidate_len; |
Bram Moolenaar | da9836c | 2010-08-16 21:53:27 +0200 | [diff] [blame] | 10342 | if (fnamecmp(maybe_unique, rival) == 0 |
| 10343 | && (rival == other_paths[j] || vim_ispathsep(*(rival - 1)))) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10344 | return FALSE; /* match */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10345 | } |
| 10346 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10347 | return TRUE; /* no match found */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10348 | } |
| 10349 | |
| 10350 | /* |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 10351 | * Split the 'path' option into an array of strings in garray_T. Relative |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10352 | * paths are expanded to their equivalent fullpath. This includes the "." |
| 10353 | * (relative to current buffer directory) and empty path (relative to current |
| 10354 | * directory) notations. |
| 10355 | * |
| 10356 | * TODO: handle upward search (;) and path limiter (**N) notations by |
| 10357 | * expanding each into their equivalent path(s). |
| 10358 | */ |
| 10359 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10360 | expand_path_option(char_u *curdir, garray_T *gap) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10361 | { |
| 10362 | char_u *path_option = *curbuf->b_p_path == NUL |
| 10363 | ? p_path : curbuf->b_p_path; |
| 10364 | char_u *buf; |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10365 | char_u *p; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10366 | int len; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10367 | |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10368 | if ((buf = alloc((int)MAXPATHL)) == NULL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10369 | return; |
| 10370 | |
| 10371 | while (*path_option != NUL) |
| 10372 | { |
| 10373 | copy_option_part(&path_option, buf, MAXPATHL, " ,"); |
| 10374 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10375 | if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1]))) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10376 | { |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10377 | /* Relative to current buffer: |
| 10378 | * "/path/file" + "." -> "/path/" |
| 10379 | * "/path/file" + "./subdir" -> "/path/subdir" */ |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10380 | if (curbuf->b_ffname == NULL) |
| 10381 | continue; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10382 | p = gettail(curbuf->b_ffname); |
| 10383 | len = (int)(p - curbuf->b_ffname); |
| 10384 | if (len + (int)STRLEN(buf) >= MAXPATHL) |
| 10385 | continue; |
| 10386 | if (buf[1] == NUL) |
| 10387 | buf[len] = NUL; |
| 10388 | else |
| 10389 | STRMOVE(buf + len, buf + 2); |
| 10390 | mch_memmove(buf, curbuf->b_ffname, len); |
| 10391 | simplify_filename(buf); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10392 | } |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10393 | else if (buf[0] == NUL) |
| 10394 | /* relative to current directory */ |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10395 | STRCPY(buf, curdir); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 10396 | else if (path_with_url(buf)) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10397 | /* URL can't be used here */ |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 10398 | continue; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10399 | else if (!mch_isFullName(buf)) |
| 10400 | { |
| 10401 | /* Expand relative path to their full path equivalent */ |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10402 | len = (int)STRLEN(curdir); |
| 10403 | if (len + (int)STRLEN(buf) + 3 > MAXPATHL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10404 | continue; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10405 | STRMOVE(buf + len + 1, buf); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10406 | STRCPY(buf, curdir); |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10407 | buf[len] = PATHSEP; |
Bram Moolenaar | 57adda1 | 2010-08-03 22:11:29 +0200 | [diff] [blame] | 10408 | simplify_filename(buf); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10409 | } |
| 10410 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10411 | if (ga_grow(gap, 1) == FAIL) |
| 10412 | break; |
Bram Moolenaar | 811fe63 | 2013-04-24 17:34:20 +0200 | [diff] [blame] | 10413 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10414 | # if defined(MSWIN) |
Bram Moolenaar | 811fe63 | 2013-04-24 17:34:20 +0200 | [diff] [blame] | 10415 | /* Avoid the path ending in a backslash, it fails when a comma is |
| 10416 | * appended. */ |
Bram Moolenaar | 4e0d974 | 2013-05-04 03:40:27 +0200 | [diff] [blame] | 10417 | len = (int)STRLEN(buf); |
Bram Moolenaar | 811fe63 | 2013-04-24 17:34:20 +0200 | [diff] [blame] | 10418 | if (buf[len - 1] == '\\') |
| 10419 | buf[len - 1] = '/'; |
| 10420 | # endif |
| 10421 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10422 | p = vim_strsave(buf); |
| 10423 | if (p == NULL) |
| 10424 | break; |
| 10425 | ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10426 | } |
| 10427 | |
| 10428 | vim_free(buf); |
| 10429 | } |
| 10430 | |
| 10431 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10432 | * Returns a pointer to the file or directory name in "fname" that matches the |
| 10433 | * longest path in "ga"p, or NULL if there is no match. For example: |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10434 | * |
| 10435 | * path: /foo/bar/baz |
| 10436 | * fname: /foo/bar/baz/quux.txt |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10437 | * returns: ^this |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10438 | */ |
| 10439 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10440 | get_path_cutoff(char_u *fname, garray_T *gap) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10441 | { |
| 10442 | int i; |
| 10443 | int maxlen = 0; |
| 10444 | char_u **path_part = (char_u **)gap->ga_data; |
| 10445 | char_u *cutoff = NULL; |
| 10446 | |
| 10447 | for (i = 0; i < gap->ga_len; i++) |
| 10448 | { |
| 10449 | int j = 0; |
| 10450 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10451 | while ((fname[j] == path_part[i][j] |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10452 | # if defined(MSWIN) |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10453 | || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j])) |
| 10454 | #endif |
| 10455 | ) && fname[j] != NUL && path_part[i][j] != NUL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10456 | j++; |
| 10457 | if (j > maxlen) |
| 10458 | { |
| 10459 | maxlen = j; |
| 10460 | cutoff = &fname[j]; |
| 10461 | } |
| 10462 | } |
| 10463 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10464 | /* skip to the file or directory name */ |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10465 | if (cutoff != NULL) |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10466 | while (vim_ispathsep(*cutoff)) |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10467 | mb_ptr_adv(cutoff); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10468 | |
| 10469 | return cutoff; |
| 10470 | } |
| 10471 | |
| 10472 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10473 | * Sorts, removes duplicates and modifies all the fullpath names in "gap" so |
| 10474 | * that they are unique with respect to each other while conserving the part |
| 10475 | * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len". |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10476 | */ |
| 10477 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10478 | uniquefy_paths(garray_T *gap, char_u *pattern) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10479 | { |
Bram Moolenaar | cb9d45c | 2010-07-20 18:10:15 +0200 | [diff] [blame] | 10480 | int i; |
| 10481 | int len; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10482 | char_u **fnames = (char_u **)gap->ga_data; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10483 | int sort_again = FALSE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10484 | char_u *pat; |
| 10485 | char_u *file_pattern; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10486 | char_u *curdir; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10487 | regmatch_T regmatch; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10488 | garray_T path_ga; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10489 | char_u **in_curdir = NULL; |
| 10490 | char_u *short_name; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10491 | |
Bram Moolenaar | cb9d45c | 2010-07-20 18:10:15 +0200 | [diff] [blame] | 10492 | remove_duplicates(gap); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10493 | ga_init2(&path_ga, (int)sizeof(char_u *), 1); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10494 | |
| 10495 | /* |
| 10496 | * We need to prepend a '*' at the beginning of file_pattern so that the |
| 10497 | * regex matches anywhere in the path. FIXME: is this valid for all |
Bram Moolenaar | b31e438 | 2010-07-24 16:01:56 +0200 | [diff] [blame] | 10498 | * possible patterns? |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10499 | */ |
Bram Moolenaar | 624c7aa | 2010-07-16 20:38:52 +0200 | [diff] [blame] | 10500 | len = (int)STRLEN(pattern); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10501 | file_pattern = alloc(len + 2); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10502 | if (file_pattern == NULL) |
| 10503 | return; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10504 | file_pattern[0] = '*'; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10505 | file_pattern[1] = NUL; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10506 | STRCAT(file_pattern, pattern); |
| 10507 | pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE); |
| 10508 | vim_free(file_pattern); |
Bram Moolenaar | b31e438 | 2010-07-24 16:01:56 +0200 | [diff] [blame] | 10509 | if (pat == NULL) |
| 10510 | return; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10511 | |
Bram Moolenaar | b31e438 | 2010-07-24 16:01:56 +0200 | [diff] [blame] | 10512 | regmatch.rm_ic = TRUE; /* always ignore case */ |
| 10513 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); |
| 10514 | vim_free(pat); |
| 10515 | if (regmatch.regprog == NULL) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10516 | return; |
| 10517 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10518 | if ((curdir = alloc((int)(MAXPATHL))) == NULL) |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10519 | goto theend; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10520 | mch_dirname(curdir, MAXPATHL); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10521 | expand_path_option(curdir, &path_ga); |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10522 | |
| 10523 | in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *)); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10524 | if (in_curdir == NULL) |
| 10525 | goto theend; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10526 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10527 | for (i = 0; i < gap->ga_len && !got_int; i++) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10528 | { |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10529 | char_u *path = fnames[i]; |
| 10530 | int is_in_curdir; |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10531 | char_u *dir_end = gettail_dir(path); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10532 | char_u *pathsep_p; |
| 10533 | char_u *path_cutoff; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10534 | |
Bram Moolenaar | 624c7aa | 2010-07-16 20:38:52 +0200 | [diff] [blame] | 10535 | len = (int)STRLEN(path); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10536 | is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0 |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10537 | && curdir[dir_end - path] == NUL; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10538 | if (is_in_curdir) |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10539 | in_curdir[i] = vim_strsave(path); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10540 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10541 | /* Shorten the filename while maintaining its uniqueness */ |
| 10542 | path_cutoff = get_path_cutoff(path, &path_ga); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10543 | |
Bram Moolenaar | 73d4e4c | 2016-08-27 21:55:13 +0200 | [diff] [blame] | 10544 | /* Don't assume all files can be reached without path when search |
| 10545 | * pattern starts with star star slash, so only remove path_cutoff |
| 10546 | * when possible. */ |
| 10547 | if (pattern[0] == '*' && pattern[1] == '*' |
| 10548 | && vim_ispathsep_nocolon(pattern[2]) |
| 10549 | && path_cutoff != NULL |
| 10550 | && vim_regexec(®match, path_cutoff, (colnr_T)0) |
| 10551 | && is_unique(path_cutoff, gap, i)) |
| 10552 | { |
| 10553 | sort_again = TRUE; |
| 10554 | mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1); |
| 10555 | } |
| 10556 | else |
| 10557 | { |
| 10558 | /* Here all files can be reached without path, so get shortest |
| 10559 | * unique path. We start at the end of the path. */ |
| 10560 | pathsep_p = path + len - 1; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10561 | |
Bram Moolenaar | 73d4e4c | 2016-08-27 21:55:13 +0200 | [diff] [blame] | 10562 | while (find_previous_pathsep(path, &pathsep_p)) |
| 10563 | if (vim_regexec(®match, pathsep_p + 1, (colnr_T)0) |
| 10564 | && is_unique(pathsep_p + 1, gap, i) |
| 10565 | && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff) |
| 10566 | { |
| 10567 | sort_again = TRUE; |
| 10568 | mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p)); |
| 10569 | break; |
| 10570 | } |
| 10571 | } |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10572 | |
| 10573 | if (mch_isFullName(path)) |
| 10574 | { |
| 10575 | /* |
| 10576 | * Last resort: shorten relative to curdir if possible. |
| 10577 | * 'possible' means: |
| 10578 | * 1. It is under the current directory. |
| 10579 | * 2. The result is actually shorter than the original. |
| 10580 | * |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10581 | * Before curdir After |
| 10582 | * /foo/bar/file.txt /foo/bar ./file.txt |
| 10583 | * c:\foo\bar\file.txt c:\foo\bar .\file.txt |
| 10584 | * /file.txt / /file.txt |
| 10585 | * c:\file.txt c:\ .\file.txt |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10586 | */ |
| 10587 | short_name = shorten_fname(path, curdir); |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10588 | if (short_name != NULL && short_name > path + 1 |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10589 | #if defined(MSWIN) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10590 | /* On windows, |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10591 | * shorten_fname("c:\a\a.txt", "c:\a\b") |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10592 | * returns "\a\a.txt", which is not really the short |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10593 | * name, hence: */ |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10594 | && !vim_ispathsep(*short_name) |
| 10595 | #endif |
| 10596 | ) |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10597 | { |
| 10598 | STRCPY(path, "."); |
| 10599 | add_pathsep(path); |
Bram Moolenaar | cda000e | 2010-08-14 13:34:39 +0200 | [diff] [blame] | 10600 | STRMOVE(path + STRLEN(path), short_name); |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10601 | } |
| 10602 | } |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10603 | ui_breakcheck(); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10604 | } |
| 10605 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10606 | /* Shorten filenames in /in/current/directory/{filename} */ |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10607 | for (i = 0; i < gap->ga_len && !got_int; i++) |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10608 | { |
| 10609 | char_u *rel_path; |
| 10610 | char_u *path = in_curdir[i]; |
| 10611 | |
| 10612 | if (path == NULL) |
| 10613 | continue; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10614 | |
| 10615 | /* If the {filename} is not unique, change it to ./{filename}. |
| 10616 | * Else reduce it to {filename} */ |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10617 | short_name = shorten_fname(path, curdir); |
| 10618 | if (short_name == NULL) |
| 10619 | short_name = path; |
| 10620 | if (is_unique(short_name, gap, i)) |
| 10621 | { |
| 10622 | STRCPY(fnames[i], short_name); |
| 10623 | continue; |
| 10624 | } |
| 10625 | |
| 10626 | rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2)); |
| 10627 | if (rel_path == NULL) |
| 10628 | goto theend; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10629 | STRCPY(rel_path, "."); |
| 10630 | add_pathsep(rel_path); |
| 10631 | STRCAT(rel_path, short_name); |
| 10632 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10633 | vim_free(fnames[i]); |
| 10634 | fnames[i] = rel_path; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10635 | sort_again = TRUE; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10636 | ui_breakcheck(); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10637 | } |
| 10638 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10639 | theend: |
| 10640 | vim_free(curdir); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10641 | if (in_curdir != NULL) |
| 10642 | { |
| 10643 | for (i = 0; i < gap->ga_len; i++) |
| 10644 | vim_free(in_curdir[i]); |
| 10645 | vim_free(in_curdir); |
| 10646 | } |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10647 | ga_clear_strings(&path_ga); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10648 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10649 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10650 | if (sort_again) |
Bram Moolenaar | cb9d45c | 2010-07-20 18:10:15 +0200 | [diff] [blame] | 10651 | remove_duplicates(gap); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10652 | } |
| 10653 | |
| 10654 | /* |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10655 | * Calls globpath() with 'path' values for the given pattern and stores the |
| 10656 | * result in "gap". |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10657 | * Returns the total number of matches. |
| 10658 | */ |
| 10659 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10660 | expand_in_path( |
| 10661 | garray_T *gap, |
| 10662 | char_u *pattern, |
| 10663 | int flags) /* EW_* flags */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10664 | { |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10665 | char_u *curdir; |
| 10666 | garray_T path_ga; |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10667 | char_u *paths = NULL; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10668 | |
Bram Moolenaar | 7f0f621 | 2010-08-03 22:21:00 +0200 | [diff] [blame] | 10669 | if ((curdir = alloc((unsigned)MAXPATHL)) == NULL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10670 | return 0; |
| 10671 | mch_dirname(curdir, MAXPATHL); |
| 10672 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10673 | ga_init2(&path_ga, (int)sizeof(char_u *), 1); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10674 | expand_path_option(curdir, &path_ga); |
| 10675 | vim_free(curdir); |
Bram Moolenaar | 006d2b0 | 2010-08-04 12:39:44 +0200 | [diff] [blame] | 10676 | if (path_ga.ga_len == 0) |
| 10677 | return 0; |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10678 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 10679 | paths = ga_concat_strings(&path_ga, ","); |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10680 | ga_clear_strings(&path_ga); |
| 10681 | if (paths == NULL) |
Bram Moolenaar | 7f0f621 | 2010-08-03 22:21:00 +0200 | [diff] [blame] | 10682 | return 0; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10683 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 10684 | globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10685 | vim_free(paths); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10686 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10687 | return gap->ga_len; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10688 | } |
| 10689 | #endif |
| 10690 | |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 10691 | #if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 10692 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10693 | * Sort "gap" and remove duplicate entries. "gap" is expected to contain a |
| 10694 | * list of file names in allocated memory. |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 10695 | */ |
| 10696 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10697 | remove_duplicates(garray_T *gap) |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 10698 | { |
| 10699 | int i; |
| 10700 | int j; |
| 10701 | char_u **fnames = (char_u **)gap->ga_data; |
| 10702 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10703 | sort_strings(fnames, gap->ga_len); |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 10704 | for (i = gap->ga_len - 1; i > 0; --i) |
| 10705 | if (fnamecmp(fnames[i - 1], fnames[i]) == 0) |
| 10706 | { |
| 10707 | vim_free(fnames[i]); |
| 10708 | for (j = i + 1; j < gap->ga_len; ++j) |
| 10709 | fnames[j - 1] = fnames[j]; |
| 10710 | --gap->ga_len; |
| 10711 | } |
| 10712 | } |
| 10713 | #endif |
| 10714 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 10715 | static int has_env_var(char_u *p); |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10716 | |
| 10717 | /* |
| 10718 | * Return TRUE if "p" contains what looks like an environment variable. |
| 10719 | * Allowing for escaping. |
| 10720 | */ |
| 10721 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10722 | has_env_var(char_u *p) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10723 | { |
| 10724 | for ( ; *p; mb_ptr_adv(p)) |
| 10725 | { |
| 10726 | if (*p == '\\' && p[1] != NUL) |
| 10727 | ++p; |
| 10728 | else if (vim_strchr((char_u *) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10729 | #if defined(MSWIN) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10730 | "$%" |
| 10731 | #else |
| 10732 | "$" |
| 10733 | #endif |
| 10734 | , *p) != NULL) |
| 10735 | return TRUE; |
| 10736 | } |
| 10737 | return FALSE; |
| 10738 | } |
| 10739 | |
| 10740 | #ifdef SPECIAL_WILDCHAR |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 10741 | static int has_special_wildchar(char_u *p); |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10742 | |
| 10743 | /* |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 10744 | * Return TRUE if "p" contains a special wildcard character, one that Vim |
| 10745 | * cannot expand, requires using a shell. |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10746 | */ |
| 10747 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10748 | has_special_wildchar(char_u *p) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10749 | { |
| 10750 | for ( ; *p; mb_ptr_adv(p)) |
| 10751 | { |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 10752 | /* Allow for escaping. */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10753 | if (*p == '\\' && p[1] != NUL) |
| 10754 | ++p; |
| 10755 | else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL) |
| 10756 | return TRUE; |
| 10757 | } |
| 10758 | return FALSE; |
| 10759 | } |
| 10760 | #endif |
| 10761 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10762 | /* |
| 10763 | * Generic wildcard expansion code. |
| 10764 | * |
| 10765 | * Characters in "pat" that should not be expanded must be preceded with a |
| 10766 | * backslash. E.g., "/path\ with\ spaces/my\*star*" |
| 10767 | * |
| 10768 | * Return FAIL when no single file was found. In this case "num_file" is not |
| 10769 | * set, and "file" may contain an error message. |
| 10770 | * Return OK when some files found. "num_file" is set to the number of |
| 10771 | * matches, "file" to the array of matches. Call FreeWild() later. |
| 10772 | */ |
| 10773 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10774 | gen_expand_wildcards( |
| 10775 | int num_pat, /* number of input patterns */ |
| 10776 | char_u **pat, /* array of input patterns */ |
| 10777 | int *num_file, /* resulting number of files */ |
| 10778 | char_u ***file, /* array of resulting files */ |
| 10779 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10780 | { |
| 10781 | int i; |
| 10782 | garray_T ga; |
| 10783 | char_u *p; |
| 10784 | static int recursive = FALSE; |
| 10785 | int add_pat; |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 10786 | int retval = OK; |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 10787 | #if defined(FEAT_SEARCHPATH) |
| 10788 | int did_expand_in_path = FALSE; |
| 10789 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10790 | |
| 10791 | /* |
| 10792 | * expand_env() is called to expand things like "~user". If this fails, |
| 10793 | * it calls ExpandOne(), which brings us back here. In this case, always |
| 10794 | * call the machine specific expansion function, if possible. Otherwise, |
| 10795 | * return FAIL. |
| 10796 | */ |
| 10797 | if (recursive) |
| 10798 | #ifdef SPECIAL_WILDCHAR |
| 10799 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 10800 | #else |
| 10801 | return FAIL; |
| 10802 | #endif |
| 10803 | |
| 10804 | #ifdef SPECIAL_WILDCHAR |
| 10805 | /* |
| 10806 | * If there are any special wildcard characters which we cannot handle |
| 10807 | * here, call machine specific function for all the expansion. This |
| 10808 | * avoids starting the shell for each argument separately. |
| 10809 | * For `=expr` do use the internal function. |
| 10810 | */ |
| 10811 | for (i = 0; i < num_pat; i++) |
| 10812 | { |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10813 | if (has_special_wildchar(pat[i]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10814 | # ifdef VIM_BACKTICK |
| 10815 | && !(vim_backtick(pat[i]) && pat[i][1] == '=') |
| 10816 | # endif |
| 10817 | ) |
| 10818 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 10819 | } |
| 10820 | #endif |
| 10821 | |
| 10822 | recursive = TRUE; |
| 10823 | |
| 10824 | /* |
| 10825 | * The matching file names are stored in a growarray. Init it empty. |
| 10826 | */ |
| 10827 | ga_init2(&ga, (int)sizeof(char_u *), 30); |
| 10828 | |
| 10829 | for (i = 0; i < num_pat; ++i) |
| 10830 | { |
| 10831 | add_pat = -1; |
| 10832 | p = pat[i]; |
| 10833 | |
| 10834 | #ifdef VIM_BACKTICK |
| 10835 | if (vim_backtick(p)) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 10836 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10837 | add_pat = expand_backtick(&ga, p, flags); |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 10838 | if (add_pat == -1) |
| 10839 | retval = FAIL; |
| 10840 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10841 | else |
| 10842 | #endif |
| 10843 | { |
| 10844 | /* |
| 10845 | * First expand environment variables, "~/" and "~user/". |
| 10846 | */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10847 | if (has_env_var(p) || *p == '~') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10848 | { |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 10849 | p = expand_env_save_opt(p, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10850 | if (p == NULL) |
| 10851 | p = pat[i]; |
| 10852 | #ifdef UNIX |
| 10853 | /* |
| 10854 | * On Unix, if expand_env() can't expand an environment |
| 10855 | * variable, use the shell to do that. Discard previously |
| 10856 | * found file names and start all over again. |
| 10857 | */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10858 | else if (has_env_var(p) || *p == '~') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10859 | { |
| 10860 | vim_free(p); |
Bram Moolenaar | 782027e | 2009-06-24 14:25:49 +0000 | [diff] [blame] | 10861 | ga_clear_strings(&ga); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10862 | i = mch_expand_wildcards(num_pat, pat, num_file, file, |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 10863 | flags|EW_KEEPDOLLAR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10864 | recursive = FALSE; |
| 10865 | return i; |
| 10866 | } |
| 10867 | #endif |
| 10868 | } |
| 10869 | |
| 10870 | /* |
| 10871 | * If there are wildcards: Expand file names and add each match to |
| 10872 | * the list. If there is no match, and EW_NOTFOUND is given, add |
| 10873 | * the pattern. |
| 10874 | * If there are no wildcards: Add the file name if it exists or |
| 10875 | * when EW_NOTFOUND is given. |
| 10876 | */ |
| 10877 | if (mch_has_exp_wildcard(p)) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10878 | { |
| 10879 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 10880 | if ((flags & EW_PATH) |
| 10881 | && !mch_isFullName(p) |
| 10882 | && !(p[0] == '.' |
| 10883 | && (vim_ispathsep(p[1]) |
| 10884 | || (p[1] == '.' && vim_ispathsep(p[2])))) |
| 10885 | ) |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10886 | { |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 10887 | /* :find completion where 'path' is used. |
| 10888 | * Recursiveness is OK here. */ |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10889 | recursive = FALSE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10890 | add_pat = expand_in_path(&ga, p, flags); |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10891 | recursive = TRUE; |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 10892 | did_expand_in_path = TRUE; |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10893 | } |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10894 | else |
| 10895 | #endif |
| 10896 | add_pat = mch_expandpath(&ga, p, flags); |
| 10897 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10898 | } |
| 10899 | |
| 10900 | if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) |
| 10901 | { |
| 10902 | char_u *t = backslash_halve_save(p); |
| 10903 | |
| 10904 | #if defined(MACOS_CLASSIC) |
| 10905 | slash_to_colon(t); |
| 10906 | #endif |
| 10907 | /* When EW_NOTFOUND is used, always add files and dirs. Makes |
| 10908 | * "vim c:/" work. */ |
| 10909 | if (flags & EW_NOTFOUND) |
| 10910 | addfile(&ga, t, flags | EW_DIR | EW_FILE); |
Bram Moolenaar | 00efded | 2016-07-07 14:29:10 +0200 | [diff] [blame] | 10911 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10912 | addfile(&ga, t, flags); |
| 10913 | vim_free(t); |
| 10914 | } |
| 10915 | |
Bram Moolenaar | b28ebbc | 2010-07-14 16:59:57 +0200 | [diff] [blame] | 10916 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 10917 | if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH)) |
Bram Moolenaar | b28ebbc | 2010-07-14 16:59:57 +0200 | [diff] [blame] | 10918 | uniquefy_paths(&ga, p); |
| 10919 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10920 | if (p != pat[i]) |
| 10921 | vim_free(p); |
| 10922 | } |
| 10923 | |
| 10924 | *num_file = ga.ga_len; |
| 10925 | *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; |
| 10926 | |
| 10927 | recursive = FALSE; |
| 10928 | |
Bram Moolenaar | 336bd62 | 2016-01-17 18:23:58 +0100 | [diff] [blame] | 10929 | return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10930 | } |
| 10931 | |
| 10932 | # ifdef VIM_BACKTICK |
| 10933 | |
| 10934 | /* |
| 10935 | * Return TRUE if we can expand this backtick thing here. |
| 10936 | */ |
| 10937 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10938 | vim_backtick(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10939 | { |
| 10940 | return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); |
| 10941 | } |
| 10942 | |
| 10943 | /* |
| 10944 | * Expand an item in `backticks` by executing it as a command. |
| 10945 | * Currently only works when pat[] starts and ends with a `. |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 10946 | * Returns number of file names found, -1 if an error is encountered. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10947 | */ |
| 10948 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10949 | expand_backtick( |
| 10950 | garray_T *gap, |
| 10951 | char_u *pat, |
| 10952 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10953 | { |
| 10954 | char_u *p; |
| 10955 | char_u *cmd; |
| 10956 | char_u *buffer; |
| 10957 | int cnt = 0; |
| 10958 | int i; |
| 10959 | |
| 10960 | /* Create the command: lop off the backticks. */ |
| 10961 | cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); |
| 10962 | if (cmd == NULL) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 10963 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10964 | |
| 10965 | #ifdef FEAT_EVAL |
| 10966 | if (*cmd == '=') /* `={expr}`: Expand expression */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 10967 | buffer = eval_to_string(cmd + 1, &p, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10968 | else |
| 10969 | #endif |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 10970 | buffer = get_cmd_output(cmd, NULL, |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 10971 | (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10972 | vim_free(cmd); |
| 10973 | if (buffer == NULL) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 10974 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10975 | |
| 10976 | cmd = buffer; |
| 10977 | while (*cmd != NUL) |
| 10978 | { |
| 10979 | cmd = skipwhite(cmd); /* skip over white space */ |
| 10980 | p = cmd; |
| 10981 | while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */ |
| 10982 | ++p; |
| 10983 | /* add an entry if it is not empty */ |
| 10984 | if (p > cmd) |
| 10985 | { |
| 10986 | i = *p; |
| 10987 | *p = NUL; |
| 10988 | addfile(gap, cmd, flags); |
| 10989 | *p = i; |
| 10990 | ++cnt; |
| 10991 | } |
| 10992 | cmd = p; |
| 10993 | while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) |
| 10994 | ++cmd; |
| 10995 | } |
| 10996 | |
| 10997 | vim_free(buffer); |
| 10998 | return cnt; |
| 10999 | } |
| 11000 | # endif /* VIM_BACKTICK */ |
| 11001 | |
| 11002 | /* |
| 11003 | * Add a file to a file list. Accepted flags: |
| 11004 | * EW_DIR add directories |
| 11005 | * EW_FILE add files |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 11006 | * EW_EXEC add executable files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11007 | * EW_NOTFOUND add even when it doesn't exist |
| 11008 | * EW_ADDSLASH add slash after directory name |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 11009 | * EW_ALLLINKS add symlink also when the referred file does not exist |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11010 | */ |
| 11011 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11012 | addfile( |
| 11013 | garray_T *gap, |
| 11014 | char_u *f, /* filename */ |
| 11015 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11016 | { |
| 11017 | char_u *p; |
| 11018 | int isdir; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 11019 | stat_T sb; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11020 | |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 11021 | /* if the file/dir/link doesn't exist, may not add it */ |
| 11022 | if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS) |
Bram Moolenaar | ab11a59 | 2015-03-06 22:00:11 +0100 | [diff] [blame] | 11023 | ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11024 | return; |
| 11025 | |
| 11026 | #ifdef FNAME_ILLEGAL |
| 11027 | /* if the file/dir contains illegal characters, don't add it */ |
| 11028 | if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) |
| 11029 | return; |
| 11030 | #endif |
| 11031 | |
| 11032 | isdir = mch_isdir(f); |
| 11033 | if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) |
| 11034 | return; |
| 11035 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 11036 | /* If the file isn't executable, may not add it. Do accept directories. |
| 11037 | * When invoked from expand_shellcmd() do not use $PATH. */ |
| 11038 | if (!isdir && (flags & EW_EXEC) |
| 11039 | && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD))) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 11040 | return; |
| 11041 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11042 | /* Make room for another item in the file list. */ |
| 11043 | if (ga_grow(gap, 1) == FAIL) |
| 11044 | return; |
| 11045 | |
| 11046 | p = alloc((unsigned)(STRLEN(f) + 1 + isdir)); |
| 11047 | if (p == NULL) |
| 11048 | return; |
| 11049 | |
| 11050 | STRCPY(p, f); |
| 11051 | #ifdef BACKSLASH_IN_FILENAME |
| 11052 | slash_adjust(p); |
| 11053 | #endif |
| 11054 | /* |
| 11055 | * Append a slash or backslash after directory names if none is present. |
| 11056 | */ |
| 11057 | #ifndef DONT_ADD_PATHSEP_TO_DIR |
| 11058 | if (isdir && (flags & EW_ADDSLASH)) |
| 11059 | add_pathsep(p); |
| 11060 | #endif |
| 11061 | ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11062 | } |
| 11063 | #endif /* !NO_EXPANDPATH */ |
| 11064 | |
| 11065 | #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO) |
| 11066 | |
| 11067 | #ifndef SEEK_SET |
| 11068 | # define SEEK_SET 0 |
| 11069 | #endif |
| 11070 | #ifndef SEEK_END |
| 11071 | # define SEEK_END 2 |
| 11072 | #endif |
| 11073 | |
| 11074 | /* |
| 11075 | * Get the stdout of an external command. |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11076 | * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not |
| 11077 | * NULL store the length there. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11078 | * Returns an allocated string, or NULL for error. |
| 11079 | */ |
| 11080 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11081 | get_cmd_output( |
| 11082 | char_u *cmd, |
| 11083 | char_u *infile, /* optional input file name */ |
| 11084 | int flags, /* can be SHELL_SILENT */ |
| 11085 | int *ret_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11086 | { |
| 11087 | char_u *tempname; |
| 11088 | char_u *command; |
| 11089 | char_u *buffer = NULL; |
| 11090 | int len; |
| 11091 | int i = 0; |
| 11092 | FILE *fd; |
| 11093 | |
| 11094 | if (check_restricted() || check_secure()) |
| 11095 | return NULL; |
| 11096 | |
| 11097 | /* get a name for the temp file */ |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 11098 | if ((tempname = vim_tempname('o', FALSE)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11099 | { |
| 11100 | EMSG(_(e_notmp)); |
| 11101 | return NULL; |
| 11102 | } |
| 11103 | |
| 11104 | /* Add the redirection stuff */ |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 11105 | command = make_filter_cmd(cmd, infile, tempname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11106 | if (command == NULL) |
| 11107 | goto done; |
| 11108 | |
| 11109 | /* |
| 11110 | * Call the shell to execute the command (errors are ignored). |
| 11111 | * Don't check timestamps here. |
| 11112 | */ |
| 11113 | ++no_check_timestamps; |
| 11114 | call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags); |
| 11115 | --no_check_timestamps; |
| 11116 | |
| 11117 | vim_free(command); |
| 11118 | |
| 11119 | /* |
| 11120 | * read the names from the file into memory |
| 11121 | */ |
| 11122 | # ifdef VMS |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 11123 | /* created temporary file is not always readable as binary */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11124 | fd = mch_fopen((char *)tempname, "r"); |
| 11125 | # else |
| 11126 | fd = mch_fopen((char *)tempname, READBIN); |
| 11127 | # endif |
| 11128 | |
| 11129 | if (fd == NULL) |
| 11130 | { |
| 11131 | EMSG2(_(e_notopen), tempname); |
| 11132 | goto done; |
| 11133 | } |
| 11134 | |
| 11135 | fseek(fd, 0L, SEEK_END); |
| 11136 | len = ftell(fd); /* get size of temp file */ |
| 11137 | fseek(fd, 0L, SEEK_SET); |
| 11138 | |
| 11139 | buffer = alloc(len + 1); |
| 11140 | if (buffer != NULL) |
| 11141 | i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd); |
| 11142 | fclose(fd); |
| 11143 | mch_remove(tempname); |
| 11144 | if (buffer == NULL) |
| 11145 | goto done; |
| 11146 | #ifdef VMS |
| 11147 | len = i; /* VMS doesn't give us what we asked for... */ |
| 11148 | #endif |
| 11149 | if (i != len) |
| 11150 | { |
| 11151 | EMSG2(_(e_notread), tempname); |
| 11152 | vim_free(buffer); |
| 11153 | buffer = NULL; |
| 11154 | } |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11155 | else if (ret_len == NULL) |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 11156 | { |
| 11157 | /* Change NUL into SOH, otherwise the string is truncated. */ |
| 11158 | for (i = 0; i < len; ++i) |
Bram Moolenaar | f40f4ab | 2013-08-03 17:31:28 +0200 | [diff] [blame] | 11159 | if (buffer[i] == NUL) |
| 11160 | buffer[i] = 1; |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 11161 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 11162 | buffer[len] = NUL; /* make sure the buffer is terminated */ |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 11163 | } |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11164 | else |
| 11165 | *ret_len = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11166 | |
| 11167 | done: |
| 11168 | vim_free(tempname); |
| 11169 | return buffer; |
| 11170 | } |
| 11171 | #endif |
| 11172 | |
| 11173 | /* |
| 11174 | * Free the list of files returned by expand_wildcards() or other expansion |
| 11175 | * functions. |
| 11176 | */ |
| 11177 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11178 | FreeWild(int count, char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11179 | { |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 11180 | if (count <= 0 || files == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11181 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11182 | while (count--) |
| 11183 | vim_free(files[count]); |
| 11184 | vim_free(files); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11185 | } |
| 11186 | |
| 11187 | /* |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 11188 | * Return TRUE when need to go to Insert mode because of 'insertmode'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11189 | * Don't do this when still processing a command or a mapping. |
| 11190 | * Don't do this when inside a ":normal" command. |
| 11191 | */ |
| 11192 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11193 | goto_im(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11194 | { |
| 11195 | return (p_im && stuff_empty() && typebuf_typed()); |
| 11196 | } |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11197 | |
| 11198 | /* |
Bram Moolenaar | 050fe7e | 2014-05-22 14:00:16 +0200 | [diff] [blame] | 11199 | * Returns the isolated name of the shell in allocated memory: |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11200 | * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f". |
| 11201 | * - Remove any argument. E.g., "csh -f" -> "csh". |
| 11202 | * But don't allow a space in the path, so that this works: |
| 11203 | * "/usr/bin/csh --rcfile ~/.cshrc" |
| 11204 | * But don't do that for Windows, it's common to have a space in the path. |
| 11205 | */ |
| 11206 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11207 | get_isolated_shell_name(void) |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11208 | { |
| 11209 | char_u *p; |
| 11210 | |
| 11211 | #ifdef WIN3264 |
| 11212 | p = gettail(p_sh); |
| 11213 | p = vim_strnsave(p, (int)(skiptowhite(p) - p)); |
| 11214 | #else |
| 11215 | p = skiptowhite(p_sh); |
| 11216 | if (*p == NUL) |
| 11217 | { |
| 11218 | /* No white space, use the tail. */ |
| 11219 | p = vim_strsave(gettail(p_sh)); |
| 11220 | } |
| 11221 | else |
| 11222 | { |
| 11223 | char_u *p1, *p2; |
| 11224 | |
| 11225 | /* Find the last path separator before the space. */ |
| 11226 | p1 = p_sh; |
| 11227 | for (p2 = p_sh; p2 < p; mb_ptr_adv(p2)) |
| 11228 | if (vim_ispathsep(*p2)) |
| 11229 | p1 = p2 + 1; |
| 11230 | p = vim_strnsave(p1, (int)(p - p1)); |
| 11231 | } |
| 11232 | #endif |
| 11233 | return p; |
| 11234 | } |