doomemacs/modules/feature/evil/autoload/folds.el
Henrik Lissner c7254e7bdc
Major optimization refactor, across the board
+ enable lexical-scope everywhere (lexical-binding = t): ~5-10% faster
  startup; ~5-20% general boost
+ reduce consing, function calls & garbage collection by preferring
  cl-loop & dolist over lambda closures (for mapc[ar], add-hook, and
  various cl-lib filter/map/reduce functions) -- where possible
+ prefer functions with dedicated opcodes, like assq (see byte-defop's
  in bytecomp.el for more)
+ prefer pcase & cond (faster) over cl-case
+ general refactor for code readability
+ ensure naming & style conventions are adhered to
+ appease byte-compiler by marking unused variables with underscore
+ defer minor mode activation to after-init, emacs-startup or
  window-setup hooks; a customization opportunity for users + ensures
  custom functionality won't interfere with startup.
2017-06-09 00:47:45 +02:00

89 lines
2.9 KiB
EmacsLisp

;;; feature/evil/autoload/folds.el -*- lexical-binding: t; -*-
;; `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).
;;
;; So this is my effort to combine them.
(defun +evil--vimish-fold-p ()
(cl-some #'vimish-fold--vimish-overlay-p (overlays-at (point))))
(defun +evil--ensure-modes (&rest _)
"Ensure hs-minor-mode is enabled."
(unless (bound-and-true-p hs-minor-mode)
(hs-minor-mode +1)))
(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)
;; --- fold commands ----------------------
;;;###autoload
(defun +evil-fold-p ()
(or (+evil--vimish-fold-p)
(ignore-errors
(+evil--ensure-modes)
(hs-already-hidden-p))))
;;;###autoload (autoload '+evil:fold-toggle "feature/evil/autoload/folds" nil t)
(evil-define-command +evil:fold-toggle ()
(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 ()
(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 ()
(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)."
(interactive "<c>")
(vimish-fold-unfold-all)
(if (integerp level)
(hs-hide-level (1- level))
(hs-show-all)))
;;;###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)."
(interactive "<c>")
(vimish-fold-refold-all)
(if (integerp level)
(hs-hide-level (1- level))
(hs-hide-all)))
;; --- 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)
(ignore-errors
(call-interactively
(cond ((eq major-mode 'magit-status-mode)
#'magit-section-toggle)
((+evil-fold-p)
#'+evil:fold-toggle)
(t
#'evilmi-jump-items)))))