Reply to topic  [ 5 posts ] 
Improve "Cleaning up punctuation"? 
Author Message

Joined: 2007-10-25 11:41:30
Posts: 17
Hi, I really like the "Cleaning up punctuation.nwm" macro. Could it be possible to add a couple lines to also clean up spacing errors with parentheses? Like having a space before but not after an opening parentheses, and a space after but before a closing parentheses?
In other words, turn something like “I ( really )like the( completely),new version of Nisus Writer Pro!” into:
“I (really) like the (completely), new version of Nisus Writer Pro!”


2007-10-25 12:07:59
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Post 
Adding these lines to the macro should do the trick:
Code:
# clean spaces between parenthesis/braces/brackets and adjacent content
Replace All ‘([({\[]) +’, ‘\1’, ‘E’
Replace All ‘(?<=\S)([({\[])’, ‘ \1’, ‘E’
Replace All ‘([)}\]])(?=\S)’, ‘\1 ’, ‘E’
Replace All ‘ +([)}\]])’, ‘\1’, ‘E’

Cheers!


2007-10-25 12:43:20
Profile WWW

Joined: 2006-12-14 09:09:51
Posts: 42
I also use this "Cleaning up punctuation" macro a lot, and have made several adjustments and additions too, but the limiting thing is that I cannot make this macro work properly on selected text only, using 'aEs' as options? Therefore I still use the old perl macro "Common Regexp Processing", but that macro can ruin styling...


2007-10-26 04:55:40
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Post 
The problem is that after each Replace command the replaced text is selected. Currently there is no way to preserve the selection when a replacement is made, but we have it filed as a feature we need to add.

The best you can manage right now is to manually save and restore the selection using the Selection Location/Length commands, but this would not account for changes in text length as the replacements proceed. Depending on the replacement, you might be able to calculate the new selection length yourself, eg:

Code:
$selLoc = Selection Location
$selLen = Selection Length
$count = Replace All "red", "double", "as"
While $count > 0
    $selLen += 3
    $count -= 1
End
Set Selection $selLoc, $selLen

Not convenient, but for now I think it's the only possibility.


2007-10-26 12:46:46
Profile WWW

Joined: 2006-12-14 09:09:51
Posts: 42
A more convenient 'workaround' for me -- so far -- has been to combine perl in a macro like this:
Code:
Find and Replace '[\t ]+$', '', 'aE'  #Remove trailing white space
Find and Replace '  +', ' ', 'aE' #Replace multiple spaces with one

Begin Perl
#Nisus Macro Block
#source front selection
#destination front selection
#End Nisus Macro Block
use utf8;
binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");

while (my $line = <STDIN>) {
   $line =~ s/^[\t ]+//g; #Remove leading white space
   $line =~ s/\t\t+/\t/g; #Replace multiple tabs with one
   $line =~ s/\s+([,.;:\!\?\)\]}])/\1/g; #Remove white space preceding punctuation
   $line =~ s/([\(\[{])\s+/\1/g; #Remove white space after punctuation
   $line =~ s/(?<=[,:;\!\?])(?=\p{Latin})/ /g; #Add space after punctuation followed by a letter
   $line =~ s/(?<=\S)([\(\[{])/ \1/g; #Add space before punctuation preceded by a letter
   print $line;
}
End

This way I have more control to ensure nothing 'unexpected' happens with my text using this macro.


2007-10-26 23:52:30
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software