electric-operator

https://github.com/davidshepherd7/electric-operator.git

git clone 'git://github.com/davidshepherd7/electric-operator.git'
3

Electric operator mode

travis melpa GPLv3

An emacs minor-mode to automatically add spacing around operators.

For example typing

a=10*5+2

results in

a = 10 * 5 + 2

I'm aiming to have electric-operator-mode “correctly” handle almost every operator for major modes built in to Emacs (and a few that aren't). If you find a case where it doesn't space something as expected then consider it a bug, and please report it :).

Setup

The simplest way to install electric-operator-mode is by using package.el to get it from MELPA unstable. Alternatively you can install the dependencies listed in electric-operator.el and add electric-operator.el to your load path.

Either way you also need to make sure electric-operator is loaded with (require 'electric-operator) (or you could use use-package to load packages and keep your customisations organised).

To temporarily enable electric-operator-mode simply call electric-operator-mode. To permenantly enable it for a major mode simply add it to the relevant mode hook. For example for python-mode add the following to your config:

(add-hook 'python-mode-hook #'electric-operator-mode)

Note that electric-operator-mode is not a global minor mode, and so must be enabled separately for each major mode.

Customisation

The spacing around operators is controlled by “rules”. The simplest rule is a pair containing the (unspaced) operator and the correctly spaced operator as strings. For example (cons "=" " = ") is the default rule for =. Alternatively the second element of the pair can be a function which returns the correctly spaced operator.

Each major mode has its own list of rules for the spacing of operators. The rule list for a major mode is looked up in the hash table electric-operator-mode-rules-table. Rule lists can be modified using the function electric-operator-add-rules-for-mode, which will automatically replace any existing rules for the same operator. To disable a rule set the action part of the rule (the second element) to nil.

As an example: to automatically add spacing around -> and => in python mode you would use

(electric-operator-add-rules-for-mode 'python-mode
  (cons "->" " -> ")
  (cons "=>" " => "))

To use the default rules for a new programming mode use apply to add all rules from electric-operator-prog-mode-rules:

(apply #'electric-operator-add-rules-for-mode 'my-new-mode
       electric-operator-prog-mode-rules)

The default rules for text modes can be added in the same way from the list electric-operator-prose-rules.

Major mode readiness

A number of basic operator rules are defined for any major mode, so if your language is “normal” (i.e C-like) then a good amount of functionality should just work.

If you use electric-operator for a major mode not on this list please open an issue to let me know how it went.

In general:

Contributing

I'm using Cask to manage dependencies, so to run the tests you'll need to install cask then run cask install to install dependencies. Then the tests can be run with make test.

Bug reports are also welcome!

History

Electric-operator is based on a heavily refactored version of electric-spacing by William Xu with contributions from Nikolaj Schumacher. Electric-spacing is itself based on smart-operator, also by William Xu.

Electric-operator uses simpler and more flexible rules to define how operators should be treated, and also adds a full suite of tests. However it has some additional dependencies (in particular the excellent dash and names packages) which were decided to be too heavy to add to electric-spacing.