~ubuntu-branches/debian/sid/neovim/sid

« back to all changes in this revision

Viewing changes to src/nvim/if_cscope.c

  • Committer: Package Import Robot
  • Author(s): James McCoy
  • Date: 2016-04-18 21:42:19 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20160418214219-1e6d4o1fwqarzk46
Tags: 0.1.3-1
* New upstream release.  (Closes: #820562)
* debian/control:
  + Remove unnecessary luarocks Build-Depends
  + Add libkvm-dev Build-Depends for kfreebsd-*
  + Add python(3)-neovim to Recommends.  (Closes: #812737)
  + Declare compiance with policy 3.9.8, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1632
1632
  return s;
1633
1633
}
1634
1634
 
1635
 
/*
1636
 
 * PRIVATE: cs_print_tags_priv
1637
 
 *
1638
 
 * called from cs_manage_matches()
1639
 
 */
 
1635
/// Print cscope output that was converted into ctags style entries.
 
1636
///
 
1637
/// Only called from cs_manage_matches().
 
1638
///
 
1639
/// @param matches     Array of cscope lines in ctags style. Every entry was
 
1640
//                     produced with a format string of the form
 
1641
//                          "%s\t%s\t%s;\"\t%s" or
 
1642
//                          "%s\t%s\t%s;\""
 
1643
//                     by cs_make_vim_style_matches().
 
1644
/// @param cntxts      Context for matches.
 
1645
/// @param num_matches Number of entries in matches/cntxts, always greater 0.
1640
1646
static void cs_print_tags_priv(char **matches, char **cntxts,
1641
 
                                size_t num_matches)
 
1647
                               size_t num_matches) FUNC_ATTR_NONNULL_ALL
