Page 1 of 1

Help refining a script to adjust leading and trailing spaces

Posted: 05 Jul 2019, 10:31
by ndiaz
Hello, I have the script below, which cuts the selected text and pastes it at a new location when I use Alt+Left or Alt+Right. What I would like to add to it is the ability to properly adjust leading and trailing spaces when the text is pasted so that I don't end up with extra or missing spaces. The program I use the script in doesn't adjust spaces natively like Word does.

Code: Select all

;Move selected words left or right
!Left::
!Right::
if(!x){
    send ^x
    hotkey,Alt up,paste,on
    x:=1
}
send % "^{" . subStr(a_thisHotkey,2) . "}"
return

paste:
hotkey,Alt up,paste,off
x:=0
send ^v
return
Thank you in advance for your help!

Re: Help refining a script to adjust leading and trailing spaces

Posted: 05 Jul 2019, 12:52
by rommmcek
May be you should take a look here (Padding with spaces) and here (To pad an integer with zeros or spaces...).

Re: Help refining a script to adjust leading and trailing spaces

Posted: 05 Jul 2019, 13:42
by jeeswg
To remove leading/trailing spaces you can use Trim:
Trim() / LTrim() / RTrim() - Syntax & Usage | AutoHotkey
https://www.autohotkey.com/docs/commands/Trim.htm

Adding leading/trailing spaces, to be consistent with nearby lines, may be more complicated, because you'd need info about nearby lines.

Re: Help refining a script to adjust leading and trailing spaces

Posted: 06 Jul 2019, 09:48
by rommmcek
After jeeswg's post and more thorough reading try:

Code: Select all

!Left::
!Right::
Send {Left}^+{Right}
; … the rest of your code
Otherwise, you should post before and after (wrong and desired) text!

Edit: If I'm right and you are after switching whole words, you might try:

Code: Select all

Send ^{Right}^{Left}^+{Right}
instead, which doesn't require having (but won't bother) marked word. It will switch the word near caret.