diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index cb56e1785..181468e3c 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -49,7 +49,7 @@ jobs: - name: Install dependencies run: | sudo apt-get update - sudo apt-get install ninja-build gettext libtool libtool-bin autoconf automake cmake g++ pkg-config unzip curl + sudo apt-get install ninja-build gettext libtool libtool-bin autoconf automake cmake g++ pkg-config unzip curl lua5.2 liblua5.2-dev - name: Install ${{ matrix.vimbin }} ${{ matrix.vimtag }} run: | .ci/install/linux.sh $VIM_BIN $VIM_TAG diff --git a/autoload/SpaceVim/api/system.vim b/autoload/SpaceVim/api/system.vim index 57b04aa2c..d25a632bc 100644 --- a/autoload/SpaceVim/api/system.vim +++ b/autoload/SpaceVim/api/system.vim @@ -40,7 +40,7 @@ function! s:name() abort endif endfunction -let s:system['name'] = s:name() +let s:system['name'] = function('s:name') function! s:isDarwin() abort if exists('s:is_darwin') diff --git a/autoload/SpaceVim/api/vim/compatible.vim b/autoload/SpaceVim/api/vim/compatible.vim index ee16d1f66..7b6e3bb55 100644 --- a/autoload/SpaceVim/api/vim/compatible.vim +++ b/autoload/SpaceVim/api/vim/compatible.vim @@ -345,6 +345,17 @@ else endif +" this function is only for test +function! s:self.luaeval(expr) abort + let rst = luaeval(a:expr) + if type(rst) ==# 5 + return float2nr(rst) + else + return rst + endif +endfunction + + function! SpaceVim#api#vim#compatible#get() abort return deepcopy(s:self) diff --git a/autoload/SpaceVim/default.vim b/autoload/SpaceVim/default.vim index e8084fa8f..c831a2c5d 100644 --- a/autoload/SpaceVim/default.vim +++ b/autoload/SpaceVim/default.vim @@ -76,7 +76,12 @@ function! SpaceVim#default#options() abort set backup set undofile set undolevels=1000 - let g:data_dir = g:spacevim_data_dir.'/SpaceVim/' + " Neovim 0.5 changed the undofile format + if has('nvim-0.5.0') + let g:data_dir = g:spacevim_data_dir.'/SpaceVim/' + else + let g:data_dir = g:spacevim_data_dir.'/SpaceVim/old/' + endif let g:backup_dir = g:data_dir . 'backup//' let g:swap_dir = g:data_dir . 'swap//' let g:undo_dir = g:data_dir . 'undofile//' diff --git a/docs/api/system.md b/docs/api/system.md index efcf01397..ba28344f9 100644 --- a/docs/api/system.md +++ b/docs/api/system.md @@ -5,17 +5,55 @@ description: "system API provides some basic functions and values for current os # [Available APIs](../) >> system -## values + + +- [Intro](#intro) +- [Valuables](#valuables) +- [Functions](#functions) +- [Usage](#usage) + + + +## Intro + +The `system` provides basic functions for os detection. + +## Valuables | name | values | description | | --------- | :----: | -------------------------- | | isWindows | 0 or 1 | check if the os is windows | | isLinux | 0 or 1 | check if the os is linux | | isOSX | 0 or 1 | check if the os is OSX | -| isDarwin | 0 or 1 | check if the os is Darwin | -## functions +## Functions -| name | description | -| ---------- | -------------------------------------- | -| fileformat | return the icon of current file format | +| name | description | +| ------------ | ---------------------------------------- | +| fileformat() | return the icon of current file format | +| isDarwin() | return 0 or 1, check if the os is Darwin | + +## Usage + +This api can be used in both vim script and lua script. + +**vim script:** + +```vim +let s:system = SpaceVim#api#import('system') + +" check the if current os is Windows. +if s:system.isWindows + echom "OS is Windows" +endif +``` + +**lua script:** + +```lua +local sys = require('spacevim.api').import('system') + +if sys.isWindows == 1 then + print('this is windows os!') +end +``` diff --git a/docs/cn/api/system.md b/docs/cn/api/system.md index d08669309..4af51dfbe 100644 --- a/docs/cn/api/system.md +++ b/docs/cn/api/system.md @@ -9,8 +9,9 @@ lang: zh - [简介](#简介) -- [values](#values) -- [functions](#functions) +- [变量](#变量) +- [函数](#函数) +- [基本使用](#基本使用) @@ -18,7 +19,7 @@ lang: zh system 函数提供了系统相关函数,包括判断当前系统平台,文件格式等函数。 -## values +## 变量 | names | values | descriptions | | --------- | ------ | -------------------------- | @@ -27,8 +28,34 @@ system 函数提供了系统相关函数,包括判断当前系统平台,文 | isOSX | 0 or 1 | check if the os is OSX | | isDarwin | 0 or 1 | check if the os is Darwin | -## functions +## 函数 -| names | descriptions | -| ---------- | -------------------------------------- | -| fileformat | return the icon of current file format | +| name | description | +| ------------ | ---------------------------------------- | +| fileformat() | return the icon of current file format | +| isDarwin() | return 0 or 1, check if the os is Darwin | + +## 基本使用 + +这一个函数接口提供了两种版本可供使用,Vim 脚本 和 Lua 脚本: + +**vim script:** + +```vim +let s:system = SpaceVim#api#import('system') + +" check the if current os is Windows. +if s:system.isWindows + echom "OS is Windows" +endif +``` + +**lua script:** + +```lua +local sys = require('spacevim.api').import('system') + +if sys.isWindows == 1 then + print('this is windows os!') +end +``` diff --git a/lua/spacevim.lua b/lua/spacevim.lua index c9813dd9d..a1e74575e 100644 --- a/lua/spacevim.lua +++ b/lua/spacevim.lua @@ -10,15 +10,67 @@ function M.bootstrap() options.init() layers.init() - + end function M.eval(l) - if vim['api'] ~= nil then - return vim.eval(l) - else + if vim.api ~= nil then return vim.api.nvim_eval(l) + else + return vim.eval(l) end end +-- there is no want to call viml function in old vim and neovim + +function M.call(funcname, ...) + if vim.call ~= nil then + return vim.call(funcname, ...) + else + if vim.api ~= nil then + return vim.api.nvim_call_function(funcname, {...}) + else + end + end +end + +-- this is for Vim and old neovim +M.fn = setmetatable({}, { + __index = function(t, key) + local _fn + if vim.api ~= nil and vim.api[key] ~= nil then + _fn = function() + error(string.format("Tried to call API function with vim.fn: use vim.api.%s instead", key)) + end + else + _fn = function(...) + return M.call(key, ...) + end + end + t[key] = _fn + return _fn + end + }) + +-- This is for vim and old neovim to use vim.o +M.vim_options = setmetatable({}, { + __index = function(t, key) + local _fn + if vim.api ~= nil then + -- for neovim + return vim.api.nvim_get_option(key) + else + -- for vim + _fn = M.eval('&' .. key) + end + t[key] = _fn + return _fn + end + }) + +-- this function is only for vim +function M.has(feature) + return M.eval('float2nr(has("' .. feature .. '"))') +end + return M diff --git a/lua/spacevim/api/system.lua b/lua/spacevim/api/system.lua new file mode 100644 index 000000000..a94fe5126 --- /dev/null +++ b/lua/spacevim/api/system.lua @@ -0,0 +1,94 @@ +local has = nil +local fn = nil +local vim_options = nil + +if vim.o ~= nil then + vim_options = vim.o +else + vim_options = require('spacevim').vim_options +end + + +if vim.api == nil then + has = require('spacevim').has +else + if vim.fn ~= nil then + has = vim.fn.has + else + has = require('spacevim').has + end +end + +if vim.fn == nil then + fn = require('spacevim').fn +else + fn = vim.fn +end + +local M = {} + +if has('win16') ==1 or has('win32') == 1 or has('win64') == 1 then + M.isWindows = 1 +else + M.isWindows = 0 +end +if has('unix') == 1 and has('macunix') == 0 and has('win32unix') == 0 then + M.isLinux = 1 +else + M.isLinux = 0 +end +M.isOSX = has('macunix') + +function M.name() + if M.isLinux == 1 then + return 'linux' + elseif M.isWindows == 1 then + if has('win32unix') == 1 then + return 'cygwin' + else + return 'windows' + end + else + return 'mac' + end +end + +local is_darwin = nil +function M.isDarwin() + if is_darwin ~= nil then + return is_darwin + end + if has('macunix') == 1 then + is_darwin = 1 + return is_darwin + end + if has('unix') ~= 1 then + is_darwin = 0 + return is_darwin + end + if fn.system('uname -s') == "Darwin\n" then + is_darwin = 1 + else + is_darwin = 0 + end + return is_darwin +end + +function M.fileformat() + local fileformat = '' + if vim_options.fileformat == 'dos' then + fileformat = '' + elseif vim_options.fileformat == 'unix' then + if M.isDarwin() == 1 then + fileformat = '' + else + fileformat = '' + end + elseif vim_options.fileformat == 'mac' then + fileformat = '' + end + return fileformat +end + +return M + diff --git a/test/lua/api/system.vader b/test/lua/api/system.vader new file mode 100644 index 000000000..528d23982 --- /dev/null +++ b/test/lua/api/system.vader @@ -0,0 +1,16 @@ +Execute ( lua api system ): + let api_system = SpaceVim#api#import('system') + let cmp = SpaceVim#api#import('vim#compatible') + Log 'test system.isWindows' + AssertEqual cmp.luaeval('require("spacevim.api").import("system").isWindows'), api_system.isWindows + Log 'test system.isLinux' + AssertEqual cmp.luaeval('require("spacevim.api").import("system").isLinux'), api_system.isLinux + Log 'test system.isOSX' + AssertEqual cmp.luaeval('require("spacevim.api").import("system").isOSX'), api_system.isOSX + Log 'test system.name()' + AssertEqual cmp.luaeval('require("spacevim.api").import("system").name()'), api_system.name() + Log 'test system.isDarwin()' + AssertEqual cmp.luaeval('require("spacevim.api").import("system").isDarwin()'), api_system.isDarwin() + Log 'test system.fileformat()' + AssertEqual cmp.luaeval('require("spacevim.api").import("system").fileformat()'), api_system.fileformat() + unlet api_system