]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/moz.el
a11324c79a37e0971b6d89a62635af3e3e8935c4
[.emacs.d.git] / emacs / moz.el
1 ;;; moz.el --- Lets current buffer interact with inferior mozilla.
2
3 ;; URL: http://github.com/bard/mozrepl/raw/master/chrome/content/moz.el
4
5 ;; Copyright (C) 2006 by Massimiliano Mirra
6 ;;
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.
11 ;;
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.
16 ;;
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
20 ;;
21 ;; Author: Massimiliano Mirra, <bard [at] hyperstruct [dot] net>
22 ;; Contributors:
23 ;;   - Lennart Borgman
24
25 ;;; Commentary:
26 ;;
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.
31 ;;
32 ;; This file contains
33 ;;
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'.
38 ;;
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):
42 ;;
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))
48 ;;
49 ;; Replace javascript-mode above with the name of your favorite
50 ;; javascript mode.
51 ;;
52 ;; If you got this with nXhtml the setup above is already done for
53 ;; you.
54 ;;
55 ;; *Note 1* You have to start the MozRepl server in Firefox (or
56 ;;  whatever Mozilla browser you use). From the menus do
57 ;;
58 ;;    Tools - MozRepl - Start
59 ;;
60 ;; *Note 2* For clearness and brevity the documentation says Firefox
61 ;;  where the correct term should rather be "your Mozilla web
62 ;;  browser".
63
64 ;;; Change log:
65 ;;
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.
74
75 ;;; Code:
76
77 (require 'comint)
78
79 ;; Maybe fix-me: C-c control-char are reserved for major modes. But
80 ;; this minor mode is used in only one major mode (or one family of
81 ;; major modes) so it complies I think ...
82 (defvar moz-minor-mode-map
83   (let ((map (make-sparse-keymap)))
84     (define-key map "\C-c\C-s" 'run-mozilla)
85     (define-key map "\C-\M-x"  'moz-send-defun)
86     (define-key map "\C-c\C-c" 'moz-send-defun-and-go)
87     (define-key map "\C-c\C-r" 'moz-send-region)
88     (define-key map "\C-c\C-l" 'moz-save-buffer-and-send)
89     map))
90
91 ;;;###autoload
92 (define-minor-mode moz-minor-mode
93   "MozRepl minor mode for interaction with Firefox.
94 With no argument, this command toggles the mode.
95 Non-null prefix argument turns on the mode.
96 Null prefix argument turns off the mode.
97
98 When this minor mode is enabled, some commands become available
99 to send current code area \(as understood by c-mark-function) or
100 region or buffer to an inferior MozRepl process (which will be
101 started as needed).
102
103 The following keys are bound in this minor mode:
104
105 \\{moz-minor-mode-map}"
106   nil
107   " Moz"
108   :keymap moz-minor-mode-map
109   :group 'moz)
110
111 (defalias 'run-mozilla 'inferior-moz-switch-to-mozilla)
112
113 (defvar moz-repl-name "repl"
114   "The current name of the repl.")
115
116 (defvar moz-input-separator "\n--end-remote-input\n")
117
118 (defvar moz-repl-host "localhost")
119
120 (defvar moz-repl-port 4242)
121
122 (defun moz-temporary-file ()
123   (if (and moz-temporary-file
124            (file-readable-p moz-temporary-file))
125       moz-temporary-file
126     (setq moz-temporary-file (make-temp-file "emacs-mozrepl"))))
127
128 (defun moz-send-region (start end)
129   "Send the region to Firefox via MozRepl."
130   (interactive "r")
131   (comint-send-string (inferior-moz-process)
132                       (concat moz-repl-name ".pushenv('printPrompt', 'inputMode'); "
133                               moz-repl-name ".setenv('printPrompt', false); "
134                               moz-repl-name ".setenv('inputMode', 'multiline'); "
135                               "undefined; \n"))
136   ;; Give the previous line a chance to be evaluated on its own.  If
137   ;; it gets concatenated to the following ones, we are doomed.
138   (sleep-for 0 1)
139   (comint-send-region (inferior-moz-process)
140                       start end)
141   (comint-send-string (inferior-moz-process)
142                       "\n--end-remote-input\n")
143   (comint-send-string (inferior-moz-process)
144                       (concat moz-repl-name ".popenv('inputMode', 'printPrompt'); "
145                               "undefined; \n"))
146   (comint-send-string (inferior-moz-process)
147                       "\n--end-remote-input\n")
148   (display-buffer (process-buffer (inferior-moz-process))))
149
150 (defun moz-send-defun ()
151   "Send the current function to Firefox via MozRepl.
152 Curent function is the one recognized by c-mark-function."
153   (interactive)
154   (save-excursion
155     (mark-defun)
156     (moz-send-region (point) (mark))))
157
158 (defun moz-send-defun-and-go ()
159   "Send the current function to Firefox via MozRepl.
160 Also switch to the interaction buffer."
161   (interactive)
162   (moz-send-defun)
163   (inferior-moz-switch-to-mozilla nil))
164
165 (defun moz-save-buffer-and-send ()
166   "Save the current buffer and load it in Firefox via MozRepl."
167   (interactive)
168   (save-buffer)
169   (comint-send-string (inferior-moz-process)
170                       (concat moz-repl-name ".pushenv('printPrompt', 'inputMode'); "
171                               moz-repl-name ".setenv('inputMode', 'line'); "
172                               moz-repl-name ".setenv('printPrompt', false); undefined; "))
173   (comint-send-string (inferior-moz-process)
174                       (concat moz-repl-name ".load('file://localhost/" (buffer-file-name) "');\n"
175                               moz-repl-name ".popenv('inputMode', 'printPrompt'); undefined;\n"))
176   (display-buffer (process-buffer (inferior-moz-process))))
177
178 ;;; Inferior Mode
179
180 (defvar inferior-moz-buffer nil
181   "The buffer in which the inferior process is running.")
182
183 (defun inferior-moz-insert-moz-repl ()
184   "Insert value of `moz-repl-name' and a dot (.)."
185   (interactive) (insert moz-repl-name "."))
186
187 (defvar inferior-moz-mode-map
188   (let ((map (make-sparse-keymap)))
189     (define-key map "\C-cc" 'inferior-moz-insert-moz-repl)
190     map))
191
192 ;;;###autoload
193 (define-derived-mode inferior-moz-mode comint-mode "Inf-MozRepl"
194   "Major mode for interacting with Firefox via MozRepl."
195   (setq comint-input-sender 'inferior-moz-input-sender)
196   (add-hook 'comint-output-filter-functions 'inferior-moz-track-repl-name nil t))
197
198 (defun inferior-moz-track-repl-name (comint-output)
199   (when (string-match "\\(\\w+\\)> $" comint-output)
200     (setq moz-repl-name (match-string 1 comint-output))))
201
202 (defun inferior-moz-self-insert-or-repl-name ()
203   (interactive)
204   (if (looking-back "\\(\\w+\\)> $")
205       (insert moz-repl-name ".")
206     (insert last-command-char)))
207
208 (defun inferior-moz-input-sender (proc string)
209   "Custom function to send input with comint-send-input.
210 Instead of sending input and newline separately like in
211 comint-simple-send, here we *first* concatenate input and
212 newline, then send it all together.  This prevents newline to be
213 interpreted on its own."
214   (comint-send-string proc (concat string "\n")))
215
216 (defun inferior-moz-switch-to-mozilla (arg)
217   "Switch to the inferior MozRepl buffer.
218 Create the buffer and start the MozRepl process and connect to
219 Firefox if needed.
220
221 See also `inferior-moz-start-process'."
222   (interactive "P")
223   (when arg
224     (setq moz-repl-host (read-string "Host: " "localhost"))
225     (setq moz-repl-port (read-number "Port: " 4242)))
226   (pop-to-buffer (process-buffer (inferior-moz-process)))
227   (goto-char (process-mark (inferior-moz-process))))
228
229 (defun inferior-moz-process ()
230   "Return inferior MozRepl process.  Start it if necessary.
231 See also `inferior-moz-start-process'."
232   (or (if (buffer-live-p inferior-moz-buffer)
233           (get-buffer-process inferior-moz-buffer))
234       (progn
235         (inferior-moz-start-process)
236         (inferior-moz-process))))
237
238 (defun inferior-moz-start-process ()
239   "Start an inferior Mozrepl process and connect to Firefox.
240 It runs the hook `inferior-moz-hook' after starting the process
241 and setting up the inferior Firefox buffer.
242
243 Note that you have to start the MozRepl server from Firefox."
244   (interactive)
245   (condition-case err
246       (progn
247         (setq inferior-moz-buffer
248               (apply 'make-comint "MozRepl" (cons moz-repl-host moz-repl-port) nil nil))
249         (sleep-for 0 100)
250         (with-current-buffer inferior-moz-buffer
251           (inferior-moz-mode)
252           (run-hooks 'inferior-moz-hook)))
253     (file-error
254      (with-output-to-temp-buffer "*MozRepl Error*"
255        (with-current-buffer (get-buffer "*MozRepl Error*")
256          (insert "Can't start MozRepl, the error message was:\n\n     "
257                  (error-message-string err)
258                  "\n"
259                  "\nA possible reason is that you have not installed"
260                  "\nthe MozRepl add-on to Firefox or that you have not"
261                  "\nstarted it.  You start it from the menus in Firefox:"
262                  "\n\n     Tools / MozRepl / Start"
263                  "\n"
264                  "\nSee ")
265          (insert-text-button
266           "MozRepl home page"
267           'action (lambda (button)
268                     (browse-url
269                      "http://hyperstruct.net/projects/mozrepl")
270                     )
271           'face 'button)
272          (insert
273           " for more information."
274           "\n"
275           "\nMozRepl is also available directly from Firefox add-on"
276           "\npages, but is updated less frequently there.")
277          ))
278      (error "Can't start MozRepl"))))
279
280 (provide 'moz)
281
282 ;;; moz.el ends here