aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoão Távora2023-04-08 00:40:12 +0100
committerJoão Távora2023-04-08 00:40:12 +0100
commita603aa48a56a55788c4fd1a4d8452cc359d11395 (patch)
treed215c691d1957510f585c86f0a72374c0a12e9c0
parent9848ae17161828190cc0ba31e89ae54a2f08a2ef (diff)
downloademacs-feature/breadcrumb-mode.tar.gz
emacs-feature/breadcrumb-mode.zip
Breadcrumb: first stab, very slowfeature/breadcrumb-mode
* lisp/progmodes/breadcrumb.el: New file
-rw-r--r--lisp/progmodes/breadcrumb.el93
1 files changed, 93 insertions, 0 deletions
diff --git a/lisp/progmodes/breadcrumb.el b/lisp/progmodes/breadcrumb.el
new file mode 100644
index 00000000000..08484f9f9fc
--- /dev/null
+++ b/lisp/progmodes/breadcrumb.el
@@ -0,0 +1,93 @@
1;;; breadcrumb.el --- imenu-based breadcrumb paths -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2023 João Távora
4
5;; Author: João Távora <joaotavora@gmail.com>
6;; Keywords:
7
8;; This program is free software; you can redistribute it and/or modify
9;; it under the terms of the GNU General Public License as published by
10;; the Free Software Foundation, either version 3 of the License, or
11;; (at your option) any later version.
12
13;; This program is distributed in the hope that it will be useful,
14;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;; GNU General Public License for more details.
17
18;; You should have received a copy of the GNU General Public License
19;; along with this program. If not, see <https://www.gnu.org/licenses/>.
20
21;;; Commentary:
22
23;;
24
25;;; Code:
26(require 'cl-lib)
27(require 'imenu)
28
29(defun bc-path (index-alist pos)
30 "Get breadcrumb for position POS given INDEX-ALIST."
31 (cl-labels ((containsp (node pos)
32 (let ((reg (get-text-property 0 'breadcrumb-region (car node)))
33 bare-start)
34 (cond (reg (<= (car reg) pos (cdr reg)))
35 ((number-or-marker-p (setq bare-start (cdr node)))
36 (save-excursion
37 (goto-char bare-start)
38 (end-of-defun)
39 (<= bare-start pos (point)))))))
40 (dfs (node &optional path)
41 (or (and (consp (cdr node))
42 (cl-loop with path = (cons (car node) path)
43 for c in (cdr node)
44 thereis (dfs c path)))
45 (and (containsp node pos)
46 (cons (car node) path)))))
47 (nreverse
48 (cl-loop for c in index-alist thereis (dfs c)))))
49
50(defvar bc--last-update-tick 0)
51
52(defvar bc--header-line-key [header-line mouse-1])
53
54(defun bc--format-node (p)
55 (let ((reg (get-text-property 0 'breadcrumb-region p)))
56 (if reg
57 (propertize p
58 'mouse-face 'header-line-highlight
59 'help-echo "Go here"
60 'keymap (let ((m (make-sparse-keymap)))
61 (define-key m bc--header-line-key
62 (lambda (&rest _e)
63 (interactive)
64 (push-mark)
65 (goto-char (car reg))))
66 m))
67 p)))
68
69(defun bc-path-for-header-line ()
70 (cl-loop with alist =
71 (if (and imenu--index-alist
72 (= (buffer-chars-modified-tick) bc--last-update-tick))
73 imenu--index-alist
74 (setq bc--last-update-tick (buffer-chars-modified-tick))
75 (imenu--make-index-alist))
76 for (p . more) on (bc-path alist (point))
77 collect (bc--format-node p) when more collect " > "))
78
79(defvar bc-header-line-format
80 '(:eval (bc-path-for-header-line)))
81
82(define-minor-mode bc-mode
83 "Header lines with breadcrumbs."
84 :init-value nil
85 (if bc-mode (add-to-list 'header-line-format bc-header-line-format)
86 (setq header-line-format (delq bc-header-line-format header-line-format))))
87
88(provide 'breadcrumb)
89;;; breadcrumb.el ends here
90
91;; Local Variables:
92;; read-symbol-shorthands: (("bc-" . "breadcrumb-"))
93;; End: