An RPN Calculator For Leo

92 views
Skip to first unread message

Thomas Passin

unread,
Apr 29, 2023, 1:42:07 PM4/29/23
to leo-editor
There are times while working in Leo that I have wanted to use a calculator.  Sometimes I keep a calculator program open, but this is not always ideal because the Leo window often will obscure the calculator when I want to look at it or use it.

I have adapted the open-source RPCalc calculator to run in a tab in the Leo log frame.  This calculator is a Reverse Polish Notation (RPN) style calculator, which IMHO is much better than the  algebraic-entry type.  It is the type of calculator that Hewlett-Packard made famous.

RPCalc is written in PyQt, so it was a good candidate to use as a starting point.  My adaptation turns it into a Leo plugin. A screenshot and a zip file with the files are attached to this post.  It's not quite ready for a pull request yet, though it is completely functional.  Still to do:  add type annotations, update the Help file, come up with a better way to copy the results of a calculation to the clipboard, and add a setting to prevent automatic addition of the plugin's toggle button to the icon bar.  There are also a few minor conveniences that aren't working right relating to the Help screen.

To "install" it, unzip the zip file - it has the rpcalc.py program and an associated rpcalc directory.  Now you can do one of two things:

1. Copy the file and directory into your leo/plugins directory;  add a line "rpcalc.py" to the @enabled-plugins node in your myLeoSettings.leo outline; or

2. Copy the file and directory somewhere else, set the PYTHONPATH environmental variable to point there, and add a line "rpcalc" (without the ".py") to the @enabled-plugins node in your myLeoSettings.leo outline.

To illustrate method 2 (on Windows;  there are minor differences for Linux), suppose you copy the plugin into c:\temp\leo\extras.  Then before starting Leo from a console, set the PYTHONPATH:

set PYTHONPATH=c:\temp\leo\extras;%PYTHONPATH%

You don't need to include the final ;%PYTHONPATH% if it has not been set already to point somewhere else.

Then restart Leo.  There will be a new button labeled "RPCalc" on the iconbar.  Clicking on it will toggle the calculator tab on or off.  The plugin works on Windows and Linux, and with either PyQt5 or PyQt6.

If you are not familiar with an RPN style calculator, it works by pushing numbers onto a stack and performing operations between members of the stack.  So to add 10 and 4, you key in "10" (from the keyboard or by clicking the number buttons), then press ENTER or click the ENT button.  This pushes the number 10 onto the stack. Then key/button-press "4", then click/type a plus sign.  The "plus" operation is executed between the 4 and the 10, leaving the result (14) in the stack bottom (the position with the large LCD characters).

There are many sites on the Web that will tell you more about using an RPN calculator.  RPCalc itself has a way to enter numbers in other common bases (eg., hex) - read the Help file to learn how.

There is an auxiliary menu that will open either with a right-click in the calculator main window or by pressing the ESC key.  These entries open a separate window (not in the Leo tab).  One of the items opens the help file. When the window is the one showing the registers, then the calculator result can be copied to the clipboard with ALT-v.  This is a little clumsy, and I plan to make it easier to copy the value (there is a technical oddity that is preventing the most obvious way from working).

The calculator will automatically use your Leo theme's colors and fonts.

Please test this plugin out and let us know how you like it, and if you would like anything changed.  I won't promise to make the changes, depending on how hard they may be or if they seem too inconsistent with the way the calculator works.  But I'll try!
rpcalc.zip
rpcalc-screenshot.png

Edward K. Ream

unread,
Apr 30, 2023, 7:03:14 AM4/30/23
to leo-e...@googlegroups.com
On Sat, Apr 29, 2023 at 12:42 PM Thomas Passin <tbp1...@gmail.com> wrote:

I have adapted the open-source RPCalc calculator to run in a tab in the Leo log frame.  This calculator is a Reverse Polish Notation (RPN) style calculator, which IMHO is much better than the  algebraic-entry type.  It is the type of calculator that Hewlett-Packard made famous.

Thanks for this work, Thomas. The calculator appears as expected for me.

PR #3301 is a draft containing the files you mention. It's a good start. The PR lists three problems.

Edward

jkn

unread,
Apr 30, 2023, 11:55:06 AM4/30/23
to leo-editor
I have wondered about suggesting something like this for a while, so thank you Thomas. My 'main' editor has a simple HP calculator built into it and it was an easy step to consider one for Leo.

I didn't know about RPNCalc (I have some Android RPN apps on my phone, as well as a real HP-35s), but it sounds like a good choice.

I will take a look at this shortly - thanks.

     J^n

Thomas Passin

unread,
Apr 30, 2023, 12:59:34 PM4/30/23
to leo-editor
RPCalc is a recent discovery for me. As originally written, it runs as a standalone program, and requires Qt5.  You don't need to use the installer package for Windows.  Just download the Linux tarball, decompress it, and navigate to the "source" directory.  The file to run is - surprise! - rpcalc.py.  It seems to do everything I want from an RPN calculator, except that copying the stack bottom is awkward.

To adapt it for Leo, one change was to combine all the source files into one Leo @file tree.  Another was to change the imports to use leoQt, which makes it easier to adapt to Qt5 vs Qt6, and anyway is essential if the program is to run in a Leo frame.  I'm still finding little things that aren't working for both Qt5 and Qt6 - mostly enums and flags - but I'm making progress. But overall, most of the functionality works and the thing is usable as it stands.  I'll post an updated outline soon, and after some more work it should be ready to appear in the Leo repo.

