[PREV - AFTER_COORDS]    [TOP]
NEXT

WARTS

                         The interesting stuff here is commented out:

                          o  $delta_sum to convert a pass to
                             the "after" frame of reference.

                          o  The call to "revise_locations" to
 Some actual,                try to get all the substitution
 warts-and-all               passes on the same page.
 code:
                                               GETTING_ONE_FRAME

sub do_finds_and_reps {
  my $text_ref      = shift;
  my $find_replaces = shift; # aref of aref: a series of pairs

  my $opts = shift;
  my $LIVE_DANGEROUSLY = $opts->{ LIVE_DANGEROUSLY };

  my $text_copy;
  unless( $LIVE_DANGEROUSLY ) {
    $text_copy = ${ $text_ref };
  }

  my @change_metadata;
  eval {
    for ( my $pass = 0; $pass <= $#{ $find_replaces }; $pass++ ) {
      my ($find_pat, $replace) = @{ $find_replaces->[ $pass ] };
      # my $delta_sum = 0; # running total of deltas for the pass
      my @pass; # change_metadata for this pass
      ${ $text_ref } =~
        s{$find_pat}
         {
           my $new = eval "return qq{$replace}";
           my $l1 = length( $& );
           my $l2 = length( $new );
           my $delta = $l2 - $l1;
           # pos points at the *start* of the match (inside of a s///eg)
           # Note: char numbering fixed at the start of the s///ge run
           my $p = pos( ${ $text_ref } ) + 1; # + $delta_sum;
           my $beg = $p;
           my $end = $p + $l2; # the end *after* the substitution

           # preserving some context
           my $post = substr( $',  0, 10 );  # Note: no BOF/EOF errors
           my $pre  = substr( $`, -10 );

           push @pass, {
                         pass  => $pass,
                         beg   => $beg,
                         end   => $end,   # endpoint *after* change
                         delta => $delta,
                         orig  => $&,
                         rep   => $new,
                         pre   => $pre,
                         post  => $post,
                       };

           # $delta_sum += $delta;
           $new
         }ge;
      push @change_metadata, \@pass;
    }
  };
  if ($@) {
    # Send error message to STDOUT so that it won't mess up test output.
    # (and anyway, the elisp call shell-command-to-string merges in STDERR)
    #
    # The elisp function rep-run-perl-substitutions uses prefix "Problem".
    # to spot error messages
    print "Problem: $@\n";
    # roll-back
    @change_metadata = ();
    unless( $LIVE_DANGEROUSLY ) {
      ${ $text_ref } = $text_copy;
    }
  }

#  revise_locations( \@change_metadata );

#  $change_metadata[0]->{ pre } = 'gabbagabbaHEY';

  return \@change_metadata; # array of array of hrefs (keys: beg, end, delta, orig, etc)
}
-------- [NEXT - GETTING_ONE_FRAME]