nisus.com
https://www.nisus.com/forum/

Macro to Traspose Characters
https://www.nisus.com/forum/viewtopic.php?f=17&t=2485
Page 1 of 1

Author:  lawrencegoodman [ 2007-08-07 13:13:19 ]
Post subject:  Macro to Traspose Characters

I want to write a macro where it changes the order of two letters. So if the cursor is between the 't' and 'h' in the word 'the' and I trigger the macro, the word will change to 'hte'.

I have written the following macro:

Code:
$locationofcursor = selection location
Select Start
Set Selection $locationofcursor, 1
Cut
set selection location -1
Set Selection $locationofcursor , 1
Paste
Select End


I get an error because evidently you can't use negative numbers. So how do I move backwards one space? I am trying to do this without perl if I can so I can keep it simple and understand what I am doing.

Thanks for your help.

Author:  martin [ 2007-08-07 13:52:13 ]
Post subject:  Re: Macro to Traspose Characters

lawrencegoodman wrote:
I get an error because evidently you can't use negative numbers.

Correct, the Set Selection Location command takes an absolute character location/offset in the document. Thus if your document contains (N) characters then only (1) through (N + 1) are valid locations.

As macros currently do not have any commands that perform arithmetic you'll have to resort to Perl to calculate the character location you need. However, I think you'll find the code understandable:

Code:
$locationofcursor = selection location
Select Start
Set Selection $locationofcursor, 1
Cut
Begin Perl
     $locationofcursor = $locationofcursor - 1;
End
Set Selection $locationofcursor , 0
Paste
Select End

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/