The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Opening a text editor (Vi, emacs) from within script

LegionnairedLegionnaired Registered User regular
edited November 2008 in Help / Advice Forum
So, if you've used SVN, you know that it looks at your $EDITOR variable and opens that program whenever you try to make a commit without a memo line.

I'm trying to emulate that sort of behavior, how do I get something to edit a temporary file or a buffer in memory, and automatically return to the script's execution after the text editor exits?

I'm working in Ruby but am reasonably fluent in perl, but even a pointer in the right direction would help, my Google-fu on this subject was weak.

Legionnaired on

Posts

  • LewishamLewisham Registered User regular
    edited November 2008
    So, if you've used SVN, you know that it looks at your $EDITOR variable and opens that program whenever you try to make a commit without a memo line.

    I'm trying to emulate that sort of behavior, how do I get something to edit a temporary file or a buffer in memory, and automatically return to the script's execution after the text editor exits?

    I'm working in Ruby but am reasonably fluent in perl, but even a pointer in the right direction would help, my Google-fu on this subject was weak.

    I just tried this in Perl:
    system("pico -w");
    print "Done";
    

    and it worked fine. You just have to find the right system call for Ruby. Obviously, you call the program in the $EDITOR variable.

    Lewisham on
  • taerictaeric Registered User, ClubPA regular
    edited November 2008
    For the other part of the problem, I believe you have to create the temporary file in whatever language you are using. Then, just open an editor that is editing that file, and check it for modification as soon as the call exists.

    taeric on
  • LegionnairedLegionnaired Registered User regular
    edited November 2008
    taeric wrote: »
    For the other part of the problem, I believe you have to create the temporary file in whatever language you are using. Then, just open an editor that is editing that file, and check it for modification as soon as the call exists.

    Cool, for some reason I thought that system calls behaved like forks, in that the execution of the program would still continue.

    I did the following and it worked swimmingly:
    #!/usr/bin env ruby
    
    require 'tempfile'
    t = Tempfile.new('phishmonger')
    system(ENV['EDITOR'] + ' ' + t.path)
    puts File.open(t.path).readlines
    t.unlink
    

    Please lock this thread for posterity.

    Legionnaired on
  • LewishamLewisham Registered User regular
    edited November 2008
    taeric wrote: »
    For the other part of the problem, I believe you have to create the temporary file in whatever language you are using. Then, just open an editor that is editing that file, and check it for modification as soon as the call exists.

    Cool, for some reason I thought that system calls behaved like forks, in that the execution of the program would still continue.

    Every scripting language I know of has both fork style calls (exec() in Perl) and execute and wait (system() in Perl). Scripting stuff would be hard if you couldn't wait for programs to return a value :)

    I expect, but don't know, that text editors will probably return a truth value depending on whether they saved out the file they were supposed to. You might want to check that, it's easier than checking for the file to have been modified by comparison.

    Lewisham on
Sign In or Register to comment.