1642
1648
{
1643
 
  char        *ptag;
1644
 
  char        *fname, *lno, *extra, *tbuf;
1645
 
  size_t      num;
1646
 
  char        *globalcntx = "GLOBAL";
1647
 
  char        *context;
1648
 
  char        *cstag_msg = _("Cscope tag: %s");
1649
 
 
1650
 
  assert (num_matches > 0);
1651
 
 
1652
 
  tbuf = xmalloc(strlen(matches[0]) + 1);
1653
 
 
1654
 
  strcpy(tbuf, matches[0]);
1655
 
  ptag = strtok(tbuf, "\t");
1656
 
 
1657
 
  size_t newsize = strlen(cstag_msg) + strlen(ptag);
 
1649
  char *globalcntx = "GLOBAL";
 
1650
  char *cstag_msg = _("Cscope tag: %s");
 
1651
 
 
1652
  assert(num_matches > 0);
 
1653
  assert(strcnt(matches[0], '\t') >= 2);
 
1654
 
 
1655
  char *ptag = matches[0];
 
1656
  char *ptag_end = strchr(ptag, '\t');
 
1657
  assert(ptag_end >= ptag);
 
1658
  // NUL terminate tag string in matches[0].
 
1659
  *ptag_end = NUL;
 
1660
 
 
1661
  // The "%s" in cstag_msg won't appear in the result string, so we don't need
 
1662
  // extra memory for terminating NUL.
 
1663
  size_t newsize = strlen(cstag_msg) + (size_t)(ptag_end - ptag);
1658
1664
  char *buf = xmalloc(newsize);
1659
1665
  size_t bufsize = newsize;  // Track available bufsize
1660
 
  (void)sprintf(buf, cstag_msg, ptag);
 
1666
  (void)snprintf(buf, bufsize, cstag_msg, ptag);
1661
1667
  MSG_PUTS_ATTR(buf, hl_attr(HLF_T));
1662
 
 
1663
 
  xfree(tbuf);
1664
 
 
1665
 
  MSG_PUTS_ATTR(_("\n   #   line"), hl_attr(HLF_T));      /* strlen is 7 */
 
1668
  msg_clr_eos();
 
1669
 
 
1670
  // restore matches[0]
 
1671
  *ptag_end = '\t';
 
1672
 
 
1673
  // Column headers for match number, line number and filename.
 
1674
  MSG_PUTS_ATTR(_("\n   #   line"), hl_attr(HLF_T));
1666
1675
  msg_advance(msg_col + 2);
1667
1676
  MSG_PUTS_ATTR(_("filename / context / line\n"), hl_attr(HLF_T));
1668
1677
 
1669
 
  num = 1;
1670
1678
  for (size_t i = 0; i < num_matches; i++) {
1671
 
    size_t idx = i;
1672
 
 
1673
 
    /* if we really wanted to, we could avoid this malloc and strcpy
1674
 
     * by parsing matches[i] on the fly and placing stuff into buf
1675
 
     * directly, but that's too much of a hassle
1676
 
     */
1677
 
    tbuf = xmalloc(strlen(matches[idx]) + 1);
1678
 
    (void)strcpy(tbuf, matches[idx]);
1679
 
 
1680
 
    if (strtok(tbuf, (const char *)"\t") == NULL)
1681
 
      continue;
1682
 
    if ((fname = strtok(NULL, (const char *)"\t")) == NULL)
1683
 
      continue;
1684
 
    if ((lno = strtok(NULL, (const char *)"\t")) == NULL)
1685
 
      continue;
1686
 
    extra = strtok(NULL, (const char *)"\t");
1687
 
 
1688
 
    lno[strlen(lno)-2] = '\0';      /* ignore ;" at the end */
 
1679
    assert(strcnt(matches[i], '\t') >= 2);
 
1680
 
 
1681
    // Parse filename, line number and optional part.
 
1682
    char *fname = strchr(matches[i], '\t') + 1;
 
1683
    char *fname_end = strchr(fname, '\t');
 
1684
    // Replace second '\t' in matches[i] with NUL to terminate fname.
 
1685
    *fname_end = NUL;
 
1686
 
 
1687
    char *lno = fname_end + 1;
 
1688
    char *extra = xstrchrnul(lno, '\t');
 
1689
    // Ignore ;" at the end of lno.
 
1690
    char *lno_end = extra - 2;
 
1691
    *lno_end = NUL;
 
1692
    // Do we have an optional part?
 
1693
    extra = *extra ? extra + 1 : NULL;
1689
1694
 
1690
1695
    const char *csfmt_str = "%4zu %6s  ";
1691
 
    /* hopefully 'num' (num of matches) will be less than 10^16 */
1692
 
    newsize = strlen(csfmt_str) + 16 + strlen(lno);
 
1696
    // hopefully num_matches will be less than 10^16
 
1697
    newsize = strlen(csfmt_str) + 16 + (size_t)(lno_end - lno);
1693
1698
    if (bufsize < newsize) {
1694
1699
      buf = xrealloc(buf, newsize);
1695
1700
      bufsize = newsize;
1696
1701
    }
1697
 
    (void)sprintf(buf, csfmt_str, num, lno);
 
1702
    (void)snprintf(buf, bufsize, csfmt_str, i + 1, lno);
1698
1703
    MSG_PUTS_ATTR(buf, hl_attr(HLF_CM));
1699
1704
    MSG_PUTS_LONG_ATTR(cs_pathcomponents(fname), hl_attr(HLF_CM));
1700
1705
 
1701
 
    /* compute the required space for the context */
1702
 
    if (cntxts[idx] != NULL)
1703
 
      context = cntxts[idx];
1704
 
    else
1705
 
      context = globalcntx;
 
1706
    // compute the required space for the context
 
1707
    char *context = cntxts[i] ? cntxts[i] : globalcntx;
1706
1708
 
1707
1709
    const char *cntxformat = " <<%s>>";
1708
1710
    // '%s' won't appear in result string, so:
1713
1715
      buf = xrealloc(buf, newsize);
1714
1716
      bufsize = newsize;
1715
1717
    }
1716
 
    (void)sprintf(buf, cntxformat, context);
 
1718
    int buf_len = snprintf(buf, bufsize, cntxformat, context);
 
1719
    assert(buf_len >= 0);
1717
1720
 
1718
 
    /* print the context only if it fits on the same line */
1719
 
    if (msg_col + (int)strlen(buf) >= (int)Columns)
 
1721
    // Print the context only if it fits on the same line.
 
1722
    if (msg_col + buf_len >= (int)Columns) {
1720
1723
      msg_putchar('\n');
 
1724
    }
1721
1725
    msg_advance(12);
1722
1726
    MSG_PUTS_LONG(buf);
1723
1727
    msg_putchar('\n');
1726
1730
      MSG_PUTS_LONG(extra);
1727
1731
    }
1728
1732
 
1729
 
    xfree(tbuf);     /* only after printing extra due to strtok use */
 
1733
    // restore matches[i]
 
1734
    *fname_end = '\t';
 
1735
    *lno_end = ';';
1730
1736
 
1731
 
    if (msg_col)
 
1737
    if (msg_col) {
1732
1738
      msg_putchar('\n');
 
1739
    }
1733
1740
 
1734
1741
    os_breakcheck();
1735
1742
    if (got_int) {
1736
 
      got_int = FALSE;          /* don't print any more matches */
 
1743
      got_int = false;  // don't print any more matches
1737
1744
      break;
1738
1745
    }
1739
 
 
1740
 
    num++;
1741
 
  }   /* for all matches */
 
1746
  }
1742
1747
 
1743
1748
  xfree(buf);
1744
 
} /* cs_print_tags_priv */
1745
 
 
 
1749
}
1746
1750
 
1747
1751
/*
1748
1752
 * PRIVATE: cs_read_prompt