doomemacs/modules/feature/evil/autoload/folds.el

90 lines
2.9 KiB
EmacsLisp
Raw Normal View History

;;; feature/evil/autoload/folds.el -*- lexical-binding: t; -*-
2017-05-15 20:20:06 +02:00
;; `hideshow' is a decent code folding implementation, but it won't let you
;; create custom folds. `evil-vimish-fold' offers custom folds, but essentially
;; ignores any other type of folding (indent or custom markers, which
;; hs-minor-mode gives you).
2017-05-15 20:20:06 +02:00
;;
;; So this is my effort to combine them.
(defun +evil--vimish-fold-p ()
(cl-some #'vimish-fold--vimish-overlay-p (overlays-at (point))))
2017-05-15 20:20:06 +02:00
(defun +evil--ensure-modes (&rest _)
"Ensure hs-minor-mode is enabled."
(unless (bound-and-true-p hs-minor-mode)
(hs-minor-mode +1)))
2017-05-15 20:20:06 +02:00
(advice-add #'hs-toggle-hiding :before #'+evil--ensure-modes)
(advice-add #'hs-hide-block :before #'+evil--ensure-modes)
(advice-add #'hs-hide-level :before #'+evil--ensure-modes)
(advice-add #'hs-show-all :before #'+evil--ensure-modes)
(advice-add #'hs-hide-all :before #'+evil--ensure-modes)
2017-05-15 20:20:06 +02:00
;; --- fold commands ----------------------
2017-05-15 20:20:06 +02:00
;;;###autoload
(defun +evil-fold-p ()
(or (+evil--vimish-fold-p)
(ignore-errors
(+evil--ensure-modes)
(hs-already-hidden-p))))
2017-05-15 20:20:06 +02:00
;;;###autoload (autoload '+evil:fold-toggle "feature/evil/autoload/folds" nil t)
(evil-define-command +evil:fold-toggle ()
2017-05-15 20:20:06 +02:00
(interactive)
(if (+evil--vimish-fold-p)
(vimish-fold-toggle)
(hs-toggle-hiding)))
;;;###autoload (autoload '+evil:fold-open "feature/evil/autoload/folds" nil t)
(evil-define-command +evil:fold-open ()
2017-05-15 20:20:06 +02:00
(interactive)
(if (+evil--vimish-fold-p)
(vimish-fold-unfold)
(hs-hide-block)))
;;;###autoload (autoload '+evil:fold-close "feature/evil/autoload/folds" nil t)
(evil-define-command +evil:fold-close ()
2017-05-15 20:20:06 +02:00
(interactive)
(if (+evil--vimish-fold-p)
(vimish-fold-refold)
(hs-hide-block)))
;;;###autoload (autoload '+evil:fold-open-all "feature/evil/autoload/folds" nil t)
(evil-define-command +evil:fold-open-all (&optional level)
"Open folds at LEVEL (or all folds if LEVEL is nil)."
2017-05-15 20:20:06 +02:00
(interactive "<c>")
(vimish-fold-unfold-all)
(if (integerp level)
(hs-hide-level (1- level))
(hs-show-all)))
2017-05-15 20:20:06 +02:00
;;;###autoload (autoload '+evil:fold-close-all "feature/evil/autoload/folds" nil t)
(evil-define-command +evil:fold-close-all (&optional level)
"Close folds at LEVEL (or all folds if LEVEL is nil)."
2017-05-15 20:20:06 +02:00
(interactive "<c>")
(vimish-fold-refold-all)
(if (integerp level)
(hs-hide-level (1- level))
(hs-hide-all)))
2017-05-15 20:20:06 +02:00
;; --- misc -------------------------------
;;;###autoload
(defun +evil/matchit-or-toggle-fold ()
"Do what I mean. If on a fold-able element, toggle the fold with
`hs-toggle-hiding'. Otherwise, if on a delimiter, jump to the matching one with
`evilmi-jump-items'. If in a magit-status buffer, use `magit-section-toggle'."
(interactive)
2017-05-28 15:32:16 +02:00
(ignore-errors
(call-interactively
(cond ((eq major-mode 'magit-status-mode)
#'magit-section-toggle)
((+evil-fold-p)
#'+evil:fold-toggle)
2017-05-28 15:32:16 +02:00
(t
#'evilmi-jump-items)))))