PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/vim7.1.330/src/quickfix.c

#
C | 2652 lines | 1977 code | 218 blank | 457 comment | 768 complexity | 641bef4dbcba527ea770b87a7a80b7a8 MD5 | raw 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. STRCPY(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. int opened_window = FALSE;
  1310. win_T *win;
  1311. win_T *altwin;
  1312. #endif
  1313. int print_message = TRUE;
  1314. int len;
  1315. #ifdef FEAT_FOLDING
  1316. int old_KeyTyped = KeyTyped; /* getting file may reset it */
  1317. #endif
  1318. int ok = OK;
  1319. int usable_win;
  1320. if (qi == NULL)
  1321. qi = &ql_info;
  1322. if (qi->qf_curlist >= qi->qf_listcount
  1323. || qi->qf_lists[qi->qf_curlist].qf_count == 0)
  1324. {
  1325. EMSG(_(e_quickfix));
  1326. return;
  1327. }
  1328. qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
  1329. old_qf_ptr = qf_ptr;
  1330. qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
  1331. old_qf_index = qf_index;
  1332. if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
  1333. {
  1334. while (errornr--)
  1335. {
  1336. old_qf_ptr = qf_ptr;
  1337. prev_index = qf_index;
  1338. old_qf_fnum = qf_ptr->qf_fnum;
  1339. do
  1340. {
  1341. if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
  1342. || qf_ptr->qf_next == NULL)
  1343. {
  1344. qf_ptr = old_qf_ptr;
  1345. qf_index = prev_index;
  1346. if (err != NULL)
  1347. {
  1348. EMSG(_(err));
  1349. goto theend;
  1350. }
  1351. errornr = 0;
  1352. break;
  1353. }
  1354. ++qf_index;
  1355. qf_ptr = qf_ptr->qf_next;
  1356. } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
  1357. && !qf_ptr->qf_valid)
  1358. || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
  1359. err = NULL;
  1360. }
  1361. }
  1362. else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
  1363. {
  1364. while (errornr--)
  1365. {
  1366. old_qf_ptr = qf_ptr;
  1367. prev_index = qf_index;
  1368. old_qf_fnum = qf_ptr->qf_fnum;
  1369. do
  1370. {
  1371. if (qf_index == 1 || qf_ptr->qf_prev == NULL)
  1372. {
  1373. qf_ptr = old_qf_ptr;
  1374. qf_index = prev_index;
  1375. if (err != NULL)
  1376. {
  1377. EMSG(_(err));
  1378. goto theend;
  1379. }
  1380. errornr = 0;
  1381. break;
  1382. }
  1383. --qf_index;
  1384. qf_ptr = qf_ptr->qf_prev;
  1385. } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
  1386. && !qf_ptr->qf_valid)
  1387. || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
  1388. err = NULL;
  1389. }
  1390. }
  1391. else if (errornr != 0) /* go to specified number */
  1392. {
  1393. while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
  1394. {
  1395. --qf_index;
  1396. qf_ptr = qf_ptr->qf_prev;
  1397. }
  1398. while (errornr > qf_index && qf_index <
  1399. qi->qf_lists[qi->qf_curlist].qf_count
  1400. && qf_ptr->qf_next != NULL)
  1401. {
  1402. ++qf_index;
  1403. qf_ptr = qf_ptr->qf_next;
  1404. }
  1405. }
  1406. #ifdef FEAT_WINDOWS
  1407. qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
  1408. if (qf_win_pos_update(qi, old_qf_index))
  1409. /* No need to print the error message if it's visible in the error
  1410. * window */
  1411. print_message = FALSE;
  1412. /*
  1413. * For ":helpgrep" find a help window or open one.
  1414. */
  1415. if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
  1416. {
  1417. win_T *wp;
  1418. int n;
  1419. if (cmdmod.tab != 0)
  1420. wp = NULL;
  1421. else
  1422. for (wp = firstwin; wp != NULL; wp = wp->w_next)
  1423. if (wp->w_buffer != NULL && wp->w_buffer->b_help)
  1424. break;
  1425. if (wp != NULL && wp->w_buffer->b_nwindows > 0)
  1426. win_enter(wp, TRUE);
  1427. else
  1428. {
  1429. /*
  1430. * Split off help window; put it at far top if no position
  1431. * specified, the current window is vertically split and narrow.
  1432. */
  1433. n = WSP_HELP;
  1434. # ifdef FEAT_VERTSPLIT
  1435. if (cmdmod.split == 0 && curwin->w_width != Columns
  1436. && curwin->w_width < 80)
  1437. n |= WSP_TOP;
  1438. # endif
  1439. if (win_split(0, n) == FAIL)
  1440. goto theend;
  1441. opened_window = TRUE; /* close it when fail */
  1442. if (curwin->w_height < p_hh)
  1443. win_setheight((int)p_hh);
  1444. if (qi != &ql_info) /* not a quickfix list */
  1445. {
  1446. /* The new window should use the supplied location list */
  1447. qf_free_all(curwin);
  1448. curwin->w_llist = qi;
  1449. qi->qf_refcount++;
  1450. }
  1451. }
  1452. if (!p_im)
  1453. restart_edit = 0; /* don't want insert mode in help file */
  1454. }
  1455. /*
  1456. * If currently in the quickfix window, find another window to show the
  1457. * file in.
  1458. */
  1459. if (bt_quickfix(curbuf) && !opened_window)
  1460. {
  1461. /*
  1462. * If there is no file specified, we don't know where to go.
  1463. * But do advance, otherwise ":cn" gets stuck.
  1464. */
  1465. if (qf_ptr->qf_fnum == 0)
  1466. goto theend;
  1467. /* Locate a window showing a normal buffer */
  1468. usable_win = 0;
  1469. FOR_ALL_WINDOWS(win)
  1470. if (win->w_buffer->b_p_bt[0] == NUL)
  1471. {
  1472. usable_win = 1;
  1473. break;
  1474. }
  1475. /*
  1476. * If no usable window is found and 'switchbuf' is set to 'usetab'
  1477. * then search in other tabs.
  1478. */
  1479. if (!usable_win && vim_strchr(p_swb, 'a') != NULL)
  1480. {
  1481. tabpage_T *tp;
  1482. win_T *wp;
  1483. FOR_ALL_TAB_WINDOWS(tp, wp)
  1484. {
  1485. if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
  1486. {
  1487. goto_tabpage_win(tp, wp);
  1488. usable_win = 1;
  1489. break;
  1490. }
  1491. }
  1492. }
  1493. /*
  1494. * If there is only one window and it is the quickfix window, create a
  1495. * new one above the quickfix window.
  1496. */
  1497. if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
  1498. {
  1499. ll_ref = curwin->w_llist_ref;
  1500. if (win_split(0, WSP_ABOVE) == FAIL)
  1501. goto failed; /* not enough room for window */
  1502. opened_window = TRUE; /* close it when fail */
  1503. p_swb = empty_option; /* don't split again */
  1504. # ifdef FEAT_SCROLLBIND
  1505. curwin->w_p_scb = FALSE;
  1506. # endif
  1507. if (ll_ref != NULL)
  1508. {
  1509. /* The new window should use the location list from the
  1510. * location list window */
  1511. qf_free_all(curwin);
  1512. curwin->w_llist = ll_ref;
  1513. ll_ref->qf_refcount++;
  1514. }
  1515. }
  1516. else
  1517. {
  1518. if (curwin->w_llist_ref != NULL)
  1519. {
  1520. /* In a location window */
  1521. ll_ref = curwin->w_llist_ref;
  1522. /* Find the window with the same location list */
  1523. FOR_ALL_WINDOWS(win)
  1524. if (win->w_llist == ll_ref)
  1525. break;
  1526. if (win == NULL)
  1527. {
  1528. /* Find the window showing the selected file */
  1529. FOR_ALL_WINDOWS(win)
  1530. if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
  1531. break;
  1532. if (win == NULL)
  1533. {
  1534. /* Find a previous usable window */
  1535. win = curwin;
  1536. do
  1537. {
  1538. if (win->w_buffer->b_p_bt[0] == NUL)
  1539. break;
  1540. if (win->w_prev == NULL)
  1541. win = lastwin; /* wrap around the top */
  1542. else
  1543. win = win->w_prev; /* go to previous window */
  1544. } while (win != curwin);
  1545. }
  1546. }
  1547. win_goto(win);
  1548. /* If the location list for the window is not set, then set it
  1549. * to the location list from the location window */
  1550. if (win->w_llist == NULL)
  1551. {
  1552. win->w_llist = ll_ref;
  1553. ll_ref->qf_refcount++;
  1554. }
  1555. }
  1556. else
  1557. {
  1558. /*
  1559. * Try to find a window that shows the right buffer.
  1560. * Default to the window just above the quickfix buffer.
  1561. */
  1562. win = curwin;
  1563. altwin = NULL;
  1564. for (;;)
  1565. {
  1566. if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
  1567. break;
  1568. if (win->w_prev == NULL)
  1569. win = lastwin; /* wrap around the top */
  1570. else
  1571. win = win->w_prev; /* go to previous window */
  1572. if (IS_QF_WINDOW(win))
  1573. {
  1574. /* Didn't find it, go to the window before the quickfix
  1575. * window. */
  1576. if (altwin != NULL)
  1577. win = altwin;
  1578. else if (curwin->w_prev != NULL)
  1579. win = curwin->w_prev;
  1580. else
  1581. win = curwin->w_next;
  1582. break;
  1583. }
  1584. /* Remember a usable window. */
  1585. if (altwin == NULL && !win->w_p_pvw
  1586. && win->w_buffer->b_p_bt[0] == NUL)
  1587. altwin = win;
  1588. }
  1589. win_goto(win);
  1590. }
  1591. }
  1592. }
  1593. #endif
  1594. /*
  1595. * If there is a file name,
  1596. * read the wanted file if needed, and check autowrite etc.
  1597. */
  1598. old_curbuf = curbuf;
  1599. old_lnum = curwin->w_cursor.lnum;
  1600. if (qf_ptr->qf_fnum != 0)
  1601. {
  1602. if (qf_ptr->qf_type == 1)
  1603. {
  1604. /* Open help file (do_ecmd() will set b_help flag, readfile() will
  1605. * set b_p_ro flag). */
  1606. if (!can_abandon(curbuf, forceit))
  1607. {
  1608. EMSG(_(e_nowrtmsg));
  1609. ok = FALSE;
  1610. }
  1611. else
  1612. ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
  1613. ECMD_HIDE + ECMD_SET_HELP);
  1614. }
  1615. else
  1616. ok = buflist_getfile(qf_ptr->qf_fnum,
  1617. (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
  1618. }
  1619. if (ok == OK)
  1620. {
  1621. /* When not switched to another buffer, still need to set pc mark */
  1622. if (curbuf == old_curbuf)
  1623. setpcmark();
  1624. if (qf_ptr->qf_pattern == NULL)
  1625. {
  1626. /*
  1627. * Go to line with error, unless qf_lnum is 0.
  1628. */
  1629. i = qf_ptr->qf_lnum;
  1630. if (i > 0)
  1631. {
  1632. if (i > curbuf->b_ml.ml_line_count)
  1633. i = curbuf->b_ml.ml_line_count;
  1634. curwin->w_cursor.lnum = i;
  1635. }
  1636. if (qf_ptr->qf_col > 0)
  1637. {
  1638. curwin->w_cursor.col = qf_ptr->qf_col - 1;
  1639. if (qf_ptr->qf_viscol == TRUE)
  1640. {
  1641. /*
  1642. * Check each character from the beginning of the error
  1643. * line up to the error column. For each tab character
  1644. * found, reduce the error column value by the length of
  1645. * a tab character.
  1646. */
  1647. line = ml_get_curline();
  1648. screen_col = 0;
  1649. for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
  1650. {
  1651. if (*line == NUL)
  1652. break;
  1653. if (*line++ == '\t')
  1654. {
  1655. curwin->w_cursor.col -= 7 - (screen_col % 8);
  1656. screen_col += 8 - (screen_col % 8);
  1657. }
  1658. else
  1659. ++screen_col;
  1660. }
  1661. }
  1662. check_cursor();
  1663. }
  1664. else
  1665. beginline(BL_WHITE | BL_FIX);
  1666. }
  1667. else
  1668. {
  1669. pos_T save_cursor;
  1670. /* Move the cursor to the first line in the buffer */
  1671. save_cursor = curwin->w_cursor;
  1672. curwin->w_cursor.lnum = 0;
  1673. if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1,
  1674. SEARCH_KEEP, NULL))
  1675. curwin->w_cursor = save_cursor;
  1676. }
  1677. #ifdef FEAT_FOLDING
  1678. if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
  1679. foldOpenCursor();
  1680. #endif
  1681. if (print_message)
  1682. {
  1683. /* Update the screen before showing the message */
  1684. update_topline_redraw();
  1685. sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
  1686. qi->qf_lists[qi->qf_curlist].qf_count,
  1687. qf_ptr->qf_cleared ? _(" (line deleted)") : "",
  1688. (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
  1689. /* Add the message, skipping leading whitespace and newlines. */
  1690. len = (int)STRLEN(IObuff);
  1691. qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
  1692. /* Output the message. Overwrite to avoid scrolling when the 'O'
  1693. * flag is present in 'shortmess'; But when not jumping, print the
  1694. * whole message. */
  1695. i = msg_scroll;
  1696. if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
  1697. msg_scroll = TRUE;
  1698. else if (!msg_scrolled && shortmess(SHM_OVERALL))
  1699. msg_scroll = FALSE;
  1700. msg_attr_keep(IObuff, 0, TRUE);
  1701. msg_scroll = i;
  1702. }
  1703. }
  1704. else
  1705. {
  1706. #ifdef FEAT_WINDOWS
  1707. if (opened_window)
  1708. win_close(curwin, TRUE); /* Close opened window */
  1709. #endif
  1710. if (qf_ptr->qf_fnum != 0)
  1711. {
  1712. /*
  1713. * Couldn't open file, so put index back where it was. This could
  1714. * happen if the file was readonly and we changed something.
  1715. */
  1716. #ifdef FEAT_WINDOWS
  1717. failed:
  1718. #endif
  1719. qf_ptr = old_qf_ptr;
  1720. qf_index = old_qf_index;
  1721. }
  1722. }
  1723. theend:
  1724. qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
  1725. qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
  1726. #ifdef FEAT_WINDOWS
  1727. if (p_swb != old_swb && opened_window)
  1728. {
  1729. /* Restore old 'switchbuf' value, but not when an autocommand or
  1730. * modeline has changed the value. */
  1731. if (p_swb == empty_option)
  1732. p_swb = old_swb;
  1733. else
  1734. free_string_option(old_swb);
  1735. }
  1736. #endif
  1737. }
  1738. /*
  1739. * ":clist": list all errors
  1740. * ":llist": list all locations
  1741. */
  1742. void
  1743. qf_list(eap)
  1744. exarg_T *eap;
  1745. {
  1746. buf_T *buf;
  1747. char_u *fname;
  1748. qfline_T *qfp;
  1749. int i;
  1750. int idx1 = 1;
  1751. int idx2 = -1;
  1752. int need_return = TRUE;
  1753. char_u *arg = eap->arg;
  1754. int all = eap->forceit; /* if not :cl!, only show
  1755. recognised errors */
  1756. qf_info_T *qi = &ql_info;
  1757. if (eap->cmdidx == CMD_llist)
  1758. {
  1759. qi = GET_LOC_LIST(curwin);
  1760. if (qi == NULL)
  1761. {
  1762. EMSG(_(e_loclist));
  1763. return;
  1764. }
  1765. }
  1766. if (qi->qf_curlist >= qi->qf_listcount
  1767. || qi->qf_lists[qi->qf_curlist].qf_count == 0)
  1768. {
  1769. EMSG(_(e_quickfix));
  1770. return;
  1771. }
  1772. if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
  1773. {
  1774. EMSG(_(e_trailing));
  1775. return;
  1776. }
  1777. i = qi->qf_lists[qi->qf_curlist].qf_count;
  1778. if (idx1 < 0)
  1779. idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
  1780. if (idx2 < 0)
  1781. idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
  1782. if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
  1783. all = TRUE;
  1784. qfp = qi->qf_lists[qi->qf_curlist].qf_start;
  1785. for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
  1786. {
  1787. if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
  1788. {
  1789. if (need_return)
  1790. {
  1791. msg_putchar('\n');
  1792. if (got_int)
  1793. break;
  1794. need_return = FALSE;
  1795. }
  1796. fname = NULL;
  1797. if (qfp->qf_fnum != 0
  1798. && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
  1799. {
  1800. fname = buf->b_fname;
  1801. if (qfp->qf_type == 1) /* :helpgrep */
  1802. fname = gettail(fname);
  1803. }
  1804. if (fname == NULL)
  1805. sprintf((char *)IObuff, "%2d", i);
  1806. else
  1807. vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
  1808. i, (char *)fname);
  1809. msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
  1810. ? hl_attr(HLF_L) : hl_attr(HLF_D));
  1811. if (qfp->qf_lnum == 0)
  1812. IObuff[0] = NUL;
  1813. else if (qfp->qf_col == 0)
  1814. sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
  1815. else
  1816. sprintf((char *)IObuff, ":%ld col %d",
  1817. qfp->qf_lnum, qfp->qf_col);
  1818. sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
  1819. (char *)qf_types(qfp->qf_type, qfp->qf_nr));
  1820. msg_puts_attr(IObuff, hl_attr(HLF_N));
  1821. if (qfp->qf_pattern != NULL)
  1822. {
  1823. qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
  1824. STRCAT(IObuff, ":");
  1825. msg_puts(IObuff);
  1826. }
  1827. msg_puts((char_u *)" ");
  1828. /* Remove newlines and leading whitespace from the text. For an
  1829. * unrecognized line keep the indent, the compiler may mark a word
  1830. * with ^^^^. */
  1831. qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
  1832. ? skipwhite(qfp->qf_text) : qfp->qf_text,
  1833. IObuff, IOSIZE);
  1834. msg_prt_line(IObuff, FALSE);
  1835. out_flush(); /* show one line at a time */
  1836. need_return = TRUE;
  1837. }
  1838. qfp = qfp->qf_next;
  1839. ++i;
  1840. ui_breakcheck();
  1841. }
  1842. }
  1843. /*
  1844. * Remove newlines and leading whitespace from an error message.
  1845. * Put the result in "buf[bufsize]".
  1846. */
  1847. static void
  1848. qf_fmt_text(text, buf, bufsize)
  1849. char_u *text;
  1850. char_u *buf;
  1851. int bufsize;
  1852. {
  1853. int i;
  1854. char_u *p = text;
  1855. for (i = 0; *p != NUL && i < bufsize - 1; ++i)
  1856. {
  1857. if (*p == '\n')
  1858. {
  1859. buf[i] = ' ';
  1860. while (*++p != NUL)
  1861. if (!vim_iswhite(*p) && *p != '\n')
  1862. break;
  1863. }
  1864. else
  1865. buf[i] = *p++;
  1866. }
  1867. buf[i] = NUL;
  1868. }
  1869. /*
  1870. * ":colder [count]": Up in the quickfix stack.
  1871. * ":cnewer [count]": Down in the quickfix stack.
  1872. * ":lolder [count]": Up in the location list stack.
  1873. * ":lnewer [count]": Down in the location list stack.
  1874. */
  1875. void
  1876. qf_age(eap)
  1877. exarg_T *eap;
  1878. {
  1879. qf_info_T *qi = &ql_info;
  1880. int count;
  1881. if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
  1882. {
  1883. qi = GET_LOC_LIST(curwin);
  1884. if (qi == NULL)
  1885. {
  1886. EMSG(_(e_loclist));
  1887. return;
  1888. }
  1889. }
  1890. if (eap->addr_count != 0)
  1891. count = eap->line2;
  1892. else
  1893. count = 1;
  1894. while (count--)
  1895. {
  1896. if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
  1897. {
  1898. if (qi->qf_curlist == 0)
  1899. {
  1900. EMSG(_("E380: At bottom of quickfix stack"));
  1901. return;
  1902. }
  1903. --qi->qf_curlist;
  1904. }
  1905. else
  1906. {
  1907. if (qi->qf_curlist >= qi->qf_listcount - 1)
  1908. {
  1909. EMSG(_("E381: At top of quickfix stack"));
  1910. return;
  1911. }
  1912. ++qi->qf_curlist;
  1913. }
  1914. }
  1915. qf_msg(qi);
  1916. }
  1917. static void
  1918. qf_msg(qi)
  1919. qf_info_T *qi;
  1920. {
  1921. smsg((char_u *)_("error list %d of %d; %d errors"),
  1922. qi->qf_curlist + 1, qi->qf_listcount,
  1923. qi->qf_lists[qi->qf_curlist].qf_count);
  1924. #ifdef FEAT_WINDOWS
  1925. qf_update_buffer(qi);
  1926. #endif
  1927. }
  1928. /*
  1929. * Free error list "idx".
  1930. */
  1931. static void
  1932. qf_free(qi, idx)
  1933. qf_info_T *qi;
  1934. int idx;
  1935. {
  1936. qfline_T *qfp;
  1937. while (qi->qf_lists[idx].qf_count)
  1938. {
  1939. qfp = qi->qf_lists[idx].qf_start->qf_next;
  1940. vim_free(qi->qf_lists[idx].qf_start->qf_text);
  1941. vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
  1942. vim_free(qi->qf_lists[idx].qf_start);
  1943. qi->qf_lists[idx].qf_start = qfp;
  1944. --qi->qf_lists[idx].qf_count;
  1945. }
  1946. }
  1947. /*
  1948. * qf_mark_adjust: adjust marks
  1949. */
  1950. void
  1951. qf_mark_adjust(wp, line1, line2, amount, amount_after)
  1952. win_T *wp;
  1953. linenr_T line1;
  1954. linenr_T line2;
  1955. long amount;
  1956. long amount_after;
  1957. {
  1958. int i;
  1959. qfline_T *qfp;
  1960. int idx;
  1961. qf_info_T *qi = &ql_info;
  1962. if (wp != NULL)
  1963. {
  1964. if (wp->w_llist == NULL)
  1965. return;
  1966. qi = wp->w_llist;
  1967. }
  1968. for (idx = 0; idx < qi->qf_listcount; ++idx)
  1969. if (qi->qf_lists[idx].qf_count)
  1970. for (i = 0, qfp = qi->qf_lists[idx].qf_start;
  1971. i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
  1972. if (qfp->qf_fnum == curbuf->b_fnum)
  1973. {
  1974. if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
  1975. {
  1976. if (amount == MAXLNUM)
  1977. qfp->qf_cleared = TRUE;
  1978. else
  1979. qfp->qf_lnum += amount;
  1980. }
  1981. else if (amount_after && qfp->qf_lnum > line2)
  1982. qfp->qf_lnum += amount_after;
  1983. }
  1984. }
  1985. /*
  1986. * Make a nice message out of the error character and the error number:
  1987. * char number message
  1988. * e or E 0 " error"
  1989. * w or W 0 " warning"
  1990. * i or I 0 " info"
  1991. * 0 0 ""
  1992. * other 0 " c"
  1993. * e or E n " error n"
  1994. * w or W n " warning n"
  1995. * i or I n " info n"
  1996. * 0 n " error n"
  1997. * other n " c n"
  1998. * 1 x "" :helpgrep
  1999. */
  2000. static char_u *
  2001. qf_types(c, nr)
  2002. int c, nr;
  2003. {
  2004. static char_u buf[20];
  2005. static char_u cc[3];
  2006. char_u *p;
  2007. if (c == 'W' || c == 'w')
  2008. p = (char_u *)" warning";
  2009. else if (c == 'I' || c == 'i')
  2010. p = (char_u *)" info";
  2011. else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
  2012. p = (char_u *)" error";
  2013. else if (c == 0 || c == 1)
  2014. p = (char_u *)"";
  2015. else
  2016. {
  2017. cc[0] = ' ';
  2018. cc[1] = c;
  2019. cc[2] = NUL;
  2020. p = cc;
  2021. }
  2022. if (nr <= 0)
  2023. return p;
  2024. sprintf((char *)buf, "%s %3d", (char *)p, nr);
  2025. return buf;
  2026. }
  2027. #if defined(FEAT_WINDOWS) || defined(PROTO)
  2028. /*
  2029. * ":cwindow": open the quickfix window if we have errors to display,
  2030. * close it if not.
  2031. * ":lwindow": open the location list window if we have locations to display,
  2032. * close it if not.
  2033. */
  2034. void
  2035. ex_cwindow(eap)
  2036. exarg_T *eap;
  2037. {
  2038. qf_info_T *qi = &ql_info;
  2039. win_T *win;
  2040. if (eap->cmdidx == CMD_lwindow)
  2041. {
  2042. qi = GET_LOC_LIST(curwin);
  2043. if (qi == NULL)
  2044. return;
  2045. }
  2046. /* Look for an existing quickfix window. */
  2047. win = qf_find_win(qi);
  2048. /*
  2049. * If a quickfix window is open but we have no errors to display,
  2050. * close the window. If a quickfix window is not open, then open
  2051. * it if we have errors; otherwise, leave it closed.
  2052. */
  2053. if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
  2054. || qi->qf_curlist >= qi->qf_listcount)
  2055. {
  2056. if (win != NULL)
  2057. ex_cclose(eap);
  2058. }
  2059. else if (win == NULL)
  2060. ex_copen(eap);
  2061. }
  2062. /*
  2063. * ":cclose": close the window showing the list of errors.
  2064. * ":lclose": close the window showing the location list
  2065. */
  2066. /*ARGSUSED*/
  2067. void
  2068. ex_cclose(eap)
  2069. exarg_T *eap;
  2070. {
  2071. win_T *win = NULL;
  2072. qf_info_T *qi = &ql_info;
  2073. if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
  2074. {
  2075. qi = GET_LOC_LIST(curwin);
  2076. if (qi == NULL)
  2077. return;
  2078. }
  2079. /* Find existing quickfix window and close it. */
  2080. win = qf_find_win(qi);
  2081. if (win != NULL)
  2082. win_close(win, FALSE);
  2083. }
  2084. /*
  2085. * ":copen": open a window that shows the list of errors.
  2086. * ":lopen": open a window that shows the location list.
  2087. */
  2088. void
  2089. ex_copen(eap)
  2090. exarg_T *eap;
  2091. {
  2092. qf_info_T *qi = &ql_info;
  2093. int height;
  2094. win_T *win;
  2095. tabpage_T *prevtab = curtab;
  2096. buf_T *qf_buf;
  2097. if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
  2098. {
  2099. qi = GET_LOC_LIST(curwin);
  2100. if (qi == NULL)
  2101. {
  2102. EMSG(_(e_loclist));
  2103. return;
  2104. }
  2105. }
  2106. if (eap->addr_count != 0)
  2107. height = eap->line2;
  2108. else
  2109. height = QF_WINHEIGHT;
  2110. #ifdef FEAT_VISUAL
  2111. reset_VIsual_and_resel(); /* stop Visual mode */
  2112. #endif
  2113. #ifdef FEAT_GUI
  2114. need_mouse_correct = TRUE;
  2115. #endif
  2116. /*
  2117. * Find existing quickfix window, or open a new one.
  2118. */
  2119. win = qf_find_win(qi);
  2120. if (win != NULL && cmdmod.tab == 0)
  2121. win_goto(win);
  2122. else
  2123. {
  2124. qf_buf = qf_find_buf(qi);
  2125. /* The current window becomes the previous window afterwards. */
  2126. win = curwin;
  2127. if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
  2128. /* Create the new window at the very bottom. */
  2129. win_goto(lastwin);
  2130. if (win_split(height, WSP_BELOW) == FAIL)
  2131. return; /* not enough room for window */
  2132. #ifdef FEAT_SCROLLBIND
  2133. curwin->w_p_scb = FALSE;
  2134. #endif
  2135. /* Remove the location list for the quickfix window */
  2136. qf_free_all(curwin);
  2137. if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
  2138. {
  2139. /*
  2140. * For the location list window, create a reference to the
  2141. * location list from the window 'win'.
  2142. */
  2143. curwin->w_llist_ref = win->w_llist;
  2144. win->w_llist->qf_refcount++;
  2145. }
  2146. if (qf_buf != NULL)
  2147. /* Use the existing quickfix buffer */
  2148. (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
  2149. ECMD_HIDE + ECMD_OLDBUF);
  2150. else
  2151. {
  2152. /* Create a new quickfix buffer */
  2153. (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
  2154. /* switch off 'swapfile' */
  2155. set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
  2156. set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
  2157. OPT_LOCAL);
  2158. set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
  2159. set_option_value((char_u *)"diff", 0L, NULL, OPT_LOCAL);
  2160. }
  2161. /* Only set the height when still in the same tab page and there is no
  2162. * window to the side. */
  2163. if (curtab == prevtab
  2164. #ifdef FEAT_VERTSPLIT
  2165. && curwin->w_width == Columns
  2166. #endif
  2167. )
  2168. win_setheight(height);
  2169. curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
  2170. if (win_valid(win))
  2171. prevwin = win;
  2172. }
  2173. /*
  2174. * Fill the buffer with the quickfix list.
  2175. */
  2176. qf_fill_buffer(qi);
  2177. curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
  2178. curwin->w_cursor.col = 0;
  2179. check_cursor();
  2180. update_topline(); /* scroll to show the line */
  2181. }
  2182. /*
  2183. * Return the number of the current entry (line number in the quickfix
  2184. * window).
  2185. */
  2186. linenr_T
  2187. qf_current_entry(wp)
  2188. win_T *wp;
  2189. {
  2190. qf_info_T *qi = &ql_info;
  2191. if (IS_LL_WINDOW(wp))
  2192. /* In the location list window, use the referenced location list */
  2193. qi = wp->w_llist_ref;
  2194. return qi->qf_lists[qi->qf_curlist].qf_index;
  2195. }
  2196. /*
  2197. * Update the cursor position in the quickfix window to the current error.
  2198. * Return TRUE if there is a quickfix window.
  2199. */
  2200. static int
  2201. qf_win_pos_update(qi, old_qf_index)
  2202. qf_info_T *qi;
  2203. int old_qf_index; /* previous qf_index or zero */
  2204. {
  2205. win_T *win;
  2206. int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
  2207. /*
  2208. * Put the cursor on the current error in the quickfix window, so that
  2209. * it's viewable.
  2210. */
  2211. win = qf_find_win(qi);
  2212. if (win != NULL
  2213. && qf_index <= win->w_buffer->b_ml.ml_line_count
  2214. && old_qf_index != qf_index)
  2215. {
  2216. win_T *old_curwin = curwin;
  2217. curwin = win;
  2218. curbuf = win->w_buffer;
  2219. if (qf_index > old_qf_index)
  2220. {
  2221. curwin->w_redraw_top = old_qf_index;
  2222. curwin->w_redraw_bot = qf_index;
  2223. }
  2224. else
  2225. {
  2226. curwin->w_redraw_top = qf_index;
  2227. curwin->w_redraw_bot = old_qf_index;
  2228. }
  2229. curwin->w_cursor.lnum = qf_index;
  2230. curwin->w_cursor.col = 0;
  2231. update_topline(); /* scroll to show the line */
  2232. redraw_later(VALID);
  2233. curwin->w_redr_status = TRUE; /* update ruler */
  2234. curwin = old_curwin;
  2235. curbuf = curwin->w_buffer;
  2236. }
  2237. return win != NULL;
  2238. }
  2239. /*
  2240. * Check whether the given window is displaying the specified quickfix/location
  2241. * list buffer
  2242. */
  2243. static int
  2244. is_qf_win(win, qi)
  2245. win_T *win;
  2246. qf_info_T *qi;
  2247. {
  2248. /*
  2249. * A window displaying the quickfix buffer will have the w_llist_ref field
  2250. * set to NULL.
  2251. * A window displaying a location list buffer will have the w_llist_ref
  2252. * pointing to the location list.
  2253. */
  2254. if (bt_quickfix(win->w_buffer))
  2255. if ((qi == &ql_info && win->w_llist_ref == NULL)
  2256. || (qi != &ql_info && win->w_llist_ref == qi))
  2257. return TRUE;
  2258. return FALSE;
  2259. }
  2260. /*
  2261. * Find a window displaying the quickfix/location list 'qi'
  2262. * Searches in only the windows opened in the current tab.
  2263. */
  2264. static win_T *
  2265. qf_find_win(qi)
  2266. qf_info_T *qi;
  2267. {
  2268. win_T *win;
  2269. FOR_ALL_WINDOWS(win)
  2270. if (is_qf_win(win, qi))
  2271. break;
  2272. return win;
  2273. }
  2274. /*
  2275. * Find a quickfix buffer.
  2276. * Searches in windows opened in all the tabs.
  2277. */
  2278. static buf_T *
  2279. qf_find_buf(qi)
  2280. qf_info_T *qi;
  2281. {
  2282. tabpage_T *tp;
  2283. win_T *win;
  2284. FOR_ALL_TAB_WINDOWS(tp, win)
  2285. if (is_qf_win(win, qi))
  2286. return win->w_buffer;
  2287. return NULL;
  2288. }
  2289. /*
  2290. * Find the quickfix buffer. If it exists, update the contents.
  2291. */
  2292. static void
  2293. qf_update_buffer(qi)
  2294. qf_info_T *qi;
  2295. {
  2296. buf_T *buf;
  2297. aco_save_T aco;
  2298. /* Check if a buffer for the quickfix list exists. Update it. */
  2299. buf = qf_find_buf(qi);
  2300. if (buf != NULL)
  2301. {
  2302. /* set curwin/curbuf to buf and save a few things */
  2303. aucmd_prepbuf(&aco, buf);
  2304. qf_fill_buffer(qi);
  2305. /* restore curwin/curbuf and a few other things */
  2306. aucmd_restbuf(&aco);
  2307. (void)qf_win_pos_update(qi, 0);
  2308. }
  2309. }
  2310. /*
  2311. * Fill current buffer with quickfix errors, replacing any previous contents.
  2312. * curbuf must be the quickfix buffer!
  2313. */
  2314. static void
  2315. qf_fill_buffer(qi)
  2316. qf_info_T *qi;
  2317. {
  2318. linenr_T lnum;
  2319. qfline_T *qfp;
  2320. buf_T *errbuf;
  2321. int len;
  2322. int old_KeyTyped = KeyTyped;
  2323. /* delete all existing lines */
  2324. while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
  2325. (void)ml_delete((linenr_T)1, FALSE);
  2326. /* Check if there is anything to display */
  2327. if (qi->qf_curlist < qi->qf_listcount)
  2328. {
  2329. /* Add one line for each error */
  2330. qfp = qi->qf_lists[qi->qf_curlist].qf_start;
  2331. for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
  2332. {
  2333. if (qfp->qf_fnum != 0
  2334. && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
  2335. && errbuf->b_fname != NULL)
  2336. {
  2337. if (qfp->qf_type == 1) /* :helpgrep */
  2338. STRCPY(IObuff, gettail(errbuf->b_fname));
  2339. else
  2340. STRCPY(IObuff, errbuf->b_fname);
  2341. len = (int)STRLEN(IObuff);
  2342. }
  2343. else
  2344. len = 0;
  2345. IObuff[len++] = '|';
  2346. if (qfp->qf_lnum > 0)
  2347. {
  2348. sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
  2349. len += (int)STRLEN(IObuff + len);
  2350. if (qfp->qf_col > 0)
  2351. {
  2352. sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
  2353. len += (int)STRLEN(IObuff + len);
  2354. }
  2355. sprintf((char *)IObuff + len, "%s",
  2356. (char *)qf_types(qfp->qf_type, qfp->qf_nr));
  2357. len += (int)STRLEN(IObuff + len);
  2358. }
  2359. else if (qfp->qf_pattern != NULL)
  2360. {
  2361. qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
  2362. len += (int)STRLEN(IObuff + len);
  2363. }
  2364. IObuff[len++] = '|';
  2365. IObuff[len++] = ' ';
  2366. /* Remove newlines and leading whitespace from the text.
  2367. * For an unrecognized line keep the indent, the compiler may
  2368. * mark a word with ^^^^. */
  2369. qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
  2370. IObuff + len, IOSIZE - len);
  2371. if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
  2372. == FAIL)
  2373. break;
  2374. qfp = qfp->qf_next;
  2375. }
  2376. /* Delete the empty line which is now at the end */
  2377. (void)ml_delete(lnum + 1, FALSE);
  2378. }
  2379. /* correct cursor position */
  2380. check_lnums(TRUE);
  2381. /* Set the 'filetype' to "qf" each time after filling the buffer. This
  2382. * resembles reading a file into a buffer, it's more logical when using
  2383. * autocommands. */
  2384. set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
  2385. curbuf->b_p_ma = FALSE;
  2386. #ifdef FEAT_AUTOCMD
  2387. apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
  2388. FALSE, curbuf);
  2389. apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
  2390. FALSE, curbuf);
  2391. #endif
  2392. /* make sure it will be redrawn */
  2393. redraw_curbuf_later(NOT_VALID);
  2394. /* Restore KeyTyped, setting 'filetype' may reset it. */
  2395. KeyTyped = old_KeyTyped;
  2396. }
  2397. #endif /* FEAT_WINDOWS */
  2398. /*
  2399. * Return TRUE if "buf" is the quickfix buffer.
  2400. */
  2401. int
  2402. bt_quickfix(buf)
  2403. buf_T *buf;
  2404. {
  2405. return (buf->b_p_bt[0] == 'q');
  2406. }
  2407. /*
  2408. * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
  2409. * This means the buffer name is not a file name.
  2410. */
  2411. int
  2412. bt_nofile(buf)
  2413. buf_T *buf;
  2414. {
  2415. return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
  2416. || buf->b_p_bt[0] == 'a';
  2417. }
  2418. /*
  2419. * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
  2420. */
  2421. int
  2422. bt_dontwrite(buf)
  2423. buf_T *buf;
  2424. {
  2425. return (buf->b_p_bt[0] == 'n');
  2426. }
  2427. int
  2428. bt_dontwrite_msg(buf)
  2429. buf_T *buf;
  2430. {
  2431. if (bt_dontwrite(buf))
  2432. {
  2433. EMSG(_("E382: Cannot write, 'buftype' option is set"));
  2434. return TR