AutoHotkey Community

It is currently May 27th, 2012, 11:27 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 27 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: August 4th, 2005, 4:16 pm 
Offline

Joined: August 4th, 2005, 3:56 pm
Posts: 41
Hi. I am new here and new to AHK. I've read and read here and looked at some of the documentation, but the truth is, I'm lost as to where to even begin with the code for this. So I thought I'd ask if you can help me! :D

Here's my example of what I want a code to fix, in any text processing application.

1. 5 x 2. 7 x 11. 0

I want to do find and replace to fix that to:

1.5 x 2.7 x 11.0

And, although I used two spaces in the above example between numbers, I'd like it to work if there were only one space between numbers, too. Naturally, it should only be in the number scenario and never remove the spaces between a period and the beginning of a new sentence.

I use a program called Instant Text which is an awesome program and comes with an automatic spacing feature, which means every time you type a period, it will jump ahead one or two spaces, depending on how you set it, for one or for two. I love that. But it causes a problem when typing numbers with decimal points, as above. There are already various ways to get around it. However, if this could be done in any application using AHK, and especially an AHK script turned into an .exe, well, it would be great.

Thanks for any help! Awesome site and awesome program.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2005, 5:10 pm 
Offline

Joined: December 16th, 2004, 11:45 pm
Posts: 89
Are you saying you want to write a script that auto detects and corrects extra spaces in between the decimal and number while you are typing?

_________________
Image
"the things we touch have no permanence. my master would say: there is nothing we can hold onto in this world.. only by letting go can we truly possess what is real..."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2005, 5:18 pm 
Thanks you for your reply, Pasukun. Actually, I was thinking along the lines of Find and Replace to fix it after the fact, but frankly, your suggestion would be even better. I just didn't know if that was a possibility.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2005, 5:30 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
It is, please have a look into the manual. Look specially for hotstrings.

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2005, 5:46 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
The following script sends 2 spaces after each dot typed. When a digit key is pressed immediately after this, the extra spaces are erased. You don’t need Instant Text, AHK provides all the text replacements with this kind of hotkeys or hotstrings.
Code:
Loop
{
   Input key, I L1 V
   SetEnv afterSpace,,
}
$.::
   Send .{Space 2}
   afterSpace = 1
Return
~0::
~1::
~2::
~3::
~4::
~5::
~6::
~7::
~8::
~9::IfEqual afterSpace,1, Send {Left}{BS 2}{Right}
Edit: fixed erasing after non-digit keys.


Last edited by Laszlo on August 4th, 2005, 6:42 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2005, 6:03 pm 
Offline

Joined: August 4th, 2005, 3:56 pm
Posts: 41
Laszlo! Thank you, thank you! That works perfectly, it's fantastic! You have no idea how great that is! I am very appreciative.

I will look more into hotstrings, toralf. It is already something some cohorts of mine have gotten into and something others plan to really investigate.

Thanks again to all. *Huge thumbs up sign* :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2005, 6:30 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
I updated the script, such that the space erasure won’t fire if between a dot and a digit some other character is typed. The always running loop makes note of this. These quickies are not fully tested, so be careful and prepared to tweak it.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2005, 6:40 pm 
Offline

Joined: August 4th, 2005, 3:56 pm
Posts: 41
Yes, I just saw the edit, have edited mine and am testing it. No worries if I have to tweak, I'm so grateful. So far, though, it's working just great!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2005, 7:17 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Here is another version, which also allows you to type ellipsis (a few dots in a row). The second dot erases the spaces after the first one, and does not add new ones, so a...z or 2...5 look correct.
Code:
Dots = 0
Loop
{
   Input key, I L1 V
   Dots = 0
}
$.::
   IfEqual Dots,0, Send .{Space 2}
   Else IfEqual Dots,1, Send {BS 2}.
   Else Send .
   Dots++
Return
~0::
~1::
~2::
~3::
~4::
~5::
~6::
~7::
~8::
~9::IfEqual Dots,1, Send {Left}{BS 2}{Right}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2005, 7:41 pm 
Offline

Joined: August 4th, 2005, 3:56 pm
Posts: 41
Quick on the draw, you are! Thanks! I had noticed that, as in board postings, I do..... all the time! :D It happens never or practically never in my work, so I wasn't going to sweat that, but, thank you very much! Makes it even better!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2005, 8:37 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
To beat the dead horse still more... I added a hotkey for Enter, which removes the inserted spaces from the end of the line. Also, if some special characters are pressed after a dot, like PgUp or BackSpace, the space removal is cancelled (the Input command catches it in the Loop), because at the new cursor location it makes no sense. If you want to cancel also at other special keys, like CapsLock or F3, just add their names in braces to the end of the input line. (It will grow pretty long, but it is OK.)
Code:
Dots = 0
Loop
{
   Input key, I L1 M V,{TAB}{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}
   Dots = 0
}
$.::
   IfEqual Dots,0, Send .{Space 2}
   Else IfEqual Dots,1, Send {BS 2}.
   Else Send .
   Dots++
