Skip to content

Commit

Permalink
refactor: move some functions from vimrc to noplug.vim
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangyinzuo committed Jul 23, 2024
1 parent cbb3b38 commit 4cc8fe6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 57 deletions.
11 changes: 4 additions & 7 deletions .vimrc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ if has('autocmd') " vim-tiny does not have autocmd
call mkdir(b:backup_dir, "p", 0700)
endif
endif
call system('cp ' . expand('%:p') . ' ' . b:backup_dir . '/' . expand('%:t') . strftime("~~%Y-%m%d-%X") .. '.bak')
call system('cp ' . expand('%:p') . ' ' . b:backup_dir . '/' . expand('%:t') . strftime("~~%Y-%m%d-%X") . '.bak')
endfunction
autocmd BufWritePost * call BackupFile()
set swapfile
Expand Down Expand Up @@ -368,8 +368,8 @@ if has('autocmd') " vim-tiny does not have autocmd

" neovim 0.10.0 uses ripgrep as greppg by default
command! -nargs=1 RgToQf call SystemToQf('rg --vimgrep ' . <q-args>)
noremap ]q :call ToggleQuickfix('c')<CR>
noremap ]l :call ToggleQuickfix('l')<CR>
nnoremap ]q :call ToggleQuickfix('c')<CR>
nnoremap ]l :call ToggleQuickfix('l')<CR>
nnoremap <silent> <leader>cn :cn<CR>
nnoremap <silent> <leader>cp :cp<CR>
nnoremap <silent> <leader>ln :ln<CR>
Expand All @@ -386,9 +386,6 @@ if has('autocmd') " vim-tiny does not have autocmd
" 清空高亮
nnoremap <Leader>lc :call clearmatches()<cr>
" Example:
" :e+22 ~/.vimrc
command! -nargs=0 VimExeLine exe getline(".")
if v:version >= 740 || &readonly == 0
set mouse=a
if !has('nvim') && has('unix') && exists('$TMUX')
Expand Down Expand Up @@ -438,7 +435,7 @@ if has('autocmd') " vim-tiny does not have autocmd
" [[palette]]git-blame :GitBlame
command -range -nargs=0 GitBlame :!git blame -n -L <line1>,<line2> -- %

if has("autocmd") && exists("+omnifunc")
if exists("+omnifunc")
autocmd Filetype * if &omnifunc == "" | setlocal omnifunc=syntaxcomplete#Complete | endif
endif
function! MyCppCompleteFunc(findstart, base)
Expand Down
39 changes: 39 additions & 0 deletions root/.vim/autoload/noplug.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
function noplug#GetVisualSelection()
" https://stackoverflow.com/questions/1533565/how-to-get-visually-selected-text-in-vimscript
" Why is this not a built-in Vim script function?!
let [line_start, column_start] = getpos("'<")[1:2]
let [line_end, column_end] = getpos("'>")[1:2]
let lines = getline(line_start, line_end)
if len(lines) == 0
return ''
endif
let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)]
let lines[0] = lines[0][column_start - 1:]
return join(lines, "\n")
endfunction

function! noplug#MyRead(file)
" 获取文件大小
let l:size = getfsize(a:file)
Expand Down Expand Up @@ -88,3 +102,28 @@ function noplug#SystemToQf(args)
call setqflist([], 'r', {'title': a:args})
call s:ShowQuickfixListIfNotEmpty()
endfunction

function noplug#AsyncRunOrSystem(cmd)
if exists("*asyncrun#run")
call asyncrun#run('', {'silent': 1}, a:cmd)
else
call system(a:cmd)
endif
endfunction
" NOTE: 运行 clean-vim-backup.py,仅保留最新的1个备份版本
function noplug#BackupFile()
" 获取当前文件的大小(单位:字节)
let l:filesize = getfsize(expand('%:p'))
" 设置大小阈值为5MB
let l:maxsize = 5 * 1024 * 1024
if l:filesize > l:maxsize
return
endif
if !exists("b:backup_dir")
let b:backup_dir = expand("~/.vim/backup") . expand("%:p:h")
if !isdirectory(b:backup_dir)
call mkdir(b:backup_dir, "p", 0700)
endif
endif
call noplug#AsyncRunOrSystem('cp ' . expand('%:p') . ' ' . b:backup_dir . '/' . expand('%:t') . strftime("~~%Y-%m%d-%X") . '.bak')
endfunction
2 changes: 2 additions & 0 deletions root/.vim/doc/command-line-tool.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ vim:ft=help

-------------------------------------------------------------------------------

*the-art-of-command-line* https://github.com/jlevy/the-art-of-command-line/blob/master/README-zh.md

*write-maintainable-command-line-tools*
https://github.com/jiangyinzuo/write-maintainable-command-line-tools

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ function! s:GtagsAutoUpdate()
if l:file == ''
return
endif
let l:result = AsyncRunOrSystem(s:GlobalCommand() . " -u --single-update=\"" . l:file . "\"")
let l:result = noplug#AsyncRunOrSystem(s:GlobalCommand() . " -u --single-update=\"" . l:file . "\"")
endfunction