On Sunday, April 30, 2023 at 11:55:06 AM UTC-4 jkn wrote:
I have wondered about suggesting something like this for a while, so thank you Thomas. My 'main' editor has a simple HP calculator built into it and it was an easy step to consider one for Leo.

I didn't know about RPNCalc (I have some Android RPN apps on my phone, as well as a real HP-35s), but it sounds like a good choice.

     I've used HP RPN calculators since way back in HP-45 days.  I liked the HP-25C even better, and finally ended up using an HP-15C.  Mine still works though it's slightly misplaced just now.  On my computer I've been using Free42, which seems to me to be a good balance between readability, complexity, and capability.  Now it looks like RPCalc will be taking over from Free42.

Thomas Passin

unread,
May 1, 2023, 7:42:53 AM5/1/23
to leo-editor
I'm happy to say that the RPCalc plugin is now merged into the devel branch.  Enable it in your @enabled-plugins settings node by adding a line reading rpcalc.py.

jkn

unread,
May 1, 2023, 9:02:49 AM5/1/23
to leo-editor
I got to play with a then- just out Hewlett Packard HP-67 RPN calculator at the age of around 14. It blew my mind ... and may well have directly led to me doing what I do to this day.

    J^n

Thomas Passin

unread,
May 1, 2023, 9:31:00 AM5/1/23
to leo-editor
When I was using TurboPascal and doing a lot of numerical 2-D integrations with complex numbers, I actually wrote a little library module to calculate with complex numbers as if I was using an RPN calculator.  So you could push a complex number on the stack, pop it off, multiply or add the two numbers on the stack bottom, etc.  At that time TurboPascal did not have complex numbers of its own, IIRC.  If N1 and N2 were two complex numbers you could write, for example (based on hazy memories from long ago):

push(N1)
push(N2)
CMul()
{ and so forth, pun intended }

I enjoyed using the library because it was so easy for me to write and debug calculations.  I just pictured how I would do the calculation on my HP calculator and walked through the steps.  I timed it once, and the extra overhead of using the stack library compared with a hand-crafted sequence of operations was about 25% (I'm sure my implementation could have been improved, it was pretty brute-force).  But the ease of writing the calculation and debugging it - the RPN library won hands down.

Thomas Passin

unread,
May 1, 2023, 12:46:45 PM5/1/23
to leo-editor
Devel now contains one more change.  I've changed the EXIT key (which isn't needed in the Leo tab version of the calculator) to TOCLIP. It copies the "X" register - the calculation result - to the system clipboard.

jkn

unread,
May 1, 2023, 2:22:05 PM5/1/23
to leo-editor
Shurely that should be called >CLIP  ? ;-)

Thomas Passin

unread,
May 1, 2023, 3:18:33 PM5/1/23
to leo-editor
Much better!  I'll change it soon.  BTW, I'm sorry about R> for "Roll Down".  The original code used R< and R>, apparently because we don't have up and down arrows on a normal keyboard (well, speaking for US English keyboards, anyway).  I changed the one to a caret (R^) but there's no similar down symbol one can type.  I could have used a unicode arrow but it can't be typed conveniently.  The way the code works, if you type the string on a button (some of them, anyway) it activates the same command as if you had clicked on its button.

Rob

unread,
May 1, 2023, 8:31:31 PM5/1/23
to leo-editor
For those new to trying out the plugin, I discovered that it didn't appear to load despite adding it to my enabled plugins. What I learned is:
  1. It's in the plugin list and I needed to select the rpcalc-toggle option under the plugins menu or...
  2. I see now there's an rpcalc button I didn't notice before that toggles it on and off.
HTH

Looking forward to using it as I prefer RPN calculators to the more standard ones.

Rob...

Thomas Passin

unread,
May 1, 2023, 10:04:48 PM5/1/23
to leo-editor
I forgot to add to the Help file that there is also a minibuffer command to toggle it - rpcalc-toggle.

jkn

unread,
May 2, 2023, 4:50:53 AM5/2/23
to leo-editor
FWIW I sometime use the underscore character in a 'down' sense. So R_ , perhaps.

I used to write a fair bit in (La)TeX, and that uses caret ^ for superscript, and underscore _ for subscript, so it 'feels' write to me...

J^n

jkn

unread,
May 2, 2023, 5:35:28 AM5/2/23
to leo-editor
s/write/right/, of course ;-o

Edward K. Ream

unread,
May 2, 2023, 6:04:55 AM5/2/23
to leo-e...@googlegroups.com


On Tue, May 2, 2023 at 4:35 AM jkn <jkn...@nicorp.f9.co.uk> wrote:
s/write/right/, of course ;-o

Hehe.  I remember screaming in frustration while using some nerdy sed-like text editor while working at IBM 50+ years ago. My work on editors began with those screams.

Edward

jkn

unread,
May 14, 2023, 10:52:45 AM5/14/23
to leo-editor
As a slight aside: I knew about the 'bc' (bench calculator) program in Linux/Unix, and I also knew there was a 'dc' (desk calculator) in Linux/Unix

What I didn't realise, and have only just learned, is:

* dc is the original and runs RPN
* dc predates the C programming language; it is the oldest surviving Unix program, and Ken Thompson has opined that it was the first one written (in B) for the PDP-ll running Unix
* the BC program is written on top of dc.

Thomas Passin

unread,
May 14, 2023, 7:37:21 PM5/14/23
to leo-editor
Cool! Though I'm glad I don't have to debug any of those expressions.
Reply all
Reply to author
Forward
0 new messages