# Perl script not to be run as stand_alone program, but in the context of PFE. # From PFE define extra command (Options/Preferences/Execute Menu) like # Perl c:\Perl\Progs\Pfe\EvalPFE.pfe # PFE will then call this script using Perl, and communicating via DDE links # # This particular script replace all tabs based on 4 spaces per tab within the # current selection. Also, any trailing white space is removed from the lines. # # Script written by C.M. Moerman, April 2001 (c) # http://home.hccnet.nl/Kees.Moerman/perl_pfe.html mailto:moerman@iaehv.nl # # PFE by Alan Phillips can be found on: http://www.lancs.ac.uk/people/cpaap/pfe/ # Win32::Editor::PFE by Jenda Krynicky can be found on http://jenda.krynicky.cz/ # # Note: this script does not handle leading.trailing empty lines correctly (split) # Also, select complete lines. use Win32::Editor::PFE; # implicitly also loads Win32::DDE use Win32::Clipboard; use Text::Tabs; # needed for tab expansion use strict; # strict error checking my $clip = Win32::Clipboard(); # get clipboard object my $save_clipboard = $clip->Get(); # save contents for later my $pfe = new Win32::Editor::PFE; # get pfe object message("Can't connect to PFE") if !defined $pfe; $pfe->Connect(); # get a connection to PFE sub message # print message, wait for key, and die { print "$_[0]\nScript halted\nPress key to continue....."; my $temp = <>; $clip->Set($save_clipboard); # restore clipboard exit; # die...... } sub wait_ready # wait until link becomes available { for (0..5) # with timeout at 6 seconds { return if ($pfe->Get('Status') eq 'Ready'); sleep(1); } message "PFE does not respond" if ($pfe->Get('Status') ne 'Ready'); } wait_ready(); message "Could not cut" if !$pfe->EditCut(); # place selected text on clipboard wait_ready(); my $text = $clip->Get(); # retrieve selected text # print "Got text '$text'\n"; if(length($text)) # if non-zero length { $text =~ s/\r//g; # to UNIX format (\n only) my @lines = split (/\n/, $text); # array of lines captured for (@lines) { s/\s*$//; } # remove trailing spaces on the fly $tabstop = 4; # tab is 4 spaces my @lines_without_tabs = expand(@lines); my $result = join("\n", @lines_without_tabs); $result =~ s/\n/\r\n/g; # back to DOS format $clip->Set($result) if $result; for (1..10) # retry: might be busy or fail otherwise? { wait_ready(); $result = $pfe->EditPaste(); # check if succeeded last if $result == 1; print "."; # else retry } message "Could not paste" if ($result != 1); } undef $pfe; # free pfe $clip->Set($save_clipboard); # restore clipboard # $text = <>; # debugging only: wait for key to terminate # end of script