AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Evaluating arithmetic expressions in documents
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Laszlo



Joined: 14 Feb 2005
Posts: 4002
Location: Pittsburgh

PostPosted: Tue Oct 11, 2005 7:34 pm    Post subject: Evaluating arithmetic expressions in documents Reply with quote

There were several dynamic expression evaluation methods discussed here. Some of them might be of general interest, so I post an application of the simplest one here.

Evaluating arithmetic expressions in documents: Select it, and the Win-Alt-e HotKey puts the result after it. Like, in a week there are 7*24*60 = 10080 minutes. Here " = 10080" was written by the script. (In some editors the insertion point must be at the end of the expression, so select it from left-to-right.)
Code:
#!e::                    ; Evaluates dynamic expression, Paste " = " <Result>
  Send ^c
  FileDelete $temp$.ahk  ; Here will be the temp script written
  FileAppend #NoTrayIcon`nClipBoard:=%ClipBoard%, $temp$.ahk
  RunWait $temp$.ahk     ; Run AHK to execute temp script
  Send {Right} = ^v      ; Paste the result after the expression
Return
If you want, save the current ClipBoard or ClipBoardAll in the beginning and restore it at the end. (With ClipBoardAll you have ugly side effects in MS Word and WordPerfect. Other programs seem to be OK with it.)
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4002
Location: Pittsburgh

PostPosted: Thu Oct 13, 2005 7:36 pm    Post subject: Reply with quote

Here is a somewhat enhanced version. If nothing is selected, the text in the current line is used between the insertion point and the last colon ":" on the left, after removing the colon. (If the expression spans more than one line, we have to manually select it for evaluation.) It speeds up typing, like in "There are 24*7*60 = 10080 minutes in a week". After typing "…are: 24*7*60" press the hotkey, which removes ":" and inserts " = 10080" after the expression. It works in MS Word and Notepad, but some editors copy the current line to the clipboard if nothing is selected. They need more complicated scripts, but there is probably no universal solution for every application.
Code:
#!e::                    ; Evaluate dynamic expression, Paste " = " <Result>
   ClipBoard =
   Send ^x
   ClipWait 0.25
   IfEqual ClipBoard,, {      ; No expression is selected
      Send +{Home}+{Right}^x  ; Paragraph shouldn't become empty
      ClipWait 0.25
      IfEqual ClipBoard,,Return
      StringGetPos p, ClipBoard, :, R
      StringLeft x, ClipBoard, p ; get text before last ":"
      StringTrimLeft ClipBoard, ClipBoard, p+1
      SendRaw %x%             ; Write back beginning of line
   }
   Send ^v                    ; Paste back expression
   FileDelete C:\$temp$.ahk   ; Here will be the temp script written
   FileAppend #NoTrayIcon`nClipBoard:=%ClipBoard%, C:\$temp$.ahk
   RunWait C:\$temp$.ahk      ; Run AHK to execute temp script
   Send ` = ^v                ; Paste the result after the expression
   FileDelete C:\$temp$.ahk
Return
In this version it does not matter where the insertion point is left after selecting the expression to be evaluated.
Back to top
View user's profile Send private message
ribbet.1



Joined: 20 Feb 2007
Posts: 36
Location: D.C.

PostPosted: Fri Feb 23, 2007 4:31 pm    Post subject: Re: Evaluating arithmetic expressions in documents Reply with quote

One trivial modification makes it more useful for me. Simply remove the "{Right} = " and it replaces your expression with its solution.

Code:
#!e::                    ; Evaluates dynamic expression, Paste " = " <Result>
  Send ^c
  FileDelete $temp$.ahk  ; Here will be the temp script written
  FileAppend #NoTrayIcon`nClipBoard:=%ClipBoard%, $temp$.ahk
  RunWait $temp$.ahk     ; Run AHK to execute temp script
  Send ^v      ; Replace the expression with its result
Return


I am going to impress some women with this one, I'll tell you! Thanks everybody! Very Happy


Last edited by ribbet.1 on Fri Feb 23, 2007 4:42 pm; edited 1 time in total
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 3624
Location: Belgrade

PostPosted: Fri Feb 23, 2007 4:37 pm    Post subject: Reply with quote

No, you will not.

Women are not interested in math expressions, generaly speaking.

Buy a BIG chocolate and talk about celebrities all the time, and you may lay something Razz
_________________
Back to top
View user's profile Send private message MSN Messenger
Laszlo



Joined: 14 Feb 2005
Posts: 4002
Location: Pittsburgh

PostPosted: Fri Feb 23, 2007 6:40 pm    Post subject: Re: Evaluating arithmetic expressions in documents Reply with quote

ribbet.1 wrote:
One trivial modification makes it more useful for me.
Then use it your way. However, your script could have problems in other applications:
- Between sending ^c and using the ClipBoard there may not be enough time for Windows to set up the ClipBoard. In some applications, sometimes you will see its old content, or nothing.
- In some applications ^c removes the selection. Depending on where the insertion point was, you end up inserting the result before or after the original selection.
- It is an extra step to select the expression before evaluation. It is more convenient (faster) to get the expression selected automatically. With the new regular expression functions you can do it simply and more intelligently.
Back to top
View user's profile Send private message
ribbet.1



Joined: 20 Feb 2007
Posts: 36
Location: D.C.

PostPosted: Fri Feb 23, 2007 9:06 pm    Post subject: Reply with quote

