Jump to content

Trimming active selection


Recommended Posts

In a given field there is a body of text:

Quote

the quick brown fox jumped over the lazy dog.

ignoring any formatting if the active selection was the word fox and the active selection contained a leading and or trailing space.

_fox or fox_ or _fox_ 

I would wish to trim the space just to the word fox and reset the active start and end selection before my script continues.

Right now I have 3 very verbose IF sections in the script one that tests if there is a leading or trailing or both.

 

Link to post
Share on other sites

I don't think there is a truly elegant solution to this. If you can assume that the selection contains at most one leading and one trailing space, then you can use two If [] steps to (a) adjust the selection start if there is a leading space and (b) adjust the selection end if there is a trailing space. If the assumption cannot be made, then you need to loop.

 

Link to post
Share on other sites

On second thought:

Set Variable [ $selectionStart; Value: Get ( ActiveSelectionStart ) ]
Set Variable [ $selection; Value: Middle ( Get ( ActiveFieldContents ) ; $selectionStart ; Get ( ActiveSelectionSize ) ) ] 
Set Variable [ $text; Value: Trim ( $selection ) ]
Set Variable [ $textStart; Value: $selectionStart + Position ( $selection ; $text ; 1 ; 1 ) - 1 ]
Set Selection [ Start Position: $textStart; End Position: $textStart + Length ( $text ) - 1 ]

Note that this assumes you have selected something besides spaces - otherwise it will move your selection to the last character of the preceding word, if one exists, or after the first space if it doesn't.

 

Edited by comment
Link to post
Share on other sites

This is what i knocked together - use the json to set the selection.

Let  ( 
[ 
input = Get(ActiveFieldContents) 
;start = Get(ActiveSelectionStart) 
;size = Get(ActiveSelectionSize) 
;text = Middle ( input ; start ; size )

// 0 ; 1 ; 2 ; 3  for space as none prefix suffix both 
;sp =  Case ( 
Left ( text ; 1 ) ≠ Char(32) and  Right ( text ; 1 ) ≠ Char(32) ; 0 ;  
Left ( text ; 1 ) = Char(32) and  Right ( text ; 1 ) = Char(32) ; 1 ;  
Left ( text ; 1 ) = Char(32) ; 2 ; 
Right( text ; 1 ) = Char(32) ; 3  )

; start = Choose ( sp ; start ; start + 1 ; start + 1 ; start )
; size = Choose ( sp ; size ; size - 2 ; size - 1 ; size - 1 )
; end = start + size

] ; 

JSONSetElement ( "" ; 
[ "sp" ; sp ; JSONNumber]
;[ "start" ; start ; JSONNumber]
;[ "size" ; size ; JSONNumber]
;[ "end" ; end ; JSONNumber] 
;[ "text" ; text ; JSONString] 
;[ "selected" ; Trim ( text ) ; JSONString] 

)

)

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.