From 7f9cde4ce19a0f3d8e9e555dabda5867bd878e46 Mon Sep 17 00:00:00 2001 From: Sergio Alvarez-Napagao Date: Fri, 30 Jan 2015 11:34:47 +0000 Subject: [PATCH 1/2] Add option to enable unconditional balancing of [] and {}. --- doc/paredit.txt | 10 +++++++++- plugin/paredit.vim | 23 ++++++++++++++--------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/doc/paredit.txt b/doc/paredit.txt index f9e59ae..579fc47 100644 --- a/doc/paredit.txt +++ b/doc/paredit.txt @@ -22,7 +22,7 @@ added or deleted, they are entered or removed in pairs. The function takes care of strings and comments, so no parenthesis and square bracket balancing is performed inside a string or comment. Please note that [] and {} pairs are not balanced for Lisp filetypes, only -for Clojure and Scheme. +for Clojure and Scheme by default. The idea is taken from the paredit mode of Emacs, but not all paredit.el editing functions are implemented or behave exactly the same way as they do @@ -378,6 +378,9 @@ PAREDIT OPTIONS *paredit-options* |g:paredit_electric_return| If nonzero, electric return feature is enabled. +|g:paredit_full_balancing| If nonzero, [] and {} are also balanced, + regardless of the language. + |g:paredit_smartjump| If nonzero, '(' and ')' also target square brackets and curly braces when editing Clojure or Scheme. @@ -414,6 +417,11 @@ to send the command line to the swank server for evaluation. Please find a video demonstration of the electric return feature here: http://img8.imageshack.us/img8/9479/openparen.gif + *g:paredit_full_balancing* +If nonzero, this option changes the default behavior of only balancing +'(' and ')' when the language is Clojure or Scheme. Therefore, in this option +'[', ']', '{', and '}' are also balanced regardless of the language. + *g:paredit_smartjump* If nonzero, this option changes the behavior of '(' and ')' in normal and visual modes when editing Clojure or Scheme. Rather than jumping to nearest open or close diff --git a/plugin/paredit.vim b/plugin/paredit.vim index 4fcfd9b..3215f48 100644 --- a/plugin/paredit.vim +++ b/plugin/paredit.vim @@ -29,6 +29,11 @@ if !exists( 'g:paredit_mode' ) let g:paredit_mode = 1 endif +" Enable [] and {} balancing outside Clojure and Scheme +if !exists( 'g:paredit_full_balancing' ) + let g:paredit_full_balancing = 0 +endif + " Match delimiter this number of lines before and after cursor position if !exists( 'g:paredit_matchlines' ) let g:paredit_matchlines = 100 @@ -82,7 +87,7 @@ function! PareditInitBuffer() let b:paredit_init = 1 " in case they are accidentally removed " Also define regular expressions to identify special characters used by paredit - if &ft =~ '.*\(clojure\|scheme\|racket\).*' + if &ft =~ '.*\(clojure\|scheme\|racket\).*' || g:paredit_full_balancing let b:any_matched_char = '(\|)\|\[\|\]\|{\|}\|\"' let b:any_matched_pair = '()\|\[\]\|{}\|\"\"' let b:any_opening_char = '(\|\[\|{' @@ -219,7 +224,7 @@ function! PareditInitBuffer() silent! unmap cb silent! unmap ciw silent! unmap caw - if &ft =~ '.*\(clojure\|scheme\|racket\).*' + if &ft =~ '.*\(clojure\|scheme\|racket\).*' || g:paredit_full_balancing silent! iunmap [ silent! iunmap ] silent! iunmap { @@ -251,7 +256,7 @@ endfunction " Include all prefix and special characters in 'iskeyword' function! s:SetKeyword() let old_value = &iskeyword - if &ft =~ '.*\(clojure\|scheme\|racket\).*' + if &ft =~ '.*\(clojure\|scheme\|racket\).*' || g:paredit_full_balancing setlocal iskeyword+=+,-,*,/,%,<,=,>,:,$,?,!,@-@,94,~,#,\|,& else setlocal iskeyword+=+,-,*,/,%,<,=,>,:,$,?,!,@-@,94,~,#,\|,&,.,{,},[,] @@ -574,7 +579,7 @@ function! s:IsBalanced() return 0 endif - if &ft =~ '.*\(clojure\|scheme\|racket\).*' + if &ft =~ '.*\(clojure\|scheme\|racket\).*' || g:paredit_full_balancing let b1 = searchpair( '\[', '', '\]', 'brnmW', s:skip_sc, matchb ) let b2 = searchpair( '\[', '', '\]', 'rnmW', s:skip_sc, matchf ) if !(b1 == b2) && !(b1 == b2 - 1 && line[c-1] == '[') && !(b1 == b2 + 1 && line[c-1] == ']') @@ -637,7 +642,7 @@ function! s:Unbalanced( matched ) while 1 let matched = tmp let tmp = substitute( tmp, '(\(\s*\))', ' \1 ', 'g') - if &ft =~ '.*\(clojure\|scheme\|racket\).*' + if &ft =~ '.*\(clojure\|scheme\|racket\).*' || g:paredit_full_balancing let tmp = substitute( tmp, '\[\(\s*\)\]', ' \1 ', 'g') let tmp = substitute( tmp, '{\(\s*\)}', ' \1 ', 'g') endif @@ -645,7 +650,7 @@ function! s:Unbalanced( matched ) if tmp == matched " All paired chars eliminated let tmp = substitute( tmp, ')\(\s*\)(', ' \1 ', 'g') - if &ft =~ '.*\(clojure\|scheme\|racket\).*' + if &ft =~ '.*\(clojure\|scheme\|racket\).*' || g:paredit_full_balancing let tmp = substitute( tmp, '\]\(\s*\)\[', ' \1 ', 'g') let tmp = substitute( tmp, '}\(\s*\){', ' \1 ', 'g') endif @@ -810,7 +815,7 @@ function! s:ReGatherUp() normal! ddk endwhile normal! Jl - elseif g:paredit_electric_return && getline('.') =~ '^\s*\(\]\|}\)' && &ft =~ '.*\(clojure\|scheme\|racket\).*' + elseif g:paredit_electric_return && getline('.') =~ '^\s*\(\]\|}\)' && (&ft =~ '.*\(clojure\|scheme\|racket\).*' || g:paredit_full_balancing) " Re-gather electric returns in the current line for ']' and '}' normal! k while getline( line('.') ) =~ '^\s*$' @@ -865,7 +870,7 @@ function! PareditInsertClosing( open, close ) normal! Jl return endif - if len(nextline) > 0 && nextline[0] =~ '\]\|}' && &ft =~ '.*\(clojure\|scheme\|racket\).*' + if len(nextline) > 0 && nextline[0] =~ '\]\|}' && (&ft =~ '.*\(clojure\|scheme\|racket\).*' || g:paredit_full_balancing) " Re-gather electric returns in the line of the closing ']' or '}' call setline( line('.'), substitute( line, '\s*$', '', 'g' ) ) normal! Jxl @@ -1495,7 +1500,7 @@ function! s:FindClosing() endif call setpos( '.', [0, l, c, 0] ) - if &ft =~ '.*\(clojure\|scheme\|racket\).*' + if &ft =~ '.*\(clojure\|scheme\|racket\).*' || g:paredit_full_balancing call PareditFindClosing( '[', ']', 0 ) let lp = line( '.' ) let cp = col( '.' ) From c934ba47849069ed4fdf27af6d7b5559b090e0dc Mon Sep 17 00:00:00 2001 From: Sergio Alvarez-Napagao Date: Fri, 30 Jan 2015 11:46:55 +0000 Subject: [PATCH 2/2] Cover case that was not taken care of for full balancing. --- plugin/paredit.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/paredit.vim b/plugin/paredit.vim index 3215f48..6dbc4b1 100644 --- a/plugin/paredit.vim +++ b/plugin/paredit.vim @@ -155,7 +155,7 @@ function! PareditInitBuffer() execute 'nmap ' . g:paredit_leader.' d[(,S' execute 'nmap ' . g:paredit_leader.' d])%,S' call RepeatableNNoRemap(g:paredit_leader . 'I', ':call PareditRaise()') - if &ft =~ '.*\(clojure\|scheme\|racket\).*' + if &ft =~ '.*\(clojure\|scheme\|racket\).*' || g:paredit_full_balancing inoremap [ PareditInsertOpening('[',']') inoremap ] =(pumvisible() ? "\C-Y>" : ""):let save_ve=&ve:set ve=all:call PareditInsertClosing('[',']'):let &ve=save_ve inoremap { PareditInsertOpening('{','}')