Return
$Enter::
   IfEqual Dots,1, Send {BS 2}
   Send {Enter}
   Dots = 0
Return
~0::
~1::
~2::
~3::
~4::
~5::
~6::
~7::
~8::
~9::IfEqual Dots,1, Send {Left}{BS 2}{Right}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2005, 9:43 pm 
Offline

Joined: August 4th, 2005, 3:56 pm
Posts: 41
Very much obliged yet still, for the improvement tweaks, Laszlo!


Report this post
Top
 Profile  
Reply with quote  
PostPosted: August 8th, 2005, 4:17 am 
Offline

Joined: August 8th, 2005, 3:44 am
Posts: 13
I just spent a lot of time working with someone else's idea of how to program this working entirely with Input, only to be defeated by bizarre deletion errors when backspacing and making corrections to words typed before the period and when arrowing up and making corrections/insertions in the line above.

So far, your code doesn't seem to have that problem (the Hotkey approach seems to give a much greater ability to clear registers following non-alphanumeric navigation keys), and looks like it could be easily expanded to include any and all other punctuation we want autospacing for.

However, there are a few additional things we need to handle that you haven't covered here, and which I can't figure out how to code.

First, the dot between numbers should only lose its spacing if there is a number before as well as a number after the dot. With your code, if someone types a period after a word and then types a number at the start of the next sentence, the spacing will disappear. The other programmer did this:

Code:
;number space suppression
  if  (oldkey=":")
  {
   IfInstring, numbers,%key%
   {
   send, {Left 3}{Delete 2}{Right}
   }

;optional space suppression
   if  (oldkey=":" and key=":" )
   {
   send, {BS 5}
   }
  }


I can't figure out how to get the second condition in there on your script, which is to say I can't figure out the syntax for a 2-condition If statement with a hotkey.

Like Harrie, I am brand new to AHk, have never done any programming, and am finding the help files a little overwhelming.

If you could suggest a way to code the check for the number preceding the dot, that would take care of one of the three remaining things we would need this code to handle.

The second problem, which is the second part of the code above, is that sometimes someone will forget and hit the spacebar even though autospacing is already there. This is also an issue of tracking the key before the current key, so that if the previous key was a dot and the current key is a space, the script will backspace twice to remove the autospacing and just leave the manual space(s).

And before you give that horse its decent burial, can you possibly think of a way we could check for the previous character in the document to see if it is a space, when it may not have been inserted from the keyboard (characters often come through the IT expander). If it is, we would want the script to automatically backspace once if the next character typed is a punctuation mark. (Expansions from our typing shortcuts are inserted with an autospace at the end to precede the next word, but the space needs to disappear if the next thing you type is not another word but a punctuation mark, parenthesis, etc.) Could that check possibly be handled through a Left, ^c, Right, and check on clipboard contents before each key is entered? Or can you think of any other more efficient way to do it?


Report this post
Top
 Profile  
Reply with quote  
PostPosted: August 8th, 2005, 7:00 am 
Offline

Joined: August 8th, 2005, 3:44 am
Posts: 13
While we're waiting to see if Laszlo is willing to do CPR on that horse, here is my small contribution to the thread. I may not be able to write code, but I'm pretty good at doing mindless scut work. :lol: This is Laszlo's last script version expanded to include all the punctuation marks. Maybe it will at least save someone a little typing time.

Code:
;Laszlo-AutospaceCorrect3.ahk

;Spacing is automatically added after punctuation marks
;If a period or colon is typed inside a number string,
;the autospacing is automatically suppressed.
;To manually suppress autospacing, type the punctuation mark twice.
;Manual suppression must be used for a comma inside a number string.

Comma = 0
Semic = 0
Colon = 0
Period = 0
Questn = 0
Exclam = 0

Loop
{
   Input key,I L1 M V, {TAB}{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}
{PgUp}{PgDn}
   
   Comma = 0
   Semic = 0
   Colon = 0
   Period = 0
   Questn = 0
   Exclam = 0

}

$,::
   IfEqual Comma,0, Send ,,{Space 1}
   Else IfEqual Comma,1, Send {BS 1} 
   Else Send ,,
   Comma++
Return

$;::
   IfEqual Semic,0, Send `;{Space 1}
   Else IfEqual Semic,1, Send {BS 1} 
   Else Send `;
   Semic++