"
Expand All @@ -577,7 +577,7 @@ function! GtagsCandidateCore(lead, line, pos)
return glob(l:pattern)
else
let l:cmd = s:GlobalCommand() . ' ' . '-c' . s:option . ' ' . a:lead
call AsyncRunOrSystem(l:cmd)
call noplug#AsyncRunOrSystem(l:cmd)
endif
endfunction

Expand Down
55 changes: 7 additions & 48 deletions root/.vim/vimrc
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,7 @@ if has('autocmd') " vim-tiny does not have autocmd
set nobackup
set nowritebackup

function AsyncRunOrSystem(cmd)
if exists("*asyncrun#run")
call asyncrun#run('', {'silent': 1}, a:cmd)
else
call system(a:cmd)
endif
endfunction
" NOTE: 运行 clean-vim-backup.py,仅保留最新的1个备份版本
function BackupFile()
" 获取当前文件的大小(单位:字节)
let l:filesize = getfsize(expand('%:p'))
" 设置大小阈值为5MB
let l:maxsize = 5 * 1024 * 1024
if l:filesize > l:maxsize
return
endif
if !exists("b:backup_dir")
let b:backup_dir = expand("~/.vim/backup") . expand("%:p:h")
if !isdirectory(b:backup_dir)
call mkdir(b:backup_dir, "p", 0700)
endif
endif
call AsyncRunOrSystem('cp ' . expand('%:p') . ' ' . b:backup_dir . '/' . expand('%:t') . strftime("~~%Y-%m%d-%X") .. '.bak')
endfunction
autocmd BufWritePost * call BackupFile()
autocmd BufWritePost * call noplug#BackupFile()
set swapfile
set updatecount=100
" 会话不保存options, 防止重新set background=dark后,覆盖一些highlight设置
Expand Down Expand Up @@ -243,7 +219,7 @@ if has('autocmd') " vim-tiny does not have autocmd
set undofile
endif
"""""""""""""""""""""""""""""""
if has("patch-8.1.0360") " nvim also has this patch
if has("patch-8.1.0360")
set diffopt=vertical,filler,context:3,indent-heuristic,algorithm:patience,internal
endif
command -nargs=0 DiffthisSetfiletypeText :diffthis | setf text
Expand Down Expand Up @@ -299,30 +275,16 @@ if has('autocmd') " vim-tiny does not have autocmd
augroup END
endif

function GetVisualSelection()
" https://stackoverflow.com/questions/1533565/how-to-get-visually-selected-text-in-vimscript
" Why is this not a built-in Vim script function?!
let [line_start, column_start] = getpos("'<")[1:2]
let [line_end, column_end] = getpos("'>")[1:2]
let lines = getline(line_start, line_end)
if len(lines) == 0
return ''
endif
let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)]
let lines[0] = lines[0][column_start - 1:]
return join(lines, "\n")
endfunction

nnoremap <silent> <leader>vw :call noplug#FindWord(expand("<cword>"))<CR>
vnoremap <silent> <leader>vw :call noplug#FindWord(GetVisualSelection())<CR>
vnoremap <silent> <leader>vw :call noplug#FindWord(noplug#GetVisualSelection())<CR>
command! -nargs=1 VimWord call noplug#FindWord(<q-args>)

nnoremap <silent> <leader>vy :call noplug#FindType(expand("<cword>"))<CR>
vnoremap <silent> <leader>vy :call noplug#FindType(GetVisualSelection())<CR>
vnoremap <silent> <leader>vy :call noplug#FindType(noplug#GetVisualSelection())<CR>
command! -nargs=1 VimType call noplug#FindType(<q-args>)

nnoremap <silent> <leader>vd :call noplug#FindDefinitionFunction(expand("<cword>"))<CR>
vnoremap <silent> <leader>vd :call noplug#FindDefinitionFunction(GetVisualSelection())<CR>
vnoremap <silent> <leader>vd :call noplug#FindDefinitionFunction(noplug#GetVisualSelection())<CR>
command! -nargs=1 VimDef call noplug#FindDefinitionFunction(<q-args>)

" Reference: vim.fandom.com/wiki/Searching_for_files
Expand All @@ -337,8 +299,8 @@ if has('autocmd') " vim-tiny does not have autocmd
" neovim 0.10.0 uses ripgrep as greppg by default
command! -nargs=1 RgToQf call noplug#SystemToQf('rg --vimgrep ' . <q-args>)

noremap ]q :call noplug#ToggleQuickfix('c')<CR>
noremap ]l :call noplug#ToggleQuickfix('l')<CR>
nnoremap ]q :call noplug#ToggleQuickfix('c')<CR>
nnoremap ]l :call noplug#ToggleQuickfix('l')<CR>
nnoremap <silent> <leader>cn :cn<CR>
nnoremap <silent> <leader>cp :cp<CR>
nnoremap <silent> <leader>ln :ln<CR>
Expand All @@ -352,9 +314,6 @@ if has('autocmd') " vim-tiny does not have autocmd
" 清空高亮
nnoremap <Leader>lc :call clearmatches()<cr>
" Example:
" :e+22 ~/.vimrc
command! -nargs=0 VimExeLine exe getline(".")
if v:version >= 740 || &readonly == 0
set mouse=a
if !has('nvim') && has('unix') && exists('$TMUX')
Expand Down

0 comments on commit 4cc8fe6

Please sign in to comment.