Sublime Evaluate Plugin

The other day I released a modified version of William Bonds Prefixr Plugin. I took it as a boilerplate towards my plugin, as it handles everything I've wanted to learn about Python and Sublimes API for a while now.

Download Sublime Evaluate!

I completely re-typed and stripped out everything I didn't need to properly understand and parse in my head what was going on. Multiple selections, threads and the Sublime API.

So taking a stripped out, no functionality starting point, I was able to add in my own functions to begin mimicking Textmate's Evaluate command, simply by making use of Pythons eval command.

Currently it's limited to what eval can do, such as concatenation and arithmetic, but I plan on adding custom functions which will evaluate to a useful output. Perhaps mimic more of Textmate; date, time, cal etc…

Whilst making this plugin, I learnt a lot about Pythons internals and how making use of the _future_ import, we can change how functions will run. The reason I was looking into this was because eval doesn't automatically return decimals, only integers. To fix this, we can add:

{% codeblock lang:python %} from future import division
{% endcodeblock %}

Which will automatically handle divisions and decimals correctly, rather than having to do *9/11. *notice the trailing "." that forces the decimals to show. Stupid really.

Anyway, you can download the plugin here! I have a lot of future improvements that I'd like to add, simply because I'm using this plugin a lot!

  • Evaluate line - if there is no selection, then perhaps it'd evaluate the entire line instead.
  • Custom evaluation expressions - as mentioned above; date, cal, time, current directory etc.
  • Evaluate hex to RGB(A) and vice versa - this is something my colleague is specifically after.

At any rate, it's handy, it really is :)

comments powered by Disqus