Well, I compiled it, tried out the compiled version, and then gave it to a girl, and Wham!! it didn't work. So you are right, this is no way to impress the ladies. You must be able to provide a working prototype at least.

I see that the script creates a temporary ahk script that it requires ahk to execute, so as it is you cannot compile this as a standalone, unless the person using it also has ahk installed. I now see that is why I can't impress women.

I want to work around this, but I don't have the time ATM. If anybody would like to suggest how to do this I would appreciate. I am new to AHK. I love this script and a few years ago spent some time looking for something just like this with no success. If it could be a standalone it would be extremely useful for just about anybody.

I eventually settled on Supercalc by Plamen Djonev, which at least integrates some of the features of a text processor with a calculator, and is still about the best calculator for my purposes. But this script really fills a need that I perceive has yet to be met by a standalone application.
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4002
Location: Pittsburgh

PostPosted: Fri Feb 23, 2007 10:25 pm    Post subject: Reply with quote

If you only want arithmetic expressions to be evaluated, you can incorporate one of the expression parsers posted here.
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5811

PostPosted: Sat Feb 24, 2007 7:00 am    Post subject: Reply with quote

Very Nice Sir. Very Happy
Back to top
View user's profile Send private message
ribbet.1



Joined: 20 Feb 2007
Posts: 36
Location: D.C.

PostPosted: Sat Feb 24, 2007 5:05 pm    Post subject: Reply with quote

Skan wrote:
Very Nice Sir. Very Happy

Agreed. This kind of thing should be compiled and offered as freeware, with appropriate disclaimers and credits of course. I would like to try at least one of these code snippets you showed me, and get back to you, perhaps at the page you linked instead of here.

I honestly searched several freeware sites for something like this a few years ago. And while I truly admire Mr. Djonev's Supercalc, I have always felt it could be taken further, and he is too busy with other things.

I think the ability to evaluate a simple math equation without leaving your text editor is one of those things that should just about be included with any OS, but never has been. Instead we all get our own virtual Radio Shack calculator. I have never been able to impress the ladies with that one, even the real one with its own holster.
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4002
Location: Pittsburgh

PostPosted: Sat Feb 24, 2007 5:11 pm    Post subject: Reply with quote

ribbet.1 wrote:
I have never been able to impress the ladies with that one, even the real one with its own holster.
Have you tried one with diamonds inlayed? Wink
Back to top
View user's profile Send private message
ribbet.1



Joined: 20 Feb 2007
Posts: 36
Location: D.C.

PostPosted: Sat Feb 24, 2007 6:13 pm    Post subject: Reply with quote

Laszlo wrote:
Have you tried one with diamonds inlayed? Wink


I save that for the black tie kind of affair, not just everyday.
Back to top
View user's profile Send private message
ribbet.1



Joined: 20 Feb 2007
Posts: 36
Location: D.C.

PostPosted: Sat Feb 24, 2007 6:29 pm    Post subject: Re: Evaluating arithmetic expressions in documents Reply with quote

Laszlo wrote:
ribbet.1 wrote:
One trivial modification makes it more useful for me.
Then use it your way. However, your script could have problems in other applications:
- Between sending ^c and using the ClipBoard there may not be enough time for Windows to set up the ClipBoard. In some applications, sometimes you will see its old content, or nothing.
- In some applications ^c removes the selection. Depending on where the insertion point was, you end up inserting the result before or after the original selection.
- It is an extra step to select the expression before evaluation. It is more convenient (faster) to get the expression selected automatically. With the new regular expression functions you can do it simply and more intelligently.


Agreed on all counts. And BTW, I'm not familiar with AHK much yet, but I noticed a ClipboardWait function. I wonder if this would apply to the problem you mentioned.

So I am actually painting the bedroom right now, and just stopping in, but it appears to me adding a ClipboardWait at the juncture you point out, and an optional auto highlight would make this nicer for some folks. I would be more inclined to have the auto highlight be activated by a separate optional hotkey so I don't have to do it that way. It is only more convenient and intelligent insofar as that is what you want. I can easily imagine scenarios where you would not want it necessarily. For example, simplifying an equation by resolving smaller portions within it with this script.

But it would be nice to start to learn how to work w/ regexp's since I have bought the book and everything so I could do well to try that little function as well. I want to make a few search tools that would include proximity features, but that is another topic. At any rate, this is really exciting to me.
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 3624
Location: Belgrade

PostPosted: Mon Feb 26, 2007 2:57 pm    Post subject: Reply with quote

I tried this script for the first time today and I am not very happy about its speed, due to the fact that it runs another ahk....


Expressions should be executed within script itself.
This can be probably done the easiest way using some COM object.
_________________
Back to top
View user's profile Send private message MSN Messenger
Laszlo



Joined: 14 Feb 2005
Posts: 4002
Location: Pittsburgh

PostPosted: Mon Feb 26, 2007 3:29 pm    Post subject: Reply with quote

majkinetor wrote:
I am not very happy about its speed, due to the fact that it runs another ahk....
Expressions should be executed within script itself.
This can be probably done the easiest way using some COM object.
You missed my link above. There is no need for COM, the script there finishes faster than an external program or another AHK copy could be started.
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 3624
Location: Belgrade

PostPosted: Mon Feb 26, 2007 3:53 pm    Post subject: Reply with quote

why didn't you provide the above script using that function to evaluate expressions ?

I dont' have time to read every post carefuly, and I imagine I am not the only one....
_________________
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group