Monday, April 2, 2012

Re: maps in "if" statement can not work.

Hi,

stardiviner wrote:
> => On [2012-03-28 07:20:26 +0200]:
> Jürgen Krämer Said:
>>
>> stardiviner wrote:
>>> I found a tips on wikia, it can autocapitalize first character in sentence.
>>> Bellowing is the code in my vimrc.
>>> But I found that those mappings in "if" statement can not work.
>>> Is there anyone know why ?
>>> (The original code does not have the "if" statement. I add it by myself.)
>>>
>>> "Inputing '/' cancels capitalizing without delay
>>> function! CapNextKey(prev)
>>> redraw
>>> let input = nr2char(getchar())
>>> let input = (input == '/' ? "\e" : input)
>>> if input=~'[.?!\r[:blank:]]' "punctuations, return, spaces
>>> exe 'normal! i' . input . "\<Right>"
>>> return CapNextKey(input)
>>> elseif input=="\e"
>>> return "\<del>"
>>> elseif a:prev=~'[\r[:blank:]]'
>>> return toupper(input) . "\<del>"
>>> else
>>> return input . "\<del>"
>>> endif
>>> endfunction
>>>
>>> function! InsertEnterCheck()
>>> let trunc = getline(".")[0:col(".")-2]
>>> if col(".")==1
>>> return CapNextKey("\r")
>>> elseif trunc=~'[?!.]\s*$\|^\s*$' "ie, 'text.[t]ext'
>>> return CapNextKey(trunc[-1:-1])
>>> else
>>> return "\<Del>"
>>> endif
>>> endfunction
>>>
>>> " FIXME those maps can not run in "if" statement.
>>> if &filetype == "markdown" || &filetype == "mail"
>>> inoremap <silent> . ._<Left><C-R>=CapNextKey(".")<CR>
>>> inoremap <silent> ? ?_<Left><C-R>=CapNextKey("?")<CR>
>>> inoremap <silent> ! !_<Left><C-R>=CapNextKey("!")<CR>
>>> inoremap <silent> <CR> <CR>_<Left><C-R>=CapNextKey("\r")<CR>
>>> nnoremap <silent> o o_<Left><C-R>=CapNextKey("\r")<CR>
>>> nnoremap <silent> O O_<Left><C-R>=CapNextKey("\r")<CR>
>>> nnoremap <silent> a a_<Left><C-R>=InsertEnterCheck()<CR>
>>> nnoremap <silent> A A_<Left><C-R>=InsertEnterCheck()<CR>
>>> nnoremap <silent> i i_<Left><C-R>=InsertEnterCheck()<CR>
>>> nnoremap <silent> I I_<Left><C-R>=InsertEnterCheck()<CR>
>>> endif
>>
>> putting this in your .vimrc is too early. .vimrc is sourced before file
>> type detection takes place. At the &filetype is checked in your code it
>> is thus still empty.
>>
>> You might consider to put your code in a separate file and source it
>> from your markdown and mail filetype plugins. Or leave the function
>> definitions in .vimrc, but put only the part the if and endif in your
>> filetype plugins.
>
> Put them in other file like ftplugin/{markdown,mail}.vim ???

yes, something like that. Another possibility would be to use the
autoloading of functions. To do this you have to put your function in a
script file under the autoload directory and prefix them with the file
name and a hash, e.g. in ~/.vim/autoload/stardiviner.vim you put:

"Inputing '/' cancels capitalizing without delay
function! stardiviner#CapNextKey(prev)
redraw
let input = nr2char(getchar())
let input = (input == '/' ? "\e" : input)
if input=~'[.?!\r[:blank:]]' "punctuations, return, spaces
exe 'normal! i' . input . "\<Right>"
return CapNextKey(input)
elseif input=="\e"
return "\<del>"
elseif a:prev=~'[\r[:blank:]]'
return toupper(input) . "\<del>"
else
return input . "\<del>"
endif
endfunction

function! stardiviner#InsertEnterCheck()
let trunc = getline(".")[0:col(".")-2]
if col(".")==1
return CapNextKey("\r")
elseif trunc=~'[?!.]\s*$\|^\s*$' "ie, 'text.[t]ext'
return CapNextKey(trunc[-1:-1])
else
return "\<Del>"
endif
endfunction

Also wrap your mappings in a function and put them inside this file (and
probably restrict them to the current buffer):

function! stardiviner#SetupMaps()
if &filetype == "markdown" || &filetype == "mail"
inoremap <buffer> <silent> . ._<Left><C-R>=stardiviner#CapNextKey(".")<CR>
inoremap <buffer> <silent> ? ?_<Left><C-R>=stardiviner#CapNextKey("?")<CR>
inoremap <buffer> <silent> ! !_<Left><C-R>=stardiviner#CapNextKey("!")<CR>
inoremap <buffer> <silent> <CR> <CR>_<Left><C-R>=stardiviner#CapNextKey("\r")<CR>
nnoremap <buffer> <silent> o o_<Left><C-R>=stardiviner#CapNextKey("\r")<CR>
nnoremap <buffer> <silent> O O_<Left><C-R>=stardiviner#CapNextKey("\r")<CR>
nnoremap <buffer> <silent> a a_<Left><C-R>=stardiviner#InsertEnterCheck()<CR>
nnoremap <buffer> <silent> A A_<Left><C-R>=stardiviner#InsertEnterCheck()<CR>
nnoremap <buffer> <silent> i i_<Left><C-R>=stardiviner#InsertEnterCheck()<CR>
nnoremap <buffer> <silent> I I_<Left><C-R>=stardiviner#InsertEnterCheck()<CR>
endif
endfunction

Now edit your ~/.vim/ftplugin/{markdown,mail}.vim files and insert the
line

call stardiviner#SetupMaps()

Note that all function calls have been prefixed with "stardiviner#" --
the name of the script file in the autoload directory.

The "if"/"endif" in stardiviner#SetupMaps() is not necessary if you only
ever call this function from markdown or mail filetype plugins. I left
it there in case you want to build a larger collection of functions and
mappings and put them in this autoload script.

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment