1 ;;; moz.el --- Lets current buffer interact with inferior mozilla.
3 ;; URL: http://github.com/bard/mozrepl/raw/master/chrome/content/moz.el
5 ;; Copyright (C) 2006 by Massimiliano Mirra
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program; if not, write to the Free Software
19 ;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ;; Author: Massimiliano Mirra, <bard [at] hyperstruct [dot] net>
27 ;; This file implements communication with Firefox via MozRepl
28 ;; (http://hyperstruct.net/projects/mozrepl). It is a slightly
29 ;; modified version of the file moz.el that comes with MozLab. To use
30 ;; it you have to install the MozRepl addon in Firefox.
34 ;; * a major mode for direct interaction in a buffer (as with
35 ;; telnet) with MozRepl, `inferior-moz-mode'.
36 ;; * a minor mode for sending code portions or whole files from
37 ;; other buffers to MozRepl, `moz-minor-mode'.
39 ;; Assuming you want to use javascript-mode to edit Javascript files,
40 ;; enter the following in your .emacs initialization file (from Emacs
41 ;; integration in the help text):
43 ;; (add-to-list 'auto-mode-alist '("\\.js$" . javascript-mode))
44 ;; (autoload 'inferior-moz-mode "moz" "MozRepl Inferior Mode" t)
45 ;; (autoload 'moz-minor-mode "moz" "MozRepl Minor Mode" t)
46 ;; (add-hook 'javascript-mode-hook 'javascript-moz-setup)
47 ;; (defun javascript-moz-setup () (moz-minor-mode 1))
49 ;; Replace javascript-mode above with the name of your favorite
52 ;; If you got this with nXhtml the setup above is already done for
55 ;; *Note 1* You have to start the MozRepl server in Firefox (or
56 ;; whatever Mozilla browser you use). From the menus do
58 ;; Tools - MozRepl - Start
60 ;; *Note 2* For clearness and brevity the documentation says Firefox
61 ;; where the correct term should rather be "your Mozilla web
66 ;; 2008-07-20: Lennart Borgman
67 ;; - Add `moz-minor-mode-map'.
68 ;; - Add `inferior-moz-insert-moz-repl'.
69 ;; - Add `inferior-moz-mode-map'.
70 ;; - Add doc strings to interactive functions.
71 ;; - Make minor enhancements to documentation etc.
72 ;; - Change Mozilla to Firefox/MozRepl for clarity and brevity.
73 ;; - Add error handling when starting MozRepl.
80 ;; Maybe fix-me: C-c control-char are reserved for major modes. But
81 ;; this minor mode is used in only one major mode (or one family of
82 ;; major modes) so it complies I think ...
83 (defvar moz-minor-mode-map
84 (let ((map (make-sparse-keymap)))
85 (define-key map "\C-c\C-s" 'run-mozilla)
86 (define-key map "\C-\M-x" 'moz-send-defun)
87 (define-key map "\C-c\C-c" 'moz-send-defun-and-go)
88 (define-key map "\C-c\C-r" 'moz-send-region)
89 (define-key map "\C-c\C-l" 'moz-save-buffer-and-send)
93 (define-minor-mode moz-minor-mode
94 "MozRepl minor mode for interaction with Firefox.
95 With no argument, this command toggles the mode.
96 Non-null prefix argument turns on the mode.
97 Null prefix argument turns off the mode.
99 When this minor mode is enabled, some commands become available
100 to send current code area \(as understood by c-mark-function) or
101 region or buffer to an inferior MozRepl process (which will be
104 The following keys are bound in this minor mode:
106 \\{moz-minor-mode-map}"
109 :keymap moz-minor-mode-map
112 (defalias 'run-mozilla 'inferior-moz-switch-to-mozilla)
114 (defvar moz-repl-name "repl"
115 "The current name of the repl.")
117 (defvar moz-input-separator "\n--end-remote-input\n")
119 (defvar moz-repl-host "localhost")
121 (defvar moz-repl-port 4242)
123 (defvar moz-temporary-file nil)
125 (defun moz-temporary-file ()
126 (if (and moz-temporary-file
127 (file-readable-p moz-temporary-file))
129 (setq moz-temporary-file (make-temp-file "emacs-mozrepl"))))
131 (defun moz-send-region (start end)
132 "Send the region to Firefox via MozRepl."
134 (comint-send-string (inferior-moz-process)
135 (concat moz-repl-name ".pushenv('printPrompt', 'inputMode'); "
136 moz-repl-name ".setenv('printPrompt', false); "
137 moz-repl-name ".setenv('inputMode', 'multiline'); "
139 ;; Give the previous line a chance to be evaluated on its own. If
140 ;; it gets concatenated to the following ones, we are doomed.
142 (comint-send-region (inferior-moz-process)
144 (comint-send-string (inferior-moz-process)
145 "\n--end-remote-input\n")
146 (comint-send-string (inferior-moz-process)
147 (concat moz-repl-name ".popenv('inputMode', 'printPrompt'); "
149 (comint-send-string (inferior-moz-process)
150 "\n--end-remote-input\n")
151 (display-buffer (process-buffer (inferior-moz-process))))
153 (defun moz-send-defun ()
154 "Send the current function to Firefox via MozRepl.
155 Curent function is the one recognized by c-mark-function."
159 (moz-send-region (point) (mark))))
161 (defun moz-send-defun-and-go ()
162 "Send the current function to Firefox via MozRepl.
163 Also switch to the interaction buffer."
166 (inferior-moz-switch-to-mozilla nil))
168 (defun moz-save-buffer-and-send ()
169 "Save the current buffer and load it in Firefox via MozRepl."
172 (comint-send-string (inferior-moz-process)
173 (concat moz-repl-name ".pushenv('printPrompt', 'inputMode'); "
174 moz-repl-name ".setenv('inputMode', 'line'); "
175 moz-repl-name ".setenv('printPrompt', false); undefined; "))
176 (comint-send-string (inferior-moz-process)
177 (concat moz-repl-name ".load('file://localhost/" (buffer-file-name) "');\n"
178 moz-repl-name ".popenv('inputMode', 'printPrompt'); undefined;\n"))
179 (display-buffer (process-buffer (inferior-moz-process))))
183 (defvar inferior-moz-buffer nil
184 "The buffer in which the inferior process is running.")
186 (defun inferior-moz-insert-moz-repl ()
187 "Insert value of `moz-repl-name' and a dot (.)."
188 (interactive) (insert moz-repl-name "."))
190 (defvar inferior-moz-mode-map
191 (let ((map (make-sparse-keymap)))
192 ;; Note: changed from C-c c which is reserved for users.
193 (define-key map "\C-c\C-c" 'inferior-moz-insert-moz-repl)
197 (define-derived-mode inferior-moz-mode comint-mode "Inf-MozRepl"
198 "Major mode for interacting with Firefox via MozRepl."
199 (setq comint-input-sender 'inferior-moz-input-sender)
200 (add-hook 'comint-output-filter-functions 'inferior-moz-track-repl-name nil t))
202 (defun inferior-moz-track-repl-name (comint-output)
204 (when (string-match "\\(\\w+\\)> $" comint-output)
205 (setq moz-repl-name (match-string 1 comint-output)))))
207 (defun inferior-moz-self-insert-or-repl-name ()
209 (if (looking-back "\\(\\w+\\)> $")
210 (insert moz-repl-name ".")
211 (insert last-command-event)))
213 (defun inferior-moz-input-sender (proc string)
214 "Custom function to send input with comint-send-input.
215 Instead of sending input and newline separately like in
216 comint-simple-send, here we *first* concatenate input and
217 newline, then send it all together. This prevents newline to be
218 interpreted on its own."
219 (comint-send-string proc (concat string "\n")))
221 (defun inferior-moz-switch-to-mozilla (arg)
222 "Switch to the inferior MozRepl buffer.
223 Create the buffer and start the MozRepl process and connect to
226 See also `inferior-moz-start-process'."
229 (setq moz-repl-host (read-string "Host: " "localhost"))
230 (setq moz-repl-port (read-number "Port: " 4242)))
231 (pop-to-buffer (process-buffer (inferior-moz-process)))
232 (goto-char (process-mark (inferior-moz-process))))
234 (defun inferior-moz-process ()
235 "Return inferior MozRepl process. Start it if necessary.
236 See also `inferior-moz-start-process'."
237 (or (if (buffer-live-p inferior-moz-buffer)
238 (get-buffer-process inferior-moz-buffer))
240 (inferior-moz-start-process)
241 (inferior-moz-process))))
243 (defvar mozrepl-home-page "http://hyperstruct.net/projects/mozrepl")
245 (defun inferior-moz-start-process ()
246 "Start an inferior Mozrepl process and connect to Firefox.
247 It runs the hook `inferior-moz-hook' after starting the process
248 and setting up the inferior Firefox buffer.
250 Note that you have to start the MozRepl server from Firefox."
254 (setq inferior-moz-buffer
255 (apply 'make-comint "MozRepl" (cons moz-repl-host moz-repl-port) nil nil))
257 (with-current-buffer inferior-moz-buffer
259 (run-hooks 'inferior-moz-hook)))
261 (with-output-to-temp-buffer (help-buffer)
262 (help-setup-xref (list #'describe-function 'inferior-moz-start-process) (interactive-p))
263 (with-current-buffer (help-buffer)
264 (insert "Can't start MozRepl, the error message was:\n\n "
265 (error-message-string err)
267 "\nA possible reason is that you have not installed"
268 "\nthe MozRepl add-on to Firefox or that you have not"
269 "\nstarted it. You start it from the menus in Firefox:"
270 "\n\n Tools / MozRepl / Start"
275 'action (lambda (button)
276 (browse-url mozrepl-home-page))
277 'help-echo mozrepl-home-page
280 " for more information."
282 "\nMozRepl is also available directly from Firefox add-on"
283 "\npages, but is updated less frequently there.")
285 (error "Can't start MozRepl"))))