Fix evil-join filling non-comments poorly

This commit is contained in:
Henrik Lissner 2021-02-25 13:51:05 -05:00
parent a3f242d4b6
commit c049480cbd
2 changed files with 20 additions and 17 deletions

View file

@ -179,28 +179,31 @@ more information on modifiers."
(if file (evil-edit file))) (if file (evil-edit file)))
;;;###autoload (autoload '+evil-join-a "editor/evil/autoload/advice" nil nil) ;;;###autoload (autoload '+evil-join-a "editor/evil/autoload/advice" nil nil)
(evil-define-operator +evil-join-a (beg end) (defun +evil-join-a (orig-fn beg end)
"Join the selected lines. "Join the selected lines.
This advice improves on `evil-join' by removing comment delimiters when joining This advice improves on `evil-join' by removing comment delimiters when joining
commented lines, by using `fill-region-as-paragraph'. commented lines, by using `fill-region-as-paragraph'.
From https://github.com/emacs-evil/evil/issues/606" From https://github.com/emacs-evil/evil/issues/606"
:motion evil-line ;; But revert to the default we're not in a comment, where
(let* ((count (count-lines beg end)) ;; `fill-region-as-paragraph' is too greedy.
(count (if (> count 1) (1- count) count)) (if (not (doom-point-in-comment-p (min (max beg (1+ (point))) end)))
(fixup-mark (make-marker))) (funcall orig-fn beg end)
(unwind-protect (let* ((count (count-lines beg end))
(dotimes (var count) (count (if (> count 1) (1- count) count))
(if (and (bolp) (eolp)) (fixup-mark (make-marker)))
(join-line 1) (unwind-protect
(let* ((end (line-beginning-position 3)) (dotimes (var count)
(fill-column (1+ (- end beg)))) (if (and (bolp) (eolp))
(set-marker fixup-mark (line-end-position)) (join-line 1)
(fill-region-as-paragraph beg end nil t) (let* ((end (line-beginning-position 3))
(goto-char fixup-mark) (fill-column (1+ (- end beg))))
(fixup-whitespace)))) (set-marker fixup-mark (line-end-position))
(set-marker fixup-mark nil)))) (fill-region-as-paragraph beg end nil t)
(goto-char fixup-mark)
(fixup-whitespace))))
(set-marker fixup-mark nil)))))
;;;###autoload ;;;###autoload
(defun +evil--fix-dabbrev-in-minibuffer-h () (defun +evil--fix-dabbrev-in-minibuffer-h ()

View file

@ -165,7 +165,7 @@ directives. By default, this only recognizes C directives.")
(abort-recursive-edit))) (abort-recursive-edit)))
;; Make J (evil-join) remove comment delimiters when joining lines. ;; Make J (evil-join) remove comment delimiters when joining lines.
(advice-add #'evil-join :override #'+evil-join-a) (advice-add #'evil-join :around #'+evil-join-a)
;; Prevent gw (`evil-fill') and gq (`evil-fill-and-move') from squeezing ;; Prevent gw (`evil-fill') and gq (`evil-fill-and-move') from squeezing
;; spaces. It doesn't in vim, so it shouldn't in evil. ;; spaces. It doesn't in vim, so it shouldn't in evil.