PageRenderTime 72ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/branches/vim7.2/src/quickfix.c

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

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