Reply to topic  [ 4 posts ] 
Deleting a word 
Author Message

Joined: 2007-07-30 12:06:49
Posts: 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!


2008-02-13 10:24:30
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Post 
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:
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:
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:
Find '\\b', 'rE'
$selStart = Selection Location
Find '(?<=\\w)\\b', 'E'
$selEnd = Selection Location


2008-02-13 14:39:39
Profile WWW

Joined: 2007-07-30 12:06:49
Posts: 30
Post 
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:
$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


2008-02-19 13:07:15
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Post 
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.


2008-02-19 14:08:53
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


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:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software