drmingdrmer / xptemplate

Code snippets engine for Vim, with snippets library. XPTemplate let you write codes in a smooth, quick and comfortable way.

Home Page:http://www.vim.org/scripts/script.php?script_id=2611

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

One bug report and One confliction

lanjiann opened this issue · comments

I. When let g:xptemplate_brace_complete=1, paste from registers * and + wrong result will show up, for example: if I copy some text like getline('.')[col('.') - 1] with line end invisible symbol, which can be selected if set list, I will get(in two lines)

getline(.'[ol(.') - 1]
')

; if I virtual select an area without line end invisible symbol, I will get getline(.'[ol(.') - 1]'). Both are wrong. BTW, you can see a highlight marker inside the pasted text, which, I think, was generated by auto applying common.bracketcmpl.xpt.vim when pasting.

II. It has been a long time for me not writing C-like code. So I write a vimscript function inside .vimrc to auto add semicolon to the end of non-blank line. My vimscript function is very simple and concentrates on adding semicolon only to the end of lines, because I know the non-line-end-semicolon, such as semicolon in structure(i = 0; i < len; i++) will be autocompleted by xptemplate. My function and map are like this:

function! AutoSemicolon() "{{{
  if getline('.') =~ '^[:space:]*$'
      execute "normal o"
      startinsert
  else
      execute "normal A;"
      execute "normal o"
      startinsert
  endif
endfunction "}}}

autocmd FileType c,cpp,java <buffer><CR> <ESC>:call AutoSemicolon()<CR>

However, I notice they conflict. Before trigger the first xptemplate snippet in a file, my code work well. After triggering a xptemplate snippet, when typing , I will get error messages:

E15: Invalid expression: ^[:call AutoSemicolon()^M
E15: Invalid expression: ^[:call AutoSemicolon()^M

Could you tell me how to solve this? And could you provide this kind of facility in the future version of xptemplate?

Pasting from register * and + behave exactly the same typing very quickly.
Quick typing may break xptemplate while it put text on to screen. For now,
a work around is ":set paste" before pasting and then ":set paste!".

For the second question, I got the same error message with your snippet. It
seems like you missed a "map" command(I added inoremap before then
no error shown up) in the "autocmd" line.
autocmd FileType c,cpp,java inoremap :call AutoSemicolon()

On Tue, Feb 25, 2014 at 8:01 AM, Jian Lan notifications@github.com wrote:

I. When let g:xptemplate_brace_complete=1, paste from registers * and +
wrong result will show up, for example: if I copy some text like getline('.')[col('.')

  • 1] with line end invisible symbol, which can be selected if set list, I
    will get(in two lines)

getline(.'[ol(.') - 1]
')

; if I virtual select an area without line end invisible symbol, I will
get getline(.'[ol(.') - 1]'). Both are wrong. BTW, you can see a
highlight marker inside the pasted text, which, I think, was generated by
auto applying common.bracketcmpl.xpt.vim when pasting.

II. It has been a long time for me not writing C-like code. So I write a
vimscript function inside .vimrc to auto add semicolon to the end of
non-blank line. My vimscript function is very simple and concentrates on
adding semicolon only to the end of lines, because I know the
non-line-end-semicolon, such as semicolon in structure(i = 0; i < len; i++)
will be autocompleted by xptemplate. My function and map are like this:

function! AutoSemicolon() "{{{
if getline('.') =~ '^[:space:]*$'
execute "normal o"
startinsert
else
execute "normal A;"
execute "normal o"
startinsert
endif
endfunction "}}}

autocmd FileType c,cpp,java :call AutoSemicolon()

However, I notice they conflict. Before trigger the first xptemplate
snippet in a file, my code work well. After triggering a xptemplate
snippet, when typing , I will get error messages:

