1 ;;; fuel-mode.el -- Minor mode enabling FUEL niceties
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: 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)
35 (defgroup fuel-mode nil
36 "Mode enabling FUEL's ultimate abilities."
39 (defcustom fuel-mode-autodoc-p t
40 "Whether `fuel-autodoc-mode' gets enabled by default in factor buffers."
45 (defcustom fuel-mode-stack-p nil
46 "Whether `fuel-stack-mode' gets enabled by default in factor buffers."
54 (defun fuel-mode--read-file (arg)
55 (let* ((file (or (and arg (read-file-name "File: " nil (buffer-file-name) t))
57 (file (expand-file-name file))
58 (buffer (find-file-noselect file)))
60 (buffer-modified-p buffer)
61 (y-or-n-p "Save file? "))
65 (defun fuel-run-file (&optional arg)
66 "Sends the current file to Factor for compilation.
67 With prefix argument, ask for the file to run."
69 (let* ((f/b (fuel-mode--read-file arg))
73 (with-current-buffer buffer
74 (let ((msg (format "Compiling %s ..." file)))
75 (fuel-debug--prepare-compilation file msg)
77 (fuel-eval--send `(:fuel (,file fuel-run-file))
78 `(lambda (r) (fuel--run-file-cont r ,file))))))))
80 (defun fuel--run-file-cont (ret file)
81 (if (fuel-debug--display-retort ret (format "%s successfully compiled" file))
82 (message "Compiling %s ... OK!" file)
85 (defun fuel-eval-region (begin end &optional arg)
86 "Sends region to Fuel's listener for evaluation.
87 Unless called with a prefix, switches to the compilation results
88 buffer in case of errors."
90 (let* ((rstr (buffer-substring begin end))
91 (lines (split-string (substring-no-properties rstr)
94 (cmd `(:fuel (,(mapcar (lambda (l) `(:factor ,l)) lines))))
95 (cv (fuel-syntax--current-vocab)))
96 (fuel-debug--prepare-compilation (buffer-file-name)
97 (format "Evaluating:\n\n%s" rstr))
98 (fuel-debug--display-retort
99 (fuel-eval--send/wait cmd 10000)
101 (if cv (format "IN: %s " cv) "")
102 (fuel--shorten-region begin end 70))
105 (defun fuel-eval-extended-region (begin end &optional arg)
106 "Sends region, extended outwards to nearest definition,
107 to Fuel's listener for evaluation.
108 Unless called with a prefix, switches to the compilation results
109 buffer in case of errors."
111 (fuel-eval-region (save-excursion (goto-char begin) (mark-defun) (point))
112 (save-excursion (goto-char end) (mark-defun) (mark))
115 (defun fuel-eval-definition (&optional arg)
116 "Sends definition around point to Fuel's listener for evaluation.
117 Unless called with a prefix, switches to the compilation results
118 buffer in case of errors."
122 (let* ((begin (point))
124 (unless (< begin end) (error "No evaluable definition around point"))
125 (fuel-eval-region begin end arg))))
127 (defun fuel-update-usings (&optional arg)
128 "Asks factor for the vocabularies needed by this file,
129 optionally updating the its USING: line.
130 With prefix argument, ask for the file name."
132 (let ((file (car (fuel-mode--read-file arg))))
133 (when file (fuel-debug--uses-for-file file))))
135 (defun fuel-load-usings ()
136 "Loads all vocabularies in the current buffer's USING: from.
137 Useful to activate autodoc help messages in a vocabulary not yet
138 loaded. See documentation for `fuel-autodoc-eval-using-form-p'
141 (message "Loading all vocabularies in USING: form ...")
142 (let ((err (fuel-eval--retort-error
143 (fuel-eval--send/wait '(:fuel* (t) t :usings) 120000))))
144 (message (if err "Warning: some vocabularies failed to load"
145 "All vocabularies loaded"))))
148 ;;; Minor mode definition:
150 (make-variable-buffer-local
151 (defvar fuel-mode-string " F"
152 "Modeline indicator for fuel-mode"))
154 (defvar fuel-mode-map (make-sparse-keymap)
155 "Key map for fuel-mode")
157 (define-minor-mode fuel-mode
159 With no argument, this command toggles the mode.
160 Non-null prefix argument turns on the mode.
161 Null prefix argument turns off the mode.
163 When Fuel mode is enabled, a host of nice utilities for
164 interacting with a factor listener is at your disposal.
167 :lighter fuel-mode-string
169 :keymap fuel-mode-map
171 (setq fuel-autodoc-mode-string "/A")
172 (when fuel-mode-autodoc-p (fuel-autodoc-mode fuel-mode))
174 (setq fuel-stack-mode-string "/S")
175 (when fuel-mode-stack-p (fuel-stack-mode fuel-mode)))
180 (defun fuel-mode--key-1 (k c)
181 (define-key fuel-mode-map (vector '(control ?c) k) c)
182 (define-key fuel-mode-map (vector '(control ?c) `(control ,k)) c))
184 (defun fuel-mode--key (p k c)
185 (define-key fuel-mode-map (vector '(control ?c) `(control ,p) k) c)
186 (define-key fuel-mode-map (vector '(control ?c) `(control ,p) `(control ,k)) c))
188 (fuel-mode--key-1 ?k 'fuel-run-file)
189 (fuel-mode--key-1 ?l 'fuel-run-file)
190 (fuel-mode--key-1 ?r 'fuel-refresh-all)
191 (fuel-mode--key-1 ?z 'run-factor)
192 (fuel-mode--key-1 ?s 'fuel-switch-to-buffer)
193 (define-key fuel-mode-map "\C-x4s" 'fuel-switch-to-buffer-other-window)
194 (define-key fuel-mode-map "\C-x5s" 'fuel-switch-to-buffer-other-frame)
196 (define-key fuel-mode-map "\C-\M-x" 'fuel-eval-definition)
197 (define-key fuel-mode-map "\C-\M-r" 'fuel-eval-extended-region)
198 (define-key fuel-mode-map "\M-." 'fuel-edit-word-at-point)
199 (define-key fuel-mode-map "\M-," 'fuel-edit-pop-edit-word-stack)
200 (define-key fuel-mode-map "\C-c\M-<" 'fuel-show-callers)
201 (define-key fuel-mode-map "\C-c\M->" 'fuel-show-callees)
202 (define-key fuel-mode-map (kbd "M-TAB") 'fuel-completion--complete-symbol)
204 (fuel-mode--key ?e ?d 'fuel-edit-word-doc-at-point)
205 (fuel-mode--key ?e ?e 'fuel-eval-extended-region)
206 (fuel-mode--key ?e ?k 'fuel-run-file)
207 (fuel-mode--key ?e ?l 'fuel-load-usings)
208 (fuel-mode--key ?e ?r 'fuel-eval-region)
209 (fuel-mode--key ?e ?u 'fuel-update-usings)
210 (fuel-mode--key ?e ?v 'fuel-edit-vocabulary)
211 (fuel-mode--key ?e ?w 'fuel-edit-word)
212 (fuel-mode--key ?e ?x 'fuel-eval-definition)
214 (fuel-mode--key ?x ?a 'fuel-refactor-extract-article)
215 (fuel-mode--key ?x ?i 'fuel-refactor-inline-word)
216 (fuel-mode--key ?x ?r 'fuel-refactor-extract-region)
217 (fuel-mode--key ?x ?s 'fuel-refactor-extract-sexp)
218 (fuel-mode--key ?x ?v 'fuel-refactor-extract-vocab)
219 (fuel-mode--key ?x ?w 'fuel-refactor-rename-word)
221 (fuel-mode--key ?d ?> 'fuel-show-callees)
222 (fuel-mode--key ?d ?< 'fuel-show-callers)
223 (fuel-mode--key ?d ?v 'fuel-show-file-words)
224 (fuel-mode--key ?d ?a 'fuel-autodoc-mode)
225 (fuel-mode--key ?d ?p 'fuel-apropos)
226 (fuel-mode--key ?d ?d 'fuel-help)
227 (fuel-mode--key ?d ?e 'fuel-stack-effect-sexp)
228 (fuel-mode--key ?d ?s 'fuel-help-short)
232 ;;; fuel-mode.el ends here