Skip to content

Instantly share code, notes, and snippets.

@kat0h
Last active May 22, 2022 09:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kat0h/f743f2e42c6f1cf538241b97726788a3 to your computer and use it in GitHub Desktop.
Save kat0h/f743f2e42c6f1cf538241b97726788a3 to your computer and use it in GitHub Desktop.
has('wsl') porting for vim (供養)
diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt
index 72e2cf13f..833a39a11 100644
--- a/runtime/doc/builtin.txt
+++ b/runtime/doc/builtin.txt
@@ -10445,6 +10445,7 @@ xpm Compiled with pixmap support.
xpm_w32 Compiled with pixmap support for Win32. (Only for
backward compatibility. Use "xpm" instead.)
xsmp Compiled with X session management support.
+wsl Vim is running on WSL (Windows Subsystem for Linux).
xsmp_interact Compiled with interactive X session management support.
xterm_clipboard Compiled with support for xterm clipboard.
xterm_save Compiled with support for saving and restoring the
diff --git a/src/evalfunc.c b/src/evalfunc.c
index 35b8984c3..00bf0e56e 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -5260,6 +5260,9 @@ f_has(typval_T *argvars, typval_T *rettv)
char *name;
short present;
} has_item_T;
+#if defined(__linux__) && defined(HAVE_SYS_UTSNAME_H)
+ char_u kernel_release[256];
+#endif
static has_item_T has_list[] =
{
{"amiga",
@@ -6548,6 +6551,20 @@ f_has(typval_T *argvars, typval_T *rettv)
x = TRUE;
#ifdef FEAT_CLIPBOARD
n = clip_star.available;
+#endif
+ }
+ else if (STRICMP(name, "wsl") == 0)
+ {
+ x = TRUE;
+#if defined(__linux__) && defined(HAVE_SYS_UTSNAME_H)
+ mch_get_kernel_release(kernel_release, 256);
+ vim_strup(kernel_release);
+ if (strstr((char *)kernel_release, "MICROSOFT") != NULL)
+ n = TRUE;
+ else
+ n = FALSE;
+#else
+ n = FALSE;
#endif
}
}
diff --git a/src/os_unix.c b/src/os_unix.c
index b786f8df0..d5ed493c4 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -2526,6 +2526,21 @@ mch_get_host_name(char_u *s, int len)
}
#endif // HAVE_SYS_UTSNAME_H
+/*
+ * Insert kernel release is s[len] (like uname(1))
+ */
+#ifdef HAVE_SYS_UTSNAME_H
+ void
+mch_get_kernel_release(char_u *s, int len) {
+ struct utsname vutsname;
+
+ if (uname(&vutsname) < 0)
+ *s = NUL;
+ else
+ vim_strncpy(s, (char_u *)vutsname.release, len - 1);
+}
+#endif
+
/*
* return process ID
*/
diff --git a/src/proto/os_unix.pro b/src/proto/os_unix.pro
index 7dd8e231b..ace67ddd7 100644
--- a/src/proto/os_unix.pro
+++ b/src/proto/os_unix.pro
@@ -27,6 +27,7 @@ int vim_is_fastterm(char_u *name);
int mch_get_user_name(char_u *s, int len);
int mch_get_uname(uid_t uid, char_u *s, int len);
void mch_get_host_name(char_u *s, int len);
+void mch_get_kernel_release(char_u *s, int len);
long mch_get_pid(void);
int mch_process_running(long pid);
int mch_dirname(char_u *buf, int len);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment