|
| 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 |
0 commit comments