-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuel-debug-uses.el
229 lines (187 loc) · 7.7 KB
/
fuel-debug-uses.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
;;; fuel-debug-uses.el -- retrieving USING: stanzas -*- lexical-binding: t -*-
;; Copyright (C) 2008, 2009 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: Tue Dec 23, 2008 04:23
;;; Comentary:
;; Support for getting and updating factor source vocabulary lists.
;;; Code:
(require 'fuel-debug)
(require 'fuel-eval)
(require 'fuel-popup)
(require 'fuel-base)
;;; Customization:
;;;###autoload
(defgroup fuel-debug-uses nil
"Customization for FUEL's debug uses."
:group 'fuel)
(defface fuel-debug-uses-header-face '((t (:inherit header)))
"Header face for FUEL's debug uses."
:group 'fuel-debug-uses
:group 'fuel-faces
:group 'faces)
(defface fuel-debug-uses-prompt-face '((t (:inherit comint-highlight-prompt)))
"Prompt face for FUEL's debug uses."
:group 'fuel-debug-uses
:group 'fuel-faces
:group 'faces)
;;; Utility functions:
(defsubst fuel-debug--chomp (s)
(replace-regexp-in-string "[\n\r\f]" "" s))
(defun fuel-debug--file-lines (file)
(when (file-readable-p file)
(with-current-buffer (find-file-noselect file)
(save-excursion
(goto-char (point-min))
(let ((lines) (in-usings))
(while (not (eobp))
(when (looking-at "^USING: ") (setq in-usings t))
(let ((line (fuel-debug--chomp
(substring-no-properties (thing-at-point 'line)))))
(when in-usings (setq line (concat "! " line)))
(push line lines))
(when (and in-usings (looking-at "\\(^\\|.* \\);\\( \\|\n\\)"))
(setq in-usings nil))
(forward-line))
(reverse lines))))))
(defun fuel-debug--uses-filter (restarts)
(let ((result) (i 1) (rn 0))
(dolist (r restarts (reverse result))
(setq rn (1+ rn))
(when (string-match "Use the .+ vocabulary\\|Defer" r)
(push (list i rn r) result)
(setq i (1+ i))))))
;;; Retrieving USINGs:
(defun fuel-debug--uses-buffer ()
(or (get-buffer "*fuel uses*")
(with-current-buffer (get-buffer-create "*fuel uses*")
(fuel-debug-uses-mode)
(fuel-popup-mode)
(current-buffer))))
(defvar-local fuel-debug--uses-file nil)
(defvar-local fuel-debug--uses-restarts nil)
(defsubst fuel-debug--uses-insert-title ()
(insert "Inferring USING: stanza for " fuel-debug--uses-file ".\n\n"))
(defun fuel-debug--uses-prepare (file)
(with-current-buffer (fuel-debug--uses-buffer)
(let ((inhibit-read-only t))
(setq fuel-debug--uses-file file
fuel-debug--uses nil
fuel-debug--uses-restarts nil)
(erase-buffer)
(fuel-debug--uses-insert-title))))
(defun fuel-debug--uses-clean ()
(setq fuel-debug--uses-file nil
fuel-debug--uses nil
fuel-debug--uses-restarts nil))
(defun fuel-debug--current-usings (file)
(with-current-buffer (find-file-noselect file)
(sort (factor-find-usings t) 'string<)))
(defun fuel-debug--uses-for-file (file)
(let* ((lines (fuel-debug--file-lines file))
(old-usings (fuel-debug--current-usings file))
(cmd `(:fuel ((V{ ,@old-usings }
[ ,file V{ ,@lines } fuel-get-uses ]
fuel-use-suggested-vocabs)) t t)))
(fuel-debug--uses-prepare file)
(with-current-buffer (fuel-debug--uses-buffer)
(let ((inhibit-read-only t))
(insert "Asking Factor. Please, wait...\n")
(fuel-eval--send cmd 'fuel-debug--uses-cont)))
(fuel-popup--display (fuel-debug--uses-buffer))))
(defun fuel-debug--uses-cont (retort)
(let ((uses (fuel-debug--uses retort))
(err (fuel-eval--retort-error retort)))
(if err
(fuel-debug--uses-display-err retort)
(fuel-debug--uses-display uses))))
(defun fuel-debug--uses-display (uses)
(let* ((inhibit-read-only t)
(old (fuel-debug--current-usings fuel-debug--uses-file))
(new (sort uses 'string<)))
(erase-buffer)
(fuel-debug--uses-insert-title)
(if (cl-equalp old new)
(progn
(insert "Current USING: is already fine!. Type 'q' to bury buffer.\n")
(fuel-debug--uses-clean))
(fuel-debug--highlight-names old new 'fuel-font-lock-debug-unneeded-vocab)
(fuel-debug--highlight-names new old 'fuel-font-lock-debug-missing-vocab)
(fuel-debug--insert-vlist "Current vocabulary list:" old)
(newline)
(fuel-debug--insert-vlist "Correct vocabulary list:" new)
(setq fuel-debug--uses new)
(insert "\nType 'y' to update your USING: to the new one.\n"))))
(defun fuel-debug--uses-display-err (retort)
(let* ((inhibit-read-only t)
(err (fuel-eval--retort-error retort))
(restarts (fuel-debug--uses-filter (fuel-eval--error-restarts err)))
(unique (= 1 (length restarts))))
(erase-buffer)
(fuel-debug--uses-insert-title)
(insert (fuel-eval--retort-output retort))
(newline)
(if (not restarts)
(insert "\nSorry, couldn't infer the vocabulary list.\n")
(setq fuel-debug--uses-restarts restarts)
(if unique (fuel-debug--uses-restart 1)
(insert "\nPlease, type the number of the desired vocabulary:\n\n")
(dolist (r restarts)
(insert (format " :%s %s\n" (cl-first r) (cl-third r))))))))
(defun fuel-debug--uses-update-usings ()
(interactive)
(let ((inhibit-read-only t)
(file fuel-debug--uses-file)
(uses fuel-debug--uses))
(when file
(insert "\nDone!")
(fuel-debug--uses-clean)
(fuel-popup--quit)
(fuel-debug--replace-usings file uses)
(message "USING: updated!"))))
(defun fuel-debug--uses-restart (n)
(when (and (> n 0) (<= n (length fuel-debug--uses-restarts)))
(let* ((inhibit-read-only t)
(restart (format ":%s" (cadr (nth (1- n) fuel-debug--uses-restarts))))
(cmd `(:fuel ([ (:factor ,restart) ] fuel-with-autouse) t t)))
(setq fuel-debug--uses-restarts nil)
(insert "\nAsking Factor. Please, wait ...\n")
(fuel-eval--send cmd 'fuel-debug--uses-cont))))
;;; Fuel uses mode:
(defconst fuel-debug--uses-header-regex
(format "^%s.*$"
(regexp-opt '("Inferring USING: stanza for "
"Current USING: is already fine!"
"Current vocabulary list:"
"Correct vocabulary list:"
"Sorry, couldn't infer the vocabulary list."
"Done!"))))
(defconst fuel-debug--uses-prompt-regex
(format "^%s"
(regexp-opt '("Asking Factor. Please, wait ..."
"Please, type the number of the desired vocabulary:"
"Type 'y' to update your USING: to the new one."))))
(defconst fuel-debug--uses-font-lock-keywords
`((,fuel-debug--uses-header-regex . 'fuel-debug-uses-header-face)
(,fuel-debug--uses-prompt-regex . 'fuel-debug-uses-prompt-face)
(,fuel-debug--restart-regex (1 'fuel-font-lock-debug-restart-number)
(2 'fuel-font-lock-debug-restart-name))))
(defvar fuel-debug-uses-mode-map
(let ((map (make-keymap)))
(suppress-keymap map)
(dotimes (n 9)
(define-key map (vector (+ ?1 n))
`(lambda () (interactive) (fuel-debug--uses-restart ,(1+ n)))))
(define-key map "y" 'fuel-debug--uses-update-usings)
(define-key map "\C-c\C-c" 'fuel-debug--uses-update-usings)
map))
;;;###autoload
(define-derived-mode fuel-debug-uses-mode fundamental-mode "FUEL Uses"
"A major mode for displaying Factor's USING: inference results.
\\{fuel-debug-uses-mode-map}"
(buffer-disable-undo)
(setq font-lock-defaults
'(fuel-debug--uses-font-lock-keywords t nil nil nil)))
(provide 'fuel-debug-uses)
;;; fuel-debug-uses.el ends here