1

I am trying to get electric indent behaviour in bazel BUILD files. These are essentially python files, although the structure is extremely simple.

Let me give you an example:

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
    deps = [
        ":hello-greet",
        "//lib:hello-time",
    ],
)

What I want is to have electric indentation behaviour for "]" and ")".

What I have tried is:

  • to add these characters to electric-indent-chars
  • to add electric-indent-post-self-insert-function to post-self-insert-hook

Neither of these steps seems to change the behaviour of the characters. What am I doing wrong?

  • What major mode are you using? Does TAB-indentation work in it? – Stefan Mar 16 at 13:06
  • Major mode is derived from python-mode. TAB runs 'indent-for-tab-command' and indents the way I want. – Realraptor Mar 16 at 15:40
  • Update: python.el sets 'electric-indent-inhibit' to avoid automatic indentation so 'electric-indent-chars' is never used. – Realraptor Mar 16 at 17:03
1

If your major mode derives from python-mode, than you likely need to make it

(setq-local electric-indent-inhibit nil)

As for why it's non-nil: python-mode sets this vars to a non-nil value because electric-indent is usually undesirable when indentation is syntactically significant (as in Python).

  • That worked, thanks! – Realraptor Mar 16 at 23:43
0

The solution is slightly more complicated than Stefan's answer, so it might be worth recording the details.

(defun rr-enable-electric-indent ()
  (setq-local electric-indent-inhibit nil)
  (add-to-list 'electric-indent-chars ?\))
  (add-to-list 'electric-indent-chars ?\]))

(add-hook 'python-mode-hook 'rr-enable-electric-indent)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.