Clean up some duplication
[factor/jcg.git] / misc / fuel / fuel-debug-uses.el
blob4842f960d1787ebcbf8c9a3755601a0a46c2ae9d
1 ;;; fuel-debug-uses.el -- retrieving USING: stanzas
3 ;; Copyright (C) 2008, 2009 Jose Antonio Ortega Ruiz
4 ;; See http://factorcode.org/license.txt for BSD license.
6 ;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
7 ;; Keywords: languages, fuel, factor
8 ;; Start date: Tue Dec 23, 2008 04:23
10 ;;; Comentary:
12 ;; Support for getting and updating factor source vocabulary lists.
14 ;;; Code:
16 (require 'fuel-debug)
17 (require 'fuel-eval)
18 (require 'fuel-popup)
19 (require 'fuel-font-lock)
20 (require 'fuel-base)
24 ;;; Customization:
26 (fuel-font-lock--defface fuel-font-lock-debug-uses-header
27 'bold fuel-debug "headers in Uses buffers")
29 (fuel-font-lock--defface fuel-font-lock-debug-uses-prompt
30 'italic fuel-debug "prompts in Uses buffers")
33 ;;; Utility functions:
35 (defsubst fuel-debug--chomp (s)
36 (replace-regexp-in-string "[\n\r\f]" "" s))
38 (defun fuel-debug--file-lines (file)
39 (when (file-readable-p file)
40 (with-current-buffer (find-file-noselect file)
41 (save-excursion
42 (goto-char (point-min))
43 (let ((lines) (in-usings))
44 (while (not (eobp))
45 (when (looking-at "^USING: ") (setq in-usings t))
46 (let ((line (fuel-debug--chomp
47 (substring-no-properties (thing-at-point 'line)))))
48 (when in-usings (setq line (concat "! " line)))
49 (push line lines))
50 (when (and in-usings (looking-at "\\(^\\|.* \\);\\( \\|\n\\)"))
51 (setq in-usings nil))
52 (forward-line))
53 (reverse lines))))))
55 (defun fuel-debug--uses-filter (restarts)
56 (let ((result) (i 1) (rn 0))
57 (dolist (r restarts (reverse result))
58 (setq rn (1+ rn))
59 (when (string-match "Use the .+ vocabulary\\|Defer" r)
60 (push (list i rn r) result)
61 (setq i (1+ i))))))
64 ;;; Retrieving USINGs:
66 (fuel-popup--define fuel-debug--uses-buffer
67 "*fuel uses*" 'fuel-debug-uses-mode)
69 (make-variable-buffer-local
70 (defvar fuel-debug--uses-file nil))
72 (make-variable-buffer-local
73 (defvar fuel-debug--uses-restarts nil))
75 (defsubst fuel-debug--uses-insert-title ()
76 (insert "Inferring USING: stanza for " fuel-debug--uses-file ".\n\n"))
78 (defun fuel-debug--uses-prepare (file)
79 (fuel--with-popup (fuel-debug--uses-buffer)
80 (setq fuel-debug--uses-file file
81 fuel-debug--uses nil
82 fuel-debug--uses-restarts nil)
83 (erase-buffer)
84 (fuel-debug--uses-insert-title)))
86 (defun fuel-debug--uses-clean ()
87 (setq fuel-debug--uses-file nil
88 fuel-debug--uses nil
89 fuel-debug--uses-restarts nil))
91 (defun fuel-debug--uses-for-file (file)
92 (let* ((lines (fuel-debug--file-lines file))
93 (cmd `(:fuel ((V{ ,@lines } fuel-get-uses)) t t)))
94 (fuel-debug--uses-prepare file)
95 (fuel--with-popup (fuel-debug--uses-buffer)
96 (insert "Asking Factor. Please, wait ...\n")
97 (fuel-eval--send cmd 'fuel-debug--uses-cont))
98 (fuel-popup--display (fuel-debug--uses-buffer))))
100 (defun fuel-debug--uses-cont (retort)
101 (let ((uses (fuel-debug--uses retort))
102 (err (fuel-eval--retort-error retort)))
103 (if uses (fuel-debug--uses-display uses)
104 (fuel-debug--uses-display-err retort))))
106 (defun fuel-debug--uses-display (uses)
107 (let* ((inhibit-read-only t)
108 (old (with-current-buffer (find-file-noselect fuel-debug--uses-file)
109 (sort (fuel-syntax--find-usings t) 'string<)))
110 (new (sort uses 'string<)))
111 (erase-buffer)
112 (fuel-debug--uses-insert-title)
113 (if (equalp old new)
114 (progn
115 (insert "Current USING: is already fine!. Type 'q' to bury buffer.\n")
116 (fuel-debug--uses-clean))
117 (fuel-debug--highlight-names old new 'fuel-font-lock-debug-unneeded-vocab)
118 (fuel-debug--highlight-names new old 'fuel-font-lock-debug-missing-vocab)
119 (fuel-debug--insert-vlist "Current vocabulary list:" old)
120 (newline)
121 (fuel-debug--insert-vlist "Correct vocabulary list:" new)
122 (setq fuel-debug--uses new)
123 (insert "\nType 'y' to update your USING: to the new one.\n"))))
125 (defun fuel-debug--uses-display-err (retort)
126 (let* ((inhibit-read-only t)
127 (err (fuel-eval--retort-error retort))
128 (restarts (fuel-debug--uses-filter (fuel-eval--error-restarts err)))
129 (unique (= 1 (length restarts))))
130 (erase-buffer)
131 (fuel-debug--uses-insert-title)
132 (insert (fuel-eval--retort-output retort))
133 (newline)
134 (if (not restarts)
135 (insert "\nSorry, couldn't infer the vocabulary list.\n")
136 (setq fuel-debug--uses-restarts restarts)
137 (if unique (fuel-debug--uses-restart 1)
138 (insert "\nPlease, type the number of the desired vocabulary:\n\n")
139 (dolist (r restarts)
140 (insert (format " :%s %s\n" (first r) (third r))))))))
142 (defun fuel-debug--uses-update-usings ()
143 (interactive)
144 (let ((inhibit-read-only t)
145 (file fuel-debug--uses-file)
146 (uses fuel-debug--uses))
147 (when (and uses file)
148 (insert "\nDone!")
149 (fuel-debug--uses-clean)
150 (fuel-popup--quit)
151 (fuel-debug--replace-usings file uses)
152 (message "USING: updated!"))))
154 (defun fuel-debug--uses-restart (n)
155 (when (and (> n 0) (<= n (length fuel-debug--uses-restarts)))
156 (let* ((inhibit-read-only t)
157 (restart (format ":%s" (cadr (nth (1- n) fuel-debug--uses-restarts))))
158 (cmd `(:fuel ([ (:factor ,restart) ] fuel-with-autouse) t t)))
159 (setq fuel-debug--uses-restarts nil)
160 (insert "\nAsking Factor. Please, wait ...\n")
161 (fuel-eval--send cmd 'fuel-debug--uses-cont))))
164 ;;; Fuel uses mode:
166 (defvar fuel-debug-uses-mode-map
167 (let ((map (make-keymap)))
168 (suppress-keymap map)
169 (dotimes (n 9)
170 (define-key map (vector (+ ?1 n))
171 `(lambda () (interactive) (fuel-debug--uses-restart ,(1+ n)))))
172 (define-key map "y" 'fuel-debug--uses-update-usings)
173 (define-key map "\C-c\C-c" 'fuel-debug--uses-update-usings)
174 map))
176 (defconst fuel-debug--uses-header-regex
177 (format "^%s.*$" (regexp-opt '("Inferring USING: stanza for "
178 "Current USING: is already fine!"
179 "Current vocabulary list:"
180 "Correct vocabulary list:"
181 "Sorry, couldn't infer the vocabulary list."
182 "Done!"))))
184 (defconst fuel-debug--uses-prompt-regex
185 (format "^%s" (regexp-opt '("Asking Factor. Please, wait ..."
186 "Please, type the number of the desired vocabulary:"
187 "Type 'y' to update your USING: to the new one."))))
189 (defconst fuel-debug--uses-font-lock-keywords
190 `((,fuel-debug--uses-header-regex . 'fuel-font-lock-debug-uses-header)
191 (,fuel-debug--uses-prompt-regex . 'fuel-font-lock-debug-uses-prompt)
192 (,fuel-debug--restart-regex (1 'fuel-font-lock-debug-restart-number)
193 (2 'fuel-font-lock-debug-restart-name))))
195 (defun fuel-debug-uses-mode ()
196 "A major mode for displaying Factor's USING: inference results."
197 (interactive)
198 (kill-all-local-variables)
199 (buffer-disable-undo)
200 (setq major-mode 'fuel-debug-uses-mode)
201 (setq mode-name "Fuel Uses:")
202 (set (make-local-variable 'font-lock-defaults)
203 '(fuel-debug--uses-font-lock-keywords t nil nil nil))
204 (use-local-map fuel-debug-uses-mode-map))
207 (provide 'fuel-debug-uses)
208 ;;; fuel-debug-uses.el ends here