Luc Hermitte (talk | contribs) m (Added to Automated_Text_Insertion Category + code reformatted + 3 comments merged) |
Luc Hermitte (talk | contribs) (Now my comments) |
||
Line 56: | Line 56: | ||
vittal at cisco dot com |
vittal at cisco dot com |
||
, May 10, 2006 5:20 |
, May 10, 2006 5:20 |
||
+ | :This function does not need to be that complex: <tt>confirm()</tt> takes a third argument for the default choice. See my next comment. |
||
+ | :--[[User:Luc Hermitte|Luc Hermitte]] 14:42, 20 July 2007 (UTC) |
||
+ | ---- |
||
+ | We can use <tt>:iab</tt> with no problem. The syntax is just slightly different. i.e: |
||
+ | function! s:Ask(abbr,expansion,defprompt) |
||
+ | let answer=confirm("Expand '".a:abbr."'?", "&Yes\n&No", a:defprompt) |
||
+ | " testing against 1 and not 2, I correctly take care of <abort> |
||
+ | return answer==1 ? a:expansion : a:abbr |
||
+ | endfunction |
||
+ | |||
+ | iab for <c-r>=<sid>Ask('for', "for () {\n}", 1)<cr> |
||
+ | |||
+ | NB: this is the kind of technique I propose in {{script|id=50|text=a vim library}}, and that by extension , I use in my {{script|id=336|text=C&C++ ftplugin suite}} -- except none of the abbreviations ask question, they are context sensitive. |
||
+ | |||
+ | --[[User:Luc Hermitte|Luc Hermitte]] 14:42, 20 July 2007 (UTC) |
||
---- |
---- |
||
<!-- parsed by vimtips.py in 0.537500 seconds--> |
<!-- parsed by vimtips.py in 0.537500 seconds--> |
Revision as of 14:42, 20 July 2007
Tip: #650 - Abbreviation that prompts whether to expand it or not
Created: February 4, 2004 7:13 Complexity: basic Author: Yakov Lerner Version: 6.0 Karma: 5/5 Imported from: Tip#650
You can define abbreviation in such a way that it will ask whether to expand it or not. The trick is to define it as insert-mode mapping with special body, not as abbreviation.
Here is how to define it:
function! AskExpand(abbr,expansion) let answer=confirm("Expand '".a:abbr."' [y] ", "&Yes\n&No") if answer==2 exec "normal! a".a:abbr else exec "normal! a".a:expansion endif endfunction imap ABC <esc>:call AskExpand("ABC","...expansion for ABC ...")<cr>a imap XYZ <esc>:call AskExpand("XYZ","...expansion for XYZ ...")<cr>a
Comments
It would be great if you could have the default prompt Yes or No for each abbreviation. It could be added as a parameter to the AskExpand.
eg.
"defprompt: 2 => Yes, 1=> NO function! MyAskExpand(abbr,expansion,defprompt) if a:defprompt==2 let answer=confirm("Expand '".a:abbr."' [y] ", "&Yes\n&No") if answer==2 exec "normal! a".a:abbr else exec "normal! a".a:expansion endif else let answer=confirm("Expand '".a:abbr."' [n] ", "&No\n&Yes") if answer==1 exec "normal! a".a:abbr else exec "normal! a".a:expansion endif endif endfunction "imap XYZ <esc>:call myAskExpand("XYZ","...expansion for XYZ ...")<cr>a imap abc <esc>:call MyAskExpand("abc",'buginf("\n:KV:%s ",__FUNCTION__ );',2)<cr>a "default is YES imap xyz <esc>:call MyAskExpand("xyz",'/* VITTAL_DEBUG */',1)<cr>a "default is NO.
vittal at cisco dot com , May 10, 2006 5:20
- This function does not need to be that complex: confirm() takes a third argument for the default choice. See my next comment.
- --Luc Hermitte 14:42, 20 July 2007 (UTC)
We can use :iab with no problem. The syntax is just slightly different. i.e:
function! s:Ask(abbr,expansion,defprompt) let answer=confirm("Expand '".a:abbr."'?", "&Yes\n&No", a:defprompt) " testing against 1 and not 2, I correctly take care of <abort> return answer==1 ? a:expansion : a:abbr endfunction iab for <c-r>=<sid>Ask('for', "for () {\n}", 1)<cr>
NB: this is the kind of technique I propose in a vim library, and that by extension , I use in my C&C++ ftplugin suite -- except none of the abbreviations ask question, they are context sensitive.
--Luc Hermitte 14:42, 20 July 2007 (UTC)