Permalink
Browse files

combine previous separate functions for normal and visual mode into 1…

… single function
  • Loading branch information...
1 parent de27db4 commit b3d05f4e4e567eeb258fcbae1f56ae1c73a5c2f8 @chrisbra committed Dec 1, 2013
Showing with 79 additions and 115 deletions.
  1. +73 −110 autoload/nrrwrgn.vim
  2. +1 −0 doc/NarrowRegion.txt
  3. +5 −5 plugin/NrrwRgn.vim
View
@@ -752,49 +752,92 @@ fun! nrrwrgn#NrrwRgnDoPrepare(...) "{{{1
let &lz = o_lz
endfun
-fun! nrrwrgn#NrrwRgn(...) range "{{{1
+fun! nrrwrgn#NrrwRgn(mode, ...) range "{{{1
+ " a:mode is set when using visual mode
+ if (a:mode)
+ " This beeps, when called from command mode
+ " e.g. by using :NRV, so using :sil!
+ " else exiting visual mode
+ exe "sil! norm! \<ESC>"
+ endif
+ let bang = (a:0 > 0 && !empty(a:1))
let o_lz = &lz
let s:o_s = @/
set lz
+ call <sid>Init()
+ if (a:mode)
+ let s:nrrw_rgn_lines[s:instn].vmode=a:mode
+ endif
+ " Protect the original buffer,
+ " so you won't accidentally modify those lines,
+ " that will later be overwritten
let orig_buf=bufnr('')
- let bang = (a:0 > 0 && !empty(a:1))
+ let _opts = <sid>SaveRestoreRegister([])
- " initialize Variables
- call <sid>Init()
- call <sid>CheckProtected()
- let first = a:firstline
- let last = a:lastline
- " If first line is in a closed fold,
- " include complete fold in Narrowed window
- if first == last && foldclosed(first) != -1
- let first = foldclosed(first)
- let last = foldclosedend(last)
+ call <sid>CheckProtected()
+ if (a:mode)
+ let [ s:nrrw_rgn_lines[s:instn].start,
+ \s:nrrw_rgn_lines[s:instn].end ] = <sid>RetVisRegionPos()
+ norm! gv"ay
+ if len(split(@a, "\n", 1)) !=
+ \ (s:nrrw_rgn_lines[s:instn].end[1] -
+ \ s:nrrw_rgn_lines[s:instn].start[1] + 1)
+ " remove trailing "\n"
+ let @a=substitute(@a, '\n$', '', '')
+ endif
+
+ if a:mode == '' && <sid>CheckRectangularRegion(@a)
+ " Rectangular selection
+ let s:nrrw_rgn_lines[s:instn].blockmode = 1
+ else
+ " Non-Rectangular selection
+ let s:nrrw_rgn_lines[s:instn].blockmode = 0
+ endif
+ else
+ let first = a:firstline
+ let last = a:lastline
+ " If first line is in a closed fold,
+ " include complete fold in Narrowed window
+ if first == last && foldclosed(first) != -1
+ let first = foldclosed(first)
+ let last = foldclosedend(last)
+ endif
+ let s:nrrw_rgn_lines[s:instn].start = [ 0, first, 0, 0 ]
+ let s:nrrw_rgn_lines[s:instn].end = [ 0, last , 0, 0 ]
+ let s:nrrw_rgn_lines[s:instn].orig_buf = orig_buf
+ let a=getline(s:nrrw_rgn_lines[s:instn].start[1],
+ \ s:nrrw_rgn_lines[s:instn].end[1])
endif
- let s:nrrw_rgn_lines[s:instn].start = [ 0, first, 0, 0 ]
- let s:nrrw_rgn_lines[s:instn].end = [ 0, last , 0, 0 ]
- let s:nrrw_rgn_lines[s:instn].orig_buf = orig_buf
- let a=getline(
- \s:nrrw_rgn_lines[s:instn].start[1],
- \s:nrrw_rgn_lines[s:instn].end[1])
call <sid>DeleteMatches(s:instn)
let win=<sid>NrrwRgnWin(bang)
if bang
- let s:nrrw_rgn_lines[s:instn].single = 1
+ let s:nrrw_rgn_lines[s:instn].single = 1
else
- noa wincmd p
- let s:nrrw_rgn_lines[s:instn].winnr = winnr()
- " Set highlighting in original window
- call <sid>AddMatches(<sid>GeneratePattern(
- \s:nrrw_rgn_lines[s:instn].start[1:2],
- \s:nrrw_rgn_lines[s:instn].end[1:2],
- \'V'), s:instn)
+ " Set the highlighting
+ noa wincmd p
+ let s:nrrw_rgn_lines[s:instn].winnr = winnr()
+ " Set highlighting in original window
+ if a:mode
+ call <sid>AddMatches(<sid>GeneratePattern(
+ \s:nrrw_rgn_lines[s:instn].start[1:2],
+ \s:nrrw_rgn_lines[s:instn].end[1:2],
+ \s:nrrw_rgn_lines[s:instn].vmode,
+ \s:nrrw_rgn_lines[s:instn].blockmode),
+ \s:instn)
+ else
+ call <sid>AddMatches(<sid>GeneratePattern(
+ \s:nrrw_rgn_lines[s:instn].start[1:2],
+ \s:nrrw_rgn_lines[s:instn].end[1:2],
+ \'V'), s:instn)
+ endif
" move back to narrowed window
noa wincmd p
endif
let b:orig_buf = orig_buf
- call setline(1, a)
- setl nomod
+ let s:nrrw_rgn_lines[s:instn].orig_buf = orig_buf
+ call setline(1, a:mode ? @a : a)
let b:nrrw_instn = s:instn
+ setl nomod
call <sid>SetupBufLocalCommands()
call <sid>NrrwRgnAuCmd(0)
if has_key(s:nrrw_aucmd, "create")
@@ -803,11 +846,11 @@ fun! nrrwrgn#NrrwRgn(...) range "{{{1
if has_key(s:nrrw_aucmd, "close")
let b:nrrw_aucmd_close = s:nrrw_aucmd["close"]
endif
+ call <sid>SaveRestoreRegister(_opts)
" restore settings
let &lz = o_lz
endfun
-
fun! nrrwrgn#Prepare() "{{{1
let ltime = localtime()
if (!exists("s:nrrw_rgn_last") || s:nrrw_rgn_last + 10 < ltime)
@@ -1028,86 +1071,6 @@ fun! nrrwrgn#WidenRegion(force) "{{{1
endif
endfun
-fun! nrrwrgn#VisualNrrwRgn(mode, ...) "{{{1
- " bang: open the narrowed buffer in the current window and don't open a
- " new split window
- if empty(a:mode)
- " in case, visual mode wasn't entered, visualmode()
- " returns an empty string and in that case, we finish
- " here
- call <sid>WarningMsg("There was no region visually selected!")
- return
- endif
- " This beeps, when called from command mode
- " e.g. by using :NRV, so using :sil!
- " else exiting visual mode
- exe "sil! norm! \<ESC>"
- let bang = (a:0 > 0 && !empty(a:1))
- " stop visualmode
- let o_lz = &lz
- let s:o_s = @/
- set lz
- call <sid>Init()
- let s:nrrw_rgn_lines[s:instn].vmode=a:mode
- " Protect the original buffer,
- " so you won't accidentally modify those lines,
- " that will later be overwritten
- let orig_buf=bufnr('')
- let _opts = <sid>SaveRestoreRegister([])
-
- call <sid>CheckProtected()
- let [ s:nrrw_rgn_lines[s:instn].start,
- \s:nrrw_rgn_lines[s:instn].end ] = <sid>RetVisRegionPos()
- call <sid>DeleteMatches(s:instn)
- norm! gv"ay
- if len(split(@a, "\n", 1)) !=
- \ (s:nrrw_rgn_lines[s:instn].end[1] -
- \ s:nrrw_rgn_lines[s:instn].start[1] + 1)
- " remove trailing "\n"
- let @a=substitute(@a, '\n$', '', '')
- endif
-
- if a:mode == '' && <sid>CheckRectangularRegion(@a)
- " Rectangular selection
- let s:nrrw_rgn_lines[s:instn].blockmode = 1
- else
- " Non-Rectangular selection
- let s:nrrw_rgn_lines[s:instn].blockmode = 0
- endif
- let win=<sid>NrrwRgnWin(bang)
- if bang
- let s:nrrw_rgn_lines[s:instn].single = 1
- else
- " Set the highlighting
- noa wincmd p
- let s:nrrw_rgn_lines[s:instn].winnr = winnr()
- call <sid>AddMatches(<sid>GeneratePattern(
- \s:nrrw_rgn_lines[s:instn].start[1:2],
- \s:nrrw_rgn_lines[s:instn].end[1:2],
- \s:nrrw_rgn_lines[s:instn].vmode,
- \s:nrrw_rgn_lines[s:instn].blockmode),
- \s:instn)
- noa wincmd p
- endif
- let b:orig_buf = orig_buf
- let s:nrrw_rgn_lines[s:instn].orig_buf = orig_buf
- silent put a
- let b:nrrw_instn = s:instn
- silent 0d _
- setl nomod
- call <sid>SetupBufLocalCommands()
- " Setup autocommands
- call <sid>NrrwRgnAuCmd(0)
- " Execute autocommands
- if has_key(s:nrrw_aucmd, "create")
- exe s:nrrw_aucmd["create"]
- endif
- call <sid>SaveRestoreRegister(_opts)
-
- " restore settings
- let &lz = o_lz
-endfun
-
fun! nrrwrgn#UnifiedDiff() "{{{1
let save_winposview=winsaveview()
let orig_win = winnr()
@@ -1201,7 +1164,7 @@ fun! nrrwrgn#LastNrrwRgn(bang) "{{{1
exe "keepj norm!" s:nrrw_rgn_lines['last'][1][1][1]. '|'
endif
" Call VisualNrrwRgn()
- call nrrwrgn#VisualNrrwRgn(visualmode(), bang)
+ call nrrwrgn#NrrwRgn(visualmode(), bang)
endif
endfu
" Debugging options "{{{1
View
@@ -339,6 +339,7 @@ looking at my Amazon whishlist: http://www.amazon.de/wishlist/2BKAHE8J7Z6UW
thanks!), instead include a fix set of option names to set when opening the
narrowed buffer.
- Switching buffers in the original narrowed buffer, may confuse NrrwRgn.
+- Code cleanup (no more separate functions for visual and normal mode)
0.31: Feb 16, 2013 {{{1
- NRM threw some errors (reported by pydave in
View
@@ -38,11 +38,11 @@ com! -bang NarrowWindow :NW
com! -bang NRLast :NRL
" Define the actual Commands "{{{2
-com! -range -bang NR :<line1>, <line2>call nrrwrgn#NrrwRgn(<q-bang>)
+com! -range -bang NR :<line1>, <line2>call nrrwrgn#NrrwRgn(0,<q-bang>)
com! -range NRP :exe ":" . <line1> . ',' . <line2> . 'call nrrwrgn#Prepare()'
-com! -bang -range NRV :call nrrwrgn#VisualNrrwRgn(visualmode(), <q-bang>)
+com! -bang -range NRV :call nrrwrgn#NrrwRgn(visualmode(), <q-bang>)
com! NUD :call nrrwrgn#UnifiedDiff()
-com! -bang NW :exe ":" . line('w0') . ',' . line('w$') . "call nrrwrgn#NrrwRgn(<q-bang>)"
+com! -bang NW :exe ":" . line('w0') . ',' . line('w$') . "call nrrwrgn#NrrwRgn(0,<q-bang>)"
com! -bang NRM :call nrrwrgn#NrrwRgnDoPrepare(<q-bang>)
com! -bang NRL :call nrrwrgn#LastNrrwRgn(<q-bang>)
@@ -59,8 +59,8 @@ endif
if !hasmapto('VisualNrrwRgnBang')
xnoremap <unique> <script> <Plug>NrrwrgnBangDo <sid>VisualNrrwBang
endif
-xnoremap <sid>VisualNrrwRgn :<c-u>call nrrwrgn#VisualNrrwRgn(visualmode(),'')<cr>
-xnoremap <sid>VisualNrrwBang :<c-u>call nrrwrgn#VisualNrrwRgn(visualmode(),'!')<cr>
+xnoremap <sid>VisualNrrwRgn :<c-u>call nrrwrgn#NrrwRgn(visualmode(),'')<cr>
+xnoremap <sid>VisualNrrwBang :<c-u>call nrrwrgn#NrrwRgn(visualmode(),'!')<cr>
" Restore: "{{{1
let &cpo=s:cpo

0 comments on commit b3d05f4

Please sign in to comment.