In his book, Interface: New Directions in Computer Systems Design, Jeff Raskin describes the ability to calculate arithmetic expressions right in the text editor window, without the need to run a calculator. Being inspired by this book, as well as articles
boo1ean "Translate selected text from any language into Russian" and
imitsuran "Fix keyboard layout a la Punto Switcher to bash" decided to write an implementation of this idea for Linux OS. As a result, we have a simple script called by a hot key that calculates the selected arithmetic expressions, replacing them with the results of calculations.
Decision
The script requires the following packages: xsel, xvkbd and bc. Install:
sudo aptitude install xsel xvkbd bc
Next, open any text editor and write:
')
In the variable PRECISION, we specify the accuracy of the decimal fraction (in the present embodiment, 4 decimal places). In the SELECTION variable, we write the selected arithmetic expression, and if the selection is not empty, then we calculate its value using bc, the result is written to the RESULT variable. If the result is not null, then we write the current contents of the clipboard to the BUFFER variable, rewrite the contents of the clipboard with the result of the calculation, and use the xvkbd utility to insert Ctrl + V via emulation (since the calculated expression is selected, the insertion will overwrite it with the result). We return the contents of the clipboard to the user.
Save the file, for example, under the name selcalc, grant permissions to execute and transfer to / usr / bin:
sudo chmod +x ./selcalc && sudo mv ./selcalc /usr/bin/
We assign a hotkey to launch the script, for example, Ctrl + Shift + C (I used the standard GNOME 2 tools).
Using
- select the arithmetic expression
- press Ctrl + Shift + C
- in the place of the selected expression we see the result of the calculation
Advantages and disadvantages
+ Calculation of arithmetic expressions without the need to run the calculator
+ Support for complex expressions (in fact, any expressions that bc is capable of calculating, including managing the priorities of calculations, exponentiation, etc.)
- Does not work in text editors that do not support pasting via Ctrl + V, it works crookedly in some editors (Mozilla Thunderbird, Skype)
Tested in Open Office, Sublime Text 2, gedit, Pidgin, Nautilus (in file rename mode).
The script can be downloaded
from the link .