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 */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 142 | while (todo > 0 && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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. */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 205 | if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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) */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 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 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 261 | while (todo > 0 && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 360 | while (todo > 0 && VIM_ISWHITE(*s)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 498 | const int eff_wwidth = wp->w_width |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 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 | * |
Bram Moolenaar | 24a2d72 | 2018-04-24 19:36:43 +0200 | [diff] [blame] | 591 | * Return OK for success, FAIL for failure |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 */ |
Bram Moolenaar | 24a2d72 | 2018-04-24 19:36:43 +0200 | [diff] [blame] | 609 | int retval = FAIL; /* return value */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 69a76fe | 2017-08-03 17:54:03 +0200 | [diff] [blame] | 630 | # ifdef FEAT_EVAL |
| 631 | && *curbuf->b_p_inde == NUL |
| 632 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 633 | ); |
| 634 | int no_si = FALSE; /* reset did_si afterwards */ |
| 635 | int first_char = NUL; /* init for GCC */ |
| 636 | #endif |
| 637 | #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) |
| 638 | int vreplace_mode; |
| 639 | #endif |
| 640 | int did_append; /* appended a new line */ |
| 641 | int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */ |
| 642 | |
| 643 | /* |
| 644 | * make a copy of the current line so we can mess with it |
| 645 | */ |
| 646 | saved_line = vim_strsave(ml_get_curline()); |
| 647 | if (saved_line == NULL) /* out of memory! */ |
| 648 | return FALSE; |
| 649 | |
| 650 | #ifdef FEAT_VREPLACE |
| 651 | if (State & VREPLACE_FLAG) |
| 652 | { |
| 653 | /* |
| 654 | * With VREPLACE we make a copy of the next line, which we will be |
| 655 | * starting to replace. First make the new line empty and let vim play |
| 656 | * with the indenting and comment leader to its heart's content. Then |
| 657 | * we grab what it ended up putting on the new line, put back the |
| 658 | * original line, and call ins_char() to put each new character onto |
| 659 | * the line, replacing what was there before and pushing the right |
| 660 | * stuff onto the replace stack. -- webb. |
| 661 | */ |
| 662 | if (curwin->w_cursor.lnum < orig_line_count) |
| 663 | next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1)); |
| 664 | else |
| 665 | next_line = vim_strsave((char_u *)""); |
| 666 | if (next_line == NULL) /* out of memory! */ |
| 667 | goto theend; |
| 668 | |
| 669 | /* |
| 670 | * In VREPLACE mode, a NL replaces the rest of the line, and starts |
| 671 | * replacing the next line, so push all of the characters left on the |
| 672 | * line onto the replace stack. We'll push any other characters that |
| 673 | * might be replaced at the start of the next line (due to autoindent |
| 674 | * etc) a bit later. |
| 675 | */ |
| 676 | replace_push(NUL); /* Call twice because BS over NL expects it */ |
| 677 | replace_push(NUL); |
| 678 | p = saved_line + curwin->w_cursor.col; |
| 679 | while (*p != NUL) |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 680 | { |
| 681 | #ifdef FEAT_MBYTE |
| 682 | if (has_mbyte) |
| 683 | p += replace_push_mb(p); |
| 684 | else |
| 685 | #endif |
| 686 | replace_push(*p++); |
| 687 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 688 | saved_line[curwin->w_cursor.col] = NUL; |
| 689 | } |
| 690 | #endif |
| 691 | |
| 692 | if ((State & INSERT) |
| 693 | #ifdef FEAT_VREPLACE |
| 694 | && !(State & VREPLACE_FLAG) |
| 695 | #endif |
| 696 | ) |
| 697 | { |
| 698 | p_extra = saved_line + curwin->w_cursor.col; |
| 699 | #ifdef FEAT_SMARTINDENT |
| 700 | if (do_si) /* need first char after new line break */ |
| 701 | { |
| 702 | p = skipwhite(p_extra); |
| 703 | first_char = *p; |
| 704 | } |
| 705 | #endif |
| 706 | #ifdef FEAT_COMMENTS |
| 707 | extra_len = (int)STRLEN(p_extra); |
| 708 | #endif |
| 709 | saved_char = *p_extra; |
| 710 | *p_extra = NUL; |
| 711 | } |
| 712 | |
| 713 | u_clearline(); /* cannot do "U" command when adding lines */ |
| 714 | #ifdef FEAT_SMARTINDENT |
| 715 | did_si = FALSE; |
| 716 | #endif |
| 717 | ai_col = 0; |
| 718 | |
| 719 | /* |
| 720 | * If we just did an auto-indent, then we didn't type anything on |
| 721 | * the prior line, and it should be truncated. Do this even if 'ai' is not |
| 722 | * set because automatically inserting a comment leader also sets did_ai. |
| 723 | */ |
| 724 | if (dir == FORWARD && did_ai) |
| 725 | trunc_line = TRUE; |
| 726 | |
| 727 | /* |
| 728 | * If 'autoindent' and/or 'smartindent' is set, try to figure out what |
| 729 | * indent to use for the new line. |
| 730 | */ |
| 731 | if (curbuf->b_p_ai |
| 732 | #ifdef FEAT_SMARTINDENT |
| 733 | || do_si |
| 734 | #endif |
| 735 | ) |
| 736 | { |
| 737 | /* |
| 738 | * count white space on current line |
| 739 | */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 740 | newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 741 | if (newindent == 0 && !(flags & OPENLINE_COM_LIST)) |
| 742 | newindent = second_line_indent; /* for ^^D command in insert mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 743 | |
| 744 | #ifdef FEAT_SMARTINDENT |
| 745 | /* |
| 746 | * Do smart indenting. |
| 747 | * In insert/replace mode (only when dir == FORWARD) |
| 748 | * we may move some text to the next line. If it starts with '{' |
| 749 | * don't add an indent. Fixes inserting a NL before '{' in line |
| 750 | * "if (condition) {" |
| 751 | */ |
| 752 | if (!trunc_line && do_si && *saved_line != NUL |
| 753 | && (p_extra == NULL || first_char != '{')) |
| 754 | { |
| 755 | char_u *ptr; |
| 756 | char_u last_char; |
| 757 | |
| 758 | old_cursor = curwin->w_cursor; |
| 759 | ptr = saved_line; |
| 760 | # ifdef FEAT_COMMENTS |
| 761 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 762 | lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 763 | else |
| 764 | lead_len = 0; |
| 765 | # endif |
| 766 | if (dir == FORWARD) |
| 767 | { |
| 768 | /* |
| 769 | * Skip preprocessor directives, unless they are |
| 770 | * recognised as comments. |
| 771 | */ |
| 772 | if ( |
| 773 | # ifdef FEAT_COMMENTS |
| 774 | lead_len == 0 && |
| 775 | # endif |
| 776 | ptr[0] == '#') |
| 777 | { |
| 778 | while (ptr[0] == '#' && curwin->w_cursor.lnum > 1) |
| 779 | ptr = ml_get(--curwin->w_cursor.lnum); |
| 780 | newindent = get_indent(); |
| 781 | } |
| 782 | # ifdef FEAT_COMMENTS |
| 783 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 784 | lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 785 | else |
| 786 | lead_len = 0; |
| 787 | if (lead_len > 0) |
| 788 | { |
| 789 | /* |
| 790 | * This case gets the following right: |
| 791 | * \* |
| 792 | * * A comment (read '\' as '/'). |
| 793 | * *\ |
| 794 | * #define IN_THE_WAY |
| 795 | * This should line up here; |
| 796 | */ |
| 797 | p = skipwhite(ptr); |
| 798 | if (p[0] == '/' && p[1] == '*') |
| 799 | p++; |
| 800 | if (p[0] == '*') |
| 801 | { |
| 802 | for (p++; *p; p++) |
| 803 | { |
| 804 | if (p[0] == '/' && p[-1] == '*') |
| 805 | { |
| 806 | /* |
| 807 | * End of C comment, indent should line up |
| 808 | * with the line containing the start of |
| 809 | * the comment |
| 810 | */ |
| 811 | curwin->w_cursor.col = (colnr_T)(p - ptr); |
| 812 | if ((pos = findmatch(NULL, NUL)) != NULL) |
| 813 | { |
| 814 | curwin->w_cursor.lnum = pos->lnum; |
| 815 | newindent = get_indent(); |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | else /* Not a comment line */ |
| 822 | # endif |
| 823 | { |
| 824 | /* Find last non-blank in line */ |
| 825 | p = ptr + STRLEN(ptr) - 1; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 826 | while (p > ptr && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 827 | --p; |
| 828 | last_char = *p; |
| 829 | |
| 830 | /* |
| 831 | * find the character just before the '{' or ';' |
| 832 | */ |
| 833 | if (last_char == '{' || last_char == ';') |
| 834 | { |
| 835 | if (p > ptr) |
| 836 | --p; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 837 | while (p > ptr && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 838 | --p; |
| 839 | } |
| 840 | /* |
| 841 | * Try to catch lines that are split over multiple |
| 842 | * lines. eg: |
| 843 | * if (condition && |
| 844 | * condition) { |
| 845 | * Should line up here! |
| 846 | * } |
| 847 | */ |
| 848 | if (*p == ')') |
| 849 | { |
| 850 | curwin->w_cursor.col = (colnr_T)(p - ptr); |
| 851 | if ((pos = findmatch(NULL, '(')) != NULL) |
| 852 | { |
| 853 | curwin->w_cursor.lnum = pos->lnum; |
| 854 | newindent = get_indent(); |
| 855 | ptr = ml_get_curline(); |
| 856 | } |
| 857 | } |
| 858 | /* |
| 859 | * If last character is '{' do indent, without |
| 860 | * checking for "if" and the like. |
| 861 | */ |
| 862 | if (last_char == '{') |
| 863 | { |
| 864 | did_si = TRUE; /* do indent */ |
| 865 | no_si = TRUE; /* don't delete it when '{' typed */ |
| 866 | } |
| 867 | /* |
| 868 | * Look for "if" and the like, use 'cinwords'. |
| 869 | * Don't do this if the previous line ended in ';' or |
| 870 | * '}'. |
| 871 | */ |
| 872 | else if (last_char != ';' && last_char != '}' |
| 873 | && cin_is_cinword(ptr)) |
| 874 | did_si = TRUE; |
| 875 | } |
| 876 | } |
| 877 | else /* dir == BACKWARD */ |
| 878 | { |
| 879 | /* |
| 880 | * Skip preprocessor directives, unless they are |
| 881 | * recognised as comments. |
| 882 | */ |
| 883 | if ( |
| 884 | # ifdef FEAT_COMMENTS |
| 885 | lead_len == 0 && |
| 886 | # endif |
| 887 | ptr[0] == '#') |
| 888 | { |
| 889 | int was_backslashed = FALSE; |
| 890 | |
| 891 | while ((ptr[0] == '#' || was_backslashed) && |
| 892 | curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 893 | { |
| 894 | if (*ptr && ptr[STRLEN(ptr) - 1] == '\\') |
| 895 | was_backslashed = TRUE; |
| 896 | else |
| 897 | was_backslashed = FALSE; |
| 898 | ptr = ml_get(++curwin->w_cursor.lnum); |
| 899 | } |
| 900 | if (was_backslashed) |
| 901 | newindent = 0; /* Got to end of file */ |
| 902 | else |
| 903 | newindent = get_indent(); |
| 904 | } |
| 905 | p = skipwhite(ptr); |
| 906 | if (*p == '}') /* if line starts with '}': do indent */ |
| 907 | did_si = TRUE; |
| 908 | else /* can delete indent when '{' typed */ |
| 909 | can_si_back = TRUE; |
| 910 | } |
| 911 | curwin->w_cursor = old_cursor; |
| 912 | } |
| 913 | if (do_si) |
| 914 | can_si = TRUE; |
| 915 | #endif /* FEAT_SMARTINDENT */ |
| 916 | |
| 917 | did_ai = TRUE; |
| 918 | } |
| 919 | |
| 920 | #ifdef FEAT_COMMENTS |
| 921 | /* |
| 922 | * Find out if the current line starts with a comment leader. |
| 923 | * This may then be inserted in front of the new line. |
| 924 | */ |
| 925 | end_comment_pending = NUL; |
| 926 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 927 | lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 928 | else |
| 929 | lead_len = 0; |
| 930 | if (lead_len > 0) |
| 931 | { |
| 932 | char_u *lead_repl = NULL; /* replaces comment leader */ |
| 933 | int lead_repl_len = 0; /* length of *lead_repl */ |
| 934 | char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ |
| 935 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 936 | char_u *comment_end = NULL; /* where lead_end has been found */ |
| 937 | int extra_space = FALSE; /* append extra space */ |
| 938 | int current_flag; |
| 939 | int require_blank = FALSE; /* requires blank after middle */ |
| 940 | char_u *p2; |
| 941 | |
| 942 | /* |
| 943 | * If the comment leader has the start, middle or end flag, it may not |
| 944 | * be used or may be replaced with the middle leader. |
| 945 | */ |
| 946 | for (p = lead_flags; *p && *p != ':'; ++p) |
| 947 | { |
| 948 | if (*p == COM_BLANK) |
| 949 | { |
| 950 | require_blank = TRUE; |
| 951 | continue; |
| 952 | } |
| 953 | if (*p == COM_START || *p == COM_MIDDLE) |
| 954 | { |
| 955 | current_flag = *p; |
| 956 | if (*p == COM_START) |
| 957 | { |
| 958 | /* |
| 959 | * Doing "O" on a start of comment does not insert leader. |
| 960 | */ |
| 961 | if (dir == BACKWARD) |
| 962 | { |
| 963 | lead_len = 0; |
| 964 | break; |
| 965 | } |
| 966 | |
| 967 | /* find start of middle part */ |
| 968 | (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); |
| 969 | require_blank = FALSE; |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | * Isolate the strings of the middle and end leader. |
| 974 | */ |
| 975 | while (*p && p[-1] != ':') /* find end of middle flags */ |
| 976 | { |
| 977 | if (*p == COM_BLANK) |
| 978 | require_blank = TRUE; |
| 979 | ++p; |
| 980 | } |
| 981 | (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); |
| 982 | |
| 983 | while (*p && p[-1] != ':') /* find end of end flags */ |
| 984 | { |
| 985 | /* Check whether we allow automatic ending of comments */ |
| 986 | if (*p == COM_AUTO_END) |
| 987 | end_comment_pending = -1; /* means we want to set it */ |
| 988 | ++p; |
| 989 | } |
| 990 | n = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 991 | |
| 992 | if (end_comment_pending == -1) /* we can set it now */ |
| 993 | end_comment_pending = lead_end[n - 1]; |
| 994 | |
| 995 | /* |
| 996 | * If the end of the comment is in the same line, don't use |
| 997 | * the comment leader. |
| 998 | */ |
| 999 | if (dir == FORWARD) |
| 1000 | { |
| 1001 | for (p = saved_line + lead_len; *p; ++p) |
| 1002 | if (STRNCMP(p, lead_end, n) == 0) |
| 1003 | { |
| 1004 | comment_end = p; |
| 1005 | lead_len = 0; |
| 1006 | break; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * Doing "o" on a start of comment inserts the middle leader. |
| 1012 | */ |
| 1013 | if (lead_len > 0) |
| 1014 | { |
| 1015 | if (current_flag == COM_START) |
| 1016 | { |
| 1017 | lead_repl = lead_middle; |
| 1018 | lead_repl_len = (int)STRLEN(lead_middle); |
| 1019 | } |
| 1020 | |
| 1021 | /* |
| 1022 | * If we have hit RETURN immediately after the start |
| 1023 | * comment leader, then put a space after the middle |
| 1024 | * comment leader on the next line. |
| 1025 | */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1026 | if (!VIM_ISWHITE(saved_line[lead_len - 1]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1027 | && ((p_extra != NULL |
| 1028 | && (int)curwin->w_cursor.col == lead_len) |
| 1029 | || (p_extra == NULL |
| 1030 | && saved_line[lead_len] == NUL) |
| 1031 | || require_blank)) |
| 1032 | extra_space = TRUE; |
| 1033 | } |
| 1034 | break; |
| 1035 | } |
| 1036 | if (*p == COM_END) |
| 1037 | { |
| 1038 | /* |
| 1039 | * Doing "o" on the end of a comment does not insert leader. |
| 1040 | * Remember where the end is, might want to use it to find the |
| 1041 | * start (for C-comments). |
| 1042 | */ |
| 1043 | if (dir == FORWARD) |
| 1044 | { |
| 1045 | comment_end = skipwhite(saved_line); |
| 1046 | lead_len = 0; |
| 1047 | break; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * Doing "O" on the end of a comment inserts the middle leader. |
| 1052 | * Find the string for the middle leader, searching backwards. |
| 1053 | */ |
| 1054 | while (p > curbuf->b_p_com && *p != ',') |
| 1055 | --p; |
| 1056 | for (lead_repl = p; lead_repl > curbuf->b_p_com |
| 1057 | && lead_repl[-1] != ':'; --lead_repl) |
| 1058 | ; |
| 1059 | lead_repl_len = (int)(p - lead_repl); |
| 1060 | |
| 1061 | /* We can probably always add an extra space when doing "O" on |
| 1062 | * the comment-end */ |
| 1063 | extra_space = TRUE; |
| 1064 | |
| 1065 | /* Check whether we allow automatic ending of comments */ |
| 1066 | for (p2 = p; *p2 && *p2 != ':'; p2++) |
| 1067 | { |
| 1068 | if (*p2 == COM_AUTO_END) |
| 1069 | end_comment_pending = -1; /* means we want to set it */ |
| 1070 | } |
| 1071 | if (end_comment_pending == -1) |
| 1072 | { |
| 1073 | /* Find last character in end-comment string */ |
| 1074 | while (*p2 && *p2 != ',') |
| 1075 | p2++; |
| 1076 | end_comment_pending = p2[-1]; |
| 1077 | } |
| 1078 | break; |
| 1079 | } |
| 1080 | if (*p == COM_FIRST) |
| 1081 | { |
| 1082 | /* |
| 1083 | * Comment leader for first line only: Don't repeat leader |
| 1084 | * when using "O", blank out leader when using "o". |
| 1085 | */ |
| 1086 | if (dir == BACKWARD) |
| 1087 | lead_len = 0; |
| 1088 | else |
| 1089 | { |
| 1090 | lead_repl = (char_u *)""; |
| 1091 | lead_repl_len = 0; |
| 1092 | } |
| 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | if (lead_len) |
| 1097 | { |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 1098 | /* allocate buffer (may concatenate p_extra later) */ |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1099 | leader = alloc(lead_len + lead_repl_len + extra_space + extra_len |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 1100 | + (second_line_indent > 0 ? second_line_indent : 0) + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1101 | allocated = leader; /* remember to free it later */ |
| 1102 | |
| 1103 | if (leader == NULL) |
| 1104 | lead_len = 0; |
| 1105 | else |
| 1106 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 1107 | vim_strncpy(leader, saved_line, lead_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1108 | |
| 1109 | /* |
| 1110 | * Replace leader with lead_repl, right or left adjusted |
| 1111 | */ |
| 1112 | if (lead_repl != NULL) |
| 1113 | { |
| 1114 | int c = 0; |
| 1115 | int off = 0; |
| 1116 | |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1117 | for (p = lead_flags; *p != NUL && *p != ':'; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1118 | { |
| 1119 | if (*p == COM_RIGHT || *p == COM_LEFT) |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1120 | c = *p++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1121 | else if (VIM_ISDIGIT(*p) || *p == '-') |
| 1122 | off = getdigits(&p); |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1123 | else |
| 1124 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1125 | } |
| 1126 | if (c == COM_RIGHT) /* right adjusted leader */ |
| 1127 | { |
| 1128 | /* find last non-white in the leader to line up with */ |
| 1129 | for (p = leader + lead_len - 1; p > leader |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1130 | && VIM_ISWHITE(*p); --p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1131 | ; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1132 | ++p; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1133 | |
| 1134 | #ifdef FEAT_MBYTE |
| 1135 | /* Compute the length of the replaced characters in |
| 1136 | * screen characters, not bytes. */ |
| 1137 | { |
| 1138 | int repl_size = vim_strnsize(lead_repl, |
| 1139 | lead_repl_len); |
| 1140 | int old_size = 0; |
| 1141 | char_u *endp = p; |
| 1142 | int l; |
| 1143 | |
| 1144 | while (old_size < repl_size && p > leader) |
| 1145 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 1146 | MB_PTR_BACK(leader, p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1147 | old_size += ptr2cells(p); |
| 1148 | } |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1149 | l = lead_repl_len - (int)(endp - p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1150 | if (l != 0) |
| 1151 | mch_memmove(endp + l, endp, |
| 1152 | (size_t)((leader + lead_len) - endp)); |
| 1153 | lead_len += l; |
| 1154 | } |
| 1155 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1156 | if (p < leader + lead_repl_len) |
| 1157 | p = leader; |
| 1158 | else |
| 1159 | p -= lead_repl_len; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1160 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
| 1162 | if (p + lead_repl_len > leader + lead_len) |
| 1163 | p[lead_repl_len] = NUL; |
| 1164 | |
| 1165 | /* blank-out any other chars from the old leader. */ |
| 1166 | while (--p >= leader) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1167 | { |
| 1168 | #ifdef FEAT_MBYTE |
| 1169 | int l = mb_head_off(leader, p); |
| 1170 | |
| 1171 | if (l > 1) |
| 1172 | { |
| 1173 | p -= l; |
| 1174 | if (ptr2cells(p) > 1) |
| 1175 | { |
| 1176 | p[1] = ' '; |
| 1177 | --l; |
| 1178 | } |
| 1179 | mch_memmove(p + 1, p + l + 1, |
| 1180 | (size_t)((leader + lead_len) - (p + l + 1))); |
| 1181 | lead_len -= l; |
| 1182 | *p = ' '; |
| 1183 | } |
| 1184 | else |
| 1185 | #endif |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1186 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1187 | *p = ' '; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1188 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1189 | } |
| 1190 | else /* left adjusted leader */ |
| 1191 | { |
| 1192 | p = skipwhite(leader); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1193 | #ifdef FEAT_MBYTE |
| 1194 | /* Compute the length of the replaced characters in |
| 1195 | * screen characters, not bytes. Move the part that is |
| 1196 | * not to be overwritten. */ |
| 1197 | { |
| 1198 | int repl_size = vim_strnsize(lead_repl, |
| 1199 | lead_repl_len); |
| 1200 | int i; |
| 1201 | int l; |
| 1202 | |
Bram Moolenaar | dc633cf | 2016-04-23 14:33:19 +0200 | [diff] [blame] | 1203 | for (i = 0; i < lead_len && p[i] != NUL; i += l) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1204 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1205 | l = (*mb_ptr2len)(p + i); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1206 | if (vim_strnsize(p, i + l) > repl_size) |
| 1207 | break; |
| 1208 | } |
| 1209 | if (i != lead_repl_len) |
| 1210 | { |
| 1211 | mch_memmove(p + lead_repl_len, p + i, |
Bram Moolenaar | 2d7ff05 | 2009-11-17 15:08:26 +0000 | [diff] [blame] | 1212 | (size_t)(lead_len - i - (p - leader))); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1213 | lead_len += lead_repl_len - i; |
| 1214 | } |
| 1215 | } |
| 1216 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1217 | mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
| 1218 | |
| 1219 | /* Replace any remaining non-white chars in the old |
| 1220 | * leader by spaces. Keep Tabs, the indent must |
| 1221 | * remain the same. */ |
| 1222 | for (p += lead_repl_len; p < leader + lead_len; ++p) |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1223 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1224 | { |
| 1225 | /* Don't put a space before a TAB. */ |
| 1226 | if (p + 1 < leader + lead_len && p[1] == TAB) |
| 1227 | { |
| 1228 | --lead_len; |
| 1229 | mch_memmove(p, p + 1, |
| 1230 | (leader + lead_len) - p); |
| 1231 | } |
| 1232 | else |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1233 | { |
| 1234 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1235 | int l = (*mb_ptr2len)(p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1236 | |
| 1237 | if (l > 1) |
| 1238 | { |
| 1239 | if (ptr2cells(p) > 1) |
| 1240 | { |
| 1241 | /* Replace a double-wide char with |
| 1242 | * two spaces */ |
| 1243 | --l; |
| 1244 | *p++ = ' '; |
| 1245 | } |
| 1246 | mch_memmove(p + 1, p + l, |
| 1247 | (leader + lead_len) - p); |
| 1248 | lead_len -= l - 1; |
| 1249 | } |
| 1250 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1251 | *p = ' '; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1252 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1253 | } |
| 1254 | *p = NUL; |
| 1255 | } |
| 1256 | |
| 1257 | /* Recompute the indent, it may have changed. */ |
| 1258 | if (curbuf->b_p_ai |
| 1259 | #ifdef FEAT_SMARTINDENT |
| 1260 | || do_si |
| 1261 | #endif |
| 1262 | ) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 1263 | newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1264 | |
| 1265 | /* Add the indent offset */ |
| 1266 | if (newindent + off < 0) |
| 1267 | { |
| 1268 | off = -newindent; |
| 1269 | newindent = 0; |
| 1270 | } |
| 1271 | else |
| 1272 | newindent += off; |
| 1273 | |
| 1274 | /* Correct trailing spaces for the shift, so that |
| 1275 | * alignment remains equal. */ |
| 1276 | while (off > 0 && lead_len > 0 |
| 1277 | && leader[lead_len - 1] == ' ') |
| 1278 | { |
| 1279 | /* Don't do it when there is a tab before the space */ |
| 1280 | if (vim_strchr(skipwhite(leader), '\t') != NULL) |
| 1281 | break; |
| 1282 | --lead_len; |
| 1283 | --off; |
| 1284 | } |
| 1285 | |
| 1286 | /* If the leader ends in white space, don't add an |
| 1287 | * extra space */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1288 | if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1289 | extra_space = FALSE; |
| 1290 | leader[lead_len] = NUL; |
| 1291 | } |
| 1292 | |
| 1293 | if (extra_space) |
| 1294 | { |
| 1295 | leader[lead_len++] = ' '; |
| 1296 | leader[lead_len] = NUL; |
| 1297 | } |
| 1298 | |
| 1299 | newcol = lead_len; |
| 1300 | |
| 1301 | /* |
| 1302 | * if a new indent will be set below, remove the indent that |
| 1303 | * is in the comment leader |
| 1304 | */ |
| 1305 | if (newindent |
| 1306 | #ifdef FEAT_SMARTINDENT |
| 1307 | || did_si |
| 1308 | #endif |
| 1309 | ) |
| 1310 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1311 | while (lead_len && VIM_ISWHITE(*leader)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1312 | { |
| 1313 | --lead_len; |
| 1314 | --newcol; |
| 1315 | ++leader; |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | } |
| 1320 | #ifdef FEAT_SMARTINDENT |
| 1321 | did_si = can_si = FALSE; |
| 1322 | #endif |
| 1323 | } |
| 1324 | else if (comment_end != NULL) |
| 1325 | { |
| 1326 | /* |
| 1327 | * We have finished a comment, so we don't use the leader. |
| 1328 | * If this was a C-comment and 'ai' or 'si' is set do a normal |
| 1329 | * indent to align with the line containing the start of the |
| 1330 | * comment. |
| 1331 | */ |
| 1332 | if (comment_end[0] == '*' && comment_end[1] == '/' && |
| 1333 | (curbuf->b_p_ai |
| 1334 | #ifdef FEAT_SMARTINDENT |
| 1335 | || do_si |
| 1336 | #endif |
| 1337 | )) |
| 1338 | { |
| 1339 | old_cursor = curwin->w_cursor; |
| 1340 | curwin->w_cursor.col = (colnr_T)(comment_end - saved_line); |
| 1341 | if ((pos = findmatch(NULL, NUL)) != NULL) |
| 1342 | { |
| 1343 | curwin->w_cursor.lnum = pos->lnum; |
| 1344 | newindent = get_indent(); |
| 1345 | } |
| 1346 | curwin->w_cursor = old_cursor; |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | #endif |
| 1351 | |
| 1352 | /* (State == INSERT || State == REPLACE), only when dir == FORWARD */ |
| 1353 | if (p_extra != NULL) |
| 1354 | { |
| 1355 | *p_extra = saved_char; /* restore char that NUL replaced */ |
| 1356 | |
| 1357 | /* |
| 1358 | * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first |
| 1359 | * non-blank. |
| 1360 | * |
| 1361 | * When in REPLACE mode, put the deleted blanks on the replace stack, |
| 1362 | * preceded by a NUL, so they can be put back when a BS is entered. |
| 1363 | */ |
| 1364 | if (REPLACE_NORMAL(State)) |
| 1365 | replace_push(NUL); /* end of extra blanks */ |
| 1366 | if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES)) |
| 1367 | { |
| 1368 | while ((*p_extra == ' ' || *p_extra == '\t') |
| 1369 | #ifdef FEAT_MBYTE |
| 1370 | && (!enc_utf8 |
| 1371 | || !utf_iscomposing(utf_ptr2char(p_extra + 1))) |
| 1372 | #endif |
| 1373 | ) |
| 1374 | { |
| 1375 | if (REPLACE_NORMAL(State)) |
| 1376 | replace_push(*p_extra); |
| 1377 | ++p_extra; |
| 1378 | ++less_cols_off; |
| 1379 | } |
| 1380 | } |
| 1381 | if (*p_extra != NUL) |
| 1382 | did_ai = FALSE; /* append some text, don't truncate now */ |
| 1383 | |
| 1384 | /* columns for marks adjusted for removed columns */ |
| 1385 | less_cols = (int)(p_extra - saved_line); |
| 1386 | } |
| 1387 | |
| 1388 | if (p_extra == NULL) |
| 1389 | p_extra = (char_u *)""; /* append empty line */ |
| 1390 | |
| 1391 | #ifdef FEAT_COMMENTS |
| 1392 | /* concatenate leader and p_extra, if there is a leader */ |
| 1393 | if (lead_len) |
| 1394 | { |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1395 | if (flags & OPENLINE_COM_LIST && second_line_indent > 0) |
| 1396 | { |
| 1397 | int i; |
Bram Moolenaar | 3610578 | 2012-06-14 20:59:25 +0200 | [diff] [blame] | 1398 | int padding = second_line_indent |
| 1399 | - (newindent + (int)STRLEN(leader)); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1400 | |
| 1401 | /* Here whitespace is inserted after the comment char. |
| 1402 | * Below, set_indent(newindent, SIN_INSERT) will insert the |
| 1403 | * whitespace needed before the comment char. */ |
| 1404 | for (i = 0; i < padding; i++) |
| 1405 | { |
| 1406 | STRCAT(leader, " "); |
Bram Moolenaar | 5fb9ec5 | 2012-07-25 16:10:03 +0200 | [diff] [blame] | 1407 | less_cols--; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1408 | newcol++; |
| 1409 | } |
| 1410 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1411 | STRCAT(leader, p_extra); |
| 1412 | p_extra = leader; |
| 1413 | did_ai = TRUE; /* So truncating blanks works with comments */ |
| 1414 | less_cols -= lead_len; |
| 1415 | } |
| 1416 | else |
| 1417 | end_comment_pending = NUL; /* turns out there was no leader */ |
| 1418 | #endif |
| 1419 | |
| 1420 | old_cursor = curwin->w_cursor; |
| 1421 | if (dir == BACKWARD) |
| 1422 | --curwin->w_cursor.lnum; |
| 1423 | #ifdef FEAT_VREPLACE |
| 1424 | if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count) |
| 1425 | #endif |
| 1426 | { |
| 1427 | if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE) |
| 1428 | == FAIL) |
| 1429 | goto theend; |
| 1430 | /* Postpone calling changed_lines(), because it would mess up folding |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 1431 | * with markers. |
| 1432 | * 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] | 1433 | * be marks there. But still needed in diff mode. */ |
| 1434 | if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count |
| 1435 | #ifdef FEAT_DIFF |
| 1436 | || curwin->w_p_diff |
| 1437 | #endif |
| 1438 | ) |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 1439 | mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1440 | did_append = TRUE; |
| 1441 | } |
| 1442 | #ifdef FEAT_VREPLACE |
| 1443 | else |
| 1444 | { |
| 1445 | /* |
| 1446 | * In VREPLACE mode we are starting to replace the next line. |
| 1447 | */ |
| 1448 | curwin->w_cursor.lnum++; |
| 1449 | if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed) |
| 1450 | { |
| 1451 | /* In case we NL to a new line, BS to the previous one, and NL |
| 1452 | * again, we don't want to save the new line for undo twice. |
| 1453 | */ |
| 1454 | (void)u_save_cursor(); /* errors are ignored! */ |
| 1455 | vr_lines_changed++; |
| 1456 | } |
| 1457 | ml_replace(curwin->w_cursor.lnum, p_extra, TRUE); |
| 1458 | changed_bytes(curwin->w_cursor.lnum, 0); |
| 1459 | curwin->w_cursor.lnum--; |
| 1460 | did_append = FALSE; |
| 1461 | } |
| 1462 | #endif |
| 1463 | |
| 1464 | if (newindent |
| 1465 | #ifdef FEAT_SMARTINDENT |
| 1466 | || did_si |
| 1467 | #endif |
| 1468 | ) |
| 1469 | { |
| 1470 | ++curwin->w_cursor.lnum; |
| 1471 | #ifdef FEAT_SMARTINDENT |
| 1472 | if (did_si) |
| 1473 | { |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 1474 | int sw = (int)get_sw_value(curbuf); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 1475 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1476 | if (p_sr) |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 1477 | newindent -= newindent % sw; |
| 1478 | newindent += sw; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1479 | } |
| 1480 | #endif |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 1481 | /* Copy the indent */ |
| 1482 | if (curbuf->b_p_ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1483 | { |
| 1484 | (void)copy_indent(newindent, saved_line); |
| 1485 | |
| 1486 | /* |
| 1487 | * Set the 'preserveindent' option so that any further screwing |
| 1488 | * with the line doesn't entirely destroy our efforts to preserve |
| 1489 | * it. It gets restored at the function end. |
| 1490 | */ |
| 1491 | curbuf->b_p_pi = TRUE; |
| 1492 | } |
| 1493 | else |
| 1494 | (void)set_indent(newindent, SIN_INSERT); |
| 1495 | less_cols -= curwin->w_cursor.col; |
| 1496 | |
| 1497 | ai_col = curwin->w_cursor.col; |
| 1498 | |
| 1499 | /* |
| 1500 | * In REPLACE mode, for each character in the new indent, there must |
| 1501 | * be a NUL on the replace stack, for when it is deleted with BS |
| 1502 | */ |
| 1503 | if (REPLACE_NORMAL(State)) |
| 1504 | for (n = 0; n < (int)curwin->w_cursor.col; ++n) |
| 1505 | replace_push(NUL); |
| 1506 | newcol += curwin->w_cursor.col; |
| 1507 | #ifdef FEAT_SMARTINDENT |
| 1508 | if (no_si) |
| 1509 | did_si = FALSE; |
| 1510 | #endif |
| 1511 | } |
| 1512 | |
| 1513 | #ifdef FEAT_COMMENTS |
| 1514 | /* |
| 1515 | * In REPLACE mode, for each character in the extra leader, there must be |
| 1516 | * a NUL on the replace stack, for when it is deleted with BS. |
| 1517 | */ |
| 1518 | if (REPLACE_NORMAL(State)) |
| 1519 | while (lead_len-- > 0) |
| 1520 | replace_push(NUL); |
| 1521 | #endif |
| 1522 | |
| 1523 | curwin->w_cursor = old_cursor; |
| 1524 | |
| 1525 | if (dir == FORWARD) |
| 1526 | { |
| 1527 | if (trunc_line || (State & INSERT)) |
| 1528 | { |
| 1529 | /* truncate current line at cursor */ |
| 1530 | saved_line[curwin->w_cursor.col] = NUL; |
| 1531 | /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */ |
| 1532 | if (trunc_line && !(flags & OPENLINE_KEEPTRAIL)) |
| 1533 | truncate_spaces(saved_line); |
| 1534 | ml_replace(curwin->w_cursor.lnum, saved_line, FALSE); |
| 1535 | saved_line = NULL; |
| 1536 | if (did_append) |
| 1537 | { |
| 1538 | changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, |
| 1539 | curwin->w_cursor.lnum + 1, 1L); |
| 1540 | did_append = FALSE; |
| 1541 | |
| 1542 | /* Move marks after the line break to the new line. */ |
| 1543 | if (flags & OPENLINE_MARKFIX) |
| 1544 | mark_col_adjust(curwin->w_cursor.lnum, |
| 1545 | curwin->w_cursor.col + less_cols_off, |
| 1546 | 1L, (long)-less_cols); |
| 1547 | } |
| 1548 | else |
| 1549 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 1550 | } |
| 1551 | |
| 1552 | /* |
| 1553 | * Put the cursor on the new line. Careful: the scrollup() above may |
| 1554 | * have moved w_cursor, we must use old_cursor. |
| 1555 | */ |
| 1556 | curwin->w_cursor.lnum = old_cursor.lnum + 1; |
| 1557 | } |
| 1558 | if (did_append) |
| 1559 | changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L); |
| 1560 | |
| 1561 | curwin->w_cursor.col = newcol; |
| 1562 | #ifdef FEAT_VIRTUALEDIT |
| 1563 | curwin->w_cursor.coladd = 0; |
| 1564 | #endif |
| 1565 | |
| 1566 | #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) |
| 1567 | /* |
| 1568 | * In VREPLACE mode, we are handling the replace stack ourselves, so stop |
| 1569 | * fixthisline() from doing it (via change_indent()) by telling it we're in |
| 1570 | * normal INSERT mode. |
| 1571 | */ |
| 1572 | if (State & VREPLACE_FLAG) |
| 1573 | { |
| 1574 | vreplace_mode = State; /* So we know to put things right later */ |
| 1575 | State = INSERT; |
| 1576 | } |
| 1577 | else |
| 1578 | vreplace_mode = 0; |
| 1579 | #endif |
| 1580 | #ifdef FEAT_LISP |
| 1581 | /* |
| 1582 | * May do lisp indenting. |
| 1583 | */ |
| 1584 | if (!p_paste |
| 1585 | # ifdef FEAT_COMMENTS |
| 1586 | && leader == NULL |
| 1587 | # endif |
| 1588 | && curbuf->b_p_lisp |
| 1589 | && curbuf->b_p_ai) |
| 1590 | { |
| 1591 | fixthisline(get_lisp_indent); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1592 | ai_col = (colnr_T)getwhitecols_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1593 | } |
| 1594 | #endif |
| 1595 | #ifdef FEAT_CINDENT |
| 1596 | /* |
| 1597 | * May do indenting after opening a new line. |
| 1598 | */ |
| 1599 | if (!p_paste |
| 1600 | && (curbuf->b_p_cin |
| 1601 | # ifdef FEAT_EVAL |
| 1602 | || *curbuf->b_p_inde != NUL |
| 1603 | # endif |
| 1604 | ) |
| 1605 | && in_cinkeys(dir == FORWARD |
| 1606 | ? KEY_OPEN_FORW |
| 1607 | : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))) |
| 1608 | { |
| 1609 | do_c_expr_indent(); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1610 | ai_col = (colnr_T)getwhitecols_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1611 | } |
| 1612 | #endif |
| 1613 | #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) |
| 1614 | if (vreplace_mode != 0) |
| 1615 | State = vreplace_mode; |
| 1616 | #endif |
| 1617 | |
| 1618 | #ifdef FEAT_VREPLACE |
| 1619 | /* |
| 1620 | * Finally, VREPLACE gets the stuff on the new line, then puts back the |
| 1621 | * original line, and inserts the new stuff char by char, pushing old stuff |
| 1622 | * onto the replace stack (via ins_char()). |
| 1623 | */ |
| 1624 | if (State & VREPLACE_FLAG) |
| 1625 | { |
| 1626 | /* Put new line in p_extra */ |
| 1627 | p_extra = vim_strsave(ml_get_curline()); |
| 1628 | if (p_extra == NULL) |
| 1629 | goto theend; |
| 1630 | |
| 1631 | /* Put back original line */ |
| 1632 | ml_replace(curwin->w_cursor.lnum, next_line, FALSE); |
| 1633 | |
| 1634 | /* Insert new stuff into line again */ |
| 1635 | curwin->w_cursor.col = 0; |
| 1636 | #ifdef FEAT_VIRTUALEDIT |
| 1637 | curwin->w_cursor.coladd = 0; |
| 1638 | #endif |
| 1639 | ins_bytes(p_extra); /* will call changed_bytes() */ |
| 1640 | vim_free(p_extra); |
| 1641 | next_line = NULL; |
| 1642 | } |
| 1643 | #endif |
| 1644 | |
Bram Moolenaar | 24a2d72 | 2018-04-24 19:36:43 +0200 | [diff] [blame] | 1645 | retval = OK; /* success! */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1646 | theend: |
| 1647 | curbuf->b_p_pi = saved_pi; |
| 1648 | vim_free(saved_line); |
| 1649 | vim_free(next_line); |
| 1650 | vim_free(allocated); |
| 1651 | return retval; |
| 1652 | } |
| 1653 | |
| 1654 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 1655 | /* |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 1656 | * get_leader_len() returns the length in bytes of the prefix of the given |
| 1657 | * string which introduces a comment. If this string is not a comment then |
| 1658 | * 0 is returned. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1659 | * When "flags" is not NULL, it is set to point to the flags of the recognized |
| 1660 | * comment leader. |
| 1661 | * "backward" must be true for the "O" command. |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1662 | * If "include_space" is set, include trailing whitespace while calculating the |
| 1663 | * length. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1664 | */ |
| 1665 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1666 | get_leader_len( |
| 1667 | char_u *line, |
| 1668 | char_u **flags, |
| 1669 | int backward, |
| 1670 | int include_space) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1671 | { |
| 1672 | int i, j; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1673 | int result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1674 | int got_com = FALSE; |
| 1675 | int found_one; |
| 1676 | char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ |
| 1677 | char_u *string; /* pointer to comment string */ |
| 1678 | char_u *list; |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1679 | int middle_match_len = 0; |
| 1680 | char_u *prev_list; |
Bram Moolenaar | 05da428 | 2011-05-10 14:44:11 +0200 | [diff] [blame] | 1681 | char_u *saved_flags = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1682 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1683 | result = i = 0; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1684 | while (VIM_ISWHITE(line[i])) /* leading white space is ignored */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1685 | ++i; |
| 1686 | |
| 1687 | /* |
| 1688 | * Repeat to match several nested comment strings. |
| 1689 | */ |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1690 | while (line[i] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1691 | { |
| 1692 | /* |
| 1693 | * scan through the 'comments' option for a match |
| 1694 | */ |
| 1695 | found_one = FALSE; |
| 1696 | for (list = curbuf->b_p_com; *list; ) |
| 1697 | { |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1698 | /* Get one option part into part_buf[]. Advance "list" to next |
| 1699 | * one. Put "string" at start of string. */ |
| 1700 | if (!got_com && flags != NULL) |
| 1701 | *flags = list; /* remember where flags started */ |
| 1702 | prev_list = list; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1703 | (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
| 1704 | string = vim_strchr(part_buf, ':'); |
| 1705 | if (string == NULL) /* missing ':', ignore this part */ |
| 1706 | continue; |
| 1707 | *string++ = NUL; /* isolate flags from string */ |
| 1708 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1709 | /* If we found a middle match previously, use that match when this |
| 1710 | * is not a middle or end. */ |
| 1711 | if (middle_match_len != 0 |
| 1712 | && vim_strchr(part_buf, COM_MIDDLE) == NULL |
| 1713 | && vim_strchr(part_buf, COM_END) == NULL) |
| 1714 | break; |
| 1715 | |
| 1716 | /* When we already found a nested comment, only accept further |
| 1717 | * nested comments. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1718 | if (got_com && vim_strchr(part_buf, COM_NEST) == NULL) |
| 1719 | continue; |
| 1720 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1721 | /* When 'O' flag present and using "O" command skip this one. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1722 | if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL) |
| 1723 | continue; |
| 1724 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1725 | /* Line contents and string must match. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1726 | * When string starts with white space, must have some white space |
| 1727 | * (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] | 1728 | * TABs and spaces). */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1729 | if (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1730 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1731 | if (i == 0 || !VIM_ISWHITE(line[i - 1])) |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 1732 | continue; /* missing white space */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1733 | while (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1734 | ++string; |
| 1735 | } |
| 1736 | for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) |
| 1737 | ; |
| 1738 | if (string[j] != NUL) |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1739 | continue; /* string doesn't match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1740 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1741 | /* When 'b' flag used, there must be white space or an |
| 1742 | * end-of-line after the string in the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1743 | if (vim_strchr(part_buf, COM_BLANK) != NULL |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1744 | && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1745 | continue; |
| 1746 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1747 | /* We have found a match, stop searching unless this is a middle |
| 1748 | * comment. The middle comment can be a substring of the end |
| 1749 | * comment in which case it's better to return the length of the |
| 1750 | * end comment and its flags. Thus we keep searching with middle |
| 1751 | * and end matches and use an end match if it matches better. */ |
| 1752 | if (vim_strchr(part_buf, COM_MIDDLE) != NULL) |
| 1753 | { |
| 1754 | if (middle_match_len == 0) |
| 1755 | { |
| 1756 | middle_match_len = j; |
| 1757 | saved_flags = prev_list; |
| 1758 | } |
| 1759 | continue; |
| 1760 | } |
| 1761 | if (middle_match_len != 0 && j > middle_match_len) |
| 1762 | /* Use this match instead of the middle match, since it's a |
| 1763 | * longer thus better match. */ |
| 1764 | middle_match_len = 0; |
| 1765 | |
| 1766 | if (middle_match_len == 0) |
| 1767 | i += j; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1768 | found_one = TRUE; |
| 1769 | break; |
| 1770 | } |
| 1771 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1772 | if (middle_match_len != 0) |
| 1773 | { |
| 1774 | /* Use the previously found middle match after failing to find a |
| 1775 | * match with an end. */ |
| 1776 | if (!got_com && flags != NULL) |
| 1777 | *flags = saved_flags; |
| 1778 | i += middle_match_len; |
| 1779 | found_one = TRUE; |
| 1780 | } |
| 1781 | |
| 1782 | /* No match found, stop scanning. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1783 | if (!found_one) |
| 1784 | break; |
| 1785 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1786 | result = i; |
| 1787 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1788 | /* Include any trailing white space. */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1789 | while (VIM_ISWHITE(line[i])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1790 | ++i; |
| 1791 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1792 | if (include_space) |
| 1793 | result = i; |
| 1794 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1795 | /* If this comment doesn't nest, stop here. */ |
| 1796 | got_com = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1797 | if (vim_strchr(part_buf, COM_NEST) == NULL) |
| 1798 | break; |
| 1799 | } |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1800 | return result; |
| 1801 | } |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1802 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1803 | /* |
| 1804 | * Return the offset at which the last comment in line starts. If there is no |
| 1805 | * comment in the whole line, -1 is returned. |
| 1806 | * |
| 1807 | * When "flags" is not null, it is set to point to the flags describing the |
| 1808 | * recognized comment leader. |
| 1809 | */ |
| 1810 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1811 | get_last_leader_offset(char_u *line, char_u **flags) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1812 | { |
| 1813 | int result = -1; |
| 1814 | int i, j; |
| 1815 | int lower_check_bound = 0; |
| 1816 | char_u *string; |
| 1817 | char_u *com_leader; |
| 1818 | char_u *com_flags; |
| 1819 | char_u *list; |
| 1820 | int found_one; |
| 1821 | char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ |
| 1822 | |
| 1823 | /* |
| 1824 | * Repeat to match several nested comment strings. |
| 1825 | */ |
| 1826 | i = (int)STRLEN(line); |
| 1827 | while (--i >= lower_check_bound) |
| 1828 | { |
| 1829 | /* |
| 1830 | * scan through the 'comments' option for a match |
| 1831 | */ |
| 1832 | found_one = FALSE; |
| 1833 | for (list = curbuf->b_p_com; *list; ) |
| 1834 | { |
| 1835 | char_u *flags_save = list; |
| 1836 | |
| 1837 | /* |
| 1838 | * Get one option part into part_buf[]. Advance list to next one. |
| 1839 | * put string at start of string. |
| 1840 | */ |
| 1841 | (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
| 1842 | string = vim_strchr(part_buf, ':'); |
| 1843 | if (string == NULL) /* If everything is fine, this cannot actually |
| 1844 | * happen. */ |
| 1845 | { |
| 1846 | continue; |
| 1847 | } |
| 1848 | *string++ = NUL; /* Isolate flags from string. */ |
| 1849 | com_leader = string; |
| 1850 | |
| 1851 | /* |
| 1852 | * Line contents and string must match. |
| 1853 | * When string starts with white space, must have some white space |
| 1854 | * (but the amount does not need to match, there might be a mix of |
| 1855 | * TABs and spaces). |
| 1856 | */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1857 | if (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1858 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1859 | if (i == 0 || !VIM_ISWHITE(line[i - 1])) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1860 | continue; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1861 | while (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1862 | ++string; |
| 1863 | } |
| 1864 | for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) |
| 1865 | /* do nothing */; |
| 1866 | if (string[j] != NUL) |
| 1867 | continue; |
| 1868 | |
| 1869 | /* |
| 1870 | * When 'b' flag used, there must be white space or an |
| 1871 | * end-of-line after the string in the line. |
| 1872 | */ |
| 1873 | if (vim_strchr(part_buf, COM_BLANK) != NULL |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1874 | && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1875 | { |
| 1876 | continue; |
| 1877 | } |
| 1878 | |
| 1879 | /* |
| 1880 | * We have found a match, stop searching. |
| 1881 | */ |
| 1882 | found_one = TRUE; |
| 1883 | |
| 1884 | if (flags) |
| 1885 | *flags = flags_save; |
| 1886 | com_flags = flags_save; |
| 1887 | |
| 1888 | break; |
| 1889 | } |
| 1890 | |
| 1891 | if (found_one) |
| 1892 | { |
| 1893 | char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */ |
| 1894 | int len1, len2, off; |
| 1895 | |
| 1896 | result = i; |
| 1897 | /* |
| 1898 | * If this comment nests, continue searching. |
| 1899 | */ |
| 1900 | if (vim_strchr(part_buf, COM_NEST) != NULL) |
| 1901 | continue; |
| 1902 | |
| 1903 | lower_check_bound = i; |
| 1904 | |
| 1905 | /* Let's verify whether the comment leader found is a substring |
| 1906 | * of other comment leaders. If it is, let's adjust the |
| 1907 | * lower_check_bound so that we make sure that we have determined |
| 1908 | * the comment leader correctly. |
| 1909 | */ |
| 1910 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1911 | while (VIM_ISWHITE(*com_leader)) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1912 | ++com_leader; |
| 1913 | len1 = (int)STRLEN(com_leader); |
| 1914 | |
| 1915 | for (list = curbuf->b_p_com; *list; ) |
| 1916 | { |
| 1917 | char_u *flags_save = list; |
| 1918 | |
| 1919 | (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ","); |
| 1920 | if (flags_save == com_flags) |
| 1921 | continue; |
| 1922 | string = vim_strchr(part_buf2, ':'); |
| 1923 | ++string; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1924 | while (VIM_ISWHITE(*string)) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1925 | ++string; |
| 1926 | len2 = (int)STRLEN(string); |
| 1927 | if (len2 == 0) |
| 1928 | continue; |
| 1929 | |
| 1930 | /* Now we have to verify whether string ends with a substring |
| 1931 | * beginning the com_leader. */ |
| 1932 | for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;) |
| 1933 | { |
| 1934 | --off; |
| 1935 | if (!STRNCMP(string + off, com_leader, len2 - off)) |
| 1936 | { |
| 1937 | if (i - off < lower_check_bound) |
| 1938 | lower_check_bound = i - off; |
| 1939 | } |
| 1940 | } |
| 1941 | } |
| 1942 | } |
| 1943 | } |
| 1944 | return result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1945 | } |
| 1946 | #endif |
| 1947 | |
| 1948 | /* |
| 1949 | * Return the number of window lines occupied by buffer line "lnum". |
| 1950 | */ |
| 1951 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1952 | plines(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1953 | { |
| 1954 | return plines_win(curwin, lnum, TRUE); |
| 1955 | } |
| 1956 | |
| 1957 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1958 | plines_win( |
| 1959 | win_T *wp, |
| 1960 | linenr_T lnum, |
| 1961 | int winheight) /* when TRUE limit to window height */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1962 | { |
| 1963 | #if defined(FEAT_DIFF) || defined(PROTO) |
| 1964 | /* Check for filler lines above this buffer line. When folded the result |
| 1965 | * is one line anyway. */ |
| 1966 | return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum); |
| 1967 | } |
| 1968 | |
| 1969 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1970 | plines_nofill(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1971 | { |
| 1972 | return plines_win_nofill(curwin, lnum, TRUE); |
| 1973 | } |
| 1974 | |
| 1975 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1976 | plines_win_nofill( |
| 1977 | win_T *wp, |
| 1978 | linenr_T lnum, |
| 1979 | int winheight) /* when TRUE limit to window height */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1980 | { |
| 1981 | #endif |
| 1982 | int lines; |
| 1983 | |
| 1984 | if (!wp->w_p_wrap) |
| 1985 | return 1; |
| 1986 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1987 | if (wp->w_width == 0) |
| 1988 | return 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1989 | |
| 1990 | #ifdef FEAT_FOLDING |
| 1991 | /* A folded lines is handled just like an empty line. */ |
| 1992 | /* NOTE: Caller must handle lines that are MAYBE folded. */ |
| 1993 | if (lineFolded(wp, lnum) == TRUE) |
| 1994 | return 1; |
| 1995 | #endif |
| 1996 | |
| 1997 | lines = plines_win_nofold(wp, lnum); |
| 1998 | if (winheight > 0 && lines > wp->w_height) |
| 1999 | return (int)wp->w_height; |
| 2000 | return lines; |
| 2001 | } |
| 2002 | |
| 2003 | /* |
| 2004 | * Return number of window lines physical line "lnum" will occupy in window |
| 2005 | * "wp". Does not care about folding, 'wrap' or 'diff'. |
| 2006 | */ |
| 2007 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2008 | plines_win_nofold(win_T *wp, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2009 | { |
| 2010 | char_u *s; |
| 2011 | long col; |
| 2012 | int width; |
| 2013 | |
| 2014 | s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
| 2015 | if (*s == NUL) /* empty line */ |
| 2016 | return 1; |
| 2017 | col = win_linetabsize(wp, s, (colnr_T)MAXCOL); |
| 2018 | |
| 2019 | /* |
| 2020 | * If list mode is on, then the '$' at the end of the line may take up one |
| 2021 | * extra column. |
| 2022 | */ |
| 2023 | if (wp->w_p_list && lcs_eol != NUL) |
| 2024 | col += 1; |
| 2025 | |
| 2026 | /* |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 2027 | * Add column offset for 'number', 'relativenumber' and 'foldcolumn'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2028 | */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2029 | width = wp->w_width - win_col_off(wp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2030 | if (width <= 0) |
| 2031 | return 32000; |
| 2032 | if (col <= width) |
| 2033 | return 1; |
| 2034 | col -= width; |
| 2035 | width += win_col_off2(wp); |
| 2036 | return (col + (width - 1)) / width + 1; |
| 2037 | } |
| 2038 | |
| 2039 | /* |
| 2040 | * Like plines_win(), but only reports the number of physical screen lines |
| 2041 | * used from the start of the line to the given column number. |
| 2042 | */ |
| 2043 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2044 | plines_win_col(win_T *wp, linenr_T lnum, long column) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2045 | { |
| 2046 | long col; |
| 2047 | char_u *s; |
| 2048 | int lines = 0; |
| 2049 | int width; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2050 | char_u *line; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2051 | |
| 2052 | #ifdef FEAT_DIFF |
| 2053 | /* Check for filler lines above this buffer line. When folded the result |
| 2054 | * is one line anyway. */ |
| 2055 | lines = diff_check_fill(wp, lnum); |
| 2056 | #endif |
| 2057 | |
| 2058 | if (!wp->w_p_wrap) |
| 2059 | return lines + 1; |
| 2060 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2061 | if (wp->w_width == 0) |
| 2062 | return lines + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2063 | |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2064 | line = s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2065 | |
| 2066 | col = 0; |
| 2067 | while (*s != NUL && --column >= 0) |
| 2068 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2069 | col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL); |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 2070 | MB_PTR_ADV(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | /* |
| 2074 | * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in |
| 2075 | * INSERT mode, then col must be adjusted so that it represents the last |
| 2076 | * screen position of the TAB. This only fixes an error when the TAB wraps |
| 2077 | * from one screen line to the next (when 'columns' is not a multiple of |
| 2078 | * 'ts') -- webb. |
| 2079 | */ |
| 2080 | if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1)) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2081 | col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2082 | |
| 2083 | /* |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 2084 | * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2085 | */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2086 | width = wp->w_width - win_col_off(wp); |
Bram Moolenaar | 2647063 | 2006-10-24 19:12:40 +0000 | [diff] [blame] | 2087 | if (width <= 0) |
| 2088 | return 9999; |
| 2089 | |
| 2090 | lines += 1; |
| 2091 | if (col > width) |
| 2092 | lines += (col - width) / (width + win_col_off2(wp)) + 1; |
| 2093 | return lines; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2097 | plines_m_win(win_T *wp, linenr_T first, linenr_T last) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2098 | { |
| 2099 | int count = 0; |
| 2100 | |
| 2101 | while (first <= last) |
| 2102 | { |
| 2103 | #ifdef FEAT_FOLDING |
| 2104 | int x; |
| 2105 | |
| 2106 | /* Check if there are any really folded lines, but also included lines |
| 2107 | * that are maybe folded. */ |
| 2108 | x = foldedCount(wp, first, NULL); |
| 2109 | if (x > 0) |
| 2110 | { |
| 2111 | ++count; /* count 1 for "+-- folded" line */ |
| 2112 | first += x; |
| 2113 | } |
| 2114 | else |
| 2115 | #endif |
| 2116 | { |
| 2117 | #ifdef FEAT_DIFF |
| 2118 | if (first == wp->w_topline) |
| 2119 | count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill; |
| 2120 | else |
| 2121 | #endif |
| 2122 | count += plines_win(wp, first, TRUE); |
| 2123 | ++first; |
| 2124 | } |
| 2125 | } |
| 2126 | return (count); |
| 2127 | } |
| 2128 | |
| 2129 | #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO) |
| 2130 | /* |
| 2131 | * Insert string "p" at the cursor position. Stops at a NUL byte. |
| 2132 | * Handles Replace mode and multi-byte characters. |
| 2133 | */ |
| 2134 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2135 | ins_bytes(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2136 | { |
| 2137 | ins_bytes_len(p, (int)STRLEN(p)); |
| 2138 | } |
| 2139 | #endif |
| 2140 | |
| 2141 | #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ |
| 2142 | || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO) |
| 2143 | /* |
| 2144 | * Insert string "p" with length "len" at the cursor position. |
| 2145 | * Handles Replace mode and multi-byte characters. |
| 2146 | */ |
| 2147 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2148 | ins_bytes_len(char_u *p, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2149 | { |
| 2150 | int i; |
| 2151 | # ifdef FEAT_MBYTE |
| 2152 | int n; |
| 2153 | |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2154 | if (has_mbyte) |
| 2155 | for (i = 0; i < len; i += n) |
| 2156 | { |
| 2157 | if (enc_utf8) |
| 2158 | /* avoid reading past p[len] */ |
| 2159 | n = utfc_ptr2len_len(p + i, len - i); |
| 2160 | else |
| 2161 | n = (*mb_ptr2len)(p + i); |
| 2162 | ins_char_bytes(p + i, n); |
| 2163 | } |
| 2164 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2165 | # endif |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2166 | for (i = 0; i < len; ++i) |
| 2167 | ins_char(p[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2168 | } |
| 2169 | #endif |
| 2170 | |
| 2171 | /* |
| 2172 | * Insert or replace a single character at the cursor position. |
| 2173 | * When in REPLACE or VREPLACE mode, replace any existing character. |
| 2174 | * Caller must have prepared for undo. |
| 2175 | * For multi-byte characters we get the whole character, the caller must |
| 2176 | * convert bytes to a character. |
| 2177 | */ |
| 2178 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2179 | ins_char(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2180 | { |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 2181 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | 560379d | 2017-01-21 22:50:00 +0100 | [diff] [blame] | 2182 | int n = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2183 | |
Bram Moolenaar | 6a8ede9 | 2017-01-22 19:49:12 +0100 | [diff] [blame] | 2184 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2185 | n = (*mb_char2bytes)(c, buf); |
| 2186 | |
| 2187 | /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte. |
| 2188 | * Happens for CTRL-Vu9900. */ |
| 2189 | if (buf[0] == 0) |
| 2190 | buf[0] = '\n'; |
Bram Moolenaar | 560379d | 2017-01-21 22:50:00 +0100 | [diff] [blame] | 2191 | #else |
| 2192 | buf[0] = c; |
| 2193 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2194 | |
| 2195 | ins_char_bytes(buf, n); |
| 2196 | } |
| 2197 | |
| 2198 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2199 | ins_char_bytes(char_u *buf, int charlen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2200 | { |
| 2201 | int c = buf[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2202 | int newlen; /* nr of bytes inserted */ |
| 2203 | int oldlen; /* nr of bytes deleted (0 when not replacing) */ |
| 2204 | char_u *p; |
| 2205 | char_u *newp; |
| 2206 | char_u *oldp; |
| 2207 | int linelen; /* length of old line including NUL */ |
| 2208 | colnr_T col; |
| 2209 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2210 | int i; |
| 2211 | |
| 2212 | #ifdef FEAT_VIRTUALEDIT |
| 2213 | /* Break tabs if needed. */ |
| 2214 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 2215 | coladvance_force(getviscol()); |
| 2216 | #endif |
| 2217 | |
| 2218 | col = curwin->w_cursor.col; |
| 2219 | oldp = ml_get(lnum); |
| 2220 | linelen = (int)STRLEN(oldp) + 1; |
| 2221 | |
| 2222 | /* The lengths default to the values for when not replacing. */ |
| 2223 | oldlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2224 | newlen = charlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2225 | |
| 2226 | if (State & REPLACE_FLAG) |
| 2227 | { |
| 2228 | #ifdef FEAT_VREPLACE |
| 2229 | if (State & VREPLACE_FLAG) |
| 2230 | { |
| 2231 | colnr_T new_vcol = 0; /* init for GCC */ |
| 2232 | colnr_T vcol; |
| 2233 | int old_list; |
| 2234 | #ifndef FEAT_MBYTE |
| 2235 | char_u buf[2]; |
| 2236 | #endif |
| 2237 | |
| 2238 | /* |
| 2239 | * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag. |
| 2240 | * Returns the old value of list, so when finished, |
| 2241 | * curwin->w_p_list should be set back to this. |
| 2242 | */ |
| 2243 | old_list = curwin->w_p_list; |
| 2244 | if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 2245 | curwin->w_p_list = FALSE; |
| 2246 | |
| 2247 | /* |
| 2248 | * In virtual replace mode each character may replace one or more |
| 2249 | * characters (zero if it's a TAB). Count the number of bytes to |
| 2250 | * be deleted to make room for the new character, counting screen |
| 2251 | * cells. May result in adding spaces to fill a gap. |
| 2252 | */ |
| 2253 | getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL); |
| 2254 | #ifndef FEAT_MBYTE |
| 2255 | buf[0] = c; |
| 2256 | buf[1] = NUL; |
| 2257 | #endif |
| 2258 | new_vcol = vcol + chartabsize(buf, vcol); |
| 2259 | while (oldp[col + oldlen] != NUL && vcol < new_vcol) |
| 2260 | { |
| 2261 | vcol += chartabsize(oldp + col + oldlen, vcol); |
| 2262 | /* Don't need to remove a TAB that takes us to the right |
| 2263 | * position. */ |
| 2264 | if (vcol > new_vcol && oldp[col + oldlen] == TAB) |
| 2265 | break; |
| 2266 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2267 | oldlen += (*mb_ptr2len)(oldp + col + oldlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2268 | #else |
| 2269 | ++oldlen; |
| 2270 | #endif |
| 2271 | /* Deleted a bit too much, insert spaces. */ |
| 2272 | if (vcol > new_vcol) |
| 2273 | newlen += vcol - new_vcol; |
| 2274 | } |
| 2275 | curwin->w_p_list = old_list; |
| 2276 | } |
| 2277 | else |
| 2278 | #endif |
| 2279 | if (oldp[col] != NUL) |
| 2280 | { |
| 2281 | /* normal replace */ |
| 2282 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2283 | oldlen = (*mb_ptr2len)(oldp + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2284 | #else |
| 2285 | oldlen = 1; |
| 2286 | #endif |
| 2287 | } |
| 2288 | |
| 2289 | |
| 2290 | /* Push the replaced bytes onto the replace stack, so that they can be |
| 2291 | * put back when BS is used. The bytes of a multi-byte character are |
| 2292 | * done the other way around, so that the first byte is popped off |
| 2293 | * first (it tells the byte length of the character). */ |
| 2294 | replace_push(NUL); |
| 2295 | for (i = 0; i < oldlen; ++i) |
| 2296 | { |
| 2297 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 2298 | if (has_mbyte) |
| 2299 | i += replace_push_mb(oldp + col + i) - 1; |
| 2300 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2301 | #endif |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 2302 | replace_push(oldp[col + i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | newp = alloc_check((unsigned)(linelen + newlen - oldlen)); |
| 2307 | if (newp == NULL) |
| 2308 | return; |
| 2309 | |
| 2310 | /* Copy bytes before the cursor. */ |
| 2311 | if (col > 0) |
| 2312 | mch_memmove(newp, oldp, (size_t)col); |
| 2313 | |
| 2314 | /* Copy bytes after the changed character(s). */ |
| 2315 | p = newp + col; |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 2316 | if (linelen > col + oldlen) |
| 2317 | mch_memmove(p + newlen, oldp + col + oldlen, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2318 | (size_t)(linelen - col - oldlen)); |
| 2319 | |
| 2320 | /* Insert or overwrite the new character. */ |
| 2321 | #ifdef FEAT_MBYTE |
| 2322 | mch_memmove(p, buf, charlen); |
| 2323 | i = charlen; |
| 2324 | #else |
| 2325 | *p = c; |
| 2326 | i = 1; |
| 2327 | #endif |
| 2328 | |
| 2329 | /* Fill with spaces when necessary. */ |
| 2330 | while (i < newlen) |
| 2331 | p[i++] = ' '; |
| 2332 | |
| 2333 | /* Replace the line in the buffer. */ |
| 2334 | ml_replace(lnum, newp, FALSE); |
| 2335 | |
| 2336 | /* mark the buffer as changed and prepare for displaying */ |
| 2337 | changed_bytes(lnum, col); |
| 2338 | |
| 2339 | /* |
| 2340 | * If we're in Insert or Replace mode and 'showmatch' is set, then briefly |
| 2341 | * show the match for right parens and braces. |
| 2342 | */ |
| 2343 | if (p_sm && (State & INSERT) |
| 2344 | && msg_silent == 0 |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 2345 | #ifdef FEAT_INS_EXPAND |
| 2346 | && !ins_compl_active() |
| 2347 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2348 | ) |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2349 | { |
| 2350 | #ifdef FEAT_MBYTE |
| 2351 | if (has_mbyte) |
| 2352 | showmatch(mb_ptr2char(buf)); |
| 2353 | else |
| 2354 | #endif |
| 2355 | showmatch(c); |
| 2356 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2357 | |
| 2358 | #ifdef FEAT_RIGHTLEFT |
| 2359 | if (!p_ri || (State & REPLACE_FLAG)) |
| 2360 | #endif |
| 2361 | { |
| 2362 | /* Normal insert: move cursor right */ |
| 2363 | #ifdef FEAT_MBYTE |
| 2364 | curwin->w_cursor.col += charlen; |
| 2365 | #else |
| 2366 | ++curwin->w_cursor.col; |
| 2367 | #endif |
| 2368 | } |
| 2369 | /* |
| 2370 | * TODO: should try to update w_row here, to avoid recomputing it later. |
| 2371 | */ |
| 2372 | } |
| 2373 | |
| 2374 | /* |
| 2375 | * Insert a string at the cursor position. |
| 2376 | * Note: Does NOT handle Replace mode. |
| 2377 | * Caller must have prepared for undo. |
| 2378 | */ |
| 2379 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2380 | ins_str(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2381 | { |
| 2382 | char_u *oldp, *newp; |
| 2383 | int newlen = (int)STRLEN(s); |
| 2384 | int oldlen; |
| 2385 | colnr_T col; |
| 2386 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2387 | |
| 2388 | #ifdef FEAT_VIRTUALEDIT |
| 2389 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 2390 | coladvance_force(getviscol()); |
| 2391 | #endif |
| 2392 | |
| 2393 | col = curwin->w_cursor.col; |
| 2394 | oldp = ml_get(lnum); |
| 2395 | oldlen = (int)STRLEN(oldp); |
| 2396 | |
| 2397 | newp = alloc_check((unsigned)(oldlen + newlen + 1)); |
| 2398 | if (newp == NULL) |
| 2399 | return; |
| 2400 | if (col > 0) |
| 2401 | mch_memmove(newp, oldp, (size_t)col); |
| 2402 | mch_memmove(newp + col, s, (size_t)newlen); |
| 2403 | mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1)); |
| 2404 | ml_replace(lnum, newp, FALSE); |
| 2405 | changed_bytes(lnum, col); |
| 2406 | curwin->w_cursor.col += newlen; |
| 2407 | } |
| 2408 | |
| 2409 | /* |
| 2410 | * Delete one character under the cursor. |
| 2411 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 2412 | * Caller must have prepared for undo. |
| 2413 | * |
| 2414 | * return FAIL for failure, OK otherwise |
| 2415 | */ |
| 2416 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2417 | del_char(int fixpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2418 | { |
| 2419 | #ifdef FEAT_MBYTE |
| 2420 | if (has_mbyte) |
| 2421 | { |
| 2422 | /* Make sure the cursor is at the start of a character. */ |
| 2423 | mb_adjust_cursor(); |
| 2424 | if (*ml_get_cursor() == NUL) |
| 2425 | return FAIL; |
| 2426 | return del_chars(1L, fixpos); |
| 2427 | } |
| 2428 | #endif |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2429 | return del_bytes(1L, fixpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 2433 | /* |
| 2434 | * Like del_bytes(), but delete characters instead of bytes. |
| 2435 | */ |
| 2436 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2437 | del_chars(long count, int fixpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2438 | { |
| 2439 | long bytes = 0; |
| 2440 | long i; |
| 2441 | char_u *p; |
| 2442 | int l; |
| 2443 | |
| 2444 | p = ml_get_cursor(); |
| 2445 | for (i = 0; i < count && *p != NUL; ++i) |
| 2446 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2447 | l = (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2448 | bytes += l; |
| 2449 | p += l; |
| 2450 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2451 | return del_bytes(bytes, fixpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2452 | } |
| 2453 | #endif |
| 2454 | |
| 2455 | /* |
| 2456 | * Delete "count" bytes under the cursor. |
| 2457 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 2458 | * Caller must have prepared for undo. |
| 2459 | * |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2460 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2461 | */ |
| 2462 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2463 | del_bytes( |
| 2464 | long count, |
| 2465 | int fixpos_arg, |
| 2466 | int use_delcombine UNUSED) /* 'delcombine' option applies */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2467 | { |
| 2468 | char_u *oldp, *newp; |
| 2469 | colnr_T oldlen; |
| 2470 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2471 | colnr_T col = curwin->w_cursor.col; |
| 2472 | int was_alloced; |
| 2473 | long movelen; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2474 | int fixpos = fixpos_arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2475 | |
| 2476 | oldp = ml_get(lnum); |
| 2477 | oldlen = (int)STRLEN(oldp); |
| 2478 | |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2479 | /* Can't do anything when the cursor is on the NUL after the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2480 | if (col >= oldlen) |
| 2481 | return FAIL; |
| 2482 | |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2483 | /* If "count" is zero there is nothing to do. */ |
| 2484 | if (count == 0) |
| 2485 | return OK; |
| 2486 | |
| 2487 | /* If "count" is negative the caller must be doing something wrong. */ |
| 2488 | if (count < 1) |
| 2489 | { |
| 2490 | IEMSGN("E950: Invalid count for del_bytes(): %ld", count); |
| 2491 | return FAIL; |
| 2492 | } |
| 2493 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2494 | #ifdef FEAT_MBYTE |
| 2495 | /* If 'delcombine' is set and deleting (less than) one character, only |
| 2496 | * delete the last combining character. */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2497 | if (p_deco && use_delcombine && enc_utf8 |
| 2498 | && utfc_ptr2len(oldp + col) >= count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2499 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2500 | int cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2501 | int n; |
| 2502 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2503 | (void)utfc_ptr2char(oldp + col, cc); |
| 2504 | if (cc[0] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2505 | { |
| 2506 | /* Find the last composing char, there can be several. */ |
| 2507 | n = col; |
| 2508 | do |
| 2509 | { |
| 2510 | col = n; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2511 | count = utf_ptr2len(oldp + n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2512 | n += count; |
| 2513 | } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n)); |
| 2514 | fixpos = 0; |
| 2515 | } |
| 2516 | } |
| 2517 | #endif |
| 2518 | |
| 2519 | /* |
| 2520 | * When count is too big, reduce it. |
| 2521 | */ |
| 2522 | movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */ |
| 2523 | if (movelen <= 1) |
| 2524 | { |
| 2525 | /* |
| 2526 | * 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] | 2527 | * fixpos is TRUE, we don't want to end up positioned at the NUL, |
| 2528 | * unless "restart_edit" is set or 'virtualedit' contains "onemore". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2529 | */ |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2530 | if (col > 0 && fixpos && restart_edit == 0 |
| 2531 | #ifdef FEAT_VIRTUALEDIT |
| 2532 | && (ve_flags & VE_ONEMORE) == 0 |
| 2533 | #endif |
| 2534 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2535 | { |
| 2536 | --curwin->w_cursor.col; |
| 2537 | #ifdef FEAT_VIRTUALEDIT |
| 2538 | curwin->w_cursor.coladd = 0; |
| 2539 | #endif |
| 2540 | #ifdef FEAT_MBYTE |
| 2541 | if (has_mbyte) |
| 2542 | curwin->w_cursor.col -= |
| 2543 | (*mb_head_off)(oldp, oldp + curwin->w_cursor.col); |
| 2544 | #endif |
| 2545 | } |
| 2546 | count = oldlen - col; |
| 2547 | movelen = 1; |
| 2548 | } |
| 2549 | |
| 2550 | /* |
| 2551 | * If the old line has been allocated the deletion can be done in the |
| 2552 | * existing line. Otherwise a new line has to be allocated |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2553 | * Can't do this when using Netbeans, because we would need to invoke |
| 2554 | * netbeans_removed(), which deallocates the line. Let ml_replace() take |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 2555 | * care of notifying Netbeans. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2556 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2557 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2558 | if (netbeans_active()) |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2559 | was_alloced = FALSE; |
| 2560 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2561 | #endif |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2562 | was_alloced = ml_line_alloced(); /* check if oldp was allocated */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2563 | if (was_alloced) |
| 2564 | newp = oldp; /* use same allocated memory */ |
| 2565 | else |
| 2566 | { /* need to allocate a new line */ |
| 2567 | newp = alloc((unsigned)(oldlen + 1 - count)); |
| 2568 | if (newp == NULL) |
| 2569 | return FAIL; |
| 2570 | mch_memmove(newp, oldp, (size_t)col); |
| 2571 | } |
| 2572 | mch_memmove(newp + col, oldp + col + count, (size_t)movelen); |
| 2573 | if (!was_alloced) |
| 2574 | ml_replace(lnum, newp, FALSE); |
| 2575 | |
| 2576 | /* mark the buffer as changed and prepare for displaying */ |
| 2577 | changed_bytes(lnum, curwin->w_cursor.col); |
| 2578 | |
| 2579 | return OK; |
| 2580 | } |
| 2581 | |
| 2582 | /* |
| 2583 | * Delete from cursor to end of line. |
| 2584 | * Caller must have prepared for undo. |
| 2585 | * |
| 2586 | * return FAIL for failure, OK otherwise |
| 2587 | */ |
| 2588 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2589 | truncate_line( |
| 2590 | int fixpos) /* if TRUE fix the cursor position when done */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2591 | { |
| 2592 | char_u *newp; |
| 2593 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2594 | colnr_T col = curwin->w_cursor.col; |
| 2595 | |
| 2596 | if (col == 0) |
| 2597 | newp = vim_strsave((char_u *)""); |
| 2598 | else |
| 2599 | newp = vim_strnsave(ml_get(lnum), col); |
| 2600 | |
| 2601 | if (newp == NULL) |
| 2602 | return FAIL; |
| 2603 | |
| 2604 | ml_replace(lnum, newp, FALSE); |
| 2605 | |
| 2606 | /* mark the buffer as changed and prepare for displaying */ |
| 2607 | changed_bytes(lnum, curwin->w_cursor.col); |
| 2608 | |
| 2609 | /* |
| 2610 | * If "fixpos" is TRUE we don't want to end up positioned at the NUL. |
| 2611 | */ |
| 2612 | if (fixpos && curwin->w_cursor.col > 0) |
| 2613 | --curwin->w_cursor.col; |
| 2614 | |
| 2615 | return OK; |
| 2616 | } |
| 2617 | |
| 2618 | /* |
| 2619 | * Delete "nlines" lines at the cursor. |
| 2620 | * Saves the lines for undo first if "undo" is TRUE. |
| 2621 | */ |
| 2622 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2623 | del_lines( |
| 2624 | long nlines, /* number of lines to delete */ |
| 2625 | int undo) /* if TRUE, prepare for undo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2626 | { |
| 2627 | long n; |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2628 | linenr_T first = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2629 | |
| 2630 | if (nlines <= 0) |
| 2631 | return; |
| 2632 | |
| 2633 | /* save the deleted lines for undo */ |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2634 | if (undo && u_savedel(first, nlines) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2635 | return; |
| 2636 | |
| 2637 | for (n = 0; n < nlines; ) |
| 2638 | { |
| 2639 | if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ |
| 2640 | break; |
| 2641 | |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2642 | ml_delete(first, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2643 | ++n; |
| 2644 | |
| 2645 | /* If we delete the last line in the file, stop */ |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2646 | if (first > curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2647 | break; |
| 2648 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2649 | |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2650 | /* Correct the cursor position before calling deleted_lines_mark(), it may |
| 2651 | * trigger a callback to display the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2652 | curwin->w_cursor.col = 0; |
| 2653 | check_cursor_lnum(); |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2654 | |
| 2655 | /* adjust marks, mark the buffer as changed and prepare for displaying */ |
| 2656 | deleted_lines_mark(first, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2660 | gchar_pos(pos_T *pos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2661 | { |
Bram Moolenaar | 8ada6aa | 2017-12-19 21:23:21 +0100 | [diff] [blame] | 2662 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2663 | |
Bram Moolenaar | 8ada6aa | 2017-12-19 21:23:21 +0100 | [diff] [blame] | 2664 | /* When searching columns is sometimes put at the end of a line. */ |
| 2665 | if (pos->col == MAXCOL) |
| 2666 | return NUL; |
| 2667 | ptr = ml_get_pos(pos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2668 | #ifdef FEAT_MBYTE |
| 2669 | if (has_mbyte) |
| 2670 | return (*mb_ptr2char)(ptr); |
| 2671 | #endif |
| 2672 | return (int)*ptr; |
| 2673 | } |
| 2674 | |
| 2675 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2676 | gchar_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2677 | { |
| 2678 | #ifdef FEAT_MBYTE |
| 2679 | if (has_mbyte) |
| 2680 | return (*mb_ptr2char)(ml_get_cursor()); |
| 2681 | #endif |
| 2682 | return (int)*ml_get_cursor(); |
| 2683 | } |
| 2684 | |
| 2685 | /* |
| 2686 | * Write a character at the current cursor position. |
| 2687 | * It is directly written into the block. |
| 2688 | */ |
| 2689 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2690 | pchar_cursor(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2691 | { |
| 2692 | *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) |
| 2693 | + curwin->w_cursor.col) = c; |
| 2694 | } |
| 2695 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2696 | /* |
| 2697 | * When extra == 0: Return TRUE if the cursor is before or on the first |
| 2698 | * non-blank in the line. |
| 2699 | * When extra == 1: Return TRUE if the cursor is before the first non-blank in |
| 2700 | * the line. |
| 2701 | */ |
| 2702 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2703 | inindent(int extra) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2704 | { |
| 2705 | char_u *ptr; |
| 2706 | colnr_T col; |
| 2707 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2708 | for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2709 | ++ptr; |
| 2710 | if (col >= curwin->w_cursor.col + extra) |
| 2711 | return TRUE; |
| 2712 | else |
| 2713 | return FALSE; |
| 2714 | } |
| 2715 | |
| 2716 | /* |
| 2717 | * Skip to next part of an option argument: Skip space and comma. |
| 2718 | */ |
| 2719 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2720 | skip_to_option_part(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2721 | { |
| 2722 | if (*p == ',') |
| 2723 | ++p; |
| 2724 | while (*p == ' ') |
| 2725 | ++p; |
| 2726 | return p; |
| 2727 | } |
| 2728 | |
| 2729 | /* |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2730 | * Call this function when something in the current buffer is changed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2731 | * |
| 2732 | * Most often called through changed_bytes() and changed_lines(), which also |
| 2733 | * mark the area of the display to be redrawn. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2734 | * |
| 2735 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2736 | */ |
| 2737 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2738 | changed(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2739 | { |
| 2740 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 2741 | if (p_imst == IM_ON_THE_SPOT) |
| 2742 | { |
| 2743 | /* The text of the preediting area is inserted, but this doesn't |
| 2744 | * mean a change of the buffer yet. That is delayed until the |
| 2745 | * text is committed. (this means preedit becomes empty) */ |
| 2746 | if (im_is_preediting() && !xim_changed_while_preediting) |
| 2747 | return; |
| 2748 | xim_changed_while_preediting = FALSE; |
| 2749 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2750 | #endif |
| 2751 | |
| 2752 | if (!curbuf->b_changed) |
| 2753 | { |
| 2754 | int save_msg_scroll = msg_scroll; |
| 2755 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2756 | /* Give a warning about changing a read-only file. This may also |
| 2757 | * check-out the file, thus change "curbuf"! */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2758 | change_warning(0); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2759 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2760 | /* Create a swap file if that is wanted. |
| 2761 | * Don't do this for "nofile" and "nowrite" buffer types. */ |
| 2762 | if (curbuf->b_may_swap |
| 2763 | #ifdef FEAT_QUICKFIX |
| 2764 | && !bt_dontwrite(curbuf) |
| 2765 | #endif |
| 2766 | ) |
| 2767 | { |
Bram Moolenaar | b01f357 | 2016-01-15 15:17:04 +0100 | [diff] [blame] | 2768 | int save_need_wait_return = need_wait_return; |
| 2769 | |
| 2770 | need_wait_return = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2771 | ml_open_file(curbuf); |
| 2772 | |
| 2773 | /* The ml_open_file() can cause an ATTENTION message. |
| 2774 | * Wait two seconds, to make sure the user reads this unexpected |
| 2775 | * message. Since we could be anywhere, call wait_return() now, |
| 2776 | * and don't let the emsg() set msg_scroll. */ |
| 2777 | if (need_wait_return && emsg_silent == 0) |
| 2778 | { |
| 2779 | out_flush(); |
| 2780 | ui_delay(2000L, TRUE); |
| 2781 | wait_return(TRUE); |
| 2782 | msg_scroll = save_msg_scroll; |
| 2783 | } |
Bram Moolenaar | b01f357 | 2016-01-15 15:17:04 +0100 | [diff] [blame] | 2784 | else |
| 2785 | need_wait_return = save_need_wait_return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2786 | } |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2787 | changed_int(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2788 | } |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 2789 | ++CHANGEDTICK(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2792 | /* |
| 2793 | * Internal part of changed(), no user interaction. |
| 2794 | */ |
| 2795 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2796 | changed_int(void) |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2797 | { |
| 2798 | curbuf->b_changed = TRUE; |
| 2799 | ml_setflags(curbuf); |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2800 | check_status(curbuf); |
| 2801 | redraw_tabline = TRUE; |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2802 | #ifdef FEAT_TITLE |
| 2803 | need_maketitle = TRUE; /* set window title later */ |
| 2804 | #endif |
| 2805 | } |
| 2806 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 2807 | static void changedOneline(buf_T *buf, linenr_T lnum); |
| 2808 | static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra); |
| 2809 | 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] | 2810 | |
| 2811 | /* |
| 2812 | * Changed bytes within a single line for the current buffer. |
| 2813 | * - marks the windows on this buffer to be redisplayed |
| 2814 | * - marks the buffer changed by calling changed() |
| 2815 | * - invalidates cached values |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2816 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2817 | */ |
| 2818 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2819 | changed_bytes(linenr_T lnum, colnr_T col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2820 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2821 | changedOneline(curbuf, lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2822 | changed_common(lnum, col, lnum + 1, 0L); |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2823 | |
| 2824 | #ifdef FEAT_DIFF |
| 2825 | /* Diff highlighting in other diff windows may need to be updated too. */ |
| 2826 | if (curwin->w_p_diff) |
| 2827 | { |
| 2828 | win_T *wp; |
| 2829 | linenr_T wlnum; |
| 2830 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 2831 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2832 | if (wp->w_p_diff && wp != curwin) |
| 2833 | { |
| 2834 | redraw_win_later(wp, VALID); |
| 2835 | wlnum = diff_lnum_win(lnum, wp); |
| 2836 | if (wlnum > 0) |
| 2837 | changedOneline(wp->w_buffer, wlnum); |
| 2838 | } |
| 2839 | } |
| 2840 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2844 | changedOneline(buf_T *buf, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2845 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2846 | if (buf->b_mod_set) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2847 | { |
| 2848 | /* find the maximum area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2849 | if (lnum < buf->b_mod_top) |
| 2850 | buf->b_mod_top = lnum; |
| 2851 | else if (lnum >= buf->b_mod_bot) |
| 2852 | buf->b_mod_bot = lnum + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2853 | } |
| 2854 | else |
| 2855 | { |
| 2856 | /* set the area that must be redisplayed to one line */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2857 | buf->b_mod_set = TRUE; |
| 2858 | buf->b_mod_top = lnum; |
| 2859 | buf->b_mod_bot = lnum + 1; |
| 2860 | buf->b_mod_xlines = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2861 | } |
| 2862 | } |
| 2863 | |
| 2864 | /* |
| 2865 | * Appended "count" lines below line "lnum" in the current buffer. |
| 2866 | * Must be called AFTER the change and after mark_adjust(). |
| 2867 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 2868 | */ |
| 2869 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2870 | appended_lines(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2871 | { |
| 2872 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 2873 | } |
| 2874 | |
| 2875 | /* |
| 2876 | * Like appended_lines(), but adjust marks first. |
| 2877 | */ |
| 2878 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2879 | appended_lines_mark(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2880 | { |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 2881 | /* 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] | 2882 | * be marks there. But it's still needed in diff mode. */ |
| 2883 | if (lnum + count < curbuf->b_ml.ml_line_count |
| 2884 | #ifdef FEAT_DIFF |
| 2885 | || curwin->w_p_diff |
| 2886 | #endif |
| 2887 | ) |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 2888 | mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2889 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 2890 | } |
| 2891 | |
| 2892 | /* |
| 2893 | * Deleted "count" lines at line "lnum" in the current buffer. |
| 2894 | * Must be called AFTER the change and after mark_adjust(). |
| 2895 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 2896 | */ |
| 2897 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2898 | deleted_lines(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2899 | { |
| 2900 | changed_lines(lnum, 0, lnum + count, -count); |
| 2901 | } |
| 2902 | |
| 2903 | /* |
| 2904 | * Like deleted_lines(), but adjust marks first. |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2905 | * Make sure the cursor is on a valid line before calling, a GUI callback may |
| 2906 | * be triggered to display the cursor. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2907 | */ |
| 2908 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2909 | deleted_lines_mark(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2910 | { |
| 2911 | mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count); |
| 2912 | changed_lines(lnum, 0, lnum + count, -count); |
| 2913 | } |
| 2914 | |
| 2915 | /* |
| 2916 | * Changed lines for the current buffer. |
| 2917 | * Must be called AFTER the change and after mark_adjust(). |
| 2918 | * - mark the buffer changed by calling changed() |
| 2919 | * - mark the windows on this buffer to be redisplayed |
| 2920 | * - invalidate cached values |
| 2921 | * "lnum" is the first line that needs displaying, "lnume" the first line |
| 2922 | * below the changed lines (BEFORE the change). |
| 2923 | * When only inserting lines, "lnum" and "lnume" are equal. |
| 2924 | * Takes care of calling changed() and updating b_mod_*. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2925 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2926 | */ |
| 2927 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2928 | changed_lines( |
| 2929 | linenr_T lnum, /* first line with change */ |
| 2930 | colnr_T col, /* column in first line with change */ |
| 2931 | linenr_T lnume, /* line below last changed line */ |
| 2932 | long xtra) /* number of extra lines (negative when deleting) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2933 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2934 | changed_lines_buf(curbuf, lnum, lnume, xtra); |
| 2935 | |
| 2936 | #ifdef FEAT_DIFF |
| 2937 | if (xtra == 0 && curwin->w_p_diff) |
| 2938 | { |
| 2939 | /* When the number of lines doesn't change then mark_adjust() isn't |
| 2940 | * called and other diff buffers still need to be marked for |
| 2941 | * displaying. */ |
| 2942 | win_T *wp; |
| 2943 | linenr_T wlnum; |
| 2944 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 2945 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2946 | if (wp->w_p_diff && wp != curwin) |
| 2947 | { |
| 2948 | redraw_win_later(wp, VALID); |
| 2949 | wlnum = diff_lnum_win(lnum, wp); |
| 2950 | if (wlnum > 0) |
| 2951 | changed_lines_buf(wp->w_buffer, wlnum, |
| 2952 | lnume - lnum + wlnum, 0L); |
| 2953 | } |
| 2954 | } |
| 2955 | #endif |
| 2956 | |
| 2957 | changed_common(lnum, col, lnume, xtra); |
| 2958 | } |
| 2959 | |
| 2960 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2961 | changed_lines_buf( |
| 2962 | buf_T *buf, |
| 2963 | linenr_T lnum, /* first line with change */ |
| 2964 | linenr_T lnume, /* line below last changed line */ |
| 2965 | long xtra) /* number of extra lines (negative when deleting) */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2966 | { |
| 2967 | if (buf->b_mod_set) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2968 | { |
| 2969 | /* find the maximum area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2970 | if (lnum < buf->b_mod_top) |
| 2971 | buf->b_mod_top = lnum; |
| 2972 | if (lnum < buf->b_mod_bot) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2973 | { |
| 2974 | /* adjust old bot position for xtra lines */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2975 | buf->b_mod_bot += xtra; |
| 2976 | if (buf->b_mod_bot < lnum) |
| 2977 | buf->b_mod_bot = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2978 | } |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2979 | if (lnume + xtra > buf->b_mod_bot) |
| 2980 | buf->b_mod_bot = lnume + xtra; |
| 2981 | buf->b_mod_xlines += xtra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2982 | } |
| 2983 | else |
| 2984 | { |
| 2985 | /* set the area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2986 | buf->b_mod_set = TRUE; |
| 2987 | buf->b_mod_top = lnum; |
| 2988 | buf->b_mod_bot = lnume + xtra; |
| 2989 | buf->b_mod_xlines = xtra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2990 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2993 | /* |
| 2994 | * Common code for when a change is was made. |
| 2995 | * See changed_lines() for the arguments. |
| 2996 | * Careful: may trigger autocommands that reload the buffer. |
| 2997 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2998 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2999 | changed_common( |
| 3000 | linenr_T lnum, |
| 3001 | colnr_T col, |
| 3002 | linenr_T lnume, |
| 3003 | long xtra) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3004 | { |
| 3005 | win_T *wp; |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3006 | tabpage_T *tp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3007 | int i; |
| 3008 | #ifdef FEAT_JUMPLIST |
| 3009 | int cols; |
| 3010 | pos_T *p; |
| 3011 | int add; |
| 3012 | #endif |
| 3013 | |
| 3014 | /* mark the buffer as modified */ |
| 3015 | changed(); |
| 3016 | |
| 3017 | /* set the '. mark */ |
| 3018 | if (!cmdmod.keepjumps) |
| 3019 | { |
| 3020 | curbuf->b_last_change.lnum = lnum; |
| 3021 | curbuf->b_last_change.col = col; |
| 3022 | |
| 3023 | #ifdef FEAT_JUMPLIST |
| 3024 | /* Create a new entry if a new undo-able change was started or we |
| 3025 | * don't have an entry yet. */ |
| 3026 | if (curbuf->b_new_change || curbuf->b_changelistlen == 0) |
| 3027 | { |
| 3028 | if (curbuf->b_changelistlen == 0) |
| 3029 | add = TRUE; |
| 3030 | else |
| 3031 | { |
| 3032 | /* Don't create a new entry when the line number is the same |
| 3033 | * as the last one and the column is not too far away. Avoids |
| 3034 | * creating many entries for typing "xxxxx". */ |
| 3035 | p = &curbuf->b_changelist[curbuf->b_changelistlen - 1]; |
| 3036 | if (p->lnum != lnum) |
| 3037 | add = TRUE; |
| 3038 | else |
| 3039 | { |
| 3040 | cols = comp_textwidth(FALSE); |
| 3041 | if (cols == 0) |
| 3042 | cols = 79; |
| 3043 | add = (p->col + cols < col || col + cols < p->col); |
| 3044 | } |
| 3045 | } |
| 3046 | if (add) |
| 3047 | { |
| 3048 | /* This is the first of a new sequence of undo-able changes |
| 3049 | * and it's at some distance of the last change. Use a new |
| 3050 | * position in the changelist. */ |
| 3051 | curbuf->b_new_change = FALSE; |
| 3052 | |
| 3053 | if (curbuf->b_changelistlen == JUMPLISTSIZE) |
| 3054 | { |
| 3055 | /* changelist is full: remove oldest entry */ |
| 3056 | curbuf->b_changelistlen = JUMPLISTSIZE - 1; |
| 3057 | mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1, |
| 3058 | sizeof(pos_T) * (JUMPLISTSIZE - 1)); |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3059 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3060 | { |
| 3061 | /* Correct position in changelist for other windows on |
| 3062 | * this buffer. */ |
| 3063 | if (wp->w_buffer == curbuf && wp->w_changelistidx > 0) |
| 3064 | --wp->w_changelistidx; |
| 3065 | } |
| 3066 | } |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3067 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3068 | { |
| 3069 | /* For other windows, if the position in the changelist is |
| 3070 | * at the end it stays at the end. */ |
| 3071 | if (wp->w_buffer == curbuf |
| 3072 | && wp->w_changelistidx == curbuf->b_changelistlen) |
| 3073 | ++wp->w_changelistidx; |
| 3074 | } |
| 3075 | ++curbuf->b_changelistlen; |
| 3076 | } |
| 3077 | } |
| 3078 | curbuf->b_changelist[curbuf->b_changelistlen - 1] = |
| 3079 | curbuf->b_last_change; |
| 3080 | /* The current window is always after the last change, so that "g," |
| 3081 | * takes you back to it. */ |
| 3082 | curwin->w_changelistidx = curbuf->b_changelistlen; |
| 3083 | #endif |
| 3084 | } |
| 3085 | |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3086 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3087 | { |
| 3088 | if (wp->w_buffer == curbuf) |
| 3089 | { |
| 3090 | /* Mark this window to be redrawn later. */ |
| 3091 | if (wp->w_redr_type < VALID) |
| 3092 | wp->w_redr_type = VALID; |
| 3093 | |
| 3094 | /* Check if a change in the buffer has invalidated the cached |
| 3095 | * values for the cursor. */ |
| 3096 | #ifdef FEAT_FOLDING |
| 3097 | /* |
| 3098 | * Update the folds for this window. Can't postpone this, because |
| 3099 | * a following operator might work on the whole fold: ">>dd". |
| 3100 | */ |
| 3101 | foldUpdate(wp, lnum, lnume + xtra - 1); |
| 3102 | |
| 3103 | /* The change may cause lines above or below the change to become |
| 3104 | * included in a fold. Set lnum/lnume to the first/last line that |
| 3105 | * might be displayed differently. |
| 3106 | * Set w_cline_folded here as an efficient way to update it when |
| 3107 | * inserting lines just above a closed fold. */ |
| 3108 | i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL); |
| 3109 | if (wp->w_cursor.lnum == lnum) |
| 3110 | wp->w_cline_folded = i; |
| 3111 | i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL); |
| 3112 | if (wp->w_cursor.lnum == lnume) |
| 3113 | wp->w_cline_folded = i; |
| 3114 | |
| 3115 | /* If the changed line is in a range of previously folded lines, |
| 3116 | * compare with the first line in that range. */ |
| 3117 | if (wp->w_cursor.lnum <= lnum) |
| 3118 | { |
| 3119 | i = find_wl_entry(wp, lnum); |
| 3120 | if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum) |
| 3121 | changed_line_abv_curs_win(wp); |
| 3122 | } |
| 3123 | #endif |
| 3124 | |
| 3125 | if (wp->w_cursor.lnum > lnum) |
| 3126 | changed_line_abv_curs_win(wp); |
| 3127 | else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col) |
| 3128 | changed_cline_bef_curs_win(wp); |
| 3129 | if (wp->w_botline >= lnum) |
| 3130 | { |
| 3131 | /* Assume that botline doesn't change (inserted lines make |
| 3132 | * other lines scroll down below botline). */ |
| 3133 | approximate_botline_win(wp); |
| 3134 | } |
| 3135 | |
| 3136 | /* Check if any w_lines[] entries have become invalid. |
| 3137 | * For entries below the change: Correct the lnums for |
| 3138 | * inserted/deleted lines. Makes it possible to stop displaying |
| 3139 | * after the change. */ |
| 3140 | for (i = 0; i < wp->w_lines_valid; ++i) |
| 3141 | if (wp->w_lines[i].wl_valid) |
| 3142 | { |
| 3143 | if (wp->w_lines[i].wl_lnum >= lnum) |
| 3144 | { |
| 3145 | if (wp->w_lines[i].wl_lnum < lnume) |
| 3146 | { |
| 3147 | /* line included in change */ |
| 3148 | wp->w_lines[i].wl_valid = FALSE; |
| 3149 | } |
| 3150 | else if (xtra != 0) |
| 3151 | { |
| 3152 | /* line below change */ |
| 3153 | wp->w_lines[i].wl_lnum += xtra; |
| 3154 | #ifdef FEAT_FOLDING |
| 3155 | wp->w_lines[i].wl_lastlnum += xtra; |
| 3156 | #endif |
| 3157 | } |
| 3158 | } |
| 3159 | #ifdef FEAT_FOLDING |
| 3160 | else if (wp->w_lines[i].wl_lastlnum >= lnum) |
| 3161 | { |
| 3162 | /* change somewhere inside this range of folded lines, |
| 3163 | * may need to be redrawn */ |
| 3164 | wp->w_lines[i].wl_valid = FALSE; |
| 3165 | } |
| 3166 | #endif |
| 3167 | } |
Bram Moolenaar | 3234cc6 | 2009-11-03 17:47:12 +0000 | [diff] [blame] | 3168 | |
| 3169 | #ifdef FEAT_FOLDING |
| 3170 | /* Take care of side effects for setting w_topline when folds have |
| 3171 | * changed. Esp. when the buffer was changed in another window. */ |
| 3172 | if (hasAnyFolding(wp)) |
| 3173 | set_topline(wp, wp->w_topline); |
| 3174 | #endif |
Bram Moolenaar | fd859c9 | 2014-05-13 12:44:24 +0200 | [diff] [blame] | 3175 | /* relative numbering may require updating more */ |
| 3176 | if (wp->w_p_rnu) |
| 3177 | redraw_win_later(wp, SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3178 | } |
| 3179 | } |
| 3180 | |
| 3181 | /* Call update_screen() later, which checks out what needs to be redrawn, |
| 3182 | * since it notices b_mod_set and then uses b_mod_*. */ |
| 3183 | if (must_redraw < VALID) |
| 3184 | must_redraw = VALID; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3185 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3186 | /* when the cursor line is changed always trigger CursorMoved */ |
Bram Moolenaar | e163f1c | 2006-10-17 09:12:21 +0000 | [diff] [blame] | 3187 | if (lnum <= curwin->w_cursor.lnum |
| 3188 | && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum) |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3189 | last_cursormoved.lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | /* |
| 3193 | * unchanged() is called when the changed flag must be reset for buffer 'buf' |
| 3194 | */ |
| 3195 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3196 | unchanged( |
| 3197 | buf_T *buf, |
| 3198 | int ff) /* also reset 'fileformat' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3199 | { |
Bram Moolenaar | 164c60f | 2011-01-22 00:11:50 +0100 | [diff] [blame] | 3200 | if (buf->b_changed || (ff && file_ff_differs(buf, FALSE))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3201 | { |
| 3202 | buf->b_changed = 0; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3203 | ml_setflags(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3204 | if (ff) |
| 3205 | save_file_ff(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3206 | check_status(buf); |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 3207 | redraw_tabline = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3208 | #ifdef FEAT_TITLE |
| 3209 | need_maketitle = TRUE; /* set window title later */ |
| 3210 | #endif |
| 3211 | } |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 3212 | ++CHANGEDTICK(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3213 | #ifdef FEAT_NETBEANS_INTG |
| 3214 | netbeans_unmodified(buf); |
| 3215 | #endif |
| 3216 | } |
| 3217 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3218 | /* |
| 3219 | * check_status: called when the status bars for the buffer 'buf' |
| 3220 | * need to be updated |
| 3221 | */ |
| 3222 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3223 | check_status(buf_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3224 | { |
| 3225 | win_T *wp; |
| 3226 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 3227 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3228 | if (wp->w_buffer == buf && wp->w_status_height) |
| 3229 | { |
| 3230 | wp->w_redr_status = TRUE; |
| 3231 | if (must_redraw < VALID) |
| 3232 | must_redraw = VALID; |
| 3233 | } |
| 3234 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3235 | |
| 3236 | /* |
| 3237 | * If the file is readonly, give a warning message with the first change. |
| 3238 | * Don't do this for autocommands. |
| 3239 | * Don't use emsg(), because it flushes the macro buffer. |
Bram Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 3240 | * 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] | 3241 | * will be TRUE. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3242 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3243 | */ |
| 3244 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3245 | change_warning( |
| 3246 | int col) /* column for message; non-zero when in insert |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3247 | mode and 'showmode' is on */ |
| 3248 | { |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 3249 | static char *w_readonly = N_("W10: Warning: Changing a readonly file"); |
| 3250 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3251 | if (curbuf->b_did_warn == FALSE |
| 3252 | && curbufIsChanged() == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3253 | && !autocmd_busy |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3254 | && curbuf->b_p_ro) |
| 3255 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3256 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3257 | apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3258 | --curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3259 | if (!curbuf->b_p_ro) |
| 3260 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3268 | msg_source(HL_ATTR(HLF_W)); |
| 3269 | MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST); |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 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 */ |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3322 | smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3355 | || c == K_MOUSEMOVE |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3356 | || c == K_MIDDLEMOUSE |
| 3357 | || c == K_MIDDLEDRAG |
| 3358 | || c == K_MIDDLERELEASE |
| 3359 | || c == K_RIGHTMOUSE |
| 3360 | || c == K_RIGHTDRAG |
| 3361 | || c == K_RIGHTRELEASE |
| 3362 | || c == K_MOUSEDOWN |
| 3363 | || c == K_MOUSEUP |
| 3364 | || c == K_MOUSELEFT |
| 3365 | || c == K_MOUSERIGHT |
| 3366 | || c == K_X1MOUSE |
| 3367 | || c == K_X1DRAG |
| 3368 | || c == K_X1RELEASE |
| 3369 | || c == K_X2MOUSE |
| 3370 | || c == K_X2DRAG |
| 3371 | || c == K_X2RELEASE; |
| 3372 | } |
| 3373 | #endif |
| 3374 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3375 | /* |
| 3376 | * Get a key stroke directly from the user. |
| 3377 | * Ignores mouse clicks and scrollbar events, except a click for the left |
| 3378 | * button (used at the more prompt). |
| 3379 | * Doesn't use vgetc(), because it syncs undo and eats mapped characters. |
| 3380 | * Disadvantage: typeahead is ignored. |
| 3381 | * Translates the interrupt character for unix to ESC. |
| 3382 | */ |
| 3383 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3384 | get_keystroke(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3385 | { |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3386 | char_u *buf = NULL; |
| 3387 | int buflen = 150; |
| 3388 | int maxlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3389 | int len = 0; |
| 3390 | int n; |
| 3391 | int save_mapped_ctrl_c = mapped_ctrl_c; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3392 | int waited = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3393 | |
| 3394 | mapped_ctrl_c = FALSE; /* mappings are not used here */ |
| 3395 | for (;;) |
| 3396 | { |
| 3397 | cursor_on(); |
| 3398 | out_flush(); |
| 3399 | |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3400 | /* Leave some room for check_termcode() to insert a key code into (max |
| 3401 | * 5 chars plus NUL). And fix_input_buffer() can triple the number of |
| 3402 | * bytes. */ |
| 3403 | maxlen = (buflen - 6 - len) / 3; |
| 3404 | if (buf == NULL) |
| 3405 | buf = alloc(buflen); |
| 3406 | else if (maxlen < 10) |
| 3407 | { |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 3408 | char_u *t_buf = buf; |
| 3409 | |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 3410 | /* Need some more space. This might happen when receiving a long |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3411 | * escape sequence. */ |
| 3412 | buflen += 100; |
| 3413 | buf = vim_realloc(buf, buflen); |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 3414 | if (buf == NULL) |
| 3415 | vim_free(t_buf); |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3416 | maxlen = (buflen - 6 - len) / 3; |
| 3417 | } |
| 3418 | if (buf == NULL) |
| 3419 | { |
| 3420 | do_outofmem_msg((long_u)buflen); |
| 3421 | return ESC; /* panic! */ |
| 3422 | } |
| 3423 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3424 | /* First time: blocking wait. Second time: wait up to 100ms for a |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3425 | * terminal code to complete. */ |
| 3426 | n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3427 | if (n > 0) |
| 3428 | { |
| 3429 | /* Replace zero and CSI by a special key code. */ |
Bram Moolenaar | 6bff02e | 2016-08-16 22:50:55 +0200 | [diff] [blame] | 3430 | n = fix_input_buffer(buf + len, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3431 | len += n; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3432 | waited = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3433 | } |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3434 | else if (len > 0) |
| 3435 | ++waited; /* keep track of the waiting time */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3436 | |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3437 | /* Incomplete termcode and not timed out yet: get more characters */ |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3438 | if ((n = check_termcode(1, buf, buflen, &len)) < 0 |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3439 | && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3440 | continue; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3441 | |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3442 | if (n == KEYLEN_REMOVED) /* key code removed */ |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3443 | { |
Bram Moolenaar | fd30cd4 | 2011-03-22 13:07:26 +0100 | [diff] [blame] | 3444 | if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0) |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3445 | { |
| 3446 | /* Redrawing was postponed, do it now. */ |
| 3447 | update_screen(0); |
| 3448 | setcursor(); /* put cursor back where it belongs */ |
| 3449 | } |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3450 | continue; |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3451 | } |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3452 | if (n > 0) /* found a termcode: adjust length */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3453 | len = n; |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3454 | if (len == 0) /* nothing typed yet */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3455 | continue; |
| 3456 | |
| 3457 | /* Handle modifier and/or special key code. */ |
| 3458 | n = buf[0]; |
| 3459 | if (n == K_SPECIAL) |
| 3460 | { |
| 3461 | n = TO_SPECIAL(buf[1], buf[2]); |
| 3462 | if (buf[1] == KS_MODIFIER |
| 3463 | || n == K_IGNORE |
Bram Moolenaar | a5be25e | 2013-03-16 21:35:33 +0100 | [diff] [blame] | 3464 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3465 | || (is_mouse_key(n) && n != K_LEFTMOUSE) |
Bram Moolenaar | a5be25e | 2013-03-16 21:35:33 +0100 | [diff] [blame] | 3466 | #endif |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3467 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3468 | || n == K_VER_SCROLLBAR |
| 3469 | || n == K_HOR_SCROLLBAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3470 | #endif |
| 3471 | ) |
| 3472 | { |
| 3473 | if (buf[1] == KS_MODIFIER) |
| 3474 | mod_mask = buf[2]; |
| 3475 | len -= 3; |
| 3476 | if (len > 0) |
| 3477 | mch_memmove(buf, buf + 3, (size_t)len); |
| 3478 | continue; |
| 3479 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 3480 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3481 | } |
| 3482 | #ifdef FEAT_MBYTE |
| 3483 | if (has_mbyte) |
| 3484 | { |
| 3485 | if (MB_BYTE2LEN(n) > len) |
| 3486 | continue; /* more bytes to get */ |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3487 | buf[len >= buflen ? buflen - 1 : len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3488 | n = (*mb_ptr2char)(buf); |
| 3489 | } |
| 3490 | #endif |
| 3491 | #ifdef UNIX |
| 3492 | if (n == intr_char) |
| 3493 | n = ESC; |
| 3494 | #endif |
| 3495 | break; |
| 3496 | } |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3497 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3498 | |
| 3499 | mapped_ctrl_c = save_mapped_ctrl_c; |
| 3500 | return n; |
| 3501 | } |
| 3502 | |
| 3503 | /* |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3504 | * Get a number from the user. |
| 3505 | * When "mouse_used" is not NULL allow using the mouse. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3506 | */ |
| 3507 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3508 | get_number( |
| 3509 | int colon, /* allow colon to abort */ |
| 3510 | int *mouse_used) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3511 | { |
| 3512 | int n = 0; |
| 3513 | int c; |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3514 | int typed = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3515 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3516 | if (mouse_used != NULL) |
| 3517 | *mouse_used = FALSE; |
| 3518 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3519 | /* When not printing messages, the user won't know what to type, return a |
| 3520 | * zero (as if CR was hit). */ |
| 3521 | if (msg_silent != 0) |
| 3522 | return 0; |
| 3523 | |
| 3524 | #ifdef USE_ON_FLY_SCROLL |
| 3525 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 3526 | #endif |
| 3527 | ++no_mapping; |
| 3528 | ++allow_keys; /* no mapping here, but recognize keys */ |
| 3529 | for (;;) |
| 3530 | { |
| 3531 | windgoto(msg_row, msg_col); |
| 3532 | c = safe_vgetc(); |
| 3533 | if (VIM_ISDIGIT(c)) |
| 3534 | { |
| 3535 | n = n * 10 + c - '0'; |
| 3536 | msg_putchar(c); |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3537 | ++typed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3538 | } |
| 3539 | else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H) |
| 3540 | { |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3541 | if (typed > 0) |
| 3542 | { |
| 3543 | MSG_PUTS("\b \b"); |
| 3544 | --typed; |
| 3545 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3546 | n /= 10; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3547 | } |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3548 | #ifdef FEAT_MOUSE |
| 3549 | else if (mouse_used != NULL && c == K_LEFTMOUSE) |
| 3550 | { |
| 3551 | *mouse_used = TRUE; |
| 3552 | n = mouse_row + 1; |
| 3553 | break; |
| 3554 | } |
| 3555 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3556 | else if (n == 0 && c == ':' && colon) |
| 3557 | { |
| 3558 | stuffcharReadbuff(':'); |
| 3559 | if (!exmode_active) |
| 3560 | cmdline_row = msg_row; |
| 3561 | skip_redraw = TRUE; /* skip redraw once */ |
| 3562 | do_redraw = FALSE; |
| 3563 | break; |
| 3564 | } |
| 3565 | else if (c == CAR || c == NL || c == Ctrl_C || c == ESC) |
| 3566 | break; |
| 3567 | } |
| 3568 | --no_mapping; |
| 3569 | --allow_keys; |
| 3570 | return n; |
| 3571 | } |
| 3572 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3573 | /* |
| 3574 | * Ask the user to enter a number. |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3575 | * When "mouse_used" is not NULL allow using the mouse and in that case return |
| 3576 | * the line number. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3577 | */ |
| 3578 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3579 | prompt_for_number(int *mouse_used) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3580 | { |
| 3581 | int i; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3582 | int save_cmdline_row; |
| 3583 | int save_State; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3584 | |
| 3585 | /* When using ":silent" assume that <CR> was entered. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3586 | if (mouse_used != NULL) |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 3587 | MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): ")); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3588 | else |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 3589 | MSG_PUTS(_("Type number and <Enter> (empty cancels): ")); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3590 | |
Bram Moolenaar | 203335e | 2006-09-03 14:35:42 +0000 | [diff] [blame] | 3591 | /* Set the state such that text can be selected/copied/pasted and we still |
| 3592 | * get mouse events. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3593 | save_cmdline_row = cmdline_row; |
Bram Moolenaar | 203335e | 2006-09-03 14:35:42 +0000 | [diff] [blame] | 3594 | cmdline_row = 0; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3595 | save_State = State; |
Bram Moolenaar | c904107 | 2017-07-16 15:48:46 +0200 | [diff] [blame] | 3596 | State = ASKMORE; /* prevents a screen update when using a timer */ |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3597 | #ifdef FEAT_MOUSE |
| 3598 | /* May show different mouse shape. */ |
| 3599 | setmouse(); |
| 3600 | #endif |
| 3601 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3602 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3603 | i = get_number(TRUE, mouse_used); |
| 3604 | if (KeyTyped) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3605 | { |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3606 | /* don't call wait_return() now */ |
| 3607 | /* msg_putchar('\n'); */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3608 | cmdline_row = msg_row - 1; |
| 3609 | need_wait_return = FALSE; |
| 3610 | msg_didany = FALSE; |
Bram Moolenaar | b245016 | 2009-07-22 09:04:20 +0000 | [diff] [blame] | 3611 | msg_didout = FALSE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3612 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3613 | else |
| 3614 | cmdline_row = save_cmdline_row; |
| 3615 | State = save_State; |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3616 | #ifdef FEAT_MOUSE |
| 3617 | /* May need to restore mouse shape. */ |
| 3618 | setmouse(); |
| 3619 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3620 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3621 | return i; |
| 3622 | } |
| 3623 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3624 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3625 | msgmore(long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3626 | { |
| 3627 | long pn; |
| 3628 | |
| 3629 | if (global_busy /* no messages now, wait until global is finished */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3630 | || !messaging()) /* 'lazyredraw' set, don't do messages now */ |
| 3631 | return; |
| 3632 | |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 3633 | /* We don't want to overwrite another important message, but do overwrite |
| 3634 | * a previous "more lines" or "fewer lines" message, so that "5dd" and |
| 3635 | * then "put" reports the last action. */ |
| 3636 | if (keep_msg != NULL && !keep_msg_more) |
| 3637 | return; |
| 3638 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3639 | if (n > 0) |
| 3640 | pn = n; |
| 3641 | else |
| 3642 | pn = -n; |
| 3643 | |
| 3644 | if (pn > p_report) |
| 3645 | { |
| 3646 | if (pn == 1) |
| 3647 | { |
| 3648 | if (n > 0) |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3649 | vim_strncpy(msg_buf, (char_u *)_("1 more line"), |
| 3650 | MSG_BUF_LEN - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3651 | else |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3652 | vim_strncpy(msg_buf, (char_u *)_("1 line less"), |
| 3653 | MSG_BUF_LEN - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3654 | } |
| 3655 | else |
| 3656 | { |
| 3657 | if (n > 0) |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3658 | vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
| 3659 | _("%ld more lines"), pn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3660 | else |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3661 | vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
| 3662 | _("%ld fewer lines"), pn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3663 | } |
| 3664 | if (got_int) |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3665 | vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3666 | if (msg(msg_buf)) |
| 3667 | { |
Bram Moolenaar | 238a564 | 2006-02-21 22:12:05 +0000 | [diff] [blame] | 3668 | set_keep_msg(msg_buf, 0); |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 3669 | keep_msg_more = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3670 | } |
| 3671 | } |
| 3672 | } |
| 3673 | |
| 3674 | /* |
| 3675 | * flush map and typeahead buffers and give a warning for an error |
| 3676 | */ |
| 3677 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3678 | beep_flush(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3679 | { |
| 3680 | if (emsg_silent == 0) |
| 3681 | { |
| 3682 | flush_buffers(FALSE); |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3683 | vim_beep(BO_ERROR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3684 | } |
| 3685 | } |
| 3686 | |
| 3687 | /* |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3688 | * Give a warning for an error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3689 | */ |
| 3690 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3691 | vim_beep( |
| 3692 | unsigned val) /* one of the BO_ values, e.g., BO_OPER */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3693 | { |
Bram Moolenaar | b48e96f | 2018-02-13 12:26:14 +0100 | [diff] [blame] | 3694 | #ifdef FEAT_EVAL |
| 3695 | called_vim_beep = TRUE; |
| 3696 | #endif |
| 3697 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3698 | if (emsg_silent == 0) |
| 3699 | { |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3700 | if (!((bo_flags & val) || (bo_flags & BO_ALL))) |
| 3701 | { |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3702 | #ifdef ELAPSED_FUNC |
| 3703 | static int did_init = FALSE; |
| 3704 | static ELAPSED_TYPE start_tv; |
| 3705 | |
| 3706 | /* Only beep once per half a second, otherwise a sequence of beeps |
| 3707 | * would freeze Vim. */ |
| 3708 | if (!did_init || ELAPSED_FUNC(start_tv) > 500) |
| 3709 | { |
| 3710 | did_init = TRUE; |
| 3711 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3712 | #endif |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3713 | if (p_vb |
| 3714 | #ifdef FEAT_GUI |
| 3715 | /* While the GUI is starting up the termcap is set for |
| 3716 | * the GUI but the output still goes to a terminal. */ |
| 3717 | && !(gui.in_use && gui.starting) |
| 3718 | #endif |
| 3719 | ) |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3720 | { |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3721 | out_str_cf(T_VB); |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3722 | #ifdef FEAT_VTP |
| 3723 | /* No restore color information, refresh the screen. */ |
| 3724 | if (has_vtp_working() != 0 |
| 3725 | # ifdef FEAT_TERMGUICOLORS |
Bram Moolenaar | c5cd885 | 2018-05-01 15:47:38 +0200 | [diff] [blame] | 3726 | && (p_tgc || (!p_tgc && t_colors >= 256)) |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3727 | # endif |
| 3728 | ) |
| 3729 | { |
| 3730 | redraw_later(CLEAR); |
| 3731 | update_screen(0); |
| 3732 | redrawcmd(); |
| 3733 | } |
| 3734 | #endif |
| 3735 | } |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3736 | else |
| 3737 | out_char(BELL); |
| 3738 | #ifdef ELAPSED_FUNC |
| 3739 | } |
| 3740 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3741 | } |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3742 | |
Bram Moolenaar | b48e96f | 2018-02-13 12:26:14 +0100 | [diff] [blame] | 3743 | /* When 'debug' contains "beep" produce a message. If we are sourcing |
| 3744 | * a script or executing a function give the user a hint where the beep |
| 3745 | * comes from. */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3746 | if (vim_strchr(p_debug, 'e') != NULL) |
| 3747 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3748 | msg_source(HL_ATTR(HLF_W)); |
| 3749 | msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W)); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3750 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3751 | } |
| 3752 | } |
| 3753 | |
| 3754 | /* |
| 3755 | * To get the "real" home directory: |
| 3756 | * - get value of $HOME |
| 3757 | * For Unix: |
| 3758 | * - go to that directory |
| 3759 | * - do mch_dirname() to get the real name of that directory. |
| 3760 | * This also works with mounts and links. |
| 3761 | * Don't do this for MS-DOS, it will change the "current dir" for a drive. |
| 3762 | */ |
| 3763 | static char_u *homedir = NULL; |
| 3764 | |
| 3765 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3766 | init_homedir(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3767 | { |
| 3768 | char_u *var; |
| 3769 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3770 | /* In case we are called a second time (when 'encoding' changes). */ |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3771 | VIM_CLEAR(homedir); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3772 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3773 | #ifdef VMS |
| 3774 | var = mch_getenv((char_u *)"SYS$LOGIN"); |
| 3775 | #else |
| 3776 | var = mch_getenv((char_u *)"HOME"); |
| 3777 | #endif |
| 3778 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3779 | #ifdef WIN3264 |
| 3780 | /* |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3781 | * Typically, $HOME is not defined on Windows, unless the user has |
| 3782 | * specifically defined it for Vim's sake. However, on Windows NT |
| 3783 | * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for |
| 3784 | * each user. Try constructing $HOME from these. |
| 3785 | */ |
Bram Moolenaar | b47a259 | 2017-08-30 13:22:28 +0200 | [diff] [blame] | 3786 | if (var == NULL || *var == NUL) |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3787 | { |
| 3788 | char_u *homedrive, *homepath; |
| 3789 | |
| 3790 | homedrive = mch_getenv((char_u *)"HOMEDRIVE"); |
| 3791 | homepath = mch_getenv((char_u *)"HOMEPATH"); |
| 3792 | if (homepath == NULL || *homepath == NUL) |
| 3793 | homepath = (char_u *)"\\"; |
| 3794 | if (homedrive != NULL |
| 3795 | && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL) |
| 3796 | { |
| 3797 | sprintf((char *)NameBuff, "%s%s", homedrive, homepath); |
| 3798 | if (NameBuff[0] != NUL) |
| 3799 | var = NameBuff; |
| 3800 | } |
| 3801 | } |
| 3802 | |
| 3803 | if (var == NULL) |
| 3804 | var = mch_getenv((char_u *)"USERPROFILE"); |
| 3805 | |
| 3806 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3807 | * Weird but true: $HOME may contain an indirect reference to another |
| 3808 | * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set |
| 3809 | * when $HOME is being set. |
| 3810 | */ |
| 3811 | if (var != NULL && *var == '%') |
| 3812 | { |
| 3813 | char_u *p; |
| 3814 | char_u *exp; |
| 3815 | |
| 3816 | p = vim_strchr(var + 1, '%'); |
| 3817 | if (p != NULL) |
| 3818 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3819 | vim_strncpy(NameBuff, var + 1, p - (var + 1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3820 | exp = mch_getenv(NameBuff); |
| 3821 | if (exp != NULL && *exp != NUL |
| 3822 | && STRLEN(exp) + STRLEN(p) < MAXPATHL) |
| 3823 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 3824 | vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3825 | var = NameBuff; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3826 | } |
| 3827 | } |
| 3828 | } |
| 3829 | |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3830 | if (var != NULL && *var == NUL) /* empty is same as not set */ |
| 3831 | var = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3832 | |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3833 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3834 | if (enc_utf8 && var != NULL) |
| 3835 | { |
| 3836 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 3837 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3838 | |
| 3839 | /* Convert from active codepage to UTF-8. Other conversions are |
| 3840 | * not done, because they would fail for non-ASCII characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3841 | acp_to_enc(var, (int)STRLEN(var), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3842 | if (pp != NULL) |
| 3843 | { |
| 3844 | homedir = pp; |
| 3845 | return; |
| 3846 | } |
| 3847 | } |
| 3848 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3849 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3850 | /* |
| 3851 | * Default home dir is C:/ |
| 3852 | * Best assumption we can make in such a situation. |
| 3853 | */ |
| 3854 | if (var == NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3855 | var = (char_u *)"C:/"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3856 | #endif |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3857 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3858 | if (var != NULL) |
| 3859 | { |
| 3860 | #ifdef UNIX |
| 3861 | /* |
| 3862 | * Change to the directory and get the actual path. This resolves |
| 3863 | * links. Don't do it when we can't return. |
| 3864 | */ |
| 3865 | if (mch_dirname(NameBuff, MAXPATHL) == OK |
| 3866 | && mch_chdir((char *)NameBuff) == 0) |
| 3867 | { |
| 3868 | if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK) |
| 3869 | var = IObuff; |
| 3870 | if (mch_chdir((char *)NameBuff) != 0) |
| 3871 | EMSG(_(e_prev_dir)); |
| 3872 | } |
| 3873 | #endif |
| 3874 | homedir = vim_strsave(var); |
| 3875 | } |
| 3876 | } |
| 3877 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3878 | #if defined(EXITFREE) || defined(PROTO) |
| 3879 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3880 | free_homedir(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3881 | { |
| 3882 | vim_free(homedir); |
| 3883 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 3884 | |
| 3885 | # ifdef FEAT_CMDL_COMPL |
| 3886 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3887 | free_users(void) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 3888 | { |
| 3889 | ga_clear_strings(&ga_users); |
| 3890 | } |
| 3891 | # endif |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3892 | #endif |
| 3893 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3894 | /* |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3895 | * Call expand_env() and store the result in an allocated string. |
| 3896 | * This is not very memory efficient, this expects the result to be freed |
| 3897 | * again soon. |
| 3898 | */ |
| 3899 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3900 | expand_env_save(char_u *src) |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3901 | { |
| 3902 | return expand_env_save_opt(src, FALSE); |
| 3903 | } |
| 3904 | |
| 3905 | /* |
| 3906 | * Idem, but when "one" is TRUE handle the string as one file name, only |
| 3907 | * expand "~" at the start. |
| 3908 | */ |
| 3909 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3910 | expand_env_save_opt(char_u *src, int one) |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3911 | { |
| 3912 | char_u *p; |
| 3913 | |
| 3914 | p = alloc(MAXPATHL); |
| 3915 | if (p != NULL) |
| 3916 | expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL); |
| 3917 | return p; |
| 3918 | } |
| 3919 | |
| 3920 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3921 | * Expand environment variable with path name. |
| 3922 | * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded. |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3923 | * Skips over "\ ", "\~" and "\$" (not for Win32 though). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3924 | * If anything fails no expansion is done and dst equals src. |
| 3925 | */ |
| 3926 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3927 | expand_env( |
| 3928 | char_u *src, /* input string e.g. "$HOME/vim.hlp" */ |
| 3929 | char_u *dst, /* where to put the result */ |
| 3930 | int dstlen) /* maximum length of the result */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3931 | { |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 3932 | expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3933 | } |
| 3934 | |
| 3935 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3936 | expand_env_esc( |
| 3937 | char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */ |
| 3938 | char_u *dst, /* where to put the result */ |
| 3939 | int dstlen, /* maximum length of the result */ |
| 3940 | int esc, /* escape spaces in expanded variables */ |
| 3941 | int one, /* "srcp" is one file name */ |
| 3942 | char_u *startstr) /* start again after this (can be NULL) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3943 | { |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3944 | char_u *src; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3945 | char_u *tail; |
| 3946 | int c; |
| 3947 | char_u *var; |
| 3948 | int copy_char; |
| 3949 | int mustfree; /* var was allocated, need to free it later */ |
| 3950 | int at_start = TRUE; /* at start of a name */ |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3951 | int startstr_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3952 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3953 | if (startstr != NULL) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3954 | startstr_len = (int)STRLEN(startstr); |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3955 | |
| 3956 | src = skipwhite(srcp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3957 | --dstlen; /* leave one char space for "\," */ |
| 3958 | while (*src && dstlen > 0) |
| 3959 | { |
Bram Moolenaar | be83b73 | 2015-08-25 14:21:19 +0200 | [diff] [blame] | 3960 | #ifdef FEAT_EVAL |
| 3961 | /* Skip over `=expr`. */ |
| 3962 | if (src[0] == '`' && src[1] == '=') |
| 3963 | { |
| 3964 | size_t len; |
| 3965 | |
| 3966 | var = src; |
| 3967 | src += 2; |
| 3968 | (void)skip_expr(&src); |
| 3969 | if (*src == '`') |
| 3970 | ++src; |
| 3971 | len = src - var; |
| 3972 | if (len > (size_t)dstlen) |
| 3973 | len = dstlen; |
| 3974 | vim_strncpy(dst, var, len); |
| 3975 | dst += len; |
Bram Moolenaar | 5df1ed2 | 2015-09-01 16:25:34 +0200 | [diff] [blame] | 3976 | dstlen -= (int)len; |
Bram Moolenaar | be83b73 | 2015-08-25 14:21:19 +0200 | [diff] [blame] | 3977 | continue; |
| 3978 | } |
| 3979 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3980 | copy_char = TRUE; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 3981 | if ((*src == '$' |
| 3982 | #ifdef VMS |
| 3983 | && at_start |
| 3984 | #endif |
| 3985 | ) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 3986 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3987 | || *src == '%' |
| 3988 | #endif |
| 3989 | || (*src == '~' && at_start)) |
| 3990 | { |
| 3991 | mustfree = FALSE; |
| 3992 | |
| 3993 | /* |
| 3994 | * The variable name is copied into dst temporarily, because it may |
| 3995 | * be a string in read-only memory and a NUL needs to be appended. |
| 3996 | */ |
| 3997 | if (*src != '~') /* environment var */ |
| 3998 | { |
| 3999 | tail = src + 1; |
| 4000 | var = dst; |
| 4001 | c = dstlen - 1; |
| 4002 | |
| 4003 | #ifdef UNIX |
| 4004 | /* Unix has ${var-name} type environment vars */ |
| 4005 | if (*tail == '{' && !vim_isIDc('{')) |
| 4006 | { |
| 4007 | tail++; /* ignore '{' */ |
| 4008 | while (c-- > 0 && *tail && *tail != '}') |
| 4009 | *var++ = *tail++; |
| 4010 | } |
| 4011 | else |
| 4012 | #endif |
| 4013 | { |
| 4014 | while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail)) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4015 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4016 | || (*src == '%' && *tail != '%') |
| 4017 | #endif |
| 4018 | )) |
| 4019 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4020 | *var++ = *tail++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4021 | } |
| 4022 | } |
| 4023 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4024 | #if defined(MSWIN) || defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4025 | # ifdef UNIX |
| 4026 | if (src[1] == '{' && *tail != '}') |
| 4027 | # else |
| 4028 | if (*src == '%' && *tail != '%') |
| 4029 | # endif |
| 4030 | var = NULL; |
| 4031 | else |
| 4032 | { |
| 4033 | # ifdef UNIX |
| 4034 | if (src[1] == '{') |
| 4035 | # else |
| 4036 | if (*src == '%') |
| 4037 | #endif |
| 4038 | ++tail; |
| 4039 | #endif |
| 4040 | *var = NUL; |
| 4041 | var = vim_getenv(dst, &mustfree); |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4042 | #if defined(MSWIN) || defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4043 | } |
| 4044 | #endif |
| 4045 | } |
| 4046 | /* home directory */ |
| 4047 | else if ( src[1] == NUL |
| 4048 | || vim_ispathsep(src[1]) |
| 4049 | || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL) |
| 4050 | { |
| 4051 | var = homedir; |
| 4052 | tail = src + 1; |
| 4053 | } |
| 4054 | else /* user directory */ |
| 4055 | { |
| 4056 | #if defined(UNIX) || (defined(VMS) && defined(USER_HOME)) |
| 4057 | /* |
| 4058 | * Copy ~user to dst[], so we can put a NUL after it. |
| 4059 | */ |
| 4060 | tail = src; |
| 4061 | var = dst; |
| 4062 | c = dstlen - 1; |
| 4063 | while ( c-- > 0 |
| 4064 | && *tail |
| 4065 | && vim_isfilec(*tail) |
| 4066 | && !vim_ispathsep(*tail)) |
| 4067 | *var++ = *tail++; |
| 4068 | *var = NUL; |
| 4069 | # ifdef UNIX |
| 4070 | /* |
| 4071 | * If the system supports getpwnam(), use it. |
| 4072 | * Otherwise, or if getpwnam() fails, the shell is used to |
| 4073 | * expand ~user. This is slower and may fail if the shell |
| 4074 | * does not support ~user (old versions of /bin/sh). |
| 4075 | */ |
| 4076 | # if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H) |
| 4077 | { |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 4078 | /* Note: memory allocated by getpwnam() is never freed. |
| 4079 | * Calling endpwent() apparently doesn't help. */ |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 4080 | struct passwd *pw = (*dst == NUL) |
| 4081 | ? NULL : getpwnam((char *)dst + 1); |
| 4082 | |
| 4083 | var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4084 | } |
| 4085 | if (var == NULL) |
| 4086 | # endif |
| 4087 | { |
| 4088 | expand_T xpc; |
| 4089 | |
| 4090 | ExpandInit(&xpc); |
| 4091 | xpc.xp_context = EXPAND_FILES; |
| 4092 | var = ExpandOne(&xpc, dst, NULL, |
| 4093 | WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4094 | mustfree = TRUE; |
| 4095 | } |
| 4096 | |
| 4097 | # else /* !UNIX, thus VMS */ |
| 4098 | /* |
| 4099 | * USER_HOME is a comma-separated list of |
| 4100 | * directories to search for the user account in. |
| 4101 | */ |
| 4102 | { |
| 4103 | char_u test[MAXPATHL], paths[MAXPATHL]; |
| 4104 | char_u *path, *next_path, *ptr; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4105 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4106 | |
| 4107 | STRCPY(paths, USER_HOME); |
| 4108 | next_path = paths; |
| 4109 | while (*next_path) |
| 4110 | { |
| 4111 | for (path = next_path; *next_path && *next_path != ','; |
| 4112 | next_path++); |
| 4113 | if (*next_path) |
| 4114 | *next_path++ = NUL; |
| 4115 | STRCPY(test, path); |
| 4116 | STRCAT(test, "/"); |
| 4117 | STRCAT(test, dst + 1); |
| 4118 | if (mch_stat(test, &st) == 0) |
| 4119 | { |
| 4120 | var = alloc(STRLEN(test) + 1); |
| 4121 | STRCPY(var, test); |
| 4122 | mustfree = TRUE; |
| 4123 | break; |
| 4124 | } |
| 4125 | } |
| 4126 | } |
| 4127 | # endif /* UNIX */ |
| 4128 | #else |
| 4129 | /* cannot expand user's home directory, so don't try */ |
| 4130 | var = NULL; |
| 4131 | tail = (char_u *)""; /* for gcc */ |
| 4132 | #endif /* UNIX || VMS */ |
| 4133 | } |
| 4134 | |
| 4135 | #ifdef BACKSLASH_IN_FILENAME |
| 4136 | /* If 'shellslash' is set change backslashes to forward slashes. |
| 4137 | * Can't use slash_adjust(), p_ssl may be set temporarily. */ |
| 4138 | if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL) |
| 4139 | { |
| 4140 | char_u *p = vim_strsave(var); |
| 4141 | |
| 4142 | if (p != NULL) |
| 4143 | { |
| 4144 | if (mustfree) |
| 4145 | vim_free(var); |
| 4146 | var = p; |
| 4147 | mustfree = TRUE; |
| 4148 | forward_slash(var); |
| 4149 | } |
| 4150 | } |
| 4151 | #endif |
| 4152 | |
| 4153 | /* If "var" contains white space, escape it with a backslash. |
| 4154 | * Required for ":e ~/tt" when $HOME includes a space. */ |
| 4155 | if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) |
| 4156 | { |
| 4157 | char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); |
| 4158 | |
| 4159 | if (p != NULL) |
| 4160 | { |
| 4161 | if (mustfree) |
| 4162 | vim_free(var); |
| 4163 | var = p; |
| 4164 | mustfree = TRUE; |
| 4165 | } |
| 4166 | } |
| 4167 | |
| 4168 | if (var != NULL && *var != NUL |
| 4169 | && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) |
| 4170 | { |
| 4171 | STRCPY(dst, var); |
| 4172 | dstlen -= (int)STRLEN(var); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4173 | c = (int)STRLEN(var); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4174 | /* if var[] ends in a path separator and tail[] starts |
| 4175 | * with it, skip a character */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4176 | if (*var != NUL && after_pathsep(dst, dst + c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4177 | #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) |
| 4178 | && dst[-1] != ':' |
| 4179 | #endif |
| 4180 | && vim_ispathsep(*tail)) |
| 4181 | ++tail; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4182 | dst += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4183 | src = tail; |
| 4184 | copy_char = FALSE; |
| 4185 | } |
| 4186 | if (mustfree) |
| 4187 | vim_free(var); |
| 4188 | } |
| 4189 | |
| 4190 | if (copy_char) /* copy at least one char */ |
| 4191 | { |
| 4192 | /* |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 4193 | * Recognize the start of a new name, for '~'. |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4194 | * Don't do this when "one" is TRUE, to avoid expanding "~" in |
| 4195 | * ":edit foo ~ foo". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4196 | */ |
| 4197 | at_start = FALSE; |
| 4198 | if (src[0] == '\\' && src[1] != NUL) |
| 4199 | { |
| 4200 | *dst++ = *src++; |
| 4201 | --dstlen; |
| 4202 | } |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4203 | else if ((src[0] == ' ' || src[0] == ',') && !one) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4204 | at_start = TRUE; |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4205 | if (dstlen > 0) |
| 4206 | { |
| 4207 | *dst++ = *src++; |
| 4208 | --dstlen; |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4209 | |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4210 | if (startstr != NULL && src - startstr_len >= srcp |
| 4211 | && STRNCMP(src - startstr_len, startstr, |
| 4212 | startstr_len) == 0) |
| 4213 | at_start = TRUE; |
| 4214 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4215 | } |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4216 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4217 | } |
| 4218 | *dst = NUL; |
| 4219 | } |
| 4220 | |
| 4221 | /* |
| 4222 | * Vim's version of getenv(). |
| 4223 | * Special handling of $HOME, $VIM and $VIMRUNTIME. |
Bram Moolenaar | 2f6b0b8 | 2005-03-08 22:43:10 +0000 | [diff] [blame] | 4224 | * Also does ACP to 'enc' conversion for Win32. |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4225 | * "mustfree" is set to TRUE when returned is allocated, it must be |
| 4226 | * initialized to FALSE by the caller. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4227 | */ |
| 4228 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4229 | vim_getenv(char_u *name, int *mustfree) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4230 | { |
| 4231 | char_u *p; |
| 4232 | char_u *pend; |
| 4233 | int vimruntime; |
| 4234 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4235 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4236 | /* use "C:/" when $HOME is not set */ |
| 4237 | if (STRCMP(name, "HOME") == 0) |
| 4238 | return homedir; |
| 4239 | #endif |
| 4240 | |
| 4241 | p = mch_getenv(name); |
| 4242 | if (p != NULL && *p == NUL) /* empty is the same as not set */ |
| 4243 | p = NULL; |
| 4244 | |
| 4245 | if (p != NULL) |
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 are |
| 4254 | * not done, because they would fail for non-ASCII characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4255 | acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4256 | if (pp != NULL) |
| 4257 | { |
| 4258 | p = pp; |
| 4259 | *mustfree = TRUE; |
| 4260 | } |
| 4261 | } |
| 4262 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4263 | return p; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4264 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4265 | |
| 4266 | vimruntime = (STRCMP(name, "VIMRUNTIME") == 0); |
| 4267 | if (!vimruntime && STRCMP(name, "VIM") != 0) |
| 4268 | return NULL; |
| 4269 | |
| 4270 | /* |
| 4271 | * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM. |
| 4272 | * Don't do this when default_vimruntime_dir is non-empty. |
| 4273 | */ |
| 4274 | if (vimruntime |
| 4275 | #ifdef HAVE_PATHDEF |
| 4276 | && *default_vimruntime_dir == NUL |
| 4277 | #endif |
| 4278 | ) |
| 4279 | { |
| 4280 | p = mch_getenv((char_u *)"VIM"); |
| 4281 | if (p != NULL && *p == NUL) /* empty is the same as not set */ |
| 4282 | p = NULL; |
| 4283 | if (p != NULL) |
| 4284 | { |
| 4285 | p = vim_version_dir(p); |
| 4286 | if (p != NULL) |
| 4287 | *mustfree = TRUE; |
| 4288 | else |
| 4289 | p = mch_getenv((char_u *)"VIM"); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4290 | |
| 4291 | #if defined(FEAT_MBYTE) && defined(WIN3264) |
| 4292 | if (enc_utf8) |
| 4293 | { |
| 4294 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4295 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4296 | |
| 4297 | /* Convert from active codepage to UTF-8. Other conversions |
| 4298 | * are not done, because they would fail for non-ASCII |
| 4299 | * characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4300 | acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4301 | if (pp != NULL) |
| 4302 | { |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4303 | if (*mustfree) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4304 | vim_free(p); |
| 4305 | p = pp; |
| 4306 | *mustfree = TRUE; |
| 4307 | } |
| 4308 | } |
| 4309 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4310 | } |
| 4311 | } |
| 4312 | |
| 4313 | /* |
| 4314 | * When expanding $VIM or $VIMRUNTIME fails, try using: |
| 4315 | * - the directory name from 'helpfile' (unless it contains '$') |
| 4316 | * - the executable name from argv[0] |
| 4317 | */ |
| 4318 | if (p == NULL) |
| 4319 | { |
| 4320 | if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL) |
| 4321 | p = p_hf; |
| 4322 | #ifdef USE_EXE_NAME |
| 4323 | /* |
| 4324 | * Use the name of the executable, obtained from argv[0]. |
| 4325 | */ |
| 4326 | else |
| 4327 | p = exe_name; |
| 4328 | #endif |
| 4329 | if (p != NULL) |
| 4330 | { |
| 4331 | /* remove the file name */ |
| 4332 | pend = gettail(p); |
| 4333 | |
| 4334 | /* remove "doc/" from 'helpfile', if present */ |
| 4335 | if (p == p_hf) |
| 4336 | pend = remove_tail(p, pend, (char_u *)"doc"); |
| 4337 | |
| 4338 | #ifdef USE_EXE_NAME |
| 4339 | # ifdef MACOS_X |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4340 | /* remove "MacOS" from exe_name and add "Resources/vim" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4341 | if (p == exe_name) |
| 4342 | { |
| 4343 | char_u *pend1; |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4344 | char_u *pnew; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4345 | |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4346 | pend1 = remove_tail(p, pend, (char_u *)"MacOS"); |
| 4347 | if (pend1 != pend) |
| 4348 | { |
| 4349 | pnew = alloc((unsigned)(pend1 - p) + 15); |
| 4350 | if (pnew != NULL) |
| 4351 | { |
| 4352 | STRNCPY(pnew, p, (pend1 - p)); |
| 4353 | STRCPY(pnew + (pend1 - p), "Resources/vim"); |
| 4354 | p = pnew; |
| 4355 | pend = p + STRLEN(p); |
| 4356 | } |
| 4357 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4358 | } |
| 4359 | # endif |
| 4360 | /* remove "src/" from exe_name, if present */ |
| 4361 | if (p == exe_name) |
| 4362 | pend = remove_tail(p, pend, (char_u *)"src"); |
| 4363 | #endif |
| 4364 | |
| 4365 | /* for $VIM, remove "runtime/" or "vim54/", if present */ |
| 4366 | if (!vimruntime) |
| 4367 | { |
| 4368 | pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME); |
| 4369 | pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT); |
| 4370 | } |
| 4371 | |
| 4372 | /* remove trailing path separator */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4373 | if (pend > p && after_pathsep(p, pend)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4374 | --pend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4375 | |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4376 | #ifdef MACOS_X |
| 4377 | if (p == exe_name || p == p_hf) |
| 4378 | #endif |
| 4379 | /* check that the result is a directory name */ |
| 4380 | p = vim_strnsave(p, (int)(pend - p)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4381 | |
| 4382 | if (p != NULL && !mch_isdir(p)) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 4383 | VIM_CLEAR(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4384 | else |
| 4385 | { |
| 4386 | #ifdef USE_EXE_NAME |
| 4387 | /* may add "/vim54" or "/runtime" if it exists */ |
| 4388 | if (vimruntime && (pend = vim_version_dir(p)) != NULL) |
| 4389 | { |
| 4390 | vim_free(p); |
| 4391 | p = pend; |
| 4392 | } |
| 4393 | #endif |
| 4394 | *mustfree = TRUE; |
| 4395 | } |
| 4396 | } |
| 4397 | } |
| 4398 | |
| 4399 | #ifdef HAVE_PATHDEF |
| 4400 | /* When there is a pathdef.c file we can use default_vim_dir and |
| 4401 | * default_vimruntime_dir */ |
| 4402 | if (p == NULL) |
| 4403 | { |
| 4404 | /* Only use default_vimruntime_dir when it is not empty */ |
| 4405 | if (vimruntime && *default_vimruntime_dir != NUL) |
| 4406 | { |
| 4407 | p = default_vimruntime_dir; |
| 4408 | *mustfree = FALSE; |
| 4409 | } |
| 4410 | else if (*default_vim_dir != NUL) |
| 4411 | { |
| 4412 | if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) |
| 4413 | *mustfree = TRUE; |
| 4414 | else |
| 4415 | { |
| 4416 | p = default_vim_dir; |
| 4417 | *mustfree = FALSE; |
| 4418 | } |
| 4419 | } |
| 4420 | } |
| 4421 | #endif |
| 4422 | |
| 4423 | /* |
| 4424 | * Set the environment variable, so that the new value can be found fast |
| 4425 | * next time, and others can also use it (e.g. Perl). |
| 4426 | */ |
| 4427 | if (p != NULL) |
| 4428 | { |
| 4429 | if (vimruntime) |
| 4430 | { |
| 4431 | vim_setenv((char_u *)"VIMRUNTIME", p); |
| 4432 | didset_vimruntime = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4433 | } |
| 4434 | else |
| 4435 | { |
| 4436 | vim_setenv((char_u *)"VIM", p); |
| 4437 | didset_vim = TRUE; |
| 4438 | } |
| 4439 | } |
| 4440 | return p; |
| 4441 | } |
| 4442 | |
| 4443 | /* |
| 4444 | * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. |
| 4445 | * Return NULL if not, return its name in allocated memory otherwise. |
| 4446 | */ |
| 4447 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4448 | vim_version_dir(char_u *vimdir) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4449 | { |
| 4450 | char_u *p; |
| 4451 | |
| 4452 | if (vimdir == NULL || *vimdir == NUL) |
| 4453 | return NULL; |
| 4454 | p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); |
| 4455 | if (p != NULL && mch_isdir(p)) |
| 4456 | return p; |
| 4457 | vim_free(p); |
| 4458 | p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); |
| 4459 | if (p != NULL && mch_isdir(p)) |
| 4460 | return p; |
| 4461 | vim_free(p); |
| 4462 | return NULL; |
| 4463 | } |
| 4464 | |
| 4465 | /* |
| 4466 | * If the string between "p" and "pend" ends in "name/", return "pend" minus |
| 4467 | * the length of "name/". Otherwise return "pend". |
| 4468 | */ |
| 4469 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4470 | remove_tail(char_u *p, char_u *pend, char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4471 | { |
| 4472 | int len = (int)STRLEN(name) + 1; |
| 4473 | char_u *newend = pend - len; |
| 4474 | |
| 4475 | if (newend >= p |
| 4476 | && fnamencmp(newend, name, len - 1) == 0 |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4477 | && (newend == p || after_pathsep(p, newend))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4478 | return newend; |
| 4479 | return pend; |
| 4480 | } |
| 4481 | |
Bram Moolenaar | 137374f | 2018-05-13 15:59:50 +0200 | [diff] [blame] | 4482 | void |
| 4483 | vim_unsetenv(char_u *var) |
| 4484 | { |
| 4485 | #ifdef HAVE_UNSETENV |
| 4486 | unsetenv((char *)var); |
| 4487 | #else |
Bram Moolenaar | 1af6a4b | 2018-05-14 22:58:34 +0200 | [diff] [blame] | 4488 | vim_setenv(var, (char_u *)""); |
Bram Moolenaar | 137374f | 2018-05-13 15:59:50 +0200 | [diff] [blame] | 4489 | #endif |
| 4490 | } |
| 4491 | |
| 4492 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4493 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4494 | * Our portable version of setenv. |
| 4495 | */ |
| 4496 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4497 | vim_setenv(char_u *name, char_u *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4498 | { |
| 4499 | #ifdef HAVE_SETENV |
| 4500 | mch_setenv((char *)name, (char *)val, 1); |
| 4501 | #else |
| 4502 | char_u *envbuf; |
| 4503 | |
| 4504 | /* |
| 4505 | * Putenv does not copy the string, it has to remain |
| 4506 | * valid. The allocated memory will never be freed. |
| 4507 | */ |
| 4508 | envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2)); |
| 4509 | if (envbuf != NULL) |
| 4510 | { |
| 4511 | sprintf((char *)envbuf, "%s=%s", name, val); |
| 4512 | putenv((char *)envbuf); |
| 4513 | } |
| 4514 | #endif |
Bram Moolenaar | 011a34d | 2012-02-29 13:49:09 +0100 | [diff] [blame] | 4515 | #ifdef FEAT_GETTEXT |
| 4516 | /* |
| 4517 | * When setting $VIMRUNTIME adjust the directory to find message |
| 4518 | * translations to $VIMRUNTIME/lang. |
| 4519 | */ |
| 4520 | if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) |
| 4521 | { |
| 4522 | char_u *buf = concat_str(val, (char_u *)"/lang"); |
| 4523 | |
| 4524 | if (buf != NULL) |
| 4525 | { |
| 4526 | bindtextdomain(VIMPACKAGE, (char *)buf); |
| 4527 | vim_free(buf); |
| 4528 | } |
| 4529 | } |
| 4530 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4531 | } |
| 4532 | |
| 4533 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 4534 | /* |
| 4535 | * Function given to ExpandGeneric() to obtain an environment variable name. |
| 4536 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4537 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4538 | get_env_name( |
| 4539 | expand_T *xp UNUSED, |
| 4540 | int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4541 | { |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 4542 | # if defined(AMIGA) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4543 | /* |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 4544 | * No environ[] on the Amiga. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4545 | */ |
| 4546 | return NULL; |
| 4547 | # else |
| 4548 | # ifndef __WIN32__ |
| 4549 | /* Borland C++ 5.2 has this in a header file. */ |
| 4550 | extern char **environ; |
| 4551 | # endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 4552 | # define ENVNAMELEN 100 |
| 4553 | static char_u name[ENVNAMELEN]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4554 | char_u *str; |
| 4555 | int n; |
| 4556 | |
| 4557 | str = (char_u *)environ[idx]; |
| 4558 | if (str == NULL) |
| 4559 | return NULL; |
| 4560 | |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 4561 | for (n = 0; n < ENVNAMELEN - 1; ++n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4562 | { |
| 4563 | if (str[n] == '=' || str[n] == NUL) |
| 4564 | break; |
| 4565 | name[n] = str[n]; |
| 4566 | } |
| 4567 | name[n] = NUL; |
| 4568 | return name; |
| 4569 | # endif |
| 4570 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4571 | |
| 4572 | /* |
| 4573 | * Find all user names for user completion. |
| 4574 | * Done only once and then cached. |
| 4575 | */ |
| 4576 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4577 | init_users(void) |
Bram Moolenaar | 01b626c | 2013-06-16 22:49:14 +0200 | [diff] [blame] | 4578 | { |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4579 | static int lazy_init_done = FALSE; |
| 4580 | |
| 4581 | if (lazy_init_done) |
| 4582 | return; |
| 4583 | |
| 4584 | lazy_init_done = TRUE; |
| 4585 | ga_init2(&ga_users, sizeof(char_u *), 20); |
| 4586 | |
| 4587 | # if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H) |
| 4588 | { |
| 4589 | char_u* user; |
| 4590 | struct passwd* pw; |
| 4591 | |
| 4592 | setpwent(); |
| 4593 | while ((pw = getpwent()) != NULL) |
| 4594 | /* pw->pw_name shouldn't be NULL but just in case... */ |
| 4595 | if (pw->pw_name != NULL) |
| 4596 | { |
| 4597 | if (ga_grow(&ga_users, 1) == FAIL) |
| 4598 | break; |
| 4599 | user = vim_strsave((char_u*)pw->pw_name); |
| 4600 | if (user == NULL) |
| 4601 | break; |
| 4602 | ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user; |
| 4603 | } |
| 4604 | endpwent(); |
| 4605 | } |
| 4606 | # endif |
| 4607 | } |
| 4608 | |
| 4609 | /* |
| 4610 | * Function given to ExpandGeneric() to obtain an user names. |
| 4611 | */ |
| 4612 | char_u* |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4613 | get_users(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4614 | { |
| 4615 | init_users(); |
| 4616 | if (idx < ga_users.ga_len) |
| 4617 | return ((char_u **)ga_users.ga_data)[idx]; |
| 4618 | return NULL; |
| 4619 | } |
| 4620 | |
| 4621 | /* |
| 4622 | * Check whether name matches a user name. Return: |
| 4623 | * 0 if name does not match any user name. |
| 4624 | * 1 if name partially matches the beginning of a user name. |
| 4625 | * 2 is name fully matches a user name. |
| 4626 | */ |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4627 | int match_user(char_u* name) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4628 | { |
| 4629 | int i; |
| 4630 | int n = (int)STRLEN(name); |
| 4631 | int result = 0; |
| 4632 | |
| 4633 | init_users(); |
| 4634 | for (i = 0; i < ga_users.ga_len; i++) |
| 4635 | { |
| 4636 | if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0) |
| 4637 | return 2; /* full match */ |
| 4638 | if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0) |
| 4639 | result = 1; /* partial match */ |
| 4640 | } |
| 4641 | return result; |
| 4642 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4643 | #endif |
| 4644 | |
| 4645 | /* |
| 4646 | * Replace home directory by "~" in each space or comma separated file name in |
| 4647 | * 'src'. |
| 4648 | * If anything fails (except when out of space) dst equals src. |
| 4649 | */ |
| 4650 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4651 | home_replace( |
| 4652 | buf_T *buf, /* when not NULL, check for help files */ |
| 4653 | char_u *src, /* input file name */ |
| 4654 | char_u *dst, /* where to put the result */ |
| 4655 | int dstlen, /* maximum length of the result */ |
| 4656 | int one) /* if TRUE, only replace one file name, include |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4657 | spaces and commas in the file name. */ |
| 4658 | { |
| 4659 | size_t dirlen = 0, envlen = 0; |
| 4660 | size_t len; |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4661 | char_u *homedir_env, *homedir_env_orig; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4662 | char_u *p; |
| 4663 | |
| 4664 | if (src == NULL) |
| 4665 | { |
| 4666 | *dst = NUL; |
| 4667 | return; |
| 4668 | } |
| 4669 | |
| 4670 | /* |
| 4671 | * If the file is a help file, remove the path completely. |
| 4672 | */ |
| 4673 | if (buf != NULL && buf->b_help) |
| 4674 | { |
Bram Moolenaar | 0af2d32 | 2017-08-05 23:00:53 +0200 | [diff] [blame] | 4675 | vim_snprintf((char *)dst, dstlen, "%s", gettail(src)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4676 | return; |
| 4677 | } |
| 4678 | |
| 4679 | /* |
| 4680 | * We check both the value of the $HOME environment variable and the |
| 4681 | * "real" home directory. |
| 4682 | */ |
| 4683 | if (homedir != NULL) |
| 4684 | dirlen = STRLEN(homedir); |
| 4685 | |
| 4686 | #ifdef VMS |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4687 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4688 | #else |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4689 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); |
| 4690 | #endif |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 4691 | #ifdef WIN3264 |
| 4692 | if (homedir_env == NULL) |
| 4693 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE"); |
| 4694 | #endif |
Bram Moolenaar | bef4790 | 2012-07-06 16:49:40 +0200 | [diff] [blame] | 4695 | /* Empty is the same as not set. */ |
| 4696 | if (homedir_env != NULL && *homedir_env == NUL) |
| 4697 | homedir_env = NULL; |
| 4698 | |
Bram Moolenaar | e60c2e5 | 2013-06-05 19:35:38 +0200 | [diff] [blame] | 4699 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) |
Bram Moolenaar | bef4790 | 2012-07-06 16:49:40 +0200 | [diff] [blame] | 4700 | if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL) |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4701 | { |
| 4702 | int usedlen = 0; |
| 4703 | int flen; |
| 4704 | char_u *fbuf = NULL; |
| 4705 | |
| 4706 | flen = (int)STRLEN(homedir_env); |
Bram Moolenaar | d12f811 | 2012-06-20 17:56:09 +0200 | [diff] [blame] | 4707 | (void)modify_fname((char_u *)":p", &usedlen, |
| 4708 | &homedir_env, &fbuf, &flen); |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4709 | flen = (int)STRLEN(homedir_env); |
| 4710 | if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) |
| 4711 | /* Remove the trailing / that is added to a directory. */ |
| 4712 | homedir_env[flen - 1] = NUL; |
| 4713 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4714 | #endif |
| 4715 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4716 | if (homedir_env != NULL) |
| 4717 | envlen = STRLEN(homedir_env); |
| 4718 | |
| 4719 | if (!one) |
| 4720 | src = skipwhite(src); |
| 4721 | while (*src && dstlen > 0) |
| 4722 | { |
| 4723 | /* |
| 4724 | * Here we are at the beginning of a file name. |
| 4725 | * First, check to see if the beginning of the file name matches |
| 4726 | * $HOME or the "real" home directory. Check that there is a '/' |
| 4727 | * after the match (so that if e.g. the file is "/home/pieter/bla", |
| 4728 | * and the home directory is "/home/piet", the file does not end up |
| 4729 | * as "~er/bla" (which would seem to indicate the file "bla" in user |
| 4730 | * er's home directory)). |
| 4731 | */ |
| 4732 | p = homedir; |
| 4733 | len = dirlen; |
| 4734 | for (;;) |
| 4735 | { |
| 4736 | if ( len |
| 4737 | && fnamencmp(src, p, len) == 0 |
| 4738 | && (vim_ispathsep(src[len]) |
| 4739 | || (!one && (src[len] == ',' || src[len] == ' ')) |
| 4740 | || src[len] == NUL)) |
| 4741 | { |
| 4742 | src += len; |
| 4743 | if (--dstlen > 0) |
| 4744 | *dst++ = '~'; |
| 4745 | |
| 4746 | /* |
| 4747 | * If it's just the home directory, add "/". |
| 4748 | */ |
| 4749 | if (!vim_ispathsep(src[0]) && --dstlen > 0) |
| 4750 | *dst++ = '/'; |
| 4751 | break; |
| 4752 | } |
| 4753 | if (p == homedir_env) |
| 4754 | break; |
| 4755 | p = homedir_env; |
| 4756 | len = envlen; |
| 4757 | } |
| 4758 | |
| 4759 | /* if (!one) skip to separator: space or comma */ |
| 4760 | while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) |
| 4761 | *dst++ = *src++; |
| 4762 | /* skip separator */ |
| 4763 | while ((*src == ' ' || *src == ',') && --dstlen > 0) |
| 4764 | *dst++ = *src++; |
| 4765 | } |
| 4766 | /* if (dstlen == 0) out of space, what to do??? */ |
| 4767 | |
| 4768 | *dst = NUL; |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4769 | |
| 4770 | if (homedir_env != homedir_env_orig) |
| 4771 | vim_free(homedir_env); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | /* |
| 4775 | * Like home_replace, store the replaced string in allocated memory. |
| 4776 | * When something fails, NULL is returned. |
| 4777 | */ |
| 4778 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4779 | home_replace_save( |
| 4780 | buf_T *buf, /* when not NULL, check for help files */ |
| 4781 | char_u *src) /* input file name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4782 | { |
| 4783 | char_u *dst; |
| 4784 | unsigned len; |
| 4785 | |
| 4786 | len = 3; /* space for "~/" and trailing NUL */ |
| 4787 | if (src != NULL) /* just in case */ |
| 4788 | len += (unsigned)STRLEN(src); |
| 4789 | dst = alloc(len); |
| 4790 | if (dst != NULL) |
| 4791 | home_replace(buf, src, dst, len, TRUE); |
| 4792 | return dst; |
| 4793 | } |
| 4794 | |
| 4795 | /* |
| 4796 | * Compare two file names and return: |
| 4797 | * FPC_SAME if they both exist and are the same file. |
| 4798 | * FPC_SAMEX if they both don't exist and have the same file name. |
| 4799 | * FPC_DIFF if they both exist and are different files. |
| 4800 | * FPC_NOTX if they both don't exist. |
| 4801 | * FPC_DIFFX if one of them doesn't exist. |
| 4802 | * For the first name environment variables are expanded |
| 4803 | */ |
| 4804 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4805 | fullpathcmp( |
| 4806 | char_u *s1, |
| 4807 | char_u *s2, |
| 4808 | int checkname) /* when both don't exist, check file names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4809 | { |
| 4810 | #ifdef UNIX |
| 4811 | char_u exp1[MAXPATHL]; |
| 4812 | char_u full1[MAXPATHL]; |
| 4813 | char_u full2[MAXPATHL]; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4814 | stat_T st1, st2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4815 | int r1, r2; |
| 4816 | |
| 4817 | expand_env(s1, exp1, MAXPATHL); |
| 4818 | r1 = mch_stat((char *)exp1, &st1); |
| 4819 | r2 = mch_stat((char *)s2, &st2); |
| 4820 | if (r1 != 0 && r2 != 0) |
| 4821 | { |
| 4822 | /* if mch_stat() doesn't work, may compare the names */ |
| 4823 | if (checkname) |
| 4824 | { |
| 4825 | if (fnamecmp(exp1, s2) == 0) |
| 4826 | return FPC_SAMEX; |
| 4827 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 4828 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 4829 | if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) |
| 4830 | return FPC_SAMEX; |
| 4831 | } |
| 4832 | return FPC_NOTX; |
| 4833 | } |
| 4834 | if (r1 != 0 || r2 != 0) |
| 4835 | return FPC_DIFFX; |
| 4836 | if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) |
| 4837 | return FPC_SAME; |
| 4838 | return FPC_DIFF; |
| 4839 | #else |
| 4840 | char_u *exp1; /* expanded s1 */ |
| 4841 | char_u *full1; /* full path of s1 */ |
| 4842 | char_u *full2; /* full path of s2 */ |
| 4843 | int retval = FPC_DIFF; |
| 4844 | int r1, r2; |
| 4845 | |
| 4846 | /* allocate one buffer to store three paths (alloc()/free() is slow!) */ |
| 4847 | if ((exp1 = alloc(MAXPATHL * 3)) != NULL) |
| 4848 | { |
| 4849 | full1 = exp1 + MAXPATHL; |
| 4850 | full2 = full1 + MAXPATHL; |
| 4851 | |
| 4852 | expand_env(s1, exp1, MAXPATHL); |
| 4853 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 4854 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 4855 | |
| 4856 | /* If vim_FullName() fails, the file probably doesn't exist. */ |
| 4857 | if (r1 != OK && r2 != OK) |
| 4858 | { |
| 4859 | if (checkname && fnamecmp(exp1, s2) == 0) |
| 4860 | retval = FPC_SAMEX; |
| 4861 | else |
| 4862 | retval = FPC_NOTX; |
| 4863 | } |
| 4864 | else if (r1 != OK || r2 != OK) |
| 4865 | retval = FPC_DIFFX; |
| 4866 | else if (fnamecmp(full1, full2)) |
| 4867 | retval = FPC_DIFF; |
| 4868 | else |
| 4869 | retval = FPC_SAME; |
| 4870 | vim_free(exp1); |
| 4871 | } |
| 4872 | return retval; |
| 4873 | #endif |
| 4874 | } |
| 4875 | |
| 4876 | /* |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 4877 | * Get the tail of a path: the file name. |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 4878 | * 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] | 4879 | * Fail safe: never returns NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4880 | */ |
| 4881 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4882 | gettail(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4883 | { |
| 4884 | char_u *p1, *p2; |
| 4885 | |
| 4886 | if (fname == NULL) |
| 4887 | return (char_u *)""; |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 4888 | 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] | 4889 | { |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 4890 | if (vim_ispathsep_nocolon(*p2)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4891 | p1 = p2 + 1; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4892 | MB_PTR_ADV(p2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4893 | } |
| 4894 | return p1; |
| 4895 | } |
| 4896 | |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 4897 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 4898 | static char_u *gettail_dir(char_u *fname); |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 4899 | |
| 4900 | /* |
| 4901 | * Return the end of the directory name, on the first path |
| 4902 | * separator: |
| 4903 | * "/path/file", "/path/dir/", "/path//dir", "/file" |
| 4904 | * ^ ^ ^ ^ |
| 4905 | */ |
| 4906 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4907 | gettail_dir(char_u *fname) |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 4908 | { |
| 4909 | char_u *dir_end = fname; |
| 4910 | char_u *next_dir_end = fname; |
| 4911 | int look_for_sep = TRUE; |
| 4912 | char_u *p; |
| 4913 | |
| 4914 | for (p = fname; *p != NUL; ) |
| 4915 | { |
| 4916 | if (vim_ispathsep(*p)) |
| 4917 | { |
| 4918 | if (look_for_sep) |
| 4919 | { |
| 4920 | next_dir_end = p; |
| 4921 | look_for_sep = FALSE; |
| 4922 | } |
| 4923 | } |
| 4924 | else |
| 4925 | { |
| 4926 | if (!look_for_sep) |
| 4927 | dir_end = next_dir_end; |
| 4928 | look_for_sep = TRUE; |
| 4929 | } |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4930 | MB_PTR_ADV(p); |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 4931 | } |
| 4932 | return dir_end; |
| 4933 | } |
| 4934 | #endif |
| 4935 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4936 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4937 | * Get pointer to tail of "fname", including path separators. Putting a NUL |
| 4938 | * here leaves the directory name. Takes care of "c:/" and "//". |
| 4939 | * Always returns a valid pointer. |
| 4940 | */ |
| 4941 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4942 | gettail_sep(char_u *fname) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4943 | { |
| 4944 | char_u *p; |
| 4945 | char_u *t; |
| 4946 | |
| 4947 | p = get_past_head(fname); /* don't remove the '/' from "c:/file" */ |
| 4948 | t = gettail(fname); |
| 4949 | while (t > p && after_pathsep(fname, t)) |
| 4950 | --t; |
| 4951 | #ifdef VMS |
| 4952 | /* path separator is part of the path */ |
| 4953 | ++t; |
| 4954 | #endif |
| 4955 | return t; |
| 4956 | } |
| 4957 | |
| 4958 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4959 | * get the next path component (just after the next path separator). |
| 4960 | */ |
| 4961 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4962 | getnextcomp(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4963 | { |
| 4964 | while (*fname && !vim_ispathsep(*fname)) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4965 | MB_PTR_ADV(fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4966 | if (*fname) |
| 4967 | ++fname; |
| 4968 | return fname; |
| 4969 | } |
| 4970 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4971 | /* |
| 4972 | * Get a pointer to one character past the head of a path name. |
| 4973 | * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. |
| 4974 | * If there is no head, path is returned. |
| 4975 | */ |
| 4976 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4977 | get_past_head(char_u *path) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4978 | { |
| 4979 | char_u *retval; |
| 4980 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4981 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4982 | /* may skip "c:" */ |
| 4983 | if (isalpha(path[0]) && path[1] == ':') |
| 4984 | retval = path + 2; |
| 4985 | else |
| 4986 | retval = path; |
| 4987 | #else |
| 4988 | # if defined(AMIGA) |
| 4989 | /* may skip "label:" */ |
| 4990 | retval = vim_strchr(path, ':'); |
| 4991 | if (retval == NULL) |
| 4992 | retval = path; |
| 4993 | # else /* Unix */ |
| 4994 | retval = path; |
| 4995 | # endif |
| 4996 | #endif |
| 4997 | |
| 4998 | while (vim_ispathsep(*retval)) |
| 4999 | ++retval; |
| 5000 | |
| 5001 | return retval; |
| 5002 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5003 | |
| 5004 | /* |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5005 | * Return TRUE if 'c' is a path separator. |
| 5006 | * Note that for MS-Windows this includes the colon. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5007 | */ |
| 5008 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5009 | vim_ispathsep(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5010 | { |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5011 | #ifdef UNIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5012 | return (c == '/'); /* UNIX has ':' inside file names */ |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5013 | #else |
| 5014 | # ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5015 | return (c == ':' || c == '/' || c == '\\'); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5016 | # else |
| 5017 | # ifdef VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5018 | /* server"user passwd"::device:[full.path.name]fname.extension;version" */ |
| 5019 | return (c == ':' || c == '[' || c == ']' || c == '/' |
| 5020 | || c == '<' || c == '>' || c == '"' ); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5021 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5022 | return (c == ':' || c == '/'); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5023 | # endif /* VMS */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5024 | # endif |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5025 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5026 | } |
| 5027 | |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5028 | /* |
| 5029 | * Like vim_ispathsep(c), but exclude the colon for MS-Windows. |
| 5030 | */ |
| 5031 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5032 | vim_ispathsep_nocolon(int c) |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5033 | { |
| 5034 | return vim_ispathsep(c) |
| 5035 | #ifdef BACKSLASH_IN_FILENAME |
| 5036 | && c != ':' |
| 5037 | #endif |
| 5038 | ; |
| 5039 | } |
| 5040 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5041 | #if defined(FEAT_SEARCHPATH) || defined(PROTO) |
| 5042 | /* |
| 5043 | * return TRUE if 'c' is a path list separator. |
| 5044 | */ |
| 5045 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5046 | vim_ispathlistsep(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5047 | { |
| 5048 | #ifdef UNIX |
| 5049 | return (c == ':'); |
| 5050 | #else |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 5051 | return (c == ';'); /* might not be right for every system... */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5052 | #endif |
| 5053 | } |
| 5054 | #endif |
| 5055 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5056 | /* |
| 5057 | * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" |
| 5058 | * It's done in-place. |
| 5059 | */ |
| 5060 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5061 | shorten_dir(char_u *str) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5062 | { |
| 5063 | char_u *tail, *s, *d; |
| 5064 | int skip = FALSE; |
| 5065 | |
| 5066 | tail = gettail(str); |
| 5067 | d = str; |
| 5068 | for (s = str; ; ++s) |
| 5069 | { |
| 5070 | if (s >= tail) /* copy the whole tail */ |
| 5071 | { |
| 5072 | *d++ = *s; |
| 5073 | if (*s == NUL) |
| 5074 | break; |
| 5075 | } |
| 5076 | else if (vim_ispathsep(*s)) /* copy '/' and next char */ |
| 5077 | { |
| 5078 | *d++ = *s; |
| 5079 | skip = FALSE; |
| 5080 | } |
| 5081 | else if (!skip) |
| 5082 | { |
| 5083 | *d++ = *s; /* copy next char */ |
| 5084 | if (*s != '~' && *s != '.') /* and leading "~" and "." */ |
| 5085 | skip = TRUE; |
| 5086 | # ifdef FEAT_MBYTE |
| 5087 | if (has_mbyte) |
| 5088 | { |
| 5089 | int l = mb_ptr2len(s); |
| 5090 | |
| 5091 | while (--l > 0) |
Bram Moolenaar | b6baca5 | 2006-08-15 20:24:14 +0000 | [diff] [blame] | 5092 | *d++ = *++s; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5093 | } |
| 5094 | # endif |
| 5095 | } |
| 5096 | } |
| 5097 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5098 | |
Bram Moolenaar | 900b4d7 | 2005-12-12 22:05:50 +0000 | [diff] [blame] | 5099 | /* |
| 5100 | * Return TRUE if the directory of "fname" exists, FALSE otherwise. |
| 5101 | * Also returns TRUE if there is no directory name. |
| 5102 | * "fname" must be writable!. |
| 5103 | */ |
| 5104 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5105 | dir_of_file_exists(char_u *fname) |
Bram Moolenaar | 900b4d7 | 2005-12-12 22:05:50 +0000 | [diff] [blame] | 5106 | { |
| 5107 | char_u *p; |
| 5108 | int c; |
| 5109 | int retval; |
| 5110 | |
| 5111 | p = gettail_sep(fname); |
| 5112 | if (p == fname) |
| 5113 | return TRUE; |
| 5114 | c = *p; |
| 5115 | *p = NUL; |
| 5116 | retval = mch_isdir(fname); |
| 5117 | *p = c; |
| 5118 | return retval; |
| 5119 | } |
| 5120 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5121 | /* |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5122 | * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally |
| 5123 | * and deal with 'fileignorecase'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5124 | */ |
| 5125 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5126 | vim_fnamecmp(char_u *x, char_u *y) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5127 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5128 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5129 | return vim_fnamencmp(x, y, MAXPATHL); |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5130 | #else |
| 5131 | if (p_fic) |
| 5132 | return MB_STRICMP(x, y); |
| 5133 | return STRCMP(x, y); |
| 5134 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5135 | } |
| 5136 | |
| 5137 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5138 | vim_fnamencmp(char_u *x, char_u *y, size_t len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5139 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5140 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5141 | char_u *px = x; |
| 5142 | char_u *py = y; |
| 5143 | int cx = NUL; |
| 5144 | int cy = NUL; |
| 5145 | |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5146 | while (len > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5147 | { |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5148 | cx = PTR2CHAR(px); |
| 5149 | cy = PTR2CHAR(py); |
| 5150 | if (cx == NUL || cy == NUL |
| 5151 | || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy) |
| 5152 | && !(cx == '/' && cy == '\\') |
| 5153 | && !(cx == '\\' && cy == '/'))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5154 | break; |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5155 | len -= MB_PTR2LEN(px); |
| 5156 | px += MB_PTR2LEN(px); |
| 5157 | py += MB_PTR2LEN(py); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5158 | } |
| 5159 | if (len == 0) |
| 5160 | return 0; |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5161 | return (cx - cy); |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5162 | #else |
| 5163 | if (p_fic) |
| 5164 | return MB_STRNICMP(x, y, len); |
| 5165 | return STRNCMP(x, y, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5166 | #endif |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5167 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5168 | |
| 5169 | /* |
| 5170 | * Concatenate file names fname1 and fname2 into allocated memory. |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 5171 | * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5172 | */ |
| 5173 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5174 | concat_fnames(char_u *fname1, char_u *fname2, int sep) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5175 | { |
| 5176 | char_u *dest; |
| 5177 | |
| 5178 | dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3)); |
| 5179 | if (dest != NULL) |
| 5180 | { |
| 5181 | STRCPY(dest, fname1); |
| 5182 | if (sep) |
| 5183 | add_pathsep(dest); |
| 5184 | STRCAT(dest, fname2); |
| 5185 | } |
| 5186 | return dest; |
| 5187 | } |
| 5188 | |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5189 | /* |
| 5190 | * Concatenate two strings and return the result in allocated memory. |
| 5191 | * Returns NULL when out of memory. |
| 5192 | */ |
| 5193 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5194 | concat_str(char_u *str1, char_u *str2) |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5195 | { |
| 5196 | char_u *dest; |
| 5197 | size_t l = STRLEN(str1); |
| 5198 | |
| 5199 | dest = alloc((unsigned)(l + STRLEN(str2) + 1L)); |
| 5200 | if (dest != NULL) |
| 5201 | { |
| 5202 | STRCPY(dest, str1); |
| 5203 | STRCPY(dest + l, str2); |
| 5204 | } |
| 5205 | return dest; |
| 5206 | } |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5207 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5208 | /* |
| 5209 | * Add a path separator to a file name, unless it already ends in a path |
| 5210 | * separator. |
| 5211 | */ |
| 5212 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5213 | add_pathsep(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5214 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5215 | if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5216 | STRCAT(p, PATHSEPSTR); |
| 5217 | } |
| 5218 | |
| 5219 | /* |
| 5220 | * FullName_save - Make an allocated copy of a full file name. |
| 5221 | * Returns NULL when out of memory. |
| 5222 | */ |
| 5223 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5224 | FullName_save( |
| 5225 | char_u *fname, |
| 5226 | int force) /* force expansion, even when it already looks |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 5227 | * like a full path name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5228 | { |
| 5229 | char_u *buf; |
| 5230 | char_u *new_fname = NULL; |
| 5231 | |
| 5232 | if (fname == NULL) |
| 5233 | return NULL; |
| 5234 | |
| 5235 | buf = alloc((unsigned)MAXPATHL); |
| 5236 | if (buf != NULL) |
| 5237 | { |
| 5238 | if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) |
| 5239 | new_fname = vim_strsave(buf); |
| 5240 | else |
| 5241 | new_fname = vim_strsave(fname); |
| 5242 | vim_free(buf); |
| 5243 | } |
| 5244 | return new_fname; |
| 5245 | } |
| 5246 | |
| 5247 | #if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL) |
| 5248 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5249 | static char_u *skip_string(char_u *p); |
| 5250 | static pos_T *ind_find_start_comment(void); |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5251 | static pos_T *ind_find_start_CORS(linenr_T *is_raw); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5252 | static pos_T *find_start_rawstring(int ind_maxcomment); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5253 | |
| 5254 | /* |
| 5255 | * Find the start of a comment, not knowing if we are in a comment right now. |
| 5256 | * Search starts at w_cursor.lnum and goes backwards. |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5257 | * Return NULL when not inside a comment. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5258 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 5259 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5260 | ind_find_start_comment(void) /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 5261 | { |
| 5262 | return find_start_comment(curbuf->b_ind_maxcomment); |
| 5263 | } |
| 5264 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5265 | pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5266 | find_start_comment(int ind_maxcomment) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5267 | { |
| 5268 | pos_T *pos; |
| 5269 | char_u *line; |
| 5270 | char_u *p; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5271 | int cur_maxcomment = ind_maxcomment; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5272 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5273 | for (;;) |
| 5274 | { |
| 5275 | pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment); |
| 5276 | if (pos == NULL) |
| 5277 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5278 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5279 | /* |
| 5280 | * Check if the comment start we found is inside a string. |
| 5281 | * If it is then restrict the search to below this line and try again. |
| 5282 | */ |
| 5283 | line = ml_get(pos->lnum); |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 5284 | for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5285 | p = skip_string(p); |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 5286 | if ((colnr_T)(p - line) <= pos->col) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5287 | break; |
| 5288 | cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; |
| 5289 | if (cur_maxcomment <= 0) |
| 5290 | { |
| 5291 | pos = NULL; |
| 5292 | break; |
| 5293 | } |
| 5294 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5295 | return pos; |
| 5296 | } |
| 5297 | |
| 5298 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5299 | * Find the start of a comment or raw string, not knowing if we are in a |
| 5300 | * comment or raw string right now. |
| 5301 | * Search starts at w_cursor.lnum and goes backwards. |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5302 | * If is_raw is given and returns start of raw_string, sets it to true. |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5303 | * Return NULL when not inside a comment or raw string. |
| 5304 | * "CORS" -> Comment Or Raw String |
| 5305 | */ |
| 5306 | static pos_T * |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5307 | ind_find_start_CORS(linenr_T *is_raw) /* XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5308 | { |
Bram Moolenaar | 089af18 | 2015-10-07 11:41:49 +0200 | [diff] [blame] | 5309 | static pos_T comment_pos_copy; |
| 5310 | pos_T *comment_pos; |
| 5311 | pos_T *rs_pos; |
| 5312 | |
| 5313 | comment_pos = find_start_comment(curbuf->b_ind_maxcomment); |
| 5314 | if (comment_pos != NULL) |
| 5315 | { |
| 5316 | /* Need to make a copy of the static pos in findmatchlimit(), |
| 5317 | * calling find_start_rawstring() may change it. */ |
| 5318 | comment_pos_copy = *comment_pos; |
| 5319 | comment_pos = &comment_pos_copy; |
| 5320 | } |
| 5321 | rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment); |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5322 | |
| 5323 | /* If comment_pos is before rs_pos the raw string is inside the comment. |
| 5324 | * If rs_pos is before comment_pos the comment is inside the raw string. */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 5325 | if (comment_pos == NULL || (rs_pos != NULL |
| 5326 | && LT_POS(*rs_pos, *comment_pos))) |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5327 | { |
| 5328 | if (is_raw != NULL && rs_pos != NULL) |
| 5329 | *is_raw = rs_pos->lnum; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5330 | return rs_pos; |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5331 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5332 | return comment_pos; |
| 5333 | } |
| 5334 | |
| 5335 | /* |
| 5336 | * Find the start of a raw string, not knowing if we are in one right now. |
| 5337 | * Search starts at w_cursor.lnum and goes backwards. |
| 5338 | * Return NULL when not inside a raw string. |
| 5339 | */ |
| 5340 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5341 | find_start_rawstring(int ind_maxcomment) /* XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5342 | { |
| 5343 | pos_T *pos; |
| 5344 | char_u *line; |
| 5345 | char_u *p; |
| 5346 | int cur_maxcomment = ind_maxcomment; |
| 5347 | |
| 5348 | for (;;) |
| 5349 | { |
| 5350 | pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment); |
| 5351 | if (pos == NULL) |
| 5352 | break; |
| 5353 | |
| 5354 | /* |
| 5355 | * Check if the raw string start we found is inside a string. |
| 5356 | * If it is then restrict the search to below this line and try again. |
| 5357 | */ |
| 5358 | line = ml_get(pos->lnum); |
| 5359 | for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) |
| 5360 | p = skip_string(p); |
| 5361 | if ((colnr_T)(p - line) <= pos->col) |
| 5362 | break; |
| 5363 | cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; |
| 5364 | if (cur_maxcomment <= 0) |
| 5365 | { |
| 5366 | pos = NULL; |
| 5367 | break; |
| 5368 | } |
| 5369 | } |
| 5370 | return pos; |
| 5371 | } |
| 5372 | |
| 5373 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5374 | * Skip to the end of a "string" and a 'c' character. |
| 5375 | * If there is no string or character, return argument unmodified. |
| 5376 | */ |
| 5377 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5378 | skip_string(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5379 | { |
| 5380 | int i; |
| 5381 | |
| 5382 | /* |
| 5383 | * We loop, because strings may be concatenated: "date""time". |
| 5384 | */ |
| 5385 | for ( ; ; ++p) |
| 5386 | { |
| 5387 | if (p[0] == '\'') /* 'c' or '\n' or '\000' */ |
| 5388 | { |
| 5389 | if (!p[1]) /* ' at end of line */ |
| 5390 | break; |
| 5391 | i = 2; |
| 5392 | if (p[1] == '\\') /* '\n' or '\000' */ |
| 5393 | { |
| 5394 | ++i; |
| 5395 | while (vim_isdigit(p[i - 1])) /* '\000' */ |
| 5396 | ++i; |
| 5397 | } |
| 5398 | if (p[i] == '\'') /* check for trailing ' */ |
| 5399 | { |
| 5400 | p += i; |
| 5401 | continue; |
| 5402 | } |
| 5403 | } |
| 5404 | else if (p[0] == '"') /* start of string */ |
| 5405 | { |
| 5406 | for (++p; p[0]; ++p) |
| 5407 | { |
| 5408 | if (p[0] == '\\' && p[1] != NUL) |
| 5409 | ++p; |
| 5410 | else if (p[0] == '"') /* end of string */ |
| 5411 | break; |
| 5412 | } |
| 5413 | if (p[0] == '"') |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5414 | continue; /* continue for another string */ |
| 5415 | } |
| 5416 | else if (p[0] == 'R' && p[1] == '"') |
| 5417 | { |
| 5418 | /* Raw string: R"[delim](...)[delim]" */ |
| 5419 | char_u *delim = p + 2; |
| 5420 | char_u *paren = vim_strchr(delim, '('); |
| 5421 | |
| 5422 | if (paren != NULL) |
| 5423 | { |
| 5424 | size_t delim_len = paren - delim; |
| 5425 | |
| 5426 | for (p += 3; *p; ++p) |
| 5427 | if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0 |
| 5428 | && p[delim_len + 1] == '"') |
| 5429 | { |
| 5430 | p += delim_len + 1; |
| 5431 | break; |
| 5432 | } |
| 5433 | if (p[0] == '"') |
| 5434 | continue; /* continue for another string */ |
| 5435 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5436 | } |
| 5437 | break; /* no string found */ |
| 5438 | } |
| 5439 | if (!*p) |
| 5440 | --p; /* backup from NUL */ |
| 5441 | return p; |
| 5442 | } |
| 5443 | #endif /* FEAT_CINDENT || FEAT_SYN_HL */ |
| 5444 | |
| 5445 | #if defined(FEAT_CINDENT) || defined(PROTO) |
| 5446 | |
| 5447 | /* |
| 5448 | * Do C or expression indenting on the current line. |
| 5449 | */ |
| 5450 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5451 | do_c_expr_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5452 | { |
| 5453 | # ifdef FEAT_EVAL |
| 5454 | if (*curbuf->b_p_inde != NUL) |
| 5455 | fixthisline(get_expr_indent); |
| 5456 | else |
| 5457 | # endif |
| 5458 | fixthisline(get_c_indent); |
| 5459 | } |
| 5460 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 5461 | /* Find result cache for cpp_baseclass */ |
| 5462 | typedef struct { |
| 5463 | int found; |
| 5464 | lpos_T lpos; |
| 5465 | } cpp_baseclass_cache_T; |
| 5466 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5467 | /* |
| 5468 | * Functions for C-indenting. |
| 5469 | * Most of this originally comes from Eric Fischer. |
| 5470 | */ |
| 5471 | /* |
| 5472 | * Below "XXX" means that this function may unlock the current line. |
| 5473 | */ |
| 5474 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5475 | static char_u *cin_skipcomment(char_u *); |
| 5476 | static int cin_nocode(char_u *); |
| 5477 | static pos_T *find_line_comment(void); |
| 5478 | static int cin_has_js_key(char_u *text); |
| 5479 | static int cin_islabel_skip(char_u **); |
| 5480 | static int cin_isdefault(char_u *); |
| 5481 | static char_u *after_label(char_u *l); |
| 5482 | static int get_indent_nolabel(linenr_T lnum); |
| 5483 | static int skip_label(linenr_T, char_u **pp); |
| 5484 | static int cin_first_id_amount(void); |
| 5485 | static int cin_get_equal_amount(linenr_T lnum); |
| 5486 | static int cin_ispreproc(char_u *); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5487 | static int cin_iscomment(char_u *); |
| 5488 | static int cin_islinecomment(char_u *); |
| 5489 | static int cin_isterminated(char_u *, int, int); |
| 5490 | static int cin_isinit(void); |
| 5491 | static int cin_isfuncdecl(char_u **, linenr_T, linenr_T); |
| 5492 | static int cin_isif(char_u *); |
| 5493 | static int cin_iselse(char_u *); |
| 5494 | static int cin_isdo(char_u *); |
| 5495 | static int cin_iswhileofdo(char_u *, linenr_T); |
| 5496 | static int cin_is_if_for_while_before_offset(char_u *line, int *poffset); |
| 5497 | static int cin_iswhileofdo_end(int terminated); |
| 5498 | static int cin_isbreak(char_u *); |
| 5499 | static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached); |
| 5500 | static int get_baseclass_amount(int col); |
| 5501 | static int cin_ends_in(char_u *, char_u *, char_u *); |
| 5502 | static int cin_starts_with(char_u *s, char *word); |
| 5503 | static int cin_skip2pos(pos_T *trypos); |
| 5504 | static pos_T *find_start_brace(void); |
| 5505 | static pos_T *find_match_paren(int); |
| 5506 | static pos_T *find_match_char(int c, int ind_maxparen); |
| 5507 | static int corr_ind_maxparen(pos_T *startpos); |
| 5508 | static int find_last_paren(char_u *l, int start, int end); |
| 5509 | static int find_match(int lookfor, linenr_T ourscope); |
| 5510 | static int cin_is_cpp_namespace(char_u *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5511 | |
| 5512 | /* |
| 5513 | * Skip over white space and C comments within the line. |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5514 | * Also skip over Perl/shell comments if desired. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5515 | */ |
| 5516 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5517 | cin_skipcomment(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5518 | { |
| 5519 | while (*s) |
| 5520 | { |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5521 | char_u *prev_s = s; |
| 5522 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5523 | s = skipwhite(s); |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5524 | |
| 5525 | /* Perl/shell # comment comment continues until eol. Require a space |
| 5526 | * before # to avoid recognizing $#array. */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 5527 | if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#') |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5528 | { |
| 5529 | s += STRLEN(s); |
| 5530 | break; |
| 5531 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5532 | if (*s != '/') |
| 5533 | break; |
| 5534 | ++s; |
| 5535 | if (*s == '/') /* slash-slash comment continues till eol */ |
| 5536 | { |
| 5537 | s += STRLEN(s); |
| 5538 | break; |
| 5539 | } |
| 5540 | if (*s != '*') |
| 5541 | break; |
| 5542 | for (++s; *s; ++s) /* skip slash-star comment */ |
| 5543 | if (s[0] == '*' && s[1] == '/') |
| 5544 | { |
| 5545 | s += 2; |
| 5546 | break; |
| 5547 | } |
| 5548 | } |
| 5549 | return s; |
| 5550 | } |
| 5551 | |
| 5552 | /* |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 5553 | * 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] | 5554 | * not considered code. |
| 5555 | */ |
| 5556 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5557 | cin_nocode(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5558 | { |
| 5559 | return *cin_skipcomment(s) == NUL; |
| 5560 | } |
| 5561 | |
| 5562 | /* |
| 5563 | * Check previous lines for a "//" line comment, skipping over blank lines. |
| 5564 | */ |
| 5565 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5566 | find_line_comment(void) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5567 | { |
| 5568 | static pos_T pos; |
| 5569 | char_u *line; |
| 5570 | char_u *p; |
| 5571 | |
| 5572 | pos = curwin->w_cursor; |
| 5573 | while (--pos.lnum > 0) |
| 5574 | { |
| 5575 | line = ml_get(pos.lnum); |
| 5576 | p = skipwhite(line); |
| 5577 | if (cin_islinecomment(p)) |
| 5578 | { |
| 5579 | pos.col = (int)(p - line); |
| 5580 | return &pos; |
| 5581 | } |
| 5582 | if (*p != NUL) |
| 5583 | break; |
| 5584 | } |
| 5585 | return NULL; |
| 5586 | } |
| 5587 | |
| 5588 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5589 | * Return TRUE if "text" starts with "key:". |
| 5590 | */ |
| 5591 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5592 | cin_has_js_key(char_u *text) |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5593 | { |
| 5594 | char_u *s = skipwhite(text); |
Bram Moolenaar | ece29e8 | 2014-08-06 12:49:18 +0200 | [diff] [blame] | 5595 | int quote = -1; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5596 | |
| 5597 | if (*s == '\'' || *s == '"') |
| 5598 | { |
| 5599 | /* can be 'key': or "key": */ |
| 5600 | quote = *s; |
| 5601 | ++s; |
| 5602 | } |
| 5603 | if (!vim_isIDc(*s)) /* need at least one ID character */ |
| 5604 | return FALSE; |
| 5605 | |
| 5606 | while (vim_isIDc(*s)) |
| 5607 | ++s; |
| 5608 | if (*s == quote) |
| 5609 | ++s; |
| 5610 | |
| 5611 | s = cin_skipcomment(s); |
| 5612 | |
| 5613 | /* "::" is not a label, it's C++ */ |
| 5614 | return (*s == ':' && s[1] != ':'); |
| 5615 | } |
| 5616 | |
| 5617 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5618 | * Check if string matches "label:"; move to character after ':' if true. |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5619 | * "*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] | 5620 | */ |
| 5621 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5622 | cin_islabel_skip(char_u **s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5623 | { |
| 5624 | if (!vim_isIDc(**s)) /* need at least one ID character */ |
| 5625 | return FALSE; |
| 5626 | |
| 5627 | while (vim_isIDc(**s)) |
| 5628 | (*s)++; |
| 5629 | |
| 5630 | *s = cin_skipcomment(*s); |
| 5631 | |
| 5632 | /* "::" is not a label, it's C++ */ |
| 5633 | return (**s == ':' && *++*s != ':'); |
| 5634 | } |
| 5635 | |
| 5636 | /* |
| 5637 | * Recognize a label: "label:". |
| 5638 | * Note: curwin->w_cursor must be where we are looking for the label. |
| 5639 | */ |
| 5640 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5641 | cin_islabel(void) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5642 | { |
| 5643 | char_u *s; |
| 5644 | |
| 5645 | s = cin_skipcomment(ml_get_curline()); |
| 5646 | |
| 5647 | /* |
| 5648 | * Exclude "default" from labels, since it should be indented |
| 5649 | * like a switch label. Same for C++ scope declarations. |
| 5650 | */ |
| 5651 | if (cin_isdefault(s)) |
| 5652 | return FALSE; |
| 5653 | if (cin_isscopedecl(s)) |
| 5654 | return FALSE; |
| 5655 | |
| 5656 | if (cin_islabel_skip(&s)) |
| 5657 | { |
| 5658 | /* |
| 5659 | * Only accept a label if the previous line is terminated or is a case |
| 5660 | * label. |
| 5661 | */ |
| 5662 | pos_T cursor_save; |
| 5663 | pos_T *trypos; |
| 5664 | char_u *line; |
| 5665 | |
| 5666 | cursor_save = curwin->w_cursor; |
| 5667 | while (curwin->w_cursor.lnum > 1) |
| 5668 | { |
| 5669 | --curwin->w_cursor.lnum; |
| 5670 | |
| 5671 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5672 | * If we're in a comment or raw string now, skip to the start of |
| 5673 | * it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5674 | */ |
| 5675 | curwin->w_cursor.col = 0; |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5676 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5677 | curwin->w_cursor = *trypos; |
| 5678 | |
| 5679 | line = ml_get_curline(); |
| 5680 | if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */ |
| 5681 | continue; |
| 5682 | if (*(line = cin_skipcomment(line)) == NUL) |
| 5683 | continue; |
| 5684 | |
| 5685 | curwin->w_cursor = cursor_save; |
| 5686 | if (cin_isterminated(line, TRUE, FALSE) |
| 5687 | || cin_isscopedecl(line) |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5688 | || cin_iscase(line, TRUE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5689 | || (cin_islabel_skip(&line) && cin_nocode(line))) |
| 5690 | return TRUE; |
| 5691 | return FALSE; |
| 5692 | } |
| 5693 | curwin->w_cursor = cursor_save; |
| 5694 | return TRUE; /* label at start of file??? */ |
| 5695 | } |
| 5696 | return FALSE; |
| 5697 | } |
| 5698 | |
| 5699 | /* |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5700 | * Recognize structure initialization and enumerations: |
| 5701 | * "[typedef] [static|public|protected|private] enum" |
| 5702 | * "[typedef] [static|public|protected|private] = {" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5703 | */ |
| 5704 | static int |
| 5705 | cin_isinit(void) |
| 5706 | { |
| 5707 | char_u *s; |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5708 | static char *skip[] = {"static", "public", "protected", "private"}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5709 | |
| 5710 | s = cin_skipcomment(ml_get_curline()); |
| 5711 | |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5712 | if (cin_starts_with(s, "typedef")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5713 | s = cin_skipcomment(s + 7); |
| 5714 | |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5715 | for (;;) |
| 5716 | { |
| 5717 | int i, l; |
Bram Moolenaar | a528565 | 2011-12-14 20:05:21 +0100 | [diff] [blame] | 5718 | |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5719 | for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i) |
| 5720 | { |
Bram Moolenaar | b3cb982 | 2013-03-13 17:01:52 +0100 | [diff] [blame] | 5721 | l = (int)strlen(skip[i]); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5722 | if (cin_starts_with(s, skip[i])) |
| 5723 | { |
| 5724 | s = cin_skipcomment(s + l); |
| 5725 | l = 0; |
| 5726 | break; |
| 5727 | } |
| 5728 | } |
| 5729 | if (l != 0) |
| 5730 | break; |
| 5731 | } |
| 5732 | |
| 5733 | if (cin_starts_with(s, "enum")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5734 | return TRUE; |
| 5735 | |
| 5736 | if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) |
| 5737 | return TRUE; |
| 5738 | |
| 5739 | return FALSE; |
| 5740 | } |
| 5741 | |
| 5742 | /* |
| 5743 | * Recognize a switch label: "case .*:" or "default:". |
| 5744 | */ |
| 5745 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5746 | cin_iscase( |
| 5747 | char_u *s, |
| 5748 | int strict) /* Allow relaxed check of case statement for JS */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5749 | { |
| 5750 | s = cin_skipcomment(s); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5751 | if (cin_starts_with(s, "case")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5752 | { |
| 5753 | for (s += 4; *s; ++s) |
| 5754 | { |
| 5755 | s = cin_skipcomment(s); |
| 5756 | if (*s == ':') |
| 5757 | { |
| 5758 | if (s[1] == ':') /* skip over "::" for C++ */ |
| 5759 | ++s; |
| 5760 | else |
| 5761 | return TRUE; |
| 5762 | } |
| 5763 | if (*s == '\'' && s[1] && s[2] == '\'') |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5764 | s += 2; /* skip over ':' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5765 | else if (*s == '/' && (s[1] == '*' || s[1] == '/')) |
| 5766 | return FALSE; /* stop at comment */ |
| 5767 | else if (*s == '"') |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5768 | { |
| 5769 | /* JS etc. */ |
| 5770 | if (strict) |
| 5771 | return FALSE; /* stop at string */ |
| 5772 | else |
| 5773 | return TRUE; |
| 5774 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5775 | } |
| 5776 | return FALSE; |
| 5777 | } |
| 5778 | |
| 5779 | if (cin_isdefault(s)) |
| 5780 | return TRUE; |
| 5781 | return FALSE; |
| 5782 | } |
| 5783 | |
| 5784 | /* |
| 5785 | * Recognize a "default" switch label. |
| 5786 | */ |
| 5787 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5788 | cin_isdefault(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5789 | { |
| 5790 | return (STRNCMP(s, "default", 7) == 0 |
| 5791 | && *(s = cin_skipcomment(s + 7)) == ':' |
| 5792 | && s[1] != ':'); |
| 5793 | } |
| 5794 | |
| 5795 | /* |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 5796 | * Recognize a "public/private/protected" scope declaration label. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5797 | */ |
| 5798 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5799 | cin_isscopedecl(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5800 | { |
| 5801 | int i; |
| 5802 | |
| 5803 | s = cin_skipcomment(s); |
| 5804 | if (STRNCMP(s, "public", 6) == 0) |
| 5805 | i = 6; |
| 5806 | else if (STRNCMP(s, "protected", 9) == 0) |
| 5807 | i = 9; |
| 5808 | else if (STRNCMP(s, "private", 7) == 0) |
| 5809 | i = 7; |
| 5810 | else |
| 5811 | return FALSE; |
| 5812 | return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':'); |
| 5813 | } |
| 5814 | |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 5815 | /* Maximum number of lines to search back for a "namespace" line. */ |
| 5816 | #define FIND_NAMESPACE_LIM 20 |
| 5817 | |
| 5818 | /* |
| 5819 | * Recognize a "namespace" scope declaration. |
| 5820 | */ |
| 5821 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5822 | cin_is_cpp_namespace(char_u *s) |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 5823 | { |
| 5824 | char_u *p; |
| 5825 | int has_name = FALSE; |
Bram Moolenaar | ca8b8d6 | 2016-11-17 21:30:27 +0100 | [diff] [blame] | 5826 | int has_name_start = FALSE; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 5827 | |
| 5828 | s = cin_skipcomment(s); |
| 5829 | if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9]))) |
| 5830 | { |
| 5831 | p = cin_skipcomment(skipwhite(s + 9)); |
| 5832 | while (*p != NUL) |
| 5833 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 5834 | if (VIM_ISWHITE(*p)) |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 5835 | { |
| 5836 | has_name = TRUE; /* found end of a name */ |
| 5837 | p = cin_skipcomment(skipwhite(p)); |
| 5838 | } |
| 5839 | else if (*p == '{') |
| 5840 | { |
| 5841 | break; |
| 5842 | } |
| 5843 | else if (vim_iswordc(*p)) |
| 5844 | { |
Bram Moolenaar | ca8b8d6 | 2016-11-17 21:30:27 +0100 | [diff] [blame] | 5845 | has_name_start = TRUE; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 5846 | if (has_name) |
| 5847 | return FALSE; /* word character after skipping past name */ |
| 5848 | ++p; |
| 5849 | } |
Bram Moolenaar | ca8b8d6 | 2016-11-17 21:30:27 +0100 | [diff] [blame] | 5850 | else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2])) |
| 5851 | { |
| 5852 | if (!has_name_start || has_name) |
| 5853 | return FALSE; |
| 5854 | /* C++ 17 nested namespace */ |
| 5855 | p += 3; |
| 5856 | } |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 5857 | else |
| 5858 | { |
| 5859 | return FALSE; |
| 5860 | } |
| 5861 | } |
| 5862 | return TRUE; |
| 5863 | } |
| 5864 | return FALSE; |
| 5865 | } |
| 5866 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5867 | /* |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 5868 | * Recognize a `extern "C"` or `extern "C++"` linkage specifications. |
| 5869 | */ |
| 5870 | static int |
| 5871 | cin_is_cpp_extern_c(char_u *s) |
| 5872 | { |
| 5873 | char_u *p; |
| 5874 | int has_string_literal = FALSE; |
| 5875 | |
| 5876 | s = cin_skipcomment(s); |
| 5877 | if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6]))) |
| 5878 | { |
| 5879 | p = cin_skipcomment(skipwhite(s + 6)); |
| 5880 | while (*p != NUL) |
| 5881 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 5882 | if (VIM_ISWHITE(*p)) |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 5883 | { |
| 5884 | p = cin_skipcomment(skipwhite(p)); |
| 5885 | } |
| 5886 | else if (*p == '{') |
| 5887 | { |
| 5888 | break; |
| 5889 | } |
| 5890 | else if (p[0] == '"' && p[1] == 'C' && p[2] == '"') |
| 5891 | { |
| 5892 | if (has_string_literal) |
| 5893 | return FALSE; |
| 5894 | has_string_literal = TRUE; |
| 5895 | p += 3; |
| 5896 | } |
| 5897 | else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+' |
| 5898 | && p[4] == '"') |
| 5899 | { |
| 5900 | if (has_string_literal) |
| 5901 | return FALSE; |
| 5902 | has_string_literal = TRUE; |
| 5903 | p += 5; |
| 5904 | } |
| 5905 | else |
| 5906 | { |
| 5907 | return FALSE; |
| 5908 | } |
| 5909 | } |
| 5910 | return has_string_literal ? TRUE : FALSE; |
| 5911 | } |
| 5912 | return FALSE; |
| 5913 | } |
| 5914 | |
| 5915 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5916 | * Return a pointer to the first non-empty non-comment character after a ':'. |
| 5917 | * Return NULL if not found. |
| 5918 | * case 234: a = b; |
| 5919 | * ^ |
| 5920 | */ |
| 5921 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5922 | after_label(char_u *l) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5923 | { |
| 5924 | for ( ; *l; ++l) |
| 5925 | { |
| 5926 | if (*l == ':') |
| 5927 | { |
| 5928 | if (l[1] == ':') /* skip over "::" for C++ */ |
| 5929 | ++l; |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5930 | else if (!cin_iscase(l + 1, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5931 | break; |
| 5932 | } |
| 5933 | else if (*l == '\'' && l[1] && l[2] == '\'') |
| 5934 | l += 2; /* skip over 'x' */ |
| 5935 | } |
| 5936 | if (*l == NUL) |
| 5937 | return NULL; |
| 5938 | l = cin_skipcomment(l + 1); |
| 5939 | if (*l == NUL) |
| 5940 | return NULL; |
| 5941 | return l; |
| 5942 | } |
| 5943 | |
| 5944 | /* |
| 5945 | * Get indent of line "lnum", skipping a label. |
| 5946 | * Return 0 if there is nothing after the label. |
| 5947 | */ |
| 5948 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5949 | get_indent_nolabel (linenr_T lnum) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5950 | { |
| 5951 | char_u *l; |
| 5952 | pos_T fp; |
| 5953 | colnr_T col; |
| 5954 | char_u *p; |
| 5955 | |
| 5956 | l = ml_get(lnum); |
| 5957 | p = after_label(l); |
| 5958 | if (p == NULL) |
| 5959 | return 0; |
| 5960 | |
| 5961 | fp.col = (colnr_T)(p - l); |
| 5962 | fp.lnum = lnum; |
| 5963 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 5964 | return (int)col; |
| 5965 | } |
| 5966 | |
| 5967 | /* |
| 5968 | * Find indent for line "lnum", ignoring any case or jump label. |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5969 | * Also return a pointer to the text (after the label) in "pp". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5970 | * label: if (asdf && asdfasdf) |
| 5971 | * ^ |
| 5972 | */ |
| 5973 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5974 | skip_label(linenr_T lnum, char_u **pp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5975 | { |
| 5976 | char_u *l; |
| 5977 | int amount; |
| 5978 | pos_T cursor_save; |
| 5979 | |
| 5980 | cursor_save = curwin->w_cursor; |
| 5981 | curwin->w_cursor.lnum = lnum; |
| 5982 | l = ml_get_curline(); |
| 5983 | /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 5984 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5985 | { |
| 5986 | amount = get_indent_nolabel(lnum); |
| 5987 | l = after_label(ml_get_curline()); |
| 5988 | if (l == NULL) /* just in case */ |
| 5989 | l = ml_get_curline(); |
| 5990 | } |
| 5991 | else |
| 5992 | { |
| 5993 | amount = get_indent(); |
| 5994 | l = ml_get_curline(); |
| 5995 | } |
| 5996 | *pp = l; |
| 5997 | |
| 5998 | curwin->w_cursor = cursor_save; |
| 5999 | return amount; |
| 6000 | } |
| 6001 | |
| 6002 | /* |
| 6003 | * Return the indent of the first variable name after a type in a declaration. |
| 6004 | * int a, indent of "a" |
| 6005 | * static struct foo b, indent of "b" |
| 6006 | * enum bla c, indent of "c" |
| 6007 | * Returns zero when it doesn't look like a declaration. |
| 6008 | */ |
| 6009 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6010 | cin_first_id_amount(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6011 | { |
| 6012 | char_u *line, *p, *s; |
| 6013 | int len; |
| 6014 | pos_T fp; |
| 6015 | colnr_T col; |
| 6016 | |
| 6017 | line = ml_get_curline(); |
| 6018 | p = skipwhite(line); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6019 | len = (int)(skiptowhite(p) - p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6020 | if (len == 6 && STRNCMP(p, "static", 6) == 0) |
| 6021 | { |
| 6022 | p = skipwhite(p + 6); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 6023 | len = (int)(skiptowhite(p) - p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6024 | } |
| 6025 | if (len == 6 && STRNCMP(p, "struct", 6) == 0) |
| 6026 | p = skipwhite(p + 6); |
| 6027 | else if (len == 4 && STRNCMP(p, "enum", 4) == 0) |
| 6028 | p = skipwhite(p + 4); |
| 6029 | else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0) |
| 6030 | || (len == 6 && STRNCMP(p, "signed", 6) == 0)) |
| 6031 | { |
| 6032 | s = skipwhite(p + len); |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6033 | if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3])) |
| 6034 | || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4])) |
| 6035 | || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5])) |
| 6036 | || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4]))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6037 | p = s; |
| 6038 | } |
| 6039 | for (len = 0; vim_isIDc(p[len]); ++len) |
| 6040 | ; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6041 | if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6042 | return 0; |
| 6043 | |
| 6044 | p = skipwhite(p + len); |
| 6045 | fp.lnum = curwin->w_cursor.lnum; |
| 6046 | fp.col = (colnr_T)(p - line); |
| 6047 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 6048 | return (int)col; |
| 6049 | } |
| 6050 | |
| 6051 | /* |
| 6052 | * Return the indent of the first non-blank after an equal sign. |
| 6053 | * char *foo = "here"; |
| 6054 | * Return zero if no (useful) equal sign found. |
| 6055 | * Return -1 if the line above "lnum" ends in a backslash. |
| 6056 | * foo = "asdf\ |
| 6057 | * asdf\ |
| 6058 | * here"; |
| 6059 | */ |
| 6060 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6061 | cin_get_equal_amount(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6062 | { |
| 6063 | char_u *line; |
| 6064 | char_u *s; |
| 6065 | colnr_T col; |
| 6066 | pos_T fp; |
| 6067 | |
| 6068 | if (lnum > 1) |
| 6069 | { |
| 6070 | line = ml_get(lnum - 1); |
| 6071 | if (*line != NUL && line[STRLEN(line) - 1] == '\\') |
| 6072 | return -1; |
| 6073 | } |
| 6074 | |
| 6075 | line = s = ml_get(lnum); |
| 6076 | while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL) |
| 6077 | { |
| 6078 | if (cin_iscomment(s)) /* ignore comments */ |
| 6079 | s = cin_skipcomment(s); |
| 6080 | else |
| 6081 | ++s; |
| 6082 | } |
| 6083 | if (*s != '=') |
| 6084 | return 0; |
| 6085 | |
| 6086 | s = skipwhite(s + 1); |
| 6087 | if (cin_nocode(s)) |
| 6088 | return 0; |
| 6089 | |
| 6090 | if (*s == '"') /* nice alignment for continued strings */ |
| 6091 | ++s; |
| 6092 | |
| 6093 | fp.lnum = lnum; |
| 6094 | fp.col = (colnr_T)(s - line); |
| 6095 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 6096 | return (int)col; |
| 6097 | } |
| 6098 | |
| 6099 | /* |
| 6100 | * Recognize a preprocessor statement: Any line that starts with '#'. |
| 6101 | */ |
| 6102 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6103 | cin_ispreproc(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6104 | { |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6105 | if (*skipwhite(s) == '#') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6106 | return TRUE; |
| 6107 | return FALSE; |
| 6108 | } |
| 6109 | |
| 6110 | /* |
| 6111 | * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a |
| 6112 | * continuation line of a preprocessor statement. Decrease "*lnump" to the |
| 6113 | * start and return the line in "*pp". |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6114 | * Put the amount of indent in "*amount". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6115 | */ |
| 6116 | static int |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6117 | cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6118 | { |
| 6119 | char_u *line = *pp; |
| 6120 | linenr_T lnum = *lnump; |
| 6121 | int retval = FALSE; |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6122 | int candidate_amount = *amount; |
| 6123 | |
| 6124 | if (*line != NUL && line[STRLEN(line) - 1] == '\\') |
| 6125 | candidate_amount = get_indent_lnum(lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6126 | |
Bram Moolenaar | d8e9bb2 | 2005-07-09 21:14:46 +0000 | [diff] [blame] | 6127 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6128 | { |
| 6129 | if (cin_ispreproc(line)) |
| 6130 | { |
| 6131 | retval = TRUE; |
| 6132 | *lnump = lnum; |
| 6133 | break; |
| 6134 | } |
| 6135 | if (lnum == 1) |
| 6136 | break; |
| 6137 | line = ml_get(--lnum); |
| 6138 | if (*line == NUL || line[STRLEN(line) - 1] != '\\') |
| 6139 | break; |
| 6140 | } |
| 6141 | |
| 6142 | if (lnum != *lnump) |
| 6143 | *pp = ml_get(*lnump); |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6144 | if (retval) |
| 6145 | *amount = candidate_amount; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6146 | return retval; |
| 6147 | } |
| 6148 | |
| 6149 | /* |
| 6150 | * Recognize the start of a C or C++ comment. |
| 6151 | */ |
| 6152 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6153 | cin_iscomment(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6154 | { |
| 6155 | return (p[0] == '/' && (p[1] == '*' || p[1] == '/')); |
| 6156 | } |
| 6157 | |
| 6158 | /* |
| 6159 | * Recognize the start of a "//" comment. |
| 6160 | */ |
| 6161 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6162 | cin_islinecomment(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6163 | { |
| 6164 | return (p[0] == '/' && p[1] == '/'); |
| 6165 | } |
| 6166 | |
| 6167 | /* |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6168 | * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or |
| 6169 | * '}'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6170 | * Don't consider "} else" a terminated line. |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6171 | * If a line begins with an "else", only consider it terminated if no unmatched |
| 6172 | * opening braces follow (handle "else { foo();" correctly). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6173 | * Return the character terminating the line (ending char's have precedence if |
| 6174 | * both apply in order to determine initializations). |
| 6175 | */ |
| 6176 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6177 | cin_isterminated( |
| 6178 | char_u *s, |
| 6179 | int incl_open, /* include '{' at the end as terminator */ |
| 6180 | int incl_comma) /* recognize a trailing comma */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6181 | { |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6182 | char_u found_start = 0; |
| 6183 | unsigned n_open = 0; |
| 6184 | int is_else = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6185 | |
| 6186 | s = cin_skipcomment(s); |
| 6187 | |
| 6188 | if (*s == '{' || (*s == '}' && !cin_iselse(s))) |
| 6189 | found_start = *s; |
| 6190 | |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6191 | if (!found_start) |
| 6192 | is_else = cin_iselse(s); |
| 6193 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6194 | while (*s) |
| 6195 | { |
| 6196 | /* skip over comments, "" strings and 'c'haracters */ |
| 6197 | s = skip_string(cin_skipcomment(s)); |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6198 | if (*s == '}' && n_open > 0) |
| 6199 | --n_open; |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6200 | if ((!is_else || n_open == 0) |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6201 | && (*s == ';' || *s == '}' || (incl_comma && *s == ',')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6202 | && cin_nocode(s + 1)) |
| 6203 | return *s; |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6204 | else if (*s == '{') |
| 6205 | { |
| 6206 | if (incl_open && cin_nocode(s + 1)) |
| 6207 | return *s; |
| 6208 | else |
| 6209 | ++n_open; |
| 6210 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6211 | |
| 6212 | if (*s) |
| 6213 | s++; |
| 6214 | } |
| 6215 | return found_start; |
| 6216 | } |
| 6217 | |
| 6218 | /* |
| 6219 | * Recognize the basic picture of a function declaration -- it needs to |
| 6220 | * have an open paren somewhere and a close paren at the end of the line and |
| 6221 | * no semicolons anywhere. |
| 6222 | * When a line ends in a comma we continue looking in the next line. |
| 6223 | * "sp" points to a string with the line. When looking at other lines it must |
| 6224 | * be restored to the line. When it's NULL fetch lines here. |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6225 | * "first_lnum" is where we start looking. |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6226 | * "min_lnum" is the line before which we will not be looking. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6227 | */ |
| 6228 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6229 | cin_isfuncdecl( |
| 6230 | char_u **sp, |
| 6231 | linenr_T first_lnum, |
| 6232 | linenr_T min_lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6233 | { |
| 6234 | char_u *s; |
| 6235 | linenr_T lnum = first_lnum; |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6236 | linenr_T save_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6237 | int retval = FALSE; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6238 | pos_T *trypos; |
| 6239 | int just_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6240 | |
| 6241 | if (sp == NULL) |
| 6242 | s = ml_get(lnum); |
| 6243 | else |
| 6244 | s = *sp; |
| 6245 | |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6246 | curwin->w_cursor.lnum = lnum; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6247 | if (find_last_paren(s, '(', ')') |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6248 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6249 | { |
| 6250 | lnum = trypos->lnum; |
| 6251 | if (lnum < min_lnum) |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6252 | { |
| 6253 | curwin->w_cursor.lnum = save_lnum; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6254 | return FALSE; |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6255 | } |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6256 | |
| 6257 | s = ml_get(lnum); |
| 6258 | } |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6259 | curwin->w_cursor.lnum = save_lnum; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6260 | |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6261 | /* Ignore line starting with #. */ |
| 6262 | if (cin_ispreproc(s)) |
| 6263 | return FALSE; |
| 6264 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6265 | while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"') |
| 6266 | { |
| 6267 | if (cin_iscomment(s)) /* ignore comments */ |
| 6268 | s = cin_skipcomment(s); |
Bram Moolenaar | e01f4f8 | 2015-11-10 14:06:53 +0100 | [diff] [blame] | 6269 | else if (*s == ':') |
| 6270 | { |
| 6271 | if (*(s + 1) == ':') |
| 6272 | s += 2; |
| 6273 | else |
| 6274 | /* To avoid a mistake in the following situation: |
| 6275 | * A::A(int a, int b) |
| 6276 | * : a(0) // <--not a function decl |
| 6277 | * , b(0) |
| 6278 | * {... |
| 6279 | */ |
| 6280 | return FALSE; |
| 6281 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6282 | else |
| 6283 | ++s; |
| 6284 | } |
| 6285 | if (*s != '(') |
| 6286 | return FALSE; /* ';', ' or " before any () or no '(' */ |
| 6287 | |
| 6288 | while (*s && *s != ';' && *s != '\'' && *s != '"') |
| 6289 | { |
| 6290 | if (*s == ')' && cin_nocode(s + 1)) |
| 6291 | { |
| 6292 | /* ')' at the end: may have found a match |
| 6293 | * Check for he previous line not to end in a backslash: |
| 6294 | * #if defined(x) && \ |
| 6295 | * defined(y) |
| 6296 | */ |
| 6297 | lnum = first_lnum - 1; |
| 6298 | s = ml_get(lnum); |
| 6299 | if (*s == NUL || s[STRLEN(s) - 1] != '\\') |
| 6300 | retval = TRUE; |
| 6301 | goto done; |
| 6302 | } |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6303 | if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6304 | { |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6305 | int comma = (*s == ','); |
| 6306 | |
| 6307 | /* ',' at the end: continue looking in the next line. |
| 6308 | * At the end: check for ',' in the next line, for this style: |
| 6309 | * func(arg1 |
| 6310 | * , arg2) */ |
| 6311 | for (;;) |
| 6312 | { |
| 6313 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 6314 | break; |
| 6315 | s = ml_get(++lnum); |
| 6316 | if (!cin_ispreproc(s)) |
| 6317 | break; |
| 6318 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6319 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 6320 | break; |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6321 | /* Require a comma at end of the line or a comma or ')' at the |
| 6322 | * start of next line. */ |
| 6323 | s = skipwhite(s); |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6324 | if (!just_started && (!comma && *s != ',' && *s != ')')) |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6325 | break; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6326 | just_started = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6327 | } |
| 6328 | else if (cin_iscomment(s)) /* ignore comments */ |
| 6329 | s = cin_skipcomment(s); |
| 6330 | else |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6331 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6332 | ++s; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6333 | just_started = FALSE; |
| 6334 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6335 | } |
| 6336 | |
| 6337 | done: |
| 6338 | if (lnum != first_lnum && sp != NULL) |
| 6339 | *sp = ml_get(first_lnum); |
| 6340 | |
| 6341 | return retval; |
| 6342 | } |
| 6343 | |
| 6344 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6345 | cin_isif(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6346 | { |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6347 | return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2])); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6348 | } |
| 6349 | |
| 6350 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6351 | cin_iselse( |
| 6352 | char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6353 | { |
| 6354 | if (*p == '}') /* accept "} else" */ |
| 6355 | p = cin_skipcomment(p + 1); |
| 6356 | return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4])); |
| 6357 | } |
| 6358 | |
| 6359 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6360 | cin_isdo(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6361 | { |
| 6362 | return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2])); |
| 6363 | } |
| 6364 | |
| 6365 | /* |
| 6366 | * Check if this is a "while" that should have a matching "do". |
| 6367 | * We only accept a "while (condition) ;", with only white space between the |
| 6368 | * ')' and ';'. The condition may be spread over several lines. |
| 6369 | */ |
| 6370 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6371 | cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6372 | { |
| 6373 | pos_T cursor_save; |
| 6374 | pos_T *trypos; |
| 6375 | int retval = FALSE; |
| 6376 | |
| 6377 | p = cin_skipcomment(p); |
| 6378 | if (*p == '}') /* accept "} while (cond);" */ |
| 6379 | p = cin_skipcomment(p + 1); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6380 | if (cin_starts_with(p, "while")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6381 | { |
| 6382 | cursor_save = curwin->w_cursor; |
| 6383 | curwin->w_cursor.lnum = lnum; |
| 6384 | curwin->w_cursor.col = 0; |
| 6385 | p = ml_get_curline(); |
| 6386 | while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */ |
| 6387 | { |
| 6388 | ++p; |
| 6389 | ++curwin->w_cursor.col; |
| 6390 | } |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6391 | if ((trypos = findmatchlimit(NULL, 0, 0, |
| 6392 | curbuf->b_ind_maxparen)) != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6393 | && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';') |
| 6394 | retval = TRUE; |
| 6395 | curwin->w_cursor = cursor_save; |
| 6396 | } |
| 6397 | return retval; |
| 6398 | } |
| 6399 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6400 | /* |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 6401 | * 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] | 6402 | * Return 0 if there is none. |
| 6403 | * Otherwise return !0 and update "*poffset" to point to the place where the |
| 6404 | * string was found. |
| 6405 | */ |
| 6406 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6407 | cin_is_if_for_while_before_offset(char_u *line, int *poffset) |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6408 | { |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 6409 | int offset = *poffset; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6410 | |
| 6411 | if (offset-- < 2) |
| 6412 | return 0; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6413 | while (offset > 2 && VIM_ISWHITE(line[offset])) |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6414 | --offset; |
| 6415 | |
| 6416 | offset -= 1; |
| 6417 | if (!STRNCMP(line + offset, "if", 2)) |
| 6418 | goto probablyFound; |
| 6419 | |
| 6420 | if (offset >= 1) |
| 6421 | { |
| 6422 | offset -= 1; |
| 6423 | if (!STRNCMP(line + offset, "for", 3)) |
| 6424 | goto probablyFound; |
| 6425 | |
| 6426 | if (offset >= 2) |
| 6427 | { |
| 6428 | offset -= 2; |
| 6429 | if (!STRNCMP(line + offset, "while", 5)) |
| 6430 | goto probablyFound; |
| 6431 | } |
| 6432 | } |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6433 | return 0; |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 6434 | |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6435 | probablyFound: |
| 6436 | if (!offset || !vim_isIDc(line[offset - 1])) |
| 6437 | { |
| 6438 | *poffset = offset; |
| 6439 | return 1; |
| 6440 | } |
| 6441 | return 0; |
| 6442 | } |
| 6443 | |
| 6444 | /* |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6445 | * Return TRUE if we are at the end of a do-while. |
| 6446 | * do |
| 6447 | * nothing; |
| 6448 | * while (foo |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 6449 | * && bar); <-- here |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6450 | * Adjust the cursor to the line with "while". |
| 6451 | */ |
| 6452 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6453 | cin_iswhileofdo_end(int terminated) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6454 | { |
| 6455 | char_u *line; |
| 6456 | char_u *p; |
| 6457 | char_u *s; |
| 6458 | pos_T *trypos; |
| 6459 | int i; |
| 6460 | |
| 6461 | if (terminated != ';') /* there must be a ';' at the end */ |
| 6462 | return FALSE; |
| 6463 | |
| 6464 | p = line = ml_get_curline(); |
| 6465 | while (*p != NUL) |
| 6466 | { |
| 6467 | p = cin_skipcomment(p); |
| 6468 | if (*p == ')') |
| 6469 | { |
| 6470 | s = skipwhite(p + 1); |
| 6471 | if (*s == ';' && cin_nocode(s + 1)) |
| 6472 | { |
| 6473 | /* Found ");" at end of the line, now check there is "while" |
| 6474 | * before the matching '('. XXX */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6475 | i = (int)(p - line); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6476 | curwin->w_cursor.col = i; |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6477 | trypos = find_match_paren(curbuf->b_ind_maxparen); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6478 | if (trypos != NULL) |
| 6479 | { |
| 6480 | s = cin_skipcomment(ml_get(trypos->lnum)); |
| 6481 | if (*s == '}') /* accept "} while (cond);" */ |
| 6482 | s = cin_skipcomment(s + 1); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6483 | if (cin_starts_with(s, "while")) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6484 | { |
| 6485 | curwin->w_cursor.lnum = trypos->lnum; |
| 6486 | return TRUE; |
| 6487 | } |
| 6488 | } |
| 6489 | |
| 6490 | /* Searching may have made "line" invalid, get it again. */ |
| 6491 | line = ml_get_curline(); |
| 6492 | p = line + i; |
| 6493 | } |
| 6494 | } |
| 6495 | if (*p != NUL) |
| 6496 | ++p; |
| 6497 | } |
| 6498 | return FALSE; |
| 6499 | } |
| 6500 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6501 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6502 | cin_isbreak(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6503 | { |
| 6504 | return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5])); |
| 6505 | } |
| 6506 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6507 | /* |
| 6508 | * Find the position of a C++ base-class declaration or |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6509 | * constructor-initialization. eg: |
| 6510 | * |
| 6511 | * class MyClass : |
| 6512 | * baseClass <-- here |
| 6513 | * class MyClass : public baseClass, |
| 6514 | * anotherBaseClass <-- here (should probably lineup ??) |
| 6515 | * MyClass::MyClass(...) : |
| 6516 | * baseClass(...) <-- here (constructor-initialization) |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 6517 | * |
| 6518 | * This is a lot of guessing. Watch out for "cond ? func() : foo". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6519 | */ |
| 6520 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6521 | cin_is_cpp_baseclass( |
| 6522 | cpp_baseclass_cache_T *cached) /* input and output */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6523 | { |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6524 | lpos_T *pos = &cached->lpos; /* find position */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6525 | char_u *s; |
| 6526 | int class_or_struct, lookfor_ctor_init, cpp_base_class; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6527 | linenr_T lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6528 | char_u *line = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6529 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6530 | if (pos->lnum <= lnum) |
| 6531 | return cached->found; /* Use the cached result */ |
| 6532 | |
| 6533 | pos->col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6534 | |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 6535 | s = skipwhite(line); |
| 6536 | if (*s == '#') /* skip #define FOO x ? (x) : x */ |
| 6537 | return FALSE; |
| 6538 | s = cin_skipcomment(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6539 | if (*s == NUL) |
| 6540 | return FALSE; |
| 6541 | |
| 6542 | cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; |
| 6543 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6544 | /* Search for a line starting with '#', empty, ending in ';' or containing |
| 6545 | * '{' or '}' and start below it. This handles the following situations: |
| 6546 | * a = cond ? |
| 6547 | * func() : |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 6548 | * asdf; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6549 | * func::foo() |
| 6550 | * : something |
| 6551 | * {} |
| 6552 | * Foo::Foo (int one, int two) |
| 6553 | * : something(4), |
| 6554 | * somethingelse(3) |
| 6555 | * {} |
| 6556 | */ |
| 6557 | while (lnum > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6558 | { |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6559 | line = ml_get(lnum - 1); |
| 6560 | s = skipwhite(line); |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6561 | if (*s == '#' || *s == NUL) |
| 6562 | break; |
| 6563 | while (*s != NUL) |
| 6564 | { |
| 6565 | s = cin_skipcomment(s); |
| 6566 | if (*s == '{' || *s == '}' |
| 6567 | || (*s == ';' && cin_nocode(s + 1))) |
| 6568 | break; |
| 6569 | if (*s != NUL) |
| 6570 | ++s; |
| 6571 | } |
| 6572 | if (*s != NUL) |
| 6573 | break; |
| 6574 | --lnum; |
| 6575 | } |
| 6576 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6577 | pos->lnum = lnum; |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6578 | line = ml_get(lnum); |
Bram Moolenaar | d1b15de | 2015-10-13 16:13:39 +0200 | [diff] [blame] | 6579 | s = line; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6580 | for (;;) |
| 6581 | { |
| 6582 | if (*s == NUL) |
| 6583 | { |
| 6584 | if (lnum == curwin->w_cursor.lnum) |
| 6585 | break; |
| 6586 | /* Continue in the cursor line. */ |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6587 | line = ml_get(++lnum); |
Bram Moolenaar | d1b15de | 2015-10-13 16:13:39 +0200 | [diff] [blame] | 6588 | s = line; |
| 6589 | } |
| 6590 | if (s == line) |
| 6591 | { |
| 6592 | /* don't recognize "case (foo):" as a baseclass */ |
| 6593 | if (cin_iscase(s, FALSE)) |
| 6594 | break; |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6595 | s = cin_skipcomment(line); |
| 6596 | if (*s == NUL) |
| 6597 | continue; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6598 | } |
| 6599 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 6600 | if (s[0] == '"' || (s[0] == 'R' && s[1] == '"')) |
Bram Moolenaar | aede6ce | 2011-05-10 11:56:30 +0200 | [diff] [blame] | 6601 | s = skip_string(s) + 1; |
| 6602 | else if (s[0] == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6603 | { |
| 6604 | if (s[1] == ':') |
| 6605 | { |
| 6606 | /* skip double colon. It can't be a constructor |
| 6607 | * initialization any more */ |
| 6608 | lookfor_ctor_init = FALSE; |
| 6609 | s = cin_skipcomment(s + 2); |
| 6610 | } |
| 6611 | else if (lookfor_ctor_init || class_or_struct) |
| 6612 | { |
| 6613 | /* we have something found, that looks like the start of |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 6614 | * cpp-base-class-declaration or constructor-initialization */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6615 | cpp_base_class = TRUE; |
| 6616 | lookfor_ctor_init = class_or_struct = FALSE; |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6617 | pos->col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6618 | s = cin_skipcomment(s + 1); |
| 6619 | } |
| 6620 | else |
| 6621 | s = cin_skipcomment(s + 1); |
| 6622 | } |
| 6623 | else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5])) |
| 6624 | || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6]))) |
| 6625 | { |
| 6626 | class_or_struct = TRUE; |
| 6627 | lookfor_ctor_init = FALSE; |
| 6628 | |
| 6629 | if (*s == 'c') |
| 6630 | s = cin_skipcomment(s + 5); |
| 6631 | else |
| 6632 | s = cin_skipcomment(s + 6); |
| 6633 | } |
| 6634 | else |
| 6635 | { |
| 6636 | if (s[0] == '{' || s[0] == '}' || s[0] == ';') |
| 6637 | { |
| 6638 | cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; |
| 6639 | } |
| 6640 | else if (s[0] == ')') |
| 6641 | { |
| 6642 | /* Constructor-initialization is assumed if we come across |
| 6643 | * something like "):" */ |
| 6644 | class_or_struct = FALSE; |
| 6645 | lookfor_ctor_init = TRUE; |
| 6646 | } |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 6647 | else if (s[0] == '?') |
| 6648 | { |
| 6649 | /* Avoid seeing '() :' after '?' as constructor init. */ |
| 6650 | return FALSE; |
| 6651 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6652 | else if (!vim_isIDc(s[0])) |
| 6653 | { |
| 6654 | /* if it is not an identifier, we are wrong */ |
| 6655 | class_or_struct = FALSE; |
| 6656 | lookfor_ctor_init = FALSE; |
| 6657 | } |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6658 | else if (pos->col == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6659 | { |
| 6660 | /* it can't be a constructor-initialization any more */ |
| 6661 | lookfor_ctor_init = FALSE; |
| 6662 | |
| 6663 | /* the first statement starts here: lineup with this one... */ |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6664 | if (cpp_base_class) |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6665 | pos->col = (colnr_T)(s - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6666 | } |
| 6667 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6668 | /* When the line ends in a comma don't align with it. */ |
| 6669 | if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1)) |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6670 | pos->col = 0; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6671 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6672 | s = cin_skipcomment(s + 1); |
| 6673 | } |
| 6674 | } |
| 6675 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6676 | cached->found = cpp_base_class; |
| 6677 | if (cpp_base_class) |
| 6678 | pos->lnum = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6679 | return cpp_base_class; |
| 6680 | } |
| 6681 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6682 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6683 | get_baseclass_amount(int col) |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6684 | { |
| 6685 | int amount; |
| 6686 | colnr_T vcol; |
| 6687 | pos_T *trypos; |
| 6688 | |
| 6689 | if (col == 0) |
| 6690 | { |
| 6691 | amount = get_indent(); |
| 6692 | if (find_last_paren(ml_get_curline(), '(', ')') |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6693 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6694 | amount = get_indent_lnum(trypos->lnum); /* XXX */ |
| 6695 | if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL)) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6696 | amount += curbuf->b_ind_cpp_baseclass; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6697 | } |
| 6698 | else |
| 6699 | { |
| 6700 | curwin->w_cursor.col = col; |
| 6701 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 6702 | amount = (int)vcol; |
| 6703 | } |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6704 | if (amount < curbuf->b_ind_cpp_baseclass) |
| 6705 | amount = curbuf->b_ind_cpp_baseclass; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6706 | return amount; |
| 6707 | } |
| 6708 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6709 | /* |
| 6710 | * Return TRUE if string "s" ends with the string "find", possibly followed by |
| 6711 | * white space and comments. Skip strings and comments. |
| 6712 | * Ignore "ignore" after "find" if it's not NULL. |
| 6713 | */ |
| 6714 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6715 | cin_ends_in(char_u *s, char_u *find, char_u *ignore) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6716 | { |
| 6717 | char_u *p = s; |
| 6718 | char_u *r; |
| 6719 | int len = (int)STRLEN(find); |
| 6720 | |
| 6721 | while (*p != NUL) |
| 6722 | { |
| 6723 | p = cin_skipcomment(p); |
| 6724 | if (STRNCMP(p, find, len) == 0) |
| 6725 | { |
| 6726 | r = skipwhite(p + len); |
| 6727 | if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0) |
| 6728 | r = skipwhite(r + STRLEN(ignore)); |
| 6729 | if (cin_nocode(r)) |
| 6730 | return TRUE; |
| 6731 | } |
| 6732 | if (*p != NUL) |
| 6733 | ++p; |
| 6734 | } |
| 6735 | return FALSE; |
| 6736 | } |
| 6737 | |
| 6738 | /* |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6739 | * Return TRUE when "s" starts with "word" and then a non-ID character. |
| 6740 | */ |
| 6741 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6742 | cin_starts_with(char_u *s, char *word) |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6743 | { |
Bram Moolenaar | b3cb982 | 2013-03-13 17:01:52 +0100 | [diff] [blame] | 6744 | int l = (int)STRLEN(word); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6745 | |
| 6746 | return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l])); |
| 6747 | } |
| 6748 | |
| 6749 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6750 | * Skip strings, chars and comments until at or past "trypos". |
| 6751 | * Return the column found. |
| 6752 | */ |
| 6753 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6754 | cin_skip2pos(pos_T *trypos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6755 | { |
| 6756 | char_u *line; |
| 6757 | char_u *p; |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 6758 | char_u *new_p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6759 | |
| 6760 | p = line = ml_get(trypos->lnum); |
| 6761 | while (*p && (colnr_T)(p - line) < trypos->col) |
| 6762 | { |
| 6763 | if (cin_iscomment(p)) |
| 6764 | p = cin_skipcomment(p); |
| 6765 | else |
| 6766 | { |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 6767 | new_p = skip_string(p); |
| 6768 | if (new_p == p) |
| 6769 | ++p; |
| 6770 | else |
| 6771 | p = new_p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6772 | } |
| 6773 | } |
| 6774 | return (int)(p - line); |
| 6775 | } |
| 6776 | |
| 6777 | /* |
| 6778 | * Find the '{' at the start of the block we are in. |
| 6779 | * Return NULL if no match found. |
| 6780 | * Ignore a '{' that is in a comment, makes indenting the next three lines |
| 6781 | * work. */ |
| 6782 | /* foo() */ |
| 6783 | /* { */ |
| 6784 | /* } */ |
| 6785 | |
| 6786 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6787 | find_start_brace(void) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6788 | { |
| 6789 | pos_T cursor_save; |
| 6790 | pos_T *trypos; |
| 6791 | pos_T *pos; |
| 6792 | static pos_T pos_copy; |
| 6793 | |
| 6794 | cursor_save = curwin->w_cursor; |
| 6795 | while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL) |
| 6796 | { |
| 6797 | pos_copy = *trypos; /* copy pos_T, next findmatch will change it */ |
| 6798 | trypos = &pos_copy; |
| 6799 | curwin->w_cursor = *trypos; |
| 6800 | pos = NULL; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6801 | /* ignore the { if it's in a // or / * * / comment */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6802 | if ((colnr_T)cin_skip2pos(trypos) == trypos->col |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 6803 | && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6804 | break; |
| 6805 | if (pos != NULL) |
| 6806 | curwin->w_cursor.lnum = pos->lnum; |
| 6807 | } |
| 6808 | curwin->w_cursor = cursor_save; |
| 6809 | return trypos; |
| 6810 | } |
| 6811 | |
| 6812 | /* |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 6813 | * Find the matching '(', ignoring it if it is in a comment. |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6814 | * Return NULL if no match found. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6815 | */ |
| 6816 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6817 | find_match_paren(int ind_maxparen) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6818 | { |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6819 | return find_match_char('(', ind_maxparen); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 6820 | } |
| 6821 | |
| 6822 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6823 | find_match_char (int c, int ind_maxparen) /* XXX */ |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 6824 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6825 | pos_T cursor_save; |
| 6826 | pos_T *trypos; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6827 | static pos_T pos_copy; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6828 | int ind_maxp_wk; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6829 | |
| 6830 | cursor_save = curwin->w_cursor; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6831 | ind_maxp_wk = ind_maxparen; |
| 6832 | retry: |
| 6833 | if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6834 | { |
| 6835 | /* check if the ( is in a // comment */ |
| 6836 | if ((colnr_T)cin_skip2pos(trypos) > trypos->col) |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6837 | { |
| 6838 | ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum); |
| 6839 | if (ind_maxp_wk > 0) |
| 6840 | { |
| 6841 | curwin->w_cursor = *trypos; |
| 6842 | curwin->w_cursor.col = 0; /* XXX */ |
| 6843 | goto retry; |
| 6844 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6845 | trypos = NULL; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6846 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6847 | else |
| 6848 | { |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6849 | pos_T *trypos_wk; |
| 6850 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6851 | pos_copy = *trypos; /* copy trypos, findmatch will change it */ |
| 6852 | trypos = &pos_copy; |
| 6853 | curwin->w_cursor = *trypos; |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 6854 | if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */ |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6855 | { |
| 6856 | ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum |
| 6857 | - trypos_wk->lnum); |
| 6858 | if (ind_maxp_wk > 0) |
| 6859 | { |
| 6860 | curwin->w_cursor = *trypos_wk; |
| 6861 | goto retry; |
| 6862 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6863 | trypos = NULL; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 6864 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6865 | } |
| 6866 | } |
| 6867 | curwin->w_cursor = cursor_save; |
| 6868 | return trypos; |
| 6869 | } |
| 6870 | |
| 6871 | /* |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 6872 | * Find the matching '(', ignoring it if it is in a comment or before an |
| 6873 | * unmatched {. |
| 6874 | * Return NULL if no match found. |
| 6875 | */ |
| 6876 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6877 | find_match_paren_after_brace (int ind_maxparen) /* XXX */ |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 6878 | { |
| 6879 | pos_T *trypos = find_match_paren(ind_maxparen); |
| 6880 | |
| 6881 | if (trypos != NULL) |
| 6882 | { |
| 6883 | pos_T *tryposBrace = find_start_brace(); |
| 6884 | |
| 6885 | /* If both an unmatched '(' and '{' is found. Ignore the '(' |
| 6886 | * position if the '{' is further down. */ |
| 6887 | if (tryposBrace != NULL |
| 6888 | && (trypos->lnum != tryposBrace->lnum |
| 6889 | ? trypos->lnum < tryposBrace->lnum |
| 6890 | : trypos->col < tryposBrace->col)) |
| 6891 | trypos = NULL; |
| 6892 | } |
| 6893 | return trypos; |
| 6894 | } |
| 6895 | |
| 6896 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6897 | * Return ind_maxparen corrected for the difference in line number between the |
| 6898 | * cursor position and "startpos". This makes sure that searching for a |
| 6899 | * matching paren above the cursor line doesn't find a match because of |
| 6900 | * looking a few lines further. |
| 6901 | */ |
| 6902 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6903 | corr_ind_maxparen(pos_T *startpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6904 | { |
| 6905 | long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum; |
| 6906 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6907 | if (n > 0 && n < curbuf->b_ind_maxparen / 2) |
| 6908 | return curbuf->b_ind_maxparen - (int)n; |
| 6909 | return curbuf->b_ind_maxparen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6910 | } |
| 6911 | |
| 6912 | /* |
| 6913 | * 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] | 6914 | * line "l". "l" must point to the start of the line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6915 | */ |
| 6916 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6917 | find_last_paren(char_u *l, int start, int end) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6918 | { |
| 6919 | int i; |
| 6920 | int retval = FALSE; |
| 6921 | int open_count = 0; |
| 6922 | |
| 6923 | curwin->w_cursor.col = 0; /* default is start of line */ |
| 6924 | |
Bram Moolenaar | 6d8f9c6 | 2011-11-30 13:03:28 +0100 | [diff] [blame] | 6925 | for (i = 0; l[i] != NUL; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6926 | { |
| 6927 | i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */ |
| 6928 | i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */ |
| 6929 | if (l[i] == start) |
| 6930 | ++open_count; |
| 6931 | else if (l[i] == end) |
| 6932 | { |
| 6933 | if (open_count > 0) |
| 6934 | --open_count; |
| 6935 | else |
| 6936 | { |
| 6937 | curwin->w_cursor.col = i; |
| 6938 | retval = TRUE; |
| 6939 | } |
| 6940 | } |
| 6941 | } |
| 6942 | return retval; |
| 6943 | } |
| 6944 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 6945 | /* |
| 6946 | * Parse 'cinoptions' and set the values in "curbuf". |
| 6947 | * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes. |
| 6948 | */ |
| 6949 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6950 | parse_cino(buf_T *buf) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 6951 | { |
| 6952 | char_u *p; |
| 6953 | char_u *l; |
| 6954 | char_u *digits; |
| 6955 | int n; |
| 6956 | int divider; |
| 6957 | int fraction = 0; |
| 6958 | int sw = (int)get_sw_value(buf); |
| 6959 | |
| 6960 | /* |
| 6961 | * Set the default values. |
| 6962 | */ |
| 6963 | /* Spaces from a block's opening brace the prevailing indent for that |
| 6964 | * block should be. */ |
| 6965 | buf->b_ind_level = sw; |
| 6966 | |
| 6967 | /* Spaces from the edge of the line an open brace that's at the end of a |
| 6968 | * line is imagined to be. */ |
| 6969 | buf->b_ind_open_imag = 0; |
| 6970 | |
| 6971 | /* Spaces from the prevailing indent for a line that is not preceded by |
| 6972 | * an opening brace. */ |
| 6973 | buf->b_ind_no_brace = 0; |
| 6974 | |
| 6975 | /* Column where the first { of a function should be located }. */ |
| 6976 | buf->b_ind_first_open = 0; |
| 6977 | |
| 6978 | /* Spaces from the prevailing indent a leftmost open brace should be |
| 6979 | * located. */ |
| 6980 | buf->b_ind_open_extra = 0; |
| 6981 | |
| 6982 | /* Spaces from the matching open brace (real location for one at the left |
| 6983 | * edge; imaginary location from one that ends a line) the matching close |
| 6984 | * brace should be located. */ |
| 6985 | buf->b_ind_close_extra = 0; |
| 6986 | |
| 6987 | /* Spaces from the edge of the line an open brace sitting in the leftmost |
| 6988 | * column is imagined to be. */ |
| 6989 | buf->b_ind_open_left_imag = 0; |
| 6990 | |
| 6991 | /* Spaces jump labels should be shifted to the left if N is non-negative, |
| 6992 | * otherwise the jump label will be put to column 1. */ |
| 6993 | buf->b_ind_jump_label = -1; |
| 6994 | |
| 6995 | /* Spaces from the switch() indent a "case xx" label should be located. */ |
| 6996 | buf->b_ind_case = sw; |
| 6997 | |
| 6998 | /* Spaces from the "case xx:" code after a switch() should be located. */ |
| 6999 | buf->b_ind_case_code = sw; |
| 7000 | |
| 7001 | /* Lineup break at end of case in switch() with case label. */ |
| 7002 | buf->b_ind_case_break = 0; |
| 7003 | |
| 7004 | /* Spaces from the class declaration indent a scope declaration label |
| 7005 | * should be located. */ |
| 7006 | buf->b_ind_scopedecl = sw; |
| 7007 | |
| 7008 | /* Spaces from the scope declaration label code should be located. */ |
| 7009 | buf->b_ind_scopedecl_code = sw; |
| 7010 | |
| 7011 | /* Amount K&R-style parameters should be indented. */ |
| 7012 | buf->b_ind_param = sw; |
| 7013 | |
| 7014 | /* Amount a function type spec should be indented. */ |
| 7015 | buf->b_ind_func_type = sw; |
| 7016 | |
| 7017 | /* Amount a cpp base class declaration or constructor initialization |
| 7018 | * should be indented. */ |
| 7019 | buf->b_ind_cpp_baseclass = sw; |
| 7020 | |
| 7021 | /* additional spaces beyond the prevailing indent a continuation line |
| 7022 | * should be located. */ |
| 7023 | buf->b_ind_continuation = sw; |
| 7024 | |
| 7025 | /* Spaces from the indent of the line with an unclosed parentheses. */ |
| 7026 | buf->b_ind_unclosed = sw * 2; |
| 7027 | |
| 7028 | /* Spaces from the indent of the line with an unclosed parentheses, which |
| 7029 | * itself is also unclosed. */ |
| 7030 | buf->b_ind_unclosed2 = sw; |
| 7031 | |
| 7032 | /* Suppress ignoring spaces from the indent of a line starting with an |
| 7033 | * unclosed parentheses. */ |
| 7034 | buf->b_ind_unclosed_noignore = 0; |
| 7035 | |
| 7036 | /* If the opening paren is the last nonwhite character on the line, and |
| 7037 | * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer |
| 7038 | * context (for very long lines). */ |
| 7039 | buf->b_ind_unclosed_wrapped = 0; |
| 7040 | |
| 7041 | /* Suppress ignoring white space when lining up with the character after |
| 7042 | * an unclosed parentheses. */ |
| 7043 | buf->b_ind_unclosed_whiteok = 0; |
| 7044 | |
| 7045 | /* Indent a closing parentheses under the line start of the matching |
| 7046 | * opening parentheses. */ |
| 7047 | buf->b_ind_matching_paren = 0; |
| 7048 | |
| 7049 | /* Indent a closing parentheses under the previous line. */ |
| 7050 | buf->b_ind_paren_prev = 0; |
| 7051 | |
| 7052 | /* Extra indent for comments. */ |
| 7053 | buf->b_ind_comment = 0; |
| 7054 | |
| 7055 | /* Spaces from the comment opener when there is nothing after it. */ |
| 7056 | buf->b_ind_in_comment = 3; |
| 7057 | |
| 7058 | /* Boolean: if non-zero, use b_ind_in_comment even if there is something |
| 7059 | * after the comment opener. */ |
| 7060 | buf->b_ind_in_comment2 = 0; |
| 7061 | |
| 7062 | /* Max lines to search for an open paren. */ |
| 7063 | buf->b_ind_maxparen = 20; |
| 7064 | |
| 7065 | /* Max lines to search for an open comment. */ |
| 7066 | buf->b_ind_maxcomment = 70; |
| 7067 | |
| 7068 | /* Handle braces for java code. */ |
| 7069 | buf->b_ind_java = 0; |
| 7070 | |
| 7071 | /* Not to confuse JS object properties with labels. */ |
| 7072 | buf->b_ind_js = 0; |
| 7073 | |
| 7074 | /* Handle blocked cases correctly. */ |
| 7075 | buf->b_ind_keep_case_label = 0; |
| 7076 | |
| 7077 | /* Handle C++ namespace. */ |
| 7078 | buf->b_ind_cpp_namespace = 0; |
| 7079 | |
| 7080 | /* Handle continuation lines containing conditions of if(), for() and |
| 7081 | * while(). */ |
| 7082 | buf->b_ind_if_for_while = 0; |
| 7083 | |
Bram Moolenaar | 6b64394 | 2017-03-05 19:44:06 +0100 | [diff] [blame] | 7084 | /* indentation for # comments */ |
| 7085 | buf->b_ind_hash_comment = 0; |
| 7086 | |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 7087 | /* Handle C++ extern "C" or "C++" */ |
| 7088 | buf->b_ind_cpp_extern_c = 0; |
| 7089 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7090 | for (p = buf->b_p_cino; *p; ) |
| 7091 | { |
| 7092 | l = p++; |
| 7093 | if (*p == '-') |
| 7094 | ++p; |
| 7095 | digits = p; /* remember where the digits start */ |
| 7096 | n = getdigits(&p); |
| 7097 | divider = 0; |
| 7098 | if (*p == '.') /* ".5s" means a fraction */ |
| 7099 | { |
| 7100 | fraction = atol((char *)++p); |
| 7101 | while (VIM_ISDIGIT(*p)) |
| 7102 | { |
| 7103 | ++p; |
| 7104 | if (divider) |
| 7105 | divider *= 10; |
| 7106 | else |
| 7107 | divider = 10; |
| 7108 | } |
| 7109 | } |
| 7110 | if (*p == 's') /* "2s" means two times 'shiftwidth' */ |
| 7111 | { |
| 7112 | if (p == digits) |
| 7113 | n = sw; /* just "s" is one 'shiftwidth' */ |
| 7114 | else |
| 7115 | { |
| 7116 | n *= sw; |
| 7117 | if (divider) |
| 7118 | n += (sw * fraction + divider / 2) / divider; |
| 7119 | } |
| 7120 | ++p; |
| 7121 | } |
| 7122 | if (l[1] == '-') |
| 7123 | n = -n; |
| 7124 | |
| 7125 | /* When adding an entry here, also update the default 'cinoptions' in |
| 7126 | * doc/indent.txt, and add explanation for it! */ |
| 7127 | switch (*l) |
| 7128 | { |
| 7129 | case '>': buf->b_ind_level = n; break; |
| 7130 | case 'e': buf->b_ind_open_imag = n; break; |
| 7131 | case 'n': buf->b_ind_no_brace = n; break; |
| 7132 | case 'f': buf->b_ind_first_open = n; break; |
| 7133 | case '{': buf->b_ind_open_extra = n; break; |
| 7134 | case '}': buf->b_ind_close_extra = n; break; |
| 7135 | case '^': buf->b_ind_open_left_imag = n; break; |
| 7136 | case 'L': buf->b_ind_jump_label = n; break; |
| 7137 | case ':': buf->b_ind_case = n; break; |
| 7138 | case '=': buf->b_ind_case_code = n; break; |
| 7139 | case 'b': buf->b_ind_case_break = n; break; |
| 7140 | case 'p': buf->b_ind_param = n; break; |
| 7141 | case 't': buf->b_ind_func_type = n; break; |
| 7142 | case '/': buf->b_ind_comment = n; break; |
| 7143 | case 'c': buf->b_ind_in_comment = n; break; |
| 7144 | case 'C': buf->b_ind_in_comment2 = n; break; |
| 7145 | case 'i': buf->b_ind_cpp_baseclass = n; break; |
| 7146 | case '+': buf->b_ind_continuation = n; break; |
| 7147 | case '(': buf->b_ind_unclosed = n; break; |
| 7148 | case 'u': buf->b_ind_unclosed2 = n; break; |
| 7149 | case 'U': buf->b_ind_unclosed_noignore = n; break; |
| 7150 | case 'W': buf->b_ind_unclosed_wrapped = n; break; |
| 7151 | case 'w': buf->b_ind_unclosed_whiteok = n; break; |
| 7152 | case 'm': buf->b_ind_matching_paren = n; break; |
| 7153 | case 'M': buf->b_ind_paren_prev = n; break; |
| 7154 | case ')': buf->b_ind_maxparen = n; break; |
| 7155 | case '*': buf->b_ind_maxcomment = n; break; |
| 7156 | case 'g': buf->b_ind_scopedecl = n; break; |
| 7157 | case 'h': buf->b_ind_scopedecl_code = n; break; |
| 7158 | case 'j': buf->b_ind_java = n; break; |
| 7159 | case 'J': buf->b_ind_js = n; break; |
| 7160 | case 'l': buf->b_ind_keep_case_label = n; break; |
| 7161 | case '#': buf->b_ind_hash_comment = n; break; |
| 7162 | case 'N': buf->b_ind_cpp_namespace = n; break; |
| 7163 | case 'k': buf->b_ind_if_for_while = n; break; |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 7164 | case 'E': buf->b_ind_cpp_extern_c = n; break; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7165 | } |
| 7166 | if (*p == ',') |
| 7167 | ++p; |
| 7168 | } |
| 7169 | } |
| 7170 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7171 | /* |
| 7172 | * Return the desired indent for C code. |
| 7173 | * Return -1 if the indent should be left alone (inside a raw string). |
| 7174 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7175 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7176 | get_c_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7177 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7178 | pos_T cur_curpos; |
| 7179 | int amount; |
| 7180 | int scope_amount; |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 7181 | int cur_amount = MAXCOL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7182 | colnr_T col; |
| 7183 | char_u *theline; |
| 7184 | char_u *linecopy; |
| 7185 | pos_T *trypos; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7186 | pos_T *comment_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7187 | pos_T *tryposBrace = NULL; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7188 | pos_T tryposCopy; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7189 | pos_T our_paren_pos; |
| 7190 | char_u *start; |
| 7191 | int start_brace; |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 7192 | #define BRACE_IN_COL0 1 /* '{' is in column 0 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7193 | #define BRACE_AT_START 2 /* '{' is at start of line */ |
| 7194 | #define BRACE_AT_END 3 /* '{' is at end of line */ |
| 7195 | linenr_T ourscope; |
| 7196 | char_u *l; |
| 7197 | char_u *look; |
| 7198 | char_u terminated; |
| 7199 | int lookfor; |
| 7200 | #define LOOKFOR_INITIAL 0 |
| 7201 | #define LOOKFOR_IF 1 |
| 7202 | #define LOOKFOR_DO 2 |
| 7203 | #define LOOKFOR_CASE 3 |
| 7204 | #define LOOKFOR_ANY 4 |
| 7205 | #define LOOKFOR_TERM 5 |
| 7206 | #define LOOKFOR_UNTERM 6 |
| 7207 | #define LOOKFOR_SCOPEDECL 7 |
| 7208 | #define LOOKFOR_NOBREAK 8 |
| 7209 | #define LOOKFOR_CPP_BASECLASS 9 |
| 7210 | #define LOOKFOR_ENUM_OR_INIT 10 |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7211 | #define LOOKFOR_JS_KEY 11 |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 7212 | #define LOOKFOR_COMMA 12 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7213 | |
| 7214 | int whilelevel; |
| 7215 | linenr_T lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7216 | int n; |
| 7217 | int iscase; |
| 7218 | int lookfor_break; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 7219 | int lookfor_cpp_namespace = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7220 | int cont_amount = 0; /* amount for continuation line */ |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 7221 | int original_line_islabel; |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 7222 | int added_to_amount = 0; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7223 | int js_cur_has_key = 0; |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 7224 | linenr_T raw_string_start = 0; |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 7225 | cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } }; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7226 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7227 | /* make a copy, value is changed below */ |
| 7228 | int ind_continuation = curbuf->b_ind_continuation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7229 | |
| 7230 | /* remember where the cursor was when we started */ |
| 7231 | cur_curpos = curwin->w_cursor; |
| 7232 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7233 | /* if we are at line 1 zero indent is fine, right? */ |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 7234 | if (cur_curpos.lnum == 1) |
| 7235 | return 0; |
| 7236 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7237 | /* Get a copy of the current contents of the line. |
| 7238 | * This is required, because only the most recent line obtained with |
| 7239 | * ml_get is valid! */ |
| 7240 | linecopy = vim_strsave(ml_get(cur_curpos.lnum)); |
| 7241 | if (linecopy == NULL) |
| 7242 | return 0; |
| 7243 | |
| 7244 | /* |
| 7245 | * In insert mode and the cursor is on a ')' truncate the line at the |
| 7246 | * cursor position. We don't want to line up with the matching '(' when |
| 7247 | * inserting new stuff. |
| 7248 | * For unknown reasons the cursor might be past the end of the line, thus |
| 7249 | * check for that. |
| 7250 | */ |
| 7251 | if ((State & INSERT) |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 7252 | && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7253 | && linecopy[curwin->w_cursor.col] == ')') |
| 7254 | linecopy[curwin->w_cursor.col] = NUL; |
| 7255 | |
| 7256 | theline = skipwhite(linecopy); |
| 7257 | |
| 7258 | /* move the cursor to the start of the line */ |
| 7259 | |
| 7260 | curwin->w_cursor.col = 0; |
| 7261 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7262 | original_line_islabel = cin_islabel(); /* XXX */ |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 7263 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7264 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7265 | * If we are inside a raw string don't change the indent. |
| 7266 | * Ignore a raw string inside a comment. |
| 7267 | */ |
| 7268 | comment_pos = ind_find_start_comment(); |
| 7269 | if (comment_pos != NULL) |
| 7270 | { |
| 7271 | /* findmatchlimit() static pos is overwritten, make a copy */ |
| 7272 | tryposCopy = *comment_pos; |
| 7273 | comment_pos = &tryposCopy; |
| 7274 | } |
| 7275 | trypos = find_start_rawstring(curbuf->b_ind_maxcomment); |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 7276 | if (trypos != NULL && (comment_pos == NULL |
| 7277 | || LT_POS(*trypos, *comment_pos))) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7278 | { |
| 7279 | amount = -1; |
| 7280 | goto laterend; |
| 7281 | } |
| 7282 | |
| 7283 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7284 | * #defines and so on always go at the left when included in 'cinkeys'. |
| 7285 | */ |
| 7286 | if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE))) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7287 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7288 | amount = curbuf->b_ind_hash_comment; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7289 | goto theend; |
| 7290 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7291 | |
| 7292 | /* |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 7293 | * Is it a non-case label? Then that goes at the left margin too unless: |
| 7294 | * - JS flag is set. |
| 7295 | * - 'L' item has a positive value. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7296 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7297 | if (original_line_islabel && !curbuf->b_ind_js |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7298 | && curbuf->b_ind_jump_label < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7299 | { |
| 7300 | amount = 0; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7301 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7302 | } |
| 7303 | |
| 7304 | /* |
| 7305 | * If we're inside a "//" comment and there is a "//" comment in a |
| 7306 | * previous line, lineup with that one. |
| 7307 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7308 | if (cin_islinecomment(theline) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7309 | && (trypos = find_line_comment()) != NULL) /* XXX */ |
| 7310 | { |
| 7311 | /* find how indented the line beginning the comment is */ |
| 7312 | getvcol(curwin, trypos, &col, NULL, NULL); |
| 7313 | amount = col; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7314 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7315 | } |
| 7316 | |
| 7317 | /* |
| 7318 | * If we're inside a comment and not looking at the start of the |
| 7319 | * comment, try using the 'comments' option. |
| 7320 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7321 | if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7322 | { |
| 7323 | int lead_start_len = 2; |
| 7324 | int lead_middle_len = 1; |
| 7325 | char_u lead_start[COM_MAX_LEN]; /* start-comment string */ |
| 7326 | char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ |
| 7327 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 7328 | char_u *p; |
| 7329 | int start_align = 0; |
| 7330 | int start_off = 0; |
| 7331 | int done = FALSE; |
| 7332 | |
| 7333 | /* find how indented the line beginning the comment is */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7334 | getvcol(curwin, comment_pos, &col, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7335 | amount = col; |
Bram Moolenaar | 4aa9742 | 2011-04-11 14:27:38 +0200 | [diff] [blame] | 7336 | *lead_start = NUL; |
| 7337 | *lead_middle = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7338 | |
| 7339 | p = curbuf->b_p_com; |
| 7340 | while (*p != NUL) |
| 7341 | { |
| 7342 | int align = 0; |
| 7343 | int off = 0; |
| 7344 | int what = 0; |
| 7345 | |
| 7346 | while (*p != NUL && *p != ':') |
| 7347 | { |
| 7348 | if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE) |
| 7349 | what = *p++; |
| 7350 | else if (*p == COM_LEFT || *p == COM_RIGHT) |
| 7351 | align = *p++; |
| 7352 | else if (VIM_ISDIGIT(*p) || *p == '-') |
| 7353 | off = getdigits(&p); |
| 7354 | else |
| 7355 | ++p; |
| 7356 | } |
| 7357 | |
| 7358 | if (*p == ':') |
| 7359 | ++p; |
| 7360 | (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 7361 | if (what == COM_START) |
| 7362 | { |
| 7363 | STRCPY(lead_start, lead_end); |
| 7364 | lead_start_len = (int)STRLEN(lead_start); |
| 7365 | start_off = off; |
| 7366 | start_align = align; |
| 7367 | } |
| 7368 | else if (what == COM_MIDDLE) |
| 7369 | { |
| 7370 | STRCPY(lead_middle, lead_end); |
| 7371 | lead_middle_len = (int)STRLEN(lead_middle); |
| 7372 | } |
| 7373 | else if (what == COM_END) |
| 7374 | { |
| 7375 | /* If our line starts with the middle comment string, line it |
| 7376 | * up with the comment opener per the 'comments' option. */ |
| 7377 | if (STRNCMP(theline, lead_middle, lead_middle_len) == 0 |
| 7378 | && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0) |
| 7379 | { |
| 7380 | done = TRUE; |
| 7381 | if (curwin->w_cursor.lnum > 1) |
| 7382 | { |
| 7383 | /* If the start comment string matches in the previous |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 7384 | * line, use the indent of that line plus offset. If |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7385 | * the middle comment string matches in the previous |
| 7386 | * line, use the indent of that line. XXX */ |
| 7387 | look = skipwhite(ml_get(curwin->w_cursor.lnum - 1)); |
| 7388 | if (STRNCMP(look, lead_start, lead_start_len) == 0) |
| 7389 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 7390 | else if (STRNCMP(look, lead_middle, |
| 7391 | lead_middle_len) == 0) |
| 7392 | { |
| 7393 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 7394 | break; |
| 7395 | } |
| 7396 | /* If the start comment string doesn't match with the |
| 7397 | * start of the comment, skip this entry. XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7398 | else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7399 | lead_start, lead_start_len) != 0) |
| 7400 | continue; |
| 7401 | } |
| 7402 | if (start_off != 0) |
| 7403 | amount += start_off; |
| 7404 | else if (start_align == COM_RIGHT) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 7405 | amount += vim_strsize(lead_start) |
| 7406 | - vim_strsize(lead_middle); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7407 | break; |
| 7408 | } |
| 7409 | |
| 7410 | /* If our line starts with the end comment string, line it up |
| 7411 | * with the middle comment */ |
| 7412 | if (STRNCMP(theline, lead_middle, lead_middle_len) != 0 |
| 7413 | && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0) |
| 7414 | { |
| 7415 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 7416 | /* XXX */ |
| 7417 | if (off != 0) |
| 7418 | amount += off; |
| 7419 | else if (align == COM_RIGHT) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 7420 | amount += vim_strsize(lead_start) |
| 7421 | - vim_strsize(lead_middle); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7422 | done = TRUE; |
| 7423 | break; |
| 7424 | } |
| 7425 | } |
| 7426 | } |
| 7427 | |
| 7428 | /* If our line starts with an asterisk, line up with the |
| 7429 | * asterisk in the comment opener; otherwise, line up |
| 7430 | * with the first character of the comment text. |
| 7431 | */ |
| 7432 | if (done) |
| 7433 | ; |
| 7434 | else if (theline[0] == '*') |
| 7435 | amount += 1; |
| 7436 | else |
| 7437 | { |
| 7438 | /* |
| 7439 | * If we are more than one line away from the comment opener, take |
| 7440 | * the indent of the previous non-empty line. If 'cino' has "CO" |
| 7441 | * and we are just below the comment opener and there are any |
| 7442 | * white characters after it line up with the text after it; |
| 7443 | * otherwise, add the amount specified by "c" in 'cino' |
| 7444 | */ |
| 7445 | amount = -1; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7446 | for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7447 | { |
| 7448 | if (linewhite(lnum)) /* skip blank lines */ |
| 7449 | continue; |
| 7450 | amount = get_indent_lnum(lnum); /* XXX */ |
| 7451 | break; |
| 7452 | } |
| 7453 | if (amount == -1) /* use the comment opener */ |
| 7454 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7455 | if (!curbuf->b_ind_in_comment2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7456 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7457 | start = ml_get(comment_pos->lnum); |
| 7458 | look = start + comment_pos->col + 2; /* skip / and * */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7459 | if (*look != NUL) /* if something after it */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7460 | comment_pos->col = (colnr_T)(skipwhite(look) - start); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7461 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7462 | getvcol(curwin, comment_pos, &col, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7463 | amount = col; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7464 | if (curbuf->b_ind_in_comment2 || *look == NUL) |
| 7465 | amount += curbuf->b_ind_in_comment; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7466 | } |
| 7467 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7468 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7469 | } |
| 7470 | |
| 7471 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7472 | * Are we looking at a ']' that has a match? |
| 7473 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7474 | if (*skipwhite(theline) == ']' |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7475 | && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL) |
| 7476 | { |
| 7477 | /* align with the line containing the '['. */ |
| 7478 | amount = get_indent_lnum(trypos->lnum); |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7479 | goto theend; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7480 | } |
| 7481 | |
| 7482 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7483 | * Are we inside parentheses or braces? |
| 7484 | */ /* XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7485 | if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7486 | && curbuf->b_ind_java == 0) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7487 | || (tryposBrace = find_start_brace()) != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7488 | || trypos != NULL) |
| 7489 | { |
| 7490 | if (trypos != NULL && tryposBrace != NULL) |
| 7491 | { |
| 7492 | /* Both an unmatched '(' and '{' is found. Use the one which is |
| 7493 | * closer to the current cursor position, set the other to NULL. */ |
| 7494 | if (trypos->lnum != tryposBrace->lnum |
| 7495 | ? trypos->lnum < tryposBrace->lnum |
| 7496 | : trypos->col < tryposBrace->col) |
| 7497 | trypos = NULL; |
| 7498 | else |
| 7499 | tryposBrace = NULL; |
| 7500 | } |
| 7501 | |
| 7502 | if (trypos != NULL) |
| 7503 | { |
| 7504 | /* |
| 7505 | * If the matching paren is more than one line away, use the indent of |
| 7506 | * a previous non-empty line that matches the same paren. |
| 7507 | */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7508 | if (theline[0] == ')' && curbuf->b_ind_paren_prev) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7509 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7510 | /* Line up with the start of the matching paren line. */ |
| 7511 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */ |
| 7512 | } |
| 7513 | else |
| 7514 | { |
| 7515 | amount = -1; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7516 | our_paren_pos = *trypos; |
| 7517 | for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7518 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7519 | l = skipwhite(ml_get(lnum)); |
| 7520 | if (cin_nocode(l)) /* skip comment lines */ |
| 7521 | continue; |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 7522 | if (cin_ispreproc_cont(&l, &lnum, &amount)) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7523 | continue; /* ignore #define, #if, etc. */ |
| 7524 | curwin->w_cursor.lnum = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7525 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7526 | /* Skip a comment or raw string. XXX */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 7527 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7528 | { |
| 7529 | lnum = trypos->lnum + 1; |
| 7530 | continue; |
| 7531 | } |
| 7532 | |
| 7533 | /* XXX */ |
| 7534 | if ((trypos = find_match_paren( |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7535 | corr_ind_maxparen(&cur_curpos))) != NULL |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7536 | && trypos->lnum == our_paren_pos.lnum |
| 7537 | && trypos->col == our_paren_pos.col) |
| 7538 | { |
| 7539 | amount = get_indent_lnum(lnum); /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7540 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7541 | if (theline[0] == ')') |
| 7542 | { |
| 7543 | if (our_paren_pos.lnum != lnum |
| 7544 | && cur_amount > amount) |
| 7545 | cur_amount = amount; |
| 7546 | amount = -1; |
| 7547 | } |
| 7548 | break; |
| 7549 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7550 | } |
| 7551 | } |
| 7552 | |
| 7553 | /* |
| 7554 | * Line up with line where the matching paren is. XXX |
| 7555 | * If the line starts with a '(' or the indent for unclosed |
| 7556 | * parentheses is zero, line up with the unclosed parentheses. |
| 7557 | */ |
| 7558 | if (amount == -1) |
| 7559 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7560 | int ignore_paren_col = 0; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7561 | int is_if_for_while = 0; |
| 7562 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7563 | if (curbuf->b_ind_if_for_while) |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7564 | { |
| 7565 | /* Look for the outermost opening parenthesis on this line |
| 7566 | * and check whether it belongs to an "if", "for" or "while". */ |
| 7567 | |
| 7568 | pos_T cursor_save = curwin->w_cursor; |
| 7569 | pos_T outermost; |
| 7570 | char_u *line; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7571 | |
| 7572 | trypos = &our_paren_pos; |
| 7573 | do { |
| 7574 | outermost = *trypos; |
| 7575 | curwin->w_cursor.lnum = outermost.lnum; |
| 7576 | curwin->w_cursor.col = outermost.col; |
| 7577 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7578 | trypos = find_match_paren(curbuf->b_ind_maxparen); |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7579 | } while (trypos && trypos->lnum == outermost.lnum); |
| 7580 | |
| 7581 | curwin->w_cursor = cursor_save; |
| 7582 | |
| 7583 | line = ml_get(outermost.lnum); |
| 7584 | |
| 7585 | is_if_for_while = |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 7586 | cin_is_if_for_while_before_offset(line, &outermost.col); |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7587 | } |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7588 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7589 | amount = skip_label(our_paren_pos.lnum, &look); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7590 | look = skipwhite(look); |
| 7591 | if (*look == '(') |
| 7592 | { |
| 7593 | linenr_T save_lnum = curwin->w_cursor.lnum; |
| 7594 | char_u *line; |
| 7595 | int look_col; |
| 7596 | |
| 7597 | /* Ignore a '(' in front of the line that has a match before |
| 7598 | * our matching '('. */ |
| 7599 | curwin->w_cursor.lnum = our_paren_pos.lnum; |
| 7600 | line = ml_get_curline(); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7601 | look_col = (int)(look - line); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7602 | curwin->w_cursor.col = look_col + 1; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7603 | if ((trypos = findmatchlimit(NULL, ')', 0, |
| 7604 | curbuf->b_ind_maxparen)) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7605 | != NULL |
| 7606 | && trypos->lnum == our_paren_pos.lnum |
| 7607 | && trypos->col < our_paren_pos.col) |
| 7608 | ignore_paren_col = trypos->col + 1; |
| 7609 | |
| 7610 | curwin->w_cursor.lnum = save_lnum; |
| 7611 | look = ml_get(our_paren_pos.lnum) + look_col; |
| 7612 | } |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7613 | if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0 |
| 7614 | && is_if_for_while == 0) |
| 7615 | || (!curbuf->b_ind_unclosed_noignore && *look == '(' |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7616 | && ignore_paren_col == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7617 | { |
| 7618 | /* |
| 7619 | * If we're looking at a close paren, line up right there; |
| 7620 | * otherwise, line up with the next (non-white) character. |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7621 | * When b_ind_unclosed_wrapped is set and the matching paren is |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7622 | * the last nonwhite character of the line, use either the |
| 7623 | * indent of the current line or the indentation of the next |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7624 | * outer paren and add b_ind_unclosed_wrapped (for very long |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7625 | * lines). |
| 7626 | */ |
| 7627 | if (theline[0] != ')') |
| 7628 | { |
| 7629 | cur_amount = MAXCOL; |
| 7630 | l = ml_get(our_paren_pos.lnum); |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7631 | if (curbuf->b_ind_unclosed_wrapped |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7632 | && cin_ends_in(l, (char_u *)"(", NULL)) |
| 7633 | { |
| 7634 | /* look for opening unmatched paren, indent one level |
| 7635 | * for each additional level */ |
| 7636 | n = 1; |
| 7637 | for (col = 0; col < our_paren_pos.col; ++col) |
| 7638 | { |
| 7639 | switch (l[col]) |
| 7640 | { |
| 7641 | case '(': |
| 7642 | case '{': ++n; |
| 7643 | break; |
| 7644 | |
| 7645 | case ')': |
| 7646 | case '}': if (n > 1) |
| 7647 | --n; |
| 7648 | break; |
| 7649 | } |
| 7650 | } |
| 7651 | |
| 7652 | our_paren_pos.col = 0; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7653 | amount += n * curbuf->b_ind_unclosed_wrapped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7654 | } |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7655 | else if (curbuf->b_ind_unclosed_whiteok) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7656 | our_paren_pos.col++; |
| 7657 | else |
| 7658 | { |
| 7659 | col = our_paren_pos.col + 1; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 7660 | while (VIM_ISWHITE(l[col])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7661 | col++; |
| 7662 | if (l[col] != NUL) /* In case of trailing space */ |
| 7663 | our_paren_pos.col = col; |
| 7664 | else |
| 7665 | our_paren_pos.col++; |
| 7666 | } |
| 7667 | } |
| 7668 | |
| 7669 | /* |
| 7670 | * Find how indented the paren is, or the character after it |
| 7671 | * if we did the above "if". |
| 7672 | */ |
| 7673 | if (our_paren_pos.col > 0) |
| 7674 | { |
| 7675 | getvcol(curwin, &our_paren_pos, &col, NULL, NULL); |
| 7676 | if (cur_amount > (int)col) |
| 7677 | cur_amount = col; |
| 7678 | } |
| 7679 | } |
| 7680 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7681 | if (theline[0] == ')' && curbuf->b_ind_matching_paren) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7682 | { |
| 7683 | /* Line up with the start of the matching paren line. */ |
| 7684 | } |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7685 | else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0) |
| 7686 | || (!curbuf->b_ind_unclosed_noignore |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7687 | && *look == '(' && ignore_paren_col == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7688 | { |
| 7689 | if (cur_amount != MAXCOL) |
| 7690 | amount = cur_amount; |
| 7691 | } |
| 7692 | else |
| 7693 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7694 | /* Add b_ind_unclosed2 for each '(' before our matching one, |
| 7695 | * but ignore (void) before the line (ignore_paren_col). */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7696 | col = our_paren_pos.col; |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 7697 | while ((int)our_paren_pos.col > ignore_paren_col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7698 | { |
| 7699 | --our_paren_pos.col; |
| 7700 | switch (*ml_get_pos(&our_paren_pos)) |
| 7701 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7702 | case '(': amount += curbuf->b_ind_unclosed2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7703 | col = our_paren_pos.col; |
| 7704 | break; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7705 | case ')': amount -= curbuf->b_ind_unclosed2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7706 | col = MAXCOL; |
| 7707 | break; |
| 7708 | } |
| 7709 | } |
| 7710 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7711 | /* Use b_ind_unclosed once, when the first '(' is not inside |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7712 | * braces */ |
| 7713 | if (col == MAXCOL) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7714 | amount += curbuf->b_ind_unclosed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7715 | else |
| 7716 | { |
| 7717 | curwin->w_cursor.lnum = our_paren_pos.lnum; |
| 7718 | curwin->w_cursor.col = col; |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 7719 | if (find_match_paren_after_brace(curbuf->b_ind_maxparen) |
| 7720 | != NULL) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7721 | amount += curbuf->b_ind_unclosed2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7722 | else |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7723 | { |
| 7724 | if (is_if_for_while) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7725 | amount += curbuf->b_ind_if_for_while; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7726 | else |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7727 | amount += curbuf->b_ind_unclosed; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7728 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7729 | } |
| 7730 | /* |
| 7731 | * For a line starting with ')' use the minimum of the two |
| 7732 | * positions, to avoid giving it more indent than the previous |
| 7733 | * lines: |
| 7734 | * func_long_name( if (x |
| 7735 | * arg && yy |
| 7736 | * ) ^ not here ) ^ not here |
| 7737 | */ |
| 7738 | if (cur_amount < amount) |
| 7739 | amount = cur_amount; |
| 7740 | } |
| 7741 | } |
| 7742 | |
| 7743 | /* add extra indent for a comment */ |
| 7744 | if (cin_iscomment(theline)) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7745 | amount += curbuf->b_ind_comment; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7746 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7747 | else |
| 7748 | { |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7749 | /* |
| 7750 | * We are inside braces, there is a { before this line at the position |
| 7751 | * stored in tryposBrace. |
Bram Moolenaar | 04d17ae | 2014-08-06 17:44:14 +0200 | [diff] [blame] | 7752 | * Make a copy of tryposBrace, it may point to pos_copy inside |
| 7753 | * find_start_brace(), which may be changed somewhere. |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7754 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7755 | tryposCopy = *tryposBrace; |
| 7756 | tryposBrace = &tryposCopy; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7757 | trypos = tryposBrace; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7758 | ourscope = trypos->lnum; |
| 7759 | start = ml_get(ourscope); |
| 7760 | |
| 7761 | /* |
| 7762 | * Now figure out how indented the line is in general. |
| 7763 | * If the brace was at the start of the line, we use that; |
| 7764 | * otherwise, check out the indentation of the line as |
| 7765 | * a whole and then add the "imaginary indent" to that. |
| 7766 | */ |
| 7767 | look = skipwhite(start); |
| 7768 | if (*look == '{') |
| 7769 | { |
| 7770 | getvcol(curwin, trypos, &col, NULL, NULL); |
| 7771 | amount = col; |
| 7772 | if (*start == '{') |
| 7773 | start_brace = BRACE_IN_COL0; |
| 7774 | else |
| 7775 | start_brace = BRACE_AT_START; |
| 7776 | } |
| 7777 | else |
| 7778 | { |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7779 | /* That opening brace might have been on a continuation |
| 7780 | * line. if so, find the start of the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7781 | curwin->w_cursor.lnum = ourscope; |
| 7782 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7783 | /* Position the cursor over the rightmost paren, so that |
| 7784 | * matching it will take us back to the start of the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7785 | lnum = ourscope; |
| 7786 | if (find_last_paren(start, '(', ')') |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7787 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) |
| 7788 | != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7789 | lnum = trypos->lnum; |
| 7790 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7791 | /* It could have been something like |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7792 | * case 1: if (asdf && |
| 7793 | * ldfd) { |
| 7794 | * } |
| 7795 | */ |
Bram Moolenaar | e7eb789 | 2014-07-02 17:02:36 +0200 | [diff] [blame] | 7796 | if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label) |
| 7797 | && cin_iscase(skipwhite(ml_get_curline()), FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7798 | amount = get_indent(); |
Bram Moolenaar | e7eb789 | 2014-07-02 17:02:36 +0200 | [diff] [blame] | 7799 | else if (curbuf->b_ind_js) |
| 7800 | amount = get_indent_lnum(lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7801 | else |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7802 | amount = skip_label(lnum, &l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7803 | |
| 7804 | start_brace = BRACE_AT_END; |
| 7805 | } |
| 7806 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7807 | /* For Javascript check if the line starts with "key:". */ |
| 7808 | if (curbuf->b_ind_js) |
| 7809 | js_cur_has_key = cin_has_js_key(theline); |
| 7810 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7811 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7812 | * If we're looking at a closing brace, that's where |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7813 | * we want to be. otherwise, add the amount of room |
| 7814 | * that an indent is supposed to be. |
| 7815 | */ |
| 7816 | if (theline[0] == '}') |
| 7817 | { |
| 7818 | /* |
| 7819 | * they may want closing braces to line up with something |
| 7820 | * other than the open brace. indulge them, if so. |
| 7821 | */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7822 | amount += curbuf->b_ind_close_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7823 | } |
| 7824 | else |
| 7825 | { |
| 7826 | /* |
| 7827 | * If we're looking at an "else", try to find an "if" |
| 7828 | * to match it with. |
| 7829 | * If we're looking at a "while", try to find a "do" |
| 7830 | * to match it with. |
| 7831 | */ |
| 7832 | lookfor = LOOKFOR_INITIAL; |
| 7833 | if (cin_iselse(theline)) |
| 7834 | lookfor = LOOKFOR_IF; |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7835 | else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7836 | lookfor = LOOKFOR_DO; |
| 7837 | if (lookfor != LOOKFOR_INITIAL) |
| 7838 | { |
| 7839 | curwin->w_cursor.lnum = cur_curpos.lnum; |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7840 | if (find_match(lookfor, ourscope) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7841 | { |
| 7842 | amount = get_indent(); /* XXX */ |
| 7843 | goto theend; |
| 7844 | } |
| 7845 | } |
| 7846 | |
| 7847 | /* |
| 7848 | * We get here if we are not on an "while-of-do" or "else" (or |
| 7849 | * failed to find a matching "if"). |
| 7850 | * Search backwards for something to line up with. |
| 7851 | * First set amount for when we don't find anything. |
| 7852 | */ |
| 7853 | |
| 7854 | /* |
| 7855 | * if the '{' is _really_ at the left margin, use the imaginary |
| 7856 | * location of a left-margin brace. Otherwise, correct the |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7857 | * location for b_ind_open_extra. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7858 | */ |
| 7859 | |
| 7860 | if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */ |
| 7861 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7862 | amount = curbuf->b_ind_open_left_imag; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 7863 | lookfor_cpp_namespace = TRUE; |
| 7864 | } |
| 7865 | else if (start_brace == BRACE_AT_START && |
| 7866 | lookfor_cpp_namespace) /* '{' is at start */ |
| 7867 | { |
| 7868 | |
| 7869 | lookfor_cpp_namespace = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7870 | } |
| 7871 | else |
| 7872 | { |
| 7873 | if (start_brace == BRACE_AT_END) /* '{' is at end of line */ |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 7874 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7875 | amount += curbuf->b_ind_open_imag; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 7876 | |
| 7877 | l = skipwhite(ml_get_curline()); |
| 7878 | if (cin_is_cpp_namespace(l)) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7879 | amount += curbuf->b_ind_cpp_namespace; |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 7880 | else if (cin_is_cpp_extern_c(l)) |
| 7881 | amount += curbuf->b_ind_cpp_extern_c; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 7882 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7883 | else |
| 7884 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7885 | /* Compensate for adding b_ind_open_extra later. */ |
| 7886 | amount -= curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7887 | if (amount < 0) |
| 7888 | amount = 0; |
| 7889 | } |
| 7890 | } |
| 7891 | |
| 7892 | lookfor_break = FALSE; |
| 7893 | |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 7894 | if (cin_iscase(theline, FALSE)) /* it's a switch() label */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7895 | { |
| 7896 | lookfor = LOOKFOR_CASE; /* find a previous switch() label */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7897 | amount += curbuf->b_ind_case; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7898 | } |
| 7899 | else if (cin_isscopedecl(theline)) /* private:, ... */ |
| 7900 | { |
| 7901 | lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7902 | amount += curbuf->b_ind_scopedecl; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7903 | } |
| 7904 | else |
| 7905 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7906 | if (curbuf->b_ind_case_break && cin_isbreak(theline)) |
| 7907 | /* break; ... */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7908 | lookfor_break = TRUE; |
| 7909 | |
| 7910 | lookfor = LOOKFOR_INITIAL; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7911 | /* b_ind_level from start of block */ |
| 7912 | amount += curbuf->b_ind_level; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7913 | } |
| 7914 | scope_amount = amount; |
| 7915 | whilelevel = 0; |
| 7916 | |
| 7917 | /* |
| 7918 | * Search backwards. If we find something we recognize, line up |
| 7919 | * with that. |
| 7920 | * |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7921 | * If we're looking at an open brace, indent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7922 | * the usual amount relative to the conditional |
| 7923 | * that opens the block. |
| 7924 | */ |
| 7925 | curwin->w_cursor = cur_curpos; |
| 7926 | for (;;) |
| 7927 | { |
| 7928 | curwin->w_cursor.lnum--; |
| 7929 | curwin->w_cursor.col = 0; |
| 7930 | |
| 7931 | /* |
| 7932 | * If we went all the way back to the start of our scope, line |
| 7933 | * up with it. |
| 7934 | */ |
| 7935 | if (curwin->w_cursor.lnum <= ourscope) |
| 7936 | { |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 7937 | /* We reached end of scope: |
| 7938 | * If looking for a enum or structure initialization |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7939 | * go further back: |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 7940 | * If it is an initializer (enum xxx or xxx =), then |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7941 | * don't add ind_continuation, otherwise it is a variable |
| 7942 | * declaration: |
| 7943 | * int x, |
| 7944 | * here; <-- add ind_continuation |
| 7945 | */ |
| 7946 | if (lookfor == LOOKFOR_ENUM_OR_INIT) |
| 7947 | { |
| 7948 | if (curwin->w_cursor.lnum == 0 |
| 7949 | || curwin->w_cursor.lnum |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7950 | < ourscope - curbuf->b_ind_maxparen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7951 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7952 | /* nothing found (abuse curbuf->b_ind_maxparen as |
| 7953 | * limit) assume terminated line (i.e. a variable |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7954 | * initialization) */ |
| 7955 | if (cont_amount > 0) |
| 7956 | amount = cont_amount; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7957 | else if (!curbuf->b_ind_js) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7958 | amount += ind_continuation; |
| 7959 | break; |
| 7960 | } |
| 7961 | |
| 7962 | l = ml_get_curline(); |
| 7963 | |
| 7964 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7965 | * If we're in a comment or raw string now, skip to |
| 7966 | * the start of it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7967 | */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 7968 | trypos = ind_find_start_CORS(NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7969 | if (trypos != NULL) |
| 7970 | { |
| 7971 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 7972 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7973 | continue; |
| 7974 | } |
| 7975 | |
| 7976 | /* |
| 7977 | * Skip preprocessor directives and blank lines. |
| 7978 | */ |
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 | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7981 | continue; |
| 7982 | |
| 7983 | if (cin_nocode(l)) |
| 7984 | continue; |
| 7985 | |
| 7986 | terminated = cin_isterminated(l, FALSE, TRUE); |
| 7987 | |
| 7988 | /* |
| 7989 | * If we are at top level and the line looks like a |
| 7990 | * function declaration, we are done |
| 7991 | * (it's a variable declaration). |
| 7992 | */ |
| 7993 | if (start_brace != BRACE_IN_COL0 |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7994 | || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7995 | { |
| 7996 | /* if the line is terminated with another ',' |
| 7997 | * it is a continued variable initialization. |
| 7998 | * don't add extra indent. |
| 7999 | * TODO: does not work, if a function |
| 8000 | * declaration is split over multiple lines: |
| 8001 | * cin_isfuncdecl returns FALSE then. |
| 8002 | */ |
| 8003 | if (terminated == ',') |
| 8004 | break; |
| 8005 | |
| 8006 | /* if it es a enum declaration or an assignment, |
| 8007 | * we are done. |
| 8008 | */ |
| 8009 | if (terminated != ';' && cin_isinit()) |
| 8010 | break; |
| 8011 | |
| 8012 | /* nothing useful found */ |
| 8013 | if (terminated == 0 || terminated == '{') |
| 8014 | continue; |
| 8015 | } |
| 8016 | |
| 8017 | if (terminated != ';') |
| 8018 | { |
| 8019 | /* Skip parens and braces. Position the cursor |
| 8020 | * over the rightmost paren, so that matching it |
| 8021 | * will take us back to the start of the line. |
| 8022 | */ /* XXX */ |
| 8023 | trypos = NULL; |
| 8024 | if (find_last_paren(l, '(', ')')) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8025 | trypos = find_match_paren( |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8026 | curbuf->b_ind_maxparen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8027 | |
| 8028 | if (trypos == NULL && find_last_paren(l, '{', '}')) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8029 | trypos = find_start_brace(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8030 | |
| 8031 | if (trypos != NULL) |
| 8032 | { |
| 8033 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8034 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8035 | continue; |
| 8036 | } |
| 8037 | } |
| 8038 | |
| 8039 | /* it's a variable declaration, add indentation |
| 8040 | * like in |
| 8041 | * int a, |
| 8042 | * b; |
| 8043 | */ |
| 8044 | if (cont_amount > 0) |
| 8045 | amount = cont_amount; |
| 8046 | else |
| 8047 | amount += ind_continuation; |
| 8048 | } |
| 8049 | else if (lookfor == LOOKFOR_UNTERM) |
| 8050 | { |
| 8051 | if (cont_amount > 0) |
| 8052 | amount = cont_amount; |
| 8053 | else |
| 8054 | amount += ind_continuation; |
| 8055 | } |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8056 | else |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 8057 | { |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8058 | if (lookfor != LOOKFOR_TERM |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8059 | && lookfor != LOOKFOR_CPP_BASECLASS |
| 8060 | && lookfor != LOOKFOR_COMMA) |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8061 | { |
| 8062 | amount = scope_amount; |
| 8063 | if (theline[0] == '{') |
| 8064 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8065 | amount += curbuf->b_ind_open_extra; |
| 8066 | added_to_amount = curbuf->b_ind_open_extra; |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8067 | } |
| 8068 | } |
| 8069 | |
| 8070 | if (lookfor_cpp_namespace) |
| 8071 | { |
| 8072 | /* |
| 8073 | * Looking for C++ namespace, need to look further |
| 8074 | * back. |
| 8075 | */ |
| 8076 | if (curwin->w_cursor.lnum == ourscope) |
| 8077 | continue; |
| 8078 | |
| 8079 | if (curwin->w_cursor.lnum == 0 |
| 8080 | || curwin->w_cursor.lnum |
| 8081 | < ourscope - FIND_NAMESPACE_LIM) |
| 8082 | break; |
| 8083 | |
| 8084 | l = ml_get_curline(); |
| 8085 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8086 | /* If we're in a comment or raw string now, skip |
| 8087 | * to the start of it. */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 8088 | trypos = ind_find_start_CORS(NULL); |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8089 | if (trypos != NULL) |
| 8090 | { |
| 8091 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 8092 | curwin->w_cursor.col = 0; |
| 8093 | continue; |
| 8094 | } |
| 8095 | |
| 8096 | /* Skip preprocessor directives and blank lines. */ |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8097 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, |
| 8098 | &amount)) |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8099 | continue; |
| 8100 | |
| 8101 | /* Finally the actual check for "namespace". */ |
| 8102 | if (cin_is_cpp_namespace(l)) |
| 8103 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8104 | amount += curbuf->b_ind_cpp_namespace |
| 8105 | - added_to_amount; |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8106 | break; |
| 8107 | } |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 8108 | else if (cin_is_cpp_extern_c(l)) |
| 8109 | { |
| 8110 | amount += curbuf->b_ind_cpp_extern_c |
| 8111 | - added_to_amount; |
| 8112 | break; |
| 8113 | } |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8114 | |
| 8115 | if (cin_nocode(l)) |
| 8116 | continue; |
| 8117 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8118 | } |
| 8119 | break; |
| 8120 | } |
| 8121 | |
| 8122 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8123 | * If we're in a comment or raw string now, skip to the start |
| 8124 | * of it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8125 | */ /* XXX */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 8126 | if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8127 | { |
| 8128 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8129 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8130 | continue; |
| 8131 | } |
| 8132 | |
| 8133 | l = ml_get_curline(); |
| 8134 | |
| 8135 | /* |
| 8136 | * If this is a switch() label, may line up relative to that. |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8137 | * If this is a C++ scope declaration, do the same. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8138 | */ |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8139 | iscase = cin_iscase(l, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8140 | if (iscase || cin_isscopedecl(l)) |
| 8141 | { |
| 8142 | /* we are only looking for cpp base class |
| 8143 | * declaration/initialization any longer */ |
| 8144 | if (lookfor == LOOKFOR_CPP_BASECLASS) |
| 8145 | break; |
| 8146 | |
| 8147 | /* When looking for a "do" we are not interested in |
| 8148 | * labels. */ |
| 8149 | if (whilelevel > 0) |
| 8150 | continue; |
| 8151 | |
| 8152 | /* |
| 8153 | * case xx: |
| 8154 | * c = 99 + <- this indent plus continuation |
| 8155 | *-> here; |
| 8156 | */ |
| 8157 | if (lookfor == LOOKFOR_UNTERM |
| 8158 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8159 | { |
| 8160 | if (cont_amount > 0) |
| 8161 | amount = cont_amount; |
| 8162 | else |
| 8163 | amount += ind_continuation; |
| 8164 | break; |
| 8165 | } |
| 8166 | |
| 8167 | /* |
| 8168 | * case xx: <- line up with this case |
| 8169 | * x = 333; |
| 8170 | * case yy: |
| 8171 | */ |
| 8172 | if ( (iscase && lookfor == LOOKFOR_CASE) |
| 8173 | || (iscase && lookfor_break) |
| 8174 | || (!iscase && lookfor == LOOKFOR_SCOPEDECL)) |
| 8175 | { |
| 8176 | /* |
| 8177 | * Check that this case label is not for another |
| 8178 | * switch() |
| 8179 | */ /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8180 | if ((trypos = find_start_brace()) == NULL |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8181 | || trypos->lnum == ourscope) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8182 | { |
| 8183 | amount = get_indent(); /* XXX */ |
| 8184 | break; |
| 8185 | } |
| 8186 | continue; |
| 8187 | } |
| 8188 | |
| 8189 | n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */ |
| 8190 | |
| 8191 | /* |
| 8192 | * case xx: if (cond) <- line up with this if |
| 8193 | * y = y + 1; |
| 8194 | * -> s = 99; |
| 8195 | * |
| 8196 | * case xx: |
| 8197 | * if (cond) <- line up with this line |
| 8198 | * y = y + 1; |
| 8199 | * -> s = 99; |
| 8200 | */ |
| 8201 | if (lookfor == LOOKFOR_TERM) |
| 8202 | { |
| 8203 | if (n) |
| 8204 | amount = n; |
| 8205 | |
| 8206 | if (!lookfor_break) |
| 8207 | break; |
| 8208 | } |
| 8209 | |
| 8210 | /* |
| 8211 | * case xx: x = x + 1; <- line up with this x |
| 8212 | * -> y = y + 1; |
| 8213 | * |
| 8214 | * case xx: if (cond) <- line up with this if |
| 8215 | * -> y = y + 1; |
| 8216 | */ |
| 8217 | if (n) |
| 8218 | { |
| 8219 | amount = n; |
| 8220 | l = after_label(ml_get_curline()); |
| 8221 | if (l != NULL && cin_is_cinword(l)) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 8222 | { |
| 8223 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8224 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 8225 | else |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8226 | amount += curbuf->b_ind_level |
| 8227 | + curbuf->b_ind_no_brace; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 8228 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8229 | break; |
| 8230 | } |
| 8231 | |
| 8232 | /* |
| 8233 | * Try to get the indent of a statement before the switch |
| 8234 | * label. If nothing is found, line up relative to the |
| 8235 | * switch label. |
| 8236 | * break; <- may line up with this line |
| 8237 | * case xx: |
| 8238 | * -> y = 1; |
| 8239 | */ |
| 8240 | scope_amount = get_indent() + (iscase /* XXX */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8241 | ? curbuf->b_ind_case_code |
| 8242 | : curbuf->b_ind_scopedecl_code); |
| 8243 | lookfor = curbuf->b_ind_case_break |
| 8244 | ? LOOKFOR_NOBREAK : LOOKFOR_ANY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8245 | continue; |
| 8246 | } |
| 8247 | |
| 8248 | /* |
| 8249 | * Looking for a switch() label or C++ scope declaration, |
| 8250 | * ignore other lines, skip {}-blocks. |
| 8251 | */ |
| 8252 | if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL) |
| 8253 | { |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8254 | if (find_last_paren(l, '{', '}') |
| 8255 | && (trypos = find_start_brace()) != NULL) |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8256 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8257 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8258 | curwin->w_cursor.col = 0; |
| 8259 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8260 | continue; |
| 8261 | } |
| 8262 | |
| 8263 | /* |
| 8264 | * Ignore jump labels with nothing after them. |
| 8265 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8266 | if (!curbuf->b_ind_js && cin_islabel()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8267 | { |
| 8268 | l = after_label(ml_get_curline()); |
| 8269 | if (l == NULL || cin_nocode(l)) |
| 8270 | continue; |
| 8271 | } |
| 8272 | |
| 8273 | /* |
| 8274 | * Ignore #defines, #if, etc. |
| 8275 | * Ignore comment and empty lines. |
| 8276 | * (need to get the line again, cin_islabel() may have |
| 8277 | * unlocked it) |
| 8278 | */ |
| 8279 | l = ml_get_curline(); |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8280 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8281 | || cin_nocode(l)) |
| 8282 | continue; |
| 8283 | |
| 8284 | /* |
| 8285 | * Are we at the start of a cpp base class declaration or |
| 8286 | * constructor initialization? |
| 8287 | */ /* XXX */ |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8288 | n = FALSE; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8289 | if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0) |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8290 | { |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 8291 | n = cin_is_cpp_baseclass(&cache_cpp_baseclass); |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8292 | l = ml_get_curline(); |
| 8293 | } |
| 8294 | if (n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8295 | { |
| 8296 | if (lookfor == LOOKFOR_UNTERM) |
| 8297 | { |
| 8298 | if (cont_amount > 0) |
| 8299 | amount = cont_amount; |
| 8300 | else |
| 8301 | amount += ind_continuation; |
| 8302 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8303 | else if (theline[0] == '{') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8304 | { |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8305 | /* Need to find start of the declaration. */ |
| 8306 | lookfor = LOOKFOR_UNTERM; |
| 8307 | ind_continuation = 0; |
| 8308 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8309 | } |
| 8310 | else |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8311 | /* XXX */ |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 8312 | amount = get_baseclass_amount( |
| 8313 | cache_cpp_baseclass.lpos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8314 | break; |
| 8315 | } |
| 8316 | else if (lookfor == LOOKFOR_CPP_BASECLASS) |
| 8317 | { |
| 8318 | /* only look, whether there is a cpp base class |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8319 | * declaration or initialization before the opening brace. |
| 8320 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8321 | if (cin_isterminated(l, TRUE, FALSE)) |
| 8322 | break; |
| 8323 | else |
| 8324 | continue; |
| 8325 | } |
| 8326 | |
| 8327 | /* |
| 8328 | * What happens next depends on the line being terminated. |
| 8329 | * If terminated with a ',' only consider it terminating if |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 8330 | * there is another unterminated statement behind, eg: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8331 | * 123, |
| 8332 | * sizeof |
| 8333 | * here |
| 8334 | * Otherwise check whether it is a enumeration or structure |
| 8335 | * initialisation (not indented) or a variable declaration |
| 8336 | * (indented). |
| 8337 | */ |
| 8338 | terminated = cin_isterminated(l, FALSE, TRUE); |
| 8339 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8340 | if (js_cur_has_key) |
| 8341 | { |
| 8342 | js_cur_has_key = 0; /* only check the first line */ |
| 8343 | if (curbuf->b_ind_js && terminated == ',') |
| 8344 | { |
| 8345 | /* For Javascript we might be inside an object: |
| 8346 | * key: something, <- align with this |
| 8347 | * key: something |
| 8348 | * or: |
| 8349 | * key: something + <- align with this |
| 8350 | * something, |
| 8351 | * key: something |
| 8352 | */ |
| 8353 | lookfor = LOOKFOR_JS_KEY; |
| 8354 | } |
| 8355 | } |
| 8356 | if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l)) |
| 8357 | { |
| 8358 | amount = get_indent(); |
| 8359 | break; |
| 8360 | } |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8361 | if (lookfor == LOOKFOR_COMMA) |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8362 | { |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8363 | if (tryposBrace != NULL && tryposBrace->lnum |
| 8364 | >= curwin->w_cursor.lnum) |
| 8365 | break; |
| 8366 | if (terminated == ',') |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8367 | /* line below current line is the one that starts a |
| 8368 | * (possibly broken) line ending in a comma. */ |
| 8369 | break; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8370 | else |
| 8371 | { |
| 8372 | amount = get_indent(); |
| 8373 | if (curwin->w_cursor.lnum - 1 == ourscope) |
| 8374 | /* line above is start of the scope, thus current |
| 8375 | * line is the one that stars a (possibly broken) |
| 8376 | * line ending in a comma. */ |
| 8377 | break; |
| 8378 | } |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8379 | } |
| 8380 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8381 | if (terminated == 0 || (lookfor != LOOKFOR_UNTERM |
| 8382 | && terminated == ',')) |
| 8383 | { |
Bram Moolenaar | 089af18 | 2015-10-07 11:41:49 +0200 | [diff] [blame] | 8384 | if (lookfor != LOOKFOR_ENUM_OR_INIT && |
| 8385 | (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '[')) |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8386 | amount += ind_continuation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8387 | /* |
| 8388 | * if we're in the middle of a paren thing, |
| 8389 | * go back to the line that starts it so |
| 8390 | * we can get the right prevailing indent |
| 8391 | * if ( foo && |
| 8392 | * bar ) |
| 8393 | */ |
| 8394 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8395 | * Position the cursor over the rightmost paren, so that |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8396 | * matching it will take us back to the start of the line. |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8397 | * Ignore a match before the start of the block. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8398 | */ |
| 8399 | (void)find_last_paren(l, '(', ')'); |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8400 | trypos = find_match_paren(corr_ind_maxparen(&cur_curpos)); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8401 | if (trypos != NULL && (trypos->lnum < tryposBrace->lnum |
| 8402 | || (trypos->lnum == tryposBrace->lnum |
| 8403 | && trypos->col < tryposBrace->col))) |
| 8404 | trypos = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8405 | |
| 8406 | /* |
| 8407 | * If we are looking for ',', we also look for matching |
| 8408 | * braces. |
| 8409 | */ |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8410 | if (trypos == NULL && terminated == ',' |
| 8411 | && find_last_paren(l, '{', '}')) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8412 | trypos = find_start_brace(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8413 | |
| 8414 | if (trypos != NULL) |
| 8415 | { |
| 8416 | /* |
| 8417 | * Check if we are on a case label now. This is |
| 8418 | * handled above. |
| 8419 | * case xx: if ( asdf && |
| 8420 | * asdf) |
| 8421 | */ |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8422 | curwin->w_cursor = *trypos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8423 | l = ml_get_curline(); |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8424 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8425 | { |
| 8426 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8427 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8428 | continue; |
| 8429 | } |
| 8430 | } |
| 8431 | |
| 8432 | /* |
| 8433 | * Skip over continuation lines to find the one to get the |
| 8434 | * indent from |
| 8435 | * char *usethis = "bla\ |
| 8436 | * bla", |
| 8437 | * here; |
| 8438 | */ |
| 8439 | if (terminated == ',') |
| 8440 | { |
| 8441 | while (curwin->w_cursor.lnum > 1) |
| 8442 | { |
| 8443 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 8444 | if (*l == NUL || l[STRLEN(l) - 1] != '\\') |
| 8445 | break; |
| 8446 | --curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8447 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8448 | } |
| 8449 | } |
| 8450 | |
| 8451 | /* |
| 8452 | * Get indent and pointer to text for current line, |
| 8453 | * ignoring any jump label. XXX |
| 8454 | */ |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8455 | if (curbuf->b_ind_js) |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8456 | cur_amount = get_indent(); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8457 | else |
| 8458 | cur_amount = skip_label(curwin->w_cursor.lnum, &l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8459 | /* |
| 8460 | * If this is just above the line we are indenting, and it |
| 8461 | * starts with a '{', line it up with this line. |
| 8462 | * while (not) |
| 8463 | * -> { |
| 8464 | * } |
| 8465 | */ |
| 8466 | if (terminated != ',' && lookfor != LOOKFOR_TERM |
| 8467 | && theline[0] == '{') |
| 8468 | { |
| 8469 | amount = cur_amount; |
| 8470 | /* |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8471 | * Only add b_ind_open_extra when the current line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8472 | * doesn't start with a '{', which must have a match |
| 8473 | * in the same line (scope is the same). Probably: |
| 8474 | * { 1, 2 }, |
| 8475 | * -> { 3, 4 } |
| 8476 | */ |
| 8477 | if (*skipwhite(l) != '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8478 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8479 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8480 | if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8481 | { |
| 8482 | /* have to look back, whether it is a cpp base |
| 8483 | * class declaration or initialization */ |
| 8484 | lookfor = LOOKFOR_CPP_BASECLASS; |
| 8485 | continue; |
| 8486 | } |
| 8487 | break; |
| 8488 | } |
| 8489 | |
| 8490 | /* |
| 8491 | * Check if we are after an "if", "while", etc. |
| 8492 | * Also allow " } else". |
| 8493 | */ |
| 8494 | if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) |
| 8495 | { |
| 8496 | /* |
| 8497 | * Found an unterminated line after an if (), line up |
| 8498 | * with the last one. |
| 8499 | * if (cond) |
| 8500 | * 100 + |
| 8501 | * -> here; |
| 8502 | */ |
| 8503 | if (lookfor == LOOKFOR_UNTERM |
| 8504 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8505 | { |
| 8506 | if (cont_amount > 0) |
| 8507 | amount = cont_amount; |
| 8508 | else |
| 8509 | amount += ind_continuation; |
| 8510 | break; |
| 8511 | } |
| 8512 | |
| 8513 | /* |
| 8514 | * If this is just above the line we are indenting, we |
| 8515 | * are finished. |
| 8516 | * while (not) |
| 8517 | * -> here; |
| 8518 | * Otherwise this indent can be used when the line |
| 8519 | * before this is terminated. |
| 8520 | * yyy; |
| 8521 | * if (stat) |
| 8522 | * while (not) |
| 8523 | * xxx; |
| 8524 | * -> here; |
| 8525 | */ |
| 8526 | amount = cur_amount; |
| 8527 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8528 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8529 | if (lookfor != LOOKFOR_TERM) |
| 8530 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8531 | amount += curbuf->b_ind_level |
| 8532 | + curbuf->b_ind_no_brace; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8533 | break; |
| 8534 | } |
| 8535 | |
| 8536 | /* |
| 8537 | * Special trick: when expecting the while () after a |
| 8538 | * do, line up with the while() |
| 8539 | * do |
| 8540 | * x = 1; |
| 8541 | * -> here |
| 8542 | */ |
| 8543 | l = skipwhite(ml_get_curline()); |
| 8544 | if (cin_isdo(l)) |
| 8545 | { |
| 8546 | if (whilelevel == 0) |
| 8547 | break; |
| 8548 | --whilelevel; |
| 8549 | } |
| 8550 | |
| 8551 | /* |
| 8552 | * When searching for a terminated line, don't use the |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8553 | * one between the "if" and the matching "else". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8554 | * Need to use the scope of this "else". XXX |
| 8555 | * If whilelevel != 0 continue looking for a "do {". |
| 8556 | */ |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8557 | if (cin_iselse(l) && whilelevel == 0) |
| 8558 | { |
| 8559 | /* If we're looking at "} else", let's make sure we |
| 8560 | * find the opening brace of the enclosing scope, |
| 8561 | * not the one from "if () {". */ |
| 8562 | if (*l == '}') |
| 8563 | curwin->w_cursor.col = |
Bram Moolenaar | 9b83c2f | 2011-05-25 17:29:44 +0200 | [diff] [blame] | 8564 | (colnr_T)(l - ml_get_curline()) + 1; |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8565 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8566 | if ((trypos = find_start_brace()) == NULL |
| 8567 | || find_match(LOOKFOR_IF, trypos->lnum) |
| 8568 | == FAIL) |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8569 | break; |
| 8570 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8571 | } |
| 8572 | |
| 8573 | /* |
| 8574 | * If we're below an unterminated line that is not an |
| 8575 | * "if" or something, we may line up with this line or |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 8576 | * add something for a continuation line, depending on |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8577 | * the line before this one. |
| 8578 | */ |
| 8579 | else |
| 8580 | { |
| 8581 | /* |
| 8582 | * Found two unterminated lines on a row, line up with |
| 8583 | * the last one. |
| 8584 | * c = 99 + |
| 8585 | * 100 + |
| 8586 | * -> here; |
| 8587 | */ |
| 8588 | if (lookfor == LOOKFOR_UNTERM) |
| 8589 | { |
| 8590 | /* When line ends in a comma add extra indent */ |
| 8591 | if (terminated == ',') |
| 8592 | amount += ind_continuation; |
| 8593 | break; |
| 8594 | } |
| 8595 | |
| 8596 | if (lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8597 | { |
| 8598 | /* Found two lines ending in ',', lineup with the |
| 8599 | * lowest one, but check for cpp base class |
| 8600 | * declaration/initialization, if it is an |
| 8601 | * opening brace or we are looking just for |
| 8602 | * enumerations/initializations. */ |
| 8603 | if (terminated == ',') |
| 8604 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8605 | if (curbuf->b_ind_cpp_baseclass == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8606 | break; |
| 8607 | |
| 8608 | lookfor = LOOKFOR_CPP_BASECLASS; |
| 8609 | continue; |
| 8610 | } |
| 8611 | |
| 8612 | /* Ignore unterminated lines in between, but |
| 8613 | * reduce indent. */ |
| 8614 | if (amount > cur_amount) |
| 8615 | amount = cur_amount; |
| 8616 | } |
| 8617 | else |
| 8618 | { |
| 8619 | /* |
| 8620 | * Found first unterminated line on a row, may |
| 8621 | * line up with this line, remember its indent |
| 8622 | * 100 + |
| 8623 | * -> here; |
| 8624 | */ |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8625 | l = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8626 | amount = cur_amount; |
Bram Moolenaar | 089af18 | 2015-10-07 11:41:49 +0200 | [diff] [blame] | 8627 | |
| 8628 | n = (int)STRLEN(l); |
| 8629 | if (terminated == ',' && (*skipwhite(l) == ']' |
| 8630 | || (n >=2 && l[n - 2] == ']'))) |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8631 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8632 | |
| 8633 | /* |
| 8634 | * If previous line ends in ',', check whether we |
| 8635 | * are in an initialization or enum |
| 8636 | * struct xxx = |
| 8637 | * { |
| 8638 | * sizeof a, |
| 8639 | * 124 }; |
| 8640 | * or a normal possible continuation line. |
| 8641 | * but only, of no other statement has been found |
| 8642 | * yet. |
| 8643 | */ |
| 8644 | if (lookfor == LOOKFOR_INITIAL && terminated == ',') |
| 8645 | { |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8646 | if (curbuf->b_ind_js) |
| 8647 | { |
| 8648 | /* Search for a line ending in a comma |
| 8649 | * and line up with the line below it |
| 8650 | * (could be the current line). |
| 8651 | * some = [ |
| 8652 | * 1, <- line up here |
| 8653 | * 2, |
| 8654 | * some = [ |
| 8655 | * 3 + <- line up here |
| 8656 | * 4 * |
| 8657 | * 5, |
| 8658 | * 6, |
| 8659 | */ |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8660 | if (cin_iscomment(skipwhite(l))) |
| 8661 | break; |
| 8662 | lookfor = LOOKFOR_COMMA; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8663 | trypos = find_match_char('[', |
| 8664 | curbuf->b_ind_maxparen); |
| 8665 | if (trypos != NULL) |
| 8666 | { |
| 8667 | if (trypos->lnum |
| 8668 | == curwin->w_cursor.lnum - 1) |
| 8669 | { |
| 8670 | /* Current line is first inside |
| 8671 | * [], line up with it. */ |
| 8672 | break; |
| 8673 | } |
| 8674 | ourscope = trypos->lnum; |
| 8675 | } |
| 8676 | } |
| 8677 | else |
| 8678 | { |
| 8679 | lookfor = LOOKFOR_ENUM_OR_INIT; |
| 8680 | cont_amount = cin_first_id_amount(); |
| 8681 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8682 | } |
| 8683 | else |
| 8684 | { |
| 8685 | if (lookfor == LOOKFOR_INITIAL |
| 8686 | && *l != NUL |
| 8687 | && l[STRLEN(l) - 1] == '\\') |
| 8688 | /* XXX */ |
| 8689 | cont_amount = cin_get_equal_amount( |
| 8690 | curwin->w_cursor.lnum); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8691 | if (lookfor != LOOKFOR_TERM |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8692 | && lookfor != LOOKFOR_JS_KEY |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 8693 | && lookfor != LOOKFOR_COMMA |
| 8694 | && raw_string_start != curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8695 | lookfor = LOOKFOR_UNTERM; |
| 8696 | } |
| 8697 | } |
| 8698 | } |
| 8699 | } |
| 8700 | |
| 8701 | /* |
| 8702 | * Check if we are after a while (cond); |
| 8703 | * If so: Ignore until the matching "do". |
| 8704 | */ |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8705 | else if (cin_iswhileofdo_end(terminated)) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8706 | { |
| 8707 | /* |
| 8708 | * Found an unterminated line after a while ();, line up |
| 8709 | * with the last one. |
| 8710 | * while (cond); |
| 8711 | * 100 + <- line up with this one |
| 8712 | * -> here; |
| 8713 | */ |
| 8714 | if (lookfor == LOOKFOR_UNTERM |
| 8715 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8716 | { |
| 8717 | if (cont_amount > 0) |
| 8718 | amount = cont_amount; |
| 8719 | else |
| 8720 | amount += ind_continuation; |
| 8721 | break; |
| 8722 | } |
| 8723 | |
| 8724 | if (whilelevel == 0) |
| 8725 | { |
| 8726 | lookfor = LOOKFOR_TERM; |
| 8727 | amount = get_indent(); /* XXX */ |
| 8728 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8729 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8730 | } |
| 8731 | ++whilelevel; |
| 8732 | } |
| 8733 | |
| 8734 | /* |
| 8735 | * We are after a "normal" statement. |
| 8736 | * If we had another statement we can stop now and use the |
| 8737 | * indent of that other statement. |
| 8738 | * Otherwise the indent of the current statement may be used, |
| 8739 | * search backwards for the next "normal" statement. |
| 8740 | */ |
| 8741 | else |
| 8742 | { |
| 8743 | /* |
| 8744 | * Skip single break line, if before a switch label. It |
| 8745 | * may be lined up with the case label. |
| 8746 | */ |
| 8747 | if (lookfor == LOOKFOR_NOBREAK |
| 8748 | && cin_isbreak(skipwhite(ml_get_curline()))) |
| 8749 | { |
| 8750 | lookfor = LOOKFOR_ANY; |
| 8751 | continue; |
| 8752 | } |
| 8753 | |
| 8754 | /* |
| 8755 | * Handle "do {" line. |
| 8756 | */ |
| 8757 | if (whilelevel > 0) |
| 8758 | { |
| 8759 | l = cin_skipcomment(ml_get_curline()); |
| 8760 | if (cin_isdo(l)) |
| 8761 | { |
| 8762 | amount = get_indent(); /* XXX */ |
| 8763 | --whilelevel; |
| 8764 | continue; |
| 8765 | } |
| 8766 | } |
| 8767 | |
| 8768 | /* |
| 8769 | * Found a terminated line above an unterminated line. Add |
| 8770 | * the amount for a continuation line. |
| 8771 | * x = 1; |
| 8772 | * y = foo + |
| 8773 | * -> here; |
| 8774 | * or |
| 8775 | * int x = 1; |
| 8776 | * int foo, |
| 8777 | * -> here; |
| 8778 | */ |
| 8779 | if (lookfor == LOOKFOR_UNTERM |
| 8780 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8781 | { |
| 8782 | if (cont_amount > 0) |
| 8783 | amount = cont_amount; |
| 8784 | else |
| 8785 | amount += ind_continuation; |
| 8786 | break; |
| 8787 | } |
| 8788 | |
| 8789 | /* |
| 8790 | * Found a terminated line above a terminated line or "if" |
| 8791 | * etc. line. Use the amount of the line below us. |
| 8792 | * x = 1; x = 1; |
| 8793 | * if (asdf) y = 2; |
| 8794 | * while (asdf) ->here; |
| 8795 | * here; |
| 8796 | * ->foo; |
| 8797 | */ |
| 8798 | if (lookfor == LOOKFOR_TERM) |
| 8799 | { |
| 8800 | if (!lookfor_break && whilelevel == 0) |
| 8801 | break; |
| 8802 | } |
| 8803 | |
| 8804 | /* |
| 8805 | * First line above the one we're indenting is terminated. |
| 8806 | * To know what needs to be done look further backward for |
| 8807 | * a terminated line. |
| 8808 | */ |
| 8809 | else |
| 8810 | { |
| 8811 | /* |
| 8812 | * position the cursor over the rightmost paren, so |
| 8813 | * that matching it will take us back to the start of |
| 8814 | * the line. Helps for: |
| 8815 | * func(asdr, |
| 8816 | * asdfasdf); |
| 8817 | * here; |
| 8818 | */ |
| 8819 | term_again: |
| 8820 | l = ml_get_curline(); |
| 8821 | if (find_last_paren(l, '(', ')') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8822 | && (trypos = find_match_paren( |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8823 | curbuf->b_ind_maxparen)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8824 | { |
| 8825 | /* |
| 8826 | * Check if we are on a case label now. This is |
| 8827 | * handled above. |
| 8828 | * case xx: if ( asdf && |
| 8829 | * asdf) |
| 8830 | */ |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8831 | curwin->w_cursor = *trypos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8832 | l = ml_get_curline(); |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8833 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8834 | { |
| 8835 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8836 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8837 | continue; |
| 8838 | } |
| 8839 | } |
| 8840 | |
| 8841 | /* When aligning with the case statement, don't align |
| 8842 | * with a statement after it. |
| 8843 | * case 1: { <-- don't use this { position |
| 8844 | * stat; |
| 8845 | * } |
| 8846 | * case 2: |
| 8847 | * stat; |
| 8848 | * } |
| 8849 | */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8850 | iscase = (curbuf->b_ind_keep_case_label |
| 8851 | && cin_iscase(l, FALSE)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8852 | |
| 8853 | /* |
| 8854 | * Get indent and pointer to text for current line, |
| 8855 | * ignoring any jump label. |
| 8856 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8857 | amount = skip_label(curwin->w_cursor.lnum, &l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8858 | |
| 8859 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8860 | amount += curbuf->b_ind_open_extra; |
| 8861 | /* See remark above: "Only add b_ind_open_extra.." */ |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8862 | l = skipwhite(l); |
| 8863 | if (*l == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8864 | amount -= curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8865 | lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM; |
| 8866 | |
| 8867 | /* |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8868 | * When a terminated line starts with "else" skip to |
| 8869 | * the matching "if": |
| 8870 | * else 3; |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 8871 | * indent this; |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8872 | * Need to use the scope of this "else". XXX |
| 8873 | * If whilelevel != 0 continue looking for a "do {". |
| 8874 | */ |
| 8875 | if (lookfor == LOOKFOR_TERM |
| 8876 | && *l != '}' |
| 8877 | && cin_iselse(l) |
| 8878 | && whilelevel == 0) |
| 8879 | { |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8880 | if ((trypos = find_start_brace()) == NULL |
| 8881 | || find_match(LOOKFOR_IF, trypos->lnum) |
| 8882 | == FAIL) |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8883 | break; |
| 8884 | continue; |
| 8885 | } |
| 8886 | |
| 8887 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8888 | * If we're at the end of a block, skip to the start of |
| 8889 | * that block. |
| 8890 | */ |
Bram Moolenaar | 6d8f9c6 | 2011-11-30 13:03:28 +0100 | [diff] [blame] | 8891 | l = ml_get_curline(); |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8892 | if (find_last_paren(l, '{', '}') /* XXX */ |
| 8893 | && (trypos = find_start_brace()) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8894 | { |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8895 | curwin->w_cursor = *trypos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8896 | /* if not "else {" check for terminated again */ |
| 8897 | /* but skip block for "} else {" */ |
| 8898 | l = cin_skipcomment(ml_get_curline()); |
| 8899 | if (*l == '}' || !cin_iselse(l)) |
| 8900 | goto term_again; |
| 8901 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8902 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8903 | } |
| 8904 | } |
| 8905 | } |
| 8906 | } |
| 8907 | } |
| 8908 | } |
| 8909 | |
| 8910 | /* add extra indent for a comment */ |
| 8911 | if (cin_iscomment(theline)) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8912 | amount += curbuf->b_ind_comment; |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 8913 | |
| 8914 | /* subtract extra left-shift for jump labels */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8915 | if (curbuf->b_ind_jump_label > 0 && original_line_islabel) |
| 8916 | amount -= curbuf->b_ind_jump_label; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8917 | |
| 8918 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8919 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8920 | |
| 8921 | /* |
| 8922 | * ok -- we're not inside any sort of structure at all! |
| 8923 | * |
| 8924 | * This means we're at the top level, and everything should |
| 8925 | * basically just match where the previous line is, except |
| 8926 | * for the lines immediately following a function declaration, |
| 8927 | * which are K&R-style parameters and need to be indented. |
| 8928 | * |
| 8929 | * if our line starts with an open brace, forget about any |
| 8930 | * prevailing indent and make sure it looks like the start |
| 8931 | * of a function |
| 8932 | */ |
| 8933 | |
| 8934 | if (theline[0] == '{') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8935 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8936 | amount = curbuf->b_ind_first_open; |
| 8937 | goto theend; |
| 8938 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8939 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8940 | /* |
| 8941 | * If the NEXT line is a function declaration, the current |
| 8942 | * line needs to be indented as a function type spec. |
| 8943 | * Don't do this if the current line looks like a comment or if the |
| 8944 | * current line is terminated, ie. ends in ';', or if the current line |
| 8945 | * contains { or }: "void f() {\n if (1)" |
| 8946 | */ |
| 8947 | if (cur_curpos.lnum < curbuf->b_ml.ml_line_count |
| 8948 | && !cin_nocode(theline) |
| 8949 | && vim_strchr(theline, '{') == NULL |
| 8950 | && vim_strchr(theline, '}') == NULL |
| 8951 | && !cin_ends_in(theline, (char_u *)":", NULL) |
| 8952 | && !cin_ends_in(theline, (char_u *)",", NULL) |
| 8953 | && cin_isfuncdecl(NULL, cur_curpos.lnum + 1, |
| 8954 | cur_curpos.lnum + 1) |
| 8955 | && !cin_isterminated(theline, FALSE, TRUE)) |
| 8956 | { |
| 8957 | amount = curbuf->b_ind_func_type; |
| 8958 | goto theend; |
| 8959 | } |
| 8960 | |
| 8961 | /* search backwards until we find something we recognize */ |
| 8962 | amount = 0; |
| 8963 | curwin->w_cursor = cur_curpos; |
| 8964 | while (curwin->w_cursor.lnum > 1) |
| 8965 | { |
| 8966 | curwin->w_cursor.lnum--; |
| 8967 | curwin->w_cursor.col = 0; |
| 8968 | |
| 8969 | l = ml_get_curline(); |
| 8970 | |
| 8971 | /* |
| 8972 | * If we're in a comment or raw string now, skip to the start |
| 8973 | * of it. |
| 8974 | */ /* XXX */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 8975 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8976 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8977 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 8978 | curwin->w_cursor.col = 0; |
| 8979 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8980 | } |
| 8981 | |
| 8982 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8983 | * Are we at the start of a cpp base class declaration or |
| 8984 | * constructor initialization? |
| 8985 | */ /* XXX */ |
| 8986 | n = FALSE; |
| 8987 | if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8988 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8989 | n = cin_is_cpp_baseclass(&cache_cpp_baseclass); |
| 8990 | l = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8991 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8992 | if (n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8993 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8994 | /* XXX */ |
| 8995 | amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col); |
| 8996 | break; |
| 8997 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8998 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8999 | /* |
| 9000 | * Skip preprocessor directives and blank lines. |
| 9001 | */ |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 9002 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9003 | continue; |
| 9004 | |
| 9005 | if (cin_nocode(l)) |
| 9006 | continue; |
| 9007 | |
| 9008 | /* |
| 9009 | * If the previous line ends in ',', use one level of |
| 9010 | * indentation: |
| 9011 | * int foo, |
| 9012 | * bar; |
| 9013 | * do this before checking for '}' in case of eg. |
| 9014 | * enum foobar |
| 9015 | * { |
| 9016 | * ... |
| 9017 | * } foo, |
| 9018 | * bar; |
| 9019 | */ |
| 9020 | n = 0; |
| 9021 | if (cin_ends_in(l, (char_u *)",", NULL) |
| 9022 | || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\')) |
| 9023 | { |
| 9024 | /* take us back to opening paren */ |
| 9025 | if (find_last_paren(l, '(', ')') |
| 9026 | && (trypos = find_match_paren( |
| 9027 | curbuf->b_ind_maxparen)) != NULL) |
| 9028 | curwin->w_cursor = *trypos; |
| 9029 | |
| 9030 | /* For a line ending in ',' that is a continuation line go |
| 9031 | * back to the first line with a backslash: |
| 9032 | * char *foo = "bla\ |
| 9033 | * bla", |
| 9034 | * here; |
| 9035 | */ |
| 9036 | while (n == 0 && curwin->w_cursor.lnum > 1) |
| 9037 | { |
| 9038 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 9039 | if (*l == NUL || l[STRLEN(l) - 1] != '\\') |
| 9040 | break; |
| 9041 | --curwin->w_cursor.lnum; |
| 9042 | curwin->w_cursor.col = 0; |
| 9043 | } |
| 9044 | |
| 9045 | amount = get_indent(); /* XXX */ |
| 9046 | |
| 9047 | if (amount == 0) |
| 9048 | amount = cin_first_id_amount(); |
| 9049 | if (amount == 0) |
| 9050 | amount = ind_continuation; |
| 9051 | break; |
| 9052 | } |
| 9053 | |
| 9054 | /* |
| 9055 | * If the line looks like a function declaration, and we're |
| 9056 | * not in a comment, put it the left margin. |
| 9057 | */ |
| 9058 | if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */ |
| 9059 | break; |
| 9060 | l = ml_get_curline(); |
| 9061 | |
| 9062 | /* |
| 9063 | * Finding the closing '}' of a previous function. Put |
| 9064 | * current line at the left margin. For when 'cino' has "fs". |
| 9065 | */ |
| 9066 | if (*skipwhite(l) == '}') |
| 9067 | break; |
| 9068 | |
| 9069 | /* (matching {) |
| 9070 | * If the previous line ends on '};' (maybe followed by |
| 9071 | * comments) align at column 0. For example: |
| 9072 | * char *string_array[] = { "foo", |
| 9073 | * / * x * / "b};ar" }; / * foobar * / |
| 9074 | */ |
| 9075 | if (cin_ends_in(l, (char_u *)"};", NULL)) |
| 9076 | break; |
| 9077 | |
| 9078 | /* |
| 9079 | * If the previous line ends on '[' we are probably in an |
| 9080 | * array constant: |
| 9081 | * something = [ |
| 9082 | * 234, <- extra indent |
| 9083 | */ |
| 9084 | if (cin_ends_in(l, (char_u *)"[", NULL)) |
| 9085 | { |
| 9086 | amount = get_indent() + ind_continuation; |
| 9087 | break; |
| 9088 | } |
| 9089 | |
| 9090 | /* |
| 9091 | * Find a line only has a semicolon that belongs to a previous |
| 9092 | * line ending in '}', e.g. before an #endif. Don't increase |
| 9093 | * indent then. |
| 9094 | */ |
| 9095 | if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1)) |
| 9096 | { |
| 9097 | pos_T curpos_save = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9098 | |
| 9099 | while (curwin->w_cursor.lnum > 1) |
| 9100 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9101 | look = ml_get(--curwin->w_cursor.lnum); |
| 9102 | if (!(cin_nocode(look) || cin_ispreproc_cont( |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 9103 | &look, &curwin->w_cursor.lnum, &amount))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9104 | break; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9105 | } |
| 9106 | if (curwin->w_cursor.lnum > 0 |
| 9107 | && cin_ends_in(look, (char_u *)"}", NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9108 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9109 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9110 | curwin->w_cursor = curpos_save; |
| 9111 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9112 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9113 | /* |
| 9114 | * If the PREVIOUS line is a function declaration, the current |
| 9115 | * line (and the ones that follow) needs to be indented as |
| 9116 | * parameters. |
| 9117 | */ |
| 9118 | if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
| 9119 | { |
| 9120 | amount = curbuf->b_ind_param; |
| 9121 | break; |
| 9122 | } |
| 9123 | |
| 9124 | /* |
| 9125 | * If the previous line ends in ';' and the line before the |
| 9126 | * previous line ends in ',' or '\', ident to column zero: |
| 9127 | * int foo, |
| 9128 | * bar; |
| 9129 | * indent_to_0 here; |
| 9130 | */ |
| 9131 | if (cin_ends_in(l, (char_u *)";", NULL)) |
| 9132 | { |
| 9133 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 9134 | if (cin_ends_in(l, (char_u *)",", NULL) |
| 9135 | || (*l != NUL && l[STRLEN(l) - 1] == '\\')) |
| 9136 | break; |
| 9137 | l = ml_get_curline(); |
| 9138 | } |
| 9139 | |
| 9140 | /* |
| 9141 | * Doesn't look like anything interesting -- so just |
| 9142 | * use the indent of this line. |
| 9143 | * |
| 9144 | * Position the cursor over the rightmost paren, so that |
| 9145 | * matching it will take us back to the start of the line. |
| 9146 | */ |
| 9147 | find_last_paren(l, '(', ')'); |
| 9148 | |
| 9149 | if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
| 9150 | curwin->w_cursor = *trypos; |
| 9151 | amount = get_indent(); /* XXX */ |
| 9152 | break; |
| 9153 | } |
| 9154 | |
| 9155 | /* add extra indent for a comment */ |
| 9156 | if (cin_iscomment(theline)) |
| 9157 | amount += curbuf->b_ind_comment; |
| 9158 | |
| 9159 | /* add extra indent if the previous line ended in a backslash: |
| 9160 | * "asdfasdf\ |
| 9161 | * here"; |
| 9162 | * char *foo = "asdf\ |
| 9163 | * here"; |
| 9164 | */ |
| 9165 | if (cur_curpos.lnum > 1) |
| 9166 | { |
| 9167 | l = ml_get(cur_curpos.lnum - 1); |
| 9168 | if (*l != NUL && l[STRLEN(l) - 1] == '\\') |
| 9169 | { |
| 9170 | cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1); |
| 9171 | if (cur_amount > 0) |
| 9172 | amount = cur_amount; |
| 9173 | else if (cur_amount == 0) |
| 9174 | amount += ind_continuation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9175 | } |
| 9176 | } |
| 9177 | |
| 9178 | theend: |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9179 | if (amount < 0) |
| 9180 | amount = 0; |
| 9181 | |
| 9182 | laterend: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9183 | /* put the cursor back where it belongs */ |
| 9184 | curwin->w_cursor = cur_curpos; |
| 9185 | |
| 9186 | vim_free(linecopy); |
| 9187 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9188 | return amount; |
| 9189 | } |
| 9190 | |
| 9191 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9192 | find_match(int lookfor, linenr_T ourscope) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9193 | { |
| 9194 | char_u *look; |
| 9195 | pos_T *theirscope; |
| 9196 | char_u *mightbeif; |
| 9197 | int elselevel; |
| 9198 | int whilelevel; |
| 9199 | |
| 9200 | if (lookfor == LOOKFOR_IF) |
| 9201 | { |
| 9202 | elselevel = 1; |
| 9203 | whilelevel = 0; |
| 9204 | } |
| 9205 | else |
| 9206 | { |
| 9207 | elselevel = 0; |
| 9208 | whilelevel = 1; |
| 9209 | } |
| 9210 | |
| 9211 | curwin->w_cursor.col = 0; |
| 9212 | |
| 9213 | while (curwin->w_cursor.lnum > ourscope + 1) |
| 9214 | { |
| 9215 | curwin->w_cursor.lnum--; |
| 9216 | curwin->w_cursor.col = 0; |
| 9217 | |
| 9218 | look = cin_skipcomment(ml_get_curline()); |
| 9219 | if (cin_iselse(look) |
| 9220 | || cin_isif(look) |
| 9221 | || cin_isdo(look) /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9222 | || cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9223 | { |
| 9224 | /* |
| 9225 | * if we've gone outside the braces entirely, |
| 9226 | * we must be out of scope... |
| 9227 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9228 | theirscope = find_start_brace(); /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9229 | if (theirscope == NULL) |
| 9230 | break; |
| 9231 | |
| 9232 | /* |
| 9233 | * and if the brace enclosing this is further |
| 9234 | * back than the one enclosing the else, we're |
| 9235 | * out of luck too. |
| 9236 | */ |
| 9237 | if (theirscope->lnum < ourscope) |
| 9238 | break; |
| 9239 | |
| 9240 | /* |
| 9241 | * and if they're enclosed in a *deeper* brace, |
| 9242 | * then we can ignore it because it's in a |
| 9243 | * different scope... |
| 9244 | */ |
| 9245 | if (theirscope->lnum > ourscope) |
| 9246 | continue; |
| 9247 | |
| 9248 | /* |
| 9249 | * if it was an "else" (that's not an "else if") |
| 9250 | * then we need to go back to another if, so |
| 9251 | * increment elselevel |
| 9252 | */ |
| 9253 | look = cin_skipcomment(ml_get_curline()); |
| 9254 | if (cin_iselse(look)) |
| 9255 | { |
| 9256 | mightbeif = cin_skipcomment(look + 4); |
| 9257 | if (!cin_isif(mightbeif)) |
| 9258 | ++elselevel; |
| 9259 | continue; |
| 9260 | } |
| 9261 | |
| 9262 | /* |
| 9263 | * if it was a "while" then we need to go back to |
| 9264 | * another "do", so increment whilelevel. XXX |
| 9265 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9266 | if (cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9267 | { |
| 9268 | ++whilelevel; |
| 9269 | continue; |
| 9270 | } |
| 9271 | |
| 9272 | /* If it's an "if" decrement elselevel */ |
| 9273 | look = cin_skipcomment(ml_get_curline()); |
| 9274 | if (cin_isif(look)) |
| 9275 | { |
| 9276 | elselevel--; |
| 9277 | /* |
| 9278 | * When looking for an "if" ignore "while"s that |
| 9279 | * get in the way. |
| 9280 | */ |
| 9281 | if (elselevel == 0 && lookfor == LOOKFOR_IF) |
| 9282 | whilelevel = 0; |
| 9283 | } |
| 9284 | |
| 9285 | /* If it's a "do" decrement whilelevel */ |
| 9286 | if (cin_isdo(look)) |
| 9287 | whilelevel--; |
| 9288 | |
| 9289 | /* |
| 9290 | * if we've used up all the elses, then |
| 9291 | * this must be the if that we want! |
| 9292 | * match the indent level of that if. |
| 9293 | */ |
| 9294 | if (elselevel <= 0 && whilelevel <= 0) |
| 9295 | { |
| 9296 | return OK; |
| 9297 | } |
| 9298 | } |
| 9299 | } |
| 9300 | return FAIL; |
| 9301 | } |
| 9302 | |
| 9303 | # if defined(FEAT_EVAL) || defined(PROTO) |
| 9304 | /* |
| 9305 | * Get indent level from 'indentexpr'. |
| 9306 | */ |
| 9307 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9308 | get_expr_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9309 | { |
Bram Moolenaar | 97db554 | 2017-04-21 23:18:26 +0200 | [diff] [blame] | 9310 | int indent = -1; |
Bram Moolenaar | a701b3b | 2017-04-20 22:57:27 +0200 | [diff] [blame] | 9311 | char_u *inde_copy; |
Bram Moolenaar | 8fe8d9e | 2013-02-13 16:10:17 +0100 | [diff] [blame] | 9312 | pos_T save_pos; |
| 9313 | colnr_T save_curswant; |
| 9314 | int save_set_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9315 | int save_State; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 9316 | int use_sandbox = was_set_insecurely((char_u *)"indentexpr", |
| 9317 | OPT_LOCAL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9318 | |
Bram Moolenaar | 8fe8d9e | 2013-02-13 16:10:17 +0100 | [diff] [blame] | 9319 | /* Save and restore cursor position and curswant, in case it was changed |
| 9320 | * via :normal commands */ |
| 9321 | save_pos = curwin->w_cursor; |
| 9322 | save_curswant = curwin->w_curswant; |
| 9323 | save_set_curswant = curwin->w_set_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9324 | set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 9325 | if (use_sandbox) |
| 9326 | ++sandbox; |
| 9327 | ++textlock; |
Bram Moolenaar | a701b3b | 2017-04-20 22:57:27 +0200 | [diff] [blame] | 9328 | |
| 9329 | /* Need to make a copy, the 'indentexpr' option could be changed while |
| 9330 | * evaluating it. */ |
| 9331 | inde_copy = vim_strsave(curbuf->b_p_inde); |
| 9332 | if (inde_copy != NULL) |
| 9333 | { |
| 9334 | indent = (int)eval_to_number(inde_copy); |
| 9335 | vim_free(inde_copy); |
| 9336 | } |
| 9337 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 9338 | if (use_sandbox) |
| 9339 | --sandbox; |
| 9340 | --textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9341 | |
| 9342 | /* Restore the cursor position so that 'indentexpr' doesn't need to. |
| 9343 | * Pretend to be in Insert mode, allow cursor past end of line for "o" |
| 9344 | * command. */ |
| 9345 | save_State = State; |
| 9346 | State = INSERT; |
Bram Moolenaar | 8fe8d9e | 2013-02-13 16:10:17 +0100 | [diff] [blame] | 9347 | curwin->w_cursor = save_pos; |
| 9348 | curwin->w_curswant = save_curswant; |
| 9349 | curwin->w_set_curswant = save_set_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9350 | check_cursor(); |
| 9351 | State = save_State; |
| 9352 | |
| 9353 | /* If there is an error, just keep the current indent. */ |
| 9354 | if (indent < 0) |
| 9355 | indent = get_indent(); |
| 9356 | |
| 9357 | return indent; |
| 9358 | } |
| 9359 | # endif |
| 9360 | |
| 9361 | #endif /* FEAT_CINDENT */ |
| 9362 | |
| 9363 | #if defined(FEAT_LISP) || defined(PROTO) |
| 9364 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 9365 | static int lisp_match(char_u *p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9366 | |
| 9367 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9368 | lisp_match(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9369 | { |
| 9370 | char_u buf[LSIZE]; |
| 9371 | int len; |
Bram Moolenaar | af6c131 | 2014-03-12 18:55:58 +0100 | [diff] [blame] | 9372 | 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] | 9373 | |
| 9374 | while (*word != NUL) |
| 9375 | { |
| 9376 | (void)copy_option_part(&word, buf, LSIZE, ","); |
| 9377 | len = (int)STRLEN(buf); |
| 9378 | if (STRNCMP(buf, p, len) == 0 && p[len] == ' ') |
| 9379 | return TRUE; |
| 9380 | } |
| 9381 | return FALSE; |
| 9382 | } |
| 9383 | |
| 9384 | /* |
| 9385 | * When 'p' is present in 'cpoptions, a Vi compatible method is used. |
| 9386 | * The incompatible newer method is quite a bit better at indenting |
| 9387 | * code in lisp-like languages than the traditional one; it's still |
| 9388 | * mostly heuristics however -- Dirk van Deun, dirk@rave.org |
| 9389 | * |
| 9390 | * TODO: |
| 9391 | * Findmatch() should be adapted for lisp, also to make showmatch |
| 9392 | * work correctly: now (v5.3) it seems all C/C++ oriented: |
| 9393 | * - it does not recognize the #\( and #\) notations as character literals |
| 9394 | * - it doesn't know about comments starting with a semicolon |
| 9395 | * - it incorrectly interprets '(' as a character literal |
| 9396 | * All this messes up get_lisp_indent in some rare cases. |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9397 | * Update from Sergey Khorev: |
| 9398 | * I tried to fix the first two issues. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9399 | */ |
| 9400 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9401 | get_lisp_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9402 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9403 | pos_T *pos, realpos, paren; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9404 | int amount; |
| 9405 | char_u *that; |
| 9406 | colnr_T col; |
| 9407 | colnr_T firsttry; |
| 9408 | int parencount, quotecount; |
| 9409 | int vi_lisp; |
| 9410 | |
| 9411 | /* Set vi_lisp to use the vi-compatible method */ |
| 9412 | vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL); |
| 9413 | |
| 9414 | realpos = curwin->w_cursor; |
| 9415 | curwin->w_cursor.col = 0; |
| 9416 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9417 | if ((pos = findmatch(NULL, '(')) == NULL) |
| 9418 | pos = findmatch(NULL, '['); |
| 9419 | else |
| 9420 | { |
| 9421 | paren = *pos; |
| 9422 | pos = findmatch(NULL, '['); |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 9423 | if (pos == NULL || LT_POSP(pos, &paren)) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9424 | pos = &paren; |
| 9425 | } |
| 9426 | if (pos != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9427 | { |
| 9428 | /* Extra trick: Take the indent of the first previous non-white |
| 9429 | * line that is at the same () level. */ |
| 9430 | amount = -1; |
| 9431 | parencount = 0; |
| 9432 | |
| 9433 | while (--curwin->w_cursor.lnum >= pos->lnum) |
| 9434 | { |
| 9435 | if (linewhite(curwin->w_cursor.lnum)) |
| 9436 | continue; |
| 9437 | for (that = ml_get_curline(); *that != NUL; ++that) |
| 9438 | { |
| 9439 | if (*that == ';') |
| 9440 | { |
| 9441 | while (*(that + 1) != NUL) |
| 9442 | ++that; |
| 9443 | continue; |
| 9444 | } |
| 9445 | if (*that == '\\') |
| 9446 | { |
| 9447 | if (*(that + 1) != NUL) |
| 9448 | ++that; |
| 9449 | continue; |
| 9450 | } |
| 9451 | if (*that == '"' && *(that + 1) != NUL) |
| 9452 | { |
Bram Moolenaar | 15ff6c1 | 2006-09-15 18:18:09 +0000 | [diff] [blame] | 9453 | while (*++that && *that != '"') |
| 9454 | { |
| 9455 | /* skipping escaped characters in the string */ |
| 9456 | if (*that == '\\') |
| 9457 | { |
| 9458 | if (*++that == NUL) |
| 9459 | break; |
| 9460 | if (that[1] == NUL) |
| 9461 | { |
| 9462 | ++that; |
| 9463 | break; |
| 9464 | } |
| 9465 | } |
| 9466 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9467 | } |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9468 | if (*that == '(' || *that == '[') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9469 | ++parencount; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9470 | else if (*that == ')' || *that == ']') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9471 | --parencount; |
| 9472 | } |
| 9473 | if (parencount == 0) |
| 9474 | { |
| 9475 | amount = get_indent(); |
| 9476 | break; |
| 9477 | } |
| 9478 | } |
| 9479 | |
| 9480 | if (amount == -1) |
| 9481 | { |
| 9482 | curwin->w_cursor.lnum = pos->lnum; |
| 9483 | curwin->w_cursor.col = pos->col; |
| 9484 | col = pos->col; |
| 9485 | |
| 9486 | that = ml_get_curline(); |
| 9487 | |
| 9488 | if (vi_lisp && get_indent() == 0) |
| 9489 | amount = 2; |
| 9490 | else |
| 9491 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9492 | char_u *line = that; |
| 9493 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9494 | amount = 0; |
| 9495 | while (*that && col) |
| 9496 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9497 | amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9498 | col--; |
| 9499 | } |
| 9500 | |
| 9501 | /* |
| 9502 | * Some keywords require "body" indenting rules (the |
| 9503 | * non-standard-lisp ones are Scheme special forms): |
| 9504 | * |
| 9505 | * (let ((a 1)) instead (let ((a 1)) |
| 9506 | * (...)) of (...)) |
| 9507 | */ |
| 9508 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9509 | if (!vi_lisp && (*that == '(' || *that == '[') |
| 9510 | && lisp_match(that + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9511 | amount += 2; |
| 9512 | else |
| 9513 | { |
| 9514 | that++; |
| 9515 | amount++; |
| 9516 | firsttry = amount; |
| 9517 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 9518 | while (VIM_ISWHITE(*that)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9519 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9520 | amount += lbr_chartabsize(line, that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9521 | ++that; |
| 9522 | } |
| 9523 | |
| 9524 | if (*that && *that != ';') /* not a comment line */ |
| 9525 | { |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 9526 | /* test *that != '(' to accommodate first let/do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9527 | * argument if it is more than one line */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9528 | if (!vi_lisp && *that != '(' && *that != '[') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9529 | firsttry++; |
| 9530 | |
| 9531 | parencount = 0; |
| 9532 | quotecount = 0; |
| 9533 | |
| 9534 | if (vi_lisp |
| 9535 | || (*that != '"' |
| 9536 | && *that != '\'' |
| 9537 | && *that != '#' |
| 9538 | && (*that < '0' || *that > '9'))) |
| 9539 | { |
| 9540 | while (*that |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 9541 | && (!VIM_ISWHITE(*that) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9542 | || quotecount |
| 9543 | || parencount) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9544 | && (!((*that == '(' || *that == '[') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9545 | && !quotecount |
| 9546 | && !parencount |
| 9547 | && vi_lisp))) |
| 9548 | { |
| 9549 | if (*that == '"') |
| 9550 | quotecount = !quotecount; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9551 | if ((*that == '(' || *that == '[') |
| 9552 | && !quotecount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9553 | ++parencount; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9554 | if ((*that == ')' || *that == ']') |
| 9555 | && !quotecount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9556 | --parencount; |
| 9557 | if (*that == '\\' && *(that+1) != NUL) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9558 | amount += lbr_chartabsize_adv( |
| 9559 | line, &that, (colnr_T)amount); |
| 9560 | amount += lbr_chartabsize_adv( |
| 9561 | line, &that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9562 | } |
| 9563 | } |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 9564 | while (VIM_ISWHITE(*that)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9565 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9566 | amount += lbr_chartabsize( |
| 9567 | line, that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9568 | that++; |
| 9569 | } |
| 9570 | if (!*that || *that == ';') |
| 9571 | amount = firsttry; |
| 9572 | } |
| 9573 | } |
| 9574 | } |
| 9575 | } |
| 9576 | } |
| 9577 | else |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9578 | amount = 0; /* no matching '(' or '[' found, use zero indent */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9579 | |
| 9580 | curwin->w_cursor = realpos; |
| 9581 | |
| 9582 | return amount; |
| 9583 | } |
| 9584 | #endif /* FEAT_LISP */ |
| 9585 | |
| 9586 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9587 | prepare_to_exit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9588 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 9589 | #if defined(SIGHUP) && defined(SIG_IGN) |
| 9590 | /* Ignore SIGHUP, because a dropped connection causes a read error, which |
| 9591 | * makes Vim exit and then handling SIGHUP causes various reentrance |
| 9592 | * problems. */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 9593 | signal(SIGHUP, SIG_IGN); |
| 9594 | #endif |
| 9595 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9596 | #ifdef FEAT_GUI |
| 9597 | if (gui.in_use) |
| 9598 | { |
| 9599 | gui.dying = TRUE; |
| 9600 | out_trash(); /* trash any pending output */ |
| 9601 | } |
| 9602 | else |
| 9603 | #endif |
| 9604 | { |
| 9605 | windgoto((int)Rows - 1, 0); |
| 9606 | |
| 9607 | /* |
| 9608 | * Switch terminal mode back now, so messages end up on the "normal" |
| 9609 | * screen (if there are two screens). |
| 9610 | */ |
| 9611 | settmode(TMODE_COOK); |
Bram Moolenaar | cea912a | 2016-10-12 14:20:24 +0200 | [diff] [blame] | 9612 | stoptermcap(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9613 | out_flush(); |
| 9614 | } |
| 9615 | } |
| 9616 | |
| 9617 | /* |
| 9618 | * Preserve files and exit. |
| 9619 | * When called IObuff must contain a message. |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 9620 | * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe |
| 9621 | * functions, such as allocating memory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9622 | */ |
| 9623 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9624 | preserve_exit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9625 | { |
| 9626 | buf_T *buf; |
| 9627 | |
| 9628 | prepare_to_exit(); |
| 9629 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9630 | /* Setting this will prevent free() calls. That avoids calling free() |
| 9631 | * recursively when free() was invoked with a bad pointer. */ |
| 9632 | really_exiting = TRUE; |
| 9633 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9634 | out_str(IObuff); |
| 9635 | screen_start(); /* don't know where cursor is now */ |
| 9636 | out_flush(); |
| 9637 | |
| 9638 | ml_close_notmod(); /* close all not-modified buffers */ |
| 9639 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 9640 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9641 | { |
| 9642 | if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) |
| 9643 | { |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 9644 | OUT_STR("Vim: preserving files...\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9645 | screen_start(); /* don't know where cursor is now */ |
| 9646 | out_flush(); |
| 9647 | ml_sync_all(FALSE, FALSE); /* preserve all swap files */ |
| 9648 | break; |
| 9649 | } |
| 9650 | } |
| 9651 | |
| 9652 | ml_close_all(FALSE); /* close all memfiles, without deleting */ |
| 9653 | |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 9654 | OUT_STR("Vim: Finished.\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9655 | |
| 9656 | getout(1); |
| 9657 | } |
| 9658 | |
| 9659 | /* |
| 9660 | * return TRUE if "fname" exists. |
| 9661 | */ |
| 9662 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9663 | vim_fexists(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9664 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 9665 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9666 | |
| 9667 | if (mch_stat((char *)fname, &st)) |
| 9668 | return FALSE; |
| 9669 | return TRUE; |
| 9670 | } |
| 9671 | |
| 9672 | /* |
| 9673 | * Check for CTRL-C pressed, but only once in a while. |
| 9674 | * Should be used instead of ui_breakcheck() for functions that check for |
| 9675 | * each line in the file. Calling ui_breakcheck() each time takes too much |
| 9676 | * time, because it can be a system call. |
| 9677 | */ |
| 9678 | |
| 9679 | #ifndef BREAKCHECK_SKIP |
| 9680 | # ifdef FEAT_GUI /* assume the GUI only runs on fast computers */ |
| 9681 | # define BREAKCHECK_SKIP 200 |
| 9682 | # else |
| 9683 | # define BREAKCHECK_SKIP 32 |
| 9684 | # endif |
| 9685 | #endif |
| 9686 | |
| 9687 | static int breakcheck_count = 0; |
| 9688 | |
| 9689 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9690 | line_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9691 | { |
| 9692 | if (++breakcheck_count >= BREAKCHECK_SKIP) |
| 9693 | { |
| 9694 | breakcheck_count = 0; |
| 9695 | ui_breakcheck(); |
| 9696 | } |
| 9697 | } |
| 9698 | |
| 9699 | /* |
| 9700 | * Like line_breakcheck() but check 10 times less often. |
| 9701 | */ |
| 9702 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9703 | fast_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9704 | { |
| 9705 | if (++breakcheck_count >= BREAKCHECK_SKIP * 10) |
| 9706 | { |
| 9707 | breakcheck_count = 0; |
| 9708 | ui_breakcheck(); |
| 9709 | } |
| 9710 | } |
| 9711 | |
| 9712 | /* |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 9713 | * Invoke expand_wildcards() for one pattern. |
| 9714 | * Expand items like "%:h" before the expansion. |
| 9715 | * Returns OK or FAIL. |
| 9716 | */ |
| 9717 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9718 | expand_wildcards_eval( |
| 9719 | char_u **pat, /* pointer to input pattern */ |
| 9720 | int *num_file, /* resulting number of files */ |
| 9721 | char_u ***file, /* array of resulting files */ |
| 9722 | int flags) /* EW_DIR, etc. */ |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 9723 | { |
| 9724 | int ret = FAIL; |
| 9725 | char_u *eval_pat = NULL; |
| 9726 | char_u *exp_pat = *pat; |
| 9727 | char_u *ignored_msg; |
| 9728 | int usedlen; |
| 9729 | |
| 9730 | if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') |
| 9731 | { |
| 9732 | ++emsg_off; |
| 9733 | eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, |
| 9734 | NULL, &ignored_msg, NULL); |
| 9735 | --emsg_off; |
| 9736 | if (eval_pat != NULL) |
| 9737 | exp_pat = concat_str(eval_pat, exp_pat + usedlen); |
| 9738 | } |
| 9739 | |
| 9740 | if (exp_pat != NULL) |
| 9741 | ret = expand_wildcards(1, &exp_pat, num_file, file, flags); |
| 9742 | |
| 9743 | if (eval_pat != NULL) |
| 9744 | { |
| 9745 | vim_free(exp_pat); |
| 9746 | vim_free(eval_pat); |
| 9747 | } |
| 9748 | |
| 9749 | return ret; |
| 9750 | } |
| 9751 | |
| 9752 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9753 | * Expand wildcards. Calls gen_expand_wildcards() and removes files matching |
| 9754 | * 'wildignore'. |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9755 | * 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] | 9756 | */ |
| 9757 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9758 | expand_wildcards( |
| 9759 | int num_pat, /* number of input patterns */ |
| 9760 | char_u **pat, /* array of input patterns */ |
| 9761 | int *num_files, /* resulting number of files */ |
| 9762 | char_u ***files, /* array of resulting files */ |
| 9763 | int flags) /* EW_DIR, etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9764 | { |
| 9765 | int retval; |
| 9766 | int i, j; |
| 9767 | char_u *p; |
| 9768 | int non_suf_match; /* number without matching suffix */ |
| 9769 | |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9770 | retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9771 | |
| 9772 | /* When keeping all matches, return here */ |
Bram Moolenaar | 9e193ac | 2010-07-19 23:11:27 +0200 | [diff] [blame] | 9773 | if ((flags & EW_KEEPALL) || retval == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9774 | return retval; |
| 9775 | |
| 9776 | #ifdef FEAT_WILDIGN |
| 9777 | /* |
| 9778 | * Remove names that match 'wildignore'. |
| 9779 | */ |
| 9780 | if (*p_wig) |
| 9781 | { |
| 9782 | char_u *ffname; |
| 9783 | |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9784 | /* check all files in (*files)[] */ |
| 9785 | for (i = 0; i < *num_files; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9786 | { |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9787 | ffname = FullName_save((*files)[i], FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9788 | if (ffname == NULL) /* out of memory */ |
| 9789 | break; |
| 9790 | # ifdef VMS |
| 9791 | vms_remove_version(ffname); |
| 9792 | # endif |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9793 | if (match_file_list(p_wig, (*files)[i], ffname)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9794 | { |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 9795 | /* remove this matching file from the list */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9796 | vim_free((*files)[i]); |
| 9797 | for (j = i; j + 1 < *num_files; ++j) |
| 9798 | (*files)[j] = (*files)[j + 1]; |
| 9799 | --*num_files; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9800 | --i; |
| 9801 | } |
| 9802 | vim_free(ffname); |
| 9803 | } |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9804 | |
| 9805 | /* If the number of matches is now zero, we fail. */ |
| 9806 | if (*num_files == 0) |
| 9807 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 9808 | VIM_CLEAR(*files); |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9809 | return FAIL; |
| 9810 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9811 | } |
| 9812 | #endif |
| 9813 | |
| 9814 | /* |
| 9815 | * Move the names where 'suffixes' match to the end. |
| 9816 | */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9817 | if (*num_files > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9818 | { |
| 9819 | non_suf_match = 0; |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9820 | for (i = 0; i < *num_files; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9821 | { |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9822 | if (!match_suffix((*files)[i])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9823 | { |
| 9824 | /* |
| 9825 | * Move the name without matching suffix to the front |
| 9826 | * of the list. |
| 9827 | */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9828 | p = (*files)[i]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9829 | for (j = i; j > non_suf_match; --j) |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9830 | (*files)[j] = (*files)[j - 1]; |
| 9831 | (*files)[non_suf_match++] = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9832 | } |
| 9833 | } |
| 9834 | } |
| 9835 | |
| 9836 | return retval; |
| 9837 | } |
| 9838 | |
| 9839 | /* |
| 9840 | * Return TRUE if "fname" matches with an entry in 'suffixes'. |
| 9841 | */ |
| 9842 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9843 | match_suffix(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9844 | { |
| 9845 | int fnamelen, setsuflen; |
| 9846 | char_u *setsuf; |
| 9847 | #define MAXSUFLEN 30 /* maximum length of a file suffix */ |
| 9848 | char_u suf_buf[MAXSUFLEN]; |
| 9849 | |
| 9850 | fnamelen = (int)STRLEN(fname); |
| 9851 | setsuflen = 0; |
| 9852 | for (setsuf = p_su; *setsuf; ) |
| 9853 | { |
| 9854 | setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); |
Bram Moolenaar | 055a2ba | 2009-07-14 19:40:21 +0000 | [diff] [blame] | 9855 | if (setsuflen == 0) |
| 9856 | { |
| 9857 | char_u *tail = gettail(fname); |
| 9858 | |
| 9859 | /* empty entry: match name without a '.' */ |
| 9860 | if (vim_strchr(tail, '.') == NULL) |
| 9861 | { |
| 9862 | setsuflen = 1; |
| 9863 | break; |
| 9864 | } |
| 9865 | } |
| 9866 | else |
| 9867 | { |
| 9868 | if (fnamelen >= setsuflen |
| 9869 | && fnamencmp(suf_buf, fname + fnamelen - setsuflen, |
| 9870 | (size_t)setsuflen) == 0) |
| 9871 | break; |
| 9872 | setsuflen = 0; |
| 9873 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9874 | } |
| 9875 | return (setsuflen != 0); |
| 9876 | } |
| 9877 | |
| 9878 | #if !defined(NO_EXPANDPATH) || defined(PROTO) |
| 9879 | |
| 9880 | # ifdef VIM_BACKTICK |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 9881 | static int vim_backtick(char_u *p); |
| 9882 | static int expand_backtick(garray_T *gap, char_u *pat, int flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9883 | # endif |
| 9884 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 9885 | # if defined(WIN3264) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9886 | /* |
| 9887 | * File name expansion code for MS-DOS, Win16 and Win32. It's here because |
| 9888 | * it's shared between these systems. |
| 9889 | */ |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 9890 | # if defined(PROTO) |
| 9891 | # define _cdecl |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9892 | # else |
| 9893 | # ifdef __BORLANDC__ |
| 9894 | # define _cdecl _RTLENTRYF |
| 9895 | # endif |
| 9896 | # endif |
| 9897 | |
| 9898 | /* |
| 9899 | * comparison function for qsort in dos_expandpath() |
| 9900 | */ |
| 9901 | static int _cdecl |
| 9902 | pstrcmp(const void *a, const void *b) |
| 9903 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 9904 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9905 | } |
| 9906 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9907 | /* |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9908 | * Recursively expand one path component into all matching files and/or |
| 9909 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9910 | * Return the number of matches found. |
| 9911 | * "path" has backslashes before chars that are not to be expanded, starting |
| 9912 | * at "path[wildoff]". |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9913 | * Return the number of matches found. |
| 9914 | * NOTE: much of this is identical to unix_expandpath(), keep in sync! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9915 | */ |
| 9916 | static int |
| 9917 | dos_expandpath( |
| 9918 | garray_T *gap, |
| 9919 | char_u *path, |
| 9920 | int wildoff, |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9921 | int flags, /* EW_* flags */ |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 9922 | int didstar) /* expanded "**" once already */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9923 | { |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9924 | char_u *buf; |
| 9925 | char_u *path_end; |
| 9926 | char_u *p, *s, *e; |
| 9927 | int start_len = gap->ga_len; |
| 9928 | char_u *pat; |
| 9929 | regmatch_T regmatch; |
| 9930 | int starts_with_dot; |
| 9931 | int matches; |
| 9932 | int len; |
| 9933 | int starstar = FALSE; |
| 9934 | static int stardepth = 0; /* depth for "**" expansion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9935 | WIN32_FIND_DATA fb; |
| 9936 | HANDLE hFind = (HANDLE)0; |
| 9937 | # ifdef FEAT_MBYTE |
| 9938 | WIN32_FIND_DATAW wfb; |
| 9939 | WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */ |
| 9940 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9941 | char_u *matchname; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 9942 | int ok; |
| 9943 | |
| 9944 | /* Expanding "**" may take a long time, check for CTRL-C. */ |
| 9945 | if (stardepth > 0) |
| 9946 | { |
| 9947 | ui_breakcheck(); |
| 9948 | if (got_int) |
| 9949 | return 0; |
| 9950 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9951 | |
Bram Moolenaar | 7314efd | 2015-10-31 15:32:52 +0100 | [diff] [blame] | 9952 | /* Make room for file name. When doing encoding conversion the actual |
| 9953 | * length may be quite a bit longer, thus use the maximum possible length. */ |
| 9954 | buf = alloc((int)MAXPATHL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9955 | if (buf == NULL) |
| 9956 | return 0; |
| 9957 | |
| 9958 | /* |
| 9959 | * Find the first part in the path name that contains a wildcard or a ~1. |
| 9960 | * Copy it into buf, including the preceding characters. |
| 9961 | */ |
| 9962 | p = buf; |
| 9963 | s = buf; |
| 9964 | e = NULL; |
| 9965 | path_end = path; |
| 9966 | while (*path_end != NUL) |
| 9967 | { |
| 9968 | /* May ignore a wildcard that has a backslash before it; it will |
| 9969 | * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ |
| 9970 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 9971 | *p++ = *path_end++; |
| 9972 | else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') |
| 9973 | { |
| 9974 | if (e != NULL) |
| 9975 | break; |
| 9976 | s = p + 1; |
| 9977 | } |
| 9978 | else if (path_end >= path + wildoff |
| 9979 | && vim_strchr((char_u *)"*?[~", *path_end) != NULL) |
| 9980 | e = p; |
Bram Moolenaar | 780d4c3 | 2016-03-25 17:21:19 +0100 | [diff] [blame] | 9981 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9982 | if (has_mbyte) |
| 9983 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 9984 | len = (*mb_ptr2len)(path_end); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9985 | STRNCPY(p, path_end, len); |
| 9986 | p += len; |
| 9987 | path_end += len; |
| 9988 | } |
| 9989 | else |
Bram Moolenaar | 780d4c3 | 2016-03-25 17:21:19 +0100 | [diff] [blame] | 9990 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9991 | *p++ = *path_end++; |
| 9992 | } |
| 9993 | e = p; |
| 9994 | *e = NUL; |
| 9995 | |
| 9996 | /* now we have one wildcard component between s and e */ |
| 9997 | /* Remove backslashes between "wildoff" and the start of the wildcard |
| 9998 | * component. */ |
| 9999 | for (p = buf + wildoff; p < s; ++p) |
| 10000 | if (rem_backslash(p)) |
| 10001 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 10002 | STRMOVE(p, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10003 | --e; |
| 10004 | --s; |
| 10005 | } |
| 10006 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10007 | /* Check for "**" between "s" and "e". */ |
| 10008 | for (p = s; p < e; ++p) |
| 10009 | if (p[0] == '*' && p[1] == '*') |
| 10010 | starstar = TRUE; |
| 10011 | |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10012 | starts_with_dot = *s == '.'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10013 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 10014 | if (pat == NULL) |
| 10015 | { |
| 10016 | vim_free(buf); |
| 10017 | return 0; |
| 10018 | } |
| 10019 | |
| 10020 | /* compile the regexp into a program */ |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10021 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
Bram Moolenaar | b560983 | 2011-07-20 15:04:58 +0200 | [diff] [blame] | 10022 | ++emsg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10023 | regmatch.rm_ic = TRUE; /* Always ignore case */ |
| 10024 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10025 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
Bram Moolenaar | b560983 | 2011-07-20 15:04:58 +0200 | [diff] [blame] | 10026 | --emsg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10027 | vim_free(pat); |
| 10028 | |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10029 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10030 | { |
| 10031 | vim_free(buf); |
| 10032 | return 0; |
| 10033 | } |
| 10034 | |
| 10035 | /* remember the pattern or file name being looked for */ |
| 10036 | matchname = vim_strsave(s); |
| 10037 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10038 | /* If "**" is by itself, this is the first time we encounter it and more |
| 10039 | * is following then find matches without any directory. */ |
| 10040 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 10041 | && *path_end == '/') |
| 10042 | { |
| 10043 | STRCPY(s, path_end + 1); |
| 10044 | ++stardepth; |
| 10045 | (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 10046 | --stardepth; |
| 10047 | } |
| 10048 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10049 | /* Scan all files in the directory with "dir/ *.*" */ |
| 10050 | STRCPY(s, "*.*"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10051 | # ifdef FEAT_MBYTE |
| 10052 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 10053 | { |
| 10054 | /* The active codepage differs from 'encoding'. Attempt using the |
| 10055 | * wide function. If it fails because it is not implemented fall back |
| 10056 | * to the non-wide version (for Windows 98) */ |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 10057 | wn = enc_to_utf16(buf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10058 | if (wn != NULL) |
| 10059 | { |
| 10060 | hFind = FindFirstFileW(wn, &wfb); |
| 10061 | if (hFind == INVALID_HANDLE_VALUE |
| 10062 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 10063 | VIM_CLEAR(wn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10064 | } |
| 10065 | } |
| 10066 | |
| 10067 | if (wn == NULL) |
| 10068 | # endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 10069 | hFind = FindFirstFile((LPCSTR)buf, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10070 | ok = (hFind != INVALID_HANDLE_VALUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10071 | |
| 10072 | while (ok) |
| 10073 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10074 | # ifdef FEAT_MBYTE |
| 10075 | if (wn != NULL) |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 10076 | p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10077 | else |
| 10078 | # endif |
| 10079 | p = (char_u *)fb.cFileName; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10080 | /* Ignore entries starting with a dot, unless when asked for. Accept |
| 10081 | * all entries found with "matchname". */ |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10082 | if ((p[0] != '.' || starts_with_dot |
| 10083 | || ((flags & EW_DODOT) |
| 10084 | && p[1] != NUL && (p[1] != '.' || p[2] != NUL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10085 | && (matchname == NULL |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10086 | || (regmatch.regprog != NULL |
| 10087 | && vim_regexec(®match, p, (colnr_T)0)) |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 10088 | || ((flags & EW_NOTWILD) |
| 10089 | && fnamencmp(path + (s - buf), p, e - s) == 0))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10090 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10091 | STRCPY(s, p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10092 | len = (int)STRLEN(buf); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10093 | |
| 10094 | if (starstar && stardepth < 100) |
| 10095 | { |
| 10096 | /* For "**" in the pattern first go deeper in the tree to |
| 10097 | * find matches. */ |
| 10098 | STRCPY(buf + len, "/**"); |
| 10099 | STRCPY(buf + len + 3, path_end); |
| 10100 | ++stardepth; |
| 10101 | (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); |
| 10102 | --stardepth; |
| 10103 | } |
| 10104 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10105 | STRCPY(buf + len, path_end); |
| 10106 | if (mch_has_exp_wildcard(path_end)) |
| 10107 | { |
| 10108 | /* need to expand another component of the path */ |
| 10109 | /* remove backslashes for the remaining components only */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10110 | (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10111 | } |
| 10112 | else |
| 10113 | { |
| 10114 | /* no more wildcards, check if there is a match */ |
| 10115 | /* remove backslashes for the remaining components only */ |
| 10116 | if (*path_end != 0) |
| 10117 | backslash_halve(buf + len + 1); |
| 10118 | if (mch_getperm(buf) >= 0) /* add existing file */ |
| 10119 | addfile(gap, buf, flags); |
| 10120 | } |
| 10121 | } |
| 10122 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10123 | # ifdef FEAT_MBYTE |
| 10124 | if (wn != NULL) |
| 10125 | { |
| 10126 | vim_free(p); |
| 10127 | ok = FindNextFileW(hFind, &wfb); |
| 10128 | } |
| 10129 | else |
| 10130 | # endif |
| 10131 | ok = FindNextFile(hFind, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10132 | |
| 10133 | /* If no more matches and no match was used, try expanding the name |
| 10134 | * itself. Finds the long name of a short filename. */ |
| 10135 | if (!ok && matchname != NULL && gap->ga_len == start_len) |
| 10136 | { |
| 10137 | STRCPY(s, matchname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10138 | FindClose(hFind); |
| 10139 | # ifdef FEAT_MBYTE |
| 10140 | if (wn != NULL) |
| 10141 | { |
| 10142 | vim_free(wn); |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 10143 | wn = enc_to_utf16(buf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10144 | if (wn != NULL) |
| 10145 | hFind = FindFirstFileW(wn, &wfb); |
| 10146 | } |
| 10147 | if (wn == NULL) |
| 10148 | # endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 10149 | hFind = FindFirstFile((LPCSTR)buf, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10150 | ok = (hFind != INVALID_HANDLE_VALUE); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 10151 | VIM_CLEAR(matchname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10152 | } |
| 10153 | } |
| 10154 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10155 | FindClose(hFind); |
| 10156 | # ifdef FEAT_MBYTE |
| 10157 | vim_free(wn); |
| 10158 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10159 | vim_free(buf); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10160 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10161 | vim_free(matchname); |
| 10162 | |
| 10163 | matches = gap->ga_len - start_len; |
| 10164 | if (matches > 0) |
| 10165 | qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, |
| 10166 | sizeof(char_u *), pstrcmp); |
| 10167 | return matches; |
| 10168 | } |
| 10169 | |
| 10170 | int |
| 10171 | mch_expandpath( |
| 10172 | garray_T *gap, |
| 10173 | char_u *path, |
| 10174 | int flags) /* EW_* flags */ |
| 10175 | { |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10176 | return dos_expandpath(gap, path, 0, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10177 | } |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10178 | # endif /* WIN3264 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10179 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10180 | #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ |
| 10181 | || defined(PROTO) |
| 10182 | /* |
| 10183 | * Unix style wildcard expansion code. |
| 10184 | * It's here because it's used both for Unix and Mac. |
| 10185 | */ |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 10186 | static int pstrcmp(const void *, const void *); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10187 | |
| 10188 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10189 | pstrcmp(const void *a, const void *b) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10190 | { |
| 10191 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
| 10192 | } |
| 10193 | |
| 10194 | /* |
| 10195 | * Recursively expand one path component into all matching files and/or |
| 10196 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
| 10197 | * "path" has backslashes before chars that are not to be expanded, starting |
| 10198 | * at "path + wildoff". |
| 10199 | * Return the number of matches found. |
| 10200 | * NOTE: much of this is identical to dos_expandpath(), keep in sync! |
| 10201 | */ |
| 10202 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10203 | unix_expandpath( |
| 10204 | garray_T *gap, |
| 10205 | char_u *path, |
| 10206 | int wildoff, |
| 10207 | int flags, /* EW_* flags */ |
| 10208 | int didstar) /* expanded "**" once already */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10209 | { |
| 10210 | char_u *buf; |
| 10211 | char_u *path_end; |
| 10212 | char_u *p, *s, *e; |
| 10213 | int start_len = gap->ga_len; |
| 10214 | char_u *pat; |
| 10215 | regmatch_T regmatch; |
| 10216 | int starts_with_dot; |
| 10217 | int matches; |
| 10218 | int len; |
| 10219 | int starstar = FALSE; |
| 10220 | static int stardepth = 0; /* depth for "**" expansion */ |
| 10221 | |
| 10222 | DIR *dirp; |
| 10223 | struct dirent *dp; |
| 10224 | |
| 10225 | /* Expanding "**" may take a long time, check for CTRL-C. */ |
| 10226 | if (stardepth > 0) |
| 10227 | { |
| 10228 | ui_breakcheck(); |
| 10229 | if (got_int) |
| 10230 | return 0; |
| 10231 | } |
| 10232 | |
| 10233 | /* make room for file name */ |
| 10234 | buf = alloc((int)STRLEN(path) + BASENAMELEN + 5); |
| 10235 | if (buf == NULL) |
| 10236 | return 0; |
| 10237 | |
| 10238 | /* |
| 10239 | * Find the first part in the path name that contains a wildcard. |
Bram Moolenaar | 2d0b92f | 2012-04-30 21:09:43 +0200 | [diff] [blame] | 10240 | * 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] | 10241 | * Copy it into "buf", including the preceding characters. |
| 10242 | */ |
| 10243 | p = buf; |
| 10244 | s = buf; |
| 10245 | e = NULL; |
| 10246 | path_end = path; |
| 10247 | while (*path_end != NUL) |
| 10248 | { |
| 10249 | /* May ignore a wildcard that has a backslash before it; it will |
| 10250 | * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ |
| 10251 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 10252 | *p++ = *path_end++; |
| 10253 | else if (*path_end == '/') |
| 10254 | { |
| 10255 | if (e != NULL) |
| 10256 | break; |
| 10257 | s = p + 1; |
| 10258 | } |
| 10259 | else if (path_end >= path + wildoff |
Bram Moolenaar | 2d0b92f | 2012-04-30 21:09:43 +0200 | [diff] [blame] | 10260 | && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 10261 | || (!p_fic && (flags & EW_ICASE) |
| 10262 | && isalpha(PTR2CHAR(path_end))))) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10263 | e = p; |
| 10264 | #ifdef FEAT_MBYTE |
| 10265 | if (has_mbyte) |
| 10266 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10267 | len = (*mb_ptr2len)(path_end); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10268 | STRNCPY(p, path_end, len); |
| 10269 | p += len; |
| 10270 | path_end += len; |
| 10271 | } |
| 10272 | else |
| 10273 | #endif |
| 10274 | *p++ = *path_end++; |
| 10275 | } |
| 10276 | e = p; |
| 10277 | *e = NUL; |
| 10278 | |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 10279 | /* Now we have one wildcard component between "s" and "e". */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10280 | /* Remove backslashes between "wildoff" and the start of the wildcard |
| 10281 | * component. */ |
| 10282 | for (p = buf + wildoff; p < s; ++p) |
| 10283 | if (rem_backslash(p)) |
| 10284 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 10285 | STRMOVE(p, p + 1); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10286 | --e; |
| 10287 | --s; |
| 10288 | } |
| 10289 | |
| 10290 | /* Check for "**" between "s" and "e". */ |
| 10291 | for (p = s; p < e; ++p) |
| 10292 | if (p[0] == '*' && p[1] == '*') |
| 10293 | starstar = TRUE; |
| 10294 | |
| 10295 | /* convert the file pattern to a regexp pattern */ |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10296 | starts_with_dot = *s == '.'; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10297 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 10298 | if (pat == NULL) |
| 10299 | { |
| 10300 | vim_free(buf); |
| 10301 | return 0; |
| 10302 | } |
| 10303 | |
| 10304 | /* compile the regexp into a program */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 10305 | if (flags & EW_ICASE) |
| 10306 | regmatch.rm_ic = TRUE; /* 'wildignorecase' set */ |
| 10307 | else |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 10308 | regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */ |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10309 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 10310 | ++emsg_silent; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10311 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10312 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 10313 | --emsg_silent; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10314 | vim_free(pat); |
| 10315 | |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10316 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10317 | { |
| 10318 | vim_free(buf); |
| 10319 | return 0; |
| 10320 | } |
| 10321 | |
| 10322 | /* If "**" is by itself, this is the first time we encounter it and more |
| 10323 | * is following then find matches without any directory. */ |
| 10324 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 10325 | && *path_end == '/') |
| 10326 | { |
| 10327 | STRCPY(s, path_end + 1); |
| 10328 | ++stardepth; |
| 10329 | (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 10330 | --stardepth; |
| 10331 | } |
| 10332 | |
| 10333 | /* open the directory for scanning */ |
| 10334 | *s = NUL; |
| 10335 | dirp = opendir(*buf == NUL ? "." : (char *)buf); |
| 10336 | |
| 10337 | /* Find all matching entries */ |
| 10338 | if (dirp != NULL) |
| 10339 | { |
| 10340 | for (;;) |
| 10341 | { |
| 10342 | dp = readdir(dirp); |
| 10343 | if (dp == NULL) |
| 10344 | break; |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10345 | if ((dp->d_name[0] != '.' || starts_with_dot |
| 10346 | || ((flags & EW_DODOT) |
| 10347 | && dp->d_name[1] != NUL |
| 10348 | && (dp->d_name[1] != '.' || dp->d_name[2] != NUL))) |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10349 | && ((regmatch.regprog != NULL && vim_regexec(®match, |
| 10350 | (char_u *)dp->d_name, (colnr_T)0)) |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 10351 | || ((flags & EW_NOTWILD) |
| 10352 | && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0))) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10353 | { |
| 10354 | STRCPY(s, dp->d_name); |
| 10355 | len = STRLEN(buf); |
| 10356 | |
| 10357 | if (starstar && stardepth < 100) |
| 10358 | { |
| 10359 | /* For "**" in the pattern first go deeper in the tree to |
| 10360 | * find matches. */ |
| 10361 | STRCPY(buf + len, "/**"); |
| 10362 | STRCPY(buf + len + 3, path_end); |
| 10363 | ++stardepth; |
| 10364 | (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); |
| 10365 | --stardepth; |
| 10366 | } |
| 10367 | |
| 10368 | STRCPY(buf + len, path_end); |
| 10369 | if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */ |
| 10370 | { |
| 10371 | /* need to expand another component of the path */ |
| 10372 | /* remove backslashes for the remaining components only */ |
| 10373 | (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); |
| 10374 | } |
| 10375 | else |
| 10376 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 10377 | stat_T sb; |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 10378 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10379 | /* no more wildcards, check if there is a match */ |
| 10380 | /* remove backslashes for the remaining components only */ |
| 10381 | if (*path_end != NUL) |
| 10382 | backslash_halve(buf + len + 1); |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 10383 | /* add existing file or symbolic link */ |
Bram Moolenaar | ab11a59 | 2015-03-06 22:00:11 +0100 | [diff] [blame] | 10384 | if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0 |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 10385 | : mch_getperm(buf) >= 0) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10386 | { |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 10387 | #ifdef MACOS_CONVERT |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10388 | size_t precomp_len = STRLEN(buf)+1; |
| 10389 | char_u *precomp_buf = |
| 10390 | mac_precompose_path(buf, precomp_len, &precomp_len); |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 10391 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10392 | if (precomp_buf) |
| 10393 | { |
| 10394 | mch_memmove(buf, precomp_buf, precomp_len); |
| 10395 | vim_free(precomp_buf); |
| 10396 | } |
| 10397 | #endif |
| 10398 | addfile(gap, buf, flags); |
| 10399 | } |
| 10400 | } |
| 10401 | } |
| 10402 | } |
| 10403 | |
| 10404 | closedir(dirp); |
| 10405 | } |
| 10406 | |
| 10407 | vim_free(buf); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10408 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10409 | |
| 10410 | matches = gap->ga_len - start_len; |
| 10411 | if (matches > 0) |
| 10412 | qsort(((char_u **)gap->ga_data) + start_len, matches, |
| 10413 | sizeof(char_u *), pstrcmp); |
| 10414 | return matches; |
| 10415 | } |
| 10416 | #endif |
| 10417 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10418 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 10419 | static int find_previous_pathsep(char_u *path, char_u **psep); |
| 10420 | static int is_unique(char_u *maybe_unique, garray_T *gap, int i); |
| 10421 | static void expand_path_option(char_u *curdir, garray_T *gap); |
| 10422 | static char_u *get_path_cutoff(char_u *fname, garray_T *gap); |
| 10423 | static void uniquefy_paths(garray_T *gap, char_u *pattern); |
| 10424 | 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] | 10425 | |
| 10426 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10427 | * Moves "*psep" back to the previous path separator in "path". |
| 10428 | * Returns FAIL is "*psep" ends up at the beginning of "path". |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10429 | */ |
| 10430 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10431 | find_previous_pathsep(char_u *path, char_u **psep) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10432 | { |
| 10433 | /* skip the current separator */ |
| 10434 | if (*psep > path && vim_ispathsep(**psep)) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10435 | --*psep; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10436 | |
| 10437 | /* find the previous separator */ |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10438 | while (*psep > path) |
| 10439 | { |
| 10440 | if (vim_ispathsep(**psep)) |
| 10441 | return OK; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 10442 | MB_PTR_BACK(path, *psep); |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10443 | } |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10444 | |
| 10445 | return FAIL; |
| 10446 | } |
| 10447 | |
| 10448 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10449 | * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap". |
| 10450 | * "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] | 10451 | */ |
| 10452 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10453 | is_unique(char_u *maybe_unique, garray_T *gap, int i) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10454 | { |
| 10455 | int j; |
| 10456 | int candidate_len; |
| 10457 | int other_path_len; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10458 | char_u **other_paths = (char_u **)gap->ga_data; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10459 | char_u *rival; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10460 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10461 | for (j = 0; j < gap->ga_len; j++) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10462 | { |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10463 | if (j == i) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10464 | continue; /* don't compare it with itself */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10465 | |
Bram Moolenaar | 624c7aa | 2010-07-16 20:38:52 +0200 | [diff] [blame] | 10466 | candidate_len = (int)STRLEN(maybe_unique); |
| 10467 | other_path_len = (int)STRLEN(other_paths[j]); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10468 | if (other_path_len < candidate_len) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10469 | continue; /* it's different when it's shorter */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10470 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10471 | rival = other_paths[j] + other_path_len - candidate_len; |
Bram Moolenaar | da9836c | 2010-08-16 21:53:27 +0200 | [diff] [blame] | 10472 | if (fnamecmp(maybe_unique, rival) == 0 |
| 10473 | && (rival == other_paths[j] || vim_ispathsep(*(rival - 1)))) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10474 | return FALSE; /* match */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10475 | } |
| 10476 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10477 | return TRUE; /* no match found */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10478 | } |
| 10479 | |
| 10480 | /* |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 10481 | * 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] | 10482 | * paths are expanded to their equivalent fullpath. This includes the "." |
| 10483 | * (relative to current buffer directory) and empty path (relative to current |
| 10484 | * directory) notations. |
| 10485 | * |
| 10486 | * TODO: handle upward search (;) and path limiter (**N) notations by |
| 10487 | * expanding each into their equivalent path(s). |
| 10488 | */ |
| 10489 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10490 | expand_path_option(char_u *curdir, garray_T *gap) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10491 | { |
| 10492 | char_u *path_option = *curbuf->b_p_path == NUL |
| 10493 | ? p_path : curbuf->b_p_path; |
| 10494 | char_u *buf; |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10495 | char_u *p; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10496 | int len; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10497 | |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10498 | if ((buf = alloc((int)MAXPATHL)) == NULL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10499 | return; |
| 10500 | |
| 10501 | while (*path_option != NUL) |
| 10502 | { |
| 10503 | copy_option_part(&path_option, buf, MAXPATHL, " ,"); |
| 10504 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10505 | if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1]))) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10506 | { |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10507 | /* Relative to current buffer: |
| 10508 | * "/path/file" + "." -> "/path/" |
| 10509 | * "/path/file" + "./subdir" -> "/path/subdir" */ |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10510 | if (curbuf->b_ffname == NULL) |
| 10511 | continue; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10512 | p = gettail(curbuf->b_ffname); |
| 10513 | len = (int)(p - curbuf->b_ffname); |
| 10514 | if (len + (int)STRLEN(buf) >= MAXPATHL) |
| 10515 | continue; |
| 10516 | if (buf[1] == NUL) |
| 10517 | buf[len] = NUL; |
| 10518 | else |
| 10519 | STRMOVE(buf + len, buf + 2); |
| 10520 | mch_memmove(buf, curbuf->b_ffname, len); |
| 10521 | simplify_filename(buf); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10522 | } |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10523 | else if (buf[0] == NUL) |
| 10524 | /* relative to current directory */ |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10525 | STRCPY(buf, curdir); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 10526 | else if (path_with_url(buf)) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10527 | /* URL can't be used here */ |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 10528 | continue; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10529 | else if (!mch_isFullName(buf)) |
| 10530 | { |
| 10531 | /* Expand relative path to their full path equivalent */ |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10532 | len = (int)STRLEN(curdir); |
| 10533 | if (len + (int)STRLEN(buf) + 3 > MAXPATHL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10534 | continue; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10535 | STRMOVE(buf + len + 1, buf); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10536 | STRCPY(buf, curdir); |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10537 | buf[len] = PATHSEP; |
Bram Moolenaar | 57adda1 | 2010-08-03 22:11:29 +0200 | [diff] [blame] | 10538 | simplify_filename(buf); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10539 | } |
| 10540 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10541 | if (ga_grow(gap, 1) == FAIL) |
| 10542 | break; |
Bram Moolenaar | 811fe63 | 2013-04-24 17:34:20 +0200 | [diff] [blame] | 10543 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10544 | # if defined(MSWIN) |
Bram Moolenaar | 811fe63 | 2013-04-24 17:34:20 +0200 | [diff] [blame] | 10545 | /* Avoid the path ending in a backslash, it fails when a comma is |
| 10546 | * appended. */ |
Bram Moolenaar | 4e0d974 | 2013-05-04 03:40:27 +0200 | [diff] [blame] | 10547 | len = (int)STRLEN(buf); |
Bram Moolenaar | 811fe63 | 2013-04-24 17:34:20 +0200 | [diff] [blame] | 10548 | if (buf[len - 1] == '\\') |
| 10549 | buf[len - 1] = '/'; |
| 10550 | # endif |
| 10551 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10552 | p = vim_strsave(buf); |
| 10553 | if (p == NULL) |
| 10554 | break; |
| 10555 | ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10556 | } |
| 10557 | |
| 10558 | vim_free(buf); |
| 10559 | } |
| 10560 | |
| 10561 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10562 | * Returns a pointer to the file or directory name in "fname" that matches the |
| 10563 | * 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] | 10564 | * |
| 10565 | * path: /foo/bar/baz |
| 10566 | * fname: /foo/bar/baz/quux.txt |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10567 | * returns: ^this |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10568 | */ |
| 10569 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10570 | get_path_cutoff(char_u *fname, garray_T *gap) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10571 | { |
| 10572 | int i; |
| 10573 | int maxlen = 0; |
| 10574 | char_u **path_part = (char_u **)gap->ga_data; |
| 10575 | char_u *cutoff = NULL; |
| 10576 | |
| 10577 | for (i = 0; i < gap->ga_len; i++) |
| 10578 | { |
| 10579 | int j = 0; |
| 10580 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10581 | while ((fname[j] == path_part[i][j] |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10582 | # if defined(MSWIN) |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10583 | || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j])) |
| 10584 | #endif |
| 10585 | ) && fname[j] != NUL && path_part[i][j] != NUL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10586 | j++; |
| 10587 | if (j > maxlen) |
| 10588 | { |
| 10589 | maxlen = j; |
| 10590 | cutoff = &fname[j]; |
| 10591 | } |
| 10592 | } |
| 10593 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10594 | /* skip to the file or directory name */ |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10595 | if (cutoff != NULL) |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10596 | while (vim_ispathsep(*cutoff)) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 10597 | MB_PTR_ADV(cutoff); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10598 | |
| 10599 | return cutoff; |
| 10600 | } |
| 10601 | |
| 10602 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10603 | * Sorts, removes duplicates and modifies all the fullpath names in "gap" so |
| 10604 | * that they are unique with respect to each other while conserving the part |
| 10605 | * 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] | 10606 | */ |
| 10607 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10608 | uniquefy_paths(garray_T *gap, char_u *pattern) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10609 | { |
Bram Moolenaar | cb9d45c | 2010-07-20 18:10:15 +0200 | [diff] [blame] | 10610 | int i; |
| 10611 | int len; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10612 | char_u **fnames = (char_u **)gap->ga_data; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10613 | int sort_again = FALSE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10614 | char_u *pat; |
| 10615 | char_u *file_pattern; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10616 | char_u *curdir; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10617 | regmatch_T regmatch; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10618 | garray_T path_ga; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10619 | char_u **in_curdir = NULL; |
| 10620 | char_u *short_name; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10621 | |
Bram Moolenaar | cb9d45c | 2010-07-20 18:10:15 +0200 | [diff] [blame] | 10622 | remove_duplicates(gap); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10623 | ga_init2(&path_ga, (int)sizeof(char_u *), 1); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10624 | |
| 10625 | /* |
| 10626 | * We need to prepend a '*' at the beginning of file_pattern so that the |
| 10627 | * regex matches anywhere in the path. FIXME: is this valid for all |
Bram Moolenaar | b31e438 | 2010-07-24 16:01:56 +0200 | [diff] [blame] | 10628 | * possible patterns? |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10629 | */ |
Bram Moolenaar | 624c7aa | 2010-07-16 20:38:52 +0200 | [diff] [blame] | 10630 | len = (int)STRLEN(pattern); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10631 | file_pattern = alloc(len + 2); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10632 | if (file_pattern == NULL) |
| 10633 | return; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10634 | file_pattern[0] = '*'; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10635 | file_pattern[1] = NUL; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10636 | STRCAT(file_pattern, pattern); |
| 10637 | pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE); |
| 10638 | vim_free(file_pattern); |
Bram Moolenaar | b31e438 | 2010-07-24 16:01:56 +0200 | [diff] [blame] | 10639 | if (pat == NULL) |
| 10640 | return; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10641 | |
Bram Moolenaar | b31e438 | 2010-07-24 16:01:56 +0200 | [diff] [blame] | 10642 | regmatch.rm_ic = TRUE; /* always ignore case */ |
| 10643 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); |
| 10644 | vim_free(pat); |
| 10645 | if (regmatch.regprog == NULL) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10646 | return; |
| 10647 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10648 | if ((curdir = alloc((int)(MAXPATHL))) == NULL) |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10649 | goto theend; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10650 | mch_dirname(curdir, MAXPATHL); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10651 | expand_path_option(curdir, &path_ga); |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10652 | |
| 10653 | in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *)); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10654 | if (in_curdir == NULL) |
| 10655 | goto theend; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10656 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10657 | for (i = 0; i < gap->ga_len && !got_int; i++) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10658 | { |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10659 | char_u *path = fnames[i]; |
| 10660 | int is_in_curdir; |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10661 | char_u *dir_end = gettail_dir(path); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10662 | char_u *pathsep_p; |
| 10663 | char_u *path_cutoff; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10664 | |
Bram Moolenaar | 624c7aa | 2010-07-16 20:38:52 +0200 | [diff] [blame] | 10665 | len = (int)STRLEN(path); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10666 | is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0 |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10667 | && curdir[dir_end - path] == NUL; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10668 | if (is_in_curdir) |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10669 | in_curdir[i] = vim_strsave(path); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10670 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10671 | /* Shorten the filename while maintaining its uniqueness */ |
| 10672 | path_cutoff = get_path_cutoff(path, &path_ga); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10673 | |
Bram Moolenaar | 73d4e4c | 2016-08-27 21:55:13 +0200 | [diff] [blame] | 10674 | /* Don't assume all files can be reached without path when search |
| 10675 | * pattern starts with star star slash, so only remove path_cutoff |
| 10676 | * when possible. */ |
| 10677 | if (pattern[0] == '*' && pattern[1] == '*' |
| 10678 | && vim_ispathsep_nocolon(pattern[2]) |
| 10679 | && path_cutoff != NULL |
| 10680 | && vim_regexec(®match, path_cutoff, (colnr_T)0) |
| 10681 | && is_unique(path_cutoff, gap, i)) |
| 10682 | { |
| 10683 | sort_again = TRUE; |
| 10684 | mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1); |
| 10685 | } |
| 10686 | else |
| 10687 | { |
| 10688 | /* Here all files can be reached without path, so get shortest |
| 10689 | * unique path. We start at the end of the path. */ |
| 10690 | pathsep_p = path + len - 1; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10691 | |
Bram Moolenaar | 73d4e4c | 2016-08-27 21:55:13 +0200 | [diff] [blame] | 10692 | while (find_previous_pathsep(path, &pathsep_p)) |
| 10693 | if (vim_regexec(®match, pathsep_p + 1, (colnr_T)0) |
| 10694 | && is_unique(pathsep_p + 1, gap, i) |
| 10695 | && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff) |
| 10696 | { |
| 10697 | sort_again = TRUE; |
| 10698 | mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p)); |
| 10699 | break; |
| 10700 | } |
| 10701 | } |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10702 | |
| 10703 | if (mch_isFullName(path)) |
| 10704 | { |
| 10705 | /* |
| 10706 | * Last resort: shorten relative to curdir if possible. |
| 10707 | * 'possible' means: |
| 10708 | * 1. It is under the current directory. |
| 10709 | * 2. The result is actually shorter than the original. |
| 10710 | * |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10711 | * Before curdir After |
| 10712 | * /foo/bar/file.txt /foo/bar ./file.txt |
| 10713 | * c:\foo\bar\file.txt c:\foo\bar .\file.txt |
| 10714 | * /file.txt / /file.txt |
| 10715 | * c:\file.txt c:\ .\file.txt |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10716 | */ |
| 10717 | short_name = shorten_fname(path, curdir); |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10718 | if (short_name != NULL && short_name > path + 1 |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10719 | #if defined(MSWIN) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10720 | /* On windows, |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10721 | * shorten_fname("c:\a\a.txt", "c:\a\b") |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10722 | * returns "\a\a.txt", which is not really the short |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10723 | * name, hence: */ |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10724 | && !vim_ispathsep(*short_name) |
| 10725 | #endif |
| 10726 | ) |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10727 | { |
| 10728 | STRCPY(path, "."); |
| 10729 | add_pathsep(path); |
Bram Moolenaar | cda000e | 2010-08-14 13:34:39 +0200 | [diff] [blame] | 10730 | STRMOVE(path + STRLEN(path), short_name); |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10731 | } |
| 10732 | } |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10733 | ui_breakcheck(); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10734 | } |
| 10735 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10736 | /* Shorten filenames in /in/current/directory/{filename} */ |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10737 | for (i = 0; i < gap->ga_len && !got_int; i++) |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10738 | { |
| 10739 | char_u *rel_path; |
| 10740 | char_u *path = in_curdir[i]; |
| 10741 | |
| 10742 | if (path == NULL) |
| 10743 | continue; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10744 | |
| 10745 | /* If the {filename} is not unique, change it to ./{filename}. |
| 10746 | * Else reduce it to {filename} */ |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10747 | short_name = shorten_fname(path, curdir); |
| 10748 | if (short_name == NULL) |
| 10749 | short_name = path; |
| 10750 | if (is_unique(short_name, gap, i)) |
| 10751 | { |
| 10752 | STRCPY(fnames[i], short_name); |
| 10753 | continue; |
| 10754 | } |
| 10755 | |
| 10756 | rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2)); |
| 10757 | if (rel_path == NULL) |
| 10758 | goto theend; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10759 | STRCPY(rel_path, "."); |
| 10760 | add_pathsep(rel_path); |
| 10761 | STRCAT(rel_path, short_name); |
| 10762 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10763 | vim_free(fnames[i]); |
| 10764 | fnames[i] = rel_path; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10765 | sort_again = TRUE; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10766 | ui_breakcheck(); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10767 | } |
| 10768 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10769 | theend: |
| 10770 | vim_free(curdir); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10771 | if (in_curdir != NULL) |
| 10772 | { |
| 10773 | for (i = 0; i < gap->ga_len; i++) |
| 10774 | vim_free(in_curdir[i]); |
| 10775 | vim_free(in_curdir); |
| 10776 | } |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10777 | ga_clear_strings(&path_ga); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10778 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10779 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10780 | if (sort_again) |
Bram Moolenaar | cb9d45c | 2010-07-20 18:10:15 +0200 | [diff] [blame] | 10781 | remove_duplicates(gap); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10782 | } |
| 10783 | |
| 10784 | /* |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10785 | * Calls globpath() with 'path' values for the given pattern and stores the |
| 10786 | * result in "gap". |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10787 | * Returns the total number of matches. |
| 10788 | */ |
| 10789 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10790 | expand_in_path( |
| 10791 | garray_T *gap, |
| 10792 | char_u *pattern, |
| 10793 | int flags) /* EW_* flags */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10794 | { |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10795 | char_u *curdir; |
| 10796 | garray_T path_ga; |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10797 | char_u *paths = NULL; |
Bram Moolenaar | 8a37b03 | 2018-02-03 20:43:08 +0100 | [diff] [blame] | 10798 | int glob_flags = 0; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10799 | |
Bram Moolenaar | 7f0f621 | 2010-08-03 22:21:00 +0200 | [diff] [blame] | 10800 | if ((curdir = alloc((unsigned)MAXPATHL)) == NULL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10801 | return 0; |
| 10802 | mch_dirname(curdir, MAXPATHL); |
| 10803 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10804 | ga_init2(&path_ga, (int)sizeof(char_u *), 1); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10805 | expand_path_option(curdir, &path_ga); |
| 10806 | vim_free(curdir); |
Bram Moolenaar | 006d2b0 | 2010-08-04 12:39:44 +0200 | [diff] [blame] | 10807 | if (path_ga.ga_len == 0) |
| 10808 | return 0; |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10809 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 10810 | paths = ga_concat_strings(&path_ga, ","); |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10811 | ga_clear_strings(&path_ga); |
| 10812 | if (paths == NULL) |
Bram Moolenaar | 7f0f621 | 2010-08-03 22:21:00 +0200 | [diff] [blame] | 10813 | return 0; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10814 | |
Bram Moolenaar | 8a37b03 | 2018-02-03 20:43:08 +0100 | [diff] [blame] | 10815 | if (flags & EW_ICASE) |
| 10816 | glob_flags |= WILD_ICASE; |
| 10817 | if (flags & EW_ADDSLASH) |
| 10818 | glob_flags |= WILD_ADD_SLASH; |
| 10819 | globpath(paths, pattern, gap, glob_flags); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10820 | vim_free(paths); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10821 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10822 | return gap->ga_len; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10823 | } |
| 10824 | #endif |
| 10825 | |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 10826 | #if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 10827 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10828 | * Sort "gap" and remove duplicate entries. "gap" is expected to contain a |
| 10829 | * list of file names in allocated memory. |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 10830 | */ |
| 10831 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10832 | remove_duplicates(garray_T *gap) |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 10833 | { |
| 10834 | int i; |
| 10835 | int j; |
| 10836 | char_u **fnames = (char_u **)gap->ga_data; |
| 10837 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10838 | sort_strings(fnames, gap->ga_len); |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 10839 | for (i = gap->ga_len - 1; i > 0; --i) |
| 10840 | if (fnamecmp(fnames[i - 1], fnames[i]) == 0) |
| 10841 | { |
| 10842 | vim_free(fnames[i]); |
| 10843 | for (j = i + 1; j < gap->ga_len; ++j) |
| 10844 | fnames[j - 1] = fnames[j]; |
| 10845 | --gap->ga_len; |
| 10846 | } |
| 10847 | } |
| 10848 | #endif |
| 10849 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 10850 | static int has_env_var(char_u *p); |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10851 | |
| 10852 | /* |
| 10853 | * Return TRUE if "p" contains what looks like an environment variable. |
| 10854 | * Allowing for escaping. |
| 10855 | */ |
| 10856 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10857 | has_env_var(char_u *p) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10858 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 10859 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10860 | { |
| 10861 | if (*p == '\\' && p[1] != NUL) |
| 10862 | ++p; |
| 10863 | else if (vim_strchr((char_u *) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10864 | #if defined(MSWIN) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10865 | "$%" |
| 10866 | #else |
| 10867 | "$" |
| 10868 | #endif |
| 10869 | , *p) != NULL) |
| 10870 | return TRUE; |
| 10871 | } |
| 10872 | return FALSE; |
| 10873 | } |
| 10874 | |
| 10875 | #ifdef SPECIAL_WILDCHAR |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 10876 | static int has_special_wildchar(char_u *p); |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10877 | |
| 10878 | /* |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 10879 | * Return TRUE if "p" contains a special wildcard character, one that Vim |
| 10880 | * cannot expand, requires using a shell. |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10881 | */ |
| 10882 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10883 | has_special_wildchar(char_u *p) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10884 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 10885 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10886 | { |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 10887 | /* Allow for escaping. */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10888 | if (*p == '\\' && p[1] != NUL) |
| 10889 | ++p; |
| 10890 | else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL) |
| 10891 | return TRUE; |
| 10892 | } |
| 10893 | return FALSE; |
| 10894 | } |
| 10895 | #endif |
| 10896 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10897 | /* |
| 10898 | * Generic wildcard expansion code. |
| 10899 | * |
| 10900 | * Characters in "pat" that should not be expanded must be preceded with a |
| 10901 | * backslash. E.g., "/path\ with\ spaces/my\*star*" |
| 10902 | * |
| 10903 | * Return FAIL when no single file was found. In this case "num_file" is not |
| 10904 | * set, and "file" may contain an error message. |
| 10905 | * Return OK when some files found. "num_file" is set to the number of |
| 10906 | * matches, "file" to the array of matches. Call FreeWild() later. |
| 10907 | */ |
| 10908 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10909 | gen_expand_wildcards( |
| 10910 | int num_pat, /* number of input patterns */ |
| 10911 | char_u **pat, /* array of input patterns */ |
| 10912 | int *num_file, /* resulting number of files */ |
| 10913 | char_u ***file, /* array of resulting files */ |
| 10914 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10915 | { |
| 10916 | int i; |
| 10917 | garray_T ga; |
| 10918 | char_u *p; |
| 10919 | static int recursive = FALSE; |
| 10920 | int add_pat; |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 10921 | int retval = OK; |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 10922 | #if defined(FEAT_SEARCHPATH) |
| 10923 | int did_expand_in_path = FALSE; |
| 10924 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10925 | |
| 10926 | /* |
| 10927 | * expand_env() is called to expand things like "~user". If this fails, |
| 10928 | * it calls ExpandOne(), which brings us back here. In this case, always |
| 10929 | * call the machine specific expansion function, if possible. Otherwise, |
| 10930 | * return FAIL. |
| 10931 | */ |
| 10932 | if (recursive) |
| 10933 | #ifdef SPECIAL_WILDCHAR |
| 10934 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 10935 | #else |
| 10936 | return FAIL; |
| 10937 | #endif |
| 10938 | |
| 10939 | #ifdef SPECIAL_WILDCHAR |
| 10940 | /* |
| 10941 | * If there are any special wildcard characters which we cannot handle |
| 10942 | * here, call machine specific function for all the expansion. This |
| 10943 | * avoids starting the shell for each argument separately. |
| 10944 | * For `=expr` do use the internal function. |
| 10945 | */ |
| 10946 | for (i = 0; i < num_pat; i++) |
| 10947 | { |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10948 | if (has_special_wildchar(pat[i]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10949 | # ifdef VIM_BACKTICK |
| 10950 | && !(vim_backtick(pat[i]) && pat[i][1] == '=') |
| 10951 | # endif |
| 10952 | ) |
| 10953 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 10954 | } |
| 10955 | #endif |
| 10956 | |
| 10957 | recursive = TRUE; |
| 10958 | |
| 10959 | /* |
| 10960 | * The matching file names are stored in a growarray. Init it empty. |
| 10961 | */ |
| 10962 | ga_init2(&ga, (int)sizeof(char_u *), 30); |
| 10963 | |
| 10964 | for (i = 0; i < num_pat; ++i) |
| 10965 | { |
| 10966 | add_pat = -1; |
| 10967 | p = pat[i]; |
| 10968 | |
| 10969 | #ifdef VIM_BACKTICK |
| 10970 | if (vim_backtick(p)) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 10971 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10972 | add_pat = expand_backtick(&ga, p, flags); |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 10973 | if (add_pat == -1) |
| 10974 | retval = FAIL; |
| 10975 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10976 | else |
| 10977 | #endif |
| 10978 | { |
| 10979 | /* |
| 10980 | * First expand environment variables, "~/" and "~user/". |
| 10981 | */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10982 | if (has_env_var(p) || *p == '~') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10983 | { |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 10984 | p = expand_env_save_opt(p, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10985 | if (p == NULL) |
| 10986 | p = pat[i]; |
| 10987 | #ifdef UNIX |
| 10988 | /* |
| 10989 | * On Unix, if expand_env() can't expand an environment |
| 10990 | * variable, use the shell to do that. Discard previously |
| 10991 | * found file names and start all over again. |
| 10992 | */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 10993 | else if (has_env_var(p) || *p == '~') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10994 | { |
| 10995 | vim_free(p); |
Bram Moolenaar | 782027e | 2009-06-24 14:25:49 +0000 | [diff] [blame] | 10996 | ga_clear_strings(&ga); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10997 | i = mch_expand_wildcards(num_pat, pat, num_file, file, |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 10998 | flags|EW_KEEPDOLLAR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10999 | recursive = FALSE; |
| 11000 | return i; |
| 11001 | } |
| 11002 | #endif |
| 11003 | } |
| 11004 | |
| 11005 | /* |
| 11006 | * If there are wildcards: Expand file names and add each match to |
| 11007 | * the list. If there is no match, and EW_NOTFOUND is given, add |
| 11008 | * the pattern. |
| 11009 | * If there are no wildcards: Add the file name if it exists or |
| 11010 | * when EW_NOTFOUND is given. |
| 11011 | */ |
| 11012 | if (mch_has_exp_wildcard(p)) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11013 | { |
| 11014 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11015 | if ((flags & EW_PATH) |
| 11016 | && !mch_isFullName(p) |
| 11017 | && !(p[0] == '.' |
| 11018 | && (vim_ispathsep(p[1]) |
| 11019 | || (p[1] == '.' && vim_ispathsep(p[2])))) |
| 11020 | ) |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11021 | { |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11022 | /* :find completion where 'path' is used. |
| 11023 | * Recursiveness is OK here. */ |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11024 | recursive = FALSE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11025 | add_pat = expand_in_path(&ga, p, flags); |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11026 | recursive = TRUE; |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11027 | did_expand_in_path = TRUE; |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11028 | } |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11029 | else |
| 11030 | #endif |
| 11031 | add_pat = mch_expandpath(&ga, p, flags); |
| 11032 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11033 | } |
| 11034 | |
| 11035 | if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) |
| 11036 | { |
| 11037 | char_u *t = backslash_halve_save(p); |
| 11038 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11039 | /* When EW_NOTFOUND is used, always add files and dirs. Makes |
| 11040 | * "vim c:/" work. */ |
| 11041 | if (flags & EW_NOTFOUND) |
| 11042 | addfile(&ga, t, flags | EW_DIR | EW_FILE); |
Bram Moolenaar | 00efded | 2016-07-07 14:29:10 +0200 | [diff] [blame] | 11043 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11044 | addfile(&ga, t, flags); |
| 11045 | vim_free(t); |
| 11046 | } |
| 11047 | |
Bram Moolenaar | b28ebbc | 2010-07-14 16:59:57 +0200 | [diff] [blame] | 11048 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11049 | if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH)) |
Bram Moolenaar | b28ebbc | 2010-07-14 16:59:57 +0200 | [diff] [blame] | 11050 | uniquefy_paths(&ga, p); |
| 11051 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11052 | if (p != pat[i]) |
| 11053 | vim_free(p); |
| 11054 | } |
| 11055 | |
| 11056 | *num_file = ga.ga_len; |
| 11057 | *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; |
| 11058 | |
| 11059 | recursive = FALSE; |
| 11060 | |
Bram Moolenaar | 336bd62 | 2016-01-17 18:23:58 +0100 | [diff] [blame] | 11061 | return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11062 | } |
| 11063 | |
| 11064 | # ifdef VIM_BACKTICK |
| 11065 | |
| 11066 | /* |
| 11067 | * Return TRUE if we can expand this backtick thing here. |
| 11068 | */ |
| 11069 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11070 | vim_backtick(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11071 | { |
| 11072 | return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); |
| 11073 | } |
| 11074 | |
| 11075 | /* |
| 11076 | * Expand an item in `backticks` by executing it as a command. |
| 11077 | * Currently only works when pat[] starts and ends with a `. |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11078 | * Returns number of file names found, -1 if an error is encountered. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11079 | */ |
| 11080 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11081 | expand_backtick( |
| 11082 | garray_T *gap, |
| 11083 | char_u *pat, |
| 11084 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11085 | { |
| 11086 | char_u *p; |
| 11087 | char_u *cmd; |
| 11088 | char_u *buffer; |
| 11089 | int cnt = 0; |
| 11090 | int i; |
| 11091 | |
| 11092 | /* Create the command: lop off the backticks. */ |
| 11093 | cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); |
| 11094 | if (cmd == NULL) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11095 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11096 | |
| 11097 | #ifdef FEAT_EVAL |
| 11098 | if (*cmd == '=') /* `={expr}`: Expand expression */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 11099 | buffer = eval_to_string(cmd + 1, &p, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11100 | else |
| 11101 | #endif |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 11102 | buffer = get_cmd_output(cmd, NULL, |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11103 | (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11104 | vim_free(cmd); |
| 11105 | if (buffer == NULL) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11106 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11107 | |
| 11108 | cmd = buffer; |
| 11109 | while (*cmd != NUL) |
| 11110 | { |
| 11111 | cmd = skipwhite(cmd); /* skip over white space */ |
| 11112 | p = cmd; |
| 11113 | while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */ |
| 11114 | ++p; |
| 11115 | /* add an entry if it is not empty */ |
| 11116 | if (p > cmd) |
| 11117 | { |
| 11118 | i = *p; |
| 11119 | *p = NUL; |
| 11120 | addfile(gap, cmd, flags); |
| 11121 | *p = i; |
| 11122 | ++cnt; |
| 11123 | } |
| 11124 | cmd = p; |
| 11125 | while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) |
| 11126 | ++cmd; |
| 11127 | } |
| 11128 | |
| 11129 | vim_free(buffer); |
| 11130 | return cnt; |
| 11131 | } |
| 11132 | # endif /* VIM_BACKTICK */ |
| 11133 | |
| 11134 | /* |
| 11135 | * Add a file to a file list. Accepted flags: |
| 11136 | * EW_DIR add directories |
| 11137 | * EW_FILE add files |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 11138 | * EW_EXEC add executable files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11139 | * EW_NOTFOUND add even when it doesn't exist |
| 11140 | * EW_ADDSLASH add slash after directory name |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 11141 | * EW_ALLLINKS add symlink also when the referred file does not exist |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11142 | */ |
| 11143 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11144 | addfile( |
| 11145 | garray_T *gap, |
| 11146 | char_u *f, /* filename */ |
| 11147 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11148 | { |
| 11149 | char_u *p; |
| 11150 | int isdir; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 11151 | stat_T sb; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11152 | |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 11153 | /* if the file/dir/link doesn't exist, may not add it */ |
| 11154 | if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS) |
Bram Moolenaar | ab11a59 | 2015-03-06 22:00:11 +0100 | [diff] [blame] | 11155 | ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11156 | return; |
| 11157 | |
| 11158 | #ifdef FNAME_ILLEGAL |
| 11159 | /* if the file/dir contains illegal characters, don't add it */ |
| 11160 | if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) |
| 11161 | return; |
| 11162 | #endif |
| 11163 | |
| 11164 | isdir = mch_isdir(f); |
| 11165 | if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) |
| 11166 | return; |
| 11167 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 11168 | /* If the file isn't executable, may not add it. Do accept directories. |
| 11169 | * When invoked from expand_shellcmd() do not use $PATH. */ |
| 11170 | if (!isdir && (flags & EW_EXEC) |
| 11171 | && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD))) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 11172 | return; |
| 11173 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11174 | /* Make room for another item in the file list. */ |
| 11175 | if (ga_grow(gap, 1) == FAIL) |
| 11176 | return; |
| 11177 | |
| 11178 | p = alloc((unsigned)(STRLEN(f) + 1 + isdir)); |
| 11179 | if (p == NULL) |
| 11180 | return; |
| 11181 | |
| 11182 | STRCPY(p, f); |
| 11183 | #ifdef BACKSLASH_IN_FILENAME |
| 11184 | slash_adjust(p); |
| 11185 | #endif |
| 11186 | /* |
| 11187 | * Append a slash or backslash after directory names if none is present. |
| 11188 | */ |
| 11189 | #ifndef DONT_ADD_PATHSEP_TO_DIR |
| 11190 | if (isdir && (flags & EW_ADDSLASH)) |
| 11191 | add_pathsep(p); |
| 11192 | #endif |
| 11193 | ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11194 | } |
| 11195 | #endif /* !NO_EXPANDPATH */ |
| 11196 | |
| 11197 | #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO) |
| 11198 | |
| 11199 | #ifndef SEEK_SET |
| 11200 | # define SEEK_SET 0 |
| 11201 | #endif |
| 11202 | #ifndef SEEK_END |
| 11203 | # define SEEK_END 2 |
| 11204 | #endif |
| 11205 | |
| 11206 | /* |
| 11207 | * Get the stdout of an external command. |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11208 | * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not |
| 11209 | * NULL store the length there. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11210 | * Returns an allocated string, or NULL for error. |
| 11211 | */ |
| 11212 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11213 | get_cmd_output( |
| 11214 | char_u *cmd, |
| 11215 | char_u *infile, /* optional input file name */ |
| 11216 | int flags, /* can be SHELL_SILENT */ |
| 11217 | int *ret_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11218 | { |
| 11219 | char_u *tempname; |
| 11220 | char_u *command; |
| 11221 | char_u *buffer = NULL; |
| 11222 | int len; |
| 11223 | int i = 0; |
| 11224 | FILE *fd; |
| 11225 | |
| 11226 | if (check_restricted() || check_secure()) |
| 11227 | return NULL; |
| 11228 | |
| 11229 | /* get a name for the temp file */ |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 11230 | if ((tempname = vim_tempname('o', FALSE)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11231 | { |
| 11232 | EMSG(_(e_notmp)); |
| 11233 | return NULL; |
| 11234 | } |
| 11235 | |
| 11236 | /* Add the redirection stuff */ |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 11237 | command = make_filter_cmd(cmd, infile, tempname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11238 | if (command == NULL) |
| 11239 | goto done; |
| 11240 | |
| 11241 | /* |
| 11242 | * Call the shell to execute the command (errors are ignored). |
| 11243 | * Don't check timestamps here. |
| 11244 | */ |
| 11245 | ++no_check_timestamps; |
| 11246 | call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags); |
| 11247 | --no_check_timestamps; |
| 11248 | |
| 11249 | vim_free(command); |
| 11250 | |
| 11251 | /* |
| 11252 | * read the names from the file into memory |
| 11253 | */ |
| 11254 | # ifdef VMS |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 11255 | /* created temporary file is not always readable as binary */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11256 | fd = mch_fopen((char *)tempname, "r"); |
| 11257 | # else |
| 11258 | fd = mch_fopen((char *)tempname, READBIN); |
| 11259 | # endif |
| 11260 | |
| 11261 | if (fd == NULL) |
| 11262 | { |
| 11263 | EMSG2(_(e_notopen), tempname); |
| 11264 | goto done; |
| 11265 | } |
| 11266 | |
| 11267 | fseek(fd, 0L, SEEK_END); |
| 11268 | len = ftell(fd); /* get size of temp file */ |
| 11269 | fseek(fd, 0L, SEEK_SET); |
| 11270 | |
| 11271 | buffer = alloc(len + 1); |
| 11272 | if (buffer != NULL) |
| 11273 | i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd); |
| 11274 | fclose(fd); |
| 11275 | mch_remove(tempname); |
| 11276 | if (buffer == NULL) |
| 11277 | goto done; |
| 11278 | #ifdef VMS |
| 11279 | len = i; /* VMS doesn't give us what we asked for... */ |
| 11280 | #endif |
| 11281 | if (i != len) |
| 11282 | { |
| 11283 | EMSG2(_(e_notread), tempname); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 11284 | VIM_CLEAR(buffer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11285 | } |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11286 | else if (ret_len == NULL) |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 11287 | { |
| 11288 | /* Change NUL into SOH, otherwise the string is truncated. */ |
| 11289 | for (i = 0; i < len; ++i) |
Bram Moolenaar | f40f4ab | 2013-08-03 17:31:28 +0200 | [diff] [blame] | 11290 | if (buffer[i] == NUL) |
| 11291 | buffer[i] = 1; |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 11292 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 11293 | buffer[len] = NUL; /* make sure the buffer is terminated */ |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 11294 | } |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11295 | else |
| 11296 | *ret_len = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11297 | |
| 11298 | done: |
| 11299 | vim_free(tempname); |
| 11300 | return buffer; |
| 11301 | } |
| 11302 | #endif |
| 11303 | |
| 11304 | /* |
| 11305 | * Free the list of files returned by expand_wildcards() or other expansion |
| 11306 | * functions. |
| 11307 | */ |
| 11308 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11309 | FreeWild(int count, char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11310 | { |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 11311 | if (count <= 0 || files == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11312 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11313 | while (count--) |
| 11314 | vim_free(files[count]); |
| 11315 | vim_free(files); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11316 | } |
| 11317 | |
| 11318 | /* |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 11319 | * Return TRUE when need to go to Insert mode because of 'insertmode'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11320 | * Don't do this when still processing a command or a mapping. |
| 11321 | * Don't do this when inside a ":normal" command. |
| 11322 | */ |
| 11323 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11324 | goto_im(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11325 | { |
| 11326 | return (p_im && stuff_empty() && typebuf_typed()); |
| 11327 | } |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11328 | |
| 11329 | /* |
Bram Moolenaar | 050fe7e | 2014-05-22 14:00:16 +0200 | [diff] [blame] | 11330 | * Returns the isolated name of the shell in allocated memory: |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11331 | * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f". |
| 11332 | * - Remove any argument. E.g., "csh -f" -> "csh". |
| 11333 | * But don't allow a space in the path, so that this works: |
| 11334 | * "/usr/bin/csh --rcfile ~/.cshrc" |
| 11335 | * But don't do that for Windows, it's common to have a space in the path. |
| 11336 | */ |
| 11337 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11338 | get_isolated_shell_name(void) |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11339 | { |
| 11340 | char_u *p; |
| 11341 | |
| 11342 | #ifdef WIN3264 |
| 11343 | p = gettail(p_sh); |
| 11344 | p = vim_strnsave(p, (int)(skiptowhite(p) - p)); |
| 11345 | #else |
| 11346 | p = skiptowhite(p_sh); |
| 11347 | if (*p == NUL) |
| 11348 | { |
| 11349 | /* No white space, use the tail. */ |
| 11350 | p = vim_strsave(gettail(p_sh)); |
| 11351 | } |
| 11352 | else |
| 11353 | { |
| 11354 | char_u *p1, *p2; |
| 11355 | |
| 11356 | /* Find the last path separator before the space. */ |
| 11357 | p1 = p_sh; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 11358 | for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2)) |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11359 | if (vim_ispathsep(*p2)) |
| 11360 | p1 = p2 + 1; |
| 11361 | p = vim_strnsave(p1, (int)(p - p1)); |
| 11362 | } |
| 11363 | #endif |
| 11364 | return p; |
| 11365 | } |