Return

$+;::
   IfEqual Colon,0, Send +;{Space 2}
   Else IfEqual Colon,1, Send {BS 2} 
   Else Send +;
   Colon++
Return

$.::
   IfEqual Period,0, Send .{Space 2}
   Else IfEqual Period,1, Send {BS 2} 
   Else Send .
   Period++
Return

$+/::
   IfEqual Questn,0, Send +/{Space 2}
   Else IfEqual Questn,1, Send {BS 2} 
   Else Send +/
   Questn++
Return

$+1::
   IfEqual Exclam,0, Send +1{Space 2}
   Else IfEqual Exclam,1, Send {BS 2} 
   Else Send +1
   Exclam++
Return

$Enter::
   IfEqual Comma,1, Send {BS 1}
   Comma = 0
   IfEqual Semic,1, Send {BS 1}
   Semic = 0
   IfEqual Colon,1, Send {BS 2}
   Colon = 0
   IfEqual Period,1, Send {BS 2}
   Period = 0
   IfEqual Questn,1, Send {BS 2}
   Questn = 0
   IfEqual Exclam,1, Send {BS 2}
   Exclam = 0
   Send {Enter}
Return

$Tab::
   IfEqual Comma,1, Send {BS 1}
   Comma = 0
   IfEqual Semic,1, Send {BS 1}
   Semic = 0
   IfEqual Colon,1, Send {BS 2}
   Colon = 0
   IfEqual Period,1, Send {BS 2}
   Period = 0
   IfEqual Questn,1, Send {BS 2}
   Questn = 0
   IfEqual Exclam,1, Send {BS 2}
   Exclam = 0
   Send {Tab}
Return


~0::
~1::
~2::
~3::
~4::
~5::
~6::
~7::
~8::
~9::
  {
   IfEqual Period,1,Send {Left}{BS 2}{Right}
   IfEqual Colon,1, Send {Left}{BS 2}{Right}
  }



Last edited by 14tonks on August 8th, 2005, 7:57 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 8th, 2005, 7:43 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
The input command sets the variable "key" to the last visible character before the hotkeys. We can check if it was a digit when we remove the extra digits.

It is easy to handle manual spaces: just follow the example of Enter: if there was one dot, remove the inserted spaces and add the users' space.

Things get ugly if you want to check the character to the left of the insertion point with Shift-Left, Ctrl-C. We have to save and restore the current clipboard, which already causes complex actions with side-effects in some applications, such as MS Word or Wordperfect. Some other applications allow text remain marked when you type further. In those, Shift-Left would cause unpredictable behavior. Some day AHK will have a function to get the character/word at the cursor, but until then we won't have a universal solution. If the text is in a standard Windows control, ControlGet CurrentLine/CurrentCol would work, but not all the windows are standard.

AHK's hotstrings handle expanding abbreviations better: punctuation could be terminating characters, and so there will be no extra spaces to remove. And you won't need another expander program running. To remove space prior a punctuation mark, if this space is entered through the keyboard driver, modify the hotkey for the Dot and Space. Key is set to "Space" when Space was typed, and the first Dot could check that. However, things get complicated. If Space is followed by two Dots, what shall we do? And what if two Spaces are followed by a Dot? You can set up rules, but probably there will be cases when you want an exception. Artificial intelligence has to be built in.

There are endless possibilities for further improvements. For example, if the active window changes, we can save the last key and the number of dots entered, etc. and restore them when the same window gets activated again. Shorthand handling could be improved, too, like when a small letter follows a dot, one of the spaces could be removed (like above at "etc. and"). I hope you can now tweak the script further to accommodate all of your needs.
Code:
Dots = 0
Loop
{
   Input key, I L1 M V,{TAB}{Esc}{BS}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}
   Dots = 0
}
$.::
   IfEqual Dots,0, {
      IfEqual key,Space, Send {BS}
      Send .{Space 2}
   }
   Else IfEqual Dots,1, Send {BS 2}.
   Else Send .
   Dots++
Return
$Enter::
   IfEqual Dots,1, Send {BS 2}
   Send {Enter}
   Dots = 0
Return
$Space::
   IfEqual Dots,1, Send {BS 2}
   Send {Space}
   key = Space
   Dots = 0
Return
~0::
~1::
~2::
~3::
~4::
~5::
~6::
~7::
~8::
~9::
   IfEqual Dots, 1
      If key is integer
         Send {Left}{BS 2}{Right}
Return


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 27 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 20 guests


You can post new topics in this forum
You can 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:
Powered by phpBB® Forum Software © phpBB Group