init
This commit is contained in:
27
autoload/buf_utils.vim
Normal file
27
autoload/buf_utils.vim
Normal file
@@ -0,0 +1,27 @@
|
||||
function! buf_utils#GoToBuffer(count, direction) abort
|
||||
if a:count == 0
|
||||
if a:direction ==# 'forward'
|
||||
bnext
|
||||
elseif a:direction ==# 'backward'
|
||||
bprevious
|
||||
else
|
||||
echoerr 'Bad argument ' a:direction
|
||||
endif
|
||||
return
|
||||
endif
|
||||
|
||||
"Check validity of buffer number
|
||||
if index(s:GetBufNums(), a:count) == -1
|
||||
call v:lua.vim.notify('Invalid bufnr: ' . a:count, 4, {'tile': 'nvim-config'})
|
||||
return
|
||||
endif
|
||||
|
||||
" Do not use {count} for gB (it is less useful)
|
||||
if a:direction ==# 'forward'
|
||||
silent execute('buffer', a:count)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:GetBufNums() abort
|
||||
return map(copy(getbufinfo({'buflisted':1})), 'v:val.bufnr')
|
||||
endfunction
|
||||
67
autoload/text_obj.vim
Normal file
67
autoload/text_obj.vim
Normal file
@@ -0,0 +1,67 @@
|
||||
function! text_obj#URL() abort
|
||||
if match(&runtimepath, 'vim-highlighturl') != -1
|
||||
let url_pattern = highlighturl#default_pattern()
|
||||
else
|
||||
let url_pattern = expand('<cfile>')
|
||||
if len(url_pattern) <= 10
|
||||
return
|
||||
endif
|
||||
endif
|
||||
|
||||
" Find all URLs on the line and determine if the cursor is on one of them
|
||||
let line_text = getline('.')
|
||||
let url_infos = []
|
||||
|
||||
let [_url, _idx_start, _idx_end] = matchstrpos(line_text, url_pattern)
|
||||
while _url !=# ''
|
||||
let url_infos += [[_url, _idx_start+1, _idx_end]]
|
||||
let [_url, _idx_start, _idx_end] = matchstrpos(line_text, url_pattern, _idx_end)
|
||||
endwhile
|
||||
|
||||
if len(url_infos) == 0
|
||||
return
|
||||
endif
|
||||
|
||||
let [start_col, end_col] = [-1, -1]
|
||||
let [buf_num, cur_row, cur_col] = getcurpos()[0:2]
|
||||
for url_info in url_infos
|
||||
let [_url, _idx_start, _idx_end] = url_info
|
||||
if cur_col >= _idx_start && cur_col <= _idx_end
|
||||
let start_col = _idx_start
|
||||
let end_col = _idx_end
|
||||
break
|
||||
endif
|
||||
endfor
|
||||
|
||||
if start_col == -1
|
||||
return
|
||||
endif
|
||||
|
||||
call setpos("'<", [buf_num, cur_row, start_col, 0])
|
||||
call setpos("'>", [buf_num, cur_row, end_col, 0])
|
||||
normal! gv
|
||||
endfunction
|
||||
|
||||
function! text_obj#MdCodeBlock(type) abort
|
||||
normal! $
|
||||
let start_row = searchpos('\s*```', 'bnW')[0]
|
||||
let end_row = searchpos('\s*```', 'nW')[0]
|
||||
|
||||
let buf_num = bufnr()
|
||||
if a:type ==# 'i'
|
||||
let start_row += 1
|
||||
let end_row -= 1
|
||||
endif
|
||||
|
||||
call setpos("'<", [buf_num, start_row, 1, 0])
|
||||
call setpos("'>", [buf_num, end_row, 1, 0])
|
||||
execute 'normal! `<V`>'
|
||||
endfunction
|
||||
|
||||
function! text_obj#Buffer() abort
|
||||
let buf_num = bufnr()
|
||||
|
||||
call setpos("'<", [buf_num, 1, 1, 0])
|
||||
call setpos("'>", [buf_num, line('$'), 1, 0])
|
||||
execute 'normal! `<V`>'
|
||||
endfunction
|
||||
130
autoload/utils.vim
Normal file
130
autoload/utils.vim
Normal file
@@ -0,0 +1,130 @@
|
||||
function! utils#Cabbrev(key, value) abort
|
||||
execute printf('cabbrev <expr> %s (getcmdtype() == ":" && getcmdpos() <= %d) ? %s : %s',
|
||||
\ a:key, 1 + len(a:key), <SID>Single_quote(a:value), <SID>Single_quote(a:key))
|
||||
endfunction
|
||||
|
||||
function! s:Single_quote(str) abort
|
||||
return "'" . substitute(copy(a:str), "'", "''", 'g') . "'"
|
||||
endfunction
|
||||
|
||||
function! utils#VimFolds(lnum) abort
|
||||
let l:cur_line = getline(a:lnum)
|
||||
let l:next_line = getline(a:lnum+1)
|
||||
|
||||
if l:cur_line =~# '^"{'
|
||||
return '>' . (matchend(l:cur_line, '"{*') - 1)
|
||||
endif
|
||||
|
||||
if l:cur_line ==# '' && (matchend(l:next_line, '"{*') - 1) == 1
|
||||
return 0
|
||||
endif
|
||||
|
||||
return '='
|
||||
endfunction
|
||||
|
||||
function! utils#MyFoldText() abort
|
||||
let l:line = getline(v:foldstart)
|
||||
let l:fold_line_num = v:foldend - v:foldstart
|
||||
let l:fold_text = substitute(l:line, '^"{\+', '', 'g')
|
||||
let l:fill_char_num = &textwidth - len(l:fold_text) - len(l:fold_line_num) - 10
|
||||
return printf('+%s%s %s (%s L)', repeat('-', 4), l:fold_text, repeat('-', l:fill_char_num), l:fold_line_num)
|
||||
endfunction
|
||||
|
||||
function! utils#ToggleCursorCol() abort
|
||||
if &cursorcolumn
|
||||
set nocursorcolumn
|
||||
echo 'cursorcolumn: OFF'
|
||||
else
|
||||
set cursorcolumn
|
||||
echo 'cursorcolumn: ON'
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! utils#SwitchLine(src_line_idx, direction) abort
|
||||
if a:direction ==# 'up'
|
||||
if a:src_line_idx == 1
|
||||
return
|
||||
endif
|
||||
move-2
|
||||
elseif a:direction ==# 'down'
|
||||
if a:src_line_idx == line('$')
|
||||
return
|
||||
endif
|
||||
move+1
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! utils#MoveSelection(direction) abort
|
||||
if visualmode() !=# 'V'
|
||||
return
|
||||
endif
|
||||
|
||||
let l:start_line = line("'<")
|
||||
let l:end_line = line("'>")
|
||||
let l:num_line = l:end_line - l:start_line + 1
|
||||
|
||||
if a:direction ==# 'up'
|
||||
if l:start_line == 1
|
||||
normal! gv
|
||||
return
|
||||
endif
|
||||
silent execute printf('%s,%smove-2', l:start_line, l:end_line)
|
||||
normal! gv
|
||||
elseif a:direction ==# 'down'
|
||||
if l:end_line == line('$')
|
||||
normal! gv
|
||||
return
|
||||
endif
|
||||
silent execute printf('%s,%smove+%s', l:start_line, l:end_line, l:num_line)
|
||||
normal! gv
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function utils#Get_titlestr() abort
|
||||
let l:title_str = ''
|
||||
if g:is_linux
|
||||
let l:title_str = hostname() . ' '
|
||||
endif
|
||||
|
||||
let l:buf_path = expand('%:p:~')
|
||||
let l:title_str = l:title_str . l:buf_path . ' '
|
||||
|
||||
return l:title_str
|
||||
endfunction
|
||||
|
||||
function utils#iso_time(timestamp) abort
|
||||
if !a:timestamp
|
||||
return strftime('%Y-%m-%d %H:%M:%S%z')
|
||||
endif
|
||||
|
||||
if len(a:timestamp) == 13
|
||||
let l:timestamp = a:timestamp[:-4]
|
||||
elseif len(a:timestamp) == 16
|
||||
let l:timestamp = a:timestamp[:-7]
|
||||
else
|
||||
let l:timestamp = a:timestamp
|
||||
endif
|
||||
return strftime('%Y-%m-%d %H:%M:%S%z', l:timestamp)
|
||||
endfunction
|
||||
|
||||
function! utils#CaptureCommandOutput(command) abort
|
||||
let l:tmp = @m
|
||||
redir @m
|
||||
silent! execute a:command
|
||||
redir END
|
||||
|
||||
tabnew | setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile
|
||||
|
||||
let l:lines = split(@m, '\n')
|
||||
call nvim_buf_set_lines(0, 0, 0, 0, l:lines)
|
||||
|
||||
let @m = l:tmp
|
||||
endfunction
|
||||
|
||||
function! utils#MultiEdit(patterns) abort
|
||||
for p in a:patterns
|
||||
for f in glob(p, 0, 1)
|
||||
execute 'edit ' . f
|
||||
endfor
|
||||
endfor
|
||||
endfunction
|
||||
Reference in New Issue
Block a user