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

Get help using and writing Nisus Writer Pro macros.
Post Reply
Posts: 16
Joined: 2005-05-02 11:31:06
Location: Lexington, VA

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

Post by economics » 2007-11-14 10:45:06

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

User avatar
Posts: 173
Joined: 2004-11-10 07:31:31

Post by mrennie » 2007-11-14 11:27:27


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.

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

Post by martin » 2007-11-14 14:36:27

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: Select all

$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.

Post Reply