1 ;;; fuel-mode.el -- Minor mode enabling FUEL niceties
3 ;; Copyright (C) 2008, 2009, 2010 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: Sat Dec 06, 2008 00:52
12 ;; Enhancements to vanilla factor-mode (notably, listener interaction)
13 ;; enabled by means of a minor mode.
17 (require 'fuel-listener)
18 (require 'fuel-completion)
20 (require 'fuel-debug-uses)
24 (require 'fuel-refactor)
26 (require 'fuel-autodoc)
27 (require 'fuel-font-lock)
29 (require 'fuel-syntax)
36 (defgroup fuel-mode nil
37 "Mode enabling FUEL's ultimate abilities."
40 (defcustom fuel-mode-autodoc-p t
41 "Whether `fuel-autodoc-mode' gets enabled by default in factor buffers."
46 (defcustom fuel-mode-stack-p nil
47 "Whether `fuel-stack-mode' gets enabled by default in factor buffers."
55 (defun fuel-mode--read-file (arg)
56 (let* ((file (or (and arg (read-file-name "File: " nil (buffer-file-name) t))
58 (file (expand-file-name file))
59 (buffer (find-file-noselect file)))
61 (buffer-modified-p buffer)
62 (y-or-n-p "Save file? "))
66 (defun fuel-run-file (&optional arg)
67 "Sends the current file to Factor for compilation.
68 With prefix argument, ask for the file to run."
70 (let* ((f/b (fuel-mode--read-file arg))
74 (with-current-buffer buffer
75 (let ((msg (format "Compiling %s ..." file)))
76 (fuel-debug--prepare-compilation file msg)
78 (fuel-eval--send `(:fuel (,file fuel-run-file))
79 `(lambda (r) (fuel--run-file-cont r ,file))))))))
81 (defun fuel--run-file-cont (ret file)
82 (if (fuel-debug--display-retort ret (format "%s successfully compiled" file))
83 (message "Compiling %s ... OK!" file)
86 (defun fuel-eval-region (begin end &optional arg)
87 "Sends region to Fuel's listener for evaluation.
88 Unless called with a prefix, switches to the compilation results
89 buffer in case of errors."
91 (let* ((rstr (buffer-substring begin end))
92 (lines (split-string (substring-no-properties rstr)
95 (cmd `(:fuel (,(mapcar (lambda (l) `(:factor ,l)) lines))))
96 (cv (fuel-syntax--current-vocab)))
97 (fuel-debug--prepare-compilation (buffer-file-name)
98 (format "Evaluating:\n\n%s" rstr))
99 (fuel-debug--display-retort
100 (fuel-eval--send/wait cmd 10000)
102 (if cv (format "IN: %s " cv) "")
103 (fuel--shorten-region begin end 70))
106 (defun fuel-eval-extended-region (begin end &optional arg)
107 "Sends region, extended outwards to nearest definition,
108 to Fuel's listener for evaluation.
109 Unless called with a prefix, switches to the compilation results
110 buffer in case of errors."
112 (fuel-eval-region (save-excursion (goto-char begin) (mark-defun) (point))
113 (save-excursion (goto-char end) (mark-defun) (mark))
116 (defun fuel-eval-definition (&optional arg)
117 "Sends definition around point to Fuel's listener for evaluation.
118 Unless called with a prefix, switches to the compilation results
119 buffer in case of errors."
123 (let* ((begin (point))
125 (unless (< begin end) (error "No evaluable definition around point"))
126 (fuel-eval-region begin end arg))))
128 (defun fuel-update-usings (&optional arg)
129 "Asks factor for the vocabularies needed by this file,
130 optionally updating the its USING: line.
131 With prefix argument, ask for the file name."
133 (let ((file (car (fuel-mode--read-file arg))))
134 (when file (fuel-debug--uses-for-file file))))
136 (defun fuel-load-usings ()
137 "Loads all vocabularies in the current buffer's USING: from.
138 Useful to activate autodoc help messages in a vocabulary not yet
139 loaded. See documentation for `fuel-autodoc-eval-using-form-p'
142 (message "Loading all vocabularies in USING: form ...")
143 (let ((err (fuel-eval--retort-error
144 (fuel-eval--send/wait '(:fuel* (t .) t :usings) 120000))))
145 (message (if err "Warning: some vocabularies failed to load"
146 "All vocabularies loaded"))))
149 ;;; Minor mode definition:
151 (make-variable-buffer-local
152 (defvar fuel-mode-string " F"
153 "Modeline indicator for fuel-mode"))
155 (defvar fuel-mode-map (make-sparse-keymap)
156 "Key map for fuel-mode")
158 (define-minor-mode fuel-mode
160 With no argument, this command toggles the mode.
161 Non-null prefix argument turns on the mode.
162 Null prefix argument turns off the mode.
164 When Fuel mode is enabled, a host of nice utilities for
165 interacting with a factor listener is at your disposal.
168 :lighter fuel-mode-string
170 :keymap fuel-mode-map
172 (setq fuel-autodoc-mode-string "/A")
173 (when fuel-mode-autodoc-p (fuel-autodoc-mode fuel-mode))
175 (setq fuel-stack-mode-string "/S")
176 (when fuel-mode-stack-p (fuel-stack-mode fuel-mode))
178 (let ((file-name (buffer-file-name)))
181 (not (file-exists-p file-name)))
182 (fuel-scaffold--maybe-insert))))
187 (fuel-menu--defmenu fuel fuel-mode-map
188 ("Complete symbol" ((kbd "M-TAB"))
189 fuel-completion--complete-symbol :enable (symbol-at-point))
190 ("Update USING:" ("\C-c\C-e\C-u" "\C-c\C-eu") fuel-update-usings)
192 ("Eval definition" ("\C-\M-x" "\C-c\C-e\C-x" "\C-c\C-ex")
193 fuel-eval-definition)
194 ("Eval extended region" ("\C-\M-r" "\C-c\C-e\C-e" "\C-c\C-ee")
195 fuel-eval-extended-region :enable mark-active)
196 ("Eval region" ("\C-c\C-r" "\C-c\C-e\C-r" "\C-c\C-er")
197 fuel-eval-region :enable mark-active)
199 ("Edit word at point" ("\M-." "\C-c\C-e\C-d" "\C-c\C-ed")
200 fuel-edit-word-at-point :enable (symbol-at-point))
201 ("Edit word..." ("\C-c\C-e\C-w" "\C-c\C-ew") fuel-edit-word)
202 ("Edit vocab..." ("\C-c\C-e\C-v" "\C-c\C-ev") fuel-edit-vocabulary)
203 ("Jump back" "\M-," fuel-edit-pop-edit-word-stack)
205 ("Help on word" ("\C-c\C-d\C-d" "\C-c\C-dd") fuel-help)
206 ("Short help on word" ("\C-c\C-d\C-s" "\C-c\C-ds") fuel-help)
207 ("Apropos..." ("\C-c\C-d\C-p" "\C-c\C-dp") fuel-apropos)
208 ("Show stack effect" ("\C-c\C-d\C-e" "\C-c\C-de") fuel-stack-effect-sexp)
210 ("Show all words" ("\C-c\C-d\C-v" "\C-c\C-dv") fuel-show-file-words)
211 ("Word callers" "\C-c\M-<" fuel-show-callers :enable (symbol-at-point))
212 ("Word callees" "\C-c\M->" fuel-show-callees :enable (symbol-at-point))
213 (mode "Autodoc mode" ("\C-c\C-d\C-a" "\C-c\C-da") fuel-autodoc-mode)
216 ("Rename word" ("\C-c\C-x\C-w" "\C-c\C-xw") fuel-refactor-rename-word)
217 ("Inline word" ("\C-c\C-x\C-i" "\C-c\C-xi") fuel-refactor-inline-word)
218 ("Extract region" ("\C-c\C-x\C-r" "\C-c\C-xr")
219 fuel-refactor-extract-region :enable mark-active)
220 ("Extract subregion" ("\C-c\C-x\C-s" "\C-c\C-xs")
221 fuel-refactor-extract-sexp)
222 ("Extract vocab" ("\C-c\C-x\C-v" "\C-c\C-xv")
223 fuel-refactor-extract-vocab)
224 ("Make generic" ("\C-c\C-x\C-g" "\C-c\C-xg")
225 fuel-refactor-make-generic)
227 ("Extract article" ("\C-c\C-x\C-a" "\C-c\C-xa")
228 fuel-refactor-extract-article))
230 ("Load used vocabs" ("\C-c\C-e\C-l" "\C-c\C-el") fuel-load-usings)
231 ("Run file" ("\C-c\C-k" "\C-c\C-l" "\C-c\C-e\C-k") fuel-run-file)
232 ("Run unit tests" "\C-c\C-t" fuel-test-vocab)
233 ("Refresh vocabs" "\C-c\C-r" fuel-refresh-all)
236 ("Listener" "\C-c\C-z" run-factor)
237 ("Related Factor file" "\C-c\C-o" factor-mode-visit-other-file)
238 ("Other Factor buffer" "\C-c\C-s" fuel-switch-to-buffer)
239 ("Other Factor buffer other window" "\C-x4s"
240 fuel-switch-to-buffer-other-window)
241 ("Other Factor buffer other frame" "\C-x5s"
242 fuel-switch-to-buffer-other-frame)))
246 ;;; fuel-mode.el ends here