spacevim/bundle/vim-cheat/plugin/cheat.vim
JIe 2bb7059579
Some checks failed
Detach Plugins / check (FlyGrep.vim) (push) Has been cancelled
Detach Plugins / check (GitHub.vim) (push) Has been cancelled
Detach Plugins / check (JavaUnit.vim) (push) Has been cancelled
Detach Plugins / check (SourceCounter.vim) (push) Has been cancelled
Detach Plugins / check (cpicker.nvim) (push) Has been cancelled
Detach Plugins / check (dein-ui.vim) (push) Has been cancelled
Detach Plugins / check (git.vim) (push) Has been cancelled
Detach Plugins / check (iedit.vim) (push) Has been cancelled
Detach Plugins / check (scrollbar.vim) (push) Has been cancelled
Detach Plugins / check (vim-chat) (push) Has been cancelled
Detach Plugins / check (vim-cheat) (push) Has been cancelled
Detach Plugins / check (vim-todo) (push) Has been cancelled
Detach Plugins / check (xmake.vim) (push) Has been cancelled
test / Linux (nvim, nightly) (push) Has been cancelled
test / Linux (nvim, v0.3.8) (push) Has been cancelled
test / Linux (nvim, v0.4.0) (push) Has been cancelled
test / Linux (nvim, v0.4.2) (push) Has been cancelled
test / Linux (nvim, v0.4.3) (push) Has been cancelled
test / Linux (nvim, v0.4.4) (push) Has been cancelled
test / Linux (nvim, v0.5.0) (push) Has been cancelled
test / Linux (nvim, v0.5.1) (push) Has been cancelled
test / Linux (nvim, v0.6.0) (push) Has been cancelled
test / Linux (nvim, v0.6.1) (push) Has been cancelled
test / Linux (nvim, v0.7.0) (push) Has been cancelled
test / Linux (nvim, v0.7.2) (push) Has been cancelled
test / Linux (nvim, v0.8.0) (push) Has been cancelled
test / Linux (nvim, v0.8.1) (push) Has been cancelled
test / Linux (nvim, v0.8.2) (push) Has been cancelled
test / Linux (nvim, v0.8.3) (push) Has been cancelled
test / Linux (nvim, v0.9.0) (push) Has been cancelled
test / Linux (nvim, v0.9.1) (push) Has been cancelled
test / Linux (true, vim, v7.4.052) (push) Has been cancelled
test / Linux (true, vim, v7.4.1689) (push) Has been cancelled
test / Linux (true, vim, v7.4.629) (push) Has been cancelled
test / Linux (true, vim, v8.0.0027) (push) Has been cancelled
test / Linux (true, vim, v8.0.0183) (push) Has been cancelled
test / Linux (vim, nightly) (push) Has been cancelled
test / Linux (vim, v8.0.0184) (push) Has been cancelled
test / Linux (vim, v8.0.1453) (push) Has been cancelled
test / Linux (vim, v8.1.2269) (push) Has been cancelled
test / Linux (vim, v8.2.2434) (push) Has been cancelled
test / Linux (vim, v8.2.3995) (push) Has been cancelled
test / Windows (nvim, nightly) (push) Has been cancelled
test / Windows (nvim, v0.3.8) (push) Has been cancelled
test / Windows (nvim, v0.4.2) (push) Has been cancelled
test / Windows (nvim, v0.4.3) (push) Has been cancelled
test / Windows (nvim, v0.4.4) (push) Has been cancelled
test / Windows (nvim, v0.5.0) (push) Has been cancelled
test / Windows (nvim, v0.5.1) (push) Has been cancelled
test / Windows (nvim, v0.6.0) (push) Has been cancelled
test / Windows (nvim, v0.6.1) (push) Has been cancelled
test / Windows (nvim, v0.7.0) (push) Has been cancelled
test / Windows (nvim, v0.7.2) (push) Has been cancelled
test / Windows (nvim, v0.8.0) (push) Has been cancelled
test / Windows (nvim, v0.8.1) (push) Has been cancelled
test / Windows (nvim, v0.8.2) (push) Has been cancelled
test / Windows (nvim, v0.8.3) (push) Has been cancelled
test / Windows (nvim, v0.9.0) (push) Has been cancelled
test / Windows (nvim, v0.9.1) (push) Has been cancelled
test / Windows (vim, nightly) (push) Has been cancelled
test / Windows (vim, v7.4.1185) (push) Has been cancelled
test / Windows (vim, v7.4.1689) (push) Has been cancelled
test / Windows (vim, v8.0.0027) (push) Has been cancelled
test / Windows (vim, v8.0.1453) (push) Has been cancelled
test / Windows (vim, v8.1.2269) (push) Has been cancelled
test / Windows (vim, v8.2.2434) (push) Has been cancelled
test / Windows (vim, v8.2.3995) (push) Has been cancelled
docker / docker (push) Has been cancelled
mirror / check (coding) (push) Has been cancelled
mirror / check (gitee) (push) Has been cancelled
mirror / check (gitlab) (push) Has been cancelled
init
2024-08-21 14:17:26 +08:00

