|
| 1 | +;;; foldvis-outline.el --- Display indicators for outline -*- 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 outline. |
| 23 | +;; |
| 24 | + |
| 25 | +;;; Code: |
| 26 | + |
| 27 | +(require 'outline) |
| 28 | + |
| 29 | +(require 'foldvis) |
| 30 | + |
| 31 | +;; |
| 32 | +;;; Entry |
| 33 | + |
| 34 | +;;;###autoload |
| 35 | +(defun foldvis-outline--enable () |
| 36 | + "Enable the folding minor mode." |
| 37 | + (add-hook 'outline-view-change-hook #'foldvis-hideshow--refresh nil t)) |
| 38 | + |
| 39 | +;;;###autoload |
| 40 | +(defun foldvis-outline--disable () |
| 41 | + "Disable the folding minor mode." |
| 42 | + (remove-hook 'outline-view-change-hook #'foldvis-hideshow--refresh t)) |
| 43 | + |
| 44 | +;;;###autoload |
| 45 | +(defun foldvis-outline--valid-p () |
| 46 | + "Return non-nil if the backend is valid." |
| 47 | + (and (featurep 'outline) outline-minor-mode)) |
| 48 | + |
| 49 | +;; |
| 50 | +;;; Events |
| 51 | + |
| 52 | +;;;###autoload |
| 53 | +(defun foldvis-outline--toggle () |
| 54 | + "Event to toggle folding on and off." |
| 55 | + (outline-toggle-children)) |
| 56 | + |
| 57 | +;; |
| 58 | +;;; Overwrite |
| 59 | + |
| 60 | +(defvar-local foldvis-outline--nodes nil |
| 61 | + "Store a list of nodes to fold.") |
| 62 | + |
| 63 | +(defun foldvis-outline--flag-region (from to flag) |
| 64 | + "To override the function `outline-flag-region'." |
| 65 | + (push (list from to flag) foldvis-outline--nodes)) |
| 66 | + |
| 67 | +(defun foldvis-outline--show-heading () |
| 68 | + "" |
| 69 | + (foldvis-outline--flag-region (- (point) |
| 70 | + (if (bobp) 0 |
| 71 | + (if (and outline-blank-line |
| 72 | + (eq (char-before (1- (point))) ?\n)) |
| 73 | + 2 1))) |
| 74 | + (progn (outline-end-of-heading) (point)) |
| 75 | + nil)) |
| 76 | + |
| 77 | +(defun foldvis-outline--hide-sublevels (levels) |
| 78 | + "" |
| 79 | + (if (< levels 1) |
| 80 | + (error "Must keep at least one level of headers")) |
| 81 | + (save-excursion |
| 82 | + (let* (outline-view-change-hook |
| 83 | + (beg (progn |
| 84 | + (goto-char (point-min)) |
| 85 | + ;; Skip the prelude, if any. |
| 86 | + (unless (outline-on-heading-p t) (outline-next-heading)) |
| 87 | + (point))) |
| 88 | + (end (progn |
| 89 | + (goto-char (point-max)) |
| 90 | + ;; Keep empty last line, if available. |
| 91 | + (if (bolp) (1- (point)) (point))))) |
| 92 | + (if (< end beg) |
| 93 | + (setq beg (prog1 end (setq end beg)))) |
| 94 | + ;; First hide everything. |
| 95 | + (foldvis-outline--flag-region beg end t) |
| 96 | + ;; Then unhide the top level headers. |
| 97 | + (outline-map-region |
| 98 | + (lambda () |
| 99 | + (if (<= (funcall outline-level) levels) |
| 100 | + (foldvis-outline--show-heading))) |
| 101 | + beg end) |
| 102 | + ;; Finally unhide any trailing newline. |
| 103 | + (goto-char (point-max)) |
| 104 | + (if (and (bolp) (not (bobp)) (outline-invisible-p (1- (point)))) |
| 105 | + (foldvis-outline--flag-region (1- (point)) (point) nil))))) |
| 106 | + |
| 107 | +(defun foldvis-outline--nodes () |
| 108 | + "Return a list of foldable nodes." |
| 109 | + (let (outline-view-change-hook |
| 110 | + foldvis-outline--nodes) |
| 111 | + (ignore-errors (foldvis-outline--hide-sublevels 1)) |
| 112 | + foldvis-outline--nodes)) |
| 113 | + |
| 114 | +;; |
| 115 | +;;; Core |
| 116 | + |
| 117 | +(defun foldvis-outline--create (node) |
| 118 | + "Create indicators using NODE." |
| 119 | + (when-let* ((beg (nth 0 node)) |
| 120 | + (end (nth 1 node))) |
| 121 | + (let ((folded (not (nth 2 node)))) |
| 122 | + (foldvis--create-overlays beg end folded)))) |
| 123 | + |
| 124 | +(defun foldvis-outline--within-window (node wend wstart) |
| 125 | + "Return nil if NODE is not within the current window display range. |
| 126 | +
|
| 127 | +Arguments WEND and WSTART are the range for caching." |
| 128 | + (when-let* |
| 129 | + ((range (cl-case foldvis-render-method |
| 130 | + ((or full partial) (cons (nth 0 node) |
| 131 | + (nth 1 node))) |
| 132 | + (t |
| 133 | + (user-error "Invalid render method: %s" foldvis-render-method)))) |
| 134 | + (start (car range)) |
| 135 | + (end (cdr range)) |
| 136 | + ((or (and (<= wstart start) (<= end wend)) ; with in range |
| 137 | + (and (<= wstart end) (<= start wstart)) ; just one above |
| 138 | + (and (<= wend end) (<= start wend))))) ; just one below |
| 139 | + node)) |
| 140 | + |
| 141 | +;;;###autoload |
| 142 | +(defun foldvis-outline--refresh (&rest _) |
| 143 | + "Refresh indicators for all folding range." |
| 144 | + (when outline-minor-mode |
| 145 | + (when-let* ((nodes-to-fold (foldvis-outline--nodes)) |
| 146 | + (wend (window-end nil t)) |
| 147 | + (wstart (window-start)) |
| 148 | + (nodes-to-fold |
| 149 | + (cl-remove-if-not (lambda (node) |
| 150 | + (foldvis-outline--within-window node wend wstart)) |
| 151 | + nodes-to-fold))) |
| 152 | + (foldvis--remove-ovs) |
| 153 | + (thread-last nodes-to-fold |
| 154 | + (mapc #'foldvis-outline--create))) |
| 155 | + (run-hooks 'foldvis-refresh-hook))) |
| 156 | + |
| 157 | +(provide 'foldvis-outline) |
| 158 | +;;; foldvis-outline.el ends here |
0 commit comments