PageRenderTime 69ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/vim7.0.0/src/quickfix.c

#
C | 2663 lines | 1981 code | 220 blank | 462 comment | 745 complexity | cfa0585adfb191b574ac9b3e2fcec494 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  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. * quickfix.c: functions for quickfix mode, using a file with error messages
  11. */
  12. #include "vim.h"
  13. #if defined(FEAT_QUICKFIX) || defined(PROTO)
  14. struct dir_stack_T
  15. {
  16. struct dir_stack_T *next;
  17. char_u *dirname;
  18. };
  19. static struct dir_stack_T *dir_stack = NULL;
  20. /*
  21. * For each error the next struct is allocated and linked in a list.
  22. */
  23. typedef struct qfline_S qfline_T;
  24. struct qfline_S
  25. {
  26. qfline_T *qf_next; /* pointer to next error in the list */
  27. qfline_T *qf_prev; /* pointer to previous error in the list */
  28. linenr_T qf_lnum; /* line number where the error occurred */
  29. int qf_fnum; /* file number for the line */
  30. int qf_col; /* column where the error occurred */
  31. int qf_nr; /* error number */
  32. char_u *qf_pattern; /* search pattern for the error */
  33. char_u *qf_text; /* description of the error */
  34. char_u qf_viscol; /* set to TRUE if qf_col is screen column */
  35. char_u qf_cleared; /* set to TRUE if line has been deleted */
  36. char_u qf_type; /* type of the error (mostly 'E'); 1 for
  37. :helpgrep */
  38. char_u qf_valid; /* valid error message detected */
  39. };
  40. /*
  41. * There is a stack of error lists.
  42. */
  43. #define LISTCOUNT 10
  44. typedef struct qf_list_S
  45. {
  46. qfline_T *qf_start; /* pointer to the first error */
  47. qfline_T *qf_ptr; /* pointer to the current error */
  48. int qf_count; /* number of errors (0 means no error list) */
  49. int qf_index; /* current index in the error list */
  50. int qf_nonevalid; /* TRUE if not a single valid entry found */
  51. } qf_list_T;
  52. struct qf_info_S
  53. {
  54. /*
  55. * Count of references to this list. Used only for location lists.
  56. * When a location list window reference this list, qf_refcount
  57. * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount
  58. * reaches 0, the list is freed.
  59. */
  60. int qf_refcount;
  61. int qf_listcount; /* current number of lists */
  62. int qf_curlist; /* current error list */
  63. qf_list_T qf_lists[LISTCOUNT];
  64. };
  65. static qf_info_T ql_info; /* global quickfix list */
  66. #define FMT_PATTERNS 10 /* maximum number of % recognized */
  67. /*
  68. * Structure used to hold the info of one part of 'errorformat'
  69. */
  70. typedef struct efm_S efm_T;
  71. struct efm_S
  72. {
  73. regprog_T *prog; /* pre-formatted part of 'errorformat' */
  74. efm_T *next; /* pointer to next (NULL if last) */
  75. char_u addr[FMT_PATTERNS]; /* indices of used % patterns */
  76. char_u prefix; /* prefix of this format line: */
  77. /* 'D' enter directory */
  78. /* 'X' leave directory */
  79. /* 'A' start of multi-line message */
  80. /* 'E' error message */
  81. /* 'W' warning message */
  82. /* 'I' informational message */
  83. /* 'C' continuation line */
  84. /* 'Z' end of multi-line message */
  85. /* 'G' general, unspecific message */
  86. /* 'P' push file (partial) message */
  87. /* 'Q' pop/quit file (partial) message */
  88. /* 'O' overread (partial) message */
  89. char_u flags; /* additional flags given in prefix */
  90. /* '-' do not include this line */
  91. /* '+' include whole line in message */
  92. int conthere; /* %> used */
  93. };
  94. static int qf_init_ext __ARGS((qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast));
  95. static void qf_new_list __ARGS((qf_info_T *qi));
  96. static int qf_add_entry __ARGS((qf_info_T *qi, qfline_T **prevp, char_u *dir, char_u *fname, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid));
  97. static void qf_msg __ARGS((qf_info_T *qi));
  98. static void qf_free __ARGS((qf_info_T *qi, int idx));
  99. static char_u *qf_types __ARGS((int, int));
  100. static int qf_get_fnum __ARGS((char_u *, char_u *));
  101. static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
  102. static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
  103. static char_u *qf_guess_filepath __ARGS((char_u *));
  104. static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
  105. static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
  106. #ifdef FEAT_WINDOWS
  107. static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
  108. static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
  109. static win_T *qf_find_win __ARGS((qf_info_T *qi));
  110. static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
  111. static void qf_update_buffer __ARGS((qf_info_T *qi));
  112. static void qf_fill_buffer __ARGS((qf_info_T *qi));
  113. #endif
  114. static char_u *get_mef_name __ARGS((void));
  115. static buf_T *load_dummy_buffer __ARGS((char_u *fname));
  116. static void wipe_dummy_buffer __ARGS((buf_T *buf));
  117. static void unload_dummy_buffer __ARGS((buf_T *buf));
  118. static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
  119. /* Quickfix window check helper macro */
  120. #define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
  121. /* Location list window check helper macro */
  122. #define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
  123. /*
  124. * Return location list for window 'wp'
  125. * For location list window, return the referenced location list
  126. */
  127. #define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
  128. /*
  129. * Read the errorfile "efile" into memory, line by line, building the error
  130. * list.
  131. * Return -1 for error, number of errors for success.
  132. */
  133. int
  134. qf_init(wp, efile, errorformat, newlist)
  135. win_T *wp;
  136. char_u *efile;
  137. char_u *errorformat;
  138. int newlist; /* TRUE: start a new error list */
  139. {
  140. qf_info_T *qi = &ql_info;
  141. if (efile == NULL)
  142. return FAIL;
  143. if (wp != NULL)
  144. {
  145. qi = ll_get_or_alloc_list(wp);
  146. if (qi == NULL)
  147. return FAIL;
  148. }
  149. return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
  150. (linenr_T)0, (linenr_T)0);
  151. }
  152. /*
  153. * Read the errorfile "efile" into memory, line by line, building the error
  154. * list.
  155. * Alternative: when "efile" is null read errors from buffer "buf".
  156. * Always use 'errorformat' from "buf" if there is a local value.
  157. * Then lnumfirst and lnumlast specify the range of lines to use.
  158. * Return -1 for error, number of errors for success.
  159. */
  160. static int
  161. qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast)
  162. qf_info_T *qi;
  163. char_u *efile;
  164. buf_T *buf;
  165. typval_T *tv;
  166. char_u *errorformat;
  167. int newlist; /* TRUE: start a new error list */
  168. linenr_T lnumfirst; /* first line number to use */
  169. linenr_T lnumlast; /* last line number to use */
  170. {
  171. char_u *namebuf;
  172. char_u *errmsg;
  173. char_u *pattern;
  174. char_u *fmtstr = NULL;
  175. int col = 0;
  176. char_u use_viscol = FALSE;
  177. int type = 0;
  178. int valid;
  179. linenr_T buflnum = lnumfirst;
  180. long lnum = 0L;
  181. int enr = 0;
  182. FILE *fd = NULL;
  183. qfline_T *qfprev = NULL; /* init to make SASC shut up */
  184. char_u *efmp;
  185. efm_T *fmt_first = NULL;
  186. efm_T *fmt_last = NULL;
  187. efm_T *fmt_ptr;
  188. efm_T *fmt_start = NULL;
  189. char_u *efm;
  190. char_u *ptr;
  191. char_u *srcptr;
  192. int len;
  193. int i;
  194. int round;
  195. int idx = 0;
  196. int multiline = FALSE;
  197. int multiignore = FALSE;
  198. int multiscan = FALSE;
  199. int retval = -1; /* default: return error flag */
  200. char_u *directory = NULL;
  201. char_u *currfile = NULL;
  202. char_u *tail = NULL;
  203. char_u *p_str = NULL;
  204. listitem_T *p_li = NULL;
  205. struct dir_stack_T *file_stack = NULL;
  206. regmatch_T regmatch;
  207. static struct fmtpattern
  208. {
  209. char_u convchar;
  210. char *pattern;
  211. } fmt_pat[FMT_PATTERNS] =
  212. {
  213. {'f', ".\\+"}, /* only used when at end */
  214. {'n', "\\d\\+"},
  215. {'l', "\\d\\+"},
  216. {'c', "\\d\\+"},
  217. {'t', "."},
  218. {'m', ".\\+"},
  219. {'r', ".*"},
  220. {'p', "[- .]*"},
  221. {'v', "\\d\\+"},
  222. {'s', ".\\+"}
  223. };
  224. namebuf = alloc(CMDBUFFSIZE + 1);
  225. errmsg = alloc(CMDBUFFSIZE + 1);
  226. pattern = alloc(CMDBUFFSIZE + 1);
  227. if (namebuf == NULL || errmsg == NULL || pattern == NULL)
  228. goto qf_init_end;
  229. if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
  230. {
  231. EMSG2(_(e_openerrf), efile);
  232. goto qf_init_end;
  233. }
  234. if (newlist || qi->qf_curlist == qi->qf_listcount)
  235. /* make place for a new list */
  236. qf_new_list(qi);
  237. else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
  238. /* Adding to existing list, find last entry. */
  239. for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
  240. qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
  241. ;
  242. /*
  243. * Each part of the format string is copied and modified from errorformat to
  244. * regex prog. Only a few % characters are allowed.
  245. */
  246. /* Use the local value of 'errorformat' if it's set. */
  247. if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
  248. efm = buf->b_p_efm;
  249. else
  250. efm = errorformat;
  251. /*
  252. * Get some space to modify the format string into.
  253. */
  254. i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
  255. for (round = FMT_PATTERNS; round > 0; )
  256. i += (int)STRLEN(fmt_pat[--round].pattern);
  257. #ifdef COLON_IN_FILENAME
  258. i += 12; /* "%f" can become twelve chars longer */
  259. #else
  260. i += 2; /* "%f" can become two chars longer */
  261. #endif
  262. if ((fmtstr = alloc(i)) == NULL)
  263. goto error2;
  264. while (efm[0] != NUL)
  265. {
  266. /*
  267. * Allocate a new eformat structure and put it at the end of the list
  268. */
  269. fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
  270. if (fmt_ptr == NULL)
  271. goto error2;
  272. if (fmt_first == NULL) /* first one */
  273. fmt_first = fmt_ptr;
  274. else
  275. fmt_last->next = fmt_ptr;
  276. fmt_last = fmt_ptr;
  277. /*
  278. * Isolate one part in the 'errorformat' option
  279. */
  280. for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
  281. if (efm[len] == '\\' && efm[len + 1] != NUL)
  282. ++len;
  283. /*
  284. * Build regexp pattern from current 'errorformat' option
  285. */
  286. ptr = fmtstr;
  287. *ptr++ = '^';
  288. round = 0;
  289. for (efmp = efm; efmp < efm + len; ++efmp)
  290. {
  291. if (*efmp == '%')
  292. {
  293. ++efmp;
  294. for (idx = 0; idx < FMT_PATTERNS; ++idx)
  295. if (fmt_pat[idx].convchar == *efmp)
  296. break;
  297. if (idx < FMT_PATTERNS)
  298. {
  299. if (fmt_ptr->addr[idx])
  300. {
  301. sprintf((char *)errmsg,
  302. _("E372: Too many %%%c in format string"), *efmp);
  303. EMSG(errmsg);
  304. goto error2;
  305. }
  306. if ((idx
  307. && idx < 6
  308. && vim_strchr((char_u *)"DXOPQ",
  309. fmt_ptr->prefix) != NULL)
  310. || (idx == 6
  311. && vim_strchr((char_u *)"OPQ",
  312. fmt_ptr->prefix) == NULL))
  313. {
  314. sprintf((char *)errmsg,
  315. _("E373: Unexpected %%%c in format string"), *efmp);
  316. EMSG(errmsg);
  317. goto error2;
  318. }
  319. fmt_ptr->addr[idx] = (char_u)++round;
  320. *ptr++ = '\\';
  321. *ptr++ = '(';
  322. #ifdef BACKSLASH_IN_FILENAME
  323. if (*efmp == 'f')
  324. {
  325. /* Also match "c:" in the file name, even when
  326. * checking for a colon next: "%f:".
  327. * "\%(\a:\)\=" */
  328. STRCPY(ptr, "\\%(\\a:\\)\\=");
  329. ptr += 10;
  330. }
  331. #endif
  332. if (*efmp == 'f' && efmp[1] != NUL)
  333. {
  334. if (efmp[1] != '\\' && efmp[1] != '%')
  335. {
  336. /* A file name may contain spaces, but this isn't
  337. * in "\f". For "%f:%l:%m" there may be a ":" in
  338. * the file name. Use ".\{-1,}x" instead (x is
  339. * the next character), the requirement that :999:
  340. * follows should work. */
  341. STRCPY(ptr, ".\\{-1,}");
  342. ptr += 7;
  343. }
  344. else
  345. {
  346. /* File name followed by '\\' or '%': include as
  347. * many file name chars as possible. */
  348. STRCPY(ptr, "\\f\\+");
  349. ptr += 4;
  350. }
  351. }
  352. else
  353. {
  354. srcptr = (char_u *)fmt_pat[idx].pattern;
  355. while ((*ptr = *srcptr++) != NUL)
  356. ++ptr;
  357. }
  358. *ptr++ = '\\';
  359. *ptr++ = ')';
  360. }
  361. else if (*efmp == '*')
  362. {
  363. if (*++efmp == '[' || *efmp == '\\')
  364. {
  365. if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
  366. {
  367. if (efmp[1] == '^')
  368. *ptr++ = *++efmp;
  369. if (efmp < efm + len)
  370. {
  371. *ptr++ = *++efmp; /* could be ']' */
  372. while (efmp < efm + len
  373. && (*ptr++ = *++efmp) != ']')
  374. /* skip */;
  375. if (efmp == efm + len)
  376. {
  377. EMSG(_("E374: Missing ] in format string"));
  378. goto error2;
  379. }
  380. }
  381. }
  382. else if (efmp < efm + len) /* %*\D, %*\s etc. */
  383. *ptr++ = *++efmp;
  384. *ptr++ = '\\';
  385. *ptr++ = '+';
  386. }
  387. else
  388. {
  389. /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
  390. sprintf((char *)errmsg,
  391. _("E375: Unsupported %%%c in format string"), *efmp);
  392. EMSG(errmsg);
  393. goto error2;
  394. }
  395. }
  396. else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
  397. *ptr++ = *efmp; /* regexp magic characters */
  398. else if (*efmp == '#')
  399. *ptr++ = '*';
  400. else if (*efmp == '>')
  401. fmt_ptr->conthere = TRUE;
  402. else if (efmp == efm + 1) /* analyse prefix */
  403. {
  404. if (vim_strchr((char_u *)"+-", *efmp) != NULL)
  405. fmt_ptr->flags = *efmp++;
  406. if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
  407. fmt_ptr->prefix = *efmp;
  408. else
  409. {
  410. sprintf((char *)errmsg,
  411. _("E376: Invalid %%%c in format string prefix"), *efmp);
  412. EMSG(errmsg);
  413. goto error2;
  414. }
  415. }
  416. else
  417. {
  418. sprintf((char *)errmsg,
  419. _("E377: Invalid %%%c in format string"), *efmp);
  420. EMSG(errmsg);
  421. goto error2;
  422. }
  423. }
  424. else /* copy normal character */
  425. {
  426. if (*efmp == '\\' && efmp + 1 < efm + len)
  427. ++efmp;
  428. else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
  429. *ptr++ = '\\'; /* escape regexp atoms */
  430. if (*efmp)
  431. *ptr++ = *efmp;
  432. }
  433. }
  434. *ptr++ = '$';
  435. *ptr = NUL;
  436. if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
  437. goto error2;
  438. /*
  439. * Advance to next part
  440. */
  441. efm = skip_to_option_part(efm + len); /* skip comma and spaces */
  442. }
  443. if (fmt_first == NULL) /* nothing found */
  444. {
  445. EMSG(_("E378: 'errorformat' contains no pattern"));
  446. goto error2;
  447. }
  448. /*
  449. * got_int is reset here, because it was probably set when killing the
  450. * ":make" command, but we still want to read the errorfile then.
  451. */
  452. got_int = FALSE;
  453. /* Always ignore case when looking for a matching error. */
  454. regmatch.rm_ic = TRUE;
  455. if (tv != NULL)
  456. {
  457. if (tv->v_type == VAR_STRING)
  458. p_str = tv->vval.v_string;
  459. else if (tv->v_type == VAR_LIST)
  460. p_li = tv->vval.v_list->lv_first;
  461. }
  462. /*
  463. * Read the lines in the error file one by one.
  464. * Try to recognize one of the error formats in each line.
  465. */
  466. while (!got_int)
  467. {
  468. /* Get the next line. */
  469. if (fd == NULL)
  470. {
  471. if (tv != NULL)
  472. {
  473. int len;
  474. if (tv->v_type == VAR_STRING)
  475. {
  476. /* Get the next line from the supplied string */
  477. char_u *p;
  478. if (!*p_str) /* Reached the end of the string */
  479. break;
  480. p = vim_strchr(p_str, '\n');
  481. if (p)
  482. len = (int)(p - p_str + 1);
  483. else
  484. len = (int)STRLEN(p_str);
  485. if (len > CMDBUFFSIZE - 2)
  486. vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
  487. else
  488. vim_strncpy(IObuff, p_str, len);
  489. p_str += len;
  490. }
  491. else if (tv->v_type == VAR_LIST)
  492. {
  493. /* Get the next line from the supplied list */
  494. while (p_li && p_li->li_tv.v_type != VAR_STRING)
  495. p_li = p_li->li_next; /* Skip non-string items */
  496. if (!p_li) /* End of the list */
  497. break;
  498. len = (int)STRLEN(p_li->li_tv.vval.v_string);
  499. if (len > CMDBUFFSIZE - 2)
  500. len = CMDBUFFSIZE - 2;
  501. vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
  502. p_li = p_li->li_next; /* next item */
  503. }
  504. }
  505. else
  506. {
  507. /* Get the next line from the supplied buffer */
  508. if (buflnum > lnumlast)
  509. break;
  510. vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
  511. CMDBUFFSIZE - 2);
  512. }
  513. }
  514. else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
  515. break;
  516. IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
  517. if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
  518. *efmp = NUL;
  519. #ifdef USE_CRNL
  520. if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
  521. *efmp = NUL;
  522. #endif
  523. /* If there was no %> item start at the first pattern */
  524. if (fmt_start == NULL)
  525. fmt_ptr = fmt_first;
  526. else
  527. {
  528. fmt_ptr = fmt_start;
  529. fmt_start = NULL;
  530. }
  531. /*
  532. * Try to match each part of 'errorformat' until we find a complete
  533. * match or no match.
  534. */
  535. valid = TRUE;
  536. restofline:
  537. for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
  538. {
  539. idx = fmt_ptr->prefix;
  540. if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
  541. continue;
  542. namebuf[0] = NUL;
  543. pattern[0] = NUL;
  544. if (!multiscan)
  545. errmsg[0] = NUL;
  546. lnum = 0;
  547. col = 0;
  548. use_viscol = FALSE;
  549. enr = -1;
  550. type = 0;
  551. tail = NULL;
  552. regmatch.regprog = fmt_ptr->prog;
  553. if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
  554. {
  555. if ((idx == 'C' || idx == 'Z') && !multiline)
  556. continue;
  557. if (vim_strchr((char_u *)"EWI", idx) != NULL)
  558. type = idx;
  559. else
  560. type = 0;
  561. /*
  562. * Extract error message data from matched line
  563. */
  564. if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
  565. {
  566. int c = *regmatch.endp[i];
  567. /* Expand ~/file and $HOME/file to full path. */
  568. *regmatch.endp[i] = NUL;
  569. expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
  570. *regmatch.endp[i] = c;
  571. if (vim_strchr((char_u *)"OPQ", idx) != NULL
  572. && mch_getperm(namebuf) == -1)
  573. continue;
  574. }
  575. if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
  576. enr = (int)atol((char *)regmatch.startp[i]);
  577. if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
  578. lnum = atol((char *)regmatch.startp[i]);
  579. if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
  580. col = (int)atol((char *)regmatch.startp[i]);
  581. if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
  582. type = *regmatch.startp[i];
  583. if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
  584. STRCPY(errmsg, IObuff);
  585. else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
  586. {
  587. len = (int)(regmatch.endp[i] - regmatch.startp[i]);
  588. vim_strncpy(errmsg, regmatch.startp[i], len);
  589. }
  590. if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
  591. tail = regmatch.startp[i];
  592. if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
  593. {
  594. col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
  595. if (*((char_u *)regmatch.startp[i]) != TAB)
  596. use_viscol = TRUE;
  597. }
  598. if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
  599. {
  600. col = (int)atol((char *)regmatch.startp[i]);
  601. use_viscol = TRUE;
  602. }
  603. if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
  604. {
  605. len = (int)(regmatch.endp[i] - regmatch.startp[i]);
  606. if (len > CMDBUFFSIZE - 5)
  607. len = CMDBUFFSIZE - 5;
  608. STRCPY(pattern, "^\\V");
  609. STRNCAT(pattern, regmatch.startp[i], len);
  610. pattern[len + 3] = '\\';
  611. pattern[len + 4] = '$';
  612. pattern[len + 5] = NUL;
  613. }
  614. break;
  615. }
  616. }
  617. multiscan = FALSE;
  618. if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
  619. {
  620. if (fmt_ptr != NULL)
  621. {
  622. if (idx == 'D') /* enter directory */
  623. {
  624. if (*namebuf == NUL)
  625. {
  626. EMSG(_("E379: Missing or empty directory name"));
  627. goto error2;
  628. }
  629. if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
  630. goto error2;
  631. }
  632. else if (idx == 'X') /* leave directory */
  633. directory = qf_pop_dir(&dir_stack);
  634. }
  635. namebuf[0] = NUL; /* no match found, remove file name */
  636. lnum = 0; /* don't jump to this line */
  637. valid = FALSE;
  638. STRCPY(errmsg, IObuff); /* copy whole line to error message */
  639. if (fmt_ptr == NULL)
  640. multiline = multiignore = FALSE;
  641. }
  642. else if (fmt_ptr != NULL)
  643. {
  644. /* honor %> item */
  645. if (fmt_ptr->conthere)
  646. fmt_start = fmt_ptr;
  647. if (vim_strchr((char_u *)"AEWI", idx) != NULL)
  648. multiline = TRUE; /* start of a multi-line message */
  649. else if (vim_strchr((char_u *)"CZ", idx) != NULL)
  650. { /* continuation of multi-line msg */
  651. if (qfprev == NULL)
  652. goto error2;
  653. if (*errmsg && !multiignore)
  654. {
  655. len = (int)STRLEN(qfprev->qf_text);
  656. if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
  657. == NULL)
  658. goto error2;
  659. STRCPY(ptr, qfprev->qf_text);
  660. vim_free(qfprev->qf_text);
  661. qfprev->qf_text = ptr;
  662. *(ptr += len) = '\n';
  663. STRCPY(++ptr, errmsg);
  664. }
  665. if (qfprev->qf_nr == -1)
  666. qfprev->qf_nr = enr;
  667. if (vim_isprintc(type) && !qfprev->qf_type)
  668. qfprev->qf_type = type; /* only printable chars allowed */
  669. if (!qfprev->qf_lnum)
  670. qfprev->qf_lnum = lnum;
  671. if (!qfprev->qf_col)
  672. qfprev->qf_col = col;
  673. qfprev->qf_viscol = use_viscol;
  674. if (!qfprev->qf_fnum)
  675. qfprev->qf_fnum = qf_get_fnum(directory,
  676. *namebuf || directory ? namebuf
  677. : currfile && valid ? currfile : 0);
  678. if (idx == 'Z')
  679. multiline = multiignore = FALSE;
  680. line_breakcheck();
  681. continue;
  682. }
  683. else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
  684. {
  685. /* global file names */
  686. valid = FALSE;
  687. if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
  688. {
  689. if (*namebuf && idx == 'P')
  690. currfile = qf_push_dir(namebuf, &file_stack);
  691. else if (idx == 'Q')
  692. currfile = qf_pop_dir(&file_stack);
  693. *namebuf = NUL;
  694. if (tail && *tail)
  695. {
  696. STRCPY(IObuff, skipwhite(tail));
  697. multiscan = TRUE;
  698. goto restofline;
  699. }
  700. }
  701. }
  702. if (fmt_ptr->flags == '-') /* generally exclude this line */
  703. {
  704. if (multiline)
  705. multiignore = TRUE; /* also exclude continuation lines */
  706. continue;
  707. }
  708. }
  709. if (qf_add_entry(qi, &qfprev,
  710. directory,
  711. (*namebuf || directory)
  712. ? namebuf
  713. : ((currfile && valid) ? currfile : (char_u *)NULL),
  714. errmsg,
  715. lnum,
  716. col,
  717. use_viscol,
  718. pattern,
  719. enr,
  720. type,
  721. valid) == FAIL)
  722. goto error2;
  723. line_breakcheck();
  724. }
  725. if (fd == NULL || !ferror(fd))
  726. {
  727. if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
  728. {
  729. /* no valid entry found */
  730. qi->qf_lists[qi->qf_curlist].qf_ptr =
  731. qi->qf_lists[qi->qf_curlist].qf_start;
  732. qi->qf_lists[qi->qf_curlist].qf_index = 1;
  733. qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
  734. }
  735. else
  736. {
  737. qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
  738. if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
  739. qi->qf_lists[qi->qf_curlist].qf_ptr =
  740. qi->qf_lists[qi->qf_curlist].qf_start;
  741. }
  742. /* return number of matches */
  743. retval = qi->qf_lists[qi->qf_curlist].qf_count;
  744. goto qf_init_ok;
  745. }
  746. EMSG(_(e_readerrf));
  747. error2:
  748. qf_free(qi, qi->qf_curlist);
  749. qi->qf_listcount--;
  750. if (qi->qf_curlist > 0)
  751. --qi->qf_curlist;
  752. qf_init_ok:
  753. if (fd != NULL)
  754. fclose(fd);
  755. for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
  756. {
  757. fmt_first = fmt_ptr->next;
  758. vim_free(fmt_ptr->prog);
  759. vim_free(fmt_ptr);
  760. }
  761. qf_clean_dir_stack(&dir_stack);
  762. qf_clean_dir_stack(&file_stack);
  763. qf_init_end:
  764. vim_free(namebuf);
  765. vim_free(errmsg);
  766. vim_free(pattern);
  767. vim_free(fmtstr);
  768. #ifdef FEAT_WINDOWS
  769. qf_update_buffer(qi);
  770. #endif
  771. return retval;
  772. }
  773. /*
  774. * Prepare for adding a new quickfix list.
  775. */
  776. static void
  777. qf_new_list(qi)
  778. qf_info_T *qi;
  779. {
  780. int i;
  781. /*
  782. * If the current entry is not the last entry, delete entries below
  783. * the current entry. This makes it possible to browse in a tree-like
  784. * way with ":grep'.
  785. */
  786. while (qi->qf_listcount > qi->qf_curlist + 1)
  787. qf_free(qi, --qi->qf_listcount);
  788. /*
  789. * When the stack is full, remove to oldest entry
  790. * Otherwise, add a new entry.
  791. */
  792. if (qi->qf_listcount == LISTCOUNT)
  793. {
  794. qf_free(qi, 0);
  795. for (i = 1; i < LISTCOUNT; ++i)
  796. qi->qf_lists[i - 1] = qi->qf_lists[i];
  797. qi->qf_curlist = LISTCOUNT - 1;
  798. }
  799. else
  800. qi->qf_curlist = qi->qf_listcount++;
  801. qi->qf_lists[qi->qf_curlist].qf_index = 0;
  802. qi->qf_lists[qi->qf_curlist].qf_count = 0;
  803. }
  804. /*
  805. * Free a location list
  806. */
  807. static void
  808. ll_free_all(pqi)
  809. qf_info_T **pqi;
  810. {
  811. int i;
  812. qf_info_T *qi;
  813. qi = *pqi;
  814. if (qi == NULL)
  815. return;
  816. *pqi = NULL; /* Remove reference to this list */
  817. qi->qf_refcount--;
  818. if (qi->qf_refcount < 1)
  819. {
  820. /* No references to this location list */
  821. for (i = 0; i < qi->qf_listcount; ++i)
  822. qf_free(qi, i);
  823. vim_free(qi);
  824. }
  825. }
  826. void
  827. qf_free_all(wp)
  828. win_T *wp;
  829. {
  830. int i;
  831. qf_info_T *qi = &ql_info;
  832. if (wp != NULL)
  833. {
  834. /* location list */
  835. ll_free_all(&wp->w_llist);
  836. ll_free_all(&wp->w_llist_ref);
  837. }
  838. else
  839. /* quickfix list */
  840. for (i = 0; i < qi->qf_listcount; ++i)
  841. qf_free(qi, i);
  842. }
  843. /*
  844. * Add an entry to the end of the list of errors.
  845. * Returns OK or FAIL.
  846. */
  847. static int
  848. qf_add_entry(qi, prevp, dir, fname, mesg, lnum, col, vis_col, pattern, nr, type,
  849. valid)
  850. qf_info_T *qi; /* quickfix list */
  851. qfline_T **prevp; /* pointer to previously added entry or NULL */
  852. char_u *dir; /* optional directory name */
  853. char_u *fname; /* file name or NULL */
  854. char_u *mesg; /* message */
  855. long lnum; /* line number */
  856. int col; /* column */
  857. int vis_col; /* using visual column */
  858. char_u *pattern; /* search pattern */
  859. int nr; /* error number */
  860. int type; /* type character */
  861. int valid; /* valid entry */
  862. {
  863. qfline_T *qfp;
  864. if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
  865. return FAIL;
  866. qfp->qf_fnum = qf_get_fnum(dir, fname);
  867. if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
  868. {
  869. vim_free(qfp);
  870. return FAIL;
  871. }
  872. qfp->qf_lnum = lnum;
  873. qfp->qf_col = col;
  874. qfp->qf_viscol = vis_col;
  875. if (pattern == NULL || *pattern == NUL)
  876. qfp->qf_pattern = NULL;
  877. else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
  878. {
  879. vim_free(qfp->qf_text);
  880. vim_free(qfp);
  881. return FAIL;
  882. }
  883. qfp->qf_nr = nr;
  884. if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
  885. type = 0;
  886. qfp->qf_type = type;
  887. qfp->qf_valid = valid;
  888. if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
  889. /* first element in the list */
  890. {
  891. qi->qf_lists[qi->qf_curlist].qf_start = qfp;
  892. qfp->qf_prev = qfp; /* first element points to itself */
  893. }
  894. else
  895. {
  896. qfp->qf_prev = *prevp;
  897. (*prevp)->qf_next = qfp;
  898. }
  899. qfp->qf_next = qfp; /* last element points to itself */
  900. qfp->qf_cleared = FALSE;
  901. *prevp = qfp;
  902. ++qi->qf_lists[qi->qf_curlist].qf_count;
  903. if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
  904. /* first valid entry */
  905. {
  906. qi->qf_lists[qi->qf_curlist].qf_index =
  907. qi->qf_lists[qi->qf_curlist].qf_count;
  908. qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
  909. }
  910. return OK;
  911. }
  912. /*
  913. * Allocate a new location list
  914. */
  915. static qf_info_T *
  916. ll_new_list()
  917. {
  918. qf_info_T *qi;
  919. qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
  920. if (qi != NULL)
  921. {
  922. vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
  923. qi->qf_refcount++;
  924. }
  925. return qi;
  926. }
  927. /*
  928. * Return the location list for window 'wp'.
  929. * If not present, allocate a location list
  930. */
  931. static qf_info_T *
  932. ll_get_or_alloc_list(wp)
  933. win_T *wp;
  934. {
  935. if (IS_LL_WINDOW(wp))
  936. /* For a location list window, use the referenced location list */
  937. return wp->w_llist_ref;
  938. /*
  939. * For a non-location list window, w_llist_ref should not point to a
  940. * location list.
  941. */
  942. ll_free_all(&wp->w_llist_ref);
  943. if (wp->w_llist == NULL)
  944. wp->w_llist = ll_new_list(); /* new location list */
  945. return wp->w_llist;
  946. }
  947. /*
  948. * Copy the location list from window "from" to window "to".
  949. */
  950. void
  951. copy_loclist(from, to)
  952. win_T *from;
  953. win_T *to;
  954. {
  955. qf_info_T *qi;
  956. int idx;
  957. int i;
  958. /*
  959. * When copying from a location list window, copy the referenced
  960. * location list. For other windows, copy the location list for
  961. * that window.
  962. */
  963. if (IS_LL_WINDOW(from))
  964. qi = from->w_llist_ref;
  965. else
  966. qi = from->w_llist;
  967. if (qi == NULL) /* no location list to copy */
  968. return;
  969. /* allocate a new location list */
  970. if ((to->w_llist = ll_new_list()) == NULL)
  971. return;
  972. to->w_llist->qf_listcount = qi->qf_listcount;
  973. /* Copy the location lists one at a time */
  974. for (idx = 0; idx < qi->qf_listcount; idx++)
  975. {
  976. qf_list_T *from_qfl;
  977. qf_list_T *to_qfl;
  978. to->w_llist->qf_curlist = idx;
  979. from_qfl = &qi->qf_lists[idx];
  980. to_qfl = &to->w_llist->qf_lists[idx];
  981. /* Some of the fields are populated by qf_add_entry() */
  982. to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
  983. to_qfl->qf_count = 0;
  984. to_qfl->qf_index = 0;
  985. to_qfl->qf_start = NULL;
  986. to_qfl->qf_ptr = NULL;
  987. if (from_qfl->qf_count)
  988. {
  989. qfline_T *from_qfp;
  990. qfline_T *prevp = NULL;
  991. /* copy all the location entries in this list */
  992. for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
  993. ++i, from_qfp = from_qfp->qf_next)
  994. {
  995. if (qf_add_entry(to->w_llist, &prevp,
  996. NULL,
  997. NULL,
  998. from_qfp->qf_text,
  999. from_qfp->qf_lnum,
  1000. from_qfp->qf_col,
  1001. from_qfp->qf_viscol,
  1002. from_qfp->qf_pattern,
  1003. from_qfp->qf_nr,
  1004. 0,
  1005. from_qfp->qf_valid) == FAIL)
  1006. {
  1007. qf_free_all(to);
  1008. return;
  1009. }
  1010. /*
  1011. * qf_add_entry() will not set the qf_num field, as the
  1012. * directory and file names are not supplied. So the qf_fnum
  1013. * field is copied here.
  1014. */
  1015. prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
  1016. prevp->qf_type = from_qfp->qf_type; /* error type */
  1017. if (from_qfl->qf_ptr == from_qfp)
  1018. to_qfl->qf_ptr = prevp; /* current location */
  1019. }
  1020. }
  1021. to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
  1022. /* When no valid entries are present in the list, qf_ptr points to
  1023. * the first item in the list */
  1024. if (to_qfl->qf_nonevalid == TRUE)
  1025. to_qfl->qf_ptr = to_qfl->qf_start;
  1026. }
  1027. to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
  1028. }
  1029. /*
  1030. * get buffer number for file "dir.name"
  1031. */
  1032. static int
  1033. qf_get_fnum(directory, fname)
  1034. char_u *directory;
  1035. char_u *fname;
  1036. {
  1037. if (fname == NULL || *fname == NUL) /* no file name */
  1038. return 0;
  1039. {
  1040. #ifdef RISCOS
  1041. /* Name is reported as `main.c', but file is `c.main' */
  1042. return ro_buflist_add(fname);
  1043. #else
  1044. char_u *ptr;
  1045. int fnum;
  1046. # ifdef VMS
  1047. vms_remove_version(fname);
  1048. # endif
  1049. # ifdef BACKSLASH_IN_FILENAME
  1050. if (directory != NULL)
  1051. slash_adjust(directory);
  1052. slash_adjust(fname);
  1053. # endif
  1054. if (directory != NULL && !vim_isAbsName(fname)
  1055. && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
  1056. {
  1057. /*
  1058. * Here we check if the file really exists.
  1059. * This should normally be true, but if make works without
  1060. * "leaving directory"-messages we might have missed a
  1061. * directory change.
  1062. */
  1063. if (mch_getperm(ptr) < 0)
  1064. {
  1065. vim_free(ptr);
  1066. directory = qf_guess_filepath(fname);
  1067. if (directory)
  1068. ptr = concat_fnames(directory, fname, TRUE);
  1069. else
  1070. ptr = vim_strsave(fname);
  1071. }
  1072. /* Use concatenated directory name and file name */
  1073. fnum = buflist_add(ptr, 0);
  1074. vim_free(ptr);
  1075. return fnum;
  1076. }
  1077. return buflist_add(fname, 0);
  1078. #endif
  1079. }
  1080. }
  1081. /*
  1082. * push dirbuf onto the directory stack and return pointer to actual dir or
  1083. * NULL on error
  1084. */
  1085. static char_u *
  1086. qf_push_dir(dirbuf, stackptr)
  1087. char_u *dirbuf;
  1088. struct dir_stack_T **stackptr;
  1089. {
  1090. struct dir_stack_T *ds_new;
  1091. struct dir_stack_T *ds_ptr;
  1092. /* allocate new stack element and hook it in */
  1093. ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
  1094. if (ds_new == NULL)
  1095. return NULL;
  1096. ds_new->next = *stackptr;
  1097. *stackptr = ds_new;
  1098. /* store directory on the stack */
  1099. if (vim_isAbsName(dirbuf)
  1100. || (*stackptr)->next == NULL
  1101. || (*stackptr && dir_stack != *stackptr))
  1102. (*stackptr)->dirname = vim_strsave(dirbuf);
  1103. else
  1104. {
  1105. /* Okay we don't have an absolute path.
  1106. * dirbuf must be a subdir of one of the directories on the stack.
  1107. * Let's search...
  1108. */
  1109. ds_new = (*stackptr)->next;
  1110. (*stackptr)->dirname = NULL;
  1111. while (ds_new)
  1112. {
  1113. vim_free((*stackptr)->dirname);
  1114. (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
  1115. TRUE);
  1116. if (mch_isdir((*stackptr)->dirname) == TRUE)
  1117. break;
  1118. ds_new = ds_new->next;
  1119. }
  1120. /* clean up all dirs we already left */
  1121. while ((*stackptr)->next != ds_new)
  1122. {
  1123. ds_ptr = (*stackptr)->next;
  1124. (*stackptr)->next = (*stackptr)->next->next;
  1125. vim_free(ds_ptr->dirname);
  1126. vim_free(ds_ptr);
  1127. }
  1128. /* Nothing found -> it must be on top level */
  1129. if (ds_new == NULL)
  1130. {
  1131. vim_free((*stackptr)->dirname);
  1132. (*stackptr)->dirname = vim_strsave(dirbuf);
  1133. }
  1134. }
  1135. if ((*stackptr)->dirname != NULL)
  1136. return (*stackptr)->dirname;
  1137. else
  1138. {
  1139. ds_ptr = *stackptr;
  1140. *stackptr = (*stackptr)->next;
  1141. vim_free(ds_ptr);
  1142. return NULL;
  1143. }
  1144. }
  1145. /*
  1146. * pop dirbuf from the directory stack and return previous directory or NULL if
  1147. * stack is empty
  1148. */
  1149. static char_u *
  1150. qf_pop_dir(stackptr)
  1151. struct dir_stack_T **stackptr;
  1152. {
  1153. struct dir_stack_T *ds_ptr;
  1154. /* TODO: Should we check if dirbuf is the directory on top of the stack?
  1155. * What to do if it isn't? */
  1156. /* pop top element and free it */
  1157. if (*stackptr != NULL)
  1158. {
  1159. ds_ptr = *stackptr;
  1160. *stackptr = (*stackptr)->next;
  1161. vim_free(ds_ptr->dirname);
  1162. vim_free(ds_ptr);
  1163. }
  1164. /* return NEW top element as current dir or NULL if stack is empty*/
  1165. return *stackptr ? (*stackptr)->dirname : NULL;
  1166. }
  1167. /*
  1168. * clean up directory stack
  1169. */
  1170. static void
  1171. qf_clean_dir_stack(stackptr)
  1172. struct dir_stack_T **stackptr;
  1173. {
  1174. struct dir_stack_T *ds_ptr;
  1175. while ((ds_ptr = *stackptr) != NULL)
  1176. {
  1177. *stackptr = (*stackptr)->next;
  1178. vim_free(ds_ptr->dirname);
  1179. vim_free(ds_ptr);
  1180. }
  1181. }
  1182. /*
  1183. * Check in which directory of the directory stack the given file can be
  1184. * found.
  1185. * Returns a pointer to the directory name or NULL if not found
  1186. * Cleans up intermediate directory entries.
  1187. *
  1188. * TODO: How to solve the following problem?
  1189. * If we have the this directory tree:
  1190. * ./
  1191. * ./aa
  1192. * ./aa/bb
  1193. * ./bb
  1194. * ./bb/x.c
  1195. * and make says:
  1196. * making all in aa
  1197. * making all in bb
  1198. * x.c:9: Error
  1199. * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
  1200. * qf_guess_filepath will return NULL.
  1201. */
  1202. static char_u *
  1203. qf_guess_filepath(filename)
  1204. char_u *filename;
  1205. {
  1206. struct dir_stack_T *ds_ptr;
  1207. struct dir_stack_T *ds_tmp;
  1208. char_u *fullname;
  1209. /* no dirs on the stack - there's nothing we can do */
  1210. if (dir_stack == NULL)
  1211. return NULL;
  1212. ds_ptr = dir_stack->next;
  1213. fullname = NULL;
  1214. while (ds_ptr)
  1215. {
  1216. vim_free(fullname);
  1217. fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
  1218. /* If concat_fnames failed, just go on. The worst thing that can happen
  1219. * is that we delete the entire stack.
  1220. */
  1221. if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
  1222. break;
  1223. ds_ptr = ds_ptr->next;
  1224. }
  1225. vim_free(fullname);
  1226. /* clean up all dirs we already left */
  1227. while (dir_stack->next != ds_ptr)
  1228. {
  1229. ds_tmp = dir_stack->next;
  1230. dir_stack->next = dir_stack->next->next;
  1231. vim_free(ds_tmp->dirname);
  1232. vim_free(ds_tmp);
  1233. }
  1234. return ds_ptr==NULL? NULL: ds_ptr->dirname;
  1235. }
  1236. /*
  1237. * jump to a quickfix line
  1238. * if dir == FORWARD go "errornr" valid entries forward
  1239. * if dir == BACKWARD go "errornr" valid entries backward
  1240. * if dir == FORWARD_FILE go "errornr" valid entries files backward
  1241. * if dir == BACKWARD_FILE go "errornr" valid entries files backward
  1242. * else if "errornr" is zero, redisplay the same line
  1243. * else go to entry "errornr"
  1244. */
  1245. void
  1246. qf_jump(qi, dir, errornr, forceit)
  1247. qf_info_T *qi;
  1248. int dir;
  1249. int errornr;
  1250. int forceit;
  1251. {
  1252. qf_info_T *ll_ref;
  1253. qfline_T *qf_ptr;
  1254. qfline_T *old_qf_ptr;
  1255. int qf_index;
  1256. int old_qf_fnum;
  1257. int old_qf_index;
  1258. int prev_index;
  1259. static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
  1260. char_u *err = e_no_more_items;
  1261. linenr_T i;
  1262. buf_T *old_curbuf;
  1263. linenr_T old_lnum;
  1264. colnr_T screen_col;
  1265. colnr_T char_col;
  1266. char_u *line;
  1267. #ifdef FEAT_WINDOWS
  1268. char_u *old_swb = p_swb;
  1269. int opened_window = FALSE;
  1270. win_T *win;
  1271. win_T *altwin;
  1272. #endif
  1273. int print_message = TRUE;
  1274. int len;
  1275. #ifdef FEAT_FOLDING
  1276. int old_KeyTyped = KeyTyped; /* getting file may reset it */
  1277. #endif
  1278. int ok = OK;
  1279. int usable_win;
  1280. if (qi == NULL)
  1281. qi = &ql_info;
  1282. if (qi->qf_curlist >= qi->qf_listcount
  1283. || qi->qf_lists[qi->qf_curlist].qf_count == 0)
  1284. {
  1285. EMSG(_(e_quickfix));
  1286. return;
  1287. }
  1288. qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
  1289. old_qf_ptr = qf_ptr;
  1290. qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
  1291. old_qf_index = qf_index;
  1292. if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
  1293. {
  1294. while (errornr--)
  1295. {
  1296. old_qf_ptr = qf_ptr;
  1297. prev_index = qf_index;
  1298. old_qf_fnum = qf_ptr->qf_fnum;
  1299. do
  1300. {
  1301. if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
  1302. || qf_ptr->qf_next == NULL)
  1303. {
  1304. qf_ptr = old_qf_ptr;
  1305. qf_index = prev_index;
  1306. if (err != NULL)
  1307. {
  1308. EMSG(_(err));
  1309. goto theend;
  1310. }
  1311. errornr = 0;
  1312. break;
  1313. }
  1314. ++qf_index;
  1315. qf_ptr = qf_ptr->qf_next;
  1316. } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
  1317. && !qf_ptr->qf_valid)
  1318. || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
  1319. err = NULL;
  1320. }
  1321. }
  1322. else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
  1323. {
  1324. while (errornr--)
  1325. {
  1326. old_qf_ptr = qf_ptr;
  1327. prev_index = qf_index;
  1328. old_qf_fnum = qf_ptr->qf_fnum;
  1329. do
  1330. {
  1331. if (qf_index == 1 || qf_ptr->qf_prev == NULL)
  1332. {
  1333. qf_ptr = old_qf_ptr;
  1334. qf_index = prev_index;
  1335. if (err != NULL)
  1336. {
  1337. EMSG(_(err));
  1338. goto theend;
  1339. }
  1340. errornr = 0;
  1341. break;
  1342. }
  1343. --qf_index;
  1344. qf_ptr = qf_ptr->qf_prev;
  1345. } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
  1346. && !qf_ptr->qf_valid)
  1347. || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
  1348. err = NULL;
  1349. }
  1350. }
  1351. else if (errornr != 0) /* go to specified number */
  1352. {
  1353. while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
  1354. {
  1355. --qf_index;
  1356. qf_ptr = qf_ptr->qf_prev;
  1357. }
  1358. while (errornr > qf_index && qf_index <
  1359. qi->qf_lists[qi->qf_curlist].qf_count
  1360. && qf_ptr->qf_next != NULL)
  1361. {
  1362. ++qf_index;
  1363. qf_ptr = qf_ptr->qf_next;
  1364. }
  1365. }
  1366. #ifdef FEAT_WINDOWS
  1367. qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
  1368. if (qf_win_pos_update(qi, old_qf_index))
  1369. /* No need to print the error message if it's visible in the error
  1370. * window */
  1371. print_message = FALSE;
  1372. /*
  1373. * For ":helpgrep" find a help window or open one.
  1374. */
  1375. if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
  1376. {
  1377. win_T *wp;
  1378. int n;
  1379. if (cmdmod.tab != 0)
  1380. wp = NULL;
  1381. else
  1382. for (wp = firstwin; wp != NULL; wp = wp->w_next)
  1383. if (wp->w_buffer != NULL && wp->w_buffer->b_help)
  1384. break;
  1385. if (wp != NULL && wp->w_buffer->b_nwindows > 0)
  1386. win_enter(wp, TRUE);
  1387. else
  1388. {
  1389. /*
  1390. * Split off help window; put it at far top if no position
  1391. * specified, the current window is vertically split and narrow.
  1392. */
  1393. n = WSP_HELP;
  1394. # ifdef FEAT_VERTSPLIT
  1395. if (cmdmod.split == 0 && curwin->w_width != Columns
  1396. && curwin->w_width < 80)
  1397. n |= WSP_TOP;
  1398. # endif
  1399. if (win_split(0, n) == FAIL)
  1400. goto theend;
  1401. opened_window = TRUE; /* close it when fail */
  1402. if (curwin->w_height < p_hh)
  1403. win_setheight((int)p_hh);
  1404. if (qi != &ql_info) /* not a quickfix list */
  1405. {
  1406. /* The new window should use the supplied location list */
  1407. qf_free_all(curwin);
  1408. curwin->w_llist = qi;
  1409. qi->qf_refcount++;
  1410. }
  1411. }
  1412. if (!p_im)
  1413. restart_edit = 0; /* don't want insert mode in help file */
  1414. }
  1415. /*
  1416. * If currently in the quickfix window, find another window to show the
  1417. * file in.
  1418. */
  1419. if (bt_quickfix(curbuf) && !opened_window)
  1420. {
  1421. /*
  1422. * If there is no file specified, we don't know where to go.
  1423. * But do advance, otherwise ":cn" gets stuck.
  1424. */
  1425. if (qf_ptr->qf_fnum == 0)
  1426. goto theend;
  1427. /* Locate a window showing a normal buffer */
  1428. usable_win = 0;
  1429. FOR_ALL_WINDOWS(win)
  1430. if (win->w_buffer->b_p_bt[0] == NUL)
  1431. {
  1432. usable_win = 1;
  1433. break;
  1434. }
  1435. /*
  1436. * If there is only one window, create a new one above the quickfix
  1437. * window.
  1438. */
  1439. if (firstwin == lastwin || !usable_win)
  1440. {
  1441. ll_ref = curwin->w_llist_ref;
  1442. if (win_split(0, WSP_ABOVE) == FAIL)
  1443. goto failed; /* not enough room for window */
  1444. opened_window = TRUE; /* close it when fail */
  1445. p_swb = empty_option; /* don't split again */
  1446. # ifdef FEAT_SCROLLBIND
  1447. curwin->w_p_scb = FALSE;
  1448. # endif
  1449. if (ll_ref != NULL)
  1450. {
  1451. /* The new window should use the location list from the
  1452. * location list window */
  1453. qf_free_all(curwin);
  1454. curwin->w_llist = ll_ref;
  1455. ll_ref->qf_refcount++;
  1456. }
  1457. }
  1458. else
  1459. {
  1460. if (curwin->w_llist_ref != NULL)
  1461. {
  1462. /* In a location window */
  1463. ll_ref = curwin->w_llist_ref;
  1464. /* Find the window with the same location list */
  1465. FOR_ALL_WINDOWS(win)
  1466. if (win->w_llist == ll_ref)
  1467. break;
  1468. if (win == NULL)
  1469. {
  1470. /* Find the window showing the selected file */
  1471. FOR_ALL_WINDOWS(win)
  1472. if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
  1473. break;
  1474. if (win == NULL)
  1475. {
  1476. /* Find a previous usable window */
  1477. win = curwin;
  1478. do
  1479. {
  1480. if (win->w_buffer->b_p_bt[0] == NUL)
  1481. break;
  1482. if (win->w_prev == NULL)
  1483. win = lastwin; /* wrap around the top */
  1484. else
  1485. win = win->w_prev; /* go to previous window */
  1486. } while (win != curwin);
  1487. }
  1488. }
  1489. win_goto(win);
  1490. /* If the location list for the window is not set, then set it
  1491. * to the location list from the location window */
  1492. if (win->w_llist == NULL)
  1493. {
  1494. win->w_llist = ll_ref;
  1495. ll_ref->qf_refcount++;
  1496. }
  1497. }
  1498. else
  1499. {
  1500. /*
  1501. * Try to find a window that shows the right buffer.
  1502. * Default to the window just above the quickfix buffer.
  1503. */
  1504. win = curwin;
  1505. altwin = NULL;
  1506. for (;;)
  1507. {
  1508. if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
  1509. break;
  1510. if (win->w_prev == NULL)
  1511. win = lastwin; /* wrap around the top */
  1512. else
  1513. win = win->w_prev; /* go to previous window */
  1514. if (IS_QF_WINDOW(win))
  1515. {
  1516. /* Didn't find it, go to the window before the quickfix
  1517. * window. */
  1518. if (altwin != NULL)
  1519. win = altwin;
  1520. else if (curwin->w_prev != NULL)
  1521. win = curwin->w_prev;
  1522. else
  1523. win = curwin->w_next;
  1524. break;
  1525. }
  1526. /* Remember a usable window. */
  1527. if (altwin == NULL && !win->w_p_pvw
  1528. && win->w_buffer->b_p_bt[0] == NUL)
  1529. altwin = win;
  1530. }
  1531. win_goto(win);
  1532. }
  1533. }
  1534. }
  1535. #endif
  1536. /*
  1537. * If there is a file name,
  1538. * read the wanted file if needed, and check autowrite etc.
  1539. */
  1540. old_curbuf = curbuf;
  1541. old_lnum = curwin->w_cursor.lnum;
  1542. if (qf_ptr->qf_fnum != 0)
  1543. {
  1544. if (qf_ptr->qf_type == 1)
  1545. {
  1546. /* Open help file (do_ecmd() will set b_help flag, readfile() will
  1547. * set b_p_ro flag). */
  1548. if (!can_abandon(curbuf, forceit))
  1549. {
  1550. EMSG(_(e_nowrtmsg));
  1551. ok = FALSE;
  1552. }
  1553. else
  1554. ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
  1555. ECMD_HIDE + ECMD_SET_HELP);
  1556. }
  1557. else
  1558. ok = buflist_getfile(qf_ptr->qf_fnum,
  1559. (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
  1560. }
  1561. if (ok == OK)
  1562. {
  1563. /* When not switched to another buffer, still need to set pc mark */
  1564. if (curbuf == old_curbuf)
  1565. setpcmark();
  1566. if (qf_ptr->qf_pattern == NULL)
  1567. {
  1568. /*
  1569. * Go to line with error, unless qf_lnum is 0.
  1570. */
  1571. i = qf_ptr->qf_lnum;
  1572. if (i > 0)
  1573. {
  1574. if (i > curbuf->b_ml.ml_line_count)
  1575. i = curbuf->b_ml.ml_line_count;
  1576. curwin->w_cursor.lnum = i;
  1577. }
  1578. if (qf_ptr->qf_col > 0)
  1579. {
  1580. curwin->w_cursor.col = qf_ptr->qf_col - 1;
  1581. if (qf_ptr->qf_viscol == TRUE)
  1582. {
  1583. /*
  1584. * Check each character from the beginning of the error
  1585. * line up to the error column. For each tab character
  1586. * found, reduce the error column value by the length of
  1587. * a tab character.
  1588. */
  1589. line = ml_get_curline();
  1590. screen_col = 0;
  1591. for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
  1592. {
  1593. if (*line == NUL)
  1594. break;
  1595. if (*line++ == '\t')
  1596. {
  1597. curwin->w_cursor.col -= 7 - (screen_col % 8);
  1598. screen_col += 8 - (screen_col % 8);
  1599. }
  1600. else
  1601. ++screen_col;
  1602. }
  1603. }
  1604. check_cursor();
  1605. }
  1606. else
  1607. beginline(BL_WHITE | BL_FIX);
  1608. }
  1609. else
  1610. {
  1611. pos_T save_cursor;
  1612. /* Move the cursor to the first line in the buffer */
  1613. save_cursor = curwin->w_cursor;
  1614. curwin->w_cursor.lnum = 0;
  1615. if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
  1616. curwin->w_cursor = save_cursor;
  1617. }
  1618. #ifdef FEAT_FOLDING
  1619. if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
  1620. foldOpenCursor();
  1621. #endif
  1622. if (print_message)
  1623. {
  1624. /* Update the screen before showing the message */
  1625. update_topline_redraw();
  1626. sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
  1627. qi->qf_lists[qi->qf_curlist].qf_count,
  1628. qf_ptr->qf_cleared ? _(" (line deleted)") : "",
  1629. (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
  1630. /* Add the message, skipping leading whitespace and newlines. */
  1631. len = (int)STRLEN(IObuff);
  1632. qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
  1633. /* Output the message. Overwrite to avoid scrolling when the 'O'
  1634. * flag is present in 'shortmess'; But when not jumping, print the
  1635. * whole message. */
  1636. i = msg_scroll;
  1637. if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
  1638. msg_scroll = TRUE;
  1639. else if (!msg_scrolled && shortmess(SHM_OVERALL))
  1640. msg_scroll = FALSE;
  1641. msg_attr_keep(IObuff, 0, TRUE);
  1642. msg_scroll = i;
  1643. }
  1644. }
  1645. else
  1646. {
  1647. #ifdef FEAT_WINDOWS
  1648. if (opened_window)
  1649. win_close(curwin, TRUE); /* Close opened window */
  1650. #endif
  1651. if (qf_ptr->qf_fnum != 0)
  1652. {
  1653. /*
  1654. * Couldn't open file, so put index back where it was. This could
  1655. * happen if the file was readonly and we changed something.
  1656. */
  1657. #ifdef FEAT_WINDOWS
  1658. failed:
  1659. #endif
  1660. qf_ptr = old_qf_ptr;
  1661. qf_index = old_qf_index;
  1662. }
  1663. }
  1664. theend:
  1665. qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
  1666. qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
  1667. #ifdef FEAT_WINDOWS
  1668. if (p_swb != old_swb && opened_window)
  1669. {
  1670. /* Restore old 'switchbuf' value, but not when an autocommand or
  1671. * modeline has changed the value. */
  1672. if (p_swb == empty_option)
  1673. p_swb = old_swb;
  1674. else
  1675. free_string_option(old_swb);
  1676. }
  1677. #endif
  1678. }
  1679. /*
  1680. * ":clist": list all errors
  1681. * ":llist": list all locations
  1682. */
  1683. void
  1684. qf_list(eap)
  1685. exarg_T *eap;
  1686. {
  1687. buf_T *buf;
  1688. char_u *fname;
  1689. qfline_T *qfp;
  1690. int i;
  1691. int idx1 = 1;
  1692. int idx2 = -1;
  1693. int need_return = TRUE;
  1694. char_u *arg = eap->arg;
  1695. int all = eap->forceit; /* if not :cl!, only show
  1696. recognised errors */
  1697. qf_info_T *qi = &ql_info;
  1698. if (eap->cmdidx == CMD_llist)
  1699. {
  1700. qi = GET_LOC_LIST(curwin);
  1701. if (qi == NULL)
  1702. {
  1703. EMSG(_(e_loclist));
  1704. return;
  1705. }
  1706. }
  1707. if (qi->qf_curlist >= qi->qf_listcount
  1708. || qi->qf_lists[qi->qf_curlist].qf_count == 0)
  1709. {
  1710. EMSG(_(e_quickfix));
  1711. return;
  1712. }
  1713. if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
  1714. {
  1715. EMSG(_(e_trailing));
  1716. return;
  1717. }
  1718. i = qi->qf_lists[qi->qf_curlist].qf_count;
  1719. if (idx1 < 0)
  1720. idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
  1721. if (idx2 < 0)
  1722. idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
  1723. if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
  1724. all = TRUE;
  1725. qfp = qi->qf_lists[qi->qf_curlist].qf_start;
  1726. for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
  1727. {
  1728. if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
  1729. {
  1730. if (need_return)
  1731. {
  1732. msg_putchar('\n');
  1733. if (got_int)
  1734. break;
  1735. need_return = FALSE;
  1736. }
  1737. fname = NULL;
  1738. if (qfp->qf_fnum != 0
  1739. && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
  1740. {
  1741. fname = buf->b_fname;
  1742. if (qfp->qf_type == 1) /* :helpgrep */
  1743. fname = gettail(fname);
  1744. }
  1745. if (fname == NULL)
  1746. sprintf((char *)IObuff, "%2d", i);
  1747. else
  1748. vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
  1749. i, (char *)fname);
  1750. msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
  1751. ? hl_attr(HLF_L) : hl_attr(HLF_D));
  1752. if (qfp->qf_lnum == 0)
  1753. IObuff[0] = NUL;
  1754. else if (qfp->qf_col == 0)
  1755. sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
  1756. else
  1757. sprintf((char *)IObuff, ":%ld col %d",
  1758. qfp->qf_lnum, qfp->qf_col);
  1759. sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
  1760. (char *)qf_types(qfp->qf_type, qfp->qf_nr));
  1761. msg_puts_attr(IObuff, hl_attr(HLF_N));
  1762. if (qfp->qf_pattern != NULL)
  1763. {
  1764. qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
  1765. STRCAT(IObuff, ":");
  1766. msg_puts(IObuff);
  1767. }
  1768. msg_puts((char_u *)" ");
  1769. /* Remove newlines and leading whitespace from the text. For an
  1770. * unrecognized line keep the indent, the compiler may mark a word
  1771. * with ^^^^. */
  1772. qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
  1773. ? skipwhite(qfp->qf_text) : qfp->qf_text,
  1774. IObuff, IOSIZE);
  1775. msg_prt_line(IObuff, FALSE);
  1776. out_flush(); /* show one line at a time */
  1777. need_return = TRUE;
  1778. }
  1779. qfp = qfp->qf_next;
  1780. ++i;
  1781. ui_breakcheck();
  1782. }
  1783. }
  1784. /*
  1785. * Remove newlines and leading whitespace from an error message.
  1786. * Put the result in "buf[bufsize]".
  1787. */
  1788. static void
  1789. qf_fmt_text(text, buf, bufsize)
  1790. char_u *text;
  1791. char_u *buf;
  1792. int bufsize;
  1793. {
  1794. int i;
  1795. char_u *p = text;
  1796. for (i = 0; *p != NUL && i < bufsize - 1; ++i)
  1797. {
  1798. if (*p == '\n')
  1799. {
  1800. buf[i] = ' ';
  1801. while (*++p != NUL)
  1802. if (!vim_iswhite(*p) && *p != '\n')
  1803. break;
  1804. }
  1805. else
  1806. buf[i] = *p++;
  1807. }
  1808. buf[i] = NUL;
  1809. }
  1810. /*
  1811. * ":colder [count]": Up in the quickfix stack.
  1812. * ":cnewer [count]": Down in the quickfix stack.
  1813. * ":lolder [count]": Up in the location list stack.
  1814. * ":lnewer …

Large files files are truncated, but you can click here to view the full file