Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hooks to allow disabling of other plugins or behaviour when multi-... #2

Merged
merged 1 commit into from
Nov 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions autoload/multiedit.vim
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
" *multiedit.txt* Multi-editing for Vim
" *multiedit.txt* Multi-editing for Vim

" If Multiple_cursors_after function has been defined in ~/.vimrc, call it
func! multiedit#triggerEndEvents()
if exists('*Multiple_cursors_after')
exe "call Multiple_cursors_after()"
endif
endfunc

" If Multiple_cursors_after function has been defined in ~/.vimrc, call it
func! multiedit#triggerStartEvents()
if exists('*Multiple_cursors_before')
exe "call Multiple_cursors_before()"
endif
endfunc

" addRegion() {{
func! multiedit#addRegion()
Expand All @@ -12,8 +26,8 @@ func! multiedit#addRegion()
let endcol = col('.')+1

" add selection to list
let sel = {
\ 'line': line,
let sel = {
\ 'line': line,
\ 'col': startcol,
\ 'len': endcol-startcol,
\ 'suffix_length': col('$')-endcol
Expand Down Expand Up @@ -93,12 +107,14 @@ endfunc

" start() {{
func! multiedit#start(bang, ...)
if !exists("b:regions")
if !exists("b:regions")
if g:multiedit_auto_restore == 0 || !multiedit#again()
return
endif
endif

call multiedit#triggerStartEvents()

let lastcol = b:first_region.col + b:first_region.len

" If bang exists OR the first region is a marker, then clear it before
Expand Down Expand Up @@ -133,15 +149,15 @@ func! multiedit#start(bang, ...)
else
startinsert
endif

" Where the magic happens
augroup multiedit
au!
" Update the highlights as you edit
au CursorMovedI * call multiedit#update(0)

" Once you leave INSERT, apply changes and delete this augroup
au InsertLeave * call multiedit#update(1) | call s:maps(0) | au! multiedit
au InsertLeave * call multiedit#update(1) | call s:maps(0) | multiedit#triggerEndEvents() | au! multiedit

if g:multiedit_auto_reset == 1
" Clear all regions once you exit insert mode
Expand Down Expand Up @@ -180,7 +196,7 @@ func! multiedit#clear(...)
if !exists("b:regions")
return
endif

" The region to delete might have been provided as an argument.
if a:0 == 1 && type(a:1) == 4
let sel = a:1
Expand Down Expand Up @@ -235,7 +251,7 @@ func! multiedit#update(change_mode)
" Clear highlights so we can make changes
syn clear MultieditRegions
syn clear MultieditFirstRegion

" Prepare the new, altered line
let linetext = getline(b:first_region.line)
let lineendlen = (len(linetext) - b:first_region.suffix_length)
Expand All @@ -256,7 +272,7 @@ func! multiedit#update(change_mode)
for region in regions
if a:change_mode

let region.col += s:offset
let region.col += s:offset
if region.line != b:first_region.line || region.col != b:first_region.col
" Get the old line
let oldline = getline(region.line)
Expand All @@ -269,7 +285,7 @@ func! multiedit#update(change_mode)
let suffix = oldline[(region.col+region.len-1):]

" Update the line
call setline(region.line, prefix.newtext.suffix)
call setline(region.line, prefix.newtext.suffix)
endif

if col >= b:first_region.col
Expand All @@ -283,7 +299,7 @@ func! multiedit#update(change_mode)

" ...move the highlight offset of regions after it
if region.col >= b:first_region.col
let region.col += s:offset
let region.col += s:offset
let s:offset = s:offset + len(newtext) - b:first_region.len
endif

Expand Down Expand Up @@ -316,7 +332,7 @@ func! multiedit#update(change_mode)

" Remeasure the strlen
let b:first_region.suffix_length = col([b:first_region.line, '$']) - b:first_region.col - b:first_region.len

" Restore cursor location
call cursor(b:first_region.line, b:first_region.col + cursor_col)

Expand Down Expand Up @@ -415,7 +431,7 @@ func! s:isOverlapping(selA, selB)
" Check for overlapping
let selAend = a:selA.col + (a:selA.len - 1)
let selBend = a:selB.col + (a:selB.len - 1)
return a:selA.col == a:selB.col || selAend == selBend
return a:selA.col == a:selB.col || selAend == selBend
\ || a:selA.col == selBend || selAend == a:selB.col
\ || (a:selA.col > a:selB.col && a:selA.col < selBend)
\ || (selAend < selBend && selAend > a:selB.col)
Expand Down
19 changes: 19 additions & 0 deletions doc/multiedit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ The highlight regions can be customized, these are the defaults:
hi default link MultieditRegions Search
hi default link MultieditFirstRegion IncSearch
<
If other plugins cause odd behaviour during multiple region editing, two
function scan be used to disable/re-enable this behaviour at apprpriate
times. An example showing how to disable NeoComplete during multiple
region editing is below:

function! Multiple_cursors_before()
exe 'NeoCompleteLock'
echo 'Disabled autocomplete'
endfunction

function! Multiple_cursors_after()
exe 'NeoCompleteUnlock'
echo 'Enabled autocomplete'
endfunction

==============================================================================
KNOWN ISSUES *multiedit-issues*
Expand All @@ -117,6 +131,11 @@ Thank you!
==============================================================================
CHANGELOG *multiedit-changelog*

2014-11-13 [2.0.3]
- New: added conditional calls to Multiple_cursors_after/Multiple_cursors_before
functions defined in your ~/.vimrc. These functions allow disabling of
NeoComplete while editing multiple regions, for example. Implemenation copied
from terryma/vim-multiple-cursors issue #51
2013-03-28 [2.0.2]
- Fix: when the region goes crazy when the text is at the start of the line (when
len == 0 it would suddenly expand to the length of the line).
Expand Down
10 changes: 5 additions & 5 deletions plugin/multiedit.vim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
" *multiedit.txt* Multi-editing for Vim
"
" *multiedit.txt* Multi-editing for Vim
"
" Version: 2.0.2
" Author: Henrik Lissner <henrik at lissner.net>
" License: MIT license
" License: MIT license
"
" Inspired by https://github.com/felixr/vim-multiedit, this plugin hopes to
" fill that multi-cursor-shaped gap in your heart that Sublime Text 2 left you
Expand Down Expand Up @@ -58,7 +58,7 @@ if g:multiedit_no_mappings != 1
nmap <leader>mA $:MultieditAddMark a<CR>
nmap <leader>mI ^:MultieditAddMark i<CR>
" Make the current selection/word an edit region
vmap <leader>m :MultieditAddRegion<CR>
vmap <leader>m :MultieditAddRegion<CR>
nmap <leader>mm viw:MultieditAddRegion<CR>
" Restore the regions from a previous edit session
nmap <leader>mu :MultieditRestore<CR>
Expand All @@ -73,6 +73,6 @@ if g:multiedit_no_mappings != 1
nmap <silent> <leader>md :MultieditClear<CR>
" Unset all regions
nmap <silent> <leader>mr :MultieditReset<CR>
endif
endif

" vim: set foldmarker={{,}} foldlevel=0 foldmethod=marker