From a619fd75b512c6b5318b7222182249f72d51ffef Mon Sep 17 00:00:00 2001 From: jiangyinzuo Date: Wed, 7 Feb 2024 13:45:56 +0800 Subject: [PATCH] feat!: remove .vimrc vimdf, add fzf-gitdiff.vim --- .vimrc | 533 ----------------------------- root/.vim/autoload/cppfoldexpr.vim | 31 -- root/.vim/autoload/mycpp.vim | 62 ++++ root/.vim/vimrc | 7 +- root/.vim/vimrc.d/fzf.vim | 1 + root/.vim/vimrc.d/plugin.vim | 5 + root/scripts/vimdf | 108 ------ 7 files changed, 71 insertions(+), 676 deletions(-) delete mode 100644 .vimrc delete mode 100644 root/.vim/autoload/cppfoldexpr.vim create mode 100644 root/.vim/autoload/mycpp.vim delete mode 100755 root/scripts/vimdf diff --git a/.vimrc b/.vimrc deleted file mode 100644 index 31fce09..0000000 --- a/.vimrc +++ /dev/null @@ -1,533 +0,0 @@ -" Single .vimrc file. The complete vimrc is `root/.vim/vimrc`. -" Custom vim configuration file `~/config_single_vimrc.vim` -set number "显示行号 -if has('autocmd') " vim-tiny does not have autocmd - set nocp "no Vi-compatible - let g:mapleader = ' ' - set modeline " 开启模式行, 读取文件开头结尾类似于 /* vim:set ts=2 sw=2 noexpandtab: */ 的配置 - set exrc " 读取当前文件夹的.vimrc - if has("patch-7.4.1649") - packadd! matchit - endif - if exists('&viminfofile') && !has('nvim') - set viminfofile=$HOME/.vim/.viminfo - " 保留50个marks - " 每个寄存器保存50行内容 - " 保存50行搜索历史 - " 保存50行命令历史 - " 禁止hlsearch - " 禁止保存超过10kb的寄存器内容 - " 保存0-9,A-Zmark - set viminfo='50,<50,/50,:50,h,s10,f100 - endif - set history=500 " Sets how many lines of history VIM has to remember - filetype indent on - filetype plugin on - syntax on "语法高亮显示 - set encoding=utf-8 - set fileencoding=utf-8 - set fileencodings=utf-8,ucs-bom,gbk,cp936,gb2312,gb18030 " vim自动探测fileencodeing的顺序列表 - set termencoding=utf-8 - set fileformats=unix,dos,mac " 文件格式 - set showmatch " 输入),}时,光标会暂时的回到相匹配的(,{。如果没有相匹配的就发出错误信息的铃声 - set matchtime=1 " showmatch的时间0.1s - set backspace=indent,eol,start "indent: BS可以删除缩进; eol: BS可以删除行末回车; start: BS可以删除原先存在的字符 - set hidden " 未保存文本就可以隐藏buffer - set cmdheight=2 " cmd行高2, 减少hit-enter - set wildmenu " command自动补全时显示菜单 - if v:version >= 802 - set wildoptions=pum " 显示popup window - else - set wildmode=list:full - endif - set wildignore=.git/*,build/*,.cache/* - set updatetime=700 " GitGutter更新和自动保存.swp的延迟时间 - - " https://www.skywind.me/blog/archives/2021 - set timeoutlen=3000 " key map 超时时间 - set ttimeout - if $TMUX != '' - set ttimeoutlen=30 - elseif &ttimeoutlen > 80 || &ttimeoutlen <= 0 - set ttimeoutlen=80 - endif - - set autowrite " 自动保存 - set scrolloff=7 " 光标离底部或顶部保持7行 - set hlsearch " Highlight search results - set incsearch " 输入搜索内容时就显示搜索结果 - set magic "模式匹配时 ^ $ . * ~ [] 具有特殊含义 - set lazyredraw " 不要在宏的中间重绘屏幕。使它们更快完成。 - set dictionary+=/usr/share/dict/american-english - set nobackup "no backup files - set nowritebackup "only in case you don't want a backup file while editing - set noswapfile "no swap files - " 会话不保存options, 防止重新set background=dark后,覆盖一些highlight设置 - if v:version >= 802 - set sessionoptions=curdir,globals,localoptions,resize,tabpages,terminal,winpos,winsize - else - set sessionoptions=curdir,globals,localoptions,resize,tabpages,winpos,winsize - endif - " 选中想格式化的段落后,可以用gq格式化 - " 设置textwidth,可以让文本格式化时自动换行 - autocmd Filetype tex setlocal textwidth=80 - """""""""""""""""""""""""""""" - "indent and tab - """""""""""""""""""""""""""""" - set smarttab - set autoindent " 跟随上一行的缩进方式 - " 不开启smartindent, 否则visual模式shift缩进多行时,行首非空白字符开头的注释不会跟着shift - " set smartindent " 以 { 或cinword变量开始的行(if、while...),换行后自动缩进 - - """ default indent - " markdown在被默认vim runtime file设置为tabstop=4 - " :verbose set tabstop? shiftwidth? softtabstop? - set tabstop=4 shiftwidth=4 softtabstop=4 - - augroup equalprg_filetype - autocmd! - " autocmd FileType c,cpp setlocal equalprg=indent - " autocmd FileType c,cpp setlocal equalprg=uncrustify\ -c\ .uncrustify.cfg\ --replace\ --no-backup - autocmd FileType sql setlocal equalprg=sqlformat\ -k\ upper\ -r\ --indent_columns\ - - augroup end - - autocmd FileType c,cpp,cuda,vim,tex,html,sh,zsh,json,lua setlocal tabstop=2 shiftwidth=2 softtabstop=2 - " tex使用空格缩进 - autocmd FileType tex setlocal expandtab - - if v:version >= 901 - packadd! editorconfig - endif - - " 显示空白字符 - " https://codepoints.net/U+23B5 - " set listchars=eol:⏎,tab:¦\ ,trail:␠,nbsp:⎵,extends:»,precedes:« - set listchars=tab:¦\ ,trail:␠,nbsp:⎵,extends:»,precedes:« - autocmd FileType man setlocal nolist - " Enable :Man command - runtime ftplugin/man.vim - - set list - " set nowrap " 不折行 - set guifont=Monospace\ Regular\ 20 - - if has("nvim-0.5.0") || has("patch-8.1.1564") - set signcolumn=number " 合并git状态与行号 - elseif v:version >= 801 - set signcolumn=yes " 同时显示git状态和行号 - endif - - if v:version >= 801 - set conceallevel=2 - - " https://unix.stackexchange.com/questions/149209/refresh-changed-content-of-file-opened-in-vim/383044#383044 - " Set to auto read when a file is changed from the outside - set autoread - " Triger `autoread` when files changes on disk - " https://unix.stackexchange.com/questions/149209/refresh-changed-content-of-file-opened-in-vim/383044#383044 - " https://vi.stackexchange.com/questions/13692/prevent-focusgained-autocmd-running-in-command-line-editing-mode - autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * if mode() !~ '\v(c|r.?|!|t)' && getcmdwintype() == '' | checktime | endif - - " Notification after file change - " https://vi.stackexchange.com/questions/13091/autocmd-event-for-autoread - autocmd FileChangedShellPost * echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl Noneau FocusGained,BufEnter * checktime - endif - - """"""""""""""""""""""""""""""" - - if has("patch-8.1.0360") - set diffopt=vertical,filler,context:3,indent-heuristic,algorithm:patience,internal - endif - - """"""""""""""""""""""""""""""""""""""""""""""""""""" termdebug - set t_Co=256 - set t_ut= - - if has('nvim') || v:version >= 801 - autocmd Filetype c,cpp,rust,cuda packadd termdebug - autocmd Filetype termdebug setlocal termwinsize=0*10000 - let g:termdebug_config = {} - let g:termdebug_config['command'] = "gdb" - " 设置一个较大的宽度,防止termdebug gdb窗口折行 - let g:termdebug_config['wide'] = 1 - endif - - """"""""""""""""""""""""""""""""""""""""""""""""""" Netrw Plugin - " open explorer :Ex :Sex :Vex - " close explorer :Rex - " - " do not load netrw - " let g:loaded_netrw = 1 - " let g:loaded_netrwPlugin = 1 - let g:netrw_browse_split = 0 " open the file using netrw buffer - let g:netrw_liststyle = 3 " tree style listing - let g:netrw_preview = 0 " preview window horizontally - if exists('$WSLENV') - let g:netrw_browsex_viewer="-" - endif - let g:netrw_hide = 1 " show not-hidden files - let g:netrw_keepdir=0 - - " see netrw-v - " 新的窗口出现在当前窗口的右边 - set splitright - let g:netrw_altv = 1 - let g:netrw_winsize = 20 - """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" - - augroup jump_to_symbol - autocmd! - " See: https://stackoverflow.com/questions/12128678/vim-go-to-beginning-end-of-next-method - " #gpt4-answer - " jump to the previous function - " 向后(b表示backward)搜索一个匹配特定模式的地方,这个模式是一个函数的开始位置。 - autocmd FileType c,cpp nnoremap [f :call search('\(\(if\\|for\\|while\\|switch\\|catch\)\_s*\)\@64 - " jump to the next function - " 向前(w表示forward)搜索一个匹配特定模式的地方,这个模式是一个函数的开始位置。 - autocmd FileType c,cpp nnoremap ]f :call search('\(\(if\\|for\\|while\\|switch\\|catch\)\_s*\)\@64 - " 搜索的模式是一个正则表达式,用来匹配不是在if, for, while, switch, 和catch后面的左花括号{,因为在C++中函数的定义是以左花括号开始的。 - " 需要注意的是,这个搜索模式可能并不完全精确,因为函数的定义还可能包含许多其他的复杂性,比如模板函数,函数指针,宏定义等等。对于一些简单的代码文件,这个方法通常可以工作得很好。 - " 这些配置会更好地在代码中快速导航,但如果你需要更精确地识别函数,你可能需要考虑使用一些更高级的插件,比如ctags,cscope等等。 - - " Go 语言的函数定义是以 func 关键字开始的,所以我们可以使用这个关键字来搜索函数的开始位置。 - " 在这里,我们用 \< 和 \> 来指定词的边界,这样我们就可以准确地匹配func,而不是 func 作为其他词的一部分。bW 和 wW是搜索命令的标志,b 表示向后搜索,w 表示向前搜索,W 表示只匹配整个单词。 - autocmd FileType go nnoremap [f :call search('\', "bW") - autocmd FileType go nnoremap ]f :call search('\', "wW") - " 以上代码仅会简单地跳转到包含 func 关键字的行,不过如果你需要更精确或更高级的功能,你可能需要考虑使用一些专门为 - " Go 语言设计的 Vim 插件,例如 vim-go。这个插件为 Go语言提供了许多功能,包括代码导航、自动完成、代码格式化等等。 - " autocmd FileType python nnoremap [f :call search('\', "bW") - " autocmd FileType python nnoremap ]f :call search('\', "wW") - autocmd FileType rust nnoremap [f :call search('\', "bW") - autocmd FileType rust nnoremap ]f :call search('\', "wW") - autocmd FileType lua nnoremap [f :call search('\', "bW") - autocmd FileType lua nnoremap ]f :call search('\', "wW") - - autocmd FileType c,cpp nnoremap [t :call search('\\|\\|\\|\', "bW") - autocmd FileType c,cpp nnoremap ]t :call search('\\|\\|\\|\', "wW") - autocmd FileType go nnoremap [t :call search('\', "bW") - autocmd FileType go nnoremap ]t :call search('\', "wW") - autocmd FileType rust nnoremap [t :call search('\\|\\|\', "bW") - autocmd FileType rust nnoremap ]t :call search('\\|\\|\', "wW") - " autocmd FileType python nnoremap [t :call search('\', "bW") - " autocmd FileType python nnoremap ]t :call search('\', "wW") - augroup END - - 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 - - function ShowQuickfixListIfNotEmpty() - let length = len(getqflist()) - if length > 1 - copen - elseif length == 1 - copen - ccl - elseif length == 0 - echo 'empty quickfix list' - endif - endfunction - - function FindWord(word) - if &filetype == 'c' || &filetype == 'cpp' || &filetype == 'cuda' - let l:extension = '**/*.c* **/*.h*' - else - let l:extension = '**/*.' . expand('%:e') - endif - " : 匹配整个单词 - exe 'vimgrep /\<'.a:word.'\>/ ' . l:extension - call ShowQuickfixListIfNotEmpty() - endfunction - - function FindType(word) - " : 匹配整个单词 - if &filetype == 'cpp' || &filetype == 'c' || &filetype == 'cuda' - exe 'vimgrep' '/\<\(struct\|union\|class\) '.a:word.'\>/' '**/*.c*' '**/*.h*' - elseif &filetype == 'python' - exe 'vimgrep' '/\/' '**/*.py' - elseif &filetype == 'go' - exe 'vimgrep' '/\/' '**/*.go' - else - echo 'unsupport filetype: '.&filetype - return - endif - call ShowQuickfixListIfNotEmpty() - endfunction - - function FindDefinitionFunction(word) - if &filetype == 'cpp' || &filetype == 'c' || &filetype == 'cuda' - exe 'vimgrep' '/\<'.a:word.'\s*(/ **/*.c* **/*.h*' - elseif &filetype == 'python' - exe 'vimgrep' '/\ vw :call FindWord(expand("")) - vnoremap vw :call FindWord(GetVisualSelection()) - command! -nargs=1 VimWord call FindWord() - - nnoremap vy :call FindType(expand("")) - vnoremap vy :call FindType(GetVisualSelection()) - command! -nargs=1 VimType call FindType() - - nnoremap vd :call FindDefinitionFunction(expand("")) - vnoremap vd :call FindDefinitionFunction(GetVisualSelection()) - command! -nargs=1 VimDef call FindDefinitionFunction() - - command! -nargs=1 SystemFindFiles call FindFiles() - command! -nargs=1 SystemRg call Ripgrep() - - noremap ]q :call ToggleQuickfix() - nnoremap cn :cn - nnoremap cp :cp - - " vnoremap t :term ++open ++rows=9 - " nnoremap t :term ++rows=9 - " if exists(':tnoremap') - " tnoremap t :hide - " endif - - " [[palette]]执行系统命令并输出到quickfix list :SystemToQf - command! -nargs=1 SystemToQf call SystemToQf() - """"""""""""""""""""""""""""""""""""""""""""""""""""""" - - " 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') - set ttymouse=xterm2 " Windows termimal 可以用鼠标改变窗口大小 - endif - - set foldmethod=marker - set foldmarker={,} - - " 默认打开所有折叠,将foldlevelstart设置为较大的值 - "" set foldlevel=-1 " 默认关闭所有折叠 - set foldlevelstart=99 - " https://www.zhihu.com/question/30782510/answer/70078216 - nnoremap zpr :setlocal foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)\\|\\|(getline(v:lnum+1)=~@/)?1:2 foldmethod=expr foldlevel=0 foldcolumn=2:set foldmethod=syntax - " 打开所有折叠: zR - - function! CppFoldExpr(lnum) - let line = getline(a:lnum) - if !exists('b:cpp_fold_level') - let b:cpp_fold_level = 0 - let b:last_primitive = '' - endif - - let open_braces = len(split(line, '{', 1)) - 1 - let close_braces = len(split(line, '}', 1)) - 1 - - if b:last_primitive == 'if' - let b:cpp_fold_level += 1 - elseif b:last_primitive == 'end' - let b:cpp_fold_level -= 1 - endif - let b:last_primitive = '' - - if line =~ '^\s*#\s*\(ifdef\|ifndef\|if\)' || open_braces > close_braces - let b:cpp_fold_level += 1 - let b:last_primitive = 'if' - return b:cpp_fold_level - elseif line =~ '^\s*#\s*\(else\|elif\)' || (open_braces == close_braces && open_braces > 0) - return b:cpp_fold_level - 1 - elseif line =~ '^\s*#\s*endif' || close_braces > open_braces - let b:last_primitive = 'end' - let b:cpp_fold_level -= 1 - return b:cpp_fold_level - endif - - return b:cpp_fold_level - endfunction - autocmd FileType c,cpp setlocal foldmethod=expr foldexpr=CppFoldExpr(v:lnum) - autocmd FileType python,vim,lua,go,markdown,sh,tex setlocal foldmethod=indent - endif - - " command -nargs=0 GitBlame !git blame -L line(".") + 1, line(".") + 1 -- % - " [[palette]]git-blame :GitBlame - command -range -nargs=0 GitBlame :!git blame -n -L , -- % - - " save - inoremap :wi - - if has("autocmd") && exists("+omnifunc") - autocmd Filetype * if &omnifunc == "" | setlocal omnifunc=syntaxcomplete#Complete | endif - endif - - function! MyCppCompleteFunc(findstart, base) - if a:findstart - " 确定补全开始的位置 - let line = getline('.') - let start = col('.') - 1 - while start > 0 && line[start - 1] =~ '\S' - let start -= 1 - endwhile - if line[start] != '<' && line[start] != '"' - return -3 - endif - return start + 1 - endif - - if getline('.') =~# &include - let filetype = &filetype - if filetype == 'cpp' || filetype == 'cuda' - " r! ls -1 /usr/include/c++/13 | awk '{print " \x27"$0"\x27"}' | paste -sd, - let completions = ['algorithm', 'any', 'array', 'atomic', 'backward', 'barrier', 'bit', 'bits', 'bitset', 'cassert', 'ccomplex', 'cctype', 'cerrno', 'cfenv', 'cfloat', 'charconv', 'chrono', 'cinttypes', 'ciso646', 'climits', 'clocale', 'cmath', 'codecvt', 'compare', 'complex', 'complex.h', 'concepts', 'condition_variable', 'coroutine', 'csetjmp', 'csignal', 'cstdalign', 'cstdarg', 'cstdbool', 'cstddef', 'cstdint', 'cstdio', 'cstdlib', 'cstring', 'ctgmath', 'ctime', 'cuchar', 'cwchar', 'cwctype', 'cxxabi.h', 'debug', 'decimal', 'deque', 'exception', 'execution', 'expected', 'experimental', 'ext', 'fenv.h', 'filesystem', 'format', 'forward_list', 'fstream', 'functional', 'future', 'initializer_list', 'iomanip', 'ios', 'iosfwd', 'iostream', 'istream', 'iterator', 'latch', 'limits', 'list', 'locale', 'map', 'math.h', 'memory', 'memory_resource', 'mutex', 'new', 'numbers', 'numeric', 'optional', 'ostream', 'parallel', 'pstl', 'queue', 'random', 'ranges', 'ratio', 'regex', 'scoped_allocator', 'semaphore', 'set', 'shared_mutex', 'source_location', 'span', 'spanstream', 'sstream', 'stack', 'stacktrace', 'stdatomic.h', 'stdexcept', 'stdfloat', 'stdlib.h', 'stop_token', 'streambuf', 'string', 'string_view', 'syncstream', 'system_error', 'tgmath.h', 'thread', 'tr1', 'tr2', 'tuple', 'typeindex', 'typeinfo', 'type_traits', 'unordered_map', 'unordered_set', 'utility', 'valarray', 'variant', 'vector', 'version'] - call filter(completions, 'v:val =~ "^" . a:base') - endif - " 使用getcompletion()获取文件类型的补全列表 - echom a:base - let completions += getcompletion(a:base . '*.h*', 'file_in_path', 1) - let completions += getcompletion(a:base . '*/$', 'file_in_path', 1) - return completions - endif - return [] - endfunction - autocmd FileType c,cpp,cuda setlocal completefunc=MyCppCompleteFunc - set laststatus=2 - set statusline=%1*%F%m%r%h%w%=%l/%L,%v\ %Y\ %{\"\".(\"\"?&enc:&fenc).((exists(\"+bomb\")\ &&\ &bomb)?\"+\":\"\").\"\"} - - " file is large from 10mb - let g:LargeFile = 1024 * 1024 * 10 - function! LargeFile() - " no syntax highlighting etc - set eventignore+=FileType - " save memory when other file is viewed - setlocal bufhidden=unload - " is read-only (write with :w new_filename) - setlocal buftype=nowrite - " no undo possible - setlocal undolevels=-1 - " display message - autocmd VimEnter * echo "The file is larger than " . (g:LargeFile / 1024 / 1024) . " MB, so some options are changed (see vimrc for details)." - endfunction - - augroup LargeFile - au! - autocmd BufReadPre * let f=getfsize(expand("")) | if f > g:LargeFile || f == -2 | call LargeFile() | endif - augroup END - - let g:tag_system = get(g:, 'tag_system', 'gtags-cscope') - if has("cscope") - " use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t' - set cscopetag - " 使用quickfix窗口显示搜索结果 - if v:version >= 800 - set cscopequickfix=s-,g-,c-,t-,e-,f-,i-,d-,a- - else - set cscopequickfix=s-,g-,c-,t-,e-,f-,i-,d- - endif - - " show msg when any other cscope db added - set cscopeverbose - " 先搜索cscope数据库,如果没有再搜索tags - set csto=0 - " This tests to see if vim was configured with the '--enable-cscope' option - " when it was compiled. If it wasn't, time to recompile vim... - - """"""""""""" My cscope/vim key mappings - " - " The following maps all invoke one of the following cscope search types: - " - " 's' symbol: find all references to the token under cursor - " 'g' global: find global definition(s) of the token under cursor - " 'c' calls: find all calls to the function name under cursor - " 't' text: find all instances of the text under cursor - " 'e' egrep: egrep search for the word under cursor - " 'f' file: open the filename under cursor - " 'i' includes: find files that include the filename under cursor - " 'd' called: find functions that function under cursor calls - " 'a' assigned: find places where this symbol is assigned a value - - " = ... 用于计算表达式(如2+2, expand("")等),并将结果插入到命令行中 - nnoremap cs :cs find s =expand("") - nnoremap cg :cs find g =expand("") - nnoremap cc :cs find c =expand("") - nnoremap ct :cs find t =expand("") - nnoremap ce :cs find e =expand("") - nnoremap cf :cs find f =expand("") - nnoremap ci :cs find i =expand("") - nnoremap cd :cs find d =expand("") - nnoremap ca :cs find a =expand("") - - " 分裂窗口显示搜索结果 - " nmap s :scs find s =expand("") - " nmap s :vert scs find s =expand("") - - if g:tag_system == 'cscope' - " Reference: https://cscope.sourceforge.net/cscope_maps.vim - """"""""""""" Standard cscope/vim boilerplate - - " reset cscope: - " :cs reset - - " add any cscope database in current directory - if filereadable("cscope.out") - cs add cscope.out - " else add the database pointed to by environment variable - elseif $CSCOPE_DB != "" - cs add $CSCOPE_DB - endif - - elseif g:tag_system == 'gtags-cscope' - set csprg=gtags-cscope - if filereadable('GTAGS') - cs add GTAGS - endif - endif - endif - - if filereadable(expand("~/config_single_vimrc.vim")) - source $HOME/config_single_vimrc.vim - endif -endif diff --git a/root/.vim/autoload/cppfoldexpr.vim b/root/.vim/autoload/cppfoldexpr.vim deleted file mode 100644 index f7d2690..0000000 --- a/root/.vim/autoload/cppfoldexpr.vim +++ /dev/null @@ -1,31 +0,0 @@ -function! cppfoldexpr#CppFoldExpr(lnum) - let line = getline(a:lnum) - if !exists('b:cpp_fold_level') - let b:cpp_fold_level = 0 - let b:last_primitive = '' - endif - - let open_braces = len(split(line, '{', 1)) - 1 - let close_braces = len(split(line, '}', 1)) - 1 - - if b:last_primitive == 'if' - let b:cpp_fold_level += 1 - elseif b:last_primitive == 'end' - let b:cpp_fold_level -= 1 - endif - let b:last_primitive = '' - - if line =~ '^\s*#\s*\(ifdef\|ifndef\|if\)' || open_braces > close_braces - let b:cpp_fold_level += 1 - let b:last_primitive = 'if' - return b:cpp_fold_level - elseif line =~ '^\s*#\s*\(else\|elif\)' || (open_braces == close_braces && open_braces > 0) - return b:cpp_fold_level - 1 - elseif line =~ '^\s*#\s*endif' || close_braces > open_braces - let b:last_primitive = 'end' - let b:cpp_fold_level -= 1 - return b:cpp_fold_level - endif - - return b:cpp_fold_level -endfunction diff --git a/root/.vim/autoload/mycpp.vim b/root/.vim/autoload/mycpp.vim new file mode 100644 index 0000000..9d41301 --- /dev/null +++ b/root/.vim/autoload/mycpp.vim @@ -0,0 +1,62 @@ +function! mycpp#FoldExpr(lnum) + let line = getline(a:lnum) + if !exists('b:cpp_fold_level') + let b:cpp_fold_level = 0 + let b:last_primitive = '' + endif + + let open_braces = len(split(line, '{', 1)) - 1 + let close_braces = len(split(line, '}', 1)) - 1 + + if b:last_primitive == 'if' + let b:cpp_fold_level += 1 + elseif b:last_primitive == 'end' + let b:cpp_fold_level -= 1 + endif + let b:last_primitive = '' + + if line =~ '^\s*#\s*\(ifdef\|ifndef\|if\)' || open_braces > close_braces + let b:cpp_fold_level += 1 + let b:last_primitive = 'if' + return b:cpp_fold_level + elseif line =~ '^\s*#\s*\(else\|elif\)' || (open_braces == close_braces && open_braces > 0) + return b:cpp_fold_level - 1 + elseif line =~ '^\s*#\s*endif' || close_braces > open_braces + let b:last_primitive = 'end' + let b:cpp_fold_level -= 1 + return b:cpp_fold_level + endif + + return b:cpp_fold_level +endfunction +function! mycpp#CompleteFunc(findstart, base) + if a:findstart + " 确定补全开始的位置 + let line = getline('.') + let start = col('.') - 1 + while start > 0 && line[start - 1] =~ '\S' + let start -= 1 + endwhile + if line[start] != '<' && line[start] != '"' + return -3 + endif + return start + 1 + endif + + if getline('.') =~# &include + let filetype = &filetype + if filetype == 'cpp' || filetype == 'cuda' + " r! ls -1 /usr/include/c++/13 | awk '{print " \x27"$0"\x27"}' | paste -sd, + let completions = ['algorithm', 'any', 'array', 'atomic', 'backward', 'barrier', 'bit', 'bits', 'bitset', 'cassert', 'ccomplex', 'cctype', 'cerrno', 'cfenv', 'cfloat', 'charconv', 'chrono', 'cinttypes', 'ciso646', 'climits', 'clocale', 'cmath', 'codecvt', 'compare', 'complex', 'complex.h', 'concepts', 'condition_variable', 'coroutine', 'csetjmp', 'csignal', 'cstdalign', 'cstdarg', 'cstdbool', 'cstddef', 'cstdint', 'cstdio', 'cstdlib', 'cstring', 'ctgmath', 'ctime', 'cuchar', 'cwchar', 'cwctype', 'cxxabi.h', 'debug', 'decimal', 'deque', 'exception', 'execution', 'expected', 'experimental', 'ext', 'fenv.h', 'filesystem', 'format', 'forward_list', 'fstream', 'functional', 'future', 'initializer_list', 'iomanip', 'ios', 'iosfwd', 'iostream', 'istream', 'iterator', 'latch', 'limits', 'list', 'locale', 'map', 'math.h', 'memory', 'memory_resource', 'mutex', 'new', 'numbers', 'numeric', 'optional', 'ostream', 'parallel', 'pstl', 'queue', 'random', 'ranges', 'ratio', 'regex', 'scoped_allocator', 'semaphore', 'set', 'shared_mutex', 'source_location', 'span', 'spanstream', 'sstream', 'stack', 'stacktrace', 'stdatomic.h', 'stdexcept', 'stdfloat', 'stdlib.h', 'stop_token', 'streambuf', 'string', 'string_view', 'syncstream', 'system_error', 'tgmath.h', 'thread', 'tr1', 'tr2', 'tuple', 'typeindex', 'typeinfo', 'type_traits', 'unordered_map', 'unordered_set', 'utility', 'valarray', 'variant', 'vector', 'version'] + call filter(completions, 'v:val =~ "^" . a:base') + else + let completions = [] + endif + " 使用getcompletion()获取文件类型的补全列表 + echom a:base + let completions += getcompletion(a:base . '*.h*', 'file_in_path', 1) + let completions += getcompletion(a:base . '*/$', 'file_in_path', 1) + return completions + endif + return [] +endfunction diff --git a/root/.vim/vimrc b/root/.vim/vimrc index d3485be..1a89d1e 100644 --- a/root/.vim/vimrc +++ b/root/.vim/vimrc @@ -327,9 +327,8 @@ if has('autocmd') " vim-tiny does not have autocmd nnoremap zpr :setlocal foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)\\|\\|(getline(v:lnum+1)=~@/)?1:2 foldmethod=expr foldlevel=0 foldcolumn=2:set foldmethod=syntax " 打开所有折叠: zR - if s:has_vimrcd - autocmd FileType c,cpp setlocal foldmethod=expr foldexpr=cppfoldexpr#CppFoldExpr(v:lnum) - endif + autocmd FileType c,cpp,cuda setlocal foldmethod=expr foldexpr=mycpp#FoldExpr(v:lnum) + autocmd FileType c,cpp,cuda setlocal completefunc=mycpp#CompleteFunc autocmd FileType python,vim,lua,go,markdown,sh,tex setlocal foldmethod=indent endif @@ -337,7 +336,7 @@ if has('autocmd') " vim-tiny does not have autocmd " command -nargs=0 GitBlame !git blame -L line(".") + 1, line(".") + 1 -- % " [[palette]]git-blame :GitBlame command -range -nargs=0 GitBlame :!git blame -n -L , -- % - + " save inoremap :wi diff --git a/root/.vim/vimrc.d/fzf.vim b/root/.vim/vimrc.d/fzf.vim index c8f7cd6..6e572a1 100644 --- a/root/.vim/vimrc.d/fzf.vim +++ b/root/.vim/vimrc.d/fzf.vim @@ -53,6 +53,7 @@ command! -nargs=0 GitUnstaged call fzf#run(fzf#wrap({'source': 'git ls-files -o " On :LS!, evaluates to '!', and '!0' becomes 1 " The query history for this command will be stored as 'ls' inside g:fzf_history_dir. " The name is ignored if g:fzf_history_dir is not defined. +let g:fzf_history_dir = '~/.local/share/fzf-history' " [[palette]]fzf ls :LS command! -bang -complete=dir -nargs=? LS \ call fzf#run(fzf#wrap({'source': 'ls', 'dir': }, 0)) diff --git a/root/.vim/vimrc.d/plugin.vim b/root/.vim/vimrc.d/plugin.vim index 399178f..2007c2e 100644 --- a/root/.vim/vimrc.d/plugin.vim +++ b/root/.vim/vimrc.d/plugin.vim @@ -197,6 +197,8 @@ if !exists('g:vscode') if v:version >= 900 " Alternative: https://github.com/gelguy/wilder.nvim Plug 'girishji/autosuggest.vim' + " External cmd is slow. + autocmd VimEnter * call g:AutoSuggestSetup({ 'cmd': { 'exclude': ['!'] }}) endif if v:version >= 802 if g:vimrc_use_coc @@ -252,6 +254,9 @@ if !exists('g:vscode') let g:GIT_LENS_ENABLED = 0 endif + Plug 'jiangyinzuo/fzf-gitdiff.vim' + command! -nargs=* GitDiff call fzf_gitdiff#FillFZF() + " Plug 'MattesGroeger/vim-bookmarks' if has('nvim') || v:version >= 802 Plug 'pechorin/any-jump.vim' diff --git a/root/scripts/vimdf b/root/scripts/vimdf deleted file mode 100755 index 3423e75..0000000 --- a/root/scripts/vimdf +++ /dev/null @@ -1,108 +0,0 @@ -#!/bin/bash -# https://superuser.com/questions/455944/vimdiff-and-git-all-at-once - -if [[ ! -d .git ]]; then - echo "No .git directory found" - exit 1 -fi - -nofile=0 -while [[ $# -gt 0 ]]; do - case $1 in - -h) - echo "Usage:" - echo " vimdf [-n] " - echo "" - echo "Options:" - echo " -n, --nofile Set 'setlocal buftype=nofile' for the current working tree." - echo "" - echo "Arguments:" - echo " The starting commit for the diff (can be an empty string)." - echo " The ending commit for the diff (can be an empty string)." - echo " The directory to apply the diff (can be an empty string)." - echo "" - echo "Description:" - echo " This script runs the 'git diff --name-only ' command and opens the results in Vim." - echo "" - echo "Examples:" - echo " To diff between the current working tree and HEAD:" - echo " $ vimdf" - echo " To diff between the current working tree and HEAD~2:" - echo " $ vimdf HEAD~2" - echo " To diff between HEAD~1 and HEAD:" - echo " $ vimdf HEAD~1 HEAD" - echo " To diff between the current working tree and HEAD in the 'src' directory:" - echo " $ vimdf '' '' src" - echo "" - echo "Vim Tips:" - echo " Use ':diffoff' to turn off diff mode." - echo " Use ':diffthis' to re-enable diff mode." - exit 1 - ;; - -n|--nofile) - nofile=1 - shift - ;; - *) - break - ;; - esac -done - -commit1=$1 -commit2=$2 -dir=$3 - -_add_file() { - cmd+="$EXE ':tabnew $f'" - if [[ $nofile == 1 ]] ; then - cmd+="$EXE ':setlocal buftype=nofile'" - fi - cmd+="$EXE ':0 | filetype detect'" - # $f may not exist in history commit - cmd+="$EXE ':vnew | f <$1>/${f} | setlocal buftype=nofile | 0read ! git show $1:$f || true' " - cmd+="$EXE ':0 | filetype detect'" -} - -_add_gitsubmodule() { - cmd+="$EXE ':tabnew | setlocal buftype=nofile | 0read ! git ls-tree $2 $f || true'" - cmd+="$EXE ':vnew | setlocal buftype=nofile | 0read ! git ls-tree $1 $f || true'" -} - -files=`git diff ${commit1} ${commit2} --name-only ${dir}` - -cmd="set nosplitright | set nobackup | set noswapfile" - -EXE="| silent! exe" - -for f in $files; do - is_submodule=$(git ls-files --stage $f | grep -c ^160000) - if [[ $is_submodule == 1 ]]; then - if [[ -z "$commit2" && -z "$commit1" ]]; then - _add_gitsubmodule HEAD - elif [[ -z "$commit2" ]]; then - _add_gitsubmodule $commit1 - elif [[ -z "$commit1" ]]; then - _add_gitsubmodule $commit2 - else - _add_gitsubmodule $commit1 $commit2 - fi - else - if [[ -z "$commit2" && -z "$commit1" ]]; then - _add_file HEAD - elif [[ -z "$commit2" ]]; then - _add_file $commit1 - elif [[ -z "$commit1" ]]; then - _add_file $commit2 - else - cmd+="$EXE ':tabnew | f <$commit2>/${f} | setlocal buftype=nofile | 0read ! git show $commit2:$f || true'" - cmd+="$EXE ':0 | filetype detect'" - cmd+="$EXE ':vnew | f <$commit1>/${f} | setlocal buftype=nofile | 0read ! git show $commit1:$f || true'" - cmd+="$EXE ':0 | filetype detect'" - fi - fi - cmd+="$EXE ':windo diffth'" -done -cmd+="$EXE 'bd 1 | tabfirst'" - -vim -c "$cmd"