Mercurial > vim
comparison src/eval.c @ 7730:80ce794827c4 v7.4.1163
commit https://github.com/vim/vim/commit/17a13437c9414a8693369a97f3be2fc8ad48c12e
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Jan 24 14:22:10 2016 +0100
patch 7.4.1163
Problem: Expressions "0 + v:true" and "'' . v:true" cause an error.
Solution: Return something sensible when using a special variable as a
number or as a string. (suggested by Damien)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 24 Jan 2016 14:30:04 +0100 |
parents | 7c52f11e6df3 |
children | 00fc76e6bd99 |
comparison
equal
deleted
inserted
replaced
7729:0f5ca5591cda | 7730:80ce794827c4 |
---|---|
7818 } | 7818 } |
7819 | 7819 |
7820 return OK; | 7820 return OK; |
7821 } | 7821 } |
7822 | 7822 |
7823 static char * | |
7824 get_var_special_name(int nr) | |
7825 { | |
7826 switch (nr) | |
7827 { | |
7828 case VVAL_FALSE: return "false"; | |
7829 case VVAL_TRUE: return "true"; | |
7830 case VVAL_NONE: return "none"; | |
7831 case VVAL_NULL: return "null"; | |
7832 } | |
7833 EMSG2(_(e_intern2), "get_var_special_name()"); | |
7834 return "42"; | |
7835 } | |
7836 | |
7823 /* | 7837 /* |
7824 * Return a string with the string representation of a variable. | 7838 * Return a string with the string representation of a variable. |
7825 * If the memory is allocated "tofree" is set to it, otherwise NULL. | 7839 * If the memory is allocated "tofree" is set to it, otherwise NULL. |
7826 * "numbuf" is used for a number. | 7840 * "numbuf" is used for a number. |
7827 * Does not put quotes around strings, as ":echo" displays values. | 7841 * Does not put quotes around strings, as ":echo" displays values. |
7912 break; | 7926 break; |
7913 #endif | 7927 #endif |
7914 | 7928 |
7915 case VAR_SPECIAL: | 7929 case VAR_SPECIAL: |
7916 *tofree = NULL; | 7930 *tofree = NULL; |
7917 switch (tv->vval.v_number) | 7931 r = (char_u *)get_var_special_name(tv->vval.v_number); |
7918 { | |
7919 case VVAL_FALSE: r = (char_u *)"false"; break; | |
7920 case VVAL_TRUE: r = (char_u *)"true"; break; | |
7921 case VVAL_NONE: r = (char_u *)"none"; break; | |
7922 case VVAL_NULL: r = (char_u *)"null"; break; | |
7923 default: EMSG2(_(e_intern2), "echo_string(special)"); | |
7924 } | |
7925 break; | 7932 break; |
7926 | 7933 |
7927 default: | 7934 default: |
7928 EMSG2(_(e_intern2), "echo_string()"); | 7935 EMSG2(_(e_intern2), "echo_string()"); |
7929 *tofree = NULL; | 7936 *tofree = NULL; |
21702 EMSG(_("E745: Using a List as a Number")); | 21709 EMSG(_("E745: Using a List as a Number")); |
21703 break; | 21710 break; |
21704 case VAR_DICT: | 21711 case VAR_DICT: |
21705 EMSG(_("E728: Using a Dictionary as a Number")); | 21712 EMSG(_("E728: Using a Dictionary as a Number")); |
21706 break; | 21713 break; |
21714 case VAR_SPECIAL: | |
21715 return varp->vval.v_number == VVAL_TRUE ? 1 : 0; | |
21716 break; | |
21707 default: | 21717 default: |
21708 EMSG2(_(e_intern2), "get_tv_number()"); | 21718 EMSG2(_(e_intern2), "get_tv_number()"); |
21709 break; | 21719 break; |
21710 } | 21720 } |
21711 if (denote == NULL) /* useful for values that must be unsigned */ | 21721 if (denote == NULL) /* useful for values that must be unsigned */ |
21857 #endif | 21867 #endif |
21858 case VAR_STRING: | 21868 case VAR_STRING: |
21859 if (varp->vval.v_string != NULL) | 21869 if (varp->vval.v_string != NULL) |
21860 return varp->vval.v_string; | 21870 return varp->vval.v_string; |
21861 return (char_u *)""; | 21871 return (char_u *)""; |
21872 case VAR_SPECIAL: | |
21873 STRCPY(buf, get_var_special_name(varp->vval.v_number)); | |
21874 return buf; | |
21875 | |
21862 default: | 21876 default: |
21863 EMSG2(_(e_intern2), "get_tv_string_buf()"); | 21877 EMSG2(_(e_intern2), "get_tv_string_buf()"); |
21864 break; | 21878 break; |
21865 } | 21879 } |
21866 return NULL; | 21880 return NULL; |