Page 1 of 1

Improve "Cleaning up punctuation"?

Posted: 2007-10-25 12:07:59
by vitaka
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!”

Posted: 2007-10-25 12:43:20
by martin
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’

Cleaning up punctuation

Posted: 2007-10-26 04:55:40
by Dirk_Barends
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...

Posted: 2007-10-26 12:46:46
by martin
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
Set Selection $selLoc, $selLen
Not convenient, but for now I think it's the only possibility.


Posted: 2007-10-26 23:52:30
by Dirk_Barends
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;
This way I have more control to ensure nothing 'unexpected' happens with my text using this macro.