How to modify Quote Selection macro to strip white space

Get help using and writing Nisus Writer Pro macros.
Post Reply
mil
Posts: 8
Joined: 2009-11-03 21:38:38

How to modify Quote Selection macro to strip white space

Post by mil » 2009-11-03 21:55:43

Hi, I'm trying out NW Pro demo. I'd like to modify the Quote Selection macro to strip out trailing space in selection before enclosing it with quotation marks. I'm new to regex. Did a quick google and found: Search for ^[ \t]+ and Replace with nothing to remove trailing white space. It does not work in PowerFind Pro. And even if it did, I'm not sure how to implement it into the macro. Can anybody help.

In case your wondering, I'm trying to prevent this from happening:

The hapless writer said, "Damn, I was lazy selecting my text again. "

I never had this problem in Nisus Classic, because it was smarter about selecting text; it didn't include the space in the selection.

Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: How to modify Quote Selection macro to strip white space

Post by Kino » 2009-11-04 02:25:47

mil wrote:Did a quick google and found: Search for ^[ \t]+ and Replace with nothing to remove trailing white space. It does not work in PowerFind Pro.
You seem to have picked up a wrong one. It does not work because It does not remove trailing white space but leading white space. To achieve your aim, just add the following line before the Find and Replace command in the macro.

Code: Select all

Find and Replace '[\t\x20]+$', '', 'sSE'
If you want to remove not only trailing white space but also leading white space, use this one instead.

Code: Select all

Replace All '^[\t\x20]+|[\t\x20]+$', '', 'sSE'

mil
Posts: 8
Joined: 2009-11-03 21:38:38

Re: How to modify Quote Selection macro to strip white space

Post by mil » 2009-11-04 13:18:06

Hi Kino, thank you for your generosity and quick response. The macro worked as expected when the selected text was at the end of a paragraph. It did not work so well otherwise. These are my results:

The appreciative writer said, “Thank you for the excellent macro.”
“But it does not work in this situation, ”he said.
Nor does it “work ”in the middle of a sentence.
He noted, “It only works at the end of paragraph. ”As can be seen here.

I also discovered another problem. Find/Replace does not insert a space in between the replaced text and the text following it. I think this is just a failing of the “word processor” that Nisus chose to base their program on. Here is another example of what I’m talking about. If I copy a word (with no following space) and then select another word (this time selecting the following space) and paste, I get the new word butting up against the next word without a space in between. However, if I cut the selected word before pasting the new word, it works as expected with a space being inserted between words. I believe that Nisus classic handled this better, as Word does.

So I’m wondering whether I’m going to have to use a Perl macro to do this. I did something similar for Word, except it had a trim function built in. As I see it, I need something like this:

