add using
[factor/jcg.git] / misc / fuel / fuel-mode.el
blob88ad73864a31df751bb1bcfea97bafe5acee9216
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
10 ;;; Comentary:
12 ;; Enhancements to vanilla factor-mode (notably, listener interaction)
13 ;; enabled by means of a minor mode.
15 ;;; Code:
17 (require 'fuel-listener)
18 (require 'fuel-completion)
19 (require 'fuel-debug)
20 (require 'fuel-debug-uses)
21 (require 'fuel-eval)
22 (require 'fuel-help)
23 (require 'fuel-xref)
24 (require 'fuel-refactor)
25 (require 'fuel-stack)
26 (require 'fuel-autodoc)
27 (require 'fuel-font-lock)
28 (require 'fuel-edit)
29 (require 'fuel-syntax)
30 (require 'fuel-base)
33 ;;; Customization:
35 (defgroup fuel-mode nil
36 "Mode enabling FUEL's ultimate abilities."
37 :group 'fuel)
39 (defcustom fuel-mode-autodoc-p t
40 "Whether `fuel-autodoc-mode' gets enabled by default in factor buffers."
41 :group 'fuel-mode
42 :group 'fuel-autodoc
43 :type 'boolean)
45 (defcustom fuel-mode-stack-p nil
46 "Whether `fuel-stack-mode' gets enabled by default in factor buffers."
47 :group 'fuel-mode
48 :group 'fuel-stack
49 :type 'boolean)
52 ;;; User commands
54 (defun fuel-mode--read-file (arg)
55 (let* ((file (or (and arg (read-file-name "File: " nil (buffer-file-name) t))
56 (buffer-file-name)))
57 (file (expand-file-name file))
58 (buffer (find-file-noselect file)))
59 (when (and buffer
60 (buffer-modified-p buffer)
61 (y-or-n-p "Save file? "))
62 (save-buffer buffer))
63 (cons file buffer)))
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."
68 (interactive "P")
69 (let* ((f/b (fuel-mode--read-file arg))
70 (file (car f/b))
71 (buffer (cdr f/b)))
72 (when buffer
73 (with-current-buffer buffer
74 (let ((msg (format "Compiling %s ..." file)))
75 (fuel-debug--prepare-compilation file msg)
76 (message 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)
83 (message "")))
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."
89 (interactive "r\nP")
90 (let* ((rstr (buffer-substring begin end))
91 (lines (split-string (substring-no-properties rstr)
92 "[\f\n\r\v]+"
93 t))
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)
100 (format "%s%s"
101 (if cv (format "IN: %s " cv) "")
102 (fuel--shorten-region begin end 70))
103 arg)))
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."
110 (interactive "r\nP")
111 (fuel-eval-region (save-excursion (goto-char begin) (mark-defun) (point))
112 (save-excursion (goto-char end) (mark-defun) (mark))
113 arg))
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."
119 (interactive "P")
120 (save-excursion
121 (mark-defun)
122 (let* ((begin (point))
123 (end (mark)))
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."
131 (interactive "P")
132 (let ((file (car (fuel-mode--read-file arg))))
133 (when file (fuel-debug--uses-for-file file))))
136 ;;; Minor mode definition:
138 (make-variable-buffer-local
139 (defvar fuel-mode-string " F"
140 "Modeline indicator for fuel-mode"))
142 (defvar fuel-mode-map (make-sparse-keymap)
143 "Key map for fuel-mode")
145 (define-minor-mode fuel-mode
146 "Toggle Fuel's 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.
153 \\{fuel-mode-map}"
154 :init-value nil
155 :lighter fuel-mode-string
156 :group 'fuel
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-stack-mode-string "/S")
163 (when fuel-mode-stack-p (fuel-stack-mode fuel-mode)))
166 ;;; Keys:
168 (defun fuel-mode--key-1 (k c)
169 (define-key fuel-mode-map (vector '(control ?c) k) c)
170 (define-key fuel-mode-map (vector '(control ?c) `(control ,k)) c))
172 (defun fuel-mode--key (p k c)
173 (define-key fuel-mode-map (vector '(control ?c) `(control ,p) k) c)
174 (define-key fuel-mode-map (vector '(control ?c) `(control ,p) `(control ,k)) c))
176 (fuel-mode--key-1 ?k 'fuel-run-file)
177 (fuel-mode--key-1 ?l 'fuel-run-file)
178 (fuel-mode--key-1 ?r 'fuel-refresh-all)
179 (fuel-mode--key-1 ?z 'run-factor)
180 (fuel-mode--key-1 ?s 'fuel-switch-to-buffer)
181 (define-key fuel-mode-map "\C-x4s" 'fuel-switch-to-buffer-other-window)
182 (define-key fuel-mode-map "\C-x5s" 'fuel-switch-to-buffer-other-frame)
184 (define-key fuel-mode-map "\C-\M-x" 'fuel-eval-definition)
185 (define-key fuel-mode-map "\C-\M-r" 'fuel-eval-extended-region)
186 (define-key fuel-mode-map "\M-." 'fuel-edit-word-at-point)
187 (define-key fuel-mode-map "\M-," 'fuel-edit-pop-edit-word-stack)
188 (define-key fuel-mode-map "\C-c\M-<" 'fuel-show-callers)
189 (define-key fuel-mode-map "\C-c\M->" 'fuel-show-callees)
190 (define-key fuel-mode-map (kbd "M-TAB") 'fuel-completion--complete-symbol)
192 (fuel-mode--key ?e ?d 'fuel-edit-word-doc-at-point)
193 (fuel-mode--key ?e ?e 'fuel-eval-extended-region)
194 (fuel-mode--key ?e ?l 'fuel-run-file)
195 (fuel-mode--key ?e ?r 'fuel-eval-region)
196 (fuel-mode--key ?e ?u 'fuel-update-usings)
197 (fuel-mode--key ?e ?v 'fuel-edit-vocabulary)
198 (fuel-mode--key ?e ?w 'fuel-edit-word)
199 (fuel-mode--key ?e ?x 'fuel-eval-definition)
201 (fuel-mode--key ?x ?i 'fuel-refactor-inline-word)
202 (fuel-mode--key ?x ?r 'fuel-refactor-extract-region)
203 (fuel-mode--key ?x ?s 'fuel-refactor-extract-sexp)
204 (fuel-mode--key ?x ?v 'fuel-refactor-extract-vocab)
205 (fuel-mode--key ?x ?w 'fuel-refactor-rename-word)
207 (fuel-mode--key ?d ?> 'fuel-show-callees)
208 (fuel-mode--key ?d ?< 'fuel-show-callers)
209 (fuel-mode--key ?d ?v 'fuel-show-file-words)
210 (fuel-mode--key ?d ?a 'fuel-autodoc-mode)
211 (fuel-mode--key ?d ?p 'fuel-apropos)
212 (fuel-mode--key ?d ?d 'fuel-help)
213 (fuel-mode--key ?d ?e 'fuel-stack-effect-sexp)
214 (fuel-mode--key ?d ?s 'fuel-help-short)
217 (provide 'fuel-mode)
218 ;;; fuel-mode.el ends here