1

So I recently encountered a problem programming in C that whenever I enter the left parenthesis character, (, Emacs auto-idents the code.

[4 spaces here]DATA *SeqQueueOut(

I know that there is this thing called electric-ident-mode, which I do not want to disable it. All I want to do is to not ident the code specifically whenever I type (. How can I do that in init.el?

1
2

C-hk( tells us:

( runs the command c-electric-paren (found in c-mode-map), which
is an interactive compiled Lisp function in ‘cc-cmds.el’.

It is bound to ), (.

I suggest:

(with-eval-after-load "cc-mode"
  (define-key c-mode-map (kbd "(") #'self-insert-command)
  (define-key c-mode-map (kbd ")") #'self-insert-command))
0

electric-indent-mode is enabled by default. You can turn it off via (electric-indent-mode -1) in your ~/.emacs, but if you want to only disable it for a particular char (e.g. () in a particular major mode, the normal way could look like:

(add-hook '<foo>-mode-hook
          (lambda ()
            (setq electric-indent-chars
                  (delq ?\( electric-indent-chars))))

If a major mode does not obey electric-indent-chars I suggest you report it as a bug in that major mode (e.g. with M-x report-emacs-bug if it's bundled with Emacs).

4
  • cc-mode uses its own electric config here. c-electric-paren says "If ‘c-syntactic-indentation’ and ‘c-electric-flag’ are both non-nil, the line is reindented unless a numeric ARG is supplied, or the parenthesis is inserted inside a literal." – phils Sep 9 '20 at 20:41
  • See also (ccmode)Electric Keys and c-toggle-electric-state – phils Sep 9 '20 at 20:44
  • I'm not sure it's a bug so much as a significantly older set of functionality? – phils Sep 9 '20 at 20:47
  • @phils: It should be adapted to the new generic functionality, so it does qualify as a bug (to the extent that obeying the variable wouldn't necessarily be incompatible with its featureset). – Stefan Sep 9 '20 at 22:45

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.