Add new emacs/hideshow module

Brings better default code folding support to various languages, like
yaml, ruby, matlab, haml and vimrc. Hideshow is still quite
unsophisticated and will need the help of another package for complete
code folding functionality. Perhaps origami or vimish fold.

The code-folding functional in the feature/evil module will soon be
replaced by that.
This commit is contained in:
Henrik Lissner 2018-07-28 01:35:47 +02:00
parent 05d9a83ff7
commit a2ffbe4ede
No known key found for this signature in database
GPG Key ID: 5F6C0EA160557395
4 changed files with 93 additions and 3 deletions

View File

@ -257,9 +257,6 @@ DEFAULT is non-nil, set the default mode-line for all buffers."
;; Built-in packages
;;
;; `hideshow'
(setq hs-hide-comments-when-hiding-all nil)
;; show typed keystrokes in minibuffer
(defun doom|enable-ui-keystrokes () (setq echo-keystrokes 0.02))
(defun doom|disable-ui-keystrokes () (setq echo-keystrokes 0))

View File

@ -54,6 +54,7 @@
ediff ; comparing files in Emacs
electric ; smarter, keyword-based electric-indent
;eshell ; a consistent, cross-platform shell (WIP)
hideshow ; basic code-folding support
imenu ; an imenu sidebar and searchable code index
;term ; terminals in Emacs
vc ; version-control and Emacs, sitting in a tree

View File

@ -0,0 +1,64 @@
;;; emacs/hideshow/autoload.el -*- lexical-binding: t; -*-
;;;###autoload
(defun +hideshow-haml-forward-sexp (arg)
(haml-forward-sexp)
(move-beginning-of-line 1))
;;;###autoload
(defun +hideshow-forward-block-by-indent (arg)
(let ((start (current-indentation)))
(forward-line)
(unless (= start (current-indentation))
(let ((range (+hideshow-indent-range)))
(goto-char (cadr range))
(end-of-line)))))
;;
;; Indentation detection
;;
(defun +hideshow--empty-line-p ()
(string= "" (string-trim (thing-at-point 'line))))
(defun +hideshow--geq-or-empty-p ()
(or (+hideshow--empty-line-p) (>= (current-indentation) base)))
(defun +hideshow--g-or-empty-p ()
(or (+hideshow--empty-line-p) (> (current-indentation) base)))
(defun +hideshow--seek (start direction before skip predicate)
"Seeks forward (if direction is 1) or backward (if direction is -1) from start, until predicate
fails. If before is nil, it will return the first line where predicate fails, otherwise it returns
the last line where predicate holds."
(save-excursion
(goto-char start)
(goto-char (point-at-bol))
(let ((bnd (if (> 0 direction)
(point-min)
(point-max)))
(pt (point)))
(when skip (forward-line direction))
(cl-loop while (and (/= (point) bnd) (funcall predicate))
do (progn
(when before (setq pt (point-at-bol)))
(forward-line direction)
(unless before (setq pt (point-at-bol)))))
pt)))
(defun +hideshow-indent-range (&optional point)
"Return the point at the begin and end of the text block with the same (or
greater) indentation. If `point' is supplied and non-nil it will return the
begin and end of the block surrounding point."
(save-excursion
(when point
(goto-char point))
(let ((base (current-indentation))
(begin (point))
(end (point)))
(setq begin (+hideshow--seek begin -1 t nil #'+hideshow--geq-or-empty-p)
begin (+hideshow--seek begin 1 nil nil #'+hideshow--g-or-empty-p)
end (+hideshow--seek end 1 t nil #'+hideshow--geq-or-empty-p)
end (+hideshow--seek end -1 nil nil #'+hideshow--empty-line-p))
(list begin end base))))

View File

@ -0,0 +1,28 @@
;;; emacs/hideshow/config.el -*- lexical-binding: t; -*-
(after! hideshow ; built-in
(setq hs-hide-comments-when-hiding-all nil)
(unless (assq 't hs-special-modes-alist)
(setq hs-special-modes-alist
(append
'((vimrc-mode "{{{" "}}}" "\"")
(yaml-mode "\\s-*\\_<\\(?:[^:]+\\)\\_>"
""
"#"
+hideshow-forward-block-by-indent nil)
(haml-mode "[#.%]" "\n" "/" +hideshow-haml-forward-sexp nil)
(ruby-mode "class\\|d\\(?:ef\\|o\\)\\|module\\|[[{]"
"end\\|[]}]"
"#\\|=begin"
ruby-forward-sexp)
(enh-ruby-mode "class\\|d\\(?:ef\\|o\\)\\|module\\|[[{]"
"end\\|[]}]"
"#\\|=begin"
enh-ruby-forward-sexp nil)
(matlab-mode "if\\|switch\\|case\\|otherwise\\|while\\|for\\|try\\|catch"
"end"
nil (lambda (arg) (matlab-forward-sexp))))
hs-special-modes-alist
'((t))))))