evil icon indicating copy to clipboard operation
evil copied to clipboard

Removing spaces when to leave insert state doesn't work except open command.

Open h-youhei opened this issue 8 years ago • 8 comments

Issue type

  • Bug report

Environment

Emacs version: GNU Emacs 25.2.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.22.10) Operating System: Arch Linux Evil version: 1.2.12 Evil installation type: MELPA Graphical/Terminal: X

Reproduction steps

(package-initialize)
(require 'evil)
(electric-indent-mode 0)
(define-key insert-state-map [return] #'newline-and-indent)
(evil-mode 1)

In scratch buffer i (defvar e ( RET ESC

Expected behavior

Remove the spaces.

Actual behavior

The spaces remain.

Further notes

Similar cases, vim remove spaces. Is that intended behavior?

this code change the behavior as I expect.

(defun evil-maybe-remove-spaces-fix (&optional do-remove)
  (if do-remove
    (progn
      (when (and
            evil-maybe-remove-spaces
            (save-excursion
              (beginning-of-line)
              (looking-at "^\\s-*$")))
         (delete-region (line-beginning-position) (line-end-position))
         (setq evil-maybe-remove-spaces nil)))
      (setq evil-maybe-remove-spaces (memq this-command '(
         evil-open-above
         evil-open-below
         evil-append
         evil-append-line
         newline
         newline-and-indent
         indent-and-newline)))))

(advice-add #'evil-maybe-remove-spaces :override #'evil-maybe-remove-spaces-fix)

7/11 I edited above code because I made mistakes about parenthis. The range is

from line 9  (looking-at ...
  to line 11 (setq evil-maybe-remove-spaces ...

h-youhei avatar Jul 11 '17 05:07 h-youhei

Please give an actual Vim example where spaces are removed on the same line cursor is on.

wasamasa avatar Jul 11 '17 05:07 wasamasa

vimrc

filetype plugin indent on

In shell vim example.el

In vim i (defvar e ( RET ESC

Vim version 8.0

h-youhei avatar Jul 11 '17 05:07 h-youhei

Thanks, I can reproduce it with that.

wasamasa avatar Jul 11 '17 06:07 wasamasa

FWIW, I think this issue has been fixed? I just tried the following, and the behavior on ESC was correct.

(electric-indent-mode 0)
(evil-define-key 'insert prog-mode-map (kbd "RET") #'newline-and-indent)```

saurik avatar Sep 27 '20 08:09 saurik

No, it hasn't. I can neither see that behavior with your code nor the repro code (which needs a tiny adjustment to work):

(package-initialize)
(require 'evil)
(electric-indent-mode 0)
(define-key evil-insert-state-map [return] #'newline-and-indent)
(evil-mode 1)

When trying either cursor is positioned back, but there's still two spaces on the line following the (defvar e one.

wasamasa avatar Sep 27 '20 08:09 wasamasa

@wasamasa OK, yeah; I actually have spent the last couple hours playing with the behavior of various indentation insertions (as I really want to maintain indent but never add new indent on RET), came across the same issue I'd just commented saying it worked, realized the mistake in how I tested it earlier, and came back here to find you already having corrected me ;P.

So yeah: this isn't fixed. What I'd missed is the importance of how the example specifically typed the extra text into the buffer after entering indent mode. I'm sorry for the confusion :(.

I am not 100% sure if I'm actually satisfying the requirements correctly, but I think this replacement for evil-maybe-remove-spaces might work correctly? (I don't really like the check on this-command, but that's what the original version was doing.)

(defun evil-maybe-remove-spaces (&optional do-remove)
  "..."

  (if (and evil-maybe-remove-spaces do-remove)
      (when (save-excursion
              (beginning-of-line)
              (looking-at "^\\s-*$"))
        (delete-region (line-beginning-position)
                       (line-end-position))))

  (setq evil-maybe-remove-spaces (memq this-command
                                       '(evil-open-above
                                         evil-open-below
                                         evil-append
                                         evil-append-line
                                         newline
                                         newline-and-indent
                                         indent-and-newline))))

saurik avatar Sep 27 '20 09:09 saurik

(That said, I also don't really understand why this one function is doing "double-duty" for both the hook that maintains the variable and the actual removal. There was maybe a reason for the old function to do it, but this new one definitely feels like it shouldn't need to, but since I didn't really understand why the old one was doing it I haven't broken it out until two functions.)

saurik avatar Sep 27 '20 09:09 saurik

The behavior I was really looking for was the behavior of what happens if you do filetype plugin indent off with set autoindent and smarttab, which is that if you hit RET after { you don't get a new indentation, but everything else keeps working. I've seemingly replicated that between the above fix for this issue (which can go into a .emacs until a similar fix ever might be committed), turning off electric indent and remapping RET in the way I did earlier (which I realized I typed wrong into GitHub, making the electric indent deactivation the "language tag... I went up and fixed that), and the following hack to newline-and-indent :D.

(defun newline-and-indent ()
    (interactive)
    (evil-maybe-remove-spaces t)
    (newline)
    (indent-relative-first-indent-point))

saurik avatar Sep 27 '20 10:09 saurik