読者です 読者をやめる 読者になる 読者になる

Evil で outline-minor-mode を使った folding

標準のキーバインディングには hs-minor-mode を使った折り畳み機能が割り当てられているけど、これを outline-minor-modeを使ってもう少しVimのそれに近くなるようにしてみた。

とりあえず、簡単にできるレベルでVimに近づけてみただけなので、挙動の差異は沢山残っている。
以下コード。

(define-key evil-normal-state-map "zo" #'evil-open-fold)
(define-key evil-normal-state-map "zO" #'evil-open-folds-at-point)
(define-key evil-normal-state-map "zc" #'evil-close-fold)
(define-key evil-normal-state-map "zC" #'evil-close-folds-at-point)
(define-key evil-normal-state-map "za" nil)
(define-key evil-normal-state-map "zA" nil)
(define-key evil-normal-state-map "zm" #'evil-fold-more)
(define-key evil-normal-state-map "zM" #'evil-close-all-folds)
(define-key evil-normal-state-map "zr" #'evil-fold-less)
(define-key evil-normal-state-map "zR" #'evil-open-all-folds)

(evil-define-command evil-open-fold ()
  "Open one fold under the cursor."
  (outline-minor-mode)
  (show-children))
(evil-define-command evil-close-fold ()
  "Close one fold under the cursor."
  (outline-minor-mode)
  (hide-subtree))
(evil-define-command evil-open-folds-at-point ()
  "Open all folds under the cursor recursively."
  (outline-minor-mode)
  (show-subtree))
(evil-define-command evil-close-folds-at-point ()
  "Close all folds under the cursor recursively."
  (outline-minor-mode)
  (hide-subtree))
(evil-define-command evil-close-all-folds ()
  "Close all folds."
  (outline-minor-mode)
  (hide-sublevels 1))
(evil-define-command evil-open-all-folds ()
  "Open all folds."
  (outline-minor-mode)
  (show-all))
(evil-define-command evil-fold-more ()
  "Fold more."
  (outline-minor-mode)
  (when (> evil-fold-level 0)
    (decf evil-fold-level)
    (hide-sublevels (+ evil-fold-level 1))))
(evil-define-command evil-fold-less ()
  "Reduce folding."
  (outline-minor-mode)
  (incf evil-fold-level)
  (hide-sublevels (+ evil-fold-level 1)))