I have defined an operator mapping which takes a region of text, then asks for an input string and then aligns the region with Tabular using the input string as an argument. It works nicely.
I have implemented it like this, using vim-operator-user to help defining a new operator:
map \aa <Plug>(operator-align)
call operator#user#define('align', 'Align')
function! Align(motion_wiseness)
let expr = input("align: ")
if len(expr) != 0
execute "'[,']Tabularize /".expr
endif
endfunction
function! Getchar()
let c = getchar()
if c =~ '^\d\+$'
let c = nr2char(c)
endif
return c
endfunction
Then I wondered if I could make it update on the fly while entering the regular expression to align with. The problem with the current approach is that you have to undo and then redo if you are not using the correct expression.
For the interactive attempt, I made this:
map \== <Plug>(operator-align-interactive)
call operator#user#define('align-interactive', 'AlignInteractive')
function! AlignInteractive(motion_wiseness)
let prompt = "Align: "
echon prompt
let expr = ""
let c = Getchar()
" CR has to be checked for separately as it acts as putting the cursor back to zero position
while c != "\<Esc>" && c != "\<CR>"
if c == "\<BS>"
if len(expr) != 0
let expr = expr[0:-2]
echon "\<CR>".substitute(expr, ".", " ", "g")
echon "\<CR>".prompt.expr
endif
else
let expr .= c
echon c
let cmd = "'[,']Tabularize /".expr
execute cmd
endif
let c = Getchar()
endwhile
endfunction
It should work but the aligning is not done before I hit enter, that is after I have finished entering input, effectively meaning that it works the same way as the non-interactive function. I wonder if the problem could be something like the screen / content not being updated during an operator execution, only after.
Any ideas on what the problem could be is appreciated!