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.
18 (require 'fuel-listener)
19 (require 'fuel-completion)
21 (require 'fuel-debug-uses)
25 (require 'fuel-refactor)
27 (require 'fuel-autodoc)
28 (require 'fuel-autohelp)
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-autohelp-p nil
47 "Whether `fuel-autohelp-mode' gets enabled by default in factor buffers."
52 (defcustom fuel-mode-stack-p nil
53 "Whether `fuel-stack-mode' gets enabled by default in factor buffers."
61 (defun fuel-run-file (&optional arg)
62 "Sends the current file to Factor for compilation.
63 With prefix argument, ask for the file to run."
65 (let* ((f/b (fuel-mode--read-file arg))
69 (with-current-buffer buffer
70 (let ((msg (format "Compiling %s ..." file)))
71 (fuel-debug--prepare-compilation file msg)
73 (fuel-eval--send `(:fuel (,file fuel-run-file))
74 `(lambda (r) (fuel--run-file-cont r ,file))))))))
76 (defun fuel--run-file-cont (ret file)
77 (if (fuel-debug--display-retort ret (format "%s successfully compiled" file))
78 (message "Compiling %s ... OK!" file)
81 (defun fuel-eval-region (begin end &optional arg)
82 "Sends region to Fuel's listener for evaluation.
83 Unless called with a prefix, switches to the compilation results
84 buffer in case of errors."
86 (let* ((rstr (buffer-substring begin end))
87 (lines (split-string (substring-no-properties rstr)
90 (cmd `(:fuel (,(mapcar (lambda (l) `(:factor ,l)) lines))))
91 (cv (factor-current-vocab)))
92 (fuel-debug--prepare-compilation (buffer-file-name)
93 (format "Evaluating:\n\n%s" rstr))
94 (fuel-debug--display-retort
95 (fuel-eval--send/wait cmd 10000)
97 (if cv (format "IN: %s " cv) "")
98 (fuel-shorten-region begin end 70))
101 (defun fuel-eval-extended-region (begin end &optional arg)
102 "Sends region, extended outwards to nearest definition,
103 to Fuel's listener for evaluation.
104 Unless called with a prefix, switches to the compilation results
105 buffer in case of errors."
107 (fuel-eval-region (save-excursion (goto-char begin) (mark-defun) (point))
108 (save-excursion (goto-char end) (mark-defun) (mark))
111 (defun fuel-eval-definition (&optional arg)
112 "Sends definition around point to Fuel's listener for evaluation.
113 Unless called with a prefix, switches to the compilation results
114 buffer in case of errors."
118 (let* ((begin (point))
120 (unless (< begin end) (error "No evaluable definition around point"))
121 (fuel-eval-region begin end arg))))
123 (defun fuel-load-usings ()
124 "Loads all vocabularies in the current buffer's USING: from.
125 Useful to activate autodoc help messages in a vocabulary not yet
126 loaded. See documentation for `fuel-autodoc-eval-using-form-p'
129 (message "Loading all vocabularies in USING: form ...")
130 (let ((err (fuel-eval--retort-error
131 (fuel-eval--send/wait '(:fuel* (t .) t :usings) 120000))))
132 (message (if err "Warning: some vocabularies failed to load"
133 "All vocabularies loaded"))))
136 ;;; Minor mode definition:
138 (defvar-local fuel-mode-string " F"
139 "Modeline indicator for fuel-mode")
141 (defvar fuel-mode-map (make-sparse-keymap)
142 "Key map for fuel-mode")
145 (define-minor-mode fuel-mode
147 With no argument, this command toggles the mode.
148 Non-null prefix argument turns on the mode.
149 Null prefix argument turns off the mode.
151 When Fuel mode is enabled, a host of nice utilities for
152 interacting with a factor listener is at your disposal.
155 :lighter fuel-mode-string
157 :keymap fuel-mode-map
159 (setq fuel-autodoc-mode-string "/A")
160 (when fuel-mode-autodoc-p (fuel-autodoc-mode fuel-mode))
162 (setq fuel-autohelp-mode-string "/H")
163 (when fuel-mode-autohelp-p (fuel-autohelp-mode fuel-mode))
165 (setq fuel-stack-mode-string "/S")
166 (when fuel-mode-stack-p (fuel-stack-mode fuel-mode))
168 (let ((file-name (buffer-file-name)))
171 (not (file-exists-p file-name)))
172 (fuel-scaffold--maybe-insert))))
177 (fuel-menu--defmenu fuel fuel-mode-map
178 ("Complete symbol" ((kbd "M-TAB"))
179 fuel-completion--complete-symbol :enable (symbol-at-point))
180 ("Update USING:" ("\C-c\C-e\C-u" "\C-c\C-eu") fuel-update-usings)
182 ("Eval definition" ("\C-\M-x" "\C-c\C-e\C-x" "\C-c\C-ex")
183 fuel-eval-definition)
184 ("Eval extended region" ("\C-\M-r" "\C-c\C-e\C-e" "\C-c\C-ee")
185 fuel-eval-extended-region :enable mark-active)
186 ("Eval region" ("\C-c\C-r" "\C-c\C-e\C-r" "\C-c\C-er")
187 fuel-eval-region :enable mark-active)
189 ("Edit word at point" ("\M-." "\C-c\C-e\C-d" "\C-c\C-ed")
190 fuel-edit-word-at-point :enable (symbol-at-point))
191 ("Edit word..." ("\C-c\C-e\C-w" "\C-c\C-ew") fuel-edit-word)
192 ("Edit vocab..." ("\C-c\C-e\C-v" "\C-c\C-ev") fuel-edit-vocabulary)
193 ("Jump back" "\M-," fuel-edit-pop-edit-word-stack)
195 ("Help on word" ("\C-c\C-d\C-d" "\C-c\C-dd") fuel-help)
196 ("Short help on word" ("\C-c\C-d\C-s" "\C-c\C-ds") fuel-help)
197 ("Apropos..." ("\C-c\C-d\C-p" "\C-c\C-dp") fuel-apropos)
198 ("Show stack effect" ("\C-c\C-d\C-e" "\C-c\C-de") fuel-stack-effect-sexp)
200 ("Show all words" ("\C-c\C-d\C-v" "\C-c\C-dv") fuel-show-file-words)
201 ("Word callers" "\C-c\M-<" fuel-show-callers :enable (symbol-at-point))
202 ("Word callees" "\C-c\M->" fuel-show-callees :enable (symbol-at-point))
203 (mode "Autodoc mode" ("\C-c\C-d\C-a" "\C-c\C-da") fuel-autodoc-mode)
206 ("Rename word" ("\C-c\C-x\C-w" "\C-c\C-xw") fuel-refactor-rename-word)
207 ("Inline word" ("\C-c\C-x\C-i" "\C-c\C-xi") fuel-refactor-inline-word)
208 ("Extract region" ("\C-c\C-x\C-r" "\C-c\C-xr")
209 fuel-refactor-extract-region :enable mark-active)
210 ("Extract subregion" ("\C-c\C-x\C-s" "\C-c\C-xs")
211 fuel-refactor-extract-sexp)
212 ("Extract vocab" ("\C-c\C-x\C-v" "\C-c\C-xv")
213 fuel-refactor-extract-vocab)
214 ("Make generic" ("\C-c\C-x\C-g" "\C-c\C-xg")
215 fuel-refactor-make-generic)
217 ("Extract article" ("\C-c\C-x\C-a" "\C-c\C-xa")
218 fuel-refactor-extract-article))
220 ("Load used vocabs" ("\C-c\C-e\C-l" "\C-c\C-el") fuel-load-usings)
221 ("Run file" ("\C-c\C-k" "\C-c\C-l" "\C-c\C-e\C-k") fuel-run-file)
222 ("Run unit tests" "\C-c\C-t" fuel-test-vocab)
223 ("Refresh vocabs" "\C-c\C-r" fuel-refresh-all)
226 ("Listener" "\C-c\C-z" run-factor)
227 ("Related Factor file" "\C-c\C-o" factor-visit-other-file)
228 ("Other Factor buffer" "\C-c\C-s" fuel-switch-to-buffer)
229 ("Other Factor buffer other window" "\C-x4s"
230 fuel-switch-to-buffer-other-window)
231 ("Other Factor buffer other frame" "\C-x5s"
232 fuel-switch-to-buffer-other-frame)))
237 ;;; fuel-mode.el ends here