From 526106d592d7203c108e72c2d30eacb70dd7a9d7 Mon Sep 17 00:00:00 2001 From: zlg Date: Sat, 2 May 2020 11:19:30 -0700 Subject: Reintegrate personal library as autoload script Functions that I've built up over time were squirreled away in a separate file for a while, but they were subtly broken and some didn't work. All functions in the library should now work as expected, and are easy to integrate into other installations of Vim. --- functions.vim | 270 ------------------------------------------------------- vimrc | 21 ++--- zlglib.vim | 281 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 292 insertions(+), 280 deletions(-) delete mode 100644 functions.vim create mode 100644 zlglib.vim diff --git a/functions.vim b/functions.vim deleted file mode 100644 index 5264701..0000000 --- a/functions.vim +++ /dev/null @@ -1,270 +0,0 @@ -" StripTrailingWhitespaces, by Drew Neil of vimcasts.org {{{1 -function! StripTrailingWhitespaces() - " Save last search and cursor position - let _s=@/ - let l = line(".") - let c = col(".") - " Do the business: - %s/\s\+$//e - " Restore previous search history and cursor position - let @/=_s - call cursor(l, c) -endfunction - -" SynStack, by Drew Neil of vimcasts.org {{{1 -function! SynStack() - if !exists("*synstack") - return - endif - echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")') -endfunction - -" CountCodeLines, by zlg {{{1 -function! CountCodeLines() - let code_count = 0 - for line in getbufline("%", 1, "$") - if (len(line) > 0 && match(line, '\S\+') > -1) - if (s:IsCommentLine(line) == 1) - continue - endif - let code_count += 1 - endif - endfor - echo code_count . ' lines of code.' -endfunction - -" IsCommentLine, by zlg {{{1 -" Meant to be used with CountCodeLines() -function! s:IsCommentLine(line) - let l:comtypes = [] - let l:comlist = split(&comments, ',') - for i in comlist - let l:type = split(i, ':') - if len(type) > 1 - let l:opt = type[1] - else - let l:opt = type[0] - endif - call add(comtypes, opt) - endfor - let i = 0 - while i < len(comtypes) - if match(a:line, '^\s*' . escape(comtypes[i], '/*')) > -1 - return 1 - endif - let i += 1 - endwhile - return 0 -endfunction - -" InsertDOW, by zlg {{{1 -function! InsertDOW() - " Let's backup a random register - let tmpx = @x - let @x = system("date +\"%a \" -d" . strftime("%Y") . "-" . substitute(expand(""), ":", "", "")) - normal "xP - " Join, since it apparently is on a line instead of just a string - j 1 - " Return the value in the x register - let @x = tmpx -endfunction - -" BufDiff, by zlg {{{1 -" Function: BufDiff -" Author: zlg -" Date: 2018-10-12 -" License: GNU GPL, version 3 -" Description: Open a new tab page with two or three buffers and enter diff mode. -" Long Description: -" BufDiff accepts two or three buffer arguments and opens a new tab page to -" work with them in diff mode. This ensures that your existing window and -" buffer layout is preserved while providing an easy way to enter a diff -" session. The arguments can be any argument that can also be passed to -" the ":buffer" Ex-command, including partial filenames (if quoted). -function! BufDiff(buf1, buf2, ...) - let fname = substitute(expand(""), "^function ", "", "") - if a:0 > 1 - echohl ErrorMsg - echomsg l:fname . ": This function accepts a maximum of three arguments." - echohl None - return - endif - - " Check for a third buffer, which will default to zero if not present - let a:buf3 = get(a:, 1, 0) - - if !bufexists(a:buf1) || !bufexists(a:buf2) || (a:0 == 1 && !bufexists(a:buf3)) - echohl ErrorMsg - echomsg l:fname . ": One or more buffer number(s) not found!" - echohl None - return - else - tabnew - exe ":buffer " . a:buf1 - diffthis - vsplit - exe ":buffer " . a:buf2 - diffthis - if a:buf3 - vsplit - exe ":buffer " . a:buf3 - diffthis - endif - return - endif -endfunction - -" SwapKeys, by zlg {{{1 -" This function isn't that fancy; it just flips a variable and calls the -" mapping function. -function! SwapKeys() - if (exists("g:use_dvorak") && (g:use_dvorak == 1 || g:use_dvorak == 0)) - let g:use_dvorak = (!g:use_dvorak) - call SetKeyMap() - else - echohl ErrorMsg - echomsg "g:use_dvorak is either unset, or an invalid value. Set to 0 or 1. - echohl None - return - endif -endfunction - -" SetKeyMap, by zlg {{{1 -" Function: SetKeyMap -" Author: zlg -" Date: 2018-11-15 -" License: GNU GPL, version 3 -" Long Description: -" SetKeyMap allows the user to set their keybindings according to the value of -" a global variable. In this case, it's g:use_dvorak, to swap between Dvorak -" and QWERTY layouts. It can be extended to use any number of key mappings -" that you need. -function! SetKeyMap() - if g:use_dvorak == 1 - " I use a Dvorak keyboard, so standard vim movement keys are a hassle. - " Since mnemonics are helpful all around, I need a mapping that gives me - " pain-free file navigation without screwing up mnemonics (too much). - " - " Thus: - " (D)elete is now (K)ill: d2w == k2w "kill 2 words" - " Un(T)il is now (J)ump-To: dt( == kj( "kill, jump-to paren" - " (N)ext is now (L)eap: cn == cl "change up to leap point" - - " Standard movement, which is the true focus of this hack. - " Mnemonics are awesome, so let's preserve as much as possible. - noremap d h - noremap h j - noremap t k - noremap n l - noremap D H - noremap H J - noremap T K - noremap N L - - noremap gh gj - noremap gt gk - - " I work with tabs every now and then. No, I don't have a mnemonic. - noremap gj gt - noremap gJ gT - - " Window movement, equally important - noremap d h - noremap h j - noremap t k - noremap n l - - nnoremap - nnoremap - nnoremap - nnoremap - - nnoremap h - nnoremap j - nnoremap k - nnoremap l - - " Account for tag jumping - nnoremap - - " Remappings for the D key - noremap k d - noremap K D - - " Remappings for the T key - noremap j t - noremap J T - - " Remapping for the L key - noremap l n - noremap L N - - " General purpose help; the originals remain for convenience - noremap - 0 - noremap _ $ - - " Fold-related keybindings - noremap zh zj - noremap zt zk - echomsg "Bindings set to Dvorak" - else - " set qwerty keys; basically just resetting things - noremap h h - noremap j j - noremap k k - noremap l l - noremap H H - noremap J J - noremap K K - noremap L L - - noremap gj gj - noremap gh gh - - noremap gt gt - noremap gT gT - - noremap h h - noremap j j - noremap h k - noremap l l - - nnoremap - nnoremap - nnoremap - nnoremap - - nnoremap h - nnoremap j - nnoremap k - nnoremap l - - nnoremap - - noremap d d - noremap D D - - noremap t t - noremap T T - - noremap n n - noremap N N - - noremap - 0 - noremap _ $ - - noremap zj zj - noremap zk zk - echomsg "Bindings set to QWERTY" - endif -endfunction - -" Custom folding display function -function! MyFoldText() - let indent = repeat(' ', indent(v:foldstart)) - let line = substitute(getline(v:foldstart), '\v^\s+', '', '') - let folded_lines = v:foldend - v:foldstart - return indent . line . ' (' . folded_lines . 'L)' -endfunction - -" vim: foldmethod=marker diff --git a/vimrc b/vimrc index 504d205..ed219d1 100644 --- a/vimrc +++ b/vimrc @@ -8,17 +8,18 @@ set fileformats=unix,dos,mac set spelllang=en_us set switchbuf=useopen -" Outsourced functions file -runtime functions.vim - " Colors and Syntax {{{1 set t_Co=16 set background=dark -colorscheme solarized +colorscheme zlg if has('syntax') && !exists('g:syntax_on') syntax enable endif +" General purpose syntax useful in all files +highlight TrailingWhitespace ctermbg=magenta term=underline +syntax match TrailingWhitespace /\s\+$/ + " Plugin-Specific {{{1 " Use vim-plug call plug#begin('~/.vim/bundle') @@ -116,16 +117,16 @@ if has("autocmd") endif " Key Bindings {{{1 -nnoremap i :call SynStack() +nnoremap i :call zlg#SynStack() nnoremap l :set list! nnoremap n :set number! nnoremap t :setfiletype text nnoremap s :set spell! -nnoremap c :call CountCodeLines() -nnoremap d :call InsertDOW() +nnoremap c :call zlg#CountCodeLines() +nnoremap d :call zlg#InsertDOW() nnoremap D "=strftime("%Y-%m-%d %H:%M", localtime())p nnoremap za -nnoremap :call StripTrailingWhitespaces() +nnoremap :call zlg#StripTrailingWhitespaces() " Thanks to /u/spupy for these short and sweet mappings that replace the " simpler functions of tpope's surround.vim plugin. Each of these will @@ -140,12 +141,12 @@ noremap w< `>a>` noremap w` `>a`` " super handy over SSH -noremap q :call SwapKeys() +noremap q :call zlg#SwapKeys() " Dvorak Hackery {{{1 if exists("g:use_dvorak") && g:use_dvorak == 1 - silent call SetKeyMap() + silent call zlg#SetKeyMap() endif " vim: foldmethod=marker diff --git a/zlglib.vim b/zlglib.vim new file mode 100644 index 0000000..0e280ff --- /dev/null +++ b/zlglib.vim @@ -0,0 +1,281 @@ +" Name: zlglib.vim +" Author: zlg +" License: Vim +" +" Integrate by symlinking to this file inside the 'autoload' directory: +" cd $HOME/.vim/autoload +" ln -s /path/to/zlglib.vim zlg.vim +" +" Next, hook things up as you wish via something like :call zlg#foo() + +" StripTrailingWhitespaces, by Drew Neil of vimcasts.org {{{1 +function! zlg#StripTrailingWhitespaces() + " Save last search and cursor position + let _s=@/ + let l = line(".") + let c = col(".") + " Do the business: + %s/\s\+$//e + " Restore previous search history and cursor position + let @/=_s + call cursor(l, c) +endfunction + +" SynStack, by Drew Neil of vimcasts.org {{{1 +function! zlg#SynStack() + if !exists("*synstack") + return + endif + echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")') +endfunction + +" CountCodeLines, by zlg {{{1 +function! zlg#CountCodeLines() + let code_count = 0 + for line in getbufline("%", 1, "$") + if (len(line) > 0 && match(line, '\S\+') > -1) + if (s:IsCommentLine(line) == 1) + continue + endif + let code_count += 1 + endif + endfor + echo code_count . ' lines of code.' +endfunction + +" IsCommentLine, by zlg {{{1 +" Meant to be used with CountCodeLines() +function! s:IsCommentLine(line) + let l:comtypes = [] + let l:comlist = split(&comments, ',') + for i in comlist + let l:type = split(i, ':') + if len(type) > 1 + let l:opt = type[1] + else + let l:opt = type[0] + endif + call add(comtypes, opt) + endfor + let i = 0 + while i < len(comtypes) + if match(a:line, '^\s*' . escape(comtypes[i], '/*')) > -1 + return 1 + endif + let i += 1 + endwhile + return 0 +endfunction + +" InsertDOW, by zlg {{{1 +function! zlg#InsertDOW() + " Let's backup a random register + let tmpx = @x + let @x = system("date +\"%a \" -d" . strftime("%Y") . "-" . substitute(expand(""), ":", "", "")) + normal "xP + " Join, since it apparently is on a line instead of just a string + j 1 + " Return the value in the x register + let @x = tmpx +endfunction + +" BufDiff, by zlg {{{1 +" Function: BufDiff +" Author: zlg +" Date: 2018-10-12 +" License: GNU GPL, version 3 +" Description: Open a new tab page with two or three buffers and enter diff mode. +" Long Description: +" BufDiff accepts two or three buffer arguments and opens a new tab page to +" work with them in diff mode. This ensures that your existing window and +" buffer layout is preserved while providing an easy way to enter a diff +" session. The arguments can be any argument that can also be passed to +" the ":buffer" Ex-command, including partial filenames (if quoted). +function! BufDiff(buf1, buf2, ...) + let fname = substitute(expand(""), "^function ", "", "") + if a:0 > 1 + echohl ErrorMsg + echomsg l:fname . ": This function accepts a maximum of three arguments." + echohl None + return + endif + + " Check for a third buffer, which will default to zero if not present + let a:buf3 = get(a:, 1, 0) + + if !bufexists(a:buf1) || !bufexists(a:buf2) || (a:0 == 1 && !bufexists(a:buf3)) + echohl ErrorMsg + echomsg l:fname . ": One or more buffer number(s) not found!" + echohl None + return + else + tabnew + exe ":buffer " . a:buf1 + diffthis + vsplit + exe ":buffer " . a:buf2 + diffthis + if a:buf3 + vsplit + exe ":buffer " . a:buf3 + diffthis + endif + return + endif +endfunction + +" SwapKeys, by zlg {{{1 +" This function isn't that fancy; it just flips a variable and calls the +" mapping function. +function! zlg#SwapKeys() + if (exists("g:use_dvorak") && (g:use_dvorak == 1 || g:use_dvorak == 0)) + let g:use_dvorak = (!g:use_dvorak) + call SetKeyMap() + else + echohl ErrorMsg + echomsg "g:use_dvorak is either unset, or an invalid value. Set to 0 or 1. + echohl None + return + endif +endfunction + +" SetKeyMap, by zlg {{{1 +" Function: SetKeyMap +" Author: zlg +" Date: 2018-11-15 +" License: GNU GPL, version 3 +" Long Description: +" SetKeyMap allows the user to set their keybindings according to the value of +" a global variable. In this case, it's g:use_dvorak, to swap between Dvorak +" and QWERTY layouts. It can be extended to use any number of key mappings +" that you need. +function! zlg#SetKeyMap() + if g:use_dvorak == 1 + " I use a Dvorak keyboard, so standard vim movement keys are a hassle. + " Since mnemonics are helpful all around, I need a mapping that gives me + " pain-free file navigation without screwing up mnemonics (too much). + " + " Thus: + " (D)elete is now (K)ill: d2w == k2w "kill 2 words" + " Un(T)il is now (J)ump-To: dt( == kj( "kill, jump-to paren" + " (N)ext is now (L)eap: cn == cl "change up to leap point" + + " Standard movement, which is the true focus of this hack. + " Mnemonics are awesome, so let's preserve as much as possible. + noremap d h + noremap h j + noremap t k + noremap n l + noremap D H + noremap H J + noremap T K + noremap N L + + noremap gh gj + noremap gt gk + + " I work with tabs every now and then. No, I don't have a mnemonic. + noremap gj gt + noremap gJ gT + + " Window movement, equally important + noremap d h + noremap h j + noremap t k + noremap n l + + nnoremap + nnoremap + nnoremap + nnoremap + + nnoremap h + nnoremap j + nnoremap k + nnoremap l + + " Account for tag jumping + nnoremap + + " Remappings for the D key + noremap k d + noremap K D + + " Remappings for the T key + noremap j t + noremap J T + + " Remapping for the L key + noremap l n + noremap L N + + " General purpose help; the originals remain for convenience + noremap - 0 + noremap _ $ + + " Fold-related keybindings + noremap zh zj + noremap zt zk + + echomsg "Bindings set to Dvorak" + else + " set qwerty keys; basically just resetting things + noremap h h + noremap j j + noremap k k + noremap l l + noremap H H + noremap J J + noremap K K + noremap L L + + noremap gj gj + noremap gh gh + + noremap gt gt + noremap gT gT + + noremap h h + noremap j j + noremap h k + noremap l l + + nnoremap + nnoremap + nnoremap + nnoremap + + nnoremap h + nnoremap j + nnoremap k + nnoremap l + + nnoremap + + noremap d d + noremap D D + + noremap t t + noremap T T + + noremap n n + noremap N N + + noremap - 0 + noremap _ $ + + noremap zj zj + noremap zk zk + echomsg "Bindings set to QWERTY" + endif +endfunction + +" Custom folding display function +function! MyFoldText() + let indent = repeat(' ', indent(v:foldstart)) + let line = substitute(getline(v:foldstart), '\v^\s+', '', '') + let folded_lines = v:foldend - v:foldstart + return indent . line . ' (' . folded_lines . 'L)' +endfunction + +" vim: foldmethod=marker -- cgit v1.2.3-54-g00ecf