1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 07:00:04 +08:00

Add vim#window api (#3133)

This commit is contained in:
Wang Shidong 2019-10-08 22:24:03 +08:00 committed by GitHub
parent 9b354e05b4
commit 8a6baac866
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 187 additions and 6 deletions

View File

@ -9,6 +9,11 @@
""
" @section vim#buffer, api-vim-buffer
" @parentsection api
" @subsection Intro
"
" vim#buffer API provides some basic functions for setting and getting config
" of vim buffer.
"
" @subsection Functions
"
" is_cmdwin()

View File

@ -0,0 +1,54 @@
"=============================================================================
" window.vim --- window api for vim and neovim
" Copyright (c) 2016-2019 Wang Shidong & Contributors
" Author: Wang Shidong < wsdjeg@outlook.com >
" URL: https://spacevim.org
" License: GPLv3
"=============================================================================
""
" @section vim#buffer, api-vim-window
" @parentsection api
" @subsection Intro
"
" vim#window API provides some basic functions for setting and getting config
" of vim window.
"
" @subsection Functions
"
" get_cursor({winid})
"
" Gets the cursor position in the window {winid}, to get the ID of a window,
" checkout |window-ID|.
let s:self = {}
if exists('*nvim_win_get_cursor')
function! s:self.get_cursor(winid) abort
return nvim_win_get_cursor(a:winid)
endfunction
elseif g:_spacevim_if_lua
function! s:self.get_cursor(winid) abort
lua require("spacevim.api.vim.window").get_cursor(vim.eval("a:winid"))
endfunction
else
function! s:self.get_cursor(winid) abort
endfunction
endif
if exists('*nvim_win_set_cursor')
function! s:self.set_cursor(winid, pos) abort
return nvim_win_set_cursor(a:winid, a:pos)
endfunction
elseif g:_spacevim_if_lua
function! s:self.set_cursor(winid, pos) abort
lua require("spacevim.api.vim.window").set_cursor(vim.eval("a:winid"), vim.eval("a:pos"))
endfunction
else
endif
function! SpaceVim#api#vim#window#get() abort
return deepcopy(s:self)
endfunction

View File

@ -73,7 +73,7 @@ function! SpaceVim#layers#lang#lua#config() abort
elseif executable('luap')
let lua_repl = 'luap'
elseif !empty(luaexe)
let lua_repl = luaexe[0] + ['-i']
let lua_repl = luaexe + ['-i']
else
let lua_repl = ['lua', '-i']
endif

View File

@ -133,9 +133,10 @@ CONTENTS *SpaceVim-contents*
9. sid............................................|SpaceVim-api-vim-sid|
10. system.........................................|SpaceVim-api-system|
11. vim#buffer.................................|SpaceVim-api-vim-buffer|
12. vim#command...............................|SpaceVim-api-vim-command|
13. vim#compatible.........................|SpaceVim-api-vim-compatible|
14. vim#message...............................|SpaceVim-api-vim-message|
12. vim#buffer.................................|SpaceVim-api-vim-window|
13. vim#command...............................|SpaceVim-api-vim-command|
14. vim#compatible.........................|SpaceVim-api-vim-compatible|
15. vim#message...............................|SpaceVim-api-vim-message|
8. FAQ........................................................|SpaceVim-faq|
9. Changelog............................................|SpaceVim-changelog|
@ -2467,6 +2468,11 @@ mac.
==============================================================================
VIM#BUFFER *SpaceVim-api-vim-buffer*
INTRO
vim#buffer API provides some basic functions for setting and getting config of
vim buffer.
FUNCTIONS
is_cmdwin()
@ -2486,6 +2492,21 @@ is a dict with following keys:
cmd: the ex command which will be run after the new buffer is created
==============================================================================
VIM#BUFFER *SpaceVim-api-vim-window*
INTRO
vim#window API provides some basic functions for setting and getting config of
vim window.
FUNCTIONS
get_cursor({winid})
Gets the cursor position in the window {winid}, to get the ID of a window,
checkout |window-ID|.
==============================================================================
VIM#COMMAND *SpaceVim-api-vim-command*

30
docs/api/vim/buffer.md Normal file
View File

@ -0,0 +1,30 @@
---
title: "vim#buffer api"
description: "vim#buffer API provides some basic functions for setting and getting config of vim buffer."
---
# [Available APIs](../../) >> vim#buffer
<!-- vim-markdown-toc GFM -->
- [Intro](#intro)
- [Functions](#functions)
<!-- vim-markdown-toc -->
## Intro
vim#buffer API provides some basic functions for setting and getting config of vim buffer.
```vim
let s:BUFFER = SpaceVim#api#import('vim#buffer')
```
## Functions
here is a list of functions implement in this api. When Vim has python or lua support,
some of these functions are better experienced
| function name | description |
| ---------------- | -------------------------- |
| `filter_do(cmd)` | filter buffers and run cmd |

35
docs/api/vim/window.md Normal file
View File

@ -0,0 +1,35 @@
---
title: "vim#window api"
description: "vim#window API provides some basic functions for setting and getting config of vim window."
---
# [Available APIs](../../) >> vim#window
<!-- vim-markdown-toc GFM -->
- [Intro](#intro)
- [Functions](#functions)
<!-- vim-markdown-toc -->
## Intro
vim#window API provides some basic functions for setting and getting config of vim window.
```vim
let s:WINDOW = SpaceVim#api#import('vim#window')
" if you want to change the cursor to [10, 3] in window winid
call s:WINDOW.set_cursor(s:winid, [10, 3])
" of cause, you can get the cursor for a window
let cursorpos = s:WINDOW.get_cursor(s:winid)
```
## Functions
here is a list of functions implement in this api. When Vim has python or lua support,
some of these functions are better experienced
| function name | description |
| ------------------------ | --------------------------------------- |
| `get_cursor(winid)` | Gets the cursor position in the window. |
| `set_cursor(winid, pos)` | Sets the cursor position in the window. |

View File

@ -7,5 +7,3 @@
"=============================================================================
execute 'source' fnamemodify(expand('<sfile>'), ':h').'/config/main.vim'
" lua require('spacevim').bootstrap()

View File

View File

@ -0,0 +1,27 @@
local window = {}
function window.get_cursor(window_id)
local winindex = vim.eval("win_id2win(" .. window_id .. ")")
local w = vim.window(winindex)
if w == nil then
vim.command("return [" .. table.concat({0, 0}, ", ") .. "]")
else
vim.command("return [" .. table.concat({w.line, w.col}, ", ") .. "]")
end
end
function window.set_cursor(window_id, pos)
local winindex = vim.eval("win_id2win(" .. window_id .. ")")
local w = vim.window(winindex)
w.line = pos[1]
w.col = pos[2]
end
function window.close(window_id)
end
return window

11
vimrc
View File

@ -8,6 +8,17 @@
" Note: Skip initialization for vim-tiny or vim-small.
if 1
let g:_spacevim_if_lua = 0
if has('lua')
try
let s:plugin_dir = fnamemodify(expand('<sfile>'), ':h').'\lua'
let s:str = s:plugin_dir . '\?.lua;' . s:plugin_dir . '\?\init.lua;'
lua package.path=vim.eval("s:str") .. package.path
let g:_spacevim_if_lua = 1
catch
let g:_spacevim_if_lua = 0
endtry
endif
execute 'source' fnamemodify(expand('<sfile>'), ':h').'/config/main.vim'
endif
" vim:set et sw=2