diff options
author | Jesse Luehrs <doy@tozt.net> | 2010-04-26 05:56:45 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2010-04-26 05:56:45 -0500 |
commit | e56eb8219ce55fb64d01cfa39a12cb1a123fbf98 (patch) | |
tree | ec7ae32c59898cca77b4cf58a6a1bcb3bffef3d9 /vimrc | |
parent | 55a0168810a2c7e3237f83d082948873bcb70dd3 (diff) | |
download | conf-e56eb8219ce55fb64d01cfa39a12cb1a123fbf98.tar.gz conf-e56eb8219ce55fb64d01cfa39a12cb1a123fbf98.zip |
add a few vimrc functions from sartak
Diffstat (limited to 'vimrc')
-rw-r--r-- | vimrc | 61 |
1 files changed, 61 insertions, 0 deletions
@@ -259,6 +259,26 @@ au BufWritePost *.{sh,pl} silent exe "!chmod +x %" " Update ctags after writing {{{ autocmd BufWritePost * if filereadable('tags') | silent exe "!ctags -a %" | redraw | endif " }}} +" Prompt to create directories if they don't exist {{{ +autocmd BufNewFile * :call <SID>ensure_dir_exists() +function s:ensure_dir_exists () + let required_dir = expand("%:h") + if !isdirectory(required_dir) + call <SID>ask_quit("Directory '" . required_dir . "' doesn't exist.", "&Create it?") + + try + call mkdir( required_dir, 'p' ) + catch + call <SID>ask_quit("Can't create '" . required_dir . "'", "&Continue anyway?") + endtry + endif +endfunction +function s:ask_quit (msg, proposed_action) + if confirm(a:msg, "&Quit?\n" . a:proposed_action) == 1 + exit + endif +endfunction +" }}} " Misc {{{ autocmd BufWritePost *conkyrc silent exe "!killall -HUP conky" autocmd QuickFixCmdPost * copen 3 @@ -482,6 +502,47 @@ endfunction nmap <silent>K :call Help(0, [], '<SID>man')<CR> xmap <silent>K :call Help(1, [], '<SID>man')<CR> " }}} +" ;= to align = signs {{{ +function s:align_assignments() + " Patterns needed to locate assignment operators... + let ASSIGN_OP = '[-+*/%|&]\?=\@<!=[=~]\@!' + let ASSIGN_LINE = '^\(.\{-}\)\s*\(' . ASSIGN_OP . '\)\(.*\)$' + + " Locate block of code to be considered (same indentation, no blanks) + let indent_pat = '^' . matchstr(getline('.'), '^\s*') . '\S' + let firstline = search('^\%('. indent_pat . '\)\@!','bnW') + 1 + let lastline = search('^\%('. indent_pat . '\)\@!', 'nW') - 1 + if lastline < 0 + let lastline = line('$') + endif + + " Decompose lines at assignment operators... + let lines = [] + for linetext in getline(firstline, lastline) + let fields = matchlist(linetext, ASSIGN_LINE) + call add(lines, fields[1:3]) + endfor + + " Determine maximal lengths of lvalue and operator... + let op_lines = filter(copy(lines),'!empty(v:val)') + let max_lval = max( map(copy(op_lines), 'strlen(v:val[0])') ) + 1 + let max_op = max( map(copy(op_lines), 'strlen(v:val[1])' ) ) + + " Recompose lines with operators at the maximum length... + let linenum = firstline + for line in lines + if !empty(line) + let newline + \ = printf("%-*s%*s%s", max_lval, line[0], max_op, line[1], line[2]) + call setline(linenum, newline) + endif + let linenum += 1 + endfor +endfunction +nmap <silent> <Leader>= :call <SID>align_assignments()<CR> +" fix this to work in visual mode properly +"xmap <silent> <Leader>= :call <SID>align_assignments()<CR> +" }}} " Miscellaneous {{{ " have Y behave analogously to D rather than to dd nmap Y y$ |