PageRenderTime 92ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/vim7.0.243/src/quickfix.c

#
C | 2655 lines | 1977 code | 219 blank | 459 comment | 768 complexity | e1a46df193033ed9f508caf568e19b38 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 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));
  97. static void qf_msg __ARGS((qf_info_T *qi));
  98. static void qf_free __ARGS((qf_info_T *qi, int idx));
  99. static char_u *qf_types __ARGS((int, int));
  100. static int qf_get_fnum __ARGS((char_u *, char_u *));
  101. static char_u *qf_push_dir __ARGS((char_u *, struct dir_stack_T **));
  102. static char_u *qf_pop_dir __ARGS((struct dir_stack_T **));
  103. static char_u *qf_guess_filepath __ARGS((char_u *));
  104. static void qf_fmt_text __ARGS((char_u *text, char_u *buf, int bufsize));
  105. static void qf_clean_dir_stack __ARGS((struct dir_stack_T **));
  106. #ifdef FEAT_WINDOWS
  107. static int qf_win_pos_update __ARGS((qf_info_T *qi, int old_qf_index));
  108. static int is_qf_win __ARGS((win_T *win, qf_info_T *qi));
  109. static win_T *qf_find_win __ARGS((qf_info_T *qi));
  110. static buf_T *qf_find_buf __ARGS((qf_info_T *qi));
  111. static void qf_update_buffer __ARGS((qf_info_T *qi));
  112. static void qf_fill_buffer __ARGS((qf_info_T *qi));
  113. #endif
  114. static char_u *get_mef_name __ARGS((void));
  115. static buf_T *load_dummy_buffer __ARGS((char_u *fname));
  116. static void wipe_dummy_buffer __ARGS((buf_T *buf));
  117. static void unload_dummy_buffer __ARGS((buf_T *buf));
  118. static qf_info_T *ll_get_or_alloc_list __ARGS((win_T *));
  119. /* Quickfix window check helper macro */
  120. #define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL)
  121. /* Location list window check helper macro */
  122. #define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL)
  123. /*
  124. * Return location list for window 'wp'
  125. * For location list window, return the referenced location list
  126. */
  127. #define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist)
  128. /*
  129. * Read the errorfile "efile" into memory, line by line, building the error
  130. * list.
  131. * Return -1 for error, number of errors for success.
  132. */
  133. int
  134. qf_init(wp, efile, errorformat, newlist)
  135. win_T *wp;
  136. char_u *efile;
  137. char_u *errorformat;
  138. int newlist; /* TRUE: start a new error list */
  139. {
  140. qf_info_T *qi = &ql_info;
  141. if (efile == NULL)
  142. return FAIL;
  143. if (wp != NULL)
  144. {
  145. qi = ll_get_or_alloc_list(wp);
  146. if (qi == NULL)
  147. return FAIL;
  148. }
  149. return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist,
  150. (linenr_T)0, (linenr_T)0);
  151. }
  152. /*
  153. * Read the errorfile "efile" into memory, line by line, building the error
  154. * list.
  155. * Alternative: when "efile" is null read errors from buffer "buf".
  156. * Always use 'errorformat' from "buf" if there is a local value.
  157. * Then lnumfirst and lnumlast specify the range of lines to use.
  158. * Return -1 for error, number of errors for success.
  159. */
  160. static int
  161. qf_init_ext(qi, efile, buf, tv, errorformat, newlist, lnumfirst, lnumlast)
  162. qf_info_T *qi;
  163. char_u *efile;
  164. buf_T *buf;
  165. typval_T *tv;
  166. char_u *errorformat;
  167. int newlist; /* TRUE: start a new error list */
  168. linenr_T lnumfirst; /* first line number to use */
  169. linenr_T lnumlast; /* last line number to use */
  170. {
  171. char_u *namebuf;
  172. char_u *errmsg;
  173. char_u *pattern;
  174. char_u *fmtstr = NULL;
  175. int col = 0;
  176. char_u use_viscol = FALSE;
  177. int type = 0;
  178. int valid;
  179. linenr_T buflnum = lnumfirst;
  180. long lnum = 0L;
  181. int enr = 0;
  182. FILE *fd = NULL;
  183. qfline_T *qfprev = NULL; /* init to make SASC shut up */
  184. char_u *efmp;
  185. efm_T *fmt_first = NULL;
  186. efm_T *fmt_last = NULL;
  187. efm_T *fmt_ptr;
  188. efm_T *fmt_start = NULL;
  189. char_u *efm;
  190. char_u *ptr;
  191. char_u *srcptr;
  192. int len;
  193. int i;
  194. int round;
  195. int idx = 0;
  196. int multiline = FALSE;
  197. int multiignore = FALSE;
  198. int multiscan = FALSE;
  199. int retval = -1; /* default: return error flag */
  200. char_u *directory = NULL;
  201. char_u *currfile = NULL;
  202. char_u *tail = NULL;
  203. char_u *p_str = NULL;
  204. listitem_T *p_li = NULL;
  205. struct dir_stack_T *file_stack = NULL;
  206. regmatch_T regmatch;
  207. static struct fmtpattern
  208. {
  209. char_u convchar;
  210. char *pattern;
  211. } fmt_pat[FMT_PATTERNS] =
  212. {
  213. {'f', ".\\+"}, /* only used when at end */
  214. {'n', "\\d\\+"},
  215. {'l', "\\d\\+"},
  216. {'c', "\\d\\+"},
  217. {'t', "."},
  218. {'m', ".\\+"},
  219. {'r', ".*"},
  220. {'p', "[- .]*"},
  221. {'v', "\\d\\+"},
  222. {'s', ".\\+"}
  223. };
  224. namebuf = alloc(CMDBUFFSIZE + 1);
  225. errmsg = alloc(CMDBUFFSIZE + 1);
  226. pattern = alloc(CMDBUFFSIZE + 1);
  227. if (namebuf == NULL || errmsg == NULL || pattern == NULL)
  228. goto qf_init_end;
  229. if (efile != NULL && (fd = mch_fopen((char *)efile, "r")) == NULL)
  230. {
  231. EMSG2(_(e_openerrf), efile);
  232. goto qf_init_end;
  233. }
  234. if (newlist || qi->qf_curlist == qi->qf_listcount)
  235. /* make place for a new list */
  236. qf_new_list(qi);
  237. else if (qi->qf_lists[qi->qf_curlist].qf_count > 0)
  238. /* Adding to existing list, find last entry. */
  239. for (qfprev = qi->qf_lists[qi->qf_curlist].qf_start;
  240. qfprev->qf_next != qfprev; qfprev = qfprev->qf_next)
  241. ;
  242. /*
  243. * Each part of the format string is copied and modified from errorformat to
  244. * regex prog. Only a few % characters are allowed.
  245. */
  246. /* Use the local value of 'errorformat' if it's set. */
  247. if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL)
  248. efm = buf->b_p_efm;
  249. else
  250. efm = errorformat;
  251. /*
  252. * Get some space to modify the format string into.
  253. */
  254. i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2);
  255. for (round = FMT_PATTERNS; round > 0; )
  256. i += (int)STRLEN(fmt_pat[--round].pattern);
  257. #ifdef COLON_IN_FILENAME
  258. i += 12; /* "%f" can become twelve chars longer */
  259. #else
  260. i += 2; /* "%f" can become two chars longer */
  261. #endif
  262. if ((fmtstr = alloc(i)) == NULL)
  263. goto error2;
  264. while (efm[0] != NUL)
  265. {
  266. /*
  267. * Allocate a new eformat structure and put it at the end of the list
  268. */
  269. fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T));
  270. if (fmt_ptr == NULL)
  271. goto error2;
  272. if (fmt_first == NULL) /* first one */
  273. fmt_first = fmt_ptr;
  274. else
  275. fmt_last->next = fmt_ptr;
  276. fmt_last = fmt_ptr;
  277. /*
  278. * Isolate one part in the 'errorformat' option
  279. */
  280. for (len = 0; efm[len] != NUL && efm[len] != ','; ++len)
  281. if (efm[len] == '\\' && efm[len + 1] != NUL)
  282. ++len;
  283. /*
  284. * Build regexp pattern from current 'errorformat' option
  285. */
  286. ptr = fmtstr;
  287. *ptr++ = '^';
  288. round = 0;
  289. for (efmp = efm; efmp < efm + len; ++efmp)
  290. {
  291. if (*efmp == '%')
  292. {
  293. ++efmp;
  294. for (idx = 0; idx < FMT_PATTERNS; ++idx)
  295. if (fmt_pat[idx].convchar == *efmp)
  296. break;
  297. if (idx < FMT_PATTERNS)
  298. {
  299. if (fmt_ptr->addr[idx])
  300. {
  301. sprintf((char *)errmsg,
  302. _("E372: Too many %%%c in format string"), *efmp);
  303. EMSG(errmsg);
  304. goto error2;
  305. }
  306. if ((idx
  307. && idx < 6
  308. && vim_strchr((char_u *)"DXOPQ",
  309. fmt_ptr->prefix) != NULL)
  310. || (idx == 6
  311. && vim_strchr((char_u *)"OPQ",
  312. fmt_ptr->prefix) == NULL))
  313. {
  314. sprintf((char *)errmsg,
  315. _("E373: Unexpected %%%c in format string"), *efmp);
  316. EMSG(errmsg);
  317. goto error2;
  318. }
  319. fmt_ptr->addr[idx] = (char_u)++round;
  320. *ptr++ = '\\';
  321. *ptr++ = '(';
  322. #ifdef BACKSLASH_IN_FILENAME
  323. if (*efmp == 'f')
  324. {
  325. /* Also match "c:" in the file name, even when
  326. * checking for a colon next: "%f:".
  327. * "\%(\a:\)\=" */
  328. STRCPY(ptr, "\\%(\\a:\\)\\=");
  329. ptr += 10;
  330. }
  331. #endif
  332. if (*efmp == 'f' && efmp[1] != NUL)
  333. {
  334. if (efmp[1] != '\\' && efmp[1] != '%')
  335. {
  336. /* A file name may contain spaces, but this isn't
  337. * in "\f". For "%f:%l:%m" there may be a ":" in
  338. * the file name. Use ".\{-1,}x" instead (x is
  339. * the next character), the requirement that :999:
  340. * follows should work. */
  341. STRCPY(ptr, ".\\{-1,}");
  342. ptr += 7;
  343. }
  344. else
  345. {
  346. /* File name followed by '\\' or '%': include as
  347. * many file name chars as possible. */
  348. STRCPY(ptr, "\\f\\+");
  349. ptr += 4;
  350. }
  351. }
  352. else
  353. {
  354. srcptr = (char_u *)fmt_pat[idx].pattern;
  355. while ((*ptr = *srcptr++) != NUL)
  356. ++ptr;
  357. }
  358. *ptr++ = '\\';
  359. *ptr++ = ')';
  360. }
  361. else if (*efmp == '*')
  362. {
  363. if (*++efmp == '[' || *efmp == '\\')
  364. {
  365. if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */
  366. {
  367. if (efmp[1] == '^')
  368. *ptr++ = *++efmp;
  369. if (efmp < efm + len)
  370. {
  371. *ptr++ = *++efmp; /* could be ']' */
  372. while (efmp < efm + len
  373. && (*ptr++ = *++efmp) != ']')
  374. /* skip */;
  375. if (efmp == efm + len)
  376. {
  377. EMSG(_("E374: Missing ] in format string"));
  378. goto error2;
  379. }
  380. }
  381. }
  382. else if (efmp < efm + len) /* %*\D, %*\s etc. */
  383. *ptr++ = *++efmp;
  384. *ptr++ = '\\';
  385. *ptr++ = '+';
  386. }
  387. else
  388. {
  389. /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */
  390. sprintf((char *)errmsg,
  391. _("E375: Unsupported %%%c in format string"), *efmp);
  392. EMSG(errmsg);
  393. goto error2;
  394. }
  395. }
  396. else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL)
  397. *ptr++ = *efmp; /* regexp magic characters */
  398. else if (*efmp == '#')
  399. *ptr++ = '*';
  400. else if (*efmp == '>')
  401. fmt_ptr->conthere = TRUE;
  402. else if (efmp == efm + 1) /* analyse prefix */
  403. {
  404. if (vim_strchr((char_u *)"+-", *efmp) != NULL)
  405. fmt_ptr->flags = *efmp++;
  406. if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL)
  407. fmt_ptr->prefix = *efmp;
  408. else
  409. {
  410. sprintf((char *)errmsg,
  411. _("E376: Invalid %%%c in format string prefix"), *efmp);
  412. EMSG(errmsg);
  413. goto error2;
  414. }
  415. }
  416. else
  417. {
  418. sprintf((char *)errmsg,
  419. _("E377: Invalid %%%c in format string"), *efmp);
  420. EMSG(errmsg);
  421. goto error2;
  422. }
  423. }
  424. else /* copy normal character */
  425. {
  426. if (*efmp == '\\' && efmp + 1 < efm + len)
  427. ++efmp;
  428. else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL)
  429. *ptr++ = '\\'; /* escape regexp atoms */
  430. if (*efmp)
  431. *ptr++ = *efmp;
  432. }
  433. }
  434. *ptr++ = '$';
  435. *ptr = NUL;
  436. if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL)
  437. goto error2;
  438. /*
  439. * Advance to next part
  440. */
  441. efm = skip_to_option_part(efm + len); /* skip comma and spaces */
  442. }
  443. if (fmt_first == NULL) /* nothing found */
  444. {
  445. EMSG(_("E378: 'errorformat' contains no pattern"));
  446. goto error2;
  447. }
  448. /*
  449. * got_int is reset here, because it was probably set when killing the
  450. * ":make" command, but we still want to read the errorfile then.
  451. */
  452. got_int = FALSE;
  453. /* Always ignore case when looking for a matching error. */
  454. regmatch.rm_ic = TRUE;
  455. if (tv != NULL)
  456. {
  457. if (tv->v_type == VAR_STRING)
  458. p_str = tv->vval.v_string;
  459. else if (tv->v_type == VAR_LIST)
  460. p_li = tv->vval.v_list->lv_first;
  461. }
  462. /*
  463. * Read the lines in the error file one by one.
  464. * Try to recognize one of the error formats in each line.
  465. */
  466. while (!got_int)
  467. {
  468. /* Get the next line. */
  469. if (fd == NULL)
  470. {
  471. if (tv != NULL)
  472. {
  473. if (tv->v_type == VAR_STRING)
  474. {
  475. /* Get the next line from the supplied string */
  476. char_u *p;
  477. if (!*p_str) /* Reached the end of the string */
  478. break;
  479. p = vim_strchr(p_str, '\n');
  480. if (p)
  481. len = (int)(p - p_str + 1);
  482. else
  483. len = (int)STRLEN(p_str);
  484. if (len > CMDBUFFSIZE - 2)
  485. vim_strncpy(IObuff, p_str, CMDBUFFSIZE - 2);
  486. else
  487. vim_strncpy(IObuff, p_str, len);
  488. p_str += len;
  489. }
  490. else if (tv->v_type == VAR_LIST)
  491. {
  492. /* Get the next line from the supplied list */
  493. while (p_li && p_li->li_tv.v_type != VAR_STRING)
  494. p_li = p_li->li_next; /* Skip non-string items */
  495. if (!p_li) /* End of the list */
  496. break;
  497. len = (int)STRLEN(p_li->li_tv.vval.v_string);
  498. if (len > CMDBUFFSIZE - 2)
  499. len = CMDBUFFSIZE - 2;
  500. vim_strncpy(IObuff, p_li->li_tv.vval.v_string, len);
  501. p_li = p_li->li_next; /* next item */
  502. }
  503. }
  504. else
  505. {
  506. /* Get the next line from the supplied buffer */
  507. if (buflnum > lnumlast)
  508. break;
  509. vim_strncpy(IObuff, ml_get_buf(buf, buflnum++, FALSE),
  510. CMDBUFFSIZE - 2);
  511. }
  512. }
  513. else if (fgets((char *)IObuff, CMDBUFFSIZE - 2, fd) == NULL)
  514. break;
  515. IObuff[CMDBUFFSIZE - 2] = NUL; /* for very long lines */
  516. if ((efmp = vim_strrchr(IObuff, '\n')) != NULL)
  517. *efmp = NUL;
  518. #ifdef USE_CRNL
  519. if ((efmp = vim_strrchr(IObuff, '\r')) != NULL)
  520. *efmp = NUL;
  521. #endif
  522. /* If there was no %> item start at the first pattern */
  523. if (fmt_start == NULL)
  524. fmt_ptr = fmt_first;
  525. else
  526. {
  527. fmt_ptr = fmt_start;
  528. fmt_start = NULL;
  529. }
  530. /*
  531. * Try to match each part of 'errorformat' until we find a complete
  532. * match or no match.
  533. */
  534. valid = TRUE;
  535. restofline:
  536. for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next)
  537. {
  538. idx = fmt_ptr->prefix;
  539. if (multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL)
  540. continue;
  541. namebuf[0] = NUL;
  542. pattern[0] = NUL;
  543. if (!multiscan)
  544. errmsg[0] = NUL;
  545. lnum = 0;
  546. col = 0;
  547. use_viscol = FALSE;
  548. enr = -1;
  549. type = 0;
  550. tail = NULL;
  551. regmatch.regprog = fmt_ptr->prog;
  552. if (vim_regexec(&regmatch, IObuff, (colnr_T)0))
  553. {
  554. if ((idx == 'C' || idx == 'Z') && !multiline)
  555. continue;
  556. if (vim_strchr((char_u *)"EWI", idx) != NULL)
  557. type = idx;
  558. else
  559. type = 0;
  560. /*
  561. * Extract error message data from matched line.
  562. * We check for an actual submatch, because "\[" and "\]" in
  563. * the 'errorformat' may cause the wrong submatch to be used.
  564. */
  565. if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */
  566. {
  567. int c;
  568. if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
  569. continue;
  570. /* Expand ~/file and $HOME/file to full path. */
  571. c = *regmatch.endp[i];
  572. *regmatch.endp[i] = NUL;
  573. expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE);
  574. *regmatch.endp[i] = c;
  575. if (vim_strchr((char_u *)"OPQ", idx) != NULL
  576. && mch_getperm(namebuf) == -1)
  577. continue;
  578. }
  579. if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */
  580. {
  581. if (regmatch.startp[i] == NULL)
  582. continue;
  583. enr = (int)atol((char *)regmatch.startp[i]);
  584. }
  585. if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */
  586. {
  587. if (regmatch.startp[i] == NULL)
  588. continue;
  589. lnum = atol((char *)regmatch.startp[i]);
  590. }
  591. if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */
  592. {
  593. if (regmatch.startp[i] == NULL)
  594. continue;
  595. col = (int)atol((char *)regmatch.startp[i]);
  596. }
  597. if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */
  598. {
  599. if (regmatch.startp[i] == NULL)
  600. continue;
  601. type = *regmatch.startp[i];
  602. }
  603. if (fmt_ptr->flags == '+' && !multiscan) /* %+ */
  604. STRCPY(errmsg, IObuff);
  605. else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */
  606. {
  607. if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
  608. continue;
  609. len = (int)(regmatch.endp[i] - regmatch.startp[i]);
  610. vim_strncpy(errmsg, regmatch.startp[i], len);
  611. }
  612. if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */
  613. {
  614. if (regmatch.startp[i] == NULL)
  615. continue;
  616. tail = regmatch.startp[i];
  617. }
  618. if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */
  619. {
  620. if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
  621. continue;
  622. col = (int)(regmatch.endp[i] - regmatch.startp[i] + 1);
  623. if (*((char_u *)regmatch.startp[i]) != TAB)
  624. use_viscol = TRUE;
  625. }
  626. if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */
  627. {
  628. if (regmatch.startp[i] == NULL)
  629. continue;
  630. col = (int)atol((char *)regmatch.startp[i]);
  631. use_viscol = TRUE;
  632. }
  633. if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */
  634. {
  635. if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL)
  636. continue;
  637. len = (int)(regmatch.endp[i] - regmatch.startp[i]);
  638. if (len > CMDBUFFSIZE - 5)
  639. len = CMDBUFFSIZE - 5;
  640. STRCPY(pattern, "^\\V");
  641. STRNCAT(pattern, regmatch.startp[i], len);
  642. pattern[len + 3] = '\\';
  643. pattern[len + 4] = '$';
  644. pattern[len + 5] = NUL;
  645. }
  646. break;
  647. }
  648. }
  649. multiscan = FALSE;
  650. if (fmt_ptr == NULL || idx == 'D' || idx == 'X')
  651. {
  652. if (fmt_ptr != NULL)
  653. {
  654. if (idx == 'D') /* enter directory */
  655. {
  656. if (*namebuf == NUL)
  657. {
  658. EMSG(_("E379: Missing or empty directory name"));
  659. goto error2;
  660. }
  661. if ((directory = qf_push_dir(namebuf, &dir_stack)) == NULL)
  662. goto error2;
  663. }
  664. else if (idx == 'X') /* leave directory */
  665. directory = qf_pop_dir(&dir_stack);
  666. }
  667. namebuf[0] = NUL; /* no match found, remove file name */
  668. lnum = 0; /* don't jump to this line */
  669. valid = FALSE;
  670. STRCPY(errmsg, IObuff); /* copy whole line to error message */
  671. if (fmt_ptr == NULL)
  672. multiline = multiignore = FALSE;
  673. }
  674. else if (fmt_ptr != NULL)
  675. {
  676. /* honor %> item */
  677. if (fmt_ptr->conthere)
  678. fmt_start = fmt_ptr;
  679. if (vim_strchr((char_u *)"AEWI", idx) != NULL)
  680. multiline = TRUE; /* start of a multi-line message */
  681. else if (vim_strchr((char_u *)"CZ", idx) != NULL)
  682. { /* continuation of multi-line msg */
  683. if (qfprev == NULL)
  684. goto error2;
  685. if (*errmsg && !multiignore)
  686. {
  687. len = (int)STRLEN(qfprev->qf_text);
  688. if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2)))
  689. == NULL)
  690. goto error2;
  691. STRCPY(ptr, qfprev->qf_text);
  692. vim_free(qfprev->qf_text);
  693. qfprev->qf_text = ptr;
  694. *(ptr += len) = '\n';
  695. STRCPY(++ptr, errmsg);
  696. }
  697. if (qfprev->qf_nr == -1)
  698. qfprev->qf_nr = enr;
  699. if (vim_isprintc(type) && !qfprev->qf_type)
  700. qfprev->qf_type = type; /* only printable chars allowed */
  701. if (!qfprev->qf_lnum)
  702. qfprev->qf_lnum = lnum;
  703. if (!qfprev->qf_col)
  704. qfprev->qf_col = col;
  705. qfprev->qf_viscol = use_viscol;
  706. if (!qfprev->qf_fnum)
  707. qfprev->qf_fnum = qf_get_fnum(directory,
  708. *namebuf || directory ? namebuf
  709. : currfile && valid ? currfile : 0);
  710. if (idx == 'Z')
  711. multiline = multiignore = FALSE;
  712. line_breakcheck();
  713. continue;
  714. }
  715. else if (vim_strchr((char_u *)"OPQ", idx) != NULL)
  716. {
  717. /* global file names */
  718. valid = FALSE;
  719. if (*namebuf == NUL || mch_getperm(namebuf) >= 0)
  720. {
  721. if (*namebuf && idx == 'P')
  722. currfile = qf_push_dir(namebuf, &file_stack);
  723. else if (idx == 'Q')
  724. currfile = qf_pop_dir(&file_stack);
  725. *namebuf = NUL;
  726. if (tail && *tail)
  727. {
  728. STRCPY(IObuff, skipwhite(tail));
  729. multiscan = TRUE;
  730. goto restofline;
  731. }
  732. }
  733. }
  734. if (fmt_ptr->flags == '-') /* generally exclude this line */
  735. {
  736. if (multiline)
  737. multiignore = TRUE; /* also exclude continuation lines */
  738. continue;
  739. }
  740. }
  741. if (qf_add_entry(qi, &qfprev,
  742. directory,
  743. (*namebuf || directory)
  744. ? namebuf
  745. : ((currfile && valid) ? currfile : (char_u *)NULL),
  746. 0,
  747. errmsg,
  748. lnum,
  749. col,
  750. use_viscol,
  751. pattern,
  752. enr,
  753. type,
  754. valid) == FAIL)
  755. goto error2;
  756. line_breakcheck();
  757. }
  758. if (fd == NULL || !ferror(fd))
  759. {
  760. if (qi->qf_lists[qi->qf_curlist].qf_index == 0)
  761. {
  762. /* no valid entry found */
  763. qi->qf_lists[qi->qf_curlist].qf_ptr =
  764. qi->qf_lists[qi->qf_curlist].qf_start;
  765. qi->qf_lists[qi->qf_curlist].qf_index = 1;
  766. qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE;
  767. }
  768. else
  769. {
  770. qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE;
  771. if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL)
  772. qi->qf_lists[qi->qf_curlist].qf_ptr =
  773. qi->qf_lists[qi->qf_curlist].qf_start;
  774. }
  775. /* return number of matches */
  776. retval = qi->qf_lists[qi->qf_curlist].qf_count;
  777. goto qf_init_ok;
  778. }
  779. EMSG(_(e_readerrf));
  780. error2:
  781. qf_free(qi, qi->qf_curlist);
  782. qi->qf_listcount--;
  783. if (qi->qf_curlist > 0)
  784. --qi->qf_curlist;
  785. qf_init_ok:
  786. if (fd != NULL)
  787. fclose(fd);
  788. for (fmt_ptr = fmt_first; fmt_ptr != NULL; fmt_ptr = fmt_first)
  789. {
  790. fmt_first = fmt_ptr->next;
  791. vim_free(fmt_ptr->prog);
  792. vim_free(fmt_ptr);
  793. }
  794. qf_clean_dir_stack(&dir_stack);
  795. qf_clean_dir_stack(&file_stack);
  796. qf_init_end:
  797. vim_free(namebuf);
  798. vim_free(errmsg);
  799. vim_free(pattern);
  800. vim_free(fmtstr);
  801. #ifdef FEAT_WINDOWS
  802. qf_update_buffer(qi);
  803. #endif
  804. return retval;
  805. }
  806. /*
  807. * Prepare for adding a new quickfix list.
  808. */
  809. static void
  810. qf_new_list(qi)
  811. qf_info_T *qi;
  812. {
  813. int i;
  814. /*
  815. * If the current entry is not the last entry, delete entries below
  816. * the current entry. This makes it possible to browse in a tree-like
  817. * way with ":grep'.
  818. */
  819. while (qi->qf_listcount > qi->qf_curlist + 1)
  820. qf_free(qi, --qi->qf_listcount);
  821. /*
  822. * When the stack is full, remove to oldest entry
  823. * Otherwise, add a new entry.
  824. */
  825. if (qi->qf_listcount == LISTCOUNT)
  826. {
  827. qf_free(qi, 0);
  828. for (i = 1; i < LISTCOUNT; ++i)
  829. qi->qf_lists[i - 1] = qi->qf_lists[i];
  830. qi->qf_curlist = LISTCOUNT - 1;
  831. }
  832. else
  833. qi->qf_curlist = qi->qf_listcount++;
  834. qi->qf_lists[qi->qf_curlist].qf_index = 0;
  835. qi->qf_lists[qi->qf_curlist].qf_count = 0;
  836. }
  837. /*
  838. * Free a location list
  839. */
  840. static void
  841. ll_free_all(pqi)
  842. qf_info_T **pqi;
  843. {
  844. int i;
  845. qf_info_T *qi;
  846. qi = *pqi;
  847. if (qi == NULL)
  848. return;
  849. *pqi = NULL; /* Remove reference to this list */
  850. qi->qf_refcount--;
  851. if (qi->qf_refcount < 1)
  852. {
  853. /* No references to this location list */
  854. for (i = 0; i < qi->qf_listcount; ++i)
  855. qf_free(qi, i);
  856. vim_free(qi);
  857. }
  858. }
  859. void
  860. qf_free_all(wp)
  861. win_T *wp;
  862. {
  863. int i;
  864. qf_info_T *qi = &ql_info;
  865. if (wp != NULL)
  866. {
  867. /* location list */
  868. ll_free_all(&wp->w_llist);
  869. ll_free_all(&wp->w_llist_ref);
  870. }
  871. else
  872. /* quickfix list */
  873. for (i = 0; i < qi->qf_listcount; ++i)
  874. qf_free(qi, i);
  875. }
  876. /*
  877. * Add an entry to the end of the list of errors.
  878. * Returns OK or FAIL.
  879. */
  880. static int
  881. qf_add_entry(qi, prevp, dir, fname, bufnum, mesg, lnum, col, vis_col, pattern,
  882. nr, type, valid)
  883. qf_info_T *qi; /* quickfix list */
  884. qfline_T **prevp; /* pointer to previously added entry or NULL */
  885. char_u *dir; /* optional directory name */
  886. char_u *fname; /* file name or NULL */
  887. int bufnum; /* buffer number or zero */
  888. char_u *mesg; /* message */
  889. long lnum; /* line number */
  890. int col; /* column */
  891. int vis_col; /* using visual column */
  892. char_u *pattern; /* search pattern */
  893. int nr; /* error number */
  894. int type; /* type character */
  895. int valid; /* valid entry */
  896. {
  897. qfline_T *qfp;
  898. if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL)
  899. return FAIL;
  900. if (bufnum != 0)
  901. qfp->qf_fnum = bufnum;
  902. else
  903. qfp->qf_fnum = qf_get_fnum(dir, fname);
  904. if ((qfp->qf_text = vim_strsave(mesg)) == NULL)
  905. {
  906. vim_free(qfp);
  907. return FAIL;
  908. }
  909. qfp->qf_lnum = lnum;
  910. qfp->qf_col = col;
  911. qfp->qf_viscol = vis_col;
  912. if (pattern == NULL || *pattern == NUL)
  913. qfp->qf_pattern = NULL;
  914. else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL)
  915. {
  916. vim_free(qfp->qf_text);
  917. vim_free(qfp);
  918. return FAIL;
  919. }
  920. qfp->qf_nr = nr;
  921. if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */
  922. type = 0;
  923. qfp->qf_type = type;
  924. qfp->qf_valid = valid;
  925. if (qi->qf_lists[qi->qf_curlist].qf_count == 0)
  926. /* first element in the list */
  927. {
  928. qi->qf_lists[qi->qf_curlist].qf_start = qfp;
  929. qfp->qf_prev = qfp; /* first element points to itself */
  930. }
  931. else
  932. {
  933. qfp->qf_prev = *prevp;
  934. (*prevp)->qf_next = qfp;
  935. }
  936. qfp->qf_next = qfp; /* last element points to itself */
  937. qfp->qf_cleared = FALSE;
  938. *prevp = qfp;
  939. ++qi->qf_lists[qi->qf_curlist].qf_count;
  940. if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid)
  941. /* first valid entry */
  942. {
  943. qi->qf_lists[qi->qf_curlist].qf_index =
  944. qi->qf_lists[qi->qf_curlist].qf_count;
  945. qi->qf_lists[qi->qf_curlist].qf_ptr = qfp;
  946. }
  947. return OK;
  948. }
  949. /*
  950. * Allocate a new location list
  951. */
  952. static qf_info_T *
  953. ll_new_list()
  954. {
  955. qf_info_T *qi;
  956. qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T));
  957. if (qi != NULL)
  958. {
  959. vim_memset(qi, 0, (size_t)(sizeof(qf_info_T)));
  960. qi->qf_refcount++;
  961. }
  962. return qi;
  963. }
  964. /*
  965. * Return the location list for window 'wp'.
  966. * If not present, allocate a location list
  967. */
  968. static qf_info_T *
  969. ll_get_or_alloc_list(wp)
  970. win_T *wp;
  971. {
  972. if (IS_LL_WINDOW(wp))
  973. /* For a location list window, use the referenced location list */
  974. return wp->w_llist_ref;
  975. /*
  976. * For a non-location list window, w_llist_ref should not point to a
  977. * location list.
  978. */
  979. ll_free_all(&wp->w_llist_ref);
  980. if (wp->w_llist == NULL)
  981. wp->w_llist = ll_new_list(); /* new location list */
  982. return wp->w_llist;
  983. }
  984. /*
  985. * Copy the location list from window "from" to window "to".
  986. */
  987. void
  988. copy_loclist(from, to)
  989. win_T *from;
  990. win_T *to;
  991. {
  992. qf_info_T *qi;
  993. int idx;
  994. int i;
  995. /*
  996. * When copying from a location list window, copy the referenced
  997. * location list. For other windows, copy the location list for
  998. * that window.
  999. */
  1000. if (IS_LL_WINDOW(from))
  1001. qi = from->w_llist_ref;
  1002. else
  1003. qi = from->w_llist;
  1004. if (qi == NULL) /* no location list to copy */
  1005. return;
  1006. /* allocate a new location list */
  1007. if ((to->w_llist = ll_new_list()) == NULL)
  1008. return;
  1009. to->w_llist->qf_listcount = qi->qf_listcount;
  1010. /* Copy the location lists one at a time */
  1011. for (idx = 0; idx < qi->qf_listcount; idx++)
  1012. {
  1013. qf_list_T *from_qfl;
  1014. qf_list_T *to_qfl;
  1015. to->w_llist->qf_curlist = idx;
  1016. from_qfl = &qi->qf_lists[idx];
  1017. to_qfl = &to->w_llist->qf_lists[idx];
  1018. /* Some of the fields are populated by qf_add_entry() */
  1019. to_qfl->qf_nonevalid = from_qfl->qf_nonevalid;
  1020. to_qfl->qf_count = 0;
  1021. to_qfl->qf_index = 0;
  1022. to_qfl->qf_start = NULL;
  1023. to_qfl->qf_ptr = NULL;
  1024. if (from_qfl->qf_count)
  1025. {
  1026. qfline_T *from_qfp;
  1027. qfline_T *prevp = NULL;
  1028. /* copy all the location entries in this list */
  1029. for (i = 0, from_qfp = from_qfl->qf_start; i < from_qfl->qf_count;
  1030. ++i, from_qfp = from_qfp->qf_next)
  1031. {
  1032. if (qf_add_entry(to->w_llist, &prevp,
  1033. NULL,
  1034. NULL,
  1035. 0,
  1036. from_qfp->qf_text,
  1037. from_qfp->qf_lnum,
  1038. from_qfp->qf_col,
  1039. from_qfp->qf_viscol,
  1040. from_qfp->qf_pattern,
  1041. from_qfp->qf_nr,
  1042. 0,
  1043. from_qfp->qf_valid) == FAIL)
  1044. {
  1045. qf_free_all(to);
  1046. return;
  1047. }
  1048. /*
  1049. * qf_add_entry() will not set the qf_num field, as the
  1050. * directory and file names are not supplied. So the qf_fnum
  1051. * field is copied here.
  1052. */
  1053. prevp->qf_fnum = from_qfp->qf_fnum; /* file number */
  1054. prevp->qf_type = from_qfp->qf_type; /* error type */
  1055. if (from_qfl->qf_ptr == from_qfp)
  1056. to_qfl->qf_ptr = prevp; /* current location */
  1057. }
  1058. }
  1059. to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */
  1060. /* When no valid entries are present in the list, qf_ptr points to
  1061. * the first item in the list */
  1062. if (to_qfl->qf_nonevalid == TRUE)
  1063. to_qfl->qf_ptr = to_qfl->qf_start;
  1064. }
  1065. to->w_llist->qf_curlist = qi->qf_curlist; /* current list */
  1066. }
  1067. /*
  1068. * get buffer number for file "dir.name"
  1069. */
  1070. static int
  1071. qf_get_fnum(directory, fname)
  1072. char_u *directory;
  1073. char_u *fname;
  1074. {
  1075. if (fname == NULL || *fname == NUL) /* no file name */
  1076. return 0;
  1077. {
  1078. #ifdef RISCOS
  1079. /* Name is reported as `main.c', but file is `c.main' */
  1080. return ro_buflist_add(fname);
  1081. #else
  1082. char_u *ptr;
  1083. int fnum;
  1084. # ifdef VMS
  1085. vms_remove_version(fname);
  1086. # endif
  1087. # ifdef BACKSLASH_IN_FILENAME
  1088. if (directory != NULL)
  1089. slash_adjust(directory);
  1090. slash_adjust(fname);
  1091. # endif
  1092. if (directory != NULL && !vim_isAbsName(fname)
  1093. && (ptr = concat_fnames(directory, fname, TRUE)) != NULL)
  1094. {
  1095. /*
  1096. * Here we check if the file really exists.
  1097. * This should normally be true, but if make works without
  1098. * "leaving directory"-messages we might have missed a
  1099. * directory change.
  1100. */
  1101. if (mch_getperm(ptr) < 0)
  1102. {
  1103. vim_free(ptr);
  1104. directory = qf_guess_filepath(fname);
  1105. if (directory)
  1106. ptr = concat_fnames(directory, fname, TRUE);
  1107. else
  1108. ptr = vim_strsave(fname);
  1109. }
  1110. /* Use concatenated directory name and file name */
  1111. fnum = buflist_add(ptr, 0);
  1112. vim_free(ptr);
  1113. return fnum;
  1114. }
  1115. return buflist_add(fname, 0);
  1116. #endif
  1117. }
  1118. }
  1119. /*
  1120. * push dirbuf onto the directory stack and return pointer to actual dir or
  1121. * NULL on error
  1122. */
  1123. static char_u *
  1124. qf_push_dir(dirbuf, stackptr)
  1125. char_u *dirbuf;
  1126. struct dir_stack_T **stackptr;
  1127. {
  1128. struct dir_stack_T *ds_new;
  1129. struct dir_stack_T *ds_ptr;
  1130. /* allocate new stack element and hook it in */
  1131. ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T));
  1132. if (ds_new == NULL)
  1133. return NULL;
  1134. ds_new->next = *stackptr;
  1135. *stackptr = ds_new;
  1136. /* store directory on the stack */
  1137. if (vim_isAbsName(dirbuf)
  1138. || (*stackptr)->next == NULL
  1139. || (*stackptr && dir_stack != *stackptr))
  1140. (*stackptr)->dirname = vim_strsave(dirbuf);
  1141. else
  1142. {
  1143. /* Okay we don't have an absolute path.
  1144. * dirbuf must be a subdir of one of the directories on the stack.
  1145. * Let's search...
  1146. */
  1147. ds_new = (*stackptr)->next;
  1148. (*stackptr)->dirname = NULL;
  1149. while (ds_new)
  1150. {
  1151. vim_free((*stackptr)->dirname);
  1152. (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
  1153. TRUE);
  1154. if (mch_isdir((*stackptr)->dirname) == TRUE)
  1155. break;
  1156. ds_new = ds_new->next;
  1157. }
  1158. /* clean up all dirs we already left */
  1159. while ((*stackptr)->next != ds_new)
  1160. {
  1161. ds_ptr = (*stackptr)->next;
  1162. (*stackptr)->next = (*stackptr)->next->next;
  1163. vim_free(ds_ptr->dirname);
  1164. vim_free(ds_ptr);
  1165. }
  1166. /* Nothing found -> it must be on top level */
  1167. if (ds_new == NULL)
  1168. {
  1169. vim_free((*stackptr)->dirname);
  1170. (*stackptr)->dirname = vim_strsave(dirbuf);
  1171. }
  1172. }
  1173. if ((*stackptr)->dirname != NULL)
  1174. return (*stackptr)->dirname;
  1175. else
  1176. {
  1177. ds_ptr = *stackptr;
  1178. *stackptr = (*stackptr)->next;
  1179. vim_free(ds_ptr);
  1180. return NULL;
  1181. }
  1182. }
  1183. /*
  1184. * pop dirbuf from the directory stack and return previous directory or NULL if
  1185. * stack is empty
  1186. */
  1187. static char_u *
  1188. qf_pop_dir(stackptr)
  1189. struct dir_stack_T **stackptr;
  1190. {
  1191. struct dir_stack_T *ds_ptr;
  1192. /* TODO: Should we check if dirbuf is the directory on top of the stack?
  1193. * What to do if it isn't? */
  1194. /* pop top element and free it */
  1195. if (*stackptr != NULL)
  1196. {
  1197. ds_ptr = *stackptr;
  1198. *stackptr = (*stackptr)->next;
  1199. vim_free(ds_ptr->dirname);
  1200. vim_free(ds_ptr);
  1201. }
  1202. /* return NEW top element as current dir or NULL if stack is empty*/
  1203. return *stackptr ? (*stackptr)->dirname : NULL;
  1204. }
  1205. /*
  1206. * clean up directory stack
  1207. */
  1208. static void
  1209. qf_clean_dir_stack(stackptr)
  1210. struct dir_stack_T **stackptr;
  1211. {
  1212. struct dir_stack_T *ds_ptr;
  1213. while ((ds_ptr = *stackptr) != NULL)
  1214. {
  1215. *stackptr = (*stackptr)->next;
  1216. vim_free(ds_ptr->dirname);
  1217. vim_free(ds_ptr);
  1218. }
  1219. }
  1220. /*
  1221. * Check in which directory of the directory stack the given file can be
  1222. * found.
  1223. * Returns a pointer to the directory name or NULL if not found
  1224. * Cleans up intermediate directory entries.
  1225. *
  1226. * TODO: How to solve the following problem?
  1227. * If we have the this directory tree:
  1228. * ./
  1229. * ./aa
  1230. * ./aa/bb
  1231. * ./bb
  1232. * ./bb/x.c
  1233. * and make says:
  1234. * making all in aa
  1235. * making all in bb
  1236. * x.c:9: Error
  1237. * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb.
  1238. * qf_guess_filepath will return NULL.
  1239. */
  1240. static char_u *
  1241. qf_guess_filepath(filename)
  1242. char_u *filename;
  1243. {
  1244. struct dir_stack_T *ds_ptr;
  1245. struct dir_stack_T *ds_tmp;
  1246. char_u *fullname;
  1247. /* no dirs on the stack - there's nothing we can do */
  1248. if (dir_stack == NULL)
  1249. return NULL;
  1250. ds_ptr = dir_stack->next;
  1251. fullname = NULL;
  1252. while (ds_ptr)
  1253. {
  1254. vim_free(fullname);
  1255. fullname = concat_fnames(ds_ptr->dirname, filename, TRUE);
  1256. /* If concat_fnames failed, just go on. The worst thing that can happen
  1257. * is that we delete the entire stack.
  1258. */
  1259. if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
  1260. break;
  1261. ds_ptr = ds_ptr->next;
  1262. }
  1263. vim_free(fullname);
  1264. /* clean up all dirs we already left */
  1265. while (dir_stack->next != ds_ptr)
  1266. {
  1267. ds_tmp = dir_stack->next;
  1268. dir_stack->next = dir_stack->next->next;
  1269. vim_free(ds_tmp->dirname);
  1270. vim_free(ds_tmp);
  1271. }
  1272. return ds_ptr==NULL? NULL: ds_ptr->dirname;
  1273. }
  1274. /*
  1275. * jump to a quickfix line
  1276. * if dir == FORWARD go "errornr" valid entries forward
  1277. * if dir == BACKWARD go "errornr" valid entries backward
  1278. * if dir == FORWARD_FILE go "errornr" valid entries files backward
  1279. * if dir == BACKWARD_FILE go "errornr" valid entries files backward
  1280. * else if "errornr" is zero, redisplay the same line
  1281. * else go to entry "errornr"
  1282. */
  1283. void
  1284. qf_jump(qi, dir, errornr, forceit)
  1285. qf_info_T *qi;
  1286. int dir;
  1287. int errornr;
  1288. int forceit;
  1289. {
  1290. qf_info_T *ll_ref;
  1291. qfline_T *qf_ptr;
  1292. qfline_T *old_qf_ptr;
  1293. int qf_index;
  1294. int old_qf_fnum;
  1295. int old_qf_index;
  1296. int prev_index;
  1297. static char_u *e_no_more_items = (char_u *)N_("E553: No more items");
  1298. char_u *err = e_no_more_items;
  1299. linenr_T i;
  1300. buf_T *old_curbuf;
  1301. linenr_T old_lnum;
  1302. colnr_T screen_col;
  1303. colnr_T char_col;
  1304. char_u *line;
  1305. #ifdef FEAT_WINDOWS
  1306. char_u *old_swb = p_swb;
  1307. int opened_window = FALSE;
  1308. win_T *win;
  1309. win_T *altwin;
  1310. #endif
  1311. int print_message = TRUE;
  1312. int len;
  1313. #ifdef FEAT_FOLDING
  1314. int old_KeyTyped = KeyTyped; /* getting file may reset it */
  1315. #endif
  1316. int ok = OK;
  1317. int usable_win;
  1318. if (qi == NULL)
  1319. qi = &ql_info;
  1320. if (qi->qf_curlist >= qi->qf_listcount
  1321. || qi->qf_lists[qi->qf_curlist].qf_count == 0)
  1322. {
  1323. EMSG(_(e_quickfix));
  1324. return;
  1325. }
  1326. qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr;
  1327. old_qf_ptr = qf_ptr;
  1328. qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
  1329. old_qf_index = qf_index;
  1330. if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */
  1331. {
  1332. while (errornr--)
  1333. {
  1334. old_qf_ptr = qf_ptr;
  1335. prev_index = qf_index;
  1336. old_qf_fnum = qf_ptr->qf_fnum;
  1337. do
  1338. {
  1339. if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count
  1340. || qf_ptr->qf_next == NULL)
  1341. {
  1342. qf_ptr = old_qf_ptr;
  1343. qf_index = prev_index;
  1344. if (err != NULL)
  1345. {
  1346. EMSG(_(err));
  1347. goto theend;
  1348. }
  1349. errornr = 0;
  1350. break;
  1351. }
  1352. ++qf_index;
  1353. qf_ptr = qf_ptr->qf_next;
  1354. } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
  1355. && !qf_ptr->qf_valid)
  1356. || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
  1357. err = NULL;
  1358. }
  1359. }
  1360. else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */
  1361. {
  1362. while (errornr--)
  1363. {
  1364. old_qf_ptr = qf_ptr;
  1365. prev_index = qf_index;
  1366. old_qf_fnum = qf_ptr->qf_fnum;
  1367. do
  1368. {
  1369. if (qf_index == 1 || qf_ptr->qf_prev == NULL)
  1370. {
  1371. qf_ptr = old_qf_ptr;
  1372. qf_index = prev_index;
  1373. if (err != NULL)
  1374. {
  1375. EMSG(_(err));
  1376. goto theend;
  1377. }
  1378. errornr = 0;
  1379. break;
  1380. }
  1381. --qf_index;
  1382. qf_ptr = qf_ptr->qf_prev;
  1383. } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid
  1384. && !qf_ptr->qf_valid)
  1385. || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum));
  1386. err = NULL;
  1387. }
  1388. }
  1389. else if (errornr != 0) /* go to specified number */
  1390. {
  1391. while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL)
  1392. {
  1393. --qf_index;
  1394. qf_ptr = qf_ptr->qf_prev;
  1395. }
  1396. while (errornr > qf_index && qf_index <
  1397. qi->qf_lists[qi->qf_curlist].qf_count
  1398. && qf_ptr->qf_next != NULL)
  1399. {
  1400. ++qf_index;
  1401. qf_ptr = qf_ptr->qf_next;
  1402. }
  1403. }
  1404. #ifdef FEAT_WINDOWS
  1405. qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
  1406. if (qf_win_pos_update(qi, old_qf_index))
  1407. /* No need to print the error message if it's visible in the error
  1408. * window */
  1409. print_message = FALSE;
  1410. /*
  1411. * For ":helpgrep" find a help window or open one.
  1412. */
  1413. if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0))
  1414. {
  1415. win_T *wp;
  1416. int n;
  1417. if (cmdmod.tab != 0)
  1418. wp = NULL;
  1419. else
  1420. for (wp = firstwin; wp != NULL; wp = wp->w_next)
  1421. if (wp->w_buffer != NULL && wp->w_buffer->b_help)
  1422. break;
  1423. if (wp != NULL && wp->w_buffer->b_nwindows > 0)
  1424. win_enter(wp, TRUE);
  1425. else
  1426. {
  1427. /*
  1428. * Split off help window; put it at far top if no position
  1429. * specified, the current window is vertically split and narrow.
  1430. */
  1431. n = WSP_HELP;
  1432. # ifdef FEAT_VERTSPLIT
  1433. if (cmdmod.split == 0 && curwin->w_width != Columns
  1434. && curwin->w_width < 80)
  1435. n |= WSP_TOP;
  1436. # endif
  1437. if (win_split(0, n) == FAIL)
  1438. goto theend;
  1439. opened_window = TRUE; /* close it when fail */
  1440. if (curwin->w_height < p_hh)
  1441. win_setheight((int)p_hh);
  1442. if (qi != &ql_info) /* not a quickfix list */
  1443. {
  1444. /* The new window should use the supplied location list */
  1445. qf_free_all(curwin);
  1446. curwin->w_llist = qi;
  1447. qi->qf_refcount++;
  1448. }
  1449. }
  1450. if (!p_im)
  1451. restart_edit = 0; /* don't want insert mode in help file */
  1452. }
  1453. /*
  1454. * If currently in the quickfix window, find another window to show the
  1455. * file in.
  1456. */
  1457. if (bt_quickfix(curbuf) && !opened_window)
  1458. {
  1459. /*
  1460. * If there is no file specified, we don't know where to go.
  1461. * But do advance, otherwise ":cn" gets stuck.
  1462. */
  1463. if (qf_ptr->qf_fnum == 0)
  1464. goto theend;
  1465. /* Locate a window showing a normal buffer */
  1466. usable_win = 0;
  1467. FOR_ALL_WINDOWS(win)
  1468. if (win->w_buffer->b_p_bt[0] == NUL)
  1469. {
  1470. usable_win = 1;
  1471. break;
  1472. }
  1473. /*
  1474. * If no usable window is found and 'switchbuf' is set to 'usetab'
  1475. * then search in other tabs.
  1476. */
  1477. if (!usable_win && vim_strchr(p_swb, 'a') != NULL)
  1478. {
  1479. tabpage_T *tp;
  1480. win_T *wp;
  1481. FOR_ALL_TAB_WINDOWS(tp, wp)
  1482. {
  1483. if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum)
  1484. {
  1485. goto_tabpage_win(tp, wp);
  1486. usable_win = 1;
  1487. break;
  1488. }
  1489. }
  1490. }
  1491. /*
  1492. * If there is only one window and is the quickfix window, create a new
  1493. * one above the quickfix window.
  1494. */
  1495. if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win)
  1496. {
  1497. ll_ref = curwin->w_llist_ref;
  1498. if (win_split(0, WSP_ABOVE) == FAIL)
  1499. goto failed; /* not enough room for window */
  1500. opened_window = TRUE; /* close it when fail */
  1501. p_swb = empty_option; /* don't split again */
  1502. # ifdef FEAT_SCROLLBIND
  1503. curwin->w_p_scb = FALSE;
  1504. # endif
  1505. if (ll_ref != NULL)
  1506. {
  1507. /* The new window should use the location list from the
  1508. * location list window */
  1509. qf_free_all(curwin);
  1510. curwin->w_llist = ll_ref;
  1511. ll_ref->qf_refcount++;
  1512. }
  1513. }
  1514. else
  1515. {
  1516. if (curwin->w_llist_ref != NULL)
  1517. {
  1518. /* In a location window */
  1519. ll_ref = curwin->w_llist_ref;
  1520. /* Find the window with the same location list */
  1521. FOR_ALL_WINDOWS(win)
  1522. if (win->w_llist == ll_ref)
  1523. break;
  1524. if (win == NULL)
  1525. {
  1526. /* Find the window showing the selected file */
  1527. FOR_ALL_WINDOWS(win)
  1528. if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
  1529. break;
  1530. if (win == NULL)
  1531. {
  1532. /* Find a previous usable window */
  1533. win = curwin;
  1534. do
  1535. {
  1536. if (win->w_buffer->b_p_bt[0] == NUL)
  1537. break;
  1538. if (win->w_prev == NULL)
  1539. win = lastwin; /* wrap around the top */
  1540. else
  1541. win = win->w_prev; /* go to previous window */
  1542. } while (win != curwin);
  1543. }
  1544. }
  1545. win_goto(win);
  1546. /* If the location list for the window is not set, then set it
  1547. * to the location list from the location window */
  1548. if (win->w_llist == NULL)
  1549. {
  1550. win->w_llist = ll_ref;
  1551. ll_ref->qf_refcount++;
  1552. }
  1553. }
  1554. else
  1555. {
  1556. /*
  1557. * Try to find a window that shows the right buffer.
  1558. * Default to the window just above the quickfix buffer.
  1559. */
  1560. win = curwin;
  1561. altwin = NULL;
  1562. for (;;)
  1563. {
  1564. if (win->w_buffer->b_fnum == qf_ptr->qf_fnum)
  1565. break;
  1566. if (win->w_prev == NULL)
  1567. win = lastwin; /* wrap around the top */
  1568. else
  1569. win = win->w_prev; /* go to previous window */
  1570. if (IS_QF_WINDOW(win))
  1571. {
  1572. /* Didn't find it, go to the window before the quickfix
  1573. * window. */
  1574. if (altwin != NULL)
  1575. win = altwin;
  1576. else if (curwin->w_prev != NULL)
  1577. win = curwin->w_prev;
  1578. else
  1579. win = curwin->w_next;
  1580. break;
  1581. }
  1582. /* Remember a usable window. */
  1583. if (altwin == NULL && !win->w_p_pvw
  1584. && win->w_buffer->b_p_bt[0] == NUL)
  1585. altwin = win;
  1586. }
  1587. win_goto(win);
  1588. }
  1589. }
  1590. }
  1591. #endif
  1592. /*
  1593. * If there is a file name,
  1594. * read the wanted file if needed, and check autowrite etc.
  1595. */
  1596. old_curbuf = curbuf;
  1597. old_lnum = curwin->w_cursor.lnum;
  1598. if (qf_ptr->qf_fnum != 0)
  1599. {
  1600. if (qf_ptr->qf_type == 1)
  1601. {
  1602. /* Open help file (do_ecmd() will set b_help flag, readfile() will
  1603. * set b_p_ro flag). */
  1604. if (!can_abandon(curbuf, forceit))
  1605. {
  1606. EMSG(_(e_nowrtmsg));
  1607. ok = FALSE;
  1608. }
  1609. else
  1610. ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1,
  1611. ECMD_HIDE + ECMD_SET_HELP);
  1612. }
  1613. else
  1614. ok = buflist_getfile(qf_ptr->qf_fnum,
  1615. (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit);
  1616. }
  1617. if (ok == OK)
  1618. {
  1619. /* When not switched to another buffer, still need to set pc mark */
  1620. if (curbuf == old_curbuf)
  1621. setpcmark();
  1622. if (qf_ptr->qf_pattern == NULL)
  1623. {
  1624. /*
  1625. * Go to line with error, unless qf_lnum is 0.
  1626. */
  1627. i = qf_ptr->qf_lnum;
  1628. if (i > 0)
  1629. {
  1630. if (i > curbuf->b_ml.ml_line_count)
  1631. i = curbuf->b_ml.ml_line_count;
  1632. curwin->w_cursor.lnum = i;
  1633. }
  1634. if (qf_ptr->qf_col > 0)
  1635. {
  1636. curwin->w_cursor.col = qf_ptr->qf_col - 1;
  1637. if (qf_ptr->qf_viscol == TRUE)
  1638. {
  1639. /*
  1640. * Check each character from the beginning of the error
  1641. * line up to the error column. For each tab character
  1642. * found, reduce the error column value by the length of
  1643. * a tab character.
  1644. */
  1645. line = ml_get_curline();
  1646. screen_col = 0;
  1647. for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col)
  1648. {
  1649. if (*line == NUL)
  1650. break;
  1651. if (*line++ == '\t')
  1652. {
  1653. curwin->w_cursor.col -= 7 - (screen_col % 8);
  1654. screen_col += 8 - (screen_col % 8);
  1655. }
  1656. else
  1657. ++screen_col;
  1658. }
  1659. }
  1660. check_cursor();
  1661. }
  1662. else
  1663. beginline(BL_WHITE | BL_FIX);
  1664. }
  1665. else
  1666. {
  1667. pos_T save_cursor;
  1668. /* Move the cursor to the first line in the buffer */
  1669. save_cursor = curwin->w_cursor;
  1670. curwin->w_cursor.lnum = 0;
  1671. if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, SEARCH_KEEP))
  1672. curwin->w_cursor = save_cursor;
  1673. }
  1674. #ifdef FEAT_FOLDING
  1675. if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped)
  1676. foldOpenCursor();
  1677. #endif
  1678. if (print_message)
  1679. {
  1680. /* Update the screen before showing the message */
  1681. update_topline_redraw();
  1682. sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index,
  1683. qi->qf_lists[qi->qf_curlist].qf_count,
  1684. qf_ptr->qf_cleared ? _(" (line deleted)") : "",
  1685. (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr));
  1686. /* Add the message, skipping leading whitespace and newlines. */
  1687. len = (int)STRLEN(IObuff);
  1688. qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len);
  1689. /* Output the message. Overwrite to avoid scrolling when the 'O'
  1690. * flag is present in 'shortmess'; But when not jumping, print the
  1691. * whole message. */
  1692. i = msg_scroll;
  1693. if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum)
  1694. msg_scroll = TRUE;
  1695. else if (!msg_scrolled && shortmess(SHM_OVERALL))
  1696. msg_scroll = FALSE;
  1697. msg_attr_keep(IObuff, 0, TRUE);
  1698. msg_scroll = i;
  1699. }
  1700. }
  1701. else
  1702. {
  1703. #ifdef FEAT_WINDOWS
  1704. if (opened_window)
  1705. win_close(curwin, TRUE); /* Close opened window */
  1706. #endif
  1707. if (qf_ptr->qf_fnum != 0)
  1708. {
  1709. /*
  1710. * Couldn't open file, so put index back where it was. This could
  1711. * happen if the file was readonly and we changed something.
  1712. */
  1713. #ifdef FEAT_WINDOWS
  1714. failed:
  1715. #endif
  1716. qf_ptr = old_qf_ptr;
  1717. qf_index = old_qf_index;
  1718. }
  1719. }
  1720. theend:
  1721. qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr;
  1722. qi->qf_lists[qi->qf_curlist].qf_index = qf_index;
  1723. #ifdef FEAT_WINDOWS
  1724. if (p_swb != old_swb && opened_window)
  1725. {
  1726. /* Restore old 'switchbuf' value, but not when an autocommand or
  1727. * modeline has changed the value. */
  1728. if (p_swb == empty_option)
  1729. p_swb = old_swb;
  1730. else
  1731. free_string_option(old_swb);
  1732. }
  1733. #endif
  1734. }
  1735. /*
  1736. * ":clist": list all errors
  1737. * ":llist": list all locations
  1738. */
  1739. void
  1740. qf_list(eap)
  1741. exarg_T *eap;
  1742. {
  1743. buf_T *buf;
  1744. char_u *fname;
  1745. qfline_T *qfp;
  1746. int i;
  1747. int idx1 = 1;
  1748. int idx2 = -1;
  1749. int need_return = TRUE;
  1750. char_u *arg = eap->arg;
  1751. int all = eap->forceit; /* if not :cl!, only show
  1752. recognised errors */
  1753. qf_info_T *qi = &ql_info;
  1754. if (eap->cmdidx == CMD_llist)
  1755. {
  1756. qi = GET_LOC_LIST(curwin);
  1757. if (qi == NULL)
  1758. {
  1759. EMSG(_(e_loclist));
  1760. return;
  1761. }
  1762. }
  1763. if (qi->qf_curlist >= qi->qf_listcount
  1764. || qi->qf_lists[qi->qf_curlist].qf_count == 0)
  1765. {
  1766. EMSG(_(e_quickfix));
  1767. return;
  1768. }
  1769. if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL)
  1770. {
  1771. EMSG(_(e_trailing));
  1772. return;
  1773. }
  1774. i = qi->qf_lists[qi->qf_curlist].qf_count;
  1775. if (idx1 < 0)
  1776. idx1 = (-idx1 > i) ? 0 : idx1 + i + 1;
  1777. if (idx2 < 0)
  1778. idx2 = (-idx2 > i) ? 0 : idx2 + i + 1;
  1779. if (qi->qf_lists[qi->qf_curlist].qf_nonevalid)
  1780. all = TRUE;
  1781. qfp = qi->qf_lists[qi->qf_curlist].qf_start;
  1782. for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; )
  1783. {
  1784. if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2)
  1785. {
  1786. if (need_return)
  1787. {
  1788. msg_putchar('\n');
  1789. if (got_int)
  1790. break;
  1791. need_return = FALSE;
  1792. }
  1793. fname = NULL;
  1794. if (qfp->qf_fnum != 0
  1795. && (buf = buflist_findnr(qfp->qf_fnum)) != NULL)
  1796. {
  1797. fname = buf->b_fname;
  1798. if (qfp->qf_type == 1) /* :helpgrep */
  1799. fname = gettail(fname);
  1800. }
  1801. if (fname == NULL)
  1802. sprintf((char *)IObuff, "%2d", i);
  1803. else
  1804. vim_snprintf((char *)IObuff, IOSIZE, "%2d %s",
  1805. i, (char *)fname);
  1806. msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index
  1807. ? hl_attr(HLF_L) : hl_attr(HLF_D));
  1808. if (qfp->qf_lnum == 0)
  1809. IObuff[0] = NUL;
  1810. else if (qfp->qf_col == 0)
  1811. sprintf((char *)IObuff, ":%ld", qfp->qf_lnum);
  1812. else
  1813. sprintf((char *)IObuff, ":%ld col %d",
  1814. qfp->qf_lnum, qfp->qf_col);
  1815. sprintf((char *)IObuff + STRLEN(IObuff), "%s:",
  1816. (char *)qf_types(qfp->qf_type, qfp->qf_nr));
  1817. msg_puts_attr(IObuff, hl_attr(HLF_N));
  1818. if (qfp->qf_pattern != NULL)
  1819. {
  1820. qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE);
  1821. STRCAT(IObuff, ":");
  1822. msg_puts(IObuff);
  1823. }
  1824. msg_puts((char_u *)" ");
  1825. /* Remove newlines and leading whitespace from the text. For an
  1826. * unrecognized line keep the indent, the compiler may mark a word
  1827. * with ^^^^. */
  1828. qf_fmt_text((fname != NULL || qfp->qf_lnum != 0)
  1829. ? skipwhite(qfp->qf_text) : qfp->qf_text,
  1830. IObuff, IOSIZE);
  1831. msg_prt_line(IObuff, FALSE);
  1832. out_flush(); /* show one line at a time */
  1833. need_return = TRUE;
  1834. }
  1835. qfp = qfp->qf_next;
  1836. ++i;
  1837. ui_breakcheck();
  1838. }
  1839. }
  1840. /*
  1841. * Remove newlines and leading whitespace from an error message.
  1842. * Put the result in "buf[bufsize]".
  1843. */
  1844. static void
  1845. qf_fmt_text(text, buf, bufsize)
  1846. char_u *text;
  1847. char_u *buf;
  1848. int bufsize;
  1849. {
  1850. int i;
  1851. char_u *p = text;
  1852. for (i = 0; *p != NUL && i < bufsize - 1; ++i)
  1853. {
  1854. if (*p == '\n')
  1855. {
  1856. buf[i] = ' ';
  1857. while (*++p != NUL)
  1858. if (!vim_iswhite(*p) && *p != '\n')
  1859. break;
  1860. }
  1861. else
  1862. buf[i] = *p++;
  1863. }
  1864. buf[i] = NUL;
  1865. }
  1866. /*
  1867. * ":colder [count]": Up in the quickfix stack.
  1868. * ":cnewer [count]": Down in the quickfix stack.
  1869. * ":lolder [count]": Up in the location list stack.
  1870. * ":lnewer [count]": Down in the location list stack.
  1871. */
  1872. void
  1873. qf_age(eap)
  1874. exarg_T *eap;
  1875. {
  1876. qf_info_T *qi = &ql_info;
  1877. int count;
  1878. if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer)
  1879. {
  1880. qi = GET_LOC_LIST(curwin);
  1881. if (qi == NULL)
  1882. {
  1883. EMSG(_(e_loclist));
  1884. return;
  1885. }
  1886. }
  1887. if (eap->addr_count != 0)
  1888. count = eap->line2;
  1889. else
  1890. count = 1;
  1891. while (count--)
  1892. {
  1893. if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder)
  1894. {
  1895. if (qi->qf_curlist == 0)
  1896. {
  1897. EMSG(_("E380: At bottom of quickfix stack"));
  1898. return;
  1899. }
  1900. --qi->qf_curlist;
  1901. }
  1902. else
  1903. {
  1904. if (qi->qf_curlist >= qi->qf_listcount - 1)
  1905. {
  1906. EMSG(_("E381: At top of quickfix stack"));
  1907. return;
  1908. }
  1909. ++qi->qf_curlist;
  1910. }
  1911. }
  1912. qf_msg(qi);
  1913. }
  1914. static void
  1915. qf_msg(qi)
  1916. qf_info_T *qi;
  1917. {
  1918. smsg((char_u *)_("error list %d of %d; %d errors"),
  1919. qi->qf_curlist + 1, qi->qf_listcount,
  1920. qi->qf_lists[qi->qf_curlist].qf_count);
  1921. #ifdef FEAT_WINDOWS
  1922. qf_update_buffer(qi);
  1923. #endif
  1924. }
  1925. /*
  1926. * Free error list "idx".
  1927. */
  1928. static void
  1929. qf_free(qi, idx)
  1930. qf_info_T *qi;
  1931. int idx;
  1932. {
  1933. qfline_T *qfp;
  1934. while (qi->qf_lists[idx].qf_count)
  1935. {
  1936. qfp = qi->qf_lists[idx].qf_start->qf_next;
  1937. vim_free(qi->qf_lists[idx].qf_start->qf_text);
  1938. vim_free(qi->qf_lists[idx].qf_start->qf_pattern);
  1939. vim_free(qi->qf_lists[idx].qf_start);
  1940. qi->qf_lists[idx].qf_start = qfp;
  1941. --qi->qf_lists[idx].qf_count;
  1942. }
  1943. }
  1944. /*
  1945. * qf_mark_adjust: adjust marks
  1946. */
  1947. void
  1948. qf_mark_adjust(wp, line1, line2, amount, amount_after)
  1949. win_T *wp;
  1950. linenr_T line1;
  1951. linenr_T line2;
  1952. long amount;
  1953. long amount_after;
  1954. {
  1955. int i;
  1956. qfline_T *qfp;
  1957. int idx;
  1958. qf_info_T *qi = &ql_info;
  1959. if (wp != NULL)
  1960. {
  1961. if (wp->w_llist == NULL)
  1962. return;
  1963. qi = wp->w_llist;
  1964. }
  1965. for (idx = 0; idx < qi->qf_listcount; ++idx)
  1966. if (qi->qf_lists[idx].qf_count)
  1967. for (i = 0, qfp = qi->qf_lists[idx].qf_start;
  1968. i < qi->qf_lists[idx].qf_count; ++i, qfp = qfp->qf_next)
  1969. if (qfp->qf_fnum == curbuf->b_fnum)
  1970. {
  1971. if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2)
  1972. {
  1973. if (amount == MAXLNUM)
  1974. qfp->qf_cleared = TRUE;
  1975. else
  1976. qfp->qf_lnum += amount;
  1977. }
  1978. else if (amount_after && qfp->qf_lnum > line2)
  1979. qfp->qf_lnum += amount_after;
  1980. }
  1981. }
  1982. /*
  1983. * Make a nice message out of the error character and the error number:
  1984. * char number message
  1985. * e or E 0 " error"
  1986. * w or W 0 " warning"
  1987. * i or I 0 " info"
  1988. * 0 0 ""
  1989. * other 0 " c"
  1990. * e or E n " error n"
  1991. * w or W n " warning n"
  1992. * i or I n " info n"
  1993. * 0 n " error n"
  1994. * other n " c n"
  1995. * 1 x "" :helpgrep
  1996. */
  1997. static char_u *
  1998. qf_types(c, nr)
  1999. int c, nr;
  2000. {
  2001. static char_u buf[20];
  2002. static char_u cc[3];
  2003. char_u *p;
  2004. if (c == 'W' || c == 'w')
  2005. p = (char_u *)" warning";
  2006. else if (c == 'I' || c == 'i')
  2007. p = (char_u *)" info";
  2008. else if (c == 'E' || c == 'e' || (c == 0 && nr > 0))
  2009. p = (char_u *)" error";
  2010. else if (c == 0 || c == 1)
  2011. p = (char_u *)"";
  2012. else
  2013. {
  2014. cc[0] = ' ';
  2015. cc[1] = c;
  2016. cc[2] = NUL;
  2017. p = cc;
  2018. }
  2019. if (nr <= 0)
  2020. return p;
  2021. sprintf((char *)buf, "%s %3d", (char *)p, nr);
  2022. return buf;
  2023. }
  2024. #if defined(FEAT_WINDOWS) || defined(PROTO)
  2025. /*
  2026. * ":cwindow": open the quickfix window if we have errors to display,
  2027. * close it if not.
  2028. * ":lwindow": open the location list window if we have locations to display,
  2029. * close it if not.
  2030. */
  2031. void
  2032. ex_cwindow(eap)
  2033. exarg_T *eap;
  2034. {
  2035. qf_info_T *qi = &ql_info;
  2036. win_T *win;
  2037. if (eap->cmdidx == CMD_lwindow)
  2038. {
  2039. qi = GET_LOC_LIST(curwin);
  2040. if (qi == NULL)
  2041. return;
  2042. }
  2043. /* Look for an existing quickfix window. */
  2044. win = qf_find_win(qi);
  2045. /*
  2046. * If a quickfix window is open but we have no errors to display,
  2047. * close the window. If a quickfix window is not open, then open
  2048. * it if we have errors; otherwise, leave it closed.
  2049. */
  2050. if (qi->qf_lists[qi->qf_curlist].qf_nonevalid
  2051. || qi->qf_curlist >= qi->qf_listcount)
  2052. {
  2053. if (win != NULL)
  2054. ex_cclose(eap);
  2055. }
  2056. else if (win == NULL)
  2057. ex_copen(eap);
  2058. }
  2059. /*
  2060. * ":cclose": close the window showing the list of errors.
  2061. * ":lclose": close the window showing the location list
  2062. */
  2063. /*ARGSUSED*/
  2064. void
  2065. ex_cclose(eap)
  2066. exarg_T *eap;
  2067. {
  2068. win_T *win = NULL;
  2069. qf_info_T *qi = &ql_info;
  2070. if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow)
  2071. {
  2072. qi = GET_LOC_LIST(curwin);
  2073. if (qi == NULL)
  2074. return;
  2075. }
  2076. /* Find existing quickfix window and close it. */
  2077. win = qf_find_win(qi);
  2078. if (win != NULL)
  2079. win_close(win, FALSE);
  2080. }
  2081. /*
  2082. * ":copen": open a window that shows the list of errors.
  2083. * ":lopen": open a window that shows the location list.
  2084. */
  2085. void
  2086. ex_copen(eap)
  2087. exarg_T *eap;
  2088. {
  2089. qf_info_T *qi = &ql_info;
  2090. int height;
  2091. win_T *win;
  2092. tabpage_T *prevtab = curtab;
  2093. buf_T *qf_buf;
  2094. if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
  2095. {
  2096. qi = GET_LOC_LIST(curwin);
  2097. if (qi == NULL)
  2098. {
  2099. EMSG(_(e_loclist));
  2100. return;
  2101. }
  2102. }
  2103. if (eap->addr_count != 0)
  2104. height = eap->line2;
  2105. else
  2106. height = QF_WINHEIGHT;
  2107. #ifdef FEAT_VISUAL
  2108. reset_VIsual_and_resel(); /* stop Visual mode */
  2109. #endif
  2110. #ifdef FEAT_GUI
  2111. need_mouse_correct = TRUE;
  2112. #endif
  2113. /*
  2114. * Find existing quickfix window, or open a new one.
  2115. */
  2116. win = qf_find_win(qi);
  2117. if (win != NULL && cmdmod.tab == 0)
  2118. win_goto(win);
  2119. else
  2120. {
  2121. qf_buf = qf_find_buf(qi);
  2122. /* The current window becomes the previous window afterwards. */
  2123. win = curwin;
  2124. if (eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow)
  2125. /* Create the new window at the very bottom. */
  2126. win_goto(lastwin);
  2127. if (win_split(height, WSP_BELOW) == FAIL)
  2128. return; /* not enough room for window */
  2129. #ifdef FEAT_SCROLLBIND
  2130. curwin->w_p_scb = FALSE;
  2131. #endif
  2132. /* Remove the location list for the quickfix window */
  2133. qf_free_all(curwin);
  2134. if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow)
  2135. {
  2136. /*
  2137. * For the location list window, create a reference to the
  2138. * location list from the window 'win'.
  2139. */
  2140. curwin->w_llist_ref = win->w_llist;
  2141. win->w_llist->qf_refcount++;
  2142. }
  2143. if (qf_buf != NULL)
  2144. /* Use the existing quickfix buffer */
  2145. (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE,
  2146. ECMD_HIDE + ECMD_OLDBUF);
  2147. else
  2148. {
  2149. /* Create a new quickfix buffer */
  2150. (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
  2151. /* switch off 'swapfile' */
  2152. set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
  2153. set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix",
  2154. OPT_LOCAL);
  2155. set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL);
  2156. set_option_value((char_u *)"diff", 0L, (char_u *)"", OPT_LOCAL);
  2157. }
  2158. /* Only set the height when still in the same tab page and there is no
  2159. * window to the side. */
  2160. if (curtab == prevtab
  2161. #ifdef FEAT_VERTSPLIT
  2162. && curwin->w_width == Columns
  2163. #endif
  2164. )
  2165. win_setheight(height);
  2166. curwin->w_p_wfh = TRUE; /* set 'winfixheight' */
  2167. if (win_valid(win))
  2168. prevwin = win;
  2169. }
  2170. /*
  2171. * Fill the buffer with the quickfix list.
  2172. */
  2173. qf_fill_buffer(qi);
  2174. curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index;
  2175. curwin->w_cursor.col = 0;
  2176. check_cursor();
  2177. update_topline(); /* scroll to show the line */
  2178. }
  2179. /*
  2180. * Return the number of the current entry (line number in the quickfix
  2181. * window).
  2182. */
  2183. linenr_T
  2184. qf_current_entry(wp)
  2185. win_T *wp;
  2186. {
  2187. qf_info_T *qi = &ql_info;
  2188. if (IS_LL_WINDOW(wp))
  2189. /* In the location list window, use the referenced location list */
  2190. qi = wp->w_llist_ref;
  2191. return qi->qf_lists[qi->qf_curlist].qf_index;
  2192. }
  2193. /*
  2194. * Update the cursor position in the quickfix window to the current error.
  2195. * Return TRUE if there is a quickfix window.
  2196. */
  2197. static int
  2198. qf_win_pos_update(qi, old_qf_index)
  2199. qf_info_T *qi;
  2200. int old_qf_index; /* previous qf_index or zero */
  2201. {
  2202. win_T *win;
  2203. int qf_index = qi->qf_lists[qi->qf_curlist].qf_index;
  2204. /*
  2205. * Put the cursor on the current error in the quickfix window, so that
  2206. * it's viewable.
  2207. */
  2208. win = qf_find_win(qi);
  2209. if (win != NULL
  2210. && qf_index <= win->w_buffer->b_ml.ml_line_count
  2211. && old_qf_index != qf_index)
  2212. {
  2213. win_T *old_curwin = curwin;
  2214. curwin = win;
  2215. curbuf = win->w_buffer;
  2216. if (qf_index > old_qf_index)
  2217. {
  2218. curwin->w_redraw_top = old_qf_index;
  2219. curwin->w_redraw_bot = qf_index;
  2220. }
  2221. else
  2222. {
  2223. curwin->w_redraw_top = qf_index;
  2224. curwin->w_redraw_bot = old_qf_index;
  2225. }
  2226. curwin->w_cursor.lnum = qf_index;
  2227. curwin->w_cursor.col = 0;
  2228. update_topline(); /* scroll to show the line */
  2229. redraw_later(VALID);
  2230. curwin->w_redr_status = TRUE; /* update ruler */
  2231. curwin = old_curwin;
  2232. curbuf = curwin->w_buffer;
  2233. }
  2234. return win != NULL;
  2235. }
  2236. /*
  2237. * Check whether the given window is displaying the specified quickfix/location
  2238. * list buffer
  2239. */
  2240. static int
  2241. is_qf_win(win, qi)
  2242. win_T *win;
  2243. qf_info_T *qi;
  2244. {
  2245. /*
  2246. * A window displaying the quickfix buffer will have the w_llist_ref field
  2247. * set to NULL.
  2248. * A window displaying a location list buffer will have the w_llist_ref
  2249. * pointing to the location list.
  2250. */
  2251. if (bt_quickfix(win->w_buffer))
  2252. if ((qi == &ql_info && win->w_llist_ref == NULL)
  2253. || (qi != &ql_info && win->w_llist_ref == qi))
  2254. return TRUE;
  2255. return FALSE;
  2256. }
  2257. /*
  2258. * Find a window displaying the quickfix/location list 'qi'
  2259. * Searches in only the windows opened in the current tab.
  2260. */
  2261. static win_T *
  2262. qf_find_win(qi)
  2263. qf_info_T *qi;
  2264. {
  2265. win_T *win;
  2266. FOR_ALL_WINDOWS(win)
  2267. if (is_qf_win(win, qi))
  2268. break;
  2269. return win;
  2270. }
  2271. /*
  2272. * Find a quickfix buffer.
  2273. * Searches in windows opened in all the tabs.
  2274. */
  2275. static buf_T *
  2276. qf_find_buf(qi)
  2277. qf_info_T *qi;
  2278. {
  2279. tabpage_T *tp;
  2280. win_T *win;
  2281. FOR_ALL_TAB_WINDOWS(tp, win)
  2282. if (is_qf_win(win, qi))
  2283. return win->w_buffer;
  2284. return NULL;
  2285. }
  2286. /*
  2287. * Find the quickfix buffer. If it exists, update the contents.
  2288. */
  2289. static void
  2290. qf_update_buffer(qi)
  2291. qf_info_T *qi;
  2292. {
  2293. buf_T *buf;
  2294. aco_save_T aco;
  2295. /* Check if a buffer for the quickfix list exists. Update it. */
  2296. buf = qf_find_buf(qi);
  2297. if (buf != NULL)
  2298. {
  2299. /* set curwin/curbuf to buf and save a few things */
  2300. aucmd_prepbuf(&aco, buf);
  2301. qf_fill_buffer(qi);
  2302. /* restore curwin/curbuf and a few other things */
  2303. aucmd_restbuf(&aco);
  2304. (void)qf_win_pos_update(qi, 0);
  2305. }
  2306. }
  2307. /*
  2308. * Fill current buffer with quickfix errors, replacing any previous contents.
  2309. * curbuf must be the quickfix buffer!
  2310. */
  2311. static void
  2312. qf_fill_buffer(qi)
  2313. qf_info_T *qi;
  2314. {
  2315. linenr_T lnum;
  2316. qfline_T *qfp;
  2317. buf_T *errbuf;
  2318. int len;
  2319. int old_KeyTyped = KeyTyped;
  2320. /* delete all existing lines */
  2321. while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
  2322. (void)ml_delete((linenr_T)1, FALSE);
  2323. /* Check if there is anything to display */
  2324. if (qi->qf_curlist < qi->qf_listcount)
  2325. {
  2326. /* Add one line for each error */
  2327. qfp = qi->qf_lists[qi->qf_curlist].qf_start;
  2328. for (lnum = 0; lnum < qi->qf_lists[qi->qf_curlist].qf_count; ++lnum)
  2329. {
  2330. if (qfp->qf_fnum != 0
  2331. && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL
  2332. && errbuf->b_fname != NULL)
  2333. {
  2334. if (qfp->qf_type == 1) /* :helpgrep */
  2335. STRCPY(IObuff, gettail(errbuf->b_fname));
  2336. else
  2337. STRCPY(IObuff, errbuf->b_fname);
  2338. len = (int)STRLEN(IObuff);
  2339. }
  2340. else
  2341. len = 0;
  2342. IObuff[len++] = '|';
  2343. if (qfp->qf_lnum > 0)
  2344. {
  2345. sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum);
  2346. len += (int)STRLEN(IObuff + len);
  2347. if (qfp->qf_col > 0)
  2348. {
  2349. sprintf((char *)IObuff + len, " col %d", qfp->qf_col);
  2350. len += (int)STRLEN(IObuff + len);
  2351. }
  2352. sprintf((char *)IObuff + len, "%s",
  2353. (char *)qf_types(qfp->qf_type, qfp->qf_nr));
  2354. len += (int)STRLEN(IObuff + len);
  2355. }
  2356. else if (qfp->qf_pattern != NULL)
  2357. {
  2358. qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len);
  2359. len += (int)STRLEN(IObuff + len);
  2360. }
  2361. IObuff[len++] = '|';
  2362. IObuff[len++] = ' ';
  2363. /* Remove newlines and leading whitespace from the text.
  2364. * For an unrecognized line keep the indent, the compiler may
  2365. * mark a word with ^^^^. */
  2366. qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
  2367. IObuff + len, IOSIZE - len);
  2368. if (ml_append(lnum, IObuff, (colnr_T)STRLEN(IObuff) + 1, FALSE)
  2369. == FAIL)
  2370. break;
  2371. qfp = qfp->qf_next;
  2372. }
  2373. /* Delete the empty line which is now at the end */
  2374. (void)ml_delete(lnum + 1, FALSE);
  2375. }
  2376. /* correct cursor position */
  2377. check_lnums(TRUE);
  2378. /* Set the 'filetype' to "qf" each time after filling the buffer. This
  2379. * resembles reading a file into a buffer, it's more logical when using
  2380. * autocommands. */
  2381. set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
  2382. curbuf->b_p_ma = FALSE;
  2383. #ifdef FEAT_AUTOCMD
  2384. apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL,
  2385. FALSE, curbuf);
  2386. apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
  2387. FALSE, curbuf);
  2388. #endif
  2389. /* make sure it will be redrawn */
  2390. redraw_curbuf_later(NOT_VALID);
  2391. /* Restore KeyTyped, setting 'filetype' may reset it. */
  2392. KeyTyped = old_KeyTyped;
  2393. }
  2394. #endif /* FEAT_WINDOWS */
  2395. /*
  2396. * Return TRUE if "buf" is the quickfix buffer.
  2397. */
  2398. int
  2399. bt_quickfix(buf)
  2400. buf_T *buf;
  2401. {
  2402. return (buf->b_p_bt[0] == 'q');
  2403. }
  2404. /*
  2405. * Return TRUE if "buf" is a "nofile" or "acwrite" buffer.
  2406. * This means the buffer name is not a file name.
  2407. */
  2408. int
  2409. bt_nofile(buf)
  2410. buf_T *buf;
  2411. {
  2412. return (buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
  2413. || buf->b_p_bt[0] == 'a';
  2414. }
  2415. /*
  2416. * Return TRUE if "buf" is a "nowrite" or "nofile" buffer.
  2417. */
  2418. int
  2419. bt_dontwrite(buf)
  2420. buf_T *buf;
  2421. {
  2422. return (buf->b_p_bt[0] == 'n');
  2423. }
  2424. int
  2425. bt_dontwrite_msg(buf)
  2426. buf_T *buf;
  2427. {
  2428. if (bt_dontwrite(buf))
  2429. {
  2430. EMSG(_("E382: Cannot write, 'buftype' option is set"));
  2431. return TRUE;
  2432. }
  2433. return FALSE;
  2434. }
  2435. /*
  2436. * Return TRUE if the buffer should be hidden, according to 'hidden', ":hi