alexmurray / evil-vimish-fold

63 stars 5 forks source link

Selecting two lines and folding folds 3 lines #22

Open aaronjensen opened 3 years ago

aaronjensen commented 3 years ago

If I use V, then hit j to select two lines, followed by zf, it folds 3 lines. This differs from vim, which will only fold 2. I'm guessing this happens because as far as the range is concerned, I think emacs considers the point to be at the beginning of the next line and, in emacs mode, vimish-fold assumes you want to fold the line that includes the point. This seems perhaps wrong, but I wanted to check in here first to see if that's known, I've got some configuration issue, or something else.

Thanks!

9ziggy9 commented 4 months ago

This is really old but I racked my brain trying to get a fix. I think this has something to do with certain evil-mode configurations which lend to visual mode consuming the final newline character of a region during line-selection.

You may have noticed that standard visual selection leads to no issues.

A way to fix this is to wrap your calls to vimish-fold with a condition which decrements the endpoint if the region happens to end in a newline character. Here is my use-package config, just take the zig/vimish-fold-region function and adapt it as you need.

(use-package vimish-fold
  :ensure t
  :after evil
  :preface
  (defun zig/vimish-fold-region ()
    (interactive)
    (when (region-active-p)
      (let ((beg (region-beginning)) (end (region-end)))
        (if (= (char-before end) ?\n) (setq end (1- end)))
        (vimish-fold beg end)
        (evil-normal-state))))
  :bind (:map evil-visual-state-map
         ("<tab>"   . zig/vimish-fold-region)
         :map evil-normal-state-map
         ("<tab>"   . 'vimish-fold-toggle)
         ("M-<tab>" . 'vimish-fold-delete))
  :config
  (vimish-fold-global-mode 1)
  (setq vimish-fold-persist-on-saving t))

I also found this in the evil-mode documentation, although this did not work in my case and I believe that this is because it again does not account for the newline character consumption in LINE selection mode.

evil-v$-excludes-newline
If non-nil, evil-end-of-line does not move as far as to include the `` `` char at eol. This makes v$ consistent with $ used as a motion (e.g. v$y is consistent with y$ in normal state).