Search code examples
cemacscursor-positionautoformatting

Disable all Emacs c-mode reformatting, insertion/refusal, cursor jumps


I want to keep all syntax highlighting, autocomplete, commands etc. of Emacs c-mode (and I'll add in company-mode or similar shortly) but I want to disable everything in c-mode which:

  1. Reformats other than if I C-<command> or M-<command> ask it to.

  2. Moves my cursor around the source, even momentarily, unless I asked for it. For example, closing parens/braces highlighting the matching one.

  3. Ever i) inserts anything I didn't type (other than 1 Tab = 4 spaces) or ii) refuses to insert something I did type because formatting.

  4. Reformats, repaginates, realigns or otherwise edits the source for any reason, including on paste, on save, or because the moon is gibbous.

I've already set (setq-default c-basic-offset 4) and (setq-default indent-tabs-mode nil).

I have tried:

  • C-x C-l a.k.a. c-toggle-electric-state a.k.a. (setq-default c-electric-flag nil) which fixes some of 4, but Tab still reformats code when it feels inclined, and I'm guessing isn't alone; and doesn't fix 2 or 3.

  • c-auto-align-backslashes which fixes one above case only.

  • (setq c-syntactic-indentation nil) which partially fixes 4, but breaks 1 and 3 (for instance, Tab doesn't insert at the cursor), and doesn't fix 2.

  • text-mode which obviously removes all the c-mode features I want to keep.

  • Various searches and dives into https://www.gnu.org/software/emacs/manual/html_mono/ccmode.html, which carefully documents 2^1024 settings but doesn't exhibit an "all formatting off" switch that I can find.

  • Scanning https://sourceforge.net/p/cc-mode/cc-mode/ci/default/tree/ for obvious flags to disable (*blork*?)

I've been at this for hours, so any help gratefully received.

Update: More thrashing around: installing emacs source code (apt-get install emacs24-el) and using C-h k <key combo> to find the function called when a key is pressed. M-x find-function to browse that source to work out what it's actually doing and look for hooks. (setq-default indent-tabs-mode nil) to stick with spaces, not tabs. (setq-default blink-matching-paren nil) to prevent cursor jump on closing parens/braces. I still can't disable formatting, or achieve 1, 2, 3 and 4, but I'm getting better at whack-a-mole.


Solution

  • First observation: this is a difficult sort of question to answer, because "disable everything that does ...." is a bit unspecific. However...

    Item 2. (Moving point momentarily to the matching open paren when you type a close paren) is easy: (setq blink-matching-paren nil). This is in the Emacs manual on page "Matching Parentheses". (Whoops! You've already found that one).

    You don't like the action of the tab key; what do you want it to do? It's bound to c-indent-command, which indents the current line inwards or outwards to the "correct" position. I'm guessing you want simply to insert spaces to bring point to the next 4*nth position. How about tab-to-tab-stop? Try (define-key c-mode-base-map "\t" 'tab-to-tab-stop). Note that defining it in the default key map would not work, since the major mode key map takes priority over it.

    Having done C-c C-l to disable the automatic indentation on typing, e.g., a ;, does typing a ; still do anything to offend? If so, you could disable its "special" features entirely with (define-key c-mode-base-map ";" 'self-insert-command)

    Again, a list of specific things you don't like, even a long list, is a lot easier to deal with than "everything which inserts anything I didn't type". The maintainer of the mode just doesn't mentally classify things that way.

    Anyhow, I hope you manage to get things sorted out.