Reply to topic  [ 3 posts ] 
switching adjacent letters (I'm a bad typist...) 
Author Message

Joined: 2005-05-02 11:31:06
Posts: 16
Location: Lexington, VA
This is inelegant, my sense is there should be a way to do it more concisely. But the following seems to work consistently and the "if" seems to catch start-of-document failures.

I gave it a name (Reverse Characters.nwm) and assigned a simple and meaningful keystroke to it (⌘R).

$pplace=Selection Location
$pplace -= 1
if $pplace<2
exit
else
Set Selection $pplace, 1
end
:Edit:Cut:Cut
$pplace -= 1
Set Selection Location $pplace
:Edit:Paste:Paste
$pplace += 2
Set Selection Location $pplace

Note that I tried to Find/Replace, but it goes one extra character backwards at the end of a line, which would often be the case in actual use. Maybe someone can figure out an elegant way so that it finds the previous character consistently and doesn't need a lot of checks for failure points?
Here's what didn't work (I tried many variations, it didn't seem robust).

Find and Replace '(.)(.)', '\2\1', 'bE'
Select end


2007-11-14 10:45:06
Profile
User avatar

Joined: 2004-11-10 07:31:31
Posts: 173
Post 
Hi,

there's already a feature built into Cocoa (and therefore available in every Cocoa application, including Nisus Writer Express/Pro) that allows you to do just that. Place your cursor between two characters, then hit Ctrl+t, and the character on the left of your cursor will be switched with the one on the right.


2007-11-14 11:27:27
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Post 
mrennie's method is the most native (and fastest) way to accomplish this. However, its effect is slightly different than that of the macro you've posted. Control + T will transpose the character before the caret with the character after the caret. The macro you've posed transposes the two characters immediately before the caret.

As for a more elegant version of the macro, I would use this:
Code:
$selLoc = Selection Location

# there must be two character behind the caret
If $selLoc <= 2
   Exit
End

# place the 2nd character before the 1st
$selLoc -= 1
Set Selection $selLoc, 1
$savedChar = Read Selection
Delete
$selLoc -= 1
Set Selection $selLoc, 0
Write Selection $savedChar

# restore original selection
$selLoc += 2
Set Selection $selLoc, 0

The key difference is that it does not use the clipboard as a scratch space. This is better because it will be more efficient and also preserve whatever is already on the clipboard.

As for your Find & Replace macro, I'm confused as to why it works sometimes and not other times. I'll look into it.


2007-11-14 14:36:27
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 3 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:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software