Skip to content

Instantly share code, notes, and snippets.

@zw963
Created December 31, 2021 06:44
Show Gist options
  • Save zw963/6a2cbd28406956304ff20e864b51d911 to your computer and use it in GitHub Desktop.
Save zw963/6a2cbd28406956304ff20e864b51d911 to your computer and use it in GitHub Desktop.
(require 'hideshow)
(unless (memq 'hs-headline mode-line-format)
(setq mode-line-format
(append '("-" hs-headline) mode-line-format)))
;; 支持 hideshow 时,显示小箭头以及隐藏的行数。
(setq elpy-folding-fringe-indicators t)
(when (fboundp 'define-fringe-bitmap)
(define-fringe-bitmap 'elpy-folding-fringe-marker
(vector #b00000000
#b00000000
#b00000000
#b11000011
#b11100111
#b01111110
#b00111100
#b00011000))
(define-fringe-bitmap 'elpy-folding-fringe-foldable-marker
(vector #b00100000
#b00010000
#b00001000
#b00000100
#b00000100
#b00001000
#b00010000
#b00100000)))
(defun elpy-folding--display-code-line-counts (ov)
"Display a folded region indicator with the number of folded lines.
Meant to be used as `hs-set-up-overlay'."
(let* ((marker-string "*fringe-dummy*")
(marker-length (length marker-string))
(close-icon (if (featurep 'all-the-icons) (concat "... " (all-the-icons-material "unfold_more")) "...")))
(cond
((eq 'code (overlay-get ov 'hs))
(let* ((nmb-line (count-lines (overlay-start ov) (overlay-end ov)))
(display-string (format (concat "(%d)" close-icon) nmb-line)))
;; fringe indicator
(when elpy-folding-fringe-indicators
(put-text-property 0 marker-length 'display
(list 'left-fringe 'elpy-folding-fringe-marker
'elpy-folding-fringe-face)
marker-string)
(overlay-put ov 'before-string marker-string)
(overlay-put ov 'elpy-hs-fringe t))
;; folding indicator
(put-text-property 0 (length display-string)
'face 'elpy-folding-face display-string)
(put-text-property 0 (length display-string)
'mouse-face 'highlight display-string)
(overlay-put ov 'display display-string)
(overlay-put ov 'elpy-hs-folded t)))
;; for docstring and comments, we don't display the number of line
((or (eq 'docstring (overlay-get ov 'hs))
(eq 'comment (overlay-get ov 'hs)))
(let ((display-string close-icon))
(put-text-property 0 (length display-string)
'mouse-face 'highlight display-string)
(overlay-put ov 'display display-string)
(overlay-put ov 'elpy-hs-folded t))))))
(setq hs-set-up-overlay #'elpy-folding--display-code-line-counts)
(add-hook 'prog-mode-hook 'hs-minor-mode)
(add-hook 'hs-minor-mode-hook (lambda ()
(local-set-key [(control c) (/)] 'hs-hide-all)
(local-set-key [(control c) (\\)] 'hs-show-all)
(local-set-key [(control tab)] 'hs-toggle-hiding)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment