switching adjacent letters (I'm a bad typist...)
Page 1 of 1

Author:  economics [ 2007-11-14 10:45:06 ]
Post subject:  switching adjacent letters (I'm a bad typist...)

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
Set Selection $pplace, 1
$pplace -= 1
Set Selection Location $pplace
$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

Author:  mrennie [ 2007-11-14 11:27:27 ]
Post subject: 


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.

Author:  martin [ 2007-11-14 14:36:27 ]
Post subject: 

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:
$selLoc = Selection Location

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

# place the 2nd character before the 1st
$selLoc -= 1
Set Selection $selLoc, 1
$savedChar = Read Selection
$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.

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group