Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Evaluating arithmetic expressions in documents


  • Please log in to reply
26 replies to this topic
Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
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.)
#!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.)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
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.
#!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.

ribbet.1
  • Members
  • 198 posts
  • Last active: Feb 07 2012 01:21 AM
  • Joined: 20 Feb 2007
One trivial modification makes it more useful for me. Simply remove the "{Right} = " and it replaces your expression with its solution.

#!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! :D

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
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 :p
Posted Image

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

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.

ribbet.1
  • Members
  • 198 posts
  • Last active: Feb 07 2012 01:21 AM
  • Joined: 20 Feb 2007
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.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
If you only want arithmetic expressions to be evaluated, you can incorporate one of the expression parsers posted here.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Very Nice Sir. :D

ribbet.1
  • Members
  • 198 posts
  • Last active: Feb 07 2012 01:21 AM
  • Joined: 20 Feb 2007

Very Nice Sir. :D

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.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

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:

ribbet.1
  • Members
  • 198 posts
  • Last active: Feb 07 2012 01:21 AM
  • Joined: 20 Feb 2007

Have you tried one with diamonds inlayed? :wink:


I save that for the black tie kind of affair, not just everyday.

ribbet.1
  • Members
  • 198 posts
  • Last active: Feb 07 2012 01:21 AM
  • Joined: 20 Feb 2007

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.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
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.
Posted Image

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

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.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
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....
Posted Image