E15: Invalid expression: ^[:call AutoSemicolon()^M
E15: Invalid expression: ^[:call AutoSemicolon()^M

Could you tell me how to solve this? And could you provide this kind of
facility in the future version of xptemplate?


Reply to this email directly or view it on GitHubhttps://github.com//issues/43
.

要了几天饱饭就不记得西北风啥味了

For the second question, it is only a copy and paste problem in my post. In my .vimrc, I'm pretty sure that I have the inoremap. The proof is: before applying xptemplate in *.c file, my snippet works and works well, no error messages. Only after applying one snippet of xptemplate and then apply my snippet again, two error messages show up. If delete that inoremap from that autocmd, I will get different error message like:

Error detected while processing FileType Auto commands for "c":
E488: Trailing characters: <buffer><CR> <ESC>:call AutoSemicolon()<CR>

However, I notice, besides add inoremap, you also deleted from my autocmd, and you said you won't get error messages any more. This inspires me. I test no autocmd, and everything works good, even with xptemplate. You're right. Thank you!

Now I know the problem origin from . However, I need that . Without it all buffers opened after a c program buffer will run this snippet, and add semicolon automatically. It shouldn't work like this.

Hi! I have done more tests. I find something interesting: my snippet interact with xptemplate, and the results are different if applying different xptemplate snippets. If I use

autocmd FileType c,cpp,java inoremap <buffer><CR> <ESC>:call AutoSemicolon()<CR>

Inside xptemplate main snippet, the error messages in my first post will be generated after typing in insert mode. Inside xptemplate struct snippet and enum snippet, type won't generate errors, but semicolon also won't be added automatically. When cursor jump to the last highlight area of these two snippet(just after the close brace }), type will generate error messages as inside xptemplate main snippet.

If I delete , no error messages will be generated any more. However, inside xptemplate main snippet, type will generate semicolon and a wrong indent in the next line. Inside xptemplate struct snippet and enum snippet, type won't auto add semicolons to statements before cursor jump to the last highlight area.

Xptemplate saves some key mapping during applying snippet, add some other
key mappings and restore original key mapping when snippet finished.

is one of them that need to be remapped.

But vim has problems retrieving key mapping information: it does not
distinguish mapping or non- mapping at the time I created
xptemplate. Thus I let xptemplate to guess if it is a mapping. Your
mapping of seems likely to be a mapping then when restoring
mapping, xptemplate made the wrong mapping of it.

I see that newest vim supplies maparg() to retrieve complete information of
mapping. Thus I am gonna make a fix of it in days.
You can have a quick fix by add a space char between "AutoSemicolon" and
"()":

autocmd FileType c,cpp,java inoremap :call
AutoSemicolon ()

This prevent the right-hand part of mapping to be recognised as
mapping.

And thank you a lot for tracing this issue that deep.

On Tue, Feb 25, 2014 at 5:03 PM, Jian Lan notifications@github.com wrote:

Hi! I have done more tests. I find something interesting: my snippet
interact with xptemplate, and the results are different if applying
different xptemplate snippets. If I use

autocmd FileType c,cpp,java inoremap :call AutoSemicolon()

Inside xptemplate main snippet, the error messages in my first post will
be generated after typing in insert mode. Inside xptemplate struct snippet
and enum snippet, type won't generate errors, but semicolon also won't be
added automatically. When cursor jump to the last highlight area of these
two snippet(just after the close brace }), type will generate error
messages as inside xptemplate main snippet.

If I delete , no error messages will be generated any more. However,
inside xptemplate main snippet, type will generate semicolon and a wrong
indent in the next line. Inside xptemplate struct snippet and enum snippet,
type won't auto add semicolons to statements before cursor jump to the last
highlight area.


Reply to this email directly or view it on GitHubhttps://github.com//issues/43#issuecomment-35987710
.

要了几天饱饭就不记得西北风啥味了

I just pushed branch dist and branch master to github that should fix your
problem in vim newer than 7.4

Old version of vim still has the problem.

On Thu, Feb 27, 2014 at 2:00 AM, dr-dr xp drdr.xp@gmail.com wrote:

Xptemplate saves some key mapping during applying snippet, add some other
key mappings and restore original key mapping when snippet finished.

is one of them that need to be remapped.

But vim has problems retrieving key mapping information: it does not
distinguish mapping or non- mapping at the time I created
xptemplate. Thus I let xptemplate to guess if it is a mapping. Your
mapping of seems likely to be a mapping then when restoring
mapping, xptemplate made the wrong mapping of it.

I see that newest vim supplies maparg() to retrieve complete information
of mapping. Thus I am gonna make a fix of it in days.
You can have a quick fix by add a space char between "AutoSemicolon" and
"()":

