purcell / emacs.d

An Emacs configuration bundle with batteries included
BSD 2-Clause "Simplified" License
6.84k stars 2.05k forks source link

Newline behavior affect the indentation #792

Closed Eason0210 closed 1 year ago

Eason0210 commented 2 years ago

@purcell ,

I found that the indentation of close brace ( }) and backet (] ) is wrong innix-code, c/c++-mode, js-mode and so on.

You can reproduce it by input the code programs = { };, and then press Enter inside the { } in nix-mode.

for example, this is my darwin.nix:

{ pkgs, lib, config, ... }:

let home           = builtins.getEnv "HOME";
    tmpdir         = "/tmp";
in {
  imports = [ <home-manager/nix-darwin> ];

  programs = { #cursor is here, and Press Enter.

}; # this line will indent to the beginning of line.
}

And I found that it is the new line behavior affect the indentation. https://github.com/purcell/emacs.d/blob/85ce2603e1b891bfa98f40ed3b9018024c62dc14/lisp/init-editing-utils.el#L79

zhou1615 commented 2 years ago

I am not sure, but it seems the logic of newline-and-indent is changed. I changed it to (global-set-key (kbd "RET") 'newline), everything works fine for me.

purcell commented 2 years ago

Hmm, I can't reproduce the initially-reported issue locally, with Emacs 28.1.

tsilvap commented 1 year ago

I can confirm this issue is still happening. I noticed this problem in cperl-mode, c-mode, and js-mode. I assume it happens in all prog-modes that use braces for blocks.

I'm providing the steps that I used to reproduce as well, in case it helps (Emacs 28.2, clean clone of the repo):

  1. Open a new C file, say "main.c".
  2. Insert the following code (except for the |, which represents where the cursor should be):
    int main()
    {
     if (a >= b) {|}
    }
  3. Press <return> (or M-x newline-and-indent).

Expected behavior

int main()
{
  if (a >= b) {
    |
  }
}

Actual behavior

int main()
{
  if (a >= b) {
    |
}
}
betaprior commented 1 year ago

I am running into the same behavior with doom emacs running on 28.2. As usual, it's easier to patch around a problem than figure out what's going on:

(defun my/fix-indentation-after-newline ()
  (save-excursion
    (next-line)
    (beginning-of-line)
    (if (looking-at "^[})]") (indent-according-to-mode))))

(advice-add 'newline-and-indent :after 'my/fix-indentation-after-newline)