Deleting a word

Get help using and writing Nisus Writer Pro macros.
Post Reply
lawrencegoodman
Posts: 30
Joined: 2007-07-30 12:06:49

Deleting a word

Post by lawrencegoodman » 2008-02-13 10:24:30

I want to write a macro that deletes the word where the cursor is. So the macro would first select the word, then delete it, and then move the cursor one character to the right so if I invoke the macro right away again, the next word is deleted.

This is what I've written, but it doesn't do anything. Any advice:

$selLen = Selection Length
# find the beginning of the current word
Find '\s|\.*', 'rE'
$selStart = Selection Location
Find '\s|\.*', "E"
$selEnd = Selection Location
Begin Perl
# $selLen = $selLen + 1;
$selLen = $selEnd - $selStart;
End
# Select the word and delete it
Set Selection $selStart, $selLen
Delete


Thanks!

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

Post by martin » 2008-02-13 14:39:39

Hi Lawrence- the problem with your macro is that your Find expressions don't do what you want. Try them out yourself to see what their current effects are.

The best solution is to use the "\b" regex escape, which matches word boundaries. So to find the start of a word:

Code: Select all

Find '\\b', 'rE'
Finding the end of the word is a bit more tricky because a forward search for "\b" at this point will match the exact same boundary (the one at the start of the word). There's two options, the first is to advance the selection yourself, so the currently boundary is skipped, eg:

Code: Select all

Find '\\b', 'rE'
$selStart = Selection Location
$selEnd = $selStart
$selEnd += 1
Set Selection Location $selEnd
Find '\\b', 'E'
$selEnd = Selection Location
The other solution is to use look-behind to assure that the word boundary you find is preceded by a word character:

Code: Select all

Find '\\b', 'rE'
$selStart = Selection Location
Find '(?<=\\w)\\b', 'E'
$selEnd = Selection Location

lawrencegoodman
Posts: 30
Joined: 2007-07-30 12:06:49

Post by lawrencegoodman » 2008-02-19 13:07:15

Thanks for that suggestion, but I couldn't quite get it to work. The macro below sort of works, but it isn't able to handle any kind of punctuation, i.e. when it gets to a comma or apostraphe, it doesn't delete the word, just the punctuation or a bit of the word.
Thanks as always for the help.

Code: Select all

$selLen = Selection Length
$selStart = Selection Location
# # # find the end of the current word 
Find '\s|\?|\.|\:|\’|\"|”', 'E'
$selEnd = Selection Location 
$selEnd += 1
$selEnd = Selection Location 
# # find the start of the current word
Find '\\b', 'rE' 
$selStart = Selection Location
Begin Perl 
$selLen = $selEnd - $selStart;
End 
# # # Select the word and delete it 
Set Selection $selStart, $selLen
Delete
$selEnd = Selection Location
$selEnd += 1
Set Selection $selEnd, 0

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

Post by martin » 2008-02-19 14:08:53

The problem is that "\b" matches the boundary between punctuation and whitespace, but your custom find expression can move past it. After your first find completes the reverse search stops when it hits the punctuation attached to the end of the word.

You'll either want to use the same criteria to match boundaries in both of your expressions, or somehow account for the additional content. Eg: as I did in my prior post by moving the caret or using look-behind.

Post Reply