home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / unix / vim-6.2.tar.bz2 / vim-6.2.tar / vim62 / src / quickfix.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-05-30  |  51.9 KB  |  2,207 lines

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