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 | 06ae70d | 2013-06-17 19:26:36 +0200 | [diff] [blame] | 23 | #if defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 24 | static void init_users(void); |
Bram Moolenaar | 06ae70d | 2013-06-17 19:26:36 +0200 | [diff] [blame] | 25 | #endif |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 26 | static int copy_indent(int size, char_u *src); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 27 | |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 28 | /* All user names (for ~user completion as done by shell). */ |
| 29 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 30 | static garray_T ga_users; |
| 31 | #endif |
| 32 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 33 | /* |
| 34 | * Count the size (in window cells) of the indent in the current line. |
| 35 | */ |
| 36 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 37 | get_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 39 | #ifdef FEAT_VARTABS |
| 40 | return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts, |
| 41 | curbuf->b_p_vts_array, FALSE); |
| 42 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 43 | 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] | 44 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Count the size (in window cells) of the indent in line "lnum". |
| 49 | */ |
| 50 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 51 | get_indent_lnum(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 52 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 53 | #ifdef FEAT_VARTABS |
| 54 | return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts, |
| 55 | curbuf->b_p_vts_array, FALSE); |
| 56 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 57 | 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] | 58 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | #if defined(FEAT_FOLDING) || defined(PROTO) |
| 62 | /* |
| 63 | * Count the size (in window cells) of the indent in line "lnum" of buffer |
| 64 | * "buf". |
| 65 | */ |
| 66 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 67 | get_indent_buf(buf_T *buf, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 68 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 69 | #ifdef FEAT_VARTABS |
| 70 | return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE), |
| 71 | (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE); |
| 72 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 73 | 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] | 74 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 75 | } |
| 76 | #endif |
| 77 | |
| 78 | /* |
| 79 | * count the size (in window cells) of the indent in line "ptr", with |
| 80 | * 'tabstop' at "ts" |
| 81 | */ |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 82 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 83 | get_indent_str( |
| 84 | char_u *ptr, |
| 85 | int ts, |
| 86 | int list) /* if TRUE, count only screen size for tabs */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 87 | { |
| 88 | int count = 0; |
| 89 | |
| 90 | for ( ; *ptr; ++ptr) |
| 91 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 92 | if (*ptr == TAB) |
| 93 | { |
| 94 | if (!list || lcs_tab1) /* count a tab for what it is worth */ |
| 95 | count += ts - (count % ts); |
| 96 | else |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 97 | /* In list mode, when tab is not set, count screen char width |
| 98 | * for Tab, displays: ^I */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 99 | count += ptr2cells(ptr); |
| 100 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 101 | else if (*ptr == ' ') |
| 102 | ++count; /* count a space for one */ |
| 103 | else |
| 104 | break; |
| 105 | } |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 106 | return count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 109 | #ifdef FEAT_VARTABS |
| 110 | /* |
| 111 | * Count the size (in window cells) of the indent in line "ptr", using |
| 112 | * variable tabstops. |
| 113 | * if "list" is TRUE, count only screen size for tabs. |
| 114 | */ |
| 115 | int |
| 116 | get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list) |
| 117 | { |
| 118 | int count = 0; |
| 119 | |
| 120 | for ( ; *ptr; ++ptr) |
| 121 | { |
| 122 | if (*ptr == TAB) /* count a tab for what it is worth */ |
| 123 | { |
| 124 | if (!list || lcs_tab1) |
| 125 | count += tabstop_padding(count, ts, vts); |
| 126 | else |
| 127 | /* In list mode, when tab is not set, count screen char width |
| 128 | * for Tab, displays: ^I */ |
| 129 | count += ptr2cells(ptr); |
| 130 | } |
| 131 | else if (*ptr == ' ') |
| 132 | ++count; /* count a space for one */ |
| 133 | else |
| 134 | break; |
| 135 | } |
| 136 | return count; |
| 137 | } |
| 138 | #endif |
| 139 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 140 | /* |
| 141 | * Set the indent of the current line. |
| 142 | * Leaves the cursor on the first non-blank in the line. |
| 143 | * Caller must take care of undo. |
| 144 | * "flags": |
| 145 | * SIN_CHANGED: call changed_bytes() if the line was changed. |
| 146 | * SIN_INSERT: insert the indent in front of the line. |
| 147 | * SIN_UNDO: save line for undo before changing it. |
| 148 | * Returns TRUE if the line was changed. |
| 149 | */ |
| 150 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 151 | set_indent( |
| 152 | int size, /* measured in spaces */ |
| 153 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 154 | { |
| 155 | char_u *p; |
| 156 | char_u *newline; |
| 157 | char_u *oldline; |
| 158 | char_u *s; |
| 159 | int todo; |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 160 | int ind_len; /* measured in characters */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 161 | int line_len; |
| 162 | int doit = FALSE; |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 163 | int ind_done = 0; /* measured in spaces */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 164 | #ifdef FEAT_VARTABS |
| 165 | int ind_col = 0; |
| 166 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 167 | int tab_pad; |
Bram Moolenaar | 5409c05 | 2005-03-18 20:27:04 +0000 | [diff] [blame] | 168 | int retval = FALSE; |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 169 | int orig_char_len = -1; /* number of initial whitespace chars when |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 170 | 'et' and 'pi' are both set */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * First check if there is anything to do and compute the number of |
| 174 | * characters needed for the indent. |
| 175 | */ |
| 176 | todo = size; |
| 177 | ind_len = 0; |
| 178 | p = oldline = ml_get_curline(); |
| 179 | |
| 180 | /* Calculate the buffer size for the new indent, and check to see if it |
| 181 | * isn't already set */ |
| 182 | |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 183 | /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and |
| 184 | * 'preserveindent' are set count the number of characters at the |
| 185 | * beginning of the line to be copied */ |
| 186 | if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 187 | { |
| 188 | /* If 'preserveindent' is set then reuse as much as possible of |
| 189 | * the existing indent structure for the new indent */ |
| 190 | if (!(flags & SIN_INSERT) && curbuf->b_p_pi) |
| 191 | { |
| 192 | ind_done = 0; |
| 193 | |
| 194 | /* count as many characters as we can use */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 195 | while (todo > 0 && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 196 | { |
| 197 | if (*p == TAB) |
| 198 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 199 | #ifdef FEAT_VARTABS |
| 200 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 201 | curbuf->b_p_vts_array); |
| 202 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 203 | tab_pad = (int)curbuf->b_p_ts |
| 204 | - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 205 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 206 | /* stop if this tab will overshoot the target */ |
| 207 | if (todo < tab_pad) |
| 208 | break; |
| 209 | todo -= tab_pad; |
| 210 | ++ind_len; |
| 211 | ind_done += tab_pad; |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | --todo; |
| 216 | ++ind_len; |
| 217 | ++ind_done; |
| 218 | } |
| 219 | ++p; |
| 220 | } |
| 221 | |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 222 | #ifdef FEAT_VARTABS |
| 223 | /* These diverge from this point. */ |
| 224 | ind_col = ind_done; |
| 225 | #endif |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 226 | /* Set initial number of whitespace chars to copy if we are |
| 227 | * preserving indent but expandtab is set */ |
| 228 | if (curbuf->b_p_et) |
| 229 | orig_char_len = ind_len; |
| 230 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 231 | /* Fill to next tabstop with a tab, if possible */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 232 | #ifdef FEAT_VARTABS |
| 233 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 234 | curbuf->b_p_vts_array); |
| 235 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 236 | 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] | 237 | #endif |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 238 | if (todo >= tab_pad && orig_char_len == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 239 | { |
| 240 | doit = TRUE; |
| 241 | todo -= tab_pad; |
| 242 | ++ind_len; |
| 243 | /* ind_done += tab_pad; */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 244 | #ifdef FEAT_VARTABS |
| 245 | ind_col += tab_pad; |
| 246 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | /* count tabs required for indent */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 251 | #ifdef FEAT_VARTABS |
| 252 | for (;;) |
| 253 | { |
| 254 | tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts, |
| 255 | curbuf->b_p_vts_array); |
| 256 | if (todo < tab_pad) |
| 257 | break; |
| 258 | if (*p != TAB) |
| 259 | doit = TRUE; |
| 260 | else |
| 261 | ++p; |
| 262 | todo -= tab_pad; |
| 263 | ++ind_len; |
| 264 | ind_col += tab_pad; |
| 265 | } |
| 266 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 267 | while (todo >= (int)curbuf->b_p_ts) |
| 268 | { |
| 269 | if (*p != TAB) |
| 270 | doit = TRUE; |
| 271 | else |
| 272 | ++p; |
| 273 | todo -= (int)curbuf->b_p_ts; |
| 274 | ++ind_len; |
| 275 | /* ind_done += (int)curbuf->b_p_ts; */ |
| 276 | } |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 277 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 278 | } |
| 279 | /* count spaces required for indent */ |
| 280 | while (todo > 0) |
| 281 | { |
| 282 | if (*p != ' ') |
| 283 | doit = TRUE; |
| 284 | else |
| 285 | ++p; |
| 286 | --todo; |
| 287 | ++ind_len; |
| 288 | /* ++ind_done; */ |
| 289 | } |
| 290 | |
| 291 | /* Return if the indent is OK already. */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 292 | if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 293 | return FALSE; |
| 294 | |
| 295 | /* Allocate memory for the new line. */ |
| 296 | if (flags & SIN_INSERT) |
| 297 | p = oldline; |
| 298 | else |
| 299 | p = skipwhite(p); |
| 300 | line_len = (int)STRLEN(p) + 1; |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 301 | |
| 302 | /* If 'preserveindent' and 'expandtab' are both set keep the original |
| 303 | * characters and allocate accordingly. We will fill the rest with spaces |
| 304 | * after the if (!curbuf->b_p_et) below. */ |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 305 | if (orig_char_len != -1) |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 306 | { |
| 307 | newline = alloc(orig_char_len + size - ind_done + line_len); |
| 308 | if (newline == NULL) |
| 309 | return FALSE; |
Bram Moolenaar | 4d64b78 | 2007-08-14 20:16:42 +0000 | [diff] [blame] | 310 | todo = size - ind_done; |
| 311 | ind_len = orig_char_len + todo; /* Set total length of indent in |
| 312 | * characters, which may have been |
| 313 | * undercounted until now */ |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 314 | p = oldline; |
| 315 | s = newline; |
| 316 | while (orig_char_len > 0) |
| 317 | { |
| 318 | *s++ = *p++; |
| 319 | orig_char_len--; |
| 320 | } |
Bram Moolenaar | 913626c | 2008-01-03 11:43:42 +0000 | [diff] [blame] | 321 | |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 322 | /* Skip over any additional white space (useful when newindent is less |
| 323 | * than old) */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 324 | while (VIM_ISWHITE(*p)) |
Bram Moolenaar | 913626c | 2008-01-03 11:43:42 +0000 | [diff] [blame] | 325 | ++p; |
Bram Moolenaar | cc00b95 | 2007-08-11 12:32:57 +0000 | [diff] [blame] | 326 | |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 327 | } |
| 328 | else |
| 329 | { |
| 330 | todo = size; |
| 331 | newline = alloc(ind_len + line_len); |
| 332 | if (newline == NULL) |
| 333 | return FALSE; |
| 334 | s = newline; |
| 335 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 336 | |
| 337 | /* Put the characters in the new line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 338 | /* if 'expandtab' isn't set: use TABs */ |
| 339 | if (!curbuf->b_p_et) |
| 340 | { |
| 341 | /* If 'preserveindent' is set then reuse as much as possible of |
| 342 | * the existing indent structure for the new indent */ |
| 343 | if (!(flags & SIN_INSERT) && curbuf->b_p_pi) |
| 344 | { |
| 345 | p = oldline; |
| 346 | ind_done = 0; |
| 347 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 348 | while (todo > 0 && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 349 | { |
| 350 | if (*p == TAB) |
| 351 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 352 | #ifdef FEAT_VARTABS |
| 353 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 354 | curbuf->b_p_vts_array); |
| 355 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 356 | tab_pad = (int)curbuf->b_p_ts |
| 357 | - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 358 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 359 | /* stop if this tab will overshoot the target */ |
| 360 | if (todo < tab_pad) |
| 361 | break; |
| 362 | todo -= tab_pad; |
| 363 | ind_done += tab_pad; |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | --todo; |
| 368 | ++ind_done; |
| 369 | } |
| 370 | *s++ = *p++; |
| 371 | } |
| 372 | |
| 373 | /* Fill to next tabstop with a tab, if possible */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 374 | #ifdef FEAT_VARTABS |
| 375 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 376 | curbuf->b_p_vts_array); |
| 377 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 378 | 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] | 379 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 380 | if (todo >= tab_pad) |
| 381 | { |
| 382 | *s++ = TAB; |
| 383 | todo -= tab_pad; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 384 | #ifdef FEAT_VARTABS |
| 385 | ind_done += tab_pad; |
| 386 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | p = skipwhite(p); |
| 390 | } |
| 391 | |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 392 | #ifdef FEAT_VARTABS |
| 393 | for (;;) |
| 394 | { |
| 395 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 396 | curbuf->b_p_vts_array); |
| 397 | if (todo < tab_pad) |
| 398 | break; |
| 399 | *s++ = TAB; |
| 400 | todo -= tab_pad; |
| 401 | ind_done += tab_pad; |
| 402 | } |
| 403 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 404 | while (todo >= (int)curbuf->b_p_ts) |
| 405 | { |
| 406 | *s++ = TAB; |
| 407 | todo -= (int)curbuf->b_p_ts; |
| 408 | } |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 409 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 410 | } |
| 411 | while (todo > 0) |
| 412 | { |
| 413 | *s++ = ' '; |
| 414 | --todo; |
| 415 | } |
| 416 | mch_memmove(s, p, (size_t)line_len); |
| 417 | |
| 418 | /* Replace the line (unless undo fails). */ |
| 419 | if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK) |
| 420 | { |
| 421 | ml_replace(curwin->w_cursor.lnum, newline, FALSE); |
| 422 | if (flags & SIN_CHANGED) |
| 423 | changed_bytes(curwin->w_cursor.lnum, 0); |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 424 | /* Correct saved cursor position if it is in this line. */ |
| 425 | if (saved_cursor.lnum == curwin->w_cursor.lnum) |
| 426 | { |
| 427 | if (saved_cursor.col >= (colnr_T)(p - oldline)) |
| 428 | /* cursor was after the indent, adjust for the number of |
| 429 | * bytes added/removed */ |
| 430 | saved_cursor.col += ind_len - (colnr_T)(p - oldline); |
| 431 | else if (saved_cursor.col >= (colnr_T)(s - newline)) |
| 432 | /* cursor was in the indent, and is now after it, put it back |
| 433 | * at the start of the indent (replacing spaces with TAB) */ |
| 434 | saved_cursor.col = (colnr_T)(s - newline); |
| 435 | } |
Bram Moolenaar | 5409c05 | 2005-03-18 20:27:04 +0000 | [diff] [blame] | 436 | retval = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 437 | } |
| 438 | else |
| 439 | vim_free(newline); |
| 440 | |
| 441 | curwin->w_cursor.col = ind_len; |
Bram Moolenaar | 5409c05 | 2005-03-18 20:27:04 +0000 | [diff] [blame] | 442 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* |
| 446 | * Copy the indent from ptr to the current line (and fill to size) |
| 447 | * Leaves the cursor on the first non-blank in the line. |
| 448 | * Returns TRUE if the line was changed. |
| 449 | */ |
| 450 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 451 | copy_indent(int size, char_u *src) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 452 | { |
| 453 | char_u *p = NULL; |
| 454 | char_u *line = NULL; |
| 455 | char_u *s; |
| 456 | int todo; |
| 457 | int ind_len; |
| 458 | int line_len = 0; |
| 459 | int tab_pad; |
| 460 | int ind_done; |
| 461 | int round; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 462 | #ifdef FEAT_VARTABS |
| 463 | int ind_col; |
| 464 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 465 | |
| 466 | /* Round 1: compute the number of characters needed for the indent |
| 467 | * Round 2: copy the characters. */ |
| 468 | for (round = 1; round <= 2; ++round) |
| 469 | { |
| 470 | todo = size; |
| 471 | ind_len = 0; |
| 472 | ind_done = 0; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 473 | #ifdef FEAT_VARTABS |
| 474 | ind_col = 0; |
| 475 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 476 | s = src; |
| 477 | |
| 478 | /* Count/copy the usable portion of the source line */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 479 | while (todo > 0 && VIM_ISWHITE(*s)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 480 | { |
| 481 | if (*s == TAB) |
| 482 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 483 | #ifdef FEAT_VARTABS |
| 484 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 485 | curbuf->b_p_vts_array); |
| 486 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 487 | tab_pad = (int)curbuf->b_p_ts |
| 488 | - (ind_done % (int)curbuf->b_p_ts); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 489 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 490 | /* Stop if this tab will overshoot the target */ |
| 491 | if (todo < tab_pad) |
| 492 | break; |
| 493 | todo -= tab_pad; |
| 494 | ind_done += tab_pad; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 495 | #ifdef FEAT_VARTABS |
| 496 | ind_col += tab_pad; |
| 497 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 498 | } |
| 499 | else |
| 500 | { |
| 501 | --todo; |
| 502 | ++ind_done; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 503 | #ifdef FEAT_VARTABS |
| 504 | ++ind_col; |
| 505 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 506 | } |
| 507 | ++ind_len; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 508 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 509 | *p++ = *s; |
| 510 | ++s; |
| 511 | } |
| 512 | |
| 513 | /* Fill to next tabstop with a tab, if possible */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 514 | #ifdef FEAT_VARTABS |
| 515 | tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, |
| 516 | curbuf->b_p_vts_array); |
| 517 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 518 | 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] | 519 | #endif |
Bram Moolenaar | c42e7ed | 2011-09-07 19:58:09 +0200 | [diff] [blame] | 520 | if (todo >= tab_pad && !curbuf->b_p_et) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 521 | { |
| 522 | todo -= tab_pad; |
| 523 | ++ind_len; |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 524 | #ifdef FEAT_VARTABS |
| 525 | ind_col += tab_pad; |
| 526 | #endif |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 527 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 528 | *p++ = TAB; |
| 529 | } |
| 530 | |
| 531 | /* Add tabs required for indent */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 532 | if (!curbuf->b_p_et) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 533 | { |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 534 | #ifdef FEAT_VARTABS |
| 535 | for (;;) |
| 536 | { |
| 537 | tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts, |
| 538 | curbuf->b_p_vts_array); |
| 539 | if (todo < tab_pad) |
| 540 | break; |
| 541 | todo -= tab_pad; |
| 542 | ++ind_len; |
| 543 | ind_col += tab_pad; |
| 544 | if (p != NULL) |
| 545 | *p++ = TAB; |
| 546 | } |
| 547 | #else |
| 548 | while (todo >= (int)curbuf->b_p_ts) |
| 549 | { |
| 550 | todo -= (int)curbuf->b_p_ts; |
| 551 | ++ind_len; |
| 552 | if (p != NULL) |
| 553 | *p++ = TAB; |
| 554 | } |
| 555 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* Count/add spaces required for indent */ |
| 559 | while (todo > 0) |
| 560 | { |
| 561 | --todo; |
| 562 | ++ind_len; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 563 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 564 | *p++ = ' '; |
| 565 | } |
| 566 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 567 | if (p == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 568 | { |
| 569 | /* Allocate memory for the result: the copied indent, new indent |
| 570 | * and the rest of the line. */ |
| 571 | line_len = (int)STRLEN(ml_get_curline()) + 1; |
| 572 | line = alloc(ind_len + line_len); |
| 573 | if (line == NULL) |
| 574 | return FALSE; |
| 575 | p = line; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /* Append the original line */ |
| 580 | mch_memmove(p, ml_get_curline(), (size_t)line_len); |
| 581 | |
| 582 | /* Replace the line */ |
| 583 | ml_replace(curwin->w_cursor.lnum, line, FALSE); |
| 584 | |
| 585 | /* Put the cursor after the indent. */ |
| 586 | curwin->w_cursor.col = ind_len; |
| 587 | return TRUE; |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * Return the indent of the current line after a number. Return -1 if no |
| 592 | * number was found. Used for 'n' in 'formatoptions': numbered list. |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 593 | * Since a pattern is used it can actually handle more than numbers. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 594 | */ |
| 595 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 596 | get_number_indent(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 597 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 598 | colnr_T col; |
| 599 | pos_T pos; |
| 600 | |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 601 | regmatch_T regmatch; |
| 602 | int lead_len = 0; /* length of comment leader */ |
| 603 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 604 | if (lnum > curbuf->b_ml.ml_line_count) |
| 605 | return -1; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 606 | pos.lnum = 0; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 607 | |
| 608 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 609 | /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */ |
| 610 | if ((State & INSERT) || has_format_option(FO_Q_COMS)) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 611 | lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE); |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 612 | #endif |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 613 | regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC); |
| 614 | if (regmatch.regprog != NULL) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 615 | { |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 616 | regmatch.rm_ic = FALSE; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 617 | |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 618 | /* vim_regexec() expects a pointer to a line. This lets us |
| 619 | * start matching for the flp beyond any comment leader... */ |
| 620 | if (vim_regexec(®match, ml_get(lnum) + lead_len, (colnr_T)0)) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 621 | { |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 622 | pos.lnum = lnum; |
| 623 | pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum)); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 624 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 625 | pos.coladd = 0; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 626 | #endif |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 627 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 628 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 629 | } |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 630 | |
| 631 | if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 632 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 633 | getvcol(curwin, &pos, &col, NULL, NULL); |
| 634 | return (int)col; |
| 635 | } |
| 636 | |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 637 | #if defined(FEAT_LINEBREAK) || defined(PROTO) |
| 638 | /* |
| 639 | * Return appropriate space number for breakindent, taking influencing |
| 640 | * parameters into account. Window must be specified, since it is not |
| 641 | * necessarily always the current one. |
| 642 | */ |
| 643 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 644 | get_breakindent_win( |
| 645 | win_T *wp, |
| 646 | char_u *line) /* start of the line */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 647 | { |
| 648 | static int prev_indent = 0; /* cached indent value */ |
| 649 | static long prev_ts = 0L; /* cached tabstop value */ |
| 650 | static char_u *prev_line = NULL; /* cached pointer to line */ |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 651 | static varnumber_T prev_tick = 0; /* changedtick of cached value */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 652 | #ifdef FEAT_VARTABS |
| 653 | static int *prev_vts = NULL; /* cached vartabs values */ |
| 654 | #endif |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 655 | int bri = 0; |
| 656 | /* window width minus window margin space, i.e. what rests for text */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 657 | const int eff_wwidth = wp->w_width |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 658 | - ((wp->w_p_nu || wp->w_p_rnu) |
| 659 | && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL) |
| 660 | ? number_width(wp) + 1 : 0); |
| 661 | |
| 662 | /* used cached indent, unless pointer or 'tabstop' changed */ |
Bram Moolenaar | a40aa76 | 2014-06-25 22:55:38 +0200 | [diff] [blame] | 663 | if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 664 | || prev_tick != CHANGEDTICK(wp->w_buffer) |
| 665 | #ifdef FEAT_VARTABS |
| 666 | || prev_vts != wp->w_buffer->b_p_vts_array |
| 667 | #endif |
| 668 | ) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 669 | { |
| 670 | prev_line = line; |
| 671 | prev_ts = wp->w_buffer->b_p_ts; |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 672 | prev_tick = CHANGEDTICK(wp->w_buffer); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 673 | #ifdef FEAT_VARTABS |
| 674 | prev_vts = wp->w_buffer->b_p_vts_array; |
| 675 | prev_indent = get_indent_str_vtab(line, |
| 676 | (int)wp->w_buffer->b_p_ts, |
| 677 | wp->w_buffer->b_p_vts_array, wp->w_p_list); |
| 678 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 679 | prev_indent = get_indent_str(line, |
Bram Moolenaar | 9d7a592 | 2014-06-26 21:24:56 +0200 | [diff] [blame] | 680 | (int)wp->w_buffer->b_p_ts, wp->w_p_list); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 681 | #endif |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 682 | } |
Bram Moolenaar | 9d7a592 | 2014-06-26 21:24:56 +0200 | [diff] [blame] | 683 | bri = prev_indent + wp->w_p_brishift; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 684 | |
| 685 | /* indent minus the length of the showbreak string */ |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 686 | if (wp->w_p_brisbr) |
| 687 | bri -= vim_strsize(p_sbr); |
| 688 | |
| 689 | /* Add offset for number column, if 'n' is in 'cpoptions' */ |
| 690 | bri += win_col_off2(wp); |
| 691 | |
| 692 | /* never indent past left window margin */ |
| 693 | if (bri < 0) |
| 694 | bri = 0; |
| 695 | /* always leave at least bri_min characters on the left, |
| 696 | * if text width is sufficient */ |
| 697 | else if (bri > eff_wwidth - wp->w_p_brimin) |
| 698 | bri = (eff_wwidth - wp->w_p_brimin < 0) |
| 699 | ? 0 : eff_wwidth - wp->w_p_brimin; |
| 700 | |
| 701 | return bri; |
| 702 | } |
| 703 | #endif |
| 704 | |
| 705 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 706 | #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) |
| 707 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 708 | static int cin_is_cinword(char_u *line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 709 | |
| 710 | /* |
| 711 | * Return TRUE if the string "line" starts with a word from 'cinwords'. |
| 712 | */ |
| 713 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 714 | cin_is_cinword(char_u *line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 715 | { |
| 716 | char_u *cinw; |
| 717 | char_u *cinw_buf; |
| 718 | int cinw_len; |
| 719 | int retval = FALSE; |
| 720 | int len; |
| 721 | |
| 722 | cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1; |
| 723 | cinw_buf = alloc((unsigned)cinw_len); |
| 724 | if (cinw_buf != NULL) |
| 725 | { |
| 726 | line = skipwhite(line); |
| 727 | for (cinw = curbuf->b_p_cinw; *cinw; ) |
| 728 | { |
| 729 | len = copy_option_part(&cinw, cinw_buf, cinw_len, ","); |
| 730 | if (STRNCMP(line, cinw_buf, len) == 0 |
| 731 | && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1]))) |
| 732 | { |
| 733 | retval = TRUE; |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | vim_free(cinw_buf); |
| 738 | } |
| 739 | return retval; |
| 740 | } |
| 741 | #endif |
| 742 | |
| 743 | /* |
| 744 | * open_line: Add a new line below or above the current line. |
| 745 | * |
| 746 | * For VREPLACE mode, we only add a new line when we get to the end of the |
| 747 | * file, otherwise we just start replacing the next line. |
| 748 | * |
| 749 | * Caller must take care of undo. Since VREPLACE may affect any number of |
| 750 | * lines however, it may call u_save_cursor() again when starting to change a |
| 751 | * new line. |
| 752 | * "flags": OPENLINE_DELSPACES delete spaces after cursor |
| 753 | * OPENLINE_DO_COM format comments |
| 754 | * OPENLINE_KEEPTRAIL keep trailing spaces |
| 755 | * OPENLINE_MARKFIX adjust mark positions after the line break |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 756 | * OPENLINE_COM_LIST format comments with list or 2nd line indent |
| 757 | * |
| 758 | * "second_line_indent": indent for after ^^D in Insert mode or if flag |
| 759 | * OPENLINE_COM_LIST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 760 | * |
Bram Moolenaar | 24a2d72 | 2018-04-24 19:36:43 +0200 | [diff] [blame] | 761 | * Return OK for success, FAIL for failure |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 762 | */ |
| 763 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 764 | open_line( |
| 765 | int dir, /* FORWARD or BACKWARD */ |
| 766 | int flags, |
| 767 | int second_line_indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 768 | { |
| 769 | char_u *saved_line; /* copy of the original line */ |
| 770 | char_u *next_line = NULL; /* copy of the next line */ |
| 771 | char_u *p_extra = NULL; /* what goes to next line */ |
| 772 | int less_cols = 0; /* less columns for mark in new line */ |
| 773 | int less_cols_off = 0; /* columns to skip for mark adjust */ |
| 774 | pos_T old_cursor; /* old cursor position */ |
| 775 | int newcol = 0; /* new cursor column */ |
| 776 | int newindent = 0; /* auto-indent of the new line */ |
| 777 | int n; |
| 778 | int trunc_line = FALSE; /* truncate current line afterwards */ |
Bram Moolenaar | 24a2d72 | 2018-04-24 19:36:43 +0200 | [diff] [blame] | 779 | int retval = FAIL; /* return value */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 780 | #ifdef FEAT_COMMENTS |
| 781 | int extra_len = 0; /* length of p_extra string */ |
| 782 | int lead_len; /* length of comment leader */ |
| 783 | char_u *lead_flags; /* position in 'comments' for comment leader */ |
| 784 | char_u *leader = NULL; /* copy of comment leader */ |
| 785 | #endif |
| 786 | char_u *allocated = NULL; /* allocated memory */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 787 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 788 | int saved_char = NUL; /* init for GCC */ |
| 789 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS) |
| 790 | pos_T *pos; |
| 791 | #endif |
| 792 | #ifdef FEAT_SMARTINDENT |
| 793 | int do_si = (!p_paste && curbuf->b_p_si |
| 794 | # ifdef FEAT_CINDENT |
| 795 | && !curbuf->b_p_cin |
| 796 | # endif |
Bram Moolenaar | 69a76fe | 2017-08-03 17:54:03 +0200 | [diff] [blame] | 797 | # ifdef FEAT_EVAL |
| 798 | && *curbuf->b_p_inde == NUL |
| 799 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 800 | ); |
| 801 | int no_si = FALSE; /* reset did_si afterwards */ |
| 802 | int first_char = NUL; /* init for GCC */ |
| 803 | #endif |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 804 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 805 | int vreplace_mode; |
| 806 | #endif |
| 807 | int did_append; /* appended a new line */ |
| 808 | int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */ |
| 809 | |
| 810 | /* |
| 811 | * make a copy of the current line so we can mess with it |
| 812 | */ |
| 813 | saved_line = vim_strsave(ml_get_curline()); |
| 814 | if (saved_line == NULL) /* out of memory! */ |
| 815 | return FALSE; |
| 816 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 817 | if (State & VREPLACE_FLAG) |
| 818 | { |
| 819 | /* |
| 820 | * With VREPLACE we make a copy of the next line, which we will be |
| 821 | * starting to replace. First make the new line empty and let vim play |
| 822 | * with the indenting and comment leader to its heart's content. Then |
| 823 | * we grab what it ended up putting on the new line, put back the |
| 824 | * original line, and call ins_char() to put each new character onto |
| 825 | * the line, replacing what was there before and pushing the right |
| 826 | * stuff onto the replace stack. -- webb. |
| 827 | */ |
| 828 | if (curwin->w_cursor.lnum < orig_line_count) |
| 829 | next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1)); |
| 830 | else |
| 831 | next_line = vim_strsave((char_u *)""); |
| 832 | if (next_line == NULL) /* out of memory! */ |
| 833 | goto theend; |
| 834 | |
| 835 | /* |
| 836 | * In VREPLACE mode, a NL replaces the rest of the line, and starts |
| 837 | * replacing the next line, so push all of the characters left on the |
| 838 | * line onto the replace stack. We'll push any other characters that |
| 839 | * might be replaced at the start of the next line (due to autoindent |
| 840 | * etc) a bit later. |
| 841 | */ |
| 842 | replace_push(NUL); /* Call twice because BS over NL expects it */ |
| 843 | replace_push(NUL); |
| 844 | p = saved_line + curwin->w_cursor.col; |
| 845 | while (*p != NUL) |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 846 | { |
| 847 | #ifdef FEAT_MBYTE |
| 848 | if (has_mbyte) |
| 849 | p += replace_push_mb(p); |
| 850 | else |
| 851 | #endif |
| 852 | replace_push(*p++); |
| 853 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 854 | saved_line[curwin->w_cursor.col] = NUL; |
| 855 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 856 | |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 857 | if ((State & INSERT) && !(State & VREPLACE_FLAG)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 858 | { |
| 859 | p_extra = saved_line + curwin->w_cursor.col; |
| 860 | #ifdef FEAT_SMARTINDENT |
| 861 | if (do_si) /* need first char after new line break */ |
| 862 | { |
| 863 | p = skipwhite(p_extra); |
| 864 | first_char = *p; |
| 865 | } |
| 866 | #endif |
| 867 | #ifdef FEAT_COMMENTS |
| 868 | extra_len = (int)STRLEN(p_extra); |
| 869 | #endif |
| 870 | saved_char = *p_extra; |
| 871 | *p_extra = NUL; |
| 872 | } |
| 873 | |
| 874 | u_clearline(); /* cannot do "U" command when adding lines */ |
| 875 | #ifdef FEAT_SMARTINDENT |
| 876 | did_si = FALSE; |
| 877 | #endif |
| 878 | ai_col = 0; |
| 879 | |
| 880 | /* |
| 881 | * If we just did an auto-indent, then we didn't type anything on |
| 882 | * the prior line, and it should be truncated. Do this even if 'ai' is not |
| 883 | * set because automatically inserting a comment leader also sets did_ai. |
| 884 | */ |
| 885 | if (dir == FORWARD && did_ai) |
| 886 | trunc_line = TRUE; |
| 887 | |
| 888 | /* |
| 889 | * If 'autoindent' and/or 'smartindent' is set, try to figure out what |
| 890 | * indent to use for the new line. |
| 891 | */ |
| 892 | if (curbuf->b_p_ai |
| 893 | #ifdef FEAT_SMARTINDENT |
| 894 | || do_si |
| 895 | #endif |
| 896 | ) |
| 897 | { |
| 898 | /* |
| 899 | * count white space on current line |
| 900 | */ |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 901 | #ifdef FEAT_VARTABS |
| 902 | newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts, |
| 903 | curbuf->b_p_vts_array, FALSE); |
| 904 | #else |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 905 | newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE); |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 906 | #endif |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 907 | if (newindent == 0 && !(flags & OPENLINE_COM_LIST)) |
| 908 | newindent = second_line_indent; /* for ^^D command in insert mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 909 | |
| 910 | #ifdef FEAT_SMARTINDENT |
| 911 | /* |
| 912 | * Do smart indenting. |
| 913 | * In insert/replace mode (only when dir == FORWARD) |
| 914 | * we may move some text to the next line. If it starts with '{' |
| 915 | * don't add an indent. Fixes inserting a NL before '{' in line |
| 916 | * "if (condition) {" |
| 917 | */ |
| 918 | if (!trunc_line && do_si && *saved_line != NUL |
| 919 | && (p_extra == NULL || first_char != '{')) |
| 920 | { |
| 921 | char_u *ptr; |
| 922 | char_u last_char; |
| 923 | |
| 924 | old_cursor = curwin->w_cursor; |
| 925 | ptr = saved_line; |
| 926 | # ifdef FEAT_COMMENTS |
| 927 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 928 | lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 929 | else |
| 930 | lead_len = 0; |
| 931 | # endif |
| 932 | if (dir == FORWARD) |
| 933 | { |
| 934 | /* |
| 935 | * Skip preprocessor directives, unless they are |
| 936 | * recognised as comments. |
| 937 | */ |
| 938 | if ( |
| 939 | # ifdef FEAT_COMMENTS |
| 940 | lead_len == 0 && |
| 941 | # endif |
| 942 | ptr[0] == '#') |
| 943 | { |
| 944 | while (ptr[0] == '#' && curwin->w_cursor.lnum > 1) |
| 945 | ptr = ml_get(--curwin->w_cursor.lnum); |
| 946 | newindent = get_indent(); |
| 947 | } |
| 948 | # ifdef FEAT_COMMENTS |
| 949 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 950 | lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 951 | else |
| 952 | lead_len = 0; |
| 953 | if (lead_len > 0) |
| 954 | { |
| 955 | /* |
| 956 | * This case gets the following right: |
| 957 | * \* |
| 958 | * * A comment (read '\' as '/'). |
| 959 | * *\ |
| 960 | * #define IN_THE_WAY |
| 961 | * This should line up here; |
| 962 | */ |
| 963 | p = skipwhite(ptr); |
| 964 | if (p[0] == '/' && p[1] == '*') |
| 965 | p++; |
| 966 | if (p[0] == '*') |
| 967 | { |
| 968 | for (p++; *p; p++) |
| 969 | { |
| 970 | if (p[0] == '/' && p[-1] == '*') |
| 971 | { |
| 972 | /* |
| 973 | * End of C comment, indent should line up |
| 974 | * with the line containing the start of |
| 975 | * the comment |
| 976 | */ |
| 977 | curwin->w_cursor.col = (colnr_T)(p - ptr); |
| 978 | if ((pos = findmatch(NULL, NUL)) != NULL) |
| 979 | { |
| 980 | curwin->w_cursor.lnum = pos->lnum; |
| 981 | newindent = get_indent(); |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | else /* Not a comment line */ |
| 988 | # endif |
| 989 | { |
| 990 | /* Find last non-blank in line */ |
| 991 | p = ptr + STRLEN(ptr) - 1; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 992 | while (p > ptr && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 993 | --p; |
| 994 | last_char = *p; |
| 995 | |
| 996 | /* |
| 997 | * find the character just before the '{' or ';' |
| 998 | */ |
| 999 | if (last_char == '{' || last_char == ';') |
| 1000 | { |
| 1001 | if (p > ptr) |
| 1002 | --p; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1003 | while (p > ptr && VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1004 | --p; |
| 1005 | } |
| 1006 | /* |
| 1007 | * Try to catch lines that are split over multiple |
| 1008 | * lines. eg: |
| 1009 | * if (condition && |
| 1010 | * condition) { |
| 1011 | * Should line up here! |
| 1012 | * } |
| 1013 | */ |
| 1014 | if (*p == ')') |
| 1015 | { |
| 1016 | curwin->w_cursor.col = (colnr_T)(p - ptr); |
| 1017 | if ((pos = findmatch(NULL, '(')) != NULL) |
| 1018 | { |
| 1019 | curwin->w_cursor.lnum = pos->lnum; |
| 1020 | newindent = get_indent(); |
| 1021 | ptr = ml_get_curline(); |
| 1022 | } |
| 1023 | } |
| 1024 | /* |
| 1025 | * If last character is '{' do indent, without |
| 1026 | * checking for "if" and the like. |
| 1027 | */ |
| 1028 | if (last_char == '{') |
| 1029 | { |
| 1030 | did_si = TRUE; /* do indent */ |
| 1031 | no_si = TRUE; /* don't delete it when '{' typed */ |
| 1032 | } |
| 1033 | /* |
| 1034 | * Look for "if" and the like, use 'cinwords'. |
| 1035 | * Don't do this if the previous line ended in ';' or |
| 1036 | * '}'. |
| 1037 | */ |
| 1038 | else if (last_char != ';' && last_char != '}' |
| 1039 | && cin_is_cinword(ptr)) |
| 1040 | did_si = TRUE; |
| 1041 | } |
| 1042 | } |
| 1043 | else /* dir == BACKWARD */ |
| 1044 | { |
| 1045 | /* |
| 1046 | * Skip preprocessor directives, unless they are |
| 1047 | * recognised as comments. |
| 1048 | */ |
| 1049 | if ( |
| 1050 | # ifdef FEAT_COMMENTS |
| 1051 | lead_len == 0 && |
| 1052 | # endif |
| 1053 | ptr[0] == '#') |
| 1054 | { |
| 1055 | int was_backslashed = FALSE; |
| 1056 | |
| 1057 | while ((ptr[0] == '#' || was_backslashed) && |
| 1058 | curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 1059 | { |
| 1060 | if (*ptr && ptr[STRLEN(ptr) - 1] == '\\') |
| 1061 | was_backslashed = TRUE; |
| 1062 | else |
| 1063 | was_backslashed = FALSE; |
| 1064 | ptr = ml_get(++curwin->w_cursor.lnum); |
| 1065 | } |
| 1066 | if (was_backslashed) |
| 1067 | newindent = 0; /* Got to end of file */ |
| 1068 | else |
| 1069 | newindent = get_indent(); |
| 1070 | } |
| 1071 | p = skipwhite(ptr); |
| 1072 | if (*p == '}') /* if line starts with '}': do indent */ |
| 1073 | did_si = TRUE; |
| 1074 | else /* can delete indent when '{' typed */ |
| 1075 | can_si_back = TRUE; |
| 1076 | } |
| 1077 | curwin->w_cursor = old_cursor; |
| 1078 | } |
| 1079 | if (do_si) |
| 1080 | can_si = TRUE; |
| 1081 | #endif /* FEAT_SMARTINDENT */ |
| 1082 | |
| 1083 | did_ai = TRUE; |
| 1084 | } |
| 1085 | |
| 1086 | #ifdef FEAT_COMMENTS |
| 1087 | /* |
| 1088 | * Find out if the current line starts with a comment leader. |
| 1089 | * This may then be inserted in front of the new line. |
| 1090 | */ |
| 1091 | end_comment_pending = NUL; |
| 1092 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1093 | lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1094 | else |
| 1095 | lead_len = 0; |
| 1096 | if (lead_len > 0) |
| 1097 | { |
| 1098 | char_u *lead_repl = NULL; /* replaces comment leader */ |
| 1099 | int lead_repl_len = 0; /* length of *lead_repl */ |
| 1100 | char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ |
| 1101 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 1102 | char_u *comment_end = NULL; /* where lead_end has been found */ |
| 1103 | int extra_space = FALSE; /* append extra space */ |
| 1104 | int current_flag; |
| 1105 | int require_blank = FALSE; /* requires blank after middle */ |
| 1106 | char_u *p2; |
| 1107 | |
| 1108 | /* |
| 1109 | * If the comment leader has the start, middle or end flag, it may not |
| 1110 | * be used or may be replaced with the middle leader. |
| 1111 | */ |
| 1112 | for (p = lead_flags; *p && *p != ':'; ++p) |
| 1113 | { |
| 1114 | if (*p == COM_BLANK) |
| 1115 | { |
| 1116 | require_blank = TRUE; |
| 1117 | continue; |
| 1118 | } |
| 1119 | if (*p == COM_START || *p == COM_MIDDLE) |
| 1120 | { |
| 1121 | current_flag = *p; |
| 1122 | if (*p == COM_START) |
| 1123 | { |
| 1124 | /* |
| 1125 | * Doing "O" on a start of comment does not insert leader. |
| 1126 | */ |
| 1127 | if (dir == BACKWARD) |
| 1128 | { |
| 1129 | lead_len = 0; |
| 1130 | break; |
| 1131 | } |
| 1132 | |
| 1133 | /* find start of middle part */ |
| 1134 | (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); |
| 1135 | require_blank = FALSE; |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * Isolate the strings of the middle and end leader. |
| 1140 | */ |
| 1141 | while (*p && p[-1] != ':') /* find end of middle flags */ |
| 1142 | { |
| 1143 | if (*p == COM_BLANK) |
| 1144 | require_blank = TRUE; |
| 1145 | ++p; |
| 1146 | } |
| 1147 | (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); |
| 1148 | |
| 1149 | while (*p && p[-1] != ':') /* find end of end flags */ |
| 1150 | { |
| 1151 | /* Check whether we allow automatic ending of comments */ |
| 1152 | if (*p == COM_AUTO_END) |
| 1153 | end_comment_pending = -1; /* means we want to set it */ |
| 1154 | ++p; |
| 1155 | } |
| 1156 | n = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 1157 | |
| 1158 | if (end_comment_pending == -1) /* we can set it now */ |
| 1159 | end_comment_pending = lead_end[n - 1]; |
| 1160 | |
| 1161 | /* |
| 1162 | * If the end of the comment is in the same line, don't use |
| 1163 | * the comment leader. |
| 1164 | */ |
| 1165 | if (dir == FORWARD) |
| 1166 | { |
| 1167 | for (p = saved_line + lead_len; *p; ++p) |
| 1168 | if (STRNCMP(p, lead_end, n) == 0) |
| 1169 | { |
| 1170 | comment_end = p; |
| 1171 | lead_len = 0; |
| 1172 | break; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | * Doing "o" on a start of comment inserts the middle leader. |
| 1178 | */ |
| 1179 | if (lead_len > 0) |
| 1180 | { |
| 1181 | if (current_flag == COM_START) |
| 1182 | { |
| 1183 | lead_repl = lead_middle; |
| 1184 | lead_repl_len = (int)STRLEN(lead_middle); |
| 1185 | } |
| 1186 | |
| 1187 | /* |
| 1188 | * If we have hit RETURN immediately after the start |
| 1189 | * comment leader, then put a space after the middle |
| 1190 | * comment leader on the next line. |
| 1191 | */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1192 | if (!VIM_ISWHITE(saved_line[lead_len - 1]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1193 | && ((p_extra != NULL |
| 1194 | && (int)curwin->w_cursor.col == lead_len) |
| 1195 | || (p_extra == NULL |
| 1196 | && saved_line[lead_len] == NUL) |
| 1197 | || require_blank)) |
| 1198 | extra_space = TRUE; |
| 1199 | } |
| 1200 | break; |
| 1201 | } |
| 1202 | if (*p == COM_END) |
| 1203 | { |
| 1204 | /* |
| 1205 | * Doing "o" on the end of a comment does not insert leader. |
| 1206 | * Remember where the end is, might want to use it to find the |
| 1207 | * start (for C-comments). |
| 1208 | */ |
| 1209 | if (dir == FORWARD) |
| 1210 | { |
| 1211 | comment_end = skipwhite(saved_line); |
| 1212 | lead_len = 0; |
| 1213 | break; |
| 1214 | } |
| 1215 | |
| 1216 | /* |
| 1217 | * Doing "O" on the end of a comment inserts the middle leader. |
| 1218 | * Find the string for the middle leader, searching backwards. |
| 1219 | */ |
| 1220 | while (p > curbuf->b_p_com && *p != ',') |
| 1221 | --p; |
| 1222 | for (lead_repl = p; lead_repl > curbuf->b_p_com |
| 1223 | && lead_repl[-1] != ':'; --lead_repl) |
| 1224 | ; |
| 1225 | lead_repl_len = (int)(p - lead_repl); |
| 1226 | |
| 1227 | /* We can probably always add an extra space when doing "O" on |
| 1228 | * the comment-end */ |
| 1229 | extra_space = TRUE; |
| 1230 | |
| 1231 | /* Check whether we allow automatic ending of comments */ |
| 1232 | for (p2 = p; *p2 && *p2 != ':'; p2++) |
| 1233 | { |
| 1234 | if (*p2 == COM_AUTO_END) |
| 1235 | end_comment_pending = -1; /* means we want to set it */ |
| 1236 | } |
| 1237 | if (end_comment_pending == -1) |
| 1238 | { |
| 1239 | /* Find last character in end-comment string */ |
| 1240 | while (*p2 && *p2 != ',') |
| 1241 | p2++; |
| 1242 | end_comment_pending = p2[-1]; |
| 1243 | } |
| 1244 | break; |
| 1245 | } |
| 1246 | if (*p == COM_FIRST) |
| 1247 | { |
| 1248 | /* |
| 1249 | * Comment leader for first line only: Don't repeat leader |
| 1250 | * when using "O", blank out leader when using "o". |
| 1251 | */ |
| 1252 | if (dir == BACKWARD) |
| 1253 | lead_len = 0; |
| 1254 | else |
| 1255 | { |
| 1256 | lead_repl = (char_u *)""; |
| 1257 | lead_repl_len = 0; |
| 1258 | } |
| 1259 | break; |
| 1260 | } |
| 1261 | } |
| 1262 | if (lead_len) |
| 1263 | { |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 1264 | /* allocate buffer (may concatenate p_extra later) */ |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1265 | leader = alloc(lead_len + lead_repl_len + extra_space + extra_len |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 1266 | + (second_line_indent > 0 ? second_line_indent : 0) + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1267 | allocated = leader; /* remember to free it later */ |
| 1268 | |
| 1269 | if (leader == NULL) |
| 1270 | lead_len = 0; |
| 1271 | else |
| 1272 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 1273 | vim_strncpy(leader, saved_line, lead_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1274 | |
| 1275 | /* |
| 1276 | * Replace leader with lead_repl, right or left adjusted |
| 1277 | */ |
| 1278 | if (lead_repl != NULL) |
| 1279 | { |
| 1280 | int c = 0; |
| 1281 | int off = 0; |
| 1282 | |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1283 | for (p = lead_flags; *p != NUL && *p != ':'; ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1284 | { |
| 1285 | if (*p == COM_RIGHT || *p == COM_LEFT) |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1286 | c = *p++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1287 | else if (VIM_ISDIGIT(*p) || *p == '-') |
| 1288 | off = getdigits(&p); |
Bram Moolenaar | d7d5b47 | 2009-11-11 16:30:08 +0000 | [diff] [blame] | 1289 | else |
| 1290 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1291 | } |
| 1292 | if (c == COM_RIGHT) /* right adjusted leader */ |
| 1293 | { |
| 1294 | /* find last non-white in the leader to line up with */ |
| 1295 | for (p = leader + lead_len - 1; p > leader |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1296 | && VIM_ISWHITE(*p); --p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1297 | ; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1298 | ++p; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1299 | |
| 1300 | #ifdef FEAT_MBYTE |
| 1301 | /* Compute the length of the replaced characters in |
| 1302 | * screen characters, not bytes. */ |
| 1303 | { |
| 1304 | int repl_size = vim_strnsize(lead_repl, |
| 1305 | lead_repl_len); |
| 1306 | int old_size = 0; |
| 1307 | char_u *endp = p; |
| 1308 | int l; |
| 1309 | |
| 1310 | while (old_size < repl_size && p > leader) |
| 1311 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 1312 | MB_PTR_BACK(leader, p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1313 | old_size += ptr2cells(p); |
| 1314 | } |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1315 | l = lead_repl_len - (int)(endp - p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1316 | if (l != 0) |
| 1317 | mch_memmove(endp + l, endp, |
| 1318 | (size_t)((leader + lead_len) - endp)); |
| 1319 | lead_len += l; |
| 1320 | } |
| 1321 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1322 | if (p < leader + lead_repl_len) |
| 1323 | p = leader; |
| 1324 | else |
| 1325 | p -= lead_repl_len; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1326 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1327 | mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
| 1328 | if (p + lead_repl_len > leader + lead_len) |
| 1329 | p[lead_repl_len] = NUL; |
| 1330 | |
| 1331 | /* blank-out any other chars from the old leader. */ |
| 1332 | while (--p >= leader) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1333 | { |
| 1334 | #ifdef FEAT_MBYTE |
| 1335 | int l = mb_head_off(leader, p); |
| 1336 | |
| 1337 | if (l > 1) |
| 1338 | { |
| 1339 | p -= l; |
| 1340 | if (ptr2cells(p) > 1) |
| 1341 | { |
| 1342 | p[1] = ' '; |
| 1343 | --l; |
| 1344 | } |
| 1345 | mch_memmove(p + 1, p + l + 1, |
| 1346 | (size_t)((leader + lead_len) - (p + l + 1))); |
| 1347 | lead_len -= l; |
| 1348 | *p = ' '; |
| 1349 | } |
| 1350 | else |
| 1351 | #endif |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1352 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1353 | *p = ' '; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1354 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1355 | } |
| 1356 | else /* left adjusted leader */ |
| 1357 | { |
| 1358 | p = skipwhite(leader); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1359 | #ifdef FEAT_MBYTE |
| 1360 | /* Compute the length of the replaced characters in |
| 1361 | * screen characters, not bytes. Move the part that is |
| 1362 | * not to be overwritten. */ |
| 1363 | { |
| 1364 | int repl_size = vim_strnsize(lead_repl, |
| 1365 | lead_repl_len); |
| 1366 | int i; |
| 1367 | int l; |
| 1368 | |
Bram Moolenaar | dc633cf | 2016-04-23 14:33:19 +0200 | [diff] [blame] | 1369 | for (i = 0; i < lead_len && p[i] != NUL; i += l) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1370 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1371 | l = (*mb_ptr2len)(p + i); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1372 | if (vim_strnsize(p, i + l) > repl_size) |
| 1373 | break; |
| 1374 | } |
| 1375 | if (i != lead_repl_len) |
| 1376 | { |
| 1377 | mch_memmove(p + lead_repl_len, p + i, |
Bram Moolenaar | 2d7ff05 | 2009-11-17 15:08:26 +0000 | [diff] [blame] | 1378 | (size_t)(lead_len - i - (p - leader))); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1379 | lead_len += lead_repl_len - i; |
| 1380 | } |
| 1381 | } |
| 1382 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1383 | mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
| 1384 | |
| 1385 | /* Replace any remaining non-white chars in the old |
| 1386 | * leader by spaces. Keep Tabs, the indent must |
| 1387 | * remain the same. */ |
| 1388 | for (p += lead_repl_len; p < leader + lead_len; ++p) |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1389 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1390 | { |
| 1391 | /* Don't put a space before a TAB. */ |
| 1392 | if (p + 1 < leader + lead_len && p[1] == TAB) |
| 1393 | { |
| 1394 | --lead_len; |
| 1395 | mch_memmove(p, p + 1, |
| 1396 | (leader + lead_len) - p); |
| 1397 | } |
| 1398 | else |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1399 | { |
| 1400 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1401 | int l = (*mb_ptr2len)(p); |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1402 | |
| 1403 | if (l > 1) |
| 1404 | { |
| 1405 | if (ptr2cells(p) > 1) |
| 1406 | { |
| 1407 | /* Replace a double-wide char with |
| 1408 | * two spaces */ |
| 1409 | --l; |
| 1410 | *p++ = ' '; |
| 1411 | } |
| 1412 | mch_memmove(p + 1, p + l, |
| 1413 | (leader + lead_len) - p); |
| 1414 | lead_len -= l - 1; |
| 1415 | } |
| 1416 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1417 | *p = ' '; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 1418 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1419 | } |
| 1420 | *p = NUL; |
| 1421 | } |
| 1422 | |
| 1423 | /* Recompute the indent, it may have changed. */ |
| 1424 | if (curbuf->b_p_ai |
| 1425 | #ifdef FEAT_SMARTINDENT |
| 1426 | || do_si |
| 1427 | #endif |
| 1428 | ) |
Bram Moolenaar | 04958cb | 2018-06-23 19:23:02 +0200 | [diff] [blame] | 1429 | #ifdef FEAT_VARTABS |
| 1430 | newindent = get_indent_str_vtab(leader, curbuf->b_p_ts, |
| 1431 | curbuf->b_p_vts_array, FALSE); |
| 1432 | #else |
| 1433 | newindent = get_indent_str(leader, |
| 1434 | (int)curbuf->b_p_ts, FALSE); |
| 1435 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1436 | |
| 1437 | /* Add the indent offset */ |
| 1438 | if (newindent + off < 0) |
| 1439 | { |
| 1440 | off = -newindent; |
| 1441 | newindent = 0; |
| 1442 | } |
| 1443 | else |
| 1444 | newindent += off; |
| 1445 | |
| 1446 | /* Correct trailing spaces for the shift, so that |
| 1447 | * alignment remains equal. */ |
| 1448 | while (off > 0 && lead_len > 0 |
| 1449 | && leader[lead_len - 1] == ' ') |
| 1450 | { |
| 1451 | /* Don't do it when there is a tab before the space */ |
| 1452 | if (vim_strchr(skipwhite(leader), '\t') != NULL) |
| 1453 | break; |
| 1454 | --lead_len; |
| 1455 | --off; |
| 1456 | } |
| 1457 | |
| 1458 | /* If the leader ends in white space, don't add an |
| 1459 | * extra space */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1460 | if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1461 | extra_space = FALSE; |
| 1462 | leader[lead_len] = NUL; |
| 1463 | } |
| 1464 | |
| 1465 | if (extra_space) |
| 1466 | { |
| 1467 | leader[lead_len++] = ' '; |
| 1468 | leader[lead_len] = NUL; |
| 1469 | } |
| 1470 | |
| 1471 | newcol = lead_len; |
| 1472 | |
| 1473 | /* |
| 1474 | * if a new indent will be set below, remove the indent that |
| 1475 | * is in the comment leader |
| 1476 | */ |
| 1477 | if (newindent |
| 1478 | #ifdef FEAT_SMARTINDENT |
| 1479 | || did_si |
| 1480 | #endif |
| 1481 | ) |
| 1482 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1483 | while (lead_len && VIM_ISWHITE(*leader)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1484 | { |
| 1485 | --lead_len; |
| 1486 | --newcol; |
| 1487 | ++leader; |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | } |
| 1492 | #ifdef FEAT_SMARTINDENT |
| 1493 | did_si = can_si = FALSE; |
| 1494 | #endif |
| 1495 | } |
| 1496 | else if (comment_end != NULL) |
| 1497 | { |
| 1498 | /* |
| 1499 | * We have finished a comment, so we don't use the leader. |
| 1500 | * If this was a C-comment and 'ai' or 'si' is set do a normal |
| 1501 | * indent to align with the line containing the start of the |
| 1502 | * comment. |
| 1503 | */ |
| 1504 | if (comment_end[0] == '*' && comment_end[1] == '/' && |
| 1505 | (curbuf->b_p_ai |
| 1506 | #ifdef FEAT_SMARTINDENT |
| 1507 | || do_si |
| 1508 | #endif |
| 1509 | )) |
| 1510 | { |
| 1511 | old_cursor = curwin->w_cursor; |
| 1512 | curwin->w_cursor.col = (colnr_T)(comment_end - saved_line); |
| 1513 | if ((pos = findmatch(NULL, NUL)) != NULL) |
| 1514 | { |
| 1515 | curwin->w_cursor.lnum = pos->lnum; |
| 1516 | newindent = get_indent(); |
| 1517 | } |
| 1518 | curwin->w_cursor = old_cursor; |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | #endif |
| 1523 | |
| 1524 | /* (State == INSERT || State == REPLACE), only when dir == FORWARD */ |
| 1525 | if (p_extra != NULL) |
| 1526 | { |
| 1527 | *p_extra = saved_char; /* restore char that NUL replaced */ |
| 1528 | |
| 1529 | /* |
| 1530 | * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first |
| 1531 | * non-blank. |
| 1532 | * |
| 1533 | * When in REPLACE mode, put the deleted blanks on the replace stack, |
| 1534 | * preceded by a NUL, so they can be put back when a BS is entered. |
| 1535 | */ |
| 1536 | if (REPLACE_NORMAL(State)) |
| 1537 | replace_push(NUL); /* end of extra blanks */ |
| 1538 | if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES)) |
| 1539 | { |
| 1540 | while ((*p_extra == ' ' || *p_extra == '\t') |
| 1541 | #ifdef FEAT_MBYTE |
| 1542 | && (!enc_utf8 |
| 1543 | || !utf_iscomposing(utf_ptr2char(p_extra + 1))) |
| 1544 | #endif |
| 1545 | ) |
| 1546 | { |
| 1547 | if (REPLACE_NORMAL(State)) |
| 1548 | replace_push(*p_extra); |
| 1549 | ++p_extra; |
| 1550 | ++less_cols_off; |
| 1551 | } |
| 1552 | } |
| 1553 | if (*p_extra != NUL) |
| 1554 | did_ai = FALSE; /* append some text, don't truncate now */ |
| 1555 | |
| 1556 | /* columns for marks adjusted for removed columns */ |
| 1557 | less_cols = (int)(p_extra - saved_line); |
| 1558 | } |
| 1559 | |
| 1560 | if (p_extra == NULL) |
| 1561 | p_extra = (char_u *)""; /* append empty line */ |
| 1562 | |
| 1563 | #ifdef FEAT_COMMENTS |
| 1564 | /* concatenate leader and p_extra, if there is a leader */ |
| 1565 | if (lead_len) |
| 1566 | { |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1567 | if (flags & OPENLINE_COM_LIST && second_line_indent > 0) |
| 1568 | { |
| 1569 | int i; |
Bram Moolenaar | 3610578 | 2012-06-14 20:59:25 +0200 | [diff] [blame] | 1570 | int padding = second_line_indent |
| 1571 | - (newindent + (int)STRLEN(leader)); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1572 | |
| 1573 | /* Here whitespace is inserted after the comment char. |
| 1574 | * Below, set_indent(newindent, SIN_INSERT) will insert the |
| 1575 | * whitespace needed before the comment char. */ |
| 1576 | for (i = 0; i < padding; i++) |
| 1577 | { |
| 1578 | STRCAT(leader, " "); |
Bram Moolenaar | 5fb9ec5 | 2012-07-25 16:10:03 +0200 | [diff] [blame] | 1579 | less_cols--; |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1580 | newcol++; |
| 1581 | } |
| 1582 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1583 | STRCAT(leader, p_extra); |
| 1584 | p_extra = leader; |
| 1585 | did_ai = TRUE; /* So truncating blanks works with comments */ |
| 1586 | less_cols -= lead_len; |
| 1587 | } |
| 1588 | else |
| 1589 | end_comment_pending = NUL; /* turns out there was no leader */ |
| 1590 | #endif |
| 1591 | |
| 1592 | old_cursor = curwin->w_cursor; |
| 1593 | if (dir == BACKWARD) |
| 1594 | --curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1595 | if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1596 | { |
| 1597 | if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE) |
| 1598 | == FAIL) |
| 1599 | goto theend; |
| 1600 | /* Postpone calling changed_lines(), because it would mess up folding |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 1601 | * with markers. |
| 1602 | * 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] | 1603 | * be marks there. But still needed in diff mode. */ |
| 1604 | if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count |
| 1605 | #ifdef FEAT_DIFF |
| 1606 | || curwin->w_p_diff |
| 1607 | #endif |
| 1608 | ) |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 1609 | mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1610 | did_append = TRUE; |
| 1611 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1612 | else |
| 1613 | { |
| 1614 | /* |
| 1615 | * In VREPLACE mode we are starting to replace the next line. |
| 1616 | */ |
| 1617 | curwin->w_cursor.lnum++; |
| 1618 | if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed) |
| 1619 | { |
| 1620 | /* In case we NL to a new line, BS to the previous one, and NL |
| 1621 | * again, we don't want to save the new line for undo twice. |
| 1622 | */ |
| 1623 | (void)u_save_cursor(); /* errors are ignored! */ |
| 1624 | vr_lines_changed++; |
| 1625 | } |
| 1626 | ml_replace(curwin->w_cursor.lnum, p_extra, TRUE); |
| 1627 | changed_bytes(curwin->w_cursor.lnum, 0); |
| 1628 | curwin->w_cursor.lnum--; |
| 1629 | did_append = FALSE; |
| 1630 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1631 | |
| 1632 | if (newindent |
| 1633 | #ifdef FEAT_SMARTINDENT |
| 1634 | || did_si |
| 1635 | #endif |
| 1636 | ) |
| 1637 | { |
| 1638 | ++curwin->w_cursor.lnum; |
| 1639 | #ifdef FEAT_SMARTINDENT |
| 1640 | if (did_si) |
| 1641 | { |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 1642 | int sw = (int)get_sw_value(curbuf); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 1643 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1644 | if (p_sr) |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 1645 | newindent -= newindent % sw; |
| 1646 | newindent += sw; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1647 | } |
| 1648 | #endif |
Bram Moolenaar | 5002c29 | 2007-07-24 13:26:15 +0000 | [diff] [blame] | 1649 | /* Copy the indent */ |
| 1650 | if (curbuf->b_p_ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1651 | { |
| 1652 | (void)copy_indent(newindent, saved_line); |
| 1653 | |
| 1654 | /* |
| 1655 | * Set the 'preserveindent' option so that any further screwing |
| 1656 | * with the line doesn't entirely destroy our efforts to preserve |
| 1657 | * it. It gets restored at the function end. |
| 1658 | */ |
| 1659 | curbuf->b_p_pi = TRUE; |
| 1660 | } |
| 1661 | else |
| 1662 | (void)set_indent(newindent, SIN_INSERT); |
| 1663 | less_cols -= curwin->w_cursor.col; |
| 1664 | |
| 1665 | ai_col = curwin->w_cursor.col; |
| 1666 | |
| 1667 | /* |
| 1668 | * In REPLACE mode, for each character in the new indent, there must |
| 1669 | * be a NUL on the replace stack, for when it is deleted with BS |
| 1670 | */ |
| 1671 | if (REPLACE_NORMAL(State)) |
| 1672 | for (n = 0; n < (int)curwin->w_cursor.col; ++n) |
| 1673 | replace_push(NUL); |
| 1674 | newcol += curwin->w_cursor.col; |
| 1675 | #ifdef FEAT_SMARTINDENT |
| 1676 | if (no_si) |
| 1677 | did_si = FALSE; |
| 1678 | #endif |
| 1679 | } |
| 1680 | |
| 1681 | #ifdef FEAT_COMMENTS |
| 1682 | /* |
| 1683 | * In REPLACE mode, for each character in the extra leader, there must be |
| 1684 | * a NUL on the replace stack, for when it is deleted with BS. |
| 1685 | */ |
| 1686 | if (REPLACE_NORMAL(State)) |
| 1687 | while (lead_len-- > 0) |
| 1688 | replace_push(NUL); |
| 1689 | #endif |
| 1690 | |
| 1691 | curwin->w_cursor = old_cursor; |
| 1692 | |
| 1693 | if (dir == FORWARD) |
| 1694 | { |
| 1695 | if (trunc_line || (State & INSERT)) |
| 1696 | { |
| 1697 | /* truncate current line at cursor */ |
| 1698 | saved_line[curwin->w_cursor.col] = NUL; |
| 1699 | /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */ |
| 1700 | if (trunc_line && !(flags & OPENLINE_KEEPTRAIL)) |
| 1701 | truncate_spaces(saved_line); |
| 1702 | ml_replace(curwin->w_cursor.lnum, saved_line, FALSE); |
| 1703 | saved_line = NULL; |
| 1704 | if (did_append) |
| 1705 | { |
| 1706 | changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, |
| 1707 | curwin->w_cursor.lnum + 1, 1L); |
| 1708 | did_append = FALSE; |
| 1709 | |
| 1710 | /* Move marks after the line break to the new line. */ |
| 1711 | if (flags & OPENLINE_MARKFIX) |
| 1712 | mark_col_adjust(curwin->w_cursor.lnum, |
| 1713 | curwin->w_cursor.col + less_cols_off, |
| 1714 | 1L, (long)-less_cols); |
| 1715 | } |
| 1716 | else |
| 1717 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 1718 | } |
| 1719 | |
| 1720 | /* |
| 1721 | * Put the cursor on the new line. Careful: the scrollup() above may |
| 1722 | * have moved w_cursor, we must use old_cursor. |
| 1723 | */ |
| 1724 | curwin->w_cursor.lnum = old_cursor.lnum + 1; |
| 1725 | } |
| 1726 | if (did_append) |
| 1727 | changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L); |
| 1728 | |
| 1729 | curwin->w_cursor.col = newcol; |
| 1730 | #ifdef FEAT_VIRTUALEDIT |
| 1731 | curwin->w_cursor.coladd = 0; |
| 1732 | #endif |
| 1733 | |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 1734 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1735 | /* |
| 1736 | * In VREPLACE mode, we are handling the replace stack ourselves, so stop |
| 1737 | * fixthisline() from doing it (via change_indent()) by telling it we're in |
| 1738 | * normal INSERT mode. |
| 1739 | */ |
| 1740 | if (State & VREPLACE_FLAG) |
| 1741 | { |
| 1742 | vreplace_mode = State; /* So we know to put things right later */ |
| 1743 | State = INSERT; |
| 1744 | } |
| 1745 | else |
| 1746 | vreplace_mode = 0; |
| 1747 | #endif |
| 1748 | #ifdef FEAT_LISP |
| 1749 | /* |
| 1750 | * May do lisp indenting. |
| 1751 | */ |
| 1752 | if (!p_paste |
| 1753 | # ifdef FEAT_COMMENTS |
| 1754 | && leader == NULL |
| 1755 | # endif |
| 1756 | && curbuf->b_p_lisp |
| 1757 | && curbuf->b_p_ai) |
| 1758 | { |
| 1759 | fixthisline(get_lisp_indent); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1760 | ai_col = (colnr_T)getwhitecols_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1761 | } |
| 1762 | #endif |
| 1763 | #ifdef FEAT_CINDENT |
| 1764 | /* |
| 1765 | * May do indenting after opening a new line. |
| 1766 | */ |
| 1767 | if (!p_paste |
| 1768 | && (curbuf->b_p_cin |
| 1769 | # ifdef FEAT_EVAL |
| 1770 | || *curbuf->b_p_inde != NUL |
| 1771 | # endif |
| 1772 | ) |
| 1773 | && in_cinkeys(dir == FORWARD |
| 1774 | ? KEY_OPEN_FORW |
| 1775 | : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))) |
| 1776 | { |
| 1777 | do_c_expr_indent(); |
Bram Moolenaar | e2e69e4 | 2017-09-02 20:30:35 +0200 | [diff] [blame] | 1778 | ai_col = (colnr_T)getwhitecols_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1779 | } |
| 1780 | #endif |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 1781 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1782 | if (vreplace_mode != 0) |
| 1783 | State = vreplace_mode; |
| 1784 | #endif |
| 1785 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1786 | /* |
| 1787 | * Finally, VREPLACE gets the stuff on the new line, then puts back the |
| 1788 | * original line, and inserts the new stuff char by char, pushing old stuff |
| 1789 | * onto the replace stack (via ins_char()). |
| 1790 | */ |
| 1791 | if (State & VREPLACE_FLAG) |
| 1792 | { |
| 1793 | /* Put new line in p_extra */ |
| 1794 | p_extra = vim_strsave(ml_get_curline()); |
| 1795 | if (p_extra == NULL) |
| 1796 | goto theend; |
| 1797 | |
| 1798 | /* Put back original line */ |
| 1799 | ml_replace(curwin->w_cursor.lnum, next_line, FALSE); |
| 1800 | |
| 1801 | /* Insert new stuff into line again */ |
| 1802 | curwin->w_cursor.col = 0; |
| 1803 | #ifdef FEAT_VIRTUALEDIT |
| 1804 | curwin->w_cursor.coladd = 0; |
| 1805 | #endif |
| 1806 | ins_bytes(p_extra); /* will call changed_bytes() */ |
| 1807 | vim_free(p_extra); |
| 1808 | next_line = NULL; |
| 1809 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1810 | |
Bram Moolenaar | 24a2d72 | 2018-04-24 19:36:43 +0200 | [diff] [blame] | 1811 | retval = OK; /* success! */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1812 | theend: |
| 1813 | curbuf->b_p_pi = saved_pi; |
| 1814 | vim_free(saved_line); |
| 1815 | vim_free(next_line); |
| 1816 | vim_free(allocated); |
| 1817 | return retval; |
| 1818 | } |
| 1819 | |
| 1820 | #if defined(FEAT_COMMENTS) || defined(PROTO) |
| 1821 | /* |
Bram Moolenaar | 2c019c8 | 2013-10-06 17:46:56 +0200 | [diff] [blame] | 1822 | * get_leader_len() returns the length in bytes of the prefix of the given |
| 1823 | * string which introduces a comment. If this string is not a comment then |
| 1824 | * 0 is returned. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1825 | * When "flags" is not NULL, it is set to point to the flags of the recognized |
| 1826 | * comment leader. |
| 1827 | * "backward" must be true for the "O" command. |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1828 | * If "include_space" is set, include trailing whitespace while calculating the |
| 1829 | * length. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1830 | */ |
| 1831 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1832 | get_leader_len( |
| 1833 | char_u *line, |
| 1834 | char_u **flags, |
| 1835 | int backward, |
| 1836 | int include_space) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1837 | { |
| 1838 | int i, j; |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1839 | int result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1840 | int got_com = FALSE; |
| 1841 | int found_one; |
| 1842 | char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ |
| 1843 | char_u *string; /* pointer to comment string */ |
| 1844 | char_u *list; |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1845 | int middle_match_len = 0; |
| 1846 | char_u *prev_list; |
Bram Moolenaar | 05da428 | 2011-05-10 14:44:11 +0200 | [diff] [blame] | 1847 | char_u *saved_flags = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1848 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1849 | result = i = 0; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1850 | while (VIM_ISWHITE(line[i])) /* leading white space is ignored */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1851 | ++i; |
| 1852 | |
| 1853 | /* |
| 1854 | * Repeat to match several nested comment strings. |
| 1855 | */ |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1856 | while (line[i] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1857 | { |
| 1858 | /* |
| 1859 | * scan through the 'comments' option for a match |
| 1860 | */ |
| 1861 | found_one = FALSE; |
| 1862 | for (list = curbuf->b_p_com; *list; ) |
| 1863 | { |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1864 | /* Get one option part into part_buf[]. Advance "list" to next |
| 1865 | * one. Put "string" at start of string. */ |
| 1866 | if (!got_com && flags != NULL) |
| 1867 | *flags = list; /* remember where flags started */ |
| 1868 | prev_list = list; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1869 | (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
| 1870 | string = vim_strchr(part_buf, ':'); |
| 1871 | if (string == NULL) /* missing ':', ignore this part */ |
| 1872 | continue; |
| 1873 | *string++ = NUL; /* isolate flags from string */ |
| 1874 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1875 | /* If we found a middle match previously, use that match when this |
| 1876 | * is not a middle or end. */ |
| 1877 | if (middle_match_len != 0 |
| 1878 | && vim_strchr(part_buf, COM_MIDDLE) == NULL |
| 1879 | && vim_strchr(part_buf, COM_END) == NULL) |
| 1880 | break; |
| 1881 | |
| 1882 | /* When we already found a nested comment, only accept further |
| 1883 | * nested comments. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1884 | if (got_com && vim_strchr(part_buf, COM_NEST) == NULL) |
| 1885 | continue; |
| 1886 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1887 | /* When 'O' flag present and using "O" command skip this one. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1888 | if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL) |
| 1889 | continue; |
| 1890 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1891 | /* Line contents and string must match. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1892 | * When string starts with white space, must have some white space |
| 1893 | * (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] | 1894 | * TABs and spaces). */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1895 | if (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1896 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1897 | if (i == 0 || !VIM_ISWHITE(line[i - 1])) |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 1898 | continue; /* missing white space */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1899 | while (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1900 | ++string; |
| 1901 | } |
| 1902 | for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) |
| 1903 | ; |
| 1904 | if (string[j] != NUL) |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1905 | continue; /* string doesn't match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1906 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1907 | /* When 'b' flag used, there must be white space or an |
| 1908 | * end-of-line after the string in the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1909 | if (vim_strchr(part_buf, COM_BLANK) != NULL |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1910 | && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1911 | continue; |
| 1912 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1913 | /* We have found a match, stop searching unless this is a middle |
| 1914 | * comment. The middle comment can be a substring of the end |
| 1915 | * comment in which case it's better to return the length of the |
| 1916 | * end comment and its flags. Thus we keep searching with middle |
| 1917 | * and end matches and use an end match if it matches better. */ |
| 1918 | if (vim_strchr(part_buf, COM_MIDDLE) != NULL) |
| 1919 | { |
| 1920 | if (middle_match_len == 0) |
| 1921 | { |
| 1922 | middle_match_len = j; |
| 1923 | saved_flags = prev_list; |
| 1924 | } |
| 1925 | continue; |
| 1926 | } |
| 1927 | if (middle_match_len != 0 && j > middle_match_len) |
| 1928 | /* Use this match instead of the middle match, since it's a |
| 1929 | * longer thus better match. */ |
| 1930 | middle_match_len = 0; |
| 1931 | |
| 1932 | if (middle_match_len == 0) |
| 1933 | i += j; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1934 | found_one = TRUE; |
| 1935 | break; |
| 1936 | } |
| 1937 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1938 | if (middle_match_len != 0) |
| 1939 | { |
| 1940 | /* Use the previously found middle match after failing to find a |
| 1941 | * match with an end. */ |
| 1942 | if (!got_com && flags != NULL) |
| 1943 | *flags = saved_flags; |
| 1944 | i += middle_match_len; |
| 1945 | found_one = TRUE; |
| 1946 | } |
| 1947 | |
| 1948 | /* No match found, stop scanning. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1949 | if (!found_one) |
| 1950 | break; |
| 1951 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1952 | result = i; |
| 1953 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1954 | /* Include any trailing white space. */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 1955 | while (VIM_ISWHITE(line[i])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1956 | ++i; |
| 1957 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1958 | if (include_space) |
| 1959 | result = i; |
| 1960 | |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1961 | /* If this comment doesn't nest, stop here. */ |
| 1962 | got_com = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1963 | if (vim_strchr(part_buf, COM_NEST) == NULL) |
| 1964 | break; |
| 1965 | } |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1966 | return result; |
| 1967 | } |
Bram Moolenaar | a4271d5 | 2011-05-10 13:38:27 +0200 | [diff] [blame] | 1968 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1969 | /* |
| 1970 | * Return the offset at which the last comment in line starts. If there is no |
| 1971 | * comment in the whole line, -1 is returned. |
| 1972 | * |
| 1973 | * When "flags" is not null, it is set to point to the flags describing the |
| 1974 | * recognized comment leader. |
| 1975 | */ |
| 1976 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 1977 | get_last_leader_offset(char_u *line, char_u **flags) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 1978 | { |
| 1979 | int result = -1; |
| 1980 | int i, j; |
| 1981 | int lower_check_bound = 0; |
| 1982 | char_u *string; |
| 1983 | char_u *com_leader; |
| 1984 | char_u *com_flags; |
| 1985 | char_u *list; |
| 1986 | int found_one; |
| 1987 | char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ |
| 1988 | |
| 1989 | /* |
| 1990 | * Repeat to match several nested comment strings. |
| 1991 | */ |
| 1992 | i = (int)STRLEN(line); |
| 1993 | while (--i >= lower_check_bound) |
| 1994 | { |
| 1995 | /* |
| 1996 | * scan through the 'comments' option for a match |
| 1997 | */ |
| 1998 | found_one = FALSE; |
| 1999 | for (list = curbuf->b_p_com; *list; ) |
| 2000 | { |
| 2001 | char_u *flags_save = list; |
| 2002 | |
| 2003 | /* |
| 2004 | * Get one option part into part_buf[]. Advance list to next one. |
| 2005 | * put string at start of string. |
| 2006 | */ |
| 2007 | (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
| 2008 | string = vim_strchr(part_buf, ':'); |
| 2009 | if (string == NULL) /* If everything is fine, this cannot actually |
| 2010 | * happen. */ |
| 2011 | { |
| 2012 | continue; |
| 2013 | } |
| 2014 | *string++ = NUL; /* Isolate flags from string. */ |
| 2015 | com_leader = string; |
| 2016 | |
| 2017 | /* |
| 2018 | * Line contents and string must match. |
| 2019 | * When string starts with white space, must have some white space |
| 2020 | * (but the amount does not need to match, there might be a mix of |
| 2021 | * TABs and spaces). |
| 2022 | */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2023 | if (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2024 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2025 | if (i == 0 || !VIM_ISWHITE(line[i - 1])) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2026 | continue; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2027 | while (VIM_ISWHITE(string[0])) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2028 | ++string; |
| 2029 | } |
| 2030 | for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) |
| 2031 | /* do nothing */; |
| 2032 | if (string[j] != NUL) |
| 2033 | continue; |
| 2034 | |
| 2035 | /* |
| 2036 | * When 'b' flag used, there must be white space or an |
| 2037 | * end-of-line after the string in the line. |
| 2038 | */ |
| 2039 | if (vim_strchr(part_buf, COM_BLANK) != NULL |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2040 | && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2041 | { |
| 2042 | continue; |
| 2043 | } |
| 2044 | |
| 2045 | /* |
| 2046 | * We have found a match, stop searching. |
| 2047 | */ |
| 2048 | found_one = TRUE; |
| 2049 | |
| 2050 | if (flags) |
| 2051 | *flags = flags_save; |
| 2052 | com_flags = flags_save; |
| 2053 | |
| 2054 | break; |
| 2055 | } |
| 2056 | |
| 2057 | if (found_one) |
| 2058 | { |
| 2059 | char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */ |
| 2060 | int len1, len2, off; |
| 2061 | |
| 2062 | result = i; |
| 2063 | /* |
| 2064 | * If this comment nests, continue searching. |
| 2065 | */ |
| 2066 | if (vim_strchr(part_buf, COM_NEST) != NULL) |
| 2067 | continue; |
| 2068 | |
| 2069 | lower_check_bound = i; |
| 2070 | |
| 2071 | /* Let's verify whether the comment leader found is a substring |
| 2072 | * of other comment leaders. If it is, let's adjust the |
| 2073 | * lower_check_bound so that we make sure that we have determined |
| 2074 | * the comment leader correctly. |
| 2075 | */ |
| 2076 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2077 | while (VIM_ISWHITE(*com_leader)) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2078 | ++com_leader; |
| 2079 | len1 = (int)STRLEN(com_leader); |
| 2080 | |
| 2081 | for (list = curbuf->b_p_com; *list; ) |
| 2082 | { |
| 2083 | char_u *flags_save = list; |
| 2084 | |
| 2085 | (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ","); |
| 2086 | if (flags_save == com_flags) |
| 2087 | continue; |
| 2088 | string = vim_strchr(part_buf2, ':'); |
| 2089 | ++string; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2090 | while (VIM_ISWHITE(*string)) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 2091 | ++string; |
| 2092 | len2 = (int)STRLEN(string); |
| 2093 | if (len2 == 0) |
| 2094 | continue; |
| 2095 | |
| 2096 | /* Now we have to verify whether string ends with a substring |
| 2097 | * beginning the com_leader. */ |
| 2098 | for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;) |
| 2099 | { |
| 2100 | --off; |
| 2101 | if (!STRNCMP(string + off, com_leader, len2 - off)) |
| 2102 | { |
| 2103 | if (i - off < lower_check_bound) |
| 2104 | lower_check_bound = i - off; |
| 2105 | } |
| 2106 | } |
| 2107 | } |
| 2108 | } |
| 2109 | } |
| 2110 | return result; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2111 | } |
| 2112 | #endif |
| 2113 | |
| 2114 | /* |
| 2115 | * Return the number of window lines occupied by buffer line "lnum". |
| 2116 | */ |
| 2117 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2118 | plines(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2119 | { |
| 2120 | return plines_win(curwin, lnum, TRUE); |
| 2121 | } |
| 2122 | |
| 2123 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2124 | plines_win( |
| 2125 | win_T *wp, |
| 2126 | linenr_T lnum, |
| 2127 | int winheight) /* when TRUE limit to window height */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2128 | { |
| 2129 | #if defined(FEAT_DIFF) || defined(PROTO) |
| 2130 | /* Check for filler lines above this buffer line. When folded the result |
| 2131 | * is one line anyway. */ |
| 2132 | return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum); |
| 2133 | } |
| 2134 | |
| 2135 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2136 | plines_nofill(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2137 | { |
| 2138 | return plines_win_nofill(curwin, lnum, TRUE); |
| 2139 | } |
| 2140 | |
| 2141 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2142 | plines_win_nofill( |
| 2143 | win_T *wp, |
| 2144 | linenr_T lnum, |
| 2145 | int winheight) /* when TRUE limit to window height */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2146 | { |
| 2147 | #endif |
| 2148 | int lines; |
| 2149 | |
| 2150 | if (!wp->w_p_wrap) |
| 2151 | return 1; |
| 2152 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2153 | if (wp->w_width == 0) |
| 2154 | return 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2155 | |
| 2156 | #ifdef FEAT_FOLDING |
| 2157 | /* A folded lines is handled just like an empty line. */ |
| 2158 | /* NOTE: Caller must handle lines that are MAYBE folded. */ |
| 2159 | if (lineFolded(wp, lnum) == TRUE) |
| 2160 | return 1; |
| 2161 | #endif |
| 2162 | |
| 2163 | lines = plines_win_nofold(wp, lnum); |
| 2164 | if (winheight > 0 && lines > wp->w_height) |
| 2165 | return (int)wp->w_height; |
| 2166 | return lines; |
| 2167 | } |
| 2168 | |
| 2169 | /* |
| 2170 | * Return number of window lines physical line "lnum" will occupy in window |
| 2171 | * "wp". Does not care about folding, 'wrap' or 'diff'. |
| 2172 | */ |
| 2173 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2174 | plines_win_nofold(win_T *wp, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2175 | { |
| 2176 | char_u *s; |
| 2177 | long col; |
| 2178 | int width; |
| 2179 | |
| 2180 | s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
| 2181 | if (*s == NUL) /* empty line */ |
| 2182 | return 1; |
| 2183 | col = win_linetabsize(wp, s, (colnr_T)MAXCOL); |
| 2184 | |
| 2185 | /* |
| 2186 | * If list mode is on, then the '$' at the end of the line may take up one |
| 2187 | * extra column. |
| 2188 | */ |
| 2189 | if (wp->w_p_list && lcs_eol != NUL) |
| 2190 | col += 1; |
| 2191 | |
| 2192 | /* |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 2193 | * Add column offset for 'number', 'relativenumber' and 'foldcolumn'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2194 | */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2195 | width = wp->w_width - win_col_off(wp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2196 | if (width <= 0) |
| 2197 | return 32000; |
| 2198 | if (col <= width) |
| 2199 | return 1; |
| 2200 | col -= width; |
| 2201 | width += win_col_off2(wp); |
| 2202 | return (col + (width - 1)) / width + 1; |
| 2203 | } |
| 2204 | |
| 2205 | /* |
| 2206 | * Like plines_win(), but only reports the number of physical screen lines |
| 2207 | * used from the start of the line to the given column number. |
| 2208 | */ |
| 2209 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2210 | plines_win_col(win_T *wp, linenr_T lnum, long column) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2211 | { |
| 2212 | long col; |
| 2213 | char_u *s; |
| 2214 | int lines = 0; |
| 2215 | int width; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2216 | char_u *line; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2217 | |
| 2218 | #ifdef FEAT_DIFF |
| 2219 | /* Check for filler lines above this buffer line. When folded the result |
| 2220 | * is one line anyway. */ |
| 2221 | lines = diff_check_fill(wp, lnum); |
| 2222 | #endif |
| 2223 | |
| 2224 | if (!wp->w_p_wrap) |
| 2225 | return lines + 1; |
| 2226 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2227 | if (wp->w_width == 0) |
| 2228 | return lines + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2229 | |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2230 | line = s = ml_get_buf(wp->w_buffer, lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2231 | |
| 2232 | col = 0; |
| 2233 | while (*s != NUL && --column >= 0) |
| 2234 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2235 | col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL); |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 2236 | MB_PTR_ADV(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | /* |
| 2240 | * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in |
| 2241 | * INSERT mode, then col must be adjusted so that it represents the last |
| 2242 | * screen position of the TAB. This only fixes an error when the TAB wraps |
| 2243 | * from one screen line to the next (when 'columns' is not a multiple of |
| 2244 | * 'ts') -- webb. |
| 2245 | */ |
| 2246 | if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1)) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2247 | col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2248 | |
| 2249 | /* |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 2250 | * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2251 | */ |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2252 | width = wp->w_width - win_col_off(wp); |
Bram Moolenaar | 2647063 | 2006-10-24 19:12:40 +0000 | [diff] [blame] | 2253 | if (width <= 0) |
| 2254 | return 9999; |
| 2255 | |
| 2256 | lines += 1; |
| 2257 | if (col > width) |
| 2258 | lines += (col - width) / (width + win_col_off2(wp)) + 1; |
| 2259 | return lines; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2263 | plines_m_win(win_T *wp, linenr_T first, linenr_T last) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2264 | { |
| 2265 | int count = 0; |
| 2266 | |
| 2267 | while (first <= last) |
| 2268 | { |
| 2269 | #ifdef FEAT_FOLDING |
| 2270 | int x; |
| 2271 | |
| 2272 | /* Check if there are any really folded lines, but also included lines |
| 2273 | * that are maybe folded. */ |
| 2274 | x = foldedCount(wp, first, NULL); |
| 2275 | if (x > 0) |
| 2276 | { |
| 2277 | ++count; /* count 1 for "+-- folded" line */ |
| 2278 | first += x; |
| 2279 | } |
| 2280 | else |
| 2281 | #endif |
| 2282 | { |
| 2283 | #ifdef FEAT_DIFF |
| 2284 | if (first == wp->w_topline) |
| 2285 | count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill; |
| 2286 | else |
| 2287 | #endif |
| 2288 | count += plines_win(wp, first, TRUE); |
| 2289 | ++first; |
| 2290 | } |
| 2291 | } |
| 2292 | return (count); |
| 2293 | } |
| 2294 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2295 | /* |
| 2296 | * Insert string "p" at the cursor position. Stops at a NUL byte. |
| 2297 | * Handles Replace mode and multi-byte characters. |
| 2298 | */ |
| 2299 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2300 | ins_bytes(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2301 | { |
| 2302 | ins_bytes_len(p, (int)STRLEN(p)); |
| 2303 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2304 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2305 | /* |
| 2306 | * Insert string "p" with length "len" at the cursor position. |
| 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_len(char_u *p, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2311 | { |
| 2312 | int i; |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 2313 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2314 | int n; |
| 2315 | |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2316 | if (has_mbyte) |
| 2317 | for (i = 0; i < len; i += n) |
| 2318 | { |
| 2319 | if (enc_utf8) |
| 2320 | /* avoid reading past p[len] */ |
| 2321 | n = utfc_ptr2len_len(p + i, len - i); |
| 2322 | else |
| 2323 | n = (*mb_ptr2len)(p + i); |
| 2324 | ins_char_bytes(p + i, n); |
| 2325 | } |
| 2326 | else |
Bram Moolenaar | 1f0bfe5 | 2018-07-29 16:09:22 +0200 | [diff] [blame] | 2327 | #endif |
Bram Moolenaar | 176dd1e | 2008-06-21 14:30:28 +0000 | [diff] [blame] | 2328 | for (i = 0; i < len; ++i) |
| 2329 | ins_char(p[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2330 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2331 | |
| 2332 | /* |
| 2333 | * Insert or replace a single character at the cursor position. |
| 2334 | * When in REPLACE or VREPLACE mode, replace any existing character. |
| 2335 | * Caller must have prepared for undo. |
| 2336 | * For multi-byte characters we get the whole character, the caller must |
| 2337 | * convert bytes to a character. |
| 2338 | */ |
| 2339 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2340 | ins_char(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2341 | { |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 2342 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | 560379d | 2017-01-21 22:50:00 +0100 | [diff] [blame] | 2343 | int n = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2344 | |
Bram Moolenaar | 6a8ede9 | 2017-01-22 19:49:12 +0100 | [diff] [blame] | 2345 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2346 | n = (*mb_char2bytes)(c, buf); |
| 2347 | |
| 2348 | /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte. |
| 2349 | * Happens for CTRL-Vu9900. */ |
| 2350 | if (buf[0] == 0) |
| 2351 | buf[0] = '\n'; |
Bram Moolenaar | 560379d | 2017-01-21 22:50:00 +0100 | [diff] [blame] | 2352 | #else |
| 2353 | buf[0] = c; |
| 2354 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2355 | |
| 2356 | ins_char_bytes(buf, n); |
| 2357 | } |
| 2358 | |
| 2359 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2360 | ins_char_bytes(char_u *buf, int charlen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2361 | { |
| 2362 | int c = buf[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2363 | int newlen; /* nr of bytes inserted */ |
| 2364 | int oldlen; /* nr of bytes deleted (0 when not replacing) */ |
| 2365 | char_u *p; |
| 2366 | char_u *newp; |
| 2367 | char_u *oldp; |
| 2368 | int linelen; /* length of old line including NUL */ |
| 2369 | colnr_T col; |
| 2370 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2371 | int i; |
| 2372 | |
| 2373 | #ifdef FEAT_VIRTUALEDIT |
| 2374 | /* Break tabs if needed. */ |
| 2375 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 2376 | coladvance_force(getviscol()); |
| 2377 | #endif |
| 2378 | |
| 2379 | col = curwin->w_cursor.col; |
| 2380 | oldp = ml_get(lnum); |
| 2381 | linelen = (int)STRLEN(oldp) + 1; |
| 2382 | |
| 2383 | /* The lengths default to the values for when not replacing. */ |
| 2384 | oldlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2385 | newlen = charlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2386 | |
| 2387 | if (State & REPLACE_FLAG) |
| 2388 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2389 | if (State & VREPLACE_FLAG) |
| 2390 | { |
| 2391 | colnr_T new_vcol = 0; /* init for GCC */ |
| 2392 | colnr_T vcol; |
| 2393 | int old_list; |
| 2394 | #ifndef FEAT_MBYTE |
| 2395 | char_u buf[2]; |
| 2396 | #endif |
| 2397 | |
| 2398 | /* |
| 2399 | * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag. |
| 2400 | * Returns the old value of list, so when finished, |
| 2401 | * curwin->w_p_list should be set back to this. |
| 2402 | */ |
| 2403 | old_list = curwin->w_p_list; |
| 2404 | if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 2405 | curwin->w_p_list = FALSE; |
| 2406 | |
| 2407 | /* |
| 2408 | * In virtual replace mode each character may replace one or more |
| 2409 | * characters (zero if it's a TAB). Count the number of bytes to |
| 2410 | * be deleted to make room for the new character, counting screen |
| 2411 | * cells. May result in adding spaces to fill a gap. |
| 2412 | */ |
| 2413 | getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL); |
| 2414 | #ifndef FEAT_MBYTE |
| 2415 | buf[0] = c; |
| 2416 | buf[1] = NUL; |
| 2417 | #endif |
| 2418 | new_vcol = vcol + chartabsize(buf, vcol); |
| 2419 | while (oldp[col + oldlen] != NUL && vcol < new_vcol) |
| 2420 | { |
| 2421 | vcol += chartabsize(oldp + col + oldlen, vcol); |
| 2422 | /* Don't need to remove a TAB that takes us to the right |
| 2423 | * position. */ |
| 2424 | if (vcol > new_vcol && oldp[col + oldlen] == TAB) |
| 2425 | break; |
| 2426 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2427 | oldlen += (*mb_ptr2len)(oldp + col + oldlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2428 | #else |
| 2429 | ++oldlen; |
| 2430 | #endif |
| 2431 | /* Deleted a bit too much, insert spaces. */ |
| 2432 | if (vcol > new_vcol) |
| 2433 | newlen += vcol - new_vcol; |
| 2434 | } |
| 2435 | curwin->w_p_list = old_list; |
| 2436 | } |
| 2437 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2438 | if (oldp[col] != NUL) |
| 2439 | { |
| 2440 | /* normal replace */ |
| 2441 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2442 | oldlen = (*mb_ptr2len)(oldp + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2443 | #else |
| 2444 | oldlen = 1; |
| 2445 | #endif |
| 2446 | } |
| 2447 | |
| 2448 | |
| 2449 | /* Push the replaced bytes onto the replace stack, so that they can be |
| 2450 | * put back when BS is used. The bytes of a multi-byte character are |
| 2451 | * done the other way around, so that the first byte is popped off |
| 2452 | * first (it tells the byte length of the character). */ |
| 2453 | replace_push(NUL); |
| 2454 | for (i = 0; i < oldlen; ++i) |
| 2455 | { |
| 2456 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 2457 | if (has_mbyte) |
| 2458 | i += replace_push_mb(oldp + col + i) - 1; |
| 2459 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2460 | #endif |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 2461 | replace_push(oldp[col + i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2462 | } |
| 2463 | } |
| 2464 | |
| 2465 | newp = alloc_check((unsigned)(linelen + newlen - oldlen)); |
| 2466 | if (newp == NULL) |
| 2467 | return; |
| 2468 | |
| 2469 | /* Copy bytes before the cursor. */ |
| 2470 | if (col > 0) |
| 2471 | mch_memmove(newp, oldp, (size_t)col); |
| 2472 | |
| 2473 | /* Copy bytes after the changed character(s). */ |
| 2474 | p = newp + col; |
Bram Moolenaar | 9ad89c6 | 2017-10-26 22:04:04 +0200 | [diff] [blame] | 2475 | if (linelen > col + oldlen) |
| 2476 | mch_memmove(p + newlen, oldp + col + oldlen, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2477 | (size_t)(linelen - col - oldlen)); |
| 2478 | |
| 2479 | /* Insert or overwrite the new character. */ |
| 2480 | #ifdef FEAT_MBYTE |
| 2481 | mch_memmove(p, buf, charlen); |
| 2482 | i = charlen; |
| 2483 | #else |
| 2484 | *p = c; |
| 2485 | i = 1; |
| 2486 | #endif |
| 2487 | |
| 2488 | /* Fill with spaces when necessary. */ |
| 2489 | while (i < newlen) |
| 2490 | p[i++] = ' '; |
| 2491 | |
| 2492 | /* Replace the line in the buffer. */ |
| 2493 | ml_replace(lnum, newp, FALSE); |
| 2494 | |
| 2495 | /* mark the buffer as changed and prepare for displaying */ |
| 2496 | changed_bytes(lnum, col); |
| 2497 | |
| 2498 | /* |
| 2499 | * If we're in Insert or Replace mode and 'showmatch' is set, then briefly |
| 2500 | * show the match for right parens and braces. |
| 2501 | */ |
| 2502 | if (p_sm && (State & INSERT) |
| 2503 | && msg_silent == 0 |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 2504 | #ifdef FEAT_INS_EXPAND |
| 2505 | && !ins_compl_active() |
| 2506 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2507 | ) |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2508 | { |
| 2509 | #ifdef FEAT_MBYTE |
| 2510 | if (has_mbyte) |
| 2511 | showmatch(mb_ptr2char(buf)); |
| 2512 | else |
| 2513 | #endif |
| 2514 | showmatch(c); |
| 2515 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2516 | |
| 2517 | #ifdef FEAT_RIGHTLEFT |
| 2518 | if (!p_ri || (State & REPLACE_FLAG)) |
| 2519 | #endif |
| 2520 | { |
| 2521 | /* Normal insert: move cursor right */ |
| 2522 | #ifdef FEAT_MBYTE |
| 2523 | curwin->w_cursor.col += charlen; |
| 2524 | #else |
| 2525 | ++curwin->w_cursor.col; |
| 2526 | #endif |
| 2527 | } |
| 2528 | /* |
| 2529 | * TODO: should try to update w_row here, to avoid recomputing it later. |
| 2530 | */ |
| 2531 | } |
| 2532 | |
| 2533 | /* |
| 2534 | * Insert a string at the cursor position. |
| 2535 | * Note: Does NOT handle Replace mode. |
| 2536 | * Caller must have prepared for undo. |
| 2537 | */ |
| 2538 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2539 | ins_str(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2540 | { |
| 2541 | char_u *oldp, *newp; |
| 2542 | int newlen = (int)STRLEN(s); |
| 2543 | int oldlen; |
| 2544 | colnr_T col; |
| 2545 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2546 | |
| 2547 | #ifdef FEAT_VIRTUALEDIT |
| 2548 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 2549 | coladvance_force(getviscol()); |
| 2550 | #endif |
| 2551 | |
| 2552 | col = curwin->w_cursor.col; |
| 2553 | oldp = ml_get(lnum); |
| 2554 | oldlen = (int)STRLEN(oldp); |
| 2555 | |
| 2556 | newp = alloc_check((unsigned)(oldlen + newlen + 1)); |
| 2557 | if (newp == NULL) |
| 2558 | return; |
| 2559 | if (col > 0) |
| 2560 | mch_memmove(newp, oldp, (size_t)col); |
| 2561 | mch_memmove(newp + col, s, (size_t)newlen); |
| 2562 | mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1)); |
| 2563 | ml_replace(lnum, newp, FALSE); |
| 2564 | changed_bytes(lnum, col); |
| 2565 | curwin->w_cursor.col += newlen; |
| 2566 | } |
| 2567 | |
| 2568 | /* |
| 2569 | * Delete one character under the cursor. |
| 2570 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 2571 | * Caller must have prepared for undo. |
| 2572 | * |
| 2573 | * return FAIL for failure, OK otherwise |
| 2574 | */ |
| 2575 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2576 | del_char(int fixpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2577 | { |
| 2578 | #ifdef FEAT_MBYTE |
| 2579 | if (has_mbyte) |
| 2580 | { |
| 2581 | /* Make sure the cursor is at the start of a character. */ |
| 2582 | mb_adjust_cursor(); |
| 2583 | if (*ml_get_cursor() == NUL) |
| 2584 | return FAIL; |
| 2585 | return del_chars(1L, fixpos); |
| 2586 | } |
| 2587 | #endif |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2588 | return del_bytes(1L, fixpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 2592 | /* |
| 2593 | * Like del_bytes(), but delete characters instead of bytes. |
| 2594 | */ |
| 2595 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2596 | del_chars(long count, int fixpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2597 | { |
| 2598 | long bytes = 0; |
| 2599 | long i; |
| 2600 | char_u *p; |
| 2601 | int l; |
| 2602 | |
| 2603 | p = ml_get_cursor(); |
| 2604 | for (i = 0; i < count && *p != NUL; ++i) |
| 2605 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2606 | l = (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2607 | bytes += l; |
| 2608 | p += l; |
| 2609 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2610 | return del_bytes(bytes, fixpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2611 | } |
| 2612 | #endif |
| 2613 | |
| 2614 | /* |
| 2615 | * Delete "count" bytes under the cursor. |
| 2616 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 2617 | * Caller must have prepared for undo. |
| 2618 | * |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2619 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2620 | */ |
| 2621 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2622 | del_bytes( |
| 2623 | long count, |
| 2624 | int fixpos_arg, |
| 2625 | int use_delcombine UNUSED) /* 'delcombine' option applies */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2626 | { |
| 2627 | char_u *oldp, *newp; |
| 2628 | colnr_T oldlen; |
| 2629 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2630 | colnr_T col = curwin->w_cursor.col; |
| 2631 | int was_alloced; |
| 2632 | long movelen; |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2633 | int fixpos = fixpos_arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2634 | |
| 2635 | oldp = ml_get(lnum); |
| 2636 | oldlen = (int)STRLEN(oldp); |
| 2637 | |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2638 | /* 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] | 2639 | if (col >= oldlen) |
| 2640 | return FAIL; |
| 2641 | |
Bram Moolenaar | 191f18b | 2018-02-04 16:38:47 +0100 | [diff] [blame] | 2642 | /* If "count" is zero there is nothing to do. */ |
| 2643 | if (count == 0) |
| 2644 | return OK; |
| 2645 | |
| 2646 | /* If "count" is negative the caller must be doing something wrong. */ |
| 2647 | if (count < 1) |
| 2648 | { |
| 2649 | IEMSGN("E950: Invalid count for del_bytes(): %ld", count); |
| 2650 | return FAIL; |
| 2651 | } |
| 2652 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2653 | #ifdef FEAT_MBYTE |
| 2654 | /* If 'delcombine' is set and deleting (less than) one character, only |
| 2655 | * delete the last combining character. */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2656 | if (p_deco && use_delcombine && enc_utf8 |
| 2657 | && utfc_ptr2len(oldp + col) >= count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2658 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2659 | int cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2660 | int n; |
| 2661 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2662 | (void)utfc_ptr2char(oldp + col, cc); |
| 2663 | if (cc[0] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2664 | { |
| 2665 | /* Find the last composing char, there can be several. */ |
| 2666 | n = col; |
| 2667 | do |
| 2668 | { |
| 2669 | col = n; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2670 | count = utf_ptr2len(oldp + n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2671 | n += count; |
| 2672 | } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n)); |
| 2673 | fixpos = 0; |
| 2674 | } |
| 2675 | } |
| 2676 | #endif |
| 2677 | |
| 2678 | /* |
| 2679 | * When count is too big, reduce it. |
| 2680 | */ |
| 2681 | movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */ |
| 2682 | if (movelen <= 1) |
| 2683 | { |
| 2684 | /* |
| 2685 | * 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] | 2686 | * fixpos is TRUE, we don't want to end up positioned at the NUL, |
| 2687 | * unless "restart_edit" is set or 'virtualedit' contains "onemore". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2688 | */ |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 2689 | if (col > 0 && fixpos && restart_edit == 0 |
| 2690 | #ifdef FEAT_VIRTUALEDIT |
| 2691 | && (ve_flags & VE_ONEMORE) == 0 |
| 2692 | #endif |
| 2693 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2694 | { |
| 2695 | --curwin->w_cursor.col; |
| 2696 | #ifdef FEAT_VIRTUALEDIT |
| 2697 | curwin->w_cursor.coladd = 0; |
| 2698 | #endif |
| 2699 | #ifdef FEAT_MBYTE |
| 2700 | if (has_mbyte) |
| 2701 | curwin->w_cursor.col -= |
| 2702 | (*mb_head_off)(oldp, oldp + curwin->w_cursor.col); |
| 2703 | #endif |
| 2704 | } |
| 2705 | count = oldlen - col; |
| 2706 | movelen = 1; |
| 2707 | } |
| 2708 | |
| 2709 | /* |
| 2710 | * If the old line has been allocated the deletion can be done in the |
| 2711 | * existing line. Otherwise a new line has to be allocated |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2712 | * Can't do this when using Netbeans, because we would need to invoke |
| 2713 | * netbeans_removed(), which deallocates the line. Let ml_replace() take |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 2714 | * care of notifying Netbeans. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2715 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2716 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 2717 | if (netbeans_active()) |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2718 | was_alloced = FALSE; |
| 2719 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2720 | #endif |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 2721 | was_alloced = ml_line_alloced(); /* check if oldp was allocated */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2722 | if (was_alloced) |
| 2723 | newp = oldp; /* use same allocated memory */ |
| 2724 | else |
| 2725 | { /* need to allocate a new line */ |
| 2726 | newp = alloc((unsigned)(oldlen + 1 - count)); |
| 2727 | if (newp == NULL) |
| 2728 | return FAIL; |
| 2729 | mch_memmove(newp, oldp, (size_t)col); |
| 2730 | } |
| 2731 | mch_memmove(newp + col, oldp + col + count, (size_t)movelen); |
| 2732 | if (!was_alloced) |
| 2733 | ml_replace(lnum, newp, FALSE); |
| 2734 | |
| 2735 | /* mark the buffer as changed and prepare for displaying */ |
| 2736 | changed_bytes(lnum, curwin->w_cursor.col); |
| 2737 | |
| 2738 | return OK; |
| 2739 | } |
| 2740 | |
| 2741 | /* |
| 2742 | * Delete from cursor to end of line. |
| 2743 | * Caller must have prepared for undo. |
| 2744 | * |
| 2745 | * return FAIL for failure, OK otherwise |
| 2746 | */ |
| 2747 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2748 | truncate_line( |
| 2749 | int fixpos) /* if TRUE fix the cursor position when done */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2750 | { |
| 2751 | char_u *newp; |
| 2752 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2753 | colnr_T col = curwin->w_cursor.col; |
| 2754 | |
| 2755 | if (col == 0) |
| 2756 | newp = vim_strsave((char_u *)""); |
| 2757 | else |
| 2758 | newp = vim_strnsave(ml_get(lnum), col); |
| 2759 | |
| 2760 | if (newp == NULL) |
| 2761 | return FAIL; |
| 2762 | |
| 2763 | ml_replace(lnum, newp, FALSE); |
| 2764 | |
| 2765 | /* mark the buffer as changed and prepare for displaying */ |
| 2766 | changed_bytes(lnum, curwin->w_cursor.col); |
| 2767 | |
| 2768 | /* |
| 2769 | * If "fixpos" is TRUE we don't want to end up positioned at the NUL. |
| 2770 | */ |
| 2771 | if (fixpos && curwin->w_cursor.col > 0) |
| 2772 | --curwin->w_cursor.col; |
| 2773 | |
| 2774 | return OK; |
| 2775 | } |
| 2776 | |
| 2777 | /* |
| 2778 | * Delete "nlines" lines at the cursor. |
| 2779 | * Saves the lines for undo first if "undo" is TRUE. |
| 2780 | */ |
| 2781 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2782 | del_lines( |
| 2783 | long nlines, /* number of lines to delete */ |
| 2784 | int undo) /* if TRUE, prepare for undo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2785 | { |
| 2786 | long n; |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2787 | linenr_T first = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2788 | |
| 2789 | if (nlines <= 0) |
| 2790 | return; |
| 2791 | |
| 2792 | /* save the deleted lines for undo */ |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2793 | if (undo && u_savedel(first, nlines) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2794 | return; |
| 2795 | |
| 2796 | for (n = 0; n < nlines; ) |
| 2797 | { |
| 2798 | if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ |
| 2799 | break; |
| 2800 | |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2801 | ml_delete(first, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2802 | ++n; |
| 2803 | |
| 2804 | /* If we delete the last line in the file, stop */ |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2805 | if (first > curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2806 | break; |
| 2807 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2808 | |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2809 | /* Correct the cursor position before calling deleted_lines_mark(), it may |
| 2810 | * trigger a callback to display the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2811 | curwin->w_cursor.col = 0; |
| 2812 | check_cursor_lnum(); |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 2813 | |
| 2814 | /* adjust marks, mark the buffer as changed and prepare for displaying */ |
| 2815 | deleted_lines_mark(first, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2819 | gchar_pos(pos_T *pos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2820 | { |
Bram Moolenaar | 8ada6aa | 2017-12-19 21:23:21 +0100 | [diff] [blame] | 2821 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2822 | |
Bram Moolenaar | 8ada6aa | 2017-12-19 21:23:21 +0100 | [diff] [blame] | 2823 | /* When searching columns is sometimes put at the end of a line. */ |
| 2824 | if (pos->col == MAXCOL) |
| 2825 | return NUL; |
| 2826 | ptr = ml_get_pos(pos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2827 | #ifdef FEAT_MBYTE |
| 2828 | if (has_mbyte) |
| 2829 | return (*mb_ptr2char)(ptr); |
| 2830 | #endif |
| 2831 | return (int)*ptr; |
| 2832 | } |
| 2833 | |
| 2834 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2835 | gchar_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2836 | { |
| 2837 | #ifdef FEAT_MBYTE |
| 2838 | if (has_mbyte) |
| 2839 | return (*mb_ptr2char)(ml_get_cursor()); |
| 2840 | #endif |
| 2841 | return (int)*ml_get_cursor(); |
| 2842 | } |
| 2843 | |
| 2844 | /* |
| 2845 | * Write a character at the current cursor position. |
| 2846 | * It is directly written into the block. |
| 2847 | */ |
| 2848 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2849 | pchar_cursor(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2850 | { |
| 2851 | *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) |
| 2852 | + curwin->w_cursor.col) = c; |
| 2853 | } |
| 2854 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2855 | /* |
| 2856 | * When extra == 0: Return TRUE if the cursor is before or on the first |
| 2857 | * non-blank in the line. |
| 2858 | * When extra == 1: Return TRUE if the cursor is before the first non-blank in |
| 2859 | * the line. |
| 2860 | */ |
| 2861 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2862 | inindent(int extra) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2863 | { |
| 2864 | char_u *ptr; |
| 2865 | colnr_T col; |
| 2866 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2867 | for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2868 | ++ptr; |
| 2869 | if (col >= curwin->w_cursor.col + extra) |
| 2870 | return TRUE; |
| 2871 | else |
| 2872 | return FALSE; |
| 2873 | } |
| 2874 | |
| 2875 | /* |
| 2876 | * Skip to next part of an option argument: Skip space and comma. |
| 2877 | */ |
| 2878 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2879 | skip_to_option_part(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2880 | { |
| 2881 | if (*p == ',') |
| 2882 | ++p; |
| 2883 | while (*p == ' ') |
| 2884 | ++p; |
| 2885 | return p; |
| 2886 | } |
| 2887 | |
| 2888 | /* |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2889 | * Call this function when something in the current buffer is changed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2890 | * |
| 2891 | * Most often called through changed_bytes() and changed_lines(), which also |
| 2892 | * mark the area of the display to be redrawn. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2893 | * |
| 2894 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2895 | */ |
| 2896 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2897 | changed(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2898 | { |
| 2899 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 2900 | if (p_imst == IM_ON_THE_SPOT) |
| 2901 | { |
| 2902 | /* The text of the preediting area is inserted, but this doesn't |
| 2903 | * mean a change of the buffer yet. That is delayed until the |
| 2904 | * text is committed. (this means preedit becomes empty) */ |
| 2905 | if (im_is_preediting() && !xim_changed_while_preediting) |
| 2906 | return; |
| 2907 | xim_changed_while_preediting = FALSE; |
| 2908 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2909 | #endif |
| 2910 | |
| 2911 | if (!curbuf->b_changed) |
| 2912 | { |
| 2913 | int save_msg_scroll = msg_scroll; |
| 2914 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2915 | /* Give a warning about changing a read-only file. This may also |
| 2916 | * check-out the file, thus change "curbuf"! */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2917 | change_warning(0); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2918 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2919 | /* Create a swap file if that is wanted. |
| 2920 | * Don't do this for "nofile" and "nowrite" buffer types. */ |
| 2921 | if (curbuf->b_may_swap |
| 2922 | #ifdef FEAT_QUICKFIX |
| 2923 | && !bt_dontwrite(curbuf) |
| 2924 | #endif |
| 2925 | ) |
| 2926 | { |
Bram Moolenaar | b01f357 | 2016-01-15 15:17:04 +0100 | [diff] [blame] | 2927 | int save_need_wait_return = need_wait_return; |
| 2928 | |
| 2929 | need_wait_return = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2930 | ml_open_file(curbuf); |
| 2931 | |
| 2932 | /* The ml_open_file() can cause an ATTENTION message. |
| 2933 | * Wait two seconds, to make sure the user reads this unexpected |
| 2934 | * message. Since we could be anywhere, call wait_return() now, |
| 2935 | * and don't let the emsg() set msg_scroll. */ |
| 2936 | if (need_wait_return && emsg_silent == 0) |
| 2937 | { |
| 2938 | out_flush(); |
| 2939 | ui_delay(2000L, TRUE); |
| 2940 | wait_return(TRUE); |
| 2941 | msg_scroll = save_msg_scroll; |
| 2942 | } |
Bram Moolenaar | b01f357 | 2016-01-15 15:17:04 +0100 | [diff] [blame] | 2943 | else |
| 2944 | need_wait_return = save_need_wait_return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2945 | } |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2946 | changed_int(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2947 | } |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 2948 | ++CHANGEDTICK(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2951 | /* |
| 2952 | * Internal part of changed(), no user interaction. |
| 2953 | */ |
| 2954 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2955 | changed_int(void) |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2956 | { |
| 2957 | curbuf->b_changed = TRUE; |
| 2958 | ml_setflags(curbuf); |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2959 | check_status(curbuf); |
| 2960 | redraw_tabline = TRUE; |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 2961 | #ifdef FEAT_TITLE |
| 2962 | need_maketitle = TRUE; /* set window title later */ |
| 2963 | #endif |
| 2964 | } |
| 2965 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 2966 | static void changedOneline(buf_T *buf, linenr_T lnum); |
| 2967 | static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra); |
| 2968 | 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] | 2969 | |
| 2970 | /* |
| 2971 | * Changed bytes within a single line for the current buffer. |
| 2972 | * - marks the windows on this buffer to be redisplayed |
| 2973 | * - marks the buffer changed by calling changed() |
| 2974 | * - invalidates cached values |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 2975 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2976 | */ |
| 2977 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 2978 | changed_bytes(linenr_T lnum, colnr_T col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2979 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2980 | changedOneline(curbuf, lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2981 | changed_common(lnum, col, lnum + 1, 0L); |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2982 | |
| 2983 | #ifdef FEAT_DIFF |
| 2984 | /* Diff highlighting in other diff windows may need to be updated too. */ |
| 2985 | if (curwin->w_p_diff) |
| 2986 | { |
| 2987 | win_T *wp; |
| 2988 | linenr_T wlnum; |
| 2989 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 2990 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 2991 | if (wp->w_p_diff && wp != curwin) |
| 2992 | { |
| 2993 | redraw_win_later(wp, VALID); |
| 2994 | wlnum = diff_lnum_win(lnum, wp); |
| 2995 | if (wlnum > 0) |
| 2996 | changedOneline(wp->w_buffer, wlnum); |
| 2997 | } |
| 2998 | } |
| 2999 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3003 | changedOneline(buf_T *buf, linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3004 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3005 | if (buf->b_mod_set) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3006 | { |
| 3007 | /* find the maximum area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3008 | if (lnum < buf->b_mod_top) |
| 3009 | buf->b_mod_top = lnum; |
| 3010 | else if (lnum >= buf->b_mod_bot) |
| 3011 | buf->b_mod_bot = lnum + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3012 | } |
| 3013 | else |
| 3014 | { |
| 3015 | /* set the area that must be redisplayed to one line */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3016 | buf->b_mod_set = TRUE; |
| 3017 | buf->b_mod_top = lnum; |
| 3018 | buf->b_mod_bot = lnum + 1; |
| 3019 | buf->b_mod_xlines = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | /* |
| 3024 | * Appended "count" lines below line "lnum" in the current buffer. |
| 3025 | * Must be called AFTER the change and after mark_adjust(). |
| 3026 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 3027 | */ |
| 3028 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3029 | appended_lines(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3030 | { |
| 3031 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 3032 | } |
| 3033 | |
| 3034 | /* |
| 3035 | * Like appended_lines(), but adjust marks first. |
| 3036 | */ |
| 3037 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3038 | appended_lines_mark(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3039 | { |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 3040 | /* 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] | 3041 | * be marks there. But it's still needed in diff mode. */ |
| 3042 | if (lnum + count < curbuf->b_ml.ml_line_count |
| 3043 | #ifdef FEAT_DIFF |
| 3044 | || curwin->w_p_diff |
| 3045 | #endif |
| 3046 | ) |
Bram Moolenaar | 82faa25 | 2016-06-04 20:14:07 +0200 | [diff] [blame] | 3047 | mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3048 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 3049 | } |
| 3050 | |
| 3051 | /* |
| 3052 | * Deleted "count" lines at line "lnum" in the current buffer. |
| 3053 | * Must be called AFTER the change and after mark_adjust(). |
| 3054 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 3055 | */ |
| 3056 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3057 | deleted_lines(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3058 | { |
| 3059 | changed_lines(lnum, 0, lnum + count, -count); |
| 3060 | } |
| 3061 | |
| 3062 | /* |
| 3063 | * Like deleted_lines(), but adjust marks first. |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 3064 | * Make sure the cursor is on a valid line before calling, a GUI callback may |
| 3065 | * be triggered to display the cursor. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3066 | */ |
| 3067 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3068 | deleted_lines_mark(linenr_T lnum, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3069 | { |
| 3070 | mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count); |
| 3071 | changed_lines(lnum, 0, lnum + count, -count); |
| 3072 | } |
| 3073 | |
| 3074 | /* |
| 3075 | * Changed lines for the current buffer. |
| 3076 | * Must be called AFTER the change and after mark_adjust(). |
| 3077 | * - mark the buffer changed by calling changed() |
| 3078 | * - mark the windows on this buffer to be redisplayed |
| 3079 | * - invalidate cached values |
| 3080 | * "lnum" is the first line that needs displaying, "lnume" the first line |
| 3081 | * below the changed lines (BEFORE the change). |
| 3082 | * When only inserting lines, "lnum" and "lnume" are equal. |
| 3083 | * Takes care of calling changed() and updating b_mod_*. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3084 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3085 | */ |
| 3086 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3087 | changed_lines( |
| 3088 | linenr_T lnum, /* first line with change */ |
| 3089 | colnr_T col, /* column in first line with change */ |
| 3090 | linenr_T lnume, /* line below last changed line */ |
| 3091 | long xtra) /* number of extra lines (negative when deleting) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3092 | { |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3093 | changed_lines_buf(curbuf, lnum, lnume, xtra); |
| 3094 | |
| 3095 | #ifdef FEAT_DIFF |
| 3096 | if (xtra == 0 && curwin->w_p_diff) |
| 3097 | { |
| 3098 | /* When the number of lines doesn't change then mark_adjust() isn't |
| 3099 | * called and other diff buffers still need to be marked for |
| 3100 | * displaying. */ |
| 3101 | win_T *wp; |
| 3102 | linenr_T wlnum; |
| 3103 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 3104 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3105 | if (wp->w_p_diff && wp != curwin) |
| 3106 | { |
| 3107 | redraw_win_later(wp, VALID); |
| 3108 | wlnum = diff_lnum_win(lnum, wp); |
| 3109 | if (wlnum > 0) |
| 3110 | changed_lines_buf(wp->w_buffer, wlnum, |
| 3111 | lnume - lnum + wlnum, 0L); |
| 3112 | } |
| 3113 | } |
| 3114 | #endif |
| 3115 | |
| 3116 | changed_common(lnum, col, lnume, xtra); |
| 3117 | } |
| 3118 | |
| 3119 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3120 | changed_lines_buf( |
| 3121 | buf_T *buf, |
| 3122 | linenr_T lnum, /* first line with change */ |
| 3123 | linenr_T lnume, /* line below last changed line */ |
| 3124 | long xtra) /* number of extra lines (negative when deleting) */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3125 | { |
| 3126 | if (buf->b_mod_set) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3127 | { |
| 3128 | /* find the maximum area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3129 | if (lnum < buf->b_mod_top) |
| 3130 | buf->b_mod_top = lnum; |
| 3131 | if (lnum < buf->b_mod_bot) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3132 | { |
| 3133 | /* adjust old bot position for xtra lines */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3134 | buf->b_mod_bot += xtra; |
| 3135 | if (buf->b_mod_bot < lnum) |
| 3136 | buf->b_mod_bot = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3137 | } |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3138 | if (lnume + xtra > buf->b_mod_bot) |
| 3139 | buf->b_mod_bot = lnume + xtra; |
| 3140 | buf->b_mod_xlines += xtra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3141 | } |
| 3142 | else |
| 3143 | { |
| 3144 | /* set the area that must be redisplayed */ |
Bram Moolenaar | dba8a91 | 2005-04-24 22:08:39 +0000 | [diff] [blame] | 3145 | buf->b_mod_set = TRUE; |
| 3146 | buf->b_mod_top = lnum; |
| 3147 | buf->b_mod_bot = lnume + xtra; |
| 3148 | buf->b_mod_xlines = xtra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3149 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3152 | /* |
| 3153 | * Common code for when a change is was made. |
| 3154 | * See changed_lines() for the arguments. |
| 3155 | * Careful: may trigger autocommands that reload the buffer. |
| 3156 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3157 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3158 | changed_common( |
| 3159 | linenr_T lnum, |
| 3160 | colnr_T col, |
| 3161 | linenr_T lnume, |
| 3162 | long xtra) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3163 | { |
| 3164 | win_T *wp; |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3165 | tabpage_T *tp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3166 | int i; |
| 3167 | #ifdef FEAT_JUMPLIST |
| 3168 | int cols; |
| 3169 | pos_T *p; |
| 3170 | int add; |
| 3171 | #endif |
| 3172 | |
| 3173 | /* mark the buffer as modified */ |
| 3174 | changed(); |
| 3175 | |
| 3176 | /* set the '. mark */ |
| 3177 | if (!cmdmod.keepjumps) |
| 3178 | { |
| 3179 | curbuf->b_last_change.lnum = lnum; |
| 3180 | curbuf->b_last_change.col = col; |
| 3181 | |
| 3182 | #ifdef FEAT_JUMPLIST |
| 3183 | /* Create a new entry if a new undo-able change was started or we |
| 3184 | * don't have an entry yet. */ |
| 3185 | if (curbuf->b_new_change || curbuf->b_changelistlen == 0) |
| 3186 | { |
| 3187 | if (curbuf->b_changelistlen == 0) |
| 3188 | add = TRUE; |
| 3189 | else |
| 3190 | { |
| 3191 | /* Don't create a new entry when the line number is the same |
| 3192 | * as the last one and the column is not too far away. Avoids |
| 3193 | * creating many entries for typing "xxxxx". */ |
| 3194 | p = &curbuf->b_changelist[curbuf->b_changelistlen - 1]; |
| 3195 | if (p->lnum != lnum) |
| 3196 | add = TRUE; |
| 3197 | else |
| 3198 | { |
| 3199 | cols = comp_textwidth(FALSE); |
| 3200 | if (cols == 0) |
| 3201 | cols = 79; |
| 3202 | add = (p->col + cols < col || col + cols < p->col); |
| 3203 | } |
| 3204 | } |
| 3205 | if (add) |
| 3206 | { |
| 3207 | /* This is the first of a new sequence of undo-able changes |
| 3208 | * and it's at some distance of the last change. Use a new |
| 3209 | * position in the changelist. */ |
| 3210 | curbuf->b_new_change = FALSE; |
| 3211 | |
| 3212 | if (curbuf->b_changelistlen == JUMPLISTSIZE) |
| 3213 | { |
| 3214 | /* changelist is full: remove oldest entry */ |
| 3215 | curbuf->b_changelistlen = JUMPLISTSIZE - 1; |
| 3216 | mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1, |
| 3217 | sizeof(pos_T) * (JUMPLISTSIZE - 1)); |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3218 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3219 | { |
| 3220 | /* Correct position in changelist for other windows on |
| 3221 | * this buffer. */ |
| 3222 | if (wp->w_buffer == curbuf && wp->w_changelistidx > 0) |
| 3223 | --wp->w_changelistidx; |
| 3224 | } |
| 3225 | } |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3226 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3227 | { |
| 3228 | /* For other windows, if the position in the changelist is |
| 3229 | * at the end it stays at the end. */ |
| 3230 | if (wp->w_buffer == curbuf |
| 3231 | && wp->w_changelistidx == curbuf->b_changelistlen) |
| 3232 | ++wp->w_changelistidx; |
| 3233 | } |
| 3234 | ++curbuf->b_changelistlen; |
| 3235 | } |
| 3236 | } |
| 3237 | curbuf->b_changelist[curbuf->b_changelistlen - 1] = |
| 3238 | curbuf->b_last_change; |
| 3239 | /* The current window is always after the last change, so that "g," |
| 3240 | * takes you back to it. */ |
| 3241 | curwin->w_changelistidx = curbuf->b_changelistlen; |
| 3242 | #endif |
| 3243 | } |
| 3244 | |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 3245 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3246 | { |
| 3247 | if (wp->w_buffer == curbuf) |
| 3248 | { |
| 3249 | /* Mark this window to be redrawn later. */ |
| 3250 | if (wp->w_redr_type < VALID) |
| 3251 | wp->w_redr_type = VALID; |
| 3252 | |
| 3253 | /* Check if a change in the buffer has invalidated the cached |
| 3254 | * values for the cursor. */ |
| 3255 | #ifdef FEAT_FOLDING |
| 3256 | /* |
| 3257 | * Update the folds for this window. Can't postpone this, because |
| 3258 | * a following operator might work on the whole fold: ">>dd". |
| 3259 | */ |
| 3260 | foldUpdate(wp, lnum, lnume + xtra - 1); |
| 3261 | |
| 3262 | /* The change may cause lines above or below the change to become |
| 3263 | * included in a fold. Set lnum/lnume to the first/last line that |
| 3264 | * might be displayed differently. |
| 3265 | * Set w_cline_folded here as an efficient way to update it when |
| 3266 | * inserting lines just above a closed fold. */ |
| 3267 | i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL); |
| 3268 | if (wp->w_cursor.lnum == lnum) |
| 3269 | wp->w_cline_folded = i; |
| 3270 | i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL); |
| 3271 | if (wp->w_cursor.lnum == lnume) |
| 3272 | wp->w_cline_folded = i; |
| 3273 | |
| 3274 | /* If the changed line is in a range of previously folded lines, |
| 3275 | * compare with the first line in that range. */ |
| 3276 | if (wp->w_cursor.lnum <= lnum) |
| 3277 | { |
| 3278 | i = find_wl_entry(wp, lnum); |
| 3279 | if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum) |
| 3280 | changed_line_abv_curs_win(wp); |
| 3281 | } |
| 3282 | #endif |
| 3283 | |
| 3284 | if (wp->w_cursor.lnum > lnum) |
| 3285 | changed_line_abv_curs_win(wp); |
| 3286 | else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col) |
| 3287 | changed_cline_bef_curs_win(wp); |
| 3288 | if (wp->w_botline >= lnum) |
| 3289 | { |
| 3290 | /* Assume that botline doesn't change (inserted lines make |
| 3291 | * other lines scroll down below botline). */ |
| 3292 | approximate_botline_win(wp); |
| 3293 | } |
| 3294 | |
| 3295 | /* Check if any w_lines[] entries have become invalid. |
| 3296 | * For entries below the change: Correct the lnums for |
| 3297 | * inserted/deleted lines. Makes it possible to stop displaying |
| 3298 | * after the change. */ |
| 3299 | for (i = 0; i < wp->w_lines_valid; ++i) |
| 3300 | if (wp->w_lines[i].wl_valid) |
| 3301 | { |
| 3302 | if (wp->w_lines[i].wl_lnum >= lnum) |
| 3303 | { |
| 3304 | if (wp->w_lines[i].wl_lnum < lnume) |
| 3305 | { |
| 3306 | /* line included in change */ |
| 3307 | wp->w_lines[i].wl_valid = FALSE; |
| 3308 | } |
| 3309 | else if (xtra != 0) |
| 3310 | { |
| 3311 | /* line below change */ |
| 3312 | wp->w_lines[i].wl_lnum += xtra; |
| 3313 | #ifdef FEAT_FOLDING |
| 3314 | wp->w_lines[i].wl_lastlnum += xtra; |
| 3315 | #endif |
| 3316 | } |
| 3317 | } |
| 3318 | #ifdef FEAT_FOLDING |
| 3319 | else if (wp->w_lines[i].wl_lastlnum >= lnum) |
| 3320 | { |
| 3321 | /* change somewhere inside this range of folded lines, |
| 3322 | * may need to be redrawn */ |
| 3323 | wp->w_lines[i].wl_valid = FALSE; |
| 3324 | } |
| 3325 | #endif |
| 3326 | } |
Bram Moolenaar | 3234cc6 | 2009-11-03 17:47:12 +0000 | [diff] [blame] | 3327 | |
| 3328 | #ifdef FEAT_FOLDING |
| 3329 | /* Take care of side effects for setting w_topline when folds have |
| 3330 | * changed. Esp. when the buffer was changed in another window. */ |
| 3331 | if (hasAnyFolding(wp)) |
| 3332 | set_topline(wp, wp->w_topline); |
| 3333 | #endif |
Bram Moolenaar | fd859c9 | 2014-05-13 12:44:24 +0200 | [diff] [blame] | 3334 | /* relative numbering may require updating more */ |
| 3335 | if (wp->w_p_rnu) |
| 3336 | redraw_win_later(wp, SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3337 | } |
| 3338 | } |
| 3339 | |
| 3340 | /* Call update_screen() later, which checks out what needs to be redrawn, |
| 3341 | * since it notices b_mod_set and then uses b_mod_*. */ |
| 3342 | if (must_redraw < VALID) |
| 3343 | must_redraw = VALID; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3344 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3345 | /* when the cursor line is changed always trigger CursorMoved */ |
Bram Moolenaar | e163f1c | 2006-10-17 09:12:21 +0000 | [diff] [blame] | 3346 | if (lnum <= curwin->w_cursor.lnum |
| 3347 | && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum) |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3348 | last_cursormoved.lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
| 3351 | /* |
| 3352 | * unchanged() is called when the changed flag must be reset for buffer 'buf' |
| 3353 | */ |
| 3354 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3355 | unchanged( |
| 3356 | buf_T *buf, |
| 3357 | int ff) /* also reset 'fileformat' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3358 | { |
Bram Moolenaar | 164c60f | 2011-01-22 00:11:50 +0100 | [diff] [blame] | 3359 | if (buf->b_changed || (ff && file_ff_differs(buf, FALSE))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3360 | { |
| 3361 | buf->b_changed = 0; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3362 | ml_setflags(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3363 | if (ff) |
| 3364 | save_file_ff(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3365 | check_status(buf); |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 3366 | redraw_tabline = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3367 | #ifdef FEAT_TITLE |
| 3368 | need_maketitle = TRUE; /* set window title later */ |
| 3369 | #endif |
| 3370 | } |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 3371 | ++CHANGEDTICK(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3372 | #ifdef FEAT_NETBEANS_INTG |
| 3373 | netbeans_unmodified(buf); |
| 3374 | #endif |
| 3375 | } |
| 3376 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3377 | /* |
| 3378 | * check_status: called when the status bars for the buffer 'buf' |
| 3379 | * need to be updated |
| 3380 | */ |
| 3381 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3382 | check_status(buf_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3383 | { |
| 3384 | win_T *wp; |
| 3385 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 3386 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3387 | if (wp->w_buffer == buf && wp->w_status_height) |
| 3388 | { |
| 3389 | wp->w_redr_status = TRUE; |
| 3390 | if (must_redraw < VALID) |
| 3391 | must_redraw = VALID; |
| 3392 | } |
| 3393 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3394 | |
| 3395 | /* |
| 3396 | * If the file is readonly, give a warning message with the first change. |
| 3397 | * Don't do this for autocommands. |
| 3398 | * Don't use emsg(), because it flushes the macro buffer. |
Bram Moolenaar | d5cdbeb | 2005-10-10 20:59:28 +0000 | [diff] [blame] | 3399 | * 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] | 3400 | * will be TRUE. |
Bram Moolenaar | b0b5088 | 2010-07-07 18:26:28 +0200 | [diff] [blame] | 3401 | * Careful: may trigger autocommands that reload the buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3402 | */ |
| 3403 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3404 | change_warning( |
| 3405 | int col) /* column for message; non-zero when in insert |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3406 | mode and 'showmode' is on */ |
| 3407 | { |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 3408 | static char *w_readonly = N_("W10: Warning: Changing a readonly file"); |
| 3409 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3410 | if (curbuf->b_did_warn == FALSE |
| 3411 | && curbufIsChanged() == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3412 | && !autocmd_busy |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3413 | && curbuf->b_p_ro) |
| 3414 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3415 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3416 | apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3417 | --curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3418 | if (!curbuf->b_p_ro) |
| 3419 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3420 | /* |
| 3421 | * Do what msg() does, but with a column offset if the warning should |
| 3422 | * be after the mode message. |
| 3423 | */ |
| 3424 | msg_start(); |
| 3425 | if (msg_row == Rows - 1) |
| 3426 | msg_col = col; |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3427 | msg_source(HL_ATTR(HLF_W)); |
| 3428 | MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST); |
Bram Moolenaar | 496c526 | 2009-03-18 14:42:00 +0000 | [diff] [blame] | 3429 | #ifdef FEAT_EVAL |
| 3430 | set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1); |
| 3431 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3432 | msg_clr_eos(); |
| 3433 | (void)msg_end(); |
Bram Moolenaar | e5f2a07 | 2017-02-01 22:31:49 +0100 | [diff] [blame] | 3434 | if (msg_silent == 0 && !silent_mode |
| 3435 | #ifdef FEAT_EVAL |
| 3436 | && time_for_testing != 1 |
| 3437 | #endif |
| 3438 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3439 | { |
| 3440 | out_flush(); |
| 3441 | ui_delay(1000L, TRUE); /* give the user time to think about it */ |
| 3442 | } |
| 3443 | curbuf->b_did_warn = TRUE; |
| 3444 | redraw_cmdline = FALSE; /* don't redraw and erase the message */ |
| 3445 | if (msg_row < Rows - 1) |
| 3446 | showmode(); |
| 3447 | } |
| 3448 | } |
| 3449 | |
| 3450 | /* |
| 3451 | * Ask for a reply from the user, a 'y' or a 'n'. |
| 3452 | * No other characters are accepted, the message is repeated until a valid |
| 3453 | * reply is entered or CTRL-C is hit. |
| 3454 | * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters |
| 3455 | * from any buffers but directly from the user. |
| 3456 | * |
| 3457 | * return the 'y' or 'n' |
| 3458 | */ |
| 3459 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3460 | ask_yesno(char_u *str, int direct) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3461 | { |
| 3462 | int r = ' '; |
| 3463 | int save_State = State; |
| 3464 | |
| 3465 | if (exiting) /* put terminal in raw mode for this question */ |
| 3466 | settmode(TMODE_RAW); |
| 3467 | ++no_wait_return; |
| 3468 | #ifdef USE_ON_FLY_SCROLL |
| 3469 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 3470 | #endif |
| 3471 | State = CONFIRM; /* mouse behaves like with :confirm */ |
| 3472 | #ifdef FEAT_MOUSE |
| 3473 | setmouse(); /* disables mouse for xterm */ |
| 3474 | #endif |
| 3475 | ++no_mapping; |
| 3476 | ++allow_keys; /* no mapping here, but recognize keys */ |
| 3477 | |
| 3478 | while (r != 'y' && r != 'n') |
| 3479 | { |
| 3480 | /* same highlighting as for wait_return */ |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3481 | smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3482 | if (direct) |
| 3483 | r = get_keystroke(); |
| 3484 | else |
Bram Moolenaar | 913626c | 2008-01-03 11:43:42 +0000 | [diff] [blame] | 3485 | r = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3486 | if (r == Ctrl_C || r == ESC) |
| 3487 | r = 'n'; |
| 3488 | msg_putchar(r); /* show what you typed */ |
| 3489 | out_flush(); |
| 3490 | } |
| 3491 | --no_wait_return; |
| 3492 | State = save_State; |
| 3493 | #ifdef FEAT_MOUSE |
| 3494 | setmouse(); |
| 3495 | #endif |
| 3496 | --no_mapping; |
| 3497 | --allow_keys; |
| 3498 | |
| 3499 | return r; |
| 3500 | } |
| 3501 | |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3502 | #if defined(FEAT_MOUSE) || defined(PROTO) |
| 3503 | /* |
| 3504 | * Return TRUE if "c" is a mouse key. |
| 3505 | */ |
| 3506 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3507 | is_mouse_key(int c) |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3508 | { |
| 3509 | return c == K_LEFTMOUSE |
| 3510 | || c == K_LEFTMOUSE_NM |
| 3511 | || c == K_LEFTDRAG |
| 3512 | || c == K_LEFTRELEASE |
| 3513 | || c == K_LEFTRELEASE_NM |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 3514 | || c == K_MOUSEMOVE |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3515 | || c == K_MIDDLEMOUSE |
| 3516 | || c == K_MIDDLEDRAG |
| 3517 | || c == K_MIDDLERELEASE |
| 3518 | || c == K_RIGHTMOUSE |
| 3519 | || c == K_RIGHTDRAG |
| 3520 | || c == K_RIGHTRELEASE |
| 3521 | || c == K_MOUSEDOWN |
| 3522 | || c == K_MOUSEUP |
| 3523 | || c == K_MOUSELEFT |
| 3524 | || c == K_MOUSERIGHT |
| 3525 | || c == K_X1MOUSE |
| 3526 | || c == K_X1DRAG |
| 3527 | || c == K_X1RELEASE |
| 3528 | || c == K_X2MOUSE |
| 3529 | || c == K_X2DRAG |
| 3530 | || c == K_X2RELEASE; |
| 3531 | } |
| 3532 | #endif |
| 3533 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3534 | /* |
| 3535 | * Get a key stroke directly from the user. |
| 3536 | * Ignores mouse clicks and scrollbar events, except a click for the left |
| 3537 | * button (used at the more prompt). |
| 3538 | * Doesn't use vgetc(), because it syncs undo and eats mapped characters. |
| 3539 | * Disadvantage: typeahead is ignored. |
| 3540 | * Translates the interrupt character for unix to ESC. |
| 3541 | */ |
| 3542 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3543 | get_keystroke(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3544 | { |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3545 | char_u *buf = NULL; |
| 3546 | int buflen = 150; |
| 3547 | int maxlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3548 | int len = 0; |
| 3549 | int n; |
| 3550 | int save_mapped_ctrl_c = mapped_ctrl_c; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3551 | int waited = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3552 | |
| 3553 | mapped_ctrl_c = FALSE; /* mappings are not used here */ |
| 3554 | for (;;) |
| 3555 | { |
| 3556 | cursor_on(); |
| 3557 | out_flush(); |
| 3558 | |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3559 | /* Leave some room for check_termcode() to insert a key code into (max |
| 3560 | * 5 chars plus NUL). And fix_input_buffer() can triple the number of |
| 3561 | * bytes. */ |
| 3562 | maxlen = (buflen - 6 - len) / 3; |
| 3563 | if (buf == NULL) |
| 3564 | buf = alloc(buflen); |
| 3565 | else if (maxlen < 10) |
| 3566 | { |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 3567 | char_u *t_buf = buf; |
| 3568 | |
Bram Moolenaar | dc7e85e | 2012-06-20 12:40:08 +0200 | [diff] [blame] | 3569 | /* Need some more space. This might happen when receiving a long |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3570 | * escape sequence. */ |
| 3571 | buflen += 100; |
| 3572 | buf = vim_realloc(buf, buflen); |
Bram Moolenaar | 9abd5c6 | 2015-02-10 18:34:01 +0100 | [diff] [blame] | 3573 | if (buf == NULL) |
| 3574 | vim_free(t_buf); |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3575 | maxlen = (buflen - 6 - len) / 3; |
| 3576 | } |
| 3577 | if (buf == NULL) |
| 3578 | { |
| 3579 | do_outofmem_msg((long_u)buflen); |
| 3580 | return ESC; /* panic! */ |
| 3581 | } |
| 3582 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3583 | /* First time: blocking wait. Second time: wait up to 100ms for a |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3584 | * terminal code to complete. */ |
| 3585 | n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3586 | if (n > 0) |
| 3587 | { |
| 3588 | /* Replace zero and CSI by a special key code. */ |
Bram Moolenaar | 6bff02e | 2016-08-16 22:50:55 +0200 | [diff] [blame] | 3589 | n = fix_input_buffer(buf + len, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3590 | len += n; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3591 | waited = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3592 | } |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3593 | else if (len > 0) |
| 3594 | ++waited; /* keep track of the waiting time */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3595 | |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3596 | /* Incomplete termcode and not timed out yet: get more characters */ |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3597 | if ((n = check_termcode(1, buf, buflen, &len)) < 0 |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3598 | && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3599 | continue; |
Bram Moolenaar | 4395a71 | 2006-09-05 18:57:57 +0000 | [diff] [blame] | 3600 | |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3601 | if (n == KEYLEN_REMOVED) /* key code removed */ |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3602 | { |
Bram Moolenaar | fd30cd4 | 2011-03-22 13:07:26 +0100 | [diff] [blame] | 3603 | if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0) |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3604 | { |
| 3605 | /* Redrawing was postponed, do it now. */ |
| 3606 | update_screen(0); |
| 3607 | setcursor(); /* put cursor back where it belongs */ |
| 3608 | } |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3609 | continue; |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3610 | } |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3611 | if (n > 0) /* found a termcode: adjust length */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3612 | len = n; |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 3613 | if (len == 0) /* nothing typed yet */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3614 | continue; |
| 3615 | |
| 3616 | /* Handle modifier and/or special key code. */ |
| 3617 | n = buf[0]; |
| 3618 | if (n == K_SPECIAL) |
| 3619 | { |
| 3620 | n = TO_SPECIAL(buf[1], buf[2]); |
| 3621 | if (buf[1] == KS_MODIFIER |
| 3622 | || n == K_IGNORE |
Bram Moolenaar | a5be25e | 2013-03-16 21:35:33 +0100 | [diff] [blame] | 3623 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3624 | || (is_mouse_key(n) && n != K_LEFTMOUSE) |
Bram Moolenaar | a5be25e | 2013-03-16 21:35:33 +0100 | [diff] [blame] | 3625 | #endif |
Bram Moolenaar | 2526ef2 | 2013-03-16 14:20:51 +0100 | [diff] [blame] | 3626 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3627 | || n == K_VER_SCROLLBAR |
| 3628 | || n == K_HOR_SCROLLBAR |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3629 | #endif |
| 3630 | ) |
| 3631 | { |
| 3632 | if (buf[1] == KS_MODIFIER) |
| 3633 | mod_mask = buf[2]; |
| 3634 | len -= 3; |
| 3635 | if (len > 0) |
| 3636 | mch_memmove(buf, buf + 3, (size_t)len); |
| 3637 | continue; |
| 3638 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 3639 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3640 | } |
| 3641 | #ifdef FEAT_MBYTE |
| 3642 | if (has_mbyte) |
| 3643 | { |
| 3644 | if (MB_BYTE2LEN(n) > len) |
| 3645 | continue; /* more bytes to get */ |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3646 | buf[len >= buflen ? buflen - 1 : len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3647 | n = (*mb_ptr2char)(buf); |
| 3648 | } |
| 3649 | #endif |
| 3650 | #ifdef UNIX |
| 3651 | if (n == intr_char) |
| 3652 | n = ESC; |
| 3653 | #endif |
| 3654 | break; |
| 3655 | } |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 3656 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3657 | |
| 3658 | mapped_ctrl_c = save_mapped_ctrl_c; |
| 3659 | return n; |
| 3660 | } |
| 3661 | |
| 3662 | /* |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3663 | * Get a number from the user. |
| 3664 | * When "mouse_used" is not NULL allow using the mouse. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3665 | */ |
| 3666 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3667 | get_number( |
| 3668 | int colon, /* allow colon to abort */ |
| 3669 | int *mouse_used) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3670 | { |
| 3671 | int n = 0; |
| 3672 | int c; |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3673 | int typed = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3674 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3675 | if (mouse_used != NULL) |
| 3676 | *mouse_used = FALSE; |
| 3677 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3678 | /* When not printing messages, the user won't know what to type, return a |
| 3679 | * zero (as if CR was hit). */ |
| 3680 | if (msg_silent != 0) |
| 3681 | return 0; |
| 3682 | |
| 3683 | #ifdef USE_ON_FLY_SCROLL |
| 3684 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 3685 | #endif |
| 3686 | ++no_mapping; |
| 3687 | ++allow_keys; /* no mapping here, but recognize keys */ |
| 3688 | for (;;) |
| 3689 | { |
| 3690 | windgoto(msg_row, msg_col); |
| 3691 | c = safe_vgetc(); |
| 3692 | if (VIM_ISDIGIT(c)) |
| 3693 | { |
| 3694 | n = n * 10 + c - '0'; |
| 3695 | msg_putchar(c); |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3696 | ++typed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3697 | } |
| 3698 | else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H) |
| 3699 | { |
Bram Moolenaar | 3991dab | 2006-03-27 17:01:56 +0000 | [diff] [blame] | 3700 | if (typed > 0) |
| 3701 | { |
| 3702 | MSG_PUTS("\b \b"); |
| 3703 | --typed; |
| 3704 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3705 | n /= 10; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3706 | } |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3707 | #ifdef FEAT_MOUSE |
| 3708 | else if (mouse_used != NULL && c == K_LEFTMOUSE) |
| 3709 | { |
| 3710 | *mouse_used = TRUE; |
| 3711 | n = mouse_row + 1; |
| 3712 | break; |
| 3713 | } |
| 3714 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3715 | else if (n == 0 && c == ':' && colon) |
| 3716 | { |
| 3717 | stuffcharReadbuff(':'); |
| 3718 | if (!exmode_active) |
| 3719 | cmdline_row = msg_row; |
| 3720 | skip_redraw = TRUE; /* skip redraw once */ |
| 3721 | do_redraw = FALSE; |
| 3722 | break; |
| 3723 | } |
| 3724 | else if (c == CAR || c == NL || c == Ctrl_C || c == ESC) |
| 3725 | break; |
| 3726 | } |
| 3727 | --no_mapping; |
| 3728 | --allow_keys; |
| 3729 | return n; |
| 3730 | } |
| 3731 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3732 | /* |
| 3733 | * Ask the user to enter a number. |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3734 | * When "mouse_used" is not NULL allow using the mouse and in that case return |
| 3735 | * the line number. |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3736 | */ |
| 3737 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3738 | prompt_for_number(int *mouse_used) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3739 | { |
| 3740 | int i; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3741 | int save_cmdline_row; |
| 3742 | int save_State; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3743 | |
| 3744 | /* When using ":silent" assume that <CR> was entered. */ |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3745 | if (mouse_used != NULL) |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 3746 | MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): ")); |
Bram Moolenaar | 42eeac3 | 2005-06-29 22:40:58 +0000 | [diff] [blame] | 3747 | else |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 3748 | MSG_PUTS(_("Type number and <Enter> (empty cancels): ")); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3749 | |
Bram Moolenaar | 203335e | 2006-09-03 14:35:42 +0000 | [diff] [blame] | 3750 | /* Set the state such that text can be selected/copied/pasted and we still |
| 3751 | * get mouse events. */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3752 | save_cmdline_row = cmdline_row; |
Bram Moolenaar | 203335e | 2006-09-03 14:35:42 +0000 | [diff] [blame] | 3753 | cmdline_row = 0; |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3754 | save_State = State; |
Bram Moolenaar | c904107 | 2017-07-16 15:48:46 +0200 | [diff] [blame] | 3755 | State = ASKMORE; /* prevents a screen update when using a timer */ |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3756 | #ifdef FEAT_MOUSE |
| 3757 | /* May show different mouse shape. */ |
| 3758 | setmouse(); |
| 3759 | #endif |
| 3760 | |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3761 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3762 | i = get_number(TRUE, mouse_used); |
| 3763 | if (KeyTyped) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3764 | { |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 3765 | /* don't call wait_return() now */ |
| 3766 | /* msg_putchar('\n'); */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3767 | cmdline_row = msg_row - 1; |
| 3768 | need_wait_return = FALSE; |
| 3769 | msg_didany = FALSE; |
Bram Moolenaar | b245016 | 2009-07-22 09:04:20 +0000 | [diff] [blame] | 3770 | msg_didout = FALSE; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3771 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3772 | else |
| 3773 | cmdline_row = save_cmdline_row; |
| 3774 | State = save_State; |
Bram Moolenaar | 7365831 | 2018-04-24 17:41:57 +0200 | [diff] [blame] | 3775 | #ifdef FEAT_MOUSE |
| 3776 | /* May need to restore mouse shape. */ |
| 3777 | setmouse(); |
| 3778 | #endif |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 3779 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3780 | return i; |
| 3781 | } |
| 3782 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3783 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3784 | msgmore(long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3785 | { |
| 3786 | long pn; |
| 3787 | |
| 3788 | if (global_busy /* no messages now, wait until global is finished */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3789 | || !messaging()) /* 'lazyredraw' set, don't do messages now */ |
| 3790 | return; |
| 3791 | |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 3792 | /* We don't want to overwrite another important message, but do overwrite |
| 3793 | * a previous "more lines" or "fewer lines" message, so that "5dd" and |
| 3794 | * then "put" reports the last action. */ |
| 3795 | if (keep_msg != NULL && !keep_msg_more) |
| 3796 | return; |
| 3797 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3798 | if (n > 0) |
| 3799 | pn = n; |
| 3800 | else |
| 3801 | pn = -n; |
| 3802 | |
| 3803 | if (pn > p_report) |
| 3804 | { |
| 3805 | if (pn == 1) |
| 3806 | { |
| 3807 | if (n > 0) |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3808 | vim_strncpy(msg_buf, (char_u *)_("1 more line"), |
| 3809 | MSG_BUF_LEN - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3810 | else |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3811 | vim_strncpy(msg_buf, (char_u *)_("1 line less"), |
| 3812 | MSG_BUF_LEN - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3813 | } |
| 3814 | else |
| 3815 | { |
| 3816 | if (n > 0) |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3817 | vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
| 3818 | _("%ld more lines"), pn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3819 | else |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3820 | vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
| 3821 | _("%ld fewer lines"), pn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3822 | } |
| 3823 | if (got_int) |
Bram Moolenaar | ef9d6aa | 2011-04-11 16:56:35 +0200 | [diff] [blame] | 3824 | vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3825 | if (msg(msg_buf)) |
| 3826 | { |
Bram Moolenaar | 238a564 | 2006-02-21 22:12:05 +0000 | [diff] [blame] | 3827 | set_keep_msg(msg_buf, 0); |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 3828 | keep_msg_more = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3829 | } |
| 3830 | } |
| 3831 | } |
| 3832 | |
| 3833 | /* |
| 3834 | * flush map and typeahead buffers and give a warning for an error |
| 3835 | */ |
| 3836 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3837 | beep_flush(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3838 | { |
| 3839 | if (emsg_silent == 0) |
| 3840 | { |
| 3841 | flush_buffers(FALSE); |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3842 | vim_beep(BO_ERROR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3843 | } |
| 3844 | } |
| 3845 | |
| 3846 | /* |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3847 | * Give a warning for an error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3848 | */ |
| 3849 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3850 | vim_beep( |
| 3851 | unsigned val) /* one of the BO_ values, e.g., BO_OPER */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3852 | { |
Bram Moolenaar | b48e96f | 2018-02-13 12:26:14 +0100 | [diff] [blame] | 3853 | #ifdef FEAT_EVAL |
| 3854 | called_vim_beep = TRUE; |
| 3855 | #endif |
| 3856 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3857 | if (emsg_silent == 0) |
| 3858 | { |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3859 | if (!((bo_flags & val) || (bo_flags & BO_ALL))) |
| 3860 | { |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3861 | #ifdef ELAPSED_FUNC |
| 3862 | static int did_init = FALSE; |
| 3863 | static ELAPSED_TYPE start_tv; |
| 3864 | |
| 3865 | /* Only beep once per half a second, otherwise a sequence of beeps |
| 3866 | * would freeze Vim. */ |
| 3867 | if (!did_init || ELAPSED_FUNC(start_tv) > 500) |
| 3868 | { |
| 3869 | did_init = TRUE; |
| 3870 | ELAPSED_INIT(start_tv); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3871 | #endif |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3872 | if (p_vb |
| 3873 | #ifdef FEAT_GUI |
| 3874 | /* While the GUI is starting up the termcap is set for |
| 3875 | * the GUI but the output still goes to a terminal. */ |
| 3876 | && !(gui.in_use && gui.starting) |
| 3877 | #endif |
| 3878 | ) |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3879 | { |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3880 | out_str_cf(T_VB); |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3881 | #ifdef FEAT_VTP |
| 3882 | /* No restore color information, refresh the screen. */ |
| 3883 | if (has_vtp_working() != 0 |
| 3884 | # ifdef FEAT_TERMGUICOLORS |
Bram Moolenaar | c5cd885 | 2018-05-01 15:47:38 +0200 | [diff] [blame] | 3885 | && (p_tgc || (!p_tgc && t_colors >= 256)) |
Bram Moolenaar | cafafb3 | 2018-02-22 21:07:09 +0100 | [diff] [blame] | 3886 | # endif |
| 3887 | ) |
| 3888 | { |
| 3889 | redraw_later(CLEAR); |
| 3890 | update_screen(0); |
| 3891 | redrawcmd(); |
| 3892 | } |
| 3893 | #endif |
| 3894 | } |
Bram Moolenaar | 2e147ca | 2017-06-27 17:09:37 +0200 | [diff] [blame] | 3895 | else |
| 3896 | out_char(BELL); |
| 3897 | #ifdef ELAPSED_FUNC |
| 3898 | } |
| 3899 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3900 | } |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3901 | |
Bram Moolenaar | b48e96f | 2018-02-13 12:26:14 +0100 | [diff] [blame] | 3902 | /* When 'debug' contains "beep" produce a message. If we are sourcing |
| 3903 | * a script or executing a function give the user a hint where the beep |
| 3904 | * comes from. */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3905 | if (vim_strchr(p_debug, 'e') != NULL) |
| 3906 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3907 | msg_source(HL_ATTR(HLF_W)); |
| 3908 | msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W)); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 3909 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | /* |
| 3914 | * To get the "real" home directory: |
| 3915 | * - get value of $HOME |
| 3916 | * For Unix: |
| 3917 | * - go to that directory |
| 3918 | * - do mch_dirname() to get the real name of that directory. |
| 3919 | * This also works with mounts and links. |
| 3920 | * Don't do this for MS-DOS, it will change the "current dir" for a drive. |
| 3921 | */ |
| 3922 | static char_u *homedir = NULL; |
| 3923 | |
| 3924 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 3925 | init_homedir(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3926 | { |
| 3927 | char_u *var; |
| 3928 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3929 | /* In case we are called a second time (when 'encoding' changes). */ |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3930 | VIM_CLEAR(homedir); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3931 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3932 | #ifdef VMS |
| 3933 | var = mch_getenv((char_u *)"SYS$LOGIN"); |
| 3934 | #else |
| 3935 | var = mch_getenv((char_u *)"HOME"); |
| 3936 | #endif |
| 3937 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3938 | #ifdef WIN3264 |
| 3939 | /* |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3940 | * Typically, $HOME is not defined on Windows, unless the user has |
| 3941 | * specifically defined it for Vim's sake. However, on Windows NT |
| 3942 | * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for |
| 3943 | * each user. Try constructing $HOME from these. |
| 3944 | */ |
Bram Moolenaar | b47a259 | 2017-08-30 13:22:28 +0200 | [diff] [blame] | 3945 | if (var == NULL || *var == NUL) |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3946 | { |
| 3947 | char_u *homedrive, *homepath; |
| 3948 | |
| 3949 | homedrive = mch_getenv((char_u *)"HOMEDRIVE"); |
| 3950 | homepath = mch_getenv((char_u *)"HOMEPATH"); |
| 3951 | if (homepath == NULL || *homepath == NUL) |
| 3952 | homepath = (char_u *)"\\"; |
| 3953 | if (homedrive != NULL |
| 3954 | && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL) |
| 3955 | { |
| 3956 | sprintf((char *)NameBuff, "%s%s", homedrive, homepath); |
| 3957 | if (NameBuff[0] != NUL) |
| 3958 | var = NameBuff; |
| 3959 | } |
| 3960 | } |
| 3961 | |
| 3962 | if (var == NULL) |
| 3963 | var = mch_getenv((char_u *)"USERPROFILE"); |
| 3964 | |
| 3965 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3966 | * Weird but true: $HOME may contain an indirect reference to another |
| 3967 | * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set |
| 3968 | * when $HOME is being set. |
| 3969 | */ |
| 3970 | if (var != NULL && *var == '%') |
| 3971 | { |
| 3972 | char_u *p; |
| 3973 | char_u *exp; |
| 3974 | |
| 3975 | p = vim_strchr(var + 1, '%'); |
| 3976 | if (p != NULL) |
| 3977 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3978 | vim_strncpy(NameBuff, var + 1, p - (var + 1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3979 | exp = mch_getenv(NameBuff); |
| 3980 | if (exp != NULL && *exp != NUL |
| 3981 | && STRLEN(exp) + STRLEN(p) < MAXPATHL) |
| 3982 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 3983 | vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3984 | var = NameBuff; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3985 | } |
| 3986 | } |
| 3987 | } |
| 3988 | |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3989 | if (var != NULL && *var == NUL) /* empty is same as not set */ |
| 3990 | var = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3991 | |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 3992 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3993 | if (enc_utf8 && var != NULL) |
| 3994 | { |
| 3995 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 3996 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3997 | |
| 3998 | /* Convert from active codepage to UTF-8. Other conversions are |
| 3999 | * not done, because they would fail for non-ASCII characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4000 | acp_to_enc(var, (int)STRLEN(var), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4001 | if (pp != NULL) |
| 4002 | { |
| 4003 | homedir = pp; |
| 4004 | return; |
| 4005 | } |
| 4006 | } |
| 4007 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4008 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4009 | /* |
| 4010 | * Default home dir is C:/ |
| 4011 | * Best assumption we can make in such a situation. |
| 4012 | */ |
| 4013 | if (var == NULL) |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 4014 | var = (char_u *)"C:/"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4015 | #endif |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 4016 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4017 | if (var != NULL) |
| 4018 | { |
| 4019 | #ifdef UNIX |
| 4020 | /* |
| 4021 | * Change to the directory and get the actual path. This resolves |
| 4022 | * links. Don't do it when we can't return. |
| 4023 | */ |
| 4024 | if (mch_dirname(NameBuff, MAXPATHL) == OK |
| 4025 | && mch_chdir((char *)NameBuff) == 0) |
| 4026 | { |
| 4027 | if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK) |
| 4028 | var = IObuff; |
| 4029 | if (mch_chdir((char *)NameBuff) != 0) |
| 4030 | EMSG(_(e_prev_dir)); |
| 4031 | } |
| 4032 | #endif |
| 4033 | homedir = vim_strsave(var); |
| 4034 | } |
| 4035 | } |
| 4036 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 4037 | #if defined(EXITFREE) || defined(PROTO) |
| 4038 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4039 | free_homedir(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 4040 | { |
| 4041 | vim_free(homedir); |
| 4042 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4043 | |
| 4044 | # ifdef FEAT_CMDL_COMPL |
| 4045 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4046 | free_users(void) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4047 | { |
| 4048 | ga_clear_strings(&ga_users); |
| 4049 | } |
| 4050 | # endif |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 4051 | #endif |
| 4052 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4053 | /* |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4054 | * Call expand_env() and store the result in an allocated string. |
| 4055 | * This is not very memory efficient, this expects the result to be freed |
| 4056 | * again soon. |
| 4057 | */ |
| 4058 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4059 | expand_env_save(char_u *src) |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4060 | { |
| 4061 | return expand_env_save_opt(src, FALSE); |
| 4062 | } |
| 4063 | |
| 4064 | /* |
| 4065 | * Idem, but when "one" is TRUE handle the string as one file name, only |
| 4066 | * expand "~" at the start. |
| 4067 | */ |
| 4068 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4069 | expand_env_save_opt(char_u *src, int one) |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4070 | { |
| 4071 | char_u *p; |
| 4072 | |
| 4073 | p = alloc(MAXPATHL); |
| 4074 | if (p != NULL) |
| 4075 | expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL); |
| 4076 | return p; |
| 4077 | } |
| 4078 | |
| 4079 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4080 | * Expand environment variable with path name. |
| 4081 | * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded. |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4082 | * Skips over "\ ", "\~" and "\$" (not for Win32 though). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4083 | * If anything fails no expansion is done and dst equals src. |
| 4084 | */ |
| 4085 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4086 | expand_env( |
| 4087 | char_u *src, /* input string e.g. "$HOME/vim.hlp" */ |
| 4088 | char_u *dst, /* where to put the result */ |
| 4089 | int dstlen) /* maximum length of the result */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4090 | { |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4091 | expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4095 | expand_env_esc( |
| 4096 | char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */ |
| 4097 | char_u *dst, /* where to put the result */ |
| 4098 | int dstlen, /* maximum length of the result */ |
| 4099 | int esc, /* escape spaces in expanded variables */ |
| 4100 | int one, /* "srcp" is one file name */ |
| 4101 | char_u *startstr) /* start again after this (can be NULL) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4102 | { |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4103 | char_u *src; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4104 | char_u *tail; |
| 4105 | int c; |
| 4106 | char_u *var; |
| 4107 | int copy_char; |
| 4108 | int mustfree; /* var was allocated, need to free it later */ |
| 4109 | int at_start = TRUE; /* at start of a name */ |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4110 | int startstr_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4111 | |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4112 | if (startstr != NULL) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4113 | startstr_len = (int)STRLEN(startstr); |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4114 | |
| 4115 | src = skipwhite(srcp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4116 | --dstlen; /* leave one char space for "\," */ |
| 4117 | while (*src && dstlen > 0) |
| 4118 | { |
Bram Moolenaar | be83b73 | 2015-08-25 14:21:19 +0200 | [diff] [blame] | 4119 | #ifdef FEAT_EVAL |
| 4120 | /* Skip over `=expr`. */ |
| 4121 | if (src[0] == '`' && src[1] == '=') |
| 4122 | { |
| 4123 | size_t len; |
| 4124 | |
| 4125 | var = src; |
| 4126 | src += 2; |
| 4127 | (void)skip_expr(&src); |
| 4128 | if (*src == '`') |
| 4129 | ++src; |
| 4130 | len = src - var; |
| 4131 | if (len > (size_t)dstlen) |
| 4132 | len = dstlen; |
| 4133 | vim_strncpy(dst, var, len); |
| 4134 | dst += len; |
Bram Moolenaar | 5df1ed2 | 2015-09-01 16:25:34 +0200 | [diff] [blame] | 4135 | dstlen -= (int)len; |
Bram Moolenaar | be83b73 | 2015-08-25 14:21:19 +0200 | [diff] [blame] | 4136 | continue; |
| 4137 | } |
| 4138 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4139 | copy_char = TRUE; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 4140 | if ((*src == '$' |
| 4141 | #ifdef VMS |
| 4142 | && at_start |
| 4143 | #endif |
| 4144 | ) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4145 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4146 | || *src == '%' |
| 4147 | #endif |
| 4148 | || (*src == '~' && at_start)) |
| 4149 | { |
| 4150 | mustfree = FALSE; |
| 4151 | |
| 4152 | /* |
| 4153 | * The variable name is copied into dst temporarily, because it may |
| 4154 | * be a string in read-only memory and a NUL needs to be appended. |
| 4155 | */ |
| 4156 | if (*src != '~') /* environment var */ |
| 4157 | { |
| 4158 | tail = src + 1; |
| 4159 | var = dst; |
| 4160 | c = dstlen - 1; |
| 4161 | |
| 4162 | #ifdef UNIX |
| 4163 | /* Unix has ${var-name} type environment vars */ |
| 4164 | if (*tail == '{' && !vim_isIDc('{')) |
| 4165 | { |
| 4166 | tail++; /* ignore '{' */ |
| 4167 | while (c-- > 0 && *tail && *tail != '}') |
| 4168 | *var++ = *tail++; |
| 4169 | } |
| 4170 | else |
| 4171 | #endif |
| 4172 | { |
| 4173 | while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail)) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4174 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4175 | || (*src == '%' && *tail != '%') |
| 4176 | #endif |
| 4177 | )) |
| 4178 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4179 | *var++ = *tail++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4180 | } |
| 4181 | } |
| 4182 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4183 | #if defined(MSWIN) || defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4184 | # ifdef UNIX |
| 4185 | if (src[1] == '{' && *tail != '}') |
| 4186 | # else |
| 4187 | if (*src == '%' && *tail != '%') |
| 4188 | # endif |
| 4189 | var = NULL; |
| 4190 | else |
| 4191 | { |
| 4192 | # ifdef UNIX |
| 4193 | if (src[1] == '{') |
| 4194 | # else |
| 4195 | if (*src == '%') |
| 4196 | #endif |
| 4197 | ++tail; |
| 4198 | #endif |
| 4199 | *var = NUL; |
| 4200 | var = vim_getenv(dst, &mustfree); |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4201 | #if defined(MSWIN) || defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4202 | } |
| 4203 | #endif |
| 4204 | } |
| 4205 | /* home directory */ |
| 4206 | else if ( src[1] == NUL |
| 4207 | || vim_ispathsep(src[1]) |
| 4208 | || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL) |
| 4209 | { |
| 4210 | var = homedir; |
| 4211 | tail = src + 1; |
| 4212 | } |
| 4213 | else /* user directory */ |
| 4214 | { |
| 4215 | #if defined(UNIX) || (defined(VMS) && defined(USER_HOME)) |
| 4216 | /* |
| 4217 | * Copy ~user to dst[], so we can put a NUL after it. |
| 4218 | */ |
| 4219 | tail = src; |
| 4220 | var = dst; |
| 4221 | c = dstlen - 1; |
| 4222 | while ( c-- > 0 |
| 4223 | && *tail |
| 4224 | && vim_isfilec(*tail) |
| 4225 | && !vim_ispathsep(*tail)) |
| 4226 | *var++ = *tail++; |
| 4227 | *var = NUL; |
| 4228 | # ifdef UNIX |
| 4229 | /* |
| 4230 | * If the system supports getpwnam(), use it. |
| 4231 | * Otherwise, or if getpwnam() fails, the shell is used to |
| 4232 | * expand ~user. This is slower and may fail if the shell |
| 4233 | * does not support ~user (old versions of /bin/sh). |
| 4234 | */ |
| 4235 | # if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H) |
| 4236 | { |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 4237 | /* Note: memory allocated by getpwnam() is never freed. |
| 4238 | * Calling endpwent() apparently doesn't help. */ |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 4239 | struct passwd *pw = (*dst == NUL) |
| 4240 | ? NULL : getpwnam((char *)dst + 1); |
| 4241 | |
| 4242 | var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4243 | } |
| 4244 | if (var == NULL) |
| 4245 | # endif |
| 4246 | { |
| 4247 | expand_T xpc; |
| 4248 | |
| 4249 | ExpandInit(&xpc); |
| 4250 | xpc.xp_context = EXPAND_FILES; |
| 4251 | var = ExpandOne(&xpc, dst, NULL, |
| 4252 | WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4253 | mustfree = TRUE; |
| 4254 | } |
| 4255 | |
| 4256 | # else /* !UNIX, thus VMS */ |
| 4257 | /* |
| 4258 | * USER_HOME is a comma-separated list of |
| 4259 | * directories to search for the user account in. |
| 4260 | */ |
| 4261 | { |
| 4262 | char_u test[MAXPATHL], paths[MAXPATHL]; |
| 4263 | char_u *path, *next_path, *ptr; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4264 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4265 | |
| 4266 | STRCPY(paths, USER_HOME); |
| 4267 | next_path = paths; |
| 4268 | while (*next_path) |
| 4269 | { |
| 4270 | for (path = next_path; *next_path && *next_path != ','; |
| 4271 | next_path++); |
| 4272 | if (*next_path) |
| 4273 | *next_path++ = NUL; |
| 4274 | STRCPY(test, path); |
| 4275 | STRCAT(test, "/"); |
| 4276 | STRCAT(test, dst + 1); |
| 4277 | if (mch_stat(test, &st) == 0) |
| 4278 | { |
| 4279 | var = alloc(STRLEN(test) + 1); |
| 4280 | STRCPY(var, test); |
| 4281 | mustfree = TRUE; |
| 4282 | break; |
| 4283 | } |
| 4284 | } |
| 4285 | } |
| 4286 | # endif /* UNIX */ |
| 4287 | #else |
| 4288 | /* cannot expand user's home directory, so don't try */ |
| 4289 | var = NULL; |
| 4290 | tail = (char_u *)""; /* for gcc */ |
| 4291 | #endif /* UNIX || VMS */ |
| 4292 | } |
| 4293 | |
| 4294 | #ifdef BACKSLASH_IN_FILENAME |
| 4295 | /* If 'shellslash' is set change backslashes to forward slashes. |
| 4296 | * Can't use slash_adjust(), p_ssl may be set temporarily. */ |
| 4297 | if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL) |
| 4298 | { |
| 4299 | char_u *p = vim_strsave(var); |
| 4300 | |
| 4301 | if (p != NULL) |
| 4302 | { |
| 4303 | if (mustfree) |
| 4304 | vim_free(var); |
| 4305 | var = p; |
| 4306 | mustfree = TRUE; |
| 4307 | forward_slash(var); |
| 4308 | } |
| 4309 | } |
| 4310 | #endif |
| 4311 | |
| 4312 | /* If "var" contains white space, escape it with a backslash. |
| 4313 | * Required for ":e ~/tt" when $HOME includes a space. */ |
| 4314 | if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) |
| 4315 | { |
| 4316 | char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); |
| 4317 | |
| 4318 | if (p != NULL) |
| 4319 | { |
| 4320 | if (mustfree) |
| 4321 | vim_free(var); |
| 4322 | var = p; |
| 4323 | mustfree = TRUE; |
| 4324 | } |
| 4325 | } |
| 4326 | |
| 4327 | if (var != NULL && *var != NUL |
| 4328 | && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) |
| 4329 | { |
| 4330 | STRCPY(dst, var); |
| 4331 | dstlen -= (int)STRLEN(var); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4332 | c = (int)STRLEN(var); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4333 | /* if var[] ends in a path separator and tail[] starts |
| 4334 | * with it, skip a character */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4335 | if (*var != NUL && after_pathsep(dst, dst + c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4336 | #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) |
| 4337 | && dst[-1] != ':' |
| 4338 | #endif |
| 4339 | && vim_ispathsep(*tail)) |
| 4340 | ++tail; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4341 | dst += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4342 | src = tail; |
| 4343 | copy_char = FALSE; |
| 4344 | } |
| 4345 | if (mustfree) |
| 4346 | vim_free(var); |
| 4347 | } |
| 4348 | |
| 4349 | if (copy_char) /* copy at least one char */ |
| 4350 | { |
| 4351 | /* |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 4352 | * Recognize the start of a new name, for '~'. |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4353 | * Don't do this when "one" is TRUE, to avoid expanding "~" in |
| 4354 | * ":edit foo ~ foo". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4355 | */ |
| 4356 | at_start = FALSE; |
| 4357 | if (src[0] == '\\' && src[1] != NUL) |
| 4358 | { |
| 4359 | *dst++ = *src++; |
| 4360 | --dstlen; |
| 4361 | } |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4362 | else if ((src[0] == ' ' || src[0] == ',') && !one) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4363 | at_start = TRUE; |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4364 | if (dstlen > 0) |
| 4365 | { |
| 4366 | *dst++ = *src++; |
| 4367 | --dstlen; |
Bram Moolenaar | 24bbcfe | 2005-06-28 23:32:02 +0000 | [diff] [blame] | 4368 | |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4369 | if (startstr != NULL && src - startstr_len >= srcp |
| 4370 | && STRNCMP(src - startstr_len, startstr, |
| 4371 | startstr_len) == 0) |
| 4372 | at_start = TRUE; |
| 4373 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4374 | } |
Bram Moolenaar | 1c86409 | 2017-08-06 18:15:45 +0200 | [diff] [blame] | 4375 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4376 | } |
| 4377 | *dst = NUL; |
| 4378 | } |
| 4379 | |
| 4380 | /* |
| 4381 | * Vim's version of getenv(). |
| 4382 | * Special handling of $HOME, $VIM and $VIMRUNTIME. |
Bram Moolenaar | 2f6b0b8 | 2005-03-08 22:43:10 +0000 | [diff] [blame] | 4383 | * Also does ACP to 'enc' conversion for Win32. |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4384 | * "mustfree" is set to TRUE when returned is allocated, it must be |
| 4385 | * initialized to FALSE by the caller. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4386 | */ |
| 4387 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4388 | vim_getenv(char_u *name, int *mustfree) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4389 | { |
| 4390 | char_u *p; |
| 4391 | char_u *pend; |
| 4392 | int vimruntime; |
| 4393 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4394 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4395 | /* use "C:/" when $HOME is not set */ |
| 4396 | if (STRCMP(name, "HOME") == 0) |
| 4397 | return homedir; |
| 4398 | #endif |
| 4399 | |
| 4400 | p = mch_getenv(name); |
| 4401 | if (p != NULL && *p == NUL) /* empty is the same as not set */ |
| 4402 | p = NULL; |
| 4403 | |
| 4404 | if (p != NULL) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4405 | { |
| 4406 | #if defined(FEAT_MBYTE) && defined(WIN3264) |
| 4407 | if (enc_utf8) |
| 4408 | { |
| 4409 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4410 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4411 | |
| 4412 | /* Convert from active codepage to UTF-8. Other conversions are |
| 4413 | * not done, because they would fail for non-ASCII characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4414 | acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4415 | if (pp != NULL) |
| 4416 | { |
| 4417 | p = pp; |
| 4418 | *mustfree = TRUE; |
| 4419 | } |
| 4420 | } |
| 4421 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4422 | return p; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4423 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4424 | |
| 4425 | vimruntime = (STRCMP(name, "VIMRUNTIME") == 0); |
| 4426 | if (!vimruntime && STRCMP(name, "VIM") != 0) |
| 4427 | return NULL; |
| 4428 | |
| 4429 | /* |
| 4430 | * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM. |
| 4431 | * Don't do this when default_vimruntime_dir is non-empty. |
| 4432 | */ |
| 4433 | if (vimruntime |
| 4434 | #ifdef HAVE_PATHDEF |
| 4435 | && *default_vimruntime_dir == NUL |
| 4436 | #endif |
| 4437 | ) |
| 4438 | { |
| 4439 | p = mch_getenv((char_u *)"VIM"); |
| 4440 | if (p != NULL && *p == NUL) /* empty is the same as not set */ |
| 4441 | p = NULL; |
| 4442 | if (p != NULL) |
| 4443 | { |
| 4444 | p = vim_version_dir(p); |
| 4445 | if (p != NULL) |
| 4446 | *mustfree = TRUE; |
| 4447 | else |
| 4448 | p = mch_getenv((char_u *)"VIM"); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4449 | |
| 4450 | #if defined(FEAT_MBYTE) && defined(WIN3264) |
| 4451 | if (enc_utf8) |
| 4452 | { |
| 4453 | int len; |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4454 | char_u *pp = NULL; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4455 | |
| 4456 | /* Convert from active codepage to UTF-8. Other conversions |
| 4457 | * are not done, because they would fail for non-ASCII |
| 4458 | * characters. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4459 | acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4460 | if (pp != NULL) |
| 4461 | { |
Bram Moolenaar | b453a53 | 2011-04-28 17:48:44 +0200 | [diff] [blame] | 4462 | if (*mustfree) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4463 | vim_free(p); |
| 4464 | p = pp; |
| 4465 | *mustfree = TRUE; |
| 4466 | } |
| 4467 | } |
| 4468 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4469 | } |
| 4470 | } |
| 4471 | |
| 4472 | /* |
| 4473 | * When expanding $VIM or $VIMRUNTIME fails, try using: |
| 4474 | * - the directory name from 'helpfile' (unless it contains '$') |
| 4475 | * - the executable name from argv[0] |
| 4476 | */ |
| 4477 | if (p == NULL) |
| 4478 | { |
| 4479 | if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL) |
| 4480 | p = p_hf; |
| 4481 | #ifdef USE_EXE_NAME |
| 4482 | /* |
| 4483 | * Use the name of the executable, obtained from argv[0]. |
| 4484 | */ |
| 4485 | else |
| 4486 | p = exe_name; |
| 4487 | #endif |
| 4488 | if (p != NULL) |
| 4489 | { |
| 4490 | /* remove the file name */ |
| 4491 | pend = gettail(p); |
| 4492 | |
| 4493 | /* remove "doc/" from 'helpfile', if present */ |
| 4494 | if (p == p_hf) |
| 4495 | pend = remove_tail(p, pend, (char_u *)"doc"); |
| 4496 | |
| 4497 | #ifdef USE_EXE_NAME |
| 4498 | # ifdef MACOS_X |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4499 | /* remove "MacOS" from exe_name and add "Resources/vim" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4500 | if (p == exe_name) |
| 4501 | { |
| 4502 | char_u *pend1; |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4503 | char_u *pnew; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4504 | |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4505 | pend1 = remove_tail(p, pend, (char_u *)"MacOS"); |
| 4506 | if (pend1 != pend) |
| 4507 | { |
| 4508 | pnew = alloc((unsigned)(pend1 - p) + 15); |
| 4509 | if (pnew != NULL) |
| 4510 | { |
| 4511 | STRNCPY(pnew, p, (pend1 - p)); |
| 4512 | STRCPY(pnew + (pend1 - p), "Resources/vim"); |
| 4513 | p = pnew; |
| 4514 | pend = p + STRLEN(p); |
| 4515 | } |
| 4516 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4517 | } |
| 4518 | # endif |
| 4519 | /* remove "src/" from exe_name, if present */ |
| 4520 | if (p == exe_name) |
| 4521 | pend = remove_tail(p, pend, (char_u *)"src"); |
| 4522 | #endif |
| 4523 | |
| 4524 | /* for $VIM, remove "runtime/" or "vim54/", if present */ |
| 4525 | if (!vimruntime) |
| 4526 | { |
| 4527 | pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME); |
| 4528 | pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT); |
| 4529 | } |
| 4530 | |
| 4531 | /* remove trailing path separator */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4532 | if (pend > p && after_pathsep(p, pend)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4533 | --pend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4534 | |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 4535 | #ifdef MACOS_X |
| 4536 | if (p == exe_name || p == p_hf) |
| 4537 | #endif |
| 4538 | /* check that the result is a directory name */ |
| 4539 | p = vim_strnsave(p, (int)(pend - p)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4540 | |
| 4541 | if (p != NULL && !mch_isdir(p)) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 4542 | VIM_CLEAR(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4543 | else |
| 4544 | { |
| 4545 | #ifdef USE_EXE_NAME |
| 4546 | /* may add "/vim54" or "/runtime" if it exists */ |
| 4547 | if (vimruntime && (pend = vim_version_dir(p)) != NULL) |
| 4548 | { |
| 4549 | vim_free(p); |
| 4550 | p = pend; |
| 4551 | } |
| 4552 | #endif |
| 4553 | *mustfree = TRUE; |
| 4554 | } |
| 4555 | } |
| 4556 | } |
| 4557 | |
| 4558 | #ifdef HAVE_PATHDEF |
| 4559 | /* When there is a pathdef.c file we can use default_vim_dir and |
| 4560 | * default_vimruntime_dir */ |
| 4561 | if (p == NULL) |
| 4562 | { |
| 4563 | /* Only use default_vimruntime_dir when it is not empty */ |
| 4564 | if (vimruntime && *default_vimruntime_dir != NUL) |
| 4565 | { |
| 4566 | p = default_vimruntime_dir; |
| 4567 | *mustfree = FALSE; |
| 4568 | } |
| 4569 | else if (*default_vim_dir != NUL) |
| 4570 | { |
| 4571 | if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) |
| 4572 | *mustfree = TRUE; |
| 4573 | else |
| 4574 | { |
| 4575 | p = default_vim_dir; |
| 4576 | *mustfree = FALSE; |
| 4577 | } |
| 4578 | } |
| 4579 | } |
| 4580 | #endif |
| 4581 | |
| 4582 | /* |
| 4583 | * Set the environment variable, so that the new value can be found fast |
| 4584 | * next time, and others can also use it (e.g. Perl). |
| 4585 | */ |
| 4586 | if (p != NULL) |
| 4587 | { |
| 4588 | if (vimruntime) |
| 4589 | { |
| 4590 | vim_setenv((char_u *)"VIMRUNTIME", p); |
| 4591 | didset_vimruntime = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4592 | } |
| 4593 | else |
| 4594 | { |
| 4595 | vim_setenv((char_u *)"VIM", p); |
| 4596 | didset_vim = TRUE; |
| 4597 | } |
| 4598 | } |
| 4599 | return p; |
| 4600 | } |
| 4601 | |
| 4602 | /* |
| 4603 | * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. |
| 4604 | * Return NULL if not, return its name in allocated memory otherwise. |
| 4605 | */ |
| 4606 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4607 | vim_version_dir(char_u *vimdir) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4608 | { |
| 4609 | char_u *p; |
| 4610 | |
| 4611 | if (vimdir == NULL || *vimdir == NUL) |
| 4612 | return NULL; |
| 4613 | p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); |
| 4614 | if (p != NULL && mch_isdir(p)) |
| 4615 | return p; |
| 4616 | vim_free(p); |
| 4617 | p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); |
| 4618 | if (p != NULL && mch_isdir(p)) |
| 4619 | return p; |
| 4620 | vim_free(p); |
| 4621 | return NULL; |
| 4622 | } |
| 4623 | |
| 4624 | /* |
| 4625 | * If the string between "p" and "pend" ends in "name/", return "pend" minus |
| 4626 | * the length of "name/". Otherwise return "pend". |
| 4627 | */ |
| 4628 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4629 | remove_tail(char_u *p, char_u *pend, char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4630 | { |
| 4631 | int len = (int)STRLEN(name) + 1; |
| 4632 | char_u *newend = pend - len; |
| 4633 | |
| 4634 | if (newend >= p |
| 4635 | && fnamencmp(newend, name, len - 1) == 0 |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4636 | && (newend == p || after_pathsep(p, newend))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4637 | return newend; |
| 4638 | return pend; |
| 4639 | } |
| 4640 | |
Bram Moolenaar | 137374f | 2018-05-13 15:59:50 +0200 | [diff] [blame] | 4641 | void |
| 4642 | vim_unsetenv(char_u *var) |
| 4643 | { |
| 4644 | #ifdef HAVE_UNSETENV |
| 4645 | unsetenv((char *)var); |
| 4646 | #else |
Bram Moolenaar | 1af6a4b | 2018-05-14 22:58:34 +0200 | [diff] [blame] | 4647 | vim_setenv(var, (char_u *)""); |
Bram Moolenaar | 137374f | 2018-05-13 15:59:50 +0200 | [diff] [blame] | 4648 | #endif |
| 4649 | } |
| 4650 | |
| 4651 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4652 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4653 | * Our portable version of setenv. |
| 4654 | */ |
| 4655 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4656 | vim_setenv(char_u *name, char_u *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4657 | { |
| 4658 | #ifdef HAVE_SETENV |
| 4659 | mch_setenv((char *)name, (char *)val, 1); |
| 4660 | #else |
| 4661 | char_u *envbuf; |
| 4662 | |
| 4663 | /* |
| 4664 | * Putenv does not copy the string, it has to remain |
| 4665 | * valid. The allocated memory will never be freed. |
| 4666 | */ |
| 4667 | envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2)); |
| 4668 | if (envbuf != NULL) |
| 4669 | { |
| 4670 | sprintf((char *)envbuf, "%s=%s", name, val); |
| 4671 | putenv((char *)envbuf); |
| 4672 | } |
| 4673 | #endif |
Bram Moolenaar | 011a34d | 2012-02-29 13:49:09 +0100 | [diff] [blame] | 4674 | #ifdef FEAT_GETTEXT |
| 4675 | /* |
| 4676 | * When setting $VIMRUNTIME adjust the directory to find message |
| 4677 | * translations to $VIMRUNTIME/lang. |
| 4678 | */ |
| 4679 | if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) |
| 4680 | { |
| 4681 | char_u *buf = concat_str(val, (char_u *)"/lang"); |
| 4682 | |
| 4683 | if (buf != NULL) |
| 4684 | { |
| 4685 | bindtextdomain(VIMPACKAGE, (char *)buf); |
| 4686 | vim_free(buf); |
| 4687 | } |
| 4688 | } |
| 4689 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
| 4692 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 4693 | /* |
| 4694 | * Function given to ExpandGeneric() to obtain an environment variable name. |
| 4695 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4696 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4697 | get_env_name( |
| 4698 | expand_T *xp UNUSED, |
| 4699 | int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4700 | { |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 4701 | # if defined(AMIGA) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4702 | /* |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 4703 | * No environ[] on the Amiga. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4704 | */ |
| 4705 | return NULL; |
| 4706 | # else |
| 4707 | # ifndef __WIN32__ |
| 4708 | /* Borland C++ 5.2 has this in a header file. */ |
| 4709 | extern char **environ; |
| 4710 | # endif |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 4711 | # define ENVNAMELEN 100 |
| 4712 | static char_u name[ENVNAMELEN]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4713 | char_u *str; |
| 4714 | int n; |
| 4715 | |
| 4716 | str = (char_u *)environ[idx]; |
| 4717 | if (str == NULL) |
| 4718 | return NULL; |
| 4719 | |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 4720 | for (n = 0; n < ENVNAMELEN - 1; ++n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4721 | { |
| 4722 | if (str[n] == '=' || str[n] == NUL) |
| 4723 | break; |
| 4724 | name[n] = str[n]; |
| 4725 | } |
| 4726 | name[n] = NUL; |
| 4727 | return name; |
| 4728 | # endif |
| 4729 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4730 | |
| 4731 | /* |
| 4732 | * Find all user names for user completion. |
| 4733 | * Done only once and then cached. |
| 4734 | */ |
| 4735 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4736 | init_users(void) |
Bram Moolenaar | 01b626c | 2013-06-16 22:49:14 +0200 | [diff] [blame] | 4737 | { |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4738 | static int lazy_init_done = FALSE; |
| 4739 | |
| 4740 | if (lazy_init_done) |
| 4741 | return; |
| 4742 | |
| 4743 | lazy_init_done = TRUE; |
| 4744 | ga_init2(&ga_users, sizeof(char_u *), 20); |
| 4745 | |
| 4746 | # if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H) |
| 4747 | { |
| 4748 | char_u* user; |
| 4749 | struct passwd* pw; |
| 4750 | |
| 4751 | setpwent(); |
| 4752 | while ((pw = getpwent()) != NULL) |
| 4753 | /* pw->pw_name shouldn't be NULL but just in case... */ |
| 4754 | if (pw->pw_name != NULL) |
| 4755 | { |
| 4756 | if (ga_grow(&ga_users, 1) == FAIL) |
| 4757 | break; |
| 4758 | user = vim_strsave((char_u*)pw->pw_name); |
| 4759 | if (user == NULL) |
| 4760 | break; |
| 4761 | ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user; |
| 4762 | } |
| 4763 | endpwent(); |
| 4764 | } |
Bram Moolenaar | 828c3d7 | 2018-06-19 18:58:07 +0200 | [diff] [blame] | 4765 | # elif defined(WIN3264) |
| 4766 | { |
| 4767 | char_u* user; |
| 4768 | DWORD nusers = 0, ntotal = 0, i; |
| 4769 | PUSER_INFO_0 uinfo; |
| 4770 | |
| 4771 | if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH, |
| 4772 | &nusers, &ntotal, NULL) == NERR_Success) |
| 4773 | { |
| 4774 | for (i = 0; i < nusers; i++) |
| 4775 | { |
| 4776 | if (ga_grow(&ga_users, 1) == FAIL) |
| 4777 | break; |
| 4778 | user = utf16_to_enc(uinfo[i].usri0_name, NULL); |
| 4779 | if (user == NULL) |
| 4780 | break; |
| 4781 | ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user; |
| 4782 | } |
| 4783 | |
| 4784 | NetApiBufferFree(uinfo); |
| 4785 | } |
| 4786 | } |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4787 | # endif |
| 4788 | } |
| 4789 | |
| 4790 | /* |
| 4791 | * Function given to ExpandGeneric() to obtain an user names. |
| 4792 | */ |
| 4793 | char_u* |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4794 | get_users(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4795 | { |
| 4796 | init_users(); |
| 4797 | if (idx < ga_users.ga_len) |
| 4798 | return ((char_u **)ga_users.ga_data)[idx]; |
| 4799 | return NULL; |
| 4800 | } |
| 4801 | |
| 4802 | /* |
| 4803 | * Check whether name matches a user name. Return: |
| 4804 | * 0 if name does not match any user name. |
| 4805 | * 1 if name partially matches the beginning of a user name. |
| 4806 | * 2 is name fully matches a user name. |
| 4807 | */ |
Bram Moolenaar | 6c5d104 | 2018-07-07 16:41:13 +0200 | [diff] [blame] | 4808 | int |
| 4809 | match_user(char_u *name) |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4810 | { |
| 4811 | int i; |
| 4812 | int n = (int)STRLEN(name); |
| 4813 | int result = 0; |
| 4814 | |
| 4815 | init_users(); |
| 4816 | for (i = 0; i < ga_users.ga_len; i++) |
| 4817 | { |
| 4818 | if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0) |
| 4819 | return 2; /* full match */ |
| 4820 | if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0) |
| 4821 | result = 1; /* partial match */ |
| 4822 | } |
| 4823 | return result; |
| 4824 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4825 | #endif |
| 4826 | |
| 4827 | /* |
| 4828 | * Replace home directory by "~" in each space or comma separated file name in |
| 4829 | * 'src'. |
| 4830 | * If anything fails (except when out of space) dst equals src. |
| 4831 | */ |
| 4832 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4833 | home_replace( |
| 4834 | buf_T *buf, /* when not NULL, check for help files */ |
| 4835 | char_u *src, /* input file name */ |
| 4836 | char_u *dst, /* where to put the result */ |
| 4837 | int dstlen, /* maximum length of the result */ |
| 4838 | int one) /* if TRUE, only replace one file name, include |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4839 | spaces and commas in the file name. */ |
| 4840 | { |
| 4841 | size_t dirlen = 0, envlen = 0; |
| 4842 | size_t len; |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4843 | char_u *homedir_env, *homedir_env_orig; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4844 | char_u *p; |
| 4845 | |
| 4846 | if (src == NULL) |
| 4847 | { |
| 4848 | *dst = NUL; |
| 4849 | return; |
| 4850 | } |
| 4851 | |
| 4852 | /* |
| 4853 | * If the file is a help file, remove the path completely. |
| 4854 | */ |
| 4855 | if (buf != NULL && buf->b_help) |
| 4856 | { |
Bram Moolenaar | 0af2d32 | 2017-08-05 23:00:53 +0200 | [diff] [blame] | 4857 | vim_snprintf((char *)dst, dstlen, "%s", gettail(src)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4858 | return; |
| 4859 | } |
| 4860 | |
| 4861 | /* |
| 4862 | * We check both the value of the $HOME environment variable and the |
| 4863 | * "real" home directory. |
| 4864 | */ |
| 4865 | if (homedir != NULL) |
| 4866 | dirlen = STRLEN(homedir); |
| 4867 | |
| 4868 | #ifdef VMS |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4869 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4870 | #else |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4871 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); |
| 4872 | #endif |
Bram Moolenaar | 48340b6 | 2017-08-29 22:08:53 +0200 | [diff] [blame] | 4873 | #ifdef WIN3264 |
| 4874 | if (homedir_env == NULL) |
| 4875 | homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE"); |
| 4876 | #endif |
Bram Moolenaar | bef4790 | 2012-07-06 16:49:40 +0200 | [diff] [blame] | 4877 | /* Empty is the same as not set. */ |
| 4878 | if (homedir_env != NULL && *homedir_env == NUL) |
| 4879 | homedir_env = NULL; |
| 4880 | |
Bram Moolenaar | e60c2e5 | 2013-06-05 19:35:38 +0200 | [diff] [blame] | 4881 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) |
Bram Moolenaar | bef4790 | 2012-07-06 16:49:40 +0200 | [diff] [blame] | 4882 | if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL) |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4883 | { |
| 4884 | int usedlen = 0; |
| 4885 | int flen; |
| 4886 | char_u *fbuf = NULL; |
| 4887 | |
| 4888 | flen = (int)STRLEN(homedir_env); |
Bram Moolenaar | 00136dc | 2018-07-25 21:19:13 +0200 | [diff] [blame] | 4889 | (void)modify_fname((char_u *)":p", FALSE, &usedlen, |
Bram Moolenaar | d12f811 | 2012-06-20 17:56:09 +0200 | [diff] [blame] | 4890 | &homedir_env, &fbuf, &flen); |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4891 | flen = (int)STRLEN(homedir_env); |
| 4892 | if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) |
| 4893 | /* Remove the trailing / that is added to a directory. */ |
| 4894 | homedir_env[flen - 1] = NUL; |
| 4895 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4896 | #endif |
| 4897 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4898 | if (homedir_env != NULL) |
| 4899 | envlen = STRLEN(homedir_env); |
| 4900 | |
| 4901 | if (!one) |
| 4902 | src = skipwhite(src); |
| 4903 | while (*src && dstlen > 0) |
| 4904 | { |
| 4905 | /* |
| 4906 | * Here we are at the beginning of a file name. |
| 4907 | * First, check to see if the beginning of the file name matches |
| 4908 | * $HOME or the "real" home directory. Check that there is a '/' |
| 4909 | * after the match (so that if e.g. the file is "/home/pieter/bla", |
| 4910 | * and the home directory is "/home/piet", the file does not end up |
| 4911 | * as "~er/bla" (which would seem to indicate the file "bla" in user |
| 4912 | * er's home directory)). |
| 4913 | */ |
| 4914 | p = homedir; |
| 4915 | len = dirlen; |
| 4916 | for (;;) |
| 4917 | { |
| 4918 | if ( len |
| 4919 | && fnamencmp(src, p, len) == 0 |
| 4920 | && (vim_ispathsep(src[len]) |
| 4921 | || (!one && (src[len] == ',' || src[len] == ' ')) |
| 4922 | || src[len] == NUL)) |
| 4923 | { |
| 4924 | src += len; |
| 4925 | if (--dstlen > 0) |
| 4926 | *dst++ = '~'; |
| 4927 | |
| 4928 | /* |
| 4929 | * If it's just the home directory, add "/". |
| 4930 | */ |
| 4931 | if (!vim_ispathsep(src[0]) && --dstlen > 0) |
| 4932 | *dst++ = '/'; |
| 4933 | break; |
| 4934 | } |
| 4935 | if (p == homedir_env) |
| 4936 | break; |
| 4937 | p = homedir_env; |
| 4938 | len = envlen; |
| 4939 | } |
| 4940 | |
| 4941 | /* if (!one) skip to separator: space or comma */ |
| 4942 | while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) |
| 4943 | *dst++ = *src++; |
| 4944 | /* skip separator */ |
| 4945 | while ((*src == ' ' || *src == ',') && --dstlen > 0) |
| 4946 | *dst++ = *src++; |
| 4947 | } |
| 4948 | /* if (dstlen == 0) out of space, what to do??? */ |
| 4949 | |
| 4950 | *dst = NUL; |
Bram Moolenaar | 9158f9e | 2012-06-20 14:02:27 +0200 | [diff] [blame] | 4951 | |
| 4952 | if (homedir_env != homedir_env_orig) |
| 4953 | vim_free(homedir_env); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4954 | } |
| 4955 | |
| 4956 | /* |
| 4957 | * Like home_replace, store the replaced string in allocated memory. |
| 4958 | * When something fails, NULL is returned. |
| 4959 | */ |
| 4960 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4961 | home_replace_save( |
| 4962 | buf_T *buf, /* when not NULL, check for help files */ |
| 4963 | char_u *src) /* input file name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4964 | { |
| 4965 | char_u *dst; |
| 4966 | unsigned len; |
| 4967 | |
| 4968 | len = 3; /* space for "~/" and trailing NUL */ |
| 4969 | if (src != NULL) /* just in case */ |
| 4970 | len += (unsigned)STRLEN(src); |
| 4971 | dst = alloc(len); |
| 4972 | if (dst != NULL) |
| 4973 | home_replace(buf, src, dst, len, TRUE); |
| 4974 | return dst; |
| 4975 | } |
| 4976 | |
| 4977 | /* |
| 4978 | * Compare two file names and return: |
| 4979 | * FPC_SAME if they both exist and are the same file. |
| 4980 | * FPC_SAMEX if they both don't exist and have the same file name. |
| 4981 | * FPC_DIFF if they both exist and are different files. |
| 4982 | * FPC_NOTX if they both don't exist. |
| 4983 | * FPC_DIFFX if one of them doesn't exist. |
| 4984 | * For the first name environment variables are expanded |
| 4985 | */ |
| 4986 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 4987 | fullpathcmp( |
| 4988 | char_u *s1, |
| 4989 | char_u *s2, |
| 4990 | int checkname) /* when both don't exist, check file names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4991 | { |
| 4992 | #ifdef UNIX |
| 4993 | char_u exp1[MAXPATHL]; |
| 4994 | char_u full1[MAXPATHL]; |
| 4995 | char_u full2[MAXPATHL]; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 4996 | stat_T st1, st2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4997 | int r1, r2; |
| 4998 | |
| 4999 | expand_env(s1, exp1, MAXPATHL); |
| 5000 | r1 = mch_stat((char *)exp1, &st1); |
| 5001 | r2 = mch_stat((char *)s2, &st2); |
| 5002 | if (r1 != 0 && r2 != 0) |
| 5003 | { |
| 5004 | /* if mch_stat() doesn't work, may compare the names */ |
| 5005 | if (checkname) |
| 5006 | { |
| 5007 | if (fnamecmp(exp1, s2) == 0) |
| 5008 | return FPC_SAMEX; |
| 5009 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 5010 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 5011 | if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) |
| 5012 | return FPC_SAMEX; |
| 5013 | } |
| 5014 | return FPC_NOTX; |
| 5015 | } |
| 5016 | if (r1 != 0 || r2 != 0) |
| 5017 | return FPC_DIFFX; |
| 5018 | if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) |
| 5019 | return FPC_SAME; |
| 5020 | return FPC_DIFF; |
| 5021 | #else |
| 5022 | char_u *exp1; /* expanded s1 */ |
| 5023 | char_u *full1; /* full path of s1 */ |
| 5024 | char_u *full2; /* full path of s2 */ |
| 5025 | int retval = FPC_DIFF; |
| 5026 | int r1, r2; |
| 5027 | |
| 5028 | /* allocate one buffer to store three paths (alloc()/free() is slow!) */ |
| 5029 | if ((exp1 = alloc(MAXPATHL * 3)) != NULL) |
| 5030 | { |
| 5031 | full1 = exp1 + MAXPATHL; |
| 5032 | full2 = full1 + MAXPATHL; |
| 5033 | |
| 5034 | expand_env(s1, exp1, MAXPATHL); |
| 5035 | r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); |
| 5036 | r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); |
| 5037 | |
| 5038 | /* If vim_FullName() fails, the file probably doesn't exist. */ |
| 5039 | if (r1 != OK && r2 != OK) |
| 5040 | { |
| 5041 | if (checkname && fnamecmp(exp1, s2) == 0) |
| 5042 | retval = FPC_SAMEX; |
| 5043 | else |
| 5044 | retval = FPC_NOTX; |
| 5045 | } |
| 5046 | else if (r1 != OK || r2 != OK) |
| 5047 | retval = FPC_DIFFX; |
| 5048 | else if (fnamecmp(full1, full2)) |
| 5049 | retval = FPC_DIFF; |
| 5050 | else |
| 5051 | retval = FPC_SAME; |
| 5052 | vim_free(exp1); |
| 5053 | } |
| 5054 | return retval; |
| 5055 | #endif |
| 5056 | } |
| 5057 | |
| 5058 | /* |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5059 | * Get the tail of a path: the file name. |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 5060 | * 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] | 5061 | * Fail safe: never returns NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5062 | */ |
| 5063 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5064 | gettail(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5065 | { |
| 5066 | char_u *p1, *p2; |
| 5067 | |
| 5068 | if (fname == NULL) |
| 5069 | return (char_u *)""; |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5070 | 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] | 5071 | { |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5072 | if (vim_ispathsep_nocolon(*p2)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5073 | p1 = p2 + 1; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5074 | MB_PTR_ADV(p2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5075 | } |
| 5076 | return p1; |
| 5077 | } |
| 5078 | |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 5079 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5080 | static char_u *gettail_dir(char_u *fname); |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 5081 | |
| 5082 | /* |
| 5083 | * Return the end of the directory name, on the first path |
| 5084 | * separator: |
| 5085 | * "/path/file", "/path/dir/", "/path//dir", "/file" |
| 5086 | * ^ ^ ^ ^ |
| 5087 | */ |
| 5088 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5089 | gettail_dir(char_u *fname) |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 5090 | { |
| 5091 | char_u *dir_end = fname; |
| 5092 | char_u *next_dir_end = fname; |
| 5093 | int look_for_sep = TRUE; |
| 5094 | char_u *p; |
| 5095 | |
| 5096 | for (p = fname; *p != NUL; ) |
| 5097 | { |
| 5098 | if (vim_ispathsep(*p)) |
| 5099 | { |
| 5100 | if (look_for_sep) |
| 5101 | { |
| 5102 | next_dir_end = p; |
| 5103 | look_for_sep = FALSE; |
| 5104 | } |
| 5105 | } |
| 5106 | else |
| 5107 | { |
| 5108 | if (!look_for_sep) |
| 5109 | dir_end = next_dir_end; |
| 5110 | look_for_sep = TRUE; |
| 5111 | } |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5112 | MB_PTR_ADV(p); |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 5113 | } |
| 5114 | return dir_end; |
| 5115 | } |
| 5116 | #endif |
| 5117 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5118 | /* |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5119 | * Get pointer to tail of "fname", including path separators. Putting a NUL |
| 5120 | * here leaves the directory name. Takes care of "c:/" and "//". |
| 5121 | * Always returns a valid pointer. |
| 5122 | */ |
| 5123 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5124 | gettail_sep(char_u *fname) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5125 | { |
| 5126 | char_u *p; |
| 5127 | char_u *t; |
| 5128 | |
| 5129 | p = get_past_head(fname); /* don't remove the '/' from "c:/file" */ |
| 5130 | t = gettail(fname); |
| 5131 | while (t > p && after_pathsep(fname, t)) |
| 5132 | --t; |
| 5133 | #ifdef VMS |
| 5134 | /* path separator is part of the path */ |
| 5135 | ++t; |
| 5136 | #endif |
| 5137 | return t; |
| 5138 | } |
| 5139 | |
| 5140 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5141 | * get the next path component (just after the next path separator). |
| 5142 | */ |
| 5143 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5144 | getnextcomp(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5145 | { |
| 5146 | while (*fname && !vim_ispathsep(*fname)) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5147 | MB_PTR_ADV(fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5148 | if (*fname) |
| 5149 | ++fname; |
| 5150 | return fname; |
| 5151 | } |
| 5152 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5153 | /* |
| 5154 | * Get a pointer to one character past the head of a path name. |
| 5155 | * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. |
| 5156 | * If there is no head, path is returned. |
| 5157 | */ |
| 5158 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5159 | get_past_head(char_u *path) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5160 | { |
| 5161 | char_u *retval; |
| 5162 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5163 | #if defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5164 | /* may skip "c:" */ |
| 5165 | if (isalpha(path[0]) && path[1] == ':') |
| 5166 | retval = path + 2; |
| 5167 | else |
| 5168 | retval = path; |
| 5169 | #else |
| 5170 | # if defined(AMIGA) |
| 5171 | /* may skip "label:" */ |
| 5172 | retval = vim_strchr(path, ':'); |
| 5173 | if (retval == NULL) |
| 5174 | retval = path; |
| 5175 | # else /* Unix */ |
| 5176 | retval = path; |
| 5177 | # endif |
| 5178 | #endif |
| 5179 | |
| 5180 | while (vim_ispathsep(*retval)) |
| 5181 | ++retval; |
| 5182 | |
| 5183 | return retval; |
| 5184 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5185 | |
| 5186 | /* |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5187 | * Return TRUE if 'c' is a path separator. |
| 5188 | * Note that for MS-Windows this includes the colon. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5189 | */ |
| 5190 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5191 | vim_ispathsep(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5192 | { |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5193 | #ifdef UNIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5194 | return (c == '/'); /* UNIX has ':' inside file names */ |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5195 | #else |
| 5196 | # ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5197 | return (c == ':' || c == '/' || c == '\\'); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5198 | # else |
| 5199 | # ifdef VMS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5200 | /* server"user passwd"::device:[full.path.name]fname.extension;version" */ |
| 5201 | return (c == ':' || c == '[' || c == ']' || c == '/' |
| 5202 | || c == '<' || c == '>' || c == '"' ); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5203 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5204 | return (c == ':' || c == '/'); |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5205 | # endif /* VMS */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5206 | # endif |
Bram Moolenaar | e60acc1 | 2011-05-10 16:41:25 +0200 | [diff] [blame] | 5207 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5208 | } |
| 5209 | |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5210 | /* |
| 5211 | * Like vim_ispathsep(c), but exclude the colon for MS-Windows. |
| 5212 | */ |
| 5213 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5214 | vim_ispathsep_nocolon(int c) |
Bram Moolenaar | 69c3500 | 2013-11-04 02:54:12 +0100 | [diff] [blame] | 5215 | { |
| 5216 | return vim_ispathsep(c) |
| 5217 | #ifdef BACKSLASH_IN_FILENAME |
| 5218 | && c != ':' |
| 5219 | #endif |
| 5220 | ; |
| 5221 | } |
| 5222 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5223 | #if defined(FEAT_SEARCHPATH) || defined(PROTO) |
| 5224 | /* |
| 5225 | * return TRUE if 'c' is a path list separator. |
| 5226 | */ |
| 5227 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5228 | vim_ispathlistsep(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5229 | { |
| 5230 | #ifdef UNIX |
| 5231 | return (c == ':'); |
| 5232 | #else |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 5233 | return (c == ';'); /* might not be right for every system... */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5234 | #endif |
| 5235 | } |
| 5236 | #endif |
| 5237 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5238 | /* |
| 5239 | * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" |
| 5240 | * It's done in-place. |
| 5241 | */ |
| 5242 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5243 | shorten_dir(char_u *str) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5244 | { |
| 5245 | char_u *tail, *s, *d; |
| 5246 | int skip = FALSE; |
| 5247 | |
| 5248 | tail = gettail(str); |
| 5249 | d = str; |
| 5250 | for (s = str; ; ++s) |
| 5251 | { |
| 5252 | if (s >= tail) /* copy the whole tail */ |
| 5253 | { |
| 5254 | *d++ = *s; |
| 5255 | if (*s == NUL) |
| 5256 | break; |
| 5257 | } |
| 5258 | else if (vim_ispathsep(*s)) /* copy '/' and next char */ |
| 5259 | { |
| 5260 | *d++ = *s; |
| 5261 | skip = FALSE; |
| 5262 | } |
| 5263 | else if (!skip) |
| 5264 | { |
| 5265 | *d++ = *s; /* copy next char */ |
| 5266 | if (*s != '~' && *s != '.') /* and leading "~" and "." */ |
| 5267 | skip = TRUE; |
| 5268 | # ifdef FEAT_MBYTE |
| 5269 | if (has_mbyte) |
| 5270 | { |
| 5271 | int l = mb_ptr2len(s); |
| 5272 | |
| 5273 | while (--l > 0) |
Bram Moolenaar | b6baca5 | 2006-08-15 20:24:14 +0000 | [diff] [blame] | 5274 | *d++ = *++s; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5275 | } |
| 5276 | # endif |
| 5277 | } |
| 5278 | } |
| 5279 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5280 | |
Bram Moolenaar | 900b4d7 | 2005-12-12 22:05:50 +0000 | [diff] [blame] | 5281 | /* |
| 5282 | * Return TRUE if the directory of "fname" exists, FALSE otherwise. |
| 5283 | * Also returns TRUE if there is no directory name. |
| 5284 | * "fname" must be writable!. |
| 5285 | */ |
| 5286 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5287 | dir_of_file_exists(char_u *fname) |
Bram Moolenaar | 900b4d7 | 2005-12-12 22:05:50 +0000 | [diff] [blame] | 5288 | { |
| 5289 | char_u *p; |
| 5290 | int c; |
| 5291 | int retval; |
| 5292 | |
| 5293 | p = gettail_sep(fname); |
| 5294 | if (p == fname) |
| 5295 | return TRUE; |
| 5296 | c = *p; |
| 5297 | *p = NUL; |
| 5298 | retval = mch_isdir(fname); |
| 5299 | *p = c; |
| 5300 | return retval; |
| 5301 | } |
| 5302 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5303 | /* |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5304 | * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally |
| 5305 | * and deal with 'fileignorecase'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5306 | */ |
| 5307 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5308 | vim_fnamecmp(char_u *x, char_u *y) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5309 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5310 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5311 | return vim_fnamencmp(x, y, MAXPATHL); |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5312 | #else |
| 5313 | if (p_fic) |
| 5314 | return MB_STRICMP(x, y); |
| 5315 | return STRCMP(x, y); |
| 5316 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5317 | } |
| 5318 | |
| 5319 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5320 | vim_fnamencmp(char_u *x, char_u *y, size_t len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5321 | { |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5322 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5323 | char_u *px = x; |
| 5324 | char_u *py = y; |
| 5325 | int cx = NUL; |
| 5326 | int cy = NUL; |
| 5327 | |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5328 | while (len > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5329 | { |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5330 | cx = PTR2CHAR(px); |
| 5331 | cy = PTR2CHAR(py); |
| 5332 | if (cx == NUL || cy == NUL |
| 5333 | || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy) |
| 5334 | && !(cx == '/' && cy == '\\') |
| 5335 | && !(cx == '\\' && cy == '/'))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5336 | break; |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5337 | len -= MB_PTR2LEN(px); |
| 5338 | px += MB_PTR2LEN(px); |
| 5339 | py += MB_PTR2LEN(py); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5340 | } |
| 5341 | if (len == 0) |
| 5342 | return 0; |
Bram Moolenaar | d0e2d94 | 2013-03-19 18:31:49 +0100 | [diff] [blame] | 5343 | return (cx - cy); |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5344 | #else |
| 5345 | if (p_fic) |
| 5346 | return MB_STRNICMP(x, y, len); |
| 5347 | return STRNCMP(x, y, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5348 | #endif |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 5349 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5350 | |
| 5351 | /* |
| 5352 | * Concatenate file names fname1 and fname2 into allocated memory. |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 5353 | * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5354 | */ |
| 5355 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5356 | concat_fnames(char_u *fname1, char_u *fname2, int sep) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5357 | { |
| 5358 | char_u *dest; |
| 5359 | |
| 5360 | dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3)); |
| 5361 | if (dest != NULL) |
| 5362 | { |
| 5363 | STRCPY(dest, fname1); |
| 5364 | if (sep) |
| 5365 | add_pathsep(dest); |
| 5366 | STRCAT(dest, fname2); |
| 5367 | } |
| 5368 | return dest; |
| 5369 | } |
| 5370 | |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5371 | /* |
| 5372 | * Concatenate two strings and return the result in allocated memory. |
| 5373 | * Returns NULL when out of memory. |
| 5374 | */ |
| 5375 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5376 | concat_str(char_u *str1, char_u *str2) |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5377 | { |
| 5378 | char_u *dest; |
| 5379 | size_t l = STRLEN(str1); |
| 5380 | |
| 5381 | dest = alloc((unsigned)(l + STRLEN(str2) + 1L)); |
| 5382 | if (dest != NULL) |
| 5383 | { |
| 5384 | STRCPY(dest, str1); |
| 5385 | STRCPY(dest + l, str2); |
| 5386 | } |
| 5387 | return dest; |
| 5388 | } |
Bram Moolenaar | d675464 | 2005-01-17 22:18:45 +0000 | [diff] [blame] | 5389 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5390 | /* |
| 5391 | * Add a path separator to a file name, unless it already ends in a path |
| 5392 | * separator. |
| 5393 | */ |
| 5394 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5395 | add_pathsep(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5396 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5397 | if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5398 | STRCAT(p, PATHSEPSTR); |
| 5399 | } |
| 5400 | |
| 5401 | /* |
| 5402 | * FullName_save - Make an allocated copy of a full file name. |
| 5403 | * Returns NULL when out of memory. |
| 5404 | */ |
| 5405 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5406 | FullName_save( |
| 5407 | char_u *fname, |
| 5408 | int force) /* force expansion, even when it already looks |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 5409 | * like a full path name */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5410 | { |
| 5411 | char_u *buf; |
| 5412 | char_u *new_fname = NULL; |
| 5413 | |
| 5414 | if (fname == NULL) |
| 5415 | return NULL; |
| 5416 | |
| 5417 | buf = alloc((unsigned)MAXPATHL); |
| 5418 | if (buf != NULL) |
| 5419 | { |
| 5420 | if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) |
| 5421 | new_fname = vim_strsave(buf); |
| 5422 | else |
| 5423 | new_fname = vim_strsave(fname); |
| 5424 | vim_free(buf); |
| 5425 | } |
| 5426 | return new_fname; |
| 5427 | } |
| 5428 | |
| 5429 | #if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL) |
| 5430 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5431 | static char_u *skip_string(char_u *p); |
| 5432 | static pos_T *ind_find_start_comment(void); |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5433 | static pos_T *ind_find_start_CORS(linenr_T *is_raw); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5434 | static pos_T *find_start_rawstring(int ind_maxcomment); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5435 | |
| 5436 | /* |
| 5437 | * Find the start of a comment, not knowing if we are in a comment right now. |
| 5438 | * Search starts at w_cursor.lnum and goes backwards. |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5439 | * Return NULL when not inside a comment. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5440 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 5441 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5442 | ind_find_start_comment(void) /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 5443 | { |
| 5444 | return find_start_comment(curbuf->b_ind_maxcomment); |
| 5445 | } |
| 5446 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5447 | pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5448 | find_start_comment(int ind_maxcomment) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5449 | { |
| 5450 | pos_T *pos; |
| 5451 | char_u *line; |
| 5452 | char_u *p; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5453 | int cur_maxcomment = ind_maxcomment; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5454 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5455 | for (;;) |
| 5456 | { |
| 5457 | pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment); |
| 5458 | if (pos == NULL) |
| 5459 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5460 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5461 | /* |
| 5462 | * Check if the comment start we found is inside a string. |
| 5463 | * If it is then restrict the search to below this line and try again. |
| 5464 | */ |
| 5465 | line = ml_get(pos->lnum); |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 5466 | for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5467 | p = skip_string(p); |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 5468 | if ((colnr_T)(p - line) <= pos->col) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 5469 | break; |
| 5470 | cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; |
| 5471 | if (cur_maxcomment <= 0) |
| 5472 | { |
| 5473 | pos = NULL; |
| 5474 | break; |
| 5475 | } |
| 5476 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5477 | return pos; |
| 5478 | } |
| 5479 | |
| 5480 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5481 | * Find the start of a comment or raw string, not knowing if we are in a |
| 5482 | * comment or raw string right now. |
| 5483 | * Search starts at w_cursor.lnum and goes backwards. |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5484 | * 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] | 5485 | * Return NULL when not inside a comment or raw string. |
| 5486 | * "CORS" -> Comment Or Raw String |
| 5487 | */ |
| 5488 | static pos_T * |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5489 | ind_find_start_CORS(linenr_T *is_raw) /* XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5490 | { |
Bram Moolenaar | 089af18 | 2015-10-07 11:41:49 +0200 | [diff] [blame] | 5491 | static pos_T comment_pos_copy; |
| 5492 | pos_T *comment_pos; |
| 5493 | pos_T *rs_pos; |
| 5494 | |
| 5495 | comment_pos = find_start_comment(curbuf->b_ind_maxcomment); |
| 5496 | if (comment_pos != NULL) |
| 5497 | { |
| 5498 | /* Need to make a copy of the static pos in findmatchlimit(), |
| 5499 | * calling find_start_rawstring() may change it. */ |
| 5500 | comment_pos_copy = *comment_pos; |
| 5501 | comment_pos = &comment_pos_copy; |
| 5502 | } |
| 5503 | rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment); |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5504 | |
| 5505 | /* If comment_pos is before rs_pos the raw string is inside the comment. |
| 5506 | * 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] | 5507 | if (comment_pos == NULL || (rs_pos != NULL |
| 5508 | && LT_POS(*rs_pos, *comment_pos))) |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5509 | { |
| 5510 | if (is_raw != NULL && rs_pos != NULL) |
| 5511 | *is_raw = rs_pos->lnum; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5512 | return rs_pos; |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5513 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5514 | return comment_pos; |
| 5515 | } |
| 5516 | |
| 5517 | /* |
| 5518 | * Find the start of a raw string, not knowing if we are in one right now. |
| 5519 | * Search starts at w_cursor.lnum and goes backwards. |
| 5520 | * Return NULL when not inside a raw string. |
| 5521 | */ |
| 5522 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5523 | find_start_rawstring(int ind_maxcomment) /* XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5524 | { |
| 5525 | pos_T *pos; |
| 5526 | char_u *line; |
| 5527 | char_u *p; |
| 5528 | int cur_maxcomment = ind_maxcomment; |
| 5529 | |
| 5530 | for (;;) |
| 5531 | { |
| 5532 | pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment); |
| 5533 | if (pos == NULL) |
| 5534 | break; |
| 5535 | |
| 5536 | /* |
| 5537 | * Check if the raw string start we found is inside a string. |
| 5538 | * If it is then restrict the search to below this line and try again. |
| 5539 | */ |
| 5540 | line = ml_get(pos->lnum); |
| 5541 | for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) |
| 5542 | p = skip_string(p); |
| 5543 | if ((colnr_T)(p - line) <= pos->col) |
| 5544 | break; |
| 5545 | cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; |
| 5546 | if (cur_maxcomment <= 0) |
| 5547 | { |
| 5548 | pos = NULL; |
| 5549 | break; |
| 5550 | } |
| 5551 | } |
| 5552 | return pos; |
| 5553 | } |
| 5554 | |
| 5555 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5556 | * Skip to the end of a "string" and a 'c' character. |
| 5557 | * If there is no string or character, return argument unmodified. |
| 5558 | */ |
| 5559 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5560 | skip_string(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5561 | { |
| 5562 | int i; |
| 5563 | |
| 5564 | /* |
| 5565 | * We loop, because strings may be concatenated: "date""time". |
| 5566 | */ |
| 5567 | for ( ; ; ++p) |
| 5568 | { |
| 5569 | if (p[0] == '\'') /* 'c' or '\n' or '\000' */ |
| 5570 | { |
| 5571 | if (!p[1]) /* ' at end of line */ |
| 5572 | break; |
| 5573 | i = 2; |
| 5574 | if (p[1] == '\\') /* '\n' or '\000' */ |
| 5575 | { |
| 5576 | ++i; |
| 5577 | while (vim_isdigit(p[i - 1])) /* '\000' */ |
| 5578 | ++i; |
| 5579 | } |
| 5580 | if (p[i] == '\'') /* check for trailing ' */ |
| 5581 | { |
| 5582 | p += i; |
| 5583 | continue; |
| 5584 | } |
| 5585 | } |
| 5586 | else if (p[0] == '"') /* start of string */ |
| 5587 | { |
| 5588 | for (++p; p[0]; ++p) |
| 5589 | { |
| 5590 | if (p[0] == '\\' && p[1] != NUL) |
| 5591 | ++p; |
| 5592 | else if (p[0] == '"') /* end of string */ |
| 5593 | break; |
| 5594 | } |
| 5595 | if (p[0] == '"') |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5596 | continue; /* continue for another string */ |
| 5597 | } |
| 5598 | else if (p[0] == 'R' && p[1] == '"') |
| 5599 | { |
| 5600 | /* Raw string: R"[delim](...)[delim]" */ |
| 5601 | char_u *delim = p + 2; |
| 5602 | char_u *paren = vim_strchr(delim, '('); |
| 5603 | |
| 5604 | if (paren != NULL) |
| 5605 | { |
| 5606 | size_t delim_len = paren - delim; |
| 5607 | |
| 5608 | for (p += 3; *p; ++p) |
| 5609 | if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0 |
| 5610 | && p[delim_len + 1] == '"') |
| 5611 | { |
| 5612 | p += delim_len + 1; |
| 5613 | break; |
| 5614 | } |
| 5615 | if (p[0] == '"') |
| 5616 | continue; /* continue for another string */ |
| 5617 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5618 | } |
| 5619 | break; /* no string found */ |
| 5620 | } |
| 5621 | if (!*p) |
| 5622 | --p; /* backup from NUL */ |
| 5623 | return p; |
| 5624 | } |
| 5625 | #endif /* FEAT_CINDENT || FEAT_SYN_HL */ |
| 5626 | |
| 5627 | #if defined(FEAT_CINDENT) || defined(PROTO) |
| 5628 | |
| 5629 | /* |
| 5630 | * Do C or expression indenting on the current line. |
| 5631 | */ |
| 5632 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5633 | do_c_expr_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5634 | { |
| 5635 | # ifdef FEAT_EVAL |
| 5636 | if (*curbuf->b_p_inde != NUL) |
| 5637 | fixthisline(get_expr_indent); |
| 5638 | else |
| 5639 | # endif |
| 5640 | fixthisline(get_c_indent); |
| 5641 | } |
| 5642 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 5643 | /* Find result cache for cpp_baseclass */ |
| 5644 | typedef struct { |
| 5645 | int found; |
| 5646 | lpos_T lpos; |
| 5647 | } cpp_baseclass_cache_T; |
| 5648 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5649 | /* |
| 5650 | * Functions for C-indenting. |
| 5651 | * Most of this originally comes from Eric Fischer. |
| 5652 | */ |
| 5653 | /* |
| 5654 | * Below "XXX" means that this function may unlock the current line. |
| 5655 | */ |
| 5656 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5657 | static char_u *cin_skipcomment(char_u *); |
| 5658 | static int cin_nocode(char_u *); |
| 5659 | static pos_T *find_line_comment(void); |
| 5660 | static int cin_has_js_key(char_u *text); |
| 5661 | static int cin_islabel_skip(char_u **); |
| 5662 | static int cin_isdefault(char_u *); |
| 5663 | static char_u *after_label(char_u *l); |
| 5664 | static int get_indent_nolabel(linenr_T lnum); |
| 5665 | static int skip_label(linenr_T, char_u **pp); |
| 5666 | static int cin_first_id_amount(void); |
| 5667 | static int cin_get_equal_amount(linenr_T lnum); |
| 5668 | static int cin_ispreproc(char_u *); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 5669 | static int cin_iscomment(char_u *); |
| 5670 | static int cin_islinecomment(char_u *); |
| 5671 | static int cin_isterminated(char_u *, int, int); |
| 5672 | static int cin_isinit(void); |
| 5673 | static int cin_isfuncdecl(char_u **, linenr_T, linenr_T); |
| 5674 | static int cin_isif(char_u *); |
| 5675 | static int cin_iselse(char_u *); |
| 5676 | static int cin_isdo(char_u *); |
| 5677 | static int cin_iswhileofdo(char_u *, linenr_T); |
| 5678 | static int cin_is_if_for_while_before_offset(char_u *line, int *poffset); |
| 5679 | static int cin_iswhileofdo_end(int terminated); |
| 5680 | static int cin_isbreak(char_u *); |
| 5681 | static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached); |
| 5682 | static int get_baseclass_amount(int col); |
| 5683 | static int cin_ends_in(char_u *, char_u *, char_u *); |
| 5684 | static int cin_starts_with(char_u *s, char *word); |
| 5685 | static int cin_skip2pos(pos_T *trypos); |
| 5686 | static pos_T *find_start_brace(void); |
| 5687 | static pos_T *find_match_paren(int); |
| 5688 | static pos_T *find_match_char(int c, int ind_maxparen); |
| 5689 | static int corr_ind_maxparen(pos_T *startpos); |
| 5690 | static int find_last_paren(char_u *l, int start, int end); |
| 5691 | static int find_match(int lookfor, linenr_T ourscope); |
| 5692 | static int cin_is_cpp_namespace(char_u *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5693 | |
| 5694 | /* |
| 5695 | * Skip over white space and C comments within the line. |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5696 | * Also skip over Perl/shell comments if desired. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5697 | */ |
| 5698 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5699 | cin_skipcomment(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5700 | { |
| 5701 | while (*s) |
| 5702 | { |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5703 | char_u *prev_s = s; |
| 5704 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5705 | s = skipwhite(s); |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5706 | |
| 5707 | /* Perl/shell # comment comment continues until eol. Require a space |
| 5708 | * before # to avoid recognizing $#array. */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 5709 | if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#') |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 5710 | { |
| 5711 | s += STRLEN(s); |
| 5712 | break; |
| 5713 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5714 | if (*s != '/') |
| 5715 | break; |
| 5716 | ++s; |
| 5717 | if (*s == '/') /* slash-slash comment continues till eol */ |
| 5718 | { |
| 5719 | s += STRLEN(s); |
| 5720 | break; |
| 5721 | } |
| 5722 | if (*s != '*') |
| 5723 | break; |
| 5724 | for (++s; *s; ++s) /* skip slash-star comment */ |
| 5725 | if (s[0] == '*' && s[1] == '/') |
| 5726 | { |
| 5727 | s += 2; |
| 5728 | break; |
| 5729 | } |
| 5730 | } |
| 5731 | return s; |
| 5732 | } |
| 5733 | |
| 5734 | /* |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 5735 | * 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] | 5736 | * not considered code. |
| 5737 | */ |
| 5738 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5739 | cin_nocode(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5740 | { |
| 5741 | return *cin_skipcomment(s) == NUL; |
| 5742 | } |
| 5743 | |
| 5744 | /* |
| 5745 | * Check previous lines for a "//" line comment, skipping over blank lines. |
| 5746 | */ |
| 5747 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5748 | find_line_comment(void) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5749 | { |
| 5750 | static pos_T pos; |
| 5751 | char_u *line; |
| 5752 | char_u *p; |
| 5753 | |
| 5754 | pos = curwin->w_cursor; |
| 5755 | while (--pos.lnum > 0) |
| 5756 | { |
| 5757 | line = ml_get(pos.lnum); |
| 5758 | p = skipwhite(line); |
| 5759 | if (cin_islinecomment(p)) |
| 5760 | { |
| 5761 | pos.col = (int)(p - line); |
| 5762 | return &pos; |
| 5763 | } |
| 5764 | if (*p != NUL) |
| 5765 | break; |
| 5766 | } |
| 5767 | return NULL; |
| 5768 | } |
| 5769 | |
| 5770 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5771 | * Return TRUE if "text" starts with "key:". |
| 5772 | */ |
| 5773 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5774 | cin_has_js_key(char_u *text) |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5775 | { |
| 5776 | char_u *s = skipwhite(text); |
Bram Moolenaar | ece29e8 | 2014-08-06 12:49:18 +0200 | [diff] [blame] | 5777 | int quote = -1; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5778 | |
| 5779 | if (*s == '\'' || *s == '"') |
| 5780 | { |
| 5781 | /* can be 'key': or "key": */ |
| 5782 | quote = *s; |
| 5783 | ++s; |
| 5784 | } |
| 5785 | if (!vim_isIDc(*s)) /* need at least one ID character */ |
| 5786 | return FALSE; |
| 5787 | |
| 5788 | while (vim_isIDc(*s)) |
| 5789 | ++s; |
| 5790 | if (*s == quote) |
| 5791 | ++s; |
| 5792 | |
| 5793 | s = cin_skipcomment(s); |
| 5794 | |
| 5795 | /* "::" is not a label, it's C++ */ |
| 5796 | return (*s == ':' && s[1] != ':'); |
| 5797 | } |
| 5798 | |
| 5799 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5800 | * Check if string matches "label:"; move to character after ':' if true. |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 5801 | * "*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] | 5802 | */ |
| 5803 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5804 | cin_islabel_skip(char_u **s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5805 | { |
| 5806 | if (!vim_isIDc(**s)) /* need at least one ID character */ |
| 5807 | return FALSE; |
| 5808 | |
| 5809 | while (vim_isIDc(**s)) |
| 5810 | (*s)++; |
| 5811 | |
| 5812 | *s = cin_skipcomment(*s); |
| 5813 | |
| 5814 | /* "::" is not a label, it's C++ */ |
| 5815 | return (**s == ':' && *++*s != ':'); |
| 5816 | } |
| 5817 | |
| 5818 | /* |
| 5819 | * Recognize a label: "label:". |
| 5820 | * Note: curwin->w_cursor must be where we are looking for the label. |
| 5821 | */ |
| 5822 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5823 | cin_islabel(void) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5824 | { |
| 5825 | char_u *s; |
| 5826 | |
| 5827 | s = cin_skipcomment(ml_get_curline()); |
| 5828 | |
| 5829 | /* |
| 5830 | * Exclude "default" from labels, since it should be indented |
| 5831 | * like a switch label. Same for C++ scope declarations. |
| 5832 | */ |
| 5833 | if (cin_isdefault(s)) |
| 5834 | return FALSE; |
| 5835 | if (cin_isscopedecl(s)) |
| 5836 | return FALSE; |
| 5837 | |
| 5838 | if (cin_islabel_skip(&s)) |
| 5839 | { |
| 5840 | /* |
| 5841 | * Only accept a label if the previous line is terminated or is a case |
| 5842 | * label. |
| 5843 | */ |
| 5844 | pos_T cursor_save; |
| 5845 | pos_T *trypos; |
| 5846 | char_u *line; |
| 5847 | |
| 5848 | cursor_save = curwin->w_cursor; |
| 5849 | while (curwin->w_cursor.lnum > 1) |
| 5850 | { |
| 5851 | --curwin->w_cursor.lnum; |
| 5852 | |
| 5853 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 5854 | * If we're in a comment or raw string now, skip to the start of |
| 5855 | * it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5856 | */ |
| 5857 | curwin->w_cursor.col = 0; |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 5858 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5859 | curwin->w_cursor = *trypos; |
| 5860 | |
| 5861 | line = ml_get_curline(); |
| 5862 | if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */ |
| 5863 | continue; |
| 5864 | if (*(line = cin_skipcomment(line)) == NUL) |
| 5865 | continue; |
| 5866 | |
| 5867 | curwin->w_cursor = cursor_save; |
| 5868 | if (cin_isterminated(line, TRUE, FALSE) |
| 5869 | || cin_isscopedecl(line) |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5870 | || cin_iscase(line, TRUE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5871 | || (cin_islabel_skip(&line) && cin_nocode(line))) |
| 5872 | return TRUE; |
| 5873 | return FALSE; |
| 5874 | } |
| 5875 | curwin->w_cursor = cursor_save; |
| 5876 | return TRUE; /* label at start of file??? */ |
| 5877 | } |
| 5878 | return FALSE; |
| 5879 | } |
| 5880 | |
| 5881 | /* |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5882 | * Recognize structure initialization and enumerations: |
| 5883 | * "[typedef] [static|public|protected|private] enum" |
| 5884 | * "[typedef] [static|public|protected|private] = {" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5885 | */ |
| 5886 | static int |
| 5887 | cin_isinit(void) |
| 5888 | { |
| 5889 | char_u *s; |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5890 | static char *skip[] = {"static", "public", "protected", "private"}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5891 | |
| 5892 | s = cin_skipcomment(ml_get_curline()); |
| 5893 | |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5894 | if (cin_starts_with(s, "typedef")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5895 | s = cin_skipcomment(s + 7); |
| 5896 | |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5897 | for (;;) |
| 5898 | { |
| 5899 | int i, l; |
Bram Moolenaar | a528565 | 2011-12-14 20:05:21 +0100 | [diff] [blame] | 5900 | |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5901 | for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i) |
| 5902 | { |
Bram Moolenaar | b3cb982 | 2013-03-13 17:01:52 +0100 | [diff] [blame] | 5903 | l = (int)strlen(skip[i]); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5904 | if (cin_starts_with(s, skip[i])) |
| 5905 | { |
| 5906 | s = cin_skipcomment(s + l); |
| 5907 | l = 0; |
| 5908 | break; |
| 5909 | } |
| 5910 | } |
| 5911 | if (l != 0) |
| 5912 | break; |
| 5913 | } |
| 5914 | |
| 5915 | if (cin_starts_with(s, "enum")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5916 | return TRUE; |
| 5917 | |
| 5918 | if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) |
| 5919 | return TRUE; |
| 5920 | |
| 5921 | return FALSE; |
| 5922 | } |
| 5923 | |
| 5924 | /* |
| 5925 | * Recognize a switch label: "case .*:" or "default:". |
| 5926 | */ |
| 5927 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5928 | cin_iscase( |
| 5929 | char_u *s, |
| 5930 | int strict) /* Allow relaxed check of case statement for JS */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5931 | { |
| 5932 | s = cin_skipcomment(s); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 5933 | if (cin_starts_with(s, "case")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5934 | { |
| 5935 | for (s += 4; *s; ++s) |
| 5936 | { |
| 5937 | s = cin_skipcomment(s); |
| 5938 | if (*s == ':') |
| 5939 | { |
| 5940 | if (s[1] == ':') /* skip over "::" for C++ */ |
| 5941 | ++s; |
| 5942 | else |
| 5943 | return TRUE; |
| 5944 | } |
| 5945 | if (*s == '\'' && s[1] && s[2] == '\'') |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5946 | s += 2; /* skip over ':' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5947 | else if (*s == '/' && (s[1] == '*' || s[1] == '/')) |
| 5948 | return FALSE; /* stop at comment */ |
| 5949 | else if (*s == '"') |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 5950 | { |
| 5951 | /* JS etc. */ |
| 5952 | if (strict) |
| 5953 | return FALSE; /* stop at string */ |
| 5954 | else |
| 5955 | return TRUE; |
| 5956 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5957 | } |
| 5958 | return FALSE; |
| 5959 | } |
| 5960 | |
| 5961 | if (cin_isdefault(s)) |
| 5962 | return TRUE; |
| 5963 | return FALSE; |
| 5964 | } |
| 5965 | |
| 5966 | /* |
| 5967 | * Recognize a "default" switch label. |
| 5968 | */ |
| 5969 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5970 | cin_isdefault(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5971 | { |
| 5972 | return (STRNCMP(s, "default", 7) == 0 |
| 5973 | && *(s = cin_skipcomment(s + 7)) == ':' |
| 5974 | && s[1] != ':'); |
| 5975 | } |
| 5976 | |
| 5977 | /* |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 5978 | * Recognize a "public/private/protected" scope declaration label. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5979 | */ |
| 5980 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 5981 | cin_isscopedecl(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5982 | { |
| 5983 | int i; |
| 5984 | |
| 5985 | s = cin_skipcomment(s); |
| 5986 | if (STRNCMP(s, "public", 6) == 0) |
| 5987 | i = 6; |
| 5988 | else if (STRNCMP(s, "protected", 9) == 0) |
| 5989 | i = 9; |
| 5990 | else if (STRNCMP(s, "private", 7) == 0) |
| 5991 | i = 7; |
| 5992 | else |
| 5993 | return FALSE; |
| 5994 | return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':'); |
| 5995 | } |
| 5996 | |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 5997 | /* Maximum number of lines to search back for a "namespace" line. */ |
| 5998 | #define FIND_NAMESPACE_LIM 20 |
| 5999 | |
| 6000 | /* |
| 6001 | * Recognize a "namespace" scope declaration. |
| 6002 | */ |
| 6003 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6004 | cin_is_cpp_namespace(char_u *s) |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 6005 | { |
| 6006 | char_u *p; |
| 6007 | int has_name = FALSE; |
Bram Moolenaar | ca8b8d6 | 2016-11-17 21:30:27 +0100 | [diff] [blame] | 6008 | int has_name_start = FALSE; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 6009 | |
| 6010 | s = cin_skipcomment(s); |
| 6011 | if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9]))) |
| 6012 | { |
| 6013 | p = cin_skipcomment(skipwhite(s + 9)); |
| 6014 | while (*p != NUL) |
| 6015 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6016 | if (VIM_ISWHITE(*p)) |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 6017 | { |
| 6018 | has_name = TRUE; /* found end of a name */ |
| 6019 | p = cin_skipcomment(skipwhite(p)); |
| 6020 | } |
| 6021 | else if (*p == '{') |
| 6022 | { |
| 6023 | break; |
| 6024 | } |
| 6025 | else if (vim_iswordc(*p)) |
| 6026 | { |
Bram Moolenaar | ca8b8d6 | 2016-11-17 21:30:27 +0100 | [diff] [blame] | 6027 | has_name_start = TRUE; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 6028 | if (has_name) |
| 6029 | return FALSE; /* word character after skipping past name */ |
| 6030 | ++p; |
| 6031 | } |
Bram Moolenaar | ca8b8d6 | 2016-11-17 21:30:27 +0100 | [diff] [blame] | 6032 | else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2])) |
| 6033 | { |
| 6034 | if (!has_name_start || has_name) |
| 6035 | return FALSE; |
| 6036 | /* C++ 17 nested namespace */ |
| 6037 | p += 3; |
| 6038 | } |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 6039 | else |
| 6040 | { |
| 6041 | return FALSE; |
| 6042 | } |
| 6043 | } |
| 6044 | return TRUE; |
| 6045 | } |
| 6046 | return FALSE; |
| 6047 | } |
| 6048 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6049 | /* |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 6050 | * Recognize a `extern "C"` or `extern "C++"` linkage specifications. |
| 6051 | */ |
| 6052 | static int |
| 6053 | cin_is_cpp_extern_c(char_u *s) |
| 6054 | { |
| 6055 | char_u *p; |
| 6056 | int has_string_literal = FALSE; |
| 6057 | |
| 6058 | s = cin_skipcomment(s); |
| 6059 | if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6]))) |
| 6060 | { |
| 6061 | p = cin_skipcomment(skipwhite(s + 6)); |
| 6062 | while (*p != NUL) |
| 6063 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6064 | if (VIM_ISWHITE(*p)) |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 6065 | { |
| 6066 | p = cin_skipcomment(skipwhite(p)); |
| 6067 | } |
| 6068 | else if (*p == '{') |
| 6069 | { |
| 6070 | break; |
| 6071 | } |
| 6072 | else if (p[0] == '"' && p[1] == 'C' && p[2] == '"') |
| 6073 | { |
| 6074 | if (has_string_literal) |
| 6075 | return FALSE; |
| 6076 | has_string_literal = TRUE; |
| 6077 | p += 3; |
| 6078 | } |
| 6079 | else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+' |
| 6080 | && p[4] == '"') |
| 6081 | { |
| 6082 | if (has_string_literal) |
| 6083 | return FALSE; |
| 6084 | has_string_literal = TRUE; |
| 6085 | p += 5; |
| 6086 | } |
| 6087 | else |
| 6088 | { |
| 6089 | return FALSE; |
| 6090 | } |
| 6091 | } |
| 6092 | return has_string_literal ? TRUE : FALSE; |
| 6093 | } |
| 6094 | return FALSE; |
| 6095 | } |
| 6096 | |
| 6097 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6098 | * Return a pointer to the first non-empty non-comment character after a ':'. |
| 6099 | * Return NULL if not found. |
| 6100 | * case 234: a = b; |
| 6101 | * ^ |
| 6102 | */ |
| 6103 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6104 | after_label(char_u *l) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6105 | { |
| 6106 | for ( ; *l; ++l) |
| 6107 | { |
| 6108 | if (*l == ':') |
| 6109 | { |
| 6110 | if (l[1] == ':') /* skip over "::" for C++ */ |
| 6111 | ++l; |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 6112 | else if (!cin_iscase(l + 1, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6113 | break; |
| 6114 | } |
| 6115 | else if (*l == '\'' && l[1] && l[2] == '\'') |
| 6116 | l += 2; /* skip over 'x' */ |
| 6117 | } |
| 6118 | if (*l == NUL) |
| 6119 | return NULL; |
| 6120 | l = cin_skipcomment(l + 1); |
| 6121 | if (*l == NUL) |
| 6122 | return NULL; |
| 6123 | return l; |
| 6124 | } |
| 6125 | |
| 6126 | /* |
| 6127 | * Get indent of line "lnum", skipping a label. |
| 6128 | * Return 0 if there is nothing after the label. |
| 6129 | */ |
| 6130 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6131 | get_indent_nolabel (linenr_T lnum) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6132 | { |
| 6133 | char_u *l; |
| 6134 | pos_T fp; |
| 6135 | colnr_T col; |
| 6136 | char_u *p; |
| 6137 | |
| 6138 | l = ml_get(lnum); |
| 6139 | p = after_label(l); |
| 6140 | if (p == NULL) |
| 6141 | return 0; |
| 6142 | |
| 6143 | fp.col = (colnr_T)(p - l); |
| 6144 | fp.lnum = lnum; |
| 6145 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 6146 | return (int)col; |
| 6147 | } |
| 6148 | |
| 6149 | /* |
| 6150 | * Find indent for line "lnum", ignoring any case or jump label. |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6151 | * Also return a pointer to the text (after the label) in "pp". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6152 | * label: if (asdf && asdfasdf) |
| 6153 | * ^ |
| 6154 | */ |
| 6155 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6156 | skip_label(linenr_T lnum, char_u **pp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6157 | { |
| 6158 | char_u *l; |
| 6159 | int amount; |
| 6160 | pos_T cursor_save; |
| 6161 | |
| 6162 | cursor_save = curwin->w_cursor; |
| 6163 | curwin->w_cursor.lnum = lnum; |
| 6164 | l = ml_get_curline(); |
| 6165 | /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6166 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6167 | { |
| 6168 | amount = get_indent_nolabel(lnum); |
| 6169 | l = after_label(ml_get_curline()); |
| 6170 | if (l == NULL) /* just in case */ |
| 6171 | l = ml_get_curline(); |
| 6172 | } |
| 6173 | else |
| 6174 | { |
| 6175 | amount = get_indent(); |
| 6176 | l = ml_get_curline(); |
| 6177 | } |
| 6178 | *pp = l; |
| 6179 | |
| 6180 | curwin->w_cursor = cursor_save; |
| 6181 | return amount; |
| 6182 | } |
| 6183 | |
| 6184 | /* |
| 6185 | * Return the indent of the first variable name after a type in a declaration. |
| 6186 | * int a, indent of "a" |
| 6187 | * static struct foo b, indent of "b" |
| 6188 | * enum bla c, indent of "c" |
| 6189 | * Returns zero when it doesn't look like a declaration. |
| 6190 | */ |
| 6191 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6192 | cin_first_id_amount(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6193 | { |
| 6194 | char_u *line, *p, *s; |
| 6195 | int len; |
| 6196 | pos_T fp; |
| 6197 | colnr_T col; |
| 6198 | |
| 6199 | line = ml_get_curline(); |
| 6200 | p = skipwhite(line); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6201 | len = (int)(skiptowhite(p) - p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6202 | if (len == 6 && STRNCMP(p, "static", 6) == 0) |
| 6203 | { |
| 6204 | p = skipwhite(p + 6); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 6205 | len = (int)(skiptowhite(p) - p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6206 | } |
| 6207 | if (len == 6 && STRNCMP(p, "struct", 6) == 0) |
| 6208 | p = skipwhite(p + 6); |
| 6209 | else if (len == 4 && STRNCMP(p, "enum", 4) == 0) |
| 6210 | p = skipwhite(p + 4); |
| 6211 | else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0) |
| 6212 | || (len == 6 && STRNCMP(p, "signed", 6) == 0)) |
| 6213 | { |
| 6214 | s = skipwhite(p + len); |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6215 | if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3])) |
| 6216 | || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4])) |
| 6217 | || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5])) |
| 6218 | || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4]))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6219 | p = s; |
| 6220 | } |
| 6221 | for (len = 0; vim_isIDc(p[len]); ++len) |
| 6222 | ; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6223 | if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6224 | return 0; |
| 6225 | |
| 6226 | p = skipwhite(p + len); |
| 6227 | fp.lnum = curwin->w_cursor.lnum; |
| 6228 | fp.col = (colnr_T)(p - line); |
| 6229 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 6230 | return (int)col; |
| 6231 | } |
| 6232 | |
| 6233 | /* |
| 6234 | * Return the indent of the first non-blank after an equal sign. |
| 6235 | * char *foo = "here"; |
| 6236 | * Return zero if no (useful) equal sign found. |
| 6237 | * Return -1 if the line above "lnum" ends in a backslash. |
| 6238 | * foo = "asdf\ |
| 6239 | * asdf\ |
| 6240 | * here"; |
| 6241 | */ |
| 6242 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6243 | cin_get_equal_amount(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6244 | { |
| 6245 | char_u *line; |
| 6246 | char_u *s; |
| 6247 | colnr_T col; |
| 6248 | pos_T fp; |
| 6249 | |
| 6250 | if (lnum > 1) |
| 6251 | { |
| 6252 | line = ml_get(lnum - 1); |
| 6253 | if (*line != NUL && line[STRLEN(line) - 1] == '\\') |
| 6254 | return -1; |
| 6255 | } |
| 6256 | |
| 6257 | line = s = ml_get(lnum); |
| 6258 | while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL) |
| 6259 | { |
| 6260 | if (cin_iscomment(s)) /* ignore comments */ |
| 6261 | s = cin_skipcomment(s); |
| 6262 | else |
| 6263 | ++s; |
| 6264 | } |
| 6265 | if (*s != '=') |
| 6266 | return 0; |
| 6267 | |
| 6268 | s = skipwhite(s + 1); |
| 6269 | if (cin_nocode(s)) |
| 6270 | return 0; |
| 6271 | |
| 6272 | if (*s == '"') /* nice alignment for continued strings */ |
| 6273 | ++s; |
| 6274 | |
| 6275 | fp.lnum = lnum; |
| 6276 | fp.col = (colnr_T)(s - line); |
| 6277 | getvcol(curwin, &fp, &col, NULL, NULL); |
| 6278 | return (int)col; |
| 6279 | } |
| 6280 | |
| 6281 | /* |
| 6282 | * Recognize a preprocessor statement: Any line that starts with '#'. |
| 6283 | */ |
| 6284 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6285 | cin_ispreproc(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6286 | { |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6287 | if (*skipwhite(s) == '#') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6288 | return TRUE; |
| 6289 | return FALSE; |
| 6290 | } |
| 6291 | |
| 6292 | /* |
| 6293 | * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a |
| 6294 | * continuation line of a preprocessor statement. Decrease "*lnump" to the |
| 6295 | * start and return the line in "*pp". |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6296 | * Put the amount of indent in "*amount". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6297 | */ |
| 6298 | static int |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6299 | cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6300 | { |
| 6301 | char_u *line = *pp; |
| 6302 | linenr_T lnum = *lnump; |
| 6303 | int retval = FALSE; |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6304 | int candidate_amount = *amount; |
| 6305 | |
| 6306 | if (*line != NUL && line[STRLEN(line) - 1] == '\\') |
| 6307 | candidate_amount = get_indent_lnum(lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6308 | |
Bram Moolenaar | d8e9bb2 | 2005-07-09 21:14:46 +0000 | [diff] [blame] | 6309 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6310 | { |
| 6311 | if (cin_ispreproc(line)) |
| 6312 | { |
| 6313 | retval = TRUE; |
| 6314 | *lnump = lnum; |
| 6315 | break; |
| 6316 | } |
| 6317 | if (lnum == 1) |
| 6318 | break; |
| 6319 | line = ml_get(--lnum); |
| 6320 | if (*line == NUL || line[STRLEN(line) - 1] != '\\') |
| 6321 | break; |
| 6322 | } |
| 6323 | |
| 6324 | if (lnum != *lnump) |
| 6325 | *pp = ml_get(*lnump); |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 6326 | if (retval) |
| 6327 | *amount = candidate_amount; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6328 | return retval; |
| 6329 | } |
| 6330 | |
| 6331 | /* |
| 6332 | * Recognize the start of a C or C++ comment. |
| 6333 | */ |
| 6334 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6335 | cin_iscomment(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6336 | { |
| 6337 | return (p[0] == '/' && (p[1] == '*' || p[1] == '/')); |
| 6338 | } |
| 6339 | |
| 6340 | /* |
| 6341 | * Recognize the start of a "//" comment. |
| 6342 | */ |
| 6343 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6344 | cin_islinecomment(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6345 | { |
| 6346 | return (p[0] == '/' && p[1] == '/'); |
| 6347 | } |
| 6348 | |
| 6349 | /* |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6350 | * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or |
| 6351 | * '}'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6352 | * Don't consider "} else" a terminated line. |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6353 | * If a line begins with an "else", only consider it terminated if no unmatched |
| 6354 | * opening braces follow (handle "else { foo();" correctly). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6355 | * Return the character terminating the line (ending char's have precedence if |
| 6356 | * both apply in order to determine initializations). |
| 6357 | */ |
| 6358 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6359 | cin_isterminated( |
| 6360 | char_u *s, |
| 6361 | int incl_open, /* include '{' at the end as terminator */ |
| 6362 | int incl_comma) /* recognize a trailing comma */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6363 | { |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6364 | char_u found_start = 0; |
| 6365 | unsigned n_open = 0; |
| 6366 | int is_else = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6367 | |
| 6368 | s = cin_skipcomment(s); |
| 6369 | |
| 6370 | if (*s == '{' || (*s == '}' && !cin_iselse(s))) |
| 6371 | found_start = *s; |
| 6372 | |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6373 | if (!found_start) |
| 6374 | is_else = cin_iselse(s); |
| 6375 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6376 | while (*s) |
| 6377 | { |
| 6378 | /* skip over comments, "" strings and 'c'haracters */ |
| 6379 | s = skip_string(cin_skipcomment(s)); |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6380 | if (*s == '}' && n_open > 0) |
| 6381 | --n_open; |
Bram Moolenaar | 496f951 | 2011-05-19 16:35:09 +0200 | [diff] [blame] | 6382 | if ((!is_else || n_open == 0) |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6383 | && (*s == ';' || *s == '}' || (incl_comma && *s == ',')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6384 | && cin_nocode(s + 1)) |
| 6385 | return *s; |
Bram Moolenaar | 4ae06c1 | 2011-05-10 11:39:19 +0200 | [diff] [blame] | 6386 | else if (*s == '{') |
| 6387 | { |
| 6388 | if (incl_open && cin_nocode(s + 1)) |
| 6389 | return *s; |
| 6390 | else |
| 6391 | ++n_open; |
| 6392 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6393 | |
| 6394 | if (*s) |
| 6395 | s++; |
| 6396 | } |
| 6397 | return found_start; |
| 6398 | } |
| 6399 | |
| 6400 | /* |
| 6401 | * Recognize the basic picture of a function declaration -- it needs to |
| 6402 | * have an open paren somewhere and a close paren at the end of the line and |
| 6403 | * no semicolons anywhere. |
| 6404 | * When a line ends in a comma we continue looking in the next line. |
| 6405 | * "sp" points to a string with the line. When looking at other lines it must |
| 6406 | * be restored to the line. When it's NULL fetch lines here. |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6407 | * "first_lnum" is where we start looking. |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6408 | * "min_lnum" is the line before which we will not be looking. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6409 | */ |
| 6410 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6411 | cin_isfuncdecl( |
| 6412 | char_u **sp, |
| 6413 | linenr_T first_lnum, |
| 6414 | linenr_T min_lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6415 | { |
| 6416 | char_u *s; |
| 6417 | linenr_T lnum = first_lnum; |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6418 | linenr_T save_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6419 | int retval = FALSE; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6420 | pos_T *trypos; |
| 6421 | int just_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6422 | |
| 6423 | if (sp == NULL) |
| 6424 | s = ml_get(lnum); |
| 6425 | else |
| 6426 | s = *sp; |
| 6427 | |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6428 | curwin->w_cursor.lnum = lnum; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6429 | if (find_last_paren(s, '(', ')') |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6430 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6431 | { |
| 6432 | lnum = trypos->lnum; |
| 6433 | if (lnum < min_lnum) |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6434 | { |
| 6435 | curwin->w_cursor.lnum = save_lnum; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6436 | return FALSE; |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6437 | } |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6438 | |
| 6439 | s = ml_get(lnum); |
| 6440 | } |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 6441 | curwin->w_cursor.lnum = save_lnum; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6442 | |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6443 | /* Ignore line starting with #. */ |
| 6444 | if (cin_ispreproc(s)) |
| 6445 | return FALSE; |
| 6446 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6447 | while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"') |
| 6448 | { |
| 6449 | if (cin_iscomment(s)) /* ignore comments */ |
| 6450 | s = cin_skipcomment(s); |
Bram Moolenaar | e01f4f8 | 2015-11-10 14:06:53 +0100 | [diff] [blame] | 6451 | else if (*s == ':') |
| 6452 | { |
| 6453 | if (*(s + 1) == ':') |
| 6454 | s += 2; |
| 6455 | else |
| 6456 | /* To avoid a mistake in the following situation: |
| 6457 | * A::A(int a, int b) |
| 6458 | * : a(0) // <--not a function decl |
| 6459 | * , b(0) |
| 6460 | * {... |
| 6461 | */ |
| 6462 | return FALSE; |
| 6463 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6464 | else |
| 6465 | ++s; |
| 6466 | } |
| 6467 | if (*s != '(') |
| 6468 | return FALSE; /* ';', ' or " before any () or no '(' */ |
| 6469 | |
| 6470 | while (*s && *s != ';' && *s != '\'' && *s != '"') |
| 6471 | { |
| 6472 | if (*s == ')' && cin_nocode(s + 1)) |
| 6473 | { |
| 6474 | /* ')' at the end: may have found a match |
| 6475 | * Check for he previous line not to end in a backslash: |
| 6476 | * #if defined(x) && \ |
| 6477 | * defined(y) |
| 6478 | */ |
| 6479 | lnum = first_lnum - 1; |
| 6480 | s = ml_get(lnum); |
| 6481 | if (*s == NUL || s[STRLEN(s) - 1] != '\\') |
| 6482 | retval = TRUE; |
| 6483 | goto done; |
| 6484 | } |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6485 | if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6486 | { |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6487 | int comma = (*s == ','); |
| 6488 | |
| 6489 | /* ',' at the end: continue looking in the next line. |
| 6490 | * At the end: check for ',' in the next line, for this style: |
| 6491 | * func(arg1 |
| 6492 | * , arg2) */ |
| 6493 | for (;;) |
| 6494 | { |
| 6495 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 6496 | break; |
| 6497 | s = ml_get(++lnum); |
| 6498 | if (!cin_ispreproc(s)) |
| 6499 | break; |
| 6500 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6501 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 6502 | break; |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6503 | /* Require a comma at end of the line or a comma or ')' at the |
| 6504 | * start of next line. */ |
| 6505 | s = skipwhite(s); |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6506 | if (!just_started && (!comma && *s != ',' && *s != ')')) |
Bram Moolenaar | 8d2d71d | 2011-04-28 13:02:09 +0200 | [diff] [blame] | 6507 | break; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6508 | just_started = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6509 | } |
| 6510 | else if (cin_iscomment(s)) /* ignore comments */ |
| 6511 | s = cin_skipcomment(s); |
| 6512 | else |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6513 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6514 | ++s; |
Bram Moolenaar | c367faa | 2011-12-14 20:21:35 +0100 | [diff] [blame] | 6515 | just_started = FALSE; |
| 6516 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6517 | } |
| 6518 | |
| 6519 | done: |
| 6520 | if (lnum != first_lnum && sp != NULL) |
| 6521 | *sp = ml_get(first_lnum); |
| 6522 | |
| 6523 | return retval; |
| 6524 | } |
| 6525 | |
| 6526 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6527 | cin_isif(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6528 | { |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6529 | return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2])); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6530 | } |
| 6531 | |
| 6532 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6533 | cin_iselse( |
| 6534 | char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6535 | { |
| 6536 | if (*p == '}') /* accept "} else" */ |
| 6537 | p = cin_skipcomment(p + 1); |
| 6538 | return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4])); |
| 6539 | } |
| 6540 | |
| 6541 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6542 | cin_isdo(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6543 | { |
| 6544 | return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2])); |
| 6545 | } |
| 6546 | |
| 6547 | /* |
| 6548 | * Check if this is a "while" that should have a matching "do". |
| 6549 | * We only accept a "while (condition) ;", with only white space between the |
| 6550 | * ')' and ';'. The condition may be spread over several lines. |
| 6551 | */ |
| 6552 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6553 | cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6554 | { |
| 6555 | pos_T cursor_save; |
| 6556 | pos_T *trypos; |
| 6557 | int retval = FALSE; |
| 6558 | |
| 6559 | p = cin_skipcomment(p); |
| 6560 | if (*p == '}') /* accept "} while (cond);" */ |
| 6561 | p = cin_skipcomment(p + 1); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6562 | if (cin_starts_with(p, "while")) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6563 | { |
| 6564 | cursor_save = curwin->w_cursor; |
| 6565 | curwin->w_cursor.lnum = lnum; |
| 6566 | curwin->w_cursor.col = 0; |
| 6567 | p = ml_get_curline(); |
| 6568 | while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */ |
| 6569 | { |
| 6570 | ++p; |
| 6571 | ++curwin->w_cursor.col; |
| 6572 | } |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6573 | if ((trypos = findmatchlimit(NULL, 0, 0, |
| 6574 | curbuf->b_ind_maxparen)) != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6575 | && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';') |
| 6576 | retval = TRUE; |
| 6577 | curwin->w_cursor = cursor_save; |
| 6578 | } |
| 6579 | return retval; |
| 6580 | } |
| 6581 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6582 | /* |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 6583 | * 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] | 6584 | * Return 0 if there is none. |
| 6585 | * Otherwise return !0 and update "*poffset" to point to the place where the |
| 6586 | * string was found. |
| 6587 | */ |
| 6588 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6589 | cin_is_if_for_while_before_offset(char_u *line, int *poffset) |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6590 | { |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 6591 | int offset = *poffset; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6592 | |
| 6593 | if (offset-- < 2) |
| 6594 | return 0; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6595 | while (offset > 2 && VIM_ISWHITE(line[offset])) |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6596 | --offset; |
| 6597 | |
| 6598 | offset -= 1; |
| 6599 | if (!STRNCMP(line + offset, "if", 2)) |
| 6600 | goto probablyFound; |
| 6601 | |
| 6602 | if (offset >= 1) |
| 6603 | { |
| 6604 | offset -= 1; |
| 6605 | if (!STRNCMP(line + offset, "for", 3)) |
| 6606 | goto probablyFound; |
| 6607 | |
| 6608 | if (offset >= 2) |
| 6609 | { |
| 6610 | offset -= 2; |
| 6611 | if (!STRNCMP(line + offset, "while", 5)) |
| 6612 | goto probablyFound; |
| 6613 | } |
| 6614 | } |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6615 | return 0; |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 6616 | |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6617 | probablyFound: |
| 6618 | if (!offset || !vim_isIDc(line[offset - 1])) |
| 6619 | { |
| 6620 | *poffset = offset; |
| 6621 | return 1; |
| 6622 | } |
| 6623 | return 0; |
| 6624 | } |
| 6625 | |
| 6626 | /* |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6627 | * Return TRUE if we are at the end of a do-while. |
| 6628 | * do |
| 6629 | * nothing; |
| 6630 | * while (foo |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 6631 | * && bar); <-- here |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6632 | * Adjust the cursor to the line with "while". |
| 6633 | */ |
| 6634 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6635 | cin_iswhileofdo_end(int terminated) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6636 | { |
| 6637 | char_u *line; |
| 6638 | char_u *p; |
| 6639 | char_u *s; |
| 6640 | pos_T *trypos; |
| 6641 | int i; |
| 6642 | |
| 6643 | if (terminated != ';') /* there must be a ';' at the end */ |
| 6644 | return FALSE; |
| 6645 | |
| 6646 | p = line = ml_get_curline(); |
| 6647 | while (*p != NUL) |
| 6648 | { |
| 6649 | p = cin_skipcomment(p); |
| 6650 | if (*p == ')') |
| 6651 | { |
| 6652 | s = skipwhite(p + 1); |
| 6653 | if (*s == ';' && cin_nocode(s + 1)) |
| 6654 | { |
| 6655 | /* Found ");" at end of the line, now check there is "while" |
| 6656 | * before the matching '('. XXX */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6657 | i = (int)(p - line); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6658 | curwin->w_cursor.col = i; |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6659 | trypos = find_match_paren(curbuf->b_ind_maxparen); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6660 | if (trypos != NULL) |
| 6661 | { |
| 6662 | s = cin_skipcomment(ml_get(trypos->lnum)); |
| 6663 | if (*s == '}') /* accept "} while (cond);" */ |
| 6664 | s = cin_skipcomment(s + 1); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6665 | if (cin_starts_with(s, "while")) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6666 | { |
| 6667 | curwin->w_cursor.lnum = trypos->lnum; |
| 6668 | return TRUE; |
| 6669 | } |
| 6670 | } |
| 6671 | |
| 6672 | /* Searching may have made "line" invalid, get it again. */ |
| 6673 | line = ml_get_curline(); |
| 6674 | p = line + i; |
| 6675 | } |
| 6676 | } |
| 6677 | if (*p != NUL) |
| 6678 | ++p; |
| 6679 | } |
| 6680 | return FALSE; |
| 6681 | } |
| 6682 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6683 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6684 | cin_isbreak(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6685 | { |
| 6686 | return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5])); |
| 6687 | } |
| 6688 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6689 | /* |
| 6690 | * Find the position of a C++ base-class declaration or |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6691 | * constructor-initialization. eg: |
| 6692 | * |
| 6693 | * class MyClass : |
| 6694 | * baseClass <-- here |
| 6695 | * class MyClass : public baseClass, |
| 6696 | * anotherBaseClass <-- here (should probably lineup ??) |
| 6697 | * MyClass::MyClass(...) : |
| 6698 | * baseClass(...) <-- here (constructor-initialization) |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 6699 | * |
| 6700 | * This is a lot of guessing. Watch out for "cond ? func() : foo". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6701 | */ |
| 6702 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6703 | cin_is_cpp_baseclass( |
| 6704 | cpp_baseclass_cache_T *cached) /* input and output */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6705 | { |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6706 | lpos_T *pos = &cached->lpos; /* find position */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6707 | char_u *s; |
| 6708 | int class_or_struct, lookfor_ctor_init, cpp_base_class; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6709 | linenr_T lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6710 | char_u *line = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6711 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6712 | if (pos->lnum <= lnum) |
| 6713 | return cached->found; /* Use the cached result */ |
| 6714 | |
| 6715 | pos->col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6716 | |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 6717 | s = skipwhite(line); |
| 6718 | if (*s == '#') /* skip #define FOO x ? (x) : x */ |
| 6719 | return FALSE; |
| 6720 | s = cin_skipcomment(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6721 | if (*s == NUL) |
| 6722 | return FALSE; |
| 6723 | |
| 6724 | cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; |
| 6725 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6726 | /* Search for a line starting with '#', empty, ending in ';' or containing |
| 6727 | * '{' or '}' and start below it. This handles the following situations: |
| 6728 | * a = cond ? |
| 6729 | * func() : |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 6730 | * asdf; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6731 | * func::foo() |
| 6732 | * : something |
| 6733 | * {} |
| 6734 | * Foo::Foo (int one, int two) |
| 6735 | * : something(4), |
| 6736 | * somethingelse(3) |
| 6737 | * {} |
| 6738 | */ |
| 6739 | while (lnum > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6740 | { |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6741 | line = ml_get(lnum - 1); |
| 6742 | s = skipwhite(line); |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6743 | if (*s == '#' || *s == NUL) |
| 6744 | break; |
| 6745 | while (*s != NUL) |
| 6746 | { |
| 6747 | s = cin_skipcomment(s); |
| 6748 | if (*s == '{' || *s == '}' |
| 6749 | || (*s == ';' && cin_nocode(s + 1))) |
| 6750 | break; |
| 6751 | if (*s != NUL) |
| 6752 | ++s; |
| 6753 | } |
| 6754 | if (*s != NUL) |
| 6755 | break; |
| 6756 | --lnum; |
| 6757 | } |
| 6758 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6759 | pos->lnum = lnum; |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6760 | line = ml_get(lnum); |
Bram Moolenaar | d1b15de | 2015-10-13 16:13:39 +0200 | [diff] [blame] | 6761 | s = line; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6762 | for (;;) |
| 6763 | { |
| 6764 | if (*s == NUL) |
| 6765 | { |
| 6766 | if (lnum == curwin->w_cursor.lnum) |
| 6767 | break; |
| 6768 | /* Continue in the cursor line. */ |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6769 | line = ml_get(++lnum); |
Bram Moolenaar | d1b15de | 2015-10-13 16:13:39 +0200 | [diff] [blame] | 6770 | s = line; |
| 6771 | } |
| 6772 | if (s == line) |
| 6773 | { |
| 6774 | /* don't recognize "case (foo):" as a baseclass */ |
| 6775 | if (cin_iscase(s, FALSE)) |
| 6776 | break; |
Bram Moolenaar | e7c5686 | 2007-08-04 10:14:52 +0000 | [diff] [blame] | 6777 | s = cin_skipcomment(line); |
| 6778 | if (*s == NUL) |
| 6779 | continue; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6780 | } |
| 6781 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 6782 | if (s[0] == '"' || (s[0] == 'R' && s[1] == '"')) |
Bram Moolenaar | aede6ce | 2011-05-10 11:56:30 +0200 | [diff] [blame] | 6783 | s = skip_string(s) + 1; |
| 6784 | else if (s[0] == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6785 | { |
| 6786 | if (s[1] == ':') |
| 6787 | { |
| 6788 | /* skip double colon. It can't be a constructor |
| 6789 | * initialization any more */ |
| 6790 | lookfor_ctor_init = FALSE; |
| 6791 | s = cin_skipcomment(s + 2); |
| 6792 | } |
| 6793 | else if (lookfor_ctor_init || class_or_struct) |
| 6794 | { |
| 6795 | /* we have something found, that looks like the start of |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 6796 | * cpp-base-class-declaration or constructor-initialization */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6797 | cpp_base_class = TRUE; |
| 6798 | lookfor_ctor_init = class_or_struct = FALSE; |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6799 | pos->col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6800 | s = cin_skipcomment(s + 1); |
| 6801 | } |
| 6802 | else |
| 6803 | s = cin_skipcomment(s + 1); |
| 6804 | } |
| 6805 | else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5])) |
| 6806 | || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6]))) |
| 6807 | { |
| 6808 | class_or_struct = TRUE; |
| 6809 | lookfor_ctor_init = FALSE; |
| 6810 | |
| 6811 | if (*s == 'c') |
| 6812 | s = cin_skipcomment(s + 5); |
| 6813 | else |
| 6814 | s = cin_skipcomment(s + 6); |
| 6815 | } |
| 6816 | else |
| 6817 | { |
| 6818 | if (s[0] == '{' || s[0] == '}' || s[0] == ';') |
| 6819 | { |
| 6820 | cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; |
| 6821 | } |
| 6822 | else if (s[0] == ')') |
| 6823 | { |
| 6824 | /* Constructor-initialization is assumed if we come across |
| 6825 | * something like "):" */ |
| 6826 | class_or_struct = FALSE; |
| 6827 | lookfor_ctor_init = TRUE; |
| 6828 | } |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 6829 | else if (s[0] == '?') |
| 6830 | { |
| 6831 | /* Avoid seeing '() :' after '?' as constructor init. */ |
| 6832 | return FALSE; |
| 6833 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6834 | else if (!vim_isIDc(s[0])) |
| 6835 | { |
| 6836 | /* if it is not an identifier, we are wrong */ |
| 6837 | class_or_struct = FALSE; |
| 6838 | lookfor_ctor_init = FALSE; |
| 6839 | } |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6840 | else if (pos->col == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6841 | { |
| 6842 | /* it can't be a constructor-initialization any more */ |
| 6843 | lookfor_ctor_init = FALSE; |
| 6844 | |
| 6845 | /* the first statement starts here: lineup with this one... */ |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6846 | if (cpp_base_class) |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6847 | pos->col = (colnr_T)(s - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6848 | } |
| 6849 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6850 | /* When the line ends in a comma don't align with it. */ |
| 6851 | if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1)) |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6852 | pos->col = 0; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6853 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6854 | s = cin_skipcomment(s + 1); |
| 6855 | } |
| 6856 | } |
| 6857 | |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 6858 | cached->found = cpp_base_class; |
| 6859 | if (cpp_base_class) |
| 6860 | pos->lnum = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6861 | return cpp_base_class; |
| 6862 | } |
| 6863 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6864 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6865 | get_baseclass_amount(int col) |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6866 | { |
| 6867 | int amount; |
| 6868 | colnr_T vcol; |
| 6869 | pos_T *trypos; |
| 6870 | |
| 6871 | if (col == 0) |
| 6872 | { |
| 6873 | amount = get_indent(); |
| 6874 | if (find_last_paren(ml_get_curline(), '(', ')') |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6875 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6876 | amount = get_indent_lnum(trypos->lnum); /* XXX */ |
| 6877 | if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL)) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6878 | amount += curbuf->b_ind_cpp_baseclass; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6879 | } |
| 6880 | else |
| 6881 | { |
| 6882 | curwin->w_cursor.col = col; |
| 6883 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 6884 | amount = (int)vcol; |
| 6885 | } |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 6886 | if (amount < curbuf->b_ind_cpp_baseclass) |
| 6887 | amount = curbuf->b_ind_cpp_baseclass; |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 6888 | return amount; |
| 6889 | } |
| 6890 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6891 | /* |
| 6892 | * Return TRUE if string "s" ends with the string "find", possibly followed by |
| 6893 | * white space and comments. Skip strings and comments. |
| 6894 | * Ignore "ignore" after "find" if it's not NULL. |
| 6895 | */ |
| 6896 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6897 | cin_ends_in(char_u *s, char_u *find, char_u *ignore) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6898 | { |
| 6899 | char_u *p = s; |
| 6900 | char_u *r; |
| 6901 | int len = (int)STRLEN(find); |
| 6902 | |
| 6903 | while (*p != NUL) |
| 6904 | { |
| 6905 | p = cin_skipcomment(p); |
| 6906 | if (STRNCMP(p, find, len) == 0) |
| 6907 | { |
| 6908 | r = skipwhite(p + len); |
| 6909 | if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0) |
| 6910 | r = skipwhite(r + STRLEN(ignore)); |
| 6911 | if (cin_nocode(r)) |
| 6912 | return TRUE; |
| 6913 | } |
| 6914 | if (*p != NUL) |
| 6915 | ++p; |
| 6916 | } |
| 6917 | return FALSE; |
| 6918 | } |
| 6919 | |
| 6920 | /* |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6921 | * Return TRUE when "s" starts with "word" and then a non-ID character. |
| 6922 | */ |
| 6923 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6924 | cin_starts_with(char_u *s, char *word) |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6925 | { |
Bram Moolenaar | b3cb982 | 2013-03-13 17:01:52 +0100 | [diff] [blame] | 6926 | int l = (int)STRLEN(word); |
Bram Moolenaar | 7534221 | 2013-03-07 13:13:52 +0100 | [diff] [blame] | 6927 | |
| 6928 | return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l])); |
| 6929 | } |
| 6930 | |
| 6931 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6932 | * Skip strings, chars and comments until at or past "trypos". |
| 6933 | * Return the column found. |
| 6934 | */ |
| 6935 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6936 | cin_skip2pos(pos_T *trypos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6937 | { |
| 6938 | char_u *line; |
| 6939 | char_u *p; |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 6940 | char_u *new_p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6941 | |
| 6942 | p = line = ml_get(trypos->lnum); |
| 6943 | while (*p && (colnr_T)(p - line) < trypos->col) |
| 6944 | { |
| 6945 | if (cin_iscomment(p)) |
| 6946 | p = cin_skipcomment(p); |
| 6947 | else |
| 6948 | { |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 6949 | new_p = skip_string(p); |
| 6950 | if (new_p == p) |
| 6951 | ++p; |
| 6952 | else |
| 6953 | p = new_p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6954 | } |
| 6955 | } |
| 6956 | return (int)(p - line); |
| 6957 | } |
| 6958 | |
| 6959 | /* |
| 6960 | * Find the '{' at the start of the block we are in. |
| 6961 | * Return NULL if no match found. |
| 6962 | * Ignore a '{' that is in a comment, makes indenting the next three lines |
| 6963 | * work. */ |
| 6964 | /* foo() */ |
| 6965 | /* { */ |
| 6966 | /* } */ |
| 6967 | |
| 6968 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6969 | find_start_brace(void) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6970 | { |
| 6971 | pos_T cursor_save; |
| 6972 | pos_T *trypos; |
| 6973 | pos_T *pos; |
| 6974 | static pos_T pos_copy; |
| 6975 | |
| 6976 | cursor_save = curwin->w_cursor; |
| 6977 | while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL) |
| 6978 | { |
| 6979 | pos_copy = *trypos; /* copy pos_T, next findmatch will change it */ |
| 6980 | trypos = &pos_copy; |
| 6981 | curwin->w_cursor = *trypos; |
| 6982 | pos = NULL; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 6983 | /* ignore the { if it's in a // or / * * / comment */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6984 | if ((colnr_T)cin_skip2pos(trypos) == trypos->col |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 6985 | && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6986 | break; |
| 6987 | if (pos != NULL) |
| 6988 | curwin->w_cursor.lnum = pos->lnum; |
| 6989 | } |
| 6990 | curwin->w_cursor = cursor_save; |
| 6991 | return trypos; |
| 6992 | } |
| 6993 | |
| 6994 | /* |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 6995 | * Find the matching '(', ignoring it if it is in a comment. |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 6996 | * Return NULL if no match found. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6997 | */ |
| 6998 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 6999 | find_match_paren(int ind_maxparen) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7000 | { |
Bram Moolenaar | 80c3fd7 | 2016-09-10 15:52:55 +0200 | [diff] [blame] | 7001 | return find_match_char('(', ind_maxparen); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7002 | } |
| 7003 | |
| 7004 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7005 | find_match_char (int c, int ind_maxparen) /* XXX */ |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7006 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7007 | pos_T cursor_save; |
| 7008 | pos_T *trypos; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7009 | static pos_T pos_copy; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7010 | int ind_maxp_wk; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7011 | |
| 7012 | cursor_save = curwin->w_cursor; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7013 | ind_maxp_wk = ind_maxparen; |
| 7014 | retry: |
| 7015 | if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7016 | { |
| 7017 | /* check if the ( is in a // comment */ |
| 7018 | if ((colnr_T)cin_skip2pos(trypos) > trypos->col) |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7019 | { |
| 7020 | ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum); |
| 7021 | if (ind_maxp_wk > 0) |
| 7022 | { |
| 7023 | curwin->w_cursor = *trypos; |
| 7024 | curwin->w_cursor.col = 0; /* XXX */ |
| 7025 | goto retry; |
| 7026 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7027 | trypos = NULL; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7028 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7029 | else |
| 7030 | { |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7031 | pos_T *trypos_wk; |
| 7032 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7033 | pos_copy = *trypos; /* copy trypos, findmatch will change it */ |
| 7034 | trypos = &pos_copy; |
| 7035 | curwin->w_cursor = *trypos; |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 7036 | if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */ |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7037 | { |
| 7038 | ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum |
| 7039 | - trypos_wk->lnum); |
| 7040 | if (ind_maxp_wk > 0) |
| 7041 | { |
| 7042 | curwin->w_cursor = *trypos_wk; |
| 7043 | goto retry; |
| 7044 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7045 | trypos = NULL; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 7046 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7047 | } |
| 7048 | } |
| 7049 | curwin->w_cursor = cursor_save; |
| 7050 | return trypos; |
| 7051 | } |
| 7052 | |
| 7053 | /* |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 7054 | * Find the matching '(', ignoring it if it is in a comment or before an |
| 7055 | * unmatched {. |
| 7056 | * Return NULL if no match found. |
| 7057 | */ |
| 7058 | static pos_T * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7059 | find_match_paren_after_brace (int ind_maxparen) /* XXX */ |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 7060 | { |
| 7061 | pos_T *trypos = find_match_paren(ind_maxparen); |
| 7062 | |
| 7063 | if (trypos != NULL) |
| 7064 | { |
| 7065 | pos_T *tryposBrace = find_start_brace(); |
| 7066 | |
| 7067 | /* If both an unmatched '(' and '{' is found. Ignore the '(' |
| 7068 | * position if the '{' is further down. */ |
| 7069 | if (tryposBrace != NULL |
| 7070 | && (trypos->lnum != tryposBrace->lnum |
| 7071 | ? trypos->lnum < tryposBrace->lnum |
| 7072 | : trypos->col < tryposBrace->col)) |
| 7073 | trypos = NULL; |
| 7074 | } |
| 7075 | return trypos; |
| 7076 | } |
| 7077 | |
| 7078 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7079 | * Return ind_maxparen corrected for the difference in line number between the |
| 7080 | * cursor position and "startpos". This makes sure that searching for a |
| 7081 | * matching paren above the cursor line doesn't find a match because of |
| 7082 | * looking a few lines further. |
| 7083 | */ |
| 7084 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7085 | corr_ind_maxparen(pos_T *startpos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7086 | { |
| 7087 | long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum; |
| 7088 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7089 | if (n > 0 && n < curbuf->b_ind_maxparen / 2) |
| 7090 | return curbuf->b_ind_maxparen - (int)n; |
| 7091 | return curbuf->b_ind_maxparen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7092 | } |
| 7093 | |
| 7094 | /* |
| 7095 | * 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] | 7096 | * line "l". "l" must point to the start of the line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7097 | */ |
| 7098 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7099 | find_last_paren(char_u *l, int start, int end) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7100 | { |
| 7101 | int i; |
| 7102 | int retval = FALSE; |
| 7103 | int open_count = 0; |
| 7104 | |
| 7105 | curwin->w_cursor.col = 0; /* default is start of line */ |
| 7106 | |
Bram Moolenaar | 6d8f9c6 | 2011-11-30 13:03:28 +0100 | [diff] [blame] | 7107 | for (i = 0; l[i] != NUL; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7108 | { |
| 7109 | i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */ |
| 7110 | i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */ |
| 7111 | if (l[i] == start) |
| 7112 | ++open_count; |
| 7113 | else if (l[i] == end) |
| 7114 | { |
| 7115 | if (open_count > 0) |
| 7116 | --open_count; |
| 7117 | else |
| 7118 | { |
| 7119 | curwin->w_cursor.col = i; |
| 7120 | retval = TRUE; |
| 7121 | } |
| 7122 | } |
| 7123 | } |
| 7124 | return retval; |
| 7125 | } |
| 7126 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7127 | /* |
| 7128 | * Parse 'cinoptions' and set the values in "curbuf". |
| 7129 | * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes. |
| 7130 | */ |
| 7131 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7132 | parse_cino(buf_T *buf) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7133 | { |
| 7134 | char_u *p; |
| 7135 | char_u *l; |
| 7136 | char_u *digits; |
| 7137 | int n; |
| 7138 | int divider; |
| 7139 | int fraction = 0; |
| 7140 | int sw = (int)get_sw_value(buf); |
| 7141 | |
| 7142 | /* |
| 7143 | * Set the default values. |
| 7144 | */ |
| 7145 | /* Spaces from a block's opening brace the prevailing indent for that |
| 7146 | * block should be. */ |
| 7147 | buf->b_ind_level = sw; |
| 7148 | |
| 7149 | /* Spaces from the edge of the line an open brace that's at the end of a |
| 7150 | * line is imagined to be. */ |
| 7151 | buf->b_ind_open_imag = 0; |
| 7152 | |
| 7153 | /* Spaces from the prevailing indent for a line that is not preceded by |
| 7154 | * an opening brace. */ |
| 7155 | buf->b_ind_no_brace = 0; |
| 7156 | |
| 7157 | /* Column where the first { of a function should be located }. */ |
| 7158 | buf->b_ind_first_open = 0; |
| 7159 | |
| 7160 | /* Spaces from the prevailing indent a leftmost open brace should be |
| 7161 | * located. */ |
| 7162 | buf->b_ind_open_extra = 0; |
| 7163 | |
| 7164 | /* Spaces from the matching open brace (real location for one at the left |
| 7165 | * edge; imaginary location from one that ends a line) the matching close |
| 7166 | * brace should be located. */ |
| 7167 | buf->b_ind_close_extra = 0; |
| 7168 | |
| 7169 | /* Spaces from the edge of the line an open brace sitting in the leftmost |
| 7170 | * column is imagined to be. */ |
| 7171 | buf->b_ind_open_left_imag = 0; |
| 7172 | |
| 7173 | /* Spaces jump labels should be shifted to the left if N is non-negative, |
| 7174 | * otherwise the jump label will be put to column 1. */ |
| 7175 | buf->b_ind_jump_label = -1; |
| 7176 | |
| 7177 | /* Spaces from the switch() indent a "case xx" label should be located. */ |
| 7178 | buf->b_ind_case = sw; |
| 7179 | |
| 7180 | /* Spaces from the "case xx:" code after a switch() should be located. */ |
| 7181 | buf->b_ind_case_code = sw; |
| 7182 | |
| 7183 | /* Lineup break at end of case in switch() with case label. */ |
| 7184 | buf->b_ind_case_break = 0; |
| 7185 | |
| 7186 | /* Spaces from the class declaration indent a scope declaration label |
| 7187 | * should be located. */ |
| 7188 | buf->b_ind_scopedecl = sw; |
| 7189 | |
| 7190 | /* Spaces from the scope declaration label code should be located. */ |
| 7191 | buf->b_ind_scopedecl_code = sw; |
| 7192 | |
| 7193 | /* Amount K&R-style parameters should be indented. */ |
| 7194 | buf->b_ind_param = sw; |
| 7195 | |
| 7196 | /* Amount a function type spec should be indented. */ |
| 7197 | buf->b_ind_func_type = sw; |
| 7198 | |
| 7199 | /* Amount a cpp base class declaration or constructor initialization |
| 7200 | * should be indented. */ |
| 7201 | buf->b_ind_cpp_baseclass = sw; |
| 7202 | |
| 7203 | /* additional spaces beyond the prevailing indent a continuation line |
| 7204 | * should be located. */ |
| 7205 | buf->b_ind_continuation = sw; |
| 7206 | |
| 7207 | /* Spaces from the indent of the line with an unclosed parentheses. */ |
| 7208 | buf->b_ind_unclosed = sw * 2; |
| 7209 | |
| 7210 | /* Spaces from the indent of the line with an unclosed parentheses, which |
| 7211 | * itself is also unclosed. */ |
| 7212 | buf->b_ind_unclosed2 = sw; |
| 7213 | |
| 7214 | /* Suppress ignoring spaces from the indent of a line starting with an |
| 7215 | * unclosed parentheses. */ |
| 7216 | buf->b_ind_unclosed_noignore = 0; |
| 7217 | |
| 7218 | /* If the opening paren is the last nonwhite character on the line, and |
| 7219 | * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer |
| 7220 | * context (for very long lines). */ |
| 7221 | buf->b_ind_unclosed_wrapped = 0; |
| 7222 | |
| 7223 | /* Suppress ignoring white space when lining up with the character after |
| 7224 | * an unclosed parentheses. */ |
| 7225 | buf->b_ind_unclosed_whiteok = 0; |
| 7226 | |
| 7227 | /* Indent a closing parentheses under the line start of the matching |
| 7228 | * opening parentheses. */ |
| 7229 | buf->b_ind_matching_paren = 0; |
| 7230 | |
| 7231 | /* Indent a closing parentheses under the previous line. */ |
| 7232 | buf->b_ind_paren_prev = 0; |
| 7233 | |
| 7234 | /* Extra indent for comments. */ |
| 7235 | buf->b_ind_comment = 0; |
| 7236 | |
| 7237 | /* Spaces from the comment opener when there is nothing after it. */ |
| 7238 | buf->b_ind_in_comment = 3; |
| 7239 | |
| 7240 | /* Boolean: if non-zero, use b_ind_in_comment even if there is something |
| 7241 | * after the comment opener. */ |
| 7242 | buf->b_ind_in_comment2 = 0; |
| 7243 | |
| 7244 | /* Max lines to search for an open paren. */ |
| 7245 | buf->b_ind_maxparen = 20; |
| 7246 | |
| 7247 | /* Max lines to search for an open comment. */ |
| 7248 | buf->b_ind_maxcomment = 70; |
| 7249 | |
| 7250 | /* Handle braces for java code. */ |
| 7251 | buf->b_ind_java = 0; |
| 7252 | |
| 7253 | /* Not to confuse JS object properties with labels. */ |
| 7254 | buf->b_ind_js = 0; |
| 7255 | |
| 7256 | /* Handle blocked cases correctly. */ |
| 7257 | buf->b_ind_keep_case_label = 0; |
| 7258 | |
| 7259 | /* Handle C++ namespace. */ |
| 7260 | buf->b_ind_cpp_namespace = 0; |
| 7261 | |
| 7262 | /* Handle continuation lines containing conditions of if(), for() and |
| 7263 | * while(). */ |
| 7264 | buf->b_ind_if_for_while = 0; |
| 7265 | |
Bram Moolenaar | 6b64394 | 2017-03-05 19:44:06 +0100 | [diff] [blame] | 7266 | /* indentation for # comments */ |
| 7267 | buf->b_ind_hash_comment = 0; |
| 7268 | |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 7269 | /* Handle C++ extern "C" or "C++" */ |
| 7270 | buf->b_ind_cpp_extern_c = 0; |
| 7271 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7272 | for (p = buf->b_p_cino; *p; ) |
| 7273 | { |
| 7274 | l = p++; |
| 7275 | if (*p == '-') |
| 7276 | ++p; |
| 7277 | digits = p; /* remember where the digits start */ |
| 7278 | n = getdigits(&p); |
| 7279 | divider = 0; |
| 7280 | if (*p == '.') /* ".5s" means a fraction */ |
| 7281 | { |
| 7282 | fraction = atol((char *)++p); |
| 7283 | while (VIM_ISDIGIT(*p)) |
| 7284 | { |
| 7285 | ++p; |
| 7286 | if (divider) |
| 7287 | divider *= 10; |
| 7288 | else |
| 7289 | divider = 10; |
| 7290 | } |
| 7291 | } |
| 7292 | if (*p == 's') /* "2s" means two times 'shiftwidth' */ |
| 7293 | { |
| 7294 | if (p == digits) |
| 7295 | n = sw; /* just "s" is one 'shiftwidth' */ |
| 7296 | else |
| 7297 | { |
| 7298 | n *= sw; |
| 7299 | if (divider) |
| 7300 | n += (sw * fraction + divider / 2) / divider; |
| 7301 | } |
| 7302 | ++p; |
| 7303 | } |
| 7304 | if (l[1] == '-') |
| 7305 | n = -n; |
| 7306 | |
| 7307 | /* When adding an entry here, also update the default 'cinoptions' in |
| 7308 | * doc/indent.txt, and add explanation for it! */ |
| 7309 | switch (*l) |
| 7310 | { |
| 7311 | case '>': buf->b_ind_level = n; break; |
| 7312 | case 'e': buf->b_ind_open_imag = n; break; |
| 7313 | case 'n': buf->b_ind_no_brace = n; break; |
| 7314 | case 'f': buf->b_ind_first_open = n; break; |
| 7315 | case '{': buf->b_ind_open_extra = n; break; |
| 7316 | case '}': buf->b_ind_close_extra = n; break; |
| 7317 | case '^': buf->b_ind_open_left_imag = n; break; |
| 7318 | case 'L': buf->b_ind_jump_label = n; break; |
| 7319 | case ':': buf->b_ind_case = n; break; |
| 7320 | case '=': buf->b_ind_case_code = n; break; |
| 7321 | case 'b': buf->b_ind_case_break = n; break; |
| 7322 | case 'p': buf->b_ind_param = n; break; |
| 7323 | case 't': buf->b_ind_func_type = n; break; |
| 7324 | case '/': buf->b_ind_comment = n; break; |
| 7325 | case 'c': buf->b_ind_in_comment = n; break; |
| 7326 | case 'C': buf->b_ind_in_comment2 = n; break; |
| 7327 | case 'i': buf->b_ind_cpp_baseclass = n; break; |
| 7328 | case '+': buf->b_ind_continuation = n; break; |
| 7329 | case '(': buf->b_ind_unclosed = n; break; |
| 7330 | case 'u': buf->b_ind_unclosed2 = n; break; |
| 7331 | case 'U': buf->b_ind_unclosed_noignore = n; break; |
| 7332 | case 'W': buf->b_ind_unclosed_wrapped = n; break; |
| 7333 | case 'w': buf->b_ind_unclosed_whiteok = n; break; |
| 7334 | case 'm': buf->b_ind_matching_paren = n; break; |
| 7335 | case 'M': buf->b_ind_paren_prev = n; break; |
| 7336 | case ')': buf->b_ind_maxparen = n; break; |
| 7337 | case '*': buf->b_ind_maxcomment = n; break; |
| 7338 | case 'g': buf->b_ind_scopedecl = n; break; |
| 7339 | case 'h': buf->b_ind_scopedecl_code = n; break; |
| 7340 | case 'j': buf->b_ind_java = n; break; |
| 7341 | case 'J': buf->b_ind_js = n; break; |
| 7342 | case 'l': buf->b_ind_keep_case_label = n; break; |
| 7343 | case '#': buf->b_ind_hash_comment = n; break; |
| 7344 | case 'N': buf->b_ind_cpp_namespace = n; break; |
| 7345 | case 'k': buf->b_ind_if_for_while = n; break; |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 7346 | case 'E': buf->b_ind_cpp_extern_c = n; break; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7347 | } |
| 7348 | if (*p == ',') |
| 7349 | ++p; |
| 7350 | } |
| 7351 | } |
| 7352 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7353 | /* |
| 7354 | * Return the desired indent for C code. |
| 7355 | * Return -1 if the indent should be left alone (inside a raw string). |
| 7356 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7357 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 7358 | get_c_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7359 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7360 | pos_T cur_curpos; |
| 7361 | int amount; |
| 7362 | int scope_amount; |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 7363 | int cur_amount = MAXCOL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7364 | colnr_T col; |
| 7365 | char_u *theline; |
| 7366 | char_u *linecopy; |
| 7367 | pos_T *trypos; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7368 | pos_T *comment_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7369 | pos_T *tryposBrace = NULL; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7370 | pos_T tryposCopy; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7371 | pos_T our_paren_pos; |
| 7372 | char_u *start; |
| 7373 | int start_brace; |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 7374 | #define BRACE_IN_COL0 1 /* '{' is in column 0 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7375 | #define BRACE_AT_START 2 /* '{' is at start of line */ |
| 7376 | #define BRACE_AT_END 3 /* '{' is at end of line */ |
| 7377 | linenr_T ourscope; |
| 7378 | char_u *l; |
| 7379 | char_u *look; |
| 7380 | char_u terminated; |
| 7381 | int lookfor; |
| 7382 | #define LOOKFOR_INITIAL 0 |
| 7383 | #define LOOKFOR_IF 1 |
| 7384 | #define LOOKFOR_DO 2 |
| 7385 | #define LOOKFOR_CASE 3 |
| 7386 | #define LOOKFOR_ANY 4 |
| 7387 | #define LOOKFOR_TERM 5 |
| 7388 | #define LOOKFOR_UNTERM 6 |
| 7389 | #define LOOKFOR_SCOPEDECL 7 |
| 7390 | #define LOOKFOR_NOBREAK 8 |
| 7391 | #define LOOKFOR_CPP_BASECLASS 9 |
| 7392 | #define LOOKFOR_ENUM_OR_INIT 10 |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7393 | #define LOOKFOR_JS_KEY 11 |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 7394 | #define LOOKFOR_COMMA 12 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7395 | |
| 7396 | int whilelevel; |
| 7397 | linenr_T lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7398 | int n; |
| 7399 | int iscase; |
| 7400 | int lookfor_break; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 7401 | int lookfor_cpp_namespace = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7402 | int cont_amount = 0; /* amount for continuation line */ |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 7403 | int original_line_islabel; |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 7404 | int added_to_amount = 0; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7405 | int js_cur_has_key = 0; |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 7406 | linenr_T raw_string_start = 0; |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 7407 | cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } }; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7408 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7409 | /* make a copy, value is changed below */ |
| 7410 | int ind_continuation = curbuf->b_ind_continuation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7411 | |
| 7412 | /* remember where the cursor was when we started */ |
| 7413 | cur_curpos = curwin->w_cursor; |
| 7414 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7415 | /* if we are at line 1 zero indent is fine, right? */ |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 7416 | if (cur_curpos.lnum == 1) |
| 7417 | return 0; |
| 7418 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7419 | /* Get a copy of the current contents of the line. |
| 7420 | * This is required, because only the most recent line obtained with |
| 7421 | * ml_get is valid! */ |
| 7422 | linecopy = vim_strsave(ml_get(cur_curpos.lnum)); |
| 7423 | if (linecopy == NULL) |
| 7424 | return 0; |
| 7425 | |
| 7426 | /* |
| 7427 | * In insert mode and the cursor is on a ')' truncate the line at the |
| 7428 | * cursor position. We don't want to line up with the matching '(' when |
| 7429 | * inserting new stuff. |
| 7430 | * For unknown reasons the cursor might be past the end of the line, thus |
| 7431 | * check for that. |
| 7432 | */ |
| 7433 | if ((State & INSERT) |
Bram Moolenaar | 2c4278f | 2009-05-17 11:33:22 +0000 | [diff] [blame] | 7434 | && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7435 | && linecopy[curwin->w_cursor.col] == ')') |
| 7436 | linecopy[curwin->w_cursor.col] = NUL; |
| 7437 | |
| 7438 | theline = skipwhite(linecopy); |
| 7439 | |
| 7440 | /* move the cursor to the start of the line */ |
| 7441 | |
| 7442 | curwin->w_cursor.col = 0; |
| 7443 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7444 | original_line_islabel = cin_islabel(); /* XXX */ |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 7445 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7446 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7447 | * If we are inside a raw string don't change the indent. |
| 7448 | * Ignore a raw string inside a comment. |
| 7449 | */ |
| 7450 | comment_pos = ind_find_start_comment(); |
| 7451 | if (comment_pos != NULL) |
| 7452 | { |
| 7453 | /* findmatchlimit() static pos is overwritten, make a copy */ |
| 7454 | tryposCopy = *comment_pos; |
| 7455 | comment_pos = &tryposCopy; |
| 7456 | } |
| 7457 | trypos = find_start_rawstring(curbuf->b_ind_maxcomment); |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 7458 | if (trypos != NULL && (comment_pos == NULL |
| 7459 | || LT_POS(*trypos, *comment_pos))) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7460 | { |
| 7461 | amount = -1; |
| 7462 | goto laterend; |
| 7463 | } |
| 7464 | |
| 7465 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7466 | * #defines and so on always go at the left when included in 'cinkeys'. |
| 7467 | */ |
| 7468 | if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE))) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7469 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7470 | amount = curbuf->b_ind_hash_comment; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7471 | goto theend; |
| 7472 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7473 | |
| 7474 | /* |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 7475 | * Is it a non-case label? Then that goes at the left margin too unless: |
| 7476 | * - JS flag is set. |
| 7477 | * - 'L' item has a positive value. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7478 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7479 | if (original_line_islabel && !curbuf->b_ind_js |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7480 | && curbuf->b_ind_jump_label < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7481 | { |
| 7482 | amount = 0; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7483 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7484 | } |
| 7485 | |
| 7486 | /* |
| 7487 | * If we're inside a "//" comment and there is a "//" comment in a |
| 7488 | * previous line, lineup with that one. |
| 7489 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7490 | if (cin_islinecomment(theline) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7491 | && (trypos = find_line_comment()) != NULL) /* XXX */ |
| 7492 | { |
| 7493 | /* find how indented the line beginning the comment is */ |
| 7494 | getvcol(curwin, trypos, &col, NULL, NULL); |
| 7495 | amount = col; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7496 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7497 | } |
| 7498 | |
| 7499 | /* |
| 7500 | * If we're inside a comment and not looking at the start of the |
| 7501 | * comment, try using the 'comments' option. |
| 7502 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7503 | if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7504 | { |
| 7505 | int lead_start_len = 2; |
| 7506 | int lead_middle_len = 1; |
| 7507 | char_u lead_start[COM_MAX_LEN]; /* start-comment string */ |
| 7508 | char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ |
| 7509 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 7510 | char_u *p; |
| 7511 | int start_align = 0; |
| 7512 | int start_off = 0; |
| 7513 | int done = FALSE; |
| 7514 | |
| 7515 | /* find how indented the line beginning the comment is */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7516 | getvcol(curwin, comment_pos, &col, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7517 | amount = col; |
Bram Moolenaar | 4aa9742 | 2011-04-11 14:27:38 +0200 | [diff] [blame] | 7518 | *lead_start = NUL; |
| 7519 | *lead_middle = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7520 | |
| 7521 | p = curbuf->b_p_com; |
| 7522 | while (*p != NUL) |
| 7523 | { |
| 7524 | int align = 0; |
| 7525 | int off = 0; |
| 7526 | int what = 0; |
| 7527 | |
| 7528 | while (*p != NUL && *p != ':') |
| 7529 | { |
| 7530 | if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE) |
| 7531 | what = *p++; |
| 7532 | else if (*p == COM_LEFT || *p == COM_RIGHT) |
| 7533 | align = *p++; |
| 7534 | else if (VIM_ISDIGIT(*p) || *p == '-') |
| 7535 | off = getdigits(&p); |
| 7536 | else |
| 7537 | ++p; |
| 7538 | } |
| 7539 | |
| 7540 | if (*p == ':') |
| 7541 | ++p; |
| 7542 | (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 7543 | if (what == COM_START) |
| 7544 | { |
| 7545 | STRCPY(lead_start, lead_end); |
| 7546 | lead_start_len = (int)STRLEN(lead_start); |
| 7547 | start_off = off; |
| 7548 | start_align = align; |
| 7549 | } |
| 7550 | else if (what == COM_MIDDLE) |
| 7551 | { |
| 7552 | STRCPY(lead_middle, lead_end); |
| 7553 | lead_middle_len = (int)STRLEN(lead_middle); |
| 7554 | } |
| 7555 | else if (what == COM_END) |
| 7556 | { |
| 7557 | /* If our line starts with the middle comment string, line it |
| 7558 | * up with the comment opener per the 'comments' option. */ |
| 7559 | if (STRNCMP(theline, lead_middle, lead_middle_len) == 0 |
| 7560 | && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0) |
| 7561 | { |
| 7562 | done = TRUE; |
| 7563 | if (curwin->w_cursor.lnum > 1) |
| 7564 | { |
| 7565 | /* If the start comment string matches in the previous |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 7566 | * line, use the indent of that line plus offset. If |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7567 | * the middle comment string matches in the previous |
| 7568 | * line, use the indent of that line. XXX */ |
| 7569 | look = skipwhite(ml_get(curwin->w_cursor.lnum - 1)); |
| 7570 | if (STRNCMP(look, lead_start, lead_start_len) == 0) |
| 7571 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 7572 | else if (STRNCMP(look, lead_middle, |
| 7573 | lead_middle_len) == 0) |
| 7574 | { |
| 7575 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 7576 | break; |
| 7577 | } |
| 7578 | /* If the start comment string doesn't match with the |
| 7579 | * start of the comment, skip this entry. XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7580 | else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7581 | lead_start, lead_start_len) != 0) |
| 7582 | continue; |
| 7583 | } |
| 7584 | if (start_off != 0) |
| 7585 | amount += start_off; |
| 7586 | else if (start_align == COM_RIGHT) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 7587 | amount += vim_strsize(lead_start) |
| 7588 | - vim_strsize(lead_middle); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7589 | break; |
| 7590 | } |
| 7591 | |
| 7592 | /* If our line starts with the end comment string, line it up |
| 7593 | * with the middle comment */ |
| 7594 | if (STRNCMP(theline, lead_middle, lead_middle_len) != 0 |
| 7595 | && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0) |
| 7596 | { |
| 7597 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); |
| 7598 | /* XXX */ |
| 7599 | if (off != 0) |
| 7600 | amount += off; |
| 7601 | else if (align == COM_RIGHT) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 7602 | amount += vim_strsize(lead_start) |
| 7603 | - vim_strsize(lead_middle); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7604 | done = TRUE; |
| 7605 | break; |
| 7606 | } |
| 7607 | } |
| 7608 | } |
| 7609 | |
| 7610 | /* If our line starts with an asterisk, line up with the |
| 7611 | * asterisk in the comment opener; otherwise, line up |
| 7612 | * with the first character of the comment text. |
| 7613 | */ |
| 7614 | if (done) |
| 7615 | ; |
| 7616 | else if (theline[0] == '*') |
| 7617 | amount += 1; |
| 7618 | else |
| 7619 | { |
| 7620 | /* |
| 7621 | * If we are more than one line away from the comment opener, take |
| 7622 | * the indent of the previous non-empty line. If 'cino' has "CO" |
| 7623 | * and we are just below the comment opener and there are any |
| 7624 | * white characters after it line up with the text after it; |
| 7625 | * otherwise, add the amount specified by "c" in 'cino' |
| 7626 | */ |
| 7627 | amount = -1; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7628 | for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7629 | { |
| 7630 | if (linewhite(lnum)) /* skip blank lines */ |
| 7631 | continue; |
| 7632 | amount = get_indent_lnum(lnum); /* XXX */ |
| 7633 | break; |
| 7634 | } |
| 7635 | if (amount == -1) /* use the comment opener */ |
| 7636 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7637 | if (!curbuf->b_ind_in_comment2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7638 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7639 | start = ml_get(comment_pos->lnum); |
| 7640 | look = start + comment_pos->col + 2; /* skip / and * */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7641 | if (*look != NUL) /* if something after it */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7642 | comment_pos->col = (colnr_T)(skipwhite(look) - start); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7643 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7644 | getvcol(curwin, comment_pos, &col, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7645 | amount = col; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7646 | if (curbuf->b_ind_in_comment2 || *look == NUL) |
| 7647 | amount += curbuf->b_ind_in_comment; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7648 | } |
| 7649 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7650 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7651 | } |
| 7652 | |
| 7653 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7654 | * Are we looking at a ']' that has a match? |
| 7655 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7656 | if (*skipwhite(theline) == ']' |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7657 | && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL) |
| 7658 | { |
| 7659 | /* align with the line containing the '['. */ |
| 7660 | amount = get_indent_lnum(trypos->lnum); |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7661 | goto theend; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7662 | } |
| 7663 | |
| 7664 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7665 | * Are we inside parentheses or braces? |
| 7666 | */ /* XXX */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7667 | if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7668 | && curbuf->b_ind_java == 0) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7669 | || (tryposBrace = find_start_brace()) != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7670 | || trypos != NULL) |
| 7671 | { |
| 7672 | if (trypos != NULL && tryposBrace != NULL) |
| 7673 | { |
| 7674 | /* Both an unmatched '(' and '{' is found. Use the one which is |
| 7675 | * closer to the current cursor position, set the other to NULL. */ |
| 7676 | if (trypos->lnum != tryposBrace->lnum |
| 7677 | ? trypos->lnum < tryposBrace->lnum |
| 7678 | : trypos->col < tryposBrace->col) |
| 7679 | trypos = NULL; |
| 7680 | else |
| 7681 | tryposBrace = NULL; |
| 7682 | } |
| 7683 | |
| 7684 | if (trypos != NULL) |
| 7685 | { |
| 7686 | /* |
| 7687 | * If the matching paren is more than one line away, use the indent of |
| 7688 | * a previous non-empty line that matches the same paren. |
| 7689 | */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7690 | if (theline[0] == ')' && curbuf->b_ind_paren_prev) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7691 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7692 | /* Line up with the start of the matching paren line. */ |
| 7693 | amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */ |
| 7694 | } |
| 7695 | else |
| 7696 | { |
| 7697 | amount = -1; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7698 | our_paren_pos = *trypos; |
| 7699 | for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7700 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7701 | l = skipwhite(ml_get(lnum)); |
| 7702 | if (cin_nocode(l)) /* skip comment lines */ |
| 7703 | continue; |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 7704 | if (cin_ispreproc_cont(&l, &lnum, &amount)) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7705 | continue; /* ignore #define, #if, etc. */ |
| 7706 | curwin->w_cursor.lnum = lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7707 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7708 | /* Skip a comment or raw string. XXX */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 7709 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7710 | { |
| 7711 | lnum = trypos->lnum + 1; |
| 7712 | continue; |
| 7713 | } |
| 7714 | |
| 7715 | /* XXX */ |
| 7716 | if ((trypos = find_match_paren( |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7717 | corr_ind_maxparen(&cur_curpos))) != NULL |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7718 | && trypos->lnum == our_paren_pos.lnum |
| 7719 | && trypos->col == our_paren_pos.col) |
| 7720 | { |
| 7721 | amount = get_indent_lnum(lnum); /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7722 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7723 | if (theline[0] == ')') |
| 7724 | { |
| 7725 | if (our_paren_pos.lnum != lnum |
| 7726 | && cur_amount > amount) |
| 7727 | cur_amount = amount; |
| 7728 | amount = -1; |
| 7729 | } |
| 7730 | break; |
| 7731 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7732 | } |
| 7733 | } |
| 7734 | |
| 7735 | /* |
| 7736 | * Line up with line where the matching paren is. XXX |
| 7737 | * If the line starts with a '(' or the indent for unclosed |
| 7738 | * parentheses is zero, line up with the unclosed parentheses. |
| 7739 | */ |
| 7740 | if (amount == -1) |
| 7741 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7742 | int ignore_paren_col = 0; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7743 | int is_if_for_while = 0; |
| 7744 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7745 | if (curbuf->b_ind_if_for_while) |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7746 | { |
| 7747 | /* Look for the outermost opening parenthesis on this line |
| 7748 | * and check whether it belongs to an "if", "for" or "while". */ |
| 7749 | |
| 7750 | pos_T cursor_save = curwin->w_cursor; |
| 7751 | pos_T outermost; |
| 7752 | char_u *line; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7753 | |
| 7754 | trypos = &our_paren_pos; |
| 7755 | do { |
| 7756 | outermost = *trypos; |
| 7757 | curwin->w_cursor.lnum = outermost.lnum; |
| 7758 | curwin->w_cursor.col = outermost.col; |
| 7759 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7760 | trypos = find_match_paren(curbuf->b_ind_maxparen); |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7761 | } while (trypos && trypos->lnum == outermost.lnum); |
| 7762 | |
| 7763 | curwin->w_cursor = cursor_save; |
| 7764 | |
| 7765 | line = ml_get(outermost.lnum); |
| 7766 | |
| 7767 | is_if_for_while = |
Bram Moolenaar | b345d49 | 2012-04-09 20:42:26 +0200 | [diff] [blame] | 7768 | cin_is_if_for_while_before_offset(line, &outermost.col); |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7769 | } |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7770 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7771 | amount = skip_label(our_paren_pos.lnum, &look); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7772 | look = skipwhite(look); |
| 7773 | if (*look == '(') |
| 7774 | { |
| 7775 | linenr_T save_lnum = curwin->w_cursor.lnum; |
| 7776 | char_u *line; |
| 7777 | int look_col; |
| 7778 | |
| 7779 | /* Ignore a '(' in front of the line that has a match before |
| 7780 | * our matching '('. */ |
| 7781 | curwin->w_cursor.lnum = our_paren_pos.lnum; |
| 7782 | line = ml_get_curline(); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7783 | look_col = (int)(look - line); |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7784 | curwin->w_cursor.col = look_col + 1; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7785 | if ((trypos = findmatchlimit(NULL, ')', 0, |
| 7786 | curbuf->b_ind_maxparen)) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7787 | != NULL |
| 7788 | && trypos->lnum == our_paren_pos.lnum |
| 7789 | && trypos->col < our_paren_pos.col) |
| 7790 | ignore_paren_col = trypos->col + 1; |
| 7791 | |
| 7792 | curwin->w_cursor.lnum = save_lnum; |
| 7793 | look = ml_get(our_paren_pos.lnum) + look_col; |
| 7794 | } |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7795 | if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0 |
| 7796 | && is_if_for_while == 0) |
| 7797 | || (!curbuf->b_ind_unclosed_noignore && *look == '(' |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7798 | && ignore_paren_col == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7799 | { |
| 7800 | /* |
| 7801 | * If we're looking at a close paren, line up right there; |
| 7802 | * otherwise, line up with the next (non-white) character. |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7803 | * When b_ind_unclosed_wrapped is set and the matching paren is |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7804 | * the last nonwhite character of the line, use either the |
| 7805 | * indent of the current line or the indentation of the next |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7806 | * outer paren and add b_ind_unclosed_wrapped (for very long |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7807 | * lines). |
| 7808 | */ |
| 7809 | if (theline[0] != ')') |
| 7810 | { |
| 7811 | cur_amount = MAXCOL; |
| 7812 | l = ml_get(our_paren_pos.lnum); |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7813 | if (curbuf->b_ind_unclosed_wrapped |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7814 | && cin_ends_in(l, (char_u *)"(", NULL)) |
| 7815 | { |
| 7816 | /* look for opening unmatched paren, indent one level |
| 7817 | * for each additional level */ |
| 7818 | n = 1; |
| 7819 | for (col = 0; col < our_paren_pos.col; ++col) |
| 7820 | { |
| 7821 | switch (l[col]) |
| 7822 | { |
| 7823 | case '(': |
| 7824 | case '{': ++n; |
| 7825 | break; |
| 7826 | |
| 7827 | case ')': |
| 7828 | case '}': if (n > 1) |
| 7829 | --n; |
| 7830 | break; |
| 7831 | } |
| 7832 | } |
| 7833 | |
| 7834 | our_paren_pos.col = 0; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7835 | amount += n * curbuf->b_ind_unclosed_wrapped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7836 | } |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7837 | else if (curbuf->b_ind_unclosed_whiteok) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7838 | our_paren_pos.col++; |
| 7839 | else |
| 7840 | { |
| 7841 | col = our_paren_pos.col + 1; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 7842 | while (VIM_ISWHITE(l[col])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7843 | col++; |
| 7844 | if (l[col] != NUL) /* In case of trailing space */ |
| 7845 | our_paren_pos.col = col; |
| 7846 | else |
| 7847 | our_paren_pos.col++; |
| 7848 | } |
| 7849 | } |
| 7850 | |
| 7851 | /* |
| 7852 | * Find how indented the paren is, or the character after it |
| 7853 | * if we did the above "if". |
| 7854 | */ |
| 7855 | if (our_paren_pos.col > 0) |
| 7856 | { |
| 7857 | getvcol(curwin, &our_paren_pos, &col, NULL, NULL); |
| 7858 | if (cur_amount > (int)col) |
| 7859 | cur_amount = col; |
| 7860 | } |
| 7861 | } |
| 7862 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7863 | if (theline[0] == ')' && curbuf->b_ind_matching_paren) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7864 | { |
| 7865 | /* Line up with the start of the matching paren line. */ |
| 7866 | } |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7867 | else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0) |
| 7868 | || (!curbuf->b_ind_unclosed_noignore |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 7869 | && *look == '(' && ignore_paren_col == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7870 | { |
| 7871 | if (cur_amount != MAXCOL) |
| 7872 | amount = cur_amount; |
| 7873 | } |
| 7874 | else |
| 7875 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7876 | /* Add b_ind_unclosed2 for each '(' before our matching one, |
| 7877 | * but ignore (void) before the line (ignore_paren_col). */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7878 | col = our_paren_pos.col; |
Bram Moolenaar | b21e584 | 2006-04-16 18:30:08 +0000 | [diff] [blame] | 7879 | while ((int)our_paren_pos.col > ignore_paren_col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7880 | { |
| 7881 | --our_paren_pos.col; |
| 7882 | switch (*ml_get_pos(&our_paren_pos)) |
| 7883 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7884 | case '(': amount += curbuf->b_ind_unclosed2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7885 | col = our_paren_pos.col; |
| 7886 | break; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7887 | case ')': amount -= curbuf->b_ind_unclosed2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7888 | col = MAXCOL; |
| 7889 | break; |
| 7890 | } |
| 7891 | } |
| 7892 | |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7893 | /* Use b_ind_unclosed once, when the first '(' is not inside |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7894 | * braces */ |
| 7895 | if (col == MAXCOL) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7896 | amount += curbuf->b_ind_unclosed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7897 | else |
| 7898 | { |
| 7899 | curwin->w_cursor.lnum = our_paren_pos.lnum; |
| 7900 | curwin->w_cursor.col = col; |
Bram Moolenaar | 81439a6 | 2014-07-02 18:27:48 +0200 | [diff] [blame] | 7901 | if (find_match_paren_after_brace(curbuf->b_ind_maxparen) |
| 7902 | != NULL) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7903 | amount += curbuf->b_ind_unclosed2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7904 | else |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7905 | { |
| 7906 | if (is_if_for_while) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7907 | amount += curbuf->b_ind_if_for_while; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7908 | else |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7909 | amount += curbuf->b_ind_unclosed; |
Bram Moolenaar | 3675fa0 | 2012-04-05 17:17:42 +0200 | [diff] [blame] | 7910 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7911 | } |
| 7912 | /* |
| 7913 | * For a line starting with ')' use the minimum of the two |
| 7914 | * positions, to avoid giving it more indent than the previous |
| 7915 | * lines: |
| 7916 | * func_long_name( if (x |
| 7917 | * arg && yy |
| 7918 | * ) ^ not here ) ^ not here |
| 7919 | */ |
| 7920 | if (cur_amount < amount) |
| 7921 | amount = cur_amount; |
| 7922 | } |
| 7923 | } |
| 7924 | |
| 7925 | /* add extra indent for a comment */ |
| 7926 | if (cin_iscomment(theline)) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 7927 | amount += curbuf->b_ind_comment; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7928 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7929 | else |
| 7930 | { |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7931 | /* |
| 7932 | * We are inside braces, there is a { before this line at the position |
| 7933 | * stored in tryposBrace. |
Bram Moolenaar | 04d17ae | 2014-08-06 17:44:14 +0200 | [diff] [blame] | 7934 | * Make a copy of tryposBrace, it may point to pos_copy inside |
| 7935 | * find_start_brace(), which may be changed somewhere. |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7936 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 7937 | tryposCopy = *tryposBrace; |
| 7938 | tryposBrace = &tryposCopy; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7939 | trypos = tryposBrace; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7940 | ourscope = trypos->lnum; |
| 7941 | start = ml_get(ourscope); |
| 7942 | |
| 7943 | /* |
| 7944 | * Now figure out how indented the line is in general. |
| 7945 | * If the brace was at the start of the line, we use that; |
| 7946 | * otherwise, check out the indentation of the line as |
| 7947 | * a whole and then add the "imaginary indent" to that. |
| 7948 | */ |
| 7949 | look = skipwhite(start); |
| 7950 | if (*look == '{') |
| 7951 | { |
| 7952 | getvcol(curwin, trypos, &col, NULL, NULL); |
| 7953 | amount = col; |
| 7954 | if (*start == '{') |
| 7955 | start_brace = BRACE_IN_COL0; |
| 7956 | else |
| 7957 | start_brace = BRACE_AT_START; |
| 7958 | } |
| 7959 | else |
| 7960 | { |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7961 | /* That opening brace might have been on a continuation |
| 7962 | * line. if so, find the start of the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7963 | curwin->w_cursor.lnum = ourscope; |
| 7964 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7965 | /* Position the cursor over the rightmost paren, so that |
| 7966 | * matching it will take us back to the start of the line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7967 | lnum = ourscope; |
| 7968 | if (find_last_paren(start, '(', ')') |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7969 | && (trypos = find_match_paren(curbuf->b_ind_maxparen)) |
| 7970 | != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7971 | lnum = trypos->lnum; |
| 7972 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7973 | /* It could have been something like |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7974 | * case 1: if (asdf && |
| 7975 | * ldfd) { |
| 7976 | * } |
| 7977 | */ |
Bram Moolenaar | e7eb789 | 2014-07-02 17:02:36 +0200 | [diff] [blame] | 7978 | if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label) |
| 7979 | && cin_iscase(skipwhite(ml_get_curline()), FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7980 | amount = get_indent(); |
Bram Moolenaar | e7eb789 | 2014-07-02 17:02:36 +0200 | [diff] [blame] | 7981 | else if (curbuf->b_ind_js) |
| 7982 | amount = get_indent_lnum(lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7983 | else |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7984 | amount = skip_label(lnum, &l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7985 | |
| 7986 | start_brace = BRACE_AT_END; |
| 7987 | } |
| 7988 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7989 | /* For Javascript check if the line starts with "key:". */ |
| 7990 | if (curbuf->b_ind_js) |
| 7991 | js_cur_has_key = cin_has_js_key(theline); |
| 7992 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7993 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 7994 | * If we're looking at a closing brace, that's where |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7995 | * we want to be. otherwise, add the amount of room |
| 7996 | * that an indent is supposed to be. |
| 7997 | */ |
| 7998 | if (theline[0] == '}') |
| 7999 | { |
| 8000 | /* |
| 8001 | * they may want closing braces to line up with something |
| 8002 | * other than the open brace. indulge them, if so. |
| 8003 | */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8004 | amount += curbuf->b_ind_close_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8005 | } |
| 8006 | else |
| 8007 | { |
| 8008 | /* |
| 8009 | * If we're looking at an "else", try to find an "if" |
| 8010 | * to match it with. |
| 8011 | * If we're looking at a "while", try to find a "do" |
| 8012 | * to match it with. |
| 8013 | */ |
| 8014 | lookfor = LOOKFOR_INITIAL; |
| 8015 | if (cin_iselse(theline)) |
| 8016 | lookfor = LOOKFOR_IF; |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8017 | else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8018 | lookfor = LOOKFOR_DO; |
| 8019 | if (lookfor != LOOKFOR_INITIAL) |
| 8020 | { |
| 8021 | curwin->w_cursor.lnum = cur_curpos.lnum; |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8022 | if (find_match(lookfor, ourscope) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8023 | { |
| 8024 | amount = get_indent(); /* XXX */ |
| 8025 | goto theend; |
| 8026 | } |
| 8027 | } |
| 8028 | |
| 8029 | /* |
| 8030 | * We get here if we are not on an "while-of-do" or "else" (or |
| 8031 | * failed to find a matching "if"). |
| 8032 | * Search backwards for something to line up with. |
| 8033 | * First set amount for when we don't find anything. |
| 8034 | */ |
| 8035 | |
| 8036 | /* |
| 8037 | * if the '{' is _really_ at the left margin, use the imaginary |
| 8038 | * location of a left-margin brace. Otherwise, correct the |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8039 | * location for b_ind_open_extra. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8040 | */ |
| 8041 | |
| 8042 | if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */ |
| 8043 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8044 | amount = curbuf->b_ind_open_left_imag; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 8045 | lookfor_cpp_namespace = TRUE; |
| 8046 | } |
| 8047 | else if (start_brace == BRACE_AT_START && |
| 8048 | lookfor_cpp_namespace) /* '{' is at start */ |
| 8049 | { |
| 8050 | |
| 8051 | lookfor_cpp_namespace = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8052 | } |
| 8053 | else |
| 8054 | { |
| 8055 | if (start_brace == BRACE_AT_END) /* '{' is at end of line */ |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 8056 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8057 | amount += curbuf->b_ind_open_imag; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 8058 | |
| 8059 | l = skipwhite(ml_get_curline()); |
| 8060 | if (cin_is_cpp_namespace(l)) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8061 | amount += curbuf->b_ind_cpp_namespace; |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 8062 | else if (cin_is_cpp_extern_c(l)) |
| 8063 | amount += curbuf->b_ind_cpp_extern_c; |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 8064 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8065 | else |
| 8066 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8067 | /* Compensate for adding b_ind_open_extra later. */ |
| 8068 | amount -= curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8069 | if (amount < 0) |
| 8070 | amount = 0; |
| 8071 | } |
| 8072 | } |
| 8073 | |
| 8074 | lookfor_break = FALSE; |
| 8075 | |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8076 | if (cin_iscase(theline, FALSE)) /* it's a switch() label */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8077 | { |
| 8078 | lookfor = LOOKFOR_CASE; /* find a previous switch() label */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8079 | amount += curbuf->b_ind_case; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8080 | } |
| 8081 | else if (cin_isscopedecl(theline)) /* private:, ... */ |
| 8082 | { |
| 8083 | lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8084 | amount += curbuf->b_ind_scopedecl; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8085 | } |
| 8086 | else |
| 8087 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8088 | if (curbuf->b_ind_case_break && cin_isbreak(theline)) |
| 8089 | /* break; ... */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8090 | lookfor_break = TRUE; |
| 8091 | |
| 8092 | lookfor = LOOKFOR_INITIAL; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8093 | /* b_ind_level from start of block */ |
| 8094 | amount += curbuf->b_ind_level; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8095 | } |
| 8096 | scope_amount = amount; |
| 8097 | whilelevel = 0; |
| 8098 | |
| 8099 | /* |
| 8100 | * Search backwards. If we find something we recognize, line up |
| 8101 | * with that. |
| 8102 | * |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8103 | * If we're looking at an open brace, indent |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8104 | * the usual amount relative to the conditional |
| 8105 | * that opens the block. |
| 8106 | */ |
| 8107 | curwin->w_cursor = cur_curpos; |
| 8108 | for (;;) |
| 8109 | { |
| 8110 | curwin->w_cursor.lnum--; |
| 8111 | curwin->w_cursor.col = 0; |
| 8112 | |
| 8113 | /* |
| 8114 | * If we went all the way back to the start of our scope, line |
| 8115 | * up with it. |
| 8116 | */ |
| 8117 | if (curwin->w_cursor.lnum <= ourscope) |
| 8118 | { |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8119 | /* We reached end of scope: |
| 8120 | * If looking for a enum or structure initialization |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8121 | * go further back: |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8122 | * If it is an initializer (enum xxx or xxx =), then |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8123 | * don't add ind_continuation, otherwise it is a variable |
| 8124 | * declaration: |
| 8125 | * int x, |
| 8126 | * here; <-- add ind_continuation |
| 8127 | */ |
| 8128 | if (lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8129 | { |
| 8130 | if (curwin->w_cursor.lnum == 0 |
| 8131 | || curwin->w_cursor.lnum |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8132 | < ourscope - curbuf->b_ind_maxparen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8133 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8134 | /* nothing found (abuse curbuf->b_ind_maxparen as |
| 8135 | * limit) assume terminated line (i.e. a variable |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8136 | * initialization) */ |
| 8137 | if (cont_amount > 0) |
| 8138 | amount = cont_amount; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8139 | else if (!curbuf->b_ind_js) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8140 | amount += ind_continuation; |
| 8141 | break; |
| 8142 | } |
| 8143 | |
| 8144 | l = ml_get_curline(); |
| 8145 | |
| 8146 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8147 | * If we're in a comment or raw string now, skip to |
| 8148 | * the start of it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8149 | */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 8150 | trypos = ind_find_start_CORS(NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8151 | if (trypos != NULL) |
| 8152 | { |
| 8153 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8154 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8155 | continue; |
| 8156 | } |
| 8157 | |
| 8158 | /* |
| 8159 | * Skip preprocessor directives and blank lines. |
| 8160 | */ |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8161 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, |
| 8162 | &amount)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8163 | continue; |
| 8164 | |
| 8165 | if (cin_nocode(l)) |
| 8166 | continue; |
| 8167 | |
| 8168 | terminated = cin_isterminated(l, FALSE, TRUE); |
| 8169 | |
| 8170 | /* |
| 8171 | * If we are at top level and the line looks like a |
| 8172 | * function declaration, we are done |
| 8173 | * (it's a variable declaration). |
| 8174 | */ |
| 8175 | if (start_brace != BRACE_IN_COL0 |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8176 | || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8177 | { |
| 8178 | /* if the line is terminated with another ',' |
| 8179 | * it is a continued variable initialization. |
| 8180 | * don't add extra indent. |
| 8181 | * TODO: does not work, if a function |
| 8182 | * declaration is split over multiple lines: |
| 8183 | * cin_isfuncdecl returns FALSE then. |
| 8184 | */ |
| 8185 | if (terminated == ',') |
| 8186 | break; |
| 8187 | |
| 8188 | /* if it es a enum declaration or an assignment, |
| 8189 | * we are done. |
| 8190 | */ |
| 8191 | if (terminated != ';' && cin_isinit()) |
| 8192 | break; |
| 8193 | |
| 8194 | /* nothing useful found */ |
| 8195 | if (terminated == 0 || terminated == '{') |
| 8196 | continue; |
| 8197 | } |
| 8198 | |
| 8199 | if (terminated != ';') |
| 8200 | { |
| 8201 | /* Skip parens and braces. Position the cursor |
| 8202 | * over the rightmost paren, so that matching it |
| 8203 | * will take us back to the start of the line. |
| 8204 | */ /* XXX */ |
| 8205 | trypos = NULL; |
| 8206 | if (find_last_paren(l, '(', ')')) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8207 | trypos = find_match_paren( |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8208 | curbuf->b_ind_maxparen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8209 | |
| 8210 | if (trypos == NULL && find_last_paren(l, '{', '}')) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8211 | trypos = find_start_brace(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8212 | |
| 8213 | if (trypos != NULL) |
| 8214 | { |
| 8215 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8216 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8217 | continue; |
| 8218 | } |
| 8219 | } |
| 8220 | |
| 8221 | /* it's a variable declaration, add indentation |
| 8222 | * like in |
| 8223 | * int a, |
| 8224 | * b; |
| 8225 | */ |
| 8226 | if (cont_amount > 0) |
| 8227 | amount = cont_amount; |
| 8228 | else |
| 8229 | amount += ind_continuation; |
| 8230 | } |
| 8231 | else if (lookfor == LOOKFOR_UNTERM) |
| 8232 | { |
| 8233 | if (cont_amount > 0) |
| 8234 | amount = cont_amount; |
| 8235 | else |
| 8236 | amount += ind_continuation; |
| 8237 | } |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8238 | else |
Bram Moolenaar | ed38b0a | 2011-05-25 15:16:18 +0200 | [diff] [blame] | 8239 | { |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8240 | if (lookfor != LOOKFOR_TERM |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8241 | && lookfor != LOOKFOR_CPP_BASECLASS |
| 8242 | && lookfor != LOOKFOR_COMMA) |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8243 | { |
| 8244 | amount = scope_amount; |
| 8245 | if (theline[0] == '{') |
| 8246 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8247 | amount += curbuf->b_ind_open_extra; |
| 8248 | added_to_amount = curbuf->b_ind_open_extra; |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8249 | } |
| 8250 | } |
| 8251 | |
| 8252 | if (lookfor_cpp_namespace) |
| 8253 | { |
| 8254 | /* |
| 8255 | * Looking for C++ namespace, need to look further |
| 8256 | * back. |
| 8257 | */ |
| 8258 | if (curwin->w_cursor.lnum == ourscope) |
| 8259 | continue; |
| 8260 | |
| 8261 | if (curwin->w_cursor.lnum == 0 |
| 8262 | || curwin->w_cursor.lnum |
| 8263 | < ourscope - FIND_NAMESPACE_LIM) |
| 8264 | break; |
| 8265 | |
| 8266 | l = ml_get_curline(); |
| 8267 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8268 | /* If we're in a comment or raw string now, skip |
| 8269 | * to the start of it. */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 8270 | trypos = ind_find_start_CORS(NULL); |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8271 | if (trypos != NULL) |
| 8272 | { |
| 8273 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 8274 | curwin->w_cursor.col = 0; |
| 8275 | continue; |
| 8276 | } |
| 8277 | |
| 8278 | /* Skip preprocessor directives and blank lines. */ |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8279 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, |
| 8280 | &amount)) |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8281 | continue; |
| 8282 | |
| 8283 | /* Finally the actual check for "namespace". */ |
| 8284 | if (cin_is_cpp_namespace(l)) |
| 8285 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8286 | amount += curbuf->b_ind_cpp_namespace |
| 8287 | - added_to_amount; |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8288 | break; |
| 8289 | } |
Bram Moolenaar | 7720ba8 | 2017-03-08 22:19:26 +0100 | [diff] [blame] | 8290 | else if (cin_is_cpp_extern_c(l)) |
| 8291 | { |
| 8292 | amount += curbuf->b_ind_cpp_extern_c |
| 8293 | - added_to_amount; |
| 8294 | break; |
| 8295 | } |
Bram Moolenaar | e79d153 | 2011-10-04 18:03:47 +0200 | [diff] [blame] | 8296 | |
| 8297 | if (cin_nocode(l)) |
| 8298 | continue; |
| 8299 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8300 | } |
| 8301 | break; |
| 8302 | } |
| 8303 | |
| 8304 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 8305 | * If we're in a comment or raw string now, skip to the start |
| 8306 | * of it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8307 | */ /* XXX */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 8308 | if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8309 | { |
| 8310 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8311 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8312 | continue; |
| 8313 | } |
| 8314 | |
| 8315 | l = ml_get_curline(); |
| 8316 | |
| 8317 | /* |
| 8318 | * If this is a switch() label, may line up relative to that. |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8319 | * If this is a C++ scope declaration, do the same. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8320 | */ |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8321 | iscase = cin_iscase(l, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8322 | if (iscase || cin_isscopedecl(l)) |
| 8323 | { |
| 8324 | /* we are only looking for cpp base class |
| 8325 | * declaration/initialization any longer */ |
| 8326 | if (lookfor == LOOKFOR_CPP_BASECLASS) |
| 8327 | break; |
| 8328 | |
| 8329 | /* When looking for a "do" we are not interested in |
| 8330 | * labels. */ |
| 8331 | if (whilelevel > 0) |
| 8332 | continue; |
| 8333 | |
| 8334 | /* |
| 8335 | * case xx: |
| 8336 | * c = 99 + <- this indent plus continuation |
| 8337 | *-> here; |
| 8338 | */ |
| 8339 | if (lookfor == LOOKFOR_UNTERM |
| 8340 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8341 | { |
| 8342 | if (cont_amount > 0) |
| 8343 | amount = cont_amount; |
| 8344 | else |
| 8345 | amount += ind_continuation; |
| 8346 | break; |
| 8347 | } |
| 8348 | |
| 8349 | /* |
| 8350 | * case xx: <- line up with this case |
| 8351 | * x = 333; |
| 8352 | * case yy: |
| 8353 | */ |
| 8354 | if ( (iscase && lookfor == LOOKFOR_CASE) |
| 8355 | || (iscase && lookfor_break) |
| 8356 | || (!iscase && lookfor == LOOKFOR_SCOPEDECL)) |
| 8357 | { |
| 8358 | /* |
| 8359 | * Check that this case label is not for another |
| 8360 | * switch() |
| 8361 | */ /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8362 | if ((trypos = find_start_brace()) == NULL |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8363 | || trypos->lnum == ourscope) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8364 | { |
| 8365 | amount = get_indent(); /* XXX */ |
| 8366 | break; |
| 8367 | } |
| 8368 | continue; |
| 8369 | } |
| 8370 | |
| 8371 | n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */ |
| 8372 | |
| 8373 | /* |
| 8374 | * case xx: if (cond) <- line up with this if |
| 8375 | * y = y + 1; |
| 8376 | * -> s = 99; |
| 8377 | * |
| 8378 | * case xx: |
| 8379 | * if (cond) <- line up with this line |
| 8380 | * y = y + 1; |
| 8381 | * -> s = 99; |
| 8382 | */ |
| 8383 | if (lookfor == LOOKFOR_TERM) |
| 8384 | { |
| 8385 | if (n) |
| 8386 | amount = n; |
| 8387 | |
| 8388 | if (!lookfor_break) |
| 8389 | break; |
| 8390 | } |
| 8391 | |
| 8392 | /* |
| 8393 | * case xx: x = x + 1; <- line up with this x |
| 8394 | * -> y = y + 1; |
| 8395 | * |
| 8396 | * case xx: if (cond) <- line up with this if |
| 8397 | * -> y = y + 1; |
| 8398 | */ |
| 8399 | if (n) |
| 8400 | { |
| 8401 | amount = n; |
| 8402 | l = after_label(ml_get_curline()); |
| 8403 | if (l != NULL && cin_is_cinword(l)) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 8404 | { |
| 8405 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8406 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 8407 | else |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8408 | amount += curbuf->b_ind_level |
| 8409 | + curbuf->b_ind_no_brace; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 8410 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8411 | break; |
| 8412 | } |
| 8413 | |
| 8414 | /* |
| 8415 | * Try to get the indent of a statement before the switch |
| 8416 | * label. If nothing is found, line up relative to the |
| 8417 | * switch label. |
| 8418 | * break; <- may line up with this line |
| 8419 | * case xx: |
| 8420 | * -> y = 1; |
| 8421 | */ |
| 8422 | scope_amount = get_indent() + (iscase /* XXX */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8423 | ? curbuf->b_ind_case_code |
| 8424 | : curbuf->b_ind_scopedecl_code); |
| 8425 | lookfor = curbuf->b_ind_case_break |
| 8426 | ? LOOKFOR_NOBREAK : LOOKFOR_ANY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8427 | continue; |
| 8428 | } |
| 8429 | |
| 8430 | /* |
| 8431 | * Looking for a switch() label or C++ scope declaration, |
| 8432 | * ignore other lines, skip {}-blocks. |
| 8433 | */ |
| 8434 | if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL) |
| 8435 | { |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8436 | if (find_last_paren(l, '{', '}') |
| 8437 | && (trypos = find_start_brace()) != NULL) |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8438 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8439 | curwin->w_cursor.lnum = trypos->lnum + 1; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8440 | curwin->w_cursor.col = 0; |
| 8441 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8442 | continue; |
| 8443 | } |
| 8444 | |
| 8445 | /* |
| 8446 | * Ignore jump labels with nothing after them. |
| 8447 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8448 | if (!curbuf->b_ind_js && cin_islabel()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8449 | { |
| 8450 | l = after_label(ml_get_curline()); |
| 8451 | if (l == NULL || cin_nocode(l)) |
| 8452 | continue; |
| 8453 | } |
| 8454 | |
| 8455 | /* |
| 8456 | * Ignore #defines, #if, etc. |
| 8457 | * Ignore comment and empty lines. |
| 8458 | * (need to get the line again, cin_islabel() may have |
| 8459 | * unlocked it) |
| 8460 | */ |
| 8461 | l = ml_get_curline(); |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 8462 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8463 | || cin_nocode(l)) |
| 8464 | continue; |
| 8465 | |
| 8466 | /* |
| 8467 | * Are we at the start of a cpp base class declaration or |
| 8468 | * constructor initialization? |
| 8469 | */ /* XXX */ |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8470 | n = FALSE; |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8471 | if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0) |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8472 | { |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 8473 | n = cin_is_cpp_baseclass(&cache_cpp_baseclass); |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8474 | l = ml_get_curline(); |
| 8475 | } |
| 8476 | if (n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8477 | { |
| 8478 | if (lookfor == LOOKFOR_UNTERM) |
| 8479 | { |
| 8480 | if (cont_amount > 0) |
| 8481 | amount = cont_amount; |
| 8482 | else |
| 8483 | amount += ind_continuation; |
| 8484 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8485 | else if (theline[0] == '{') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8486 | { |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8487 | /* Need to find start of the declaration. */ |
| 8488 | lookfor = LOOKFOR_UNTERM; |
| 8489 | ind_continuation = 0; |
| 8490 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8491 | } |
| 8492 | else |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8493 | /* XXX */ |
Bram Moolenaar | 4032cfd | 2015-05-04 17:50:33 +0200 | [diff] [blame] | 8494 | amount = get_baseclass_amount( |
| 8495 | cache_cpp_baseclass.lpos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8496 | break; |
| 8497 | } |
| 8498 | else if (lookfor == LOOKFOR_CPP_BASECLASS) |
| 8499 | { |
| 8500 | /* only look, whether there is a cpp base class |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 8501 | * declaration or initialization before the opening brace. |
| 8502 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8503 | if (cin_isterminated(l, TRUE, FALSE)) |
| 8504 | break; |
| 8505 | else |
| 8506 | continue; |
| 8507 | } |
| 8508 | |
| 8509 | /* |
| 8510 | * What happens next depends on the line being terminated. |
| 8511 | * If terminated with a ',' only consider it terminating if |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 8512 | * there is another unterminated statement behind, eg: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8513 | * 123, |
| 8514 | * sizeof |
| 8515 | * here |
| 8516 | * Otherwise check whether it is a enumeration or structure |
| 8517 | * initialisation (not indented) or a variable declaration |
| 8518 | * (indented). |
| 8519 | */ |
| 8520 | terminated = cin_isterminated(l, FALSE, TRUE); |
| 8521 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8522 | if (js_cur_has_key) |
| 8523 | { |
| 8524 | js_cur_has_key = 0; /* only check the first line */ |
| 8525 | if (curbuf->b_ind_js && terminated == ',') |
| 8526 | { |
| 8527 | /* For Javascript we might be inside an object: |
| 8528 | * key: something, <- align with this |
| 8529 | * key: something |
| 8530 | * or: |
| 8531 | * key: something + <- align with this |
| 8532 | * something, |
| 8533 | * key: something |
| 8534 | */ |
| 8535 | lookfor = LOOKFOR_JS_KEY; |
| 8536 | } |
| 8537 | } |
| 8538 | if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l)) |
| 8539 | { |
| 8540 | amount = get_indent(); |
| 8541 | break; |
| 8542 | } |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8543 | if (lookfor == LOOKFOR_COMMA) |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8544 | { |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8545 | if (tryposBrace != NULL && tryposBrace->lnum |
| 8546 | >= curwin->w_cursor.lnum) |
| 8547 | break; |
| 8548 | if (terminated == ',') |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8549 | /* line below current line is the one that starts a |
| 8550 | * (possibly broken) line ending in a comma. */ |
| 8551 | break; |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8552 | else |
| 8553 | { |
| 8554 | amount = get_indent(); |
| 8555 | if (curwin->w_cursor.lnum - 1 == ourscope) |
| 8556 | /* line above is start of the scope, thus current |
| 8557 | * line is the one that stars a (possibly broken) |
| 8558 | * line ending in a comma. */ |
| 8559 | break; |
| 8560 | } |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8561 | } |
| 8562 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8563 | if (terminated == 0 || (lookfor != LOOKFOR_UNTERM |
| 8564 | && terminated == ',')) |
| 8565 | { |
Bram Moolenaar | 089af18 | 2015-10-07 11:41:49 +0200 | [diff] [blame] | 8566 | if (lookfor != LOOKFOR_ENUM_OR_INIT && |
| 8567 | (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '[')) |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8568 | amount += ind_continuation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8569 | /* |
| 8570 | * if we're in the middle of a paren thing, |
| 8571 | * go back to the line that starts it so |
| 8572 | * we can get the right prevailing indent |
| 8573 | * if ( foo && |
| 8574 | * bar ) |
| 8575 | */ |
| 8576 | /* |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8577 | * Position the cursor over the rightmost paren, so that |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8578 | * matching it will take us back to the start of the line. |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8579 | * Ignore a match before the start of the block. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8580 | */ |
| 8581 | (void)find_last_paren(l, '(', ')'); |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8582 | trypos = find_match_paren(corr_ind_maxparen(&cur_curpos)); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8583 | if (trypos != NULL && (trypos->lnum < tryposBrace->lnum |
| 8584 | || (trypos->lnum == tryposBrace->lnum |
| 8585 | && trypos->col < tryposBrace->col))) |
| 8586 | trypos = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8587 | |
| 8588 | /* |
| 8589 | * If we are looking for ',', we also look for matching |
| 8590 | * braces. |
| 8591 | */ |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8592 | if (trypos == NULL && terminated == ',' |
| 8593 | && find_last_paren(l, '{', '}')) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8594 | trypos = find_start_brace(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8595 | |
| 8596 | if (trypos != NULL) |
| 8597 | { |
| 8598 | /* |
| 8599 | * Check if we are on a case label now. This is |
| 8600 | * handled above. |
| 8601 | * case xx: if ( asdf && |
| 8602 | * asdf) |
| 8603 | */ |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8604 | curwin->w_cursor = *trypos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8605 | l = ml_get_curline(); |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8606 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8607 | { |
| 8608 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8609 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8610 | continue; |
| 8611 | } |
| 8612 | } |
| 8613 | |
| 8614 | /* |
| 8615 | * Skip over continuation lines to find the one to get the |
| 8616 | * indent from |
| 8617 | * char *usethis = "bla\ |
| 8618 | * bla", |
| 8619 | * here; |
| 8620 | */ |
| 8621 | if (terminated == ',') |
| 8622 | { |
| 8623 | while (curwin->w_cursor.lnum > 1) |
| 8624 | { |
| 8625 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 8626 | if (*l == NUL || l[STRLEN(l) - 1] != '\\') |
| 8627 | break; |
| 8628 | --curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 8629 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8630 | } |
| 8631 | } |
| 8632 | |
| 8633 | /* |
| 8634 | * Get indent and pointer to text for current line, |
| 8635 | * ignoring any jump label. XXX |
| 8636 | */ |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8637 | if (curbuf->b_ind_js) |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 8638 | cur_amount = get_indent(); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8639 | else |
| 8640 | cur_amount = skip_label(curwin->w_cursor.lnum, &l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8641 | /* |
| 8642 | * If this is just above the line we are indenting, and it |
| 8643 | * starts with a '{', line it up with this line. |
| 8644 | * while (not) |
| 8645 | * -> { |
| 8646 | * } |
| 8647 | */ |
| 8648 | if (terminated != ',' && lookfor != LOOKFOR_TERM |
| 8649 | && theline[0] == '{') |
| 8650 | { |
| 8651 | amount = cur_amount; |
| 8652 | /* |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8653 | * Only add b_ind_open_extra when the current line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8654 | * doesn't start with a '{', which must have a match |
| 8655 | * in the same line (scope is the same). Probably: |
| 8656 | * { 1, 2 }, |
| 8657 | * -> { 3, 4 } |
| 8658 | */ |
| 8659 | if (*skipwhite(l) != '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8660 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8661 | |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8662 | if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8663 | { |
| 8664 | /* have to look back, whether it is a cpp base |
| 8665 | * class declaration or initialization */ |
| 8666 | lookfor = LOOKFOR_CPP_BASECLASS; |
| 8667 | continue; |
| 8668 | } |
| 8669 | break; |
| 8670 | } |
| 8671 | |
| 8672 | /* |
| 8673 | * Check if we are after an "if", "while", etc. |
| 8674 | * Also allow " } else". |
| 8675 | */ |
| 8676 | if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) |
| 8677 | { |
| 8678 | /* |
| 8679 | * Found an unterminated line after an if (), line up |
| 8680 | * with the last one. |
| 8681 | * if (cond) |
| 8682 | * 100 + |
| 8683 | * -> here; |
| 8684 | */ |
| 8685 | if (lookfor == LOOKFOR_UNTERM |
| 8686 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8687 | { |
| 8688 | if (cont_amount > 0) |
| 8689 | amount = cont_amount; |
| 8690 | else |
| 8691 | amount += ind_continuation; |
| 8692 | break; |
| 8693 | } |
| 8694 | |
| 8695 | /* |
| 8696 | * If this is just above the line we are indenting, we |
| 8697 | * are finished. |
| 8698 | * while (not) |
| 8699 | * -> here; |
| 8700 | * Otherwise this indent can be used when the line |
| 8701 | * before this is terminated. |
| 8702 | * yyy; |
| 8703 | * if (stat) |
| 8704 | * while (not) |
| 8705 | * xxx; |
| 8706 | * -> here; |
| 8707 | */ |
| 8708 | amount = cur_amount; |
| 8709 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8710 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8711 | if (lookfor != LOOKFOR_TERM) |
| 8712 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8713 | amount += curbuf->b_ind_level |
| 8714 | + curbuf->b_ind_no_brace; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8715 | break; |
| 8716 | } |
| 8717 | |
| 8718 | /* |
| 8719 | * Special trick: when expecting the while () after a |
| 8720 | * do, line up with the while() |
| 8721 | * do |
| 8722 | * x = 1; |
| 8723 | * -> here |
| 8724 | */ |
| 8725 | l = skipwhite(ml_get_curline()); |
| 8726 | if (cin_isdo(l)) |
| 8727 | { |
| 8728 | if (whilelevel == 0) |
| 8729 | break; |
| 8730 | --whilelevel; |
| 8731 | } |
| 8732 | |
| 8733 | /* |
| 8734 | * When searching for a terminated line, don't use the |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8735 | * one between the "if" and the matching "else". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8736 | * Need to use the scope of this "else". XXX |
| 8737 | * If whilelevel != 0 continue looking for a "do {". |
| 8738 | */ |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8739 | if (cin_iselse(l) && whilelevel == 0) |
| 8740 | { |
| 8741 | /* If we're looking at "} else", let's make sure we |
| 8742 | * find the opening brace of the enclosing scope, |
| 8743 | * not the one from "if () {". */ |
| 8744 | if (*l == '}') |
| 8745 | curwin->w_cursor.col = |
Bram Moolenaar | 9b83c2f | 2011-05-25 17:29:44 +0200 | [diff] [blame] | 8746 | (colnr_T)(l - ml_get_curline()) + 1; |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8747 | |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 8748 | if ((trypos = find_start_brace()) == NULL |
| 8749 | || find_match(LOOKFOR_IF, trypos->lnum) |
| 8750 | == FAIL) |
Bram Moolenaar | 334adf0 | 2011-05-25 13:34:04 +0200 | [diff] [blame] | 8751 | break; |
| 8752 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8753 | } |
| 8754 | |
| 8755 | /* |
| 8756 | * If we're below an unterminated line that is not an |
| 8757 | * "if" or something, we may line up with this line or |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 8758 | * add something for a continuation line, depending on |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8759 | * the line before this one. |
| 8760 | */ |
| 8761 | else |
| 8762 | { |
| 8763 | /* |
| 8764 | * Found two unterminated lines on a row, line up with |
| 8765 | * the last one. |
| 8766 | * c = 99 + |
| 8767 | * 100 + |
| 8768 | * -> here; |
| 8769 | */ |
| 8770 | if (lookfor == LOOKFOR_UNTERM) |
| 8771 | { |
| 8772 | /* When line ends in a comma add extra indent */ |
| 8773 | if (terminated == ',') |
| 8774 | amount += ind_continuation; |
| 8775 | break; |
| 8776 | } |
| 8777 | |
| 8778 | if (lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8779 | { |
| 8780 | /* Found two lines ending in ',', lineup with the |
| 8781 | * lowest one, but check for cpp base class |
| 8782 | * declaration/initialization, if it is an |
| 8783 | * opening brace or we are looking just for |
| 8784 | * enumerations/initializations. */ |
| 8785 | if (terminated == ',') |
| 8786 | { |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8787 | if (curbuf->b_ind_cpp_baseclass == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8788 | break; |
| 8789 | |
| 8790 | lookfor = LOOKFOR_CPP_BASECLASS; |
| 8791 | continue; |
| 8792 | } |
| 8793 | |
| 8794 | /* Ignore unterminated lines in between, but |
| 8795 | * reduce indent. */ |
| 8796 | if (amount > cur_amount) |
| 8797 | amount = cur_amount; |
| 8798 | } |
| 8799 | else |
| 8800 | { |
| 8801 | /* |
| 8802 | * Found first unterminated line on a row, may |
| 8803 | * line up with this line, remember its indent |
| 8804 | * 100 + |
| 8805 | * -> here; |
| 8806 | */ |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8807 | l = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8808 | amount = cur_amount; |
Bram Moolenaar | 089af18 | 2015-10-07 11:41:49 +0200 | [diff] [blame] | 8809 | |
| 8810 | n = (int)STRLEN(l); |
| 8811 | if (terminated == ',' && (*skipwhite(l) == ']' |
| 8812 | || (n >=2 && l[n - 2] == ']'))) |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8813 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8814 | |
| 8815 | /* |
| 8816 | * If previous line ends in ',', check whether we |
| 8817 | * are in an initialization or enum |
| 8818 | * struct xxx = |
| 8819 | * { |
| 8820 | * sizeof a, |
| 8821 | * 124 }; |
| 8822 | * or a normal possible continuation line. |
| 8823 | * but only, of no other statement has been found |
| 8824 | * yet. |
| 8825 | */ |
| 8826 | if (lookfor == LOOKFOR_INITIAL && terminated == ',') |
| 8827 | { |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8828 | if (curbuf->b_ind_js) |
| 8829 | { |
| 8830 | /* Search for a line ending in a comma |
| 8831 | * and line up with the line below it |
| 8832 | * (could be the current line). |
| 8833 | * some = [ |
| 8834 | * 1, <- line up here |
| 8835 | * 2, |
| 8836 | * some = [ |
| 8837 | * 3 + <- line up here |
| 8838 | * 4 * |
| 8839 | * 5, |
| 8840 | * 6, |
| 8841 | */ |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8842 | if (cin_iscomment(skipwhite(l))) |
| 8843 | break; |
| 8844 | lookfor = LOOKFOR_COMMA; |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8845 | trypos = find_match_char('[', |
| 8846 | curbuf->b_ind_maxparen); |
| 8847 | if (trypos != NULL) |
| 8848 | { |
| 8849 | if (trypos->lnum |
| 8850 | == curwin->w_cursor.lnum - 1) |
| 8851 | { |
| 8852 | /* Current line is first inside |
| 8853 | * [], line up with it. */ |
| 8854 | break; |
| 8855 | } |
| 8856 | ourscope = trypos->lnum; |
| 8857 | } |
| 8858 | } |
| 8859 | else |
| 8860 | { |
| 8861 | lookfor = LOOKFOR_ENUM_OR_INIT; |
| 8862 | cont_amount = cin_first_id_amount(); |
| 8863 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8864 | } |
| 8865 | else |
| 8866 | { |
| 8867 | if (lookfor == LOOKFOR_INITIAL |
| 8868 | && *l != NUL |
| 8869 | && l[STRLEN(l) - 1] == '\\') |
| 8870 | /* XXX */ |
| 8871 | cont_amount = cin_get_equal_amount( |
| 8872 | curwin->w_cursor.lnum); |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8873 | if (lookfor != LOOKFOR_TERM |
Bram Moolenaar | dcefba9 | 2015-03-20 19:06:06 +0100 | [diff] [blame] | 8874 | && lookfor != LOOKFOR_JS_KEY |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 8875 | && lookfor != LOOKFOR_COMMA |
| 8876 | && raw_string_start != curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8877 | lookfor = LOOKFOR_UNTERM; |
| 8878 | } |
| 8879 | } |
| 8880 | } |
| 8881 | } |
| 8882 | |
| 8883 | /* |
| 8884 | * Check if we are after a while (cond); |
| 8885 | * If so: Ignore until the matching "do". |
| 8886 | */ |
Bram Moolenaar | 9f4fe7c | 2014-07-03 22:57:55 +0200 | [diff] [blame] | 8887 | else if (cin_iswhileofdo_end(terminated)) /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8888 | { |
| 8889 | /* |
| 8890 | * Found an unterminated line after a while ();, line up |
| 8891 | * with the last one. |
| 8892 | * while (cond); |
| 8893 | * 100 + <- line up with this one |
| 8894 | * -> here; |
| 8895 | */ |
| 8896 | if (lookfor == LOOKFOR_UNTERM |
| 8897 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8898 | { |
| 8899 | if (cont_amount > 0) |
| 8900 | amount = cont_amount; |
| 8901 | else |
| 8902 | amount += ind_continuation; |
| 8903 | break; |
| 8904 | } |
| 8905 | |
| 8906 | if (whilelevel == 0) |
| 8907 | { |
| 8908 | lookfor = LOOKFOR_TERM; |
| 8909 | amount = get_indent(); /* XXX */ |
| 8910 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 8911 | amount += curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8912 | } |
| 8913 | ++whilelevel; |
| 8914 | } |
| 8915 | |
| 8916 | /* |
| 8917 | * We are after a "normal" statement. |
| 8918 | * If we had another statement we can stop now and use the |
| 8919 | * indent of that other statement. |
| 8920 | * Otherwise the indent of the current statement may be used, |
| 8921 | * search backwards for the next "normal" statement. |
| 8922 | */ |
| 8923 | else |
| 8924 | { |
| 8925 | /* |
| 8926 | * Skip single break line, if before a switch label. It |
| 8927 | * may be lined up with the case label. |
| 8928 | */ |
| 8929 | if (lookfor == LOOKFOR_NOBREAK |
| 8930 | && cin_isbreak(skipwhite(ml_get_curline()))) |
| 8931 | { |
| 8932 | lookfor = LOOKFOR_ANY; |
| 8933 | continue; |
| 8934 | } |
| 8935 | |
| 8936 | /* |
| 8937 | * Handle "do {" line. |
| 8938 | */ |
| 8939 | if (whilelevel > 0) |
| 8940 | { |
| 8941 | l = cin_skipcomment(ml_get_curline()); |
| 8942 | if (cin_isdo(l)) |
| 8943 | { |
| 8944 | amount = get_indent(); /* XXX */ |
| 8945 | --whilelevel; |
| 8946 | continue; |
| 8947 | } |
| 8948 | } |
| 8949 | |
| 8950 | /* |
| 8951 | * Found a terminated line above an unterminated line. Add |
| 8952 | * the amount for a continuation line. |
| 8953 | * x = 1; |
| 8954 | * y = foo + |
| 8955 | * -> here; |
| 8956 | * or |
| 8957 | * int x = 1; |
| 8958 | * int foo, |
| 8959 | * -> here; |
| 8960 | */ |
| 8961 | if (lookfor == LOOKFOR_UNTERM |
| 8962 | || lookfor == LOOKFOR_ENUM_OR_INIT) |
| 8963 | { |
| 8964 | if (cont_amount > 0) |
| 8965 | amount = cont_amount; |
| 8966 | else |
| 8967 | amount += ind_continuation; |
| 8968 | break; |
| 8969 | } |
| 8970 | |
| 8971 | /* |
| 8972 | * Found a terminated line above a terminated line or "if" |
| 8973 | * etc. line. Use the amount of the line below us. |
| 8974 | * x = 1; x = 1; |
| 8975 | * if (asdf) y = 2; |
| 8976 | * while (asdf) ->here; |
| 8977 | * here; |
| 8978 | * ->foo; |
| 8979 | */ |
| 8980 | if (lookfor == LOOKFOR_TERM) |
| 8981 | { |
| 8982 | if (!lookfor_break && whilelevel == 0) |
| 8983 | break; |
| 8984 | } |
| 8985 | |
| 8986 | /* |
| 8987 | * First line above the one we're indenting is terminated. |
| 8988 | * To know what needs to be done look further backward for |
| 8989 | * a terminated line. |
| 8990 | */ |
| 8991 | else |
| 8992 | { |
| 8993 | /* |
| 8994 | * position the cursor over the rightmost paren, so |
| 8995 | * that matching it will take us back to the start of |
| 8996 | * the line. Helps for: |
| 8997 | * func(asdr, |
| 8998 | * asdfasdf); |
| 8999 | * here; |
| 9000 | */ |
| 9001 | term_again: |
| 9002 | l = ml_get_curline(); |
| 9003 | if (find_last_paren(l, '(', ')') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9004 | && (trypos = find_match_paren( |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9005 | curbuf->b_ind_maxparen)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9006 | { |
| 9007 | /* |
| 9008 | * Check if we are on a case label now. This is |
| 9009 | * handled above. |
| 9010 | * case xx: if ( asdf && |
| 9011 | * asdf) |
| 9012 | */ |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 9013 | curwin->w_cursor = *trypos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9014 | l = ml_get_curline(); |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 9015 | if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9016 | { |
| 9017 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 9018 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9019 | continue; |
| 9020 | } |
| 9021 | } |
| 9022 | |
| 9023 | /* When aligning with the case statement, don't align |
| 9024 | * with a statement after it. |
| 9025 | * case 1: { <-- don't use this { position |
| 9026 | * stat; |
| 9027 | * } |
| 9028 | * case 2: |
| 9029 | * stat; |
| 9030 | * } |
| 9031 | */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9032 | iscase = (curbuf->b_ind_keep_case_label |
| 9033 | && cin_iscase(l, FALSE)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9034 | |
| 9035 | /* |
| 9036 | * Get indent and pointer to text for current line, |
| 9037 | * ignoring any jump label. |
| 9038 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9039 | amount = skip_label(curwin->w_cursor.lnum, &l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9040 | |
| 9041 | if (theline[0] == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9042 | amount += curbuf->b_ind_open_extra; |
| 9043 | /* See remark above: "Only add b_ind_open_extra.." */ |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 9044 | l = skipwhite(l); |
| 9045 | if (*l == '{') |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9046 | amount -= curbuf->b_ind_open_extra; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9047 | lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM; |
| 9048 | |
| 9049 | /* |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 9050 | * When a terminated line starts with "else" skip to |
| 9051 | * the matching "if": |
| 9052 | * else 3; |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 9053 | * indent this; |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 9054 | * Need to use the scope of this "else". XXX |
| 9055 | * If whilelevel != 0 continue looking for a "do {". |
| 9056 | */ |
| 9057 | if (lookfor == LOOKFOR_TERM |
| 9058 | && *l != '}' |
| 9059 | && cin_iselse(l) |
| 9060 | && whilelevel == 0) |
| 9061 | { |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9062 | if ((trypos = find_start_brace()) == NULL |
| 9063 | || find_match(LOOKFOR_IF, trypos->lnum) |
| 9064 | == FAIL) |
Bram Moolenaar | 18144c8 | 2006-04-12 21:52:12 +0000 | [diff] [blame] | 9065 | break; |
| 9066 | continue; |
| 9067 | } |
| 9068 | |
| 9069 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9070 | * If we're at the end of a block, skip to the start of |
| 9071 | * that block. |
| 9072 | */ |
Bram Moolenaar | 6d8f9c6 | 2011-11-30 13:03:28 +0100 | [diff] [blame] | 9073 | l = ml_get_curline(); |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9074 | if (find_last_paren(l, '{', '}') /* XXX */ |
| 9075 | && (trypos = find_start_brace()) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9076 | { |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 9077 | curwin->w_cursor = *trypos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9078 | /* if not "else {" check for terminated again */ |
| 9079 | /* but skip block for "} else {" */ |
| 9080 | l = cin_skipcomment(ml_get_curline()); |
| 9081 | if (*l == '}' || !cin_iselse(l)) |
| 9082 | goto term_again; |
| 9083 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | ddfc978 | 2008-02-25 20:55:22 +0000 | [diff] [blame] | 9084 | curwin->w_cursor.col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9085 | } |
| 9086 | } |
| 9087 | } |
| 9088 | } |
| 9089 | } |
| 9090 | } |
| 9091 | |
| 9092 | /* add extra indent for a comment */ |
| 9093 | if (cin_iscomment(theline)) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9094 | amount += curbuf->b_ind_comment; |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 9095 | |
| 9096 | /* subtract extra left-shift for jump labels */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9097 | if (curbuf->b_ind_jump_label > 0 && original_line_islabel) |
| 9098 | amount -= curbuf->b_ind_jump_label; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9099 | |
| 9100 | goto theend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9101 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9102 | |
| 9103 | /* |
| 9104 | * ok -- we're not inside any sort of structure at all! |
| 9105 | * |
| 9106 | * This means we're at the top level, and everything should |
| 9107 | * basically just match where the previous line is, except |
| 9108 | * for the lines immediately following a function declaration, |
| 9109 | * which are K&R-style parameters and need to be indented. |
| 9110 | * |
| 9111 | * if our line starts with an open brace, forget about any |
| 9112 | * prevailing indent and make sure it looks like the start |
| 9113 | * of a function |
| 9114 | */ |
| 9115 | |
| 9116 | if (theline[0] == '{') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9117 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9118 | amount = curbuf->b_ind_first_open; |
| 9119 | goto theend; |
| 9120 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9121 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9122 | /* |
| 9123 | * If the NEXT line is a function declaration, the current |
| 9124 | * line needs to be indented as a function type spec. |
| 9125 | * Don't do this if the current line looks like a comment or if the |
| 9126 | * current line is terminated, ie. ends in ';', or if the current line |
| 9127 | * contains { or }: "void f() {\n if (1)" |
| 9128 | */ |
| 9129 | if (cur_curpos.lnum < curbuf->b_ml.ml_line_count |
| 9130 | && !cin_nocode(theline) |
| 9131 | && vim_strchr(theline, '{') == NULL |
| 9132 | && vim_strchr(theline, '}') == NULL |
| 9133 | && !cin_ends_in(theline, (char_u *)":", NULL) |
| 9134 | && !cin_ends_in(theline, (char_u *)",", NULL) |
| 9135 | && cin_isfuncdecl(NULL, cur_curpos.lnum + 1, |
| 9136 | cur_curpos.lnum + 1) |
| 9137 | && !cin_isterminated(theline, FALSE, TRUE)) |
| 9138 | { |
| 9139 | amount = curbuf->b_ind_func_type; |
| 9140 | goto theend; |
| 9141 | } |
| 9142 | |
| 9143 | /* search backwards until we find something we recognize */ |
| 9144 | amount = 0; |
| 9145 | curwin->w_cursor = cur_curpos; |
| 9146 | while (curwin->w_cursor.lnum > 1) |
| 9147 | { |
| 9148 | curwin->w_cursor.lnum--; |
| 9149 | curwin->w_cursor.col = 0; |
| 9150 | |
| 9151 | l = ml_get_curline(); |
| 9152 | |
| 9153 | /* |
| 9154 | * If we're in a comment or raw string now, skip to the start |
| 9155 | * of it. |
| 9156 | */ /* XXX */ |
Bram Moolenaar | dde8131 | 2017-08-26 17:49:01 +0200 | [diff] [blame] | 9157 | if ((trypos = ind_find_start_CORS(NULL)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9158 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9159 | curwin->w_cursor.lnum = trypos->lnum + 1; |
| 9160 | curwin->w_cursor.col = 0; |
| 9161 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9162 | } |
| 9163 | |
| 9164 | /* |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9165 | * Are we at the start of a cpp base class declaration or |
| 9166 | * constructor initialization? |
| 9167 | */ /* XXX */ |
| 9168 | n = FALSE; |
| 9169 | if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9170 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9171 | n = cin_is_cpp_baseclass(&cache_cpp_baseclass); |
| 9172 | l = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9173 | } |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9174 | if (n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9175 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9176 | /* XXX */ |
| 9177 | amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col); |
| 9178 | break; |
| 9179 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9180 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9181 | /* |
| 9182 | * Skip preprocessor directives and blank lines. |
| 9183 | */ |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 9184 | if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9185 | continue; |
| 9186 | |
| 9187 | if (cin_nocode(l)) |
| 9188 | continue; |
| 9189 | |
| 9190 | /* |
| 9191 | * If the previous line ends in ',', use one level of |
| 9192 | * indentation: |
| 9193 | * int foo, |
| 9194 | * bar; |
| 9195 | * do this before checking for '}' in case of eg. |
| 9196 | * enum foobar |
| 9197 | * { |
| 9198 | * ... |
| 9199 | * } foo, |
| 9200 | * bar; |
| 9201 | */ |
| 9202 | n = 0; |
| 9203 | if (cin_ends_in(l, (char_u *)",", NULL) |
| 9204 | || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\')) |
| 9205 | { |
| 9206 | /* take us back to opening paren */ |
| 9207 | if (find_last_paren(l, '(', ')') |
| 9208 | && (trypos = find_match_paren( |
| 9209 | curbuf->b_ind_maxparen)) != NULL) |
| 9210 | curwin->w_cursor = *trypos; |
| 9211 | |
| 9212 | /* For a line ending in ',' that is a continuation line go |
| 9213 | * back to the first line with a backslash: |
| 9214 | * char *foo = "bla\ |
| 9215 | * bla", |
| 9216 | * here; |
| 9217 | */ |
| 9218 | while (n == 0 && curwin->w_cursor.lnum > 1) |
| 9219 | { |
| 9220 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 9221 | if (*l == NUL || l[STRLEN(l) - 1] != '\\') |
| 9222 | break; |
| 9223 | --curwin->w_cursor.lnum; |
| 9224 | curwin->w_cursor.col = 0; |
| 9225 | } |
| 9226 | |
| 9227 | amount = get_indent(); /* XXX */ |
| 9228 | |
| 9229 | if (amount == 0) |
| 9230 | amount = cin_first_id_amount(); |
| 9231 | if (amount == 0) |
| 9232 | amount = ind_continuation; |
| 9233 | break; |
| 9234 | } |
| 9235 | |
| 9236 | /* |
| 9237 | * If the line looks like a function declaration, and we're |
| 9238 | * not in a comment, put it the left margin. |
| 9239 | */ |
| 9240 | if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */ |
| 9241 | break; |
| 9242 | l = ml_get_curline(); |
| 9243 | |
| 9244 | /* |
| 9245 | * Finding the closing '}' of a previous function. Put |
| 9246 | * current line at the left margin. For when 'cino' has "fs". |
| 9247 | */ |
| 9248 | if (*skipwhite(l) == '}') |
| 9249 | break; |
| 9250 | |
| 9251 | /* (matching {) |
| 9252 | * If the previous line ends on '};' (maybe followed by |
| 9253 | * comments) align at column 0. For example: |
| 9254 | * char *string_array[] = { "foo", |
| 9255 | * / * x * / "b};ar" }; / * foobar * / |
| 9256 | */ |
| 9257 | if (cin_ends_in(l, (char_u *)"};", NULL)) |
| 9258 | break; |
| 9259 | |
| 9260 | /* |
| 9261 | * If the previous line ends on '[' we are probably in an |
| 9262 | * array constant: |
| 9263 | * something = [ |
| 9264 | * 234, <- extra indent |
| 9265 | */ |
| 9266 | if (cin_ends_in(l, (char_u *)"[", NULL)) |
| 9267 | { |
| 9268 | amount = get_indent() + ind_continuation; |
| 9269 | break; |
| 9270 | } |
| 9271 | |
| 9272 | /* |
| 9273 | * Find a line only has a semicolon that belongs to a previous |
| 9274 | * line ending in '}', e.g. before an #endif. Don't increase |
| 9275 | * indent then. |
| 9276 | */ |
| 9277 | if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1)) |
| 9278 | { |
| 9279 | pos_T curpos_save = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9280 | |
| 9281 | while (curwin->w_cursor.lnum > 1) |
| 9282 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9283 | look = ml_get(--curwin->w_cursor.lnum); |
| 9284 | if (!(cin_nocode(look) || cin_ispreproc_cont( |
Bram Moolenaar | c6aa475 | 2017-01-07 15:39:43 +0100 | [diff] [blame] | 9285 | &look, &curwin->w_cursor.lnum, &amount))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9286 | break; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9287 | } |
| 9288 | if (curwin->w_cursor.lnum > 0 |
| 9289 | && cin_ends_in(look, (char_u *)"}", NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9290 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9291 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9292 | curwin->w_cursor = curpos_save; |
| 9293 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9294 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9295 | /* |
| 9296 | * If the PREVIOUS line is a function declaration, the current |
| 9297 | * line (and the ones that follow) needs to be indented as |
| 9298 | * parameters. |
| 9299 | */ |
| 9300 | if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) |
| 9301 | { |
| 9302 | amount = curbuf->b_ind_param; |
| 9303 | break; |
| 9304 | } |
| 9305 | |
| 9306 | /* |
| 9307 | * If the previous line ends in ';' and the line before the |
| 9308 | * previous line ends in ',' or '\', ident to column zero: |
| 9309 | * int foo, |
| 9310 | * bar; |
| 9311 | * indent_to_0 here; |
| 9312 | */ |
| 9313 | if (cin_ends_in(l, (char_u *)";", NULL)) |
| 9314 | { |
| 9315 | l = ml_get(curwin->w_cursor.lnum - 1); |
| 9316 | if (cin_ends_in(l, (char_u *)",", NULL) |
| 9317 | || (*l != NUL && l[STRLEN(l) - 1] == '\\')) |
| 9318 | break; |
| 9319 | l = ml_get_curline(); |
| 9320 | } |
| 9321 | |
| 9322 | /* |
| 9323 | * Doesn't look like anything interesting -- so just |
| 9324 | * use the indent of this line. |
| 9325 | * |
| 9326 | * Position the cursor over the rightmost paren, so that |
| 9327 | * matching it will take us back to the start of the line. |
| 9328 | */ |
| 9329 | find_last_paren(l, '(', ')'); |
| 9330 | |
| 9331 | if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) |
| 9332 | curwin->w_cursor = *trypos; |
| 9333 | amount = get_indent(); /* XXX */ |
| 9334 | break; |
| 9335 | } |
| 9336 | |
| 9337 | /* add extra indent for a comment */ |
| 9338 | if (cin_iscomment(theline)) |
| 9339 | amount += curbuf->b_ind_comment; |
| 9340 | |
| 9341 | /* add extra indent if the previous line ended in a backslash: |
| 9342 | * "asdfasdf\ |
| 9343 | * here"; |
| 9344 | * char *foo = "asdf\ |
| 9345 | * here"; |
| 9346 | */ |
| 9347 | if (cur_curpos.lnum > 1) |
| 9348 | { |
| 9349 | l = ml_get(cur_curpos.lnum - 1); |
| 9350 | if (*l != NUL && l[STRLEN(l) - 1] == '\\') |
| 9351 | { |
| 9352 | cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1); |
| 9353 | if (cur_amount > 0) |
| 9354 | amount = cur_amount; |
| 9355 | else if (cur_amount == 0) |
| 9356 | amount += ind_continuation; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9357 | } |
| 9358 | } |
| 9359 | |
| 9360 | theend: |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 9361 | if (amount < 0) |
| 9362 | amount = 0; |
| 9363 | |
| 9364 | laterend: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9365 | /* put the cursor back where it belongs */ |
| 9366 | curwin->w_cursor = cur_curpos; |
| 9367 | |
| 9368 | vim_free(linecopy); |
| 9369 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9370 | return amount; |
| 9371 | } |
| 9372 | |
| 9373 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9374 | find_match(int lookfor, linenr_T ourscope) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9375 | { |
| 9376 | char_u *look; |
| 9377 | pos_T *theirscope; |
| 9378 | char_u *mightbeif; |
| 9379 | int elselevel; |
| 9380 | int whilelevel; |
| 9381 | |
| 9382 | if (lookfor == LOOKFOR_IF) |
| 9383 | { |
| 9384 | elselevel = 1; |
| 9385 | whilelevel = 0; |
| 9386 | } |
| 9387 | else |
| 9388 | { |
| 9389 | elselevel = 0; |
| 9390 | whilelevel = 1; |
| 9391 | } |
| 9392 | |
| 9393 | curwin->w_cursor.col = 0; |
| 9394 | |
| 9395 | while (curwin->w_cursor.lnum > ourscope + 1) |
| 9396 | { |
| 9397 | curwin->w_cursor.lnum--; |
| 9398 | curwin->w_cursor.col = 0; |
| 9399 | |
| 9400 | look = cin_skipcomment(ml_get_curline()); |
| 9401 | if (cin_iselse(look) |
| 9402 | || cin_isif(look) |
| 9403 | || cin_isdo(look) /* XXX */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9404 | || cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9405 | { |
| 9406 | /* |
| 9407 | * if we've gone outside the braces entirely, |
| 9408 | * we must be out of scope... |
| 9409 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9410 | theirscope = find_start_brace(); /* XXX */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9411 | if (theirscope == NULL) |
| 9412 | break; |
| 9413 | |
| 9414 | /* |
| 9415 | * and if the brace enclosing this is further |
| 9416 | * back than the one enclosing the else, we're |
| 9417 | * out of luck too. |
| 9418 | */ |
| 9419 | if (theirscope->lnum < ourscope) |
| 9420 | break; |
| 9421 | |
| 9422 | /* |
| 9423 | * and if they're enclosed in a *deeper* brace, |
| 9424 | * then we can ignore it because it's in a |
| 9425 | * different scope... |
| 9426 | */ |
| 9427 | if (theirscope->lnum > ourscope) |
| 9428 | continue; |
| 9429 | |
| 9430 | /* |
| 9431 | * if it was an "else" (that's not an "else if") |
| 9432 | * then we need to go back to another if, so |
| 9433 | * increment elselevel |
| 9434 | */ |
| 9435 | look = cin_skipcomment(ml_get_curline()); |
| 9436 | if (cin_iselse(look)) |
| 9437 | { |
| 9438 | mightbeif = cin_skipcomment(look + 4); |
| 9439 | if (!cin_isif(mightbeif)) |
| 9440 | ++elselevel; |
| 9441 | continue; |
| 9442 | } |
| 9443 | |
| 9444 | /* |
| 9445 | * if it was a "while" then we need to go back to |
| 9446 | * another "do", so increment whilelevel. XXX |
| 9447 | */ |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 9448 | if (cin_iswhileofdo(look, curwin->w_cursor.lnum)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9449 | { |
| 9450 | ++whilelevel; |
| 9451 | continue; |
| 9452 | } |
| 9453 | |
| 9454 | /* If it's an "if" decrement elselevel */ |
| 9455 | look = cin_skipcomment(ml_get_curline()); |
| 9456 | if (cin_isif(look)) |
| 9457 | { |
| 9458 | elselevel--; |
| 9459 | /* |
| 9460 | * When looking for an "if" ignore "while"s that |
| 9461 | * get in the way. |
| 9462 | */ |
| 9463 | if (elselevel == 0 && lookfor == LOOKFOR_IF) |
| 9464 | whilelevel = 0; |
| 9465 | } |
| 9466 | |
| 9467 | /* If it's a "do" decrement whilelevel */ |
| 9468 | if (cin_isdo(look)) |
| 9469 | whilelevel--; |
| 9470 | |
| 9471 | /* |
| 9472 | * if we've used up all the elses, then |
| 9473 | * this must be the if that we want! |
| 9474 | * match the indent level of that if. |
| 9475 | */ |
| 9476 | if (elselevel <= 0 && whilelevel <= 0) |
| 9477 | { |
| 9478 | return OK; |
| 9479 | } |
| 9480 | } |
| 9481 | } |
| 9482 | return FAIL; |
| 9483 | } |
| 9484 | |
| 9485 | # if defined(FEAT_EVAL) || defined(PROTO) |
| 9486 | /* |
| 9487 | * Get indent level from 'indentexpr'. |
| 9488 | */ |
| 9489 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9490 | get_expr_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9491 | { |
Bram Moolenaar | 97db554 | 2017-04-21 23:18:26 +0200 | [diff] [blame] | 9492 | int indent = -1; |
Bram Moolenaar | a701b3b | 2017-04-20 22:57:27 +0200 | [diff] [blame] | 9493 | char_u *inde_copy; |
Bram Moolenaar | 8fe8d9e | 2013-02-13 16:10:17 +0100 | [diff] [blame] | 9494 | pos_T save_pos; |
| 9495 | colnr_T save_curswant; |
| 9496 | int save_set_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9497 | int save_State; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 9498 | int use_sandbox = was_set_insecurely((char_u *)"indentexpr", |
| 9499 | OPT_LOCAL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9500 | |
Bram Moolenaar | 8fe8d9e | 2013-02-13 16:10:17 +0100 | [diff] [blame] | 9501 | /* Save and restore cursor position and curswant, in case it was changed |
| 9502 | * via :normal commands */ |
| 9503 | save_pos = curwin->w_cursor; |
| 9504 | save_curswant = curwin->w_curswant; |
| 9505 | save_set_curswant = curwin->w_set_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9506 | set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 9507 | if (use_sandbox) |
| 9508 | ++sandbox; |
| 9509 | ++textlock; |
Bram Moolenaar | a701b3b | 2017-04-20 22:57:27 +0200 | [diff] [blame] | 9510 | |
| 9511 | /* Need to make a copy, the 'indentexpr' option could be changed while |
| 9512 | * evaluating it. */ |
| 9513 | inde_copy = vim_strsave(curbuf->b_p_inde); |
| 9514 | if (inde_copy != NULL) |
| 9515 | { |
| 9516 | indent = (int)eval_to_number(inde_copy); |
| 9517 | vim_free(inde_copy); |
| 9518 | } |
| 9519 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 9520 | if (use_sandbox) |
| 9521 | --sandbox; |
| 9522 | --textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9523 | |
| 9524 | /* Restore the cursor position so that 'indentexpr' doesn't need to. |
| 9525 | * Pretend to be in Insert mode, allow cursor past end of line for "o" |
| 9526 | * command. */ |
| 9527 | save_State = State; |
| 9528 | State = INSERT; |
Bram Moolenaar | 8fe8d9e | 2013-02-13 16:10:17 +0100 | [diff] [blame] | 9529 | curwin->w_cursor = save_pos; |
| 9530 | curwin->w_curswant = save_curswant; |
| 9531 | curwin->w_set_curswant = save_set_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9532 | check_cursor(); |
| 9533 | State = save_State; |
| 9534 | |
| 9535 | /* If there is an error, just keep the current indent. */ |
| 9536 | if (indent < 0) |
| 9537 | indent = get_indent(); |
| 9538 | |
| 9539 | return indent; |
| 9540 | } |
| 9541 | # endif |
| 9542 | |
| 9543 | #endif /* FEAT_CINDENT */ |
| 9544 | |
| 9545 | #if defined(FEAT_LISP) || defined(PROTO) |
| 9546 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 9547 | static int lisp_match(char_u *p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9548 | |
| 9549 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9550 | lisp_match(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9551 | { |
| 9552 | char_u buf[LSIZE]; |
| 9553 | int len; |
Bram Moolenaar | af6c131 | 2014-03-12 18:55:58 +0100 | [diff] [blame] | 9554 | 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] | 9555 | |
| 9556 | while (*word != NUL) |
| 9557 | { |
| 9558 | (void)copy_option_part(&word, buf, LSIZE, ","); |
| 9559 | len = (int)STRLEN(buf); |
| 9560 | if (STRNCMP(buf, p, len) == 0 && p[len] == ' ') |
| 9561 | return TRUE; |
| 9562 | } |
| 9563 | return FALSE; |
| 9564 | } |
| 9565 | |
| 9566 | /* |
| 9567 | * When 'p' is present in 'cpoptions, a Vi compatible method is used. |
| 9568 | * The incompatible newer method is quite a bit better at indenting |
| 9569 | * code in lisp-like languages than the traditional one; it's still |
| 9570 | * mostly heuristics however -- Dirk van Deun, dirk@rave.org |
| 9571 | * |
| 9572 | * TODO: |
| 9573 | * Findmatch() should be adapted for lisp, also to make showmatch |
| 9574 | * work correctly: now (v5.3) it seems all C/C++ oriented: |
| 9575 | * - it does not recognize the #\( and #\) notations as character literals |
| 9576 | * - it doesn't know about comments starting with a semicolon |
| 9577 | * - it incorrectly interprets '(' as a character literal |
| 9578 | * All this messes up get_lisp_indent in some rare cases. |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9579 | * Update from Sergey Khorev: |
| 9580 | * I tried to fix the first two issues. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9581 | */ |
| 9582 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9583 | get_lisp_indent(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9584 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9585 | pos_T *pos, realpos, paren; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9586 | int amount; |
| 9587 | char_u *that; |
| 9588 | colnr_T col; |
| 9589 | colnr_T firsttry; |
| 9590 | int parencount, quotecount; |
| 9591 | int vi_lisp; |
| 9592 | |
| 9593 | /* Set vi_lisp to use the vi-compatible method */ |
| 9594 | vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL); |
| 9595 | |
| 9596 | realpos = curwin->w_cursor; |
| 9597 | curwin->w_cursor.col = 0; |
| 9598 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9599 | if ((pos = findmatch(NULL, '(')) == NULL) |
| 9600 | pos = findmatch(NULL, '['); |
| 9601 | else |
| 9602 | { |
| 9603 | paren = *pos; |
| 9604 | pos = findmatch(NULL, '['); |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 9605 | if (pos == NULL || LT_POSP(pos, &paren)) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9606 | pos = &paren; |
| 9607 | } |
| 9608 | if (pos != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9609 | { |
| 9610 | /* Extra trick: Take the indent of the first previous non-white |
| 9611 | * line that is at the same () level. */ |
| 9612 | amount = -1; |
| 9613 | parencount = 0; |
| 9614 | |
| 9615 | while (--curwin->w_cursor.lnum >= pos->lnum) |
| 9616 | { |
| 9617 | if (linewhite(curwin->w_cursor.lnum)) |
| 9618 | continue; |
| 9619 | for (that = ml_get_curline(); *that != NUL; ++that) |
| 9620 | { |
| 9621 | if (*that == ';') |
| 9622 | { |
| 9623 | while (*(that + 1) != NUL) |
| 9624 | ++that; |
| 9625 | continue; |
| 9626 | } |
| 9627 | if (*that == '\\') |
| 9628 | { |
| 9629 | if (*(that + 1) != NUL) |
| 9630 | ++that; |
| 9631 | continue; |
| 9632 | } |
| 9633 | if (*that == '"' && *(that + 1) != NUL) |
| 9634 | { |
Bram Moolenaar | 15ff6c1 | 2006-09-15 18:18:09 +0000 | [diff] [blame] | 9635 | while (*++that && *that != '"') |
| 9636 | { |
| 9637 | /* skipping escaped characters in the string */ |
| 9638 | if (*that == '\\') |
| 9639 | { |
| 9640 | if (*++that == NUL) |
| 9641 | break; |
| 9642 | if (that[1] == NUL) |
| 9643 | { |
| 9644 | ++that; |
| 9645 | break; |
| 9646 | } |
| 9647 | } |
| 9648 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9649 | } |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9650 | if (*that == '(' || *that == '[') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9651 | ++parencount; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9652 | else if (*that == ')' || *that == ']') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9653 | --parencount; |
| 9654 | } |
| 9655 | if (parencount == 0) |
| 9656 | { |
| 9657 | amount = get_indent(); |
| 9658 | break; |
| 9659 | } |
| 9660 | } |
| 9661 | |
| 9662 | if (amount == -1) |
| 9663 | { |
| 9664 | curwin->w_cursor.lnum = pos->lnum; |
| 9665 | curwin->w_cursor.col = pos->col; |
| 9666 | col = pos->col; |
| 9667 | |
| 9668 | that = ml_get_curline(); |
| 9669 | |
| 9670 | if (vi_lisp && get_indent() == 0) |
| 9671 | amount = 2; |
| 9672 | else |
| 9673 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9674 | char_u *line = that; |
| 9675 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9676 | amount = 0; |
| 9677 | while (*that && col) |
| 9678 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9679 | amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9680 | col--; |
| 9681 | } |
| 9682 | |
| 9683 | /* |
| 9684 | * Some keywords require "body" indenting rules (the |
| 9685 | * non-standard-lisp ones are Scheme special forms): |
| 9686 | * |
| 9687 | * (let ((a 1)) instead (let ((a 1)) |
| 9688 | * (...)) of (...)) |
| 9689 | */ |
| 9690 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9691 | if (!vi_lisp && (*that == '(' || *that == '[') |
| 9692 | && lisp_match(that + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9693 | amount += 2; |
| 9694 | else |
| 9695 | { |
| 9696 | that++; |
| 9697 | amount++; |
| 9698 | firsttry = amount; |
| 9699 | |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 9700 | while (VIM_ISWHITE(*that)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9701 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9702 | amount += lbr_chartabsize(line, that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9703 | ++that; |
| 9704 | } |
| 9705 | |
| 9706 | if (*that && *that != ';') /* not a comment line */ |
| 9707 | { |
Bram Moolenaar | e21877a | 2008-02-13 09:58:14 +0000 | [diff] [blame] | 9708 | /* test *that != '(' to accommodate first let/do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9709 | * argument if it is more than one line */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9710 | if (!vi_lisp && *that != '(' && *that != '[') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9711 | firsttry++; |
| 9712 | |
| 9713 | parencount = 0; |
| 9714 | quotecount = 0; |
| 9715 | |
| 9716 | if (vi_lisp |
| 9717 | || (*that != '"' |
| 9718 | && *that != '\'' |
| 9719 | && *that != '#' |
| 9720 | && (*that < '0' || *that > '9'))) |
| 9721 | { |
| 9722 | while (*that |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 9723 | && (!VIM_ISWHITE(*that) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9724 | || quotecount |
| 9725 | || parencount) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9726 | && (!((*that == '(' || *that == '[') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9727 | && !quotecount |
| 9728 | && !parencount |
| 9729 | && vi_lisp))) |
| 9730 | { |
| 9731 | if (*that == '"') |
| 9732 | quotecount = !quotecount; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9733 | if ((*that == '(' || *that == '[') |
| 9734 | && !quotecount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9735 | ++parencount; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9736 | if ((*that == ')' || *that == ']') |
| 9737 | && !quotecount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9738 | --parencount; |
| 9739 | if (*that == '\\' && *(that+1) != NUL) |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9740 | amount += lbr_chartabsize_adv( |
| 9741 | line, &that, (colnr_T)amount); |
| 9742 | amount += lbr_chartabsize_adv( |
| 9743 | line, &that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9744 | } |
| 9745 | } |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 9746 | while (VIM_ISWHITE(*that)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9747 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9748 | amount += lbr_chartabsize( |
| 9749 | line, that, (colnr_T)amount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9750 | that++; |
| 9751 | } |
| 9752 | if (!*that || *that == ';') |
| 9753 | amount = firsttry; |
| 9754 | } |
| 9755 | } |
| 9756 | } |
| 9757 | } |
| 9758 | } |
| 9759 | else |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 9760 | amount = 0; /* no matching '(' or '[' found, use zero indent */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9761 | |
| 9762 | curwin->w_cursor = realpos; |
| 9763 | |
| 9764 | return amount; |
| 9765 | } |
| 9766 | #endif /* FEAT_LISP */ |
| 9767 | |
| 9768 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9769 | prepare_to_exit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9770 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 9771 | #if defined(SIGHUP) && defined(SIG_IGN) |
| 9772 | /* Ignore SIGHUP, because a dropped connection causes a read error, which |
| 9773 | * makes Vim exit and then handling SIGHUP causes various reentrance |
| 9774 | * problems. */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 9775 | signal(SIGHUP, SIG_IGN); |
| 9776 | #endif |
| 9777 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9778 | #ifdef FEAT_GUI |
| 9779 | if (gui.in_use) |
| 9780 | { |
| 9781 | gui.dying = TRUE; |
| 9782 | out_trash(); /* trash any pending output */ |
| 9783 | } |
| 9784 | else |
| 9785 | #endif |
| 9786 | { |
| 9787 | windgoto((int)Rows - 1, 0); |
| 9788 | |
| 9789 | /* |
| 9790 | * Switch terminal mode back now, so messages end up on the "normal" |
| 9791 | * screen (if there are two screens). |
| 9792 | */ |
| 9793 | settmode(TMODE_COOK); |
Bram Moolenaar | cea912a | 2016-10-12 14:20:24 +0200 | [diff] [blame] | 9794 | stoptermcap(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9795 | out_flush(); |
| 9796 | } |
| 9797 | } |
| 9798 | |
| 9799 | /* |
| 9800 | * Preserve files and exit. |
| 9801 | * When called IObuff must contain a message. |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 9802 | * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe |
| 9803 | * functions, such as allocating memory. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9804 | */ |
| 9805 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9806 | preserve_exit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9807 | { |
| 9808 | buf_T *buf; |
| 9809 | |
| 9810 | prepare_to_exit(); |
| 9811 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 9812 | /* Setting this will prevent free() calls. That avoids calling free() |
| 9813 | * recursively when free() was invoked with a bad pointer. */ |
| 9814 | really_exiting = TRUE; |
| 9815 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9816 | out_str(IObuff); |
| 9817 | screen_start(); /* don't know where cursor is now */ |
| 9818 | out_flush(); |
| 9819 | |
| 9820 | ml_close_notmod(); /* close all not-modified buffers */ |
| 9821 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 9822 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9823 | { |
| 9824 | if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) |
| 9825 | { |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 9826 | OUT_STR("Vim: preserving files...\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9827 | screen_start(); /* don't know where cursor is now */ |
| 9828 | out_flush(); |
| 9829 | ml_sync_all(FALSE, FALSE); /* preserve all swap files */ |
| 9830 | break; |
| 9831 | } |
| 9832 | } |
| 9833 | |
| 9834 | ml_close_all(FALSE); /* close all memfiles, without deleting */ |
| 9835 | |
Bram Moolenaar | bec9c20 | 2013-09-05 21:41:39 +0200 | [diff] [blame] | 9836 | OUT_STR("Vim: Finished.\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9837 | |
| 9838 | getout(1); |
| 9839 | } |
| 9840 | |
| 9841 | /* |
| 9842 | * return TRUE if "fname" exists. |
| 9843 | */ |
| 9844 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9845 | vim_fexists(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9846 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 9847 | stat_T st; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9848 | |
| 9849 | if (mch_stat((char *)fname, &st)) |
| 9850 | return FALSE; |
| 9851 | return TRUE; |
| 9852 | } |
| 9853 | |
| 9854 | /* |
| 9855 | * Check for CTRL-C pressed, but only once in a while. |
| 9856 | * Should be used instead of ui_breakcheck() for functions that check for |
| 9857 | * each line in the file. Calling ui_breakcheck() each time takes too much |
| 9858 | * time, because it can be a system call. |
| 9859 | */ |
| 9860 | |
| 9861 | #ifndef BREAKCHECK_SKIP |
| 9862 | # ifdef FEAT_GUI /* assume the GUI only runs on fast computers */ |
| 9863 | # define BREAKCHECK_SKIP 200 |
| 9864 | # else |
| 9865 | # define BREAKCHECK_SKIP 32 |
| 9866 | # endif |
| 9867 | #endif |
| 9868 | |
| 9869 | static int breakcheck_count = 0; |
| 9870 | |
| 9871 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9872 | line_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9873 | { |
| 9874 | if (++breakcheck_count >= BREAKCHECK_SKIP) |
| 9875 | { |
| 9876 | breakcheck_count = 0; |
| 9877 | ui_breakcheck(); |
| 9878 | } |
| 9879 | } |
| 9880 | |
| 9881 | /* |
| 9882 | * Like line_breakcheck() but check 10 times less often. |
| 9883 | */ |
| 9884 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9885 | fast_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9886 | { |
| 9887 | if (++breakcheck_count >= BREAKCHECK_SKIP * 10) |
| 9888 | { |
| 9889 | breakcheck_count = 0; |
| 9890 | ui_breakcheck(); |
| 9891 | } |
| 9892 | } |
| 9893 | |
| 9894 | /* |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 9895 | * Invoke expand_wildcards() for one pattern. |
| 9896 | * Expand items like "%:h" before the expansion. |
| 9897 | * Returns OK or FAIL. |
| 9898 | */ |
| 9899 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9900 | expand_wildcards_eval( |
| 9901 | char_u **pat, /* pointer to input pattern */ |
| 9902 | int *num_file, /* resulting number of files */ |
| 9903 | char_u ***file, /* array of resulting files */ |
| 9904 | int flags) /* EW_DIR, etc. */ |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 9905 | { |
| 9906 | int ret = FAIL; |
| 9907 | char_u *eval_pat = NULL; |
| 9908 | char_u *exp_pat = *pat; |
| 9909 | char_u *ignored_msg; |
| 9910 | int usedlen; |
| 9911 | |
| 9912 | if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') |
| 9913 | { |
| 9914 | ++emsg_off; |
| 9915 | eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, |
| 9916 | NULL, &ignored_msg, NULL); |
| 9917 | --emsg_off; |
| 9918 | if (eval_pat != NULL) |
| 9919 | exp_pat = concat_str(eval_pat, exp_pat + usedlen); |
| 9920 | } |
| 9921 | |
| 9922 | if (exp_pat != NULL) |
| 9923 | ret = expand_wildcards(1, &exp_pat, num_file, file, flags); |
| 9924 | |
| 9925 | if (eval_pat != NULL) |
| 9926 | { |
| 9927 | vim_free(exp_pat); |
| 9928 | vim_free(eval_pat); |
| 9929 | } |
| 9930 | |
| 9931 | return ret; |
| 9932 | } |
| 9933 | |
| 9934 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9935 | * Expand wildcards. Calls gen_expand_wildcards() and removes files matching |
| 9936 | * 'wildignore'. |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9937 | * 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] | 9938 | */ |
| 9939 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 9940 | expand_wildcards( |
| 9941 | int num_pat, /* number of input patterns */ |
| 9942 | char_u **pat, /* array of input patterns */ |
| 9943 | int *num_files, /* resulting number of files */ |
| 9944 | char_u ***files, /* array of resulting files */ |
| 9945 | int flags) /* EW_DIR, etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9946 | { |
| 9947 | int retval; |
| 9948 | int i, j; |
| 9949 | char_u *p; |
| 9950 | int non_suf_match; /* number without matching suffix */ |
| 9951 | |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9952 | retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9953 | |
| 9954 | /* When keeping all matches, return here */ |
Bram Moolenaar | 9e193ac | 2010-07-19 23:11:27 +0200 | [diff] [blame] | 9955 | if ((flags & EW_KEEPALL) || retval == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9956 | return retval; |
| 9957 | |
| 9958 | #ifdef FEAT_WILDIGN |
| 9959 | /* |
| 9960 | * Remove names that match 'wildignore'. |
| 9961 | */ |
| 9962 | if (*p_wig) |
| 9963 | { |
| 9964 | char_u *ffname; |
| 9965 | |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9966 | /* check all files in (*files)[] */ |
| 9967 | for (i = 0; i < *num_files; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9968 | { |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9969 | ffname = FullName_save((*files)[i], FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9970 | if (ffname == NULL) /* out of memory */ |
| 9971 | break; |
| 9972 | # ifdef VMS |
| 9973 | vms_remove_version(ffname); |
| 9974 | # endif |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9975 | if (match_file_list(p_wig, (*files)[i], ffname)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9976 | { |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 9977 | /* remove this matching file from the list */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9978 | vim_free((*files)[i]); |
| 9979 | for (j = i; j + 1 < *num_files; ++j) |
| 9980 | (*files)[j] = (*files)[j + 1]; |
| 9981 | --*num_files; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9982 | --i; |
| 9983 | } |
| 9984 | vim_free(ffname); |
| 9985 | } |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9986 | |
| 9987 | /* If the number of matches is now zero, we fail. */ |
| 9988 | if (*num_files == 0) |
| 9989 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 9990 | VIM_CLEAR(*files); |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9991 | return FAIL; |
| 9992 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9993 | } |
| 9994 | #endif |
| 9995 | |
| 9996 | /* |
| 9997 | * Move the names where 'suffixes' match to the end. |
| 9998 | */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 9999 | if (*num_files > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10000 | { |
| 10001 | non_suf_match = 0; |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10002 | for (i = 0; i < *num_files; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10003 | { |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10004 | if (!match_suffix((*files)[i])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10005 | { |
| 10006 | /* |
| 10007 | * Move the name without matching suffix to the front |
| 10008 | * of the list. |
| 10009 | */ |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10010 | p = (*files)[i]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10011 | for (j = i; j > non_suf_match; --j) |
Bram Moolenaar | 7b256fe | 2015-09-15 19:05:59 +0200 | [diff] [blame] | 10012 | (*files)[j] = (*files)[j - 1]; |
| 10013 | (*files)[non_suf_match++] = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10014 | } |
| 10015 | } |
| 10016 | } |
| 10017 | |
| 10018 | return retval; |
| 10019 | } |
| 10020 | |
| 10021 | /* |
| 10022 | * Return TRUE if "fname" matches with an entry in 'suffixes'. |
| 10023 | */ |
| 10024 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10025 | match_suffix(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10026 | { |
| 10027 | int fnamelen, setsuflen; |
| 10028 | char_u *setsuf; |
| 10029 | #define MAXSUFLEN 30 /* maximum length of a file suffix */ |
| 10030 | char_u suf_buf[MAXSUFLEN]; |
| 10031 | |
| 10032 | fnamelen = (int)STRLEN(fname); |
| 10033 | setsuflen = 0; |
| 10034 | for (setsuf = p_su; *setsuf; ) |
| 10035 | { |
| 10036 | setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); |
Bram Moolenaar | 055a2ba | 2009-07-14 19:40:21 +0000 | [diff] [blame] | 10037 | if (setsuflen == 0) |
| 10038 | { |
| 10039 | char_u *tail = gettail(fname); |
| 10040 | |
| 10041 | /* empty entry: match name without a '.' */ |
| 10042 | if (vim_strchr(tail, '.') == NULL) |
| 10043 | { |
| 10044 | setsuflen = 1; |
| 10045 | break; |
| 10046 | } |
| 10047 | } |
| 10048 | else |
| 10049 | { |
| 10050 | if (fnamelen >= setsuflen |
| 10051 | && fnamencmp(suf_buf, fname + fnamelen - setsuflen, |
| 10052 | (size_t)setsuflen) == 0) |
| 10053 | break; |
| 10054 | setsuflen = 0; |
| 10055 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10056 | } |
| 10057 | return (setsuflen != 0); |
| 10058 | } |
| 10059 | |
| 10060 | #if !defined(NO_EXPANDPATH) || defined(PROTO) |
| 10061 | |
| 10062 | # ifdef VIM_BACKTICK |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 10063 | static int vim_backtick(char_u *p); |
| 10064 | static int expand_backtick(garray_T *gap, char_u *pat, int flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10065 | # endif |
| 10066 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10067 | # if defined(WIN3264) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10068 | /* |
| 10069 | * File name expansion code for MS-DOS, Win16 and Win32. It's here because |
| 10070 | * it's shared between these systems. |
| 10071 | */ |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10072 | # if defined(PROTO) |
| 10073 | # define _cdecl |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10074 | # else |
| 10075 | # ifdef __BORLANDC__ |
| 10076 | # define _cdecl _RTLENTRYF |
| 10077 | # endif |
| 10078 | # endif |
| 10079 | |
| 10080 | /* |
| 10081 | * comparison function for qsort in dos_expandpath() |
| 10082 | */ |
| 10083 | static int _cdecl |
| 10084 | pstrcmp(const void *a, const void *b) |
| 10085 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 10086 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10087 | } |
| 10088 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10089 | /* |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10090 | * Recursively expand one path component into all matching files and/or |
| 10091 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10092 | * Return the number of matches found. |
| 10093 | * "path" has backslashes before chars that are not to be expanded, starting |
| 10094 | * at "path[wildoff]". |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10095 | * Return the number of matches found. |
| 10096 | * NOTE: much of this is identical to unix_expandpath(), keep in sync! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10097 | */ |
| 10098 | static int |
| 10099 | dos_expandpath( |
| 10100 | garray_T *gap, |
| 10101 | char_u *path, |
| 10102 | int wildoff, |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10103 | int flags, /* EW_* flags */ |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 10104 | int didstar) /* expanded "**" once already */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10105 | { |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10106 | char_u *buf; |
| 10107 | char_u *path_end; |
| 10108 | char_u *p, *s, *e; |
| 10109 | int start_len = gap->ga_len; |
| 10110 | char_u *pat; |
| 10111 | regmatch_T regmatch; |
| 10112 | int starts_with_dot; |
| 10113 | int matches; |
| 10114 | int len; |
| 10115 | int starstar = FALSE; |
| 10116 | static int stardepth = 0; /* depth for "**" expansion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10117 | WIN32_FIND_DATA fb; |
| 10118 | HANDLE hFind = (HANDLE)0; |
| 10119 | # ifdef FEAT_MBYTE |
| 10120 | WIN32_FIND_DATAW wfb; |
| 10121 | WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */ |
| 10122 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10123 | char_u *matchname; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10124 | int ok; |
| 10125 | |
| 10126 | /* Expanding "**" may take a long time, check for CTRL-C. */ |
| 10127 | if (stardepth > 0) |
| 10128 | { |
| 10129 | ui_breakcheck(); |
| 10130 | if (got_int) |
| 10131 | return 0; |
| 10132 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10133 | |
Bram Moolenaar | 7314efd | 2015-10-31 15:32:52 +0100 | [diff] [blame] | 10134 | /* Make room for file name. When doing encoding conversion the actual |
| 10135 | * length may be quite a bit longer, thus use the maximum possible length. */ |
| 10136 | buf = alloc((int)MAXPATHL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10137 | if (buf == NULL) |
| 10138 | return 0; |
| 10139 | |
| 10140 | /* |
| 10141 | * Find the first part in the path name that contains a wildcard or a ~1. |
| 10142 | * Copy it into buf, including the preceding characters. |
| 10143 | */ |
| 10144 | p = buf; |
| 10145 | s = buf; |
| 10146 | e = NULL; |
| 10147 | path_end = path; |
| 10148 | while (*path_end != NUL) |
| 10149 | { |
| 10150 | /* May ignore a wildcard that has a backslash before it; it will |
| 10151 | * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ |
| 10152 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 10153 | *p++ = *path_end++; |
| 10154 | else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') |
| 10155 | { |
| 10156 | if (e != NULL) |
| 10157 | break; |
| 10158 | s = p + 1; |
| 10159 | } |
| 10160 | else if (path_end >= path + wildoff |
| 10161 | && vim_strchr((char_u *)"*?[~", *path_end) != NULL) |
| 10162 | e = p; |
Bram Moolenaar | 780d4c3 | 2016-03-25 17:21:19 +0100 | [diff] [blame] | 10163 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10164 | if (has_mbyte) |
| 10165 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10166 | len = (*mb_ptr2len)(path_end); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10167 | STRNCPY(p, path_end, len); |
| 10168 | p += len; |
| 10169 | path_end += len; |
| 10170 | } |
| 10171 | else |
Bram Moolenaar | 780d4c3 | 2016-03-25 17:21:19 +0100 | [diff] [blame] | 10172 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10173 | *p++ = *path_end++; |
| 10174 | } |
| 10175 | e = p; |
| 10176 | *e = NUL; |
| 10177 | |
| 10178 | /* now we have one wildcard component between s and e */ |
| 10179 | /* Remove backslashes between "wildoff" and the start of the wildcard |
| 10180 | * component. */ |
| 10181 | for (p = buf + wildoff; p < s; ++p) |
| 10182 | if (rem_backslash(p)) |
| 10183 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 10184 | STRMOVE(p, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10185 | --e; |
| 10186 | --s; |
| 10187 | } |
| 10188 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10189 | /* Check for "**" between "s" and "e". */ |
| 10190 | for (p = s; p < e; ++p) |
| 10191 | if (p[0] == '*' && p[1] == '*') |
| 10192 | starstar = TRUE; |
| 10193 | |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10194 | starts_with_dot = *s == '.'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10195 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 10196 | if (pat == NULL) |
| 10197 | { |
| 10198 | vim_free(buf); |
| 10199 | return 0; |
| 10200 | } |
| 10201 | |
| 10202 | /* compile the regexp into a program */ |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10203 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
Bram Moolenaar | b560983 | 2011-07-20 15:04:58 +0200 | [diff] [blame] | 10204 | ++emsg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10205 | regmatch.rm_ic = TRUE; /* Always ignore case */ |
| 10206 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10207 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
Bram Moolenaar | b560983 | 2011-07-20 15:04:58 +0200 | [diff] [blame] | 10208 | --emsg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10209 | vim_free(pat); |
| 10210 | |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10211 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10212 | { |
| 10213 | vim_free(buf); |
| 10214 | return 0; |
| 10215 | } |
| 10216 | |
| 10217 | /* remember the pattern or file name being looked for */ |
| 10218 | matchname = vim_strsave(s); |
| 10219 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10220 | /* If "**" is by itself, this is the first time we encounter it and more |
| 10221 | * is following then find matches without any directory. */ |
| 10222 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 10223 | && *path_end == '/') |
| 10224 | { |
| 10225 | STRCPY(s, path_end + 1); |
| 10226 | ++stardepth; |
| 10227 | (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 10228 | --stardepth; |
| 10229 | } |
| 10230 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10231 | /* Scan all files in the directory with "dir/ *.*" */ |
| 10232 | STRCPY(s, "*.*"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10233 | # ifdef FEAT_MBYTE |
| 10234 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 10235 | { |
| 10236 | /* The active codepage differs from 'encoding'. Attempt using the |
| 10237 | * wide function. If it fails because it is not implemented fall back |
| 10238 | * to the non-wide version (for Windows 98) */ |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 10239 | wn = enc_to_utf16(buf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10240 | if (wn != NULL) |
| 10241 | { |
| 10242 | hFind = FindFirstFileW(wn, &wfb); |
| 10243 | if (hFind == INVALID_HANDLE_VALUE |
| 10244 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 10245 | VIM_CLEAR(wn); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10246 | } |
| 10247 | } |
| 10248 | |
| 10249 | if (wn == NULL) |
| 10250 | # endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 10251 | hFind = FindFirstFile((LPCSTR)buf, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10252 | ok = (hFind != INVALID_HANDLE_VALUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10253 | |
| 10254 | while (ok) |
| 10255 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10256 | # ifdef FEAT_MBYTE |
| 10257 | if (wn != NULL) |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 10258 | p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10259 | else |
| 10260 | # endif |
| 10261 | p = (char_u *)fb.cFileName; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10262 | /* Ignore entries starting with a dot, unless when asked for. Accept |
| 10263 | * all entries found with "matchname". */ |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10264 | if ((p[0] != '.' || starts_with_dot |
| 10265 | || ((flags & EW_DODOT) |
| 10266 | && p[1] != NUL && (p[1] != '.' || p[2] != NUL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10267 | && (matchname == NULL |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10268 | || (regmatch.regprog != NULL |
| 10269 | && vim_regexec(®match, p, (colnr_T)0)) |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 10270 | || ((flags & EW_NOTWILD) |
| 10271 | && fnamencmp(path + (s - buf), p, e - s) == 0))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10272 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10273 | STRCPY(s, p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10274 | len = (int)STRLEN(buf); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10275 | |
| 10276 | if (starstar && stardepth < 100) |
| 10277 | { |
| 10278 | /* For "**" in the pattern first go deeper in the tree to |
| 10279 | * find matches. */ |
| 10280 | STRCPY(buf + len, "/**"); |
| 10281 | STRCPY(buf + len + 3, path_end); |
| 10282 | ++stardepth; |
| 10283 | (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); |
| 10284 | --stardepth; |
| 10285 | } |
| 10286 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10287 | STRCPY(buf + len, path_end); |
| 10288 | if (mch_has_exp_wildcard(path_end)) |
| 10289 | { |
| 10290 | /* need to expand another component of the path */ |
| 10291 | /* remove backslashes for the remaining components only */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10292 | (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10293 | } |
| 10294 | else |
| 10295 | { |
| 10296 | /* no more wildcards, check if there is a match */ |
| 10297 | /* remove backslashes for the remaining components only */ |
| 10298 | if (*path_end != 0) |
| 10299 | backslash_halve(buf + len + 1); |
| 10300 | if (mch_getperm(buf) >= 0) /* add existing file */ |
| 10301 | addfile(gap, buf, flags); |
| 10302 | } |
| 10303 | } |
| 10304 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10305 | # ifdef FEAT_MBYTE |
| 10306 | if (wn != NULL) |
| 10307 | { |
| 10308 | vim_free(p); |
| 10309 | ok = FindNextFileW(hFind, &wfb); |
| 10310 | } |
| 10311 | else |
| 10312 | # endif |
| 10313 | ok = FindNextFile(hFind, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10314 | |
| 10315 | /* If no more matches and no match was used, try expanding the name |
| 10316 | * itself. Finds the long name of a short filename. */ |
| 10317 | if (!ok && matchname != NULL && gap->ga_len == start_len) |
| 10318 | { |
| 10319 | STRCPY(s, matchname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10320 | FindClose(hFind); |
| 10321 | # ifdef FEAT_MBYTE |
| 10322 | if (wn != NULL) |
| 10323 | { |
| 10324 | vim_free(wn); |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 10325 | wn = enc_to_utf16(buf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10326 | if (wn != NULL) |
| 10327 | hFind = FindFirstFileW(wn, &wfb); |
| 10328 | } |
| 10329 | if (wn == NULL) |
| 10330 | # endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 10331 | hFind = FindFirstFile((LPCSTR)buf, &fb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10332 | ok = (hFind != INVALID_HANDLE_VALUE); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 10333 | VIM_CLEAR(matchname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10334 | } |
| 10335 | } |
| 10336 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10337 | FindClose(hFind); |
| 10338 | # ifdef FEAT_MBYTE |
| 10339 | vim_free(wn); |
| 10340 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10341 | vim_free(buf); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10342 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10343 | vim_free(matchname); |
| 10344 | |
| 10345 | matches = gap->ga_len - start_len; |
| 10346 | if (matches > 0) |
| 10347 | qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, |
| 10348 | sizeof(char_u *), pstrcmp); |
| 10349 | return matches; |
| 10350 | } |
| 10351 | |
| 10352 | int |
| 10353 | mch_expandpath( |
| 10354 | garray_T *gap, |
| 10355 | char_u *path, |
| 10356 | int flags) /* EW_* flags */ |
| 10357 | { |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10358 | return dos_expandpath(gap, path, 0, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10359 | } |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10360 | # endif /* WIN3264 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10361 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10362 | #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ |
| 10363 | || defined(PROTO) |
| 10364 | /* |
| 10365 | * Unix style wildcard expansion code. |
| 10366 | * It's here because it's used both for Unix and Mac. |
| 10367 | */ |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 10368 | static int pstrcmp(const void *, const void *); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10369 | |
| 10370 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10371 | pstrcmp(const void *a, const void *b) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10372 | { |
| 10373 | return (pathcmp(*(char **)a, *(char **)b, -1)); |
| 10374 | } |
| 10375 | |
| 10376 | /* |
| 10377 | * Recursively expand one path component into all matching files and/or |
| 10378 | * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. |
| 10379 | * "path" has backslashes before chars that are not to be expanded, starting |
| 10380 | * at "path + wildoff". |
| 10381 | * Return the number of matches found. |
| 10382 | * NOTE: much of this is identical to dos_expandpath(), keep in sync! |
| 10383 | */ |
| 10384 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10385 | unix_expandpath( |
| 10386 | garray_T *gap, |
| 10387 | char_u *path, |
| 10388 | int wildoff, |
| 10389 | int flags, /* EW_* flags */ |
| 10390 | int didstar) /* expanded "**" once already */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10391 | { |
| 10392 | char_u *buf; |
| 10393 | char_u *path_end; |
| 10394 | char_u *p, *s, *e; |
| 10395 | int start_len = gap->ga_len; |
| 10396 | char_u *pat; |
| 10397 | regmatch_T regmatch; |
| 10398 | int starts_with_dot; |
| 10399 | int matches; |
| 10400 | int len; |
| 10401 | int starstar = FALSE; |
| 10402 | static int stardepth = 0; /* depth for "**" expansion */ |
| 10403 | |
| 10404 | DIR *dirp; |
| 10405 | struct dirent *dp; |
| 10406 | |
| 10407 | /* Expanding "**" may take a long time, check for CTRL-C. */ |
| 10408 | if (stardepth > 0) |
| 10409 | { |
| 10410 | ui_breakcheck(); |
| 10411 | if (got_int) |
| 10412 | return 0; |
| 10413 | } |
| 10414 | |
| 10415 | /* make room for file name */ |
| 10416 | buf = alloc((int)STRLEN(path) + BASENAMELEN + 5); |
| 10417 | if (buf == NULL) |
| 10418 | return 0; |
| 10419 | |
| 10420 | /* |
| 10421 | * Find the first part in the path name that contains a wildcard. |
Bram Moolenaar | 2d0b92f | 2012-04-30 21:09:43 +0200 | [diff] [blame] | 10422 | * 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] | 10423 | * Copy it into "buf", including the preceding characters. |
| 10424 | */ |
| 10425 | p = buf; |
| 10426 | s = buf; |
| 10427 | e = NULL; |
| 10428 | path_end = path; |
| 10429 | while (*path_end != NUL) |
| 10430 | { |
| 10431 | /* May ignore a wildcard that has a backslash before it; it will |
| 10432 | * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ |
| 10433 | if (path_end >= path + wildoff && rem_backslash(path_end)) |
| 10434 | *p++ = *path_end++; |
| 10435 | else if (*path_end == '/') |
| 10436 | { |
| 10437 | if (e != NULL) |
| 10438 | break; |
| 10439 | s = p + 1; |
| 10440 | } |
| 10441 | else if (path_end >= path + wildoff |
Bram Moolenaar | 2d0b92f | 2012-04-30 21:09:43 +0200 | [diff] [blame] | 10442 | && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 10443 | || (!p_fic && (flags & EW_ICASE) |
| 10444 | && isalpha(PTR2CHAR(path_end))))) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10445 | e = p; |
| 10446 | #ifdef FEAT_MBYTE |
| 10447 | if (has_mbyte) |
| 10448 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10449 | len = (*mb_ptr2len)(path_end); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10450 | STRNCPY(p, path_end, len); |
| 10451 | p += len; |
| 10452 | path_end += len; |
| 10453 | } |
| 10454 | else |
| 10455 | #endif |
| 10456 | *p++ = *path_end++; |
| 10457 | } |
| 10458 | e = p; |
| 10459 | *e = NUL; |
| 10460 | |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 10461 | /* Now we have one wildcard component between "s" and "e". */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10462 | /* Remove backslashes between "wildoff" and the start of the wildcard |
| 10463 | * component. */ |
| 10464 | for (p = buf + wildoff; p < s; ++p) |
| 10465 | if (rem_backslash(p)) |
| 10466 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 10467 | STRMOVE(p, p + 1); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10468 | --e; |
| 10469 | --s; |
| 10470 | } |
| 10471 | |
| 10472 | /* Check for "**" between "s" and "e". */ |
| 10473 | for (p = s; p < e; ++p) |
| 10474 | if (p[0] == '*' && p[1] == '*') |
| 10475 | starstar = TRUE; |
| 10476 | |
| 10477 | /* convert the file pattern to a regexp pattern */ |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10478 | starts_with_dot = *s == '.'; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10479 | pat = file_pat_to_reg_pat(s, e, NULL, FALSE); |
| 10480 | if (pat == NULL) |
| 10481 | { |
| 10482 | vim_free(buf); |
| 10483 | return 0; |
| 10484 | } |
| 10485 | |
| 10486 | /* compile the regexp into a program */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 10487 | if (flags & EW_ICASE) |
| 10488 | regmatch.rm_ic = TRUE; /* 'wildignorecase' set */ |
| 10489 | else |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 10490 | regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */ |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10491 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 10492 | ++emsg_silent; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10493 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10494 | if (flags & (EW_NOERROR | EW_NOTWILD)) |
| 10495 | --emsg_silent; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10496 | vim_free(pat); |
| 10497 | |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10498 | if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10499 | { |
| 10500 | vim_free(buf); |
| 10501 | return 0; |
| 10502 | } |
| 10503 | |
| 10504 | /* If "**" is by itself, this is the first time we encounter it and more |
| 10505 | * is following then find matches without any directory. */ |
| 10506 | if (!didstar && stardepth < 100 && starstar && e - s == 2 |
| 10507 | && *path_end == '/') |
| 10508 | { |
| 10509 | STRCPY(s, path_end + 1); |
| 10510 | ++stardepth; |
| 10511 | (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); |
| 10512 | --stardepth; |
| 10513 | } |
| 10514 | |
| 10515 | /* open the directory for scanning */ |
| 10516 | *s = NUL; |
| 10517 | dirp = opendir(*buf == NUL ? "." : (char *)buf); |
| 10518 | |
| 10519 | /* Find all matching entries */ |
| 10520 | if (dirp != NULL) |
| 10521 | { |
| 10522 | for (;;) |
| 10523 | { |
| 10524 | dp = readdir(dirp); |
| 10525 | if (dp == NULL) |
| 10526 | break; |
Bram Moolenaar | d82103e | 2016-01-17 17:04:05 +0100 | [diff] [blame] | 10527 | if ((dp->d_name[0] != '.' || starts_with_dot |
| 10528 | || ((flags & EW_DODOT) |
| 10529 | && dp->d_name[1] != NUL |
| 10530 | && (dp->d_name[1] != '.' || dp->d_name[2] != NUL))) |
Bram Moolenaar | 1f5965b | 2012-01-10 18:37:58 +0100 | [diff] [blame] | 10531 | && ((regmatch.regprog != NULL && vim_regexec(®match, |
| 10532 | (char_u *)dp->d_name, (colnr_T)0)) |
Bram Moolenaar | 0b573a5 | 2011-07-27 17:31:47 +0200 | [diff] [blame] | 10533 | || ((flags & EW_NOTWILD) |
| 10534 | && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0))) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10535 | { |
| 10536 | STRCPY(s, dp->d_name); |
| 10537 | len = STRLEN(buf); |
| 10538 | |
| 10539 | if (starstar && stardepth < 100) |
| 10540 | { |
| 10541 | /* For "**" in the pattern first go deeper in the tree to |
| 10542 | * find matches. */ |
| 10543 | STRCPY(buf + len, "/**"); |
| 10544 | STRCPY(buf + len + 3, path_end); |
| 10545 | ++stardepth; |
| 10546 | (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); |
| 10547 | --stardepth; |
| 10548 | } |
| 10549 | |
| 10550 | STRCPY(buf + len, path_end); |
| 10551 | if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */ |
| 10552 | { |
| 10553 | /* need to expand another component of the path */ |
| 10554 | /* remove backslashes for the remaining components only */ |
| 10555 | (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); |
| 10556 | } |
| 10557 | else |
| 10558 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 10559 | stat_T sb; |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 10560 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10561 | /* no more wildcards, check if there is a match */ |
| 10562 | /* remove backslashes for the remaining components only */ |
| 10563 | if (*path_end != NUL) |
| 10564 | backslash_halve(buf + len + 1); |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 10565 | /* add existing file or symbolic link */ |
Bram Moolenaar | ab11a59 | 2015-03-06 22:00:11 +0100 | [diff] [blame] | 10566 | if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0 |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 10567 | : mch_getperm(buf) >= 0) |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10568 | { |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 10569 | #ifdef MACOS_CONVERT |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10570 | size_t precomp_len = STRLEN(buf)+1; |
| 10571 | char_u *precomp_buf = |
| 10572 | mac_precompose_path(buf, precomp_len, &precomp_len); |
Bram Moolenaar | 95e9b49 | 2006-03-15 23:04:43 +0000 | [diff] [blame] | 10573 | |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10574 | if (precomp_buf) |
| 10575 | { |
| 10576 | mch_memmove(buf, precomp_buf, precomp_len); |
| 10577 | vim_free(precomp_buf); |
| 10578 | } |
| 10579 | #endif |
| 10580 | addfile(gap, buf, flags); |
| 10581 | } |
| 10582 | } |
| 10583 | } |
| 10584 | } |
| 10585 | |
| 10586 | closedir(dirp); |
| 10587 | } |
| 10588 | |
| 10589 | vim_free(buf); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10590 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 10591 | |
| 10592 | matches = gap->ga_len - start_len; |
| 10593 | if (matches > 0) |
| 10594 | qsort(((char_u **)gap->ga_data) + start_len, matches, |
| 10595 | sizeof(char_u *), pstrcmp); |
| 10596 | return matches; |
| 10597 | } |
| 10598 | #endif |
| 10599 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10600 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 10601 | static int find_previous_pathsep(char_u *path, char_u **psep); |
| 10602 | static int is_unique(char_u *maybe_unique, garray_T *gap, int i); |
| 10603 | static void expand_path_option(char_u *curdir, garray_T *gap); |
| 10604 | static char_u *get_path_cutoff(char_u *fname, garray_T *gap); |
| 10605 | static void uniquefy_paths(garray_T *gap, char_u *pattern); |
| 10606 | static int expand_in_path(garray_T *gap, char_u *pattern, int flags); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10607 | |
| 10608 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10609 | * Moves "*psep" back to the previous path separator in "path". |
| 10610 | * Returns FAIL is "*psep" ends up at the beginning of "path". |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10611 | */ |
| 10612 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10613 | find_previous_pathsep(char_u *path, char_u **psep) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10614 | { |
| 10615 | /* skip the current separator */ |
| 10616 | if (*psep > path && vim_ispathsep(**psep)) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10617 | --*psep; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10618 | |
| 10619 | /* find the previous separator */ |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10620 | while (*psep > path) |
| 10621 | { |
| 10622 | if (vim_ispathsep(**psep)) |
| 10623 | return OK; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 10624 | MB_PTR_BACK(path, *psep); |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10625 | } |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10626 | |
| 10627 | return FAIL; |
| 10628 | } |
| 10629 | |
| 10630 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10631 | * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap". |
| 10632 | * "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] | 10633 | */ |
| 10634 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10635 | is_unique(char_u *maybe_unique, garray_T *gap, int i) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10636 | { |
| 10637 | int j; |
| 10638 | int candidate_len; |
| 10639 | int other_path_len; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10640 | char_u **other_paths = (char_u **)gap->ga_data; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10641 | char_u *rival; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10642 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10643 | for (j = 0; j < gap->ga_len; j++) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10644 | { |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10645 | if (j == i) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10646 | continue; /* don't compare it with itself */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10647 | |
Bram Moolenaar | 624c7aa | 2010-07-16 20:38:52 +0200 | [diff] [blame] | 10648 | candidate_len = (int)STRLEN(maybe_unique); |
| 10649 | other_path_len = (int)STRLEN(other_paths[j]); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10650 | if (other_path_len < candidate_len) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10651 | continue; /* it's different when it's shorter */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10652 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10653 | rival = other_paths[j] + other_path_len - candidate_len; |
Bram Moolenaar | da9836c | 2010-08-16 21:53:27 +0200 | [diff] [blame] | 10654 | if (fnamecmp(maybe_unique, rival) == 0 |
| 10655 | && (rival == other_paths[j] || vim_ispathsep(*(rival - 1)))) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10656 | return FALSE; /* match */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10657 | } |
| 10658 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10659 | return TRUE; /* no match found */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10660 | } |
| 10661 | |
| 10662 | /* |
Bram Moolenaar | 1a509df | 2010-08-01 17:59:57 +0200 | [diff] [blame] | 10663 | * 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] | 10664 | * paths are expanded to their equivalent fullpath. This includes the "." |
| 10665 | * (relative to current buffer directory) and empty path (relative to current |
| 10666 | * directory) notations. |
| 10667 | * |
| 10668 | * TODO: handle upward search (;) and path limiter (**N) notations by |
| 10669 | * expanding each into their equivalent path(s). |
| 10670 | */ |
| 10671 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10672 | expand_path_option(char_u *curdir, garray_T *gap) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10673 | { |
| 10674 | char_u *path_option = *curbuf->b_p_path == NUL |
| 10675 | ? p_path : curbuf->b_p_path; |
| 10676 | char_u *buf; |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10677 | char_u *p; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10678 | int len; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10679 | |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10680 | if ((buf = alloc((int)MAXPATHL)) == NULL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10681 | return; |
| 10682 | |
| 10683 | while (*path_option != NUL) |
| 10684 | { |
| 10685 | copy_option_part(&path_option, buf, MAXPATHL, " ,"); |
| 10686 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10687 | if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1]))) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10688 | { |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10689 | /* Relative to current buffer: |
| 10690 | * "/path/file" + "." -> "/path/" |
| 10691 | * "/path/file" + "./subdir" -> "/path/subdir" */ |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10692 | if (curbuf->b_ffname == NULL) |
| 10693 | continue; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10694 | p = gettail(curbuf->b_ffname); |
| 10695 | len = (int)(p - curbuf->b_ffname); |
| 10696 | if (len + (int)STRLEN(buf) >= MAXPATHL) |
| 10697 | continue; |
| 10698 | if (buf[1] == NUL) |
| 10699 | buf[len] = NUL; |
| 10700 | else |
| 10701 | STRMOVE(buf + len, buf + 2); |
| 10702 | mch_memmove(buf, curbuf->b_ffname, len); |
| 10703 | simplify_filename(buf); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10704 | } |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10705 | else if (buf[0] == NUL) |
| 10706 | /* relative to current directory */ |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10707 | STRCPY(buf, curdir); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 10708 | else if (path_with_url(buf)) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10709 | /* URL can't be used here */ |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 10710 | continue; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10711 | else if (!mch_isFullName(buf)) |
| 10712 | { |
| 10713 | /* Expand relative path to their full path equivalent */ |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10714 | len = (int)STRLEN(curdir); |
| 10715 | if (len + (int)STRLEN(buf) + 3 > MAXPATHL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10716 | continue; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10717 | STRMOVE(buf + len + 1, buf); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10718 | STRCPY(buf, curdir); |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10719 | buf[len] = PATHSEP; |
Bram Moolenaar | 57adda1 | 2010-08-03 22:11:29 +0200 | [diff] [blame] | 10720 | simplify_filename(buf); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10721 | } |
| 10722 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10723 | if (ga_grow(gap, 1) == FAIL) |
| 10724 | break; |
Bram Moolenaar | 811fe63 | 2013-04-24 17:34:20 +0200 | [diff] [blame] | 10725 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10726 | # if defined(MSWIN) |
Bram Moolenaar | 811fe63 | 2013-04-24 17:34:20 +0200 | [diff] [blame] | 10727 | /* Avoid the path ending in a backslash, it fails when a comma is |
| 10728 | * appended. */ |
Bram Moolenaar | 4e0d974 | 2013-05-04 03:40:27 +0200 | [diff] [blame] | 10729 | len = (int)STRLEN(buf); |
Bram Moolenaar | 811fe63 | 2013-04-24 17:34:20 +0200 | [diff] [blame] | 10730 | if (buf[len - 1] == '\\') |
| 10731 | buf[len - 1] = '/'; |
| 10732 | # endif |
| 10733 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10734 | p = vim_strsave(buf); |
| 10735 | if (p == NULL) |
| 10736 | break; |
| 10737 | ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10738 | } |
| 10739 | |
| 10740 | vim_free(buf); |
| 10741 | } |
| 10742 | |
| 10743 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10744 | * Returns a pointer to the file or directory name in "fname" that matches the |
| 10745 | * 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] | 10746 | * |
| 10747 | * path: /foo/bar/baz |
| 10748 | * fname: /foo/bar/baz/quux.txt |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10749 | * returns: ^this |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10750 | */ |
| 10751 | static char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10752 | get_path_cutoff(char_u *fname, garray_T *gap) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10753 | { |
| 10754 | int i; |
| 10755 | int maxlen = 0; |
| 10756 | char_u **path_part = (char_u **)gap->ga_data; |
| 10757 | char_u *cutoff = NULL; |
| 10758 | |
| 10759 | for (i = 0; i < gap->ga_len; i++) |
| 10760 | { |
| 10761 | int j = 0; |
| 10762 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10763 | while ((fname[j] == path_part[i][j] |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10764 | # if defined(MSWIN) |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10765 | || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j])) |
| 10766 | #endif |
| 10767 | ) && fname[j] != NUL && path_part[i][j] != NUL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10768 | j++; |
| 10769 | if (j > maxlen) |
| 10770 | { |
| 10771 | maxlen = j; |
| 10772 | cutoff = &fname[j]; |
| 10773 | } |
| 10774 | } |
| 10775 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10776 | /* skip to the file or directory name */ |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10777 | if (cutoff != NULL) |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10778 | while (vim_ispathsep(*cutoff)) |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 10779 | MB_PTR_ADV(cutoff); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10780 | |
| 10781 | return cutoff; |
| 10782 | } |
| 10783 | |
| 10784 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10785 | * Sorts, removes duplicates and modifies all the fullpath names in "gap" so |
| 10786 | * that they are unique with respect to each other while conserving the part |
| 10787 | * 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] | 10788 | */ |
| 10789 | static void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10790 | uniquefy_paths(garray_T *gap, char_u *pattern) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10791 | { |
Bram Moolenaar | cb9d45c | 2010-07-20 18:10:15 +0200 | [diff] [blame] | 10792 | int i; |
| 10793 | int len; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10794 | char_u **fnames = (char_u **)gap->ga_data; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10795 | int sort_again = FALSE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10796 | char_u *pat; |
| 10797 | char_u *file_pattern; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10798 | char_u *curdir; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10799 | regmatch_T regmatch; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10800 | garray_T path_ga; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10801 | char_u **in_curdir = NULL; |
| 10802 | char_u *short_name; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10803 | |
Bram Moolenaar | cb9d45c | 2010-07-20 18:10:15 +0200 | [diff] [blame] | 10804 | remove_duplicates(gap); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10805 | ga_init2(&path_ga, (int)sizeof(char_u *), 1); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10806 | |
| 10807 | /* |
| 10808 | * We need to prepend a '*' at the beginning of file_pattern so that the |
| 10809 | * regex matches anywhere in the path. FIXME: is this valid for all |
Bram Moolenaar | b31e438 | 2010-07-24 16:01:56 +0200 | [diff] [blame] | 10810 | * possible patterns? |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10811 | */ |
Bram Moolenaar | 624c7aa | 2010-07-16 20:38:52 +0200 | [diff] [blame] | 10812 | len = (int)STRLEN(pattern); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10813 | file_pattern = alloc(len + 2); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10814 | if (file_pattern == NULL) |
| 10815 | return; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10816 | file_pattern[0] = '*'; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10817 | file_pattern[1] = NUL; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10818 | STRCAT(file_pattern, pattern); |
| 10819 | pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE); |
| 10820 | vim_free(file_pattern); |
Bram Moolenaar | b31e438 | 2010-07-24 16:01:56 +0200 | [diff] [blame] | 10821 | if (pat == NULL) |
| 10822 | return; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10823 | |
Bram Moolenaar | b31e438 | 2010-07-24 16:01:56 +0200 | [diff] [blame] | 10824 | regmatch.rm_ic = TRUE; /* always ignore case */ |
| 10825 | regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); |
| 10826 | vim_free(pat); |
| 10827 | if (regmatch.regprog == NULL) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10828 | return; |
| 10829 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10830 | if ((curdir = alloc((int)(MAXPATHL))) == NULL) |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10831 | goto theend; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10832 | mch_dirname(curdir, MAXPATHL); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10833 | expand_path_option(curdir, &path_ga); |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10834 | |
| 10835 | in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *)); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10836 | if (in_curdir == NULL) |
| 10837 | goto theend; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10838 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10839 | for (i = 0; i < gap->ga_len && !got_int; i++) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10840 | { |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10841 | char_u *path = fnames[i]; |
| 10842 | int is_in_curdir; |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10843 | char_u *dir_end = gettail_dir(path); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10844 | char_u *pathsep_p; |
| 10845 | char_u *path_cutoff; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10846 | |
Bram Moolenaar | 624c7aa | 2010-07-16 20:38:52 +0200 | [diff] [blame] | 10847 | len = (int)STRLEN(path); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10848 | is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0 |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10849 | && curdir[dir_end - path] == NUL; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10850 | if (is_in_curdir) |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10851 | in_curdir[i] = vim_strsave(path); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10852 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10853 | /* Shorten the filename while maintaining its uniqueness */ |
| 10854 | path_cutoff = get_path_cutoff(path, &path_ga); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10855 | |
Bram Moolenaar | 73d4e4c | 2016-08-27 21:55:13 +0200 | [diff] [blame] | 10856 | /* Don't assume all files can be reached without path when search |
| 10857 | * pattern starts with star star slash, so only remove path_cutoff |
| 10858 | * when possible. */ |
| 10859 | if (pattern[0] == '*' && pattern[1] == '*' |
| 10860 | && vim_ispathsep_nocolon(pattern[2]) |
| 10861 | && path_cutoff != NULL |
| 10862 | && vim_regexec(®match, path_cutoff, (colnr_T)0) |
| 10863 | && is_unique(path_cutoff, gap, i)) |
| 10864 | { |
| 10865 | sort_again = TRUE; |
| 10866 | mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1); |
| 10867 | } |
| 10868 | else |
| 10869 | { |
| 10870 | /* Here all files can be reached without path, so get shortest |
| 10871 | * unique path. We start at the end of the path. */ |
| 10872 | pathsep_p = path + len - 1; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10873 | |
Bram Moolenaar | 73d4e4c | 2016-08-27 21:55:13 +0200 | [diff] [blame] | 10874 | while (find_previous_pathsep(path, &pathsep_p)) |
| 10875 | if (vim_regexec(®match, pathsep_p + 1, (colnr_T)0) |
| 10876 | && is_unique(pathsep_p + 1, gap, i) |
| 10877 | && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff) |
| 10878 | { |
| 10879 | sort_again = TRUE; |
| 10880 | mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p)); |
| 10881 | break; |
| 10882 | } |
| 10883 | } |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10884 | |
| 10885 | if (mch_isFullName(path)) |
| 10886 | { |
| 10887 | /* |
| 10888 | * Last resort: shorten relative to curdir if possible. |
| 10889 | * 'possible' means: |
| 10890 | * 1. It is under the current directory. |
| 10891 | * 2. The result is actually shorter than the original. |
| 10892 | * |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10893 | * Before curdir After |
| 10894 | * /foo/bar/file.txt /foo/bar ./file.txt |
| 10895 | * c:\foo\bar\file.txt c:\foo\bar .\file.txt |
| 10896 | * /file.txt / /file.txt |
| 10897 | * c:\file.txt c:\ .\file.txt |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10898 | */ |
| 10899 | short_name = shorten_fname(path, curdir); |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10900 | if (short_name != NULL && short_name > path + 1 |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 10901 | #if defined(MSWIN) |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10902 | /* On windows, |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10903 | * shorten_fname("c:\a\a.txt", "c:\a\b") |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10904 | * returns "\a\a.txt", which is not really the short |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10905 | * name, hence: */ |
Bram Moolenaar | 3171026 | 2010-08-13 13:36:15 +0200 | [diff] [blame] | 10906 | && !vim_ispathsep(*short_name) |
| 10907 | #endif |
| 10908 | ) |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10909 | { |
| 10910 | STRCPY(path, "."); |
| 10911 | add_pathsep(path); |
Bram Moolenaar | cda000e | 2010-08-14 13:34:39 +0200 | [diff] [blame] | 10912 | STRMOVE(path + STRLEN(path), short_name); |
Bram Moolenaar | 9bc040c | 2010-08-11 22:05:57 +0200 | [diff] [blame] | 10913 | } |
| 10914 | } |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10915 | ui_breakcheck(); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10916 | } |
| 10917 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10918 | /* Shorten filenames in /in/current/directory/{filename} */ |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10919 | for (i = 0; i < gap->ga_len && !got_int; i++) |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10920 | { |
| 10921 | char_u *rel_path; |
| 10922 | char_u *path = in_curdir[i]; |
| 10923 | |
| 10924 | if (path == NULL) |
| 10925 | continue; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10926 | |
| 10927 | /* If the {filename} is not unique, change it to ./{filename}. |
| 10928 | * Else reduce it to {filename} */ |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10929 | short_name = shorten_fname(path, curdir); |
| 10930 | if (short_name == NULL) |
| 10931 | short_name = path; |
| 10932 | if (is_unique(short_name, gap, i)) |
| 10933 | { |
| 10934 | STRCPY(fnames[i], short_name); |
| 10935 | continue; |
| 10936 | } |
| 10937 | |
| 10938 | rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2)); |
| 10939 | if (rel_path == NULL) |
| 10940 | goto theend; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10941 | STRCPY(rel_path, "."); |
| 10942 | add_pathsep(rel_path); |
| 10943 | STRCAT(rel_path, short_name); |
| 10944 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10945 | vim_free(fnames[i]); |
| 10946 | fnames[i] = rel_path; |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10947 | sort_again = TRUE; |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 10948 | ui_breakcheck(); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10949 | } |
| 10950 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10951 | theend: |
| 10952 | vim_free(curdir); |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10953 | if (in_curdir != NULL) |
| 10954 | { |
| 10955 | for (i = 0; i < gap->ga_len; i++) |
| 10956 | vim_free(in_curdir[i]); |
| 10957 | vim_free(in_curdir); |
| 10958 | } |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10959 | ga_clear_strings(&path_ga); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 10960 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10961 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10962 | if (sort_again) |
Bram Moolenaar | cb9d45c | 2010-07-20 18:10:15 +0200 | [diff] [blame] | 10963 | remove_duplicates(gap); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10964 | } |
| 10965 | |
| 10966 | /* |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10967 | * Calls globpath() with 'path' values for the given pattern and stores the |
| 10968 | * result in "gap". |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10969 | * Returns the total number of matches. |
| 10970 | */ |
| 10971 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 10972 | expand_in_path( |
| 10973 | garray_T *gap, |
| 10974 | char_u *pattern, |
| 10975 | int flags) /* EW_* flags */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10976 | { |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10977 | char_u *curdir; |
| 10978 | garray_T path_ga; |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 10979 | char_u *paths = NULL; |
Bram Moolenaar | 8a37b03 | 2018-02-03 20:43:08 +0100 | [diff] [blame] | 10980 | int glob_flags = 0; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 10981 | |
Bram Moolenaar | 7f0f621 | 2010-08-03 22:21:00 +0200 | [diff] [blame] | 10982 | if ((curdir = alloc((unsigned)MAXPATHL)) == NULL) |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10983 | return 0; |
| 10984 | mch_dirname(curdir, MAXPATHL); |
| 10985 | |
Bram Moolenaar | 0be992e | 2010-08-12 21:50:51 +0200 | [diff] [blame] | 10986 | ga_init2(&path_ga, (int)sizeof(char_u *), 1); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10987 | expand_path_option(curdir, &path_ga); |
| 10988 | vim_free(curdir); |
Bram Moolenaar | 006d2b0 | 2010-08-04 12:39:44 +0200 | [diff] [blame] | 10989 | if (path_ga.ga_len == 0) |
| 10990 | return 0; |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10991 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 10992 | paths = ga_concat_strings(&path_ga, ","); |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 10993 | ga_clear_strings(&path_ga); |
| 10994 | if (paths == NULL) |
Bram Moolenaar | 7f0f621 | 2010-08-03 22:21:00 +0200 | [diff] [blame] | 10995 | return 0; |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 10996 | |
Bram Moolenaar | 8a37b03 | 2018-02-03 20:43:08 +0100 | [diff] [blame] | 10997 | if (flags & EW_ICASE) |
| 10998 | glob_flags |= WILD_ICASE; |
| 10999 | if (flags & EW_ADDSLASH) |
| 11000 | glob_flags |= WILD_ADD_SLASH; |
| 11001 | globpath(paths, pattern, gap, glob_flags); |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 11002 | vim_free(paths); |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11003 | |
Bram Moolenaar | bdc975c | 2010-08-02 21:33:37 +0200 | [diff] [blame] | 11004 | return gap->ga_len; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11005 | } |
| 11006 | #endif |
| 11007 | |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 11008 | #if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 11009 | /* |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 11010 | * Sort "gap" and remove duplicate entries. "gap" is expected to contain a |
| 11011 | * list of file names in allocated memory. |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 11012 | */ |
| 11013 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11014 | remove_duplicates(garray_T *gap) |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 11015 | { |
| 11016 | int i; |
| 11017 | int j; |
| 11018 | char_u **fnames = (char_u **)gap->ga_data; |
| 11019 | |
Bram Moolenaar | dc685ab | 2010-08-13 21:16:49 +0200 | [diff] [blame] | 11020 | sort_strings(fnames, gap->ga_len); |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 11021 | for (i = gap->ga_len - 1; i > 0; --i) |
| 11022 | if (fnamecmp(fnames[i - 1], fnames[i]) == 0) |
| 11023 | { |
| 11024 | vim_free(fnames[i]); |
| 11025 | for (j = i + 1; j < gap->ga_len; ++j) |
| 11026 | fnames[j - 1] = fnames[j]; |
| 11027 | --gap->ga_len; |
| 11028 | } |
| 11029 | } |
| 11030 | #endif |
| 11031 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 11032 | static int has_env_var(char_u *p); |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11033 | |
| 11034 | /* |
| 11035 | * Return TRUE if "p" contains what looks like an environment variable. |
| 11036 | * Allowing for escaping. |
| 11037 | */ |
| 11038 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11039 | has_env_var(char_u *p) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11040 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 11041 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11042 | { |
| 11043 | if (*p == '\\' && p[1] != NUL) |
| 11044 | ++p; |
| 11045 | else if (vim_strchr((char_u *) |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 11046 | #if defined(MSWIN) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11047 | "$%" |
| 11048 | #else |
| 11049 | "$" |
| 11050 | #endif |
| 11051 | , *p) != NULL) |
| 11052 | return TRUE; |
| 11053 | } |
| 11054 | return FALSE; |
| 11055 | } |
| 11056 | |
| 11057 | #ifdef SPECIAL_WILDCHAR |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 11058 | static int has_special_wildchar(char_u *p); |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11059 | |
| 11060 | /* |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 11061 | * Return TRUE if "p" contains a special wildcard character, one that Vim |
| 11062 | * cannot expand, requires using a shell. |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11063 | */ |
| 11064 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11065 | has_special_wildchar(char_u *p) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11066 | { |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 11067 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11068 | { |
Bram Moolenaar | 187a4f2 | 2017-02-23 17:07:14 +0100 | [diff] [blame] | 11069 | /* Allow for escaping. */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11070 | if (*p == '\\' && p[1] != NUL) |
| 11071 | ++p; |
| 11072 | else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL) |
| 11073 | return TRUE; |
| 11074 | } |
| 11075 | return FALSE; |
| 11076 | } |
| 11077 | #endif |
| 11078 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11079 | /* |
| 11080 | * Generic wildcard expansion code. |
| 11081 | * |
| 11082 | * Characters in "pat" that should not be expanded must be preceded with a |
| 11083 | * backslash. E.g., "/path\ with\ spaces/my\*star*" |
| 11084 | * |
| 11085 | * Return FAIL when no single file was found. In this case "num_file" is not |
| 11086 | * set, and "file" may contain an error message. |
| 11087 | * Return OK when some files found. "num_file" is set to the number of |
| 11088 | * matches, "file" to the array of matches. Call FreeWild() later. |
| 11089 | */ |
| 11090 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11091 | gen_expand_wildcards( |
| 11092 | int num_pat, /* number of input patterns */ |
| 11093 | char_u **pat, /* array of input patterns */ |
| 11094 | int *num_file, /* resulting number of files */ |
| 11095 | char_u ***file, /* array of resulting files */ |
| 11096 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11097 | { |
| 11098 | int i; |
| 11099 | garray_T ga; |
| 11100 | char_u *p; |
| 11101 | static int recursive = FALSE; |
| 11102 | int add_pat; |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11103 | int retval = OK; |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11104 | #if defined(FEAT_SEARCHPATH) |
| 11105 | int did_expand_in_path = FALSE; |
| 11106 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11107 | |
| 11108 | /* |
| 11109 | * expand_env() is called to expand things like "~user". If this fails, |
| 11110 | * it calls ExpandOne(), which brings us back here. In this case, always |
| 11111 | * call the machine specific expansion function, if possible. Otherwise, |
| 11112 | * return FAIL. |
| 11113 | */ |
| 11114 | if (recursive) |
| 11115 | #ifdef SPECIAL_WILDCHAR |
| 11116 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 11117 | #else |
| 11118 | return FAIL; |
| 11119 | #endif |
| 11120 | |
| 11121 | #ifdef SPECIAL_WILDCHAR |
| 11122 | /* |
| 11123 | * If there are any special wildcard characters which we cannot handle |
| 11124 | * here, call machine specific function for all the expansion. This |
| 11125 | * avoids starting the shell for each argument separately. |
| 11126 | * For `=expr` do use the internal function. |
| 11127 | */ |
| 11128 | for (i = 0; i < num_pat; i++) |
| 11129 | { |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11130 | if (has_special_wildchar(pat[i]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11131 | # ifdef VIM_BACKTICK |
| 11132 | && !(vim_backtick(pat[i]) && pat[i][1] == '=') |
| 11133 | # endif |
| 11134 | ) |
| 11135 | return mch_expand_wildcards(num_pat, pat, num_file, file, flags); |
| 11136 | } |
| 11137 | #endif |
| 11138 | |
| 11139 | recursive = TRUE; |
| 11140 | |
| 11141 | /* |
| 11142 | * The matching file names are stored in a growarray. Init it empty. |
| 11143 | */ |
| 11144 | ga_init2(&ga, (int)sizeof(char_u *), 30); |
| 11145 | |
| 11146 | for (i = 0; i < num_pat; ++i) |
| 11147 | { |
| 11148 | add_pat = -1; |
| 11149 | p = pat[i]; |
| 11150 | |
| 11151 | #ifdef VIM_BACKTICK |
| 11152 | if (vim_backtick(p)) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11153 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11154 | add_pat = expand_backtick(&ga, p, flags); |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11155 | if (add_pat == -1) |
| 11156 | retval = FAIL; |
| 11157 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11158 | else |
| 11159 | #endif |
| 11160 | { |
| 11161 | /* |
| 11162 | * First expand environment variables, "~/" and "~user/". |
| 11163 | */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11164 | if (has_env_var(p) || *p == '~') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11165 | { |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 11166 | p = expand_env_save_opt(p, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11167 | if (p == NULL) |
| 11168 | p = pat[i]; |
| 11169 | #ifdef UNIX |
| 11170 | /* |
| 11171 | * On Unix, if expand_env() can't expand an environment |
| 11172 | * variable, use the shell to do that. Discard previously |
| 11173 | * found file names and start all over again. |
| 11174 | */ |
Bram Moolenaar | f4e1143 | 2013-07-03 16:53:03 +0200 | [diff] [blame] | 11175 | else if (has_env_var(p) || *p == '~') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11176 | { |
| 11177 | vim_free(p); |
Bram Moolenaar | 782027e | 2009-06-24 14:25:49 +0000 | [diff] [blame] | 11178 | ga_clear_strings(&ga); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11179 | i = mch_expand_wildcards(num_pat, pat, num_file, file, |
Bram Moolenaar | e4df164 | 2014-08-29 12:58:44 +0200 | [diff] [blame] | 11180 | flags|EW_KEEPDOLLAR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11181 | recursive = FALSE; |
| 11182 | return i; |
| 11183 | } |
| 11184 | #endif |
| 11185 | } |
| 11186 | |
| 11187 | /* |
| 11188 | * If there are wildcards: Expand file names and add each match to |
| 11189 | * the list. If there is no match, and EW_NOTFOUND is given, add |
| 11190 | * the pattern. |
| 11191 | * If there are no wildcards: Add the file name if it exists or |
| 11192 | * when EW_NOTFOUND is given. |
| 11193 | */ |
| 11194 | if (mch_has_exp_wildcard(p)) |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11195 | { |
| 11196 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11197 | if ((flags & EW_PATH) |
| 11198 | && !mch_isFullName(p) |
| 11199 | && !(p[0] == '.' |
| 11200 | && (vim_ispathsep(p[1]) |
| 11201 | || (p[1] == '.' && vim_ispathsep(p[2])))) |
| 11202 | ) |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11203 | { |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11204 | /* :find completion where 'path' is used. |
| 11205 | * Recursiveness is OK here. */ |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11206 | recursive = FALSE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11207 | add_pat = expand_in_path(&ga, p, flags); |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11208 | recursive = TRUE; |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11209 | did_expand_in_path = TRUE; |
Bram Moolenaar | 80a7dcf | 2010-08-04 17:07:20 +0200 | [diff] [blame] | 11210 | } |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 11211 | else |
| 11212 | #endif |
| 11213 | add_pat = mch_expandpath(&ga, p, flags); |
| 11214 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11215 | } |
| 11216 | |
| 11217 | if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) |
| 11218 | { |
| 11219 | char_u *t = backslash_halve_save(p); |
| 11220 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11221 | /* When EW_NOTFOUND is used, always add files and dirs. Makes |
| 11222 | * "vim c:/" work. */ |
| 11223 | if (flags & EW_NOTFOUND) |
| 11224 | addfile(&ga, t, flags | EW_DIR | EW_FILE); |
Bram Moolenaar | 00efded | 2016-07-07 14:29:10 +0200 | [diff] [blame] | 11225 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11226 | addfile(&ga, t, flags); |
| 11227 | vim_free(t); |
| 11228 | } |
| 11229 | |
Bram Moolenaar | b28ebbc | 2010-07-14 16:59:57 +0200 | [diff] [blame] | 11230 | #if defined(FEAT_SEARCHPATH) |
Bram Moolenaar | d732f9a | 2010-08-15 13:29:11 +0200 | [diff] [blame] | 11231 | if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH)) |
Bram Moolenaar | b28ebbc | 2010-07-14 16:59:57 +0200 | [diff] [blame] | 11232 | uniquefy_paths(&ga, p); |
| 11233 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11234 | if (p != pat[i]) |
| 11235 | vim_free(p); |
| 11236 | } |
| 11237 | |
| 11238 | *num_file = ga.ga_len; |
| 11239 | *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; |
| 11240 | |
| 11241 | recursive = FALSE; |
| 11242 | |
Bram Moolenaar | 336bd62 | 2016-01-17 18:23:58 +0100 | [diff] [blame] | 11243 | return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11244 | } |
| 11245 | |
| 11246 | # ifdef VIM_BACKTICK |
| 11247 | |
| 11248 | /* |
| 11249 | * Return TRUE if we can expand this backtick thing here. |
| 11250 | */ |
| 11251 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11252 | vim_backtick(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11253 | { |
| 11254 | return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); |
| 11255 | } |
| 11256 | |
| 11257 | /* |
| 11258 | * Expand an item in `backticks` by executing it as a command. |
| 11259 | * Currently only works when pat[] starts and ends with a `. |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11260 | * Returns number of file names found, -1 if an error is encountered. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11261 | */ |
| 11262 | static int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11263 | expand_backtick( |
| 11264 | garray_T *gap, |
| 11265 | char_u *pat, |
| 11266 | int flags) /* EW_* flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11267 | { |
| 11268 | char_u *p; |
| 11269 | char_u *cmd; |
| 11270 | char_u *buffer; |
| 11271 | int cnt = 0; |
| 11272 | int i; |
| 11273 | |
| 11274 | /* Create the command: lop off the backticks. */ |
| 11275 | cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); |
| 11276 | if (cmd == NULL) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11277 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11278 | |
| 11279 | #ifdef FEAT_EVAL |
| 11280 | if (*cmd == '=') /* `={expr}`: Expand expression */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 11281 | buffer = eval_to_string(cmd + 1, &p, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11282 | else |
| 11283 | #endif |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 11284 | buffer = get_cmd_output(cmd, NULL, |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11285 | (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11286 | vim_free(cmd); |
| 11287 | if (buffer == NULL) |
Bram Moolenaar | 3f18893 | 2015-08-25 13:57:04 +0200 | [diff] [blame] | 11288 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11289 | |
| 11290 | cmd = buffer; |
| 11291 | while (*cmd != NUL) |
| 11292 | { |
| 11293 | cmd = skipwhite(cmd); /* skip over white space */ |
| 11294 | p = cmd; |
| 11295 | while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */ |
| 11296 | ++p; |
| 11297 | /* add an entry if it is not empty */ |
| 11298 | if (p > cmd) |
| 11299 | { |
| 11300 | i = *p; |
| 11301 | *p = NUL; |
| 11302 | addfile(gap, cmd, flags); |
| 11303 | *p = i; |
| 11304 | ++cnt; |
| 11305 | } |
| 11306 | cmd = p; |
| 11307 | while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) |
| 11308 | ++cmd; |
| 11309 | } |
| 11310 | |
| 11311 | vim_free(buffer); |
| 11312 | return cnt; |
| 11313 | } |
| 11314 | # endif /* VIM_BACKTICK */ |
| 11315 | |
| 11316 | /* |
| 11317 | * Add a file to a file list. Accepted flags: |
| 11318 | * EW_DIR add directories |
| 11319 | * EW_FILE add files |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 11320 | * EW_EXEC add executable files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11321 | * EW_NOTFOUND add even when it doesn't exist |
| 11322 | * EW_ADDSLASH add slash after directory name |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 11323 | * EW_ALLLINKS add symlink also when the referred file does not exist |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11324 | */ |
| 11325 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11326 | addfile( |
| 11327 | garray_T *gap, |
| 11328 | char_u *f, /* filename */ |
| 11329 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11330 | { |
| 11331 | char_u *p; |
| 11332 | int isdir; |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 11333 | stat_T sb; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11334 | |
Bram Moolenaar | d8b77f7 | 2015-03-05 21:21:19 +0100 | [diff] [blame] | 11335 | /* if the file/dir/link doesn't exist, may not add it */ |
| 11336 | if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS) |
Bram Moolenaar | ab11a59 | 2015-03-06 22:00:11 +0100 | [diff] [blame] | 11337 | ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11338 | return; |
| 11339 | |
| 11340 | #ifdef FNAME_ILLEGAL |
| 11341 | /* if the file/dir contains illegal characters, don't add it */ |
| 11342 | if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) |
| 11343 | return; |
| 11344 | #endif |
| 11345 | |
| 11346 | isdir = mch_isdir(f); |
| 11347 | if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) |
| 11348 | return; |
| 11349 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 11350 | /* If the file isn't executable, may not add it. Do accept directories. |
| 11351 | * When invoked from expand_shellcmd() do not use $PATH. */ |
| 11352 | if (!isdir && (flags & EW_EXEC) |
| 11353 | && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD))) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 11354 | return; |
| 11355 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11356 | /* Make room for another item in the file list. */ |
| 11357 | if (ga_grow(gap, 1) == FAIL) |
| 11358 | return; |
| 11359 | |
| 11360 | p = alloc((unsigned)(STRLEN(f) + 1 + isdir)); |
| 11361 | if (p == NULL) |
| 11362 | return; |
| 11363 | |
| 11364 | STRCPY(p, f); |
| 11365 | #ifdef BACKSLASH_IN_FILENAME |
| 11366 | slash_adjust(p); |
| 11367 | #endif |
| 11368 | /* |
| 11369 | * Append a slash or backslash after directory names if none is present. |
| 11370 | */ |
| 11371 | #ifndef DONT_ADD_PATHSEP_TO_DIR |
| 11372 | if (isdir && (flags & EW_ADDSLASH)) |
| 11373 | add_pathsep(p); |
| 11374 | #endif |
| 11375 | ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11376 | } |
| 11377 | #endif /* !NO_EXPANDPATH */ |
| 11378 | |
| 11379 | #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO) |
| 11380 | |
| 11381 | #ifndef SEEK_SET |
| 11382 | # define SEEK_SET 0 |
| 11383 | #endif |
| 11384 | #ifndef SEEK_END |
| 11385 | # define SEEK_END 2 |
| 11386 | #endif |
| 11387 | |
| 11388 | /* |
| 11389 | * Get the stdout of an external command. |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11390 | * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not |
| 11391 | * NULL store the length there. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11392 | * Returns an allocated string, or NULL for error. |
| 11393 | */ |
| 11394 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11395 | get_cmd_output( |
| 11396 | char_u *cmd, |
| 11397 | char_u *infile, /* optional input file name */ |
| 11398 | int flags, /* can be SHELL_SILENT */ |
| 11399 | int *ret_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11400 | { |
| 11401 | char_u *tempname; |
| 11402 | char_u *command; |
| 11403 | char_u *buffer = NULL; |
| 11404 | int len; |
| 11405 | int i = 0; |
| 11406 | FILE *fd; |
| 11407 | |
| 11408 | if (check_restricted() || check_secure()) |
| 11409 | return NULL; |
| 11410 | |
| 11411 | /* get a name for the temp file */ |
Bram Moolenaar | e5c421c | 2015-03-31 13:33:08 +0200 | [diff] [blame] | 11412 | if ((tempname = vim_tempname('o', FALSE)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11413 | { |
| 11414 | EMSG(_(e_notmp)); |
| 11415 | return NULL; |
| 11416 | } |
| 11417 | |
| 11418 | /* Add the redirection stuff */ |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 11419 | command = make_filter_cmd(cmd, infile, tempname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11420 | if (command == NULL) |
| 11421 | goto done; |
| 11422 | |
| 11423 | /* |
| 11424 | * Call the shell to execute the command (errors are ignored). |
| 11425 | * Don't check timestamps here. |
| 11426 | */ |
| 11427 | ++no_check_timestamps; |
| 11428 | call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags); |
| 11429 | --no_check_timestamps; |
| 11430 | |
| 11431 | vim_free(command); |
| 11432 | |
| 11433 | /* |
| 11434 | * read the names from the file into memory |
| 11435 | */ |
| 11436 | # ifdef VMS |
Bram Moolenaar | 2539402 | 2007-05-10 19:06:20 +0000 | [diff] [blame] | 11437 | /* created temporary file is not always readable as binary */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11438 | fd = mch_fopen((char *)tempname, "r"); |
| 11439 | # else |
| 11440 | fd = mch_fopen((char *)tempname, READBIN); |
| 11441 | # endif |
| 11442 | |
| 11443 | if (fd == NULL) |
| 11444 | { |
| 11445 | EMSG2(_(e_notopen), tempname); |
| 11446 | goto done; |
| 11447 | } |
| 11448 | |
| 11449 | fseek(fd, 0L, SEEK_END); |
| 11450 | len = ftell(fd); /* get size of temp file */ |
| 11451 | fseek(fd, 0L, SEEK_SET); |
| 11452 | |
| 11453 | buffer = alloc(len + 1); |
| 11454 | if (buffer != NULL) |
| 11455 | i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd); |
| 11456 | fclose(fd); |
| 11457 | mch_remove(tempname); |
| 11458 | if (buffer == NULL) |
| 11459 | goto done; |
| 11460 | #ifdef VMS |
| 11461 | len = i; /* VMS doesn't give us what we asked for... */ |
| 11462 | #endif |
| 11463 | if (i != len) |
| 11464 | { |
| 11465 | EMSG2(_(e_notread), tempname); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 11466 | VIM_CLEAR(buffer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11467 | } |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11468 | else if (ret_len == NULL) |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 11469 | { |
| 11470 | /* Change NUL into SOH, otherwise the string is truncated. */ |
| 11471 | for (i = 0; i < len; ++i) |
Bram Moolenaar | f40f4ab | 2013-08-03 17:31:28 +0200 | [diff] [blame] | 11472 | if (buffer[i] == NUL) |
| 11473 | buffer[i] = 1; |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 11474 | |
Bram Moolenaar | 162bd91 | 2010-07-28 22:29:10 +0200 | [diff] [blame] | 11475 | buffer[len] = NUL; /* make sure the buffer is terminated */ |
Bram Moolenaar | fb332a2 | 2013-08-03 14:10:50 +0200 | [diff] [blame] | 11476 | } |
Bram Moolenaar | 39c29ed | 2014-04-05 19:44:40 +0200 | [diff] [blame] | 11477 | else |
| 11478 | *ret_len = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11479 | |
| 11480 | done: |
| 11481 | vim_free(tempname); |
| 11482 | return buffer; |
| 11483 | } |
| 11484 | #endif |
| 11485 | |
| 11486 | /* |
| 11487 | * Free the list of files returned by expand_wildcards() or other expansion |
| 11488 | * functions. |
| 11489 | */ |
| 11490 | void |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11491 | FreeWild(int count, char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11492 | { |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 11493 | if (count <= 0 || files == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11494 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11495 | while (count--) |
| 11496 | vim_free(files[count]); |
| 11497 | vim_free(files); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11498 | } |
| 11499 | |
| 11500 | /* |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 11501 | * Return TRUE when need to go to Insert mode because of 'insertmode'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11502 | * Don't do this when still processing a command or a mapping. |
| 11503 | * Don't do this when inside a ":normal" command. |
| 11504 | */ |
| 11505 | int |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11506 | goto_im(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11507 | { |
| 11508 | return (p_im && stuff_empty() && typebuf_typed()); |
| 11509 | } |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11510 | |
| 11511 | /* |
Bram Moolenaar | 050fe7e | 2014-05-22 14:00:16 +0200 | [diff] [blame] | 11512 | * Returns the isolated name of the shell in allocated memory: |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11513 | * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f". |
| 11514 | * - Remove any argument. E.g., "csh -f" -> "csh". |
| 11515 | * But don't allow a space in the path, so that this works: |
| 11516 | * "/usr/bin/csh --rcfile ~/.cshrc" |
| 11517 | * But don't do that for Windows, it's common to have a space in the path. |
| 11518 | */ |
| 11519 | char_u * |
Bram Moolenaar | 9b57814 | 2016-01-30 19:39:49 +0100 | [diff] [blame] | 11520 | get_isolated_shell_name(void) |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11521 | { |
| 11522 | char_u *p; |
| 11523 | |
| 11524 | #ifdef WIN3264 |
| 11525 | p = gettail(p_sh); |
| 11526 | p = vim_strnsave(p, (int)(skiptowhite(p) - p)); |
| 11527 | #else |
| 11528 | p = skiptowhite(p_sh); |
| 11529 | if (*p == NUL) |
| 11530 | { |
| 11531 | /* No white space, use the tail. */ |
| 11532 | p = vim_strsave(gettail(p_sh)); |
| 11533 | } |
| 11534 | else |
| 11535 | { |
| 11536 | char_u *p1, *p2; |
| 11537 | |
| 11538 | /* Find the last path separator before the space. */ |
| 11539 | p1 = p_sh; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 11540 | for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2)) |
Bram Moolenaar | 75a8d74 | 2014-05-07 15:10:21 +0200 | [diff] [blame] | 11541 | if (vim_ispathsep(*p2)) |
| 11542 | p1 = p2 + 1; |
| 11543 | p = vim_strnsave(p1, (int)(p - p1)); |
| 11544 | } |
| 11545 | #endif |
| 11546 | return p; |
| 11547 | } |