为什么在此Vimscript中未设置'<和'>?

时间:2018-07-21 21:28:15

标签: vim

我正试图编写一个vimscript来转换C ++声明,例如:

virtual int funct(int aaa=one, float b=two);

定义:

int SomeClass::funct(int aaa, float b) {
}

..然后添加代码。我希望它能与运动和视觉模式选择一起使用,因此我从vim帮助中获取了if语句,然后将我的东西放进去。

nmap <silent> <F4> :set opfunc=TestMe<CR>g@
vmap <silent> <F4> :<C-U>call TestMe(visualmode(), 1)<CR>

function! TestMe(type, ...)
  " I grabbed the below from the help, but removed 
  " the 'y' at the end of each exe commands.

  let sel_save = &selection
  let &selection = "inclusive"
  let reg_save = @@

  if a:0  " Invoked from Visual mode, use gv command.
    silent exe "normal! gv"
  elseif a:type == 'line'
    silent exe "normal! '[V']"
  else
    silent exe "normal! `[v`]"
  endif

  " My code below:

  " gets rid of virtual keyword
  exec "'<,'>s/\\(\\s*\\)virtual\\s\\+/\\1/ge" 

  " gets rid of optional parameter assignments
  exec "'<,'>s/\\s*=\\s*[a-zA-Z_0-9\:]\\+//ge" 

  " adds <contents of A register>:: in front of function name
  exec "'<,'>g/\\s*[^\\s]*\\s*(.*)\\s*;\\s*$/norm! ==%%b\"aPa::" 

  " replaces ; with {} and newlines
  exec "'<,'>s/\\s*;\\s*$/ {\r\t}\r/g" 

  " The following is from vim help again:
  let &selection = sel_save
  let @@ = reg_save
endfunction

这是我要在其上进行测试的代码:

        int funct1();
        virtual int funct2(int a, float b);
        virtual int funct3(int aaa=one, float b=two);
        virtual Blah::Type funct3(int aaa=one, Another::Type b=two);
        Blah::Type funct3(int aaa=one, Another::Type b=two) = 0;

我认为将会发生的是,if语句将使用适当的v命令来选择相关文本,然后运行我的exec命令。然后,这些exec语句将使用'<,'>将这些命令限制为所选部分。但是,我第一次执行该命令时会说“'<未设置标记”。或者其他的东西。此后,它似乎作用于随机线上(可能基于先前的视觉选择)。

但是,当我手动以视觉方式选择代码并在:提示符下手动执行每一行时,它的行为与我期望的一样。我想念什么?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您需要在函数之后添加CGDirectDisplayID sid = ((NSNumber *)[win.screen.deviceDescription objectForKey:@"NSScreenNumber"]).unsignedIntegerValue; CFArrayRef ms = CGDisplayCopyAllDisplayModes(sid, NULL); CFIndex n = CFArrayGetCount(ms); NSSize ns; for(int i = 0; i < n; ++i){ CGDisplayModeRef m = (CGDisplayModeRef)CFArrayGetValueAtIndex(ms, i); if(CGDisplayModeGetIOFlags(m) & kDisplayModeNativeFlag){ ns.width = CGDisplayModeGetPixelWidth(m); ns.height = CGDisplayModeGetPixelHeight(m); break; } } CFRelease(ms); 。例如:

range

然后使用function! TestMe(type, ...) range a:firstline

请参见a:lastline