-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuel-xref.el
298 lines (242 loc) · 9.75 KB
/
fuel-xref.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
;;; fuel-xref.el -- showing cross-reference info -*- lexical-binding: t -*-
;; Copyright (C) 2008, 2009, 2010 Jose Antonio Ortega Ruiz
;; See https://factorcode.org/license.txt for BSD license.
;; Author: Jose Antonio Ortega Ruiz <[email protected]>
;; Keywords: languages, fuel, factor
;; Start date: Sat Dec 20, 2008 22:00
;;; Comentary:
;; A mode and utilities for showing cross-reference information.
;;; Code:
(require 'fuel-edit)
(require 'fuel-completion)
(require 'fuel-help)
(require 'fuel-eval)
(require 'fuel-popup)
(require 'fuel-menu)
(require 'fuel-base)
(require 'factor-mode)
(require 'cl-seq)
(require 'button)
;;; Customization:
;;;###autoload
(defgroup fuel-xref nil
"FUEL's cross-referencing engine."
:group 'fuel)
(defcustom fuel-xref-follow-link-to-word-p t
"Whether, when following a link to a caller, we position the
cursor at the first ocurrence of the used word."
:group 'fuel-xref
:type 'boolean)
(defcustom fuel-xref-follow-link-method nil
"How new buffers are opened when following a crossref link."
:group 'fuel-xref
:type '(choice (const :tag "Other window" window)
(const :tag "Other frame" frame)
(const :tag "Current window" nil)))
(defface fuel-xref-link-face '((t (:inherit link)))
"Highlighting links in cross-reference buffers."
:group 'fuel-xref
:group 'fuel-faces
:group 'fuel)
(defvar-local fuel-xref--word nil)
;;; Buttons:
(define-button-type 'fuel-xref--button-type
'action 'fuel-xref--follow-link
'follow-link t
'face 'fuel-xref-link-face)
(defun fuel-xref--follow-link (button)
(let ((file (button-get button 'file))
(line (button-get button 'line)))
(when (not file)
(error "No file for this ref (it's probably a primitive)"))
(when (not (file-readable-p file))
(error "File '%s' is not readable" file))
(let ((word fuel-xref--word))
(fuel-edit--visit-file file fuel-xref-follow-link-method)
(when (numberp line)
(goto-char (point-min))
(forward-line (1- line)))
(when (and word fuel-xref-follow-link-to-word-p)
(and (re-search-forward (format "\\_<%s\\_>" word)
(factor-end-of-defun-pos)
t)
(goto-char (match-beginning 0)))))))
;;; The xref buffer:
(defun fuel-xref--eval<x--y> (arg word context)
"A helper for the very common task of calling an ( x -- y ) factor word."
(let ((cmd (list :fuel* (list (list arg word)) context)))
(fuel-eval--retort-result (fuel-eval--send/wait cmd))))
(defun fuel-xref--buffer ()
(or (get-buffer "*fuel xref*")
(with-current-buffer (get-buffer-create "*fuel xref*")
(fuel-xref-mode)
(fuel-popup-mode)
(current-buffer))))
(defun fuel-xref--pluralize-count (count item)
(let ((fmt (if (= count 1) "%d %s" "%d %ss")))
(format fmt count item)))
(defun fuel-xref--insert-link (title file line-num)
(insert-text-button title
:type 'fuel-xref--button-type
'help-echo (format "File: %s (%s)" file line-num)
'file file
'line line-num))
(defun fuel-xref--insert-word (word vocab file line-num)
(insert " ")
(fuel-xref--insert-link word file line-num)
(insert (if line-num (format " line %s" line-num)
" primitive"))
(newline))
(defun fuel-xref--insert-vocab-words (vocab-def xrefs)
(cl-destructuring-bind (vocab file) vocab-def
(insert "in ")
(fuel-xref--insert-link (or vocab "unknown vocabs") file 1)
(let ((count-str (fuel-xref--pluralize-count (length xrefs) "word")))
(insert (format " %s:\n" count-str))))
(dolist (xref xrefs)
(apply 'fuel-xref--insert-word xref))
(newline))
(defun fuel-xref--display-word-groups (search-str cc xref-groups)
"Should be called in a with-current-buffer context"
(let ((inhibit-read-only t)
(title-str (format "Words %s %s:\n\n" cc search-str)))
(erase-buffer)
(insert (propertize title-str 'font-lock-face 'bold))
(dolist (group xref-groups)
(apply 'fuel-xref--insert-vocab-words group)))
(goto-char (point-min))
(message "")
(fuel-popup--display (current-buffer)))
(defun fuel-xref--display-vocabs (search-str cc xrefs)
"Should be called in a with-current-buffer context"
(put-text-property 0 (length search-str) 'font-lock-face 'bold search-str)
(let* ((inhibit-read-only t)
(xrefs (cl-remove-if (lambda (el) (not (nth 2 el))) xrefs))
(count-str (fuel-xref--pluralize-count (length xrefs) "vocab"))
(title-str (format "%s %s %s:\n\n" count-str cc search-str)))
(erase-buffer)
(insert title-str)
(cl-loop for (vocab _ file line-num) in xrefs do
(insert " ")
(fuel-xref--insert-link vocab file line-num)
(newline)))
(goto-char (point-min))
(message "")
(fuel-popup--display (current-buffer)))
(defun fuel-xref--callers (word)
(fuel-xref--eval<x--y>
(list :quote word)
'fuel-callers-xref
(factor-current-vocab)))
(defun fuel-xref--show-callers (word)
(let ((res (fuel-xref--callers word)))
(with-current-buffer (fuel-xref--buffer)
(setq fuel-xref--word word)
(fuel-xref--display-word-groups word "calling" res))))
(defun fuel-xref--word-callers-files (word)
(mapcar 'cadar (fuel-xref--callers word)))
(defun fuel-xref--show-callees (word)
(let ((res (fuel-xref--eval<x--y>
(list :quote word)
'fuel-callees-xref
(factor-current-vocab))))
(with-current-buffer (fuel-xref--buffer)
(setq fuel-xref--word nil)
(fuel-xref--display-word-groups word "used by" res))))
(defun fuel-xref--apropos (str)
(let ((res (fuel-xref--eval<x--y> str 'fuel-apropos-xref "")))
(with-current-buffer (fuel-xref--buffer)
(setq fuel-xref--word nil)
(fuel-xref--display-word-groups str "containing" res))))
(defun fuel-xref--show-vocab-words (vocab)
(let ((res (fuel-xref--eval<x--y> vocab 'fuel-vocab-xref vocab)))
(with-current-buffer (fuel-xref--buffer)
(setq fuel-xref--word nil)
(fuel-xref--display-word-groups vocab "in vocabulary" res))))
(defun fuel-xref--show-vocab-usage (vocab)
(let ((res (fuel-xref--eval<x--y> vocab 'fuel-vocab-usage-xref "")))
(with-current-buffer (fuel-xref--buffer)
(setq fuel-xref--word nil)
(fuel-xref--display-vocabs vocab "using" res))))
(defun fuel-xref--show-vocab-uses (vocab)
(let ((res (fuel-xref--eval<x--y> vocab 'fuel-vocab-uses-xref "")))
(with-current-buffer (fuel-xref--buffer)
(setq fuel-xref--word nil)
(fuel-xref--display-vocabs vocab "used by" res))))
;;; User commands:
(defvar fuel-xref--word-history nil)
(defun fuel-show-callers (&optional arg)
"Show a list of callers of word or vocabulary at point.
With prefix argument, ask for word."
(interactive "P")
(let ((word (if arg (fuel-completion--read-word "Find callers for: "
(factor-symbol-at-point)
fuel-xref--word-history)
(factor-symbol-at-point))))
(when word
(message "Looking up %s's users ..." word)
(if (and (not arg)
(factor-on-vocab))
(fuel-xref--show-vocab-usage word)
(fuel-xref--show-callers word)))))
(defun fuel-show-callees (&optional arg)
"Show a list of callers of word or vocabulary at point.
With prefix argument, ask for word."
(interactive "P")
(let ((word (if arg (fuel-completion--read-word "Find callees for: "
(factor-symbol-at-point)
fuel-xref--word-history)
(factor-symbol-at-point))))
(when word
(message "Looking up %s's callees ..." word)
(if (and (not arg)
(factor-on-vocab))
(fuel-xref--show-vocab-uses word)
(fuel-xref--show-callees word)))))
(defvar fuel-xref--vocab-history nil)
(defun fuel-vocab-uses (&optional arg)
"Show a list of vocabularies used by a given one.
With prefix argument, force reload of vocabulary list."
(interactive "P")
(let ((vocab (fuel-completion--read-vocab arg
(factor-symbol-at-point)
fuel-xref--vocab-history)))
(fuel-xref--show-vocab-uses vocab)))
(defun fuel-vocab-usage (&optional arg)
"Show a list of vocabularies that use a given one.
With prefix argument, force reload of vocabulary list."
(interactive "P")
(let ((vocab (fuel-completion--read-vocab arg
(factor-symbol-at-point)
fuel-xref--vocab-history)))
(fuel-xref--show-vocab-usage vocab)))
(defun fuel-apropos (str)
"Show a list of words containing the given substring."
(interactive "MFind words containing: ")
(message "Looking up %s's references ..." str)
(fuel-xref--apropos str))
(defun fuel-show-file-words (&optional arg)
"Show a list of words in current file.
With prefix argument, ask for the vocab."
(interactive "P")
(let ((vocab (or (and (not arg) (factor-current-vocab))
(fuel-completion--read-vocab nil))))
(when vocab
(fuel-xref--show-vocab-words vocab))))
;;; Xref mode:
(defun fuel-xref-show-help ()
(interactive)
(let ((fuel-help-always-ask nil))
(fuel-help)))
;;;###autoload
(define-derived-mode fuel-xref-mode fundamental-mode "FUEL Xref"
"Mode for displaying FUEL cross-reference information.
\\{fuel-xref-mode-map}"
:syntax-table factor-mode-syntax-table
(buffer-disable-undo)
(suppress-keymap fuel-xref-mode-map)
(set-keymap-parent fuel-xref-mode-map button-buffer-map)
(define-key fuel-xref-mode-map "h" 'fuel-xref-show-help)
(setq buffer-read-only t))
(provide 'fuel-xref)
;;; fuel-xref.el ends here