Mercurial > vim
comparison src/term.c @ 13872:9d3ddfa88a56 v8.0.1807
patch 8.0.1807: function to set terminal name is too long
commit https://github.com/vim/vim/commit/69e056915c4145b7b64c60963797692a5b293561
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu May 10 14:11:52 2018 +0200
patch 8.0.1807: function to set terminal name is too long
Problem: Function to set terminal name is too long.
Solution: Refactor the function. Fix typo in test.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Thu, 10 May 2018 14:15:05 +0200 |
parents | ca8953d36264 |
children | fc2f175e8169 |
comparison
equal
deleted
inserted
replaced
13871:4c0ff440f460 | 13872:9d3ddfa88a56 |
---|---|
1542 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB", | 1542 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB", |
1543 NULL | 1543 NULL |
1544 }; | 1544 }; |
1545 #endif | 1545 #endif |
1546 | 1546 |
1547 static void | |
1548 get_term_entries(int *height, int *width) | |
1549 { | |
1550 static struct { | |
1551 enum SpecialKey dest; /* index in term_strings[] */ | |
1552 char *name; /* termcap name for string */ | |
1553 } string_names[] = | |
1554 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"}, | |
1555 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"}, | |
1556 {KS_CL, "cl"}, {KS_CD, "cd"}, | |
1557 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"}, | |
1558 {KS_ME, "me"}, {KS_MR, "mr"}, | |
1559 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"}, | |
1560 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"}, | |
1561 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"}, | |
1562 {KS_STE,"Te"}, {KS_STS,"Ts"}, | |
1563 {KS_CM, "cm"}, {KS_SR, "sr"}, | |
1564 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"}, | |
1565 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"}, | |
1566 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"}, | |
1567 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"}, | |
1568 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"}, | |
1569 {KS_VS, "vs"}, {KS_CVS, "VS"}, | |
1570 {KS_CIS, "IS"}, {KS_CIE, "IE"}, | |
1571 {KS_CSC, "SC"}, {KS_CEC, "EC"}, | |
1572 {KS_TS, "ts"}, {KS_FS, "fs"}, | |
1573 {KS_CWP, "WP"}, {KS_CWS, "WS"}, | |
1574 {KS_CSI, "SI"}, {KS_CEI, "EI"}, | |
1575 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"}, | |
1576 {KS_8F, "8f"}, {KS_8B, "8b"}, | |
1577 {KS_CBE, "BE"}, {KS_CBD, "BD"}, | |
1578 {KS_CPS, "PS"}, {KS_CPE, "PE"}, | |
1579 {(enum SpecialKey)0, NULL} | |
1580 }; | |
1581 int i; | |
1582 char_u *p; | |
1583 static char_u tstrbuf[TBUFSZ]; | |
1584 char_u *tp = tstrbuf; | |
1585 | |
1586 /* | |
1587 * get output strings | |
1588 */ | |
1589 for (i = 0; string_names[i].name != NULL; ++i) | |
1590 { | |
1591 if (TERM_STR(string_names[i].dest) == NULL | |
1592 || TERM_STR(string_names[i].dest) == empty_option) | |
1593 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp); | |
1594 } | |
1595 | |
1596 /* tgetflag() returns 1 if the flag is present, 0 if not and | |
1597 * possibly -1 if the flag doesn't exist. */ | |
1598 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0) | |
1599 T_MS = (char_u *)"y"; | |
1600 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0) | |
1601 T_XS = (char_u *)"y"; | |
1602 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0) | |
1603 T_XN = (char_u *)"y"; | |
1604 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0) | |
1605 T_DB = (char_u *)"y"; | |
1606 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0) | |
1607 T_DA = (char_u *)"y"; | |
1608 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0) | |
1609 T_UT = (char_u *)"y"; | |
1610 | |
1611 /* | |
1612 * get key codes | |
1613 */ | |
1614 for (i = 0; key_names[i] != NULL; ++i) | |
1615 if (find_termcode((char_u *)key_names[i]) == NULL) | |
1616 { | |
1617 p = TGETSTR(key_names[i], &tp); | |
1618 /* if cursor-left == backspace, ignore it (televideo 925) */ | |
1619 if (p != NULL | |
1620 && (*p != Ctrl_H | |
1621 || key_names[i][0] != 'k' | |
1622 || key_names[i][1] != 'l')) | |
1623 add_termcode((char_u *)key_names[i], p, FALSE); | |
1624 } | |
1625 | |
1626 if (*height == 0) | |
1627 *height = tgetnum("li"); | |
1628 if (*width == 0) | |
1629 *width = tgetnum("co"); | |
1630 | |
1631 /* | |
1632 * Get number of colors (if not done already). | |
1633 */ | |
1634 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option) | |
1635 set_color_count(tgetnum("Co")); | |
1636 | |
1637 # ifndef hpux | |
1638 BC = (char *)TGETSTR("bc", &tp); | |
1639 UP = (char *)TGETSTR("up", &tp); | |
1640 p = TGETSTR("pc", &tp); | |
1641 if (p) | |
1642 PC = *p; | |
1643 # endif | |
1644 } | |
1645 | |
1646 static void | |
1647 report_term_error(char_u *error_msg, char_u *term) | |
1648 { | |
1649 struct builtin_term *termp; | |
1650 | |
1651 mch_errmsg("\r\n"); | |
1652 if (error_msg != NULL) | |
1653 { | |
1654 mch_errmsg((char *)error_msg); | |
1655 mch_errmsg("\r\n"); | |
1656 } | |
1657 mch_errmsg("'"); | |
1658 mch_errmsg((char *)term); | |
1659 mch_errmsg(_("' not known. Available builtin terminals are:")); | |
1660 mch_errmsg("\r\n"); | |
1661 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp) | |
1662 { | |
1663 if (termp->bt_entry == (int)KS_NAME) | |
1664 { | |
1665 #ifdef HAVE_TGETENT | |
1666 mch_errmsg(" builtin_"); | |
1667 #else | |
1668 mch_errmsg(" "); | |
1669 #endif | |
1670 mch_errmsg(termp->bt_string); | |
1671 mch_errmsg("\r\n"); | |
1672 } | |
1673 } | |
1674 } | |
1675 | |
1676 static void | |
1677 report_default_term(char_u *term) | |
1678 { | |
1679 mch_errmsg(_("defaulting to '")); | |
1680 mch_errmsg((char *)term); | |
1681 mch_errmsg("'\r\n"); | |
1682 if (emsg_silent == 0) | |
1683 { | |
1684 screen_start(); /* don't know where cursor is now */ | |
1685 out_flush(); | |
1686 if (!is_not_a_term()) | |
1687 ui_delay(2000L, TRUE); | |
1688 } | |
1689 } | |
1690 | |
1547 /* | 1691 /* |
1548 * Set terminal options for terminal "term". | 1692 * Set terminal options for terminal "term". |
1549 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise. | 1693 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise. |
1550 * | 1694 * |
1551 * While doing this, until ttest(), some options may be NULL, be careful. | 1695 * While doing this, until ttest(), some options may be NULL, be careful. |
1593 /* | 1737 /* |
1594 * Use external termcap | 1738 * Use external termcap |
1595 */ | 1739 */ |
1596 if (try == 1) | 1740 if (try == 1) |
1597 { | 1741 { |
1598 char_u *p; | |
1599 static char_u tstrbuf[TBUFSZ]; | |
1600 int i; | |
1601 char_u tbuf[TBUFSZ]; | 1742 char_u tbuf[TBUFSZ]; |
1602 char_u *tp; | |
1603 static struct { | |
1604 enum SpecialKey dest; /* index in term_strings[] */ | |
1605 char *name; /* termcap name for string */ | |
1606 } string_names[] = | |
1607 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"}, | |
1608 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"}, | |
1609 {KS_CL, "cl"}, {KS_CD, "cd"}, | |
1610 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"}, | |
1611 {KS_ME, "me"}, {KS_MR, "mr"}, | |
1612 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"}, | |
1613 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"}, | |
1614 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"}, | |
1615 {KS_STE,"Te"}, {KS_STS,"Ts"}, | |
1616 {KS_CM, "cm"}, {KS_SR, "sr"}, | |
1617 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"}, | |
1618 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"}, | |
1619 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"}, | |
1620 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"}, | |
1621 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"}, | |
1622 {KS_VS, "vs"}, {KS_CVS, "VS"}, | |
1623 {KS_CIS, "IS"}, {KS_CIE, "IE"}, | |
1624 {KS_CSC, "SC"}, {KS_CEC, "EC"}, | |
1625 {KS_TS, "ts"}, {KS_FS, "fs"}, | |
1626 {KS_CWP, "WP"}, {KS_CWS, "WS"}, | |
1627 {KS_CSI, "SI"}, {KS_CEI, "EI"}, | |
1628 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"}, | |
1629 {KS_8F, "8f"}, {KS_8B, "8b"}, | |
1630 {KS_CBE, "BE"}, {KS_CBD, "BD"}, | |
1631 {KS_CPS, "PS"}, {KS_CPE, "PE"}, | |
1632 {(enum SpecialKey)0, NULL} | |
1633 }; | |
1634 | 1743 |
1635 /* | 1744 /* |
1636 * If the external termcap does not have a matching entry, try the | 1745 * If the external termcap does not have a matching entry, try the |
1637 * builtin ones. | 1746 * builtin ones. |
1638 */ | 1747 */ |
1639 if ((error_msg = tgetent_error(tbuf, term)) == NULL) | 1748 if ((error_msg = tgetent_error(tbuf, term)) == NULL) |
1640 { | 1749 { |
1641 tp = tstrbuf; | |
1642 if (!termcap_cleared) | 1750 if (!termcap_cleared) |
1643 { | 1751 { |
1644 clear_termoptions(); /* clear old options */ | 1752 clear_termoptions(); /* clear old options */ |
1645 termcap_cleared = TRUE; | 1753 termcap_cleared = TRUE; |
1646 } | 1754 } |
1647 | 1755 |
1648 /* get output strings */ | 1756 get_term_entries(&height, &width); |
1649 for (i = 0; string_names[i].name != NULL; ++i) | |
1650 { | |
1651 if (TERM_STR(string_names[i].dest) == NULL | |
1652 || TERM_STR(string_names[i].dest) == empty_option) | |
1653 TERM_STR(string_names[i].dest) = | |
1654 TGETSTR(string_names[i].name, &tp); | |
1655 } | |
1656 | |
1657 /* tgetflag() returns 1 if the flag is present, 0 if not and | |
1658 * possibly -1 if the flag doesn't exist. */ | |
1659 if ((T_MS == NULL || T_MS == empty_option) | |
1660 && tgetflag("ms") > 0) | |
1661 T_MS = (char_u *)"y"; | |
1662 if ((T_XS == NULL || T_XS == empty_option) | |
1663 && tgetflag("xs") > 0) | |
1664 T_XS = (char_u *)"y"; | |
1665 if ((T_XN == NULL || T_XN == empty_option) | |
1666 && tgetflag("xn") > 0) | |
1667 T_XN = (char_u *)"y"; | |
1668 if ((T_DB == NULL || T_DB == empty_option) | |
1669 && tgetflag("db") > 0) | |
1670 T_DB = (char_u *)"y"; | |
1671 if ((T_DA == NULL || T_DA == empty_option) | |
1672 && tgetflag("da") > 0) | |
1673 T_DA = (char_u *)"y"; | |
1674 if ((T_UT == NULL || T_UT == empty_option) | |
1675 && tgetflag("ut") > 0) | |
1676 T_UT = (char_u *)"y"; | |
1677 | |
1678 | |
1679 /* | |
1680 * get key codes | |
1681 */ | |
1682 for (i = 0; key_names[i] != NULL; ++i) | |
1683 { | |
1684 if (find_termcode((char_u *)key_names[i]) == NULL) | |
1685 { | |
1686 p = TGETSTR(key_names[i], &tp); | |
1687 /* if cursor-left == backspace, ignore it (televideo | |
1688 * 925) */ | |
1689 if (p != NULL | |
1690 && (*p != Ctrl_H | |
1691 || key_names[i][0] != 'k' | |
1692 || key_names[i][1] != 'l')) | |
1693 add_termcode((char_u *)key_names[i], p, FALSE); | |
1694 } | |
1695 } | |
1696 | |
1697 if (height == 0) | |
1698 height = tgetnum("li"); | |
1699 if (width == 0) | |
1700 width = tgetnum("co"); | |
1701 | |
1702 /* | |
1703 * Get number of colors (if not done already). | |
1704 */ | |
1705 if (TERM_STR(KS_CCO) == NULL | |
1706 || TERM_STR(KS_CCO) == empty_option) | |
1707 set_color_count(tgetnum("Co")); | |
1708 | |
1709 # ifndef hpux | |
1710 BC = (char *)TGETSTR("bc", &tp); | |
1711 UP = (char *)TGETSTR("up", &tp); | |
1712 p = TGETSTR("pc", &tp); | |
1713 if (p) | |
1714 PC = *p; | |
1715 # endif /* hpux */ | |
1716 } | 1757 } |
1717 } | 1758 } |
1718 else /* try == 0 || try == 2 */ | 1759 else /* try == 0 || try == 2 */ |
1719 #endif /* HAVE_TGETENT */ | 1760 #endif /* HAVE_TGETENT */ |
1720 /* | 1761 /* |
1746 if (try == 0) /* try external one */ | 1787 if (try == 0) /* try external one */ |
1747 continue; | 1788 continue; |
1748 if (termcap_cleared) /* found in external termcap */ | 1789 if (termcap_cleared) /* found in external termcap */ |
1749 break; | 1790 break; |
1750 #endif | 1791 #endif |
1751 | 1792 report_term_error(error_msg, term); |
1752 mch_errmsg("\r\n"); | 1793 |
1753 if (error_msg != NULL) | |
1754 { | |
1755 mch_errmsg((char *)error_msg); | |
1756 mch_errmsg("\r\n"); | |
1757 } | |
1758 mch_errmsg("'"); | |
1759 mch_errmsg((char *)term); | |
1760 mch_errmsg(_("' not known. Available builtin terminals are:")); | |
1761 mch_errmsg("\r\n"); | |
1762 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; | |
1763 ++termp) | |
1764 { | |
1765 if (termp->bt_entry == (int)KS_NAME) | |
1766 { | |
1767 #ifdef HAVE_TGETENT | |
1768 mch_errmsg(" builtin_"); | |
1769 #else | |
1770 mch_errmsg(" "); | |
1771 #endif | |
1772 mch_errmsg(termp->bt_string); | |
1773 mch_errmsg("\r\n"); | |
1774 } | |
1775 } | |
1776 /* when user typed :set term=xxx, quit here */ | 1794 /* when user typed :set term=xxx, quit here */ |
1777 if (starting != NO_SCREEN) | 1795 if (starting != NO_SCREEN) |
1778 { | 1796 { |
1779 screen_start(); /* don't know where cursor is now */ | 1797 screen_start(); /* don't know where cursor is now */ |
1780 wait_return(TRUE); | 1798 wait_return(TRUE); |
1781 return FAIL; | 1799 return FAIL; |
1782 } | 1800 } |
1783 term = DEFAULT_TERM; | 1801 term = DEFAULT_TERM; |
1784 mch_errmsg(_("defaulting to '")); | 1802 report_default_term(term); |
1785 mch_errmsg((char *)term); | |
1786 mch_errmsg("'\r\n"); | |
1787 if (emsg_silent == 0) | |
1788 { | |
1789 screen_start(); /* don't know where cursor is now */ | |
1790 out_flush(); | |
1791 if (!is_not_a_term()) | |
1792 ui_delay(2000L, TRUE); | |
1793 } | |
1794 set_string_option_direct((char_u *)"term", -1, term, | 1803 set_string_option_direct((char_u *)"term", -1, term, |
1795 OPT_FREE, 0); | 1804 OPT_FREE, 0); |
1796 display_errors(); | 1805 display_errors(); |
1797 } | 1806 } |
1798 out_flush(); | 1807 out_flush(); |