Reply to topic  [ 3 posts ] 
Transpose two noncontiguous selections 
Author Message
User avatar

Joined: 2004-12-14 07:54:49
Posts: 156
Location: Düsseldorf Germany
Hi again,

in the old days one could select two noncontiguous textbits and with an easy F/R expression say Find/Replace '\1\2' '\2\1' This doesn't work in NWP. What do I have to do inetead?

Best Matze


2011-06-28 02:06:58
Profile WWW
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
You're right, that in Classic Nisus Writer one could use "\1" to represent the first selection, and so on. Now that NWP uses standard regular expressions, "\1" (a back reference) represents the first capture.

So for example, you could match all adjacent doubled whitespace characters using "(\s)\1" (eg: that matches two spaces next to each other, or two returns next to each other. But does not match a space followed by a return.)

Transposing two multipart selections in NWP is much more involved, but it can still be done using the TextSelection object. Here's one solution:
Code:
# must have document to work on
$doc = Document.active
If ! $doc
   Exit
End

# must have exactly two selections
$selections = $doc.textSelections
If 2 != $selections.count
   Prompt 'Must have exactly two selections to swap.'
   Exit
End
$selection1 = $selections.firstValue
$selection2 = $selections.lastValue

# gather text contents before doing any replacements
$text1 = $selection1.subtext
$text2 = $selection2.subtext

# replace in reverse order so locations don't shift
$selection2.text.replaceInRange($selection2.range, $text1)
$selection1.text.replaceInRange($selection1.range, $text2)

# construct new selections for just replaced content
$newRange1 = Range.new($selection1.location, $text2.length)
$newRange2 = Range.new($selection2.location, $text1.length)
If $selection1.text == $selection2.text
   $delta = $selection1.length - $newRange1.length
   $newRange2.location -= $delta
End

$newSelection1 = TextSelection.new($selection1.text, $newRange1)
$newSelection2 = TextSelection.new($selection2.text, $newRange2)
$newSelections = Array.new($newSelection1, $newSelection2)
$doc.setSelection($newSelections)


2011-06-28 09:37:01
Profile WWW
User avatar

Joined: 2004-12-14 07:54:49
Posts: 156
Location: Düsseldorf Germany
Perfect, Martin, thank you so much! This macro has been a 'daily macro', couldn't live without it, now it's back again.
Best, Matze


2011-06-28 23:38:59
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests


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