aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2015-05-05 00:15:09 -0400
committerJustin M. Keyes <justinkz@gmail.com>2016-10-25 11:40:37 +0200
commite7e2844d468dc0551fbd58097cc7f8f881e75ce3 (patch)
treefb13d138c7d124e128282d3711a0a86b323d986c
parentf96dfae52fe5ec211f6cfdf539882f2d10806374 (diff)
downloadrneovim-e7e2844d468dc0551fbd58097cc7f8f881e75ce3.tar.gz
rneovim-e7e2844d468dc0551fbd58097cc7f8f881e75ce3.tar.bz2
rneovim-e7e2844d468dc0551fbd58097cc7f8f881e75ce3.zip
version: has("nvim-1.2.3")
Helped-by: Daniel Hahler <git@thequod.de>
-rw-r--r--runtime/doc/eval.txt9
-rw-r--r--src/nvim/eval.c42
-rw-r--r--src/nvim/syntax.c2
-rw-r--r--src/nvim/version.c57
-rw-r--r--test/functional/eval/has_spec.lua51
5 files changed, 130 insertions, 31 deletions
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index cfd62bacfe..3ce0d1cd87 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -7453,7 +7453,11 @@ There are four types of features:
Example: >
:if has("gui_running")
< *has-patch*
-3. Included patches. The "patch123" feature means that patch 123 has been
+3. {Nvim} version. The "nvim-1.2.3" feature means that the Nvim version is
+ 1.2.3 or later. Example: >
+ :if has("nvim-1.2.3")
+<
+4. Included patches. The "patch123" feature means that patch 123 has been
included. Note that this form does not check the version of Vim, you need
to inspect |v:version| for that.
Example (checking version 6.2.148 or later): >
@@ -7461,7 +7465,7 @@ There are four types of features:
< Note that it's possible for patch 147 to be omitted even though 148 is
included.
-4. Beyond a certain version or at a certain version and including a specific
+5. Beyond a certain version or at a certain version and including a specific
patch. The "patch-7.4.237" feature means that the Vim version is 7.5 or
later, or it is version 7.4 and patch 237 was included.
Note that this only works for patch 7.4.237 and later, before that you
@@ -7533,6 +7537,7 @@ multi_byte Compiled with support for 'encoding'
multi_byte_encoding 'encoding' is set to a multi-byte encoding.
multi_byte_ime Compiled with support for IME input method.
multi_lang Compiled with support for multiple languages.
+nvim This is Nvim. |has-patch|
ole Compiled with OLE automation support for Win32.
path_extra Compiled with up/downwards search in 'path' and 'tags'
persistent_undo Compiled with support for persistent undo history.
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 76f33e7d8c..9cd9b5c8b4 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -10524,16 +10524,10 @@ static void f_glob2regpat(typval_T *argvars, typval_T *rettv, FunPtr fptr)
: file_pat_to_reg_pat(pat, NULL, NULL, false);
}
-/*
- * "has()" function
- */
+/// "has()" function
static void f_has(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
- int i;
- char_u *name;
- int n = FALSE;
- static char *(has_list[]) =
- {
+ static char *(has_list[]) = {
#ifdef UNIX
"unix",
#endif
@@ -10646,36 +10640,44 @@ static void f_has(typval_T *argvars, typval_T *rettv, FunPtr fptr)
NULL
};
- name = get_tv_string(&argvars[0]);
- for (i = 0; has_list[i] != NULL; ++i)
+ bool n = false;
+ char *name = (char *)get_tv_string(&argvars[0]);
+
+ for (int i = 0; has_list[i] != NULL; i++) {
if (STRICMP(name, has_list[i]) == 0) {
- n = TRUE;
+ n = true;
break;
}
+ }
- if (n == FALSE) {
+ if (!n) {
if (STRNICMP(name, "patch", 5) == 0) {
if (name[5] == '-'
- && STRLEN(name) > 11
+ && strlen(name) > 11
&& ascii_isdigit(name[6])
&& ascii_isdigit(name[8])
&& ascii_isdigit(name[10])) {
- int major = atoi((char *)name + 6);
- int minor = atoi((char *)name + 8);
+ int major = atoi(name + 6);
+ int minor = atoi(name + 8);
// Expect "patch-9.9.01234".
n = (major < VIM_VERSION_MAJOR
|| (major == VIM_VERSION_MAJOR
&& (minor < VIM_VERSION_MINOR
|| (minor == VIM_VERSION_MINOR
- && has_patch(atoi((char *)name + 10))))));
+ && has_vim_patch(atoi(name + 10))))));
} else {
- n = has_patch(atoi((char *)name + 5));
+ n = has_vim_patch(atoi(name + 5));
+ }
+ } else if (STRNICMP(name, "nvim", 4) == 0) {
+ // Expect "nvim-x.y.z"
+ if (name[4] == '-' && strlen(name) >= 10) {
+ n = has_nvim_version(name + 5);
}
} else if (STRICMP(name, "vim_starting") == 0) {
n = (starting != 0);
} else if (STRICMP(name, "multi_byte_encoding") == 0) {
- n = has_mbyte;
+ n = has_mbyte != 0;
#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
} else if (STRICMP(name, "iconv") == 0) {
n = iconv_enabled(false);
@@ -10685,8 +10687,8 @@ static void f_has(typval_T *argvars, typval_T *rettv, FunPtr fptr)
}
}
- if (n == FALSE && eval_has_provider((char *)name)) {
- n = TRUE;
+ if (!n && eval_has_provider(name)) {
+ n = true;
}
rettv->vval.v_number = n;
diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c
index 6fd7603629..b49ae9da21 100644
--- a/src/nvim/syntax.c
+++ b/src/nvim/syntax.c
@@ -5537,7 +5537,7 @@ void ex_ownsyntax(exarg_T *eap)
}
}
-int syntax_present(win_T *win)
+bool syntax_present(win_T *win)
{
return win->w_s->b_syn_patterns.ga_len != 0
|| win->w_s->b_syn_clusters.ga_len != 0
diff --git a/src/nvim/version.c b/src/nvim/version.c
index 2dfe5d3248..6b9c8bf6b1 100644
--- a/src/nvim/version.c
+++ b/src/nvim/version.c
@@ -2457,20 +2457,61 @@ static char *(extra_patches[]) = {
NULL
};
-/// Checks whether patch `n` has been included.
+/// Compares a version string to the current Nvim version.
///
-/// @param n The patch number.
+/// @param version Version string like "1.3.42"
///
-/// @return TRUE if patch "n" has been included.
-int has_patch(int n)
+/// @return true if Nvim is at or above the version.
+bool has_nvim_version(char *version_str)
+ FUNC_ATTR_NONNULL_ALL
{
- int i;
- for (i = 0; included_patches[i] != 0; ++i) {
+ char *p = version_str;
+ int major = 0;
+ int minor = 0;
+ int patch = 0;
+
+ if (!ascii_isdigit(*p)) {
+ return false;
+ }
+ major = atoi(p);
+ p = strchr(p, '.'); // Find the next dot.
+
+ if (p) {
+ p++; // Advance past the dot.
+ if (!ascii_isdigit(*p)) {
+ return false;
+ }
+ minor = atoi(p);
+ p = strchr(p, '.');
+ if (p) {
+ p++;
+ if (!ascii_isdigit(*p)) {
+ return false;
+ }
+ patch = atoi(p);
+ }
+ }
+
+ return (major < NVIM_VERSION_MAJOR
+ || (major == NVIM_VERSION_MAJOR
+ && (minor < NVIM_VERSION_MINOR
+ || (minor == NVIM_VERSION_MINOR
+ && patch <= NVIM_VERSION_PATCH))));
+}
+
+/// Checks whether a Vim patch has been included.
+///
+/// @param n Patch number.
+///
+/// @return true if patch `n` has been included.
+bool has_vim_patch(int n)
+{
+ for (int i = 0; included_patches[i] != 0; i++) {
if (included_patches[i] == n) {
- return TRUE;
+ return true;
}
}
- return FALSE;
+ return false;
}
void ex_version(exarg_T *eap)
diff --git a/test/functional/eval/has_spec.lua b/test/functional/eval/has_spec.lua
new file mode 100644
index 0000000000..ecd25c7123
--- /dev/null
+++ b/test/functional/eval/has_spec.lua
@@ -0,0 +1,51 @@
+local helpers = require('test.functional.helpers')(after_each)
+local eq = helpers.eq
+local clear = helpers.clear
+local funcs = helpers.funcs
+
+describe('has()', function()
+ before_each(clear)
+
+ it('"nvim-x.y.z"', function()
+ eq(0, funcs.has("nvim-"))
+ eq(0, funcs.has("nvim- "))
+ eq(0, funcs.has("nvim- \t "))
+ eq(0, funcs.has("nvim-0. 1. 1"))
+ eq(0, funcs.has("nvim-0. 1.1"))
+ eq(0, funcs.has("nvim-0.1. 1"))
+ eq(0, funcs.has("nvim-a"))
+ eq(0, funcs.has("nvim-a.b.c"))
+ eq(0, funcs.has("nvim-0.b.c"))
+ eq(0, funcs.has("nvim-0.0.c"))
+ eq(0, funcs.has("nvim-0.b.0"))
+ eq(0, funcs.has("nvim-a.b.0"))
+ eq(0, funcs.has("nvim-0.1"))
+ eq(0, funcs.has("nvim-.0.0.0"))
+ eq(0, funcs.has("nvim-.0"))
+ eq(0, funcs.has("nvim-0."))
+ eq(0, funcs.has("nvim-0.."))
+ eq(0, funcs.has("nvim-."))
+ eq(0, funcs.has("nvim-.."))
+ eq(0, funcs.has("nvim-..."))
+ eq(0, funcs.has("nvim-42"))
+ eq(0, funcs.has("nvim-9999"))
+ eq(0, funcs.has("nvim-99.001.05"))
+
+ eq(1, funcs.has("nvim"))
+ eq(1, funcs.has("nvim-0.0.0"))
+ eq(1, funcs.has("nvim-0.1.1."))
+ eq(1, funcs.has("nvim-0.1.1.abc"))
+ eq(1, funcs.has("nvim-0.1.1.."))
+ eq(1, funcs.has("nvim-0.1.1.. .."))
+ eq(1, funcs.has("nvim-0.1.1.... "))
+ eq(1, funcs.has("nvim-0.0.0"))
+ eq(1, funcs.has("nvim-0.0.1"))
+ eq(1, funcs.has("nvim-0.1.0"))
+ eq(1, funcs.has("nvim-0.1.1"))
+ eq(1, funcs.has("nvim-0.1.5"))
+ eq(1, funcs.has("nvim-0000.001.05"))
+ eq(1, funcs.has("nvim-0.01.005"))
+ eq(1, funcs.has("nvim-00.001.05"))
+ end)
+
+end)