122 lines
3.7 KiB
VimL

"=============================================================================
" cheat.vim --- cheat plugin
" Copyright (c) 2016-2023 Wang Shidong & Contributors
" Author: Wang Shidong < wsdjeg@outlook.com >
" URL: https://spacevim.org
" License: GPLv3
"=============================================================================
if exists('g:loaded_cheat')
finish
endif
""
" Set the path to the cheat sheets cache file, can be overriden from
" .vimrc with:
" >
" let g:cheats_dir = '/path/to/your/cache/file'
" <
let g:cheats_dir = get(g:, 'cheats_dir', $HOME . '/.cheat/')
" Set the split direction for the output buffer.
" It can be overriden from .vimrc as well, by setting g:cheats_split to hor | ver
let s:cheats_split = get(g:, 'cheats_split', 'hor')
" Constants
let s:splitCmdMap = { 'ver' : 'vsp' , 'hor' : 'sp' }
let s:cheat_options = {"-add" : "add new cheatsheet" , "-update" : "update current cheatsheet"}
" Func Defs
func! FindOrCreateOutWin(bufName)
let outWinNr = bufwinnr(a:bufName)
let outBufNr = bufnr(a:bufName)
" Find or create a window for the bufName
if outWinNr == -1
" Create a new window
exec s:splitCmdMap[s:cheats_split]
let outWinNr = bufwinnr('%')
if outBufNr != -1
" The buffer already exists. Open it here.
exec 'b'.outBufNr
endif
" Jump back to the previous window the user was editing in.
exec 'wincmd p'
endif
" Find the buffer number or create one for bufName
if outBufNr == -1
" Jump to the output window
exec outWinNr.' wincmd w'
" Open a new output buffer
exec 'e '.a:bufName
setlocal noswapfile
setlocal buftype=nofile
setlocal wrap
setlocal nobuflisted
let outBufNr = bufnr('%')
" Jump back to the previous window the user was editing in.
exec 'wincmd p'
endif
return outBufNr
endf
func! s:RunAndRedirectOut(cheatName, bufName)
" Change to the output buffer window
let outWinNr = bufwinnr(a:bufName)
exec outWinNr.' wincmd w'
let f = fnameescape(g:cheats_dir . a:cheatName)
if filereadable(f)
call append(0, readfile(f))
end
normal! gg
endf
func! CheatCompletion(ArgLead, CmdLine, CursorPos)
echom "arglead:[".a:ArgLead ."] cmdline:[" .a:CmdLine ."] cursorpos:[" .a:CursorPos ."]"
if a:ArgLead =~ '^-\w*'
return join(keys(s:cheat_options),"\n")
endif
return join(cheat#List_sheets(),"\n")
endf
func! Cheat(...)
let c = a:0 != 0 ? a:000 : split(input('Cheat Sheet: ', '', 'custom,CheatCompletion'),' ')
if len(c) == 1 && c[0] !~ '^-\w*'
let outBuf = FindOrCreateOutWin('__cheat_output__')
call s:RunAndRedirectOut(c[0], outBuf)
elseif len(c) == 2 && c[0] ==# '-add' && c[1] !~ '^-\w*'
if index(cheat#List_sheets(), c[1]) == -1
exe "split ". g:cheats_dir . fnameescape(c[1])
else
echohl WarningMsg | echom "cheets " . c[1] . " already exists" | echohl None
endif
elseif len(c) == 2 && c[0] ==# '-update' && c[1] !~ '^-\w*'
call cheat#Update(c[1])
endif
endf
func! CheatCurrent()
call Cheat(expand('<cword>'))
endf
" Commands Mappings
comm! -nargs=* -complete=custom,CheatCompletion Cheat :call Cheat(<f-args>)
comm! CheatCurrent :call CheatCurrent()
" Disable default mappings
" let g:Cheat_EnableDefaultMappings = 0
if get(g:, 'Cheat_EnableDefaultMappings', 1)
if empty(maparg('<Leader>C', 'n'))
nnoremap <Leader>C :call Cheat()<CR>
endif
" Ask for cheatsheet for the word under cursor
if empty(maparg('<Leader>ch', 'n'))
nmap <leader>ch :call CheatCurrent()<CR>
endif
endif
let g:loaded_cheat = 1