]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/nxhtml/related/moz.el
remove toolbar and menubar
[.emacs.d.git] / emacs / nxhtml / related / 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 (require 'cc-cmds)
79
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)
90     map))
91
92 ;;;###autoload
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.
98
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
102 started as needed).
103
104 The following keys are bound in this minor mode:
105
106 \\{moz-minor-mode-map}"
107   nil
108   " Moz"
109   :keymap moz-minor-mode-map
110   :group 'moz)
111
112 (defalias 'run-mozilla 'inferior-moz-switch-to-mozilla)
113
114 (defvar moz-repl-name "repl"
115   "The current name of the repl.")
116
117 (defvar moz-input-separator "\n--end-remote-input\n")
118
119 (defvar moz-repl-host "localhost")
120
121 (defvar moz-repl-port 4242)
122
123 (defvar moz-temporary-file nil)
124
125 (defun moz-temporary-file ()
126   (if (and moz-temporary-file
127            (file-readable-p moz-temporary-file))
128       moz-temporary-file
129     (setq moz-temporary-file (make-temp-file "emacs-mozrepl"))))
130
131 (defun moz-send-region (start end)
132   "Send the region to Firefox via MozRepl."
133   (interactive "r")
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'); "
138                               "undefined; \n"))
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.
141   (sleep-for 0 1)
142   (comint-send-region (inferior-moz-process)
143                       start end)
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'); "
148                               "undefined; \n"))
149   (comint-send-string (inferior-moz-process)
150                       "\n--end-remote-input\n")
151   (display-buffer (process-buffer (inferior-moz-process))))
152
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."
156   (interactive)
157   (save-excursion
158     (c-mark-function)
159     (moz-send-region (point) (mark))))
160
161 (defun moz-send-defun-and-go ()
162   "Send the current function to Firefox via MozRepl.
163 Also switch to the interaction buffer."
164   (interactive)
165   (moz-send-defun)
166   (inferior-moz-switch-to-mozilla nil))
167
168 (defun moz-save-buffer-and-send ()
169   "Save the current buffer and load it in Firefox via MozRepl."
170   (interactive)
171   (save-buffer)
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))))
180
181 ;;; Inferior Mode
182
183 (defvar inferior-moz-buffer nil
184   "The buffer in which the inferior process is running.")
185
186 (defun inferior-moz-insert-moz-repl ()
187   "Insert value of `moz-repl-name' and a dot (.)."
188   (interactive) (insert moz-repl-name "."))
189
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)
194     map))
195
196 ;;;###autoload
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))
201
202 (defun inferior-moz-track-repl-name (comint-output)
203   (save-match-data
204     (when (string-match "\\(\\w+\\)> $" comint-output)
205       (setq moz-repl-name (match-string 1 comint-output)))))
206
207 (defun inferior-moz-self-insert-or-repl-name ()
208   (interactive)
209   (if (looking-back "\\(\\w+\\)> $")
210       (insert moz-repl-name ".")
211     (insert last-command-event)))
212
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")))
220
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
224 Firefox if needed.
225
226 See also `inferior-moz-start-process'."
227   (interactive "P")
228   (when arg
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))))
233
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))
239       (progn
240         (inferior-moz-start-process)
241         (inferior-moz-process))))
242
243 (defvar mozrepl-home-page "http://hyperstruct.net/projects/mozrepl")
244
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.
249
250 Note that you have to start the MozRepl server from Firefox."
251   (interactive)
252   (condition-case err
253       (progn
254         (setq inferior-moz-buffer
255               (apply 'make-comint "MozRepl" (cons moz-repl-host moz-repl-port) nil nil))
256         (sleep-for 0 100)
257         (with-current-buffer inferior-moz-buffer
258           (inferior-moz-mode)
259           (run-hooks 'inferior-moz-hook)))
260     (file-error
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)
266                  "\n"
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"
271                  "\n"
272                  "\nSee ")
273          (insert-text-button
274           "MozRepl home page"
275           'action (lambda (button)
276                     (browse-url mozrepl-home-page))
277           'help-echo mozrepl-home-page
278           'face 'button)
279          (insert
280           " for more information."
281           "\n"
282           "\nMozRepl is also available directly from Firefox add-on"
283           "\npages, but is updated less frequently there.")
284          ))
285      (error "Can't start MozRepl"))))
286
287 (provide 'moz)
288
289 ;;; moz.el ends here