comparison .emacs.d/inf-groovy.el @ 0:2764b4f45f9f

1st commit
author Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
date Mon, 21 Apr 2014 04:30:59 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:2764b4f45f9f
1 ;;; -*-Emacs-Lisp-*-
2 ;;;
3 ;;; Stuart Clayman sclayman@ee.ucl.ac.uk
4 ;;; 2006-08-01 v1
5 ;;; 2010-04-07 v2 fixes for new groovy-mode
6 ;;;
7 ;;; Inferior Groovy Mode - groovy process in a buffer.
8 ;;; adapted from cmuscheme.el and inf-haskell.el
9 ;;;
10 ;;; Usage:
11 ;;;
12 ;;; (1) modify .emacs to use groovy-mode
13 ;;; for example :
14 ;;;
15 ;;; (autoload 'groovy-mode "groovy-mode"
16 ;;; "Mode for editing groovy source files" t)
17 ;;; (setq auto-mode-alist
18 ;;; (append '(("\\.groovy$" . groovy-mode)) auto-mode-alist))
19 ;;; (setq interpreter-mode-alist (append '(("groovy" . groovy-mode))
20 ;;; interpreter-mode-alist))
21 ;;;
22 ;;; (2) set to load inf-groovy and set inf-groovy key definition in groovy-mode.
23 ;;;
24 ;;; (autoload 'groovy-mode "groovy-mode" "Groovy mode." t)
25 ;;; (autoload 'run-groovy "inf-groovy" "Run an inferior Groovy process")
26 ;;; (autoload 'inf-groovy-keys "inf-groovy" "Set local key defs for inf-groovy in groovy-mode")
27 ;;;
28 ;;; (add-hook 'groovy-mode-hook
29 ;;; '(lambda ()
30 ;;; (inf-groovy-keys)
31 ;;; ))
32 ;;;
33 ;;; ;; can set groovy-home here, if not in environment
34 ;;; (setq inferior-groovy-mode-hook
35 ;;; '(lambda()
36 ;;; (setq groovy-home "/Users/sclayman/Downloads/groovy-1.7.1/")
37 ;;; ))
38 ;;;
39 ;;; (3) execute
40 ;;; M-x run-groovy
41 ;;;
42
43 (require 'comint)
44 (require 'compile)
45 (require 'groovy-mode)
46
47 ;;;; for groovy
48 (defvar groovy-home (getenv "GROOVY_HOME"))
49
50 (defvar groovy-program-name "groovysh --color=false"
51 "*Program invoked by the run-groovy command")
52
53 (defvar inferior-groovy-first-prompt-pattern "^groovy:.*> *"
54 "first prompt regex pattern of groovy interpreter.")
55
56 (defvar inferior-groovy-prompt-pattern "^groovy:.*> *"
57 "prompt regex pattern of groovy interpreter.")
58
59 ;;
60 ;; mode variables
61 ;;
62 (defvar inferior-groovy-mode-hook nil
63 "*Hook for customising inferior-groovy mode.")
64
65 (defvar inferior-groovy-mode-map nil
66 "*Mode map for inferior-groovy-mode")
67
68 (defconst inferior-groovy-error-regexp-alist
69 '(("SyntaxError: compile error\n^\\([^\(].*\\):\\([1-9][0-9]*\\):" 1 2)
70 ("^\tfrom \\([^\(].*\\):\\([1-9][0-9]*\\)\\(:in `.*'\\)?$" 1 2)))
71
72 (cond ((not inferior-groovy-mode-map)
73 (setq inferior-groovy-mode-map
74 (copy-keymap comint-mode-map))
75 ; (define-key inferior-groovy-mode-map "\M-\C-x" ;gnu convention
76 ; 'groovy-send-definition)
77 ; (define-key inferior-groovy-mode-map "\C-x\C-e" 'groovy-send-last-sexp)
78 (define-key inferior-groovy-mode-map "\C-c\C-l" 'groovy-load-file)
79 (define-key inferior-groovy-mode-map "\C-c\C-m" 'inferior-groovy-newline-and-go)
80 ))
81
82 (defun inf-groovy-keys ()
83 "Set local key defs for inf-groovy in groovy-mode"
84 (define-key groovy-mode-map "\M-\C-x" 'groovy-send-definition)
85 (define-key groovy-mode-map "\C-x\C-e" 'groovy-send-last-sexp)
86 ;;(define-key groovy-mode-map "\C-c\M-b" 'groovy-send-block)
87 ;;(define-key groovy-mode-map "\C-c\C-b" 'groovy-send-block-and-go)
88 (define-key groovy-mode-map "\C-c\M-d" 'groovy-send-definition)
89 (define-key groovy-mode-map "\C-c\C-x" 'groovy-send-definition-and-go)
90 (define-key groovy-mode-map "\C-c\C-x" 'groovy-send-definition-and-go)
91 (define-key groovy-mode-map "\C-c\M-r" 'groovy-send-region)
92 (define-key groovy-mode-map "\C-c\C-r" 'groovy-send-region-and-go)
93 (define-key groovy-mode-map "\C-c\C-z" 'switch-to-groovy)
94 (define-key groovy-mode-map "\C-c\C-l" 'groovy-load-file)
95 (define-key groovy-mode-map "\C-c\C-s" 'run-groovy)
96 )
97
98 (defvar groovy-buffer nil "current groovy (actually groovysh) process buffer.")
99
100 (defun inferior-groovy-mode ()
101 "Major mode for interacting with an inferior groovy (groovysh) process.
102
103 The following commands are available:
104 \\{inferior-groovy-mode-map}
105
106 A groovy process can be fired up with M-x run-groovy.
107
108 Customisation: Entry to this mode runs the hooks on comint-mode-hook and
109 inferior-groovy-mode-hook (in that order).
110
111 You can send text to the inferior groovy process from other buffers containing
112 Groovy source.
113 switch-to-groovy switches the current buffer to the groovy process buffer.
114 groovy-send-definition sends the current definition to the groovy process.
115 groovy-send-region sends the current region to the groovy process.
116
117 groovy-send-definition-and-go, groovy-send-region-and-go,
118 switch to the groovy process buffer after sending their text.
119 For information on running multiple processes in multiple buffers, see
120 documentation for variable groovy-buffer.
121
122 Commands:
123 Return after the end of the process' output sends the text from the
124 end of process to point.
125 Return before the end of the process' output copies the sexp ending at point
126 to the end of the process' output, and sends it.
127 Delete converts tabs to spaces as it moves back.
128 Tab indents for groovy; with argument, shifts rest
129 of expression rigidly with the current line.
130 C-M-q does Tab on each line starting within following expression.
131 Paragraphs are separated only by blank lines. # start comments.
132 If you accidentally suspend your process, use \\[comint-continue-subjob]
133 to continue it."
134 (interactive)
135 (comint-mode)
136 ;; Customise in inferior-groovy-mode-hook
137 (setq comint-prompt-regexp inferior-groovy-prompt-pattern)
138 ;; (groovy-mode-variables)
139 (setq major-mode 'inferior-groovy-mode)
140 (setq mode-name "Inferior Groovy")
141 (setq mode-line-process '(":%s"))
142 (use-local-map inferior-groovy-mode-map)
143 (define-key inferior-groovy-mode-map "\C-c\C-m" 'inferior-groovy-newline-and-go)
144 (setq comint-input-filter (function groovy-input-filter))
145 (setq comint-get-old-input (function groovy-get-old-input))
146 (setq comint-use-prompt-regexp t) ;; added v2
147 (setq comint-process-echoes t) ;; added v2
148 (setq comint-eol-on-send t) ;; added v2
149 (compilation-shell-minor-mode t)
150 (make-local-variable 'compilation-error-regexp-alist)
151 (setq compilation-error-regexp-alist inferior-groovy-error-regexp-alist)
152 (run-hooks 'inferior-groovy-mode-hook))
153
154 (defvar inferior-groovy-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
155 "*Input matching this regexp are not saved on the history list.
156 Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters.")
157
158 (defun inferior-groovy-newline-and-go()
159 (interactive)
160 (comint-send-input)
161 (let ((proc (groovy-proc)))
162 (comint-send-string proc "go\n")))
163
164 (defun groovy-input-filter (str)
165 "Don't save anything matching inferior-groovy-filter-regexp"
166 (not (string-match inferior-groovy-filter-regexp str)))
167
168 ;; adapted from replace-in-string in XEmacs (subr.el)
169 (defun remove-in-string (str regexp)
170 "Remove all matches in STR for REGEXP and returns the new string."
171 (let ((rtn-str "") (start 0) match prev-start)
172 (while (setq match (string-match regexp str start))
173 (setq prev-start start
174 start (match-end 0)
175 rtn-str (concat rtn-str (substring str prev-start match))))
176 (concat rtn-str (substring str start))))
177
178 (defun groovy-get-old-input ()
179 "Snarf the sexp ending at point"
180 (save-excursion
181 (let ((end (point)))
182 (re-search-backward inferior-groovy-first-prompt-pattern)
183 (remove-in-string (buffer-substring (point) end)
184 inferior-groovy-prompt-pattern)
185 )))
186
187 (defun groovy-args-to-list (string)
188 (let ((where (string-match "[ \t]" string)))
189 (cond ((null where) (list string))
190 ((not (= where 0))
191 (cons (substring string 0 where)
192 (groovy-args-to-list (substring string (+ 1 where)
193 (length string)))))
194 (t (let ((pos (string-match "[^ \t]" string)))
195 (if (null pos)
196 nil
197 (groovy-args-to-list (substring string pos
198 (length string)))))))))
199
200 (defun run-groovy (cmd)
201 "Run an inferior Groovy process, input and output via buffer *groovy*.
202 If there is a process already running in `*groovy*', switch to that buffer.
203 With argument, allows you to edit the command line (default is value
204 of `groovy-program-name'). Runs the hooks `inferior-groovy-mode-hook'
205 \(after the `comint-mode-hook' is run).
206 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
207
208 (interactive (list (if current-prefix-arg
209 (read-string "Run Groovy: " groovy-program-name)
210 (concat groovy-home "/bin/" groovy-program-name))))
211 (if (not (comint-check-proc "*groovy*"))
212 (let ((cmdlist (groovy-args-to-list cmd)))
213 (set-buffer (apply 'make-comint "groovy" (car cmdlist)
214 nil (cdr cmdlist)))
215 (inferior-groovy-mode)))
216 ;(setq groovy-program-name cmd)
217 (setq groovy-buffer "*groovy*")
218 (pop-to-buffer "*groovy*")
219 (get-buffer-process groovy-buffer)
220 )
221
222
223 (defun groovy-proc ()
224 "Returns the current groovy process. See variable groovy-buffer."
225 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-groovy-mode)
226 (current-buffer)
227 groovy-buffer))))
228 (or proc
229 (call-interactively 'run-groovy))))
230
231 ;;; was (error "No current process. See variable groovy-buffer"))))
232
233 (defun groovy-send-region (start end)
234 "Send the current region to the inferior Groovy process."
235 (interactive "r")
236
237 (save-excursion
238 (save-restriction
239 (let (( str (concat (buffer-substring start end) "\n"))
240 (proc (groovy-proc)))
241
242 (with-current-buffer (process-buffer proc)
243 (while (and
244 (goto-char comint-last-input-end)
245 (not (re-search-forward comint-prompt-regexp nil t))
246 (accept-process-output proc)))
247 (goto-char (process-mark proc))
248 (insert-before-markers str)
249 (move-marker comint-last-input-end (point))
250 (comint-send-string proc str)
251 (comint-send-string proc "go\n")
252 )
253 )
254 )))
255
256
257
258 (defun groovy-send-definition ()
259 "Send the current definition to the inferior Groovy process."
260 (interactive)
261 (save-excursion
262 (c-end-of-defun)
263 (let ((end (point)))
264 (c-beginning-of-defun)
265 (groovy-send-region (point) end))))
266
267 (defun groovy-send-last-sexp ()
268 "Send the previous sexp to the inferior Groovy process."
269 (interactive)
270 (groovy-send-region (save-excursion (backward-sexp) (point)) (point)))
271
272 ;; v2. current groovy-mode does not support beginning-of-block, end-of-block
273 ;; (defun groovy-send-block ()
274 ;; "Send the current block to the inferior Groovy process."
275 ;; (interactive)
276 ;; (save-excursion
277 ;; (groovy-end-of-block)
278 ;; (end-of-line)
279 ;; (let ((end (point)))
280 ;; (groovy-beginning-of-block)
281 ;; (groovy-send-region (point) end))))
282
283 (defun switch-to-groovy (eob-p)
284 "Switch to the groovy process buffer.
285 With argument, positions cursor at end of buffer."
286 (interactive "P")
287 (if (get-buffer groovy-buffer)
288 (pop-to-buffer groovy-buffer)
289 (error "No current process buffer. See variable groovy-buffer."))
290 (cond (eob-p
291 (push-mark)
292 (goto-char (point-max)))))
293
294 (defun groovy-send-region-and-go (start end)
295 "Send the current region to the inferior Groovy process.
296 Then switch to the process buffer."
297 (interactive "r")
298 (groovy-send-region start end)
299
300 (switch-to-groovy t))
301
302 (defun groovy-send-definition-and-go ()
303 "Send the current definition to the inferior Groovy.
304 Then switch to the process buffer."
305 (interactive)
306 (groovy-send-definition)
307 (switch-to-groovy t))
308
309 ;; (defun groovy-send-block-and-go ()
310 ;; "Send the current block to the inferior Groovy.
311 ;; Then switch to the process buffer."
312 ;; (interactive)
313 ;; (groovy-send-block)
314 ;; (switch-to-groovy t))
315
316 (defvar groovy-source-modes '(groovy-mode)
317 "*Used to determine if a buffer contains Groovy source code.
318 If it's loaded into a buffer that is in one of these major modes, it's
319 considered a groovy source file by groovy-load-file.
320 Used by these commands to determine defaults.")
321
322 (defvar groovy-prev-l/c-dir/file nil
323 "Caches the last (directory . file) pair.
324 Caches the last pair used in the last groovy-load-file command.
325 Used for determining the default in the
326 next one.")
327
328 (defun groovy-load-file (file-name)
329 "Load a Groovy file into the inferior Groovy process."
330 (interactive (comint-get-source "Load Groovy file: " groovy-prev-l/c-dir/file
331 groovy-source-modes t)) ; T because LOAD
332 ; needs an exact name
333 (comint-check-source file-name) ; Check to see if buffer needs saved.
334 (setq groovy-prev-l/c-dir/file (cons (file-name-directory file-name)
335 (file-name-nondirectory file-name)))
336 (comint-send-string (groovy-proc) (concat "\\i "
337 file-name
338 "\n")))
339 ;;; Do the user's customisation...
340
341 (defvar inf-groovy-load-hook nil
342 "This hook is run when inf-groovy is loaded in.
343 This is a good place to put keybindings.")
344
345 (run-hooks 'inf-groovy-load-hook)
346
347 (provide 'inf-groovy)
348
349 ;;; inf-groovy.el ends here