autocmd FileType c,cpp,java inoremap :call AutoSemicolon ()

This prevent the right-hand part of mapping to be recognised as
mapping.

And thank you a lot for tracing this issue that deep.

On Tue, Feb 25, 2014 at 5:03 PM, Jian Lan notifications@github.comwrote:

Hi! I have done more tests. I find something interesting: my snippet
interact with xptemplate, and the results are different if applying
different xptemplate snippets. If I use

autocmd FileType c,cpp,java inoremap :call AutoSemicolon()

Inside xptemplate main snippet, the error messages in my first post will
be generated after typing in insert mode. Inside xptemplate struct snippet
and enum snippet, type won't generate errors, but semicolon also won't be
added automatically. When cursor jump to the last highlight area of these
two snippet(just after the close brace }), type will generate error
messages as inside xptemplate main snippet.

If I delete , no error messages will be generated any more. However,
inside xptemplate main snippet, type will generate semicolon and a wrong
indent in the next line. Inside xptemplate struct snippet and enum snippet,
type won't auto add semicolons to statements before cursor jump to the last
highlight area.


Reply to this email directly or view it on GitHubhttps://github.com//issues/43#issuecomment-35987710
.

要了几天饱饭就不记得西北风啥味了

要了几天饱饭就不记得西北风啥味了

Thank you! My problem has been solved after this update in my vim 7.4.

However, I have another question: my inoremap works well inside main snippet, while it doesn't work inside struct and enum snippets. You said "Xptemplate saves some key mapping during applying snippet". I think this is the reason. It works in some snippets contexts, and it doesn't work in some other snippets contexts. I just want to know how can I detect what kind of snippet contexts my cursor is in, and then I can make my inoremap work well in struct snippets. I also want to upgrade my AutoSemicolon() to AutoPunctuation() which can add "," automatically when in the enum snippet context.

The snippet I posted before also had bugs. If ignore the case of "," of enum by now, after debugging, it should be like this:

function! AutoSemicolon() "{{{
  if getline('.') !~ '^[:space:]*$'
      execute "normal A;"
  endif
  execute "normal o"
endfunction "}}}

autocmd FileType c,cpp inoremap <buffer><CR> <ESC>:call AutoSemicolon()<CR>S

The S at the end of this autocmd makes sure after mapping, there is a right indent in the new line.

echo b:xptemplateData.renderContext.snipObject.name

This way you can get the snippet name you are filling in to.

echo b:xptemplateData.renderContext.phase

This indicates where it is in process or finished. "fillin" means in the
mid of applying some snippet. "uninit" or "finished" means being not
applying any snippet.

I hope this would help.

On Thu, Mar 6, 2014 at 6:08 AM, Jian Lan notifications@github.com wrote:

Thank you! My problem has been solved after this update in my vim 7.4.

However, I have another question: my inoremap works well inside main
snippet, while it doesn't work inside struct and enum snippets. You said
"Xptemplate saves some key mapping during applying snippet". I think this
is the reason. It works in some snippets contexts, and it doesn't work in
some other snippets contexts. I just want to know how can I detect what
kind of snippet contexts my cursor is in, and then I can make my
inoremap work well in struct snippets. I also want to upgrade my
AutoSemicolon() to AutoPunctuation() which can add "," automatically when
in the enum snippet context.

The snippet I posted before also had bugs. If ignore the case of "," of
enum by now, after debugging, it should be like this:

function! AutoSemicolon() "{{{
if getline('.') !~ '^[:space:]*$'
execute "normal A;"
endif
execute "normal o"
endfunction "}}}

autocmd FileType c,cpp inoremap :call AutoSemicolon()S

The S at the end of this autocmd makes sure after mapping, there is a
right indent in the new line.


Reply to this email directly or view it on GitHubhttps://github.com//issues/43#issuecomment-36800486
.

要了几天饱饭就不记得西北风啥味了

Yes, they can help. Thank you!

After testing I know when b:xptemplateData.renderContext.phase is "fillin" phase, my inoremap of doesn't work. It seems that I can't map when in "fillin" phase. Could you tell me how to get around of it(I tired to help myself and read the source code of xptemplate. Since I don't have experience of reading so much code, it's very hard for me)?

