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 | 828c3d7 | 2018-06-19 18:58:07 +0200 | [diff] [blame] | 17 | #if defined(FEAT_CMDL_COMPL) && defined(WIN3264) |
| 18 | # include <lm.h> |
| 19 | #endif |
| 20 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 21 | static char_u *vim_version_dir(char_u *vimdir); |
| 22 | static char_u *remove_tail(char_u *p, char_u *pend, char_u *name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 23 | |
Bram Moolenaar | 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 | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 35 | #ifdef FEAT_VARTABS |
| 36 | return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts, |
| 37 | curbuf->b_p_vts_array, FALSE); |
| 38 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 39 | return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 40 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* |
| 44 | * Count the size (in window cells) of the indent in line "lnum". |
| 45 | */ |
| 46 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 47 | get_indent_lnum(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 48 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 49 | #ifdef FEAT_VARTABS |
| 50 | return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts, |
| 51 | curbuf->b_p_vts_array, FALSE); |
| 52 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 53 | return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 54 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | #if defined(FEAT_FOLDING) || defined(PROTO) |
| 58 | /* |
| 59 | * Count the size (in window cells) of the indent in line "lnum" of buffer |
| 60 | * "buf". |
| 61 | */ |
| 62 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 63 | get_indent_buf(buf_T *buf, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 64 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 65 | #ifdef FEAT_VARTABS |
| 66 | return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE), |
| 67 | (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE); |
| 68 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 69 | return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 70 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 71 | } |
| 72 | #endif |
| 73 | |
| 74 | /* |
| 75 | * count the size (in window cells) of the indent in line "ptr", with |
| 76 | * 'tabstop' at "ts" |
| 77 | */ |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 78 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 79 | get_indent_str( |
| 80 | char_u *ptr, |
| 81 | int ts, |
| 82 | int list) /* if TRUE, count only screen size for tabs */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 83 | { |
| 84 | int count = 0; |
| 85 | |
| 86 | for ( ; *ptr; ++ptr) |
| 87 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 88 | if (*ptr == TAB) |
| 89 | { |
| 90 | if (!list || lcs_tab1) /* count a tab for what it is worth */ |
| 91 | count += ts - (count % ts); |
| 92 | else |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 93 | /* In list mode, when tab is not set, count screen char width |
| 94 | * for Tab, displays: ^I */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 95 | count += ptr2cells(ptr); |
| 96 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | else if (*ptr == ' ') |
| 98 | ++count; /* count a space for one */ |
| 99 | else |
| 100 | break; |
| 101 | } |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 102 | return count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 105 | #ifdef FEAT_VARTABS |
| 106 | /* |
| 107 | * Count the size (in window cells) of the indent in line "ptr", using |
| 108 | * variable tabstops. |
| 109 | * if "list" is TRUE, count only screen size for tabs. |
| 110 | */ |
| 111 | int |
| 112 | get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list) |
| 113 | { |
| 114 | int count = 0; |
| 115 | |
| 116 | for ( ; *ptr; ++ptr) |
| 117 | { |
| 118 | if (*ptr == TAB) /* count a tab for what it is worth */ |
| 119 | { |
| 120 | if (!list || lcs_tab1) |
| 121 | count += tabstop_padding(count, ts, vts); |
| 122 | else |
| 123 | /* In list mode, when tab is not set, count screen char width |
| 124 | * for Tab, displays: ^I */ |
| 125 | count += ptr2cells(ptr); |
| 126 | } |
| 127 | else if (*ptr == ' ') |
| 128 | ++count; /* count a space for one */ |
| 129 | else |
| 130 | break; |
| 131 | } |
| 132 | return count; |
| 133 | } |
| 134 | #endif |
| 135 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 136 | /* |
| 137 | * Set the indent of the current line. |
| 138 | * Leaves the cursor on the first non-blank in the line. |
| 139 | * Caller must take care of undo. |
| 140 | * "flags": |
| 141 | * SIN_CHANGED: call changed_bytes() if the line was changed. |
| 142 | * SIN_INSERT: insert the indent in front of the line. |
| 143 | * SIN_UNDO: save line for undo before changing it. |
| 144 | * Returns TRUE if the line was changed. |
| 145 | */ |
| 146 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 147 | set_indent( |
| 148 | int size, /* measured in spaces */ |
| 149 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 150 | { |
| 151 | char_u *p; |
| 152 | char_u *newline; |
| 153 | char_u *oldline; |
| 154 | char_u *s; |
| 155 | int todo; |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 156 | int ind_len; /* measured in characters */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 157 | int line_len; |
| 158 | int doit = FALSE; |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 159 | int ind_done = 0; /* measured in spaces */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 160 | #ifdef FEAT_VARTABS |
| 161 | int ind_col = 0; |
| 162 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 163 | int tab_pad; |
Bram Moolenaar | 5409c05 | 2005-03-18 20:27:04 +0000 | [diff] [blame] | 164 | int retval = FALSE; |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 165 | int orig_char_len = -1; /* number of initial whitespace chars when |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 166 | 'et' and 'pi' are both set */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * First check if there is anything to do and compute the number of |
| 170 | * characters needed for the indent. |
| 171 | */ |
| 172 | todo = size; |
| 173 | ind_len = 0; |
| 174 | p = oldline = ml_get_curline(); |
| 175 | |
| 176 | /* Calculate the buffer size for the new indent, and check to see if it |
| 177 | * isn't already set */ |
| 178 | |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 179 | /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and |
| 180 | * 'preserveindent' are set count the number of characters at the |
| 181 | * beginning of the line to be copied */ |
| 182 | if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 183 | { |
| 184 | /* If 'preserveindent' is set then reuse as much as possible of |
| 185 | * the existing indent structure for the new indent */ |
| 186 | if (!(flags & SIN_INSERT) && curbuf->b_p_pi) |
| 187 | { |
| 188 | ind_done = 0; |
| 189 | |
| 190 | /* count as many characters as we can use */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 191 | while (todo > 0 && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 192 | { |
| 193 | if (*p == TAB) |
| 194 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 195 | #ifdef FEAT_VARTABS |
| 196 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 197 | curbuf->b_p_vts_array); |
| 198 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 199 | tab_pad = (int)curbuf->b_p_ts |
| 200 | - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 201 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 202 | /* stop if this tab will overshoot the target */ |
| 203 | if (todo < tab_pad) |
| 204 | break; |
| 205 | todo -= tab_pad; |
| 206 | ++ind_len; |
| 207 | ind_done += tab_pad; |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | --todo; |
| 212 | ++ind_len; |
| 213 | ++ind_done; |
| 214 | } |
| 215 | ++p; |
| 216 | } |
| 217 | |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 218 | #ifdef FEAT_VARTABS |
| 219 | /* These diverge from this point. */ |
| 220 | ind_col = ind_done; |
| 221 | #endif |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 222 | /* Set initial number of whitespace chars to copy if we are |
| 223 | * preserving indent but expandtab is set */ |
| 224 | if (curbuf->b_p_et) |
| 225 | orig_char_len = ind_len; |
| 226 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 227 | /* Fill to next tabstop with a tab, if possible */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 228 | #ifdef FEAT_VARTABS |
| 229 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 230 | curbuf->b_p_vts_array); |
| 231 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 232 | tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 233 | #endif |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 234 | if (todo >= tab_pad && orig_char_len == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 235 | { |
| 236 | doit = TRUE; |
| 237 | todo -= tab_pad; |
| 238 | ++ind_len; |
| 239 | /* ind_done += tab_pad; */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 240 | #ifdef FEAT_VARTABS |
| 241 | ind_col += tab_pad; |
| 242 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | /* count tabs required for indent */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 247 | #ifdef FEAT_VARTABS |
| 248 | for (;;) |
| 249 | { |
| 250 | tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts, |
| 251 | curbuf->b_p_vts_array); |
| 252 | if (todo < tab_pad) |
| 253 | break; |
| 254 | if (*p != TAB) |
| 255 | doit = TRUE; |
| 256 | else |
| 257 | ++p; |
| 258 | todo -= tab_pad; |
| 259 | ++ind_len; |
| 260 | ind_col += tab_pad; |
| 261 | } |
| 262 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 263 | while (todo >= (int)curbuf->b_p_ts) |
| 264 | { |
| 265 | if (*p != TAB) |
| 266 | doit = TRUE; |
| 267 | else |
| 268 | ++p; |
| 269 | todo -= (int)curbuf->b_p_ts; |
| 270 | ++ind_len; |
| 271 | /* ind_done += (int)curbuf->b_p_ts; */ |
| 272 | } |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 273 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 274 | } |
| 275 | /* count spaces required for indent */ |
| 276 | while (todo > 0) |
| 277 | { |
| 278 | if (*p != ' ') |
| 279 | doit = TRUE; |
| 280 | else |
| 281 | ++p; |
| 282 | --todo; |
| 283 | ++ind_len; |
| 284 | /* ++ind_done; */ |
| 285 | } |
| 286 | |
| 287 | /* Return if the indent is OK already. */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 288 | if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 289 | return FALSE; |
| 290 | |
| 291 | /* Allocate memory for the new line. */ |
| 292 | if (flags & SIN_INSERT) |
| 293 | p = oldline; |
| 294 | else |
| 295 | p = skipwhite(p); |
| 296 | line_len = (int)STRLEN(p) + 1; |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 297 | |
| 298 | /* If 'preserveindent' and 'expandtab' are both set keep the original |
| 299 | * characters and allocate accordingly. We will fill the rest with spaces |
| 300 | * after the if (!curbuf->b_p_et) below. */ |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 301 | if (orig_char_len != -1) |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 302 | { |
| 303 | newline = alloc(orig_char_len + size - ind_done + line_len); |
| 304 | if (newline == NULL) |
| 305 | return FALSE; |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 306 | todo = size - ind_done; |
| 307 | ind_len = orig_char_len + todo; /* Set total length of indent in |
| 308 | * characters, which may have been |
| 309 | * undercounted until now */ |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 310 | p = oldline; |
| 311 | s = newline; |
| 312 | while (orig_char_len > 0) |
| 313 | { |
| 314 | *s++ = *p++; |
| 315 | orig_char_len--; |
| 316 | } |
Bram Moolenaar | 913626c | 2008-01-03 11:43:42 +0000 | [diff] [blame] | 317 | |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 318 | /* Skip over any additional white space (useful when newindent is less |
| 319 | * than old) */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 320 | while (VIM_ISWHITE(*p)) |
Bram Moolenaar | 913626c | 2008-01-03 11:43:42 +0000 | [diff] [blame] | 321 | ++p; |
Bram Moolenaar | cc00b95 | 2007-08-11 12:32:57 +0000 | [diff] [blame] | 322 | |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 323 | } |
| 324 | else |
| 325 | { |
| 326 | todo = size; |
| 327 | newline = alloc(ind_len + line_len); |
| 328 | if (newline == NULL) |
| 329 | return FALSE; |
| 330 | s = newline; |
| 331 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 332 | |
| 333 | /* Put the characters in the new line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 334 | /* if 'expandtab' isn't set: use TABs */ |
| 335 | if (!curbuf->b_p_et) |
| 336 | { |
| 337 | /* If 'preserveindent' is set then reuse as much as possible of |
| 338 | * the existing indent structure for the new indent */ |
| 339 | if (!(flags & SIN_INSERT) && curbuf->b_p_pi) |
| 340 | { |
| 341 | p = oldline; |
| 342 | ind_done = 0; |
| 343 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 344 | while (todo > 0 && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 345 | { |
| 346 | if (*p == TAB) |
| 347 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 348 | #ifdef FEAT_VARTABS |
| 349 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 350 | curbuf->b_p_vts_array); |
| 351 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 352 | tab_pad = (int)curbuf->b_p_ts |
| 353 | - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 354 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 355 | /* stop if this tab will overshoot the target */ |
| 356 | if (todo < tab_pad) |
| 357 | break; |
| 358 | todo -= tab_pad; |
| 359 | ind_done += tab_pad; |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | --todo; |
| 364 | ++ind_done; |
| 365 | } |
| 366 | *s++ = *p++; |
| 367 | } |
| 368 | |
| 369 | /* Fill to next tabstop with a tab, if possible */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 370 | #ifdef FEAT_VARTABS |
| 371 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 372 | curbuf->b_p_vts_array); |
| 373 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 374 | tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 375 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 376 | if (todo >= tab_pad) |
| 377 | { |
| 378 | *s++ = TAB; |
| 379 | todo -= tab_pad; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 380 | #ifdef FEAT_VARTABS |
| 381 | ind_done += tab_pad; |
| 382 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | p = skipwhite(p); |
| 386 | } |
| 387 | |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 388 | #ifdef FEAT_VARTABS |
| 389 | for (;;) |
| 390 | { |
| 391 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 392 | curbuf->b_p_vts_array); |
| 393 | if (todo < tab_pad) |
| 394 | break; |
| 395 | *s++ = TAB; |
| 396 | todo -= tab_pad; |
| 397 | ind_done += tab_pad; |
| 398 | } |
| 399 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 400 | while (todo >= (int)curbuf->b_p_ts) |
| 401 | { |
| 402 | *s++ = TAB; |
| 403 | todo -= (int)curbuf->b_p_ts; |
| 404 | } |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 405 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 406 | } |
| 407 | while (todo > 0) |
| 408 | { |
| 409 | *s++ = ' '; |
| 410 | --todo; |
| 411 | } |
| 412 | mch_memmove(s, p, (size_t)line_len); |
| 413 | |
Bram Moolenaar | 663bc89 | 2019-01-08 23:07:24 +0100 | [diff] [blame] | 414 | // Replace the line (unless undo fails). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 415 | if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK) |
| 416 | { |
| 417 | ml_replace(curwin->w_cursor.lnum, newline, FALSE); |
| 418 | if (flags & SIN_CHANGED) |
| 419 | changed_bytes(curwin->w_cursor.lnum, 0); |
Bram Moolenaar | 663bc89 | 2019-01-08 23:07:24 +0100 | [diff] [blame] | 420 | |
| 421 | // Correct saved cursor position if it is in this line. |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 422 | if (saved_cursor.lnum == curwin->w_cursor.lnum) |
| 423 | { |
| 424 | if (saved_cursor.col >= (colnr_T)(p - oldline)) |
Bram Moolenaar | 663bc89 | 2019-01-08 23:07:24 +0100 | [diff] [blame] | 425 | // cursor was after the indent, adjust for the number of |
| 426 | // bytes added/removed |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 427 | saved_cursor.col += ind_len - (colnr_T)(p - oldline); |
| 428 | else if (saved_cursor.col >= (colnr_T)(s - newline)) |
Bram Moolenaar | 663bc89 | 2019-01-08 23:07:24 +0100 | [diff] [blame] | 429 | // cursor was in the indent, and is now after it, put it back |
| 430 | // at the start of the indent (replacing spaces with TAB) |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 431 | saved_cursor.col = (colnr_T)(s - newline); |
| 432 | } |
Bram Moolenaar | 663bc89 | 2019-01-08 23:07:24 +0100 | [diff] [blame] | 433 | #ifdef FEAT_TEXT_PROP |
| 434 | adjust_prop_columns(curwin->w_cursor.lnum, (colnr_T)(p - oldline), |
| 435 | ind_len - (colnr_T)(p - oldline)); |
| 436 | #endif |
Bram Moolenaar | 5409c05 | 2005-03-18 20:27:04 +0000 | [diff] [blame] | 437 | retval = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 438 | } |
| 439 | else |
| 440 | vim_free(newline); |
| 441 | |
| 442 | curwin->w_cursor.col = ind_len; |
Bram Moolenaar | 5409c05 | 2005-03-18 20:27:04 +0000 | [diff] [blame] | 443 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* |
| 447 | * Copy the indent from ptr to the current line (and fill to size) |
| 448 | * Leaves the cursor on the first non-blank in the line. |
| 449 | * Returns TRUE if the line was changed. |
| 450 | */ |
| 451 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 452 | copy_indent(int size, char_u *src) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 453 | { |
| 454 | char_u *p = NULL; |
| 455 | char_u *line = NULL; |
| 456 | char_u *s; |
| 457 | int todo; |
| 458 | int ind_len; |
| 459 | int line_len = 0; |
| 460 | int tab_pad; |
| 461 | int ind_done; |
| 462 | int round; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 463 | #ifdef FEAT_VARTABS |
| 464 | int ind_col; |
| 465 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 466 | |
| 467 | /* Round 1: compute the number of characters needed for the indent |
| 468 | * Round 2: copy the characters. */ |
| 469 | for (round = 1; round <= 2; ++round) |
| 470 | { |
| 471 | todo = size; |
| 472 | ind_len = 0; |
| 473 | ind_done = 0; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 474 | #ifdef FEAT_VARTABS |
| 475 | ind_col = 0; |
| 476 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 477 | s = src; |
| 478 | |
| 479 | /* Count/copy the usable portion of the source line */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 480 | while (todo > 0 && VIM_ISWHITE(*s)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 481 | { |
| 482 | if (*s == TAB) |
| 483 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 484 | #ifdef FEAT_VARTABS |
| 485 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 486 | curbuf->b_p_vts_array); |
| 487 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 488 | tab_pad = (int)curbuf->b_p_ts |
| 489 | - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 490 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 491 | /* Stop if this tab will overshoot the target */ |
| 492 | if (todo < tab_pad) |
| 493 | break; |
| 494 | todo -= tab_pad; |
| 495 | ind_done += tab_pad; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 496 | #ifdef FEAT_VARTABS |
| 497 | ind_col += tab_pad; |
| 498 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 499 | } |
| 500 | else |
| 501 | { |
| 502 | --todo; |
| 503 | ++ind_done; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 504 | #ifdef FEAT_VARTABS |
| 505 | ++ind_col; |
| 506 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 507 | } |
| 508 | ++ind_len; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 509 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 510 | *p++ = *s; |
| 511 | ++s; |
| 512 | } |
| 513 | |
| 514 | /* Fill to next tabstop with a tab, if possible */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 515 | #ifdef FEAT_VARTABS |
| 516 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 517 | curbuf->b_p_vts_array); |
| 518 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 519 | tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 520 | #endif |
Bram Moolenaar | c42e7ed | 2011-09-07 19:58:09 +0200 | [diff] [blame] | 521 | if (todo >= tab_pad && !curbuf->b_p_et) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 522 | { |
| 523 | todo -= tab_pad; |
| 524 | ++ind_len; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 525 | #ifdef FEAT_VARTABS |
| 526 | ind_col += tab_pad; |
| 527 | #endif |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 528 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 529 | *p++ = TAB; |
| 530 | } |
| 531 | |
| 532 | /* Add tabs required for indent */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 533 | if (!curbuf->b_p_et) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 534 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 535 | #ifdef FEAT_VARTABS |
| 536 | for (;;) |
| 537 | { |
| 538 | tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts, |
| 539 | curbuf->b_p_vts_array); |
| 540 | if (todo < tab_pad) |
| 541 | break; |
| 542 | todo -= tab_pad; |
| 543 | ++ind_len; |
| 544 | ind_col += tab_pad; |
| 545 | if (p != NULL) |
| 546 | *p++ = TAB; |
| 547 | } |
| 548 | #else |
| 549 | while (todo >= (int)curbuf->b_p_ts) |
| 550 | { |
| 551 | todo -= (int)curbuf->b_p_ts; |
| 552 | ++ind_len; |
| 553 | if (p != NULL) |
| 554 | *p++ = TAB; |
| 555 | } |
| 556 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /* Count/add spaces required for indent */ |
| 560 | while (todo > 0) |
| 561 | { |
| 562 | --todo; |
| 563 | ++ind_len; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 564 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 565 | *p++ = ' '; |
| 566 | } |
| 567 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 568 | if (p == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 569 | { |
| 570 | /* Allocate memory for the result: the copied indent, new indent |
| 571 | * and the rest of the line. */ |
| 572 | line_len = (int)STRLEN(ml_get_curline()) + 1; |
| 573 | line = alloc(ind_len + line_len); |
| 574 | if (line == NULL) |
| 575 | return FALSE; |
| 576 | p = line; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /* Append the original line */ |
| 581 | mch_memmove(p, ml_get_curline(), (size_t)line_len); |
| 582 | |
| 583 | /* Replace the line */ |
| 584 | ml_replace(curwin->w_cursor.lnum, line, FALSE); |
| 585 | |
| 586 | /* Put the cursor after the indent. */ |
| 587 | curwin->w_cursor.col = ind_len; |
| 588 | return TRUE; |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Return the indent of the current line after a number. Return -1 if no |
| 593 | * number was found. Used for 'n' in 'formatoptions': numbered list. |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 594 | * Since a pattern is used it can actually handle more than numbers. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 595 | */ |
| 596 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 597 | get_number_indent(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 598 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 599 | colnr_T col; |
| 600 | pos_T pos; |
| 601 | |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 602 | regmatch_T regmatch; |
| 603 | int lead_len = 0; /* length of comment leader */ |
| 604 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 605 | if (lnum > curbuf->b_ml.ml_line_count) |
| 606 | return -1; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 607 | pos.lnum = 0; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 608 | |
| 609 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 610 | /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */ |
| 611 | if ((State & INSERT) || has_format_option(FO_Q_COMS)) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 612 | lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE); |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 613 | #endif |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 614 | regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC); |
| 615 | if (regmatch.regprog != NULL) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 616 | { |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 617 | regmatch.rm_ic = FALSE; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 618 | |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 619 | /* vim_regexec() expects a pointer to a line. This lets us |
| 620 | * start matching for the flp beyond any comment leader... */ |
| 621 | if (vim_regexec(®match, ml_get(lnum) + lead_len, (colnr_T)0)) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 622 | { |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 623 | pos.lnum = lnum; |
| 624 | pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum)); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 625 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 626 | pos.coladd = 0; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 627 | #endif |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 628 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 629 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 630 | } |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 631 | |
| 632 | if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 633 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 634 | getvcol(curwin, &pos, &col, NULL, NULL); |
| 635 | return (int)col; |
| 636 | } |
| 637 | |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 638 | #if defined(FEAT_LINEBREAK) || defined(PROTO) |
| 639 | /* |
| 640 | * Return appropriate space number for breakindent, taking influencing |
| 641 | * parameters into account. Window must be specified, since it is not |
| 642 | * necessarily always the current one. |
| 643 | */ |
| 644 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 645 | get_breakindent_win( |
| 646 | win_T *wp, |
| 647 | char_u *line) /* start of the line */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 648 | { |
| 649 | static int prev_indent = 0; /* cached indent value */ |
| 650 | static long prev_ts = 0L; /* cached tabstop value */ |
| 651 | static char_u *prev_line = NULL; /* cached pointer to line */ |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 652 | static varnumber_T prev_tick = 0; /* changedtick of cached value */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 653 | #ifdef FEAT_VARTABS |
| 654 | static int *prev_vts = NULL; /* cached vartabs values */ |
| 655 | #endif |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 656 | int bri = 0; |
| 657 | /* window width minus window margin space, i.e. what rests for text */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 658 | const int eff_wwidth = wp->w_width |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 659 | - ((wp->w_p_nu || wp->w_p_rnu) |
| 660 | && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL) |
| 661 | ? number_width(wp) + 1 : 0); |
| 662 | |
| 663 | /* used cached indent, unless pointer or 'tabstop' changed */ |
Bram Moolenaar | a40aa76 | 2014-06-25 22:55:38 +0200 | [diff] [blame] | 664 | if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 665 | || prev_tick != CHANGEDTICK(wp->w_buffer) |
| 666 | #ifdef FEAT_VARTABS |
| 667 | || prev_vts != wp->w_buffer->b_p_vts_array |
| 668 | #endif |
| 669 | ) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 670 | { |
| 671 | prev_line = line; |
| 672 | prev_ts = wp->w_buffer->b_p_ts; |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 673 | prev_tick = CHANGEDTICK(wp->w_buffer); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 674 | #ifdef FEAT_VARTABS |
| 675 | prev_vts = wp->w_buffer->b_p_vts_array; |
| 676 | prev_indent = get_indent_str_vtab(line, |
| 677 | (int)wp->w_buffer->b_p_ts, |
| 678 | wp->w_buffer->b_p_vts_array, wp->w_p_list); |
| 679 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 680 | prev_indent = get_indent_str(line, |
Bram Moolenaar | 9d7a592 | 2014-06-26 21:24:56 +0200 | [diff] [blame] | 681 | (int)wp->w_buffer->b_p_ts, wp->w_p_list); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 682 | #endif |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 683 | } |
Bram Moolenaar | 9d7a592 | 2014-06-26 21:24:56 +0200 | [diff] [blame] | 684 | bri = prev_indent + wp->w_p_brishift; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 685 | |
| 686 | /* indent minus the length of the showbreak string */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 687 | if (wp->w_p_brisbr) |
| 688 | bri -= vim_strsize(p_sbr); |
| 689 | |
| 690 | /* Add offset for number column, if 'n' is in 'cpoptions' */ |
| 691 | bri += win_col_off2(wp); |
| 692 | |
| 693 | /* never indent past left window margin */ |
| 694 | if (bri < 0) |
| 695 | bri = 0; |
| 696 | /* always leave at least bri_min characters on the left, |
| 697 | * if text width is sufficient */ |
| 698 | else if (bri > eff_wwidth - wp->w_p_brimin) |
| 699 | bri = (eff_wwidth - wp->w_p_brimin < 0) |
| 700 | ? 0 : eff_wwidth - wp->w_p_brimin; |
| 701 | |
| 702 | return bri; |
| 703 | } |
| 704 | #endif |
| 705 | |
| 706 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 707 | #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) |
| 708 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 709 | /* |
| 710 | * Return TRUE if the string "line" starts with a word from 'cinwords'. |
| 711 | */ |
| 712 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 713 | cin_is_cinword(char_u *line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 714 | { |
| 715 | char_u *cinw; |
| 716 | char_u *cinw_buf; |
| 717 | int cinw_len; |
| 718 | int retval = FALSE; |
| 719 | int len; |
| 720 | |
| 721 | cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1; |
| 722 | cinw_buf = alloc((unsigned)cinw_len); |
| 723 | if (cinw_buf != NULL) |
| 724 | { |
| 725 | line = skipwhite(line); |
| 726 | for (cinw = curbuf->b_p_cinw; *cinw; ) |
| 727 | { |
| 728 | len = copy_option_part(&cinw, cinw_buf, cinw_len, ","); |
| 729 | if (STRNCMP(line, cinw_buf, len) == 0 |
| 730 | && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1]))) |
| 731 | { |
| 732 | retval = TRUE; |
| 733 | break; |
| 734 | } |
| 735 | } |
| 736 | vim_free(cinw_buf); |
| 737 | } |
| 738 | return retval; |
| 739 | } |
| 740 | #endif |
| 741 | |
| 742 | /* |
| 743 | * open_line: Add a new line below or above the current line. |
| 744 | * |
| 745 | * For VREPLACE mode, we only add a new line when we get to the end of the |
| 746 | * file, otherwise we just start replacing the next line. |
| 747 | * |
| 748 | * Caller must take care of undo. Since VREPLACE may affect any number of |
| 749 | * lines however, it may call u_save_cursor() again when starting to change a |
| 750 | * new line. |
| 751 | * "flags": OPENLINE_DELSPACES delete spaces after cursor |
| 752 | * OPENLINE_DO_COM format comments |
| 753 | * OPENLINE_KEEPTRAIL keep trailing spaces |
| 754 | * OPENLINE_MARKFIX adjust mark positions after the line break |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 755 | * OPENLINE_COM_LIST format comments with list or 2nd line indent |
| 756 | * |
| 757 | * "second_line_indent": indent for after ^^D in Insert mode or if flag |
| 758 | * OPENLINE_COM_LIST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 759 | * |
Bram Moolenaar | 24a2d72 | 2018-04-24 19:36:43 +0200 | [diff] [blame] | 760 | * Return OK for success, FAIL for failure |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 761 | */ |
| 762 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 763 | open_line( |
| 764 | int dir, /* FORWARD or BACKWARD */ |
| 765 | int flags, |
| 766 | int second_line_indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 767 | { |
| 768 | char_u *saved_line; /* copy of the original line */ |
| 769 | char_u *next_line = NULL; /* copy of the next line */ |
| 770 | char_u *p_extra = NULL; /* what goes to next line */ |
| 771 | int less_cols = 0; /* less columns for mark in new line */ |
| 772 | int less_cols_off = 0; /* columns to skip for mark adjust */ |
| 773 | pos_T old_cursor; /* old cursor position */ |
| 774 | int newcol = 0; /* new cursor column */ |
| 775 | int newindent = 0; /* auto-indent of the new line */ |
| 776 | int n; |
| 777 | int trunc_line = FALSE; /* truncate current line afterwards */ |
Bram Moolenaar | 24a2d72 | 2018-04-24 19:36:43 +0200 | [diff] [blame] | 778 | int retval = FAIL; /* return value */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 779 | #ifdef FEAT_COMMENTS |
| 780 | int extra_len = 0; /* length of p_extra string */ |
| 781 | int lead_len; /* length of comment leader */ |
| 782 | char_u *lead_flags; /* position in 'comments' for comment leader */ |
| 783 | char_u *leader = NULL; /* copy of comment leader */ |
| 784 | #endif |
| 785 | char_u *allocated = NULL; /* allocated memory */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 786 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 787 | int saved_char = NUL; /* init for GCC */ |
| 788 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS) |
| 789 | pos_T *pos; |
| 790 | #endif |
| 791 | #ifdef FEAT_SMARTINDENT |
| 792 | int do_si = (!p_paste && curbuf->b_p_si |
| 793 | # ifdef FEAT_CINDENT |
| 794 | && !curbuf->b_p_cin |
| 795 | # endif |
Bram Moolenaar | 69a76fe | 2017-08-03 17:54:03 +0200 | [diff] [blame] | 796 | # ifdef FEAT_EVAL |
| 797 | && *curbuf->b_p_inde == NUL |
| 798 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 799 | ); |
| 800 | int no_si = FALSE; /* reset did_si afterwards */ |
| 801 | int first_char = NUL; /* init for GCC */ |
| 802 | #endif |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 803 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 804 | int vreplace_mode; |
| 805 | #endif |
| 806 | int did_append; /* appended a new line */ |
| 807 | int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */ |
| 808 | |
| 809 | /* |
| 810 | * make a copy of the current line so we can mess with it |
| 811 | */ |
| 812 | saved_line = vim_strsave(ml_get_curline()); |
| 813 | if (saved_line == NULL) /* out of memory! */ |
| 814 | return FALSE; |
| 815 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 816 | if (State & VREPLACE_FLAG) |
| 817 | { |
| 818 | /* |
| 819 | * With VREPLACE we make a copy of the next line, which we will be |
| 820 | * starting to replace. First make the new line empty and let vim play |
| 821 | * with the indenting and comment leader to its heart's content. Then |
| 822 | * we grab what it ended up putting on the new line, put back the |
| 823 | * original line, and call ins_char() to put each new character onto |
| 824 | * the line, replacing what was there before and pushing the right |
| 825 | * stuff onto the replace stack. -- webb. |
| 826 | */ |
| 827 | if (curwin->w_cursor.lnum < orig_line_count) |
| 828 | next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1)); |
| 829 | else |
| 830 | next_line = vim_strsave((char_u *)""); |
| 831 | if (next_line == NULL) /* out of memory! */ |
| 832 | goto theend; |
| 833 | |
| 834 | /* |
| 835 | * In VREPLACE mode, a NL replaces the rest of the line, and starts |
| 836 | * replacing the next line, so push all of the characters left on the |
| 837 | * line onto the replace stack. We'll push any other characters that |
| 838 | * might be replaced at the start of the next line (due to autoindent |
| 839 | * etc) a bit later. |
| 840 | */ |
| 841 | replace_push(NUL); /* Call twice because BS over NL expects it */ |
| 842 | replace_push(NUL); |
| 843 | p = saved_line + curwin->w_cursor.col; |
| 844 | while (*p != NUL) |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 845 | { |
| 846 | #ifdef FEAT_MBYTE |
| 847 | if (has_mbyte) |
| 848 | p += replace_push_mb(p); |
| 849 | else |
| 850 | #endif |
| 851 | replace_push(*p++); |
| 852 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 853 | saved_line[curwin->w_cursor.col] = NUL; |
| 854 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 855 | |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 856 | if ((State & INSERT) && !(State & VREPLACE_FLAG)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 857 | { |
| 858 | p_extra = saved_line + curwin->w_cursor.col; |
| 859 | #ifdef FEAT_SMARTINDENT |
| 860 | if (do_si) /* need first char after new line break */ |
| 861 | { |
| 862 | p = skipwhite(p_extra); |
| 863 | first_char = *p; |
| 864 | } |
| 865 | #endif |
| 866 | #ifdef FEAT_COMMENTS |
| 867 | extra_len = (int)STRLEN(p_extra); |
| 868 | #endif |
| 869 | saved_char = *p_extra; |
| 870 | *p_extra = NUL; |
| 871 | } |
| 872 | |
| 873 | u_clearline(); /* cannot do "U" command when adding lines */ |
| 874 | #ifdef FEAT_SMARTINDENT |
| 875 | did_si = FALSE; |
| 876 | #endif |
| 877 | ai_col = 0; |
| 878 | |
| 879 | /* |
| 880 | * If we just did an auto-indent, then we didn't type anything on |
| 881 | * the prior line, and it should be truncated. Do this even if 'ai' is not |
| 882 | * set because automatically inserting a comment leader also sets did_ai. |
| 883 | */ |
| 884 | if (dir == FORWARD && did_ai) |
| 885 | trunc_line = TRUE; |
| 886 | |
| 887 | /* |
| 888 | * If 'autoindent' and/or 'smartindent' is set, try to figure out what |
| 889 | * indent to use for the new line. |
| 890 | */ |
| 891 | if (curbuf->b_p_ai |
| 892 | #ifdef FEAT_SMARTINDENT |
| 893 | || do_si |
| 894 | #endif |
| 895 | ) |
| 896 | { |
| 897 | /* |
| 898 | * count white space on current line |
| 899 | */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 900 | #ifdef FEAT_VARTABS |
| 901 | newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts, |
| 902 | curbuf->b_p_vts_array, FALSE); |
| 903 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 904 | newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 905 | #endif |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 906 | if (newindent == 0 && !(flags & OPENLINE_COM_LIST)) |
| 907 | newindent = second_line_indent; /* for ^^D command in insert mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 908 | |
| 909 | #ifdef FEAT_SMARTINDENT |
| 910 | /* |
| 911 | * Do smart indenting. |
| 912 | * In insert/replace mode (only when dir == FORWARD) |
| 913 | * we may move some text to the next line. If it starts with '{' |
| 914 | * don't add an indent. Fixes inserting a NL before '{' in line |
| 915 | * "if (condition) {" |
| 916 | */ |
| 917 | if (!trunc_line && do_si && *saved_line != NUL |
| 918 | && (p_extra == NULL || first_char != '{')) |
| 919 | { |
| 920 | char_u *ptr; |
| 921 | char_u last_char; |
| 922 | |
| 923 | old_cursor = curwin->w_cursor; |
| 924 | ptr = saved_line; |
| 925 | # ifdef FEAT_COMMENTS |
| 926 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 927 | lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 928 | else |
| 929 | lead_len = 0; |
| 930 | # endif |
| 931 | if (dir == FORWARD) |
| 932 | { |
| 933 | /* |
| 934 | * Skip preprocessor directives, unless they are |
| 935 | * recognised as comments. |
| 936 | */ |
| 937 | if ( |
| 938 | # ifdef FEAT_COMMENTS |
| 939 | lead_len == 0 && |
| 940 | # endif |
| 941 | ptr[0] == '#') |
| 942 | { |
| 943 | while (ptr[0] == '#' && curwin->w_cursor.lnum > 1) |
| 944 | ptr = ml_get(--curwin->w_cursor.lnum); |
| 945 | newindent = get_indent(); |
| 946 | } |
| 947 | # ifdef FEAT_COMMENTS |
| 948 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 949 | lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 950 | else |
| 951 | lead_len = 0; |
| 952 | if (lead_len > 0) |
| 953 | { |
| 954 | /* |
| 955 | * This case gets the following right: |
| 956 | * \* |
| 957 | * * A comment (read '\' as '/'). |
| 958 | * *\ |
| 959 | * #define IN_THE_WAY |
| 960 | * This should line up here; |
| 961 | */ |
| 962 | p = skipwhite(ptr); |
| 963 | if (p[0] == '/' && p[1] == '*') |
| 964 | p++; |
| 965 | if (p[0] == '*') |
| 966 | { |
| 967 | for (p++; *p; p++) |
| 968 | { |
| 969 | if (p[0] == '/' && p[-1] == '*') |
| 970 | { |
| 971 | /* |
| 972 | * End of C comment, indent should line up |
| 973 | * with the line containing the start of |
| 974 | * the comment |
| 975 | */ |
| 976 | curwin->w_cursor.col = (colnr_T)(p - ptr); |
| 977 | if ((pos = findmatch(NULL, NUL)) != NULL) |
| 978 | { |
| 979 | curwin->w_cursor.lnum = pos->lnum; |
| 980 | newindent = get_indent(); |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | else /* Not a comment line */ |
| 987 | # endif |
| 988 | { |
| 989 | /* Find last non-blank in line */ |
| 990 | p = ptr + STRLEN(ptr) - 1; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 991 | while (p > ptr && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 992 | --p; |
| 993 | last_char = *p; |
| 994 | |
| 995 | /* |
| 996 | * find the character just before the '{' or ';' |
| 997 | */ |
| 998 | if (last_char == '{' || last_char == ';') |
| 999 | { |
| 1000 | if (p > ptr) |
| 1001 | --p; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1002 | while (p > ptr && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1003 | --p; |
| 1004 | } |
| 1005 | /* |
| 1006 | * Try to catch lines that are split over multiple |
| 1007 | * lines. eg: |
| 1008 | * if (condition && |
| 1009 | * condition) { |
| 1010 | * Should line up here! |
| 1011 | * } |
| 1012 | */ |
| 1013 | if (*p == ')') |
| 1014 | { |
| 1015 | curwin->w_cursor.col = (colnr_T)(p - ptr); |
| 1016 | if ((pos = findmatch(NULL, '(')) != NULL) |
| 1017 | { |
| 1018 | curwin->w_cursor.lnum = pos->lnum; |
| 1019 | newindent = get_indent(); |
| 1020 | ptr = ml_get_curline(); |
| 1021 | } |
| 1022 | } |
| 1023 | /* |
| 1024 | * If last character is '{' do indent, without |
| 1025 | * checking for "if" and the like. |
| 1026 | */ |
| 1027 | if (last_char == '{') |
| 1028 | { |
| 1029 | did_si = TRUE; /* do indent */ |
| 1030 | no_si = TRUE; /* don't delete it when '{' typed */ |
| 1031 | } |
| 1032 | /* |
| 1033 | * Look for "if" and the like, use 'cinwords'. |
| 1034 | * Don't do this if the previous line ended in ';' or |
| 1035 | * '}'. |
| 1036 | */ |
| 1037 | else if (last_char != ';' && last_char != '}' |
| 1038 | && cin_is_cinword(ptr)) |
| 1039 | did_si = TRUE; |
| 1040 | } |
| 1041 | } |
| 1042 | else /* dir == BACKWARD */ |
| 1043 | { |
| 1044 | /* |
| 1045 | * Skip preprocessor directives, unless they are |
| 1046 | * recognised as comments. |
| 1047 | */ |
| 1048 | if ( |
| 1049 | # ifdef FEAT_COMMENTS |
| 1050 | lead_len == 0 && |
| 1051 | # endif |
| 1052 | ptr[0] == '#') |
| 1053 | { |
| 1054 | int was_backslashed = FALSE; |
| 1055 | |
| 1056 | while ((ptr[0] == '#' || was_backslashed) && |
| 1057 | curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 1058 | { |
| 1059 | if (*ptr && ptr[STRLEN(ptr) - 1] == '\\') |
| 1060 | was_backslashed = TRUE; |
| 1061 | else |
| 1062 | was_backslashed = FALSE; |
| 1063 | ptr = ml_get(++curwin->w_cursor.lnum); |
| 1064 | } |
| 1065 | if (was_backslashed) |
| 1066 | newindent = 0; /* Got to end of file */ |
| 1067 | else |
| 1068 | newindent = get_indent(); |
| 1069 | } |
| 1070 | p = skipwhite(ptr); |
| 1071 | if (*p == '}') /* if line starts with '}': do indent */ |
| 1072 | did_si = TRUE; |
| 1073 | else /* can delete indent when '{' typed */ |
| 1074 | can_si_back = TRUE; |
| 1075 | } |
| 1076 | curwin->w_cursor = old_cursor; |
| 1077 | } |
| 1078 | if (do_si) |
| 1079 | can_si = TRUE; |
| 1080 | #endif /* FEAT_SMARTINDENT */ |
| 1081 | |
| 1082 | did_ai = TRUE; |
| 1083 | } |
| 1084 | |
| 1085 | #ifdef FEAT_COMMENTS |
| 1086 | /* |
| 1087 | * Find out if the current line starts with a comment leader. |
| 1088 | * This may then be inserted in front of the new line. |
| 1089 | */ |
| 1090 | end_comment_pending = NUL; |
| 1091 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1092 | lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1093 | else |
| 1094 | lead_len = 0; |
| 1095 | if (lead_len > 0) |
| 1096 | { |
| 1097 | char_u *lead_repl = NULL; /* replaces comment leader */ |
| 1098 | int lead_repl_len = 0; /* length of *lead_repl */ |
| 1099 | char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ |
| 1100 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 1101 | char_u *comment_end = NULL; /* where lead_end has been found */ |
| 1102 | int extra_space = FALSE; /* append extra space */ |
| 1103 | int current_flag; |
| 1104 | int require_blank = FALSE; /* requires blank after middle */ |
| 1105 | char_u *p2; |
| 1106 | |
| 1107 | /* |
| 1108 | * If the comment leader has the start, middle or end flag, it may not |
| 1109 | * be used or may be replaced with the middle leader. |
| 1110 | */ |
| 1111 | for (p = lead_flags; *p && *p != ':'; ++p) |
| 1112 | { |
| 1113 | if (*p == COM_BLANK) |
| 1114 | { |
| 1115 | require_blank = TRUE; |
| 1116 | continue; |
| 1117 | } |
| 1118 | if (*p == COM_START || *p == COM_MIDDLE) |
| 1119 | { |
| 1120 | current_flag = *p; |
| 1121 | if (*p == COM_START) |
| 1122 | { |
| 1123 | /* |
| 1124 | * Doing "O" on a start of comment does not insert leader. |
| 1125 | */ |
| 1126 | if (dir == BACKWARD) |
| 1127 | { |
| 1128 | lead_len = 0; |
| 1129 | break; |
| 1130 | } |
| 1131 | |
| 1132 | /* find start of middle part */ |
| 1133 | (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); |
| 1134 | require_blank = FALSE; |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * Isolate the strings of the middle and end leader. |
| 1139 | */ |
| 1140 | while (*p && p[-1] != ':') /* find end of middle flags */ |
| 1141 | { |
| 1142 | if (*p == COM_BLANK) |
| 1143 | require_blank = TRUE; |
| 1144 | ++p; |
| 1145 | } |
| 1146 | (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); |
| 1147 | |
| 1148 | while (*p && p[-1] != ':') /* find end of end flags */ |
| 1149 | { |
| 1150 | /* Check whether we allow automatic ending of comments */ |
| 1151 | if (*p == COM_AUTO_END) |
| 1152 | end_comment_pending = -1; /* means we want to set it */ |
| 1153 | ++p; |
| 1154 | } |
| 1155 | n = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 1156 | |
| 1157 | if (end_comment_pending == -1) /* we can set it now */ |
| 1158 | end_comment_pending = lead_end[n - 1]; |
| 1159 | |
| 1160 | /* |
| 1161 | * If the end of the comment is in the same line, don't use |
| 1162 | * the comment leader. |
| 1163 | */ |
| 1164 | if (dir == FORWARD) |
| 1165 | { |
| 1166 | for (p = saved_line + lead_len; *p; ++p) |
| 1167 | if (STRNCMP(p, lead_end, n) == 0) |
| 1168 | { |
| 1169 | comment_end = p; |
| 1170 | lead_len = 0; |
| 1171 | break; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * Doing "o" on a start of comment inserts the middle leader. |
| 1177 | */ |
| 1178 | if (lead_len > 0) |
| 1179 | { |
| 1180 | if (current_flag == COM_START) |
| 1181 | { |
| 1182 | lead_repl = lead_middle; |
| 1183 | lead_repl_len = (int)STRLEN(lead_middle); |
| 1184 | } |
| 1185 | |
| 1186 | /* |
| 1187 | * If we have hit RETURN immediately after the start |
| 1188 | * comment leader, then put a space after the middle |
| 1189 | * comment leader on the next line. |
| 1190 | */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1191 | if (!VIM_ISWHITE(saved_line[lead_len - 1]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1192 | && ((p_extra != NULL |
| 1193 | && (int)curwin->w_cursor.col == lead_len) |
| 1194 | || (p_extra == NULL |
| 1195 | && saved_line[lead_len] == NUL) |
| 1196 | || require_blank)) |
| 1197 | extra_space = TRUE; |
| 1198 | } |
| 1199 | break; |
| 1200 | } |
| 1201 | if (*p == COM_END) |
| 1202 | { |
| 1203 | /* |
| 1204 | * Doing "o" on the end of a comment does not insert leader. |
| 1205 | * Remember where the end is, might want to use it to find the |
| 1206 | * start (for C-comments). |
| 1207 | */ |
| 1208 | if (dir == FORWARD) |
| 1209 | { |
| 1210 | comment_end = skipwhite(saved_line); |
| 1211 | lead_len = 0; |
| 1212 | break; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * Doing "O" on the end of a comment inserts the middle leader. |
| 1217 | * Find the string for the middle leader, searching backwards. |
| 1218 | */ |
| 1219 | while (p > curbuf->b_p_com && *p != ',') |
| 1220 | --p; |
| 1221 | for (lead_repl = p; lead_repl > curbuf->b_p_com |
| 1222 | && lead_repl[-1] != ':'; --lead_repl) |
| 1223 | ; |
| 1224 | lead_repl_len = (int)(p - lead_repl); |
| 1225 | |
| 1226 | /* We can probably always add an extra space when doing "O" on |
| 1227 | * the comment-end */ |
| 1228 | extra_space = TRUE; |
| 1229 | |
| 1230 | /* Check whether we allow automatic ending of comments */ |
| 1231 | for (p2 = p; *p2 && *p2 != ':'; p2++) |
| 1232 | { |
| 1233 | if (*p2 == COM_AUTO_END) |
| 1234 | end_comment_pending = -1; /* means we want to set it */ |
| 1235 | } |
| 1236 | if (end_comment_pending == -1) |
| 1237 | { |
| 1238 | /* Find last character in end-comment string */ |
| 1239 | while (*p2 && *p2 != ',') |
| 1240 | p2++; |
| 1241 | end_comment_pending = p2[-1]; |
| 1242 | } |
| 1243 | break; |
| 1244 | } |
| 1245 | if (*p == COM_FIRST) |
| 1246 | { |
| 1247 | /* |
| 1248 | * Comment leader for first line only: Don't repeat leader |
| 1249 | * when using "O", blank out leader when using "o". |
| 1250 | */ |
| 1251 | if (dir == BACKWARD) |
| 1252 | lead_len = 0; |
| 1253 | else |
| 1254 | { |
| 1255 | lead_repl = (char_u *)""; |
| 1256 | lead_repl_len = 0; |
| 1257 | } |
| 1258 | break; |
| 1259 | } |
| 1260 | } |
| 1261 | if (lead_len) |
| 1262 | { |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 1263 | /* allocate buffer (may concatenate p_extra later) */ |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1264 | leader = alloc(lead_len + lead_repl_len + extra_space + extra_len |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 1265 | + (second_line_indent > 0 ? second_line_indent : 0) + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1266 | allocated = leader; /* remember to free it later */ |
| 1267 | |
| 1268 | if (leader == NULL) |
| 1269 | lead_len = 0; |
| 1270 | else |
| 1271 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 1272 | vim_strncpy(leader, saved_line, lead_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1273 | |
| 1274 | /* |
| 1275 | * Replace leader with lead_repl, right or left adjusted |
| 1276 | */ |
| 1277 | if (lead_repl != NULL) |
| 1278 | { |
| 1279 | int c = 0; |
| 1280 | int off = 0; |
| 1281 | |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1282 | for (p = lead_flags; *p != NUL && *p != ':'; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1283 | { |
| 1284 | if (*p == COM_RIGHT || *p == COM_LEFT) |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1285 | c = *p++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1286 | else if (VIM_ISDIGIT(*p) || *p == '-') |
| 1287 | off = getdigits(&p); |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1288 | else |
| 1289 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1290 | } |
| 1291 | if (c == COM_RIGHT) /* right adjusted leader */ |
| 1292 | { |
| 1293 | /* find last non-white in the leader to line up with */ |
| 1294 | for (p = leader + lead_len - 1; p > leader |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1295 | && VIM_ISWHITE(*p); --p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1296 | ; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1297 | ++p; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1298 | |
| 1299 | #ifdef FEAT_MBYTE |
| 1300 | /* Compute the length of the replaced characters in |
| 1301 | * screen characters, not bytes. */ |
| 1302 | { |
| 1303 | int repl_size = vim_strnsize(lead_repl, |
| 1304 | lead_repl_len); |
| 1305 | int old_size = 0; |
| 1306 | char_u *endp = p; |
| 1307 | int l; |
| 1308 | |
| 1309 | while (old_size < repl_size && p > leader) |
| 1310 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 1311 | MB_PTR_BACK(leader, p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1312 | old_size += ptr2cells(p); |
| 1313 | } |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1314 | l = lead_repl_len - (int)(endp - p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1315 | if (l != 0) |
| 1316 | mch_memmove(endp + l, endp, |
| 1317 | (size_t)((leader + lead_len) - endp)); |
| 1318 | lead_len += l; |
| 1319 | } |
| 1320 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1321 | if (p < leader + lead_repl_len) |
| 1322 | p = leader; |
| 1323 | else |
| 1324 | p -= lead_repl_len; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1325 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1326 | mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
| 1327 | if (p + lead_repl_len > leader + lead_len) |
| 1328 | p[lead_repl_len] = NUL; |
| 1329 | |
| 1330 | /* blank-out any other chars from the old leader. */ |
| 1331 | while (--p >= leader) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1332 | { |
| 1333 | #ifdef FEAT_MBYTE |
| 1334 | int l = mb_head_off(leader, p); |
| 1335 | |
| 1336 | if (l > 1) |
| 1337 | { |
| 1338 | p -= l; |
| 1339 | if (ptr2cells(p) > 1) |
| 1340 | { |
| 1341 | p[1] = ' '; |
| 1342 | --l; |
| 1343 | } |
| 1344 | mch_memmove(p + 1, p + l + 1, |
| 1345 | (size_t)((leader + lead_len) - (p + l + 1))); |
| 1346 | lead_len -= l; |
| 1347 | *p = ' '; |
| 1348 | } |
| 1349 | else |
| 1350 | #endif |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1351 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1352 | *p = ' '; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1353 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1354 | } |
| 1355 | else /* left adjusted leader */ |
| 1356 | { |
| 1357 | p = skipwhite(leader); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1358 | #ifdef FEAT_MBYTE |
| 1359 | /* Compute the length of the replaced characters in |
| 1360 | * screen characters, not bytes. Move the part that is |
| 1361 | * not to be overwritten. */ |
| 1362 | { |
| 1363 | int repl_size = vim_strnsize(lead_repl, |
| 1364 | lead_repl_len); |
| 1365 | int i; |
| 1366 | int l; |
| 1367 | |
Bram Moolenaar | dc633cf | 2016-04-23 14:33:19 +0200 | [diff] [blame] | 1368 | for (i = 0; i < lead_len && p[i] != NUL; i += l) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1369 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1370 | l = (*mb_ptr2len)(p + i); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1371 | if (vim_strnsize(p, i + l) > repl_size) |
| 1372 | break; |
| 1373 | } |
| 1374 | if (i != lead_repl_len) |
| 1375 | { |
| 1376 | mch_memmove(p + lead_repl_len, p + i, |
Bram Moolenaar | 2d7ff05 | 2009-11-17 15:08:26 +0000 | [diff] [blame] | 1377 | (size_t)(lead_len - i - (p - leader))); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1378 | lead_len += lead_repl_len - i; |
| 1379 | } |
| 1380 | } |
| 1381 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1382 | mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
| 1383 | |
| 1384 | /* Replace any remaining non-white chars in the old |
| 1385 | * leader by spaces. Keep Tabs, the indent must |
| 1386 | * remain the same. */ |
| 1387 | for (p += lead_repl_len; p < leader + lead_len; ++p) |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1388 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1389 | { |
| 1390 | /* Don't put a space before a TAB. */ |
| 1391 | if (p + 1 < leader + lead_len && p[1] == TAB) |
| 1392 | { |
| 1393 | --lead_len; |
| 1394 | mch_memmove(p, p + 1, |
| 1395 | (leader + lead_len) - p); |
| 1396 | } |
| 1397 | else |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1398 | { |
| 1399 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1400 | int l = (*mb_ptr2len)(p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1401 | |
| 1402 | if (l > 1) |
| 1403 | { |
| 1404 | if (ptr2cells(p) > 1) |
| 1405 | { |
| 1406 | /* Replace a double-wide char with |
| 1407 | * two spaces */ |
| 1408 | --l; |
| 1409 | *p++ = ' '; |
| 1410 | } |
| 1411 | mch_memmove(p + 1, p + l, |
| 1412 | (leader + lead_len) - p); |
| 1413 | lead_len -= l - 1; |
| 1414 | } |
| 1415 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1416 | *p = ' '; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1417 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1418 | } |
| 1419 | *p = NUL; |
| 1420 | } |
| 1421 | |
| 1422 | /* Recompute the indent, it may have changed. */ |
| 1423 | if (curbuf->b_p_ai |
| 1424 | #ifdef FEAT_SMARTINDENT |
| 1425 | || do_si |
| 1426 | #endif |
| 1427 | ) |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 1428 | #ifdef FEAT_VARTABS |
| 1429 | newindent = get_indent_str_vtab(leader, curbuf->b_p_ts, |
| 1430 | curbuf->b_p_vts_array, FALSE); |
| 1431 | #else |
| 1432 | newindent = get_indent_str(leader, |
| 1433 | (int)curbuf->b_p_ts, FALSE); |
| 1434 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1435 | |
| 1436 | /* Add the indent offset */ |
| 1437 | if (newindent + off < 0) |
| 1438 | { |
| 1439 | off = -newindent; |
| 1440 | newindent = 0; |
| 1441 | } |
| 1442 | else |
| 1443 | newindent += off; |
| 1444 | |
| 1445 | /* Correct trailing spaces for the shift, so that |
| 1446 | * alignment remains equal. */ |
| 1447 | while (off > 0 && lead_len > 0 |
| 1448 | && leader[lead_len - 1] == ' ') |
| 1449 | { |
| 1450 | /* Don't do it when there is a tab before the space */ |
| 1451 | if (vim_strchr(skipwhite(leader), '\t') != NULL) |
| 1452 | break; |
| 1453 | --lead_len; |
| 1454 | --off; |
| 1455 | } |
| 1456 | |
| 1457 | /* If the leader ends in white space, don't add an |
| 1458 | * extra space */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1459 | if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1460 | extra_space = FALSE; |
| 1461 | leader[lead_len] = NUL; |
| 1462 | } |
| 1463 | |
| 1464 | if (extra_space) |
| 1465 | { |
| 1466 | leader[lead_len++] = ' '; |
| 1467 | leader[lead_len] = NUL; |
| 1468 | } |
| 1469 | |
| 1470 | newcol = lead_len; |
| 1471 | |
| 1472 | /* |
| 1473 | * if a new indent will be set below, remove the indent that |
| 1474 | * is in the comment leader |
| 1475 | */ |
| 1476 | if (newindent |
| 1477 | #ifdef FEAT_SMARTINDENT |
| 1478 | || did_si |
| 1479 | #endif |
| 1480 | ) |
| 1481 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1482 | while (lead_len && VIM_ISWHITE(*leader)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1483 | { |
| 1484 | --lead_len; |
| 1485 | --newcol; |
| 1486 | ++leader; |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | } |
| 1491 | #ifdef FEAT_SMARTINDENT |
| 1492 | did_si = can_si = FALSE; |
| 1493 | #endif |
| 1494 | } |
| 1495 | else if (comment_end != NULL) |
| 1496 | { |
| 1497 | /* |
| 1498 | * We have finished a comment, so we don't use the leader. |
| 1499 | * If this was a C-comment and 'ai' or 'si' is set do a normal |
| 1500 | * indent to align with the line containing the start of the |
| 1501 | * comment. |
| 1502 | */ |
| 1503 | if (comment_end[0] == '*' && comment_end[1] == '/' && |
| 1504 | (curbuf->b_p_ai |
| 1505 | #ifdef FEAT_SMARTINDENT |
| 1506 | || do_si |
| 1507 | #endif |
| 1508 | )) |
| 1509 | { |
| 1510 | old_cursor = curwin->w_cursor; |
| 1511 | curwin->w_cursor.col = (colnr_T)(comment_end - saved_line); |
| 1512 | if ((pos = findmatch(NULL, NUL)) != NULL) |
| 1513 | { |
| 1514 | curwin->w_cursor.lnum = pos->lnum; |
| 1515 | newindent = get_indent(); |
| 1516 | } |
| 1517 | curwin->w_cursor = old_cursor; |
| 1518 | } |
| 1519 | } |
| 1520 | } |
| 1521 | #endif |
| 1522 | |
| 1523 | /* (State == INSERT || State == REPLACE), only when dir == FORWARD */ |
| 1524 | if (p_extra != NULL) |
| 1525 | { |
| 1526 | *p_extra = saved_char; /* restore char that NUL replaced */ |
| 1527 | |
| 1528 | /* |
| 1529 | * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first |
| 1530 | * non-blank. |
| 1531 | * |
| 1532 | * When in REPLACE mode, put the deleted blanks on the replace stack, |
| 1533 | * preceded by a NUL, so they can be put back when a BS is entered. |
| 1534 | */ |
| 1535 | if (REPLACE_NORMAL(State)) |
| 1536 | replace_push(NUL); /* end of extra blanks */ |
| 1537 | if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES)) |
| 1538 | { |
| 1539 | while ((*p_extra == ' ' || *p_extra == '\t') |
| 1540 | #ifdef FEAT_MBYTE |
| 1541 | && (!enc_utf8 |
| 1542 | || !utf_iscomposing(utf_ptr2char(p_extra + 1))) |
| 1543 | #endif |
| 1544 | ) |
| 1545 | { |
| 1546 | if (REPLACE_NORMAL(State)) |
| 1547 | replace_push(*p_extra); |
| 1548 | ++p_extra; |
| 1549 | ++less_cols_off; |
| 1550 | } |
| 1551 | } |
| 1552 | if (*p_extra != NUL) |
| 1553 | did_ai = FALSE; /* append some text, don't truncate now */ |
| 1554 | |
| 1555 | /* columns for marks adjusted for removed columns */ |
| 1556 | less_cols = (int)(p_extra - saved_line); |
| 1557 | } |
| 1558 | |
| 1559 | if (p_extra == NULL) |
| 1560 | p_extra = (char_u *)""; /* append empty line */ |
| 1561 | |
| 1562 | #ifdef FEAT_COMMENTS |
| 1563 | /* concatenate leader and p_extra, if there is a leader */ |
| 1564 | if (lead_len) |
| 1565 | { |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1566 | if (flags & OPENLINE_COM_LIST && second_line_indent > 0) |
| 1567 | { |
| 1568 | int i; |
Bram Moolenaar | 3610578 | 2012-06-14 20:59:25 +0200 | [diff] [blame] | 1569 | int padding = second_line_indent |
| 1570 | - (newindent + (int)STRLEN(leader)); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1571 | |
| 1572 | /* Here whitespace is inserted after the comment char. |
| 1573 | * Below, set_indent(newindent, SIN_INSERT) will insert the |
| 1574 | * whitespace needed before the comment char. */ |
| 1575 | for (i = 0; i < padding; i++) |
| 1576 | { |
| 1577 | STRCAT(leader, " "); |
Bram Moolenaar | 5fb9ec5 | 2012-07-25 16:10:03 +0200 | [diff] [blame] | 1578 | less_cols--; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1579 | newcol++; |
| 1580 | } |
| 1581 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1582 | STRCAT(leader, p_extra); |
| 1583 | p_extra = leader; |
| 1584 | did_ai = TRUE; /* So truncating blanks works with comments */ |
| 1585 | less_cols -= lead_len; |
| 1586 | } |
| 1587 | else |
| 1588 | end_comment_pending = NUL; /* turns out there was no leader */ |
| 1589 | #endif |
| 1590 | |
| 1591 | old_cursor = curwin->w_cursor; |
| 1592 | if (dir == BACKWARD) |
| 1593 | --curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1594 | if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1595 | { |
| 1596 | if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE) |
| 1597 | == FAIL) |
| 1598 | goto theend; |
| 1599 | /* Postpone calling changed_lines(), because it would mess up folding |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 1600 | * with markers. |
| 1601 | * 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] | 1602 | * be marks there. But still needed in diff mode. */ |
| 1603 | if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count |
| 1604 | #ifdef FEAT_DIFF |
| 1605 | || curwin->w_p_diff |
| 1606 | #endif |
| 1607 | ) |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 1608 | mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1609 | did_append = TRUE; |
| 1610 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1611 | else |
| 1612 | { |
| 1613 | /* |
| 1614 | * In VREPLACE mode we are starting to replace the next line. |
| 1615 | */ |
| 1616 | curwin->w_cursor.lnum++; |
| 1617 | if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed) |
| 1618 | { |
| 1619 | /* In case we NL to a new line, BS to the previous one, and NL |
| 1620 | * again, we don't want to save the new line for undo twice. |
| 1621 | */ |
| 1622 | (void)u_save_cursor(); /* errors are ignored! */ |
| 1623 | vr_lines_changed++; |
| 1624 | } |
| 1625 | ml_replace(curwin->w_cursor.lnum, p_extra, TRUE); |
| 1626 | changed_bytes(curwin->w_cursor.lnum, 0); |
| 1627 | curwin->w_cursor.lnum--; |
| 1628 | did_append = FALSE; |
| 1629 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1630 | |
| 1631 | if (newindent |
| 1632 | #ifdef FEAT_SMARTINDENT |
| 1633 | || did_si |
| 1634 | #endif |
| 1635 | ) |
| 1636 | { |
| 1637 | ++curwin->w_cursor.lnum; |
| 1638 | #ifdef FEAT_SMARTINDENT |
| 1639 | if (did_si) |
| 1640 | { |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 1641 | int sw = (int)get_sw_value(curbuf); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 1642 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1643 | if (p_sr) |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 1644 | newindent -= newindent % sw; |
| 1645 | newindent += sw; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1646 | } |
| 1647 | #endif |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 1648 | /* Copy the indent */ |
| 1649 | if (curbuf->b_p_ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1650 | { |
| 1651 | (void)copy_indent(newindent, saved_line); |
| 1652 | |
| 1653 | /* |
| 1654 | * Set the 'preserveindent' option so that any further screwing |
| 1655 | * with the line doesn't entirely destroy our efforts to preserve |
| 1656 | * it. It gets restored at the function end. |
| 1657 | */ |
| 1658 | curbuf->b_p_pi = TRUE; |
| 1659 | } |
| 1660 | else |
| 1661 | (void)set_indent(newindent, SIN_INSERT); |
| 1662 | less_cols -= curwin->w_cursor.col; |
| 1663 | |
| 1664 | ai_col = curwin->w_cursor.col; |
| 1665 | |
| 1666 | /* |
| 1667 | * In REPLACE mode, for each character in the new indent, there must |
| 1668 | * be a NUL on the replace stack, for when it is deleted with BS |
| 1669 | */ |
| 1670 | if (REPLACE_NORMAL(State)) |
| 1671 | for (n = 0; n < (int)curwin->w_cursor.col; ++n) |
| 1672 | replace_push(NUL); |
| 1673 | newcol += curwin->w_cursor.col; |
| 1674 | #ifdef FEAT_SMARTINDENT |
| 1675 | if (no_si) |
| 1676 | did_si = FALSE; |
| 1677 | #endif |
| 1678 | } |
| 1679 | |
| 1680 | #ifdef FEAT_COMMENTS |
| 1681 | /* |
| 1682 | * In REPLACE mode, for each character in the extra leader, there must be |
| 1683 | * a NUL on the replace stack, for when it is deleted with BS. |
| 1684 | */ |
| 1685 | if (REPLACE_NORMAL(State)) |
| 1686 | while (lead_len-- > 0) |
| 1687 | replace_push(NUL); |
| 1688 | #endif |
| 1689 | |
| 1690 | curwin->w_cursor = old_cursor; |
| 1691 | |
| 1692 | if (dir == FORWARD) |
| 1693 | { |
| 1694 | if (trunc_line || (State & INSERT)) |
| 1695 | { |
| 1696 | /* truncate current line at cursor */ |
| 1697 | saved_line[curwin->w_cursor.col] = NUL; |
| 1698 | /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */ |
| 1699 | if (trunc_line && !(flags & OPENLINE_KEEPTRAIL)) |
| 1700 | truncate_spaces(saved_line); |
| 1701 | ml_replace(curwin->w_cursor.lnum, saved_line, FALSE); |
| 1702 | saved_line = NULL; |
| 1703 | if (did_append) |
| 1704 | { |
| 1705 | changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, |
| 1706 | curwin->w_cursor.lnum + 1, 1L); |
| 1707 | did_append = FALSE; |
| 1708 | |
| 1709 | /* Move marks after the line break to the new line. */ |
| 1710 | if (flags & OPENLINE_MARKFIX) |
| 1711 | mark_col_adjust(curwin->w_cursor.lnum, |
| 1712 | curwin->w_cursor.col + less_cols_off, |
Bram Moolenaar | e1e714e | 2018-12-31 23:58:24 +0100 | [diff] [blame] | 1713 | 1L, (long)-less_cols, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1714 | } |
| 1715 | else |
| 1716 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 1717 | } |
| 1718 | |
| 1719 | /* |
| 1720 | * Put the cursor on the new line. Careful: the scrollup() above may |
| 1721 | * have moved w_cursor, we must use old_cursor. |
| 1722 | */ |
| 1723 | curwin->w_cursor.lnum = old_cursor.lnum + 1; |
| 1724 | } |
| 1725 | if (did_append) |
| 1726 | changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L); |
| 1727 | |
| 1728 | curwin->w_cursor.col = newcol; |
| 1729 | #ifdef FEAT_VIRTUALEDIT |
| 1730 | curwin->w_cursor.coladd = 0; |
| 1731 | #endif |
| 1732 | |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 1733 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1734 | /* |
| 1735 | * In VREPLACE mode, we are handling the replace stack ourselves, so stop |
| 1736 | * fixthisline() from doing it (via change_indent()) by telling it we're in |
| 1737 | * normal INSERT mode. |
| 1738 | */ |
| 1739 | if (State & VREPLACE_FLAG) |
| 1740 | { |
| 1741 | vreplace_mode = State; /* So we know to put things right later */ |
| 1742 | State = INSERT; |
| 1743 | } |
| 1744 | else |
| 1745 | vreplace_mode = 0; |
| 1746 | #endif |
| 1747 | #ifdef FEAT_LISP |
| 1748 | /* |
| 1749 | * May do lisp indenting. |
| 1750 | */ |
| 1751 | if (!p_paste |
| 1752 | # ifdef FEAT_COMMENTS |
| 1753 | && leader == NULL |
| 1754 | # endif |
| 1755 | && curbuf->b_p_lisp |
| 1756 | && curbuf->b_p_ai) |
| 1757 | { |
| 1758 | fixthisline(get_lisp_indent); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1759 | ai_col = (colnr_T)getwhitecols_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1760 | } |
| 1761 | #endif |
| 1762 | #ifdef FEAT_CINDENT |
| 1763 | /* |
| 1764 | * May do indenting after opening a new line. |
| 1765 | */ |
| 1766 | if (!p_paste |
| 1767 | && (curbuf->b_p_cin |
| 1768 | # ifdef FEAT_EVAL |
| 1769 | || *curbuf->b_p_inde != NUL |
| 1770 | # endif |
| 1771 | ) |
| 1772 | && in_cinkeys(dir == FORWARD |
| 1773 | ? KEY_OPEN_FORW |
| 1774 | : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))) |
| 1775 | { |
| 1776 | do_c_expr_indent(); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1777 | ai_col = (colnr_T)getwhitecols_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1778 | } |
| 1779 | #endif |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 1780 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1781 | if (vreplace_mode != 0) |
| 1782 | State = vreplace_mode; |
| 1783 | #endif |
| 1784 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1785 | /* |
| 1786 | * Finally, VREPLACE gets the stuff on the new line, then puts back the |
| 1787 | * original line, and inserts the new stuff char by char, pushing old stuff |
| 1788 | * onto the replace stack (via ins_char()). |
| 1789 | */ |
| 1790 | if (State & VREPLACE_FLAG) |
| 1791 | { |
| 1792 | /* Put new line in p_extra */ |
| 1793 | p_extra = vim_strsave(ml_get_curline()); |
| 1794 | if (p_extra == NULL) |
| 1795 | goto theend; |
| 1796 | |
| 1797 | /* Put back original line */ |
| 1798 | ml_replace(curwin->w_cursor.lnum, next_line, FALSE); |
| 1799 | |
| 1800 | /* Insert new stuff into line again */ |
| 1801 | curwin->w_cursor.col = 0; |
| 1802 | #ifdef FEAT_VIRTUALEDIT |
| 1803 | curwin->w_cursor.coladd = 0; |
| 1804 | #endif |
| 1805 | ins_bytes(p_extra); /* will call changed_bytes() */ |
| 1806 | vim_free(p_extra); |
| 1807 | next_line = NULL; |
| 1808 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1809 | |
Bram Moolenaar | 24a2d72 | 2018-04-24 19:36:43 +0200 | [diff] [blame] | 1810 | retval = OK; /* success! */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1811 | theend: |
| 1812 | curbuf->b_p_pi = saved_pi; |
| 1813 | vim_free(saved_line); |
| 1814 | vim_free(next_line); |
| 1815 | vim_free(allocated); |
| 1816 | return retval; |
| 1817 | } |
| 1818 | |
| 1819 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 1820 | /* |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 1821 | * get_leader_len() returns the length in bytes of the prefix of the given |
| 1822 | * string which introduces a comment. If this string is not a comment then |
| 1823 | * 0 is returned. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1824 | * When "flags" is not NULL, it is set to point to the flags of the recognized |
| 1825 | * comment leader. |
| 1826 | * "backward" must be true for the "O" command. |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1827 | * If "include_space" is set, include trailing whitespace while calculating the |
| 1828 | * length. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1829 | */ |
| 1830 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1831 | get_leader_len( |
| 1832 | char_u *line, |
| 1833 | char_u **flags, |
| 1834 | int backward, |
| 1835 | int include_space) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1836 | { |
| 1837 | int i, j; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1838 | int result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1839 | int got_com = FALSE; |
| 1840 | int found_one; |
| 1841 | char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ |
| 1842 | char_u *string; /* pointer to comment string */ |
| 1843 | char_u *list; |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1844 | int middle_match_len = 0; |
| 1845 | char_u *prev_list; |
Bram Moolenaar | 05da428 | 2011-05-10 14:44:11 +0200 | [diff] [blame] | 1846 | char_u *saved_flags = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1847 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1848 | result = i = 0; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1849 | while (VIM_ISWHITE(line[i])) /* leading white space is ignored */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1850 | ++i; |
| 1851 | |
| 1852 | /* |
| 1853 | * Repeat to match several nested comment strings. |
| 1854 | */ |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1855 | while (line[i] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1856 | { |
| 1857 | /* |
| 1858 | * scan through the 'comments' option for a match |
| 1859 | */ |
| 1860 | found_one = FALSE; |
| 1861 | for (list = curbuf->b_p_com; *list; ) |
| 1862 | { |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1863 | /* Get one option part into part_buf[]. Advance "list" to next |
| 1864 | * one. Put "string" at start of string. */ |
| 1865 | if (!got_com && flags != NULL) |
| 1866 | *flags = list; /* remember where flags started */ |
| 1867 | prev_list = list; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1868 | (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
| 1869 | string = vim_strchr(part_buf, ':'); |
| 1870 | if (string == NULL) /* missing ':', ignore this part */ |
| 1871 | continue; |
| 1872 | *string++ = NUL; /* isolate flags from string */ |
| 1873 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1874 | /* If we found a middle match previously, use that match when this |
| 1875 | * is not a middle or end. */ |
| 1876 | if (middle_match_len != 0 |
| 1877 | && vim_strchr(part_buf, COM_MIDDLE) == NULL |
| 1878 | && vim_strchr(part_buf, COM_END) == NULL) |
| 1879 | break; |
| 1880 | |
| 1881 | /* When we already found a nested comment, only accept further |
| 1882 | * nested comments. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1883 | if (got_com && vim_strchr(part_buf, COM_NEST) == NULL) |
| 1884 | continue; |
| 1885 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1886 | /* When 'O' flag present and using "O" command skip this one. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1887 | if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL) |
| 1888 | continue; |
| 1889 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1890 | /* Line contents and string must match. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1891 | * When string starts with white space, must have some white space |
| 1892 | * (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] | 1893 | * TABs and spaces). */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1894 | if (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1895 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1896 | if (i == 0 || !VIM_ISWHITE(line[i - 1])) |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 1897 | continue; /* missing white space */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1898 | while (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1899 | ++string; |
| 1900 | } |
| 1901 | for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) |
| 1902 | ; |
| 1903 | if (string[j] != NUL) |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1904 | continue; /* string doesn't match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1905 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1906 | /* When 'b' flag used, there must be white space or an |
| 1907 | * end-of-line after the string in the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1908 | if (vim_strchr(part_buf, COM_BLANK) != NULL |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1909 | && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1910 | continue; |
| 1911 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1912 | /* We have found a match, stop searching unless this is a middle |
| 1913 | * comment. The middle comment can be a substring of the end |
| 1914 | * comment in which case it's better to return the length of the |
| 1915 | * end comment and its flags. Thus we keep searching with middle |
| 1916 | * and end matches and use an end match if it matches better. */ |
| 1917 | if (vim_strchr(part_buf, COM_MIDDLE) != NULL) |
| 1918 | { |
| 1919 | if (middle_match_len == 0) |
| 1920 | { |
| 1921 | middle_match_len = j; |
| 1922 | saved_flags = prev_list; |
| 1923 | } |
| 1924 | continue; |
| 1925 | } |
| 1926 | if (middle_match_len != 0 && j > middle_match_len) |
| 1927 | /* Use this match instead of the middle match, since it's a |
| 1928 | * longer thus better match. */ |
| 1929 | middle_match_len = 0; |
| 1930 | |
| 1931 | if (middle_match_len == 0) |
| 1932 | i += j; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1933 | found_one = TRUE; |
| 1934 | break; |
| 1935 | } |
| 1936 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1937 | if (middle_match_len != 0) |
| 1938 | { |
| 1939 | /* Use the previously found middle match after failing to find a |
| 1940 | * match with an end. */ |
| 1941 | if (!got_com && flags != NULL) |
| 1942 | *flags = saved_flags; |
| 1943 | i += middle_match_len; |
| 1944 | found_one = TRUE; |
| 1945 | } |
| 1946 | |
| 1947 | /* No match found, stop scanning. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1948 | if (!found_one) |
| 1949 | break; |
| 1950 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1951 | result = i; |
| 1952 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1953 | /* Include any trailing white space. */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1954 | while (VIM_ISWHITE(line[i])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1955 | ++i; |
| 1956 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1957 | if (include_space) |
| 1958 | result = i; |
| 1959 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1960 | /* If this comment doesn't nest, stop here. */ |
| 1961 | got_com = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1962 | if (vim_strchr(part_buf, COM_NEST) == NULL) |
| 1963 | break; |
| 1964 | } |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1965 | return result; |
| 1966 | } |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1967 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1968 | /* |
| 1969 | * Return the offset at which the last comment in line starts. If there is no |
| 1970 | * comment in the whole line, -1 is returned. |
| 1971 | * |
| 1972 | * When "flags" is not null, it is set to point to the flags describing the |
| 1973 | * recognized comment leader. |
| 1974 | */ |
| 1975 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1976 | get_last_leader_offset(char_u *line, char_u **flags) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1977 | { |
| 1978 | int result = -1; |
| 1979 | int i, j; |
| 1980 | int lower_check_bound = 0; |
| 1981 | char_u *string; |
| 1982 | char_u *com_leader; |
| 1983 | char_u *com_flags; |
| 1984 | char_u *list; |
| 1985 | int found_one; |
| 1986 | char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ |
| 1987 | |
| 1988 | /* |
| 1989 | * Repeat to match several nested comment strings. |
| 1990 | */ |
| 1991 | i = (int)STRLEN(line); |
| 1992 | while (--i >= lower_check_bound) |
| 1993 | { |
| 1994 | /* |
| 1995 | * scan through the 'comments' option for a match |
| 1996 | */ |
| 1997 | found_one = FALSE; |
| 1998 | for (list = curbuf->b_p_com; *list; ) |
| 1999 | { |
| 2000 | char_u *flags_save = list; |
| 2001 | |
| 2002 | /* |
| 2003 | * Get one option part into part_buf[]. Advance list to next one. |
| 2004 | * put string at start of string. |
| 2005 | */ |
| 2006 | (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
| 2007 | string = vim_strchr(part_buf, ':'); |
| 2008 | if (string == NULL) /* If everything is fine, this cannot actually |
| 2009 | * happen. */ |
| 2010 | { |
| 2011 | continue; |
| 2012 | } |
| 2013 | *string++ = NUL; /* Isolate flags from string. */ |
| 2014 | com_leader = string; |
| 2015 | |
| 2016 | /* |
| 2017 | * Line contents and string must match. |
| 2018 | * When string starts with white space, must have some white space |
| 2019 | * (but the amount does not need to match, there might be a mix of |
| 2020 | * TABs and spaces). |
| 2021 | */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2022 | if (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2023 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2024 | if (i == 0 || !VIM_ISWHITE(line[i - 1])) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2025 | continue; |
Bram Moolenaar | 5393281 | 2018-12-07 21:08:49 +0100 | [diff] [blame] | 2026 | while (VIM_ISWHITE(*string)) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2027 | ++string; |
| 2028 | } |
| 2029 | for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) |
| 2030 | /* do nothing */; |
| 2031 | if (string[j] != NUL) |
| 2032 | continue; |
| 2033 | |
| 2034 | /* |
| 2035 | * When 'b' flag used, there must be white space or an |
| 2036 | * end-of-line after the string in the line. |
| 2037 | */ |
| 2038 | if (vim_strchr(part_buf, COM_BLANK) != NULL |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2039 | && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2040 | continue; |
Bram Moolenaar | 5393281 | 2018-12-07 21:08:49 +0100 | [diff] [blame] | 2041 | |
Bram Moolenaar | 4af7259 | 2018-12-09 15:00:52 +0100 | [diff] [blame] | 2042 | if (vim_strchr(part_buf, COM_MIDDLE) != NULL) |
Bram Moolenaar | 5393281 | 2018-12-07 21:08:49 +0100 | [diff] [blame] | 2043 | { |
Bram Moolenaar | 4af7259 | 2018-12-09 15:00:52 +0100 | [diff] [blame] | 2044 | // For a middlepart comment, only consider it to match if |
| 2045 | // everything before the current position in the line is |
| 2046 | // whitespace. Otherwise we would think we are inside a |
| 2047 | // comment if the middle part appears somewhere in the middle |
| 2048 | // of the line. E.g. for C the "*" appears often. |
Bram Moolenaar | 5393281 | 2018-12-07 21:08:49 +0100 | [diff] [blame] | 2049 | for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++) |
| 2050 | ; |
| 2051 | if (j < i) |
| 2052 | continue; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | /* |
| 2056 | * We have found a match, stop searching. |
| 2057 | */ |
| 2058 | found_one = TRUE; |
| 2059 | |
| 2060 | if (flags) |
| 2061 | *flags = flags_save; |
| 2062 | com_flags = flags_save; |
| 2063 | |
| 2064 | break; |
| 2065 | } |
| 2066 | |
| 2067 | if (found_one) |
| 2068 | { |
| 2069 | char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */ |
| 2070 | int len1, len2, off; |
| 2071 | |
| 2072 | result = i; |
| 2073 | /* |
| 2074 | * If this comment nests, continue searching. |
| 2075 | */ |
| 2076 | if (vim_strchr(part_buf, COM_NEST) != NULL) |
| 2077 | continue; |
| 2078 | |
| 2079 | lower_check_bound = i; |
| 2080 | |
| 2081 | /* Let's verify whether the comment leader found is a substring |
| 2082 | * of other comment leaders. If it is, let's adjust the |
| 2083 | * lower_check_bound so that we make sure that we have determined |
| 2084 | * the comment leader correctly. |
| 2085 | */ |
| 2086 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2087 | while (VIM_ISWHITE(*com_leader)) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2088 | ++com_leader; |
| 2089 | len1 = (int)STRLEN(com_leader); |
| 2090 | |
| 2091 | for (list = curbuf->b_p_com; *list; ) |
| 2092 | { |
| 2093 | char_u *flags_save = list; |
| 2094 | |
| 2095 | (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ","); |
| 2096 | if (flags_save == com_flags) |
| 2097 | continue; |
| 2098 | string = vim_strchr(part_buf2, ':'); |
| 2099 | ++string; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2100 | while (VIM_ISWHITE(*string)) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2101 | ++string; |
| 2102 | len2 = (int)STRLEN(string); |
| 2103 | if (len2 == 0) |
| 2104 | continue; |
| 2105 | |
| 2106 | /* Now we have to verify whether string ends with a substring |
| 2107 | * beginning the com_leader. */ |
| 2108 | for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;) |
| 2109 | { |
| 2110 | --off; |
| 2111 | if (!STRNCMP(string + off, com_leader, len2 - off)) |
| 2112 | { |
| 2113 | if (i - off < lower_check_bound) |
| 2114 | lower_check_bound = i - off; |
| 2115 | } |
| 2116 | } |
| 2117 | } |
| 2118 | } |
| 2119 | } |
| 2120 | return result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2121 | } |
| 2122 | #endif |
| 2123 | |
| 2124 | /* |
| 2125 | * Return the number of window lines occupied by buffer line "lnum". |
| 2126 | */ |
| 2127 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2128 | plines(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2129 | { |
| 2130 | return plines_win(curwin, lnum, TRUE); |
| 2131 | } |
| 2132 | |
| 2133 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2134 | plines_win( |
| 2135 | win_T *wp, |
| 2136 | linenr_T lnum, |
| 2137 | int winheight) /* when TRUE limit to window height */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2138 | { |
| 2139 | #if defined(FEAT_DIFF) || defined(PROTO) |
| 2140 | /* Check for filler lines above this buffer line. When folded the result |
| 2141 | * is one line anyway. */ |
| 2142 | return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum); |
| 2143 | } |
| 2144 | |
| 2145 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2146 | plines_nofill(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2147 | { |
| 2148 | return plines_win_nofill(curwin, lnum, TRUE); |
| 2149 | } |
| 2150 | |
| 2151 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2152 | plines_win_nofill( |
| 2153 | win_T *wp, |
| 2154 | linenr_T lnum, |
| 2155 | int winheight) /* when TRUE limit to window height */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2156 | { |
| 2157 | #endif |
| 2158 | int lines; |
| 2159 | |
| 2160 | if (!wp->w_p_wrap) |
| 2161 | return 1; |
| 2162 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2163 | if (wp->w_width == 0) |
| 2164 | return 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2165 | |
| 2166 | #ifdef FEAT_FOLDING |
| 2167 | /* A folded lines is handled just like an empty line. */ |
| 2168 | /* NOTE: Caller must handle lines that are MAYBE folded. */ |
| 2169 | if (lineFolded(wp, lnum) == TRUE) |
| 2170 | return 1; |
| 2171 | #endif |
| 2172 | |
| 2173 | lines = plines_win_nofold(wp, lnum); |
| 2174 | if (winheight > 0 && lines > wp->w_height) |
| 2175 | return (int)wp->w_height; |
| 2176 | return lines; |
| 2177 | } |
| 2178 | |
| 2179 | /* |
| 2180 | * Return number of window lines physical line "lnum" will occupy in window |
| 2181 | * "wp". Does not care about folding, 'wrap' or 'diff'. |
| 2182 | */ |
| 2183 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2184 | plines_win_nofold(win_T *wp, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2185 | { |
| 2186 | char_u *s; |
| 2187 | long col; |
| 2188 | int width; |
| 2189 | |
| 2190 | s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
| 2191 | if (*s == NUL) /* empty line */ |
| 2192 | return 1; |
| 2193 | col = win_linetabsize(wp, s, (colnr_T)MAXCOL); |
| 2194 | |
| 2195 | /* |
| 2196 | * If list mode is on, then the '$' at the end of the line may take up one |
| 2197 | * extra column. |
| 2198 | */ |
| 2199 | if (wp->w_p_list && lcs_eol != NUL) |
| 2200 | col += 1; |
| 2201 | |
| 2202 | /* |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 2203 | * Add column offset for 'number', 'relativenumber' and 'foldcolumn'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2204 | */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2205 | width = wp->w_width - win_col_off(wp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2206 | if (width <= 0) |
| 2207 | return 32000; |
| 2208 | if (col <= width) |
| 2209 | return 1; |
| 2210 | col -= width; |
| 2211 | width += win_col_off2(wp); |
| 2212 | return (col + (width - 1)) / width + 1; |
| 2213 | } |
| 2214 | |
| 2215 | /* |
| 2216 | * Like plines_win(), but only reports the number of physical screen lines |
| 2217 | * used from the start of the line to the given column number. |
| 2218 | */ |
| 2219 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2220 | plines_win_col(win_T *wp, linenr_T lnum, long column) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2221 | { |
| 2222 | long col; |
| 2223 | char_u *s; |
| 2224 | int lines = 0; |
| 2225 | int width; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2226 | char_u *line; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2227 | |
| 2228 | #ifdef FEAT_DIFF |
| 2229 | /* Check for filler lines above this buffer line. When folded the result |
| 2230 | * is one line anyway. */ |
| 2231 | lines = diff_check_fill(wp, lnum); |
| 2232 | #endif |
| 2233 | |
| 2234 | if (!wp->w_p_wrap) |
| 2235 | return lines + 1; |
| 2236 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2237 | if (wp->w_width == 0) |
| 2238 | return lines + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2239 | |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2240 | line = s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2241 | |
| 2242 | col = 0; |
| 2243 | while (*s != NUL && --column >= 0) |
| 2244 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2245 | col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL); |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 2246 | MB_PTR_ADV(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | /* |
| 2250 | * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in |
| 2251 | * INSERT mode, then col must be adjusted so that it represents the last |
| 2252 | * screen position of the TAB. This only fixes an error when the TAB wraps |
| 2253 | * from one screen line to the next (when 'columns' is not a multiple of |
| 2254 | * 'ts') -- webb. |
| 2255 | */ |
| 2256 | if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1)) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2257 | col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2258 | |
| 2259 | /* |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 2260 | * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2261 | */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2262 | width = wp->w_width - win_col_off(wp); |
Bram Moolenaar | 2647063 | 2006-10-24 19:12:40 +0000 | [diff] [blame] | 2263 | if (width <= 0) |
| 2264 | return 9999; |
| 2265 | |
| 2266 | lines += 1; |
| 2267 | if (col > width) |
| 2268 | lines += (col - width) / (width + win_col_off2(wp)) + 1; |
| 2269 | return lines; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2273 | plines_m_win(win_T *wp, linenr_T first, linenr_T last) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2274 | { |
| 2275 | int count = 0; |
| 2276 | |
| 2277 | while (first <= last) |
| 2278 | { |
| 2279 | #ifdef FEAT_FOLDING |
| 2280 | int x; |
| 2281 | |
| 2282 | /* Check if there are any really folded lines, but also included lines |
| 2283 | * that are maybe folded. */ |
| 2284 | x = foldedCount(wp, first, NULL); |
| 2285 | if (x > 0) |
| 2286 | { |
| 2287 | ++count; /* count 1 for "+-- folded" line */ |
| 2288 | first += x; |
| 2289 | } |
| 2290 | else |
| 2291 | #endif |
| 2292 | { |
| 2293 | #ifdef FEAT_DIFF |
| 2294 | if (first == wp->w_topline) |
| 2295 | count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill; |
| 2296 | else |
| 2297 | #endif |
| 2298 | count += plines_win(wp, first, TRUE); |
| 2299 | ++first; |
| 2300 | } |
| 2301 | } |
| 2302 | return (count); |
| 2303 | } |
| 2304 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2305 | /* |
| 2306 | * Insert string "p" at the cursor position. Stops at a NUL byte. |
| 2307 | * Handles Replace mode and multi-byte characters. |
| 2308 | */ |
| 2309 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2310 | ins_bytes(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2311 | { |
| 2312 | ins_bytes_len(p, (int)STRLEN(p)); |
| 2313 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2314 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2315 | /* |
| 2316 | * Insert string "p" with length "len" at the cursor position. |
| 2317 | * Handles Replace mode and multi-byte characters. |
| 2318 | */ |
| 2319 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2320 | ins_bytes_len(char_u *p, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2321 | { |
| 2322 | int i; |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 2323 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2324 | int n; |
| 2325 | |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2326 | if (has_mbyte) |
| 2327 | for (i = 0; i < len; i += n) |
| 2328 | { |
| 2329 | if (enc_utf8) |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2330 | // avoid reading past p[len] |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2331 | n = utfc_ptr2len_len(p + i, len - i); |
| 2332 | else |
| 2333 | n = (*mb_ptr2len)(p + i); |
| 2334 | ins_char_bytes(p + i, n); |
| 2335 | } |
| 2336 | else |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 2337 | #endif |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2338 | for (i = 0; i < len; ++i) |
| 2339 | ins_char(p[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2340 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2341 | |
| 2342 | /* |
| 2343 | * Insert or replace a single character at the cursor position. |
| 2344 | * When in REPLACE or VREPLACE mode, replace any existing character. |
| 2345 | * Caller must have prepared for undo. |
| 2346 | * For multi-byte characters we get the whole character, the caller must |
| 2347 | * convert bytes to a character. |
| 2348 | */ |
| 2349 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2350 | ins_char(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2351 | { |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 2352 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | 560379d | 2017-01-21 22:50:00 +0100 | [diff] [blame] | 2353 | int n = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2354 | |
Bram Moolenaar | 6a8ede9 | 2017-01-22 19:49:12 +0100 | [diff] [blame] | 2355 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2356 | n = (*mb_char2bytes)(c, buf); |
| 2357 | |
| 2358 | /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte. |
| 2359 | * Happens for CTRL-Vu9900. */ |
| 2360 | if (buf[0] == 0) |
| 2361 | buf[0] = '\n'; |
Bram Moolenaar | 560379d | 2017-01-21 22:50:00 +0100 | [diff] [blame] | 2362 | #else |
| 2363 | buf[0] = c; |
| 2364 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2365 | |
| 2366 | ins_char_bytes(buf, n); |
| 2367 | } |
| 2368 | |
| 2369 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2370 | ins_char_bytes(char_u *buf, int charlen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2371 | { |
| 2372 | int c = buf[0]; |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2373 | int newlen; // nr of bytes inserted |
| 2374 | int oldlen; // nr of bytes deleted (0 when not replacing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2375 | char_u *p; |
| 2376 | char_u *newp; |
| 2377 | char_u *oldp; |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2378 | int linelen; // length of old line including NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2379 | colnr_T col; |
| 2380 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2381 | int i; |
| 2382 | |
| 2383 | #ifdef FEAT_VIRTUALEDIT |
| 2384 | /* Break tabs if needed. */ |
| 2385 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 2386 | coladvance_force(getviscol()); |
| 2387 | #endif |
| 2388 | |
| 2389 | col = curwin->w_cursor.col; |
| 2390 | oldp = ml_get(lnum); |
| 2391 | linelen = (int)STRLEN(oldp) + 1; |
| 2392 | |
| 2393 | /* The lengths default to the values for when not replacing. */ |
| 2394 | oldlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2395 | newlen = charlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2396 | |
| 2397 | if (State & REPLACE_FLAG) |
| 2398 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2399 | if (State & VREPLACE_FLAG) |
| 2400 | { |
| 2401 | colnr_T new_vcol = 0; /* init for GCC */ |
| 2402 | colnr_T vcol; |
| 2403 | int old_list; |
| 2404 | #ifndef FEAT_MBYTE |
Bram Moolenaar | 402385a | 2019-01-11 14:10:03 +0100 | [diff] [blame] | 2405 | char_u cbuf[2]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2406 | #endif |
| 2407 | |
| 2408 | /* |
| 2409 | * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag. |
| 2410 | * Returns the old value of list, so when finished, |
| 2411 | * curwin->w_p_list should be set back to this. |
| 2412 | */ |
| 2413 | old_list = curwin->w_p_list; |
| 2414 | if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 2415 | curwin->w_p_list = FALSE; |
| 2416 | |
| 2417 | /* |
| 2418 | * In virtual replace mode each character may replace one or more |
| 2419 | * characters (zero if it's a TAB). Count the number of bytes to |
| 2420 | * be deleted to make room for the new character, counting screen |
| 2421 | * cells. May result in adding spaces to fill a gap. |
| 2422 | */ |
| 2423 | getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL); |
| 2424 | #ifndef FEAT_MBYTE |
Bram Moolenaar | 402385a | 2019-01-11 14:10:03 +0100 | [diff] [blame] | 2425 | cbuf[0] = c; |
| 2426 | cbuf[1] = NUL; |
| 2427 | new_vcol = vcol + chartabsize(cbuf, vcol); |
| 2428 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2429 | new_vcol = vcol + chartabsize(buf, vcol); |
Bram Moolenaar | 402385a | 2019-01-11 14:10:03 +0100 | [diff] [blame] | 2430 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2431 | while (oldp[col + oldlen] != NUL && vcol < new_vcol) |
| 2432 | { |
| 2433 | vcol += chartabsize(oldp + col + oldlen, vcol); |
| 2434 | /* Don't need to remove a TAB that takes us to the right |
| 2435 | * position. */ |
| 2436 | if (vcol > new_vcol && oldp[col + oldlen] == TAB) |
| 2437 | break; |
| 2438 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2439 | oldlen += (*mb_ptr2len)(oldp + col + oldlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2440 | #else |
| 2441 | ++oldlen; |
| 2442 | #endif |
| 2443 | /* Deleted a bit too much, insert spaces. */ |
| 2444 | if (vcol > new_vcol) |
| 2445 | newlen += vcol - new_vcol; |
| 2446 | } |
| 2447 | curwin->w_p_list = old_list; |
| 2448 | } |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2449 | else if (oldp[col] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2450 | { |
| 2451 | /* normal replace */ |
| 2452 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2453 | oldlen = (*mb_ptr2len)(oldp + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2454 | #else |
| 2455 | oldlen = 1; |
| 2456 | #endif |
| 2457 | } |
| 2458 | |
| 2459 | |
| 2460 | /* Push the replaced bytes onto the replace stack, so that they can be |
| 2461 | * put back when BS is used. The bytes of a multi-byte character are |
| 2462 | * done the other way around, so that the first byte is popped off |
| 2463 | * first (it tells the byte length of the character). */ |
| 2464 | replace_push(NUL); |
| 2465 | for (i = 0; i < oldlen; ++i) |
| 2466 | { |
| 2467 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 2468 | if (has_mbyte) |
| 2469 | i += replace_push_mb(oldp + col + i) - 1; |
| 2470 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2471 | #endif |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 2472 | replace_push(oldp[col + i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | newp = alloc_check((unsigned)(linelen + newlen - oldlen)); |
| 2477 | if (newp == NULL) |
| 2478 | return; |
| 2479 | |
| 2480 | /* Copy bytes before the cursor. */ |
| 2481 | if (col > 0) |
| 2482 | mch_memmove(newp, oldp, (size_t)col); |
| 2483 | |
| 2484 | /* Copy bytes after the changed character(s). */ |
| 2485 | p = newp + col; |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 2486 | if (linelen > col + oldlen) |
| 2487 | mch_memmove(p + newlen, oldp + col + oldlen, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2488 | (size_t)(linelen - col - oldlen)); |
| 2489 | |
| 2490 | /* Insert or overwrite the new character. */ |
| 2491 | #ifdef FEAT_MBYTE |
| 2492 | mch_memmove(p, buf, charlen); |
| 2493 | i = charlen; |
| 2494 | #else |
| 2495 | *p = c; |
| 2496 | i = 1; |
| 2497 | #endif |
| 2498 | |
| 2499 | /* Fill with spaces when necessary. */ |
| 2500 | while (i < newlen) |
| 2501 | p[i++] = ' '; |
| 2502 | |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2503 | // Replace the line in the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2504 | ml_replace(lnum, newp, FALSE); |
| 2505 | |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2506 | // mark the buffer as changed and prepare for displaying |
| 2507 | inserted_bytes(lnum, col, newlen - oldlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2508 | |
| 2509 | /* |
| 2510 | * If we're in Insert or Replace mode and 'showmatch' is set, then briefly |
| 2511 | * show the match for right parens and braces. |
| 2512 | */ |
| 2513 | if (p_sm && (State & INSERT) |
| 2514 | && msg_silent == 0 |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 2515 | #ifdef FEAT_INS_EXPAND |
| 2516 | && !ins_compl_active() |
| 2517 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2518 | ) |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2519 | { |
| 2520 | #ifdef FEAT_MBYTE |
| 2521 | if (has_mbyte) |
| 2522 | showmatch(mb_ptr2char(buf)); |
| 2523 | else |
| 2524 | #endif |
| 2525 | showmatch(c); |
| 2526 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2527 | |
| 2528 | #ifdef FEAT_RIGHTLEFT |
| 2529 | if (!p_ri || (State & REPLACE_FLAG)) |
| 2530 | #endif |
| 2531 | { |
| 2532 | /* Normal insert: move cursor right */ |
| 2533 | #ifdef FEAT_MBYTE |
| 2534 | curwin->w_cursor.col += charlen; |
| 2535 | #else |
| 2536 | ++curwin->w_cursor.col; |
| 2537 | #endif |
| 2538 | } |
| 2539 | /* |
| 2540 | * TODO: should try to update w_row here, to avoid recomputing it later. |
| 2541 | */ |
| 2542 | } |
| 2543 | |
| 2544 | /* |
| 2545 | * Insert a string at the cursor position. |
| 2546 | * Note: Does NOT handle Replace mode. |
| 2547 | * Caller must have prepared for undo. |
| 2548 | */ |
| 2549 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2550 | ins_str(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2551 | { |
| 2552 | char_u *oldp, *newp; |
| 2553 | int newlen = (int)STRLEN(s); |
| 2554 | int oldlen; |
| 2555 | colnr_T col; |
| 2556 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2557 | |
| 2558 | #ifdef FEAT_VIRTUALEDIT |
| 2559 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 2560 | coladvance_force(getviscol()); |
| 2561 | #endif |
| 2562 | |
| 2563 | col = curwin->w_cursor.col; |
| 2564 | oldp = ml_get(lnum); |
| 2565 | oldlen = (int)STRLEN(oldp); |
| 2566 | |
| 2567 | newp = alloc_check((unsigned)(oldlen + newlen + 1)); |
| 2568 | if (newp == NULL) |
| 2569 | return; |
| 2570 | if (col > 0) |
| 2571 | mch_memmove(newp, oldp, (size_t)col); |
| 2572 | mch_memmove(newp + col, s, (size_t)newlen); |
| 2573 | mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1)); |
| 2574 | ml_replace(lnum, newp, FALSE); |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 2575 | inserted_bytes(lnum, col, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2576 | curwin->w_cursor.col += newlen; |
| 2577 | } |
| 2578 | |
| 2579 | /* |
| 2580 | * Delete one character under the cursor. |
| 2581 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 2582 | * Caller must have prepared for undo. |
| 2583 | * |
| 2584 | * return FAIL for failure, OK otherwise |
| 2585 | */ |
| 2586 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2587 | del_char(int fixpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2588 | { |
| 2589 | #ifdef FEAT_MBYTE |
| 2590 | if (has_mbyte) |
| 2591 | { |
| 2592 | /* Make sure the cursor is at the start of a character. */ |
| 2593 | mb_adjust_cursor(); |
| 2594 | if (*ml_get_cursor() == NUL) |
| 2595 | return FAIL; |
| 2596 | return del_chars(1L, fixpos); |
| 2597 | } |
| 2598 | #endif |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2599 | return del_bytes(1L, fixpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 2603 | /* |
| 2604 | * Like del_bytes(), but delete characters instead of bytes. |
| 2605 | */ |
| 2606 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2607 | del_chars(long count, int fixpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2608 | { |
| 2609 | long bytes = 0; |
| 2610 | long i; |
| 2611 | char_u *p; |
| 2612 | int l; |
| 2613 | |
| 2614 | p = ml_get_cursor(); |
| 2615 | for (i = 0; i < count && *p != NUL; ++i) |
| 2616 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2617 | l = (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2618 | bytes += l; |
| 2619 | p += l; |
| 2620 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2621 | return del_bytes(bytes, fixpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2622 | } |
| 2623 | #endif |
| 2624 | |
| 2625 | /* |
| 2626 | * Delete "count" bytes under the cursor. |
| 2627 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 2628 | * Caller must have prepared for undo. |
| 2629 | * |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2630 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2631 | */ |
| 2632 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2633 | del_bytes( |
| 2634 | long count, |
| 2635 | int fixpos_arg, |
| 2636 | int use_delcombine UNUSED) /* 'delcombine' option applies */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2637 | { |
| 2638 | char_u *oldp, *newp; |
| 2639 | colnr_T oldlen; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2640 | colnr_T newlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2641 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2642 | colnr_T col = curwin->w_cursor.col; |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2643 | int alloc_newp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2644 | long movelen; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2645 | int fixpos = fixpos_arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2646 | |
| 2647 | oldp = ml_get(lnum); |
| 2648 | oldlen = (int)STRLEN(oldp); |
| 2649 | |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2650 | /* 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] | 2651 | if (col >= oldlen) |
| 2652 | return FAIL; |
| 2653 | |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2654 | /* If "count" is zero there is nothing to do. */ |
| 2655 | if (count == 0) |
| 2656 | return OK; |
| 2657 | |
| 2658 | /* If "count" is negative the caller must be doing something wrong. */ |
| 2659 | if (count < 1) |
| 2660 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2661 | siemsg("E950: Invalid count for del_bytes(): %ld", count); |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2662 | return FAIL; |
| 2663 | } |
| 2664 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2665 | #ifdef FEAT_MBYTE |
| 2666 | /* If 'delcombine' is set and deleting (less than) one character, only |
| 2667 | * delete the last combining character. */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2668 | if (p_deco && use_delcombine && enc_utf8 |
| 2669 | && utfc_ptr2len(oldp + col) >= count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2670 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2671 | int cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2672 | int n; |
| 2673 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2674 | (void)utfc_ptr2char(oldp + col, cc); |
| 2675 | if (cc[0] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2676 | { |
| 2677 | /* Find the last composing char, there can be several. */ |
| 2678 | n = col; |
| 2679 | do |
| 2680 | { |
| 2681 | col = n; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2682 | count = utf_ptr2len(oldp + n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2683 | n += count; |
| 2684 | } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n)); |
| 2685 | fixpos = 0; |
| 2686 | } |
| 2687 | } |
| 2688 | #endif |
| 2689 | |
| 2690 | /* |
| 2691 | * When count is too big, reduce it. |
| 2692 | */ |
| 2693 | movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */ |
| 2694 | if (movelen <= 1) |
| 2695 | { |
| 2696 | /* |
| 2697 | * 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] | 2698 | * fixpos is TRUE, we don't want to end up positioned at the NUL, |
| 2699 | * unless "restart_edit" is set or 'virtualedit' contains "onemore". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2700 | */ |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2701 | if (col > 0 && fixpos && restart_edit == 0 |
| 2702 | #ifdef FEAT_VIRTUALEDIT |
| 2703 | && (ve_flags & VE_ONEMORE) == 0 |
| 2704 | #endif |
| 2705 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2706 | { |
| 2707 | --curwin->w_cursor.col; |
| 2708 | #ifdef FEAT_VIRTUALEDIT |
| 2709 | curwin->w_cursor.coladd = 0; |
| 2710 | #endif |
| 2711 | #ifdef FEAT_MBYTE |
| 2712 | if (has_mbyte) |
| 2713 | curwin->w_cursor.col -= |
| 2714 | (*mb_head_off)(oldp, oldp + curwin->w_cursor.col); |
| 2715 | #endif |
| 2716 | } |
| 2717 | count = oldlen - col; |
| 2718 | movelen = 1; |
| 2719 | } |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2720 | newlen = oldlen - count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2721 | |
| 2722 | /* |
| 2723 | * If the old line has been allocated the deletion can be done in the |
| 2724 | * existing line. Otherwise a new line has to be allocated |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2725 | * Can't do this when using Netbeans, because we would need to invoke |
| 2726 | * netbeans_removed(), which deallocates the line. Let ml_replace() take |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 2727 | * care of notifying Netbeans. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2728 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2729 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2730 | if (netbeans_active()) |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2731 | alloc_newp = TRUE; |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2732 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2733 | #endif |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2734 | alloc_newp = !ml_line_alloced(); // check if oldp was allocated |
| 2735 | if (!alloc_newp) |
| 2736 | newp = oldp; // use same allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2737 | else |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2738 | { // need to allocate a new line |
| 2739 | newp = alloc((unsigned)(newlen + 1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2740 | if (newp == NULL) |
| 2741 | return FAIL; |
| 2742 | mch_memmove(newp, oldp, (size_t)col); |
| 2743 | } |
| 2744 | mch_memmove(newp + col, oldp + col + count, (size_t)movelen); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2745 | if (alloc_newp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2746 | ml_replace(lnum, newp, FALSE); |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2747 | #ifdef FEAT_TEXT_PROP |
| 2748 | else |
| 2749 | { |
| 2750 | // Also move any following text properties. |
| 2751 | if (oldlen + 1 < curbuf->b_ml.ml_line_len) |
| 2752 | mch_memmove(newp + newlen + 1, oldp + oldlen + 1, |
| 2753 | (size_t)curbuf->b_ml.ml_line_len - oldlen - 1); |
| 2754 | curbuf->b_ml.ml_line_len -= count; |
| 2755 | } |
| 2756 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2757 | |
Bram Moolenaar | 98aefe7 | 2018-12-13 22:20:09 +0100 | [diff] [blame] | 2758 | // mark the buffer as changed and prepare for displaying |
Bram Moolenaar | 33c8ca9 | 2019-01-02 18:00:27 +0100 | [diff] [blame] | 2759 | inserted_bytes(lnum, curwin->w_cursor.col, -count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2760 | |
| 2761 | return OK; |
| 2762 | } |
| 2763 | |
| 2764 | /* |
| 2765 | * Delete from cursor to end of line. |
| 2766 | * Caller must have prepared for undo. |
| 2767 | * |
| 2768 | * return FAIL for failure, OK otherwise |
| 2769 | */ |
| 2770 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2771 | truncate_line( |
| 2772 | int fixpos) /* if TRUE fix the cursor position when done */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2773 | { |
| 2774 | char_u *newp; |
| 2775 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2776 | colnr_T col = curwin->w_cursor.col; |
| 2777 | |
| 2778 | if (col == 0) |
| 2779 | newp = vim_strsave((char_u *)""); |
| 2780 | else |
| 2781 | newp = vim_strnsave(ml_get(lnum), col); |
| 2782 | |
| 2783 | if (newp == NULL) |
| 2784 | return FAIL; |
| 2785 | |
| 2786 | ml_replace(lnum, newp, FALSE); |
| 2787 | |
| 2788 | /* mark the buffer as changed and prepare for displaying */ |
| 2789 | changed_bytes(lnum, curwin->w_cursor.col); |
| 2790 | |
| 2791 | /* |
| 2792 | * If "fixpos" is TRUE we don't want to end up positioned at the NUL. |
| 2793 | */ |
| 2794 | if (fixpos && curwin->w_cursor.col > 0) |
| 2795 | --curwin->w_cursor.col; |
| 2796 | |
| 2797 | return OK; |
| 2798 | } |
| 2799 | |
| 2800 | /* |
| 2801 | * Delete "nlines" lines at the cursor. |
| 2802 | * Saves the lines for undo first if "undo" is TRUE. |
| 2803 | */ |
| 2804 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2805 | del_lines( |
| 2806 | long nlines, /* number of lines to delete */ |
| 2807 | int undo) /* if TRUE, prepare for undo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2808 | { |
| 2809 | long n; |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2810 | linenr_T first = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2811 | |
| 2812 | if (nlines <= 0) |
| 2813 | return; |
| 2814 | |
| 2815 | /* save the deleted lines for undo */ |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2816 | if (undo && u_savedel(first, nlines) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2817 | return; |
| 2818 | |
| 2819 | for (n = 0; n < nlines; ) |
| 2820 | { |
| 2821 | if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ |
| 2822 | break; |
| 2823 | |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2824 | ml_delete(first, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2825 | ++n; |
| 2826 | |
| 2827 | /* If we delete the last line in the file, stop */ |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2828 | if (first > curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2829 | break; |
| 2830 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2831 | |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2832 | /* Correct the cursor position before calling deleted_lines_mark(), it may |
| 2833 | * trigger a callback to display the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2834 | curwin->w_cursor.col = 0; |
| 2835 | check_cursor_lnum(); |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2836 | |
| 2837 | /* adjust marks, mark the buffer as changed and prepare for displaying */ |
| 2838 | deleted_lines_mark(first, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2842 | gchar_pos(pos_T *pos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2843 | { |
Bram Moolenaar | 8ada6aa | 2017-12-19 21:23:21 +0100 | [diff] [blame] | 2844 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2845 | |
Bram Moolenaar | 8ada6aa | 2017-12-19 21:23:21 +0100 | [diff] [blame] | 2846 | /* When searching columns is sometimes put at the end of a line. */ |
| 2847 | if (pos->col == MAXCOL) |
| 2848 | return NUL; |
| 2849 | ptr = ml_get_pos(pos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2850 | #ifdef FEAT_MBYTE |
| 2851 | if (has_mbyte) |
| 2852 | return (*mb_ptr2char)(ptr); |
| 2853 | #endif |
| 2854 | return (int)*ptr; |
| 2855 | } |
| 2856 | |
| 2857 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2858 | gchar_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2859 | { |
| 2860 | #ifdef FEAT_MBYTE |
| 2861 | if (has_mbyte) |
| 2862 | return (*mb_ptr2char)(ml_get_cursor()); |
| 2863 | #endif |
| 2864 | return (int)*ml_get_cursor(); |
| 2865 | } |
| 2866 | |
| 2867 | /* |
| 2868 | * Write a character at the current cursor position. |
| 2869 | * It is directly written into the block. |
| 2870 | */ |
| 2871 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2872 | pchar_cursor(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2873 | { |
| 2874 | *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) |
| 2875 | + curwin->w_cursor.col) = c; |
| 2876 | } |
| 2877 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2878 | /* |
| 2879 | * When extra == 0: Return TRUE if the cursor is before or on the first |
| 2880 | * non-blank in the line. |
| 2881 | * When extra == 1: Return TRUE if the cursor is before the first non-blank in |
| 2882 | * the line. |
| 2883 | */ |
| 2884 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2885 | inindent(int extra) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2886 | { |
| 2887 | char_u *ptr; |
| 2888 | colnr_T col; |
| 2889 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2890 | for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2891 | ++ptr; |
| 2892 | if (col >= curwin->w_cursor.col + extra) |
| 2893 | return TRUE; |
| 2894 | else |
| 2895 | return FALSE; |
| 2896 | } |
| 2897 | |
| 2898 | /* |
| 2899 | * Skip to next part of an option argument: Skip space and comma. |
| 2900 | */ |
| 2901 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2902 | skip_to_option_part(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2903 | { |
| 2904 | if (*p == ',') |
| 2905 | ++p; |
| 2906 | while (*p == ' ') |
| 2907 | ++p; |
| 2908 | return p; |
| 2909 | } |
| 2910 | |
| 2911 | /* |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2912 | * Call this function when something in the current buffer is changed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2913 | * |
| 2914 | * Most often called through changed_bytes() and changed_lines(), which also |
| 2915 | * mark the area of the display to be redrawn. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2916 | * |
| 2917 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2918 | */ |
| 2919 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2920 | changed(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2921 | { |
| 2922 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 2923 | if (p_imst == IM_ON_THE_SPOT) |
| 2924 | { |
| 2925 | /* The text of the preediting area is inserted, but this doesn't |
| 2926 | * mean a change of the buffer yet. That is delayed until the |
| 2927 | * text is committed. (this means preedit becomes empty) */ |
| 2928 | if (im_is_preediting() && !xim_changed_while_preediting) |
| 2929 | return; |
| 2930 | xim_changed_while_preediting = FALSE; |
| 2931 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2932 | #endif |
| 2933 | |
| 2934 | if (!curbuf->b_changed) |
| 2935 | { |
| 2936 | int save_msg_scroll = msg_scroll; |
| 2937 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2938 | /* Give a warning about changing a read-only file. This may also |
| 2939 | * check-out the file, thus change "curbuf"! */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2940 | change_warning(0); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2941 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2942 | /* Create a swap file if that is wanted. |
| 2943 | * Don't do this for "nofile" and "nowrite" buffer types. */ |
| 2944 | if (curbuf->b_may_swap |
| 2945 | #ifdef FEAT_QUICKFIX |
| 2946 | && !bt_dontwrite(curbuf) |
| 2947 | #endif |
| 2948 | ) |
| 2949 | { |
Bram Moolenaar | b01f357 | 2016-01-15 15:17:04 +0100 | [diff] [blame] | 2950 | int save_need_wait_return = need_wait_return; |
| 2951 | |
| 2952 | need_wait_return = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2953 | ml_open_file(curbuf); |
| 2954 | |
| 2955 | /* The ml_open_file() can cause an ATTENTION message. |
| 2956 | * Wait two seconds, to make sure the user reads this unexpected |
| 2957 | * message. Since we could be anywhere, call wait_return() now, |
| 2958 | * and don't let the emsg() set msg_scroll. */ |
| 2959 | if (need_wait_return && emsg_silent == 0) |
| 2960 | { |
| 2961 | out_flush(); |
| 2962 | ui_delay(2000L, TRUE); |
| 2963 | wait_return(TRUE); |
| 2964 | msg_scroll = save_msg_scroll; |
| 2965 | } |
Bram Moolenaar | b01f357 | 2016-01-15 15:17:04 +0100 | [diff] [blame] | 2966 | else |
| 2967 | need_wait_return = save_need_wait_return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2968 | } |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2969 | changed_int(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2970 | } |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 2971 | ++CHANGEDTICK(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2974 | /* |
| 2975 | * Internal part of changed(), no user interaction. |
| 2976 | */ |
| 2977 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2978 | changed_int(void) |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2979 | { |
| 2980 | curbuf->b_changed = TRUE; |
| 2981 | ml_setflags(curbuf); |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2982 | check_status(curbuf); |
| 2983 | redraw_tabline = TRUE; |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2984 | #ifdef FEAT_TITLE |
| 2985 | need_maketitle = TRUE; /* set window title later */ |
| 2986 | #endif |
| 2987 | } |
| 2988 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 2989 | static void changedOneline(buf_T *buf, linenr_T lnum); |
| 2990 | static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra); |
| 2991 | 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] | 2992 | |
| 2993 | /* |
| 2994 | * Changed bytes within a single line for the current buffer. |
| 2995 | * - marks the windows on this buffer to be redisplayed |
| 2996 | * - marks the buffer changed by calling changed() |
| 2997 | * - invalidates cached values |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2998 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2999 | */ |
| 3000 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3001 | changed_bytes(linenr_T lnum, colnr_T col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3002 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3003 | changedOneline(curbuf, lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3004 | changed_common(lnum, col, lnum + 1, 0L); |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3005 | |
| 3006 | #ifdef FEAT_DIFF |
| 3007 | /* Diff highlighting in other diff windows may need to be updated too. */ |
| 3008 | if (curwin->w_p_diff) |
| 3009 | { |
| 3010 | win_T *wp; |
| 3011 | linenr_T wlnum; |
| 3012 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 3013 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3014 | if (wp->w_p_diff && wp != curwin) |
| 3015 | { |
| 3016 | redraw_win_later(wp, VALID); |
| 3017 | wlnum = diff_lnum_win(lnum, wp); |
| 3018 | if (wlnum > 0) |
| 3019 | changedOneline(wp->w_buffer, wlnum); |
| 3020 | } |
| 3021 | } |
| 3022 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 3025 | /* |
| 3026 | * Like changed_bytes() but also adjust text properties for "added" bytes. |
| 3027 | * When "added" is negative text was deleted. |
| 3028 | */ |
| 3029 | void |
Bram Moolenaar | 402385a | 2019-01-11 14:10:03 +0100 | [diff] [blame] | 3030 | inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED) |
Bram Moolenaar | 44746aa | 2019-01-02 00:02:11 +0100 | [diff] [blame] | 3031 | { |
| 3032 | changed_bytes(lnum, col); |
| 3033 | |
| 3034 | #ifdef FEAT_TEXT_PROP |
| 3035 | if (curbuf->b_has_textprop && added != 0) |
| 3036 | adjust_prop_columns(lnum, col, added); |
| 3037 | #endif |
| 3038 | } |
| 3039 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3040 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3041 | changedOneline(buf_T *buf, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3042 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3043 | if (buf->b_mod_set) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3044 | { |
| 3045 | /* find the maximum area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3046 | if (lnum < buf->b_mod_top) |
| 3047 | buf->b_mod_top = lnum; |
| 3048 | else if (lnum >= buf->b_mod_bot) |
| 3049 | buf->b_mod_bot = lnum + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3050 | } |
| 3051 | else |
| 3052 | { |
| 3053 | /* set the area that must be redisplayed to one line */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3054 | buf->b_mod_set = TRUE; |
| 3055 | buf->b_mod_top = lnum; |
| 3056 | buf->b_mod_bot = lnum + 1; |
| 3057 | buf->b_mod_xlines = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3058 | } |
| 3059 | } |
| 3060 | |
| 3061 | /* |
| 3062 | * Appended "count" lines below line "lnum" in the current buffer. |
| 3063 | * Must be called AFTER the change and after mark_adjust(). |
| 3064 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 3065 | */ |
| 3066 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3067 | appended_lines(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3068 | { |
| 3069 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 3070 | } |
| 3071 | |
| 3072 | /* |
| 3073 | * Like appended_lines(), but adjust marks first. |
| 3074 | */ |
| 3075 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3076 | appended_lines_mark(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3077 | { |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 3078 | /* 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] | 3079 | * be marks there. But it's still needed in diff mode. */ |
| 3080 | if (lnum + count < curbuf->b_ml.ml_line_count |
| 3081 | #ifdef FEAT_DIFF |
| 3082 | || curwin->w_p_diff |
| 3083 | #endif |
| 3084 | ) |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 3085 | mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3086 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 3087 | } |
| 3088 | |
| 3089 | /* |
| 3090 | * Deleted "count" lines at line "lnum" in the current buffer. |
| 3091 | * Must be called AFTER the change and after mark_adjust(). |
| 3092 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 3093 | */ |
| 3094 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3095 | deleted_lines(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3096 | { |
| 3097 | changed_lines(lnum, 0, lnum + count, -count); |
| 3098 | } |
| 3099 | |
| 3100 | /* |
| 3101 | * Like deleted_lines(), but adjust marks first. |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 3102 | * Make sure the cursor is on a valid line before calling, a GUI callback may |
| 3103 | * be triggered to display the cursor. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3104 | */ |
| 3105 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3106 | deleted_lines_mark(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3107 | { |
| 3108 | mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count); |
| 3109 | changed_lines(lnum, 0, lnum + count, -count); |
| 3110 | } |
| 3111 | |
| 3112 | /* |
| 3113 | * Changed lines for the current buffer. |
| 3114 | * Must be called AFTER the change and after mark_adjust(). |
| 3115 | * - mark the buffer changed by calling changed() |
| 3116 | * - mark the windows on this buffer to be redisplayed |
| 3117 | * - invalidate cached values |
| 3118 | * "lnum" is the first line that needs displaying, "lnume" the first line |
| 3119 | * below the changed lines (BEFORE the change). |
| 3120 | * When only inserting lines, "lnum" and "lnume" are equal. |
| 3121 | * Takes care of calling changed() and updating b_mod_*. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3122 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3123 | */ |
| 3124 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3125 | changed_lines( |
| 3126 | linenr_T lnum, /* first line with change */ |
| 3127 | colnr_T col, /* column in first line with change */ |
| 3128 | linenr_T lnume, /* line below last changed line */ |
| 3129 | long xtra) /* number of extra lines (negative when deleting) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3130 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3131 | changed_lines_buf(curbuf, lnum, lnume, xtra); |
| 3132 | |
| 3133 | #ifdef FEAT_DIFF |
Bram Moolenaar | e3521d9 | 2018-09-16 14:10:31 +0200 | [diff] [blame] | 3134 | if (xtra == 0 && curwin->w_p_diff && !diff_internal()) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3135 | { |
| 3136 | /* When the number of lines doesn't change then mark_adjust() isn't |
| 3137 | * called and other diff buffers still need to be marked for |
| 3138 | * displaying. */ |
| 3139 | win_T *wp; |
| 3140 | linenr_T wlnum; |
| 3141 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 3142 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3143 | if (wp->w_p_diff && wp != curwin) |
| 3144 | { |
| 3145 | redraw_win_later(wp, VALID); |
| 3146 | wlnum = diff_lnum_win(lnum, wp); |
| 3147 | if (wlnum > 0) |
| 3148 | changed_lines_buf(wp->w_buffer, wlnum, |
| 3149 | lnume - lnum + wlnum, 0L); |
| 3150 | } |
| 3151 | } |
| 3152 | #endif |
| 3153 | |
| 3154 | changed_common(lnum, col, lnume, xtra); |
| 3155 | } |
| 3156 | |
| 3157 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3158 | changed_lines_buf( |
| 3159 | buf_T *buf, |
| 3160 | linenr_T lnum, /* first line with change */ |
| 3161 | linenr_T lnume, /* line below last changed line */ |
| 3162 | long xtra) /* number of extra lines (negative when deleting) */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3163 | { |
| 3164 | if (buf->b_mod_set) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3165 | { |
| 3166 | /* find the maximum area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3167 | if (lnum < buf->b_mod_top) |
| 3168 | buf->b_mod_top = lnum; |
| 3169 | if (lnum < buf->b_mod_bot) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3170 | { |
| 3171 | /* adjust old bot position for xtra lines */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3172 | buf->b_mod_bot += xtra; |
| 3173 | if (buf->b_mod_bot < lnum) |
| 3174 | buf->b_mod_bot = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3175 | } |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3176 | if (lnume + xtra > buf->b_mod_bot) |
| 3177 | buf->b_mod_bot = lnume + xtra; |
| 3178 | buf->b_mod_xlines += xtra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3179 | } |
| 3180 | else |
| 3181 | { |
| 3182 | /* set the area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3183 | buf->b_mod_set = TRUE; |
| 3184 | buf->b_mod_top = lnum; |
| 3185 | buf->b_mod_bot = lnume + xtra; |
| 3186 | buf->b_mod_xlines = xtra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3187 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3190 | /* |
| 3191 | * Common code for when a change is was made. |
| 3192 | * See changed_lines() for the arguments. |
| 3193 | * Careful: may trigger autocommands that reload the buffer. |
| 3194 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3195 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3196 | changed_common( |
| 3197 | linenr_T lnum, |
| 3198 | colnr_T col, |
| 3199 | linenr_T lnume, |
| 3200 | long xtra) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3201 | { |
| 3202 | win_T *wp; |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3203 | tabpage_T *tp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3204 | int i; |
| 3205 | #ifdef FEAT_JUMPLIST |
| 3206 | int cols; |
| 3207 | pos_T *p; |
| 3208 | int add; |
| 3209 | #endif |
| 3210 | |
| 3211 | /* mark the buffer as modified */ |
| 3212 | changed(); |
| 3213 | |
Bram Moolenaar | e3521d9 | 2018-09-16 14:10:31 +0200 | [diff] [blame] | 3214 | #ifdef FEAT_DIFF |
| 3215 | if (curwin->w_p_diff && diff_internal()) |
| 3216 | curtab->tp_diff_update = TRUE; |
| 3217 | #endif |
| 3218 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3219 | /* set the '. mark */ |
| 3220 | if (!cmdmod.keepjumps) |
| 3221 | { |
| 3222 | curbuf->b_last_change.lnum = lnum; |
| 3223 | curbuf->b_last_change.col = col; |
| 3224 | |
| 3225 | #ifdef FEAT_JUMPLIST |
| 3226 | /* Create a new entry if a new undo-able change was started or we |
| 3227 | * don't have an entry yet. */ |
| 3228 | if (curbuf->b_new_change || curbuf->b_changelistlen == 0) |
| 3229 | { |
| 3230 | if (curbuf->b_changelistlen == 0) |
| 3231 | add = TRUE; |
| 3232 | else |
| 3233 | { |
| 3234 | /* Don't create a new entry when the line number is the same |
| 3235 | * as the last one and the column is not too far away. Avoids |
| 3236 | * creating many entries for typing "xxxxx". */ |
| 3237 | p = &curbuf->b_changelist[curbuf->b_changelistlen - 1]; |
| 3238 | if (p->lnum != lnum) |
| 3239 | add = TRUE; |
| 3240 | else |
| 3241 | { |
| 3242 | cols = comp_textwidth(FALSE); |
| 3243 | if (cols == 0) |
| 3244 | cols = 79; |
| 3245 | add = (p->col + cols < col || col + cols < p->col); |
| 3246 | } |
| 3247 | } |
| 3248 | if (add) |
| 3249 | { |
| 3250 | /* This is the first of a new sequence of undo-able changes |
| 3251 | * and it's at some distance of the last change. Use a new |
| 3252 | * position in the changelist. */ |
| 3253 | curbuf->b_new_change = FALSE; |
| 3254 | |
| 3255 | if (curbuf->b_changelistlen == JUMPLISTSIZE) |
| 3256 | { |
| 3257 | /* changelist is full: remove oldest entry */ |
| 3258 | curbuf->b_changelistlen = JUMPLISTSIZE - 1; |
| 3259 | mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1, |
| 3260 | sizeof(pos_T) * (JUMPLISTSIZE - 1)); |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3261 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3262 | { |
| 3263 | /* Correct position in changelist for other windows on |
| 3264 | * this buffer. */ |
| 3265 | if (wp->w_buffer == curbuf && wp->w_changelistidx > 0) |
| 3266 | --wp->w_changelistidx; |
| 3267 | } |
| 3268 | } |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3269 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3270 | { |
| 3271 | /* For other windows, if the position in the changelist is |
| 3272 | * at the end it stays at the end. */ |
| 3273 | if (wp->w_buffer == curbuf |
| 3274 | && wp->w_changelistidx == curbuf->b_changelistlen) |
| 3275 | ++wp->w_changelistidx; |
| 3276 | } |
| 3277 | ++curbuf->b_changelistlen; |
| 3278 | } |
| 3279 | } |
| 3280 | curbuf->b_changelist[curbuf->b_changelistlen - 1] = |
| 3281 | curbuf->b_last_change; |
| 3282 | /* The current window is always after the last change, so that "g," |
| 3283 | * takes you back to it. */ |
| 3284 | curwin->w_changelistidx = curbuf->b_changelistlen; |
| 3285 | #endif |
| 3286 | } |
| 3287 | |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3288 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3289 | { |
| 3290 | if (wp->w_buffer == curbuf) |
| 3291 | { |
| 3292 | /* Mark this window to be redrawn later. */ |
| 3293 | if (wp->w_redr_type < VALID) |
| 3294 | wp->w_redr_type = VALID; |
| 3295 | |
| 3296 | /* Check if a change in the buffer has invalidated the cached |
| 3297 | * values for the cursor. */ |
| 3298 | #ifdef FEAT_FOLDING |
| 3299 | /* |
| 3300 | * Update the folds for this window. Can't postpone this, because |
| 3301 | * a following operator might work on the whole fold: ">>dd". |
| 3302 | */ |
| 3303 | foldUpdate(wp, lnum, lnume + xtra - 1); |
| 3304 | |
| 3305 | /* The change may cause lines above or below the change to become |
| 3306 | * included in a fold. Set lnum/lnume to the first/last line that |
| 3307 | * might be displayed differently. |
| 3308 | * Set w_cline_folded here as an efficient way to update it when |
| 3309 | * inserting lines just above a closed fold. */ |
| 3310 | i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL); |
| 3311 | if (wp->w_cursor.lnum == lnum) |
| 3312 | wp->w_cline_folded = i; |
| 3313 | i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL); |
| 3314 | if (wp->w_cursor.lnum == lnume) |
| 3315 | wp->w_cline_folded = i; |
| 3316 | |
| 3317 | /* If the changed line is in a range of previously folded lines, |
| 3318 | * compare with the first line in that range. */ |
| 3319 | if (wp->w_cursor.lnum <= lnum) |
| 3320 | { |
| 3321 | i = find_wl_entry(wp, lnum); |
| 3322 | if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum) |
| 3323 | changed_line_abv_curs_win(wp); |
| 3324 | } |
| 3325 | #endif |
| 3326 | |
| 3327 | if (wp->w_cursor.lnum > lnum) |
| 3328 | changed_line_abv_curs_win(wp); |
| 3329 | else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col) |
| 3330 | changed_cline_bef_curs_win(wp); |
| 3331 | if (wp->w_botline >= lnum) |
| 3332 | { |
| 3333 | /* Assume that botline doesn't change (inserted lines make |
| 3334 | * other lines scroll down below botline). */ |
| 3335 | approximate_botline_win(wp); |
| 3336 | } |
| 3337 | |
| 3338 | /* Check if any w_lines[] entries have become invalid. |
| 3339 | * For entries below the change: Correct the lnums for |
| 3340 | * inserted/deleted lines. Makes it possible to stop displaying |
| 3341 | * after the change. */ |
| 3342 | for (i = 0; i < wp->w_lines_valid; ++i) |
| 3343 | if (wp->w_lines[i].wl_valid) |
| 3344 | { |
| 3345 | if (wp->w_lines[i].wl_lnum >= lnum) |
| 3346 | { |
| 3347 | if (wp->w_lines[i].wl_lnum < lnume) |
| 3348 | { |
| 3349 | /* line included in change */ |
| 3350 | wp->w_lines[i].wl_valid = FALSE; |
| 3351 | } |
| 3352 | else if (xtra != 0) |
| 3353 | { |
| 3354 | /* line below change */ |
| 3355 | wp->w_lines[i].wl_lnum += xtra; |
| 3356 | #ifdef FEAT_FOLDING |
| 3357 | wp->w_lines[i].wl_lastlnum += xtra; |
| 3358 | #endif |
| 3359 | } |
| 3360 | } |
| 3361 | #ifdef FEAT_FOLDING |
| 3362 | else if (wp->w_lines[i].wl_lastlnum >= lnum) |
| 3363 | { |
| 3364 | /* change somewhere inside this range of folded lines, |
| 3365 | * may need to be redrawn */ |
| 3366 | wp->w_lines[i].wl_valid = FALSE; |
| 3367 | } |
| 3368 | #endif |
| 3369 | } |
Bram Moolenaar | 3234cc6 | 2009-11-03 17:47:12 +0000 | [diff] [blame] | 3370 | |
| 3371 | #ifdef FEAT_FOLDING |
| 3372 | /* Take care of side effects for setting w_topline when folds have |
| 3373 | * changed. Esp. when the buffer was changed in another window. */ |
| 3374 | if (hasAnyFolding(wp)) |
| 3375 | set_topline(wp, wp->w_topline); |
| 3376 | #endif |
Bram Moolenaar | fd859c9 | 2014-05-13 12:44:24 +0200 | [diff] [blame] | 3377 | /* relative numbering may require updating more */ |
| 3378 | if (wp->w_p_rnu) |
| 3379 | redraw_win_later(wp, SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3380 | } |
| 3381 | } |
| 3382 | |
| 3383 | /* Call update_screen() later, which checks out what needs to be redrawn, |
| 3384 | * since it notices b_mod_set and then uses b_mod_*. */ |
| 3385 | if (must_redraw < VALID) |
| 3386 | must_redraw = VALID; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3387 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3388 | /* when the cursor line is changed always trigger CursorMoved */ |
Bram Moolenaar | e163f1c | 2006-10-17 09:12:21 +0000 | [diff] [blame] | 3389 | if (lnum <= curwin->w_cursor.lnum |
| 3390 | && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum) |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3391 | last_cursormoved.lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | /* |
| 3395 | * unchanged() is called when the changed flag must be reset for buffer 'buf' |
| 3396 | */ |
| 3397 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3398 | unchanged( |
| 3399 | buf_T *buf, |
| 3400 | int ff) /* also reset 'fileformat' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3401 | { |
Bram Moolenaar | 164c60f | 2011-01-22 00:11:50 +0100 | [diff] [blame] | 3402 | if (buf->b_changed || (ff && file_ff_differs(buf, FALSE))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3403 | { |
| 3404 | buf->b_changed = 0; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3405 | ml_setflags(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3406 | if (ff) |
| 3407 | save_file_ff(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3408 | check_status(buf); |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 3409 | redraw_tabline = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3410 | #ifdef FEAT_TITLE |
| 3411 | need_maketitle = TRUE; /* set window title later */ |
| 3412 | #endif |
| 3413 | } |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 3414 | ++CHANGEDTICK(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3415 | #ifdef FEAT_NETBEANS_INTG |
| 3416 | netbeans_unmodified(buf); |
| 3417 | #endif |
| 3418 | } |
| 3419 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3420 | /* |
| 3421 | * check_status: called when the status bars for the buffer 'buf' |
| 3422 | * need to be updated |
| 3423 | */ |
| 3424 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3425 | check_status(buf_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3426 | { |
| 3427 | win_T *wp; |
| 3428 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 3429 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3430 | if (wp->w_buffer == buf && wp->w_status_height) |
| 3431 | { |
| 3432 | wp->w_redr_status = TRUE; |
| 3433 | if (must_redraw < VALID) |
| 3434 | must_redraw = VALID; |
| 3435 | } |
| 3436 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3437 | |
| 3438 | /* |
| 3439 | * If the file is readonly, give a warning message with the first change. |
| 3440 | * Don't do this for autocommands. |
| 3441 | * Don't use emsg(), because it flushes the macro buffer. |
Bram Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 3442 | * 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] | 3443 | * will be TRUE. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3444 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3445 | */ |
| 3446 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3447 | change_warning( |
| 3448 | int col) /* column for message; non-zero when in insert |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3449 | mode and 'showmode' is on */ |
| 3450 | { |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 3451 | static char *w_readonly = N_("W10: Warning: Changing a readonly file"); |
| 3452 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3453 | if (curbuf->b_did_warn == FALSE |
| 3454 | && curbufIsChanged() == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3455 | && !autocmd_busy |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3456 | && curbuf->b_p_ro) |
| 3457 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3458 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3459 | apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3460 | --curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3461 | if (!curbuf->b_p_ro) |
| 3462 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3463 | /* |
| 3464 | * Do what msg() does, but with a column offset if the warning should |
| 3465 | * be after the mode message. |
| 3466 | */ |
| 3467 | msg_start(); |
| 3468 | if (msg_row == Rows - 1) |
| 3469 | msg_col = col; |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3470 | msg_source(HL_ATTR(HLF_W)); |
| 3471 | MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST); |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 3472 | #ifdef FEAT_EVAL |
| 3473 | set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1); |
| 3474 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3475 | msg_clr_eos(); |
| 3476 | (void)msg_end(); |
Bram Moolenaar | e5f2a07 | 2017-02-01 22:31:49 +0100 | [diff] [blame] | 3477 | if (msg_silent == 0 && !silent_mode |
| 3478 | #ifdef FEAT_EVAL |
| 3479 | && time_for_testing != 1 |
| 3480 | #endif |
| 3481 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3482 | { |
| 3483 | out_flush(); |
| 3484 | ui_delay(1000L, TRUE); /* give the user time to think about it */ |
| 3485 | } |
| 3486 | curbuf->b_did_warn = TRUE; |
| 3487 | redraw_cmdline = FALSE; /* don't redraw and erase the message */ |
| 3488 | if (msg_row < Rows - 1) |
| 3489 | showmode(); |
| 3490 | } |
| 3491 | } |
| 3492 | |
| 3493 | /* |
| 3494 | * Ask for a reply from the user, a 'y' or a 'n'. |
| 3495 | * No other characters are accepted, the message is repeated until a valid |
| 3496 | * reply is entered or CTRL-C is hit. |
| 3497 | * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters |
| 3498 | * from any buffers but directly from the user. |
| 3499 | * |
| 3500 | * return the 'y' or 'n' |
| 3501 | */ |
| 3502 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3503 | ask_yesno(char_u *str, int direct) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3504 | { |
| 3505 | int r = ' '; |
| 3506 | int save_State = State; |
| 3507 | |
| 3508 | if (exiting) /* put terminal in raw mode for this question */ |
| 3509 | settmode(TMODE_RAW); |
| 3510 | ++no_wait_return; |
| 3511 | #ifdef USE_ON_FLY_SCROLL |
| 3512 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 3513 | #endif |
| 3514 | State = CONFIRM; /* mouse behaves like with :confirm */ |
| 3515 | #ifdef FEAT_MOUSE |
| 3516 | setmouse(); /* disables mouse for xterm */ |
| 3517 | #endif |
| 3518 | ++no_mapping; |
| 3519 | ++allow_keys; /* no mapping here, but recognize keys */ |
| 3520 | |
| 3521 | while (r != 'y' && r != 'n') |
| 3522 | { |
| 3523 | /* same highlighting as for wait_return */ |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3524 | smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3525 | if (direct) |
| 3526 | r = get_keystroke(); |
| 3527 | else |
Bram Moolenaar | 913626c | 2008-01-03 11:43:42 +0000 | [diff] [blame] | 3528 | r = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3529 | if (r == Ctrl_C || r == ESC) |
| 3530 | r = 'n'; |
| 3531 | msg_putchar(r); /* show what you typed */ |
| 3532 | out_flush(); |
| 3533 | } |
| 3534 | --no_wait_return; |
| 3535 | State = save_State; |
| 3536 | #ifdef FEAT_MOUSE |
| 3537 | setmouse(); |
| 3538 | #endif |
| 3539 | --no_mapping; |
| 3540 | --allow_keys; |
| 3541 | |
| 3542 | return r; |
| 3543 | } |
| 3544 | |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3545 | #if defined(FEAT_MOUSE) || defined(PROTO) |
| 3546 | /* |
| 3547 | * Return TRUE if "c" is a mouse key. |
| 3548 | */ |
| 3549 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3550 | is_mouse_key(int c) |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3551 | { |
| 3552 | return c == K_LEFTMOUSE |
| 3553 | || c == K_LEFTMOUSE_NM |
| 3554 | || c == K_LEFTDRAG |
| 3555 | || c == K_LEFTRELEASE |
| 3556 | || c == K_LEFTRELEASE_NM |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3557 | || c == K_MOUSEMOVE |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3558 | || c == K_MIDDLEMOUSE |
| 3559 | || c == K_MIDDLEDRAG |
| 3560 | || c == K_MIDDLERELEASE |
| 3561 | || c == K_RIGHTMOUSE |
| 3562 | || c == K_RIGHTDRAG |
| 3563 | || c == K_RIGHTRELEASE |
| 3564 | || c == K_MOUSEDOWN |
| 3565 | || c == K_MOUSEUP |
| 3566 | || c == K_MOUSELEFT |
| 3567 | || c == K_MOUSERIGHT |
| 3568 | || c == K_X1MOUSE |
| 3569 | || c == K_X1DRAG |
| 3570 | || c == K_X1RELEASE |
| 3571 | || c == K_X2MOUSE |
| 3572 | || c == K_X2DRAG |
| 3573 | || c == K_X2RELEASE; |
| 3574 | } |
| 3575 | #endif |
| 3576 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3577 | /* |
| 3578 | * Get a key stroke directly from the user. |
| 3579 | * Ignores mouse clicks and scrollbar events, except a click for the left |
| 3580 | * button (used at the more prompt). |
| 3581 | * Doesn't use vgetc(), because it syncs undo and eats mapped characters. |
| 3582 | * Disadvantage: typeahead is ignored. |
| 3583 | * Translates the interrupt character for unix to ESC. |
| 3584 | */ |
| 3585 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3586 | get_keystroke(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3587 | { |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3588 | char_u *buf = NULL; |
| 3589 | int buflen = 150; |
| 3590 | int maxlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3591 | int len = 0; |
| 3592 | int n; |
| 3593 | int save_mapped_ctrl_c = mapped_ctrl_c; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3594 | int waited = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3595 | |
| 3596 | mapped_ctrl_c = FALSE; /* mappings are not used here */ |
| 3597 | for (;;) |
| 3598 | { |
| 3599 | cursor_on(); |
| 3600 | out_flush(); |
| 3601 | |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3602 | /* Leave some room for check_termcode() to insert a key code into (max |
| 3603 | * 5 chars plus NUL). And fix_input_buffer() can triple the number of |
| 3604 | * bytes. */ |
| 3605 | maxlen = (buflen - 6 - len) / 3; |
| 3606 | if (buf == NULL) |
| 3607 | buf = alloc(buflen); |
| 3608 | else if (maxlen < 10) |
| 3609 | { |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 3610 | char_u *t_buf = buf; |
| 3611 | |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 3612 | /* Need some more space. This might happen when receiving a long |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3613 | * escape sequence. */ |
| 3614 | buflen += 100; |
| 3615 | buf = vim_realloc(buf, buflen); |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 3616 | if (buf == NULL) |
| 3617 | vim_free(t_buf); |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3618 | maxlen = (buflen - 6 - len) / 3; |
| 3619 | } |
| 3620 | if (buf == NULL) |
| 3621 | { |
| 3622 | do_outofmem_msg((long_u)buflen); |
| 3623 | return ESC; /* panic! */ |
| 3624 | } |
| 3625 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3626 | /* First time: blocking wait. Second time: wait up to 100ms for a |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3627 | * terminal code to complete. */ |
| 3628 | n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3629 | if (n > 0) |
| 3630 | { |
| 3631 | /* Replace zero and CSI by a special key code. */ |
Bram Moolenaar | 6bff02e | 2016-08-16 22:50:55 +0200 | [diff] [blame] | 3632 | n = fix_input_buffer(buf + len, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3633 | len += n; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3634 | waited = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3635 | } |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3636 | else if (len > 0) |
| 3637 | ++waited; /* keep track of the waiting time */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3638 | |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3639 | /* Incomplete termcode and not timed out yet: get more characters */ |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3640 | if ((n = check_termcode(1, buf, buflen, &len)) < 0 |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3641 | && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3642 | continue; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3643 | |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3644 | if (n == KEYLEN_REMOVED) /* key code removed */ |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3645 | { |
Bram Moolenaar | fd30cd4 | 2011-03-22 13:07:26 +0100 | [diff] [blame] | 3646 | if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0) |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3647 | { |
| 3648 | /* Redrawing was postponed, do it now. */ |
| 3649 | update_screen(0); |
| 3650 | setcursor(); /* put cursor back where it belongs */ |
| 3651 | } |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3652 | continue; |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3653 | } |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3654 | if (n > 0) /* found a termcode: adjust length */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3655 | len = n; |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3656 | if (len == 0) /* nothing typed yet */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3657 | continue; |
| 3658 | |
| 3659 | /* Handle modifier and/or special key code. */ |
| 3660 | n = buf[0]; |
| 3661 | if (n == K_SPECIAL) |
| 3662 | { |
| 3663 | n = TO_SPECIAL(buf[1], buf[2]); |
| 3664 | if (buf[1] == KS_MODIFIER |
| 3665 | || n == K_IGNORE |
Bram Moolenaar | a5be25e | 2013-03-16 21:35:33 +0100 | [diff] [blame] | 3666 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3667 | || (is_mouse_key(n) && n != K_LEFTMOUSE) |
Bram Moolenaar | a5be25e | 2013-03-16 21:35:33 +0100 | [diff] [blame] | 3668 | #endif |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3669 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3670 | || n == K_VER_SCROLLBAR |
| 3671 | || n == K_HOR_SCROLLBAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3672 | #endif |
| 3673 | ) |
| 3674 | { |
| 3675 | if (buf[1] == KS_MODIFIER) |
| 3676 | mod_mask = buf[2]; |
| 3677 | len -= 3; |
| 3678 | if (len > 0) |
| 3679 | mch_memmove(buf, buf + 3, (size_t)len); |
| 3680 | continue; |
| 3681 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 3682 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3683 | } |
| 3684 | #ifdef FEAT_MBYTE |
| 3685 | if (has_mbyte) |
| 3686 | { |
| 3687 | if (MB_BYTE2LEN(n) > len) |
| 3688 | continue; /* more bytes to get */ |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3689 | buf[len >= buflen ? buflen - 1 : len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3690 | n = (*mb_ptr2char)(buf); |
| 3691 | } |
| 3692 | #endif |
| 3693 | #ifdef UNIX |
| 3694 | if (n == intr_char) |
| 3695 | n = ESC; |
| 3696 | #endif |
| 3697 | break; |
| 3698 | } |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3699 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3700 | |
| 3701 | mapped_ctrl_c = save_mapped_ctrl_c; |
| 3702 | return n; |
| 3703 | } |
| 3704 | |
| 3705 | /* |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3706 | * Get a number from the user. |
| 3707 | * When "mouse_used" is not NULL allow using the mouse. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3708 | */ |
| 3709 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3710 | get_number( |
| 3711 | int colon, /* allow colon to abort */ |
| 3712 | int *mouse_used) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3713 | { |
| 3714 | int n = 0; |
| 3715 | int c; |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3716 | int typed = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3717 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3718 | if (mouse_used != NULL) |
| 3719 | *mouse_used = FALSE; |
| 3720 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3721 | /* When not printing messages, the user won't know what to type, return a |
| 3722 | * zero (as if CR was hit). */ |
| 3723 | if (msg_silent != 0) |
| 3724 | return 0; |
| 3725 | |
| 3726 | #ifdef USE_ON_FLY_SCROLL |
| 3727 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 3728 | #endif |
| 3729 | ++no_mapping; |
| 3730 | ++allow_keys; /* no mapping here, but recognize keys */ |
| 3731 | for (;;) |
| 3732 | { |
| 3733 | windgoto(msg_row, msg_col); |
| 3734 | c = safe_vgetc(); |
| 3735 | if (VIM_ISDIGIT(c)) |
| 3736 | { |
| 3737 | n = n * 10 + c - '0'; |
| 3738 | msg_putchar(c); |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3739 | ++typed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3740 | } |
| 3741 | else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H) |
| 3742 | { |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3743 | if (typed > 0) |
| 3744 | { |
| 3745 | MSG_PUTS("\b \b"); |
| 3746 | --typed; |
| 3747 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3748 | n /= 10; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3749 | } |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3750 | #ifdef FEAT_MOUSE |
| 3751 | else if (mouse_used != NULL && c == K_LEFTMOUSE) |
| 3752 | { |
| 3753 | *mouse_used = TRUE; |
| 3754 | n = mouse_row + 1; |
| 3755 | break; |
| 3756 | } |
| 3757 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3758 | else if (n == 0 && c == ':' && colon) |
| 3759 | { |
| 3760 | stuffcharReadbuff(':'); |
| 3761 | if (!exmode_active) |
| 3762 | cmdline_row = msg_row; |
| 3763 | skip_redraw = TRUE; /* skip redraw once */ |
| 3764 | do_redraw = FALSE; |
| 3765 | break; |
| 3766 | } |
| 3767 | else if (c == CAR || c == NL || c == Ctrl_C || c == ESC) |
| 3768 | break; |
| 3769 | } |
| 3770 | --no_mapping; |
| 3771 | --allow_keys; |
| 3772 | return n; |
| 3773 | } |
| 3774 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3775 | /* |
| 3776 | * Ask the user to enter a number. |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3777 | * When "mouse_used" is not NULL allow using the mouse and in that case return |
| 3778 | * the line number. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3779 | */ |
| 3780 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3781 | prompt_for_number(int *mouse_used) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3782 | { |
| 3783 | int i; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3784 | int save_cmdline_row; |
| 3785 | int save_State; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3786 | |
| 3787 | /* When using ":silent" assume that <CR> was entered. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3788 | if (mouse_used != NULL) |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 3789 | MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): ")); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3790 | else |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 3791 | MSG_PUTS(_("Type number and <Enter> (empty cancels): ")); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3792 | |
Bram Moolenaar | 4cbdf15 | 2018-08-26 21:23:07 +0200 | [diff] [blame] | 3793 | // Set the state such that text can be selected/copied/pasted and we still |
| 3794 | // get mouse events. redraw_after_callback() will not redraw if cmdline_row |
| 3795 | // is zero. |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3796 | save_cmdline_row = cmdline_row; |
Bram Moolenaar | 203335e | 2006-09-03 14:35:42 +0000 | [diff] [blame] | 3797 | cmdline_row = 0; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3798 | save_State = State; |
Bram Moolenaar | 4cbdf15 | 2018-08-26 21:23:07 +0200 | [diff] [blame] | 3799 | State = CMDLINE; |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3800 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 4cbdf15 | 2018-08-26 21:23:07 +0200 | [diff] [blame] | 3801 | // May show different mouse shape. |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3802 | setmouse(); |
| 3803 | #endif |
| 3804 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3805 | i = get_number(TRUE, mouse_used); |
| 3806 | if (KeyTyped) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3807 | { |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3808 | /* don't call wait_return() now */ |
| 3809 | /* msg_putchar('\n'); */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3810 | cmdline_row = msg_row - 1; |
| 3811 | need_wait_return = FALSE; |
| 3812 | msg_didany = FALSE; |
Bram Moolenaar | b245016 | 2009-07-22 09:04:20 +0000 | [diff] [blame] | 3813 | msg_didout = FALSE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3814 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3815 | else |
| 3816 | cmdline_row = save_cmdline_row; |
| 3817 | State = save_State; |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3818 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 4cbdf15 | 2018-08-26 21:23:07 +0200 | [diff] [blame] | 3819 | // May need to restore mouse shape. |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3820 | setmouse(); |
| 3821 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3822 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3823 | return i; |
| 3824 | } |
| 3825 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3826 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3827 | msgmore(long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3828 | { |
| 3829 | long pn; |
| 3830 | |
| 3831 | if (global_busy /* no messages now, wait until global is finished */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3832 | || !messaging()) /* 'lazyredraw' set, don't do messages now */ |
| 3833 | return; |
| 3834 | |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 3835 | /* We don't want to overwrite another important message, but do overwrite |
| 3836 | * a previous "more lines" or "fewer lines" message, so that "5dd" and |
| 3837 | * then "put" reports the last action. */ |
| 3838 | if (keep_msg != NULL && !keep_msg_more) |
| 3839 | return; |
| 3840 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3841 | if (n > 0) |
| 3842 | pn = n; |
| 3843 | else |
| 3844 | pn = -n; |
| 3845 | |
| 3846 | if (pn > p_report) |
| 3847 | { |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 3848 | if (n > 0) |
| 3849 | vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
| 3850 | NGETTEXT("%ld more line", "%ld more lines", pn), pn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3851 | else |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 3852 | vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
| 3853 | NGETTEXT("%ld line less", "%ld fewer lines", pn), pn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3854 | if (got_int) |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3855 | vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3856 | if (msg(msg_buf)) |
| 3857 | { |
Bram Moolenaar | 238a564 | 2006-02-21 22:12:05 +0000 | [diff] [blame] | 3858 | set_keep_msg(msg_buf, 0); |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 3859 | keep_msg_more = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3860 | } |
| 3861 | } |
| 3862 | } |
| 3863 | |
| 3864 | /* |
| 3865 | * flush map and typeahead buffers and give a warning for an error |
| 3866 | */ |
| 3867 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3868 | beep_flush(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3869 | { |
| 3870 | if (emsg_silent == 0) |
| 3871 | { |
Bram Moolenaar | 6a2633b | 2018-10-07 23:16:36 +0200 | [diff] [blame] | 3872 | flush_buffers(FLUSH_MINIMAL); |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3873 | vim_beep(BO_ERROR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3874 | } |
| 3875 | } |
| 3876 | |
| 3877 | /* |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3878 | * Give a warning for an error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3879 | */ |
| 3880 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3881 | vim_beep( |
| 3882 | unsigned val) /* one of the BO_ values, e.g., BO_OPER */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3883 | { |
Bram Moolenaar | b48e96f | 2018-02-13 12:26:14 +0100 | [diff] [blame] | 3884 | #ifdef FEAT_EVAL |
| 3885 | called_vim_beep = TRUE; |
| 3886 | #endif |
| 3887 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3888 | if (emsg_silent == 0) |
| 3889 | { |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3890 | if (!((bo_flags & val) || (bo_flags & BO_ALL))) |
| 3891 | { |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3892 | #ifdef ELAPSED_FUNC |
| 3893 | static int did_init = FALSE; |
Bram Moolenaar | 1ac56c2 | 2019-01-17 22:28:22 +0100 | [diff] [blame] | 3894 | static elapsed_T start_tv; |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3895 | |
| 3896 | /* Only beep once per half a second, otherwise a sequence of beeps |
| 3897 | * would freeze Vim. */ |
| 3898 | if (!did_init || ELAPSED_FUNC(start_tv) > 500) |
| 3899 | { |
| 3900 | did_init = TRUE; |
| 3901 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3902 | #endif |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3903 | if (p_vb |
| 3904 | #ifdef FEAT_GUI |
| 3905 | /* While the GUI is starting up the termcap is set for |
| 3906 | * the GUI but the output still goes to a terminal. */ |
| 3907 | && !(gui.in_use && gui.starting) |
| 3908 | #endif |
| 3909 | ) |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3910 | { |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3911 | out_str_cf(T_VB); |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3912 | #ifdef FEAT_VTP |
| 3913 | /* No restore color information, refresh the screen. */ |
| 3914 | if (has_vtp_working() != 0 |
| 3915 | # ifdef FEAT_TERMGUICOLORS |
Bram Moolenaar | c5cd885 | 2018-05-01 15:47:38 +0200 | [diff] [blame] | 3916 | && (p_tgc || (!p_tgc && t_colors >= 256)) |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3917 | # endif |
| 3918 | ) |
| 3919 | { |
| 3920 | redraw_later(CLEAR); |
| 3921 | update_screen(0); |
| 3922 | redrawcmd(); |
| 3923 | } |
| 3924 | #endif |
| 3925 | } |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3926 | else |
| 3927 | out_char(BELL); |
| 3928 | #ifdef ELAPSED_FUNC |
| 3929 | } |
| 3930 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3931 | } |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3932 | |
Bram Moolenaar | b48e96f | 2018-02-13 12:26:14 +0100 | [diff] [blame] | 3933 | /* When 'debug' contains "beep" produce a message. If we are sourcing |
| 3934 | * a script or executing a function give the user a hint where the beep |
| 3935 | * comes from. */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3936 | if (vim_strchr(p_debug, 'e') != NULL) |
| 3937 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3938 | msg_source(HL_ATTR(HLF_W)); |
| 3939 | msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W)); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3940 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3941 | } |
| 3942 | } |
| 3943 | |
| 3944 | /* |
| 3945 | * To get the "real" home directory: |
| 3946 | * - get value of $HOME |
| 3947 | * For Unix: |
| 3948 | * - go to that directory |
| 3949 | * - do mch_dirname() to get the real name of that directory. |
| 3950 | * This also works with mounts and links. |
| 3951 | * Don't do this for MS-DOS, it will change the "current dir" for a drive. |
Bram Moolenaar | 25a494c | 2018-11-16 19:39:50 +0100 | [diff] [blame] | 3952 | * For Windows: |
| 3953 | * This code is duplicated in init_homedir() in dosinst.c. Keep in sync! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3954 | */ |
| 3955 | static char_u *homedir = NULL; |
| 3956 | |
| 3957 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3958 | init_homedir(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3959 | { |
| 3960 | char_u *var; |
| 3961 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3962 | /* In case we are called a second time (when 'encoding' changes). */ |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3963 | VIM_CLEAR(homedir); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3964 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3965 | #ifdef VMS |
| 3966 | var = mch_getenv((char_u *)"SYS$LOGIN"); |
| 3967 | #else |
| 3968 | var = mch_getenv((char_u *)"HOME"); |
| 3969 | #endif |
| 3970 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3971 | #ifdef WIN3264 |
| 3972 | /* |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3973 | * Typically, $HOME is not defined on Windows, unless the user has |
| 3974 | * specifically defined it for Vim's sake. However, on Windows NT |
| 3975 | * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for |
| 3976 | * each user. Try constructing $HOME from these. |
| 3977 | */ |
Bram Moolenaar | b47a259 | 2017-08-30 13:22:28 +0200 | [diff] [blame] | 3978 | if (var == NULL || *var == NUL) |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3979 | { |
| 3980 | char_u *homedrive, *homepath; |
| 3981 | |
| 3982 | homedrive = mch_getenv((char_u *)"HOMEDRIVE"); |
| 3983 | homepath = mch_getenv((char_u *)"HOMEPATH"); |
| 3984 | if (homepath == NULL || *homepath == NUL) |
| 3985 | homepath = (char_u *)"\\"; |
| 3986 | if (homedrive != NULL |
| 3987 | && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL) |
| 3988 | { |
| 3989 | sprintf((char *)NameBuff, "%s%s", homedrive, homepath); |
| 3990 | if (NameBuff[0] != NUL) |
| 3991 | var = NameBuff; |
| 3992 | } |
| 3993 | } |
| 3994 | |
| 3995 | if (var == NULL) |
| 3996 | var = mch_getenv((char_u *)"USERPROFILE"); |
| 3997 | |
| 3998 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3999 | * Weird but true: $HOME may contain an indirect reference to another |
| 4000 | * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set |
| 4001 | * when $HOME is being set. |
| 4002 | */ |
| 4003 | if (var != NULL && *var == '%') |
| 4004 | { |
| 4005 | char_u *p; |
| 4006 | char_u *exp; |
| 4007 | |
| 4008 | p = vim_strchr(var + 1, '%'); |
| 4009 | if (p != NULL) |
| 4010 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4011 | vim_strncpy(NameBuff, var + 1, p - (var + 1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4012 | exp = mch_getenv(NameBuff); |
| 4013 | if (exp != NULL && *exp != NUL |
| 4014 | && STRLEN(exp) + STRLEN(p) < MAXPATHL) |
| 4015 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 4016 | vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4017 | var = NameBuff; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4018 | } |
| 4019 | } |
| 4020 | } |
| 4021 | |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 4022 | if (var != NULL && *var == NUL) /* empty is same as not set */ |
| 4023 | var = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4024 | |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 4025 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4026 | if (enc_utf8 && var != NULL) |
| 4027 | { |
| 4028 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4029 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4030 | |
| 4031 | /* Convert from active codepage to UTF-8. Other conversions are |
| 4032 | * not done, because they would fail for non-ASCII characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4033 | acp_to_enc(var, (int)STRLEN(var), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4034 | if (pp != NULL) |
| 4035 | { |
| 4036 | homedir = pp; |
| 4037 | return; |
| 4038 | } |
| 4039 | } |
| 4040 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4041 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4042 | /* |
| 4043 | * Default home dir is C:/ |
| 4044 | * Best assumption we can make in such a situation. |
| 4045 | */ |
| 4046 | if (var == NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 4047 | var = (char_u *)"C:/"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4048 | #endif |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 4049 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4050 | if (var != NULL) |
| 4051 | { |
| 4052 | #ifdef UNIX |
| 4053 | /* |
| 4054 | * Change to the directory and get the actual path. This resolves |
| 4055 | * links. Don't do it when we can't return. |
| 4056 | */ |
| 4057 | if (mch_dirname(NameBuff, MAXPATHL) == OK |
| 4058 | && mch_chdir((char *)NameBuff) == 0) |
| 4059 | { |
| 4060 | if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK) |
| 4061 | var = IObuff; |
| 4062 | if (mch_chdir((char *)NameBuff) != 0) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4063 | emsg(_(e_prev_dir)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4064 | } |
| 4065 | #endif |
| 4066 | homedir = vim_strsave(var); |
| 4067 | } |
| 4068 | } |
| 4069 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 4070 | #if defined(EXITFREE) || defined(PROTO) |
| 4071 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4072 | free_homedir(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 4073 | { |
| 4074 | vim_free(homedir); |
| 4075 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4076 | |
| 4077 | # ifdef FEAT_CMDL_COMPL |
| 4078 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4079 | free_users(void) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4080 | { |
| 4081 | ga_clear_strings(&ga_users); |
| 4082 | } |
| 4083 | # endif |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 4084 | #endif |
| 4085 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4086 | /* |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4087 | * Call expand_env() and store the result in an allocated string. |
| 4088 | * This is not very memory efficient, this expects the result to be freed |
| 4089 | * again soon. |
| 4090 | */ |
| 4091 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4092 | expand_env_save(char_u *src) |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4093 | { |
| 4094 | return expand_env_save_opt(src, FALSE); |
| 4095 | } |
| 4096 | |
| 4097 | /* |
| 4098 | * Idem, but when "one" is TRUE handle the string as one file name, only |
| 4099 | * expand "~" at the start. |
| 4100 | */ |
| 4101 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4102 | expand_env_save_opt(char_u *src, int one) |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4103 | { |
| 4104 | char_u *p; |
| 4105 | |
| 4106 | p = alloc(MAXPATHL); |
| 4107 | if (p != NULL) |
| 4108 | expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL); |
| 4109 | return p; |
| 4110 | } |
| 4111 | |
| 4112 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4113 | * Expand environment variable with path name. |
| 4114 | * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded. |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4115 | * Skips over "\ ", "\~" and "\$" (not for Win32 though). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4116 | * If anything fails no expansion is done and dst equals src. |
| 4117 | */ |
| 4118 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4119 | expand_env( |
| 4120 | char_u *src, /* input string e.g. "$HOME/vim.hlp" */ |
| 4121 | char_u *dst, /* where to put the result */ |
| 4122 | int dstlen) /* maximum length of the result */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4123 | { |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4124 | expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
| 4127 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4128 | expand_env_esc( |
| 4129 | char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */ |
| 4130 | char_u *dst, /* where to put the result */ |
| 4131 | int dstlen, /* maximum length of the result */ |
| 4132 | int esc, /* escape spaces in expanded variables */ |
| 4133 | int one, /* "srcp" is one file name */ |
| 4134 | char_u *startstr) /* start again after this (can be NULL) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4135 | { |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4136 | char_u *src; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4137 | char_u *tail; |
| 4138 | int c; |
| 4139 | char_u *var; |
| 4140 | int copy_char; |
| 4141 | int mustfree; /* var was allocated, need to free it later */ |
| 4142 | int at_start = TRUE; /* at start of a name */ |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4143 | int startstr_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4144 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4145 | if (startstr != NULL) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4146 | startstr_len = (int)STRLEN(startstr); |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4147 | |
| 4148 | src = skipwhite(srcp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4149 | --dstlen; /* leave one char space for "\," */ |
| 4150 | while (*src && dstlen > 0) |
| 4151 | { |
Bram Moolenaar | be83b73 | 2015-08-25 14:21:19 +0200 | [diff] [blame] | 4152 | #ifdef FEAT_EVAL |
| 4153 | /* Skip over `=expr`. */ |
| 4154 | if (src[0] == '`' && src[1] == '=') |
| 4155 | { |
| 4156 | size_t len; |
| 4157 | |
| 4158 | var = src; |
| 4159 | src += 2; |
| 4160 | (void)skip_expr(&src); |
| 4161 | if (*src == '`') |
| 4162 | ++src; |
| 4163 | len = src - var; |
| 4164 | if (len > (size_t)dstlen) |
| 4165 | len = dstlen; |
| 4166 | vim_strncpy(dst, var, len); |
| 4167 | dst += len; |
Bram Moolenaar | 5df1ed2 | 2015-09-01 16:25:34 +0200 | [diff] [blame] | 4168 | dstlen -= (int)len; |
Bram Moolenaar | be83b73 | 2015-08-25 14:21:19 +0200 | [diff] [blame] | 4169 | continue; |
| 4170 | } |
| 4171 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4172 | copy_char = TRUE; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4173 | if ((*src == '$' |
| 4174 | #ifdef VMS |
| 4175 | && at_start |
| 4176 | #endif |
| 4177 | ) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4178 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4179 | || *src == '%' |
| 4180 | #endif |
| 4181 | || (*src == '~' && at_start)) |
| 4182 | { |
| 4183 | mustfree = FALSE; |
| 4184 | |
| 4185 | /* |
| 4186 | * The variable name is copied into dst temporarily, because it may |
| 4187 | * be a string in read-only memory and a NUL needs to be appended. |
| 4188 | */ |
| 4189 | if (*src != '~') /* environment var */ |
| 4190 | { |
| 4191 | tail = src + 1; |
| 4192 | var = dst; |
| 4193 | c = dstlen - 1; |
| 4194 | |
| 4195 | #ifdef UNIX |
| 4196 | /* Unix has ${var-name} type environment vars */ |
| 4197 | if (*tail == '{' && !vim_isIDc('{')) |
| 4198 | { |
| 4199 | tail++; /* ignore '{' */ |
| 4200 | while (c-- > 0 && *tail && *tail != '}') |
| 4201 | *var++ = *tail++; |
| 4202 | } |
| 4203 | else |
| 4204 | #endif |
| 4205 | { |
| 4206 | while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail)) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4207 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4208 | || (*src == '%' && *tail != '%') |
| 4209 | #endif |
| 4210 | )) |
| 4211 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4212 | *var++ = *tail++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4213 | } |
| 4214 | } |
| 4215 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4216 | #if defined(MSWIN) || defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4217 | # ifdef UNIX |
| 4218 | if (src[1] == '{' && *tail != '}') |
| 4219 | # else |
| 4220 | if (*src == '%' && *tail != '%') |
| 4221 | # endif |
| 4222 | var = NULL; |
| 4223 | else |
| 4224 | { |
| 4225 | # ifdef UNIX |
| 4226 | if (src[1] == '{') |
| 4227 | # else |
| 4228 | if (*src == '%') |
| 4229 | #endif |
| 4230 | ++tail; |
| 4231 | #endif |
| 4232 | *var = NUL; |
| 4233 | var = vim_getenv(dst, &mustfree); |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4234 | #if defined(MSWIN) || defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4235 | } |
| 4236 | #endif |
| 4237 | } |
| 4238 | /* home directory */ |
| 4239 | else if ( src[1] == NUL |
| 4240 | || vim_ispathsep(src[1]) |
| 4241 | || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL) |
| 4242 | { |
| 4243 | var = homedir; |
| 4244 | tail = src + 1; |
| 4245 | } |
| 4246 | else /* user directory */ |
| 4247 | { |
| 4248 | #if defined(UNIX) || (defined(VMS) && defined(USER_HOME)) |
| 4249 | /* |
| 4250 | * Copy ~user to dst[], so we can put a NUL after it. |
| 4251 | */ |
| 4252 | tail = src; |
| 4253 | var = dst; |
| 4254 | c = dstlen - 1; |
| 4255 | while ( c-- > 0 |
| 4256 | && *tail |
| 4257 | && vim_isfilec(*tail) |
| 4258 | && !vim_ispathsep(*tail)) |
| 4259 | *var++ = *tail++; |
| 4260 | *var = NUL; |
| 4261 | # ifdef UNIX |
| 4262 | /* |
| 4263 | * If the system supports getpwnam(), use it. |
| 4264 | * Otherwise, or if getpwnam() fails, the shell is used to |
| 4265 | * expand ~user. This is slower and may fail if the shell |
| 4266 | * does not support ~user (old versions of /bin/sh). |
| 4267 | */ |
| 4268 | # if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H) |
| 4269 | { |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 4270 | /* Note: memory allocated by getpwnam() is never freed. |
| 4271 | * Calling endpwent() apparently doesn't help. */ |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 4272 | struct passwd *pw = (*dst == NUL) |
| 4273 | ? NULL : getpwnam((char *)dst + 1); |
| 4274 | |
| 4275 | var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4276 | } |
| 4277 | if (var == NULL) |
| 4278 | # endif |
| 4279 | { |
| 4280 | expand_T xpc; |
| 4281 | |
| 4282 | ExpandInit(&xpc); |
| 4283 | xpc.xp_context = EXPAND_FILES; |
| 4284 | var = ExpandOne(&xpc, dst, NULL, |
| 4285 | WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4286 | mustfree = TRUE; |
| 4287 | } |
| 4288 | |
| 4289 | # else /* !UNIX, thus VMS */ |
| 4290 | /* |
| 4291 | * USER_HOME is a comma-separated list of |
| 4292 | * directories to search for the user account in. |
| 4293 | */ |
| 4294 | { |
| 4295 | char_u test[MAXPATHL], paths[MAXPATHL]; |
| 4296 | char_u *path, *next_path, *ptr; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4297 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4298 | |
| 4299 | STRCPY(paths, USER_HOME); |
| 4300 | next_path = paths; |
| 4301 | while (*next_path) |
| 4302 | { |
| 4303 | for (path = next_path; *next_path && *next_path != ','; |
| 4304 | next_path++); |
| 4305 | if (*next_path) |
| 4306 | *next_path++ = NUL; |
| 4307 | STRCPY(test, path); |
| 4308 | STRCAT(test, "/"); |
| 4309 | STRCAT(test, dst + 1); |
| 4310 | if (mch_stat(test, &st) == 0) |
| 4311 | { |
| 4312 | var = alloc(STRLEN(test) + 1); |
| 4313 | STRCPY(var, test); |
| 4314 | mustfree = TRUE; |
| 4315 | break; |
| 4316 | } |
| 4317 | } |
| 4318 | } |
| 4319 | # endif /* UNIX */ |
| 4320 | #else |
| 4321 | /* cannot expand user's home directory, so don't try */ |
| 4322 | var = NULL; |
| 4323 | tail = (char_u *)""; /* for gcc */ |
| 4324 | #endif /* UNIX || VMS */ |
| 4325 | } |
| 4326 | |
| 4327 | #ifdef BACKSLASH_IN_FILENAME |
| 4328 | /* If 'shellslash' is set change backslashes to forward slashes. |
| 4329 | * Can't use slash_adjust(), p_ssl may be set temporarily. */ |
| 4330 | if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL) |
| 4331 | { |
| 4332 | char_u *p = vim_strsave(var); |
| 4333 | |
| 4334 | if (p != NULL) |
| 4335 | { |
| 4336 | if (mustfree) |
| 4337 | vim_free(var); |
| 4338 | var = p; |
| 4339 | mustfree = TRUE; |
| 4340 | forward_slash(var); |
| 4341 | } |
| 4342 | } |
| 4343 | #endif |
| 4344 | |
| 4345 | /* If "var" contains white space, escape it with a backslash. |
| 4346 | * Required for ":e ~/tt" when $HOME includes a space. */ |
| 4347 | if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) |
| 4348 | { |
| 4349 | char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); |
| 4350 | |
| 4351 | if (p != NULL) |
| 4352 | { |
| 4353 | if (mustfree) |
| 4354 | vim_free(var); |
| 4355 | var = p; |
| 4356 | mustfree = TRUE; |
| 4357 | } |
| 4358 | } |
| 4359 | |
| 4360 | if (var != NULL && *var != NUL |
| 4361 | && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) |
| 4362 | { |
| 4363 | STRCPY(dst, var); |
| 4364 | dstlen -= (int)STRLEN(var); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4365 | c = (int)STRLEN(var); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4366 | /* if var[] ends in a path separator and tail[] starts |
| 4367 | * with it, skip a character */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4368 | if (*var != NUL && after_pathsep(dst, dst + c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4369 | #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) |
| 4370 | && dst[-1] != ':' |
| 4371 | #endif |
| 4372 | && vim_ispathsep(*tail)) |
| 4373 | ++tail; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4374 | dst += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4375 | src = tail; |
| 4376 | copy_char = FALSE; |
| 4377 | } |
| 4378 | if (mustfree) |
| 4379 | vim_free(var); |
| 4380 | } |
| 4381 | |
| 4382 | if (copy_char) /* copy at least one char */ |
| 4383 | { |
| 4384 | /* |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 4385 | * Recognize the start of a new name, for '~'. |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4386 | * Don't do this when "one" is TRUE, to avoid expanding "~" in |
| 4387 | * ":edit foo ~ foo". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4388 | */ |
| 4389 | at_start = FALSE; |
| 4390 | if (src[0] == '\\' && src[1] != NUL) |
| 4391 | { |
| 4392 | *dst++ = *src++; |
| 4393 | --dstlen; |
| 4394 | } |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4395 | else if ((src[0] == ' ' || src[0] == ',') && !one) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4396 | at_start = TRUE; |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4397 | if (dstlen > 0) |
| 4398 | { |
| 4399 | *dst++ = *src++; |
| 4400 | --dstlen; |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4401 | |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4402 | if (startstr != NULL && src - startstr_len >= srcp |
| 4403 | && STRNCMP(src - startstr_len, startstr, |
| 4404 | startstr_len) == 0) |
| 4405 | at_start = TRUE; |
| 4406 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4407 | } |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4408 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4409 | } |
| 4410 | *dst = NUL; |
| 4411 | } |
| 4412 | |
| 4413 | /* |
| 4414 | * Vim's version of getenv(). |
| 4415 | * Special handling of $HOME, $VIM and $VIMRUNTIME. |
Bram Moolenaar | 2f6b0b8 | 2005-03-08 22:43:10 +0000 | [diff] [blame] | 4416 | * Also does ACP to 'enc' conversion for Win32. |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4417 | * "mustfree" is set to TRUE when returned is allocated, it must be |
| 4418 | * initialized to FALSE by the caller. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4419 | */ |
| 4420 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4421 | vim_getenv(char_u *name, int *mustfree) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4422 | { |
| 4423 | char_u *p; |
| 4424 | char_u *pend; |
| 4425 | int vimruntime; |
| 4426 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4427 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4428 | /* use "C:/" when $HOME is not set */ |
| 4429 | if (STRCMP(name, "HOME") == 0) |
| 4430 | return homedir; |
| 4431 | #endif |
| 4432 | |
| 4433 | p = mch_getenv(name); |
| 4434 | if (p != NULL && *p == NUL) /* empty is the same as not set */ |
| 4435 | p = NULL; |
| 4436 | |
| 4437 | if (p != NULL) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4438 | { |
| 4439 | #if defined(FEAT_MBYTE) && defined(WIN3264) |
| 4440 | if (enc_utf8) |
| 4441 | { |
| 4442 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4443 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4444 | |
| 4445 | /* Convert from active codepage to UTF-8. Other conversions are |
| 4446 | * not done, because they would fail for non-ASCII characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4447 | acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4448 | if (pp != NULL) |
| 4449 | { |
| 4450 | p = pp; |
| 4451 | *mustfree = TRUE; |
| 4452 | } |
| 4453 | } |
| 4454 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4455 | return p; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4456 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4457 | |
| 4458 | vimruntime = (STRCMP(name, "VIMRUNTIME") == 0); |
| 4459 | if (!vimruntime && STRCMP(name, "VIM") != 0) |
| 4460 | return NULL; |
| 4461 | |
| 4462 | /* |
| 4463 | * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM. |
| 4464 | * Don't do this when default_vimruntime_dir is non-empty. |
| 4465 | */ |
| 4466 | if (vimruntime |
| 4467 | #ifdef HAVE_PATHDEF |
| 4468 | && *default_vimruntime_dir == NUL |
| 4469 | #endif |
| 4470 | ) |
| 4471 | { |
| 4472 | p = mch_getenv((char_u *)"VIM"); |
| 4473 | if (p != NULL && *p == NUL) /* empty is the same as not set */ |
| 4474 | p = NULL; |
| 4475 | if (p != NULL) |
| 4476 | { |
| 4477 | p = vim_version_dir(p); |
| 4478 | if (p != NULL) |
| 4479 | *mustfree = TRUE; |
| 4480 | else |
| 4481 | p = mch_getenv((char_u *)"VIM"); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4482 | |
| 4483 | #if defined(FEAT_MBYTE) && defined(WIN3264) |
| 4484 | if (enc_utf8) |
| 4485 | { |
| 4486 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4487 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4488 | |
| 4489 | /* Convert from active codepage to UTF-8. Other conversions |
| 4490 | * are not done, because they would fail for non-ASCII |
| 4491 | * characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4492 | acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4493 | if (pp != NULL) |
| 4494 | { |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4495 | if (*mustfree) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4496 | vim_free(p); |
| 4497 | p = pp; |
| 4498 | *mustfree = TRUE; |
| 4499 | } |
| 4500 | } |
| 4501 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4502 | } |
| 4503 | } |
| 4504 | |
| 4505 | /* |
| 4506 | * When expanding $VIM or $VIMRUNTIME fails, try using: |
| 4507 | * - the directory name from 'helpfile' (unless it contains '$') |
| 4508 | * - the executable name from argv[0] |
| 4509 | */ |
| 4510 | if (p == NULL) |
| 4511 | { |
| 4512 | if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL) |
| 4513 | p = p_hf; |
| 4514 | #ifdef USE_EXE_NAME |
| 4515 | /* |
| 4516 | * Use the name of the executable, obtained from argv[0]. |
| 4517 | */ |
| 4518 | else |
| 4519 | p = exe_name; |
| 4520 | #endif |
| 4521 | if (p != NULL) |
| 4522 | { |
| 4523 | /* remove the file name */ |
| 4524 | pend = gettail(p); |
| 4525 | |
| 4526 | /* remove "doc/" from 'helpfile', if present */ |
| 4527 | if (p == p_hf) |
| 4528 | pend = remove_tail(p, pend, (char_u *)"doc"); |
| 4529 | |
| 4530 | #ifdef USE_EXE_NAME |
| 4531 | # ifdef MACOS_X |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4532 | /* remove "MacOS" from exe_name and add "Resources/vim" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4533 | if (p == exe_name) |
| 4534 | { |
| 4535 | char_u *pend1; |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4536 | char_u *pnew; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4537 | |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4538 | pend1 = remove_tail(p, pend, (char_u *)"MacOS"); |
| 4539 | if (pend1 != pend) |
| 4540 | { |
| 4541 | pnew = alloc((unsigned)(pend1 - p) + 15); |
| 4542 | if (pnew != NULL) |
| 4543 | { |
| 4544 | STRNCPY(pnew, p, (pend1 - p)); |
| 4545 | STRCPY(pnew + (pend1 - p), "Resources/vim"); |
| 4546 | p = pnew; |
| 4547 | pend = p + STRLEN(p); |
| 4548 | } |
| 4549 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4550 | } |
| 4551 | # endif |
| 4552 | /* remove "src/" from exe_name, if present */ |
| 4553 | if (p == exe_name) |
| 4554 | pend = remove_tail(p, pend, (char_u *)"src"); |
| 4555 | #endif |
| 4556 | |
| 4557 | /* for $VIM, remove "runtime/" or "vim54/", if present */ |
| 4558 | if (!vimruntime) |
| 4559 | { |
| 4560 | pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME); |
| 4561 | pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT); |
| 4562 | } |
| 4563 | |
| 4564 | /* remove trailing path separator */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4565 | if (pend > p && after_pathsep(p, pend)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4566 | --pend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4567 | |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4568 | #ifdef MACOS_X |
| 4569 | if (p == exe_name || p == p_hf) |
| 4570 | #endif |
| 4571 | /* check that the result is a directory name */ |
| 4572 | p = vim_strnsave(p, (int)(pend - p)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4573 | |
| 4574 | if (p != NULL && !mch_isdir(p)) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 4575 | VIM_CLEAR(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4576 | else |
| 4577 | { |
| 4578 | #ifdef USE_EXE_NAME |
| 4579 | /* may add "/vim54" or "/runtime" if it exists */ |
| 4580 | if (vimruntime && (pend = vim_version_dir(p)) != NULL) |
| 4581 | { |
| 4582 | vim_free(p); |
| 4583 | p = pend; |
| 4584 | } |
| 4585 | #endif |
| 4586 | *mustfree = TRUE; |
| 4587 | } |
| 4588 | } |
| 4589 | } |
| 4590 | |
| 4591 | #ifdef HAVE_PATHDEF |
| 4592 | /* When there is a pathdef.c file we can use default_vim_dir and |
| 4593 | * default_vimruntime_dir */ |
| 4594 | if (p == NULL) |
| 4595 | { |
| 4596 | /* Only use default_vimruntime_dir when it is not empty */ |
| 4597 | if (vimruntime && *default_vimruntime_dir != NUL) |
| 4598 | { |
| 4599 | p = default_vimruntime_dir; |
| 4600 | *mustfree = FALSE; |
| 4601 | } |
| 4602 | else if (*default_vim_dir != NUL) |
| 4603 | { |
| 4604 | if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) |
| 4605 | *mustfree = TRUE; |
| 4606 | else |
| 4607 | { |
| 4608 | p = default_vim_dir; |
| 4609 | *mustfree = FALSE; |
| 4610 | } |
| 4611 | } |
| 4612 | } |
| 4613 | #endif |
| 4614 | |
| 4615 | /* |
| 4616 | * Set the environment variable, so that the new value can be found fast |
| 4617 | * next time, and others can also use it (e.g. Perl). |
| 4618 | */ |
| 4619 | if (p != NULL) |
| 4620 | { |
| 4621 | if (vimruntime) |
| 4622 | { |
| 4623 | vim_setenv((char_u *)"VIMRUNTIME", p); |
| 4624 | didset_vimruntime = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4625 | } |
| 4626 | else |
| 4627 | { |
| 4628 | vim_setenv((char_u *)"VIM", p); |
| 4629 | didset_vim = TRUE; |
| 4630 | } |
| 4631 | } |
| 4632 | return p; |
| 4633 | } |
| 4634 | |
| 4635 | /* |
| 4636 | * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. |
| 4637 | * Return NULL if not, return its name in allocated memory otherwise. |
| 4638 | */ |
| 4639 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4640 | vim_version_dir(char_u *vimdir) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4641 | { |
| 4642 | char_u *p; |
| 4643 | |
| 4644 | if (vimdir == NULL || *vimdir == NUL) |
| 4645 | return NULL; |
| 4646 | p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); |
| 4647 | if (p != NULL && mch_isdir(p)) |
| 4648 | return p; |
| 4649 | vim_free(p); |
| 4650 | p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); |
| 4651 | if (p != NULL && mch_isdir(p)) |
| 4652 | return p; |
| 4653 | vim_free(p); |
| 4654 | return NULL; |
| 4655 | } |
| 4656 | |
| 4657 | /* |
| 4658 | * If the string between "p" and "pend" ends in "name/", return "pend" minus |
| 4659 | * the length of "name/". Otherwise return "pend". |
| 4660 | */ |
| 4661 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4662 | remove_tail(char_u *p, char_u *pend, char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4663 | { |
| 4664 | int len = (int)STRLEN(name) + 1; |
| 4665 | char_u *newend = pend - len; |
| 4666 | |
| 4667 | if (newend >= p |
| 4668 | && fnamencmp(newend, name, len - 1) == 0 |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4669 | && (newend == p || after_pathsep(p, newend))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4670 | return newend; |
| 4671 | return pend; |
| 4672 | } |
| 4673 | |
Bram Moolenaar | 137374f | 2018-05-13 15:59:50 +0200 | [diff] [blame] | 4674 | void |
| 4675 | vim_unsetenv(char_u *var) |
| 4676 | { |
| 4677 | #ifdef HAVE_UNSETENV |
| 4678 | unsetenv((char *)var); |
| 4679 | #else |
Bram Moolenaar | 1af6a4b | 2018-05-14 22:58:34 +0200 | [diff] [blame] | 4680 | vim_setenv(var, (char_u *)""); |
Bram Moolenaar | 137374f | 2018-05-13 15:59:50 +0200 | [diff] [blame] | 4681 | #endif |
| 4682 | } |
| 4683 | |
| 4684 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4685 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4686 | * Our portable version of setenv. |
| 4687 | */ |
| 4688 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4689 | vim_setenv(char_u *name, char_u *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4690 | { |
| 4691 | #ifdef HAVE_SETENV |
| 4692 | mch_setenv((char *)name, (char *)val, 1); |
| 4693 | #else |
| 4694 | char_u *envbuf; |
| 4695 | |
| 4696 | /* |
| 4697 | * Putenv does not copy the string, it has to remain |
| 4698 | * valid. The allocated memory will never be freed. |
| 4699 | */ |
| 4700 | envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2)); |
| 4701 | if (envbuf != NULL) |
| 4702 | { |
| 4703 | sprintf((char *)envbuf, "%s=%s", name, val); |
| 4704 | putenv((char *)envbuf); |
| 4705 | } |
| 4706 | #endif |
Bram Moolenaar | 011a34d | 2012-02-29 13:49:09 +0100 | [diff] [blame] | 4707 | #ifdef FEAT_GETTEXT |
| 4708 | /* |
| 4709 | * When setting $VIMRUNTIME adjust the directory to find message |
| 4710 | * translations to $VIMRUNTIME/lang. |
| 4711 | */ |
| 4712 | if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) |
| 4713 | { |
| 4714 | char_u *buf = concat_str(val, (char_u *)"/lang"); |
| 4715 | |
| 4716 | if (buf != NULL) |
| 4717 | { |
| 4718 | bindtextdomain(VIMPACKAGE, (char *)buf); |
| 4719 | vim_free(buf); |
| 4720 | } |
| 4721 | } |
| 4722 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 4726 | /* |
| 4727 | * Function given to ExpandGeneric() to obtain an environment variable name. |
| 4728 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4729 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4730 | get_env_name( |
| 4731 | expand_T *xp UNUSED, |
| 4732 | int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4733 | { |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 4734 | # if defined(AMIGA) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4735 | /* |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 4736 | * No environ[] on the Amiga. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4737 | */ |
| 4738 | return NULL; |
| 4739 | # else |
| 4740 | # ifndef __WIN32__ |
| 4741 | /* Borland C++ 5.2 has this in a header file. */ |
| 4742 | extern char **environ; |
| 4743 | # endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 4744 | # define ENVNAMELEN 100 |
| 4745 | static char_u name[ENVNAMELEN]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4746 | char_u *str; |
| 4747 | int n; |
| 4748 | |
| 4749 | str = (char_u *)environ[idx]; |
| 4750 | if (str == NULL) |
| 4751 | return NULL; |
| 4752 | |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 4753 | for (n = 0; n < ENVNAMELEN - 1; ++n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4754 | { |
| 4755 | if (str[n] == '=' || str[n] == NUL) |
| 4756 | break; |
| 4757 | name[n] = str[n]; |
| 4758 | } |
| 4759 | name[n] = NUL; |
| 4760 | return name; |
| 4761 | # endif |
| 4762 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4763 | |
| 4764 | /* |
Bram Moolenaar | 6b0b83f | 2018-09-10 19:03:05 +0200 | [diff] [blame] | 4765 | * Add a user name to the list of users in ga_users. |
| 4766 | * Do nothing if user name is NULL or empty. |
| 4767 | */ |
| 4768 | static void |
| 4769 | add_user(char_u *user, int need_copy) |
| 4770 | { |
| 4771 | char_u *user_copy = (user != NULL && need_copy) |
| 4772 | ? vim_strsave(user) : user; |
| 4773 | |
| 4774 | if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL) |
| 4775 | { |
| 4776 | if (need_copy) |
| 4777 | vim_free(user); |
| 4778 | return; |
| 4779 | } |
| 4780 | ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy; |
| 4781 | } |
| 4782 | |
| 4783 | /* |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4784 | * Find all user names for user completion. |
| 4785 | * Done only once and then cached. |
| 4786 | */ |
| 4787 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4788 | init_users(void) |
Bram Moolenaar | 01b626c | 2013-06-16 22:49:14 +0200 | [diff] [blame] | 4789 | { |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4790 | static int lazy_init_done = FALSE; |
| 4791 | |
| 4792 | if (lazy_init_done) |
| 4793 | return; |
| 4794 | |
| 4795 | lazy_init_done = TRUE; |
| 4796 | ga_init2(&ga_users, sizeof(char_u *), 20); |
| 4797 | |
| 4798 | # if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H) |
| 4799 | { |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4800 | struct passwd* pw; |
| 4801 | |
| 4802 | setpwent(); |
| 4803 | while ((pw = getpwent()) != NULL) |
Bram Moolenaar | 6b0b83f | 2018-09-10 19:03:05 +0200 | [diff] [blame] | 4804 | add_user((char_u *)pw->pw_name, TRUE); |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4805 | endpwent(); |
| 4806 | } |
Bram Moolenaar | 828c3d7 | 2018-06-19 18:58:07 +0200 | [diff] [blame] | 4807 | # elif defined(WIN3264) |
| 4808 | { |
Bram Moolenaar | 828c3d7 | 2018-06-19 18:58:07 +0200 | [diff] [blame] | 4809 | DWORD nusers = 0, ntotal = 0, i; |
| 4810 | PUSER_INFO_0 uinfo; |
| 4811 | |
| 4812 | if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH, |
| 4813 | &nusers, &ntotal, NULL) == NERR_Success) |
| 4814 | { |
| 4815 | for (i = 0; i < nusers; i++) |
Bram Moolenaar | 6b0b83f | 2018-09-10 19:03:05 +0200 | [diff] [blame] | 4816 | add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE); |
Bram Moolenaar | 828c3d7 | 2018-06-19 18:58:07 +0200 | [diff] [blame] | 4817 | |
| 4818 | NetApiBufferFree(uinfo); |
| 4819 | } |
| 4820 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4821 | # endif |
Bram Moolenaar | 6b0b83f | 2018-09-10 19:03:05 +0200 | [diff] [blame] | 4822 | # if defined(HAVE_GETPWNAM) |
| 4823 | { |
| 4824 | char_u *user_env = mch_getenv((char_u *)"USER"); |
| 4825 | |
| 4826 | // The $USER environment variable may be a valid remote user name (NIS, |
| 4827 | // LDAP) not already listed by getpwent(), as getpwent() only lists |
| 4828 | // local user names. If $USER is not already listed, check whether it |
| 4829 | // is a valid remote user name using getpwnam() and if it is, add it to |
| 4830 | // the list of user names. |
| 4831 | |
| 4832 | if (user_env != NULL && *user_env != NUL) |
| 4833 | { |
| 4834 | int i; |
| 4835 | |
| 4836 | for (i = 0; i < ga_users.ga_len; i++) |
| 4837 | { |
| 4838 | char_u *local_user = ((char_u **)ga_users.ga_data)[i]; |
| 4839 | |
| 4840 | if (STRCMP(local_user, user_env) == 0) |
| 4841 | break; |
| 4842 | } |
| 4843 | |
| 4844 | if (i == ga_users.ga_len) |
| 4845 | { |
| 4846 | struct passwd *pw = getpwnam((char *)user_env); |
| 4847 | |
| 4848 | if (pw != NULL) |
| 4849 | add_user((char_u *)pw->pw_name, TRUE); |
| 4850 | } |
| 4851 | } |
| 4852 | } |
| 4853 | # endif |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | /* |
| 4857 | * Function given to ExpandGeneric() to obtain an user names. |
| 4858 | */ |
| 4859 | char_u* |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4860 | get_users(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4861 | { |
| 4862 | init_users(); |
| 4863 | if (idx < ga_users.ga_len) |
| 4864 | return ((char_u **)ga_users.ga_data)[idx]; |
| 4865 | return NULL; |
| 4866 | } |
| 4867 | |
| 4868 | /* |
| 4869 | * Check whether name matches a user name. Return: |
| 4870 | * 0 if name does not match any user name. |
| 4871 | * 1 if name partially matches the beginning of a user name. |
| 4872 | * 2 is name fully matches a user name. |
| 4873 | */ |
Bram Moolenaar | 6c5d104 | 2018-07-07 16:41:13 +0200 | [diff] [blame] | 4874 | int |
| 4875 | match_user(char_u *name) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4876 | { |
| 4877 | int i; |
| 4878 | int n = (int)STRLEN(name); |
| 4879 | int result = 0; |
| 4880 | |
| 4881 | init_users(); |
| 4882 | for (i = 0; i < ga_users.ga_len; i++) |
| 4883 | { |
| 4884 | if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0) |
| 4885 | return 2; /* full match */ |
| 4886 | if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0) |
| 4887 | result = 1; /* partial match */ |
| 4888 | } |
| 4889 | return result; |
| 4890 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4891 | #endif |
| 4892 | |
| 4893 | /* |
| 4894 | * Replace home directory by "~" in each space or comma separated file name in |
| 4895 | * 'src'. |
| 4896 | * If anything fails (except when out of space) dst equals src. |
| 4897 | */ |
| 4898 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4899 | home_replace( |
| 4900 | buf_T *buf, /* when not NULL, check for help files */ |
| 4901 | char_u *src, /* input file name */ |
| 4902 | char_u *dst, /* where to put the result */ |
| 4903 | int dstlen, /* maximum length of the result */ |
| 4904 | int one) /* if TRUE, only replace one file name, include |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4905 | spaces and commas in the file name. */ |
| 4906 | { |
| 4907 | size_t dirlen = 0, envlen = 0; |
| 4908 | size_t len; |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4909 | char_u *homedir_env, *homedir_env_orig; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4910 | char_u *p; |
| 4911 | |
| 4912 | if (src == NULL) |
| 4913 | { |
| 4914 | *dst = NUL; |
| 4915 | return; |
| 4916 | } |
| 4917 | |
| 4918 | /* |
| 4919 | * If the file is a help file, remove the path completely. |
| 4920 | */ |
| 4921 | if (buf != NULL && buf->b_help) |
| 4922 | { |
Bram Moolenaar | 0af2d32 | 2017-08-05 23:00:53 +0200 | [diff] [blame] | 4923 | vim_snprintf((char *)dst, dstlen, "%s", gettail(src)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4924 | return; |
| 4925 | } |
| 4926 | |
| 4927 | /* |
| 4928 | * We check both the value of the $HOME environment variable and the |
| 4929 | * "real" home directory. |
| 4930 | */ |
| 4931 | if (homedir != NULL) |
| 4932 | dirlen = STRLEN(homedir); |
| 4933 | |
| 4934 | #ifdef VMS |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4935 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4936 | #else |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4937 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); |
| 4938 | #endif |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 4939 | #ifdef WIN3264 |
| 4940 | if (homedir_env == NULL) |
| 4941 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE"); |
| 4942 | #endif |
Bram Moolenaar | bef4790 | 2012-07-06 16:49:40 +0200 | [diff] [blame] | 4943 | /* Empty is the same as not set. */ |
| 4944 | if (homedir_env != NULL && *homedir_env == NUL) |
| 4945 | homedir_env = NULL; |
| 4946 | |
Bram Moolenaar | e60c2e5 | 2013-06-05 19:35:38 +0200 | [diff] [blame] | 4947 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) |
Bram Moolenaar | ef0a1d5 | 2018-12-30 11:38:57 +0100 | [diff] [blame] | 4948 | if (homedir_env != NULL && *homedir_env == '~') |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4949 | { |
| 4950 | int usedlen = 0; |
| 4951 | int flen; |
| 4952 | char_u *fbuf = NULL; |
| 4953 | |
| 4954 | flen = (int)STRLEN(homedir_env); |
Bram Moolenaar | 00136dc | 2018-07-25 21:19:13 +0200 | [diff] [blame] | 4955 | (void)modify_fname((char_u *)":p", FALSE, &usedlen, |
Bram Moolenaar | d12f811 | 2012-06-20 17:56:09 +0200 | [diff] [blame] | 4956 | &homedir_env, &fbuf, &flen); |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4957 | flen = (int)STRLEN(homedir_env); |
| 4958 | if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) |
| 4959 | /* Remove the trailing / that is added to a directory. */ |
| 4960 | homedir_env[flen - 1] = NUL; |
| 4961 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4962 | #endif |
| 4963 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4964 | if (homedir_env != NULL) |
| 4965 | envlen = STRLEN(homedir_env); |
| 4966 | |
| 4967 | if (!one) |
| 4968 | src = skipwhite(src); |
| 4969 | while (*src && dstlen > 0) |
| 4970 | { |
| 4971 | /* |
| 4972 | * Here we are at the beginning of a file name. |
| 4973 | * First, check to see if the beginning of the file name matches |
| 4974 | * $HOME or the "real" home directory. Check that there is a '/' |
| 4975 | * after the match (so that if e.g. the file is "/home/pieter/bla", |
| 4976 | * and the home directory is "/home/piet", the file does not end up |
| 4977 | * as "~er/bla" (which would seem to indicate the file "bla" in user |
| 4978 | * er's home directory)). |
| 4979 | */ |
| 4980 | p = homedir; |
| 4981 | len = dirlen; |
| 4982 | for (;;) |
| 4983 | { |
| 4984 | if ( len |
| 4985 | && fnamencmp(src, p, len) == 0 |
| 4986 | && (vim_ispathsep(src[len]) |
| 4987 | || (!one && (src[len] == ',' || src[len] == ' ')) |
| 4988 | || src[len] == NUL)) |
| 4989 | { |
| 4990 | src += len; |
| 4991 | if (--dstlen > 0) |
| 4992 | *dst++ = '~'; |
| 4993 | |
| 4994 | /* |
| 4995 | * If it's just the home directory, add "/". |
| 4996 | */ |
| 4997 | if (!vim_ispathsep(src[0]) && --dstlen > 0) |
| 4998 | *dst++ = '/'; |
| 4999 | break; |
| 5000 | } |
| 5001 | if (p == homedir_env) |
| 5002 | break; |
| 5003 | p = homedir_env; |
| 5004 | len = envlen; |
| 5005 | } |
| 5006 | |
| 5007 | /* if (!one) skip to separator: space or comma */ |
| 5008 | while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) |
| 5009 | *dst++ = *src++; |
| 5010 | /* skip separator */ |
| 5011 | while ((*src == ' ' || *src == ',') && --dstlen > 0) |
| 5012 | *dst++ = *src++; |
| 5013 | } |
| 5014 | /* if (dstlen == 0) out of space, what to do??? */ |
| 5015 | |
| 5016 | *dst = NUL; |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 5017 | |
| 5018 | if (homedir_env != homedir_env_orig) |
| 5019 | vim_free(homedir_env); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
| 5022 | /* |
| 5023 | * Like home_replace, store the replaced string in allocated memory. |
| 5024 | * When something fails, NULL is returned. |
| 5025 | */ |
| 5026 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5027 | home_replace_save( |
| 5028 | buf_T *buf, /* when not NULL, check for help files */ |
| 5029 | char_u *src) /* input file name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5030 | { |
| 5031 | char_u *dst; |
| 5032 | unsigned len; |
| 5033 | |
| 5034 | len = 3; /* space for "~/" and trailing NUL */ |
| 5035 | if (src != NULL) /* just in case */ |
| 5036 | len += (unsigned)STRLEN(src); |
| 5037 | dst = alloc(len); |
| 5038 | if (dst != NULL) |
| 5039 | home_replace(buf, src, dst, len, TRUE); |
| 5040 | return dst; |
| 5041 | } |
| 5042 | |
| 5043 | /* |
| 5044 | * Compare two file names and return: |
| 5045 | * FPC_SAME if they both exist and are the same file. |
| 5046 | * FPC_SAMEX if they both don't exist and have the same file name. |
| 5047 | * FPC_DIFF if they both exist and are different files. |
| 5048 | * FPC_NOTX if they both don't exist. |
| 5049 | * FPC_DIFFX if one of them doesn't exist. |
| 5050 | * For the first name environment variables are expanded |
| 5051 | */ |
| 5052 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5053 | fullpathcmp( |
| 5054 | char_u *s1, |
| 5055 | char_u *s2, |
| 5056 | int checkname) /* when both don't exist, check file names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5057 | { |
| 5058 | #ifdef UNIX |
| 5059 | char_u exp1[MAXPATHL]; |
| 5060 | char_u full1[MAXPATHL]; |
| 5061 | char_u full2[MAXPATHL]; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 5062 | stat_T st1, st2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5063 | int r1, r2; |
| 5064 | |
| 5065 | expand_env(s1, exp1, MAXPATHL); |
| 5066 | r1 = mch_stat((char *)exp1, &st1); |
| 5067 | r2 = mch_stat((char *)s2, &st2); |
| 5068 | if (r1 != 0 && r2 != 0) |
| 5069 | { |
| 5070 | /* if mch_stat() doesn't work, may compare the names */ |
| 5071 | if (checkname) |
| 5072 | { |
| 5073 | if (fnamecmp(exp1, s2) == 0) |
| 5074 | return FPC_SAMEX; |
| 5075 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 5076 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 5077 | if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) |
| 5078 | return FPC_SAMEX; |
| 5079 | } |
| 5080 | return FPC_NOTX; |
| 5081 | } |
| 5082 | if (r1 != 0 || r2 != 0) |
| 5083 | return FPC_DIFFX; |
| 5084 | if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) |
| 5085 | return FPC_SAME; |
| 5086 | return FPC_DIFF; |
| 5087 | #else |
| 5088 | char_u *exp1; /* expanded s1 */ |
| 5089 | char_u *full1; /* full path of s1 */ |
| 5090 | char_u *full2; /* full path of s2 */ |
| 5091 | int retval = FPC_DIFF; |
| 5092 | int r1, r2; |
| 5093 | |
| 5094 | /* allocate one buffer to store three paths (alloc()/free() is slow!) */ |
| 5095 | if ((exp1 = alloc(MAXPATHL * 3)) != NULL) |
| 5096 | { |
| 5097 | full1 = exp1 + MAXPATHL; |
| 5098 | full2 = full1 + MAXPATHL; |
| 5099 | |
| 5100 | expand_env(s1, exp1, MAXPATHL); |
| 5101 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 5102 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 5103 | |
| 5104 | /* If vim_FullName() fails, the file probably doesn't exist. */ |
| 5105 | if (r1 != OK && r2 != OK) |
| 5106 | { |
| 5107 | if (checkname && fnamecmp(exp1, s2) == 0) |
| 5108 | retval = FPC_SAMEX; |
| 5109 | else |
| 5110 | retval = FPC_NOTX; |
| 5111 | } |
| 5112 | else if (r1 != OK || r2 != OK) |
| 5113 | retval = FPC_DIFFX; |
| 5114 | else if (fnamecmp(full1, full2)) |
| 5115 | retval = FPC_DIFF; |
| 5116 | else |
| 5117 | retval = FPC_SAME; |
| 5118 | vim_free(exp1); |
| 5119 | } |
| 5120 | return retval; |
| 5121 | #endif |
| 5122 | } |
| 5123 | |
| 5124 | /* |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5125 | * Get the tail of a path: the file name. |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 5126 | * 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] | 5127 | * Fail safe: never returns NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5128 | */ |
| 5129 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5130 | gettail(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5131 | { |
| 5132 | char_u *p1, *p2; |
| 5133 | |
| 5134 | if (fname == NULL) |
| 5135 | return (char_u *)""; |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5136 | 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] | 5137 | { |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5138 | if (vim_ispathsep_nocolon(*p2)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5139 | p1 = p2 + 1; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5140 | MB_PTR_ADV(p2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5141 | } |
| 5142 | return p1; |
| 5143 | } |
| 5144 | |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 5145 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 5146 | /* |
| 5147 | * Return the end of the directory name, on the first path |
| 5148 | * separator: |
| 5149 | * "/path/file", "/path/dir/", "/path//dir", "/file" |
| 5150 | * ^ ^ ^ ^ |
| 5151 | */ |
| 5152 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5153 | gettail_dir(char_u *fname) |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 5154 | { |
| 5155 | char_u *dir_end = fname; |
| 5156 | char_u *next_dir_end = fname; |
| 5157 | int look_for_sep = TRUE; |
| 5158 | char_u *p; |
| 5159 | |
| 5160 | for (p = fname; *p != NUL; ) |
| 5161 | { |
| 5162 | if (vim_ispathsep(*p)) |
| 5163 | { |
| 5164 | if (look_for_sep) |
| 5165 | { |
| 5166 | next_dir_end = p; |
| 5167 | look_for_sep = FALSE; |
| 5168 | } |
| 5169 | } |
| 5170 | else |
| 5171 | { |
| 5172 | if (!look_for_sep) |
| 5173 | dir_end = next_dir_end; |
| 5174 | look_for_sep = TRUE; |
| 5175 | } |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5176 | MB_PTR_ADV(p); |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 5177 | } |
| 5178 | return dir_end; |
| 5179 | } |
| 5180 | #endif |
| 5181 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5182 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5183 | * Get pointer to tail of "fname", including path separators. Putting a NUL |
| 5184 | * here leaves the directory name. Takes care of "c:/" and "//". |
| 5185 | * Always returns a valid pointer. |
| 5186 | */ |
| 5187 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5188 | gettail_sep(char_u *fname) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5189 | { |
| 5190 | char_u *p; |
| 5191 | char_u *t; |
| 5192 | |
| 5193 | p = get_past_head(fname); /* don't remove the '/' from "c:/file" */ |
| 5194 | t = gettail(fname); |
| 5195 | while (t > p && after_pathsep(fname, t)) |
| 5196 | --t; |
| 5197 | #ifdef VMS |
| 5198 | /* path separator is part of the path */ |
| 5199 | ++t; |
| 5200 | #endif |
| 5201 | return t; |
| 5202 | } |
| 5203 | |
| 5204 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5205 | * get the next path component (just after the next path separator). |
| 5206 | */ |
| 5207 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5208 | getnextcomp(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5209 | { |
| 5210 | while (*fname && !vim_ispathsep(*fname)) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5211 | MB_PTR_ADV(fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5212 | if (*fname) |
| 5213 | ++fname; |
| 5214 | return fname; |
| 5215 | } |
| 5216 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5217 | /* |
| 5218 | * Get a pointer to one character past the head of a path name. |
| 5219 | * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. |
| 5220 | * If there is no head, path is returned. |
| 5221 | */ |
| 5222 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5223 | get_past_head(char_u *path) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5224 | { |
| 5225 | char_u *retval; |
| 5226 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5227 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5228 | /* may skip "c:" */ |
| 5229 | if (isalpha(path[0]) && path[1] == ':') |
| 5230 | retval = path + 2; |
| 5231 | else |
| 5232 | retval = path; |
| 5233 | #else |
| 5234 | # if defined(AMIGA) |
| 5235 | /* may skip "label:" */ |
| 5236 | retval = vim_strchr(path, ':'); |
| 5237 | if (retval == NULL) |
| 5238 | retval = path; |
| 5239 | # else /* Unix */ |
| 5240 | retval = path; |
| 5241 | # endif |
| 5242 | #endif |
| 5243 | |
| 5244 | while (vim_ispathsep(*retval)) |
| 5245 | ++retval; |
| 5246 | |
| 5247 | return retval; |
| 5248 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5249 | |
| 5250 | /* |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5251 | * Return TRUE if 'c' is a path separator. |
| 5252 | * Note that for MS-Windows this includes the colon. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5253 | */ |
| 5254 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5255 | vim_ispathsep(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5256 | { |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5257 | #ifdef UNIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5258 | return (c == '/'); /* UNIX has ':' inside file names */ |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5259 | #else |
| 5260 | # ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5261 | return (c == ':' || c == '/' || c == '\\'); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5262 | # else |
| 5263 | # ifdef VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5264 | /* server"user passwd"::device:[full.path.name]fname.extension;version" */ |
| 5265 | return (c == ':' || c == '[' || c == ']' || c == '/' |
| 5266 | || c == '<' || c == '>' || c == '"' ); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5267 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5268 | return (c == ':' || c == '/'); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5269 | # endif /* VMS */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5270 | # endif |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5271 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5272 | } |
| 5273 | |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5274 | /* |
| 5275 | * Like vim_ispathsep(c), but exclude the colon for MS-Windows. |
| 5276 | */ |
| 5277 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5278 | vim_ispathsep_nocolon(int c) |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5279 | { |
| 5280 | return vim_ispathsep(c) |
| 5281 | #ifdef BACKSLASH_IN_FILENAME |
| 5282 | && c != ':' |
| 5283 | #endif |
| 5284 | ; |
| 5285 | } |
| 5286 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5287 | #if defined(FEAT_SEARCHPATH) || defined(PROTO) |
| 5288 | /* |
| 5289 | * return TRUE if 'c' is a path list separator. |
| 5290 | */ |
| 5291 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5292 | vim_ispathlistsep(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5293 | { |
| 5294 | #ifdef UNIX |
| 5295 | return (c == ':'); |
| 5296 | #else |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 5297 | return (c == ';'); /* might not be right for every system... */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5298 | #endif |
| 5299 | } |
| 5300 | #endif |
| 5301 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5302 | /* |
| 5303 | * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" |
| 5304 | * It's done in-place. |
| 5305 | */ |
| 5306 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5307 | shorten_dir(char_u *str) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5308 | { |
| 5309 | char_u *tail, *s, *d; |
| 5310 | int skip = FALSE; |
| 5311 | |
| 5312 | tail = gettail(str); |
| 5313 | d = str; |
| 5314 | for (s = str; ; ++s) |
| 5315 | { |
| 5316 | if (s >= tail) /* copy the whole tail */ |
| 5317 | { |
| 5318 | *d++ = *s; |
| 5319 | if (*s == NUL) |
| 5320 | break; |
| 5321 | } |
| 5322 | else if (vim_ispathsep(*s)) /* copy '/' and next char */ |
| 5323 | { |
| 5324 | *d++ = *s; |
| 5325 | skip = FALSE; |
| 5326 | } |
| 5327 | else if (!skip) |
| 5328 | { |
| 5329 | *d++ = *s; /* copy next char */ |
| 5330 | if (*s != '~' && *s != '.') /* and leading "~" and "." */ |
| 5331 | skip = TRUE; |
| 5332 | # ifdef FEAT_MBYTE |
| 5333 | if (has_mbyte) |
| 5334 | { |
| 5335 | int l = mb_ptr2len(s); |
| 5336 | |
| 5337 | while (--l > 0) |
Bram Moolenaar | b6baca5 | 2006-08-15 20:24:14 +0000 | [diff] [blame] | 5338 | *d++ = *++s; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5339 | } |
| 5340 | # endif |
| 5341 | } |
| 5342 | } |
| 5343 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5344 | |
Bram Moolenaar | 900b4d7 | 2005-12-12 22:05:50 +0000 | [diff] [blame] | 5345 | /* |
| 5346 | * Return TRUE if the directory of "fname" exists, FALSE otherwise. |
| 5347 | * Also returns TRUE if there is no directory name. |
| 5348 | * "fname" must be writable!. |
| 5349 | */ |
| 5350 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5351 | dir_of_file_exists(char_u *fname) |
Bram Moolenaar | 900b4d7 | 2005-12-12 22:05:50 +0000 | [diff] [blame] | 5352 | { |
| 5353 | char_u *p; |
| 5354 | int c; |
| 5355 | int retval; |
| 5356 | |
| 5357 | p = gettail_sep(fname); |
| 5358 | if (p == fname) |
| 5359 | return TRUE; |
| 5360 | c = *p; |
| 5361 | *p = NUL; |
| 5362 | retval = mch_isdir(fname); |
| 5363 | *p = c; |
| 5364 | return retval; |
| 5365 | } |
| 5366 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5367 | /* |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5368 | * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally |
| 5369 | * and deal with 'fileignorecase'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5370 | */ |
| 5371 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5372 | vim_fnamecmp(char_u *x, char_u *y) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5373 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5374 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5375 | return vim_fnamencmp(x, y, MAXPATHL); |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5376 | #else |
| 5377 | if (p_fic) |
| 5378 | return MB_STRICMP(x, y); |
| 5379 | return STRCMP(x, y); |
| 5380 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5381 | } |
| 5382 | |
| 5383 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5384 | vim_fnamencmp(char_u *x, char_u *y, size_t len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5385 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5386 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5387 | char_u *px = x; |
| 5388 | char_u *py = y; |
| 5389 | int cx = NUL; |
| 5390 | int cy = NUL; |
| 5391 | |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5392 | while (len > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5393 | { |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5394 | cx = PTR2CHAR(px); |
| 5395 | cy = PTR2CHAR(py); |
| 5396 | if (cx == NUL || cy == NUL |
| 5397 | || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy) |
| 5398 | && !(cx == '/' && cy == '\\') |
| 5399 | && !(cx == '\\' && cy == '/'))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5400 | break; |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5401 | len -= MB_PTR2LEN(px); |
| 5402 | px += MB_PTR2LEN(px); |
| 5403 | py += MB_PTR2LEN(py); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5404 | } |
| 5405 | if (len == 0) |
| 5406 | return 0; |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5407 | return (cx - cy); |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5408 | #else |
| 5409 | if (p_fic) |
| 5410 | return MB_STRNICMP(x, y, len); |
| 5411 | return STRNCMP(x, y, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5412 | #endif |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5413 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5414 | |
| 5415 | /* |
| 5416 | * Concatenate file names fname1 and fname2 into allocated memory. |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 5417 | * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5418 | */ |
| 5419 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5420 | concat_fnames(char_u *fname1, char_u *fname2, int sep) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5421 | { |
| 5422 | char_u *dest; |
| 5423 | |
| 5424 | dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3)); |
| 5425 | if (dest != NULL) |
| 5426 | { |
| 5427 | STRCPY(dest, fname1); |
| 5428 | if (sep) |
| 5429 | add_pathsep(dest); |
| 5430 | STRCAT(dest, fname2); |
| 5431 | } |
| 5432 | return dest; |
| 5433 | } |
| 5434 | |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5435 | /* |
| 5436 | * Concatenate two strings and return the result in allocated memory. |
| 5437 | * Returns NULL when out of memory. |
| 5438 | */ |
| 5439 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5440 | concat_str(char_u *str1, char_u *str2) |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5441 | { |
| 5442 | char_u *dest; |
| 5443 | size_t l = STRLEN(str1); |
| 5444 | |
| 5445 | dest = alloc((unsigned)(l + STRLEN(str2) + 1L)); |
| 5446 | if (dest != NULL) |
| 5447 | { |
| 5448 | STRCPY(dest, str1); |
| 5449 | STRCPY(dest + l, str2); |
| 5450 | } |
| 5451 | return dest; |
| 5452 | } |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5453 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5454 | /* |
| 5455 | * Add a path separator to a file name, unless it already ends in a path |
| 5456 | * separator. |
| 5457 | */ |
| 5458 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5459 | add_pathsep(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5460 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5461 | if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5462 | STRCAT(p, PATHSEPSTR); |
| 5463 | } |
| 5464 | |
| 5465 | /* |
| 5466 | * FullName_save - Make an allocated copy of a full file name. |
| 5467 | * Returns NULL when out of memory. |
| 5468 | */ |
| 5469 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5470 | FullName_save( |
| 5471 | char_u *fname, |
| 5472 | int force) /* force expansion, even when it already looks |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 5473 | * like a full path name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5474 | { |
| 5475 | char_u *buf; |
| 5476 | char_u *new_fname = NULL; |
| 5477 | |
| 5478 | if (fname == NULL) |
| 5479 | return NULL; |
| 5480 | |
| 5481 | buf = alloc((unsigned)MAXPATHL); |
| 5482 | if (buf != NULL) |
| 5483 | { |
| 5484 | if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) |
| 5485 | new_fname = vim_strsave(buf); |
| 5486 | else |
| 5487 | new_fname = vim_strsave(fname); |
| 5488 | vim_free(buf); |
| 5489 | } |
| 5490 | return new_fname; |
| 5491 | } |
| 5492 | |
| 5493 | #if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL) |
| 5494 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5495 | static char_u *skip_string(char_u *p); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5496 | static pos_T *find_start_rawstring(int ind_maxcomment); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5497 | |
| 5498 | /* |
| 5499 | * Find the start of a comment, not knowing if we are in a comment right now. |
| 5500 | * Search starts at w_cursor.lnum and goes backwards. |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5501 | * Return NULL when not inside a comment. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5502 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 5503 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5504 | ind_find_start_comment(void) /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 5505 | { |
| 5506 | return find_start_comment(curbuf->b_ind_maxcomment); |
| 5507 | } |
| 5508 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5509 | pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5510 | find_start_comment(int ind_maxcomment) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5511 | { |
| 5512 | pos_T *pos; |
| 5513 | char_u *line; |
| 5514 | char_u *p; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5515 | int cur_maxcomment = ind_maxcomment; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5516 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5517 | for (;;) |
| 5518 | { |
| 5519 | pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment); |
| 5520 | if (pos == NULL) |
| 5521 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5522 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5523 | /* |
| 5524 | * Check if the comment start we found is inside a string. |
| 5525 | * If it is then restrict the search to below this line and try again. |
| 5526 | */ |
| 5527 | line = ml_get(pos->lnum); |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 5528 | for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5529 | p = skip_string(p); |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 5530 | if ((colnr_T)(p - line) <= pos->col) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5531 | break; |
| 5532 | cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; |
| 5533 | if (cur_maxcomment <= 0) |
| 5534 | { |
| 5535 | pos = NULL; |
| 5536 | break; |
| 5537 | } |
| 5538 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5539 | return pos; |
| 5540 | } |
| 5541 | |
| 5542 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5543 | * Find the start of a comment or raw string, not knowing if we are in a |
| 5544 | * comment or raw string right now. |
| 5545 | * Search starts at w_cursor.lnum and goes backwards. |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5546 | * 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] | 5547 | * Return NULL when not inside a comment or raw string. |
| 5548 | * "CORS" -> Comment Or Raw String |
| 5549 | */ |
| 5550 | static pos_T * |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5551 | ind_find_start_CORS(linenr_T *is_raw) /* XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5552 | { |
Bram Moolenaar | 089af18 | 2015-10-07 11:41:49 +0200 | [diff] [blame] | 5553 | static pos_T comment_pos_copy; |
| 5554 | pos_T *comment_pos; |
| 5555 | pos_T *rs_pos; |
| 5556 | |
| 5557 | comment_pos = find_start_comment(curbuf->b_ind_maxcomment); |
| 5558 | if (comment_pos != NULL) |
| 5559 | { |
| 5560 | /* Need to make a copy of the static pos in findmatchlimit(), |
| 5561 | * calling find_start_rawstring() may change it. */ |
| 5562 | comment_pos_copy = *comment_pos; |
| 5563 | comment_pos = &comment_pos_copy; |
| 5564 | } |
| 5565 | rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment); |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5566 | |
| 5567 | /* If comment_pos is before rs_pos the raw string is inside the comment. |
| 5568 | * 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] | 5569 | if (comment_pos == NULL || (rs_pos != NULL |
| 5570 | && LT_POS(*rs_pos, *comment_pos))) |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5571 | { |
| 5572 | if (is_raw != NULL && rs_pos != NULL) |
| 5573 | *is_raw = rs_pos->lnum; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5574 | return rs_pos; |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5575 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5576 | return comment_pos; |
| 5577 | } |
| 5578 | |
| 5579 | /* |
| 5580 | * Find the start of a raw string, not knowing if we are in one right now. |
| 5581 | * Search starts at w_cursor.lnum and goes backwards. |
| 5582 | * Return NULL when not inside a raw string. |
| 5583 | */ |
| 5584 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5585 | find_start_rawstring(int ind_maxcomment) /* XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5586 | { |
| 5587 | pos_T *pos; |
| 5588 | char_u *line; |
| 5589 | char_u *p; |
| 5590 | int cur_maxcomment = ind_maxcomment; |
| 5591 | |
| 5592 | for (;;) |
| 5593 | { |
| 5594 | pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment); |
| 5595 | if (pos == NULL) |
| 5596 | break; |
| 5597 | |
| 5598 | /* |
| 5599 | * Check if the raw string start we found is inside a string. |
| 5600 | * If it is then restrict the search to below this line and try again. |
| 5601 | */ |
| 5602 | line = ml_get(pos->lnum); |
| 5603 | for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) |
| 5604 | p = skip_string(p); |
| 5605 | if ((colnr_T)(p - line) <= pos->col) |
| 5606 | break; |
| 5607 | cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; |
| 5608 | if (cur_maxcomment <= 0) |
| 5609 | { |
| 5610 | pos = NULL; |
| 5611 | break; |
| 5612 | } |
| 5613 | } |
| 5614 | return pos; |
| 5615 | } |
| 5616 | |
| 5617 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5618 | * Skip to the end of a "string" and a 'c' character. |
| 5619 | * If there is no string or character, return argument unmodified. |
| 5620 | */ |
| 5621 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5622 | skip_string(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5623 | { |
| 5624 | int i; |
| 5625 | |
| 5626 | /* |
| 5627 | * We loop, because strings may be concatenated: "date""time". |
| 5628 | */ |
| 5629 | for ( ; ; ++p) |
| 5630 | { |
| 5631 | if (p[0] == '\'') /* 'c' or '\n' or '\000' */ |
| 5632 | { |
| 5633 | if (!p[1]) /* ' at end of line */ |
| 5634 | break; |
| 5635 | i = 2; |
| 5636 | if (p[1] == '\\') /* '\n' or '\000' */ |
| 5637 | { |
| 5638 | ++i; |
| 5639 | while (vim_isdigit(p[i - 1])) /* '\000' */ |
| 5640 | ++i; |
| 5641 | } |
| 5642 | if (p[i] == '\'') /* check for trailing ' */ |
| 5643 | { |
| 5644 | p += i; |
| 5645 | continue; |
| 5646 | } |
| 5647 | } |
| 5648 | else if (p[0] == '"') /* start of string */ |
| 5649 | { |
| 5650 | for (++p; p[0]; ++p) |
| 5651 | { |
| 5652 | if (p[0] == '\\' && p[1] != NUL) |
| 5653 | ++p; |
| 5654 | else if (p[0] == '"') /* end of string */ |
| 5655 | break; |
| 5656 | } |
| 5657 | if (p[0] == '"') |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5658 | continue; /* continue for another string */ |
| 5659 | } |
| 5660 | else if (p[0] == 'R' && p[1] == '"') |
| 5661 | { |
| 5662 | /* Raw string: R"[delim](...)[delim]" */ |
| 5663 | char_u *delim = p + 2; |
| 5664 | char_u *paren = vim_strchr(delim, '('); |
| 5665 | |
| 5666 | if (paren != NULL) |
| 5667 | { |
| 5668 | size_t delim_len = paren - delim; |
| 5669 | |
| 5670 | for (p += 3; *p; ++p) |
| 5671 | if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0 |
| 5672 | && p[delim_len + 1] == '"') |
| 5673 | { |
| 5674 | p += delim_len + 1; |
| 5675 | break; |
| 5676 | } |
| 5677 | if (p[0] == '"') |
| 5678 | continue; /* continue for another string */ |
| 5679 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5680 | } |
| 5681 | break; /* no string found */ |
| 5682 | } |
| 5683 | if (!*p) |
| 5684 | --p; /* backup from NUL */ |
| 5685 | return p; |
| 5686 | } |
| 5687 | #endif /* FEAT_CINDENT || FEAT_SYN_HL */ |
| 5688 | |
| 5689 | #if defined(FEAT_CINDENT) || defined(PROTO) |
| 5690 | |
| 5691 | /* |
| 5692 | * Do C or expression indenting on the current line. |
| 5693 | */ |
| 5694 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5695 | do_c_expr_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5696 | { |
| 5697 | # ifdef FEAT_EVAL |
| 5698 | if (*curbuf->b_p_inde != NUL) |
| 5699 | fixthisline(get_expr_indent); |
| 5700 | else |
| 5701 | # endif |
| 5702 | fixthisline(get_c_indent); |
| 5703 | } |
| 5704 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 5705 | /* Find result cache for cpp_baseclass */ |
| 5706 | typedef struct { |
| 5707 | int found; |
| 5708 | lpos_T lpos; |
| 5709 | } cpp_baseclass_cache_T; |
| 5710 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5711 | /* |
| 5712 | * Functions for C-indenting. |
| 5713 | * Most of this originally comes from Eric Fischer. |
| 5714 | */ |
| 5715 | /* |
| 5716 | * Below "XXX" means that this function may unlock the current line. |
| 5717 | */ |
| 5718 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5719 | static int cin_isdefault(char_u *); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5720 | static int cin_ispreproc(char_u *); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5721 | static int cin_iscomment(char_u *); |
| 5722 | static int cin_islinecomment(char_u *); |
| 5723 | static int cin_isterminated(char_u *, int, int); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5724 | static int cin_iselse(char_u *); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5725 | static int cin_ends_in(char_u *, char_u *, char_u *); |
| 5726 | static int cin_starts_with(char_u *s, char *word); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5727 | static pos_T *find_match_paren(int); |
| 5728 | static pos_T *find_match_char(int c, int ind_maxparen); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5729 | static int find_last_paren(char_u *l, int start, int end); |
| 5730 | static int find_match(int lookfor, linenr_T ourscope); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5731 | |
| 5732 | /* |
| 5733 | * Skip over white space and C comments within the line. |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5734 | * Also skip over Perl/shell comments if desired. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5735 | */ |
| 5736 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5737 | cin_skipcomment(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5738 | { |
| 5739 | while (*s) |
| 5740 | { |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5741 | char_u *prev_s = s; |
| 5742 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5743 | s = skipwhite(s); |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5744 | |
| 5745 | /* Perl/shell # comment comment continues until eol. Require a space |
| 5746 | * before # to avoid recognizing $#array. */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 5747 | if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#') |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5748 | { |
| 5749 | s += STRLEN(s); |
| 5750 | break; |
| 5751 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5752 | if (*s != '/') |
| 5753 | break; |
| 5754 | ++s; |
| 5755 | if (*s == '/') /* slash-slash comment continues till eol */ |
| 5756 | { |
| 5757 | s += STRLEN(s); |
| 5758 | break; |
| 5759 | } |
| 5760 | if (*s != '*') |
| 5761 | break; |
| 5762 | for (++s; *s; ++s) /* skip slash-star comment */ |
| 5763 | if (s[0] == '*' && s[1] == '/') |
| 5764 | { |
| 5765 | s += 2; |
| 5766 | break; |
| 5767 | } |
| 5768 | } |
| 5769 | return s; |
| 5770 | } |
| 5771 | |
| 5772 | /* |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 5773 | * 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] | 5774 | * not considered code. |
| 5775 | */ |
| 5776 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5777 | cin_nocode(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5778 | { |
| 5779 | return *cin_skipcomment(s) == NUL; |
| 5780 | } |
| 5781 | |
| 5782 | /* |
| 5783 | * Check previous lines for a "//" line comment, skipping over blank lines. |
| 5784 | */ |
| 5785 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5786 | find_line_comment(void) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5787 | { |
| 5788 | static pos_T pos; |
| 5789 | char_u *line; |
| 5790 | char_u *p; |
| 5791 | |
| 5792 | pos = curwin->w_cursor; |
| 5793 | while (--pos.lnum > 0) |
| 5794 | { |
| 5795 | line = ml_get(pos.lnum); |
| 5796 | p = skipwhite(line); |
| 5797 | if (cin_islinecomment(p)) |
| 5798 | { |
| 5799 | pos.col = (int)(p - line); |
| 5800 | return &pos; |
| 5801 | } |
| 5802 | if (*p != NUL) |
| 5803 | break; |
| 5804 | } |
| 5805 | return NULL; |
| 5806 | } |
| 5807 | |
| 5808 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5809 | * Return TRUE if "text" starts with "key:". |
| 5810 | */ |
| 5811 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5812 | cin_has_js_key(char_u *text) |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5813 | { |
| 5814 | char_u *s = skipwhite(text); |
Bram Moolenaar | ece29e8 | 2014-08-06 12:49:18 +0200 | [diff] [blame] | 5815 | int quote = -1; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5816 | |
| 5817 | if (*s == '\'' || *s == '"') |
| 5818 | { |
| 5819 | /* can be 'key': or "key": */ |
| 5820 | quote = *s; |
| 5821 | ++s; |
| 5822 | } |
| 5823 | if (!vim_isIDc(*s)) /* need at least one ID character */ |
| 5824 | return FALSE; |
| 5825 | |
| 5826 | while (vim_isIDc(*s)) |
| 5827 | ++s; |
| 5828 | if (*s == quote) |
| 5829 | ++s; |
| 5830 | |
| 5831 | s = cin_skipcomment(s); |
| 5832 | |
| 5833 | /* "::" is not a label, it's C++ */ |
| 5834 | return (*s == ':' && s[1] != ':'); |
| 5835 | } |
| 5836 | |
| 5837 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5838 | * Check if string matches "label:"; move to character after ':' if true. |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5839 | * "*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] | 5840 | */ |
| 5841 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5842 | cin_islabel_skip(char_u **s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5843 | { |
| 5844 | if (!vim_isIDc(**s)) /* need at least one ID character */ |
| 5845 | return FALSE; |
| 5846 | |
| 5847 | while (vim_isIDc(**s)) |
| 5848 | (*s)++; |
| 5849 | |
| 5850 | *s = cin_skipcomment(*s); |
| 5851 | |
| 5852 | /* "::" is not a label, it's C++ */ |
| 5853 | return (**s == ':' && *++*s != ':'); |
| 5854 | } |
| 5855 | |
| 5856 | /* |
| 5857 | * Recognize a label: "label:". |
| 5858 | * Note: curwin->w_cursor must be where we are looking for the label. |
| 5859 | */ |
| 5860 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5861 | cin_islabel(void) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5862 | { |
| 5863 | char_u *s; |
| 5864 | |
| 5865 | s = cin_skipcomment(ml_get_curline()); |
| 5866 | |
| 5867 | /* |
| 5868 | * Exclude "default" from labels, since it should be indented |
| 5869 | * like a switch label. Same for C++ scope declarations. |
| 5870 | */ |
| 5871 | if (cin_isdefault(s)) |
| 5872 | return FALSE; |
| 5873 | if (cin_isscopedecl(s)) |
| 5874 | return FALSE; |
| 5875 | |
| 5876 | if (cin_islabel_skip(&s)) |
| 5877 | { |
| 5878 | /* |
| 5879 | * Only accept a label if the previous line is terminated or is a case |
| 5880 | * label. |
| 5881 | */ |
| 5882 | pos_T cursor_save; |
| 5883 | pos_T *trypos; |
| 5884 | char_u *line; |
| 5885 | |
| 5886 | cursor_save = curwin->w_cursor; |
| 5887 | while (curwin->w_cursor.lnum > 1) |
| 5888 | { |
| 5889 | --curwin->w_cursor.lnum; |
| 5890 | |
| 5891 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5892 | * If we're in a comment or raw string now, skip to the start of |
| 5893 | * it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5894 | */ |
| 5895 | curwin->w_cursor.col = 0; |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5896 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5897 | curwin->w_cursor = *trypos; |
| 5898 | |
| 5899 | line = ml_get_curline(); |
| 5900 | if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */ |
| 5901 | continue; |
| 5902 | if (*(line = cin_skipcomment(line)) == NUL) |
| 5903 | continue; |
| 5904 | |
| 5905 | curwin->w_cursor = cursor_save; |
| 5906 | if (cin_isterminated(line, TRUE, FALSE) |
| 5907 | || cin_isscopedecl(line) |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5908 | || cin_iscase(line, TRUE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5909 | || (cin_islabel_skip(&line) && cin_nocode(line))) |
| 5910 | return TRUE; |
| 5911 | return FALSE; |
| 5912 | } |
| 5913 | curwin->w_cursor = cursor_save; |
| 5914 | return TRUE; /* label at start of file??? */ |
| 5915 | } |
| 5916 | return FALSE; |
| 5917 | } |
| 5918 | |
| 5919 | /* |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5920 | * Recognize structure initialization and enumerations: |
| 5921 | * "[typedef] [static|public|protected|private] enum" |
| 5922 | * "[typedef] [static|public|protected|private] = {" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5923 | */ |
| 5924 | static int |
| 5925 | cin_isinit(void) |
| 5926 | { |
| 5927 | char_u *s; |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5928 | static char *skip[] = {"static", "public", "protected", "private"}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5929 | |
| 5930 | s = cin_skipcomment(ml_get_curline()); |
| 5931 | |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5932 | if (cin_starts_with(s, "typedef")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5933 | s = cin_skipcomment(s + 7); |
| 5934 | |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5935 | for (;;) |
| 5936 | { |
| 5937 | int i, l; |
Bram Moolenaar | a528565 | 2011-12-14 20:05:21 +0100 | [diff] [blame] | 5938 | |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5939 | for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i) |
| 5940 | { |
Bram Moolenaar | b3cb982 | 2013-03-13 17:01:52 +0100 | [diff] [blame] | 5941 | l = (int)strlen(skip[i]); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5942 | if (cin_starts_with(s, skip[i])) |
| 5943 | { |
| 5944 | s = cin_skipcomment(s + l); |
| 5945 | l = 0; |
| 5946 | break; |
| 5947 | } |
| 5948 | } |
| 5949 | if (l != 0) |
| 5950 | break; |
| 5951 | } |
| 5952 | |
| 5953 | if (cin_starts_with(s, "enum")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5954 | return TRUE; |
| 5955 | |
| 5956 | if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) |
| 5957 | return TRUE; |
| 5958 | |
| 5959 | return FALSE; |
| 5960 | } |
| 5961 | |
| 5962 | /* |
| 5963 | * Recognize a switch label: "case .*:" or "default:". |
| 5964 | */ |
| 5965 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5966 | cin_iscase( |
| 5967 | char_u *s, |
| 5968 | int strict) /* Allow relaxed check of case statement for JS */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5969 | { |
| 5970 | s = cin_skipcomment(s); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5971 | if (cin_starts_with(s, "case")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5972 | { |
| 5973 | for (s += 4; *s; ++s) |
| 5974 | { |
| 5975 | s = cin_skipcomment(s); |
| 5976 | if (*s == ':') |
| 5977 | { |
| 5978 | if (s[1] == ':') /* skip over "::" for C++ */ |
| 5979 | ++s; |
| 5980 | else |
| 5981 | return TRUE; |
| 5982 | } |
| 5983 | if (*s == '\'' && s[1] && s[2] == '\'') |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5984 | s += 2; /* skip over ':' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5985 | else if (*s == '/' && (s[1] == '*' || s[1] == '/')) |
| 5986 | return FALSE; /* stop at comment */ |
| 5987 | else if (*s == '"') |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5988 | { |
| 5989 | /* JS etc. */ |
| 5990 | if (strict) |
| 5991 | return FALSE; /* stop at string */ |
| 5992 | else |
| 5993 | return TRUE; |
| 5994 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5995 | } |
| 5996 | return FALSE; |
| 5997 | } |
| 5998 | |
| 5999 | if (cin_isdefault(s)) |
| 6000 | return TRUE; |
| 6001 | return FALSE; |
| 6002 | } |
| 6003 | |
| 6004 | /* |
| 6005 | * Recognize a "default" switch label. |
| 6006 | */ |
| 6007 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6008 | cin_isdefault(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6009 | { |
| 6010 | return (STRNCMP(s, "default", 7) == 0 |
| 6011 | && *(s = cin_skipcomment(s + 7)) == ':' |
| 6012 | && s[1] != ':'); |
| 6013 | } |
| 6014 | |
| 6015 | /* |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 6016 | * Recognize a "public/private/protected" scope declaration label. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6017 | */ |
| 6018 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6019 | cin_isscopedecl(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6020 | { |
| 6021 | int i; |
| 6022 | |
| 6023 | s = cin_skipcomment(s); |
| 6024 | if (STRNCMP(s, "public", 6) == 0) |
| 6025 | i = 6; |
| 6026 | else if (STRNCMP(s, "protected", 9) == 0) |
| 6027 | i = 9; |
| 6028 | else if (STRNCMP(s, "private", 7) == 0) |
| 6029 | i = 7; |
| 6030 | else |
| 6031 | return FALSE; |
| 6032 | return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':'); |
| 6033 | } |
| 6034 | |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 6035 | /* Maximum number of lines to search back for a "namespace" line. */ |
| 6036 | #define FIND_NAMESPACE_LIM 20 |
| 6037 | |
| 6038 | /* |
| 6039 | * Recognize a "namespace" scope declaration. |
| 6040 | */ |
| 6041 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6042 | cin_is_cpp_namespace(char_u *s) |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 6043 | { |
| 6044 | char_u *p; |
| 6045 | int has_name = FALSE; |
Bram Moolenaar | ca8b8d6 | 2016-11-17 21:30:27 +0100 | [diff] [blame] | 6046 | int has_name_start = FALSE; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 6047 | |
| 6048 | s = cin_skipcomment(s); |
| 6049 | if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9]))) |
| 6050 | { |
| 6051 | p = cin_skipcomment(skipwhite(s + 9)); |
| 6052 | while (*p != NUL) |
| 6053 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6054 | if (VIM_ISWHITE(*p)) |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 6055 | { |
| 6056 | has_name = TRUE; /* found end of a name */ |
| 6057 | p = cin_skipcomment(skipwhite(p)); |
| 6058 | } |
| 6059 | else if (*p == '{') |
| 6060 | { |
| 6061 | break; |
| 6062 | } |
| 6063 | else if (vim_iswordc(*p)) |
| 6064 | { |
Bram Moolenaar | ca8b8d6 | 2016-11-17 21:30:27 +0100 | [diff] [blame] | 6065 | has_name_start = TRUE; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 6066 | if (has_name) |
| 6067 | return FALSE; /* word character after skipping past name */ |
| 6068 | ++p; |
| 6069 | } |
Bram Moolenaar | ca8b8d6 | 2016-11-17 21:30:27 +0100 | [diff] [blame] | 6070 | else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2])) |
| 6071 | { |
| 6072 | if (!has_name_start || has_name) |
| 6073 | return FALSE; |
| 6074 | /* C++ 17 nested namespace */ |
| 6075 | p += 3; |
| 6076 | } |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 6077 | else |
| 6078 | { |
| 6079 | return FALSE; |
| 6080 | } |
| 6081 | } |
| 6082 | return TRUE; |
| 6083 | } |
| 6084 | return FALSE; |
| 6085 | } |
| 6086 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6087 | /* |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 6088 | * Recognize a `extern "C"` or `extern "C++"` linkage specifications. |
| 6089 | */ |
| 6090 | static int |
| 6091 | cin_is_cpp_extern_c(char_u *s) |
| 6092 | { |
| 6093 | char_u *p; |
| 6094 | int has_string_literal = FALSE; |
| 6095 | |
| 6096 | s = cin_skipcomment(s); |
| 6097 | if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6]))) |
| 6098 | { |
| 6099 | p = cin_skipcomment(skipwhite(s + 6)); |
| 6100 | while (*p != NUL) |
| 6101 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6102 | if (VIM_ISWHITE(*p)) |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 6103 | { |
| 6104 | p = cin_skipcomment(skipwhite(p)); |
| 6105 | } |
| 6106 | else if (*p == '{') |
| 6107 | { |
| 6108 | break; |
| 6109 | } |
| 6110 | else if (p[0] == '"' && p[1] == 'C' && p[2] == '"') |
| 6111 | { |
| 6112 | if (has_string_literal) |
| 6113 | return FALSE; |
| 6114 | has_string_literal = TRUE; |
| 6115 | p += 3; |
| 6116 | } |
| 6117 | else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+' |
| 6118 | && p[4] == '"') |
| 6119 | { |
| 6120 | if (has_string_literal) |
| 6121 | return FALSE; |
| 6122 | has_string_literal = TRUE; |
| 6123 | p += 5; |
| 6124 | } |
| 6125 | else |
| 6126 | { |
| 6127 | return FALSE; |
| 6128 | } |
| 6129 | } |
| 6130 | return has_string_literal ? TRUE : FALSE; |
| 6131 | } |
| 6132 | return FALSE; |
| 6133 | } |
| 6134 | |
| 6135 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6136 | * Return a pointer to the first non-empty non-comment character after a ':'. |
| 6137 | * Return NULL if not found. |
| 6138 | * case 234: a = b; |
| 6139 | * ^ |
| 6140 | */ |
| 6141 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6142 | after_label(char_u *l) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6143 | { |
| 6144 | for ( ; *l; ++l) |
| 6145 | { |
| 6146 | if (*l == ':') |
| 6147 | { |
| 6148 | if (l[1] == ':') /* skip over "::" for C++ */ |
| 6149 | ++l; |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 6150 | else if (!cin_iscase(l + 1, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6151 | break; |
| 6152 | } |
| 6153 | else if (*l == '\'' && l[1] && l[2] == '\'') |
| 6154 | l += 2; /* skip over 'x' */ |
| 6155 | } |
| 6156 | if (*l == NUL) |
| 6157 | return NULL; |
| 6158 | l = cin_skipcomment(l + 1); |
| 6159 | if (*l == NUL) |
| 6160 | return NULL; |
| 6161 | return l; |
| 6162 | } |
| 6163 | |
| 6164 | /* |
| 6165 | * Get indent of line "lnum", skipping a label. |
| 6166 | * Return 0 if there is nothing after the label. |
| 6167 | */ |
| 6168 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6169 | get_indent_nolabel (linenr_T lnum) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6170 | { |
| 6171 | char_u *l; |
| 6172 | pos_T fp; |
| 6173 | colnr_T col; |
| 6174 | char_u *p; |
| 6175 | |
| 6176 | l = ml_get(lnum); |
| 6177 | p = after_label(l); |
| 6178 | if (p == NULL) |
| 6179 | return 0; |
| 6180 | |
| 6181 | fp.col = (colnr_T)(p - l); |
| 6182 | fp.lnum = lnum; |
| 6183 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 6184 | return (int)col; |
| 6185 | } |
| 6186 | |
| 6187 | /* |
| 6188 | * Find indent for line "lnum", ignoring any case or jump label. |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6189 | * Also return a pointer to the text (after the label) in "pp". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6190 | * label: if (asdf && asdfasdf) |
| 6191 | * ^ |
| 6192 | */ |
| 6193 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6194 | skip_label(linenr_T lnum, char_u **pp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6195 | { |
| 6196 | char_u *l; |
| 6197 | int amount; |
| 6198 | pos_T cursor_save; |
| 6199 | |
| 6200 | cursor_save = curwin->w_cursor; |
| 6201 | curwin->w_cursor.lnum = lnum; |
| 6202 | l = ml_get_curline(); |
| 6203 | /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6204 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6205 | { |
| 6206 | amount = get_indent_nolabel(lnum); |
| 6207 | l = after_label(ml_get_curline()); |
| 6208 | if (l == NULL) /* just in case */ |
| 6209 | l = ml_get_curline(); |
| 6210 | } |
| 6211 | else |
| 6212 | { |
| 6213 | amount = get_indent(); |
| 6214 | l = ml_get_curline(); |
| 6215 | } |
| 6216 | *pp = l; |
| 6217 | |
| 6218 | curwin->w_cursor = cursor_save; |
| 6219 | return amount; |
| 6220 | } |
| 6221 | |
| 6222 | /* |
| 6223 | * Return the indent of the first variable name after a type in a declaration. |
| 6224 | * int a, indent of "a" |
| 6225 | * static struct foo b, indent of "b" |
| 6226 | * enum bla c, indent of "c" |
| 6227 | * Returns zero when it doesn't look like a declaration. |
| 6228 | */ |
| 6229 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6230 | cin_first_id_amount(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6231 | { |
| 6232 | char_u *line, *p, *s; |
| 6233 | int len; |
| 6234 | pos_T fp; |
| 6235 | colnr_T col; |
| 6236 | |
| 6237 | line = ml_get_curline(); |
| 6238 | p = skipwhite(line); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6239 | len = (int)(skiptowhite(p) - p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6240 | if (len == 6 && STRNCMP(p, "static", 6) == 0) |
| 6241 | { |
| 6242 | p = skipwhite(p + 6); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 6243 | len = (int)(skiptowhite(p) - p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6244 | } |
| 6245 | if (len == 6 && STRNCMP(p, "struct", 6) == 0) |
| 6246 | p = skipwhite(p + 6); |
| 6247 | else if (len == 4 && STRNCMP(p, "enum", 4) == 0) |
| 6248 | p = skipwhite(p + 4); |
| 6249 | else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0) |
| 6250 | || (len == 6 && STRNCMP(p, "signed", 6) == 0)) |
| 6251 | { |
| 6252 | s = skipwhite(p + len); |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6253 | if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3])) |
| 6254 | || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4])) |
| 6255 | || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5])) |
| 6256 | || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4]))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6257 | p = s; |
| 6258 | } |
| 6259 | for (len = 0; vim_isIDc(p[len]); ++len) |
| 6260 | ; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6261 | if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6262 | return 0; |
| 6263 | |
| 6264 | p = skipwhite(p + len); |
| 6265 | fp.lnum = curwin->w_cursor.lnum; |
| 6266 | fp.col = (colnr_T)(p - line); |
| 6267 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 6268 | return (int)col; |
| 6269 | } |
| 6270 | |
| 6271 | /* |
| 6272 | * Return the indent of the first non-blank after an equal sign. |
| 6273 | * char *foo = "here"; |
| 6274 | * Return zero if no (useful) equal sign found. |
| 6275 | * Return -1 if the line above "lnum" ends in a backslash. |
| 6276 | * foo = "asdf\ |
| 6277 | * asdf\ |
| 6278 | * here"; |
| 6279 | */ |
| 6280 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6281 | cin_get_equal_amount(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6282 | { |
| 6283 | char_u *line; |
| 6284 | char_u *s; |
| 6285 | colnr_T col; |
| 6286 | pos_T fp; |
| 6287 | |
| 6288 | if (lnum > 1) |
| 6289 | { |
| 6290 | line = ml_get(lnum - 1); |
| 6291 | if (*line != NUL && line[STRLEN(line) - 1] == '\\') |
| 6292 | return -1; |
| 6293 | } |
| 6294 | |
| 6295 | line = s = ml_get(lnum); |
| 6296 | while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL) |
| 6297 | { |
| 6298 | if (cin_iscomment(s)) /* ignore comments */ |
| 6299 | s = cin_skipcomment(s); |
| 6300 | else |
| 6301 | ++s; |
| 6302 | } |
| 6303 | if (*s != '=') |
| 6304 | return 0; |
| 6305 | |
| 6306 | s = skipwhite(s + 1); |
| 6307 | if (cin_nocode(s)) |
| 6308 | return 0; |
| 6309 | |
| 6310 | if (*s == '"') /* nice alignment for continued strings */ |
| 6311 | ++s; |
| 6312 | |
| 6313 | fp.lnum = lnum; |
| 6314 | fp.col = (colnr_T)(s - line); |
| 6315 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 6316 | return (int)col; |
| 6317 | } |
| 6318 | |
| 6319 | /* |
| 6320 | * Recognize a preprocessor statement: Any line that starts with '#'. |
| 6321 | */ |
| 6322 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6323 | cin_ispreproc(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6324 | { |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6325 | if (*skipwhite(s) == '#') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6326 | return TRUE; |
| 6327 | return FALSE; |
| 6328 | } |
| 6329 | |
| 6330 | /* |
| 6331 | * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a |
| 6332 | * continuation line of a preprocessor statement. Decrease "*lnump" to the |
| 6333 | * start and return the line in "*pp". |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6334 | * Put the amount of indent in "*amount". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6335 | */ |
| 6336 | static int |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6337 | cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6338 | { |
| 6339 | char_u *line = *pp; |
| 6340 | linenr_T lnum = *lnump; |
| 6341 | int retval = FALSE; |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6342 | int candidate_amount = *amount; |
| 6343 | |
| 6344 | if (*line != NUL && line[STRLEN(line) - 1] == '\\') |
| 6345 | candidate_amount = get_indent_lnum(lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6346 | |
Bram Moolenaar | d8e9bb2 | 2005-07-09 21:14:46 +0000 | [diff] [blame] | 6347 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6348 | { |
| 6349 | if (cin_ispreproc(line)) |
| 6350 | { |
| 6351 | retval = TRUE; |
| 6352 | *lnump = lnum; |
| 6353 | break; |
| 6354 | } |
| 6355 | if (lnum == 1) |
| 6356 | break; |
| 6357 | line = ml_get(--lnum); |
| 6358 | if (*line == NUL || line[STRLEN(line) - 1] != '\\') |
| 6359 | break; |
| 6360 | } |
| 6361 | |
| 6362 | if (lnum != *lnump) |
| 6363 | *pp = ml_get(*lnump); |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6364 | if (retval) |
| 6365 | *amount = candidate_amount; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6366 | return retval; |
| 6367 | } |
| 6368 | |
| 6369 | /* |
| 6370 | * Recognize the start of a C or C++ comment. |
| 6371 | */ |
| 6372 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6373 | cin_iscomment(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6374 | { |
| 6375 | return (p[0] == '/' && (p[1] == '*' || p[1] == '/')); |
| 6376 | } |
| 6377 | |
| 6378 | /* |
| 6379 | * Recognize the start of a "//" comment. |
| 6380 | */ |
| 6381 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6382 | cin_islinecomment(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6383 | { |
| 6384 | return (p[0] == '/' && p[1] == '/'); |
| 6385 | } |
| 6386 | |
| 6387 | /* |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6388 | * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or |
| 6389 | * '}'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6390 | * Don't consider "} else" a terminated line. |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6391 | * If a line begins with an "else", only consider it terminated if no unmatched |
| 6392 | * opening braces follow (handle "else { foo();" correctly). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6393 | * Return the character terminating the line (ending char's have precedence if |
| 6394 | * both apply in order to determine initializations). |
| 6395 | */ |
| 6396 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6397 | cin_isterminated( |
| 6398 | char_u *s, |
| 6399 | int incl_open, /* include '{' at the end as terminator */ |
| 6400 | int incl_comma) /* recognize a trailing comma */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6401 | { |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6402 | char_u found_start = 0; |
| 6403 | unsigned n_open = 0; |
| 6404 | int is_else = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6405 | |
| 6406 | s = cin_skipcomment(s); |
| 6407 | |
| 6408 | if (*s == '{' || (*s == '}' && !cin_iselse(s))) |
| 6409 | found_start = *s; |
| 6410 | |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6411 | if (!found_start) |
| 6412 | is_else = cin_iselse(s); |
| 6413 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6414 | while (*s) |
| 6415 | { |
| 6416 | /* skip over comments, "" strings and 'c'haracters */ |
| 6417 | s = skip_string(cin_skipcomment(s)); |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6418 | if (*s == '}' && n_open > 0) |
| 6419 | --n_open; |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6420 | if ((!is_else || n_open == 0) |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6421 | && (*s == ';' || *s == '}' || (incl_comma && *s == ',')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6422 | && cin_nocode(s + 1)) |
| 6423 | return *s; |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6424 | else if (*s == '{') |
| 6425 | { |
| 6426 | if (incl_open && cin_nocode(s + 1)) |
| 6427 | return *s; |
| 6428 | else |
| 6429 | ++n_open; |
| 6430 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6431 | |
| 6432 | if (*s) |
| 6433 | s++; |
| 6434 | } |
| 6435 | return found_start; |
| 6436 | } |
| 6437 | |
| 6438 | /* |
| 6439 | * Recognize the basic picture of a function declaration -- it needs to |
| 6440 | * have an open paren somewhere and a close paren at the end of the line and |
| 6441 | * no semicolons anywhere. |
| 6442 | * When a line ends in a comma we continue looking in the next line. |
| 6443 | * "sp" points to a string with the line. When looking at other lines it must |
| 6444 | * be restored to the line. When it's NULL fetch lines here. |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6445 | * "first_lnum" is where we start looking. |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6446 | * "min_lnum" is the line before which we will not be looking. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6447 | */ |
| 6448 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6449 | cin_isfuncdecl( |
| 6450 | char_u **sp, |
| 6451 | linenr_T first_lnum, |
| 6452 | linenr_T min_lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6453 | { |
| 6454 | char_u *s; |
| 6455 | linenr_T lnum = first_lnum; |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6456 | linenr_T save_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6457 | int retval = FALSE; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6458 | pos_T *trypos; |
| 6459 | int just_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6460 | |
| 6461 | if (sp == NULL) |
| 6462 | s = ml_get(lnum); |
| 6463 | else |
| 6464 | s = *sp; |
| 6465 | |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6466 | curwin->w_cursor.lnum = lnum; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6467 | if (find_last_paren(s, '(', ')') |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6468 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6469 | { |
| 6470 | lnum = trypos->lnum; |
| 6471 | if (lnum < min_lnum) |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6472 | { |
| 6473 | curwin->w_cursor.lnum = save_lnum; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6474 | return FALSE; |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6475 | } |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6476 | |
| 6477 | s = ml_get(lnum); |
| 6478 | } |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6479 | curwin->w_cursor.lnum = save_lnum; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6480 | |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6481 | /* Ignore line starting with #. */ |
| 6482 | if (cin_ispreproc(s)) |
| 6483 | return FALSE; |
| 6484 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6485 | while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"') |
| 6486 | { |
| 6487 | if (cin_iscomment(s)) /* ignore comments */ |
| 6488 | s = cin_skipcomment(s); |
Bram Moolenaar | e01f4f8 | 2015-11-10 14:06:53 +0100 | [diff] [blame] | 6489 | else if (*s == ':') |
| 6490 | { |
| 6491 | if (*(s + 1) == ':') |
| 6492 | s += 2; |
| 6493 | else |
| 6494 | /* To avoid a mistake in the following situation: |
| 6495 | * A::A(int a, int b) |
| 6496 | * : a(0) // <--not a function decl |
| 6497 | * , b(0) |
| 6498 | * {... |
| 6499 | */ |
| 6500 | return FALSE; |
| 6501 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6502 | else |
| 6503 | ++s; |
| 6504 | } |
| 6505 | if (*s != '(') |
| 6506 | return FALSE; /* ';', ' or " before any () or no '(' */ |
| 6507 | |
| 6508 | while (*s && *s != ';' && *s != '\'' && *s != '"') |
| 6509 | { |
| 6510 | if (*s == ')' && cin_nocode(s + 1)) |
| 6511 | { |
| 6512 | /* ')' at the end: may have found a match |
| 6513 | * Check for he previous line not to end in a backslash: |
| 6514 | * #if defined(x) && \ |
| 6515 | * defined(y) |
| 6516 | */ |
| 6517 | lnum = first_lnum - 1; |
| 6518 | s = ml_get(lnum); |
| 6519 | if (*s == NUL || s[STRLEN(s) - 1] != '\\') |
| 6520 | retval = TRUE; |
| 6521 | goto done; |
| 6522 | } |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6523 | if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6524 | { |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6525 | int comma = (*s == ','); |
| 6526 | |
| 6527 | /* ',' at the end: continue looking in the next line. |
| 6528 | * At the end: check for ',' in the next line, for this style: |
| 6529 | * func(arg1 |
| 6530 | * , arg2) */ |
| 6531 | for (;;) |
| 6532 | { |
| 6533 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 6534 | break; |
| 6535 | s = ml_get(++lnum); |
| 6536 | if (!cin_ispreproc(s)) |
| 6537 | break; |
| 6538 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6539 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 6540 | break; |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6541 | /* Require a comma at end of the line or a comma or ')' at the |
| 6542 | * start of next line. */ |
| 6543 | s = skipwhite(s); |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6544 | if (!just_started && (!comma && *s != ',' && *s != ')')) |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6545 | break; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6546 | just_started = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6547 | } |
| 6548 | else if (cin_iscomment(s)) /* ignore comments */ |
| 6549 | s = cin_skipcomment(s); |
| 6550 | else |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6551 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6552 | ++s; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6553 | just_started = FALSE; |
| 6554 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6555 | } |
| 6556 | |
| 6557 | done: |
| 6558 | if (lnum != first_lnum && sp != NULL) |
| 6559 | *sp = ml_get(first_lnum); |
| 6560 | |
| 6561 | return retval; |
| 6562 | } |
| 6563 | |
| 6564 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6565 | cin_isif(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6566 | { |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6567 | return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2])); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6568 | } |
| 6569 | |
| 6570 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6571 | cin_iselse( |
| 6572 | char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6573 | { |
| 6574 | if (*p == '}') /* accept "} else" */ |
| 6575 | p = cin_skipcomment(p + 1); |
| 6576 | return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4])); |
| 6577 | } |
| 6578 | |
| 6579 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6580 | cin_isdo(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6581 | { |
| 6582 | return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2])); |
| 6583 | } |
| 6584 | |
| 6585 | /* |
| 6586 | * Check if this is a "while" that should have a matching "do". |
| 6587 | * We only accept a "while (condition) ;", with only white space between the |
| 6588 | * ')' and ';'. The condition may be spread over several lines. |
| 6589 | */ |
| 6590 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6591 | cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6592 | { |
| 6593 | pos_T cursor_save; |
| 6594 | pos_T *trypos; |
| 6595 | int retval = FALSE; |
| 6596 | |
| 6597 | p = cin_skipcomment(p); |
| 6598 | if (*p == '}') /* accept "} while (cond);" */ |
| 6599 | p = cin_skipcomment(p + 1); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6600 | if (cin_starts_with(p, "while")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6601 | { |
| 6602 | cursor_save = curwin->w_cursor; |
| 6603 | curwin->w_cursor.lnum = lnum; |
| 6604 | curwin->w_cursor.col = 0; |
| 6605 | p = ml_get_curline(); |
| 6606 | while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */ |
| 6607 | { |
| 6608 | ++p; |
| 6609 | ++curwin->w_cursor.col; |
| 6610 | } |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6611 | if ((trypos = findmatchlimit(NULL, 0, 0, |
| 6612 | curbuf->b_ind_maxparen)) != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6613 | && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';') |
| 6614 | retval = TRUE; |
| 6615 | curwin->w_cursor = cursor_save; |
| 6616 | } |
| 6617 | return retval; |
| 6618 | } |
| 6619 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6620 | /* |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 6621 | * 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] | 6622 | * Return 0 if there is none. |
| 6623 | * Otherwise return !0 and update "*poffset" to point to the place where the |
| 6624 | * string was found. |
| 6625 | */ |
| 6626 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6627 | cin_is_if_for_while_before_offset(char_u *line, int *poffset) |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6628 | { |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 6629 | int offset = *poffset; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6630 | |
| 6631 | if (offset-- < 2) |
| 6632 | return 0; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6633 | while (offset > 2 && VIM_ISWHITE(line[offset])) |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6634 | --offset; |
| 6635 | |
| 6636 | offset -= 1; |
| 6637 | if (!STRNCMP(line + offset, "if", 2)) |
| 6638 | goto probablyFound; |
| 6639 | |
| 6640 | if (offset >= 1) |
| 6641 | { |
| 6642 | offset -= 1; |
| 6643 | if (!STRNCMP(line + offset, "for", 3)) |
| 6644 | goto probablyFound; |
| 6645 | |
| 6646 | if (offset >= 2) |
| 6647 | { |
| 6648 | offset -= 2; |
| 6649 | if (!STRNCMP(line + offset, "while", 5)) |
| 6650 | goto probablyFound; |
| 6651 | } |
| 6652 | } |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6653 | return 0; |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 6654 | |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6655 | probablyFound: |
| 6656 | if (!offset || !vim_isIDc(line[offset - 1])) |
| 6657 | { |
| 6658 | *poffset = offset; |
| 6659 | return 1; |
| 6660 | } |
| 6661 | return 0; |
| 6662 | } |
| 6663 | |
| 6664 | /* |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6665 | * Return TRUE if we are at the end of a do-while. |
| 6666 | * do |
| 6667 | * nothing; |
| 6668 | * while (foo |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 6669 | * && bar); <-- here |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6670 | * Adjust the cursor to the line with "while". |
| 6671 | */ |
| 6672 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6673 | cin_iswhileofdo_end(int terminated) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6674 | { |
| 6675 | char_u *line; |
| 6676 | char_u *p; |
| 6677 | char_u *s; |
| 6678 | pos_T *trypos; |
| 6679 | int i; |
| 6680 | |
| 6681 | if (terminated != ';') /* there must be a ';' at the end */ |
| 6682 | return FALSE; |
| 6683 | |
| 6684 | p = line = ml_get_curline(); |
| 6685 | while (*p != NUL) |
| 6686 | { |
| 6687 | p = cin_skipcomment(p); |
| 6688 | if (*p == ')') |
| 6689 | { |
| 6690 | s = skipwhite(p + 1); |
| 6691 | if (*s == ';' && cin_nocode(s + 1)) |
| 6692 | { |
| 6693 | /* Found ");" at end of the line, now check there is "while" |
| 6694 | * before the matching '('. XXX */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6695 | i = (int)(p - line); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6696 | curwin->w_cursor.col = i; |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6697 | trypos = find_match_paren(curbuf->b_ind_maxparen); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6698 | if (trypos != NULL) |
| 6699 | { |
| 6700 | s = cin_skipcomment(ml_get(trypos->lnum)); |
| 6701 | if (*s == '}') /* accept "} while (cond);" */ |
| 6702 | s = cin_skipcomment(s + 1); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6703 | if (cin_starts_with(s, "while")) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6704 | { |
| 6705 | curwin->w_cursor.lnum = trypos->lnum; |
| 6706 | return TRUE; |
| 6707 | } |
| 6708 | } |
| 6709 | |
| 6710 | /* Searching may have made "line" invalid, get it again. */ |
| 6711 | line = ml_get_curline(); |
| 6712 | p = line + i; |
| 6713 | } |
| 6714 | } |
| 6715 | if (*p != NUL) |
| 6716 | ++p; |
| 6717 | } |
| 6718 | return FALSE; |
| 6719 | } |
| 6720 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6721 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6722 | cin_isbreak(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6723 | { |
| 6724 | return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5])); |
| 6725 | } |
| 6726 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6727 | /* |
| 6728 | * Find the position of a C++ base-class declaration or |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6729 | * constructor-initialization. eg: |
| 6730 | * |
| 6731 | * class MyClass : |
| 6732 | * baseClass <-- here |
| 6733 | * class MyClass : public baseClass, |
| 6734 | * anotherBaseClass <-- here (should probably lineup ??) |
| 6735 | * MyClass::MyClass(...) : |
| 6736 | * baseClass(...) <-- here (constructor-initialization) |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 6737 | * |
| 6738 | * This is a lot of guessing. Watch out for "cond ? func() : foo". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6739 | */ |
| 6740 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6741 | cin_is_cpp_baseclass( |
| 6742 | cpp_baseclass_cache_T *cached) /* input and output */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6743 | { |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6744 | lpos_T *pos = &cached->lpos; /* find position */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6745 | char_u *s; |
| 6746 | int class_or_struct, lookfor_ctor_init, cpp_base_class; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6747 | linenr_T lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6748 | char_u *line = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6749 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6750 | if (pos->lnum <= lnum) |
| 6751 | return cached->found; /* Use the cached result */ |
| 6752 | |
| 6753 | pos->col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6754 | |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 6755 | s = skipwhite(line); |
| 6756 | if (*s == '#') /* skip #define FOO x ? (x) : x */ |
| 6757 | return FALSE; |
| 6758 | s = cin_skipcomment(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6759 | if (*s == NUL) |
| 6760 | return FALSE; |
| 6761 | |
| 6762 | cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; |
| 6763 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6764 | /* Search for a line starting with '#', empty, ending in ';' or containing |
| 6765 | * '{' or '}' and start below it. This handles the following situations: |
| 6766 | * a = cond ? |
| 6767 | * func() : |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 6768 | * asdf; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6769 | * func::foo() |
| 6770 | * : something |
| 6771 | * {} |
| 6772 | * Foo::Foo (int one, int two) |
| 6773 | * : something(4), |
| 6774 | * somethingelse(3) |
| 6775 | * {} |
| 6776 | */ |
| 6777 | while (lnum > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6778 | { |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6779 | line = ml_get(lnum - 1); |
| 6780 | s = skipwhite(line); |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6781 | if (*s == '#' || *s == NUL) |
| 6782 | break; |
| 6783 | while (*s != NUL) |
| 6784 | { |
| 6785 | s = cin_skipcomment(s); |
| 6786 | if (*s == '{' || *s == '}' |
| 6787 | || (*s == ';' && cin_nocode(s + 1))) |
| 6788 | break; |
| 6789 | if (*s != NUL) |
| 6790 | ++s; |
| 6791 | } |
| 6792 | if (*s != NUL) |
| 6793 | break; |
| 6794 | --lnum; |
| 6795 | } |
| 6796 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6797 | pos->lnum = lnum; |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6798 | line = ml_get(lnum); |
Bram Moolenaar | d1b15de | 2015-10-13 16:13:39 +0200 | [diff] [blame] | 6799 | s = line; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6800 | for (;;) |
| 6801 | { |
| 6802 | if (*s == NUL) |
| 6803 | { |
| 6804 | if (lnum == curwin->w_cursor.lnum) |
| 6805 | break; |
| 6806 | /* Continue in the cursor line. */ |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6807 | line = ml_get(++lnum); |
Bram Moolenaar | d1b15de | 2015-10-13 16:13:39 +0200 | [diff] [blame] | 6808 | s = line; |
| 6809 | } |
| 6810 | if (s == line) |
| 6811 | { |
| 6812 | /* don't recognize "case (foo):" as a baseclass */ |
| 6813 | if (cin_iscase(s, FALSE)) |
| 6814 | break; |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6815 | s = cin_skipcomment(line); |
| 6816 | if (*s == NUL) |
| 6817 | continue; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6818 | } |
| 6819 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 6820 | if (s[0] == '"' || (s[0] == 'R' && s[1] == '"')) |
Bram Moolenaar | aede6ce | 2011-05-10 11:56:30 +0200 | [diff] [blame] | 6821 | s = skip_string(s) + 1; |
| 6822 | else if (s[0] == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6823 | { |
| 6824 | if (s[1] == ':') |
| 6825 | { |
| 6826 | /* skip double colon. It can't be a constructor |
| 6827 | * initialization any more */ |
| 6828 | lookfor_ctor_init = FALSE; |
| 6829 | s = cin_skipcomment(s + 2); |
| 6830 | } |
| 6831 | else if (lookfor_ctor_init || class_or_struct) |
| 6832 | { |
| 6833 | /* we have something found, that looks like the start of |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 6834 | * cpp-base-class-declaration or constructor-initialization */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6835 | cpp_base_class = TRUE; |
| 6836 | lookfor_ctor_init = class_or_struct = FALSE; |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6837 | pos->col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6838 | s = cin_skipcomment(s + 1); |
| 6839 | } |
| 6840 | else |
| 6841 | s = cin_skipcomment(s + 1); |
| 6842 | } |
| 6843 | else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5])) |
| 6844 | || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6]))) |
| 6845 | { |
| 6846 | class_or_struct = TRUE; |
| 6847 | lookfor_ctor_init = FALSE; |
| 6848 | |
| 6849 | if (*s == 'c') |
| 6850 | s = cin_skipcomment(s + 5); |
| 6851 | else |
| 6852 | s = cin_skipcomment(s + 6); |
| 6853 | } |
| 6854 | else |
| 6855 | { |
| 6856 | if (s[0] == '{' || s[0] == '}' || s[0] == ';') |
| 6857 | { |
| 6858 | cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; |
| 6859 | } |
| 6860 | else if (s[0] == ')') |
| 6861 | { |
| 6862 | /* Constructor-initialization is assumed if we come across |
| 6863 | * something like "):" */ |
| 6864 | class_or_struct = FALSE; |
| 6865 | lookfor_ctor_init = TRUE; |
| 6866 | } |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 6867 | else if (s[0] == '?') |
| 6868 | { |
| 6869 | /* Avoid seeing '() :' after '?' as constructor init. */ |
| 6870 | return FALSE; |
| 6871 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6872 | else if (!vim_isIDc(s[0])) |
| 6873 | { |
| 6874 | /* if it is not an identifier, we are wrong */ |
| 6875 | class_or_struct = FALSE; |
| 6876 | lookfor_ctor_init = FALSE; |
| 6877 | } |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6878 | else if (pos->col == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6879 | { |
| 6880 | /* it can't be a constructor-initialization any more */ |
| 6881 | lookfor_ctor_init = FALSE; |
| 6882 | |
| 6883 | /* the first statement starts here: lineup with this one... */ |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6884 | if (cpp_base_class) |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6885 | pos->col = (colnr_T)(s - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6886 | } |
| 6887 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6888 | /* When the line ends in a comma don't align with it. */ |
| 6889 | if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1)) |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6890 | pos->col = 0; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6891 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6892 | s = cin_skipcomment(s + 1); |
| 6893 | } |
| 6894 | } |
| 6895 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6896 | cached->found = cpp_base_class; |
| 6897 | if (cpp_base_class) |
| 6898 | pos->lnum = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6899 | return cpp_base_class; |
| 6900 | } |
| 6901 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6902 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6903 | get_baseclass_amount(int col) |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6904 | { |
| 6905 | int amount; |
| 6906 | colnr_T vcol; |
| 6907 | pos_T *trypos; |
| 6908 | |
| 6909 | if (col == 0) |
| 6910 | { |
| 6911 | amount = get_indent(); |
| 6912 | if (find_last_paren(ml_get_curline(), '(', ')') |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6913 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6914 | amount = get_indent_lnum(trypos->lnum); /* XXX */ |
| 6915 | if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL)) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6916 | amount += curbuf->b_ind_cpp_baseclass; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6917 | } |
| 6918 | else |
| 6919 | { |
| 6920 | curwin->w_cursor.col = col; |
| 6921 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 6922 | amount = (int)vcol; |
| 6923 | } |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6924 | if (amount < curbuf->b_ind_cpp_baseclass) |
| 6925 | amount = curbuf->b_ind_cpp_baseclass; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6926 | return amount; |
| 6927 | } |
| 6928 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6929 | /* |
| 6930 | * Return TRUE if string "s" ends with the string "find", possibly followed by |
| 6931 | * white space and comments. Skip strings and comments. |
| 6932 | * Ignore "ignore" after "find" if it's not NULL. |
| 6933 | */ |
| 6934 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6935 | cin_ends_in(char_u *s, char_u *find, char_u *ignore) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6936 | { |
| 6937 | char_u *p = s; |
| 6938 | char_u *r; |
| 6939 | int len = (int)STRLEN(find); |
| 6940 | |
| 6941 | while (*p != NUL) |
| 6942 | { |
| 6943 | p = cin_skipcomment(p); |
| 6944 | if (STRNCMP(p, find, len) == 0) |
| 6945 | { |
| 6946 | r = skipwhite(p + len); |
| 6947 | if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0) |
| 6948 | r = skipwhite(r + STRLEN(ignore)); |
| 6949 | if (cin_nocode(r)) |
| 6950 | return TRUE; |
| 6951 | } |
| 6952 | if (*p != NUL) |
| 6953 | ++p; |
| 6954 | } |
| 6955 | return FALSE; |
| 6956 | } |
| 6957 | |
| 6958 | /* |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6959 | * Return TRUE when "s" starts with "word" and then a non-ID character. |
| 6960 | */ |
| 6961 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6962 | cin_starts_with(char_u *s, char *word) |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6963 | { |
Bram Moolenaar | b3cb982 | 2013-03-13 17:01:52 +0100 | [diff] [blame] | 6964 | int l = (int)STRLEN(word); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6965 | |
| 6966 | return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l])); |
| 6967 | } |
| 6968 | |
| 6969 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6970 | * Skip strings, chars and comments until at or past "trypos". |
| 6971 | * Return the column found. |
| 6972 | */ |
| 6973 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6974 | cin_skip2pos(pos_T *trypos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6975 | { |
| 6976 | char_u *line; |
| 6977 | char_u *p; |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 6978 | char_u *new_p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6979 | |
| 6980 | p = line = ml_get(trypos->lnum); |
| 6981 | while (*p && (colnr_T)(p - line) < trypos->col) |
| 6982 | { |
| 6983 | if (cin_iscomment(p)) |
| 6984 | p = cin_skipcomment(p); |
| 6985 | else |
| 6986 | { |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 6987 | new_p = skip_string(p); |
| 6988 | if (new_p == p) |
| 6989 | ++p; |
| 6990 | else |
| 6991 | p = new_p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6992 | } |
| 6993 | } |
| 6994 | return (int)(p - line); |
| 6995 | } |
| 6996 | |
| 6997 | /* |
| 6998 | * Find the '{' at the start of the block we are in. |
| 6999 | * Return NULL if no match found. |
| 7000 | * Ignore a '{' that is in a comment, makes indenting the next three lines |
| 7001 | * work. */ |
| 7002 | /* foo() */ |
| 7003 | /* { */ |
| 7004 | /* } */ |
| 7005 | |
| 7006 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7007 | find_start_brace(void) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7008 | { |
| 7009 | pos_T cursor_save; |
| 7010 | pos_T *trypos; |
| 7011 | pos_T *pos; |
| 7012 | static pos_T pos_copy; |
| 7013 | |
| 7014 | cursor_save = curwin->w_cursor; |
| 7015 | while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL) |
| 7016 | { |
| 7017 | pos_copy = *trypos; /* copy pos_T, next findmatch will change it */ |
| 7018 | trypos = &pos_copy; |
| 7019 | curwin->w_cursor = *trypos; |
| 7020 | pos = NULL; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7021 | /* ignore the { if it's in a // or / * * / comment */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7022 | if ((colnr_T)cin_skip2pos(trypos) == trypos->col |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 7023 | && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7024 | break; |
| 7025 | if (pos != NULL) |
| 7026 | curwin->w_cursor.lnum = pos->lnum; |
| 7027 | } |
| 7028 | curwin->w_cursor = cursor_save; |
| 7029 | return trypos; |
| 7030 | } |
| 7031 | |
| 7032 | /* |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 7033 | * Find the matching '(', ignoring it if it is in a comment. |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7034 | * Return NULL if no match found. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7035 | */ |
| 7036 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7037 | find_match_paren(int ind_maxparen) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7038 | { |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 7039 | return find_match_char('(', ind_maxparen); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7040 | } |
| 7041 | |
| 7042 | static pos_T * |
Bram Moolenaar | 6dff58f | 2018-09-30 21:43:26 +0200 | [diff] [blame] | 7043 | find_match_char(int c, int ind_maxparen) /* XXX */ |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7044 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7045 | pos_T cursor_save; |
| 7046 | pos_T *trypos; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7047 | static pos_T pos_copy; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7048 | int ind_maxp_wk; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7049 | |
| 7050 | cursor_save = curwin->w_cursor; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7051 | ind_maxp_wk = ind_maxparen; |
| 7052 | retry: |
| 7053 | if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7054 | { |
| 7055 | /* check if the ( is in a // comment */ |
| 7056 | if ((colnr_T)cin_skip2pos(trypos) > trypos->col) |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7057 | { |
| 7058 | ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum); |
| 7059 | if (ind_maxp_wk > 0) |
| 7060 | { |
| 7061 | curwin->w_cursor = *trypos; |
| 7062 | curwin->w_cursor.col = 0; /* XXX */ |
| 7063 | goto retry; |
| 7064 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7065 | trypos = NULL; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7066 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7067 | else |
| 7068 | { |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7069 | pos_T *trypos_wk; |
| 7070 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7071 | pos_copy = *trypos; /* copy trypos, findmatch will change it */ |
| 7072 | trypos = &pos_copy; |
| 7073 | curwin->w_cursor = *trypos; |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 7074 | if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */ |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7075 | { |
| 7076 | ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum |
| 7077 | - trypos_wk->lnum); |
| 7078 | if (ind_maxp_wk > 0) |
| 7079 | { |
| 7080 | curwin->w_cursor = *trypos_wk; |
| 7081 | goto retry; |
| 7082 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7083 | trypos = NULL; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7084 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7085 | } |
| 7086 | } |
| 7087 | curwin->w_cursor = cursor_save; |
| 7088 | return trypos; |
| 7089 | } |
| 7090 | |
| 7091 | /* |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 7092 | * Find the matching '(', ignoring it if it is in a comment or before an |
| 7093 | * unmatched {. |
| 7094 | * Return NULL if no match found. |
| 7095 | */ |
| 7096 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7097 | find_match_paren_after_brace (int ind_maxparen) /* XXX */ |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 7098 | { |
| 7099 | pos_T *trypos = find_match_paren(ind_maxparen); |
| 7100 | |
| 7101 | if (trypos != NULL) |
| 7102 | { |
| 7103 | pos_T *tryposBrace = find_start_brace(); |
| 7104 | |
| 7105 | /* If both an unmatched '(' and '{' is found. Ignore the '(' |
| 7106 | * position if the '{' is further down. */ |
| 7107 | if (tryposBrace != NULL |
| 7108 | && (trypos->lnum != tryposBrace->lnum |
| 7109 | ? trypos->lnum < tryposBrace->lnum |
| 7110 | : trypos->col < tryposBrace->col)) |
| 7111 | trypos = NULL; |
| 7112 | } |
| 7113 | return trypos; |
| 7114 | } |
| 7115 | |
| 7116 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7117 | * Return ind_maxparen corrected for the difference in line number between the |
| 7118 | * cursor position and "startpos". This makes sure that searching for a |
| 7119 | * matching paren above the cursor line doesn't find a match because of |
| 7120 | * looking a few lines further. |
| 7121 | */ |
| 7122 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7123 | corr_ind_maxparen(pos_T *startpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7124 | { |
| 7125 | long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum; |
| 7126 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7127 | if (n > 0 && n < curbuf->b_ind_maxparen / 2) |
| 7128 | return curbuf->b_ind_maxparen - (int)n; |
| 7129 | return curbuf->b_ind_maxparen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7130 | } |
| 7131 | |
| 7132 | /* |
| 7133 | * 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] | 7134 | * line "l". "l" must point to the start of the line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7135 | */ |
| 7136 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7137 | find_last_paren(char_u *l, int start, int end) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7138 | { |
| 7139 | int i; |
| 7140 | int retval = FALSE; |
| 7141 | int open_count = 0; |
| 7142 | |
| 7143 | curwin->w_cursor.col = 0; /* default is start of line */ |
| 7144 | |
Bram Moolenaar | 6d8f9c6 | 2011-11-30 13:03:28 +0100 | [diff] [blame] | 7145 | for (i = 0; l[i] != NUL; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7146 | { |
| 7147 | i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */ |
| 7148 | i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */ |
| 7149 | if (l[i] == start) |
| 7150 | ++open_count; |
| 7151 | else if (l[i] == end) |
| 7152 | { |
| 7153 | if (open_count > 0) |
| 7154 | --open_count; |
| 7155 | else |
| 7156 | { |
| 7157 | curwin->w_cursor.col = i; |
| 7158 | retval = TRUE; |
| 7159 | } |
| 7160 | } |
| 7161 | } |
| 7162 | return retval; |
| 7163 | } |
| 7164 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7165 | /* |
| 7166 | * Parse 'cinoptions' and set the values in "curbuf". |
| 7167 | * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes. |
| 7168 | */ |
| 7169 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7170 | parse_cino(buf_T *buf) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7171 | { |
| 7172 | char_u *p; |
| 7173 | char_u *l; |
| 7174 | char_u *digits; |
| 7175 | int n; |
| 7176 | int divider; |
| 7177 | int fraction = 0; |
| 7178 | int sw = (int)get_sw_value(buf); |
| 7179 | |
| 7180 | /* |
| 7181 | * Set the default values. |
| 7182 | */ |
| 7183 | /* Spaces from a block's opening brace the prevailing indent for that |
| 7184 | * block should be. */ |
| 7185 | buf->b_ind_level = sw; |
| 7186 | |
| 7187 | /* Spaces from the edge of the line an open brace that's at the end of a |
| 7188 | * line is imagined to be. */ |
| 7189 | buf->b_ind_open_imag = 0; |
| 7190 | |
| 7191 | /* Spaces from the prevailing indent for a line that is not preceded by |
| 7192 | * an opening brace. */ |
| 7193 | buf->b_ind_no_brace = 0; |
| 7194 | |
| 7195 | /* Column where the first { of a function should be located }. */ |
| 7196 | buf->b_ind_first_open = 0; |
| 7197 | |
| 7198 | /* Spaces from the prevailing indent a leftmost open brace should be |
| 7199 | * located. */ |
| 7200 | buf->b_ind_open_extra = 0; |
| 7201 | |
| 7202 | /* Spaces from the matching open brace (real location for one at the left |
| 7203 | * edge; imaginary location from one that ends a line) the matching close |
| 7204 | * brace should be located. */ |
| 7205 | buf->b_ind_close_extra = 0; |
| 7206 | |
| 7207 | /* Spaces from the edge of the line an open brace sitting in the leftmost |
| 7208 | * column is imagined to be. */ |
| 7209 | buf->b_ind_open_left_imag = 0; |
| 7210 | |
| 7211 | /* Spaces jump labels should be shifted to the left if N is non-negative, |
| 7212 | * otherwise the jump label will be put to column 1. */ |
| 7213 | buf->b_ind_jump_label = -1; |
| 7214 | |
| 7215 | /* Spaces from the switch() indent a "case xx" label should be located. */ |
| 7216 | buf->b_ind_case = sw; |
| 7217 | |
| 7218 | /* Spaces from the "case xx:" code after a switch() should be located. */ |
| 7219 | buf->b_ind_case_code = sw; |
| 7220 | |
| 7221 | /* Lineup break at end of case in switch() with case label. */ |
| 7222 | buf->b_ind_case_break = 0; |
| 7223 | |
| 7224 | /* Spaces from the class declaration indent a scope declaration label |
| 7225 | * should be located. */ |
| 7226 | buf->b_ind_scopedecl = sw; |
| 7227 | |
| 7228 | /* Spaces from the scope declaration label code should be located. */ |
| 7229 | buf->b_ind_scopedecl_code = sw; |
| 7230 | |
| 7231 | /* Amount K&R-style parameters should be indented. */ |
| 7232 | buf->b_ind_param = sw; |
| 7233 | |
| 7234 | /* Amount a function type spec should be indented. */ |
| 7235 | buf->b_ind_func_type = sw; |
| 7236 | |
| 7237 | /* Amount a cpp base class declaration or constructor initialization |
| 7238 | * should be indented. */ |
| 7239 | buf->b_ind_cpp_baseclass = sw; |
| 7240 | |
| 7241 | /* additional spaces beyond the prevailing indent a continuation line |
| 7242 | * should be located. */ |
| 7243 | buf->b_ind_continuation = sw; |
| 7244 | |
| 7245 | /* Spaces from the indent of the line with an unclosed parentheses. */ |
| 7246 | buf->b_ind_unclosed = sw * 2; |
| 7247 | |
| 7248 | /* Spaces from the indent of the line with an unclosed parentheses, which |
| 7249 | * itself is also unclosed. */ |
| 7250 | buf->b_ind_unclosed2 = sw; |
| 7251 | |
| 7252 | /* Suppress ignoring spaces from the indent of a line starting with an |
| 7253 | * unclosed parentheses. */ |
| 7254 | buf->b_ind_unclosed_noignore = 0; |
| 7255 | |
| 7256 | /* If the opening paren is the last nonwhite character on the line, and |
| 7257 | * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer |
| 7258 | * context (for very long lines). */ |
| 7259 | buf->b_ind_unclosed_wrapped = 0; |
| 7260 | |
| 7261 | /* Suppress ignoring white space when lining up with the character after |
| 7262 | * an unclosed parentheses. */ |
| 7263 | buf->b_ind_unclosed_whiteok = 0; |
| 7264 | |
| 7265 | /* Indent a closing parentheses under the line start of the matching |
| 7266 | * opening parentheses. */ |
| 7267 | buf->b_ind_matching_paren = 0; |
| 7268 | |
| 7269 | /* Indent a closing parentheses under the previous line. */ |
| 7270 | buf->b_ind_paren_prev = 0; |
| 7271 | |
| 7272 | /* Extra indent for comments. */ |
| 7273 | buf->b_ind_comment = 0; |
| 7274 | |
| 7275 | /* Spaces from the comment opener when there is nothing after it. */ |
| 7276 | buf->b_ind_in_comment = 3; |
| 7277 | |
| 7278 | /* Boolean: if non-zero, use b_ind_in_comment even if there is something |
| 7279 | * after the comment opener. */ |
| 7280 | buf->b_ind_in_comment2 = 0; |
| 7281 | |
| 7282 | /* Max lines to search for an open paren. */ |
| 7283 | buf->b_ind_maxparen = 20; |
| 7284 | |
| 7285 | /* Max lines to search for an open comment. */ |
| 7286 | buf->b_ind_maxcomment = 70; |
| 7287 | |
| 7288 | /* Handle braces for java code. */ |
| 7289 | buf->b_ind_java = 0; |
| 7290 | |
| 7291 | /* Not to confuse JS object properties with labels. */ |
| 7292 | buf->b_ind_js = 0; |
| 7293 | |
| 7294 | /* Handle blocked cases correctly. */ |
| 7295 | buf->b_ind_keep_case_label = 0; |
| 7296 | |
| 7297 | /* Handle C++ namespace. */ |
| 7298 | buf->b_ind_cpp_namespace = 0; |
| 7299 | |
| 7300 | /* Handle continuation lines containing conditions of if(), for() and |
| 7301 | * while(). */ |
| 7302 | buf->b_ind_if_for_while = 0; |
| 7303 | |
Bram Moolenaar | 6b64394 | 2017-03-05 19:44:06 +0100 | [diff] [blame] | 7304 | /* indentation for # comments */ |
| 7305 | buf->b_ind_hash_comment = 0; |
| 7306 | |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 7307 | /* Handle C++ extern "C" or "C++" */ |
| 7308 | buf->b_ind_cpp_extern_c = 0; |
| 7309 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7310 | for (p = buf->b_p_cino; *p; ) |
| 7311 | { |
| 7312 | l = p++; |
| 7313 | if (*p == '-') |
| 7314 | ++p; |
| 7315 | digits = p; /* remember where the digits start */ |
| 7316 | n = getdigits(&p); |
| 7317 | divider = 0; |
| 7318 | if (*p == '.') /* ".5s" means a fraction */ |
| 7319 | { |
| 7320 | fraction = atol((char *)++p); |
| 7321 | while (VIM_ISDIGIT(*p)) |
| 7322 | { |
| 7323 | ++p; |
| 7324 | if (divider) |
| 7325 | divider *= 10; |
| 7326 | else |
| 7327 | divider = 10; |
| 7328 | } |
| 7329 | } |
| 7330 | if (*p == 's') /* "2s" means two times 'shiftwidth' */ |
| 7331 | { |
| 7332 | if (p == digits) |
| 7333 | n = sw; /* just "s" is one 'shiftwidth' */ |
| 7334 | else |
| 7335 | { |
| 7336 | n *= sw; |
| 7337 | if (divider) |
| 7338 | n += (sw * fraction + divider / 2) / divider; |
| 7339 | } |
| 7340 | ++p; |
| 7341 | } |
| 7342 | if (l[1] == '-') |
| 7343 | n = -n; |
| 7344 | |
| 7345 | /* When adding an entry here, also update the default 'cinoptions' in |
| 7346 | * doc/indent.txt, and add explanation for it! */ |
| 7347 | switch (*l) |
| 7348 | { |
| 7349 | case '>': buf->b_ind_level = n; break; |
| 7350 | case 'e': buf->b_ind_open_imag = n; break; |
| 7351 | case 'n': buf->b_ind_no_brace = n; break; |
| 7352 | case 'f': buf->b_ind_first_open = n; break; |
| 7353 | case '{': buf->b_ind_open_extra = n; break; |
| 7354 | case '}': buf->b_ind_close_extra = n; break; |
| 7355 | case '^': buf->b_ind_open_left_imag = n; break; |
| 7356 | case 'L': buf->b_ind_jump_label = n; break; |
| 7357 | case ':': buf->b_ind_case = n; break; |
| 7358 | case '=': buf->b_ind_case_code = n; break; |
| 7359 | case 'b': buf->b_ind_case_break = n; break; |
| 7360 | case 'p': buf->b_ind_param = n; break; |
| 7361 | case 't': buf->b_ind_func_type = n; break; |
| 7362 | case '/': buf->b_ind_comment = n; break; |
| 7363 | case 'c': buf->b_ind_in_comment = n; break; |
| 7364 | case 'C': buf->b_ind_in_comment2 = n; break; |
| 7365 | case 'i': buf->b_ind_cpp_baseclass = n; break; |
| 7366 | case '+': buf->b_ind_continuation = n; break; |
| 7367 | case '(': buf->b_ind_unclosed = n; break; |
| 7368 | case 'u': buf->b_ind_unclosed2 = n; break; |
| 7369 | case 'U': buf->b_ind_unclosed_noignore = n; break; |
| 7370 | case 'W': buf->b_ind_unclosed_wrapped = n; break; |
| 7371 | case 'w': buf->b_ind_unclosed_whiteok = n; break; |
| 7372 | case 'm': buf->b_ind_matching_paren = n; break; |
| 7373 | case 'M': buf->b_ind_paren_prev = n; break; |
| 7374 | case ')': buf->b_ind_maxparen = n; break; |
| 7375 | case '*': buf->b_ind_maxcomment = n; break; |
| 7376 | case 'g': buf->b_ind_scopedecl = n; break; |
| 7377 | case 'h': buf->b_ind_scopedecl_code = n; break; |
| 7378 | case 'j': buf->b_ind_java = n; break; |
| 7379 | case 'J': buf->b_ind_js = n; break; |
| 7380 | case 'l': buf->b_ind_keep_case_label = n; break; |
| 7381 | case '#': buf->b_ind_hash_comment = n; break; |
| 7382 | case 'N': buf->b_ind_cpp_namespace = n; break; |
| 7383 | case 'k': buf->b_ind_if_for_while = n; break; |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 7384 | case 'E': buf->b_ind_cpp_extern_c = n; break; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7385 | } |
| 7386 | if (*p == ',') |
| 7387 | ++p; |
| 7388 | } |
| 7389 | } |
| 7390 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7391 | /* |
| 7392 | * Return the desired indent for C code. |
| 7393 | * Return -1 if the indent should be left alone (inside a raw string). |
| 7394 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7395 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7396 | get_c_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7397 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7398 | pos_T cur_curpos; |
| 7399 | int amount; |
| 7400 | int scope_amount; |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 7401 | int cur_amount = MAXCOL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7402 | colnr_T col; |
| 7403 | char_u *theline; |
| 7404 | char_u *linecopy; |
| 7405 | pos_T *trypos; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7406 | pos_T *comment_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7407 | pos_T *tryposBrace = NULL; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7408 | pos_T tryposCopy; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7409 | pos_T our_paren_pos; |
| 7410 | char_u *start; |
| 7411 | int start_brace; |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 7412 | #define BRACE_IN_COL0 1 /* '{' is in column 0 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7413 | #define BRACE_AT_START 2 /* '{' is at start of line */ |
| 7414 | #define BRACE_AT_END 3 /* '{' is at end of line */ |
| 7415 | linenr_T ourscope; |
| 7416 | char_u *l; |
| 7417 | char_u *look; |
| 7418 | char_u terminated; |
| 7419 | int lookfor; |
| 7420 | #define LOOKFOR_INITIAL 0 |
| 7421 | #define LOOKFOR_IF 1 |
| 7422 | #define LOOKFOR_DO 2 |
| 7423 | #define LOOKFOR_CASE 3 |
| 7424 | #define LOOKFOR_ANY 4 |
| 7425 | #define LOOKFOR_TERM 5 |
| 7426 | #define LOOKFOR_UNTERM 6 |
| 7427 | #define LOOKFOR_SCOPEDECL 7 |
| 7428 | #define LOOKFOR_NOBREAK 8 |
| 7429 | #define LOOKFOR_CPP_BASECLASS 9 |
| 7430 | #define LOOKFOR_ENUM_OR_INIT 10 |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7431 | #define LOOKFOR_JS_KEY 11 |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 7432 | #define LOOKFOR_COMMA 12 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7433 | |
| 7434 | int whilelevel; |
| 7435 | linenr_T lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7436 | int n; |
| 7437 | int iscase; |
| 7438 | int lookfor_break; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 7439 | int lookfor_cpp_namespace = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7440 | int cont_amount = 0; /* amount for continuation line */ |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 7441 | int original_line_islabel; |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 7442 | int added_to_amount = 0; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7443 | int js_cur_has_key = 0; |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 7444 | linenr_T raw_string_start = 0; |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 7445 | cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } }; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7446 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7447 | /* make a copy, value is changed below */ |
| 7448 | int ind_continuation = curbuf->b_ind_continuation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7449 | |
| 7450 | /* remember where the cursor was when we started */ |
| 7451 | cur_curpos = curwin->w_cursor; |
| 7452 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7453 | /* if we are at line 1 zero indent is fine, right? */ |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 7454 | if (cur_curpos.lnum == 1) |
| 7455 | return 0; |
| 7456 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7457 | /* Get a copy of the current contents of the line. |
| 7458 | * This is required, because only the most recent line obtained with |
| 7459 | * ml_get is valid! */ |
| 7460 | linecopy = vim_strsave(ml_get(cur_curpos.lnum)); |
| 7461 | if (linecopy == NULL) |
| 7462 | return 0; |
| 7463 | |
| 7464 | /* |
| 7465 | * In insert mode and the cursor is on a ')' truncate the line at the |
| 7466 | * cursor position. We don't want to line up with the matching '(' when |
| 7467 | * inserting new stuff. |
| 7468 | * For unknown reasons the cursor might be past the end of the line, thus |
| 7469 | * check for that. |
| 7470 | */ |
| 7471 | if ((State & INSERT) |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 7472 | && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7473 | && linecopy[curwin->w_cursor.col] == ')') |
| 7474 | linecopy[curwin->w_cursor.col] = NUL; |
| 7475 | |
| 7476 | theline = skipwhite(linecopy); |
| 7477 | |
| 7478 | /* move the cursor to the start of the line */ |
| 7479 | |
| 7480 | curwin->w_cursor.col = 0; |
| 7481 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7482 | original_line_islabel = cin_islabel(); /* XXX */ |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 7483 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7484 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7485 | * If we are inside a raw string don't change the indent. |
| 7486 | * Ignore a raw string inside a comment. |
| 7487 | */ |
| 7488 | comment_pos = ind_find_start_comment(); |
| 7489 | if (comment_pos != NULL) |
| 7490 | { |
| 7491 | /* findmatchlimit() static pos is overwritten, make a copy */ |
| 7492 | tryposCopy = *comment_pos; |
| 7493 | comment_pos = &tryposCopy; |
| 7494 | } |
| 7495 | trypos = find_start_rawstring(curbuf->b_ind_maxcomment); |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 7496 | if (trypos != NULL && (comment_pos == NULL |
| 7497 | || LT_POS(*trypos, *comment_pos))) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7498 | { |
| 7499 | amount = -1; |
| 7500 | goto laterend; |
| 7501 | } |
| 7502 | |
| 7503 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7504 | * #defines and so on always go at the left when included in 'cinkeys'. |
| 7505 | */ |
| 7506 | if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE))) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7507 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7508 | amount = curbuf->b_ind_hash_comment; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7509 | goto theend; |
| 7510 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7511 | |
| 7512 | /* |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 7513 | * Is it a non-case label? Then that goes at the left margin too unless: |
| 7514 | * - JS flag is set. |
| 7515 | * - 'L' item has a positive value. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7516 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7517 | if (original_line_islabel && !curbuf->b_ind_js |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7518 | && curbuf->b_ind_jump_label < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7519 | { |
| 7520 | amount = 0; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7521 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7522 | } |
| 7523 | |
| 7524 | /* |
| 7525 | * If we're inside a "//" comment and there is a "//" comment in a |
| 7526 | * previous line, lineup with that one. |
| 7527 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7528 | if (cin_islinecomment(theline) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7529 | && (trypos = find_line_comment()) != NULL) /* XXX */ |
| 7530 | { |
| 7531 | /* find how indented the line beginning the comment is */ |
| 7532 | getvcol(curwin, trypos, &col, NULL, NULL); |
| 7533 | amount = col; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7534 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7535 | } |
| 7536 | |
| 7537 | /* |
| 7538 | * If we're inside a comment and not looking at the start of the |
| 7539 | * comment, try using the 'comments' option. |
| 7540 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7541 | if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7542 | { |
| 7543 | int lead_start_len = 2; |
| 7544 | int lead_middle_len = 1; |
| 7545 | char_u lead_start[COM_MAX_LEN]; /* start-comment string */ |
| 7546 | char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ |
| 7547 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 7548 | char_u *p; |
| 7549 | int start_align = 0; |
| 7550 | int start_off = 0; |
| 7551 | int done = FALSE; |
| 7552 | |
| 7553 | /* find how indented the line beginning the comment is */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7554 | getvcol(curwin, comment_pos, &col, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7555 | amount = col; |
Bram Moolenaar | 4aa9742 | 2011-04-11 14:27:38 +0200 | [diff] [blame] | 7556 | *lead_start = NUL; |
| 7557 | *lead_middle = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7558 | |
| 7559 | p = curbuf->b_p_com; |
| 7560 | while (*p != NUL) |
| 7561 | { |
| 7562 | int align = 0; |
| 7563 | int off = 0; |
| 7564 | int what = 0; |
| 7565 | |
| 7566 | while (*p != NUL && *p != ':') |
| 7567 | { |
| 7568 | if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE) |
| 7569 | what = *p++; |
| 7570 | else if (*p == COM_LEFT || *p == COM_RIGHT) |
| 7571 | align = *p++; |
| 7572 | else if (VIM_ISDIGIT(*p) || *p == '-') |
| 7573 | off = getdigits(&p); |
| 7574 | else |
| 7575 | ++p; |
| 7576 | } |
| 7577 | |
| 7578 | if (*p == ':') |
| 7579 | ++p; |
| 7580 | (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 7581 | if (what == COM_START) |
| 7582 | { |
| 7583 | STRCPY(lead_start, lead_end); |
| 7584 | lead_start_len = (int)STRLEN(lead_start); |
| 7585 | start_off = off; |
| 7586 | start_align = align; |
| 7587 | } |
| 7588 | else if (what == COM_MIDDLE) |
| 7589 | { |
| 7590 | STRCPY(lead_middle, lead_end); |
| 7591 | lead_middle_len = (int)STRLEN(lead_middle); |
| 7592 | } |
| 7593 | else if (what == COM_END) |
| 7594 | { |
| 7595 | /* If our line starts with the middle comment string, line it |
| 7596 | * up with the comment opener per the 'comments' option. */ |
| 7597 | if (STRNCMP(theline, lead_middle, lead_middle_len) == 0 |
| 7598 | && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0) |
| 7599 | { |
| 7600 | done = TRUE; |
| 7601 | if (curwin->w_cursor.lnum > 1) |
| 7602 | { |
| 7603 | /* If the start comment string matches in the previous |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 7604 | * line, use the indent of that line plus offset. If |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7605 | * the middle comment string matches in the previous |
| 7606 | * line, use the indent of that line. XXX */ |
| 7607 | look = skipwhite(ml_get(curwin->w_cursor.lnum - 1)); |
| 7608 | if (STRNCMP(look, lead_start, lead_start_len) == 0) |
| 7609 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 7610 | else if (STRNCMP(look, lead_middle, |
| 7611 | lead_middle_len) == 0) |
| 7612 | { |
| 7613 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 7614 | break; |
| 7615 | } |
| 7616 | /* If the start comment string doesn't match with the |
| 7617 | * start of the comment, skip this entry. XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7618 | else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7619 | lead_start, lead_start_len) != 0) |
| 7620 | continue; |
| 7621 | } |
| 7622 | if (start_off != 0) |
| 7623 | amount += start_off; |
| 7624 | else if (start_align == COM_RIGHT) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 7625 | amount += vim_strsize(lead_start) |
| 7626 | - vim_strsize(lead_middle); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7627 | break; |
| 7628 | } |
| 7629 | |
| 7630 | /* If our line starts with the end comment string, line it up |
| 7631 | * with the middle comment */ |
| 7632 | if (STRNCMP(theline, lead_middle, lead_middle_len) != 0 |
| 7633 | && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0) |
| 7634 | { |
| 7635 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 7636 | /* XXX */ |
| 7637 | if (off != 0) |
| 7638 | amount += off; |
| 7639 | else if (align == COM_RIGHT) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 7640 | amount += vim_strsize(lead_start) |
| 7641 | - vim_strsize(lead_middle); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7642 | done = TRUE; |
| 7643 | break; |
| 7644 | } |
| 7645 | } |
| 7646 | } |
| 7647 | |
| 7648 | /* If our line starts with an asterisk, line up with the |
| 7649 | * asterisk in the comment opener; otherwise, line up |
| 7650 | * with the first character of the comment text. |
| 7651 | */ |
| 7652 | if (done) |
| 7653 | ; |
| 7654 | else if (theline[0] == '*') |
| 7655 | amount += 1; |
| 7656 | else |
| 7657 | { |
| 7658 | /* |
| 7659 | * If we are more than one line away from the comment opener, take |
| 7660 | * the indent of the previous non-empty line. If 'cino' has "CO" |
| 7661 | * and we are just below the comment opener and there are any |
| 7662 | * white characters after it line up with the text after it; |
| 7663 | * otherwise, add the amount specified by "c" in 'cino' |
| 7664 | */ |
| 7665 | amount = -1; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7666 | for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7667 | { |
| 7668 | if (linewhite(lnum)) /* skip blank lines */ |
| 7669 | continue; |
| 7670 | amount = get_indent_lnum(lnum); /* XXX */ |
| 7671 | break; |
| 7672 | } |
| 7673 | if (amount == -1) /* use the comment opener */ |
| 7674 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7675 | if (!curbuf->b_ind_in_comment2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7676 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7677 | start = ml_get(comment_pos->lnum); |
| 7678 | look = start + comment_pos->col + 2; /* skip / and * */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7679 | if (*look != NUL) /* if something after it */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7680 | comment_pos->col = (colnr_T)(skipwhite(look) - start); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7681 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7682 | getvcol(curwin, comment_pos, &col, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7683 | amount = col; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7684 | if (curbuf->b_ind_in_comment2 || *look == NUL) |
| 7685 | amount += curbuf->b_ind_in_comment; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7686 | } |
| 7687 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7688 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7689 | } |
| 7690 | |
| 7691 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7692 | * Are we looking at a ']' that has a match? |
| 7693 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7694 | if (*skipwhite(theline) == ']' |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7695 | && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL) |
| 7696 | { |
| 7697 | /* align with the line containing the '['. */ |
| 7698 | amount = get_indent_lnum(trypos->lnum); |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7699 | goto theend; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7700 | } |
| 7701 | |
| 7702 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7703 | * Are we inside parentheses or braces? |
| 7704 | */ /* XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7705 | if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7706 | && curbuf->b_ind_java == 0) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7707 | || (tryposBrace = find_start_brace()) != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7708 | || trypos != NULL) |
| 7709 | { |
| 7710 | if (trypos != NULL && tryposBrace != NULL) |
| 7711 | { |
| 7712 | /* Both an unmatched '(' and '{' is found. Use the one which is |
| 7713 | * closer to the current cursor position, set the other to NULL. */ |
| 7714 | if (trypos->lnum != tryposBrace->lnum |
| 7715 | ? trypos->lnum < tryposBrace->lnum |
| 7716 | : trypos->col < tryposBrace->col) |
| 7717 | trypos = NULL; |
| 7718 | else |
| 7719 | tryposBrace = NULL; |
| 7720 | } |
| 7721 | |
| 7722 | if (trypos != NULL) |
| 7723 | { |
| 7724 | /* |
| 7725 | * If the matching paren is more than one line away, use the indent of |
| 7726 | * a previous non-empty line that matches the same paren. |
| 7727 | */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7728 | if (theline[0] == ')' && curbuf->b_ind_paren_prev) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7729 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7730 | /* Line up with the start of the matching paren line. */ |
| 7731 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */ |
| 7732 | } |
| 7733 | else |
| 7734 | { |
| 7735 | amount = -1; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7736 | our_paren_pos = *trypos; |
| 7737 | for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7738 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7739 | l = skipwhite(ml_get(lnum)); |
| 7740 | if (cin_nocode(l)) /* skip comment lines */ |
| 7741 | continue; |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 7742 | if (cin_ispreproc_cont(&l, &lnum, &amount)) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7743 | continue; /* ignore #define, #if, etc. */ |
| 7744 | curwin->w_cursor.lnum = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7745 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7746 | /* Skip a comment or raw string. XXX */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 7747 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7748 | { |
| 7749 | lnum = trypos->lnum + 1; |
| 7750 | continue; |
| 7751 | } |
| 7752 | |
| 7753 | /* XXX */ |
| 7754 | if ((trypos = find_match_paren( |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7755 | corr_ind_maxparen(&cur_curpos))) != NULL |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7756 | && trypos->lnum == our_paren_pos.lnum |
| 7757 | && trypos->col == our_paren_pos.col) |
| 7758 | { |
| 7759 | amount = get_indent_lnum(lnum); /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7760 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7761 | if (theline[0] == ')') |
| 7762 | { |
| 7763 | if (our_paren_pos.lnum != lnum |
| 7764 | && cur_amount > amount) |
| 7765 | cur_amount = amount; |
| 7766 | amount = -1; |
| 7767 | } |
| 7768 | break; |
| 7769 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7770 | } |
| 7771 | } |
| 7772 | |
| 7773 | /* |
| 7774 | * Line up with line where the matching paren is. XXX |
| 7775 | * If the line starts with a '(' or the indent for unclosed |
| 7776 | * parentheses is zero, line up with the unclosed parentheses. |
| 7777 | */ |
| 7778 | if (amount == -1) |
| 7779 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7780 | int ignore_paren_col = 0; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7781 | int is_if_for_while = 0; |
| 7782 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7783 | if (curbuf->b_ind_if_for_while) |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7784 | { |
| 7785 | /* Look for the outermost opening parenthesis on this line |
| 7786 | * and check whether it belongs to an "if", "for" or "while". */ |
| 7787 | |
| 7788 | pos_T cursor_save = curwin->w_cursor; |
| 7789 | pos_T outermost; |
| 7790 | char_u *line; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7791 | |
| 7792 | trypos = &our_paren_pos; |
| 7793 | do { |
| 7794 | outermost = *trypos; |
| 7795 | curwin->w_cursor.lnum = outermost.lnum; |
| 7796 | curwin->w_cursor.col = outermost.col; |
| 7797 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7798 | trypos = find_match_paren(curbuf->b_ind_maxparen); |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7799 | } while (trypos && trypos->lnum == outermost.lnum); |
| 7800 | |
| 7801 | curwin->w_cursor = cursor_save; |
| 7802 | |
| 7803 | line = ml_get(outermost.lnum); |
| 7804 | |
| 7805 | is_if_for_while = |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 7806 | cin_is_if_for_while_before_offset(line, &outermost.col); |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7807 | } |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7808 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7809 | amount = skip_label(our_paren_pos.lnum, &look); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7810 | look = skipwhite(look); |
| 7811 | if (*look == '(') |
| 7812 | { |
| 7813 | linenr_T save_lnum = curwin->w_cursor.lnum; |
| 7814 | char_u *line; |
| 7815 | int look_col; |
| 7816 | |
| 7817 | /* Ignore a '(' in front of the line that has a match before |
| 7818 | * our matching '('. */ |
| 7819 | curwin->w_cursor.lnum = our_paren_pos.lnum; |
| 7820 | line = ml_get_curline(); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7821 | look_col = (int)(look - line); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7822 | curwin->w_cursor.col = look_col + 1; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7823 | if ((trypos = findmatchlimit(NULL, ')', 0, |
| 7824 | curbuf->b_ind_maxparen)) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7825 | != NULL |
| 7826 | && trypos->lnum == our_paren_pos.lnum |
| 7827 | && trypos->col < our_paren_pos.col) |
| 7828 | ignore_paren_col = trypos->col + 1; |
| 7829 | |
| 7830 | curwin->w_cursor.lnum = save_lnum; |
| 7831 | look = ml_get(our_paren_pos.lnum) + look_col; |
| 7832 | } |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7833 | if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0 |
| 7834 | && is_if_for_while == 0) |
| 7835 | || (!curbuf->b_ind_unclosed_noignore && *look == '(' |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7836 | && ignore_paren_col == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7837 | { |
| 7838 | /* |
| 7839 | * If we're looking at a close paren, line up right there; |
| 7840 | * otherwise, line up with the next (non-white) character. |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7841 | * When b_ind_unclosed_wrapped is set and the matching paren is |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7842 | * the last nonwhite character of the line, use either the |
| 7843 | * indent of the current line or the indentation of the next |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7844 | * outer paren and add b_ind_unclosed_wrapped (for very long |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7845 | * lines). |
| 7846 | */ |
| 7847 | if (theline[0] != ')') |
| 7848 | { |
| 7849 | cur_amount = MAXCOL; |
| 7850 | l = ml_get(our_paren_pos.lnum); |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7851 | if (curbuf->b_ind_unclosed_wrapped |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7852 | && cin_ends_in(l, (char_u *)"(", NULL)) |
| 7853 | { |
| 7854 | /* look for opening unmatched paren, indent one level |
| 7855 | * for each additional level */ |
| 7856 | n = 1; |
| 7857 | for (col = 0; col < our_paren_pos.col; ++col) |
| 7858 | { |
| 7859 | switch (l[col]) |
| 7860 | { |
| 7861 | case '(': |
| 7862 | case '{': ++n; |
| 7863 | break; |
| 7864 | |
| 7865 | case ')': |
| 7866 | case '}': if (n > 1) |
| 7867 | --n; |
| 7868 | break; |
| 7869 | } |
| 7870 | } |
| 7871 | |
| 7872 | our_paren_pos.col = 0; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7873 | amount += n * curbuf->b_ind_unclosed_wrapped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7874 | } |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7875 | else if (curbuf->b_ind_unclosed_whiteok) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7876 | our_paren_pos.col++; |
| 7877 | else |
| 7878 | { |
| 7879 | col = our_paren_pos.col + 1; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 7880 | while (VIM_ISWHITE(l[col])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7881 | col++; |
| 7882 | if (l[col] != NUL) /* In case of trailing space */ |
| 7883 | our_paren_pos.col = col; |
| 7884 | else |
| 7885 | our_paren_pos.col++; |
| 7886 | } |
| 7887 | } |
| 7888 | |
| 7889 | /* |
| 7890 | * Find how indented the paren is, or the character after it |
| 7891 | * if we did the above "if". |
| 7892 | */ |
| 7893 | if (our_paren_pos.col > 0) |
| 7894 | { |
| 7895 | getvcol(curwin, &our_paren_pos, &col, NULL, NULL); |
| 7896 | if (cur_amount > (int)col) |
| 7897 | cur_amount = col; |
| 7898 | } |
| 7899 | } |
| 7900 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7901 | if (theline[0] == ')' && curbuf->b_ind_matching_paren) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7902 | { |
| 7903 | /* Line up with the start of the matching paren line. */ |
| 7904 | } |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7905 | else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0) |
| 7906 | || (!curbuf->b_ind_unclosed_noignore |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7907 | && *look == '(' && ignore_paren_col == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7908 | { |
| 7909 | if (cur_amount != MAXCOL) |
| 7910 | amount = cur_amount; |
| 7911 | } |
| 7912 | else |
| 7913 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7914 | /* Add b_ind_unclosed2 for each '(' before our matching one, |
| 7915 | * but ignore (void) before the line (ignore_paren_col). */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7916 | col = our_paren_pos.col; |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 7917 | while ((int)our_paren_pos.col > ignore_paren_col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7918 | { |
| 7919 | --our_paren_pos.col; |
| 7920 | switch (*ml_get_pos(&our_paren_pos)) |
| 7921 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7922 | case '(': amount += curbuf->b_ind_unclosed2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7923 | col = our_paren_pos.col; |
| 7924 | break; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7925 | case ')': amount -= curbuf->b_ind_unclosed2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7926 | col = MAXCOL; |
| 7927 | break; |
| 7928 | } |
| 7929 | } |
| 7930 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7931 | /* Use b_ind_unclosed once, when the first '(' is not inside |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7932 | * braces */ |
| 7933 | if (col == MAXCOL) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7934 | amount += curbuf->b_ind_unclosed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7935 | else |
| 7936 | { |
| 7937 | curwin->w_cursor.lnum = our_paren_pos.lnum; |
| 7938 | curwin->w_cursor.col = col; |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 7939 | if (find_match_paren_after_brace(curbuf->b_ind_maxparen) |
| 7940 | != NULL) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7941 | amount += curbuf->b_ind_unclosed2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7942 | else |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7943 | { |
| 7944 | if (is_if_for_while) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7945 | amount += curbuf->b_ind_if_for_while; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7946 | else |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7947 | amount += curbuf->b_ind_unclosed; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7948 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7949 | } |
| 7950 | /* |
| 7951 | * For a line starting with ')' use the minimum of the two |
| 7952 | * positions, to avoid giving it more indent than the previous |
| 7953 | * lines: |
| 7954 | * func_long_name( if (x |
| 7955 | * arg && yy |
| 7956 | * ) ^ not here ) ^ not here |
| 7957 | */ |
| 7958 | if (cur_amount < amount) |
| 7959 | amount = cur_amount; |
| 7960 | } |
| 7961 | } |
| 7962 | |
| 7963 | /* add extra indent for a comment */ |
| 7964 | if (cin_iscomment(theline)) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7965 | amount += curbuf->b_ind_comment; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7966 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7967 | else |
| 7968 | { |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7969 | /* |
| 7970 | * We are inside braces, there is a { before this line at the position |
| 7971 | * stored in tryposBrace. |
Bram Moolenaar | 04d17ae | 2014-08-06 17:44:14 +0200 | [diff] [blame] | 7972 | * Make a copy of tryposBrace, it may point to pos_copy inside |
| 7973 | * find_start_brace(), which may be changed somewhere. |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7974 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7975 | tryposCopy = *tryposBrace; |
| 7976 | tryposBrace = &tryposCopy; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7977 | trypos = tryposBrace; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7978 | ourscope = trypos->lnum; |
| 7979 | start = ml_get(ourscope); |
| 7980 | |
| 7981 | /* |
| 7982 | * Now figure out how indented the line is in general. |
| 7983 | * If the brace was at the start of the line, we use that; |
| 7984 | * otherwise, check out the indentation of the line as |
| 7985 | * a whole and then add the "imaginary indent" to that. |
| 7986 | */ |
| 7987 | look = skipwhite(start); |
| 7988 | if (*look == '{') |
| 7989 | { |
| 7990 | getvcol(curwin, trypos, &col, NULL, NULL); |
| 7991 | amount = col; |
| 7992 | if (*start == '{') |
| 7993 | start_brace = BRACE_IN_COL0; |
| 7994 | else |
| 7995 | start_brace = BRACE_AT_START; |
| 7996 | } |
| 7997 | else |
| 7998 | { |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7999 | /* That opening brace might have been on a continuation |
| 8000 | * line. if so, find the start of the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8001 | curwin->w_cursor.lnum = ourscope; |
| 8002 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8003 | /* Position the cursor over the rightmost paren, so that |
| 8004 | * matching it will take us back to the start of the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8005 | lnum = ourscope; |
| 8006 | if (find_last_paren(start, '(', ')') |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8007 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) |
| 8008 | != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8009 | lnum = trypos->lnum; |
| 8010 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8011 | /* It could have been something like |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8012 | * case 1: if (asdf && |
| 8013 | * ldfd) { |
| 8014 | * } |
| 8015 | */ |
Bram Moolenaar | e7eb789 | 2014-07-02 17:02:36 +0200 | [diff] [blame] | 8016 | if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label) |
| 8017 | && cin_iscase(skipwhite(ml_get_curline()), FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8018 | amount = get_indent(); |
Bram Moolenaar | e7eb789 | 2014-07-02 17:02:36 +0200 | [diff] [blame] | 8019 | else if (curbuf->b_ind_js) |
| 8020 | amount = get_indent_lnum(lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8021 | else |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8022 | amount = skip_label(lnum, &l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8023 | |
| 8024 | start_brace = BRACE_AT_END; |
| 8025 | } |
| 8026 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8027 | /* For Javascript check if the line starts with "key:". */ |
| 8028 | if (curbuf->b_ind_js) |
| 8029 | js_cur_has_key = cin_has_js_key(theline); |
| 8030 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8031 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8032 | * If we're looking at a closing brace, that's where |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8033 | * we want to be. otherwise, add the amount of room |
| 8034 | * that an indent is supposed to be. |
| 8035 | */ |
| 8036 | if (theline[0] == '}') |
| 8037 | { |
| 8038 | /* |
| 8039 | * they may want closing braces to line up with something |
| 8040 | * other than the open brace. indulge them, if so. |
| 8041 | */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8042 | amount += curbuf->b_ind_close_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8043 | } |
| 8044 | else |
| 8045 | { |
| 8046 | /* |
| 8047 | * If we're looking at an "else", try to find an "if" |
| 8048 | * to match it with. |
| 8049 | * If we're looking at a "while", try to find a "do" |
| 8050 | * to match it with. |
| 8051 | */ |
| 8052 | lookfor = LOOKFOR_INITIAL; |
| 8053 | if (cin_iselse(theline)) |
| 8054 | lookfor = LOOKFOR_IF; |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8055 | else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8056 | lookfor = LOOKFOR_DO; |
| 8057 | if (lookfor != LOOKFOR_INITIAL) |
| 8058 | { |
| 8059 | curwin->w_cursor.lnum = cur_curpos.lnum; |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8060 | if (find_match(lookfor, ourscope) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8061 | { |
| 8062 | amount = get_indent(); /* XXX */ |
| 8063 | goto theend; |
| 8064 | } |
| 8065 | } |
| 8066 | |
| 8067 | /* |
| 8068 | * We get here if we are not on an "while-of-do" or "else" (or |
| 8069 | * failed to find a matching "if"). |
| 8070 | * Search backwards for something to line up with. |
| 8071 | * First set amount for when we don't find anything. |
| 8072 | */ |
| 8073 | |
| 8074 | /* |
| 8075 | * if the '{' is _really_ at the left margin, use the imaginary |
| 8076 | * location of a left-margin brace. Otherwise, correct the |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8077 | * location for b_ind_open_extra. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8078 | */ |
| 8079 | |
| 8080 | if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */ |
| 8081 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8082 | amount = curbuf->b_ind_open_left_imag; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 8083 | lookfor_cpp_namespace = TRUE; |
| 8084 | } |
| 8085 | else if (start_brace == BRACE_AT_START && |
| 8086 | lookfor_cpp_namespace) /* '{' is at start */ |
| 8087 | { |
| 8088 | |
| 8089 | lookfor_cpp_namespace = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8090 | } |
| 8091 | else |
| 8092 | { |
| 8093 | if (start_brace == BRACE_AT_END) /* '{' is at end of line */ |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 8094 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8095 | amount += curbuf->b_ind_open_imag; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 8096 | |
| 8097 | l = skipwhite(ml_get_curline()); |
| 8098 | if (cin_is_cpp_namespace(l)) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8099 | amount += curbuf->b_ind_cpp_namespace; |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 8100 | else if (cin_is_cpp_extern_c(l)) |
| 8101 | amount += curbuf->b_ind_cpp_extern_c; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 8102 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8103 | else |
| 8104 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8105 | /* Compensate for adding b_ind_open_extra later. */ |
| 8106 | amount -= curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8107 | if (amount < 0) |
| 8108 | amount = 0; |
| 8109 | } |
| 8110 | } |
| 8111 | |
| 8112 | lookfor_break = FALSE; |
| 8113 | |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8114 | if (cin_iscase(theline, FALSE)) /* it's a switch() label */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8115 | { |
| 8116 | lookfor = LOOKFOR_CASE; /* find a previous switch() label */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8117 | amount += curbuf->b_ind_case; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8118 | } |
| 8119 | else if (cin_isscopedecl(theline)) /* private:, ... */ |
| 8120 | { |
| 8121 | lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8122 | amount += curbuf->b_ind_scopedecl; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8123 | } |
| 8124 | else |
| 8125 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8126 | if (curbuf->b_ind_case_break && cin_isbreak(theline)) |
| 8127 | /* break; ... */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8128 | lookfor_break = TRUE; |
| 8129 | |
| 8130 | lookfor = LOOKFOR_INITIAL; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8131 | /* b_ind_level from start of block */ |
| 8132 | amount += curbuf->b_ind_level; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8133 | } |
| 8134 | scope_amount = amount; |
| 8135 | whilelevel = 0; |
| 8136 | |
| 8137 | /* |
| 8138 | * Search backwards. If we find something we recognize, line up |
| 8139 | * with that. |
| 8140 | * |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8141 | * If we're looking at an open brace, indent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8142 | * the usual amount relative to the conditional |
| 8143 | * that opens the block. |
| 8144 | */ |
| 8145 | curwin->w_cursor = cur_curpos; |
| 8146 | for (;;) |
| 8147 | { |
| 8148 | curwin->w_cursor.lnum--; |
| 8149 | curwin->w_cursor.col = 0; |
| 8150 | |
| 8151 | /* |
| 8152 | * If we went all the way back to the start of our scope, line |
| 8153 | * up with it. |
| 8154 | */ |
| 8155 | if (curwin->w_cursor.lnum <= ourscope) |
| 8156 | { |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8157 | /* We reached end of scope: |
| 8158 | * If looking for a enum or structure initialization |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8159 | * go further back: |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8160 | * If it is an initializer (enum xxx or xxx =), then |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8161 | * don't add ind_continuation, otherwise it is a variable |
| 8162 | * declaration: |
| 8163 | * int x, |
| 8164 | * here; <-- add ind_continuation |
| 8165 | */ |
| 8166 | if (lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8167 | { |
| 8168 | if (curwin->w_cursor.lnum == 0 |
| 8169 | || curwin->w_cursor.lnum |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8170 | < ourscope - curbuf->b_ind_maxparen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8171 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8172 | /* nothing found (abuse curbuf->b_ind_maxparen as |
| 8173 | * limit) assume terminated line (i.e. a variable |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8174 | * initialization) */ |
| 8175 | if (cont_amount > 0) |
| 8176 | amount = cont_amount; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8177 | else if (!curbuf->b_ind_js) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8178 | amount += ind_continuation; |
| 8179 | break; |
| 8180 | } |
| 8181 | |
| 8182 | l = ml_get_curline(); |
| 8183 | |
| 8184 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8185 | * If we're in a comment or raw string now, skip to |
| 8186 | * the start of it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8187 | */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 8188 | trypos = ind_find_start_CORS(NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8189 | if (trypos != NULL) |
| 8190 | { |
| 8191 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8192 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8193 | continue; |
| 8194 | } |
| 8195 | |
| 8196 | /* |
| 8197 | * Skip preprocessor directives and blank lines. |
| 8198 | */ |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8199 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, |
| 8200 | &amount)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8201 | continue; |
| 8202 | |
| 8203 | if (cin_nocode(l)) |
| 8204 | continue; |
| 8205 | |
| 8206 | terminated = cin_isterminated(l, FALSE, TRUE); |
| 8207 | |
| 8208 | /* |
| 8209 | * If we are at top level and the line looks like a |
| 8210 | * function declaration, we are done |
| 8211 | * (it's a variable declaration). |
| 8212 | */ |
| 8213 | if (start_brace != BRACE_IN_COL0 |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8214 | || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8215 | { |
| 8216 | /* if the line is terminated with another ',' |
| 8217 | * it is a continued variable initialization. |
| 8218 | * don't add extra indent. |
| 8219 | * TODO: does not work, if a function |
| 8220 | * declaration is split over multiple lines: |
| 8221 | * cin_isfuncdecl returns FALSE then. |
| 8222 | */ |
| 8223 | if (terminated == ',') |
| 8224 | break; |
| 8225 | |
| 8226 | /* if it es a enum declaration or an assignment, |
| 8227 | * we are done. |
| 8228 | */ |
| 8229 | if (terminated != ';' && cin_isinit()) |
| 8230 | break; |
| 8231 | |
| 8232 | /* nothing useful found */ |
| 8233 | if (terminated == 0 || terminated == '{') |
| 8234 | continue; |
| 8235 | } |
| 8236 | |
| 8237 | if (terminated != ';') |
| 8238 | { |
| 8239 | /* Skip parens and braces. Position the cursor |
| 8240 | * over the rightmost paren, so that matching it |
| 8241 | * will take us back to the start of the line. |
| 8242 | */ /* XXX */ |
| 8243 | trypos = NULL; |
| 8244 | if (find_last_paren(l, '(', ')')) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8245 | trypos = find_match_paren( |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8246 | curbuf->b_ind_maxparen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8247 | |
| 8248 | if (trypos == NULL && find_last_paren(l, '{', '}')) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8249 | trypos = find_start_brace(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8250 | |
| 8251 | if (trypos != NULL) |
| 8252 | { |
| 8253 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8254 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8255 | continue; |
| 8256 | } |
| 8257 | } |
| 8258 | |
| 8259 | /* it's a variable declaration, add indentation |
| 8260 | * like in |
| 8261 | * int a, |
| 8262 | * b; |
| 8263 | */ |
| 8264 | if (cont_amount > 0) |
| 8265 | amount = cont_amount; |
| 8266 | else |
| 8267 | amount += ind_continuation; |
| 8268 | } |
| 8269 | else if (lookfor == LOOKFOR_UNTERM) |
| 8270 | { |
| 8271 | if (cont_amount > 0) |
| 8272 | amount = cont_amount; |
| 8273 | else |
| 8274 | amount += ind_continuation; |
| 8275 | } |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8276 | else |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 8277 | { |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8278 | if (lookfor != LOOKFOR_TERM |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8279 | && lookfor != LOOKFOR_CPP_BASECLASS |
| 8280 | && lookfor != LOOKFOR_COMMA) |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8281 | { |
| 8282 | amount = scope_amount; |
| 8283 | if (theline[0] == '{') |
| 8284 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8285 | amount += curbuf->b_ind_open_extra; |
| 8286 | added_to_amount = curbuf->b_ind_open_extra; |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8287 | } |
| 8288 | } |
| 8289 | |
| 8290 | if (lookfor_cpp_namespace) |
| 8291 | { |
| 8292 | /* |
| 8293 | * Looking for C++ namespace, need to look further |
| 8294 | * back. |
| 8295 | */ |
| 8296 | if (curwin->w_cursor.lnum == ourscope) |
| 8297 | continue; |
| 8298 | |
| 8299 | if (curwin->w_cursor.lnum == 0 |
| 8300 | || curwin->w_cursor.lnum |
| 8301 | < ourscope - FIND_NAMESPACE_LIM) |
| 8302 | break; |
| 8303 | |
| 8304 | l = ml_get_curline(); |
| 8305 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8306 | /* If we're in a comment or raw string now, skip |
| 8307 | * to the start of it. */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 8308 | trypos = ind_find_start_CORS(NULL); |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8309 | if (trypos != NULL) |
| 8310 | { |
| 8311 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 8312 | curwin->w_cursor.col = 0; |
| 8313 | continue; |
| 8314 | } |
| 8315 | |
| 8316 | /* Skip preprocessor directives and blank lines. */ |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8317 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, |
| 8318 | &amount)) |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8319 | continue; |
| 8320 | |
| 8321 | /* Finally the actual check for "namespace". */ |
| 8322 | if (cin_is_cpp_namespace(l)) |
| 8323 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8324 | amount += curbuf->b_ind_cpp_namespace |
| 8325 | - added_to_amount; |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8326 | break; |
| 8327 | } |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 8328 | else if (cin_is_cpp_extern_c(l)) |
| 8329 | { |
| 8330 | amount += curbuf->b_ind_cpp_extern_c |
| 8331 | - added_to_amount; |
| 8332 | break; |
| 8333 | } |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8334 | |
| 8335 | if (cin_nocode(l)) |
| 8336 | continue; |
| 8337 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8338 | } |
| 8339 | break; |
| 8340 | } |
| 8341 | |
| 8342 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8343 | * If we're in a comment or raw string now, skip to the start |
| 8344 | * of it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8345 | */ /* XXX */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 8346 | if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8347 | { |
| 8348 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8349 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8350 | continue; |
| 8351 | } |
| 8352 | |
| 8353 | l = ml_get_curline(); |
| 8354 | |
| 8355 | /* |
| 8356 | * If this is a switch() label, may line up relative to that. |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8357 | * If this is a C++ scope declaration, do the same. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8358 | */ |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8359 | iscase = cin_iscase(l, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8360 | if (iscase || cin_isscopedecl(l)) |
| 8361 | { |
| 8362 | /* we are only looking for cpp base class |
| 8363 | * declaration/initialization any longer */ |
| 8364 | if (lookfor == LOOKFOR_CPP_BASECLASS) |
| 8365 | break; |
| 8366 | |
| 8367 | /* When looking for a "do" we are not interested in |
| 8368 | * labels. */ |
| 8369 | if (whilelevel > 0) |
| 8370 | continue; |
| 8371 | |
| 8372 | /* |
| 8373 | * case xx: |
| 8374 | * c = 99 + <- this indent plus continuation |
| 8375 | *-> here; |
| 8376 | */ |
| 8377 | if (lookfor == LOOKFOR_UNTERM |
| 8378 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8379 | { |
| 8380 | if (cont_amount > 0) |
| 8381 | amount = cont_amount; |
| 8382 | else |
| 8383 | amount += ind_continuation; |
| 8384 | break; |
| 8385 | } |
| 8386 | |
| 8387 | /* |
| 8388 | * case xx: <- line up with this case |
| 8389 | * x = 333; |
| 8390 | * case yy: |
| 8391 | */ |
| 8392 | if ( (iscase && lookfor == LOOKFOR_CASE) |
| 8393 | || (iscase && lookfor_break) |
| 8394 | || (!iscase && lookfor == LOOKFOR_SCOPEDECL)) |
| 8395 | { |
| 8396 | /* |
| 8397 | * Check that this case label is not for another |
| 8398 | * switch() |
| 8399 | */ /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8400 | if ((trypos = find_start_brace()) == NULL |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8401 | || trypos->lnum == ourscope) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8402 | { |
| 8403 | amount = get_indent(); /* XXX */ |
| 8404 | break; |
| 8405 | } |
| 8406 | continue; |
| 8407 | } |
| 8408 | |
| 8409 | n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */ |
| 8410 | |
| 8411 | /* |
| 8412 | * case xx: if (cond) <- line up with this if |
| 8413 | * y = y + 1; |
| 8414 | * -> s = 99; |
| 8415 | * |
| 8416 | * case xx: |
| 8417 | * if (cond) <- line up with this line |
| 8418 | * y = y + 1; |
| 8419 | * -> s = 99; |
| 8420 | */ |
| 8421 | if (lookfor == LOOKFOR_TERM) |
| 8422 | { |
| 8423 | if (n) |
| 8424 | amount = n; |
| 8425 | |
| 8426 | if (!lookfor_break) |
| 8427 | break; |
| 8428 | } |
| 8429 | |
| 8430 | /* |
| 8431 | * case xx: x = x + 1; <- line up with this x |
| 8432 | * -> y = y + 1; |
| 8433 | * |
| 8434 | * case xx: if (cond) <- line up with this if |
| 8435 | * -> y = y + 1; |
| 8436 | */ |
| 8437 | if (n) |
| 8438 | { |
| 8439 | amount = n; |
| 8440 | l = after_label(ml_get_curline()); |
| 8441 | if (l != NULL && cin_is_cinword(l)) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 8442 | { |
| 8443 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8444 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 8445 | else |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8446 | amount += curbuf->b_ind_level |
| 8447 | + curbuf->b_ind_no_brace; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 8448 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8449 | break; |
| 8450 | } |
| 8451 | |
| 8452 | /* |
| 8453 | * Try to get the indent of a statement before the switch |
| 8454 | * label. If nothing is found, line up relative to the |
| 8455 | * switch label. |
| 8456 | * break; <- may line up with this line |
| 8457 | * case xx: |
| 8458 | * -> y = 1; |
| 8459 | */ |
| 8460 | scope_amount = get_indent() + (iscase /* XXX */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8461 | ? curbuf->b_ind_case_code |
| 8462 | : curbuf->b_ind_scopedecl_code); |
| 8463 | lookfor = curbuf->b_ind_case_break |
| 8464 | ? LOOKFOR_NOBREAK : LOOKFOR_ANY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8465 | continue; |
| 8466 | } |
| 8467 | |
| 8468 | /* |
| 8469 | * Looking for a switch() label or C++ scope declaration, |
| 8470 | * ignore other lines, skip {}-blocks. |
| 8471 | */ |
| 8472 | if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL) |
| 8473 | { |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8474 | if (find_last_paren(l, '{', '}') |
| 8475 | && (trypos = find_start_brace()) != NULL) |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8476 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8477 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8478 | curwin->w_cursor.col = 0; |
| 8479 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8480 | continue; |
| 8481 | } |
| 8482 | |
| 8483 | /* |
| 8484 | * Ignore jump labels with nothing after them. |
| 8485 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8486 | if (!curbuf->b_ind_js && cin_islabel()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8487 | { |
| 8488 | l = after_label(ml_get_curline()); |
| 8489 | if (l == NULL || cin_nocode(l)) |
| 8490 | continue; |
| 8491 | } |
| 8492 | |
| 8493 | /* |
| 8494 | * Ignore #defines, #if, etc. |
| 8495 | * Ignore comment and empty lines. |
| 8496 | * (need to get the line again, cin_islabel() may have |
| 8497 | * unlocked it) |
| 8498 | */ |
| 8499 | l = ml_get_curline(); |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8500 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8501 | || cin_nocode(l)) |
| 8502 | continue; |
| 8503 | |
| 8504 | /* |
| 8505 | * Are we at the start of a cpp base class declaration or |
| 8506 | * constructor initialization? |
| 8507 | */ /* XXX */ |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8508 | n = FALSE; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8509 | if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0) |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8510 | { |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 8511 | n = cin_is_cpp_baseclass(&cache_cpp_baseclass); |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8512 | l = ml_get_curline(); |
| 8513 | } |
| 8514 | if (n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8515 | { |
| 8516 | if (lookfor == LOOKFOR_UNTERM) |
| 8517 | { |
| 8518 | if (cont_amount > 0) |
| 8519 | amount = cont_amount; |
| 8520 | else |
| 8521 | amount += ind_continuation; |
| 8522 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8523 | else if (theline[0] == '{') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8524 | { |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8525 | /* Need to find start of the declaration. */ |
| 8526 | lookfor = LOOKFOR_UNTERM; |
| 8527 | ind_continuation = 0; |
| 8528 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8529 | } |
| 8530 | else |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8531 | /* XXX */ |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 8532 | amount = get_baseclass_amount( |
| 8533 | cache_cpp_baseclass.lpos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8534 | break; |
| 8535 | } |
| 8536 | else if (lookfor == LOOKFOR_CPP_BASECLASS) |
| 8537 | { |
| 8538 | /* only look, whether there is a cpp base class |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8539 | * declaration or initialization before the opening brace. |
| 8540 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8541 | if (cin_isterminated(l, TRUE, FALSE)) |
| 8542 | break; |
| 8543 | else |
| 8544 | continue; |
| 8545 | } |
| 8546 | |
| 8547 | /* |
| 8548 | * What happens next depends on the line being terminated. |
| 8549 | * If terminated with a ',' only consider it terminating if |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 8550 | * there is another unterminated statement behind, eg: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8551 | * 123, |
| 8552 | * sizeof |
| 8553 | * here |
| 8554 | * Otherwise check whether it is a enumeration or structure |
| 8555 | * initialisation (not indented) or a variable declaration |
| 8556 | * (indented). |
| 8557 | */ |
| 8558 | terminated = cin_isterminated(l, FALSE, TRUE); |
| 8559 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8560 | if (js_cur_has_key) |
| 8561 | { |
| 8562 | js_cur_has_key = 0; /* only check the first line */ |
| 8563 | if (curbuf->b_ind_js && terminated == ',') |
| 8564 | { |
| 8565 | /* For Javascript we might be inside an object: |
| 8566 | * key: something, <- align with this |
| 8567 | * key: something |
| 8568 | * or: |
| 8569 | * key: something + <- align with this |
| 8570 | * something, |
| 8571 | * key: something |
| 8572 | */ |
| 8573 | lookfor = LOOKFOR_JS_KEY; |
| 8574 | } |
| 8575 | } |
| 8576 | if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l)) |
| 8577 | { |
| 8578 | amount = get_indent(); |
| 8579 | break; |
| 8580 | } |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8581 | if (lookfor == LOOKFOR_COMMA) |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8582 | { |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8583 | if (tryposBrace != NULL && tryposBrace->lnum |
| 8584 | >= curwin->w_cursor.lnum) |
| 8585 | break; |
| 8586 | if (terminated == ',') |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8587 | /* line below current line is the one that starts a |
| 8588 | * (possibly broken) line ending in a comma. */ |
| 8589 | break; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8590 | else |
| 8591 | { |
| 8592 | amount = get_indent(); |
| 8593 | if (curwin->w_cursor.lnum - 1 == ourscope) |
| 8594 | /* line above is start of the scope, thus current |
| 8595 | * line is the one that stars a (possibly broken) |
| 8596 | * line ending in a comma. */ |
| 8597 | break; |
| 8598 | } |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8599 | } |
| 8600 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8601 | if (terminated == 0 || (lookfor != LOOKFOR_UNTERM |
| 8602 | && terminated == ',')) |
| 8603 | { |
Bram Moolenaar | 089af18 | 2015-10-07 11:41:49 +0200 | [diff] [blame] | 8604 | if (lookfor != LOOKFOR_ENUM_OR_INIT && |
| 8605 | (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '[')) |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8606 | amount += ind_continuation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8607 | /* |
| 8608 | * if we're in the middle of a paren thing, |
| 8609 | * go back to the line that starts it so |
| 8610 | * we can get the right prevailing indent |
| 8611 | * if ( foo && |
| 8612 | * bar ) |
| 8613 | */ |
| 8614 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8615 | * Position the cursor over the rightmost paren, so that |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8616 | * matching it will take us back to the start of the line. |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8617 | * Ignore a match before the start of the block. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8618 | */ |
| 8619 | (void)find_last_paren(l, '(', ')'); |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8620 | trypos = find_match_paren(corr_ind_maxparen(&cur_curpos)); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8621 | if (trypos != NULL && (trypos->lnum < tryposBrace->lnum |
| 8622 | || (trypos->lnum == tryposBrace->lnum |
| 8623 | && trypos->col < tryposBrace->col))) |
| 8624 | trypos = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8625 | |
| 8626 | /* |
| 8627 | * If we are looking for ',', we also look for matching |
| 8628 | * braces. |
| 8629 | */ |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8630 | if (trypos == NULL && terminated == ',' |
| 8631 | && find_last_paren(l, '{', '}')) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8632 | trypos = find_start_brace(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8633 | |
| 8634 | if (trypos != NULL) |
| 8635 | { |
| 8636 | /* |
| 8637 | * Check if we are on a case label now. This is |
| 8638 | * handled above. |
| 8639 | * case xx: if ( asdf && |
| 8640 | * asdf) |
| 8641 | */ |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8642 | curwin->w_cursor = *trypos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8643 | l = ml_get_curline(); |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8644 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8645 | { |
| 8646 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8647 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8648 | continue; |
| 8649 | } |
| 8650 | } |
| 8651 | |
| 8652 | /* |
| 8653 | * Skip over continuation lines to find the one to get the |
| 8654 | * indent from |
| 8655 | * char *usethis = "bla\ |
| 8656 | * bla", |
| 8657 | * here; |
| 8658 | */ |
| 8659 | if (terminated == ',') |
| 8660 | { |
| 8661 | while (curwin->w_cursor.lnum > 1) |
| 8662 | { |
| 8663 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 8664 | if (*l == NUL || l[STRLEN(l) - 1] != '\\') |
| 8665 | break; |
| 8666 | --curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8667 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8668 | } |
| 8669 | } |
| 8670 | |
| 8671 | /* |
| 8672 | * Get indent and pointer to text for current line, |
| 8673 | * ignoring any jump label. XXX |
| 8674 | */ |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8675 | if (curbuf->b_ind_js) |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8676 | cur_amount = get_indent(); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8677 | else |
| 8678 | cur_amount = skip_label(curwin->w_cursor.lnum, &l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8679 | /* |
| 8680 | * If this is just above the line we are indenting, and it |
| 8681 | * starts with a '{', line it up with this line. |
| 8682 | * while (not) |
| 8683 | * -> { |
| 8684 | * } |
| 8685 | */ |
| 8686 | if (terminated != ',' && lookfor != LOOKFOR_TERM |
| 8687 | && theline[0] == '{') |
| 8688 | { |
| 8689 | amount = cur_amount; |
| 8690 | /* |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8691 | * Only add b_ind_open_extra when the current line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8692 | * doesn't start with a '{', which must have a match |
| 8693 | * in the same line (scope is the same). Probably: |
| 8694 | * { 1, 2 }, |
| 8695 | * -> { 3, 4 } |
| 8696 | */ |
| 8697 | if (*skipwhite(l) != '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8698 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8699 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8700 | if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8701 | { |
| 8702 | /* have to look back, whether it is a cpp base |
| 8703 | * class declaration or initialization */ |
| 8704 | lookfor = LOOKFOR_CPP_BASECLASS; |
| 8705 | continue; |
| 8706 | } |
| 8707 | break; |
| 8708 | } |
| 8709 | |
| 8710 | /* |
| 8711 | * Check if we are after an "if", "while", etc. |
| 8712 | * Also allow " } else". |
| 8713 | */ |
| 8714 | if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) |
| 8715 | { |
| 8716 | /* |
| 8717 | * Found an unterminated line after an if (), line up |
| 8718 | * with the last one. |
| 8719 | * if (cond) |
| 8720 | * 100 + |
| 8721 | * -> here; |
| 8722 | */ |
| 8723 | if (lookfor == LOOKFOR_UNTERM |
| 8724 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8725 | { |
| 8726 | if (cont_amount > 0) |
| 8727 | amount = cont_amount; |
| 8728 | else |
| 8729 | amount += ind_continuation; |
| 8730 | break; |
| 8731 | } |
| 8732 | |
| 8733 | /* |
| 8734 | * If this is just above the line we are indenting, we |
| 8735 | * are finished. |
| 8736 | * while (not) |
| 8737 | * -> here; |
| 8738 | * Otherwise this indent can be used when the line |
| 8739 | * before this is terminated. |
| 8740 | * yyy; |
| 8741 | * if (stat) |
| 8742 | * while (not) |
| 8743 | * xxx; |
| 8744 | * -> here; |
| 8745 | */ |
| 8746 | amount = cur_amount; |
| 8747 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8748 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8749 | if (lookfor != LOOKFOR_TERM) |
| 8750 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8751 | amount += curbuf->b_ind_level |
| 8752 | + curbuf->b_ind_no_brace; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8753 | break; |
| 8754 | } |
| 8755 | |
| 8756 | /* |
| 8757 | * Special trick: when expecting the while () after a |
| 8758 | * do, line up with the while() |
| 8759 | * do |
| 8760 | * x = 1; |
| 8761 | * -> here |
| 8762 | */ |
| 8763 | l = skipwhite(ml_get_curline()); |
| 8764 | if (cin_isdo(l)) |
| 8765 | { |
| 8766 | if (whilelevel == 0) |
| 8767 | break; |
| 8768 | --whilelevel; |
| 8769 | } |
| 8770 | |
| 8771 | /* |
| 8772 | * When searching for a terminated line, don't use the |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8773 | * one between the "if" and the matching "else". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8774 | * Need to use the scope of this "else". XXX |
| 8775 | * If whilelevel != 0 continue looking for a "do {". |
| 8776 | */ |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8777 | if (cin_iselse(l) && whilelevel == 0) |
| 8778 | { |
| 8779 | /* If we're looking at "} else", let's make sure we |
| 8780 | * find the opening brace of the enclosing scope, |
| 8781 | * not the one from "if () {". */ |
| 8782 | if (*l == '}') |
| 8783 | curwin->w_cursor.col = |
Bram Moolenaar | 9b83c2f | 2011-05-25 17:29:44 +0200 | [diff] [blame] | 8784 | (colnr_T)(l - ml_get_curline()) + 1; |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8785 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8786 | if ((trypos = find_start_brace()) == NULL |
| 8787 | || find_match(LOOKFOR_IF, trypos->lnum) |
| 8788 | == FAIL) |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8789 | break; |
| 8790 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8791 | } |
| 8792 | |
| 8793 | /* |
| 8794 | * If we're below an unterminated line that is not an |
| 8795 | * "if" or something, we may line up with this line or |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 8796 | * add something for a continuation line, depending on |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8797 | * the line before this one. |
| 8798 | */ |
| 8799 | else |
| 8800 | { |
| 8801 | /* |
| 8802 | * Found two unterminated lines on a row, line up with |
| 8803 | * the last one. |
| 8804 | * c = 99 + |
| 8805 | * 100 + |
| 8806 | * -> here; |
| 8807 | */ |
| 8808 | if (lookfor == LOOKFOR_UNTERM) |
| 8809 | { |
| 8810 | /* When line ends in a comma add extra indent */ |
| 8811 | if (terminated == ',') |
| 8812 | amount += ind_continuation; |
| 8813 | break; |
| 8814 | } |
| 8815 | |
| 8816 | if (lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8817 | { |
| 8818 | /* Found two lines ending in ',', lineup with the |
| 8819 | * lowest one, but check for cpp base class |
| 8820 | * declaration/initialization, if it is an |
| 8821 | * opening brace or we are looking just for |
| 8822 | * enumerations/initializations. */ |
| 8823 | if (terminated == ',') |
| 8824 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8825 | if (curbuf->b_ind_cpp_baseclass == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8826 | break; |
| 8827 | |
| 8828 | lookfor = LOOKFOR_CPP_BASECLASS; |
| 8829 | continue; |
| 8830 | } |
| 8831 | |
| 8832 | /* Ignore unterminated lines in between, but |
| 8833 | * reduce indent. */ |
| 8834 | if (amount > cur_amount) |
| 8835 | amount = cur_amount; |
| 8836 | } |
| 8837 | else |
| 8838 | { |
| 8839 | /* |
| 8840 | * Found first unterminated line on a row, may |
| 8841 | * line up with this line, remember its indent |
| 8842 | * 100 + |
| 8843 | * -> here; |
| 8844 | */ |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8845 | l = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8846 | amount = cur_amount; |
Bram Moolenaar | 089af18 | 2015-10-07 11:41:49 +0200 | [diff] [blame] | 8847 | |
| 8848 | n = (int)STRLEN(l); |
| 8849 | if (terminated == ',' && (*skipwhite(l) == ']' |
| 8850 | || (n >=2 && l[n - 2] == ']'))) |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8851 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8852 | |
| 8853 | /* |
| 8854 | * If previous line ends in ',', check whether we |
| 8855 | * are in an initialization or enum |
| 8856 | * struct xxx = |
| 8857 | * { |
| 8858 | * sizeof a, |
| 8859 | * 124 }; |
| 8860 | * or a normal possible continuation line. |
| 8861 | * but only, of no other statement has been found |
| 8862 | * yet. |
| 8863 | */ |
| 8864 | if (lookfor == LOOKFOR_INITIAL && terminated == ',') |
| 8865 | { |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8866 | if (curbuf->b_ind_js) |
| 8867 | { |
| 8868 | /* Search for a line ending in a comma |
| 8869 | * and line up with the line below it |
| 8870 | * (could be the current line). |
| 8871 | * some = [ |
| 8872 | * 1, <- line up here |
| 8873 | * 2, |
| 8874 | * some = [ |
| 8875 | * 3 + <- line up here |
| 8876 | * 4 * |
| 8877 | * 5, |
| 8878 | * 6, |
| 8879 | */ |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8880 | if (cin_iscomment(skipwhite(l))) |
| 8881 | break; |
| 8882 | lookfor = LOOKFOR_COMMA; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8883 | trypos = find_match_char('[', |
| 8884 | curbuf->b_ind_maxparen); |
| 8885 | if (trypos != NULL) |
| 8886 | { |
| 8887 | if (trypos->lnum |
| 8888 | == curwin->w_cursor.lnum - 1) |
| 8889 | { |
| 8890 | /* Current line is first inside |
| 8891 | * [], line up with it. */ |
| 8892 | break; |
| 8893 | } |
| 8894 | ourscope = trypos->lnum; |
| 8895 | } |
| 8896 | } |
| 8897 | else |
| 8898 | { |
| 8899 | lookfor = LOOKFOR_ENUM_OR_INIT; |
| 8900 | cont_amount = cin_first_id_amount(); |
| 8901 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8902 | } |
| 8903 | else |
| 8904 | { |
| 8905 | if (lookfor == LOOKFOR_INITIAL |
| 8906 | && *l != NUL |
| 8907 | && l[STRLEN(l) - 1] == '\\') |
| 8908 | /* XXX */ |
| 8909 | cont_amount = cin_get_equal_amount( |
| 8910 | curwin->w_cursor.lnum); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8911 | if (lookfor != LOOKFOR_TERM |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8912 | && lookfor != LOOKFOR_JS_KEY |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 8913 | && lookfor != LOOKFOR_COMMA |
| 8914 | && raw_string_start != curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8915 | lookfor = LOOKFOR_UNTERM; |
| 8916 | } |
| 8917 | } |
| 8918 | } |
| 8919 | } |
| 8920 | |
| 8921 | /* |
| 8922 | * Check if we are after a while (cond); |
| 8923 | * If so: Ignore until the matching "do". |
| 8924 | */ |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8925 | else if (cin_iswhileofdo_end(terminated)) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8926 | { |
| 8927 | /* |
| 8928 | * Found an unterminated line after a while ();, line up |
| 8929 | * with the last one. |
| 8930 | * while (cond); |
| 8931 | * 100 + <- line up with this one |
| 8932 | * -> here; |
| 8933 | */ |
| 8934 | if (lookfor == LOOKFOR_UNTERM |
| 8935 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8936 | { |
| 8937 | if (cont_amount > 0) |
| 8938 | amount = cont_amount; |
| 8939 | else |
| 8940 | amount += ind_continuation; |
| 8941 | break; |
| 8942 | } |
| 8943 | |
| 8944 | if (whilelevel == 0) |
| 8945 | { |
| 8946 | lookfor = LOOKFOR_TERM; |
| 8947 | amount = get_indent(); /* XXX */ |
| 8948 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8949 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8950 | } |
| 8951 | ++whilelevel; |
| 8952 | } |
| 8953 | |
| 8954 | /* |
| 8955 | * We are after a "normal" statement. |
| 8956 | * If we had another statement we can stop now and use the |
| 8957 | * indent of that other statement. |
| 8958 | * Otherwise the indent of the current statement may be used, |
| 8959 | * search backwards for the next "normal" statement. |
| 8960 | */ |
| 8961 | else |
| 8962 | { |
| 8963 | /* |
| 8964 | * Skip single break line, if before a switch label. It |
| 8965 | * may be lined up with the case label. |
| 8966 | */ |
| 8967 | if (lookfor == LOOKFOR_NOBREAK |
| 8968 | && cin_isbreak(skipwhite(ml_get_curline()))) |
| 8969 | { |
| 8970 | lookfor = LOOKFOR_ANY; |
| 8971 | continue; |
| 8972 | } |
| 8973 | |
| 8974 | /* |
| 8975 | * Handle "do {" line. |
| 8976 | */ |
| 8977 | if (whilelevel > 0) |
| 8978 | { |
| 8979 | l = cin_skipcomment(ml_get_curline()); |
| 8980 | if (cin_isdo(l)) |
| 8981 | { |
| 8982 | amount = get_indent(); /* XXX */ |
| 8983 | --whilelevel; |
| 8984 | continue; |
| 8985 | } |
| 8986 | } |
| 8987 | |
| 8988 | /* |
| 8989 | * Found a terminated line above an unterminated line. Add |
| 8990 | * the amount for a continuation line. |
| 8991 | * x = 1; |
| 8992 | * y = foo + |
| 8993 | * -> here; |
| 8994 | * or |
| 8995 | * int x = 1; |
| 8996 | * int foo, |
| 8997 | * -> here; |
| 8998 | */ |
| 8999 | if (lookfor == LOOKFOR_UNTERM |
| 9000 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 9001 | { |
| 9002 | if (cont_amount > 0) |
| 9003 | amount = cont_amount; |
| 9004 | else |
| 9005 | amount += ind_continuation; |
| 9006 | break; |
| 9007 | } |
| 9008 | |
| 9009 | /* |
| 9010 | * Found a terminated line above a terminated line or "if" |
| 9011 | * etc. line. Use the amount of the line below us. |
| 9012 | * x = 1; x = 1; |
| 9013 | * if (asdf) y = 2; |
| 9014 | * while (asdf) ->here; |
| 9015 | * here; |
| 9016 | * ->foo; |
| 9017 | */ |
| 9018 | if (lookfor == LOOKFOR_TERM) |
| 9019 | { |
| 9020 | if (!lookfor_break && whilelevel == 0) |
| 9021 | break; |
| 9022 | } |
| 9023 | |
| 9024 | /* |
| 9025 | * First line above the one we're indenting is terminated. |
| 9026 | * To know what needs to be done look further backward for |
| 9027 | * a terminated line. |
| 9028 | */ |
| 9029 | else |
| 9030 | { |
| 9031 | /* |
| 9032 | * position the cursor over the rightmost paren, so |
| 9033 | * that matching it will take us back to the start of |
| 9034 | * the line. Helps for: |
| 9035 | * func(asdr, |
| 9036 | * asdfasdf); |
| 9037 | * here; |
| 9038 | */ |
| 9039 | term_again: |
| 9040 | l = ml_get_curline(); |
| 9041 | if (find_last_paren(l, '(', ')') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9042 | && (trypos = find_match_paren( |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9043 | curbuf->b_ind_maxparen)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9044 | { |
| 9045 | /* |
| 9046 | * Check if we are on a case label now. This is |
| 9047 | * handled above. |
| 9048 | * case xx: if ( asdf && |
| 9049 | * asdf) |
| 9050 | */ |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 9051 | curwin->w_cursor = *trypos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9052 | l = ml_get_curline(); |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 9053 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9054 | { |
| 9055 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 9056 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9057 | continue; |
| 9058 | } |
| 9059 | } |
| 9060 | |
| 9061 | /* When aligning with the case statement, don't align |
| 9062 | * with a statement after it. |
| 9063 | * case 1: { <-- don't use this { position |
| 9064 | * stat; |
| 9065 | * } |
| 9066 | * case 2: |
| 9067 | * stat; |
| 9068 | * } |
| 9069 | */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9070 | iscase = (curbuf->b_ind_keep_case_label |
| 9071 | && cin_iscase(l, FALSE)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9072 | |
| 9073 | /* |
| 9074 | * Get indent and pointer to text for current line, |
| 9075 | * ignoring any jump label. |
| 9076 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9077 | amount = skip_label(curwin->w_cursor.lnum, &l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9078 | |
| 9079 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9080 | amount += curbuf->b_ind_open_extra; |
| 9081 | /* See remark above: "Only add b_ind_open_extra.." */ |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 9082 | l = skipwhite(l); |
| 9083 | if (*l == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9084 | amount -= curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9085 | lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM; |
| 9086 | |
| 9087 | /* |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 9088 | * When a terminated line starts with "else" skip to |
| 9089 | * the matching "if": |
| 9090 | * else 3; |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 9091 | * indent this; |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 9092 | * Need to use the scope of this "else". XXX |
| 9093 | * If whilelevel != 0 continue looking for a "do {". |
| 9094 | */ |
| 9095 | if (lookfor == LOOKFOR_TERM |
| 9096 | && *l != '}' |
| 9097 | && cin_iselse(l) |
| 9098 | && whilelevel == 0) |
| 9099 | { |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9100 | if ((trypos = find_start_brace()) == NULL |
| 9101 | || find_match(LOOKFOR_IF, trypos->lnum) |
| 9102 | == FAIL) |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 9103 | break; |
| 9104 | continue; |
| 9105 | } |
| 9106 | |
| 9107 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9108 | * If we're at the end of a block, skip to the start of |
| 9109 | * that block. |
| 9110 | */ |
Bram Moolenaar | 6d8f9c6 | 2011-11-30 13:03:28 +0100 | [diff] [blame] | 9111 | l = ml_get_curline(); |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9112 | if (find_last_paren(l, '{', '}') /* XXX */ |
| 9113 | && (trypos = find_start_brace()) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9114 | { |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 9115 | curwin->w_cursor = *trypos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9116 | /* if not "else {" check for terminated again */ |
| 9117 | /* but skip block for "} else {" */ |
| 9118 | l = cin_skipcomment(ml_get_curline()); |
| 9119 | if (*l == '}' || !cin_iselse(l)) |
| 9120 | goto term_again; |
| 9121 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 9122 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9123 | } |
| 9124 | } |
| 9125 | } |
| 9126 | } |
| 9127 | } |
| 9128 | } |
| 9129 | |
| 9130 | /* add extra indent for a comment */ |
| 9131 | if (cin_iscomment(theline)) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9132 | amount += curbuf->b_ind_comment; |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 9133 | |
| 9134 | /* subtract extra left-shift for jump labels */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9135 | if (curbuf->b_ind_jump_label > 0 && original_line_islabel) |
| 9136 | amount -= curbuf->b_ind_jump_label; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9137 | |
| 9138 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9139 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9140 | |
| 9141 | /* |
| 9142 | * ok -- we're not inside any sort of structure at all! |
| 9143 | * |
| 9144 | * This means we're at the top level, and everything should |
| 9145 | * basically just match where the previous line is, except |
| 9146 | * for the lines immediately following a function declaration, |
| 9147 | * which are K&R-style parameters and need to be indented. |
| 9148 | * |
| 9149 | * if our line starts with an open brace, forget about any |
| 9150 | * prevailing indent and make sure it looks like the start |
| 9151 | * of a function |
| 9152 | */ |
| 9153 | |
| 9154 | if (theline[0] == '{') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9155 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9156 | amount = curbuf->b_ind_first_open; |
| 9157 | goto theend; |
| 9158 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9159 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9160 | /* |
| 9161 | * If the NEXT line is a function declaration, the current |
| 9162 | * line needs to be indented as a function type spec. |
| 9163 | * Don't do this if the current line looks like a comment or if the |
| 9164 | * current line is terminated, ie. ends in ';', or if the current line |
| 9165 | * contains { or }: "void f() {\n if (1)" |
| 9166 | */ |
| 9167 | if (cur_curpos.lnum < curbuf->b_ml.ml_line_count |
| 9168 | && !cin_nocode(theline) |
| 9169 | && vim_strchr(theline, '{') == NULL |
| 9170 | && vim_strchr(theline, '}') == NULL |
| 9171 | && !cin_ends_in(theline, (char_u *)":", NULL) |
| 9172 | && !cin_ends_in(theline, (char_u *)",", NULL) |
| 9173 | && cin_isfuncdecl(NULL, cur_curpos.lnum + 1, |
| 9174 | cur_curpos.lnum + 1) |
| 9175 | && !cin_isterminated(theline, FALSE, TRUE)) |
| 9176 | { |
| 9177 | amount = curbuf->b_ind_func_type; |
| 9178 | goto theend; |
| 9179 | } |
| 9180 | |
| 9181 | /* search backwards until we find something we recognize */ |
| 9182 | amount = 0; |
| 9183 | curwin->w_cursor = cur_curpos; |
| 9184 | while (curwin->w_cursor.lnum > 1) |
| 9185 | { |
| 9186 | curwin->w_cursor.lnum--; |
| 9187 | curwin->w_cursor.col = 0; |
| 9188 | |
| 9189 | l = ml_get_curline(); |
| 9190 | |
| 9191 | /* |
| 9192 | * If we're in a comment or raw string now, skip to the start |
| 9193 | * of it. |
| 9194 | */ /* XXX */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 9195 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9196 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9197 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 9198 | curwin->w_cursor.col = 0; |
| 9199 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9200 | } |
| 9201 | |
| 9202 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9203 | * Are we at the start of a cpp base class declaration or |
| 9204 | * constructor initialization? |
| 9205 | */ /* XXX */ |
| 9206 | n = FALSE; |
| 9207 | if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9208 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9209 | n = cin_is_cpp_baseclass(&cache_cpp_baseclass); |
| 9210 | l = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9211 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9212 | if (n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9213 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9214 | /* XXX */ |
| 9215 | amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col); |
| 9216 | break; |
| 9217 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9218 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9219 | /* |
| 9220 | * Skip preprocessor directives and blank lines. |
| 9221 | */ |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 9222 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9223 | continue; |
| 9224 | |
| 9225 | if (cin_nocode(l)) |
| 9226 | continue; |
| 9227 | |
| 9228 | /* |
| 9229 | * If the previous line ends in ',', use one level of |
| 9230 | * indentation: |
| 9231 | * int foo, |
| 9232 | * bar; |
| 9233 | * do this before checking for '}' in case of eg. |
| 9234 | * enum foobar |
| 9235 | * { |
| 9236 | * ... |
| 9237 | * } foo, |
| 9238 | * bar; |
| 9239 | */ |
| 9240 | n = 0; |
| 9241 | if (cin_ends_in(l, (char_u *)",", NULL) |
| 9242 | || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\')) |
| 9243 | { |
| 9244 | /* take us back to opening paren */ |
| 9245 | if (find_last_paren(l, '(', ')') |
| 9246 | && (trypos = find_match_paren( |
| 9247 | curbuf->b_ind_maxparen)) != NULL) |
| 9248 | curwin->w_cursor = *trypos; |
| 9249 | |
| 9250 | /* For a line ending in ',' that is a continuation line go |
| 9251 | * back to the first line with a backslash: |
| 9252 | * char *foo = "bla\ |
| 9253 | * bla", |
| 9254 | * here; |
| 9255 | */ |
| 9256 | while (n == 0 && curwin->w_cursor.lnum > 1) |
| 9257 | { |
| 9258 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 9259 | if (*l == NUL || l[STRLEN(l) - 1] != '\\') |
| 9260 | break; |
| 9261 | --curwin->w_cursor.lnum; |
| 9262 | curwin->w_cursor.col = 0; |
| 9263 | } |
| 9264 | |
| 9265 | amount = get_indent(); /* XXX */ |
| 9266 | |
| 9267 | if (amount == 0) |
| 9268 | amount = cin_first_id_amount(); |
| 9269 | if (amount == 0) |
| 9270 | amount = ind_continuation; |
| 9271 | break; |
| 9272 | } |
| 9273 | |
| 9274 | /* |
| 9275 | * If the line looks like a function declaration, and we're |
| 9276 | * not in a comment, put it the left margin. |
| 9277 | */ |
| 9278 | if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */ |
| 9279 | break; |
| 9280 | l = ml_get_curline(); |
| 9281 | |
| 9282 | /* |
| 9283 | * Finding the closing '}' of a previous function. Put |
| 9284 | * current line at the left margin. For when 'cino' has "fs". |
| 9285 | */ |
| 9286 | if (*skipwhite(l) == '}') |
| 9287 | break; |
| 9288 | |
| 9289 | /* (matching {) |
| 9290 | * If the previous line ends on '};' (maybe followed by |
| 9291 | * comments) align at column 0. For example: |
| 9292 | * char *string_array[] = { "foo", |
| 9293 | * / * x * / "b};ar" }; / * foobar * / |
| 9294 | */ |
| 9295 | if (cin_ends_in(l, (char_u *)"};", NULL)) |
| 9296 | break; |
| 9297 | |
| 9298 | /* |
| 9299 | * If the previous line ends on '[' we are probably in an |
| 9300 | * array constant: |
| 9301 | * something = [ |
| 9302 | * 234, <- extra indent |
| 9303 | */ |
| 9304 | if (cin_ends_in(l, (char_u *)"[", NULL)) |
| 9305 | { |
| 9306 | amount = get_indent() + ind_continuation; |
| 9307 | break; |
| 9308 | } |
| 9309 | |
| 9310 | /* |
| 9311 | * Find a line only has a semicolon that belongs to a previous |
| 9312 | * line ending in '}', e.g. before an #endif. Don't increase |
| 9313 | * indent then. |
| 9314 | */ |
| 9315 | if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1)) |
| 9316 | { |
| 9317 | pos_T curpos_save = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9318 | |
| 9319 | while (curwin->w_cursor.lnum > 1) |
| 9320 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9321 | look = ml_get(--curwin->w_cursor.lnum); |
| 9322 | if (!(cin_nocode(look) || cin_ispreproc_cont( |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 9323 | &look, &curwin->w_cursor.lnum, &amount))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9324 | break; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9325 | } |
| 9326 | if (curwin->w_cursor.lnum > 0 |
| 9327 | && cin_ends_in(look, (char_u *)"}", NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9328 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9329 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9330 | curwin->w_cursor = curpos_save; |
| 9331 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9332 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9333 | /* |
| 9334 | * If the PREVIOUS line is a function declaration, the current |
| 9335 | * line (and the ones that follow) needs to be indented as |
| 9336 | * parameters. |
| 9337 | */ |
| 9338 | if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
| 9339 | { |
| 9340 | amount = curbuf->b_ind_param; |
| 9341 | break; |
| 9342 | } |
| 9343 | |
| 9344 | /* |
| 9345 | * If the previous line ends in ';' and the line before the |
| 9346 | * previous line ends in ',' or '\', ident to column zero: |
| 9347 | * int foo, |
| 9348 | * bar; |
| 9349 | * indent_to_0 here; |
| 9350 | */ |
| 9351 | if (cin_ends_in(l, (char_u *)";", NULL)) |
| 9352 | { |
| 9353 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 9354 | if (cin_ends_in(l, (char_u *)",", NULL) |
| 9355 | || (*l != NUL && l[STRLEN(l) - 1] == '\\')) |
| 9356 | break; |
| 9357 | l = ml_get_curline(); |
| 9358 | } |
| 9359 | |
| 9360 | /* |
| 9361 | * Doesn't look like anything interesting -- so just |
| 9362 | * use the indent of this line. |
| 9363 | * |
| 9364 | * Position the cursor over the rightmost paren, so that |
| 9365 | * matching it will take us back to the start of the line. |
| 9366 | */ |
| 9367 | find_last_paren(l, '(', ')'); |
| 9368 | |
| 9369 | if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
| 9370 | curwin->w_cursor = *trypos; |
| 9371 | amount = get_indent(); /* XXX */ |
| 9372 | break; |
| 9373 | } |
| 9374 | |
| 9375 | /* add extra indent for a comment */ |
| 9376 | if (cin_iscomment(theline)) |
| 9377 | amount += curbuf->b_ind_comment; |
| 9378 | |
| 9379 | /* add extra indent if the previous line ended in a backslash: |
| 9380 | * "asdfasdf\ |
| 9381 | * here"; |
| 9382 | * char *foo = "asdf\ |
| 9383 | * here"; |
| 9384 | */ |
| 9385 | if (cur_curpos.lnum > 1) |
| 9386 | { |
| 9387 | l = ml_get(cur_curpos.lnum - 1); |
| 9388 | if (*l != NUL && l[STRLEN(l) - 1] == '\\') |
| 9389 | { |
| 9390 | cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1); |
| 9391 | if (cur_amount > 0) |
| 9392 | amount = cur_amount; |
| 9393 | else if (cur_amount == 0) |
| 9394 | amount += ind_continuation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9395 | } |
| 9396 | } |
| 9397 | |
| 9398 | theend: |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9399 | if (amount < 0) |
| 9400 | amount = 0; |
| 9401 | |
| 9402 | laterend: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9403 | /* put the cursor back where it belongs */ |
| 9404 | curwin->w_cursor = cur_curpos; |
| 9405 | |
| 9406 | vim_free(linecopy); |
| 9407 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9408 | return amount; |
| 9409 | } |
| 9410 | |
| 9411 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9412 | find_match(int lookfor, linenr_T ourscope) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9413 | { |
| 9414 | char_u *look; |
| 9415 | pos_T *theirscope; |
| 9416 | char_u *mightbeif; |
| 9417 | int elselevel; |
| 9418 | int whilelevel; |
| 9419 | |
| 9420 | if (lookfor == LOOKFOR_IF) |
| 9421 | { |
| 9422 | elselevel = 1; |
| 9423 | whilelevel = 0; |
| 9424 | } |
| 9425 | else |
| 9426 | { |
| 9427 | elselevel = 0; |
| 9428 | whilelevel = 1; |
| 9429 | } |
| 9430 | |
| 9431 | curwin->w_cursor.col = 0; |
| 9432 | |
| 9433 | while (curwin->w_cursor.lnum > ourscope + 1) |
| 9434 | { |
| 9435 | curwin->w_cursor.lnum--; |
| 9436 | curwin->w_cursor.col = 0; |
| 9437 | |
| 9438 | look = cin_skipcomment(ml_get_curline()); |
| 9439 | if (cin_iselse(look) |
| 9440 | || cin_isif(look) |
| 9441 | || cin_isdo(look) /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9442 | || cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9443 | { |
| 9444 | /* |
| 9445 | * if we've gone outside the braces entirely, |
| 9446 | * we must be out of scope... |
| 9447 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9448 | theirscope = find_start_brace(); /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9449 | if (theirscope == NULL) |
| 9450 | break; |
| 9451 | |
| 9452 | /* |
| 9453 | * and if the brace enclosing this is further |
| 9454 | * back than the one enclosing the else, we're |
| 9455 | * out of luck too. |
| 9456 | */ |
| 9457 | if (theirscope->lnum < ourscope) |
| 9458 | break; |
| 9459 | |
| 9460 | /* |
| 9461 | * and if they're enclosed in a *deeper* brace, |
| 9462 | * then we can ignore it because it's in a |
| 9463 | * different scope... |
| 9464 | */ |
| 9465 | if (theirscope->lnum > ourscope) |
| 9466 | continue; |
| 9467 | |
| 9468 | /* |
| 9469 | * if it was an "else" (that's not an "else if") |
| 9470 | * then we need to go back to another if, so |
| 9471 | * increment elselevel |
| 9472 | */ |
| 9473 | look = cin_skipcomment(ml_get_curline()); |
| 9474 | if (cin_iselse(look)) |
| 9475 | { |
| 9476 | mightbeif = cin_skipcomment(look + 4); |
| 9477 | if (!cin_isif(mightbeif)) |
| 9478 | ++elselevel; |
| 9479 | continue; |
| 9480 | } |
| 9481 | |
| 9482 | /* |
| 9483 | * if it was a "while" then we need to go back to |
| 9484 | * another "do", so increment whilelevel. XXX |
| 9485 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9486 | if (cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9487 | { |
| 9488 | ++whilelevel; |
| 9489 | continue; |
| 9490 | } |
| 9491 | |
| 9492 | /* If it's an "if" decrement elselevel */ |
| 9493 | look = cin_skipcomment(ml_get_curline()); |
| 9494 | if (cin_isif(look)) |
| 9495 | { |
| 9496 | elselevel--; |
| 9497 | /* |
| 9498 | * When looking for an "if" ignore "while"s that |
| 9499 | * get in the way. |
| 9500 | */ |
| 9501 | if (elselevel == 0 && lookfor == LOOKFOR_IF) |
| 9502 | whilelevel = 0; |
| 9503 | } |
| 9504 | |
| 9505 | /* If it's a "do" decrement whilelevel */ |
| 9506 | if (cin_isdo(look)) |
| 9507 | whilelevel--; |
| 9508 | |
| 9509 | /* |
| 9510 | * if we've used up all the elses, then |
| 9511 | * this must be the if that we want! |
| 9512 | * match the indent level of that if. |
| 9513 | */ |
| 9514 | if (elselevel <= 0 && whilelevel <= 0) |
| 9515 | { |
| 9516 | return OK; |
| 9517 | } |
| 9518 | } |
| 9519 | } |
| 9520 | return FAIL; |
| 9521 | } |
| 9522 | |
| 9523 | # if defined(FEAT_EVAL) || defined(PROTO) |
| 9524 | /* |
| 9525 | * Get indent level from 'indentexpr'. |
| 9526 | */ |
| 9527 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9528 | get_expr_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9529 | { |
Bram Moolenaar | 97db554 | 2017-04-21 23:18:26 +0200 | [diff] [blame] | 9530 | int indent = -1; |
Bram Moolenaar | a701b3b | 2017-04-20 22:57:27 +0200 | [diff] [blame] | 9531 | char_u *inde_copy; |
Bram Moolenaar | 8fe8d9e | 2013-02-13 16:10:17 +0100 | [diff] [blame] | 9532 | pos_T save_pos; |
| 9533 | colnr_T save_curswant; |
| 9534 | int save_set_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9535 | int save_State; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 9536 | int use_sandbox = was_set_insecurely((char_u *)"indentexpr", |
| 9537 | OPT_LOCAL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9538 | |
Bram Moolenaar | 8fe8d9e | 2013-02-13 16:10:17 +0100 | [diff] [blame] | 9539 | /* Save and restore cursor position and curswant, in case it was changed |
| 9540 | * via :normal commands */ |
| 9541 | save_pos = curwin->w_cursor; |
| 9542 | save_curswant = curwin->w_curswant; |
| 9543 | save_set_curswant = curwin->w_set_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9544 | set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 9545 | if (use_sandbox) |
| 9546 | ++sandbox; |
| 9547 | ++textlock; |
Bram Moolenaar | a701b3b | 2017-04-20 22:57:27 +0200 | [diff] [blame] | 9548 | |
| 9549 | /* Need to make a copy, the 'indentexpr' option could be changed while |
| 9550 | * evaluating it. */ |
| 9551 | inde_copy = vim_strsave(curbuf->b_p_inde); |
| 9552 | if (inde_copy != NULL) |
| 9553 | { |
| 9554 | indent = (int)eval_to_number(inde_copy); |
| 9555 | vim_free(inde_copy); |
| 9556 | } |
| 9557 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 9558 | if (use_sandbox) |
| 9559 | --sandbox; |
| 9560 | --textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9561 | |
| 9562 | /* Restore the cursor position so that 'indentexpr' doesn't need to. |
| 9563 | * Pretend to be in Insert mode, allow cursor past end of line for "o" |
| 9564 | * command. */ |
| 9565 | save_State = State; |
| 9566 | State = INSERT; |
Bram Moolenaar | 8fe8d9e | 2013-02-13 16:10:17 +0100 | [diff] [blame] | 9567 | curwin->w_cursor = save_pos; |
| 9568 | curwin->w_curswant = save_curswant; |
| 9569 | curwin->w_set_curswant = save_set_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9570 | check_cursor(); |
| 9571 | State = save_State; |
| 9572 | |
| 9573 | /* If there is an error, just keep the current indent. */ |
| 9574 | if (indent < 0) |
| 9575 | indent = get_indent(); |
| 9576 | |
| 9577 | return indent; |
| 9578 | } |
| 9579 | # endif |
| 9580 | |
| 9581 | #endif /* FEAT_CINDENT */ |
| 9582 | |
| 9583 | #if defined(FEAT_LISP) || defined(PROTO) |
| 9584 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9585 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9586 | lisp_match(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9587 | { |
| 9588 | char_u buf[LSIZE]; |
| 9589 | int len; |
Bram Moolenaar | af6c131 | 2014-03-12 18:55:58 +0100 | [diff] [blame] | 9590 | 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] | 9591 | |
| 9592 | while (*word != NUL) |
| 9593 | { |
| 9594 | (void)copy_option_part(&word, buf, LSIZE, ","); |
| 9595 | len = (int)STRLEN(buf); |
| 9596 | if (STRNCMP(buf, p, len) == 0 && p[len] == ' ') |
| 9597 | return TRUE; |
| 9598 | } |
| 9599 | return FALSE; |
| 9600 | } |
| 9601 | |
| 9602 | /* |
| 9603 | * When 'p' is present in 'cpoptions, a Vi compatible method is used. |
| 9604 | * The incompatible newer method is quite a bit better at indenting |
| 9605 | * code in lisp-like languages than the traditional one; it's still |
| 9606 | * mostly heuristics however -- Dirk van Deun, dirk@rave.org |
| 9607 | * |
| 9608 | * TODO: |
| 9609 | * Findmatch() should be adapted for lisp, also to make showmatch |
| 9610 | * work correctly: now (v5.3) it seems all C/C++ oriented: |
| 9611 | * - it does not recognize the #\( and #\) notations as character literals |
| 9612 | * - it doesn't know about comments starting with a semicolon |
| 9613 | * - it incorrectly interprets '(' as a character literal |
| 9614 | * All this messes up get_lisp_indent in some rare cases. |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9615 | * Update from Sergey Khorev: |
| 9616 | * I tried to fix the first two issues. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9617 | */ |
| 9618 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9619 | get_lisp_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9620 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9621 | pos_T *pos, realpos, paren; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9622 | int amount; |
| 9623 | char_u *that; |
| 9624 | colnr_T col; |
| 9625 | colnr_T firsttry; |
| 9626 | int parencount, quotecount; |
| 9627 | int vi_lisp; |
| 9628 | |
| 9629 | /* Set vi_lisp to use the vi-compatible method */ |
| 9630 | vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL); |
| 9631 | |
| 9632 | realpos = curwin->w_cursor; |
| 9633 | curwin->w_cursor.col = 0; |
| 9634 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9635 | if ((pos = findmatch(NULL, '(')) == NULL) |
| 9636 | pos = findmatch(NULL, '['); |
| 9637 | else |
| 9638 | { |
| 9639 | paren = *pos; |
| 9640 | pos = findmatch(NULL, '['); |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 9641 | if (pos == NULL || LT_POSP(pos, &paren)) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9642 | pos = &paren; |
| 9643 | } |
| 9644 | if (pos != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9645 | { |
| 9646 | /* Extra trick: Take the indent of the first previous non-white |
| 9647 | * line that is at the same () level. */ |
| 9648 | amount = -1; |
| 9649 | parencount = 0; |
| 9650 | |
| 9651 | while (--curwin->w_cursor.lnum >= pos->lnum) |
| 9652 | { |
| 9653 | if (linewhite(curwin->w_cursor.lnum)) |
| 9654 | continue; |
| 9655 | for (that = ml_get_curline(); *that != NUL; ++that) |
| 9656 | { |
| 9657 | if (*that == ';') |
| 9658 | { |
| 9659 | while (*(that + 1) != NUL) |
| 9660 | ++that; |
| 9661 | continue; |
| 9662 | } |
| 9663 | if (*that == '\\') |
| 9664 | { |
| 9665 | if (*(that + 1) != NUL) |
| 9666 | ++that; |
| 9667 | continue; |
| 9668 | } |
| 9669 | if (*that == '"' && *(that + 1) != NUL) |
| 9670 | { |
Bram Moolenaar | 15ff6c1 | 2006-09-15 18:18:09 +0000 | [diff] [blame] | 9671 | while (*++that && *that != '"') |
| 9672 | { |
| 9673 | /* skipping escaped characters in the string */ |
| 9674 | if (*that == '\\') |
| 9675 | { |
| 9676 | if (*++that == NUL) |
| 9677 | break; |
| 9678 | if (that[1] == NUL) |
| 9679 | { |
| 9680 | ++that; |
| 9681 | break; |
| 9682 | } |
| 9683 | } |
| 9684 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9685 | } |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9686 | if (*that == '(' || *that == '[') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9687 | ++parencount; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9688 | else if (*that == ')' || *that == ']') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9689 | --parencount; |
| 9690 | } |
| 9691 | if (parencount == 0) |
| 9692 | { |
| 9693 | amount = get_indent(); |
| 9694 | break; |
| 9695 | } |
| 9696 | } |
| 9697 | |
| 9698 | if (amount == -1) |
| 9699 | { |
| 9700 | curwin->w_cursor.lnum = pos->lnum; |
| 9701 | curwin->w_cursor.col = pos->col; |
| 9702 | col = pos->col; |
| 9703 | |
| 9704 | that = ml_get_curline(); |
| 9705 | |
| 9706 | if (vi_lisp && get_indent() == 0) |
| 9707 | amount = 2; |
| 9708 | else |
| 9709 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9710 | char_u *line = that; |
| 9711 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9712 | amount = 0; |
| 9713 | while (*that && col) |
| 9714 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9715 | amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9716 | col--; |
| 9717 | } |
| 9718 | |
| 9719 | /* |
| 9720 | * Some keywords require "body" indenting rules (the |
| 9721 | * non-standard-lisp ones are Scheme special forms): |
| 9722 | * |
| 9723 | * (let ((a 1)) instead (let ((a 1)) |
| 9724 | * (...)) of (...)) |
| 9725 | */ |
| 9726 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9727 | if (!vi_lisp && (*that == '(' || *that == '[') |
| 9728 | && lisp_match(that + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9729 | amount += 2; |
| 9730 | else |
| 9731 | { |
| 9732 | that++; |
| 9733 | amount++; |
| 9734 | firsttry = amount; |
| 9735 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 9736 | while (VIM_ISWHITE(*that)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9737 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9738 | amount += lbr_chartabsize(line, that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9739 | ++that; |
| 9740 | } |
| 9741 | |
| 9742 | if (*that && *that != ';') /* not a comment line */ |
| 9743 | { |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 9744 | /* test *that != '(' to accommodate first let/do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9745 | * argument if it is more than one line */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9746 | if (!vi_lisp && *that != '(' && *that != '[') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9747 | firsttry++; |
| 9748 | |
| 9749 | parencount = 0; |
| 9750 | quotecount = 0; |
| 9751 | |
| 9752 | if (vi_lisp |
| 9753 | || (*that != '"' |
| 9754 | && *that != '\'' |
| 9755 | && *that != '#' |
| 9756 | && (*that < '0' || *that > '9'))) |
| 9757 | { |
| 9758 | while (*that |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 9759 | && (!VIM_ISWHITE(*that) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9760 | || quotecount |
| 9761 | || parencount) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9762 | && (!((*that == '(' || *that == '[') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9763 | && !quotecount |
| 9764 | && !parencount |
| 9765 | && vi_lisp))) |
| 9766 | { |
| 9767 | if (*that == '"') |
| 9768 | quotecount = !quotecount; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9769 | if ((*that == '(' || *that == '[') |
| 9770 | && !quotecount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9771 | ++parencount; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9772 | if ((*that == ')' || *that == ']') |
| 9773 | && !quotecount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9774 | --parencount; |
| 9775 | if (*that == '\\' && *(that+1) != NUL) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9776 | amount += lbr_chartabsize_adv( |
| 9777 | line, &that, (colnr_T)amount); |
| 9778 | amount += lbr_chartabsize_adv( |
| 9779 | line, &that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9780 | } |
| 9781 | } |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 9782 | while (VIM_ISWHITE(*that)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9783 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9784 | amount += lbr_chartabsize( |
| 9785 | line, that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9786 | that++; |
| 9787 | } |
| 9788 | if (!*that || *that == ';') |
| 9789 | amount = firsttry; |
| 9790 | } |
| 9791 | } |
| 9792 | } |
| 9793 | } |
| 9794 | } |
| 9795 | else |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9796 | amount = 0; /* no matching '(' or '[' found, use zero indent */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9797 | |
| 9798 | curwin->w_cursor = realpos; |
| 9799 | |
| 9800 | return amount; |
| 9801 | } |
| 9802 | #endif /* FEAT_LISP */ |
| 9803 | |
| 9804 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9805 | prepare_to_exit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9806 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 9807 | #if defined(SIGHUP) && defined(SIG_IGN) |
| 9808 | /* Ignore SIGHUP, because a dropped connection causes a read error, which |
| 9809 | * makes Vim exit and then handling SIGHUP causes various reentrance |
| 9810 | * problems. */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 9811 | signal(SIGHUP, SIG_IGN); |
| 9812 | #endif |
| 9813 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9814 | #ifdef FEAT_GUI |
| 9815 | if (gui.in_use) |
| 9816 | { |
| 9817 | gui.dying = TRUE; |
| 9818 | out_trash(); /* trash any pending output */ |
| 9819 | } |
| 9820 | else |
| 9821 | #endif |
| 9822 | { |
| 9823 | windgoto((int)Rows - 1, 0); |
| 9824 | |
| 9825 | /* |
| 9826 | * Switch terminal mode back now, so messages end up on the "normal" |
| 9827 | * screen (if there are two screens). |
| 9828 | */ |
| 9829 | settmode(TMODE_COOK); |
Bram Moolenaar | cea912a | 2016-10-12 14:20:24 +0200 | [diff] [blame] | 9830 | stoptermcap(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9831 | out_flush(); |
| 9832 | } |
| 9833 | } |
| 9834 | |
| 9835 | /* |
| 9836 | * Preserve files and exit. |
| 9837 | * When called IObuff must contain a message. |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 9838 | * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe |
| 9839 | * functions, such as allocating memory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9840 | */ |
| 9841 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9842 | preserve_exit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9843 | { |
| 9844 | buf_T *buf; |
| 9845 | |
| 9846 | prepare_to_exit(); |
| 9847 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9848 | /* Setting this will prevent free() calls. That avoids calling free() |
| 9849 | * recursively when free() was invoked with a bad pointer. */ |
| 9850 | really_exiting = TRUE; |
| 9851 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9852 | out_str(IObuff); |
| 9853 | screen_start(); /* don't know where cursor is now */ |
| 9854 | out_flush(); |
| 9855 | |
| 9856 | ml_close_notmod(); /* close all not-modified buffers */ |
| 9857 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 9858 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9859 | { |
| 9860 | if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) |
| 9861 | { |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 9862 | OUT_STR("Vim: preserving files...\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9863 | screen_start(); /* don't know where cursor is now */ |
| 9864 | out_flush(); |
| 9865 | ml_sync_all(FALSE, FALSE); /* preserve all swap files */ |
| 9866 | break; |
| 9867 | } |
| 9868 | } |
| 9869 | |
| 9870 | ml_close_all(FALSE); /* close all memfiles, without deleting */ |
| 9871 | |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 9872 | OUT_STR("Vim: Finished.\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9873 | |
| 9874 | getout(1); |
| 9875 | } |
| 9876 | |
| 9877 | /* |
| 9878 | * return TRUE if "fname" exists. |
| 9879 | */ |
| 9880 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9881 | vim_fexists(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9882 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 9883 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9884 | |
| 9885 | if (mch_stat((char *)fname, &st)) |
| 9886 | return FALSE; |
| 9887 | return TRUE; |
| 9888 | } |
| 9889 | |
| 9890 | /* |
| 9891 | * Check for CTRL-C pressed, but only once in a while. |
| 9892 | * Should be used instead of ui_breakcheck() for functions that check for |
| 9893 | * each line in the file. Calling ui_breakcheck() each time takes too much |
| 9894 | * time, because it can be a system call. |
| 9895 | */ |
| 9896 | |
| 9897 | #ifndef BREAKCHECK_SKIP |
| 9898 | # ifdef FEAT_GUI /* assume the GUI only runs on fast computers */ |
| 9899 | # define BREAKCHECK_SKIP 200 |
| 9900 | # else |
| 9901 | # define BREAKCHECK_SKIP 32 |
| 9902 | # endif |
| 9903 | #endif |
| 9904 | |
| 9905 | static int breakcheck_count = 0; |
| 9906 | |
| 9907 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9908 | line_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9909 | { |
| 9910 | if (++breakcheck_count >= BREAKCHECK_SKIP) |
| 9911 | { |
| 9912 | breakcheck_count = 0; |
| 9913 | ui_breakcheck(); |
| 9914 | } |
| 9915 | } |
| 9916 | |
| 9917 | /* |
| 9918 | * Like line_breakcheck() but check 10 times less often. |
| 9919 | */ |
| 9920 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9921 | fast_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9922 | { |
| 9923 | if (++breakcheck_count >= BREAKCHECK_SKIP * 10) |
| 9924 | { |
| 9925 | breakcheck_count = 0; |
| 9926 | ui_breakcheck(); |
| 9927 | } |
| 9928 | } |
| 9929 | |
| 9930 | /* |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 9931 | * Invoke expand_wildcards() for one pattern. |
| 9932 | * Expand items like "%:h" before the expansion. |
| 9933 | * Returns OK or FAIL. |
| 9934 | */ |
| 9935 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9936 | expand_wildcards_eval( |
| 9937 | char_u **pat, /* pointer to input pattern */ |
| 9938 | int *num_file, /* resulting number of files */ |
| 9939 | char_u ***file, /* array of resulting files */ |
| 9940 | int flags) /* EW_DIR, etc. */ |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 9941 | { |
| 9942 | int ret = FAIL; |
| 9943 | char_u *eval_pat = NULL; |
| 9944 | char_u *exp_pat = *pat; |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 9945 | char *ignored_msg; |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 9946 | int usedlen; |
| 9947 | |
| 9948 | if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') |
| 9949 | { |
| 9950 | ++emsg_off; |
| 9951 | eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, |
| 9952 | NULL, &ignored_msg, NULL); |
| 9953 | --emsg_off; |
| 9954 | if (eval_pat != NULL) |
| 9955 | exp_pat = concat_str(eval_pat, exp_pat + usedlen); |
| 9956 | } |
| 9957 | |
| 9958 | if (exp_pat != NULL) |
| 9959 | ret = expand_wildcards(1, &exp_pat, num_file, file, flags); |
| 9960 | |
| 9961 | if (eval_pat != NULL) |
| 9962 | { |
| 9963 | vim_free(exp_pat); |
| 9964 | vim_free(eval_pat); |
| 9965 | } |
| 9966 | |
| 9967 | return ret; |
| 9968 | } |
| 9969 | |
| 9970 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9971 | * Expand wildcards. Calls gen_expand_wildcards() and removes files matching |
| 9972 | * 'wildignore'. |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9973 | * 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] | 9974 | */ |
| 9975 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9976 | expand_wildcards( |
| 9977 | int num_pat, /* number of input patterns */ |
| 9978 | char_u **pat, /* array of input patterns */ |
| 9979 | int *num_files, /* resulting number of files */ |
| 9980 | char_u ***files, /* array of resulting files */ |
| 9981 | int flags) /* EW_DIR, etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9982 | { |
| 9983 | int retval; |
| 9984 | int i, j; |
| 9985 | char_u *p; |
| 9986 | int non_suf_match; /* number without matching suffix */ |
| 9987 | |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9988 | retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9989 | |
| 9990 | /* When keeping all matches, return here */ |
Bram Moolenaar | 9e193ac | 2010-07-19 23:11:27 +0200 | [diff] [blame] | 9991 | if ((flags & EW_KEEPALL) || retval == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9992 | return retval; |
| 9993 | |
| 9994 | #ifdef FEAT_WILDIGN |
| 9995 | /* |
| 9996 | * Remove names that match 'wildignore'. |
| 9997 | */ |
| 9998 | if (*p_wig) |
| 9999 | { |
| 10000 | char_u *ffname; |
| 10001 | |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10002 | /* check all files in (*files)[] */ |
| 10003 | for (i = 0; i < *num_files; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10004 | { |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10005 | ffname = FullName_save((*files)[i], FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10006 | if (ffname == NULL) /* out of memory */ |
| 10007 | break; |
| 10008 | # ifdef VMS |
| 10009 | vms_remove_version(ffname); |
| 10010 | # endif |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10011 | if (match_file_list(p_wig, (*files)[i], ffname)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10012 | { |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 10013 | /* remove this matching file from the list */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10014 | vim_free((*files)[i]); |
| 10015 | for (j = i; j + 1 < *num_files; ++j) |
| 10016 | (*files)[j] = (*files)[j + 1]; |
| 10017 | --*num_files; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10018 | --i; |
| 10019 | } |
| 10020 | vim_free(ffname); |
| 10021 | } |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10022 | |
| 10023 | /* If the number of matches is now zero, we fail. */ |
| 10024 | if (*num_files == 0) |
| 10025 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 10026 | VIM_CLEAR(*files); |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10027 | return FAIL; |
| 10028 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10029 | } |
| 10030 | #endif |
| 10031 | |
| 10032 | /* |
| 10033 | * Move the names where 'suffixes' match to the end. |
| 10034 | */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10035 | if (*num_files > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10036 | { |
| 10037 | non_suf_match = 0; |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10038 | for (i = 0; i < *num_files; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10039 | { |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10040 | if (!match_suffix((*files)[i])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10041 | { |
| 10042 | /* |
| 10043 | * Move the name without matching suffix to the front |
| 10044 | * of the list. |
| 10045 | */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10046 | p = (*files)[i]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10047 | for (j = i; j > non_suf_match; --j) |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10048 | (*files)[j] = (*files)[j - 1]; |
| 10049 | (*files)[non_suf_match++] = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10050 | } |
| 10051 | } |
| 10052 | } |
| 10053 | |
| 10054 | return retval; |
| 10055 | } |
| 10056 | |
| 10057 | /* |
| 10058 | * Return TRUE if "fname" matches with an entry in 'suffixes'. |
| 10059 | */ |
| 10060 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10061 | match_suffix(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10062 | { |
| 10063 | int fnamelen, setsuflen; |
| 10064 | char_u *setsuf; |
| 10065 | #define MAXSUFLEN 30 /* maximum length of a file suffix */ |
| 10066 | char_u suf_buf[MAXSUFLEN]; |
| 10067 | |
| 10068 | fnamelen = (int)STRLEN(fname); |
| 10069 | setsuflen = 0; |
| 10070 | for (setsuf = p_su; *setsuf; ) |
| 10071 | { |
| 10072 | setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); |
Bram Moolenaar | 055a2ba | 2009-07-14 19:40:21 +0000 | [diff] [blame] | 10073 | if (setsuflen == 0) |
| 10074 | { |
| 10075 | char_u *tail = gettail(fname); |
| 10076 | |
| 10077 | /* empty entry: match name without a '.' */ |
| 10078 | if (vim_strchr(tail, '.') == NULL) |
| 10079 | { |
| 10080 | setsuflen = 1; |
| 10081 | break; |
| 10082 | } |
| 10083 | } |
| 10084 | else |
| 10085 | { |
| 10086 | if (fnamelen >= setsuflen |
| 10087 | && fnamencmp(suf_buf, fname + fnamelen - setsuflen, |
| 10088 | (size_t)setsuflen) == 0) |
| 10089 | break; |
| 10090 | setsuflen = 0; |
| 10091 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10092 | } |
| 10093 | return (setsuflen != 0); |
| 10094 | } |
| 10095 | |
| 10096 | #if !defined(NO_EXPANDPATH) || defined(PROTO) |
| 10097 | |
| 10098 | # ifdef VIM_BACKTICK |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 10099 | static int vim_backtick(char_u *p); |
| 10100 | static int expand_backtick(garray_T *gap, char_u *pat, int flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10101 | # endif |
| 10102 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10103 | # if defined(WIN3264) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10104 | /* |
| 10105 | * File name expansion code for MS-DOS, Win16 and Win32. It's here because |
| 10106 | * it's shared between these systems. |
| 10107 | */ |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10108 | # if defined(PROTO) |
| 10109 | # define _cdecl |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10110 | # else |
| 10111 | # ifdef __BORLANDC__ |
| 10112 | # define _cdecl _RTLENTRYF |
| 10113 | # endif |
| 10114 | # endif |
| 10115 | |
| 10116 | /* |
| 10117 | * comparison function for qsort in dos_expandpath() |
| 10118 | */ |
| 10119 | static int _cdecl |
| 10120 | pstrcmp(const void *a, const void *b) |
| 10121 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 10122 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10123 | } |
| 10124 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10125 | /* |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10126 | * Recursively expand one path component into all matching files and/or |
| 10127 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10128 | * Return the number of matches found. |
| 10129 | * "path" has backslashes before chars that are not to be expanded, starting |
| 10130 | * at "path[wildoff]". |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10131 | * Return the number of matches found. |
| 10132 | * NOTE: much of this is identical to unix_expandpath(), keep in sync! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10133 | */ |
| 10134 | static int |
| 10135 | dos_expandpath( |
| 10136 | garray_T *gap, |
| 10137 | char_u *path, |
| 10138 | int wildoff, |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10139 | int flags, /* EW_* flags */ |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 10140 | int didstar) /* expanded "**" once already */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10141 | { |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10142 | char_u *buf; |
| 10143 | char_u *path_end; |
| 10144 | char_u *p, *s, *e; |
| 10145 | int start_len = gap->ga_len; |
| 10146 | char_u *pat; |
| 10147 | regmatch_T regmatch; |
| 10148 | int starts_with_dot; |
| 10149 | int matches; |
| 10150 | int len; |
| 10151 | int starstar = FALSE; |
| 10152 | static int stardepth = 0; /* depth for "**" expansion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10153 | WIN32_FIND_DATA fb; |
| 10154 | HANDLE hFind = (HANDLE)0; |
| 10155 | # ifdef FEAT_MBYTE |
| 10156 | WIN32_FIND_DATAW wfb; |
| 10157 | WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */ |
| 10158 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10159 | char_u *matchname; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10160 | int ok; |
| 10161 | |
| 10162 | /* Expanding "**" may take a long time, check for CTRL-C. */ |
| 10163 | if (stardepth > 0) |
| 10164 | { |
| 10165 | ui_breakcheck(); |
| 10166 | if (got_int) |
| 10167 | return 0; |
| 10168 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10169 | |
Bram Moolenaar | 7314efd | 2015-10-31 15:32:52 +0100 | [diff] [blame] | 10170 | /* Make room for file name. When doing encoding conversion the actual |
| 10171 | * length may be quite a bit longer, thus use the maximum possible length. */ |
| 10172 | buf = alloc((int)MAXPATHL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10173 | if (buf == NULL) |
| 10174 | return 0; |
| 10175 | |
| 10176 | /* |
| 10177 | * Find the first part in the path name that contains a wildcard or a ~1. |
| 10178 | * Copy it into buf, including the preceding characters. |
| 10179 | */ |
| 10180 | p = buf; |
| 10181 | s = buf; |
| 10182 | e = NULL; |
| 10183 | path_end = path; |
| 10184 | while (*path_end != NUL) |
| 10185 | { |
| 10186 | /* May ignore a wildcard that has a backslash before it; it will |
| 10187 | * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ |
| 10188 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 10189 | *p++ = *path_end++; |
| 10190 | else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') |
| 10191 | { |
| 10192 | if (e != NULL) |
| 10193 | break; |
| 10194 | s = p + 1; |
| 10195 | } |
| 10196 | else if (path_end >= path + wildoff |
| 10197 | && vim_strchr((char_u *)"*?[~", *path_end) != NULL) |
| 10198 | e = p; |
Bram Moolenaar | 780d4c3 | 2016-03-25 17:21:19 +0100 | [diff] [blame] | 10199 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10200 | if (has_mbyte) |
| 10201 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10202 | len = (*mb_ptr2len)(path_end); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10203 | STRNCPY(p, path_end, len); |
| 10204 | p += len; |
| 10205 | path_end += len; |
| 10206 | } |
| 10207 | else |
Bram Moolenaar | 780d4c3 | 2016-03-25 17:21:19 +0100 | [diff] [blame] | 10208 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10209 | *p++ = *path_end++; |
| 10210 | } |
| 10211 | e = p; |
| 10212 | *e = NUL; |
| 10213 | |
| 10214 | /* now we have one wildcard component between s and e */ |
| 10215 | /* Remove backslashes between "wildoff" and the start of the wildcard |
| 10216 | * component. */ |
| 10217 | for (p = buf + wildoff; p < s; ++p) |
| 10218 | if (rem_backslash(p)) |
| 10219 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 10220 | STRMOVE(p, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10221 | --e; |
| 10222 | --s; |
| 10223 | } |
| 10224 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10225 | /* Check for "**" between "s" and "e". */ |
| 10226 | for (p = s; p < e; ++p) |
| 10227 | if (p[0] == '*' && p[1] == '*') |
| 10228 | starstar = TRUE; |
| 10229 | |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10230 | starts_with_dot = *s == '.'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10231 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 10232 | if (pat == NULL) |
| 10233 | { |
| 10234 | vim_free(buf); |
| 10235 | return 0; |
| 10236 | } |
| 10237 | |
| 10238 | /* compile the regexp into a program */ |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10239 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
Bram Moolenaar | b560983 | 2011-07-20 15:04:58 +0200 | [diff] [blame] | 10240 | ++emsg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10241 | regmatch.rm_ic = TRUE; /* Always ignore case */ |
| 10242 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10243 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
Bram Moolenaar | b560983 | 2011-07-20 15:04:58 +0200 | [diff] [blame] | 10244 | --emsg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10245 | vim_free(pat); |
| 10246 | |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10247 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10248 | { |
| 10249 | vim_free(buf); |
| 10250 | return 0; |
| 10251 | } |
| 10252 | |
| 10253 | /* remember the pattern or file name being looked for */ |
| 10254 | matchname = vim_strsave(s); |
| 10255 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10256 | /* If "**" is by itself, this is the first time we encounter it and more |
| 10257 | * is following then find matches without any directory. */ |
| 10258 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 10259 | && *path_end == '/') |
| 10260 | { |
| 10261 | STRCPY(s, path_end + 1); |
| 10262 | ++stardepth; |
| 10263 | (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 10264 | --stardepth; |
| 10265 | } |
| 10266 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10267 | /* Scan all files in the directory with "dir/ *.*" */ |
| 10268 | STRCPY(s, "*.*"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10269 | # ifdef FEAT_MBYTE |
| 10270 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 10271 | { |
| 10272 | /* The active codepage differs from 'encoding'. Attempt using the |
| 10273 | * wide function. If it fails because it is not implemented fall back |
| 10274 | * to the non-wide version (for Windows 98) */ |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 10275 | wn = enc_to_utf16(buf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10276 | if (wn != NULL) |
| 10277 | { |
| 10278 | hFind = FindFirstFileW(wn, &wfb); |
| 10279 | if (hFind == INVALID_HANDLE_VALUE |
| 10280 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 10281 | VIM_CLEAR(wn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10282 | } |
| 10283 | } |
| 10284 | |
| 10285 | if (wn == NULL) |
| 10286 | # endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 10287 | hFind = FindFirstFile((LPCSTR)buf, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10288 | ok = (hFind != INVALID_HANDLE_VALUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10289 | |
| 10290 | while (ok) |
| 10291 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10292 | # ifdef FEAT_MBYTE |
| 10293 | if (wn != NULL) |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 10294 | p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10295 | else |
| 10296 | # endif |
| 10297 | p = (char_u *)fb.cFileName; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10298 | /* Ignore entries starting with a dot, unless when asked for. Accept |
| 10299 | * all entries found with "matchname". */ |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10300 | if ((p[0] != '.' || starts_with_dot |
| 10301 | || ((flags & EW_DODOT) |
| 10302 | && p[1] != NUL && (p[1] != '.' || p[2] != NUL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10303 | && (matchname == NULL |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10304 | || (regmatch.regprog != NULL |
| 10305 | && vim_regexec(®match, p, (colnr_T)0)) |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 10306 | || ((flags & EW_NOTWILD) |
| 10307 | && fnamencmp(path + (s - buf), p, e - s) == 0))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10308 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10309 | STRCPY(s, p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10310 | len = (int)STRLEN(buf); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10311 | |
| 10312 | if (starstar && stardepth < 100) |
| 10313 | { |
| 10314 | /* For "**" in the pattern first go deeper in the tree to |
| 10315 | * find matches. */ |
| 10316 | STRCPY(buf + len, "/**"); |
| 10317 | STRCPY(buf + len + 3, path_end); |
| 10318 | ++stardepth; |
| 10319 | (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); |
| 10320 | --stardepth; |
| 10321 | } |
| 10322 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10323 | STRCPY(buf + len, path_end); |
| 10324 | if (mch_has_exp_wildcard(path_end)) |
| 10325 | { |
| 10326 | /* need to expand another component of the path */ |
| 10327 | /* remove backslashes for the remaining components only */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10328 | (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10329 | } |
| 10330 | else |
| 10331 | { |
| 10332 | /* no more wildcards, check if there is a match */ |
| 10333 | /* remove backslashes for the remaining components only */ |
| 10334 | if (*path_end != 0) |
| 10335 | backslash_halve(buf + len + 1); |
| 10336 | if (mch_getperm(buf) >= 0) /* add existing file */ |
| 10337 | addfile(gap, buf, flags); |
| 10338 | } |
| 10339 | } |
| 10340 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10341 | # ifdef FEAT_MBYTE |
| 10342 | if (wn != NULL) |
| 10343 | { |
| 10344 | vim_free(p); |
| 10345 | ok = FindNextFileW(hFind, &wfb); |
| 10346 | } |
| 10347 | else |
| 10348 | # endif |
| 10349 | ok = FindNextFile(hFind, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10350 | |
| 10351 | /* If no more matches and no match was used, try expanding the name |
| 10352 | * itself. Finds the long name of a short filename. */ |
| 10353 | if (!ok && matchname != NULL && gap->ga_len == start_len) |
| 10354 | { |
| 10355 | STRCPY(s, matchname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10356 | FindClose(hFind); |
| 10357 | # ifdef FEAT_MBYTE |
| 10358 | if (wn != NULL) |
| 10359 | { |
| 10360 | vim_free(wn); |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 10361 | wn = enc_to_utf16(buf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10362 | if (wn != NULL) |
| 10363 | hFind = FindFirstFileW(wn, &wfb); |
| 10364 | } |
| 10365 | if (wn == NULL) |
| 10366 | # endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 10367 | hFind = FindFirstFile((LPCSTR)buf, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10368 | ok = (hFind != INVALID_HANDLE_VALUE); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 10369 | VIM_CLEAR(matchname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10370 | } |
| 10371 | } |
| 10372 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10373 | FindClose(hFind); |
| 10374 | # ifdef FEAT_MBYTE |
| 10375 | vim_free(wn); |
| 10376 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10377 | vim_free(buf); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10378 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10379 | vim_free(matchname); |
| 10380 | |
| 10381 | matches = gap->ga_len - start_len; |
| 10382 | if (matches > 0) |
| 10383 | qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, |
| 10384 | sizeof(char_u *), pstrcmp); |
| 10385 | return matches; |
| 10386 | } |
| 10387 | |
| 10388 | int |
| 10389 | mch_expandpath( |
| 10390 | garray_T *gap, |
| 10391 | char_u *path, |
| 10392 | int flags) /* EW_* flags */ |
| 10393 | { |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10394 | return dos_expandpath(gap, path, 0, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10395 | } |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10396 | # endif /* WIN3264 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10397 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10398 | #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ |
| 10399 | || defined(PROTO) |
| 10400 | /* |
| 10401 | * Unix style wildcard expansion code. |
| 10402 | * It's here because it's used both for Unix and Mac. |
| 10403 | */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10404 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10405 | pstrcmp(const void *a, const void *b) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10406 | { |
| 10407 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
| 10408 | } |
| 10409 | |
| 10410 | /* |
| 10411 | * Recursively expand one path component into all matching files and/or |
| 10412 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
| 10413 | * "path" has backslashes before chars that are not to be expanded, starting |
| 10414 | * at "path + wildoff". |
| 10415 | * Return the number of matches found. |
| 10416 | * NOTE: much of this is identical to dos_expandpath(), keep in sync! |
| 10417 | */ |
| 10418 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10419 | unix_expandpath( |
| 10420 | garray_T *gap, |
| 10421 | char_u *path, |
| 10422 | int wildoff, |
| 10423 | int flags, /* EW_* flags */ |
| 10424 | int didstar) /* expanded "**" once already */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10425 | { |
| 10426 | char_u *buf; |
| 10427 | char_u *path_end; |
| 10428 | char_u *p, *s, *e; |
| 10429 | int start_len = gap->ga_len; |
| 10430 | char_u *pat; |
| 10431 | regmatch_T regmatch; |
| 10432 | int starts_with_dot; |
| 10433 | int matches; |
| 10434 | int len; |
| 10435 | int starstar = FALSE; |
| 10436 | static int stardepth = 0; /* depth for "**" expansion */ |
| 10437 | |
| 10438 | DIR *dirp; |
| 10439 | struct dirent *dp; |
| 10440 | |
| 10441 | /* Expanding "**" may take a long time, check for CTRL-C. */ |
| 10442 | if (stardepth > 0) |
| 10443 | { |
| 10444 | ui_breakcheck(); |
| 10445 | if (got_int) |
| 10446 | return 0; |
| 10447 | } |
| 10448 | |
| 10449 | /* make room for file name */ |
| 10450 | buf = alloc((int)STRLEN(path) + BASENAMELEN + 5); |
| 10451 | if (buf == NULL) |
| 10452 | return 0; |
| 10453 | |
| 10454 | /* |
| 10455 | * Find the first part in the path name that contains a wildcard. |
Bram Moolenaar | 2d0b92f | 2012-04-30 21:09:43 +0200 | [diff] [blame] | 10456 | * 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] | 10457 | * Copy it into "buf", including the preceding characters. |
| 10458 | */ |
| 10459 | p = buf; |
| 10460 | s = buf; |
| 10461 | e = NULL; |
| 10462 | path_end = path; |
| 10463 | while (*path_end != NUL) |
| 10464 | { |
| 10465 | /* May ignore a wildcard that has a backslash before it; it will |
| 10466 | * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ |
| 10467 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 10468 | *p++ = *path_end++; |
| 10469 | else if (*path_end == '/') |
| 10470 | { |
| 10471 | if (e != NULL) |
| 10472 | break; |
| 10473 | s = p + 1; |
| 10474 | } |
| 10475 | else if (path_end >= path + wildoff |
Bram Moolenaar | 2d0b92f | 2012-04-30 21:09:43 +0200 | [diff] [blame] | 10476 | && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 10477 | || (!p_fic && (flags & EW_ICASE) |
| 10478 | && isalpha(PTR2CHAR(path_end))))) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10479 | e = p; |
| 10480 | #ifdef FEAT_MBYTE |
| 10481 | if (has_mbyte) |
| 10482 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10483 | len = (*mb_ptr2len)(path_end); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10484 | STRNCPY(p, path_end, len); |
| 10485 | p += len; |
| 10486 | path_end += len; |
| 10487 | } |
| 10488 | else |
| 10489 | #endif |
| 10490 | *p++ = *path_end++; |
| 10491 | } |
| 10492 | e = p; |
| 10493 | *e = NUL; |
| 10494 | |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 10495 | /* Now we have one wildcard component between "s" and "e". */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10496 | /* Remove backslashes between "wildoff" and the start of the wildcard |
| 10497 | * component. */ |
| 10498 | for (p = buf + wildoff; p < s; ++p) |
| 10499 | if (rem_backslash(p)) |
| 10500 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 10501 | STRMOVE(p, p + 1); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10502 | --e; |
| 10503 | --s; |
| 10504 | } |
| 10505 | |
| 10506 | /* Check for "**" between "s" and "e". */ |
| 10507 | for (p = s; p < e; ++p) |
| 10508 | if (p[0] == '*' && p[1] == '*') |
| 10509 | starstar = TRUE; |
| 10510 | |
| 10511 | /* convert the file pattern to a regexp pattern */ |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10512 | starts_with_dot = *s == '.'; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10513 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 10514 | if (pat == NULL) |
| 10515 | { |
| 10516 | vim_free(buf); |
| 10517 | return 0; |
| 10518 | } |
| 10519 | |
| 10520 | /* compile the regexp into a program */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 10521 | if (flags & EW_ICASE) |
| 10522 | regmatch.rm_ic = TRUE; /* 'wildignorecase' set */ |
| 10523 | else |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 10524 | regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */ |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10525 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 10526 | ++emsg_silent; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10527 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10528 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 10529 | --emsg_silent; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10530 | vim_free(pat); |
| 10531 | |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10532 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10533 | { |
| 10534 | vim_free(buf); |
| 10535 | return 0; |
| 10536 | } |
| 10537 | |
| 10538 | /* If "**" is by itself, this is the first time we encounter it and more |
| 10539 | * is following then find matches without any directory. */ |
| 10540 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 10541 | && *path_end == '/') |
| 10542 | { |
| 10543 | STRCPY(s, path_end + 1); |
| 10544 | ++stardepth; |
| 10545 | (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 10546 | --stardepth; |
| 10547 | } |
| 10548 | |
| 10549 | /* open the directory for scanning */ |
| 10550 | *s = NUL; |
| 10551 | dirp = opendir(*buf == NUL ? "." : (char *)buf); |
| 10552 | |
| 10553 | /* Find all matching entries */ |
| 10554 | if (dirp != NULL) |
| 10555 | { |
| 10556 | for (;;) |
| 10557 | { |
| 10558 | dp = readdir(dirp); |
| 10559 | if (dp == NULL) |
| 10560 | break; |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10561 | if ((dp->d_name[0] != '.' || starts_with_dot |
| 10562 | || ((flags & EW_DODOT) |
| 10563 | && dp->d_name[1] != NUL |
| 10564 | && (dp->d_name[1] != '.' || dp->d_name[2] != NUL))) |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10565 | && ((regmatch.regprog != NULL && vim_regexec(®match, |
| 10566 | (char_u *)dp->d_name, (colnr_T)0)) |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 10567 | || ((flags & EW_NOTWILD) |
| 10568 | && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0))) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10569 | { |
| 10570 | STRCPY(s, dp->d_name); |
| 10571 | len = STRLEN(buf); |
| 10572 | |
| 10573 | if (starstar && stardepth < 100) |
| 10574 | { |
| 10575 | /* For "**" in the pattern first go deeper in the tree to |
| 10576 | * find matches. */ |
| 10577 | STRCPY(buf + len, "/**"); |
| 10578 | STRCPY(buf + len + 3, path_end); |
| 10579 | ++stardepth; |
| 10580 | (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); |
| 10581 | --stardepth; |
| 10582 | } |
| 10583 | |
| 10584 | STRCPY(buf + len, path_end); |
| 10585 | if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */ |
| 10586 | { |
| 10587 | /* need to expand another component of the path */ |
| 10588 | /* remove backslashes for the remaining components only */ |
| 10589 | (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); |
| 10590 | } |
| 10591 | else |
| 10592 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 10593 | stat_T sb; |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 10594 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10595 | /* no more wildcards, check if there is a match */ |
| 10596 | /* remove backslashes for the remaining components only */ |
| 10597 | if (*path_end != NUL) |
| 10598 | backslash_halve(buf + len + 1); |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 10599 | /* add existing file or symbolic link */ |
Bram Moolenaar | ab11a59 | 2015-03-06 22:00:11 +0100 | [diff] [blame] | 10600 | if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0 |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 10601 | : mch_getperm(buf) >= 0) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10602 | { |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 10603 | #ifdef MACOS_CONVERT |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10604 | size_t precomp_len = STRLEN(buf)+1; |
| 10605 | char_u *precomp_buf = |
| 10606 | mac_precompose_path(buf, precomp_len, &precomp_len); |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 10607 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10608 | if (precomp_buf) |
| 10609 | { |
| 10610 | mch_memmove(buf, precomp_buf, precomp_len); |
| 10611 | vim_free(precomp_buf); |
| 10612 | } |
| 10613 | #endif |
| 10614 | addfile(gap, buf, flags); |
| 10615 | } |
| 10616 | } |
| 10617 | } |
| 10618 | } |
| 10619 | |
| 10620 | closedir(dirp); |
| 10621 | } |
| 10622 | |
| 10623 | vim_free(buf); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10624 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10625 | |
| 10626 | matches = gap->ga_len - start_len; |
| 10627 | if (matches > 0) |
| 10628 | qsort(((char_u **)gap->ga_data) + start_len, matches, |
| 10629 | sizeof(char_u *), pstrcmp); |
| 10630 | return matches; |
| 10631 | } |
| 10632 | #endif |
| 10633 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10634 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10635 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10636 | * Moves "*psep" back to the previous path separator in "path". |
| 10637 | * Returns FAIL is "*psep" ends up at the beginning of "path". |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10638 | */ |
| 10639 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10640 | find_previous_pathsep(char_u *path, char_u **psep) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10641 | { |
| 10642 | /* skip the current separator */ |
| 10643 | if (*psep > path && vim_ispathsep(**psep)) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10644 | --*psep; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10645 | |
| 10646 | /* find the previous separator */ |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10647 | while (*psep > path) |
| 10648 | { |
| 10649 | if (vim_ispathsep(**psep)) |
| 10650 | return OK; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 10651 | MB_PTR_BACK(path, *psep); |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10652 | } |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10653 | |
| 10654 | return FAIL; |
| 10655 | } |
| 10656 | |
| 10657 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10658 | * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap". |
| 10659 | * "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] | 10660 | */ |
| 10661 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10662 | is_unique(char_u *maybe_unique, garray_T *gap, int i) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10663 | { |
| 10664 | int j; |
| 10665 | int candidate_len; |
| 10666 | int other_path_len; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10667 | char_u **other_paths = (char_u **)gap->ga_data; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10668 | char_u *rival; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10669 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10670 | for (j = 0; j < gap->ga_len; j++) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10671 | { |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10672 | if (j == i) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10673 | continue; /* don't compare it with itself */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10674 | |
Bram Moolenaar | 624c7aa | 2010-07-16 20:38:52 +0200 | [diff] [blame] | 10675 | candidate_len = (int)STRLEN(maybe_unique); |
| 10676 | other_path_len = (int)STRLEN(other_paths[j]); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10677 | if (other_path_len < candidate_len) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10678 | continue; /* it's different when it's shorter */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10679 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10680 | rival = other_paths[j] + other_path_len - candidate_len; |
Bram Moolenaar | da9836c | 2010-08-16 21:53:27 +0200 | [diff] [blame] | 10681 | if (fnamecmp(maybe_unique, rival) == 0 |
| 10682 | && (rival == other_paths[j] || vim_ispathsep(*(rival - 1)))) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10683 | return FALSE; /* match */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10684 | } |
| 10685 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10686 | return TRUE; /* no match found */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10687 | } |
| 10688 | |
| 10689 | /* |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 10690 | * 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] | 10691 | * paths are expanded to their equivalent fullpath. This includes the "." |
| 10692 | * (relative to current buffer directory) and empty path (relative to current |
| 10693 | * directory) notations. |
| 10694 | * |
| 10695 | * TODO: handle upward search (;) and path limiter (**N) notations by |
| 10696 | * expanding each into their equivalent path(s). |
| 10697 | */ |
| 10698 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10699 | expand_path_option(char_u *curdir, garray_T *gap) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10700 | { |
| 10701 | char_u *path_option = *curbuf->b_p_path == NUL |
| 10702 | ? p_path : curbuf->b_p_path; |
| 10703 | char_u *buf; |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10704 | char_u *p; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10705 | int len; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10706 | |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10707 | if ((buf = alloc((int)MAXPATHL)) == NULL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10708 | return; |
| 10709 | |
| 10710 | while (*path_option != NUL) |
| 10711 | { |
| 10712 | copy_option_part(&path_option, buf, MAXPATHL, " ,"); |
| 10713 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10714 | if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1]))) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10715 | { |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10716 | /* Relative to current buffer: |
| 10717 | * "/path/file" + "." -> "/path/" |
| 10718 | * "/path/file" + "./subdir" -> "/path/subdir" */ |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10719 | if (curbuf->b_ffname == NULL) |
| 10720 | continue; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10721 | p = gettail(curbuf->b_ffname); |
| 10722 | len = (int)(p - curbuf->b_ffname); |
| 10723 | if (len + (int)STRLEN(buf) >= MAXPATHL) |
| 10724 | continue; |
| 10725 | if (buf[1] == NUL) |
| 10726 | buf[len] = NUL; |
| 10727 | else |
| 10728 | STRMOVE(buf + len, buf + 2); |
| 10729 | mch_memmove(buf, curbuf->b_ffname, len); |
| 10730 | simplify_filename(buf); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10731 | } |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10732 | else if (buf[0] == NUL) |
| 10733 | /* relative to current directory */ |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10734 | STRCPY(buf, curdir); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 10735 | else if (path_with_url(buf)) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10736 | /* URL can't be used here */ |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 10737 | continue; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10738 | else if (!mch_isFullName(buf)) |
| 10739 | { |
| 10740 | /* Expand relative path to their full path equivalent */ |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10741 | len = (int)STRLEN(curdir); |
| 10742 | if (len + (int)STRLEN(buf) + 3 > MAXPATHL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10743 | continue; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10744 | STRMOVE(buf + len + 1, buf); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10745 | STRCPY(buf, curdir); |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10746 | buf[len] = PATHSEP; |
Bram Moolenaar | 57adda1 | 2010-08-03 22:11:29 +0200 | [diff] [blame] | 10747 | simplify_filename(buf); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10748 | } |
| 10749 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10750 | if (ga_grow(gap, 1) == FAIL) |
| 10751 | break; |
Bram Moolenaar | 811fe63 | 2013-04-24 17:34:20 +0200 | [diff] [blame] | 10752 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10753 | # if defined(MSWIN) |
Bram Moolenaar | 811fe63 | 2013-04-24 17:34:20 +0200 | [diff] [blame] | 10754 | /* Avoid the path ending in a backslash, it fails when a comma is |
| 10755 | * appended. */ |
Bram Moolenaar | 4e0d974 | 2013-05-04 03:40:27 +0200 | [diff] [blame] | 10756 | len = (int)STRLEN(buf); |
Bram Moolenaar | 811fe63 | 2013-04-24 17:34:20 +0200 | [diff] [blame] | 10757 | if (buf[len - 1] == '\\') |
| 10758 | buf[len - 1] = '/'; |
| 10759 | # endif |
| 10760 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10761 | p = vim_strsave(buf); |
| 10762 | if (p == NULL) |
| 10763 | break; |
| 10764 | ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10765 | } |
| 10766 | |
| 10767 | vim_free(buf); |
| 10768 | } |
| 10769 | |
| 10770 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10771 | * Returns a pointer to the file or directory name in "fname" that matches the |
| 10772 | * 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] | 10773 | * |
| 10774 | * path: /foo/bar/baz |
| 10775 | * fname: /foo/bar/baz/quux.txt |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10776 | * returns: ^this |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10777 | */ |
| 10778 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10779 | get_path_cutoff(char_u *fname, garray_T *gap) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10780 | { |
| 10781 | int i; |
| 10782 | int maxlen = 0; |
| 10783 | char_u **path_part = (char_u **)gap->ga_data; |
| 10784 | char_u *cutoff = NULL; |
| 10785 | |
| 10786 | for (i = 0; i < gap->ga_len; i++) |
| 10787 | { |
| 10788 | int j = 0; |
| 10789 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10790 | while ((fname[j] == path_part[i][j] |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10791 | # if defined(MSWIN) |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10792 | || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j])) |
| 10793 | #endif |
| 10794 | ) && fname[j] != NUL && path_part[i][j] != NUL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10795 | j++; |
| 10796 | if (j > maxlen) |
| 10797 | { |
| 10798 | maxlen = j; |
| 10799 | cutoff = &fname[j]; |
| 10800 | } |
| 10801 | } |
| 10802 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10803 | /* skip to the file or directory name */ |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10804 | if (cutoff != NULL) |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10805 | while (vim_ispathsep(*cutoff)) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 10806 | MB_PTR_ADV(cutoff); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10807 | |
| 10808 | return cutoff; |
| 10809 | } |
| 10810 | |
| 10811 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10812 | * Sorts, removes duplicates and modifies all the fullpath names in "gap" so |
| 10813 | * that they are unique with respect to each other while conserving the part |
| 10814 | * 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] | 10815 | */ |
| 10816 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10817 | uniquefy_paths(garray_T *gap, char_u *pattern) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10818 | { |
Bram Moolenaar | cb9d45c | 2010-07-20 18:10:15 +0200 | [diff] [blame] | 10819 | int i; |
| 10820 | int len; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10821 | char_u **fnames = (char_u **)gap->ga_data; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10822 | int sort_again = FALSE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10823 | char_u *pat; |
| 10824 | char_u *file_pattern; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10825 | char_u *curdir; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10826 | regmatch_T regmatch; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10827 | garray_T path_ga; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10828 | char_u **in_curdir = NULL; |
| 10829 | char_u *short_name; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10830 | |
Bram Moolenaar | cb9d45c | 2010-07-20 18:10:15 +0200 | [diff] [blame] | 10831 | remove_duplicates(gap); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10832 | ga_init2(&path_ga, (int)sizeof(char_u *), 1); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10833 | |
| 10834 | /* |
| 10835 | * We need to prepend a '*' at the beginning of file_pattern so that the |
| 10836 | * regex matches anywhere in the path. FIXME: is this valid for all |
Bram Moolenaar | b31e438 | 2010-07-24 16:01:56 +0200 | [diff] [blame] | 10837 | * possible patterns? |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10838 | */ |
Bram Moolenaar | 624c7aa | 2010-07-16 20:38:52 +0200 | [diff] [blame] | 10839 | len = (int)STRLEN(pattern); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10840 | file_pattern = alloc(len + 2); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10841 | if (file_pattern == NULL) |
| 10842 | return; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10843 | file_pattern[0] = '*'; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10844 | file_pattern[1] = NUL; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10845 | STRCAT(file_pattern, pattern); |
| 10846 | pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE); |
| 10847 | vim_free(file_pattern); |
Bram Moolenaar | b31e438 | 2010-07-24 16:01:56 +0200 | [diff] [blame] | 10848 | if (pat == NULL) |
| 10849 | return; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10850 | |
Bram Moolenaar | b31e438 | 2010-07-24 16:01:56 +0200 | [diff] [blame] | 10851 | regmatch.rm_ic = TRUE; /* always ignore case */ |
| 10852 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); |
| 10853 | vim_free(pat); |
| 10854 | if (regmatch.regprog == NULL) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10855 | return; |
| 10856 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10857 | if ((curdir = alloc((int)(MAXPATHL))) == NULL) |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10858 | goto theend; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10859 | mch_dirname(curdir, MAXPATHL); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10860 | expand_path_option(curdir, &path_ga); |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10861 | |
| 10862 | in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *)); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10863 | if (in_curdir == NULL) |
| 10864 | goto theend; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10865 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10866 | for (i = 0; i < gap->ga_len && !got_int; i++) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10867 | { |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10868 | char_u *path = fnames[i]; |
| 10869 | int is_in_curdir; |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10870 | char_u *dir_end = gettail_dir(path); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10871 | char_u *pathsep_p; |
| 10872 | char_u *path_cutoff; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10873 | |
Bram Moolenaar | 624c7aa | 2010-07-16 20:38:52 +0200 | [diff] [blame] | 10874 | len = (int)STRLEN(path); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10875 | is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0 |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10876 | && curdir[dir_end - path] == NUL; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10877 | if (is_in_curdir) |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10878 | in_curdir[i] = vim_strsave(path); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10879 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10880 | /* Shorten the filename while maintaining its uniqueness */ |
| 10881 | path_cutoff = get_path_cutoff(path, &path_ga); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10882 | |
Bram Moolenaar | 73d4e4c | 2016-08-27 21:55:13 +0200 | [diff] [blame] | 10883 | /* Don't assume all files can be reached without path when search |
| 10884 | * pattern starts with star star slash, so only remove path_cutoff |
| 10885 | * when possible. */ |
| 10886 | if (pattern[0] == '*' && pattern[1] == '*' |
| 10887 | && vim_ispathsep_nocolon(pattern[2]) |
| 10888 | && path_cutoff != NULL |
| 10889 | && vim_regexec(®match, path_cutoff, (colnr_T)0) |
| 10890 | && is_unique(path_cutoff, gap, i)) |
| 10891 | { |
| 10892 | sort_again = TRUE; |
| 10893 | mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1); |
| 10894 | } |
| 10895 | else |
| 10896 | { |
| 10897 | /* Here all files can be reached without path, so get shortest |
| 10898 | * unique path. We start at the end of the path. */ |
| 10899 | pathsep_p = path + len - 1; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10900 | |
Bram Moolenaar | 73d4e4c | 2016-08-27 21:55:13 +0200 | [diff] [blame] | 10901 | while (find_previous_pathsep(path, &pathsep_p)) |
| 10902 | if (vim_regexec(®match, pathsep_p + 1, (colnr_T)0) |
| 10903 | && is_unique(pathsep_p + 1, gap, i) |
| 10904 | && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff) |
| 10905 | { |
| 10906 | sort_again = TRUE; |
| 10907 | mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p)); |
| 10908 | break; |
| 10909 | } |
| 10910 | } |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10911 | |
| 10912 | if (mch_isFullName(path)) |
| 10913 | { |
| 10914 | /* |
| 10915 | * Last resort: shorten relative to curdir if possible. |
| 10916 | * 'possible' means: |
| 10917 | * 1. It is under the current directory. |
| 10918 | * 2. The result is actually shorter than the original. |
| 10919 | * |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10920 | * Before curdir After |
| 10921 | * /foo/bar/file.txt /foo/bar ./file.txt |
| 10922 | * c:\foo\bar\file.txt c:\foo\bar .\file.txt |
| 10923 | * /file.txt / /file.txt |
| 10924 | * c:\file.txt c:\ .\file.txt |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10925 | */ |
| 10926 | short_name = shorten_fname(path, curdir); |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10927 | if (short_name != NULL && short_name > path + 1 |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10928 | #if defined(MSWIN) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10929 | /* On windows, |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10930 | * shorten_fname("c:\a\a.txt", "c:\a\b") |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10931 | * returns "\a\a.txt", which is not really the short |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10932 | * name, hence: */ |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10933 | && !vim_ispathsep(*short_name) |
| 10934 | #endif |
| 10935 | ) |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10936 | { |
| 10937 | STRCPY(path, "."); |
| 10938 | add_pathsep(path); |
Bram Moolenaar | cda000e | 2010-08-14 13:34:39 +0200 | [diff] [blame] | 10939 | STRMOVE(path + STRLEN(path), short_name); |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10940 | } |
| 10941 | } |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10942 | ui_breakcheck(); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10943 | } |
| 10944 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10945 | /* Shorten filenames in /in/current/directory/{filename} */ |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10946 | for (i = 0; i < gap->ga_len && !got_int; i++) |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10947 | { |
| 10948 | char_u *rel_path; |
| 10949 | char_u *path = in_curdir[i]; |
| 10950 | |
| 10951 | if (path == NULL) |
| 10952 | continue; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10953 | |
| 10954 | /* If the {filename} is not unique, change it to ./{filename}. |
| 10955 | * Else reduce it to {filename} */ |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10956 | short_name = shorten_fname(path, curdir); |
| 10957 | if (short_name == NULL) |
| 10958 | short_name = path; |
| 10959 | if (is_unique(short_name, gap, i)) |
| 10960 | { |
| 10961 | STRCPY(fnames[i], short_name); |
| 10962 | continue; |
| 10963 | } |
| 10964 | |
| 10965 | rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2)); |
| 10966 | if (rel_path == NULL) |
| 10967 | goto theend; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10968 | STRCPY(rel_path, "."); |
| 10969 | add_pathsep(rel_path); |
| 10970 | STRCAT(rel_path, short_name); |
| 10971 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10972 | vim_free(fnames[i]); |
| 10973 | fnames[i] = rel_path; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10974 | sort_again = TRUE; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10975 | ui_breakcheck(); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10976 | } |
| 10977 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10978 | theend: |
| 10979 | vim_free(curdir); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10980 | if (in_curdir != NULL) |
| 10981 | { |
| 10982 | for (i = 0; i < gap->ga_len; i++) |
| 10983 | vim_free(in_curdir[i]); |
| 10984 | vim_free(in_curdir); |
| 10985 | } |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10986 | ga_clear_strings(&path_ga); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10987 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10988 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10989 | if (sort_again) |
Bram Moolenaar | cb9d45c | 2010-07-20 18:10:15 +0200 | [diff] [blame] | 10990 | remove_duplicates(gap); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10991 | } |
| 10992 | |
| 10993 | /* |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10994 | * Calls globpath() with 'path' values for the given pattern and stores the |
| 10995 | * result in "gap". |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10996 | * Returns the total number of matches. |
| 10997 | */ |
| 10998 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10999 | expand_in_path( |
| 11000 | garray_T *gap, |
| 11001 | char_u *pattern, |
| 11002 | int flags) /* EW_* flags */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11003 | { |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 11004 | char_u *curdir; |
| 11005 | garray_T path_ga; |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 11006 | char_u *paths = NULL; |
Bram Moolenaar | 8a37b03 | 2018-02-03 20:43:08 +0100 | [diff] [blame] | 11007 | int glob_flags = 0; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11008 | |
Bram Moolenaar | 7f0f621 | 2010-08-03 22:21:00 +0200 | [diff] [blame] | 11009 | if ((curdir = alloc((unsigned)MAXPATHL)) == NULL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 11010 | return 0; |
| 11011 | mch_dirname(curdir, MAXPATHL); |
| 11012 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 11013 | ga_init2(&path_ga, (int)sizeof(char_u *), 1); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 11014 | expand_path_option(curdir, &path_ga); |
| 11015 | vim_free(curdir); |
Bram Moolenaar | 006d2b0 | 2010-08-04 12:39:44 +0200 | [diff] [blame] | 11016 | if (path_ga.ga_len == 0) |
| 11017 | return 0; |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11018 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 11019 | paths = ga_concat_strings(&path_ga, ","); |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11020 | ga_clear_strings(&path_ga); |
| 11021 | if (paths == NULL) |
Bram Moolenaar | 7f0f621 | 2010-08-03 22:21:00 +0200 | [diff] [blame] | 11022 | return 0; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 11023 | |
Bram Moolenaar | 8a37b03 | 2018-02-03 20:43:08 +0100 | [diff] [blame] | 11024 | if (flags & EW_ICASE) |
| 11025 | glob_flags |= WILD_ICASE; |
| 11026 | if (flags & EW_ADDSLASH) |
| 11027 | glob_flags |= WILD_ADD_SLASH; |
| 11028 | globpath(paths, pattern, gap, glob_flags); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 11029 | vim_free(paths); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11030 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 11031 | return gap->ga_len; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11032 | } |
| 11033 | #endif |
| 11034 | |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 11035 | #if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 11036 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 11037 | * Sort "gap" and remove duplicate entries. "gap" is expected to contain a |
| 11038 | * list of file names in allocated memory. |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 11039 | */ |
| 11040 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11041 | remove_duplicates(garray_T *gap) |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 11042 | { |
| 11043 | int i; |
| 11044 | int j; |
| 11045 | char_u **fnames = (char_u **)gap->ga_data; |
| 11046 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 11047 | sort_strings(fnames, gap->ga_len); |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 11048 | for (i = gap->ga_len - 1; i > 0; --i) |
| 11049 | if (fnamecmp(fnames[i - 1], fnames[i]) == 0) |
| 11050 | { |
| 11051 | vim_free(fnames[i]); |
| 11052 | for (j = i + 1; j < gap->ga_len; ++j) |
| 11053 | fnames[j - 1] = fnames[j]; |
| 11054 | --gap->ga_len; |
| 11055 | } |
| 11056 | } |
| 11057 | #endif |
| 11058 | |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11059 | /* |
| 11060 | * Return TRUE if "p" contains what looks like an environment variable. |
| 11061 | * Allowing for escaping. |
| 11062 | */ |
| 11063 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11064 | has_env_var(char_u *p) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11065 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 11066 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11067 | { |
| 11068 | if (*p == '\\' && p[1] != NUL) |
| 11069 | ++p; |
| 11070 | else if (vim_strchr((char_u *) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 11071 | #if defined(MSWIN) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11072 | "$%" |
| 11073 | #else |
| 11074 | "$" |
| 11075 | #endif |
| 11076 | , *p) != NULL) |
| 11077 | return TRUE; |
| 11078 | } |
| 11079 | return FALSE; |
| 11080 | } |
| 11081 | |
| 11082 | #ifdef SPECIAL_WILDCHAR |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11083 | /* |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 11084 | * Return TRUE if "p" contains a special wildcard character, one that Vim |
| 11085 | * cannot expand, requires using a shell. |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11086 | */ |
| 11087 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11088 | has_special_wildchar(char_u *p) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11089 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 11090 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11091 | { |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 11092 | /* Allow for escaping. */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11093 | if (*p == '\\' && p[1] != NUL) |
| 11094 | ++p; |
| 11095 | else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL) |
| 11096 | return TRUE; |
| 11097 | } |
| 11098 | return FALSE; |
| 11099 | } |
| 11100 | #endif |
| 11101 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11102 | /* |
| 11103 | * Generic wildcard expansion code. |
| 11104 | * |
| 11105 | * Characters in "pat" that should not be expanded must be preceded with a |
| 11106 | * backslash. E.g., "/path\ with\ spaces/my\*star*" |
| 11107 | * |
| 11108 | * Return FAIL when no single file was found. In this case "num_file" is not |
| 11109 | * set, and "file" may contain an error message. |
| 11110 | * Return OK when some files found. "num_file" is set to the number of |
| 11111 | * matches, "file" to the array of matches. Call FreeWild() later. |
| 11112 | */ |
| 11113 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11114 | gen_expand_wildcards( |
| 11115 | int num_pat, /* number of input patterns */ |
| 11116 | char_u **pat, /* array of input patterns */ |
| 11117 | int *num_file, /* resulting number of files */ |
| 11118 | char_u ***file, /* array of resulting files */ |
| 11119 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11120 | { |
| 11121 | int i; |
| 11122 | garray_T ga; |
| 11123 | char_u *p; |
| 11124 | static int recursive = FALSE; |
| 11125 | int add_pat; |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11126 | int retval = OK; |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11127 | #if defined(FEAT_SEARCHPATH) |
| 11128 | int did_expand_in_path = FALSE; |
| 11129 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11130 | |
| 11131 | /* |
| 11132 | * expand_env() is called to expand things like "~user". If this fails, |
| 11133 | * it calls ExpandOne(), which brings us back here. In this case, always |
| 11134 | * call the machine specific expansion function, if possible. Otherwise, |
| 11135 | * return FAIL. |
| 11136 | */ |
| 11137 | if (recursive) |
| 11138 | #ifdef SPECIAL_WILDCHAR |
| 11139 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 11140 | #else |
| 11141 | return FAIL; |
| 11142 | #endif |
| 11143 | |
| 11144 | #ifdef SPECIAL_WILDCHAR |
| 11145 | /* |
| 11146 | * If there are any special wildcard characters which we cannot handle |
| 11147 | * here, call machine specific function for all the expansion. This |
| 11148 | * avoids starting the shell for each argument separately. |
| 11149 | * For `=expr` do use the internal function. |
| 11150 | */ |
| 11151 | for (i = 0; i < num_pat; i++) |
| 11152 | { |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11153 | if (has_special_wildchar(pat[i]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11154 | # ifdef VIM_BACKTICK |
| 11155 | && !(vim_backtick(pat[i]) && pat[i][1] == '=') |
| 11156 | # endif |
| 11157 | ) |
| 11158 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 11159 | } |
| 11160 | #endif |
| 11161 | |
| 11162 | recursive = TRUE; |
| 11163 | |
| 11164 | /* |
| 11165 | * The matching file names are stored in a growarray. Init it empty. |
| 11166 | */ |
| 11167 | ga_init2(&ga, (int)sizeof(char_u *), 30); |
| 11168 | |
| 11169 | for (i = 0; i < num_pat; ++i) |
| 11170 | { |
| 11171 | add_pat = -1; |
| 11172 | p = pat[i]; |
| 11173 | |
| 11174 | #ifdef VIM_BACKTICK |
| 11175 | if (vim_backtick(p)) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11176 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11177 | add_pat = expand_backtick(&ga, p, flags); |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11178 | if (add_pat == -1) |
| 11179 | retval = FAIL; |
| 11180 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11181 | else |
| 11182 | #endif |
| 11183 | { |
| 11184 | /* |
| 11185 | * First expand environment variables, "~/" and "~user/". |
| 11186 | */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11187 | if (has_env_var(p) || *p == '~') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11188 | { |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 11189 | p = expand_env_save_opt(p, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11190 | if (p == NULL) |
| 11191 | p = pat[i]; |
| 11192 | #ifdef UNIX |
| 11193 | /* |
| 11194 | * On Unix, if expand_env() can't expand an environment |
| 11195 | * variable, use the shell to do that. Discard previously |
| 11196 | * found file names and start all over again. |
| 11197 | */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11198 | else if (has_env_var(p) || *p == '~') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11199 | { |
| 11200 | vim_free(p); |
Bram Moolenaar | 782027e | 2009-06-24 14:25:49 +0000 | [diff] [blame] | 11201 | ga_clear_strings(&ga); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11202 | i = mch_expand_wildcards(num_pat, pat, num_file, file, |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 11203 | flags|EW_KEEPDOLLAR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11204 | recursive = FALSE; |
| 11205 | return i; |
| 11206 | } |
| 11207 | #endif |
| 11208 | } |
| 11209 | |
| 11210 | /* |
| 11211 | * If there are wildcards: Expand file names and add each match to |
| 11212 | * the list. If there is no match, and EW_NOTFOUND is given, add |
| 11213 | * the pattern. |
| 11214 | * If there are no wildcards: Add the file name if it exists or |
| 11215 | * when EW_NOTFOUND is given. |
| 11216 | */ |
| 11217 | if (mch_has_exp_wildcard(p)) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11218 | { |
| 11219 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11220 | if ((flags & EW_PATH) |
| 11221 | && !mch_isFullName(p) |
| 11222 | && !(p[0] == '.' |
| 11223 | && (vim_ispathsep(p[1]) |
| 11224 | || (p[1] == '.' && vim_ispathsep(p[2])))) |
| 11225 | ) |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11226 | { |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11227 | /* :find completion where 'path' is used. |
| 11228 | * Recursiveness is OK here. */ |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11229 | recursive = FALSE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11230 | add_pat = expand_in_path(&ga, p, flags); |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11231 | recursive = TRUE; |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11232 | did_expand_in_path = TRUE; |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11233 | } |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11234 | else |
| 11235 | #endif |
| 11236 | add_pat = mch_expandpath(&ga, p, flags); |
| 11237 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11238 | } |
| 11239 | |
| 11240 | if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) |
| 11241 | { |
| 11242 | char_u *t = backslash_halve_save(p); |
| 11243 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11244 | /* When EW_NOTFOUND is used, always add files and dirs. Makes |
| 11245 | * "vim c:/" work. */ |
| 11246 | if (flags & EW_NOTFOUND) |
| 11247 | addfile(&ga, t, flags | EW_DIR | EW_FILE); |
Bram Moolenaar | 00efded | 2016-07-07 14:29:10 +0200 | [diff] [blame] | 11248 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11249 | addfile(&ga, t, flags); |
| 11250 | vim_free(t); |
| 11251 | } |
| 11252 | |
Bram Moolenaar | b28ebbc | 2010-07-14 16:59:57 +0200 | [diff] [blame] | 11253 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11254 | if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH)) |
Bram Moolenaar | b28ebbc | 2010-07-14 16:59:57 +0200 | [diff] [blame] | 11255 | uniquefy_paths(&ga, p); |
| 11256 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11257 | if (p != pat[i]) |
| 11258 | vim_free(p); |
| 11259 | } |
| 11260 | |
| 11261 | *num_file = ga.ga_len; |
| 11262 | *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; |
| 11263 | |
| 11264 | recursive = FALSE; |
| 11265 | |
Bram Moolenaar | 336bd62 | 2016-01-17 18:23:58 +0100 | [diff] [blame] | 11266 | return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11267 | } |
| 11268 | |
| 11269 | # ifdef VIM_BACKTICK |
| 11270 | |
| 11271 | /* |
| 11272 | * Return TRUE if we can expand this backtick thing here. |
| 11273 | */ |
| 11274 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11275 | vim_backtick(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11276 | { |
| 11277 | return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); |
| 11278 | } |
| 11279 | |
| 11280 | /* |
| 11281 | * Expand an item in `backticks` by executing it as a command. |
| 11282 | * Currently only works when pat[] starts and ends with a `. |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11283 | * Returns number of file names found, -1 if an error is encountered. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11284 | */ |
| 11285 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11286 | expand_backtick( |
| 11287 | garray_T *gap, |
| 11288 | char_u *pat, |
| 11289 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11290 | { |
| 11291 | char_u *p; |
| 11292 | char_u *cmd; |
| 11293 | char_u *buffer; |
| 11294 | int cnt = 0; |
| 11295 | int i; |
| 11296 | |
| 11297 | /* Create the command: lop off the backticks. */ |
| 11298 | cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); |
| 11299 | if (cmd == NULL) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11300 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11301 | |
| 11302 | #ifdef FEAT_EVAL |
| 11303 | if (*cmd == '=') /* `={expr}`: Expand expression */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 11304 | buffer = eval_to_string(cmd + 1, &p, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11305 | else |
| 11306 | #endif |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 11307 | buffer = get_cmd_output(cmd, NULL, |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11308 | (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11309 | vim_free(cmd); |
| 11310 | if (buffer == NULL) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11311 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11312 | |
| 11313 | cmd = buffer; |
| 11314 | while (*cmd != NUL) |
| 11315 | { |
| 11316 | cmd = skipwhite(cmd); /* skip over white space */ |
| 11317 | p = cmd; |
| 11318 | while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */ |
| 11319 | ++p; |
| 11320 | /* add an entry if it is not empty */ |
| 11321 | if (p > cmd) |
| 11322 | { |
| 11323 | i = *p; |
| 11324 | *p = NUL; |
| 11325 | addfile(gap, cmd, flags); |
| 11326 | *p = i; |
| 11327 | ++cnt; |
| 11328 | } |
| 11329 | cmd = p; |
| 11330 | while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) |
| 11331 | ++cmd; |
| 11332 | } |
| 11333 | |
| 11334 | vim_free(buffer); |
| 11335 | return cnt; |
| 11336 | } |
| 11337 | # endif /* VIM_BACKTICK */ |
| 11338 | |
| 11339 | /* |
| 11340 | * Add a file to a file list. Accepted flags: |
| 11341 | * EW_DIR add directories |
| 11342 | * EW_FILE add files |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 11343 | * EW_EXEC add executable files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11344 | * EW_NOTFOUND add even when it doesn't exist |
| 11345 | * EW_ADDSLASH add slash after directory name |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 11346 | * EW_ALLLINKS add symlink also when the referred file does not exist |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11347 | */ |
| 11348 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11349 | addfile( |
| 11350 | garray_T *gap, |
| 11351 | char_u *f, /* filename */ |
| 11352 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11353 | { |
| 11354 | char_u *p; |
| 11355 | int isdir; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 11356 | stat_T sb; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11357 | |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 11358 | /* if the file/dir/link doesn't exist, may not add it */ |
| 11359 | if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS) |
Bram Moolenaar | ab11a59 | 2015-03-06 22:00:11 +0100 | [diff] [blame] | 11360 | ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11361 | return; |
| 11362 | |
| 11363 | #ifdef FNAME_ILLEGAL |
| 11364 | /* if the file/dir contains illegal characters, don't add it */ |
| 11365 | if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) |
| 11366 | return; |
| 11367 | #endif |
| 11368 | |
| 11369 | isdir = mch_isdir(f); |
| 11370 | if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) |
| 11371 | return; |
| 11372 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 11373 | /* If the file isn't executable, may not add it. Do accept directories. |
| 11374 | * When invoked from expand_shellcmd() do not use $PATH. */ |
| 11375 | if (!isdir && (flags & EW_EXEC) |
| 11376 | && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD))) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 11377 | return; |
| 11378 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11379 | /* Make room for another item in the file list. */ |
| 11380 | if (ga_grow(gap, 1) == FAIL) |
| 11381 | return; |
| 11382 | |
| 11383 | p = alloc((unsigned)(STRLEN(f) + 1 + isdir)); |
| 11384 | if (p == NULL) |
| 11385 | return; |
| 11386 | |
| 11387 | STRCPY(p, f); |
| 11388 | #ifdef BACKSLASH_IN_FILENAME |
| 11389 | slash_adjust(p); |
| 11390 | #endif |
| 11391 | /* |
| 11392 | * Append a slash or backslash after directory names if none is present. |
| 11393 | */ |
| 11394 | #ifndef DONT_ADD_PATHSEP_TO_DIR |
| 11395 | if (isdir && (flags & EW_ADDSLASH)) |
| 11396 | add_pathsep(p); |
| 11397 | #endif |
| 11398 | ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11399 | } |
| 11400 | #endif /* !NO_EXPANDPATH */ |
| 11401 | |
| 11402 | #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO) |
| 11403 | |
| 11404 | #ifndef SEEK_SET |
| 11405 | # define SEEK_SET 0 |
| 11406 | #endif |
| 11407 | #ifndef SEEK_END |
| 11408 | # define SEEK_END 2 |
| 11409 | #endif |
| 11410 | |
| 11411 | /* |
| 11412 | * Get the stdout of an external command. |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11413 | * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not |
| 11414 | * NULL store the length there. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11415 | * Returns an allocated string, or NULL for error. |
| 11416 | */ |
| 11417 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11418 | get_cmd_output( |
| 11419 | char_u *cmd, |
| 11420 | char_u *infile, /* optional input file name */ |
| 11421 | int flags, /* can be SHELL_SILENT */ |
| 11422 | int *ret_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11423 | { |
| 11424 | char_u *tempname; |
| 11425 | char_u *command; |
| 11426 | char_u *buffer = NULL; |
| 11427 | int len; |
| 11428 | int i = 0; |
| 11429 | FILE *fd; |
| 11430 | |
| 11431 | if (check_restricted() || check_secure()) |
| 11432 | return NULL; |
| 11433 | |
| 11434 | /* get a name for the temp file */ |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 11435 | if ((tempname = vim_tempname('o', FALSE)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11436 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 11437 | emsg(_(e_notmp)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11438 | return NULL; |
| 11439 | } |
| 11440 | |
| 11441 | /* Add the redirection stuff */ |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 11442 | command = make_filter_cmd(cmd, infile, tempname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11443 | if (command == NULL) |
| 11444 | goto done; |
| 11445 | |
| 11446 | /* |
| 11447 | * Call the shell to execute the command (errors are ignored). |
| 11448 | * Don't check timestamps here. |
| 11449 | */ |
| 11450 | ++no_check_timestamps; |
| 11451 | call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags); |
| 11452 | --no_check_timestamps; |
| 11453 | |
| 11454 | vim_free(command); |
| 11455 | |
| 11456 | /* |
| 11457 | * read the names from the file into memory |
| 11458 | */ |
| 11459 | # ifdef VMS |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 11460 | /* created temporary file is not always readable as binary */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11461 | fd = mch_fopen((char *)tempname, "r"); |
| 11462 | # else |
| 11463 | fd = mch_fopen((char *)tempname, READBIN); |
| 11464 | # endif |
| 11465 | |
| 11466 | if (fd == NULL) |
| 11467 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 11468 | semsg(_(e_notopen), tempname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11469 | goto done; |
| 11470 | } |
| 11471 | |
| 11472 | fseek(fd, 0L, SEEK_END); |
| 11473 | len = ftell(fd); /* get size of temp file */ |
| 11474 | fseek(fd, 0L, SEEK_SET); |
| 11475 | |
| 11476 | buffer = alloc(len + 1); |
| 11477 | if (buffer != NULL) |
| 11478 | i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd); |
| 11479 | fclose(fd); |
| 11480 | mch_remove(tempname); |
| 11481 | if (buffer == NULL) |
| 11482 | goto done; |
| 11483 | #ifdef VMS |
| 11484 | len = i; /* VMS doesn't give us what we asked for... */ |
| 11485 | #endif |
| 11486 | if (i != len) |
| 11487 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 11488 | semsg(_(e_notread), tempname); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 11489 | VIM_CLEAR(buffer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11490 | } |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11491 | else if (ret_len == NULL) |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 11492 | { |
| 11493 | /* Change NUL into SOH, otherwise the string is truncated. */ |
| 11494 | for (i = 0; i < len; ++i) |
Bram Moolenaar | f40f4ab | 2013-08-03 17:31:28 +0200 | [diff] [blame] | 11495 | if (buffer[i] == NUL) |
| 11496 | buffer[i] = 1; |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 11497 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 11498 | buffer[len] = NUL; /* make sure the buffer is terminated */ |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 11499 | } |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11500 | else |
| 11501 | *ret_len = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11502 | |
| 11503 | done: |
| 11504 | vim_free(tempname); |
| 11505 | return buffer; |
| 11506 | } |
| 11507 | #endif |
| 11508 | |
| 11509 | /* |
| 11510 | * Free the list of files returned by expand_wildcards() or other expansion |
| 11511 | * functions. |
| 11512 | */ |
| 11513 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11514 | FreeWild(int count, char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11515 | { |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 11516 | if (count <= 0 || files == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11517 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11518 | while (count--) |
| 11519 | vim_free(files[count]); |
| 11520 | vim_free(files); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11521 | } |
| 11522 | |
| 11523 | /* |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 11524 | * Return TRUE when need to go to Insert mode because of 'insertmode'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11525 | * Don't do this when still processing a command or a mapping. |
| 11526 | * Don't do this when inside a ":normal" command. |
| 11527 | */ |
| 11528 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11529 | goto_im(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11530 | { |
| 11531 | return (p_im && stuff_empty() && typebuf_typed()); |
| 11532 | } |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11533 | |
| 11534 | /* |
Bram Moolenaar | 050fe7e | 2014-05-22 14:00:16 +0200 | [diff] [blame] | 11535 | * Returns the isolated name of the shell in allocated memory: |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11536 | * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f". |
| 11537 | * - Remove any argument. E.g., "csh -f" -> "csh". |
| 11538 | * But don't allow a space in the path, so that this works: |
| 11539 | * "/usr/bin/csh --rcfile ~/.cshrc" |
| 11540 | * But don't do that for Windows, it's common to have a space in the path. |
| 11541 | */ |
| 11542 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11543 | get_isolated_shell_name(void) |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11544 | { |
| 11545 | char_u *p; |
| 11546 | |
| 11547 | #ifdef WIN3264 |
| 11548 | p = gettail(p_sh); |
| 11549 | p = vim_strnsave(p, (int)(skiptowhite(p) - p)); |
| 11550 | #else |
| 11551 | p = skiptowhite(p_sh); |
| 11552 | if (*p == NUL) |
| 11553 | { |
| 11554 | /* No white space, use the tail. */ |
| 11555 | p = vim_strsave(gettail(p_sh)); |
| 11556 | } |
| 11557 | else |
| 11558 | { |
| 11559 | char_u *p1, *p2; |
| 11560 | |
| 11561 | /* Find the last path separator before the space. */ |
| 11562 | p1 = p_sh; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 11563 | for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2)) |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11564 | if (vim_ispathsep(*p2)) |
| 11565 | p1 = p2 + 1; |
| 11566 | p = vim_strnsave(p1, (int)(p - p1)); |
| 11567 | } |
| 11568 | #endif |
| 11569 | return p; |
| 11570 | } |