سؤال

I am using a the vim-screen plugin that enable me to write scripts , start an interpreter in the same window and send lines the the interpreter. Problem is that the interpreter do not accept statements written on several lines.

exemple: This will work f:{[x] y:y+1; Z:y+1; :Z; };

But this won't

f:{[x] y:y+1;
    Z:y+1;
    :Z;
 };

How can I write a vim function that I could call to reshape the lines in order to be sent to the interpreter?

EDIT: I had no success in making this function, I would like to create a function that would, from a input like this (that would be visually selected)

F:{[a;b;r]
//ccc1
   aaa1;
aaa2;
  //ccc2
    aaa3;
};

output something like this F:{[a;b;r] aaa1; aaa2; aaa3; }; So I created a bounty

هل كانت مفيدة؟

المحلول

If you want to actually modify the buffer, J / :join do that. If you just want to join the lines that are sent to the interpreter (but keep them split in the buffer), you can retrieve the selected lines with getline(), and then join() them. Here's an example command:

:command! -range Invoke echomsg join(getline(<line1>,<line2>), '')

Edit

Based on that, you can "massage" the List of lines returned by getline(). E.g. to ignore the commented lines:

:command! -range Invoke echomsg join(filter(getline(<line1>,<line2>), 'v:val !~# "^\\s*//"'), '')

Additionally strip leading whitespace (this becomes unwieldy in a single line; better use a function now):

:command! -range Invoke echomsg join(map(filter(getline(<line1>,<line2>), 'v:val !~# "^\\s*//"'), 'substitute(v:val, "^\\s\\+", " ", "g")'), '')

نصائح أخرى

Standard continuation character in vimscript scripts is backslash in the beginning of the next line. So, this

f:{[x] y:y+1;
  \  Z:y+1;
  \  :Z;
 \ };

should work.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top