vi input mode in command line Matlab? vi input mode in command line Matlab? ruby ruby

vi input mode in command line Matlab?


Yes, that should be easy enough. It's just a special case of the general "open a process and bind to its stdin and stdout" problem, and that's not difficult.

A bit of Google searching finds that IO.popen() is the right piece of Ruby for that, and there's a little more detail in the replies here: http://groups.google.com/group/ruby-talk-google/browse_thread/thread/0bbf0a3f1668184c. Hopefully, that's enough to get you started!

Update: Looks like you're almost there with your wrapper. What you need to get finished is recognize when Matlab is asking for input, and only ask the user for input then. I'd suggest trying this pseudocode:

while input_line = Readline.readline('>> ', true)  io.puts input_line  while ((output_line = io.gets) != '>> ')  // Loop until we get a prompt.    puts io.gets  endend

That's not quite right, as you need to do the inner loop once before you ask for the first input line, but it should give you the idea. You might need to adjust the prompt text that it's looking for, too.

Update 2: Okay, so we also need to account for the fact that there's no EOL after a prompt and so io.gets will hang. Here's a revised version that uses the fact that you can give a blank line to a Matlab prompt and it will just give you another prompt without doing anything. I've rearranged the loop to make things a little clearer, though this means you now have to add logic to figure out when you're done.

while [not done]   // figure this out somehow  io.puts blank_line                        // This will answer the first                                            // prompt we get.  while ((output_line = io.gets) != '>> ')  // Loop until we get a prompt.    puts io.gets                            // This won't hang, since the  end                                       // prompt will get the blank                                            // line we just sent.  input_line = Readline.readline('>> ', true)  // Get something, feed it  io.puts input_line                           // to the next prompt.  output_line = io.gets   // This will eat the prompt that corresponds to                          // the line we just fed in.end


You could have used rlwrap in a straight-forward manner.

rlwrap is a wrapper that uses the GNU readline library to allow the editingof keyboard input for any other command. 

http://utopia.knoware.nl/~hlub/rlwrap/#rlwrap

Unfortunately it will block context-sensitive tab completion in MATLAB, which is useful on its own.


Actually, you might be better off writing this in C - then you can call the matlab engine directly. This basically allows you to write your own front-end to matlab, if you are so inclined, using the GNU Readline library.