mystring = selected text
trimmedstring = right trimmed mystring (using s/\s+$//; somehow)
quotestring = U+201C +trimmedstring + U+201D
cut selected text
paste quotestring

Any suggestions on how to quickest learn the syntax and insert it into a Nisus macro? The Macro Language Reference needs more examples, and I’m still wading through the Nisus User Guide to learn the regex expressions. Are there better or more verbose primers? Thanks in advance for any more help.

mil

mil
Posts: 8
Joined: 2009-11-03 21:38:38

Re: How to modify Quote Selection macro to strip white space

Post by mil » 2009-11-04 18:51:58

Perhaps I am going at this from the wrong angle. I have been trying to find a way to trim the white space, because that is what I had to do to work around Word's limitations. What dawned on me is that I have no idea what the limitations of Nisus/regular expressions are. Perhaps there is a more elegant solution.

What I want is to be able to use the Quote Selection macro, and have it enclose the text correctly even if I accidentally select the space after the last word/punctuation.

Is there a way with regular expressions to determine if the selected text ends with whitespace, and if so to shorten the selection, so that it no longer includes the whitespace. This would be the ideal solution.

Thanks,
mil

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

Re: How to modify Quote Selection macro to strip white space

Post by martin » 2009-11-04 19:02:24

mil wrote:Hi Kino, thank you for your generosity and quick response. The macro worked as expected when the selected text was at the end of a paragraph. It did not work so well otherwise.
That's because the "^" and "$" anchors match the start/end of paragraphs and ignore the start/end of the selection. It would be nice if there were anchors for the start/end of the selection, but alas there are none.
If I copy a word (with no following space) and then select another word (this time selecting the following space) and paste, I get the new word butting up against the next word without a space in between.
When moving words around try using a double-click to select these words. That will inform Nisus Writer that you're interested in maintaining spacing after the text you are pasting.
I did something similar for Word, except it had a trim function built in. As I see it, I need something like this:

mystring = selected text
trimmedstring = right trimmed mystring (using s/\s+$//; somehow)
quotestring = U+201C +trimmedstring + U+201D
cut selected text
paste quotestring
A close direct translation of that idea:

Code: Select all

$text = Read Selection
$text.findAndReplace('\s+$', '', 'E')
$quotedText = Text.newWithCodepoint(0x201C)
$quotedText &= $text
$quotedText &= Text.newWithCodepoint(0x201D)
Write Selection $quotedText
But there's many ways to accomplish the goal. Here's another that does the change inline (in the document text):

Code: Select all

$sel = TextSelection.active
Select End
Type Text "\u201D"
$startRange = Range.new($sel.location, 0)
TextSelection.setActiveRange($startRange)
Type Text "\u201C"
Find and Replace '\s*(?=\u201D)', '', 'E'

mil
Posts: 8
Joined: 2009-11-03 21:38:38

Re: How to modify Quote Selection macro to strip white space

Post by mil » 2009-11-04 21:59:00

Martin, thanks for your help. As I said, I'm very new at this (yesterday & today) so it will take me a while to figure out what you've given me and the difference between the two. If you could point me to any resources (in addition to the Macro Reference Guide & the User Guide) to help get up to speed on the macro language & regular expression syntax that would be much appreciated. I am starting at ground zero, so examples are extremely helpful.

As far as the Quotation macro, I've realized that trimming the whitespace from the selection will not work, because Find/Replace does not add a space back in for you (as Word did) and as cut and paste does. So the quotation marks will butt up against the next word. Is Nisus able to determine if a selection ends in whitespace and then shorten the selection so that it does not include the whitespace (but does not delete it from the document)? Or is perl able to do this?

And to better explain, I can’t imagine the whitespace ever being more than a single space. My problem is that when selecting text to enclose in quotation marks, it is very easy to overshoot the terminal punctuation mark and include the following space. When writing dialog, there is almost always terminal punctuation, otherwise double-clicking to select the word as you suggested would work. I realize that more careful text selection obviates the need for this macro, and zooming way in makes this easier, but you really would not believe how easy it is to under or over-select when you’re talking about a comma.

Thanks again,
Mil

Another solution would be a Transpose Last 2 Characters macro, to fix the problem after the fact. Is this trivial?

Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: How to modify Quote Selection macro to strip white space

Post by Kino » 2009-11-04 22:04:55

mil wrote:Is there a way with regular expressions to determine if the selected text ends with whitespace, and if so to shorten the selection, so that it no longer includes the whitespace.
This command will do the job.

Code: Select all

Find '\S.*\S', 'Es'
# \S  no-whitespace character
# .   any character
# *   zero or more times
I think you can get the expected result by adding it before the Find and Replace command in the Quote Selectio macro (and removing the command I suggested in my earlier posting).

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

Re: How to modify Quote Selection macro to strip white space

Post by martin » 2009-11-05 03:30:27

mil wrote:If you could point me to any resources (in addition to the Macro Reference Guide & the User Guide) to help get up to speed on the macro language & regular expression syntax that would be much appreciated. I am starting at ground zero, so examples are extremely helpful.
Aside from the macro reference, you might check out the Nisus macro repository, or Kino's macro website. There's also plenty of resources for regular expressions on the internet, regular-expressions.info is one that's quite good.
As far as the Quotation macro, I've realized that trimming the whitespace from the selection will not work
You should find Kino's expression will work quite well. The full new quote selection macro is then just:

Code: Select all

Find '\S.*\S', 'Es' # exclude whitespace at the head and tail of the selection
Find and Replace '.+', '“\0”', 'sEU' # quote the selection

mil
Posts: 8
Joined: 2009-11-03 21:38:38

Re: How to modify Quote Selection macro to strip white space

Post by mil » 2009-11-05 07:57:53

Thanks. You guys are awesome. I especially appreciate the comments. I can't wait to try these out this afternoon.

mil
Posts: 8
Joined: 2009-11-03 21:38:38

Re: How to modify Quote Selection macro to strip white space

Post by mil » 2009-11-05 15:33:37

Macro works perfectly. Thanks Kino and Martin. You have saved me a lot of time trying to figure this out for myself and have taught me a little in the process. Thanks again for the comments. I mentioned before that I was a Nisus user pre OSX. I didn’t realize how much I missed the Nisus community. Too bad you can’t list that as a feature on the box.

I do have one last question, though. Sometimes when I copy and paste something into the macro file, the output is in Courier. Do you have to write your macros in the normal style, or can you not have more than one font in the macro? Or am I doing something else wrong? It’s working fine now. It was just a little weird.

Thanks again,
Mil

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

Re: How to modify Quote Selection macro to strip white space

Post by martin » 2009-11-05 17:15:04

mil wrote:Thanks again for the comments. I mentioned before that I was a Nisus user pre OSX. I didn’t realize how much I missed the Nisus community. Too bad you can’t list that as a feature on the box.
Very happy to help :)
Sometimes when I copy and paste something into the macro file, the output is in Courier. Do you have to write your macros in the normal style, or can you not have more than one font in the macro?
You can have any number of fonts (or any other formatting) in a macro file. The macro itself won't care what font you have applied, unless your macro is doing some attribute-sensitive text insertion or searching. Personally I prefer to keep my entire macro in Courier when possible, so I will always paste text into a macro using "Paste Text Only".

Post Reply