Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

editd

by tye (Sage)
on Jul 26, 2002 at 22:29 UTC ( [id://185686]=sourcecode: print w/replies, xml ) Need Help??
Category: Win32 Stuff
Author/Contact Info tye
Description:

Works like pushd and popd but brings up an editor with the stack of directories and lets you edit them.

I have vi.bat that does "start c:\path\to\vim\gvim %*" so I can just type "vi file" at the command prompt and not have it wait for gvim to exit. I also have vim.bat which doesn't include "start" (just "c:\path\to\vim\gvim %*") so I can do SET VISUAL=vim and editd.bat will use vi to let me edit the directory stack. In fact, I call it vid.bat on my computers.

This probably requires WinNT/Win2K.

@rem = '--*-Perl-*--
@pushd .
@pushd > "%TEMP%\editd.txt"
@if defined VISUAL call "%VISUAL%" "%TEMP%\editd.txt"
@if not defined VISUAL notepad "%TEMP%\editd.txt"
@echo - >> "%~f0"
@pushd >> "%~f0"
@popd
@perl -x "%~f0" "%TEMP%\editd.txt"
@if NOT "%OS%" == "Windows_NT" goto endofperl
@if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
@if %errorlevel% == 9009 echo You do not have Perl in your PATH.
@if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
@goto endofperl
@rem ';
#!/usr/bin/perl -w
#line 17
use strict;

main( @ARGV );
exit( 0 );

sub main {
    my( $file )= @_;
    my $start= tell(DATA)
        or  die "Can't tell(DATA): $!";
    open DATA, "+< $0"  or  die "Can't read self ($0): $!\n";
    seek( DATA, $start, 0 )
        or  die "Can't fseek(DATA,$start,0): $!";
    die "Expected :endofperl after __END__ of $0.\n"
        unless  <DATA> =~ /^\s*:endofperl\s*$/i;
    $start= tell(DATA)
        or  die "Can't tell(DATA): $!";
    my @curr;
    while(  <DATA>  ) {
        if(  /^\s*-\s*$/  ) {
            @curr= ();
        } else {
            chomp;
            unshift @curr, $_;
        }
    }
    open USER, "< $file"
        or  die "Can't read $file: $!\n";
    my @user= reverse <USER>;
    chomp @user;
    close USER;
    while(  @curr  &&  @user  &&  $curr[0] eq $user[0]  ) {
        shift @curr;
        shift @user;
    }
    seek( DATA, $start, 0 )
        or  die "Can't fseek(DATA,$start,0): $!";
    for(  @curr  ) {
        print DATA "popd\n";
    }
    for my $dir (  @user  ) {
        print DATA qq{pushd "$dir"\n};
    }
    print DATA "pushd\n";
    truncate DATA, tell(DATA);
}
__END__
:endofperl
Replies are listed 'Best First'.
Re: editd
by jsprat (Curate) on Jul 27, 2002 at 03:28 UTC
    Self-modifying batch files, brings back memories of programming with MS QuickBasic! BTW, this does require NT, since 9x doesn't have pushd or popd.

    OT - A comment on vim - when you type 'gvim' at the command prompt, you don't need to wait for gvim to exit to get your prompt back (since at least version 5.6, could be earlier). 'start gvim' isn't necessary, as control is returned almost immediately.

      Typing path/to/gvim at the command prompt doesn't wait but putting path/to/gvim in a *.BAT file does wait, and that is a good thing too, otherwise this script could not be used with gvim.

              - tye (but my friends call me "Tye")
        Apologies in advance if I'm not reading your reply right...

        Try set VISUAL=gvim (if vim is in your path), then run vid.bat. You can edit your directory stack and popd to your heart's content, and it works just fine.

        Update: I hit submit, and then realized that I _did_ misread your reply. The command processor will wait for gvim to exit when called from vid.bat, then continues when you exit or it would never push the directories you add. I originally thought you meant if you didn't have something like 'vi.bat', it wouldn't work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://185686]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-18 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found