Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Attempt to fix the '[ and '] marks

  • Loading branch information...
commit ca3072f927b1549600cc992252b02150adc2392a 1 parent ad7417c
Andrew Radev authored
Showing with 32 additions and 13 deletions.
  1. +32 −13 autoload/whitespaste.vim
45 autoload/whitespaste.vim
View
@@ -55,16 +55,31 @@ function! whitespaste#PasteLinewise(normal_command)
let saved_cursor = getpos('.')
for definition in get(definitions, 'bottom', {})
- if s:HandleDefinition(bottom, definition, pasted_area_end, target_area_end)
+ let [success, delta] = s:HandleDefinition(bottom, definition, pasted_area_end, target_area_end)
+ if success
+ let target_area_end += delta
break
endif
endfor
for definition in get(definitions, 'top', {})
- if s:HandleDefinition(top, definition, target_area_start, pasted_area_start)
+ let [success, delta] = s:HandleDefinition(top, definition, target_area_start, pasted_area_start)
+ if success
+ let pasted_area_start += delta
+ let pasted_area_end += delta
+ let target_area_end += delta
break
endif
endfor
+
+ " Restore [ and ] marks
+ let start_pos = getpos("'[")
+ let start_pos[1] = target_area_start + 1
+ call setpos("'[", start_pos)
+
+ let end_pos = getpos("']")
+ let end_pos[1] = target_area_end - 1
+ call setpos("']", end_pos)
finally
call setpos('.', saved_cursor)
endtry
@@ -79,27 +94,27 @@ function! s:HandleDefinition(actual, definition, start, end)
endif
if has_key(definition, 'target_line') && actual.target_line != definition.target_line
- return 0
+ return [0, 0]
endif
if has_key(definition, 'target_text') && actual.target_text !~ definition.target_text
- return 0
+ return [0, 0]
endif
if has_key(definition, 'pasted_line') && actual.pasted_line != definition.pasted_line
- return 0
+ return [0, 0]
endif
if has_key(definition, 'pasted_text') && actual.pasted_text !~ definition.pasted_text
- return 0
+ return [0, 0]
endif
if has_key(definition, 'compress_blank_lines')
- call whitespaste#CompressBlankLines(start, end, definition.compress_blank_lines)
+ let line_change = whitespaste#CompressBlankLines(start, end, definition.compress_blank_lines)
elseif has_key(definition, 'blank_lines')
- call whitespaste#SetBlankLines(start, end, definition.blank_lines)
+ let line_change = whitespaste#SetBlankLines(start, end, definition.blank_lines)
else
throw "No 'blank_lines' or 'compress_blank_lines' key in definition"
endif
- return 1
+ return [1, line_change]
endfunction
" For now, just works like a normal "paste"
@@ -117,9 +132,9 @@ function! whitespaste#CompressBlankLines(start, end, line_count)
let existing_line_count = (end - start) - 1
if existing_line_count < 0 || existing_line_count <= line_count
- return
+ return 0
else
- call whitespaste#SetBlankLines(start, end, line_count)
+ return whitespaste#SetBlankLines(start, end, line_count)
endif
endfunction
@@ -128,21 +143,25 @@ function! whitespaste#SetBlankLines(start, end, line_count)
let existing_line_count = (end - start) - 1
if existing_line_count < 0
- return
+ return 0
endif
if existing_line_count > 0 && line_count == 0
" delete lines
silent exe (start + 1).','.(end - 1).'delete _'
undojoin
+ return -existing_line_count
elseif existing_line_count > line_count
" remove some lines
let delta = existing_line_count - line_count
silent exe (start + 1).','.(start + delta).'delete _'
undojoin
+ return -delta
elseif existing_line_count < line_count
" add some lines
- call append(start, repeat([''], line_count - existing_line_count))
+ let delta = line_count - existing_line_count
+ call append(start, repeat([''], delta))
undojoin
+ return delta
endif
endfunction
Please sign in to comment.
Something went wrong with that request. Please try again.