From e3f412abb749d7465d99b46a99b7f4a32373d60c Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Thu, 23 Aug 2018 18:42:09 +0200 Subject: [PATCH] Add +evil-want-o/O-to-continue-comments Controls whether o/O continue commented lines. Since this is the default behavior of vim, it is enabled by default. --- modules/feature/evil/config.el | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/modules/feature/evil/config.el b/modules/feature/evil/config.el index 8b495be87..f50a8cce3 100644 --- a/modules/feature/evil/config.el +++ b/modules/feature/evil/config.el @@ -3,6 +3,10 @@ ;; I'm a vimmer at heart. Its modal philosophy suits me better, and this module ;; strives to make Emacs a much better vim than vim was. +(defvar +evil-want-o/O-to-continue-comments t + "If non-nil, the o/O keys will continue comment lines if the point is on a +line with a linewise comment.") + ;; Set these defaults before `evil'; use `defvar' so they can be changed prior ;; to loading. (defvar evil-want-C-u-scroll t) @@ -122,6 +126,38 @@ (advice-add #'counsel-git-grep-action :around #'+evil*set-jump) (advice-add #'helm-ag--find-file-action :around #'+evil*set-jump) + ;; Make o/O continue comments + (defun +evil*insert-newline-above-and-respect-comments (orig-fn) + (if (or (not +evil-want-o/O-to-continue-comments) + (evil-insert-state-p)) + (funcall orig-fn) + (evil-narrow-to-field + (if (nth 4 (syntax-ppss (line-end-position))) + (evil-save-goal-column + (comment-beginning) + ;; Use a dummy char to force correct indentation + (insert "_") + (save-excursion (call-interactively #'comment-indent-new-line)) + (delete-char -1)) + (evil-move-beginning-of-line) + (insert (if use-hard-newlines hard-newline "\n")) + (forward-line -1) + (back-to-indentation))))) + (advice-add #'evil-insert-newline-above :around #'+evil*insert-newline-above-and-respect-comments) + + (defun +evil*insert-newline-below-and-respect-comments (orig-fn) + (if (or (not +evil-want-o/O-to-continue-comments) + (evil-insert-state-p)) + (funcall orig-fn) + (let ((comment-p (nth 4 (syntax-ppss (line-end-position))))) + (evil-narrow-to-field + (evil-move-end-of-line) + (if comment-p + (comment-indent-new-line) + (insert (if use-hard-newlines hard-newline "\n")) + (back-to-indentation)))))) + (advice-add #'evil-insert-newline-below :around #'+evil*insert-newline-below-and-respect-comments) + ;; --- custom interactive codes ----------- ;; These arg types will highlight matches in the current buffer (evil-ex-define-argument-type buffer-match :runner +evil-ex-buffer-match)