Skip to content

Commit 8dbd9bf

Browse files
committed
list max
1 parent 6d8da38 commit 8dbd9bf

File tree

5 files changed

+228
-24
lines changed

5 files changed

+228
-24
lines changed

foldvis-hideshow.el

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
;;; foldvis-hideshow.el --- Display indicators for hideshow -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2025 Shen, Jen-Chieh
4+
5+
;; This file is not part of GNU Emacs.
6+
7+
;; This program is free software: you can redistribute it and/or modify
8+
;; it under the terms of the GNU General Public License as published by
9+
;; the Free Software Foundation, either version 3 of the License, or
10+
;; (at your option) any later version.
11+
12+
;; This program is distributed in the hope that it will be useful,
13+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
;; GNU General Public License for more details.
16+
17+
;; You should have received a copy of the GNU General Public License
18+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
20+
;;; Commentary:
21+
;;
22+
;; Display indicators for hideshow.
23+
;;
24+
25+
;;; Code:
26+
27+
(require 'hideshow)
28+
29+
(require 'foldvis)
30+
31+
;;
32+
;; (@* "Entry" )
33+
;;
34+
35+
;;;###autoload
36+
(defun foldvis-hideshow--enable ()
37+
"Enable the folding minor mode."
38+
(add-hook 'hs-show-hook #'foldvis-hideshow--refresh nil t)
39+
(add-hook 'hs-hide-hook #'foldvis-hideshow--refresh nil t))
40+
41+
;;;###autoload
42+
(defun foldvis-hideshow--disable ()
43+
"Disable the folding minor mode."
44+
(remove-hook 'hs-show-hook #'foldvis-hideshow--refresh t)
45+
(remove-hook 'hs-hide-hook #'foldvis-hideshow--refresh t))
46+
47+
;;;###autoload
48+
(defun foldvis-hideshow--valid ()
49+
"Return non-nil if the backend is valid."
50+
(and (featurep 'hideshow) hs-minor-mode))
51+
52+
;;
53+
;; (@* "Events" )
54+
;;
55+
56+
;;;###autoload
57+
(defun foldvis-hideshow--toggle ()
58+
"Event to toggle folding on and off."
59+
(hs-toggle-hiding))
60+
61+
;;
62+
;; (@* "Core" )
63+
;;
64+
65+
;;;###autoload
66+
(defun foldvis-hideshow--refresh (&rest _)
67+
"Refresh indicators for all folding range."
68+
(when hs-minor-mode
69+
(goto-char (point-min))
70+
(while (search-forward-regexp hs-block-start-regexp nil t)
71+
(when (if hideshowvis-ignore-same-line
72+
(let ((begin-line (save-excursion
73+
(goto-char (match-beginning 0))
74+
(line-number-at-pos (point)))))
75+
(save-excursion
76+
(goto-char (match-beginning 0))
77+
(ignore-errors
78+
(progn
79+
(funcall hs-forward-sexp-func 1)
80+
(> (line-number-at-pos (point)) begin-line)))))
81+
t)
82+
(let* ((ovl (make-overlay (match-beginning 0) (match-end 0))))
83+
(overlay-put ovl 'before-string
84+
(propertize
85+
"*hideshowvis*"
86+
'display
87+
(list 'left-fringe
88+
'hideshowvis-hideable-marker
89+
'hideshowvis-hidable-face)))
90+
(overlay-put ovl 'hideshowvis-hs t))))
91+
(run-hooks 'foldvis-refresh-hook)))
92+
93+
(provide 'foldvis-hideshow)
94+
;;; foldvis-hideshow.el ends here

foldvis-origami.el

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
;;; foldvis-origami.el --- Display indicators for origami -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2025 Shen, Jen-Chieh
4+
5+
;; This file is not part of GNU Emacs.
6+
7+
;; This program is free software: you can redistribute it and/or modify
8+
;; it under the terms of the GNU General Public License as published by
9+
;; the Free Software Foundation, either version 3 of the License, or
10+
;; (at your option) any later version.
11+
12+
;; This program is distributed in the hope that it will be useful,
13+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
;; GNU General Public License for more details.
16+
17+
;; You should have received a copy of the GNU General Public License
18+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
20+
;;; Commentary:
21+
;;
22+
;; Display indicators for origami.
23+
;;
24+
25+
;;; Code:
26+
27+
(require 'origami)
28+
29+
(require 'foldvis)
30+
31+
;;
32+
;; (@* "Entry" )
33+
;;
34+
35+
;;;###autoload
36+
(defun foldvis-origami--valid ()
37+
"Return non-nil if the backend is valid."
38+
(and (featurep 'origami) origami-mode))
39+
40+
;;
41+
;; (@* "Events" )
42+
;;
43+
44+
;;;###autoload
45+
(defun foldvis-origami--toggle ()
46+
"Event to toggle folding on and off."
47+
(call-interactively #'origami-toggle-node))
48+
49+
;;
50+
;; (@* "Core" )
51+
;;
52+
53+
(defun foldvis-origami--create (node)
54+
"Create indicators using NODE."
55+
(when-let* ((beg (origami-fold-beg node))
56+
(end (origami-fold-end node)))
57+
(let ((folded (not (origami-fold-open? node))))
58+
(foldvis--create-overlays beg end folded))))
59+
60+
(defun foldvis-origami--within-window (node wend wstart)
61+
"Return nil if NODE is not within the current window display range.
62+
63+
Arguments WEND and WSTART are the range for caching."
64+
(when-let*
65+
((range (cl-case foldvis-render-method
66+
((or full partial) (cons (origami-fold-beg node)
67+
(origami-fold-end node)))
68+
(t
69+
(user-error "Invalid render method: %s" foldvis-render-method))))
70+
(start (car range))
71+
(end (cdr range))
72+
((or (and (<= wstart start) (<= end wend)) ; with in range
73+
(and (<= wstart end) (<= start wstart)) ; just one above
74+
(and (<= wend end) (<= start wend))))) ; just one below
75+
node))
76+
77+
;;;###autoload
78+
(defun foldvis-origami--refresh (&rest _)
79+
"Refresh indicators for all folding range."
80+
(origami-util-with-current-buffer buffer
81+
(when-let*
82+
((tree (ignore-errors (origami-get-fold-tree buffer)))
83+
(nodes-to-fold (elt tree 4))
84+
(wend (window-end nil t))
85+
(wstart (window-start))
86+
(nodes-to-fold
87+
(cl-remove-if-not (lambda (node)
88+
(foldvis-origami--within-window node wend wstart))
89+
nodes-to-fold)))
90+
(foldvis--remove-ovs)
91+
(thread-last nodes-to-fold
92+
(mapc #'foldvis-origami--create))
93+
(run-hooks 'foldvis-refresh-hook))))
94+
95+
(provide 'foldvis-origami)
96+
;;; foldvis-origami.el ends here

foldvis-treesit-fold.el

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@
3434

3535
;;;###autoload
3636
(defun foldvis-treesit-fold--enable ()
37-
"Start folding minor mode."
37+
"Enable the folding minor mode."
3838
(advice-add 'treesit-fold--after-command :after #'foldvis-treesit-fold--refresh))
3939

4040
;;;###autoload
4141
(defun foldvis-treesit-fold--disable ()
42-
"Stop folding minor mode."
42+
"Disable the folding minor mode."
4343
(advice-remove 'treesit-fold--after-command #'foldvis-treesit-fold--refresh))
4444

45+
;;;###autoload
46+
(defun foldvis-treesit-fold--valid ()
47+
"Return non-nil if the backend is valid."
48+
(and (featurep 'treesit-fold) treesit-fold-mode))
49+
4550
;;
4651
;; (@* "Events" )
4752
;;

foldvis-ts-fold.el

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,21 @@
3434

3535
;;;###autoload
3636
(defun foldvis-ts-fold--enable ()
37-
"Start folding minor mode."
37+
"Enable the folding minor mode."
3838
(add-hook 'tree-sitter-after-change-functions #'foldvis--trigger-render nil t)
3939
(advice-add 'ts-fold--after-command :after #'foldvis-ts-fold--refresh))
4040

4141
;;;###autoload
4242
(defun foldvis-ts-fold--disable ()
43-
"Stop folding minor mode."
43+
"Disable the folding minor mode."
4444
(remove-hook 'tree-sitter-after-change-functions #'foldvis--trigger-render t)
4545
(advice-remove 'ts-fold--after-command #'foldvis-ts-fold--refresh))
4646

47+
;;;###autoload
48+
(defun foldvis-ts-fold--valid ()
49+
"Return non-nil if the backend is valid."
50+
(and (featurep 'ts-fold) ts-fold-mode))
51+
4752
;;
4853
;; (@* "Events" )
4954
;;

foldvis.el

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,27 @@ See macro `with-selected-window' description for arguments WINDOW and BODY."
156156
;; (@* "Entry" )
157157
;;
158158

159-
(defun foldvis--choose-backend ()
160-
"Set the current possible backend."
161-
(setq foldvis--backend
162-
(or foldvis--backend
163-
(cl-some (lambda (x)
164-
(when (featurep x)
165-
x))
166-
foldvis-backends))))
167-
168-
(defun foldvis--call-backend (name)
169-
"Call the backend function by NAME."
170-
(when-let* ((name (format "foldvis-%s-%s" foldvis--backend name))
159+
(defun foldvis--call-backend (name &optional backend)
160+
"Call the BACKEND function by NAME."
161+
(when-let* ((name (format "foldvis-%s-%s" (or backend
162+
foldvis--backend)
163+
name))
171164
(name (intern name))
172165
((fboundp name)))
173166
(funcall name)))
174167

168+
(defun foldvis--choose-backend ()
169+
"Set the current possible backend."
170+
(let ((backend (cl-some (lambda (x)
171+
(when (foldvis--call-backend "-valid" x)
172+
x))
173+
foldvis-backends)))
174+
(when (and foldvis--backend
175+
(not (equal foldvis--backend backend))) ; has changed?
176+
;; TODO: ..
177+
)
178+
(setq foldvis--backend backend)))
179+
175180
(defun foldvis--enable ()
176181
"Start folding minor mode."
177182
(foldvis--call-backend "-enable")
@@ -280,11 +285,11 @@ is created); this is used to determie what indicators' bitmap to use."
280285
This is a static/constant method."
281286
(let ((prior foldvis-priority))
282287
(cl-case bitmap
283-
(foldvis-fr-plus (+ prior 2))
288+
(foldvis-fr-plus (+ prior 2))
284289
(foldvis-fr-minus-tail (+ prior 2))
285-
(foldvis-fr-end-left (+ prior 1))
286-
(foldvis-fr-end-right (+ prior 1))
287-
(t prior))))
290+
(foldvis-fr-end-left (+ prior 1))
291+
(foldvis-fr-end-right (+ prior 1))
292+
(t prior))))
288293

289294
(defun foldvis--get-string (folded ov bitmap)
290295
"Return a string or nil for indicators overlay (OV).
@@ -330,10 +335,9 @@ Argument FOLDED holds folding state; it's a boolean."
330335
(last-ov (nth len-1 ov-lst))
331336
(index 1))
332337
;; Head
333-
(foldvis--active-ov
334-
folded first-ov
335-
(if folded 'foldvis-fr-plus
336-
'foldvis-fr-minus-tail))
338+
(foldvis--active-ov folded first-ov
339+
(if folded 'foldvis-fr-plus
340+
'foldvis-fr-minus-tail))
337341
;; Last
338342
(foldvis--active-ov folded last-ov (foldvis--get-end-fringe))
339343
;; In between `head' and `last'

0 commit comments

Comments
 (0)