evil-mode '>' evil-shift-left 失去选择

evil-mode '>' evil-shift-left 失去选择

有什么原因导致 evil-mode evil-shift-left 失去选择?

即,我希望能够在选择上多次点击“>”和“<”来左右移动文本。

以下是实现。有没有办法添加建议来改变这种行为?

邪恶命令.el

(evil-define-operator evil-shift-right (beg end &optional count)
  "Shift text from BEG to END to the right.
The text is shifted to the nearest multiple of `evil-shift-width'
\(the rounding can be disabled by setting `evil-shift-round').
See also `evil-shift-left'."
  :type line
  (interactive "<r><vc>")
  (let ((beg (set-marker (make-marker) beg))
        (end (set-marker (make-marker) end)))
    (dotimes (i (or count 1))
      (if (not evil-shift-round)
          (indent-rigidly beg end evil-shift-width)
        (let* ((indent
                (save-excursion
                  (goto-char beg)
                  (evil-move-beginning-of-line nil)
                  (while (and (< (point) end) (looking-at "[ \t]*$"))
                    (forward-line))
                  (if (> (point) end) 0
                    (current-indentation))))
               (offset (- evil-shift-width (mod indent evil-shift-width))))
          (indent-rigidly beg end offset))))
    (set-marker beg nil)
    (set-marker end nil)))

答案1

这会使当前行为超载以重新选择(位于djoyner/dotfiles/emacs/lisp/evil-config.el#L36-L40)。

; Overload shifts so that they don't lose the selection
(define-key evil-visual-state-map (kbd ">") 'djoyner/evil-shift-right-visual)
(define-key evil-visual-state-map (kbd "<") 'djoyner/evil-shift-left-visual)
(define-key evil-visual-state-map [tab] 'djoyner/evil-shift-right-visual)
(define-key evil-visual-state-map [S-tab] 'djoyner/evil-shift-left-visual)

(defun djoyner/evil-shift-left-visual ()
  (interactive)
  (evil-shift-left (region-beginning) (region-end))
  (evil-normal-state)
  (evil-visual-restore))

(defun djoyner/evil-shift-right-visual ()
  (interactive)
  (evil-shift-right (region-beginning) (region-end))
  (evil-normal-state)
  (evil-visual-restore))

答案2

与@avsej 已经提供的答案非常相似,但使用call-interactively可确保 evil-shift-{left,right} 接收其所有参数(例如COUNT)。例如,

(defun my-visual-shift-left ()
  (interactive)
  (call-interactively 'evil-shift-left)
  (evil-normal-state)
  (evil-visual-restore))

这样,您仍然可以一次缩进多个单位(例如4>)。

相关内容