Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 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 | * quickfix.c: functions for quickfix mode, using a file with error messages |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | #if defined(FEAT_QUICKFIX) || defined(PROTO) |
| 17 | |
| 18 | struct dir_stack_T |
| 19 | { |
| 20 | struct dir_stack_T *next; |
| 21 | char_u *dirname; |
| 22 | }; |
| 23 | |
| 24 | static struct dir_stack_T *dir_stack = NULL; |
| 25 | |
| 26 | /* |
| 27 | * for each error the next struct is allocated and linked in a list |
| 28 | */ |
| 29 | struct qf_line |
| 30 | { |
| 31 | struct qf_line *qf_next; /* pointer to next error in the list */ |
| 32 | struct qf_line *qf_prev; /* pointer to previous error in the list */ |
| 33 | linenr_T qf_lnum; /* line number where the error occurred */ |
| 34 | int qf_fnum; /* file number for the line */ |
| 35 | int qf_col; /* column where the error occurred */ |
| 36 | int qf_nr; /* error number */ |
| 37 | char_u *qf_text; /* description of the error */ |
| 38 | char_u qf_virt_col; /* set to TRUE if qf_col is screen column */ |
| 39 | char_u qf_cleared;/* set to TRUE if line has been deleted */ |
| 40 | char_u qf_type; /* type of the error (mostly 'E'); 1 for |
| 41 | :helpgrep */ |
| 42 | char_u qf_valid; /* valid error message detected */ |
| 43 | }; |
| 44 | |
| 45 | /* |
| 46 | * There is a stack of error lists. |
| 47 | */ |
| 48 | #define LISTCOUNT 10 |
| 49 | |
| 50 | struct qf_list |
| 51 | { |
| 52 | struct qf_line *qf_start; /* pointer to the first error */ |
| 53 | struct qf_line *qf_ptr; /* pointer to the current error */ |
| 54 | int qf_count; /* number of errors (0 means no error list) */ |
| 55 | int qf_index; /* current index in the error list */ |
| 56 | int qf_nonevalid; /* TRUE if not a single valid entry found */ |
| 57 | } qf_lists[LISTCOUNT]; |
| 58 | |
| 59 | static int qf_curlist = 0; /* current error list */ |
| 60 | static int qf_listcount = 0; /* current number of lists */ |
| 61 | |
| 62 | #define FMT_PATTERNS 9 /* maximum number of % recognized */ |
| 63 | |
| 64 | /* |
| 65 | * Structure used to hold the info of one part of 'errorformat' |
| 66 | */ |
| 67 | struct eformat |
| 68 | { |
| 69 | regprog_T *prog; /* pre-formatted part of 'errorformat' */ |
| 70 | struct eformat *next; /* pointer to next (NULL if last) */ |
| 71 | char_u addr[FMT_PATTERNS]; /* indices of used % patterns */ |
| 72 | char_u prefix; /* prefix of this format line: */ |
| 73 | /* 'D' enter directory */ |
| 74 | /* 'X' leave directory */ |
| 75 | /* 'A' start of multi-line message */ |
| 76 | /* 'E' error message */ |
| 77 | /* 'W' warning message */ |
| 78 | /* 'I' informational message */ |
| 79 | /* 'C' continuation line */ |
| 80 | /* 'Z' end of multi-line message */ |
| 81 | /* 'G' general, unspecific message */ |
| 82 | /* 'P' push file (partial) message */ |
| 83 | /* 'Q' pop/quit file (partial) message */ |
| 84 | /* 'O' overread (partial) message */ |
| 85 | char_u flags; /* additional flags given in prefix */ |
| 86 | /* '-' do not include this line */ |
| 87 | }; |
| 88 | |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 89 | static int qf_init_ext __ARGS((char_u *efile, buf_T *buf, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 90 | static void qf_new_list __ARGS((void)); |
| 91 | static int qf_add_entry __ARGS((struct qf_line **prevp, char_u *dir, char_u *fname, char_u *mesg, long lnum, int col, int virt_col, int nr, int type, int valid)); |
| 92 | static void qf_msg __ARGS((void)); |
| 93 | static void qf_free __ARGS((int idx)); |
| 94 | static char_u *qf_types __ARGS((int, int)); |
| 95 | static int qf_get_fnum __ARGS((char_u *, char_u *)); |
| 96 | static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **)); |
| 97 | static char_u *qf_pop_dir __ARGS((struct dir_stack_T **)); |
| 98 | static char_u *qf_guess_filepath __ARGS((char_u *)); |
| 99 | static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize)); |
| 100 | static void qf_clean_dir_stack __ARGS((struct dir_stack_T **)); |
| 101 | #ifdef FEAT_WINDOWS |
| 102 | static int qf_win_pos_update __ARGS((int old_qf_index)); |
| 103 | static buf_T *qf_find_buf __ARGS((void)); |
| 104 | static void qf_update_buffer __ARGS((void)); |
| 105 | static void qf_fill_buffer __ARGS((void)); |
| 106 | #endif |
| 107 | static char_u *get_mef_name __ARGS((void)); |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 108 | static buf_T *load_dummy_buffer __ARGS((char_u *fname)); |
| 109 | static void wipe_dummy_buffer __ARGS((buf_T *buf)); |
| 110 | static void unload_dummy_buffer __ARGS((buf_T *buf)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 111 | |
| 112 | /* |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 113 | * Read the errorfile "efile" into memory, line by line, building the error |
| 114 | * list. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 115 | * Return -1 for error, number of errors for success. |
| 116 | */ |
| 117 | int |
| 118 | qf_init(efile, errorformat, newlist) |
| 119 | char_u *efile; |
| 120 | char_u *errorformat; |
| 121 | int newlist; /* TRUE: start a new error list */ |
| 122 | { |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 123 | if (efile == NULL) |
| 124 | return FAIL; |
| 125 | return qf_init_ext(efile, curbuf, errorformat, newlist, |
| 126 | (linenr_T)0, (linenr_T)0); |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Read the errorfile "efile" into memory, line by line, building the error |
| 131 | * list. |
| 132 | * Alternative: when "efile" is null read errors from buffer "buf". |
| 133 | * Always use 'errorformat' from "buf" if there is a local value. |
| 134 | * Then lnumfirst and lnumlast specify the range of lines to use. |
| 135 | * Return -1 for error, number of errors for success. |
| 136 | */ |
| 137 | static int |
| 138 | qf_init_ext(efile, buf, errorformat, newlist, lnumfirst, lnumlast) |
| 139 | char_u *efile; |
| 140 | buf_T *buf; |
| 141 | char_u *errorformat; |
| 142 | int newlist; /* TRUE: start a new error list */ |
| 143 | linenr_T lnumfirst; /* first line number to use */ |
| 144 | linenr_T lnumlast; /* last line number to use */ |
| 145 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 146 | char_u *namebuf; |
| 147 | char_u *errmsg; |
| 148 | char_u *fmtstr = NULL; |
| 149 | int col = 0; |
| 150 | char_u use_virt_col = FALSE; |
| 151 | int type = 0; |
| 152 | int valid; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 153 | linenr_T buflnum = lnumfirst; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 154 | long lnum = 0L; |
| 155 | int enr = 0; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 156 | FILE *fd = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 157 | struct qf_line *qfprev = NULL; /* init to make SASC shut up */ |
| 158 | char_u *efmp; |
| 159 | struct eformat *fmt_first = NULL; |
| 160 | struct eformat *fmt_last = NULL; |
| 161 | struct eformat *fmt_ptr; |
| 162 | char_u *efm; |
| 163 | char_u *ptr; |
| 164 | char_u *srcptr; |
| 165 | int len; |
| 166 | int i; |
| 167 | int round; |
| 168 | int idx = 0; |
| 169 | int multiline = FALSE; |
| 170 | int multiignore = FALSE; |
| 171 | int multiscan = FALSE; |
| 172 | int retval = -1; /* default: return error flag */ |
| 173 | char_u *directory = NULL; |
| 174 | char_u *currfile = NULL; |
| 175 | char_u *tail = NULL; |
| 176 | struct dir_stack_T *file_stack = NULL; |
| 177 | regmatch_T regmatch; |
| 178 | static struct fmtpattern |
| 179 | { |
| 180 | char_u convchar; |
| 181 | char *pattern; |
| 182 | } fmt_pat[FMT_PATTERNS] = |
| 183 | { |
| 184 | {'f', "\\f\\+"}, |
| 185 | {'n', "\\d\\+"}, |
| 186 | {'l', "\\d\\+"}, |
| 187 | {'c', "\\d\\+"}, |
| 188 | {'t', "."}, |
| 189 | {'m', ".\\+"}, |
| 190 | {'r', ".*"}, |
| 191 | {'p', "[- .]*"}, |
| 192 | {'v', "\\d\\+"} |
| 193 | }; |
| 194 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 195 | namebuf = alloc(CMDBUFFSIZE + 1); |
| 196 | errmsg = alloc(CMDBUFFSIZE + 1); |
| 197 | if (namebuf == NULL || errmsg == NULL) |
| 198 | goto qf_init_end; |
| 199 | |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 200 | if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 201 | { |
| 202 | EMSG2(_(e_openerrf), efile); |
| 203 | goto qf_init_end; |
| 204 | } |
| 205 | |
| 206 | if (newlist || qf_curlist == qf_listcount) |
| 207 | /* make place for a new list */ |
| 208 | qf_new_list(); |
| 209 | else if (qf_lists[qf_curlist].qf_count > 0) |
| 210 | /* Adding to existing list, find last entry. */ |
| 211 | for (qfprev = qf_lists[qf_curlist].qf_start; |
| 212 | qfprev->qf_next != qfprev; qfprev = qfprev->qf_next) |
| 213 | ; |
| 214 | |
| 215 | /* |
| 216 | * Each part of the format string is copied and modified from errorformat to |
| 217 | * regex prog. Only a few % characters are allowed. |
| 218 | */ |
| 219 | /* Use the local value of 'errorformat' if it's set. */ |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 220 | if (errorformat == p_efm && *buf->b_p_efm != NUL) |
| 221 | efm = buf->b_p_efm; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 222 | else |
| 223 | efm = errorformat; |
| 224 | /* |
| 225 | * Get some space to modify the format string into. |
| 226 | */ |
| 227 | i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2); |
| 228 | for (round = FMT_PATTERNS; round > 0; ) |
| 229 | i += (int)STRLEN(fmt_pat[--round].pattern); |
| 230 | #ifdef COLON_IN_FILENAME |
| 231 | i += 12; /* "%f" can become twelve chars longer */ |
| 232 | #else |
| 233 | i += 2; /* "%f" can become two chars longer */ |
| 234 | #endif |
| 235 | if ((fmtstr = alloc(i)) == NULL) |
| 236 | goto error2; |
| 237 | |
| 238 | while (efm[0]) |
| 239 | { |
| 240 | /* |
| 241 | * Allocate a new eformat structure and put it at the end of the list |
| 242 | */ |
| 243 | fmt_ptr = (struct eformat *)alloc((unsigned)sizeof(struct eformat)); |
| 244 | if (fmt_ptr == NULL) |
| 245 | goto error2; |
| 246 | if (fmt_first == NULL) /* first one */ |
| 247 | fmt_first = fmt_ptr; |
| 248 | else |
| 249 | fmt_last->next = fmt_ptr; |
| 250 | fmt_last = fmt_ptr; |
| 251 | fmt_ptr->prefix = NUL; |
| 252 | fmt_ptr->flags = NUL; |
| 253 | fmt_ptr->next = NULL; |
| 254 | fmt_ptr->prog = NULL; |
| 255 | for (round = FMT_PATTERNS; round > 0; ) |
| 256 | fmt_ptr->addr[--round] = NUL; |
| 257 | /* round is 0 now */ |
| 258 | |
| 259 | /* |
| 260 | * Isolate one part in the 'errorformat' option |
| 261 | */ |
| 262 | for (len = 0; efm[len] != NUL && efm[len] != ','; ++len) |
| 263 | if (efm[len] == '\\' && efm[len + 1] != NUL) |
| 264 | ++len; |
| 265 | |
| 266 | /* |
| 267 | * Build regexp pattern from current 'errorformat' option |
| 268 | */ |
| 269 | ptr = fmtstr; |
| 270 | *ptr++ = '^'; |
| 271 | for (efmp = efm; efmp < efm + len; ++efmp) |
| 272 | { |
| 273 | if (*efmp == '%') |
| 274 | { |
| 275 | ++efmp; |
| 276 | for (idx = 0; idx < FMT_PATTERNS; ++idx) |
| 277 | if (fmt_pat[idx].convchar == *efmp) |
| 278 | break; |
| 279 | if (idx < FMT_PATTERNS) |
| 280 | { |
| 281 | if (fmt_ptr->addr[idx]) |
| 282 | { |
| 283 | sprintf((char *)errmsg, |
| 284 | _("E372: Too many %%%c in format string"), *efmp); |
| 285 | EMSG(errmsg); |
| 286 | goto error2; |
| 287 | } |
| 288 | if ((idx |
| 289 | && idx < 6 |
| 290 | && vim_strchr((char_u *)"DXOPQ", |
| 291 | fmt_ptr->prefix) != NULL) |
| 292 | || (idx == 6 |
| 293 | && vim_strchr((char_u *)"OPQ", |
| 294 | fmt_ptr->prefix) == NULL)) |
| 295 | { |
| 296 | sprintf((char *)errmsg, |
| 297 | _("E373: Unexpected %%%c in format string"), *efmp); |
| 298 | EMSG(errmsg); |
| 299 | goto error2; |
| 300 | } |
| 301 | fmt_ptr->addr[idx] = (char_u)++round; |
| 302 | *ptr++ = '\\'; |
| 303 | *ptr++ = '('; |
| 304 | #ifdef BACKSLASH_IN_FILENAME |
| 305 | if (*efmp == 'f') |
| 306 | { |
| 307 | /* Also match "c:" in the file name, even when |
| 308 | * checking for a colon next: "%f:". |
| 309 | * "\%(\a:\)\=" */ |
| 310 | STRCPY(ptr, "\\%(\\a:\\)\\="); |
| 311 | ptr += 10; |
| 312 | } |
| 313 | #endif |
| 314 | if (*efmp == 'f' && efmp[1] != NUL |
| 315 | && efmp[1] != '\\' && efmp[1] != '%') |
| 316 | { |
| 317 | /* A file name may contain spaces, but this isn't in |
| 318 | * "\f". use "[^x]\+" instead (x is next character) */ |
| 319 | *ptr++ = '['; |
| 320 | *ptr++ = '^'; |
| 321 | *ptr++ = efmp[1]; |
| 322 | *ptr++ = ']'; |
| 323 | *ptr++ = '\\'; |
| 324 | *ptr++ = '+'; |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | srcptr = (char_u *)fmt_pat[idx].pattern; |
| 329 | while ((*ptr = *srcptr++) != NUL) |
| 330 | ++ptr; |
| 331 | } |
| 332 | *ptr++ = '\\'; |
| 333 | *ptr++ = ')'; |
| 334 | } |
| 335 | else if (*efmp == '*') |
| 336 | { |
| 337 | if (*++efmp == '[' || *efmp == '\\') |
| 338 | { |
| 339 | if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */ |
| 340 | { |
| 341 | if (efmp[1] == '^') |
| 342 | *ptr++ = *++efmp; |
| 343 | if (efmp < efm + len) |
| 344 | { |
| 345 | *ptr++ = *++efmp; /* could be ']' */ |
| 346 | while (efmp < efm + len |
| 347 | && (*ptr++ = *++efmp) != ']') |
| 348 | /* skip */; |
| 349 | if (efmp == efm + len) |
| 350 | { |
| 351 | EMSG(_("E374: Missing ] in format string")); |
| 352 | goto error2; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | else if (efmp < efm + len) /* %*\D, %*\s etc. */ |
| 357 | *ptr++ = *++efmp; |
| 358 | *ptr++ = '\\'; |
| 359 | *ptr++ = '+'; |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */ |
| 364 | sprintf((char *)errmsg, |
| 365 | _("E375: Unsupported %%%c in format string"), *efmp); |
| 366 | EMSG(errmsg); |
| 367 | goto error2; |
| 368 | } |
| 369 | } |
| 370 | else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL) |
| 371 | *ptr++ = *efmp; /* regexp magic characters */ |
| 372 | else if (*efmp == '#') |
| 373 | *ptr++ = '*'; |
| 374 | else if (efmp == efm + 1) /* analyse prefix */ |
| 375 | { |
| 376 | if (vim_strchr((char_u *)"+-", *efmp) != NULL) |
| 377 | fmt_ptr->flags = *efmp++; |
| 378 | if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL) |
| 379 | fmt_ptr->prefix = *efmp; |
| 380 | else |
| 381 | { |
| 382 | sprintf((char *)errmsg, |
| 383 | _("E376: Invalid %%%c in format string prefix"), *efmp); |
| 384 | EMSG(errmsg); |
| 385 | goto error2; |
| 386 | } |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | sprintf((char *)errmsg, |
| 391 | _("E377: Invalid %%%c in format string"), *efmp); |
| 392 | EMSG(errmsg); |
| 393 | goto error2; |
| 394 | } |
| 395 | } |
| 396 | else /* copy normal character */ |
| 397 | { |
| 398 | if (*efmp == '\\' && efmp + 1 < efm + len) |
| 399 | ++efmp; |
| 400 | else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL) |
| 401 | *ptr++ = '\\'; /* escape regexp atoms */ |
| 402 | if (*efmp) |
| 403 | *ptr++ = *efmp; |
| 404 | } |
| 405 | } |
| 406 | *ptr++ = '$'; |
| 407 | *ptr = NUL; |
| 408 | if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL) |
| 409 | goto error2; |
| 410 | /* |
| 411 | * Advance to next part |
| 412 | */ |
| 413 | efm = skip_to_option_part(efm + len); /* skip comma and spaces */ |
| 414 | } |
| 415 | if (fmt_first == NULL) /* nothing found */ |
| 416 | { |
| 417 | EMSG(_("E378: 'errorformat' contains no pattern")); |
| 418 | goto error2; |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * got_int is reset here, because it was probably set when killing the |
| 423 | * ":make" command, but we still want to read the errorfile then. |
| 424 | */ |
| 425 | got_int = FALSE; |
| 426 | |
| 427 | /* Always ignore case when looking for a matching error. */ |
| 428 | regmatch.rm_ic = TRUE; |
| 429 | |
| 430 | /* |
| 431 | * Read the lines in the error file one by one. |
| 432 | * Try to recognize one of the error formats in each line. |
| 433 | */ |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 434 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 435 | { |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 436 | /* Get the next line. */ |
| 437 | if (fd == NULL) |
| 438 | { |
| 439 | if (buflnum > lnumlast) |
| 440 | break; |
| 441 | STRNCPY(IObuff, ml_get_buf(buf, buflnum++, FALSE), CMDBUFFSIZE - 2); |
| 442 | } |
| 443 | else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL) |
| 444 | break; |
| 445 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 446 | IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */ |
| 447 | if ((efmp = vim_strrchr(IObuff, '\n')) != NULL) |
| 448 | *efmp = NUL; |
| 449 | #ifdef USE_CRNL |
| 450 | if ((efmp = vim_strrchr(IObuff, '\r')) != NULL) |
| 451 | *efmp = NUL; |
| 452 | #endif |
| 453 | |
| 454 | /* |
| 455 | * Try to match each part of 'errorformat' until we find a complete |
| 456 | * match or no match. |
| 457 | */ |
| 458 | valid = TRUE; |
| 459 | restofline: |
| 460 | for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next) |
| 461 | { |
| 462 | idx = fmt_ptr->prefix; |
| 463 | if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL) |
| 464 | continue; |
| 465 | namebuf[0] = NUL; |
| 466 | if (!multiscan) |
| 467 | errmsg[0] = NUL; |
| 468 | lnum = 0; |
| 469 | col = 0; |
| 470 | use_virt_col = FALSE; |
| 471 | enr = -1; |
| 472 | type = 0; |
| 473 | tail = NULL; |
| 474 | |
| 475 | regmatch.regprog = fmt_ptr->prog; |
| 476 | if (vim_regexec(®match, IObuff, (colnr_T)0)) |
| 477 | { |
| 478 | if ((idx == 'C' || idx == 'Z') && !multiline) |
| 479 | continue; |
| 480 | if (vim_strchr((char_u *)"EWI", idx) != NULL) |
| 481 | type = idx; |
| 482 | else |
| 483 | type = 0; |
| 484 | /* |
| 485 | * Extract error message data from matched line |
| 486 | */ |
| 487 | if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */ |
| 488 | { |
| 489 | len = (int)(regmatch.endp[i] - regmatch.startp[i]); |
| 490 | STRNCPY(namebuf, regmatch.startp[i], len); |
| 491 | namebuf[len] = NUL; |
| 492 | if (vim_strchr((char_u *)"OPQ", idx) != NULL |
| 493 | && mch_getperm(namebuf) == -1) |
| 494 | continue; |
| 495 | } |
| 496 | if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */ |
| 497 | enr = (int)atol((char *)regmatch.startp[i]); |
| 498 | if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */ |
| 499 | lnum = atol((char *)regmatch.startp[i]); |
| 500 | if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */ |
| 501 | col = (int)atol((char *)regmatch.startp[i]); |
| 502 | if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */ |
| 503 | type = *regmatch.startp[i]; |
| 504 | if (fmt_ptr->flags == '+' && !multiscan) /* %+ */ |
| 505 | STRCPY(errmsg, IObuff); |
| 506 | else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */ |
| 507 | { |
| 508 | len = (int)(regmatch.endp[i] - regmatch.startp[i]); |
| 509 | STRNCPY(errmsg, regmatch.startp[i], len); |
| 510 | errmsg[len] = NUL; |
| 511 | } |
| 512 | if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */ |
| 513 | tail = regmatch.startp[i]; |
| 514 | if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */ |
| 515 | { |
| 516 | col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1); |
| 517 | if (*((char_u *)regmatch.startp[i]) != TAB) |
| 518 | use_virt_col = TRUE; |
| 519 | } |
| 520 | if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */ |
| 521 | { |
| 522 | col = (int)atol((char *)regmatch.startp[i]); |
| 523 | use_virt_col = TRUE; |
| 524 | } |
| 525 | break; |
| 526 | } |
| 527 | } |
| 528 | multiscan = FALSE; |
| 529 | if (!fmt_ptr || idx == 'D' || idx == 'X') |
| 530 | { |
| 531 | if (fmt_ptr) |
| 532 | { |
| 533 | if (idx == 'D') /* enter directory */ |
| 534 | { |
| 535 | if (*namebuf == NUL) |
| 536 | { |
| 537 | EMSG(_("E379: Missing or empty directory name")); |
| 538 | goto error2; |
| 539 | } |
| 540 | if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL) |
| 541 | goto error2; |
| 542 | } |
| 543 | else if (idx == 'X') /* leave directory */ |
| 544 | directory = qf_pop_dir(&dir_stack); |
| 545 | } |
| 546 | namebuf[0] = NUL; /* no match found, remove file name */ |
| 547 | lnum = 0; /* don't jump to this line */ |
| 548 | valid = FALSE; |
| 549 | STRCPY(errmsg, IObuff); /* copy whole line to error message */ |
| 550 | if (!fmt_ptr) |
| 551 | multiline = multiignore = FALSE; |
| 552 | } |
| 553 | else if (fmt_ptr) |
| 554 | { |
| 555 | if (vim_strchr((char_u *)"AEWI", idx) != NULL) |
| 556 | multiline = TRUE; /* start of a multi-line message */ |
| 557 | else if (vim_strchr((char_u *)"CZ", idx) != NULL) |
| 558 | { /* continuation of multi-line msg */ |
| 559 | if (qfprev == NULL) |
| 560 | goto error2; |
| 561 | if (*errmsg && !multiignore) |
| 562 | { |
| 563 | len = (int)STRLEN(qfprev->qf_text); |
| 564 | if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2))) |
| 565 | == NULL) |
| 566 | goto error2; |
| 567 | STRCPY(ptr, qfprev->qf_text); |
| 568 | vim_free(qfprev->qf_text); |
| 569 | qfprev->qf_text = ptr; |
| 570 | *(ptr += len) = '\n'; |
| 571 | STRCPY(++ptr, errmsg); |
| 572 | } |
| 573 | if (qfprev->qf_nr == -1) |
| 574 | qfprev->qf_nr = enr; |
| 575 | if (vim_isprintc(type) && !qfprev->qf_type) |
| 576 | qfprev->qf_type = type; /* only printable chars allowed */ |
| 577 | if (!qfprev->qf_lnum) |
| 578 | qfprev->qf_lnum = lnum; |
| 579 | if (!qfprev->qf_col) |
| 580 | qfprev->qf_col = col; |
| 581 | qfprev->qf_virt_col = use_virt_col; |
| 582 | if (!qfprev->qf_fnum) |
| 583 | qfprev->qf_fnum = qf_get_fnum(directory, |
| 584 | *namebuf || directory ? namebuf |
| 585 | : currfile && valid ? currfile : 0); |
| 586 | if (idx == 'Z') |
| 587 | multiline = multiignore = FALSE; |
| 588 | line_breakcheck(); |
| 589 | continue; |
| 590 | } |
| 591 | else if (vim_strchr((char_u *)"OPQ", idx) != NULL) |
| 592 | { |
| 593 | /* global file names */ |
| 594 | valid = FALSE; |
| 595 | if (*namebuf == NUL || mch_getperm(namebuf) >= 0) |
| 596 | { |
| 597 | if (*namebuf && idx == 'P') |
| 598 | currfile = qf_push_dir(namebuf, &file_stack); |
| 599 | else if (idx == 'Q') |
| 600 | currfile = qf_pop_dir(&file_stack); |
| 601 | *namebuf = NUL; |
| 602 | if (tail && *tail) |
| 603 | { |
| 604 | STRCPY(IObuff, skipwhite(tail)); |
| 605 | multiscan = TRUE; |
| 606 | goto restofline; |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | if (fmt_ptr->flags == '-') /* generally exclude this line */ |
| 611 | { |
| 612 | if (multiline) |
| 613 | multiignore = TRUE; /* also exclude continuation lines */ |
| 614 | continue; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | if (qf_add_entry(&qfprev, |
| 619 | directory, |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 620 | (*namebuf || directory) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 621 | ? namebuf |
Bram Moolenaar | 9d75c83 | 2005-01-25 21:57:23 +0000 | [diff] [blame] | 622 | : ((currfile && valid) ? currfile : (char_u *)NULL), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 623 | errmsg, |
| 624 | lnum, |
| 625 | col, |
| 626 | use_virt_col, |
| 627 | enr, |
| 628 | type, |
| 629 | valid) == FAIL) |
| 630 | goto error2; |
| 631 | line_breakcheck(); |
| 632 | } |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 633 | if (fd == NULL || !ferror(fd)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 634 | { |
| 635 | if (qf_lists[qf_curlist].qf_index == 0) /* no valid entry found */ |
| 636 | { |
| 637 | qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start; |
| 638 | qf_lists[qf_curlist].qf_index = 1; |
| 639 | qf_lists[qf_curlist].qf_nonevalid = TRUE; |
| 640 | } |
| 641 | else |
| 642 | { |
| 643 | qf_lists[qf_curlist].qf_nonevalid = FALSE; |
| 644 | if (qf_lists[qf_curlist].qf_ptr == NULL) |
| 645 | qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start; |
| 646 | } |
| 647 | retval = qf_lists[qf_curlist].qf_count; /* return number of matches */ |
| 648 | goto qf_init_ok; |
| 649 | } |
| 650 | EMSG(_(e_readerrf)); |
| 651 | error2: |
| 652 | qf_free(qf_curlist); |
| 653 | qf_listcount--; |
| 654 | if (qf_curlist > 0) |
| 655 | --qf_curlist; |
| 656 | qf_init_ok: |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 657 | if (fd != NULL) |
| 658 | fclose(fd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 659 | for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first) |
| 660 | { |
| 661 | fmt_first = fmt_ptr->next; |
| 662 | vim_free(fmt_ptr->prog); |
| 663 | vim_free(fmt_ptr); |
| 664 | } |
| 665 | qf_clean_dir_stack(&dir_stack); |
| 666 | qf_clean_dir_stack(&file_stack); |
| 667 | qf_init_end: |
| 668 | vim_free(namebuf); |
| 669 | vim_free(errmsg); |
| 670 | vim_free(fmtstr); |
| 671 | |
| 672 | #ifdef FEAT_WINDOWS |
| 673 | qf_update_buffer(); |
| 674 | #endif |
| 675 | |
| 676 | return retval; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Prepare for adding a new quickfix list. |
| 681 | */ |
| 682 | static void |
| 683 | qf_new_list() |
| 684 | { |
| 685 | int i; |
| 686 | |
| 687 | /* |
| 688 | * If the current entry is not the last entry, delete entries below |
| 689 | * the current entry. This makes it possible to browse in a tree-like |
| 690 | * way with ":grep'. |
| 691 | */ |
| 692 | while (qf_listcount > qf_curlist + 1) |
| 693 | qf_free(--qf_listcount); |
| 694 | |
| 695 | /* |
| 696 | * When the stack is full, remove to oldest entry |
| 697 | * Otherwise, add a new entry. |
| 698 | */ |
| 699 | if (qf_listcount == LISTCOUNT) |
| 700 | { |
| 701 | qf_free(0); |
| 702 | for (i = 1; i < LISTCOUNT; ++i) |
| 703 | qf_lists[i - 1] = qf_lists[i]; |
| 704 | qf_curlist = LISTCOUNT - 1; |
| 705 | } |
| 706 | else |
| 707 | qf_curlist = qf_listcount++; |
| 708 | qf_lists[qf_curlist].qf_index = 0; |
| 709 | qf_lists[qf_curlist].qf_count = 0; |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * Add an entry to the end of the list of errors. |
| 714 | * Returns OK or FAIL. |
| 715 | */ |
| 716 | static int |
| 717 | qf_add_entry(prevp, dir, fname, mesg, lnum, col, virt_col, nr, type, valid) |
| 718 | struct qf_line **prevp; /* pointer to previously added entry or NULL */ |
| 719 | char_u *dir; /* optional directory name */ |
| 720 | char_u *fname; /* file name or NULL */ |
| 721 | char_u *mesg; /* message */ |
| 722 | long lnum; /* line number */ |
| 723 | int col; /* column */ |
| 724 | int virt_col; /* using virtual column */ |
| 725 | int nr; /* error number */ |
| 726 | int type; /* type character */ |
| 727 | int valid; /* valid entry */ |
| 728 | { |
| 729 | struct qf_line *qfp; |
| 730 | |
| 731 | if ((qfp = (struct qf_line *)alloc((unsigned)sizeof(struct qf_line))) |
| 732 | == NULL) |
| 733 | return FAIL; |
| 734 | qfp->qf_fnum = qf_get_fnum(dir, fname); |
| 735 | if ((qfp->qf_text = vim_strsave(mesg)) == NULL) |
| 736 | { |
| 737 | vim_free(qfp); |
| 738 | return FAIL; |
| 739 | } |
| 740 | qfp->qf_lnum = lnum; |
| 741 | qfp->qf_col = col; |
| 742 | qfp->qf_virt_col = virt_col; |
| 743 | qfp->qf_nr = nr; |
| 744 | if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */ |
| 745 | type = 0; |
| 746 | qfp->qf_type = type; |
| 747 | qfp->qf_valid = valid; |
| 748 | |
| 749 | if (qf_lists[qf_curlist].qf_count == 0) /* first element in the list */ |
| 750 | { |
| 751 | qf_lists[qf_curlist].qf_start = qfp; |
| 752 | qfp->qf_prev = qfp; /* first element points to itself */ |
| 753 | } |
| 754 | else |
| 755 | { |
| 756 | qfp->qf_prev = *prevp; |
| 757 | (*prevp)->qf_next = qfp; |
| 758 | } |
| 759 | qfp->qf_next = qfp; /* last element points to itself */ |
| 760 | qfp->qf_cleared = FALSE; |
| 761 | *prevp = qfp; |
| 762 | ++qf_lists[qf_curlist].qf_count; |
| 763 | if (qf_lists[qf_curlist].qf_index == 0 && qfp->qf_valid) |
| 764 | /* first valid entry */ |
| 765 | { |
| 766 | qf_lists[qf_curlist].qf_index = qf_lists[qf_curlist].qf_count; |
| 767 | qf_lists[qf_curlist].qf_ptr = qfp; |
| 768 | } |
| 769 | |
| 770 | return OK; |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * get buffer number for file "dir.name" |
| 775 | */ |
| 776 | static int |
| 777 | qf_get_fnum(directory, fname) |
| 778 | char_u *directory; |
| 779 | char_u *fname; |
| 780 | { |
| 781 | if (fname == NULL || *fname == NUL) /* no file name */ |
| 782 | return 0; |
| 783 | { |
| 784 | #ifdef RISCOS |
| 785 | /* Name is reported as `main.c', but file is `c.main' */ |
| 786 | return ro_buflist_add(fname); |
| 787 | #else |
| 788 | char_u *ptr; |
| 789 | int fnum; |
| 790 | |
| 791 | # ifdef VMS |
| 792 | vms_remove_version(fname); |
| 793 | # endif |
| 794 | # ifdef BACKSLASH_IN_FILENAME |
| 795 | if (directory != NULL) |
| 796 | slash_adjust(directory); |
| 797 | slash_adjust(fname); |
| 798 | # endif |
| 799 | if (directory != NULL && !vim_isAbsName(fname) |
| 800 | && (ptr = concat_fnames(directory, fname, TRUE)) != NULL) |
| 801 | { |
| 802 | /* |
| 803 | * Here we check if the file really exists. |
| 804 | * This should normally be true, but if make works without |
| 805 | * "leaving directory"-messages we might have missed a |
| 806 | * directory change. |
| 807 | */ |
| 808 | if (mch_getperm(ptr) < 0) |
| 809 | { |
| 810 | vim_free(ptr); |
| 811 | directory = qf_guess_filepath(fname); |
| 812 | if (directory) |
| 813 | ptr = concat_fnames(directory, fname, TRUE); |
| 814 | else |
| 815 | ptr = vim_strsave(fname); |
| 816 | } |
| 817 | /* Use concatenated directory name and file name */ |
| 818 | fnum = buflist_add(ptr, 0); |
| 819 | vim_free(ptr); |
| 820 | return fnum; |
| 821 | } |
| 822 | return buflist_add(fname, 0); |
| 823 | #endif |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * push dirbuf onto the directory stack and return pointer to actual dir or |
| 829 | * NULL on error |
| 830 | */ |
| 831 | static char_u * |
| 832 | qf_push_dir(dirbuf, stackptr) |
| 833 | char_u *dirbuf; |
| 834 | struct dir_stack_T **stackptr; |
| 835 | { |
| 836 | struct dir_stack_T *ds_new; |
| 837 | struct dir_stack_T *ds_ptr; |
| 838 | |
| 839 | /* allocate new stack element and hook it in */ |
| 840 | ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T)); |
| 841 | if (ds_new == NULL) |
| 842 | return NULL; |
| 843 | |
| 844 | ds_new->next = *stackptr; |
| 845 | *stackptr = ds_new; |
| 846 | |
| 847 | /* store directory on the stack */ |
| 848 | if (vim_isAbsName(dirbuf) |
| 849 | || (*stackptr)->next == NULL |
| 850 | || (*stackptr && dir_stack != *stackptr)) |
| 851 | (*stackptr)->dirname = vim_strsave(dirbuf); |
| 852 | else |
| 853 | { |
| 854 | /* Okay we don't have an absolute path. |
| 855 | * dirbuf must be a subdir of one of the directories on the stack. |
| 856 | * Let's search... |
| 857 | */ |
| 858 | ds_new = (*stackptr)->next; |
| 859 | (*stackptr)->dirname = NULL; |
| 860 | while (ds_new) |
| 861 | { |
| 862 | vim_free((*stackptr)->dirname); |
| 863 | (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf, |
| 864 | TRUE); |
| 865 | if (mch_isdir((*stackptr)->dirname) == TRUE) |
| 866 | break; |
| 867 | |
| 868 | ds_new = ds_new->next; |
| 869 | } |
| 870 | |
| 871 | /* clean up all dirs we already left */ |
| 872 | while ((*stackptr)->next != ds_new) |
| 873 | { |
| 874 | ds_ptr = (*stackptr)->next; |
| 875 | (*stackptr)->next = (*stackptr)->next->next; |
| 876 | vim_free(ds_ptr->dirname); |
| 877 | vim_free(ds_ptr); |
| 878 | } |
| 879 | |
| 880 | /* Nothing found -> it must be on top level */ |
| 881 | if (ds_new == NULL) |
| 882 | { |
| 883 | vim_free((*stackptr)->dirname); |
| 884 | (*stackptr)->dirname = vim_strsave(dirbuf); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | if ((*stackptr)->dirname != NULL) |
| 889 | return (*stackptr)->dirname; |
| 890 | else |
| 891 | { |
| 892 | ds_ptr = *stackptr; |
| 893 | *stackptr = (*stackptr)->next; |
| 894 | vim_free(ds_ptr); |
| 895 | return NULL; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | |
| 900 | /* |
| 901 | * pop dirbuf from the directory stack and return previous directory or NULL if |
| 902 | * stack is empty |
| 903 | */ |
| 904 | static char_u * |
| 905 | qf_pop_dir(stackptr) |
| 906 | struct dir_stack_T **stackptr; |
| 907 | { |
| 908 | struct dir_stack_T *ds_ptr; |
| 909 | |
| 910 | /* TODO: Should we check if dirbuf is the directory on top of the stack? |
| 911 | * What to do if it isn't? */ |
| 912 | |
| 913 | /* pop top element and free it */ |
| 914 | if (*stackptr != NULL) |
| 915 | { |
| 916 | ds_ptr = *stackptr; |
| 917 | *stackptr = (*stackptr)->next; |
| 918 | vim_free(ds_ptr->dirname); |
| 919 | vim_free(ds_ptr); |
| 920 | } |
| 921 | |
| 922 | /* return NEW top element as current dir or NULL if stack is empty*/ |
| 923 | return *stackptr ? (*stackptr)->dirname : NULL; |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * clean up directory stack |
| 928 | */ |
| 929 | static void |
| 930 | qf_clean_dir_stack(stackptr) |
| 931 | struct dir_stack_T **stackptr; |
| 932 | { |
| 933 | struct dir_stack_T *ds_ptr; |
| 934 | |
| 935 | while ((ds_ptr = *stackptr) != NULL) |
| 936 | { |
| 937 | *stackptr = (*stackptr)->next; |
| 938 | vim_free(ds_ptr->dirname); |
| 939 | vim_free(ds_ptr); |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * Check in which directory of the directory stack the given file can be |
| 945 | * found. |
| 946 | * Returns a pointer to the directory name or NULL if not found |
| 947 | * Cleans up intermediate directory entries. |
| 948 | * |
| 949 | * TODO: How to solve the following problem? |
| 950 | * If we have the this directory tree: |
| 951 | * ./ |
| 952 | * ./aa |
| 953 | * ./aa/bb |
| 954 | * ./bb |
| 955 | * ./bb/x.c |
| 956 | * and make says: |
| 957 | * making all in aa |
| 958 | * making all in bb |
| 959 | * x.c:9: Error |
| 960 | * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb. |
| 961 | * qf_guess_filepath will return NULL. |
| 962 | */ |
| 963 | static char_u * |
| 964 | qf_guess_filepath(filename) |
| 965 | char_u *filename; |
| 966 | { |
| 967 | struct dir_stack_T *ds_ptr; |
| 968 | struct dir_stack_T *ds_tmp; |
| 969 | char_u *fullname; |
| 970 | |
| 971 | /* no dirs on the stack - there's nothing we can do */ |
| 972 | if (dir_stack == NULL) |
| 973 | return NULL; |
| 974 | |
| 975 | ds_ptr = dir_stack->next; |
| 976 | fullname = NULL; |
| 977 | while (ds_ptr) |
| 978 | { |
| 979 | vim_free(fullname); |
| 980 | fullname = concat_fnames(ds_ptr->dirname, filename, TRUE); |
| 981 | |
| 982 | /* If concat_fnames failed, just go on. The worst thing that can happen |
| 983 | * is that we delete the entire stack. |
| 984 | */ |
| 985 | if ((fullname != NULL) && (mch_getperm(fullname) >= 0)) |
| 986 | break; |
| 987 | |
| 988 | ds_ptr = ds_ptr->next; |
| 989 | } |
| 990 | |
| 991 | vim_free(fullname); |
| 992 | |
| 993 | /* clean up all dirs we already left */ |
| 994 | while (dir_stack->next != ds_ptr) |
| 995 | { |
| 996 | ds_tmp = dir_stack->next; |
| 997 | dir_stack->next = dir_stack->next->next; |
| 998 | vim_free(ds_tmp->dirname); |
| 999 | vim_free(ds_tmp); |
| 1000 | } |
| 1001 | |
| 1002 | return ds_ptr==NULL? NULL: ds_ptr->dirname; |
| 1003 | |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * jump to a quickfix line |
| 1008 | * if dir == FORWARD go "errornr" valid entries forward |
| 1009 | * if dir == BACKWARD go "errornr" valid entries backward |
| 1010 | * if dir == FORWARD_FILE go "errornr" valid entries files backward |
| 1011 | * if dir == BACKWARD_FILE go "errornr" valid entries files backward |
| 1012 | * else if "errornr" is zero, redisplay the same line |
| 1013 | * else go to entry "errornr" |
| 1014 | */ |
| 1015 | void |
| 1016 | qf_jump(dir, errornr, forceit) |
| 1017 | int dir; |
| 1018 | int errornr; |
| 1019 | int forceit; |
| 1020 | { |
| 1021 | struct qf_line *qf_ptr; |
| 1022 | struct qf_line *old_qf_ptr; |
| 1023 | int qf_index; |
| 1024 | int old_qf_fnum; |
| 1025 | int old_qf_index; |
| 1026 | int prev_index; |
| 1027 | static char_u *e_no_more_items = (char_u *)N_("E553: No more items"); |
| 1028 | char_u *err = e_no_more_items; |
| 1029 | linenr_T i; |
| 1030 | buf_T *old_curbuf; |
| 1031 | linenr_T old_lnum; |
| 1032 | char_u *old_swb = p_swb; |
| 1033 | colnr_T screen_col; |
| 1034 | colnr_T char_col; |
| 1035 | char_u *line; |
| 1036 | #ifdef FEAT_WINDOWS |
| 1037 | int opened_window = FALSE; |
| 1038 | win_T *win; |
| 1039 | win_T *altwin; |
| 1040 | #endif |
| 1041 | int print_message = TRUE; |
| 1042 | int len; |
| 1043 | #ifdef FEAT_FOLDING |
| 1044 | int old_KeyTyped = KeyTyped; /* getting file may reset it */ |
| 1045 | #endif |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1046 | int ok = OK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0) |
| 1049 | { |
| 1050 | EMSG(_(e_quickfix)); |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | qf_ptr = qf_lists[qf_curlist].qf_ptr; |
| 1055 | old_qf_ptr = qf_ptr; |
| 1056 | qf_index = qf_lists[qf_curlist].qf_index; |
| 1057 | old_qf_index = qf_index; |
| 1058 | if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */ |
| 1059 | { |
| 1060 | while (errornr--) |
| 1061 | { |
| 1062 | old_qf_ptr = qf_ptr; |
| 1063 | prev_index = qf_index; |
| 1064 | old_qf_fnum = qf_ptr->qf_fnum; |
| 1065 | do |
| 1066 | { |
| 1067 | if (qf_index == qf_lists[qf_curlist].qf_count |
| 1068 | || qf_ptr->qf_next == NULL) |
| 1069 | { |
| 1070 | qf_ptr = old_qf_ptr; |
| 1071 | qf_index = prev_index; |
| 1072 | if (err != NULL) |
| 1073 | { |
| 1074 | EMSG(_(err)); |
| 1075 | goto theend; |
| 1076 | } |
| 1077 | errornr = 0; |
| 1078 | break; |
| 1079 | } |
| 1080 | ++qf_index; |
| 1081 | qf_ptr = qf_ptr->qf_next; |
| 1082 | } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid) |
| 1083 | || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum)); |
| 1084 | err = NULL; |
| 1085 | } |
| 1086 | } |
| 1087 | else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */ |
| 1088 | { |
| 1089 | while (errornr--) |
| 1090 | { |
| 1091 | old_qf_ptr = qf_ptr; |
| 1092 | prev_index = qf_index; |
| 1093 | old_qf_fnum = qf_ptr->qf_fnum; |
| 1094 | do |
| 1095 | { |
| 1096 | if (qf_index == 1 || qf_ptr->qf_prev == NULL) |
| 1097 | { |
| 1098 | qf_ptr = old_qf_ptr; |
| 1099 | qf_index = prev_index; |
| 1100 | if (err != NULL) |
| 1101 | { |
| 1102 | EMSG(_(err)); |
| 1103 | goto theend; |
| 1104 | } |
| 1105 | errornr = 0; |
| 1106 | break; |
| 1107 | } |
| 1108 | --qf_index; |
| 1109 | qf_ptr = qf_ptr->qf_prev; |
| 1110 | } while ((!qf_lists[qf_curlist].qf_nonevalid && !qf_ptr->qf_valid) |
| 1111 | || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum)); |
| 1112 | err = NULL; |
| 1113 | } |
| 1114 | } |
| 1115 | else if (errornr != 0) /* go to specified number */ |
| 1116 | { |
| 1117 | while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL) |
| 1118 | { |
| 1119 | --qf_index; |
| 1120 | qf_ptr = qf_ptr->qf_prev; |
| 1121 | } |
| 1122 | while (errornr > qf_index && qf_index < qf_lists[qf_curlist].qf_count |
| 1123 | && qf_ptr->qf_next != NULL) |
| 1124 | { |
| 1125 | ++qf_index; |
| 1126 | qf_ptr = qf_ptr->qf_next; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | #ifdef FEAT_WINDOWS |
| 1131 | qf_lists[qf_curlist].qf_index = qf_index; |
| 1132 | if (qf_win_pos_update(old_qf_index)) |
| 1133 | /* No need to print the error message if it's visible in the error |
| 1134 | * window */ |
| 1135 | print_message = FALSE; |
| 1136 | |
| 1137 | /* |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1138 | * For ":helpgrep" find a help window or open one. |
| 1139 | */ |
| 1140 | if (qf_ptr->qf_type == 1 && !curwin->w_buffer->b_help) |
| 1141 | { |
| 1142 | win_T *wp; |
| 1143 | int n; |
| 1144 | |
| 1145 | for (wp = firstwin; wp != NULL; wp = wp->w_next) |
| 1146 | if (wp->w_buffer != NULL && wp->w_buffer->b_help) |
| 1147 | break; |
| 1148 | if (wp != NULL && wp->w_buffer->b_nwindows > 0) |
| 1149 | win_enter(wp, TRUE); |
| 1150 | else |
| 1151 | { |
| 1152 | /* |
| 1153 | * Split off help window; put it at far top if no position |
| 1154 | * specified, the current window is vertically split and narrow. |
| 1155 | */ |
| 1156 | n = WSP_HELP; |
| 1157 | # ifdef FEAT_VERTSPLIT |
| 1158 | if (cmdmod.split == 0 && curwin->w_width != Columns |
| 1159 | && curwin->w_width < 80) |
| 1160 | n |= WSP_TOP; |
| 1161 | # endif |
| 1162 | if (win_split(0, n) == FAIL) |
| 1163 | goto theend; |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 1164 | opened_window = TRUE; /* close it when fail */ |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1165 | |
| 1166 | if (curwin->w_height < p_hh) |
| 1167 | win_setheight((int)p_hh); |
| 1168 | } |
| 1169 | |
| 1170 | if (!p_im) |
| 1171 | restart_edit = 0; /* don't want insert mode in help file */ |
| 1172 | } |
| 1173 | |
| 1174 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1175 | * If currently in the quickfix window, find another window to show the |
| 1176 | * file in. |
| 1177 | */ |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 1178 | if (bt_quickfix(curbuf) && !opened_window) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1179 | { |
| 1180 | /* |
| 1181 | * If there is no file specified, we don't know where to go. |
| 1182 | * But do advance, otherwise ":cn" gets stuck. |
| 1183 | */ |
| 1184 | if (qf_ptr->qf_fnum == 0) |
| 1185 | goto theend; |
| 1186 | |
| 1187 | /* |
| 1188 | * If there is only one window, create a new one above the quickfix |
| 1189 | * window. |
| 1190 | */ |
| 1191 | if (firstwin == lastwin) |
| 1192 | { |
| 1193 | if (win_split(0, WSP_ABOVE) == FAIL) |
| 1194 | goto failed; /* not enough room for window */ |
| 1195 | opened_window = TRUE; /* close it when fail */ |
| 1196 | p_swb = empty_option; /* don't split again */ |
| 1197 | # ifdef FEAT_SCROLLBIND |
| 1198 | curwin->w_p_scb = FALSE; |
| 1199 | # endif |
| 1200 | } |
| 1201 | else |
| 1202 | { |
| 1203 | /* |
| 1204 | * Try to find a window that shows the right buffer. |
| 1205 | * Default to the window just above the quickfix buffer. |
| 1206 | */ |
| 1207 | win = curwin; |
| 1208 | altwin = NULL; |
| 1209 | for (;;) |
| 1210 | { |
| 1211 | if (win->w_buffer->b_fnum == qf_ptr->qf_fnum) |
| 1212 | break; |
| 1213 | if (win->w_prev == NULL) |
| 1214 | win = lastwin; /* wrap around the top */ |
| 1215 | else |
| 1216 | win = win->w_prev; /* go to previous window */ |
| 1217 | |
| 1218 | if (bt_quickfix(win->w_buffer)) |
| 1219 | { |
| 1220 | /* Didn't find it, go to the window before the quickfix |
| 1221 | * window. */ |
| 1222 | if (altwin != NULL) |
| 1223 | win = altwin; |
| 1224 | else if (curwin->w_prev != NULL) |
| 1225 | win = curwin->w_prev; |
| 1226 | else |
| 1227 | win = curwin->w_next; |
| 1228 | break; |
| 1229 | } |
| 1230 | |
| 1231 | /* Remember a usable window. */ |
| 1232 | if (altwin == NULL && !win->w_p_pvw |
| 1233 | && win->w_buffer->b_p_bt[0] == NUL) |
| 1234 | altwin = win; |
| 1235 | } |
| 1236 | |
| 1237 | win_goto(win); |
| 1238 | } |
| 1239 | } |
| 1240 | #endif |
| 1241 | |
| 1242 | /* |
| 1243 | * If there is a file name, |
| 1244 | * read the wanted file if needed, and check autowrite etc. |
| 1245 | */ |
| 1246 | old_curbuf = curbuf; |
| 1247 | old_lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1248 | |
| 1249 | if (qf_ptr->qf_fnum != 0) |
| 1250 | { |
| 1251 | if (qf_ptr->qf_type == 1) |
| 1252 | { |
| 1253 | /* Open help file (do_ecmd() will set b_help flag, readfile() will |
| 1254 | * set b_p_ro flag). */ |
| 1255 | if (!can_abandon(curbuf, forceit)) |
| 1256 | { |
| 1257 | EMSG(_(e_nowrtmsg)); |
| 1258 | ok = FALSE; |
| 1259 | } |
| 1260 | else |
| 1261 | ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1, |
| 1262 | ECMD_HIDE + ECMD_SET_HELP); |
| 1263 | } |
| 1264 | else |
| 1265 | ok = buflist_getfile(qf_ptr->qf_fnum, |
| 1266 | (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit); |
| 1267 | } |
| 1268 | |
| 1269 | if (ok == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1270 | { |
| 1271 | /* When not switched to another buffer, still need to set pc mark */ |
| 1272 | if (curbuf == old_curbuf) |
| 1273 | setpcmark(); |
| 1274 | |
| 1275 | /* |
| 1276 | * Go to line with error, unless qf_lnum is 0. |
| 1277 | */ |
| 1278 | i = qf_ptr->qf_lnum; |
| 1279 | if (i > 0) |
| 1280 | { |
| 1281 | if (i > curbuf->b_ml.ml_line_count) |
| 1282 | i = curbuf->b_ml.ml_line_count; |
| 1283 | curwin->w_cursor.lnum = i; |
| 1284 | } |
| 1285 | if (qf_ptr->qf_col > 0) |
| 1286 | { |
| 1287 | curwin->w_cursor.col = qf_ptr->qf_col - 1; |
| 1288 | if (qf_ptr->qf_virt_col == TRUE) |
| 1289 | { |
| 1290 | /* |
| 1291 | * Check each character from the beginning of the error |
| 1292 | * line up to the error column. For each tab character |
| 1293 | * found, reduce the error column value by the length of |
| 1294 | * a tab character. |
| 1295 | */ |
| 1296 | line = ml_get_curline(); |
| 1297 | screen_col = 0; |
| 1298 | for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col) |
| 1299 | { |
| 1300 | if (*line == NUL) |
| 1301 | break; |
| 1302 | if (*line++ == '\t') |
| 1303 | { |
| 1304 | curwin->w_cursor.col -= 7 - (screen_col % 8); |
| 1305 | screen_col += 8 - (screen_col % 8); |
| 1306 | } |
| 1307 | else |
| 1308 | ++screen_col; |
| 1309 | } |
| 1310 | } |
| 1311 | check_cursor(); |
| 1312 | } |
| 1313 | else |
| 1314 | beginline(BL_WHITE | BL_FIX); |
| 1315 | |
| 1316 | #ifdef FEAT_FOLDING |
| 1317 | if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped) |
| 1318 | foldOpenCursor(); |
| 1319 | #endif |
| 1320 | if (print_message) |
| 1321 | { |
| 1322 | /* Update the screen before showing the message */ |
| 1323 | update_topline_redraw(); |
| 1324 | sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index, |
| 1325 | qf_lists[qf_curlist].qf_count, |
| 1326 | qf_ptr->qf_cleared ? _(" (line deleted)") : "", |
| 1327 | (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr)); |
| 1328 | /* Add the message, skipping leading whitespace and newlines. */ |
| 1329 | len = (int)STRLEN(IObuff); |
| 1330 | qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len); |
| 1331 | |
| 1332 | /* Output the message. Overwrite to avoid scrolling when the 'O' |
| 1333 | * flag is present in 'shortmess'; But when not jumping, print the |
| 1334 | * whole message. */ |
| 1335 | i = msg_scroll; |
| 1336 | if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum) |
| 1337 | msg_scroll = TRUE; |
| 1338 | else if (!msg_scrolled && shortmess(SHM_OVERALL)) |
| 1339 | msg_scroll = FALSE; |
| 1340 | msg_attr_keep(IObuff, 0, TRUE); |
| 1341 | msg_scroll = i; |
| 1342 | } |
| 1343 | } |
| 1344 | else |
| 1345 | { |
| 1346 | #ifdef FEAT_WINDOWS |
| 1347 | if (opened_window) |
| 1348 | win_close(curwin, TRUE); /* Close opened window */ |
| 1349 | #endif |
| 1350 | if (qf_ptr->qf_fnum != 0) |
| 1351 | { |
| 1352 | /* |
| 1353 | * Couldn't open file, so put index back where it was. This could |
| 1354 | * happen if the file was readonly and we changed something. |
| 1355 | */ |
| 1356 | #ifdef FEAT_WINDOWS |
| 1357 | failed: |
| 1358 | #endif |
| 1359 | qf_ptr = old_qf_ptr; |
| 1360 | qf_index = old_qf_index; |
| 1361 | } |
| 1362 | } |
| 1363 | theend: |
| 1364 | qf_lists[qf_curlist].qf_ptr = qf_ptr; |
| 1365 | qf_lists[qf_curlist].qf_index = qf_index; |
| 1366 | #ifdef FEAT_WINDOWS |
| 1367 | if (p_swb != old_swb && opened_window) |
| 1368 | { |
| 1369 | /* Restore old 'switchbuf' value, but not when an autocommand or |
| 1370 | * modeline has changed the value. */ |
| 1371 | if (p_swb == empty_option) |
| 1372 | p_swb = old_swb; |
| 1373 | else |
| 1374 | free_string_option(old_swb); |
| 1375 | } |
| 1376 | #endif |
| 1377 | } |
| 1378 | |
| 1379 | /* |
| 1380 | * ":clist": list all errors |
| 1381 | */ |
| 1382 | void |
| 1383 | qf_list(eap) |
| 1384 | exarg_T *eap; |
| 1385 | { |
| 1386 | buf_T *buf; |
| 1387 | char_u *fname; |
| 1388 | struct qf_line *qfp; |
| 1389 | int i; |
| 1390 | int idx1 = 1; |
| 1391 | int idx2 = -1; |
| 1392 | int need_return = TRUE; |
| 1393 | int last_printed = 1; |
| 1394 | char_u *arg = eap->arg; |
| 1395 | int all = eap->forceit; /* if not :cl!, only show |
| 1396 | recognised errors */ |
| 1397 | |
| 1398 | if (qf_curlist >= qf_listcount || qf_lists[qf_curlist].qf_count == 0) |
| 1399 | { |
| 1400 | EMSG(_(e_quickfix)); |
| 1401 | return; |
| 1402 | } |
| 1403 | if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL) |
| 1404 | { |
| 1405 | EMSG(_(e_trailing)); |
| 1406 | return; |
| 1407 | } |
| 1408 | i = qf_lists[qf_curlist].qf_count; |
| 1409 | if (idx1 < 0) |
| 1410 | idx1 = (-idx1 > i) ? 0 : idx1 + i + 1; |
| 1411 | if (idx2 < 0) |
| 1412 | idx2 = (-idx2 > i) ? 0 : idx2 + i + 1; |
| 1413 | |
| 1414 | more_back_used = TRUE; |
| 1415 | if (qf_lists[qf_curlist].qf_nonevalid) |
| 1416 | all = TRUE; |
| 1417 | qfp = qf_lists[qf_curlist].qf_start; |
| 1418 | for (i = 1; !got_int && i <= qf_lists[qf_curlist].qf_count; ) |
| 1419 | { |
| 1420 | if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2) |
| 1421 | { |
| 1422 | if (need_return) |
| 1423 | { |
| 1424 | msg_putchar('\n'); |
| 1425 | need_return = FALSE; |
| 1426 | } |
| 1427 | if (more_back == 0) |
| 1428 | { |
| 1429 | fname = NULL; |
| 1430 | if (qfp->qf_fnum != 0 |
| 1431 | && (buf = buflist_findnr(qfp->qf_fnum)) != NULL) |
| 1432 | { |
| 1433 | fname = buf->b_fname; |
| 1434 | if (qfp->qf_type == 1) /* :helpgrep */ |
| 1435 | fname = gettail(fname); |
| 1436 | } |
| 1437 | if (fname == NULL) |
| 1438 | sprintf((char *)IObuff, "%2d", i); |
| 1439 | else |
| 1440 | sprintf((char *)IObuff, "%2d %s", i, (char *)fname); |
| 1441 | msg_outtrans_attr(IObuff, i == qf_lists[qf_curlist].qf_index |
| 1442 | ? hl_attr(HLF_L) : hl_attr(HLF_D)); |
| 1443 | if (qfp->qf_lnum == 0) |
| 1444 | IObuff[0] = NUL; |
| 1445 | else if (qfp->qf_col == 0) |
| 1446 | sprintf((char *)IObuff, ":%ld", qfp->qf_lnum); |
| 1447 | else |
| 1448 | sprintf((char *)IObuff, ":%ld col %d", |
| 1449 | qfp->qf_lnum, qfp->qf_col); |
| 1450 | sprintf((char *)IObuff + STRLEN(IObuff), "%s: ", |
| 1451 | (char *)qf_types(qfp->qf_type, qfp->qf_nr)); |
| 1452 | msg_puts_attr(IObuff, hl_attr(HLF_N)); |
| 1453 | /* Remove newlines and leading whitespace from the text. |
| 1454 | * For an unrecognized line keep the indent, the compiler may |
| 1455 | * mark a word with ^^^^. */ |
| 1456 | qf_fmt_text((fname != NULL || qfp->qf_lnum != 0) |
| 1457 | ? skipwhite(qfp->qf_text) : qfp->qf_text, |
| 1458 | IObuff, IOSIZE); |
| 1459 | msg_prt_line(IObuff); |
| 1460 | out_flush(); /* show one line at a time */ |
| 1461 | need_return = TRUE; |
| 1462 | last_printed = i; |
| 1463 | } |
| 1464 | } |
| 1465 | if (more_back) |
| 1466 | { |
| 1467 | /* scrolling backwards from the more-prompt */ |
| 1468 | /* TODO: compute the number of items from the screen lines */ |
| 1469 | more_back = more_back * 2 - 1; |
| 1470 | while (i > last_printed - more_back && i > idx1) |
| 1471 | { |
| 1472 | do |
| 1473 | { |
| 1474 | qfp = qfp->qf_prev; |
| 1475 | --i; |
| 1476 | } |
| 1477 | while (i > idx1 && !qfp->qf_valid && !all); |
| 1478 | } |
| 1479 | more_back = 0; |
| 1480 | } |
| 1481 | else |
| 1482 | { |
| 1483 | qfp = qfp->qf_next; |
| 1484 | ++i; |
| 1485 | } |
| 1486 | ui_breakcheck(); |
| 1487 | } |
| 1488 | more_back_used = FALSE; |
| 1489 | } |
| 1490 | |
| 1491 | /* |
| 1492 | * Remove newlines and leading whitespace from an error message. |
| 1493 | * Put the result in "buf[bufsize]". |
| 1494 | */ |
| 1495 | static void |
| 1496 | qf_fmt_text(text, buf, bufsize) |
| 1497 | char_u *text; |
| 1498 | char_u *buf; |
| 1499 | int bufsize; |
| 1500 | { |
| 1501 | int i; |
| 1502 | char_u *p = text; |
| 1503 | |
| 1504 | for (i = 0; *p != NUL && i < bufsize - 1; ++i) |
| 1505 | { |
| 1506 | if (*p == '\n') |
| 1507 | { |
| 1508 | buf[i] = ' '; |
| 1509 | while (*++p != NUL) |
| 1510 | if (!vim_iswhite(*p) && *p != '\n') |
| 1511 | break; |
| 1512 | } |
| 1513 | else |
| 1514 | buf[i] = *p++; |
| 1515 | } |
| 1516 | buf[i] = NUL; |
| 1517 | } |
| 1518 | |
| 1519 | /* |
| 1520 | * ":colder [count]": Up in the quickfix stack. |
| 1521 | * ":cnewer [count]": Down in the quickfix stack. |
| 1522 | */ |
| 1523 | void |
| 1524 | qf_age(eap) |
| 1525 | exarg_T *eap; |
| 1526 | { |
| 1527 | int count; |
| 1528 | |
| 1529 | if (eap->addr_count != 0) |
| 1530 | count = eap->line2; |
| 1531 | else |
| 1532 | count = 1; |
| 1533 | while (count--) |
| 1534 | { |
| 1535 | if (eap->cmdidx == CMD_colder) |
| 1536 | { |
| 1537 | if (qf_curlist == 0) |
| 1538 | { |
| 1539 | EMSG(_("E380: At bottom of quickfix stack")); |
| 1540 | return; |
| 1541 | } |
| 1542 | --qf_curlist; |
| 1543 | } |
| 1544 | else |
| 1545 | { |
| 1546 | if (qf_curlist >= qf_listcount - 1) |
| 1547 | { |
| 1548 | EMSG(_("E381: At top of quickfix stack")); |
| 1549 | return; |
| 1550 | } |
| 1551 | ++qf_curlist; |
| 1552 | } |
| 1553 | } |
| 1554 | qf_msg(); |
| 1555 | } |
| 1556 | |
| 1557 | static void |
| 1558 | qf_msg() |
| 1559 | { |
| 1560 | smsg((char_u *)_("error list %d of %d; %d errors"), |
| 1561 | qf_curlist + 1, qf_listcount, qf_lists[qf_curlist].qf_count); |
| 1562 | #ifdef FEAT_WINDOWS |
| 1563 | qf_update_buffer(); |
| 1564 | #endif |
| 1565 | } |
| 1566 | |
| 1567 | /* |
| 1568 | * free the error list |
| 1569 | */ |
| 1570 | static void |
| 1571 | qf_free(idx) |
| 1572 | int idx; |
| 1573 | { |
| 1574 | struct qf_line *qfp; |
| 1575 | |
| 1576 | while (qf_lists[idx].qf_count) |
| 1577 | { |
| 1578 | qfp = qf_lists[idx].qf_start->qf_next; |
| 1579 | vim_free(qf_lists[idx].qf_start->qf_text); |
| 1580 | vim_free(qf_lists[idx].qf_start); |
| 1581 | qf_lists[idx].qf_start = qfp; |
| 1582 | --qf_lists[idx].qf_count; |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | /* |
| 1587 | * qf_mark_adjust: adjust marks |
| 1588 | */ |
| 1589 | void |
| 1590 | qf_mark_adjust(line1, line2, amount, amount_after) |
| 1591 | linenr_T line1; |
| 1592 | linenr_T line2; |
| 1593 | long amount; |
| 1594 | long amount_after; |
| 1595 | { |
| 1596 | int i; |
| 1597 | struct qf_line *qfp; |
| 1598 | int idx; |
| 1599 | |
| 1600 | for (idx = 0; idx < qf_listcount; ++idx) |
| 1601 | if (qf_lists[idx].qf_count) |
| 1602 | for (i = 0, qfp = qf_lists[idx].qf_start; |
| 1603 | i < qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next) |
| 1604 | if (qfp->qf_fnum == curbuf->b_fnum) |
| 1605 | { |
| 1606 | if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2) |
| 1607 | { |
| 1608 | if (amount == MAXLNUM) |
| 1609 | qfp->qf_cleared = TRUE; |
| 1610 | else |
| 1611 | qfp->qf_lnum += amount; |
| 1612 | } |
| 1613 | else if (amount_after && qfp->qf_lnum > line2) |
| 1614 | qfp->qf_lnum += amount_after; |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | /* |
| 1619 | * Make a nice message out of the error character and the error number: |
| 1620 | * char number message |
| 1621 | * e or E 0 " error" |
| 1622 | * w or W 0 " warning" |
| 1623 | * i or I 0 " info" |
| 1624 | * 0 0 "" |
| 1625 | * other 0 " c" |
| 1626 | * e or E n " error n" |
| 1627 | * w or W n " warning n" |
| 1628 | * i or I n " info n" |
| 1629 | * 0 n " error n" |
| 1630 | * other n " c n" |
| 1631 | * 1 x "" :helpgrep |
| 1632 | */ |
| 1633 | static char_u * |
| 1634 | qf_types(c, nr) |
| 1635 | int c, nr; |
| 1636 | { |
| 1637 | static char_u buf[20]; |
| 1638 | static char_u cc[3]; |
| 1639 | char_u *p; |
| 1640 | |
| 1641 | if (c == 'W' || c == 'w') |
| 1642 | p = (char_u *)" warning"; |
| 1643 | else if (c == 'I' || c == 'i') |
| 1644 | p = (char_u *)" info"; |
| 1645 | else if (c == 'E' || c == 'e' || (c == 0 && nr > 0)) |
| 1646 | p = (char_u *)" error"; |
| 1647 | else if (c == 0 || c == 1) |
| 1648 | p = (char_u *)""; |
| 1649 | else |
| 1650 | { |
| 1651 | cc[0] = ' '; |
| 1652 | cc[1] = c; |
| 1653 | cc[2] = NUL; |
| 1654 | p = cc; |
| 1655 | } |
| 1656 | |
| 1657 | if (nr <= 0) |
| 1658 | return p; |
| 1659 | |
| 1660 | sprintf((char *)buf, "%s %3d", (char *)p, nr); |
| 1661 | return buf; |
| 1662 | } |
| 1663 | |
| 1664 | #if defined(FEAT_WINDOWS) || defined(PROTO) |
| 1665 | /* |
| 1666 | * ":cwindow": open the quickfix window if we have errors to display, |
| 1667 | * close it if not. |
| 1668 | */ |
| 1669 | void |
| 1670 | ex_cwindow(eap) |
| 1671 | exarg_T *eap; |
| 1672 | { |
| 1673 | win_T *win; |
| 1674 | |
| 1675 | /* |
| 1676 | * Look for an existing quickfix window. |
| 1677 | */ |
| 1678 | for (win = firstwin; win != NULL; win = win->w_next) |
| 1679 | if (bt_quickfix(win->w_buffer)) |
| 1680 | break; |
| 1681 | |
| 1682 | /* |
| 1683 | * If a quickfix window is open but we have no errors to display, |
| 1684 | * close the window. If a quickfix window is not open, then open |
| 1685 | * it if we have errors; otherwise, leave it closed. |
| 1686 | */ |
| 1687 | if (qf_lists[qf_curlist].qf_nonevalid || qf_curlist >= qf_listcount) |
| 1688 | { |
| 1689 | if (win != NULL) |
| 1690 | ex_cclose(eap); |
| 1691 | } |
| 1692 | else if (win == NULL) |
| 1693 | ex_copen(eap); |
| 1694 | } |
| 1695 | |
| 1696 | /* |
| 1697 | * ":cclose": close the window showing the list of errors. |
| 1698 | */ |
| 1699 | /*ARGSUSED*/ |
| 1700 | void |
| 1701 | ex_cclose(eap) |
| 1702 | exarg_T *eap; |
| 1703 | { |
| 1704 | win_T *win; |
| 1705 | |
| 1706 | /* |
| 1707 | * Find existing quickfix window and close it. |
| 1708 | */ |
| 1709 | for (win = firstwin; win != NULL; win = win->w_next) |
| 1710 | if (bt_quickfix(win->w_buffer)) |
| 1711 | break; |
| 1712 | |
| 1713 | if (win != NULL) |
| 1714 | win_close(win, FALSE); |
| 1715 | } |
| 1716 | |
| 1717 | /* |
| 1718 | * ":copen": open a window that shows the list of errors. |
| 1719 | */ |
| 1720 | void |
| 1721 | ex_copen(eap) |
| 1722 | exarg_T *eap; |
| 1723 | { |
| 1724 | int height; |
| 1725 | buf_T *buf; |
| 1726 | win_T *win; |
| 1727 | |
| 1728 | if (eap->addr_count != 0) |
| 1729 | height = eap->line2; |
| 1730 | else |
| 1731 | height = QF_WINHEIGHT; |
| 1732 | |
| 1733 | #ifdef FEAT_VISUAL |
| 1734 | reset_VIsual_and_resel(); /* stop Visual mode */ |
| 1735 | #endif |
| 1736 | #ifdef FEAT_GUI |
| 1737 | need_mouse_correct = TRUE; |
| 1738 | #endif |
| 1739 | |
| 1740 | /* |
| 1741 | * Find existing quickfix window, or open a new one. |
| 1742 | */ |
| 1743 | for (win = firstwin; win != NULL; win = win->w_next) |
| 1744 | if (bt_quickfix(win->w_buffer)) |
| 1745 | break; |
| 1746 | if (win != NULL) |
| 1747 | win_goto(win); |
| 1748 | else |
| 1749 | { |
| 1750 | /* The current window becomes the previous window afterwards. */ |
| 1751 | win = curwin; |
| 1752 | |
| 1753 | /* Create the new window at the very bottom. */ |
| 1754 | win_goto(lastwin); |
| 1755 | if (win_split(height, WSP_BELOW) == FAIL) |
| 1756 | return; /* not enough room for window */ |
| 1757 | #ifdef FEAT_SCROLLBIND |
| 1758 | curwin->w_p_scb = FALSE; |
| 1759 | #endif |
| 1760 | |
| 1761 | /* |
| 1762 | * Find existing quickfix buffer, or create a new one. |
| 1763 | */ |
| 1764 | buf = qf_find_buf(); |
| 1765 | if (buf == NULL) |
| 1766 | { |
| 1767 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE); |
| 1768 | /* switch off 'swapfile' */ |
| 1769 | set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL); |
| 1770 | set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix", |
| 1771 | OPT_LOCAL); |
| 1772 | set_option_value((char_u *)"bh", 0L, (char_u *)"delete", OPT_LOCAL); |
| 1773 | set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL); |
| 1774 | } |
| 1775 | else if (buf != curbuf) |
| 1776 | set_curbuf(buf, DOBUF_GOTO); |
| 1777 | |
Bram Moolenaar | 383f9bc | 2005-01-19 22:18:32 +0000 | [diff] [blame] | 1778 | #ifdef FEAT_VERTSPLIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1779 | /* Only set the height when there is no window to the side. */ |
| 1780 | if (curwin->w_width == Columns) |
Bram Moolenaar | 383f9bc | 2005-01-19 22:18:32 +0000 | [diff] [blame] | 1781 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1782 | win_setheight(height); |
| 1783 | curwin->w_p_wfh = TRUE; /* set 'winfixheight' */ |
| 1784 | if (win_valid(win)) |
| 1785 | prevwin = win; |
| 1786 | } |
| 1787 | |
| 1788 | /* |
| 1789 | * Fill the buffer with the quickfix list. |
| 1790 | */ |
| 1791 | qf_fill_buffer(); |
| 1792 | |
| 1793 | curwin->w_cursor.lnum = qf_lists[qf_curlist].qf_index; |
| 1794 | curwin->w_cursor.col = 0; |
| 1795 | check_cursor(); |
| 1796 | update_topline(); /* scroll to show the line */ |
| 1797 | } |
| 1798 | |
| 1799 | /* |
| 1800 | * Return the number of the current entry (line number in the quickfix |
| 1801 | * window). |
| 1802 | */ |
| 1803 | linenr_T |
| 1804 | qf_current_entry() |
| 1805 | { |
| 1806 | return qf_lists[qf_curlist].qf_index; |
| 1807 | } |
| 1808 | |
| 1809 | /* |
| 1810 | * Update the cursor position in the quickfix window to the current error. |
| 1811 | * Return TRUE if there is a quickfix window. |
| 1812 | */ |
| 1813 | static int |
| 1814 | qf_win_pos_update(old_qf_index) |
| 1815 | int old_qf_index; /* previous qf_index or zero */ |
| 1816 | { |
| 1817 | win_T *win; |
| 1818 | int qf_index = qf_lists[qf_curlist].qf_index; |
| 1819 | |
| 1820 | /* |
| 1821 | * Put the cursor on the current error in the quickfix window, so that |
| 1822 | * it's viewable. |
| 1823 | */ |
| 1824 | for (win = firstwin; win != NULL; win = win->w_next) |
| 1825 | if (bt_quickfix(win->w_buffer)) |
| 1826 | break; |
| 1827 | if (win != NULL |
| 1828 | && qf_index <= win->w_buffer->b_ml.ml_line_count |
| 1829 | && old_qf_index != qf_index) |
| 1830 | { |
| 1831 | win_T *old_curwin = curwin; |
| 1832 | |
| 1833 | curwin = win; |
| 1834 | curbuf = win->w_buffer; |
| 1835 | if (qf_index > old_qf_index) |
| 1836 | { |
| 1837 | curwin->w_redraw_top = old_qf_index; |
| 1838 | curwin->w_redraw_bot = qf_index; |
| 1839 | } |
| 1840 | else |
| 1841 | { |
| 1842 | curwin->w_redraw_top = qf_index; |
| 1843 | curwin->w_redraw_bot = old_qf_index; |
| 1844 | } |
| 1845 | curwin->w_cursor.lnum = qf_index; |
| 1846 | curwin->w_cursor.col = 0; |
| 1847 | update_topline(); /* scroll to show the line */ |
| 1848 | redraw_later(VALID); |
| 1849 | curwin->w_redr_status = TRUE; /* update ruler */ |
| 1850 | curwin = old_curwin; |
| 1851 | curbuf = curwin->w_buffer; |
| 1852 | } |
| 1853 | return win != NULL; |
| 1854 | } |
| 1855 | |
| 1856 | /* |
| 1857 | * Find quickfix buffer. |
| 1858 | */ |
| 1859 | static buf_T * |
| 1860 | qf_find_buf() |
| 1861 | { |
| 1862 | buf_T *buf; |
| 1863 | |
| 1864 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 1865 | if (bt_quickfix(buf)) |
| 1866 | break; |
| 1867 | return buf; |
| 1868 | } |
| 1869 | |
| 1870 | /* |
| 1871 | * Find the quickfix buffer. If it exists, update the contents. |
| 1872 | */ |
| 1873 | static void |
| 1874 | qf_update_buffer() |
| 1875 | { |
| 1876 | buf_T *buf; |
| 1877 | #ifdef FEAT_AUTOCMD |
| 1878 | aco_save_T aco; |
| 1879 | #else |
| 1880 | buf_T *save_curbuf; |
| 1881 | #endif |
| 1882 | |
| 1883 | /* Check if a buffer for the quickfix list exists. Update it. */ |
| 1884 | buf = qf_find_buf(); |
| 1885 | if (buf != NULL) |
| 1886 | { |
| 1887 | #ifdef FEAT_AUTOCMD |
| 1888 | /* set curwin/curbuf to buf and save a few things */ |
| 1889 | aucmd_prepbuf(&aco, buf); |
| 1890 | #else |
| 1891 | save_curbuf = curbuf; |
| 1892 | curbuf = buf; |
| 1893 | #endif |
| 1894 | |
| 1895 | qf_fill_buffer(); |
| 1896 | |
| 1897 | #ifdef FEAT_AUTOCMD |
| 1898 | /* restore curwin/curbuf and a few other things */ |
| 1899 | aucmd_restbuf(&aco); |
| 1900 | #else |
| 1901 | curbuf = save_curbuf; |
| 1902 | #endif |
| 1903 | |
| 1904 | (void)qf_win_pos_update(0); |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | /* |
| 1909 | * Fill current buffer with quickfix errors, replacing any previous contents. |
| 1910 | * curbuf must be the quickfix buffer! |
| 1911 | */ |
| 1912 | static void |
| 1913 | qf_fill_buffer() |
| 1914 | { |
| 1915 | linenr_T lnum; |
| 1916 | struct qf_line *qfp; |
| 1917 | buf_T *errbuf; |
| 1918 | int len; |
| 1919 | int old_KeyTyped = KeyTyped; |
| 1920 | |
| 1921 | /* delete all existing lines */ |
| 1922 | while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0) |
| 1923 | (void)ml_delete((linenr_T)1, FALSE); |
| 1924 | |
| 1925 | /* Check if there is anything to display */ |
| 1926 | if (qf_curlist < qf_listcount) |
| 1927 | { |
| 1928 | /* Add one line for each error */ |
| 1929 | qfp = qf_lists[qf_curlist].qf_start; |
| 1930 | for (lnum = 0; lnum < qf_lists[qf_curlist].qf_count; ++lnum) |
| 1931 | { |
| 1932 | if (qfp->qf_fnum != 0 |
| 1933 | && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL |
| 1934 | && errbuf->b_fname != NULL) |
| 1935 | { |
| 1936 | if (qfp->qf_type == 1) /* :helpgrep */ |
| 1937 | STRCPY(IObuff, gettail(errbuf->b_fname)); |
| 1938 | else |
| 1939 | STRCPY(IObuff, errbuf->b_fname); |
| 1940 | len = (int)STRLEN(IObuff); |
| 1941 | } |
| 1942 | else |
| 1943 | len = 0; |
| 1944 | IObuff[len++] = '|'; |
| 1945 | |
| 1946 | if (qfp->qf_lnum > 0) |
| 1947 | { |
| 1948 | sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum); |
| 1949 | len += (int)STRLEN(IObuff + len); |
| 1950 | |
| 1951 | if (qfp->qf_col > 0) |
| 1952 | { |
| 1953 | sprintf((char *)IObuff + len, " col %d", qfp->qf_col); |
| 1954 | len += (int)STRLEN(IObuff + len); |
| 1955 | } |
| 1956 | |
| 1957 | sprintf((char *)IObuff + len, "%s", |
| 1958 | (char *)qf_types(qfp->qf_type, qfp->qf_nr)); |
| 1959 | len += (int)STRLEN(IObuff + len); |
| 1960 | } |
| 1961 | IObuff[len++] = '|'; |
| 1962 | IObuff[len++] = ' '; |
| 1963 | |
| 1964 | /* Remove newlines and leading whitespace from the text. |
| 1965 | * For an unrecognized line keep the indent, the compiler may |
| 1966 | * mark a word with ^^^^. */ |
| 1967 | qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text, |
| 1968 | IObuff + len, IOSIZE - len); |
| 1969 | |
| 1970 | if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE) |
| 1971 | == FAIL) |
| 1972 | break; |
| 1973 | qfp = qfp->qf_next; |
| 1974 | } |
| 1975 | /* Delete the empty line which is now at the end */ |
| 1976 | (void)ml_delete(lnum + 1, FALSE); |
| 1977 | } |
| 1978 | |
| 1979 | /* correct cursor position */ |
| 1980 | check_lnums(TRUE); |
| 1981 | |
| 1982 | /* Set the 'filetype' to "qf" each time after filling the buffer. This |
| 1983 | * resembles reading a file into a buffer, it's more logical when using |
| 1984 | * autocommands. */ |
| 1985 | set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL); |
| 1986 | curbuf->b_p_ma = FALSE; |
| 1987 | |
| 1988 | #ifdef FEAT_AUTOCMD |
| 1989 | apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL, |
| 1990 | FALSE, curbuf); |
| 1991 | apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL, |
| 1992 | FALSE, curbuf); |
| 1993 | #endif |
| 1994 | |
| 1995 | /* make sure it will be redrawn */ |
| 1996 | redraw_curbuf_later(NOT_VALID); |
| 1997 | |
| 1998 | /* Restore KeyTyped, setting 'filetype' may reset it. */ |
| 1999 | KeyTyped = old_KeyTyped; |
| 2000 | } |
| 2001 | |
| 2002 | #endif /* FEAT_WINDOWS */ |
| 2003 | |
| 2004 | /* |
| 2005 | * Return TRUE if "buf" is the quickfix buffer. |
| 2006 | */ |
| 2007 | int |
| 2008 | bt_quickfix(buf) |
| 2009 | buf_T *buf; |
| 2010 | { |
| 2011 | return (buf->b_p_bt[0] == 'q'); |
| 2012 | } |
| 2013 | |
| 2014 | /* |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 2015 | * Return TRUE if "buf" is a "nofile" or "acwrite" buffer. |
| 2016 | * This means the buffer name is not a file name. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2017 | */ |
| 2018 | int |
| 2019 | bt_nofile(buf) |
| 2020 | buf_T *buf; |
| 2021 | { |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 2022 | return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f') |
| 2023 | || buf->b_p_bt[0] == 'a'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | /* |
| 2027 | * Return TRUE if "buf" is a "nowrite" or "nofile" buffer. |
| 2028 | */ |
| 2029 | int |
| 2030 | bt_dontwrite(buf) |
| 2031 | buf_T *buf; |
| 2032 | { |
| 2033 | return (buf->b_p_bt[0] == 'n'); |
| 2034 | } |
| 2035 | |
| 2036 | int |
| 2037 | bt_dontwrite_msg(buf) |
| 2038 | buf_T *buf; |
| 2039 | { |
| 2040 | if (bt_dontwrite(buf)) |
| 2041 | { |
| 2042 | EMSG(_("E382: Cannot write, 'buftype' option is set")); |
| 2043 | return TRUE; |
| 2044 | } |
| 2045 | return FALSE; |
| 2046 | } |
| 2047 | |
| 2048 | /* |
| 2049 | * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide" |
| 2050 | * and 'bufhidden'. |
| 2051 | */ |
| 2052 | int |
| 2053 | buf_hide(buf) |
| 2054 | buf_T *buf; |
| 2055 | { |
| 2056 | /* 'bufhidden' overrules 'hidden' and ":hide", check it first */ |
| 2057 | switch (buf->b_p_bh[0]) |
| 2058 | { |
| 2059 | case 'u': /* "unload" */ |
| 2060 | case 'w': /* "wipe" */ |
| 2061 | case 'd': return FALSE; /* "delete" */ |
| 2062 | case 'h': return TRUE; /* "hide" */ |
| 2063 | } |
| 2064 | return (p_hid || cmdmod.hide); |
| 2065 | } |
| 2066 | |
| 2067 | /* |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2068 | * Return TRUE when using ":vimgrep" for ":grep". |
| 2069 | */ |
| 2070 | int |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2071 | grep_internal(cmdidx) |
| 2072 | cmdidx_T cmdidx; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2073 | { |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2074 | return ((cmdidx == CMD_grep || cmdidx == CMD_grepadd) |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2075 | && STRCMP("internal", |
| 2076 | *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0); |
| 2077 | } |
| 2078 | |
| 2079 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2080 | * Used for ":make", ":grep" and ":grepadd". |
| 2081 | */ |
| 2082 | void |
| 2083 | ex_make(eap) |
| 2084 | exarg_T *eap; |
| 2085 | { |
| 2086 | char_u *name; |
| 2087 | char_u *cmd; |
| 2088 | unsigned len; |
| 2089 | |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2090 | /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */ |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2091 | if (grep_internal(eap->cmdidx)) |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2092 | { |
| 2093 | ex_vimgrep(eap); |
| 2094 | return; |
| 2095 | } |
| 2096 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2097 | autowrite_all(); |
| 2098 | name = get_mef_name(); |
| 2099 | if (name == NULL) |
| 2100 | return; |
| 2101 | mch_remove(name); /* in case it's not unique */ |
| 2102 | |
| 2103 | /* |
| 2104 | * If 'shellpipe' empty: don't redirect to 'errorfile'. |
| 2105 | */ |
| 2106 | len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1; |
| 2107 | if (*p_sp != NUL) |
| 2108 | len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(name) + 3; |
| 2109 | cmd = alloc(len); |
| 2110 | if (cmd == NULL) |
| 2111 | return; |
| 2112 | sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg, |
| 2113 | (char *)p_shq); |
| 2114 | if (*p_sp != NUL) |
| 2115 | append_redir(cmd, p_sp, name); |
| 2116 | /* |
| 2117 | * Output a newline if there's something else than the :make command that |
| 2118 | * was typed (in which case the cursor is in column 0). |
| 2119 | */ |
| 2120 | if (msg_col == 0) |
| 2121 | msg_didout = FALSE; |
| 2122 | msg_start(); |
| 2123 | MSG_PUTS(":!"); |
| 2124 | msg_outtrans(cmd); /* show what we are doing */ |
| 2125 | |
| 2126 | /* let the shell know if we are redirecting output or not */ |
| 2127 | do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0); |
| 2128 | |
| 2129 | #ifdef AMIGA |
| 2130 | out_flush(); |
| 2131 | /* read window status report and redraw before message */ |
| 2132 | (void)char_avail(); |
| 2133 | #endif |
| 2134 | |
| 2135 | if (qf_init(name, eap->cmdidx != CMD_make ? p_gefm : p_efm, |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2136 | eap->cmdidx != CMD_grepadd) > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2137 | && !eap->forceit) |
| 2138 | qf_jump(0, 0, FALSE); /* display first error */ |
| 2139 | |
| 2140 | mch_remove(name); |
| 2141 | vim_free(name); |
| 2142 | vim_free(cmd); |
| 2143 | } |
| 2144 | |
| 2145 | /* |
| 2146 | * Return the name for the errorfile, in allocated memory. |
| 2147 | * Find a new unique name when 'makeef' contains "##". |
| 2148 | * Returns NULL for error. |
| 2149 | */ |
| 2150 | static char_u * |
| 2151 | get_mef_name() |
| 2152 | { |
| 2153 | char_u *p; |
| 2154 | char_u *name; |
| 2155 | static int start = -1; |
| 2156 | static int off = 0; |
| 2157 | #ifdef HAVE_LSTAT |
| 2158 | struct stat sb; |
| 2159 | #endif |
| 2160 | |
| 2161 | if (*p_mef == NUL) |
| 2162 | { |
| 2163 | name = vim_tempname('e'); |
| 2164 | if (name == NULL) |
| 2165 | EMSG(_(e_notmp)); |
| 2166 | return name; |
| 2167 | } |
| 2168 | |
| 2169 | for (p = p_mef; *p; ++p) |
| 2170 | if (p[0] == '#' && p[1] == '#') |
| 2171 | break; |
| 2172 | |
| 2173 | if (*p == NUL) |
| 2174 | return vim_strsave(p_mef); |
| 2175 | |
| 2176 | /* Keep trying until the name doesn't exist yet. */ |
| 2177 | for (;;) |
| 2178 | { |
| 2179 | if (start == -1) |
| 2180 | start = mch_get_pid(); |
| 2181 | else |
| 2182 | off += 19; |
| 2183 | |
| 2184 | name = alloc((unsigned)STRLEN(p_mef) + 30); |
| 2185 | if (name == NULL) |
| 2186 | break; |
| 2187 | STRCPY(name, p_mef); |
| 2188 | sprintf((char *)name + (p - p_mef), "%d%d", start, off); |
| 2189 | STRCAT(name, p + 2); |
| 2190 | if (mch_getperm(name) < 0 |
| 2191 | #ifdef HAVE_LSTAT |
| 2192 | /* Don't accept a symbolic link, its a security risk. */ |
| 2193 | && mch_lstat((char *)name, &sb) < 0 |
| 2194 | #endif |
| 2195 | ) |
| 2196 | break; |
| 2197 | vim_free(name); |
| 2198 | } |
| 2199 | return name; |
| 2200 | } |
| 2201 | |
| 2202 | /* |
| 2203 | * ":cc", ":crewind", ":cfirst" and ":clast". |
| 2204 | */ |
| 2205 | void |
| 2206 | ex_cc(eap) |
| 2207 | exarg_T *eap; |
| 2208 | { |
| 2209 | qf_jump(0, |
| 2210 | eap->addr_count > 0 |
| 2211 | ? (int)eap->line2 |
| 2212 | : eap->cmdidx == CMD_cc |
| 2213 | ? 0 |
| 2214 | : (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_cfirst) |
| 2215 | ? 1 |
| 2216 | : 32767, |
| 2217 | eap->forceit); |
| 2218 | } |
| 2219 | |
| 2220 | /* |
| 2221 | * ":cnext", ":cnfile", ":cNext" and ":cprevious". |
| 2222 | */ |
| 2223 | void |
| 2224 | ex_cnext(eap) |
| 2225 | exarg_T *eap; |
| 2226 | { |
| 2227 | qf_jump(eap->cmdidx == CMD_cnext |
| 2228 | ? FORWARD |
| 2229 | : eap->cmdidx == CMD_cnfile |
| 2230 | ? FORWARD_FILE |
| 2231 | : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_cNfile) |
| 2232 | ? BACKWARD_FILE |
| 2233 | : BACKWARD, |
| 2234 | eap->addr_count > 0 ? (int)eap->line2 : 1, eap->forceit); |
| 2235 | } |
| 2236 | |
| 2237 | /* |
| 2238 | * ":cfile" command. |
| 2239 | */ |
| 2240 | void |
| 2241 | ex_cfile(eap) |
| 2242 | exarg_T *eap; |
| 2243 | { |
| 2244 | if (*eap->arg != NUL) |
| 2245 | set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE); |
| 2246 | if (qf_init(p_ef, p_efm, TRUE) > 0 && eap->cmdidx == CMD_cfile) |
| 2247 | qf_jump(0, 0, eap->forceit); /* display first error */ |
| 2248 | } |
| 2249 | |
| 2250 | /* |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2251 | * ":vimgrep {pattern} file(s)" |
| 2252 | */ |
| 2253 | void |
| 2254 | ex_vimgrep(eap) |
| 2255 | exarg_T *eap; |
| 2256 | { |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2257 | regmmatch_T regmatch; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2258 | char_u *save_cpo; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 2259 | int fcount; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2260 | char_u **fnames; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 2261 | char_u *s; |
| 2262 | char_u *p; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2263 | int i; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 2264 | int fi; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2265 | struct qf_line *prevp = NULL; |
| 2266 | long lnum; |
| 2267 | garray_T ga; |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2268 | buf_T *buf; |
| 2269 | int duplicate_name = FALSE; |
| 2270 | int using_dummy; |
| 2271 | int found_match; |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2272 | buf_T *first_match_buf = NULL; |
| 2273 | time_t seconds = 0; |
| 2274 | #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) |
| 2275 | char_u *save_ei = NULL; |
| 2276 | aco_save_T aco; |
| 2277 | #endif |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2278 | |
| 2279 | /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ |
| 2280 | save_cpo = p_cpo; |
| 2281 | p_cpo = empty_option; |
| 2282 | |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2283 | /* Get the search pattern: either white-separated or enclosed in // */ |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2284 | regmatch.regprog = NULL; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 2285 | p = skip_vimgrep_pat(eap->arg, &s); |
| 2286 | if (p == NULL) |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2287 | { |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 2288 | EMSG(_("E682: Invalid search pattern or delimiter")); |
| 2289 | goto theend; |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2290 | } |
| 2291 | if (*p != NUL) |
| 2292 | *p++ = NUL; |
| 2293 | regmatch.regprog = vim_regcomp(s, RE_MAGIC); |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2294 | if (regmatch.regprog == NULL) |
| 2295 | goto theend; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2296 | regmatch.rmm_ic = p_ic; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2297 | |
| 2298 | p = skipwhite(p); |
| 2299 | if (*p == NUL) |
| 2300 | { |
| 2301 | EMSG(_("E683: File name missing or invalid pattern")); |
| 2302 | goto theend; |
| 2303 | } |
| 2304 | |
| 2305 | if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_vimgrepadd) |
| 2306 | || qf_curlist == qf_listcount) |
| 2307 | /* make place for a new list */ |
| 2308 | qf_new_list(); |
| 2309 | else if (qf_lists[qf_curlist].qf_count > 0) |
| 2310 | /* Adding to existing list, find last entry. */ |
| 2311 | for (prevp = qf_lists[qf_curlist].qf_start; |
| 2312 | prevp->qf_next != prevp; prevp = prevp->qf_next) |
| 2313 | ; |
| 2314 | |
| 2315 | /* parse the list of arguments */ |
| 2316 | if (get_arglist(&ga, p) == FAIL) |
| 2317 | goto theend; |
| 2318 | i = gen_expand_wildcards(ga.ga_len, (char_u **)ga.ga_data, |
| 2319 | &fcount, &fnames, EW_FILE|EW_NOTFOUND); |
| 2320 | ga_clear(&ga); |
| 2321 | if (i == FAIL) |
| 2322 | goto theend; |
| 2323 | if (fcount == 0) |
| 2324 | { |
| 2325 | EMSG(_(e_nomatch)); |
| 2326 | goto theend; |
| 2327 | } |
| 2328 | |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2329 | seconds = (time_t)0; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2330 | for (fi = 0; fi < fcount && !got_int; ++fi) |
| 2331 | { |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2332 | if (time(NULL) > seconds) |
| 2333 | { |
| 2334 | /* Display the file name every second or so. */ |
| 2335 | seconds = time(NULL); |
| 2336 | msg_start(); |
| 2337 | p = msg_strtrunc(fnames[fi]); |
| 2338 | if (p == NULL) |
| 2339 | msg_outtrans(fnames[fi]); |
| 2340 | else |
| 2341 | { |
| 2342 | msg_outtrans(p); |
| 2343 | vim_free(p); |
| 2344 | } |
| 2345 | msg_clr_eos(); |
| 2346 | msg_didout = FALSE; /* overwrite this message */ |
| 2347 | msg_nowait = TRUE; /* don't wait for this message */ |
| 2348 | msg_col = 0; |
| 2349 | out_flush(); |
| 2350 | } |
| 2351 | |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2352 | buf = buflist_findname_exp(fnames[fi]); |
| 2353 | if (buf == NULL || buf->b_ml.ml_mfp == NULL) |
| 2354 | { |
| 2355 | /* Remember that a buffer with this name already exists. */ |
| 2356 | duplicate_name = (buf != NULL); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2357 | using_dummy = TRUE; |
| 2358 | |
| 2359 | #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) |
| 2360 | /* Don't do Filetype autocommands to avoid loading syntax and |
| 2361 | * indent scripts, a great speed improvement. */ |
| 2362 | save_ei = au_event_disable(",Filetype"); |
| 2363 | #endif |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2364 | |
| 2365 | /* Load file into a buffer, so that 'fileencoding' is detected, |
| 2366 | * autocommands applied, etc. */ |
| 2367 | buf = load_dummy_buffer(fnames[fi]); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2368 | |
| 2369 | #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) |
| 2370 | au_event_restore(save_ei); |
| 2371 | #endif |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2372 | } |
| 2373 | else |
| 2374 | /* Use existing, loaded buffer. */ |
| 2375 | using_dummy = FALSE; |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2376 | |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2377 | if (buf == NULL) |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2378 | { |
| 2379 | if (!got_int) |
| 2380 | smsg((char_u *)_("Cannot open file \"%s\""), fnames[fi]); |
| 2381 | } |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2382 | else |
| 2383 | { |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2384 | found_match = FALSE; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 2385 | #ifdef HAVE_SETJMP_H |
| 2386 | /* |
| 2387 | * Matching with a regexp may cause a very deep recursive call of |
| 2388 | * regmatch(). Vim will crash when running out of stack space. |
| 2389 | * Catch this here if the system supports it. |
| 2390 | * It's a bit slow, thus do it outside of the loop. |
| 2391 | */ |
| 2392 | mch_startjmp(); |
| 2393 | if (SETJMP(lc_jump_env) != 0) |
| 2394 | { |
| 2395 | mch_didjmp(); |
| 2396 | # ifdef SIGHASARG |
| 2397 | if (lc_signal != SIGINT) |
| 2398 | # endif |
| 2399 | EMSG(_(e_complex)); |
| 2400 | got_int = TRUE; |
| 2401 | goto jumpend; |
| 2402 | } |
| 2403 | #endif |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2404 | for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum) |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2405 | { |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2406 | if (vim_regexec_multi(®match, curwin, buf, lnum, |
| 2407 | (colnr_T)0) > 0) |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2408 | { |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2409 | if (qf_add_entry(&prevp, |
| 2410 | NULL, /* dir */ |
| 2411 | fnames[fi], |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2412 | ml_get_buf(buf, |
| 2413 | regmatch.startpos[0].lnum + lnum, FALSE), |
| 2414 | regmatch.startpos[0].lnum + lnum, |
| 2415 | regmatch.startpos[0].col + 1, |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2416 | FALSE, /* virt_col */ |
| 2417 | 0, /* nr */ |
| 2418 | 0, /* type */ |
| 2419 | TRUE /* valid */ |
| 2420 | ) == FAIL) |
| 2421 | { |
| 2422 | got_int = TRUE; |
| 2423 | break; |
| 2424 | } |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2425 | else |
| 2426 | found_match = TRUE; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2427 | } |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2428 | line_breakcheck(); |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2429 | if (got_int) |
| 2430 | break; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2431 | } |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 2432 | #ifdef HAVE_SETJMP_H |
| 2433 | jumpend: |
| 2434 | mch_endjmp(); |
| 2435 | #endif |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2436 | |
| 2437 | if (using_dummy) |
| 2438 | { |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2439 | if (found_match && first_match_buf == NULL) |
| 2440 | first_match_buf = buf; |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2441 | if (duplicate_name) |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2442 | { |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2443 | /* Never keep a dummy buffer if there is another buffer |
| 2444 | * with the same name. */ |
| 2445 | wipe_dummy_buffer(buf); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2446 | buf = NULL; |
| 2447 | } |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2448 | else if (!buf_hide(buf)) |
| 2449 | { |
| 2450 | /* When not hiding the buffer and no match was found we |
| 2451 | * don't need to remember the buffer, wipe it out. If |
| 2452 | * there was a match and it wasn't the first one: only |
| 2453 | * unload the buffer. */ |
| 2454 | if (!found_match) |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2455 | { |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2456 | wipe_dummy_buffer(buf); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2457 | buf = NULL; |
| 2458 | } |
| 2459 | else if (buf != first_match_buf) |
| 2460 | { |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2461 | unload_dummy_buffer(buf); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2462 | buf = NULL; |
| 2463 | } |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2464 | } |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2465 | |
| 2466 | #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) |
| 2467 | if (buf != NULL) |
| 2468 | { |
| 2469 | /* The buffer is still loaded, the Filetype autocommands |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 2470 | * need to be done now, in that buffer. And then the |
| 2471 | * modelines (again). */ |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2472 | aucmd_prepbuf(&aco, buf); |
| 2473 | apply_autocmds(EVENT_FILETYPE, buf->b_p_ft, |
| 2474 | buf->b_fname, TRUE, buf); |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 2475 | do_modelines(FALSE); |
Bram Moolenaar | dcaf10e | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2476 | aucmd_restbuf(&aco); |
| 2477 | } |
| 2478 | #endif |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2479 | } |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | FreeWild(fcount, fnames); |
| 2484 | |
| 2485 | qf_lists[qf_curlist].qf_nonevalid = FALSE; |
| 2486 | qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start; |
| 2487 | qf_lists[qf_curlist].qf_index = 1; |
| 2488 | |
| 2489 | #ifdef FEAT_WINDOWS |
| 2490 | qf_update_buffer(); |
| 2491 | #endif |
| 2492 | |
| 2493 | /* Jump to first match. */ |
| 2494 | if (qf_lists[qf_curlist].qf_count > 0) |
| 2495 | qf_jump(0, 0, FALSE); |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2496 | else |
| 2497 | EMSG2(_(e_nomatch2), s); |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2498 | |
| 2499 | theend: |
| 2500 | vim_free(regmatch.regprog); |
| 2501 | |
| 2502 | /* Only resture 'cpo' when it wasn't set in the mean time. */ |
| 2503 | if (p_cpo == empty_option) |
| 2504 | p_cpo = save_cpo; |
| 2505 | else |
| 2506 | free_string_option(save_cpo); |
| 2507 | } |
| 2508 | |
| 2509 | /* |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 2510 | * Skip over the pattern argument of ":vimgrep /pat/". |
| 2511 | * Put the start of the pattern in "*s", unless "s" is NULL. |
| 2512 | * Return a pointer to the char just past the pattern. |
| 2513 | */ |
| 2514 | char_u * |
| 2515 | skip_vimgrep_pat(p, s) |
| 2516 | char_u *p; |
| 2517 | char_u **s; |
| 2518 | { |
| 2519 | int c; |
| 2520 | |
| 2521 | if (vim_isIDc(*p)) |
| 2522 | { |
| 2523 | if (s != NULL) |
| 2524 | *s = p; |
| 2525 | return skiptowhite(p); |
| 2526 | } |
| 2527 | if (s != NULL) |
| 2528 | *s = p + 1; |
| 2529 | c = *p; |
| 2530 | p = skip_regexp(p + 1, c, TRUE, NULL); |
| 2531 | if (*p != c) |
| 2532 | return NULL; |
| 2533 | return p; |
| 2534 | } |
| 2535 | |
| 2536 | /* |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2537 | * Load file "fname" into a dummy buffer and return the buffer pointer. |
| 2538 | * Returns NULL if it fails. |
| 2539 | * Must call unload_dummy_buffer() or wipe_dummy_buffer() later! |
| 2540 | */ |
| 2541 | static buf_T * |
| 2542 | load_dummy_buffer(fname) |
| 2543 | char_u *fname; |
| 2544 | { |
| 2545 | buf_T *newbuf; |
| 2546 | int failed = TRUE; |
| 2547 | #ifdef FEAT_AUTOCMD |
| 2548 | aco_save_T aco; |
| 2549 | #else |
| 2550 | buf_T *old_curbuf = curbuf; |
| 2551 | #endif |
| 2552 | |
| 2553 | /* Allocate a buffer without putting it in the buffer list. */ |
| 2554 | newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); |
| 2555 | if (newbuf == NULL) |
| 2556 | return NULL; |
| 2557 | |
| 2558 | #ifdef FEAT_AUTOCMD |
| 2559 | /* set curwin/curbuf to buf and save a few things */ |
| 2560 | aucmd_prepbuf(&aco, newbuf); |
| 2561 | #else |
| 2562 | curbuf = newbuf; |
| 2563 | curwin->w_buffer = newbuf; |
| 2564 | #endif |
| 2565 | |
| 2566 | /* Need to set the filename for autocommands. */ |
| 2567 | (void)setfname(curbuf, fname, NULL, FALSE); |
| 2568 | |
| 2569 | if (ml_open() == OK) |
| 2570 | { |
| 2571 | /* Create swap file now to avoid the ATTENTION message. */ |
| 2572 | check_need_swap(TRUE); |
| 2573 | |
| 2574 | /* Remove the "dummy" flag, otherwise autocommands may not |
| 2575 | * work. */ |
| 2576 | curbuf->b_flags &= ~BF_DUMMY; |
| 2577 | |
| 2578 | if (readfile(fname, NULL, |
| 2579 | (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, |
| 2580 | NULL, READ_NEW | READ_DUMMY) == OK |
| 2581 | && !(curbuf->b_flags & BF_NEW)) |
| 2582 | { |
| 2583 | failed = FALSE; |
| 2584 | if (curbuf != newbuf) |
| 2585 | { |
| 2586 | /* Bloody autocommands changed the buffer! */ |
| 2587 | if (buf_valid(newbuf)) |
| 2588 | wipe_buffer(newbuf, FALSE); |
| 2589 | newbuf = curbuf; |
| 2590 | } |
| 2591 | } |
| 2592 | } |
| 2593 | |
| 2594 | #ifdef FEAT_AUTOCMD |
| 2595 | /* restore curwin/curbuf and a few other things */ |
| 2596 | aucmd_restbuf(&aco); |
| 2597 | #else |
| 2598 | curbuf = old_curbuf; |
| 2599 | curwin->w_buffer = old_curbuf; |
| 2600 | #endif |
| 2601 | |
| 2602 | if (!buf_valid(newbuf)) |
| 2603 | return NULL; |
| 2604 | if (failed) |
| 2605 | { |
| 2606 | wipe_dummy_buffer(newbuf); |
| 2607 | return NULL; |
| 2608 | } |
| 2609 | return newbuf; |
| 2610 | } |
| 2611 | |
| 2612 | /* |
| 2613 | * Wipe out the dummy buffer that load_dummy_buffer() created. |
| 2614 | */ |
| 2615 | static void |
| 2616 | wipe_dummy_buffer(buf) |
| 2617 | buf_T *buf; |
| 2618 | { |
| 2619 | if (curbuf != buf) /* safety check */ |
| 2620 | wipe_buffer(buf, FALSE); |
| 2621 | } |
| 2622 | |
| 2623 | /* |
| 2624 | * Unload the dummy buffer that load_dummy_buffer() created. |
| 2625 | */ |
| 2626 | static void |
| 2627 | unload_dummy_buffer(buf) |
| 2628 | buf_T *buf; |
| 2629 | { |
| 2630 | if (curbuf != buf) /* safety check */ |
| 2631 | close_buffer(NULL, buf, DOBUF_UNLOAD); |
| 2632 | } |
| 2633 | |
| 2634 | /* |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2635 | * ":[range]cbuffer [bufnr]" command. |
| 2636 | */ |
| 2637 | void |
| 2638 | ex_cbuffer(eap) |
| 2639 | exarg_T *eap; |
| 2640 | { |
| 2641 | buf_T *buf = NULL; |
| 2642 | |
| 2643 | if (*eap->arg == NUL) |
| 2644 | buf = curbuf; |
| 2645 | else if (*skipwhite(skipdigits(eap->arg)) == NUL) |
| 2646 | buf = buflist_findnr(atoi((char *)eap->arg)); |
| 2647 | if (buf == NULL) |
| 2648 | EMSG(_(e_invarg)); |
| 2649 | else if (buf->b_ml.ml_mfp == NULL) |
| 2650 | EMSG(_("E681: Buffer is not loaded")); |
| 2651 | else |
| 2652 | { |
| 2653 | if (eap->addr_count == 0) |
| 2654 | { |
| 2655 | eap->line1 = 1; |
| 2656 | eap->line2 = buf->b_ml.ml_line_count; |
| 2657 | } |
| 2658 | if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count |
| 2659 | || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count) |
| 2660 | EMSG(_(e_invrange)); |
| 2661 | else |
| 2662 | qf_init_ext(NULL, buf, p_efm, TRUE, eap->line1, eap->line2); |
| 2663 | } |
| 2664 | } |
| 2665 | |
| 2666 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2667 | * ":helpgrep {pattern}" |
| 2668 | */ |
| 2669 | void |
| 2670 | ex_helpgrep(eap) |
| 2671 | exarg_T *eap; |
| 2672 | { |
| 2673 | regmatch_T regmatch; |
| 2674 | char_u *save_cpo; |
| 2675 | char_u *p; |
| 2676 | int fcount; |
| 2677 | char_u **fnames; |
| 2678 | FILE *fd; |
| 2679 | int fi; |
| 2680 | struct qf_line *prevp = NULL; |
| 2681 | long lnum; |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 2682 | #ifdef FEAT_MULTI_LANG |
| 2683 | char_u *lang; |
| 2684 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2685 | |
| 2686 | /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ |
| 2687 | save_cpo = p_cpo; |
| 2688 | p_cpo = (char_u *)""; |
| 2689 | |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 2690 | #ifdef FEAT_MULTI_LANG |
| 2691 | /* Check for a specified language */ |
| 2692 | lang = check_help_lang(eap->arg); |
| 2693 | #endif |
| 2694 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2695 | regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING); |
| 2696 | regmatch.rm_ic = FALSE; |
| 2697 | if (regmatch.regprog != NULL) |
| 2698 | { |
| 2699 | /* create a new quickfix list */ |
| 2700 | qf_new_list(); |
| 2701 | |
| 2702 | /* Go through all directories in 'runtimepath' */ |
| 2703 | p = p_rtp; |
| 2704 | while (*p != NUL && !got_int) |
| 2705 | { |
| 2706 | copy_option_part(&p, NameBuff, MAXPATHL, ","); |
| 2707 | |
| 2708 | /* Find all "*.txt" and "*.??x" files in the "doc" directory. */ |
| 2709 | add_pathsep(NameBuff); |
| 2710 | STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)"); |
| 2711 | if (gen_expand_wildcards(1, &NameBuff, &fcount, |
| 2712 | &fnames, EW_FILE|EW_SILENT) == OK |
| 2713 | && fcount > 0) |
| 2714 | { |
| 2715 | for (fi = 0; fi < fcount && !got_int; ++fi) |
| 2716 | { |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 2717 | #ifdef FEAT_MULTI_LANG |
| 2718 | /* Skip files for a different language. */ |
| 2719 | if (lang != NULL |
| 2720 | && STRNICMP(lang, fnames[fi] |
| 2721 | + STRLEN(fnames[fi]) - 3, 2) != 0 |
| 2722 | && !(STRNICMP(lang, "en", 2) == 0 |
| 2723 | && STRNICMP("txt", fnames[fi] |
| 2724 | + STRLEN(fnames[fi]) - 3, 3) == 0)) |
| 2725 | continue; |
| 2726 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2727 | fd = fopen((char *)fnames[fi], "r"); |
| 2728 | if (fd != NULL) |
| 2729 | { |
| 2730 | lnum = 1; |
| 2731 | while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) |
| 2732 | { |
| 2733 | if (vim_regexec(®match, IObuff, (colnr_T)0)) |
| 2734 | { |
| 2735 | int l = STRLEN(IObuff); |
| 2736 | |
| 2737 | /* remove trailing CR, LF, spaces, etc. */ |
| 2738 | while (l > 0 && IObuff[l - 1] <= ' ') |
| 2739 | IObuff[--l] = NUL; |
| 2740 | |
| 2741 | if (qf_add_entry(&prevp, |
| 2742 | NULL, /* dir */ |
| 2743 | fnames[fi], |
| 2744 | IObuff, |
| 2745 | lnum, |
Bram Moolenaar | 8169525 | 2004-12-29 20:58:21 +0000 | [diff] [blame] | 2746 | (int)(regmatch.startp[0] - IObuff) |
| 2747 | + 1, /* col */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2748 | FALSE, /* virt_col */ |
| 2749 | 0, /* nr */ |
| 2750 | 1, /* type */ |
| 2751 | TRUE /* valid */ |
| 2752 | ) == FAIL) |
| 2753 | { |
| 2754 | got_int = TRUE; |
| 2755 | break; |
| 2756 | } |
| 2757 | } |
| 2758 | ++lnum; |
| 2759 | line_breakcheck(); |
| 2760 | } |
| 2761 | fclose(fd); |
| 2762 | } |
| 2763 | } |
| 2764 | FreeWild(fcount, fnames); |
| 2765 | } |
| 2766 | } |
| 2767 | vim_free(regmatch.regprog); |
| 2768 | |
| 2769 | qf_lists[qf_curlist].qf_nonevalid = FALSE; |
| 2770 | qf_lists[qf_curlist].qf_ptr = qf_lists[qf_curlist].qf_start; |
| 2771 | qf_lists[qf_curlist].qf_index = 1; |
| 2772 | } |
| 2773 | |
| 2774 | p_cpo = save_cpo; |
| 2775 | |
| 2776 | #ifdef FEAT_WINDOWS |
| 2777 | qf_update_buffer(); |
| 2778 | #endif |
| 2779 | |
| 2780 | /* Jump to first match. */ |
| 2781 | if (qf_lists[qf_curlist].qf_count > 0) |
| 2782 | qf_jump(0, 0, FALSE); |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 2783 | else |
| 2784 | EMSG2(_(e_nomatch2), eap->arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | #endif /* FEAT_QUICKFIX */ |