feature/evil: refactor folding & outline support

Code folding commands will now obey outline headers (if
outline-minor-mode is on and in outline-mode).
This commit is contained in:
Henrik Lissner 2018-09-20 22:23:33 -04:00
parent 9245e030bb
commit 6fce87bd06
4 changed files with 108 additions and 59 deletions

View file

@ -5,6 +5,12 @@
"Face to hightlight `hideshow' overlays." "Face to hightlight `hideshow' overlays."
:group 'doom-themes) :group 'doom-themes)
;;;###autoload
(defun +hideshow*ensure-mode (&rest _)
"Ensure hs-minor-mode is enabled."
(unless (bound-and-true-p hs-minor-mode)
(hs-minor-mode +1)))
;;;###autoload ;;;###autoload
(defun +hideshow-haml-forward-sexp (arg) (defun +hideshow-haml-forward-sexp (arg)
(haml-forward-sexp arg) (haml-forward-sexp arg)

View file

@ -25,7 +25,14 @@
enh-ruby-forward-sexp nil) enh-ruby-forward-sexp nil)
(matlab-mode "if\\|switch\\|case\\|otherwise\\|while\\|for\\|try\\|catch" (matlab-mode "if\\|switch\\|case\\|otherwise\\|while\\|for\\|try\\|catch"
"end" "end"
nil (lambda (arg) (matlab-forward-sexp)))) nil (lambda (_arg) (matlab-forward-sexp))))
hs-special-modes-alist hs-special-modes-alist
'((t)))))) '((t))))))
;; Ensure `hs-minor-mode' is active when triggering these commands
(advice-add #'hs-toggle-hiding :before #'+hideshow*ensure-mode)
(advice-add #'hs-hide-block :before #'+hideshow*ensure-mode)
(advice-add #'hs-hide-level :before #'+hideshow*ensure-mode)
(advice-add #'hs-show-all :before #'+hideshow*ensure-mode)
(advice-add #'hs-hide-all :before #'+hideshow*ensure-mode)

View file

@ -1,38 +1,34 @@
;;; feature/evil/autoload/folds.el -*- lexical-binding: t; -*- ;;; feature/evil/autoload/folds.el -*- lexical-binding: t; -*-
(require 'evil-vimish-fold)
(require 'hideshow) (require 'hideshow)
;; `hideshow' is a decent code folding implementation, but it won't let you ;; `hideshow' is a decent code folding implementation, but it won't let you
;; create custom folds. `evil-vimish-fold' offers custom folds, but essentially ;; create custom folds. `vimish-fold' offers custom folds, but essentially
;; ignores any other type of folding (indent or custom markers, which ;; ignores any other type of folding (indent or custom markers, which
;; hs-minor-mode gives you). ;; hs-minor-mode and `outline-mode' give you).
;; ;;
;; So this is my effort to combine them. ;; So this is my effort to combine them.
(defun +evil--vimish-fold-p () (defun +evil--vimish-fold-p ()
(cl-some #'vimish-fold--vimish-overlay-p (overlays-at (point)))) (and (featurep 'vimish-fold)
(cl-some #'vimish-fold--vimish-overlay-p
(overlays-at (point)))))
(defun +evil--ensure-modes (&rest _) (defun +evil--outline-fold-p ()
"Ensure hs-minor-mode is enabled." (and (or (bound-and-true-p outline-minor-mode)
(unless (bound-and-true-p hs-minor-mode) (derived-mode-p 'outline-mode))
(hs-minor-mode +1))) (outline-on-heading-p)))
(advice-add #'hs-toggle-hiding :before #'+evil--ensure-modes) (defun +evil--hideshow-fold-p ()
(advice-add #'hs-hide-block :before #'+evil--ensure-modes) (hs-minor-mode +1)
(advice-add #'hs-hide-level :before #'+evil--ensure-modes) (save-excursion
(advice-add #'hs-show-all :before #'+evil--ensure-modes) (ignore-errors
(advice-add #'hs-hide-all :before #'+evil--ensure-modes) (or (hs-looking-at-block-start-p)
(hs-find-block-beginning)))))
;; --- fold commands ---------------------- ;;
;; Code folding
;;;###autoload
(defun +evil-fold-p ()
(or (+evil--vimish-fold-p)
(ignore-errors
(+evil--ensure-modes)
(hs-already-hidden-p))))
(defmacro +evil-from-eol (&rest body) (defmacro +evil-from-eol (&rest body)
"Perform action after moving to the end of the line." "Perform action after moving to the end of the line."
@ -40,47 +36,87 @@
(end-of-line) (end-of-line)
,@body)) ,@body))
;;;###autoload (autoload '+evil:fold-toggle "feature/evil/autoload/folds" nil t) ;;;###autoload
(evil-define-command +evil:fold-toggle () (defun +evil/fold-toggle ()
(interactive) (interactive)
(if (+evil--vimish-fold-p) (save-excursion
(vimish-fold-toggle) (cond ((+evil--vimish-fold-p) (vimish-fold-toggle))
(+evil-from-eol (hs-toggle-hiding)))) ((+evil--hideshow-fold-p) (+evil-from-eol (hs-toggle-hiding)))
((+evil--outline-fold-p) (outline-toggle-children)))))
;;;###autoload (autoload '+evil:fold-open "feature/evil/autoload/folds" nil t) ;;;###autoload
(evil-define-command +evil:fold-open () (defun +evil/fold-open ()
(interactive) (interactive)
(if (+evil--vimish-fold-p) (save-excursion
(vimish-fold-unfold) (cond ((+evil--vimish-fold-p) (vimish-fold-unfold))
(+evil-from-eol (hs-show-block)))) ((+evil--hideshow-fold-p) (+evil-from-eol (hs-show-block)))
((+evil--outline-fold-p)
(outline-show-children)
(outline-show-entry)))))
;;;###autoload (autoload '+evil:fold-close "feature/evil/autoload/folds" nil t) ;;;###autoload
(evil-define-command +evil:fold-close () (defun +evil/fold-close ()
(interactive) (interactive)
(if (+evil--vimish-fold-p) (save-excursion
(vimish-fold-refold) (cond ((+evil--vimish-fold-p) (vimish-fold-refold))
(+evil-from-eol (hs-hide-block)))) ((+evil--hideshow-fold-p) (+evil-from-eol (hs-hide-block)))
((+evil--outline-fold-p) (outline-hide-subtree)))))
;;;###autoload (autoload '+evil:fold-open-all "feature/evil/autoload/folds" nil t) ;;;###autoload
(evil-define-command +evil:fold-open-all (&optional level) (defun +evil/fold-open-all (&optional level)
"Open folds at LEVEL (or all folds if LEVEL is nil)." "Open folds at LEVEL (or all folds if LEVEL is nil)."
(interactive "<c>") (interactive
(vimish-fold-unfold-all) (list (if current-prefix-arg (prefix-numeric-value current-prefix-arg))))
(if (integerp level) (when (featurep 'vimish-fold)
(hs-hide-level (1- level)) (vimish-fold-unfold-all))
(hs-show-all))) (save-excursion
(if (integerp level)
(progn
(outline-hide-sublevels (max 1 (1- level)))
(hs-life-goes-on
(hs-hide-level-recursive (1- level) (point-min) (point-max))))
(hs-show-all)
(when (fboundp 'outline-show-all)
(outline-show-all)))))
;;;###autoload (autoload '+evil:fold-close-all "feature/evil/autoload/folds" nil t) ;;;###autoload
(evil-define-command +evil:fold-close-all (&optional level) (defun +evil/fold-close-all (&optional level)
"Close folds at LEVEL (or all folds if LEVEL is nil)." "Close folds at LEVEL (or all folds if LEVEL is nil)."
(interactive "<c>") (interactive
(vimish-fold-refold-all) (list (if current-prefix-arg (prefix-numeric-value current-prefix-arg))))
(if (integerp level) (save-excursion
(hs-hide-level (1- level)) (when (featurep 'vimish-fold)
(hs-hide-all))) (vimish-fold-refold-all))
(if (integerp level)
(progn
(when (fboundp 'outline-hide-sublevels)
(outline-hide-sublevels (max 1 (1- level))))
(hs-life-goes-on
(hs-hide-level-recursive (1- level) (point-min) (point-max))))
(when (fboundp 'outline-hide-sublevels)
(outline-hide-sublevels 1))
(hs-hide-all))))
(defun +evil--invisible-points (count)
(let (points)
(save-excursion
(catch 'abort
(if (< count 0) (beginning-of-line))
(while (re-search-forward hs-block-start-regexp nil t
(if (> count 0) 1 -1))
(unless (invisible-p (point))
(end-of-line)
(when (hs-already-hidden-p)
(push (point) points)
(when (>= (length points) count)
(throw 'abort nil))))
(forward-line (if (> count 0) 1 -1)))))
points))
;; --- misc -------------------------------
;;
;; Misc
;;;###autoload ;;;###autoload
(defun +evil/matchit-or-toggle-fold () (defun +evil/matchit-or-toggle-fold ()

View file

@ -73,14 +73,14 @@ line with a linewise comment.")
;; `evil-delete' in wgrep buffers. ;; `evil-delete' in wgrep buffers.
(define-key wgrep-mode-map [remap evil-delete] #'+evil-delete)) (define-key wgrep-mode-map [remap evil-delete] #'+evil-delete))
;; replace native folding commands ;; Add vimish-fold, outline-mode & hideshow support to folding commands
(define-key! 'global (define-key! 'global
[remap evil-toggle-fold] #'+evil:fold-toggle [remap evil-toggle-fold] #'+evil/fold-toggle
[remap evil-close-fold] #'+evil:fold-close [remap evil-close-fold] #'+evil/fold-close
[remap evil-open-fold] #'+evil:fold-open [remap evil-open-fold] #'+evil/fold-open
[remap evil-open-fold-rec] #'+evil:fold-open [remap evil-open-fold-rec] #'+evil/fold-open
[remap evil-close-folds] #'+evil:fold-close-all [remap evil-close-folds] #'+evil/fold-close-all
[remap evil-open-folds] #'+evil:fold-open-all) [remap evil-open-folds] #'+evil/fold-open-all)
(defun +evil|disable-highlights () (defun +evil|disable-highlights ()
"Disable ex search buffer highlights." "Disable ex search buffer highlights."