Improve "Cleaning up punctuation"?

Get help using and writing Nisus Writer Pro macros.
Post Reply
vitaka
Posts: 17
Joined: 2007-10-25 11:41:30

Improve "Cleaning up punctuation"?

Post by vitaka » 2007-10-25 12:07:59

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!”

User avatar
martin
Official Nisus Person
Posts: 4368
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Post by martin » 2007-10-25 12:43:20

Adding these lines to the macro should do the trick:

Code: Select all

# 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!

Dirk_Barends
Posts: 42
Joined: 2006-12-14 09:09:51

Cleaning up punctuation

Post by Dirk_Barends » 2007-10-26 04:55:40

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...

User avatar
martin
Official Nisus Person
Posts: 4368
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Post by martin » 2007-10-26 12:46:46

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: Select all

$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.

Dirk_Barends
Posts: 42
Joined: 2006-12-14 09:09:51

workaround

Post by Dirk_Barends » 2007-10-26 23:52:30

A more convenient 'workaround' for me -- so far -- has been to combine perl in a macro like this:

Code: Select all

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.

Post Reply