# 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 will paste a rectangular area into the current text file # starting at the current cursor position. The data pasted will be taken from the # file taken from a previous rectangular copy or cut operation, as placed in # the file %TEMP%\_columns.pfe # # 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 yet handle tabs!! use Win32::Editor::PFE; # implicitly also loads Win32::DDE use Win32::Clipboard; 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; } 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(); my $column1 = $pfe->Get('ColumnNumber') - 1; my $line1 = $pfe->Get('LineNumber') - 1; # position of begin of block # Note: top of file: PFE pos (1,1), this script (0,0) # Selecting a 2x2 character block (2 right, 1 down) makes this PFE(3,2), this(2,1) # read file with previously saved rectangular info my $dirname = $ENV{'TEMP'} || $ENV{'TMP'} || "C:"; open (FILENAME, "$dirname/_columns.pfe") # open file for output || message "Can't open file '$dirname/_columns.pfe' ($!)"; my @columns = ; # complete file at once close FILENAME; wait_ready(); # go select block in which text is to be inserted message("Can't position (CaretHome)") if !$pfe->CaretHome(0); wait_ready(); if (!$pfe->CaretDown(scalar(@columns)-1, 1)) { # Oops, have hit end of file wait_ready(); # so act accordingly message("Can't position (CaretDown)") if !$pfe->CaretEndOfFile(1); } wait_ready(); # and include up to end of line message("Can't position (CaretEOL)") if !$pfe->CaretEndOfLine(1); wait_ready(); message "Could not cut" if !$pfe->EditCut(); # place selected text on clipboard wait_ready(); my $text = $clip->Get(); # retrieve selected text if(length($text)) # if non-zero length { my $result = ""; my $nextline; my @lines = split (/\n/, $text); # array of lines captured my $gap = (@columns - @lines); # number of lines short for column for (0..$gap-1) { push @lines, ""; } # add required number of empty lines at end for (@lines) # treat all lines: insert column { s/[\x0A\x0D]//g; # first strip end of line characters $nextline = shift @columns; # get data to be inserted chomp $nextline; if(length($_) < $column1) # short line, padd with spaces { $result .= $_ . (" " x ($column1 - length($_))) . $nextline; } else # long line, insert $nextline { $result .= substr($_, 0, $column1) . $nextline . substr($_, $column1); } $result =~ s/ *$//; # remove trailing spaces $result .= "\x0D\x0A"; # add end of line } chop($result); chop($result); # don't need last end-of-line (CR/LF) $clip->Set($result) if $result; # copy back to clipboard for paste operation 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; $clip->Set($save_clipboard); # restore clipboard # my $temp = <>; # wait for debug purposes. # end of script