Xpt remap cr before entering fillin phase. I am afraid that there is no way to get around.

But I might add an callback for end user to do something before and after Xptemplate cr. like:
let g:xptemplate_key_hook_before_cr='insertsomething()'

What about this?

发自我的 iPhone

在 2014年3月8日,2:11,Jian Lan notifications@github.com 写道:

Yes, they can help. Thank you!

After testing I know when b:xptemplateData.renderContext.phase is "fillin" phase, my inoremap of doesn't work. It seems that I can't map when in "fillin" phase. Could you tell me how to get around of it(I tired to help myself and read the source code of xptemplate. Since I don't have experience of reading so much code, it's very hard for me)?


Reply to this email directly or view it on GitHub.

Could you explain how does it work. I don't quite understand what does that mean.

I've pushed this patch to github.

With the latest master or dist, the following line in .vimrc appends "abc"
every time pressed during applying snippet:

let g:xptemplate_hook_before_cr = 'abc'

And this appends current time every time pressed:

fun! s:OutputDate()
return strftime('%c')
endfunction
inoremap do_my_work OutputDate()
let g:xptemplate_hook_before_cr = 'do_my_work'

g:xptemplate_hook_before_cr does not accept .

And if you want to use mapping with g:xptemplate_hook_before_cr ,
you need a in-the-middle mapping like it does in the second example.

I hope this feature would help:)

On Tue, Mar 11, 2014 at 7:44 AM, Jian Lan notifications@github.com wrote:

Could you explain how does it work. I don't quite understand what does
that mean.


Reply to this email directly or view it on GitHubhttps://github.com//issues/43#issuecomment-37248618
.

要了几天饱饭就不记得西北风啥味了

Thank you!

I'm trying to play with it. It helps, but my snippet now doesn't work perfect. I still need to do some debug for my snippet.

I'm also trying another way. I think, with the functions provided in you codes, I can try to make struct,
enum, and union work like this:
for example, when triggering the snippet of struct, it will show

struct `name^ 
{
        `data_type^ `member^;
        `...^
};

Semicolons autocomplete for every item in side struct, and press to generate another item with two parts of "data_type" and "member" and a semicolon at the end. If this finally works, since all the items and semicolons can be autocompleted, I can actually get around of the remap problem in most of the cases when I don't occasionally type the inside block by hand. I'm still working on it.

XPT struct " tips

struct `a^ {

`data_type^ `member^;

more...`

{{^ data_type^member^;

more...`

^`}}^};

I supposed this is want you want.

"more..." defines an repetition. The placeholder "more..." also includes 4
indent space and line break. Thus when it is removed, these space will be
removed too.

{{ and }} defines body of repetition which includes another "more..." to
trigger next repetition.

On Thu, Mar 20, 2014 at 5:53 AM, Jian Lan notifications@github.com wrote:

Thank you!

I'm trying to play with it. It helps, but my snippet now doesn't work
perfect. I still need to do some debug for my snippet.

I'm also trying another way. I think, with the functions provided in you
codes, I can try to make struct,
enum, and union work like this:
for example, when triggering the snippet of struct, it will show

struct name^ { data_type^ member^; ...^
};

Semicolons autocomplete for every item in side struct, and press to
generate another item with two parts of "data_type" and "member" and a
semicolon at the end. If this finally works, since all the items and
semicolons can be autocompleted, I can actually get around of the remap
problem in most of the cases when I don't occasionally type the inside
block by hand. I'm still working on it.


Reply to this email directly or view it on GitHubhttps://github.com//issues/43#issuecomment-38111926
.

要了几天饱饭就不记得西北风啥味了

Yes, I think this is what I want.
Thank you for all your reply for all my questions!
I think this issue can be closed now.

My pleasure.:D

On Mon, Mar 24, 2014 at 11:05 AM, Jian Lan notifications@github.com wrote:

Yes, I think this is what I want.
Thank you for all your reply for all my questions!
I think this issue can be closed now.


Reply to this email directly or view it on GitHubhttps://github.com//issues/43#issuecomment-38409018
.

要了几天饱饭就不记得西北风啥味了